sun8i-codec.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * This driver supports the digital controls for the internal codec
  4. * found in Allwinner's A33 SoCs.
  5. *
  6. * (C) Copyright 2010-2016
  7. * Reuuimlla Technology Co., Ltd. <www.reuuimllatech.com>
  8. * huangxin <[email protected]>
  9. * Mylène Josserand <[email protected]>
  10. */
  11. #include <linux/module.h>
  12. #include <linux/delay.h>
  13. #include <linux/clk.h>
  14. #include <linux/io.h>
  15. #include <linux/of_device.h>
  16. #include <linux/pm_runtime.h>
  17. #include <linux/regmap.h>
  18. #include <linux/log2.h>
  19. #include <sound/pcm_params.h>
  20. #include <sound/soc.h>
  21. #include <sound/soc-dapm.h>
  22. #include <sound/tlv.h>
  23. #define SUN8I_SYSCLK_CTL 0x00c
  24. #define SUN8I_SYSCLK_CTL_AIF1CLK_ENA 11
  25. #define SUN8I_SYSCLK_CTL_AIF1CLK_SRC_PLL (0x2 << 8)
  26. #define SUN8I_SYSCLK_CTL_AIF2CLK_ENA 7
  27. #define SUN8I_SYSCLK_CTL_AIF2CLK_SRC_PLL (0x2 << 4)
  28. #define SUN8I_SYSCLK_CTL_SYSCLK_ENA 3
  29. #define SUN8I_SYSCLK_CTL_SYSCLK_SRC 0
  30. #define SUN8I_SYSCLK_CTL_SYSCLK_SRC_AIF1CLK (0x0 << 0)
  31. #define SUN8I_SYSCLK_CTL_SYSCLK_SRC_AIF2CLK (0x1 << 0)
  32. #define SUN8I_MOD_CLK_ENA 0x010
  33. #define SUN8I_MOD_CLK_ENA_AIF1 15
  34. #define SUN8I_MOD_CLK_ENA_AIF2 14
  35. #define SUN8I_MOD_CLK_ENA_AIF3 13
  36. #define SUN8I_MOD_CLK_ENA_ADC 3
  37. #define SUN8I_MOD_CLK_ENA_DAC 2
  38. #define SUN8I_MOD_RST_CTL 0x014
  39. #define SUN8I_MOD_RST_CTL_AIF1 15
  40. #define SUN8I_MOD_RST_CTL_AIF2 14
  41. #define SUN8I_MOD_RST_CTL_AIF3 13
  42. #define SUN8I_MOD_RST_CTL_ADC 3
  43. #define SUN8I_MOD_RST_CTL_DAC 2
  44. #define SUN8I_SYS_SR_CTRL 0x018
  45. #define SUN8I_SYS_SR_CTRL_AIF1_FS 12
  46. #define SUN8I_SYS_SR_CTRL_AIF2_FS 8
  47. #define SUN8I_AIF_CLK_CTRL(n) (0x040 * (1 + (n)))
  48. #define SUN8I_AIF_CLK_CTRL_MSTR_MOD 15
  49. #define SUN8I_AIF_CLK_CTRL_CLK_INV 13
  50. #define SUN8I_AIF_CLK_CTRL_BCLK_DIV 9
  51. #define SUN8I_AIF_CLK_CTRL_LRCK_DIV 6
  52. #define SUN8I_AIF_CLK_CTRL_WORD_SIZ 4
  53. #define SUN8I_AIF_CLK_CTRL_DATA_FMT 2
  54. #define SUN8I_AIF1_ADCDAT_CTRL 0x044
  55. #define SUN8I_AIF1_ADCDAT_CTRL_AIF1_AD0L_ENA 15
  56. #define SUN8I_AIF1_ADCDAT_CTRL_AIF1_AD0R_ENA 14
  57. #define SUN8I_AIF1_ADCDAT_CTRL_AIF1_AD0L_SRC 10
  58. #define SUN8I_AIF1_ADCDAT_CTRL_AIF1_AD0R_SRC 8
  59. #define SUN8I_AIF1_DACDAT_CTRL 0x048
  60. #define SUN8I_AIF1_DACDAT_CTRL_AIF1_DA0L_ENA 15
  61. #define SUN8I_AIF1_DACDAT_CTRL_AIF1_DA0R_ENA 14
  62. #define SUN8I_AIF1_DACDAT_CTRL_AIF1_DA0L_SRC 10
  63. #define SUN8I_AIF1_DACDAT_CTRL_AIF1_DA0R_SRC 8
  64. #define SUN8I_AIF1_MXR_SRC 0x04c
  65. #define SUN8I_AIF1_MXR_SRC_AD0L_MXR_SRC_AIF1DA0L 15
  66. #define SUN8I_AIF1_MXR_SRC_AD0L_MXR_SRC_AIF2DACL 14
  67. #define SUN8I_AIF1_MXR_SRC_AD0L_MXR_SRC_ADCL 13
  68. #define SUN8I_AIF1_MXR_SRC_AD0L_MXR_SRC_AIF2DACR 12
  69. #define SUN8I_AIF1_MXR_SRC_AD0R_MXR_SRC_AIF1DA0R 11
  70. #define SUN8I_AIF1_MXR_SRC_AD0R_MXR_SRC_AIF2DACR 10
  71. #define SUN8I_AIF1_MXR_SRC_AD0R_MXR_SRC_ADCR 9
  72. #define SUN8I_AIF1_MXR_SRC_AD0R_MXR_SRC_AIF2DACL 8
  73. #define SUN8I_AIF1_VOL_CTRL1 0x050
  74. #define SUN8I_AIF1_VOL_CTRL1_AD0L_VOL 8
  75. #define SUN8I_AIF1_VOL_CTRL1_AD0R_VOL 0
  76. #define SUN8I_AIF1_VOL_CTRL3 0x058
  77. #define SUN8I_AIF1_VOL_CTRL3_DA0L_VOL 8
  78. #define SUN8I_AIF1_VOL_CTRL3_DA0R_VOL 0
  79. #define SUN8I_AIF2_ADCDAT_CTRL 0x084
  80. #define SUN8I_AIF2_ADCDAT_CTRL_AIF2_ADCL_ENA 15
  81. #define SUN8I_AIF2_ADCDAT_CTRL_AIF2_ADCR_ENA 14
  82. #define SUN8I_AIF2_ADCDAT_CTRL_AIF2_ADCL_SRC 10
  83. #define SUN8I_AIF2_ADCDAT_CTRL_AIF2_ADCR_SRC 8
  84. #define SUN8I_AIF2_DACDAT_CTRL 0x088
  85. #define SUN8I_AIF2_DACDAT_CTRL_AIF2_DACL_ENA 15
  86. #define SUN8I_AIF2_DACDAT_CTRL_AIF2_DACR_ENA 14
  87. #define SUN8I_AIF2_DACDAT_CTRL_AIF2_DACL_SRC 10
  88. #define SUN8I_AIF2_DACDAT_CTRL_AIF2_DACR_SRC 8
  89. #define SUN8I_AIF2_MXR_SRC 0x08c
  90. #define SUN8I_AIF2_MXR_SRC_ADCL_MXR_SRC_AIF1DA0L 15
  91. #define SUN8I_AIF2_MXR_SRC_ADCL_MXR_SRC_AIF1DA1L 14
  92. #define SUN8I_AIF2_MXR_SRC_ADCL_MXR_SRC_AIF2DACR 13
  93. #define SUN8I_AIF2_MXR_SRC_ADCL_MXR_SRC_ADCL 12
  94. #define SUN8I_AIF2_MXR_SRC_ADCR_MXR_SRC_AIF1DA0R 11
  95. #define SUN8I_AIF2_MXR_SRC_ADCR_MXR_SRC_AIF1DA1R 10
  96. #define SUN8I_AIF2_MXR_SRC_ADCR_MXR_SRC_AIF2DACL 9
  97. #define SUN8I_AIF2_MXR_SRC_ADCR_MXR_SRC_ADCR 8
  98. #define SUN8I_AIF2_VOL_CTRL1 0x090
  99. #define SUN8I_AIF2_VOL_CTRL1_ADCL_VOL 8
  100. #define SUN8I_AIF2_VOL_CTRL1_ADCR_VOL 0
  101. #define SUN8I_AIF2_VOL_CTRL2 0x098
  102. #define SUN8I_AIF2_VOL_CTRL2_DACL_VOL 8
  103. #define SUN8I_AIF2_VOL_CTRL2_DACR_VOL 0
  104. #define SUN8I_AIF3_CLK_CTRL_AIF3_CLK_SRC_AIF1 (0x0 << 0)
  105. #define SUN8I_AIF3_CLK_CTRL_AIF3_CLK_SRC_AIF2 (0x1 << 0)
  106. #define SUN8I_AIF3_CLK_CTRL_AIF3_CLK_SRC_AIF1CLK (0x2 << 0)
  107. #define SUN8I_AIF3_PATH_CTRL 0x0cc
  108. #define SUN8I_AIF3_PATH_CTRL_AIF3_ADC_SRC 10
  109. #define SUN8I_AIF3_PATH_CTRL_AIF2_DAC_SRC 8
  110. #define SUN8I_AIF3_PATH_CTRL_AIF3_PINS_TRI 7
  111. #define SUN8I_ADC_DIG_CTRL 0x100
  112. #define SUN8I_ADC_DIG_CTRL_ENAD 15
  113. #define SUN8I_ADC_DIG_CTRL_ADOUT_DTS 2
  114. #define SUN8I_ADC_DIG_CTRL_ADOUT_DLY 1
  115. #define SUN8I_ADC_VOL_CTRL 0x104
  116. #define SUN8I_ADC_VOL_CTRL_ADCL_VOL 8
  117. #define SUN8I_ADC_VOL_CTRL_ADCR_VOL 0
  118. #define SUN8I_DAC_DIG_CTRL 0x120
  119. #define SUN8I_DAC_DIG_CTRL_ENDA 15
  120. #define SUN8I_DAC_VOL_CTRL 0x124
  121. #define SUN8I_DAC_VOL_CTRL_DACL_VOL 8
  122. #define SUN8I_DAC_VOL_CTRL_DACR_VOL 0
  123. #define SUN8I_DAC_MXR_SRC 0x130
  124. #define SUN8I_DAC_MXR_SRC_DACL_MXR_SRC_AIF1DA0L 15
  125. #define SUN8I_DAC_MXR_SRC_DACL_MXR_SRC_AIF1DA1L 14
  126. #define SUN8I_DAC_MXR_SRC_DACL_MXR_SRC_AIF2DACL 13
  127. #define SUN8I_DAC_MXR_SRC_DACL_MXR_SRC_ADCL 12
  128. #define SUN8I_DAC_MXR_SRC_DACR_MXR_SRC_AIF1DA0R 11
  129. #define SUN8I_DAC_MXR_SRC_DACR_MXR_SRC_AIF1DA1R 10
  130. #define SUN8I_DAC_MXR_SRC_DACR_MXR_SRC_AIF2DACR 9
  131. #define SUN8I_DAC_MXR_SRC_DACR_MXR_SRC_ADCR 8
  132. #define SUN8I_SYSCLK_CTL_AIF1CLK_SRC_MASK GENMASK(9, 8)
  133. #define SUN8I_SYSCLK_CTL_AIF2CLK_SRC_MASK GENMASK(5, 4)
  134. #define SUN8I_SYS_SR_CTRL_AIF1_FS_MASK GENMASK(15, 12)
  135. #define SUN8I_SYS_SR_CTRL_AIF2_FS_MASK GENMASK(11, 8)
  136. #define SUN8I_AIF_CLK_CTRL_CLK_INV_MASK GENMASK(14, 13)
  137. #define SUN8I_AIF_CLK_CTRL_BCLK_DIV_MASK GENMASK(12, 9)
  138. #define SUN8I_AIF_CLK_CTRL_LRCK_DIV_MASK GENMASK(8, 6)
  139. #define SUN8I_AIF_CLK_CTRL_WORD_SIZ_MASK GENMASK(5, 4)
  140. #define SUN8I_AIF_CLK_CTRL_DATA_FMT_MASK GENMASK(3, 2)
  141. #define SUN8I_AIF3_CLK_CTRL_AIF3_CLK_SRC_MASK GENMASK(1, 0)
  142. #define SUN8I_CODEC_PASSTHROUGH_SAMPLE_RATE 48000
  143. #define SUN8I_CODEC_PCM_FORMATS (SNDRV_PCM_FMTBIT_S8 |\
  144. SNDRV_PCM_FMTBIT_S16_LE |\
  145. SNDRV_PCM_FMTBIT_S20_LE |\
  146. SNDRV_PCM_FMTBIT_S24_LE |\
  147. SNDRV_PCM_FMTBIT_S20_3LE|\
  148. SNDRV_PCM_FMTBIT_S24_3LE)
  149. #define SUN8I_CODEC_PCM_RATES (SNDRV_PCM_RATE_8000_48000|\
  150. SNDRV_PCM_RATE_88200 |\
  151. SNDRV_PCM_RATE_96000 |\
  152. SNDRV_PCM_RATE_176400 |\
  153. SNDRV_PCM_RATE_192000 |\
  154. SNDRV_PCM_RATE_KNOT)
  155. enum {
  156. SUN8I_CODEC_AIF1,
  157. SUN8I_CODEC_AIF2,
  158. SUN8I_CODEC_AIF3,
  159. SUN8I_CODEC_NAIFS
  160. };
  161. struct sun8i_codec_aif {
  162. unsigned int lrck_div_order;
  163. unsigned int sample_rate;
  164. unsigned int slots;
  165. unsigned int slot_width;
  166. unsigned int active_streams : 2;
  167. unsigned int open_streams : 2;
  168. };
  169. struct sun8i_codec_quirks {
  170. bool legacy_widgets : 1;
  171. bool lrck_inversion : 1;
  172. };
  173. struct sun8i_codec {
  174. struct regmap *regmap;
  175. struct clk *clk_module;
  176. const struct sun8i_codec_quirks *quirks;
  177. struct sun8i_codec_aif aifs[SUN8I_CODEC_NAIFS];
  178. unsigned int sysclk_rate;
  179. int sysclk_refcnt;
  180. };
  181. static struct snd_soc_dai_driver sun8i_codec_dais[];
  182. static int sun8i_codec_runtime_resume(struct device *dev)
  183. {
  184. struct sun8i_codec *scodec = dev_get_drvdata(dev);
  185. int ret;
  186. regcache_cache_only(scodec->regmap, false);
  187. ret = regcache_sync(scodec->regmap);
  188. if (ret) {
  189. dev_err(dev, "Failed to sync regmap cache\n");
  190. return ret;
  191. }
  192. return 0;
  193. }
  194. static int sun8i_codec_runtime_suspend(struct device *dev)
  195. {
  196. struct sun8i_codec *scodec = dev_get_drvdata(dev);
  197. regcache_cache_only(scodec->regmap, true);
  198. regcache_mark_dirty(scodec->regmap);
  199. return 0;
  200. }
  201. static int sun8i_codec_get_hw_rate(unsigned int sample_rate)
  202. {
  203. switch (sample_rate) {
  204. case 7350:
  205. case 8000:
  206. return 0x0;
  207. case 11025:
  208. return 0x1;
  209. case 12000:
  210. return 0x2;
  211. case 14700:
  212. case 16000:
  213. return 0x3;
  214. case 22050:
  215. return 0x4;
  216. case 24000:
  217. return 0x5;
  218. case 29400:
  219. case 32000:
  220. return 0x6;
  221. case 44100:
  222. return 0x7;
  223. case 48000:
  224. return 0x8;
  225. case 88200:
  226. case 96000:
  227. return 0x9;
  228. case 176400:
  229. case 192000:
  230. return 0xa;
  231. default:
  232. return -EINVAL;
  233. }
  234. }
  235. static int sun8i_codec_update_sample_rate(struct sun8i_codec *scodec)
  236. {
  237. unsigned int max_rate = 0;
  238. int hw_rate, i;
  239. for (i = SUN8I_CODEC_AIF1; i < SUN8I_CODEC_NAIFS; ++i) {
  240. struct sun8i_codec_aif *aif = &scodec->aifs[i];
  241. if (aif->active_streams)
  242. max_rate = max(max_rate, aif->sample_rate);
  243. }
  244. /* Set the sample rate for ADC->DAC passthrough when no AIF is active. */
  245. if (!max_rate)
  246. max_rate = SUN8I_CODEC_PASSTHROUGH_SAMPLE_RATE;
  247. hw_rate = sun8i_codec_get_hw_rate(max_rate);
  248. if (hw_rate < 0)
  249. return hw_rate;
  250. regmap_update_bits(scodec->regmap, SUN8I_SYS_SR_CTRL,
  251. SUN8I_SYS_SR_CTRL_AIF1_FS_MASK,
  252. hw_rate << SUN8I_SYS_SR_CTRL_AIF1_FS);
  253. return 0;
  254. }
  255. static int sun8i_codec_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
  256. {
  257. struct sun8i_codec *scodec = snd_soc_dai_get_drvdata(dai);
  258. u32 dsp_format, format, invert, value;
  259. /* clock masters */
  260. switch (fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) {
  261. case SND_SOC_DAIFMT_CBC_CFC: /* Codec slave, DAI master */
  262. value = 0x1;
  263. break;
  264. case SND_SOC_DAIFMT_CBP_CFP: /* Codec Master, DAI slave */
  265. value = 0x0;
  266. break;
  267. default:
  268. return -EINVAL;
  269. }
  270. if (dai->id == SUN8I_CODEC_AIF3) {
  271. /* AIF3 only supports master mode. */
  272. if (value)
  273. return -EINVAL;
  274. /* Use the AIF2 BCLK and LRCK for AIF3. */
  275. regmap_update_bits(scodec->regmap, SUN8I_AIF_CLK_CTRL(dai->id),
  276. SUN8I_AIF3_CLK_CTRL_AIF3_CLK_SRC_MASK,
  277. SUN8I_AIF3_CLK_CTRL_AIF3_CLK_SRC_AIF2);
  278. } else {
  279. regmap_update_bits(scodec->regmap, SUN8I_AIF_CLK_CTRL(dai->id),
  280. BIT(SUN8I_AIF_CLK_CTRL_MSTR_MOD),
  281. value << SUN8I_AIF_CLK_CTRL_MSTR_MOD);
  282. }
  283. /* DAI format */
  284. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  285. case SND_SOC_DAIFMT_I2S:
  286. format = 0x0;
  287. break;
  288. case SND_SOC_DAIFMT_LEFT_J:
  289. format = 0x1;
  290. break;
  291. case SND_SOC_DAIFMT_RIGHT_J:
  292. format = 0x2;
  293. break;
  294. case SND_SOC_DAIFMT_DSP_A:
  295. format = 0x3;
  296. dsp_format = 0x0; /* Set LRCK_INV to 0 */
  297. break;
  298. case SND_SOC_DAIFMT_DSP_B:
  299. format = 0x3;
  300. dsp_format = 0x1; /* Set LRCK_INV to 1 */
  301. break;
  302. default:
  303. return -EINVAL;
  304. }
  305. if (dai->id == SUN8I_CODEC_AIF3) {
  306. /* AIF3 only supports DSP mode. */
  307. if (format != 3)
  308. return -EINVAL;
  309. } else {
  310. regmap_update_bits(scodec->regmap, SUN8I_AIF_CLK_CTRL(dai->id),
  311. SUN8I_AIF_CLK_CTRL_DATA_FMT_MASK,
  312. format << SUN8I_AIF_CLK_CTRL_DATA_FMT);
  313. }
  314. /* clock inversion */
  315. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  316. case SND_SOC_DAIFMT_NB_NF: /* Normal */
  317. invert = 0x0;
  318. break;
  319. case SND_SOC_DAIFMT_NB_IF: /* Inverted LRCK */
  320. invert = 0x1;
  321. break;
  322. case SND_SOC_DAIFMT_IB_NF: /* Inverted BCLK */
  323. invert = 0x2;
  324. break;
  325. case SND_SOC_DAIFMT_IB_IF: /* Both inverted */
  326. invert = 0x3;
  327. break;
  328. default:
  329. return -EINVAL;
  330. }
  331. if (format == 0x3) {
  332. /* Inverted LRCK is not available in DSP mode. */
  333. if (invert & BIT(0))
  334. return -EINVAL;
  335. /* Instead, the bit selects between DSP A/B formats. */
  336. invert |= dsp_format;
  337. } else {
  338. /*
  339. * It appears that the DAI and the codec in the A33 SoC don't
  340. * share the same polarity for the LRCK signal when they mean
  341. * 'normal' and 'inverted' in the datasheet.
  342. *
  343. * Since the DAI here is our regular i2s driver that have been
  344. * tested with way more codecs than just this one, it means
  345. * that the codec probably gets it backward, and we have to
  346. * invert the value here.
  347. */
  348. invert ^= scodec->quirks->lrck_inversion;
  349. }
  350. regmap_update_bits(scodec->regmap, SUN8I_AIF_CLK_CTRL(dai->id),
  351. SUN8I_AIF_CLK_CTRL_CLK_INV_MASK,
  352. invert << SUN8I_AIF_CLK_CTRL_CLK_INV);
  353. return 0;
  354. }
  355. static int sun8i_codec_set_tdm_slot(struct snd_soc_dai *dai,
  356. unsigned int tx_mask, unsigned int rx_mask,
  357. int slots, int slot_width)
  358. {
  359. struct sun8i_codec *scodec = snd_soc_dai_get_drvdata(dai);
  360. struct sun8i_codec_aif *aif = &scodec->aifs[dai->id];
  361. if (slot_width && !is_power_of_2(slot_width))
  362. return -EINVAL;
  363. aif->slots = slots;
  364. aif->slot_width = slot_width;
  365. return 0;
  366. }
  367. static const unsigned int sun8i_codec_rates[] = {
  368. 7350, 8000, 11025, 12000, 14700, 16000, 22050, 24000,
  369. 29400, 32000, 44100, 48000, 88200, 96000, 176400, 192000,
  370. };
  371. static const struct snd_pcm_hw_constraint_list sun8i_codec_all_rates = {
  372. .list = sun8i_codec_rates,
  373. .count = ARRAY_SIZE(sun8i_codec_rates),
  374. };
  375. static const struct snd_pcm_hw_constraint_list sun8i_codec_22M_rates = {
  376. .list = sun8i_codec_rates,
  377. .count = ARRAY_SIZE(sun8i_codec_rates),
  378. .mask = 0x5555,
  379. };
  380. static const struct snd_pcm_hw_constraint_list sun8i_codec_24M_rates = {
  381. .list = sun8i_codec_rates,
  382. .count = ARRAY_SIZE(sun8i_codec_rates),
  383. .mask = 0xaaaa,
  384. };
  385. static int sun8i_codec_startup(struct snd_pcm_substream *substream,
  386. struct snd_soc_dai *dai)
  387. {
  388. struct sun8i_codec *scodec = snd_soc_dai_get_drvdata(dai);
  389. const struct snd_pcm_hw_constraint_list *list;
  390. /* hw_constraints is not relevant for codec2codec DAIs. */
  391. if (dai->id != SUN8I_CODEC_AIF1)
  392. return 0;
  393. if (!scodec->sysclk_refcnt)
  394. list = &sun8i_codec_all_rates;
  395. else if (scodec->sysclk_rate == 22579200)
  396. list = &sun8i_codec_22M_rates;
  397. else if (scodec->sysclk_rate == 24576000)
  398. list = &sun8i_codec_24M_rates;
  399. else
  400. return -EINVAL;
  401. return snd_pcm_hw_constraint_list(substream->runtime, 0,
  402. SNDRV_PCM_HW_PARAM_RATE, list);
  403. }
  404. struct sun8i_codec_clk_div {
  405. u8 div;
  406. u8 val;
  407. };
  408. static const struct sun8i_codec_clk_div sun8i_codec_bclk_div[] = {
  409. { .div = 1, .val = 0 },
  410. { .div = 2, .val = 1 },
  411. { .div = 4, .val = 2 },
  412. { .div = 6, .val = 3 },
  413. { .div = 8, .val = 4 },
  414. { .div = 12, .val = 5 },
  415. { .div = 16, .val = 6 },
  416. { .div = 24, .val = 7 },
  417. { .div = 32, .val = 8 },
  418. { .div = 48, .val = 9 },
  419. { .div = 64, .val = 10 },
  420. { .div = 96, .val = 11 },
  421. { .div = 128, .val = 12 },
  422. { .div = 192, .val = 13 },
  423. };
  424. static int sun8i_codec_get_bclk_div(unsigned int sysclk_rate,
  425. unsigned int lrck_div_order,
  426. unsigned int sample_rate)
  427. {
  428. unsigned int div = sysclk_rate / sample_rate >> lrck_div_order;
  429. int i;
  430. for (i = 0; i < ARRAY_SIZE(sun8i_codec_bclk_div); i++) {
  431. const struct sun8i_codec_clk_div *bdiv = &sun8i_codec_bclk_div[i];
  432. if (bdiv->div == div)
  433. return bdiv->val;
  434. }
  435. return -EINVAL;
  436. }
  437. static int sun8i_codec_get_lrck_div_order(unsigned int slots,
  438. unsigned int slot_width)
  439. {
  440. unsigned int div = slots * slot_width;
  441. if (div < 16 || div > 256)
  442. return -EINVAL;
  443. return order_base_2(div);
  444. }
  445. static unsigned int sun8i_codec_get_sysclk_rate(unsigned int sample_rate)
  446. {
  447. return (sample_rate % 4000) ? 22579200 : 24576000;
  448. }
  449. static int sun8i_codec_hw_params(struct snd_pcm_substream *substream,
  450. struct snd_pcm_hw_params *params,
  451. struct snd_soc_dai *dai)
  452. {
  453. struct sun8i_codec *scodec = snd_soc_dai_get_drvdata(dai);
  454. struct sun8i_codec_aif *aif = &scodec->aifs[dai->id];
  455. unsigned int sample_rate = params_rate(params);
  456. unsigned int slots = aif->slots ?: params_channels(params);
  457. unsigned int slot_width = aif->slot_width ?: params_width(params);
  458. unsigned int sysclk_rate = sun8i_codec_get_sysclk_rate(sample_rate);
  459. int bclk_div, lrck_div_order, ret, word_size;
  460. u32 clk_reg;
  461. /* word size */
  462. switch (params_width(params)) {
  463. case 8:
  464. word_size = 0x0;
  465. break;
  466. case 16:
  467. word_size = 0x1;
  468. break;
  469. case 20:
  470. word_size = 0x2;
  471. break;
  472. case 24:
  473. word_size = 0x3;
  474. break;
  475. default:
  476. return -EINVAL;
  477. }
  478. regmap_update_bits(scodec->regmap, SUN8I_AIF_CLK_CTRL(dai->id),
  479. SUN8I_AIF_CLK_CTRL_WORD_SIZ_MASK,
  480. word_size << SUN8I_AIF_CLK_CTRL_WORD_SIZ);
  481. /* LRCK divider (BCLK/LRCK ratio) */
  482. lrck_div_order = sun8i_codec_get_lrck_div_order(slots, slot_width);
  483. if (lrck_div_order < 0)
  484. return lrck_div_order;
  485. if (dai->id == SUN8I_CODEC_AIF2 || dai->id == SUN8I_CODEC_AIF3) {
  486. /* AIF2 and AIF3 share AIF2's BCLK and LRCK generation circuitry. */
  487. int partner = (SUN8I_CODEC_AIF2 + SUN8I_CODEC_AIF3) - dai->id;
  488. const struct sun8i_codec_aif *partner_aif = &scodec->aifs[partner];
  489. const char *partner_name = sun8i_codec_dais[partner].name;
  490. if (partner_aif->open_streams &&
  491. (lrck_div_order != partner_aif->lrck_div_order ||
  492. sample_rate != partner_aif->sample_rate)) {
  493. dev_err(dai->dev,
  494. "%s sample and bit rates must match %s when both are used\n",
  495. dai->name, partner_name);
  496. return -EBUSY;
  497. }
  498. clk_reg = SUN8I_AIF_CLK_CTRL(SUN8I_CODEC_AIF2);
  499. } else {
  500. clk_reg = SUN8I_AIF_CLK_CTRL(dai->id);
  501. }
  502. regmap_update_bits(scodec->regmap, clk_reg,
  503. SUN8I_AIF_CLK_CTRL_LRCK_DIV_MASK,
  504. (lrck_div_order - 4) << SUN8I_AIF_CLK_CTRL_LRCK_DIV);
  505. /* BCLK divider (SYSCLK/BCLK ratio) */
  506. bclk_div = sun8i_codec_get_bclk_div(sysclk_rate, lrck_div_order, sample_rate);
  507. if (bclk_div < 0)
  508. return bclk_div;
  509. regmap_update_bits(scodec->regmap, clk_reg,
  510. SUN8I_AIF_CLK_CTRL_BCLK_DIV_MASK,
  511. bclk_div << SUN8I_AIF_CLK_CTRL_BCLK_DIV);
  512. /*
  513. * SYSCLK rate
  514. *
  515. * Clock rate protection is reference counted; but hw_params may be
  516. * called many times per substream, without matching calls to hw_free.
  517. * Protect the clock rate once per AIF, on the first hw_params call
  518. * for the first substream. clk_set_rate() will allow clock rate
  519. * changes on subsequent calls if only one AIF has open streams.
  520. */
  521. ret = (aif->open_streams ? clk_set_rate : clk_set_rate_exclusive)(scodec->clk_module,
  522. sysclk_rate);
  523. if (ret == -EBUSY)
  524. dev_err(dai->dev,
  525. "%s sample rate (%u Hz) conflicts with other audio streams\n",
  526. dai->name, sample_rate);
  527. if (ret < 0)
  528. return ret;
  529. if (!aif->open_streams)
  530. scodec->sysclk_refcnt++;
  531. scodec->sysclk_rate = sysclk_rate;
  532. aif->lrck_div_order = lrck_div_order;
  533. aif->sample_rate = sample_rate;
  534. aif->open_streams |= BIT(substream->stream);
  535. return sun8i_codec_update_sample_rate(scodec);
  536. }
  537. static int sun8i_codec_hw_free(struct snd_pcm_substream *substream,
  538. struct snd_soc_dai *dai)
  539. {
  540. struct sun8i_codec *scodec = snd_soc_dai_get_drvdata(dai);
  541. struct sun8i_codec_aif *aif = &scodec->aifs[dai->id];
  542. /* Drop references when the last substream for the AIF is freed. */
  543. if (aif->open_streams != BIT(substream->stream))
  544. goto done;
  545. clk_rate_exclusive_put(scodec->clk_module);
  546. scodec->sysclk_refcnt--;
  547. aif->lrck_div_order = 0;
  548. aif->sample_rate = 0;
  549. done:
  550. aif->open_streams &= ~BIT(substream->stream);
  551. return 0;
  552. }
  553. static const struct snd_soc_dai_ops sun8i_codec_dai_ops = {
  554. .set_fmt = sun8i_codec_set_fmt,
  555. .set_tdm_slot = sun8i_codec_set_tdm_slot,
  556. .startup = sun8i_codec_startup,
  557. .hw_params = sun8i_codec_hw_params,
  558. .hw_free = sun8i_codec_hw_free,
  559. };
  560. static struct snd_soc_dai_driver sun8i_codec_dais[] = {
  561. {
  562. .name = "sun8i-codec-aif1",
  563. .id = SUN8I_CODEC_AIF1,
  564. .ops = &sun8i_codec_dai_ops,
  565. /* capture capabilities */
  566. .capture = {
  567. .stream_name = "AIF1 Capture",
  568. .channels_min = 1,
  569. .channels_max = 2,
  570. .rates = SUN8I_CODEC_PCM_RATES,
  571. .formats = SUN8I_CODEC_PCM_FORMATS,
  572. .sig_bits = 24,
  573. },
  574. /* playback capabilities */
  575. .playback = {
  576. .stream_name = "AIF1 Playback",
  577. .channels_min = 1,
  578. .channels_max = 2,
  579. .rates = SUN8I_CODEC_PCM_RATES,
  580. .formats = SUN8I_CODEC_PCM_FORMATS,
  581. },
  582. .symmetric_rate = true,
  583. .symmetric_channels = true,
  584. .symmetric_sample_bits = true,
  585. },
  586. {
  587. .name = "sun8i-codec-aif2",
  588. .id = SUN8I_CODEC_AIF2,
  589. .ops = &sun8i_codec_dai_ops,
  590. /* capture capabilities */
  591. .capture = {
  592. .stream_name = "AIF2 Capture",
  593. .channels_min = 1,
  594. .channels_max = 2,
  595. .rates = SUN8I_CODEC_PCM_RATES,
  596. .formats = SUN8I_CODEC_PCM_FORMATS,
  597. .sig_bits = 24,
  598. },
  599. /* playback capabilities */
  600. .playback = {
  601. .stream_name = "AIF2 Playback",
  602. .channels_min = 1,
  603. .channels_max = 2,
  604. .rates = SUN8I_CODEC_PCM_RATES,
  605. .formats = SUN8I_CODEC_PCM_FORMATS,
  606. },
  607. .symmetric_rate = true,
  608. .symmetric_channels = true,
  609. .symmetric_sample_bits = true,
  610. },
  611. {
  612. .name = "sun8i-codec-aif3",
  613. .id = SUN8I_CODEC_AIF3,
  614. .ops = &sun8i_codec_dai_ops,
  615. /* capture capabilities */
  616. .capture = {
  617. .stream_name = "AIF3 Capture",
  618. .channels_min = 1,
  619. .channels_max = 1,
  620. .rates = SUN8I_CODEC_PCM_RATES,
  621. .formats = SUN8I_CODEC_PCM_FORMATS,
  622. .sig_bits = 24,
  623. },
  624. /* playback capabilities */
  625. .playback = {
  626. .stream_name = "AIF3 Playback",
  627. .channels_min = 1,
  628. .channels_max = 1,
  629. .rates = SUN8I_CODEC_PCM_RATES,
  630. .formats = SUN8I_CODEC_PCM_FORMATS,
  631. },
  632. .symmetric_rate = true,
  633. .symmetric_channels = true,
  634. .symmetric_sample_bits = true,
  635. },
  636. };
  637. static const DECLARE_TLV_DB_SCALE(sun8i_codec_vol_scale, -12000, 75, 1);
  638. static const struct snd_kcontrol_new sun8i_codec_controls[] = {
  639. SOC_DOUBLE_TLV("AIF1 AD0 Capture Volume",
  640. SUN8I_AIF1_VOL_CTRL1,
  641. SUN8I_AIF1_VOL_CTRL1_AD0L_VOL,
  642. SUN8I_AIF1_VOL_CTRL1_AD0R_VOL,
  643. 0xc0, 0, sun8i_codec_vol_scale),
  644. SOC_DOUBLE_TLV("AIF1 DA0 Playback Volume",
  645. SUN8I_AIF1_VOL_CTRL3,
  646. SUN8I_AIF1_VOL_CTRL3_DA0L_VOL,
  647. SUN8I_AIF1_VOL_CTRL3_DA0R_VOL,
  648. 0xc0, 0, sun8i_codec_vol_scale),
  649. SOC_DOUBLE_TLV("AIF2 ADC Capture Volume",
  650. SUN8I_AIF2_VOL_CTRL1,
  651. SUN8I_AIF2_VOL_CTRL1_ADCL_VOL,
  652. SUN8I_AIF2_VOL_CTRL1_ADCR_VOL,
  653. 0xc0, 0, sun8i_codec_vol_scale),
  654. SOC_DOUBLE_TLV("AIF2 DAC Playback Volume",
  655. SUN8I_AIF2_VOL_CTRL2,
  656. SUN8I_AIF2_VOL_CTRL2_DACL_VOL,
  657. SUN8I_AIF2_VOL_CTRL2_DACR_VOL,
  658. 0xc0, 0, sun8i_codec_vol_scale),
  659. SOC_DOUBLE_TLV("ADC Capture Volume",
  660. SUN8I_ADC_VOL_CTRL,
  661. SUN8I_ADC_VOL_CTRL_ADCL_VOL,
  662. SUN8I_ADC_VOL_CTRL_ADCR_VOL,
  663. 0xc0, 0, sun8i_codec_vol_scale),
  664. SOC_DOUBLE_TLV("DAC Playback Volume",
  665. SUN8I_DAC_VOL_CTRL,
  666. SUN8I_DAC_VOL_CTRL_DACL_VOL,
  667. SUN8I_DAC_VOL_CTRL_DACR_VOL,
  668. 0xc0, 0, sun8i_codec_vol_scale),
  669. };
  670. static int sun8i_codec_aif_event(struct snd_soc_dapm_widget *w,
  671. struct snd_kcontrol *kcontrol, int event)
  672. {
  673. struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
  674. struct sun8i_codec *scodec = snd_soc_component_get_drvdata(component);
  675. struct sun8i_codec_aif *aif = &scodec->aifs[w->sname[3] - '1'];
  676. int stream = w->id == snd_soc_dapm_aif_out;
  677. if (SND_SOC_DAPM_EVENT_ON(event))
  678. aif->active_streams |= BIT(stream);
  679. else
  680. aif->active_streams &= ~BIT(stream);
  681. return sun8i_codec_update_sample_rate(scodec);
  682. }
  683. static const char *const sun8i_aif_stereo_mux_enum_values[] = {
  684. "Stereo", "Reverse Stereo", "Sum Mono", "Mix Mono"
  685. };
  686. static SOC_ENUM_DOUBLE_DECL(sun8i_aif1_ad0_stereo_mux_enum,
  687. SUN8I_AIF1_ADCDAT_CTRL,
  688. SUN8I_AIF1_ADCDAT_CTRL_AIF1_AD0L_SRC,
  689. SUN8I_AIF1_ADCDAT_CTRL_AIF1_AD0R_SRC,
  690. sun8i_aif_stereo_mux_enum_values);
  691. static const struct snd_kcontrol_new sun8i_aif1_ad0_stereo_mux_control =
  692. SOC_DAPM_ENUM("AIF1 AD0 Stereo Capture Route",
  693. sun8i_aif1_ad0_stereo_mux_enum);
  694. static SOC_ENUM_DOUBLE_DECL(sun8i_aif2_adc_stereo_mux_enum,
  695. SUN8I_AIF2_ADCDAT_CTRL,
  696. SUN8I_AIF2_ADCDAT_CTRL_AIF2_ADCL_SRC,
  697. SUN8I_AIF2_ADCDAT_CTRL_AIF2_ADCR_SRC,
  698. sun8i_aif_stereo_mux_enum_values);
  699. static const struct snd_kcontrol_new sun8i_aif2_adc_stereo_mux_control =
  700. SOC_DAPM_ENUM("AIF2 ADC Stereo Capture Route",
  701. sun8i_aif2_adc_stereo_mux_enum);
  702. static const char *const sun8i_aif3_adc_mux_enum_values[] = {
  703. "None", "AIF2 ADCL", "AIF2 ADCR"
  704. };
  705. static SOC_ENUM_SINGLE_DECL(sun8i_aif3_adc_mux_enum,
  706. SUN8I_AIF3_PATH_CTRL,
  707. SUN8I_AIF3_PATH_CTRL_AIF3_ADC_SRC,
  708. sun8i_aif3_adc_mux_enum_values);
  709. static const struct snd_kcontrol_new sun8i_aif3_adc_mux_control =
  710. SOC_DAPM_ENUM("AIF3 ADC Source Capture Route",
  711. sun8i_aif3_adc_mux_enum);
  712. static const struct snd_kcontrol_new sun8i_aif1_ad0_mixer_controls[] = {
  713. SOC_DAPM_DOUBLE("AIF1 Slot 0 Digital ADC Capture Switch",
  714. SUN8I_AIF1_MXR_SRC,
  715. SUN8I_AIF1_MXR_SRC_AD0L_MXR_SRC_AIF1DA0L,
  716. SUN8I_AIF1_MXR_SRC_AD0R_MXR_SRC_AIF1DA0R, 1, 0),
  717. SOC_DAPM_DOUBLE("AIF2 Digital ADC Capture Switch",
  718. SUN8I_AIF1_MXR_SRC,
  719. SUN8I_AIF1_MXR_SRC_AD0L_MXR_SRC_AIF2DACL,
  720. SUN8I_AIF1_MXR_SRC_AD0R_MXR_SRC_AIF2DACR, 1, 0),
  721. SOC_DAPM_DOUBLE("AIF1 Data Digital ADC Capture Switch",
  722. SUN8I_AIF1_MXR_SRC,
  723. SUN8I_AIF1_MXR_SRC_AD0L_MXR_SRC_ADCL,
  724. SUN8I_AIF1_MXR_SRC_AD0R_MXR_SRC_ADCR, 1, 0),
  725. SOC_DAPM_DOUBLE("AIF2 Inv Digital ADC Capture Switch",
  726. SUN8I_AIF1_MXR_SRC,
  727. SUN8I_AIF1_MXR_SRC_AD0L_MXR_SRC_AIF2DACR,
  728. SUN8I_AIF1_MXR_SRC_AD0R_MXR_SRC_AIF2DACL, 1, 0),
  729. };
  730. static const struct snd_kcontrol_new sun8i_aif2_adc_mixer_controls[] = {
  731. SOC_DAPM_DOUBLE("AIF2 ADC Mixer AIF1 DA0 Capture Switch",
  732. SUN8I_AIF2_MXR_SRC,
  733. SUN8I_AIF2_MXR_SRC_ADCL_MXR_SRC_AIF1DA0L,
  734. SUN8I_AIF2_MXR_SRC_ADCR_MXR_SRC_AIF1DA0R, 1, 0),
  735. SOC_DAPM_DOUBLE("AIF2 ADC Mixer AIF1 DA1 Capture Switch",
  736. SUN8I_AIF2_MXR_SRC,
  737. SUN8I_AIF2_MXR_SRC_ADCL_MXR_SRC_AIF1DA1L,
  738. SUN8I_AIF2_MXR_SRC_ADCR_MXR_SRC_AIF1DA1R, 1, 0),
  739. SOC_DAPM_DOUBLE("AIF2 ADC Mixer AIF2 DAC Rev Capture Switch",
  740. SUN8I_AIF2_MXR_SRC,
  741. SUN8I_AIF2_MXR_SRC_ADCL_MXR_SRC_AIF2DACR,
  742. SUN8I_AIF2_MXR_SRC_ADCR_MXR_SRC_AIF2DACL, 1, 0),
  743. SOC_DAPM_DOUBLE("AIF2 ADC Mixer ADC Capture Switch",
  744. SUN8I_AIF2_MXR_SRC,
  745. SUN8I_AIF2_MXR_SRC_ADCL_MXR_SRC_ADCL,
  746. SUN8I_AIF2_MXR_SRC_ADCR_MXR_SRC_ADCR, 1, 0),
  747. };
  748. static const char *const sun8i_aif2_dac_mux_enum_values[] = {
  749. "AIF2", "AIF3+2", "AIF2+3"
  750. };
  751. static SOC_ENUM_SINGLE_DECL(sun8i_aif2_dac_mux_enum,
  752. SUN8I_AIF3_PATH_CTRL,
  753. SUN8I_AIF3_PATH_CTRL_AIF2_DAC_SRC,
  754. sun8i_aif2_dac_mux_enum_values);
  755. static const struct snd_kcontrol_new sun8i_aif2_dac_mux_control =
  756. SOC_DAPM_ENUM("AIF2 DAC Source Playback Route",
  757. sun8i_aif2_dac_mux_enum);
  758. static SOC_ENUM_DOUBLE_DECL(sun8i_aif1_da0_stereo_mux_enum,
  759. SUN8I_AIF1_DACDAT_CTRL,
  760. SUN8I_AIF1_DACDAT_CTRL_AIF1_DA0L_SRC,
  761. SUN8I_AIF1_DACDAT_CTRL_AIF1_DA0R_SRC,
  762. sun8i_aif_stereo_mux_enum_values);
  763. static const struct snd_kcontrol_new sun8i_aif1_da0_stereo_mux_control =
  764. SOC_DAPM_ENUM("AIF1 DA0 Stereo Playback Route",
  765. sun8i_aif1_da0_stereo_mux_enum);
  766. static SOC_ENUM_DOUBLE_DECL(sun8i_aif2_dac_stereo_mux_enum,
  767. SUN8I_AIF2_DACDAT_CTRL,
  768. SUN8I_AIF2_DACDAT_CTRL_AIF2_DACL_SRC,
  769. SUN8I_AIF2_DACDAT_CTRL_AIF2_DACR_SRC,
  770. sun8i_aif_stereo_mux_enum_values);
  771. static const struct snd_kcontrol_new sun8i_aif2_dac_stereo_mux_control =
  772. SOC_DAPM_ENUM("AIF2 DAC Stereo Playback Route",
  773. sun8i_aif2_dac_stereo_mux_enum);
  774. static const struct snd_kcontrol_new sun8i_dac_mixer_controls[] = {
  775. SOC_DAPM_DOUBLE("AIF1 Slot 0 Digital DAC Playback Switch",
  776. SUN8I_DAC_MXR_SRC,
  777. SUN8I_DAC_MXR_SRC_DACL_MXR_SRC_AIF1DA0L,
  778. SUN8I_DAC_MXR_SRC_DACR_MXR_SRC_AIF1DA0R, 1, 0),
  779. SOC_DAPM_DOUBLE("AIF1 Slot 1 Digital DAC Playback Switch",
  780. SUN8I_DAC_MXR_SRC,
  781. SUN8I_DAC_MXR_SRC_DACL_MXR_SRC_AIF1DA1L,
  782. SUN8I_DAC_MXR_SRC_DACR_MXR_SRC_AIF1DA1R, 1, 0),
  783. SOC_DAPM_DOUBLE("AIF2 Digital DAC Playback Switch", SUN8I_DAC_MXR_SRC,
  784. SUN8I_DAC_MXR_SRC_DACL_MXR_SRC_AIF2DACL,
  785. SUN8I_DAC_MXR_SRC_DACR_MXR_SRC_AIF2DACR, 1, 0),
  786. SOC_DAPM_DOUBLE("ADC Digital DAC Playback Switch", SUN8I_DAC_MXR_SRC,
  787. SUN8I_DAC_MXR_SRC_DACL_MXR_SRC_ADCL,
  788. SUN8I_DAC_MXR_SRC_DACR_MXR_SRC_ADCR, 1, 0),
  789. };
  790. static const struct snd_soc_dapm_widget sun8i_codec_dapm_widgets[] = {
  791. /* System Clocks */
  792. SND_SOC_DAPM_CLOCK_SUPPLY("mod"),
  793. SND_SOC_DAPM_SUPPLY("AIF1CLK",
  794. SUN8I_SYSCLK_CTL,
  795. SUN8I_SYSCLK_CTL_AIF1CLK_ENA, 0, NULL, 0),
  796. SND_SOC_DAPM_SUPPLY("AIF2CLK",
  797. SUN8I_SYSCLK_CTL,
  798. SUN8I_SYSCLK_CTL_AIF2CLK_ENA, 0, NULL, 0),
  799. SND_SOC_DAPM_SUPPLY("SYSCLK",
  800. SUN8I_SYSCLK_CTL,
  801. SUN8I_SYSCLK_CTL_SYSCLK_ENA, 0, NULL, 0),
  802. /* Module Clocks */
  803. SND_SOC_DAPM_SUPPLY("CLK AIF1",
  804. SUN8I_MOD_CLK_ENA,
  805. SUN8I_MOD_CLK_ENA_AIF1, 0, NULL, 0),
  806. SND_SOC_DAPM_SUPPLY("CLK AIF2",
  807. SUN8I_MOD_CLK_ENA,
  808. SUN8I_MOD_CLK_ENA_AIF2, 0, NULL, 0),
  809. SND_SOC_DAPM_SUPPLY("CLK AIF3",
  810. SUN8I_MOD_CLK_ENA,
  811. SUN8I_MOD_CLK_ENA_AIF3, 0, NULL, 0),
  812. SND_SOC_DAPM_SUPPLY("CLK ADC",
  813. SUN8I_MOD_CLK_ENA,
  814. SUN8I_MOD_CLK_ENA_ADC, 0, NULL, 0),
  815. SND_SOC_DAPM_SUPPLY("CLK DAC",
  816. SUN8I_MOD_CLK_ENA,
  817. SUN8I_MOD_CLK_ENA_DAC, 0, NULL, 0),
  818. /* Module Resets */
  819. SND_SOC_DAPM_SUPPLY("RST AIF1",
  820. SUN8I_MOD_RST_CTL,
  821. SUN8I_MOD_RST_CTL_AIF1, 0, NULL, 0),
  822. SND_SOC_DAPM_SUPPLY("RST AIF2",
  823. SUN8I_MOD_RST_CTL,
  824. SUN8I_MOD_RST_CTL_AIF2, 0, NULL, 0),
  825. SND_SOC_DAPM_SUPPLY("RST AIF3",
  826. SUN8I_MOD_RST_CTL,
  827. SUN8I_MOD_RST_CTL_AIF3, 0, NULL, 0),
  828. SND_SOC_DAPM_SUPPLY("RST ADC",
  829. SUN8I_MOD_RST_CTL,
  830. SUN8I_MOD_RST_CTL_ADC, 0, NULL, 0),
  831. SND_SOC_DAPM_SUPPLY("RST DAC",
  832. SUN8I_MOD_RST_CTL,
  833. SUN8I_MOD_RST_CTL_DAC, 0, NULL, 0),
  834. /* Module Supplies */
  835. SND_SOC_DAPM_SUPPLY("ADC",
  836. SUN8I_ADC_DIG_CTRL,
  837. SUN8I_ADC_DIG_CTRL_ENAD, 0, NULL, 0),
  838. SND_SOC_DAPM_SUPPLY("DAC",
  839. SUN8I_DAC_DIG_CTRL,
  840. SUN8I_DAC_DIG_CTRL_ENDA, 0, NULL, 0),
  841. /* AIF "ADC" Outputs */
  842. SND_SOC_DAPM_AIF_OUT_E("AIF1 AD0L", "AIF1 Capture", 0,
  843. SUN8I_AIF1_ADCDAT_CTRL,
  844. SUN8I_AIF1_ADCDAT_CTRL_AIF1_AD0L_ENA, 0,
  845. sun8i_codec_aif_event,
  846. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  847. SND_SOC_DAPM_AIF_OUT("AIF1 AD0R", "AIF1 Capture", 1,
  848. SUN8I_AIF1_ADCDAT_CTRL,
  849. SUN8I_AIF1_ADCDAT_CTRL_AIF1_AD0R_ENA, 0),
  850. SND_SOC_DAPM_AIF_OUT_E("AIF2 ADCL", "AIF2 Capture", 0,
  851. SUN8I_AIF2_ADCDAT_CTRL,
  852. SUN8I_AIF2_ADCDAT_CTRL_AIF2_ADCL_ENA, 0,
  853. sun8i_codec_aif_event,
  854. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  855. SND_SOC_DAPM_AIF_OUT("AIF2 ADCR", "AIF2 Capture", 1,
  856. SUN8I_AIF2_ADCDAT_CTRL,
  857. SUN8I_AIF2_ADCDAT_CTRL_AIF2_ADCR_ENA, 0),
  858. SND_SOC_DAPM_AIF_OUT_E("AIF3 ADC", "AIF3 Capture", 0,
  859. SND_SOC_NOPM, 0, 0,
  860. sun8i_codec_aif_event,
  861. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  862. /* AIF "ADC" Mono/Stereo Muxes */
  863. SND_SOC_DAPM_MUX("AIF1 AD0L Stereo Mux", SND_SOC_NOPM, 0, 0,
  864. &sun8i_aif1_ad0_stereo_mux_control),
  865. SND_SOC_DAPM_MUX("AIF1 AD0R Stereo Mux", SND_SOC_NOPM, 0, 0,
  866. &sun8i_aif1_ad0_stereo_mux_control),
  867. SND_SOC_DAPM_MUX("AIF2 ADCL Stereo Mux", SND_SOC_NOPM, 0, 0,
  868. &sun8i_aif2_adc_stereo_mux_control),
  869. SND_SOC_DAPM_MUX("AIF2 ADCR Stereo Mux", SND_SOC_NOPM, 0, 0,
  870. &sun8i_aif2_adc_stereo_mux_control),
  871. /* AIF "ADC" Output Muxes */
  872. SND_SOC_DAPM_MUX("AIF3 ADC Source Capture Route", SND_SOC_NOPM, 0, 0,
  873. &sun8i_aif3_adc_mux_control),
  874. /* AIF "ADC" Mixers */
  875. SOC_MIXER_ARRAY("AIF1 AD0L Mixer", SND_SOC_NOPM, 0, 0,
  876. sun8i_aif1_ad0_mixer_controls),
  877. SOC_MIXER_ARRAY("AIF1 AD0R Mixer", SND_SOC_NOPM, 0, 0,
  878. sun8i_aif1_ad0_mixer_controls),
  879. SOC_MIXER_ARRAY("AIF2 ADCL Mixer", SND_SOC_NOPM, 0, 0,
  880. sun8i_aif2_adc_mixer_controls),
  881. SOC_MIXER_ARRAY("AIF2 ADCR Mixer", SND_SOC_NOPM, 0, 0,
  882. sun8i_aif2_adc_mixer_controls),
  883. /* AIF "DAC" Input Muxes */
  884. SND_SOC_DAPM_MUX("AIF2 DACL Source", SND_SOC_NOPM, 0, 0,
  885. &sun8i_aif2_dac_mux_control),
  886. SND_SOC_DAPM_MUX("AIF2 DACR Source", SND_SOC_NOPM, 0, 0,
  887. &sun8i_aif2_dac_mux_control),
  888. /* AIF "DAC" Mono/Stereo Muxes */
  889. SND_SOC_DAPM_MUX("AIF1 DA0L Stereo Mux", SND_SOC_NOPM, 0, 0,
  890. &sun8i_aif1_da0_stereo_mux_control),
  891. SND_SOC_DAPM_MUX("AIF1 DA0R Stereo Mux", SND_SOC_NOPM, 0, 0,
  892. &sun8i_aif1_da0_stereo_mux_control),
  893. SND_SOC_DAPM_MUX("AIF2 DACL Stereo Mux", SND_SOC_NOPM, 0, 0,
  894. &sun8i_aif2_dac_stereo_mux_control),
  895. SND_SOC_DAPM_MUX("AIF2 DACR Stereo Mux", SND_SOC_NOPM, 0, 0,
  896. &sun8i_aif2_dac_stereo_mux_control),
  897. /* AIF "DAC" Inputs */
  898. SND_SOC_DAPM_AIF_IN_E("AIF1 DA0L", "AIF1 Playback", 0,
  899. SUN8I_AIF1_DACDAT_CTRL,
  900. SUN8I_AIF1_DACDAT_CTRL_AIF1_DA0L_ENA, 0,
  901. sun8i_codec_aif_event,
  902. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  903. SND_SOC_DAPM_AIF_IN("AIF1 DA0R", "AIF1 Playback", 1,
  904. SUN8I_AIF1_DACDAT_CTRL,
  905. SUN8I_AIF1_DACDAT_CTRL_AIF1_DA0R_ENA, 0),
  906. SND_SOC_DAPM_AIF_IN_E("AIF2 DACL", "AIF2 Playback", 0,
  907. SUN8I_AIF2_DACDAT_CTRL,
  908. SUN8I_AIF2_DACDAT_CTRL_AIF2_DACL_ENA, 0,
  909. sun8i_codec_aif_event,
  910. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  911. SND_SOC_DAPM_AIF_IN("AIF2 DACR", "AIF2 Playback", 1,
  912. SUN8I_AIF2_DACDAT_CTRL,
  913. SUN8I_AIF2_DACDAT_CTRL_AIF2_DACR_ENA, 0),
  914. SND_SOC_DAPM_AIF_IN_E("AIF3 DAC", "AIF3 Playback", 0,
  915. SND_SOC_NOPM, 0, 0,
  916. sun8i_codec_aif_event,
  917. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  918. /* ADC Inputs (connected to analog codec DAPM context) */
  919. SND_SOC_DAPM_ADC("ADCL", NULL, SND_SOC_NOPM, 0, 0),
  920. SND_SOC_DAPM_ADC("ADCR", NULL, SND_SOC_NOPM, 0, 0),
  921. /* DAC Outputs (connected to analog codec DAPM context) */
  922. SND_SOC_DAPM_DAC("DACL", NULL, SND_SOC_NOPM, 0, 0),
  923. SND_SOC_DAPM_DAC("DACR", NULL, SND_SOC_NOPM, 0, 0),
  924. /* DAC Mixers */
  925. SOC_MIXER_ARRAY("DACL Mixer", SND_SOC_NOPM, 0, 0,
  926. sun8i_dac_mixer_controls),
  927. SOC_MIXER_ARRAY("DACR Mixer", SND_SOC_NOPM, 0, 0,
  928. sun8i_dac_mixer_controls),
  929. };
  930. static const struct snd_soc_dapm_route sun8i_codec_dapm_routes[] = {
  931. /* Clock Routes */
  932. { "AIF1CLK", NULL, "mod" },
  933. { "SYSCLK", NULL, "AIF1CLK" },
  934. { "CLK AIF1", NULL, "AIF1CLK" },
  935. { "CLK AIF1", NULL, "SYSCLK" },
  936. { "RST AIF1", NULL, "CLK AIF1" },
  937. { "AIF1 AD0L", NULL, "RST AIF1" },
  938. { "AIF1 AD0R", NULL, "RST AIF1" },
  939. { "AIF1 DA0L", NULL, "RST AIF1" },
  940. { "AIF1 DA0R", NULL, "RST AIF1" },
  941. { "CLK AIF2", NULL, "AIF2CLK" },
  942. { "CLK AIF2", NULL, "SYSCLK" },
  943. { "RST AIF2", NULL, "CLK AIF2" },
  944. { "AIF2 ADCL", NULL, "RST AIF2" },
  945. { "AIF2 ADCR", NULL, "RST AIF2" },
  946. { "AIF2 DACL", NULL, "RST AIF2" },
  947. { "AIF2 DACR", NULL, "RST AIF2" },
  948. { "CLK AIF3", NULL, "AIF1CLK" },
  949. { "CLK AIF3", NULL, "SYSCLK" },
  950. { "RST AIF3", NULL, "CLK AIF3" },
  951. { "AIF3 ADC", NULL, "RST AIF3" },
  952. { "AIF3 DAC", NULL, "RST AIF3" },
  953. { "CLK ADC", NULL, "SYSCLK" },
  954. { "RST ADC", NULL, "CLK ADC" },
  955. { "ADC", NULL, "RST ADC" },
  956. { "ADCL", NULL, "ADC" },
  957. { "ADCR", NULL, "ADC" },
  958. { "CLK DAC", NULL, "SYSCLK" },
  959. { "RST DAC", NULL, "CLK DAC" },
  960. { "DAC", NULL, "RST DAC" },
  961. { "DACL", NULL, "DAC" },
  962. { "DACR", NULL, "DAC" },
  963. /* AIF "ADC" Output Routes */
  964. { "AIF1 AD0L", NULL, "AIF1 AD0L Stereo Mux" },
  965. { "AIF1 AD0R", NULL, "AIF1 AD0R Stereo Mux" },
  966. { "AIF2 ADCL", NULL, "AIF2 ADCL Stereo Mux" },
  967. { "AIF2 ADCR", NULL, "AIF2 ADCR Stereo Mux" },
  968. { "AIF3 ADC", NULL, "AIF3 ADC Source Capture Route" },
  969. /* AIF "ADC" Mono/Stereo Mux Routes */
  970. { "AIF1 AD0L Stereo Mux", "Stereo", "AIF1 AD0L Mixer" },
  971. { "AIF1 AD0L Stereo Mux", "Reverse Stereo", "AIF1 AD0R Mixer" },
  972. { "AIF1 AD0L Stereo Mux", "Sum Mono", "AIF1 AD0L Mixer" },
  973. { "AIF1 AD0L Stereo Mux", "Sum Mono", "AIF1 AD0R Mixer" },
  974. { "AIF1 AD0L Stereo Mux", "Mix Mono", "AIF1 AD0L Mixer" },
  975. { "AIF1 AD0L Stereo Mux", "Mix Mono", "AIF1 AD0R Mixer" },
  976. { "AIF1 AD0R Stereo Mux", "Stereo", "AIF1 AD0R Mixer" },
  977. { "AIF1 AD0R Stereo Mux", "Reverse Stereo", "AIF1 AD0L Mixer" },
  978. { "AIF1 AD0R Stereo Mux", "Sum Mono", "AIF1 AD0L Mixer" },
  979. { "AIF1 AD0R Stereo Mux", "Sum Mono", "AIF1 AD0R Mixer" },
  980. { "AIF1 AD0R Stereo Mux", "Mix Mono", "AIF1 AD0L Mixer" },
  981. { "AIF1 AD0R Stereo Mux", "Mix Mono", "AIF1 AD0R Mixer" },
  982. { "AIF2 ADCL Stereo Mux", "Stereo", "AIF2 ADCL Mixer" },
  983. { "AIF2 ADCL Stereo Mux", "Reverse Stereo", "AIF2 ADCR Mixer" },
  984. { "AIF2 ADCL Stereo Mux", "Sum Mono", "AIF2 ADCL Mixer" },
  985. { "AIF2 ADCL Stereo Mux", "Sum Mono", "AIF2 ADCR Mixer" },
  986. { "AIF2 ADCL Stereo Mux", "Mix Mono", "AIF2 ADCL Mixer" },
  987. { "AIF2 ADCL Stereo Mux", "Mix Mono", "AIF2 ADCR Mixer" },
  988. { "AIF2 ADCR Stereo Mux", "Stereo", "AIF2 ADCR Mixer" },
  989. { "AIF2 ADCR Stereo Mux", "Reverse Stereo", "AIF2 ADCL Mixer" },
  990. { "AIF2 ADCR Stereo Mux", "Sum Mono", "AIF2 ADCL Mixer" },
  991. { "AIF2 ADCR Stereo Mux", "Sum Mono", "AIF2 ADCR Mixer" },
  992. { "AIF2 ADCR Stereo Mux", "Mix Mono", "AIF2 ADCL Mixer" },
  993. { "AIF2 ADCR Stereo Mux", "Mix Mono", "AIF2 ADCR Mixer" },
  994. /* AIF "ADC" Output Mux Routes */
  995. { "AIF3 ADC Source Capture Route", "AIF2 ADCL", "AIF2 ADCL Mixer" },
  996. { "AIF3 ADC Source Capture Route", "AIF2 ADCR", "AIF2 ADCR Mixer" },
  997. /* AIF "ADC" Mixer Routes */
  998. { "AIF1 AD0L Mixer", "AIF1 Slot 0 Digital ADC Capture Switch", "AIF1 DA0L Stereo Mux" },
  999. { "AIF1 AD0L Mixer", "AIF2 Digital ADC Capture Switch", "AIF2 DACL Source" },
  1000. { "AIF1 AD0L Mixer", "AIF1 Data Digital ADC Capture Switch", "ADCL" },
  1001. { "AIF1 AD0L Mixer", "AIF2 Inv Digital ADC Capture Switch", "AIF2 DACR Source" },
  1002. { "AIF1 AD0R Mixer", "AIF1 Slot 0 Digital ADC Capture Switch", "AIF1 DA0R Stereo Mux" },
  1003. { "AIF1 AD0R Mixer", "AIF2 Digital ADC Capture Switch", "AIF2 DACR Source" },
  1004. { "AIF1 AD0R Mixer", "AIF1 Data Digital ADC Capture Switch", "ADCR" },
  1005. { "AIF1 AD0R Mixer", "AIF2 Inv Digital ADC Capture Switch", "AIF2 DACL Source" },
  1006. { "AIF2 ADCL Mixer", "AIF2 ADC Mixer AIF1 DA0 Capture Switch", "AIF1 DA0L Stereo Mux" },
  1007. { "AIF2 ADCL Mixer", "AIF2 ADC Mixer AIF2 DAC Rev Capture Switch", "AIF2 DACR Source" },
  1008. { "AIF2 ADCL Mixer", "AIF2 ADC Mixer ADC Capture Switch", "ADCL" },
  1009. { "AIF2 ADCR Mixer", "AIF2 ADC Mixer AIF1 DA0 Capture Switch", "AIF1 DA0R Stereo Mux" },
  1010. { "AIF2 ADCR Mixer", "AIF2 ADC Mixer AIF2 DAC Rev Capture Switch", "AIF2 DACL Source" },
  1011. { "AIF2 ADCR Mixer", "AIF2 ADC Mixer ADC Capture Switch", "ADCR" },
  1012. /* AIF "DAC" Input Mux Routes */
  1013. { "AIF2 DACL Source", "AIF2", "AIF2 DACL Stereo Mux" },
  1014. { "AIF2 DACL Source", "AIF3+2", "AIF3 DAC" },
  1015. { "AIF2 DACL Source", "AIF2+3", "AIF2 DACL Stereo Mux" },
  1016. { "AIF2 DACR Source", "AIF2", "AIF2 DACR Stereo Mux" },
  1017. { "AIF2 DACR Source", "AIF3+2", "AIF2 DACR Stereo Mux" },
  1018. { "AIF2 DACR Source", "AIF2+3", "AIF3 DAC" },
  1019. /* AIF "DAC" Mono/Stereo Mux Routes */
  1020. { "AIF1 DA0L Stereo Mux", "Stereo", "AIF1 DA0L" },
  1021. { "AIF1 DA0L Stereo Mux", "Reverse Stereo", "AIF1 DA0R" },
  1022. { "AIF1 DA0L Stereo Mux", "Sum Mono", "AIF1 DA0L" },
  1023. { "AIF1 DA0L Stereo Mux", "Sum Mono", "AIF1 DA0R" },
  1024. { "AIF1 DA0L Stereo Mux", "Mix Mono", "AIF1 DA0L" },
  1025. { "AIF1 DA0L Stereo Mux", "Mix Mono", "AIF1 DA0R" },
  1026. { "AIF1 DA0R Stereo Mux", "Stereo", "AIF1 DA0R" },
  1027. { "AIF1 DA0R Stereo Mux", "Reverse Stereo", "AIF1 DA0L" },
  1028. { "AIF1 DA0R Stereo Mux", "Sum Mono", "AIF1 DA0L" },
  1029. { "AIF1 DA0R Stereo Mux", "Sum Mono", "AIF1 DA0R" },
  1030. { "AIF1 DA0R Stereo Mux", "Mix Mono", "AIF1 DA0L" },
  1031. { "AIF1 DA0R Stereo Mux", "Mix Mono", "AIF1 DA0R" },
  1032. { "AIF2 DACL Stereo Mux", "Stereo", "AIF2 DACL" },
  1033. { "AIF2 DACL Stereo Mux", "Reverse Stereo", "AIF2 DACR" },
  1034. { "AIF2 DACL Stereo Mux", "Sum Mono", "AIF2 DACL" },
  1035. { "AIF2 DACL Stereo Mux", "Sum Mono", "AIF2 DACR" },
  1036. { "AIF2 DACL Stereo Mux", "Mix Mono", "AIF2 DACL" },
  1037. { "AIF2 DACL Stereo Mux", "Mix Mono", "AIF2 DACR" },
  1038. { "AIF2 DACR Stereo Mux", "Stereo", "AIF2 DACR" },
  1039. { "AIF2 DACR Stereo Mux", "Reverse Stereo", "AIF2 DACL" },
  1040. { "AIF2 DACR Stereo Mux", "Sum Mono", "AIF2 DACL" },
  1041. { "AIF2 DACR Stereo Mux", "Sum Mono", "AIF2 DACR" },
  1042. { "AIF2 DACR Stereo Mux", "Mix Mono", "AIF2 DACL" },
  1043. { "AIF2 DACR Stereo Mux", "Mix Mono", "AIF2 DACR" },
  1044. /* DAC Output Routes */
  1045. { "DACL", NULL, "DACL Mixer" },
  1046. { "DACR", NULL, "DACR Mixer" },
  1047. /* DAC Mixer Routes */
  1048. { "DACL Mixer", "AIF1 Slot 0 Digital DAC Playback Switch", "AIF1 DA0L Stereo Mux" },
  1049. { "DACL Mixer", "AIF2 Digital DAC Playback Switch", "AIF2 DACL Source" },
  1050. { "DACL Mixer", "ADC Digital DAC Playback Switch", "ADCL" },
  1051. { "DACR Mixer", "AIF1 Slot 0 Digital DAC Playback Switch", "AIF1 DA0R Stereo Mux" },
  1052. { "DACR Mixer", "AIF2 Digital DAC Playback Switch", "AIF2 DACR Source" },
  1053. { "DACR Mixer", "ADC Digital DAC Playback Switch", "ADCR" },
  1054. };
  1055. static const struct snd_soc_dapm_widget sun8i_codec_legacy_widgets[] = {
  1056. /* Legacy ADC Inputs (connected to analog codec DAPM context) */
  1057. SND_SOC_DAPM_ADC("AIF1 Slot 0 Left ADC", NULL, SND_SOC_NOPM, 0, 0),
  1058. SND_SOC_DAPM_ADC("AIF1 Slot 0 Right ADC", NULL, SND_SOC_NOPM, 0, 0),
  1059. /* Legacy DAC Outputs (connected to analog codec DAPM context) */
  1060. SND_SOC_DAPM_DAC("AIF1 Slot 0 Left", NULL, SND_SOC_NOPM, 0, 0),
  1061. SND_SOC_DAPM_DAC("AIF1 Slot 0 Right", NULL, SND_SOC_NOPM, 0, 0),
  1062. };
  1063. static const struct snd_soc_dapm_route sun8i_codec_legacy_routes[] = {
  1064. /* Legacy ADC Routes */
  1065. { "ADCL", NULL, "AIF1 Slot 0 Left ADC" },
  1066. { "ADCR", NULL, "AIF1 Slot 0 Right ADC" },
  1067. /* Legacy DAC Routes */
  1068. { "AIF1 Slot 0 Left", NULL, "DACL" },
  1069. { "AIF1 Slot 0 Right", NULL, "DACR" },
  1070. };
  1071. static int sun8i_codec_component_probe(struct snd_soc_component *component)
  1072. {
  1073. struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
  1074. struct sun8i_codec *scodec = snd_soc_component_get_drvdata(component);
  1075. int ret;
  1076. /* Add widgets for backward compatibility with old device trees. */
  1077. if (scodec->quirks->legacy_widgets) {
  1078. ret = snd_soc_dapm_new_controls(dapm, sun8i_codec_legacy_widgets,
  1079. ARRAY_SIZE(sun8i_codec_legacy_widgets));
  1080. if (ret)
  1081. return ret;
  1082. ret = snd_soc_dapm_add_routes(dapm, sun8i_codec_legacy_routes,
  1083. ARRAY_SIZE(sun8i_codec_legacy_routes));
  1084. if (ret)
  1085. return ret;
  1086. }
  1087. /*
  1088. * AIF1CLK and AIF2CLK share a pair of clock parents: PLL_AUDIO ("mod")
  1089. * and MCLK (from the CPU DAI connected to AIF1). MCLK's parent is also
  1090. * PLL_AUDIO, so using it adds no additional flexibility. Use PLL_AUDIO
  1091. * directly to simplify the clock tree.
  1092. */
  1093. regmap_update_bits(scodec->regmap, SUN8I_SYSCLK_CTL,
  1094. SUN8I_SYSCLK_CTL_AIF1CLK_SRC_MASK |
  1095. SUN8I_SYSCLK_CTL_AIF2CLK_SRC_MASK,
  1096. SUN8I_SYSCLK_CTL_AIF1CLK_SRC_PLL |
  1097. SUN8I_SYSCLK_CTL_AIF2CLK_SRC_PLL);
  1098. /* Use AIF1CLK as the SYSCLK parent since AIF1 is used most often. */
  1099. regmap_update_bits(scodec->regmap, SUN8I_SYSCLK_CTL,
  1100. BIT(SUN8I_SYSCLK_CTL_SYSCLK_SRC),
  1101. SUN8I_SYSCLK_CTL_SYSCLK_SRC_AIF1CLK);
  1102. /* Program the default sample rate. */
  1103. sun8i_codec_update_sample_rate(scodec);
  1104. return 0;
  1105. }
  1106. static const struct snd_soc_component_driver sun8i_soc_component = {
  1107. .controls = sun8i_codec_controls,
  1108. .num_controls = ARRAY_SIZE(sun8i_codec_controls),
  1109. .dapm_widgets = sun8i_codec_dapm_widgets,
  1110. .num_dapm_widgets = ARRAY_SIZE(sun8i_codec_dapm_widgets),
  1111. .dapm_routes = sun8i_codec_dapm_routes,
  1112. .num_dapm_routes = ARRAY_SIZE(sun8i_codec_dapm_routes),
  1113. .probe = sun8i_codec_component_probe,
  1114. .idle_bias_on = 1,
  1115. .endianness = 1,
  1116. };
  1117. static const struct regmap_config sun8i_codec_regmap_config = {
  1118. .reg_bits = 32,
  1119. .reg_stride = 4,
  1120. .val_bits = 32,
  1121. .max_register = SUN8I_DAC_MXR_SRC,
  1122. .cache_type = REGCACHE_FLAT,
  1123. };
  1124. static int sun8i_codec_probe(struct platform_device *pdev)
  1125. {
  1126. struct sun8i_codec *scodec;
  1127. void __iomem *base;
  1128. int ret;
  1129. scodec = devm_kzalloc(&pdev->dev, sizeof(*scodec), GFP_KERNEL);
  1130. if (!scodec)
  1131. return -ENOMEM;
  1132. scodec->clk_module = devm_clk_get(&pdev->dev, "mod");
  1133. if (IS_ERR(scodec->clk_module)) {
  1134. dev_err(&pdev->dev, "Failed to get the module clock\n");
  1135. return PTR_ERR(scodec->clk_module);
  1136. }
  1137. base = devm_platform_ioremap_resource(pdev, 0);
  1138. if (IS_ERR(base)) {
  1139. dev_err(&pdev->dev, "Failed to map the registers\n");
  1140. return PTR_ERR(base);
  1141. }
  1142. scodec->regmap = devm_regmap_init_mmio_clk(&pdev->dev, "bus", base,
  1143. &sun8i_codec_regmap_config);
  1144. if (IS_ERR(scodec->regmap)) {
  1145. dev_err(&pdev->dev, "Failed to create our regmap\n");
  1146. return PTR_ERR(scodec->regmap);
  1147. }
  1148. scodec->quirks = of_device_get_match_data(&pdev->dev);
  1149. platform_set_drvdata(pdev, scodec);
  1150. pm_runtime_enable(&pdev->dev);
  1151. if (!pm_runtime_enabled(&pdev->dev)) {
  1152. ret = sun8i_codec_runtime_resume(&pdev->dev);
  1153. if (ret)
  1154. goto err_pm_disable;
  1155. }
  1156. ret = devm_snd_soc_register_component(&pdev->dev, &sun8i_soc_component,
  1157. sun8i_codec_dais,
  1158. ARRAY_SIZE(sun8i_codec_dais));
  1159. if (ret) {
  1160. dev_err(&pdev->dev, "Failed to register codec\n");
  1161. goto err_suspend;
  1162. }
  1163. return ret;
  1164. err_suspend:
  1165. if (!pm_runtime_status_suspended(&pdev->dev))
  1166. sun8i_codec_runtime_suspend(&pdev->dev);
  1167. err_pm_disable:
  1168. pm_runtime_disable(&pdev->dev);
  1169. return ret;
  1170. }
  1171. static int sun8i_codec_remove(struct platform_device *pdev)
  1172. {
  1173. pm_runtime_disable(&pdev->dev);
  1174. if (!pm_runtime_status_suspended(&pdev->dev))
  1175. sun8i_codec_runtime_suspend(&pdev->dev);
  1176. return 0;
  1177. }
  1178. static const struct sun8i_codec_quirks sun8i_a33_quirks = {
  1179. .legacy_widgets = true,
  1180. .lrck_inversion = true,
  1181. };
  1182. static const struct sun8i_codec_quirks sun50i_a64_quirks = {
  1183. };
  1184. static const struct of_device_id sun8i_codec_of_match[] = {
  1185. { .compatible = "allwinner,sun8i-a33-codec", .data = &sun8i_a33_quirks },
  1186. { .compatible = "allwinner,sun50i-a64-codec", .data = &sun50i_a64_quirks },
  1187. {}
  1188. };
  1189. MODULE_DEVICE_TABLE(of, sun8i_codec_of_match);
  1190. static const struct dev_pm_ops sun8i_codec_pm_ops = {
  1191. SET_RUNTIME_PM_OPS(sun8i_codec_runtime_suspend,
  1192. sun8i_codec_runtime_resume, NULL)
  1193. };
  1194. static struct platform_driver sun8i_codec_driver = {
  1195. .driver = {
  1196. .name = "sun8i-codec",
  1197. .of_match_table = sun8i_codec_of_match,
  1198. .pm = &sun8i_codec_pm_ops,
  1199. },
  1200. .probe = sun8i_codec_probe,
  1201. .remove = sun8i_codec_remove,
  1202. };
  1203. module_platform_driver(sun8i_codec_driver);
  1204. MODULE_DESCRIPTION("Allwinner A33 (sun8i) codec driver");
  1205. MODULE_AUTHOR("Mylène Josserand <[email protected]>");
  1206. MODULE_LICENSE("GPL");
  1207. MODULE_ALIAS("platform:sun8i-codec");