backlight.c 16 KB

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