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