aat2870_bl.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. * linux/drivers/video/backlight/aat2870_bl.c
  3. *
  4. * Copyright (c) 2011, NVIDIA Corporation.
  5. * Author: Jin Park <jinyoungp@nvidia.com>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * version 2 as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  19. * 02110-1301 USA
  20. */
  21. #include <linux/module.h>
  22. #include <linux/kernel.h>
  23. #include <linux/init.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/mutex.h>
  26. #include <linux/delay.h>
  27. #include <linux/fb.h>
  28. #include <linux/backlight.h>
  29. #include <linux/mfd/aat2870.h>
  30. struct aat2870_bl_driver_data {
  31. struct platform_device *pdev;
  32. struct backlight_device *bd;
  33. int channels;
  34. int max_current;
  35. int brightness; /* current brightness */
  36. };
  37. static inline int aat2870_brightness(struct aat2870_bl_driver_data *aat2870_bl,
  38. int brightness)
  39. {
  40. struct backlight_device *bd = aat2870_bl->bd;
  41. int val;
  42. val = brightness * (aat2870_bl->max_current - 1);
  43. val /= bd->props.max_brightness;
  44. return val;
  45. }
  46. static inline int aat2870_bl_enable(struct aat2870_bl_driver_data *aat2870_bl)
  47. {
  48. struct aat2870_data *aat2870
  49. = dev_get_drvdata(aat2870_bl->pdev->dev.parent);
  50. return aat2870->write(aat2870, AAT2870_BL_CH_EN,
  51. (u8)aat2870_bl->channels);
  52. }
  53. static inline int aat2870_bl_disable(struct aat2870_bl_driver_data *aat2870_bl)
  54. {
  55. struct aat2870_data *aat2870
  56. = dev_get_drvdata(aat2870_bl->pdev->dev.parent);
  57. return aat2870->write(aat2870, AAT2870_BL_CH_EN, 0x0);
  58. }
  59. static int aat2870_bl_update_status(struct backlight_device *bd)
  60. {
  61. struct aat2870_bl_driver_data *aat2870_bl = bl_get_data(bd);
  62. struct aat2870_data *aat2870 =
  63. dev_get_drvdata(aat2870_bl->pdev->dev.parent);
  64. int brightness = bd->props.brightness;
  65. int ret;
  66. if ((brightness < 0) || (bd->props.max_brightness < brightness)) {
  67. dev_err(&bd->dev, "invalid brightness, %d\n", brightness);
  68. return -EINVAL;
  69. }
  70. dev_dbg(&bd->dev, "brightness=%d, power=%d, state=%d\n",
  71. bd->props.brightness, bd->props.power, bd->props.state);
  72. if ((bd->props.power != FB_BLANK_UNBLANK) ||
  73. (bd->props.state & BL_CORE_FBBLANK) ||
  74. (bd->props.state & BL_CORE_SUSPENDED))
  75. brightness = 0;
  76. ret = aat2870->write(aat2870, AAT2870_BLM,
  77. (u8)aat2870_brightness(aat2870_bl, brightness));
  78. if (ret < 0)
  79. return ret;
  80. if (brightness == 0) {
  81. ret = aat2870_bl_disable(aat2870_bl);
  82. if (ret < 0)
  83. return ret;
  84. } else if (aat2870_bl->brightness == 0) {
  85. ret = aat2870_bl_enable(aat2870_bl);
  86. if (ret < 0)
  87. return ret;
  88. }
  89. aat2870_bl->brightness = brightness;
  90. return 0;
  91. }
  92. static int aat2870_bl_check_fb(struct backlight_device *bd, struct fb_info *fi)
  93. {
  94. return 1;
  95. }
  96. static const struct backlight_ops aat2870_bl_ops = {
  97. .options = BL_CORE_SUSPENDRESUME,
  98. .update_status = aat2870_bl_update_status,
  99. .check_fb = aat2870_bl_check_fb,
  100. };
  101. static int aat2870_bl_probe(struct platform_device *pdev)
  102. {
  103. struct aat2870_bl_platform_data *pdata = dev_get_platdata(&pdev->dev);
  104. struct aat2870_bl_driver_data *aat2870_bl;
  105. struct backlight_device *bd;
  106. struct backlight_properties props;
  107. int ret = 0;
  108. if (!pdata) {
  109. dev_err(&pdev->dev, "No platform data\n");
  110. ret = -ENXIO;
  111. goto out;
  112. }
  113. if (pdev->id != AAT2870_ID_BL) {
  114. dev_err(&pdev->dev, "Invalid device ID, %d\n", pdev->id);
  115. ret = -EINVAL;
  116. goto out;
  117. }
  118. aat2870_bl = devm_kzalloc(&pdev->dev,
  119. sizeof(struct aat2870_bl_driver_data),
  120. GFP_KERNEL);
  121. if (!aat2870_bl) {
  122. ret = -ENOMEM;
  123. goto out;
  124. }
  125. memset(&props, 0, sizeof(struct backlight_properties));
  126. props.type = BACKLIGHT_RAW;
  127. bd = devm_backlight_device_register(&pdev->dev, "aat2870-backlight",
  128. &pdev->dev, aat2870_bl, &aat2870_bl_ops,
  129. &props);
  130. if (IS_ERR(bd)) {
  131. dev_err(&pdev->dev,
  132. "Failed allocate memory for backlight device\n");
  133. ret = PTR_ERR(bd);
  134. goto out;
  135. }
  136. aat2870_bl->pdev = pdev;
  137. platform_set_drvdata(pdev, aat2870_bl);
  138. aat2870_bl->bd = bd;
  139. if (pdata->channels > 0)
  140. aat2870_bl->channels = pdata->channels;
  141. else
  142. aat2870_bl->channels = AAT2870_BL_CH_ALL;
  143. if (pdata->max_current > 0)
  144. aat2870_bl->max_current = pdata->max_current;
  145. else
  146. aat2870_bl->max_current = AAT2870_CURRENT_27_9;
  147. if (pdata->max_brightness > 0)
  148. bd->props.max_brightness = pdata->max_brightness;
  149. else
  150. bd->props.max_brightness = 255;
  151. aat2870_bl->brightness = 0;
  152. bd->props.power = FB_BLANK_UNBLANK;
  153. bd->props.brightness = bd->props.max_brightness;
  154. ret = aat2870_bl_update_status(bd);
  155. if (ret < 0) {
  156. dev_err(&pdev->dev, "Failed to initialize\n");
  157. return ret;
  158. }
  159. return 0;
  160. out:
  161. return ret;
  162. }
  163. static int aat2870_bl_remove(struct platform_device *pdev)
  164. {
  165. struct aat2870_bl_driver_data *aat2870_bl = platform_get_drvdata(pdev);
  166. struct backlight_device *bd = aat2870_bl->bd;
  167. bd->props.power = FB_BLANK_POWERDOWN;
  168. bd->props.brightness = 0;
  169. backlight_update_status(bd);
  170. return 0;
  171. }
  172. static struct platform_driver aat2870_bl_driver = {
  173. .driver = {
  174. .name = "aat2870-backlight",
  175. },
  176. .probe = aat2870_bl_probe,
  177. .remove = aat2870_bl_remove,
  178. };
  179. static int __init aat2870_bl_init(void)
  180. {
  181. return platform_driver_register(&aat2870_bl_driver);
  182. }
  183. subsys_initcall(aat2870_bl_init);
  184. static void __exit aat2870_bl_exit(void)
  185. {
  186. platform_driver_unregister(&aat2870_bl_driver);
  187. }
  188. module_exit(aat2870_bl_exit);
  189. MODULE_DESCRIPTION("AnalogicTech AAT2870 Backlight");
  190. MODULE_LICENSE("GPL");
  191. MODULE_AUTHOR("Jin Park <jinyoungp@nvidia.com>");