backlight: use kstrtoul()
[deliverable/linux.git] / drivers / video / backlight / backlight.c
CommitLineData
1da177e4
LT
1/*
2 * Backlight Lowlevel Control Abstraction
3 *
4 * Copyright (C) 2003,2004 Hewlett-Packard Company
5 *
6 */
7
1da177e4
LT
8#include <linux/module.h>
9#include <linux/init.h>
10#include <linux/device.h>
11#include <linux/backlight.h>
12#include <linux/notifier.h>
13#include <linux/ctype.h>
14#include <linux/err.h>
15#include <linux/fb.h>
5a0e3ad6 16#include <linux/slab.h>
1da177e4 17
321709c5
RP
18#ifdef CONFIG_PMAC_BACKLIGHT
19#include <asm/backlight.h>
20#endif
3d5eeadd 21
c338bfb5 22static const char *const backlight_types[] = {
bb7ca747
MG
23 [BACKLIGHT_RAW] = "raw",
24 [BACKLIGHT_PLATFORM] = "platform",
25 [BACKLIGHT_FIRMWARE] = "firmware",
26};
27
3d5eeadd
JS
28#if defined(CONFIG_FB) || (defined(CONFIG_FB_MODULE) && \
29 defined(CONFIG_BACKLIGHT_CLASS_DEVICE_MODULE))
30/* This callback gets called when something important happens inside a
31 * framebuffer driver. We're looking if that important event is blanking,
32 * and if it is, we're switching backlight power as well ...
33 */
34static int fb_notifier_callback(struct notifier_block *self,
35 unsigned long event, void *data)
36{
37 struct backlight_device *bd;
38 struct fb_event *evdata = data;
39
40 /* If we aren't interested in this event, skip it immediately ... */
994efacd 41 if (event != FB_EVENT_BLANK && event != FB_EVENT_CONBLANK)
3d5eeadd
JS
42 return 0;
43
44 bd = container_of(self, struct backlight_device, fb_notif);
599a52d1
RP
45 mutex_lock(&bd->ops_lock);
46 if (bd->ops)
47 if (!bd->ops->check_fb ||
57e148b6 48 bd->ops->check_fb(bd, evdata->info)) {
599a52d1 49 bd->props.fb_blank = *(int *)evdata->data;
c835ee7f
RP
50 if (bd->props.fb_blank == FB_BLANK_UNBLANK)
51 bd->props.state &= ~BL_CORE_FBBLANK;
52 else
53 bd->props.state |= BL_CORE_FBBLANK;
28ee086d 54 backlight_update_status(bd);
3d5eeadd 55 }
599a52d1 56 mutex_unlock(&bd->ops_lock);
3d5eeadd
JS
57 return 0;
58}
59
60static int backlight_register_fb(struct backlight_device *bd)
61{
62 memset(&bd->fb_notif, 0, sizeof(bd->fb_notif));
63 bd->fb_notif.notifier_call = fb_notifier_callback;
64
65 return fb_register_client(&bd->fb_notif);
66}
67
68static void backlight_unregister_fb(struct backlight_device *bd)
69{
70 fb_unregister_client(&bd->fb_notif);
71}
72#else
73static inline int backlight_register_fb(struct backlight_device *bd)
74{
75 return 0;
76}
77
78static inline void backlight_unregister_fb(struct backlight_device *bd)
79{
80}
81#endif /* CONFIG_FB */
82
325253a6
MG
83static void backlight_generate_event(struct backlight_device *bd,
84 enum backlight_update_reason reason)
85{
86 char *envp[2];
87
88 switch (reason) {
89 case BACKLIGHT_UPDATE_SYSFS:
90 envp[0] = "SOURCE=sysfs";
91 break;
92 case BACKLIGHT_UPDATE_HOTKEY:
93 envp[0] = "SOURCE=hotkey";
94 break;
95 default:
96 envp[0] = "SOURCE=unknown";
97 break;
98 }
99 envp[1] = NULL;
100 kobject_uevent_env(&bd->dev.kobj, KOBJ_CHANGE, envp);
89dfc28c 101 sysfs_notify(&bd->dev.kobj, NULL, "actual_brightness");
325253a6
MG
102}
103
655bfd7a 104static ssize_t backlight_show_power(struct device *dev,
66655760 105 struct device_attribute *attr, char *buf)
1da177e4 106{
655bfd7a 107 struct backlight_device *bd = to_backlight_device(dev);
1da177e4 108
599a52d1 109 return sprintf(buf, "%d\n", bd->props.power);
1da177e4
LT
110}
111
655bfd7a
RP
112static ssize_t backlight_store_power(struct device *dev,
113 struct device_attribute *attr, const char *buf, size_t count)
1da177e4 114{
9a2c61a9 115 int rc;
655bfd7a 116 struct backlight_device *bd = to_backlight_device(dev);
9a2c61a9 117 unsigned long power;
1da177e4 118
66655760 119 rc = kstrtoul(buf, 0, &power);
9a2c61a9
PM
120 if (rc)
121 return rc;
1da177e4 122
9a2c61a9 123 rc = -ENXIO;
599a52d1
RP
124 mutex_lock(&bd->ops_lock);
125 if (bd->ops) {
9a2c61a9 126 pr_debug("backlight: set power to %lu\n", power);
51552453
HD
127 if (bd->props.power != power) {
128 bd->props.power = power;
129 backlight_update_status(bd);
130 }
1da177e4 131 rc = count;
6ca01765 132 }
599a52d1 133 mutex_unlock(&bd->ops_lock);
1da177e4
LT
134
135 return rc;
136}
137
655bfd7a
RP
138static ssize_t backlight_show_brightness(struct device *dev,
139 struct device_attribute *attr, char *buf)
1da177e4 140{
655bfd7a 141 struct backlight_device *bd = to_backlight_device(dev);
1da177e4 142
599a52d1 143 return sprintf(buf, "%d\n", bd->props.brightness);
1da177e4
LT
144}
145
655bfd7a
RP
146static ssize_t backlight_store_brightness(struct device *dev,
147 struct device_attribute *attr, const char *buf, size_t count)
1da177e4 148{
9a2c61a9 149 int rc;
655bfd7a 150 struct backlight_device *bd = to_backlight_device(dev);
9a2c61a9 151 unsigned long brightness;
1da177e4 152
66655760 153 rc = kstrtoul(buf, 0, &brightness);
9a2c61a9
PM
154 if (rc)
155 return rc;
156
157 rc = -ENXIO;
1da177e4 158
599a52d1
RP
159 mutex_lock(&bd->ops_lock);
160 if (bd->ops) {
161 if (brightness > bd->props.max_brightness)
6ca01765
RP
162 rc = -EINVAL;
163 else {
9a2c61a9 164 pr_debug("backlight: set brightness to %lu\n",
6ca01765 165 brightness);
9be1df98
ZR
166 bd->props.brightness = brightness;
167 backlight_update_status(bd);
6ca01765
RP
168 rc = count;
169 }
170 }
599a52d1 171 mutex_unlock(&bd->ops_lock);
1da177e4 172
325253a6
MG
173 backlight_generate_event(bd, BACKLIGHT_UPDATE_SYSFS);
174
1da177e4
LT
175 return rc;
176}
177
bb7ca747
MG
178static ssize_t backlight_show_type(struct device *dev,
179 struct device_attribute *attr, char *buf)
180{
181 struct backlight_device *bd = to_backlight_device(dev);
182
183 return sprintf(buf, "%s\n", backlight_types[bd->props.type]);
184}
185
655bfd7a
RP
186static ssize_t backlight_show_max_brightness(struct device *dev,
187 struct device_attribute *attr, char *buf)
1da177e4 188{
655bfd7a 189 struct backlight_device *bd = to_backlight_device(dev);
1da177e4 190
599a52d1 191 return sprintf(buf, "%d\n", bd->props.max_brightness);
6ca01765
RP
192}
193
655bfd7a
RP
194static ssize_t backlight_show_actual_brightness(struct device *dev,
195 struct device_attribute *attr, char *buf)
6ca01765
RP
196{
197 int rc = -ENXIO;
655bfd7a 198 struct backlight_device *bd = to_backlight_device(dev);
6ca01765 199
599a52d1
RP
200 mutex_lock(&bd->ops_lock);
201 if (bd->ops && bd->ops->get_brightness)
202 rc = sprintf(buf, "%d\n", bd->ops->get_brightness(bd));
203 mutex_unlock(&bd->ops_lock);
1da177e4
LT
204
205 return rc;
206}
207
0ad90efd 208static struct class *backlight_class;
655bfd7a 209
c835ee7f
RP
210static int backlight_suspend(struct device *dev, pm_message_t state)
211{
212 struct backlight_device *bd = to_backlight_device(dev);
213
d1d73578
UKK
214 mutex_lock(&bd->ops_lock);
215 if (bd->ops && bd->ops->options & BL_CORE_SUSPENDRESUME) {
c835ee7f
RP
216 bd->props.state |= BL_CORE_SUSPENDED;
217 backlight_update_status(bd);
c835ee7f 218 }
d1d73578 219 mutex_unlock(&bd->ops_lock);
c835ee7f
RP
220
221 return 0;
222}
223
224static int backlight_resume(struct device *dev)
225{
226 struct backlight_device *bd = to_backlight_device(dev);
227
d1d73578
UKK
228 mutex_lock(&bd->ops_lock);
229 if (bd->ops && bd->ops->options & BL_CORE_SUSPENDRESUME) {
c835ee7f
RP
230 bd->props.state &= ~BL_CORE_SUSPENDED;
231 backlight_update_status(bd);
c835ee7f 232 }
d1d73578 233 mutex_unlock(&bd->ops_lock);
c835ee7f
RP
234
235 return 0;
236}
237
655bfd7a 238static void bl_device_release(struct device *dev)
1da177e4
LT
239{
240 struct backlight_device *bd = to_backlight_device(dev);
241 kfree(bd);
242}
243
655bfd7a
RP
244static struct device_attribute bl_device_attributes[] = {
245 __ATTR(bl_power, 0644, backlight_show_power, backlight_store_power),
246 __ATTR(brightness, 0644, backlight_show_brightness,
6ca01765 247 backlight_store_brightness),
655bfd7a 248 __ATTR(actual_brightness, 0444, backlight_show_actual_brightness,
6ca01765 249 NULL),
655bfd7a 250 __ATTR(max_brightness, 0444, backlight_show_max_brightness, NULL),
bb7ca747 251 __ATTR(type, 0444, backlight_show_type, NULL),
655bfd7a 252 __ATTR_NULL,
1da177e4
LT
253};
254
325253a6
MG
255/**
256 * backlight_force_update - tell the backlight subsystem that hardware state
257 * has changed
258 * @bd: the backlight device to update
259 *
260 * Updates the internal state of the backlight in response to a hardware event,
261 * and generate a uevent to notify userspace
262 */
263void backlight_force_update(struct backlight_device *bd,
264 enum backlight_update_reason reason)
265{
266 mutex_lock(&bd->ops_lock);
267 if (bd->ops && bd->ops->get_brightness)
268 bd->props.brightness = bd->ops->get_brightness(bd);
269 mutex_unlock(&bd->ops_lock);
270 backlight_generate_event(bd, reason);
271}
272EXPORT_SYMBOL(backlight_force_update);
273
1da177e4
LT
274/**
275 * backlight_device_register - create and register a new object of
276 * backlight_device class.
277 * @name: the name of the new object(must be the same as the name of the
278 * respective framebuffer device).
f6ec2d96 279 * @parent: a pointer to the parent device
655bfd7a
RP
280 * @devdata: an optional pointer to be stored for private driver use. The
281 * methods may retrieve it by using bl_get_data(bd).
599a52d1 282 * @ops: the backlight operations structure.
1da177e4 283 *
655bfd7a 284 * Creates and registers new backlight device. Returns either an
1da177e4
LT
285 * ERR_PTR() or a pointer to the newly allocated device.
286 */
519ab5f2 287struct backlight_device *backlight_device_register(const char *name,
a19a6ee6
MG
288 struct device *parent, void *devdata, const struct backlight_ops *ops,
289 const struct backlight_properties *props)
1da177e4 290{
1da177e4 291 struct backlight_device *new_bd;
655bfd7a 292 int rc;
1da177e4 293
655bfd7a 294 pr_debug("backlight_device_register: name=%s\n", name);
1da177e4 295
599a52d1 296 new_bd = kzalloc(sizeof(struct backlight_device), GFP_KERNEL);
90968e8e 297 if (!new_bd)
10ad1b73 298 return ERR_PTR(-ENOMEM);
1da177e4 299
28ee086d 300 mutex_init(&new_bd->update_lock);
599a52d1 301 mutex_init(&new_bd->ops_lock);
1da177e4 302
655bfd7a
RP
303 new_bd->dev.class = backlight_class;
304 new_bd->dev.parent = parent;
305 new_bd->dev.release = bl_device_release;
64dba9a9 306 dev_set_name(&new_bd->dev, name);
655bfd7a
RP
307 dev_set_drvdata(&new_bd->dev, devdata);
308
a19a6ee6 309 /* Set default properties */
bb7ca747 310 if (props) {
a19a6ee6
MG
311 memcpy(&new_bd->props, props,
312 sizeof(struct backlight_properties));
bb7ca747
MG
313 if (props->type <= 0 || props->type >= BACKLIGHT_TYPE_MAX) {
314 WARN(1, "%s: invalid backlight type", name);
315 new_bd->props.type = BACKLIGHT_RAW;
316 }
317 } else {
318 new_bd->props.type = BACKLIGHT_RAW;
319 }
a19a6ee6 320
655bfd7a 321 rc = device_register(&new_bd->dev);
90968e8e 322 if (rc) {
2fd5a154 323 kfree(new_bd);
1da177e4
LT
324 return ERR_PTR(rc);
325 }
326
3d5eeadd 327 rc = backlight_register_fb(new_bd);
2fd5a154 328 if (rc) {
655bfd7a 329 device_unregister(&new_bd->dev);
2fd5a154
DT
330 return ERR_PTR(rc);
331 }
332
655bfd7a 333 new_bd->ops = ops;
1da177e4 334
321709c5
RP
335#ifdef CONFIG_PMAC_BACKLIGHT
336 mutex_lock(&pmac_backlight_mutex);
337 if (!pmac_backlight)
338 pmac_backlight = new_bd;
339 mutex_unlock(&pmac_backlight_mutex);
340#endif
341
1da177e4
LT
342 return new_bd;
343}
344EXPORT_SYMBOL(backlight_device_register);
345
346/**
347 * backlight_device_unregister - unregisters a backlight device object.
348 * @bd: the backlight device object to be unregistered and freed.
349 *
350 * Unregisters a previously registered via backlight_device_register object.
351 */
352void backlight_device_unregister(struct backlight_device *bd)
353{
1da177e4
LT
354 if (!bd)
355 return;
356
321709c5
RP
357#ifdef CONFIG_PMAC_BACKLIGHT
358 mutex_lock(&pmac_backlight_mutex);
359 if (pmac_backlight == bd)
360 pmac_backlight = NULL;
361 mutex_unlock(&pmac_backlight_mutex);
362#endif
599a52d1
RP
363 mutex_lock(&bd->ops_lock);
364 bd->ops = NULL;
365 mutex_unlock(&bd->ops_lock);
1da177e4 366
3d5eeadd 367 backlight_unregister_fb(bd);
655bfd7a 368 device_unregister(&bd->dev);
1da177e4
LT
369}
370EXPORT_SYMBOL(backlight_device_unregister);
371
372static void __exit backlight_class_exit(void)
373{
655bfd7a 374 class_destroy(backlight_class);
1da177e4
LT
375}
376
377static int __init backlight_class_init(void)
378{
655bfd7a
RP
379 backlight_class = class_create(THIS_MODULE, "backlight");
380 if (IS_ERR(backlight_class)) {
381 printk(KERN_WARNING "Unable to create backlight class; errno = %ld\n",
382 PTR_ERR(backlight_class));
383 return PTR_ERR(backlight_class);
384 }
385
386 backlight_class->dev_attrs = bl_device_attributes;
c835ee7f
RP
387 backlight_class->suspend = backlight_suspend;
388 backlight_class->resume = backlight_resume;
655bfd7a 389 return 0;
1da177e4
LT
390}
391
392/*
393 * if this is compiled into the kernel, we need to ensure that the
394 * class is registered before users of the class try to register lcd's
395 */
396postcore_initcall(backlight_class_init);
397module_exit(backlight_class_exit);
398
399MODULE_LICENSE("GPL");
400MODULE_AUTHOR("Jamey Hicks <jamey.hicks@hp.com>, Andrew Zabolotny <zap@homelink.ru>");
401MODULE_DESCRIPTION("Backlight Lowlevel Control Abstraction");
This page took 0.644606 seconds and 5 git commands to generate.