Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * Backlight Lowlevel Control Abstraction | |
3 | * | |
4 | * Copyright (C) 2003,2004 Hewlett-Packard Company | |
5 | * | |
6 | */ | |
7 | ||
1da177e4 LT |
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> | |
1da177e4 | 16 | |
321709c5 RP |
17 | #ifdef CONFIG_PMAC_BACKLIGHT |
18 | #include <asm/backlight.h> | |
19 | #endif | |
3d5eeadd JS |
20 | |
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 | */ | |
27 | static 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 ... */ | |
994efacd | 34 | if (event != FB_EVENT_BLANK && event != FB_EVENT_CONBLANK) |
3d5eeadd JS |
35 | return 0; |
36 | ||
37 | bd = container_of(self, struct backlight_device, fb_notif); | |
249040dc | 38 | mutex_lock(&bd->props_lock); |
3d5eeadd JS |
39 | if (bd->props) |
40 | if (!bd->props->check_fb || | |
41 | bd->props->check_fb(evdata->info)) { | |
42 | bd->props->fb_blank = *(int *)evdata->data; | |
28ee086d | 43 | backlight_update_status(bd); |
3d5eeadd | 44 | } |
249040dc | 45 | mutex_unlock(&bd->props_lock); |
3d5eeadd JS |
46 | return 0; |
47 | } | |
48 | ||
49 | static 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 | ||
57 | static void backlight_unregister_fb(struct backlight_device *bd) | |
58 | { | |
59 | fb_unregister_client(&bd->fb_notif); | |
60 | } | |
61 | #else | |
62 | static inline int backlight_register_fb(struct backlight_device *bd) | |
63 | { | |
64 | return 0; | |
65 | } | |
66 | ||
67 | static inline void backlight_unregister_fb(struct backlight_device *bd) | |
68 | { | |
69 | } | |
70 | #endif /* CONFIG_FB */ | |
71 | ||
1da177e4 LT |
72 | static ssize_t backlight_show_power(struct class_device *cdev, char *buf) |
73 | { | |
6ca01765 | 74 | int rc = -ENXIO; |
1da177e4 LT |
75 | struct backlight_device *bd = to_backlight_device(cdev); |
76 | ||
249040dc | 77 | mutex_lock(&bd->props_lock); |
90968e8e | 78 | if (bd->props) |
6ca01765 | 79 | rc = sprintf(buf, "%d\n", bd->props->power); |
249040dc | 80 | mutex_unlock(&bd->props_lock); |
1da177e4 LT |
81 | |
82 | return rc; | |
83 | } | |
84 | ||
85 | static ssize_t backlight_store_power(struct class_device *cdev, const char *buf, size_t count) | |
86 | { | |
68673afd | 87 | int rc = -ENXIO; |
1da177e4 LT |
88 | char *endp; |
89 | struct backlight_device *bd = to_backlight_device(cdev); | |
68673afd RP |
90 | int power = simple_strtoul(buf, &endp, 0); |
91 | size_t size = endp - buf; | |
1da177e4 | 92 | |
68673afd RP |
93 | if (*endp && isspace(*endp)) |
94 | size++; | |
95 | if (size != count) | |
1da177e4 LT |
96 | return -EINVAL; |
97 | ||
249040dc | 98 | mutex_lock(&bd->props_lock); |
90968e8e | 99 | if (bd->props) { |
1da177e4 | 100 | pr_debug("backlight: set power to %d\n", power); |
6ca01765 | 101 | bd->props->power = power; |
28ee086d | 102 | backlight_update_status(bd); |
1da177e4 | 103 | rc = count; |
6ca01765 | 104 | } |
249040dc | 105 | mutex_unlock(&bd->props_lock); |
1da177e4 LT |
106 | |
107 | return rc; | |
108 | } | |
109 | ||
110 | static ssize_t backlight_show_brightness(struct class_device *cdev, char *buf) | |
111 | { | |
6ca01765 | 112 | int rc = -ENXIO; |
1da177e4 LT |
113 | struct backlight_device *bd = to_backlight_device(cdev); |
114 | ||
249040dc | 115 | mutex_lock(&bd->props_lock); |
90968e8e | 116 | if (bd->props) |
6ca01765 | 117 | rc = sprintf(buf, "%d\n", bd->props->brightness); |
249040dc | 118 | mutex_unlock(&bd->props_lock); |
1da177e4 LT |
119 | |
120 | return rc; | |
121 | } | |
122 | ||
123 | static ssize_t backlight_store_brightness(struct class_device *cdev, const char *buf, size_t count) | |
124 | { | |
68673afd | 125 | int rc = -ENXIO; |
1da177e4 LT |
126 | char *endp; |
127 | struct backlight_device *bd = to_backlight_device(cdev); | |
68673afd RP |
128 | int brightness = simple_strtoul(buf, &endp, 0); |
129 | size_t size = endp - buf; | |
1da177e4 | 130 | |
68673afd RP |
131 | if (*endp && isspace(*endp)) |
132 | size++; | |
133 | if (size != count) | |
1da177e4 LT |
134 | return -EINVAL; |
135 | ||
249040dc | 136 | mutex_lock(&bd->props_lock); |
90968e8e | 137 | if (bd->props) { |
6ca01765 RP |
138 | if (brightness > bd->props->max_brightness) |
139 | rc = -EINVAL; | |
140 | else { | |
141 | pr_debug("backlight: set brightness to %d\n", | |
142 | brightness); | |
143 | bd->props->brightness = brightness; | |
28ee086d | 144 | backlight_update_status(bd); |
6ca01765 RP |
145 | rc = count; |
146 | } | |
147 | } | |
249040dc | 148 | mutex_unlock(&bd->props_lock); |
1da177e4 LT |
149 | |
150 | return rc; | |
151 | } | |
152 | ||
153 | static ssize_t backlight_show_max_brightness(struct class_device *cdev, char *buf) | |
154 | { | |
6ca01765 | 155 | int rc = -ENXIO; |
1da177e4 LT |
156 | struct backlight_device *bd = to_backlight_device(cdev); |
157 | ||
249040dc | 158 | mutex_lock(&bd->props_lock); |
90968e8e | 159 | if (bd->props) |
1da177e4 | 160 | rc = sprintf(buf, "%d\n", bd->props->max_brightness); |
249040dc | 161 | mutex_unlock(&bd->props_lock); |
6ca01765 RP |
162 | |
163 | return rc; | |
164 | } | |
165 | ||
166 | static ssize_t backlight_show_actual_brightness(struct class_device *cdev, | |
167 | char *buf) | |
168 | { | |
169 | int rc = -ENXIO; | |
170 | struct backlight_device *bd = to_backlight_device(cdev); | |
171 | ||
249040dc | 172 | mutex_lock(&bd->props_lock); |
90968e8e | 173 | if (bd->props && bd->props->get_brightness) |
6ca01765 | 174 | rc = sprintf(buf, "%d\n", bd->props->get_brightness(bd)); |
249040dc | 175 | mutex_unlock(&bd->props_lock); |
1da177e4 LT |
176 | |
177 | return rc; | |
178 | } | |
179 | ||
180 | static void backlight_class_release(struct class_device *dev) | |
181 | { | |
182 | struct backlight_device *bd = to_backlight_device(dev); | |
183 | kfree(bd); | |
184 | } | |
185 | ||
186 | static struct class backlight_class = { | |
187 | .name = "backlight", | |
188 | .release = backlight_class_release, | |
189 | }; | |
190 | ||
191 | #define DECLARE_ATTR(_name,_mode,_show,_store) \ | |
192 | { \ | |
193 | .attr = { .name = __stringify(_name), .mode = _mode, .owner = THIS_MODULE }, \ | |
194 | .show = _show, \ | |
195 | .store = _store, \ | |
196 | } | |
197 | ||
d95159cf | 198 | static const struct class_device_attribute bl_class_device_attributes[] = { |
1da177e4 | 199 | DECLARE_ATTR(power, 0644, backlight_show_power, backlight_store_power), |
6ca01765 RP |
200 | DECLARE_ATTR(brightness, 0644, backlight_show_brightness, |
201 | backlight_store_brightness), | |
202 | DECLARE_ATTR(actual_brightness, 0444, backlight_show_actual_brightness, | |
203 | NULL), | |
1da177e4 LT |
204 | DECLARE_ATTR(max_brightness, 0444, backlight_show_max_brightness, NULL), |
205 | }; | |
206 | ||
1da177e4 LT |
207 | /** |
208 | * backlight_device_register - create and register a new object of | |
209 | * backlight_device class. | |
210 | * @name: the name of the new object(must be the same as the name of the | |
211 | * respective framebuffer device). | |
212 | * @devdata: an optional pointer to be stored in the class_device. The | |
213 | * methods may retrieve it by using class_get_devdata(&bd->class_dev). | |
214 | * @bp: the backlight properties structure. | |
215 | * | |
216 | * Creates and registers new backlight class_device. Returns either an | |
217 | * ERR_PTR() or a pointer to the newly allocated device. | |
218 | */ | |
519ab5f2 YL |
219 | struct backlight_device *backlight_device_register(const char *name, |
220 | struct device *dev, | |
221 | void *devdata, | |
222 | struct backlight_properties *bp) | |
1da177e4 LT |
223 | { |
224 | int i, rc; | |
225 | struct backlight_device *new_bd; | |
226 | ||
227 | pr_debug("backlight_device_alloc: name=%s\n", name); | |
228 | ||
229 | new_bd = kmalloc(sizeof(struct backlight_device), GFP_KERNEL); | |
90968e8e | 230 | if (!new_bd) |
10ad1b73 | 231 | return ERR_PTR(-ENOMEM); |
1da177e4 | 232 | |
28ee086d | 233 | mutex_init(&new_bd->update_lock); |
249040dc | 234 | mutex_init(&new_bd->props_lock); |
1da177e4 LT |
235 | new_bd->props = bp; |
236 | memset(&new_bd->class_dev, 0, sizeof(new_bd->class_dev)); | |
237 | new_bd->class_dev.class = &backlight_class; | |
519ab5f2 | 238 | new_bd->class_dev.dev = dev; |
1da177e4 LT |
239 | strlcpy(new_bd->class_dev.class_id, name, KOBJ_NAME_LEN); |
240 | class_set_devdata(&new_bd->class_dev, devdata); | |
241 | ||
242 | rc = class_device_register(&new_bd->class_dev); | |
90968e8e | 243 | if (rc) { |
2fd5a154 | 244 | kfree(new_bd); |
1da177e4 LT |
245 | return ERR_PTR(rc); |
246 | } | |
247 | ||
3d5eeadd | 248 | rc = backlight_register_fb(new_bd); |
2fd5a154 DT |
249 | if (rc) { |
250 | class_device_unregister(&new_bd->class_dev); | |
251 | return ERR_PTR(rc); | |
252 | } | |
253 | ||
1da177e4 LT |
254 | |
255 | for (i = 0; i < ARRAY_SIZE(bl_class_device_attributes); i++) { | |
256 | rc = class_device_create_file(&new_bd->class_dev, | |
257 | &bl_class_device_attributes[i]); | |
90968e8e | 258 | if (rc) { |
1da177e4 LT |
259 | while (--i >= 0) |
260 | class_device_remove_file(&new_bd->class_dev, | |
261 | &bl_class_device_attributes[i]); | |
262 | class_device_unregister(&new_bd->class_dev); | |
263 | /* No need to kfree(new_bd) since release() method was called */ | |
264 | return ERR_PTR(rc); | |
265 | } | |
266 | } | |
267 | ||
321709c5 RP |
268 | #ifdef CONFIG_PMAC_BACKLIGHT |
269 | mutex_lock(&pmac_backlight_mutex); | |
270 | if (!pmac_backlight) | |
271 | pmac_backlight = new_bd; | |
272 | mutex_unlock(&pmac_backlight_mutex); | |
273 | #endif | |
274 | ||
1da177e4 LT |
275 | return new_bd; |
276 | } | |
277 | EXPORT_SYMBOL(backlight_device_register); | |
278 | ||
279 | /** | |
280 | * backlight_device_unregister - unregisters a backlight device object. | |
281 | * @bd: the backlight device object to be unregistered and freed. | |
282 | * | |
283 | * Unregisters a previously registered via backlight_device_register object. | |
284 | */ | |
285 | void backlight_device_unregister(struct backlight_device *bd) | |
286 | { | |
287 | int i; | |
288 | ||
289 | if (!bd) | |
290 | return; | |
291 | ||
292 | pr_debug("backlight_device_unregister: name=%s\n", bd->class_dev.class_id); | |
293 | ||
321709c5 RP |
294 | #ifdef CONFIG_PMAC_BACKLIGHT |
295 | mutex_lock(&pmac_backlight_mutex); | |
296 | if (pmac_backlight == bd) | |
297 | pmac_backlight = NULL; | |
298 | mutex_unlock(&pmac_backlight_mutex); | |
299 | #endif | |
300 | ||
1da177e4 LT |
301 | for (i = 0; i < ARRAY_SIZE(bl_class_device_attributes); i++) |
302 | class_device_remove_file(&bd->class_dev, | |
303 | &bl_class_device_attributes[i]); | |
304 | ||
249040dc | 305 | mutex_lock(&bd->props_lock); |
1da177e4 | 306 | bd->props = NULL; |
249040dc | 307 | mutex_unlock(&bd->props_lock); |
1da177e4 | 308 | |
3d5eeadd | 309 | backlight_unregister_fb(bd); |
1da177e4 LT |
310 | |
311 | class_device_unregister(&bd->class_dev); | |
312 | } | |
313 | EXPORT_SYMBOL(backlight_device_unregister); | |
314 | ||
315 | static void __exit backlight_class_exit(void) | |
316 | { | |
317 | class_unregister(&backlight_class); | |
318 | } | |
319 | ||
320 | static int __init backlight_class_init(void) | |
321 | { | |
322 | return class_register(&backlight_class); | |
323 | } | |
324 | ||
325 | /* | |
326 | * if this is compiled into the kernel, we need to ensure that the | |
327 | * class is registered before users of the class try to register lcd's | |
328 | */ | |
329 | postcore_initcall(backlight_class_init); | |
330 | module_exit(backlight_class_exit); | |
331 | ||
332 | MODULE_LICENSE("GPL"); | |
333 | MODULE_AUTHOR("Jamey Hicks <jamey.hicks@hp.com>, Andrew Zabolotny <zap@homelink.ru>"); | |
334 | MODULE_DESCRIPTION("Backlight Lowlevel Control Abstraction"); |