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 | |
Jingoo Han | 35f9616 | 2012-05-29 15:07:16 -0700 | [diff] [blame] | 8 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 9 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | #include <linux/module.h> |
| 11 | #include <linux/init.h> |
| 12 | #include <linux/device.h> |
| 13 | #include <linux/backlight.h> |
| 14 | #include <linux/notifier.h> |
| 15 | #include <linux/ctype.h> |
| 16 | #include <linux/err.h> |
| 17 | #include <linux/fb.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 18 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | |
Richard Purdie | 321709c | 2007-02-10 15:04:08 +0000 | [diff] [blame] | 20 | #ifdef CONFIG_PMAC_BACKLIGHT |
| 21 | #include <asm/backlight.h> |
| 22 | #endif |
James Simmons | 3d5eead | 2006-12-08 02:40:47 -0800 | [diff] [blame] | 23 | |
Bart Van Assche | c338bfb | 2011-09-10 20:13:01 +0200 | [diff] [blame] | 24 | static const char *const backlight_types[] = { |
Matthew Garrett | bb7ca74 | 2011-03-22 16:30:21 -0700 | [diff] [blame] | 25 | [BACKLIGHT_RAW] = "raw", |
| 26 | [BACKLIGHT_PLATFORM] = "platform", |
| 27 | [BACKLIGHT_FIRMWARE] = "firmware", |
| 28 | }; |
| 29 | |
James Simmons | 3d5eead | 2006-12-08 02:40:47 -0800 | [diff] [blame] | 30 | #if defined(CONFIG_FB) || (defined(CONFIG_FB_MODULE) && \ |
| 31 | defined(CONFIG_BACKLIGHT_CLASS_DEVICE_MODULE)) |
| 32 | /* This callback gets called when something important happens inside a |
| 33 | * framebuffer driver. We're looking if that important event is blanking, |
| 34 | * and if it is, we're switching backlight power as well ... |
| 35 | */ |
| 36 | static int fb_notifier_callback(struct notifier_block *self, |
| 37 | unsigned long event, void *data) |
| 38 | { |
| 39 | struct backlight_device *bd; |
| 40 | struct fb_event *evdata = data; |
| 41 | |
| 42 | /* If we aren't interested in this event, skip it immediately ... */ |
Richard Purdie | 994efac | 2007-02-09 09:46:45 +0000 | [diff] [blame] | 43 | if (event != FB_EVENT_BLANK && event != FB_EVENT_CONBLANK) |
James Simmons | 3d5eead | 2006-12-08 02:40:47 -0800 | [diff] [blame] | 44 | return 0; |
| 45 | |
| 46 | bd = container_of(self, struct backlight_device, fb_notif); |
Richard Purdie | 599a52d | 2007-02-10 23:07:48 +0000 | [diff] [blame] | 47 | mutex_lock(&bd->ops_lock); |
| 48 | if (bd->ops) |
| 49 | if (!bd->ops->check_fb || |
Bruno Prémont | 57e148b | 2010-02-21 00:20:01 +0100 | [diff] [blame] | 50 | bd->ops->check_fb(bd, evdata->info)) { |
Richard Purdie | 599a52d | 2007-02-10 23:07:48 +0000 | [diff] [blame] | 51 | bd->props.fb_blank = *(int *)evdata->data; |
Richard Purdie | c835ee7 | 2009-01-06 21:00:19 +0000 | [diff] [blame] | 52 | if (bd->props.fb_blank == FB_BLANK_UNBLANK) |
| 53 | bd->props.state &= ~BL_CORE_FBBLANK; |
| 54 | else |
| 55 | bd->props.state |= BL_CORE_FBBLANK; |
Richard Purdie | 28ee086 | 2007-02-08 22:25:09 +0000 | [diff] [blame] | 56 | backlight_update_status(bd); |
James Simmons | 3d5eead | 2006-12-08 02:40:47 -0800 | [diff] [blame] | 57 | } |
Richard Purdie | 599a52d | 2007-02-10 23:07:48 +0000 | [diff] [blame] | 58 | mutex_unlock(&bd->ops_lock); |
James Simmons | 3d5eead | 2006-12-08 02:40:47 -0800 | [diff] [blame] | 59 | return 0; |
| 60 | } |
| 61 | |
| 62 | static int backlight_register_fb(struct backlight_device *bd) |
| 63 | { |
| 64 | memset(&bd->fb_notif, 0, sizeof(bd->fb_notif)); |
| 65 | bd->fb_notif.notifier_call = fb_notifier_callback; |
| 66 | |
| 67 | return fb_register_client(&bd->fb_notif); |
| 68 | } |
| 69 | |
| 70 | static void backlight_unregister_fb(struct backlight_device *bd) |
| 71 | { |
| 72 | fb_unregister_client(&bd->fb_notif); |
| 73 | } |
| 74 | #else |
| 75 | static inline int backlight_register_fb(struct backlight_device *bd) |
| 76 | { |
| 77 | return 0; |
| 78 | } |
| 79 | |
| 80 | static inline void backlight_unregister_fb(struct backlight_device *bd) |
| 81 | { |
| 82 | } |
| 83 | #endif /* CONFIG_FB */ |
| 84 | |
Matthew Garrett | 325253a | 2009-07-14 17:06:02 +0100 | [diff] [blame] | 85 | static void backlight_generate_event(struct backlight_device *bd, |
| 86 | enum backlight_update_reason reason) |
| 87 | { |
| 88 | char *envp[2]; |
| 89 | |
| 90 | switch (reason) { |
| 91 | case BACKLIGHT_UPDATE_SYSFS: |
| 92 | envp[0] = "SOURCE=sysfs"; |
| 93 | break; |
| 94 | case BACKLIGHT_UPDATE_HOTKEY: |
| 95 | envp[0] = "SOURCE=hotkey"; |
| 96 | break; |
| 97 | default: |
| 98 | envp[0] = "SOURCE=unknown"; |
| 99 | break; |
| 100 | } |
| 101 | envp[1] = NULL; |
| 102 | kobject_uevent_env(&bd->dev.kobj, KOBJ_CHANGE, envp); |
Henrique de Moraes Holschuh | 89dfc28 | 2009-09-20 14:44:47 -0300 | [diff] [blame] | 103 | sysfs_notify(&bd->dev.kobj, NULL, "actual_brightness"); |
Matthew Garrett | 325253a | 2009-07-14 17:06:02 +0100 | [diff] [blame] | 104 | } |
| 105 | |
Greg Kroah-Hartman | ea1bb70 | 2013-07-24 15:05:30 -0700 | [diff] [blame^] | 106 | static ssize_t bl_power_show(struct device *dev, struct device_attribute *attr, |
| 107 | char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | { |
Richard Purdie | 655bfd7 | 2007-07-09 12:17:24 +0100 | [diff] [blame] | 109 | struct backlight_device *bd = to_backlight_device(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | |
Richard Purdie | 599a52d | 2007-02-10 23:07:48 +0000 | [diff] [blame] | 111 | return sprintf(buf, "%d\n", bd->props.power); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Greg Kroah-Hartman | ea1bb70 | 2013-07-24 15:05:30 -0700 | [diff] [blame^] | 114 | static ssize_t bl_power_store(struct device *dev, struct device_attribute *attr, |
| 115 | const char *buf, size_t count) |
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 | int rc; |
Richard Purdie | 655bfd7 | 2007-07-09 12:17:24 +0100 | [diff] [blame] | 118 | struct backlight_device *bd = to_backlight_device(dev); |
Pavel Machek | 9a2c61a | 2008-12-03 08:43:48 +0000 | [diff] [blame] | 119 | unsigned long power; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | |
Jingoo Han | 6665576 | 2012-01-10 15:09:19 -0800 | [diff] [blame] | 121 | rc = kstrtoul(buf, 0, &power); |
Pavel Machek | 9a2c61a | 2008-12-03 08:43:48 +0000 | [diff] [blame] | 122 | if (rc) |
| 123 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | |
Pavel Machek | 9a2c61a | 2008-12-03 08:43:48 +0000 | [diff] [blame] | 125 | rc = -ENXIO; |
Richard Purdie | 599a52d | 2007-02-10 23:07:48 +0000 | [diff] [blame] | 126 | mutex_lock(&bd->ops_lock); |
| 127 | if (bd->ops) { |
Jingoo Han | 35f9616 | 2012-05-29 15:07:16 -0700 | [diff] [blame] | 128 | pr_debug("set power to %lu\n", power); |
Helge Deller | 5155245 | 2008-01-13 23:01:13 +0000 | [diff] [blame] | 129 | if (bd->props.power != power) { |
| 130 | bd->props.power = power; |
| 131 | backlight_update_status(bd); |
| 132 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | rc = count; |
Richard Purdie | 6ca0176 | 2006-03-31 02:31:49 -0800 | [diff] [blame] | 134 | } |
Richard Purdie | 599a52d | 2007-02-10 23:07:48 +0000 | [diff] [blame] | 135 | mutex_unlock(&bd->ops_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | |
| 137 | return rc; |
| 138 | } |
Greg Kroah-Hartman | ea1bb70 | 2013-07-24 15:05:30 -0700 | [diff] [blame^] | 139 | static DEVICE_ATTR_RW(bl_power); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | |
Greg Kroah-Hartman | ea1bb70 | 2013-07-24 15:05:30 -0700 | [diff] [blame^] | 141 | static ssize_t brightness_show(struct device *dev, |
Richard Purdie | 655bfd7 | 2007-07-09 12:17:24 +0100 | [diff] [blame] | 142 | struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | { |
Richard Purdie | 655bfd7 | 2007-07-09 12:17:24 +0100 | [diff] [blame] | 144 | struct backlight_device *bd = to_backlight_device(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | |
Richard Purdie | 599a52d | 2007-02-10 23:07:48 +0000 | [diff] [blame] | 146 | return sprintf(buf, "%d\n", bd->props.brightness); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | } |
| 148 | |
Greg Kroah-Hartman | ea1bb70 | 2013-07-24 15:05:30 -0700 | [diff] [blame^] | 149 | static ssize_t brightness_store(struct device *dev, |
Richard Purdie | 655bfd7 | 2007-07-09 12:17:24 +0100 | [diff] [blame] | 150 | struct device_attribute *attr, const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | { |
Pavel Machek | 9a2c61a | 2008-12-03 08:43:48 +0000 | [diff] [blame] | 152 | int rc; |
Richard Purdie | 655bfd7 | 2007-07-09 12:17:24 +0100 | [diff] [blame] | 153 | struct backlight_device *bd = to_backlight_device(dev); |
Pavel Machek | 9a2c61a | 2008-12-03 08:43:48 +0000 | [diff] [blame] | 154 | unsigned long brightness; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | |
Jingoo Han | 6665576 | 2012-01-10 15:09:19 -0800 | [diff] [blame] | 156 | rc = kstrtoul(buf, 0, &brightness); |
Pavel Machek | 9a2c61a | 2008-12-03 08:43:48 +0000 | [diff] [blame] | 157 | if (rc) |
| 158 | return rc; |
| 159 | |
| 160 | rc = -ENXIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | |
Richard Purdie | 599a52d | 2007-02-10 23:07:48 +0000 | [diff] [blame] | 162 | mutex_lock(&bd->ops_lock); |
| 163 | if (bd->ops) { |
| 164 | if (brightness > bd->props.max_brightness) |
Richard Purdie | 6ca0176 | 2006-03-31 02:31:49 -0800 | [diff] [blame] | 165 | rc = -EINVAL; |
| 166 | else { |
Jingoo Han | 35f9616 | 2012-05-29 15:07:16 -0700 | [diff] [blame] | 167 | pr_debug("set brightness to %lu\n", brightness); |
Zhang Rui | 9be1df9 | 2009-01-08 14:11:30 +0000 | [diff] [blame] | 168 | bd->props.brightness = brightness; |
| 169 | backlight_update_status(bd); |
Richard Purdie | 6ca0176 | 2006-03-31 02:31:49 -0800 | [diff] [blame] | 170 | rc = count; |
| 171 | } |
| 172 | } |
Richard Purdie | 599a52d | 2007-02-10 23:07:48 +0000 | [diff] [blame] | 173 | mutex_unlock(&bd->ops_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | |
Matthew Garrett | 325253a | 2009-07-14 17:06:02 +0100 | [diff] [blame] | 175 | backlight_generate_event(bd, BACKLIGHT_UPDATE_SYSFS); |
| 176 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | return rc; |
| 178 | } |
Greg Kroah-Hartman | ea1bb70 | 2013-07-24 15:05:30 -0700 | [diff] [blame^] | 179 | static DEVICE_ATTR_RW(brightness); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | |
Greg Kroah-Hartman | ea1bb70 | 2013-07-24 15:05:30 -0700 | [diff] [blame^] | 181 | static ssize_t type_show(struct device *dev, struct device_attribute *attr, |
| 182 | char *buf) |
Matthew Garrett | bb7ca74 | 2011-03-22 16:30:21 -0700 | [diff] [blame] | 183 | { |
| 184 | struct backlight_device *bd = to_backlight_device(dev); |
| 185 | |
| 186 | return sprintf(buf, "%s\n", backlight_types[bd->props.type]); |
| 187 | } |
Greg Kroah-Hartman | ea1bb70 | 2013-07-24 15:05:30 -0700 | [diff] [blame^] | 188 | static DEVICE_ATTR_RO(type); |
Matthew Garrett | bb7ca74 | 2011-03-22 16:30:21 -0700 | [diff] [blame] | 189 | |
Greg Kroah-Hartman | ea1bb70 | 2013-07-24 15:05:30 -0700 | [diff] [blame^] | 190 | static ssize_t max_brightness_show(struct device *dev, |
Richard Purdie | 655bfd7 | 2007-07-09 12:17:24 +0100 | [diff] [blame] | 191 | struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | { |
Richard Purdie | 655bfd7 | 2007-07-09 12:17:24 +0100 | [diff] [blame] | 193 | struct backlight_device *bd = to_backlight_device(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | |
Richard Purdie | 599a52d | 2007-02-10 23:07:48 +0000 | [diff] [blame] | 195 | return sprintf(buf, "%d\n", bd->props.max_brightness); |
Richard Purdie | 6ca0176 | 2006-03-31 02:31:49 -0800 | [diff] [blame] | 196 | } |
Greg Kroah-Hartman | ea1bb70 | 2013-07-24 15:05:30 -0700 | [diff] [blame^] | 197 | static DEVICE_ATTR_RO(max_brightness); |
Richard Purdie | 6ca0176 | 2006-03-31 02:31:49 -0800 | [diff] [blame] | 198 | |
Greg Kroah-Hartman | ea1bb70 | 2013-07-24 15:05:30 -0700 | [diff] [blame^] | 199 | static ssize_t actual_brightness_show(struct device *dev, |
Richard Purdie | 655bfd7 | 2007-07-09 12:17:24 +0100 | [diff] [blame] | 200 | struct device_attribute *attr, char *buf) |
Richard Purdie | 6ca0176 | 2006-03-31 02:31:49 -0800 | [diff] [blame] | 201 | { |
| 202 | int rc = -ENXIO; |
Richard Purdie | 655bfd7 | 2007-07-09 12:17:24 +0100 | [diff] [blame] | 203 | struct backlight_device *bd = to_backlight_device(dev); |
Richard Purdie | 6ca0176 | 2006-03-31 02:31:49 -0800 | [diff] [blame] | 204 | |
Richard Purdie | 599a52d | 2007-02-10 23:07:48 +0000 | [diff] [blame] | 205 | mutex_lock(&bd->ops_lock); |
| 206 | if (bd->ops && bd->ops->get_brightness) |
| 207 | rc = sprintf(buf, "%d\n", bd->ops->get_brightness(bd)); |
| 208 | mutex_unlock(&bd->ops_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | |
| 210 | return rc; |
| 211 | } |
Greg Kroah-Hartman | ea1bb70 | 2013-07-24 15:05:30 -0700 | [diff] [blame^] | 212 | static DEVICE_ATTR_RO(actual_brightness); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | |
Adrian Bunk | 0ad90ef | 2007-08-11 10:27:19 +0100 | [diff] [blame] | 214 | static struct class *backlight_class; |
Richard Purdie | 655bfd7 | 2007-07-09 12:17:24 +0100 | [diff] [blame] | 215 | |
Shuah Khan | 3601792 | 2013-07-03 15:05:16 -0700 | [diff] [blame] | 216 | #ifdef CONFIG_PM_SLEEP |
| 217 | static int backlight_suspend(struct device *dev) |
Richard Purdie | c835ee7 | 2009-01-06 21:00:19 +0000 | [diff] [blame] | 218 | { |
| 219 | struct backlight_device *bd = to_backlight_device(dev); |
| 220 | |
Uwe Kleine-König | d1d7357 | 2010-11-24 12:57:14 -0800 | [diff] [blame] | 221 | mutex_lock(&bd->ops_lock); |
| 222 | if (bd->ops && bd->ops->options & BL_CORE_SUSPENDRESUME) { |
Richard Purdie | c835ee7 | 2009-01-06 21:00:19 +0000 | [diff] [blame] | 223 | bd->props.state |= BL_CORE_SUSPENDED; |
| 224 | backlight_update_status(bd); |
Richard Purdie | c835ee7 | 2009-01-06 21:00:19 +0000 | [diff] [blame] | 225 | } |
Uwe Kleine-König | d1d7357 | 2010-11-24 12:57:14 -0800 | [diff] [blame] | 226 | mutex_unlock(&bd->ops_lock); |
Richard Purdie | c835ee7 | 2009-01-06 21:00:19 +0000 | [diff] [blame] | 227 | |
| 228 | return 0; |
| 229 | } |
| 230 | |
| 231 | static int backlight_resume(struct device *dev) |
| 232 | { |
| 233 | struct backlight_device *bd = to_backlight_device(dev); |
| 234 | |
Uwe Kleine-König | d1d7357 | 2010-11-24 12:57:14 -0800 | [diff] [blame] | 235 | mutex_lock(&bd->ops_lock); |
| 236 | if (bd->ops && bd->ops->options & BL_CORE_SUSPENDRESUME) { |
Richard Purdie | c835ee7 | 2009-01-06 21:00:19 +0000 | [diff] [blame] | 237 | bd->props.state &= ~BL_CORE_SUSPENDED; |
| 238 | backlight_update_status(bd); |
Richard Purdie | c835ee7 | 2009-01-06 21:00:19 +0000 | [diff] [blame] | 239 | } |
Uwe Kleine-König | d1d7357 | 2010-11-24 12:57:14 -0800 | [diff] [blame] | 240 | mutex_unlock(&bd->ops_lock); |
Richard Purdie | c835ee7 | 2009-01-06 21:00:19 +0000 | [diff] [blame] | 241 | |
| 242 | return 0; |
| 243 | } |
Shuah Khan | 3601792 | 2013-07-03 15:05:16 -0700 | [diff] [blame] | 244 | #endif |
| 245 | |
| 246 | static SIMPLE_DEV_PM_OPS(backlight_class_dev_pm_ops, backlight_suspend, |
| 247 | backlight_resume); |
Richard Purdie | c835ee7 | 2009-01-06 21:00:19 +0000 | [diff] [blame] | 248 | |
Richard Purdie | 655bfd7 | 2007-07-09 12:17:24 +0100 | [diff] [blame] | 249 | static void bl_device_release(struct device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | { |
| 251 | struct backlight_device *bd = to_backlight_device(dev); |
| 252 | kfree(bd); |
| 253 | } |
| 254 | |
Greg Kroah-Hartman | ea1bb70 | 2013-07-24 15:05:30 -0700 | [diff] [blame^] | 255 | static struct attribute *bl_device_attrs[] = { |
| 256 | &dev_attr_bl_power.attr, |
| 257 | &dev_attr_brightness.attr, |
| 258 | &dev_attr_actual_brightness.attr, |
| 259 | &dev_attr_max_brightness.attr, |
| 260 | &dev_attr_type.attr, |
| 261 | NULL, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | }; |
Greg Kroah-Hartman | ea1bb70 | 2013-07-24 15:05:30 -0700 | [diff] [blame^] | 263 | ATTRIBUTE_GROUPS(bl_device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | /** |
Matthew Garrett | 325253a | 2009-07-14 17:06:02 +0100 | [diff] [blame] | 266 | * backlight_force_update - tell the backlight subsystem that hardware state |
| 267 | * has changed |
| 268 | * @bd: the backlight device to update |
| 269 | * |
| 270 | * Updates the internal state of the backlight in response to a hardware event, |
| 271 | * and generate a uevent to notify userspace |
| 272 | */ |
| 273 | void backlight_force_update(struct backlight_device *bd, |
| 274 | enum backlight_update_reason reason) |
| 275 | { |
| 276 | mutex_lock(&bd->ops_lock); |
| 277 | if (bd->ops && bd->ops->get_brightness) |
| 278 | bd->props.brightness = bd->ops->get_brightness(bd); |
| 279 | mutex_unlock(&bd->ops_lock); |
| 280 | backlight_generate_event(bd, reason); |
| 281 | } |
| 282 | EXPORT_SYMBOL(backlight_force_update); |
| 283 | |
| 284 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | * backlight_device_register - create and register a new object of |
| 286 | * backlight_device class. |
| 287 | * @name: the name of the new object(must be the same as the name of the |
| 288 | * respective framebuffer device). |
Sebastian Siewior | f6ec2d9 | 2008-07-16 23:05:49 +0100 | [diff] [blame] | 289 | * @parent: a pointer to the parent device |
Richard Purdie | 655bfd7 | 2007-07-09 12:17:24 +0100 | [diff] [blame] | 290 | * @devdata: an optional pointer to be stored for private driver use. The |
| 291 | * methods may retrieve it by using bl_get_data(bd). |
Richard Purdie | 599a52d | 2007-02-10 23:07:48 +0000 | [diff] [blame] | 292 | * @ops: the backlight operations structure. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | * |
Richard Purdie | 655bfd7 | 2007-07-09 12:17:24 +0100 | [diff] [blame] | 294 | * Creates and registers new backlight device. Returns either an |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | * ERR_PTR() or a pointer to the newly allocated device. |
| 296 | */ |
Yu Luming | 519ab5f | 2006-12-19 12:56:15 -0800 | [diff] [blame] | 297 | struct backlight_device *backlight_device_register(const char *name, |
Matthew Garrett | a19a6ee | 2010-02-17 16:39:44 -0500 | [diff] [blame] | 298 | struct device *parent, void *devdata, const struct backlight_ops *ops, |
| 299 | const struct backlight_properties *props) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | struct backlight_device *new_bd; |
Richard Purdie | 655bfd7 | 2007-07-09 12:17:24 +0100 | [diff] [blame] | 302 | int rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | |
Richard Purdie | 655bfd7 | 2007-07-09 12:17:24 +0100 | [diff] [blame] | 304 | pr_debug("backlight_device_register: name=%s\n", name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | |
Richard Purdie | 599a52d | 2007-02-10 23:07:48 +0000 | [diff] [blame] | 306 | new_bd = kzalloc(sizeof(struct backlight_device), GFP_KERNEL); |
Dmitry Torokhov | 90968e8 | 2007-02-08 00:12:28 +0000 | [diff] [blame] | 307 | if (!new_bd) |
Jean Delvare | 10ad1b7 | 2006-03-09 17:33:36 -0800 | [diff] [blame] | 308 | return ERR_PTR(-ENOMEM); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | |
Richard Purdie | 28ee086 | 2007-02-08 22:25:09 +0000 | [diff] [blame] | 310 | mutex_init(&new_bd->update_lock); |
Richard Purdie | 599a52d | 2007-02-10 23:07:48 +0000 | [diff] [blame] | 311 | mutex_init(&new_bd->ops_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | |
Richard Purdie | 655bfd7 | 2007-07-09 12:17:24 +0100 | [diff] [blame] | 313 | new_bd->dev.class = backlight_class; |
| 314 | new_bd->dev.parent = parent; |
| 315 | new_bd->dev.release = bl_device_release; |
Kees Cook | 02aa2a3 | 2013-07-03 15:04:56 -0700 | [diff] [blame] | 316 | dev_set_name(&new_bd->dev, "%s", name); |
Richard Purdie | 655bfd7 | 2007-07-09 12:17:24 +0100 | [diff] [blame] | 317 | dev_set_drvdata(&new_bd->dev, devdata); |
| 318 | |
Matthew Garrett | a19a6ee | 2010-02-17 16:39:44 -0500 | [diff] [blame] | 319 | /* Set default properties */ |
Matthew Garrett | bb7ca74 | 2011-03-22 16:30:21 -0700 | [diff] [blame] | 320 | if (props) { |
Matthew Garrett | a19a6ee | 2010-02-17 16:39:44 -0500 | [diff] [blame] | 321 | memcpy(&new_bd->props, props, |
| 322 | sizeof(struct backlight_properties)); |
Matthew Garrett | bb7ca74 | 2011-03-22 16:30:21 -0700 | [diff] [blame] | 323 | if (props->type <= 0 || props->type >= BACKLIGHT_TYPE_MAX) { |
| 324 | WARN(1, "%s: invalid backlight type", name); |
| 325 | new_bd->props.type = BACKLIGHT_RAW; |
| 326 | } |
| 327 | } else { |
| 328 | new_bd->props.type = BACKLIGHT_RAW; |
| 329 | } |
Matthew Garrett | a19a6ee | 2010-02-17 16:39:44 -0500 | [diff] [blame] | 330 | |
Richard Purdie | 655bfd7 | 2007-07-09 12:17:24 +0100 | [diff] [blame] | 331 | rc = device_register(&new_bd->dev); |
Dmitry Torokhov | 90968e8 | 2007-02-08 00:12:28 +0000 | [diff] [blame] | 332 | if (rc) { |
Dmitry Torokhov | 2fd5a15 | 2007-02-07 22:25:50 +0000 | [diff] [blame] | 333 | kfree(new_bd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | return ERR_PTR(rc); |
| 335 | } |
| 336 | |
James Simmons | 3d5eead | 2006-12-08 02:40:47 -0800 | [diff] [blame] | 337 | rc = backlight_register_fb(new_bd); |
Dmitry Torokhov | 2fd5a15 | 2007-02-07 22:25:50 +0000 | [diff] [blame] | 338 | if (rc) { |
Richard Purdie | 655bfd7 | 2007-07-09 12:17:24 +0100 | [diff] [blame] | 339 | device_unregister(&new_bd->dev); |
Dmitry Torokhov | 2fd5a15 | 2007-02-07 22:25:50 +0000 | [diff] [blame] | 340 | return ERR_PTR(rc); |
| 341 | } |
| 342 | |
Richard Purdie | 655bfd7 | 2007-07-09 12:17:24 +0100 | [diff] [blame] | 343 | new_bd->ops = ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | |
Richard Purdie | 321709c | 2007-02-10 15:04:08 +0000 | [diff] [blame] | 345 | #ifdef CONFIG_PMAC_BACKLIGHT |
| 346 | mutex_lock(&pmac_backlight_mutex); |
| 347 | if (!pmac_backlight) |
| 348 | pmac_backlight = new_bd; |
| 349 | mutex_unlock(&pmac_backlight_mutex); |
| 350 | #endif |
| 351 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | return new_bd; |
| 353 | } |
| 354 | EXPORT_SYMBOL(backlight_device_register); |
| 355 | |
| 356 | /** |
| 357 | * backlight_device_unregister - unregisters a backlight device object. |
| 358 | * @bd: the backlight device object to be unregistered and freed. |
| 359 | * |
| 360 | * Unregisters a previously registered via backlight_device_register object. |
| 361 | */ |
| 362 | void backlight_device_unregister(struct backlight_device *bd) |
| 363 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | if (!bd) |
| 365 | return; |
| 366 | |
Richard Purdie | 321709c | 2007-02-10 15:04:08 +0000 | [diff] [blame] | 367 | #ifdef CONFIG_PMAC_BACKLIGHT |
| 368 | mutex_lock(&pmac_backlight_mutex); |
| 369 | if (pmac_backlight == bd) |
| 370 | pmac_backlight = NULL; |
| 371 | mutex_unlock(&pmac_backlight_mutex); |
| 372 | #endif |
Richard Purdie | 599a52d | 2007-02-10 23:07:48 +0000 | [diff] [blame] | 373 | mutex_lock(&bd->ops_lock); |
| 374 | bd->ops = NULL; |
| 375 | mutex_unlock(&bd->ops_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | |
James Simmons | 3d5eead | 2006-12-08 02:40:47 -0800 | [diff] [blame] | 377 | backlight_unregister_fb(bd); |
Richard Purdie | 655bfd7 | 2007-07-09 12:17:24 +0100 | [diff] [blame] | 378 | device_unregister(&bd->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | } |
| 380 | EXPORT_SYMBOL(backlight_device_unregister); |
| 381 | |
Jingoo Han | 8318fde4 | 2013-07-03 15:05:13 -0700 | [diff] [blame] | 382 | static void devm_backlight_device_release(struct device *dev, void *res) |
| 383 | { |
| 384 | struct backlight_device *backlight = *(struct backlight_device **)res; |
| 385 | |
| 386 | backlight_device_unregister(backlight); |
| 387 | } |
| 388 | |
| 389 | static int devm_backlight_device_match(struct device *dev, void *res, |
| 390 | void *data) |
| 391 | { |
| 392 | struct backlight_device **r = res; |
| 393 | |
| 394 | return *r == data; |
| 395 | } |
| 396 | |
| 397 | /** |
| 398 | * devm_backlight_device_register - resource managed backlight_device_register() |
| 399 | * @dev: the device to register |
| 400 | * @name: the name of the device |
| 401 | * @parent: a pointer to the parent device |
| 402 | * @devdata: an optional pointer to be stored for private driver use |
| 403 | * @ops: the backlight operations structure |
| 404 | * @props: the backlight properties |
| 405 | * |
| 406 | * @return a struct backlight on success, or an ERR_PTR on error |
| 407 | * |
| 408 | * Managed backlight_device_register(). The backlight_device returned |
| 409 | * from this function are automatically freed on driver detach. |
| 410 | * See backlight_device_register() for more information. |
| 411 | */ |
| 412 | struct backlight_device *devm_backlight_device_register(struct device *dev, |
| 413 | const char *name, struct device *parent, void *devdata, |
| 414 | const struct backlight_ops *ops, |
| 415 | const struct backlight_properties *props) |
| 416 | { |
| 417 | struct backlight_device **ptr, *backlight; |
| 418 | |
| 419 | ptr = devres_alloc(devm_backlight_device_release, sizeof(*ptr), |
| 420 | GFP_KERNEL); |
| 421 | if (!ptr) |
| 422 | return ERR_PTR(-ENOMEM); |
| 423 | |
| 424 | backlight = backlight_device_register(name, parent, devdata, ops, |
| 425 | props); |
| 426 | if (!IS_ERR(backlight)) { |
| 427 | *ptr = backlight; |
| 428 | devres_add(dev, ptr); |
| 429 | } else { |
| 430 | devres_free(ptr); |
| 431 | } |
| 432 | |
| 433 | return backlight; |
| 434 | } |
| 435 | EXPORT_SYMBOL(devm_backlight_device_register); |
| 436 | |
| 437 | /** |
| 438 | * devm_backlight_device_unregister - resource managed backlight_device_unregister() |
| 439 | * @dev: the device to unregister |
| 440 | * @bd: the backlight device to unregister |
| 441 | * |
| 442 | * Deallocated a backlight allocated with devm_backlight_device_register(). |
| 443 | * Normally this function will not need to be called and the resource management |
| 444 | * code will ensure that the resource is freed. |
| 445 | */ |
| 446 | void devm_backlight_device_unregister(struct device *dev, |
| 447 | struct backlight_device *bd) |
| 448 | { |
| 449 | int rc; |
| 450 | |
| 451 | rc = devres_release(dev, devm_backlight_device_release, |
| 452 | devm_backlight_device_match, bd); |
| 453 | WARN_ON(rc); |
| 454 | } |
| 455 | EXPORT_SYMBOL(devm_backlight_device_unregister); |
| 456 | |
Thierry Reding | 762a936 | 2012-12-17 16:01:06 -0800 | [diff] [blame] | 457 | #ifdef CONFIG_OF |
Greg Kroah-Hartman | 3213f63 | 2013-02-06 16:43:02 -0800 | [diff] [blame] | 458 | static int of_parent_match(struct device *dev, const void *data) |
Thierry Reding | 762a936 | 2012-12-17 16:01:06 -0800 | [diff] [blame] | 459 | { |
| 460 | return dev->parent && dev->parent->of_node == data; |
| 461 | } |
| 462 | |
| 463 | /** |
| 464 | * of_find_backlight_by_node() - find backlight device by device-tree node |
| 465 | * @node: device-tree node of the backlight device |
| 466 | * |
| 467 | * Returns a pointer to the backlight device corresponding to the given DT |
| 468 | * node or NULL if no such backlight device exists or if the device hasn't |
| 469 | * been probed yet. |
| 470 | * |
| 471 | * This function obtains a reference on the backlight device and it is the |
| 472 | * caller's responsibility to drop the reference by calling put_device() on |
| 473 | * the backlight device's .dev field. |
| 474 | */ |
| 475 | struct backlight_device *of_find_backlight_by_node(struct device_node *node) |
| 476 | { |
| 477 | struct device *dev; |
| 478 | |
| 479 | dev = class_find_device(backlight_class, NULL, node, of_parent_match); |
| 480 | |
| 481 | return dev ? to_backlight_device(dev) : NULL; |
| 482 | } |
| 483 | EXPORT_SYMBOL(of_find_backlight_by_node); |
| 484 | #endif |
| 485 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 486 | static void __exit backlight_class_exit(void) |
| 487 | { |
Richard Purdie | 655bfd7 | 2007-07-09 12:17:24 +0100 | [diff] [blame] | 488 | class_destroy(backlight_class); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | } |
| 490 | |
| 491 | static int __init backlight_class_init(void) |
| 492 | { |
Richard Purdie | 655bfd7 | 2007-07-09 12:17:24 +0100 | [diff] [blame] | 493 | backlight_class = class_create(THIS_MODULE, "backlight"); |
| 494 | if (IS_ERR(backlight_class)) { |
Jingoo Han | 35f9616 | 2012-05-29 15:07:16 -0700 | [diff] [blame] | 495 | pr_warn("Unable to create backlight class; errno = %ld\n", |
| 496 | PTR_ERR(backlight_class)); |
Richard Purdie | 655bfd7 | 2007-07-09 12:17:24 +0100 | [diff] [blame] | 497 | return PTR_ERR(backlight_class); |
| 498 | } |
| 499 | |
Greg Kroah-Hartman | ea1bb70 | 2013-07-24 15:05:30 -0700 | [diff] [blame^] | 500 | backlight_class->dev_groups = bl_device_groups; |
Shuah Khan | 3601792 | 2013-07-03 15:05:16 -0700 | [diff] [blame] | 501 | backlight_class->pm = &backlight_class_dev_pm_ops; |
Richard Purdie | 655bfd7 | 2007-07-09 12:17:24 +0100 | [diff] [blame] | 502 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | /* |
| 506 | * if this is compiled into the kernel, we need to ensure that the |
| 507 | * class is registered before users of the class try to register lcd's |
| 508 | */ |
| 509 | postcore_initcall(backlight_class_init); |
| 510 | module_exit(backlight_class_exit); |
| 511 | |
| 512 | MODULE_LICENSE("GPL"); |
| 513 | MODULE_AUTHOR("Jamey Hicks <jamey.hicks@hp.com>, Andrew Zabolotny <zap@homelink.ru>"); |
| 514 | MODULE_DESCRIPTION("Backlight Lowlevel Control Abstraction"); |