mt6380-regulator.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. /*
  2. * Copyright (c) 2017 MediaTek Inc.
  3. * Author: Chenglin Xu <chenglin.xu@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/regulator/driver.h>
  19. #include <linux/regulator/machine.h>
  20. #include <linux/regulator/mt6380-regulator.h>
  21. #include <linux/regulator/of_regulator.h>
  22. /* PMIC Registers */
  23. #define MT6380_ALDO_CON_0 0x0000
  24. #define MT6380_BTLDO_CON_0 0x0004
  25. #define MT6380_COMP_CON_0 0x0008
  26. #define MT6380_CPUBUCK_CON_0 0x000C
  27. #define MT6380_CPUBUCK_CON_1 0x0010
  28. #define MT6380_CPUBUCK_CON_2 0x0014
  29. #define MT6380_DDRLDO_CON_0 0x0018
  30. #define MT6380_MLDO_CON_0 0x001C
  31. #define MT6380_PALDO_CON_0 0x0020
  32. #define MT6380_PHYLDO_CON_0 0x0024
  33. #define MT6380_SIDO_CON_0 0x0028
  34. #define MT6380_SIDO_CON_1 0x002C
  35. #define MT6380_SIDO_CON_2 0x0030
  36. #define MT6380_SLDO_CON_0 0x0034
  37. #define MT6380_TLDO_CON_0 0x0038
  38. #define MT6380_STARTUP_CON_0 0x003C
  39. #define MT6380_STARTUP_CON_1 0x0040
  40. #define MT6380_SMPS_TOP_CON_0 0x0044
  41. #define MT6380_SMPS_TOP_CON_1 0x0048
  42. #define MT6380_ANA_CTRL_0 0x0050
  43. #define MT6380_ANA_CTRL_1 0x0054
  44. #define MT6380_ANA_CTRL_2 0x0058
  45. #define MT6380_ANA_CTRL_3 0x005C
  46. #define MT6380_ANA_CTRL_4 0x0060
  47. #define MT6380_SPK_CON9 0x0064
  48. #define MT6380_SPK_CON11 0x0068
  49. #define MT6380_SPK_CON12 0x006A
  50. #define MT6380_CLK_CTRL 0x0070
  51. #define MT6380_PINMUX_CTRL 0x0074
  52. #define MT6380_IO_CTRL 0x0078
  53. #define MT6380_SLP_MODE_CTRL_0 0x007C
  54. #define MT6380_SLP_MODE_CTRL_1 0x0080
  55. #define MT6380_SLP_MODE_CTRL_2 0x0084
  56. #define MT6380_SLP_MODE_CTRL_3 0x0088
  57. #define MT6380_SLP_MODE_CTRL_4 0x008C
  58. #define MT6380_SLP_MODE_CTRL_5 0x0090
  59. #define MT6380_SLP_MODE_CTRL_6 0x0094
  60. #define MT6380_SLP_MODE_CTRL_7 0x0098
  61. #define MT6380_SLP_MODE_CTRL_8 0x009C
  62. #define MT6380_FCAL_CTRL_0 0x00A0
  63. #define MT6380_FCAL_CTRL_1 0x00A4
  64. #define MT6380_LDO_CTRL_0 0x00A8
  65. #define MT6380_LDO_CTRL_1 0x00AC
  66. #define MT6380_LDO_CTRL_2 0x00B0
  67. #define MT6380_LDO_CTRL_3 0x00B4
  68. #define MT6380_LDO_CTRL_4 0x00B8
  69. #define MT6380_DEBUG_CTRL_0 0x00BC
  70. #define MT6380_EFU_CTRL_0 0x0200
  71. #define MT6380_EFU_CTRL_1 0x0201
  72. #define MT6380_EFU_CTRL_2 0x0202
  73. #define MT6380_EFU_CTRL_3 0x0203
  74. #define MT6380_EFU_CTRL_4 0x0204
  75. #define MT6380_EFU_CTRL_5 0x0205
  76. #define MT6380_EFU_CTRL_6 0x0206
  77. #define MT6380_EFU_CTRL_7 0x0207
  78. #define MT6380_EFU_CTRL_8 0x0208
  79. #define MT6380_REGULATOR_MODE_AUTO 0
  80. #define MT6380_REGULATOR_MODE_FORCE_PWM 1
  81. /*
  82. * mt6380 regulators' information
  83. *
  84. * @desc: standard fields of regulator description
  85. * @vselon_reg: Register sections for hardware control mode of bucks
  86. * @modeset_reg: Register for controlling the buck/LDO control mode
  87. * @modeset_mask: Mask for controlling the buck/LDO control mode
  88. */
  89. struct mt6380_regulator_info {
  90. struct regulator_desc desc;
  91. u32 vselon_reg;
  92. u32 modeset_reg;
  93. u32 modeset_mask;
  94. };
  95. #define MT6380_BUCK(match, vreg, min, max, step, volt_ranges, enreg, \
  96. vosel, vosel_mask, enbit, voselon, _modeset_reg, \
  97. _modeset_mask) \
  98. [MT6380_ID_##vreg] = { \
  99. .desc = { \
  100. .name = #vreg, \
  101. .of_match = of_match_ptr(match), \
  102. .ops = &mt6380_volt_range_ops, \
  103. .type = REGULATOR_VOLTAGE, \
  104. .id = MT6380_ID_##vreg, \
  105. .owner = THIS_MODULE, \
  106. .n_voltages = ((max) - (min)) / (step) + 1, \
  107. .linear_ranges = volt_ranges, \
  108. .n_linear_ranges = ARRAY_SIZE(volt_ranges), \
  109. .vsel_reg = vosel, \
  110. .vsel_mask = vosel_mask, \
  111. .enable_reg = enreg, \
  112. .enable_mask = BIT(enbit), \
  113. }, \
  114. .vselon_reg = voselon, \
  115. .modeset_reg = _modeset_reg, \
  116. .modeset_mask = _modeset_mask, \
  117. }
  118. #define MT6380_LDO(match, vreg, ldo_volt_table, enreg, enbit, vosel, \
  119. vosel_mask, _modeset_reg, _modeset_mask) \
  120. [MT6380_ID_##vreg] = { \
  121. .desc = { \
  122. .name = #vreg, \
  123. .of_match = of_match_ptr(match), \
  124. .ops = &mt6380_volt_table_ops, \
  125. .type = REGULATOR_VOLTAGE, \
  126. .id = MT6380_ID_##vreg, \
  127. .owner = THIS_MODULE, \
  128. .n_voltages = ARRAY_SIZE(ldo_volt_table), \
  129. .volt_table = ldo_volt_table, \
  130. .vsel_reg = vosel, \
  131. .vsel_mask = vosel_mask, \
  132. .enable_reg = enreg, \
  133. .enable_mask = BIT(enbit), \
  134. }, \
  135. .modeset_reg = _modeset_reg, \
  136. .modeset_mask = _modeset_mask, \
  137. }
  138. #define MT6380_REG_FIXED(match, vreg, enreg, enbit, volt, \
  139. _modeset_reg, _modeset_mask) \
  140. [MT6380_ID_##vreg] = { \
  141. .desc = { \
  142. .name = #vreg, \
  143. .of_match = of_match_ptr(match), \
  144. .ops = &mt6380_volt_fixed_ops, \
  145. .type = REGULATOR_VOLTAGE, \
  146. .id = MT6380_ID_##vreg, \
  147. .owner = THIS_MODULE, \
  148. .n_voltages = 1, \
  149. .enable_reg = enreg, \
  150. .enable_mask = BIT(enbit), \
  151. .min_uV = volt, \
  152. }, \
  153. .modeset_reg = _modeset_reg, \
  154. .modeset_mask = _modeset_mask, \
  155. }
  156. static const struct regulator_linear_range buck_volt_range1[] = {
  157. REGULATOR_LINEAR_RANGE(600000, 0, 0xfe, 6250),
  158. };
  159. static const struct regulator_linear_range buck_volt_range2[] = {
  160. REGULATOR_LINEAR_RANGE(600000, 0, 0xfe, 6250),
  161. };
  162. static const struct regulator_linear_range buck_volt_range3[] = {
  163. REGULATOR_LINEAR_RANGE(1200000, 0, 0x3c, 25000),
  164. };
  165. static const u32 ldo_volt_table1[] = {
  166. 1400000, 1350000, 1300000, 1250000, 1200000, 1150000, 1100000, 1050000,
  167. };
  168. static const u32 ldo_volt_table2[] = {
  169. 2200000, 3300000,
  170. };
  171. static const u32 ldo_volt_table3[] = {
  172. 1240000, 1390000, 1540000, 1840000,
  173. };
  174. static const u32 ldo_volt_table4[] = {
  175. 2200000, 3300000,
  176. };
  177. static int mt6380_regulator_set_mode(struct regulator_dev *rdev,
  178. unsigned int mode)
  179. {
  180. int ret, val = 0;
  181. struct mt6380_regulator_info *info = rdev_get_drvdata(rdev);
  182. switch (mode) {
  183. case REGULATOR_MODE_NORMAL:
  184. val = MT6380_REGULATOR_MODE_AUTO;
  185. break;
  186. case REGULATOR_MODE_FAST:
  187. val = MT6380_REGULATOR_MODE_FORCE_PWM;
  188. break;
  189. default:
  190. return -EINVAL;
  191. }
  192. val <<= ffs(info->modeset_mask) - 1;
  193. ret = regmap_update_bits(rdev->regmap, info->modeset_reg,
  194. info->modeset_mask, val);
  195. return ret;
  196. }
  197. static unsigned int mt6380_regulator_get_mode(struct regulator_dev *rdev)
  198. {
  199. unsigned int val;
  200. unsigned int mode;
  201. int ret;
  202. struct mt6380_regulator_info *info = rdev_get_drvdata(rdev);
  203. ret = regmap_read(rdev->regmap, info->modeset_reg, &val);
  204. if (ret < 0)
  205. return ret;
  206. val &= info->modeset_mask;
  207. val >>= ffs(info->modeset_mask) - 1;
  208. switch (val) {
  209. case MT6380_REGULATOR_MODE_AUTO:
  210. mode = REGULATOR_MODE_NORMAL;
  211. break;
  212. case MT6380_REGULATOR_MODE_FORCE_PWM:
  213. mode = REGULATOR_MODE_FAST;
  214. break;
  215. default:
  216. return -EINVAL;
  217. }
  218. return mode;
  219. }
  220. static const struct regulator_ops mt6380_volt_range_ops = {
  221. .list_voltage = regulator_list_voltage_linear_range,
  222. .map_voltage = regulator_map_voltage_linear_range,
  223. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  224. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  225. .set_voltage_time_sel = regulator_set_voltage_time_sel,
  226. .enable = regulator_enable_regmap,
  227. .disable = regulator_disable_regmap,
  228. .is_enabled = regulator_is_enabled_regmap,
  229. .set_mode = mt6380_regulator_set_mode,
  230. .get_mode = mt6380_regulator_get_mode,
  231. };
  232. static const struct regulator_ops mt6380_volt_table_ops = {
  233. .list_voltage = regulator_list_voltage_table,
  234. .map_voltage = regulator_map_voltage_iterate,
  235. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  236. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  237. .set_voltage_time_sel = regulator_set_voltage_time_sel,
  238. .enable = regulator_enable_regmap,
  239. .disable = regulator_disable_regmap,
  240. .is_enabled = regulator_is_enabled_regmap,
  241. .set_mode = mt6380_regulator_set_mode,
  242. .get_mode = mt6380_regulator_get_mode,
  243. };
  244. static const struct regulator_ops mt6380_volt_fixed_ops = {
  245. .list_voltage = regulator_list_voltage_linear,
  246. .enable = regulator_enable_regmap,
  247. .disable = regulator_disable_regmap,
  248. .is_enabled = regulator_is_enabled_regmap,
  249. .set_mode = mt6380_regulator_set_mode,
  250. .get_mode = mt6380_regulator_get_mode,
  251. };
  252. /* The array is indexed by id(MT6380_ID_XXX) */
  253. static struct mt6380_regulator_info mt6380_regulators[] = {
  254. MT6380_BUCK("buck-vcore1", VCPU, 600000, 1393750, 6250,
  255. buck_volt_range1, MT6380_ANA_CTRL_3, MT6380_ANA_CTRL_1,
  256. 0xfe, 3, MT6380_ANA_CTRL_1,
  257. MT6380_CPUBUCK_CON_0, 0x8000000),
  258. MT6380_BUCK("buck-vcore", VCORE, 600000, 1393750, 6250,
  259. buck_volt_range2, MT6380_ANA_CTRL_3, MT6380_ANA_CTRL_2,
  260. 0xfe, 2, MT6380_ANA_CTRL_2, MT6380_SIDO_CON_0, 0x1000000),
  261. MT6380_BUCK("buck-vrf", VRF, 1200000, 1575000, 25000,
  262. buck_volt_range3, MT6380_ANA_CTRL_3, MT6380_SIDO_CON_0,
  263. 0x78, 1, MT6380_SIDO_CON_0, MT6380_SIDO_CON_0, 0x8000),
  264. MT6380_LDO("ldo-vm", VMLDO, ldo_volt_table1, MT6380_LDO_CTRL_0,
  265. 1, MT6380_MLDO_CON_0, 0xE000, MT6380_ANA_CTRL_1, 0x4000000),
  266. MT6380_LDO("ldo-va", VALDO, ldo_volt_table2, MT6380_LDO_CTRL_0,
  267. 2, MT6380_ALDO_CON_0, 0x400, MT6380_ALDO_CON_0, 0x20),
  268. MT6380_REG_FIXED("ldo-vphy", VPHYLDO, MT6380_LDO_CTRL_0, 7, 1800000,
  269. MT6380_PHYLDO_CON_0, 0x80),
  270. MT6380_LDO("ldo-vddr", VDDRLDO, ldo_volt_table3, MT6380_LDO_CTRL_0,
  271. 8, MT6380_DDRLDO_CON_0, 0x3000, MT6380_DDRLDO_CON_0, 0x80),
  272. MT6380_LDO("ldo-vt", VTLDO, ldo_volt_table4, MT6380_LDO_CTRL_0, 3,
  273. MT6380_TLDO_CON_0, 0x400, MT6380_TLDO_CON_0, 0x20),
  274. };
  275. static int mt6380_regulator_probe(struct platform_device *pdev)
  276. {
  277. struct regmap *regmap = dev_get_regmap(pdev->dev.parent, NULL);
  278. struct regulator_config config = {};
  279. struct regulator_dev *rdev;
  280. int i;
  281. for (i = 0; i < MT6380_MAX_REGULATOR; i++) {
  282. config.dev = &pdev->dev;
  283. config.driver_data = &mt6380_regulators[i];
  284. config.regmap = regmap;
  285. rdev = devm_regulator_register(&pdev->dev,
  286. &mt6380_regulators[i].desc,
  287. &config);
  288. if (IS_ERR(rdev)) {
  289. dev_err(&pdev->dev, "failed to register %s\n",
  290. mt6380_regulators[i].desc.name);
  291. return PTR_ERR(rdev);
  292. }
  293. }
  294. return 0;
  295. }
  296. static const struct platform_device_id mt6380_platform_ids[] = {
  297. {"mt6380-regulator", 0},
  298. { /* sentinel */ },
  299. };
  300. MODULE_DEVICE_TABLE(platform, mt6380_platform_ids);
  301. static const struct of_device_id mt6380_of_match[] = {
  302. { .compatible = "mediatek,mt6380-regulator", },
  303. { /* sentinel */ },
  304. };
  305. MODULE_DEVICE_TABLE(of, mt6380_of_match);
  306. static struct platform_driver mt6380_regulator_driver = {
  307. .driver = {
  308. .name = "mt6380-regulator",
  309. .of_match_table = of_match_ptr(mt6380_of_match),
  310. },
  311. .probe = mt6380_regulator_probe,
  312. .id_table = mt6380_platform_ids,
  313. };
  314. module_platform_driver(mt6380_regulator_driver);
  315. MODULE_AUTHOR("Chenglin Xu <chenglin.xu@mediatek.com>");
  316. MODULE_DESCRIPTION("Regulator Driver for MediaTek MT6380 PMIC");
  317. MODULE_LICENSE("GPL v2");