backlight.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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->props_lock);
  34. if (bd->props)
  35. if (!bd->props->check_fb ||
  36. bd->props->check_fb(evdata->info)) {
  37. bd->props->fb_blank = *(int *)evdata->data;
  38. backlight_update_status(bd);
  39. }
  40. mutex_unlock(&bd->props_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. int rc = -ENXIO;
  65. struct backlight_device *bd = to_backlight_device(cdev);
  66. mutex_lock(&bd->props_lock);
  67. if (bd->props)
  68. rc = sprintf(buf, "%d\n", bd->props->power);
  69. mutex_unlock(&bd->props_lock);
  70. return rc;
  71. }
  72. static ssize_t backlight_store_power(struct class_device *cdev, const char *buf, size_t count)
  73. {
  74. int rc = -ENXIO;
  75. char *endp;
  76. struct backlight_device *bd = to_backlight_device(cdev);
  77. int power = simple_strtoul(buf, &endp, 0);
  78. size_t size = endp - buf;
  79. if (*endp && isspace(*endp))
  80. size++;
  81. if (size != count)
  82. return -EINVAL;
  83. mutex_lock(&bd->props_lock);
  84. if (bd->props) {
  85. pr_debug("backlight: set power to %d\n", power);
  86. bd->props->power = power;
  87. backlight_update_status(bd);
  88. rc = count;
  89. }
  90. mutex_unlock(&bd->props_lock);
  91. return rc;
  92. }
  93. static ssize_t backlight_show_brightness(struct class_device *cdev, char *buf)
  94. {
  95. int rc = -ENXIO;
  96. struct backlight_device *bd = to_backlight_device(cdev);
  97. mutex_lock(&bd->props_lock);
  98. if (bd->props)
  99. rc = sprintf(buf, "%d\n", bd->props->brightness);
  100. mutex_unlock(&bd->props_lock);
  101. return rc;
  102. }
  103. static ssize_t backlight_store_brightness(struct class_device *cdev, const char *buf, size_t count)
  104. {
  105. int rc = -ENXIO;
  106. char *endp;
  107. struct backlight_device *bd = to_backlight_device(cdev);
  108. int brightness = simple_strtoul(buf, &endp, 0);
  109. size_t size = endp - buf;
  110. if (*endp && isspace(*endp))
  111. size++;
  112. if (size != count)
  113. return -EINVAL;
  114. mutex_lock(&bd->props_lock);
  115. if (bd->props) {
  116. if (brightness > bd->props->max_brightness)
  117. rc = -EINVAL;
  118. else {
  119. pr_debug("backlight: set brightness to %d\n",
  120. brightness);
  121. bd->props->brightness = brightness;
  122. backlight_update_status(bd);
  123. rc = count;
  124. }
  125. }
  126. mutex_unlock(&bd->props_lock);
  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. mutex_lock(&bd->props_lock);
  134. if (bd->props)
  135. rc = sprintf(buf, "%d\n", bd->props->max_brightness);
  136. mutex_unlock(&bd->props_lock);
  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. mutex_lock(&bd->props_lock);
  145. if (bd->props && bd->props->get_brightness)
  146. rc = sprintf(buf, "%d\n", bd->props->get_brightness(bd));
  147. mutex_unlock(&bd->props_lock);
  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 (!new_bd)
  195. return ERR_PTR(-ENOMEM);
  196. mutex_init(&new_bd->update_lock);
  197. mutex_init(&new_bd->props_lock);
  198. new_bd->props = bp;
  199. memset(&new_bd->class_dev, 0, sizeof(new_bd->class_dev));
  200. new_bd->class_dev.class = &backlight_class;
  201. new_bd->class_dev.dev = dev;
  202. strlcpy(new_bd->class_dev.class_id, name, KOBJ_NAME_LEN);
  203. class_set_devdata(&new_bd->class_dev, devdata);
  204. rc = class_device_register(&new_bd->class_dev);
  205. if (rc) {
  206. kfree(new_bd);
  207. return ERR_PTR(rc);
  208. }
  209. rc = backlight_register_fb(new_bd);
  210. if (rc) {
  211. class_device_unregister(&new_bd->class_dev);
  212. return ERR_PTR(rc);
  213. }
  214. for (i = 0; i < ARRAY_SIZE(bl_class_device_attributes); i++) {
  215. rc = class_device_create_file(&new_bd->class_dev,
  216. &bl_class_device_attributes[i]);
  217. if (rc) {
  218. while (--i >= 0)
  219. class_device_remove_file(&new_bd->class_dev,
  220. &bl_class_device_attributes[i]);
  221. class_device_unregister(&new_bd->class_dev);
  222. /* No need to kfree(new_bd) since release() method was called */
  223. return ERR_PTR(rc);
  224. }
  225. }
  226. #ifdef CONFIG_PMAC_BACKLIGHT
  227. mutex_lock(&pmac_backlight_mutex);
  228. if (!pmac_backlight)
  229. pmac_backlight = new_bd;
  230. mutex_unlock(&pmac_backlight_mutex);
  231. #endif
  232. return new_bd;
  233. }
  234. EXPORT_SYMBOL(backlight_device_register);
  235. /**
  236. * backlight_device_unregister - unregisters a backlight device object.
  237. * @bd: the backlight device object to be unregistered and freed.
  238. *
  239. * Unregisters a previously registered via backlight_device_register object.
  240. */
  241. void backlight_device_unregister(struct backlight_device *bd)
  242. {
  243. int i;
  244. if (!bd)
  245. return;
  246. pr_debug("backlight_device_unregister: name=%s\n", bd->class_dev.class_id);
  247. #ifdef CONFIG_PMAC_BACKLIGHT
  248. mutex_lock(&pmac_backlight_mutex);
  249. if (pmac_backlight == bd)
  250. pmac_backlight = NULL;
  251. mutex_unlock(&pmac_backlight_mutex);
  252. #endif
  253. for (i = 0; i < ARRAY_SIZE(bl_class_device_attributes); i++)
  254. class_device_remove_file(&bd->class_dev,
  255. &bl_class_device_attributes[i]);
  256. mutex_lock(&bd->props_lock);
  257. bd->props = NULL;
  258. mutex_unlock(&bd->props_lock);
  259. backlight_unregister_fb(bd);
  260. class_device_unregister(&bd->class_dev);
  261. }
  262. EXPORT_SYMBOL(backlight_device_unregister);
  263. static void __exit backlight_class_exit(void)
  264. {
  265. class_unregister(&backlight_class);
  266. }
  267. static int __init backlight_class_init(void)
  268. {
  269. return class_register(&backlight_class);
  270. }
  271. /*
  272. * if this is compiled into the kernel, we need to ensure that the
  273. * class is registered before users of the class try to register lcd's
  274. */
  275. postcore_initcall(backlight_class_init);
  276. module_exit(backlight_class_exit);
  277. MODULE_LICENSE("GPL");
  278. MODULE_AUTHOR("Jamey Hicks <jamey.hicks@hp.com>, Andrew Zabolotny <zap@homelink.ru>");
  279. MODULE_DESCRIPTION("Backlight Lowlevel Control Abstraction");