bcm590xx-regulator.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. /*
  2. * Broadcom BCM590xx regulator driver
  3. *
  4. * Copyright 2014 Linaro Limited
  5. * Author: Matt Porter <[email protected]>
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the
  9. * Free Software Foundation; either version 2 of the License, or (at your
  10. * option) any later version.
  11. */
  12. #include <linux/err.h>
  13. #include <linux/init.h>
  14. #include <linux/kernel.h>
  15. #include <linux/mfd/bcm590xx.h>
  16. #include <linux/module.h>
  17. #include <linux/of.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/regulator/driver.h>
  20. #include <linux/regulator/machine.h>
  21. #include <linux/regulator/of_regulator.h>
  22. #include <linux/slab.h>
  23. /* I2C slave 0 registers */
  24. #define BCM590XX_RFLDOPMCTRL1 0x60
  25. #define BCM590XX_IOSR1PMCTRL1 0x7a
  26. #define BCM590XX_IOSR2PMCTRL1 0x7c
  27. #define BCM590XX_CSRPMCTRL1 0x7e
  28. #define BCM590XX_SDSR1PMCTRL1 0x82
  29. #define BCM590XX_SDSR2PMCTRL1 0x86
  30. #define BCM590XX_MSRPMCTRL1 0x8a
  31. #define BCM590XX_VSRPMCTRL1 0x8e
  32. #define BCM590XX_RFLDOCTRL 0x96
  33. #define BCM590XX_CSRVOUT1 0xc0
  34. /* I2C slave 1 registers */
  35. #define BCM590XX_GPLDO5PMCTRL1 0x16
  36. #define BCM590XX_GPLDO6PMCTRL1 0x18
  37. #define BCM590XX_GPLDO1CTRL 0x1a
  38. #define BCM590XX_GPLDO2CTRL 0x1b
  39. #define BCM590XX_GPLDO3CTRL 0x1c
  40. #define BCM590XX_GPLDO4CTRL 0x1d
  41. #define BCM590XX_GPLDO5CTRL 0x1e
  42. #define BCM590XX_GPLDO6CTRL 0x1f
  43. #define BCM590XX_OTG_CTRL 0x40
  44. #define BCM590XX_GPLDO1PMCTRL1 0x57
  45. #define BCM590XX_GPLDO2PMCTRL1 0x59
  46. #define BCM590XX_GPLDO3PMCTRL1 0x5b
  47. #define BCM590XX_GPLDO4PMCTRL1 0x5d
  48. #define BCM590XX_REG_ENABLE BIT(7)
  49. #define BCM590XX_VBUS_ENABLE BIT(2)
  50. #define BCM590XX_LDO_VSEL_MASK GENMASK(5, 3)
  51. #define BCM590XX_SR_VSEL_MASK GENMASK(5, 0)
  52. /*
  53. * RFLDO to VSR regulators are
  54. * accessed via I2C slave 0
  55. */
  56. /* LDO regulator IDs */
  57. #define BCM590XX_REG_RFLDO 0
  58. #define BCM590XX_REG_CAMLDO1 1
  59. #define BCM590XX_REG_CAMLDO2 2
  60. #define BCM590XX_REG_SIMLDO1 3
  61. #define BCM590XX_REG_SIMLDO2 4
  62. #define BCM590XX_REG_SDLDO 5
  63. #define BCM590XX_REG_SDXLDO 6
  64. #define BCM590XX_REG_MMCLDO1 7
  65. #define BCM590XX_REG_MMCLDO2 8
  66. #define BCM590XX_REG_AUDLDO 9
  67. #define BCM590XX_REG_MICLDO 10
  68. #define BCM590XX_REG_USBLDO 11
  69. #define BCM590XX_REG_VIBLDO 12
  70. /* DCDC regulator IDs */
  71. #define BCM590XX_REG_CSR 13
  72. #define BCM590XX_REG_IOSR1 14
  73. #define BCM590XX_REG_IOSR2 15
  74. #define BCM590XX_REG_MSR 16
  75. #define BCM590XX_REG_SDSR1 17
  76. #define BCM590XX_REG_SDSR2 18
  77. #define BCM590XX_REG_VSR 19
  78. /*
  79. * GPLDO1 to VBUS regulators are
  80. * accessed via I2C slave 1
  81. */
  82. #define BCM590XX_REG_GPLDO1 20
  83. #define BCM590XX_REG_GPLDO2 21
  84. #define BCM590XX_REG_GPLDO3 22
  85. #define BCM590XX_REG_GPLDO4 23
  86. #define BCM590XX_REG_GPLDO5 24
  87. #define BCM590XX_REG_GPLDO6 25
  88. #define BCM590XX_REG_VBUS 26
  89. #define BCM590XX_NUM_REGS 27
  90. #define BCM590XX_REG_IS_LDO(n) (n < BCM590XX_REG_CSR)
  91. #define BCM590XX_REG_IS_GPLDO(n) \
  92. ((n > BCM590XX_REG_VSR) && (n < BCM590XX_REG_VBUS))
  93. #define BCM590XX_REG_IS_VBUS(n) (n == BCM590XX_REG_VBUS)
  94. struct bcm590xx_board {
  95. struct regulator_init_data *bcm590xx_pmu_init_data[BCM590XX_NUM_REGS];
  96. };
  97. /* LDO group A: supported voltages in microvolts */
  98. static const unsigned int ldo_a_table[] = {
  99. 1200000, 1800000, 2500000, 2700000, 2800000,
  100. 2900000, 3000000, 3300000,
  101. };
  102. /* LDO group C: supported voltages in microvolts */
  103. static const unsigned int ldo_c_table[] = {
  104. 3100000, 1800000, 2500000, 2700000, 2800000,
  105. 2900000, 3000000, 3300000,
  106. };
  107. static const unsigned int ldo_vbus[] = {
  108. 5000000,
  109. };
  110. /* DCDC group CSR: supported voltages in microvolts */
  111. static const struct regulator_linear_range dcdc_csr_ranges[] = {
  112. REGULATOR_LINEAR_RANGE(860000, 2, 50, 10000),
  113. REGULATOR_LINEAR_RANGE(1360000, 51, 55, 20000),
  114. REGULATOR_LINEAR_RANGE(900000, 56, 63, 0),
  115. };
  116. /* DCDC group IOSR1: supported voltages in microvolts */
  117. static const struct regulator_linear_range dcdc_iosr1_ranges[] = {
  118. REGULATOR_LINEAR_RANGE(860000, 2, 51, 10000),
  119. REGULATOR_LINEAR_RANGE(1500000, 52, 52, 0),
  120. REGULATOR_LINEAR_RANGE(1800000, 53, 53, 0),
  121. REGULATOR_LINEAR_RANGE(900000, 54, 63, 0),
  122. };
  123. /* DCDC group SDSR1: supported voltages in microvolts */
  124. static const struct regulator_linear_range dcdc_sdsr1_ranges[] = {
  125. REGULATOR_LINEAR_RANGE(860000, 2, 50, 10000),
  126. REGULATOR_LINEAR_RANGE(1340000, 51, 51, 0),
  127. REGULATOR_LINEAR_RANGE(900000, 52, 63, 0),
  128. };
  129. struct bcm590xx_info {
  130. const char *name;
  131. const char *vin_name;
  132. u8 n_voltages;
  133. const unsigned int *volt_table;
  134. u8 n_linear_ranges;
  135. const struct regulator_linear_range *linear_ranges;
  136. };
  137. #define BCM590XX_REG_TABLE(_name, _table) \
  138. { \
  139. .name = #_name, \
  140. .n_voltages = ARRAY_SIZE(_table), \
  141. .volt_table = _table, \
  142. }
  143. #define BCM590XX_REG_RANGES(_name, _ranges) \
  144. { \
  145. .name = #_name, \
  146. .n_voltages = 64, \
  147. .n_linear_ranges = ARRAY_SIZE(_ranges), \
  148. .linear_ranges = _ranges, \
  149. }
  150. static struct bcm590xx_info bcm590xx_regs[] = {
  151. BCM590XX_REG_TABLE(rfldo, ldo_a_table),
  152. BCM590XX_REG_TABLE(camldo1, ldo_c_table),
  153. BCM590XX_REG_TABLE(camldo2, ldo_c_table),
  154. BCM590XX_REG_TABLE(simldo1, ldo_a_table),
  155. BCM590XX_REG_TABLE(simldo2, ldo_a_table),
  156. BCM590XX_REG_TABLE(sdldo, ldo_c_table),
  157. BCM590XX_REG_TABLE(sdxldo, ldo_a_table),
  158. BCM590XX_REG_TABLE(mmcldo1, ldo_a_table),
  159. BCM590XX_REG_TABLE(mmcldo2, ldo_a_table),
  160. BCM590XX_REG_TABLE(audldo, ldo_a_table),
  161. BCM590XX_REG_TABLE(micldo, ldo_a_table),
  162. BCM590XX_REG_TABLE(usbldo, ldo_a_table),
  163. BCM590XX_REG_TABLE(vibldo, ldo_c_table),
  164. BCM590XX_REG_RANGES(csr, dcdc_csr_ranges),
  165. BCM590XX_REG_RANGES(iosr1, dcdc_iosr1_ranges),
  166. BCM590XX_REG_RANGES(iosr2, dcdc_iosr1_ranges),
  167. BCM590XX_REG_RANGES(msr, dcdc_iosr1_ranges),
  168. BCM590XX_REG_RANGES(sdsr1, dcdc_sdsr1_ranges),
  169. BCM590XX_REG_RANGES(sdsr2, dcdc_iosr1_ranges),
  170. BCM590XX_REG_RANGES(vsr, dcdc_iosr1_ranges),
  171. BCM590XX_REG_TABLE(gpldo1, ldo_a_table),
  172. BCM590XX_REG_TABLE(gpldo2, ldo_a_table),
  173. BCM590XX_REG_TABLE(gpldo3, ldo_a_table),
  174. BCM590XX_REG_TABLE(gpldo4, ldo_a_table),
  175. BCM590XX_REG_TABLE(gpldo5, ldo_a_table),
  176. BCM590XX_REG_TABLE(gpldo6, ldo_a_table),
  177. BCM590XX_REG_TABLE(vbus, ldo_vbus),
  178. };
  179. struct bcm590xx_reg {
  180. struct regulator_desc *desc;
  181. struct bcm590xx *mfd;
  182. };
  183. static int bcm590xx_get_vsel_register(int id)
  184. {
  185. if (BCM590XX_REG_IS_LDO(id))
  186. return BCM590XX_RFLDOCTRL + id;
  187. else if (BCM590XX_REG_IS_GPLDO(id))
  188. return BCM590XX_GPLDO1CTRL + id;
  189. else
  190. return BCM590XX_CSRVOUT1 + (id - BCM590XX_REG_CSR) * 3;
  191. }
  192. static int bcm590xx_get_enable_register(int id)
  193. {
  194. int reg = 0;
  195. if (BCM590XX_REG_IS_LDO(id))
  196. reg = BCM590XX_RFLDOPMCTRL1 + id * 2;
  197. else if (BCM590XX_REG_IS_GPLDO(id))
  198. reg = BCM590XX_GPLDO1PMCTRL1 + id * 2;
  199. else
  200. switch (id) {
  201. case BCM590XX_REG_CSR:
  202. reg = BCM590XX_CSRPMCTRL1;
  203. break;
  204. case BCM590XX_REG_IOSR1:
  205. reg = BCM590XX_IOSR1PMCTRL1;
  206. break;
  207. case BCM590XX_REG_IOSR2:
  208. reg = BCM590XX_IOSR2PMCTRL1;
  209. break;
  210. case BCM590XX_REG_MSR:
  211. reg = BCM590XX_MSRPMCTRL1;
  212. break;
  213. case BCM590XX_REG_SDSR1:
  214. reg = BCM590XX_SDSR1PMCTRL1;
  215. break;
  216. case BCM590XX_REG_SDSR2:
  217. reg = BCM590XX_SDSR2PMCTRL1;
  218. break;
  219. case BCM590XX_REG_VBUS:
  220. reg = BCM590XX_OTG_CTRL;
  221. }
  222. return reg;
  223. }
  224. static struct regulator_ops bcm590xx_ops_ldo = {
  225. .is_enabled = regulator_is_enabled_regmap,
  226. .enable = regulator_enable_regmap,
  227. .disable = regulator_disable_regmap,
  228. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  229. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  230. .list_voltage = regulator_list_voltage_table,
  231. .map_voltage = regulator_map_voltage_iterate,
  232. };
  233. static struct regulator_ops bcm590xx_ops_dcdc = {
  234. .is_enabled = regulator_is_enabled_regmap,
  235. .enable = regulator_enable_regmap,
  236. .disable = regulator_disable_regmap,
  237. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  238. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  239. .list_voltage = regulator_list_voltage_linear_range,
  240. .map_voltage = regulator_map_voltage_linear_range,
  241. };
  242. static struct regulator_ops bcm590xx_ops_vbus = {
  243. .is_enabled = regulator_is_enabled_regmap,
  244. .enable = regulator_enable_regmap,
  245. .disable = regulator_disable_regmap,
  246. };
  247. #define BCM590XX_MATCH(_name, _id) \
  248. { \
  249. .name = #_name, \
  250. .driver_data = (void *)&bcm590xx_regs[BCM590XX_REG_##_id], \
  251. }
  252. static struct of_regulator_match bcm590xx_matches[] = {
  253. BCM590XX_MATCH(rfldo, RFLDO),
  254. BCM590XX_MATCH(camldo1, CAMLDO1),
  255. BCM590XX_MATCH(camldo2, CAMLDO2),
  256. BCM590XX_MATCH(simldo1, SIMLDO1),
  257. BCM590XX_MATCH(simldo2, SIMLDO2),
  258. BCM590XX_MATCH(sdldo, SDLDO),
  259. BCM590XX_MATCH(sdxldo, SDXLDO),
  260. BCM590XX_MATCH(mmcldo1, MMCLDO1),
  261. BCM590XX_MATCH(mmcldo2, MMCLDO2),
  262. BCM590XX_MATCH(audldo, AUDLDO),
  263. BCM590XX_MATCH(micldo, MICLDO),
  264. BCM590XX_MATCH(usbldo, USBLDO),
  265. BCM590XX_MATCH(vibldo, VIBLDO),
  266. BCM590XX_MATCH(csr, CSR),
  267. BCM590XX_MATCH(iosr1, IOSR1),
  268. BCM590XX_MATCH(iosr2, IOSR2),
  269. BCM590XX_MATCH(msr, MSR),
  270. BCM590XX_MATCH(sdsr1, SDSR1),
  271. BCM590XX_MATCH(sdsr2, SDSR2),
  272. BCM590XX_MATCH(vsr, VSR),
  273. BCM590XX_MATCH(gpldo1, GPLDO1),
  274. BCM590XX_MATCH(gpldo2, GPLDO2),
  275. BCM590XX_MATCH(gpldo3, GPLDO3),
  276. BCM590XX_MATCH(gpldo4, GPLDO4),
  277. BCM590XX_MATCH(gpldo5, GPLDO5),
  278. BCM590XX_MATCH(gpldo6, GPLDO6),
  279. BCM590XX_MATCH(vbus, VBUS),
  280. };
  281. static struct bcm590xx_board *bcm590xx_parse_dt_reg_data(
  282. struct platform_device *pdev,
  283. struct of_regulator_match **bcm590xx_reg_matches)
  284. {
  285. struct bcm590xx_board *data;
  286. struct device_node *np = pdev->dev.parent->of_node;
  287. struct device_node *regulators;
  288. struct of_regulator_match *matches = bcm590xx_matches;
  289. int count = ARRAY_SIZE(bcm590xx_matches);
  290. int idx = 0;
  291. int ret;
  292. if (!np) {
  293. dev_err(&pdev->dev, "of node not found\n");
  294. return NULL;
  295. }
  296. data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
  297. if (!data)
  298. return NULL;
  299. np = of_node_get(np);
  300. regulators = of_get_child_by_name(np, "regulators");
  301. if (!regulators) {
  302. dev_warn(&pdev->dev, "regulator node not found\n");
  303. return NULL;
  304. }
  305. ret = of_regulator_match(&pdev->dev, regulators, matches, count);
  306. of_node_put(regulators);
  307. if (ret < 0) {
  308. dev_err(&pdev->dev, "Error parsing regulator init data: %d\n",
  309. ret);
  310. return NULL;
  311. }
  312. *bcm590xx_reg_matches = matches;
  313. for (idx = 0; idx < count; idx++) {
  314. if (!matches[idx].init_data || !matches[idx].of_node)
  315. continue;
  316. data->bcm590xx_pmu_init_data[idx] = matches[idx].init_data;
  317. }
  318. return data;
  319. }
  320. static int bcm590xx_probe(struct platform_device *pdev)
  321. {
  322. struct bcm590xx *bcm590xx = dev_get_drvdata(pdev->dev.parent);
  323. struct bcm590xx_board *pmu_data = NULL;
  324. struct bcm590xx_reg *pmu;
  325. struct regulator_config config = { };
  326. struct bcm590xx_info *info;
  327. struct regulator_init_data *reg_data;
  328. struct regulator_dev *rdev;
  329. struct of_regulator_match *bcm590xx_reg_matches = NULL;
  330. int i;
  331. pmu_data = bcm590xx_parse_dt_reg_data(pdev,
  332. &bcm590xx_reg_matches);
  333. pmu = devm_kzalloc(&pdev->dev, sizeof(*pmu), GFP_KERNEL);
  334. if (!pmu)
  335. return -ENOMEM;
  336. pmu->mfd = bcm590xx;
  337. platform_set_drvdata(pdev, pmu);
  338. pmu->desc = devm_kzalloc(&pdev->dev, BCM590XX_NUM_REGS *
  339. sizeof(struct regulator_desc), GFP_KERNEL);
  340. if (!pmu->desc)
  341. return -ENOMEM;
  342. info = bcm590xx_regs;
  343. for (i = 0; i < BCM590XX_NUM_REGS; i++, info++) {
  344. if (pmu_data)
  345. reg_data = pmu_data->bcm590xx_pmu_init_data[i];
  346. else
  347. reg_data = NULL;
  348. /* Register the regulators */
  349. pmu->desc[i].name = info->name;
  350. pmu->desc[i].supply_name = info->vin_name;
  351. pmu->desc[i].id = i;
  352. pmu->desc[i].volt_table = info->volt_table;
  353. pmu->desc[i].n_voltages = info->n_voltages;
  354. pmu->desc[i].linear_ranges = info->linear_ranges;
  355. pmu->desc[i].n_linear_ranges = info->n_linear_ranges;
  356. if ((BCM590XX_REG_IS_LDO(i)) || (BCM590XX_REG_IS_GPLDO(i))) {
  357. pmu->desc[i].ops = &bcm590xx_ops_ldo;
  358. pmu->desc[i].vsel_mask = BCM590XX_LDO_VSEL_MASK;
  359. } else if (BCM590XX_REG_IS_VBUS(i))
  360. pmu->desc[i].ops = &bcm590xx_ops_vbus;
  361. else {
  362. pmu->desc[i].ops = &bcm590xx_ops_dcdc;
  363. pmu->desc[i].vsel_mask = BCM590XX_SR_VSEL_MASK;
  364. }
  365. if (BCM590XX_REG_IS_VBUS(i))
  366. pmu->desc[i].enable_mask = BCM590XX_VBUS_ENABLE;
  367. else {
  368. pmu->desc[i].vsel_reg = bcm590xx_get_vsel_register(i);
  369. pmu->desc[i].enable_is_inverted = true;
  370. pmu->desc[i].enable_mask = BCM590XX_REG_ENABLE;
  371. }
  372. pmu->desc[i].enable_reg = bcm590xx_get_enable_register(i);
  373. pmu->desc[i].type = REGULATOR_VOLTAGE;
  374. pmu->desc[i].owner = THIS_MODULE;
  375. config.dev = bcm590xx->dev;
  376. config.init_data = reg_data;
  377. config.driver_data = pmu;
  378. if (BCM590XX_REG_IS_GPLDO(i) || BCM590XX_REG_IS_VBUS(i))
  379. config.regmap = bcm590xx->regmap_sec;
  380. else
  381. config.regmap = bcm590xx->regmap_pri;
  382. if (bcm590xx_reg_matches)
  383. config.of_node = bcm590xx_reg_matches[i].of_node;
  384. rdev = devm_regulator_register(&pdev->dev, &pmu->desc[i],
  385. &config);
  386. if (IS_ERR(rdev)) {
  387. dev_err(bcm590xx->dev,
  388. "failed to register %s regulator\n",
  389. pdev->name);
  390. return PTR_ERR(rdev);
  391. }
  392. }
  393. return 0;
  394. }
  395. static struct platform_driver bcm590xx_regulator_driver = {
  396. .driver = {
  397. .name = "bcm590xx-vregs",
  398. },
  399. .probe = bcm590xx_probe,
  400. };
  401. module_platform_driver(bcm590xx_regulator_driver);
  402. MODULE_AUTHOR("Matt Porter <[email protected]>");
  403. MODULE_DESCRIPTION("BCM590xx voltage regulator driver");
  404. MODULE_LICENSE("GPL v2");
  405. MODULE_ALIAS("platform:bcm590xx-vregs");