backlight.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  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 struct list_head backlight_dev_list;
  21. static struct mutex backlight_dev_list_mutex;
  22. static struct blocking_notifier_head backlight_notifier;
  23. static const char *const backlight_types[] = {
  24. [BACKLIGHT_RAW] = "raw",
  25. [BACKLIGHT_PLATFORM] = "platform",
  26. [BACKLIGHT_FIRMWARE] = "firmware",
  27. };
  28. #if defined(CONFIG_FB) || (defined(CONFIG_FB_MODULE) && \
  29. defined(CONFIG_BACKLIGHT_CLASS_DEVICE_MODULE))
  30. /* This callback gets called when something important happens inside a
  31. * framebuffer driver. We're looking if that important event is blanking,
  32. * and if it is and necessary, we're switching backlight power as well ...
  33. */
  34. static int fb_notifier_callback(struct notifier_block *self,
  35. unsigned long event, void *data)
  36. {
  37. struct backlight_device *bd;
  38. struct fb_event *evdata = data;
  39. int node = evdata->info->node;
  40. int fb_blank = 0;
  41. /* If we aren't interested in this event, skip it immediately ... */
  42. if (event != FB_EVENT_BLANK && event != FB_EVENT_CONBLANK)
  43. return 0;
  44. bd = container_of(self, struct backlight_device, fb_notif);
  45. mutex_lock(&bd->ops_lock);
  46. if (bd->ops)
  47. if (!bd->ops->check_fb ||
  48. bd->ops->check_fb(bd, evdata->info)) {
  49. fb_blank = *(int *)evdata->data;
  50. if (fb_blank == FB_BLANK_UNBLANK &&
  51. !bd->fb_bl_on[node]) {
  52. bd->fb_bl_on[node] = true;
  53. if (!bd->use_count++) {
  54. bd->props.state &= ~BL_CORE_FBBLANK;
  55. bd->props.fb_blank = FB_BLANK_UNBLANK;
  56. backlight_update_status(bd);
  57. }
  58. } else if (fb_blank != FB_BLANK_UNBLANK &&
  59. bd->fb_bl_on[node]) {
  60. bd->fb_bl_on[node] = false;
  61. if (!(--bd->use_count)) {
  62. bd->props.state |= BL_CORE_FBBLANK;
  63. bd->props.fb_blank = fb_blank;
  64. backlight_update_status(bd);
  65. }
  66. }
  67. }
  68. mutex_unlock(&bd->ops_lock);
  69. return 0;
  70. }
  71. static int backlight_register_fb(struct backlight_device *bd)
  72. {
  73. memset(&bd->fb_notif, 0, sizeof(bd->fb_notif));
  74. bd->fb_notif.notifier_call = fb_notifier_callback;
  75. return fb_register_client(&bd->fb_notif);
  76. }
  77. static void backlight_unregister_fb(struct backlight_device *bd)
  78. {
  79. fb_unregister_client(&bd->fb_notif);
  80. }
  81. #else
  82. static inline int backlight_register_fb(struct backlight_device *bd)
  83. {
  84. return 0;
  85. }
  86. static inline void backlight_unregister_fb(struct backlight_device *bd)
  87. {
  88. }
  89. #endif /* CONFIG_FB */
  90. static void backlight_generate_event(struct backlight_device *bd,
  91. enum backlight_update_reason reason)
  92. {
  93. char *envp[2];
  94. switch (reason) {
  95. case BACKLIGHT_UPDATE_SYSFS:
  96. envp[0] = "SOURCE=sysfs";
  97. break;
  98. case BACKLIGHT_UPDATE_HOTKEY:
  99. envp[0] = "SOURCE=hotkey";
  100. break;
  101. default:
  102. envp[0] = "SOURCE=unknown";
  103. break;
  104. }
  105. envp[1] = NULL;
  106. kobject_uevent_env(&bd->dev.kobj, KOBJ_CHANGE, envp);
  107. sysfs_notify(&bd->dev.kobj, NULL, "actual_brightness");
  108. }
  109. static ssize_t bl_power_show(struct device *dev, struct device_attribute *attr,
  110. char *buf)
  111. {
  112. struct backlight_device *bd = to_backlight_device(dev);
  113. return sprintf(buf, "%d\n", bd->props.power);
  114. }
  115. static ssize_t bl_power_store(struct device *dev, struct device_attribute *attr,
  116. const char *buf, size_t count)
  117. {
  118. int rc;
  119. struct backlight_device *bd = to_backlight_device(dev);
  120. unsigned long power;
  121. rc = kstrtoul(buf, 0, &power);
  122. if (rc)
  123. return rc;
  124. rc = -ENXIO;
  125. mutex_lock(&bd->ops_lock);
  126. if (bd->ops) {
  127. pr_debug("set power to %lu\n", power);
  128. if (bd->props.power != power) {
  129. bd->props.power = power;
  130. backlight_update_status(bd);
  131. }
  132. rc = count;
  133. }
  134. mutex_unlock(&bd->ops_lock);
  135. return rc;
  136. }
  137. static DEVICE_ATTR_RW(bl_power);
  138. static ssize_t brightness_show(struct device *dev,
  139. struct device_attribute *attr, char *buf)
  140. {
  141. struct backlight_device *bd = to_backlight_device(dev);
  142. return sprintf(buf, "%d\n", bd->props.brightness);
  143. }
  144. int backlight_device_set_brightness(struct backlight_device *bd,
  145. unsigned long brightness)
  146. {
  147. int rc = -ENXIO;
  148. mutex_lock(&bd->ops_lock);
  149. if (bd->ops) {
  150. if (brightness > bd->props.max_brightness)
  151. rc = -EINVAL;
  152. else {
  153. pr_debug("set brightness to %lu\n", brightness);
  154. bd->props.brightness = brightness;
  155. backlight_update_status(bd);
  156. rc = 0;
  157. }
  158. }
  159. mutex_unlock(&bd->ops_lock);
  160. backlight_generate_event(bd, BACKLIGHT_UPDATE_SYSFS);
  161. return rc;
  162. }
  163. EXPORT_SYMBOL(backlight_device_set_brightness);
  164. static ssize_t brightness_store(struct device *dev,
  165. struct device_attribute *attr, const char *buf, size_t count)
  166. {
  167. int rc;
  168. struct backlight_device *bd = to_backlight_device(dev);
  169. unsigned long brightness;
  170. rc = kstrtoul(buf, 0, &brightness);
  171. if (rc)
  172. return rc;
  173. rc = backlight_device_set_brightness(bd, brightness);
  174. return rc ? rc : count;
  175. }
  176. static DEVICE_ATTR_RW(brightness);
  177. static ssize_t type_show(struct device *dev, struct device_attribute *attr,
  178. char *buf)
  179. {
  180. struct backlight_device *bd = to_backlight_device(dev);
  181. return sprintf(buf, "%s\n", backlight_types[bd->props.type]);
  182. }
  183. static DEVICE_ATTR_RO(type);
  184. static ssize_t max_brightness_show(struct device *dev,
  185. struct device_attribute *attr, char *buf)
  186. {
  187. struct backlight_device *bd = to_backlight_device(dev);
  188. return sprintf(buf, "%d\n", bd->props.max_brightness);
  189. }
  190. static DEVICE_ATTR_RO(max_brightness);
  191. static ssize_t actual_brightness_show(struct device *dev,
  192. struct device_attribute *attr, char *buf)
  193. {
  194. int rc = -ENXIO;
  195. struct backlight_device *bd = to_backlight_device(dev);
  196. mutex_lock(&bd->ops_lock);
  197. if (bd->ops && bd->ops->get_brightness)
  198. rc = sprintf(buf, "%d\n", bd->ops->get_brightness(bd));
  199. else
  200. rc = sprintf(buf, "%d\n", bd->props.brightness);
  201. mutex_unlock(&bd->ops_lock);
  202. return rc;
  203. }
  204. static DEVICE_ATTR_RO(actual_brightness);
  205. static struct class *backlight_class;
  206. #ifdef CONFIG_PM_SLEEP
  207. static int backlight_suspend(struct device *dev)
  208. {
  209. struct backlight_device *bd = to_backlight_device(dev);
  210. mutex_lock(&bd->ops_lock);
  211. if (bd->ops && bd->ops->options & BL_CORE_SUSPENDRESUME) {
  212. bd->props.state |= BL_CORE_SUSPENDED;
  213. backlight_update_status(bd);
  214. }
  215. mutex_unlock(&bd->ops_lock);
  216. return 0;
  217. }
  218. static int backlight_resume(struct device *dev)
  219. {
  220. struct backlight_device *bd = to_backlight_device(dev);
  221. mutex_lock(&bd->ops_lock);
  222. if (bd->ops && bd->ops->options & BL_CORE_SUSPENDRESUME) {
  223. bd->props.state &= ~BL_CORE_SUSPENDED;
  224. backlight_update_status(bd);
  225. }
  226. mutex_unlock(&bd->ops_lock);
  227. return 0;
  228. }
  229. #endif
  230. static SIMPLE_DEV_PM_OPS(backlight_class_dev_pm_ops, backlight_suspend,
  231. backlight_resume);
  232. static void bl_device_release(struct device *dev)
  233. {
  234. struct backlight_device *bd = to_backlight_device(dev);
  235. kfree(bd);
  236. }
  237. static struct attribute *bl_device_attrs[] = {
  238. &dev_attr_bl_power.attr,
  239. &dev_attr_brightness.attr,
  240. &dev_attr_actual_brightness.attr,
  241. &dev_attr_max_brightness.attr,
  242. &dev_attr_type.attr,
  243. NULL,
  244. };
  245. ATTRIBUTE_GROUPS(bl_device);
  246. /**
  247. * backlight_force_update - tell the backlight subsystem that hardware state
  248. * has changed
  249. * @bd: the backlight device to update
  250. *
  251. * Updates the internal state of the backlight in response to a hardware event,
  252. * and generate a uevent to notify userspace
  253. */
  254. void backlight_force_update(struct backlight_device *bd,
  255. enum backlight_update_reason reason)
  256. {
  257. mutex_lock(&bd->ops_lock);
  258. if (bd->ops && bd->ops->get_brightness)
  259. bd->props.brightness = bd->ops->get_brightness(bd);
  260. mutex_unlock(&bd->ops_lock);
  261. backlight_generate_event(bd, reason);
  262. }
  263. EXPORT_SYMBOL(backlight_force_update);
  264. /**
  265. * backlight_device_register - create and register a new object of
  266. * backlight_device class.
  267. * @name: the name of the new object(must be the same as the name of the
  268. * respective framebuffer device).
  269. * @parent: a pointer to the parent device
  270. * @devdata: an optional pointer to be stored for private driver use. The
  271. * methods may retrieve it by using bl_get_data(bd).
  272. * @ops: the backlight operations structure.
  273. *
  274. * Creates and registers new backlight device. Returns either an
  275. * ERR_PTR() or a pointer to the newly allocated device.
  276. */
  277. struct backlight_device *backlight_device_register(const char *name,
  278. struct device *parent, void *devdata, const struct backlight_ops *ops,
  279. const struct backlight_properties *props)
  280. {
  281. struct backlight_device *new_bd;
  282. int rc;
  283. pr_debug("backlight_device_register: name=%s\n", name);
  284. new_bd = kzalloc(sizeof(struct backlight_device), GFP_KERNEL);
  285. if (!new_bd)
  286. return ERR_PTR(-ENOMEM);
  287. mutex_init(&new_bd->update_lock);
  288. mutex_init(&new_bd->ops_lock);
  289. new_bd->dev.class = backlight_class;
  290. new_bd->dev.parent = parent;
  291. new_bd->dev.release = bl_device_release;
  292. dev_set_name(&new_bd->dev, "%s", name);
  293. dev_set_drvdata(&new_bd->dev, devdata);
  294. /* Set default properties */
  295. if (props) {
  296. memcpy(&new_bd->props, props,
  297. sizeof(struct backlight_properties));
  298. if (props->type <= 0 || props->type >= BACKLIGHT_TYPE_MAX) {
  299. WARN(1, "%s: invalid backlight type", name);
  300. new_bd->props.type = BACKLIGHT_RAW;
  301. }
  302. } else {
  303. new_bd->props.type = BACKLIGHT_RAW;
  304. }
  305. rc = device_register(&new_bd->dev);
  306. if (rc) {
  307. put_device(&new_bd->dev);
  308. return ERR_PTR(rc);
  309. }
  310. rc = backlight_register_fb(new_bd);
  311. if (rc) {
  312. device_unregister(&new_bd->dev);
  313. return ERR_PTR(rc);
  314. }
  315. new_bd->ops = ops;
  316. #ifdef CONFIG_PMAC_BACKLIGHT
  317. mutex_lock(&pmac_backlight_mutex);
  318. if (!pmac_backlight)
  319. pmac_backlight = new_bd;
  320. mutex_unlock(&pmac_backlight_mutex);
  321. #endif
  322. mutex_lock(&backlight_dev_list_mutex);
  323. list_add(&new_bd->entry, &backlight_dev_list);
  324. mutex_unlock(&backlight_dev_list_mutex);
  325. blocking_notifier_call_chain(&backlight_notifier,
  326. BACKLIGHT_REGISTERED, new_bd);
  327. return new_bd;
  328. }
  329. EXPORT_SYMBOL(backlight_device_register);
  330. struct backlight_device *backlight_device_get_by_type(enum backlight_type type)
  331. {
  332. bool found = false;
  333. struct backlight_device *bd;
  334. mutex_lock(&backlight_dev_list_mutex);
  335. list_for_each_entry(bd, &backlight_dev_list, entry) {
  336. if (bd->props.type == type) {
  337. found = true;
  338. break;
  339. }
  340. }
  341. mutex_unlock(&backlight_dev_list_mutex);
  342. return found ? bd : NULL;
  343. }
  344. EXPORT_SYMBOL(backlight_device_get_by_type);
  345. /**
  346. * backlight_device_unregister - unregisters a backlight device object.
  347. * @bd: the backlight device object to be unregistered and freed.
  348. *
  349. * Unregisters a previously registered via backlight_device_register object.
  350. */
  351. void backlight_device_unregister(struct backlight_device *bd)
  352. {
  353. if (!bd)
  354. return;
  355. mutex_lock(&backlight_dev_list_mutex);
  356. list_del(&bd->entry);
  357. mutex_unlock(&backlight_dev_list_mutex);
  358. #ifdef CONFIG_PMAC_BACKLIGHT
  359. mutex_lock(&pmac_backlight_mutex);
  360. if (pmac_backlight == bd)
  361. pmac_backlight = NULL;
  362. mutex_unlock(&pmac_backlight_mutex);
  363. #endif
  364. blocking_notifier_call_chain(&backlight_notifier,
  365. BACKLIGHT_UNREGISTERED, bd);
  366. mutex_lock(&bd->ops_lock);
  367. bd->ops = NULL;
  368. mutex_unlock(&bd->ops_lock);
  369. backlight_unregister_fb(bd);
  370. device_unregister(&bd->dev);
  371. }
  372. EXPORT_SYMBOL(backlight_device_unregister);
  373. static void devm_backlight_device_release(struct device *dev, void *res)
  374. {
  375. struct backlight_device *backlight = *(struct backlight_device **)res;
  376. backlight_device_unregister(backlight);
  377. }
  378. static int devm_backlight_device_match(struct device *dev, void *res,
  379. void *data)
  380. {
  381. struct backlight_device **r = res;
  382. return *r == data;
  383. }
  384. /**
  385. * backlight_register_notifier - get notified of backlight (un)registration
  386. * @nb: notifier block with the notifier to call on backlight (un)registration
  387. *
  388. * @return 0 on success, otherwise a negative error code
  389. *
  390. * Register a notifier to get notified when backlight devices get registered
  391. * or unregistered.
  392. */
  393. int backlight_register_notifier(struct notifier_block *nb)
  394. {
  395. return blocking_notifier_chain_register(&backlight_notifier, nb);
  396. }
  397. EXPORT_SYMBOL(backlight_register_notifier);
  398. /**
  399. * backlight_unregister_notifier - unregister a backlight notifier
  400. * @nb: notifier block to unregister
  401. *
  402. * @return 0 on success, otherwise a negative error code
  403. *
  404. * Register a notifier to get notified when backlight devices get registered
  405. * or unregistered.
  406. */
  407. int backlight_unregister_notifier(struct notifier_block *nb)
  408. {
  409. return blocking_notifier_chain_unregister(&backlight_notifier, nb);
  410. }
  411. EXPORT_SYMBOL(backlight_unregister_notifier);
  412. /**
  413. * devm_backlight_device_register - resource managed backlight_device_register()
  414. * @dev: the device to register
  415. * @name: the name of the device
  416. * @parent: a pointer to the parent device
  417. * @devdata: an optional pointer to be stored for private driver use
  418. * @ops: the backlight operations structure
  419. * @props: the backlight properties
  420. *
  421. * @return a struct backlight on success, or an ERR_PTR on error
  422. *
  423. * Managed backlight_device_register(). The backlight_device returned
  424. * from this function are automatically freed on driver detach.
  425. * See backlight_device_register() for more information.
  426. */
  427. struct backlight_device *devm_backlight_device_register(struct device *dev,
  428. const char *name, struct device *parent, void *devdata,
  429. const struct backlight_ops *ops,
  430. const struct backlight_properties *props)
  431. {
  432. struct backlight_device **ptr, *backlight;
  433. ptr = devres_alloc(devm_backlight_device_release, sizeof(*ptr),
  434. GFP_KERNEL);
  435. if (!ptr)
  436. return ERR_PTR(-ENOMEM);
  437. backlight = backlight_device_register(name, parent, devdata, ops,
  438. props);
  439. if (!IS_ERR(backlight)) {
  440. *ptr = backlight;
  441. devres_add(dev, ptr);
  442. } else {
  443. devres_free(ptr);
  444. }
  445. return backlight;
  446. }
  447. EXPORT_SYMBOL(devm_backlight_device_register);
  448. /**
  449. * devm_backlight_device_unregister - resource managed backlight_device_unregister()
  450. * @dev: the device to unregister
  451. * @bd: the backlight device to unregister
  452. *
  453. * Deallocated a backlight allocated with devm_backlight_device_register().
  454. * Normally this function will not need to be called and the resource management
  455. * code will ensure that the resource is freed.
  456. */
  457. void devm_backlight_device_unregister(struct device *dev,
  458. struct backlight_device *bd)
  459. {
  460. int rc;
  461. rc = devres_release(dev, devm_backlight_device_release,
  462. devm_backlight_device_match, bd);
  463. WARN_ON(rc);
  464. }
  465. EXPORT_SYMBOL(devm_backlight_device_unregister);
  466. #ifdef CONFIG_OF
  467. static int of_parent_match(struct device *dev, const void *data)
  468. {
  469. return dev->parent && dev->parent->of_node == data;
  470. }
  471. /**
  472. * of_find_backlight_by_node() - find backlight device by device-tree node
  473. * @node: device-tree node of the backlight device
  474. *
  475. * Returns a pointer to the backlight device corresponding to the given DT
  476. * node or NULL if no such backlight device exists or if the device hasn't
  477. * been probed yet.
  478. *
  479. * This function obtains a reference on the backlight device and it is the
  480. * caller's responsibility to drop the reference by calling put_device() on
  481. * the backlight device's .dev field.
  482. */
  483. struct backlight_device *of_find_backlight_by_node(struct device_node *node)
  484. {
  485. struct device *dev;
  486. dev = class_find_device(backlight_class, NULL, node, of_parent_match);
  487. return dev ? to_backlight_device(dev) : NULL;
  488. }
  489. EXPORT_SYMBOL(of_find_backlight_by_node);
  490. #endif
  491. static void __exit backlight_class_exit(void)
  492. {
  493. class_destroy(backlight_class);
  494. }
  495. static int __init backlight_class_init(void)
  496. {
  497. backlight_class = class_create(THIS_MODULE, "backlight");
  498. if (IS_ERR(backlight_class)) {
  499. pr_warn("Unable to create backlight class; errno = %ld\n",
  500. PTR_ERR(backlight_class));
  501. return PTR_ERR(backlight_class);
  502. }
  503. backlight_class->dev_groups = bl_device_groups;
  504. backlight_class->pm = &backlight_class_dev_pm_ops;
  505. INIT_LIST_HEAD(&backlight_dev_list);
  506. mutex_init(&backlight_dev_list_mutex);
  507. BLOCKING_INIT_NOTIFIER_HEAD(&backlight_notifier);
  508. return 0;
  509. }
  510. /*
  511. * if this is compiled into the kernel, we need to ensure that the
  512. * class is registered before users of the class try to register lcd's
  513. */
  514. postcore_initcall(backlight_class_init);
  515. module_exit(backlight_class_exit);
  516. MODULE_LICENSE("GPL");
  517. MODULE_AUTHOR("Jamey Hicks <jamey.hicks@hp.com>, Andrew Zabolotny <zap@homelink.ru>");
  518. MODULE_DESCRIPTION("Backlight Lowlevel Control Abstraction");