Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * LCD 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/lcd.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 | |
James Simmons | 3d5eead | 2006-12-08 02:40:47 -0800 | [diff] [blame] | 20 | #if defined(CONFIG_FB) || (defined(CONFIG_FB_MODULE) && \ |
| 21 | defined(CONFIG_LCD_CLASS_DEVICE_MODULE)) |
| 22 | /* This callback gets called when something important happens inside a |
| 23 | * framebuffer driver. We're looking if that important event is blanking, |
| 24 | * and if it is, we're switching lcd power as well ... |
| 25 | */ |
| 26 | static int fb_notifier_callback(struct notifier_block *self, |
| 27 | unsigned long event, void *data) |
| 28 | { |
| 29 | struct lcd_device *ld; |
| 30 | struct fb_event *evdata = data; |
| 31 | |
| 32 | /* If we aren't interested in this event, skip it immediately ... */ |
Eric Miao | faa312d | 2008-08-29 04:18:43 +0800 | [diff] [blame] | 33 | switch (event) { |
| 34 | case FB_EVENT_BLANK: |
| 35 | case FB_EVENT_MODE_CHANGE: |
| 36 | case FB_EVENT_MODE_CHANGE_ALL: |
Inki Dae | d54ad83 | 2012-05-29 15:07:13 -0700 | [diff] [blame] | 37 | case FB_EARLY_EVENT_BLANK: |
| 38 | case FB_R_EARLY_EVENT_BLANK: |
Eric Miao | faa312d | 2008-08-29 04:18:43 +0800 | [diff] [blame] | 39 | break; |
| 40 | default: |
James Simmons | 3d5eead | 2006-12-08 02:40:47 -0800 | [diff] [blame] | 41 | return 0; |
Eric Miao | faa312d | 2008-08-29 04:18:43 +0800 | [diff] [blame] | 42 | } |
James Simmons | 3d5eead | 2006-12-08 02:40:47 -0800 | [diff] [blame] | 43 | |
| 44 | ld = container_of(self, struct lcd_device, fb_notif); |
Eric Miao | faa312d | 2008-08-29 04:18:43 +0800 | [diff] [blame] | 45 | if (!ld->ops) |
| 46 | return 0; |
| 47 | |
Richard Purdie | 599a52d | 2007-02-10 23:07:48 +0000 | [diff] [blame] | 48 | mutex_lock(&ld->ops_lock); |
Eric Miao | faa312d | 2008-08-29 04:18:43 +0800 | [diff] [blame] | 49 | if (!ld->ops->check_fb || ld->ops->check_fb(ld, evdata->info)) { |
Ben Dooks | b3b4dc8 | 2008-11-19 15:36:25 -0800 | [diff] [blame] | 50 | if (event == FB_EVENT_BLANK) { |
| 51 | if (ld->ops->set_power) |
| 52 | ld->ops->set_power(ld, *(int *)evdata->data); |
Inki Dae | d54ad83 | 2012-05-29 15:07:13 -0700 | [diff] [blame] | 53 | } else if (event == FB_EARLY_EVENT_BLANK) { |
| 54 | if (ld->ops->early_set_power) |
| 55 | ld->ops->early_set_power(ld, |
| 56 | *(int *)evdata->data); |
| 57 | } else if (event == FB_R_EARLY_EVENT_BLANK) { |
| 58 | if (ld->ops->r_early_set_power) |
| 59 | ld->ops->r_early_set_power(ld, |
| 60 | *(int *)evdata->data); |
Ben Dooks | b3b4dc8 | 2008-11-19 15:36:25 -0800 | [diff] [blame] | 61 | } else { |
| 62 | if (ld->ops->set_mode) |
| 63 | ld->ops->set_mode(ld, evdata->data); |
| 64 | } |
Eric Miao | faa312d | 2008-08-29 04:18:43 +0800 | [diff] [blame] | 65 | } |
Richard Purdie | 599a52d | 2007-02-10 23:07:48 +0000 | [diff] [blame] | 66 | mutex_unlock(&ld->ops_lock); |
James Simmons | 3d5eead | 2006-12-08 02:40:47 -0800 | [diff] [blame] | 67 | return 0; |
| 68 | } |
| 69 | |
| 70 | static int lcd_register_fb(struct lcd_device *ld) |
| 71 | { |
Jean Delvare | 1e0fa6b | 2009-10-02 11:28:18 +0200 | [diff] [blame] | 72 | memset(&ld->fb_notif, 0, sizeof(ld->fb_notif)); |
James Simmons | 3d5eead | 2006-12-08 02:40:47 -0800 | [diff] [blame] | 73 | ld->fb_notif.notifier_call = fb_notifier_callback; |
| 74 | return fb_register_client(&ld->fb_notif); |
| 75 | } |
| 76 | |
| 77 | static void lcd_unregister_fb(struct lcd_device *ld) |
| 78 | { |
| 79 | fb_unregister_client(&ld->fb_notif); |
| 80 | } |
| 81 | #else |
| 82 | static int lcd_register_fb(struct lcd_device *ld) |
| 83 | { |
| 84 | return 0; |
| 85 | } |
| 86 | |
| 87 | static inline void lcd_unregister_fb(struct lcd_device *ld) |
| 88 | { |
| 89 | } |
| 90 | #endif /* CONFIG_FB */ |
| 91 | |
Greg Kroah-Hartman | d79fd03 | 2013-07-24 15:05:31 -0700 | [diff] [blame] | 92 | static ssize_t lcd_power_show(struct device *dev, struct device_attribute *attr, |
Richard Purdie | 655bfd7 | 2007-07-09 12:17:24 +0100 | [diff] [blame] | 93 | char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | { |
| 95 | int rc; |
Richard Purdie | 655bfd7 | 2007-07-09 12:17:24 +0100 | [diff] [blame] | 96 | struct lcd_device *ld = to_lcd_device(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | |
Richard Purdie | 599a52d | 2007-02-10 23:07:48 +0000 | [diff] [blame] | 98 | mutex_lock(&ld->ops_lock); |
| 99 | if (ld->ops && ld->ops->get_power) |
| 100 | rc = sprintf(buf, "%d\n", ld->ops->get_power(ld)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | else |
| 102 | rc = -ENXIO; |
Richard Purdie | 599a52d | 2007-02-10 23:07:48 +0000 | [diff] [blame] | 103 | mutex_unlock(&ld->ops_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | |
| 105 | return rc; |
| 106 | } |
| 107 | |
Greg Kroah-Hartman | d79fd03 | 2013-07-24 15:05:31 -0700 | [diff] [blame] | 108 | static ssize_t lcd_power_store(struct device *dev, |
Richard Purdie | 655bfd7 | 2007-07-09 12:17:24 +0100 | [diff] [blame] | 109 | struct device_attribute *attr, const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | { |
Jingoo Han | 424e06e | 2012-12-17 16:01:10 -0800 | [diff] [blame] | 111 | int rc; |
Richard Purdie | 655bfd7 | 2007-07-09 12:17:24 +0100 | [diff] [blame] | 112 | struct lcd_device *ld = to_lcd_device(dev); |
Jingoo Han | 6665576 | 2012-01-10 15:09:19 -0800 | [diff] [blame] | 113 | unsigned long power; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | |
Jingoo Han | 6665576 | 2012-01-10 15:09:19 -0800 | [diff] [blame] | 115 | rc = kstrtoul(buf, 0, &power); |
| 116 | if (rc) |
| 117 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | |
Jingoo Han | 424e06e | 2012-12-17 16:01:10 -0800 | [diff] [blame] | 119 | rc = -ENXIO; |
| 120 | |
Richard Purdie | 599a52d | 2007-02-10 23:07:48 +0000 | [diff] [blame] | 121 | mutex_lock(&ld->ops_lock); |
| 122 | if (ld->ops && ld->ops->set_power) { |
Jingoo Han | 35f9616 | 2012-05-29 15:07:16 -0700 | [diff] [blame] | 123 | pr_debug("set power to %lu\n", power); |
Richard Purdie | 599a52d | 2007-02-10 23:07:48 +0000 | [diff] [blame] | 124 | ld->ops->set_power(ld, power); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | rc = count; |
Richard Purdie | 68673af | 2006-05-15 09:44:15 -0700 | [diff] [blame] | 126 | } |
Richard Purdie | 599a52d | 2007-02-10 23:07:48 +0000 | [diff] [blame] | 127 | mutex_unlock(&ld->ops_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | |
| 129 | return rc; |
| 130 | } |
Greg Kroah-Hartman | d79fd03 | 2013-07-24 15:05:31 -0700 | [diff] [blame] | 131 | static DEVICE_ATTR_RW(lcd_power); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | |
Greg Kroah-Hartman | d79fd03 | 2013-07-24 15:05:31 -0700 | [diff] [blame] | 133 | static ssize_t contrast_show(struct device *dev, |
Richard Purdie | 655bfd7 | 2007-07-09 12:17:24 +0100 | [diff] [blame] | 134 | struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | { |
Richard Purdie | 68673af | 2006-05-15 09:44:15 -0700 | [diff] [blame] | 136 | int rc = -ENXIO; |
Richard Purdie | 655bfd7 | 2007-07-09 12:17:24 +0100 | [diff] [blame] | 137 | struct lcd_device *ld = to_lcd_device(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | |
Richard Purdie | 599a52d | 2007-02-10 23:07:48 +0000 | [diff] [blame] | 139 | mutex_lock(&ld->ops_lock); |
| 140 | if (ld->ops && ld->ops->get_contrast) |
| 141 | rc = sprintf(buf, "%d\n", ld->ops->get_contrast(ld)); |
| 142 | mutex_unlock(&ld->ops_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | |
| 144 | return rc; |
| 145 | } |
| 146 | |
Greg Kroah-Hartman | d79fd03 | 2013-07-24 15:05:31 -0700 | [diff] [blame] | 147 | static ssize_t contrast_store(struct device *dev, |
Richard Purdie | 655bfd7 | 2007-07-09 12:17:24 +0100 | [diff] [blame] | 148 | struct device_attribute *attr, const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | { |
Jingoo Han | 424e06e | 2012-12-17 16:01:10 -0800 | [diff] [blame] | 150 | int rc; |
Richard Purdie | 655bfd7 | 2007-07-09 12:17:24 +0100 | [diff] [blame] | 151 | struct lcd_device *ld = to_lcd_device(dev); |
Jingoo Han | 6665576 | 2012-01-10 15:09:19 -0800 | [diff] [blame] | 152 | unsigned long contrast; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | |
Jingoo Han | 6665576 | 2012-01-10 15:09:19 -0800 | [diff] [blame] | 154 | rc = kstrtoul(buf, 0, &contrast); |
| 155 | if (rc) |
| 156 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | |
Jingoo Han | 424e06e | 2012-12-17 16:01:10 -0800 | [diff] [blame] | 158 | rc = -ENXIO; |
| 159 | |
Richard Purdie | 599a52d | 2007-02-10 23:07:48 +0000 | [diff] [blame] | 160 | mutex_lock(&ld->ops_lock); |
| 161 | if (ld->ops && ld->ops->set_contrast) { |
Jingoo Han | 35f9616 | 2012-05-29 15:07:16 -0700 | [diff] [blame] | 162 | pr_debug("set contrast to %lu\n", contrast); |
Richard Purdie | 599a52d | 2007-02-10 23:07:48 +0000 | [diff] [blame] | 163 | ld->ops->set_contrast(ld, contrast); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | rc = count; |
Richard Purdie | 68673af | 2006-05-15 09:44:15 -0700 | [diff] [blame] | 165 | } |
Richard Purdie | 599a52d | 2007-02-10 23:07:48 +0000 | [diff] [blame] | 166 | mutex_unlock(&ld->ops_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | |
| 168 | return rc; |
| 169 | } |
Greg Kroah-Hartman | d79fd03 | 2013-07-24 15:05:31 -0700 | [diff] [blame] | 170 | static DEVICE_ATTR_RW(contrast); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | |
Greg Kroah-Hartman | d79fd03 | 2013-07-24 15:05:31 -0700 | [diff] [blame] | 172 | static ssize_t max_contrast_show(struct device *dev, |
Richard Purdie | 655bfd7 | 2007-07-09 12:17:24 +0100 | [diff] [blame] | 173 | struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | { |
Richard Purdie | 655bfd7 | 2007-07-09 12:17:24 +0100 | [diff] [blame] | 175 | struct lcd_device *ld = to_lcd_device(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | |
Richard Purdie | 599a52d | 2007-02-10 23:07:48 +0000 | [diff] [blame] | 177 | return sprintf(buf, "%d\n", ld->props.max_contrast); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | } |
Greg Kroah-Hartman | d79fd03 | 2013-07-24 15:05:31 -0700 | [diff] [blame] | 179 | static DEVICE_ATTR_RO(max_contrast); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | |
Adrian Bunk | 0ad90ef | 2007-08-11 10:27:19 +0100 | [diff] [blame] | 181 | static struct class *lcd_class; |
Richard Purdie | 655bfd7 | 2007-07-09 12:17:24 +0100 | [diff] [blame] | 182 | |
| 183 | static void lcd_device_release(struct device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | { |
| 185 | struct lcd_device *ld = to_lcd_device(dev); |
| 186 | kfree(ld); |
| 187 | } |
| 188 | |
Greg Kroah-Hartman | d79fd03 | 2013-07-24 15:05:31 -0700 | [diff] [blame] | 189 | static struct attribute *lcd_device_attrs[] = { |
| 190 | &dev_attr_lcd_power.attr, |
| 191 | &dev_attr_contrast.attr, |
| 192 | &dev_attr_max_contrast.attr, |
| 193 | NULL, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | }; |
Greg Kroah-Hartman | d79fd03 | 2013-07-24 15:05:31 -0700 | [diff] [blame] | 195 | ATTRIBUTE_GROUPS(lcd_device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | /** |
| 198 | * lcd_device_register - register a new object of lcd_device class. |
| 199 | * @name: the name of the new object(must be the same as the name of the |
| 200 | * respective framebuffer device). |
Richard Purdie | 655bfd7 | 2007-07-09 12:17:24 +0100 | [diff] [blame] | 201 | * @devdata: an optional pointer to be stored in the device. The |
| 202 | * methods may retrieve it by using lcd_get_data(ld). |
Richard Purdie | 599a52d | 2007-02-10 23:07:48 +0000 | [diff] [blame] | 203 | * @ops: the lcd operations structure. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | * |
Richard Purdie | 655bfd7 | 2007-07-09 12:17:24 +0100 | [diff] [blame] | 205 | * Creates and registers a new lcd device. Returns either an ERR_PTR() |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | * or a pointer to the newly allocated device. |
| 207 | */ |
Richard Purdie | 655bfd7 | 2007-07-09 12:17:24 +0100 | [diff] [blame] | 208 | struct lcd_device *lcd_device_register(const char *name, struct device *parent, |
| 209 | void *devdata, struct lcd_ops *ops) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | struct lcd_device *new_ld; |
Richard Purdie | 655bfd7 | 2007-07-09 12:17:24 +0100 | [diff] [blame] | 212 | int rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | |
| 214 | pr_debug("lcd_device_register: name=%s\n", name); |
| 215 | |
Richard Purdie | 599a52d | 2007-02-10 23:07:48 +0000 | [diff] [blame] | 216 | new_ld = kzalloc(sizeof(struct lcd_device), GFP_KERNEL); |
Dmitry Torokhov | 90968e8 | 2007-02-08 00:12:28 +0000 | [diff] [blame] | 217 | if (!new_ld) |
Jean Delvare | 10ad1b7 | 2006-03-09 17:33:36 -0800 | [diff] [blame] | 218 | return ERR_PTR(-ENOMEM); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | |
Richard Purdie | 599a52d | 2007-02-10 23:07:48 +0000 | [diff] [blame] | 220 | mutex_init(&new_ld->ops_lock); |
Richard Purdie | 28ee086 | 2007-02-08 22:25:09 +0000 | [diff] [blame] | 221 | mutex_init(&new_ld->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | |
Richard Purdie | 655bfd7 | 2007-07-09 12:17:24 +0100 | [diff] [blame] | 223 | new_ld->dev.class = lcd_class; |
| 224 | new_ld->dev.parent = parent; |
| 225 | new_ld->dev.release = lcd_device_release; |
Kees Cook | 02aa2a3 | 2013-07-03 15:04:56 -0700 | [diff] [blame] | 226 | dev_set_name(&new_ld->dev, "%s", name); |
Richard Purdie | 655bfd7 | 2007-07-09 12:17:24 +0100 | [diff] [blame] | 227 | dev_set_drvdata(&new_ld->dev, devdata); |
| 228 | |
| 229 | rc = device_register(&new_ld->dev); |
Dmitry Torokhov | 90968e8 | 2007-02-08 00:12:28 +0000 | [diff] [blame] | 230 | if (rc) { |
Dmitry Torokhov | 2fd5a15 | 2007-02-07 22:25:50 +0000 | [diff] [blame] | 231 | kfree(new_ld); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | return ERR_PTR(rc); |
| 233 | } |
| 234 | |
James Simmons | 3d5eead | 2006-12-08 02:40:47 -0800 | [diff] [blame] | 235 | rc = lcd_register_fb(new_ld); |
Dmitry Torokhov | 2fd5a15 | 2007-02-07 22:25:50 +0000 | [diff] [blame] | 236 | if (rc) { |
Richard Purdie | 655bfd7 | 2007-07-09 12:17:24 +0100 | [diff] [blame] | 237 | device_unregister(&new_ld->dev); |
Dmitry Torokhov | 2fd5a15 | 2007-02-07 22:25:50 +0000 | [diff] [blame] | 238 | return ERR_PTR(rc); |
| 239 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | |
Richard Purdie | 655bfd7 | 2007-07-09 12:17:24 +0100 | [diff] [blame] | 241 | new_ld->ops = ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | |
| 243 | return new_ld; |
| 244 | } |
| 245 | EXPORT_SYMBOL(lcd_device_register); |
| 246 | |
| 247 | /** |
| 248 | * lcd_device_unregister - unregisters a object of lcd_device class. |
| 249 | * @ld: the lcd device object to be unregistered and freed. |
| 250 | * |
| 251 | * Unregisters a previously registered via lcd_device_register object. |
| 252 | */ |
| 253 | void lcd_device_unregister(struct lcd_device *ld) |
| 254 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | if (!ld) |
| 256 | return; |
| 257 | |
Richard Purdie | 599a52d | 2007-02-10 23:07:48 +0000 | [diff] [blame] | 258 | mutex_lock(&ld->ops_lock); |
| 259 | ld->ops = NULL; |
| 260 | mutex_unlock(&ld->ops_lock); |
James Simmons | 3d5eead | 2006-12-08 02:40:47 -0800 | [diff] [blame] | 261 | lcd_unregister_fb(ld); |
Richard Purdie | 655bfd7 | 2007-07-09 12:17:24 +0100 | [diff] [blame] | 262 | |
| 263 | device_unregister(&ld->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | } |
| 265 | EXPORT_SYMBOL(lcd_device_unregister); |
| 266 | |
Jingoo Han | 1d0c48e | 2013-07-03 15:05:14 -0700 | [diff] [blame] | 267 | static void devm_lcd_device_release(struct device *dev, void *res) |
| 268 | { |
| 269 | struct lcd_device *lcd = *(struct lcd_device **)res; |
| 270 | |
| 271 | lcd_device_unregister(lcd); |
| 272 | } |
| 273 | |
| 274 | static int devm_lcd_device_match(struct device *dev, void *res, void *data) |
| 275 | { |
| 276 | struct lcd_device **r = res; |
| 277 | |
| 278 | return *r == data; |
| 279 | } |
| 280 | |
| 281 | /** |
| 282 | * devm_lcd_device_register - resource managed lcd_device_register() |
| 283 | * @dev: the device to register |
| 284 | * @name: the name of the device |
| 285 | * @parent: a pointer to the parent device |
| 286 | * @devdata: an optional pointer to be stored for private driver use |
| 287 | * @ops: the lcd operations structure |
| 288 | * |
| 289 | * @return a struct lcd on success, or an ERR_PTR on error |
| 290 | * |
| 291 | * Managed lcd_device_register(). The lcd_device returned from this function |
| 292 | * are automatically freed on driver detach. See lcd_device_register() |
| 293 | * for more information. |
| 294 | */ |
| 295 | struct lcd_device *devm_lcd_device_register(struct device *dev, |
| 296 | const char *name, struct device *parent, |
| 297 | void *devdata, struct lcd_ops *ops) |
| 298 | { |
| 299 | struct lcd_device **ptr, *lcd; |
| 300 | |
| 301 | ptr = devres_alloc(devm_lcd_device_release, sizeof(*ptr), GFP_KERNEL); |
| 302 | if (!ptr) |
| 303 | return ERR_PTR(-ENOMEM); |
| 304 | |
| 305 | lcd = lcd_device_register(name, parent, devdata, ops); |
| 306 | if (!IS_ERR(lcd)) { |
| 307 | *ptr = lcd; |
| 308 | devres_add(dev, ptr); |
| 309 | } else { |
| 310 | devres_free(ptr); |
| 311 | } |
| 312 | |
| 313 | return lcd; |
| 314 | } |
| 315 | EXPORT_SYMBOL(devm_lcd_device_register); |
| 316 | |
| 317 | /** |
| 318 | * devm_lcd_device_unregister - resource managed lcd_device_unregister() |
| 319 | * @dev: the device to unregister |
| 320 | * @ld: the lcd device to unregister |
| 321 | * |
| 322 | * Deallocated a lcd allocated with devm_lcd_device_register(). Normally |
| 323 | * this function will not need to be called and the resource management |
| 324 | * code will ensure that the resource is freed. |
| 325 | */ |
| 326 | void devm_lcd_device_unregister(struct device *dev, struct lcd_device *ld) |
| 327 | { |
| 328 | int rc; |
| 329 | |
| 330 | rc = devres_release(dev, devm_lcd_device_release, |
| 331 | devm_lcd_device_match, ld); |
| 332 | WARN_ON(rc); |
| 333 | } |
| 334 | EXPORT_SYMBOL(devm_lcd_device_unregister); |
| 335 | |
| 336 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | static void __exit lcd_class_exit(void) |
| 338 | { |
Richard Purdie | 655bfd7 | 2007-07-09 12:17:24 +0100 | [diff] [blame] | 339 | class_destroy(lcd_class); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | static int __init lcd_class_init(void) |
| 343 | { |
Richard Purdie | 655bfd7 | 2007-07-09 12:17:24 +0100 | [diff] [blame] | 344 | lcd_class = class_create(THIS_MODULE, "lcd"); |
| 345 | if (IS_ERR(lcd_class)) { |
Jingoo Han | 35f9616 | 2012-05-29 15:07:16 -0700 | [diff] [blame] | 346 | pr_warn("Unable to create backlight class; errno = %ld\n", |
| 347 | PTR_ERR(lcd_class)); |
Richard Purdie | 655bfd7 | 2007-07-09 12:17:24 +0100 | [diff] [blame] | 348 | return PTR_ERR(lcd_class); |
| 349 | } |
| 350 | |
Greg Kroah-Hartman | d79fd03 | 2013-07-24 15:05:31 -0700 | [diff] [blame] | 351 | lcd_class->dev_groups = lcd_device_groups; |
Richard Purdie | 655bfd7 | 2007-07-09 12:17:24 +0100 | [diff] [blame] | 352 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | /* |
| 356 | * if this is compiled into the kernel, we need to ensure that the |
| 357 | * class is registered before users of the class try to register lcd's |
| 358 | */ |
| 359 | postcore_initcall(lcd_class_init); |
| 360 | module_exit(lcd_class_exit); |
| 361 | |
| 362 | MODULE_LICENSE("GPL"); |
| 363 | MODULE_AUTHOR("Jamey Hicks <jamey.hicks@hp.com>, Andrew Zabolotny <zap@homelink.ru>"); |
| 364 | MODULE_DESCRIPTION("LCD Lowlevel Control Abstraction"); |