1 /* 2 * Backlight 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/backlight.h> 12 #include <linux/notifier.h> 13 #include <linux/ctype.h> 14 #include <linux/err.h> 15 #include <linux/fb.h> 16 #include <asm/bug.h> 17 18 static ssize_t backlight_show_power(struct class_device *cdev, char *buf) 19 { 20 int rc; 21 struct backlight_device *bd = to_backlight_device(cdev); 22 23 down(&bd->sem); 24 if (likely(bd->props && bd->props->get_power)) 25 rc = sprintf(buf, "%d\n", bd->props->get_power(bd)); 26 else 27 rc = -ENXIO; 28 up(&bd->sem); 29 30 return rc; 31 } 32 33 static ssize_t backlight_store_power(struct class_device *cdev, const char *buf, size_t count) 34 { 35 int rc, power; 36 char *endp; 37 struct backlight_device *bd = to_backlight_device(cdev); 38 39 power = simple_strtoul(buf, &endp, 0); 40 if (*endp && !isspace(*endp)) 41 return -EINVAL; 42 43 down(&bd->sem); 44 if (likely(bd->props && bd->props->set_power)) { 45 pr_debug("backlight: set power to %d\n", power); 46 bd->props->set_power(bd, power); 47 rc = count; 48 } else 49 rc = -ENXIO; 50 up(&bd->sem); 51 52 return rc; 53 } 54 55 static ssize_t backlight_show_brightness(struct class_device *cdev, char *buf) 56 { 57 int rc; 58 struct backlight_device *bd = to_backlight_device(cdev); 59 60 down(&bd->sem); 61 if (likely(bd->props && bd->props->get_brightness)) 62 rc = sprintf(buf, "%d\n", bd->props->get_brightness(bd)); 63 else 64 rc = -ENXIO; 65 up(&bd->sem); 66 67 return rc; 68 } 69 70 static ssize_t backlight_store_brightness(struct class_device *cdev, const char *buf, size_t count) 71 { 72 int rc, brightness; 73 char *endp; 74 struct backlight_device *bd = to_backlight_device(cdev); 75 76 brightness = simple_strtoul(buf, &endp, 0); 77 if (*endp && !isspace(*endp)) 78 return -EINVAL; 79 80 down(&bd->sem); 81 if (likely(bd->props && bd->props->set_brightness)) { 82 pr_debug("backlight: set brightness to %d\n", brightness); 83 bd->props->set_brightness(bd, brightness); 84 rc = count; 85 } else 86 rc = -ENXIO; 87 up(&bd->sem); 88 89 return rc; 90 } 91 92 static ssize_t backlight_show_max_brightness(struct class_device *cdev, char *buf) 93 { 94 int rc; 95 struct backlight_device *bd = to_backlight_device(cdev); 96 97 down(&bd->sem); 98 if (likely(bd->props)) 99 rc = sprintf(buf, "%d\n", bd->props->max_brightness); 100 else 101 rc = -ENXIO; 102 up(&bd->sem); 103 104 return rc; 105 } 106 107 static void backlight_class_release(struct class_device *dev) 108 { 109 struct backlight_device *bd = to_backlight_device(dev); 110 kfree(bd); 111 } 112 113 static struct class backlight_class = { 114 .name = "backlight", 115 .release = backlight_class_release, 116 }; 117 118 #define DECLARE_ATTR(_name,_mode,_show,_store) \ 119 { \ 120 .attr = { .name = __stringify(_name), .mode = _mode, .owner = THIS_MODULE }, \ 121 .show = _show, \ 122 .store = _store, \ 123 } 124 125 static struct class_device_attribute bl_class_device_attributes[] = { 126 DECLARE_ATTR(power, 0644, backlight_show_power, backlight_store_power), 127 DECLARE_ATTR(brightness, 0644, backlight_show_brightness, backlight_store_brightness), 128 DECLARE_ATTR(max_brightness, 0444, backlight_show_max_brightness, NULL), 129 }; 130 131 /* This callback gets called when something important happens inside a 132 * framebuffer driver. We're looking if that important event is blanking, 133 * and if it is, we're switching backlight power as well ... 134 */ 135 static int fb_notifier_callback(struct notifier_block *self, 136 unsigned long event, void *data) 137 { 138 struct backlight_device *bd; 139 struct fb_event *evdata =(struct fb_event *)data; 140 141 /* If we aren't interested in this event, skip it immediately ... */ 142 if (event != FB_EVENT_BLANK) 143 return 0; 144 145 bd = container_of(self, struct backlight_device, fb_notif); 146 down(&bd->sem); 147 if (bd->props) 148 if (!bd->props->check_fb || bd->props->check_fb(evdata->info)) 149 bd->props->set_power(bd, *(int *)evdata->data); 150 up(&bd->sem); 151 return 0; 152 } 153 154 /** 155 * backlight_device_register - create and register a new object of 156 * backlight_device class. 157 * @name: the name of the new object(must be the same as the name of the 158 * respective framebuffer device). 159 * @devdata: an optional pointer to be stored in the class_device. The 160 * methods may retrieve it by using class_get_devdata(&bd->class_dev). 161 * @bp: the backlight properties structure. 162 * 163 * Creates and registers new backlight class_device. Returns either an 164 * ERR_PTR() or a pointer to the newly allocated device. 165 */ 166 struct backlight_device *backlight_device_register(const char *name, void *devdata, 167 struct backlight_properties *bp) 168 { 169 int i, rc; 170 struct backlight_device *new_bd; 171 172 pr_debug("backlight_device_alloc: name=%s\n", name); 173 174 new_bd = kmalloc(sizeof(struct backlight_device), GFP_KERNEL); 175 if (unlikely(!new_bd)) 176 return ERR_PTR(ENOMEM); 177 178 init_MUTEX(&new_bd->sem); 179 new_bd->props = bp; 180 memset(&new_bd->class_dev, 0, sizeof(new_bd->class_dev)); 181 new_bd->class_dev.class = &backlight_class; 182 strlcpy(new_bd->class_dev.class_id, name, KOBJ_NAME_LEN); 183 class_set_devdata(&new_bd->class_dev, devdata); 184 185 rc = class_device_register(&new_bd->class_dev); 186 if (unlikely(rc)) { 187 error: kfree(new_bd); 188 return ERR_PTR(rc); 189 } 190 191 memset(&new_bd->fb_notif, 0, sizeof(new_bd->fb_notif)); 192 new_bd->fb_notif.notifier_call = fb_notifier_callback; 193 194 rc = fb_register_client(&new_bd->fb_notif); 195 if (unlikely(rc)) 196 goto error; 197 198 for (i = 0; i < ARRAY_SIZE(bl_class_device_attributes); i++) { 199 rc = class_device_create_file(&new_bd->class_dev, 200 &bl_class_device_attributes[i]); 201 if (unlikely(rc)) { 202 while (--i >= 0) 203 class_device_remove_file(&new_bd->class_dev, 204 &bl_class_device_attributes[i]); 205 class_device_unregister(&new_bd->class_dev); 206 /* No need to kfree(new_bd) since release() method was called */ 207 return ERR_PTR(rc); 208 } 209 } 210 211 return new_bd; 212 } 213 EXPORT_SYMBOL(backlight_device_register); 214 215 /** 216 * backlight_device_unregister - unregisters a backlight device object. 217 * @bd: the backlight device object to be unregistered and freed. 218 * 219 * Unregisters a previously registered via backlight_device_register object. 220 */ 221 void backlight_device_unregister(struct backlight_device *bd) 222 { 223 int i; 224 225 if (!bd) 226 return; 227 228 pr_debug("backlight_device_unregister: name=%s\n", bd->class_dev.class_id); 229 230 for (i = 0; i < ARRAY_SIZE(bl_class_device_attributes); i++) 231 class_device_remove_file(&bd->class_dev, 232 &bl_class_device_attributes[i]); 233 234 down(&bd->sem); 235 bd->props = NULL; 236 up(&bd->sem); 237 238 fb_unregister_client(&bd->fb_notif); 239 240 class_device_unregister(&bd->class_dev); 241 } 242 EXPORT_SYMBOL(backlight_device_unregister); 243 244 static void __exit backlight_class_exit(void) 245 { 246 class_unregister(&backlight_class); 247 } 248 249 static int __init backlight_class_init(void) 250 { 251 return class_register(&backlight_class); 252 } 253 254 /* 255 * if this is compiled into the kernel, we need to ensure that the 256 * class is registered before users of the class try to register lcd's 257 */ 258 postcore_initcall(backlight_class_init); 259 module_exit(backlight_class_exit); 260 261 MODULE_LICENSE("GPL"); 262 MODULE_AUTHOR("Jamey Hicks <jamey.hicks@hp.com>, Andrew Zabolotny <zap@homelink.ru>"); 263 MODULE_DESCRIPTION("Backlight Lowlevel Control Abstraction"); 264