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