pwm_bl.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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/module.h>
  13. #include <linux/kernel.h>
  14. #include <linux/init.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/fb.h>
  17. #include <linux/backlight.h>
  18. #include <linux/err.h>
  19. #include <linux/pwm.h>
  20. #include <linux/pwm_backlight.h>
  21. #include <linux/slab.h>
  22. struct pwm_bl_data {
  23. struct pwm_device *pwm;
  24. struct device *dev;
  25. unsigned int period;
  26. unsigned int lth_brightness;
  27. unsigned int *levels;
  28. int (*notify)(struct device *,
  29. int brightness);
  30. void (*notify_after)(struct device *,
  31. int brightness);
  32. int (*check_fb)(struct device *, struct fb_info *);
  33. void (*exit)(struct device *);
  34. };
  35. static int pwm_backlight_update_status(struct backlight_device *bl)
  36. {
  37. struct pwm_bl_data *pb = bl_get_data(bl);
  38. int brightness = bl->props.brightness;
  39. int max = bl->props.max_brightness;
  40. if (bl->props.power != FB_BLANK_UNBLANK ||
  41. bl->props.fb_blank != FB_BLANK_UNBLANK ||
  42. bl->props.state & BL_CORE_FBBLANK)
  43. brightness = 0;
  44. if (pb->notify)
  45. brightness = pb->notify(pb->dev, brightness);
  46. if (brightness == 0) {
  47. pwm_config(pb->pwm, 0, pb->period);
  48. pwm_disable(pb->pwm);
  49. } else {
  50. int duty_cycle;
  51. if (pb->levels) {
  52. duty_cycle = pb->levels[brightness];
  53. max = pb->levels[max];
  54. } else {
  55. duty_cycle = brightness;
  56. }
  57. duty_cycle = pb->lth_brightness +
  58. (duty_cycle * (pb->period - pb->lth_brightness) / max);
  59. pwm_config(pb->pwm, duty_cycle, pb->period);
  60. pwm_enable(pb->pwm);
  61. }
  62. if (pb->notify_after)
  63. pb->notify_after(pb->dev, brightness);
  64. return 0;
  65. }
  66. static int pwm_backlight_get_brightness(struct backlight_device *bl)
  67. {
  68. return bl->props.brightness;
  69. }
  70. static int pwm_backlight_check_fb(struct backlight_device *bl,
  71. struct fb_info *info)
  72. {
  73. struct pwm_bl_data *pb = bl_get_data(bl);
  74. return !pb->check_fb || pb->check_fb(pb->dev, info);
  75. }
  76. static const struct backlight_ops pwm_backlight_ops = {
  77. .update_status = pwm_backlight_update_status,
  78. .get_brightness = pwm_backlight_get_brightness,
  79. .check_fb = pwm_backlight_check_fb,
  80. };
  81. #ifdef CONFIG_OF
  82. static int pwm_backlight_parse_dt(struct device *dev,
  83. struct platform_pwm_backlight_data *data)
  84. {
  85. struct device_node *node = dev->of_node;
  86. struct property *prop;
  87. int length;
  88. u32 value;
  89. int ret;
  90. if (!node)
  91. return -ENODEV;
  92. memset(data, 0, sizeof(*data));
  93. /* determine the number of brightness levels */
  94. prop = of_find_property(node, "brightness-levels", &length);
  95. if (!prop)
  96. return -EINVAL;
  97. data->max_brightness = length / sizeof(u32);
  98. /* read brightness levels from DT property */
  99. if (data->max_brightness > 0) {
  100. size_t size = sizeof(*data->levels) * data->max_brightness;
  101. data->levels = devm_kzalloc(dev, size, GFP_KERNEL);
  102. if (!data->levels)
  103. return -ENOMEM;
  104. ret = of_property_read_u32_array(node, "brightness-levels",
  105. data->levels,
  106. data->max_brightness);
  107. if (ret < 0)
  108. return ret;
  109. ret = of_property_read_u32(node, "default-brightness-level",
  110. &value);
  111. if (ret < 0)
  112. return ret;
  113. data->dft_brightness = value;
  114. data->max_brightness--;
  115. }
  116. /*
  117. * TODO: Most users of this driver use a number of GPIOs to control
  118. * backlight power. Support for specifying these needs to be
  119. * added.
  120. */
  121. return 0;
  122. }
  123. static struct of_device_id pwm_backlight_of_match[] = {
  124. { .compatible = "pwm-backlight" },
  125. { }
  126. };
  127. MODULE_DEVICE_TABLE(of, pwm_backlight_of_match);
  128. #else
  129. static int pwm_backlight_parse_dt(struct device *dev,
  130. struct platform_pwm_backlight_data *data)
  131. {
  132. return -ENODEV;
  133. }
  134. #endif
  135. static int pwm_backlight_probe(struct platform_device *pdev)
  136. {
  137. struct platform_pwm_backlight_data *data = pdev->dev.platform_data;
  138. struct platform_pwm_backlight_data defdata;
  139. struct backlight_properties props;
  140. struct backlight_device *bl;
  141. struct pwm_bl_data *pb;
  142. unsigned int max;
  143. int ret;
  144. if (!data) {
  145. ret = pwm_backlight_parse_dt(&pdev->dev, &defdata);
  146. if (ret < 0) {
  147. dev_err(&pdev->dev, "failed to find platform data\n");
  148. return ret;
  149. }
  150. data = &defdata;
  151. }
  152. if (data->init) {
  153. ret = data->init(&pdev->dev);
  154. if (ret < 0)
  155. return ret;
  156. }
  157. pb = devm_kzalloc(&pdev->dev, sizeof(*pb), GFP_KERNEL);
  158. if (!pb) {
  159. dev_err(&pdev->dev, "no memory for state\n");
  160. ret = -ENOMEM;
  161. goto err_alloc;
  162. }
  163. if (data->levels) {
  164. max = data->levels[data->max_brightness];
  165. pb->levels = data->levels;
  166. } else
  167. max = data->max_brightness;
  168. pb->notify = data->notify;
  169. pb->notify_after = data->notify_after;
  170. pb->check_fb = data->check_fb;
  171. pb->exit = data->exit;
  172. pb->dev = &pdev->dev;
  173. pb->pwm = devm_pwm_get(&pdev->dev, NULL);
  174. if (IS_ERR(pb->pwm)) {
  175. dev_err(&pdev->dev, "unable to request PWM, trying legacy API\n");
  176. pb->pwm = pwm_request(data->pwm_id, "pwm-backlight");
  177. if (IS_ERR(pb->pwm)) {
  178. dev_err(&pdev->dev, "unable to request legacy PWM\n");
  179. ret = PTR_ERR(pb->pwm);
  180. goto err_alloc;
  181. }
  182. }
  183. dev_dbg(&pdev->dev, "got pwm for backlight\n");
  184. /*
  185. * The DT case will set the pwm_period_ns field to 0 and store the
  186. * period, parsed from the DT, in the PWM device. For the non-DT case,
  187. * set the period from platform data.
  188. */
  189. if (data->pwm_period_ns > 0)
  190. pwm_set_period(pb->pwm, data->pwm_period_ns);
  191. pb->period = pwm_get_period(pb->pwm);
  192. pb->lth_brightness = data->lth_brightness * (pb->period / max);
  193. memset(&props, 0, sizeof(struct backlight_properties));
  194. props.type = BACKLIGHT_RAW;
  195. props.max_brightness = data->max_brightness;
  196. bl = backlight_device_register(dev_name(&pdev->dev), &pdev->dev, pb,
  197. &pwm_backlight_ops, &props);
  198. if (IS_ERR(bl)) {
  199. dev_err(&pdev->dev, "failed to register backlight\n");
  200. ret = PTR_ERR(bl);
  201. goto err_alloc;
  202. }
  203. if (data->dft_brightness > data->max_brightness) {
  204. dev_warn(&pdev->dev,
  205. "invalid default brightness level: %u, using %u\n",
  206. data->dft_brightness, data->max_brightness);
  207. data->dft_brightness = data->max_brightness;
  208. }
  209. bl->props.brightness = data->dft_brightness;
  210. backlight_update_status(bl);
  211. platform_set_drvdata(pdev, bl);
  212. return 0;
  213. err_alloc:
  214. if (data->exit)
  215. data->exit(&pdev->dev);
  216. return ret;
  217. }
  218. static int pwm_backlight_remove(struct platform_device *pdev)
  219. {
  220. struct backlight_device *bl = platform_get_drvdata(pdev);
  221. struct pwm_bl_data *pb = bl_get_data(bl);
  222. backlight_device_unregister(bl);
  223. pwm_config(pb->pwm, 0, pb->period);
  224. pwm_disable(pb->pwm);
  225. if (pb->exit)
  226. pb->exit(&pdev->dev);
  227. return 0;
  228. }
  229. #ifdef CONFIG_PM
  230. static int pwm_backlight_suspend(struct device *dev)
  231. {
  232. struct backlight_device *bl = dev_get_drvdata(dev);
  233. struct pwm_bl_data *pb = bl_get_data(bl);
  234. if (pb->notify)
  235. pb->notify(pb->dev, 0);
  236. pwm_config(pb->pwm, 0, pb->period);
  237. pwm_disable(pb->pwm);
  238. if (pb->notify_after)
  239. pb->notify_after(pb->dev, 0);
  240. return 0;
  241. }
  242. static int pwm_backlight_resume(struct device *dev)
  243. {
  244. struct backlight_device *bl = dev_get_drvdata(dev);
  245. backlight_update_status(bl);
  246. return 0;
  247. }
  248. static SIMPLE_DEV_PM_OPS(pwm_backlight_pm_ops, pwm_backlight_suspend,
  249. pwm_backlight_resume);
  250. #endif
  251. static struct platform_driver pwm_backlight_driver = {
  252. .driver = {
  253. .name = "pwm-backlight",
  254. .owner = THIS_MODULE,
  255. #ifdef CONFIG_PM
  256. .pm = &pwm_backlight_pm_ops,
  257. #endif
  258. .of_match_table = of_match_ptr(pwm_backlight_of_match),
  259. },
  260. .probe = pwm_backlight_probe,
  261. .remove = pwm_backlight_remove,
  262. };
  263. module_platform_driver(pwm_backlight_driver);
  264. MODULE_DESCRIPTION("PWM based Backlight Driver");
  265. MODULE_LICENSE("GPL");
  266. MODULE_ALIAS("platform:pwm-backlight");