backlight: gpio_backlight: use devm_backlight_device_register()
[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
35f96162
JH
8#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9
1da177e4
LT
10#include <linux/module.h>
11#include <linux/init.h>
12#include <linux/device.h>
13#include <linux/backlight.h>
14#include <linux/notifier.h>
15#include <linux/ctype.h>
16#include <linux/err.h>
17#include <linux/fb.h>
5a0e3ad6 18#include <linux/slab.h>
1da177e4 19
321709c5
RP
20#ifdef CONFIG_PMAC_BACKLIGHT
21#include <asm/backlight.h>
22#endif
3d5eeadd 23
c338bfb5 24static const char *const backlight_types[] = {
bb7ca747
MG
25 [BACKLIGHT_RAW] = "raw",
26 [BACKLIGHT_PLATFORM] = "platform",
27 [BACKLIGHT_FIRMWARE] = "firmware",
28};
29
3d5eeadd
JS
30#if defined(CONFIG_FB) || (defined(CONFIG_FB_MODULE) && \
31 defined(CONFIG_BACKLIGHT_CLASS_DEVICE_MODULE))
32/* This callback gets called when something important happens inside a
33 * framebuffer driver. We're looking if that important event is blanking,
34 * and if it is, we're switching backlight power as well ...
35 */
36static int fb_notifier_callback(struct notifier_block *self,
37 unsigned long event, void *data)
38{
39 struct backlight_device *bd;
40 struct fb_event *evdata = data;
41
42 /* If we aren't interested in this event, skip it immediately ... */
994efacd 43 if (event != FB_EVENT_BLANK && event != FB_EVENT_CONBLANK)
3d5eeadd
JS
44 return 0;
45
46 bd = container_of(self, struct backlight_device, fb_notif);
599a52d1
RP
47 mutex_lock(&bd->ops_lock);
48 if (bd->ops)
49 if (!bd->ops->check_fb ||
57e148b6 50 bd->ops->check_fb(bd, evdata->info)) {
599a52d1 51 bd->props.fb_blank = *(int *)evdata->data;
c835ee7f
RP
52 if (bd->props.fb_blank == FB_BLANK_UNBLANK)
53 bd->props.state &= ~BL_CORE_FBBLANK;
54 else
55 bd->props.state |= BL_CORE_FBBLANK;
28ee086d 56 backlight_update_status(bd);
3d5eeadd 57 }
599a52d1 58 mutex_unlock(&bd->ops_lock);
3d5eeadd
JS
59 return 0;
60}
61
62static int backlight_register_fb(struct backlight_device *bd)
63{
64 memset(&bd->fb_notif, 0, sizeof(bd->fb_notif));
65 bd->fb_notif.notifier_call = fb_notifier_callback;
66
67 return fb_register_client(&bd->fb_notif);
68}
69
70static void backlight_unregister_fb(struct backlight_device *bd)
71{
72 fb_unregister_client(&bd->fb_notif);
73}
74#else
75static inline int backlight_register_fb(struct backlight_device *bd)
76{
77 return 0;
78}
79
80static inline void backlight_unregister_fb(struct backlight_device *bd)
81{
82}
83#endif /* CONFIG_FB */
84
325253a6
MG
85static void backlight_generate_event(struct backlight_device *bd,
86 enum backlight_update_reason reason)
87{
88 char *envp[2];
89
90 switch (reason) {
91 case BACKLIGHT_UPDATE_SYSFS:
92 envp[0] = "SOURCE=sysfs";
93 break;
94 case BACKLIGHT_UPDATE_HOTKEY:
95 envp[0] = "SOURCE=hotkey";
96 break;
97 default:
98 envp[0] = "SOURCE=unknown";
99 break;
100 }
101 envp[1] = NULL;
102 kobject_uevent_env(&bd->dev.kobj, KOBJ_CHANGE, envp);
89dfc28c 103 sysfs_notify(&bd->dev.kobj, NULL, "actual_brightness");
325253a6
MG
104}
105
ea1bb706
GKH
106static ssize_t bl_power_show(struct device *dev, struct device_attribute *attr,
107 char *buf)
1da177e4 108{
655bfd7a 109 struct backlight_device *bd = to_backlight_device(dev);
1da177e4 110
599a52d1 111 return sprintf(buf, "%d\n", bd->props.power);
1da177e4
LT
112}
113
ea1bb706
GKH
114static ssize_t bl_power_store(struct device *dev, struct device_attribute *attr,
115 const char *buf, size_t count)
1da177e4 116{
9a2c61a9 117 int rc;
655bfd7a 118 struct backlight_device *bd = to_backlight_device(dev);
9a2c61a9 119 unsigned long power;
1da177e4 120
66655760 121 rc = kstrtoul(buf, 0, &power);
9a2c61a9
PM
122 if (rc)
123 return rc;
1da177e4 124
9a2c61a9 125 rc = -ENXIO;
599a52d1
RP
126 mutex_lock(&bd->ops_lock);
127 if (bd->ops) {
35f96162 128 pr_debug("set power to %lu\n", power);
51552453
HD
129 if (bd->props.power != power) {
130 bd->props.power = power;
131 backlight_update_status(bd);
132 }
1da177e4 133 rc = count;
6ca01765 134 }
599a52d1 135 mutex_unlock(&bd->ops_lock);
1da177e4
LT
136
137 return rc;
138}
ea1bb706 139static DEVICE_ATTR_RW(bl_power);
1da177e4 140
ea1bb706 141static ssize_t brightness_show(struct device *dev,
655bfd7a 142 struct device_attribute *attr, char *buf)
1da177e4 143{
655bfd7a 144 struct backlight_device *bd = to_backlight_device(dev);
1da177e4 145
599a52d1 146 return sprintf(buf, "%d\n", bd->props.brightness);
1da177e4
LT
147}
148
ea1bb706 149static ssize_t brightness_store(struct device *dev,
655bfd7a 150 struct device_attribute *attr, const char *buf, size_t count)
1da177e4 151{
9a2c61a9 152 int rc;
655bfd7a 153 struct backlight_device *bd = to_backlight_device(dev);
9a2c61a9 154 unsigned long brightness;
1da177e4 155
66655760 156 rc = kstrtoul(buf, 0, &brightness);
9a2c61a9
PM
157 if (rc)
158 return rc;
159
160 rc = -ENXIO;
1da177e4 161
599a52d1
RP
162 mutex_lock(&bd->ops_lock);
163 if (bd->ops) {
164 if (brightness > bd->props.max_brightness)
6ca01765
RP
165 rc = -EINVAL;
166 else {
35f96162 167 pr_debug("set brightness to %lu\n", brightness);
9be1df98
ZR
168 bd->props.brightness = brightness;
169 backlight_update_status(bd);
6ca01765
RP
170 rc = count;
171 }
172 }
599a52d1 173 mutex_unlock(&bd->ops_lock);
1da177e4 174
325253a6
MG
175 backlight_generate_event(bd, BACKLIGHT_UPDATE_SYSFS);
176
1da177e4
LT
177 return rc;
178}
ea1bb706 179static DEVICE_ATTR_RW(brightness);
1da177e4 180
ea1bb706
GKH
181static ssize_t type_show(struct device *dev, struct device_attribute *attr,
182 char *buf)
bb7ca747
MG
183{
184 struct backlight_device *bd = to_backlight_device(dev);
185
186 return sprintf(buf, "%s\n", backlight_types[bd->props.type]);
187}
ea1bb706 188static DEVICE_ATTR_RO(type);
bb7ca747 189
ea1bb706 190static ssize_t max_brightness_show(struct device *dev,
655bfd7a 191 struct device_attribute *attr, char *buf)
1da177e4 192{
655bfd7a 193 struct backlight_device *bd = to_backlight_device(dev);
1da177e4 194
599a52d1 195 return sprintf(buf, "%d\n", bd->props.max_brightness);
6ca01765 196}
ea1bb706 197static DEVICE_ATTR_RO(max_brightness);
6ca01765 198
ea1bb706 199static ssize_t actual_brightness_show(struct device *dev,
655bfd7a 200 struct device_attribute *attr, char *buf)
6ca01765
RP
201{
202 int rc = -ENXIO;
655bfd7a 203 struct backlight_device *bd = to_backlight_device(dev);
6ca01765 204
599a52d1
RP
205 mutex_lock(&bd->ops_lock);
206 if (bd->ops && bd->ops->get_brightness)
207 rc = sprintf(buf, "%d\n", bd->ops->get_brightness(bd));
208 mutex_unlock(&bd->ops_lock);
1da177e4
LT
209
210 return rc;
211}
ea1bb706 212static DEVICE_ATTR_RO(actual_brightness);
1da177e4 213
0ad90efd 214static struct class *backlight_class;
655bfd7a 215
3601792e
SK
216#ifdef CONFIG_PM_SLEEP
217static int backlight_suspend(struct device *dev)
c835ee7f
RP
218{
219 struct backlight_device *bd = to_backlight_device(dev);
220
d1d73578
UKK
221 mutex_lock(&bd->ops_lock);
222 if (bd->ops && bd->ops->options & BL_CORE_SUSPENDRESUME) {
c835ee7f
RP
223 bd->props.state |= BL_CORE_SUSPENDED;
224 backlight_update_status(bd);
c835ee7f 225 }
d1d73578 226 mutex_unlock(&bd->ops_lock);
c835ee7f
RP
227
228 return 0;
229}
230
231static int backlight_resume(struct device *dev)
232{
233 struct backlight_device *bd = to_backlight_device(dev);
234
d1d73578
UKK
235 mutex_lock(&bd->ops_lock);
236 if (bd->ops && bd->ops->options & BL_CORE_SUSPENDRESUME) {
c835ee7f
RP
237 bd->props.state &= ~BL_CORE_SUSPENDED;
238 backlight_update_status(bd);
c835ee7f 239 }
d1d73578 240 mutex_unlock(&bd->ops_lock);
c835ee7f
RP
241
242 return 0;
243}
3601792e
SK
244#endif
245
246static SIMPLE_DEV_PM_OPS(backlight_class_dev_pm_ops, backlight_suspend,
247 backlight_resume);
c835ee7f 248
655bfd7a 249static void bl_device_release(struct device *dev)
1da177e4
LT
250{
251 struct backlight_device *bd = to_backlight_device(dev);
252 kfree(bd);
253}
254
ea1bb706
GKH
255static struct attribute *bl_device_attrs[] = {
256 &dev_attr_bl_power.attr,
257 &dev_attr_brightness.attr,
258 &dev_attr_actual_brightness.attr,
259 &dev_attr_max_brightness.attr,
260 &dev_attr_type.attr,
261 NULL,
1da177e4 262};
ea1bb706 263ATTRIBUTE_GROUPS(bl_device);
1da177e4 264
325253a6
MG
265/**
266 * backlight_force_update - tell the backlight subsystem that hardware state
267 * has changed
268 * @bd: the backlight device to update
269 *
270 * Updates the internal state of the backlight in response to a hardware event,
271 * and generate a uevent to notify userspace
272 */
273void backlight_force_update(struct backlight_device *bd,
274 enum backlight_update_reason reason)
275{
276 mutex_lock(&bd->ops_lock);
277 if (bd->ops && bd->ops->get_brightness)
278 bd->props.brightness = bd->ops->get_brightness(bd);
279 mutex_unlock(&bd->ops_lock);
280 backlight_generate_event(bd, reason);
281}
282EXPORT_SYMBOL(backlight_force_update);
283
1da177e4
LT
284/**
285 * backlight_device_register - create and register a new object of
286 * backlight_device class.
287 * @name: the name of the new object(must be the same as the name of the
288 * respective framebuffer device).
f6ec2d96 289 * @parent: a pointer to the parent device
655bfd7a
RP
290 * @devdata: an optional pointer to be stored for private driver use. The
291 * methods may retrieve it by using bl_get_data(bd).
599a52d1 292 * @ops: the backlight operations structure.
1da177e4 293 *
655bfd7a 294 * Creates and registers new backlight device. Returns either an
1da177e4
LT
295 * ERR_PTR() or a pointer to the newly allocated device.
296 */
519ab5f2 297struct backlight_device *backlight_device_register(const char *name,
a19a6ee6
MG
298 struct device *parent, void *devdata, const struct backlight_ops *ops,
299 const struct backlight_properties *props)
1da177e4 300{
1da177e4 301 struct backlight_device *new_bd;
655bfd7a 302 int rc;
1da177e4 303
655bfd7a 304 pr_debug("backlight_device_register: name=%s\n", name);
1da177e4 305
599a52d1 306 new_bd = kzalloc(sizeof(struct backlight_device), GFP_KERNEL);
90968e8e 307 if (!new_bd)
10ad1b73 308 return ERR_PTR(-ENOMEM);
1da177e4 309
28ee086d 310 mutex_init(&new_bd->update_lock);
599a52d1 311 mutex_init(&new_bd->ops_lock);
1da177e4 312
655bfd7a
RP
313 new_bd->dev.class = backlight_class;
314 new_bd->dev.parent = parent;
315 new_bd->dev.release = bl_device_release;
02aa2a37 316 dev_set_name(&new_bd->dev, "%s", name);
655bfd7a
RP
317 dev_set_drvdata(&new_bd->dev, devdata);
318
a19a6ee6 319 /* Set default properties */
bb7ca747 320 if (props) {
a19a6ee6
MG
321 memcpy(&new_bd->props, props,
322 sizeof(struct backlight_properties));
bb7ca747
MG
323 if (props->type <= 0 || props->type >= BACKLIGHT_TYPE_MAX) {
324 WARN(1, "%s: invalid backlight type", name);
325 new_bd->props.type = BACKLIGHT_RAW;
326 }
327 } else {
328 new_bd->props.type = BACKLIGHT_RAW;
329 }
a19a6ee6 330
655bfd7a 331 rc = device_register(&new_bd->dev);
90968e8e 332 if (rc) {
2fd5a154 333 kfree(new_bd);
1da177e4
LT
334 return ERR_PTR(rc);
335 }
336
3d5eeadd 337 rc = backlight_register_fb(new_bd);
2fd5a154 338 if (rc) {
655bfd7a 339 device_unregister(&new_bd->dev);
2fd5a154
DT
340 return ERR_PTR(rc);
341 }
342
655bfd7a 343 new_bd->ops = ops;
1da177e4 344
321709c5
RP
345#ifdef CONFIG_PMAC_BACKLIGHT
346 mutex_lock(&pmac_backlight_mutex);
347 if (!pmac_backlight)
348 pmac_backlight = new_bd;
349 mutex_unlock(&pmac_backlight_mutex);
350#endif
351
1da177e4
LT
352 return new_bd;
353}
354EXPORT_SYMBOL(backlight_device_register);
355
356/**
357 * backlight_device_unregister - unregisters a backlight device object.
358 * @bd: the backlight device object to be unregistered and freed.
359 *
360 * Unregisters a previously registered via backlight_device_register object.
361 */
362void backlight_device_unregister(struct backlight_device *bd)
363{
1da177e4
LT
364 if (!bd)
365 return;
366
321709c5
RP
367#ifdef CONFIG_PMAC_BACKLIGHT
368 mutex_lock(&pmac_backlight_mutex);
369 if (pmac_backlight == bd)
370 pmac_backlight = NULL;
371 mutex_unlock(&pmac_backlight_mutex);
372#endif
599a52d1
RP
373 mutex_lock(&bd->ops_lock);
374 bd->ops = NULL;
375 mutex_unlock(&bd->ops_lock);
1da177e4 376
3d5eeadd 377 backlight_unregister_fb(bd);
655bfd7a 378 device_unregister(&bd->dev);
1da177e4
LT
379}
380EXPORT_SYMBOL(backlight_device_unregister);
381
8318fde4
JH
382static void devm_backlight_device_release(struct device *dev, void *res)
383{
384 struct backlight_device *backlight = *(struct backlight_device **)res;
385
386 backlight_device_unregister(backlight);
387}
388
389static int devm_backlight_device_match(struct device *dev, void *res,
390 void *data)
391{
392 struct backlight_device **r = res;
393
394 return *r == data;
395}
396
397/**
398 * devm_backlight_device_register - resource managed backlight_device_register()
399 * @dev: the device to register
400 * @name: the name of the device
401 * @parent: a pointer to the parent device
402 * @devdata: an optional pointer to be stored for private driver use
403 * @ops: the backlight operations structure
404 * @props: the backlight properties
405 *
406 * @return a struct backlight on success, or an ERR_PTR on error
407 *
408 * Managed backlight_device_register(). The backlight_device returned
409 * from this function are automatically freed on driver detach.
410 * See backlight_device_register() for more information.
411 */
412struct backlight_device *devm_backlight_device_register(struct device *dev,
413 const char *name, struct device *parent, void *devdata,
414 const struct backlight_ops *ops,
415 const struct backlight_properties *props)
416{
417 struct backlight_device **ptr, *backlight;
418
419 ptr = devres_alloc(devm_backlight_device_release, sizeof(*ptr),
420 GFP_KERNEL);
421 if (!ptr)
422 return ERR_PTR(-ENOMEM);
423
424 backlight = backlight_device_register(name, parent, devdata, ops,
425 props);
426 if (!IS_ERR(backlight)) {
427 *ptr = backlight;
428 devres_add(dev, ptr);
429 } else {
430 devres_free(ptr);
431 }
432
433 return backlight;
434}
435EXPORT_SYMBOL(devm_backlight_device_register);
436
437/**
438 * devm_backlight_device_unregister - resource managed backlight_device_unregister()
439 * @dev: the device to unregister
440 * @bd: the backlight device to unregister
441 *
442 * Deallocated a backlight allocated with devm_backlight_device_register().
443 * Normally this function will not need to be called and the resource management
444 * code will ensure that the resource is freed.
445 */
446void devm_backlight_device_unregister(struct device *dev,
447 struct backlight_device *bd)
448{
449 int rc;
450
451 rc = devres_release(dev, devm_backlight_device_release,
452 devm_backlight_device_match, bd);
453 WARN_ON(rc);
454}
455EXPORT_SYMBOL(devm_backlight_device_unregister);
456
762a936f 457#ifdef CONFIG_OF
3213f631 458static int of_parent_match(struct device *dev, const void *data)
762a936f
TR
459{
460 return dev->parent && dev->parent->of_node == data;
461}
462
463/**
464 * of_find_backlight_by_node() - find backlight device by device-tree node
465 * @node: device-tree node of the backlight device
466 *
467 * Returns a pointer to the backlight device corresponding to the given DT
468 * node or NULL if no such backlight device exists or if the device hasn't
469 * been probed yet.
470 *
471 * This function obtains a reference on the backlight device and it is the
472 * caller's responsibility to drop the reference by calling put_device() on
473 * the backlight device's .dev field.
474 */
475struct backlight_device *of_find_backlight_by_node(struct device_node *node)
476{
477 struct device *dev;
478
479 dev = class_find_device(backlight_class, NULL, node, of_parent_match);
480
481 return dev ? to_backlight_device(dev) : NULL;
482}
483EXPORT_SYMBOL(of_find_backlight_by_node);
484#endif
485
1da177e4
LT
486static void __exit backlight_class_exit(void)
487{
655bfd7a 488 class_destroy(backlight_class);
1da177e4
LT
489}
490
491static int __init backlight_class_init(void)
492{
655bfd7a
RP
493 backlight_class = class_create(THIS_MODULE, "backlight");
494 if (IS_ERR(backlight_class)) {
35f96162
JH
495 pr_warn("Unable to create backlight class; errno = %ld\n",
496 PTR_ERR(backlight_class));
655bfd7a
RP
497 return PTR_ERR(backlight_class);
498 }
499
ea1bb706 500 backlight_class->dev_groups = bl_device_groups;
3601792e 501 backlight_class->pm = &backlight_class_dev_pm_ops;
655bfd7a 502 return 0;
1da177e4
LT
503}
504
505/*
506 * if this is compiled into the kernel, we need to ensure that the
507 * class is registered before users of the class try to register lcd's
508 */
509postcore_initcall(backlight_class_init);
510module_exit(backlight_class_exit);
511
512MODULE_LICENSE("GPL");
513MODULE_AUTHOR("Jamey Hicks <jamey.hicks@hp.com>, Andrew Zabolotny <zap@homelink.ru>");
514MODULE_DESCRIPTION("Backlight Lowlevel Control Abstraction");
This page took 0.92178 seconds and 5 git commands to generate.