backlight.c 6.9 KB

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