mt6392-regulator.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. /*
  2. * Copyright (c) 2019 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/mt6392/registers.h>
  15. #include <linux/regulator/driver.h>
  16. #include <linux/regulator/machine.h>
  17. #include <linux/regulator/mt6392-regulator.h>
  18. #include <linux/regulator/of_regulator.h>
  19. #define MT6392_BUCK_MODE_AUTO 0
  20. #define MT6392_BUCK_MODE_FORCE_PWM 1
  21. #define MT6392_LDO_MODE_NORMAL 0
  22. #define MT6392_LDO_MODE_LP 1
  23. /*
  24. * MT6392 regulators' information
  25. *
  26. * @desc: standard fields of regulator description.
  27. * @qi: Mask for query enable signal status of regulators
  28. * @vselon_reg: Register sections for hardware control mode of bucks
  29. * @vselctrl_reg: Register for controlling the buck control mode.
  30. * @vselctrl_mask: Mask for query buck's voltage control mode.
  31. */
  32. struct mt6392_regulator_info {
  33. struct regulator_desc desc;
  34. u32 qi;
  35. u32 vselon_reg;
  36. u32 vselctrl_reg;
  37. u32 vselctrl_mask;
  38. u32 modeset_reg;
  39. u32 modeset_mask;
  40. };
  41. #define MT6392_BUCK(match, vreg, min, max, step, volt_ranges, enreg, \
  42. vosel, vosel_mask, voselon, vosel_ctrl, \
  43. _modeset_reg, _modeset_mask) \
  44. [MT6392_ID_##vreg] = { \
  45. .desc = { \
  46. .name = #vreg, \
  47. .of_match = of_match_ptr(match), \
  48. .ops = &mt6392_volt_range_ops, \
  49. .type = REGULATOR_VOLTAGE, \
  50. .id = MT6392_ID_##vreg, \
  51. .owner = THIS_MODULE, \
  52. .n_voltages = (max - min)/step + 1, \
  53. .linear_ranges = volt_ranges, \
  54. .n_linear_ranges = ARRAY_SIZE(volt_ranges), \
  55. .vsel_reg = vosel, \
  56. .vsel_mask = vosel_mask, \
  57. .enable_reg = enreg, \
  58. .enable_mask = BIT(0), \
  59. }, \
  60. .qi = BIT(13), \
  61. .vselon_reg = voselon, \
  62. .vselctrl_reg = vosel_ctrl, \
  63. .vselctrl_mask = BIT(1), \
  64. .modeset_reg = _modeset_reg, \
  65. .modeset_mask = _modeset_mask, \
  66. }
  67. #define MT6392_LDO(match, vreg, ldo_volt_table, enreg, enbit, vosel, \
  68. vosel_mask, _modeset_reg, _modeset_mask) \
  69. [MT6392_ID_##vreg] = { \
  70. .desc = { \
  71. .name = #vreg, \
  72. .of_match = of_match_ptr(match), \
  73. .ops = &mt6392_volt_table_ops, \
  74. .type = REGULATOR_VOLTAGE, \
  75. .id = MT6392_ID_##vreg, \
  76. .owner = THIS_MODULE, \
  77. .n_voltages = ARRAY_SIZE(ldo_volt_table), \
  78. .volt_table = ldo_volt_table, \
  79. .vsel_reg = vosel, \
  80. .vsel_mask = vosel_mask, \
  81. .enable_reg = enreg, \
  82. .enable_mask = BIT(enbit), \
  83. }, \
  84. .qi = BIT(15), \
  85. .modeset_reg = _modeset_reg, \
  86. .modeset_mask = _modeset_mask, \
  87. }
  88. #define MT6392_REG_FIXED(match, vreg, enreg, enbit, volt, \
  89. _modeset_reg, _modeset_mask) \
  90. [MT6392_ID_##vreg] = { \
  91. .desc = { \
  92. .name = #vreg, \
  93. .of_match = of_match_ptr(match), \
  94. .ops = &mt6392_volt_fixed_ops, \
  95. .type = REGULATOR_VOLTAGE, \
  96. .id = MT6392_ID_##vreg, \
  97. .owner = THIS_MODULE, \
  98. .n_voltages = 1, \
  99. .enable_reg = enreg, \
  100. .enable_mask = BIT(enbit), \
  101. .min_uV = volt, \
  102. }, \
  103. .qi = BIT(15), \
  104. .modeset_reg = _modeset_reg, \
  105. .modeset_mask = _modeset_mask, \
  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(1400000, 0, 0x7f, 12500),
  112. };
  113. static const u32 ldo_volt_table1[] = {
  114. 1800000, 1900000, 2000000, 2200000,
  115. };
  116. static const u32 ldo_volt_table2[] = {
  117. 3300000, 3400000, 3500000, 3600000,
  118. };
  119. static const u32 ldo_volt_table3[] = {
  120. 1800000, 3300000,
  121. };
  122. static const u32 ldo_volt_table4[] = {
  123. 3000000, 3300000,
  124. };
  125. static const u32 ldo_volt_table5[] = {
  126. 1200000, 1300000, 1500000, 1800000, 2000000, 2800000, 3000000, 3300000,
  127. };
  128. static const u32 ldo_volt_table6[] = {
  129. 1240000, 1390000,
  130. };
  131. static const u32 ldo_volt_table7[] = {
  132. 1200000, 1300000, 1500000, 1800000,
  133. };
  134. static const u32 ldo_volt_table8[] = {
  135. 1800000, 2000000,
  136. };
  137. static int mt6392_get_status(struct regulator_dev *rdev)
  138. {
  139. int ret;
  140. u32 regval;
  141. struct mt6392_regulator_info *info = rdev_get_drvdata(rdev);
  142. ret = regmap_read(rdev->regmap, info->desc.enable_reg, &regval);
  143. if (ret != 0) {
  144. dev_info(&rdev->dev, "Failed to get enable reg: %d\n", ret);
  145. return ret;
  146. }
  147. return (regval & info->qi) ? REGULATOR_STATUS_ON : REGULATOR_STATUS_OFF;
  148. }
  149. static int mt6392_buck_set_mode(struct regulator_dev *rdev, unsigned int mode)
  150. {
  151. int ret, val = 0;
  152. struct mt6392_regulator_info *info = rdev_get_drvdata(rdev);
  153. u32 reg_value;
  154. if (!info->modeset_mask) {
  155. dev_info(&rdev->dev, "regulator %s doesn't support set_mode\n",
  156. info->desc.name);
  157. return -EINVAL;
  158. }
  159. switch (mode) {
  160. case REGULATOR_MODE_FAST:
  161. val = MT6392_BUCK_MODE_FORCE_PWM;
  162. break;
  163. case REGULATOR_MODE_NORMAL:
  164. val = MT6392_BUCK_MODE_AUTO;
  165. break;
  166. default:
  167. return -EINVAL;
  168. }
  169. val <<= ffs(info->modeset_mask) - 1;
  170. ret = regmap_update_bits(rdev->regmap, info->modeset_reg,
  171. info->modeset_mask, val);
  172. if (regmap_read(rdev->regmap, info->modeset_reg, &reg_value) < 0) {
  173. dev_info(&rdev->dev, "Failed to read register value\n");
  174. return -EIO;
  175. }
  176. dev_info(&rdev->dev, "info->modeset_reg 0x%x = 0x%x\n",
  177. info->modeset_reg, reg_value);
  178. return ret;
  179. }
  180. static unsigned int mt6392_buck_get_mode(struct regulator_dev *rdev)
  181. {
  182. unsigned int val;
  183. unsigned int mode;
  184. int ret;
  185. struct mt6392_regulator_info *info = rdev_get_drvdata(rdev);
  186. if (!info->modeset_mask) {
  187. dev_info(&rdev->dev, "regulator %s doesn't support get_mode\n",
  188. info->desc.name);
  189. return -EINVAL;
  190. }
  191. ret = regmap_read(rdev->regmap, info->modeset_reg, &val);
  192. if (ret < 0)
  193. return ret;
  194. val &= info->modeset_mask;
  195. val >>= ffs(info->modeset_mask) - 1;
  196. if (val & 0x1)
  197. mode = REGULATOR_MODE_FAST;
  198. else
  199. mode = REGULATOR_MODE_NORMAL;
  200. return mode;
  201. }
  202. static int mt6392_ldo_set_mode(struct regulator_dev *rdev, unsigned int mode)
  203. {
  204. int ret, val = 0;
  205. struct mt6392_regulator_info *info = rdev_get_drvdata(rdev);
  206. if (!info->modeset_mask) {
  207. dev_info(&rdev->dev, "regulator %s doesn't support set_mode\n",
  208. info->desc.name);
  209. return -EINVAL;
  210. }
  211. switch (mode) {
  212. case REGULATOR_MODE_STANDBY:
  213. val = MT6392_LDO_MODE_LP;
  214. break;
  215. case REGULATOR_MODE_NORMAL:
  216. val = MT6392_LDO_MODE_NORMAL;
  217. break;
  218. default:
  219. return -EINVAL;
  220. }
  221. val <<= ffs(info->modeset_mask) - 1;
  222. ret = regmap_update_bits(rdev->regmap, info->modeset_reg,
  223. info->modeset_mask, val);
  224. return ret;
  225. }
  226. static unsigned int mt6392_ldo_get_mode(struct regulator_dev *rdev)
  227. {
  228. unsigned int val;
  229. unsigned int mode;
  230. int ret;
  231. struct mt6392_regulator_info *info = rdev_get_drvdata(rdev);
  232. if (!info->modeset_mask) {
  233. dev_info(&rdev->dev, "regulator %s doesn't support get_mode\n",
  234. info->desc.name);
  235. return -EINVAL;
  236. }
  237. ret = regmap_read(rdev->regmap, info->modeset_reg, &val);
  238. if (ret < 0)
  239. return ret;
  240. val &= info->modeset_mask;
  241. val >>= ffs(info->modeset_mask) - 1;
  242. if (val & 0x1)
  243. mode = REGULATOR_MODE_STANDBY;
  244. else
  245. mode = REGULATOR_MODE_NORMAL;
  246. return mode;
  247. }
  248. static struct regulator_ops mt6392_volt_range_ops = {
  249. .list_voltage = regulator_list_voltage_linear_range,
  250. .map_voltage = regulator_map_voltage_linear_range,
  251. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  252. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  253. .set_voltage_time_sel = regulator_set_voltage_time_sel,
  254. .enable = regulator_enable_regmap,
  255. .disable = regulator_disable_regmap,
  256. .is_enabled = regulator_is_enabled_regmap,
  257. .get_status = mt6392_get_status,
  258. .set_mode = mt6392_buck_set_mode,
  259. .get_mode = mt6392_buck_get_mode,
  260. };
  261. static struct regulator_ops mt6392_volt_table_ops = {
  262. .list_voltage = regulator_list_voltage_table,
  263. .map_voltage = regulator_map_voltage_iterate,
  264. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  265. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  266. .set_voltage_time_sel = regulator_set_voltage_time_sel,
  267. .enable = regulator_enable_regmap,
  268. .disable = regulator_disable_regmap,
  269. .is_enabled = regulator_is_enabled_regmap,
  270. .get_status = mt6392_get_status,
  271. .set_mode = mt6392_ldo_set_mode,
  272. .get_mode = mt6392_ldo_get_mode,
  273. };
  274. static struct regulator_ops mt6392_volt_fixed_ops = {
  275. .list_voltage = regulator_list_voltage_linear,
  276. .enable = regulator_enable_regmap,
  277. .disable = regulator_disable_regmap,
  278. .is_enabled = regulator_is_enabled_regmap,
  279. .get_status = mt6392_get_status,
  280. .set_mode = mt6392_ldo_set_mode,
  281. .get_mode = mt6392_ldo_get_mode,
  282. };
  283. /* The array is indexed by id(MT6392_ID_XXX) */
  284. static struct mt6392_regulator_info mt6392_regulators[] = {
  285. MT6392_BUCK("buck_vproc", VPROC, 700000, 1493750, 6250,
  286. buck_volt_range1, MT6392_VPROC_CON7, MT6392_VPROC_CON9, 0x7f,
  287. MT6392_VPROC_CON10, MT6392_VPROC_CON5,
  288. MT6392_VPROC_CON2, 0x100),
  289. MT6392_BUCK("buck_vsys", VSYS, 1400000, 2987500, 12500,
  290. buck_volt_range2, MT6392_VSYS_CON7, MT6392_VSYS_CON9, 0x7f,
  291. MT6392_VSYS_CON10, MT6392_VSYS_CON5, MT6392_VSYS_CON2, 0x100),
  292. MT6392_BUCK("buck_vcore", VCORE, 700000, 1493750, 6250,
  293. buck_volt_range1, MT6392_VCORE_CON7, MT6392_VCORE_CON9, 0x7f,
  294. MT6392_VCORE_CON10, MT6392_VCORE_CON5,
  295. MT6392_VCORE_CON2, 0x100),
  296. MT6392_REG_FIXED("ldo_vxo22", VXO22, MT6392_ANALDO_CON1, 10, 2200000,
  297. MT6392_ANALDO_CON1, 0x2),
  298. MT6392_LDO("ldo_vaud22", VAUD22, ldo_volt_table1,
  299. MT6392_ANALDO_CON2, 14, MT6392_ANALDO_CON8, 0x60,
  300. MT6392_ANALDO_CON2, 0x2),
  301. MT6392_REG_FIXED("ldo_vcama", VCAMA, MT6392_ANALDO_CON4, 15, 2800000,
  302. -1, 0),
  303. MT6392_REG_FIXED("ldo_vaud28", VAUD28, MT6392_ANALDO_CON23, 14, 2800000,
  304. MT6392_ANALDO_CON23, 0x2),
  305. MT6392_REG_FIXED("ldo_vadc18", VADC18, MT6392_ANALDO_CON25, 14, 1800000,
  306. MT6392_ANALDO_CON25, 0x2),
  307. MT6392_LDO("ldo_vcn35", VCN35, ldo_volt_table2,
  308. MT6392_ANALDO_CON21, 12, MT6392_ANALDO_CON16, 0xC,
  309. MT6392_ANALDO_CON21, 0x2),
  310. MT6392_REG_FIXED("ldo_vio28", VIO28, MT6392_DIGLDO_CON0, 14, 2800000,
  311. MT6392_DIGLDO_CON0, 0x2),
  312. MT6392_REG_FIXED("ldo_vusb", VUSB, MT6392_DIGLDO_CON2, 14, 3300000,
  313. MT6392_DIGLDO_CON2, 0x2),
  314. MT6392_LDO("ldo_vmc", VMC, ldo_volt_table3,
  315. MT6392_DIGLDO_CON3, 12, MT6392_DIGLDO_CON24, 0x10,
  316. MT6392_DIGLDO_CON3, 0x2),
  317. MT6392_LDO("ldo_vmch", VMCH, ldo_volt_table4,
  318. MT6392_DIGLDO_CON5, 14, MT6392_DIGLDO_CON26, 0x80,
  319. MT6392_DIGLDO_CON5, 0x2),
  320. MT6392_LDO("ldo_vemc3v3", VEMC3V3, ldo_volt_table4,
  321. MT6392_DIGLDO_CON6, 14, MT6392_DIGLDO_CON27, 0x80,
  322. MT6392_DIGLDO_CON6, 0x2),
  323. MT6392_LDO("ldo_vgp1", VGP1, ldo_volt_table5,
  324. MT6392_DIGLDO_CON7, 15, MT6392_DIGLDO_CON28, 0xE0,
  325. MT6392_DIGLDO_CON7, 0x2),
  326. MT6392_LDO("ldo_vgp2", VGP2, ldo_volt_table5,
  327. MT6392_DIGLDO_CON8, 15, MT6392_DIGLDO_CON29, 0xE0,
  328. MT6392_DIGLDO_CON8, 0x2),
  329. MT6392_REG_FIXED("ldo_vcn18", VCN18, MT6392_DIGLDO_CON11, 14, 1800000,
  330. MT6392_DIGLDO_CON11, 0x2),
  331. MT6392_LDO("ldo_vcamaf", VCAMAF, ldo_volt_table5,
  332. MT6392_DIGLDO_CON31, 15, MT6392_DIGLDO_CON32, 0xE0,
  333. MT6392_DIGLDO_CON31, 0x2),
  334. MT6392_LDO("ldo_vm", VM, ldo_volt_table6,
  335. MT6392_DIGLDO_CON47, 14, MT6392_DIGLDO_CON48, 0x30,
  336. MT6392_DIGLDO_CON47, 0x2),
  337. MT6392_REG_FIXED("ldo_vio18", VIO18, MT6392_DIGLDO_CON49, 14, 1800000,
  338. MT6392_DIGLDO_CON49, 0x2),
  339. MT6392_LDO("ldo_vcamd", VCAMD, ldo_volt_table7,
  340. MT6392_DIGLDO_CON51, 14, MT6392_DIGLDO_CON52, 0x60,
  341. MT6392_DIGLDO_CON51, 0x2),
  342. MT6392_REG_FIXED("ldo_vcamio", VCAMIO, MT6392_DIGLDO_CON53, 14, 1800000,
  343. MT6392_DIGLDO_CON53, 0x2),
  344. MT6392_REG_FIXED("ldo_vm25", VM25, MT6392_DIGLDO_CON55, 14, 2500000,
  345. MT6392_DIGLDO_CON55, 0x2),
  346. MT6392_LDO("ldo_vefuse", VEFUSE, ldo_volt_table8,
  347. MT6392_DIGLDO_CON57, 14, MT6392_DIGLDO_CON58, 0x10,
  348. MT6392_DIGLDO_CON57, 0x2),
  349. };
  350. static int mt6392_set_buck_vosel_reg(struct platform_device *pdev)
  351. {
  352. struct mt6397_chip *mt6392 = dev_get_drvdata(pdev->dev.parent);
  353. int i;
  354. u32 regval;
  355. for (i = 0; i < MT6392_MAX_REGULATOR; i++) {
  356. if (mt6392_regulators[i].vselctrl_reg) {
  357. if (regmap_read(mt6392->regmap,
  358. mt6392_regulators[i].vselctrl_reg,
  359. &regval) < 0) {
  360. dev_info(&pdev->dev,
  361. "Failed to read buck ctrl\n");
  362. return -EIO;
  363. }
  364. if (regval & mt6392_regulators[i].vselctrl_mask) {
  365. mt6392_regulators[i].desc.vsel_reg =
  366. mt6392_regulators[i].vselon_reg;
  367. }
  368. }
  369. }
  370. return 0;
  371. }
  372. static int mt6392_regulator_probe(struct platform_device *pdev)
  373. {
  374. struct mt6397_chip *mt6392 = dev_get_drvdata(pdev->dev.parent);
  375. struct regulator_config config = {};
  376. struct regulator_dev *rdev;
  377. struct regulation_constraints *c;
  378. int i;
  379. u32 reg_value;
  380. /* Query buck controller to select activated voltage register part */
  381. if (mt6392_set_buck_vosel_reg(pdev))
  382. return -EIO;
  383. /* Read PMIC chip revision to update constraints and voltage table */
  384. if (regmap_read(mt6392->regmap, MT6392_CID, &reg_value) < 0) {
  385. dev_info(&pdev->dev, "Failed to read Chip ID\n");
  386. return -EIO;
  387. }
  388. dev_info(&pdev->dev, "Chip ID = 0x%x\n", reg_value);
  389. for (i = 0; i < MT6392_MAX_REGULATOR; i++) {
  390. config.dev = &pdev->dev;
  391. config.driver_data = &mt6392_regulators[i];
  392. config.regmap = mt6392->regmap;
  393. rdev = devm_regulator_register(&pdev->dev,
  394. &mt6392_regulators[i].desc, &config);
  395. if (IS_ERR(rdev)) {
  396. dev_info(&pdev->dev, "failed to register %s\n",
  397. mt6392_regulators[i].desc.name);
  398. return PTR_ERR(rdev);
  399. }
  400. /* Constrain board-specific capabilities according to what
  401. * this driver and the chip itself can actually do.
  402. */
  403. c = rdev->constraints;
  404. c->valid_modes_mask |= REGULATOR_MODE_NORMAL|
  405. REGULATOR_MODE_STANDBY | REGULATOR_MODE_FAST;
  406. c->valid_ops_mask |= REGULATOR_CHANGE_MODE;
  407. }
  408. return 0;
  409. }
  410. static const struct platform_device_id mt6392_platform_ids[] = {
  411. {"mt6392-regulator", 0},
  412. { /* sentinel */ },
  413. };
  414. MODULE_DEVICE_TABLE(platform, mt6392_platform_ids);
  415. static const struct of_device_id mt6392_of_match[] = {
  416. { .compatible = "mediatek,mt6392-regulator", },
  417. { /* sentinel */ },
  418. };
  419. MODULE_DEVICE_TABLE(of, mt6392_of_match);
  420. static struct platform_driver mt6392_regulator_driver = {
  421. .driver = {
  422. .name = "mt6392-regulator",
  423. .of_match_table = of_match_ptr(mt6392_of_match),
  424. },
  425. .probe = mt6392_regulator_probe,
  426. .id_table = mt6392_platform_ids,
  427. };
  428. module_platform_driver(mt6392_regulator_driver);
  429. MODULE_AUTHOR("Chen Zhong <chen.zhong@mediatek.com>");
  430. MODULE_DESCRIPTION("Regulator Driver for MediaTek MT6392 PMIC");
  431. MODULE_LICENSE("GPL v2");