mt6358-regulator.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745
  1. // SPDX-License-Identifier: GPL-2.0
  2. //
  3. // Copyright (c) 2019 MediaTek Inc.
  4. #include <linux/mfd/mt6358/registers.h>
  5. #include <linux/mfd/mt6397/core.h>
  6. #include <linux/module.h>
  7. #include <linux/of.h>
  8. #include <linux/platform_device.h>
  9. #include <linux/regmap.h>
  10. #include <linux/regulator/driver.h>
  11. #include <linux/regulator/machine.h>
  12. #include <linux/regulator/mt6358-regulator.h>
  13. #include <linux/regulator/of_regulator.h>
  14. #include <dt-bindings/regulator/mediatek,mt6397-regulator.h>
  15. /*
  16. * MT6358 regulators' information
  17. *
  18. * @desc: standard fields of regulator description.
  19. * @qi: Mask for query enable signal status of regulators
  20. */
  21. struct mt6358_regulator_info {
  22. struct regulator_desc desc;
  23. u32 status_reg;
  24. u32 qi;
  25. u32 da_vsel_reg;
  26. u32 da_vsel_mask;
  27. u32 modeset_reg;
  28. u32 modeset_mask;
  29. };
  30. #define to_regulator_info(x) container_of_const((x), struct mt6358_regulator_info, desc)
  31. #define MT6358_BUCK(match, vreg, supply, min, max, step, \
  32. vosel_mask, _da_vsel_reg, _da_vsel_mask, \
  33. _modeset_reg, _modeset_shift) \
  34. [MT6358_ID_##vreg] = { \
  35. .desc = { \
  36. .name = #vreg, \
  37. .supply_name = supply, \
  38. .of_match = of_match_ptr(match), \
  39. .ops = &mt6358_buck_ops, \
  40. .type = REGULATOR_VOLTAGE, \
  41. .id = MT6358_ID_##vreg, \
  42. .owner = THIS_MODULE, \
  43. .n_voltages = ((max) - (min)) / (step) + 1, \
  44. .min_uV = (min), \
  45. .uV_step = (step), \
  46. .vsel_reg = MT6358_BUCK_##vreg##_ELR0, \
  47. .vsel_mask = vosel_mask, \
  48. .enable_reg = MT6358_BUCK_##vreg##_CON0, \
  49. .enable_mask = BIT(0), \
  50. .of_map_mode = mt6358_map_mode, \
  51. }, \
  52. .status_reg = MT6358_BUCK_##vreg##_DBG1, \
  53. .qi = BIT(0), \
  54. .da_vsel_reg = _da_vsel_reg, \
  55. .da_vsel_mask = _da_vsel_mask, \
  56. .modeset_reg = _modeset_reg, \
  57. .modeset_mask = BIT(_modeset_shift), \
  58. }
  59. #define MT6358_LDO(match, vreg, supply, volt_ranges, enreg, enbit, vosel, vosel_mask) \
  60. [MT6358_ID_##vreg] = { \
  61. .desc = { \
  62. .name = #vreg, \
  63. .supply_name = supply, \
  64. .of_match = of_match_ptr(match), \
  65. .ops = &mt6358_volt_table_ops, \
  66. .type = REGULATOR_VOLTAGE, \
  67. .id = MT6358_ID_##vreg, \
  68. .owner = THIS_MODULE, \
  69. .n_voltages = ARRAY_SIZE(volt_ranges##_ranges) * 11, \
  70. .linear_ranges = volt_ranges##_ranges, \
  71. .linear_range_selectors_bitfield = volt_ranges##_selectors, \
  72. .n_linear_ranges = ARRAY_SIZE(volt_ranges##_ranges), \
  73. .vsel_range_reg = vosel, \
  74. .vsel_range_mask = vosel_mask, \
  75. .vsel_reg = MT6358_##vreg##_ANA_CON0, \
  76. .vsel_mask = GENMASK(3, 0), \
  77. .enable_reg = enreg, \
  78. .enable_mask = BIT(enbit), \
  79. }, \
  80. .status_reg = MT6358_LDO_##vreg##_CON1, \
  81. .qi = BIT(15), \
  82. }
  83. #define MT6358_LDO1(match, vreg, supply, min, max, step, \
  84. _da_vsel_reg, _da_vsel_mask, vosel, vosel_mask) \
  85. [MT6358_ID_##vreg] = { \
  86. .desc = { \
  87. .name = #vreg, \
  88. .supply_name = supply, \
  89. .of_match = of_match_ptr(match), \
  90. .ops = &mt6358_volt_range_ops, \
  91. .type = REGULATOR_VOLTAGE, \
  92. .id = MT6358_ID_##vreg, \
  93. .owner = THIS_MODULE, \
  94. .n_voltages = ((max) - (min)) / (step) + 1, \
  95. .min_uV = (min), \
  96. .uV_step = (step), \
  97. .vsel_reg = vosel, \
  98. .vsel_mask = vosel_mask, \
  99. .enable_reg = MT6358_LDO_##vreg##_CON0, \
  100. .enable_mask = BIT(0), \
  101. }, \
  102. .da_vsel_reg = _da_vsel_reg, \
  103. .da_vsel_mask = _da_vsel_mask, \
  104. .status_reg = MT6358_LDO_##vreg##_DBG1, \
  105. .qi = BIT(0), \
  106. }
  107. #define MT6358_REG_FIXED(match, vreg, supply, enreg, enbit, volt) \
  108. [MT6358_ID_##vreg] = { \
  109. .desc = { \
  110. .name = #vreg, \
  111. .supply_name = supply, \
  112. .of_match = of_match_ptr(match), \
  113. .ops = &mt6358_volt_fixed_ops, \
  114. .type = REGULATOR_VOLTAGE, \
  115. .id = MT6358_ID_##vreg, \
  116. .owner = THIS_MODULE, \
  117. .n_voltages = 11, \
  118. .vsel_reg = MT6358_##vreg##_ANA_CON0, \
  119. .vsel_mask = GENMASK(3, 0), \
  120. .enable_reg = enreg, \
  121. .enable_mask = BIT(enbit), \
  122. .min_uV = volt, \
  123. .uV_step = 10000, \
  124. }, \
  125. .status_reg = MT6358_LDO_##vreg##_CON1, \
  126. .qi = BIT(15), \
  127. }
  128. #define MT6366_BUCK(match, vreg, min, max, step, \
  129. vosel_mask, _da_vsel_reg, _da_vsel_mask, \
  130. _modeset_reg, _modeset_shift) \
  131. [MT6366_ID_##vreg] = { \
  132. .desc = { \
  133. .name = #vreg, \
  134. .supply_name = "vsys-" match, \
  135. .of_match = of_match_ptr(match), \
  136. .ops = &mt6358_buck_ops, \
  137. .type = REGULATOR_VOLTAGE, \
  138. .id = MT6366_ID_##vreg, \
  139. .owner = THIS_MODULE, \
  140. .n_voltages = ((max) - (min)) / (step) + 1, \
  141. .min_uV = (min), \
  142. .uV_step = (step), \
  143. .vsel_reg = MT6358_BUCK_##vreg##_ELR0, \
  144. .vsel_mask = vosel_mask, \
  145. .enable_reg = MT6358_BUCK_##vreg##_CON0, \
  146. .enable_mask = BIT(0), \
  147. .of_map_mode = mt6358_map_mode, \
  148. }, \
  149. .status_reg = MT6358_BUCK_##vreg##_DBG1, \
  150. .qi = BIT(0), \
  151. .da_vsel_reg = _da_vsel_reg, \
  152. .da_vsel_mask = _da_vsel_mask, \
  153. .modeset_reg = _modeset_reg, \
  154. .modeset_mask = BIT(_modeset_shift), \
  155. }
  156. #define MT6366_LDO(match, vreg, volt_ranges, supply, enreg, enbit, vosel, vosel_mask) \
  157. [MT6366_ID_##vreg] = { \
  158. .desc = { \
  159. .name = #vreg, \
  160. .supply_name = supply, \
  161. .of_match = of_match_ptr(match), \
  162. .ops = &mt6358_volt_table_ops, \
  163. .type = REGULATOR_VOLTAGE, \
  164. .id = MT6366_ID_##vreg, \
  165. .owner = THIS_MODULE, \
  166. .n_voltages = ARRAY_SIZE(volt_ranges##_ranges) * 11, \
  167. .linear_ranges = volt_ranges##_ranges, \
  168. .linear_range_selectors_bitfield = volt_ranges##_selectors, \
  169. .n_linear_ranges = ARRAY_SIZE(volt_ranges##_ranges), \
  170. .vsel_range_reg = vosel, \
  171. .vsel_range_mask = vosel_mask, \
  172. .vsel_reg = MT6358_##vreg##_ANA_CON0, \
  173. .vsel_mask = GENMASK(3, 0), \
  174. .enable_reg = enreg, \
  175. .enable_mask = BIT(enbit), \
  176. }, \
  177. .status_reg = MT6358_LDO_##vreg##_CON1, \
  178. .qi = BIT(15), \
  179. }
  180. #define MT6366_LDO1(match, vreg, supply, min, max, step, \
  181. _da_vsel_reg, _da_vsel_mask, vosel, vosel_mask) \
  182. [MT6366_ID_##vreg] = { \
  183. .desc = { \
  184. .name = #vreg, \
  185. .supply_name = supply, \
  186. .of_match = of_match_ptr(match), \
  187. .ops = &mt6358_volt_range_ops, \
  188. .type = REGULATOR_VOLTAGE, \
  189. .id = MT6366_ID_##vreg, \
  190. .owner = THIS_MODULE, \
  191. .n_voltages = ((max) - (min)) / (step) + 1, \
  192. .min_uV = (min), \
  193. .uV_step = (step), \
  194. .vsel_reg = vosel, \
  195. .vsel_mask = vosel_mask, \
  196. .enable_reg = MT6358_LDO_##vreg##_CON0, \
  197. .enable_mask = BIT(0), \
  198. }, \
  199. .da_vsel_reg = _da_vsel_reg, \
  200. .da_vsel_mask = _da_vsel_mask, \
  201. .status_reg = MT6358_LDO_##vreg##_DBG1, \
  202. .qi = BIT(0), \
  203. }
  204. #define MT6366_REG_FIXED(match, vreg, supply, enreg, enbit, volt) \
  205. [MT6366_ID_##vreg] = { \
  206. .desc = { \
  207. .name = #vreg, \
  208. .supply_name = supply, \
  209. .of_match = of_match_ptr(match), \
  210. .ops = &mt6358_volt_fixed_ops, \
  211. .type = REGULATOR_VOLTAGE, \
  212. .id = MT6366_ID_##vreg, \
  213. .owner = THIS_MODULE, \
  214. .n_voltages = 11, \
  215. .vsel_reg = MT6358_##vreg##_ANA_CON0, \
  216. .vsel_mask = GENMASK(3, 0), \
  217. .enable_reg = enreg, \
  218. .enable_mask = BIT(enbit), \
  219. .min_uV = volt, \
  220. .uV_step = 10000, \
  221. }, \
  222. .status_reg = MT6358_LDO_##vreg##_CON1, \
  223. .qi = BIT(15), \
  224. }
  225. /* VDRAM2 voltage selector not shown in datasheet */
  226. static const unsigned int vdram2_selectors[] = { 0, 12 };
  227. static const struct linear_range vdram2_ranges[] = {
  228. REGULATOR_LINEAR_RANGE(600000, 0, 10, 10000),
  229. REGULATOR_LINEAR_RANGE(1800000, 0, 10, 10000),
  230. };
  231. static const unsigned int vsim_selectors[] = { 3, 4, 8, 11, 12 };
  232. static const struct linear_range vsim_ranges[] = {
  233. REGULATOR_LINEAR_RANGE(1700000, 0, 10, 10000),
  234. REGULATOR_LINEAR_RANGE(1800000, 0, 10, 10000),
  235. REGULATOR_LINEAR_RANGE(2700000, 0, 10, 10000),
  236. REGULATOR_LINEAR_RANGE(3000000, 0, 10, 10000),
  237. REGULATOR_LINEAR_RANGE(3100000, 0, 10, 10000),
  238. };
  239. static const unsigned int vibr_selectors[] = { 0, 1, 2, 4, 5, 9, 11, 13 };
  240. static const struct linear_range vibr_ranges[] = {
  241. REGULATOR_LINEAR_RANGE(1200000, 0, 10, 10000),
  242. REGULATOR_LINEAR_RANGE(1300000, 0, 10, 10000),
  243. REGULATOR_LINEAR_RANGE(1500000, 0, 10, 10000),
  244. REGULATOR_LINEAR_RANGE(1800000, 0, 10, 10000),
  245. REGULATOR_LINEAR_RANGE(2000000, 0, 10, 10000),
  246. REGULATOR_LINEAR_RANGE(2800000, 0, 10, 10000),
  247. REGULATOR_LINEAR_RANGE(3000000, 0, 10, 10000),
  248. REGULATOR_LINEAR_RANGE(3300000, 0, 10, 10000),
  249. };
  250. /* VUSB voltage selector not shown in datasheet */
  251. static const unsigned int vusb_selectors[] = { 3, 4 };
  252. static const struct linear_range vusb_ranges[] = {
  253. REGULATOR_LINEAR_RANGE(3000000, 0, 10, 10000),
  254. REGULATOR_LINEAR_RANGE(3100000, 0, 10, 10000),
  255. };
  256. static const unsigned int vcamd_selectors[] = { 3, 4, 5, 6, 7, 9, 12 };
  257. static const struct linear_range vcamd_ranges[] = {
  258. REGULATOR_LINEAR_RANGE(900000, 0, 10, 10000),
  259. REGULATOR_LINEAR_RANGE(1000000, 0, 10, 10000),
  260. REGULATOR_LINEAR_RANGE(1100000, 0, 10, 10000),
  261. REGULATOR_LINEAR_RANGE(1200000, 0, 10, 10000),
  262. REGULATOR_LINEAR_RANGE(1300000, 0, 10, 10000),
  263. REGULATOR_LINEAR_RANGE(1500000, 0, 10, 10000),
  264. REGULATOR_LINEAR_RANGE(1800000, 0, 10, 10000),
  265. };
  266. static const unsigned int vefuse_selectors[] = { 11, 12, 13 };
  267. static const struct linear_range vefuse_ranges[] = {
  268. REGULATOR_LINEAR_RANGE(1700000, 0, 10, 10000),
  269. REGULATOR_LINEAR_RANGE(1800000, 0, 10, 10000),
  270. REGULATOR_LINEAR_RANGE(1900000, 0, 10, 10000),
  271. };
  272. static const unsigned int vmch_vemc_selectors[] = { 2, 3, 5 };
  273. static const struct linear_range vmch_vemc_ranges[] = {
  274. REGULATOR_LINEAR_RANGE(2900000, 0, 10, 10000),
  275. REGULATOR_LINEAR_RANGE(3000000, 0, 10, 10000),
  276. REGULATOR_LINEAR_RANGE(3300000, 0, 10, 10000),
  277. };
  278. static const unsigned int vcama_selectors[] = { 0, 7, 9, 10, 11, 12 };
  279. static const struct linear_range vcama_ranges[] = {
  280. REGULATOR_LINEAR_RANGE(1800000, 0, 10, 10000),
  281. REGULATOR_LINEAR_RANGE(2500000, 0, 10, 10000),
  282. REGULATOR_LINEAR_RANGE(2700000, 0, 10, 10000),
  283. REGULATOR_LINEAR_RANGE(2800000, 0, 10, 10000),
  284. REGULATOR_LINEAR_RANGE(2900000, 0, 10, 10000),
  285. REGULATOR_LINEAR_RANGE(3000000, 0, 10, 10000),
  286. };
  287. static const unsigned int vcn33_selectors[] = { 1, 2, 3 };
  288. static const struct linear_range vcn33_ranges[] = {
  289. REGULATOR_LINEAR_RANGE(3300000, 0, 10, 10000),
  290. REGULATOR_LINEAR_RANGE(3400000, 0, 10, 10000),
  291. REGULATOR_LINEAR_RANGE(3500000, 0, 10, 10000),
  292. };
  293. static const unsigned int vmc_selectors[] = { 4, 10, 11, 13 };
  294. static const struct linear_range vmc_ranges[] = {
  295. REGULATOR_LINEAR_RANGE(1800000, 0, 10, 10000),
  296. REGULATOR_LINEAR_RANGE(2900000, 0, 10, 10000),
  297. REGULATOR_LINEAR_RANGE(3000000, 0, 10, 10000),
  298. REGULATOR_LINEAR_RANGE(3300000, 0, 10, 10000),
  299. };
  300. static const unsigned int vldo28_selectors[] = { 1, 3 };
  301. static const struct linear_range vldo28_ranges[] = {
  302. REGULATOR_LINEAR_RANGE(2800000, 0, 10, 10000),
  303. REGULATOR_LINEAR_RANGE(3000000, 0, 10, 10000),
  304. };
  305. static const unsigned int mt6366_vmddr_selectors[] = { 0, 1, 2, 3, 4, 5, 6, 7, 9, 12 };
  306. static const struct linear_range mt6366_vmddr_ranges[] = {
  307. REGULATOR_LINEAR_RANGE(600000, 0, 10, 10000),
  308. REGULATOR_LINEAR_RANGE(700000, 0, 10, 10000),
  309. REGULATOR_LINEAR_RANGE(800000, 0, 10, 10000),
  310. REGULATOR_LINEAR_RANGE(900000, 0, 10, 10000),
  311. REGULATOR_LINEAR_RANGE(1000000, 0, 10, 10000),
  312. REGULATOR_LINEAR_RANGE(1100000, 0, 10, 10000),
  313. REGULATOR_LINEAR_RANGE(1200000, 0, 10, 10000),
  314. REGULATOR_LINEAR_RANGE(1300000, 0, 10, 10000),
  315. REGULATOR_LINEAR_RANGE(1500000, 0, 10, 10000),
  316. REGULATOR_LINEAR_RANGE(1800000, 0, 10, 10000),
  317. };
  318. static const unsigned int mt6366_vcn18_vm18_selectors[] = {
  319. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
  320. static const struct linear_range mt6366_vcn18_vm18_ranges[] = {
  321. REGULATOR_LINEAR_RANGE(600000, 0, 10, 10000),
  322. REGULATOR_LINEAR_RANGE(700000, 0, 10, 10000),
  323. REGULATOR_LINEAR_RANGE(800000, 0, 10, 10000),
  324. REGULATOR_LINEAR_RANGE(900000, 0, 10, 10000),
  325. REGULATOR_LINEAR_RANGE(1000000, 0, 10, 10000),
  326. REGULATOR_LINEAR_RANGE(1100000, 0, 10, 10000),
  327. REGULATOR_LINEAR_RANGE(1200000, 0, 10, 10000),
  328. REGULATOR_LINEAR_RANGE(1300000, 0, 10, 10000),
  329. REGULATOR_LINEAR_RANGE(1400000, 0, 10, 10000),
  330. REGULATOR_LINEAR_RANGE(1500000, 0, 10, 10000),
  331. REGULATOR_LINEAR_RANGE(1600000, 0, 10, 10000),
  332. REGULATOR_LINEAR_RANGE(1700000, 0, 10, 10000),
  333. REGULATOR_LINEAR_RANGE(1800000, 0, 10, 10000),
  334. REGULATOR_LINEAR_RANGE(1900000, 0, 10, 10000),
  335. REGULATOR_LINEAR_RANGE(2000000, 0, 10, 10000),
  336. REGULATOR_LINEAR_RANGE(2100000, 0, 10, 10000),
  337. };
  338. static unsigned int mt6358_map_mode(unsigned int mode)
  339. {
  340. return mode == MT6397_BUCK_MODE_AUTO ?
  341. REGULATOR_MODE_NORMAL : REGULATOR_MODE_FAST;
  342. }
  343. static int mt6358_get_buck_voltage_sel(struct regulator_dev *rdev)
  344. {
  345. const struct mt6358_regulator_info *info = to_regulator_info(rdev->desc);
  346. int ret, regval;
  347. ret = regmap_read(rdev->regmap, info->da_vsel_reg, &regval);
  348. if (ret != 0) {
  349. dev_err(&rdev->dev,
  350. "Failed to get mt6358 Buck %s vsel reg: %d\n",
  351. info->desc.name, ret);
  352. return ret;
  353. }
  354. ret = (regval & info->da_vsel_mask) >> (ffs(info->da_vsel_mask) - 1);
  355. return ret;
  356. }
  357. static int mt6358_get_status(struct regulator_dev *rdev)
  358. {
  359. const struct mt6358_regulator_info *info = to_regulator_info(rdev->desc);
  360. int ret;
  361. u32 regval;
  362. ret = regmap_read(rdev->regmap, info->status_reg, &regval);
  363. if (ret != 0) {
  364. dev_info(&rdev->dev, "Failed to get enable reg: %d\n", ret);
  365. return ret;
  366. }
  367. return (regval & info->qi) ? REGULATOR_STATUS_ON : REGULATOR_STATUS_OFF;
  368. }
  369. static int mt6358_regulator_set_mode(struct regulator_dev *rdev,
  370. unsigned int mode)
  371. {
  372. const struct mt6358_regulator_info *info = to_regulator_info(rdev->desc);
  373. int val;
  374. switch (mode) {
  375. case REGULATOR_MODE_FAST:
  376. val = MT6397_BUCK_MODE_FORCE_PWM;
  377. break;
  378. case REGULATOR_MODE_NORMAL:
  379. val = MT6397_BUCK_MODE_AUTO;
  380. break;
  381. default:
  382. return -EINVAL;
  383. }
  384. dev_dbg(&rdev->dev, "mt6358 buck set_mode %#x, %#x, %#x\n",
  385. info->modeset_reg, info->modeset_mask, val);
  386. val <<= ffs(info->modeset_mask) - 1;
  387. return regmap_update_bits(rdev->regmap, info->modeset_reg,
  388. info->modeset_mask, val);
  389. }
  390. static unsigned int mt6358_regulator_get_mode(struct regulator_dev *rdev)
  391. {
  392. const struct mt6358_regulator_info *info = to_regulator_info(rdev->desc);
  393. int ret, regval;
  394. ret = regmap_read(rdev->regmap, info->modeset_reg, &regval);
  395. if (ret != 0) {
  396. dev_err(&rdev->dev,
  397. "Failed to get mt6358 buck mode: %d\n", ret);
  398. return ret;
  399. }
  400. switch ((regval & info->modeset_mask) >> (ffs(info->modeset_mask) - 1)) {
  401. case MT6397_BUCK_MODE_AUTO:
  402. return REGULATOR_MODE_NORMAL;
  403. case MT6397_BUCK_MODE_FORCE_PWM:
  404. return REGULATOR_MODE_FAST;
  405. default:
  406. return -EINVAL;
  407. }
  408. }
  409. static const struct regulator_ops mt6358_buck_ops = {
  410. .list_voltage = regulator_list_voltage_linear,
  411. .map_voltage = regulator_map_voltage_linear,
  412. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  413. .get_voltage_sel = mt6358_get_buck_voltage_sel,
  414. .set_voltage_time_sel = regulator_set_voltage_time_sel,
  415. .enable = regulator_enable_regmap,
  416. .disable = regulator_disable_regmap,
  417. .is_enabled = regulator_is_enabled_regmap,
  418. .get_status = mt6358_get_status,
  419. .set_mode = mt6358_regulator_set_mode,
  420. .get_mode = mt6358_regulator_get_mode,
  421. };
  422. static const struct regulator_ops mt6358_volt_range_ops = {
  423. .list_voltage = regulator_list_voltage_linear,
  424. .map_voltage = regulator_map_voltage_linear,
  425. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  426. .get_voltage_sel = mt6358_get_buck_voltage_sel,
  427. .set_voltage_time_sel = regulator_set_voltage_time_sel,
  428. .enable = regulator_enable_regmap,
  429. .disable = regulator_disable_regmap,
  430. .is_enabled = regulator_is_enabled_regmap,
  431. .get_status = mt6358_get_status,
  432. };
  433. static const struct regulator_ops mt6358_volt_table_ops = {
  434. .list_voltage = regulator_list_voltage_pickable_linear_range,
  435. .map_voltage = regulator_map_voltage_pickable_linear_range,
  436. .set_voltage_sel = regulator_set_voltage_sel_pickable_regmap,
  437. .get_voltage_sel = regulator_get_voltage_sel_pickable_regmap,
  438. .set_voltage_time_sel = regulator_set_voltage_time_sel,
  439. .enable = regulator_enable_regmap,
  440. .disable = regulator_disable_regmap,
  441. .is_enabled = regulator_is_enabled_regmap,
  442. .get_status = mt6358_get_status,
  443. };
  444. /* "Fixed" LDOs with output voltage calibration +0 ~ +10 mV */
  445. static const struct regulator_ops mt6358_volt_fixed_ops = {
  446. .list_voltage = regulator_list_voltage_linear,
  447. .map_voltage = regulator_map_voltage_linear,
  448. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  449. .get_voltage_sel = mt6358_get_buck_voltage_sel,
  450. .set_voltage_time_sel = regulator_set_voltage_time_sel,
  451. .enable = regulator_enable_regmap,
  452. .disable = regulator_disable_regmap,
  453. .is_enabled = regulator_is_enabled_regmap,
  454. .get_status = mt6358_get_status,
  455. };
  456. /* The array is indexed by id(MT6358_ID_XXX) */
  457. static const struct mt6358_regulator_info mt6358_regulators[] = {
  458. MT6358_BUCK("buck_vdram1", VDRAM1, "vsys-vdram1", 500000, 2087500, 12500,
  459. 0x7f, MT6358_BUCK_VDRAM1_DBG0, 0x7f, MT6358_VDRAM1_ANA_CON0, 8),
  460. MT6358_BUCK("buck_vcore", VCORE, "vsys-vcore", 500000, 1293750, 6250,
  461. 0x7f, MT6358_BUCK_VCORE_DBG0, 0x7f, MT6358_VCORE_VGPU_ANA_CON0, 1),
  462. MT6358_BUCK("buck_vpa", VPA, "vsys-vpa", 500000, 3650000, 50000,
  463. 0x3f, MT6358_BUCK_VPA_DBG0, 0x3f, MT6358_VPA_ANA_CON0, 3),
  464. MT6358_BUCK("buck_vproc11", VPROC11, "vsys-vproc11", 500000, 1293750, 6250,
  465. 0x7f, MT6358_BUCK_VPROC11_DBG0, 0x7f, MT6358_VPROC_ANA_CON0, 1),
  466. MT6358_BUCK("buck_vproc12", VPROC12, "vsys-vproc12", 500000, 1293750, 6250,
  467. 0x7f, MT6358_BUCK_VPROC12_DBG0, 0x7f, MT6358_VPROC_ANA_CON0, 2),
  468. MT6358_BUCK("buck_vgpu", VGPU, "vsys-vgpu", 500000, 1293750, 6250,
  469. 0x7f, MT6358_BUCK_VGPU_ELR0, 0x7f, MT6358_VCORE_VGPU_ANA_CON0, 2),
  470. MT6358_BUCK("buck_vs2", VS2, "vsys-vs2", 500000, 2087500, 12500,
  471. 0x7f, MT6358_BUCK_VS2_DBG0, 0x7f, MT6358_VS2_ANA_CON0, 8),
  472. MT6358_BUCK("buck_vmodem", VMODEM, "vsys-vmodem", 500000, 1293750, 6250,
  473. 0x7f, MT6358_BUCK_VMODEM_DBG0, 0x7f, MT6358_VMODEM_ANA_CON0, 8),
  474. MT6358_BUCK("buck_vs1", VS1, "vsys-vs1", 1000000, 2587500, 12500,
  475. 0x7f, MT6358_BUCK_VS1_DBG0, 0x7f, MT6358_VS1_ANA_CON0, 8),
  476. MT6358_REG_FIXED("ldo_vrf12", VRF12, "vs2-ldo2", MT6358_LDO_VRF12_CON0, 0, 1200000),
  477. MT6358_REG_FIXED("ldo_vio18", VIO18, "vs1-ldo1", MT6358_LDO_VIO18_CON0, 0, 1800000),
  478. MT6358_REG_FIXED("ldo_vcamio", VCAMIO, "vs1-ldo1", MT6358_LDO_VCAMIO_CON0, 0, 1800000),
  479. MT6358_REG_FIXED("ldo_vcn18", VCN18, "vs1-ldo1", MT6358_LDO_VCN18_CON0, 0, 1800000),
  480. MT6358_REG_FIXED("ldo_vfe28", VFE28, "vsys-ldo1", MT6358_LDO_VFE28_CON0, 0, 2800000),
  481. MT6358_REG_FIXED("ldo_vcn28", VCN28, "vsys-ldo1", MT6358_LDO_VCN28_CON0, 0, 2800000),
  482. MT6358_REG_FIXED("ldo_vxo22", VXO22, "vsys-ldo1", MT6358_LDO_VXO22_CON0, 0, 2200000),
  483. MT6358_REG_FIXED("ldo_vaux18", VAUX18, "vsys-ldo1", MT6358_LDO_VAUX18_CON0, 0, 1800000),
  484. MT6358_REG_FIXED("ldo_vbif28", VBIF28, "vsys-ldo1", MT6358_LDO_VBIF28_CON0, 0, 2800000),
  485. MT6358_REG_FIXED("ldo_vio28", VIO28, "vsys-ldo2", MT6358_LDO_VIO28_CON0, 0, 2800000),
  486. MT6358_REG_FIXED("ldo_va12", VA12, "vs2-ldo2", MT6358_LDO_VA12_CON0, 0, 1200000),
  487. MT6358_REG_FIXED("ldo_vrf18", VRF18, "vs1-ldo1", MT6358_LDO_VRF18_CON0, 0, 1800000),
  488. MT6358_REG_FIXED("ldo_vaud28", VAUD28, "vsys-ldo1", MT6358_LDO_VAUD28_CON0, 0, 2800000),
  489. MT6358_LDO("ldo_vdram2", VDRAM2, "vs2-ldo1", vdram2,
  490. MT6358_LDO_VDRAM2_CON0, 0, MT6358_LDO_VDRAM2_ELR0, 0xf),
  491. MT6358_LDO("ldo_vsim1", VSIM1, "vsys-ldo1", vsim,
  492. MT6358_LDO_VSIM1_CON0, 0, MT6358_VSIM1_ANA_CON0, 0xf00),
  493. MT6358_LDO("ldo_vibr", VIBR, "vsys-ldo3", vibr,
  494. MT6358_LDO_VIBR_CON0, 0, MT6358_VIBR_ANA_CON0, 0xf00),
  495. MT6358_LDO("ldo_vusb", VUSB, "vsys-ldo1", vusb,
  496. MT6358_LDO_VUSB_CON0_0, 0, MT6358_VUSB_ANA_CON0, 0x700),
  497. MT6358_LDO("ldo_vcamd", VCAMD, "vs2-ldo4", vcamd,
  498. MT6358_LDO_VCAMD_CON0, 0, MT6358_VCAMD_ANA_CON0, 0xf00),
  499. MT6358_LDO("ldo_vefuse", VEFUSE, "vs1-ldo1", vefuse,
  500. MT6358_LDO_VEFUSE_CON0, 0, MT6358_VEFUSE_ANA_CON0, 0xf00),
  501. MT6358_LDO("ldo_vmch", VMCH, "vsys-ldo2", vmch_vemc,
  502. MT6358_LDO_VMCH_CON0, 0, MT6358_VMCH_ANA_CON0, 0x700),
  503. MT6358_LDO("ldo_vcama1", VCAMA1, "vsys-ldo3", vcama,
  504. MT6358_LDO_VCAMA1_CON0, 0, MT6358_VCAMA1_ANA_CON0, 0xf00),
  505. MT6358_LDO("ldo_vemc", VEMC, "vsys-ldo2", vmch_vemc,
  506. MT6358_LDO_VEMC_CON0, 0, MT6358_VEMC_ANA_CON0, 0x700),
  507. MT6358_LDO("ldo_vcn33", VCN33, "vsys-ldo3", vcn33,
  508. MT6358_LDO_VCN33_CON0_0, 0, MT6358_VCN33_ANA_CON0, 0x300),
  509. MT6358_LDO("ldo_vcama2", VCAMA2, "vsys-ldo3", vcama,
  510. MT6358_LDO_VCAMA2_CON0, 0, MT6358_VCAMA2_ANA_CON0, 0xf00),
  511. MT6358_LDO("ldo_vmc", VMC, "vsys-ldo2", vmc,
  512. MT6358_LDO_VMC_CON0, 0, MT6358_VMC_ANA_CON0, 0xf00),
  513. MT6358_LDO("ldo_vldo28", VLDO28, "vsys-ldo2", vldo28,
  514. MT6358_LDO_VLDO28_CON0_0, 0,
  515. MT6358_VLDO28_ANA_CON0, 0x300),
  516. MT6358_LDO("ldo_vsim2", VSIM2, "vsys-ldo2", vsim,
  517. MT6358_LDO_VSIM2_CON0, 0, MT6358_VSIM2_ANA_CON0, 0xf00),
  518. MT6358_LDO1("ldo_vsram_proc11", VSRAM_PROC11, "vs2-ldo3", 500000, 1293750, 6250,
  519. MT6358_LDO_VSRAM_PROC11_DBG0, 0x7f00, MT6358_LDO_VSRAM_CON0, 0x7f),
  520. MT6358_LDO1("ldo_vsram_others", VSRAM_OTHERS, "vs2-ldo3", 500000, 1293750, 6250,
  521. MT6358_LDO_VSRAM_OTHERS_DBG0, 0x7f00, MT6358_LDO_VSRAM_CON2, 0x7f),
  522. MT6358_LDO1("ldo_vsram_gpu", VSRAM_GPU, "vs2-ldo3", 500000, 1293750, 6250,
  523. MT6358_LDO_VSRAM_GPU_DBG0, 0x7f00, MT6358_LDO_VSRAM_CON3, 0x7f),
  524. MT6358_LDO1("ldo_vsram_proc12", VSRAM_PROC12, "vs2-ldo3", 500000, 1293750, 6250,
  525. MT6358_LDO_VSRAM_PROC12_DBG0, 0x7f00, MT6358_LDO_VSRAM_CON1, 0x7f),
  526. };
  527. /* The array is indexed by id(MT6366_ID_XXX) */
  528. static const struct mt6358_regulator_info mt6366_regulators[] = {
  529. MT6366_BUCK("vdram1", VDRAM1, 500000, 2087500, 12500,
  530. 0x7f, MT6358_BUCK_VDRAM1_DBG0, 0x7f, MT6358_VDRAM1_ANA_CON0, 8),
  531. MT6366_BUCK("vcore", VCORE, 500000, 1293750, 6250,
  532. 0x7f, MT6358_BUCK_VCORE_DBG0, 0x7f, MT6358_VCORE_VGPU_ANA_CON0, 1),
  533. MT6366_BUCK("vpa", VPA, 500000, 3650000, 50000,
  534. 0x3f, MT6358_BUCK_VPA_DBG0, 0x3f, MT6358_VPA_ANA_CON0, 3),
  535. MT6366_BUCK("vproc11", VPROC11, 500000, 1293750, 6250,
  536. 0x7f, MT6358_BUCK_VPROC11_DBG0, 0x7f, MT6358_VPROC_ANA_CON0, 1),
  537. MT6366_BUCK("vproc12", VPROC12, 500000, 1293750, 6250,
  538. 0x7f, MT6358_BUCK_VPROC12_DBG0, 0x7f, MT6358_VPROC_ANA_CON0, 2),
  539. MT6366_BUCK("vgpu", VGPU, 500000, 1293750, 6250,
  540. 0x7f, MT6358_BUCK_VGPU_ELR0, 0x7f, MT6358_VCORE_VGPU_ANA_CON0, 2),
  541. MT6366_BUCK("vs2", VS2, 500000, 2087500, 12500,
  542. 0x7f, MT6358_BUCK_VS2_DBG0, 0x7f, MT6358_VS2_ANA_CON0, 8),
  543. MT6366_BUCK("vmodem", VMODEM, 500000, 1293750, 6250,
  544. 0x7f, MT6358_BUCK_VMODEM_DBG0, 0x7f, MT6358_VMODEM_ANA_CON0, 8),
  545. MT6366_BUCK("vs1", VS1, 1000000, 2587500, 12500,
  546. 0x7f, MT6358_BUCK_VS1_DBG0, 0x7f, MT6358_VS1_ANA_CON0, 8),
  547. MT6366_REG_FIXED("vrf12", VRF12, "vs2-ldo2", MT6358_LDO_VRF12_CON0, 0, 1200000),
  548. MT6366_REG_FIXED("vio18", VIO18, "vs1-ldo1", MT6358_LDO_VIO18_CON0, 0, 1800000),
  549. MT6366_REG_FIXED("vfe28", VFE28, "vsys-ldo1", MT6358_LDO_VFE28_CON0, 0, 2800000),
  550. MT6366_REG_FIXED("vcn28", VCN28, "vsys-ldo1", MT6358_LDO_VCN28_CON0, 0, 2800000),
  551. MT6366_REG_FIXED("vxo22", VXO22, "vsys-ldo1", MT6358_LDO_VXO22_CON0, 0, 2200000),
  552. MT6366_REG_FIXED("vaux18", VAUX18, "vsys-ldo1", MT6358_LDO_VAUX18_CON0, 0, 1800000),
  553. MT6366_REG_FIXED("vbif28", VBIF28, "vsys-ldo1", MT6358_LDO_VBIF28_CON0, 0, 2800000),
  554. MT6366_REG_FIXED("vio28", VIO28, "vsys-ldo2", MT6358_LDO_VIO28_CON0, 0, 2800000),
  555. MT6366_REG_FIXED("va12", VA12, "vs2-ldo2", MT6358_LDO_VA12_CON0, 0, 1200000),
  556. MT6366_REG_FIXED("vrf18", VRF18, "vs1-ldo1", MT6358_LDO_VRF18_CON0, 0, 1800000),
  557. MT6366_REG_FIXED("vaud28", VAUD28, "vsys-ldo1", MT6358_LDO_VAUD28_CON0, 0, 2800000),
  558. MT6366_LDO("vdram2", VDRAM2, vdram2, "vs2-ldo1",
  559. MT6358_LDO_VDRAM2_CON0, 0, MT6358_LDO_VDRAM2_ELR0, 0x10),
  560. MT6366_LDO("vsim1", VSIM1, vsim, "vsys-ldo1",
  561. MT6358_LDO_VSIM1_CON0, 0, MT6358_VSIM1_ANA_CON0, 0xf00),
  562. MT6366_LDO("vibr", VIBR, vibr, "vsys-ldo3",
  563. MT6358_LDO_VIBR_CON0, 0, MT6358_VIBR_ANA_CON0, 0xf00),
  564. MT6366_LDO("vusb", VUSB, vusb, "vsys-ldo1",
  565. MT6358_LDO_VUSB_CON0_0, 0, MT6358_VUSB_ANA_CON0, 0x700),
  566. MT6366_LDO("vefuse", VEFUSE, vefuse, "vs1-ldo1",
  567. MT6358_LDO_VEFUSE_CON0, 0, MT6358_VEFUSE_ANA_CON0, 0xf00),
  568. MT6366_LDO("vmch", VMCH, vmch_vemc, "vsys-ldo2",
  569. MT6358_LDO_VMCH_CON0, 0, MT6358_VMCH_ANA_CON0, 0x700),
  570. MT6366_LDO("vemc", VEMC, vmch_vemc, "vsys-ldo3",
  571. MT6358_LDO_VEMC_CON0, 0, MT6358_VEMC_ANA_CON0, 0x700),
  572. MT6366_LDO("vcn33", VCN33, vcn33, "vsys-ldo3",
  573. MT6358_LDO_VCN33_CON0_0, 0, MT6358_VCN33_ANA_CON0, 0x300),
  574. MT6366_LDO("vmc", VMC, vmc, "vsys-ldo2",
  575. MT6358_LDO_VMC_CON0, 0, MT6358_VMC_ANA_CON0, 0xf00),
  576. MT6366_LDO("vsim2", VSIM2, vsim, "vsys-ldo2",
  577. MT6358_LDO_VSIM2_CON0, 0, MT6358_VSIM2_ANA_CON0, 0xf00),
  578. MT6366_LDO("vcn18", VCN18, mt6366_vcn18_vm18, "vs1-ldo1",
  579. MT6358_LDO_VCN18_CON0, 0, MT6358_VCN18_ANA_CON0, 0xf00),
  580. MT6366_LDO("vm18", VM18, mt6366_vcn18_vm18, "vs1-ldo1",
  581. MT6358_LDO_VM18_CON0, 0, MT6358_VM18_ANA_CON0, 0xf00),
  582. MT6366_LDO("vmddr", VMDDR, mt6366_vmddr, "vs2-ldo1",
  583. MT6358_LDO_VMDDR_CON0, 0, MT6358_VMDDR_ANA_CON0, 0xf00),
  584. MT6366_LDO1("vsram-proc11", VSRAM_PROC11, "vs2-ldo3", 500000, 1293750, 6250,
  585. MT6358_LDO_VSRAM_PROC11_DBG0, 0x7f00, MT6358_LDO_VSRAM_CON0, 0x7f),
  586. MT6366_LDO1("vsram-others", VSRAM_OTHERS, "vs2-ldo3", 500000, 1293750, 6250,
  587. MT6358_LDO_VSRAM_OTHERS_DBG0, 0x7f00, MT6358_LDO_VSRAM_CON2, 0x7f),
  588. MT6366_LDO1("vsram-gpu", VSRAM_GPU, "vs2-ldo3", 500000, 1293750, 6250,
  589. MT6358_LDO_VSRAM_GPU_DBG0, 0x7f00, MT6358_LDO_VSRAM_CON3, 0x7f),
  590. MT6366_LDO1("vsram-proc12", VSRAM_PROC12, "vs2-ldo3", 500000, 1293750, 6250,
  591. MT6358_LDO_VSRAM_PROC12_DBG0, 0x7f00, MT6358_LDO_VSRAM_CON1, 0x7f),
  592. MT6366_LDO1("vsram-core", VSRAM_CORE, "vs2-ldo3", 500000, 1293750, 6250,
  593. MT6358_LDO_VSRAM_CORE_DBG0, 0x7f00, MT6358_LDO_VSRAM_CON5, 0x7f),
  594. };
  595. static int mt6358_sync_vcn33_setting(struct device *dev)
  596. {
  597. struct mt6397_chip *mt6397 = dev_get_drvdata(dev->parent);
  598. unsigned int val;
  599. int ret;
  600. /*
  601. * VCN33_WIFI and VCN33_BT are two separate enable bits for the same
  602. * regulator. They share the same voltage setting and output pin.
  603. * Instead of having two potentially conflicting regulators, just have
  604. * one VCN33 regulator. Sync the two enable bits and only use one in
  605. * the regulator device.
  606. */
  607. ret = regmap_read(mt6397->regmap, MT6358_LDO_VCN33_CON0_1, &val);
  608. if (ret) {
  609. dev_err(dev, "Failed to read VCN33_WIFI setting\n");
  610. return ret;
  611. }
  612. if (!(val & BIT(0)))
  613. return 0;
  614. /* Sync VCN33_WIFI enable status to VCN33_BT */
  615. ret = regmap_update_bits(mt6397->regmap, MT6358_LDO_VCN33_CON0_0, BIT(0), BIT(0));
  616. if (ret) {
  617. dev_err(dev, "Failed to sync VCN33_WIFI setting to VCN33_BT\n");
  618. return ret;
  619. }
  620. /* Disable VCN33_WIFI */
  621. ret = regmap_update_bits(mt6397->regmap, MT6358_LDO_VCN33_CON0_1, BIT(0), 0);
  622. if (ret) {
  623. dev_err(dev, "Failed to disable VCN33_WIFI\n");
  624. return ret;
  625. }
  626. return 0;
  627. }
  628. static int mt6358_regulator_probe(struct platform_device *pdev)
  629. {
  630. struct mt6397_chip *mt6397 = dev_get_drvdata(pdev->dev.parent);
  631. struct regulator_config config = {};
  632. struct regulator_dev *rdev;
  633. const struct mt6358_regulator_info *mt6358_info;
  634. int i, max_regulator, ret;
  635. switch (mt6397->chip_id) {
  636. case MT6358_CHIP_ID:
  637. max_regulator = MT6358_MAX_REGULATOR;
  638. mt6358_info = mt6358_regulators;
  639. break;
  640. case MT6366_CHIP_ID:
  641. max_regulator = MT6366_MAX_REGULATOR;
  642. mt6358_info = mt6366_regulators;
  643. break;
  644. default:
  645. dev_err(&pdev->dev, "unsupported chip ID: %d\n", mt6397->chip_id);
  646. return -EINVAL;
  647. }
  648. ret = mt6358_sync_vcn33_setting(&pdev->dev);
  649. if (ret)
  650. return ret;
  651. for (i = 0; i < max_regulator; i++) {
  652. config.dev = &pdev->dev;
  653. config.regmap = mt6397->regmap;
  654. rdev = devm_regulator_register(&pdev->dev,
  655. &mt6358_info[i].desc,
  656. &config);
  657. if (IS_ERR(rdev)) {
  658. dev_err(&pdev->dev, "failed to register %s\n",
  659. mt6358_info[i].desc.name);
  660. return PTR_ERR(rdev);
  661. }
  662. }
  663. return 0;
  664. }
  665. static const struct platform_device_id mt6358_platform_ids[] = {
  666. {"mt6358-regulator", 0},
  667. { /* sentinel */ },
  668. };
  669. MODULE_DEVICE_TABLE(platform, mt6358_platform_ids);
  670. static struct platform_driver mt6358_regulator_driver = {
  671. .driver = {
  672. .name = "mt6358-regulator",
  673. .probe_type = PROBE_PREFER_ASYNCHRONOUS,
  674. },
  675. .probe = mt6358_regulator_probe,
  676. .id_table = mt6358_platform_ids,
  677. };
  678. module_platform_driver(mt6358_regulator_driver);
  679. MODULE_AUTHOR("Hsin-Hsiung Wang <hsin-hsiung.wang@mediatek.com>");
  680. MODULE_DESCRIPTION("Regulator Driver for MediaTek MT6358 PMIC");
  681. MODULE_LICENSE("GPL");