mt6323-regulator.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. /*
  2. * Copyright (c) 2016 MediaTek Inc.
  3. * Author: Chen Zhong <chen.zhong@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. #include <linux/module.h>
  10. #include <linux/of.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/regmap.h>
  13. #include <linux/mfd/mt6397/core.h>
  14. #include <linux/mfd/mt6323/registers.h>
  15. #include <linux/regulator/driver.h>
  16. #include <linux/regulator/machine.h>
  17. #include <linux/regulator/mt6323-regulator.h>
  18. #include <linux/regulator/of_regulator.h>
  19. #define MT6323_LDO_MODE_NORMAL 0
  20. #define MT6323_LDO_MODE_LP 1
  21. /*
  22. * MT6323 regulators' information
  23. *
  24. * @desc: standard fields of regulator description.
  25. * @qi: Mask for query enable signal status of regulators
  26. * @vselon_reg: Register sections for hardware control mode of bucks
  27. * @vselctrl_reg: Register for controlling the buck control mode.
  28. * @vselctrl_mask: Mask for query buck's voltage control mode.
  29. */
  30. struct mt6323_regulator_info {
  31. struct regulator_desc desc;
  32. u32 qi;
  33. u32 vselon_reg;
  34. u32 vselctrl_reg;
  35. u32 vselctrl_mask;
  36. u32 modeset_reg;
  37. u32 modeset_mask;
  38. };
  39. #define MT6323_BUCK(match, vreg, min, max, step, volt_ranges, enreg, \
  40. vosel, vosel_mask, voselon, vosel_ctrl) \
  41. [MT6323_ID_##vreg] = { \
  42. .desc = { \
  43. .name = #vreg, \
  44. .of_match = of_match_ptr(match), \
  45. .ops = &mt6323_volt_range_ops, \
  46. .type = REGULATOR_VOLTAGE, \
  47. .id = MT6323_ID_##vreg, \
  48. .owner = THIS_MODULE, \
  49. .n_voltages = (max - min)/step + 1, \
  50. .linear_ranges = volt_ranges, \
  51. .n_linear_ranges = ARRAY_SIZE(volt_ranges), \
  52. .vsel_reg = vosel, \
  53. .vsel_mask = vosel_mask, \
  54. .enable_reg = enreg, \
  55. .enable_mask = BIT(0), \
  56. }, \
  57. .qi = BIT(13), \
  58. .vselon_reg = voselon, \
  59. .vselctrl_reg = vosel_ctrl, \
  60. .vselctrl_mask = BIT(1), \
  61. }
  62. #define MT6323_LDO(match, vreg, ldo_volt_table, enreg, enbit, vosel, \
  63. vosel_mask, _modeset_reg, _modeset_mask) \
  64. [MT6323_ID_##vreg] = { \
  65. .desc = { \
  66. .name = #vreg, \
  67. .of_match = of_match_ptr(match), \
  68. .ops = &mt6323_volt_table_ops, \
  69. .type = REGULATOR_VOLTAGE, \
  70. .id = MT6323_ID_##vreg, \
  71. .owner = THIS_MODULE, \
  72. .n_voltages = ARRAY_SIZE(ldo_volt_table), \
  73. .volt_table = ldo_volt_table, \
  74. .vsel_reg = vosel, \
  75. .vsel_mask = vosel_mask, \
  76. .enable_reg = enreg, \
  77. .enable_mask = BIT(enbit), \
  78. }, \
  79. .qi = BIT(15), \
  80. .modeset_reg = _modeset_reg, \
  81. .modeset_mask = _modeset_mask, \
  82. }
  83. #define MT6323_REG_FIXED(match, vreg, enreg, enbit, volt, \
  84. _modeset_reg, _modeset_mask) \
  85. [MT6323_ID_##vreg] = { \
  86. .desc = { \
  87. .name = #vreg, \
  88. .of_match = of_match_ptr(match), \
  89. .ops = &mt6323_volt_fixed_ops, \
  90. .type = REGULATOR_VOLTAGE, \
  91. .id = MT6323_ID_##vreg, \
  92. .owner = THIS_MODULE, \
  93. .n_voltages = 1, \
  94. .enable_reg = enreg, \
  95. .enable_mask = BIT(enbit), \
  96. .min_uV = volt, \
  97. }, \
  98. .qi = BIT(15), \
  99. .modeset_reg = _modeset_reg, \
  100. .modeset_mask = _modeset_mask, \
  101. }
  102. static const struct regulator_linear_range buck_volt_range1[] = {
  103. REGULATOR_LINEAR_RANGE(700000, 0, 0x7f, 6250),
  104. };
  105. static const struct regulator_linear_range buck_volt_range2[] = {
  106. REGULATOR_LINEAR_RANGE(1400000, 0, 0x7f, 12500),
  107. };
  108. static const struct regulator_linear_range buck_volt_range3[] = {
  109. REGULATOR_LINEAR_RANGE(500000, 0, 0x3f, 50000),
  110. };
  111. static const u32 ldo_volt_table1[] = {
  112. 3300000, 3400000, 3500000, 3600000,
  113. };
  114. static const u32 ldo_volt_table2[] = {
  115. 1500000, 1800000, 2500000, 2800000,
  116. };
  117. static const u32 ldo_volt_table3[] = {
  118. 1800000, 3300000,
  119. };
  120. static const u32 ldo_volt_table4[] = {
  121. 3000000, 3300000,
  122. };
  123. static const u32 ldo_volt_table5[] = {
  124. 1200000, 1300000, 1500000, 1800000, 2000000, 2800000, 3000000, 3300000,
  125. };
  126. static const u32 ldo_volt_table6[] = {
  127. 1200000, 1300000, 1500000, 1800000, 2500000, 2800000, 3000000, 2000000,
  128. };
  129. static const u32 ldo_volt_table7[] = {
  130. 1200000, 1300000, 1500000, 1800000,
  131. };
  132. static const u32 ldo_volt_table8[] = {
  133. 1800000, 3000000,
  134. };
  135. static const u32 ldo_volt_table9[] = {
  136. 1200000, 1350000, 1500000, 1800000,
  137. };
  138. static const u32 ldo_volt_table10[] = {
  139. 1200000, 1300000, 1500000, 1800000,
  140. };
  141. static int mt6323_get_status(struct regulator_dev *rdev)
  142. {
  143. int ret;
  144. u32 regval;
  145. struct mt6323_regulator_info *info = rdev_get_drvdata(rdev);
  146. ret = regmap_read(rdev->regmap, info->desc.enable_reg, &regval);
  147. if (ret != 0) {
  148. dev_err(&rdev->dev, "Failed to get enable reg: %d\n", ret);
  149. return ret;
  150. }
  151. return (regval & info->qi) ? REGULATOR_STATUS_ON : REGULATOR_STATUS_OFF;
  152. }
  153. static int mt6323_ldo_set_mode(struct regulator_dev *rdev, unsigned int mode)
  154. {
  155. int ret, val = 0;
  156. struct mt6323_regulator_info *info = rdev_get_drvdata(rdev);
  157. if (!info->modeset_mask) {
  158. dev_err(&rdev->dev, "regulator %s doesn't support set_mode\n",
  159. info->desc.name);
  160. return -EINVAL;
  161. }
  162. switch (mode) {
  163. case REGULATOR_MODE_STANDBY:
  164. val = MT6323_LDO_MODE_LP;
  165. break;
  166. case REGULATOR_MODE_NORMAL:
  167. val = MT6323_LDO_MODE_NORMAL;
  168. break;
  169. default:
  170. return -EINVAL;
  171. }
  172. val <<= ffs(info->modeset_mask) - 1;
  173. ret = regmap_update_bits(rdev->regmap, info->modeset_reg,
  174. info->modeset_mask, val);
  175. return ret;
  176. }
  177. static unsigned int mt6323_ldo_get_mode(struct regulator_dev *rdev)
  178. {
  179. unsigned int val;
  180. unsigned int mode;
  181. int ret;
  182. struct mt6323_regulator_info *info = rdev_get_drvdata(rdev);
  183. if (!info->modeset_mask) {
  184. dev_err(&rdev->dev, "regulator %s doesn't support get_mode\n",
  185. info->desc.name);
  186. return -EINVAL;
  187. }
  188. ret = regmap_read(rdev->regmap, info->modeset_reg, &val);
  189. if (ret < 0)
  190. return ret;
  191. val &= info->modeset_mask;
  192. val >>= ffs(info->modeset_mask) - 1;
  193. if (val & 0x1)
  194. mode = REGULATOR_MODE_STANDBY;
  195. else
  196. mode = REGULATOR_MODE_NORMAL;
  197. return mode;
  198. }
  199. static const struct regulator_ops mt6323_volt_range_ops = {
  200. .list_voltage = regulator_list_voltage_linear_range,
  201. .map_voltage = regulator_map_voltage_linear_range,
  202. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  203. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  204. .set_voltage_time_sel = regulator_set_voltage_time_sel,
  205. .enable = regulator_enable_regmap,
  206. .disable = regulator_disable_regmap,
  207. .is_enabled = regulator_is_enabled_regmap,
  208. .get_status = mt6323_get_status,
  209. };
  210. static const struct regulator_ops mt6323_volt_table_ops = {
  211. .list_voltage = regulator_list_voltage_table,
  212. .map_voltage = regulator_map_voltage_iterate,
  213. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  214. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  215. .set_voltage_time_sel = regulator_set_voltage_time_sel,
  216. .enable = regulator_enable_regmap,
  217. .disable = regulator_disable_regmap,
  218. .is_enabled = regulator_is_enabled_regmap,
  219. .get_status = mt6323_get_status,
  220. .set_mode = mt6323_ldo_set_mode,
  221. .get_mode = mt6323_ldo_get_mode,
  222. };
  223. static const struct regulator_ops mt6323_volt_fixed_ops = {
  224. .list_voltage = regulator_list_voltage_linear,
  225. .enable = regulator_enable_regmap,
  226. .disable = regulator_disable_regmap,
  227. .is_enabled = regulator_is_enabled_regmap,
  228. .get_status = mt6323_get_status,
  229. .set_mode = mt6323_ldo_set_mode,
  230. .get_mode = mt6323_ldo_get_mode,
  231. };
  232. /* The array is indexed by id(MT6323_ID_XXX) */
  233. static struct mt6323_regulator_info mt6323_regulators[] = {
  234. MT6323_BUCK("buck_vproc", VPROC, 700000, 1493750, 6250,
  235. buck_volt_range1, MT6323_VPROC_CON7, MT6323_VPROC_CON9, 0x7f,
  236. MT6323_VPROC_CON10, MT6323_VPROC_CON5),
  237. MT6323_BUCK("buck_vsys", VSYS, 1400000, 2987500, 12500,
  238. buck_volt_range2, MT6323_VSYS_CON7, MT6323_VSYS_CON9, 0x7f,
  239. MT6323_VSYS_CON10, MT6323_VSYS_CON5),
  240. MT6323_BUCK("buck_vpa", VPA, 500000, 3650000, 50000,
  241. buck_volt_range3, MT6323_VPA_CON7, MT6323_VPA_CON9,
  242. 0x3f, MT6323_VPA_CON10, MT6323_VPA_CON5),
  243. MT6323_REG_FIXED("ldo_vtcxo", VTCXO, MT6323_ANALDO_CON1, 10, 2800000,
  244. MT6323_ANALDO_CON1, 0x2),
  245. MT6323_REG_FIXED("ldo_vcn28", VCN28, MT6323_ANALDO_CON19, 12, 2800000,
  246. MT6323_ANALDO_CON20, 0x2),
  247. MT6323_LDO("ldo_vcn33_bt", VCN33_BT, ldo_volt_table1,
  248. MT6323_ANALDO_CON16, 7, MT6323_ANALDO_CON16, 0xC,
  249. MT6323_ANALDO_CON21, 0x2),
  250. MT6323_LDO("ldo_vcn33_wifi", VCN33_WIFI, ldo_volt_table1,
  251. MT6323_ANALDO_CON17, 12, MT6323_ANALDO_CON16, 0xC,
  252. MT6323_ANALDO_CON21, 0x2),
  253. MT6323_REG_FIXED("ldo_va", VA, MT6323_ANALDO_CON2, 14, 2800000,
  254. MT6323_ANALDO_CON2, 0x2),
  255. MT6323_LDO("ldo_vcama", VCAMA, ldo_volt_table2,
  256. MT6323_ANALDO_CON4, 15, MT6323_ANALDO_CON10, 0x60, -1, 0),
  257. MT6323_REG_FIXED("ldo_vio28", VIO28, MT6323_DIGLDO_CON0, 14, 2800000,
  258. MT6323_DIGLDO_CON0, 0x2),
  259. MT6323_REG_FIXED("ldo_vusb", VUSB, MT6323_DIGLDO_CON2, 14, 3300000,
  260. MT6323_DIGLDO_CON2, 0x2),
  261. MT6323_LDO("ldo_vmc", VMC, ldo_volt_table3,
  262. MT6323_DIGLDO_CON3, 12, MT6323_DIGLDO_CON24, 0x10,
  263. MT6323_DIGLDO_CON3, 0x2),
  264. MT6323_LDO("ldo_vmch", VMCH, ldo_volt_table4,
  265. MT6323_DIGLDO_CON5, 14, MT6323_DIGLDO_CON26, 0x80,
  266. MT6323_DIGLDO_CON5, 0x2),
  267. MT6323_LDO("ldo_vemc3v3", VEMC3V3, ldo_volt_table4,
  268. MT6323_DIGLDO_CON6, 14, MT6323_DIGLDO_CON27, 0x80,
  269. MT6323_DIGLDO_CON6, 0x2),
  270. MT6323_LDO("ldo_vgp1", VGP1, ldo_volt_table5,
  271. MT6323_DIGLDO_CON7, 15, MT6323_DIGLDO_CON28, 0xE0,
  272. MT6323_DIGLDO_CON7, 0x2),
  273. MT6323_LDO("ldo_vgp2", VGP2, ldo_volt_table6,
  274. MT6323_DIGLDO_CON8, 15, MT6323_DIGLDO_CON29, 0xE0,
  275. MT6323_DIGLDO_CON8, 0x2),
  276. MT6323_LDO("ldo_vgp3", VGP3, ldo_volt_table7,
  277. MT6323_DIGLDO_CON9, 15, MT6323_DIGLDO_CON30, 0x60,
  278. MT6323_DIGLDO_CON9, 0x2),
  279. MT6323_REG_FIXED("ldo_vcn18", VCN18, MT6323_DIGLDO_CON11, 14, 1800000,
  280. MT6323_DIGLDO_CON11, 0x2),
  281. MT6323_LDO("ldo_vsim1", VSIM1, ldo_volt_table8,
  282. MT6323_DIGLDO_CON13, 15, MT6323_DIGLDO_CON34, 0x20,
  283. MT6323_DIGLDO_CON13, 0x2),
  284. MT6323_LDO("ldo_vsim2", VSIM2, ldo_volt_table8,
  285. MT6323_DIGLDO_CON14, 15, MT6323_DIGLDO_CON35, 0x20,
  286. MT6323_DIGLDO_CON14, 0x2),
  287. MT6323_REG_FIXED("ldo_vrtc", VRTC, MT6323_DIGLDO_CON15, 8, 2800000,
  288. -1, 0),
  289. MT6323_LDO("ldo_vcamaf", VCAMAF, ldo_volt_table5,
  290. MT6323_DIGLDO_CON31, 15, MT6323_DIGLDO_CON32, 0xE0,
  291. MT6323_DIGLDO_CON31, 0x2),
  292. MT6323_LDO("ldo_vibr", VIBR, ldo_volt_table5,
  293. MT6323_DIGLDO_CON39, 15, MT6323_DIGLDO_CON40, 0xE0,
  294. MT6323_DIGLDO_CON39, 0x2),
  295. MT6323_REG_FIXED("ldo_vrf18", VRF18, MT6323_DIGLDO_CON45, 15, 1825000,
  296. MT6323_DIGLDO_CON45, 0x2),
  297. MT6323_LDO("ldo_vm", VM, ldo_volt_table9,
  298. MT6323_DIGLDO_CON47, 14, MT6323_DIGLDO_CON48, 0x30,
  299. MT6323_DIGLDO_CON47, 0x2),
  300. MT6323_REG_FIXED("ldo_vio18", VIO18, MT6323_DIGLDO_CON49, 14, 1800000,
  301. MT6323_DIGLDO_CON49, 0x2),
  302. MT6323_LDO("ldo_vcamd", VCAMD, ldo_volt_table10,
  303. MT6323_DIGLDO_CON51, 14, MT6323_DIGLDO_CON52, 0x60,
  304. MT6323_DIGLDO_CON51, 0x2),
  305. MT6323_REG_FIXED("ldo_vcamio", VCAMIO, MT6323_DIGLDO_CON53, 14, 1800000,
  306. MT6323_DIGLDO_CON53, 0x2),
  307. };
  308. static int mt6323_set_buck_vosel_reg(struct platform_device *pdev)
  309. {
  310. struct mt6397_chip *mt6323 = dev_get_drvdata(pdev->dev.parent);
  311. int i;
  312. u32 regval;
  313. for (i = 0; i < MT6323_MAX_REGULATOR; i++) {
  314. if (mt6323_regulators[i].vselctrl_reg) {
  315. if (regmap_read(mt6323->regmap,
  316. mt6323_regulators[i].vselctrl_reg,
  317. &regval) < 0) {
  318. dev_err(&pdev->dev,
  319. "Failed to read buck ctrl\n");
  320. return -EIO;
  321. }
  322. if (regval & mt6323_regulators[i].vselctrl_mask) {
  323. mt6323_regulators[i].desc.vsel_reg =
  324. mt6323_regulators[i].vselon_reg;
  325. }
  326. }
  327. }
  328. return 0;
  329. }
  330. static int mt6323_regulator_probe(struct platform_device *pdev)
  331. {
  332. struct mt6397_chip *mt6323 = dev_get_drvdata(pdev->dev.parent);
  333. struct regulator_config config = {};
  334. struct regulator_dev *rdev;
  335. int i;
  336. u32 reg_value;
  337. /* Query buck controller to select activated voltage register part */
  338. if (mt6323_set_buck_vosel_reg(pdev))
  339. return -EIO;
  340. /* Read PMIC chip revision to update constraints and voltage table */
  341. if (regmap_read(mt6323->regmap, MT6323_CID, &reg_value) < 0) {
  342. dev_err(&pdev->dev, "Failed to read Chip ID\n");
  343. return -EIO;
  344. }
  345. dev_info(&pdev->dev, "Chip ID = 0x%x\n", reg_value);
  346. for (i = 0; i < MT6323_MAX_REGULATOR; i++) {
  347. config.dev = &pdev->dev;
  348. config.driver_data = &mt6323_regulators[i];
  349. config.regmap = mt6323->regmap;
  350. rdev = devm_regulator_register(&pdev->dev,
  351. &mt6323_regulators[i].desc, &config);
  352. if (IS_ERR(rdev)) {
  353. dev_err(&pdev->dev, "failed to register %s\n",
  354. mt6323_regulators[i].desc.name);
  355. return PTR_ERR(rdev);
  356. }
  357. }
  358. return 0;
  359. }
  360. static const struct platform_device_id mt6323_platform_ids[] = {
  361. {"mt6323-regulator", 0},
  362. { /* sentinel */ },
  363. };
  364. MODULE_DEVICE_TABLE(platform, mt6323_platform_ids);
  365. static struct platform_driver mt6323_regulator_driver = {
  366. .driver = {
  367. .name = "mt6323-regulator",
  368. },
  369. .probe = mt6323_regulator_probe,
  370. .id_table = mt6323_platform_ids,
  371. };
  372. module_platform_driver(mt6323_regulator_driver);
  373. MODULE_AUTHOR("Chen Zhong <chen.zhong@mediatek.com>");
  374. MODULE_DESCRIPTION("Regulator Driver for MediaTek MT6323 PMIC");
  375. MODULE_LICENSE("GPL v2");