backlight.c 8.1 KB

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