backlight.c 16 KB

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