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