mt6357-regulator.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. // SPDX-License-Identifier: GPL-2.0
  2. //
  3. // Copyright (c) 2022 MediaTek Inc.
  4. // Copyright (c) 2022 BayLibre, SAS.
  5. // Author: Chen Zhong <chen.zhong@mediatek.com>
  6. // Author: Fabien Parent <fparent@baylibre.com>
  7. // Author: Alexandre Mergnat <amergnat@baylibre.com>
  8. //
  9. // Based on mt6397-regulator.c
  10. //
  11. #include <linux/module.h>
  12. #include <linux/of.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/regmap.h>
  15. #include <linux/mfd/mt6397/core.h>
  16. #include <linux/mfd/mt6357/registers.h>
  17. #include <linux/regulator/driver.h>
  18. #include <linux/regulator/machine.h>
  19. #include <linux/regulator/mt6357-regulator.h>
  20. #include <linux/regulator/of_regulator.h>
  21. /*
  22. * MT6357 regulators' information
  23. *
  24. * @desc: standard fields of regulator description.
  25. * @da_vsel_reg: Monitor register for query buck's voltage.
  26. * @da_vsel_mask: Mask for query buck's voltage.
  27. */
  28. struct mt6357_regulator_info {
  29. struct regulator_desc desc;
  30. u32 da_vsel_reg;
  31. u32 da_vsel_mask;
  32. };
  33. #define MT6357_BUCK(match, vreg, min, max, step, \
  34. volt_ranges, vosel_reg, vosel_mask, _da_vsel_mask) \
  35. [MT6357_ID_##vreg] = { \
  36. .desc = { \
  37. .name = #vreg, \
  38. .of_match = of_match_ptr(match), \
  39. .regulators_node = "regulators", \
  40. .ops = &mt6357_volt_range_ops, \
  41. .type = REGULATOR_VOLTAGE, \
  42. .id = MT6357_ID_##vreg, \
  43. .owner = THIS_MODULE, \
  44. .n_voltages = ((max) - (min)) / (step) + 1, \
  45. .linear_ranges = volt_ranges, \
  46. .n_linear_ranges = ARRAY_SIZE(volt_ranges), \
  47. .vsel_reg = vosel_reg, \
  48. .vsel_mask = vosel_mask, \
  49. .enable_reg = MT6357_BUCK_##vreg##_CON0, \
  50. .enable_mask = BIT(0), \
  51. }, \
  52. .da_vsel_reg = MT6357_BUCK_##vreg##_DBG0, \
  53. .da_vsel_mask = vosel_mask, \
  54. }
  55. #define MT6357_LDO(match, vreg, ldo_volt_table, \
  56. enreg, vosel, vosel_mask) \
  57. [MT6357_ID_##vreg] = { \
  58. .desc = { \
  59. .name = #vreg, \
  60. .of_match = of_match_ptr(match), \
  61. .regulators_node = "regulators", \
  62. .ops = &mt6357_volt_table_ops, \
  63. .type = REGULATOR_VOLTAGE, \
  64. .id = MT6357_ID_##vreg, \
  65. .owner = THIS_MODULE, \
  66. .n_voltages = ARRAY_SIZE(ldo_volt_table), \
  67. .volt_table = ldo_volt_table, \
  68. .vsel_reg = vosel, \
  69. .vsel_mask = vosel_mask, \
  70. .enable_reg = enreg, \
  71. .enable_mask = BIT(0), \
  72. }, \
  73. }
  74. #define MT6357_LDO1(match, vreg, min, max, step, volt_ranges, \
  75. enreg, vosel, vosel_mask) \
  76. [MT6357_ID_##vreg] = { \
  77. .desc = { \
  78. .name = #vreg, \
  79. .of_match = of_match_ptr(match), \
  80. .regulators_node = "regulators", \
  81. .ops = &mt6357_volt_range_ops, \
  82. .type = REGULATOR_VOLTAGE, \
  83. .id = MT6357_ID_##vreg, \
  84. .owner = THIS_MODULE, \
  85. .n_voltages = ((max) - (min)) / (step) + 1, \
  86. .linear_ranges = volt_ranges, \
  87. .n_linear_ranges = ARRAY_SIZE(volt_ranges), \
  88. .vsel_reg = vosel, \
  89. .vsel_mask = vosel_mask, \
  90. .enable_reg = enreg, \
  91. .enable_mask = BIT(0), \
  92. }, \
  93. .da_vsel_reg = MT6357_LDO_##vreg##_DBG0, \
  94. .da_vsel_mask = 0x7f00, \
  95. }
  96. #define MT6357_REG_FIXED(match, vreg, volt) \
  97. [MT6357_ID_##vreg] = { \
  98. .desc = { \
  99. .name = #vreg, \
  100. .of_match = of_match_ptr(match), \
  101. .regulators_node = "regulators", \
  102. .ops = &mt6357_volt_fixed_ops, \
  103. .type = REGULATOR_VOLTAGE, \
  104. .id = MT6357_ID_##vreg, \
  105. .owner = THIS_MODULE, \
  106. .n_voltages = 1, \
  107. .enable_reg = MT6357_LDO_##vreg##_CON0, \
  108. .enable_mask = BIT(0), \
  109. .min_uV = volt, \
  110. }, \
  111. }
  112. /**
  113. * mt6357_get_buck_voltage_sel - get_voltage_sel for regmap users
  114. *
  115. * @rdev: regulator to operate on
  116. *
  117. * Regulators that use regmap for their register I/O can set the
  118. * da_vsel_reg and da_vsel_mask fields in the info structure and
  119. * then use this as their get_voltage_sel operation.
  120. */
  121. static int mt6357_get_buck_voltage_sel(struct regulator_dev *rdev)
  122. {
  123. int ret, regval;
  124. struct mt6357_regulator_info *info = rdev_get_drvdata(rdev);
  125. ret = regmap_read(rdev->regmap, info->da_vsel_reg, &regval);
  126. if (ret != 0) {
  127. dev_err(&rdev->dev,
  128. "Failed to get mt6357 Buck %s vsel reg: %d\n",
  129. info->desc.name, ret);
  130. return ret;
  131. }
  132. regval &= info->da_vsel_mask;
  133. regval >>= ffs(info->da_vsel_mask) - 1;
  134. return regval;
  135. }
  136. static const struct regulator_ops mt6357_volt_range_ops = {
  137. .list_voltage = regulator_list_voltage_linear_range,
  138. .map_voltage = regulator_map_voltage_linear_range,
  139. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  140. .get_voltage_sel = mt6357_get_buck_voltage_sel,
  141. .set_voltage_time_sel = regulator_set_voltage_time_sel,
  142. .enable = regulator_enable_regmap,
  143. .disable = regulator_disable_regmap,
  144. .is_enabled = regulator_is_enabled_regmap,
  145. };
  146. static const struct regulator_ops mt6357_volt_table_ops = {
  147. .list_voltage = regulator_list_voltage_table,
  148. .map_voltage = regulator_map_voltage_iterate,
  149. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  150. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  151. .set_voltage_time_sel = regulator_set_voltage_time_sel,
  152. .enable = regulator_enable_regmap,
  153. .disable = regulator_disable_regmap,
  154. .is_enabled = regulator_is_enabled_regmap,
  155. };
  156. static const struct regulator_ops mt6357_volt_fixed_ops = {
  157. .list_voltage = regulator_list_voltage_linear,
  158. .enable = regulator_enable_regmap,
  159. .disable = regulator_disable_regmap,
  160. .is_enabled = regulator_is_enabled_regmap,
  161. };
  162. static const int vxo22_voltages[] = {
  163. 2200000,
  164. 0,
  165. 2400000,
  166. };
  167. static const int vefuse_voltages[] = {
  168. 1200000,
  169. 1300000,
  170. 1500000,
  171. 0,
  172. 1800000,
  173. 0,
  174. 0,
  175. 0,
  176. 0,
  177. 2800000,
  178. 2900000,
  179. 3000000,
  180. 0,
  181. 3300000,
  182. };
  183. static const int vcn33_voltages[] = {
  184. 0,
  185. 3300000,
  186. 3400000,
  187. 3500000,
  188. };
  189. static const int vcama_voltages[] = {
  190. 0,
  191. 0,
  192. 0,
  193. 0,
  194. 0,
  195. 0,
  196. 0,
  197. 2500000,
  198. 0,
  199. 0,
  200. 2800000,
  201. };
  202. static const int vcamd_voltages[] = {
  203. 0,
  204. 0,
  205. 0,
  206. 0,
  207. 1000000,
  208. 1100000,
  209. 1200000,
  210. 1300000,
  211. 0,
  212. 1500000,
  213. 0,
  214. 0,
  215. 1800000,
  216. };
  217. static const int vldo28_voltages[] = {
  218. 0,
  219. 2800000,
  220. 0,
  221. 3000000,
  222. };
  223. static const int vdram_voltages[] = {
  224. 0,
  225. 1100000,
  226. 1200000,
  227. };
  228. static const int vsim_voltages[] = {
  229. 0,
  230. 0,
  231. 0,
  232. 1700000,
  233. 1800000,
  234. 0,
  235. 0,
  236. 0,
  237. 2700000,
  238. 0,
  239. 0,
  240. 3000000,
  241. 3100000,
  242. };
  243. static const int vibr_voltages[] = {
  244. 1200000,
  245. 1300000,
  246. 1500000,
  247. 0,
  248. 1800000,
  249. 2000000,
  250. 0,
  251. 0,
  252. 0,
  253. 2800000,
  254. 0,
  255. 3000000,
  256. 0,
  257. 3300000,
  258. };
  259. static const int vmc_voltages[] = {
  260. 0,
  261. 0,
  262. 0,
  263. 0,
  264. 1800000,
  265. 0,
  266. 0,
  267. 0,
  268. 0,
  269. 0,
  270. 2900000,
  271. 3000000,
  272. 0,
  273. 3300000,
  274. };
  275. static const int vmch_voltages[] = {
  276. 0,
  277. 0,
  278. 2900000,
  279. 3000000,
  280. 0,
  281. 3300000,
  282. };
  283. static const int vemc_voltages[] = {
  284. 0,
  285. 0,
  286. 2900000,
  287. 3000000,
  288. 0,
  289. 3300000,
  290. };
  291. static const int vusb_voltages[] = {
  292. 0,
  293. 0,
  294. 0,
  295. 3000000,
  296. 3100000,
  297. };
  298. static const struct linear_range buck_volt_range1[] = {
  299. REGULATOR_LINEAR_RANGE(518750, 0, 0x7f, 6250),
  300. };
  301. static const struct linear_range buck_volt_range2[] = {
  302. REGULATOR_LINEAR_RANGE(500000, 0, 0x7f, 6250),
  303. };
  304. static const struct linear_range buck_volt_range3[] = {
  305. REGULATOR_LINEAR_RANGE(500000, 0, 0x3f, 50000),
  306. };
  307. static const struct linear_range buck_volt_range4[] = {
  308. REGULATOR_LINEAR_RANGE(1200000, 0, 0x7f, 12500),
  309. };
  310. /* The array is indexed by id(MT6357_ID_XXX) */
  311. static struct mt6357_regulator_info mt6357_regulators[] = {
  312. /* Bucks */
  313. MT6357_BUCK("buck-vcore", VCORE, 518750, 1312500, 6250,
  314. buck_volt_range1, MT6357_BUCK_VCORE_ELR0, 0x7f, 0x7f),
  315. MT6357_BUCK("buck-vproc", VPROC, 518750, 1312500, 6250,
  316. buck_volt_range1, MT6357_BUCK_VPROC_ELR0, 0x7f, 0x7f),
  317. MT6357_BUCK("buck-vmodem", VMODEM, 500000, 1293750, 6250,
  318. buck_volt_range2, MT6357_BUCK_VMODEM_ELR0, 0x7f, 0x7f),
  319. MT6357_BUCK("buck-vpa", VPA, 500000, 3650000, 50000,
  320. buck_volt_range3, MT6357_BUCK_VPA_CON1, 0x3f, 0x3f),
  321. MT6357_BUCK("buck-vs1", VS1, 1200000, 2787500, 12500,
  322. buck_volt_range4, MT6357_BUCK_VS1_ELR0, 0x7f, 0x7f),
  323. /* LDOs */
  324. MT6357_LDO("ldo-vcama", VCAMA, vcama_voltages,
  325. MT6357_LDO_VCAMA_CON0, MT6357_VCAMA_ANA_CON0, 0xf00),
  326. MT6357_LDO("ldo-vcamd", VCAMD, vcamd_voltages,
  327. MT6357_LDO_VCAMD_CON0, MT6357_VCAMD_ANA_CON0, 0xf00),
  328. MT6357_LDO("ldo-vcn33-bt", VCN33_BT, vcn33_voltages,
  329. MT6357_LDO_VCN33_CON0_0, MT6357_VCN33_ANA_CON0, 0x300),
  330. MT6357_LDO("ldo-vcn33-wifi", VCN33_WIFI, vcn33_voltages,
  331. MT6357_LDO_VCN33_CON0_1, MT6357_VCN33_ANA_CON0, 0x300),
  332. MT6357_LDO("ldo-vdram", VDRAM, vdram_voltages,
  333. MT6357_LDO_VDRAM_CON0, MT6357_VDRAM_ELR_2, 0x300),
  334. MT6357_LDO("ldo-vefuse", VEFUSE, vefuse_voltages,
  335. MT6357_LDO_VEFUSE_CON0, MT6357_VEFUSE_ANA_CON0, 0xf00),
  336. MT6357_LDO("ldo-vemc", VEMC, vemc_voltages,
  337. MT6357_LDO_VEMC_CON0, MT6357_VEMC_ANA_CON0, 0x700),
  338. MT6357_LDO("ldo-vibr", VIBR, vibr_voltages,
  339. MT6357_LDO_VIBR_CON0, MT6357_VIBR_ANA_CON0, 0xf00),
  340. MT6357_LDO("ldo-vldo28", VLDO28, vldo28_voltages,
  341. MT6357_LDO_VLDO28_CON0_0, MT6357_VLDO28_ANA_CON0, 0x300),
  342. MT6357_LDO("ldo-vmc", VMC, vmc_voltages,
  343. MT6357_LDO_VMC_CON0, MT6357_VMC_ANA_CON0, 0xf00),
  344. MT6357_LDO("ldo-vmch", VMCH, vmch_voltages,
  345. MT6357_LDO_VMCH_CON0, MT6357_VMCH_ANA_CON0, 0x700),
  346. MT6357_LDO("ldo-vsim1", VSIM1, vsim_voltages,
  347. MT6357_LDO_VSIM1_CON0, MT6357_VSIM1_ANA_CON0, 0xf00),
  348. MT6357_LDO("ldo-vsim2", VSIM2, vsim_voltages,
  349. MT6357_LDO_VSIM2_CON0, MT6357_VSIM2_ANA_CON0, 0xf00),
  350. MT6357_LDO("ldo-vusb33", VUSB33, vusb_voltages,
  351. MT6357_LDO_VUSB33_CON0_0, MT6357_VUSB33_ANA_CON0, 0x700),
  352. MT6357_LDO("ldo-vxo22", VXO22, vxo22_voltages,
  353. MT6357_LDO_VXO22_CON0, MT6357_VXO22_ANA_CON0, 0x300),
  354. MT6357_LDO1("ldo-vsram-proc", VSRAM_PROC, 518750, 1312500, 6250,
  355. buck_volt_range1, MT6357_LDO_VSRAM_PROC_CON0,
  356. MT6357_LDO_VSRAM_CON0, 0x7f00),
  357. MT6357_LDO1("ldo-vsram-others", VSRAM_OTHERS, 518750, 1312500, 6250,
  358. buck_volt_range1, MT6357_LDO_VSRAM_OTHERS_CON0,
  359. MT6357_LDO_VSRAM_CON1, 0x7f00),
  360. MT6357_REG_FIXED("ldo-vaud28", VAUD28, 2800000),
  361. MT6357_REG_FIXED("ldo-vaux18", VAUX18, 1800000),
  362. MT6357_REG_FIXED("ldo-vcamio18", VCAMIO, 1800000),
  363. MT6357_REG_FIXED("ldo-vcn18", VCN18, 1800000),
  364. MT6357_REG_FIXED("ldo-vcn28", VCN28, 2800000),
  365. MT6357_REG_FIXED("ldo-vfe28", VFE28, 2800000),
  366. MT6357_REG_FIXED("ldo-vio18", VIO18, 1800000),
  367. MT6357_REG_FIXED("ldo-vio28", VIO28, 2800000),
  368. MT6357_REG_FIXED("ldo-vrf12", VRF12, 1200000),
  369. MT6357_REG_FIXED("ldo-vrf18", VRF18, 1800000),
  370. };
  371. static int mt6357_regulator_probe(struct platform_device *pdev)
  372. {
  373. struct mt6397_chip *mt6357 = dev_get_drvdata(pdev->dev.parent);
  374. struct regulator_config config = {};
  375. struct regulator_dev *rdev;
  376. int i;
  377. pdev->dev.of_node = pdev->dev.parent->of_node;
  378. for (i = 0; i < MT6357_MAX_REGULATOR; i++) {
  379. config.dev = &pdev->dev;
  380. config.driver_data = &mt6357_regulators[i];
  381. config.regmap = mt6357->regmap;
  382. rdev = devm_regulator_register(&pdev->dev,
  383. &mt6357_regulators[i].desc,
  384. &config);
  385. if (IS_ERR(rdev)) {
  386. dev_err(&pdev->dev, "failed to register %s\n",
  387. mt6357_regulators[i].desc.name);
  388. return PTR_ERR(rdev);
  389. }
  390. }
  391. return 0;
  392. }
  393. static const struct platform_device_id mt6357_platform_ids[] = {
  394. { "mt6357-regulator" },
  395. { /* sentinel */ },
  396. };
  397. MODULE_DEVICE_TABLE(platform, mt6357_platform_ids);
  398. static struct platform_driver mt6357_regulator_driver = {
  399. .driver = {
  400. .name = "mt6357-regulator",
  401. .probe_type = PROBE_PREFER_ASYNCHRONOUS,
  402. },
  403. .probe = mt6357_regulator_probe,
  404. .id_table = mt6357_platform_ids,
  405. };
  406. module_platform_driver(mt6357_regulator_driver);
  407. MODULE_AUTHOR("Chen Zhong <chen.zhong@mediatek.com>");
  408. MODULE_AUTHOR("Fabien Parent <fabien.parent@linaro.org>");
  409. MODULE_AUTHOR("Alexandre Mergnat <amergnat@baylibre.com>");
  410. MODULE_DESCRIPTION("Regulator Driver for MediaTek MT6357 PMIC");
  411. MODULE_LICENSE("GPL");