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 17 #ifdef CONFIG_PMAC_BACKLIGHT 18 #include <asm/backlight.h> 19 #endif 20 21 #if defined(CONFIG_FB) || (defined(CONFIG_FB_MODULE) && \ 22 defined(CONFIG_BACKLIGHT_CLASS_DEVICE_MODULE)) 23 /* This callback gets called when something important happens inside a 24 * framebuffer driver. We're looking if that important event is blanking, 25 * and if it is, we're switching backlight power as well ... 26 */ 27 static int fb_notifier_callback(struct notifier_block *self, 28 unsigned long event, void *data) 29 { 30 struct backlight_device *bd; 31 struct fb_event *evdata = data; 32 33 /* If we aren't interested in this event, skip it immediately ... */ 34 if (event != FB_EVENT_BLANK && event != FB_EVENT_CONBLANK) 35 return 0; 36 37 bd = container_of(self, struct backlight_device, fb_notif); 38 if (!lock_fb_info(evdata->info)) 39 return -ENODEV; 40 mutex_lock(&bd->ops_lock); 41 if (bd->ops) 42 if (!bd->ops->check_fb || 43 bd->ops->check_fb(evdata->info)) { 44 bd->props.fb_blank = *(int *)evdata->data; 45 if (bd->props.fb_blank == FB_BLANK_UNBLANK) 46 bd->props.state &= ~BL_CORE_FBBLANK; 47 else 48 bd->props.state |= BL_CORE_FBBLANK; 49 backlight_update_status(bd); 50 } 51 mutex_unlock(&bd->ops_lock); 52 unlock_fb_info(evdata->info); 53 return 0; 54 } 55 56 static int backlight_register_fb(struct backlight_device *bd) 57 { 58 memset(&bd->fb_notif, 0, sizeof(bd->fb_notif)); 59 bd->fb_notif.notifier_call = fb_notifier_callback; 60 61 return fb_register_client(&bd->fb_notif); 62 } 63 64 static void backlight_unregister_fb(struct backlight_device *bd) 65 { 66 fb_unregister_client(&bd->fb_notif); 67 } 68 #else 69 static inline int backlight_register_fb(struct backlight_device *bd) 70 { 71 return 0; 72 } 73 74 static inline void backlight_unregister_fb(struct backlight_device *bd) 75 { 76 } 77 #endif /* CONFIG_FB */ 78 79 static ssize_t backlight_show_power(struct device *dev, 80 struct device_attribute *attr,char *buf) 81 { 82 struct backlight_device *bd = to_backlight_device(dev); 83 84 return sprintf(buf, "%d\n", bd->props.power); 85 } 86 87 static ssize_t backlight_store_power(struct device *dev, 88 struct device_attribute *attr, const char *buf, size_t count) 89 { 90 int rc; 91 struct backlight_device *bd = to_backlight_device(dev); 92 unsigned long power; 93 94 rc = strict_strtoul(buf, 0, &power); 95 if (rc) 96 return rc; 97 98 rc = -ENXIO; 99 mutex_lock(&bd->ops_lock); 100 if (bd->ops) { 101 pr_debug("backlight: set power to %lu\n", power); 102 if (bd->props.power != power) { 103 bd->props.power = power; 104 backlight_update_status(bd); 105 } 106 rc = count; 107 } 108 mutex_unlock(&bd->ops_lock); 109 110 return rc; 111 } 112 113 static ssize_t backlight_show_brightness(struct device *dev, 114 struct device_attribute *attr, char *buf) 115 { 116 struct backlight_device *bd = to_backlight_device(dev); 117 118 return sprintf(buf, "%d\n", bd->props.brightness); 119 } 120 121 static ssize_t backlight_store_brightness(struct device *dev, 122 struct device_attribute *attr, const char *buf, size_t count) 123 { 124 int rc; 125 struct backlight_device *bd = to_backlight_device(dev); 126 unsigned long brightness; 127 128 rc = strict_strtoul(buf, 0, &brightness); 129 if (rc) 130 return rc; 131 132 rc = -ENXIO; 133 134 mutex_lock(&bd->ops_lock); 135 if (bd->ops) { 136 if (brightness > bd->props.max_brightness) 137 rc = -EINVAL; 138 else { 139 pr_debug("backlight: set brightness to %lu\n", 140 brightness); 141 bd->props.brightness = brightness; 142 backlight_update_status(bd); 143 rc = count; 144 } 145 } 146 mutex_unlock(&bd->ops_lock); 147 148 return rc; 149 } 150 151 static ssize_t backlight_show_max_brightness(struct device *dev, 152 struct device_attribute *attr, char *buf) 153 { 154 struct backlight_device *bd = to_backlight_device(dev); 155 156 return sprintf(buf, "%d\n", bd->props.max_brightness); 157 } 158 159 static ssize_t backlight_show_actual_brightness(struct device *dev, 160 struct device_attribute *attr, char *buf) 161 { 162 int rc = -ENXIO; 163 struct backlight_device *bd = to_backlight_device(dev); 164 165 mutex_lock(&bd->ops_lock); 166 if (bd->ops && bd->ops->get_brightness) 167 rc = sprintf(buf, "%d\n", bd->ops->get_brightness(bd)); 168 mutex_unlock(&bd->ops_lock); 169 170 return rc; 171 } 172 173 static struct class *backlight_class; 174 175 static int backlight_suspend(struct device *dev, pm_message_t state) 176 { 177 struct backlight_device *bd = to_backlight_device(dev); 178 179 if (bd->ops->options & BL_CORE_SUSPENDRESUME) { 180 mutex_lock(&bd->ops_lock); 181 bd->props.state |= BL_CORE_SUSPENDED; 182 backlight_update_status(bd); 183 mutex_unlock(&bd->ops_lock); 184 } 185 186 return 0; 187 } 188 189 static int backlight_resume(struct device *dev) 190 { 191 struct backlight_device *bd = to_backlight_device(dev); 192 193 if (bd->ops->options & BL_CORE_SUSPENDRESUME) { 194 mutex_lock(&bd->ops_lock); 195 bd->props.state &= ~BL_CORE_SUSPENDED; 196 backlight_update_status(bd); 197 mutex_unlock(&bd->ops_lock); 198 } 199 200 return 0; 201 } 202 203 static void bl_device_release(struct device *dev) 204 { 205 struct backlight_device *bd = to_backlight_device(dev); 206 kfree(bd); 207 } 208 209 static struct device_attribute bl_device_attributes[] = { 210 __ATTR(bl_power, 0644, backlight_show_power, backlight_store_power), 211 __ATTR(brightness, 0644, backlight_show_brightness, 212 backlight_store_brightness), 213 __ATTR(actual_brightness, 0444, backlight_show_actual_brightness, 214 NULL), 215 __ATTR(max_brightness, 0444, backlight_show_max_brightness, NULL), 216 __ATTR_NULL, 217 }; 218 219 /** 220 * backlight_device_register - create and register a new object of 221 * backlight_device class. 222 * @name: the name of the new object(must be the same as the name of the 223 * respective framebuffer device). 224 * @parent: a pointer to the parent device 225 * @devdata: an optional pointer to be stored for private driver use. The 226 * methods may retrieve it by using bl_get_data(bd). 227 * @ops: the backlight operations structure. 228 * 229 * Creates and registers new backlight device. Returns either an 230 * ERR_PTR() or a pointer to the newly allocated device. 231 */ 232 struct backlight_device *backlight_device_register(const char *name, 233 struct device *parent, void *devdata, struct backlight_ops *ops) 234 { 235 struct backlight_device *new_bd; 236 int rc; 237 238 pr_debug("backlight_device_register: name=%s\n", name); 239 240 new_bd = kzalloc(sizeof(struct backlight_device), GFP_KERNEL); 241 if (!new_bd) 242 return ERR_PTR(-ENOMEM); 243 244 mutex_init(&new_bd->update_lock); 245 mutex_init(&new_bd->ops_lock); 246 247 new_bd->dev.class = backlight_class; 248 new_bd->dev.parent = parent; 249 new_bd->dev.release = bl_device_release; 250 dev_set_name(&new_bd->dev, name); 251 dev_set_drvdata(&new_bd->dev, devdata); 252 253 rc = device_register(&new_bd->dev); 254 if (rc) { 255 kfree(new_bd); 256 return ERR_PTR(rc); 257 } 258 259 rc = backlight_register_fb(new_bd); 260 if (rc) { 261 device_unregister(&new_bd->dev); 262 return ERR_PTR(rc); 263 } 264 265 new_bd->ops = ops; 266 267 #ifdef CONFIG_PMAC_BACKLIGHT 268 mutex_lock(&pmac_backlight_mutex); 269 if (!pmac_backlight) 270 pmac_backlight = new_bd; 271 mutex_unlock(&pmac_backlight_mutex); 272 #endif 273 274 return new_bd; 275 } 276 EXPORT_SYMBOL(backlight_device_register); 277 278 /** 279 * backlight_device_unregister - unregisters a backlight device object. 280 * @bd: the backlight device object to be unregistered and freed. 281 * 282 * Unregisters a previously registered via backlight_device_register object. 283 */ 284 void backlight_device_unregister(struct backlight_device *bd) 285 { 286 if (!bd) 287 return; 288 289 #ifdef CONFIG_PMAC_BACKLIGHT 290 mutex_lock(&pmac_backlight_mutex); 291 if (pmac_backlight == bd) 292 pmac_backlight = NULL; 293 mutex_unlock(&pmac_backlight_mutex); 294 #endif 295 mutex_lock(&bd->ops_lock); 296 bd->ops = NULL; 297 mutex_unlock(&bd->ops_lock); 298 299 backlight_unregister_fb(bd); 300 device_unregister(&bd->dev); 301 } 302 EXPORT_SYMBOL(backlight_device_unregister); 303 304 static void __exit backlight_class_exit(void) 305 { 306 class_destroy(backlight_class); 307 } 308 309 static int __init backlight_class_init(void) 310 { 311 backlight_class = class_create(THIS_MODULE, "backlight"); 312 if (IS_ERR(backlight_class)) { 313 printk(KERN_WARNING "Unable to create backlight class; errno = %ld\n", 314 PTR_ERR(backlight_class)); 315 return PTR_ERR(backlight_class); 316 } 317 318 backlight_class->dev_attrs = bl_device_attributes; 319 backlight_class->suspend = backlight_suspend; 320 backlight_class->resume = backlight_resume; 321 return 0; 322 } 323 324 /* 325 * if this is compiled into the kernel, we need to ensure that the 326 * class is registered before users of the class try to register lcd's 327 */ 328 postcore_initcall(backlight_class_init); 329 module_exit(backlight_class_exit); 330 331 MODULE_LICENSE("GPL"); 332 MODULE_AUTHOR("Jamey Hicks <jamey.hicks@hp.com>, Andrew Zabolotny <zap@homelink.ru>"); 333 MODULE_DESCRIPTION("Backlight Lowlevel Control Abstraction"); 334