pwm_bl.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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/consumer.h>
  13. #include <linux/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. struct gpio_desc *enable_gpio;
  34. unsigned int scale;
  35. bool legacy;
  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 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 (pb->enable_gpio)
  52. gpiod_set_value_cansleep(pb->enable_gpio, 1);
  53. pwm_enable(pb->pwm);
  54. pb->enabled = true;
  55. }
  56. static void pwm_backlight_power_off(struct pwm_bl_data *pb)
  57. {
  58. if (!pb->enabled)
  59. return;
  60. pwm_config(pb->pwm, 0, pb->period);
  61. pwm_disable(pb->pwm);
  62. if (pb->enable_gpio)
  63. gpiod_set_value_cansleep(pb->enable_gpio, 0);
  64. regulator_disable(pb->power_supply);
  65. pb->enabled = false;
  66. }
  67. static int compute_duty_cycle(struct pwm_bl_data *pb, int brightness)
  68. {
  69. unsigned int lth = pb->lth_brightness;
  70. u64 duty_cycle;
  71. if (pb->levels)
  72. duty_cycle = pb->levels[brightness];
  73. else
  74. duty_cycle = brightness;
  75. duty_cycle *= pb->period - lth;
  76. do_div(duty_cycle, pb->scale);
  77. return duty_cycle + lth;
  78. }
  79. static int pwm_backlight_update_status(struct backlight_device *bl)
  80. {
  81. struct pwm_bl_data *pb = bl_get_data(bl);
  82. int brightness = bl->props.brightness;
  83. int duty_cycle;
  84. if (bl->props.power != FB_BLANK_UNBLANK ||
  85. bl->props.fb_blank != FB_BLANK_UNBLANK ||
  86. bl->props.state & BL_CORE_FBBLANK)
  87. brightness = 0;
  88. if (pb->notify)
  89. brightness = pb->notify(pb->dev, brightness);
  90. if (brightness > 0) {
  91. duty_cycle = compute_duty_cycle(pb, brightness);
  92. pwm_config(pb->pwm, duty_cycle, pb->period);
  93. pwm_backlight_power_on(pb, brightness);
  94. } else
  95. pwm_backlight_power_off(pb);
  96. if (pb->notify_after)
  97. pb->notify_after(pb->dev, brightness);
  98. return 0;
  99. }
  100. static int pwm_backlight_check_fb(struct backlight_device *bl,
  101. struct fb_info *info)
  102. {
  103. struct pwm_bl_data *pb = bl_get_data(bl);
  104. return !pb->check_fb || pb->check_fb(pb->dev, info);
  105. }
  106. static const struct backlight_ops pwm_backlight_ops = {
  107. .update_status = pwm_backlight_update_status,
  108. .check_fb = pwm_backlight_check_fb,
  109. };
  110. #ifdef CONFIG_OF
  111. static int pwm_backlight_parse_dt(struct device *dev,
  112. struct platform_pwm_backlight_data *data)
  113. {
  114. struct device_node *node = dev->of_node;
  115. struct property *prop;
  116. int length;
  117. u32 value;
  118. int ret;
  119. if (!node)
  120. return -ENODEV;
  121. memset(data, 0, sizeof(*data));
  122. /* determine the number of brightness levels */
  123. prop = of_find_property(node, "brightness-levels", &length);
  124. if (!prop)
  125. return -EINVAL;
  126. data->max_brightness = length / sizeof(u32);
  127. /* read brightness levels from DT property */
  128. if (data->max_brightness > 0) {
  129. size_t size = sizeof(*data->levels) * data->max_brightness;
  130. data->levels = devm_kzalloc(dev, size, GFP_KERNEL);
  131. if (!data->levels)
  132. return -ENOMEM;
  133. ret = of_property_read_u32_array(node, "brightness-levels",
  134. data->levels,
  135. data->max_brightness);
  136. if (ret < 0)
  137. return ret;
  138. ret = of_property_read_u32(node, "default-brightness-level",
  139. &value);
  140. if (ret < 0)
  141. return ret;
  142. data->dft_brightness = value;
  143. data->max_brightness--;
  144. }
  145. data->enable_gpio = -EINVAL;
  146. return 0;
  147. }
  148. static struct of_device_id pwm_backlight_of_match[] = {
  149. { .compatible = "pwm-backlight" },
  150. { }
  151. };
  152. MODULE_DEVICE_TABLE(of, pwm_backlight_of_match);
  153. #else
  154. static int pwm_backlight_parse_dt(struct device *dev,
  155. struct platform_pwm_backlight_data *data)
  156. {
  157. return -ENODEV;
  158. }
  159. #endif
  160. static int pwm_backlight_probe(struct platform_device *pdev)
  161. {
  162. struct platform_pwm_backlight_data *data = dev_get_platdata(&pdev->dev);
  163. struct platform_pwm_backlight_data defdata;
  164. struct backlight_properties props;
  165. struct backlight_device *bl;
  166. struct device_node *node = pdev->dev.of_node;
  167. struct pwm_bl_data *pb;
  168. int initial_blank = FB_BLANK_UNBLANK;
  169. struct pwm_args pargs;
  170. int ret;
  171. if (!data) {
  172. ret = pwm_backlight_parse_dt(&pdev->dev, &defdata);
  173. if (ret < 0) {
  174. dev_err(&pdev->dev, "failed to find platform data\n");
  175. return ret;
  176. }
  177. data = &defdata;
  178. }
  179. if (data->init) {
  180. ret = data->init(&pdev->dev);
  181. if (ret < 0)
  182. return ret;
  183. }
  184. pb = devm_kzalloc(&pdev->dev, sizeof(*pb), GFP_KERNEL);
  185. if (!pb) {
  186. ret = -ENOMEM;
  187. goto err_alloc;
  188. }
  189. if (data->levels) {
  190. unsigned int i;
  191. for (i = 0; i <= data->max_brightness; i++)
  192. if (data->levels[i] > pb->scale)
  193. pb->scale = data->levels[i];
  194. pb->levels = data->levels;
  195. } else
  196. pb->scale = data->max_brightness;
  197. pb->notify = data->notify;
  198. pb->notify_after = data->notify_after;
  199. pb->check_fb = data->check_fb;
  200. pb->exit = data->exit;
  201. pb->dev = &pdev->dev;
  202. pb->enabled = false;
  203. pb->enable_gpio = devm_gpiod_get_optional(&pdev->dev, "enable",
  204. GPIOD_ASIS);
  205. if (IS_ERR(pb->enable_gpio)) {
  206. ret = PTR_ERR(pb->enable_gpio);
  207. goto err_alloc;
  208. }
  209. /*
  210. * Compatibility fallback for drivers still using the integer GPIO
  211. * platform data. Must go away soon.
  212. */
  213. if (!pb->enable_gpio && gpio_is_valid(data->enable_gpio)) {
  214. ret = devm_gpio_request_one(&pdev->dev, data->enable_gpio,
  215. GPIOF_OUT_INIT_HIGH, "enable");
  216. if (ret < 0) {
  217. dev_err(&pdev->dev, "failed to request GPIO#%d: %d\n",
  218. data->enable_gpio, ret);
  219. goto err_alloc;
  220. }
  221. pb->enable_gpio = gpio_to_desc(data->enable_gpio);
  222. }
  223. if (pb->enable_gpio) {
  224. /*
  225. * If the driver is probed from the device tree and there is a
  226. * phandle link pointing to the backlight node, it is safe to
  227. * assume that another driver will enable the backlight at the
  228. * appropriate time. Therefore, if it is disabled, keep it so.
  229. */
  230. if (node && node->phandle &&
  231. gpiod_get_direction(pb->enable_gpio) == GPIOF_DIR_OUT &&
  232. gpiod_get_value(pb->enable_gpio) == 0)
  233. initial_blank = FB_BLANK_POWERDOWN;
  234. else
  235. gpiod_direction_output(pb->enable_gpio, 1);
  236. }
  237. pb->power_supply = devm_regulator_get(&pdev->dev, "power");
  238. if (IS_ERR(pb->power_supply)) {
  239. ret = PTR_ERR(pb->power_supply);
  240. goto err_alloc;
  241. }
  242. if (node && node->phandle && !regulator_is_enabled(pb->power_supply))
  243. initial_blank = FB_BLANK_POWERDOWN;
  244. pb->pwm = devm_pwm_get(&pdev->dev, NULL);
  245. if (IS_ERR(pb->pwm) && PTR_ERR(pb->pwm) != -EPROBE_DEFER && !node) {
  246. dev_err(&pdev->dev, "unable to request PWM, trying legacy API\n");
  247. pb->legacy = true;
  248. pb->pwm = pwm_request(data->pwm_id, "pwm-backlight");
  249. }
  250. if (IS_ERR(pb->pwm)) {
  251. ret = PTR_ERR(pb->pwm);
  252. if (ret != -EPROBE_DEFER)
  253. dev_err(&pdev->dev, "unable to request PWM\n");
  254. goto err_alloc;
  255. }
  256. dev_dbg(&pdev->dev, "got pwm for backlight\n");
  257. /*
  258. * FIXME: pwm_apply_args() should be removed when switching to
  259. * the atomic PWM API.
  260. */
  261. pwm_apply_args(pb->pwm);
  262. /*
  263. * The DT case will set the pwm_period_ns field to 0 and store the
  264. * period, parsed from the DT, in the PWM device. For the non-DT case,
  265. * set the period from platform data if it has not already been set
  266. * via the PWM lookup table.
  267. */
  268. pwm_get_args(pb->pwm, &pargs);
  269. pb->period = pargs.period;
  270. if (!pb->period && (data->pwm_period_ns > 0))
  271. pb->period = data->pwm_period_ns;
  272. pb->lth_brightness = data->lth_brightness * (pb->period / pb->scale);
  273. memset(&props, 0, sizeof(struct backlight_properties));
  274. props.type = BACKLIGHT_RAW;
  275. props.max_brightness = data->max_brightness;
  276. bl = backlight_device_register(dev_name(&pdev->dev), &pdev->dev, pb,
  277. &pwm_backlight_ops, &props);
  278. if (IS_ERR(bl)) {
  279. dev_err(&pdev->dev, "failed to register backlight\n");
  280. ret = PTR_ERR(bl);
  281. if (pb->legacy)
  282. pwm_free(pb->pwm);
  283. goto err_alloc;
  284. }
  285. if (data->dft_brightness > data->max_brightness) {
  286. dev_warn(&pdev->dev,
  287. "invalid default brightness level: %u, using %u\n",
  288. data->dft_brightness, data->max_brightness);
  289. data->dft_brightness = data->max_brightness;
  290. }
  291. bl->props.brightness = data->dft_brightness;
  292. bl->props.power = initial_blank;
  293. backlight_update_status(bl);
  294. platform_set_drvdata(pdev, bl);
  295. return 0;
  296. err_alloc:
  297. if (data->exit)
  298. data->exit(&pdev->dev);
  299. return ret;
  300. }
  301. static int pwm_backlight_remove(struct platform_device *pdev)
  302. {
  303. struct backlight_device *bl = platform_get_drvdata(pdev);
  304. struct pwm_bl_data *pb = bl_get_data(bl);
  305. backlight_device_unregister(bl);
  306. pwm_backlight_power_off(pb);
  307. if (pb->exit)
  308. pb->exit(&pdev->dev);
  309. if (pb->legacy)
  310. pwm_free(pb->pwm);
  311. return 0;
  312. }
  313. static void pwm_backlight_shutdown(struct platform_device *pdev)
  314. {
  315. struct backlight_device *bl = platform_get_drvdata(pdev);
  316. struct pwm_bl_data *pb = bl_get_data(bl);
  317. pwm_backlight_power_off(pb);
  318. }
  319. #ifdef CONFIG_PM_SLEEP
  320. static int pwm_backlight_suspend(struct device *dev)
  321. {
  322. struct backlight_device *bl = dev_get_drvdata(dev);
  323. struct pwm_bl_data *pb = bl_get_data(bl);
  324. if (pb->notify)
  325. pb->notify(pb->dev, 0);
  326. pwm_backlight_power_off(pb);
  327. if (pb->notify_after)
  328. pb->notify_after(pb->dev, 0);
  329. return 0;
  330. }
  331. static int pwm_backlight_resume(struct device *dev)
  332. {
  333. struct backlight_device *bl = dev_get_drvdata(dev);
  334. backlight_update_status(bl);
  335. return 0;
  336. }
  337. #endif
  338. static const struct dev_pm_ops pwm_backlight_pm_ops = {
  339. #ifdef CONFIG_PM_SLEEP
  340. .suspend = pwm_backlight_suspend,
  341. .resume = pwm_backlight_resume,
  342. .poweroff = pwm_backlight_suspend,
  343. .restore = pwm_backlight_resume,
  344. #endif
  345. };
  346. static struct platform_driver pwm_backlight_driver = {
  347. .driver = {
  348. .name = "pwm-backlight",
  349. .pm = &pwm_backlight_pm_ops,
  350. .of_match_table = of_match_ptr(pwm_backlight_of_match),
  351. },
  352. .probe = pwm_backlight_probe,
  353. .remove = pwm_backlight_remove,
  354. .shutdown = pwm_backlight_shutdown,
  355. };
  356. module_platform_driver(pwm_backlight_driver);
  357. MODULE_DESCRIPTION("PWM based Backlight Driver");
  358. MODULE_LICENSE("GPL");
  359. MODULE_ALIAS("platform:pwm-backlight");