backlight.c 6.9 KB

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