rt5631.c 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738
  1. /*
  2. * rt5631.c -- RT5631 ALSA Soc Audio driver
  3. *
  4. * Copyright 2011 Realtek Microelectronics
  5. *
  6. * Author: flove <flove@realtek.com>
  7. *
  8. * Based on WM8753.c
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. *
  14. */
  15. #include <linux/module.h>
  16. #include <linux/moduleparam.h>
  17. #include <linux/init.h>
  18. #include <linux/delay.h>
  19. #include <linux/pm.h>
  20. #include <linux/i2c.h>
  21. #include <linux/regmap.h>
  22. #include <sound/core.h>
  23. #include <sound/pcm.h>
  24. #include <sound/pcm_params.h>
  25. #include <sound/soc.h>
  26. #include <sound/soc-dapm.h>
  27. #include <sound/initval.h>
  28. #include <sound/tlv.h>
  29. #include "rt5631.h"
  30. struct rt5631_priv {
  31. struct regmap *regmap;
  32. int codec_version;
  33. int master;
  34. int sysclk;
  35. int rx_rate;
  36. int bclk_rate;
  37. int dmic_used_flag;
  38. };
  39. static const struct reg_default rt5631_reg[] = {
  40. { RT5631_SPK_OUT_VOL, 0x8888 },
  41. { RT5631_HP_OUT_VOL, 0x8080 },
  42. { RT5631_MONO_AXO_1_2_VOL, 0xa080 },
  43. { RT5631_AUX_IN_VOL, 0x0808 },
  44. { RT5631_ADC_REC_MIXER, 0xf0f0 },
  45. { RT5631_VDAC_DIG_VOL, 0x0010 },
  46. { RT5631_OUTMIXER_L_CTRL, 0xffc0 },
  47. { RT5631_OUTMIXER_R_CTRL, 0xffc0 },
  48. { RT5631_AXO1MIXER_CTRL, 0x88c0 },
  49. { RT5631_AXO2MIXER_CTRL, 0x88c0 },
  50. { RT5631_DIG_MIC_CTRL, 0x3000 },
  51. { RT5631_MONO_INPUT_VOL, 0x8808 },
  52. { RT5631_SPK_MIXER_CTRL, 0xf8f8 },
  53. { RT5631_SPK_MONO_OUT_CTRL, 0xfc00 },
  54. { RT5631_SPK_MONO_HP_OUT_CTRL, 0x4440 },
  55. { RT5631_SDP_CTRL, 0x8000 },
  56. { RT5631_MONO_SDP_CTRL, 0x8000 },
  57. { RT5631_STEREO_AD_DA_CLK_CTRL, 0x2010 },
  58. { RT5631_GEN_PUR_CTRL_REG, 0x0e00 },
  59. { RT5631_INT_ST_IRQ_CTRL_2, 0x071a },
  60. { RT5631_MISC_CTRL, 0x2040 },
  61. { RT5631_DEPOP_FUN_CTRL_2, 0x8000 },
  62. { RT5631_SOFT_VOL_CTRL, 0x07e0 },
  63. { RT5631_ALC_CTRL_1, 0x0206 },
  64. { RT5631_ALC_CTRL_3, 0x2000 },
  65. { RT5631_PSEUDO_SPATL_CTRL, 0x0553 },
  66. };
  67. /**
  68. * rt5631_write_index - write index register of 2nd layer
  69. */
  70. static void rt5631_write_index(struct snd_soc_codec *codec,
  71. unsigned int reg, unsigned int value)
  72. {
  73. snd_soc_write(codec, RT5631_INDEX_ADD, reg);
  74. snd_soc_write(codec, RT5631_INDEX_DATA, value);
  75. }
  76. /**
  77. * rt5631_read_index - read index register of 2nd layer
  78. */
  79. static unsigned int rt5631_read_index(struct snd_soc_codec *codec,
  80. unsigned int reg)
  81. {
  82. unsigned int value;
  83. snd_soc_write(codec, RT5631_INDEX_ADD, reg);
  84. value = snd_soc_read(codec, RT5631_INDEX_DATA);
  85. return value;
  86. }
  87. static int rt5631_reset(struct snd_soc_codec *codec)
  88. {
  89. return snd_soc_write(codec, RT5631_RESET, 0);
  90. }
  91. static bool rt5631_volatile_register(struct device *dev, unsigned int reg)
  92. {
  93. switch (reg) {
  94. case RT5631_RESET:
  95. case RT5631_INT_ST_IRQ_CTRL_2:
  96. case RT5631_INDEX_ADD:
  97. case RT5631_INDEX_DATA:
  98. case RT5631_EQ_CTRL:
  99. return 1;
  100. default:
  101. return 0;
  102. }
  103. }
  104. static bool rt5631_readable_register(struct device *dev, unsigned int reg)
  105. {
  106. switch (reg) {
  107. case RT5631_RESET:
  108. case RT5631_SPK_OUT_VOL:
  109. case RT5631_HP_OUT_VOL:
  110. case RT5631_MONO_AXO_1_2_VOL:
  111. case RT5631_AUX_IN_VOL:
  112. case RT5631_STEREO_DAC_VOL_1:
  113. case RT5631_MIC_CTRL_1:
  114. case RT5631_STEREO_DAC_VOL_2:
  115. case RT5631_ADC_CTRL_1:
  116. case RT5631_ADC_REC_MIXER:
  117. case RT5631_ADC_CTRL_2:
  118. case RT5631_VDAC_DIG_VOL:
  119. case RT5631_OUTMIXER_L_CTRL:
  120. case RT5631_OUTMIXER_R_CTRL:
  121. case RT5631_AXO1MIXER_CTRL:
  122. case RT5631_AXO2MIXER_CTRL:
  123. case RT5631_MIC_CTRL_2:
  124. case RT5631_DIG_MIC_CTRL:
  125. case RT5631_MONO_INPUT_VOL:
  126. case RT5631_SPK_MIXER_CTRL:
  127. case RT5631_SPK_MONO_OUT_CTRL:
  128. case RT5631_SPK_MONO_HP_OUT_CTRL:
  129. case RT5631_SDP_CTRL:
  130. case RT5631_MONO_SDP_CTRL:
  131. case RT5631_STEREO_AD_DA_CLK_CTRL:
  132. case RT5631_PWR_MANAG_ADD1:
  133. case RT5631_PWR_MANAG_ADD2:
  134. case RT5631_PWR_MANAG_ADD3:
  135. case RT5631_PWR_MANAG_ADD4:
  136. case RT5631_GEN_PUR_CTRL_REG:
  137. case RT5631_GLOBAL_CLK_CTRL:
  138. case RT5631_PLL_CTRL:
  139. case RT5631_INT_ST_IRQ_CTRL_1:
  140. case RT5631_INT_ST_IRQ_CTRL_2:
  141. case RT5631_GPIO_CTRL:
  142. case RT5631_MISC_CTRL:
  143. case RT5631_DEPOP_FUN_CTRL_1:
  144. case RT5631_DEPOP_FUN_CTRL_2:
  145. case RT5631_JACK_DET_CTRL:
  146. case RT5631_SOFT_VOL_CTRL:
  147. case RT5631_ALC_CTRL_1:
  148. case RT5631_ALC_CTRL_2:
  149. case RT5631_ALC_CTRL_3:
  150. case RT5631_PSEUDO_SPATL_CTRL:
  151. case RT5631_INDEX_ADD:
  152. case RT5631_INDEX_DATA:
  153. case RT5631_EQ_CTRL:
  154. case RT5631_VENDOR_ID:
  155. case RT5631_VENDOR_ID1:
  156. case RT5631_VENDOR_ID2:
  157. return 1;
  158. default:
  159. return 0;
  160. }
  161. }
  162. static const DECLARE_TLV_DB_SCALE(out_vol_tlv, -4650, 150, 0);
  163. static const DECLARE_TLV_DB_SCALE(dac_vol_tlv, -95625, 375, 0);
  164. static const DECLARE_TLV_DB_SCALE(in_vol_tlv, -3450, 150, 0);
  165. /* {0, +20, +24, +30, +35, +40, +44, +50, +52}dB */
  166. static const DECLARE_TLV_DB_RANGE(mic_bst_tlv,
  167. 0, 0, TLV_DB_SCALE_ITEM(0, 0, 0),
  168. 1, 1, TLV_DB_SCALE_ITEM(2000, 0, 0),
  169. 2, 2, TLV_DB_SCALE_ITEM(2400, 0, 0),
  170. 3, 5, TLV_DB_SCALE_ITEM(3000, 500, 0),
  171. 6, 6, TLV_DB_SCALE_ITEM(4400, 0, 0),
  172. 7, 7, TLV_DB_SCALE_ITEM(5000, 0, 0),
  173. 8, 8, TLV_DB_SCALE_ITEM(5200, 0, 0)
  174. );
  175. static int rt5631_dmic_get(struct snd_kcontrol *kcontrol,
  176. struct snd_ctl_elem_value *ucontrol)
  177. {
  178. struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
  179. struct rt5631_priv *rt5631 = snd_soc_codec_get_drvdata(codec);
  180. ucontrol->value.integer.value[0] = rt5631->dmic_used_flag;
  181. return 0;
  182. }
  183. static int rt5631_dmic_put(struct snd_kcontrol *kcontrol,
  184. struct snd_ctl_elem_value *ucontrol)
  185. {
  186. struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
  187. struct rt5631_priv *rt5631 = snd_soc_codec_get_drvdata(codec);
  188. rt5631->dmic_used_flag = ucontrol->value.integer.value[0];
  189. return 0;
  190. }
  191. /* MIC Input Type */
  192. static const char *rt5631_input_mode[] = {
  193. "Single ended", "Differential"};
  194. static SOC_ENUM_SINGLE_DECL(rt5631_mic1_mode_enum, RT5631_MIC_CTRL_1,
  195. RT5631_MIC1_DIFF_INPUT_SHIFT, rt5631_input_mode);
  196. static SOC_ENUM_SINGLE_DECL(rt5631_mic2_mode_enum, RT5631_MIC_CTRL_1,
  197. RT5631_MIC2_DIFF_INPUT_SHIFT, rt5631_input_mode);
  198. /* MONO Input Type */
  199. static SOC_ENUM_SINGLE_DECL(rt5631_monoin_mode_enum, RT5631_MONO_INPUT_VOL,
  200. RT5631_MONO_DIFF_INPUT_SHIFT, rt5631_input_mode);
  201. /* SPK Ratio Gain Control */
  202. static const char *rt5631_spk_ratio[] = {"1.00x", "1.09x", "1.27x", "1.44x",
  203. "1.56x", "1.68x", "1.99x", "2.34x"};
  204. static SOC_ENUM_SINGLE_DECL(rt5631_spk_ratio_enum, RT5631_GEN_PUR_CTRL_REG,
  205. RT5631_SPK_AMP_RATIO_CTRL_SHIFT, rt5631_spk_ratio);
  206. static const struct snd_kcontrol_new rt5631_snd_controls[] = {
  207. /* MIC */
  208. SOC_ENUM("MIC1 Mode Control", rt5631_mic1_mode_enum),
  209. SOC_SINGLE_TLV("MIC1 Boost", RT5631_MIC_CTRL_2,
  210. RT5631_MIC1_BOOST_SHIFT, 8, 0, mic_bst_tlv),
  211. SOC_ENUM("MIC2 Mode Control", rt5631_mic2_mode_enum),
  212. SOC_SINGLE_TLV("MIC2 Boost", RT5631_MIC_CTRL_2,
  213. RT5631_MIC2_BOOST_SHIFT, 8, 0, mic_bst_tlv),
  214. /* MONO IN */
  215. SOC_ENUM("MONOIN Mode Control", rt5631_monoin_mode_enum),
  216. SOC_DOUBLE_TLV("MONOIN_RX Capture Volume", RT5631_MONO_INPUT_VOL,
  217. RT5631_L_VOL_SHIFT, RT5631_R_VOL_SHIFT,
  218. RT5631_VOL_MASK, 1, in_vol_tlv),
  219. /* AXI */
  220. SOC_DOUBLE_TLV("AXI Capture Volume", RT5631_AUX_IN_VOL,
  221. RT5631_L_VOL_SHIFT, RT5631_R_VOL_SHIFT,
  222. RT5631_VOL_MASK, 1, in_vol_tlv),
  223. /* DAC */
  224. SOC_DOUBLE_TLV("PCM Playback Volume", RT5631_STEREO_DAC_VOL_2,
  225. RT5631_L_VOL_SHIFT, RT5631_R_VOL_SHIFT,
  226. RT5631_DAC_VOL_MASK, 1, dac_vol_tlv),
  227. SOC_DOUBLE("PCM Playback Switch", RT5631_STEREO_DAC_VOL_1,
  228. RT5631_L_MUTE_SHIFT, RT5631_R_MUTE_SHIFT, 1, 1),
  229. /* AXO */
  230. SOC_SINGLE("AXO1 Playback Switch", RT5631_MONO_AXO_1_2_VOL,
  231. RT5631_L_MUTE_SHIFT, 1, 1),
  232. SOC_SINGLE("AXO2 Playback Switch", RT5631_MONO_AXO_1_2_VOL,
  233. RT5631_R_VOL_SHIFT, 1, 1),
  234. /* OUTVOL */
  235. SOC_DOUBLE("OUTVOL Channel Switch", RT5631_SPK_OUT_VOL,
  236. RT5631_L_EN_SHIFT, RT5631_R_EN_SHIFT, 1, 0),
  237. /* SPK */
  238. SOC_DOUBLE("Speaker Playback Switch", RT5631_SPK_OUT_VOL,
  239. RT5631_L_MUTE_SHIFT, RT5631_R_MUTE_SHIFT, 1, 1),
  240. SOC_DOUBLE_TLV("Speaker Playback Volume", RT5631_SPK_OUT_VOL,
  241. RT5631_L_VOL_SHIFT, RT5631_R_VOL_SHIFT, 39, 1, out_vol_tlv),
  242. /* MONO OUT */
  243. SOC_SINGLE("MONO Playback Switch", RT5631_MONO_AXO_1_2_VOL,
  244. RT5631_MUTE_MONO_SHIFT, 1, 1),
  245. /* HP */
  246. SOC_DOUBLE("HP Playback Switch", RT5631_HP_OUT_VOL,
  247. RT5631_L_MUTE_SHIFT, RT5631_R_MUTE_SHIFT, 1, 1),
  248. SOC_DOUBLE_TLV("HP Playback Volume", RT5631_HP_OUT_VOL,
  249. RT5631_L_VOL_SHIFT, RT5631_R_VOL_SHIFT,
  250. RT5631_VOL_MASK, 1, out_vol_tlv),
  251. /* DMIC */
  252. SOC_SINGLE_EXT("DMIC Switch", 0, 0, 1, 0,
  253. rt5631_dmic_get, rt5631_dmic_put),
  254. SOC_DOUBLE("DMIC Capture Switch", RT5631_DIG_MIC_CTRL,
  255. RT5631_DMIC_L_CH_MUTE_SHIFT,
  256. RT5631_DMIC_R_CH_MUTE_SHIFT, 1, 1),
  257. /* SPK Ratio Gain Control */
  258. SOC_ENUM("SPK Ratio Control", rt5631_spk_ratio_enum),
  259. };
  260. static int check_sysclk1_source(struct snd_soc_dapm_widget *source,
  261. struct snd_soc_dapm_widget *sink)
  262. {
  263. struct snd_soc_codec *codec = snd_soc_dapm_to_codec(source->dapm);
  264. unsigned int reg;
  265. reg = snd_soc_read(codec, RT5631_GLOBAL_CLK_CTRL);
  266. return reg & RT5631_SYSCLK_SOUR_SEL_PLL;
  267. }
  268. static int check_dmic_used(struct snd_soc_dapm_widget *source,
  269. struct snd_soc_dapm_widget *sink)
  270. {
  271. struct snd_soc_codec *codec = snd_soc_dapm_to_codec(source->dapm);
  272. struct rt5631_priv *rt5631 = snd_soc_codec_get_drvdata(codec);
  273. return rt5631->dmic_used_flag;
  274. }
  275. static int check_dacl_to_outmixl(struct snd_soc_dapm_widget *source,
  276. struct snd_soc_dapm_widget *sink)
  277. {
  278. struct snd_soc_codec *codec = snd_soc_dapm_to_codec(source->dapm);
  279. unsigned int reg;
  280. reg = snd_soc_read(codec, RT5631_OUTMIXER_L_CTRL);
  281. return !(reg & RT5631_M_DAC_L_TO_OUTMIXER_L);
  282. }
  283. static int check_dacr_to_outmixr(struct snd_soc_dapm_widget *source,
  284. struct snd_soc_dapm_widget *sink)
  285. {
  286. struct snd_soc_codec *codec = snd_soc_dapm_to_codec(source->dapm);
  287. unsigned int reg;
  288. reg = snd_soc_read(codec, RT5631_OUTMIXER_R_CTRL);
  289. return !(reg & RT5631_M_DAC_R_TO_OUTMIXER_R);
  290. }
  291. static int check_dacl_to_spkmixl(struct snd_soc_dapm_widget *source,
  292. struct snd_soc_dapm_widget *sink)
  293. {
  294. struct snd_soc_codec *codec = snd_soc_dapm_to_codec(source->dapm);
  295. unsigned int reg;
  296. reg = snd_soc_read(codec, RT5631_SPK_MIXER_CTRL);
  297. return !(reg & RT5631_M_DAC_L_TO_SPKMIXER_L);
  298. }
  299. static int check_dacr_to_spkmixr(struct snd_soc_dapm_widget *source,
  300. struct snd_soc_dapm_widget *sink)
  301. {
  302. struct snd_soc_codec *codec = snd_soc_dapm_to_codec(source->dapm);
  303. unsigned int reg;
  304. reg = snd_soc_read(codec, RT5631_SPK_MIXER_CTRL);
  305. return !(reg & RT5631_M_DAC_R_TO_SPKMIXER_R);
  306. }
  307. static int check_adcl_select(struct snd_soc_dapm_widget *source,
  308. struct snd_soc_dapm_widget *sink)
  309. {
  310. struct snd_soc_codec *codec = snd_soc_dapm_to_codec(source->dapm);
  311. unsigned int reg;
  312. reg = snd_soc_read(codec, RT5631_ADC_REC_MIXER);
  313. return !(reg & RT5631_M_MIC1_TO_RECMIXER_L);
  314. }
  315. static int check_adcr_select(struct snd_soc_dapm_widget *source,
  316. struct snd_soc_dapm_widget *sink)
  317. {
  318. struct snd_soc_codec *codec = snd_soc_dapm_to_codec(source->dapm);
  319. unsigned int reg;
  320. reg = snd_soc_read(codec, RT5631_ADC_REC_MIXER);
  321. return !(reg & RT5631_M_MIC2_TO_RECMIXER_R);
  322. }
  323. /**
  324. * onebit_depop_power_stage - auto depop in power stage.
  325. * @enable: power on/off
  326. *
  327. * When power on/off headphone, the depop sequence is done by hardware.
  328. */
  329. static void onebit_depop_power_stage(struct snd_soc_codec *codec, int enable)
  330. {
  331. unsigned int soft_vol, hp_zc;
  332. /* enable one-bit depop function */
  333. snd_soc_update_bits(codec, RT5631_DEPOP_FUN_CTRL_2,
  334. RT5631_EN_ONE_BIT_DEPOP, 0);
  335. /* keep soft volume and zero crossing setting */
  336. soft_vol = snd_soc_read(codec, RT5631_SOFT_VOL_CTRL);
  337. snd_soc_write(codec, RT5631_SOFT_VOL_CTRL, 0);
  338. hp_zc = snd_soc_read(codec, RT5631_INT_ST_IRQ_CTRL_2);
  339. snd_soc_write(codec, RT5631_INT_ST_IRQ_CTRL_2, hp_zc & 0xf7ff);
  340. if (enable) {
  341. /* config one-bit depop parameter */
  342. rt5631_write_index(codec, RT5631_TEST_MODE_CTRL, 0x84c0);
  343. rt5631_write_index(codec, RT5631_SPK_INTL_CTRL, 0x309f);
  344. rt5631_write_index(codec, RT5631_CP_INTL_REG2, 0x6530);
  345. /* power on capless block */
  346. snd_soc_write(codec, RT5631_DEPOP_FUN_CTRL_2,
  347. RT5631_EN_CAP_FREE_DEPOP);
  348. } else {
  349. /* power off capless block */
  350. snd_soc_write(codec, RT5631_DEPOP_FUN_CTRL_2, 0);
  351. msleep(100);
  352. }
  353. /* recover soft volume and zero crossing setting */
  354. snd_soc_write(codec, RT5631_SOFT_VOL_CTRL, soft_vol);
  355. snd_soc_write(codec, RT5631_INT_ST_IRQ_CTRL_2, hp_zc);
  356. }
  357. /**
  358. * onebit_depop_mute_stage - auto depop in mute stage.
  359. * @enable: mute/unmute
  360. *
  361. * When mute/unmute headphone, the depop sequence is done by hardware.
  362. */
  363. static void onebit_depop_mute_stage(struct snd_soc_codec *codec, int enable)
  364. {
  365. unsigned int soft_vol, hp_zc;
  366. /* enable one-bit depop function */
  367. snd_soc_update_bits(codec, RT5631_DEPOP_FUN_CTRL_2,
  368. RT5631_EN_ONE_BIT_DEPOP, 0);
  369. /* keep soft volume and zero crossing setting */
  370. soft_vol = snd_soc_read(codec, RT5631_SOFT_VOL_CTRL);
  371. snd_soc_write(codec, RT5631_SOFT_VOL_CTRL, 0);
  372. hp_zc = snd_soc_read(codec, RT5631_INT_ST_IRQ_CTRL_2);
  373. snd_soc_write(codec, RT5631_INT_ST_IRQ_CTRL_2, hp_zc & 0xf7ff);
  374. if (enable) {
  375. schedule_timeout_uninterruptible(msecs_to_jiffies(10));
  376. /* config one-bit depop parameter */
  377. rt5631_write_index(codec, RT5631_SPK_INTL_CTRL, 0x307f);
  378. snd_soc_update_bits(codec, RT5631_HP_OUT_VOL,
  379. RT5631_L_MUTE | RT5631_R_MUTE, 0);
  380. msleep(300);
  381. } else {
  382. snd_soc_update_bits(codec, RT5631_HP_OUT_VOL,
  383. RT5631_L_MUTE | RT5631_R_MUTE,
  384. RT5631_L_MUTE | RT5631_R_MUTE);
  385. msleep(100);
  386. }
  387. /* recover soft volume and zero crossing setting */
  388. snd_soc_write(codec, RT5631_SOFT_VOL_CTRL, soft_vol);
  389. snd_soc_write(codec, RT5631_INT_ST_IRQ_CTRL_2, hp_zc);
  390. }
  391. /**
  392. * onebit_depop_power_stage - step by step depop sequence in power stage.
  393. * @enable: power on/off
  394. *
  395. * When power on/off headphone, the depop sequence is done in step by step.
  396. */
  397. static void depop_seq_power_stage(struct snd_soc_codec *codec, int enable)
  398. {
  399. unsigned int soft_vol, hp_zc;
  400. /* depop control by register */
  401. snd_soc_update_bits(codec, RT5631_DEPOP_FUN_CTRL_2,
  402. RT5631_EN_ONE_BIT_DEPOP, RT5631_EN_ONE_BIT_DEPOP);
  403. /* keep soft volume and zero crossing setting */
  404. soft_vol = snd_soc_read(codec, RT5631_SOFT_VOL_CTRL);
  405. snd_soc_write(codec, RT5631_SOFT_VOL_CTRL, 0);
  406. hp_zc = snd_soc_read(codec, RT5631_INT_ST_IRQ_CTRL_2);
  407. snd_soc_write(codec, RT5631_INT_ST_IRQ_CTRL_2, hp_zc & 0xf7ff);
  408. if (enable) {
  409. /* config depop sequence parameter */
  410. rt5631_write_index(codec, RT5631_SPK_INTL_CTRL, 0x303e);
  411. /* power on headphone and charge pump */
  412. snd_soc_update_bits(codec, RT5631_PWR_MANAG_ADD3,
  413. RT5631_PWR_CHARGE_PUMP | RT5631_PWR_HP_L_AMP |
  414. RT5631_PWR_HP_R_AMP,
  415. RT5631_PWR_CHARGE_PUMP | RT5631_PWR_HP_L_AMP |
  416. RT5631_PWR_HP_R_AMP);
  417. /* power on soft generator and depop mode2 */
  418. snd_soc_write(codec, RT5631_DEPOP_FUN_CTRL_1,
  419. RT5631_POW_ON_SOFT_GEN | RT5631_EN_DEPOP2_FOR_HP);
  420. msleep(100);
  421. /* stop depop mode */
  422. snd_soc_update_bits(codec, RT5631_PWR_MANAG_ADD3,
  423. RT5631_PWR_HP_DEPOP_DIS, RT5631_PWR_HP_DEPOP_DIS);
  424. } else {
  425. /* config depop sequence parameter */
  426. rt5631_write_index(codec, RT5631_SPK_INTL_CTRL, 0x303F);
  427. snd_soc_write(codec, RT5631_DEPOP_FUN_CTRL_1,
  428. RT5631_POW_ON_SOFT_GEN | RT5631_EN_MUTE_UNMUTE_DEPOP |
  429. RT5631_PD_HPAMP_L_ST_UP | RT5631_PD_HPAMP_R_ST_UP);
  430. msleep(75);
  431. snd_soc_write(codec, RT5631_DEPOP_FUN_CTRL_1,
  432. RT5631_POW_ON_SOFT_GEN | RT5631_PD_HPAMP_L_ST_UP |
  433. RT5631_PD_HPAMP_R_ST_UP);
  434. /* start depop mode */
  435. snd_soc_update_bits(codec, RT5631_PWR_MANAG_ADD3,
  436. RT5631_PWR_HP_DEPOP_DIS, 0);
  437. /* config depop sequence parameter */
  438. snd_soc_write(codec, RT5631_DEPOP_FUN_CTRL_1,
  439. RT5631_POW_ON_SOFT_GEN | RT5631_EN_DEPOP2_FOR_HP |
  440. RT5631_PD_HPAMP_L_ST_UP | RT5631_PD_HPAMP_R_ST_UP);
  441. msleep(80);
  442. snd_soc_write(codec, RT5631_DEPOP_FUN_CTRL_1,
  443. RT5631_POW_ON_SOFT_GEN);
  444. /* power down headphone and charge pump */
  445. snd_soc_update_bits(codec, RT5631_PWR_MANAG_ADD3,
  446. RT5631_PWR_CHARGE_PUMP | RT5631_PWR_HP_L_AMP |
  447. RT5631_PWR_HP_R_AMP, 0);
  448. }
  449. /* recover soft volume and zero crossing setting */
  450. snd_soc_write(codec, RT5631_SOFT_VOL_CTRL, soft_vol);
  451. snd_soc_write(codec, RT5631_INT_ST_IRQ_CTRL_2, hp_zc);
  452. }
  453. /**
  454. * depop_seq_mute_stage - step by step depop sequence in mute stage.
  455. * @enable: mute/unmute
  456. *
  457. * When mute/unmute headphone, the depop sequence is done in step by step.
  458. */
  459. static void depop_seq_mute_stage(struct snd_soc_codec *codec, int enable)
  460. {
  461. unsigned int soft_vol, hp_zc;
  462. /* depop control by register */
  463. snd_soc_update_bits(codec, RT5631_DEPOP_FUN_CTRL_2,
  464. RT5631_EN_ONE_BIT_DEPOP, RT5631_EN_ONE_BIT_DEPOP);
  465. /* keep soft volume and zero crossing setting */
  466. soft_vol = snd_soc_read(codec, RT5631_SOFT_VOL_CTRL);
  467. snd_soc_write(codec, RT5631_SOFT_VOL_CTRL, 0);
  468. hp_zc = snd_soc_read(codec, RT5631_INT_ST_IRQ_CTRL_2);
  469. snd_soc_write(codec, RT5631_INT_ST_IRQ_CTRL_2, hp_zc & 0xf7ff);
  470. if (enable) {
  471. schedule_timeout_uninterruptible(msecs_to_jiffies(10));
  472. /* config depop sequence parameter */
  473. rt5631_write_index(codec, RT5631_SPK_INTL_CTRL, 0x302f);
  474. snd_soc_write(codec, RT5631_DEPOP_FUN_CTRL_1,
  475. RT5631_POW_ON_SOFT_GEN | RT5631_EN_MUTE_UNMUTE_DEPOP |
  476. RT5631_EN_HP_R_M_UN_MUTE_DEPOP |
  477. RT5631_EN_HP_L_M_UN_MUTE_DEPOP);
  478. snd_soc_update_bits(codec, RT5631_HP_OUT_VOL,
  479. RT5631_L_MUTE | RT5631_R_MUTE, 0);
  480. msleep(160);
  481. } else {
  482. /* config depop sequence parameter */
  483. rt5631_write_index(codec, RT5631_SPK_INTL_CTRL, 0x302f);
  484. snd_soc_write(codec, RT5631_DEPOP_FUN_CTRL_1,
  485. RT5631_POW_ON_SOFT_GEN | RT5631_EN_MUTE_UNMUTE_DEPOP |
  486. RT5631_EN_HP_R_M_UN_MUTE_DEPOP |
  487. RT5631_EN_HP_L_M_UN_MUTE_DEPOP);
  488. snd_soc_update_bits(codec, RT5631_HP_OUT_VOL,
  489. RT5631_L_MUTE | RT5631_R_MUTE,
  490. RT5631_L_MUTE | RT5631_R_MUTE);
  491. msleep(150);
  492. }
  493. /* recover soft volume and zero crossing setting */
  494. snd_soc_write(codec, RT5631_SOFT_VOL_CTRL, soft_vol);
  495. snd_soc_write(codec, RT5631_INT_ST_IRQ_CTRL_2, hp_zc);
  496. }
  497. static int hp_event(struct snd_soc_dapm_widget *w,
  498. struct snd_kcontrol *kcontrol, int event)
  499. {
  500. struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
  501. struct rt5631_priv *rt5631 = snd_soc_codec_get_drvdata(codec);
  502. switch (event) {
  503. case SND_SOC_DAPM_PRE_PMD:
  504. if (rt5631->codec_version) {
  505. onebit_depop_mute_stage(codec, 0);
  506. onebit_depop_power_stage(codec, 0);
  507. } else {
  508. depop_seq_mute_stage(codec, 0);
  509. depop_seq_power_stage(codec, 0);
  510. }
  511. break;
  512. case SND_SOC_DAPM_POST_PMU:
  513. if (rt5631->codec_version) {
  514. onebit_depop_power_stage(codec, 1);
  515. onebit_depop_mute_stage(codec, 1);
  516. } else {
  517. depop_seq_power_stage(codec, 1);
  518. depop_seq_mute_stage(codec, 1);
  519. }
  520. break;
  521. default:
  522. break;
  523. }
  524. return 0;
  525. }
  526. static int set_dmic_params(struct snd_soc_dapm_widget *w,
  527. struct snd_kcontrol *kcontrol, int event)
  528. {
  529. struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
  530. struct rt5631_priv *rt5631 = snd_soc_codec_get_drvdata(codec);
  531. switch (rt5631->rx_rate) {
  532. case 44100:
  533. case 48000:
  534. snd_soc_update_bits(codec, RT5631_DIG_MIC_CTRL,
  535. RT5631_DMIC_CLK_CTRL_MASK,
  536. RT5631_DMIC_CLK_CTRL_TO_32FS);
  537. break;
  538. case 32000:
  539. case 22050:
  540. snd_soc_update_bits(codec, RT5631_DIG_MIC_CTRL,
  541. RT5631_DMIC_CLK_CTRL_MASK,
  542. RT5631_DMIC_CLK_CTRL_TO_64FS);
  543. break;
  544. case 16000:
  545. case 11025:
  546. case 8000:
  547. snd_soc_update_bits(codec, RT5631_DIG_MIC_CTRL,
  548. RT5631_DMIC_CLK_CTRL_MASK,
  549. RT5631_DMIC_CLK_CTRL_TO_128FS);
  550. break;
  551. default:
  552. return -EINVAL;
  553. }
  554. return 0;
  555. }
  556. static const struct snd_kcontrol_new rt5631_recmixl_mixer_controls[] = {
  557. SOC_DAPM_SINGLE("OUTMIXL Capture Switch", RT5631_ADC_REC_MIXER,
  558. RT5631_M_OUTMIXL_RECMIXL_BIT, 1, 1),
  559. SOC_DAPM_SINGLE("MIC1_BST1 Capture Switch", RT5631_ADC_REC_MIXER,
  560. RT5631_M_MIC1_RECMIXL_BIT, 1, 1),
  561. SOC_DAPM_SINGLE("AXILVOL Capture Switch", RT5631_ADC_REC_MIXER,
  562. RT5631_M_AXIL_RECMIXL_BIT, 1, 1),
  563. SOC_DAPM_SINGLE("MONOIN_RX Capture Switch", RT5631_ADC_REC_MIXER,
  564. RT5631_M_MONO_IN_RECMIXL_BIT, 1, 1),
  565. };
  566. static const struct snd_kcontrol_new rt5631_recmixr_mixer_controls[] = {
  567. SOC_DAPM_SINGLE("MONOIN_RX Capture Switch", RT5631_ADC_REC_MIXER,
  568. RT5631_M_MONO_IN_RECMIXR_BIT, 1, 1),
  569. SOC_DAPM_SINGLE("AXIRVOL Capture Switch", RT5631_ADC_REC_MIXER,
  570. RT5631_M_AXIR_RECMIXR_BIT, 1, 1),
  571. SOC_DAPM_SINGLE("MIC2_BST2 Capture Switch", RT5631_ADC_REC_MIXER,
  572. RT5631_M_MIC2_RECMIXR_BIT, 1, 1),
  573. SOC_DAPM_SINGLE("OUTMIXR Capture Switch", RT5631_ADC_REC_MIXER,
  574. RT5631_M_OUTMIXR_RECMIXR_BIT, 1, 1),
  575. };
  576. static const struct snd_kcontrol_new rt5631_spkmixl_mixer_controls[] = {
  577. SOC_DAPM_SINGLE("RECMIXL Playback Switch", RT5631_SPK_MIXER_CTRL,
  578. RT5631_M_RECMIXL_SPKMIXL_BIT, 1, 1),
  579. SOC_DAPM_SINGLE("MIC1_P Playback Switch", RT5631_SPK_MIXER_CTRL,
  580. RT5631_M_MIC1P_SPKMIXL_BIT, 1, 1),
  581. SOC_DAPM_SINGLE("DACL Playback Switch", RT5631_SPK_MIXER_CTRL,
  582. RT5631_M_DACL_SPKMIXL_BIT, 1, 1),
  583. SOC_DAPM_SINGLE("OUTMIXL Playback Switch", RT5631_SPK_MIXER_CTRL,
  584. RT5631_M_OUTMIXL_SPKMIXL_BIT, 1, 1),
  585. };
  586. static const struct snd_kcontrol_new rt5631_spkmixr_mixer_controls[] = {
  587. SOC_DAPM_SINGLE("OUTMIXR Playback Switch", RT5631_SPK_MIXER_CTRL,
  588. RT5631_M_OUTMIXR_SPKMIXR_BIT, 1, 1),
  589. SOC_DAPM_SINGLE("DACR Playback Switch", RT5631_SPK_MIXER_CTRL,
  590. RT5631_M_DACR_SPKMIXR_BIT, 1, 1),
  591. SOC_DAPM_SINGLE("MIC2_P Playback Switch", RT5631_SPK_MIXER_CTRL,
  592. RT5631_M_MIC2P_SPKMIXR_BIT, 1, 1),
  593. SOC_DAPM_SINGLE("RECMIXR Playback Switch", RT5631_SPK_MIXER_CTRL,
  594. RT5631_M_RECMIXR_SPKMIXR_BIT, 1, 1),
  595. };
  596. static const struct snd_kcontrol_new rt5631_outmixl_mixer_controls[] = {
  597. SOC_DAPM_SINGLE("RECMIXL Playback Switch", RT5631_OUTMIXER_L_CTRL,
  598. RT5631_M_RECMIXL_OUTMIXL_BIT, 1, 1),
  599. SOC_DAPM_SINGLE("RECMIXR Playback Switch", RT5631_OUTMIXER_L_CTRL,
  600. RT5631_M_RECMIXR_OUTMIXL_BIT, 1, 1),
  601. SOC_DAPM_SINGLE("DACL Playback Switch", RT5631_OUTMIXER_L_CTRL,
  602. RT5631_M_DACL_OUTMIXL_BIT, 1, 1),
  603. SOC_DAPM_SINGLE("MIC1_BST1 Playback Switch", RT5631_OUTMIXER_L_CTRL,
  604. RT5631_M_MIC1_OUTMIXL_BIT, 1, 1),
  605. SOC_DAPM_SINGLE("MIC2_BST2 Playback Switch", RT5631_OUTMIXER_L_CTRL,
  606. RT5631_M_MIC2_OUTMIXL_BIT, 1, 1),
  607. SOC_DAPM_SINGLE("MONOIN_RXP Playback Switch", RT5631_OUTMIXER_L_CTRL,
  608. RT5631_M_MONO_INP_OUTMIXL_BIT, 1, 1),
  609. SOC_DAPM_SINGLE("AXILVOL Playback Switch", RT5631_OUTMIXER_L_CTRL,
  610. RT5631_M_AXIL_OUTMIXL_BIT, 1, 1),
  611. SOC_DAPM_SINGLE("AXIRVOL Playback Switch", RT5631_OUTMIXER_L_CTRL,
  612. RT5631_M_AXIR_OUTMIXL_BIT, 1, 1),
  613. SOC_DAPM_SINGLE("VDAC Playback Switch", RT5631_OUTMIXER_L_CTRL,
  614. RT5631_M_VDAC_OUTMIXL_BIT, 1, 1),
  615. };
  616. static const struct snd_kcontrol_new rt5631_outmixr_mixer_controls[] = {
  617. SOC_DAPM_SINGLE("VDAC Playback Switch", RT5631_OUTMIXER_R_CTRL,
  618. RT5631_M_VDAC_OUTMIXR_BIT, 1, 1),
  619. SOC_DAPM_SINGLE("AXIRVOL Playback Switch", RT5631_OUTMIXER_R_CTRL,
  620. RT5631_M_AXIR_OUTMIXR_BIT, 1, 1),
  621. SOC_DAPM_SINGLE("AXILVOL Playback Switch", RT5631_OUTMIXER_R_CTRL,
  622. RT5631_M_AXIL_OUTMIXR_BIT, 1, 1),
  623. SOC_DAPM_SINGLE("MONOIN_RXN Playback Switch", RT5631_OUTMIXER_R_CTRL,
  624. RT5631_M_MONO_INN_OUTMIXR_BIT, 1, 1),
  625. SOC_DAPM_SINGLE("MIC2_BST2 Playback Switch", RT5631_OUTMIXER_R_CTRL,
  626. RT5631_M_MIC2_OUTMIXR_BIT, 1, 1),
  627. SOC_DAPM_SINGLE("MIC1_BST1 Playback Switch", RT5631_OUTMIXER_R_CTRL,
  628. RT5631_M_MIC1_OUTMIXR_BIT, 1, 1),
  629. SOC_DAPM_SINGLE("DACR Playback Switch", RT5631_OUTMIXER_R_CTRL,
  630. RT5631_M_DACR_OUTMIXR_BIT, 1, 1),
  631. SOC_DAPM_SINGLE("RECMIXR Playback Switch", RT5631_OUTMIXER_R_CTRL,
  632. RT5631_M_RECMIXR_OUTMIXR_BIT, 1, 1),
  633. SOC_DAPM_SINGLE("RECMIXL Playback Switch", RT5631_OUTMIXER_R_CTRL,
  634. RT5631_M_RECMIXL_OUTMIXR_BIT, 1, 1),
  635. };
  636. static const struct snd_kcontrol_new rt5631_AXO1MIX_mixer_controls[] = {
  637. SOC_DAPM_SINGLE("MIC1_BST1 Playback Switch", RT5631_AXO1MIXER_CTRL,
  638. RT5631_M_MIC1_AXO1MIX_BIT , 1, 1),
  639. SOC_DAPM_SINGLE("MIC2_BST2 Playback Switch", RT5631_AXO1MIXER_CTRL,
  640. RT5631_M_MIC2_AXO1MIX_BIT, 1, 1),
  641. SOC_DAPM_SINGLE("OUTVOLL Playback Switch", RT5631_AXO1MIXER_CTRL,
  642. RT5631_M_OUTMIXL_AXO1MIX_BIT , 1 , 1),
  643. SOC_DAPM_SINGLE("OUTVOLR Playback Switch", RT5631_AXO1MIXER_CTRL,
  644. RT5631_M_OUTMIXR_AXO1MIX_BIT, 1, 1),
  645. };
  646. static const struct snd_kcontrol_new rt5631_AXO2MIX_mixer_controls[] = {
  647. SOC_DAPM_SINGLE("MIC1_BST1 Playback Switch", RT5631_AXO2MIXER_CTRL,
  648. RT5631_M_MIC1_AXO2MIX_BIT, 1, 1),
  649. SOC_DAPM_SINGLE("MIC2_BST2 Playback Switch", RT5631_AXO2MIXER_CTRL,
  650. RT5631_M_MIC2_AXO2MIX_BIT, 1, 1),
  651. SOC_DAPM_SINGLE("OUTVOLL Playback Switch", RT5631_AXO2MIXER_CTRL,
  652. RT5631_M_OUTMIXL_AXO2MIX_BIT, 1, 1),
  653. SOC_DAPM_SINGLE("OUTVOLR Playback Switch", RT5631_AXO2MIXER_CTRL,
  654. RT5631_M_OUTMIXR_AXO2MIX_BIT, 1 , 1),
  655. };
  656. static const struct snd_kcontrol_new rt5631_spolmix_mixer_controls[] = {
  657. SOC_DAPM_SINGLE("SPKVOLL Playback Switch", RT5631_SPK_MONO_OUT_CTRL,
  658. RT5631_M_SPKVOLL_SPOLMIX_BIT, 1, 1),
  659. SOC_DAPM_SINGLE("SPKVOLR Playback Switch", RT5631_SPK_MONO_OUT_CTRL,
  660. RT5631_M_SPKVOLR_SPOLMIX_BIT, 1, 1),
  661. };
  662. static const struct snd_kcontrol_new rt5631_spormix_mixer_controls[] = {
  663. SOC_DAPM_SINGLE("SPKVOLL Playback Switch", RT5631_SPK_MONO_OUT_CTRL,
  664. RT5631_M_SPKVOLL_SPORMIX_BIT, 1, 1),
  665. SOC_DAPM_SINGLE("SPKVOLR Playback Switch", RT5631_SPK_MONO_OUT_CTRL,
  666. RT5631_M_SPKVOLR_SPORMIX_BIT, 1, 1),
  667. };
  668. static const struct snd_kcontrol_new rt5631_monomix_mixer_controls[] = {
  669. SOC_DAPM_SINGLE("OUTVOLL Playback Switch", RT5631_SPK_MONO_OUT_CTRL,
  670. RT5631_M_OUTVOLL_MONOMIX_BIT, 1, 1),
  671. SOC_DAPM_SINGLE("OUTVOLR Playback Switch", RT5631_SPK_MONO_OUT_CTRL,
  672. RT5631_M_OUTVOLR_MONOMIX_BIT, 1, 1),
  673. };
  674. /* Left SPK Volume Input */
  675. static const char *rt5631_spkvoll_sel[] = {"Vmid", "SPKMIXL"};
  676. static SOC_ENUM_SINGLE_DECL(rt5631_spkvoll_enum, RT5631_SPK_OUT_VOL,
  677. RT5631_L_EN_SHIFT, rt5631_spkvoll_sel);
  678. static const struct snd_kcontrol_new rt5631_spkvoll_mux_control =
  679. SOC_DAPM_ENUM("Left SPKVOL SRC", rt5631_spkvoll_enum);
  680. /* Left HP Volume Input */
  681. static const char *rt5631_hpvoll_sel[] = {"Vmid", "OUTMIXL"};
  682. static SOC_ENUM_SINGLE_DECL(rt5631_hpvoll_enum, RT5631_HP_OUT_VOL,
  683. RT5631_L_EN_SHIFT, rt5631_hpvoll_sel);
  684. static const struct snd_kcontrol_new rt5631_hpvoll_mux_control =
  685. SOC_DAPM_ENUM("Left HPVOL SRC", rt5631_hpvoll_enum);
  686. /* Left Out Volume Input */
  687. static const char *rt5631_outvoll_sel[] = {"Vmid", "OUTMIXL"};
  688. static SOC_ENUM_SINGLE_DECL(rt5631_outvoll_enum, RT5631_MONO_AXO_1_2_VOL,
  689. RT5631_L_EN_SHIFT, rt5631_outvoll_sel);
  690. static const struct snd_kcontrol_new rt5631_outvoll_mux_control =
  691. SOC_DAPM_ENUM("Left OUTVOL SRC", rt5631_outvoll_enum);
  692. /* Right Out Volume Input */
  693. static const char *rt5631_outvolr_sel[] = {"Vmid", "OUTMIXR"};
  694. static SOC_ENUM_SINGLE_DECL(rt5631_outvolr_enum, RT5631_MONO_AXO_1_2_VOL,
  695. RT5631_R_EN_SHIFT, rt5631_outvolr_sel);
  696. static const struct snd_kcontrol_new rt5631_outvolr_mux_control =
  697. SOC_DAPM_ENUM("Right OUTVOL SRC", rt5631_outvolr_enum);
  698. /* Right HP Volume Input */
  699. static const char *rt5631_hpvolr_sel[] = {"Vmid", "OUTMIXR"};
  700. static SOC_ENUM_SINGLE_DECL(rt5631_hpvolr_enum, RT5631_HP_OUT_VOL,
  701. RT5631_R_EN_SHIFT, rt5631_hpvolr_sel);
  702. static const struct snd_kcontrol_new rt5631_hpvolr_mux_control =
  703. SOC_DAPM_ENUM("Right HPVOL SRC", rt5631_hpvolr_enum);
  704. /* Right SPK Volume Input */
  705. static const char *rt5631_spkvolr_sel[] = {"Vmid", "SPKMIXR"};
  706. static SOC_ENUM_SINGLE_DECL(rt5631_spkvolr_enum, RT5631_SPK_OUT_VOL,
  707. RT5631_R_EN_SHIFT, rt5631_spkvolr_sel);
  708. static const struct snd_kcontrol_new rt5631_spkvolr_mux_control =
  709. SOC_DAPM_ENUM("Right SPKVOL SRC", rt5631_spkvolr_enum);
  710. /* SPO Left Channel Input */
  711. static const char *rt5631_spol_src_sel[] = {
  712. "SPOLMIX", "MONOIN_RX", "VDAC", "DACL"};
  713. static SOC_ENUM_SINGLE_DECL(rt5631_spol_src_enum, RT5631_SPK_MONO_HP_OUT_CTRL,
  714. RT5631_SPK_L_MUX_SEL_SHIFT, rt5631_spol_src_sel);
  715. static const struct snd_kcontrol_new rt5631_spol_mux_control =
  716. SOC_DAPM_ENUM("SPOL SRC", rt5631_spol_src_enum);
  717. /* SPO Right Channel Input */
  718. static const char *rt5631_spor_src_sel[] = {
  719. "SPORMIX", "MONOIN_RX", "VDAC", "DACR"};
  720. static SOC_ENUM_SINGLE_DECL(rt5631_spor_src_enum, RT5631_SPK_MONO_HP_OUT_CTRL,
  721. RT5631_SPK_R_MUX_SEL_SHIFT, rt5631_spor_src_sel);
  722. static const struct snd_kcontrol_new rt5631_spor_mux_control =
  723. SOC_DAPM_ENUM("SPOR SRC", rt5631_spor_src_enum);
  724. /* MONO Input */
  725. static const char *rt5631_mono_src_sel[] = {"MONOMIX", "MONOIN_RX", "VDAC"};
  726. static SOC_ENUM_SINGLE_DECL(rt5631_mono_src_enum, RT5631_SPK_MONO_HP_OUT_CTRL,
  727. RT5631_MONO_MUX_SEL_SHIFT, rt5631_mono_src_sel);
  728. static const struct snd_kcontrol_new rt5631_mono_mux_control =
  729. SOC_DAPM_ENUM("MONO SRC", rt5631_mono_src_enum);
  730. /* Left HPO Input */
  731. static const char *rt5631_hpl_src_sel[] = {"Left HPVOL", "Left DAC"};
  732. static SOC_ENUM_SINGLE_DECL(rt5631_hpl_src_enum, RT5631_SPK_MONO_HP_OUT_CTRL,
  733. RT5631_HP_L_MUX_SEL_SHIFT, rt5631_hpl_src_sel);
  734. static const struct snd_kcontrol_new rt5631_hpl_mux_control =
  735. SOC_DAPM_ENUM("HPL SRC", rt5631_hpl_src_enum);
  736. /* Right HPO Input */
  737. static const char *rt5631_hpr_src_sel[] = {"Right HPVOL", "Right DAC"};
  738. static SOC_ENUM_SINGLE_DECL(rt5631_hpr_src_enum, RT5631_SPK_MONO_HP_OUT_CTRL,
  739. RT5631_HP_R_MUX_SEL_SHIFT, rt5631_hpr_src_sel);
  740. static const struct snd_kcontrol_new rt5631_hpr_mux_control =
  741. SOC_DAPM_ENUM("HPR SRC", rt5631_hpr_src_enum);
  742. static const struct snd_soc_dapm_widget rt5631_dapm_widgets[] = {
  743. /* Vmid */
  744. SND_SOC_DAPM_VMID("Vmid"),
  745. /* PLL1 */
  746. SND_SOC_DAPM_SUPPLY("PLL1", RT5631_PWR_MANAG_ADD2,
  747. RT5631_PWR_PLL1_BIT, 0, NULL, 0),
  748. /* Input Side */
  749. /* Input Lines */
  750. SND_SOC_DAPM_INPUT("MIC1"),
  751. SND_SOC_DAPM_INPUT("MIC2"),
  752. SND_SOC_DAPM_INPUT("AXIL"),
  753. SND_SOC_DAPM_INPUT("AXIR"),
  754. SND_SOC_DAPM_INPUT("MONOIN_RXN"),
  755. SND_SOC_DAPM_INPUT("MONOIN_RXP"),
  756. SND_SOC_DAPM_INPUT("DMIC"),
  757. /* MICBIAS */
  758. SND_SOC_DAPM_MICBIAS("MIC Bias1", RT5631_PWR_MANAG_ADD2,
  759. RT5631_PWR_MICBIAS1_VOL_BIT, 0),
  760. SND_SOC_DAPM_MICBIAS("MIC Bias2", RT5631_PWR_MANAG_ADD2,
  761. RT5631_PWR_MICBIAS2_VOL_BIT, 0),
  762. /* Boost */
  763. SND_SOC_DAPM_PGA("MIC1 Boost", RT5631_PWR_MANAG_ADD2,
  764. RT5631_PWR_MIC1_BOOT_GAIN_BIT, 0, NULL, 0),
  765. SND_SOC_DAPM_PGA("MIC2 Boost", RT5631_PWR_MANAG_ADD2,
  766. RT5631_PWR_MIC2_BOOT_GAIN_BIT, 0, NULL, 0),
  767. SND_SOC_DAPM_PGA("MONOIN_RXP Boost", RT5631_PWR_MANAG_ADD4,
  768. RT5631_PWR_MONO_IN_P_VOL_BIT, 0, NULL, 0),
  769. SND_SOC_DAPM_PGA("MONOIN_RXN Boost", RT5631_PWR_MANAG_ADD4,
  770. RT5631_PWR_MONO_IN_N_VOL_BIT, 0, NULL, 0),
  771. SND_SOC_DAPM_PGA("AXIL Boost", RT5631_PWR_MANAG_ADD4,
  772. RT5631_PWR_AXIL_IN_VOL_BIT, 0, NULL, 0),
  773. SND_SOC_DAPM_PGA("AXIR Boost", RT5631_PWR_MANAG_ADD4,
  774. RT5631_PWR_AXIR_IN_VOL_BIT, 0, NULL, 0),
  775. /* MONO In */
  776. SND_SOC_DAPM_MIXER("MONO_IN", SND_SOC_NOPM, 0, 0, NULL, 0),
  777. /* REC Mixer */
  778. SND_SOC_DAPM_MIXER("RECMIXL Mixer", RT5631_PWR_MANAG_ADD2,
  779. RT5631_PWR_RECMIXER_L_BIT, 0,
  780. &rt5631_recmixl_mixer_controls[0],
  781. ARRAY_SIZE(rt5631_recmixl_mixer_controls)),
  782. SND_SOC_DAPM_MIXER("RECMIXR Mixer", RT5631_PWR_MANAG_ADD2,
  783. RT5631_PWR_RECMIXER_R_BIT, 0,
  784. &rt5631_recmixr_mixer_controls[0],
  785. ARRAY_SIZE(rt5631_recmixr_mixer_controls)),
  786. /* Because of record duplication for L/R channel,
  787. * L/R ADCs need power up at the same time */
  788. SND_SOC_DAPM_MIXER("ADC Mixer", SND_SOC_NOPM, 0, 0, NULL, 0),
  789. /* DMIC */
  790. SND_SOC_DAPM_SUPPLY("DMIC Supply", RT5631_DIG_MIC_CTRL,
  791. RT5631_DMIC_ENA_SHIFT, 0,
  792. set_dmic_params, SND_SOC_DAPM_PRE_PMU),
  793. /* ADC Data Srouce */
  794. SND_SOC_DAPM_SUPPLY("Left ADC Select", RT5631_INT_ST_IRQ_CTRL_2,
  795. RT5631_ADC_DATA_SEL_MIC1_SHIFT, 0, NULL, 0),
  796. SND_SOC_DAPM_SUPPLY("Right ADC Select", RT5631_INT_ST_IRQ_CTRL_2,
  797. RT5631_ADC_DATA_SEL_MIC2_SHIFT, 0, NULL, 0),
  798. /* ADCs */
  799. SND_SOC_DAPM_ADC("Left ADC", "HIFI Capture",
  800. RT5631_PWR_MANAG_ADD1, RT5631_PWR_ADC_L_CLK_BIT, 0),
  801. SND_SOC_DAPM_ADC("Right ADC", "HIFI Capture",
  802. RT5631_PWR_MANAG_ADD1, RT5631_PWR_ADC_R_CLK_BIT, 0),
  803. /* DAC and ADC supply power */
  804. SND_SOC_DAPM_SUPPLY("I2S", RT5631_PWR_MANAG_ADD1,
  805. RT5631_PWR_MAIN_I2S_BIT, 0, NULL, 0),
  806. SND_SOC_DAPM_SUPPLY("DAC REF", RT5631_PWR_MANAG_ADD1,
  807. RT5631_PWR_DAC_REF_BIT, 0, NULL, 0),
  808. /* Output Side */
  809. /* DACs */
  810. SND_SOC_DAPM_DAC("Left DAC", "HIFI Playback",
  811. RT5631_PWR_MANAG_ADD1, RT5631_PWR_DAC_L_CLK_BIT, 0),
  812. SND_SOC_DAPM_DAC("Right DAC", "HIFI Playback",
  813. RT5631_PWR_MANAG_ADD1, RT5631_PWR_DAC_R_CLK_BIT, 0),
  814. SND_SOC_DAPM_DAC("Voice DAC", "Voice DAC Mono Playback",
  815. SND_SOC_NOPM, 0, 0),
  816. SND_SOC_DAPM_PGA("Voice DAC Boost", SND_SOC_NOPM, 0, 0, NULL, 0),
  817. /* DAC supply power */
  818. SND_SOC_DAPM_SUPPLY("Left DAC To Mixer", RT5631_PWR_MANAG_ADD1,
  819. RT5631_PWR_DAC_L_TO_MIXER_BIT, 0, NULL, 0),
  820. SND_SOC_DAPM_SUPPLY("Right DAC To Mixer", RT5631_PWR_MANAG_ADD1,
  821. RT5631_PWR_DAC_R_TO_MIXER_BIT, 0, NULL, 0),
  822. /* Left SPK Mixer */
  823. SND_SOC_DAPM_MIXER("SPKMIXL Mixer", RT5631_PWR_MANAG_ADD2,
  824. RT5631_PWR_SPKMIXER_L_BIT, 0,
  825. &rt5631_spkmixl_mixer_controls[0],
  826. ARRAY_SIZE(rt5631_spkmixl_mixer_controls)),
  827. /* Left Out Mixer */
  828. SND_SOC_DAPM_MIXER("OUTMIXL Mixer", RT5631_PWR_MANAG_ADD2,
  829. RT5631_PWR_OUTMIXER_L_BIT, 0,
  830. &rt5631_outmixl_mixer_controls[0],
  831. ARRAY_SIZE(rt5631_outmixl_mixer_controls)),
  832. /* Right Out Mixer */
  833. SND_SOC_DAPM_MIXER("OUTMIXR Mixer", RT5631_PWR_MANAG_ADD2,
  834. RT5631_PWR_OUTMIXER_R_BIT, 0,
  835. &rt5631_outmixr_mixer_controls[0],
  836. ARRAY_SIZE(rt5631_outmixr_mixer_controls)),
  837. /* Right SPK Mixer */
  838. SND_SOC_DAPM_MIXER("SPKMIXR Mixer", RT5631_PWR_MANAG_ADD2,
  839. RT5631_PWR_SPKMIXER_R_BIT, 0,
  840. &rt5631_spkmixr_mixer_controls[0],
  841. ARRAY_SIZE(rt5631_spkmixr_mixer_controls)),
  842. /* Volume Mux */
  843. SND_SOC_DAPM_MUX("Left SPKVOL Mux", RT5631_PWR_MANAG_ADD4,
  844. RT5631_PWR_SPK_L_VOL_BIT, 0,
  845. &rt5631_spkvoll_mux_control),
  846. SND_SOC_DAPM_MUX("Left HPVOL Mux", RT5631_PWR_MANAG_ADD4,
  847. RT5631_PWR_HP_L_OUT_VOL_BIT, 0,
  848. &rt5631_hpvoll_mux_control),
  849. SND_SOC_DAPM_MUX("Left OUTVOL Mux", RT5631_PWR_MANAG_ADD4,
  850. RT5631_PWR_LOUT_VOL_BIT, 0,
  851. &rt5631_outvoll_mux_control),
  852. SND_SOC_DAPM_MUX("Right OUTVOL Mux", RT5631_PWR_MANAG_ADD4,
  853. RT5631_PWR_ROUT_VOL_BIT, 0,
  854. &rt5631_outvolr_mux_control),
  855. SND_SOC_DAPM_MUX("Right HPVOL Mux", RT5631_PWR_MANAG_ADD4,
  856. RT5631_PWR_HP_R_OUT_VOL_BIT, 0,
  857. &rt5631_hpvolr_mux_control),
  858. SND_SOC_DAPM_MUX("Right SPKVOL Mux", RT5631_PWR_MANAG_ADD4,
  859. RT5631_PWR_SPK_R_VOL_BIT, 0,
  860. &rt5631_spkvolr_mux_control),
  861. /* DAC To HP */
  862. SND_SOC_DAPM_PGA_S("Left DAC_HP", 0, SND_SOC_NOPM, 0, 0, NULL, 0),
  863. SND_SOC_DAPM_PGA_S("Right DAC_HP", 0, SND_SOC_NOPM, 0, 0, NULL, 0),
  864. /* HP Depop */
  865. SND_SOC_DAPM_PGA_S("HP Depop", 1, SND_SOC_NOPM, 0, 0,
  866. hp_event, SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMU),
  867. /* AXO1 Mixer */
  868. SND_SOC_DAPM_MIXER("AXO1MIX Mixer", RT5631_PWR_MANAG_ADD3,
  869. RT5631_PWR_AXO1MIXER_BIT, 0,
  870. &rt5631_AXO1MIX_mixer_controls[0],
  871. ARRAY_SIZE(rt5631_AXO1MIX_mixer_controls)),
  872. /* SPOL Mixer */
  873. SND_SOC_DAPM_MIXER("SPOLMIX Mixer", SND_SOC_NOPM, 0, 0,
  874. &rt5631_spolmix_mixer_controls[0],
  875. ARRAY_SIZE(rt5631_spolmix_mixer_controls)),
  876. /* MONO Mixer */
  877. SND_SOC_DAPM_MIXER("MONOMIX Mixer", RT5631_PWR_MANAG_ADD3,
  878. RT5631_PWR_MONOMIXER_BIT, 0,
  879. &rt5631_monomix_mixer_controls[0],
  880. ARRAY_SIZE(rt5631_monomix_mixer_controls)),
  881. /* SPOR Mixer */
  882. SND_SOC_DAPM_MIXER("SPORMIX Mixer", SND_SOC_NOPM, 0, 0,
  883. &rt5631_spormix_mixer_controls[0],
  884. ARRAY_SIZE(rt5631_spormix_mixer_controls)),
  885. /* AXO2 Mixer */
  886. SND_SOC_DAPM_MIXER("AXO2MIX Mixer", RT5631_PWR_MANAG_ADD3,
  887. RT5631_PWR_AXO2MIXER_BIT, 0,
  888. &rt5631_AXO2MIX_mixer_controls[0],
  889. ARRAY_SIZE(rt5631_AXO2MIX_mixer_controls)),
  890. /* Mux */
  891. SND_SOC_DAPM_MUX("SPOL Mux", SND_SOC_NOPM, 0, 0,
  892. &rt5631_spol_mux_control),
  893. SND_SOC_DAPM_MUX("SPOR Mux", SND_SOC_NOPM, 0, 0,
  894. &rt5631_spor_mux_control),
  895. SND_SOC_DAPM_MUX("MONO Mux", SND_SOC_NOPM, 0, 0,
  896. &rt5631_mono_mux_control),
  897. SND_SOC_DAPM_MUX("HPL Mux", SND_SOC_NOPM, 0, 0,
  898. &rt5631_hpl_mux_control),
  899. SND_SOC_DAPM_MUX("HPR Mux", SND_SOC_NOPM, 0, 0,
  900. &rt5631_hpr_mux_control),
  901. /* AMP supply */
  902. SND_SOC_DAPM_SUPPLY("MONO Depop", RT5631_PWR_MANAG_ADD3,
  903. RT5631_PWR_MONO_DEPOP_DIS_BIT, 0, NULL, 0),
  904. SND_SOC_DAPM_SUPPLY("Class D", RT5631_PWR_MANAG_ADD1,
  905. RT5631_PWR_CLASS_D_BIT, 0, NULL, 0),
  906. /* Output Lines */
  907. SND_SOC_DAPM_OUTPUT("AUXO1"),
  908. SND_SOC_DAPM_OUTPUT("AUXO2"),
  909. SND_SOC_DAPM_OUTPUT("SPOL"),
  910. SND_SOC_DAPM_OUTPUT("SPOR"),
  911. SND_SOC_DAPM_OUTPUT("HPOL"),
  912. SND_SOC_DAPM_OUTPUT("HPOR"),
  913. SND_SOC_DAPM_OUTPUT("MONO"),
  914. };
  915. static const struct snd_soc_dapm_route rt5631_dapm_routes[] = {
  916. {"MIC1 Boost", NULL, "MIC1"},
  917. {"MIC2 Boost", NULL, "MIC2"},
  918. {"MONOIN_RXP Boost", NULL, "MONOIN_RXP"},
  919. {"MONOIN_RXN Boost", NULL, "MONOIN_RXN"},
  920. {"AXIL Boost", NULL, "AXIL"},
  921. {"AXIR Boost", NULL, "AXIR"},
  922. {"MONO_IN", NULL, "MONOIN_RXP Boost"},
  923. {"MONO_IN", NULL, "MONOIN_RXN Boost"},
  924. {"RECMIXL Mixer", "OUTMIXL Capture Switch", "OUTMIXL Mixer"},
  925. {"RECMIXL Mixer", "MIC1_BST1 Capture Switch", "MIC1 Boost"},
  926. {"RECMIXL Mixer", "AXILVOL Capture Switch", "AXIL Boost"},
  927. {"RECMIXL Mixer", "MONOIN_RX Capture Switch", "MONO_IN"},
  928. {"RECMIXR Mixer", "OUTMIXR Capture Switch", "OUTMIXR Mixer"},
  929. {"RECMIXR Mixer", "MIC2_BST2 Capture Switch", "MIC2 Boost"},
  930. {"RECMIXR Mixer", "AXIRVOL Capture Switch", "AXIR Boost"},
  931. {"RECMIXR Mixer", "MONOIN_RX Capture Switch", "MONO_IN"},
  932. {"ADC Mixer", NULL, "RECMIXL Mixer"},
  933. {"ADC Mixer", NULL, "RECMIXR Mixer"},
  934. {"Left ADC", NULL, "ADC Mixer"},
  935. {"Left ADC", NULL, "Left ADC Select", check_adcl_select},
  936. {"Left ADC", NULL, "PLL1", check_sysclk1_source},
  937. {"Left ADC", NULL, "I2S"},
  938. {"Left ADC", NULL, "DAC REF"},
  939. {"Right ADC", NULL, "ADC Mixer"},
  940. {"Right ADC", NULL, "Right ADC Select", check_adcr_select},
  941. {"Right ADC", NULL, "PLL1", check_sysclk1_source},
  942. {"Right ADC", NULL, "I2S"},
  943. {"Right ADC", NULL, "DAC REF"},
  944. {"DMIC", NULL, "DMIC Supply", check_dmic_used},
  945. {"Left ADC", NULL, "DMIC"},
  946. {"Right ADC", NULL, "DMIC"},
  947. {"Left DAC", NULL, "PLL1", check_sysclk1_source},
  948. {"Left DAC", NULL, "I2S"},
  949. {"Left DAC", NULL, "DAC REF"},
  950. {"Right DAC", NULL, "PLL1", check_sysclk1_source},
  951. {"Right DAC", NULL, "I2S"},
  952. {"Right DAC", NULL, "DAC REF"},
  953. {"Voice DAC Boost", NULL, "Voice DAC"},
  954. {"SPKMIXL Mixer", NULL, "Left DAC To Mixer", check_dacl_to_spkmixl},
  955. {"SPKMIXL Mixer", "RECMIXL Playback Switch", "RECMIXL Mixer"},
  956. {"SPKMIXL Mixer", "MIC1_P Playback Switch", "MIC1"},
  957. {"SPKMIXL Mixer", "DACL Playback Switch", "Left DAC"},
  958. {"SPKMIXL Mixer", "OUTMIXL Playback Switch", "OUTMIXL Mixer"},
  959. {"SPKMIXR Mixer", NULL, "Right DAC To Mixer", check_dacr_to_spkmixr},
  960. {"SPKMIXR Mixer", "OUTMIXR Playback Switch", "OUTMIXR Mixer"},
  961. {"SPKMIXR Mixer", "DACR Playback Switch", "Right DAC"},
  962. {"SPKMIXR Mixer", "MIC2_P Playback Switch", "MIC2"},
  963. {"SPKMIXR Mixer", "RECMIXR Playback Switch", "RECMIXR Mixer"},
  964. {"OUTMIXL Mixer", NULL, "Left DAC To Mixer", check_dacl_to_outmixl},
  965. {"OUTMIXL Mixer", "RECMIXL Playback Switch", "RECMIXL Mixer"},
  966. {"OUTMIXL Mixer", "RECMIXR Playback Switch", "RECMIXR Mixer"},
  967. {"OUTMIXL Mixer", "DACL Playback Switch", "Left DAC"},
  968. {"OUTMIXL Mixer", "MIC1_BST1 Playback Switch", "MIC1 Boost"},
  969. {"OUTMIXL Mixer", "MIC2_BST2 Playback Switch", "MIC2 Boost"},
  970. {"OUTMIXL Mixer", "MONOIN_RXP Playback Switch", "MONOIN_RXP Boost"},
  971. {"OUTMIXL Mixer", "AXILVOL Playback Switch", "AXIL Boost"},
  972. {"OUTMIXL Mixer", "AXIRVOL Playback Switch", "AXIR Boost"},
  973. {"OUTMIXL Mixer", "VDAC Playback Switch", "Voice DAC Boost"},
  974. {"OUTMIXR Mixer", NULL, "Right DAC To Mixer", check_dacr_to_outmixr},
  975. {"OUTMIXR Mixer", "RECMIXL Playback Switch", "RECMIXL Mixer"},
  976. {"OUTMIXR Mixer", "RECMIXR Playback Switch", "RECMIXR Mixer"},
  977. {"OUTMIXR Mixer", "DACR Playback Switch", "Right DAC"},
  978. {"OUTMIXR Mixer", "MIC1_BST1 Playback Switch", "MIC1 Boost"},
  979. {"OUTMIXR Mixer", "MIC2_BST2 Playback Switch", "MIC2 Boost"},
  980. {"OUTMIXR Mixer", "MONOIN_RXN Playback Switch", "MONOIN_RXN Boost"},
  981. {"OUTMIXR Mixer", "AXILVOL Playback Switch", "AXIL Boost"},
  982. {"OUTMIXR Mixer", "AXIRVOL Playback Switch", "AXIR Boost"},
  983. {"OUTMIXR Mixer", "VDAC Playback Switch", "Voice DAC Boost"},
  984. {"Left SPKVOL Mux", "SPKMIXL", "SPKMIXL Mixer"},
  985. {"Left SPKVOL Mux", "Vmid", "Vmid"},
  986. {"Left HPVOL Mux", "OUTMIXL", "OUTMIXL Mixer"},
  987. {"Left HPVOL Mux", "Vmid", "Vmid"},
  988. {"Left OUTVOL Mux", "OUTMIXL", "OUTMIXL Mixer"},
  989. {"Left OUTVOL Mux", "Vmid", "Vmid"},
  990. {"Right OUTVOL Mux", "OUTMIXR", "OUTMIXR Mixer"},
  991. {"Right OUTVOL Mux", "Vmid", "Vmid"},
  992. {"Right HPVOL Mux", "OUTMIXR", "OUTMIXR Mixer"},
  993. {"Right HPVOL Mux", "Vmid", "Vmid"},
  994. {"Right SPKVOL Mux", "SPKMIXR", "SPKMIXR Mixer"},
  995. {"Right SPKVOL Mux", "Vmid", "Vmid"},
  996. {"AXO1MIX Mixer", "MIC1_BST1 Playback Switch", "MIC1 Boost"},
  997. {"AXO1MIX Mixer", "OUTVOLL Playback Switch", "Left OUTVOL Mux"},
  998. {"AXO1MIX Mixer", "OUTVOLR Playback Switch", "Right OUTVOL Mux"},
  999. {"AXO1MIX Mixer", "MIC2_BST2 Playback Switch", "MIC2 Boost"},
  1000. {"AXO2MIX Mixer", "MIC1_BST1 Playback Switch", "MIC1 Boost"},
  1001. {"AXO2MIX Mixer", "OUTVOLL Playback Switch", "Left OUTVOL Mux"},
  1002. {"AXO2MIX Mixer", "OUTVOLR Playback Switch", "Right OUTVOL Mux"},
  1003. {"AXO2MIX Mixer", "MIC2_BST2 Playback Switch", "MIC2 Boost"},
  1004. {"SPOLMIX Mixer", "SPKVOLL Playback Switch", "Left SPKVOL Mux"},
  1005. {"SPOLMIX Mixer", "SPKVOLR Playback Switch", "Right SPKVOL Mux"},
  1006. {"SPORMIX Mixer", "SPKVOLL Playback Switch", "Left SPKVOL Mux"},
  1007. {"SPORMIX Mixer", "SPKVOLR Playback Switch", "Right SPKVOL Mux"},
  1008. {"MONOMIX Mixer", "OUTVOLL Playback Switch", "Left OUTVOL Mux"},
  1009. {"MONOMIX Mixer", "OUTVOLR Playback Switch", "Right OUTVOL Mux"},
  1010. {"SPOL Mux", "SPOLMIX", "SPOLMIX Mixer"},
  1011. {"SPOL Mux", "MONOIN_RX", "MONO_IN"},
  1012. {"SPOL Mux", "VDAC", "Voice DAC Boost"},
  1013. {"SPOL Mux", "DACL", "Left DAC"},
  1014. {"SPOR Mux", "SPORMIX", "SPORMIX Mixer"},
  1015. {"SPOR Mux", "MONOIN_RX", "MONO_IN"},
  1016. {"SPOR Mux", "VDAC", "Voice DAC Boost"},
  1017. {"SPOR Mux", "DACR", "Right DAC"},
  1018. {"MONO Mux", "MONOMIX", "MONOMIX Mixer"},
  1019. {"MONO Mux", "MONOIN_RX", "MONO_IN"},
  1020. {"MONO Mux", "VDAC", "Voice DAC Boost"},
  1021. {"Right DAC_HP", NULL, "Right DAC"},
  1022. {"Left DAC_HP", NULL, "Left DAC"},
  1023. {"HPL Mux", "Left HPVOL", "Left HPVOL Mux"},
  1024. {"HPL Mux", "Left DAC", "Left DAC_HP"},
  1025. {"HPR Mux", "Right HPVOL", "Right HPVOL Mux"},
  1026. {"HPR Mux", "Right DAC", "Right DAC_HP"},
  1027. {"HP Depop", NULL, "HPL Mux"},
  1028. {"HP Depop", NULL, "HPR Mux"},
  1029. {"AUXO1", NULL, "AXO1MIX Mixer"},
  1030. {"AUXO2", NULL, "AXO2MIX Mixer"},
  1031. {"SPOL", NULL, "Class D"},
  1032. {"SPOL", NULL, "SPOL Mux"},
  1033. {"SPOR", NULL, "Class D"},
  1034. {"SPOR", NULL, "SPOR Mux"},
  1035. {"HPOL", NULL, "HP Depop"},
  1036. {"HPOR", NULL, "HP Depop"},
  1037. {"MONO", NULL, "MONO Depop"},
  1038. {"MONO", NULL, "MONO Mux"},
  1039. };
  1040. struct coeff_clk_div {
  1041. u32 mclk;
  1042. u32 bclk;
  1043. u32 rate;
  1044. u16 reg_val;
  1045. };
  1046. /* PLL divisors */
  1047. struct pll_div {
  1048. u32 pll_in;
  1049. u32 pll_out;
  1050. u16 reg_val;
  1051. };
  1052. static const struct pll_div codec_master_pll_div[] = {
  1053. {2048000, 8192000, 0x0ea0},
  1054. {3686400, 8192000, 0x4e27},
  1055. {12000000, 8192000, 0x456b},
  1056. {13000000, 8192000, 0x495f},
  1057. {13100000, 8192000, 0x0320},
  1058. {2048000, 11289600, 0xf637},
  1059. {3686400, 11289600, 0x2f22},
  1060. {12000000, 11289600, 0x3e2f},
  1061. {13000000, 11289600, 0x4d5b},
  1062. {13100000, 11289600, 0x363b},
  1063. {2048000, 16384000, 0x1ea0},
  1064. {3686400, 16384000, 0x9e27},
  1065. {12000000, 16384000, 0x452b},
  1066. {13000000, 16384000, 0x542f},
  1067. {13100000, 16384000, 0x03a0},
  1068. {2048000, 16934400, 0xe625},
  1069. {3686400, 16934400, 0x9126},
  1070. {12000000, 16934400, 0x4d2c},
  1071. {13000000, 16934400, 0x742f},
  1072. {13100000, 16934400, 0x3c27},
  1073. {2048000, 22579200, 0x2aa0},
  1074. {3686400, 22579200, 0x2f20},
  1075. {12000000, 22579200, 0x7e2f},
  1076. {13000000, 22579200, 0x742f},
  1077. {13100000, 22579200, 0x3c27},
  1078. {2048000, 24576000, 0x2ea0},
  1079. {3686400, 24576000, 0xee27},
  1080. {12000000, 24576000, 0x2915},
  1081. {13000000, 24576000, 0x772e},
  1082. {13100000, 24576000, 0x0d20},
  1083. {26000000, 24576000, 0x2027},
  1084. {26000000, 22579200, 0x392f},
  1085. {24576000, 22579200, 0x0921},
  1086. {24576000, 24576000, 0x02a0},
  1087. };
  1088. static const struct pll_div codec_slave_pll_div[] = {
  1089. {256000, 2048000, 0x46f0},
  1090. {256000, 4096000, 0x3ea0},
  1091. {352800, 5644800, 0x3ea0},
  1092. {512000, 8192000, 0x3ea0},
  1093. {1024000, 8192000, 0x46f0},
  1094. {705600, 11289600, 0x3ea0},
  1095. {1024000, 16384000, 0x3ea0},
  1096. {1411200, 22579200, 0x3ea0},
  1097. {1536000, 24576000, 0x3ea0},
  1098. {2048000, 16384000, 0x1ea0},
  1099. {2822400, 22579200, 0x1ea0},
  1100. {2822400, 45158400, 0x5ec0},
  1101. {5644800, 45158400, 0x46f0},
  1102. {3072000, 24576000, 0x1ea0},
  1103. {3072000, 49152000, 0x5ec0},
  1104. {6144000, 49152000, 0x46f0},
  1105. {705600, 11289600, 0x3ea0},
  1106. {705600, 8467200, 0x3ab0},
  1107. {24576000, 24576000, 0x02a0},
  1108. {1411200, 11289600, 0x1690},
  1109. {2822400, 11289600, 0x0a90},
  1110. {1536000, 12288000, 0x1690},
  1111. {3072000, 12288000, 0x0a90},
  1112. };
  1113. static struct coeff_clk_div coeff_div[] = {
  1114. /* sysclk is 256fs */
  1115. {2048000, 8000 * 32, 8000, 0x1000},
  1116. {2048000, 8000 * 64, 8000, 0x0000},
  1117. {2822400, 11025 * 32, 11025, 0x1000},
  1118. {2822400, 11025 * 64, 11025, 0x0000},
  1119. {4096000, 16000 * 32, 16000, 0x1000},
  1120. {4096000, 16000 * 64, 16000, 0x0000},
  1121. {5644800, 22050 * 32, 22050, 0x1000},
  1122. {5644800, 22050 * 64, 22050, 0x0000},
  1123. {8192000, 32000 * 32, 32000, 0x1000},
  1124. {8192000, 32000 * 64, 32000, 0x0000},
  1125. {11289600, 44100 * 32, 44100, 0x1000},
  1126. {11289600, 44100 * 64, 44100, 0x0000},
  1127. {12288000, 48000 * 32, 48000, 0x1000},
  1128. {12288000, 48000 * 64, 48000, 0x0000},
  1129. {22579200, 88200 * 32, 88200, 0x1000},
  1130. {22579200, 88200 * 64, 88200, 0x0000},
  1131. {24576000, 96000 * 32, 96000, 0x1000},
  1132. {24576000, 96000 * 64, 96000, 0x0000},
  1133. /* sysclk is 512fs */
  1134. {4096000, 8000 * 32, 8000, 0x3000},
  1135. {4096000, 8000 * 64, 8000, 0x2000},
  1136. {5644800, 11025 * 32, 11025, 0x3000},
  1137. {5644800, 11025 * 64, 11025, 0x2000},
  1138. {8192000, 16000 * 32, 16000, 0x3000},
  1139. {8192000, 16000 * 64, 16000, 0x2000},
  1140. {11289600, 22050 * 32, 22050, 0x3000},
  1141. {11289600, 22050 * 64, 22050, 0x2000},
  1142. {16384000, 32000 * 32, 32000, 0x3000},
  1143. {16384000, 32000 * 64, 32000, 0x2000},
  1144. {22579200, 44100 * 32, 44100, 0x3000},
  1145. {22579200, 44100 * 64, 44100, 0x2000},
  1146. {24576000, 48000 * 32, 48000, 0x3000},
  1147. {24576000, 48000 * 64, 48000, 0x2000},
  1148. {45158400, 88200 * 32, 88200, 0x3000},
  1149. {45158400, 88200 * 64, 88200, 0x2000},
  1150. {49152000, 96000 * 32, 96000, 0x3000},
  1151. {49152000, 96000 * 64, 96000, 0x2000},
  1152. /* sysclk is 24.576Mhz or 22.5792Mhz */
  1153. {24576000, 8000 * 32, 8000, 0x7080},
  1154. {24576000, 8000 * 64, 8000, 0x6080},
  1155. {24576000, 16000 * 32, 16000, 0x5080},
  1156. {24576000, 16000 * 64, 16000, 0x4080},
  1157. {24576000, 24000 * 32, 24000, 0x5000},
  1158. {24576000, 24000 * 64, 24000, 0x4000},
  1159. {24576000, 32000 * 32, 32000, 0x3080},
  1160. {24576000, 32000 * 64, 32000, 0x2080},
  1161. {22579200, 11025 * 32, 11025, 0x7000},
  1162. {22579200, 11025 * 64, 11025, 0x6000},
  1163. {22579200, 22050 * 32, 22050, 0x5000},
  1164. {22579200, 22050 * 64, 22050, 0x4000},
  1165. };
  1166. static int get_coeff(int mclk, int rate, int timesofbclk)
  1167. {
  1168. int i;
  1169. for (i = 0; i < ARRAY_SIZE(coeff_div); i++) {
  1170. if (coeff_div[i].mclk == mclk && coeff_div[i].rate == rate &&
  1171. (coeff_div[i].bclk / coeff_div[i].rate) == timesofbclk)
  1172. return i;
  1173. }
  1174. return -EINVAL;
  1175. }
  1176. static int rt5631_hifi_pcm_params(struct snd_pcm_substream *substream,
  1177. struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
  1178. {
  1179. struct snd_soc_codec *codec = dai->codec;
  1180. struct rt5631_priv *rt5631 = snd_soc_codec_get_drvdata(codec);
  1181. int timesofbclk = 32, coeff;
  1182. unsigned int iface = 0;
  1183. dev_dbg(codec->dev, "enter %s\n", __func__);
  1184. rt5631->bclk_rate = snd_soc_params_to_bclk(params);
  1185. if (rt5631->bclk_rate < 0) {
  1186. dev_err(codec->dev, "Fail to get BCLK rate\n");
  1187. return rt5631->bclk_rate;
  1188. }
  1189. rt5631->rx_rate = params_rate(params);
  1190. if (rt5631->master)
  1191. coeff = get_coeff(rt5631->sysclk, rt5631->rx_rate,
  1192. rt5631->bclk_rate / rt5631->rx_rate);
  1193. else
  1194. coeff = get_coeff(rt5631->sysclk, rt5631->rx_rate,
  1195. timesofbclk);
  1196. if (coeff < 0) {
  1197. dev_err(codec->dev, "Fail to get coeff\n");
  1198. return coeff;
  1199. }
  1200. switch (params_width(params)) {
  1201. case 16:
  1202. break;
  1203. case 20:
  1204. iface |= RT5631_SDP_I2S_DL_20;
  1205. break;
  1206. case 24:
  1207. iface |= RT5631_SDP_I2S_DL_24;
  1208. break;
  1209. case 8:
  1210. iface |= RT5631_SDP_I2S_DL_8;
  1211. break;
  1212. default:
  1213. return -EINVAL;
  1214. }
  1215. snd_soc_update_bits(codec, RT5631_SDP_CTRL,
  1216. RT5631_SDP_I2S_DL_MASK, iface);
  1217. snd_soc_write(codec, RT5631_STEREO_AD_DA_CLK_CTRL,
  1218. coeff_div[coeff].reg_val);
  1219. return 0;
  1220. }
  1221. static int rt5631_hifi_codec_set_dai_fmt(struct snd_soc_dai *codec_dai,
  1222. unsigned int fmt)
  1223. {
  1224. struct snd_soc_codec *codec = codec_dai->codec;
  1225. struct rt5631_priv *rt5631 = snd_soc_codec_get_drvdata(codec);
  1226. unsigned int iface = 0;
  1227. dev_dbg(codec->dev, "enter %s\n", __func__);
  1228. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  1229. case SND_SOC_DAIFMT_CBM_CFM:
  1230. rt5631->master = 1;
  1231. break;
  1232. case SND_SOC_DAIFMT_CBS_CFS:
  1233. iface |= RT5631_SDP_MODE_SEL_SLAVE;
  1234. rt5631->master = 0;
  1235. break;
  1236. default:
  1237. return -EINVAL;
  1238. }
  1239. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  1240. case SND_SOC_DAIFMT_I2S:
  1241. break;
  1242. case SND_SOC_DAIFMT_LEFT_J:
  1243. iface |= RT5631_SDP_I2S_DF_LEFT;
  1244. break;
  1245. case SND_SOC_DAIFMT_DSP_A:
  1246. iface |= RT5631_SDP_I2S_DF_PCM_A;
  1247. break;
  1248. case SND_SOC_DAIFMT_DSP_B:
  1249. iface |= RT5631_SDP_I2S_DF_PCM_B;
  1250. break;
  1251. default:
  1252. return -EINVAL;
  1253. }
  1254. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  1255. case SND_SOC_DAIFMT_NB_NF:
  1256. break;
  1257. case SND_SOC_DAIFMT_IB_NF:
  1258. iface |= RT5631_SDP_I2S_BCLK_POL_CTRL;
  1259. break;
  1260. default:
  1261. return -EINVAL;
  1262. }
  1263. snd_soc_write(codec, RT5631_SDP_CTRL, iface);
  1264. return 0;
  1265. }
  1266. static int rt5631_hifi_codec_set_dai_sysclk(struct snd_soc_dai *codec_dai,
  1267. int clk_id, unsigned int freq, int dir)
  1268. {
  1269. struct snd_soc_codec *codec = codec_dai->codec;
  1270. struct rt5631_priv *rt5631 = snd_soc_codec_get_drvdata(codec);
  1271. dev_dbg(codec->dev, "enter %s, syclk=%d\n", __func__, freq);
  1272. if ((freq >= (256 * 8000)) && (freq <= (512 * 96000))) {
  1273. rt5631->sysclk = freq;
  1274. return 0;
  1275. }
  1276. return -EINVAL;
  1277. }
  1278. static int rt5631_codec_set_dai_pll(struct snd_soc_dai *codec_dai, int pll_id,
  1279. int source, unsigned int freq_in, unsigned int freq_out)
  1280. {
  1281. struct snd_soc_codec *codec = codec_dai->codec;
  1282. struct rt5631_priv *rt5631 = snd_soc_codec_get_drvdata(codec);
  1283. int i, ret = -EINVAL;
  1284. dev_dbg(codec->dev, "enter %s\n", __func__);
  1285. if (!freq_in || !freq_out) {
  1286. dev_dbg(codec->dev, "PLL disabled\n");
  1287. snd_soc_update_bits(codec, RT5631_GLOBAL_CLK_CTRL,
  1288. RT5631_SYSCLK_SOUR_SEL_MASK,
  1289. RT5631_SYSCLK_SOUR_SEL_MCLK);
  1290. return 0;
  1291. }
  1292. if (rt5631->master) {
  1293. for (i = 0; i < ARRAY_SIZE(codec_master_pll_div); i++)
  1294. if (freq_in == codec_master_pll_div[i].pll_in &&
  1295. freq_out == codec_master_pll_div[i].pll_out) {
  1296. dev_info(codec->dev,
  1297. "change PLL in master mode\n");
  1298. snd_soc_write(codec, RT5631_PLL_CTRL,
  1299. codec_master_pll_div[i].reg_val);
  1300. schedule_timeout_uninterruptible(
  1301. msecs_to_jiffies(20));
  1302. snd_soc_update_bits(codec,
  1303. RT5631_GLOBAL_CLK_CTRL,
  1304. RT5631_SYSCLK_SOUR_SEL_MASK |
  1305. RT5631_PLLCLK_SOUR_SEL_MASK,
  1306. RT5631_SYSCLK_SOUR_SEL_PLL |
  1307. RT5631_PLLCLK_SOUR_SEL_MCLK);
  1308. ret = 0;
  1309. break;
  1310. }
  1311. } else {
  1312. for (i = 0; i < ARRAY_SIZE(codec_slave_pll_div); i++)
  1313. if (freq_in == codec_slave_pll_div[i].pll_in &&
  1314. freq_out == codec_slave_pll_div[i].pll_out) {
  1315. dev_info(codec->dev,
  1316. "change PLL in slave mode\n");
  1317. snd_soc_write(codec, RT5631_PLL_CTRL,
  1318. codec_slave_pll_div[i].reg_val);
  1319. schedule_timeout_uninterruptible(
  1320. msecs_to_jiffies(20));
  1321. snd_soc_update_bits(codec,
  1322. RT5631_GLOBAL_CLK_CTRL,
  1323. RT5631_SYSCLK_SOUR_SEL_MASK |
  1324. RT5631_PLLCLK_SOUR_SEL_MASK,
  1325. RT5631_SYSCLK_SOUR_SEL_PLL |
  1326. RT5631_PLLCLK_SOUR_SEL_BCLK);
  1327. ret = 0;
  1328. break;
  1329. }
  1330. }
  1331. return ret;
  1332. }
  1333. static int rt5631_set_bias_level(struct snd_soc_codec *codec,
  1334. enum snd_soc_bias_level level)
  1335. {
  1336. struct rt5631_priv *rt5631 = snd_soc_codec_get_drvdata(codec);
  1337. switch (level) {
  1338. case SND_SOC_BIAS_ON:
  1339. case SND_SOC_BIAS_PREPARE:
  1340. snd_soc_update_bits(codec, RT5631_PWR_MANAG_ADD2,
  1341. RT5631_PWR_MICBIAS1_VOL | RT5631_PWR_MICBIAS2_VOL,
  1342. RT5631_PWR_MICBIAS1_VOL | RT5631_PWR_MICBIAS2_VOL);
  1343. break;
  1344. case SND_SOC_BIAS_STANDBY:
  1345. if (snd_soc_codec_get_bias_level(codec) == SND_SOC_BIAS_OFF) {
  1346. snd_soc_update_bits(codec, RT5631_PWR_MANAG_ADD3,
  1347. RT5631_PWR_VREF | RT5631_PWR_MAIN_BIAS,
  1348. RT5631_PWR_VREF | RT5631_PWR_MAIN_BIAS);
  1349. msleep(80);
  1350. snd_soc_update_bits(codec, RT5631_PWR_MANAG_ADD3,
  1351. RT5631_PWR_FAST_VREF_CTRL,
  1352. RT5631_PWR_FAST_VREF_CTRL);
  1353. regcache_cache_only(rt5631->regmap, false);
  1354. regcache_sync(rt5631->regmap);
  1355. }
  1356. break;
  1357. case SND_SOC_BIAS_OFF:
  1358. snd_soc_write(codec, RT5631_PWR_MANAG_ADD1, 0x0000);
  1359. snd_soc_write(codec, RT5631_PWR_MANAG_ADD2, 0x0000);
  1360. snd_soc_write(codec, RT5631_PWR_MANAG_ADD3, 0x0000);
  1361. snd_soc_write(codec, RT5631_PWR_MANAG_ADD4, 0x0000);
  1362. break;
  1363. default:
  1364. break;
  1365. }
  1366. return 0;
  1367. }
  1368. static int rt5631_probe(struct snd_soc_codec *codec)
  1369. {
  1370. struct rt5631_priv *rt5631 = snd_soc_codec_get_drvdata(codec);
  1371. unsigned int val;
  1372. val = rt5631_read_index(codec, RT5631_ADDA_MIXER_INTL_REG3);
  1373. if (val & 0x0002)
  1374. rt5631->codec_version = 1;
  1375. else
  1376. rt5631->codec_version = 0;
  1377. rt5631_reset(codec);
  1378. snd_soc_update_bits(codec, RT5631_PWR_MANAG_ADD3,
  1379. RT5631_PWR_VREF | RT5631_PWR_MAIN_BIAS,
  1380. RT5631_PWR_VREF | RT5631_PWR_MAIN_BIAS);
  1381. msleep(80);
  1382. snd_soc_update_bits(codec, RT5631_PWR_MANAG_ADD3,
  1383. RT5631_PWR_FAST_VREF_CTRL, RT5631_PWR_FAST_VREF_CTRL);
  1384. /* enable HP zero cross */
  1385. snd_soc_write(codec, RT5631_INT_ST_IRQ_CTRL_2, 0x0f18);
  1386. /* power off ClassD auto Recovery */
  1387. if (rt5631->codec_version)
  1388. snd_soc_update_bits(codec, RT5631_INT_ST_IRQ_CTRL_2,
  1389. 0x2000, 0x2000);
  1390. else
  1391. snd_soc_update_bits(codec, RT5631_INT_ST_IRQ_CTRL_2,
  1392. 0x2000, 0);
  1393. /* DMIC */
  1394. if (rt5631->dmic_used_flag) {
  1395. snd_soc_update_bits(codec, RT5631_GPIO_CTRL,
  1396. RT5631_GPIO_PIN_FUN_SEL_MASK |
  1397. RT5631_GPIO_DMIC_FUN_SEL_MASK,
  1398. RT5631_GPIO_PIN_FUN_SEL_GPIO_DIMC |
  1399. RT5631_GPIO_DMIC_FUN_SEL_DIMC);
  1400. snd_soc_update_bits(codec, RT5631_DIG_MIC_CTRL,
  1401. RT5631_DMIC_L_CH_LATCH_MASK |
  1402. RT5631_DMIC_R_CH_LATCH_MASK,
  1403. RT5631_DMIC_L_CH_LATCH_FALLING |
  1404. RT5631_DMIC_R_CH_LATCH_RISING);
  1405. }
  1406. snd_soc_codec_init_bias_level(codec, SND_SOC_BIAS_STANDBY);
  1407. return 0;
  1408. }
  1409. #define RT5631_STEREO_RATES SNDRV_PCM_RATE_8000_96000
  1410. #define RT5631_FORMAT (SNDRV_PCM_FMTBIT_S16_LE | \
  1411. SNDRV_PCM_FMTBIT_S20_3LE | \
  1412. SNDRV_PCM_FMTBIT_S24_LE | \
  1413. SNDRV_PCM_FMTBIT_S8)
  1414. static const struct snd_soc_dai_ops rt5631_ops = {
  1415. .hw_params = rt5631_hifi_pcm_params,
  1416. .set_fmt = rt5631_hifi_codec_set_dai_fmt,
  1417. .set_sysclk = rt5631_hifi_codec_set_dai_sysclk,
  1418. .set_pll = rt5631_codec_set_dai_pll,
  1419. };
  1420. static struct snd_soc_dai_driver rt5631_dai[] = {
  1421. {
  1422. .name = "rt5631-hifi",
  1423. .id = 1,
  1424. .playback = {
  1425. .stream_name = "HIFI Playback",
  1426. .channels_min = 1,
  1427. .channels_max = 2,
  1428. .rates = RT5631_STEREO_RATES,
  1429. .formats = RT5631_FORMAT,
  1430. },
  1431. .capture = {
  1432. .stream_name = "HIFI Capture",
  1433. .channels_min = 1,
  1434. .channels_max = 2,
  1435. .rates = RT5631_STEREO_RATES,
  1436. .formats = RT5631_FORMAT,
  1437. },
  1438. .ops = &rt5631_ops,
  1439. },
  1440. };
  1441. static struct snd_soc_codec_driver soc_codec_dev_rt5631 = {
  1442. .probe = rt5631_probe,
  1443. .set_bias_level = rt5631_set_bias_level,
  1444. .suspend_bias_off = true,
  1445. .controls = rt5631_snd_controls,
  1446. .num_controls = ARRAY_SIZE(rt5631_snd_controls),
  1447. .dapm_widgets = rt5631_dapm_widgets,
  1448. .num_dapm_widgets = ARRAY_SIZE(rt5631_dapm_widgets),
  1449. .dapm_routes = rt5631_dapm_routes,
  1450. .num_dapm_routes = ARRAY_SIZE(rt5631_dapm_routes),
  1451. };
  1452. static const struct i2c_device_id rt5631_i2c_id[] = {
  1453. { "rt5631", 0 },
  1454. { "alc5631", 0 },
  1455. { }
  1456. };
  1457. MODULE_DEVICE_TABLE(i2c, rt5631_i2c_id);
  1458. #ifdef CONFIG_OF
  1459. static const struct of_device_id rt5631_i2c_dt_ids[] = {
  1460. { .compatible = "realtek,rt5631"},
  1461. { .compatible = "realtek,alc5631"},
  1462. { }
  1463. };
  1464. MODULE_DEVICE_TABLE(of, rt5631_i2c_dt_ids);
  1465. #endif
  1466. static const struct regmap_config rt5631_regmap_config = {
  1467. .reg_bits = 8,
  1468. .val_bits = 16,
  1469. .readable_reg = rt5631_readable_register,
  1470. .volatile_reg = rt5631_volatile_register,
  1471. .max_register = RT5631_VENDOR_ID2,
  1472. .reg_defaults = rt5631_reg,
  1473. .num_reg_defaults = ARRAY_SIZE(rt5631_reg),
  1474. .cache_type = REGCACHE_RBTREE,
  1475. };
  1476. static int rt5631_i2c_probe(struct i2c_client *i2c,
  1477. const struct i2c_device_id *id)
  1478. {
  1479. struct rt5631_priv *rt5631;
  1480. int ret;
  1481. rt5631 = devm_kzalloc(&i2c->dev, sizeof(struct rt5631_priv),
  1482. GFP_KERNEL);
  1483. if (NULL == rt5631)
  1484. return -ENOMEM;
  1485. i2c_set_clientdata(i2c, rt5631);
  1486. rt5631->regmap = devm_regmap_init_i2c(i2c, &rt5631_regmap_config);
  1487. if (IS_ERR(rt5631->regmap))
  1488. return PTR_ERR(rt5631->regmap);
  1489. ret = snd_soc_register_codec(&i2c->dev, &soc_codec_dev_rt5631,
  1490. rt5631_dai, ARRAY_SIZE(rt5631_dai));
  1491. return ret;
  1492. }
  1493. static int rt5631_i2c_remove(struct i2c_client *client)
  1494. {
  1495. snd_soc_unregister_codec(&client->dev);
  1496. return 0;
  1497. }
  1498. static struct i2c_driver rt5631_i2c_driver = {
  1499. .driver = {
  1500. .name = "rt5631",
  1501. .of_match_table = of_match_ptr(rt5631_i2c_dt_ids),
  1502. },
  1503. .probe = rt5631_i2c_probe,
  1504. .remove = rt5631_i2c_remove,
  1505. .id_table = rt5631_i2c_id,
  1506. };
  1507. module_i2c_driver(rt5631_i2c_driver);
  1508. MODULE_DESCRIPTION("ASoC RT5631 driver");
  1509. MODULE_AUTHOR("flove <flove@realtek.com>");
  1510. MODULE_LICENSE("GPL");