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