mt6397-regulator.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /*
  2. * Copyright (c) 2014 MediaTek Inc.
  3. * Author: Flora Fu <flora.fu@mediatek.com>
  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. /*
  25. * MT6397 regulators' information
  26. *
  27. * @desc: standard fields of regulator description.
  28. * @qi: Mask for query enable signal status of regulators
  29. * @vselon_reg: Register sections for hardware control mode of bucks
  30. * @vselctrl_reg: Register for controlling the buck control mode.
  31. * @vselctrl_mask: Mask for query buck's voltage control mode.
  32. */
  33. struct mt6397_regulator_info {
  34. struct regulator_desc desc;
  35. u32 qi;
  36. u32 vselon_reg;
  37. u32 vselctrl_reg;
  38. u32 vselctrl_mask;
  39. };
  40. #define MT6397_BUCK(match, vreg, min, max, step, volt_ranges, enreg, \
  41. vosel, vosel_mask, voselon, vosel_ctrl) \
  42. [MT6397_ID_##vreg] = { \
  43. .desc = { \
  44. .name = #vreg, \
  45. .of_match = of_match_ptr(match), \
  46. .ops = &mt6397_volt_range_ops, \
  47. .type = REGULATOR_VOLTAGE, \
  48. .id = MT6397_ID_##vreg, \
  49. .owner = THIS_MODULE, \
  50. .n_voltages = (max - min)/step + 1, \
  51. .linear_ranges = volt_ranges, \
  52. .n_linear_ranges = ARRAY_SIZE(volt_ranges), \
  53. .vsel_reg = vosel, \
  54. .vsel_mask = vosel_mask, \
  55. .enable_reg = enreg, \
  56. .enable_mask = BIT(0), \
  57. }, \
  58. .qi = BIT(13), \
  59. .vselon_reg = voselon, \
  60. .vselctrl_reg = vosel_ctrl, \
  61. .vselctrl_mask = BIT(1), \
  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 u32 ldo_volt_table1[] = {
  108. 1500000, 1800000, 2500000, 2800000,
  109. };
  110. static const u32 ldo_volt_table2[] = {
  111. 1800000, 3300000,
  112. };
  113. static const u32 ldo_volt_table3[] = {
  114. 3000000, 3300000,
  115. };
  116. static const u32 ldo_volt_table4[] = {
  117. 1220000, 1300000, 1500000, 1800000, 2500000, 2800000, 3000000, 3300000,
  118. };
  119. static const u32 ldo_volt_table5[] = {
  120. 1200000, 1300000, 1500000, 1800000, 2500000, 2800000, 3000000, 3300000,
  121. };
  122. static const u32 ldo_volt_table5_v2[] = {
  123. 1200000, 1000000, 1500000, 1800000, 2500000, 2800000, 3000000, 3300000,
  124. };
  125. static const u32 ldo_volt_table6[] = {
  126. 1200000, 1300000, 1500000, 1800000, 2500000, 2800000, 3000000, 2000000,
  127. };
  128. static const u32 ldo_volt_table7[] = {
  129. 1300000, 1500000, 1800000, 2000000, 2500000, 2800000, 3000000, 3300000,
  130. };
  131. static int mt6397_get_status(struct regulator_dev *rdev)
  132. {
  133. int ret;
  134. u32 regval;
  135. struct mt6397_regulator_info *info = rdev_get_drvdata(rdev);
  136. ret = regmap_read(rdev->regmap, info->desc.enable_reg, &regval);
  137. if (ret != 0) {
  138. dev_err(&rdev->dev, "Failed to get enable reg: %d\n", ret);
  139. return ret;
  140. }
  141. return (regval & info->qi) ? REGULATOR_STATUS_ON : REGULATOR_STATUS_OFF;
  142. }
  143. static struct regulator_ops mt6397_volt_range_ops = {
  144. .list_voltage = regulator_list_voltage_linear_range,
  145. .map_voltage = regulator_map_voltage_linear_range,
  146. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  147. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  148. .set_voltage_time_sel = regulator_set_voltage_time_sel,
  149. .enable = regulator_enable_regmap,
  150. .disable = regulator_disable_regmap,
  151. .is_enabled = regulator_is_enabled_regmap,
  152. .get_status = mt6397_get_status,
  153. };
  154. static struct regulator_ops mt6397_volt_table_ops = {
  155. .list_voltage = regulator_list_voltage_table,
  156. .map_voltage = regulator_map_voltage_iterate,
  157. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  158. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  159. .set_voltage_time_sel = regulator_set_voltage_time_sel,
  160. .enable = regulator_enable_regmap,
  161. .disable = regulator_disable_regmap,
  162. .is_enabled = regulator_is_enabled_regmap,
  163. .get_status = mt6397_get_status,
  164. };
  165. static struct regulator_ops mt6397_volt_fixed_ops = {
  166. .list_voltage = regulator_list_voltage_linear,
  167. .enable = regulator_enable_regmap,
  168. .disable = regulator_disable_regmap,
  169. .is_enabled = regulator_is_enabled_regmap,
  170. .get_status = mt6397_get_status,
  171. };
  172. /* The array is indexed by id(MT6397_ID_XXX) */
  173. static struct mt6397_regulator_info mt6397_regulators[] = {
  174. MT6397_BUCK("buck_vpca15", VPCA15, 700000, 1493750, 6250,
  175. buck_volt_range1, MT6397_VCA15_CON7, MT6397_VCA15_CON9, 0x7f,
  176. MT6397_VCA15_CON10, MT6397_VCA15_CON5),
  177. MT6397_BUCK("buck_vpca7", VPCA7, 700000, 1493750, 6250,
  178. buck_volt_range1, MT6397_VPCA7_CON7, MT6397_VPCA7_CON9, 0x7f,
  179. MT6397_VPCA7_CON10, MT6397_VPCA7_CON5),
  180. MT6397_BUCK("buck_vsramca15", VSRAMCA15, 700000, 1493750, 6250,
  181. buck_volt_range1, MT6397_VSRMCA15_CON7, MT6397_VSRMCA15_CON9,
  182. 0x7f, MT6397_VSRMCA15_CON10, MT6397_VSRMCA15_CON5),
  183. MT6397_BUCK("buck_vsramca7", VSRAMCA7, 700000, 1493750, 6250,
  184. buck_volt_range1, MT6397_VSRMCA7_CON7, MT6397_VSRMCA7_CON9,
  185. 0x7f, MT6397_VSRMCA7_CON10, MT6397_VSRMCA7_CON5),
  186. MT6397_BUCK("buck_vcore", VCORE, 700000, 1493750, 6250,
  187. buck_volt_range1, MT6397_VCORE_CON7, MT6397_VCORE_CON9, 0x7f,
  188. MT6397_VCORE_CON10, MT6397_VCORE_CON5),
  189. MT6397_BUCK("buck_vgpu", VGPU, 700000, 1493750, 6250, buck_volt_range1,
  190. MT6397_VGPU_CON7, MT6397_VGPU_CON9, 0x7f,
  191. MT6397_VGPU_CON10, MT6397_VGPU_CON5),
  192. MT6397_BUCK("buck_vdrm", VDRM, 800000, 1593750, 6250, buck_volt_range2,
  193. MT6397_VDRM_CON7, MT6397_VDRM_CON9, 0x7f,
  194. MT6397_VDRM_CON10, MT6397_VDRM_CON5),
  195. MT6397_BUCK("buck_vio18", VIO18, 1500000, 2120000, 20000,
  196. buck_volt_range3, MT6397_VIO18_CON7, MT6397_VIO18_CON9, 0x1f,
  197. MT6397_VIO18_CON10, MT6397_VIO18_CON5),
  198. MT6397_REG_FIXED("ldo_vtcxo", VTCXO, MT6397_ANALDO_CON0, 10, 2800000),
  199. MT6397_REG_FIXED("ldo_va28", VA28, MT6397_ANALDO_CON1, 14, 2800000),
  200. MT6397_LDO("ldo_vcama", VCAMA, ldo_volt_table1,
  201. MT6397_ANALDO_CON2, 15, MT6397_ANALDO_CON6, 0xC0),
  202. MT6397_REG_FIXED("ldo_vio28", VIO28, MT6397_DIGLDO_CON0, 14, 2800000),
  203. MT6397_REG_FIXED("ldo_vusb", VUSB, MT6397_DIGLDO_CON1, 14, 3300000),
  204. MT6397_LDO("ldo_vmc", VMC, ldo_volt_table2,
  205. MT6397_DIGLDO_CON2, 12, MT6397_DIGLDO_CON29, 0x10),
  206. MT6397_LDO("ldo_vmch", VMCH, ldo_volt_table3,
  207. MT6397_DIGLDO_CON3, 14, MT6397_DIGLDO_CON17, 0x80),
  208. MT6397_LDO("ldo_vemc3v3", VEMC3V3, ldo_volt_table3,
  209. MT6397_DIGLDO_CON4, 14, MT6397_DIGLDO_CON18, 0x10),
  210. MT6397_LDO("ldo_vgp1", VGP1, ldo_volt_table4,
  211. MT6397_DIGLDO_CON5, 15, MT6397_DIGLDO_CON19, 0xE0),
  212. MT6397_LDO("ldo_vgp2", VGP2, ldo_volt_table5,
  213. MT6397_DIGLDO_CON6, 15, MT6397_DIGLDO_CON20, 0xE0),
  214. MT6397_LDO("ldo_vgp3", VGP3, ldo_volt_table5,
  215. MT6397_DIGLDO_CON7, 15, MT6397_DIGLDO_CON21, 0xE0),
  216. MT6397_LDO("ldo_vgp4", VGP4, ldo_volt_table5,
  217. MT6397_DIGLDO_CON8, 15, MT6397_DIGLDO_CON22, 0xE0),
  218. MT6397_LDO("ldo_vgp5", VGP5, ldo_volt_table6,
  219. MT6397_DIGLDO_CON9, 15, MT6397_DIGLDO_CON23, 0xE0),
  220. MT6397_LDO("ldo_vgp6", VGP6, ldo_volt_table5,
  221. MT6397_DIGLDO_CON10, 15, MT6397_DIGLDO_CON33, 0xE0),
  222. MT6397_LDO("ldo_vibr", VIBR, ldo_volt_table7,
  223. MT6397_DIGLDO_CON24, 15, MT6397_DIGLDO_CON25, 0xE00),
  224. };
  225. static int mt6397_set_buck_vosel_reg(struct platform_device *pdev)
  226. {
  227. struct mt6397_chip *mt6397 = dev_get_drvdata(pdev->dev.parent);
  228. int i;
  229. u32 regval;
  230. for (i = 0; i < MT6397_MAX_REGULATOR; i++) {
  231. if (mt6397_regulators[i].vselctrl_reg) {
  232. if (regmap_read(mt6397->regmap,
  233. mt6397_regulators[i].vselctrl_reg,
  234. &regval) < 0) {
  235. dev_err(&pdev->dev,
  236. "Failed to read buck ctrl\n");
  237. return -EIO;
  238. }
  239. if (regval & mt6397_regulators[i].vselctrl_mask) {
  240. mt6397_regulators[i].desc.vsel_reg =
  241. mt6397_regulators[i].vselon_reg;
  242. }
  243. }
  244. }
  245. return 0;
  246. }
  247. static int mt6397_regulator_probe(struct platform_device *pdev)
  248. {
  249. struct mt6397_chip *mt6397 = dev_get_drvdata(pdev->dev.parent);
  250. struct regulator_config config = {};
  251. struct regulator_dev *rdev;
  252. int i;
  253. u32 reg_value, version;
  254. /* Query buck controller to select activated voltage register part */
  255. if (mt6397_set_buck_vosel_reg(pdev))
  256. return -EIO;
  257. /* Read PMIC chip revision to update constraints and voltage table */
  258. if (regmap_read(mt6397->regmap, MT6397_CID, &reg_value) < 0) {
  259. dev_err(&pdev->dev, "Failed to read Chip ID\n");
  260. return -EIO;
  261. }
  262. dev_info(&pdev->dev, "Chip ID = 0x%x\n", reg_value);
  263. version = (reg_value & 0xFF);
  264. switch (version) {
  265. case MT6397_REGULATOR_ID91:
  266. mt6397_regulators[MT6397_ID_VGP2].desc.volt_table =
  267. ldo_volt_table5_v2;
  268. break;
  269. default:
  270. break;
  271. }
  272. for (i = 0; i < MT6397_MAX_REGULATOR; i++) {
  273. config.dev = &pdev->dev;
  274. config.driver_data = &mt6397_regulators[i];
  275. config.regmap = mt6397->regmap;
  276. rdev = devm_regulator_register(&pdev->dev,
  277. &mt6397_regulators[i].desc, &config);
  278. if (IS_ERR(rdev)) {
  279. dev_err(&pdev->dev, "failed to register %s\n",
  280. mt6397_regulators[i].desc.name);
  281. return PTR_ERR(rdev);
  282. }
  283. }
  284. return 0;
  285. }
  286. static struct platform_driver mt6397_regulator_driver = {
  287. .driver = {
  288. .name = "mt6397-regulator",
  289. },
  290. .probe = mt6397_regulator_probe,
  291. };
  292. module_platform_driver(mt6397_regulator_driver);
  293. MODULE_AUTHOR("Flora Fu <flora.fu@mediatek.com>");
  294. MODULE_DESCRIPTION("Regulator Driver for MediaTek MT6397 PMIC");
  295. MODULE_LICENSE("GPL");
  296. MODULE_ALIAS("platform:mt6397-regulator");