aat2870_bl.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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_get_brightness(struct backlight_device *bd)
  60. {
  61. return bd->props.brightness;
  62. }
  63. static int aat2870_bl_update_status(struct backlight_device *bd)
  64. {
  65. struct aat2870_bl_driver_data *aat2870_bl = dev_get_drvdata(&bd->dev);
  66. struct aat2870_data *aat2870 =
  67. dev_get_drvdata(aat2870_bl->pdev->dev.parent);
  68. int brightness = bd->props.brightness;
  69. int ret;
  70. if ((brightness < 0) || (bd->props.max_brightness < brightness)) {
  71. dev_err(&bd->dev, "invalid brightness, %d\n", brightness);
  72. return -EINVAL;
  73. }
  74. dev_dbg(&bd->dev, "brightness=%d, power=%d, state=%d\n",
  75. bd->props.brightness, bd->props.power, bd->props.state);
  76. if ((bd->props.power != FB_BLANK_UNBLANK) ||
  77. (bd->props.state & BL_CORE_FBBLANK) ||
  78. (bd->props.state & BL_CORE_SUSPENDED))
  79. brightness = 0;
  80. ret = aat2870->write(aat2870, AAT2870_BLM,
  81. (u8)aat2870_brightness(aat2870_bl, brightness));
  82. if (ret < 0)
  83. return ret;
  84. if (brightness == 0) {
  85. ret = aat2870_bl_disable(aat2870_bl);
  86. if (ret < 0)
  87. return ret;
  88. } else if (aat2870_bl->brightness == 0) {
  89. ret = aat2870_bl_enable(aat2870_bl);
  90. if (ret < 0)
  91. return ret;
  92. }
  93. aat2870_bl->brightness = brightness;
  94. return 0;
  95. }
  96. static int aat2870_bl_check_fb(struct backlight_device *bd, struct fb_info *fi)
  97. {
  98. return 1;
  99. }
  100. static const struct backlight_ops aat2870_bl_ops = {
  101. .options = BL_CORE_SUSPENDRESUME,
  102. .get_brightness = aat2870_bl_get_brightness,
  103. .update_status = aat2870_bl_update_status,
  104. .check_fb = aat2870_bl_check_fb,
  105. };
  106. static int aat2870_bl_probe(struct platform_device *pdev)
  107. {
  108. struct aat2870_bl_platform_data *pdata = pdev->dev.platform_data;
  109. struct aat2870_bl_driver_data *aat2870_bl;
  110. struct backlight_device *bd;
  111. struct backlight_properties props;
  112. int ret = 0;
  113. if (!pdata) {
  114. dev_err(&pdev->dev, "No platform data\n");
  115. ret = -ENXIO;
  116. goto out;
  117. }
  118. if (pdev->id != AAT2870_ID_BL) {
  119. dev_err(&pdev->dev, "Invalid device ID, %d\n", pdev->id);
  120. ret = -EINVAL;
  121. goto out;
  122. }
  123. aat2870_bl = kzalloc(sizeof(struct aat2870_bl_driver_data), GFP_KERNEL);
  124. if (!aat2870_bl) {
  125. dev_err(&pdev->dev,
  126. "Failed to allocate memory for aat2870 backlight\n");
  127. ret = -ENOMEM;
  128. goto out;
  129. }
  130. memset(&props, 0, sizeof(struct backlight_properties));
  131. props.type = BACKLIGHT_RAW;
  132. bd = backlight_device_register("aat2870-backlight", &pdev->dev,
  133. aat2870_bl, &aat2870_bl_ops, &props);
  134. if (IS_ERR(bd)) {
  135. dev_err(&pdev->dev,
  136. "Failed allocate memory for backlight device\n");
  137. ret = PTR_ERR(bd);
  138. goto out_kfree;
  139. }
  140. aat2870_bl->pdev = pdev;
  141. platform_set_drvdata(pdev, aat2870_bl);
  142. aat2870_bl->bd = bd;
  143. if (pdata->channels > 0)
  144. aat2870_bl->channels = pdata->channels;
  145. else
  146. aat2870_bl->channels = AAT2870_BL_CH_ALL;
  147. if (pdata->max_current > 0)
  148. aat2870_bl->max_current = pdata->max_current;
  149. else
  150. aat2870_bl->max_current = AAT2870_CURRENT_27_9;
  151. if (pdata->max_brightness > 0)
  152. bd->props.max_brightness = pdata->max_brightness;
  153. else
  154. bd->props.max_brightness = 255;
  155. aat2870_bl->brightness = 0;
  156. bd->props.power = FB_BLANK_UNBLANK;
  157. bd->props.brightness = bd->props.max_brightness;
  158. ret = aat2870_bl_update_status(bd);
  159. if (ret < 0) {
  160. dev_err(&pdev->dev, "Failed to initialize\n");
  161. goto out_bl_dev_unregister;
  162. }
  163. return 0;
  164. out_bl_dev_unregister:
  165. backlight_device_unregister(bd);
  166. out_kfree:
  167. kfree(aat2870_bl);
  168. out:
  169. return ret;
  170. }
  171. static int aat2870_bl_remove(struct platform_device *pdev)
  172. {
  173. struct aat2870_bl_driver_data *aat2870_bl = platform_get_drvdata(pdev);
  174. struct backlight_device *bd = aat2870_bl->bd;
  175. bd->props.power = FB_BLANK_POWERDOWN;
  176. bd->props.brightness = 0;
  177. backlight_update_status(bd);
  178. backlight_device_unregister(bd);
  179. kfree(aat2870_bl);
  180. return 0;
  181. }
  182. static struct platform_driver aat2870_bl_driver = {
  183. .driver = {
  184. .name = "aat2870-backlight",
  185. .owner = THIS_MODULE,
  186. },
  187. .probe = aat2870_bl_probe,
  188. .remove = aat2870_bl_remove,
  189. };
  190. static int __init aat2870_bl_init(void)
  191. {
  192. return platform_driver_register(&aat2870_bl_driver);
  193. }
  194. subsys_initcall(aat2870_bl_init);
  195. static void __exit aat2870_bl_exit(void)
  196. {
  197. platform_driver_unregister(&aat2870_bl_driver);
  198. }
  199. module_exit(aat2870_bl_exit);
  200. MODULE_DESCRIPTION("AnalogicTech AAT2870 Backlight");
  201. MODULE_LICENSE("GPL");
  202. MODULE_AUTHOR("Jin Park <jinyoungp@nvidia.com>");