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); | |
599a52d1 RP |
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; | |
c835ee7f RP |
43 | if (bd->props.fb_blank == FB_BLANK_UNBLANK) |
44 | bd->props.state &= ~BL_CORE_FBBLANK; | |
45 | else | |
46 | bd->props.state |= BL_CORE_FBBLANK; | |
28ee086d | 47 | backlight_update_status(bd); |
3d5eeadd | 48 | } |
599a52d1 | 49 | mutex_unlock(&bd->ops_lock); |
3d5eeadd JS |
50 | return 0; |
51 | } | |
52 | ||
53 | static int backlight_register_fb(struct backlight_device *bd) | |
54 | { | |
55 | memset(&bd->fb_notif, 0, sizeof(bd->fb_notif)); | |
56 | bd->fb_notif.notifier_call = fb_notifier_callback; | |
57 | ||
58 | return fb_register_client(&bd->fb_notif); | |
59 | } | |
60 | ||
61 | static void backlight_unregister_fb(struct backlight_device *bd) | |
62 | { | |
63 | fb_unregister_client(&bd->fb_notif); | |
64 | } | |
65 | #else | |
66 | static inline int backlight_register_fb(struct backlight_device *bd) | |
67 | { | |
68 | return 0; | |
69 | } | |
70 | ||
71 | static inline void backlight_unregister_fb(struct backlight_device *bd) | |
72 | { | |
73 | } | |
74 | #endif /* CONFIG_FB */ | |
75 | ||
325253a6 MG |
76 | static void backlight_generate_event(struct backlight_device *bd, |
77 | enum backlight_update_reason reason) | |
78 | { | |
79 | char *envp[2]; | |
80 | ||
81 | switch (reason) { | |
82 | case BACKLIGHT_UPDATE_SYSFS: | |
83 | envp[0] = "SOURCE=sysfs"; | |
84 | break; | |
85 | case BACKLIGHT_UPDATE_HOTKEY: | |
86 | envp[0] = "SOURCE=hotkey"; | |
87 | break; | |
88 | default: | |
89 | envp[0] = "SOURCE=unknown"; | |
90 | break; | |
91 | } | |
92 | envp[1] = NULL; | |
93 | kobject_uevent_env(&bd->dev.kobj, KOBJ_CHANGE, envp); | |
94 | } | |
95 | ||
655bfd7a RP |
96 | static ssize_t backlight_show_power(struct device *dev, |
97 | struct device_attribute *attr,char *buf) | |
1da177e4 | 98 | { |
655bfd7a | 99 | struct backlight_device *bd = to_backlight_device(dev); |
1da177e4 | 100 | |
599a52d1 | 101 | return sprintf(buf, "%d\n", bd->props.power); |
1da177e4 LT |
102 | } |
103 | ||
655bfd7a RP |
104 | static ssize_t backlight_store_power(struct device *dev, |
105 | struct device_attribute *attr, const char *buf, size_t count) | |
1da177e4 | 106 | { |
9a2c61a9 | 107 | int rc; |
655bfd7a | 108 | struct backlight_device *bd = to_backlight_device(dev); |
9a2c61a9 | 109 | unsigned long power; |
1da177e4 | 110 | |
9a2c61a9 PM |
111 | rc = strict_strtoul(buf, 0, &power); |
112 | if (rc) | |
113 | return rc; | |
1da177e4 | 114 | |
9a2c61a9 | 115 | rc = -ENXIO; |
599a52d1 RP |
116 | mutex_lock(&bd->ops_lock); |
117 | if (bd->ops) { | |
9a2c61a9 | 118 | pr_debug("backlight: set power to %lu\n", power); |
51552453 HD |
119 | if (bd->props.power != power) { |
120 | bd->props.power = power; | |
121 | backlight_update_status(bd); | |
122 | } | |
1da177e4 | 123 | rc = count; |
6ca01765 | 124 | } |
599a52d1 | 125 | mutex_unlock(&bd->ops_lock); |
1da177e4 LT |
126 | |
127 | return rc; | |
128 | } | |
129 | ||
655bfd7a RP |
130 | static ssize_t backlight_show_brightness(struct device *dev, |
131 | struct device_attribute *attr, char *buf) | |
1da177e4 | 132 | { |
655bfd7a | 133 | struct backlight_device *bd = to_backlight_device(dev); |
1da177e4 | 134 | |
599a52d1 | 135 | return sprintf(buf, "%d\n", bd->props.brightness); |
1da177e4 LT |
136 | } |
137 | ||
655bfd7a RP |
138 | static ssize_t backlight_store_brightness(struct device *dev, |
139 | struct device_attribute *attr, const char *buf, size_t count) | |
1da177e4 | 140 | { |
9a2c61a9 | 141 | int rc; |
655bfd7a | 142 | struct backlight_device *bd = to_backlight_device(dev); |
9a2c61a9 | 143 | unsigned long brightness; |
1da177e4 | 144 | |
9a2c61a9 PM |
145 | rc = strict_strtoul(buf, 0, &brightness); |
146 | if (rc) | |
147 | return rc; | |
148 | ||
149 | rc = -ENXIO; | |
1da177e4 | 150 | |
599a52d1 RP |
151 | mutex_lock(&bd->ops_lock); |
152 | if (bd->ops) { | |
153 | if (brightness > bd->props.max_brightness) | |
6ca01765 RP |
154 | rc = -EINVAL; |
155 | else { | |
9a2c61a9 | 156 | pr_debug("backlight: set brightness to %lu\n", |
6ca01765 | 157 | brightness); |
9be1df98 ZR |
158 | bd->props.brightness = brightness; |
159 | backlight_update_status(bd); | |
6ca01765 RP |
160 | rc = count; |
161 | } | |
162 | } | |
599a52d1 | 163 | mutex_unlock(&bd->ops_lock); |
1da177e4 | 164 | |
325253a6 MG |
165 | backlight_generate_event(bd, BACKLIGHT_UPDATE_SYSFS); |
166 | ||
1da177e4 LT |
167 | return rc; |
168 | } | |
169 | ||
655bfd7a RP |
170 | static ssize_t backlight_show_max_brightness(struct device *dev, |
171 | struct device_attribute *attr, char *buf) | |
1da177e4 | 172 | { |
655bfd7a | 173 | struct backlight_device *bd = to_backlight_device(dev); |
1da177e4 | 174 | |
599a52d1 | 175 | return sprintf(buf, "%d\n", bd->props.max_brightness); |
6ca01765 RP |
176 | } |
177 | ||
655bfd7a RP |
178 | static ssize_t backlight_show_actual_brightness(struct device *dev, |
179 | struct device_attribute *attr, char *buf) | |
6ca01765 RP |
180 | { |
181 | int rc = -ENXIO; | |
655bfd7a | 182 | struct backlight_device *bd = to_backlight_device(dev); |
6ca01765 | 183 | |
599a52d1 RP |
184 | mutex_lock(&bd->ops_lock); |
185 | if (bd->ops && bd->ops->get_brightness) | |
186 | rc = sprintf(buf, "%d\n", bd->ops->get_brightness(bd)); | |
187 | mutex_unlock(&bd->ops_lock); | |
1da177e4 LT |
188 | |
189 | return rc; | |
190 | } | |
191 | ||
0ad90efd | 192 | static struct class *backlight_class; |
655bfd7a | 193 | |
c835ee7f RP |
194 | static int backlight_suspend(struct device *dev, pm_message_t state) |
195 | { | |
196 | struct backlight_device *bd = to_backlight_device(dev); | |
197 | ||
198 | if (bd->ops->options & BL_CORE_SUSPENDRESUME) { | |
199 | mutex_lock(&bd->ops_lock); | |
200 | bd->props.state |= BL_CORE_SUSPENDED; | |
201 | backlight_update_status(bd); | |
202 | mutex_unlock(&bd->ops_lock); | |
203 | } | |
204 | ||
205 | return 0; | |
206 | } | |
207 | ||
208 | static int backlight_resume(struct device *dev) | |
209 | { | |
210 | struct backlight_device *bd = to_backlight_device(dev); | |
211 | ||
212 | if (bd->ops->options & BL_CORE_SUSPENDRESUME) { | |
213 | mutex_lock(&bd->ops_lock); | |
214 | bd->props.state &= ~BL_CORE_SUSPENDED; | |
215 | backlight_update_status(bd); | |
216 | mutex_unlock(&bd->ops_lock); | |
217 | } | |
218 | ||
219 | return 0; | |
220 | } | |
221 | ||
655bfd7a | 222 | static void bl_device_release(struct device *dev) |
1da177e4 LT |
223 | { |
224 | struct backlight_device *bd = to_backlight_device(dev); | |
225 | kfree(bd); | |
226 | } | |
227 | ||
655bfd7a RP |
228 | static struct device_attribute bl_device_attributes[] = { |
229 | __ATTR(bl_power, 0644, backlight_show_power, backlight_store_power), | |
230 | __ATTR(brightness, 0644, backlight_show_brightness, | |
6ca01765 | 231 | backlight_store_brightness), |
655bfd7a | 232 | __ATTR(actual_brightness, 0444, backlight_show_actual_brightness, |
6ca01765 | 233 | NULL), |
655bfd7a RP |
234 | __ATTR(max_brightness, 0444, backlight_show_max_brightness, NULL), |
235 | __ATTR_NULL, | |
1da177e4 LT |
236 | }; |
237 | ||
325253a6 MG |
238 | /** |
239 | * backlight_force_update - tell the backlight subsystem that hardware state | |
240 | * has changed | |
241 | * @bd: the backlight device to update | |
242 | * | |
243 | * Updates the internal state of the backlight in response to a hardware event, | |
244 | * and generate a uevent to notify userspace | |
245 | */ | |
246 | void backlight_force_update(struct backlight_device *bd, | |
247 | enum backlight_update_reason reason) | |
248 | { | |
249 | mutex_lock(&bd->ops_lock); | |
250 | if (bd->ops && bd->ops->get_brightness) | |
251 | bd->props.brightness = bd->ops->get_brightness(bd); | |
252 | mutex_unlock(&bd->ops_lock); | |
253 | backlight_generate_event(bd, reason); | |
254 | } | |
255 | EXPORT_SYMBOL(backlight_force_update); | |
256 | ||
1da177e4 LT |
257 | /** |
258 | * backlight_device_register - create and register a new object of | |
259 | * backlight_device class. | |
260 | * @name: the name of the new object(must be the same as the name of the | |
261 | * respective framebuffer device). | |
f6ec2d96 | 262 | * @parent: a pointer to the parent device |
655bfd7a RP |
263 | * @devdata: an optional pointer to be stored for private driver use. The |
264 | * methods may retrieve it by using bl_get_data(bd). | |
599a52d1 | 265 | * @ops: the backlight operations structure. |
1da177e4 | 266 | * |
655bfd7a | 267 | * Creates and registers new backlight device. Returns either an |
1da177e4 LT |
268 | * ERR_PTR() or a pointer to the newly allocated device. |
269 | */ | |
519ab5f2 | 270 | struct backlight_device *backlight_device_register(const char *name, |
655bfd7a | 271 | struct device *parent, void *devdata, struct backlight_ops *ops) |
1da177e4 | 272 | { |
1da177e4 | 273 | struct backlight_device *new_bd; |
655bfd7a | 274 | int rc; |
1da177e4 | 275 | |
655bfd7a | 276 | pr_debug("backlight_device_register: name=%s\n", name); |
1da177e4 | 277 | |
599a52d1 | 278 | new_bd = kzalloc(sizeof(struct backlight_device), GFP_KERNEL); |
90968e8e | 279 | if (!new_bd) |
10ad1b73 | 280 | return ERR_PTR(-ENOMEM); |
1da177e4 | 281 | |
28ee086d | 282 | mutex_init(&new_bd->update_lock); |
599a52d1 | 283 | mutex_init(&new_bd->ops_lock); |
1da177e4 | 284 | |
655bfd7a RP |
285 | new_bd->dev.class = backlight_class; |
286 | new_bd->dev.parent = parent; | |
287 | new_bd->dev.release = bl_device_release; | |
64dba9a9 | 288 | dev_set_name(&new_bd->dev, name); |
655bfd7a RP |
289 | dev_set_drvdata(&new_bd->dev, devdata); |
290 | ||
291 | rc = device_register(&new_bd->dev); | |
90968e8e | 292 | if (rc) { |
2fd5a154 | 293 | kfree(new_bd); |
1da177e4 LT |
294 | return ERR_PTR(rc); |
295 | } | |
296 | ||
3d5eeadd | 297 | rc = backlight_register_fb(new_bd); |
2fd5a154 | 298 | if (rc) { |
655bfd7a | 299 | device_unregister(&new_bd->dev); |
2fd5a154 DT |
300 | return ERR_PTR(rc); |
301 | } | |
302 | ||
655bfd7a | 303 | new_bd->ops = ops; |
1da177e4 | 304 | |
321709c5 RP |
305 | #ifdef CONFIG_PMAC_BACKLIGHT |
306 | mutex_lock(&pmac_backlight_mutex); | |
307 | if (!pmac_backlight) | |
308 | pmac_backlight = new_bd; | |
309 | mutex_unlock(&pmac_backlight_mutex); | |
310 | #endif | |
311 | ||
1da177e4 LT |
312 | return new_bd; |
313 | } | |
314 | EXPORT_SYMBOL(backlight_device_register); | |
315 | ||
316 | /** | |
317 | * backlight_device_unregister - unregisters a backlight device object. | |
318 | * @bd: the backlight device object to be unregistered and freed. | |
319 | * | |
320 | * Unregisters a previously registered via backlight_device_register object. | |
321 | */ | |
322 | void backlight_device_unregister(struct backlight_device *bd) | |
323 | { | |
1da177e4 LT |
324 | if (!bd) |
325 | return; | |
326 | ||
321709c5 RP |
327 | #ifdef CONFIG_PMAC_BACKLIGHT |
328 | mutex_lock(&pmac_backlight_mutex); | |
329 | if (pmac_backlight == bd) | |
330 | pmac_backlight = NULL; | |
331 | mutex_unlock(&pmac_backlight_mutex); | |
332 | #endif | |
599a52d1 RP |
333 | mutex_lock(&bd->ops_lock); |
334 | bd->ops = NULL; | |
335 | mutex_unlock(&bd->ops_lock); | |
1da177e4 | 336 | |
3d5eeadd | 337 | backlight_unregister_fb(bd); |
655bfd7a | 338 | device_unregister(&bd->dev); |
1da177e4 LT |
339 | } |
340 | EXPORT_SYMBOL(backlight_device_unregister); | |
341 | ||
342 | static void __exit backlight_class_exit(void) | |
343 | { | |
655bfd7a | 344 | class_destroy(backlight_class); |
1da177e4 LT |
345 | } |
346 | ||
347 | static int __init backlight_class_init(void) | |
348 | { | |
655bfd7a RP |
349 | backlight_class = class_create(THIS_MODULE, "backlight"); |
350 | if (IS_ERR(backlight_class)) { | |
351 | printk(KERN_WARNING "Unable to create backlight class; errno = %ld\n", | |
352 | PTR_ERR(backlight_class)); | |
353 | return PTR_ERR(backlight_class); | |
354 | } | |
355 | ||
356 | backlight_class->dev_attrs = bl_device_attributes; | |
c835ee7f RP |
357 | backlight_class->suspend = backlight_suspend; |
358 | backlight_class->resume = backlight_resume; | |
655bfd7a | 359 | return 0; |
1da177e4 LT |
360 | } |
361 | ||
362 | /* | |
363 | * if this is compiled into the kernel, we need to ensure that the | |
364 | * class is registered before users of the class try to register lcd's | |
365 | */ | |
366 | postcore_initcall(backlight_class_init); | |
367 | module_exit(backlight_class_exit); | |
368 | ||
369 | MODULE_LICENSE("GPL"); | |
370 | MODULE_AUTHOR("Jamey Hicks <jamey.hicks@hp.com>, Andrew Zabolotny <zap@homelink.ru>"); | |
371 | MODULE_DESCRIPTION("Backlight Lowlevel Control Abstraction"); |