blob: 1d97cdf6f38249ca3bb8b24f3f2be512f2df1cde [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
17static ssize_t backlight_show_power(struct class_device *cdev, char *buf)
18{
Richard Purdie6ca01762006-03-31 02:31:49 -080019 int rc = -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070020 struct backlight_device *bd = to_backlight_device(cdev);
21
22 down(&bd->sem);
Richard Purdie6ca01762006-03-31 02:31:49 -080023 if (likely(bd->props))
24 rc = sprintf(buf, "%d\n", bd->props->power);
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 up(&bd->sem);
26
27 return rc;
28}
29
30static ssize_t backlight_store_power(struct class_device *cdev, const char *buf, size_t count)
31{
Richard Purdie68673af2006-05-15 09:44:15 -070032 int rc = -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 char *endp;
34 struct backlight_device *bd = to_backlight_device(cdev);
Richard Purdie68673af2006-05-15 09:44:15 -070035 int power = simple_strtoul(buf, &endp, 0);
36 size_t size = endp - buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Richard Purdie68673af2006-05-15 09:44:15 -070038 if (*endp && isspace(*endp))
39 size++;
40 if (size != count)
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 return -EINVAL;
42
43 down(&bd->sem);
Richard Purdie6ca01762006-03-31 02:31:49 -080044 if (likely(bd->props)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 pr_debug("backlight: set power to %d\n", power);
Richard Purdie6ca01762006-03-31 02:31:49 -080046 bd->props->power = power;
47 if (likely(bd->props->update_status))
48 bd->props->update_status(bd);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 rc = count;
Richard Purdie6ca01762006-03-31 02:31:49 -080050 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 up(&bd->sem);
52
53 return rc;
54}
55
56static ssize_t backlight_show_brightness(struct class_device *cdev, char *buf)
57{
Richard Purdie6ca01762006-03-31 02:31:49 -080058 int rc = -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 struct backlight_device *bd = to_backlight_device(cdev);
60
61 down(&bd->sem);
Richard Purdie6ca01762006-03-31 02:31:49 -080062 if (likely(bd->props))
63 rc = sprintf(buf, "%d\n", bd->props->brightness);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 up(&bd->sem);
65
66 return rc;
67}
68
69static ssize_t backlight_store_brightness(struct class_device *cdev, const char *buf, size_t count)
70{
Richard Purdie68673af2006-05-15 09:44:15 -070071 int rc = -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 char *endp;
73 struct backlight_device *bd = to_backlight_device(cdev);
Richard Purdie68673af2006-05-15 09:44:15 -070074 int brightness = simple_strtoul(buf, &endp, 0);
75 size_t size = endp - buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Richard Purdie68673af2006-05-15 09:44:15 -070077 if (*endp && isspace(*endp))
78 size++;
79 if (size != count)
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 return -EINVAL;
81
82 down(&bd->sem);
Richard Purdie6ca01762006-03-31 02:31:49 -080083 if (likely(bd->props)) {
84 if (brightness > bd->props->max_brightness)
85 rc = -EINVAL;
86 else {
87 pr_debug("backlight: set brightness to %d\n",
88 brightness);
89 bd->props->brightness = brightness;
90 if (likely(bd->props->update_status))
91 bd->props->update_status(bd);
92 rc = count;
93 }
94 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 up(&bd->sem);
96
97 return rc;
98}
99
100static ssize_t backlight_show_max_brightness(struct class_device *cdev, char *buf)
101{
Richard Purdie6ca01762006-03-31 02:31:49 -0800102 int rc = -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 struct backlight_device *bd = to_backlight_device(cdev);
104
105 down(&bd->sem);
106 if (likely(bd->props))
107 rc = sprintf(buf, "%d\n", bd->props->max_brightness);
Richard Purdie6ca01762006-03-31 02:31:49 -0800108 up(&bd->sem);
109
110 return rc;
111}
112
113static ssize_t backlight_show_actual_brightness(struct class_device *cdev,
114 char *buf)
115{
116 int rc = -ENXIO;
117 struct backlight_device *bd = to_backlight_device(cdev);
118
119 down(&bd->sem);
120 if (likely(bd->props && bd->props->get_brightness))
121 rc = sprintf(buf, "%d\n", bd->props->get_brightness(bd));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 up(&bd->sem);
123
124 return rc;
125}
126
127static void backlight_class_release(struct class_device *dev)
128{
129 struct backlight_device *bd = to_backlight_device(dev);
130 kfree(bd);
131}
132
133static struct class backlight_class = {
134 .name = "backlight",
135 .release = backlight_class_release,
136};
137
138#define DECLARE_ATTR(_name,_mode,_show,_store) \
139{ \
140 .attr = { .name = __stringify(_name), .mode = _mode, .owner = THIS_MODULE }, \
141 .show = _show, \
142 .store = _store, \
143}
144
145static struct class_device_attribute bl_class_device_attributes[] = {
146 DECLARE_ATTR(power, 0644, backlight_show_power, backlight_store_power),
Richard Purdie6ca01762006-03-31 02:31:49 -0800147 DECLARE_ATTR(brightness, 0644, backlight_show_brightness,
148 backlight_store_brightness),
149 DECLARE_ATTR(actual_brightness, 0444, backlight_show_actual_brightness,
150 NULL),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 DECLARE_ATTR(max_brightness, 0444, backlight_show_max_brightness, NULL),
152};
153
154/* This callback gets called when something important happens inside a
155 * framebuffer driver. We're looking if that important event is blanking,
156 * and if it is, we're switching backlight power as well ...
157 */
158static int fb_notifier_callback(struct notifier_block *self,
159 unsigned long event, void *data)
160{
161 struct backlight_device *bd;
162 struct fb_event *evdata =(struct fb_event *)data;
163
164 /* If we aren't interested in this event, skip it immediately ... */
165 if (event != FB_EVENT_BLANK)
166 return 0;
167
168 bd = container_of(self, struct backlight_device, fb_notif);
169 down(&bd->sem);
170 if (bd->props)
Richard Purdie6ca01762006-03-31 02:31:49 -0800171 if (!bd->props->check_fb ||
172 bd->props->check_fb(evdata->info)) {
173 bd->props->fb_blank = *(int *)evdata->data;
174 if (likely(bd->props && bd->props->update_status))
175 bd->props->update_status(bd);
176 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 up(&bd->sem);
178 return 0;
179}
180
181/**
182 * backlight_device_register - create and register a new object of
183 * backlight_device class.
184 * @name: the name of the new object(must be the same as the name of the
185 * respective framebuffer device).
186 * @devdata: an optional pointer to be stored in the class_device. The
187 * methods may retrieve it by using class_get_devdata(&bd->class_dev).
188 * @bp: the backlight properties structure.
189 *
190 * Creates and registers new backlight class_device. Returns either an
191 * ERR_PTR() or a pointer to the newly allocated device.
192 */
Yu Luming519ab5f2006-12-19 12:56:15 -0800193struct backlight_device *backlight_device_register(const char *name,
194 struct device *dev,
195 void *devdata,
196 struct backlight_properties *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197{
198 int i, rc;
199 struct backlight_device *new_bd;
200
201 pr_debug("backlight_device_alloc: name=%s\n", name);
202
203 new_bd = kmalloc(sizeof(struct backlight_device), GFP_KERNEL);
204 if (unlikely(!new_bd))
Jean Delvare10ad1b72006-03-09 17:33:36 -0800205 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
207 init_MUTEX(&new_bd->sem);
208 new_bd->props = bp;
209 memset(&new_bd->class_dev, 0, sizeof(new_bd->class_dev));
210 new_bd->class_dev.class = &backlight_class;
Yu Luming519ab5f2006-12-19 12:56:15 -0800211 new_bd->class_dev.dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 strlcpy(new_bd->class_dev.class_id, name, KOBJ_NAME_LEN);
213 class_set_devdata(&new_bd->class_dev, devdata);
214
215 rc = class_device_register(&new_bd->class_dev);
216 if (unlikely(rc)) {
217error: kfree(new_bd);
218 return ERR_PTR(rc);
219 }
220
221 memset(&new_bd->fb_notif, 0, sizeof(new_bd->fb_notif));
222 new_bd->fb_notif.notifier_call = fb_notifier_callback;
223
224 rc = fb_register_client(&new_bd->fb_notif);
225 if (unlikely(rc))
226 goto error;
227
228 for (i = 0; i < ARRAY_SIZE(bl_class_device_attributes); i++) {
229 rc = class_device_create_file(&new_bd->class_dev,
230 &bl_class_device_attributes[i]);
231 if (unlikely(rc)) {
232 while (--i >= 0)
233 class_device_remove_file(&new_bd->class_dev,
234 &bl_class_device_attributes[i]);
235 class_device_unregister(&new_bd->class_dev);
236 /* No need to kfree(new_bd) since release() method was called */
237 return ERR_PTR(rc);
238 }
239 }
240
241 return new_bd;
242}
243EXPORT_SYMBOL(backlight_device_register);
244
245/**
246 * backlight_device_unregister - unregisters a backlight device object.
247 * @bd: the backlight device object to be unregistered and freed.
248 *
249 * Unregisters a previously registered via backlight_device_register object.
250 */
251void backlight_device_unregister(struct backlight_device *bd)
252{
253 int i;
254
255 if (!bd)
256 return;
257
258 pr_debug("backlight_device_unregister: name=%s\n", bd->class_dev.class_id);
259
260 for (i = 0; i < ARRAY_SIZE(bl_class_device_attributes); i++)
261 class_device_remove_file(&bd->class_dev,
262 &bl_class_device_attributes[i]);
263
264 down(&bd->sem);
Richard Purdie6ca01762006-03-31 02:31:49 -0800265 if (likely(bd->props && bd->props->update_status)) {
266 bd->props->brightness = 0;
267 bd->props->power = 0;
268 bd->props->update_status(bd);
269 }
270
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 bd->props = NULL;
272 up(&bd->sem);
273
274 fb_unregister_client(&bd->fb_notif);
275
276 class_device_unregister(&bd->class_dev);
277}
278EXPORT_SYMBOL(backlight_device_unregister);
279
280static void __exit backlight_class_exit(void)
281{
282 class_unregister(&backlight_class);
283}
284
285static int __init backlight_class_init(void)
286{
287 return class_register(&backlight_class);
288}
289
290/*
291 * if this is compiled into the kernel, we need to ensure that the
292 * class is registered before users of the class try to register lcd's
293 */
294postcore_initcall(backlight_class_init);
295module_exit(backlight_class_exit);
296
297MODULE_LICENSE("GPL");
298MODULE_AUTHOR("Jamey Hicks <jamey.hicks@hp.com>, Andrew Zabolotny <zap@homelink.ru>");
299MODULE_DESCRIPTION("Backlight Lowlevel Control Abstraction");