Linux 2.6.26-rc5
[linux-2.6/openmoko-kernel/knife-kernel.git] / drivers / video / backlight / backlight.c
blob39394757679c0882f9a2765486163383bee0935c
1 /*
2 * Backlight Lowlevel Control Abstraction
4 * Copyright (C) 2003,2004 Hewlett-Packard Company
6 */
8 #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>
17 #ifdef CONFIG_PMAC_BACKLIGHT
18 #include <asm/backlight.h>
19 #endif
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 ...
27 static int fb_notifier_callback(struct notifier_block *self,
28 unsigned long event, void *data)
30 struct backlight_device *bd;
31 struct fb_event *evdata = data;
33 /* If we aren't interested in this event, skip it immediately ... */
34 if (event != FB_EVENT_BLANK && event != FB_EVENT_CONBLANK)
35 return 0;
37 bd = container_of(self, struct backlight_device, fb_notif);
38 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;
43 backlight_update_status(bd);
45 mutex_unlock(&bd->ops_lock);
46 return 0;
49 static int backlight_register_fb(struct backlight_device *bd)
51 memset(&bd->fb_notif, 0, sizeof(bd->fb_notif));
52 bd->fb_notif.notifier_call = fb_notifier_callback;
54 return fb_register_client(&bd->fb_notif);
57 static void backlight_unregister_fb(struct backlight_device *bd)
59 fb_unregister_client(&bd->fb_notif);
61 #else
62 static inline int backlight_register_fb(struct backlight_device *bd)
64 return 0;
67 static inline void backlight_unregister_fb(struct backlight_device *bd)
70 #endif /* CONFIG_FB */
72 static ssize_t backlight_show_power(struct device *dev,
73 struct device_attribute *attr,char *buf)
75 struct backlight_device *bd = to_backlight_device(dev);
77 return sprintf(buf, "%d\n", bd->props.power);
80 static ssize_t backlight_store_power(struct device *dev,
81 struct device_attribute *attr, const char *buf, size_t count)
83 int rc = -ENXIO;
84 char *endp;
85 struct backlight_device *bd = to_backlight_device(dev);
86 int power = simple_strtoul(buf, &endp, 0);
87 size_t size = endp - buf;
89 if (*endp && isspace(*endp))
90 size++;
91 if (size != count)
92 return -EINVAL;
94 mutex_lock(&bd->ops_lock);
95 if (bd->ops) {
96 pr_debug("backlight: set power to %d\n", power);
97 if (bd->props.power != power) {
98 bd->props.power = power;
99 backlight_update_status(bd);
101 rc = count;
103 mutex_unlock(&bd->ops_lock);
105 return rc;
108 static ssize_t backlight_show_brightness(struct device *dev,
109 struct device_attribute *attr, char *buf)
111 struct backlight_device *bd = to_backlight_device(dev);
113 return sprintf(buf, "%d\n", bd->props.brightness);
116 static ssize_t backlight_store_brightness(struct device *dev,
117 struct device_attribute *attr, const char *buf, size_t count)
119 int rc = -ENXIO;
120 char *endp;
121 struct backlight_device *bd = to_backlight_device(dev);
122 int brightness = simple_strtoul(buf, &endp, 0);
123 size_t size = endp - buf;
125 if (*endp && isspace(*endp))
126 size++;
127 if (size != count)
128 return -EINVAL;
130 mutex_lock(&bd->ops_lock);
131 if (bd->ops) {
132 if (brightness > bd->props.max_brightness)
133 rc = -EINVAL;
134 else {
135 pr_debug("backlight: set brightness to %d\n",
136 brightness);
137 if (bd->props.brightness != brightness) {
138 bd->props.brightness = brightness;
139 backlight_update_status(bd);
141 rc = count;
144 mutex_unlock(&bd->ops_lock);
146 return rc;
149 static ssize_t backlight_show_max_brightness(struct device *dev,
150 struct device_attribute *attr, char *buf)
152 struct backlight_device *bd = to_backlight_device(dev);
154 return sprintf(buf, "%d\n", bd->props.max_brightness);
157 static ssize_t backlight_show_actual_brightness(struct device *dev,
158 struct device_attribute *attr, char *buf)
160 int rc = -ENXIO;
161 struct backlight_device *bd = to_backlight_device(dev);
163 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);
168 return rc;
171 static struct class *backlight_class;
173 static void bl_device_release(struct device *dev)
175 struct backlight_device *bd = to_backlight_device(dev);
176 kfree(bd);
179 static struct device_attribute bl_device_attributes[] = {
180 __ATTR(bl_power, 0644, backlight_show_power, backlight_store_power),
181 __ATTR(brightness, 0644, backlight_show_brightness,
182 backlight_store_brightness),
183 __ATTR(actual_brightness, 0444, backlight_show_actual_brightness,
184 NULL),
185 __ATTR(max_brightness, 0444, backlight_show_max_brightness, NULL),
186 __ATTR_NULL,
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).
194 * @devdata: an optional pointer to be stored for private driver use. The
195 * methods may retrieve it by using bl_get_data(bd).
196 * @ops: the backlight operations structure.
198 * Creates and registers new backlight device. Returns either an
199 * ERR_PTR() or a pointer to the newly allocated device.
201 struct backlight_device *backlight_device_register(const char *name,
202 struct device *parent, void *devdata, struct backlight_ops *ops)
204 struct backlight_device *new_bd;
205 int rc;
207 pr_debug("backlight_device_register: name=%s\n", name);
209 new_bd = kzalloc(sizeof(struct backlight_device), GFP_KERNEL);
210 if (!new_bd)
211 return ERR_PTR(-ENOMEM);
213 mutex_init(&new_bd->update_lock);
214 mutex_init(&new_bd->ops_lock);
216 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);
222 rc = device_register(&new_bd->dev);
223 if (rc) {
224 kfree(new_bd);
225 return ERR_PTR(rc);
228 rc = backlight_register_fb(new_bd);
229 if (rc) {
230 device_unregister(&new_bd->dev);
231 return ERR_PTR(rc);
234 new_bd->ops = ops;
236 #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
243 return new_bd;
245 EXPORT_SYMBOL(backlight_device_register);
248 * backlight_device_unregister - unregisters a backlight device object.
249 * @bd: the backlight device object to be unregistered and freed.
251 * Unregisters a previously registered via backlight_device_register object.
253 void backlight_device_unregister(struct backlight_device *bd)
255 if (!bd)
256 return;
258 #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
264 mutex_lock(&bd->ops_lock);
265 bd->ops = NULL;
266 mutex_unlock(&bd->ops_lock);
268 backlight_unregister_fb(bd);
269 device_unregister(&bd->dev);
271 EXPORT_SYMBOL(backlight_device_unregister);
273 static void __exit backlight_class_exit(void)
275 class_destroy(backlight_class);
278 static int __init backlight_class_init(void)
280 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);
287 backlight_class->dev_attrs = bl_device_attributes;
288 return 0;
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
295 postcore_initcall(backlight_class_init);
296 module_exit(backlight_class_exit);
298 MODULE_LICENSE("GPL");
299 MODULE_AUTHOR("Jamey Hicks <jamey.hicks@hp.com>, Andrew Zabolotny <zap@homelink.ru>");
300 MODULE_DESCRIPTION("Backlight Lowlevel Control Abstraction");