pwm_bl.c 4.9 KB

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