pwm_bl.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  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/gpio/consumer.h>
  13. #include <linux/gpio.h>
  14. #include <linux/module.h>
  15. #include <linux/kernel.h>
  16. #include <linux/init.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/fb.h>
  19. #include <linux/backlight.h>
  20. #include <linux/err.h>
  21. #include <linux/pwm.h>
  22. #include <linux/pwm_backlight.h>
  23. #include <linux/regulator/consumer.h>
  24. #include <linux/slab.h>
  25. struct pwm_bl_data {
  26. struct pwm_device *pwm;
  27. struct device *dev;
  28. unsigned int period;
  29. unsigned int lth_brightness;
  30. unsigned int *levels;
  31. bool enabled;
  32. struct regulator *power_supply;
  33. struct gpio_desc *enable_gpio;
  34. unsigned int scale;
  35. bool legacy;
  36. int (*notify)(struct device *,
  37. int brightness);
  38. void (*notify_after)(struct device *,
  39. int brightness);
  40. int (*check_fb)(struct device *, struct fb_info *);
  41. void (*exit)(struct device *);
  42. };
  43. static void pwm_backlight_power_on(struct pwm_bl_data *pb, int brightness)
  44. {
  45. int err;
  46. if (pb->enabled)
  47. return;
  48. err = regulator_enable(pb->power_supply);
  49. if (err < 0)
  50. dev_err(pb->dev, "failed to enable power supply\n");
  51. if (pb->enable_gpio)
  52. gpiod_set_value_cansleep(pb->enable_gpio, 1);
  53. pwm_enable(pb->pwm);
  54. pb->enabled = true;
  55. }
  56. static void pwm_backlight_power_off(struct pwm_bl_data *pb)
  57. {
  58. if (!pb->enabled)
  59. return;
  60. pwm_config(pb->pwm, 0, pb->period);
  61. pwm_disable(pb->pwm);
  62. if (pb->enable_gpio)
  63. gpiod_set_value_cansleep(pb->enable_gpio, 0);
  64. regulator_disable(pb->power_supply);
  65. pb->enabled = false;
  66. }
  67. static int compute_duty_cycle(struct pwm_bl_data *pb, int brightness)
  68. {
  69. unsigned int lth = pb->lth_brightness;
  70. int duty_cycle;
  71. if (pb->levels)
  72. duty_cycle = pb->levels[brightness];
  73. else
  74. duty_cycle = brightness;
  75. return (duty_cycle * (pb->period - lth) / pb->scale) + lth;
  76. }
  77. static int pwm_backlight_update_status(struct backlight_device *bl)
  78. {
  79. struct pwm_bl_data *pb = bl_get_data(bl);
  80. int brightness = bl->props.brightness;
  81. int duty_cycle;
  82. if (bl->props.power != FB_BLANK_UNBLANK ||
  83. bl->props.fb_blank != FB_BLANK_UNBLANK ||
  84. bl->props.state & BL_CORE_FBBLANK)
  85. brightness = 0;
  86. if (pb->notify)
  87. brightness = pb->notify(pb->dev, brightness);
  88. if (brightness > 0) {
  89. duty_cycle = compute_duty_cycle(pb, brightness);
  90. pwm_config(pb->pwm, duty_cycle, pb->period);
  91. pwm_backlight_power_on(pb, brightness);
  92. } else
  93. pwm_backlight_power_off(pb);
  94. if (pb->notify_after)
  95. pb->notify_after(pb->dev, brightness);
  96. return 0;
  97. }
  98. static int pwm_backlight_check_fb(struct backlight_device *bl,
  99. struct fb_info *info)
  100. {
  101. struct pwm_bl_data *pb = bl_get_data(bl);
  102. return !pb->check_fb || pb->check_fb(pb->dev, info);
  103. }
  104. static const struct backlight_ops pwm_backlight_ops = {
  105. .update_status = pwm_backlight_update_status,
  106. .check_fb = pwm_backlight_check_fb,
  107. };
  108. #ifdef CONFIG_OF
  109. static int pwm_backlight_parse_dt(struct device *dev,
  110. struct platform_pwm_backlight_data *data)
  111. {
  112. struct device_node *node = dev->of_node;
  113. struct property *prop;
  114. int length;
  115. u32 value;
  116. int ret;
  117. if (!node)
  118. return -ENODEV;
  119. memset(data, 0, sizeof(*data));
  120. /* determine the number of brightness levels */
  121. prop = of_find_property(node, "brightness-levels", &length);
  122. if (!prop)
  123. return -EINVAL;
  124. data->max_brightness = length / sizeof(u32);
  125. /* read brightness levels from DT property */
  126. if (data->max_brightness > 0) {
  127. size_t size = sizeof(*data->levels) * data->max_brightness;
  128. data->levels = devm_kzalloc(dev, size, GFP_KERNEL);
  129. if (!data->levels)
  130. return -ENOMEM;
  131. ret = of_property_read_u32_array(node, "brightness-levels",
  132. data->levels,
  133. data->max_brightness);
  134. if (ret < 0)
  135. return ret;
  136. ret = of_property_read_u32(node, "default-brightness-level",
  137. &value);
  138. if (ret < 0)
  139. return ret;
  140. data->dft_brightness = value;
  141. data->max_brightness--;
  142. }
  143. data->enable_gpio = -EINVAL;
  144. return 0;
  145. }
  146. static struct of_device_id pwm_backlight_of_match[] = {
  147. { .compatible = "pwm-backlight" },
  148. { }
  149. };
  150. MODULE_DEVICE_TABLE(of, pwm_backlight_of_match);
  151. #else
  152. static int pwm_backlight_parse_dt(struct device *dev,
  153. struct platform_pwm_backlight_data *data)
  154. {
  155. return -ENODEV;
  156. }
  157. #endif
  158. static int pwm_backlight_initial_power_state(const struct pwm_bl_data *pb)
  159. {
  160. struct device_node *node = pb->dev->of_node;
  161. /* Not booted with device tree or no phandle link to the node */
  162. if (!node || !node->phandle)
  163. return FB_BLANK_UNBLANK;
  164. /*
  165. * If the driver is probed from the device tree and there is a
  166. * phandle link pointing to the backlight node, it is safe to
  167. * assume that another driver will enable the backlight at the
  168. * appropriate time. Therefore, if it is disabled, keep it so.
  169. */
  170. /* if the enable GPIO is disabled, do not enable the backlight */
  171. if (pb->enable_gpio && gpiod_get_value(pb->enable_gpio) == 0)
  172. return FB_BLANK_POWERDOWN;
  173. /* The regulator is disabled, do not enable the backlight */
  174. if (!regulator_is_enabled(pb->power_supply))
  175. return FB_BLANK_POWERDOWN;
  176. /* The PWM is disabled, keep it like this */
  177. if (!pwm_is_enabled(pb->pwm))
  178. return FB_BLANK_POWERDOWN;
  179. return FB_BLANK_UNBLANK;
  180. }
  181. static int pwm_backlight_probe(struct platform_device *pdev)
  182. {
  183. struct platform_pwm_backlight_data *data = dev_get_platdata(&pdev->dev);
  184. struct platform_pwm_backlight_data defdata;
  185. struct backlight_properties props;
  186. struct backlight_device *bl;
  187. struct device_node *node = pdev->dev.of_node;
  188. struct pwm_bl_data *pb;
  189. struct pwm_args pargs;
  190. int ret;
  191. if (!data) {
  192. ret = pwm_backlight_parse_dt(&pdev->dev, &defdata);
  193. if (ret < 0) {
  194. dev_err(&pdev->dev, "failed to find platform data\n");
  195. return ret;
  196. }
  197. data = &defdata;
  198. }
  199. if (data->init) {
  200. ret = data->init(&pdev->dev);
  201. if (ret < 0)
  202. return ret;
  203. }
  204. pb = devm_kzalloc(&pdev->dev, sizeof(*pb), GFP_KERNEL);
  205. if (!pb) {
  206. ret = -ENOMEM;
  207. goto err_alloc;
  208. }
  209. if (data->levels) {
  210. unsigned int i;
  211. for (i = 0; i <= data->max_brightness; i++)
  212. if (data->levels[i] > pb->scale)
  213. pb->scale = data->levels[i];
  214. pb->levels = data->levels;
  215. } else
  216. pb->scale = data->max_brightness;
  217. pb->notify = data->notify;
  218. pb->notify_after = data->notify_after;
  219. pb->check_fb = data->check_fb;
  220. pb->exit = data->exit;
  221. pb->dev = &pdev->dev;
  222. pb->enabled = false;
  223. pb->enable_gpio = devm_gpiod_get_optional(&pdev->dev, "enable",
  224. GPIOD_ASIS);
  225. if (IS_ERR(pb->enable_gpio)) {
  226. ret = PTR_ERR(pb->enable_gpio);
  227. goto err_alloc;
  228. }
  229. /*
  230. * Compatibility fallback for drivers still using the integer GPIO
  231. * platform data. Must go away soon.
  232. */
  233. if (!pb->enable_gpio && gpio_is_valid(data->enable_gpio)) {
  234. ret = devm_gpio_request_one(&pdev->dev, data->enable_gpio,
  235. GPIOF_OUT_INIT_HIGH, "enable");
  236. if (ret < 0) {
  237. dev_err(&pdev->dev, "failed to request GPIO#%d: %d\n",
  238. data->enable_gpio, ret);
  239. goto err_alloc;
  240. }
  241. pb->enable_gpio = gpio_to_desc(data->enable_gpio);
  242. }
  243. /*
  244. * If the GPIO is configured as input, change the direction to output
  245. * and set the GPIO as active.
  246. * Do not force the GPIO to active when it was already output as it
  247. * could cause backlight flickering or we would enable the backlight too
  248. * early. Leave the decision of the initial backlight state for later.
  249. */
  250. if (pb->enable_gpio &&
  251. gpiod_get_direction(pb->enable_gpio) == GPIOF_DIR_IN)
  252. gpiod_direction_output(pb->enable_gpio, 1);
  253. pb->power_supply = devm_regulator_get(&pdev->dev, "power");
  254. if (IS_ERR(pb->power_supply)) {
  255. ret = PTR_ERR(pb->power_supply);
  256. goto err_alloc;
  257. }
  258. pb->pwm = devm_pwm_get(&pdev->dev, NULL);
  259. if (IS_ERR(pb->pwm) && PTR_ERR(pb->pwm) != -EPROBE_DEFER && !node) {
  260. dev_err(&pdev->dev, "unable to request PWM, trying legacy API\n");
  261. pb->legacy = true;
  262. pb->pwm = pwm_request(data->pwm_id, "pwm-backlight");
  263. }
  264. if (IS_ERR(pb->pwm)) {
  265. ret = PTR_ERR(pb->pwm);
  266. if (ret != -EPROBE_DEFER)
  267. dev_err(&pdev->dev, "unable to request PWM\n");
  268. goto err_alloc;
  269. }
  270. dev_dbg(&pdev->dev, "got pwm for backlight\n");
  271. /*
  272. * FIXME: pwm_apply_args() should be removed when switching to
  273. * the atomic PWM API.
  274. */
  275. pwm_apply_args(pb->pwm);
  276. /*
  277. * The DT case will set the pwm_period_ns field to 0 and store the
  278. * period, parsed from the DT, in the PWM device. For the non-DT case,
  279. * set the period from platform data if it has not already been set
  280. * via the PWM lookup table.
  281. */
  282. pwm_get_args(pb->pwm, &pargs);
  283. pb->period = pargs.period;
  284. if (!pb->period && (data->pwm_period_ns > 0))
  285. pb->period = data->pwm_period_ns;
  286. pb->lth_brightness = data->lth_brightness * (pb->period / pb->scale);
  287. memset(&props, 0, sizeof(struct backlight_properties));
  288. props.type = BACKLIGHT_RAW;
  289. props.max_brightness = data->max_brightness;
  290. bl = backlight_device_register(dev_name(&pdev->dev), &pdev->dev, pb,
  291. &pwm_backlight_ops, &props);
  292. if (IS_ERR(bl)) {
  293. dev_err(&pdev->dev, "failed to register backlight\n");
  294. ret = PTR_ERR(bl);
  295. if (pb->legacy)
  296. pwm_free(pb->pwm);
  297. goto err_alloc;
  298. }
  299. if (data->dft_brightness > data->max_brightness) {
  300. dev_warn(&pdev->dev,
  301. "invalid default brightness level: %u, using %u\n",
  302. data->dft_brightness, data->max_brightness);
  303. data->dft_brightness = data->max_brightness;
  304. }
  305. bl->props.brightness = data->dft_brightness;
  306. bl->props.power = pwm_backlight_initial_power_state(pb);
  307. backlight_update_status(bl);
  308. platform_set_drvdata(pdev, bl);
  309. return 0;
  310. err_alloc:
  311. if (data->exit)
  312. data->exit(&pdev->dev);
  313. return ret;
  314. }
  315. static int pwm_backlight_remove(struct platform_device *pdev)
  316. {
  317. struct backlight_device *bl = platform_get_drvdata(pdev);
  318. struct pwm_bl_data *pb = bl_get_data(bl);
  319. backlight_device_unregister(bl);
  320. pwm_backlight_power_off(pb);
  321. if (pb->exit)
  322. pb->exit(&pdev->dev);
  323. if (pb->legacy)
  324. pwm_free(pb->pwm);
  325. return 0;
  326. }
  327. static void pwm_backlight_shutdown(struct platform_device *pdev)
  328. {
  329. struct backlight_device *bl = platform_get_drvdata(pdev);
  330. struct pwm_bl_data *pb = bl_get_data(bl);
  331. pwm_backlight_power_off(pb);
  332. }
  333. #ifdef CONFIG_PM_SLEEP
  334. static int pwm_backlight_suspend(struct device *dev)
  335. {
  336. struct backlight_device *bl = dev_get_drvdata(dev);
  337. struct pwm_bl_data *pb = bl_get_data(bl);
  338. if (pb->notify)
  339. pb->notify(pb->dev, 0);
  340. pwm_backlight_power_off(pb);
  341. if (pb->notify_after)
  342. pb->notify_after(pb->dev, 0);
  343. return 0;
  344. }
  345. static int pwm_backlight_resume(struct device *dev)
  346. {
  347. struct backlight_device *bl = dev_get_drvdata(dev);
  348. backlight_update_status(bl);
  349. return 0;
  350. }
  351. #endif
  352. static const struct dev_pm_ops pwm_backlight_pm_ops = {
  353. #ifdef CONFIG_PM_SLEEP
  354. .suspend = pwm_backlight_suspend,
  355. .resume = pwm_backlight_resume,
  356. .poweroff = pwm_backlight_suspend,
  357. .restore = pwm_backlight_resume,
  358. #endif
  359. };
  360. static struct platform_driver pwm_backlight_driver = {
  361. .driver = {
  362. .name = "pwm-backlight",
  363. .pm = &pwm_backlight_pm_ops,
  364. .of_match_table = of_match_ptr(pwm_backlight_of_match),
  365. },
  366. .probe = pwm_backlight_probe,
  367. .remove = pwm_backlight_remove,
  368. .shutdown = pwm_backlight_shutdown,
  369. };
  370. module_platform_driver(pwm_backlight_driver);
  371. MODULE_DESCRIPTION("PWM based Backlight Driver");
  372. MODULE_LICENSE("GPL");
  373. MODULE_ALIAS("platform:pwm-backlight");