mt_afe_clk.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040
  1. /* Copyright (c) 2011-2013, The Linux Foundation. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. */
  12. #include "mt_afe_def.h"
  13. #include "mt_afe_clk.h"
  14. #include "mt_afe_reg.h"
  15. #include <linux/spinlock.h>
  16. #include <linux/delay.h>
  17. #include <sync_write.h>
  18. #ifdef IDLE_TASK_DRIVER_API
  19. #include <mt_idle.h>
  20. #endif
  21. #ifdef COMMON_CLOCK_FRAMEWORK_API
  22. #include <linux/clk.h>
  23. #endif
  24. enum audio_system_clock_type {
  25. CLOCK_INFRA_SYS_AUDIO = 0,
  26. CLOCK_TOP_PDN_AUDIO,
  27. CLOCK_TOP_PDN_AUD_INTBUS,
  28. CLOCK_TOP_AUD_1_SEL,
  29. CLOCK_TOP_AUD_2_SEL,
  30. CLOCK_TOP_APLL1_CK,
  31. CLOCK_TOP_APLL2_CK,
  32. CLOCK_CLK26M,
  33. CLOCK_NUM
  34. };
  35. #ifdef COMMON_CLOCK_FRAMEWORK_API
  36. struct audio_clock_attr {
  37. const char *name;
  38. const bool prepare_once;
  39. bool is_prepared;
  40. struct clk *clock;
  41. };
  42. static struct audio_clock_attr aud_clks[CLOCK_NUM] = {
  43. [CLOCK_INFRA_SYS_AUDIO] = {"infra_sys_audio_clk", true, false, NULL},
  44. [CLOCK_TOP_PDN_AUDIO] = {"top_pdn_audio", true, false, NULL},
  45. [CLOCK_TOP_PDN_AUD_INTBUS] = {"top_pdn_aud_intbus", true, false, NULL},
  46. [CLOCK_TOP_AUD_1_SEL] = {"top_audio_1_sel", false, false, NULL},
  47. [CLOCK_TOP_AUD_2_SEL] = {"top_audio_2_sel", false, false, NULL},
  48. [CLOCK_TOP_APLL1_CK] = {"top_apll1_ck", false, false, NULL},
  49. [CLOCK_TOP_APLL2_CK] = {"top_apll2_ck", false, false, NULL},
  50. [CLOCK_CLK26M] = {"clk26m", false, false, NULL}
  51. };
  52. #endif
  53. /*****************************************************************************
  54. * D A T A T Y P E S
  55. *****************************************************************************/
  56. int aud_afe_clk_cntr;
  57. int aud_dac_clk_cntr;
  58. int aud_adc_clk_cntr;
  59. int aud_i2s_clk_cntr;
  60. int aud_hdmi_clk_cntr;
  61. int aud_spdif_clk_cntr;
  62. int aud_spdif2_clk_cntr;
  63. int aud_apll22m_clk_cntr;
  64. int aud_apll24m_clk_cntr;
  65. int aud_apll1_tuner_cntr;
  66. int aud_apll2_tuner_cntr;
  67. int aud_emi_clk_cntr;
  68. static DEFINE_SPINLOCK(afe_clk_lock);
  69. static DEFINE_MUTEX(afe_clk_mutex);
  70. static DEFINE_MUTEX(emi_clk_mutex);
  71. /*****************************************************************************
  72. * INTERNAL FUNCTION
  73. *****************************************************************************/
  74. void turn_on_afe_clock(void)
  75. {
  76. #if defined(COMMON_CLOCK_FRAMEWORK_API)
  77. int ret = clk_enable(aud_clks[CLOCK_INFRA_SYS_AUDIO].clock);
  78. if (ret)
  79. pr_err("%s clk_enable %s fail %d\n",
  80. __func__, aud_clks[CLOCK_INFRA_SYS_AUDIO].name, ret);
  81. ret = clk_enable(aud_clks[CLOCK_TOP_PDN_AUD_INTBUS].clock);
  82. if (ret)
  83. pr_err("%s clk_enable %s fail %d\n",
  84. __func__, aud_clks[CLOCK_TOP_PDN_AUD_INTBUS].name, ret);
  85. ret = clk_enable(aud_clks[CLOCK_TOP_PDN_AUDIO].clock);
  86. if (ret)
  87. pr_err("%s clk_enable %s fail %d\n",
  88. __func__, aud_clks[CLOCK_TOP_PDN_AUDIO].name, ret);
  89. mt_afe_set_reg(AUDIO_TOP_CON0, 0 << 2, 1 << 2);
  90. #else
  91. /* mt_afe_topck_set_reg(AUDIO_CLK_CFG_4, 0 << 31, 1 << 31); */
  92. /* mt_afe_topck_set_reg(AUDIO_CLK_CFG_4, 0 << 23, 1 << 23); */
  93. mt_afe_set_reg(AUDIO_TOP_CON0, 0 << 2, 1 << 2);
  94. #endif
  95. }
  96. void turn_off_afe_clock(void)
  97. {
  98. #if defined(COMMON_CLOCK_FRAMEWORK_API)
  99. mt_afe_set_reg(AUDIO_TOP_CON0, 1 << 2, 1 << 2);
  100. clk_disable(aud_clks[CLOCK_TOP_PDN_AUDIO].clock);
  101. clk_disable(aud_clks[CLOCK_TOP_PDN_AUD_INTBUS].clock);
  102. clk_disable(aud_clks[CLOCK_INFRA_SYS_AUDIO].clock);
  103. #else
  104. mt_afe_set_reg(AUDIO_TOP_CON0, 1 << 2, 1 << 2);
  105. /* mt_afe_topck_set_reg(AUDIO_CLK_CFG_4, 1 << 23, 1 << 23); */
  106. /* mt_afe_topck_set_reg(AUDIO_CLK_CFG_4, 1 << 31, 1 << 31); */
  107. #endif
  108. }
  109. void turn_on_dac_clock(void)
  110. {
  111. #if defined(COMMON_CLOCK_FRAMEWORK_API)
  112. int ret = clk_enable(aud_clks[CLOCK_INFRA_SYS_AUDIO].clock);
  113. if (ret)
  114. pr_err("%s clk_enable %s fail %d\n",
  115. __func__, aud_clks[CLOCK_INFRA_SYS_AUDIO].name, ret);
  116. mt_afe_set_reg(AUDIO_TOP_CON0, 0 << 25, 1 << 25);
  117. mt_afe_set_reg(AUDIO_TOP_CON0, 0 << 26, 1 << 26);
  118. #else
  119. mt_afe_set_reg(AUDIO_TOP_CON0, 0 << 25, 1 << 25);
  120. mt_afe_set_reg(AUDIO_TOP_CON0, 0 << 26, 1 << 26);
  121. #endif
  122. }
  123. void turn_off_dac_clock(void)
  124. {
  125. #if defined(COMMON_CLOCK_FRAMEWORK_API)
  126. mt_afe_set_reg(AUDIO_TOP_CON0, 1 << 26, 1 << 26);
  127. mt_afe_set_reg(AUDIO_TOP_CON0, 1 << 25, 1 << 25);
  128. clk_disable(aud_clks[CLOCK_INFRA_SYS_AUDIO].clock);
  129. #else
  130. mt_afe_set_reg(AUDIO_TOP_CON0, 1 << 26, 1 << 26);
  131. mt_afe_set_reg(AUDIO_TOP_CON0, 1 << 25, 1 << 25);
  132. #endif
  133. }
  134. void turn_on_adc_clock(void)
  135. {
  136. #if defined(COMMON_CLOCK_FRAMEWORK_API)
  137. int ret = clk_enable(aud_clks[CLOCK_INFRA_SYS_AUDIO].clock);
  138. if (ret)
  139. pr_err("%s clk_enable %s fail %d\n",
  140. __func__, aud_clks[CLOCK_INFRA_SYS_AUDIO].name, ret);
  141. mt_afe_set_reg(AUDIO_TOP_CON0, 0 << 24, 1 << 24);
  142. #else
  143. mt_afe_set_reg(AUDIO_TOP_CON0, 0 << 24, 1 << 24);
  144. #endif
  145. }
  146. void turn_off_adc_clock(void)
  147. {
  148. #if defined(COMMON_CLOCK_FRAMEWORK_API)
  149. mt_afe_set_reg(AUDIO_TOP_CON0, 1 << 24, 1 << 24);
  150. clk_disable(aud_clks[CLOCK_INFRA_SYS_AUDIO].clock);
  151. #else
  152. mt_afe_set_reg(AUDIO_TOP_CON0, 1 << 24, 1 << 24);
  153. #endif
  154. }
  155. void turn_on_i2s_clock(void)
  156. {
  157. #if defined(COMMON_CLOCK_FRAMEWORK_API)
  158. int ret = clk_enable(aud_clks[CLOCK_INFRA_SYS_AUDIO].clock);
  159. if (ret)
  160. pr_err("%s clk_enable %s fail %d\n",
  161. __func__, aud_clks[CLOCK_INFRA_SYS_AUDIO].name, ret);
  162. mt_afe_set_reg(AUDIO_TOP_CON0, 0 << 6, 1 << 6);
  163. #else
  164. mt_afe_set_reg(AUDIO_TOP_CON0, 0 << 6, 1 << 6);
  165. #endif
  166. }
  167. void turn_off_i2s_clock(void)
  168. {
  169. #if defined(COMMON_CLOCK_FRAMEWORK_API)
  170. mt_afe_set_reg(AUDIO_TOP_CON0, 1 << 6, 1 << 6);
  171. clk_disable(aud_clks[CLOCK_INFRA_SYS_AUDIO].clock);
  172. #else
  173. mt_afe_set_reg(AUDIO_TOP_CON0, 1 << 6, 1 << 6);
  174. #endif
  175. }
  176. void turn_on_hdmi_clock(void)
  177. {
  178. #if defined(COMMON_CLOCK_FRAMEWORK_API)
  179. int ret = clk_enable(aud_clks[CLOCK_INFRA_SYS_AUDIO].clock);
  180. if (ret)
  181. pr_err("%s clk_enable %s fail %d\n",
  182. __func__, aud_clks[CLOCK_INFRA_SYS_AUDIO].name, ret);
  183. mt_afe_set_reg(AUDIO_TOP_CON0, 0 << 20, 1 << 20);
  184. #else
  185. mt_afe_set_reg(AUDIO_TOP_CON0, 0 << 20, 1 << 20);
  186. #endif
  187. }
  188. void turn_off_hdmi_clock(void)
  189. {
  190. #if defined(COMMON_CLOCK_FRAMEWORK_API)
  191. mt_afe_set_reg(AUDIO_TOP_CON0, 1 << 20, 1 << 20);
  192. clk_disable(aud_clks[CLOCK_INFRA_SYS_AUDIO].clock);
  193. #else
  194. mt_afe_set_reg(AUDIO_TOP_CON0, 1 << 20, 1 << 20);
  195. #endif
  196. }
  197. void turn_on_spdif_clock(void)
  198. {
  199. #if defined(COMMON_CLOCK_FRAMEWORK_API)
  200. int ret = clk_enable(aud_clks[CLOCK_INFRA_SYS_AUDIO].clock);
  201. if (ret)
  202. pr_err("%s clk_enable %s fail %d\n",
  203. __func__, aud_clks[CLOCK_INFRA_SYS_AUDIO].name, ret);
  204. mt_afe_set_reg(AUDIO_TOP_CON0, 0 << 21, 1 << 21);
  205. #else
  206. mt_afe_set_reg(AUDIO_TOP_CON0, 0 << 21, 1 << 21);
  207. #endif
  208. }
  209. void turn_off_spdif_clock(void)
  210. {
  211. #if defined(COMMON_CLOCK_FRAMEWORK_API)
  212. mt_afe_set_reg(AUDIO_TOP_CON0, 1 << 21, 1 << 21);
  213. clk_disable(aud_clks[CLOCK_INFRA_SYS_AUDIO].clock);
  214. #else
  215. mt_afe_set_reg(AUDIO_TOP_CON0, 1 << 21, 1 << 21);
  216. #endif
  217. }
  218. void turn_on_spdif2_clock(void)
  219. {
  220. #if defined(COMMON_CLOCK_FRAMEWORK_API)
  221. int ret = clk_enable(aud_clks[CLOCK_INFRA_SYS_AUDIO].clock);
  222. if (ret)
  223. pr_err("%s clk_enable %s fail %d\n",
  224. __func__, aud_clks[CLOCK_INFRA_SYS_AUDIO].name, ret);
  225. mt_afe_set_reg(AUDIO_TOP_CON0, 0 << 11, 1 << 11);
  226. #else
  227. mt_afe_set_reg(AUDIO_TOP_CON0, 0 << 11, 1 << 11);
  228. #endif
  229. }
  230. void turn_off_spdif2_clock(void)
  231. {
  232. #if defined(COMMON_CLOCK_FRAMEWORK_API)
  233. mt_afe_set_reg(AUDIO_TOP_CON0, 1 << 11, 1 << 11);
  234. #else
  235. mt_afe_set_reg(AUDIO_TOP_CON0, 1 << 11, 1 << 11);
  236. #endif
  237. }
  238. void turn_on_apll22m_clock(void)
  239. {
  240. #if defined(COMMON_CLOCK_FRAMEWORK_API)
  241. int ret = clk_prepare_enable(aud_clks[CLOCK_TOP_AUD_1_SEL].clock);
  242. if (ret)
  243. pr_err("%s clk_prepare_enable %s fail %d\n",
  244. __func__, aud_clks[CLOCK_TOP_AUD_1_SEL].name, ret);
  245. ret = clk_set_parent(aud_clks[CLOCK_TOP_AUD_1_SEL].clock,
  246. aud_clks[CLOCK_TOP_APLL1_CK].clock);
  247. if (ret)
  248. pr_err("%s clk_set_parent %s-%s fail %d\n",
  249. __func__, aud_clks[CLOCK_TOP_AUD_1_SEL].name,
  250. aud_clks[CLOCK_TOP_APLL1_CK].name, ret);
  251. ret = clk_enable(aud_clks[CLOCK_INFRA_SYS_AUDIO].clock);
  252. if (ret)
  253. pr_err("%s clk_enable %s fail %d\n",
  254. __func__, aud_clks[CLOCK_INFRA_SYS_AUDIO].name, ret);
  255. mt_afe_set_reg(AUDIO_TOP_CON0, 0 << 8, 1 << 8);
  256. #else
  257. mt_afe_pll_set_reg(AUDIO_APLL1_PWR_CON0, 1 << 0, 1 << 0);
  258. mt_afe_pll_set_reg(AUDIO_APLL1_PWR_CON0, 0 << 1, 1 << 1);
  259. mt_afe_pll_set_reg(AUDIO_APLL1_CON0, 1 << 0, 1 << 0);
  260. mt_afe_topck_set_reg(AUDIO_CLK_CFG_6, 0 << 31, 1 << 31); /* enable hf_haud_1_ck */
  261. mt_afe_topck_set_reg(AUDIO_CLK_CFG_6, 0x01000000, 0x03000000); /* select apll1_ck */
  262. mt_afe_set_reg(AUDIO_TOP_CON0, 0 << 8, 1 << 8);
  263. #endif
  264. }
  265. void turn_off_apll22m_clock(void)
  266. {
  267. #if defined(COMMON_CLOCK_FRAMEWORK_API)
  268. int ret;
  269. mt_afe_set_reg(AUDIO_TOP_CON0, 1 << 8, 1 << 8);
  270. clk_disable(aud_clks[CLOCK_INFRA_SYS_AUDIO].clock);
  271. ret = clk_set_parent(aud_clks[CLOCK_TOP_AUD_1_SEL].clock, aud_clks[CLOCK_CLK26M].clock);
  272. if (ret)
  273. pr_err("%s clk_set_parent %s-%s fail %d\n",
  274. __func__, aud_clks[CLOCK_TOP_AUD_1_SEL].name,
  275. aud_clks[CLOCK_CLK26M].name, ret);
  276. clk_disable_unprepare(aud_clks[CLOCK_TOP_AUD_1_SEL].clock);
  277. #else
  278. mt_afe_set_reg(AUDIO_TOP_CON0, 1 << 8, 1 << 8);
  279. mt_afe_topck_set_reg(AUDIO_CLK_CFG_6, 0x0, 0x03000000); /* select clk26m */
  280. mt_afe_topck_set_reg(AUDIO_CLK_CFG_6, 1 << 31, 1 << 31); /* disable hf_haud_1_ck */
  281. mt_afe_pll_set_reg(AUDIO_APLL1_CON0, 0 << 0, 1 << 0);
  282. mt_afe_pll_set_reg(AUDIO_APLL1_PWR_CON0, 1 << 1, 1 << 1);
  283. mt_afe_pll_set_reg(AUDIO_APLL1_PWR_CON0, 0 << 0, 1 << 0);
  284. #endif
  285. }
  286. void turn_on_apll24m_clock(void)
  287. {
  288. #if defined(COMMON_CLOCK_FRAMEWORK_API)
  289. int ret = clk_prepare_enable(aud_clks[CLOCK_TOP_AUD_2_SEL].clock);
  290. if (ret)
  291. pr_err("%s clk_prepare_enable %s fail %d\n",
  292. __func__, aud_clks[CLOCK_TOP_AUD_2_SEL].name, ret);
  293. ret = clk_set_parent(aud_clks[CLOCK_TOP_AUD_2_SEL].clock,
  294. aud_clks[CLOCK_TOP_APLL2_CK].clock);
  295. if (ret)
  296. pr_err("%s clk_set_parent %s-%s fail %d\n",
  297. __func__, aud_clks[CLOCK_TOP_AUD_2_SEL].name,
  298. aud_clks[CLOCK_TOP_APLL2_CK].name, ret);
  299. ret = clk_enable(aud_clks[CLOCK_INFRA_SYS_AUDIO].clock);
  300. if (ret)
  301. pr_err("%s clk_enable %s fail %d\n",
  302. __func__, aud_clks[CLOCK_INFRA_SYS_AUDIO].name, ret);
  303. mt_afe_set_reg(AUDIO_TOP_CON0, 0 << 9, 1 << 9);
  304. #else
  305. mt_afe_pll_set_reg(AUDIO_APLL2_PWR_CON0, 1 << 0, 1 << 0);
  306. mt_afe_pll_set_reg(AUDIO_APLL2_PWR_CON0, 0 << 1, 1 << 1);
  307. mt_afe_pll_set_reg(AUDIO_APLL2_CON0, 1 << 0, 1 << 0);
  308. mt_afe_topck_set_reg(AUDIO_CLK_CFG_7, 0 << 7, 1 << 7); /* enable hf_haud_2_ck */
  309. mt_afe_topck_set_reg(AUDIO_CLK_CFG_7, 0x1, 0x3); /* select apll2_ck */
  310. mt_afe_set_reg(AUDIO_TOP_CON0, 0 << 9, 1 << 9);
  311. #endif
  312. }
  313. void turn_off_apll24m_clock(void)
  314. {
  315. #if defined(COMMON_CLOCK_FRAMEWORK_API)
  316. int ret;
  317. mt_afe_set_reg(AUDIO_TOP_CON0, 1 << 9, 1 << 9);
  318. clk_disable(aud_clks[CLOCK_INFRA_SYS_AUDIO].clock);
  319. ret = clk_set_parent(aud_clks[CLOCK_TOP_AUD_2_SEL].clock, aud_clks[CLOCK_CLK26M].clock);
  320. if (ret)
  321. pr_err("%s clk_set_parent %s-%s fail %d\n",
  322. __func__, aud_clks[CLOCK_TOP_AUD_2_SEL].name,
  323. aud_clks[CLOCK_CLK26M].name, ret);
  324. clk_disable_unprepare(aud_clks[CLOCK_TOP_AUD_2_SEL].clock);
  325. #else
  326. mt_afe_set_reg(AUDIO_TOP_CON0, 1 << 9, 1 << 9);
  327. mt_afe_pll_set_reg(AUDIO_APLL2_CON0, 0 << 0, 1 << 0);
  328. mt_afe_topck_set_reg(AUDIO_CLK_CFG_7, 0x0, 0x3); /* select clk26m */
  329. mt_afe_topck_set_reg(AUDIO_CLK_CFG_7, 1 << 7, 1 << 7); /* disable hf_haud_2_ck */
  330. mt_afe_pll_set_reg(AUDIO_APLL2_PWR_CON0, 1 << 1, 1 << 1);
  331. mt_afe_pll_set_reg(AUDIO_APLL2_PWR_CON0, 0 << 0, 1 << 0);
  332. #endif
  333. }
  334. void turn_on_apll1_tuner_clock(void)
  335. {
  336. #if defined(COMMON_CLOCK_FRAMEWORK_API)
  337. int ret = clk_enable(aud_clks[CLOCK_INFRA_SYS_AUDIO].clock);
  338. if (ret)
  339. pr_err("%s clk_enable %s fail %d\n",
  340. __func__, aud_clks[CLOCK_INFRA_SYS_AUDIO].name, ret);
  341. mt_afe_set_reg(AUDIO_TOP_CON0, 0 << 19, 1 << 19);
  342. mt_afe_set_reg(AFE_APLL1_TUNER_CFG, 0x00008033, 0x0000FFF7);
  343. mt_afe_pll_set_reg(AP_PLL_CON5, 1 << 0, 1 << 0);
  344. #else
  345. mt_afe_set_reg(AUDIO_TOP_CON0, 0 << 19, 1 << 19);
  346. mt_afe_set_reg(AFE_APLL1_TUNER_CFG, 0x00008033, 0x0000FFF7);
  347. mt_afe_pll_set_reg(AP_PLL_CON5, 1 << 0, 1 << 0);
  348. #endif
  349. }
  350. void turn_off_apll1_tuner_clock(void)
  351. {
  352. #if defined(COMMON_CLOCK_FRAMEWORK_API)
  353. mt_afe_set_reg(AUDIO_TOP_CON0, 1 << 19, 1 << 19);
  354. clk_disable(aud_clks[CLOCK_INFRA_SYS_AUDIO].clock);
  355. mt_afe_pll_set_reg(AP_PLL_CON5, 0 << 0, 1 << 0);
  356. #else
  357. mt_afe_set_reg(AUDIO_TOP_CON0, 1 << 19, 1 << 19);
  358. mt_afe_pll_set_reg(AP_PLL_CON5, 0 << 0, 1 << 0);
  359. #endif
  360. }
  361. void turn_on_apll2_tuner_clock(void)
  362. {
  363. #if defined(COMMON_CLOCK_FRAMEWORK_API)
  364. int ret = clk_enable(aud_clks[CLOCK_INFRA_SYS_AUDIO].clock);
  365. if (ret)
  366. pr_err("%s clk_enable %s fail %d\n",
  367. __func__, aud_clks[CLOCK_INFRA_SYS_AUDIO].name, ret);
  368. mt_afe_set_reg(AUDIO_TOP_CON0, 0 << 18, 1 << 18);
  369. mt_afe_set_reg(AFE_APLL2_TUNER_CFG, 0x00008035, 0x0000FFF7);
  370. mt_afe_pll_set_reg(AP_PLL_CON5, 1 << 1, 1 << 1);
  371. #else
  372. mt_afe_set_reg(AUDIO_TOP_CON0, 0 << 18, 1 << 18);
  373. mt_afe_set_reg(AFE_APLL2_TUNER_CFG, 0x00008035, 0x0000FFF7);
  374. mt_afe_pll_set_reg(AP_PLL_CON5, 1 << 1, 1 << 1);
  375. #endif
  376. }
  377. void turn_off_apll2_tuner_clock(void)
  378. {
  379. #if defined(COMMON_CLOCK_FRAMEWORK_API)
  380. mt_afe_set_reg(AUDIO_TOP_CON0, 1 << 18, 1 << 18);
  381. clk_disable(aud_clks[CLOCK_INFRA_SYS_AUDIO].clock);
  382. mt_afe_pll_set_reg(AP_PLL_CON5, 0 << 1, 1 << 1);
  383. #else
  384. mt_afe_set_reg(AUDIO_TOP_CON0, 1 << 18, 1 << 18);
  385. mt_afe_pll_set_reg(AP_PLL_CON5, 0 << 1, 1 << 1);
  386. #endif
  387. }
  388. void mt_afe_apb_bus_init(void)
  389. {
  390. unsigned long flags;
  391. spin_lock_irqsave(&afe_clk_lock, flags);
  392. mt_afe_set_reg(AUDIO_TOP_CON0, 0x00004000, 0x00004000);
  393. spin_unlock_irqrestore(&afe_clk_lock, flags);
  394. }
  395. int mt_afe_init_clock(void *dev)
  396. {
  397. int ret = 0;
  398. #ifdef COMMON_CLOCK_FRAMEWORK_API
  399. size_t i;
  400. for (i = 0; i < ARRAY_SIZE(aud_clks); i++) {
  401. aud_clks[i].clock = devm_clk_get(dev, aud_clks[i].name);
  402. if (IS_ERR(aud_clks[i].clock)) {
  403. ret = PTR_ERR(aud_clks[i].clock);
  404. pr_err("%s devm_clk_get %s fail %d\n", __func__, aud_clks[i].name, ret);
  405. break;
  406. }
  407. }
  408. if (ret)
  409. return ret;
  410. for (i = 0; i < ARRAY_SIZE(aud_clks); i++) {
  411. if (aud_clks[i].prepare_once) {
  412. ret = clk_prepare(aud_clks[i].clock);
  413. if (ret) {
  414. pr_err("%s clk_prepare %s fail %d\n",
  415. __func__, aud_clks[i].name, ret);
  416. break;
  417. }
  418. aud_clks[i].is_prepared = true;
  419. }
  420. }
  421. #endif
  422. return ret;
  423. }
  424. void mt_afe_deinit_clock(void *dev)
  425. {
  426. #ifdef COMMON_CLOCK_FRAMEWORK_API
  427. size_t i;
  428. pr_debug("%s\n", __func__);
  429. for (i = 0; i < ARRAY_SIZE(aud_clks); i++) {
  430. if (aud_clks[i].clock && !IS_ERR(aud_clks[i].clock) && aud_clks[i].is_prepared) {
  431. clk_unprepare(aud_clks[i].clock);
  432. aud_clks[i].is_prepared = false;
  433. }
  434. }
  435. #endif
  436. }
  437. void mt_afe_power_off_default_clock(void)
  438. {
  439. #ifdef COMMON_CLOCK_FRAMEWORK_API
  440. clk_enable(aud_clks[CLOCK_INFRA_SYS_AUDIO].clock);
  441. mt_afe_set_reg(AUDIO_TOP_CON0, 0 << 19, 0xf000b42);
  442. clk_disable(aud_clks[CLOCK_INFRA_SYS_AUDIO].clock);
  443. #endif
  444. }
  445. void mt_afe_main_clk_on(void)
  446. {
  447. unsigned long flags;
  448. spin_lock_irqsave(&afe_clk_lock, flags);
  449. pr_debug("%s aud_afe_clk_cntr:%d\n", __func__, aud_afe_clk_cntr);
  450. if (aud_afe_clk_cntr == 0) {
  451. #ifdef PM_MANAGER_API
  452. turn_on_afe_clock();
  453. #endif
  454. }
  455. aud_afe_clk_cntr++;
  456. spin_unlock_irqrestore(&afe_clk_lock, flags);
  457. }
  458. void mt_afe_main_clk_off(void)
  459. {
  460. unsigned long flags;
  461. spin_lock_irqsave(&afe_clk_lock, flags);
  462. pr_debug("%s aud_afe_clk_cntr:%d\n", __func__, aud_afe_clk_cntr);
  463. aud_afe_clk_cntr--;
  464. if (aud_afe_clk_cntr == 0) {
  465. #ifdef PM_MANAGER_API
  466. turn_off_afe_clock();
  467. #endif
  468. } else if (aud_afe_clk_cntr < 0) {
  469. pr_err("%s aud_afe_clk_cntr:%d<0\n", __func__, aud_afe_clk_cntr);
  470. AUDIO_ASSERT(true);
  471. aud_afe_clk_cntr = 0;
  472. }
  473. spin_unlock_irqrestore(&afe_clk_lock, flags);
  474. }
  475. void mt_afe_dac_clk_on(void)
  476. {
  477. unsigned long flags;
  478. spin_lock_irqsave(&afe_clk_lock, flags);
  479. pr_debug("%s aud_dac_clk_cntr:%d\n", __func__, aud_dac_clk_cntr);
  480. if (aud_dac_clk_cntr == 0) {
  481. #ifdef PM_MANAGER_API
  482. turn_on_dac_clock();
  483. #endif
  484. }
  485. aud_dac_clk_cntr++;
  486. spin_unlock_irqrestore(&afe_clk_lock, flags);
  487. }
  488. void mt_afe_dac_clk_off(void)
  489. {
  490. unsigned long flags;
  491. spin_lock_irqsave(&afe_clk_lock, flags);
  492. pr_debug("%s aud_dac_clk_cntr:%d\n", __func__, aud_dac_clk_cntr);
  493. aud_dac_clk_cntr--;
  494. if (aud_dac_clk_cntr == 0) {
  495. #ifdef PM_MANAGER_API
  496. turn_off_dac_clock();
  497. #endif
  498. } else if (aud_dac_clk_cntr < 0) {
  499. pr_err("%s aud_dac_clk_cntr(%d)<0\n", __func__, aud_dac_clk_cntr);
  500. aud_dac_clk_cntr = 0;
  501. }
  502. spin_unlock_irqrestore(&afe_clk_lock, flags);
  503. }
  504. void mt_afe_adc_clk_on(void)
  505. {
  506. unsigned long flags;
  507. spin_lock_irqsave(&afe_clk_lock, flags);
  508. pr_debug("%s aud_adc_clk_cntr:%d\n", __func__, aud_adc_clk_cntr);
  509. if (aud_adc_clk_cntr == 0) {
  510. #ifdef PM_MANAGER_API
  511. turn_on_adc_clock();
  512. #endif
  513. }
  514. aud_adc_clk_cntr++;
  515. spin_unlock_irqrestore(&afe_clk_lock, flags);
  516. }
  517. void mt_afe_adc_clk_off(void)
  518. {
  519. unsigned long flags;
  520. spin_lock_irqsave(&afe_clk_lock, flags);
  521. pr_debug("%s aud_adc_clk_cntr:%d\n", __func__, aud_adc_clk_cntr);
  522. aud_adc_clk_cntr--;
  523. if (aud_adc_clk_cntr == 0) {
  524. #ifdef PM_MANAGER_API
  525. turn_off_adc_clock();
  526. #endif
  527. } else if (aud_adc_clk_cntr < 0) {
  528. pr_err("%s aud_adc_clk_cntr(%d)<0\n", __func__, aud_adc_clk_cntr);
  529. aud_adc_clk_cntr = 0;
  530. }
  531. spin_unlock_irqrestore(&afe_clk_lock, flags);
  532. }
  533. void mt_afe_i2s_clk_on(void)
  534. {
  535. unsigned long flags;
  536. spin_lock_irqsave(&afe_clk_lock, flags);
  537. pr_debug("%s aud_i2s_clk_cntr:%d\n", __func__, aud_i2s_clk_cntr);
  538. if (aud_i2s_clk_cntr == 0) {
  539. #ifdef PM_MANAGER_API
  540. turn_on_i2s_clock();
  541. #endif
  542. }
  543. aud_i2s_clk_cntr++;
  544. spin_unlock_irqrestore(&afe_clk_lock, flags);
  545. }
  546. void mt_afe_i2s_clk_off(void)
  547. {
  548. unsigned long flags;
  549. spin_lock_irqsave(&afe_clk_lock, flags);
  550. pr_debug("%s aud_i2s_clk_cntr:%d\n", __func__, aud_i2s_clk_cntr);
  551. aud_i2s_clk_cntr--;
  552. if (aud_i2s_clk_cntr == 0) {
  553. #ifdef PM_MANAGER_API
  554. turn_off_i2s_clock();
  555. #endif
  556. } else if (aud_i2s_clk_cntr < 0) {
  557. pr_err("%s aud_i2s_clk_cntr:%d<0\n", __func__, aud_i2s_clk_cntr);
  558. AUDIO_ASSERT(true);
  559. aud_i2s_clk_cntr = 0;
  560. }
  561. spin_unlock_irqrestore(&afe_clk_lock, flags);
  562. }
  563. void mt_afe_hdmi_clk_on(void)
  564. {
  565. unsigned long flags;
  566. spin_lock_irqsave(&afe_clk_lock, flags);
  567. pr_debug("%s aud_hdmi_clk_cntr:%d\n", __func__, aud_hdmi_clk_cntr);
  568. if (aud_hdmi_clk_cntr == 0) {
  569. #ifdef PM_MANAGER_API
  570. turn_on_hdmi_clock();
  571. #endif
  572. }
  573. aud_hdmi_clk_cntr++;
  574. spin_unlock_irqrestore(&afe_clk_lock, flags);
  575. }
  576. void mt_afe_hdmi_clk_off(void)
  577. {
  578. unsigned long flags;
  579. spin_lock_irqsave(&afe_clk_lock, flags);
  580. pr_debug("%s aud_hdmi_clk_cntr:%d\n", __func__, aud_hdmi_clk_cntr);
  581. aud_hdmi_clk_cntr--;
  582. if (aud_hdmi_clk_cntr == 0) {
  583. #ifdef PM_MANAGER_API
  584. turn_off_hdmi_clock();
  585. #endif
  586. } else if (aud_hdmi_clk_cntr < 0) {
  587. pr_err("%s aud_hdmi_clk_cntr:%d<0\n", __func__, aud_hdmi_clk_cntr);
  588. AUDIO_ASSERT(true);
  589. aud_hdmi_clk_cntr = 0;
  590. }
  591. spin_unlock_irqrestore(&afe_clk_lock, flags);
  592. }
  593. /*****************************************************************************
  594. * FUNCTION
  595. * mt_afe_spdif_clk_on / mt_afe_spdif_clk_off
  596. *
  597. * DESCRIPTION
  598. * Enable/Disable SPDIF clock
  599. *
  600. *****************************************************************************/
  601. void mt_afe_spdif_clk_on(void)
  602. {
  603. unsigned long flags;
  604. spin_lock_irqsave(&afe_clk_lock, flags);
  605. pr_debug("%s aud_spdif_clk_cntr:%d\n", __func__, aud_spdif_clk_cntr);
  606. if (aud_spdif_clk_cntr == 0) {
  607. #ifdef PM_MANAGER_API
  608. turn_on_spdif_clock();
  609. #endif
  610. }
  611. aud_spdif_clk_cntr++;
  612. spin_unlock_irqrestore(&afe_clk_lock, flags);
  613. }
  614. void mt_afe_spdif_clk_off(void)
  615. {
  616. unsigned long flags;
  617. spin_lock_irqsave(&afe_clk_lock, flags);
  618. pr_debug("%s aud_spdif_clk_cntr:%d\n", __func__, aud_spdif_clk_cntr);
  619. aud_spdif_clk_cntr--;
  620. if (aud_spdif_clk_cntr == 0) {
  621. #ifdef PM_MANAGER_API
  622. turn_off_spdif_clock();
  623. #endif
  624. } else if (aud_spdif_clk_cntr < 0) {
  625. pr_err("%s aud_spdif_clk_cntr:%d<0\n", __func__, aud_spdif_clk_cntr);
  626. AUDIO_ASSERT(true);
  627. aud_spdif_clk_cntr = 0;
  628. }
  629. spin_unlock_irqrestore(&afe_clk_lock, flags);
  630. }
  631. void mt_afe_spdif2_clk_on(void)
  632. {
  633. unsigned long flags;
  634. spin_lock_irqsave(&afe_clk_lock, flags);
  635. pr_debug("%s aud_spdif2_clk_cntr:%d\n", __func__, aud_spdif2_clk_cntr);
  636. if (aud_spdif2_clk_cntr == 0) {
  637. #ifdef PM_MANAGER_API
  638. turn_on_spdif2_clock();
  639. #endif
  640. }
  641. aud_spdif2_clk_cntr++;
  642. spin_unlock_irqrestore(&afe_clk_lock, flags);
  643. }
  644. void mt_afe_spdif2_clk_off(void)
  645. {
  646. unsigned long flags;
  647. spin_lock_irqsave(&afe_clk_lock, flags);
  648. pr_debug("%s aud_spdif2_clk_cntr:%d\n", __func__, aud_spdif2_clk_cntr);
  649. aud_spdif2_clk_cntr--;
  650. if (aud_spdif2_clk_cntr == 0) {
  651. #ifdef PM_MANAGER_API
  652. turn_off_spdif2_clock();
  653. #endif
  654. } else if (aud_spdif2_clk_cntr < 0) {
  655. pr_err("%s aud_spdif2_clk_cntr:%d<0\n", __func__, aud_spdif2_clk_cntr);
  656. AUDIO_ASSERT(true);
  657. aud_spdif2_clk_cntr = 0;
  658. }
  659. spin_unlock_irqrestore(&afe_clk_lock, flags);
  660. }
  661. void mt_afe_suspend_clk_on(void)
  662. {
  663. #ifdef PM_MANAGER_API
  664. unsigned long flags;
  665. pr_debug("%s aud_afe_clk_cntr:%d\n", __func__, aud_afe_clk_cntr);
  666. spin_lock_irqsave(&afe_clk_lock, flags);
  667. if (aud_afe_clk_cntr > 0)
  668. turn_on_afe_clock();
  669. if (aud_i2s_clk_cntr > 0)
  670. turn_on_i2s_clock();
  671. if (aud_dac_clk_cntr > 0)
  672. turn_on_dac_clock();
  673. if (aud_adc_clk_cntr > 0)
  674. turn_on_adc_clock();
  675. if (aud_hdmi_clk_cntr > 0)
  676. turn_on_hdmi_clock();
  677. if (aud_spdif_clk_cntr > 0)
  678. turn_on_spdif_clock();
  679. if (aud_spdif2_clk_cntr > 0)
  680. turn_on_spdif2_clock();
  681. spin_unlock_irqrestore(&afe_clk_lock, flags);
  682. mutex_lock(&afe_clk_mutex);
  683. if (aud_apll22m_clk_cntr > 0)
  684. turn_on_apll22m_clock();
  685. if (aud_apll24m_clk_cntr > 0)
  686. turn_on_apll24m_clock();
  687. mutex_unlock(&afe_clk_mutex);
  688. #endif
  689. }
  690. void mt_afe_suspend_clk_off(void)
  691. {
  692. #ifdef PM_MANAGER_API
  693. unsigned long flags;
  694. pr_debug("%s aud_afe_clk_cntr:%d\n", __func__, aud_afe_clk_cntr);
  695. mutex_lock(&afe_clk_mutex);
  696. if (aud_apll22m_clk_cntr > 0)
  697. turn_off_apll22m_clock();
  698. if (aud_apll24m_clk_cntr > 0)
  699. turn_off_apll24m_clock();
  700. mutex_unlock(&afe_clk_mutex);
  701. spin_lock_irqsave(&afe_clk_lock, flags);
  702. if (aud_i2s_clk_cntr > 0)
  703. turn_off_i2s_clock();
  704. if (aud_dac_clk_cntr > 0)
  705. turn_off_dac_clock();
  706. if (aud_adc_clk_cntr > 0)
  707. turn_off_adc_clock();
  708. if (aud_hdmi_clk_cntr > 0)
  709. turn_off_hdmi_clock();
  710. if (aud_spdif_clk_cntr > 0)
  711. turn_off_spdif_clock();
  712. if (aud_spdif2_clk_cntr > 0)
  713. turn_off_spdif2_clock();
  714. if (aud_afe_clk_cntr > 0)
  715. turn_off_afe_clock();
  716. spin_unlock_irqrestore(&afe_clk_lock, flags);
  717. #endif
  718. }
  719. void mt_afe_apll24m_clk_on(void)
  720. {
  721. mutex_lock(&afe_clk_mutex);
  722. pr_debug("%s aud_apll24m_clk_cntr:%d\n", __func__, aud_apll24m_clk_cntr);
  723. if (aud_apll24m_clk_cntr == 0) {
  724. #ifdef PM_MANAGER_API
  725. turn_on_apll24m_clock();
  726. #endif
  727. }
  728. aud_apll24m_clk_cntr++;
  729. mutex_unlock(&afe_clk_mutex);
  730. }
  731. void mt_afe_apll24m_clk_off(void)
  732. {
  733. mutex_lock(&afe_clk_mutex);
  734. pr_debug("%s aud_apll24m_clk_cntr(%d)\n", __func__, aud_apll24m_clk_cntr);
  735. aud_apll24m_clk_cntr--;
  736. if (aud_apll24m_clk_cntr == 0) {
  737. #ifdef PM_MANAGER_API
  738. turn_off_apll24m_clock();
  739. #endif
  740. } else if (aud_apll24m_clk_cntr < 0) {
  741. pr_err("%s aud_apll24m_clk_cntr:%d<0\n", __func__, aud_apll24m_clk_cntr);
  742. aud_apll24m_clk_cntr = 0;
  743. }
  744. mutex_unlock(&afe_clk_mutex);
  745. }
  746. void mt_afe_apll22m_clk_on(void)
  747. {
  748. mutex_lock(&afe_clk_mutex);
  749. pr_debug("%s aud_apll22m_clk_cntr:%d\n", __func__, aud_apll22m_clk_cntr);
  750. if (aud_apll22m_clk_cntr == 0) {
  751. #ifdef PM_MANAGER_API
  752. turn_on_apll22m_clock();
  753. #endif
  754. }
  755. aud_apll22m_clk_cntr++;
  756. mutex_unlock(&afe_clk_mutex);
  757. }
  758. void mt_afe_apll22m_clk_off(void)
  759. {
  760. mutex_lock(&afe_clk_mutex);
  761. pr_debug("%s aud_apll22m_clk_cntr(%d)\n", __func__, aud_apll22m_clk_cntr);
  762. aud_apll22m_clk_cntr--;
  763. if (aud_apll22m_clk_cntr == 0) {
  764. #ifdef PM_MANAGER_API
  765. turn_off_apll22m_clock();
  766. #endif
  767. }
  768. if (aud_apll22m_clk_cntr < 0) {
  769. pr_err("%s aud_apll22m_clk_cntr:%d<0\n", __func__, aud_apll22m_clk_cntr);
  770. aud_apll22m_clk_cntr = 0;
  771. }
  772. mutex_unlock(&afe_clk_mutex);
  773. }
  774. void mt_afe_apll1tuner_clk_on(void)
  775. {
  776. mutex_lock(&afe_clk_mutex);
  777. pr_debug("%s aud_apll1_tuner_cntr:%d\n", __func__, aud_apll1_tuner_cntr);
  778. if (aud_apll1_tuner_cntr == 0) {
  779. #ifdef PM_MANAGER_API
  780. turn_on_apll1_tuner_clock();
  781. #endif
  782. }
  783. aud_apll1_tuner_cntr++;
  784. mutex_unlock(&afe_clk_mutex);
  785. }
  786. void mt_afe_apll1tuner_clk_off(void)
  787. {
  788. mutex_lock(&afe_clk_mutex);
  789. pr_debug("%s aud_apll1_tuner_cntr:%d\n", __func__, aud_apll1_tuner_cntr);
  790. aud_apll1_tuner_cntr--;
  791. if (aud_apll1_tuner_cntr == 0) {
  792. #ifdef PM_MANAGER_API
  793. turn_off_apll1_tuner_clock();
  794. #endif
  795. } else if (aud_apll1_tuner_cntr < 0) {
  796. pr_err("%s aud_apll1_tuner_cntr:%d<0\n", __func__, aud_apll1_tuner_cntr);
  797. aud_apll1_tuner_cntr = 0;
  798. }
  799. mutex_unlock(&afe_clk_mutex);
  800. }
  801. void mt_afe_apll2tuner_clk_on(void)
  802. {
  803. mutex_lock(&afe_clk_mutex);
  804. pr_debug("%s aud_apll2_tuner_cntr:%d\n", __func__, aud_apll2_tuner_cntr);
  805. if (aud_apll2_tuner_cntr == 0) {
  806. #ifdef PM_MANAGER_API
  807. turn_on_apll2_tuner_clock();
  808. #endif
  809. }
  810. aud_apll2_tuner_cntr++;
  811. mutex_unlock(&afe_clk_mutex);
  812. }
  813. void mt_afe_apll2tuner_clk_off(void)
  814. {
  815. mutex_lock(&afe_clk_mutex);
  816. pr_debug("%s aud_apll2_tuner_cntr:%d\n", __func__, aud_apll2_tuner_cntr);
  817. aud_apll2_tuner_cntr--;
  818. if (aud_apll2_tuner_cntr == 0) {
  819. #ifdef PM_MANAGER_API
  820. turn_off_apll2_tuner_clock();
  821. #endif
  822. } else if (aud_apll2_tuner_cntr < 0) {
  823. pr_err("%s aud_apll1_tuner_cntr:%d<0\n", __func__, aud_apll2_tuner_cntr);
  824. aud_apll2_tuner_cntr = 0;
  825. }
  826. mutex_unlock(&afe_clk_mutex);
  827. }
  828. void mt_afe_emi_clk_on(void)
  829. {
  830. #ifdef IDLE_TASK_DRIVER_API
  831. mutex_lock(&emi_clk_mutex);
  832. if (aud_emi_clk_cntr == 0) {
  833. disable_dpidle_by_bit(MT_CG_AUDIO_AFE);
  834. disable_soidle_by_bit(MT_CG_AUDIO_AFE);
  835. }
  836. aud_emi_clk_cntr++;
  837. mutex_unlock(&emi_clk_mutex);
  838. #endif
  839. }
  840. void mt_afe_emi_clk_off(void)
  841. {
  842. #ifdef IDLE_TASK_DRIVER_API
  843. mutex_lock(&emi_clk_mutex);
  844. aud_emi_clk_cntr--;
  845. if (aud_emi_clk_cntr == 0) {
  846. enable_dpidle_by_bit(MT_CG_AUDIO_AFE);
  847. enable_soidle_by_bit(MT_CG_AUDIO_AFE);
  848. } else if (aud_emi_clk_cntr < 0) {
  849. pr_err("%s aud_emi_clk_cntr:%d<0\n", __func__, aud_emi_clk_cntr);
  850. aud_emi_clk_cntr = 0;
  851. }
  852. mutex_unlock(&emi_clk_mutex);
  853. #endif
  854. }