backlight.c 6.9 KB

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