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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | #include <linux/module.h> |
| 9 | #include <linux/init.h> |
| 10 | #include <linux/device.h> |
| 11 | #include <linux/lcd.h> |
| 12 | #include <linux/notifier.h> |
| 13 | #include <linux/ctype.h> |
| 14 | #include <linux/err.h> |
| 15 | #include <linux/fb.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 16 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | |
James Simmons | 3d5eead | 2006-12-08 02:40:47 -0800 | [diff] [blame] | 18 | #if defined(CONFIG_FB) || (defined(CONFIG_FB_MODULE) && \ |
| 19 | defined(CONFIG_LCD_CLASS_DEVICE_MODULE)) |
| 20 | /* This callback gets called when something important happens inside a |
| 21 | * framebuffer driver. We're looking if that important event is blanking, |
| 22 | * and if it is, we're switching lcd power as well ... |
| 23 | */ |
| 24 | static int fb_notifier_callback(struct notifier_block *self, |
| 25 | unsigned long event, void *data) |
| 26 | { |
| 27 | struct lcd_device *ld; |
| 28 | struct fb_event *evdata = data; |
| 29 | |
| 30 | /* If we aren't interested in this event, skip it immediately ... */ |
Eric Miao | faa312d | 2008-08-29 04:18:43 +0800 | [diff] [blame] | 31 | switch (event) { |
| 32 | case FB_EVENT_BLANK: |
| 33 | case FB_EVENT_MODE_CHANGE: |
| 34 | case FB_EVENT_MODE_CHANGE_ALL: |
| 35 | break; |
| 36 | default: |
James Simmons | 3d5eead | 2006-12-08 02:40:47 -0800 | [diff] [blame] | 37 | return 0; |
Eric Miao | faa312d | 2008-08-29 04:18:43 +0800 | [diff] [blame] | 38 | } |
James Simmons | 3d5eead | 2006-12-08 02:40:47 -0800 | [diff] [blame] | 39 | |
| 40 | ld = container_of(self, struct lcd_device, fb_notif); |
Eric Miao | faa312d | 2008-08-29 04:18:43 +0800 | [diff] [blame] | 41 | if (!ld->ops) |
| 42 | return 0; |
| 43 | |
Richard Purdie | 599a52d | 2007-02-10 23:07:48 +0000 | [diff] [blame] | 44 | mutex_lock(&ld->ops_lock); |
Eric Miao | faa312d | 2008-08-29 04:18:43 +0800 | [diff] [blame] | 45 | if (!ld->ops->check_fb || ld->ops->check_fb(ld, evdata->info)) { |
Ben Dooks | b3b4dc8 | 2008-11-19 15:36:25 -0800 | [diff] [blame] | 46 | if (event == FB_EVENT_BLANK) { |
| 47 | if (ld->ops->set_power) |
| 48 | ld->ops->set_power(ld, *(int *)evdata->data); |
| 49 | } else { |
| 50 | if (ld->ops->set_mode) |
| 51 | ld->ops->set_mode(ld, evdata->data); |
| 52 | } |
Eric Miao | faa312d | 2008-08-29 04:18:43 +0800 | [diff] [blame] | 53 | } |
Richard Purdie | 599a52d | 2007-02-10 23:07:48 +0000 | [diff] [blame] | 54 | mutex_unlock(&ld->ops_lock); |
James Simmons | 3d5eead | 2006-12-08 02:40:47 -0800 | [diff] [blame] | 55 | return 0; |
| 56 | } |
| 57 | |
| 58 | static int lcd_register_fb(struct lcd_device *ld) |
| 59 | { |
Jean Delvare | 1e0fa6b | 2009-10-02 11:28:18 +0200 | [diff] [blame] | 60 | memset(&ld->fb_notif, 0, sizeof(ld->fb_notif)); |
James Simmons | 3d5eead | 2006-12-08 02:40:47 -0800 | [diff] [blame] | 61 | ld->fb_notif.notifier_call = fb_notifier_callback; |
| 62 | return fb_register_client(&ld->fb_notif); |
| 63 | } |
| 64 | |
| 65 | static void lcd_unregister_fb(struct lcd_device *ld) |
| 66 | { |
| 67 | fb_unregister_client(&ld->fb_notif); |
| 68 | } |
| 69 | #else |
| 70 | static int lcd_register_fb(struct lcd_device *ld) |
| 71 | { |
| 72 | return 0; |
| 73 | } |
| 74 | |
| 75 | static inline void lcd_unregister_fb(struct lcd_device *ld) |
| 76 | { |
| 77 | } |
| 78 | #endif /* CONFIG_FB */ |
| 79 | |
Richard Purdie | 655bfd7 | 2007-07-09 12:17:24 +0100 | [diff] [blame] | 80 | static ssize_t lcd_show_power(struct device *dev, struct device_attribute *attr, |
| 81 | char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | { |
| 83 | int rc; |
Richard Purdie | 655bfd7 | 2007-07-09 12:17:24 +0100 | [diff] [blame] | 84 | struct lcd_device *ld = to_lcd_device(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | |
Richard Purdie | 599a52d | 2007-02-10 23:07:48 +0000 | [diff] [blame] | 86 | mutex_lock(&ld->ops_lock); |
| 87 | if (ld->ops && ld->ops->get_power) |
| 88 | rc = sprintf(buf, "%d\n", ld->ops->get_power(ld)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | else |
| 90 | rc = -ENXIO; |
Richard Purdie | 599a52d | 2007-02-10 23:07:48 +0000 | [diff] [blame] | 91 | mutex_unlock(&ld->ops_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | |
| 93 | return rc; |
| 94 | } |
| 95 | |
Richard Purdie | 655bfd7 | 2007-07-09 12:17:24 +0100 | [diff] [blame] | 96 | static ssize_t lcd_store_power(struct device *dev, |
| 97 | struct device_attribute *attr, const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | { |
Richard Purdie | 68673af | 2006-05-15 09:44:15 -0700 | [diff] [blame] | 99 | int rc = -ENXIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | char *endp; |
Richard Purdie | 655bfd7 | 2007-07-09 12:17:24 +0100 | [diff] [blame] | 101 | struct lcd_device *ld = to_lcd_device(dev); |
Richard Purdie | 68673af | 2006-05-15 09:44:15 -0700 | [diff] [blame] | 102 | int power = simple_strtoul(buf, &endp, 0); |
| 103 | size_t size = endp - buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | |
André Goddard Rosa | e7d2860 | 2009-12-14 18:01:06 -0800 | [diff] [blame] | 105 | if (isspace(*endp)) |
Richard Purdie | 68673af | 2006-05-15 09:44:15 -0700 | [diff] [blame] | 106 | size++; |
| 107 | if (size != count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | return -EINVAL; |
| 109 | |
Richard Purdie | 599a52d | 2007-02-10 23:07:48 +0000 | [diff] [blame] | 110 | mutex_lock(&ld->ops_lock); |
| 111 | if (ld->ops && ld->ops->set_power) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | pr_debug("lcd: set power to %d\n", power); |
Richard Purdie | 599a52d | 2007-02-10 23:07:48 +0000 | [diff] [blame] | 113 | ld->ops->set_power(ld, power); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | rc = count; |
Richard Purdie | 68673af | 2006-05-15 09:44:15 -0700 | [diff] [blame] | 115 | } |
Richard Purdie | 599a52d | 2007-02-10 23:07:48 +0000 | [diff] [blame] | 116 | mutex_unlock(&ld->ops_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | |
| 118 | return rc; |
| 119 | } |
| 120 | |
Richard Purdie | 655bfd7 | 2007-07-09 12:17:24 +0100 | [diff] [blame] | 121 | static ssize_t lcd_show_contrast(struct device *dev, |
| 122 | struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | { |
Richard Purdie | 68673af | 2006-05-15 09:44:15 -0700 | [diff] [blame] | 124 | int rc = -ENXIO; |
Richard Purdie | 655bfd7 | 2007-07-09 12:17:24 +0100 | [diff] [blame] | 125 | struct lcd_device *ld = to_lcd_device(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | |
Richard Purdie | 599a52d | 2007-02-10 23:07:48 +0000 | [diff] [blame] | 127 | mutex_lock(&ld->ops_lock); |
| 128 | if (ld->ops && ld->ops->get_contrast) |
| 129 | rc = sprintf(buf, "%d\n", ld->ops->get_contrast(ld)); |
| 130 | mutex_unlock(&ld->ops_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | |
| 132 | return rc; |
| 133 | } |
| 134 | |
Richard Purdie | 655bfd7 | 2007-07-09 12:17:24 +0100 | [diff] [blame] | 135 | static ssize_t lcd_store_contrast(struct device *dev, |
| 136 | struct device_attribute *attr, const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | { |
Richard Purdie | 68673af | 2006-05-15 09:44:15 -0700 | [diff] [blame] | 138 | int rc = -ENXIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | char *endp; |
Richard Purdie | 655bfd7 | 2007-07-09 12:17:24 +0100 | [diff] [blame] | 140 | struct lcd_device *ld = to_lcd_device(dev); |
Richard Purdie | 68673af | 2006-05-15 09:44:15 -0700 | [diff] [blame] | 141 | int contrast = simple_strtoul(buf, &endp, 0); |
| 142 | size_t size = endp - buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | |
André Goddard Rosa | e7d2860 | 2009-12-14 18:01:06 -0800 | [diff] [blame] | 144 | if (isspace(*endp)) |
Richard Purdie | 68673af | 2006-05-15 09:44:15 -0700 | [diff] [blame] | 145 | size++; |
| 146 | if (size != count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | return -EINVAL; |
| 148 | |
Richard Purdie | 599a52d | 2007-02-10 23:07:48 +0000 | [diff] [blame] | 149 | mutex_lock(&ld->ops_lock); |
| 150 | if (ld->ops && ld->ops->set_contrast) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | pr_debug("lcd: set contrast to %d\n", contrast); |
Richard Purdie | 599a52d | 2007-02-10 23:07:48 +0000 | [diff] [blame] | 152 | ld->ops->set_contrast(ld, contrast); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | rc = count; |
Richard Purdie | 68673af | 2006-05-15 09:44:15 -0700 | [diff] [blame] | 154 | } |
Richard Purdie | 599a52d | 2007-02-10 23:07:48 +0000 | [diff] [blame] | 155 | mutex_unlock(&ld->ops_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | |
| 157 | return rc; |
| 158 | } |
| 159 | |
Richard Purdie | 655bfd7 | 2007-07-09 12:17:24 +0100 | [diff] [blame] | 160 | static ssize_t lcd_show_max_contrast(struct device *dev, |
| 161 | struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | { |
Richard Purdie | 655bfd7 | 2007-07-09 12:17:24 +0100 | [diff] [blame] | 163 | struct lcd_device *ld = to_lcd_device(dev); |
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 | return sprintf(buf, "%d\n", ld->props.max_contrast); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | } |
| 167 | |
Adrian Bunk | 0ad90ef | 2007-08-11 10:27:19 +0100 | [diff] [blame] | 168 | static struct class *lcd_class; |
Richard Purdie | 655bfd7 | 2007-07-09 12:17:24 +0100 | [diff] [blame] | 169 | |
| 170 | static void lcd_device_release(struct device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | { |
| 172 | struct lcd_device *ld = to_lcd_device(dev); |
| 173 | kfree(ld); |
| 174 | } |
| 175 | |
Richard Purdie | 655bfd7 | 2007-07-09 12:17:24 +0100 | [diff] [blame] | 176 | static struct device_attribute lcd_device_attributes[] = { |
| 177 | __ATTR(lcd_power, 0644, lcd_show_power, lcd_store_power), |
| 178 | __ATTR(contrast, 0644, lcd_show_contrast, lcd_store_contrast), |
| 179 | __ATTR(max_contrast, 0444, lcd_show_max_contrast, NULL), |
| 180 | __ATTR_NULL, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | }; |
| 182 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | /** |
| 184 | * lcd_device_register - register a new object of lcd_device class. |
| 185 | * @name: the name of the new object(must be the same as the name of the |
| 186 | * respective framebuffer device). |
Richard Purdie | 655bfd7 | 2007-07-09 12:17:24 +0100 | [diff] [blame] | 187 | * @devdata: an optional pointer to be stored in the device. The |
| 188 | * methods may retrieve it by using lcd_get_data(ld). |
Richard Purdie | 599a52d | 2007-02-10 23:07:48 +0000 | [diff] [blame] | 189 | * @ops: the lcd operations structure. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | * |
Richard Purdie | 655bfd7 | 2007-07-09 12:17:24 +0100 | [diff] [blame] | 191 | * Creates and registers a new lcd device. Returns either an ERR_PTR() |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | * or a pointer to the newly allocated device. |
| 193 | */ |
Richard Purdie | 655bfd7 | 2007-07-09 12:17:24 +0100 | [diff] [blame] | 194 | struct lcd_device *lcd_device_register(const char *name, struct device *parent, |
| 195 | void *devdata, struct lcd_ops *ops) |
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 | struct lcd_device *new_ld; |
Richard Purdie | 655bfd7 | 2007-07-09 12:17:24 +0100 | [diff] [blame] | 198 | int rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | |
| 200 | pr_debug("lcd_device_register: name=%s\n", name); |
| 201 | |
Richard Purdie | 599a52d | 2007-02-10 23:07:48 +0000 | [diff] [blame] | 202 | new_ld = kzalloc(sizeof(struct lcd_device), GFP_KERNEL); |
Dmitry Torokhov | 90968e8 | 2007-02-08 00:12:28 +0000 | [diff] [blame] | 203 | if (!new_ld) |
Jean Delvare | 10ad1b7 | 2006-03-09 17:33:36 -0800 | [diff] [blame] | 204 | return ERR_PTR(-ENOMEM); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | |
Richard Purdie | 599a52d | 2007-02-10 23:07:48 +0000 | [diff] [blame] | 206 | mutex_init(&new_ld->ops_lock); |
Richard Purdie | 28ee086 | 2007-02-08 22:25:09 +0000 | [diff] [blame] | 207 | mutex_init(&new_ld->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | |
Richard Purdie | 655bfd7 | 2007-07-09 12:17:24 +0100 | [diff] [blame] | 209 | new_ld->dev.class = lcd_class; |
| 210 | new_ld->dev.parent = parent; |
| 211 | new_ld->dev.release = lcd_device_release; |
Kay Sievers | 64dba9a | 2009-01-06 10:44:35 -0800 | [diff] [blame] | 212 | dev_set_name(&new_ld->dev, name); |
Richard Purdie | 655bfd7 | 2007-07-09 12:17:24 +0100 | [diff] [blame] | 213 | dev_set_drvdata(&new_ld->dev, devdata); |
| 214 | |
| 215 | rc = device_register(&new_ld->dev); |
Dmitry Torokhov | 90968e8 | 2007-02-08 00:12:28 +0000 | [diff] [blame] | 216 | if (rc) { |
Dmitry Torokhov | 2fd5a15 | 2007-02-07 22:25:50 +0000 | [diff] [blame] | 217 | kfree(new_ld); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | return ERR_PTR(rc); |
| 219 | } |
| 220 | |
James Simmons | 3d5eead | 2006-12-08 02:40:47 -0800 | [diff] [blame] | 221 | rc = lcd_register_fb(new_ld); |
Dmitry Torokhov | 2fd5a15 | 2007-02-07 22:25:50 +0000 | [diff] [blame] | 222 | if (rc) { |
Richard Purdie | 655bfd7 | 2007-07-09 12:17:24 +0100 | [diff] [blame] | 223 | device_unregister(&new_ld->dev); |
Dmitry Torokhov | 2fd5a15 | 2007-02-07 22:25:50 +0000 | [diff] [blame] | 224 | return ERR_PTR(rc); |
| 225 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | |
Richard Purdie | 655bfd7 | 2007-07-09 12:17:24 +0100 | [diff] [blame] | 227 | new_ld->ops = ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | |
| 229 | return new_ld; |
| 230 | } |
| 231 | EXPORT_SYMBOL(lcd_device_register); |
| 232 | |
| 233 | /** |
| 234 | * lcd_device_unregister - unregisters a object of lcd_device class. |
| 235 | * @ld: the lcd device object to be unregistered and freed. |
| 236 | * |
| 237 | * Unregisters a previously registered via lcd_device_register object. |
| 238 | */ |
| 239 | void lcd_device_unregister(struct lcd_device *ld) |
| 240 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | if (!ld) |
| 242 | return; |
| 243 | |
Richard Purdie | 599a52d | 2007-02-10 23:07:48 +0000 | [diff] [blame] | 244 | mutex_lock(&ld->ops_lock); |
| 245 | ld->ops = NULL; |
| 246 | mutex_unlock(&ld->ops_lock); |
James Simmons | 3d5eead | 2006-12-08 02:40:47 -0800 | [diff] [blame] | 247 | lcd_unregister_fb(ld); |
Richard Purdie | 655bfd7 | 2007-07-09 12:17:24 +0100 | [diff] [blame] | 248 | |
| 249 | device_unregister(&ld->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | } |
| 251 | EXPORT_SYMBOL(lcd_device_unregister); |
| 252 | |
| 253 | static void __exit lcd_class_exit(void) |
| 254 | { |
Richard Purdie | 655bfd7 | 2007-07-09 12:17:24 +0100 | [diff] [blame] | 255 | class_destroy(lcd_class); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | static int __init lcd_class_init(void) |
| 259 | { |
Richard Purdie | 655bfd7 | 2007-07-09 12:17:24 +0100 | [diff] [blame] | 260 | lcd_class = class_create(THIS_MODULE, "lcd"); |
| 261 | if (IS_ERR(lcd_class)) { |
| 262 | printk(KERN_WARNING "Unable to create backlight class; errno = %ld\n", |
| 263 | PTR_ERR(lcd_class)); |
| 264 | return PTR_ERR(lcd_class); |
| 265 | } |
| 266 | |
| 267 | lcd_class->dev_attrs = lcd_device_attributes; |
| 268 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | /* |
| 272 | * if this is compiled into the kernel, we need to ensure that the |
| 273 | * class is registered before users of the class try to register lcd's |
| 274 | */ |
| 275 | postcore_initcall(lcd_class_init); |
| 276 | module_exit(lcd_class_exit); |
| 277 | |
| 278 | MODULE_LICENSE("GPL"); |
| 279 | MODULE_AUTHOR("Jamey Hicks <jamey.hicks@hp.com>, Andrew Zabolotny <zap@homelink.ru>"); |
| 280 | MODULE_DESCRIPTION("LCD Lowlevel Control Abstraction"); |