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