pwm_bl.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678
  1. /*
  2. * linux/drivers/video/backlight/pwm_bl.c
  3. *
  4. * simple PWM based backlight control, board code has to setup
  5. * 1) pin configuration so PWM waveforms can output
  6. * 2) platform_data being correctly configured
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <linux/delay.h>
  13. #include <linux/gpio/consumer.h>
  14. #include <linux/gpio.h>
  15. #include <linux/module.h>
  16. #include <linux/kernel.h>
  17. #include <linux/init.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/fb.h>
  20. #include <linux/backlight.h>
  21. #include <linux/err.h>
  22. #include <linux/pwm.h>
  23. #include <linux/pwm_backlight.h>
  24. #include <linux/regulator/consumer.h>
  25. #include <linux/slab.h>
  26. struct pwm_bl_data {
  27. struct pwm_device *pwm;
  28. struct device *dev;
  29. unsigned int period;
  30. unsigned int lth_brightness;
  31. unsigned int *levels;
  32. bool enabled;
  33. struct regulator *power_supply;
  34. struct gpio_desc *enable_gpio;
  35. unsigned int scale;
  36. bool legacy;
  37. unsigned int post_pwm_on_delay;
  38. unsigned int pwm_off_delay;
  39. int (*notify)(struct device *,
  40. int brightness);
  41. void (*notify_after)(struct device *,
  42. int brightness);
  43. int (*check_fb)(struct device *, struct fb_info *);
  44. void (*exit)(struct device *);
  45. };
  46. static void pwm_backlight_power_on(struct pwm_bl_data *pb, int brightness)
  47. {
  48. int err;
  49. if (pb->enabled)
  50. return;
  51. err = regulator_enable(pb->power_supply);
  52. if (err < 0)
  53. dev_err(pb->dev, "failed to enable power supply\n");
  54. pwm_enable(pb->pwm);
  55. if (pb->post_pwm_on_delay)
  56. msleep(pb->post_pwm_on_delay);
  57. if (pb->enable_gpio)
  58. gpiod_set_value_cansleep(pb->enable_gpio, 1);
  59. pb->enabled = true;
  60. }
  61. static void pwm_backlight_power_off(struct pwm_bl_data *pb)
  62. {
  63. if (!pb->enabled)
  64. return;
  65. if (pb->enable_gpio)
  66. gpiod_set_value_cansleep(pb->enable_gpio, 0);
  67. if (pb->pwm_off_delay)
  68. msleep(pb->pwm_off_delay);
  69. pwm_config(pb->pwm, 0, pb->period);
  70. pwm_disable(pb->pwm);
  71. regulator_disable(pb->power_supply);
  72. pb->enabled = false;
  73. }
  74. static int compute_duty_cycle(struct pwm_bl_data *pb, int brightness)
  75. {
  76. unsigned int lth = pb->lth_brightness;
  77. u64 duty_cycle;
  78. if (pb->levels)
  79. duty_cycle = pb->levels[brightness];
  80. else
  81. duty_cycle = brightness;
  82. duty_cycle *= pb->period - lth;
  83. do_div(duty_cycle, pb->scale);
  84. return duty_cycle + lth;
  85. }
  86. static int pwm_backlight_update_status(struct backlight_device *bl)
  87. {
  88. struct pwm_bl_data *pb = bl_get_data(bl);
  89. int brightness = bl->props.brightness;
  90. int duty_cycle;
  91. if (bl->props.power != FB_BLANK_UNBLANK ||
  92. bl->props.fb_blank != FB_BLANK_UNBLANK ||
  93. bl->props.state & BL_CORE_FBBLANK)
  94. brightness = 0;
  95. if (pb->notify)
  96. brightness = pb->notify(pb->dev, brightness);
  97. if (brightness > 0) {
  98. duty_cycle = compute_duty_cycle(pb, brightness);
  99. pwm_config(pb->pwm, duty_cycle, pb->period);
  100. pwm_backlight_power_on(pb, brightness);
  101. } else
  102. pwm_backlight_power_off(pb);
  103. if (pb->notify_after)
  104. pb->notify_after(pb->dev, brightness);
  105. return 0;
  106. }
  107. static int pwm_backlight_check_fb(struct backlight_device *bl,
  108. struct fb_info *info)
  109. {
  110. struct pwm_bl_data *pb = bl_get_data(bl);
  111. return !pb->check_fb || pb->check_fb(pb->dev, info);
  112. }
  113. static const struct backlight_ops pwm_backlight_ops = {
  114. .update_status = pwm_backlight_update_status,
  115. .check_fb = pwm_backlight_check_fb,
  116. };
  117. #ifdef CONFIG_OF
  118. #define PWM_LUMINANCE_SCALE 10000 /* luminance scale */
  119. /* An integer based power function */
  120. static u64 int_pow(u64 base, int exp)
  121. {
  122. u64 result = 1;
  123. while (exp) {
  124. if (exp & 1)
  125. result *= base;
  126. exp >>= 1;
  127. base *= base;
  128. }
  129. return result;
  130. }
  131. /*
  132. * CIE lightness to PWM conversion.
  133. *
  134. * The CIE 1931 lightness formula is what actually describes how we perceive
  135. * light:
  136. * Y = (L* / 902.3) if L* ≤ 0.08856
  137. * Y = ((L* + 16) / 116)^3 if L* > 0.08856
  138. *
  139. * Where Y is the luminance, the amount of light coming out of the screen, and
  140. * is a number between 0.0 and 1.0; and L* is the lightness, how bright a human
  141. * perceives the screen to be, and is a number between 0 and 100.
  142. *
  143. * The following function does the fixed point maths needed to implement the
  144. * above formula.
  145. */
  146. static u64 cie1931(unsigned int lightness, unsigned int scale)
  147. {
  148. u64 retval;
  149. lightness *= 100;
  150. if (lightness <= (8 * scale)) {
  151. retval = DIV_ROUND_CLOSEST_ULL(lightness * 10, 9023);
  152. } else {
  153. retval = int_pow((lightness + (16 * scale)) / 116, 3);
  154. retval = DIV_ROUND_CLOSEST_ULL(retval, (scale * scale));
  155. }
  156. return retval;
  157. }
  158. /*
  159. * Create a default correction table for PWM values to create linear brightness
  160. * for LED based backlights using the CIE1931 algorithm.
  161. */
  162. static
  163. int pwm_backlight_brightness_default(struct device *dev,
  164. struct platform_pwm_backlight_data *data,
  165. unsigned int period)
  166. {
  167. unsigned int i;
  168. u64 retval;
  169. /*
  170. * Once we have 4096 levels there's little point going much higher...
  171. * neither interactive sliders nor animation benefits from having
  172. * more values in the table.
  173. */
  174. data->max_brightness =
  175. min((int)DIV_ROUND_UP(period, fls(period)), 4096);
  176. data->levels = devm_kcalloc(dev, data->max_brightness,
  177. sizeof(*data->levels), GFP_KERNEL);
  178. if (!data->levels)
  179. return -ENOMEM;
  180. /* Fill the table using the cie1931 algorithm */
  181. for (i = 0; i < data->max_brightness; i++) {
  182. retval = cie1931((i * PWM_LUMINANCE_SCALE) /
  183. data->max_brightness, PWM_LUMINANCE_SCALE) *
  184. period;
  185. retval = DIV_ROUND_CLOSEST_ULL(retval, PWM_LUMINANCE_SCALE);
  186. if (retval > UINT_MAX)
  187. return -EINVAL;
  188. data->levels[i] = (unsigned int)retval;
  189. }
  190. data->dft_brightness = data->max_brightness / 2;
  191. data->max_brightness--;
  192. return 0;
  193. }
  194. static int pwm_backlight_parse_dt(struct device *dev,
  195. struct platform_pwm_backlight_data *data)
  196. {
  197. struct device_node *node = dev->of_node;
  198. unsigned int num_levels = 0;
  199. unsigned int levels_count;
  200. unsigned int num_steps = 0;
  201. struct property *prop;
  202. unsigned int *table;
  203. int length;
  204. u32 value;
  205. int ret;
  206. if (!node)
  207. return -ENODEV;
  208. memset(data, 0, sizeof(*data));
  209. /*
  210. * These values are optional and set as 0 by default, the out values
  211. * are modified only if a valid u32 value can be decoded.
  212. */
  213. of_property_read_u32(node, "post-pwm-on-delay-ms",
  214. &data->post_pwm_on_delay);
  215. of_property_read_u32(node, "pwm-off-delay-ms", &data->pwm_off_delay);
  216. data->enable_gpio = -EINVAL;
  217. /*
  218. * Determine the number of brightness levels, if this property is not
  219. * set a default table of brightness levels will be used.
  220. */
  221. prop = of_find_property(node, "brightness-levels", &length);
  222. if (!prop)
  223. return 0;
  224. data->max_brightness = length / sizeof(u32);
  225. /* read brightness levels from DT property */
  226. if (data->max_brightness > 0) {
  227. size_t size = sizeof(*data->levels) * data->max_brightness;
  228. unsigned int i, j, n = 0;
  229. data->levels = devm_kzalloc(dev, size, GFP_KERNEL);
  230. if (!data->levels)
  231. return -ENOMEM;
  232. ret = of_property_read_u32_array(node, "brightness-levels",
  233. data->levels,
  234. data->max_brightness);
  235. if (ret < 0)
  236. return ret;
  237. ret = of_property_read_u32(node, "default-brightness-level",
  238. &value);
  239. if (ret < 0)
  240. return ret;
  241. data->dft_brightness = value;
  242. /*
  243. * This property is optional, if is set enables linear
  244. * interpolation between each of the values of brightness levels
  245. * and creates a new pre-computed table.
  246. */
  247. of_property_read_u32(node, "num-interpolated-steps",
  248. &num_steps);
  249. /*
  250. * Make sure that there is at least two entries in the
  251. * brightness-levels table, otherwise we can't interpolate
  252. * between two points.
  253. */
  254. if (num_steps) {
  255. if (data->max_brightness < 2) {
  256. dev_err(dev, "can't interpolate\n");
  257. return -EINVAL;
  258. }
  259. /*
  260. * Recalculate the number of brightness levels, now
  261. * taking in consideration the number of interpolated
  262. * steps between two levels.
  263. */
  264. for (i = 0; i < data->max_brightness - 1; i++) {
  265. if ((data->levels[i + 1] - data->levels[i]) /
  266. num_steps)
  267. num_levels += num_steps;
  268. else
  269. num_levels++;
  270. }
  271. num_levels++;
  272. dev_dbg(dev, "new number of brightness levels: %d\n",
  273. num_levels);
  274. /*
  275. * Create a new table of brightness levels with all the
  276. * interpolated steps.
  277. */
  278. size = sizeof(*table) * num_levels;
  279. table = devm_kzalloc(dev, size, GFP_KERNEL);
  280. if (!table)
  281. return -ENOMEM;
  282. /* Fill the interpolated table. */
  283. levels_count = 0;
  284. for (i = 0; i < data->max_brightness - 1; i++) {
  285. value = data->levels[i];
  286. n = (data->levels[i + 1] - value) / num_steps;
  287. if (n > 0) {
  288. for (j = 0; j < num_steps; j++) {
  289. table[levels_count] = value;
  290. value += n;
  291. levels_count++;
  292. }
  293. } else {
  294. table[levels_count] = data->levels[i];
  295. levels_count++;
  296. }
  297. }
  298. table[levels_count] = data->levels[i];
  299. /*
  300. * As we use interpolation lets remove current
  301. * brightness levels table and replace for the
  302. * new interpolated table.
  303. */
  304. devm_kfree(dev, data->levels);
  305. data->levels = table;
  306. /*
  307. * Reassign max_brightness value to the new total number
  308. * of brightness levels.
  309. */
  310. data->max_brightness = num_levels;
  311. }
  312. data->max_brightness--;
  313. }
  314. return 0;
  315. }
  316. static const struct of_device_id pwm_backlight_of_match[] = {
  317. { .compatible = "pwm-backlight" },
  318. { }
  319. };
  320. MODULE_DEVICE_TABLE(of, pwm_backlight_of_match);
  321. #else
  322. static int pwm_backlight_parse_dt(struct device *dev,
  323. struct platform_pwm_backlight_data *data)
  324. {
  325. return -ENODEV;
  326. }
  327. static
  328. int pwm_backlight_brightness_default(struct device *dev,
  329. struct platform_pwm_backlight_data *data,
  330. unsigned int period)
  331. {
  332. return -ENODEV;
  333. }
  334. #endif
  335. static int pwm_backlight_initial_power_state(const struct pwm_bl_data *pb)
  336. {
  337. struct device_node *node = pb->dev->of_node;
  338. /* Not booted with device tree or no phandle link to the node */
  339. if (!node || !node->phandle)
  340. return FB_BLANK_UNBLANK;
  341. /*
  342. * If the driver is probed from the device tree and there is a
  343. * phandle link pointing to the backlight node, it is safe to
  344. * assume that another driver will enable the backlight at the
  345. * appropriate time. Therefore, if it is disabled, keep it so.
  346. */
  347. /* if the enable GPIO is disabled, do not enable the backlight */
  348. if (pb->enable_gpio && gpiod_get_value_cansleep(pb->enable_gpio) == 0)
  349. return FB_BLANK_POWERDOWN;
  350. /* The regulator is disabled, do not enable the backlight */
  351. if (!regulator_is_enabled(pb->power_supply))
  352. return FB_BLANK_POWERDOWN;
  353. /* The PWM is disabled, keep it like this */
  354. if (!pwm_is_enabled(pb->pwm))
  355. return FB_BLANK_POWERDOWN;
  356. return FB_BLANK_UNBLANK;
  357. }
  358. static int pwm_backlight_probe(struct platform_device *pdev)
  359. {
  360. struct platform_pwm_backlight_data *data = dev_get_platdata(&pdev->dev);
  361. struct platform_pwm_backlight_data defdata;
  362. struct backlight_properties props;
  363. struct backlight_device *bl;
  364. struct device_node *node = pdev->dev.of_node;
  365. struct pwm_bl_data *pb;
  366. struct pwm_state state;
  367. struct pwm_args pargs;
  368. unsigned int i;
  369. int ret;
  370. if (!data) {
  371. ret = pwm_backlight_parse_dt(&pdev->dev, &defdata);
  372. if (ret < 0) {
  373. dev_err(&pdev->dev, "failed to find platform data\n");
  374. return ret;
  375. }
  376. data = &defdata;
  377. }
  378. if (data->init) {
  379. ret = data->init(&pdev->dev);
  380. if (ret < 0)
  381. return ret;
  382. }
  383. pb = devm_kzalloc(&pdev->dev, sizeof(*pb), GFP_KERNEL);
  384. if (!pb) {
  385. ret = -ENOMEM;
  386. goto err_alloc;
  387. }
  388. pb->notify = data->notify;
  389. pb->notify_after = data->notify_after;
  390. pb->check_fb = data->check_fb;
  391. pb->exit = data->exit;
  392. pb->dev = &pdev->dev;
  393. pb->enabled = false;
  394. pb->post_pwm_on_delay = data->post_pwm_on_delay;
  395. pb->pwm_off_delay = data->pwm_off_delay;
  396. pb->enable_gpio = devm_gpiod_get_optional(&pdev->dev, "enable",
  397. GPIOD_ASIS);
  398. if (IS_ERR(pb->enable_gpio)) {
  399. ret = PTR_ERR(pb->enable_gpio);
  400. goto err_alloc;
  401. }
  402. /*
  403. * Compatibility fallback for drivers still using the integer GPIO
  404. * platform data. Must go away soon.
  405. */
  406. if (!pb->enable_gpio && gpio_is_valid(data->enable_gpio)) {
  407. ret = devm_gpio_request_one(&pdev->dev, data->enable_gpio,
  408. GPIOF_OUT_INIT_HIGH, "enable");
  409. if (ret < 0) {
  410. dev_err(&pdev->dev, "failed to request GPIO#%d: %d\n",
  411. data->enable_gpio, ret);
  412. goto err_alloc;
  413. }
  414. pb->enable_gpio = gpio_to_desc(data->enable_gpio);
  415. }
  416. /*
  417. * If the GPIO is not known to be already configured as output, that
  418. * is, if gpiod_get_direction returns either 1 or -EINVAL, change the
  419. * direction to output and set the GPIO as active.
  420. * Do not force the GPIO to active when it was already output as it
  421. * could cause backlight flickering or we would enable the backlight too
  422. * early. Leave the decision of the initial backlight state for later.
  423. */
  424. if (pb->enable_gpio &&
  425. gpiod_get_direction(pb->enable_gpio) != 0)
  426. gpiod_direction_output(pb->enable_gpio, 1);
  427. pb->power_supply = devm_regulator_get(&pdev->dev, "power");
  428. if (IS_ERR(pb->power_supply)) {
  429. ret = PTR_ERR(pb->power_supply);
  430. goto err_alloc;
  431. }
  432. pb->pwm = devm_pwm_get(&pdev->dev, NULL);
  433. if (IS_ERR(pb->pwm) && PTR_ERR(pb->pwm) != -EPROBE_DEFER && !node) {
  434. dev_err(&pdev->dev, "unable to request PWM, trying legacy API\n");
  435. pb->legacy = true;
  436. pb->pwm = pwm_request(data->pwm_id, "pwm-backlight");
  437. }
  438. if (IS_ERR(pb->pwm)) {
  439. ret = PTR_ERR(pb->pwm);
  440. if (ret != -EPROBE_DEFER)
  441. dev_err(&pdev->dev, "unable to request PWM\n");
  442. goto err_alloc;
  443. }
  444. dev_dbg(&pdev->dev, "got pwm for backlight\n");
  445. if (!data->levels) {
  446. /* Get the PWM period (in nanoseconds) */
  447. pwm_get_state(pb->pwm, &state);
  448. ret = pwm_backlight_brightness_default(&pdev->dev, data,
  449. state.period);
  450. if (ret < 0) {
  451. dev_err(&pdev->dev,
  452. "failed to setup default brightness table\n");
  453. goto err_alloc;
  454. }
  455. }
  456. for (i = 0; i <= data->max_brightness; i++) {
  457. if (data->levels[i] > pb->scale)
  458. pb->scale = data->levels[i];
  459. pb->levels = data->levels;
  460. }
  461. /*
  462. * FIXME: pwm_apply_args() should be removed when switching to
  463. * the atomic PWM API.
  464. */
  465. pwm_apply_args(pb->pwm);
  466. /*
  467. * The DT case will set the pwm_period_ns field to 0 and store the
  468. * period, parsed from the DT, in the PWM device. For the non-DT case,
  469. * set the period from platform data if it has not already been set
  470. * via the PWM lookup table.
  471. */
  472. pwm_get_args(pb->pwm, &pargs);
  473. pb->period = pargs.period;
  474. if (!pb->period && (data->pwm_period_ns > 0))
  475. pb->period = data->pwm_period_ns;
  476. pb->lth_brightness = data->lth_brightness * (pb->period / pb->scale);
  477. memset(&props, 0, sizeof(struct backlight_properties));
  478. props.type = BACKLIGHT_RAW;
  479. props.max_brightness = data->max_brightness;
  480. bl = backlight_device_register(dev_name(&pdev->dev), &pdev->dev, pb,
  481. &pwm_backlight_ops, &props);
  482. if (IS_ERR(bl)) {
  483. dev_err(&pdev->dev, "failed to register backlight\n");
  484. ret = PTR_ERR(bl);
  485. if (pb->legacy)
  486. pwm_free(pb->pwm);
  487. goto err_alloc;
  488. }
  489. if (data->dft_brightness > data->max_brightness) {
  490. dev_warn(&pdev->dev,
  491. "invalid default brightness level: %u, using %u\n",
  492. data->dft_brightness, data->max_brightness);
  493. data->dft_brightness = data->max_brightness;
  494. }
  495. bl->props.brightness = data->dft_brightness;
  496. bl->props.power = pwm_backlight_initial_power_state(pb);
  497. backlight_update_status(bl);
  498. platform_set_drvdata(pdev, bl);
  499. return 0;
  500. err_alloc:
  501. if (data->exit)
  502. data->exit(&pdev->dev);
  503. return ret;
  504. }
  505. static int pwm_backlight_remove(struct platform_device *pdev)
  506. {
  507. struct backlight_device *bl = platform_get_drvdata(pdev);
  508. struct pwm_bl_data *pb = bl_get_data(bl);
  509. backlight_device_unregister(bl);
  510. pwm_backlight_power_off(pb);
  511. if (pb->exit)
  512. pb->exit(&pdev->dev);
  513. if (pb->legacy)
  514. pwm_free(pb->pwm);
  515. return 0;
  516. }
  517. static void pwm_backlight_shutdown(struct platform_device *pdev)
  518. {
  519. struct backlight_device *bl = platform_get_drvdata(pdev);
  520. struct pwm_bl_data *pb = bl_get_data(bl);
  521. pwm_backlight_power_off(pb);
  522. }
  523. #ifdef CONFIG_PM_SLEEP
  524. static int pwm_backlight_suspend(struct device *dev)
  525. {
  526. struct backlight_device *bl = dev_get_drvdata(dev);
  527. struct pwm_bl_data *pb = bl_get_data(bl);
  528. if (pb->notify)
  529. pb->notify(pb->dev, 0);
  530. pwm_backlight_power_off(pb);
  531. if (pb->notify_after)
  532. pb->notify_after(pb->dev, 0);
  533. return 0;
  534. }
  535. static int pwm_backlight_resume(struct device *dev)
  536. {
  537. struct backlight_device *bl = dev_get_drvdata(dev);
  538. backlight_update_status(bl);
  539. return 0;
  540. }
  541. #endif
  542. static const struct dev_pm_ops pwm_backlight_pm_ops = {
  543. #ifdef CONFIG_PM_SLEEP
  544. .suspend = pwm_backlight_suspend,
  545. .resume = pwm_backlight_resume,
  546. .poweroff = pwm_backlight_suspend,
  547. .restore = pwm_backlight_resume,
  548. #endif
  549. };
  550. static struct platform_driver pwm_backlight_driver = {
  551. .driver = {
  552. .name = "pwm-backlight",
  553. .pm = &pwm_backlight_pm_ops,
  554. .of_match_table = of_match_ptr(pwm_backlight_of_match),
  555. },
  556. .probe = pwm_backlight_probe,
  557. .remove = pwm_backlight_remove,
  558. .shutdown = pwm_backlight_shutdown,
  559. };
  560. module_platform_driver(pwm_backlight_driver);
  561. MODULE_DESCRIPTION("PWM based Backlight Driver");
  562. MODULE_LICENSE("GPL");
  563. MODULE_ALIAS("platform:pwm-backlight");