mt6323-regulator.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. // SPDX-License-Identifier: GPL-2.0
  2. //
  3. // Copyright (c) 2016 MediaTek Inc.
  4. // Author: Chen Zhong <chen.zhong@mediatek.com>
  5. #include <linux/module.h>
  6. #include <linux/of.h>
  7. #include <linux/platform_device.h>
  8. #include <linux/regmap.h>
  9. #include <linux/mfd/mt6397/core.h>
  10. #include <linux/mfd/mt6323/registers.h>
  11. #include <linux/regulator/driver.h>
  12. #include <linux/regulator/machine.h>
  13. #include <linux/regulator/mt6323-regulator.h>
  14. #include <linux/regulator/of_regulator.h>
  15. #define MT6323_LDO_MODE_NORMAL 0
  16. #define MT6323_LDO_MODE_LP 1
  17. /*
  18. * MT6323 regulators' information
  19. *
  20. * @desc: standard fields of regulator description.
  21. * @qi: Mask for query enable signal status of regulators
  22. * @vselon_reg: Register sections for hardware control mode of bucks
  23. * @vselctrl_reg: Register for controlling the buck control mode.
  24. * @vselctrl_mask: Mask for query buck's voltage control mode.
  25. */
  26. struct mt6323_regulator_info {
  27. struct regulator_desc desc;
  28. u32 qi;
  29. u32 vselon_reg;
  30. u32 vselctrl_reg;
  31. u32 vselctrl_mask;
  32. u32 modeset_reg;
  33. u32 modeset_mask;
  34. };
  35. #define MT6323_BUCK(match, vreg, min, max, step, volt_ranges, enreg, \
  36. vosel, vosel_mask, voselon, vosel_ctrl) \
  37. [MT6323_ID_##vreg] = { \
  38. .desc = { \
  39. .name = #vreg, \
  40. .of_match = of_match_ptr(match), \
  41. .ops = &mt6323_volt_range_ops, \
  42. .type = REGULATOR_VOLTAGE, \
  43. .id = MT6323_ID_##vreg, \
  44. .owner = THIS_MODULE, \
  45. .n_voltages = (max - min)/step + 1, \
  46. .linear_ranges = volt_ranges, \
  47. .n_linear_ranges = ARRAY_SIZE(volt_ranges), \
  48. .vsel_reg = vosel, \
  49. .vsel_mask = vosel_mask, \
  50. .enable_reg = enreg, \
  51. .enable_mask = BIT(0), \
  52. }, \
  53. .qi = BIT(13), \
  54. .vselon_reg = voselon, \
  55. .vselctrl_reg = vosel_ctrl, \
  56. .vselctrl_mask = BIT(1), \
  57. }
  58. #define MT6323_LDO(match, vreg, ldo_volt_table, enreg, enbit, vosel, \
  59. vosel_mask, _modeset_reg, _modeset_mask) \
  60. [MT6323_ID_##vreg] = { \
  61. .desc = { \
  62. .name = #vreg, \
  63. .of_match = of_match_ptr(match), \
  64. .ops = &mt6323_volt_table_ops, \
  65. .type = REGULATOR_VOLTAGE, \
  66. .id = MT6323_ID_##vreg, \
  67. .owner = THIS_MODULE, \
  68. .n_voltages = ARRAY_SIZE(ldo_volt_table), \
  69. .volt_table = ldo_volt_table, \
  70. .vsel_reg = vosel, \
  71. .vsel_mask = vosel_mask, \
  72. .enable_reg = enreg, \
  73. .enable_mask = BIT(enbit), \
  74. }, \
  75. .qi = BIT(15), \
  76. .modeset_reg = _modeset_reg, \
  77. .modeset_mask = _modeset_mask, \
  78. }
  79. #define MT6323_REG_FIXED(match, vreg, enreg, enbit, volt, \
  80. _modeset_reg, _modeset_mask) \
  81. [MT6323_ID_##vreg] = { \
  82. .desc = { \
  83. .name = #vreg, \
  84. .of_match = of_match_ptr(match), \
  85. .ops = &mt6323_volt_fixed_ops, \
  86. .type = REGULATOR_VOLTAGE, \
  87. .id = MT6323_ID_##vreg, \
  88. .owner = THIS_MODULE, \
  89. .n_voltages = 1, \
  90. .enable_reg = enreg, \
  91. .enable_mask = BIT(enbit), \
  92. .min_uV = volt, \
  93. }, \
  94. .qi = BIT(15), \
  95. .modeset_reg = _modeset_reg, \
  96. .modeset_mask = _modeset_mask, \
  97. }
  98. static const struct linear_range buck_volt_range1[] = {
  99. REGULATOR_LINEAR_RANGE(700000, 0, 0x7f, 6250),
  100. };
  101. static const struct linear_range buck_volt_range2[] = {
  102. REGULATOR_LINEAR_RANGE(1400000, 0, 0x7f, 12500),
  103. };
  104. static const struct linear_range buck_volt_range3[] = {
  105. REGULATOR_LINEAR_RANGE(500000, 0, 0x3f, 50000),
  106. };
  107. static const unsigned int ldo_volt_table1[] = {
  108. 3300000, 3400000, 3500000, 3600000,
  109. };
  110. static const unsigned int ldo_volt_table2[] = {
  111. 1500000, 1800000, 2500000, 2800000,
  112. };
  113. static const unsigned int ldo_volt_table3[] = {
  114. 1800000, 3300000,
  115. };
  116. static const unsigned int ldo_volt_table4[] = {
  117. 3000000, 3300000,
  118. };
  119. static const unsigned int ldo_volt_table5[] = {
  120. 1200000, 1300000, 1500000, 1800000, 2000000, 2800000, 3000000, 3300000,
  121. };
  122. static const unsigned int ldo_volt_table6[] = {
  123. 1200000, 1300000, 1500000, 1800000, 2500000, 2800000, 3000000, 2000000,
  124. };
  125. static const unsigned int ldo_volt_table7[] = {
  126. 1200000, 1300000, 1500000, 1800000,
  127. };
  128. static const unsigned int ldo_volt_table8[] = {
  129. 1800000, 3000000,
  130. };
  131. static const unsigned int ldo_volt_table9[] = {
  132. 1200000, 1350000, 1500000, 1800000,
  133. };
  134. static const unsigned int ldo_volt_table10[] = {
  135. 1200000, 1300000, 1500000, 1800000,
  136. };
  137. static int mt6323_get_status(struct regulator_dev *rdev)
  138. {
  139. int ret;
  140. u32 regval;
  141. struct mt6323_regulator_info *info = rdev_get_drvdata(rdev);
  142. ret = regmap_read(rdev->regmap, info->desc.enable_reg, &regval);
  143. if (ret != 0) {
  144. dev_err(&rdev->dev, "Failed to get enable reg: %d\n", ret);
  145. return ret;
  146. }
  147. return (regval & info->qi) ? REGULATOR_STATUS_ON : REGULATOR_STATUS_OFF;
  148. }
  149. static int mt6323_ldo_set_mode(struct regulator_dev *rdev, unsigned int mode)
  150. {
  151. int ret, val = 0;
  152. struct mt6323_regulator_info *info = rdev_get_drvdata(rdev);
  153. if (!info->modeset_mask) {
  154. dev_err(&rdev->dev, "regulator %s doesn't support set_mode\n",
  155. info->desc.name);
  156. return -EINVAL;
  157. }
  158. switch (mode) {
  159. case REGULATOR_MODE_STANDBY:
  160. val = MT6323_LDO_MODE_LP;
  161. break;
  162. case REGULATOR_MODE_NORMAL:
  163. val = MT6323_LDO_MODE_NORMAL;
  164. break;
  165. default:
  166. return -EINVAL;
  167. }
  168. val <<= ffs(info->modeset_mask) - 1;
  169. ret = regmap_update_bits(rdev->regmap, info->modeset_reg,
  170. info->modeset_mask, val);
  171. return ret;
  172. }
  173. static unsigned int mt6323_ldo_get_mode(struct regulator_dev *rdev)
  174. {
  175. unsigned int val;
  176. unsigned int mode;
  177. int ret;
  178. struct mt6323_regulator_info *info = rdev_get_drvdata(rdev);
  179. if (!info->modeset_mask) {
  180. dev_err(&rdev->dev, "regulator %s doesn't support get_mode\n",
  181. info->desc.name);
  182. return -EINVAL;
  183. }
  184. ret = regmap_read(rdev->regmap, info->modeset_reg, &val);
  185. if (ret < 0)
  186. return ret;
  187. val &= info->modeset_mask;
  188. val >>= ffs(info->modeset_mask) - 1;
  189. if (val & 0x1)
  190. mode = REGULATOR_MODE_STANDBY;
  191. else
  192. mode = REGULATOR_MODE_NORMAL;
  193. return mode;
  194. }
  195. static const struct regulator_ops mt6323_volt_range_ops = {
  196. .list_voltage = regulator_list_voltage_linear_range,
  197. .map_voltage = regulator_map_voltage_linear_range,
  198. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  199. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  200. .set_voltage_time_sel = regulator_set_voltage_time_sel,
  201. .enable = regulator_enable_regmap,
  202. .disable = regulator_disable_regmap,
  203. .is_enabled = regulator_is_enabled_regmap,
  204. .get_status = mt6323_get_status,
  205. };
  206. static const struct regulator_ops mt6323_volt_table_ops = {
  207. .list_voltage = regulator_list_voltage_table,
  208. .map_voltage = regulator_map_voltage_iterate,
  209. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  210. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  211. .set_voltage_time_sel = regulator_set_voltage_time_sel,
  212. .enable = regulator_enable_regmap,
  213. .disable = regulator_disable_regmap,
  214. .is_enabled = regulator_is_enabled_regmap,
  215. .get_status = mt6323_get_status,
  216. .set_mode = mt6323_ldo_set_mode,
  217. .get_mode = mt6323_ldo_get_mode,
  218. };
  219. static const struct regulator_ops mt6323_volt_fixed_ops = {
  220. .list_voltage = regulator_list_voltage_linear,
  221. .enable = regulator_enable_regmap,
  222. .disable = regulator_disable_regmap,
  223. .is_enabled = regulator_is_enabled_regmap,
  224. .get_status = mt6323_get_status,
  225. .set_mode = mt6323_ldo_set_mode,
  226. .get_mode = mt6323_ldo_get_mode,
  227. };
  228. /* The array is indexed by id(MT6323_ID_XXX) */
  229. static struct mt6323_regulator_info mt6323_regulators[] = {
  230. MT6323_BUCK("buck_vproc", VPROC, 700000, 1493750, 6250,
  231. buck_volt_range1, MT6323_VPROC_CON7, MT6323_VPROC_CON9, 0x7f,
  232. MT6323_VPROC_CON10, MT6323_VPROC_CON5),
  233. MT6323_BUCK("buck_vsys", VSYS, 1400000, 2987500, 12500,
  234. buck_volt_range2, MT6323_VSYS_CON7, MT6323_VSYS_CON9, 0x7f,
  235. MT6323_VSYS_CON10, MT6323_VSYS_CON5),
  236. MT6323_BUCK("buck_vpa", VPA, 500000, 3650000, 50000,
  237. buck_volt_range3, MT6323_VPA_CON7, MT6323_VPA_CON9,
  238. 0x3f, MT6323_VPA_CON10, MT6323_VPA_CON5),
  239. MT6323_REG_FIXED("ldo_vtcxo", VTCXO, MT6323_ANALDO_CON1, 10, 2800000,
  240. MT6323_ANALDO_CON1, 0x2),
  241. MT6323_REG_FIXED("ldo_vcn28", VCN28, MT6323_ANALDO_CON19, 12, 2800000,
  242. MT6323_ANALDO_CON20, 0x2),
  243. MT6323_LDO("ldo_vcn33_bt", VCN33_BT, ldo_volt_table1,
  244. MT6323_ANALDO_CON16, 7, MT6323_ANALDO_CON16, 0xC,
  245. MT6323_ANALDO_CON21, 0x2),
  246. MT6323_LDO("ldo_vcn33_wifi", VCN33_WIFI, ldo_volt_table1,
  247. MT6323_ANALDO_CON17, 12, MT6323_ANALDO_CON16, 0xC,
  248. MT6323_ANALDO_CON21, 0x2),
  249. MT6323_REG_FIXED("ldo_va", VA, MT6323_ANALDO_CON2, 14, 2800000,
  250. MT6323_ANALDO_CON2, 0x2),
  251. MT6323_LDO("ldo_vcama", VCAMA, ldo_volt_table2,
  252. MT6323_ANALDO_CON4, 15, MT6323_ANALDO_CON10, 0x60, -1, 0),
  253. MT6323_REG_FIXED("ldo_vio28", VIO28, MT6323_DIGLDO_CON0, 14, 2800000,
  254. MT6323_DIGLDO_CON0, 0x2),
  255. MT6323_REG_FIXED("ldo_vusb", VUSB, MT6323_DIGLDO_CON2, 14, 3300000,
  256. MT6323_DIGLDO_CON2, 0x2),
  257. MT6323_LDO("ldo_vmc", VMC, ldo_volt_table3,
  258. MT6323_DIGLDO_CON3, 12, MT6323_DIGLDO_CON24, 0x10,
  259. MT6323_DIGLDO_CON3, 0x2),
  260. MT6323_LDO("ldo_vmch", VMCH, ldo_volt_table4,
  261. MT6323_DIGLDO_CON5, 14, MT6323_DIGLDO_CON26, 0x80,
  262. MT6323_DIGLDO_CON5, 0x2),
  263. MT6323_LDO("ldo_vemc3v3", VEMC3V3, ldo_volt_table4,
  264. MT6323_DIGLDO_CON6, 14, MT6323_DIGLDO_CON27, 0x80,
  265. MT6323_DIGLDO_CON6, 0x2),
  266. MT6323_LDO("ldo_vgp1", VGP1, ldo_volt_table5,
  267. MT6323_DIGLDO_CON7, 15, MT6323_DIGLDO_CON28, 0xE0,
  268. MT6323_DIGLDO_CON7, 0x2),
  269. MT6323_LDO("ldo_vgp2", VGP2, ldo_volt_table6,
  270. MT6323_DIGLDO_CON8, 15, MT6323_DIGLDO_CON29, 0xE0,
  271. MT6323_DIGLDO_CON8, 0x2),
  272. MT6323_LDO("ldo_vgp3", VGP3, ldo_volt_table7,
  273. MT6323_DIGLDO_CON9, 15, MT6323_DIGLDO_CON30, 0x60,
  274. MT6323_DIGLDO_CON9, 0x2),
  275. MT6323_REG_FIXED("ldo_vcn18", VCN18, MT6323_DIGLDO_CON11, 14, 1800000,
  276. MT6323_DIGLDO_CON11, 0x2),
  277. MT6323_LDO("ldo_vsim1", VSIM1, ldo_volt_table8,
  278. MT6323_DIGLDO_CON13, 15, MT6323_DIGLDO_CON34, 0x20,
  279. MT6323_DIGLDO_CON13, 0x2),
  280. MT6323_LDO("ldo_vsim2", VSIM2, ldo_volt_table8,
  281. MT6323_DIGLDO_CON14, 15, MT6323_DIGLDO_CON35, 0x20,
  282. MT6323_DIGLDO_CON14, 0x2),
  283. MT6323_REG_FIXED("ldo_vrtc", VRTC, MT6323_DIGLDO_CON15, 8, 2800000,
  284. -1, 0),
  285. MT6323_LDO("ldo_vcamaf", VCAMAF, ldo_volt_table5,
  286. MT6323_DIGLDO_CON31, 15, MT6323_DIGLDO_CON32, 0xE0,
  287. MT6323_DIGLDO_CON31, 0x2),
  288. MT6323_LDO("ldo_vibr", VIBR, ldo_volt_table5,
  289. MT6323_DIGLDO_CON39, 15, MT6323_DIGLDO_CON40, 0xE0,
  290. MT6323_DIGLDO_CON39, 0x2),
  291. MT6323_REG_FIXED("ldo_vrf18", VRF18, MT6323_DIGLDO_CON45, 15, 1825000,
  292. MT6323_DIGLDO_CON45, 0x2),
  293. MT6323_LDO("ldo_vm", VM, ldo_volt_table9,
  294. MT6323_DIGLDO_CON47, 14, MT6323_DIGLDO_CON48, 0x30,
  295. MT6323_DIGLDO_CON47, 0x2),
  296. MT6323_REG_FIXED("ldo_vio18", VIO18, MT6323_DIGLDO_CON49, 14, 1800000,
  297. MT6323_DIGLDO_CON49, 0x2),
  298. MT6323_LDO("ldo_vcamd", VCAMD, ldo_volt_table10,
  299. MT6323_DIGLDO_CON51, 14, MT6323_DIGLDO_CON52, 0x60,
  300. MT6323_DIGLDO_CON51, 0x2),
  301. MT6323_REG_FIXED("ldo_vcamio", VCAMIO, MT6323_DIGLDO_CON53, 14, 1800000,
  302. MT6323_DIGLDO_CON53, 0x2),
  303. };
  304. static int mt6323_set_buck_vosel_reg(struct platform_device *pdev)
  305. {
  306. struct mt6397_chip *mt6323 = dev_get_drvdata(pdev->dev.parent);
  307. int i;
  308. u32 regval;
  309. for (i = 0; i < MT6323_MAX_REGULATOR; i++) {
  310. if (mt6323_regulators[i].vselctrl_reg) {
  311. if (regmap_read(mt6323->regmap,
  312. mt6323_regulators[i].vselctrl_reg,
  313. &regval) < 0) {
  314. dev_err(&pdev->dev,
  315. "Failed to read buck ctrl\n");
  316. return -EIO;
  317. }
  318. if (regval & mt6323_regulators[i].vselctrl_mask) {
  319. mt6323_regulators[i].desc.vsel_reg =
  320. mt6323_regulators[i].vselon_reg;
  321. }
  322. }
  323. }
  324. return 0;
  325. }
  326. static int mt6323_regulator_probe(struct platform_device *pdev)
  327. {
  328. struct mt6397_chip *mt6323 = dev_get_drvdata(pdev->dev.parent);
  329. struct regulator_config config = {};
  330. struct regulator_dev *rdev;
  331. int i;
  332. u32 reg_value;
  333. /* Query buck controller to select activated voltage register part */
  334. if (mt6323_set_buck_vosel_reg(pdev))
  335. return -EIO;
  336. /* Read PMIC chip revision to update constraints and voltage table */
  337. if (regmap_read(mt6323->regmap, MT6323_CID, &reg_value) < 0) {
  338. dev_err(&pdev->dev, "Failed to read Chip ID\n");
  339. return -EIO;
  340. }
  341. dev_info(&pdev->dev, "Chip ID = 0x%x\n", reg_value);
  342. for (i = 0; i < MT6323_MAX_REGULATOR; i++) {
  343. config.dev = &pdev->dev;
  344. config.driver_data = &mt6323_regulators[i];
  345. config.regmap = mt6323->regmap;
  346. rdev = devm_regulator_register(&pdev->dev,
  347. &mt6323_regulators[i].desc, &config);
  348. if (IS_ERR(rdev)) {
  349. dev_err(&pdev->dev, "failed to register %s\n",
  350. mt6323_regulators[i].desc.name);
  351. return PTR_ERR(rdev);
  352. }
  353. }
  354. return 0;
  355. }
  356. static const struct platform_device_id mt6323_platform_ids[] = {
  357. {"mt6323-regulator", 0},
  358. { /* sentinel */ },
  359. };
  360. MODULE_DEVICE_TABLE(platform, mt6323_platform_ids);
  361. static struct platform_driver mt6323_regulator_driver = {
  362. .driver = {
  363. .name = "mt6323-regulator",
  364. },
  365. .probe = mt6323_regulator_probe,
  366. .id_table = mt6323_platform_ids,
  367. };
  368. module_platform_driver(mt6323_regulator_driver);
  369. MODULE_AUTHOR("Chen Zhong <chen.zhong@mediatek.com>");
  370. MODULE_DESCRIPTION("Regulator Driver for MediaTek MT6323 PMIC");
  371. MODULE_LICENSE("GPL v2");