backlight.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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. if (bd->props.fb_blank == FB_BLANK_UNBLANK)
  39. bd->props.state &= ~BL_CORE_FBBLANK;
  40. else
  41. bd->props.state |= BL_CORE_FBBLANK;
  42. backlight_update_status(bd);
  43. }
  44. mutex_unlock(&bd->ops_lock);
  45. return 0;
  46. }
  47. static int backlight_register_fb(struct backlight_device *bd)
  48. {
  49. memset(&bd->fb_notif, 0, sizeof(bd->fb_notif));
  50. bd->fb_notif.notifier_call = fb_notifier_callback;
  51. return fb_register_client(&bd->fb_notif);
  52. }
  53. static void backlight_unregister_fb(struct backlight_device *bd)
  54. {
  55. fb_unregister_client(&bd->fb_notif);
  56. }
  57. #else
  58. static inline int backlight_register_fb(struct backlight_device *bd)
  59. {
  60. return 0;
  61. }
  62. static inline void backlight_unregister_fb(struct backlight_device *bd)
  63. {
  64. }
  65. #endif /* CONFIG_FB */
  66. static ssize_t backlight_show_power(struct device *dev,
  67. struct device_attribute *attr,char *buf)
  68. {
  69. struct backlight_device *bd = to_backlight_device(dev);
  70. return sprintf(buf, "%d\n", bd->props.power);
  71. }
  72. static ssize_t backlight_store_power(struct device *dev,
  73. struct device_attribute *attr, const char *buf, size_t count)
  74. {
  75. int rc;
  76. struct backlight_device *bd = to_backlight_device(dev);
  77. unsigned long power;
  78. rc = strict_strtoul(buf, 0, &power);
  79. if (rc)
  80. return rc;
  81. rc = -ENXIO;
  82. mutex_lock(&bd->ops_lock);
  83. if (bd->ops) {
  84. pr_debug("backlight: set power to %lu\n", power);
  85. if (bd->props.power != power) {
  86. bd->props.power = power;
  87. backlight_update_status(bd);
  88. }
  89. rc = count;
  90. }
  91. mutex_unlock(&bd->ops_lock);
  92. return rc;
  93. }
  94. static ssize_t backlight_show_brightness(struct device *dev,
  95. struct device_attribute *attr, char *buf)
  96. {
  97. struct backlight_device *bd = to_backlight_device(dev);
  98. return sprintf(buf, "%d\n", bd->props.brightness);
  99. }
  100. static ssize_t backlight_store_brightness(struct device *dev,
  101. struct device_attribute *attr, const char *buf, size_t count)
  102. {
  103. int rc;
  104. struct backlight_device *bd = to_backlight_device(dev);
  105. unsigned long brightness;
  106. rc = strict_strtoul(buf, 0, &brightness);
  107. if (rc)
  108. return rc;
  109. rc = -ENXIO;
  110. mutex_lock(&bd->ops_lock);
  111. if (bd->ops) {
  112. if (brightness > bd->props.max_brightness)
  113. rc = -EINVAL;
  114. else {
  115. pr_debug("backlight: set brightness to %lu\n",
  116. brightness);
  117. bd->props.brightness = brightness;
  118. backlight_update_status(bd);
  119. rc = count;
  120. }
  121. }
  122. mutex_unlock(&bd->ops_lock);
  123. return rc;
  124. }
  125. static ssize_t backlight_show_max_brightness(struct device *dev,
  126. struct device_attribute *attr, char *buf)
  127. {
  128. struct backlight_device *bd = to_backlight_device(dev);
  129. return sprintf(buf, "%d\n", bd->props.max_brightness);
  130. }
  131. static ssize_t backlight_show_actual_brightness(struct device *dev,
  132. struct device_attribute *attr, char *buf)
  133. {
  134. int rc = -ENXIO;
  135. struct backlight_device *bd = to_backlight_device(dev);
  136. mutex_lock(&bd->ops_lock);
  137. if (bd->ops && bd->ops->get_brightness)
  138. rc = sprintf(buf, "%d\n", bd->ops->get_brightness(bd));
  139. mutex_unlock(&bd->ops_lock);
  140. return rc;
  141. }
  142. static struct class *backlight_class;
  143. static int backlight_suspend(struct device *dev, pm_message_t state)
  144. {
  145. struct backlight_device *bd = to_backlight_device(dev);
  146. if (bd->ops->options & BL_CORE_SUSPENDRESUME) {
  147. mutex_lock(&bd->ops_lock);
  148. bd->props.state |= BL_CORE_SUSPENDED;
  149. backlight_update_status(bd);
  150. mutex_unlock(&bd->ops_lock);
  151. }
  152. return 0;
  153. }
  154. static int backlight_resume(struct device *dev)
  155. {
  156. struct backlight_device *bd = to_backlight_device(dev);
  157. if (bd->ops->options & BL_CORE_SUSPENDRESUME) {
  158. mutex_lock(&bd->ops_lock);
  159. bd->props.state &= ~BL_CORE_SUSPENDED;
  160. backlight_update_status(bd);
  161. mutex_unlock(&bd->ops_lock);
  162. }
  163. return 0;
  164. }
  165. static void bl_device_release(struct device *dev)
  166. {
  167. struct backlight_device *bd = to_backlight_device(dev);
  168. kfree(bd);
  169. }
  170. static struct device_attribute bl_device_attributes[] = {
  171. __ATTR(bl_power, 0644, backlight_show_power, backlight_store_power),
  172. __ATTR(brightness, 0644, backlight_show_brightness,
  173. backlight_store_brightness),
  174. __ATTR(actual_brightness, 0444, backlight_show_actual_brightness,
  175. NULL),
  176. __ATTR(max_brightness, 0444, backlight_show_max_brightness, NULL),
  177. __ATTR_NULL,
  178. };
  179. /**
  180. * backlight_device_register - create and register a new object of
  181. * backlight_device class.
  182. * @name: the name of the new object(must be the same as the name of the
  183. * respective framebuffer device).
  184. * @parent: a pointer to the parent device
  185. * @devdata: an optional pointer to be stored for private driver use. The
  186. * methods may retrieve it by using bl_get_data(bd).
  187. * @ops: the backlight operations structure.
  188. *
  189. * Creates and registers new backlight device. Returns either an
  190. * ERR_PTR() or a pointer to the newly allocated device.
  191. */
  192. struct backlight_device *backlight_device_register(const char *name,
  193. struct device *parent, void *devdata, struct backlight_ops *ops)
  194. {
  195. struct backlight_device *new_bd;
  196. int rc;
  197. pr_debug("backlight_device_register: name=%s\n", name);
  198. new_bd = kzalloc(sizeof(struct backlight_device), GFP_KERNEL);
  199. if (!new_bd)
  200. return ERR_PTR(-ENOMEM);
  201. mutex_init(&new_bd->update_lock);
  202. mutex_init(&new_bd->ops_lock);
  203. new_bd->dev.class = backlight_class;
  204. new_bd->dev.parent = parent;
  205. new_bd->dev.release = bl_device_release;
  206. dev_set_name(&new_bd->dev, name);
  207. dev_set_drvdata(&new_bd->dev, devdata);
  208. rc = device_register(&new_bd->dev);
  209. if (rc) {
  210. kfree(new_bd);
  211. return ERR_PTR(rc);
  212. }
  213. rc = backlight_register_fb(new_bd);
  214. if (rc) {
  215. device_unregister(&new_bd->dev);
  216. return ERR_PTR(rc);
  217. }
  218. new_bd->ops = ops;
  219. #ifdef CONFIG_PMAC_BACKLIGHT
  220. mutex_lock(&pmac_backlight_mutex);
  221. if (!pmac_backlight)
  222. pmac_backlight = new_bd;
  223. mutex_unlock(&pmac_backlight_mutex);
  224. #endif
  225. return new_bd;
  226. }
  227. EXPORT_SYMBOL(backlight_device_register);
  228. /**
  229. * backlight_device_unregister - unregisters a backlight device object.
  230. * @bd: the backlight device object to be unregistered and freed.
  231. *
  232. * Unregisters a previously registered via backlight_device_register object.
  233. */
  234. void backlight_device_unregister(struct backlight_device *bd)
  235. {
  236. if (!bd)
  237. return;
  238. #ifdef CONFIG_PMAC_BACKLIGHT
  239. mutex_lock(&pmac_backlight_mutex);
  240. if (pmac_backlight == bd)
  241. pmac_backlight = NULL;
  242. mutex_unlock(&pmac_backlight_mutex);
  243. #endif
  244. mutex_lock(&bd->ops_lock);
  245. bd->ops = NULL;
  246. mutex_unlock(&bd->ops_lock);
  247. backlight_unregister_fb(bd);
  248. device_unregister(&bd->dev);
  249. }
  250. EXPORT_SYMBOL(backlight_device_unregister);
  251. static void __exit backlight_class_exit(void)
  252. {
  253. class_destroy(backlight_class);
  254. }
  255. static int __init backlight_class_init(void)
  256. {
  257. backlight_class = class_create(THIS_MODULE, "backlight");
  258. if (IS_ERR(backlight_class)) {
  259. printk(KERN_WARNING "Unable to create backlight class; errno = %ld\n",
  260. PTR_ERR(backlight_class));
  261. return PTR_ERR(backlight_class);
  262. }
  263. backlight_class->dev_attrs = bl_device_attributes;
  264. backlight_class->suspend = backlight_suspend;
  265. backlight_class->resume = backlight_resume;
  266. return 0;
  267. }
  268. /*
  269. * if this is compiled into the kernel, we need to ensure that the
  270. * class is registered before users of the class try to register lcd's
  271. */
  272. postcore_initcall(backlight_class_init);
  273. module_exit(backlight_class_exit);
  274. MODULE_LICENSE("GPL");
  275. MODULE_AUTHOR("Jamey Hicks <jamey.hicks@hp.com>, Andrew Zabolotny <zap@homelink.ru>");
  276. MODULE_DESCRIPTION("Backlight Lowlevel Control Abstraction");