1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Backlight Lowlevel Control Abstraction
4 *
5 * Copyright (C) 2003,2004 Hewlett-Packard Company
6 *
7 */
8
9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10
11 #include <linux/module.h>
12 #include <linux/init.h>
13 #include <linux/device.h>
14 #include <linux/backlight.h>
15 #include <linux/notifier.h>
16 #include <linux/ctype.h>
17 #include <linux/err.h>
18 #include <linux/fb.h>
19 #include <linux/slab.h>
20
21 #ifdef CONFIG_PMAC_BACKLIGHT
22 #include <asm/backlight.h>
23 #endif
24
25 /**
26 * DOC: overview
27 *
28 * The backlight core supports implementing backlight drivers.
29 *
30 * A backlight driver registers a driver using
31 * devm_backlight_device_register(). The properties of the backlight
32 * driver such as type and max_brightness must be specified.
33 * When the core detect changes in for example brightness or power state
34 * the update_status() operation is called. The backlight driver shall
35 * implement this operation and use it to adjust backlight.
36 *
37 * Several sysfs attributes are provided by the backlight core::
38 *
39 * - brightness R/W, set the requested brightness level
40 * - actual_brightness RO, the brightness level used by the HW
41 * - max_brightness RO, the maximum brightness level supported
42 *
43 * See Documentation/ABI/stable/sysfs-class-backlight for the full list.
44 *
45 * The backlight can be adjusted using the sysfs interface, and
46 * the backlight driver may also support adjusting backlight using
47 * a hot-key or some other platform or firmware specific way.
48 *
49 * The driver must implement the get_brightness() operation if
50 * the HW do not support all the levels that can be specified in
51 * brightness, thus providing user-space access to the actual level
52 * via the actual_brightness attribute.
53 *
54 * When the backlight changes this is reported to user-space using
55 * an uevent connected to the actual_brightness attribute.
56 * When brightness is set by platform specific means, for example
57 * a hot-key to adjust backlight, the driver must notify the backlight
58 * core that brightness has changed using backlight_force_update().
59 *
60 * The backlight driver core receives notifications from fbdev and
61 * if the event is FB_EVENT_BLANK and if the value of blank, from the
62 * FBIOBLANK ioctrl, results in a change in the backlight state the
63 * update_status() operation is called.
64 */
65
66 static struct list_head backlight_dev_list;
67 static struct mutex backlight_dev_list_mutex;
68
69 static const char *const backlight_types[] = {
70 [BACKLIGHT_RAW] = "raw",
71 [BACKLIGHT_PLATFORM] = "platform",
72 [BACKLIGHT_FIRMWARE] = "firmware",
73 };
74
75 static const char *const backlight_scale_types[] = {
76 [BACKLIGHT_SCALE_UNKNOWN] = "unknown",
77 [BACKLIGHT_SCALE_LINEAR] = "linear",
78 [BACKLIGHT_SCALE_NON_LINEAR] = "non-linear",
79 };
80
81 #if defined(CONFIG_FB_CORE) || (defined(CONFIG_FB_CORE_MODULE) && \
82 defined(CONFIG_BACKLIGHT_CLASS_DEVICE_MODULE))
83 /*
84 * fb_notifier_callback
85 *
86 * This callback gets called when something important happens inside a
87 * framebuffer driver. The backlight core only cares about FB_BLANK_UNBLANK
88 * which is reported to the driver using backlight_update_status()
89 * as a state change.
90 *
91 * There may be several fbdev's connected to the backlight device,
92 * in which case they are kept track of. A state change is only reported
93 * if there is a change in backlight for the specified fbdev.
94 */
fb_notifier_callback(struct notifier_block * self,unsigned long event,void * data)95 static int fb_notifier_callback(struct notifier_block *self,
96 unsigned long event, void *data)
97 {
98 struct backlight_device *bd;
99 struct fb_event *evdata = data;
100 struct fb_info *info = evdata->info;
101 struct backlight_device *fb_bd = fb_bl_device(info);
102 int node = info->node;
103 int fb_blank = 0;
104
105 /* If we aren't interested in this event, skip it immediately ... */
106 if (event != FB_EVENT_BLANK)
107 return 0;
108
109 bd = container_of(self, struct backlight_device, fb_notif);
110 mutex_lock(&bd->ops_lock);
111
112 if (!bd->ops)
113 goto out;
114 if (bd->ops->controls_device && !bd->ops->controls_device(bd, info->device))
115 goto out;
116 if (fb_bd && fb_bd != bd)
117 goto out;
118
119 fb_blank = *(int *)evdata->data;
120 if (fb_blank == FB_BLANK_UNBLANK && !bd->fb_bl_on[node]) {
121 bd->fb_bl_on[node] = true;
122 if (!bd->use_count++) {
123 bd->props.state &= ~BL_CORE_FBBLANK;
124 backlight_update_status(bd);
125 }
126 } else if (fb_blank != FB_BLANK_UNBLANK && bd->fb_bl_on[node]) {
127 bd->fb_bl_on[node] = false;
128 if (!(--bd->use_count)) {
129 bd->props.state |= BL_CORE_FBBLANK;
130 backlight_update_status(bd);
131 }
132 }
133 out:
134 mutex_unlock(&bd->ops_lock);
135 return 0;
136 }
137
backlight_register_fb(struct backlight_device * bd)138 static int backlight_register_fb(struct backlight_device *bd)
139 {
140 memset(&bd->fb_notif, 0, sizeof(bd->fb_notif));
141 bd->fb_notif.notifier_call = fb_notifier_callback;
142
143 return fb_register_client(&bd->fb_notif);
144 }
145
backlight_unregister_fb(struct backlight_device * bd)146 static void backlight_unregister_fb(struct backlight_device *bd)
147 {
148 fb_unregister_client(&bd->fb_notif);
149 }
150 #else
backlight_register_fb(struct backlight_device * bd)151 static inline int backlight_register_fb(struct backlight_device *bd)
152 {
153 return 0;
154 }
155
backlight_unregister_fb(struct backlight_device * bd)156 static inline void backlight_unregister_fb(struct backlight_device *bd)
157 {
158 }
159 #endif /* CONFIG_FB_CORE */
160
backlight_generate_event(struct backlight_device * bd,enum backlight_update_reason reason)161 static void backlight_generate_event(struct backlight_device *bd,
162 enum backlight_update_reason reason)
163 {
164 char *envp[2];
165
166 switch (reason) {
167 case BACKLIGHT_UPDATE_SYSFS:
168 envp[0] = "SOURCE=sysfs";
169 break;
170 case BACKLIGHT_UPDATE_HOTKEY:
171 envp[0] = "SOURCE=hotkey";
172 break;
173 default:
174 envp[0] = "SOURCE=unknown";
175 break;
176 }
177 envp[1] = NULL;
178 kobject_uevent_env(&bd->dev.kobj, KOBJ_CHANGE, envp);
179 sysfs_notify(&bd->dev.kobj, NULL, "actual_brightness");
180 }
181
bl_power_show(struct device * dev,struct device_attribute * attr,char * buf)182 static ssize_t bl_power_show(struct device *dev, struct device_attribute *attr,
183 char *buf)
184 {
185 struct backlight_device *bd = to_backlight_device(dev);
186
187 return sprintf(buf, "%d\n", bd->props.power);
188 }
189
bl_power_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)190 static ssize_t bl_power_store(struct device *dev, struct device_attribute *attr,
191 const char *buf, size_t count)
192 {
193 int rc;
194 struct backlight_device *bd = to_backlight_device(dev);
195 unsigned long power, old_power;
196
197 rc = kstrtoul(buf, 0, &power);
198 if (rc)
199 return rc;
200
201 rc = -ENXIO;
202 mutex_lock(&bd->ops_lock);
203 if (bd->ops) {
204 pr_debug("set power to %lu\n", power);
205 if (bd->props.power != power) {
206 old_power = bd->props.power;
207 bd->props.power = power;
208 rc = backlight_update_status(bd);
209 if (rc)
210 bd->props.power = old_power;
211 else
212 rc = count;
213 } else {
214 rc = count;
215 }
216 }
217 mutex_unlock(&bd->ops_lock);
218
219 return rc;
220 }
221 static DEVICE_ATTR_RW(bl_power);
222
brightness_show(struct device * dev,struct device_attribute * attr,char * buf)223 static ssize_t brightness_show(struct device *dev,
224 struct device_attribute *attr, char *buf)
225 {
226 struct backlight_device *bd = to_backlight_device(dev);
227
228 return sprintf(buf, "%d\n", bd->props.brightness);
229 }
230
backlight_device_set_brightness(struct backlight_device * bd,unsigned long brightness)231 int backlight_device_set_brightness(struct backlight_device *bd,
232 unsigned long brightness)
233 {
234 int rc = -ENXIO;
235
236 mutex_lock(&bd->ops_lock);
237 if (bd->ops) {
238 if (brightness > bd->props.max_brightness)
239 rc = -EINVAL;
240 else {
241 pr_debug("set brightness to %lu\n", brightness);
242 bd->props.brightness = brightness;
243 rc = backlight_update_status(bd);
244 }
245 }
246 mutex_unlock(&bd->ops_lock);
247
248 backlight_generate_event(bd, BACKLIGHT_UPDATE_SYSFS);
249
250 return rc;
251 }
252 EXPORT_SYMBOL(backlight_device_set_brightness);
253
brightness_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)254 static ssize_t brightness_store(struct device *dev,
255 struct device_attribute *attr, const char *buf, size_t count)
256 {
257 int rc;
258 struct backlight_device *bd = to_backlight_device(dev);
259 unsigned long brightness;
260
261 rc = kstrtoul(buf, 0, &brightness);
262 if (rc)
263 return rc;
264
265 rc = backlight_device_set_brightness(bd, brightness);
266
267 return rc ? rc : count;
268 }
269 static DEVICE_ATTR_RW(brightness);
270
type_show(struct device * dev,struct device_attribute * attr,char * buf)271 static ssize_t type_show(struct device *dev, struct device_attribute *attr,
272 char *buf)
273 {
274 struct backlight_device *bd = to_backlight_device(dev);
275
276 return sprintf(buf, "%s\n", backlight_types[bd->props.type]);
277 }
278 static DEVICE_ATTR_RO(type);
279
max_brightness_show(struct device * dev,struct device_attribute * attr,char * buf)280 static ssize_t max_brightness_show(struct device *dev,
281 struct device_attribute *attr, char *buf)
282 {
283 struct backlight_device *bd = to_backlight_device(dev);
284
285 return sprintf(buf, "%d\n", bd->props.max_brightness);
286 }
287 static DEVICE_ATTR_RO(max_brightness);
288
actual_brightness_show(struct device * dev,struct device_attribute * attr,char * buf)289 static ssize_t actual_brightness_show(struct device *dev,
290 struct device_attribute *attr, char *buf)
291 {
292 int rc = -ENXIO;
293 struct backlight_device *bd = to_backlight_device(dev);
294
295 mutex_lock(&bd->ops_lock);
296 if (bd->ops && bd->ops->get_brightness) {
297 rc = bd->ops->get_brightness(bd);
298 if (rc >= 0)
299 rc = sprintf(buf, "%d\n", rc);
300 } else {
301 rc = sprintf(buf, "%d\n", bd->props.brightness);
302 }
303 mutex_unlock(&bd->ops_lock);
304
305 return rc;
306 }
307 static DEVICE_ATTR_RO(actual_brightness);
308
scale_show(struct device * dev,struct device_attribute * attr,char * buf)309 static ssize_t scale_show(struct device *dev,
310 struct device_attribute *attr, char *buf)
311 {
312 struct backlight_device *bd = to_backlight_device(dev);
313
314 if (WARN_ON(bd->props.scale > BACKLIGHT_SCALE_NON_LINEAR))
315 return sprintf(buf, "unknown\n");
316
317 return sprintf(buf, "%s\n", backlight_scale_types[bd->props.scale]);
318 }
319 static DEVICE_ATTR_RO(scale);
320
321 #ifdef CONFIG_PM_SLEEP
backlight_suspend(struct device * dev)322 static int backlight_suspend(struct device *dev)
323 {
324 struct backlight_device *bd = to_backlight_device(dev);
325
326 mutex_lock(&bd->ops_lock);
327 if (bd->ops && bd->ops->options & BL_CORE_SUSPENDRESUME) {
328 bd->props.state |= BL_CORE_SUSPENDED;
329 backlight_update_status(bd);
330 }
331 mutex_unlock(&bd->ops_lock);
332
333 return 0;
334 }
335
backlight_resume(struct device * dev)336 static int backlight_resume(struct device *dev)
337 {
338 struct backlight_device *bd = to_backlight_device(dev);
339
340 mutex_lock(&bd->ops_lock);
341 if (bd->ops && bd->ops->options & BL_CORE_SUSPENDRESUME) {
342 bd->props.state &= ~BL_CORE_SUSPENDED;
343 backlight_update_status(bd);
344 }
345 mutex_unlock(&bd->ops_lock);
346
347 return 0;
348 }
349 #endif
350
351 static SIMPLE_DEV_PM_OPS(backlight_class_dev_pm_ops, backlight_suspend,
352 backlight_resume);
353
bl_device_release(struct device * dev)354 static void bl_device_release(struct device *dev)
355 {
356 struct backlight_device *bd = to_backlight_device(dev);
357 kfree(bd);
358 }
359
360 static struct attribute *bl_device_attrs[] = {
361 &dev_attr_bl_power.attr,
362 &dev_attr_brightness.attr,
363 &dev_attr_actual_brightness.attr,
364 &dev_attr_max_brightness.attr,
365 &dev_attr_scale.attr,
366 &dev_attr_type.attr,
367 NULL,
368 };
369 ATTRIBUTE_GROUPS(bl_device);
370
371 static const struct class backlight_class = {
372 .name = "backlight",
373 .dev_groups = bl_device_groups,
374 .pm = &backlight_class_dev_pm_ops,
375 };
376
377 /**
378 * backlight_force_update - tell the backlight subsystem that hardware state
379 * has changed
380 * @bd: the backlight device to update
381 * @reason: reason for update
382 *
383 * Updates the internal state of the backlight in response to a hardware event,
384 * and generates an uevent to notify userspace. A backlight driver shall call
385 * backlight_force_update() when the backlight is changed using, for example,
386 * a hot-key. The updated brightness is read using get_brightness() and the
387 * brightness value is reported using an uevent.
388 */
backlight_force_update(struct backlight_device * bd,enum backlight_update_reason reason)389 void backlight_force_update(struct backlight_device *bd,
390 enum backlight_update_reason reason)
391 {
392 int brightness;
393
394 mutex_lock(&bd->ops_lock);
395 if (bd->ops && bd->ops->get_brightness) {
396 brightness = bd->ops->get_brightness(bd);
397 if (brightness >= 0)
398 bd->props.brightness = brightness;
399 else
400 dev_err(&bd->dev,
401 "Could not update brightness from device: %pe\n",
402 ERR_PTR(brightness));
403 }
404 mutex_unlock(&bd->ops_lock);
405 backlight_generate_event(bd, reason);
406 }
407 EXPORT_SYMBOL(backlight_force_update);
408
409 /* deprecated - use devm_backlight_device_register() */
backlight_device_register(const char * name,struct device * parent,void * devdata,const struct backlight_ops * ops,const struct backlight_properties * props)410 struct backlight_device *backlight_device_register(const char *name,
411 struct device *parent, void *devdata, const struct backlight_ops *ops,
412 const struct backlight_properties *props)
413 {
414 struct backlight_device *new_bd;
415 int rc;
416
417 pr_debug("backlight_device_register: name=%s\n", name);
418
419 new_bd = kzalloc(sizeof(struct backlight_device), GFP_KERNEL);
420 if (!new_bd)
421 return ERR_PTR(-ENOMEM);
422
423 mutex_init(&new_bd->update_lock);
424 mutex_init(&new_bd->ops_lock);
425
426 new_bd->dev.class = &backlight_class;
427 new_bd->dev.parent = parent;
428 new_bd->dev.release = bl_device_release;
429 dev_set_name(&new_bd->dev, "%s", name);
430 dev_set_drvdata(&new_bd->dev, devdata);
431
432 /* Set default properties */
433 if (props) {
434 memcpy(&new_bd->props, props,
435 sizeof(struct backlight_properties));
436 if (props->type <= 0 || props->type >= BACKLIGHT_TYPE_MAX) {
437 WARN(1, "%s: invalid backlight type", name);
438 new_bd->props.type = BACKLIGHT_RAW;
439 }
440 } else {
441 new_bd->props.type = BACKLIGHT_RAW;
442 }
443
444 rc = device_register(&new_bd->dev);
445 if (rc) {
446 put_device(&new_bd->dev);
447 return ERR_PTR(rc);
448 }
449
450 rc = backlight_register_fb(new_bd);
451 if (rc) {
452 device_unregister(&new_bd->dev);
453 return ERR_PTR(rc);
454 }
455
456 new_bd->ops = ops;
457
458 #ifdef CONFIG_PMAC_BACKLIGHT
459 mutex_lock(&pmac_backlight_mutex);
460 if (!pmac_backlight)
461 pmac_backlight = new_bd;
462 mutex_unlock(&pmac_backlight_mutex);
463 #endif
464
465 mutex_lock(&backlight_dev_list_mutex);
466 list_add(&new_bd->entry, &backlight_dev_list);
467 mutex_unlock(&backlight_dev_list_mutex);
468
469 return new_bd;
470 }
471 EXPORT_SYMBOL(backlight_device_register);
472
473 /** backlight_device_get_by_type - find first backlight device of a type
474 * @type: the type of backlight device
475 *
476 * Look up the first backlight device of the specified type
477 *
478 * RETURNS:
479 *
480 * Pointer to backlight device if any was found. Otherwise NULL.
481 */
backlight_device_get_by_type(enum backlight_type type)482 struct backlight_device *backlight_device_get_by_type(enum backlight_type type)
483 {
484 bool found = false;
485 struct backlight_device *bd;
486
487 mutex_lock(&backlight_dev_list_mutex);
488 list_for_each_entry(bd, &backlight_dev_list, entry) {
489 if (bd->props.type == type) {
490 found = true;
491 break;
492 }
493 }
494 mutex_unlock(&backlight_dev_list_mutex);
495
496 return found ? bd : NULL;
497 }
498 EXPORT_SYMBOL(backlight_device_get_by_type);
499
500 /**
501 * backlight_device_get_by_name - Get backlight device by name
502 * @name: Device name
503 *
504 * This function looks up a backlight device by its name. It obtains a reference
505 * on the backlight device and it is the caller's responsibility to drop the
506 * reference by calling put_device().
507 *
508 * Returns:
509 * A pointer to the backlight device if found, otherwise NULL.
510 */
backlight_device_get_by_name(const char * name)511 struct backlight_device *backlight_device_get_by_name(const char *name)
512 {
513 struct device *dev;
514
515 dev = class_find_device_by_name(&backlight_class, name);
516
517 return dev ? to_backlight_device(dev) : NULL;
518 }
519 EXPORT_SYMBOL(backlight_device_get_by_name);
520
521 /* deprecated - use devm_backlight_device_unregister() */
backlight_device_unregister(struct backlight_device * bd)522 void backlight_device_unregister(struct backlight_device *bd)
523 {
524 if (!bd)
525 return;
526
527 mutex_lock(&backlight_dev_list_mutex);
528 list_del(&bd->entry);
529 mutex_unlock(&backlight_dev_list_mutex);
530
531 #ifdef CONFIG_PMAC_BACKLIGHT
532 mutex_lock(&pmac_backlight_mutex);
533 if (pmac_backlight == bd)
534 pmac_backlight = NULL;
535 mutex_unlock(&pmac_backlight_mutex);
536 #endif
537
538 mutex_lock(&bd->ops_lock);
539 bd->ops = NULL;
540 mutex_unlock(&bd->ops_lock);
541
542 backlight_unregister_fb(bd);
543 device_unregister(&bd->dev);
544 }
545 EXPORT_SYMBOL(backlight_device_unregister);
546
devm_backlight_device_release(struct device * dev,void * res)547 static void devm_backlight_device_release(struct device *dev, void *res)
548 {
549 struct backlight_device *backlight = *(struct backlight_device **)res;
550
551 backlight_device_unregister(backlight);
552 }
553
devm_backlight_device_match(struct device * dev,void * res,void * data)554 static int devm_backlight_device_match(struct device *dev, void *res,
555 void *data)
556 {
557 struct backlight_device **r = res;
558
559 return *r == data;
560 }
561
562 /**
563 * devm_backlight_device_register - register a new backlight device
564 * @dev: the device to register
565 * @name: the name of the device
566 * @parent: a pointer to the parent device (often the same as @dev)
567 * @devdata: an optional pointer to be stored for private driver use
568 * @ops: the backlight operations structure
569 * @props: the backlight properties
570 *
571 * Creates and registers new backlight device. When a backlight device
572 * is registered the configuration must be specified in the @props
573 * parameter. See description of &backlight_properties.
574 *
575 * RETURNS:
576 *
577 * struct backlight on success, or an ERR_PTR on error
578 */
devm_backlight_device_register(struct device * dev,const char * name,struct device * parent,void * devdata,const struct backlight_ops * ops,const struct backlight_properties * props)579 struct backlight_device *devm_backlight_device_register(struct device *dev,
580 const char *name, struct device *parent, void *devdata,
581 const struct backlight_ops *ops,
582 const struct backlight_properties *props)
583 {
584 struct backlight_device **ptr, *backlight;
585
586 ptr = devres_alloc(devm_backlight_device_release, sizeof(*ptr),
587 GFP_KERNEL);
588 if (!ptr)
589 return ERR_PTR(-ENOMEM);
590
591 backlight = backlight_device_register(name, parent, devdata, ops,
592 props);
593 if (!IS_ERR(backlight)) {
594 *ptr = backlight;
595 devres_add(dev, ptr);
596 } else {
597 devres_free(ptr);
598 }
599
600 return backlight;
601 }
602 EXPORT_SYMBOL(devm_backlight_device_register);
603
604 /**
605 * devm_backlight_device_unregister - unregister backlight device
606 * @dev: the device to unregister
607 * @bd: the backlight device to unregister
608 *
609 * Deallocates a backlight allocated with devm_backlight_device_register().
610 * Normally this function will not need to be called and the resource management
611 * code will ensure that the resources are freed.
612 */
devm_backlight_device_unregister(struct device * dev,struct backlight_device * bd)613 void devm_backlight_device_unregister(struct device *dev,
614 struct backlight_device *bd)
615 {
616 int rc;
617
618 rc = devres_release(dev, devm_backlight_device_release,
619 devm_backlight_device_match, bd);
620 WARN_ON(rc);
621 }
622 EXPORT_SYMBOL(devm_backlight_device_unregister);
623
624 #ifdef CONFIG_OF
of_parent_match(struct device * dev,const void * data)625 static int of_parent_match(struct device *dev, const void *data)
626 {
627 return dev->parent && dev->parent->of_node == data;
628 }
629
630 /**
631 * of_find_backlight_by_node() - find backlight device by device-tree node
632 * @node: device-tree node of the backlight device
633 *
634 * Returns a pointer to the backlight device corresponding to the given DT
635 * node or NULL if no such backlight device exists or if the device hasn't
636 * been probed yet.
637 *
638 * This function obtains a reference on the backlight device and it is the
639 * caller's responsibility to drop the reference by calling put_device() on
640 * the backlight device's .dev field.
641 */
of_find_backlight_by_node(struct device_node * node)642 struct backlight_device *of_find_backlight_by_node(struct device_node *node)
643 {
644 struct device *dev;
645
646 dev = class_find_device(&backlight_class, NULL, node, of_parent_match);
647
648 return dev ? to_backlight_device(dev) : NULL;
649 }
650 EXPORT_SYMBOL(of_find_backlight_by_node);
651 #endif
652
of_find_backlight(struct device * dev)653 static struct backlight_device *of_find_backlight(struct device *dev)
654 {
655 struct backlight_device *bd = NULL;
656 struct device_node *np;
657
658 if (!dev)
659 return NULL;
660
661 if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
662 np = of_parse_phandle(dev->of_node, "backlight", 0);
663 if (np) {
664 bd = of_find_backlight_by_node(np);
665 of_node_put(np);
666 if (!bd)
667 return ERR_PTR(-EPROBE_DEFER);
668 }
669 }
670
671 return bd;
672 }
673
devm_backlight_release(void * data)674 static void devm_backlight_release(void *data)
675 {
676 struct backlight_device *bd = data;
677
678 put_device(&bd->dev);
679 }
680
681 /**
682 * devm_of_find_backlight - find backlight for a device
683 * @dev: the device
684 *
685 * This function looks for a property named 'backlight' on the DT node
686 * connected to @dev and looks up the backlight device. The lookup is
687 * device managed so the reference to the backlight device is automatically
688 * dropped on driver detach.
689 *
690 * RETURNS:
691 *
692 * A pointer to the backlight device if found.
693 * Error pointer -EPROBE_DEFER if the DT property is set, but no backlight
694 * device is found. NULL if there's no backlight property.
695 */
devm_of_find_backlight(struct device * dev)696 struct backlight_device *devm_of_find_backlight(struct device *dev)
697 {
698 struct backlight_device *bd;
699 int ret;
700
701 bd = of_find_backlight(dev);
702 if (IS_ERR_OR_NULL(bd))
703 return bd;
704 ret = devm_add_action_or_reset(dev, devm_backlight_release, bd);
705 if (ret)
706 return ERR_PTR(ret);
707
708 return bd;
709 }
710 EXPORT_SYMBOL(devm_of_find_backlight);
711
backlight_class_exit(void)712 static void __exit backlight_class_exit(void)
713 {
714 class_unregister(&backlight_class);
715 }
716
backlight_class_init(void)717 static int __init backlight_class_init(void)
718 {
719 int ret;
720
721 ret = class_register(&backlight_class);
722 if (ret) {
723 pr_warn("Unable to create backlight class; errno = %d\n", ret);
724 return ret;
725 }
726
727 INIT_LIST_HEAD(&backlight_dev_list);
728 mutex_init(&backlight_dev_list_mutex);
729
730 return 0;
731 }
732
733 /*
734 * if this is compiled into the kernel, we need to ensure that the
735 * class is registered before users of the class try to register lcd's
736 */
737 postcore_initcall(backlight_class_init);
738 module_exit(backlight_class_exit);
739
740 MODULE_LICENSE("GPL");
741 MODULE_AUTHOR("Jamey Hicks <jamey.hicks@hp.com>, Andrew Zabolotny <zap@homelink.ru>");
742 MODULE_DESCRIPTION("Backlight Lowlevel Control Abstraction");
743