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 | c835ee7 | 2009-01-06 21:00:19 +0000 | [diff] [blame] | 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; |
Richard Purdie | 28ee086 | 2007-02-08 22:25:09 +0000 | [diff] [blame] | 47 | backlight_update_status(bd); |
James Simmons | 3d5eead | 2006-12-08 02:40:47 -0800 | [diff] [blame] | 48 | } |
Richard Purdie | 599a52d | 2007-02-10 23:07:48 +0000 | [diff] [blame] | 49 | mutex_unlock(&bd->ops_lock); |
James Simmons | 3d5eead | 2006-12-08 02:40:47 -0800 | [diff] [blame] | 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 | |
Matthew Garrett | 325253a | 2009-07-14 17:06:02 +0100 | [diff] [blame] | 76 | static void backlight_generate_event(struct backlight_device *bd, |
| 77 | enum backlight_update_reason reason) |
| 78 | { |
| 79 | char *envp[2]; |
| 80 | |
| 81 | switch (reason) { |
| 82 | case BACKLIGHT_UPDATE_SYSFS: |
| 83 | envp[0] = "SOURCE=sysfs"; |
| 84 | break; |
| 85 | case BACKLIGHT_UPDATE_HOTKEY: |
| 86 | envp[0] = "SOURCE=hotkey"; |
| 87 | break; |
| 88 | default: |
| 89 | envp[0] = "SOURCE=unknown"; |
| 90 | break; |
| 91 | } |
| 92 | envp[1] = NULL; |
| 93 | kobject_uevent_env(&bd->dev.kobj, KOBJ_CHANGE, envp); |
Henrique de Moraes Holschuh | 89dfc28 | 2009-09-20 14:44:47 -0300 | [diff] [blame] | 94 | sysfs_notify(&bd->dev.kobj, NULL, "actual_brightness"); |
Matthew Garrett | 325253a | 2009-07-14 17:06:02 +0100 | [diff] [blame] | 95 | } |
| 96 | |
Richard Purdie | 655bfd7 | 2007-07-09 12:17:24 +0100 | [diff] [blame] | 97 | static ssize_t backlight_show_power(struct device *dev, |
| 98 | struct device_attribute *attr,char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | { |
Richard Purdie | 655bfd7 | 2007-07-09 12:17:24 +0100 | [diff] [blame] | 100 | struct backlight_device *bd = to_backlight_device(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | |
Richard Purdie | 599a52d | 2007-02-10 23:07:48 +0000 | [diff] [blame] | 102 | return sprintf(buf, "%d\n", bd->props.power); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | } |
| 104 | |
Richard Purdie | 655bfd7 | 2007-07-09 12:17:24 +0100 | [diff] [blame] | 105 | static ssize_t backlight_store_power(struct device *dev, |
| 106 | struct device_attribute *attr, const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | { |
Pavel Machek | 9a2c61a | 2008-12-03 08:43:48 +0000 | [diff] [blame] | 108 | int rc; |
Richard Purdie | 655bfd7 | 2007-07-09 12:17:24 +0100 | [diff] [blame] | 109 | struct backlight_device *bd = to_backlight_device(dev); |
Pavel Machek | 9a2c61a | 2008-12-03 08:43:48 +0000 | [diff] [blame] | 110 | unsigned long power; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | |
Pavel Machek | 9a2c61a | 2008-12-03 08:43:48 +0000 | [diff] [blame] | 112 | rc = strict_strtoul(buf, 0, &power); |
| 113 | if (rc) |
| 114 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | |
Pavel Machek | 9a2c61a | 2008-12-03 08:43:48 +0000 | [diff] [blame] | 116 | rc = -ENXIO; |
Richard Purdie | 599a52d | 2007-02-10 23:07:48 +0000 | [diff] [blame] | 117 | mutex_lock(&bd->ops_lock); |
| 118 | if (bd->ops) { |
Pavel Machek | 9a2c61a | 2008-12-03 08:43:48 +0000 | [diff] [blame] | 119 | pr_debug("backlight: set power to %lu\n", power); |
Helge Deller | 5155245 | 2008-01-13 23:01:13 +0000 | [diff] [blame] | 120 | if (bd->props.power != power) { |
| 121 | bd->props.power = power; |
| 122 | backlight_update_status(bd); |
| 123 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | rc = count; |
Richard Purdie | 6ca0176 | 2006-03-31 02:31:49 -0800 | [diff] [blame] | 125 | } |
Richard Purdie | 599a52d | 2007-02-10 23:07:48 +0000 | [diff] [blame] | 126 | mutex_unlock(&bd->ops_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | |
| 128 | return rc; |
| 129 | } |
| 130 | |
Richard Purdie | 655bfd7 | 2007-07-09 12:17:24 +0100 | [diff] [blame] | 131 | static ssize_t backlight_show_brightness(struct device *dev, |
| 132 | struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | { |
Richard Purdie | 655bfd7 | 2007-07-09 12:17:24 +0100 | [diff] [blame] | 134 | struct backlight_device *bd = to_backlight_device(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | |
Richard Purdie | 599a52d | 2007-02-10 23:07:48 +0000 | [diff] [blame] | 136 | return sprintf(buf, "%d\n", bd->props.brightness); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | } |
| 138 | |
Richard Purdie | 655bfd7 | 2007-07-09 12:17:24 +0100 | [diff] [blame] | 139 | static ssize_t backlight_store_brightness(struct device *dev, |
| 140 | struct device_attribute *attr, const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | { |
Pavel Machek | 9a2c61a | 2008-12-03 08:43:48 +0000 | [diff] [blame] | 142 | int rc; |
Richard Purdie | 655bfd7 | 2007-07-09 12:17:24 +0100 | [diff] [blame] | 143 | struct backlight_device *bd = to_backlight_device(dev); |
Pavel Machek | 9a2c61a | 2008-12-03 08:43:48 +0000 | [diff] [blame] | 144 | unsigned long brightness; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | |
Pavel Machek | 9a2c61a | 2008-12-03 08:43:48 +0000 | [diff] [blame] | 146 | rc = strict_strtoul(buf, 0, &brightness); |
| 147 | if (rc) |
| 148 | return rc; |
| 149 | |
| 150 | rc = -ENXIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | |
Richard Purdie | 599a52d | 2007-02-10 23:07:48 +0000 | [diff] [blame] | 152 | mutex_lock(&bd->ops_lock); |
| 153 | if (bd->ops) { |
| 154 | if (brightness > bd->props.max_brightness) |
Richard Purdie | 6ca0176 | 2006-03-31 02:31:49 -0800 | [diff] [blame] | 155 | rc = -EINVAL; |
| 156 | else { |
Pavel Machek | 9a2c61a | 2008-12-03 08:43:48 +0000 | [diff] [blame] | 157 | pr_debug("backlight: set brightness to %lu\n", |
Richard Purdie | 6ca0176 | 2006-03-31 02:31:49 -0800 | [diff] [blame] | 158 | brightness); |
Zhang Rui | 9be1df9 | 2009-01-08 14:11:30 +0000 | [diff] [blame] | 159 | bd->props.brightness = brightness; |
| 160 | backlight_update_status(bd); |
Richard Purdie | 6ca0176 | 2006-03-31 02:31:49 -0800 | [diff] [blame] | 161 | rc = count; |
| 162 | } |
| 163 | } |
Richard Purdie | 599a52d | 2007-02-10 23:07:48 +0000 | [diff] [blame] | 164 | mutex_unlock(&bd->ops_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | |
Matthew Garrett | 325253a | 2009-07-14 17:06:02 +0100 | [diff] [blame] | 166 | backlight_generate_event(bd, BACKLIGHT_UPDATE_SYSFS); |
| 167 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | return rc; |
| 169 | } |
| 170 | |
Richard Purdie | 655bfd7 | 2007-07-09 12:17:24 +0100 | [diff] [blame] | 171 | static ssize_t backlight_show_max_brightness(struct device *dev, |
| 172 | struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | { |
Richard Purdie | 655bfd7 | 2007-07-09 12:17:24 +0100 | [diff] [blame] | 174 | struct backlight_device *bd = to_backlight_device(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | |
Richard Purdie | 599a52d | 2007-02-10 23:07:48 +0000 | [diff] [blame] | 176 | return sprintf(buf, "%d\n", bd->props.max_brightness); |
Richard Purdie | 6ca0176 | 2006-03-31 02:31:49 -0800 | [diff] [blame] | 177 | } |
| 178 | |
Richard Purdie | 655bfd7 | 2007-07-09 12:17:24 +0100 | [diff] [blame] | 179 | static ssize_t backlight_show_actual_brightness(struct device *dev, |
| 180 | struct device_attribute *attr, char *buf) |
Richard Purdie | 6ca0176 | 2006-03-31 02:31:49 -0800 | [diff] [blame] | 181 | { |
| 182 | int rc = -ENXIO; |
Richard Purdie | 655bfd7 | 2007-07-09 12:17:24 +0100 | [diff] [blame] | 183 | struct backlight_device *bd = to_backlight_device(dev); |
Richard Purdie | 6ca0176 | 2006-03-31 02:31:49 -0800 | [diff] [blame] | 184 | |
Richard Purdie | 599a52d | 2007-02-10 23:07:48 +0000 | [diff] [blame] | 185 | mutex_lock(&bd->ops_lock); |
| 186 | if (bd->ops && bd->ops->get_brightness) |
| 187 | rc = sprintf(buf, "%d\n", bd->ops->get_brightness(bd)); |
| 188 | mutex_unlock(&bd->ops_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | |
| 190 | return rc; |
| 191 | } |
| 192 | |
Adrian Bunk | 0ad90ef | 2007-08-11 10:27:19 +0100 | [diff] [blame] | 193 | static struct class *backlight_class; |
Richard Purdie | 655bfd7 | 2007-07-09 12:17:24 +0100 | [diff] [blame] | 194 | |
Richard Purdie | c835ee7 | 2009-01-06 21:00:19 +0000 | [diff] [blame] | 195 | static int backlight_suspend(struct device *dev, pm_message_t state) |
| 196 | { |
| 197 | struct backlight_device *bd = to_backlight_device(dev); |
| 198 | |
| 199 | if (bd->ops->options & BL_CORE_SUSPENDRESUME) { |
| 200 | mutex_lock(&bd->ops_lock); |
| 201 | bd->props.state |= BL_CORE_SUSPENDED; |
| 202 | backlight_update_status(bd); |
| 203 | mutex_unlock(&bd->ops_lock); |
| 204 | } |
| 205 | |
| 206 | return 0; |
| 207 | } |
| 208 | |
| 209 | static int backlight_resume(struct device *dev) |
| 210 | { |
| 211 | struct backlight_device *bd = to_backlight_device(dev); |
| 212 | |
| 213 | if (bd->ops->options & BL_CORE_SUSPENDRESUME) { |
| 214 | mutex_lock(&bd->ops_lock); |
| 215 | bd->props.state &= ~BL_CORE_SUSPENDED; |
| 216 | backlight_update_status(bd); |
| 217 | mutex_unlock(&bd->ops_lock); |
| 218 | } |
| 219 | |
| 220 | return 0; |
| 221 | } |
| 222 | |
Richard Purdie | 655bfd7 | 2007-07-09 12:17:24 +0100 | [diff] [blame] | 223 | static void bl_device_release(struct device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | { |
| 225 | struct backlight_device *bd = to_backlight_device(dev); |
| 226 | kfree(bd); |
| 227 | } |
| 228 | |
Richard Purdie | 655bfd7 | 2007-07-09 12:17:24 +0100 | [diff] [blame] | 229 | static struct device_attribute bl_device_attributes[] = { |
| 230 | __ATTR(bl_power, 0644, backlight_show_power, backlight_store_power), |
| 231 | __ATTR(brightness, 0644, backlight_show_brightness, |
Richard Purdie | 6ca0176 | 2006-03-31 02:31:49 -0800 | [diff] [blame] | 232 | backlight_store_brightness), |
Richard Purdie | 655bfd7 | 2007-07-09 12:17:24 +0100 | [diff] [blame] | 233 | __ATTR(actual_brightness, 0444, backlight_show_actual_brightness, |
Richard Purdie | 6ca0176 | 2006-03-31 02:31:49 -0800 | [diff] [blame] | 234 | NULL), |
Richard Purdie | 655bfd7 | 2007-07-09 12:17:24 +0100 | [diff] [blame] | 235 | __ATTR(max_brightness, 0444, backlight_show_max_brightness, NULL), |
| 236 | __ATTR_NULL, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | }; |
| 238 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | /** |
Matthew Garrett | 325253a | 2009-07-14 17:06:02 +0100 | [diff] [blame] | 240 | * backlight_force_update - tell the backlight subsystem that hardware state |
| 241 | * has changed |
| 242 | * @bd: the backlight device to update |
| 243 | * |
| 244 | * Updates the internal state of the backlight in response to a hardware event, |
| 245 | * and generate a uevent to notify userspace |
| 246 | */ |
| 247 | void backlight_force_update(struct backlight_device *bd, |
| 248 | enum backlight_update_reason reason) |
| 249 | { |
| 250 | mutex_lock(&bd->ops_lock); |
| 251 | if (bd->ops && bd->ops->get_brightness) |
| 252 | bd->props.brightness = bd->ops->get_brightness(bd); |
| 253 | mutex_unlock(&bd->ops_lock); |
| 254 | backlight_generate_event(bd, reason); |
| 255 | } |
| 256 | EXPORT_SYMBOL(backlight_force_update); |
| 257 | |
| 258 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | * backlight_device_register - create and register a new object of |
| 260 | * backlight_device class. |
| 261 | * @name: the name of the new object(must be the same as the name of the |
| 262 | * respective framebuffer device). |
Sebastian Siewior | f6ec2d9 | 2008-07-16 23:05:49 +0100 | [diff] [blame] | 263 | * @parent: a pointer to the parent device |
Richard Purdie | 655bfd7 | 2007-07-09 12:17:24 +0100 | [diff] [blame] | 264 | * @devdata: an optional pointer to be stored for private driver use. The |
| 265 | * methods may retrieve it by using bl_get_data(bd). |
Richard Purdie | 599a52d | 2007-02-10 23:07:48 +0000 | [diff] [blame] | 266 | * @ops: the backlight operations structure. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | * |
Richard Purdie | 655bfd7 | 2007-07-09 12:17:24 +0100 | [diff] [blame] | 268 | * Creates and registers new backlight device. Returns either an |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | * ERR_PTR() or a pointer to the newly allocated device. |
| 270 | */ |
Yu Luming | 519ab5f | 2006-12-19 12:56:15 -0800 | [diff] [blame] | 271 | struct backlight_device *backlight_device_register(const char *name, |
Richard Purdie | 655bfd7 | 2007-07-09 12:17:24 +0100 | [diff] [blame] | 272 | struct device *parent, void *devdata, struct backlight_ops *ops) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | struct backlight_device *new_bd; |
Richard Purdie | 655bfd7 | 2007-07-09 12:17:24 +0100 | [diff] [blame] | 275 | int rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | |
Richard Purdie | 655bfd7 | 2007-07-09 12:17:24 +0100 | [diff] [blame] | 277 | pr_debug("backlight_device_register: name=%s\n", name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | |
Richard Purdie | 599a52d | 2007-02-10 23:07:48 +0000 | [diff] [blame] | 279 | new_bd = kzalloc(sizeof(struct backlight_device), GFP_KERNEL); |
Dmitry Torokhov | 90968e8 | 2007-02-08 00:12:28 +0000 | [diff] [blame] | 280 | if (!new_bd) |
Jean Delvare | 10ad1b7 | 2006-03-09 17:33:36 -0800 | [diff] [blame] | 281 | return ERR_PTR(-ENOMEM); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | |
Richard Purdie | 28ee086 | 2007-02-08 22:25:09 +0000 | [diff] [blame] | 283 | mutex_init(&new_bd->update_lock); |
Richard Purdie | 599a52d | 2007-02-10 23:07:48 +0000 | [diff] [blame] | 284 | mutex_init(&new_bd->ops_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | |
Richard Purdie | 655bfd7 | 2007-07-09 12:17:24 +0100 | [diff] [blame] | 286 | new_bd->dev.class = backlight_class; |
| 287 | new_bd->dev.parent = parent; |
| 288 | new_bd->dev.release = bl_device_release; |
Kay Sievers | 64dba9a | 2009-01-06 10:44:35 -0800 | [diff] [blame] | 289 | dev_set_name(&new_bd->dev, name); |
Richard Purdie | 655bfd7 | 2007-07-09 12:17:24 +0100 | [diff] [blame] | 290 | dev_set_drvdata(&new_bd->dev, devdata); |
| 291 | |
| 292 | rc = device_register(&new_bd->dev); |
Dmitry Torokhov | 90968e8 | 2007-02-08 00:12:28 +0000 | [diff] [blame] | 293 | if (rc) { |
Dmitry Torokhov | 2fd5a15 | 2007-02-07 22:25:50 +0000 | [diff] [blame] | 294 | kfree(new_bd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | return ERR_PTR(rc); |
| 296 | } |
| 297 | |
James Simmons | 3d5eead | 2006-12-08 02:40:47 -0800 | [diff] [blame] | 298 | rc = backlight_register_fb(new_bd); |
Dmitry Torokhov | 2fd5a15 | 2007-02-07 22:25:50 +0000 | [diff] [blame] | 299 | if (rc) { |
Richard Purdie | 655bfd7 | 2007-07-09 12:17:24 +0100 | [diff] [blame] | 300 | device_unregister(&new_bd->dev); |
Dmitry Torokhov | 2fd5a15 | 2007-02-07 22:25:50 +0000 | [diff] [blame] | 301 | return ERR_PTR(rc); |
| 302 | } |
| 303 | |
Richard Purdie | 655bfd7 | 2007-07-09 12:17:24 +0100 | [diff] [blame] | 304 | new_bd->ops = ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | |
Richard Purdie | 321709c | 2007-02-10 15:04:08 +0000 | [diff] [blame] | 306 | #ifdef CONFIG_PMAC_BACKLIGHT |
| 307 | mutex_lock(&pmac_backlight_mutex); |
| 308 | if (!pmac_backlight) |
| 309 | pmac_backlight = new_bd; |
| 310 | mutex_unlock(&pmac_backlight_mutex); |
| 311 | #endif |
| 312 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | return new_bd; |
| 314 | } |
| 315 | EXPORT_SYMBOL(backlight_device_register); |
| 316 | |
| 317 | /** |
| 318 | * backlight_device_unregister - unregisters a backlight device object. |
| 319 | * @bd: the backlight device object to be unregistered and freed. |
| 320 | * |
| 321 | * Unregisters a previously registered via backlight_device_register object. |
| 322 | */ |
| 323 | void backlight_device_unregister(struct backlight_device *bd) |
| 324 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | if (!bd) |
| 326 | return; |
| 327 | |
Richard Purdie | 321709c | 2007-02-10 15:04:08 +0000 | [diff] [blame] | 328 | #ifdef CONFIG_PMAC_BACKLIGHT |
| 329 | mutex_lock(&pmac_backlight_mutex); |
| 330 | if (pmac_backlight == bd) |
| 331 | pmac_backlight = NULL; |
| 332 | mutex_unlock(&pmac_backlight_mutex); |
| 333 | #endif |
Richard Purdie | 599a52d | 2007-02-10 23:07:48 +0000 | [diff] [blame] | 334 | mutex_lock(&bd->ops_lock); |
| 335 | bd->ops = NULL; |
| 336 | mutex_unlock(&bd->ops_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | |
James Simmons | 3d5eead | 2006-12-08 02:40:47 -0800 | [diff] [blame] | 338 | backlight_unregister_fb(bd); |
Richard Purdie | 655bfd7 | 2007-07-09 12:17:24 +0100 | [diff] [blame] | 339 | device_unregister(&bd->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | } |
| 341 | EXPORT_SYMBOL(backlight_device_unregister); |
| 342 | |
| 343 | static void __exit backlight_class_exit(void) |
| 344 | { |
Richard Purdie | 655bfd7 | 2007-07-09 12:17:24 +0100 | [diff] [blame] | 345 | class_destroy(backlight_class); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | static int __init backlight_class_init(void) |
| 349 | { |
Richard Purdie | 655bfd7 | 2007-07-09 12:17:24 +0100 | [diff] [blame] | 350 | backlight_class = class_create(THIS_MODULE, "backlight"); |
| 351 | if (IS_ERR(backlight_class)) { |
| 352 | printk(KERN_WARNING "Unable to create backlight class; errno = %ld\n", |
| 353 | PTR_ERR(backlight_class)); |
| 354 | return PTR_ERR(backlight_class); |
| 355 | } |
| 356 | |
| 357 | backlight_class->dev_attrs = bl_device_attributes; |
Richard Purdie | c835ee7 | 2009-01-06 21:00:19 +0000 | [diff] [blame] | 358 | backlight_class->suspend = backlight_suspend; |
| 359 | backlight_class->resume = backlight_resume; |
Richard Purdie | 655bfd7 | 2007-07-09 12:17:24 +0100 | [diff] [blame] | 360 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | /* |
| 364 | * if this is compiled into the kernel, we need to ensure that the |
| 365 | * class is registered before users of the class try to register lcd's |
| 366 | */ |
| 367 | postcore_initcall(backlight_class_init); |
| 368 | module_exit(backlight_class_exit); |
| 369 | |
| 370 | MODULE_LICENSE("GPL"); |
| 371 | MODULE_AUTHOR("Jamey Hicks <jamey.hicks@hp.com>, Andrew Zabolotny <zap@homelink.ru>"); |
| 372 | MODULE_DESCRIPTION("Backlight Lowlevel Control Abstraction"); |