wm8400-regulator.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /*
  2. * Regulator support for WM8400
  3. *
  4. * Copyright 2008 Wolfson Microelectronics PLC.
  5. *
  6. * Author: Mark Brown <[email protected]>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of the
  11. * License, or (at your option) any later version.
  12. *
  13. */
  14. #include <linux/bug.h>
  15. #include <linux/err.h>
  16. #include <linux/kernel.h>
  17. #include <linux/module.h>
  18. #include <linux/regulator/driver.h>
  19. #include <linux/mfd/wm8400-private.h>
  20. static const struct regulator_linear_range wm8400_ldo_ranges[] = {
  21. REGULATOR_LINEAR_RANGE(900000, 0, 14, 50000),
  22. REGULATOR_LINEAR_RANGE(1700000, 15, 31, 100000),
  23. };
  24. static const struct regulator_ops wm8400_ldo_ops = {
  25. .is_enabled = regulator_is_enabled_regmap,
  26. .enable = regulator_enable_regmap,
  27. .disable = regulator_disable_regmap,
  28. .list_voltage = regulator_list_voltage_linear_range,
  29. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  30. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  31. .map_voltage = regulator_map_voltage_linear_range,
  32. };
  33. static unsigned int wm8400_dcdc_get_mode(struct regulator_dev *dev)
  34. {
  35. struct wm8400 *wm8400 = rdev_get_drvdata(dev);
  36. int offset = (rdev_get_id(dev) - WM8400_DCDC1) * 2;
  37. u16 data[2];
  38. int ret;
  39. ret = wm8400_block_read(wm8400, WM8400_DCDC1_CONTROL_1 + offset, 2,
  40. data);
  41. if (ret != 0)
  42. return 0;
  43. /* Datasheet: hibernate */
  44. if (data[0] & WM8400_DC1_SLEEP)
  45. return REGULATOR_MODE_STANDBY;
  46. /* Datasheet: standby */
  47. if (!(data[0] & WM8400_DC1_ACTIVE))
  48. return REGULATOR_MODE_IDLE;
  49. /* Datasheet: active with or without force PWM */
  50. if (data[1] & WM8400_DC1_FRC_PWM)
  51. return REGULATOR_MODE_FAST;
  52. else
  53. return REGULATOR_MODE_NORMAL;
  54. }
  55. static int wm8400_dcdc_set_mode(struct regulator_dev *dev, unsigned int mode)
  56. {
  57. struct wm8400 *wm8400 = rdev_get_drvdata(dev);
  58. int offset = (rdev_get_id(dev) - WM8400_DCDC1) * 2;
  59. int ret;
  60. switch (mode) {
  61. case REGULATOR_MODE_FAST:
  62. /* Datasheet: active with force PWM */
  63. ret = wm8400_set_bits(wm8400, WM8400_DCDC1_CONTROL_2 + offset,
  64. WM8400_DC1_FRC_PWM, WM8400_DC1_FRC_PWM);
  65. if (ret != 0)
  66. return ret;
  67. return wm8400_set_bits(wm8400, WM8400_DCDC1_CONTROL_1 + offset,
  68. WM8400_DC1_ACTIVE | WM8400_DC1_SLEEP,
  69. WM8400_DC1_ACTIVE);
  70. case REGULATOR_MODE_NORMAL:
  71. /* Datasheet: active */
  72. ret = wm8400_set_bits(wm8400, WM8400_DCDC1_CONTROL_2 + offset,
  73. WM8400_DC1_FRC_PWM, 0);
  74. if (ret != 0)
  75. return ret;
  76. return wm8400_set_bits(wm8400, WM8400_DCDC1_CONTROL_1 + offset,
  77. WM8400_DC1_ACTIVE | WM8400_DC1_SLEEP,
  78. WM8400_DC1_ACTIVE);
  79. case REGULATOR_MODE_IDLE:
  80. /* Datasheet: standby */
  81. return wm8400_set_bits(wm8400, WM8400_DCDC1_CONTROL_1 + offset,
  82. WM8400_DC1_ACTIVE | WM8400_DC1_SLEEP, 0);
  83. default:
  84. return -EINVAL;
  85. }
  86. }
  87. static unsigned int wm8400_dcdc_get_optimum_mode(struct regulator_dev *dev,
  88. int input_uV, int output_uV,
  89. int load_uA)
  90. {
  91. return REGULATOR_MODE_NORMAL;
  92. }
  93. static const struct regulator_ops wm8400_dcdc_ops = {
  94. .is_enabled = regulator_is_enabled_regmap,
  95. .enable = regulator_enable_regmap,
  96. .disable = regulator_disable_regmap,
  97. .list_voltage = regulator_list_voltage_linear,
  98. .map_voltage = regulator_map_voltage_linear,
  99. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  100. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  101. .get_mode = wm8400_dcdc_get_mode,
  102. .set_mode = wm8400_dcdc_set_mode,
  103. .get_optimum_mode = wm8400_dcdc_get_optimum_mode,
  104. };
  105. static struct regulator_desc regulators[] = {
  106. {
  107. .name = "LDO1",
  108. .id = WM8400_LDO1,
  109. .ops = &wm8400_ldo_ops,
  110. .enable_reg = WM8400_LDO1_CONTROL,
  111. .enable_mask = WM8400_LDO1_ENA,
  112. .n_voltages = WM8400_LDO1_VSEL_MASK + 1,
  113. .linear_ranges = wm8400_ldo_ranges,
  114. .n_linear_ranges = ARRAY_SIZE(wm8400_ldo_ranges),
  115. .vsel_reg = WM8400_LDO1_CONTROL,
  116. .vsel_mask = WM8400_LDO1_VSEL_MASK,
  117. .type = REGULATOR_VOLTAGE,
  118. .owner = THIS_MODULE,
  119. },
  120. {
  121. .name = "LDO2",
  122. .id = WM8400_LDO2,
  123. .ops = &wm8400_ldo_ops,
  124. .enable_reg = WM8400_LDO2_CONTROL,
  125. .enable_mask = WM8400_LDO2_ENA,
  126. .n_voltages = WM8400_LDO2_VSEL_MASK + 1,
  127. .linear_ranges = wm8400_ldo_ranges,
  128. .n_linear_ranges = ARRAY_SIZE(wm8400_ldo_ranges),
  129. .type = REGULATOR_VOLTAGE,
  130. .vsel_reg = WM8400_LDO2_CONTROL,
  131. .vsel_mask = WM8400_LDO2_VSEL_MASK,
  132. .owner = THIS_MODULE,
  133. },
  134. {
  135. .name = "LDO3",
  136. .id = WM8400_LDO3,
  137. .ops = &wm8400_ldo_ops,
  138. .enable_reg = WM8400_LDO3_CONTROL,
  139. .enable_mask = WM8400_LDO3_ENA,
  140. .n_voltages = WM8400_LDO3_VSEL_MASK + 1,
  141. .linear_ranges = wm8400_ldo_ranges,
  142. .n_linear_ranges = ARRAY_SIZE(wm8400_ldo_ranges),
  143. .vsel_reg = WM8400_LDO3_CONTROL,
  144. .vsel_mask = WM8400_LDO3_VSEL_MASK,
  145. .type = REGULATOR_VOLTAGE,
  146. .owner = THIS_MODULE,
  147. },
  148. {
  149. .name = "LDO4",
  150. .id = WM8400_LDO4,
  151. .ops = &wm8400_ldo_ops,
  152. .enable_reg = WM8400_LDO4_CONTROL,
  153. .enable_mask = WM8400_LDO4_ENA,
  154. .n_voltages = WM8400_LDO4_VSEL_MASK + 1,
  155. .linear_ranges = wm8400_ldo_ranges,
  156. .n_linear_ranges = ARRAY_SIZE(wm8400_ldo_ranges),
  157. .vsel_reg = WM8400_LDO4_CONTROL,
  158. .vsel_mask = WM8400_LDO4_VSEL_MASK,
  159. .type = REGULATOR_VOLTAGE,
  160. .owner = THIS_MODULE,
  161. },
  162. {
  163. .name = "DCDC1",
  164. .id = WM8400_DCDC1,
  165. .ops = &wm8400_dcdc_ops,
  166. .enable_reg = WM8400_DCDC1_CONTROL_1,
  167. .enable_mask = WM8400_DC1_ENA_MASK,
  168. .n_voltages = WM8400_DC1_VSEL_MASK + 1,
  169. .vsel_reg = WM8400_DCDC1_CONTROL_1,
  170. .vsel_mask = WM8400_DC1_VSEL_MASK,
  171. .min_uV = 850000,
  172. .uV_step = 25000,
  173. .type = REGULATOR_VOLTAGE,
  174. .owner = THIS_MODULE,
  175. },
  176. {
  177. .name = "DCDC2",
  178. .id = WM8400_DCDC2,
  179. .ops = &wm8400_dcdc_ops,
  180. .enable_reg = WM8400_DCDC2_CONTROL_1,
  181. .enable_mask = WM8400_DC1_ENA_MASK,
  182. .n_voltages = WM8400_DC2_VSEL_MASK + 1,
  183. .vsel_reg = WM8400_DCDC2_CONTROL_1,
  184. .vsel_mask = WM8400_DC2_VSEL_MASK,
  185. .min_uV = 850000,
  186. .uV_step = 25000,
  187. .type = REGULATOR_VOLTAGE,
  188. .owner = THIS_MODULE,
  189. },
  190. };
  191. static int wm8400_regulator_probe(struct platform_device *pdev)
  192. {
  193. struct wm8400 *wm8400 = container_of(pdev, struct wm8400, regulators[pdev->id]);
  194. struct regulator_config config = { };
  195. struct regulator_dev *rdev;
  196. config.dev = &pdev->dev;
  197. config.init_data = dev_get_platdata(&pdev->dev);
  198. config.driver_data = wm8400;
  199. config.regmap = wm8400->regmap;
  200. rdev = devm_regulator_register(&pdev->dev, &regulators[pdev->id],
  201. &config);
  202. if (IS_ERR(rdev))
  203. return PTR_ERR(rdev);
  204. platform_set_drvdata(pdev, rdev);
  205. return 0;
  206. }
  207. static struct platform_driver wm8400_regulator_driver = {
  208. .driver = {
  209. .name = "wm8400-regulator",
  210. },
  211. .probe = wm8400_regulator_probe,
  212. };
  213. /**
  214. * wm8400_register_regulator - enable software control of a WM8400 regulator
  215. *
  216. * This function enables software control of a WM8400 regulator via
  217. * the regulator API. It is intended to be called from the
  218. * platform_init() callback of the WM8400 MFD driver.
  219. *
  220. * @param dev The WM8400 device to operate on.
  221. * @param reg The regulator to control.
  222. * @param initdata Regulator initdata for the regulator.
  223. */
  224. int wm8400_register_regulator(struct device *dev, int reg,
  225. struct regulator_init_data *initdata)
  226. {
  227. struct wm8400 *wm8400 = dev_get_drvdata(dev);
  228. if (wm8400->regulators[reg].name)
  229. return -EBUSY;
  230. initdata->driver_data = wm8400;
  231. wm8400->regulators[reg].name = "wm8400-regulator";
  232. wm8400->regulators[reg].id = reg;
  233. wm8400->regulators[reg].dev.parent = dev;
  234. wm8400->regulators[reg].dev.platform_data = initdata;
  235. return platform_device_register(&wm8400->regulators[reg]);
  236. }
  237. EXPORT_SYMBOL_GPL(wm8400_register_regulator);
  238. static int __init wm8400_regulator_init(void)
  239. {
  240. return platform_driver_register(&wm8400_regulator_driver);
  241. }
  242. subsys_initcall(wm8400_regulator_init);
  243. static void __exit wm8400_regulator_exit(void)
  244. {
  245. platform_driver_unregister(&wm8400_regulator_driver);
  246. }
  247. module_exit(wm8400_regulator_exit);
  248. MODULE_AUTHOR("Mark Brown <[email protected]>");
  249. MODULE_DESCRIPTION("WM8400 regulator driver");
  250. MODULE_LICENSE("GPL");
  251. MODULE_ALIAS("platform:wm8400-regulator");