101-regulator-axp20x-prep-support-for-multiple-axp-families.patch 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. From f95b73ba272eac5495e69e12795d5ea8bc81d2cd Mon Sep 17 00:00:00 2001
  2. From: Boris BREZILLON <boris.brezillon@free-electrons.com>
  3. Date: Fri, 10 Apr 2015 12:09:03 +0800
  4. Subject: [PATCH] regulator: axp20x: Prepare support for multiple AXP chip
  5. families
  6. Rework the AXP20X_ macros and probe function to support the several chip
  7. families, so that each family can define it's own set of regulators.
  8. Signed-off-by: Boris BREZILLON <boris.brezillon@free-electrons.com>
  9. [wens@csie.org: Support different DC-DC work frequency ranges]
  10. Signed-off-by: Chen-Yu Tsai <wens@csie.org>
  11. Reviewed-by: Mark Brown <broonie@kernel.org>
  12. Signed-off-by: Lee Jones <lee.jones@linaro.org>
  13. ---
  14. drivers/regulator/axp20x-regulator.c | 143 +++++++++++++++++++++++------------
  15. 1 file changed, 94 insertions(+), 49 deletions(-)
  16. --- a/drivers/regulator/axp20x-regulator.c
  17. +++ b/drivers/regulator/axp20x-regulator.c
  18. @@ -32,15 +32,15 @@
  19. #define AXP20X_FREQ_DCDC_MASK 0x0f
  20. -#define AXP20X_DESC_IO(_id, _match, _supply, _min, _max, _step, _vreg, _vmask, \
  21. - _ereg, _emask, _enable_val, _disable_val) \
  22. - [AXP20X_##_id] = { \
  23. +#define AXP_DESC_IO(_family, _id, _match, _supply, _min, _max, _step, _vreg, \
  24. + _vmask, _ereg, _emask, _enable_val, _disable_val) \
  25. + [_family##_##_id] = { \
  26. .name = #_id, \
  27. .supply_name = (_supply), \
  28. .of_match = of_match_ptr(_match), \
  29. .regulators_node = of_match_ptr("regulators"), \
  30. .type = REGULATOR_VOLTAGE, \
  31. - .id = AXP20X_##_id, \
  32. + .id = _family##_##_id, \
  33. .n_voltages = (((_max) - (_min)) / (_step) + 1), \
  34. .owner = THIS_MODULE, \
  35. .min_uV = (_min) * 1000, \
  36. @@ -54,15 +54,15 @@
  37. .ops = &axp20x_ops, \
  38. }
  39. -#define AXP20X_DESC(_id, _match, _supply, _min, _max, _step, _vreg, _vmask, \
  40. - _ereg, _emask) \
  41. - [AXP20X_##_id] = { \
  42. +#define AXP_DESC(_family, _id, _match, _supply, _min, _max, _step, _vreg, \
  43. + _vmask, _ereg, _emask) \
  44. + [_family##_##_id] = { \
  45. .name = #_id, \
  46. .supply_name = (_supply), \
  47. .of_match = of_match_ptr(_match), \
  48. .regulators_node = of_match_ptr("regulators"), \
  49. .type = REGULATOR_VOLTAGE, \
  50. - .id = AXP20X_##_id, \
  51. + .id = _family##_##_id, \
  52. .n_voltages = (((_max) - (_min)) / (_step) + 1), \
  53. .owner = THIS_MODULE, \
  54. .min_uV = (_min) * 1000, \
  55. @@ -74,29 +74,29 @@
  56. .ops = &axp20x_ops, \
  57. }
  58. -#define AXP20X_DESC_FIXED(_id, _match, _supply, _volt) \
  59. - [AXP20X_##_id] = { \
  60. +#define AXP_DESC_FIXED(_family, _id, _match, _supply, _volt) \
  61. + [_family##_##_id] = { \
  62. .name = #_id, \
  63. .supply_name = (_supply), \
  64. .of_match = of_match_ptr(_match), \
  65. .regulators_node = of_match_ptr("regulators"), \
  66. .type = REGULATOR_VOLTAGE, \
  67. - .id = AXP20X_##_id, \
  68. + .id = _family##_##_id, \
  69. .n_voltages = 1, \
  70. .owner = THIS_MODULE, \
  71. .min_uV = (_volt) * 1000, \
  72. .ops = &axp20x_ops_fixed \
  73. }
  74. -#define AXP20X_DESC_TABLE(_id, _match, _supply, _table, _vreg, _vmask, _ereg, \
  75. - _emask) \
  76. - [AXP20X_##_id] = { \
  77. +#define AXP_DESC_TABLE(_family, _id, _match, _supply, _table, _vreg, _vmask, \
  78. + _ereg, _emask) \
  79. + [_family##_##_id] = { \
  80. .name = #_id, \
  81. .supply_name = (_supply), \
  82. .of_match = of_match_ptr(_match), \
  83. .regulators_node = of_match_ptr("regulators"), \
  84. .type = REGULATOR_VOLTAGE, \
  85. - .id = AXP20X_##_id, \
  86. + .id = _family##_##_id, \
  87. .n_voltages = ARRAY_SIZE(_table), \
  88. .owner = THIS_MODULE, \
  89. .vsel_reg = (_vreg), \
  90. @@ -136,37 +136,57 @@ static struct regulator_ops axp20x_ops =
  91. };
  92. static const struct regulator_desc axp20x_regulators[] = {
  93. - AXP20X_DESC(DCDC2, "dcdc2", "vin2", 700, 2275, 25, AXP20X_DCDC2_V_OUT,
  94. - 0x3f, AXP20X_PWR_OUT_CTRL, 0x10),
  95. - AXP20X_DESC(DCDC3, "dcdc3", "vin3", 700, 3500, 25, AXP20X_DCDC3_V_OUT,
  96. - 0x7f, AXP20X_PWR_OUT_CTRL, 0x02),
  97. - AXP20X_DESC_FIXED(LDO1, "ldo1", "acin", 1300),
  98. - AXP20X_DESC(LDO2, "ldo2", "ldo24in", 1800, 3300, 100,
  99. - AXP20X_LDO24_V_OUT, 0xf0, AXP20X_PWR_OUT_CTRL, 0x04),
  100. - AXP20X_DESC(LDO3, "ldo3", "ldo3in", 700, 3500, 25, AXP20X_LDO3_V_OUT,
  101. - 0x7f, AXP20X_PWR_OUT_CTRL, 0x40),
  102. - AXP20X_DESC_TABLE(LDO4, "ldo4", "ldo24in", axp20x_ldo4_data,
  103. - AXP20X_LDO24_V_OUT, 0x0f, AXP20X_PWR_OUT_CTRL, 0x08),
  104. - AXP20X_DESC_IO(LDO5, "ldo5", "ldo5in", 1800, 3300, 100,
  105. - AXP20X_LDO5_V_OUT, 0xf0, AXP20X_GPIO0_CTRL, 0x07,
  106. - AXP20X_IO_ENABLED, AXP20X_IO_DISABLED),
  107. + AXP_DESC(AXP20X, DCDC2, "dcdc2", "vin2", 700, 2275, 25,
  108. + AXP20X_DCDC2_V_OUT, 0x3f, AXP20X_PWR_OUT_CTRL, 0x10),
  109. + AXP_DESC(AXP20X, DCDC3, "dcdc3", "vin3", 700, 3500, 25,
  110. + AXP20X_DCDC3_V_OUT, 0x7f, AXP20X_PWR_OUT_CTRL, 0x02),
  111. + AXP_DESC_FIXED(AXP20X, LDO1, "ldo1", "acin", 1300),
  112. + AXP_DESC(AXP20X, LDO2, "ldo2", "ldo24in", 1800, 3300, 100,
  113. + AXP20X_LDO24_V_OUT, 0xf0, AXP20X_PWR_OUT_CTRL, 0x04),
  114. + AXP_DESC(AXP20X, LDO3, "ldo3", "ldo3in", 700, 3500, 25,
  115. + AXP20X_LDO3_V_OUT, 0x7f, AXP20X_PWR_OUT_CTRL, 0x40),
  116. + AXP_DESC_TABLE(AXP20X, LDO4, "ldo4", "ldo24in", axp20x_ldo4_data,
  117. + AXP20X_LDO24_V_OUT, 0x0f, AXP20X_PWR_OUT_CTRL, 0x08),
  118. + AXP_DESC_IO(AXP20X, LDO5, "ldo5", "ldo5in", 1800, 3300, 100,
  119. + AXP20X_LDO5_V_OUT, 0xf0, AXP20X_GPIO0_CTRL, 0x07,
  120. + AXP20X_IO_ENABLED, AXP20X_IO_DISABLED),
  121. };
  122. static int axp20x_set_dcdc_freq(struct platform_device *pdev, u32 dcdcfreq)
  123. {
  124. struct axp20x_dev *axp20x = dev_get_drvdata(pdev->dev.parent);
  125. + u32 min, max, def, step;
  126. - if (dcdcfreq < 750) {
  127. - dcdcfreq = 750;
  128. - dev_warn(&pdev->dev, "DCDC frequency too low. Set to 750kHz\n");
  129. + switch (axp20x->variant) {
  130. + case AXP202_ID:
  131. + case AXP209_ID:
  132. + min = 750;
  133. + max = 1875;
  134. + def = 1500;
  135. + step = 75;
  136. + break;
  137. + default:
  138. + dev_err(&pdev->dev,
  139. + "Setting DCDC frequency for unsupported AXP variant\n");
  140. + return -EINVAL;
  141. }
  142. - if (dcdcfreq > 1875) {
  143. - dcdcfreq = 1875;
  144. - dev_warn(&pdev->dev, "DCDC frequency too high. Set to 1875kHz\n");
  145. + if (dcdcfreq == 0)
  146. + dcdcfreq = def;
  147. +
  148. + if (dcdcfreq < min) {
  149. + dcdcfreq = min;
  150. + dev_warn(&pdev->dev, "DCDC frequency too low. Set to %ukHz\n",
  151. + min);
  152. }
  153. - dcdcfreq = (dcdcfreq - 750) / 75;
  154. + if (dcdcfreq > max) {
  155. + dcdcfreq = max;
  156. + dev_warn(&pdev->dev, "DCDC frequency too high. Set to %ukHz\n",
  157. + max);
  158. + }
  159. +
  160. + dcdcfreq = (dcdcfreq - min) / step;
  161. return regmap_update_bits(axp20x->regmap, AXP20X_DCDC_FREQ,
  162. AXP20X_FREQ_DCDC_MASK, dcdcfreq);
  163. @@ -176,7 +196,7 @@ static int axp20x_regulator_parse_dt(str
  164. {
  165. struct device_node *np, *regulators;
  166. int ret;
  167. - u32 dcdcfreq;
  168. + u32 dcdcfreq = 0;
  169. np = of_node_get(pdev->dev.parent->of_node);
  170. if (!np)
  171. @@ -186,7 +206,6 @@ static int axp20x_regulator_parse_dt(str
  172. if (!regulators) {
  173. dev_warn(&pdev->dev, "regulators node not found\n");
  174. } else {
  175. - dcdcfreq = 1500;
  176. of_property_read_u32(regulators, "x-powers,dcdc-freq", &dcdcfreq);
  177. ret = axp20x_set_dcdc_freq(pdev, dcdcfreq);
  178. if (ret < 0) {
  179. @@ -202,15 +221,27 @@ static int axp20x_regulator_parse_dt(str
  180. static int axp20x_set_dcdc_workmode(struct regulator_dev *rdev, int id, u32 workmode)
  181. {
  182. - unsigned int mask = AXP20X_WORKMODE_DCDC2_MASK;
  183. + struct axp20x_dev *axp20x = rdev_get_drvdata(rdev);
  184. + unsigned int mask;
  185. - if ((id != AXP20X_DCDC2) && (id != AXP20X_DCDC3))
  186. + switch (axp20x->variant) {
  187. + case AXP202_ID:
  188. + case AXP209_ID:
  189. + if ((id != AXP20X_DCDC2) && (id != AXP20X_DCDC3))
  190. + return -EINVAL;
  191. +
  192. + mask = AXP20X_WORKMODE_DCDC2_MASK;
  193. + if (id == AXP20X_DCDC3)
  194. + mask = AXP20X_WORKMODE_DCDC3_MASK;
  195. +
  196. + workmode <<= ffs(mask) - 1;
  197. + break;
  198. +
  199. + default:
  200. + /* should not happen */
  201. + WARN_ON(1);
  202. return -EINVAL;
  203. -
  204. - if (id == AXP20X_DCDC3)
  205. - mask = AXP20X_WORKMODE_DCDC3_MASK;
  206. -
  207. - workmode <<= ffs(mask) - 1;
  208. + }
  209. return regmap_update_bits(rdev->regmap, AXP20X_DCDC_MODE, mask, workmode);
  210. }
  211. @@ -219,22 +250,36 @@ static int axp20x_regulator_probe(struct
  212. {
  213. struct regulator_dev *rdev;
  214. struct axp20x_dev *axp20x = dev_get_drvdata(pdev->dev.parent);
  215. + const struct regulator_desc *regulators;
  216. struct regulator_config config = {
  217. .dev = pdev->dev.parent,
  218. .regmap = axp20x->regmap,
  219. + .driver_data = axp20x,
  220. };
  221. - int ret, i;
  222. + int ret, i, nregulators;
  223. u32 workmode;
  224. + switch (axp20x->variant) {
  225. + case AXP202_ID:
  226. + case AXP209_ID:
  227. + regulators = axp20x_regulators;
  228. + nregulators = AXP20X_REG_ID_MAX;
  229. + break;
  230. + default:
  231. + dev_err(&pdev->dev, "Unsupported AXP variant: %ld\n",
  232. + axp20x->variant);
  233. + return -EINVAL;
  234. + }
  235. +
  236. /* This only sets the dcdc freq. Ignore any errors */
  237. axp20x_regulator_parse_dt(pdev);
  238. - for (i = 0; i < AXP20X_REG_ID_MAX; i++) {
  239. - rdev = devm_regulator_register(&pdev->dev, &axp20x_regulators[i],
  240. + for (i = 0; i < nregulators; i++) {
  241. + rdev = devm_regulator_register(&pdev->dev, &regulators[i],
  242. &config);
  243. if (IS_ERR(rdev)) {
  244. dev_err(&pdev->dev, "Failed to register %s\n",
  245. - axp20x_regulators[i].name);
  246. + regulators[i].name);
  247. return PTR_ERR(rdev);
  248. }
  249. @@ -245,7 +290,7 @@ static int axp20x_regulator_probe(struct
  250. if (!ret) {
  251. if (axp20x_set_dcdc_workmode(rdev, i, workmode))
  252. dev_err(&pdev->dev, "Failed to set workmode on %s\n",
  253. - axp20x_regulators[i].name);
  254. + rdev->desc->name);
  255. }
  256. }