rt5640.c 73 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450
  1. /*
  2. * rt5640.c -- RT5640/RT5639 ALSA SoC audio codec driver
  3. *
  4. * Copyright 2011 Realtek Semiconductor Corp.
  5. * Author: Johnny Hsu <johnnyhsu@realtek.com>
  6. * Copyright (c) 2013, NVIDIA CORPORATION. All rights reserved.
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <linux/module.h>
  13. #include <linux/moduleparam.h>
  14. #include <linux/init.h>
  15. #include <linux/delay.h>
  16. #include <linux/pm.h>
  17. #include <linux/gpio.h>
  18. #include <linux/i2c.h>
  19. #include <linux/regmap.h>
  20. #include <linux/of.h>
  21. #include <linux/of_gpio.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/spi/spi.h>
  24. #include <linux/acpi.h>
  25. #include <sound/core.h>
  26. #include <sound/pcm.h>
  27. #include <sound/pcm_params.h>
  28. #include <sound/soc.h>
  29. #include <sound/soc-dapm.h>
  30. #include <sound/initval.h>
  31. #include <sound/tlv.h>
  32. #include "rl6231.h"
  33. #include "rt5640.h"
  34. #define RT5640_DEVICE_ID 0x6231
  35. #define RT5640_PR_RANGE_BASE (0xff + 1)
  36. #define RT5640_PR_SPACING 0x100
  37. #define RT5640_PR_BASE (RT5640_PR_RANGE_BASE + (0 * RT5640_PR_SPACING))
  38. static const struct regmap_range_cfg rt5640_ranges[] = {
  39. { .name = "PR", .range_min = RT5640_PR_BASE,
  40. .range_max = RT5640_PR_BASE + 0xb4,
  41. .selector_reg = RT5640_PRIV_INDEX,
  42. .selector_mask = 0xff,
  43. .selector_shift = 0x0,
  44. .window_start = RT5640_PRIV_DATA,
  45. .window_len = 0x1, },
  46. };
  47. static const struct reg_sequence init_list[] = {
  48. {RT5640_PR_BASE + 0x3d, 0x3600},
  49. {RT5640_PR_BASE + 0x12, 0x0aa8},
  50. {RT5640_PR_BASE + 0x14, 0x0aaa},
  51. {RT5640_PR_BASE + 0x20, 0x6110},
  52. {RT5640_PR_BASE + 0x21, 0xe0e0},
  53. {RT5640_PR_BASE + 0x23, 0x1804},
  54. };
  55. static const struct reg_default rt5640_reg[] = {
  56. { 0x00, 0x000e },
  57. { 0x01, 0xc8c8 },
  58. { 0x02, 0xc8c8 },
  59. { 0x03, 0xc8c8 },
  60. { 0x04, 0x8000 },
  61. { 0x0d, 0x0000 },
  62. { 0x0e, 0x0000 },
  63. { 0x0f, 0x0808 },
  64. { 0x19, 0xafaf },
  65. { 0x1a, 0xafaf },
  66. { 0x1b, 0x0000 },
  67. { 0x1c, 0x2f2f },
  68. { 0x1d, 0x2f2f },
  69. { 0x1e, 0x0000 },
  70. { 0x27, 0x7060 },
  71. { 0x28, 0x7070 },
  72. { 0x29, 0x8080 },
  73. { 0x2a, 0x5454 },
  74. { 0x2b, 0x5454 },
  75. { 0x2c, 0xaa00 },
  76. { 0x2d, 0x0000 },
  77. { 0x2e, 0xa000 },
  78. { 0x2f, 0x0000 },
  79. { 0x3b, 0x0000 },
  80. { 0x3c, 0x007f },
  81. { 0x3d, 0x0000 },
  82. { 0x3e, 0x007f },
  83. { 0x45, 0xe000 },
  84. { 0x46, 0x003e },
  85. { 0x47, 0x003e },
  86. { 0x48, 0xf800 },
  87. { 0x49, 0x3800 },
  88. { 0x4a, 0x0004 },
  89. { 0x4c, 0xfc00 },
  90. { 0x4d, 0x0000 },
  91. { 0x4f, 0x01ff },
  92. { 0x50, 0x0000 },
  93. { 0x51, 0x0000 },
  94. { 0x52, 0x01ff },
  95. { 0x53, 0xf000 },
  96. { 0x61, 0x0000 },
  97. { 0x62, 0x0000 },
  98. { 0x63, 0x00c0 },
  99. { 0x64, 0x0000 },
  100. { 0x65, 0x0000 },
  101. { 0x66, 0x0000 },
  102. { 0x6a, 0x0000 },
  103. { 0x6c, 0x0000 },
  104. { 0x70, 0x8000 },
  105. { 0x71, 0x8000 },
  106. { 0x72, 0x8000 },
  107. { 0x73, 0x1114 },
  108. { 0x74, 0x0c00 },
  109. { 0x75, 0x1d00 },
  110. { 0x80, 0x0000 },
  111. { 0x81, 0x0000 },
  112. { 0x82, 0x0000 },
  113. { 0x83, 0x0000 },
  114. { 0x84, 0x0000 },
  115. { 0x85, 0x0008 },
  116. { 0x89, 0x0000 },
  117. { 0x8a, 0x0000 },
  118. { 0x8b, 0x0600 },
  119. { 0x8c, 0x0228 },
  120. { 0x8d, 0xa000 },
  121. { 0x8e, 0x0004 },
  122. { 0x8f, 0x1100 },
  123. { 0x90, 0x0646 },
  124. { 0x91, 0x0c00 },
  125. { 0x92, 0x0000 },
  126. { 0x93, 0x3000 },
  127. { 0xb0, 0x2080 },
  128. { 0xb1, 0x0000 },
  129. { 0xb4, 0x2206 },
  130. { 0xb5, 0x1f00 },
  131. { 0xb6, 0x0000 },
  132. { 0xb8, 0x034b },
  133. { 0xb9, 0x0066 },
  134. { 0xba, 0x000b },
  135. { 0xbb, 0x0000 },
  136. { 0xbc, 0x0000 },
  137. { 0xbd, 0x0000 },
  138. { 0xbe, 0x0000 },
  139. { 0xbf, 0x0000 },
  140. { 0xc0, 0x0400 },
  141. { 0xc2, 0x0000 },
  142. { 0xc4, 0x0000 },
  143. { 0xc5, 0x0000 },
  144. { 0xc6, 0x2000 },
  145. { 0xc8, 0x0000 },
  146. { 0xc9, 0x0000 },
  147. { 0xca, 0x0000 },
  148. { 0xcb, 0x0000 },
  149. { 0xcc, 0x0000 },
  150. { 0xcf, 0x0013 },
  151. { 0xd0, 0x0680 },
  152. { 0xd1, 0x1c17 },
  153. { 0xd2, 0x8c00 },
  154. { 0xd3, 0xaa20 },
  155. { 0xd6, 0x0400 },
  156. { 0xd9, 0x0809 },
  157. { 0xfe, 0x10ec },
  158. { 0xff, 0x6231 },
  159. };
  160. static int rt5640_reset(struct snd_soc_codec *codec)
  161. {
  162. return snd_soc_write(codec, RT5640_RESET, 0);
  163. }
  164. static bool rt5640_volatile_register(struct device *dev, unsigned int reg)
  165. {
  166. int i;
  167. for (i = 0; i < ARRAY_SIZE(rt5640_ranges); i++)
  168. if ((reg >= rt5640_ranges[i].window_start &&
  169. reg <= rt5640_ranges[i].window_start +
  170. rt5640_ranges[i].window_len) ||
  171. (reg >= rt5640_ranges[i].range_min &&
  172. reg <= rt5640_ranges[i].range_max))
  173. return true;
  174. switch (reg) {
  175. case RT5640_RESET:
  176. case RT5640_ASRC_5:
  177. case RT5640_EQ_CTRL1:
  178. case RT5640_DRC_AGC_1:
  179. case RT5640_ANC_CTRL1:
  180. case RT5640_IRQ_CTRL2:
  181. case RT5640_INT_IRQ_ST:
  182. case RT5640_DSP_CTRL2:
  183. case RT5640_DSP_CTRL3:
  184. case RT5640_PRIV_INDEX:
  185. case RT5640_PRIV_DATA:
  186. case RT5640_PGM_REG_ARR1:
  187. case RT5640_PGM_REG_ARR3:
  188. case RT5640_VENDOR_ID:
  189. case RT5640_VENDOR_ID1:
  190. case RT5640_VENDOR_ID2:
  191. return true;
  192. default:
  193. return false;
  194. }
  195. }
  196. static bool rt5640_readable_register(struct device *dev, unsigned int reg)
  197. {
  198. int i;
  199. for (i = 0; i < ARRAY_SIZE(rt5640_ranges); i++)
  200. if ((reg >= rt5640_ranges[i].window_start &&
  201. reg <= rt5640_ranges[i].window_start +
  202. rt5640_ranges[i].window_len) ||
  203. (reg >= rt5640_ranges[i].range_min &&
  204. reg <= rt5640_ranges[i].range_max))
  205. return true;
  206. switch (reg) {
  207. case RT5640_RESET:
  208. case RT5640_SPK_VOL:
  209. case RT5640_HP_VOL:
  210. case RT5640_OUTPUT:
  211. case RT5640_MONO_OUT:
  212. case RT5640_IN1_IN2:
  213. case RT5640_IN3_IN4:
  214. case RT5640_INL_INR_VOL:
  215. case RT5640_DAC1_DIG_VOL:
  216. case RT5640_DAC2_DIG_VOL:
  217. case RT5640_DAC2_CTRL:
  218. case RT5640_ADC_DIG_VOL:
  219. case RT5640_ADC_DATA:
  220. case RT5640_ADC_BST_VOL:
  221. case RT5640_STO_ADC_MIXER:
  222. case RT5640_MONO_ADC_MIXER:
  223. case RT5640_AD_DA_MIXER:
  224. case RT5640_STO_DAC_MIXER:
  225. case RT5640_MONO_DAC_MIXER:
  226. case RT5640_DIG_MIXER:
  227. case RT5640_DSP_PATH1:
  228. case RT5640_DSP_PATH2:
  229. case RT5640_DIG_INF_DATA:
  230. case RT5640_REC_L1_MIXER:
  231. case RT5640_REC_L2_MIXER:
  232. case RT5640_REC_R1_MIXER:
  233. case RT5640_REC_R2_MIXER:
  234. case RT5640_HPO_MIXER:
  235. case RT5640_SPK_L_MIXER:
  236. case RT5640_SPK_R_MIXER:
  237. case RT5640_SPO_L_MIXER:
  238. case RT5640_SPO_R_MIXER:
  239. case RT5640_SPO_CLSD_RATIO:
  240. case RT5640_MONO_MIXER:
  241. case RT5640_OUT_L1_MIXER:
  242. case RT5640_OUT_L2_MIXER:
  243. case RT5640_OUT_L3_MIXER:
  244. case RT5640_OUT_R1_MIXER:
  245. case RT5640_OUT_R2_MIXER:
  246. case RT5640_OUT_R3_MIXER:
  247. case RT5640_LOUT_MIXER:
  248. case RT5640_PWR_DIG1:
  249. case RT5640_PWR_DIG2:
  250. case RT5640_PWR_ANLG1:
  251. case RT5640_PWR_ANLG2:
  252. case RT5640_PWR_MIXER:
  253. case RT5640_PWR_VOL:
  254. case RT5640_PRIV_INDEX:
  255. case RT5640_PRIV_DATA:
  256. case RT5640_I2S1_SDP:
  257. case RT5640_I2S2_SDP:
  258. case RT5640_ADDA_CLK1:
  259. case RT5640_ADDA_CLK2:
  260. case RT5640_DMIC:
  261. case RT5640_GLB_CLK:
  262. case RT5640_PLL_CTRL1:
  263. case RT5640_PLL_CTRL2:
  264. case RT5640_ASRC_1:
  265. case RT5640_ASRC_2:
  266. case RT5640_ASRC_3:
  267. case RT5640_ASRC_4:
  268. case RT5640_ASRC_5:
  269. case RT5640_HP_OVCD:
  270. case RT5640_CLS_D_OVCD:
  271. case RT5640_CLS_D_OUT:
  272. case RT5640_DEPOP_M1:
  273. case RT5640_DEPOP_M2:
  274. case RT5640_DEPOP_M3:
  275. case RT5640_CHARGE_PUMP:
  276. case RT5640_PV_DET_SPK_G:
  277. case RT5640_MICBIAS:
  278. case RT5640_EQ_CTRL1:
  279. case RT5640_EQ_CTRL2:
  280. case RT5640_WIND_FILTER:
  281. case RT5640_DRC_AGC_1:
  282. case RT5640_DRC_AGC_2:
  283. case RT5640_DRC_AGC_3:
  284. case RT5640_SVOL_ZC:
  285. case RT5640_ANC_CTRL1:
  286. case RT5640_ANC_CTRL2:
  287. case RT5640_ANC_CTRL3:
  288. case RT5640_JD_CTRL:
  289. case RT5640_ANC_JD:
  290. case RT5640_IRQ_CTRL1:
  291. case RT5640_IRQ_CTRL2:
  292. case RT5640_INT_IRQ_ST:
  293. case RT5640_GPIO_CTRL1:
  294. case RT5640_GPIO_CTRL2:
  295. case RT5640_GPIO_CTRL3:
  296. case RT5640_DSP_CTRL1:
  297. case RT5640_DSP_CTRL2:
  298. case RT5640_DSP_CTRL3:
  299. case RT5640_DSP_CTRL4:
  300. case RT5640_PGM_REG_ARR1:
  301. case RT5640_PGM_REG_ARR2:
  302. case RT5640_PGM_REG_ARR3:
  303. case RT5640_PGM_REG_ARR4:
  304. case RT5640_PGM_REG_ARR5:
  305. case RT5640_SCB_FUNC:
  306. case RT5640_SCB_CTRL:
  307. case RT5640_BASE_BACK:
  308. case RT5640_MP3_PLUS1:
  309. case RT5640_MP3_PLUS2:
  310. case RT5640_3D_HP:
  311. case RT5640_ADJ_HPF:
  312. case RT5640_HP_CALIB_AMP_DET:
  313. case RT5640_HP_CALIB2:
  314. case RT5640_SV_ZCD1:
  315. case RT5640_SV_ZCD2:
  316. case RT5640_DUMMY1:
  317. case RT5640_DUMMY2:
  318. case RT5640_DUMMY3:
  319. case RT5640_VENDOR_ID:
  320. case RT5640_VENDOR_ID1:
  321. case RT5640_VENDOR_ID2:
  322. return true;
  323. default:
  324. return false;
  325. }
  326. }
  327. static const DECLARE_TLV_DB_SCALE(out_vol_tlv, -4650, 150, 0);
  328. static const DECLARE_TLV_DB_SCALE(dac_vol_tlv, -65625, 375, 0);
  329. static const DECLARE_TLV_DB_SCALE(in_vol_tlv, -3450, 150, 0);
  330. static const DECLARE_TLV_DB_SCALE(adc_vol_tlv, -17625, 375, 0);
  331. static const DECLARE_TLV_DB_SCALE(adc_bst_tlv, 0, 1200, 0);
  332. /* {0, +20, +24, +30, +35, +40, +44, +50, +52} dB */
  333. static const DECLARE_TLV_DB_RANGE(bst_tlv,
  334. 0, 0, TLV_DB_SCALE_ITEM(0, 0, 0),
  335. 1, 1, TLV_DB_SCALE_ITEM(2000, 0, 0),
  336. 2, 2, TLV_DB_SCALE_ITEM(2400, 0, 0),
  337. 3, 5, TLV_DB_SCALE_ITEM(3000, 500, 0),
  338. 6, 6, TLV_DB_SCALE_ITEM(4400, 0, 0),
  339. 7, 7, TLV_DB_SCALE_ITEM(5000, 0, 0),
  340. 8, 8, TLV_DB_SCALE_ITEM(5200, 0, 0)
  341. );
  342. /* Interface data select */
  343. static const char * const rt5640_data_select[] = {
  344. "Normal", "Swap", "left copy to right", "right copy to left"};
  345. static SOC_ENUM_SINGLE_DECL(rt5640_if1_dac_enum, RT5640_DIG_INF_DATA,
  346. RT5640_IF1_DAC_SEL_SFT, rt5640_data_select);
  347. static SOC_ENUM_SINGLE_DECL(rt5640_if1_adc_enum, RT5640_DIG_INF_DATA,
  348. RT5640_IF1_ADC_SEL_SFT, rt5640_data_select);
  349. static SOC_ENUM_SINGLE_DECL(rt5640_if2_dac_enum, RT5640_DIG_INF_DATA,
  350. RT5640_IF2_DAC_SEL_SFT, rt5640_data_select);
  351. static SOC_ENUM_SINGLE_DECL(rt5640_if2_adc_enum, RT5640_DIG_INF_DATA,
  352. RT5640_IF2_ADC_SEL_SFT, rt5640_data_select);
  353. /* Class D speaker gain ratio */
  354. static const char * const rt5640_clsd_spk_ratio[] = {"1.66x", "1.83x", "1.94x",
  355. "2x", "2.11x", "2.22x", "2.33x", "2.44x", "2.55x", "2.66x", "2.77x"};
  356. static SOC_ENUM_SINGLE_DECL(rt5640_clsd_spk_ratio_enum, RT5640_CLS_D_OUT,
  357. RT5640_CLSD_RATIO_SFT, rt5640_clsd_spk_ratio);
  358. static const struct snd_kcontrol_new rt5640_snd_controls[] = {
  359. /* Speaker Output Volume */
  360. SOC_DOUBLE("Speaker Channel Switch", RT5640_SPK_VOL,
  361. RT5640_VOL_L_SFT, RT5640_VOL_R_SFT, 1, 1),
  362. SOC_DOUBLE_TLV("Speaker Playback Volume", RT5640_SPK_VOL,
  363. RT5640_L_VOL_SFT, RT5640_R_VOL_SFT, 39, 1, out_vol_tlv),
  364. /* Headphone Output Volume */
  365. SOC_DOUBLE("HP Channel Switch", RT5640_HP_VOL,
  366. RT5640_VOL_L_SFT, RT5640_VOL_R_SFT, 1, 1),
  367. SOC_DOUBLE_TLV("HP Playback Volume", RT5640_HP_VOL,
  368. RT5640_L_VOL_SFT, RT5640_R_VOL_SFT, 39, 1, out_vol_tlv),
  369. /* OUTPUT Control */
  370. SOC_DOUBLE("OUT Playback Switch", RT5640_OUTPUT,
  371. RT5640_L_MUTE_SFT, RT5640_R_MUTE_SFT, 1, 1),
  372. SOC_DOUBLE("OUT Channel Switch", RT5640_OUTPUT,
  373. RT5640_VOL_L_SFT, RT5640_VOL_R_SFT, 1, 1),
  374. SOC_DOUBLE_TLV("OUT Playback Volume", RT5640_OUTPUT,
  375. RT5640_L_VOL_SFT, RT5640_R_VOL_SFT, 39, 1, out_vol_tlv),
  376. /* DAC Digital Volume */
  377. SOC_DOUBLE("DAC2 Playback Switch", RT5640_DAC2_CTRL,
  378. RT5640_M_DAC_L2_VOL_SFT, RT5640_M_DAC_R2_VOL_SFT, 1, 1),
  379. SOC_DOUBLE_TLV("DAC1 Playback Volume", RT5640_DAC1_DIG_VOL,
  380. RT5640_L_VOL_SFT, RT5640_R_VOL_SFT,
  381. 175, 0, dac_vol_tlv),
  382. /* IN1/IN2/IN3 Control */
  383. SOC_SINGLE_TLV("IN1 Boost", RT5640_IN1_IN2,
  384. RT5640_BST_SFT1, 8, 0, bst_tlv),
  385. SOC_SINGLE_TLV("IN2 Boost", RT5640_IN3_IN4,
  386. RT5640_BST_SFT2, 8, 0, bst_tlv),
  387. SOC_SINGLE_TLV("IN3 Boost", RT5640_IN1_IN2,
  388. RT5640_BST_SFT2, 8, 0, bst_tlv),
  389. /* INL/INR Volume Control */
  390. SOC_DOUBLE_TLV("IN Capture Volume", RT5640_INL_INR_VOL,
  391. RT5640_INL_VOL_SFT, RT5640_INR_VOL_SFT,
  392. 31, 1, in_vol_tlv),
  393. /* ADC Digital Volume Control */
  394. SOC_DOUBLE("ADC Capture Switch", RT5640_ADC_DIG_VOL,
  395. RT5640_L_MUTE_SFT, RT5640_R_MUTE_SFT, 1, 1),
  396. SOC_DOUBLE_TLV("ADC Capture Volume", RT5640_ADC_DIG_VOL,
  397. RT5640_L_VOL_SFT, RT5640_R_VOL_SFT,
  398. 127, 0, adc_vol_tlv),
  399. SOC_DOUBLE_TLV("Mono ADC Capture Volume", RT5640_ADC_DATA,
  400. RT5640_L_VOL_SFT, RT5640_R_VOL_SFT,
  401. 127, 0, adc_vol_tlv),
  402. /* ADC Boost Volume Control */
  403. SOC_DOUBLE_TLV("ADC Boost Gain", RT5640_ADC_BST_VOL,
  404. RT5640_ADC_L_BST_SFT, RT5640_ADC_R_BST_SFT,
  405. 3, 0, adc_bst_tlv),
  406. /* Class D speaker gain ratio */
  407. SOC_ENUM("Class D SPK Ratio Control", rt5640_clsd_spk_ratio_enum),
  408. SOC_ENUM("ADC IF1 Data Switch", rt5640_if1_adc_enum),
  409. SOC_ENUM("DAC IF1 Data Switch", rt5640_if1_dac_enum),
  410. SOC_ENUM("ADC IF2 Data Switch", rt5640_if2_adc_enum),
  411. SOC_ENUM("DAC IF2 Data Switch", rt5640_if2_dac_enum),
  412. };
  413. static const struct snd_kcontrol_new rt5640_specific_snd_controls[] = {
  414. /* MONO Output Control */
  415. SOC_SINGLE("Mono Playback Switch", RT5640_MONO_OUT, RT5640_L_MUTE_SFT,
  416. 1, 1),
  417. SOC_DOUBLE_TLV("Mono DAC Playback Volume", RT5640_DAC2_DIG_VOL,
  418. RT5640_L_VOL_SFT, RT5640_R_VOL_SFT, 175, 0, dac_vol_tlv),
  419. };
  420. /**
  421. * set_dmic_clk - Set parameter of dmic.
  422. *
  423. * @w: DAPM widget.
  424. * @kcontrol: The kcontrol of this widget.
  425. * @event: Event id.
  426. *
  427. */
  428. static int set_dmic_clk(struct snd_soc_dapm_widget *w,
  429. struct snd_kcontrol *kcontrol, int event)
  430. {
  431. struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
  432. struct rt5640_priv *rt5640 = snd_soc_codec_get_drvdata(codec);
  433. int idx, rate;
  434. rate = rt5640->sysclk / rl6231_get_pre_div(rt5640->regmap,
  435. RT5640_ADDA_CLK1, RT5640_I2S_PD1_SFT);
  436. idx = rl6231_calc_dmic_clk(rate);
  437. if (idx < 0)
  438. dev_err(codec->dev, "Failed to set DMIC clock\n");
  439. else
  440. snd_soc_update_bits(codec, RT5640_DMIC, RT5640_DMIC_CLK_MASK,
  441. idx << RT5640_DMIC_CLK_SFT);
  442. return idx;
  443. }
  444. static int is_sys_clk_from_pll(struct snd_soc_dapm_widget *source,
  445. struct snd_soc_dapm_widget *sink)
  446. {
  447. struct snd_soc_codec *codec = snd_soc_dapm_to_codec(source->dapm);
  448. unsigned int val;
  449. val = snd_soc_read(codec, RT5640_GLB_CLK);
  450. val &= RT5640_SCLK_SRC_MASK;
  451. if (val == RT5640_SCLK_SRC_PLL1)
  452. return 1;
  453. else
  454. return 0;
  455. }
  456. static int is_using_asrc(struct snd_soc_dapm_widget *source,
  457. struct snd_soc_dapm_widget *sink)
  458. {
  459. struct snd_soc_codec *codec = snd_soc_dapm_to_codec(source->dapm);
  460. struct rt5640_priv *rt5640 = snd_soc_codec_get_drvdata(codec);
  461. if (!rt5640->asrc_en)
  462. return 0;
  463. return 1;
  464. }
  465. /* Digital Mixer */
  466. static const struct snd_kcontrol_new rt5640_sto_adc_l_mix[] = {
  467. SOC_DAPM_SINGLE("ADC1 Switch", RT5640_STO_ADC_MIXER,
  468. RT5640_M_ADC_L1_SFT, 1, 1),
  469. SOC_DAPM_SINGLE("ADC2 Switch", RT5640_STO_ADC_MIXER,
  470. RT5640_M_ADC_L2_SFT, 1, 1),
  471. };
  472. static const struct snd_kcontrol_new rt5640_sto_adc_r_mix[] = {
  473. SOC_DAPM_SINGLE("ADC1 Switch", RT5640_STO_ADC_MIXER,
  474. RT5640_M_ADC_R1_SFT, 1, 1),
  475. SOC_DAPM_SINGLE("ADC2 Switch", RT5640_STO_ADC_MIXER,
  476. RT5640_M_ADC_R2_SFT, 1, 1),
  477. };
  478. static const struct snd_kcontrol_new rt5640_mono_adc_l_mix[] = {
  479. SOC_DAPM_SINGLE("ADC1 Switch", RT5640_MONO_ADC_MIXER,
  480. RT5640_M_MONO_ADC_L1_SFT, 1, 1),
  481. SOC_DAPM_SINGLE("ADC2 Switch", RT5640_MONO_ADC_MIXER,
  482. RT5640_M_MONO_ADC_L2_SFT, 1, 1),
  483. };
  484. static const struct snd_kcontrol_new rt5640_mono_adc_r_mix[] = {
  485. SOC_DAPM_SINGLE("ADC1 Switch", RT5640_MONO_ADC_MIXER,
  486. RT5640_M_MONO_ADC_R1_SFT, 1, 1),
  487. SOC_DAPM_SINGLE("ADC2 Switch", RT5640_MONO_ADC_MIXER,
  488. RT5640_M_MONO_ADC_R2_SFT, 1, 1),
  489. };
  490. static const struct snd_kcontrol_new rt5640_dac_l_mix[] = {
  491. SOC_DAPM_SINGLE("Stereo ADC Switch", RT5640_AD_DA_MIXER,
  492. RT5640_M_ADCMIX_L_SFT, 1, 1),
  493. SOC_DAPM_SINGLE("INF1 Switch", RT5640_AD_DA_MIXER,
  494. RT5640_M_IF1_DAC_L_SFT, 1, 1),
  495. };
  496. static const struct snd_kcontrol_new rt5640_dac_r_mix[] = {
  497. SOC_DAPM_SINGLE("Stereo ADC Switch", RT5640_AD_DA_MIXER,
  498. RT5640_M_ADCMIX_R_SFT, 1, 1),
  499. SOC_DAPM_SINGLE("INF1 Switch", RT5640_AD_DA_MIXER,
  500. RT5640_M_IF1_DAC_R_SFT, 1, 1),
  501. };
  502. static const struct snd_kcontrol_new rt5640_sto_dac_l_mix[] = {
  503. SOC_DAPM_SINGLE("DAC L1 Switch", RT5640_STO_DAC_MIXER,
  504. RT5640_M_DAC_L1_SFT, 1, 1),
  505. SOC_DAPM_SINGLE("DAC L2 Switch", RT5640_STO_DAC_MIXER,
  506. RT5640_M_DAC_L2_SFT, 1, 1),
  507. SOC_DAPM_SINGLE("ANC Switch", RT5640_STO_DAC_MIXER,
  508. RT5640_M_ANC_DAC_L_SFT, 1, 1),
  509. };
  510. static const struct snd_kcontrol_new rt5640_sto_dac_r_mix[] = {
  511. SOC_DAPM_SINGLE("DAC R1 Switch", RT5640_STO_DAC_MIXER,
  512. RT5640_M_DAC_R1_SFT, 1, 1),
  513. SOC_DAPM_SINGLE("DAC R2 Switch", RT5640_STO_DAC_MIXER,
  514. RT5640_M_DAC_R2_SFT, 1, 1),
  515. SOC_DAPM_SINGLE("ANC Switch", RT5640_STO_DAC_MIXER,
  516. RT5640_M_ANC_DAC_R_SFT, 1, 1),
  517. };
  518. static const struct snd_kcontrol_new rt5639_sto_dac_l_mix[] = {
  519. SOC_DAPM_SINGLE("DAC L1 Switch", RT5640_STO_DAC_MIXER,
  520. RT5640_M_DAC_L1_SFT, 1, 1),
  521. SOC_DAPM_SINGLE("DAC L2 Switch", RT5640_STO_DAC_MIXER,
  522. RT5640_M_DAC_L2_SFT, 1, 1),
  523. };
  524. static const struct snd_kcontrol_new rt5639_sto_dac_r_mix[] = {
  525. SOC_DAPM_SINGLE("DAC R1 Switch", RT5640_STO_DAC_MIXER,
  526. RT5640_M_DAC_R1_SFT, 1, 1),
  527. SOC_DAPM_SINGLE("DAC R2 Switch", RT5640_STO_DAC_MIXER,
  528. RT5640_M_DAC_R2_SFT, 1, 1),
  529. };
  530. static const struct snd_kcontrol_new rt5640_mono_dac_l_mix[] = {
  531. SOC_DAPM_SINGLE("DAC L1 Switch", RT5640_MONO_DAC_MIXER,
  532. RT5640_M_DAC_L1_MONO_L_SFT, 1, 1),
  533. SOC_DAPM_SINGLE("DAC L2 Switch", RT5640_MONO_DAC_MIXER,
  534. RT5640_M_DAC_L2_MONO_L_SFT, 1, 1),
  535. SOC_DAPM_SINGLE("DAC R2 Switch", RT5640_MONO_DAC_MIXER,
  536. RT5640_M_DAC_R2_MONO_L_SFT, 1, 1),
  537. };
  538. static const struct snd_kcontrol_new rt5640_mono_dac_r_mix[] = {
  539. SOC_DAPM_SINGLE("DAC R1 Switch", RT5640_MONO_DAC_MIXER,
  540. RT5640_M_DAC_R1_MONO_R_SFT, 1, 1),
  541. SOC_DAPM_SINGLE("DAC R2 Switch", RT5640_MONO_DAC_MIXER,
  542. RT5640_M_DAC_R2_MONO_R_SFT, 1, 1),
  543. SOC_DAPM_SINGLE("DAC L2 Switch", RT5640_MONO_DAC_MIXER,
  544. RT5640_M_DAC_L2_MONO_R_SFT, 1, 1),
  545. };
  546. static const struct snd_kcontrol_new rt5640_dig_l_mix[] = {
  547. SOC_DAPM_SINGLE("DAC L1 Switch", RT5640_DIG_MIXER,
  548. RT5640_M_STO_L_DAC_L_SFT, 1, 1),
  549. SOC_DAPM_SINGLE("DAC L2 Switch", RT5640_DIG_MIXER,
  550. RT5640_M_DAC_L2_DAC_L_SFT, 1, 1),
  551. };
  552. static const struct snd_kcontrol_new rt5640_dig_r_mix[] = {
  553. SOC_DAPM_SINGLE("DAC R1 Switch", RT5640_DIG_MIXER,
  554. RT5640_M_STO_R_DAC_R_SFT, 1, 1),
  555. SOC_DAPM_SINGLE("DAC R2 Switch", RT5640_DIG_MIXER,
  556. RT5640_M_DAC_R2_DAC_R_SFT, 1, 1),
  557. };
  558. /* Analog Input Mixer */
  559. static const struct snd_kcontrol_new rt5640_rec_l_mix[] = {
  560. SOC_DAPM_SINGLE("HPOL Switch", RT5640_REC_L2_MIXER,
  561. RT5640_M_HP_L_RM_L_SFT, 1, 1),
  562. SOC_DAPM_SINGLE("INL Switch", RT5640_REC_L2_MIXER,
  563. RT5640_M_IN_L_RM_L_SFT, 1, 1),
  564. SOC_DAPM_SINGLE("BST3 Switch", RT5640_REC_L2_MIXER,
  565. RT5640_M_BST2_RM_L_SFT, 1, 1),
  566. SOC_DAPM_SINGLE("BST2 Switch", RT5640_REC_L2_MIXER,
  567. RT5640_M_BST4_RM_L_SFT, 1, 1),
  568. SOC_DAPM_SINGLE("BST1 Switch", RT5640_REC_L2_MIXER,
  569. RT5640_M_BST1_RM_L_SFT, 1, 1),
  570. SOC_DAPM_SINGLE("OUT MIXL Switch", RT5640_REC_L2_MIXER,
  571. RT5640_M_OM_L_RM_L_SFT, 1, 1),
  572. };
  573. static const struct snd_kcontrol_new rt5640_rec_r_mix[] = {
  574. SOC_DAPM_SINGLE("HPOR Switch", RT5640_REC_R2_MIXER,
  575. RT5640_M_HP_R_RM_R_SFT, 1, 1),
  576. SOC_DAPM_SINGLE("INR Switch", RT5640_REC_R2_MIXER,
  577. RT5640_M_IN_R_RM_R_SFT, 1, 1),
  578. SOC_DAPM_SINGLE("BST3 Switch", RT5640_REC_R2_MIXER,
  579. RT5640_M_BST2_RM_R_SFT, 1, 1),
  580. SOC_DAPM_SINGLE("BST2 Switch", RT5640_REC_R2_MIXER,
  581. RT5640_M_BST4_RM_R_SFT, 1, 1),
  582. SOC_DAPM_SINGLE("BST1 Switch", RT5640_REC_R2_MIXER,
  583. RT5640_M_BST1_RM_R_SFT, 1, 1),
  584. SOC_DAPM_SINGLE("OUT MIXR Switch", RT5640_REC_R2_MIXER,
  585. RT5640_M_OM_R_RM_R_SFT, 1, 1),
  586. };
  587. /* Analog Output Mixer */
  588. static const struct snd_kcontrol_new rt5640_spk_l_mix[] = {
  589. SOC_DAPM_SINGLE("REC MIXL Switch", RT5640_SPK_L_MIXER,
  590. RT5640_M_RM_L_SM_L_SFT, 1, 1),
  591. SOC_DAPM_SINGLE("INL Switch", RT5640_SPK_L_MIXER,
  592. RT5640_M_IN_L_SM_L_SFT, 1, 1),
  593. SOC_DAPM_SINGLE("DAC L1 Switch", RT5640_SPK_L_MIXER,
  594. RT5640_M_DAC_L1_SM_L_SFT, 1, 1),
  595. SOC_DAPM_SINGLE("DAC L2 Switch", RT5640_SPK_L_MIXER,
  596. RT5640_M_DAC_L2_SM_L_SFT, 1, 1),
  597. SOC_DAPM_SINGLE("OUT MIXL Switch", RT5640_SPK_L_MIXER,
  598. RT5640_M_OM_L_SM_L_SFT, 1, 1),
  599. };
  600. static const struct snd_kcontrol_new rt5640_spk_r_mix[] = {
  601. SOC_DAPM_SINGLE("REC MIXR Switch", RT5640_SPK_R_MIXER,
  602. RT5640_M_RM_R_SM_R_SFT, 1, 1),
  603. SOC_DAPM_SINGLE("INR Switch", RT5640_SPK_R_MIXER,
  604. RT5640_M_IN_R_SM_R_SFT, 1, 1),
  605. SOC_DAPM_SINGLE("DAC R1 Switch", RT5640_SPK_R_MIXER,
  606. RT5640_M_DAC_R1_SM_R_SFT, 1, 1),
  607. SOC_DAPM_SINGLE("DAC R2 Switch", RT5640_SPK_R_MIXER,
  608. RT5640_M_DAC_R2_SM_R_SFT, 1, 1),
  609. SOC_DAPM_SINGLE("OUT MIXR Switch", RT5640_SPK_R_MIXER,
  610. RT5640_M_OM_R_SM_R_SFT, 1, 1),
  611. };
  612. static const struct snd_kcontrol_new rt5640_out_l_mix[] = {
  613. SOC_DAPM_SINGLE("SPK MIXL Switch", RT5640_OUT_L3_MIXER,
  614. RT5640_M_SM_L_OM_L_SFT, 1, 1),
  615. SOC_DAPM_SINGLE("BST1 Switch", RT5640_OUT_L3_MIXER,
  616. RT5640_M_BST1_OM_L_SFT, 1, 1),
  617. SOC_DAPM_SINGLE("INL Switch", RT5640_OUT_L3_MIXER,
  618. RT5640_M_IN_L_OM_L_SFT, 1, 1),
  619. SOC_DAPM_SINGLE("REC MIXL Switch", RT5640_OUT_L3_MIXER,
  620. RT5640_M_RM_L_OM_L_SFT, 1, 1),
  621. SOC_DAPM_SINGLE("DAC R2 Switch", RT5640_OUT_L3_MIXER,
  622. RT5640_M_DAC_R2_OM_L_SFT, 1, 1),
  623. SOC_DAPM_SINGLE("DAC L2 Switch", RT5640_OUT_L3_MIXER,
  624. RT5640_M_DAC_L2_OM_L_SFT, 1, 1),
  625. SOC_DAPM_SINGLE("DAC L1 Switch", RT5640_OUT_L3_MIXER,
  626. RT5640_M_DAC_L1_OM_L_SFT, 1, 1),
  627. };
  628. static const struct snd_kcontrol_new rt5640_out_r_mix[] = {
  629. SOC_DAPM_SINGLE("SPK MIXR Switch", RT5640_OUT_R3_MIXER,
  630. RT5640_M_SM_L_OM_R_SFT, 1, 1),
  631. SOC_DAPM_SINGLE("BST2 Switch", RT5640_OUT_R3_MIXER,
  632. RT5640_M_BST4_OM_R_SFT, 1, 1),
  633. SOC_DAPM_SINGLE("BST1 Switch", RT5640_OUT_R3_MIXER,
  634. RT5640_M_BST1_OM_R_SFT, 1, 1),
  635. SOC_DAPM_SINGLE("INR Switch", RT5640_OUT_R3_MIXER,
  636. RT5640_M_IN_R_OM_R_SFT, 1, 1),
  637. SOC_DAPM_SINGLE("REC MIXR Switch", RT5640_OUT_R3_MIXER,
  638. RT5640_M_RM_R_OM_R_SFT, 1, 1),
  639. SOC_DAPM_SINGLE("DAC L2 Switch", RT5640_OUT_R3_MIXER,
  640. RT5640_M_DAC_L2_OM_R_SFT, 1, 1),
  641. SOC_DAPM_SINGLE("DAC R2 Switch", RT5640_OUT_R3_MIXER,
  642. RT5640_M_DAC_R2_OM_R_SFT, 1, 1),
  643. SOC_DAPM_SINGLE("DAC R1 Switch", RT5640_OUT_R3_MIXER,
  644. RT5640_M_DAC_R1_OM_R_SFT, 1, 1),
  645. };
  646. static const struct snd_kcontrol_new rt5639_out_l_mix[] = {
  647. SOC_DAPM_SINGLE("BST1 Switch", RT5640_OUT_L3_MIXER,
  648. RT5640_M_BST1_OM_L_SFT, 1, 1),
  649. SOC_DAPM_SINGLE("INL Switch", RT5640_OUT_L3_MIXER,
  650. RT5640_M_IN_L_OM_L_SFT, 1, 1),
  651. SOC_DAPM_SINGLE("REC MIXL Switch", RT5640_OUT_L3_MIXER,
  652. RT5640_M_RM_L_OM_L_SFT, 1, 1),
  653. SOC_DAPM_SINGLE("DAC L1 Switch", RT5640_OUT_L3_MIXER,
  654. RT5640_M_DAC_L1_OM_L_SFT, 1, 1),
  655. };
  656. static const struct snd_kcontrol_new rt5639_out_r_mix[] = {
  657. SOC_DAPM_SINGLE("BST2 Switch", RT5640_OUT_R3_MIXER,
  658. RT5640_M_BST4_OM_R_SFT, 1, 1),
  659. SOC_DAPM_SINGLE("BST1 Switch", RT5640_OUT_R3_MIXER,
  660. RT5640_M_BST1_OM_R_SFT, 1, 1),
  661. SOC_DAPM_SINGLE("INR Switch", RT5640_OUT_R3_MIXER,
  662. RT5640_M_IN_R_OM_R_SFT, 1, 1),
  663. SOC_DAPM_SINGLE("REC MIXR Switch", RT5640_OUT_R3_MIXER,
  664. RT5640_M_RM_R_OM_R_SFT, 1, 1),
  665. SOC_DAPM_SINGLE("DAC R1 Switch", RT5640_OUT_R3_MIXER,
  666. RT5640_M_DAC_R1_OM_R_SFT, 1, 1),
  667. };
  668. static const struct snd_kcontrol_new rt5640_spo_l_mix[] = {
  669. SOC_DAPM_SINGLE("DAC R1 Switch", RT5640_SPO_L_MIXER,
  670. RT5640_M_DAC_R1_SPM_L_SFT, 1, 1),
  671. SOC_DAPM_SINGLE("DAC L1 Switch", RT5640_SPO_L_MIXER,
  672. RT5640_M_DAC_L1_SPM_L_SFT, 1, 1),
  673. SOC_DAPM_SINGLE("SPKVOL R Switch", RT5640_SPO_L_MIXER,
  674. RT5640_M_SV_R_SPM_L_SFT, 1, 1),
  675. SOC_DAPM_SINGLE("SPKVOL L Switch", RT5640_SPO_L_MIXER,
  676. RT5640_M_SV_L_SPM_L_SFT, 1, 1),
  677. SOC_DAPM_SINGLE("BST1 Switch", RT5640_SPO_L_MIXER,
  678. RT5640_M_BST1_SPM_L_SFT, 1, 1),
  679. };
  680. static const struct snd_kcontrol_new rt5640_spo_r_mix[] = {
  681. SOC_DAPM_SINGLE("DAC R1 Switch", RT5640_SPO_R_MIXER,
  682. RT5640_M_DAC_R1_SPM_R_SFT, 1, 1),
  683. SOC_DAPM_SINGLE("SPKVOL R Switch", RT5640_SPO_R_MIXER,
  684. RT5640_M_SV_R_SPM_R_SFT, 1, 1),
  685. SOC_DAPM_SINGLE("BST1 Switch", RT5640_SPO_R_MIXER,
  686. RT5640_M_BST1_SPM_R_SFT, 1, 1),
  687. };
  688. static const struct snd_kcontrol_new rt5640_hpo_mix[] = {
  689. SOC_DAPM_SINGLE("HPO MIX DAC2 Switch", RT5640_HPO_MIXER,
  690. RT5640_M_DAC2_HM_SFT, 1, 1),
  691. SOC_DAPM_SINGLE("HPO MIX DAC1 Switch", RT5640_HPO_MIXER,
  692. RT5640_M_DAC1_HM_SFT, 1, 1),
  693. SOC_DAPM_SINGLE("HPO MIX HPVOL Switch", RT5640_HPO_MIXER,
  694. RT5640_M_HPVOL_HM_SFT, 1, 1),
  695. };
  696. static const struct snd_kcontrol_new rt5639_hpo_mix[] = {
  697. SOC_DAPM_SINGLE("HPO MIX DAC1 Switch", RT5640_HPO_MIXER,
  698. RT5640_M_DAC1_HM_SFT, 1, 1),
  699. SOC_DAPM_SINGLE("HPO MIX HPVOL Switch", RT5640_HPO_MIXER,
  700. RT5640_M_HPVOL_HM_SFT, 1, 1),
  701. };
  702. static const struct snd_kcontrol_new rt5640_lout_mix[] = {
  703. SOC_DAPM_SINGLE("DAC L1 Switch", RT5640_LOUT_MIXER,
  704. RT5640_M_DAC_L1_LM_SFT, 1, 1),
  705. SOC_DAPM_SINGLE("DAC R1 Switch", RT5640_LOUT_MIXER,
  706. RT5640_M_DAC_R1_LM_SFT, 1, 1),
  707. SOC_DAPM_SINGLE("OUTVOL L Switch", RT5640_LOUT_MIXER,
  708. RT5640_M_OV_L_LM_SFT, 1, 1),
  709. SOC_DAPM_SINGLE("OUTVOL R Switch", RT5640_LOUT_MIXER,
  710. RT5640_M_OV_R_LM_SFT, 1, 1),
  711. };
  712. static const struct snd_kcontrol_new rt5640_mono_mix[] = {
  713. SOC_DAPM_SINGLE("DAC R2 Switch", RT5640_MONO_MIXER,
  714. RT5640_M_DAC_R2_MM_SFT, 1, 1),
  715. SOC_DAPM_SINGLE("DAC L2 Switch", RT5640_MONO_MIXER,
  716. RT5640_M_DAC_L2_MM_SFT, 1, 1),
  717. SOC_DAPM_SINGLE("OUTVOL R Switch", RT5640_MONO_MIXER,
  718. RT5640_M_OV_R_MM_SFT, 1, 1),
  719. SOC_DAPM_SINGLE("OUTVOL L Switch", RT5640_MONO_MIXER,
  720. RT5640_M_OV_L_MM_SFT, 1, 1),
  721. SOC_DAPM_SINGLE("BST1 Switch", RT5640_MONO_MIXER,
  722. RT5640_M_BST1_MM_SFT, 1, 1),
  723. };
  724. static const struct snd_kcontrol_new spk_l_enable_control =
  725. SOC_DAPM_SINGLE_AUTODISABLE("Switch", RT5640_SPK_VOL,
  726. RT5640_L_MUTE_SFT, 1, 1);
  727. static const struct snd_kcontrol_new spk_r_enable_control =
  728. SOC_DAPM_SINGLE_AUTODISABLE("Switch", RT5640_SPK_VOL,
  729. RT5640_R_MUTE_SFT, 1, 1);
  730. static const struct snd_kcontrol_new hp_l_enable_control =
  731. SOC_DAPM_SINGLE_AUTODISABLE("Switch", RT5640_HP_VOL,
  732. RT5640_L_MUTE_SFT, 1, 1);
  733. static const struct snd_kcontrol_new hp_r_enable_control =
  734. SOC_DAPM_SINGLE_AUTODISABLE("Switch", RT5640_HP_VOL,
  735. RT5640_R_MUTE_SFT, 1, 1);
  736. /* Stereo ADC source */
  737. static const char * const rt5640_stereo_adc1_src[] = {
  738. "DIG MIX", "ADC"
  739. };
  740. static SOC_ENUM_SINGLE_DECL(rt5640_stereo_adc1_enum, RT5640_STO_ADC_MIXER,
  741. RT5640_ADC_1_SRC_SFT, rt5640_stereo_adc1_src);
  742. static const struct snd_kcontrol_new rt5640_sto_adc_1_mux =
  743. SOC_DAPM_ENUM("Stereo ADC1 Mux", rt5640_stereo_adc1_enum);
  744. static const char * const rt5640_stereo_adc2_src[] = {
  745. "DMIC1", "DMIC2", "DIG MIX"
  746. };
  747. static SOC_ENUM_SINGLE_DECL(rt5640_stereo_adc2_enum, RT5640_STO_ADC_MIXER,
  748. RT5640_ADC_2_SRC_SFT, rt5640_stereo_adc2_src);
  749. static const struct snd_kcontrol_new rt5640_sto_adc_2_mux =
  750. SOC_DAPM_ENUM("Stereo ADC2 Mux", rt5640_stereo_adc2_enum);
  751. /* Mono ADC source */
  752. static const char * const rt5640_mono_adc_l1_src[] = {
  753. "Mono DAC MIXL", "ADCL"
  754. };
  755. static SOC_ENUM_SINGLE_DECL(rt5640_mono_adc_l1_enum, RT5640_MONO_ADC_MIXER,
  756. RT5640_MONO_ADC_L1_SRC_SFT, rt5640_mono_adc_l1_src);
  757. static const struct snd_kcontrol_new rt5640_mono_adc_l1_mux =
  758. SOC_DAPM_ENUM("Mono ADC1 left source", rt5640_mono_adc_l1_enum);
  759. static const char * const rt5640_mono_adc_l2_src[] = {
  760. "DMIC L1", "DMIC L2", "Mono DAC MIXL"
  761. };
  762. static SOC_ENUM_SINGLE_DECL(rt5640_mono_adc_l2_enum, RT5640_MONO_ADC_MIXER,
  763. RT5640_MONO_ADC_L2_SRC_SFT, rt5640_mono_adc_l2_src);
  764. static const struct snd_kcontrol_new rt5640_mono_adc_l2_mux =
  765. SOC_DAPM_ENUM("Mono ADC2 left source", rt5640_mono_adc_l2_enum);
  766. static const char * const rt5640_mono_adc_r1_src[] = {
  767. "Mono DAC MIXR", "ADCR"
  768. };
  769. static SOC_ENUM_SINGLE_DECL(rt5640_mono_adc_r1_enum, RT5640_MONO_ADC_MIXER,
  770. RT5640_MONO_ADC_R1_SRC_SFT, rt5640_mono_adc_r1_src);
  771. static const struct snd_kcontrol_new rt5640_mono_adc_r1_mux =
  772. SOC_DAPM_ENUM("Mono ADC1 right source", rt5640_mono_adc_r1_enum);
  773. static const char * const rt5640_mono_adc_r2_src[] = {
  774. "DMIC R1", "DMIC R2", "Mono DAC MIXR"
  775. };
  776. static SOC_ENUM_SINGLE_DECL(rt5640_mono_adc_r2_enum, RT5640_MONO_ADC_MIXER,
  777. RT5640_MONO_ADC_R2_SRC_SFT, rt5640_mono_adc_r2_src);
  778. static const struct snd_kcontrol_new rt5640_mono_adc_r2_mux =
  779. SOC_DAPM_ENUM("Mono ADC2 right source", rt5640_mono_adc_r2_enum);
  780. /* DAC2 channel source */
  781. static const char * const rt5640_dac_l2_src[] = {
  782. "IF2", "Base L/R"
  783. };
  784. static int rt5640_dac_l2_values[] = {
  785. 0,
  786. 3,
  787. };
  788. static SOC_VALUE_ENUM_SINGLE_DECL(rt5640_dac_l2_enum,
  789. RT5640_DSP_PATH2, RT5640_DAC_L2_SEL_SFT,
  790. 0x3, rt5640_dac_l2_src, rt5640_dac_l2_values);
  791. static const struct snd_kcontrol_new rt5640_dac_l2_mux =
  792. SOC_DAPM_ENUM("DAC2 left channel source", rt5640_dac_l2_enum);
  793. static const char * const rt5640_dac_r2_src[] = {
  794. "IF2",
  795. };
  796. static int rt5640_dac_r2_values[] = {
  797. 0,
  798. };
  799. static SOC_VALUE_ENUM_SINGLE_DECL(rt5640_dac_r2_enum,
  800. RT5640_DSP_PATH2, RT5640_DAC_R2_SEL_SFT,
  801. 0x3, rt5640_dac_r2_src, rt5640_dac_r2_values);
  802. static const struct snd_kcontrol_new rt5640_dac_r2_mux =
  803. SOC_DAPM_ENUM("DAC2 right channel source", rt5640_dac_r2_enum);
  804. /* digital interface and iis interface map */
  805. static const char * const rt5640_dai_iis_map[] = {
  806. "1:1|2:2", "1:2|2:1", "1:1|2:1", "1:2|2:2"
  807. };
  808. static int rt5640_dai_iis_map_values[] = {
  809. 0,
  810. 5,
  811. 6,
  812. 7,
  813. };
  814. static SOC_VALUE_ENUM_SINGLE_DECL(rt5640_dai_iis_map_enum,
  815. RT5640_I2S1_SDP, RT5640_I2S_IF_SFT,
  816. 0x7, rt5640_dai_iis_map,
  817. rt5640_dai_iis_map_values);
  818. static const struct snd_kcontrol_new rt5640_dai_mux =
  819. SOC_DAPM_ENUM("DAI select", rt5640_dai_iis_map_enum);
  820. /* SDI select */
  821. static const char * const rt5640_sdi_sel[] = {
  822. "IF1", "IF2"
  823. };
  824. static SOC_ENUM_SINGLE_DECL(rt5640_sdi_sel_enum, RT5640_I2S2_SDP,
  825. RT5640_I2S2_SDI_SFT, rt5640_sdi_sel);
  826. static const struct snd_kcontrol_new rt5640_sdi_mux =
  827. SOC_DAPM_ENUM("SDI select", rt5640_sdi_sel_enum);
  828. static void hp_amp_power_on(struct snd_soc_codec *codec)
  829. {
  830. struct rt5640_priv *rt5640 = snd_soc_codec_get_drvdata(codec);
  831. /* depop parameters */
  832. regmap_update_bits(rt5640->regmap, RT5640_PR_BASE +
  833. RT5640_CHPUMP_INT_REG1, 0x0700, 0x0200);
  834. regmap_update_bits(rt5640->regmap, RT5640_DEPOP_M2,
  835. RT5640_DEPOP_MASK, RT5640_DEPOP_MAN);
  836. regmap_update_bits(rt5640->regmap, RT5640_DEPOP_M1,
  837. RT5640_HP_CP_MASK | RT5640_HP_SG_MASK | RT5640_HP_CB_MASK,
  838. RT5640_HP_CP_PU | RT5640_HP_SG_DIS | RT5640_HP_CB_PU);
  839. regmap_write(rt5640->regmap, RT5640_PR_BASE + RT5640_HP_DCC_INT1,
  840. 0x9f00);
  841. /* headphone amp power on */
  842. regmap_update_bits(rt5640->regmap, RT5640_PWR_ANLG1,
  843. RT5640_PWR_FV1 | RT5640_PWR_FV2, 0);
  844. regmap_update_bits(rt5640->regmap, RT5640_PWR_ANLG1,
  845. RT5640_PWR_HA,
  846. RT5640_PWR_HA);
  847. usleep_range(10000, 15000);
  848. regmap_update_bits(rt5640->regmap, RT5640_PWR_ANLG1,
  849. RT5640_PWR_FV1 | RT5640_PWR_FV2 ,
  850. RT5640_PWR_FV1 | RT5640_PWR_FV2);
  851. }
  852. static void rt5640_pmu_depop(struct snd_soc_codec *codec)
  853. {
  854. struct rt5640_priv *rt5640 = snd_soc_codec_get_drvdata(codec);
  855. regmap_update_bits(rt5640->regmap, RT5640_DEPOP_M2,
  856. RT5640_DEPOP_MASK | RT5640_DIG_DP_MASK,
  857. RT5640_DEPOP_AUTO | RT5640_DIG_DP_EN);
  858. regmap_update_bits(rt5640->regmap, RT5640_CHARGE_PUMP,
  859. RT5640_PM_HP_MASK, RT5640_PM_HP_HV);
  860. regmap_update_bits(rt5640->regmap, RT5640_DEPOP_M3,
  861. RT5640_CP_FQ1_MASK | RT5640_CP_FQ2_MASK | RT5640_CP_FQ3_MASK,
  862. (RT5640_CP_FQ_192_KHZ << RT5640_CP_FQ1_SFT) |
  863. (RT5640_CP_FQ_12_KHZ << RT5640_CP_FQ2_SFT) |
  864. (RT5640_CP_FQ_192_KHZ << RT5640_CP_FQ3_SFT));
  865. regmap_write(rt5640->regmap, RT5640_PR_BASE +
  866. RT5640_MAMP_INT_REG2, 0x1c00);
  867. regmap_update_bits(rt5640->regmap, RT5640_DEPOP_M1,
  868. RT5640_HP_CP_MASK | RT5640_HP_SG_MASK,
  869. RT5640_HP_CP_PD | RT5640_HP_SG_EN);
  870. regmap_update_bits(rt5640->regmap, RT5640_PR_BASE +
  871. RT5640_CHPUMP_INT_REG1, 0x0700, 0x0400);
  872. }
  873. static int rt5640_hp_event(struct snd_soc_dapm_widget *w,
  874. struct snd_kcontrol *kcontrol, int event)
  875. {
  876. struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
  877. struct rt5640_priv *rt5640 = snd_soc_codec_get_drvdata(codec);
  878. switch (event) {
  879. case SND_SOC_DAPM_POST_PMU:
  880. rt5640_pmu_depop(codec);
  881. rt5640->hp_mute = 0;
  882. break;
  883. case SND_SOC_DAPM_PRE_PMD:
  884. rt5640->hp_mute = 1;
  885. usleep_range(70000, 75000);
  886. break;
  887. default:
  888. return 0;
  889. }
  890. return 0;
  891. }
  892. static int rt5640_lout_event(struct snd_soc_dapm_widget *w,
  893. struct snd_kcontrol *kcontrol, int event)
  894. {
  895. struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
  896. switch (event) {
  897. case SND_SOC_DAPM_POST_PMU:
  898. hp_amp_power_on(codec);
  899. snd_soc_update_bits(codec, RT5640_PWR_ANLG1,
  900. RT5640_PWR_LM, RT5640_PWR_LM);
  901. snd_soc_update_bits(codec, RT5640_OUTPUT,
  902. RT5640_L_MUTE | RT5640_R_MUTE, 0);
  903. break;
  904. case SND_SOC_DAPM_PRE_PMD:
  905. snd_soc_update_bits(codec, RT5640_OUTPUT,
  906. RT5640_L_MUTE | RT5640_R_MUTE,
  907. RT5640_L_MUTE | RT5640_R_MUTE);
  908. snd_soc_update_bits(codec, RT5640_PWR_ANLG1,
  909. RT5640_PWR_LM, 0);
  910. break;
  911. default:
  912. return 0;
  913. }
  914. return 0;
  915. }
  916. static int rt5640_hp_power_event(struct snd_soc_dapm_widget *w,
  917. struct snd_kcontrol *kcontrol, int event)
  918. {
  919. struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
  920. switch (event) {
  921. case SND_SOC_DAPM_POST_PMU:
  922. hp_amp_power_on(codec);
  923. break;
  924. default:
  925. return 0;
  926. }
  927. return 0;
  928. }
  929. static int rt5640_hp_post_event(struct snd_soc_dapm_widget *w,
  930. struct snd_kcontrol *kcontrol, int event)
  931. {
  932. struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
  933. struct rt5640_priv *rt5640 = snd_soc_codec_get_drvdata(codec);
  934. switch (event) {
  935. case SND_SOC_DAPM_POST_PMU:
  936. if (!rt5640->hp_mute)
  937. usleep_range(80000, 85000);
  938. break;
  939. default:
  940. return 0;
  941. }
  942. return 0;
  943. }
  944. static const struct snd_soc_dapm_widget rt5640_dapm_widgets[] = {
  945. SND_SOC_DAPM_SUPPLY("PLL1", RT5640_PWR_ANLG2,
  946. RT5640_PWR_PLL_BIT, 0, NULL, 0),
  947. /* ASRC */
  948. SND_SOC_DAPM_SUPPLY_S("Stereo Filter ASRC", 1, RT5640_ASRC_1,
  949. 15, 0, NULL, 0),
  950. SND_SOC_DAPM_SUPPLY_S("I2S2 Filter ASRC", 1, RT5640_ASRC_1,
  951. 12, 0, NULL, 0),
  952. SND_SOC_DAPM_SUPPLY_S("I2S2 ASRC", 1, RT5640_ASRC_1,
  953. 11, 0, NULL, 0),
  954. SND_SOC_DAPM_SUPPLY_S("DMIC1 ASRC", 1, RT5640_ASRC_1,
  955. 9, 0, NULL, 0),
  956. SND_SOC_DAPM_SUPPLY_S("DMIC2 ASRC", 1, RT5640_ASRC_1,
  957. 8, 0, NULL, 0),
  958. /* Input Side */
  959. /* micbias */
  960. SND_SOC_DAPM_SUPPLY("LDO2", RT5640_PWR_ANLG1,
  961. RT5640_PWR_LDO2_BIT, 0, NULL, 0),
  962. SND_SOC_DAPM_SUPPLY("MICBIAS1", RT5640_PWR_ANLG2,
  963. RT5640_PWR_MB1_BIT, 0, NULL, 0),
  964. /* Input Lines */
  965. SND_SOC_DAPM_INPUT("DMIC1"),
  966. SND_SOC_DAPM_INPUT("DMIC2"),
  967. SND_SOC_DAPM_INPUT("IN1P"),
  968. SND_SOC_DAPM_INPUT("IN1N"),
  969. SND_SOC_DAPM_INPUT("IN2P"),
  970. SND_SOC_DAPM_INPUT("IN2N"),
  971. SND_SOC_DAPM_INPUT("IN3P"),
  972. SND_SOC_DAPM_INPUT("IN3N"),
  973. SND_SOC_DAPM_PGA("DMIC L1", SND_SOC_NOPM, 0, 0, NULL, 0),
  974. SND_SOC_DAPM_PGA("DMIC R1", SND_SOC_NOPM, 0, 0, NULL, 0),
  975. SND_SOC_DAPM_PGA("DMIC L2", SND_SOC_NOPM, 0, 0, NULL, 0),
  976. SND_SOC_DAPM_PGA("DMIC R2", SND_SOC_NOPM, 0, 0, NULL, 0),
  977. SND_SOC_DAPM_SUPPLY("DMIC CLK", SND_SOC_NOPM, 0, 0,
  978. set_dmic_clk, SND_SOC_DAPM_PRE_PMU),
  979. SND_SOC_DAPM_SUPPLY("DMIC1 Power", RT5640_DMIC, RT5640_DMIC_1_EN_SFT, 0,
  980. NULL, 0),
  981. SND_SOC_DAPM_SUPPLY("DMIC2 Power", RT5640_DMIC, RT5640_DMIC_2_EN_SFT, 0,
  982. NULL, 0),
  983. /* Boost */
  984. SND_SOC_DAPM_PGA("BST1", RT5640_PWR_ANLG2,
  985. RT5640_PWR_BST1_BIT, 0, NULL, 0),
  986. SND_SOC_DAPM_PGA("BST2", RT5640_PWR_ANLG2,
  987. RT5640_PWR_BST4_BIT, 0, NULL, 0),
  988. SND_SOC_DAPM_PGA("BST3", RT5640_PWR_ANLG2,
  989. RT5640_PWR_BST2_BIT, 0, NULL, 0),
  990. /* Input Volume */
  991. SND_SOC_DAPM_PGA("INL VOL", RT5640_PWR_VOL,
  992. RT5640_PWR_IN_L_BIT, 0, NULL, 0),
  993. SND_SOC_DAPM_PGA("INR VOL", RT5640_PWR_VOL,
  994. RT5640_PWR_IN_R_BIT, 0, NULL, 0),
  995. /* REC Mixer */
  996. SND_SOC_DAPM_MIXER("RECMIXL", RT5640_PWR_MIXER, RT5640_PWR_RM_L_BIT, 0,
  997. rt5640_rec_l_mix, ARRAY_SIZE(rt5640_rec_l_mix)),
  998. SND_SOC_DAPM_MIXER("RECMIXR", RT5640_PWR_MIXER, RT5640_PWR_RM_R_BIT, 0,
  999. rt5640_rec_r_mix, ARRAY_SIZE(rt5640_rec_r_mix)),
  1000. /* ADCs */
  1001. SND_SOC_DAPM_ADC("ADC L", NULL, RT5640_PWR_DIG1,
  1002. RT5640_PWR_ADC_L_BIT, 0),
  1003. SND_SOC_DAPM_ADC("ADC R", NULL, RT5640_PWR_DIG1,
  1004. RT5640_PWR_ADC_R_BIT, 0),
  1005. /* ADC Mux */
  1006. SND_SOC_DAPM_MUX("Stereo ADC L2 Mux", SND_SOC_NOPM, 0, 0,
  1007. &rt5640_sto_adc_2_mux),
  1008. SND_SOC_DAPM_MUX("Stereo ADC R2 Mux", SND_SOC_NOPM, 0, 0,
  1009. &rt5640_sto_adc_2_mux),
  1010. SND_SOC_DAPM_MUX("Stereo ADC L1 Mux", SND_SOC_NOPM, 0, 0,
  1011. &rt5640_sto_adc_1_mux),
  1012. SND_SOC_DAPM_MUX("Stereo ADC R1 Mux", SND_SOC_NOPM, 0, 0,
  1013. &rt5640_sto_adc_1_mux),
  1014. SND_SOC_DAPM_MUX("Mono ADC L2 Mux", SND_SOC_NOPM, 0, 0,
  1015. &rt5640_mono_adc_l2_mux),
  1016. SND_SOC_DAPM_MUX("Mono ADC L1 Mux", SND_SOC_NOPM, 0, 0,
  1017. &rt5640_mono_adc_l1_mux),
  1018. SND_SOC_DAPM_MUX("Mono ADC R1 Mux", SND_SOC_NOPM, 0, 0,
  1019. &rt5640_mono_adc_r1_mux),
  1020. SND_SOC_DAPM_MUX("Mono ADC R2 Mux", SND_SOC_NOPM, 0, 0,
  1021. &rt5640_mono_adc_r2_mux),
  1022. /* ADC Mixer */
  1023. SND_SOC_DAPM_SUPPLY("Stereo Filter", RT5640_PWR_DIG2,
  1024. RT5640_PWR_ADC_SF_BIT, 0, NULL, 0),
  1025. SND_SOC_DAPM_MIXER("Stereo ADC MIXL", SND_SOC_NOPM, 0, 0,
  1026. rt5640_sto_adc_l_mix, ARRAY_SIZE(rt5640_sto_adc_l_mix)),
  1027. SND_SOC_DAPM_MIXER("Stereo ADC MIXR", SND_SOC_NOPM, 0, 0,
  1028. rt5640_sto_adc_r_mix, ARRAY_SIZE(rt5640_sto_adc_r_mix)),
  1029. SND_SOC_DAPM_SUPPLY("Mono Left Filter", RT5640_PWR_DIG2,
  1030. RT5640_PWR_ADC_MF_L_BIT, 0, NULL, 0),
  1031. SND_SOC_DAPM_MIXER("Mono ADC MIXL", SND_SOC_NOPM, 0, 0,
  1032. rt5640_mono_adc_l_mix, ARRAY_SIZE(rt5640_mono_adc_l_mix)),
  1033. SND_SOC_DAPM_SUPPLY("Mono Right Filter", RT5640_PWR_DIG2,
  1034. RT5640_PWR_ADC_MF_R_BIT, 0, NULL, 0),
  1035. SND_SOC_DAPM_MIXER("Mono ADC MIXR", SND_SOC_NOPM, 0, 0,
  1036. rt5640_mono_adc_r_mix, ARRAY_SIZE(rt5640_mono_adc_r_mix)),
  1037. /* Digital Interface */
  1038. SND_SOC_DAPM_SUPPLY("I2S1", RT5640_PWR_DIG1,
  1039. RT5640_PWR_I2S1_BIT, 0, NULL, 0),
  1040. SND_SOC_DAPM_PGA("IF1 DAC", SND_SOC_NOPM, 0, 0, NULL, 0),
  1041. SND_SOC_DAPM_PGA("IF1 DAC L", SND_SOC_NOPM, 0, 0, NULL, 0),
  1042. SND_SOC_DAPM_PGA("IF1 DAC R", SND_SOC_NOPM, 0, 0, NULL, 0),
  1043. SND_SOC_DAPM_PGA("IF1 ADC", SND_SOC_NOPM, 0, 0, NULL, 0),
  1044. SND_SOC_DAPM_PGA("IF1 ADC L", SND_SOC_NOPM, 0, 0, NULL, 0),
  1045. SND_SOC_DAPM_PGA("IF1 ADC R", SND_SOC_NOPM, 0, 0, NULL, 0),
  1046. SND_SOC_DAPM_SUPPLY("I2S2", RT5640_PWR_DIG1,
  1047. RT5640_PWR_I2S2_BIT, 0, NULL, 0),
  1048. SND_SOC_DAPM_PGA("IF2 DAC", SND_SOC_NOPM, 0, 0, NULL, 0),
  1049. SND_SOC_DAPM_PGA("IF2 DAC L", SND_SOC_NOPM, 0, 0, NULL, 0),
  1050. SND_SOC_DAPM_PGA("IF2 DAC R", SND_SOC_NOPM, 0, 0, NULL, 0),
  1051. SND_SOC_DAPM_PGA("IF2 ADC", SND_SOC_NOPM, 0, 0, NULL, 0),
  1052. SND_SOC_DAPM_PGA("IF2 ADC L", SND_SOC_NOPM, 0, 0, NULL, 0),
  1053. SND_SOC_DAPM_PGA("IF2 ADC R", SND_SOC_NOPM, 0, 0, NULL, 0),
  1054. /* Digital Interface Select */
  1055. SND_SOC_DAPM_MUX("DAI1 RX Mux", SND_SOC_NOPM, 0, 0, &rt5640_dai_mux),
  1056. SND_SOC_DAPM_MUX("DAI1 TX Mux", SND_SOC_NOPM, 0, 0, &rt5640_dai_mux),
  1057. SND_SOC_DAPM_MUX("DAI1 IF1 Mux", SND_SOC_NOPM, 0, 0, &rt5640_dai_mux),
  1058. SND_SOC_DAPM_MUX("DAI1 IF2 Mux", SND_SOC_NOPM, 0, 0, &rt5640_dai_mux),
  1059. SND_SOC_DAPM_MUX("SDI1 TX Mux", SND_SOC_NOPM, 0, 0, &rt5640_sdi_mux),
  1060. SND_SOC_DAPM_MUX("DAI2 RX Mux", SND_SOC_NOPM, 0, 0, &rt5640_dai_mux),
  1061. SND_SOC_DAPM_MUX("DAI2 TX Mux", SND_SOC_NOPM, 0, 0, &rt5640_dai_mux),
  1062. SND_SOC_DAPM_MUX("DAI2 IF1 Mux", SND_SOC_NOPM, 0, 0, &rt5640_dai_mux),
  1063. SND_SOC_DAPM_MUX("DAI2 IF2 Mux", SND_SOC_NOPM, 0, 0, &rt5640_dai_mux),
  1064. SND_SOC_DAPM_MUX("SDI2 TX Mux", SND_SOC_NOPM, 0, 0, &rt5640_sdi_mux),
  1065. /* Audio Interface */
  1066. SND_SOC_DAPM_AIF_IN("AIF1RX", "AIF1 Playback", 0, SND_SOC_NOPM, 0, 0),
  1067. SND_SOC_DAPM_AIF_OUT("AIF1TX", "AIF1 Capture", 0, SND_SOC_NOPM, 0, 0),
  1068. SND_SOC_DAPM_AIF_IN("AIF2RX", "AIF2 Playback", 0, SND_SOC_NOPM, 0, 0),
  1069. SND_SOC_DAPM_AIF_OUT("AIF2TX", "AIF2 Capture", 0, SND_SOC_NOPM, 0, 0),
  1070. /* Output Side */
  1071. /* DAC mixer before sound effect */
  1072. SND_SOC_DAPM_MIXER("DAC MIXL", SND_SOC_NOPM, 0, 0,
  1073. rt5640_dac_l_mix, ARRAY_SIZE(rt5640_dac_l_mix)),
  1074. SND_SOC_DAPM_MIXER("DAC MIXR", SND_SOC_NOPM, 0, 0,
  1075. rt5640_dac_r_mix, ARRAY_SIZE(rt5640_dac_r_mix)),
  1076. /* DAC Mixer */
  1077. SND_SOC_DAPM_MIXER("Mono DAC MIXL", SND_SOC_NOPM, 0, 0,
  1078. rt5640_mono_dac_l_mix, ARRAY_SIZE(rt5640_mono_dac_l_mix)),
  1079. SND_SOC_DAPM_MIXER("Mono DAC MIXR", SND_SOC_NOPM, 0, 0,
  1080. rt5640_mono_dac_r_mix, ARRAY_SIZE(rt5640_mono_dac_r_mix)),
  1081. SND_SOC_DAPM_MIXER("DIG MIXL", SND_SOC_NOPM, 0, 0,
  1082. rt5640_dig_l_mix, ARRAY_SIZE(rt5640_dig_l_mix)),
  1083. SND_SOC_DAPM_MIXER("DIG MIXR", SND_SOC_NOPM, 0, 0,
  1084. rt5640_dig_r_mix, ARRAY_SIZE(rt5640_dig_r_mix)),
  1085. /* DACs */
  1086. SND_SOC_DAPM_DAC("DAC L1", NULL, SND_SOC_NOPM,
  1087. 0, 0),
  1088. SND_SOC_DAPM_DAC("DAC R1", NULL, SND_SOC_NOPM,
  1089. 0, 0),
  1090. SND_SOC_DAPM_SUPPLY("DAC L1 Power", RT5640_PWR_DIG1,
  1091. RT5640_PWR_DAC_L1_BIT, 0, NULL, 0),
  1092. SND_SOC_DAPM_SUPPLY("DAC R1 Power", RT5640_PWR_DIG1,
  1093. RT5640_PWR_DAC_R1_BIT, 0, NULL, 0),
  1094. /* SPK/OUT Mixer */
  1095. SND_SOC_DAPM_MIXER("SPK MIXL", RT5640_PWR_MIXER, RT5640_PWR_SM_L_BIT,
  1096. 0, rt5640_spk_l_mix, ARRAY_SIZE(rt5640_spk_l_mix)),
  1097. SND_SOC_DAPM_MIXER("SPK MIXR", RT5640_PWR_MIXER, RT5640_PWR_SM_R_BIT,
  1098. 0, rt5640_spk_r_mix, ARRAY_SIZE(rt5640_spk_r_mix)),
  1099. /* Ouput Volume */
  1100. SND_SOC_DAPM_PGA("SPKVOL L", RT5640_PWR_VOL,
  1101. RT5640_PWR_SV_L_BIT, 0, NULL, 0),
  1102. SND_SOC_DAPM_PGA("SPKVOL R", RT5640_PWR_VOL,
  1103. RT5640_PWR_SV_R_BIT, 0, NULL, 0),
  1104. SND_SOC_DAPM_PGA("OUTVOL L", RT5640_PWR_VOL,
  1105. RT5640_PWR_OV_L_BIT, 0, NULL, 0),
  1106. SND_SOC_DAPM_PGA("OUTVOL R", RT5640_PWR_VOL,
  1107. RT5640_PWR_OV_R_BIT, 0, NULL, 0),
  1108. SND_SOC_DAPM_PGA("HPOVOL L", RT5640_PWR_VOL,
  1109. RT5640_PWR_HV_L_BIT, 0, NULL, 0),
  1110. SND_SOC_DAPM_PGA("HPOVOL R", RT5640_PWR_VOL,
  1111. RT5640_PWR_HV_R_BIT, 0, NULL, 0),
  1112. /* SPO/HPO/LOUT/Mono Mixer */
  1113. SND_SOC_DAPM_MIXER("SPOL MIX", SND_SOC_NOPM, 0,
  1114. 0, rt5640_spo_l_mix, ARRAY_SIZE(rt5640_spo_l_mix)),
  1115. SND_SOC_DAPM_MIXER("SPOR MIX", SND_SOC_NOPM, 0,
  1116. 0, rt5640_spo_r_mix, ARRAY_SIZE(rt5640_spo_r_mix)),
  1117. SND_SOC_DAPM_MIXER("LOUT MIX", SND_SOC_NOPM, 0, 0,
  1118. rt5640_lout_mix, ARRAY_SIZE(rt5640_lout_mix)),
  1119. SND_SOC_DAPM_SUPPLY_S("Improve HP Amp Drv", 1, SND_SOC_NOPM,
  1120. 0, 0, rt5640_hp_power_event, SND_SOC_DAPM_POST_PMU),
  1121. SND_SOC_DAPM_PGA_S("HP Amp", 1, SND_SOC_NOPM, 0, 0,
  1122. rt5640_hp_event,
  1123. SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMU),
  1124. SND_SOC_DAPM_PGA_S("LOUT amp", 1, SND_SOC_NOPM, 0, 0,
  1125. rt5640_lout_event,
  1126. SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMU),
  1127. SND_SOC_DAPM_SUPPLY("HP L Amp", RT5640_PWR_ANLG1,
  1128. RT5640_PWR_HP_L_BIT, 0, NULL, 0),
  1129. SND_SOC_DAPM_SUPPLY("HP R Amp", RT5640_PWR_ANLG1,
  1130. RT5640_PWR_HP_R_BIT, 0, NULL, 0),
  1131. SND_SOC_DAPM_SUPPLY("Improve SPK Amp Drv", RT5640_PWR_DIG1,
  1132. RT5640_PWR_CLS_D_BIT, 0, NULL, 0),
  1133. /* Output Switch */
  1134. SND_SOC_DAPM_SWITCH("Speaker L Playback", SND_SOC_NOPM, 0, 0,
  1135. &spk_l_enable_control),
  1136. SND_SOC_DAPM_SWITCH("Speaker R Playback", SND_SOC_NOPM, 0, 0,
  1137. &spk_r_enable_control),
  1138. SND_SOC_DAPM_SWITCH("HP L Playback", SND_SOC_NOPM, 0, 0,
  1139. &hp_l_enable_control),
  1140. SND_SOC_DAPM_SWITCH("HP R Playback", SND_SOC_NOPM, 0, 0,
  1141. &hp_r_enable_control),
  1142. SND_SOC_DAPM_POST("HP Post", rt5640_hp_post_event),
  1143. /* Output Lines */
  1144. SND_SOC_DAPM_OUTPUT("SPOLP"),
  1145. SND_SOC_DAPM_OUTPUT("SPOLN"),
  1146. SND_SOC_DAPM_OUTPUT("SPORP"),
  1147. SND_SOC_DAPM_OUTPUT("SPORN"),
  1148. SND_SOC_DAPM_OUTPUT("HPOL"),
  1149. SND_SOC_DAPM_OUTPUT("HPOR"),
  1150. SND_SOC_DAPM_OUTPUT("LOUTL"),
  1151. SND_SOC_DAPM_OUTPUT("LOUTR"),
  1152. };
  1153. static const struct snd_soc_dapm_widget rt5640_specific_dapm_widgets[] = {
  1154. /* Audio DSP */
  1155. SND_SOC_DAPM_PGA("Audio DSP", SND_SOC_NOPM, 0, 0, NULL, 0),
  1156. /* ANC */
  1157. SND_SOC_DAPM_PGA("ANC", SND_SOC_NOPM, 0, 0, NULL, 0),
  1158. /* DAC2 channel Mux */
  1159. SND_SOC_DAPM_MUX("DAC L2 Mux", SND_SOC_NOPM, 0, 0, &rt5640_dac_l2_mux),
  1160. SND_SOC_DAPM_MUX("DAC R2 Mux", SND_SOC_NOPM, 0, 0, &rt5640_dac_r2_mux),
  1161. SND_SOC_DAPM_MIXER("Stereo DAC MIXL", SND_SOC_NOPM, 0, 0,
  1162. rt5640_sto_dac_l_mix, ARRAY_SIZE(rt5640_sto_dac_l_mix)),
  1163. SND_SOC_DAPM_MIXER("Stereo DAC MIXR", SND_SOC_NOPM, 0, 0,
  1164. rt5640_sto_dac_r_mix, ARRAY_SIZE(rt5640_sto_dac_r_mix)),
  1165. SND_SOC_DAPM_DAC("DAC R2", NULL, SND_SOC_NOPM, 0,
  1166. 0),
  1167. SND_SOC_DAPM_DAC("DAC L2", NULL, SND_SOC_NOPM, 0,
  1168. 0),
  1169. SND_SOC_DAPM_MIXER("OUT MIXL", RT5640_PWR_MIXER, RT5640_PWR_OM_L_BIT,
  1170. 0, rt5640_out_l_mix, ARRAY_SIZE(rt5640_out_l_mix)),
  1171. SND_SOC_DAPM_MIXER("OUT MIXR", RT5640_PWR_MIXER, RT5640_PWR_OM_R_BIT,
  1172. 0, rt5640_out_r_mix, ARRAY_SIZE(rt5640_out_r_mix)),
  1173. SND_SOC_DAPM_MIXER("HPO MIX L", SND_SOC_NOPM, 0, 0,
  1174. rt5640_hpo_mix, ARRAY_SIZE(rt5640_hpo_mix)),
  1175. SND_SOC_DAPM_MIXER("HPO MIX R", SND_SOC_NOPM, 0, 0,
  1176. rt5640_hpo_mix, ARRAY_SIZE(rt5640_hpo_mix)),
  1177. SND_SOC_DAPM_MIXER("Mono MIX", RT5640_PWR_ANLG1, RT5640_PWR_MM_BIT, 0,
  1178. rt5640_mono_mix, ARRAY_SIZE(rt5640_mono_mix)),
  1179. SND_SOC_DAPM_SUPPLY("Improve MONO Amp Drv", RT5640_PWR_ANLG1,
  1180. RT5640_PWR_MA_BIT, 0, NULL, 0),
  1181. SND_SOC_DAPM_SUPPLY("DAC L2 Power", RT5640_PWR_DIG1,
  1182. RT5640_PWR_DAC_L2_BIT, 0, NULL, 0),
  1183. SND_SOC_DAPM_SUPPLY("DAC R2 Power", RT5640_PWR_DIG1,
  1184. RT5640_PWR_DAC_R2_BIT, 0, NULL, 0),
  1185. SND_SOC_DAPM_OUTPUT("MONOP"),
  1186. SND_SOC_DAPM_OUTPUT("MONON"),
  1187. };
  1188. static const struct snd_soc_dapm_widget rt5639_specific_dapm_widgets[] = {
  1189. SND_SOC_DAPM_MIXER("Stereo DAC MIXL", SND_SOC_NOPM, 0, 0,
  1190. rt5639_sto_dac_l_mix, ARRAY_SIZE(rt5639_sto_dac_l_mix)),
  1191. SND_SOC_DAPM_MIXER("Stereo DAC MIXR", SND_SOC_NOPM, 0, 0,
  1192. rt5639_sto_dac_r_mix, ARRAY_SIZE(rt5639_sto_dac_r_mix)),
  1193. SND_SOC_DAPM_MIXER("OUT MIXL", RT5640_PWR_MIXER, RT5640_PWR_OM_L_BIT,
  1194. 0, rt5639_out_l_mix, ARRAY_SIZE(rt5639_out_l_mix)),
  1195. SND_SOC_DAPM_MIXER("OUT MIXR", RT5640_PWR_MIXER, RT5640_PWR_OM_R_BIT,
  1196. 0, rt5639_out_r_mix, ARRAY_SIZE(rt5639_out_r_mix)),
  1197. SND_SOC_DAPM_MIXER("HPO MIX L", SND_SOC_NOPM, 0, 0,
  1198. rt5639_hpo_mix, ARRAY_SIZE(rt5639_hpo_mix)),
  1199. SND_SOC_DAPM_MIXER("HPO MIX R", SND_SOC_NOPM, 0, 0,
  1200. rt5639_hpo_mix, ARRAY_SIZE(rt5639_hpo_mix)),
  1201. };
  1202. static const struct snd_soc_dapm_route rt5640_dapm_routes[] = {
  1203. { "I2S1", NULL, "Stereo Filter ASRC", is_using_asrc },
  1204. { "I2S2", NULL, "I2S2 ASRC", is_using_asrc },
  1205. { "I2S2", NULL, "I2S2 Filter ASRC", is_using_asrc },
  1206. { "DMIC1", NULL, "DMIC1 ASRC", is_using_asrc },
  1207. { "DMIC2", NULL, "DMIC2 ASRC", is_using_asrc },
  1208. {"IN1P", NULL, "LDO2"},
  1209. {"IN2P", NULL, "LDO2"},
  1210. {"IN3P", NULL, "LDO2"},
  1211. {"DMIC L1", NULL, "DMIC1"},
  1212. {"DMIC R1", NULL, "DMIC1"},
  1213. {"DMIC L2", NULL, "DMIC2"},
  1214. {"DMIC R2", NULL, "DMIC2"},
  1215. {"BST1", NULL, "IN1P"},
  1216. {"BST1", NULL, "IN1N"},
  1217. {"BST2", NULL, "IN2P"},
  1218. {"BST2", NULL, "IN2N"},
  1219. {"BST3", NULL, "IN3P"},
  1220. {"BST3", NULL, "IN3N"},
  1221. {"INL VOL", NULL, "IN2P"},
  1222. {"INR VOL", NULL, "IN2N"},
  1223. {"RECMIXL", "HPOL Switch", "HPOL"},
  1224. {"RECMIXL", "INL Switch", "INL VOL"},
  1225. {"RECMIXL", "BST3 Switch", "BST3"},
  1226. {"RECMIXL", "BST2 Switch", "BST2"},
  1227. {"RECMIXL", "BST1 Switch", "BST1"},
  1228. {"RECMIXL", "OUT MIXL Switch", "OUT MIXL"},
  1229. {"RECMIXR", "HPOR Switch", "HPOR"},
  1230. {"RECMIXR", "INR Switch", "INR VOL"},
  1231. {"RECMIXR", "BST3 Switch", "BST3"},
  1232. {"RECMIXR", "BST2 Switch", "BST2"},
  1233. {"RECMIXR", "BST1 Switch", "BST1"},
  1234. {"RECMIXR", "OUT MIXR Switch", "OUT MIXR"},
  1235. {"ADC L", NULL, "RECMIXL"},
  1236. {"ADC R", NULL, "RECMIXR"},
  1237. {"DMIC L1", NULL, "DMIC CLK"},
  1238. {"DMIC L1", NULL, "DMIC1 Power"},
  1239. {"DMIC R1", NULL, "DMIC CLK"},
  1240. {"DMIC R1", NULL, "DMIC1 Power"},
  1241. {"DMIC L2", NULL, "DMIC CLK"},
  1242. {"DMIC L2", NULL, "DMIC2 Power"},
  1243. {"DMIC R2", NULL, "DMIC CLK"},
  1244. {"DMIC R2", NULL, "DMIC2 Power"},
  1245. {"Stereo ADC L2 Mux", "DMIC1", "DMIC L1"},
  1246. {"Stereo ADC L2 Mux", "DMIC2", "DMIC L2"},
  1247. {"Stereo ADC L2 Mux", "DIG MIX", "DIG MIXL"},
  1248. {"Stereo ADC L1 Mux", "ADC", "ADC L"},
  1249. {"Stereo ADC L1 Mux", "DIG MIX", "DIG MIXL"},
  1250. {"Stereo ADC R1 Mux", "ADC", "ADC R"},
  1251. {"Stereo ADC R1 Mux", "DIG MIX", "DIG MIXR"},
  1252. {"Stereo ADC R2 Mux", "DMIC1", "DMIC R1"},
  1253. {"Stereo ADC R2 Mux", "DMIC2", "DMIC R2"},
  1254. {"Stereo ADC R2 Mux", "DIG MIX", "DIG MIXR"},
  1255. {"Mono ADC L2 Mux", "DMIC L1", "DMIC L1"},
  1256. {"Mono ADC L2 Mux", "DMIC L2", "DMIC L2"},
  1257. {"Mono ADC L2 Mux", "Mono DAC MIXL", "Mono DAC MIXL"},
  1258. {"Mono ADC L1 Mux", "Mono DAC MIXL", "Mono DAC MIXL"},
  1259. {"Mono ADC L1 Mux", "ADCL", "ADC L"},
  1260. {"Mono ADC R1 Mux", "Mono DAC MIXR", "Mono DAC MIXR"},
  1261. {"Mono ADC R1 Mux", "ADCR", "ADC R"},
  1262. {"Mono ADC R2 Mux", "DMIC R1", "DMIC R1"},
  1263. {"Mono ADC R2 Mux", "DMIC R2", "DMIC R2"},
  1264. {"Mono ADC R2 Mux", "Mono DAC MIXR", "Mono DAC MIXR"},
  1265. {"Stereo ADC MIXL", "ADC1 Switch", "Stereo ADC L1 Mux"},
  1266. {"Stereo ADC MIXL", "ADC2 Switch", "Stereo ADC L2 Mux"},
  1267. {"Stereo ADC MIXL", NULL, "Stereo Filter"},
  1268. {"Stereo Filter", NULL, "PLL1", is_sys_clk_from_pll},
  1269. {"Stereo ADC MIXR", "ADC1 Switch", "Stereo ADC R1 Mux"},
  1270. {"Stereo ADC MIXR", "ADC2 Switch", "Stereo ADC R2 Mux"},
  1271. {"Stereo ADC MIXR", NULL, "Stereo Filter"},
  1272. {"Stereo Filter", NULL, "PLL1", is_sys_clk_from_pll},
  1273. {"Mono ADC MIXL", "ADC1 Switch", "Mono ADC L1 Mux"},
  1274. {"Mono ADC MIXL", "ADC2 Switch", "Mono ADC L2 Mux"},
  1275. {"Mono ADC MIXL", NULL, "Mono Left Filter"},
  1276. {"Mono Left Filter", NULL, "PLL1", is_sys_clk_from_pll},
  1277. {"Mono ADC MIXR", "ADC1 Switch", "Mono ADC R1 Mux"},
  1278. {"Mono ADC MIXR", "ADC2 Switch", "Mono ADC R2 Mux"},
  1279. {"Mono ADC MIXR", NULL, "Mono Right Filter"},
  1280. {"Mono Right Filter", NULL, "PLL1", is_sys_clk_from_pll},
  1281. {"IF2 ADC L", NULL, "Mono ADC MIXL"},
  1282. {"IF2 ADC R", NULL, "Mono ADC MIXR"},
  1283. {"IF1 ADC L", NULL, "Stereo ADC MIXL"},
  1284. {"IF1 ADC R", NULL, "Stereo ADC MIXR"},
  1285. {"IF1 ADC", NULL, "I2S1"},
  1286. {"IF1 ADC", NULL, "IF1 ADC L"},
  1287. {"IF1 ADC", NULL, "IF1 ADC R"},
  1288. {"IF2 ADC", NULL, "I2S2"},
  1289. {"IF2 ADC", NULL, "IF2 ADC L"},
  1290. {"IF2 ADC", NULL, "IF2 ADC R"},
  1291. {"DAI1 TX Mux", "1:1|2:2", "IF1 ADC"},
  1292. {"DAI1 TX Mux", "1:2|2:1", "IF2 ADC"},
  1293. {"DAI1 IF1 Mux", "1:1|2:1", "IF1 ADC"},
  1294. {"DAI1 IF2 Mux", "1:1|2:1", "IF2 ADC"},
  1295. {"SDI1 TX Mux", "IF1", "DAI1 IF1 Mux"},
  1296. {"SDI1 TX Mux", "IF2", "DAI1 IF2 Mux"},
  1297. {"DAI2 TX Mux", "1:2|2:1", "IF1 ADC"},
  1298. {"DAI2 TX Mux", "1:1|2:2", "IF2 ADC"},
  1299. {"DAI2 IF1 Mux", "1:2|2:2", "IF1 ADC"},
  1300. {"DAI2 IF2 Mux", "1:2|2:2", "IF2 ADC"},
  1301. {"SDI2 TX Mux", "IF1", "DAI2 IF1 Mux"},
  1302. {"SDI2 TX Mux", "IF2", "DAI2 IF2 Mux"},
  1303. {"AIF1TX", NULL, "DAI1 TX Mux"},
  1304. {"AIF1TX", NULL, "SDI1 TX Mux"},
  1305. {"AIF2TX", NULL, "DAI2 TX Mux"},
  1306. {"AIF2TX", NULL, "SDI2 TX Mux"},
  1307. {"DAI1 RX Mux", "1:1|2:2", "AIF1RX"},
  1308. {"DAI1 RX Mux", "1:1|2:1", "AIF1RX"},
  1309. {"DAI1 RX Mux", "1:2|2:1", "AIF2RX"},
  1310. {"DAI1 RX Mux", "1:2|2:2", "AIF2RX"},
  1311. {"DAI2 RX Mux", "1:2|2:1", "AIF1RX"},
  1312. {"DAI2 RX Mux", "1:1|2:1", "AIF1RX"},
  1313. {"DAI2 RX Mux", "1:1|2:2", "AIF2RX"},
  1314. {"DAI2 RX Mux", "1:2|2:2", "AIF2RX"},
  1315. {"IF1 DAC", NULL, "I2S1"},
  1316. {"IF1 DAC", NULL, "DAI1 RX Mux"},
  1317. {"IF2 DAC", NULL, "I2S2"},
  1318. {"IF2 DAC", NULL, "DAI2 RX Mux"},
  1319. {"IF1 DAC L", NULL, "IF1 DAC"},
  1320. {"IF1 DAC R", NULL, "IF1 DAC"},
  1321. {"IF2 DAC L", NULL, "IF2 DAC"},
  1322. {"IF2 DAC R", NULL, "IF2 DAC"},
  1323. {"DAC MIXL", "Stereo ADC Switch", "Stereo ADC MIXL"},
  1324. {"DAC MIXL", "INF1 Switch", "IF1 DAC L"},
  1325. {"DAC MIXL", NULL, "DAC L1 Power"},
  1326. {"DAC MIXR", "Stereo ADC Switch", "Stereo ADC MIXR"},
  1327. {"DAC MIXR", "INF1 Switch", "IF1 DAC R"},
  1328. {"DAC MIXR", NULL, "DAC R1 Power"},
  1329. {"Stereo DAC MIXL", "DAC L1 Switch", "DAC MIXL"},
  1330. {"Stereo DAC MIXR", "DAC R1 Switch", "DAC MIXR"},
  1331. {"Mono DAC MIXL", "DAC L1 Switch", "DAC MIXL"},
  1332. {"Mono DAC MIXR", "DAC R1 Switch", "DAC MIXR"},
  1333. {"DIG MIXL", "DAC L1 Switch", "DAC MIXL"},
  1334. {"DIG MIXR", "DAC R1 Switch", "DAC MIXR"},
  1335. {"DAC L1", NULL, "Stereo DAC MIXL"},
  1336. {"DAC L1", NULL, "PLL1", is_sys_clk_from_pll},
  1337. {"DAC L1", NULL, "DAC L1 Power"},
  1338. {"DAC R1", NULL, "Stereo DAC MIXR"},
  1339. {"DAC R1", NULL, "PLL1", is_sys_clk_from_pll},
  1340. {"DAC R1", NULL, "DAC R1 Power"},
  1341. {"SPK MIXL", "REC MIXL Switch", "RECMIXL"},
  1342. {"SPK MIXL", "INL Switch", "INL VOL"},
  1343. {"SPK MIXL", "DAC L1 Switch", "DAC L1"},
  1344. {"SPK MIXL", "OUT MIXL Switch", "OUT MIXL"},
  1345. {"SPK MIXR", "REC MIXR Switch", "RECMIXR"},
  1346. {"SPK MIXR", "INR Switch", "INR VOL"},
  1347. {"SPK MIXR", "DAC R1 Switch", "DAC R1"},
  1348. {"SPK MIXR", "OUT MIXR Switch", "OUT MIXR"},
  1349. {"OUT MIXL", "BST1 Switch", "BST1"},
  1350. {"OUT MIXL", "INL Switch", "INL VOL"},
  1351. {"OUT MIXL", "REC MIXL Switch", "RECMIXL"},
  1352. {"OUT MIXL", "DAC L1 Switch", "DAC L1"},
  1353. {"OUT MIXR", "BST2 Switch", "BST2"},
  1354. {"OUT MIXR", "BST1 Switch", "BST1"},
  1355. {"OUT MIXR", "INR Switch", "INR VOL"},
  1356. {"OUT MIXR", "REC MIXR Switch", "RECMIXR"},
  1357. {"OUT MIXR", "DAC R1 Switch", "DAC R1"},
  1358. {"SPKVOL L", NULL, "SPK MIXL"},
  1359. {"SPKVOL R", NULL, "SPK MIXR"},
  1360. {"HPOVOL L", NULL, "OUT MIXL"},
  1361. {"HPOVOL R", NULL, "OUT MIXR"},
  1362. {"OUTVOL L", NULL, "OUT MIXL"},
  1363. {"OUTVOL R", NULL, "OUT MIXR"},
  1364. {"SPOL MIX", "DAC R1 Switch", "DAC R1"},
  1365. {"SPOL MIX", "DAC L1 Switch", "DAC L1"},
  1366. {"SPOL MIX", "SPKVOL R Switch", "SPKVOL R"},
  1367. {"SPOL MIX", "SPKVOL L Switch", "SPKVOL L"},
  1368. {"SPOL MIX", "BST1 Switch", "BST1"},
  1369. {"SPOR MIX", "DAC R1 Switch", "DAC R1"},
  1370. {"SPOR MIX", "SPKVOL R Switch", "SPKVOL R"},
  1371. {"SPOR MIX", "BST1 Switch", "BST1"},
  1372. {"HPO MIX L", "HPO MIX DAC1 Switch", "DAC L1"},
  1373. {"HPO MIX L", "HPO MIX HPVOL Switch", "HPOVOL L"},
  1374. {"HPO MIX L", NULL, "HP L Amp"},
  1375. {"HPO MIX R", "HPO MIX DAC1 Switch", "DAC R1"},
  1376. {"HPO MIX R", "HPO MIX HPVOL Switch", "HPOVOL R"},
  1377. {"HPO MIX R", NULL, "HP R Amp"},
  1378. {"LOUT MIX", "DAC L1 Switch", "DAC L1"},
  1379. {"LOUT MIX", "DAC R1 Switch", "DAC R1"},
  1380. {"LOUT MIX", "OUTVOL L Switch", "OUTVOL L"},
  1381. {"LOUT MIX", "OUTVOL R Switch", "OUTVOL R"},
  1382. {"HP Amp", NULL, "HPO MIX L"},
  1383. {"HP Amp", NULL, "HPO MIX R"},
  1384. {"Speaker L Playback", "Switch", "SPOL MIX"},
  1385. {"Speaker R Playback", "Switch", "SPOR MIX"},
  1386. {"SPOLP", NULL, "Speaker L Playback"},
  1387. {"SPOLN", NULL, "Speaker L Playback"},
  1388. {"SPORP", NULL, "Speaker R Playback"},
  1389. {"SPORN", NULL, "Speaker R Playback"},
  1390. {"SPOLP", NULL, "Improve SPK Amp Drv"},
  1391. {"SPOLN", NULL, "Improve SPK Amp Drv"},
  1392. {"SPORP", NULL, "Improve SPK Amp Drv"},
  1393. {"SPORN", NULL, "Improve SPK Amp Drv"},
  1394. {"HPOL", NULL, "Improve HP Amp Drv"},
  1395. {"HPOR", NULL, "Improve HP Amp Drv"},
  1396. {"HP L Playback", "Switch", "HP Amp"},
  1397. {"HP R Playback", "Switch", "HP Amp"},
  1398. {"HPOL", NULL, "HP L Playback"},
  1399. {"HPOR", NULL, "HP R Playback"},
  1400. {"LOUT amp", NULL, "LOUT MIX"},
  1401. {"LOUTL", NULL, "LOUT amp"},
  1402. {"LOUTR", NULL, "LOUT amp"},
  1403. };
  1404. static const struct snd_soc_dapm_route rt5640_specific_dapm_routes[] = {
  1405. {"ANC", NULL, "Stereo ADC MIXL"},
  1406. {"ANC", NULL, "Stereo ADC MIXR"},
  1407. {"Audio DSP", NULL, "DAC MIXL"},
  1408. {"Audio DSP", NULL, "DAC MIXR"},
  1409. {"DAC L2 Mux", "IF2", "IF2 DAC L"},
  1410. {"DAC L2 Mux", "Base L/R", "Audio DSP"},
  1411. {"DAC L2 Mux", NULL, "DAC L2 Power"},
  1412. {"DAC R2 Mux", "IF2", "IF2 DAC R"},
  1413. {"DAC R2 Mux", NULL, "DAC R2 Power"},
  1414. {"Stereo DAC MIXL", "DAC L2 Switch", "DAC L2 Mux"},
  1415. {"Stereo DAC MIXL", "ANC Switch", "ANC"},
  1416. {"Stereo DAC MIXR", "DAC R2 Switch", "DAC R2 Mux"},
  1417. {"Stereo DAC MIXR", "ANC Switch", "ANC"},
  1418. {"Mono DAC MIXL", "DAC L2 Switch", "DAC L2 Mux"},
  1419. {"Mono DAC MIXL", "DAC R2 Switch", "DAC R2 Mux"},
  1420. {"Mono DAC MIXR", "DAC R2 Switch", "DAC R2 Mux"},
  1421. {"Mono DAC MIXR", "DAC L2 Switch", "DAC L2 Mux"},
  1422. {"DIG MIXR", "DAC R2 Switch", "DAC R2 Mux"},
  1423. {"DIG MIXL", "DAC L2 Switch", "DAC L2 Mux"},
  1424. {"DAC L2", NULL, "Mono DAC MIXL"},
  1425. {"DAC L2", NULL, "PLL1", is_sys_clk_from_pll},
  1426. {"DAC L2", NULL, "DAC L2 Power"},
  1427. {"DAC R2", NULL, "Mono DAC MIXR"},
  1428. {"DAC R2", NULL, "PLL1", is_sys_clk_from_pll},
  1429. {"DAC R2", NULL, "DAC R2 Power"},
  1430. {"SPK MIXL", "DAC L2 Switch", "DAC L2"},
  1431. {"SPK MIXR", "DAC R2 Switch", "DAC R2"},
  1432. {"OUT MIXL", "SPK MIXL Switch", "SPK MIXL"},
  1433. {"OUT MIXR", "SPK MIXR Switch", "SPK MIXR"},
  1434. {"OUT MIXL", "DAC R2 Switch", "DAC R2"},
  1435. {"OUT MIXL", "DAC L2 Switch", "DAC L2"},
  1436. {"OUT MIXR", "DAC L2 Switch", "DAC L2"},
  1437. {"OUT MIXR", "DAC R2 Switch", "DAC R2"},
  1438. {"HPO MIX L", "HPO MIX DAC2 Switch", "DAC L2"},
  1439. {"HPO MIX R", "HPO MIX DAC2 Switch", "DAC R2"},
  1440. {"Mono MIX", "DAC R2 Switch", "DAC R2"},
  1441. {"Mono MIX", "DAC L2 Switch", "DAC L2"},
  1442. {"Mono MIX", "OUTVOL R Switch", "OUTVOL R"},
  1443. {"Mono MIX", "OUTVOL L Switch", "OUTVOL L"},
  1444. {"Mono MIX", "BST1 Switch", "BST1"},
  1445. {"MONOP", NULL, "Mono MIX"},
  1446. {"MONON", NULL, "Mono MIX"},
  1447. {"MONOP", NULL, "Improve MONO Amp Drv"},
  1448. };
  1449. static const struct snd_soc_dapm_route rt5639_specific_dapm_routes[] = {
  1450. {"Stereo DAC MIXL", "DAC L2 Switch", "IF2 DAC L"},
  1451. {"Stereo DAC MIXR", "DAC R2 Switch", "IF2 DAC R"},
  1452. {"Mono DAC MIXL", "DAC L2 Switch", "IF2 DAC L"},
  1453. {"Mono DAC MIXL", "DAC R2 Switch", "IF2 DAC R"},
  1454. {"Mono DAC MIXR", "DAC R2 Switch", "IF2 DAC R"},
  1455. {"Mono DAC MIXR", "DAC L2 Switch", "IF2 DAC L"},
  1456. {"DIG MIXL", "DAC L2 Switch", "IF2 DAC L"},
  1457. {"DIG MIXR", "DAC R2 Switch", "IF2 DAC R"},
  1458. {"IF2 DAC L", NULL, "DAC L2 Power"},
  1459. {"IF2 DAC R", NULL, "DAC R2 Power"},
  1460. };
  1461. static int get_sdp_info(struct snd_soc_codec *codec, int dai_id)
  1462. {
  1463. int ret = 0, val;
  1464. if (codec == NULL)
  1465. return -EINVAL;
  1466. val = snd_soc_read(codec, RT5640_I2S1_SDP);
  1467. val = (val & RT5640_I2S_IF_MASK) >> RT5640_I2S_IF_SFT;
  1468. switch (dai_id) {
  1469. case RT5640_AIF1:
  1470. switch (val) {
  1471. case RT5640_IF_123:
  1472. case RT5640_IF_132:
  1473. ret |= RT5640_U_IF1;
  1474. break;
  1475. case RT5640_IF_113:
  1476. ret |= RT5640_U_IF1;
  1477. case RT5640_IF_312:
  1478. case RT5640_IF_213:
  1479. ret |= RT5640_U_IF2;
  1480. break;
  1481. }
  1482. break;
  1483. case RT5640_AIF2:
  1484. switch (val) {
  1485. case RT5640_IF_231:
  1486. case RT5640_IF_213:
  1487. ret |= RT5640_U_IF1;
  1488. break;
  1489. case RT5640_IF_223:
  1490. ret |= RT5640_U_IF1;
  1491. case RT5640_IF_123:
  1492. case RT5640_IF_321:
  1493. ret |= RT5640_U_IF2;
  1494. break;
  1495. }
  1496. break;
  1497. default:
  1498. ret = -EINVAL;
  1499. break;
  1500. }
  1501. return ret;
  1502. }
  1503. static int rt5640_hw_params(struct snd_pcm_substream *substream,
  1504. struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
  1505. {
  1506. struct snd_soc_codec *codec = dai->codec;
  1507. struct rt5640_priv *rt5640 = snd_soc_codec_get_drvdata(codec);
  1508. unsigned int val_len = 0, val_clk, mask_clk;
  1509. int dai_sel, pre_div, bclk_ms, frame_size;
  1510. rt5640->lrck[dai->id] = params_rate(params);
  1511. pre_div = rl6231_get_clk_info(rt5640->sysclk, rt5640->lrck[dai->id]);
  1512. if (pre_div < 0) {
  1513. dev_err(codec->dev, "Unsupported clock setting %d for DAI %d\n",
  1514. rt5640->lrck[dai->id], dai->id);
  1515. return -EINVAL;
  1516. }
  1517. frame_size = snd_soc_params_to_frame_size(params);
  1518. if (frame_size < 0) {
  1519. dev_err(codec->dev, "Unsupported frame size: %d\n", frame_size);
  1520. return frame_size;
  1521. }
  1522. if (frame_size > 32)
  1523. bclk_ms = 1;
  1524. else
  1525. bclk_ms = 0;
  1526. rt5640->bclk[dai->id] = rt5640->lrck[dai->id] * (32 << bclk_ms);
  1527. dev_dbg(dai->dev, "bclk is %dHz and lrck is %dHz\n",
  1528. rt5640->bclk[dai->id], rt5640->lrck[dai->id]);
  1529. dev_dbg(dai->dev, "bclk_ms is %d and pre_div is %d for iis %d\n",
  1530. bclk_ms, pre_div, dai->id);
  1531. switch (params_width(params)) {
  1532. case 16:
  1533. break;
  1534. case 20:
  1535. val_len |= RT5640_I2S_DL_20;
  1536. break;
  1537. case 24:
  1538. val_len |= RT5640_I2S_DL_24;
  1539. break;
  1540. case 8:
  1541. val_len |= RT5640_I2S_DL_8;
  1542. break;
  1543. default:
  1544. return -EINVAL;
  1545. }
  1546. dai_sel = get_sdp_info(codec, dai->id);
  1547. if (dai_sel < 0) {
  1548. dev_err(codec->dev, "Failed to get sdp info: %d\n", dai_sel);
  1549. return -EINVAL;
  1550. }
  1551. if (dai_sel & RT5640_U_IF1) {
  1552. mask_clk = RT5640_I2S_BCLK_MS1_MASK | RT5640_I2S_PD1_MASK;
  1553. val_clk = bclk_ms << RT5640_I2S_BCLK_MS1_SFT |
  1554. pre_div << RT5640_I2S_PD1_SFT;
  1555. snd_soc_update_bits(codec, RT5640_I2S1_SDP,
  1556. RT5640_I2S_DL_MASK, val_len);
  1557. snd_soc_update_bits(codec, RT5640_ADDA_CLK1, mask_clk, val_clk);
  1558. }
  1559. if (dai_sel & RT5640_U_IF2) {
  1560. mask_clk = RT5640_I2S_BCLK_MS2_MASK | RT5640_I2S_PD2_MASK;
  1561. val_clk = bclk_ms << RT5640_I2S_BCLK_MS2_SFT |
  1562. pre_div << RT5640_I2S_PD2_SFT;
  1563. snd_soc_update_bits(codec, RT5640_I2S2_SDP,
  1564. RT5640_I2S_DL_MASK, val_len);
  1565. snd_soc_update_bits(codec, RT5640_ADDA_CLK1, mask_clk, val_clk);
  1566. }
  1567. return 0;
  1568. }
  1569. static int rt5640_set_dai_fmt(struct snd_soc_dai *dai, unsigned int fmt)
  1570. {
  1571. struct snd_soc_codec *codec = dai->codec;
  1572. struct rt5640_priv *rt5640 = snd_soc_codec_get_drvdata(codec);
  1573. unsigned int reg_val = 0;
  1574. int dai_sel;
  1575. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  1576. case SND_SOC_DAIFMT_CBM_CFM:
  1577. rt5640->master[dai->id] = 1;
  1578. break;
  1579. case SND_SOC_DAIFMT_CBS_CFS:
  1580. reg_val |= RT5640_I2S_MS_S;
  1581. rt5640->master[dai->id] = 0;
  1582. break;
  1583. default:
  1584. return -EINVAL;
  1585. }
  1586. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  1587. case SND_SOC_DAIFMT_NB_NF:
  1588. break;
  1589. case SND_SOC_DAIFMT_IB_NF:
  1590. reg_val |= RT5640_I2S_BP_INV;
  1591. break;
  1592. default:
  1593. return -EINVAL;
  1594. }
  1595. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  1596. case SND_SOC_DAIFMT_I2S:
  1597. break;
  1598. case SND_SOC_DAIFMT_LEFT_J:
  1599. reg_val |= RT5640_I2S_DF_LEFT;
  1600. break;
  1601. case SND_SOC_DAIFMT_DSP_A:
  1602. reg_val |= RT5640_I2S_DF_PCM_A;
  1603. break;
  1604. case SND_SOC_DAIFMT_DSP_B:
  1605. reg_val |= RT5640_I2S_DF_PCM_B;
  1606. break;
  1607. default:
  1608. return -EINVAL;
  1609. }
  1610. dai_sel = get_sdp_info(codec, dai->id);
  1611. if (dai_sel < 0) {
  1612. dev_err(codec->dev, "Failed to get sdp info: %d\n", dai_sel);
  1613. return -EINVAL;
  1614. }
  1615. if (dai_sel & RT5640_U_IF1) {
  1616. snd_soc_update_bits(codec, RT5640_I2S1_SDP,
  1617. RT5640_I2S_MS_MASK | RT5640_I2S_BP_MASK |
  1618. RT5640_I2S_DF_MASK, reg_val);
  1619. }
  1620. if (dai_sel & RT5640_U_IF2) {
  1621. snd_soc_update_bits(codec, RT5640_I2S2_SDP,
  1622. RT5640_I2S_MS_MASK | RT5640_I2S_BP_MASK |
  1623. RT5640_I2S_DF_MASK, reg_val);
  1624. }
  1625. return 0;
  1626. }
  1627. static int rt5640_set_dai_sysclk(struct snd_soc_dai *dai,
  1628. int clk_id, unsigned int freq, int dir)
  1629. {
  1630. struct snd_soc_codec *codec = dai->codec;
  1631. struct rt5640_priv *rt5640 = snd_soc_codec_get_drvdata(codec);
  1632. unsigned int reg_val = 0;
  1633. if (freq == rt5640->sysclk && clk_id == rt5640->sysclk_src)
  1634. return 0;
  1635. switch (clk_id) {
  1636. case RT5640_SCLK_S_MCLK:
  1637. reg_val |= RT5640_SCLK_SRC_MCLK;
  1638. break;
  1639. case RT5640_SCLK_S_PLL1:
  1640. reg_val |= RT5640_SCLK_SRC_PLL1;
  1641. break;
  1642. case RT5640_SCLK_S_RCCLK:
  1643. reg_val |= RT5640_SCLK_SRC_RCCLK;
  1644. break;
  1645. default:
  1646. dev_err(codec->dev, "Invalid clock id (%d)\n", clk_id);
  1647. return -EINVAL;
  1648. }
  1649. snd_soc_update_bits(codec, RT5640_GLB_CLK,
  1650. RT5640_SCLK_SRC_MASK, reg_val);
  1651. rt5640->sysclk = freq;
  1652. rt5640->sysclk_src = clk_id;
  1653. dev_dbg(dai->dev, "Sysclk is %dHz and clock id is %d\n", freq, clk_id);
  1654. return 0;
  1655. }
  1656. static int rt5640_set_dai_pll(struct snd_soc_dai *dai, int pll_id, int source,
  1657. unsigned int freq_in, unsigned int freq_out)
  1658. {
  1659. struct snd_soc_codec *codec = dai->codec;
  1660. struct rt5640_priv *rt5640 = snd_soc_codec_get_drvdata(codec);
  1661. struct rl6231_pll_code pll_code;
  1662. int ret;
  1663. if (source == rt5640->pll_src && freq_in == rt5640->pll_in &&
  1664. freq_out == rt5640->pll_out)
  1665. return 0;
  1666. if (!freq_in || !freq_out) {
  1667. dev_dbg(codec->dev, "PLL disabled\n");
  1668. rt5640->pll_in = 0;
  1669. rt5640->pll_out = 0;
  1670. snd_soc_update_bits(codec, RT5640_GLB_CLK,
  1671. RT5640_SCLK_SRC_MASK, RT5640_SCLK_SRC_MCLK);
  1672. return 0;
  1673. }
  1674. switch (source) {
  1675. case RT5640_PLL1_S_MCLK:
  1676. snd_soc_update_bits(codec, RT5640_GLB_CLK,
  1677. RT5640_PLL1_SRC_MASK, RT5640_PLL1_SRC_MCLK);
  1678. break;
  1679. case RT5640_PLL1_S_BCLK1:
  1680. snd_soc_update_bits(codec, RT5640_GLB_CLK,
  1681. RT5640_PLL1_SRC_MASK, RT5640_PLL1_SRC_BCLK1);
  1682. break;
  1683. case RT5640_PLL1_S_BCLK2:
  1684. snd_soc_update_bits(codec, RT5640_GLB_CLK,
  1685. RT5640_PLL1_SRC_MASK, RT5640_PLL1_SRC_BCLK2);
  1686. break;
  1687. default:
  1688. dev_err(codec->dev, "Unknown PLL source %d\n", source);
  1689. return -EINVAL;
  1690. }
  1691. ret = rl6231_pll_calc(freq_in, freq_out, &pll_code);
  1692. if (ret < 0) {
  1693. dev_err(codec->dev, "Unsupport input clock %d\n", freq_in);
  1694. return ret;
  1695. }
  1696. dev_dbg(codec->dev, "bypass=%d m=%d n=%d k=%d\n",
  1697. pll_code.m_bp, (pll_code.m_bp ? 0 : pll_code.m_code),
  1698. pll_code.n_code, pll_code.k_code);
  1699. snd_soc_write(codec, RT5640_PLL_CTRL1,
  1700. pll_code.n_code << RT5640_PLL_N_SFT | pll_code.k_code);
  1701. snd_soc_write(codec, RT5640_PLL_CTRL2,
  1702. (pll_code.m_bp ? 0 : pll_code.m_code) << RT5640_PLL_M_SFT |
  1703. pll_code.m_bp << RT5640_PLL_M_BP_SFT);
  1704. rt5640->pll_in = freq_in;
  1705. rt5640->pll_out = freq_out;
  1706. rt5640->pll_src = source;
  1707. return 0;
  1708. }
  1709. static int rt5640_set_bias_level(struct snd_soc_codec *codec,
  1710. enum snd_soc_bias_level level)
  1711. {
  1712. struct rt5640_priv *rt5640 = snd_soc_codec_get_drvdata(codec);
  1713. int ret;
  1714. switch (level) {
  1715. case SND_SOC_BIAS_ON:
  1716. break;
  1717. case SND_SOC_BIAS_PREPARE:
  1718. /*
  1719. * SND_SOC_BIAS_PREPARE is called while preparing for a
  1720. * transition to ON or away from ON. If current bias_level
  1721. * is SND_SOC_BIAS_ON, then it is preparing for a transition
  1722. * away from ON. Disable the clock in that case, otherwise
  1723. * enable it.
  1724. */
  1725. if (IS_ERR(rt5640->mclk))
  1726. break;
  1727. if (snd_soc_codec_get_bias_level(codec) == SND_SOC_BIAS_ON) {
  1728. clk_disable_unprepare(rt5640->mclk);
  1729. } else {
  1730. ret = clk_prepare_enable(rt5640->mclk);
  1731. if (ret)
  1732. return ret;
  1733. }
  1734. break;
  1735. case SND_SOC_BIAS_STANDBY:
  1736. if (SND_SOC_BIAS_OFF == snd_soc_codec_get_bias_level(codec)) {
  1737. snd_soc_update_bits(codec, RT5640_PWR_ANLG1,
  1738. RT5640_PWR_VREF1 | RT5640_PWR_MB |
  1739. RT5640_PWR_BG | RT5640_PWR_VREF2,
  1740. RT5640_PWR_VREF1 | RT5640_PWR_MB |
  1741. RT5640_PWR_BG | RT5640_PWR_VREF2);
  1742. usleep_range(10000, 15000);
  1743. snd_soc_update_bits(codec, RT5640_PWR_ANLG1,
  1744. RT5640_PWR_FV1 | RT5640_PWR_FV2,
  1745. RT5640_PWR_FV1 | RT5640_PWR_FV2);
  1746. snd_soc_update_bits(codec, RT5640_DUMMY1,
  1747. 0x0301, 0x0301);
  1748. snd_soc_update_bits(codec, RT5640_MICBIAS,
  1749. 0x0030, 0x0030);
  1750. }
  1751. break;
  1752. case SND_SOC_BIAS_OFF:
  1753. snd_soc_write(codec, RT5640_DEPOP_M1, 0x0004);
  1754. snd_soc_write(codec, RT5640_DEPOP_M2, 0x1100);
  1755. snd_soc_update_bits(codec, RT5640_DUMMY1, 0x1, 0);
  1756. snd_soc_write(codec, RT5640_PWR_DIG1, 0x0000);
  1757. snd_soc_write(codec, RT5640_PWR_DIG2, 0x0000);
  1758. snd_soc_write(codec, RT5640_PWR_VOL, 0x0000);
  1759. snd_soc_write(codec, RT5640_PWR_MIXER, 0x0000);
  1760. snd_soc_write(codec, RT5640_PWR_ANLG1, 0x0000);
  1761. snd_soc_write(codec, RT5640_PWR_ANLG2, 0x0000);
  1762. break;
  1763. default:
  1764. break;
  1765. }
  1766. return 0;
  1767. }
  1768. int rt5640_dmic_enable(struct snd_soc_codec *codec,
  1769. bool dmic1_data_pin, bool dmic2_data_pin)
  1770. {
  1771. struct rt5640_priv *rt5640 = snd_soc_codec_get_drvdata(codec);
  1772. regmap_update_bits(rt5640->regmap, RT5640_GPIO_CTRL1,
  1773. RT5640_GP2_PIN_MASK, RT5640_GP2_PIN_DMIC1_SCL);
  1774. if (dmic1_data_pin) {
  1775. regmap_update_bits(rt5640->regmap, RT5640_DMIC,
  1776. RT5640_DMIC_1_DP_MASK, RT5640_DMIC_1_DP_GPIO3);
  1777. regmap_update_bits(rt5640->regmap, RT5640_GPIO_CTRL1,
  1778. RT5640_GP3_PIN_MASK, RT5640_GP3_PIN_DMIC1_SDA);
  1779. }
  1780. if (dmic2_data_pin) {
  1781. regmap_update_bits(rt5640->regmap, RT5640_DMIC,
  1782. RT5640_DMIC_2_DP_MASK, RT5640_DMIC_2_DP_GPIO4);
  1783. regmap_update_bits(rt5640->regmap, RT5640_GPIO_CTRL1,
  1784. RT5640_GP4_PIN_MASK, RT5640_GP4_PIN_DMIC2_SDA);
  1785. }
  1786. return 0;
  1787. }
  1788. EXPORT_SYMBOL_GPL(rt5640_dmic_enable);
  1789. int rt5640_sel_asrc_clk_src(struct snd_soc_codec *codec,
  1790. unsigned int filter_mask, unsigned int clk_src)
  1791. {
  1792. struct rt5640_priv *rt5640 = snd_soc_codec_get_drvdata(codec);
  1793. unsigned int asrc2_mask = 0;
  1794. unsigned int asrc2_value = 0;
  1795. switch (clk_src) {
  1796. case RT5640_CLK_SEL_SYS:
  1797. case RT5640_CLK_SEL_ASRC:
  1798. break;
  1799. default:
  1800. return -EINVAL;
  1801. }
  1802. if (!filter_mask)
  1803. return -EINVAL;
  1804. if (filter_mask & RT5640_DA_STEREO_FILTER) {
  1805. asrc2_mask |= RT5640_STO_DAC_M_MASK;
  1806. asrc2_value = (asrc2_value & ~RT5640_STO_DAC_M_MASK)
  1807. | (clk_src << RT5640_STO_DAC_M_SFT);
  1808. }
  1809. if (filter_mask & RT5640_DA_MONO_L_FILTER) {
  1810. asrc2_mask |= RT5640_MDA_L_M_MASK;
  1811. asrc2_value = (asrc2_value & ~RT5640_MDA_L_M_MASK)
  1812. | (clk_src << RT5640_MDA_L_M_SFT);
  1813. }
  1814. if (filter_mask & RT5640_DA_MONO_R_FILTER) {
  1815. asrc2_mask |= RT5640_MDA_R_M_MASK;
  1816. asrc2_value = (asrc2_value & ~RT5640_MDA_R_M_MASK)
  1817. | (clk_src << RT5640_MDA_R_M_SFT);
  1818. }
  1819. if (filter_mask & RT5640_AD_STEREO_FILTER) {
  1820. asrc2_mask |= RT5640_ADC_M_MASK;
  1821. asrc2_value = (asrc2_value & ~RT5640_ADC_M_MASK)
  1822. | (clk_src << RT5640_ADC_M_SFT);
  1823. }
  1824. if (filter_mask & RT5640_AD_MONO_L_FILTER) {
  1825. asrc2_mask |= RT5640_MAD_L_M_MASK;
  1826. asrc2_value = (asrc2_value & ~RT5640_MAD_L_M_MASK)
  1827. | (clk_src << RT5640_MAD_L_M_SFT);
  1828. }
  1829. if (filter_mask & RT5640_AD_MONO_R_FILTER) {
  1830. asrc2_mask |= RT5640_MAD_R_M_MASK;
  1831. asrc2_value = (asrc2_value & ~RT5640_MAD_R_M_MASK)
  1832. | (clk_src << RT5640_MAD_R_M_SFT);
  1833. }
  1834. snd_soc_update_bits(codec, RT5640_ASRC_2,
  1835. asrc2_mask, asrc2_value);
  1836. if (snd_soc_read(codec, RT5640_ASRC_2)) {
  1837. rt5640->asrc_en = true;
  1838. snd_soc_update_bits(codec, RT5640_JD_CTRL, 0x3, 0x3);
  1839. } else {
  1840. rt5640->asrc_en = false;
  1841. snd_soc_update_bits(codec, RT5640_JD_CTRL, 0x3, 0x0);
  1842. }
  1843. return 0;
  1844. }
  1845. EXPORT_SYMBOL_GPL(rt5640_sel_asrc_clk_src);
  1846. static int rt5640_probe(struct snd_soc_codec *codec)
  1847. {
  1848. struct snd_soc_dapm_context *dapm = snd_soc_codec_get_dapm(codec);
  1849. struct rt5640_priv *rt5640 = snd_soc_codec_get_drvdata(codec);
  1850. /* Check if MCLK provided */
  1851. rt5640->mclk = devm_clk_get(codec->dev, "mclk");
  1852. if (PTR_ERR(rt5640->mclk) == -EPROBE_DEFER)
  1853. return -EPROBE_DEFER;
  1854. rt5640->codec = codec;
  1855. snd_soc_codec_force_bias_level(codec, SND_SOC_BIAS_OFF);
  1856. snd_soc_update_bits(codec, RT5640_DUMMY1, 0x0301, 0x0301);
  1857. snd_soc_update_bits(codec, RT5640_MICBIAS, 0x0030, 0x0030);
  1858. snd_soc_update_bits(codec, RT5640_DSP_PATH2, 0xfc00, 0x0c00);
  1859. switch (snd_soc_read(codec, RT5640_RESET) & RT5640_ID_MASK) {
  1860. case RT5640_ID_5640:
  1861. case RT5640_ID_5642:
  1862. snd_soc_add_codec_controls(codec,
  1863. rt5640_specific_snd_controls,
  1864. ARRAY_SIZE(rt5640_specific_snd_controls));
  1865. snd_soc_dapm_new_controls(dapm,
  1866. rt5640_specific_dapm_widgets,
  1867. ARRAY_SIZE(rt5640_specific_dapm_widgets));
  1868. snd_soc_dapm_add_routes(dapm,
  1869. rt5640_specific_dapm_routes,
  1870. ARRAY_SIZE(rt5640_specific_dapm_routes));
  1871. break;
  1872. case RT5640_ID_5639:
  1873. snd_soc_dapm_new_controls(dapm,
  1874. rt5639_specific_dapm_widgets,
  1875. ARRAY_SIZE(rt5639_specific_dapm_widgets));
  1876. snd_soc_dapm_add_routes(dapm,
  1877. rt5639_specific_dapm_routes,
  1878. ARRAY_SIZE(rt5639_specific_dapm_routes));
  1879. break;
  1880. default:
  1881. dev_err(codec->dev,
  1882. "The driver is for RT5639 RT5640 or RT5642 only\n");
  1883. return -ENODEV;
  1884. }
  1885. if (rt5640->pdata.dmic_en)
  1886. rt5640_dmic_enable(codec, rt5640->pdata.dmic1_data_pin,
  1887. rt5640->pdata.dmic2_data_pin);
  1888. return 0;
  1889. }
  1890. static int rt5640_remove(struct snd_soc_codec *codec)
  1891. {
  1892. rt5640_reset(codec);
  1893. return 0;
  1894. }
  1895. #ifdef CONFIG_PM
  1896. static int rt5640_suspend(struct snd_soc_codec *codec)
  1897. {
  1898. struct rt5640_priv *rt5640 = snd_soc_codec_get_drvdata(codec);
  1899. snd_soc_codec_force_bias_level(codec, SND_SOC_BIAS_OFF);
  1900. rt5640_reset(codec);
  1901. regcache_cache_only(rt5640->regmap, true);
  1902. regcache_mark_dirty(rt5640->regmap);
  1903. if (gpio_is_valid(rt5640->pdata.ldo1_en))
  1904. gpio_set_value_cansleep(rt5640->pdata.ldo1_en, 0);
  1905. return 0;
  1906. }
  1907. static int rt5640_resume(struct snd_soc_codec *codec)
  1908. {
  1909. struct rt5640_priv *rt5640 = snd_soc_codec_get_drvdata(codec);
  1910. if (gpio_is_valid(rt5640->pdata.ldo1_en)) {
  1911. gpio_set_value_cansleep(rt5640->pdata.ldo1_en, 1);
  1912. msleep(400);
  1913. }
  1914. regcache_cache_only(rt5640->regmap, false);
  1915. regcache_sync(rt5640->regmap);
  1916. return 0;
  1917. }
  1918. #else
  1919. #define rt5640_suspend NULL
  1920. #define rt5640_resume NULL
  1921. #endif
  1922. #define RT5640_STEREO_RATES SNDRV_PCM_RATE_8000_96000
  1923. #define RT5640_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
  1924. SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S8)
  1925. static const struct snd_soc_dai_ops rt5640_aif_dai_ops = {
  1926. .hw_params = rt5640_hw_params,
  1927. .set_fmt = rt5640_set_dai_fmt,
  1928. .set_sysclk = rt5640_set_dai_sysclk,
  1929. .set_pll = rt5640_set_dai_pll,
  1930. };
  1931. static struct snd_soc_dai_driver rt5640_dai[] = {
  1932. {
  1933. .name = "rt5640-aif1",
  1934. .id = RT5640_AIF1,
  1935. .playback = {
  1936. .stream_name = "AIF1 Playback",
  1937. .channels_min = 1,
  1938. .channels_max = 2,
  1939. .rates = RT5640_STEREO_RATES,
  1940. .formats = RT5640_FORMATS,
  1941. },
  1942. .capture = {
  1943. .stream_name = "AIF1 Capture",
  1944. .channels_min = 1,
  1945. .channels_max = 2,
  1946. .rates = RT5640_STEREO_RATES,
  1947. .formats = RT5640_FORMATS,
  1948. },
  1949. .ops = &rt5640_aif_dai_ops,
  1950. },
  1951. {
  1952. .name = "rt5640-aif2",
  1953. .id = RT5640_AIF2,
  1954. .playback = {
  1955. .stream_name = "AIF2 Playback",
  1956. .channels_min = 1,
  1957. .channels_max = 2,
  1958. .rates = RT5640_STEREO_RATES,
  1959. .formats = RT5640_FORMATS,
  1960. },
  1961. .capture = {
  1962. .stream_name = "AIF2 Capture",
  1963. .channels_min = 1,
  1964. .channels_max = 2,
  1965. .rates = RT5640_STEREO_RATES,
  1966. .formats = RT5640_FORMATS,
  1967. },
  1968. .ops = &rt5640_aif_dai_ops,
  1969. },
  1970. };
  1971. static struct snd_soc_codec_driver soc_codec_dev_rt5640 = {
  1972. .probe = rt5640_probe,
  1973. .remove = rt5640_remove,
  1974. .suspend = rt5640_suspend,
  1975. .resume = rt5640_resume,
  1976. .set_bias_level = rt5640_set_bias_level,
  1977. .idle_bias_off = true,
  1978. .component_driver = {
  1979. .controls = rt5640_snd_controls,
  1980. .num_controls = ARRAY_SIZE(rt5640_snd_controls),
  1981. .dapm_widgets = rt5640_dapm_widgets,
  1982. .num_dapm_widgets = ARRAY_SIZE(rt5640_dapm_widgets),
  1983. .dapm_routes = rt5640_dapm_routes,
  1984. .num_dapm_routes = ARRAY_SIZE(rt5640_dapm_routes),
  1985. },
  1986. };
  1987. static const struct regmap_config rt5640_regmap = {
  1988. .reg_bits = 8,
  1989. .val_bits = 16,
  1990. .use_single_rw = true,
  1991. .max_register = RT5640_VENDOR_ID2 + 1 + (ARRAY_SIZE(rt5640_ranges) *
  1992. RT5640_PR_SPACING),
  1993. .volatile_reg = rt5640_volatile_register,
  1994. .readable_reg = rt5640_readable_register,
  1995. .cache_type = REGCACHE_RBTREE,
  1996. .reg_defaults = rt5640_reg,
  1997. .num_reg_defaults = ARRAY_SIZE(rt5640_reg),
  1998. .ranges = rt5640_ranges,
  1999. .num_ranges = ARRAY_SIZE(rt5640_ranges),
  2000. };
  2001. static const struct i2c_device_id rt5640_i2c_id[] = {
  2002. { "rt5640", 0 },
  2003. { "rt5639", 0 },
  2004. { "rt5642", 0 },
  2005. { }
  2006. };
  2007. MODULE_DEVICE_TABLE(i2c, rt5640_i2c_id);
  2008. #if defined(CONFIG_OF)
  2009. static const struct of_device_id rt5640_of_match[] = {
  2010. { .compatible = "realtek,rt5639", },
  2011. { .compatible = "realtek,rt5640", },
  2012. {},
  2013. };
  2014. MODULE_DEVICE_TABLE(of, rt5640_of_match);
  2015. #endif
  2016. #ifdef CONFIG_ACPI
  2017. static const struct acpi_device_id rt5640_acpi_match[] = {
  2018. { "INT33CA", 0 },
  2019. { "10EC5640", 0 },
  2020. { "10EC5642", 0 },
  2021. { "INTCCFFD", 0 },
  2022. { },
  2023. };
  2024. MODULE_DEVICE_TABLE(acpi, rt5640_acpi_match);
  2025. #endif
  2026. static int rt5640_parse_dt(struct rt5640_priv *rt5640, struct device_node *np)
  2027. {
  2028. rt5640->pdata.in1_diff = of_property_read_bool(np,
  2029. "realtek,in1-differential");
  2030. rt5640->pdata.in2_diff = of_property_read_bool(np,
  2031. "realtek,in2-differential");
  2032. rt5640->pdata.ldo1_en = of_get_named_gpio(np,
  2033. "realtek,ldo1-en-gpios", 0);
  2034. /*
  2035. * LDO1_EN is optional (it may be statically tied on the board).
  2036. * -ENOENT means that the property doesn't exist, i.e. there is no
  2037. * GPIO, so is not an error. Any other error code means the property
  2038. * exists, but could not be parsed.
  2039. */
  2040. if (!gpio_is_valid(rt5640->pdata.ldo1_en) &&
  2041. (rt5640->pdata.ldo1_en != -ENOENT))
  2042. return rt5640->pdata.ldo1_en;
  2043. return 0;
  2044. }
  2045. static int rt5640_i2c_probe(struct i2c_client *i2c,
  2046. const struct i2c_device_id *id)
  2047. {
  2048. struct rt5640_platform_data *pdata = dev_get_platdata(&i2c->dev);
  2049. struct rt5640_priv *rt5640;
  2050. int ret;
  2051. unsigned int val;
  2052. rt5640 = devm_kzalloc(&i2c->dev,
  2053. sizeof(struct rt5640_priv),
  2054. GFP_KERNEL);
  2055. if (NULL == rt5640)
  2056. return -ENOMEM;
  2057. i2c_set_clientdata(i2c, rt5640);
  2058. if (pdata) {
  2059. rt5640->pdata = *pdata;
  2060. /*
  2061. * Translate zero'd out (default) pdata value to an invalid
  2062. * GPIO ID. This makes the pdata and DT paths consistent in
  2063. * terms of the value left in this field when no GPIO is
  2064. * specified, but means we can't actually use GPIO 0.
  2065. */
  2066. if (!rt5640->pdata.ldo1_en)
  2067. rt5640->pdata.ldo1_en = -EINVAL;
  2068. } else if (i2c->dev.of_node) {
  2069. ret = rt5640_parse_dt(rt5640, i2c->dev.of_node);
  2070. if (ret)
  2071. return ret;
  2072. } else
  2073. rt5640->pdata.ldo1_en = -EINVAL;
  2074. rt5640->regmap = devm_regmap_init_i2c(i2c, &rt5640_regmap);
  2075. if (IS_ERR(rt5640->regmap)) {
  2076. ret = PTR_ERR(rt5640->regmap);
  2077. dev_err(&i2c->dev, "Failed to allocate register map: %d\n",
  2078. ret);
  2079. return ret;
  2080. }
  2081. if (gpio_is_valid(rt5640->pdata.ldo1_en)) {
  2082. ret = devm_gpio_request_one(&i2c->dev, rt5640->pdata.ldo1_en,
  2083. GPIOF_OUT_INIT_HIGH,
  2084. "RT5640 LDO1_EN");
  2085. if (ret < 0) {
  2086. dev_err(&i2c->dev, "Failed to request LDO1_EN %d: %d\n",
  2087. rt5640->pdata.ldo1_en, ret);
  2088. return ret;
  2089. }
  2090. msleep(400);
  2091. }
  2092. regmap_read(rt5640->regmap, RT5640_VENDOR_ID2, &val);
  2093. if (val != RT5640_DEVICE_ID) {
  2094. dev_err(&i2c->dev,
  2095. "Device with ID register %#x is not rt5640/39\n", val);
  2096. return -ENODEV;
  2097. }
  2098. regmap_write(rt5640->regmap, RT5640_RESET, 0);
  2099. ret = regmap_register_patch(rt5640->regmap, init_list,
  2100. ARRAY_SIZE(init_list));
  2101. if (ret != 0)
  2102. dev_warn(&i2c->dev, "Failed to apply regmap patch: %d\n", ret);
  2103. if (rt5640->pdata.in1_diff)
  2104. regmap_update_bits(rt5640->regmap, RT5640_IN1_IN2,
  2105. RT5640_IN_DF1, RT5640_IN_DF1);
  2106. if (rt5640->pdata.in2_diff)
  2107. regmap_update_bits(rt5640->regmap, RT5640_IN3_IN4,
  2108. RT5640_IN_DF2, RT5640_IN_DF2);
  2109. if (rt5640->pdata.in3_diff)
  2110. regmap_update_bits(rt5640->regmap, RT5640_IN1_IN2,
  2111. RT5640_IN_DF2, RT5640_IN_DF2);
  2112. rt5640->hp_mute = 1;
  2113. return snd_soc_register_codec(&i2c->dev, &soc_codec_dev_rt5640,
  2114. rt5640_dai, ARRAY_SIZE(rt5640_dai));
  2115. }
  2116. static int rt5640_i2c_remove(struct i2c_client *i2c)
  2117. {
  2118. snd_soc_unregister_codec(&i2c->dev);
  2119. return 0;
  2120. }
  2121. static struct i2c_driver rt5640_i2c_driver = {
  2122. .driver = {
  2123. .name = "rt5640",
  2124. .acpi_match_table = ACPI_PTR(rt5640_acpi_match),
  2125. .of_match_table = of_match_ptr(rt5640_of_match),
  2126. },
  2127. .probe = rt5640_i2c_probe,
  2128. .remove = rt5640_i2c_remove,
  2129. .id_table = rt5640_i2c_id,
  2130. };
  2131. module_i2c_driver(rt5640_i2c_driver);
  2132. MODULE_DESCRIPTION("ASoC RT5640/RT5639 driver");
  2133. MODULE_AUTHOR("Johnny Hsu <johnnyhsu@realtek.com>");
  2134. MODULE_LICENSE("GPL v2");