rt5660.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354
  1. /*
  2. * rt5660.c -- RT5660 ALSA SoC audio codec driver
  3. *
  4. * Copyright 2016 Realtek Semiconductor Corp.
  5. * Author: Oder Chiou <oder_chiou@realtek.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/module.h>
  12. #include <linux/moduleparam.h>
  13. #include <linux/init.h>
  14. #include <linux/delay.h>
  15. #include <linux/pm.h>
  16. #include <linux/gpio.h>
  17. #include <linux/i2c.h>
  18. #include <linux/regmap.h>
  19. #include <linux/of.h>
  20. #include <linux/of_gpio.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/spi/spi.h>
  23. #include <linux/acpi.h>
  24. #include <sound/core.h>
  25. #include <sound/pcm.h>
  26. #include <sound/pcm_params.h>
  27. #include <sound/soc.h>
  28. #include <sound/soc-dapm.h>
  29. #include <sound/initval.h>
  30. #include <sound/tlv.h>
  31. #include "rl6231.h"
  32. #include "rt5660.h"
  33. #define RT5660_DEVICE_ID 0x6338
  34. #define RT5660_PR_RANGE_BASE (0xff + 1)
  35. #define RT5660_PR_SPACING 0x100
  36. #define RT5660_PR_BASE (RT5660_PR_RANGE_BASE + (0 * RT5660_PR_SPACING))
  37. static const struct regmap_range_cfg rt5660_ranges[] = {
  38. { .name = "PR", .range_min = RT5660_PR_BASE,
  39. .range_max = RT5660_PR_BASE + 0xf3,
  40. .selector_reg = RT5660_PRIV_INDEX,
  41. .selector_mask = 0xff,
  42. .selector_shift = 0x0,
  43. .window_start = RT5660_PRIV_DATA,
  44. .window_len = 0x1, },
  45. };
  46. static const struct reg_sequence rt5660_patch[] = {
  47. { RT5660_ALC_PGA_CTRL2, 0x44c3 },
  48. { RT5660_PR_BASE + 0x3d, 0x2600 },
  49. };
  50. static const struct reg_default rt5660_reg[] = {
  51. { 0x00, 0x0000 },
  52. { 0x01, 0xc800 },
  53. { 0x02, 0xc8c8 },
  54. { 0x0d, 0x1010 },
  55. { 0x0e, 0x1010 },
  56. { 0x19, 0xafaf },
  57. { 0x1c, 0x2f2f },
  58. { 0x1e, 0x0000 },
  59. { 0x27, 0x6060 },
  60. { 0x29, 0x8080 },
  61. { 0x2a, 0x4242 },
  62. { 0x2f, 0x0000 },
  63. { 0x3b, 0x0000 },
  64. { 0x3c, 0x007f },
  65. { 0x3d, 0x0000 },
  66. { 0x3e, 0x007f },
  67. { 0x45, 0xe000 },
  68. { 0x46, 0x003e },
  69. { 0x48, 0xf800 },
  70. { 0x4a, 0x0004 },
  71. { 0x4d, 0x0000 },
  72. { 0x4e, 0x0000 },
  73. { 0x4f, 0x01ff },
  74. { 0x50, 0x0000 },
  75. { 0x51, 0x0000 },
  76. { 0x52, 0x01ff },
  77. { 0x61, 0x0000 },
  78. { 0x62, 0x0000 },
  79. { 0x63, 0x00c0 },
  80. { 0x64, 0x0000 },
  81. { 0x65, 0x0000 },
  82. { 0x66, 0x0000 },
  83. { 0x70, 0x8000 },
  84. { 0x73, 0x7000 },
  85. { 0x74, 0x3c00 },
  86. { 0x75, 0x2800 },
  87. { 0x80, 0x0000 },
  88. { 0x81, 0x0000 },
  89. { 0x82, 0x0000 },
  90. { 0x8c, 0x0228 },
  91. { 0x8d, 0xa000 },
  92. { 0x8e, 0x0000 },
  93. { 0x92, 0x0000 },
  94. { 0x93, 0x3000 },
  95. { 0xa1, 0x0059 },
  96. { 0xa2, 0x0001 },
  97. { 0xa3, 0x5c80 },
  98. { 0xa4, 0x0146 },
  99. { 0xa5, 0x1f1f },
  100. { 0xa6, 0x78c6 },
  101. { 0xa7, 0xe5ec },
  102. { 0xa8, 0xba61 },
  103. { 0xa9, 0x3c78 },
  104. { 0xaa, 0x8ae2 },
  105. { 0xab, 0xe5ec },
  106. { 0xac, 0xc600 },
  107. { 0xad, 0xba61 },
  108. { 0xae, 0x17ed },
  109. { 0xb0, 0x2080 },
  110. { 0xb1, 0x0000 },
  111. { 0xb3, 0x001f },
  112. { 0xb4, 0x020c },
  113. { 0xb5, 0x1f00 },
  114. { 0xb6, 0x0000 },
  115. { 0xb7, 0x4000 },
  116. { 0xbb, 0x0000 },
  117. { 0xbd, 0x0000 },
  118. { 0xbe, 0x0000 },
  119. { 0xbf, 0x0100 },
  120. { 0xc0, 0x0000 },
  121. { 0xc2, 0x0000 },
  122. { 0xd3, 0xa220 },
  123. { 0xd9, 0x0809 },
  124. { 0xda, 0x0000 },
  125. { 0xe0, 0x8000 },
  126. { 0xe1, 0x0200 },
  127. { 0xe2, 0x8000 },
  128. { 0xe3, 0x0200 },
  129. { 0xe4, 0x0f20 },
  130. { 0xe5, 0x001f },
  131. { 0xe6, 0x020c },
  132. { 0xe7, 0x1f00 },
  133. { 0xe8, 0x0000 },
  134. { 0xe9, 0x4000 },
  135. { 0xea, 0x00a6 },
  136. { 0xeb, 0x04c3 },
  137. { 0xec, 0x27c8 },
  138. { 0xed, 0x7418 },
  139. { 0xee, 0xbf50 },
  140. { 0xef, 0x0045 },
  141. { 0xf0, 0x0007 },
  142. { 0xfa, 0x0000 },
  143. { 0xfd, 0x0000 },
  144. { 0xfe, 0x10ec },
  145. { 0xff, 0x6338 },
  146. };
  147. static bool rt5660_volatile_register(struct device *dev, unsigned int reg)
  148. {
  149. int i;
  150. for (i = 0; i < ARRAY_SIZE(rt5660_ranges); i++)
  151. if ((reg >= rt5660_ranges[i].window_start &&
  152. reg <= rt5660_ranges[i].window_start +
  153. rt5660_ranges[i].window_len) ||
  154. (reg >= rt5660_ranges[i].range_min &&
  155. reg <= rt5660_ranges[i].range_max))
  156. return true;
  157. switch (reg) {
  158. case RT5660_RESET:
  159. case RT5660_PRIV_DATA:
  160. case RT5660_EQ_CTRL1:
  161. case RT5660_IRQ_CTRL2:
  162. case RT5660_INT_IRQ_ST:
  163. case RT5660_VENDOR_ID:
  164. case RT5660_VENDOR_ID1:
  165. case RT5660_VENDOR_ID2:
  166. return true;
  167. default:
  168. return false;
  169. }
  170. }
  171. static bool rt5660_readable_register(struct device *dev, unsigned int reg)
  172. {
  173. int i;
  174. for (i = 0; i < ARRAY_SIZE(rt5660_ranges); i++)
  175. if ((reg >= rt5660_ranges[i].window_start &&
  176. reg <= rt5660_ranges[i].window_start +
  177. rt5660_ranges[i].window_len) ||
  178. (reg >= rt5660_ranges[i].range_min &&
  179. reg <= rt5660_ranges[i].range_max))
  180. return true;
  181. switch (reg) {
  182. case RT5660_RESET:
  183. case RT5660_SPK_VOL:
  184. case RT5660_LOUT_VOL:
  185. case RT5660_IN1_IN2:
  186. case RT5660_IN3_IN4:
  187. case RT5660_DAC1_DIG_VOL:
  188. case RT5660_STO1_ADC_DIG_VOL:
  189. case RT5660_ADC_BST_VOL1:
  190. case RT5660_STO1_ADC_MIXER:
  191. case RT5660_AD_DA_MIXER:
  192. case RT5660_STO_DAC_MIXER:
  193. case RT5660_DIG_INF1_DATA:
  194. case RT5660_REC_L1_MIXER:
  195. case RT5660_REC_L2_MIXER:
  196. case RT5660_REC_R1_MIXER:
  197. case RT5660_REC_R2_MIXER:
  198. case RT5660_LOUT_MIXER:
  199. case RT5660_SPK_MIXER:
  200. case RT5660_SPO_MIXER:
  201. case RT5660_SPO_CLSD_RATIO:
  202. case RT5660_OUT_L_GAIN1:
  203. case RT5660_OUT_L_GAIN2:
  204. case RT5660_OUT_L1_MIXER:
  205. case RT5660_OUT_R_GAIN1:
  206. case RT5660_OUT_R_GAIN2:
  207. case RT5660_OUT_R1_MIXER:
  208. case RT5660_PWR_DIG1:
  209. case RT5660_PWR_DIG2:
  210. case RT5660_PWR_ANLG1:
  211. case RT5660_PWR_ANLG2:
  212. case RT5660_PWR_MIXER:
  213. case RT5660_PWR_VOL:
  214. case RT5660_PRIV_INDEX:
  215. case RT5660_PRIV_DATA:
  216. case RT5660_I2S1_SDP:
  217. case RT5660_ADDA_CLK1:
  218. case RT5660_ADDA_CLK2:
  219. case RT5660_DMIC_CTRL1:
  220. case RT5660_GLB_CLK:
  221. case RT5660_PLL_CTRL1:
  222. case RT5660_PLL_CTRL2:
  223. case RT5660_CLSD_AMP_OC_CTRL:
  224. case RT5660_CLSD_AMP_CTRL:
  225. case RT5660_LOUT_AMP_CTRL:
  226. case RT5660_SPK_AMP_SPKVDD:
  227. case RT5660_MICBIAS:
  228. case RT5660_CLSD_OUT_CTRL1:
  229. case RT5660_CLSD_OUT_CTRL2:
  230. case RT5660_DIPOLE_MIC_CTRL1:
  231. case RT5660_DIPOLE_MIC_CTRL2:
  232. case RT5660_DIPOLE_MIC_CTRL3:
  233. case RT5660_DIPOLE_MIC_CTRL4:
  234. case RT5660_DIPOLE_MIC_CTRL5:
  235. case RT5660_DIPOLE_MIC_CTRL6:
  236. case RT5660_DIPOLE_MIC_CTRL7:
  237. case RT5660_DIPOLE_MIC_CTRL8:
  238. case RT5660_DIPOLE_MIC_CTRL9:
  239. case RT5660_DIPOLE_MIC_CTRL10:
  240. case RT5660_DIPOLE_MIC_CTRL11:
  241. case RT5660_DIPOLE_MIC_CTRL12:
  242. case RT5660_EQ_CTRL1:
  243. case RT5660_EQ_CTRL2:
  244. case RT5660_DRC_AGC_CTRL1:
  245. case RT5660_DRC_AGC_CTRL2:
  246. case RT5660_DRC_AGC_CTRL3:
  247. case RT5660_DRC_AGC_CTRL4:
  248. case RT5660_DRC_AGC_CTRL5:
  249. case RT5660_JD_CTRL:
  250. case RT5660_IRQ_CTRL1:
  251. case RT5660_IRQ_CTRL2:
  252. case RT5660_INT_IRQ_ST:
  253. case RT5660_GPIO_CTRL1:
  254. case RT5660_GPIO_CTRL2:
  255. case RT5660_WIND_FILTER_CTRL1:
  256. case RT5660_SV_ZCD1:
  257. case RT5660_SV_ZCD2:
  258. case RT5660_DRC1_LM_CTRL1:
  259. case RT5660_DRC1_LM_CTRL2:
  260. case RT5660_DRC2_LM_CTRL1:
  261. case RT5660_DRC2_LM_CTRL2:
  262. case RT5660_MULTI_DRC_CTRL:
  263. case RT5660_DRC2_CTRL1:
  264. case RT5660_DRC2_CTRL2:
  265. case RT5660_DRC2_CTRL3:
  266. case RT5660_DRC2_CTRL4:
  267. case RT5660_DRC2_CTRL5:
  268. case RT5660_ALC_PGA_CTRL1:
  269. case RT5660_ALC_PGA_CTRL2:
  270. case RT5660_ALC_PGA_CTRL3:
  271. case RT5660_ALC_PGA_CTRL4:
  272. case RT5660_ALC_PGA_CTRL5:
  273. case RT5660_ALC_PGA_CTRL6:
  274. case RT5660_ALC_PGA_CTRL7:
  275. case RT5660_GEN_CTRL1:
  276. case RT5660_GEN_CTRL2:
  277. case RT5660_GEN_CTRL3:
  278. case RT5660_VENDOR_ID:
  279. case RT5660_VENDOR_ID1:
  280. case RT5660_VENDOR_ID2:
  281. return true;
  282. default:
  283. return false;
  284. }
  285. }
  286. static const DECLARE_TLV_DB_SCALE(rt5660_out_vol_tlv, -4650, 150, 0);
  287. static const DECLARE_TLV_DB_SCALE(rt5660_dac_vol_tlv, -6525, 75, 0);
  288. static const DECLARE_TLV_DB_SCALE(rt5660_adc_vol_tlv, -1725, 75, 0);
  289. static const DECLARE_TLV_DB_SCALE(rt5660_adc_bst_tlv, 0, 1200, 0);
  290. static const DECLARE_TLV_DB_SCALE(rt5660_bst_tlv, -1200, 75, 0);
  291. static const struct snd_kcontrol_new rt5660_snd_controls[] = {
  292. /* Speaker Output Volume */
  293. SOC_SINGLE("Speaker Playback Switch", RT5660_SPK_VOL, RT5660_L_MUTE_SFT,
  294. 1, 1),
  295. SOC_SINGLE_TLV("Speaker Playback Volume", RT5660_SPK_VOL,
  296. RT5660_L_VOL_SFT, 39, 1, rt5660_out_vol_tlv),
  297. /* OUTPUT Control */
  298. SOC_DOUBLE("OUT Playback Switch", RT5660_LOUT_VOL, RT5660_L_MUTE_SFT,
  299. RT5660_R_MUTE_SFT, 1, 1),
  300. SOC_DOUBLE_TLV("OUT Playback Volume", RT5660_LOUT_VOL, RT5660_L_VOL_SFT,
  301. RT5660_R_VOL_SFT, 39, 1, rt5660_out_vol_tlv),
  302. /* DAC Digital Volume */
  303. SOC_DOUBLE_TLV("DAC1 Playback Volume", RT5660_DAC1_DIG_VOL,
  304. RT5660_DAC_L1_VOL_SFT, RT5660_DAC_R1_VOL_SFT, 87, 0,
  305. rt5660_dac_vol_tlv),
  306. /* IN1/IN2/IN3 Control */
  307. SOC_SINGLE_TLV("IN1 Boost Volume", RT5660_IN1_IN2, RT5660_BST_SFT1, 69,
  308. 0, rt5660_bst_tlv),
  309. SOC_SINGLE_TLV("IN2 Boost Volume", RT5660_IN1_IN2, RT5660_BST_SFT2, 69,
  310. 0, rt5660_bst_tlv),
  311. SOC_SINGLE_TLV("IN3 Boost Volume", RT5660_IN3_IN4, RT5660_BST_SFT3, 69,
  312. 0, rt5660_bst_tlv),
  313. /* ADC Digital Volume Control */
  314. SOC_DOUBLE("ADC Capture Switch", RT5660_STO1_ADC_DIG_VOL,
  315. RT5660_L_MUTE_SFT, RT5660_R_MUTE_SFT, 1, 1),
  316. SOC_DOUBLE_TLV("ADC Capture Volume", RT5660_STO1_ADC_DIG_VOL,
  317. RT5660_ADC_L_VOL_SFT, RT5660_ADC_R_VOL_SFT, 63, 0,
  318. rt5660_adc_vol_tlv),
  319. /* ADC Boost Volume Control */
  320. SOC_DOUBLE_TLV("STO1 ADC Boost Gain Volume", RT5660_ADC_BST_VOL1,
  321. RT5660_STO1_ADC_L_BST_SFT, RT5660_STO1_ADC_R_BST_SFT, 3, 0,
  322. rt5660_adc_bst_tlv),
  323. };
  324. /**
  325. * rt5660_set_dmic_clk - Set parameter of dmic.
  326. *
  327. * @w: DAPM widget.
  328. * @kcontrol: The kcontrol of this widget.
  329. * @event: Event id.
  330. *
  331. */
  332. static int rt5660_set_dmic_clk(struct snd_soc_dapm_widget *w,
  333. struct snd_kcontrol *kcontrol, int event)
  334. {
  335. struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
  336. struct rt5660_priv *rt5660 = snd_soc_codec_get_drvdata(codec);
  337. int idx, rate;
  338. rate = rt5660->sysclk / rl6231_get_pre_div(rt5660->regmap,
  339. RT5660_ADDA_CLK1, RT5660_I2S_PD1_SFT);
  340. idx = rl6231_calc_dmic_clk(rate);
  341. if (idx < 0)
  342. dev_err(codec->dev, "Failed to set DMIC clock\n");
  343. else
  344. snd_soc_update_bits(codec, RT5660_DMIC_CTRL1,
  345. RT5660_DMIC_CLK_MASK, idx << RT5660_DMIC_CLK_SFT);
  346. return idx;
  347. }
  348. static int rt5660_is_sys_clk_from_pll(struct snd_soc_dapm_widget *source,
  349. struct snd_soc_dapm_widget *sink)
  350. {
  351. struct snd_soc_codec *codec = snd_soc_dapm_to_codec(source->dapm);
  352. unsigned int val;
  353. val = snd_soc_read(codec, RT5660_GLB_CLK);
  354. val &= RT5660_SCLK_SRC_MASK;
  355. if (val == RT5660_SCLK_SRC_PLL1)
  356. return 1;
  357. else
  358. return 0;
  359. }
  360. /* Digital Mixer */
  361. static const struct snd_kcontrol_new rt5660_sto1_adc_l_mix[] = {
  362. SOC_DAPM_SINGLE("ADC1 Switch", RT5660_STO1_ADC_MIXER,
  363. RT5660_M_ADC_L1_SFT, 1, 1),
  364. SOC_DAPM_SINGLE("ADC2 Switch", RT5660_STO1_ADC_MIXER,
  365. RT5660_M_ADC_L2_SFT, 1, 1),
  366. };
  367. static const struct snd_kcontrol_new rt5660_sto1_adc_r_mix[] = {
  368. SOC_DAPM_SINGLE("ADC1 Switch", RT5660_STO1_ADC_MIXER,
  369. RT5660_M_ADC_R1_SFT, 1, 1),
  370. SOC_DAPM_SINGLE("ADC2 Switch", RT5660_STO1_ADC_MIXER,
  371. RT5660_M_ADC_R2_SFT, 1, 1),
  372. };
  373. static const struct snd_kcontrol_new rt5660_dac_l_mix[] = {
  374. SOC_DAPM_SINGLE("Stereo ADC Switch", RT5660_AD_DA_MIXER,
  375. RT5660_M_ADCMIX_L_SFT, 1, 1),
  376. SOC_DAPM_SINGLE("DAC1 Switch", RT5660_AD_DA_MIXER,
  377. RT5660_M_DAC1_L_SFT, 1, 1),
  378. };
  379. static const struct snd_kcontrol_new rt5660_dac_r_mix[] = {
  380. SOC_DAPM_SINGLE("Stereo ADC Switch", RT5660_AD_DA_MIXER,
  381. RT5660_M_ADCMIX_R_SFT, 1, 1),
  382. SOC_DAPM_SINGLE("DAC1 Switch", RT5660_AD_DA_MIXER,
  383. RT5660_M_DAC1_R_SFT, 1, 1),
  384. };
  385. static const struct snd_kcontrol_new rt5660_sto_dac_l_mix[] = {
  386. SOC_DAPM_SINGLE("DAC L1 Switch", RT5660_STO_DAC_MIXER,
  387. RT5660_M_DAC_L1_SFT, 1, 1),
  388. SOC_DAPM_SINGLE("DAC R1 Switch", RT5660_STO_DAC_MIXER,
  389. RT5660_M_DAC_R1_STO_L_SFT, 1, 1),
  390. };
  391. static const struct snd_kcontrol_new rt5660_sto_dac_r_mix[] = {
  392. SOC_DAPM_SINGLE("DAC R1 Switch", RT5660_STO_DAC_MIXER,
  393. RT5660_M_DAC_R1_SFT, 1, 1),
  394. SOC_DAPM_SINGLE("DAC L1 Switch", RT5660_STO_DAC_MIXER,
  395. RT5660_M_DAC_L1_STO_R_SFT, 1, 1),
  396. };
  397. /* Analog Input Mixer */
  398. static const struct snd_kcontrol_new rt5660_rec_l_mix[] = {
  399. SOC_DAPM_SINGLE("BST3 Switch", RT5660_REC_L2_MIXER,
  400. RT5660_M_BST3_RM_L_SFT, 1, 1),
  401. SOC_DAPM_SINGLE("BST2 Switch", RT5660_REC_L2_MIXER,
  402. RT5660_M_BST2_RM_L_SFT, 1, 1),
  403. SOC_DAPM_SINGLE("BST1 Switch", RT5660_REC_L2_MIXER,
  404. RT5660_M_BST1_RM_L_SFT, 1, 1),
  405. SOC_DAPM_SINGLE("OUT MIXL Switch", RT5660_REC_L2_MIXER,
  406. RT5660_M_OM_L_RM_L_SFT, 1, 1),
  407. };
  408. static const struct snd_kcontrol_new rt5660_rec_r_mix[] = {
  409. SOC_DAPM_SINGLE("BST3 Switch", RT5660_REC_R2_MIXER,
  410. RT5660_M_BST3_RM_R_SFT, 1, 1),
  411. SOC_DAPM_SINGLE("BST2 Switch", RT5660_REC_R2_MIXER,
  412. RT5660_M_BST2_RM_R_SFT, 1, 1),
  413. SOC_DAPM_SINGLE("BST1 Switch", RT5660_REC_R2_MIXER,
  414. RT5660_M_BST1_RM_R_SFT, 1, 1),
  415. SOC_DAPM_SINGLE("OUT MIXR Switch", RT5660_REC_R2_MIXER,
  416. RT5660_M_OM_R_RM_R_SFT, 1, 1),
  417. };
  418. static const struct snd_kcontrol_new rt5660_spk_mix[] = {
  419. SOC_DAPM_SINGLE("BST3 Switch", RT5660_SPK_MIXER,
  420. RT5660_M_BST3_SM_SFT, 1, 1),
  421. SOC_DAPM_SINGLE("BST1 Switch", RT5660_SPK_MIXER,
  422. RT5660_M_BST1_SM_SFT, 1, 1),
  423. SOC_DAPM_SINGLE("DACL Switch", RT5660_SPK_MIXER,
  424. RT5660_M_DACL_SM_SFT, 1, 1),
  425. SOC_DAPM_SINGLE("DACR Switch", RT5660_SPK_MIXER,
  426. RT5660_M_DACR_SM_SFT, 1, 1),
  427. SOC_DAPM_SINGLE("OUTMIXL Switch", RT5660_SPK_MIXER,
  428. RT5660_M_OM_L_SM_SFT, 1, 1),
  429. };
  430. static const struct snd_kcontrol_new rt5660_out_l_mix[] = {
  431. SOC_DAPM_SINGLE("BST3 Switch", RT5660_OUT_L1_MIXER,
  432. RT5660_M_BST3_OM_L_SFT, 1, 1),
  433. SOC_DAPM_SINGLE("BST2 Switch", RT5660_OUT_L1_MIXER,
  434. RT5660_M_BST2_OM_L_SFT, 1, 1),
  435. SOC_DAPM_SINGLE("BST1 Switch", RT5660_OUT_L1_MIXER,
  436. RT5660_M_BST1_OM_L_SFT, 1, 1),
  437. SOC_DAPM_SINGLE("RECMIXL Switch", RT5660_OUT_L1_MIXER,
  438. RT5660_M_RM_L_OM_L_SFT, 1, 1),
  439. SOC_DAPM_SINGLE("DACR Switch", RT5660_OUT_L1_MIXER,
  440. RT5660_M_DAC_R_OM_L_SFT, 1, 1),
  441. SOC_DAPM_SINGLE("DACL Switch", RT5660_OUT_L1_MIXER,
  442. RT5660_M_DAC_L_OM_L_SFT, 1, 1),
  443. };
  444. static const struct snd_kcontrol_new rt5660_out_r_mix[] = {
  445. SOC_DAPM_SINGLE("BST2 Switch", RT5660_OUT_R1_MIXER,
  446. RT5660_M_BST2_OM_R_SFT, 1, 1),
  447. SOC_DAPM_SINGLE("BST1 Switch", RT5660_OUT_R1_MIXER,
  448. RT5660_M_BST1_OM_R_SFT, 1, 1),
  449. SOC_DAPM_SINGLE("RECMIXR Switch", RT5660_OUT_R1_MIXER,
  450. RT5660_M_RM_R_OM_R_SFT, 1, 1),
  451. SOC_DAPM_SINGLE("DACR Switch", RT5660_OUT_R1_MIXER,
  452. RT5660_M_DAC_R_OM_R_SFT, 1, 1),
  453. SOC_DAPM_SINGLE("DACL Switch", RT5660_OUT_R1_MIXER,
  454. RT5660_M_DAC_L_OM_R_SFT, 1, 1),
  455. };
  456. static const struct snd_kcontrol_new rt5660_spo_mix[] = {
  457. SOC_DAPM_SINGLE("DACR Switch", RT5660_SPO_MIXER,
  458. RT5660_M_DAC_R_SPM_SFT, 1, 1),
  459. SOC_DAPM_SINGLE("DACL Switch", RT5660_SPO_MIXER,
  460. RT5660_M_DAC_L_SPM_SFT, 1, 1),
  461. SOC_DAPM_SINGLE("SPKVOL Switch", RT5660_SPO_MIXER,
  462. RT5660_M_SV_SPM_SFT, 1, 1),
  463. SOC_DAPM_SINGLE("BST1 Switch", RT5660_SPO_MIXER,
  464. RT5660_M_BST1_SPM_SFT, 1, 1),
  465. };
  466. static const struct snd_kcontrol_new rt5660_lout_mix[] = {
  467. SOC_DAPM_SINGLE("DAC Switch", RT5660_LOUT_MIXER,
  468. RT5660_M_DAC1_LM_SFT, 1, 1),
  469. SOC_DAPM_SINGLE("OUTMIX Switch", RT5660_LOUT_MIXER,
  470. RT5660_M_LOVOL_LM_SFT, 1, 1),
  471. };
  472. static const struct snd_kcontrol_new spk_vol_control =
  473. SOC_DAPM_SINGLE("Switch", RT5660_SPK_VOL,
  474. RT5660_VOL_L_SFT, 1, 1);
  475. static const struct snd_kcontrol_new lout_l_vol_control =
  476. SOC_DAPM_SINGLE("Switch", RT5660_LOUT_VOL,
  477. RT5660_VOL_L_SFT, 1, 1);
  478. static const struct snd_kcontrol_new lout_r_vol_control =
  479. SOC_DAPM_SINGLE("Switch", RT5660_LOUT_VOL,
  480. RT5660_VOL_R_SFT, 1, 1);
  481. /* Interface data select */
  482. static const char * const rt5660_data_select[] = {
  483. "L/R", "R/L", "L/L", "R/R"
  484. };
  485. static SOC_ENUM_SINGLE_DECL(rt5660_if1_dac_enum,
  486. RT5660_DIG_INF1_DATA, RT5660_IF1_DAC_IN_SFT, rt5660_data_select);
  487. static SOC_ENUM_SINGLE_DECL(rt5660_if1_adc_enum,
  488. RT5660_DIG_INF1_DATA, RT5660_IF1_ADC_IN_SFT, rt5660_data_select);
  489. static const struct snd_kcontrol_new rt5660_if1_dac_swap_mux =
  490. SOC_DAPM_ENUM("IF1 DAC Swap Source", rt5660_if1_dac_enum);
  491. static const struct snd_kcontrol_new rt5660_if1_adc_swap_mux =
  492. SOC_DAPM_ENUM("IF1 ADC Swap Source", rt5660_if1_adc_enum);
  493. static int rt5660_lout_event(struct snd_soc_dapm_widget *w,
  494. struct snd_kcontrol *kcontrol, int event)
  495. {
  496. struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
  497. switch (event) {
  498. case SND_SOC_DAPM_POST_PMU:
  499. snd_soc_update_bits(codec, RT5660_LOUT_AMP_CTRL,
  500. RT5660_LOUT_CO_MASK | RT5660_LOUT_CB_MASK,
  501. RT5660_LOUT_CO_EN | RT5660_LOUT_CB_PU);
  502. break;
  503. case SND_SOC_DAPM_PRE_PMD:
  504. snd_soc_update_bits(codec, RT5660_LOUT_AMP_CTRL,
  505. RT5660_LOUT_CO_MASK | RT5660_LOUT_CB_MASK,
  506. RT5660_LOUT_CO_DIS | RT5660_LOUT_CB_PD);
  507. break;
  508. default:
  509. return 0;
  510. }
  511. return 0;
  512. }
  513. static const struct snd_soc_dapm_widget rt5660_dapm_widgets[] = {
  514. SND_SOC_DAPM_SUPPLY("LDO2", RT5660_PWR_ANLG1,
  515. RT5660_PWR_LDO2_BIT, 0, NULL, 0),
  516. SND_SOC_DAPM_SUPPLY("PLL1", RT5660_PWR_ANLG2,
  517. RT5660_PWR_PLL_BIT, 0, NULL, 0),
  518. /* MICBIAS */
  519. SND_SOC_DAPM_SUPPLY("MICBIAS1", RT5660_PWR_ANLG2,
  520. RT5660_PWR_MB1_BIT, 0, NULL, 0),
  521. SND_SOC_DAPM_SUPPLY("MICBIAS2", RT5660_PWR_ANLG2,
  522. RT5660_PWR_MB2_BIT, 0, NULL, 0),
  523. /* Input Side */
  524. /* Input Lines */
  525. SND_SOC_DAPM_INPUT("DMIC L1"),
  526. SND_SOC_DAPM_INPUT("DMIC R1"),
  527. SND_SOC_DAPM_INPUT("IN1P"),
  528. SND_SOC_DAPM_INPUT("IN1N"),
  529. SND_SOC_DAPM_INPUT("IN2P"),
  530. SND_SOC_DAPM_INPUT("IN3P"),
  531. SND_SOC_DAPM_INPUT("IN3N"),
  532. SND_SOC_DAPM_SUPPLY("DMIC CLK", SND_SOC_NOPM, 0, 0,
  533. rt5660_set_dmic_clk, SND_SOC_DAPM_PRE_PMU),
  534. SND_SOC_DAPM_SUPPLY("DMIC Power", RT5660_DMIC_CTRL1,
  535. RT5660_DMIC_1_EN_SFT, 0, NULL, 0),
  536. /* Boost */
  537. SND_SOC_DAPM_PGA("BST1", RT5660_PWR_ANLG2, RT5660_PWR_BST1_BIT, 0,
  538. NULL, 0),
  539. SND_SOC_DAPM_PGA("BST2", RT5660_PWR_ANLG2, RT5660_PWR_BST2_BIT, 0,
  540. NULL, 0),
  541. SND_SOC_DAPM_PGA("BST3", RT5660_PWR_ANLG2, RT5660_PWR_BST3_BIT, 0,
  542. NULL, 0),
  543. /* REC Mixer */
  544. SND_SOC_DAPM_MIXER("RECMIXL", RT5660_PWR_MIXER, RT5660_PWR_RM_L_BIT,
  545. 0, rt5660_rec_l_mix, ARRAY_SIZE(rt5660_rec_l_mix)),
  546. SND_SOC_DAPM_MIXER("RECMIXR", RT5660_PWR_MIXER, RT5660_PWR_RM_R_BIT,
  547. 0, rt5660_rec_r_mix, ARRAY_SIZE(rt5660_rec_r_mix)),
  548. /* ADCs */
  549. SND_SOC_DAPM_ADC("ADC L", NULL, SND_SOC_NOPM, 0, 0),
  550. SND_SOC_DAPM_ADC("ADC R", NULL, SND_SOC_NOPM, 0, 0),
  551. SND_SOC_DAPM_SUPPLY("ADC L power", RT5660_PWR_DIG1,
  552. RT5660_PWR_ADC_L_BIT, 0, NULL, 0),
  553. SND_SOC_DAPM_SUPPLY("ADC R power", RT5660_PWR_DIG1,
  554. RT5660_PWR_ADC_R_BIT, 0, NULL, 0),
  555. SND_SOC_DAPM_SUPPLY("ADC clock", RT5660_PR_BASE + RT5660_CHOP_DAC_ADC,
  556. 12, 0, NULL, 0),
  557. /* ADC Mixer */
  558. SND_SOC_DAPM_SUPPLY("adc stereo1 filter", RT5660_PWR_DIG2,
  559. RT5660_PWR_ADC_S1F_BIT, 0, NULL, 0),
  560. SND_SOC_DAPM_MIXER("Sto1 ADC MIXL", SND_SOC_NOPM, 0, 0,
  561. rt5660_sto1_adc_l_mix, ARRAY_SIZE(rt5660_sto1_adc_l_mix)),
  562. SND_SOC_DAPM_MIXER("Sto1 ADC MIXR", SND_SOC_NOPM, 0, 0,
  563. rt5660_sto1_adc_r_mix, ARRAY_SIZE(rt5660_sto1_adc_r_mix)),
  564. /* ADC */
  565. SND_SOC_DAPM_ADC("Stereo1 ADC MIXL", NULL, RT5660_STO1_ADC_DIG_VOL,
  566. RT5660_L_MUTE_SFT, 1),
  567. SND_SOC_DAPM_ADC("Stereo1 ADC MIXR", NULL, RT5660_STO1_ADC_DIG_VOL,
  568. RT5660_R_MUTE_SFT, 1),
  569. /* Digital Interface */
  570. SND_SOC_DAPM_SUPPLY("I2S1", RT5660_PWR_DIG1, RT5660_PWR_I2S1_BIT, 0,
  571. NULL, 0),
  572. SND_SOC_DAPM_PGA("IF1 DAC", SND_SOC_NOPM, 0, 0, NULL, 0),
  573. SND_SOC_DAPM_PGA("IF1 DAC L", SND_SOC_NOPM, 0, 0, NULL, 0),
  574. SND_SOC_DAPM_PGA("IF1 DAC R", SND_SOC_NOPM, 0, 0, NULL, 0),
  575. SND_SOC_DAPM_MUX("IF1 DAC Swap Mux", SND_SOC_NOPM, 0, 0,
  576. &rt5660_if1_dac_swap_mux),
  577. SND_SOC_DAPM_PGA("IF1 ADC", SND_SOC_NOPM, 0, 0, NULL, 0),
  578. SND_SOC_DAPM_MUX("IF1 ADC Swap Mux", SND_SOC_NOPM, 0, 0,
  579. &rt5660_if1_adc_swap_mux),
  580. /* Audio Interface */
  581. SND_SOC_DAPM_AIF_IN("AIF1RX", "AIF1 Playback", 0, SND_SOC_NOPM, 0, 0),
  582. SND_SOC_DAPM_AIF_OUT("AIF1TX", "AIF1 Capture", 0, SND_SOC_NOPM, 0, 0),
  583. /* Output Side */
  584. /* DAC mixer before sound effect */
  585. SND_SOC_DAPM_MIXER("DAC1 MIXL", SND_SOC_NOPM, 0, 0, rt5660_dac_l_mix,
  586. ARRAY_SIZE(rt5660_dac_l_mix)),
  587. SND_SOC_DAPM_MIXER("DAC1 MIXR", SND_SOC_NOPM, 0, 0, rt5660_dac_r_mix,
  588. ARRAY_SIZE(rt5660_dac_r_mix)),
  589. /* DAC Mixer */
  590. SND_SOC_DAPM_SUPPLY("dac stereo1 filter", RT5660_PWR_DIG2,
  591. RT5660_PWR_DAC_S1F_BIT, 0, NULL, 0),
  592. SND_SOC_DAPM_MIXER("Stereo DAC MIXL", SND_SOC_NOPM, 0, 0,
  593. rt5660_sto_dac_l_mix, ARRAY_SIZE(rt5660_sto_dac_l_mix)),
  594. SND_SOC_DAPM_MIXER("Stereo DAC MIXR", SND_SOC_NOPM, 0, 0,
  595. rt5660_sto_dac_r_mix, ARRAY_SIZE(rt5660_sto_dac_r_mix)),
  596. /* DACs */
  597. SND_SOC_DAPM_DAC("DAC L1", NULL, RT5660_PWR_DIG1,
  598. RT5660_PWR_DAC_L1_BIT, 0),
  599. SND_SOC_DAPM_DAC("DAC R1", NULL, RT5660_PWR_DIG1,
  600. RT5660_PWR_DAC_R1_BIT, 0),
  601. /* OUT Mixer */
  602. SND_SOC_DAPM_MIXER("SPK MIX", RT5660_PWR_MIXER, RT5660_PWR_SM_BIT,
  603. 0, rt5660_spk_mix, ARRAY_SIZE(rt5660_spk_mix)),
  604. SND_SOC_DAPM_MIXER("OUT MIXL", RT5660_PWR_MIXER, RT5660_PWR_OM_L_BIT,
  605. 0, rt5660_out_l_mix, ARRAY_SIZE(rt5660_out_l_mix)),
  606. SND_SOC_DAPM_MIXER("OUT MIXR", RT5660_PWR_MIXER, RT5660_PWR_OM_R_BIT,
  607. 0, rt5660_out_r_mix, ARRAY_SIZE(rt5660_out_r_mix)),
  608. /* Output Volume */
  609. SND_SOC_DAPM_SWITCH("SPKVOL", RT5660_PWR_VOL,
  610. RT5660_PWR_SV_BIT, 0, &spk_vol_control),
  611. SND_SOC_DAPM_PGA("DAC 1", SND_SOC_NOPM,
  612. 0, 0, NULL, 0),
  613. SND_SOC_DAPM_PGA("LOUTVOL", SND_SOC_NOPM,
  614. 0, 0, NULL, 0),
  615. SND_SOC_DAPM_SWITCH("LOUTVOL L", SND_SOC_NOPM,
  616. RT5660_PWR_LV_L_BIT, 0, &lout_l_vol_control),
  617. SND_SOC_DAPM_SWITCH("LOUTVOL R", SND_SOC_NOPM,
  618. RT5660_PWR_LV_R_BIT, 0, &lout_r_vol_control),
  619. /* HPO/LOUT/Mono Mixer */
  620. SND_SOC_DAPM_MIXER("SPO MIX", SND_SOC_NOPM, 0,
  621. 0, rt5660_spo_mix, ARRAY_SIZE(rt5660_spo_mix)),
  622. SND_SOC_DAPM_MIXER("LOUT MIX", SND_SOC_NOPM, 0, 0,
  623. rt5660_lout_mix, ARRAY_SIZE(rt5660_lout_mix)),
  624. SND_SOC_DAPM_SUPPLY("VREF HP", RT5660_GEN_CTRL1,
  625. RT5660_PWR_VREF_HP_SFT, 0, NULL, 0),
  626. SND_SOC_DAPM_PGA_S("LOUT amp", 1, RT5660_PWR_ANLG1,
  627. RT5660_PWR_HA_BIT, 0, rt5660_lout_event,
  628. SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMU),
  629. SND_SOC_DAPM_PGA_S("SPK amp", 1, RT5660_PWR_DIG1,
  630. RT5660_PWR_CLS_D_BIT, 0, NULL, 0),
  631. /* Output Lines */
  632. SND_SOC_DAPM_OUTPUT("LOUTL"),
  633. SND_SOC_DAPM_OUTPUT("LOUTR"),
  634. SND_SOC_DAPM_OUTPUT("SPO"),
  635. };
  636. static const struct snd_soc_dapm_route rt5660_dapm_routes[] = {
  637. { "MICBIAS1", NULL, "LDO2" },
  638. { "MICBIAS2", NULL, "LDO2" },
  639. { "BST1", NULL, "IN1P" },
  640. { "BST1", NULL, "IN1N" },
  641. { "BST2", NULL, "IN2P" },
  642. { "BST3", NULL, "IN3P" },
  643. { "BST3", NULL, "IN3N" },
  644. { "RECMIXL", "BST3 Switch", "BST3" },
  645. { "RECMIXL", "BST2 Switch", "BST2" },
  646. { "RECMIXL", "BST1 Switch", "BST1" },
  647. { "RECMIXL", "OUT MIXL Switch", "OUT MIXL" },
  648. { "RECMIXR", "BST3 Switch", "BST3" },
  649. { "RECMIXR", "BST2 Switch", "BST2" },
  650. { "RECMIXR", "BST1 Switch", "BST1" },
  651. { "RECMIXR", "OUT MIXR Switch", "OUT MIXR" },
  652. { "ADC L", NULL, "RECMIXL" },
  653. { "ADC L", NULL, "ADC L power" },
  654. { "ADC L", NULL, "ADC clock" },
  655. { "ADC R", NULL, "RECMIXR" },
  656. { "ADC R", NULL, "ADC R power" },
  657. { "ADC R", NULL, "ADC clock" },
  658. {"DMIC L1", NULL, "DMIC CLK"},
  659. {"DMIC L1", NULL, "DMIC Power"},
  660. {"DMIC R1", NULL, "DMIC CLK"},
  661. {"DMIC R1", NULL, "DMIC Power"},
  662. { "Sto1 ADC MIXL", "ADC1 Switch", "ADC L" },
  663. { "Sto1 ADC MIXL", "ADC2 Switch", "DMIC L1" },
  664. { "Sto1 ADC MIXR", "ADC1 Switch", "ADC R" },
  665. { "Sto1 ADC MIXR", "ADC2 Switch", "DMIC R1" },
  666. { "Stereo1 ADC MIXL", NULL, "Sto1 ADC MIXL" },
  667. { "Stereo1 ADC MIXL", NULL, "adc stereo1 filter" },
  668. { "adc stereo1 filter", NULL, "PLL1", rt5660_is_sys_clk_from_pll },
  669. { "Stereo1 ADC MIXR", NULL, "Sto1 ADC MIXR" },
  670. { "Stereo1 ADC MIXR", NULL, "adc stereo1 filter" },
  671. { "adc stereo1 filter", NULL, "PLL1", rt5660_is_sys_clk_from_pll },
  672. { "IF1 ADC", NULL, "Stereo1 ADC MIXL" },
  673. { "IF1 ADC", NULL, "Stereo1 ADC MIXR" },
  674. { "IF1 ADC", NULL, "I2S1" },
  675. { "IF1 ADC Swap Mux", "L/R", "IF1 ADC" },
  676. { "IF1 ADC Swap Mux", "R/L", "IF1 ADC" },
  677. { "IF1 ADC Swap Mux", "L/L", "IF1 ADC" },
  678. { "IF1 ADC Swap Mux", "R/R", "IF1 ADC" },
  679. { "AIF1TX", NULL, "IF1 ADC Swap Mux" },
  680. { "IF1 DAC", NULL, "AIF1RX" },
  681. { "IF1 DAC", NULL, "I2S1" },
  682. { "IF1 DAC Swap Mux", "L/R", "IF1 DAC" },
  683. { "IF1 DAC Swap Mux", "R/L", "IF1 DAC" },
  684. { "IF1 DAC Swap Mux", "L/L", "IF1 DAC" },
  685. { "IF1 DAC Swap Mux", "R/R", "IF1 DAC" },
  686. { "IF1 DAC L", NULL, "IF1 DAC Swap Mux" },
  687. { "IF1 DAC R", NULL, "IF1 DAC Swap Mux" },
  688. { "DAC1 MIXL", "Stereo ADC Switch", "Stereo1 ADC MIXL" },
  689. { "DAC1 MIXL", "DAC1 Switch", "IF1 DAC L" },
  690. { "DAC1 MIXR", "Stereo ADC Switch", "Stereo1 ADC MIXR" },
  691. { "DAC1 MIXR", "DAC1 Switch", "IF1 DAC R" },
  692. { "Stereo DAC MIXL", "DAC L1 Switch", "DAC1 MIXL" },
  693. { "Stereo DAC MIXL", "DAC R1 Switch", "DAC1 MIXR" },
  694. { "Stereo DAC MIXL", NULL, "dac stereo1 filter" },
  695. { "dac stereo1 filter", NULL, "PLL1", rt5660_is_sys_clk_from_pll },
  696. { "Stereo DAC MIXR", "DAC R1 Switch", "DAC1 MIXR" },
  697. { "Stereo DAC MIXR", "DAC L1 Switch", "DAC1 MIXL" },
  698. { "Stereo DAC MIXR", NULL, "dac stereo1 filter" },
  699. { "dac stereo1 filter", NULL, "PLL1", rt5660_is_sys_clk_from_pll },
  700. { "DAC L1", NULL, "Stereo DAC MIXL" },
  701. { "DAC R1", NULL, "Stereo DAC MIXR" },
  702. { "SPK MIX", "BST3 Switch", "BST3" },
  703. { "SPK MIX", "BST1 Switch", "BST1" },
  704. { "SPK MIX", "DACL Switch", "DAC L1" },
  705. { "SPK MIX", "DACR Switch", "DAC R1" },
  706. { "SPK MIX", "OUTMIXL Switch", "OUT MIXL" },
  707. { "OUT MIXL", "BST3 Switch", "BST3" },
  708. { "OUT MIXL", "BST2 Switch", "BST2" },
  709. { "OUT MIXL", "BST1 Switch", "BST1" },
  710. { "OUT MIXL", "RECMIXL Switch", "RECMIXL" },
  711. { "OUT MIXL", "DACR Switch", "DAC R1" },
  712. { "OUT MIXL", "DACL Switch", "DAC L1" },
  713. { "OUT MIXR", "BST2 Switch", "BST2" },
  714. { "OUT MIXR", "BST1 Switch", "BST1" },
  715. { "OUT MIXR", "RECMIXR Switch", "RECMIXR" },
  716. { "OUT MIXR", "DACR Switch", "DAC R1" },
  717. { "OUT MIXR", "DACL Switch", "DAC L1" },
  718. { "SPO MIX", "DACR Switch", "DAC R1" },
  719. { "SPO MIX", "DACL Switch", "DAC L1" },
  720. { "SPO MIX", "SPKVOL Switch", "SPKVOL" },
  721. { "SPO MIX", "BST1 Switch", "BST1" },
  722. { "SPKVOL", "Switch", "SPK MIX" },
  723. { "LOUTVOL L", "Switch", "OUT MIXL" },
  724. { "LOUTVOL R", "Switch", "OUT MIXR" },
  725. { "LOUTVOL", NULL, "LOUTVOL L" },
  726. { "LOUTVOL", NULL, "LOUTVOL R" },
  727. { "DAC 1", NULL, "DAC L1" },
  728. { "DAC 1", NULL, "DAC R1" },
  729. { "LOUT MIX", "DAC Switch", "DAC 1" },
  730. { "LOUT MIX", "OUTMIX Switch", "LOUTVOL" },
  731. { "LOUT amp", NULL, "LOUT MIX" },
  732. { "LOUT amp", NULL, "VREF HP" },
  733. { "LOUTL", NULL, "LOUT amp" },
  734. { "LOUTR", NULL, "LOUT amp" },
  735. { "SPK amp", NULL, "SPO MIX" },
  736. { "SPO", NULL, "SPK amp" },
  737. };
  738. static int rt5660_hw_params(struct snd_pcm_substream *substream,
  739. struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
  740. {
  741. struct snd_soc_codec *codec = dai->codec;
  742. struct rt5660_priv *rt5660 = snd_soc_codec_get_drvdata(codec);
  743. unsigned int val_len = 0, val_clk, mask_clk;
  744. int pre_div, bclk_ms, frame_size;
  745. rt5660->lrck[dai->id] = params_rate(params);
  746. pre_div = rl6231_get_clk_info(rt5660->sysclk, rt5660->lrck[dai->id]);
  747. if (pre_div < 0) {
  748. dev_err(codec->dev, "Unsupported clock setting %d for DAI %d\n",
  749. rt5660->lrck[dai->id], dai->id);
  750. return -EINVAL;
  751. }
  752. frame_size = snd_soc_params_to_frame_size(params);
  753. if (frame_size < 0) {
  754. dev_err(codec->dev, "Unsupported frame size: %d\n", frame_size);
  755. return frame_size;
  756. }
  757. if (frame_size > 32)
  758. bclk_ms = 1;
  759. else
  760. bclk_ms = 0;
  761. rt5660->bclk[dai->id] = rt5660->lrck[dai->id] * (32 << bclk_ms);
  762. dev_dbg(dai->dev, "bclk is %dHz and lrck is %dHz\n",
  763. rt5660->bclk[dai->id], rt5660->lrck[dai->id]);
  764. dev_dbg(dai->dev, "bclk_ms is %d and pre_div is %d for iis %d\n",
  765. bclk_ms, pre_div, dai->id);
  766. switch (params_width(params)) {
  767. case 16:
  768. break;
  769. case 20:
  770. val_len |= RT5660_I2S_DL_20;
  771. break;
  772. case 24:
  773. val_len |= RT5660_I2S_DL_24;
  774. break;
  775. case 8:
  776. val_len |= RT5660_I2S_DL_8;
  777. break;
  778. default:
  779. return -EINVAL;
  780. }
  781. switch (dai->id) {
  782. case RT5660_AIF1:
  783. mask_clk = RT5660_I2S_BCLK_MS1_MASK | RT5660_I2S_PD1_MASK;
  784. val_clk = bclk_ms << RT5660_I2S_BCLK_MS1_SFT |
  785. pre_div << RT5660_I2S_PD1_SFT;
  786. snd_soc_update_bits(codec, RT5660_I2S1_SDP, RT5660_I2S_DL_MASK,
  787. val_len);
  788. snd_soc_update_bits(codec, RT5660_ADDA_CLK1, mask_clk, val_clk);
  789. break;
  790. default:
  791. dev_err(codec->dev, "Invalid dai->id: %d\n", dai->id);
  792. return -EINVAL;
  793. }
  794. return 0;
  795. }
  796. static int rt5660_set_dai_fmt(struct snd_soc_dai *dai, unsigned int fmt)
  797. {
  798. struct snd_soc_codec *codec = dai->codec;
  799. struct rt5660_priv *rt5660 = snd_soc_codec_get_drvdata(codec);
  800. unsigned int reg_val = 0;
  801. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  802. case SND_SOC_DAIFMT_CBM_CFM:
  803. rt5660->master[dai->id] = 1;
  804. break;
  805. case SND_SOC_DAIFMT_CBS_CFS:
  806. reg_val |= RT5660_I2S_MS_S;
  807. rt5660->master[dai->id] = 0;
  808. break;
  809. default:
  810. return -EINVAL;
  811. }
  812. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  813. case SND_SOC_DAIFMT_NB_NF:
  814. break;
  815. case SND_SOC_DAIFMT_IB_NF:
  816. reg_val |= RT5660_I2S_BP_INV;
  817. break;
  818. default:
  819. return -EINVAL;
  820. }
  821. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  822. case SND_SOC_DAIFMT_I2S:
  823. break;
  824. case SND_SOC_DAIFMT_LEFT_J:
  825. reg_val |= RT5660_I2S_DF_LEFT;
  826. break;
  827. case SND_SOC_DAIFMT_DSP_A:
  828. reg_val |= RT5660_I2S_DF_PCM_A;
  829. break;
  830. case SND_SOC_DAIFMT_DSP_B:
  831. reg_val |= RT5660_I2S_DF_PCM_B;
  832. break;
  833. default:
  834. return -EINVAL;
  835. }
  836. switch (dai->id) {
  837. case RT5660_AIF1:
  838. snd_soc_update_bits(codec, RT5660_I2S1_SDP,
  839. RT5660_I2S_MS_MASK | RT5660_I2S_BP_MASK |
  840. RT5660_I2S_DF_MASK, reg_val);
  841. break;
  842. default:
  843. dev_err(codec->dev, "Invalid dai->id: %d\n", dai->id);
  844. return -EINVAL;
  845. }
  846. return 0;
  847. }
  848. static int rt5660_set_dai_sysclk(struct snd_soc_dai *dai,
  849. int clk_id, unsigned int freq, int dir)
  850. {
  851. struct snd_soc_codec *codec = dai->codec;
  852. struct rt5660_priv *rt5660 = snd_soc_codec_get_drvdata(codec);
  853. unsigned int reg_val = 0;
  854. if (freq == rt5660->sysclk && clk_id == rt5660->sysclk_src)
  855. return 0;
  856. switch (clk_id) {
  857. case RT5660_SCLK_S_MCLK:
  858. reg_val |= RT5660_SCLK_SRC_MCLK;
  859. break;
  860. case RT5660_SCLK_S_PLL1:
  861. reg_val |= RT5660_SCLK_SRC_PLL1;
  862. break;
  863. case RT5660_SCLK_S_RCCLK:
  864. reg_val |= RT5660_SCLK_SRC_RCCLK;
  865. break;
  866. default:
  867. dev_err(codec->dev, "Invalid clock id (%d)\n", clk_id);
  868. return -EINVAL;
  869. }
  870. snd_soc_update_bits(codec, RT5660_GLB_CLK, RT5660_SCLK_SRC_MASK,
  871. reg_val);
  872. rt5660->sysclk = freq;
  873. rt5660->sysclk_src = clk_id;
  874. dev_dbg(dai->dev, "Sysclk is %dHz and clock id is %d\n", freq, clk_id);
  875. return 0;
  876. }
  877. static int rt5660_set_dai_pll(struct snd_soc_dai *dai, int pll_id, int source,
  878. unsigned int freq_in, unsigned int freq_out)
  879. {
  880. struct snd_soc_codec *codec = dai->codec;
  881. struct rt5660_priv *rt5660 = snd_soc_codec_get_drvdata(codec);
  882. struct rl6231_pll_code pll_code;
  883. int ret;
  884. if (source == rt5660->pll_src && freq_in == rt5660->pll_in &&
  885. freq_out == rt5660->pll_out)
  886. return 0;
  887. if (!freq_in || !freq_out) {
  888. dev_dbg(codec->dev, "PLL disabled\n");
  889. rt5660->pll_in = 0;
  890. rt5660->pll_out = 0;
  891. snd_soc_update_bits(codec, RT5660_GLB_CLK,
  892. RT5660_SCLK_SRC_MASK, RT5660_SCLK_SRC_MCLK);
  893. return 0;
  894. }
  895. switch (source) {
  896. case RT5660_PLL1_S_MCLK:
  897. snd_soc_update_bits(codec, RT5660_GLB_CLK,
  898. RT5660_PLL1_SRC_MASK, RT5660_PLL1_SRC_MCLK);
  899. break;
  900. case RT5660_PLL1_S_BCLK:
  901. snd_soc_update_bits(codec, RT5660_GLB_CLK,
  902. RT5660_PLL1_SRC_MASK, RT5660_PLL1_SRC_BCLK1);
  903. break;
  904. default:
  905. dev_err(codec->dev, "Unknown PLL source %d\n", source);
  906. return -EINVAL;
  907. }
  908. ret = rl6231_pll_calc(freq_in, freq_out, &pll_code);
  909. if (ret < 0) {
  910. dev_err(codec->dev, "Unsupport input clock %d\n", freq_in);
  911. return ret;
  912. }
  913. dev_dbg(codec->dev, "bypass=%d m=%d n=%d k=%d\n",
  914. pll_code.m_bp, (pll_code.m_bp ? 0 : pll_code.m_code),
  915. pll_code.n_code, pll_code.k_code);
  916. snd_soc_write(codec, RT5660_PLL_CTRL1,
  917. pll_code.n_code << RT5660_PLL_N_SFT | pll_code.k_code);
  918. snd_soc_write(codec, RT5660_PLL_CTRL2,
  919. (pll_code.m_bp ? 0 : pll_code.m_code) << RT5660_PLL_M_SFT |
  920. pll_code.m_bp << RT5660_PLL_M_BP_SFT);
  921. rt5660->pll_in = freq_in;
  922. rt5660->pll_out = freq_out;
  923. rt5660->pll_src = source;
  924. return 0;
  925. }
  926. static int rt5660_set_bias_level(struct snd_soc_codec *codec,
  927. enum snd_soc_bias_level level)
  928. {
  929. struct rt5660_priv *rt5660 = snd_soc_codec_get_drvdata(codec);
  930. int ret;
  931. switch (level) {
  932. case SND_SOC_BIAS_ON:
  933. break;
  934. case SND_SOC_BIAS_PREPARE:
  935. snd_soc_update_bits(codec, RT5660_GEN_CTRL1,
  936. RT5660_DIG_GATE_CTRL, RT5660_DIG_GATE_CTRL);
  937. if (IS_ERR(rt5660->mclk))
  938. break;
  939. if (snd_soc_codec_get_bias_level(codec) == SND_SOC_BIAS_ON) {
  940. clk_disable_unprepare(rt5660->mclk);
  941. } else {
  942. ret = clk_prepare_enable(rt5660->mclk);
  943. if (ret)
  944. return ret;
  945. }
  946. break;
  947. case SND_SOC_BIAS_STANDBY:
  948. if (snd_soc_codec_get_bias_level(codec) == SND_SOC_BIAS_OFF) {
  949. snd_soc_update_bits(codec, RT5660_PWR_ANLG1,
  950. RT5660_PWR_VREF1 | RT5660_PWR_MB |
  951. RT5660_PWR_BG | RT5660_PWR_VREF2,
  952. RT5660_PWR_VREF1 | RT5660_PWR_MB |
  953. RT5660_PWR_BG | RT5660_PWR_VREF2);
  954. usleep_range(10000, 15000);
  955. snd_soc_update_bits(codec, RT5660_PWR_ANLG1,
  956. RT5660_PWR_FV1 | RT5660_PWR_FV2,
  957. RT5660_PWR_FV1 | RT5660_PWR_FV2);
  958. }
  959. break;
  960. case SND_SOC_BIAS_OFF:
  961. snd_soc_update_bits(codec, RT5660_GEN_CTRL1,
  962. RT5660_DIG_GATE_CTRL, 0);
  963. break;
  964. default:
  965. break;
  966. }
  967. return 0;
  968. }
  969. static int rt5660_probe(struct snd_soc_codec *codec)
  970. {
  971. struct rt5660_priv *rt5660 = snd_soc_codec_get_drvdata(codec);
  972. rt5660->codec = codec;
  973. return 0;
  974. }
  975. static int rt5660_remove(struct snd_soc_codec *codec)
  976. {
  977. return snd_soc_write(codec, RT5660_RESET, 0);
  978. }
  979. #ifdef CONFIG_PM
  980. static int rt5660_suspend(struct snd_soc_codec *codec)
  981. {
  982. struct rt5660_priv *rt5660 = snd_soc_codec_get_drvdata(codec);
  983. regcache_cache_only(rt5660->regmap, true);
  984. regcache_mark_dirty(rt5660->regmap);
  985. return 0;
  986. }
  987. static int rt5660_resume(struct snd_soc_codec *codec)
  988. {
  989. struct rt5660_priv *rt5660 = snd_soc_codec_get_drvdata(codec);
  990. if (rt5660->pdata.poweroff_codec_in_suspend)
  991. usleep_range(350000, 400000);
  992. regcache_cache_only(rt5660->regmap, false);
  993. regcache_sync(rt5660->regmap);
  994. return 0;
  995. }
  996. #else
  997. #define rt5660_suspend NULL
  998. #define rt5660_resume NULL
  999. #endif
  1000. #define RT5660_STEREO_RATES SNDRV_PCM_RATE_8000_192000
  1001. #define RT5660_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
  1002. SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S8)
  1003. static const struct snd_soc_dai_ops rt5660_aif_dai_ops = {
  1004. .hw_params = rt5660_hw_params,
  1005. .set_fmt = rt5660_set_dai_fmt,
  1006. .set_sysclk = rt5660_set_dai_sysclk,
  1007. .set_pll = rt5660_set_dai_pll,
  1008. };
  1009. static struct snd_soc_dai_driver rt5660_dai[] = {
  1010. {
  1011. .name = "rt5660-aif1",
  1012. .id = RT5660_AIF1,
  1013. .playback = {
  1014. .stream_name = "AIF1 Playback",
  1015. .channels_min = 1,
  1016. .channels_max = 2,
  1017. .rates = RT5660_STEREO_RATES,
  1018. .formats = RT5660_FORMATS,
  1019. },
  1020. .capture = {
  1021. .stream_name = "AIF1 Capture",
  1022. .channels_min = 1,
  1023. .channels_max = 2,
  1024. .rates = RT5660_STEREO_RATES,
  1025. .formats = RT5660_FORMATS,
  1026. },
  1027. .ops = &rt5660_aif_dai_ops,
  1028. },
  1029. };
  1030. static struct snd_soc_codec_driver soc_codec_dev_rt5660 = {
  1031. .probe = rt5660_probe,
  1032. .remove = rt5660_remove,
  1033. .suspend = rt5660_suspend,
  1034. .resume = rt5660_resume,
  1035. .set_bias_level = rt5660_set_bias_level,
  1036. .idle_bias_off = true,
  1037. .component_driver = {
  1038. .controls = rt5660_snd_controls,
  1039. .num_controls = ARRAY_SIZE(rt5660_snd_controls),
  1040. .dapm_widgets = rt5660_dapm_widgets,
  1041. .num_dapm_widgets = ARRAY_SIZE(rt5660_dapm_widgets),
  1042. .dapm_routes = rt5660_dapm_routes,
  1043. .num_dapm_routes = ARRAY_SIZE(rt5660_dapm_routes),
  1044. },
  1045. };
  1046. static const struct regmap_config rt5660_regmap = {
  1047. .reg_bits = 8,
  1048. .val_bits = 16,
  1049. .use_single_rw = true,
  1050. .max_register = RT5660_VENDOR_ID2 + 1 + (ARRAY_SIZE(rt5660_ranges) *
  1051. RT5660_PR_SPACING),
  1052. .volatile_reg = rt5660_volatile_register,
  1053. .readable_reg = rt5660_readable_register,
  1054. .cache_type = REGCACHE_RBTREE,
  1055. .reg_defaults = rt5660_reg,
  1056. .num_reg_defaults = ARRAY_SIZE(rt5660_reg),
  1057. .ranges = rt5660_ranges,
  1058. .num_ranges = ARRAY_SIZE(rt5660_ranges),
  1059. };
  1060. static const struct i2c_device_id rt5660_i2c_id[] = {
  1061. { "rt5660", 0 },
  1062. { }
  1063. };
  1064. MODULE_DEVICE_TABLE(i2c, rt5660_i2c_id);
  1065. static const struct of_device_id rt5660_of_match[] = {
  1066. { .compatible = "realtek,rt5660", },
  1067. {},
  1068. };
  1069. MODULE_DEVICE_TABLE(of, rt5660_of_match);
  1070. static const struct acpi_device_id rt5660_acpi_match[] = {
  1071. { "10EC5660", 0 },
  1072. { },
  1073. };
  1074. MODULE_DEVICE_TABLE(acpi, rt5660_acpi_match);
  1075. static int rt5660_parse_dt(struct rt5660_priv *rt5660, struct device *dev)
  1076. {
  1077. rt5660->pdata.in1_diff = device_property_read_bool(dev,
  1078. "realtek,in1-differential");
  1079. rt5660->pdata.in3_diff = device_property_read_bool(dev,
  1080. "realtek,in3-differential");
  1081. rt5660->pdata.poweroff_codec_in_suspend = device_property_read_bool(dev,
  1082. "realtek,poweroff-in-suspend");
  1083. device_property_read_u32(dev, "realtek,dmic1-data-pin",
  1084. &rt5660->pdata.dmic1_data_pin);
  1085. return 0;
  1086. }
  1087. static int rt5660_i2c_probe(struct i2c_client *i2c,
  1088. const struct i2c_device_id *id)
  1089. {
  1090. struct rt5660_platform_data *pdata = dev_get_platdata(&i2c->dev);
  1091. struct rt5660_priv *rt5660;
  1092. int ret;
  1093. unsigned int val;
  1094. rt5660 = devm_kzalloc(&i2c->dev, sizeof(struct rt5660_priv),
  1095. GFP_KERNEL);
  1096. if (rt5660 == NULL)
  1097. return -ENOMEM;
  1098. /* Check if MCLK provided */
  1099. rt5660->mclk = devm_clk_get(&i2c->dev, "mclk");
  1100. if (PTR_ERR(rt5660->mclk) == -EPROBE_DEFER)
  1101. return -EPROBE_DEFER;
  1102. i2c_set_clientdata(i2c, rt5660);
  1103. if (pdata)
  1104. rt5660->pdata = *pdata;
  1105. else if (i2c->dev.of_node)
  1106. rt5660_parse_dt(rt5660, &i2c->dev);
  1107. rt5660->regmap = devm_regmap_init_i2c(i2c, &rt5660_regmap);
  1108. if (IS_ERR(rt5660->regmap)) {
  1109. ret = PTR_ERR(rt5660->regmap);
  1110. dev_err(&i2c->dev, "Failed to allocate register map: %d\n",
  1111. ret);
  1112. return ret;
  1113. }
  1114. regmap_read(rt5660->regmap, RT5660_VENDOR_ID2, &val);
  1115. if (val != RT5660_DEVICE_ID) {
  1116. dev_err(&i2c->dev,
  1117. "Device with ID register %#x is not rt5660\n", val);
  1118. return -ENODEV;
  1119. }
  1120. regmap_write(rt5660->regmap, RT5660_RESET, 0);
  1121. ret = regmap_register_patch(rt5660->regmap, rt5660_patch,
  1122. ARRAY_SIZE(rt5660_patch));
  1123. if (ret != 0)
  1124. dev_warn(&i2c->dev, "Failed to apply regmap patch: %d\n", ret);
  1125. if (rt5660->pdata.dmic1_data_pin) {
  1126. regmap_update_bits(rt5660->regmap, RT5660_GPIO_CTRL1,
  1127. RT5660_GP1_PIN_MASK, RT5660_GP1_PIN_DMIC1_SCL);
  1128. if (rt5660->pdata.dmic1_data_pin == RT5660_DMIC1_DATA_GPIO2)
  1129. regmap_update_bits(rt5660->regmap, RT5660_DMIC_CTRL1,
  1130. RT5660_SEL_DMIC_DATA_MASK,
  1131. RT5660_SEL_DMIC_DATA_GPIO2);
  1132. else if (rt5660->pdata.dmic1_data_pin == RT5660_DMIC1_DATA_IN1P)
  1133. regmap_update_bits(rt5660->regmap, RT5660_DMIC_CTRL1,
  1134. RT5660_SEL_DMIC_DATA_MASK,
  1135. RT5660_SEL_DMIC_DATA_IN1P);
  1136. }
  1137. return snd_soc_register_codec(&i2c->dev, &soc_codec_dev_rt5660,
  1138. rt5660_dai, ARRAY_SIZE(rt5660_dai));
  1139. }
  1140. static int rt5660_i2c_remove(struct i2c_client *i2c)
  1141. {
  1142. snd_soc_unregister_codec(&i2c->dev);
  1143. return 0;
  1144. }
  1145. static struct i2c_driver rt5660_i2c_driver = {
  1146. .driver = {
  1147. .name = "rt5660",
  1148. .acpi_match_table = ACPI_PTR(rt5660_acpi_match),
  1149. .of_match_table = of_match_ptr(rt5660_of_match),
  1150. },
  1151. .probe = rt5660_i2c_probe,
  1152. .remove = rt5660_i2c_remove,
  1153. .id_table = rt5660_i2c_id,
  1154. };
  1155. module_i2c_driver(rt5660_i2c_driver);
  1156. MODULE_DESCRIPTION("ASoC RT5660 driver");
  1157. MODULE_AUTHOR("Oder Chiou <oder_chiou@realtek.com>");
  1158. MODULE_LICENSE("GPL v2");