pwm_bl.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. int (*notify)(struct device *,
  28. int brightness);
  29. int (*check_fb)(struct device *, struct fb_info *);
  30. };
  31. static int pwm_backlight_update_status(struct backlight_device *bl)
  32. {
  33. struct pwm_bl_data *pb = dev_get_drvdata(&bl->dev);
  34. int brightness = bl->props.brightness;
  35. int max = bl->props.max_brightness;
  36. if (bl->props.power != FB_BLANK_UNBLANK)
  37. brightness = 0;
  38. if (bl->props.fb_blank != FB_BLANK_UNBLANK)
  39. brightness = 0;
  40. if (pb->notify)
  41. brightness = pb->notify(pb->dev, brightness);
  42. if (brightness == 0) {
  43. pwm_config(pb->pwm, 0, pb->period);
  44. pwm_disable(pb->pwm);
  45. } else {
  46. brightness = pb->lth_brightness +
  47. (brightness * (pb->period - pb->lth_brightness) / max);
  48. pwm_config(pb->pwm, brightness, pb->period);
  49. pwm_enable(pb->pwm);
  50. }
  51. return 0;
  52. }
  53. static int pwm_backlight_get_brightness(struct backlight_device *bl)
  54. {
  55. return bl->props.brightness;
  56. }
  57. static int pwm_backlight_check_fb(struct backlight_device *bl,
  58. struct fb_info *info)
  59. {
  60. struct pwm_bl_data *pb = dev_get_drvdata(&bl->dev);
  61. return !pb->check_fb || pb->check_fb(pb->dev, info);
  62. }
  63. static const struct backlight_ops pwm_backlight_ops = {
  64. .update_status = pwm_backlight_update_status,
  65. .get_brightness = pwm_backlight_get_brightness,
  66. .check_fb = pwm_backlight_check_fb,
  67. };
  68. static int pwm_backlight_probe(struct platform_device *pdev)
  69. {
  70. struct backlight_properties props;
  71. struct platform_pwm_backlight_data *data = pdev->dev.platform_data;
  72. struct backlight_device *bl;
  73. struct pwm_bl_data *pb;
  74. int ret;
  75. if (!data) {
  76. dev_err(&pdev->dev, "failed to find platform data\n");
  77. return -EINVAL;
  78. }
  79. if (data->init) {
  80. ret = data->init(&pdev->dev);
  81. if (ret < 0)
  82. return ret;
  83. }
  84. pb = kzalloc(sizeof(*pb), GFP_KERNEL);
  85. if (!pb) {
  86. dev_err(&pdev->dev, "no memory for state\n");
  87. ret = -ENOMEM;
  88. goto err_alloc;
  89. }
  90. pb->period = data->pwm_period_ns;
  91. pb->notify = data->notify;
  92. pb->check_fb = data->check_fb;
  93. pb->lth_brightness = data->lth_brightness *
  94. (data->pwm_period_ns / data->max_brightness);
  95. pb->dev = &pdev->dev;
  96. pb->pwm = pwm_request(data->pwm_id, "backlight");
  97. if (IS_ERR(pb->pwm)) {
  98. dev_err(&pdev->dev, "unable to request PWM for backlight\n");
  99. ret = PTR_ERR(pb->pwm);
  100. goto err_pwm;
  101. } else
  102. dev_dbg(&pdev->dev, "got pwm for backlight\n");
  103. memset(&props, 0, sizeof(struct backlight_properties));
  104. props.type = BACKLIGHT_RAW;
  105. props.max_brightness = data->max_brightness;
  106. bl = backlight_device_register(dev_name(&pdev->dev), &pdev->dev, pb,
  107. &pwm_backlight_ops, &props);
  108. if (IS_ERR(bl)) {
  109. dev_err(&pdev->dev, "failed to register backlight\n");
  110. ret = PTR_ERR(bl);
  111. goto err_bl;
  112. }
  113. bl->props.brightness = data->dft_brightness;
  114. backlight_update_status(bl);
  115. platform_set_drvdata(pdev, bl);
  116. return 0;
  117. err_bl:
  118. pwm_free(pb->pwm);
  119. err_pwm:
  120. kfree(pb);
  121. err_alloc:
  122. if (data->exit)
  123. data->exit(&pdev->dev);
  124. return ret;
  125. }
  126. static int pwm_backlight_remove(struct platform_device *pdev)
  127. {
  128. struct platform_pwm_backlight_data *data = pdev->dev.platform_data;
  129. struct backlight_device *bl = platform_get_drvdata(pdev);
  130. struct pwm_bl_data *pb = dev_get_drvdata(&bl->dev);
  131. backlight_device_unregister(bl);
  132. pwm_config(pb->pwm, 0, pb->period);
  133. pwm_disable(pb->pwm);
  134. pwm_free(pb->pwm);
  135. kfree(pb);
  136. if (data->exit)
  137. data->exit(&pdev->dev);
  138. return 0;
  139. }
  140. #ifdef CONFIG_PM
  141. static int pwm_backlight_suspend(struct platform_device *pdev,
  142. pm_message_t state)
  143. {
  144. struct backlight_device *bl = platform_get_drvdata(pdev);
  145. struct pwm_bl_data *pb = dev_get_drvdata(&bl->dev);
  146. if (pb->notify)
  147. pb->notify(pb->dev, 0);
  148. pwm_config(pb->pwm, 0, pb->period);
  149. pwm_disable(pb->pwm);
  150. return 0;
  151. }
  152. static int pwm_backlight_resume(struct platform_device *pdev)
  153. {
  154. struct backlight_device *bl = platform_get_drvdata(pdev);
  155. backlight_update_status(bl);
  156. return 0;
  157. }
  158. #else
  159. #define pwm_backlight_suspend NULL
  160. #define pwm_backlight_resume NULL
  161. #endif
  162. static struct platform_driver pwm_backlight_driver = {
  163. .driver = {
  164. .name = "pwm-backlight",
  165. .owner = THIS_MODULE,
  166. },
  167. .probe = pwm_backlight_probe,
  168. .remove = pwm_backlight_remove,
  169. .suspend = pwm_backlight_suspend,
  170. .resume = pwm_backlight_resume,
  171. };
  172. static int __init pwm_backlight_init(void)
  173. {
  174. return platform_driver_register(&pwm_backlight_driver);
  175. }
  176. module_init(pwm_backlight_init);
  177. static void __exit pwm_backlight_exit(void)
  178. {
  179. platform_driver_unregister(&pwm_backlight_driver);
  180. }
  181. module_exit(pwm_backlight_exit);
  182. MODULE_DESCRIPTION("PWM based Backlight Driver");
  183. MODULE_LICENSE("GPL");
  184. MODULE_ALIAS("platform:pwm-backlight");