bcm590xx-regulator.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. /*
  2. * Broadcom BCM590xx regulator driver
  3. *
  4. * Copyright 2014 Linaro Limited
  5. * Author: Matt Porter <mporter@linaro.org>
  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. struct bcm590xx_info **info;
  183. };
  184. static int bcm590xx_get_vsel_register(int id)
  185. {
  186. if (BCM590XX_REG_IS_LDO(id))
  187. return BCM590XX_RFLDOCTRL + id;
  188. else if (BCM590XX_REG_IS_GPLDO(id))
  189. return BCM590XX_GPLDO1CTRL + id;
  190. else
  191. return BCM590XX_CSRVOUT1 + (id - BCM590XX_REG_CSR) * 3;
  192. }
  193. static int bcm590xx_get_enable_register(int id)
  194. {
  195. int reg = 0;
  196. if (BCM590XX_REG_IS_LDO(id))
  197. reg = BCM590XX_RFLDOPMCTRL1 + id * 2;
  198. else if (BCM590XX_REG_IS_GPLDO(id))
  199. reg = BCM590XX_GPLDO1PMCTRL1 + id * 2;
  200. else
  201. switch (id) {
  202. case BCM590XX_REG_CSR:
  203. reg = BCM590XX_CSRPMCTRL1;
  204. break;
  205. case BCM590XX_REG_IOSR1:
  206. reg = BCM590XX_IOSR1PMCTRL1;
  207. break;
  208. case BCM590XX_REG_IOSR2:
  209. reg = BCM590XX_IOSR2PMCTRL1;
  210. break;
  211. case BCM590XX_REG_MSR:
  212. reg = BCM590XX_MSRPMCTRL1;
  213. break;
  214. case BCM590XX_REG_SDSR1:
  215. reg = BCM590XX_SDSR1PMCTRL1;
  216. break;
  217. case BCM590XX_REG_SDSR2:
  218. reg = BCM590XX_SDSR2PMCTRL1;
  219. break;
  220. case BCM590XX_REG_VBUS:
  221. reg = BCM590XX_OTG_CTRL;
  222. };
  223. return reg;
  224. }
  225. static struct regulator_ops bcm590xx_ops_ldo = {
  226. .is_enabled = regulator_is_enabled_regmap,
  227. .enable = regulator_enable_regmap,
  228. .disable = regulator_disable_regmap,
  229. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  230. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  231. .list_voltage = regulator_list_voltage_table,
  232. .map_voltage = regulator_map_voltage_iterate,
  233. };
  234. static struct regulator_ops bcm590xx_ops_dcdc = {
  235. .is_enabled = regulator_is_enabled_regmap,
  236. .enable = regulator_enable_regmap,
  237. .disable = regulator_disable_regmap,
  238. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  239. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  240. .list_voltage = regulator_list_voltage_linear_range,
  241. .map_voltage = regulator_map_voltage_linear_range,
  242. };
  243. static struct regulator_ops bcm590xx_ops_vbus = {
  244. .is_enabled = regulator_is_enabled_regmap,
  245. .enable = regulator_enable_regmap,
  246. .disable = regulator_disable_regmap,
  247. };
  248. #define BCM590XX_MATCH(_name, _id) \
  249. { \
  250. .name = #_name, \
  251. .driver_data = (void *)&bcm590xx_regs[BCM590XX_REG_##_id], \
  252. }
  253. static struct of_regulator_match bcm590xx_matches[] = {
  254. BCM590XX_MATCH(rfldo, RFLDO),
  255. BCM590XX_MATCH(camldo1, CAMLDO1),
  256. BCM590XX_MATCH(camldo2, CAMLDO2),
  257. BCM590XX_MATCH(simldo1, SIMLDO1),
  258. BCM590XX_MATCH(simldo2, SIMLDO2),
  259. BCM590XX_MATCH(sdldo, SDLDO),
  260. BCM590XX_MATCH(sdxldo, SDXLDO),
  261. BCM590XX_MATCH(mmcldo1, MMCLDO1),
  262. BCM590XX_MATCH(mmcldo2, MMCLDO2),
  263. BCM590XX_MATCH(audldo, AUDLDO),
  264. BCM590XX_MATCH(micldo, MICLDO),
  265. BCM590XX_MATCH(usbldo, USBLDO),
  266. BCM590XX_MATCH(vibldo, VIBLDO),
  267. BCM590XX_MATCH(csr, CSR),
  268. BCM590XX_MATCH(iosr1, IOSR1),
  269. BCM590XX_MATCH(iosr2, IOSR2),
  270. BCM590XX_MATCH(msr, MSR),
  271. BCM590XX_MATCH(sdsr1, SDSR1),
  272. BCM590XX_MATCH(sdsr2, SDSR2),
  273. BCM590XX_MATCH(vsr, VSR),
  274. BCM590XX_MATCH(gpldo1, GPLDO1),
  275. BCM590XX_MATCH(gpldo2, GPLDO2),
  276. BCM590XX_MATCH(gpldo3, GPLDO3),
  277. BCM590XX_MATCH(gpldo4, GPLDO4),
  278. BCM590XX_MATCH(gpldo5, GPLDO5),
  279. BCM590XX_MATCH(gpldo6, GPLDO6),
  280. BCM590XX_MATCH(vbus, VBUS),
  281. };
  282. static struct bcm590xx_board *bcm590xx_parse_dt_reg_data(
  283. struct platform_device *pdev,
  284. struct of_regulator_match **bcm590xx_reg_matches)
  285. {
  286. struct bcm590xx_board *data;
  287. struct device_node *np = pdev->dev.parent->of_node;
  288. struct device_node *regulators;
  289. struct of_regulator_match *matches = bcm590xx_matches;
  290. int count = ARRAY_SIZE(bcm590xx_matches);
  291. int idx = 0;
  292. int ret;
  293. if (!np) {
  294. dev_err(&pdev->dev, "of node not found\n");
  295. return NULL;
  296. }
  297. data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
  298. if (!data) {
  299. dev_err(&pdev->dev, "failed to allocate regulator board data\n");
  300. return NULL;
  301. }
  302. np = of_node_get(np);
  303. regulators = of_get_child_by_name(np, "regulators");
  304. if (!regulators) {
  305. dev_warn(&pdev->dev, "regulator node not found\n");
  306. return NULL;
  307. }
  308. ret = of_regulator_match(&pdev->dev, regulators, matches, count);
  309. of_node_put(regulators);
  310. if (ret < 0) {
  311. dev_err(&pdev->dev, "Error parsing regulator init data: %d\n",
  312. ret);
  313. return NULL;
  314. }
  315. *bcm590xx_reg_matches = matches;
  316. for (idx = 0; idx < count; idx++) {
  317. if (!matches[idx].init_data || !matches[idx].of_node)
  318. continue;
  319. data->bcm590xx_pmu_init_data[idx] = matches[idx].init_data;
  320. }
  321. return data;
  322. }
  323. static int bcm590xx_probe(struct platform_device *pdev)
  324. {
  325. struct bcm590xx *bcm590xx = dev_get_drvdata(pdev->dev.parent);
  326. struct bcm590xx_board *pmu_data = NULL;
  327. struct bcm590xx_reg *pmu;
  328. struct regulator_config config = { };
  329. struct bcm590xx_info *info;
  330. struct regulator_init_data *reg_data;
  331. struct regulator_dev *rdev;
  332. struct of_regulator_match *bcm590xx_reg_matches = NULL;
  333. int i;
  334. pmu_data = bcm590xx_parse_dt_reg_data(pdev,
  335. &bcm590xx_reg_matches);
  336. pmu = devm_kzalloc(&pdev->dev, sizeof(*pmu), GFP_KERNEL);
  337. if (!pmu) {
  338. dev_err(&pdev->dev, "Memory allocation failed for pmu\n");
  339. return -ENOMEM;
  340. }
  341. pmu->mfd = bcm590xx;
  342. platform_set_drvdata(pdev, pmu);
  343. pmu->desc = devm_kzalloc(&pdev->dev, BCM590XX_NUM_REGS *
  344. sizeof(struct regulator_desc), GFP_KERNEL);
  345. if (!pmu->desc) {
  346. dev_err(&pdev->dev, "Memory alloc fails for desc\n");
  347. return -ENOMEM;
  348. }
  349. pmu->info = devm_kzalloc(&pdev->dev, BCM590XX_NUM_REGS *
  350. sizeof(struct bcm590xx_info *), GFP_KERNEL);
  351. if (!pmu->info) {
  352. dev_err(&pdev->dev, "Memory alloc fails for info\n");
  353. return -ENOMEM;
  354. }
  355. info = bcm590xx_regs;
  356. for (i = 0; i < BCM590XX_NUM_REGS; i++, info++) {
  357. if (pmu_data)
  358. reg_data = pmu_data->bcm590xx_pmu_init_data[i];
  359. else
  360. reg_data = NULL;
  361. /* Register the regulators */
  362. pmu->info[i] = info;
  363. pmu->desc[i].name = info->name;
  364. pmu->desc[i].supply_name = info->vin_name;
  365. pmu->desc[i].id = i;
  366. pmu->desc[i].volt_table = info->volt_table;
  367. pmu->desc[i].n_voltages = info->n_voltages;
  368. pmu->desc[i].linear_ranges = info->linear_ranges;
  369. pmu->desc[i].n_linear_ranges = info->n_linear_ranges;
  370. if ((BCM590XX_REG_IS_LDO(i)) || (BCM590XX_REG_IS_GPLDO(i))) {
  371. pmu->desc[i].ops = &bcm590xx_ops_ldo;
  372. pmu->desc[i].vsel_mask = BCM590XX_LDO_VSEL_MASK;
  373. } else if (BCM590XX_REG_IS_VBUS(i))
  374. pmu->desc[i].ops = &bcm590xx_ops_vbus;
  375. else {
  376. pmu->desc[i].ops = &bcm590xx_ops_dcdc;
  377. pmu->desc[i].vsel_mask = BCM590XX_SR_VSEL_MASK;
  378. }
  379. if (BCM590XX_REG_IS_VBUS(i))
  380. pmu->desc[i].enable_mask = BCM590XX_VBUS_ENABLE;
  381. else {
  382. pmu->desc[i].vsel_reg = bcm590xx_get_vsel_register(i);
  383. pmu->desc[i].enable_is_inverted = true;
  384. pmu->desc[i].enable_mask = BCM590XX_REG_ENABLE;
  385. }
  386. pmu->desc[i].enable_reg = bcm590xx_get_enable_register(i);
  387. pmu->desc[i].type = REGULATOR_VOLTAGE;
  388. pmu->desc[i].owner = THIS_MODULE;
  389. config.dev = bcm590xx->dev;
  390. config.init_data = reg_data;
  391. config.driver_data = pmu;
  392. if (BCM590XX_REG_IS_GPLDO(i) || BCM590XX_REG_IS_VBUS(i))
  393. config.regmap = bcm590xx->regmap_sec;
  394. else
  395. config.regmap = bcm590xx->regmap_pri;
  396. if (bcm590xx_reg_matches)
  397. config.of_node = bcm590xx_reg_matches[i].of_node;
  398. rdev = devm_regulator_register(&pdev->dev, &pmu->desc[i],
  399. &config);
  400. if (IS_ERR(rdev)) {
  401. dev_err(bcm590xx->dev,
  402. "failed to register %s regulator\n",
  403. pdev->name);
  404. return PTR_ERR(rdev);
  405. }
  406. }
  407. return 0;
  408. }
  409. static struct platform_driver bcm590xx_regulator_driver = {
  410. .driver = {
  411. .name = "bcm590xx-vregs",
  412. .owner = THIS_MODULE,
  413. },
  414. .probe = bcm590xx_probe,
  415. };
  416. module_platform_driver(bcm590xx_regulator_driver);
  417. MODULE_AUTHOR("Matt Porter <mporter@linaro.org>");
  418. MODULE_DESCRIPTION("BCM590xx voltage regulator driver");
  419. MODULE_LICENSE("GPL v2");
  420. MODULE_ALIAS("platform:bcm590xx-vregs");