backlight.c 8.4 KB

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