mt6397-regulator.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. // SPDX-License-Identifier: GPL-2.0
  2. //
  3. // Copyright (c) 2014 MediaTek Inc.
  4. // Author: Flora Fu <[email protected]>
  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/mt6397/registers.h>
  11. #include <linux/regulator/driver.h>
  12. #include <linux/regulator/machine.h>
  13. #include <linux/regulator/mt6397-regulator.h>
  14. #include <linux/regulator/of_regulator.h>
  15. #include <dt-bindings/regulator/mediatek,mt6397-regulator.h>
  16. /*
  17. * MT6397 regulators' information
  18. *
  19. * @desc: standard fields of regulator description.
  20. * @qi: Mask for query enable signal status of regulators
  21. * @vselon_reg: Register sections for hardware control mode of bucks
  22. * @vselctrl_reg: Register for controlling the buck control mode.
  23. * @vselctrl_mask: Mask for query buck's voltage control mode.
  24. */
  25. struct mt6397_regulator_info {
  26. struct regulator_desc desc;
  27. u32 qi;
  28. u32 vselon_reg;
  29. u32 vselctrl_reg;
  30. u32 vselctrl_mask;
  31. u32 modeset_reg;
  32. u32 modeset_mask;
  33. };
  34. #define MT6397_BUCK(match, vreg, min, max, step, volt_ranges, enreg, \
  35. vosel, vosel_mask, voselon, vosel_ctrl, _modeset_reg, \
  36. _modeset_shift) \
  37. [MT6397_ID_##vreg] = { \
  38. .desc = { \
  39. .name = #vreg, \
  40. .of_match = of_match_ptr(match), \
  41. .ops = &mt6397_volt_range_ops, \
  42. .type = REGULATOR_VOLTAGE, \
  43. .id = MT6397_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. .of_map_mode = mt6397_map_mode, \
  53. }, \
  54. .qi = BIT(13), \
  55. .vselon_reg = voselon, \
  56. .vselctrl_reg = vosel_ctrl, \
  57. .vselctrl_mask = BIT(1), \
  58. .modeset_reg = _modeset_reg, \
  59. .modeset_mask = BIT(_modeset_shift), \
  60. }
  61. #define MT6397_LDO(match, vreg, ldo_volt_table, enreg, enbit, vosel, \
  62. vosel_mask) \
  63. [MT6397_ID_##vreg] = { \
  64. .desc = { \
  65. .name = #vreg, \
  66. .of_match = of_match_ptr(match), \
  67. .ops = &mt6397_volt_table_ops, \
  68. .type = REGULATOR_VOLTAGE, \
  69. .id = MT6397_ID_##vreg, \
  70. .owner = THIS_MODULE, \
  71. .n_voltages = ARRAY_SIZE(ldo_volt_table), \
  72. .volt_table = ldo_volt_table, \
  73. .vsel_reg = vosel, \
  74. .vsel_mask = vosel_mask, \
  75. .enable_reg = enreg, \
  76. .enable_mask = BIT(enbit), \
  77. }, \
  78. .qi = BIT(15), \
  79. }
  80. #define MT6397_REG_FIXED(match, vreg, enreg, enbit, volt) \
  81. [MT6397_ID_##vreg] = { \
  82. .desc = { \
  83. .name = #vreg, \
  84. .of_match = of_match_ptr(match), \
  85. .ops = &mt6397_volt_fixed_ops, \
  86. .type = REGULATOR_VOLTAGE, \
  87. .id = MT6397_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. }
  96. static const struct linear_range buck_volt_range1[] = {
  97. REGULATOR_LINEAR_RANGE(700000, 0, 0x7f, 6250),
  98. };
  99. static const struct linear_range buck_volt_range2[] = {
  100. REGULATOR_LINEAR_RANGE(800000, 0, 0x7f, 6250),
  101. };
  102. static const struct linear_range buck_volt_range3[] = {
  103. REGULATOR_LINEAR_RANGE(1500000, 0, 0x1f, 20000),
  104. };
  105. static const unsigned int ldo_volt_table1[] = {
  106. 1500000, 1800000, 2500000, 2800000,
  107. };
  108. static const unsigned int ldo_volt_table2[] = {
  109. 1800000, 3300000,
  110. };
  111. static const unsigned int ldo_volt_table3[] = {
  112. 3000000, 3300000,
  113. };
  114. static const unsigned int ldo_volt_table4[] = {
  115. 1220000, 1300000, 1500000, 1800000, 2500000, 2800000, 3000000, 3300000,
  116. };
  117. static const unsigned int ldo_volt_table5[] = {
  118. 1200000, 1300000, 1500000, 1800000, 2500000, 2800000, 3000000, 3300000,
  119. };
  120. static const unsigned int ldo_volt_table5_v2[] = {
  121. 1200000, 1000000, 1500000, 1800000, 2500000, 2800000, 3000000, 3300000,
  122. };
  123. static const unsigned int ldo_volt_table6[] = {
  124. 1200000, 1300000, 1500000, 1800000, 2500000, 2800000, 3000000, 2000000,
  125. };
  126. static const unsigned int ldo_volt_table7[] = {
  127. 1300000, 1500000, 1800000, 2000000, 2500000, 2800000, 3000000, 3300000,
  128. };
  129. static unsigned int mt6397_map_mode(unsigned int mode)
  130. {
  131. switch (mode) {
  132. case MT6397_BUCK_MODE_AUTO:
  133. return REGULATOR_MODE_NORMAL;
  134. case MT6397_BUCK_MODE_FORCE_PWM:
  135. return REGULATOR_MODE_FAST;
  136. default:
  137. return REGULATOR_MODE_INVALID;
  138. }
  139. }
  140. static int mt6397_regulator_set_mode(struct regulator_dev *rdev,
  141. unsigned int mode)
  142. {
  143. struct mt6397_regulator_info *info = rdev_get_drvdata(rdev);
  144. int ret, val;
  145. switch (mode) {
  146. case REGULATOR_MODE_FAST:
  147. val = MT6397_BUCK_MODE_FORCE_PWM;
  148. break;
  149. case REGULATOR_MODE_NORMAL:
  150. val = MT6397_BUCK_MODE_AUTO;
  151. break;
  152. default:
  153. ret = -EINVAL;
  154. goto err_mode;
  155. }
  156. dev_dbg(&rdev->dev, "mt6397 buck set_mode %#x, %#x, %#x\n",
  157. info->modeset_reg, info->modeset_mask, val);
  158. val <<= ffs(info->modeset_mask) - 1;
  159. ret = regmap_update_bits(rdev->regmap, info->modeset_reg,
  160. info->modeset_mask, val);
  161. err_mode:
  162. if (ret != 0) {
  163. dev_err(&rdev->dev,
  164. "Failed to set mt6397 buck mode: %d\n", ret);
  165. return ret;
  166. }
  167. return 0;
  168. }
  169. static unsigned int mt6397_regulator_get_mode(struct regulator_dev *rdev)
  170. {
  171. struct mt6397_regulator_info *info = rdev_get_drvdata(rdev);
  172. int ret, regval;
  173. ret = regmap_read(rdev->regmap, info->modeset_reg, &regval);
  174. if (ret != 0) {
  175. dev_err(&rdev->dev,
  176. "Failed to get mt6397 buck mode: %d\n", ret);
  177. return ret;
  178. }
  179. regval &= info->modeset_mask;
  180. regval >>= ffs(info->modeset_mask) - 1;
  181. switch (regval) {
  182. case MT6397_BUCK_MODE_AUTO:
  183. return REGULATOR_MODE_NORMAL;
  184. case MT6397_BUCK_MODE_FORCE_PWM:
  185. return REGULATOR_MODE_FAST;
  186. default:
  187. return -EINVAL;
  188. }
  189. }
  190. static int mt6397_get_status(struct regulator_dev *rdev)
  191. {
  192. int ret;
  193. u32 regval;
  194. struct mt6397_regulator_info *info = rdev_get_drvdata(rdev);
  195. ret = regmap_read(rdev->regmap, info->desc.enable_reg, &regval);
  196. if (ret != 0) {
  197. dev_err(&rdev->dev, "Failed to get enable reg: %d\n", ret);
  198. return ret;
  199. }
  200. return (regval & info->qi) ? REGULATOR_STATUS_ON : REGULATOR_STATUS_OFF;
  201. }
  202. static const struct regulator_ops mt6397_volt_range_ops = {
  203. .list_voltage = regulator_list_voltage_linear_range,
  204. .map_voltage = regulator_map_voltage_linear_range,
  205. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  206. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  207. .set_voltage_time_sel = regulator_set_voltage_time_sel,
  208. .enable = regulator_enable_regmap,
  209. .disable = regulator_disable_regmap,
  210. .is_enabled = regulator_is_enabled_regmap,
  211. .get_status = mt6397_get_status,
  212. .set_mode = mt6397_regulator_set_mode,
  213. .get_mode = mt6397_regulator_get_mode,
  214. };
  215. static const struct regulator_ops mt6397_volt_table_ops = {
  216. .list_voltage = regulator_list_voltage_table,
  217. .map_voltage = regulator_map_voltage_iterate,
  218. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  219. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  220. .set_voltage_time_sel = regulator_set_voltage_time_sel,
  221. .enable = regulator_enable_regmap,
  222. .disable = regulator_disable_regmap,
  223. .is_enabled = regulator_is_enabled_regmap,
  224. .get_status = mt6397_get_status,
  225. };
  226. static const struct regulator_ops mt6397_volt_fixed_ops = {
  227. .list_voltage = regulator_list_voltage_linear,
  228. .enable = regulator_enable_regmap,
  229. .disable = regulator_disable_regmap,
  230. .is_enabled = regulator_is_enabled_regmap,
  231. .get_status = mt6397_get_status,
  232. };
  233. /* The array is indexed by id(MT6397_ID_XXX) */
  234. static struct mt6397_regulator_info mt6397_regulators[] = {
  235. MT6397_BUCK("buck_vpca15", VPCA15, 700000, 1493750, 6250,
  236. buck_volt_range1, MT6397_VCA15_CON7, MT6397_VCA15_CON9, 0x7f,
  237. MT6397_VCA15_CON10, MT6397_VCA15_CON5, MT6397_VCA15_CON2, 11),
  238. MT6397_BUCK("buck_vpca7", VPCA7, 700000, 1493750, 6250,
  239. buck_volt_range1, MT6397_VPCA7_CON7, MT6397_VPCA7_CON9, 0x7f,
  240. MT6397_VPCA7_CON10, MT6397_VPCA7_CON5, MT6397_VPCA7_CON2, 8),
  241. MT6397_BUCK("buck_vsramca15", VSRAMCA15, 700000, 1493750, 6250,
  242. buck_volt_range1, MT6397_VSRMCA15_CON7, MT6397_VSRMCA15_CON9,
  243. 0x7f, MT6397_VSRMCA15_CON10, MT6397_VSRMCA15_CON5,
  244. MT6397_VSRMCA15_CON2, 8),
  245. MT6397_BUCK("buck_vsramca7", VSRAMCA7, 700000, 1493750, 6250,
  246. buck_volt_range1, MT6397_VSRMCA7_CON7, MT6397_VSRMCA7_CON9,
  247. 0x7f, MT6397_VSRMCA7_CON10, MT6397_VSRMCA7_CON5,
  248. MT6397_VSRMCA7_CON2, 8),
  249. MT6397_BUCK("buck_vcore", VCORE, 700000, 1493750, 6250,
  250. buck_volt_range1, MT6397_VCORE_CON7, MT6397_VCORE_CON9, 0x7f,
  251. MT6397_VCORE_CON10, MT6397_VCORE_CON5, MT6397_VCORE_CON2, 8),
  252. MT6397_BUCK("buck_vgpu", VGPU, 700000, 1493750, 6250, buck_volt_range1,
  253. MT6397_VGPU_CON7, MT6397_VGPU_CON9, 0x7f,
  254. MT6397_VGPU_CON10, MT6397_VGPU_CON5, MT6397_VGPU_CON2, 8),
  255. MT6397_BUCK("buck_vdrm", VDRM, 800000, 1593750, 6250, buck_volt_range2,
  256. MT6397_VDRM_CON7, MT6397_VDRM_CON9, 0x7f,
  257. MT6397_VDRM_CON10, MT6397_VDRM_CON5, MT6397_VDRM_CON2, 8),
  258. MT6397_BUCK("buck_vio18", VIO18, 1500000, 2120000, 20000,
  259. buck_volt_range3, MT6397_VIO18_CON7, MT6397_VIO18_CON9, 0x1f,
  260. MT6397_VIO18_CON10, MT6397_VIO18_CON5, MT6397_VIO18_CON2, 8),
  261. MT6397_REG_FIXED("ldo_vtcxo", VTCXO, MT6397_ANALDO_CON0, 10, 2800000),
  262. MT6397_REG_FIXED("ldo_va28", VA28, MT6397_ANALDO_CON1, 14, 2800000),
  263. MT6397_LDO("ldo_vcama", VCAMA, ldo_volt_table1,
  264. MT6397_ANALDO_CON2, 15, MT6397_ANALDO_CON6, 0xC0),
  265. MT6397_REG_FIXED("ldo_vio28", VIO28, MT6397_DIGLDO_CON0, 14, 2800000),
  266. MT6397_REG_FIXED("ldo_vusb", VUSB, MT6397_DIGLDO_CON1, 14, 3300000),
  267. MT6397_LDO("ldo_vmc", VMC, ldo_volt_table2,
  268. MT6397_DIGLDO_CON2, 12, MT6397_DIGLDO_CON29, 0x10),
  269. MT6397_LDO("ldo_vmch", VMCH, ldo_volt_table3,
  270. MT6397_DIGLDO_CON3, 14, MT6397_DIGLDO_CON17, 0x80),
  271. MT6397_LDO("ldo_vemc3v3", VEMC3V3, ldo_volt_table3,
  272. MT6397_DIGLDO_CON4, 14, MT6397_DIGLDO_CON18, 0x10),
  273. MT6397_LDO("ldo_vgp1", VGP1, ldo_volt_table4,
  274. MT6397_DIGLDO_CON5, 15, MT6397_DIGLDO_CON19, 0xE0),
  275. MT6397_LDO("ldo_vgp2", VGP2, ldo_volt_table5,
  276. MT6397_DIGLDO_CON6, 15, MT6397_DIGLDO_CON20, 0xE0),
  277. MT6397_LDO("ldo_vgp3", VGP3, ldo_volt_table5,
  278. MT6397_DIGLDO_CON7, 15, MT6397_DIGLDO_CON21, 0xE0),
  279. MT6397_LDO("ldo_vgp4", VGP4, ldo_volt_table5,
  280. MT6397_DIGLDO_CON8, 15, MT6397_DIGLDO_CON22, 0xE0),
  281. MT6397_LDO("ldo_vgp5", VGP5, ldo_volt_table6,
  282. MT6397_DIGLDO_CON9, 15, MT6397_DIGLDO_CON23, 0xE0),
  283. MT6397_LDO("ldo_vgp6", VGP6, ldo_volt_table5,
  284. MT6397_DIGLDO_CON10, 15, MT6397_DIGLDO_CON33, 0xE0),
  285. MT6397_LDO("ldo_vibr", VIBR, ldo_volt_table7,
  286. MT6397_DIGLDO_CON24, 15, MT6397_DIGLDO_CON25, 0xE00),
  287. };
  288. static int mt6397_set_buck_vosel_reg(struct platform_device *pdev)
  289. {
  290. struct mt6397_chip *mt6397 = dev_get_drvdata(pdev->dev.parent);
  291. int i;
  292. u32 regval;
  293. for (i = 0; i < MT6397_MAX_REGULATOR; i++) {
  294. if (mt6397_regulators[i].vselctrl_reg) {
  295. if (regmap_read(mt6397->regmap,
  296. mt6397_regulators[i].vselctrl_reg,
  297. &regval) < 0) {
  298. dev_err(&pdev->dev,
  299. "Failed to read buck ctrl\n");
  300. return -EIO;
  301. }
  302. if (regval & mt6397_regulators[i].vselctrl_mask) {
  303. mt6397_regulators[i].desc.vsel_reg =
  304. mt6397_regulators[i].vselon_reg;
  305. }
  306. }
  307. }
  308. return 0;
  309. }
  310. static int mt6397_regulator_probe(struct platform_device *pdev)
  311. {
  312. struct mt6397_chip *mt6397 = dev_get_drvdata(pdev->dev.parent);
  313. struct regulator_config config = {};
  314. struct regulator_dev *rdev;
  315. int i;
  316. u32 reg_value, version;
  317. /* Query buck controller to select activated voltage register part */
  318. if (mt6397_set_buck_vosel_reg(pdev))
  319. return -EIO;
  320. /* Read PMIC chip revision to update constraints and voltage table */
  321. if (regmap_read(mt6397->regmap, MT6397_CID, &reg_value) < 0) {
  322. dev_err(&pdev->dev, "Failed to read Chip ID\n");
  323. return -EIO;
  324. }
  325. dev_info(&pdev->dev, "Chip ID = 0x%x\n", reg_value);
  326. version = (reg_value & 0xFF);
  327. switch (version) {
  328. case MT6397_REGULATOR_ID91:
  329. mt6397_regulators[MT6397_ID_VGP2].desc.volt_table =
  330. ldo_volt_table5_v2;
  331. break;
  332. default:
  333. break;
  334. }
  335. for (i = 0; i < MT6397_MAX_REGULATOR; i++) {
  336. config.dev = &pdev->dev;
  337. config.driver_data = &mt6397_regulators[i];
  338. config.regmap = mt6397->regmap;
  339. rdev = devm_regulator_register(&pdev->dev,
  340. &mt6397_regulators[i].desc, &config);
  341. if (IS_ERR(rdev)) {
  342. dev_err(&pdev->dev, "failed to register %s\n",
  343. mt6397_regulators[i].desc.name);
  344. return PTR_ERR(rdev);
  345. }
  346. }
  347. return 0;
  348. }
  349. static const struct platform_device_id mt6397_platform_ids[] = {
  350. {"mt6397-regulator", 0},
  351. { /* sentinel */ },
  352. };
  353. MODULE_DEVICE_TABLE(platform, mt6397_platform_ids);
  354. static const struct of_device_id mt6397_of_match[] = {
  355. { .compatible = "mediatek,mt6397-regulator", },
  356. { /* sentinel */ },
  357. };
  358. MODULE_DEVICE_TABLE(of, mt6397_of_match);
  359. static struct platform_driver mt6397_regulator_driver = {
  360. .driver = {
  361. .name = "mt6397-regulator",
  362. .of_match_table = of_match_ptr(mt6397_of_match),
  363. },
  364. .probe = mt6397_regulator_probe,
  365. .id_table = mt6397_platform_ids,
  366. };
  367. module_platform_driver(mt6397_regulator_driver);
  368. MODULE_AUTHOR("Flora Fu <[email protected]>");
  369. MODULE_DESCRIPTION("Regulator Driver for MediaTek MT6397 PMIC");
  370. MODULE_LICENSE("GPL");