mt8195-afe-clk.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * mt8195-afe-clk.c -- Mediatek 8195 afe clock ctrl
  4. *
  5. * Copyright (c) 2021 MediaTek Inc.
  6. * Author: Bicycle Tsai <[email protected]>
  7. * Trevor Wu <[email protected]>
  8. */
  9. #include <linux/clk.h>
  10. #include "mt8195-afe-common.h"
  11. #include "mt8195-afe-clk.h"
  12. #include "mt8195-reg.h"
  13. #include "mt8195-audsys-clk.h"
  14. static const char *aud_clks[MT8195_CLK_NUM] = {
  15. /* xtal */
  16. [MT8195_CLK_XTAL_26M] = "clk26m",
  17. /* divider */
  18. [MT8195_CLK_TOP_APLL1] = "apll1_ck",
  19. [MT8195_CLK_TOP_APLL2] = "apll2_ck",
  20. [MT8195_CLK_TOP_APLL12_DIV0] = "apll12_div0",
  21. [MT8195_CLK_TOP_APLL12_DIV1] = "apll12_div1",
  22. [MT8195_CLK_TOP_APLL12_DIV2] = "apll12_div2",
  23. [MT8195_CLK_TOP_APLL12_DIV3] = "apll12_div3",
  24. [MT8195_CLK_TOP_APLL12_DIV9] = "apll12_div9",
  25. /* mux */
  26. [MT8195_CLK_TOP_A1SYS_HP_SEL] = "a1sys_hp_sel",
  27. [MT8195_CLK_TOP_AUD_INTBUS_SEL] = "aud_intbus_sel",
  28. [MT8195_CLK_TOP_AUDIO_H_SEL] = "audio_h_sel",
  29. [MT8195_CLK_TOP_AUDIO_LOCAL_BUS_SEL] = "audio_local_bus_sel",
  30. [MT8195_CLK_TOP_DPTX_M_SEL] = "dptx_m_sel",
  31. [MT8195_CLK_TOP_I2SO1_M_SEL] = "i2so1_m_sel",
  32. [MT8195_CLK_TOP_I2SO2_M_SEL] = "i2so2_m_sel",
  33. [MT8195_CLK_TOP_I2SI1_M_SEL] = "i2si1_m_sel",
  34. [MT8195_CLK_TOP_I2SI2_M_SEL] = "i2si2_m_sel",
  35. /* clock gate */
  36. [MT8195_CLK_INFRA_AO_AUDIO_26M_B] = "infra_ao_audio_26m_b",
  37. [MT8195_CLK_SCP_ADSP_AUDIODSP] = "scp_adsp_audiodsp",
  38. /* afe clock gate */
  39. [MT8195_CLK_AUD_AFE] = "aud_afe",
  40. [MT8195_CLK_AUD_APLL1_TUNER] = "aud_apll1_tuner",
  41. [MT8195_CLK_AUD_APLL2_TUNER] = "aud_apll2_tuner",
  42. [MT8195_CLK_AUD_APLL] = "aud_apll",
  43. [MT8195_CLK_AUD_APLL2] = "aud_apll2",
  44. [MT8195_CLK_AUD_DAC] = "aud_dac",
  45. [MT8195_CLK_AUD_ADC] = "aud_adc",
  46. [MT8195_CLK_AUD_DAC_HIRES] = "aud_dac_hires",
  47. [MT8195_CLK_AUD_A1SYS_HP] = "aud_a1sys_hp",
  48. [MT8195_CLK_AUD_ADC_HIRES] = "aud_adc_hires",
  49. [MT8195_CLK_AUD_ADDA6_ADC] = "aud_adda6_adc",
  50. [MT8195_CLK_AUD_ADDA6_ADC_HIRES] = "aud_adda6_adc_hires",
  51. [MT8195_CLK_AUD_I2SIN] = "aud_i2sin",
  52. [MT8195_CLK_AUD_TDM_IN] = "aud_tdm_in",
  53. [MT8195_CLK_AUD_I2S_OUT] = "aud_i2s_out",
  54. [MT8195_CLK_AUD_TDM_OUT] = "aud_tdm_out",
  55. [MT8195_CLK_AUD_HDMI_OUT] = "aud_hdmi_out",
  56. [MT8195_CLK_AUD_ASRC11] = "aud_asrc11",
  57. [MT8195_CLK_AUD_ASRC12] = "aud_asrc12",
  58. [MT8195_CLK_AUD_A1SYS] = "aud_a1sys",
  59. [MT8195_CLK_AUD_A2SYS] = "aud_a2sys",
  60. [MT8195_CLK_AUD_PCMIF] = "aud_pcmif",
  61. [MT8195_CLK_AUD_MEMIF_UL1] = "aud_memif_ul1",
  62. [MT8195_CLK_AUD_MEMIF_UL2] = "aud_memif_ul2",
  63. [MT8195_CLK_AUD_MEMIF_UL3] = "aud_memif_ul3",
  64. [MT8195_CLK_AUD_MEMIF_UL4] = "aud_memif_ul4",
  65. [MT8195_CLK_AUD_MEMIF_UL5] = "aud_memif_ul5",
  66. [MT8195_CLK_AUD_MEMIF_UL6] = "aud_memif_ul6",
  67. [MT8195_CLK_AUD_MEMIF_UL8] = "aud_memif_ul8",
  68. [MT8195_CLK_AUD_MEMIF_UL9] = "aud_memif_ul9",
  69. [MT8195_CLK_AUD_MEMIF_UL10] = "aud_memif_ul10",
  70. [MT8195_CLK_AUD_MEMIF_DL2] = "aud_memif_dl2",
  71. [MT8195_CLK_AUD_MEMIF_DL3] = "aud_memif_dl3",
  72. [MT8195_CLK_AUD_MEMIF_DL6] = "aud_memif_dl6",
  73. [MT8195_CLK_AUD_MEMIF_DL7] = "aud_memif_dl7",
  74. [MT8195_CLK_AUD_MEMIF_DL8] = "aud_memif_dl8",
  75. [MT8195_CLK_AUD_MEMIF_DL10] = "aud_memif_dl10",
  76. [MT8195_CLK_AUD_MEMIF_DL11] = "aud_memif_dl11",
  77. };
  78. struct mt8195_afe_tuner_cfg {
  79. unsigned int id;
  80. int apll_div_reg;
  81. unsigned int apll_div_shift;
  82. unsigned int apll_div_maskbit;
  83. unsigned int apll_div_default;
  84. int ref_ck_sel_reg;
  85. unsigned int ref_ck_sel_shift;
  86. unsigned int ref_ck_sel_maskbit;
  87. unsigned int ref_ck_sel_default;
  88. int tuner_en_reg;
  89. unsigned int tuner_en_shift;
  90. unsigned int tuner_en_maskbit;
  91. int upper_bound_reg;
  92. unsigned int upper_bound_shift;
  93. unsigned int upper_bound_maskbit;
  94. unsigned int upper_bound_default;
  95. spinlock_t ctrl_lock; /* lock for apll tuner ctrl*/
  96. int ref_cnt;
  97. };
  98. static struct mt8195_afe_tuner_cfg mt8195_afe_tuner_cfgs[MT8195_AUD_PLL_NUM] = {
  99. [MT8195_AUD_PLL1] = {
  100. .id = MT8195_AUD_PLL1,
  101. .apll_div_reg = AFE_APLL_TUNER_CFG,
  102. .apll_div_shift = 4,
  103. .apll_div_maskbit = 0xf,
  104. .apll_div_default = 0x7,
  105. .ref_ck_sel_reg = AFE_APLL_TUNER_CFG,
  106. .ref_ck_sel_shift = 1,
  107. .ref_ck_sel_maskbit = 0x3,
  108. .ref_ck_sel_default = 0x2,
  109. .tuner_en_reg = AFE_APLL_TUNER_CFG,
  110. .tuner_en_shift = 0,
  111. .tuner_en_maskbit = 0x1,
  112. .upper_bound_reg = AFE_APLL_TUNER_CFG,
  113. .upper_bound_shift = 8,
  114. .upper_bound_maskbit = 0xff,
  115. .upper_bound_default = 0x3,
  116. },
  117. [MT8195_AUD_PLL2] = {
  118. .id = MT8195_AUD_PLL2,
  119. .apll_div_reg = AFE_APLL_TUNER_CFG1,
  120. .apll_div_shift = 4,
  121. .apll_div_maskbit = 0xf,
  122. .apll_div_default = 0x7,
  123. .ref_ck_sel_reg = AFE_APLL_TUNER_CFG1,
  124. .ref_ck_sel_shift = 1,
  125. .ref_ck_sel_maskbit = 0x3,
  126. .ref_ck_sel_default = 0x1,
  127. .tuner_en_reg = AFE_APLL_TUNER_CFG1,
  128. .tuner_en_shift = 0,
  129. .tuner_en_maskbit = 0x1,
  130. .upper_bound_reg = AFE_APLL_TUNER_CFG1,
  131. .upper_bound_shift = 8,
  132. .upper_bound_maskbit = 0xff,
  133. .upper_bound_default = 0x3,
  134. },
  135. [MT8195_AUD_PLL3] = {
  136. .id = MT8195_AUD_PLL3,
  137. .apll_div_reg = AFE_EARC_APLL_TUNER_CFG,
  138. .apll_div_shift = 4,
  139. .apll_div_maskbit = 0x3f,
  140. .apll_div_default = 0x3,
  141. .ref_ck_sel_reg = AFE_EARC_APLL_TUNER_CFG,
  142. .ref_ck_sel_shift = 24,
  143. .ref_ck_sel_maskbit = 0x3,
  144. .ref_ck_sel_default = 0x0,
  145. .tuner_en_reg = AFE_EARC_APLL_TUNER_CFG,
  146. .tuner_en_shift = 0,
  147. .tuner_en_maskbit = 0x1,
  148. .upper_bound_reg = AFE_EARC_APLL_TUNER_CFG,
  149. .upper_bound_shift = 12,
  150. .upper_bound_maskbit = 0xff,
  151. .upper_bound_default = 0x4,
  152. },
  153. [MT8195_AUD_PLL4] = {
  154. .id = MT8195_AUD_PLL4,
  155. .apll_div_reg = AFE_SPDIFIN_APLL_TUNER_CFG,
  156. .apll_div_shift = 4,
  157. .apll_div_maskbit = 0x3f,
  158. .apll_div_default = 0x7,
  159. .ref_ck_sel_reg = AFE_SPDIFIN_APLL_TUNER_CFG1,
  160. .ref_ck_sel_shift = 8,
  161. .ref_ck_sel_maskbit = 0x1,
  162. .ref_ck_sel_default = 0,
  163. .tuner_en_reg = AFE_SPDIFIN_APLL_TUNER_CFG,
  164. .tuner_en_shift = 0,
  165. .tuner_en_maskbit = 0x1,
  166. .upper_bound_reg = AFE_SPDIFIN_APLL_TUNER_CFG,
  167. .upper_bound_shift = 12,
  168. .upper_bound_maskbit = 0xff,
  169. .upper_bound_default = 0x4,
  170. },
  171. [MT8195_AUD_PLL5] = {
  172. .id = MT8195_AUD_PLL5,
  173. .apll_div_reg = AFE_LINEIN_APLL_TUNER_CFG,
  174. .apll_div_shift = 4,
  175. .apll_div_maskbit = 0x3f,
  176. .apll_div_default = 0x3,
  177. .ref_ck_sel_reg = AFE_LINEIN_APLL_TUNER_CFG,
  178. .ref_ck_sel_shift = 24,
  179. .ref_ck_sel_maskbit = 0x1,
  180. .ref_ck_sel_default = 0,
  181. .tuner_en_reg = AFE_LINEIN_APLL_TUNER_CFG,
  182. .tuner_en_shift = 0,
  183. .tuner_en_maskbit = 0x1,
  184. .upper_bound_reg = AFE_LINEIN_APLL_TUNER_CFG,
  185. .upper_bound_shift = 12,
  186. .upper_bound_maskbit = 0xff,
  187. .upper_bound_default = 0x4,
  188. },
  189. };
  190. static struct mt8195_afe_tuner_cfg *mt8195_afe_found_apll_tuner(unsigned int id)
  191. {
  192. if (id >= MT8195_AUD_PLL_NUM)
  193. return NULL;
  194. return &mt8195_afe_tuner_cfgs[id];
  195. }
  196. static int mt8195_afe_init_apll_tuner(unsigned int id)
  197. {
  198. struct mt8195_afe_tuner_cfg *cfg = mt8195_afe_found_apll_tuner(id);
  199. if (!cfg)
  200. return -EINVAL;
  201. cfg->ref_cnt = 0;
  202. spin_lock_init(&cfg->ctrl_lock);
  203. return 0;
  204. }
  205. static int mt8195_afe_setup_apll_tuner(struct mtk_base_afe *afe,
  206. unsigned int id)
  207. {
  208. const struct mt8195_afe_tuner_cfg *cfg = mt8195_afe_found_apll_tuner(id);
  209. if (!cfg)
  210. return -EINVAL;
  211. regmap_update_bits(afe->regmap, cfg->apll_div_reg,
  212. cfg->apll_div_maskbit << cfg->apll_div_shift,
  213. cfg->apll_div_default << cfg->apll_div_shift);
  214. regmap_update_bits(afe->regmap, cfg->ref_ck_sel_reg,
  215. cfg->ref_ck_sel_maskbit << cfg->ref_ck_sel_shift,
  216. cfg->ref_ck_sel_default << cfg->ref_ck_sel_shift);
  217. regmap_update_bits(afe->regmap, cfg->upper_bound_reg,
  218. cfg->upper_bound_maskbit << cfg->upper_bound_shift,
  219. cfg->upper_bound_default << cfg->upper_bound_shift);
  220. return 0;
  221. }
  222. static int mt8195_afe_enable_tuner_clk(struct mtk_base_afe *afe,
  223. unsigned int id)
  224. {
  225. struct mt8195_afe_private *afe_priv = afe->platform_priv;
  226. switch (id) {
  227. case MT8195_AUD_PLL1:
  228. mt8195_afe_enable_clk(afe, afe_priv->clk[MT8195_CLK_AUD_APLL]);
  229. mt8195_afe_enable_clk(afe, afe_priv->clk[MT8195_CLK_AUD_APLL1_TUNER]);
  230. break;
  231. case MT8195_AUD_PLL2:
  232. mt8195_afe_enable_clk(afe, afe_priv->clk[MT8195_CLK_AUD_APLL2]);
  233. mt8195_afe_enable_clk(afe, afe_priv->clk[MT8195_CLK_AUD_APLL2_TUNER]);
  234. break;
  235. default:
  236. break;
  237. }
  238. return 0;
  239. }
  240. static int mt8195_afe_disable_tuner_clk(struct mtk_base_afe *afe,
  241. unsigned int id)
  242. {
  243. struct mt8195_afe_private *afe_priv = afe->platform_priv;
  244. switch (id) {
  245. case MT8195_AUD_PLL1:
  246. mt8195_afe_disable_clk(afe, afe_priv->clk[MT8195_CLK_AUD_APLL1_TUNER]);
  247. mt8195_afe_disable_clk(afe, afe_priv->clk[MT8195_CLK_AUD_APLL]);
  248. break;
  249. case MT8195_AUD_PLL2:
  250. mt8195_afe_disable_clk(afe, afe_priv->clk[MT8195_CLK_AUD_APLL2_TUNER]);
  251. mt8195_afe_disable_clk(afe, afe_priv->clk[MT8195_CLK_AUD_APLL2]);
  252. break;
  253. default:
  254. break;
  255. }
  256. return 0;
  257. }
  258. static int mt8195_afe_enable_apll_tuner(struct mtk_base_afe *afe,
  259. unsigned int id)
  260. {
  261. struct mt8195_afe_tuner_cfg *cfg = mt8195_afe_found_apll_tuner(id);
  262. unsigned long flags;
  263. int ret;
  264. if (!cfg)
  265. return -EINVAL;
  266. ret = mt8195_afe_setup_apll_tuner(afe, id);
  267. if (ret)
  268. return ret;
  269. ret = mt8195_afe_enable_tuner_clk(afe, id);
  270. if (ret)
  271. return ret;
  272. spin_lock_irqsave(&cfg->ctrl_lock, flags);
  273. cfg->ref_cnt++;
  274. if (cfg->ref_cnt == 1)
  275. regmap_update_bits(afe->regmap,
  276. cfg->tuner_en_reg,
  277. cfg->tuner_en_maskbit << cfg->tuner_en_shift,
  278. 1 << cfg->tuner_en_shift);
  279. spin_unlock_irqrestore(&cfg->ctrl_lock, flags);
  280. return 0;
  281. }
  282. static int mt8195_afe_disable_apll_tuner(struct mtk_base_afe *afe,
  283. unsigned int id)
  284. {
  285. struct mt8195_afe_tuner_cfg *cfg = mt8195_afe_found_apll_tuner(id);
  286. unsigned long flags;
  287. int ret;
  288. if (!cfg)
  289. return -EINVAL;
  290. spin_lock_irqsave(&cfg->ctrl_lock, flags);
  291. cfg->ref_cnt--;
  292. if (cfg->ref_cnt == 0)
  293. regmap_update_bits(afe->regmap,
  294. cfg->tuner_en_reg,
  295. cfg->tuner_en_maskbit << cfg->tuner_en_shift,
  296. 0 << cfg->tuner_en_shift);
  297. else if (cfg->ref_cnt < 0)
  298. cfg->ref_cnt = 0;
  299. spin_unlock_irqrestore(&cfg->ctrl_lock, flags);
  300. ret = mt8195_afe_disable_tuner_clk(afe, id);
  301. if (ret)
  302. return ret;
  303. return 0;
  304. }
  305. int mt8195_afe_get_mclk_source_clk_id(int sel)
  306. {
  307. switch (sel) {
  308. case MT8195_MCK_SEL_26M:
  309. return MT8195_CLK_XTAL_26M;
  310. case MT8195_MCK_SEL_APLL1:
  311. return MT8195_CLK_TOP_APLL1;
  312. case MT8195_MCK_SEL_APLL2:
  313. return MT8195_CLK_TOP_APLL2;
  314. default:
  315. return -EINVAL;
  316. }
  317. }
  318. int mt8195_afe_get_mclk_source_rate(struct mtk_base_afe *afe, int apll)
  319. {
  320. struct mt8195_afe_private *afe_priv = afe->platform_priv;
  321. int clk_id = mt8195_afe_get_mclk_source_clk_id(apll);
  322. if (clk_id < 0) {
  323. dev_dbg(afe->dev, "invalid clk id\n");
  324. return 0;
  325. }
  326. return clk_get_rate(afe_priv->clk[clk_id]);
  327. }
  328. int mt8195_afe_get_default_mclk_source_by_rate(int rate)
  329. {
  330. return ((rate % 8000) == 0) ?
  331. MT8195_MCK_SEL_APLL1 : MT8195_MCK_SEL_APLL2;
  332. }
  333. int mt8195_afe_init_clock(struct mtk_base_afe *afe)
  334. {
  335. struct mt8195_afe_private *afe_priv = afe->platform_priv;
  336. int i, ret;
  337. mt8195_audsys_clk_register(afe);
  338. afe_priv->clk =
  339. devm_kcalloc(afe->dev, MT8195_CLK_NUM, sizeof(*afe_priv->clk),
  340. GFP_KERNEL);
  341. if (!afe_priv->clk)
  342. return -ENOMEM;
  343. for (i = 0; i < MT8195_CLK_NUM; i++) {
  344. afe_priv->clk[i] = devm_clk_get(afe->dev, aud_clks[i]);
  345. if (IS_ERR(afe_priv->clk[i])) {
  346. dev_dbg(afe->dev, "%s(), devm_clk_get %s fail, ret %ld\n",
  347. __func__, aud_clks[i],
  348. PTR_ERR(afe_priv->clk[i]));
  349. return PTR_ERR(afe_priv->clk[i]);
  350. }
  351. }
  352. /* initial tuner */
  353. for (i = 0; i < MT8195_AUD_PLL_NUM; i++) {
  354. ret = mt8195_afe_init_apll_tuner(i);
  355. if (ret) {
  356. dev_dbg(afe->dev, "%s(), init apll_tuner%d failed",
  357. __func__, (i + 1));
  358. return -EINVAL;
  359. }
  360. }
  361. return 0;
  362. }
  363. int mt8195_afe_enable_clk(struct mtk_base_afe *afe, struct clk *clk)
  364. {
  365. int ret;
  366. if (clk) {
  367. ret = clk_prepare_enable(clk);
  368. if (ret) {
  369. dev_dbg(afe->dev, "%s(), failed to enable clk\n",
  370. __func__);
  371. return ret;
  372. }
  373. } else {
  374. dev_dbg(afe->dev, "NULL clk\n");
  375. }
  376. return 0;
  377. }
  378. EXPORT_SYMBOL_GPL(mt8195_afe_enable_clk);
  379. void mt8195_afe_disable_clk(struct mtk_base_afe *afe, struct clk *clk)
  380. {
  381. if (clk)
  382. clk_disable_unprepare(clk);
  383. else
  384. dev_dbg(afe->dev, "NULL clk\n");
  385. }
  386. EXPORT_SYMBOL_GPL(mt8195_afe_disable_clk);
  387. int mt8195_afe_prepare_clk(struct mtk_base_afe *afe, struct clk *clk)
  388. {
  389. int ret;
  390. if (clk) {
  391. ret = clk_prepare(clk);
  392. if (ret) {
  393. dev_dbg(afe->dev, "%s(), failed to prepare clk\n",
  394. __func__);
  395. return ret;
  396. }
  397. } else {
  398. dev_dbg(afe->dev, "NULL clk\n");
  399. }
  400. return 0;
  401. }
  402. void mt8195_afe_unprepare_clk(struct mtk_base_afe *afe, struct clk *clk)
  403. {
  404. if (clk)
  405. clk_unprepare(clk);
  406. else
  407. dev_dbg(afe->dev, "NULL clk\n");
  408. }
  409. int mt8195_afe_enable_clk_atomic(struct mtk_base_afe *afe, struct clk *clk)
  410. {
  411. int ret;
  412. if (clk) {
  413. ret = clk_enable(clk);
  414. if (ret) {
  415. dev_dbg(afe->dev, "%s(), failed to clk enable\n",
  416. __func__);
  417. return ret;
  418. }
  419. } else {
  420. dev_dbg(afe->dev, "NULL clk\n");
  421. }
  422. return 0;
  423. }
  424. void mt8195_afe_disable_clk_atomic(struct mtk_base_afe *afe, struct clk *clk)
  425. {
  426. if (clk)
  427. clk_disable(clk);
  428. else
  429. dev_dbg(afe->dev, "NULL clk\n");
  430. }
  431. int mt8195_afe_set_clk_rate(struct mtk_base_afe *afe, struct clk *clk,
  432. unsigned int rate)
  433. {
  434. int ret;
  435. if (clk) {
  436. ret = clk_set_rate(clk, rate);
  437. if (ret) {
  438. dev_dbg(afe->dev, "%s(), failed to set clk rate\n",
  439. __func__);
  440. return ret;
  441. }
  442. }
  443. return 0;
  444. }
  445. int mt8195_afe_set_clk_parent(struct mtk_base_afe *afe, struct clk *clk,
  446. struct clk *parent)
  447. {
  448. int ret;
  449. if (clk && parent) {
  450. ret = clk_set_parent(clk, parent);
  451. if (ret) {
  452. dev_dbg(afe->dev, "%s(), failed to set clk parent\n",
  453. __func__);
  454. return ret;
  455. }
  456. }
  457. return 0;
  458. }
  459. static unsigned int get_top_cg_reg(unsigned int cg_type)
  460. {
  461. switch (cg_type) {
  462. case MT8195_TOP_CG_A1SYS_TIMING:
  463. case MT8195_TOP_CG_A2SYS_TIMING:
  464. case MT8195_TOP_CG_26M_TIMING:
  465. return ASYS_TOP_CON;
  466. default:
  467. return 0;
  468. }
  469. }
  470. static unsigned int get_top_cg_mask(unsigned int cg_type)
  471. {
  472. switch (cg_type) {
  473. case MT8195_TOP_CG_A1SYS_TIMING:
  474. return ASYS_TOP_CON_A1SYS_TIMING_ON;
  475. case MT8195_TOP_CG_A2SYS_TIMING:
  476. return ASYS_TOP_CON_A2SYS_TIMING_ON;
  477. case MT8195_TOP_CG_26M_TIMING:
  478. return ASYS_TOP_CON_26M_TIMING_ON;
  479. default:
  480. return 0;
  481. }
  482. }
  483. static unsigned int get_top_cg_on_val(unsigned int cg_type)
  484. {
  485. switch (cg_type) {
  486. case MT8195_TOP_CG_A1SYS_TIMING:
  487. case MT8195_TOP_CG_A2SYS_TIMING:
  488. case MT8195_TOP_CG_26M_TIMING:
  489. return get_top_cg_mask(cg_type);
  490. default:
  491. return 0;
  492. }
  493. }
  494. static unsigned int get_top_cg_off_val(unsigned int cg_type)
  495. {
  496. switch (cg_type) {
  497. case MT8195_TOP_CG_A1SYS_TIMING:
  498. case MT8195_TOP_CG_A2SYS_TIMING:
  499. case MT8195_TOP_CG_26M_TIMING:
  500. return 0;
  501. default:
  502. return get_top_cg_mask(cg_type);
  503. }
  504. }
  505. static int mt8195_afe_enable_top_cg(struct mtk_base_afe *afe, unsigned int cg_type)
  506. {
  507. unsigned int reg = get_top_cg_reg(cg_type);
  508. unsigned int mask = get_top_cg_mask(cg_type);
  509. unsigned int val = get_top_cg_on_val(cg_type);
  510. regmap_update_bits(afe->regmap, reg, mask, val);
  511. return 0;
  512. }
  513. static int mt8195_afe_disable_top_cg(struct mtk_base_afe *afe, unsigned int cg_type)
  514. {
  515. unsigned int reg = get_top_cg_reg(cg_type);
  516. unsigned int mask = get_top_cg_mask(cg_type);
  517. unsigned int val = get_top_cg_off_val(cg_type);
  518. regmap_update_bits(afe->regmap, reg, mask, val);
  519. return 0;
  520. }
  521. int mt8195_afe_enable_reg_rw_clk(struct mtk_base_afe *afe)
  522. {
  523. struct mt8195_afe_private *afe_priv = afe->platform_priv;
  524. int i;
  525. static const unsigned int clk_array[] = {
  526. MT8195_CLK_SCP_ADSP_AUDIODSP, /* bus clock for infra */
  527. MT8195_CLK_TOP_AUDIO_H_SEL, /* clock for ADSP bus */
  528. MT8195_CLK_TOP_AUDIO_LOCAL_BUS_SEL, /* bus clock for DRAM access */
  529. MT8195_CLK_TOP_AUD_INTBUS_SEL, /* bus clock for AFE SRAM access */
  530. MT8195_CLK_INFRA_AO_AUDIO_26M_B, /* audio 26M clock */
  531. MT8195_CLK_AUD_AFE, /* AFE HW master switch */
  532. MT8195_CLK_AUD_A1SYS_HP, /* AFE HW clock*/
  533. MT8195_CLK_AUD_A1SYS, /* AFE HW clock */
  534. };
  535. for (i = 0; i < ARRAY_SIZE(clk_array); i++)
  536. mt8195_afe_enable_clk(afe, afe_priv->clk[clk_array[i]]);
  537. return 0;
  538. }
  539. int mt8195_afe_disable_reg_rw_clk(struct mtk_base_afe *afe)
  540. {
  541. struct mt8195_afe_private *afe_priv = afe->platform_priv;
  542. int i;
  543. static const unsigned int clk_array[] = {
  544. MT8195_CLK_AUD_A1SYS,
  545. MT8195_CLK_AUD_A1SYS_HP,
  546. MT8195_CLK_AUD_AFE,
  547. MT8195_CLK_INFRA_AO_AUDIO_26M_B,
  548. MT8195_CLK_TOP_AUD_INTBUS_SEL,
  549. MT8195_CLK_TOP_AUDIO_LOCAL_BUS_SEL,
  550. MT8195_CLK_TOP_AUDIO_H_SEL,
  551. MT8195_CLK_SCP_ADSP_AUDIODSP,
  552. };
  553. for (i = 0; i < ARRAY_SIZE(clk_array); i++)
  554. mt8195_afe_disable_clk(afe, afe_priv->clk[clk_array[i]]);
  555. return 0;
  556. }
  557. static int mt8195_afe_enable_afe_on(struct mtk_base_afe *afe)
  558. {
  559. regmap_update_bits(afe->regmap, AFE_DAC_CON0, 0x1, 0x1);
  560. return 0;
  561. }
  562. static int mt8195_afe_disable_afe_on(struct mtk_base_afe *afe)
  563. {
  564. regmap_update_bits(afe->regmap, AFE_DAC_CON0, 0x1, 0x0);
  565. return 0;
  566. }
  567. static int mt8195_afe_enable_timing_sys(struct mtk_base_afe *afe)
  568. {
  569. struct mt8195_afe_private *afe_priv = afe->platform_priv;
  570. int i;
  571. static const unsigned int clk_array[] = {
  572. MT8195_CLK_AUD_A1SYS,
  573. MT8195_CLK_AUD_A2SYS,
  574. };
  575. static const unsigned int cg_array[] = {
  576. MT8195_TOP_CG_A1SYS_TIMING,
  577. MT8195_TOP_CG_A2SYS_TIMING,
  578. MT8195_TOP_CG_26M_TIMING,
  579. };
  580. for (i = 0; i < ARRAY_SIZE(clk_array); i++)
  581. mt8195_afe_enable_clk(afe, afe_priv->clk[clk_array[i]]);
  582. for (i = 0; i < ARRAY_SIZE(cg_array); i++)
  583. mt8195_afe_enable_top_cg(afe, cg_array[i]);
  584. return 0;
  585. }
  586. static int mt8195_afe_disable_timing_sys(struct mtk_base_afe *afe)
  587. {
  588. struct mt8195_afe_private *afe_priv = afe->platform_priv;
  589. int i;
  590. static const unsigned int clk_array[] = {
  591. MT8195_CLK_AUD_A2SYS,
  592. MT8195_CLK_AUD_A1SYS,
  593. };
  594. static const unsigned int cg_array[] = {
  595. MT8195_TOP_CG_26M_TIMING,
  596. MT8195_TOP_CG_A2SYS_TIMING,
  597. MT8195_TOP_CG_A1SYS_TIMING,
  598. };
  599. for (i = 0; i < ARRAY_SIZE(cg_array); i++)
  600. mt8195_afe_disable_top_cg(afe, cg_array[i]);
  601. for (i = 0; i < ARRAY_SIZE(clk_array); i++)
  602. mt8195_afe_disable_clk(afe, afe_priv->clk[clk_array[i]]);
  603. return 0;
  604. }
  605. int mt8195_afe_enable_main_clock(struct mtk_base_afe *afe)
  606. {
  607. mt8195_afe_enable_timing_sys(afe);
  608. mt8195_afe_enable_afe_on(afe);
  609. mt8195_afe_enable_apll_tuner(afe, MT8195_AUD_PLL1);
  610. mt8195_afe_enable_apll_tuner(afe, MT8195_AUD_PLL2);
  611. return 0;
  612. }
  613. int mt8195_afe_disable_main_clock(struct mtk_base_afe *afe)
  614. {
  615. mt8195_afe_disable_apll_tuner(afe, MT8195_AUD_PLL2);
  616. mt8195_afe_disable_apll_tuner(afe, MT8195_AUD_PLL1);
  617. mt8195_afe_disable_afe_on(afe);
  618. mt8195_afe_disable_timing_sys(afe);
  619. return 0;
  620. }