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