backlight.c 10 KB

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