pwm_bl.c 9.8 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.h>
  13. #include <linux/of_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. int enable_gpio;
  34. unsigned long enable_gpio_flags;
  35. unsigned int scale;
  36. int (*notify)(struct device *,
  37. int brightness);
  38. void (*notify_after)(struct device *,
  39. int brightness);
  40. int (*check_fb)(struct device *, struct fb_info *);
  41. void (*exit)(struct device *);
  42. };
  43. static void pwm_backlight_power_on(struct pwm_bl_data *pb, int brightness)
  44. {
  45. int duty_cycle, err;
  46. if (pb->enabled)
  47. return;
  48. err = regulator_enable(pb->power_supply);
  49. if (err < 0)
  50. dev_err(pb->dev, "failed to enable power supply\n");
  51. if (gpio_is_valid(pb->enable_gpio)) {
  52. if (pb->enable_gpio_flags & PWM_BACKLIGHT_GPIO_ACTIVE_LOW)
  53. gpio_set_value(pb->enable_gpio, 0);
  54. else
  55. gpio_set_value(pb->enable_gpio, 1);
  56. }
  57. pwm_enable(pb->pwm);
  58. pb->enabled = true;
  59. }
  60. static void pwm_backlight_power_off(struct pwm_bl_data *pb)
  61. {
  62. if (!pb->enabled)
  63. return;
  64. pwm_config(pb->pwm, 0, pb->period);
  65. pwm_disable(pb->pwm);
  66. if (gpio_is_valid(pb->enable_gpio)) {
  67. if (pb->enable_gpio_flags & PWM_BACKLIGHT_GPIO_ACTIVE_LOW)
  68. gpio_set_value(pb->enable_gpio, 1);
  69. else
  70. gpio_set_value(pb->enable_gpio, 0);
  71. }
  72. regulator_disable(pb->power_supply);
  73. pb->enabled = false;
  74. }
  75. static int compute_duty_cycle(struct pwm_bl_data *pb, int brightness)
  76. {
  77. unsigned int lth = pb->lth_brightness;
  78. int duty_cycle;
  79. if (pb->levels)
  80. duty_cycle = pb->levels[brightness];
  81. else
  82. duty_cycle = brightness;
  83. return (duty_cycle * (pb->period - lth) / pb->scale) + lth;
  84. }
  85. static int pwm_backlight_update_status(struct backlight_device *bl)
  86. {
  87. struct pwm_bl_data *pb = bl_get_data(bl);
  88. int brightness = bl->props.brightness;
  89. int duty_cycle;
  90. if (bl->props.power != FB_BLANK_UNBLANK ||
  91. bl->props.fb_blank != FB_BLANK_UNBLANK ||
  92. bl->props.state & BL_CORE_FBBLANK)
  93. brightness = 0;
  94. if (pb->notify)
  95. brightness = pb->notify(pb->dev, brightness);
  96. if (brightness > 0) {
  97. duty_cycle = compute_duty_cycle(pb, brightness);
  98. pwm_config(pb->pwm, duty_cycle, pb->period);
  99. pwm_backlight_power_on(pb, brightness);
  100. } else
  101. pwm_backlight_power_off(pb);
  102. if (pb->notify_after)
  103. pb->notify_after(pb->dev, brightness);
  104. return 0;
  105. }
  106. static int pwm_backlight_get_brightness(struct backlight_device *bl)
  107. {
  108. return bl->props.brightness;
  109. }
  110. static int pwm_backlight_check_fb(struct backlight_device *bl,
  111. struct fb_info *info)
  112. {
  113. struct pwm_bl_data *pb = bl_get_data(bl);
  114. return !pb->check_fb || pb->check_fb(pb->dev, info);
  115. }
  116. static const struct backlight_ops pwm_backlight_ops = {
  117. .update_status = pwm_backlight_update_status,
  118. .get_brightness = pwm_backlight_get_brightness,
  119. .check_fb = pwm_backlight_check_fb,
  120. };
  121. #ifdef CONFIG_OF
  122. static int pwm_backlight_parse_dt(struct device *dev,
  123. struct platform_pwm_backlight_data *data)
  124. {
  125. struct device_node *node = dev->of_node;
  126. enum of_gpio_flags flags;
  127. struct property *prop;
  128. int length;
  129. u32 value;
  130. int ret;
  131. if (!node)
  132. return -ENODEV;
  133. memset(data, 0, sizeof(*data));
  134. /* determine the number of brightness levels */
  135. prop = of_find_property(node, "brightness-levels", &length);
  136. if (!prop)
  137. return -EINVAL;
  138. data->max_brightness = length / sizeof(u32);
  139. /* read brightness levels from DT property */
  140. if (data->max_brightness > 0) {
  141. size_t size = sizeof(*data->levels) * data->max_brightness;
  142. data->levels = devm_kzalloc(dev, size, GFP_KERNEL);
  143. if (!data->levels)
  144. return -ENOMEM;
  145. ret = of_property_read_u32_array(node, "brightness-levels",
  146. data->levels,
  147. data->max_brightness);
  148. if (ret < 0)
  149. return ret;
  150. ret = of_property_read_u32(node, "default-brightness-level",
  151. &value);
  152. if (ret < 0)
  153. return ret;
  154. data->dft_brightness = value;
  155. data->max_brightness--;
  156. }
  157. data->enable_gpio = of_get_named_gpio_flags(node, "enable-gpios", 0,
  158. &flags);
  159. if (data->enable_gpio == -EPROBE_DEFER)
  160. return -EPROBE_DEFER;
  161. if (gpio_is_valid(data->enable_gpio) && (flags & OF_GPIO_ACTIVE_LOW))
  162. data->enable_gpio_flags |= PWM_BACKLIGHT_GPIO_ACTIVE_LOW;
  163. return 0;
  164. }
  165. static struct of_device_id pwm_backlight_of_match[] = {
  166. { .compatible = "pwm-backlight" },
  167. { }
  168. };
  169. MODULE_DEVICE_TABLE(of, pwm_backlight_of_match);
  170. #else
  171. static int pwm_backlight_parse_dt(struct device *dev,
  172. struct platform_pwm_backlight_data *data)
  173. {
  174. return -ENODEV;
  175. }
  176. #endif
  177. static int pwm_backlight_probe(struct platform_device *pdev)
  178. {
  179. struct platform_pwm_backlight_data *data = pdev->dev.platform_data;
  180. struct platform_pwm_backlight_data defdata;
  181. struct backlight_properties props;
  182. struct backlight_device *bl;
  183. struct pwm_bl_data *pb;
  184. int ret;
  185. if (!data) {
  186. ret = pwm_backlight_parse_dt(&pdev->dev, &defdata);
  187. if (ret < 0) {
  188. dev_err(&pdev->dev, "failed to find platform data\n");
  189. return ret;
  190. }
  191. data = &defdata;
  192. }
  193. if (data->init) {
  194. ret = data->init(&pdev->dev);
  195. if (ret < 0)
  196. return ret;
  197. }
  198. pb = devm_kzalloc(&pdev->dev, sizeof(*pb), GFP_KERNEL);
  199. if (!pb) {
  200. dev_err(&pdev->dev, "no memory for state\n");
  201. ret = -ENOMEM;
  202. goto err_alloc;
  203. }
  204. if (data->levels) {
  205. unsigned int i;
  206. for (i = 0; i <= data->max_brightness; i++)
  207. if (data->levels[i] > pb->scale)
  208. pb->scale = data->levels[i];
  209. pb->levels = data->levels;
  210. } else
  211. pb->scale = data->max_brightness;
  212. pb->enable_gpio = data->enable_gpio;
  213. pb->enable_gpio_flags = data->enable_gpio_flags;
  214. pb->notify = data->notify;
  215. pb->notify_after = data->notify_after;
  216. pb->check_fb = data->check_fb;
  217. pb->exit = data->exit;
  218. pb->dev = &pdev->dev;
  219. pb->enabled = false;
  220. if (gpio_is_valid(pb->enable_gpio)) {
  221. unsigned long flags;
  222. if (pb->enable_gpio_flags & PWM_BACKLIGHT_GPIO_ACTIVE_LOW)
  223. flags = GPIOF_OUT_INIT_HIGH;
  224. else
  225. flags = GPIOF_OUT_INIT_LOW;
  226. ret = gpio_request_one(pb->enable_gpio, flags, "enable");
  227. if (ret < 0) {
  228. dev_err(&pdev->dev, "failed to request GPIO#%d: %d\n",
  229. pb->enable_gpio, ret);
  230. goto err_alloc;
  231. }
  232. }
  233. pb->power_supply = devm_regulator_get(&pdev->dev, "power");
  234. if (IS_ERR(pb->power_supply)) {
  235. ret = PTR_ERR(pb->power_supply);
  236. goto err_gpio;
  237. }
  238. pb->pwm = devm_pwm_get(&pdev->dev, NULL);
  239. if (IS_ERR(pb->pwm)) {
  240. dev_err(&pdev->dev, "unable to request PWM, trying legacy API\n");
  241. pb->pwm = pwm_request(data->pwm_id, "pwm-backlight");
  242. if (IS_ERR(pb->pwm)) {
  243. dev_err(&pdev->dev, "unable to request legacy PWM\n");
  244. ret = PTR_ERR(pb->pwm);
  245. goto err_gpio;
  246. }
  247. }
  248. dev_dbg(&pdev->dev, "got pwm for backlight\n");
  249. /*
  250. * The DT case will set the pwm_period_ns field to 0 and store the
  251. * period, parsed from the DT, in the PWM device. For the non-DT case,
  252. * set the period from platform data.
  253. */
  254. if (data->pwm_period_ns > 0)
  255. pwm_set_period(pb->pwm, data->pwm_period_ns);
  256. pb->period = pwm_get_period(pb->pwm);
  257. pb->lth_brightness = data->lth_brightness * (pb->period / pb->scale);
  258. memset(&props, 0, sizeof(struct backlight_properties));
  259. props.type = BACKLIGHT_RAW;
  260. props.max_brightness = data->max_brightness;
  261. bl = backlight_device_register(dev_name(&pdev->dev), &pdev->dev, pb,
  262. &pwm_backlight_ops, &props);
  263. if (IS_ERR(bl)) {
  264. dev_err(&pdev->dev, "failed to register backlight\n");
  265. ret = PTR_ERR(bl);
  266. goto err_gpio;
  267. }
  268. if (data->dft_brightness > data->max_brightness) {
  269. dev_warn(&pdev->dev,
  270. "invalid default brightness level: %u, using %u\n",
  271. data->dft_brightness, data->max_brightness);
  272. data->dft_brightness = data->max_brightness;
  273. }
  274. bl->props.brightness = data->dft_brightness;
  275. backlight_update_status(bl);
  276. platform_set_drvdata(pdev, bl);
  277. return 0;
  278. err_gpio:
  279. if (gpio_is_valid(pb->enable_gpio))
  280. gpio_free(pb->enable_gpio);
  281. err_alloc:
  282. if (data->exit)
  283. data->exit(&pdev->dev);
  284. return ret;
  285. }
  286. static int pwm_backlight_remove(struct platform_device *pdev)
  287. {
  288. struct backlight_device *bl = platform_get_drvdata(pdev);
  289. struct pwm_bl_data *pb = bl_get_data(bl);
  290. backlight_device_unregister(bl);
  291. pwm_backlight_power_off(pb);
  292. if (pb->exit)
  293. pb->exit(&pdev->dev);
  294. return 0;
  295. }
  296. #ifdef CONFIG_PM_SLEEP
  297. static int pwm_backlight_suspend(struct device *dev)
  298. {
  299. struct backlight_device *bl = dev_get_drvdata(dev);
  300. struct pwm_bl_data *pb = bl_get_data(bl);
  301. if (pb->notify)
  302. pb->notify(pb->dev, 0);
  303. pwm_backlight_power_off(pb);
  304. if (pb->notify_after)
  305. pb->notify_after(pb->dev, 0);
  306. return 0;
  307. }
  308. static int pwm_backlight_resume(struct device *dev)
  309. {
  310. struct backlight_device *bl = dev_get_drvdata(dev);
  311. backlight_update_status(bl);
  312. return 0;
  313. }
  314. #endif
  315. static const struct dev_pm_ops pwm_backlight_pm_ops = {
  316. #ifdef CONFIG_PM_SLEEP
  317. .suspend = pwm_backlight_suspend,
  318. .resume = pwm_backlight_resume,
  319. .poweroff = pwm_backlight_suspend,
  320. .restore = pwm_backlight_resume,
  321. #endif
  322. };
  323. static struct platform_driver pwm_backlight_driver = {
  324. .driver = {
  325. .name = "pwm-backlight",
  326. .owner = THIS_MODULE,
  327. .pm = &pwm_backlight_pm_ops,
  328. .of_match_table = of_match_ptr(pwm_backlight_of_match),
  329. },
  330. .probe = pwm_backlight_probe,
  331. .remove = pwm_backlight_remove,
  332. };
  333. module_platform_driver(pwm_backlight_driver);
  334. MODULE_DESCRIPTION("PWM based Backlight Driver");
  335. MODULE_LICENSE("GPL");
  336. MODULE_ALIAS("platform:pwm-backlight");