Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Backlight Lowlevel Control Abstraction |
| 3 | * |
| 4 | * Copyright (C) 2003,2004 Hewlett-Packard Company |
| 5 | * |
| 6 | */ |
| 7 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | |
James Simmons | 3d5eead | 2006-12-08 02:40:47 -0800 | [diff] [blame] | 17 | |
| 18 | #if defined(CONFIG_FB) || (defined(CONFIG_FB_MODULE) && \ |
| 19 | defined(CONFIG_BACKLIGHT_CLASS_DEVICE_MODULE)) |
| 20 | /* This callback gets called when something important happens inside a |
| 21 | * framebuffer driver. We're looking if that important event is blanking, |
| 22 | * and if it is, we're switching backlight power as well ... |
| 23 | */ |
| 24 | static int fb_notifier_callback(struct notifier_block *self, |
| 25 | unsigned long event, void *data) |
| 26 | { |
| 27 | struct backlight_device *bd; |
| 28 | struct fb_event *evdata = data; |
| 29 | |
| 30 | /* If we aren't interested in this event, skip it immediately ... */ |
| 31 | if (event != FB_EVENT_BLANK) |
| 32 | return 0; |
| 33 | |
| 34 | bd = container_of(self, struct backlight_device, fb_notif); |
| 35 | down(&bd->sem); |
| 36 | if (bd->props) |
| 37 | if (!bd->props->check_fb || |
| 38 | bd->props->check_fb(evdata->info)) { |
| 39 | bd->props->fb_blank = *(int *)evdata->data; |
Richard Purdie | 28ee086 | 2007-02-08 22:25:09 +0000 | [diff] [blame^] | 40 | backlight_update_status(bd); |
James Simmons | 3d5eead | 2006-12-08 02:40:47 -0800 | [diff] [blame] | 41 | } |
| 42 | up(&bd->sem); |
| 43 | return 0; |
| 44 | } |
| 45 | |
| 46 | static int backlight_register_fb(struct backlight_device *bd) |
| 47 | { |
| 48 | memset(&bd->fb_notif, 0, sizeof(bd->fb_notif)); |
| 49 | bd->fb_notif.notifier_call = fb_notifier_callback; |
| 50 | |
| 51 | return fb_register_client(&bd->fb_notif); |
| 52 | } |
| 53 | |
| 54 | static void backlight_unregister_fb(struct backlight_device *bd) |
| 55 | { |
| 56 | fb_unregister_client(&bd->fb_notif); |
| 57 | } |
| 58 | #else |
| 59 | static inline int backlight_register_fb(struct backlight_device *bd) |
| 60 | { |
| 61 | return 0; |
| 62 | } |
| 63 | |
| 64 | static inline void backlight_unregister_fb(struct backlight_device *bd) |
| 65 | { |
| 66 | } |
| 67 | #endif /* CONFIG_FB */ |
| 68 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | static ssize_t backlight_show_power(struct class_device *cdev, char *buf) |
| 70 | { |
Richard Purdie | 6ca0176 | 2006-03-31 02:31:49 -0800 | [diff] [blame] | 71 | int rc = -ENXIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | struct backlight_device *bd = to_backlight_device(cdev); |
| 73 | |
| 74 | down(&bd->sem); |
Dmitry Torokhov | 90968e8 | 2007-02-08 00:12:28 +0000 | [diff] [blame] | 75 | if (bd->props) |
Richard Purdie | 6ca0176 | 2006-03-31 02:31:49 -0800 | [diff] [blame] | 76 | rc = sprintf(buf, "%d\n", bd->props->power); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | up(&bd->sem); |
| 78 | |
| 79 | return rc; |
| 80 | } |
| 81 | |
| 82 | static ssize_t backlight_store_power(struct class_device *cdev, const char *buf, size_t count) |
| 83 | { |
Richard Purdie | 68673af | 2006-05-15 09:44:15 -0700 | [diff] [blame] | 84 | int rc = -ENXIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | char *endp; |
| 86 | struct backlight_device *bd = to_backlight_device(cdev); |
Richard Purdie | 68673af | 2006-05-15 09:44:15 -0700 | [diff] [blame] | 87 | int power = simple_strtoul(buf, &endp, 0); |
| 88 | size_t size = endp - buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | |
Richard Purdie | 68673af | 2006-05-15 09:44:15 -0700 | [diff] [blame] | 90 | if (*endp && isspace(*endp)) |
| 91 | size++; |
| 92 | if (size != count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | return -EINVAL; |
| 94 | |
| 95 | down(&bd->sem); |
Dmitry Torokhov | 90968e8 | 2007-02-08 00:12:28 +0000 | [diff] [blame] | 96 | if (bd->props) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | pr_debug("backlight: set power to %d\n", power); |
Richard Purdie | 6ca0176 | 2006-03-31 02:31:49 -0800 | [diff] [blame] | 98 | bd->props->power = power; |
Richard Purdie | 28ee086 | 2007-02-08 22:25:09 +0000 | [diff] [blame^] | 99 | backlight_update_status(bd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | rc = count; |
Richard Purdie | 6ca0176 | 2006-03-31 02:31:49 -0800 | [diff] [blame] | 101 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | up(&bd->sem); |
| 103 | |
| 104 | return rc; |
| 105 | } |
| 106 | |
| 107 | static ssize_t backlight_show_brightness(struct class_device *cdev, char *buf) |
| 108 | { |
Richard Purdie | 6ca0176 | 2006-03-31 02:31:49 -0800 | [diff] [blame] | 109 | int rc = -ENXIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | struct backlight_device *bd = to_backlight_device(cdev); |
| 111 | |
| 112 | down(&bd->sem); |
Dmitry Torokhov | 90968e8 | 2007-02-08 00:12:28 +0000 | [diff] [blame] | 113 | if (bd->props) |
Richard Purdie | 6ca0176 | 2006-03-31 02:31:49 -0800 | [diff] [blame] | 114 | rc = sprintf(buf, "%d\n", bd->props->brightness); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | up(&bd->sem); |
| 116 | |
| 117 | return rc; |
| 118 | } |
| 119 | |
| 120 | static ssize_t backlight_store_brightness(struct class_device *cdev, const char *buf, size_t count) |
| 121 | { |
Richard Purdie | 68673af | 2006-05-15 09:44:15 -0700 | [diff] [blame] | 122 | int rc = -ENXIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | char *endp; |
| 124 | struct backlight_device *bd = to_backlight_device(cdev); |
Richard Purdie | 68673af | 2006-05-15 09:44:15 -0700 | [diff] [blame] | 125 | int brightness = simple_strtoul(buf, &endp, 0); |
| 126 | size_t size = endp - buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | |
Richard Purdie | 68673af | 2006-05-15 09:44:15 -0700 | [diff] [blame] | 128 | if (*endp && isspace(*endp)) |
| 129 | size++; |
| 130 | if (size != count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | return -EINVAL; |
| 132 | |
| 133 | down(&bd->sem); |
Dmitry Torokhov | 90968e8 | 2007-02-08 00:12:28 +0000 | [diff] [blame] | 134 | if (bd->props) { |
Richard Purdie | 6ca0176 | 2006-03-31 02:31:49 -0800 | [diff] [blame] | 135 | if (brightness > bd->props->max_brightness) |
| 136 | rc = -EINVAL; |
| 137 | else { |
| 138 | pr_debug("backlight: set brightness to %d\n", |
| 139 | brightness); |
| 140 | bd->props->brightness = brightness; |
Richard Purdie | 28ee086 | 2007-02-08 22:25:09 +0000 | [diff] [blame^] | 141 | backlight_update_status(bd); |
Richard Purdie | 6ca0176 | 2006-03-31 02:31:49 -0800 | [diff] [blame] | 142 | rc = count; |
| 143 | } |
| 144 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | up(&bd->sem); |
| 146 | |
| 147 | return rc; |
| 148 | } |
| 149 | |
| 150 | static ssize_t backlight_show_max_brightness(struct class_device *cdev, char *buf) |
| 151 | { |
Richard Purdie | 6ca0176 | 2006-03-31 02:31:49 -0800 | [diff] [blame] | 152 | int rc = -ENXIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | struct backlight_device *bd = to_backlight_device(cdev); |
| 154 | |
| 155 | down(&bd->sem); |
Dmitry Torokhov | 90968e8 | 2007-02-08 00:12:28 +0000 | [diff] [blame] | 156 | if (bd->props) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | rc = sprintf(buf, "%d\n", bd->props->max_brightness); |
Richard Purdie | 6ca0176 | 2006-03-31 02:31:49 -0800 | [diff] [blame] | 158 | up(&bd->sem); |
| 159 | |
| 160 | return rc; |
| 161 | } |
| 162 | |
| 163 | static ssize_t backlight_show_actual_brightness(struct class_device *cdev, |
| 164 | char *buf) |
| 165 | { |
| 166 | int rc = -ENXIO; |
| 167 | struct backlight_device *bd = to_backlight_device(cdev); |
| 168 | |
| 169 | down(&bd->sem); |
Dmitry Torokhov | 90968e8 | 2007-02-08 00:12:28 +0000 | [diff] [blame] | 170 | if (bd->props && bd->props->get_brightness) |
Richard Purdie | 6ca0176 | 2006-03-31 02:31:49 -0800 | [diff] [blame] | 171 | rc = sprintf(buf, "%d\n", bd->props->get_brightness(bd)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | up(&bd->sem); |
| 173 | |
| 174 | return rc; |
| 175 | } |
| 176 | |
| 177 | static void backlight_class_release(struct class_device *dev) |
| 178 | { |
| 179 | struct backlight_device *bd = to_backlight_device(dev); |
| 180 | kfree(bd); |
| 181 | } |
| 182 | |
| 183 | static struct class backlight_class = { |
| 184 | .name = "backlight", |
| 185 | .release = backlight_class_release, |
| 186 | }; |
| 187 | |
| 188 | #define DECLARE_ATTR(_name,_mode,_show,_store) \ |
| 189 | { \ |
| 190 | .attr = { .name = __stringify(_name), .mode = _mode, .owner = THIS_MODULE }, \ |
| 191 | .show = _show, \ |
| 192 | .store = _store, \ |
| 193 | } |
| 194 | |
Helge Deller | d95159c | 2006-12-08 02:40:28 -0800 | [diff] [blame] | 195 | static const struct class_device_attribute bl_class_device_attributes[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | DECLARE_ATTR(power, 0644, backlight_show_power, backlight_store_power), |
Richard Purdie | 6ca0176 | 2006-03-31 02:31:49 -0800 | [diff] [blame] | 197 | DECLARE_ATTR(brightness, 0644, backlight_show_brightness, |
| 198 | backlight_store_brightness), |
| 199 | DECLARE_ATTR(actual_brightness, 0444, backlight_show_actual_brightness, |
| 200 | NULL), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | DECLARE_ATTR(max_brightness, 0444, backlight_show_max_brightness, NULL), |
| 202 | }; |
| 203 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | /** |
| 205 | * backlight_device_register - create and register a new object of |
| 206 | * backlight_device class. |
| 207 | * @name: the name of the new object(must be the same as the name of the |
| 208 | * respective framebuffer device). |
| 209 | * @devdata: an optional pointer to be stored in the class_device. The |
| 210 | * methods may retrieve it by using class_get_devdata(&bd->class_dev). |
| 211 | * @bp: the backlight properties structure. |
| 212 | * |
| 213 | * Creates and registers new backlight class_device. Returns either an |
| 214 | * ERR_PTR() or a pointer to the newly allocated device. |
| 215 | */ |
Yu Luming | 519ab5f | 2006-12-19 12:56:15 -0800 | [diff] [blame] | 216 | struct backlight_device *backlight_device_register(const char *name, |
| 217 | struct device *dev, |
| 218 | void *devdata, |
| 219 | struct backlight_properties *bp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | { |
| 221 | int i, rc; |
| 222 | struct backlight_device *new_bd; |
| 223 | |
| 224 | pr_debug("backlight_device_alloc: name=%s\n", name); |
| 225 | |
| 226 | new_bd = kmalloc(sizeof(struct backlight_device), GFP_KERNEL); |
Dmitry Torokhov | 90968e8 | 2007-02-08 00:12:28 +0000 | [diff] [blame] | 227 | if (!new_bd) |
Jean Delvare | 10ad1b7 | 2006-03-09 17:33:36 -0800 | [diff] [blame] | 228 | return ERR_PTR(-ENOMEM); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | |
Richard Purdie | 28ee086 | 2007-02-08 22:25:09 +0000 | [diff] [blame^] | 230 | mutex_init(&new_bd->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | init_MUTEX(&new_bd->sem); |
| 232 | new_bd->props = bp; |
| 233 | memset(&new_bd->class_dev, 0, sizeof(new_bd->class_dev)); |
| 234 | new_bd->class_dev.class = &backlight_class; |
Yu Luming | 519ab5f | 2006-12-19 12:56:15 -0800 | [diff] [blame] | 235 | new_bd->class_dev.dev = dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | strlcpy(new_bd->class_dev.class_id, name, KOBJ_NAME_LEN); |
| 237 | class_set_devdata(&new_bd->class_dev, devdata); |
| 238 | |
| 239 | rc = class_device_register(&new_bd->class_dev); |
Dmitry Torokhov | 90968e8 | 2007-02-08 00:12:28 +0000 | [diff] [blame] | 240 | if (rc) { |
Dmitry Torokhov | 2fd5a15 | 2007-02-07 22:25:50 +0000 | [diff] [blame] | 241 | kfree(new_bd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | return ERR_PTR(rc); |
| 243 | } |
| 244 | |
James Simmons | 3d5eead | 2006-12-08 02:40:47 -0800 | [diff] [blame] | 245 | rc = backlight_register_fb(new_bd); |
Dmitry Torokhov | 2fd5a15 | 2007-02-07 22:25:50 +0000 | [diff] [blame] | 246 | if (rc) { |
| 247 | class_device_unregister(&new_bd->class_dev); |
| 248 | return ERR_PTR(rc); |
| 249 | } |
| 250 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | |
| 252 | for (i = 0; i < ARRAY_SIZE(bl_class_device_attributes); i++) { |
| 253 | rc = class_device_create_file(&new_bd->class_dev, |
| 254 | &bl_class_device_attributes[i]); |
Dmitry Torokhov | 90968e8 | 2007-02-08 00:12:28 +0000 | [diff] [blame] | 255 | if (rc) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | while (--i >= 0) |
| 257 | class_device_remove_file(&new_bd->class_dev, |
| 258 | &bl_class_device_attributes[i]); |
| 259 | class_device_unregister(&new_bd->class_dev); |
| 260 | /* No need to kfree(new_bd) since release() method was called */ |
| 261 | return ERR_PTR(rc); |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | return new_bd; |
| 266 | } |
| 267 | EXPORT_SYMBOL(backlight_device_register); |
| 268 | |
| 269 | /** |
| 270 | * backlight_device_unregister - unregisters a backlight device object. |
| 271 | * @bd: the backlight device object to be unregistered and freed. |
| 272 | * |
| 273 | * Unregisters a previously registered via backlight_device_register object. |
| 274 | */ |
| 275 | void backlight_device_unregister(struct backlight_device *bd) |
| 276 | { |
| 277 | int i; |
| 278 | |
| 279 | if (!bd) |
| 280 | return; |
| 281 | |
| 282 | pr_debug("backlight_device_unregister: name=%s\n", bd->class_dev.class_id); |
| 283 | |
| 284 | for (i = 0; i < ARRAY_SIZE(bl_class_device_attributes); i++) |
| 285 | class_device_remove_file(&bd->class_dev, |
| 286 | &bl_class_device_attributes[i]); |
| 287 | |
| 288 | down(&bd->sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | bd->props = NULL; |
| 290 | up(&bd->sem); |
| 291 | |
James Simmons | 3d5eead | 2006-12-08 02:40:47 -0800 | [diff] [blame] | 292 | backlight_unregister_fb(bd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | |
| 294 | class_device_unregister(&bd->class_dev); |
| 295 | } |
| 296 | EXPORT_SYMBOL(backlight_device_unregister); |
| 297 | |
| 298 | static void __exit backlight_class_exit(void) |
| 299 | { |
| 300 | class_unregister(&backlight_class); |
| 301 | } |
| 302 | |
| 303 | static int __init backlight_class_init(void) |
| 304 | { |
| 305 | return class_register(&backlight_class); |
| 306 | } |
| 307 | |
| 308 | /* |
| 309 | * if this is compiled into the kernel, we need to ensure that the |
| 310 | * class is registered before users of the class try to register lcd's |
| 311 | */ |
| 312 | postcore_initcall(backlight_class_init); |
| 313 | module_exit(backlight_class_exit); |
| 314 | |
| 315 | MODULE_LICENSE("GPL"); |
| 316 | MODULE_AUTHOR("Jamey Hicks <jamey.hicks@hp.com>, Andrew Zabolotny <zap@homelink.ru>"); |
| 317 | MODULE_DESCRIPTION("Backlight Lowlevel Control Abstraction"); |