backlight.c 9.6 KB

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