pwm_bl.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. if (pb->levels) {
  51. brightness = pb->levels[brightness];
  52. max = pb->levels[max];
  53. }
  54. brightness = pb->lth_brightness +
  55. (brightness * (pb->period - pb->lth_brightness) / max);
  56. pwm_config(pb->pwm, brightness, pb->period);
  57. pwm_enable(pb->pwm);
  58. }
  59. if (pb->notify_after)
  60. pb->notify_after(pb->dev, brightness);
  61. return 0;
  62. }
  63. static int pwm_backlight_get_brightness(struct backlight_device *bl)
  64. {
  65. return bl->props.brightness;
  66. }
  67. static int pwm_backlight_check_fb(struct backlight_device *bl,
  68. struct fb_info *info)
  69. {
  70. struct pwm_bl_data *pb = dev_get_drvdata(&bl->dev);
  71. return !pb->check_fb || pb->check_fb(pb->dev, info);
  72. }
  73. static const struct backlight_ops pwm_backlight_ops = {
  74. .update_status = pwm_backlight_update_status,
  75. .get_brightness = pwm_backlight_get_brightness,
  76. .check_fb = pwm_backlight_check_fb,
  77. };
  78. #ifdef CONFIG_OF
  79. static int pwm_backlight_parse_dt(struct device *dev,
  80. struct platform_pwm_backlight_data *data)
  81. {
  82. struct device_node *node = dev->of_node;
  83. struct property *prop;
  84. int length;
  85. u32 value;
  86. int ret;
  87. if (!node)
  88. return -ENODEV;
  89. memset(data, 0, sizeof(*data));
  90. /* determine the number of brightness levels */
  91. prop = of_find_property(node, "brightness-levels", &length);
  92. if (!prop)
  93. return -EINVAL;
  94. data->max_brightness = length / sizeof(u32);
  95. /* read brightness levels from DT property */
  96. if (data->max_brightness > 0) {
  97. size_t size = sizeof(*data->levels) * data->max_brightness;
  98. data->levels = devm_kzalloc(dev, size, GFP_KERNEL);
  99. if (!data->levels)
  100. return -ENOMEM;
  101. ret = of_property_read_u32_array(node, "brightness-levels",
  102. data->levels,
  103. data->max_brightness);
  104. if (ret < 0)
  105. return ret;
  106. ret = of_property_read_u32(node, "default-brightness-level",
  107. &value);
  108. if (ret < 0)
  109. return ret;
  110. if (value >= data->max_brightness) {
  111. dev_warn(dev, "invalid default brightness level: %u, using %u\n",
  112. value, data->max_brightness - 1);
  113. value = data->max_brightness - 1;
  114. }
  115. data->dft_brightness = value;
  116. data->max_brightness--;
  117. }
  118. /*
  119. * TODO: Most users of this driver use a number of GPIOs to control
  120. * backlight power. Support for specifying these needs to be
  121. * added.
  122. */
  123. return 0;
  124. }
  125. static struct of_device_id pwm_backlight_of_match[] = {
  126. { .compatible = "pwm-backlight" },
  127. { }
  128. };
  129. MODULE_DEVICE_TABLE(of, pwm_backlight_of_match);
  130. #else
  131. static int pwm_backlight_parse_dt(struct device *dev,
  132. struct platform_pwm_backlight_data *data)
  133. {
  134. return -ENODEV;
  135. }
  136. #endif
  137. static int pwm_backlight_probe(struct platform_device *pdev)
  138. {
  139. struct platform_pwm_backlight_data *data = pdev->dev.platform_data;
  140. struct platform_pwm_backlight_data defdata;
  141. struct backlight_properties props;
  142. struct backlight_device *bl;
  143. struct pwm_bl_data *pb;
  144. unsigned int max;
  145. int ret;
  146. if (!data) {
  147. ret = pwm_backlight_parse_dt(&pdev->dev, &defdata);
  148. if (ret < 0) {
  149. dev_err(&pdev->dev, "failed to find platform data\n");
  150. return ret;
  151. }
  152. data = &defdata;
  153. }
  154. if (data->init) {
  155. ret = data->init(&pdev->dev);
  156. if (ret < 0)
  157. return ret;
  158. }
  159. pb = devm_kzalloc(&pdev->dev, sizeof(*pb), GFP_KERNEL);
  160. if (!pb) {
  161. dev_err(&pdev->dev, "no memory for state\n");
  162. ret = -ENOMEM;
  163. goto err_alloc;
  164. }
  165. if (data->levels) {
  166. max = data->levels[data->max_brightness];
  167. pb->levels = data->levels;
  168. } else
  169. max = data->max_brightness;
  170. pb->notify = data->notify;
  171. pb->notify_after = data->notify_after;
  172. pb->check_fb = data->check_fb;
  173. pb->exit = data->exit;
  174. pb->dev = &pdev->dev;
  175. pb->pwm = pwm_get(&pdev->dev, NULL);
  176. if (IS_ERR(pb->pwm)) {
  177. dev_err(&pdev->dev, "unable to request PWM, trying legacy API\n");
  178. pb->pwm = pwm_request(data->pwm_id, "pwm-backlight");
  179. if (IS_ERR(pb->pwm)) {
  180. dev_err(&pdev->dev, "unable to request legacy PWM\n");
  181. ret = PTR_ERR(pb->pwm);
  182. goto err_alloc;
  183. }
  184. }
  185. dev_dbg(&pdev->dev, "got pwm for backlight\n");
  186. /*
  187. * The DT case will set the pwm_period_ns field to 0 and store the
  188. * period, parsed from the DT, in the PWM device. For the non-DT case,
  189. * set the period from platform data.
  190. */
  191. if (data->pwm_period_ns > 0)
  192. pwm_set_period(pb->pwm, data->pwm_period_ns);
  193. pb->period = pwm_get_period(pb->pwm);
  194. pb->lth_brightness = data->lth_brightness * (pb->period / max);
  195. memset(&props, 0, sizeof(struct backlight_properties));
  196. props.type = BACKLIGHT_RAW;
  197. props.max_brightness = data->max_brightness;
  198. bl = backlight_device_register(dev_name(&pdev->dev), &pdev->dev, pb,
  199. &pwm_backlight_ops, &props);
  200. if (IS_ERR(bl)) {
  201. dev_err(&pdev->dev, "failed to register backlight\n");
  202. ret = PTR_ERR(bl);
  203. goto err_bl;
  204. }
  205. bl->props.brightness = data->dft_brightness;
  206. backlight_update_status(bl);
  207. platform_set_drvdata(pdev, bl);
  208. return 0;
  209. err_bl:
  210. pwm_put(pb->pwm);
  211. err_alloc:
  212. if (data->exit)
  213. data->exit(&pdev->dev);
  214. return ret;
  215. }
  216. static int pwm_backlight_remove(struct platform_device *pdev)
  217. {
  218. struct backlight_device *bl = platform_get_drvdata(pdev);
  219. struct pwm_bl_data *pb = dev_get_drvdata(&bl->dev);
  220. backlight_device_unregister(bl);
  221. pwm_config(pb->pwm, 0, pb->period);
  222. pwm_disable(pb->pwm);
  223. pwm_put(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");