pwm_bl.c 9.7 KB

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