blob: 80d292fb92d8a3c10825130867dbdc55b586f939 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Backlight Lowlevel Control Abstraction
3 *
4 * Copyright (C) 2003,2004 Hewlett-Packard Company
5 *
6 */
7
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <linux/module.h>
9#include <linux/init.h>
10#include <linux/device.h>
11#include <linux/backlight.h>
12#include <linux/notifier.h>
13#include <linux/ctype.h>
14#include <linux/err.h>
15#include <linux/fb.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090016#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
Richard Purdie321709c2007-02-10 15:04:08 +000018#ifdef CONFIG_PMAC_BACKLIGHT
19#include <asm/backlight.h>
20#endif
James Simmons3d5eead2006-12-08 02:40:47 -080021
Matthew Garrettbb7ca742011-03-22 16:30:21 -070022static const char const *backlight_types[] = {
23 [BACKLIGHT_RAW] = "raw",
24 [BACKLIGHT_PLATFORM] = "platform",
25 [BACKLIGHT_FIRMWARE] = "firmware",
26};
27
James Simmons3d5eead2006-12-08 02:40:47 -080028#if defined(CONFIG_FB) || (defined(CONFIG_FB_MODULE) && \
29 defined(CONFIG_BACKLIGHT_CLASS_DEVICE_MODULE))
30/* This callback gets called when something important happens inside a
31 * framebuffer driver. We're looking if that important event is blanking,
32 * and if it is, we're switching backlight power as well ...
33 */
34static int fb_notifier_callback(struct notifier_block *self,
35 unsigned long event, void *data)
36{
37 struct backlight_device *bd;
38 struct fb_event *evdata = data;
39
40 /* If we aren't interested in this event, skip it immediately ... */
Richard Purdie994efac2007-02-09 09:46:45 +000041 if (event != FB_EVENT_BLANK && event != FB_EVENT_CONBLANK)
James Simmons3d5eead2006-12-08 02:40:47 -080042 return 0;
43
44 bd = container_of(self, struct backlight_device, fb_notif);
Richard Purdie599a52d2007-02-10 23:07:48 +000045 mutex_lock(&bd->ops_lock);
46 if (bd->ops)
47 if (!bd->ops->check_fb ||
Bruno Prémont57e148b2010-02-21 00:20:01 +010048 bd->ops->check_fb(bd, evdata->info)) {
Richard Purdie599a52d2007-02-10 23:07:48 +000049 bd->props.fb_blank = *(int *)evdata->data;
Richard Purdiec835ee72009-01-06 21:00:19 +000050 if (bd->props.fb_blank == FB_BLANK_UNBLANK)
51 bd->props.state &= ~BL_CORE_FBBLANK;
52 else
53 bd->props.state |= BL_CORE_FBBLANK;
Richard Purdie28ee0862007-02-08 22:25:09 +000054 backlight_update_status(bd);
James Simmons3d5eead2006-12-08 02:40:47 -080055 }
Richard Purdie599a52d2007-02-10 23:07:48 +000056 mutex_unlock(&bd->ops_lock);
James Simmons3d5eead2006-12-08 02:40:47 -080057 return 0;
58}
59
60static int backlight_register_fb(struct backlight_device *bd)
61{
62 memset(&bd->fb_notif, 0, sizeof(bd->fb_notif));
63 bd->fb_notif.notifier_call = fb_notifier_callback;
64
65 return fb_register_client(&bd->fb_notif);
66}
67
68static void backlight_unregister_fb(struct backlight_device *bd)
69{
70 fb_unregister_client(&bd->fb_notif);
71}
72#else
73static inline int backlight_register_fb(struct backlight_device *bd)
74{
75 return 0;
76}
77
78static inline void backlight_unregister_fb(struct backlight_device *bd)
79{
80}
81#endif /* CONFIG_FB */
82
Matthew Garrett325253a2009-07-14 17:06:02 +010083static void backlight_generate_event(struct backlight_device *bd,
84 enum backlight_update_reason reason)
85{
86 char *envp[2];
87
88 switch (reason) {
89 case BACKLIGHT_UPDATE_SYSFS:
90 envp[0] = "SOURCE=sysfs";
91 break;
92 case BACKLIGHT_UPDATE_HOTKEY:
93 envp[0] = "SOURCE=hotkey";
94 break;
95 default:
96 envp[0] = "SOURCE=unknown";
97 break;
98 }
99 envp[1] = NULL;
100 kobject_uevent_env(&bd->dev.kobj, KOBJ_CHANGE, envp);
Henrique de Moraes Holschuh89dfc282009-09-20 14:44:47 -0300101 sysfs_notify(&bd->dev.kobj, NULL, "actual_brightness");
Matthew Garrett325253a2009-07-14 17:06:02 +0100102}
103
Richard Purdie655bfd72007-07-09 12:17:24 +0100104static ssize_t backlight_show_power(struct device *dev,
105 struct device_attribute *attr,char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106{
Richard Purdie655bfd72007-07-09 12:17:24 +0100107 struct backlight_device *bd = to_backlight_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
Richard Purdie599a52d2007-02-10 23:07:48 +0000109 return sprintf(buf, "%d\n", bd->props.power);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110}
111
Richard Purdie655bfd72007-07-09 12:17:24 +0100112static ssize_t backlight_store_power(struct device *dev,
113 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114{
Pavel Machek9a2c61a2008-12-03 08:43:48 +0000115 int rc;
Richard Purdie655bfd72007-07-09 12:17:24 +0100116 struct backlight_device *bd = to_backlight_device(dev);
Pavel Machek9a2c61a2008-12-03 08:43:48 +0000117 unsigned long power;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
Pavel Machek9a2c61a2008-12-03 08:43:48 +0000119 rc = strict_strtoul(buf, 0, &power);
120 if (rc)
121 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
Pavel Machek9a2c61a2008-12-03 08:43:48 +0000123 rc = -ENXIO;
Richard Purdie599a52d2007-02-10 23:07:48 +0000124 mutex_lock(&bd->ops_lock);
125 if (bd->ops) {
Pavel Machek9a2c61a2008-12-03 08:43:48 +0000126 pr_debug("backlight: set power to %lu\n", power);
Helge Deller51552452008-01-13 23:01:13 +0000127 if (bd->props.power != power) {
128 bd->props.power = power;
129 backlight_update_status(bd);
130 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 rc = count;
Richard Purdie6ca01762006-03-31 02:31:49 -0800132 }
Richard Purdie599a52d2007-02-10 23:07:48 +0000133 mutex_unlock(&bd->ops_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
135 return rc;
136}
137
Richard Purdie655bfd72007-07-09 12:17:24 +0100138static ssize_t backlight_show_brightness(struct device *dev,
139 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140{
Richard Purdie655bfd72007-07-09 12:17:24 +0100141 struct backlight_device *bd = to_backlight_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Richard Purdie599a52d2007-02-10 23:07:48 +0000143 return sprintf(buf, "%d\n", bd->props.brightness);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144}
145
Richard Purdie655bfd72007-07-09 12:17:24 +0100146static ssize_t backlight_store_brightness(struct device *dev,
147 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148{
Pavel Machek9a2c61a2008-12-03 08:43:48 +0000149 int rc;
Richard Purdie655bfd72007-07-09 12:17:24 +0100150 struct backlight_device *bd = to_backlight_device(dev);
Pavel Machek9a2c61a2008-12-03 08:43:48 +0000151 unsigned long brightness;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
Pavel Machek9a2c61a2008-12-03 08:43:48 +0000153 rc = strict_strtoul(buf, 0, &brightness);
154 if (rc)
155 return rc;
156
157 rc = -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
Richard Purdie599a52d2007-02-10 23:07:48 +0000159 mutex_lock(&bd->ops_lock);
160 if (bd->ops) {
161 if (brightness > bd->props.max_brightness)
Richard Purdie6ca01762006-03-31 02:31:49 -0800162 rc = -EINVAL;
163 else {
Pavel Machek9a2c61a2008-12-03 08:43:48 +0000164 pr_debug("backlight: set brightness to %lu\n",
Richard Purdie6ca01762006-03-31 02:31:49 -0800165 brightness);
Zhang Rui9be1df92009-01-08 14:11:30 +0000166 bd->props.brightness = brightness;
167 backlight_update_status(bd);
Richard Purdie6ca01762006-03-31 02:31:49 -0800168 rc = count;
169 }
170 }
Richard Purdie599a52d2007-02-10 23:07:48 +0000171 mutex_unlock(&bd->ops_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
Matthew Garrett325253a2009-07-14 17:06:02 +0100173 backlight_generate_event(bd, BACKLIGHT_UPDATE_SYSFS);
174
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 return rc;
176}
177
Matthew Garrettbb7ca742011-03-22 16:30:21 -0700178static ssize_t backlight_show_type(struct device *dev,
179 struct device_attribute *attr, char *buf)
180{
181 struct backlight_device *bd = to_backlight_device(dev);
182
183 return sprintf(buf, "%s\n", backlight_types[bd->props.type]);
184}
185
Richard Purdie655bfd72007-07-09 12:17:24 +0100186static ssize_t backlight_show_max_brightness(struct device *dev,
187 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188{
Richard Purdie655bfd72007-07-09 12:17:24 +0100189 struct backlight_device *bd = to_backlight_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
Richard Purdie599a52d2007-02-10 23:07:48 +0000191 return sprintf(buf, "%d\n", bd->props.max_brightness);
Richard Purdie6ca01762006-03-31 02:31:49 -0800192}
193
Richard Purdie655bfd72007-07-09 12:17:24 +0100194static ssize_t backlight_show_actual_brightness(struct device *dev,
195 struct device_attribute *attr, char *buf)
Richard Purdie6ca01762006-03-31 02:31:49 -0800196{
197 int rc = -ENXIO;
Richard Purdie655bfd72007-07-09 12:17:24 +0100198 struct backlight_device *bd = to_backlight_device(dev);
Richard Purdie6ca01762006-03-31 02:31:49 -0800199
Richard Purdie599a52d2007-02-10 23:07:48 +0000200 mutex_lock(&bd->ops_lock);
201 if (bd->ops && bd->ops->get_brightness)
202 rc = sprintf(buf, "%d\n", bd->ops->get_brightness(bd));
203 mutex_unlock(&bd->ops_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
205 return rc;
206}
207
Adrian Bunk0ad90ef2007-08-11 10:27:19 +0100208static struct class *backlight_class;
Richard Purdie655bfd72007-07-09 12:17:24 +0100209
Richard Purdiec835ee72009-01-06 21:00:19 +0000210static int backlight_suspend(struct device *dev, pm_message_t state)
211{
212 struct backlight_device *bd = to_backlight_device(dev);
213
Uwe Kleine-Königd1d73572010-11-24 12:57:14 -0800214 mutex_lock(&bd->ops_lock);
215 if (bd->ops && bd->ops->options & BL_CORE_SUSPENDRESUME) {
Richard Purdiec835ee72009-01-06 21:00:19 +0000216 bd->props.state |= BL_CORE_SUSPENDED;
217 backlight_update_status(bd);
Richard Purdiec835ee72009-01-06 21:00:19 +0000218 }
Uwe Kleine-Königd1d73572010-11-24 12:57:14 -0800219 mutex_unlock(&bd->ops_lock);
Richard Purdiec835ee72009-01-06 21:00:19 +0000220
221 return 0;
222}
223
224static int backlight_resume(struct device *dev)
225{
226 struct backlight_device *bd = to_backlight_device(dev);
227
Uwe Kleine-Königd1d73572010-11-24 12:57:14 -0800228 mutex_lock(&bd->ops_lock);
229 if (bd->ops && bd->ops->options & BL_CORE_SUSPENDRESUME) {
Richard Purdiec835ee72009-01-06 21:00:19 +0000230 bd->props.state &= ~BL_CORE_SUSPENDED;
231 backlight_update_status(bd);
Richard Purdiec835ee72009-01-06 21:00:19 +0000232 }
Uwe Kleine-Königd1d73572010-11-24 12:57:14 -0800233 mutex_unlock(&bd->ops_lock);
Richard Purdiec835ee72009-01-06 21:00:19 +0000234
235 return 0;
236}
237
Richard Purdie655bfd72007-07-09 12:17:24 +0100238static void bl_device_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239{
240 struct backlight_device *bd = to_backlight_device(dev);
241 kfree(bd);
242}
243
Richard Purdie655bfd72007-07-09 12:17:24 +0100244static struct device_attribute bl_device_attributes[] = {
245 __ATTR(bl_power, 0644, backlight_show_power, backlight_store_power),
246 __ATTR(brightness, 0644, backlight_show_brightness,
Richard Purdie6ca01762006-03-31 02:31:49 -0800247 backlight_store_brightness),
Richard Purdie655bfd72007-07-09 12:17:24 +0100248 __ATTR(actual_brightness, 0444, backlight_show_actual_brightness,
Richard Purdie6ca01762006-03-31 02:31:49 -0800249 NULL),
Richard Purdie655bfd72007-07-09 12:17:24 +0100250 __ATTR(max_brightness, 0444, backlight_show_max_brightness, NULL),
Matthew Garrettbb7ca742011-03-22 16:30:21 -0700251 __ATTR(type, 0444, backlight_show_type, NULL),
Richard Purdie655bfd72007-07-09 12:17:24 +0100252 __ATTR_NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253};
254
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255/**
Matthew Garrett325253a2009-07-14 17:06:02 +0100256 * backlight_force_update - tell the backlight subsystem that hardware state
257 * has changed
258 * @bd: the backlight device to update
259 *
260 * Updates the internal state of the backlight in response to a hardware event,
261 * and generate a uevent to notify userspace
262 */
263void backlight_force_update(struct backlight_device *bd,
264 enum backlight_update_reason reason)
265{
266 mutex_lock(&bd->ops_lock);
267 if (bd->ops && bd->ops->get_brightness)
268 bd->props.brightness = bd->ops->get_brightness(bd);
269 mutex_unlock(&bd->ops_lock);
270 backlight_generate_event(bd, reason);
271}
272EXPORT_SYMBOL(backlight_force_update);
273
274/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 * backlight_device_register - create and register a new object of
276 * backlight_device class.
277 * @name: the name of the new object(must be the same as the name of the
278 * respective framebuffer device).
Sebastian Siewiorf6ec2d92008-07-16 23:05:49 +0100279 * @parent: a pointer to the parent device
Richard Purdie655bfd72007-07-09 12:17:24 +0100280 * @devdata: an optional pointer to be stored for private driver use. The
281 * methods may retrieve it by using bl_get_data(bd).
Richard Purdie599a52d2007-02-10 23:07:48 +0000282 * @ops: the backlight operations structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 *
Richard Purdie655bfd72007-07-09 12:17:24 +0100284 * Creates and registers new backlight device. Returns either an
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 * ERR_PTR() or a pointer to the newly allocated device.
286 */
Yu Luming519ab5f2006-12-19 12:56:15 -0800287struct backlight_device *backlight_device_register(const char *name,
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500288 struct device *parent, void *devdata, const struct backlight_ops *ops,
289 const struct backlight_properties *props)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 struct backlight_device *new_bd;
Richard Purdie655bfd72007-07-09 12:17:24 +0100292 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
Richard Purdie655bfd72007-07-09 12:17:24 +0100294 pr_debug("backlight_device_register: name=%s\n", name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
Richard Purdie599a52d2007-02-10 23:07:48 +0000296 new_bd = kzalloc(sizeof(struct backlight_device), GFP_KERNEL);
Dmitry Torokhov90968e82007-02-08 00:12:28 +0000297 if (!new_bd)
Jean Delvare10ad1b72006-03-09 17:33:36 -0800298 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
Richard Purdie28ee0862007-02-08 22:25:09 +0000300 mutex_init(&new_bd->update_lock);
Richard Purdie599a52d2007-02-10 23:07:48 +0000301 mutex_init(&new_bd->ops_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
Richard Purdie655bfd72007-07-09 12:17:24 +0100303 new_bd->dev.class = backlight_class;
304 new_bd->dev.parent = parent;
305 new_bd->dev.release = bl_device_release;
Kay Sievers64dba9a2009-01-06 10:44:35 -0800306 dev_set_name(&new_bd->dev, name);
Richard Purdie655bfd72007-07-09 12:17:24 +0100307 dev_set_drvdata(&new_bd->dev, devdata);
308
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500309 /* Set default properties */
Matthew Garrettbb7ca742011-03-22 16:30:21 -0700310 if (props) {
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500311 memcpy(&new_bd->props, props,
312 sizeof(struct backlight_properties));
Matthew Garrettbb7ca742011-03-22 16:30:21 -0700313 if (props->type <= 0 || props->type >= BACKLIGHT_TYPE_MAX) {
314 WARN(1, "%s: invalid backlight type", name);
315 new_bd->props.type = BACKLIGHT_RAW;
316 }
317 } else {
318 new_bd->props.type = BACKLIGHT_RAW;
319 }
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500320
Richard Purdie655bfd72007-07-09 12:17:24 +0100321 rc = device_register(&new_bd->dev);
Dmitry Torokhov90968e82007-02-08 00:12:28 +0000322 if (rc) {
Dmitry Torokhov2fd5a152007-02-07 22:25:50 +0000323 kfree(new_bd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 return ERR_PTR(rc);
325 }
326
James Simmons3d5eead2006-12-08 02:40:47 -0800327 rc = backlight_register_fb(new_bd);
Dmitry Torokhov2fd5a152007-02-07 22:25:50 +0000328 if (rc) {
Richard Purdie655bfd72007-07-09 12:17:24 +0100329 device_unregister(&new_bd->dev);
Dmitry Torokhov2fd5a152007-02-07 22:25:50 +0000330 return ERR_PTR(rc);
331 }
332
Richard Purdie655bfd72007-07-09 12:17:24 +0100333 new_bd->ops = ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
Richard Purdie321709c2007-02-10 15:04:08 +0000335#ifdef CONFIG_PMAC_BACKLIGHT
336 mutex_lock(&pmac_backlight_mutex);
337 if (!pmac_backlight)
338 pmac_backlight = new_bd;
339 mutex_unlock(&pmac_backlight_mutex);
340#endif
341
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 return new_bd;
343}
344EXPORT_SYMBOL(backlight_device_register);
345
346/**
347 * backlight_device_unregister - unregisters a backlight device object.
348 * @bd: the backlight device object to be unregistered and freed.
349 *
350 * Unregisters a previously registered via backlight_device_register object.
351 */
352void backlight_device_unregister(struct backlight_device *bd)
353{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 if (!bd)
355 return;
356
Richard Purdie321709c2007-02-10 15:04:08 +0000357#ifdef CONFIG_PMAC_BACKLIGHT
358 mutex_lock(&pmac_backlight_mutex);
359 if (pmac_backlight == bd)
360 pmac_backlight = NULL;
361 mutex_unlock(&pmac_backlight_mutex);
362#endif
Richard Purdie599a52d2007-02-10 23:07:48 +0000363 mutex_lock(&bd->ops_lock);
364 bd->ops = NULL;
365 mutex_unlock(&bd->ops_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
James Simmons3d5eead2006-12-08 02:40:47 -0800367 backlight_unregister_fb(bd);
Richard Purdie655bfd72007-07-09 12:17:24 +0100368 device_unregister(&bd->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369}
370EXPORT_SYMBOL(backlight_device_unregister);
371
372static void __exit backlight_class_exit(void)
373{
Richard Purdie655bfd72007-07-09 12:17:24 +0100374 class_destroy(backlight_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375}
376
377static int __init backlight_class_init(void)
378{
Richard Purdie655bfd72007-07-09 12:17:24 +0100379 backlight_class = class_create(THIS_MODULE, "backlight");
380 if (IS_ERR(backlight_class)) {
381 printk(KERN_WARNING "Unable to create backlight class; errno = %ld\n",
382 PTR_ERR(backlight_class));
383 return PTR_ERR(backlight_class);
384 }
385
386 backlight_class->dev_attrs = bl_device_attributes;
Richard Purdiec835ee72009-01-06 21:00:19 +0000387 backlight_class->suspend = backlight_suspend;
388 backlight_class->resume = backlight_resume;
Richard Purdie655bfd72007-07-09 12:17:24 +0100389 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390}
391
392/*
393 * if this is compiled into the kernel, we need to ensure that the
394 * class is registered before users of the class try to register lcd's
395 */
396postcore_initcall(backlight_class_init);
397module_exit(backlight_class_exit);
398
399MODULE_LICENSE("GPL");
400MODULE_AUTHOR("Jamey Hicks <jamey.hicks@hp.com>, Andrew Zabolotny <zap@homelink.ru>");
401MODULE_DESCRIPTION("Backlight Lowlevel Control Abstraction");