wm8400-regulator.c 8.1 KB

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