pwm_bl.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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. int (*notify)(struct device *,
  36. int brightness);
  37. void (*notify_after)(struct device *,
  38. int brightness);
  39. int (*check_fb)(struct device *, struct fb_info *);
  40. void (*exit)(struct device *);
  41. };
  42. static void pwm_backlight_power_on(struct pwm_bl_data *pb, int brightness)
  43. {
  44. int err;
  45. if (pb->enabled)
  46. return;
  47. err = regulator_enable(pb->power_supply);
  48. if (err < 0)
  49. dev_err(pb->dev, "failed to enable power supply\n");
  50. if (pb->enable_gpio)
  51. gpiod_set_value(pb->enable_gpio, 1);
  52. pwm_enable(pb->pwm);
  53. pb->enabled = true;
  54. }
  55. static void pwm_backlight_power_off(struct pwm_bl_data *pb)
  56. {
  57. if (!pb->enabled)
  58. return;
  59. pwm_config(pb->pwm, 0, pb->period);
  60. pwm_disable(pb->pwm);
  61. if (pb->enable_gpio)
  62. gpiod_set_value(pb->enable_gpio, 0);
  63. regulator_disable(pb->power_supply);
  64. pb->enabled = false;
  65. }
  66. static int compute_duty_cycle(struct pwm_bl_data *pb, int brightness)
  67. {
  68. unsigned int lth = pb->lth_brightness;
  69. int duty_cycle;
  70. if (pb->levels)
  71. duty_cycle = pb->levels[brightness];
  72. else
  73. duty_cycle = brightness;
  74. return (duty_cycle * (pb->period - lth) / pb->scale) + lth;
  75. }
  76. static int pwm_backlight_update_status(struct backlight_device *bl)
  77. {
  78. struct pwm_bl_data *pb = bl_get_data(bl);
  79. int brightness = bl->props.brightness;
  80. int duty_cycle;
  81. if (bl->props.power != FB_BLANK_UNBLANK ||
  82. bl->props.fb_blank != FB_BLANK_UNBLANK ||
  83. bl->props.state & BL_CORE_FBBLANK)
  84. brightness = 0;
  85. if (pb->notify)
  86. brightness = pb->notify(pb->dev, brightness);
  87. if (brightness > 0) {
  88. duty_cycle = compute_duty_cycle(pb, brightness);
  89. pwm_config(pb->pwm, duty_cycle, pb->period);
  90. pwm_backlight_power_on(pb, brightness);
  91. } else
  92. pwm_backlight_power_off(pb);
  93. if (pb->notify_after)
  94. pb->notify_after(pb->dev, brightness);
  95. return 0;
  96. }
  97. static int pwm_backlight_check_fb(struct backlight_device *bl,
  98. struct fb_info *info)
  99. {
  100. struct pwm_bl_data *pb = bl_get_data(bl);
  101. return !pb->check_fb || pb->check_fb(pb->dev, info);
  102. }
  103. static const struct backlight_ops pwm_backlight_ops = {
  104. .update_status = pwm_backlight_update_status,
  105. .check_fb = pwm_backlight_check_fb,
  106. };
  107. #ifdef CONFIG_OF
  108. static int pwm_backlight_parse_dt(struct device *dev,
  109. struct platform_pwm_backlight_data *data)
  110. {
  111. struct device_node *node = dev->of_node;
  112. struct property *prop;
  113. int length;
  114. u32 value;
  115. int ret;
  116. if (!node)
  117. return -ENODEV;
  118. memset(data, 0, sizeof(*data));
  119. /* determine the number of brightness levels */
  120. prop = of_find_property(node, "brightness-levels", &length);
  121. if (!prop)
  122. return -EINVAL;
  123. data->max_brightness = length / sizeof(u32);
  124. /* read brightness levels from DT property */
  125. if (data->max_brightness > 0) {
  126. size_t size = sizeof(*data->levels) * data->max_brightness;
  127. data->levels = devm_kzalloc(dev, size, GFP_KERNEL);
  128. if (!data->levels)
  129. return -ENOMEM;
  130. ret = of_property_read_u32_array(node, "brightness-levels",
  131. data->levels,
  132. data->max_brightness);
  133. if (ret < 0)
  134. return ret;
  135. ret = of_property_read_u32(node, "default-brightness-level",
  136. &value);
  137. if (ret < 0)
  138. return ret;
  139. data->dft_brightness = value;
  140. data->max_brightness--;
  141. }
  142. data->enable_gpio = -EINVAL;
  143. return 0;
  144. }
  145. static struct of_device_id pwm_backlight_of_match[] = {
  146. { .compatible = "pwm-backlight" },
  147. { }
  148. };
  149. MODULE_DEVICE_TABLE(of, pwm_backlight_of_match);
  150. #else
  151. static int pwm_backlight_parse_dt(struct device *dev,
  152. struct platform_pwm_backlight_data *data)
  153. {
  154. return -ENODEV;
  155. }
  156. #endif
  157. static int pwm_backlight_probe(struct platform_device *pdev)
  158. {
  159. struct platform_pwm_backlight_data *data = dev_get_platdata(&pdev->dev);
  160. struct platform_pwm_backlight_data defdata;
  161. struct backlight_properties props;
  162. struct backlight_device *bl;
  163. struct pwm_bl_data *pb;
  164. int ret;
  165. if (!data) {
  166. ret = pwm_backlight_parse_dt(&pdev->dev, &defdata);
  167. if (ret < 0) {
  168. dev_err(&pdev->dev, "failed to find platform data\n");
  169. return ret;
  170. }
  171. data = &defdata;
  172. }
  173. if (data->init) {
  174. ret = data->init(&pdev->dev);
  175. if (ret < 0)
  176. return ret;
  177. }
  178. pb = devm_kzalloc(&pdev->dev, sizeof(*pb), GFP_KERNEL);
  179. if (!pb) {
  180. ret = -ENOMEM;
  181. goto err_alloc;
  182. }
  183. if (data->levels) {
  184. unsigned int i;
  185. for (i = 0; i <= data->max_brightness; i++)
  186. if (data->levels[i] > pb->scale)
  187. pb->scale = data->levels[i];
  188. pb->levels = data->levels;
  189. } else
  190. pb->scale = data->max_brightness;
  191. pb->notify = data->notify;
  192. pb->notify_after = data->notify_after;
  193. pb->check_fb = data->check_fb;
  194. pb->exit = data->exit;
  195. pb->dev = &pdev->dev;
  196. pb->enabled = false;
  197. pb->enable_gpio = devm_gpiod_get_optional(&pdev->dev, "enable");
  198. if (IS_ERR(pb->enable_gpio)) {
  199. ret = PTR_ERR(pb->enable_gpio);
  200. goto err_alloc;
  201. }
  202. /*
  203. * Compatibility fallback for drivers still using the integer GPIO
  204. * platform data. Must go away soon.
  205. */
  206. if (!pb->enable_gpio && gpio_is_valid(data->enable_gpio)) {
  207. ret = devm_gpio_request_one(&pdev->dev, data->enable_gpio,
  208. GPIOF_OUT_INIT_HIGH, "enable");
  209. if (ret < 0) {
  210. dev_err(&pdev->dev, "failed to request GPIO#%d: %d\n",
  211. data->enable_gpio, ret);
  212. goto err_alloc;
  213. }
  214. pb->enable_gpio = gpio_to_desc(data->enable_gpio);
  215. }
  216. if (pb->enable_gpio)
  217. gpiod_direction_output(pb->enable_gpio, 1);
  218. pb->power_supply = devm_regulator_get(&pdev->dev, "power");
  219. if (IS_ERR(pb->power_supply)) {
  220. ret = PTR_ERR(pb->power_supply);
  221. goto err_alloc;
  222. }
  223. pb->pwm = devm_pwm_get(&pdev->dev, NULL);
  224. if (IS_ERR(pb->pwm)) {
  225. dev_err(&pdev->dev, "unable to request PWM, trying legacy API\n");
  226. pb->pwm = pwm_request(data->pwm_id, "pwm-backlight");
  227. if (IS_ERR(pb->pwm)) {
  228. dev_err(&pdev->dev, "unable to request legacy PWM\n");
  229. ret = PTR_ERR(pb->pwm);
  230. goto err_alloc;
  231. }
  232. }
  233. dev_dbg(&pdev->dev, "got pwm for backlight\n");
  234. /*
  235. * The DT case will set the pwm_period_ns field to 0 and store the
  236. * period, parsed from the DT, in the PWM device. For the non-DT case,
  237. * set the period from platform data if it has not already been set
  238. * via the PWM lookup table.
  239. */
  240. pb->period = pwm_get_period(pb->pwm);
  241. if (!pb->period && (data->pwm_period_ns > 0)) {
  242. pb->period = data->pwm_period_ns;
  243. pwm_set_period(pb->pwm, data->pwm_period_ns);
  244. }
  245. pb->lth_brightness = data->lth_brightness * (pb->period / pb->scale);
  246. memset(&props, 0, sizeof(struct backlight_properties));
  247. props.type = BACKLIGHT_RAW;
  248. props.max_brightness = data->max_brightness;
  249. bl = backlight_device_register(dev_name(&pdev->dev), &pdev->dev, pb,
  250. &pwm_backlight_ops, &props);
  251. if (IS_ERR(bl)) {
  252. dev_err(&pdev->dev, "failed to register backlight\n");
  253. ret = PTR_ERR(bl);
  254. goto err_alloc;
  255. }
  256. if (data->dft_brightness > data->max_brightness) {
  257. dev_warn(&pdev->dev,
  258. "invalid default brightness level: %u, using %u\n",
  259. data->dft_brightness, data->max_brightness);
  260. data->dft_brightness = data->max_brightness;
  261. }
  262. bl->props.brightness = data->dft_brightness;
  263. backlight_update_status(bl);
  264. platform_set_drvdata(pdev, bl);
  265. return 0;
  266. err_alloc:
  267. if (data->exit)
  268. data->exit(&pdev->dev);
  269. return ret;
  270. }
  271. static int pwm_backlight_remove(struct platform_device *pdev)
  272. {
  273. struct backlight_device *bl = platform_get_drvdata(pdev);
  274. struct pwm_bl_data *pb = bl_get_data(bl);
  275. backlight_device_unregister(bl);
  276. pwm_backlight_power_off(pb);
  277. if (pb->exit)
  278. pb->exit(&pdev->dev);
  279. return 0;
  280. }
  281. static void pwm_backlight_shutdown(struct platform_device *pdev)
  282. {
  283. struct backlight_device *bl = platform_get_drvdata(pdev);
  284. struct pwm_bl_data *pb = bl_get_data(bl);
  285. pwm_backlight_power_off(pb);
  286. }
  287. #ifdef CONFIG_PM_SLEEP
  288. static int pwm_backlight_suspend(struct device *dev)
  289. {
  290. struct backlight_device *bl = dev_get_drvdata(dev);
  291. struct pwm_bl_data *pb = bl_get_data(bl);
  292. if (pb->notify)
  293. pb->notify(pb->dev, 0);
  294. pwm_backlight_power_off(pb);
  295. if (pb->notify_after)
  296. pb->notify_after(pb->dev, 0);
  297. return 0;
  298. }
  299. static int pwm_backlight_resume(struct device *dev)
  300. {
  301. struct backlight_device *bl = dev_get_drvdata(dev);
  302. backlight_update_status(bl);
  303. return 0;
  304. }
  305. #endif
  306. static const struct dev_pm_ops pwm_backlight_pm_ops = {
  307. #ifdef CONFIG_PM_SLEEP
  308. .suspend = pwm_backlight_suspend,
  309. .resume = pwm_backlight_resume,
  310. .poweroff = pwm_backlight_suspend,
  311. .restore = pwm_backlight_resume,
  312. #endif
  313. };
  314. static struct platform_driver pwm_backlight_driver = {
  315. .driver = {
  316. .name = "pwm-backlight",
  317. .pm = &pwm_backlight_pm_ops,
  318. .of_match_table = of_match_ptr(pwm_backlight_of_match),
  319. },
  320. .probe = pwm_backlight_probe,
  321. .remove = pwm_backlight_remove,
  322. .shutdown = pwm_backlight_shutdown,
  323. };
  324. module_platform_driver(pwm_backlight_driver);
  325. MODULE_DESCRIPTION("PWM based Backlight Driver");
  326. MODULE_LICENSE("GPL");
  327. MODULE_ALIAS("platform:pwm-backlight");