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