mt6397-regulator.c 13 KB

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