pwm_bl.c 7.6 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 = dev_get_drvdata(&bl->dev);
  38. int brightness = bl->props.brightness;
  39. int max = bl->props.max_brightness;
  40. if (bl->props.power != FB_BLANK_UNBLANK)
  41. brightness = 0;
  42. if (bl->props.fb_blank != FB_BLANK_UNBLANK)
  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 = dev_get_drvdata(&bl->dev);
  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. if (value >= data->max_brightness) {
  114. dev_warn(dev, "invalid default brightness level: %u, using %u\n",
  115. value, data->max_brightness - 1);
  116. value = data->max_brightness - 1;
  117. }
  118. data->dft_brightness = value;
  119. data->max_brightness--;
  120. }
  121. /*
  122. * TODO: Most users of this driver use a number of GPIOs to control
  123. * backlight power. Support for specifying these needs to be
  124. * added.
  125. */
  126. return 0;
  127. }
  128. static struct of_device_id pwm_backlight_of_match[] = {
  129. { .compatible = "pwm-backlight" },
  130. { }
  131. };
  132. MODULE_DEVICE_TABLE(of, pwm_backlight_of_match);
  133. #else
  134. static int pwm_backlight_parse_dt(struct device *dev,
  135. struct platform_pwm_backlight_data *data)
  136. {
  137. return -ENODEV;
  138. }
  139. #endif
  140. static int pwm_backlight_probe(struct platform_device *pdev)
  141. {
  142. struct platform_pwm_backlight_data *data = pdev->dev.platform_data;
  143. struct platform_pwm_backlight_data defdata;
  144. struct backlight_properties props;
  145. struct backlight_device *bl;
  146. struct pwm_bl_data *pb;
  147. unsigned int max;
  148. int ret;
  149. if (!data) {
  150. ret = pwm_backlight_parse_dt(&pdev->dev, &defdata);
  151. if (ret < 0) {
  152. dev_err(&pdev->dev, "failed to find platform data\n");
  153. return ret;
  154. }
  155. data = &defdata;
  156. }
  157. if (data->init) {
  158. ret = data->init(&pdev->dev);
  159. if (ret < 0)
  160. return ret;
  161. }
  162. pb = devm_kzalloc(&pdev->dev, sizeof(*pb), GFP_KERNEL);
  163. if (!pb) {
  164. dev_err(&pdev->dev, "no memory for state\n");
  165. ret = -ENOMEM;
  166. goto err_alloc;
  167. }
  168. if (data->levels) {
  169. max = data->levels[data->max_brightness];
  170. pb->levels = data->levels;
  171. } else
  172. max = data->max_brightness;
  173. pb->notify = data->notify;
  174. pb->notify_after = data->notify_after;
  175. pb->check_fb = data->check_fb;
  176. pb->exit = data->exit;
  177. pb->dev = &pdev->dev;
  178. pb->pwm = devm_pwm_get(&pdev->dev, NULL);
  179. if (IS_ERR(pb->pwm)) {
  180. dev_err(&pdev->dev, "unable to request PWM, trying legacy API\n");
  181. pb->pwm = pwm_request(data->pwm_id, "pwm-backlight");
  182. if (IS_ERR(pb->pwm)) {
  183. dev_err(&pdev->dev, "unable to request legacy PWM\n");
  184. ret = PTR_ERR(pb->pwm);
  185. goto err_alloc;
  186. }
  187. }
  188. dev_dbg(&pdev->dev, "got pwm for backlight\n");
  189. /*
  190. * The DT case will set the pwm_period_ns field to 0 and store the
  191. * period, parsed from the DT, in the PWM device. For the non-DT case,
  192. * set the period from platform data.
  193. */
  194. if (data->pwm_period_ns > 0)
  195. pwm_set_period(pb->pwm, data->pwm_period_ns);
  196. pb->period = pwm_get_period(pb->pwm);
  197. pb->lth_brightness = data->lth_brightness * (pb->period / max);
  198. memset(&props, 0, sizeof(struct backlight_properties));
  199. props.type = BACKLIGHT_RAW;
  200. props.max_brightness = data->max_brightness;
  201. bl = backlight_device_register(dev_name(&pdev->dev), &pdev->dev, pb,
  202. &pwm_backlight_ops, &props);
  203. if (IS_ERR(bl)) {
  204. dev_err(&pdev->dev, "failed to register backlight\n");
  205. ret = PTR_ERR(bl);
  206. goto err_alloc;
  207. }
  208. bl->props.brightness = data->dft_brightness;
  209. backlight_update_status(bl);
  210. platform_set_drvdata(pdev, bl);
  211. return 0;
  212. err_alloc:
  213. if (data->exit)
  214. data->exit(&pdev->dev);
  215. return ret;
  216. }
  217. static int pwm_backlight_remove(struct platform_device *pdev)
  218. {
  219. struct backlight_device *bl = platform_get_drvdata(pdev);
  220. struct pwm_bl_data *pb = dev_get_drvdata(&bl->dev);
  221. backlight_device_unregister(bl);
  222. pwm_config(pb->pwm, 0, pb->period);
  223. pwm_disable(pb->pwm);
  224. if (pb->exit)
  225. pb->exit(&pdev->dev);
  226. return 0;
  227. }
  228. #ifdef CONFIG_PM
  229. static int pwm_backlight_suspend(struct device *dev)
  230. {
  231. struct backlight_device *bl = dev_get_drvdata(dev);
  232. struct pwm_bl_data *pb = dev_get_drvdata(&bl->dev);
  233. if (pb->notify)
  234. pb->notify(pb->dev, 0);
  235. pwm_config(pb->pwm, 0, pb->period);
  236. pwm_disable(pb->pwm);
  237. if (pb->notify_after)
  238. pb->notify_after(pb->dev, 0);
  239. return 0;
  240. }
  241. static int pwm_backlight_resume(struct device *dev)
  242. {
  243. struct backlight_device *bl = dev_get_drvdata(dev);
  244. backlight_update_status(bl);
  245. return 0;
  246. }
  247. static SIMPLE_DEV_PM_OPS(pwm_backlight_pm_ops, pwm_backlight_suspend,
  248. pwm_backlight_resume);
  249. #endif
  250. static struct platform_driver pwm_backlight_driver = {
  251. .driver = {
  252. .name = "pwm-backlight",
  253. .owner = THIS_MODULE,
  254. #ifdef CONFIG_PM
  255. .pm = &pwm_backlight_pm_ops,
  256. #endif
  257. .of_match_table = of_match_ptr(pwm_backlight_of_match),
  258. },
  259. .probe = pwm_backlight_probe,
  260. .remove = pwm_backlight_remove,
  261. };
  262. module_platform_driver(pwm_backlight_driver);
  263. MODULE_DESCRIPTION("PWM based Backlight Driver");
  264. MODULE_LICENSE("GPL");
  265. MODULE_ALIAS("platform:pwm-backlight");