backlight.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. /*
  2. * Backlight Lowlevel Control Abstraction
  3. *
  4. * Copyright (C) 2003,2004 Hewlett-Packard Company
  5. *
  6. */
  7. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  8. #include <linux/module.h>
  9. #include <linux/init.h>
  10. #include <linux/device.h>
  11. #include <linux/backlight.h>
  12. #include <linux/notifier.h>
  13. #include <linux/ctype.h>
  14. #include <linux/err.h>
  15. #include <linux/fb.h>
  16. #include <linux/slab.h>
  17. #ifdef CONFIG_PMAC_BACKLIGHT
  18. #include <asm/backlight.h>
  19. #endif
  20. static const char *const backlight_types[] = {
  21. [BACKLIGHT_RAW] = "raw",
  22. [BACKLIGHT_PLATFORM] = "platform",
  23. [BACKLIGHT_FIRMWARE] = "firmware",
  24. };
  25. #if defined(CONFIG_FB) || (defined(CONFIG_FB_MODULE) && \
  26. defined(CONFIG_BACKLIGHT_CLASS_DEVICE_MODULE))
  27. /* This callback gets called when something important happens inside a
  28. * framebuffer driver. We're looking if that important event is blanking,
  29. * and if it is, we're switching backlight power as well ...
  30. */
  31. static int fb_notifier_callback(struct notifier_block *self,
  32. unsigned long event, void *data)
  33. {
  34. struct backlight_device *bd;
  35. struct fb_event *evdata = data;
  36. /* If we aren't interested in this event, skip it immediately ... */
  37. if (event != FB_EVENT_BLANK && event != FB_EVENT_CONBLANK)
  38. return 0;
  39. bd = container_of(self, struct backlight_device, fb_notif);
  40. mutex_lock(&bd->ops_lock);
  41. if (bd->ops)
  42. if (!bd->ops->check_fb ||
  43. bd->ops->check_fb(bd, evdata->info)) {
  44. bd->props.fb_blank = *(int *)evdata->data;
  45. if (bd->props.fb_blank == FB_BLANK_UNBLANK)
  46. bd->props.state &= ~BL_CORE_FBBLANK;
  47. else
  48. bd->props.state |= BL_CORE_FBBLANK;
  49. backlight_update_status(bd);
  50. }
  51. mutex_unlock(&bd->ops_lock);
  52. return 0;
  53. }
  54. static int backlight_register_fb(struct backlight_device *bd)
  55. {
  56. memset(&bd->fb_notif, 0, sizeof(bd->fb_notif));
  57. bd->fb_notif.notifier_call = fb_notifier_callback;
  58. return fb_register_client(&bd->fb_notif);
  59. }
  60. static void backlight_unregister_fb(struct backlight_device *bd)
  61. {
  62. fb_unregister_client(&bd->fb_notif);
  63. }
  64. #else
  65. static inline int backlight_register_fb(struct backlight_device *bd)
  66. {
  67. return 0;
  68. }
  69. static inline void backlight_unregister_fb(struct backlight_device *bd)
  70. {
  71. }
  72. #endif /* CONFIG_FB */
  73. static void backlight_generate_event(struct backlight_device *bd,
  74. enum backlight_update_reason reason)
  75. {
  76. char *envp[2];
  77. switch (reason) {
  78. case BACKLIGHT_UPDATE_SYSFS:
  79. envp[0] = "SOURCE=sysfs";
  80. break;
  81. case BACKLIGHT_UPDATE_HOTKEY:
  82. envp[0] = "SOURCE=hotkey";
  83. break;
  84. default:
  85. envp[0] = "SOURCE=unknown";
  86. break;
  87. }
  88. envp[1] = NULL;
  89. kobject_uevent_env(&bd->dev.kobj, KOBJ_CHANGE, envp);
  90. sysfs_notify(&bd->dev.kobj, NULL, "actual_brightness");
  91. }
  92. static ssize_t backlight_show_power(struct device *dev,
  93. struct device_attribute *attr, char *buf)
  94. {
  95. struct backlight_device *bd = to_backlight_device(dev);
  96. return sprintf(buf, "%d\n", bd->props.power);
  97. }
  98. static ssize_t backlight_store_power(struct device *dev,
  99. struct device_attribute *attr, const char *buf, size_t count)
  100. {
  101. int rc;
  102. struct backlight_device *bd = to_backlight_device(dev);
  103. unsigned long power;
  104. rc = kstrtoul(buf, 0, &power);
  105. if (rc)
  106. return rc;
  107. rc = -ENXIO;
  108. mutex_lock(&bd->ops_lock);
  109. if (bd->ops) {
  110. pr_debug("set power to %lu\n", power);
  111. if (bd->props.power != power) {
  112. bd->props.power = power;
  113. backlight_update_status(bd);
  114. }
  115. rc = count;
  116. }
  117. mutex_unlock(&bd->ops_lock);
  118. return rc;
  119. }
  120. static ssize_t backlight_show_brightness(struct device *dev,
  121. struct device_attribute *attr, char *buf)
  122. {
  123. struct backlight_device *bd = to_backlight_device(dev);
  124. return sprintf(buf, "%d\n", bd->props.brightness);
  125. }
  126. static ssize_t backlight_store_brightness(struct device *dev,
  127. struct device_attribute *attr, const char *buf, size_t count)
  128. {
  129. int rc;
  130. struct backlight_device *bd = to_backlight_device(dev);
  131. unsigned long brightness;
  132. rc = kstrtoul(buf, 0, &brightness);
  133. if (rc)
  134. return rc;
  135. rc = -ENXIO;
  136. mutex_lock(&bd->ops_lock);
  137. if (bd->ops) {
  138. if (brightness > bd->props.max_brightness)
  139. rc = -EINVAL;
  140. else {
  141. pr_debug("set brightness to %lu\n", 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. #ifdef CONFIG_PM_SLEEP
  176. static int backlight_suspend(struct device *dev)
  177. {
  178. struct backlight_device *bd = to_backlight_device(dev);
  179. mutex_lock(&bd->ops_lock);
  180. if (bd->ops && bd->ops->options & BL_CORE_SUSPENDRESUME) {
  181. bd->props.state |= BL_CORE_SUSPENDED;
  182. backlight_update_status(bd);
  183. }
  184. mutex_unlock(&bd->ops_lock);
  185. return 0;
  186. }
  187. static int backlight_resume(struct device *dev)
  188. {
  189. struct backlight_device *bd = to_backlight_device(dev);
  190. mutex_lock(&bd->ops_lock);
  191. if (bd->ops && bd->ops->options & BL_CORE_SUSPENDRESUME) {
  192. bd->props.state &= ~BL_CORE_SUSPENDED;
  193. backlight_update_status(bd);
  194. }
  195. mutex_unlock(&bd->ops_lock);
  196. return 0;
  197. }
  198. #endif
  199. static SIMPLE_DEV_PM_OPS(backlight_class_dev_pm_ops, backlight_suspend,
  200. backlight_resume);
  201. static void bl_device_release(struct device *dev)
  202. {
  203. struct backlight_device *bd = to_backlight_device(dev);
  204. kfree(bd);
  205. }
  206. static struct device_attribute bl_device_attributes[] = {
  207. __ATTR(bl_power, 0644, backlight_show_power, backlight_store_power),
  208. __ATTR(brightness, 0644, backlight_show_brightness,
  209. backlight_store_brightness),
  210. __ATTR(actual_brightness, 0444, backlight_show_actual_brightness,
  211. NULL),
  212. __ATTR(max_brightness, 0444, backlight_show_max_brightness, NULL),
  213. __ATTR(type, 0444, backlight_show_type, NULL),
  214. __ATTR_NULL,
  215. };
  216. /**
  217. * backlight_force_update - tell the backlight subsystem that hardware state
  218. * has changed
  219. * @bd: the backlight device to update
  220. *
  221. * Updates the internal state of the backlight in response to a hardware event,
  222. * and generate a uevent to notify userspace
  223. */
  224. void backlight_force_update(struct backlight_device *bd,
  225. enum backlight_update_reason reason)
  226. {
  227. mutex_lock(&bd->ops_lock);
  228. if (bd->ops && bd->ops->get_brightness)
  229. bd->props.brightness = bd->ops->get_brightness(bd);
  230. mutex_unlock(&bd->ops_lock);
  231. backlight_generate_event(bd, reason);
  232. }
  233. EXPORT_SYMBOL(backlight_force_update);
  234. /**
  235. * backlight_device_register - create and register a new object of
  236. * backlight_device class.
  237. * @name: the name of the new object(must be the same as the name of the
  238. * respective framebuffer device).
  239. * @parent: a pointer to the parent device
  240. * @devdata: an optional pointer to be stored for private driver use. The
  241. * methods may retrieve it by using bl_get_data(bd).
  242. * @ops: the backlight operations structure.
  243. *
  244. * Creates and registers new backlight device. Returns either an
  245. * ERR_PTR() or a pointer to the newly allocated device.
  246. */
  247. struct backlight_device *backlight_device_register(const char *name,
  248. struct device *parent, void *devdata, const struct backlight_ops *ops,
  249. const struct backlight_properties *props)
  250. {
  251. struct backlight_device *new_bd;
  252. int rc;
  253. pr_debug("backlight_device_register: name=%s\n", name);
  254. new_bd = kzalloc(sizeof(struct backlight_device), GFP_KERNEL);
  255. if (!new_bd)
  256. return ERR_PTR(-ENOMEM);
  257. mutex_init(&new_bd->update_lock);
  258. mutex_init(&new_bd->ops_lock);
  259. new_bd->dev.class = backlight_class;
  260. new_bd->dev.parent = parent;
  261. new_bd->dev.release = bl_device_release;
  262. dev_set_name(&new_bd->dev, "%s", name);
  263. dev_set_drvdata(&new_bd->dev, devdata);
  264. /* Set default properties */
  265. if (props) {
  266. memcpy(&new_bd->props, props,
  267. sizeof(struct backlight_properties));
  268. if (props->type <= 0 || props->type >= BACKLIGHT_TYPE_MAX) {
  269. WARN(1, "%s: invalid backlight type", name);
  270. new_bd->props.type = BACKLIGHT_RAW;
  271. }
  272. } else {
  273. new_bd->props.type = BACKLIGHT_RAW;
  274. }
  275. rc = device_register(&new_bd->dev);
  276. if (rc) {
  277. kfree(new_bd);
  278. return ERR_PTR(rc);
  279. }
  280. rc = backlight_register_fb(new_bd);
  281. if (rc) {
  282. device_unregister(&new_bd->dev);
  283. return ERR_PTR(rc);
  284. }
  285. new_bd->ops = ops;
  286. #ifdef CONFIG_PMAC_BACKLIGHT
  287. mutex_lock(&pmac_backlight_mutex);
  288. if (!pmac_backlight)
  289. pmac_backlight = new_bd;
  290. mutex_unlock(&pmac_backlight_mutex);
  291. #endif
  292. return new_bd;
  293. }
  294. EXPORT_SYMBOL(backlight_device_register);
  295. /**
  296. * backlight_device_unregister - unregisters a backlight device object.
  297. * @bd: the backlight device object to be unregistered and freed.
  298. *
  299. * Unregisters a previously registered via backlight_device_register object.
  300. */
  301. void backlight_device_unregister(struct backlight_device *bd)
  302. {
  303. if (!bd)
  304. return;
  305. #ifdef CONFIG_PMAC_BACKLIGHT
  306. mutex_lock(&pmac_backlight_mutex);
  307. if (pmac_backlight == bd)
  308. pmac_backlight = NULL;
  309. mutex_unlock(&pmac_backlight_mutex);
  310. #endif
  311. mutex_lock(&bd->ops_lock);
  312. bd->ops = NULL;
  313. mutex_unlock(&bd->ops_lock);
  314. backlight_unregister_fb(bd);
  315. device_unregister(&bd->dev);
  316. }
  317. EXPORT_SYMBOL(backlight_device_unregister);
  318. static void devm_backlight_device_release(struct device *dev, void *res)
  319. {
  320. struct backlight_device *backlight = *(struct backlight_device **)res;
  321. backlight_device_unregister(backlight);
  322. }
  323. static int devm_backlight_device_match(struct device *dev, void *res,
  324. void *data)
  325. {
  326. struct backlight_device **r = res;
  327. return *r == data;
  328. }
  329. /**
  330. * devm_backlight_device_register - resource managed backlight_device_register()
  331. * @dev: the device to register
  332. * @name: the name of the device
  333. * @parent: a pointer to the parent device
  334. * @devdata: an optional pointer to be stored for private driver use
  335. * @ops: the backlight operations structure
  336. * @props: the backlight properties
  337. *
  338. * @return a struct backlight on success, or an ERR_PTR on error
  339. *
  340. * Managed backlight_device_register(). The backlight_device returned
  341. * from this function are automatically freed on driver detach.
  342. * See backlight_device_register() for more information.
  343. */
  344. struct backlight_device *devm_backlight_device_register(struct device *dev,
  345. const char *name, struct device *parent, void *devdata,
  346. const struct backlight_ops *ops,
  347. const struct backlight_properties *props)
  348. {
  349. struct backlight_device **ptr, *backlight;
  350. ptr = devres_alloc(devm_backlight_device_release, sizeof(*ptr),
  351. GFP_KERNEL);
  352. if (!ptr)
  353. return ERR_PTR(-ENOMEM);
  354. backlight = backlight_device_register(name, parent, devdata, ops,
  355. props);
  356. if (!IS_ERR(backlight)) {
  357. *ptr = backlight;
  358. devres_add(dev, ptr);
  359. } else {
  360. devres_free(ptr);
  361. }
  362. return backlight;
  363. }
  364. EXPORT_SYMBOL(devm_backlight_device_register);
  365. /**
  366. * devm_backlight_device_unregister - resource managed backlight_device_unregister()
  367. * @dev: the device to unregister
  368. * @bd: the backlight device to unregister
  369. *
  370. * Deallocated a backlight allocated with devm_backlight_device_register().
  371. * Normally this function will not need to be called and the resource management
  372. * code will ensure that the resource is freed.
  373. */
  374. void devm_backlight_device_unregister(struct device *dev,
  375. struct backlight_device *bd)
  376. {
  377. int rc;
  378. rc = devres_release(dev, devm_backlight_device_release,
  379. devm_backlight_device_match, bd);
  380. WARN_ON(rc);
  381. }
  382. EXPORT_SYMBOL(devm_backlight_device_unregister);
  383. #ifdef CONFIG_OF
  384. static int of_parent_match(struct device *dev, const void *data)
  385. {
  386. return dev->parent && dev->parent->of_node == data;
  387. }
  388. /**
  389. * of_find_backlight_by_node() - find backlight device by device-tree node
  390. * @node: device-tree node of the backlight device
  391. *
  392. * Returns a pointer to the backlight device corresponding to the given DT
  393. * node or NULL if no such backlight device exists or if the device hasn't
  394. * been probed yet.
  395. *
  396. * This function obtains a reference on the backlight device and it is the
  397. * caller's responsibility to drop the reference by calling put_device() on
  398. * the backlight device's .dev field.
  399. */
  400. struct backlight_device *of_find_backlight_by_node(struct device_node *node)
  401. {
  402. struct device *dev;
  403. dev = class_find_device(backlight_class, NULL, node, of_parent_match);
  404. return dev ? to_backlight_device(dev) : NULL;
  405. }
  406. EXPORT_SYMBOL(of_find_backlight_by_node);
  407. #endif
  408. static void __exit backlight_class_exit(void)
  409. {
  410. class_destroy(backlight_class);
  411. }
  412. static int __init backlight_class_init(void)
  413. {
  414. backlight_class = class_create(THIS_MODULE, "backlight");
  415. if (IS_ERR(backlight_class)) {
  416. pr_warn("Unable to create backlight class; errno = %ld\n",
  417. PTR_ERR(backlight_class));
  418. return PTR_ERR(backlight_class);
  419. }
  420. backlight_class->dev_attrs = bl_device_attributes;
  421. backlight_class->pm = &backlight_class_dev_pm_ops;
  422. return 0;
  423. }
  424. /*
  425. * if this is compiled into the kernel, we need to ensure that the
  426. * class is registered before users of the class try to register lcd's
  427. */
  428. postcore_initcall(backlight_class_init);
  429. module_exit(backlight_class_exit);
  430. MODULE_LICENSE("GPL");
  431. MODULE_AUTHOR("Jamey Hicks <jamey.hicks@hp.com>, Andrew Zabolotny <zap@homelink.ru>");
  432. MODULE_DESCRIPTION("Backlight Lowlevel Control Abstraction");