mt6397-regulator.c 13 KB

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