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