backlight.c 8.0 KB

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