backlight.c 21 KB

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