blob: 39394757679c0882f9a2765486163383bee0935c [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 Purdie28ee0862007-02-08 22:25:09 +000043 backlight_update_status(bd);
James Simmons3d5eead2006-12-08 02:40:47 -080044 }
Richard Purdie599a52d2007-02-10 23:07:48 +000045 mutex_unlock(&bd->ops_lock);
James Simmons3d5eead2006-12-08 02:40:47 -080046 return 0;
47}
48
49static int backlight_register_fb(struct backlight_device *bd)
50{
51 memset(&bd->fb_notif, 0, sizeof(bd->fb_notif));
52 bd->fb_notif.notifier_call = fb_notifier_callback;
53
54 return fb_register_client(&bd->fb_notif);
55}
56
57static void backlight_unregister_fb(struct backlight_device *bd)
58{
59 fb_unregister_client(&bd->fb_notif);
60}
61#else
62static inline int backlight_register_fb(struct backlight_device *bd)
63{
64 return 0;
65}
66
67static inline void backlight_unregister_fb(struct backlight_device *bd)
68{
69}
70#endif /* CONFIG_FB */
71
Richard Purdie655bfd72007-07-09 12:17:24 +010072static ssize_t backlight_show_power(struct device *dev,
73 struct device_attribute *attr,char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074{
Richard Purdie655bfd72007-07-09 12:17:24 +010075 struct backlight_device *bd = to_backlight_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Richard Purdie599a52d2007-02-10 23:07:48 +000077 return sprintf(buf, "%d\n", bd->props.power);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078}
79
Richard Purdie655bfd72007-07-09 12:17:24 +010080static ssize_t backlight_store_power(struct device *dev,
81 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082{
Richard Purdie68673af2006-05-15 09:44:15 -070083 int rc = -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 char *endp;
Richard Purdie655bfd72007-07-09 12:17:24 +010085 struct backlight_device *bd = to_backlight_device(dev);
Richard Purdie68673af2006-05-15 09:44:15 -070086 int power = simple_strtoul(buf, &endp, 0);
87 size_t size = endp - buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Richard Purdie68673af2006-05-15 09:44:15 -070089 if (*endp && isspace(*endp))
90 size++;
91 if (size != count)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 return -EINVAL;
93
Richard Purdie599a52d2007-02-10 23:07:48 +000094 mutex_lock(&bd->ops_lock);
95 if (bd->ops) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 pr_debug("backlight: set power to %d\n", power);
Helge Deller51552452008-01-13 23:01:13 +000097 if (bd->props.power != power) {
98 bd->props.power = power;
99 backlight_update_status(bd);
100 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 rc = count;
Richard Purdie6ca01762006-03-31 02:31:49 -0800102 }
Richard Purdie599a52d2007-02-10 23:07:48 +0000103 mutex_unlock(&bd->ops_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
105 return rc;
106}
107
Richard Purdie655bfd72007-07-09 12:17:24 +0100108static ssize_t backlight_show_brightness(struct device *dev,
109 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110{
Richard Purdie655bfd72007-07-09 12:17:24 +0100111 struct backlight_device *bd = to_backlight_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
Richard Purdie599a52d2007-02-10 23:07:48 +0000113 return sprintf(buf, "%d\n", bd->props.brightness);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114}
115
Richard Purdie655bfd72007-07-09 12:17:24 +0100116static ssize_t backlight_store_brightness(struct device *dev,
117 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118{
Richard Purdie68673af2006-05-15 09:44:15 -0700119 int rc = -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 char *endp;
Richard Purdie655bfd72007-07-09 12:17:24 +0100121 struct backlight_device *bd = to_backlight_device(dev);
Richard Purdie68673af2006-05-15 09:44:15 -0700122 int brightness = simple_strtoul(buf, &endp, 0);
123 size_t size = endp - buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Richard Purdie68673af2006-05-15 09:44:15 -0700125 if (*endp && isspace(*endp))
126 size++;
127 if (size != count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 return -EINVAL;
129
Richard Purdie599a52d2007-02-10 23:07:48 +0000130 mutex_lock(&bd->ops_lock);
131 if (bd->ops) {
132 if (brightness > bd->props.max_brightness)
Richard Purdie6ca01762006-03-31 02:31:49 -0800133 rc = -EINVAL;
134 else {
135 pr_debug("backlight: set brightness to %d\n",
136 brightness);
Helge Deller51552452008-01-13 23:01:13 +0000137 if (bd->props.brightness != brightness) {
138 bd->props.brightness = brightness;
139 backlight_update_status(bd);
140 }
Richard Purdie6ca01762006-03-31 02:31:49 -0800141 rc = count;
142 }
143 }
Richard Purdie599a52d2007-02-10 23:07:48 +0000144 mutex_unlock(&bd->ops_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
146 return rc;
147}
148
Richard Purdie655bfd72007-07-09 12:17:24 +0100149static ssize_t backlight_show_max_brightness(struct device *dev,
150 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151{
Richard Purdie655bfd72007-07-09 12:17:24 +0100152 struct backlight_device *bd = to_backlight_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Richard Purdie599a52d2007-02-10 23:07:48 +0000154 return sprintf(buf, "%d\n", bd->props.max_brightness);
Richard Purdie6ca01762006-03-31 02:31:49 -0800155}
156
Richard Purdie655bfd72007-07-09 12:17:24 +0100157static ssize_t backlight_show_actual_brightness(struct device *dev,
158 struct device_attribute *attr, char *buf)
Richard Purdie6ca01762006-03-31 02:31:49 -0800159{
160 int rc = -ENXIO;
Richard Purdie655bfd72007-07-09 12:17:24 +0100161 struct backlight_device *bd = to_backlight_device(dev);
Richard Purdie6ca01762006-03-31 02:31:49 -0800162
Richard Purdie599a52d2007-02-10 23:07:48 +0000163 mutex_lock(&bd->ops_lock);
164 if (bd->ops && bd->ops->get_brightness)
165 rc = sprintf(buf, "%d\n", bd->ops->get_brightness(bd));
166 mutex_unlock(&bd->ops_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
168 return rc;
169}
170
Adrian Bunk0ad90ef2007-08-11 10:27:19 +0100171static struct class *backlight_class;
Richard Purdie655bfd72007-07-09 12:17:24 +0100172
173static void bl_device_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174{
175 struct backlight_device *bd = to_backlight_device(dev);
176 kfree(bd);
177}
178
Richard Purdie655bfd72007-07-09 12:17:24 +0100179static struct device_attribute bl_device_attributes[] = {
180 __ATTR(bl_power, 0644, backlight_show_power, backlight_store_power),
181 __ATTR(brightness, 0644, backlight_show_brightness,
Richard Purdie6ca01762006-03-31 02:31:49 -0800182 backlight_store_brightness),
Richard Purdie655bfd72007-07-09 12:17:24 +0100183 __ATTR(actual_brightness, 0444, backlight_show_actual_brightness,
Richard Purdie6ca01762006-03-31 02:31:49 -0800184 NULL),
Richard Purdie655bfd72007-07-09 12:17:24 +0100185 __ATTR(max_brightness, 0444, backlight_show_max_brightness, NULL),
186 __ATTR_NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187};
188
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189/**
190 * backlight_device_register - create and register a new object of
191 * backlight_device class.
192 * @name: the name of the new object(must be the same as the name of the
193 * respective framebuffer device).
Richard Purdie655bfd72007-07-09 12:17:24 +0100194 * @devdata: an optional pointer to be stored for private driver use. The
195 * methods may retrieve it by using bl_get_data(bd).
Richard Purdie599a52d2007-02-10 23:07:48 +0000196 * @ops: the backlight operations structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 *
Richard Purdie655bfd72007-07-09 12:17:24 +0100198 * Creates and registers new backlight device. Returns either an
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 * ERR_PTR() or a pointer to the newly allocated device.
200 */
Yu Luming519ab5f2006-12-19 12:56:15 -0800201struct backlight_device *backlight_device_register(const char *name,
Richard Purdie655bfd72007-07-09 12:17:24 +0100202 struct device *parent, void *devdata, struct backlight_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 struct backlight_device *new_bd;
Richard Purdie655bfd72007-07-09 12:17:24 +0100205 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
Richard Purdie655bfd72007-07-09 12:17:24 +0100207 pr_debug("backlight_device_register: name=%s\n", name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
Richard Purdie599a52d2007-02-10 23:07:48 +0000209 new_bd = kzalloc(sizeof(struct backlight_device), GFP_KERNEL);
Dmitry Torokhov90968e82007-02-08 00:12:28 +0000210 if (!new_bd)
Jean Delvare10ad1b72006-03-09 17:33:36 -0800211 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
Richard Purdie28ee0862007-02-08 22:25:09 +0000213 mutex_init(&new_bd->update_lock);
Richard Purdie599a52d2007-02-10 23:07:48 +0000214 mutex_init(&new_bd->ops_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
Richard Purdie655bfd72007-07-09 12:17:24 +0100216 new_bd->dev.class = backlight_class;
217 new_bd->dev.parent = parent;
218 new_bd->dev.release = bl_device_release;
219 strlcpy(new_bd->dev.bus_id, name, BUS_ID_SIZE);
220 dev_set_drvdata(&new_bd->dev, devdata);
221
222 rc = device_register(&new_bd->dev);
Dmitry Torokhov90968e82007-02-08 00:12:28 +0000223 if (rc) {
Dmitry Torokhov2fd5a152007-02-07 22:25:50 +0000224 kfree(new_bd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 return ERR_PTR(rc);
226 }
227
James Simmons3d5eead2006-12-08 02:40:47 -0800228 rc = backlight_register_fb(new_bd);
Dmitry Torokhov2fd5a152007-02-07 22:25:50 +0000229 if (rc) {
Richard Purdie655bfd72007-07-09 12:17:24 +0100230 device_unregister(&new_bd->dev);
Dmitry Torokhov2fd5a152007-02-07 22:25:50 +0000231 return ERR_PTR(rc);
232 }
233
Richard Purdie655bfd72007-07-09 12:17:24 +0100234 new_bd->ops = ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
Richard Purdie321709c2007-02-10 15:04:08 +0000236#ifdef CONFIG_PMAC_BACKLIGHT
237 mutex_lock(&pmac_backlight_mutex);
238 if (!pmac_backlight)
239 pmac_backlight = new_bd;
240 mutex_unlock(&pmac_backlight_mutex);
241#endif
242
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 return new_bd;
244}
245EXPORT_SYMBOL(backlight_device_register);
246
247/**
248 * backlight_device_unregister - unregisters a backlight device object.
249 * @bd: the backlight device object to be unregistered and freed.
250 *
251 * Unregisters a previously registered via backlight_device_register object.
252 */
253void backlight_device_unregister(struct backlight_device *bd)
254{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 if (!bd)
256 return;
257
Richard Purdie321709c2007-02-10 15:04:08 +0000258#ifdef CONFIG_PMAC_BACKLIGHT
259 mutex_lock(&pmac_backlight_mutex);
260 if (pmac_backlight == bd)
261 pmac_backlight = NULL;
262 mutex_unlock(&pmac_backlight_mutex);
263#endif
Richard Purdie599a52d2007-02-10 23:07:48 +0000264 mutex_lock(&bd->ops_lock);
265 bd->ops = NULL;
266 mutex_unlock(&bd->ops_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
James Simmons3d5eead2006-12-08 02:40:47 -0800268 backlight_unregister_fb(bd);
Richard Purdie655bfd72007-07-09 12:17:24 +0100269 device_unregister(&bd->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270}
271EXPORT_SYMBOL(backlight_device_unregister);
272
273static void __exit backlight_class_exit(void)
274{
Richard Purdie655bfd72007-07-09 12:17:24 +0100275 class_destroy(backlight_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276}
277
278static int __init backlight_class_init(void)
279{
Richard Purdie655bfd72007-07-09 12:17:24 +0100280 backlight_class = class_create(THIS_MODULE, "backlight");
281 if (IS_ERR(backlight_class)) {
282 printk(KERN_WARNING "Unable to create backlight class; errno = %ld\n",
283 PTR_ERR(backlight_class));
284 return PTR_ERR(backlight_class);
285 }
286
287 backlight_class->dev_attrs = bl_device_attributes;
288 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289}
290
291/*
292 * if this is compiled into the kernel, we need to ensure that the
293 * class is registered before users of the class try to register lcd's
294 */
295postcore_initcall(backlight_class_init);
296module_exit(backlight_class_exit);
297
298MODULE_LICENSE("GPL");
299MODULE_AUTHOR("Jamey Hicks <jamey.hicks@hp.com>, Andrew Zabolotny <zap@homelink.ru>");
300MODULE_DESCRIPTION("Backlight Lowlevel Control Abstraction");