as3711-regulator.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * AS3711 PMIC regulator driver, using DCDC Step Down and LDO supplies
  4. *
  5. * Copyright (C) 2012 Renesas Electronics Corporation
  6. * Author: Guennadi Liakhovetski, <g.liakhovetski@gmx.de>
  7. */
  8. #include <linux/err.h>
  9. #include <linux/init.h>
  10. #include <linux/mfd/as3711.h>
  11. #include <linux/module.h>
  12. #include <linux/of.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/regmap.h>
  15. #include <linux/regulator/driver.h>
  16. #include <linux/regulator/of_regulator.h>
  17. #include <linux/slab.h>
  18. /*
  19. * The regulator API supports 4 modes of operataion: FAST, NORMAL, IDLE and
  20. * STANDBY. We map them in the following way to AS3711 SD1-4 DCDC modes:
  21. * FAST: sdX_fast=1
  22. * NORMAL: low_noise=1
  23. * IDLE: low_noise=0
  24. */
  25. static int as3711_set_mode_sd(struct regulator_dev *rdev, unsigned int mode)
  26. {
  27. unsigned int fast_bit = rdev->desc->enable_mask,
  28. low_noise_bit = fast_bit << 4;
  29. u8 val;
  30. switch (mode) {
  31. case REGULATOR_MODE_FAST:
  32. val = fast_bit | low_noise_bit;
  33. break;
  34. case REGULATOR_MODE_NORMAL:
  35. val = low_noise_bit;
  36. break;
  37. case REGULATOR_MODE_IDLE:
  38. val = 0;
  39. break;
  40. default:
  41. return -EINVAL;
  42. }
  43. return regmap_update_bits(rdev->regmap, AS3711_SD_CONTROL_1,
  44. low_noise_bit | fast_bit, val);
  45. }
  46. static unsigned int as3711_get_mode_sd(struct regulator_dev *rdev)
  47. {
  48. unsigned int fast_bit = rdev->desc->enable_mask,
  49. low_noise_bit = fast_bit << 4, mask = fast_bit | low_noise_bit;
  50. unsigned int val;
  51. int ret = regmap_read(rdev->regmap, AS3711_SD_CONTROL_1, &val);
  52. if (ret < 0)
  53. return ret;
  54. if ((val & mask) == mask)
  55. return REGULATOR_MODE_FAST;
  56. if ((val & mask) == low_noise_bit)
  57. return REGULATOR_MODE_NORMAL;
  58. if (!(val & mask))
  59. return REGULATOR_MODE_IDLE;
  60. return -EINVAL;
  61. }
  62. static const struct regulator_ops as3711_sd_ops = {
  63. .is_enabled = regulator_is_enabled_regmap,
  64. .enable = regulator_enable_regmap,
  65. .disable = regulator_disable_regmap,
  66. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  67. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  68. .list_voltage = regulator_list_voltage_linear_range,
  69. .map_voltage = regulator_map_voltage_linear_range,
  70. .get_mode = as3711_get_mode_sd,
  71. .set_mode = as3711_set_mode_sd,
  72. };
  73. static const struct regulator_ops as3711_aldo_ops = {
  74. .is_enabled = regulator_is_enabled_regmap,
  75. .enable = regulator_enable_regmap,
  76. .disable = regulator_disable_regmap,
  77. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  78. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  79. .list_voltage = regulator_list_voltage_linear_range,
  80. .map_voltage = regulator_map_voltage_linear_range,
  81. };
  82. static const struct regulator_ops as3711_dldo_ops = {
  83. .is_enabled = regulator_is_enabled_regmap,
  84. .enable = regulator_enable_regmap,
  85. .disable = regulator_disable_regmap,
  86. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  87. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  88. .list_voltage = regulator_list_voltage_linear_range,
  89. .map_voltage = regulator_map_voltage_linear_range,
  90. };
  91. static const struct regulator_linear_range as3711_sd_ranges[] = {
  92. REGULATOR_LINEAR_RANGE(612500, 0x1, 0x40, 12500),
  93. REGULATOR_LINEAR_RANGE(1425000, 0x41, 0x70, 25000),
  94. REGULATOR_LINEAR_RANGE(2650000, 0x71, 0x7f, 50000),
  95. };
  96. static const struct regulator_linear_range as3711_aldo_ranges[] = {
  97. REGULATOR_LINEAR_RANGE(1200000, 0, 0xf, 50000),
  98. REGULATOR_LINEAR_RANGE(1800000, 0x10, 0x1f, 100000),
  99. };
  100. static const struct regulator_linear_range as3711_dldo_ranges[] = {
  101. REGULATOR_LINEAR_RANGE(900000, 0, 0x10, 50000),
  102. REGULATOR_LINEAR_RANGE(1750000, 0x20, 0x3f, 50000),
  103. };
  104. #define AS3711_REG(_id, _en_reg, _en_bit, _vmask, _sfx) \
  105. [AS3711_REGULATOR_ ## _id] = { \
  106. .name = "as3711-regulator-" # _id, \
  107. .id = AS3711_REGULATOR_ ## _id, \
  108. .n_voltages = (_vmask + 1), \
  109. .ops = &as3711_ ## _sfx ## _ops, \
  110. .type = REGULATOR_VOLTAGE, \
  111. .owner = THIS_MODULE, \
  112. .vsel_reg = AS3711_ ## _id ## _VOLTAGE, \
  113. .vsel_mask = _vmask, \
  114. .enable_reg = AS3711_ ## _en_reg, \
  115. .enable_mask = BIT(_en_bit), \
  116. .linear_ranges = as3711_ ## _sfx ## _ranges, \
  117. .n_linear_ranges = ARRAY_SIZE(as3711_ ## _sfx ## _ranges), \
  118. }
  119. static const struct regulator_desc as3711_reg_desc[] = {
  120. AS3711_REG(SD_1, SD_CONTROL, 0, 0x7f, sd),
  121. AS3711_REG(SD_2, SD_CONTROL, 1, 0x7f, sd),
  122. AS3711_REG(SD_3, SD_CONTROL, 2, 0x7f, sd),
  123. AS3711_REG(SD_4, SD_CONTROL, 3, 0x7f, sd),
  124. AS3711_REG(LDO_1, LDO_1_VOLTAGE, 7, 0x1f, aldo),
  125. AS3711_REG(LDO_2, LDO_2_VOLTAGE, 7, 0x1f, aldo),
  126. AS3711_REG(LDO_3, LDO_3_VOLTAGE, 7, 0x3f, dldo),
  127. AS3711_REG(LDO_4, LDO_4_VOLTAGE, 7, 0x3f, dldo),
  128. AS3711_REG(LDO_5, LDO_5_VOLTAGE, 7, 0x3f, dldo),
  129. AS3711_REG(LDO_6, LDO_6_VOLTAGE, 7, 0x3f, dldo),
  130. AS3711_REG(LDO_7, LDO_7_VOLTAGE, 7, 0x3f, dldo),
  131. AS3711_REG(LDO_8, LDO_8_VOLTAGE, 7, 0x3f, dldo),
  132. /* StepUp output voltage depends on supplying regulator */
  133. };
  134. #define AS3711_REGULATOR_NUM ARRAY_SIZE(as3711_reg_desc)
  135. static struct of_regulator_match
  136. as3711_regulator_matches[AS3711_REGULATOR_NUM] = {
  137. [AS3711_REGULATOR_SD_1] = { .name = "sd1" },
  138. [AS3711_REGULATOR_SD_2] = { .name = "sd2" },
  139. [AS3711_REGULATOR_SD_3] = { .name = "sd3" },
  140. [AS3711_REGULATOR_SD_4] = { .name = "sd4" },
  141. [AS3711_REGULATOR_LDO_1] = { .name = "ldo1" },
  142. [AS3711_REGULATOR_LDO_2] = { .name = "ldo2" },
  143. [AS3711_REGULATOR_LDO_3] = { .name = "ldo3" },
  144. [AS3711_REGULATOR_LDO_4] = { .name = "ldo4" },
  145. [AS3711_REGULATOR_LDO_5] = { .name = "ldo5" },
  146. [AS3711_REGULATOR_LDO_6] = { .name = "ldo6" },
  147. [AS3711_REGULATOR_LDO_7] = { .name = "ldo7" },
  148. [AS3711_REGULATOR_LDO_8] = { .name = "ldo8" },
  149. };
  150. static int as3711_regulator_parse_dt(struct device *dev,
  151. struct device_node **of_node, const int count)
  152. {
  153. struct as3711_regulator_pdata *pdata = dev_get_platdata(dev);
  154. struct device_node *regulators =
  155. of_get_child_by_name(dev->parent->of_node, "regulators");
  156. struct of_regulator_match *match;
  157. int ret, i;
  158. if (!regulators) {
  159. dev_err(dev, "regulator node not found\n");
  160. return -ENODEV;
  161. }
  162. ret = of_regulator_match(dev->parent, regulators,
  163. as3711_regulator_matches, count);
  164. of_node_put(regulators);
  165. if (ret < 0) {
  166. dev_err(dev, "Error parsing regulator init data: %d\n", ret);
  167. return ret;
  168. }
  169. for (i = 0, match = as3711_regulator_matches; i < count; i++, match++)
  170. if (match->of_node) {
  171. pdata->init_data[i] = match->init_data;
  172. of_node[i] = match->of_node;
  173. }
  174. return 0;
  175. }
  176. static int as3711_regulator_probe(struct platform_device *pdev)
  177. {
  178. struct as3711_regulator_pdata *pdata = dev_get_platdata(&pdev->dev);
  179. struct as3711 *as3711 = dev_get_drvdata(pdev->dev.parent);
  180. struct regulator_config config = {.dev = &pdev->dev,};
  181. struct device_node *of_node[AS3711_REGULATOR_NUM] = {};
  182. struct regulator_dev *rdev;
  183. int ret;
  184. int id;
  185. if (!pdata) {
  186. dev_err(&pdev->dev, "No platform data...\n");
  187. return -ENODEV;
  188. }
  189. if (pdev->dev.parent->of_node) {
  190. ret = as3711_regulator_parse_dt(&pdev->dev, of_node, AS3711_REGULATOR_NUM);
  191. if (ret < 0) {
  192. dev_err(&pdev->dev, "DT parsing failed: %d\n", ret);
  193. return ret;
  194. }
  195. }
  196. for (id = 0; id < AS3711_REGULATOR_NUM; id++) {
  197. config.init_data = pdata->init_data[id];
  198. config.regmap = as3711->regmap;
  199. config.of_node = of_node[id];
  200. rdev = devm_regulator_register(&pdev->dev, &as3711_reg_desc[id],
  201. &config);
  202. if (IS_ERR(rdev)) {
  203. dev_err(&pdev->dev, "Failed to register regulator %s\n",
  204. as3711_reg_desc[id].name);
  205. return PTR_ERR(rdev);
  206. }
  207. }
  208. return 0;
  209. }
  210. static struct platform_driver as3711_regulator_driver = {
  211. .driver = {
  212. .name = "as3711-regulator",
  213. },
  214. .probe = as3711_regulator_probe,
  215. };
  216. static int __init as3711_regulator_init(void)
  217. {
  218. return platform_driver_register(&as3711_regulator_driver);
  219. }
  220. subsys_initcall(as3711_regulator_init);
  221. static void __exit as3711_regulator_exit(void)
  222. {
  223. platform_driver_unregister(&as3711_regulator_driver);
  224. }
  225. module_exit(as3711_regulator_exit);
  226. MODULE_AUTHOR("Guennadi Liakhovetski <g.liakhovetski@gmx.de>");
  227. MODULE_DESCRIPTION("AS3711 regulator driver");
  228. MODULE_ALIAS("platform:as3711-regulator");
  229. MODULE_LICENSE("GPL v2");