backlight.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Backlight Lowlevel Control Abstraction
  4. *
  5. * Copyright (C) 2003,2004 Hewlett-Packard Company
  6. *
  7. */
  8. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  9. #include <linux/module.h>
  10. #include <linux/init.h>
  11. #include <linux/device.h>
  12. #include <linux/backlight.h>
  13. #include <linux/notifier.h>
  14. #include <linux/ctype.h>
  15. #include <linux/err.h>
  16. #include <linux/fb.h>
  17. #include <linux/slab.h>
  18. #ifdef CONFIG_PMAC_BACKLIGHT
  19. #include <asm/backlight.h>
  20. #endif
  21. /**
  22. * DOC: overview
  23. *
  24. * The backlight core supports implementing backlight drivers.
  25. *
  26. * A backlight driver registers a driver using
  27. * devm_backlight_device_register(). The properties of the backlight
  28. * driver such as type and max_brightness must be specified.
  29. * When the core detect changes in for example brightness or power state
  30. * the update_status() operation is called. The backlight driver shall
  31. * implement this operation and use it to adjust backlight.
  32. *
  33. * Several sysfs attributes are provided by the backlight core::
  34. *
  35. * - brightness R/W, set the requested brightness level
  36. * - actual_brightness RO, the brightness level used by the HW
  37. * - max_brightness RO, the maximum brightness level supported
  38. *
  39. * See Documentation/ABI/stable/sysfs-class-backlight for the full list.
  40. *
  41. * The backlight can be adjusted using the sysfs interface, and
  42. * the backlight driver may also support adjusting backlight using
  43. * a hot-key or some other platform or firmware specific way.
  44. *
  45. * The driver must implement the get_brightness() operation if
  46. * the HW do not support all the levels that can be specified in
  47. * brightness, thus providing user-space access to the actual level
  48. * via the actual_brightness attribute.
  49. *
  50. * When the backlight changes this is reported to user-space using
  51. * an uevent connected to the actual_brightness attribute.
  52. * When brightness is set by platform specific means, for example
  53. * a hot-key to adjust backlight, the driver must notify the backlight
  54. * core that brightness has changed using backlight_force_update().
  55. *
  56. * The backlight driver core receives notifications from fbdev and
  57. * if the event is FB_EVENT_BLANK and if the value of blank, from the
  58. * FBIOBLANK ioctrl, results in a change in the backlight state the
  59. * update_status() operation is called.
  60. */
  61. static struct list_head backlight_dev_list;
  62. static struct mutex backlight_dev_list_mutex;
  63. static struct blocking_notifier_head backlight_notifier;
  64. static const char *const backlight_types[] = {
  65. [BACKLIGHT_RAW] = "raw",
  66. [BACKLIGHT_PLATFORM] = "platform",
  67. [BACKLIGHT_FIRMWARE] = "firmware",
  68. };
  69. static const char *const backlight_scale_types[] = {
  70. [BACKLIGHT_SCALE_UNKNOWN] = "unknown",
  71. [BACKLIGHT_SCALE_LINEAR] = "linear",
  72. [BACKLIGHT_SCALE_NON_LINEAR] = "non-linear",
  73. };
  74. #if defined(CONFIG_FB) || (defined(CONFIG_FB_MODULE) && \
  75. defined(CONFIG_BACKLIGHT_CLASS_DEVICE_MODULE))
  76. /*
  77. * fb_notifier_callback
  78. *
  79. * This callback gets called when something important happens inside a
  80. * framebuffer driver. The backlight core only cares about FB_BLANK_UNBLANK
  81. * which is reported to the driver using backlight_update_status()
  82. * as a state change.
  83. *
  84. * There may be several fbdev's connected to the backlight device,
  85. * in which case they are kept track of. A state change is only reported
  86. * if there is a change in backlight for the specified fbdev.
  87. */
  88. static int fb_notifier_callback(struct notifier_block *self,
  89. unsigned long event, void *data)
  90. {
  91. struct backlight_device *bd;
  92. struct fb_event *evdata = data;
  93. int node = evdata->info->node;
  94. int fb_blank = 0;
  95. /* If we aren't interested in this event, skip it immediately ... */
  96. if (event != FB_EVENT_BLANK)
  97. return 0;
  98. bd = container_of(self, struct backlight_device, fb_notif);
  99. mutex_lock(&bd->ops_lock);
  100. if (!bd->ops)
  101. goto out;
  102. if (bd->ops->check_fb && !bd->ops->check_fb(bd, evdata->info))
  103. goto out;
  104. fb_blank = *(int *)evdata->data;
  105. if (fb_blank == FB_BLANK_UNBLANK && !bd->fb_bl_on[node]) {
  106. bd->fb_bl_on[node] = true;
  107. if (!bd->use_count++) {
  108. bd->props.state &= ~BL_CORE_FBBLANK;
  109. bd->props.fb_blank = FB_BLANK_UNBLANK;
  110. backlight_update_status(bd);
  111. }
  112. } else if (fb_blank != FB_BLANK_UNBLANK && bd->fb_bl_on[node]) {
  113. bd->fb_bl_on[node] = false;
  114. if (!(--bd->use_count)) {
  115. bd->props.state |= BL_CORE_FBBLANK;
  116. bd->props.fb_blank = fb_blank;
  117. backlight_update_status(bd);
  118. }
  119. }
  120. out:
  121. mutex_unlock(&bd->ops_lock);
  122. return 0;
  123. }
  124. static int backlight_register_fb(struct backlight_device *bd)
  125. {
  126. memset(&bd->fb_notif, 0, sizeof(bd->fb_notif));
  127. bd->fb_notif.notifier_call = fb_notifier_callback;
  128. return fb_register_client(&bd->fb_notif);
  129. }
  130. static void backlight_unregister_fb(struct backlight_device *bd)
  131. {
  132. fb_unregister_client(&bd->fb_notif);
  133. }
  134. #else
  135. static inline int backlight_register_fb(struct backlight_device *bd)
  136. {
  137. return 0;
  138. }
  139. static inline void backlight_unregister_fb(struct backlight_device *bd)
  140. {
  141. }
  142. #endif /* CONFIG_FB */
  143. static void backlight_generate_event(struct backlight_device *bd,
  144. enum backlight_update_reason reason)
  145. {
  146. char *envp[2];
  147. switch (reason) {
  148. case BACKLIGHT_UPDATE_SYSFS:
  149. envp[0] = "SOURCE=sysfs";
  150. break;
  151. case BACKLIGHT_UPDATE_HOTKEY:
  152. envp[0] = "SOURCE=hotkey";
  153. break;
  154. default:
  155. envp[0] = "SOURCE=unknown";
  156. break;
  157. }
  158. envp[1] = NULL;
  159. kobject_uevent_env(&bd->dev.kobj, KOBJ_CHANGE, envp);
  160. sysfs_notify(&bd->dev.kobj, NULL, "actual_brightness");
  161. }
  162. static ssize_t bl_power_show(struct device *dev, struct device_attribute *attr,
  163. char *buf)
  164. {
  165. struct backlight_device *bd = to_backlight_device(dev);
  166. return sprintf(buf, "%d\n", bd->props.power);
  167. }
  168. static ssize_t bl_power_store(struct device *dev, struct device_attribute *attr,
  169. const char *buf, size_t count)
  170. {
  171. int rc;
  172. struct backlight_device *bd = to_backlight_device(dev);
  173. unsigned long power, old_power;
  174. rc = kstrtoul(buf, 0, &power);
  175. if (rc)
  176. return rc;
  177. rc = -ENXIO;
  178. mutex_lock(&bd->ops_lock);
  179. if (bd->ops) {
  180. pr_debug("set power to %lu\n", power);
  181. if (bd->props.power != power) {
  182. old_power = bd->props.power;
  183. bd->props.power = power;
  184. rc = backlight_update_status(bd);
  185. if (rc)
  186. bd->props.power = old_power;
  187. else
  188. rc = count;
  189. } else {
  190. rc = count;
  191. }
  192. }
  193. mutex_unlock(&bd->ops_lock);
  194. return rc;
  195. }
  196. static DEVICE_ATTR_RW(bl_power);
  197. static ssize_t brightness_show(struct device *dev,
  198. struct device_attribute *attr, char *buf)
  199. {
  200. struct backlight_device *bd = to_backlight_device(dev);
  201. return sprintf(buf, "%d\n", bd->props.brightness);
  202. }
  203. int backlight_device_set_brightness(struct backlight_device *bd,
  204. unsigned long brightness)
  205. {
  206. int rc = -ENXIO;
  207. mutex_lock(&bd->ops_lock);
  208. if (bd->ops) {
  209. if (brightness > bd->props.max_brightness)
  210. rc = -EINVAL;
  211. else {
  212. pr_debug("set brightness to %lu\n", brightness);
  213. bd->props.brightness = brightness;
  214. rc = backlight_update_status(bd);
  215. }
  216. }
  217. mutex_unlock(&bd->ops_lock);
  218. backlight_generate_event(bd, BACKLIGHT_UPDATE_SYSFS);
  219. return rc;
  220. }
  221. EXPORT_SYMBOL(backlight_device_set_brightness);
  222. static ssize_t brightness_store(struct device *dev,
  223. struct device_attribute *attr, const char *buf, size_t count)
  224. {
  225. int rc;
  226. struct backlight_device *bd = to_backlight_device(dev);
  227. unsigned long brightness;
  228. rc = kstrtoul(buf, 0, &brightness);
  229. if (rc)
  230. return rc;
  231. rc = backlight_device_set_brightness(bd, brightness);
  232. return rc ? rc : count;
  233. }
  234. static DEVICE_ATTR_RW(brightness);
  235. static ssize_t type_show(struct device *dev, struct device_attribute *attr,
  236. char *buf)
  237. {
  238. struct backlight_device *bd = to_backlight_device(dev);
  239. return sprintf(buf, "%s\n", backlight_types[bd->props.type]);
  240. }
  241. static DEVICE_ATTR_RO(type);
  242. static ssize_t max_brightness_show(struct device *dev,
  243. struct device_attribute *attr, char *buf)
  244. {
  245. struct backlight_device *bd = to_backlight_device(dev);
  246. return sprintf(buf, "%d\n", bd->props.max_brightness);
  247. }
  248. static DEVICE_ATTR_RO(max_brightness);
  249. static ssize_t actual_brightness_show(struct device *dev,
  250. struct device_attribute *attr, char *buf)
  251. {
  252. int rc = -ENXIO;
  253. struct backlight_device *bd = to_backlight_device(dev);
  254. mutex_lock(&bd->ops_lock);
  255. if (bd->ops && bd->ops->get_brightness)
  256. rc = sprintf(buf, "%d\n", bd->ops->get_brightness(bd));
  257. else
  258. rc = sprintf(buf, "%d\n", bd->props.brightness);
  259. mutex_unlock(&bd->ops_lock);
  260. return rc;
  261. }
  262. static DEVICE_ATTR_RO(actual_brightness);
  263. static ssize_t scale_show(struct device *dev,
  264. struct device_attribute *attr, char *buf)
  265. {
  266. struct backlight_device *bd = to_backlight_device(dev);
  267. if (WARN_ON(bd->props.scale > BACKLIGHT_SCALE_NON_LINEAR))
  268. return sprintf(buf, "unknown\n");
  269. return sprintf(buf, "%s\n", backlight_scale_types[bd->props.scale]);
  270. }
  271. static DEVICE_ATTR_RO(scale);
  272. static struct class *backlight_class;
  273. #ifdef CONFIG_PM_SLEEP
  274. static int backlight_suspend(struct device *dev)
  275. {
  276. struct backlight_device *bd = to_backlight_device(dev);
  277. mutex_lock(&bd->ops_lock);
  278. if (bd->ops && bd->ops->options & BL_CORE_SUSPENDRESUME) {
  279. bd->props.state |= BL_CORE_SUSPENDED;
  280. backlight_update_status(bd);
  281. }
  282. mutex_unlock(&bd->ops_lock);
  283. return 0;
  284. }
  285. static int backlight_resume(struct device *dev)
  286. {
  287. struct backlight_device *bd = to_backlight_device(dev);
  288. mutex_lock(&bd->ops_lock);
  289. if (bd->ops && bd->ops->options & BL_CORE_SUSPENDRESUME) {
  290. bd->props.state &= ~BL_CORE_SUSPENDED;
  291. backlight_update_status(bd);
  292. }
  293. mutex_unlock(&bd->ops_lock);
  294. return 0;
  295. }
  296. #endif
  297. static SIMPLE_DEV_PM_OPS(backlight_class_dev_pm_ops, backlight_suspend,
  298. backlight_resume);
  299. static void bl_device_release(struct device *dev)
  300. {
  301. struct backlight_device *bd = to_backlight_device(dev);
  302. kfree(bd);
  303. }
  304. static struct attribute *bl_device_attrs[] = {
  305. &dev_attr_bl_power.attr,
  306. &dev_attr_brightness.attr,
  307. &dev_attr_actual_brightness.attr,
  308. &dev_attr_max_brightness.attr,
  309. &dev_attr_scale.attr,
  310. &dev_attr_type.attr,
  311. NULL,
  312. };
  313. ATTRIBUTE_GROUPS(bl_device);
  314. /**
  315. * backlight_force_update - tell the backlight subsystem that hardware state
  316. * has changed
  317. * @bd: the backlight device to update
  318. * @reason: reason for update
  319. *
  320. * Updates the internal state of the backlight in response to a hardware event,
  321. * and generates an uevent to notify userspace. A backlight driver shall call
  322. * backlight_force_update() when the backlight is changed using, for example,
  323. * a hot-key. The updated brightness is read using get_brightness() and the
  324. * brightness value is reported using an uevent.
  325. */
  326. void backlight_force_update(struct backlight_device *bd,
  327. enum backlight_update_reason reason)
  328. {
  329. mutex_lock(&bd->ops_lock);
  330. if (bd->ops && bd->ops->get_brightness)
  331. bd->props.brightness = bd->ops->get_brightness(bd);
  332. mutex_unlock(&bd->ops_lock);
  333. backlight_generate_event(bd, reason);
  334. }
  335. EXPORT_SYMBOL(backlight_force_update);
  336. /* deprecated - use devm_backlight_device_register() */
  337. struct backlight_device *backlight_device_register(const char *name,
  338. struct device *parent, void *devdata, const struct backlight_ops *ops,
  339. const struct backlight_properties *props)
  340. {
  341. struct backlight_device *new_bd;
  342. int rc;
  343. pr_debug("backlight_device_register: name=%s\n", name);
  344. new_bd = kzalloc(sizeof(struct backlight_device), GFP_KERNEL);
  345. if (!new_bd)
  346. return ERR_PTR(-ENOMEM);
  347. mutex_init(&new_bd->update_lock);
  348. mutex_init(&new_bd->ops_lock);
  349. new_bd->dev.class = backlight_class;
  350. new_bd->dev.parent = parent;
  351. new_bd->dev.release = bl_device_release;
  352. dev_set_name(&new_bd->dev, "%s", name);
  353. dev_set_drvdata(&new_bd->dev, devdata);
  354. /* Set default properties */
  355. if (props) {
  356. memcpy(&new_bd->props, props,
  357. sizeof(struct backlight_properties));
  358. if (props->type <= 0 || props->type >= BACKLIGHT_TYPE_MAX) {
  359. WARN(1, "%s: invalid backlight type", name);
  360. new_bd->props.type = BACKLIGHT_RAW;
  361. }
  362. } else {
  363. new_bd->props.type = BACKLIGHT_RAW;
  364. }
  365. rc = device_register(&new_bd->dev);
  366. if (rc) {
  367. put_device(&new_bd->dev);
  368. return ERR_PTR(rc);
  369. }
  370. rc = backlight_register_fb(new_bd);
  371. if (rc) {
  372. device_unregister(&new_bd->dev);
  373. return ERR_PTR(rc);
  374. }
  375. new_bd->ops = ops;
  376. #ifdef CONFIG_PMAC_BACKLIGHT
  377. mutex_lock(&pmac_backlight_mutex);
  378. if (!pmac_backlight)
  379. pmac_backlight = new_bd;
  380. mutex_unlock(&pmac_backlight_mutex);
  381. #endif
  382. mutex_lock(&backlight_dev_list_mutex);
  383. list_add(&new_bd->entry, &backlight_dev_list);
  384. mutex_unlock(&backlight_dev_list_mutex);
  385. blocking_notifier_call_chain(&backlight_notifier,
  386. BACKLIGHT_REGISTERED, new_bd);
  387. return new_bd;
  388. }
  389. EXPORT_SYMBOL(backlight_device_register);
  390. /** backlight_device_get_by_type - find first backlight device of a type
  391. * @type: the type of backlight device
  392. *
  393. * Look up the first backlight device of the specified type
  394. *
  395. * RETURNS:
  396. *
  397. * Pointer to backlight device if any was found. Otherwise NULL.
  398. */
  399. struct backlight_device *backlight_device_get_by_type(enum backlight_type type)
  400. {
  401. bool found = false;
  402. struct backlight_device *bd;
  403. mutex_lock(&backlight_dev_list_mutex);
  404. list_for_each_entry(bd, &backlight_dev_list, entry) {
  405. if (bd->props.type == type) {
  406. found = true;
  407. break;
  408. }
  409. }
  410. mutex_unlock(&backlight_dev_list_mutex);
  411. return found ? bd : NULL;
  412. }
  413. EXPORT_SYMBOL(backlight_device_get_by_type);
  414. /**
  415. * backlight_device_get_by_name - Get backlight device by name
  416. * @name: Device name
  417. *
  418. * This function looks up a backlight device by its name. It obtains a reference
  419. * on the backlight device and it is the caller's responsibility to drop the
  420. * reference by calling backlight_put().
  421. *
  422. * Returns:
  423. * A pointer to the backlight device if found, otherwise NULL.
  424. */
  425. struct backlight_device *backlight_device_get_by_name(const char *name)
  426. {
  427. struct device *dev;
  428. dev = class_find_device_by_name(backlight_class, name);
  429. return dev ? to_backlight_device(dev) : NULL;
  430. }
  431. EXPORT_SYMBOL(backlight_device_get_by_name);
  432. /* deprecated - use devm_backlight_device_unregister() */
  433. void backlight_device_unregister(struct backlight_device *bd)
  434. {
  435. if (!bd)
  436. return;
  437. mutex_lock(&backlight_dev_list_mutex);
  438. list_del(&bd->entry);
  439. mutex_unlock(&backlight_dev_list_mutex);
  440. #ifdef CONFIG_PMAC_BACKLIGHT
  441. mutex_lock(&pmac_backlight_mutex);
  442. if (pmac_backlight == bd)
  443. pmac_backlight = NULL;
  444. mutex_unlock(&pmac_backlight_mutex);
  445. #endif
  446. blocking_notifier_call_chain(&backlight_notifier,
  447. BACKLIGHT_UNREGISTERED, bd);
  448. mutex_lock(&bd->ops_lock);
  449. bd->ops = NULL;
  450. mutex_unlock(&bd->ops_lock);
  451. backlight_unregister_fb(bd);
  452. device_unregister(&bd->dev);
  453. }
  454. EXPORT_SYMBOL(backlight_device_unregister);
  455. static void devm_backlight_device_release(struct device *dev, void *res)
  456. {
  457. struct backlight_device *backlight = *(struct backlight_device **)res;
  458. backlight_device_unregister(backlight);
  459. }
  460. static int devm_backlight_device_match(struct device *dev, void *res,
  461. void *data)
  462. {
  463. struct backlight_device **r = res;
  464. return *r == data;
  465. }
  466. /**
  467. * backlight_register_notifier - get notified of backlight (un)registration
  468. * @nb: notifier block with the notifier to call on backlight (un)registration
  469. *
  470. * Register a notifier to get notified when backlight devices get registered
  471. * or unregistered.
  472. *
  473. * RETURNS:
  474. *
  475. * 0 on success, otherwise a negative error code
  476. */
  477. int backlight_register_notifier(struct notifier_block *nb)
  478. {
  479. return blocking_notifier_chain_register(&backlight_notifier, nb);
  480. }
  481. EXPORT_SYMBOL(backlight_register_notifier);
  482. /**
  483. * backlight_unregister_notifier - unregister a backlight notifier
  484. * @nb: notifier block to unregister
  485. *
  486. * Register a notifier to get notified when backlight devices get registered
  487. * or unregistered.
  488. *
  489. * RETURNS:
  490. *
  491. * 0 on success, otherwise a negative error code
  492. */
  493. int backlight_unregister_notifier(struct notifier_block *nb)
  494. {
  495. return blocking_notifier_chain_unregister(&backlight_notifier, nb);
  496. }
  497. EXPORT_SYMBOL(backlight_unregister_notifier);
  498. /**
  499. * devm_backlight_device_register - register a new backlight device
  500. * @dev: the device to register
  501. * @name: the name of the device
  502. * @parent: a pointer to the parent device (often the same as @dev)
  503. * @devdata: an optional pointer to be stored for private driver use
  504. * @ops: the backlight operations structure
  505. * @props: the backlight properties
  506. *
  507. * Creates and registers new backlight device. When a backlight device
  508. * is registered the configuration must be specified in the @props
  509. * parameter. See description of &backlight_properties.
  510. *
  511. * RETURNS:
  512. *
  513. * struct backlight on success, or an ERR_PTR on error
  514. */
  515. struct backlight_device *devm_backlight_device_register(struct device *dev,
  516. const char *name, struct device *parent, void *devdata,
  517. const struct backlight_ops *ops,
  518. const struct backlight_properties *props)
  519. {
  520. struct backlight_device **ptr, *backlight;
  521. ptr = devres_alloc(devm_backlight_device_release, sizeof(*ptr),
  522. GFP_KERNEL);
  523. if (!ptr)
  524. return ERR_PTR(-ENOMEM);
  525. backlight = backlight_device_register(name, parent, devdata, ops,
  526. props);
  527. if (!IS_ERR(backlight)) {
  528. *ptr = backlight;
  529. devres_add(dev, ptr);
  530. } else {
  531. devres_free(ptr);
  532. }
  533. return backlight;
  534. }
  535. EXPORT_SYMBOL(devm_backlight_device_register);
  536. /**
  537. * devm_backlight_device_unregister - unregister backlight device
  538. * @dev: the device to unregister
  539. * @bd: the backlight device to unregister
  540. *
  541. * Deallocates a backlight allocated with devm_backlight_device_register().
  542. * Normally this function will not need to be called and the resource management
  543. * code will ensure that the resources are freed.
  544. */
  545. void devm_backlight_device_unregister(struct device *dev,
  546. struct backlight_device *bd)
  547. {
  548. int rc;
  549. rc = devres_release(dev, devm_backlight_device_release,
  550. devm_backlight_device_match, bd);
  551. WARN_ON(rc);
  552. }
  553. EXPORT_SYMBOL(devm_backlight_device_unregister);
  554. #ifdef CONFIG_OF
  555. static int of_parent_match(struct device *dev, const void *data)
  556. {
  557. return dev->parent && dev->parent->of_node == data;
  558. }
  559. /**
  560. * of_find_backlight_by_node() - find backlight device by device-tree node
  561. * @node: device-tree node of the backlight device
  562. *
  563. * Returns a pointer to the backlight device corresponding to the given DT
  564. * node or NULL if no such backlight device exists or if the device hasn't
  565. * been probed yet.
  566. *
  567. * This function obtains a reference on the backlight device and it is the
  568. * caller's responsibility to drop the reference by calling put_device() on
  569. * the backlight device's .dev field.
  570. */
  571. struct backlight_device *of_find_backlight_by_node(struct device_node *node)
  572. {
  573. struct device *dev;
  574. dev = class_find_device(backlight_class, NULL, node, of_parent_match);
  575. return dev ? to_backlight_device(dev) : NULL;
  576. }
  577. EXPORT_SYMBOL(of_find_backlight_by_node);
  578. #endif
  579. static struct backlight_device *of_find_backlight(struct device *dev)
  580. {
  581. struct backlight_device *bd = NULL;
  582. struct device_node *np;
  583. if (!dev)
  584. return NULL;
  585. if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
  586. np = of_parse_phandle(dev->of_node, "backlight", 0);
  587. if (np) {
  588. bd = of_find_backlight_by_node(np);
  589. of_node_put(np);
  590. if (!bd)
  591. return ERR_PTR(-EPROBE_DEFER);
  592. }
  593. }
  594. return bd;
  595. }
  596. static void devm_backlight_release(void *data)
  597. {
  598. struct backlight_device *bd = data;
  599. if (bd)
  600. put_device(&bd->dev);
  601. }
  602. /**
  603. * devm_of_find_backlight - find backlight for a device
  604. * @dev: the device
  605. *
  606. * This function looks for a property named 'backlight' on the DT node
  607. * connected to @dev and looks up the backlight device. The lookup is
  608. * device managed so the reference to the backlight device is automatically
  609. * dropped on driver detach.
  610. *
  611. * RETURNS:
  612. *
  613. * A pointer to the backlight device if found.
  614. * Error pointer -EPROBE_DEFER if the DT property is set, but no backlight
  615. * device is found. NULL if there's no backlight property.
  616. */
  617. struct backlight_device *devm_of_find_backlight(struct device *dev)
  618. {
  619. struct backlight_device *bd;
  620. int ret;
  621. bd = of_find_backlight(dev);
  622. if (IS_ERR_OR_NULL(bd))
  623. return bd;
  624. ret = devm_add_action(dev, devm_backlight_release, bd);
  625. if (ret) {
  626. put_device(&bd->dev);
  627. return ERR_PTR(ret);
  628. }
  629. return bd;
  630. }
  631. EXPORT_SYMBOL(devm_of_find_backlight);
  632. static void __exit backlight_class_exit(void)
  633. {
  634. class_destroy(backlight_class);
  635. }
  636. static int __init backlight_class_init(void)
  637. {
  638. backlight_class = class_create(THIS_MODULE, "backlight");
  639. if (IS_ERR(backlight_class)) {
  640. pr_warn("Unable to create backlight class; errno = %ld\n",
  641. PTR_ERR(backlight_class));
  642. return PTR_ERR(backlight_class);
  643. }
  644. backlight_class->dev_groups = bl_device_groups;
  645. backlight_class->pm = &backlight_class_dev_pm_ops;
  646. INIT_LIST_HEAD(&backlight_dev_list);
  647. mutex_init(&backlight_dev_list_mutex);
  648. BLOCKING_INIT_NOTIFIER_HEAD(&backlight_notifier);
  649. return 0;
  650. }
  651. /*
  652. * if this is compiled into the kernel, we need to ensure that the
  653. * class is registered before users of the class try to register lcd's
  654. */
  655. postcore_initcall(backlight_class_init);
  656. module_exit(backlight_class_exit);
  657. MODULE_LICENSE("GPL");
  658. MODULE_AUTHOR("Jamey Hicks <jamey.hicks@hp.com>, Andrew Zabolotny <zap@homelink.ru>");
  659. MODULE_DESCRIPTION("Backlight Lowlevel Control Abstraction");