backlight.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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. mutex_lock(&bd->props_lock);
  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. backlight_update_status(bd);
  36. }
  37. mutex_unlock(&bd->props_lock);
  38. return 0;
  39. }
  40. static int backlight_register_fb(struct backlight_device *bd)
  41. {
  42. memset(&bd->fb_notif, 0, sizeof(bd->fb_notif));
  43. bd->fb_notif.notifier_call = fb_notifier_callback;
  44. return fb_register_client(&bd->fb_notif);
  45. }
  46. static void backlight_unregister_fb(struct backlight_device *bd)
  47. {
  48. fb_unregister_client(&bd->fb_notif);
  49. }
  50. #else
  51. static inline int backlight_register_fb(struct backlight_device *bd)
  52. {
  53. return 0;
  54. }
  55. static inline void backlight_unregister_fb(struct backlight_device *bd)
  56. {
  57. }
  58. #endif /* CONFIG_FB */
  59. static ssize_t backlight_show_power(struct class_device *cdev, char *buf)
  60. {
  61. int rc = -ENXIO;
  62. struct backlight_device *bd = to_backlight_device(cdev);
  63. mutex_lock(&bd->props_lock);
  64. if (bd->props)
  65. rc = sprintf(buf, "%d\n", bd->props->power);
  66. mutex_unlock(&bd->props_lock);
  67. return rc;
  68. }
  69. static ssize_t backlight_store_power(struct class_device *cdev, const char *buf, size_t count)
  70. {
  71. int rc = -ENXIO;
  72. char *endp;
  73. struct backlight_device *bd = to_backlight_device(cdev);
  74. int power = simple_strtoul(buf, &endp, 0);
  75. size_t size = endp - buf;
  76. if (*endp && isspace(*endp))
  77. size++;
  78. if (size != count)
  79. return -EINVAL;
  80. mutex_lock(&bd->props_lock);
  81. if (bd->props) {
  82. pr_debug("backlight: set power to %d\n", power);
  83. bd->props->power = power;
  84. backlight_update_status(bd);
  85. rc = count;
  86. }
  87. mutex_unlock(&bd->props_lock);
  88. return rc;
  89. }
  90. static ssize_t backlight_show_brightness(struct class_device *cdev, char *buf)
  91. {
  92. int rc = -ENXIO;
  93. struct backlight_device *bd = to_backlight_device(cdev);
  94. mutex_lock(&bd->props_lock);
  95. if (bd->props)
  96. rc = sprintf(buf, "%d\n", bd->props->brightness);
  97. mutex_unlock(&bd->props_lock);
  98. return rc;
  99. }
  100. static ssize_t backlight_store_brightness(struct class_device *cdev, const char *buf, size_t count)
  101. {
  102. int rc = -ENXIO;
  103. char *endp;
  104. struct backlight_device *bd = to_backlight_device(cdev);
  105. int brightness = simple_strtoul(buf, &endp, 0);
  106. size_t size = endp - buf;
  107. if (*endp && isspace(*endp))
  108. size++;
  109. if (size != count)
  110. return -EINVAL;
  111. mutex_lock(&bd->props_lock);
  112. if (bd->props) {
  113. if (brightness > bd->props->max_brightness)
  114. rc = -EINVAL;
  115. else {
  116. pr_debug("backlight: set brightness to %d\n",
  117. brightness);
  118. bd->props->brightness = brightness;
  119. backlight_update_status(bd);
  120. rc = count;
  121. }
  122. }
  123. mutex_unlock(&bd->props_lock);
  124. return rc;
  125. }
  126. static ssize_t backlight_show_max_brightness(struct class_device *cdev, char *buf)
  127. {
  128. int rc = -ENXIO;
  129. struct backlight_device *bd = to_backlight_device(cdev);
  130. mutex_lock(&bd->props_lock);
  131. if (bd->props)
  132. rc = sprintf(buf, "%d\n", bd->props->max_brightness);
  133. mutex_unlock(&bd->props_lock);
  134. return rc;
  135. }
  136. static ssize_t backlight_show_actual_brightness(struct class_device *cdev,
  137. char *buf)
  138. {
  139. int rc = -ENXIO;
  140. struct backlight_device *bd = to_backlight_device(cdev);
  141. mutex_lock(&bd->props_lock);
  142. if (bd->props && bd->props->get_brightness)
  143. rc = sprintf(buf, "%d\n", bd->props->get_brightness(bd));
  144. mutex_unlock(&bd->props_lock);
  145. return rc;
  146. }
  147. static void backlight_class_release(struct class_device *dev)
  148. {
  149. struct backlight_device *bd = to_backlight_device(dev);
  150. kfree(bd);
  151. }
  152. static struct class backlight_class = {
  153. .name = "backlight",
  154. .release = backlight_class_release,
  155. };
  156. #define DECLARE_ATTR(_name,_mode,_show,_store) \
  157. { \
  158. .attr = { .name = __stringify(_name), .mode = _mode, .owner = THIS_MODULE }, \
  159. .show = _show, \
  160. .store = _store, \
  161. }
  162. static const struct class_device_attribute bl_class_device_attributes[] = {
  163. DECLARE_ATTR(power, 0644, backlight_show_power, backlight_store_power),
  164. DECLARE_ATTR(brightness, 0644, backlight_show_brightness,
  165. backlight_store_brightness),
  166. DECLARE_ATTR(actual_brightness, 0444, backlight_show_actual_brightness,
  167. NULL),
  168. DECLARE_ATTR(max_brightness, 0444, backlight_show_max_brightness, NULL),
  169. };
  170. /**
  171. * backlight_device_register - create and register a new object of
  172. * backlight_device class.
  173. * @name: the name of the new object(must be the same as the name of the
  174. * respective framebuffer device).
  175. * @devdata: an optional pointer to be stored in the class_device. The
  176. * methods may retrieve it by using class_get_devdata(&bd->class_dev).
  177. * @bp: the backlight properties structure.
  178. *
  179. * Creates and registers new backlight class_device. Returns either an
  180. * ERR_PTR() or a pointer to the newly allocated device.
  181. */
  182. struct backlight_device *backlight_device_register(const char *name,
  183. struct device *dev,
  184. void *devdata,
  185. struct backlight_properties *bp)
  186. {
  187. int i, rc;
  188. struct backlight_device *new_bd;
  189. pr_debug("backlight_device_alloc: name=%s\n", name);
  190. new_bd = kmalloc(sizeof(struct backlight_device), GFP_KERNEL);
  191. if (!new_bd)
  192. return ERR_PTR(-ENOMEM);
  193. mutex_init(&new_bd->update_lock);
  194. mutex_init(&new_bd->props_lock);
  195. new_bd->props = bp;
  196. memset(&new_bd->class_dev, 0, sizeof(new_bd->class_dev));
  197. new_bd->class_dev.class = &backlight_class;
  198. new_bd->class_dev.dev = dev;
  199. strlcpy(new_bd->class_dev.class_id, name, KOBJ_NAME_LEN);
  200. class_set_devdata(&new_bd->class_dev, devdata);
  201. rc = class_device_register(&new_bd->class_dev);
  202. if (rc) {
  203. kfree(new_bd);
  204. return ERR_PTR(rc);
  205. }
  206. rc = backlight_register_fb(new_bd);
  207. if (rc) {
  208. class_device_unregister(&new_bd->class_dev);
  209. return ERR_PTR(rc);
  210. }
  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 (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. mutex_lock(&bd->props_lock);
  242. bd->props = NULL;
  243. mutex_unlock(&bd->props_lock);
  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");