backlight.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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 void backlight_generate_event(struct backlight_device *bd,
  67. enum backlight_update_reason reason)
  68. {
  69. char *envp[2];
  70. switch (reason) {
  71. case BACKLIGHT_UPDATE_SYSFS:
  72. envp[0] = "SOURCE=sysfs";
  73. break;
  74. case BACKLIGHT_UPDATE_HOTKEY:
  75. envp[0] = "SOURCE=hotkey";
  76. break;
  77. default:
  78. envp[0] = "SOURCE=unknown";
  79. break;
  80. }
  81. envp[1] = NULL;
  82. kobject_uevent_env(&bd->dev.kobj, KOBJ_CHANGE, envp);
  83. sysfs_notify(&bd->dev.kobj, NULL, "actual_brightness");
  84. }
  85. static ssize_t backlight_show_power(struct device *dev,
  86. struct device_attribute *attr,char *buf)
  87. {
  88. struct backlight_device *bd = to_backlight_device(dev);
  89. return sprintf(buf, "%d\n", bd->props.power);
  90. }
  91. static ssize_t backlight_store_power(struct device *dev,
  92. struct device_attribute *attr, const char *buf, size_t count)
  93. {
  94. int rc;
  95. struct backlight_device *bd = to_backlight_device(dev);
  96. unsigned long power;
  97. rc = strict_strtoul(buf, 0, &power);
  98. if (rc)
  99. return rc;
  100. rc = -ENXIO;
  101. mutex_lock(&bd->ops_lock);
  102. if (bd->ops) {
  103. pr_debug("backlight: set power to %lu\n", power);
  104. if (bd->props.power != power) {
  105. bd->props.power = power;
  106. backlight_update_status(bd);
  107. }
  108. rc = count;
  109. }
  110. mutex_unlock(&bd->ops_lock);
  111. return rc;
  112. }
  113. static ssize_t backlight_show_brightness(struct device *dev,
  114. struct device_attribute *attr, char *buf)
  115. {
  116. struct backlight_device *bd = to_backlight_device(dev);
  117. return sprintf(buf, "%d\n", bd->props.brightness);
  118. }
  119. static ssize_t backlight_store_brightness(struct device *dev,
  120. struct device_attribute *attr, const char *buf, size_t count)
  121. {
  122. int rc;
  123. struct backlight_device *bd = to_backlight_device(dev);
  124. unsigned long brightness;
  125. rc = strict_strtoul(buf, 0, &brightness);
  126. if (rc)
  127. return rc;
  128. rc = -ENXIO;
  129. mutex_lock(&bd->ops_lock);
  130. if (bd->ops) {
  131. if (brightness > bd->props.max_brightness)
  132. rc = -EINVAL;
  133. else {
  134. pr_debug("backlight: set brightness to %lu\n",
  135. brightness);
  136. bd->props.brightness = brightness;
  137. backlight_update_status(bd);
  138. rc = count;
  139. }
  140. }
  141. mutex_unlock(&bd->ops_lock);
  142. backlight_generate_event(bd, BACKLIGHT_UPDATE_SYSFS);
  143. return rc;
  144. }
  145. static ssize_t backlight_show_max_brightness(struct device *dev,
  146. struct device_attribute *attr, char *buf)
  147. {
  148. struct backlight_device *bd = to_backlight_device(dev);
  149. return sprintf(buf, "%d\n", bd->props.max_brightness);
  150. }
  151. static ssize_t backlight_show_actual_brightness(struct device *dev,
  152. struct device_attribute *attr, char *buf)
  153. {
  154. int rc = -ENXIO;
  155. struct backlight_device *bd = to_backlight_device(dev);
  156. mutex_lock(&bd->ops_lock);
  157. if (bd->ops && bd->ops->get_brightness)
  158. rc = sprintf(buf, "%d\n", bd->ops->get_brightness(bd));
  159. mutex_unlock(&bd->ops_lock);
  160. return rc;
  161. }
  162. static struct class *backlight_class;
  163. static int backlight_suspend(struct device *dev, pm_message_t state)
  164. {
  165. struct backlight_device *bd = to_backlight_device(dev);
  166. if (bd->ops->options & BL_CORE_SUSPENDRESUME) {
  167. mutex_lock(&bd->ops_lock);
  168. bd->props.state |= BL_CORE_SUSPENDED;
  169. backlight_update_status(bd);
  170. mutex_unlock(&bd->ops_lock);
  171. }
  172. return 0;
  173. }
  174. static int backlight_resume(struct device *dev)
  175. {
  176. struct backlight_device *bd = to_backlight_device(dev);
  177. if (bd->ops->options & BL_CORE_SUSPENDRESUME) {
  178. mutex_lock(&bd->ops_lock);
  179. bd->props.state &= ~BL_CORE_SUSPENDED;
  180. backlight_update_status(bd);
  181. mutex_unlock(&bd->ops_lock);
  182. }
  183. return 0;
  184. }
  185. static void bl_device_release(struct device *dev)
  186. {
  187. struct backlight_device *bd = to_backlight_device(dev);
  188. kfree(bd);
  189. }
  190. static struct device_attribute bl_device_attributes[] = {
  191. __ATTR(bl_power, 0644, backlight_show_power, backlight_store_power),
  192. __ATTR(brightness, 0644, backlight_show_brightness,
  193. backlight_store_brightness),
  194. __ATTR(actual_brightness, 0444, backlight_show_actual_brightness,
  195. NULL),
  196. __ATTR(max_brightness, 0444, backlight_show_max_brightness, NULL),
  197. __ATTR_NULL,
  198. };
  199. /**
  200. * backlight_force_update - tell the backlight subsystem that hardware state
  201. * has changed
  202. * @bd: the backlight device to update
  203. *
  204. * Updates the internal state of the backlight in response to a hardware event,
  205. * and generate a uevent to notify userspace
  206. */
  207. void backlight_force_update(struct backlight_device *bd,
  208. enum backlight_update_reason reason)
  209. {
  210. mutex_lock(&bd->ops_lock);
  211. if (bd->ops && bd->ops->get_brightness)
  212. bd->props.brightness = bd->ops->get_brightness(bd);
  213. mutex_unlock(&bd->ops_lock);
  214. backlight_generate_event(bd, reason);
  215. }
  216. EXPORT_SYMBOL(backlight_force_update);
  217. /**
  218. * backlight_device_register - create and register a new object of
  219. * backlight_device class.
  220. * @name: the name of the new object(must be the same as the name of the
  221. * respective framebuffer device).
  222. * @parent: a pointer to the parent device
  223. * @devdata: an optional pointer to be stored for private driver use. The
  224. * methods may retrieve it by using bl_get_data(bd).
  225. * @ops: the backlight operations structure.
  226. *
  227. * Creates and registers new backlight device. Returns either an
  228. * ERR_PTR() or a pointer to the newly allocated device.
  229. */
  230. struct backlight_device *backlight_device_register(const char *name,
  231. struct device *parent, void *devdata, const struct backlight_ops *ops)
  232. {
  233. struct backlight_device *new_bd;
  234. int rc;
  235. pr_debug("backlight_device_register: name=%s\n", name);
  236. new_bd = kzalloc(sizeof(struct backlight_device), GFP_KERNEL);
  237. if (!new_bd)
  238. return ERR_PTR(-ENOMEM);
  239. mutex_init(&new_bd->update_lock);
  240. mutex_init(&new_bd->ops_lock);
  241. new_bd->dev.class = backlight_class;
  242. new_bd->dev.parent = parent;
  243. new_bd->dev.release = bl_device_release;
  244. dev_set_name(&new_bd->dev, name);
  245. dev_set_drvdata(&new_bd->dev, devdata);
  246. rc = device_register(&new_bd->dev);
  247. if (rc) {
  248. kfree(new_bd);
  249. return ERR_PTR(rc);
  250. }
  251. rc = backlight_register_fb(new_bd);
  252. if (rc) {
  253. device_unregister(&new_bd->dev);
  254. return ERR_PTR(rc);
  255. }
  256. new_bd->ops = ops;
  257. #ifdef CONFIG_PMAC_BACKLIGHT
  258. mutex_lock(&pmac_backlight_mutex);
  259. if (!pmac_backlight)
  260. pmac_backlight = new_bd;
  261. mutex_unlock(&pmac_backlight_mutex);
  262. #endif
  263. return new_bd;
  264. }
  265. EXPORT_SYMBOL(backlight_device_register);
  266. /**
  267. * backlight_device_unregister - unregisters a backlight device object.
  268. * @bd: the backlight device object to be unregistered and freed.
  269. *
  270. * Unregisters a previously registered via backlight_device_register object.
  271. */
  272. void backlight_device_unregister(struct backlight_device *bd)
  273. {
  274. if (!bd)
  275. return;
  276. #ifdef CONFIG_PMAC_BACKLIGHT
  277. mutex_lock(&pmac_backlight_mutex);
  278. if (pmac_backlight == bd)
  279. pmac_backlight = NULL;
  280. mutex_unlock(&pmac_backlight_mutex);
  281. #endif
  282. mutex_lock(&bd->ops_lock);
  283. bd->ops = NULL;
  284. mutex_unlock(&bd->ops_lock);
  285. backlight_unregister_fb(bd);
  286. device_unregister(&bd->dev);
  287. }
  288. EXPORT_SYMBOL(backlight_device_unregister);
  289. static void __exit backlight_class_exit(void)
  290. {
  291. class_destroy(backlight_class);
  292. }
  293. static int __init backlight_class_init(void)
  294. {
  295. backlight_class = class_create(THIS_MODULE, "backlight");
  296. if (IS_ERR(backlight_class)) {
  297. printk(KERN_WARNING "Unable to create backlight class; errno = %ld\n",
  298. PTR_ERR(backlight_class));
  299. return PTR_ERR(backlight_class);
  300. }
  301. backlight_class->dev_attrs = bl_device_attributes;
  302. backlight_class->suspend = backlight_suspend;
  303. backlight_class->resume = backlight_resume;
  304. return 0;
  305. }
  306. /*
  307. * if this is compiled into the kernel, we need to ensure that the
  308. * class is registered before users of the class try to register lcd's
  309. */
  310. postcore_initcall(backlight_class_init);
  311. module_exit(backlight_class_exit);
  312. MODULE_LICENSE("GPL");
  313. MODULE_AUTHOR("Jamey Hicks <jamey.hicks@hp.com>, Andrew Zabolotny <zap@homelink.ru>");
  314. MODULE_DESCRIPTION("Backlight Lowlevel Control Abstraction");