bcm590xx-regulator.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Broadcom BCM590xx regulator driver
  4. *
  5. * Copyright 2014 Linaro Limited
  6. * Author: Matt Porter <mporter@linaro.org>
  7. */
  8. #include <linux/err.h>
  9. #include <linux/init.h>
  10. #include <linux/kernel.h>
  11. #include <linux/mfd/bcm590xx.h>
  12. #include <linux/module.h>
  13. #include <linux/of.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/regulator/driver.h>
  16. #include <linux/regulator/machine.h>
  17. #include <linux/regulator/of_regulator.h>
  18. #include <linux/slab.h>
  19. /* I2C slave 0 registers */
  20. #define BCM590XX_RFLDOPMCTRL1 0x60
  21. #define BCM590XX_IOSR1PMCTRL1 0x7a
  22. #define BCM590XX_IOSR2PMCTRL1 0x7c
  23. #define BCM590XX_CSRPMCTRL1 0x7e
  24. #define BCM590XX_SDSR1PMCTRL1 0x82
  25. #define BCM590XX_SDSR2PMCTRL1 0x86
  26. #define BCM590XX_MSRPMCTRL1 0x8a
  27. #define BCM590XX_VSRPMCTRL1 0x8e
  28. #define BCM590XX_RFLDOCTRL 0x96
  29. #define BCM590XX_CSRVOUT1 0xc0
  30. /* I2C slave 1 registers */
  31. #define BCM590XX_GPLDO5PMCTRL1 0x16
  32. #define BCM590XX_GPLDO6PMCTRL1 0x18
  33. #define BCM590XX_GPLDO1CTRL 0x1a
  34. #define BCM590XX_GPLDO2CTRL 0x1b
  35. #define BCM590XX_GPLDO3CTRL 0x1c
  36. #define BCM590XX_GPLDO4CTRL 0x1d
  37. #define BCM590XX_GPLDO5CTRL 0x1e
  38. #define BCM590XX_GPLDO6CTRL 0x1f
  39. #define BCM590XX_OTG_CTRL 0x40
  40. #define BCM590XX_GPLDO1PMCTRL1 0x57
  41. #define BCM590XX_GPLDO2PMCTRL1 0x59
  42. #define BCM590XX_GPLDO3PMCTRL1 0x5b
  43. #define BCM590XX_GPLDO4PMCTRL1 0x5d
  44. #define BCM590XX_REG_ENABLE BIT(7)
  45. #define BCM590XX_VBUS_ENABLE BIT(2)
  46. #define BCM590XX_LDO_VSEL_MASK GENMASK(5, 3)
  47. #define BCM590XX_SR_VSEL_MASK GENMASK(5, 0)
  48. /*
  49. * RFLDO to VSR regulators are
  50. * accessed via I2C slave 0
  51. */
  52. /* LDO regulator IDs */
  53. #define BCM590XX_REG_RFLDO 0
  54. #define BCM590XX_REG_CAMLDO1 1
  55. #define BCM590XX_REG_CAMLDO2 2
  56. #define BCM590XX_REG_SIMLDO1 3
  57. #define BCM590XX_REG_SIMLDO2 4
  58. #define BCM590XX_REG_SDLDO 5
  59. #define BCM590XX_REG_SDXLDO 6
  60. #define BCM590XX_REG_MMCLDO1 7
  61. #define BCM590XX_REG_MMCLDO2 8
  62. #define BCM590XX_REG_AUDLDO 9
  63. #define BCM590XX_REG_MICLDO 10
  64. #define BCM590XX_REG_USBLDO 11
  65. #define BCM590XX_REG_VIBLDO 12
  66. /* DCDC regulator IDs */
  67. #define BCM590XX_REG_CSR 13
  68. #define BCM590XX_REG_IOSR1 14
  69. #define BCM590XX_REG_IOSR2 15
  70. #define BCM590XX_REG_MSR 16
  71. #define BCM590XX_REG_SDSR1 17
  72. #define BCM590XX_REG_SDSR2 18
  73. #define BCM590XX_REG_VSR 19
  74. /*
  75. * GPLDO1 to VBUS regulators are
  76. * accessed via I2C slave 1
  77. */
  78. #define BCM590XX_REG_GPLDO1 20
  79. #define BCM590XX_REG_GPLDO2 21
  80. #define BCM590XX_REG_GPLDO3 22
  81. #define BCM590XX_REG_GPLDO4 23
  82. #define BCM590XX_REG_GPLDO5 24
  83. #define BCM590XX_REG_GPLDO6 25
  84. #define BCM590XX_REG_VBUS 26
  85. #define BCM590XX_NUM_REGS 27
  86. #define BCM590XX_REG_IS_LDO(n) (n < BCM590XX_REG_CSR)
  87. #define BCM590XX_REG_IS_GPLDO(n) \
  88. ((n > BCM590XX_REG_VSR) && (n < BCM590XX_REG_VBUS))
  89. #define BCM590XX_REG_IS_VBUS(n) (n == BCM590XX_REG_VBUS)
  90. /* LDO group A: supported voltages in microvolts */
  91. static const unsigned int ldo_a_table[] = {
  92. 1200000, 1800000, 2500000, 2700000, 2800000,
  93. 2900000, 3000000, 3300000,
  94. };
  95. /* LDO group C: supported voltages in microvolts */
  96. static const unsigned int ldo_c_table[] = {
  97. 3100000, 1800000, 2500000, 2700000, 2800000,
  98. 2900000, 3000000, 3300000,
  99. };
  100. static const unsigned int ldo_vbus[] = {
  101. 5000000,
  102. };
  103. /* DCDC group CSR: supported voltages in microvolts */
  104. static const struct regulator_linear_range dcdc_csr_ranges[] = {
  105. REGULATOR_LINEAR_RANGE(860000, 2, 50, 10000),
  106. REGULATOR_LINEAR_RANGE(1360000, 51, 55, 20000),
  107. REGULATOR_LINEAR_RANGE(900000, 56, 63, 0),
  108. };
  109. /* DCDC group IOSR1: supported voltages in microvolts */
  110. static const struct regulator_linear_range dcdc_iosr1_ranges[] = {
  111. REGULATOR_LINEAR_RANGE(860000, 2, 51, 10000),
  112. REGULATOR_LINEAR_RANGE(1500000, 52, 52, 0),
  113. REGULATOR_LINEAR_RANGE(1800000, 53, 53, 0),
  114. REGULATOR_LINEAR_RANGE(900000, 54, 63, 0),
  115. };
  116. /* DCDC group SDSR1: supported voltages in microvolts */
  117. static const struct regulator_linear_range dcdc_sdsr1_ranges[] = {
  118. REGULATOR_LINEAR_RANGE(860000, 2, 50, 10000),
  119. REGULATOR_LINEAR_RANGE(1340000, 51, 51, 0),
  120. REGULATOR_LINEAR_RANGE(900000, 52, 63, 0),
  121. };
  122. struct bcm590xx_info {
  123. const char *name;
  124. const char *vin_name;
  125. u8 n_voltages;
  126. const unsigned int *volt_table;
  127. u8 n_linear_ranges;
  128. const struct regulator_linear_range *linear_ranges;
  129. };
  130. #define BCM590XX_REG_TABLE(_name, _table) \
  131. { \
  132. .name = #_name, \
  133. .n_voltages = ARRAY_SIZE(_table), \
  134. .volt_table = _table, \
  135. }
  136. #define BCM590XX_REG_RANGES(_name, _ranges) \
  137. { \
  138. .name = #_name, \
  139. .n_voltages = 64, \
  140. .n_linear_ranges = ARRAY_SIZE(_ranges), \
  141. .linear_ranges = _ranges, \
  142. }
  143. static struct bcm590xx_info bcm590xx_regs[] = {
  144. BCM590XX_REG_TABLE(rfldo, ldo_a_table),
  145. BCM590XX_REG_TABLE(camldo1, ldo_c_table),
  146. BCM590XX_REG_TABLE(camldo2, ldo_c_table),
  147. BCM590XX_REG_TABLE(simldo1, ldo_a_table),
  148. BCM590XX_REG_TABLE(simldo2, ldo_a_table),
  149. BCM590XX_REG_TABLE(sdldo, ldo_c_table),
  150. BCM590XX_REG_TABLE(sdxldo, ldo_a_table),
  151. BCM590XX_REG_TABLE(mmcldo1, ldo_a_table),
  152. BCM590XX_REG_TABLE(mmcldo2, ldo_a_table),
  153. BCM590XX_REG_TABLE(audldo, ldo_a_table),
  154. BCM590XX_REG_TABLE(micldo, ldo_a_table),
  155. BCM590XX_REG_TABLE(usbldo, ldo_a_table),
  156. BCM590XX_REG_TABLE(vibldo, ldo_c_table),
  157. BCM590XX_REG_RANGES(csr, dcdc_csr_ranges),
  158. BCM590XX_REG_RANGES(iosr1, dcdc_iosr1_ranges),
  159. BCM590XX_REG_RANGES(iosr2, dcdc_iosr1_ranges),
  160. BCM590XX_REG_RANGES(msr, dcdc_iosr1_ranges),
  161. BCM590XX_REG_RANGES(sdsr1, dcdc_sdsr1_ranges),
  162. BCM590XX_REG_RANGES(sdsr2, dcdc_iosr1_ranges),
  163. BCM590XX_REG_RANGES(vsr, dcdc_iosr1_ranges),
  164. BCM590XX_REG_TABLE(gpldo1, ldo_a_table),
  165. BCM590XX_REG_TABLE(gpldo2, ldo_a_table),
  166. BCM590XX_REG_TABLE(gpldo3, ldo_a_table),
  167. BCM590XX_REG_TABLE(gpldo4, ldo_a_table),
  168. BCM590XX_REG_TABLE(gpldo5, ldo_a_table),
  169. BCM590XX_REG_TABLE(gpldo6, ldo_a_table),
  170. BCM590XX_REG_TABLE(vbus, ldo_vbus),
  171. };
  172. struct bcm590xx_reg {
  173. struct regulator_desc *desc;
  174. struct bcm590xx *mfd;
  175. };
  176. static int bcm590xx_get_vsel_register(int id)
  177. {
  178. if (BCM590XX_REG_IS_LDO(id))
  179. return BCM590XX_RFLDOCTRL + id;
  180. else if (BCM590XX_REG_IS_GPLDO(id))
  181. return BCM590XX_GPLDO1CTRL + id;
  182. else
  183. return BCM590XX_CSRVOUT1 + (id - BCM590XX_REG_CSR) * 3;
  184. }
  185. static int bcm590xx_get_enable_register(int id)
  186. {
  187. int reg = 0;
  188. if (BCM590XX_REG_IS_LDO(id))
  189. reg = BCM590XX_RFLDOPMCTRL1 + id * 2;
  190. else if (BCM590XX_REG_IS_GPLDO(id))
  191. reg = BCM590XX_GPLDO1PMCTRL1 + id * 2;
  192. else
  193. switch (id) {
  194. case BCM590XX_REG_CSR:
  195. reg = BCM590XX_CSRPMCTRL1;
  196. break;
  197. case BCM590XX_REG_IOSR1:
  198. reg = BCM590XX_IOSR1PMCTRL1;
  199. break;
  200. case BCM590XX_REG_IOSR2:
  201. reg = BCM590XX_IOSR2PMCTRL1;
  202. break;
  203. case BCM590XX_REG_MSR:
  204. reg = BCM590XX_MSRPMCTRL1;
  205. break;
  206. case BCM590XX_REG_SDSR1:
  207. reg = BCM590XX_SDSR1PMCTRL1;
  208. break;
  209. case BCM590XX_REG_SDSR2:
  210. reg = BCM590XX_SDSR2PMCTRL1;
  211. break;
  212. case BCM590XX_REG_VSR:
  213. reg = BCM590XX_VSRPMCTRL1;
  214. break;
  215. case BCM590XX_REG_VBUS:
  216. reg = BCM590XX_OTG_CTRL;
  217. break;
  218. }
  219. return reg;
  220. }
  221. static const struct regulator_ops bcm590xx_ops_ldo = {
  222. .is_enabled = regulator_is_enabled_regmap,
  223. .enable = regulator_enable_regmap,
  224. .disable = regulator_disable_regmap,
  225. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  226. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  227. .list_voltage = regulator_list_voltage_table,
  228. .map_voltage = regulator_map_voltage_iterate,
  229. };
  230. static const struct regulator_ops bcm590xx_ops_dcdc = {
  231. .is_enabled = regulator_is_enabled_regmap,
  232. .enable = regulator_enable_regmap,
  233. .disable = regulator_disable_regmap,
  234. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  235. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  236. .list_voltage = regulator_list_voltage_linear_range,
  237. .map_voltage = regulator_map_voltage_linear_range,
  238. };
  239. static const struct regulator_ops bcm590xx_ops_vbus = {
  240. .is_enabled = regulator_is_enabled_regmap,
  241. .enable = regulator_enable_regmap,
  242. .disable = regulator_disable_regmap,
  243. };
  244. static int bcm590xx_probe(struct platform_device *pdev)
  245. {
  246. struct bcm590xx *bcm590xx = dev_get_drvdata(pdev->dev.parent);
  247. struct bcm590xx_reg *pmu;
  248. struct regulator_config config = { };
  249. struct bcm590xx_info *info;
  250. struct regulator_dev *rdev;
  251. int i;
  252. pmu = devm_kzalloc(&pdev->dev, sizeof(*pmu), GFP_KERNEL);
  253. if (!pmu)
  254. return -ENOMEM;
  255. pmu->mfd = bcm590xx;
  256. platform_set_drvdata(pdev, pmu);
  257. pmu->desc = devm_kcalloc(&pdev->dev,
  258. BCM590XX_NUM_REGS,
  259. sizeof(struct regulator_desc),
  260. GFP_KERNEL);
  261. if (!pmu->desc)
  262. return -ENOMEM;
  263. info = bcm590xx_regs;
  264. for (i = 0; i < BCM590XX_NUM_REGS; i++, info++) {
  265. /* Register the regulators */
  266. pmu->desc[i].name = info->name;
  267. pmu->desc[i].of_match = of_match_ptr(info->name);
  268. pmu->desc[i].regulators_node = of_match_ptr("regulators");
  269. pmu->desc[i].supply_name = info->vin_name;
  270. pmu->desc[i].id = i;
  271. pmu->desc[i].volt_table = info->volt_table;
  272. pmu->desc[i].n_voltages = info->n_voltages;
  273. pmu->desc[i].linear_ranges = info->linear_ranges;
  274. pmu->desc[i].n_linear_ranges = info->n_linear_ranges;
  275. if ((BCM590XX_REG_IS_LDO(i)) || (BCM590XX_REG_IS_GPLDO(i))) {
  276. pmu->desc[i].ops = &bcm590xx_ops_ldo;
  277. pmu->desc[i].vsel_mask = BCM590XX_LDO_VSEL_MASK;
  278. } else if (BCM590XX_REG_IS_VBUS(i))
  279. pmu->desc[i].ops = &bcm590xx_ops_vbus;
  280. else {
  281. pmu->desc[i].ops = &bcm590xx_ops_dcdc;
  282. pmu->desc[i].vsel_mask = BCM590XX_SR_VSEL_MASK;
  283. }
  284. if (BCM590XX_REG_IS_VBUS(i))
  285. pmu->desc[i].enable_mask = BCM590XX_VBUS_ENABLE;
  286. else {
  287. pmu->desc[i].vsel_reg = bcm590xx_get_vsel_register(i);
  288. pmu->desc[i].enable_is_inverted = true;
  289. pmu->desc[i].enable_mask = BCM590XX_REG_ENABLE;
  290. }
  291. pmu->desc[i].enable_reg = bcm590xx_get_enable_register(i);
  292. pmu->desc[i].type = REGULATOR_VOLTAGE;
  293. pmu->desc[i].owner = THIS_MODULE;
  294. config.dev = bcm590xx->dev;
  295. config.driver_data = pmu;
  296. if (BCM590XX_REG_IS_GPLDO(i) || BCM590XX_REG_IS_VBUS(i))
  297. config.regmap = bcm590xx->regmap_sec;
  298. else
  299. config.regmap = bcm590xx->regmap_pri;
  300. rdev = devm_regulator_register(&pdev->dev, &pmu->desc[i],
  301. &config);
  302. if (IS_ERR(rdev)) {
  303. dev_err(bcm590xx->dev,
  304. "failed to register %s regulator\n",
  305. pdev->name);
  306. return PTR_ERR(rdev);
  307. }
  308. }
  309. return 0;
  310. }
  311. static struct platform_driver bcm590xx_regulator_driver = {
  312. .driver = {
  313. .name = "bcm590xx-vregs",
  314. },
  315. .probe = bcm590xx_probe,
  316. };
  317. module_platform_driver(bcm590xx_regulator_driver);
  318. MODULE_AUTHOR("Matt Porter <mporter@linaro.org>");
  319. MODULE_DESCRIPTION("BCM590xx voltage regulator driver");
  320. MODULE_LICENSE("GPL v2");
  321. MODULE_ALIAS("platform:bcm590xx-vregs");