1 /* 2 * LCD Lowlevel Control Abstraction 3 * 4 * Copyright (C) 2003,2004 Hewlett-Packard Company 5 * 6 */ 7 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> 16 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 switch (event) { 31 case FB_EVENT_BLANK: 32 case FB_EVENT_MODE_CHANGE: 33 case FB_EVENT_MODE_CHANGE_ALL: 34 break; 35 default: 36 return 0; 37 } 38 39 ld = container_of(self, struct lcd_device, fb_notif); 40 if (!ld->ops) 41 return 0; 42 43 if (!lock_fb_info(evdata->info)) 44 return -ENODEV; 45 mutex_lock(&ld->ops_lock); 46 if (!ld->ops->check_fb || ld->ops->check_fb(ld, evdata->info)) { 47 if (event == FB_EVENT_BLANK) { 48 if (ld->ops->set_power) 49 ld->ops->set_power(ld, *(int *)evdata->data); 50 } else { 51 if (ld->ops->set_mode) 52 ld->ops->set_mode(ld, evdata->data); 53 } 54 } 55 mutex_unlock(&ld->ops_lock); 56 unlock_fb_info(evdata->info); 57 return 0; 58 } 59 60 static int lcd_register_fb(struct lcd_device *ld) 61 { 62 memset(&ld->fb_notif, 0, sizeof(&ld->fb_notif)); 63 ld->fb_notif.notifier_call = fb_notifier_callback; 64 return fb_register_client(&ld->fb_notif); 65 } 66 67 static void lcd_unregister_fb(struct lcd_device *ld) 68 { 69 fb_unregister_client(&ld->fb_notif); 70 } 71 #else 72 static int lcd_register_fb(struct lcd_device *ld) 73 { 74 return 0; 75 } 76 77 static inline void lcd_unregister_fb(struct lcd_device *ld) 78 { 79 } 80 #endif /* CONFIG_FB */ 81 82 static ssize_t lcd_show_power(struct device *dev, struct device_attribute *attr, 83 char *buf) 84 { 85 int rc; 86 struct lcd_device *ld = to_lcd_device(dev); 87 88 mutex_lock(&ld->ops_lock); 89 if (ld->ops && ld->ops->get_power) 90 rc = sprintf(buf, "%d\n", ld->ops->get_power(ld)); 91 else 92 rc = -ENXIO; 93 mutex_unlock(&ld->ops_lock); 94 95 return rc; 96 } 97 98 static ssize_t lcd_store_power(struct device *dev, 99 struct device_attribute *attr, const char *buf, size_t count) 100 { 101 int rc = -ENXIO; 102 char *endp; 103 struct lcd_device *ld = to_lcd_device(dev); 104 int power = simple_strtoul(buf, &endp, 0); 105 size_t size = endp - buf; 106 107 if (*endp && isspace(*endp)) 108 size++; 109 if (size != count) 110 return -EINVAL; 111 112 mutex_lock(&ld->ops_lock); 113 if (ld->ops && ld->ops->set_power) { 114 pr_debug("lcd: set power to %d\n", power); 115 ld->ops->set_power(ld, power); 116 rc = count; 117 } 118 mutex_unlock(&ld->ops_lock); 119 120 return rc; 121 } 122 123 static ssize_t lcd_show_contrast(struct device *dev, 124 struct device_attribute *attr, char *buf) 125 { 126 int rc = -ENXIO; 127 struct lcd_device *ld = to_lcd_device(dev); 128 129 mutex_lock(&ld->ops_lock); 130 if (ld->ops && ld->ops->get_contrast) 131 rc = sprintf(buf, "%d\n", ld->ops->get_contrast(ld)); 132 mutex_unlock(&ld->ops_lock); 133 134 return rc; 135 } 136 137 static ssize_t lcd_store_contrast(struct device *dev, 138 struct device_attribute *attr, const char *buf, size_t count) 139 { 140 int rc = -ENXIO; 141 char *endp; 142 struct lcd_device *ld = to_lcd_device(dev); 143 int contrast = simple_strtoul(buf, &endp, 0); 144 size_t size = endp - buf; 145 146 if (*endp && isspace(*endp)) 147 size++; 148 if (size != count) 149 return -EINVAL; 150 151 mutex_lock(&ld->ops_lock); 152 if (ld->ops && ld->ops->set_contrast) { 153 pr_debug("lcd: set contrast to %d\n", contrast); 154 ld->ops->set_contrast(ld, contrast); 155 rc = count; 156 } 157 mutex_unlock(&ld->ops_lock); 158 159 return rc; 160 } 161 162 static ssize_t lcd_show_max_contrast(struct device *dev, 163 struct device_attribute *attr, char *buf) 164 { 165 struct lcd_device *ld = to_lcd_device(dev); 166 167 return sprintf(buf, "%d\n", ld->props.max_contrast); 168 } 169 170 static struct class *lcd_class; 171 172 static void lcd_device_release(struct device *dev) 173 { 174 struct lcd_device *ld = to_lcd_device(dev); 175 kfree(ld); 176 } 177 178 static struct device_attribute lcd_device_attributes[] = { 179 __ATTR(lcd_power, 0644, lcd_show_power, lcd_store_power), 180 __ATTR(contrast, 0644, lcd_show_contrast, lcd_store_contrast), 181 __ATTR(max_contrast, 0444, lcd_show_max_contrast, NULL), 182 __ATTR_NULL, 183 }; 184 185 /** 186 * lcd_device_register - register a new object of lcd_device class. 187 * @name: the name of the new object(must be the same as the name of the 188 * respective framebuffer device). 189 * @devdata: an optional pointer to be stored in the device. The 190 * methods may retrieve it by using lcd_get_data(ld). 191 * @ops: the lcd operations structure. 192 * 193 * Creates and registers a new lcd device. Returns either an ERR_PTR() 194 * or a pointer to the newly allocated device. 195 */ 196 struct lcd_device *lcd_device_register(const char *name, struct device *parent, 197 void *devdata, struct lcd_ops *ops) 198 { 199 struct lcd_device *new_ld; 200 int rc; 201 202 pr_debug("lcd_device_register: name=%s\n", name); 203 204 new_ld = kzalloc(sizeof(struct lcd_device), GFP_KERNEL); 205 if (!new_ld) 206 return ERR_PTR(-ENOMEM); 207 208 mutex_init(&new_ld->ops_lock); 209 mutex_init(&new_ld->update_lock); 210 211 new_ld->dev.class = lcd_class; 212 new_ld->dev.parent = parent; 213 new_ld->dev.release = lcd_device_release; 214 dev_set_name(&new_ld->dev, name); 215 dev_set_drvdata(&new_ld->dev, devdata); 216 217 rc = device_register(&new_ld->dev); 218 if (rc) { 219 kfree(new_ld); 220 return ERR_PTR(rc); 221 } 222 223 rc = lcd_register_fb(new_ld); 224 if (rc) { 225 device_unregister(&new_ld->dev); 226 return ERR_PTR(rc); 227 } 228 229 new_ld->ops = ops; 230 231 return new_ld; 232 } 233 EXPORT_SYMBOL(lcd_device_register); 234 235 /** 236 * lcd_device_unregister - unregisters a object of lcd_device class. 237 * @ld: the lcd device object to be unregistered and freed. 238 * 239 * Unregisters a previously registered via lcd_device_register object. 240 */ 241 void lcd_device_unregister(struct lcd_device *ld) 242 { 243 if (!ld) 244 return; 245 246 mutex_lock(&ld->ops_lock); 247 ld->ops = NULL; 248 mutex_unlock(&ld->ops_lock); 249 lcd_unregister_fb(ld); 250 251 device_unregister(&ld->dev); 252 } 253 EXPORT_SYMBOL(lcd_device_unregister); 254 255 static void __exit lcd_class_exit(void) 256 { 257 class_destroy(lcd_class); 258 } 259 260 static int __init lcd_class_init(void) 261 { 262 lcd_class = class_create(THIS_MODULE, "lcd"); 263 if (IS_ERR(lcd_class)) { 264 printk(KERN_WARNING "Unable to create backlight class; errno = %ld\n", 265 PTR_ERR(lcd_class)); 266 return PTR_ERR(lcd_class); 267 } 268 269 lcd_class->dev_attrs = lcd_device_attributes; 270 return 0; 271 } 272 273 /* 274 * if this is compiled into the kernel, we need to ensure that the 275 * class is registered before users of the class try to register lcd's 276 */ 277 postcore_initcall(lcd_class_init); 278 module_exit(lcd_class_exit); 279 280 MODULE_LICENSE("GPL"); 281 MODULE_AUTHOR("Jamey Hicks <jamey.hicks@hp.com>, Andrew Zabolotny <zap@homelink.ru>"); 282 MODULE_DESCRIPTION("LCD Lowlevel Control Abstraction"); 283