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