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