pwm_bl.c 7.9 KB

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