mt6315-misc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2019 MediaTek Inc.
  4. * Copyright (C) 2021 XiaoMi, Inc.
  5. */
  6. #include <linux/mfd/mt6315/registers.h>
  7. #include <linux/pmif.h>
  8. #include <linux/regmap.h>
  9. #include <linux/regulator/consumer.h>
  10. #include <linux/regulator/mt6315-misc.h>
  11. #include <linux/regulator/mt6315-regulator.h>
  12. #define LP_INIT_SETTING_VERIFIED 1
  13. #define MT6315_DECL_CHIP(_mid, _saddr)\
  14. { \
  15. .master_idx = _mid, \
  16. .slave_addr = _saddr, \
  17. .regmap = NULL, \
  18. }
  19. static struct mt6315_misc mt6315_misc[] = {
  20. #if defined(CONFIG_MACH_MT6885) || defined(CONFIG_MACH_MT6873) \
  21. || defined(CONFIG_MACH_MT6893)
  22. MT6315_DECL_CHIP(SPMI_MASTER_0, MT6315_SLAVE_ID_6),
  23. MT6315_DECL_CHIP(SPMI_MASTER_0, MT6315_SLAVE_ID_7),
  24. MT6315_DECL_CHIP(SPMI_MASTER_0, MT6315_SLAVE_ID_3),
  25. #elif defined(CONFIG_MACH_MT6833) || defined(CONFIG_MACH_MT6853)
  26. MT6315_DECL_CHIP(SPMI_MASTER_0, MT6315_SLAVE_ID_3),
  27. #endif
  28. };
  29. static struct mt6315_misc *mt6315_find_chip_sid(u32 sid)
  30. {
  31. int i;
  32. for (i = 0; i < ARRAY_SIZE(mt6315_misc); i++) {
  33. if (mt6315_misc[i].slave_addr == sid)
  34. return &mt6315_misc[i];
  35. }
  36. return NULL;
  37. }
  38. static unsigned int g_vmodem_vosel;
  39. static unsigned int g_vnr_vosel;
  40. static unsigned int g_vsram_md_vosel;
  41. static void mt6315_S3_default_vosel(void)
  42. {
  43. struct mt6315_misc *mt6315;
  44. struct regmap *regmap;
  45. mt6315 = mt6315_find_chip_sid(MT6315_SLAVE_ID_3);
  46. if (!mt6315) {
  47. pr_info("%s MT6315S3 not ready.\n", __func__);
  48. return;
  49. }
  50. regmap = mt6315->regmap;
  51. if (!regmap) {
  52. pr_info("%s null regmap.\n", __func__);
  53. return;
  54. }
  55. if (g_vmodem_vosel != 0) {
  56. #if defined(CONFIG_MACH_MT6885) || defined(CONFIG_MACH_MT6873) \
  57. || defined(CONFIG_MACH_MT6893)
  58. regmap_write(regmap, MT6315_PMIC_RG_BUCK_VBUCK1_VOSEL_ADDR,
  59. g_vmodem_vosel);
  60. regmap_write(regmap, MT6315_PMIC_RG_BUCK_VBUCK3_VOSEL_ADDR,
  61. g_vnr_vosel);
  62. regmap_write(regmap, MT6315_PMIC_RG_BUCK_VBUCK4_VOSEL_ADDR,
  63. g_vsram_md_vosel);
  64. #elif defined(CONFIG_MACH_MT6833) || defined(CONFIG_MACH_MT6853)
  65. regmap_write(regmap, MT6315_PMIC_RG_BUCK_VBUCK4_VOSEL_ADDR,
  66. g_vsram_md_vosel);
  67. regmap_write(regmap, MT6315_PMIC_RG_BUCK_VBUCK1_VOSEL_ADDR,
  68. g_vmodem_vosel);
  69. #endif
  70. pr_info("[%s] set vmodem=0x%x, vnr=0x%x, vsram_md=0x%x\n"
  71. , __func__, g_vmodem_vosel, g_vnr_vosel,
  72. g_vsram_md_vosel);
  73. } else {
  74. #if defined(CONFIG_MACH_MT6885) || defined(CONFIG_MACH_MT6873) \
  75. || defined(CONFIG_MACH_MT6893)
  76. regmap_read(regmap, MT6315_PMIC_DA_VBUCK1_VOSEL_ADDR,
  77. &g_vmodem_vosel);
  78. regmap_read(regmap, MT6315_PMIC_DA_VBUCK3_VOSEL_ADDR,
  79. &g_vnr_vosel);
  80. regmap_read(regmap, MT6315_PMIC_DA_VBUCK4_VOSEL_ADDR,
  81. &g_vsram_md_vosel);
  82. #elif defined(CONFIG_MACH_MT6833) || defined(CONFIG_MACH_MT6853)
  83. regmap_read(regmap, MT6315_PMIC_DA_VBUCK1_VOSEL_ADDR,
  84. &g_vmodem_vosel);
  85. g_vnr_vosel = g_vmodem_vosel;
  86. regmap_read(regmap, MT6315_PMIC_DA_VBUCK4_VOSEL_ADDR,
  87. &g_vsram_md_vosel);
  88. #endif
  89. pr_info("[%s] record vmodem=0x%x, vnr=0x%x, vsram_md=0x%x\n"
  90. , __func__, g_vmodem_vosel, g_vnr_vosel,
  91. g_vsram_md_vosel);
  92. }
  93. }
  94. void mt6315_vmd1_pmic_setting_on(void)
  95. {
  96. /* Reset VMODEM/VNR/VSRAM_MD default voltage */
  97. mt6315_S3_default_vosel();
  98. }
  99. static int is_mt6315_S3_exist(void)
  100. {
  101. int ret = 0;
  102. struct regulator *reg;
  103. reg = regulator_get_optional(NULL, "3_vbuck1");
  104. if (IS_ERR(reg))
  105. return 0;
  106. if (regulator_is_enabled(reg))
  107. ret = 1;
  108. regulator_put(reg);
  109. return ret;
  110. }
  111. static int is_mt6315_S6_exist(void)
  112. {
  113. int ret = 0;
  114. struct regulator *reg;
  115. reg = regulator_get_optional(NULL, "6_vbuck1");
  116. if (IS_ERR(reg))
  117. return 0;
  118. if (regulator_is_enabled(reg))
  119. ret = 1;
  120. regulator_put(reg);
  121. return ret;
  122. }
  123. static int is_mt6315_S7_exist(void)
  124. {
  125. int ret = 0;
  126. struct regulator *reg;
  127. reg = regulator_get_optional(NULL, "7_vbuck1");
  128. if (IS_ERR(reg))
  129. return 0;
  130. if (regulator_is_enabled(reg))
  131. ret = 1;
  132. regulator_put(reg);
  133. return ret;
  134. }
  135. int is_mt6315_exist(void)
  136. {
  137. #if defined(CONFIG_MACH_MT6885) || defined(CONFIG_MACH_MT6873) \
  138. || defined(CONFIG_MACH_MT6893)
  139. pr_info("%s S3:%d S6:%d S7:%d\n", __func__, is_mt6315_S3_exist()
  140. , is_mt6315_S6_exist(), is_mt6315_S7_exist());
  141. if (is_mt6315_S3_exist() && is_mt6315_S6_exist() &&
  142. is_mt6315_S7_exist())
  143. return 1;
  144. #elif defined(CONFIG_MACH_MT6833) || defined(CONFIG_MACH_MT6853)
  145. pr_info("%s S3:%d\n", __func__, is_mt6315_S3_exist());
  146. return is_mt6315_S3_exist();
  147. #endif
  148. return 0;
  149. }
  150. #if LP_INIT_SETTING_VERIFIED
  151. static void mt6315_vbuck1_lp_setting(struct regmap *regmap,
  152. enum MT6315_BUCK_EN_USER user, unsigned char mode,
  153. unsigned char en, unsigned char cfg)
  154. {
  155. if (user == MT6315_SRCLKEN0) {
  156. regmap_update_bits(regmap,
  157. MT6315_PMIC_RG_BUCK_VBUCK1_HW0_OP_MODE_ADDR,
  158. 0x1 << MT6315_PMIC_RG_BUCK_VBUCK1_HW0_OP_MODE_SHIFT,
  159. mode << MT6315_PMIC_RG_BUCK_VBUCK1_HW0_OP_MODE_SHIFT);
  160. regmap_update_bits(regmap,
  161. MT6315_PMIC_RG_BUCK_VBUCK1_HW0_OP_EN_ADDR,
  162. 0x1 << MT6315_PMIC_RG_BUCK_VBUCK1_HW0_OP_EN_SHIFT,
  163. en << MT6315_PMIC_RG_BUCK_VBUCK1_HW0_OP_EN_SHIFT);
  164. regmap_update_bits(regmap,
  165. MT6315_PMIC_RG_BUCK_VBUCK1_HW0_OP_CFG_ADDR,
  166. 0x1 << MT6315_PMIC_RG_BUCK_VBUCK1_HW0_OP_CFG_SHIFT,
  167. cfg << MT6315_PMIC_RG_BUCK_VBUCK1_HW0_OP_CFG_SHIFT);
  168. } else {
  169. pr_info("%s non support user control(%d).\n", __func__, user);
  170. }
  171. }
  172. static void mt6315_vbuck2_lp_setting(struct regmap *regmap,
  173. enum MT6315_BUCK_EN_USER user, unsigned char mode,
  174. unsigned char en, unsigned char cfg)
  175. {
  176. if (user == MT6315_SRCLKEN0) {
  177. regmap_update_bits(regmap,
  178. MT6315_PMIC_RG_BUCK_VBUCK2_HW0_OP_MODE_ADDR,
  179. 0x1 << MT6315_PMIC_RG_BUCK_VBUCK2_HW0_OP_MODE_SHIFT,
  180. mode << MT6315_PMIC_RG_BUCK_VBUCK2_HW0_OP_MODE_SHIFT);
  181. regmap_update_bits(regmap,
  182. MT6315_PMIC_RG_BUCK_VBUCK2_HW0_OP_EN_ADDR,
  183. 0x1 << MT6315_PMIC_RG_BUCK_VBUCK2_HW0_OP_EN_SHIFT,
  184. en << MT6315_PMIC_RG_BUCK_VBUCK2_HW0_OP_EN_SHIFT);
  185. regmap_update_bits(regmap,
  186. MT6315_PMIC_RG_BUCK_VBUCK2_HW0_OP_CFG_ADDR,
  187. 0x1 << MT6315_PMIC_RG_BUCK_VBUCK2_HW0_OP_CFG_SHIFT,
  188. cfg << MT6315_PMIC_RG_BUCK_VBUCK2_HW0_OP_CFG_SHIFT);
  189. } else {
  190. pr_info("%s non support user control(%d).\n", __func__, user);
  191. }
  192. }
  193. static void mt6315_vbuck3_lp_setting(struct regmap *regmap,
  194. enum MT6315_BUCK_EN_USER user, unsigned char mode,
  195. unsigned char en, unsigned char cfg)
  196. {
  197. if (user == MT6315_SRCLKEN0) {
  198. regmap_update_bits(regmap,
  199. MT6315_PMIC_RG_BUCK_VBUCK3_HW0_OP_MODE_ADDR,
  200. 0x1 << MT6315_PMIC_RG_BUCK_VBUCK3_HW0_OP_MODE_SHIFT,
  201. mode << MT6315_PMIC_RG_BUCK_VBUCK3_HW0_OP_MODE_SHIFT);
  202. regmap_update_bits(regmap,
  203. MT6315_PMIC_RG_BUCK_VBUCK3_HW0_OP_EN_ADDR,
  204. 0x1 << MT6315_PMIC_RG_BUCK_VBUCK3_HW0_OP_EN_SHIFT,
  205. en << MT6315_PMIC_RG_BUCK_VBUCK3_HW0_OP_EN_SHIFT);
  206. regmap_update_bits(regmap,
  207. MT6315_PMIC_RG_BUCK_VBUCK3_HW0_OP_CFG_ADDR,
  208. 0x1 << MT6315_PMIC_RG_BUCK_VBUCK3_HW0_OP_CFG_SHIFT,
  209. cfg << MT6315_PMIC_RG_BUCK_VBUCK3_HW0_OP_CFG_SHIFT);
  210. } else {
  211. pr_info("%s non support user control(%d).\n", __func__, user);
  212. }
  213. }
  214. static void mt6315_vbuck4_lp_setting(struct regmap *regmap,
  215. enum MT6315_BUCK_EN_USER user, unsigned char mode,
  216. unsigned char en, unsigned char cfg)
  217. {
  218. if (user == MT6315_SRCLKEN0) {
  219. regmap_update_bits(regmap,
  220. MT6315_PMIC_RG_BUCK_VBUCK4_HW0_OP_MODE_ADDR,
  221. 0x1 << MT6315_PMIC_RG_BUCK_VBUCK4_HW0_OP_MODE_SHIFT,
  222. mode << MT6315_PMIC_RG_BUCK_VBUCK4_HW0_OP_MODE_SHIFT);
  223. regmap_update_bits(regmap,
  224. MT6315_PMIC_RG_BUCK_VBUCK4_HW0_OP_EN_ADDR,
  225. 0x1 << MT6315_PMIC_RG_BUCK_VBUCK4_HW0_OP_EN_SHIFT,
  226. en << MT6315_PMIC_RG_BUCK_VBUCK4_HW0_OP_EN_SHIFT);
  227. regmap_update_bits(regmap,
  228. MT6315_PMIC_RG_BUCK_VBUCK4_HW0_OP_CFG_ADDR,
  229. 0x1 << MT6315_PMIC_RG_BUCK_VBUCK4_HW0_OP_CFG_SHIFT,
  230. cfg << MT6315_PMIC_RG_BUCK_VBUCK4_HW0_OP_CFG_SHIFT);
  231. } else {
  232. pr_info("%s non support user control(%d).\n", __func__, user);
  233. }
  234. }
  235. static void mt6315_lp_set(unsigned char slave_id, unsigned char buck_id,
  236. enum MT6315_BUCK_EN_USER user, unsigned char op_mode,
  237. unsigned char op_en, unsigned char op_cfg)
  238. {
  239. struct mt6315_misc *mt6315;
  240. struct regmap *regmap;
  241. mt6315 = mt6315_find_chip_sid(slave_id);
  242. if (!mt6315) {
  243. pr_info("%s MT6315S%d not ready\n", __func__, slave_id);
  244. return;
  245. }
  246. regmap = mt6315->regmap;
  247. if (!regmap) {
  248. pr_info("%s null regmap.\n", __func__);
  249. return;
  250. }
  251. if (buck_id == 1)
  252. mt6315_vbuck1_lp_setting(regmap, user, op_mode, op_en, op_cfg);
  253. else if (buck_id == 2)
  254. mt6315_vbuck2_lp_setting(regmap, user, op_mode, op_en, op_cfg);
  255. else if (buck_id == 3)
  256. mt6315_vbuck3_lp_setting(regmap, user, op_mode, op_en, op_cfg);
  257. else if (buck_id == 4)
  258. mt6315_vbuck4_lp_setting(regmap, user, op_mode, op_en, op_cfg);
  259. else
  260. pr_info("%s invalid buck_id=%d.\n", __func__, buck_id);
  261. }
  262. /* enable VDIG18 SRCLKEN low power mode */
  263. static void mt6315_vdig18_hw_op_set(unsigned char slave_id, unsigned char en)
  264. {
  265. struct mt6315_misc *mt6315;
  266. struct regmap *regmap;
  267. mt6315 = mt6315_find_chip_sid(slave_id);
  268. if (!mt6315) {
  269. pr_info("%s MT6315S%d not ready\n", __func__, slave_id);
  270. return;
  271. }
  272. regmap = mt6315->regmap;
  273. if (!regmap) {
  274. pr_info("%s null regmap.\n", __func__);
  275. return;
  276. }
  277. regmap_write(regmap, MT6315_PMIC_DIG_WPK_KEY_H_ADDR, 0x63);
  278. regmap_write(regmap, MT6315_PMIC_DIG_WPK_KEY_ADDR, 0x15);
  279. regmap_update_bits(regmap,
  280. MT6315_PMIC_RG_LDO_VDIG18_HW_OP_EN_ADDR,
  281. 0x1 << MT6315_PMIC_RG_LDO_VDIG18_HW_OP_EN_SHIFT,
  282. en << MT6315_PMIC_RG_LDO_VDIG18_HW_OP_EN_SHIFT);
  283. regmap_write(regmap, MT6315_PMIC_DIG_WPK_KEY_ADDR, 0x0);
  284. regmap_write(regmap, MT6315_PMIC_DIG_WPK_KEY_H_ADDR, 0x0);
  285. }
  286. #endif /* End of LP_INIT_SETTING_VERIFIED */
  287. static void mt6315_S3_lp_initial_setting(void)
  288. {
  289. #if LP_INIT_SETTING_VERIFIED
  290. #if defined(CONFIG_MACH_MT6885) || defined(CONFIG_MACH_MT6873) \
  291. || defined(CONFIG_MACH_MT6893)
  292. mt6315_vdig18_hw_op_set(MT6315_SLAVE_ID_3, 1);
  293. /* vmodem/vnr/vsram_md */
  294. mt6315_lp_set(MT6315_SLAVE_ID_3, 1, MT6315_SRCLKEN0, 1, 1, HW_LP);
  295. mt6315_lp_set(MT6315_SLAVE_ID_3, 3, MT6315_SRCLKEN0, 1, 1, HW_LP);
  296. mt6315_lp_set(MT6315_SLAVE_ID_3, 4, MT6315_SRCLKEN0, 1, 1, HW_LP);
  297. #elif defined(CONFIG_MACH_MT6833) || defined(CONFIG_MACH_MT6853)
  298. mt6315_vdig18_hw_op_set(MT6315_SLAVE_ID_3, 1);
  299. /* vmodem/vpu/vsram_md */
  300. mt6315_lp_set(MT6315_SLAVE_ID_3, 1, MT6315_SRCLKEN0, 1, 1, HW_LP);
  301. mt6315_lp_set(MT6315_SLAVE_ID_3, 3, MT6315_SRCLKEN0, 1, 1, HW_LP);
  302. mt6315_lp_set(MT6315_SLAVE_ID_3, 4, MT6315_SRCLKEN0, 1, 1, HW_LP);
  303. #endif
  304. #endif
  305. }
  306. static void mt6315_S6_lp_initial_setting(void)
  307. {
  308. #if LP_INIT_SETTING_VERIFIED
  309. #if defined(CONFIG_MACH_MT6885) || defined(CONFIG_MACH_MT6873) \
  310. || defined(CONFIG_MACH_MT6893)
  311. mt6315_vdig18_hw_op_set(MT6315_SLAVE_ID_6, 1);
  312. #endif
  313. #endif
  314. }
  315. static void mt6315_S7_lp_initial_setting(void)
  316. {
  317. #if LP_INIT_SETTING_VERIFIED
  318. #if defined(CONFIG_MACH_MT6893)
  319. mt6315_vdig18_hw_op_set(MT6315_SLAVE_ID_7, 1);
  320. /* vsram_core */
  321. mt6315_lp_set(MT6315_SLAVE_ID_7, 4, MT6315_SRCLKEN0, 1, 1, HW_LP);
  322. #elif defined(CONFIG_MACH_MT6885)
  323. mt6315_vdig18_hw_op_set(MT6315_SLAVE_ID_7, 1);
  324. /* vsram_core */
  325. mt6315_lp_set(MT6315_SLAVE_ID_7, 3, MT6315_SRCLKEN0, 1, 1, HW_LP);
  326. #elif defined(CONFIG_MACH_MT6873)
  327. mt6315_vdig18_hw_op_set(MT6315_SLAVE_ID_7, 1);
  328. #endif
  329. #endif
  330. }
  331. static void mt6315_misc_initial_setting(u32 sid)
  332. {
  333. switch (sid) {
  334. case MT6315_SLAVE_ID_3:
  335. mt6315_S3_lp_initial_setting();
  336. mt6315_S3_default_vosel();
  337. break;
  338. case MT6315_SLAVE_ID_6:
  339. mt6315_S6_lp_initial_setting();
  340. break;
  341. case MT6315_SLAVE_ID_7:
  342. mt6315_S7_lp_initial_setting();
  343. break;
  344. default:
  345. pr_info("unsupported chip sid: %d\n", sid);
  346. return;
  347. }
  348. }
  349. void mt6315_misc_init(u32 sid, struct regmap *regmap)
  350. {
  351. struct mt6315_misc *mt6315;
  352. mt6315 = mt6315_find_chip_sid(sid);
  353. if (mt6315)
  354. mt6315->regmap = regmap;
  355. mt6315_misc_initial_setting(sid);
  356. pr_info("%s sid=%d done\n", __func__, sid);
  357. }