blob: 6e1446ae7f52e07b1a83aef9778f5e9d0ab7eed4 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
Richard Purdie321709c2007-02-10 15:04:08 +000017#ifdef CONFIG_PMAC_BACKLIGHT
18#include <asm/backlight.h>
19#endif
James Simmons3d5eead2006-12-08 02:40:47 -080020
21#if defined(CONFIG_FB) || (defined(CONFIG_FB_MODULE) && \
22 defined(CONFIG_BACKLIGHT_CLASS_DEVICE_MODULE))
23/* This callback gets called when something important happens inside a
24 * framebuffer driver. We're looking if that important event is blanking,
25 * and if it is, we're switching backlight power as well ...
26 */
27static int fb_notifier_callback(struct notifier_block *self,
28 unsigned long event, void *data)
29{
30 struct backlight_device *bd;
31 struct fb_event *evdata = data;
32
33 /* If we aren't interested in this event, skip it immediately ... */
Richard Purdie994efac2007-02-09 09:46:45 +000034 if (event != FB_EVENT_BLANK && event != FB_EVENT_CONBLANK)
James Simmons3d5eead2006-12-08 02:40:47 -080035 return 0;
36
37 bd = container_of(self, struct backlight_device, fb_notif);
Richard Purdie599a52d2007-02-10 23:07:48 +000038 mutex_lock(&bd->ops_lock);
39 if (bd->ops)
40 if (!bd->ops->check_fb ||
41 bd->ops->check_fb(evdata->info)) {
42 bd->props.fb_blank = *(int *)evdata->data;
Richard Purdiec835ee72009-01-06 21:00:19 +000043 if (bd->props.fb_blank == FB_BLANK_UNBLANK)
44 bd->props.state &= ~BL_CORE_FBBLANK;
45 else
46 bd->props.state |= BL_CORE_FBBLANK;
Richard Purdie28ee0862007-02-08 22:25:09 +000047 backlight_update_status(bd);
James Simmons3d5eead2006-12-08 02:40:47 -080048 }
Richard Purdie599a52d2007-02-10 23:07:48 +000049 mutex_unlock(&bd->ops_lock);
James Simmons3d5eead2006-12-08 02:40:47 -080050 return 0;
51}
52
53static int backlight_register_fb(struct backlight_device *bd)
54{
55 memset(&bd->fb_notif, 0, sizeof(bd->fb_notif));
56 bd->fb_notif.notifier_call = fb_notifier_callback;
57
58 return fb_register_client(&bd->fb_notif);
59}
60
61static void backlight_unregister_fb(struct backlight_device *bd)
62{
63 fb_unregister_client(&bd->fb_notif);
64}
65#else
66static inline int backlight_register_fb(struct backlight_device *bd)
67{
68 return 0;
69}
70
71static inline void backlight_unregister_fb(struct backlight_device *bd)
72{
73}
74#endif /* CONFIG_FB */
75
Matthew Garrett325253a2009-07-14 17:06:02 +010076static void backlight_generate_event(struct backlight_device *bd,
77 enum backlight_update_reason reason)
78{
79 char *envp[2];
80
81 switch (reason) {
82 case BACKLIGHT_UPDATE_SYSFS:
83 envp[0] = "SOURCE=sysfs";
84 break;
85 case BACKLIGHT_UPDATE_HOTKEY:
86 envp[0] = "SOURCE=hotkey";
87 break;
88 default:
89 envp[0] = "SOURCE=unknown";
90 break;
91 }
92 envp[1] = NULL;
93 kobject_uevent_env(&bd->dev.kobj, KOBJ_CHANGE, envp);
94}
95
Richard Purdie655bfd72007-07-09 12:17:24 +010096static ssize_t backlight_show_power(struct device *dev,
97 struct device_attribute *attr,char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098{
Richard Purdie655bfd72007-07-09 12:17:24 +010099 struct backlight_device *bd = to_backlight_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Richard Purdie599a52d2007-02-10 23:07:48 +0000101 return sprintf(buf, "%d\n", bd->props.power);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102}
103
Richard Purdie655bfd72007-07-09 12:17:24 +0100104static ssize_t backlight_store_power(struct device *dev,
105 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106{
Pavel Machek9a2c61a2008-12-03 08:43:48 +0000107 int rc;
Richard Purdie655bfd72007-07-09 12:17:24 +0100108 struct backlight_device *bd = to_backlight_device(dev);
Pavel Machek9a2c61a2008-12-03 08:43:48 +0000109 unsigned long power;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
Pavel Machek9a2c61a2008-12-03 08:43:48 +0000111 rc = strict_strtoul(buf, 0, &power);
112 if (rc)
113 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
Pavel Machek9a2c61a2008-12-03 08:43:48 +0000115 rc = -ENXIO;
Richard Purdie599a52d2007-02-10 23:07:48 +0000116 mutex_lock(&bd->ops_lock);
117 if (bd->ops) {
Pavel Machek9a2c61a2008-12-03 08:43:48 +0000118 pr_debug("backlight: set power to %lu\n", power);
Helge Deller51552452008-01-13 23:01:13 +0000119 if (bd->props.power != power) {
120 bd->props.power = power;
121 backlight_update_status(bd);
122 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 rc = count;
Richard Purdie6ca01762006-03-31 02:31:49 -0800124 }
Richard Purdie599a52d2007-02-10 23:07:48 +0000125 mutex_unlock(&bd->ops_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
127 return rc;
128}
129
Richard Purdie655bfd72007-07-09 12:17:24 +0100130static ssize_t backlight_show_brightness(struct device *dev,
131 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132{
Richard Purdie655bfd72007-07-09 12:17:24 +0100133 struct backlight_device *bd = to_backlight_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
Richard Purdie599a52d2007-02-10 23:07:48 +0000135 return sprintf(buf, "%d\n", bd->props.brightness);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136}
137
Richard Purdie655bfd72007-07-09 12:17:24 +0100138static ssize_t backlight_store_brightness(struct device *dev,
139 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140{
Pavel Machek9a2c61a2008-12-03 08:43:48 +0000141 int rc;
Richard Purdie655bfd72007-07-09 12:17:24 +0100142 struct backlight_device *bd = to_backlight_device(dev);
Pavel Machek9a2c61a2008-12-03 08:43:48 +0000143 unsigned long brightness;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
Pavel Machek9a2c61a2008-12-03 08:43:48 +0000145 rc = strict_strtoul(buf, 0, &brightness);
146 if (rc)
147 return rc;
148
149 rc = -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
Richard Purdie599a52d2007-02-10 23:07:48 +0000151 mutex_lock(&bd->ops_lock);
152 if (bd->ops) {
153 if (brightness > bd->props.max_brightness)
Richard Purdie6ca01762006-03-31 02:31:49 -0800154 rc = -EINVAL;
155 else {
Pavel Machek9a2c61a2008-12-03 08:43:48 +0000156 pr_debug("backlight: set brightness to %lu\n",
Richard Purdie6ca01762006-03-31 02:31:49 -0800157 brightness);
Zhang Rui9be1df92009-01-08 14:11:30 +0000158 bd->props.brightness = brightness;
159 backlight_update_status(bd);
Richard Purdie6ca01762006-03-31 02:31:49 -0800160 rc = count;
161 }
162 }
Richard Purdie599a52d2007-02-10 23:07:48 +0000163 mutex_unlock(&bd->ops_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
Matthew Garrett325253a2009-07-14 17:06:02 +0100165 backlight_generate_event(bd, BACKLIGHT_UPDATE_SYSFS);
166
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 return rc;
168}
169
Richard Purdie655bfd72007-07-09 12:17:24 +0100170static ssize_t backlight_show_max_brightness(struct device *dev,
171 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172{
Richard Purdie655bfd72007-07-09 12:17:24 +0100173 struct backlight_device *bd = to_backlight_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
Richard Purdie599a52d2007-02-10 23:07:48 +0000175 return sprintf(buf, "%d\n", bd->props.max_brightness);
Richard Purdie6ca01762006-03-31 02:31:49 -0800176}
177
Richard Purdie655bfd72007-07-09 12:17:24 +0100178static ssize_t backlight_show_actual_brightness(struct device *dev,
179 struct device_attribute *attr, char *buf)
Richard Purdie6ca01762006-03-31 02:31:49 -0800180{
181 int rc = -ENXIO;
Richard Purdie655bfd72007-07-09 12:17:24 +0100182 struct backlight_device *bd = to_backlight_device(dev);
Richard Purdie6ca01762006-03-31 02:31:49 -0800183
Richard Purdie599a52d2007-02-10 23:07:48 +0000184 mutex_lock(&bd->ops_lock);
185 if (bd->ops && bd->ops->get_brightness)
186 rc = sprintf(buf, "%d\n", bd->ops->get_brightness(bd));
187 mutex_unlock(&bd->ops_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
189 return rc;
190}
191
Adrian Bunk0ad90ef2007-08-11 10:27:19 +0100192static struct class *backlight_class;
Richard Purdie655bfd72007-07-09 12:17:24 +0100193
Richard Purdiec835ee72009-01-06 21:00:19 +0000194static int backlight_suspend(struct device *dev, pm_message_t state)
195{
196 struct backlight_device *bd = to_backlight_device(dev);
197
198 if (bd->ops->options & BL_CORE_SUSPENDRESUME) {
199 mutex_lock(&bd->ops_lock);
200 bd->props.state |= BL_CORE_SUSPENDED;
201 backlight_update_status(bd);
202 mutex_unlock(&bd->ops_lock);
203 }
204
205 return 0;
206}
207
208static int backlight_resume(struct device *dev)
209{
210 struct backlight_device *bd = to_backlight_device(dev);
211
212 if (bd->ops->options & BL_CORE_SUSPENDRESUME) {
213 mutex_lock(&bd->ops_lock);
214 bd->props.state &= ~BL_CORE_SUSPENDED;
215 backlight_update_status(bd);
216 mutex_unlock(&bd->ops_lock);
217 }
218
219 return 0;
220}
221
Richard Purdie655bfd72007-07-09 12:17:24 +0100222static void bl_device_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223{
224 struct backlight_device *bd = to_backlight_device(dev);
225 kfree(bd);
226}
227
Richard Purdie655bfd72007-07-09 12:17:24 +0100228static struct device_attribute bl_device_attributes[] = {
229 __ATTR(bl_power, 0644, backlight_show_power, backlight_store_power),
230 __ATTR(brightness, 0644, backlight_show_brightness,
Richard Purdie6ca01762006-03-31 02:31:49 -0800231 backlight_store_brightness),
Richard Purdie655bfd72007-07-09 12:17:24 +0100232 __ATTR(actual_brightness, 0444, backlight_show_actual_brightness,
Richard Purdie6ca01762006-03-31 02:31:49 -0800233 NULL),
Richard Purdie655bfd72007-07-09 12:17:24 +0100234 __ATTR(max_brightness, 0444, backlight_show_max_brightness, NULL),
235 __ATTR_NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236};
237
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238/**
Matthew Garrett325253a2009-07-14 17:06:02 +0100239 * backlight_force_update - tell the backlight subsystem that hardware state
240 * has changed
241 * @bd: the backlight device to update
242 *
243 * Updates the internal state of the backlight in response to a hardware event,
244 * and generate a uevent to notify userspace
245 */
246void backlight_force_update(struct backlight_device *bd,
247 enum backlight_update_reason reason)
248{
249 mutex_lock(&bd->ops_lock);
250 if (bd->ops && bd->ops->get_brightness)
251 bd->props.brightness = bd->ops->get_brightness(bd);
252 mutex_unlock(&bd->ops_lock);
253 backlight_generate_event(bd, reason);
254}
255EXPORT_SYMBOL(backlight_force_update);
256
257/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 * backlight_device_register - create and register a new object of
259 * backlight_device class.
260 * @name: the name of the new object(must be the same as the name of the
261 * respective framebuffer device).
Sebastian Siewiorf6ec2d92008-07-16 23:05:49 +0100262 * @parent: a pointer to the parent device
Richard Purdie655bfd72007-07-09 12:17:24 +0100263 * @devdata: an optional pointer to be stored for private driver use. The
264 * methods may retrieve it by using bl_get_data(bd).
Richard Purdie599a52d2007-02-10 23:07:48 +0000265 * @ops: the backlight operations structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 *
Richard Purdie655bfd72007-07-09 12:17:24 +0100267 * Creates and registers new backlight device. Returns either an
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 * ERR_PTR() or a pointer to the newly allocated device.
269 */
Yu Luming519ab5f2006-12-19 12:56:15 -0800270struct backlight_device *backlight_device_register(const char *name,
Richard Purdie655bfd72007-07-09 12:17:24 +0100271 struct device *parent, void *devdata, struct backlight_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 struct backlight_device *new_bd;
Richard Purdie655bfd72007-07-09 12:17:24 +0100274 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
Richard Purdie655bfd72007-07-09 12:17:24 +0100276 pr_debug("backlight_device_register: name=%s\n", name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
Richard Purdie599a52d2007-02-10 23:07:48 +0000278 new_bd = kzalloc(sizeof(struct backlight_device), GFP_KERNEL);
Dmitry Torokhov90968e82007-02-08 00:12:28 +0000279 if (!new_bd)
Jean Delvare10ad1b72006-03-09 17:33:36 -0800280 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
Richard Purdie28ee0862007-02-08 22:25:09 +0000282 mutex_init(&new_bd->update_lock);
Richard Purdie599a52d2007-02-10 23:07:48 +0000283 mutex_init(&new_bd->ops_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
Richard Purdie655bfd72007-07-09 12:17:24 +0100285 new_bd->dev.class = backlight_class;
286 new_bd->dev.parent = parent;
287 new_bd->dev.release = bl_device_release;
Kay Sievers64dba9a2009-01-06 10:44:35 -0800288 dev_set_name(&new_bd->dev, name);
Richard Purdie655bfd72007-07-09 12:17:24 +0100289 dev_set_drvdata(&new_bd->dev, devdata);
290
291 rc = device_register(&new_bd->dev);
Dmitry Torokhov90968e82007-02-08 00:12:28 +0000292 if (rc) {
Dmitry Torokhov2fd5a152007-02-07 22:25:50 +0000293 kfree(new_bd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 return ERR_PTR(rc);
295 }
296
James Simmons3d5eead2006-12-08 02:40:47 -0800297 rc = backlight_register_fb(new_bd);
Dmitry Torokhov2fd5a152007-02-07 22:25:50 +0000298 if (rc) {
Richard Purdie655bfd72007-07-09 12:17:24 +0100299 device_unregister(&new_bd->dev);
Dmitry Torokhov2fd5a152007-02-07 22:25:50 +0000300 return ERR_PTR(rc);
301 }
302
Richard Purdie655bfd72007-07-09 12:17:24 +0100303 new_bd->ops = ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
Richard Purdie321709c2007-02-10 15:04:08 +0000305#ifdef CONFIG_PMAC_BACKLIGHT
306 mutex_lock(&pmac_backlight_mutex);
307 if (!pmac_backlight)
308 pmac_backlight = new_bd;
309 mutex_unlock(&pmac_backlight_mutex);
310#endif
311
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 return new_bd;
313}
314EXPORT_SYMBOL(backlight_device_register);
315
316/**
317 * backlight_device_unregister - unregisters a backlight device object.
318 * @bd: the backlight device object to be unregistered and freed.
319 *
320 * Unregisters a previously registered via backlight_device_register object.
321 */
322void backlight_device_unregister(struct backlight_device *bd)
323{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 if (!bd)
325 return;
326
Richard Purdie321709c2007-02-10 15:04:08 +0000327#ifdef CONFIG_PMAC_BACKLIGHT
328 mutex_lock(&pmac_backlight_mutex);
329 if (pmac_backlight == bd)
330 pmac_backlight = NULL;
331 mutex_unlock(&pmac_backlight_mutex);
332#endif
Richard Purdie599a52d2007-02-10 23:07:48 +0000333 mutex_lock(&bd->ops_lock);
334 bd->ops = NULL;
335 mutex_unlock(&bd->ops_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
James Simmons3d5eead2006-12-08 02:40:47 -0800337 backlight_unregister_fb(bd);
Richard Purdie655bfd72007-07-09 12:17:24 +0100338 device_unregister(&bd->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339}
340EXPORT_SYMBOL(backlight_device_unregister);
341
342static void __exit backlight_class_exit(void)
343{
Richard Purdie655bfd72007-07-09 12:17:24 +0100344 class_destroy(backlight_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345}
346
347static int __init backlight_class_init(void)
348{
Richard Purdie655bfd72007-07-09 12:17:24 +0100349 backlight_class = class_create(THIS_MODULE, "backlight");
350 if (IS_ERR(backlight_class)) {
351 printk(KERN_WARNING "Unable to create backlight class; errno = %ld\n",
352 PTR_ERR(backlight_class));
353 return PTR_ERR(backlight_class);
354 }
355
356 backlight_class->dev_attrs = bl_device_attributes;
Richard Purdiec835ee72009-01-06 21:00:19 +0000357 backlight_class->suspend = backlight_suspend;
358 backlight_class->resume = backlight_resume;
Richard Purdie655bfd72007-07-09 12:17:24 +0100359 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360}
361
362/*
363 * if this is compiled into the kernel, we need to ensure that the
364 * class is registered before users of the class try to register lcd's
365 */
366postcore_initcall(backlight_class_init);
367module_exit(backlight_class_exit);
368
369MODULE_LICENSE("GPL");
370MODULE_AUTHOR("Jamey Hicks <jamey.hicks@hp.com>, Andrew Zabolotny <zap@homelink.ru>");
371MODULE_DESCRIPTION("Backlight Lowlevel Control Abstraction");