backlight.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  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 bl_power_show(struct device *dev, struct device_attribute *attr,
  93. 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 bl_power_store(struct device *dev, struct device_attribute *attr,
  99. 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 DEVICE_ATTR_RW(bl_power);
  121. static ssize_t brightness_show(struct device *dev,
  122. struct device_attribute *attr, char *buf)
  123. {
  124. struct backlight_device *bd = to_backlight_device(dev);
  125. return sprintf(buf, "%d\n", bd->props.brightness);
  126. }
  127. static ssize_t brightness_store(struct device *dev,
  128. struct device_attribute *attr, const char *buf, size_t count)
  129. {
  130. int rc;
  131. struct backlight_device *bd = to_backlight_device(dev);
  132. unsigned long brightness;
  133. rc = kstrtoul(buf, 0, &brightness);
  134. if (rc)
  135. return rc;
  136. rc = -ENXIO;
  137. mutex_lock(&bd->ops_lock);
  138. if (bd->ops) {
  139. if (brightness > bd->props.max_brightness)
  140. rc = -EINVAL;
  141. else {
  142. pr_debug("set brightness to %lu\n", brightness);
  143. bd->props.brightness = brightness;
  144. backlight_update_status(bd);
  145. rc = count;
  146. }
  147. }
  148. mutex_unlock(&bd->ops_lock);
  149. backlight_generate_event(bd, BACKLIGHT_UPDATE_SYSFS);
  150. return rc;
  151. }
  152. static DEVICE_ATTR_RW(brightness);
  153. static ssize_t type_show(struct device *dev, struct device_attribute *attr,
  154. char *buf)
  155. {
  156. struct backlight_device *bd = to_backlight_device(dev);
  157. return sprintf(buf, "%s\n", backlight_types[bd->props.type]);
  158. }
  159. static DEVICE_ATTR_RO(type);
  160. static ssize_t max_brightness_show(struct device *dev,
  161. struct device_attribute *attr, char *buf)
  162. {
  163. struct backlight_device *bd = to_backlight_device(dev);
  164. return sprintf(buf, "%d\n", bd->props.max_brightness);
  165. }
  166. static DEVICE_ATTR_RO(max_brightness);
  167. static ssize_t actual_brightness_show(struct device *dev,
  168. struct device_attribute *attr, char *buf)
  169. {
  170. int rc = -ENXIO;
  171. struct backlight_device *bd = to_backlight_device(dev);
  172. mutex_lock(&bd->ops_lock);
  173. if (bd->ops && bd->ops->get_brightness)
  174. rc = sprintf(buf, "%d\n", bd->ops->get_brightness(bd));
  175. mutex_unlock(&bd->ops_lock);
  176. return rc;
  177. }
  178. static DEVICE_ATTR_RO(actual_brightness);
  179. static struct class *backlight_class;
  180. #ifdef CONFIG_PM_SLEEP
  181. static int backlight_suspend(struct device *dev)
  182. {
  183. struct backlight_device *bd = to_backlight_device(dev);
  184. mutex_lock(&bd->ops_lock);
  185. if (bd->ops && bd->ops->options & BL_CORE_SUSPENDRESUME) {
  186. bd->props.state |= BL_CORE_SUSPENDED;
  187. backlight_update_status(bd);
  188. }
  189. mutex_unlock(&bd->ops_lock);
  190. return 0;
  191. }
  192. static int backlight_resume(struct device *dev)
  193. {
  194. struct backlight_device *bd = to_backlight_device(dev);
  195. mutex_lock(&bd->ops_lock);
  196. if (bd->ops && bd->ops->options & BL_CORE_SUSPENDRESUME) {
  197. bd->props.state &= ~BL_CORE_SUSPENDED;
  198. backlight_update_status(bd);
  199. }
  200. mutex_unlock(&bd->ops_lock);
  201. return 0;
  202. }
  203. #endif
  204. static SIMPLE_DEV_PM_OPS(backlight_class_dev_pm_ops, backlight_suspend,
  205. backlight_resume);
  206. static void bl_device_release(struct device *dev)
  207. {
  208. struct backlight_device *bd = to_backlight_device(dev);
  209. kfree(bd);
  210. }
  211. static struct attribute *bl_device_attrs[] = {
  212. &dev_attr_bl_power.attr,
  213. &dev_attr_brightness.attr,
  214. &dev_attr_actual_brightness.attr,
  215. &dev_attr_max_brightness.attr,
  216. &dev_attr_type.attr,
  217. NULL,
  218. };
  219. ATTRIBUTE_GROUPS(bl_device);
  220. /**
  221. * backlight_force_update - tell the backlight subsystem that hardware state
  222. * has changed
  223. * @bd: the backlight device to update
  224. *
  225. * Updates the internal state of the backlight in response to a hardware event,
  226. * and generate a uevent to notify userspace
  227. */
  228. void backlight_force_update(struct backlight_device *bd,
  229. enum backlight_update_reason reason)
  230. {
  231. mutex_lock(&bd->ops_lock);
  232. if (bd->ops && bd->ops->get_brightness)
  233. bd->props.brightness = bd->ops->get_brightness(bd);
  234. mutex_unlock(&bd->ops_lock);
  235. backlight_generate_event(bd, reason);
  236. }
  237. EXPORT_SYMBOL(backlight_force_update);
  238. /**
  239. * backlight_device_register - create and register a new object of
  240. * backlight_device class.
  241. * @name: the name of the new object(must be the same as the name of the
  242. * respective framebuffer device).
  243. * @parent: a pointer to the parent device
  244. * @devdata: an optional pointer to be stored for private driver use. The
  245. * methods may retrieve it by using bl_get_data(bd).
  246. * @ops: the backlight operations structure.
  247. *
  248. * Creates and registers new backlight device. Returns either an
  249. * ERR_PTR() or a pointer to the newly allocated device.
  250. */
  251. struct backlight_device *backlight_device_register(const char *name,
  252. struct device *parent, void *devdata, const struct backlight_ops *ops,
  253. const struct backlight_properties *props)
  254. {
  255. struct backlight_device *new_bd;
  256. int rc;
  257. pr_debug("backlight_device_register: name=%s\n", name);
  258. new_bd = kzalloc(sizeof(struct backlight_device), GFP_KERNEL);
  259. if (!new_bd)
  260. return ERR_PTR(-ENOMEM);
  261. mutex_init(&new_bd->update_lock);
  262. mutex_init(&new_bd->ops_lock);
  263. new_bd->dev.class = backlight_class;
  264. new_bd->dev.parent = parent;
  265. new_bd->dev.release = bl_device_release;
  266. dev_set_name(&new_bd->dev, "%s", name);
  267. dev_set_drvdata(&new_bd->dev, devdata);
  268. /* Set default properties */
  269. if (props) {
  270. memcpy(&new_bd->props, props,
  271. sizeof(struct backlight_properties));
  272. if (props->type <= 0 || props->type >= BACKLIGHT_TYPE_MAX) {
  273. WARN(1, "%s: invalid backlight type", name);
  274. new_bd->props.type = BACKLIGHT_RAW;
  275. }
  276. } else {
  277. new_bd->props.type = BACKLIGHT_RAW;
  278. }
  279. rc = device_register(&new_bd->dev);
  280. if (rc) {
  281. kfree(new_bd);
  282. return ERR_PTR(rc);
  283. }
  284. rc = backlight_register_fb(new_bd);
  285. if (rc) {
  286. device_unregister(&new_bd->dev);
  287. return ERR_PTR(rc);
  288. }
  289. new_bd->ops = ops;
  290. #ifdef CONFIG_PMAC_BACKLIGHT
  291. mutex_lock(&pmac_backlight_mutex);
  292. if (!pmac_backlight)
  293. pmac_backlight = new_bd;
  294. mutex_unlock(&pmac_backlight_mutex);
  295. #endif
  296. return new_bd;
  297. }
  298. EXPORT_SYMBOL(backlight_device_register);
  299. /**
  300. * backlight_device_unregister - unregisters a backlight device object.
  301. * @bd: the backlight device object to be unregistered and freed.
  302. *
  303. * Unregisters a previously registered via backlight_device_register object.
  304. */
  305. void backlight_device_unregister(struct backlight_device *bd)
  306. {
  307. if (!bd)
  308. return;
  309. #ifdef CONFIG_PMAC_BACKLIGHT
  310. mutex_lock(&pmac_backlight_mutex);
  311. if (pmac_backlight == bd)
  312. pmac_backlight = NULL;
  313. mutex_unlock(&pmac_backlight_mutex);
  314. #endif
  315. mutex_lock(&bd->ops_lock);
  316. bd->ops = NULL;
  317. mutex_unlock(&bd->ops_lock);
  318. backlight_unregister_fb(bd);
  319. device_unregister(&bd->dev);
  320. }
  321. EXPORT_SYMBOL(backlight_device_unregister);
  322. static void devm_backlight_device_release(struct device *dev, void *res)
  323. {
  324. struct backlight_device *backlight = *(struct backlight_device **)res;
  325. backlight_device_unregister(backlight);
  326. }
  327. static int devm_backlight_device_match(struct device *dev, void *res,
  328. void *data)
  329. {
  330. struct backlight_device **r = res;
  331. return *r == data;
  332. }
  333. /**
  334. * devm_backlight_device_register - resource managed backlight_device_register()
  335. * @dev: the device to register
  336. * @name: the name of the device
  337. * @parent: a pointer to the parent device
  338. * @devdata: an optional pointer to be stored for private driver use
  339. * @ops: the backlight operations structure
  340. * @props: the backlight properties
  341. *
  342. * @return a struct backlight on success, or an ERR_PTR on error
  343. *
  344. * Managed backlight_device_register(). The backlight_device returned
  345. * from this function are automatically freed on driver detach.
  346. * See backlight_device_register() for more information.
  347. */
  348. struct backlight_device *devm_backlight_device_register(struct device *dev,
  349. const char *name, struct device *parent, void *devdata,
  350. const struct backlight_ops *ops,
  351. const struct backlight_properties *props)
  352. {
  353. struct backlight_device **ptr, *backlight;
  354. ptr = devres_alloc(devm_backlight_device_release, sizeof(*ptr),
  355. GFP_KERNEL);
  356. if (!ptr)
  357. return ERR_PTR(-ENOMEM);
  358. backlight = backlight_device_register(name, parent, devdata, ops,
  359. props);
  360. if (!IS_ERR(backlight)) {
  361. *ptr = backlight;
  362. devres_add(dev, ptr);
  363. } else {
  364. devres_free(ptr);
  365. }
  366. return backlight;
  367. }
  368. EXPORT_SYMBOL(devm_backlight_device_register);
  369. /**
  370. * devm_backlight_device_unregister - resource managed backlight_device_unregister()
  371. * @dev: the device to unregister
  372. * @bd: the backlight device to unregister
  373. *
  374. * Deallocated a backlight allocated with devm_backlight_device_register().
  375. * Normally this function will not need to be called and the resource management
  376. * code will ensure that the resource is freed.
  377. */
  378. void devm_backlight_device_unregister(struct device *dev,
  379. struct backlight_device *bd)
  380. {
  381. int rc;
  382. rc = devres_release(dev, devm_backlight_device_release,
  383. devm_backlight_device_match, bd);
  384. WARN_ON(rc);
  385. }
  386. EXPORT_SYMBOL(devm_backlight_device_unregister);
  387. #ifdef CONFIG_OF
  388. static int of_parent_match(struct device *dev, const void *data)
  389. {
  390. return dev->parent && dev->parent->of_node == data;
  391. }
  392. /**
  393. * of_find_backlight_by_node() - find backlight device by device-tree node
  394. * @node: device-tree node of the backlight device
  395. *
  396. * Returns a pointer to the backlight device corresponding to the given DT
  397. * node or NULL if no such backlight device exists or if the device hasn't
  398. * been probed yet.
  399. *
  400. * This function obtains a reference on the backlight device and it is the
  401. * caller's responsibility to drop the reference by calling put_device() on
  402. * the backlight device's .dev field.
  403. */
  404. struct backlight_device *of_find_backlight_by_node(struct device_node *node)
  405. {
  406. struct device *dev;
  407. dev = class_find_device(backlight_class, NULL, node, of_parent_match);
  408. return dev ? to_backlight_device(dev) : NULL;
  409. }
  410. EXPORT_SYMBOL(of_find_backlight_by_node);
  411. #endif
  412. static void __exit backlight_class_exit(void)
  413. {
  414. class_destroy(backlight_class);
  415. }
  416. static int __init backlight_class_init(void)
  417. {
  418. backlight_class = class_create(THIS_MODULE, "backlight");
  419. if (IS_ERR(backlight_class)) {
  420. pr_warn("Unable to create backlight class; errno = %ld\n",
  421. PTR_ERR(backlight_class));
  422. return PTR_ERR(backlight_class);
  423. }
  424. backlight_class->dev_groups = bl_device_groups;
  425. backlight_class->pm = &backlight_class_dev_pm_ops;
  426. return 0;
  427. }
  428. /*
  429. * if this is compiled into the kernel, we need to ensure that the
  430. * class is registered before users of the class try to register lcd's
  431. */
  432. postcore_initcall(backlight_class_init);
  433. module_exit(backlight_class_exit);
  434. MODULE_LICENSE("GPL");
  435. MODULE_AUTHOR("Jamey Hicks <jamey.hicks@hp.com>, Andrew Zabolotny <zap@homelink.ru>");
  436. MODULE_DESCRIPTION("Backlight Lowlevel Control Abstraction");