as3711-regulator.c 8.5 KB

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