wm831x_bl.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /*
  2. * Backlight driver for Wolfson Microelectronics WM831x PMICs
  3. *
  4. * Copyright 2009 Wolfson Microelectonics plc
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/init.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/module.h>
  14. #include <linux/fb.h>
  15. #include <linux/backlight.h>
  16. #include <linux/slab.h>
  17. #include <linux/mfd/wm831x/core.h>
  18. #include <linux/mfd/wm831x/pdata.h>
  19. #include <linux/mfd/wm831x/regulator.h>
  20. struct wm831x_backlight_data {
  21. struct wm831x *wm831x;
  22. int isink_reg;
  23. int current_brightness;
  24. };
  25. static int wm831x_backlight_set(struct backlight_device *bl, int brightness)
  26. {
  27. struct wm831x_backlight_data *data = bl_get_data(bl);
  28. struct wm831x *wm831x = data->wm831x;
  29. int power_up = !data->current_brightness && brightness;
  30. int power_down = data->current_brightness && !brightness;
  31. int ret;
  32. if (power_up) {
  33. /* Enable the ISINK */
  34. ret = wm831x_set_bits(wm831x, data->isink_reg,
  35. WM831X_CS1_ENA, WM831X_CS1_ENA);
  36. if (ret < 0)
  37. goto err;
  38. /* Enable the DC-DC */
  39. ret = wm831x_set_bits(wm831x, WM831X_DCDC_ENABLE,
  40. WM831X_DC4_ENA, WM831X_DC4_ENA);
  41. if (ret < 0)
  42. goto err;
  43. }
  44. if (power_down) {
  45. /* DCDC first */
  46. ret = wm831x_set_bits(wm831x, WM831X_DCDC_ENABLE,
  47. WM831X_DC4_ENA, 0);
  48. if (ret < 0)
  49. goto err;
  50. /* ISINK */
  51. ret = wm831x_set_bits(wm831x, data->isink_reg,
  52. WM831X_CS1_DRIVE | WM831X_CS1_ENA, 0);
  53. if (ret < 0)
  54. goto err;
  55. }
  56. /* Set the new brightness */
  57. ret = wm831x_set_bits(wm831x, data->isink_reg,
  58. WM831X_CS1_ISEL_MASK, brightness);
  59. if (ret < 0)
  60. goto err;
  61. if (power_up) {
  62. /* Drive current through the ISINK */
  63. ret = wm831x_set_bits(wm831x, data->isink_reg,
  64. WM831X_CS1_DRIVE, WM831X_CS1_DRIVE);
  65. if (ret < 0)
  66. return ret;
  67. }
  68. data->current_brightness = brightness;
  69. return 0;
  70. err:
  71. /* If we were in the middle of a power transition always shut down
  72. * for safety.
  73. */
  74. if (power_up || power_down) {
  75. wm831x_set_bits(wm831x, WM831X_DCDC_ENABLE, WM831X_DC4_ENA, 0);
  76. wm831x_set_bits(wm831x, data->isink_reg, WM831X_CS1_ENA, 0);
  77. }
  78. return ret;
  79. }
  80. static int wm831x_backlight_update_status(struct backlight_device *bl)
  81. {
  82. int brightness = bl->props.brightness;
  83. if (bl->props.power != FB_BLANK_UNBLANK)
  84. brightness = 0;
  85. if (bl->props.fb_blank != FB_BLANK_UNBLANK)
  86. brightness = 0;
  87. if (bl->props.state & BL_CORE_SUSPENDED)
  88. brightness = 0;
  89. return wm831x_backlight_set(bl, brightness);
  90. }
  91. static int wm831x_backlight_get_brightness(struct backlight_device *bl)
  92. {
  93. struct wm831x_backlight_data *data = bl_get_data(bl);
  94. return data->current_brightness;
  95. }
  96. static const struct backlight_ops wm831x_backlight_ops = {
  97. .options = BL_CORE_SUSPENDRESUME,
  98. .update_status = wm831x_backlight_update_status,
  99. .get_brightness = wm831x_backlight_get_brightness,
  100. };
  101. static int wm831x_backlight_probe(struct platform_device *pdev)
  102. {
  103. struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
  104. struct wm831x_pdata *wm831x_pdata = dev_get_platdata(pdev->dev.parent);
  105. struct wm831x_backlight_pdata *pdata;
  106. struct wm831x_backlight_data *data;
  107. struct backlight_device *bl;
  108. struct backlight_properties props;
  109. int ret, i, max_isel, isink_reg, dcdc_cfg;
  110. /* We need platform data */
  111. if (wm831x_pdata)
  112. pdata = wm831x_pdata->backlight;
  113. else
  114. pdata = NULL;
  115. if (!pdata) {
  116. dev_err(&pdev->dev, "No platform data supplied\n");
  117. return -EINVAL;
  118. }
  119. /* Figure out the maximum current we can use */
  120. for (i = 0; i < WM831X_ISINK_MAX_ISEL; i++) {
  121. if (wm831x_isinkv_values[i] > pdata->max_uA)
  122. break;
  123. }
  124. if (i == 0) {
  125. dev_err(&pdev->dev, "Invalid max_uA: %duA\n", pdata->max_uA);
  126. return -EINVAL;
  127. }
  128. max_isel = i - 1;
  129. if (pdata->max_uA != wm831x_isinkv_values[max_isel])
  130. dev_warn(&pdev->dev,
  131. "Maximum current is %duA not %duA as requested\n",
  132. wm831x_isinkv_values[max_isel], pdata->max_uA);
  133. switch (pdata->isink) {
  134. case 1:
  135. isink_reg = WM831X_CURRENT_SINK_1;
  136. dcdc_cfg = 0;
  137. break;
  138. case 2:
  139. isink_reg = WM831X_CURRENT_SINK_2;
  140. dcdc_cfg = WM831X_DC4_FBSRC;
  141. break;
  142. default:
  143. dev_err(&pdev->dev, "Invalid ISINK %d\n", pdata->isink);
  144. return -EINVAL;
  145. }
  146. /* Configure the ISINK to use for feedback */
  147. ret = wm831x_reg_unlock(wm831x);
  148. if (ret < 0)
  149. return ret;
  150. ret = wm831x_set_bits(wm831x, WM831X_DC4_CONTROL, WM831X_DC4_FBSRC,
  151. dcdc_cfg);
  152. wm831x_reg_lock(wm831x);
  153. if (ret < 0)
  154. return ret;
  155. data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
  156. if (data == NULL)
  157. return -ENOMEM;
  158. data->wm831x = wm831x;
  159. data->current_brightness = 0;
  160. data->isink_reg = isink_reg;
  161. memset(&props, 0, sizeof(props));
  162. props.type = BACKLIGHT_RAW;
  163. props.max_brightness = max_isel;
  164. bl = devm_backlight_device_register(&pdev->dev, "wm831x", &pdev->dev,
  165. data, &wm831x_backlight_ops, &props);
  166. if (IS_ERR(bl)) {
  167. dev_err(&pdev->dev, "failed to register backlight\n");
  168. return PTR_ERR(bl);
  169. }
  170. bl->props.brightness = max_isel;
  171. platform_set_drvdata(pdev, bl);
  172. /* Disable the DCDC if it was started so we can bootstrap */
  173. wm831x_set_bits(wm831x, WM831X_DCDC_ENABLE, WM831X_DC4_ENA, 0);
  174. backlight_update_status(bl);
  175. return 0;
  176. }
  177. static struct platform_driver wm831x_backlight_driver = {
  178. .driver = {
  179. .name = "wm831x-backlight",
  180. },
  181. .probe = wm831x_backlight_probe,
  182. };
  183. module_platform_driver(wm831x_backlight_driver);
  184. MODULE_DESCRIPTION("Backlight Driver for WM831x PMICs");
  185. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com");
  186. MODULE_LICENSE("GPL");
  187. MODULE_ALIAS("platform:wm831x-backlight");