1
2
3
4
5
6
7
8#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9
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>
18#include <linux/slab.h>
19
20#ifdef CONFIG_PMAC_BACKLIGHT
21#include <asm/backlight.h>
22#endif
23
24static struct list_head backlight_dev_list;
25static struct mutex backlight_dev_list_mutex;
26
27static const char *const backlight_types[] = {
28 [BACKLIGHT_RAW] = "raw",
29 [BACKLIGHT_PLATFORM] = "platform",
30 [BACKLIGHT_FIRMWARE] = "firmware",
31};
32
33#if defined(CONFIG_FB) || (defined(CONFIG_FB_MODULE) && \
34 defined(CONFIG_BACKLIGHT_CLASS_DEVICE_MODULE))
35
36
37
38
39static int fb_notifier_callback(struct notifier_block *self,
40 unsigned long event, void *data)
41{
42 struct backlight_device *bd;
43 struct fb_event *evdata = data;
44
45
46 if (event != FB_EVENT_BLANK && event != FB_EVENT_CONBLANK)
47 return 0;
48
49 bd = container_of(self, struct backlight_device, fb_notif);
50 mutex_lock(&bd->ops_lock);
51 if (bd->ops)
52 if (!bd->ops->check_fb ||
53 bd->ops->check_fb(bd, evdata->info)) {
54 bd->props.fb_blank = *(int *)evdata->data;
55 if (bd->props.fb_blank == FB_BLANK_UNBLANK)
56 bd->props.state &= ~BL_CORE_FBBLANK;
57 else
58 bd->props.state |= BL_CORE_FBBLANK;
59 backlight_update_status(bd);
60 }
61 mutex_unlock(&bd->ops_lock);
62 return 0;
63}
64
65static int backlight_register_fb(struct backlight_device *bd)
66{
67 memset(&bd->fb_notif, 0, sizeof(bd->fb_notif));
68 bd->fb_notif.notifier_call = fb_notifier_callback;
69
70 return fb_register_client(&bd->fb_notif);
71}
72
73static void backlight_unregister_fb(struct backlight_device *bd)
74{
75 fb_unregister_client(&bd->fb_notif);
76}
77#else
78static inline int backlight_register_fb(struct backlight_device *bd)
79{
80 return 0;
81}
82
83static inline void backlight_unregister_fb(struct backlight_device *bd)
84{
85}
86#endif
87
88static void backlight_generate_event(struct backlight_device *bd,
89 enum backlight_update_reason reason)
90{
91 char *envp[2];
92
93 switch (reason) {
94 case BACKLIGHT_UPDATE_SYSFS:
95 envp[0] = "SOURCE=sysfs";
96 break;
97 case BACKLIGHT_UPDATE_HOTKEY:
98 envp[0] = "SOURCE=hotkey";
99 break;
100 default:
101 envp[0] = "SOURCE=unknown";
102 break;
103 }
104 envp[1] = NULL;
105 kobject_uevent_env(&bd->dev.kobj, KOBJ_CHANGE, envp);
106 sysfs_notify(&bd->dev.kobj, NULL, "actual_brightness");
107}
108
109static ssize_t bl_power_show(struct device *dev, struct device_attribute *attr,
110 char *buf)
111{
112 struct backlight_device *bd = to_backlight_device(dev);
113
114 return sprintf(buf, "%d\n", bd->props.power);
115}
116
117static ssize_t bl_power_store(struct device *dev, struct device_attribute *attr,
118 const char *buf, size_t count)
119{
120 int rc;
121 struct backlight_device *bd = to_backlight_device(dev);
122 unsigned long power;
123
124 rc = kstrtoul(buf, 0, &power);
125 if (rc)
126 return rc;
127
128 rc = -ENXIO;
129 mutex_lock(&bd->ops_lock);
130 if (bd->ops) {
131 pr_debug("set power to %lu\n", power);
132 if (bd->props.power != power) {
133 bd->props.power = power;
134 backlight_update_status(bd);
135 }
136 rc = count;
137 }
138 mutex_unlock(&bd->ops_lock);
139
140 return rc;
141}
142static DEVICE_ATTR_RW(bl_power);
143
144static ssize_t brightness_show(struct device *dev,
145 struct device_attribute *attr, char *buf)
146{
147 struct backlight_device *bd = to_backlight_device(dev);
148
149 return sprintf(buf, "%d\n", bd->props.brightness);
150}
151
152static ssize_t brightness_store(struct device *dev,
153 struct device_attribute *attr, const char *buf, size_t count)
154{
155 int rc;
156 struct backlight_device *bd = to_backlight_device(dev);
157 unsigned long brightness;
158
159 rc = kstrtoul(buf, 0, &brightness);
160 if (rc)
161 return rc;
162
163 rc = -ENXIO;
164
165 mutex_lock(&bd->ops_lock);
166 if (bd->ops) {
167 if (brightness > bd->props.max_brightness)
168 rc = -EINVAL;
169 else {
170 pr_debug("set brightness to %lu\n", brightness);
171 bd->props.brightness = brightness;
172 backlight_update_status(bd);
173 rc = count;
174 }
175 }
176 mutex_unlock(&bd->ops_lock);
177
178 backlight_generate_event(bd, BACKLIGHT_UPDATE_SYSFS);
179
180 return rc;
181}
182static DEVICE_ATTR_RW(brightness);
183
184static ssize_t type_show(struct device *dev, struct device_attribute *attr,
185 char *buf)
186{
187 struct backlight_device *bd = to_backlight_device(dev);
188
189 return sprintf(buf, "%s\n", backlight_types[bd->props.type]);
190}
191static DEVICE_ATTR_RO(type);
192
193static ssize_t max_brightness_show(struct device *dev,
194 struct device_attribute *attr, char *buf)
195{
196 struct backlight_device *bd = to_backlight_device(dev);
197
198 return sprintf(buf, "%d\n", bd->props.max_brightness);
199}
200static DEVICE_ATTR_RO(max_brightness);
201
202static ssize_t actual_brightness_show(struct device *dev,
203 struct device_attribute *attr, char *buf)
204{
205 int rc = -ENXIO;
206 struct backlight_device *bd = to_backlight_device(dev);
207
208 mutex_lock(&bd->ops_lock);
209 if (bd->ops && bd->ops->get_brightness)
210 rc = sprintf(buf, "%d\n", bd->ops->get_brightness(bd));
211 mutex_unlock(&bd->ops_lock);
212
213 return rc;
214}
215static DEVICE_ATTR_RO(actual_brightness);
216
217static struct class *backlight_class;
218
219#ifdef CONFIG_PM_SLEEP
220static int backlight_suspend(struct device *dev)
221{
222 struct backlight_device *bd = to_backlight_device(dev);
223
224 mutex_lock(&bd->ops_lock);
225 if (bd->ops && bd->ops->options & BL_CORE_SUSPENDRESUME) {
226 bd->props.state |= BL_CORE_SUSPENDED;
227 backlight_update_status(bd);
228 }
229 mutex_unlock(&bd->ops_lock);
230
231 return 0;
232}
233
234static int backlight_resume(struct device *dev)
235{
236 struct backlight_device *bd = to_backlight_device(dev);
237
238 mutex_lock(&bd->ops_lock);
239 if (bd->ops && bd->ops->options & BL_CORE_SUSPENDRESUME) {
240 bd->props.state &= ~BL_CORE_SUSPENDED;
241 backlight_update_status(bd);
242 }
243 mutex_unlock(&bd->ops_lock);
244
245 return 0;
246}
247#endif
248
249static SIMPLE_DEV_PM_OPS(backlight_class_dev_pm_ops, backlight_suspend,
250 backlight_resume);
251
252static void bl_device_release(struct device *dev)
253{
254 struct backlight_device *bd = to_backlight_device(dev);
255 kfree(bd);
256}
257
258static struct attribute *bl_device_attrs[] = {
259 &dev_attr_bl_power.attr,
260 &dev_attr_brightness.attr,
261 &dev_attr_actual_brightness.attr,
262 &dev_attr_max_brightness.attr,
263 &dev_attr_type.attr,
264 NULL,
265};
266ATTRIBUTE_GROUPS(bl_device);
267
268
269
270
271
272
273
274
275
276void backlight_force_update(struct backlight_device *bd,
277 enum backlight_update_reason reason)
278{
279 mutex_lock(&bd->ops_lock);
280 if (bd->ops && bd->ops->get_brightness)
281 bd->props.brightness = bd->ops->get_brightness(bd);
282 mutex_unlock(&bd->ops_lock);
283 backlight_generate_event(bd, reason);
284}
285EXPORT_SYMBOL(backlight_force_update);
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300struct backlight_device *backlight_device_register(const char *name,
301 struct device *parent, void *devdata, const struct backlight_ops *ops,
302 const struct backlight_properties *props)
303{
304 struct backlight_device *new_bd;
305 int rc;
306
307 pr_debug("backlight_device_register: name=%s\n", name);
308
309 new_bd = kzalloc(sizeof(struct backlight_device), GFP_KERNEL);
310 if (!new_bd)
311 return ERR_PTR(-ENOMEM);
312
313 mutex_init(&new_bd->update_lock);
314 mutex_init(&new_bd->ops_lock);
315
316 new_bd->dev.class = backlight_class;
317 new_bd->dev.parent = parent;
318 new_bd->dev.release = bl_device_release;
319 dev_set_name(&new_bd->dev, "%s", name);
320 dev_set_drvdata(&new_bd->dev, devdata);
321
322
323 if (props) {
324 memcpy(&new_bd->props, props,
325 sizeof(struct backlight_properties));
326 if (props->type <= 0 || props->type >= BACKLIGHT_TYPE_MAX) {
327 WARN(1, "%s: invalid backlight type", name);
328 new_bd->props.type = BACKLIGHT_RAW;
329 }
330 } else {
331 new_bd->props.type = BACKLIGHT_RAW;
332 }
333
334 rc = device_register(&new_bd->dev);
335 if (rc) {
336 kfree(new_bd);
337 return ERR_PTR(rc);
338 }
339
340 rc = backlight_register_fb(new_bd);
341 if (rc) {
342 device_unregister(&new_bd->dev);
343 return ERR_PTR(rc);
344 }
345
346 new_bd->ops = ops;
347
348#ifdef CONFIG_PMAC_BACKLIGHT
349 mutex_lock(&pmac_backlight_mutex);
350 if (!pmac_backlight)
351 pmac_backlight = new_bd;
352 mutex_unlock(&pmac_backlight_mutex);
353#endif
354
355 mutex_lock(&backlight_dev_list_mutex);
356 list_add(&new_bd->entry, &backlight_dev_list);
357 mutex_unlock(&backlight_dev_list_mutex);
358
359 return new_bd;
360}
361EXPORT_SYMBOL(backlight_device_register);
362
363bool backlight_device_registered(enum backlight_type type)
364{
365 bool found = false;
366 struct backlight_device *bd;
367
368 mutex_lock(&backlight_dev_list_mutex);
369 list_for_each_entry(bd, &backlight_dev_list, entry) {
370 if (bd->props.type == type) {
371 found = true;
372 break;
373 }
374 }
375 mutex_unlock(&backlight_dev_list_mutex);
376
377 return found;
378}
379EXPORT_SYMBOL(backlight_device_registered);
380
381
382
383
384
385
386
387void backlight_device_unregister(struct backlight_device *bd)
388{
389 if (!bd)
390 return;
391
392 mutex_lock(&backlight_dev_list_mutex);
393 list_del(&bd->entry);
394 mutex_unlock(&backlight_dev_list_mutex);
395
396#ifdef CONFIG_PMAC_BACKLIGHT
397 mutex_lock(&pmac_backlight_mutex);
398 if (pmac_backlight == bd)
399 pmac_backlight = NULL;
400 mutex_unlock(&pmac_backlight_mutex);
401#endif
402 mutex_lock(&bd->ops_lock);
403 bd->ops = NULL;
404 mutex_unlock(&bd->ops_lock);
405
406 backlight_unregister_fb(bd);
407 device_unregister(&bd->dev);
408}
409EXPORT_SYMBOL(backlight_device_unregister);
410
411static void devm_backlight_device_release(struct device *dev, void *res)
412{
413 struct backlight_device *backlight = *(struct backlight_device **)res;
414
415 backlight_device_unregister(backlight);
416}
417
418static int devm_backlight_device_match(struct device *dev, void *res,
419 void *data)
420{
421 struct backlight_device **r = res;
422
423 return *r == data;
424}
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441struct backlight_device *devm_backlight_device_register(struct device *dev,
442 const char *name, struct device *parent, void *devdata,
443 const struct backlight_ops *ops,
444 const struct backlight_properties *props)
445{
446 struct backlight_device **ptr, *backlight;
447
448 ptr = devres_alloc(devm_backlight_device_release, sizeof(*ptr),
449 GFP_KERNEL);
450 if (!ptr)
451 return ERR_PTR(-ENOMEM);
452
453 backlight = backlight_device_register(name, parent, devdata, ops,
454 props);
455 if (!IS_ERR(backlight)) {
456 *ptr = backlight;
457 devres_add(dev, ptr);
458 } else {
459 devres_free(ptr);
460 }
461
462 return backlight;
463}
464EXPORT_SYMBOL(devm_backlight_device_register);
465
466
467
468
469
470
471
472
473
474
475void devm_backlight_device_unregister(struct device *dev,
476 struct backlight_device *bd)
477{
478 int rc;
479
480 rc = devres_release(dev, devm_backlight_device_release,
481 devm_backlight_device_match, bd);
482 WARN_ON(rc);
483}
484EXPORT_SYMBOL(devm_backlight_device_unregister);
485
486#ifdef CONFIG_OF
487static int of_parent_match(struct device *dev, const void *data)
488{
489 return dev->parent && dev->parent->of_node == data;
490}
491
492
493
494
495
496
497
498
499
500
501
502
503
504struct backlight_device *of_find_backlight_by_node(struct device_node *node)
505{
506 struct device *dev;
507
508 dev = class_find_device(backlight_class, NULL, node, of_parent_match);
509
510 return dev ? to_backlight_device(dev) : NULL;
511}
512EXPORT_SYMBOL(of_find_backlight_by_node);
513#endif
514
515static void __exit backlight_class_exit(void)
516{
517 class_destroy(backlight_class);
518}
519
520static int __init backlight_class_init(void)
521{
522 backlight_class = class_create(THIS_MODULE, "backlight");
523 if (IS_ERR(backlight_class)) {
524 pr_warn("Unable to create backlight class; errno = %ld\n",
525 PTR_ERR(backlight_class));
526 return PTR_ERR(backlight_class);
527 }
528
529 backlight_class->dev_groups = bl_device_groups;
530 backlight_class->pm = &backlight_class_dev_pm_ops;
531 INIT_LIST_HEAD(&backlight_dev_list);
532 mutex_init(&backlight_dev_list_mutex);
533 return 0;
534}
535
536
537
538
539
540postcore_initcall(backlight_class_init);
541module_exit(backlight_class_exit);
542
543MODULE_LICENSE("GPL");
544MODULE_AUTHOR("Jamey Hicks <jamey.hicks@hp.com>, Andrew Zabolotny <zap@homelink.ru>");
545MODULE_DESCRIPTION("Backlight Lowlevel Control Abstraction");
546