wm8400-regulator.c 7.7 KB

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