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