blob: dd37cbcaf8ce164a4ac5e7293a745204ede08ac4 [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);
Andrea Righi66c1ca02009-03-31 15:25:18 -070038 if (!lock_fb_info(evdata->info))
39 return -ENODEV;
Richard Purdie599a52d2007-02-10 23:07:48 +000040 mutex_lock(&bd->ops_lock);
41 if (bd->ops)
42 if (!bd->ops->check_fb ||
43 bd->ops->check_fb(evdata->info)) {
44 bd->props.fb_blank = *(int *)evdata->data;
Richard Purdiec835ee72009-01-06 21:00:19 +000045 if (bd->props.fb_blank == FB_BLANK_UNBLANK)
46 bd->props.state &= ~BL_CORE_FBBLANK;
47 else
48 bd->props.state |= BL_CORE_FBBLANK;
Richard Purdie28ee0862007-02-08 22:25:09 +000049 backlight_update_status(bd);
James Simmons3d5eead2006-12-08 02:40:47 -080050 }
Richard Purdie599a52d2007-02-10 23:07:48 +000051 mutex_unlock(&bd->ops_lock);
Andrea Righi66c1ca02009-03-31 15:25:18 -070052 unlock_fb_info(evdata->info);
James Simmons3d5eead2006-12-08 02:40:47 -080053 return 0;
54}
55
56static int backlight_register_fb(struct backlight_device *bd)
57{
58 memset(&bd->fb_notif, 0, sizeof(bd->fb_notif));
59 bd->fb_notif.notifier_call = fb_notifier_callback;
60
61 return fb_register_client(&bd->fb_notif);
62}
63
64static void backlight_unregister_fb(struct backlight_device *bd)
65{
66 fb_unregister_client(&bd->fb_notif);
67}
68#else
69static inline int backlight_register_fb(struct backlight_device *bd)
70{
71 return 0;
72}
73
74static inline void backlight_unregister_fb(struct backlight_device *bd)
75{
76}
77#endif /* CONFIG_FB */
78
Richard Purdie655bfd72007-07-09 12:17:24 +010079static ssize_t backlight_show_power(struct device *dev,
80 struct device_attribute *attr,char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070081{
Richard Purdie655bfd72007-07-09 12:17:24 +010082 struct backlight_device *bd = to_backlight_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
Richard Purdie599a52d2007-02-10 23:07:48 +000084 return sprintf(buf, "%d\n", bd->props.power);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085}
86
Richard Purdie655bfd72007-07-09 12:17:24 +010087static ssize_t backlight_store_power(struct device *dev,
88 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -070089{
Pavel Machek9a2c61a2008-12-03 08:43:48 +000090 int rc;
Richard Purdie655bfd72007-07-09 12:17:24 +010091 struct backlight_device *bd = to_backlight_device(dev);
Pavel Machek9a2c61a2008-12-03 08:43:48 +000092 unsigned long power;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
Pavel Machek9a2c61a2008-12-03 08:43:48 +000094 rc = strict_strtoul(buf, 0, &power);
95 if (rc)
96 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Pavel Machek9a2c61a2008-12-03 08:43:48 +000098 rc = -ENXIO;
Richard Purdie599a52d2007-02-10 23:07:48 +000099 mutex_lock(&bd->ops_lock);
100 if (bd->ops) {
Pavel Machek9a2c61a2008-12-03 08:43:48 +0000101 pr_debug("backlight: set power to %lu\n", power);
Helge Deller51552452008-01-13 23:01:13 +0000102 if (bd->props.power != power) {
103 bd->props.power = power;
104 backlight_update_status(bd);
105 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 rc = count;
Richard Purdie6ca01762006-03-31 02:31:49 -0800107 }
Richard Purdie599a52d2007-02-10 23:07:48 +0000108 mutex_unlock(&bd->ops_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
110 return rc;
111}
112
Richard Purdie655bfd72007-07-09 12:17:24 +0100113static ssize_t backlight_show_brightness(struct device *dev,
114 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115{
Richard Purdie655bfd72007-07-09 12:17:24 +0100116 struct backlight_device *bd = to_backlight_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Richard Purdie599a52d2007-02-10 23:07:48 +0000118 return sprintf(buf, "%d\n", bd->props.brightness);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119}
120
Richard Purdie655bfd72007-07-09 12:17:24 +0100121static ssize_t backlight_store_brightness(struct device *dev,
122 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123{
Pavel Machek9a2c61a2008-12-03 08:43:48 +0000124 int rc;
Richard Purdie655bfd72007-07-09 12:17:24 +0100125 struct backlight_device *bd = to_backlight_device(dev);
Pavel Machek9a2c61a2008-12-03 08:43:48 +0000126 unsigned long brightness;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
Pavel Machek9a2c61a2008-12-03 08:43:48 +0000128 rc = strict_strtoul(buf, 0, &brightness);
129 if (rc)
130 return rc;
131
132 rc = -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
Richard Purdie599a52d2007-02-10 23:07:48 +0000134 mutex_lock(&bd->ops_lock);
135 if (bd->ops) {
136 if (brightness > bd->props.max_brightness)
Richard Purdie6ca01762006-03-31 02:31:49 -0800137 rc = -EINVAL;
138 else {
Pavel Machek9a2c61a2008-12-03 08:43:48 +0000139 pr_debug("backlight: set brightness to %lu\n",
Richard Purdie6ca01762006-03-31 02:31:49 -0800140 brightness);
Zhang Rui9be1df92009-01-08 14:11:30 +0000141 bd->props.brightness = brightness;
142 backlight_update_status(bd);
Richard Purdie6ca01762006-03-31 02:31:49 -0800143 rc = count;
144 }
145 }
Richard Purdie599a52d2007-02-10 23:07:48 +0000146 mutex_unlock(&bd->ops_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
148 return rc;
149}
150
Richard Purdie655bfd72007-07-09 12:17:24 +0100151static ssize_t backlight_show_max_brightness(struct device *dev,
152 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
Richard Purdie655bfd72007-07-09 12:17:24 +0100154 struct backlight_device *bd = to_backlight_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
Richard Purdie599a52d2007-02-10 23:07:48 +0000156 return sprintf(buf, "%d\n", bd->props.max_brightness);
Richard Purdie6ca01762006-03-31 02:31:49 -0800157}
158
Richard Purdie655bfd72007-07-09 12:17:24 +0100159static ssize_t backlight_show_actual_brightness(struct device *dev,
160 struct device_attribute *attr, char *buf)
Richard Purdie6ca01762006-03-31 02:31:49 -0800161{
162 int rc = -ENXIO;
Richard Purdie655bfd72007-07-09 12:17:24 +0100163 struct backlight_device *bd = to_backlight_device(dev);
Richard Purdie6ca01762006-03-31 02:31:49 -0800164
Richard Purdie599a52d2007-02-10 23:07:48 +0000165 mutex_lock(&bd->ops_lock);
166 if (bd->ops && bd->ops->get_brightness)
167 rc = sprintf(buf, "%d\n", bd->ops->get_brightness(bd));
168 mutex_unlock(&bd->ops_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
170 return rc;
171}
172
Adrian Bunk0ad90ef2007-08-11 10:27:19 +0100173static struct class *backlight_class;
Richard Purdie655bfd72007-07-09 12:17:24 +0100174
Richard Purdiec835ee72009-01-06 21:00:19 +0000175static int backlight_suspend(struct device *dev, pm_message_t state)
176{
177 struct backlight_device *bd = to_backlight_device(dev);
178
179 if (bd->ops->options & BL_CORE_SUSPENDRESUME) {
180 mutex_lock(&bd->ops_lock);
181 bd->props.state |= BL_CORE_SUSPENDED;
182 backlight_update_status(bd);
183 mutex_unlock(&bd->ops_lock);
184 }
185
186 return 0;
187}
188
189static int backlight_resume(struct device *dev)
190{
191 struct backlight_device *bd = to_backlight_device(dev);
192
193 if (bd->ops->options & BL_CORE_SUSPENDRESUME) {
194 mutex_lock(&bd->ops_lock);
195 bd->props.state &= ~BL_CORE_SUSPENDED;
196 backlight_update_status(bd);
197 mutex_unlock(&bd->ops_lock);
198 }
199
200 return 0;
201}
202
Richard Purdie655bfd72007-07-09 12:17:24 +0100203static void bl_device_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204{
205 struct backlight_device *bd = to_backlight_device(dev);
206 kfree(bd);
207}
208
Richard Purdie655bfd72007-07-09 12:17:24 +0100209static struct device_attribute bl_device_attributes[] = {
210 __ATTR(bl_power, 0644, backlight_show_power, backlight_store_power),
211 __ATTR(brightness, 0644, backlight_show_brightness,
Richard Purdie6ca01762006-03-31 02:31:49 -0800212 backlight_store_brightness),
Richard Purdie655bfd72007-07-09 12:17:24 +0100213 __ATTR(actual_brightness, 0444, backlight_show_actual_brightness,
Richard Purdie6ca01762006-03-31 02:31:49 -0800214 NULL),
Richard Purdie655bfd72007-07-09 12:17:24 +0100215 __ATTR(max_brightness, 0444, backlight_show_max_brightness, NULL),
216 __ATTR_NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217};
218
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219/**
220 * backlight_device_register - create and register a new object of
221 * backlight_device class.
222 * @name: the name of the new object(must be the same as the name of the
223 * respective framebuffer device).
Sebastian Siewiorf6ec2d92008-07-16 23:05:49 +0100224 * @parent: a pointer to the parent device
Richard Purdie655bfd72007-07-09 12:17:24 +0100225 * @devdata: an optional pointer to be stored for private driver use. The
226 * methods may retrieve it by using bl_get_data(bd).
Richard Purdie599a52d2007-02-10 23:07:48 +0000227 * @ops: the backlight operations structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 *
Richard Purdie655bfd72007-07-09 12:17:24 +0100229 * Creates and registers new backlight device. Returns either an
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 * ERR_PTR() or a pointer to the newly allocated device.
231 */
Yu Luming519ab5f2006-12-19 12:56:15 -0800232struct backlight_device *backlight_device_register(const char *name,
Richard Purdie655bfd72007-07-09 12:17:24 +0100233 struct device *parent, void *devdata, struct backlight_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 struct backlight_device *new_bd;
Richard Purdie655bfd72007-07-09 12:17:24 +0100236 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
Richard Purdie655bfd72007-07-09 12:17:24 +0100238 pr_debug("backlight_device_register: name=%s\n", name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
Richard Purdie599a52d2007-02-10 23:07:48 +0000240 new_bd = kzalloc(sizeof(struct backlight_device), GFP_KERNEL);
Dmitry Torokhov90968e82007-02-08 00:12:28 +0000241 if (!new_bd)
Jean Delvare10ad1b72006-03-09 17:33:36 -0800242 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
Richard Purdie28ee0862007-02-08 22:25:09 +0000244 mutex_init(&new_bd->update_lock);
Richard Purdie599a52d2007-02-10 23:07:48 +0000245 mutex_init(&new_bd->ops_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
Richard Purdie655bfd72007-07-09 12:17:24 +0100247 new_bd->dev.class = backlight_class;
248 new_bd->dev.parent = parent;
249 new_bd->dev.release = bl_device_release;
Kay Sievers64dba9a2009-01-06 10:44:35 -0800250 dev_set_name(&new_bd->dev, name);
Richard Purdie655bfd72007-07-09 12:17:24 +0100251 dev_set_drvdata(&new_bd->dev, devdata);
252
253 rc = device_register(&new_bd->dev);
Dmitry Torokhov90968e82007-02-08 00:12:28 +0000254 if (rc) {
Dmitry Torokhov2fd5a152007-02-07 22:25:50 +0000255 kfree(new_bd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 return ERR_PTR(rc);
257 }
258
James Simmons3d5eead2006-12-08 02:40:47 -0800259 rc = backlight_register_fb(new_bd);
Dmitry Torokhov2fd5a152007-02-07 22:25:50 +0000260 if (rc) {
Richard Purdie655bfd72007-07-09 12:17:24 +0100261 device_unregister(&new_bd->dev);
Dmitry Torokhov2fd5a152007-02-07 22:25:50 +0000262 return ERR_PTR(rc);
263 }
264
Richard Purdie655bfd72007-07-09 12:17:24 +0100265 new_bd->ops = ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
Richard Purdie321709c2007-02-10 15:04:08 +0000267#ifdef CONFIG_PMAC_BACKLIGHT
268 mutex_lock(&pmac_backlight_mutex);
269 if (!pmac_backlight)
270 pmac_backlight = new_bd;
271 mutex_unlock(&pmac_backlight_mutex);
272#endif
273
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 return new_bd;
275}
276EXPORT_SYMBOL(backlight_device_register);
277
278/**
279 * backlight_device_unregister - unregisters a backlight device object.
280 * @bd: the backlight device object to be unregistered and freed.
281 *
282 * Unregisters a previously registered via backlight_device_register object.
283 */
284void backlight_device_unregister(struct backlight_device *bd)
285{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 if (!bd)
287 return;
288
Richard Purdie321709c2007-02-10 15:04:08 +0000289#ifdef CONFIG_PMAC_BACKLIGHT
290 mutex_lock(&pmac_backlight_mutex);
291 if (pmac_backlight == bd)
292 pmac_backlight = NULL;
293 mutex_unlock(&pmac_backlight_mutex);
294#endif
Richard Purdie599a52d2007-02-10 23:07:48 +0000295 mutex_lock(&bd->ops_lock);
296 bd->ops = NULL;
297 mutex_unlock(&bd->ops_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
James Simmons3d5eead2006-12-08 02:40:47 -0800299 backlight_unregister_fb(bd);
Richard Purdie655bfd72007-07-09 12:17:24 +0100300 device_unregister(&bd->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301}
302EXPORT_SYMBOL(backlight_device_unregister);
303
304static void __exit backlight_class_exit(void)
305{
Richard Purdie655bfd72007-07-09 12:17:24 +0100306 class_destroy(backlight_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307}
308
309static int __init backlight_class_init(void)
310{
Richard Purdie655bfd72007-07-09 12:17:24 +0100311 backlight_class = class_create(THIS_MODULE, "backlight");
312 if (IS_ERR(backlight_class)) {
313 printk(KERN_WARNING "Unable to create backlight class; errno = %ld\n",
314 PTR_ERR(backlight_class));
315 return PTR_ERR(backlight_class);
316 }
317
318 backlight_class->dev_attrs = bl_device_attributes;
Richard Purdiec835ee72009-01-06 21:00:19 +0000319 backlight_class->suspend = backlight_suspend;
320 backlight_class->resume = backlight_resume;
Richard Purdie655bfd72007-07-09 12:17:24 +0100321 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322}
323
324/*
325 * if this is compiled into the kernel, we need to ensure that the
326 * class is registered before users of the class try to register lcd's
327 */
328postcore_initcall(backlight_class_init);
329module_exit(backlight_class_exit);
330
331MODULE_LICENSE("GPL");
332MODULE_AUTHOR("Jamey Hicks <jamey.hicks@hp.com>, Andrew Zabolotny <zap@homelink.ru>");
333MODULE_DESCRIPTION("Backlight Lowlevel Control Abstraction");