mt6331-regulator.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. // SPDX-License-Identifier: GPL-2.0
  2. //
  3. // Copyright (c) 2022 Collabora Ltd.
  4. // Author: AngeloGioacchino Del Regno <[email protected]>
  5. //
  6. // Based on mt6323-regulator.c,
  7. // Copyright (c) 2016 MediaTek Inc.
  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/mt6331/registers.h>
  15. #include <linux/regulator/driver.h>
  16. #include <linux/regulator/machine.h>
  17. #include <linux/regulator/mt6331-regulator.h>
  18. #include <linux/regulator/of_regulator.h>
  19. #define MT6331_LDO_MODE_NORMAL 0
  20. #define MT6331_LDO_MODE_LP 1
  21. /*
  22. * MT6331 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. * @status_reg: Register for regulator enable status where qi unavailable
  30. * @status_mask: Mask for querying regulator enable status
  31. */
  32. struct mt6331_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. u32 status_reg;
  41. u32 status_mask;
  42. };
  43. #define MT6331_BUCK(match, vreg, min, max, step, volt_ranges, enreg, \
  44. vosel, vosel_mask, voselon, vosel_ctrl) \
  45. [MT6331_ID_##vreg] = { \
  46. .desc = { \
  47. .name = #vreg, \
  48. .of_match = of_match_ptr(match), \
  49. .ops = &mt6331_volt_range_ops, \
  50. .type = REGULATOR_VOLTAGE, \
  51. .id = MT6331_ID_##vreg, \
  52. .owner = THIS_MODULE, \
  53. .n_voltages = (max - min)/step + 1, \
  54. .linear_ranges = volt_ranges, \
  55. .n_linear_ranges = ARRAY_SIZE(volt_ranges), \
  56. .vsel_reg = vosel, \
  57. .vsel_mask = vosel_mask, \
  58. .enable_reg = enreg, \
  59. .enable_mask = BIT(0), \
  60. }, \
  61. .qi = BIT(13), \
  62. .vselon_reg = voselon, \
  63. .vselctrl_reg = vosel_ctrl, \
  64. .vselctrl_mask = BIT(1), \
  65. .status_mask = 0, \
  66. }
  67. #define MT6331_LDO_AO(match, vreg, ldo_volt_table, vosel, vosel_mask) \
  68. [MT6331_ID_##vreg] = { \
  69. .desc = { \
  70. .name = #vreg, \
  71. .of_match = of_match_ptr(match), \
  72. .ops = &mt6331_volt_table_ao_ops, \
  73. .type = REGULATOR_VOLTAGE, \
  74. .id = MT6331_ID_##vreg, \
  75. .owner = THIS_MODULE, \
  76. .n_voltages = ARRAY_SIZE(ldo_volt_table), \
  77. .volt_table = ldo_volt_table, \
  78. .vsel_reg = vosel, \
  79. .vsel_mask = vosel_mask, \
  80. }, \
  81. }
  82. #define MT6331_LDO_S(match, vreg, ldo_volt_table, enreg, enbit, vosel, \
  83. vosel_mask, _modeset_reg, _modeset_mask, \
  84. _status_reg, _status_mask) \
  85. [MT6331_ID_##vreg] = { \
  86. .desc = { \
  87. .name = #vreg, \
  88. .of_match = of_match_ptr(match), \
  89. .ops = &mt6331_volt_table_no_qi_ops, \
  90. .type = REGULATOR_VOLTAGE, \
  91. .id = MT6331_ID_##vreg, \
  92. .owner = THIS_MODULE, \
  93. .n_voltages = ARRAY_SIZE(ldo_volt_table), \
  94. .volt_table = ldo_volt_table, \
  95. .vsel_reg = vosel, \
  96. .vsel_mask = vosel_mask, \
  97. .enable_reg = enreg, \
  98. .enable_mask = BIT(enbit), \
  99. }, \
  100. .modeset_reg = _modeset_reg, \
  101. .modeset_mask = _modeset_mask, \
  102. .status_reg = _status_reg, \
  103. .status_mask = _status_mask, \
  104. }
  105. #define MT6331_LDO(match, vreg, ldo_volt_table, enreg, enbit, vosel, \
  106. vosel_mask, _modeset_reg, _modeset_mask) \
  107. [MT6331_ID_##vreg] = { \
  108. .desc = { \
  109. .name = #vreg, \
  110. .of_match = of_match_ptr(match), \
  111. .ops = (_modeset_reg ? \
  112. &mt6331_volt_table_ops : \
  113. &mt6331_volt_table_no_ms_ops), \
  114. .type = REGULATOR_VOLTAGE, \
  115. .id = MT6331_ID_##vreg, \
  116. .owner = THIS_MODULE, \
  117. .n_voltages = ARRAY_SIZE(ldo_volt_table), \
  118. .volt_table = ldo_volt_table, \
  119. .vsel_reg = vosel, \
  120. .vsel_mask = vosel_mask, \
  121. .enable_reg = enreg, \
  122. .enable_mask = BIT(enbit), \
  123. }, \
  124. .qi = BIT(15), \
  125. .modeset_reg = _modeset_reg, \
  126. .modeset_mask = _modeset_mask, \
  127. }
  128. #define MT6331_REG_FIXED(match, vreg, enreg, enbit, qibit, volt, \
  129. _modeset_reg, _modeset_mask) \
  130. [MT6331_ID_##vreg] = { \
  131. .desc = { \
  132. .name = #vreg, \
  133. .of_match = of_match_ptr(match), \
  134. .ops = (_modeset_reg ? \
  135. &mt6331_volt_fixed_ops : \
  136. &mt6331_volt_fixed_no_ms_ops), \
  137. .type = REGULATOR_VOLTAGE, \
  138. .id = MT6331_ID_##vreg, \
  139. .owner = THIS_MODULE, \
  140. .n_voltages = 1, \
  141. .enable_reg = enreg, \
  142. .enable_mask = BIT(enbit), \
  143. .min_uV = volt, \
  144. }, \
  145. .qi = BIT(qibit), \
  146. .modeset_reg = _modeset_reg, \
  147. .modeset_mask = _modeset_mask, \
  148. }
  149. static const struct linear_range buck_volt_range[] = {
  150. REGULATOR_LINEAR_RANGE(700000, 0, 0x7f, 6250),
  151. };
  152. static const unsigned int ldo_volt_table1[] = {
  153. 2800000, 3000000, 0, 3200000
  154. };
  155. static const unsigned int ldo_volt_table2[] = {
  156. 1500000, 1800000, 2500000, 2800000,
  157. };
  158. static const unsigned int ldo_volt_table3[] = {
  159. 1200000, 1300000, 1500000, 1800000, 2000000, 2800000, 3000000, 3300000,
  160. };
  161. static const unsigned int ldo_volt_table4[] = {
  162. 0, 0, 1700000, 1800000, 1860000, 2760000, 3000000, 3100000,
  163. };
  164. static const unsigned int ldo_volt_table5[] = {
  165. 1800000, 3300000, 1800000, 3300000,
  166. };
  167. static const unsigned int ldo_volt_table6[] = {
  168. 3000000, 3300000,
  169. };
  170. static const unsigned int ldo_volt_table7[] = {
  171. 1200000, 1600000, 1700000, 1800000, 1900000, 2000000, 2100000, 2200000,
  172. };
  173. static const unsigned int ldo_volt_table8[] = {
  174. 900000, 1000000, 1100000, 1220000, 1300000, 1500000, 1500000, 1500000,
  175. };
  176. static const unsigned int ldo_volt_table9[] = {
  177. 1000000, 1050000, 1100000, 1150000, 1200000, 1250000, 1300000, 1300000,
  178. };
  179. static const unsigned int ldo_volt_table10[] = {
  180. 1200000, 1300000, 1500000, 1800000,
  181. };
  182. static const unsigned int ldo_volt_table11[] = {
  183. 1200000, 1300000, 1400000, 1500000, 1600000, 1700000, 1800000, 1800000,
  184. };
  185. static int mt6331_get_status(struct regulator_dev *rdev)
  186. {
  187. struct mt6331_regulator_info *info = rdev_get_drvdata(rdev);
  188. u32 regval;
  189. int ret;
  190. ret = regmap_read(rdev->regmap, info->desc.enable_reg, &regval);
  191. if (ret != 0) {
  192. dev_err(&rdev->dev, "Failed to get enable reg: %d\n", ret);
  193. return ret;
  194. }
  195. return (regval & info->qi) ? REGULATOR_STATUS_ON : REGULATOR_STATUS_OFF;
  196. }
  197. static int mt6331_ldo_set_mode(struct regulator_dev *rdev, unsigned int mode)
  198. {
  199. struct mt6331_regulator_info *info = rdev_get_drvdata(rdev);
  200. int val;
  201. switch (mode) {
  202. case REGULATOR_MODE_STANDBY:
  203. val = MT6331_LDO_MODE_LP;
  204. break;
  205. case REGULATOR_MODE_NORMAL:
  206. val = MT6331_LDO_MODE_NORMAL;
  207. break;
  208. default:
  209. return -EINVAL;
  210. }
  211. val <<= ffs(info->modeset_mask) - 1;
  212. return regmap_update_bits(rdev->regmap, info->modeset_reg,
  213. info->modeset_mask, val);
  214. }
  215. static unsigned int mt6331_ldo_get_mode(struct regulator_dev *rdev)
  216. {
  217. struct mt6331_regulator_info *info = rdev_get_drvdata(rdev);
  218. unsigned int val;
  219. int ret;
  220. ret = regmap_read(rdev->regmap, info->modeset_reg, &val);
  221. if (ret < 0)
  222. return ret;
  223. val &= info->modeset_mask;
  224. val >>= ffs(info->modeset_mask) - 1;
  225. return (val & BIT(0)) ? REGULATOR_MODE_STANDBY : REGULATOR_MODE_NORMAL;
  226. }
  227. static const struct regulator_ops mt6331_volt_range_ops = {
  228. .list_voltage = regulator_list_voltage_linear_range,
  229. .map_voltage = regulator_map_voltage_linear_range,
  230. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  231. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  232. .set_voltage_time_sel = regulator_set_voltage_time_sel,
  233. .enable = regulator_enable_regmap,
  234. .disable = regulator_disable_regmap,
  235. .is_enabled = regulator_is_enabled_regmap,
  236. .get_status = mt6331_get_status,
  237. };
  238. static const struct regulator_ops mt6331_volt_table_no_ms_ops = {
  239. .list_voltage = regulator_list_voltage_table,
  240. .map_voltage = regulator_map_voltage_iterate,
  241. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  242. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  243. .set_voltage_time_sel = regulator_set_voltage_time_sel,
  244. .enable = regulator_enable_regmap,
  245. .disable = regulator_disable_regmap,
  246. .is_enabled = regulator_is_enabled_regmap,
  247. .get_status = mt6331_get_status,
  248. };
  249. static const struct regulator_ops mt6331_volt_table_no_qi_ops = {
  250. .list_voltage = regulator_list_voltage_table,
  251. .map_voltage = regulator_map_voltage_iterate,
  252. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  253. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  254. .set_voltage_time_sel = regulator_set_voltage_time_sel,
  255. .enable = regulator_enable_regmap,
  256. .disable = regulator_disable_regmap,
  257. .is_enabled = regulator_is_enabled_regmap,
  258. .set_mode = mt6331_ldo_set_mode,
  259. .get_mode = mt6331_ldo_get_mode,
  260. };
  261. static const struct regulator_ops mt6331_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 = mt6331_get_status,
  271. .set_mode = mt6331_ldo_set_mode,
  272. .get_mode = mt6331_ldo_get_mode,
  273. };
  274. static const struct regulator_ops mt6331_volt_table_ao_ops = {
  275. .list_voltage = regulator_list_voltage_table,
  276. .map_voltage = regulator_map_voltage_iterate,
  277. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  278. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  279. .set_voltage_time_sel = regulator_set_voltage_time_sel,
  280. };
  281. static const struct regulator_ops mt6331_volt_fixed_no_ms_ops = {
  282. .list_voltage = regulator_list_voltage_linear,
  283. .enable = regulator_enable_regmap,
  284. .disable = regulator_disable_regmap,
  285. .is_enabled = regulator_is_enabled_regmap,
  286. .get_status = mt6331_get_status,
  287. };
  288. static const struct regulator_ops mt6331_volt_fixed_ops = {
  289. .list_voltage = regulator_list_voltage_linear,
  290. .enable = regulator_enable_regmap,
  291. .disable = regulator_disable_regmap,
  292. .is_enabled = regulator_is_enabled_regmap,
  293. .get_status = mt6331_get_status,
  294. .set_mode = mt6331_ldo_set_mode,
  295. .get_mode = mt6331_ldo_get_mode,
  296. };
  297. /* The array is indexed by id(MT6331_ID_XXX) */
  298. static struct mt6331_regulator_info mt6331_regulators[] = {
  299. MT6331_BUCK("buck-vdvfs11", VDVFS11, 700000, 1493750, 6250,
  300. buck_volt_range, MT6331_VDVFS11_CON9,
  301. MT6331_VDVFS11_CON11, GENMASK(6, 0),
  302. MT6331_VDVFS11_CON12, MT6331_VDVFS11_CON7),
  303. MT6331_BUCK("buck-vdvfs12", VDVFS12, 700000, 1493750, 6250,
  304. buck_volt_range, MT6331_VDVFS12_CON9,
  305. MT6331_VDVFS12_CON11, GENMASK(6, 0),
  306. MT6331_VDVFS12_CON12, MT6331_VDVFS12_CON7),
  307. MT6331_BUCK("buck-vdvfs13", VDVFS13, 700000, 1493750, 6250,
  308. buck_volt_range, MT6331_VDVFS13_CON9,
  309. MT6331_VDVFS13_CON11, GENMASK(6, 0),
  310. MT6331_VDVFS13_CON12, MT6331_VDVFS13_CON7),
  311. MT6331_BUCK("buck-vdvfs14", VDVFS14, 700000, 1493750, 6250,
  312. buck_volt_range, MT6331_VDVFS14_CON9,
  313. MT6331_VDVFS14_CON11, GENMASK(6, 0),
  314. MT6331_VDVFS14_CON12, MT6331_VDVFS14_CON7),
  315. MT6331_BUCK("buck-vcore2", VCORE2, 700000, 1493750, 6250,
  316. buck_volt_range, MT6331_VCORE2_CON9,
  317. MT6331_VCORE2_CON11, GENMASK(6, 0),
  318. MT6331_VCORE2_CON12, MT6331_VCORE2_CON7),
  319. MT6331_REG_FIXED("buck-vio18", VIO18, MT6331_VIO18_CON9, 0, 13, 1800000, 0, 0),
  320. MT6331_REG_FIXED("ldo-vrtc", VRTC, MT6331_DIGLDO_CON11, 8, 15, 2800000, 0, 0),
  321. MT6331_REG_FIXED("ldo-vtcxo1", VTCXO1, MT6331_ANALDO_CON1, 10, 15, 2800000,
  322. MT6331_ANALDO_CON1, GENMASK(1, 0)),
  323. MT6331_REG_FIXED("ldo-vtcxo2", VTCXO2, MT6331_ANALDO_CON2, 10, 15, 2800000,
  324. MT6331_ANALDO_CON2, GENMASK(1, 0)),
  325. MT6331_REG_FIXED("ldo-vsram", VSRAM_DVFS1, MT6331_SYSLDO_CON4, 10, 15, 1012500,
  326. MT6331_SYSLDO_CON4, GENMASK(1, 0)),
  327. MT6331_REG_FIXED("ldo-vio28", VIO28, MT6331_DIGLDO_CON1, 10, 15, 2800000,
  328. MT6331_DIGLDO_CON1, GENMASK(1, 0)),
  329. MT6331_LDO("ldo-avdd32aud", AVDD32_AUD, ldo_volt_table1, MT6331_ANALDO_CON3, 10,
  330. MT6331_ANALDO_CON10, GENMASK(6, 5), MT6331_ANALDO_CON3, GENMASK(1, 0)),
  331. MT6331_LDO("ldo-vauxa32", VAUXA32, ldo_volt_table1, MT6331_ANALDO_CON4, 10,
  332. MT6331_ANALDO_CON6, GENMASK(6, 5), MT6331_ANALDO_CON4, GENMASK(1, 0)),
  333. MT6331_LDO("ldo-vemc33", VEMC33, ldo_volt_table6, MT6331_DIGLDO_CON5, 10,
  334. MT6331_DIGLDO_CON17, BIT(6), MT6331_DIGLDO_CON5, GENMASK(1, 0)),
  335. MT6331_LDO("ldo-vibr", VIBR, ldo_volt_table3, MT6331_DIGLDO_CON12, 10,
  336. MT6331_DIGLDO_CON20, GENMASK(6, 4), MT6331_DIGLDO_CON12, GENMASK(1, 0)),
  337. MT6331_LDO("ldo-vmc", VMC, ldo_volt_table5, MT6331_DIGLDO_CON3, 10,
  338. MT6331_DIGLDO_CON15, GENMASK(5, 4), MT6331_DIGLDO_CON3, GENMASK(1, 0)),
  339. MT6331_LDO("ldo-vmch", VMCH, ldo_volt_table6, MT6331_DIGLDO_CON4, 10,
  340. MT6331_DIGLDO_CON16, BIT(6), MT6331_DIGLDO_CON4, GENMASK(1, 0)),
  341. MT6331_LDO("ldo-vmipi", VMIPI, ldo_volt_table3, MT6331_SYSLDO_CON5, 10,
  342. MT6331_SYSLDO_CON13, GENMASK(5, 3), MT6331_SYSLDO_CON5, GENMASK(1, 0)),
  343. MT6331_LDO("ldo-vsim1", VSIM1, ldo_volt_table4, MT6331_DIGLDO_CON8, 10,
  344. MT6331_DIGLDO_CON21, GENMASK(6, 4), MT6331_DIGLDO_CON8, GENMASK(1, 0)),
  345. MT6331_LDO("ldo-vsim2", VSIM2, ldo_volt_table4, MT6331_DIGLDO_CON9, 10,
  346. MT6331_DIGLDO_CON22, GENMASK(6, 4), MT6331_DIGLDO_CON9, GENMASK(1, 0)),
  347. MT6331_LDO("ldo-vusb10", VUSB10, ldo_volt_table9, MT6331_SYSLDO_CON2, 10,
  348. MT6331_SYSLDO_CON10, GENMASK(5, 3), MT6331_SYSLDO_CON2, GENMASK(1, 0)),
  349. MT6331_LDO("ldo-vcama", VCAMA, ldo_volt_table2, MT6331_ANALDO_CON5, 15,
  350. MT6331_ANALDO_CON9, GENMASK(5, 4), 0, 0),
  351. MT6331_LDO_S("ldo-vcamaf", VCAM_AF, ldo_volt_table3, MT6331_DIGLDO_CON2, 10,
  352. MT6331_DIGLDO_CON14, GENMASK(6, 4), MT6331_DIGLDO_CON2, GENMASK(1, 0),
  353. MT6331_EN_STATUS1, BIT(0)),
  354. MT6331_LDO_S("ldo-vcamd", VCAMD, ldo_volt_table8, MT6331_SYSLDO_CON1, 15,
  355. MT6331_SYSLDO_CON9, GENMASK(6, 4), MT6331_SYSLDO_CON1, GENMASK(1, 0),
  356. MT6331_EN_STATUS1, BIT(11)),
  357. MT6331_LDO_S("ldo-vcamio", VCAM_IO, ldo_volt_table10, MT6331_SYSLDO_CON3, 10,
  358. MT6331_SYSLDO_CON11, GENMASK(4, 3), MT6331_SYSLDO_CON3, GENMASK(1, 0),
  359. MT6331_EN_STATUS1, BIT(13)),
  360. MT6331_LDO_S("ldo-vgp1", VGP1, ldo_volt_table3, MT6331_DIGLDO_CON6, 10,
  361. MT6331_DIGLDO_CON19, GENMASK(6, 4), MT6331_DIGLDO_CON6, GENMASK(1, 0),
  362. MT6331_EN_STATUS1, BIT(4)),
  363. MT6331_LDO_S("ldo-vgp2", VGP2, ldo_volt_table10, MT6331_SYSLDO_CON6, 10,
  364. MT6331_SYSLDO_CON14, GENMASK(4, 3), MT6331_SYSLDO_CON6, GENMASK(1, 0),
  365. MT6331_EN_STATUS1, BIT(15)),
  366. MT6331_LDO_S("ldo-vgp3", VGP3, ldo_volt_table10, MT6331_SYSLDO_CON7, 10,
  367. MT6331_SYSLDO_CON15, GENMASK(4, 3), MT6331_SYSLDO_CON7, GENMASK(1, 0),
  368. MT6331_EN_STATUS2, BIT(0)),
  369. MT6331_LDO_S("ldo-vgp4", VGP4, ldo_volt_table7, MT6331_DIGLDO_CON7, 10,
  370. MT6331_DIGLDO_CON18, GENMASK(6, 4), MT6331_DIGLDO_CON7, GENMASK(1, 0),
  371. MT6331_EN_STATUS1, BIT(5)),
  372. MT6331_LDO_AO("ldo-vdig18", VDIG18, ldo_volt_table11,
  373. MT6331_DIGLDO_CON28, GENMASK(14, 12)),
  374. };
  375. static int mt6331_set_buck_vosel_reg(struct platform_device *pdev)
  376. {
  377. struct mt6397_chip *mt6331 = dev_get_drvdata(pdev->dev.parent);
  378. int i;
  379. u32 regval;
  380. for (i = 0; i < MT6331_ID_VREG_MAX; i++) {
  381. if (mt6331_regulators[i].vselctrl_reg) {
  382. if (regmap_read(mt6331->regmap,
  383. mt6331_regulators[i].vselctrl_reg,
  384. &regval) < 0) {
  385. dev_err(&pdev->dev,
  386. "Failed to read buck ctrl\n");
  387. return -EIO;
  388. }
  389. if (regval & mt6331_regulators[i].vselctrl_mask) {
  390. mt6331_regulators[i].desc.vsel_reg =
  391. mt6331_regulators[i].vselon_reg;
  392. }
  393. }
  394. }
  395. return 0;
  396. }
  397. static int mt6331_regulator_probe(struct platform_device *pdev)
  398. {
  399. struct mt6397_chip *mt6331 = dev_get_drvdata(pdev->dev.parent);
  400. struct regulator_config config = {};
  401. struct regulator_dev *rdev;
  402. int i;
  403. u32 reg_value;
  404. /* Query buck controller to select activated voltage register part */
  405. if (mt6331_set_buck_vosel_reg(pdev))
  406. return -EIO;
  407. /* Read PMIC chip revision to update constraints and voltage table */
  408. if (regmap_read(mt6331->regmap, MT6331_HWCID, &reg_value) < 0) {
  409. dev_err(&pdev->dev, "Failed to read Chip ID\n");
  410. return -EIO;
  411. }
  412. reg_value &= GENMASK(7, 0);
  413. dev_info(&pdev->dev, "Chip ID = 0x%x\n", reg_value);
  414. /*
  415. * ChipID 0x10 is "MT6331 E1", has a different voltage table and
  416. * it's currently not supported in this driver. Upon detection of
  417. * this ID, refuse to register the regulators, as we will wrongly
  418. * interpret the VSEL for this revision, potentially overvolting
  419. * some device.
  420. */
  421. if (reg_value == 0x10) {
  422. dev_err(&pdev->dev, "Chip version not supported. Bailing out.\n");
  423. return -EINVAL;
  424. }
  425. for (i = 0; i < MT6331_ID_VREG_MAX; i++) {
  426. config.dev = &pdev->dev;
  427. config.driver_data = &mt6331_regulators[i];
  428. config.regmap = mt6331->regmap;
  429. rdev = devm_regulator_register(&pdev->dev,
  430. &mt6331_regulators[i].desc, &config);
  431. if (IS_ERR(rdev)) {
  432. dev_err(&pdev->dev, "failed to register %s\n",
  433. mt6331_regulators[i].desc.name);
  434. return PTR_ERR(rdev);
  435. }
  436. }
  437. return 0;
  438. }
  439. static const struct platform_device_id mt6331_platform_ids[] = {
  440. {"mt6331-regulator", 0},
  441. { /* sentinel */ },
  442. };
  443. MODULE_DEVICE_TABLE(platform, mt6331_platform_ids);
  444. static struct platform_driver mt6331_regulator_driver = {
  445. .driver = {
  446. .name = "mt6331-regulator",
  447. },
  448. .probe = mt6331_regulator_probe,
  449. .id_table = mt6331_platform_ids,
  450. };
  451. module_platform_driver(mt6331_regulator_driver);
  452. MODULE_AUTHOR("AngeloGioacchino Del Regno <[email protected]>");
  453. MODULE_DESCRIPTION("Regulator Driver for MediaTek MT6331 PMIC");
  454. MODULE_LICENSE("GPL");