sun8i-codec.c 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720
  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 <huangxin@Reuuimllatech.com>
  9. * Mylène Josserand <mylene.josserand@free-electrons.com>
  10. */
  11. #include <linux/module.h>
  12. #include <linux/delay.h>
  13. #include <linux/clk.h>
  14. #include <linux/input.h>
  15. #include <linux/io.h>
  16. #include <linux/irq.h>
  17. #include <linux/mutex.h>
  18. #include <linux/of.h>
  19. #include <linux/pm_runtime.h>
  20. #include <linux/regmap.h>
  21. #include <linux/log2.h>
  22. #include <sound/jack.h>
  23. #include <sound/pcm_params.h>
  24. #include <sound/soc.h>
  25. #include <sound/soc-dapm.h>
  26. #include <sound/tlv.h>
  27. #define SUN8I_SYSCLK_CTL 0x00c
  28. #define SUN8I_SYSCLK_CTL_AIF1CLK_ENA 11
  29. #define SUN8I_SYSCLK_CTL_AIF1CLK_SRC_PLL (0x2 << 8)
  30. #define SUN8I_SYSCLK_CTL_AIF2CLK_ENA 7
  31. #define SUN8I_SYSCLK_CTL_AIF2CLK_SRC_PLL (0x2 << 4)
  32. #define SUN8I_SYSCLK_CTL_SYSCLK_ENA 3
  33. #define SUN8I_SYSCLK_CTL_SYSCLK_SRC 0
  34. #define SUN8I_SYSCLK_CTL_SYSCLK_SRC_AIF1CLK (0x0 << 0)
  35. #define SUN8I_SYSCLK_CTL_SYSCLK_SRC_AIF2CLK (0x1 << 0)
  36. #define SUN8I_MOD_CLK_ENA 0x010
  37. #define SUN8I_MOD_CLK_ENA_AIF1 15
  38. #define SUN8I_MOD_CLK_ENA_AIF2 14
  39. #define SUN8I_MOD_CLK_ENA_AIF3 13
  40. #define SUN8I_MOD_CLK_ENA_ADC 3
  41. #define SUN8I_MOD_CLK_ENA_DAC 2
  42. #define SUN8I_MOD_RST_CTL 0x014
  43. #define SUN8I_MOD_RST_CTL_AIF1 15
  44. #define SUN8I_MOD_RST_CTL_AIF2 14
  45. #define SUN8I_MOD_RST_CTL_AIF3 13
  46. #define SUN8I_MOD_RST_CTL_ADC 3
  47. #define SUN8I_MOD_RST_CTL_DAC 2
  48. #define SUN8I_SYS_SR_CTRL 0x018
  49. #define SUN8I_SYS_SR_CTRL_AIF1_FS 12
  50. #define SUN8I_SYS_SR_CTRL_AIF2_FS 8
  51. #define SUN8I_AIF_CLK_CTRL(n) (0x040 * (1 + (n)))
  52. #define SUN8I_AIF_CLK_CTRL_MSTR_MOD 15
  53. #define SUN8I_AIF_CLK_CTRL_CLK_INV 13
  54. #define SUN8I_AIF_CLK_CTRL_BCLK_DIV 9
  55. #define SUN8I_AIF_CLK_CTRL_LRCK_DIV 6
  56. #define SUN8I_AIF_CLK_CTRL_WORD_SIZ 4
  57. #define SUN8I_AIF_CLK_CTRL_DATA_FMT 2
  58. #define SUN8I_AIF1_ADCDAT_CTRL 0x044
  59. #define SUN8I_AIF1_ADCDAT_CTRL_AIF1_AD0L_ENA 15
  60. #define SUN8I_AIF1_ADCDAT_CTRL_AIF1_AD0R_ENA 14
  61. #define SUN8I_AIF1_ADCDAT_CTRL_AIF1_AD0L_SRC 10
  62. #define SUN8I_AIF1_ADCDAT_CTRL_AIF1_AD0R_SRC 8
  63. #define SUN8I_AIF1_DACDAT_CTRL 0x048
  64. #define SUN8I_AIF1_DACDAT_CTRL_AIF1_DA0L_ENA 15
  65. #define SUN8I_AIF1_DACDAT_CTRL_AIF1_DA0R_ENA 14
  66. #define SUN8I_AIF1_DACDAT_CTRL_AIF1_DA0L_SRC 10
  67. #define SUN8I_AIF1_DACDAT_CTRL_AIF1_DA0R_SRC 8
  68. #define SUN8I_AIF1_MXR_SRC 0x04c
  69. #define SUN8I_AIF1_MXR_SRC_AD0L_MXR_SRC_AIF1DA0L 15
  70. #define SUN8I_AIF1_MXR_SRC_AD0L_MXR_SRC_AIF2DACL 14
  71. #define SUN8I_AIF1_MXR_SRC_AD0L_MXR_SRC_ADCL 13
  72. #define SUN8I_AIF1_MXR_SRC_AD0L_MXR_SRC_AIF2DACR 12
  73. #define SUN8I_AIF1_MXR_SRC_AD0R_MXR_SRC_AIF1DA0R 11
  74. #define SUN8I_AIF1_MXR_SRC_AD0R_MXR_SRC_AIF2DACR 10
  75. #define SUN8I_AIF1_MXR_SRC_AD0R_MXR_SRC_ADCR 9
  76. #define SUN8I_AIF1_MXR_SRC_AD0R_MXR_SRC_AIF2DACL 8
  77. #define SUN8I_AIF1_VOL_CTRL1 0x050
  78. #define SUN8I_AIF1_VOL_CTRL1_AD0L_VOL 8
  79. #define SUN8I_AIF1_VOL_CTRL1_AD0R_VOL 0
  80. #define SUN8I_AIF1_VOL_CTRL3 0x058
  81. #define SUN8I_AIF1_VOL_CTRL3_DA0L_VOL 8
  82. #define SUN8I_AIF1_VOL_CTRL3_DA0R_VOL 0
  83. #define SUN8I_AIF2_ADCDAT_CTRL 0x084
  84. #define SUN8I_AIF2_ADCDAT_CTRL_AIF2_ADCL_ENA 15
  85. #define SUN8I_AIF2_ADCDAT_CTRL_AIF2_ADCR_ENA 14
  86. #define SUN8I_AIF2_ADCDAT_CTRL_AIF2_ADCL_SRC 10
  87. #define SUN8I_AIF2_ADCDAT_CTRL_AIF2_ADCR_SRC 8
  88. #define SUN8I_AIF2_DACDAT_CTRL 0x088
  89. #define SUN8I_AIF2_DACDAT_CTRL_AIF2_DACL_ENA 15
  90. #define SUN8I_AIF2_DACDAT_CTRL_AIF2_DACR_ENA 14
  91. #define SUN8I_AIF2_DACDAT_CTRL_AIF2_DACL_SRC 10
  92. #define SUN8I_AIF2_DACDAT_CTRL_AIF2_DACR_SRC 8
  93. #define SUN8I_AIF2_MXR_SRC 0x08c
  94. #define SUN8I_AIF2_MXR_SRC_ADCL_MXR_SRC_AIF1DA0L 15
  95. #define SUN8I_AIF2_MXR_SRC_ADCL_MXR_SRC_AIF1DA1L 14
  96. #define SUN8I_AIF2_MXR_SRC_ADCL_MXR_SRC_AIF2DACR 13
  97. #define SUN8I_AIF2_MXR_SRC_ADCL_MXR_SRC_ADCL 12
  98. #define SUN8I_AIF2_MXR_SRC_ADCR_MXR_SRC_AIF1DA0R 11
  99. #define SUN8I_AIF2_MXR_SRC_ADCR_MXR_SRC_AIF1DA1R 10
  100. #define SUN8I_AIF2_MXR_SRC_ADCR_MXR_SRC_AIF2DACL 9
  101. #define SUN8I_AIF2_MXR_SRC_ADCR_MXR_SRC_ADCR 8
  102. #define SUN8I_AIF2_VOL_CTRL1 0x090
  103. #define SUN8I_AIF2_VOL_CTRL1_ADCL_VOL 8
  104. #define SUN8I_AIF2_VOL_CTRL1_ADCR_VOL 0
  105. #define SUN8I_AIF2_VOL_CTRL2 0x098
  106. #define SUN8I_AIF2_VOL_CTRL2_DACL_VOL 8
  107. #define SUN8I_AIF2_VOL_CTRL2_DACR_VOL 0
  108. #define SUN8I_AIF3_CLK_CTRL_AIF3_CLK_SRC_AIF1 (0x0 << 0)
  109. #define SUN8I_AIF3_CLK_CTRL_AIF3_CLK_SRC_AIF2 (0x1 << 0)
  110. #define SUN8I_AIF3_CLK_CTRL_AIF3_CLK_SRC_AIF1CLK (0x2 << 0)
  111. #define SUN8I_AIF3_PATH_CTRL 0x0cc
  112. #define SUN8I_AIF3_PATH_CTRL_AIF3_ADC_SRC 10
  113. #define SUN8I_AIF3_PATH_CTRL_AIF2_DAC_SRC 8
  114. #define SUN8I_AIF3_PATH_CTRL_AIF3_PINS_TRI 7
  115. #define SUN8I_ADC_DIG_CTRL 0x100
  116. #define SUN8I_ADC_DIG_CTRL_ENAD 15
  117. #define SUN8I_ADC_DIG_CTRL_ADOUT_DTS 2
  118. #define SUN8I_ADC_DIG_CTRL_ADOUT_DLY 1
  119. #define SUN8I_ADC_VOL_CTRL 0x104
  120. #define SUN8I_ADC_VOL_CTRL_ADCL_VOL 8
  121. #define SUN8I_ADC_VOL_CTRL_ADCR_VOL 0
  122. #define SUN8I_HMIC_CTRL1 0x110
  123. #define SUN8I_HMIC_CTRL1_HMIC_M 12
  124. #define SUN8I_HMIC_CTRL1_HMIC_N 8
  125. #define SUN8I_HMIC_CTRL1_MDATA_THRESHOLD_DB 5
  126. #define SUN8I_HMIC_CTRL1_JACK_OUT_IRQ_EN 4
  127. #define SUN8I_HMIC_CTRL1_JACK_IN_IRQ_EN 3
  128. #define SUN8I_HMIC_CTRL1_HMIC_DATA_IRQ_EN 0
  129. #define SUN8I_HMIC_CTRL2 0x114
  130. #define SUN8I_HMIC_CTRL2_HMIC_SAMPLE 14
  131. #define SUN8I_HMIC_CTRL2_HMIC_MDATA_THRESHOLD 8
  132. #define SUN8I_HMIC_CTRL2_HMIC_SF 6
  133. #define SUN8I_HMIC_STS 0x118
  134. #define SUN8I_HMIC_STS_MDATA_DISCARD 13
  135. #define SUN8I_HMIC_STS_HMIC_DATA 8
  136. #define SUN8I_HMIC_STS_JACK_OUT_IRQ_ST 4
  137. #define SUN8I_HMIC_STS_JACK_IN_IRQ_ST 3
  138. #define SUN8I_HMIC_STS_HMIC_DATA_IRQ_ST 0
  139. #define SUN8I_DAC_DIG_CTRL 0x120
  140. #define SUN8I_DAC_DIG_CTRL_ENDA 15
  141. #define SUN8I_DAC_VOL_CTRL 0x124
  142. #define SUN8I_DAC_VOL_CTRL_DACL_VOL 8
  143. #define SUN8I_DAC_VOL_CTRL_DACR_VOL 0
  144. #define SUN8I_DAC_MXR_SRC 0x130
  145. #define SUN8I_DAC_MXR_SRC_DACL_MXR_SRC_AIF1DA0L 15
  146. #define SUN8I_DAC_MXR_SRC_DACL_MXR_SRC_AIF1DA1L 14
  147. #define SUN8I_DAC_MXR_SRC_DACL_MXR_SRC_AIF2DACL 13
  148. #define SUN8I_DAC_MXR_SRC_DACL_MXR_SRC_ADCL 12
  149. #define SUN8I_DAC_MXR_SRC_DACR_MXR_SRC_AIF1DA0R 11
  150. #define SUN8I_DAC_MXR_SRC_DACR_MXR_SRC_AIF1DA1R 10
  151. #define SUN8I_DAC_MXR_SRC_DACR_MXR_SRC_AIF2DACR 9
  152. #define SUN8I_DAC_MXR_SRC_DACR_MXR_SRC_ADCR 8
  153. #define SUN8I_SYSCLK_CTL_AIF1CLK_SRC_MASK GENMASK(9, 8)
  154. #define SUN8I_SYSCLK_CTL_AIF2CLK_SRC_MASK GENMASK(5, 4)
  155. #define SUN8I_SYS_SR_CTRL_AIF1_FS_MASK GENMASK(15, 12)
  156. #define SUN8I_SYS_SR_CTRL_AIF2_FS_MASK GENMASK(11, 8)
  157. #define SUN8I_AIF_CLK_CTRL_CLK_INV_MASK GENMASK(14, 13)
  158. #define SUN8I_AIF_CLK_CTRL_BCLK_DIV_MASK GENMASK(12, 9)
  159. #define SUN8I_AIF_CLK_CTRL_LRCK_DIV_MASK GENMASK(8, 6)
  160. #define SUN8I_AIF_CLK_CTRL_WORD_SIZ_MASK GENMASK(5, 4)
  161. #define SUN8I_AIF_CLK_CTRL_DATA_FMT_MASK GENMASK(3, 2)
  162. #define SUN8I_AIF3_CLK_CTRL_AIF3_CLK_SRC_MASK GENMASK(1, 0)
  163. #define SUN8I_HMIC_CTRL1_HMIC_M_MASK GENMASK(15, 12)
  164. #define SUN8I_HMIC_CTRL1_HMIC_N_MASK GENMASK(11, 8)
  165. #define SUN8I_HMIC_CTRL1_MDATA_THRESHOLD_DB_MASK GENMASK(6, 5)
  166. #define SUN8I_HMIC_CTRL2_HMIC_SAMPLE_MASK GENMASK(15, 14)
  167. #define SUN8I_HMIC_CTRL2_HMIC_SF_MASK GENMASK(7, 6)
  168. #define SUN8I_HMIC_STS_HMIC_DATA_MASK GENMASK(12, 8)
  169. #define SUN8I_CODEC_BUTTONS (SND_JACK_BTN_0|\
  170. SND_JACK_BTN_1|\
  171. SND_JACK_BTN_2|\
  172. SND_JACK_BTN_3)
  173. #define SUN8I_CODEC_PASSTHROUGH_SAMPLE_RATE 48000
  174. #define SUN8I_CODEC_PCM_FORMATS (SNDRV_PCM_FMTBIT_S8 |\
  175. SNDRV_PCM_FMTBIT_S16_LE |\
  176. SNDRV_PCM_FMTBIT_S20_LE |\
  177. SNDRV_PCM_FMTBIT_S24_LE |\
  178. SNDRV_PCM_FMTBIT_S20_3LE|\
  179. SNDRV_PCM_FMTBIT_S24_3LE)
  180. #define SUN8I_CODEC_PCM_RATES (SNDRV_PCM_RATE_8000_48000|\
  181. SNDRV_PCM_RATE_88200 |\
  182. SNDRV_PCM_RATE_96000 |\
  183. SNDRV_PCM_RATE_176400 |\
  184. SNDRV_PCM_RATE_192000 |\
  185. SNDRV_PCM_RATE_KNOT)
  186. enum {
  187. SUN8I_CODEC_AIF1,
  188. SUN8I_CODEC_AIF2,
  189. SUN8I_CODEC_AIF3,
  190. SUN8I_CODEC_NAIFS
  191. };
  192. struct sun8i_codec_aif {
  193. unsigned int lrck_div_order;
  194. unsigned int sample_rate;
  195. unsigned int slots;
  196. unsigned int slot_width;
  197. unsigned int active_streams : 2;
  198. unsigned int open_streams : 2;
  199. };
  200. struct sun8i_codec_quirks {
  201. bool bus_clock : 1;
  202. bool jack_detection : 1;
  203. bool legacy_widgets : 1;
  204. bool lrck_inversion : 1;
  205. };
  206. enum {
  207. SUN8I_JACK_STATUS_DISCONNECTED,
  208. SUN8I_JACK_STATUS_WAITING_HBIAS,
  209. SUN8I_JACK_STATUS_CONNECTED,
  210. };
  211. struct sun8i_codec {
  212. struct snd_soc_component *component;
  213. struct regmap *regmap;
  214. struct clk *clk_bus;
  215. struct clk *clk_module;
  216. const struct sun8i_codec_quirks *quirks;
  217. struct sun8i_codec_aif aifs[SUN8I_CODEC_NAIFS];
  218. struct snd_soc_jack *jack;
  219. struct delayed_work jack_work;
  220. int jack_irq;
  221. int jack_status;
  222. int jack_type;
  223. int jack_last_sample;
  224. ktime_t jack_hbias_ready;
  225. struct mutex jack_mutex;
  226. int last_hmic_irq;
  227. unsigned int sysclk_rate;
  228. int sysclk_refcnt;
  229. };
  230. static struct snd_soc_dai_driver sun8i_codec_dais[];
  231. static int sun8i_codec_runtime_resume(struct device *dev)
  232. {
  233. struct sun8i_codec *scodec = dev_get_drvdata(dev);
  234. int ret;
  235. ret = clk_prepare_enable(scodec->clk_bus);
  236. if (ret) {
  237. dev_err(dev, "Failed to enable the bus clock\n");
  238. return ret;
  239. }
  240. regcache_cache_only(scodec->regmap, false);
  241. ret = regcache_sync(scodec->regmap);
  242. if (ret) {
  243. dev_err(dev, "Failed to sync regmap cache\n");
  244. return ret;
  245. }
  246. return 0;
  247. }
  248. static int sun8i_codec_runtime_suspend(struct device *dev)
  249. {
  250. struct sun8i_codec *scodec = dev_get_drvdata(dev);
  251. regcache_cache_only(scodec->regmap, true);
  252. regcache_mark_dirty(scodec->regmap);
  253. clk_disable_unprepare(scodec->clk_bus);
  254. return 0;
  255. }
  256. static int sun8i_codec_get_hw_rate(unsigned int sample_rate)
  257. {
  258. switch (sample_rate) {
  259. case 7350:
  260. case 8000:
  261. return 0x0;
  262. case 11025:
  263. return 0x1;
  264. case 12000:
  265. return 0x2;
  266. case 14700:
  267. case 16000:
  268. return 0x3;
  269. case 22050:
  270. return 0x4;
  271. case 24000:
  272. return 0x5;
  273. case 29400:
  274. case 32000:
  275. return 0x6;
  276. case 44100:
  277. return 0x7;
  278. case 48000:
  279. return 0x8;
  280. case 88200:
  281. case 96000:
  282. return 0x9;
  283. case 176400:
  284. case 192000:
  285. return 0xa;
  286. default:
  287. return -EINVAL;
  288. }
  289. }
  290. static int sun8i_codec_update_sample_rate(struct sun8i_codec *scodec)
  291. {
  292. unsigned int max_rate = 0;
  293. int hw_rate, i;
  294. for (i = SUN8I_CODEC_AIF1; i < SUN8I_CODEC_NAIFS; ++i) {
  295. struct sun8i_codec_aif *aif = &scodec->aifs[i];
  296. if (aif->active_streams)
  297. max_rate = max(max_rate, aif->sample_rate);
  298. }
  299. /* Set the sample rate for ADC->DAC passthrough when no AIF is active. */
  300. if (!max_rate)
  301. max_rate = SUN8I_CODEC_PASSTHROUGH_SAMPLE_RATE;
  302. hw_rate = sun8i_codec_get_hw_rate(max_rate);
  303. if (hw_rate < 0)
  304. return hw_rate;
  305. regmap_update_bits(scodec->regmap, SUN8I_SYS_SR_CTRL,
  306. SUN8I_SYS_SR_CTRL_AIF1_FS_MASK,
  307. hw_rate << SUN8I_SYS_SR_CTRL_AIF1_FS);
  308. return 0;
  309. }
  310. static int sun8i_codec_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
  311. {
  312. struct sun8i_codec *scodec = snd_soc_dai_get_drvdata(dai);
  313. u32 dsp_format, format, invert, value;
  314. /* clock masters */
  315. switch (fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) {
  316. case SND_SOC_DAIFMT_CBC_CFC: /* Codec slave, DAI master */
  317. value = 0x1;
  318. break;
  319. case SND_SOC_DAIFMT_CBP_CFP: /* Codec Master, DAI slave */
  320. value = 0x0;
  321. break;
  322. default:
  323. return -EINVAL;
  324. }
  325. if (dai->id == SUN8I_CODEC_AIF3) {
  326. /* AIF3 only supports master mode. */
  327. if (value)
  328. return -EINVAL;
  329. /* Use the AIF2 BCLK and LRCK for AIF3. */
  330. regmap_update_bits(scodec->regmap, SUN8I_AIF_CLK_CTRL(dai->id),
  331. SUN8I_AIF3_CLK_CTRL_AIF3_CLK_SRC_MASK,
  332. SUN8I_AIF3_CLK_CTRL_AIF3_CLK_SRC_AIF2);
  333. } else {
  334. regmap_update_bits(scodec->regmap, SUN8I_AIF_CLK_CTRL(dai->id),
  335. BIT(SUN8I_AIF_CLK_CTRL_MSTR_MOD),
  336. value << SUN8I_AIF_CLK_CTRL_MSTR_MOD);
  337. }
  338. /* DAI format */
  339. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  340. case SND_SOC_DAIFMT_I2S:
  341. format = 0x0;
  342. break;
  343. case SND_SOC_DAIFMT_LEFT_J:
  344. format = 0x1;
  345. break;
  346. case SND_SOC_DAIFMT_RIGHT_J:
  347. format = 0x2;
  348. break;
  349. case SND_SOC_DAIFMT_DSP_A:
  350. format = 0x3;
  351. dsp_format = 0x0; /* Set LRCK_INV to 0 */
  352. break;
  353. case SND_SOC_DAIFMT_DSP_B:
  354. format = 0x3;
  355. dsp_format = 0x1; /* Set LRCK_INV to 1 */
  356. break;
  357. default:
  358. return -EINVAL;
  359. }
  360. if (dai->id == SUN8I_CODEC_AIF3) {
  361. /* AIF3 only supports DSP mode. */
  362. if (format != 3)
  363. return -EINVAL;
  364. } else {
  365. regmap_update_bits(scodec->regmap, SUN8I_AIF_CLK_CTRL(dai->id),
  366. SUN8I_AIF_CLK_CTRL_DATA_FMT_MASK,
  367. format << SUN8I_AIF_CLK_CTRL_DATA_FMT);
  368. }
  369. /* clock inversion */
  370. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  371. case SND_SOC_DAIFMT_NB_NF: /* Normal */
  372. invert = 0x0;
  373. break;
  374. case SND_SOC_DAIFMT_NB_IF: /* Inverted LRCK */
  375. invert = 0x1;
  376. break;
  377. case SND_SOC_DAIFMT_IB_NF: /* Inverted BCLK */
  378. invert = 0x2;
  379. break;
  380. case SND_SOC_DAIFMT_IB_IF: /* Both inverted */
  381. invert = 0x3;
  382. break;
  383. default:
  384. return -EINVAL;
  385. }
  386. if (format == 0x3) {
  387. /* Inverted LRCK is not available in DSP mode. */
  388. if (invert & BIT(0))
  389. return -EINVAL;
  390. /* Instead, the bit selects between DSP A/B formats. */
  391. invert |= dsp_format;
  392. } else {
  393. /*
  394. * It appears that the DAI and the codec in the A33 SoC don't
  395. * share the same polarity for the LRCK signal when they mean
  396. * 'normal' and 'inverted' in the datasheet.
  397. *
  398. * Since the DAI here is our regular i2s driver that have been
  399. * tested with way more codecs than just this one, it means
  400. * that the codec probably gets it backward, and we have to
  401. * invert the value here.
  402. */
  403. invert ^= scodec->quirks->lrck_inversion;
  404. }
  405. regmap_update_bits(scodec->regmap, SUN8I_AIF_CLK_CTRL(dai->id),
  406. SUN8I_AIF_CLK_CTRL_CLK_INV_MASK,
  407. invert << SUN8I_AIF_CLK_CTRL_CLK_INV);
  408. return 0;
  409. }
  410. static int sun8i_codec_set_tdm_slot(struct snd_soc_dai *dai,
  411. unsigned int tx_mask, unsigned int rx_mask,
  412. int slots, int slot_width)
  413. {
  414. struct sun8i_codec *scodec = snd_soc_dai_get_drvdata(dai);
  415. struct sun8i_codec_aif *aif = &scodec->aifs[dai->id];
  416. if (slot_width && !is_power_of_2(slot_width))
  417. return -EINVAL;
  418. aif->slots = slots;
  419. aif->slot_width = slot_width;
  420. return 0;
  421. }
  422. static const unsigned int sun8i_codec_rates[] = {
  423. 7350, 8000, 11025, 12000, 14700, 16000, 22050, 24000,
  424. 29400, 32000, 44100, 48000, 88200, 96000, 176400, 192000,
  425. };
  426. static const struct snd_pcm_hw_constraint_list sun8i_codec_all_rates = {
  427. .list = sun8i_codec_rates,
  428. .count = ARRAY_SIZE(sun8i_codec_rates),
  429. };
  430. static const struct snd_pcm_hw_constraint_list sun8i_codec_22M_rates = {
  431. .list = sun8i_codec_rates,
  432. .count = ARRAY_SIZE(sun8i_codec_rates),
  433. .mask = 0x5555,
  434. };
  435. static const struct snd_pcm_hw_constraint_list sun8i_codec_24M_rates = {
  436. .list = sun8i_codec_rates,
  437. .count = ARRAY_SIZE(sun8i_codec_rates),
  438. .mask = 0xaaaa,
  439. };
  440. static int sun8i_codec_startup(struct snd_pcm_substream *substream,
  441. struct snd_soc_dai *dai)
  442. {
  443. struct sun8i_codec *scodec = snd_soc_dai_get_drvdata(dai);
  444. const struct snd_pcm_hw_constraint_list *list;
  445. /* hw_constraints is not relevant for codec2codec DAIs. */
  446. if (dai->id != SUN8I_CODEC_AIF1)
  447. return 0;
  448. if (!scodec->sysclk_refcnt)
  449. list = &sun8i_codec_all_rates;
  450. else if (scodec->sysclk_rate == 22579200)
  451. list = &sun8i_codec_22M_rates;
  452. else if (scodec->sysclk_rate == 24576000)
  453. list = &sun8i_codec_24M_rates;
  454. else
  455. return -EINVAL;
  456. return snd_pcm_hw_constraint_list(substream->runtime, 0,
  457. SNDRV_PCM_HW_PARAM_RATE, list);
  458. }
  459. struct sun8i_codec_clk_div {
  460. u8 div;
  461. u8 val;
  462. };
  463. static const struct sun8i_codec_clk_div sun8i_codec_bclk_div[] = {
  464. { .div = 1, .val = 0 },
  465. { .div = 2, .val = 1 },
  466. { .div = 4, .val = 2 },
  467. { .div = 6, .val = 3 },
  468. { .div = 8, .val = 4 },
  469. { .div = 12, .val = 5 },
  470. { .div = 16, .val = 6 },
  471. { .div = 24, .val = 7 },
  472. { .div = 32, .val = 8 },
  473. { .div = 48, .val = 9 },
  474. { .div = 64, .val = 10 },
  475. { .div = 96, .val = 11 },
  476. { .div = 128, .val = 12 },
  477. { .div = 192, .val = 13 },
  478. };
  479. static int sun8i_codec_get_bclk_div(unsigned int sysclk_rate,
  480. unsigned int lrck_div_order,
  481. unsigned int sample_rate)
  482. {
  483. unsigned int div = sysclk_rate / sample_rate >> lrck_div_order;
  484. int i;
  485. for (i = 0; i < ARRAY_SIZE(sun8i_codec_bclk_div); i++) {
  486. const struct sun8i_codec_clk_div *bdiv = &sun8i_codec_bclk_div[i];
  487. if (bdiv->div == div)
  488. return bdiv->val;
  489. }
  490. return -EINVAL;
  491. }
  492. static int sun8i_codec_get_lrck_div_order(unsigned int slots,
  493. unsigned int slot_width)
  494. {
  495. unsigned int div = slots * slot_width;
  496. if (div < 16 || div > 256)
  497. return -EINVAL;
  498. return order_base_2(div);
  499. }
  500. static unsigned int sun8i_codec_get_sysclk_rate(unsigned int sample_rate)
  501. {
  502. return (sample_rate % 4000) ? 22579200 : 24576000;
  503. }
  504. static int sun8i_codec_hw_params(struct snd_pcm_substream *substream,
  505. struct snd_pcm_hw_params *params,
  506. struct snd_soc_dai *dai)
  507. {
  508. struct sun8i_codec *scodec = snd_soc_dai_get_drvdata(dai);
  509. struct sun8i_codec_aif *aif = &scodec->aifs[dai->id];
  510. unsigned int sample_rate = params_rate(params);
  511. unsigned int slots = aif->slots ?: params_channels(params);
  512. unsigned int slot_width = aif->slot_width ?: params_width(params);
  513. unsigned int sysclk_rate = sun8i_codec_get_sysclk_rate(sample_rate);
  514. int bclk_div, lrck_div_order, ret, word_size;
  515. u32 clk_reg;
  516. /* word size */
  517. switch (params_width(params)) {
  518. case 8:
  519. word_size = 0x0;
  520. break;
  521. case 16:
  522. word_size = 0x1;
  523. break;
  524. case 20:
  525. word_size = 0x2;
  526. break;
  527. case 24:
  528. word_size = 0x3;
  529. break;
  530. default:
  531. return -EINVAL;
  532. }
  533. regmap_update_bits(scodec->regmap, SUN8I_AIF_CLK_CTRL(dai->id),
  534. SUN8I_AIF_CLK_CTRL_WORD_SIZ_MASK,
  535. word_size << SUN8I_AIF_CLK_CTRL_WORD_SIZ);
  536. /* LRCK divider (BCLK/LRCK ratio) */
  537. lrck_div_order = sun8i_codec_get_lrck_div_order(slots, slot_width);
  538. if (lrck_div_order < 0)
  539. return lrck_div_order;
  540. if (dai->id == SUN8I_CODEC_AIF2 || dai->id == SUN8I_CODEC_AIF3) {
  541. /* AIF2 and AIF3 share AIF2's BCLK and LRCK generation circuitry. */
  542. int partner = (SUN8I_CODEC_AIF2 + SUN8I_CODEC_AIF3) - dai->id;
  543. const struct sun8i_codec_aif *partner_aif = &scodec->aifs[partner];
  544. const char *partner_name = sun8i_codec_dais[partner].name;
  545. if (partner_aif->open_streams &&
  546. (lrck_div_order != partner_aif->lrck_div_order ||
  547. sample_rate != partner_aif->sample_rate)) {
  548. dev_err(dai->dev,
  549. "%s sample and bit rates must match %s when both are used\n",
  550. dai->name, partner_name);
  551. return -EBUSY;
  552. }
  553. clk_reg = SUN8I_AIF_CLK_CTRL(SUN8I_CODEC_AIF2);
  554. } else {
  555. clk_reg = SUN8I_AIF_CLK_CTRL(dai->id);
  556. }
  557. regmap_update_bits(scodec->regmap, clk_reg,
  558. SUN8I_AIF_CLK_CTRL_LRCK_DIV_MASK,
  559. (lrck_div_order - 4) << SUN8I_AIF_CLK_CTRL_LRCK_DIV);
  560. /* BCLK divider (SYSCLK/BCLK ratio) */
  561. bclk_div = sun8i_codec_get_bclk_div(sysclk_rate, lrck_div_order, sample_rate);
  562. if (bclk_div < 0)
  563. return bclk_div;
  564. regmap_update_bits(scodec->regmap, clk_reg,
  565. SUN8I_AIF_CLK_CTRL_BCLK_DIV_MASK,
  566. bclk_div << SUN8I_AIF_CLK_CTRL_BCLK_DIV);
  567. /*
  568. * SYSCLK rate
  569. *
  570. * Clock rate protection is reference counted; but hw_params may be
  571. * called many times per substream, without matching calls to hw_free.
  572. * Protect the clock rate once per AIF, on the first hw_params call
  573. * for the first substream. clk_set_rate() will allow clock rate
  574. * changes on subsequent calls if only one AIF has open streams.
  575. */
  576. ret = (aif->open_streams ? clk_set_rate : clk_set_rate_exclusive)(scodec->clk_module,
  577. sysclk_rate);
  578. if (ret == -EBUSY)
  579. dev_err(dai->dev,
  580. "%s sample rate (%u Hz) conflicts with other audio streams\n",
  581. dai->name, sample_rate);
  582. if (ret < 0)
  583. return ret;
  584. if (!aif->open_streams)
  585. scodec->sysclk_refcnt++;
  586. scodec->sysclk_rate = sysclk_rate;
  587. aif->lrck_div_order = lrck_div_order;
  588. aif->sample_rate = sample_rate;
  589. aif->open_streams |= BIT(substream->stream);
  590. return sun8i_codec_update_sample_rate(scodec);
  591. }
  592. static int sun8i_codec_hw_free(struct snd_pcm_substream *substream,
  593. struct snd_soc_dai *dai)
  594. {
  595. struct sun8i_codec *scodec = snd_soc_dai_get_drvdata(dai);
  596. struct sun8i_codec_aif *aif = &scodec->aifs[dai->id];
  597. /* Drop references when the last substream for the AIF is freed. */
  598. if (aif->open_streams != BIT(substream->stream))
  599. goto done;
  600. clk_rate_exclusive_put(scodec->clk_module);
  601. scodec->sysclk_refcnt--;
  602. aif->lrck_div_order = 0;
  603. aif->sample_rate = 0;
  604. done:
  605. aif->open_streams &= ~BIT(substream->stream);
  606. return 0;
  607. }
  608. static const struct snd_soc_dai_ops sun8i_codec_dai_ops = {
  609. .set_fmt = sun8i_codec_set_fmt,
  610. .set_tdm_slot = sun8i_codec_set_tdm_slot,
  611. .startup = sun8i_codec_startup,
  612. .hw_params = sun8i_codec_hw_params,
  613. .hw_free = sun8i_codec_hw_free,
  614. };
  615. static struct snd_soc_dai_driver sun8i_codec_dais[] = {
  616. {
  617. .name = "sun8i-codec-aif1",
  618. .id = SUN8I_CODEC_AIF1,
  619. .ops = &sun8i_codec_dai_ops,
  620. /* capture capabilities */
  621. .capture = {
  622. .stream_name = "AIF1 Capture",
  623. .channels_min = 1,
  624. .channels_max = 2,
  625. .rates = SUN8I_CODEC_PCM_RATES,
  626. .formats = SUN8I_CODEC_PCM_FORMATS,
  627. .sig_bits = 24,
  628. },
  629. /* playback capabilities */
  630. .playback = {
  631. .stream_name = "AIF1 Playback",
  632. .channels_min = 1,
  633. .channels_max = 2,
  634. .rates = SUN8I_CODEC_PCM_RATES,
  635. .formats = SUN8I_CODEC_PCM_FORMATS,
  636. },
  637. .symmetric_rate = true,
  638. .symmetric_channels = true,
  639. .symmetric_sample_bits = true,
  640. },
  641. {
  642. .name = "sun8i-codec-aif2",
  643. .id = SUN8I_CODEC_AIF2,
  644. .ops = &sun8i_codec_dai_ops,
  645. /* capture capabilities */
  646. .capture = {
  647. .stream_name = "AIF2 Capture",
  648. .channels_min = 1,
  649. .channels_max = 2,
  650. .rates = SUN8I_CODEC_PCM_RATES,
  651. .formats = SUN8I_CODEC_PCM_FORMATS,
  652. .sig_bits = 24,
  653. },
  654. /* playback capabilities */
  655. .playback = {
  656. .stream_name = "AIF2 Playback",
  657. .channels_min = 1,
  658. .channels_max = 2,
  659. .rates = SUN8I_CODEC_PCM_RATES,
  660. .formats = SUN8I_CODEC_PCM_FORMATS,
  661. },
  662. .symmetric_rate = true,
  663. .symmetric_channels = true,
  664. .symmetric_sample_bits = true,
  665. },
  666. {
  667. .name = "sun8i-codec-aif3",
  668. .id = SUN8I_CODEC_AIF3,
  669. .ops = &sun8i_codec_dai_ops,
  670. /* capture capabilities */
  671. .capture = {
  672. .stream_name = "AIF3 Capture",
  673. .channels_min = 1,
  674. .channels_max = 1,
  675. .rates = SUN8I_CODEC_PCM_RATES,
  676. .formats = SUN8I_CODEC_PCM_FORMATS,
  677. .sig_bits = 24,
  678. },
  679. /* playback capabilities */
  680. .playback = {
  681. .stream_name = "AIF3 Playback",
  682. .channels_min = 1,
  683. .channels_max = 1,
  684. .rates = SUN8I_CODEC_PCM_RATES,
  685. .formats = SUN8I_CODEC_PCM_FORMATS,
  686. },
  687. .symmetric_rate = true,
  688. .symmetric_channels = true,
  689. .symmetric_sample_bits = true,
  690. },
  691. };
  692. static const DECLARE_TLV_DB_SCALE(sun8i_codec_vol_scale, -12000, 75, 1);
  693. static const struct snd_kcontrol_new sun8i_codec_controls[] = {
  694. SOC_DOUBLE_TLV("AIF1 AD0 Capture Volume",
  695. SUN8I_AIF1_VOL_CTRL1,
  696. SUN8I_AIF1_VOL_CTRL1_AD0L_VOL,
  697. SUN8I_AIF1_VOL_CTRL1_AD0R_VOL,
  698. 0xc0, 0, sun8i_codec_vol_scale),
  699. SOC_DOUBLE_TLV("AIF1 DA0 Playback Volume",
  700. SUN8I_AIF1_VOL_CTRL3,
  701. SUN8I_AIF1_VOL_CTRL3_DA0L_VOL,
  702. SUN8I_AIF1_VOL_CTRL3_DA0R_VOL,
  703. 0xc0, 0, sun8i_codec_vol_scale),
  704. SOC_DOUBLE_TLV("AIF2 ADC Capture Volume",
  705. SUN8I_AIF2_VOL_CTRL1,
  706. SUN8I_AIF2_VOL_CTRL1_ADCL_VOL,
  707. SUN8I_AIF2_VOL_CTRL1_ADCR_VOL,
  708. 0xc0, 0, sun8i_codec_vol_scale),
  709. SOC_DOUBLE_TLV("AIF2 DAC Playback Volume",
  710. SUN8I_AIF2_VOL_CTRL2,
  711. SUN8I_AIF2_VOL_CTRL2_DACL_VOL,
  712. SUN8I_AIF2_VOL_CTRL2_DACR_VOL,
  713. 0xc0, 0, sun8i_codec_vol_scale),
  714. SOC_DOUBLE_TLV("ADC Capture Volume",
  715. SUN8I_ADC_VOL_CTRL,
  716. SUN8I_ADC_VOL_CTRL_ADCL_VOL,
  717. SUN8I_ADC_VOL_CTRL_ADCR_VOL,
  718. 0xc0, 0, sun8i_codec_vol_scale),
  719. SOC_DOUBLE_TLV("DAC Playback Volume",
  720. SUN8I_DAC_VOL_CTRL,
  721. SUN8I_DAC_VOL_CTRL_DACL_VOL,
  722. SUN8I_DAC_VOL_CTRL_DACR_VOL,
  723. 0xc0, 0, sun8i_codec_vol_scale),
  724. };
  725. static int sun8i_codec_aif_event(struct snd_soc_dapm_widget *w,
  726. struct snd_kcontrol *kcontrol, int event)
  727. {
  728. struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
  729. struct sun8i_codec *scodec = snd_soc_component_get_drvdata(component);
  730. struct sun8i_codec_aif *aif = &scodec->aifs[w->sname[3] - '1'];
  731. int stream = w->id == snd_soc_dapm_aif_out;
  732. if (SND_SOC_DAPM_EVENT_ON(event))
  733. aif->active_streams |= BIT(stream);
  734. else
  735. aif->active_streams &= ~BIT(stream);
  736. return sun8i_codec_update_sample_rate(scodec);
  737. }
  738. static const char *const sun8i_aif_stereo_mux_enum_values[] = {
  739. "Stereo", "Reverse Stereo", "Sum Mono", "Mix Mono"
  740. };
  741. static SOC_ENUM_DOUBLE_DECL(sun8i_aif1_ad0_stereo_mux_enum,
  742. SUN8I_AIF1_ADCDAT_CTRL,
  743. SUN8I_AIF1_ADCDAT_CTRL_AIF1_AD0L_SRC,
  744. SUN8I_AIF1_ADCDAT_CTRL_AIF1_AD0R_SRC,
  745. sun8i_aif_stereo_mux_enum_values);
  746. static const struct snd_kcontrol_new sun8i_aif1_ad0_stereo_mux_control =
  747. SOC_DAPM_ENUM("AIF1 AD0 Stereo Capture Route",
  748. sun8i_aif1_ad0_stereo_mux_enum);
  749. static SOC_ENUM_DOUBLE_DECL(sun8i_aif2_adc_stereo_mux_enum,
  750. SUN8I_AIF2_ADCDAT_CTRL,
  751. SUN8I_AIF2_ADCDAT_CTRL_AIF2_ADCL_SRC,
  752. SUN8I_AIF2_ADCDAT_CTRL_AIF2_ADCR_SRC,
  753. sun8i_aif_stereo_mux_enum_values);
  754. static const struct snd_kcontrol_new sun8i_aif2_adc_stereo_mux_control =
  755. SOC_DAPM_ENUM("AIF2 ADC Stereo Capture Route",
  756. sun8i_aif2_adc_stereo_mux_enum);
  757. static const char *const sun8i_aif3_adc_mux_enum_values[] = {
  758. "None", "AIF2 ADCL", "AIF2 ADCR"
  759. };
  760. static SOC_ENUM_SINGLE_DECL(sun8i_aif3_adc_mux_enum,
  761. SUN8I_AIF3_PATH_CTRL,
  762. SUN8I_AIF3_PATH_CTRL_AIF3_ADC_SRC,
  763. sun8i_aif3_adc_mux_enum_values);
  764. static const struct snd_kcontrol_new sun8i_aif3_adc_mux_control =
  765. SOC_DAPM_ENUM("AIF3 ADC Source Capture Route",
  766. sun8i_aif3_adc_mux_enum);
  767. static const struct snd_kcontrol_new sun8i_aif1_ad0_mixer_controls[] = {
  768. SOC_DAPM_DOUBLE("AIF1 Slot 0 Digital ADC Capture Switch",
  769. SUN8I_AIF1_MXR_SRC,
  770. SUN8I_AIF1_MXR_SRC_AD0L_MXR_SRC_AIF1DA0L,
  771. SUN8I_AIF1_MXR_SRC_AD0R_MXR_SRC_AIF1DA0R, 1, 0),
  772. SOC_DAPM_DOUBLE("AIF2 Digital ADC Capture Switch",
  773. SUN8I_AIF1_MXR_SRC,
  774. SUN8I_AIF1_MXR_SRC_AD0L_MXR_SRC_AIF2DACL,
  775. SUN8I_AIF1_MXR_SRC_AD0R_MXR_SRC_AIF2DACR, 1, 0),
  776. SOC_DAPM_DOUBLE("AIF1 Data Digital ADC Capture Switch",
  777. SUN8I_AIF1_MXR_SRC,
  778. SUN8I_AIF1_MXR_SRC_AD0L_MXR_SRC_ADCL,
  779. SUN8I_AIF1_MXR_SRC_AD0R_MXR_SRC_ADCR, 1, 0),
  780. SOC_DAPM_DOUBLE("AIF2 Inv Digital ADC Capture Switch",
  781. SUN8I_AIF1_MXR_SRC,
  782. SUN8I_AIF1_MXR_SRC_AD0L_MXR_SRC_AIF2DACR,
  783. SUN8I_AIF1_MXR_SRC_AD0R_MXR_SRC_AIF2DACL, 1, 0),
  784. };
  785. static const struct snd_kcontrol_new sun8i_aif2_adc_mixer_controls[] = {
  786. SOC_DAPM_DOUBLE("AIF2 ADC Mixer AIF1 DA0 Capture Switch",
  787. SUN8I_AIF2_MXR_SRC,
  788. SUN8I_AIF2_MXR_SRC_ADCL_MXR_SRC_AIF1DA0L,
  789. SUN8I_AIF2_MXR_SRC_ADCR_MXR_SRC_AIF1DA0R, 1, 0),
  790. SOC_DAPM_DOUBLE("AIF2 ADC Mixer AIF1 DA1 Capture Switch",
  791. SUN8I_AIF2_MXR_SRC,
  792. SUN8I_AIF2_MXR_SRC_ADCL_MXR_SRC_AIF1DA1L,
  793. SUN8I_AIF2_MXR_SRC_ADCR_MXR_SRC_AIF1DA1R, 1, 0),
  794. SOC_DAPM_DOUBLE("AIF2 ADC Mixer AIF2 DAC Rev Capture Switch",
  795. SUN8I_AIF2_MXR_SRC,
  796. SUN8I_AIF2_MXR_SRC_ADCL_MXR_SRC_AIF2DACR,
  797. SUN8I_AIF2_MXR_SRC_ADCR_MXR_SRC_AIF2DACL, 1, 0),
  798. SOC_DAPM_DOUBLE("AIF2 ADC Mixer ADC Capture Switch",
  799. SUN8I_AIF2_MXR_SRC,
  800. SUN8I_AIF2_MXR_SRC_ADCL_MXR_SRC_ADCL,
  801. SUN8I_AIF2_MXR_SRC_ADCR_MXR_SRC_ADCR, 1, 0),
  802. };
  803. static const char *const sun8i_aif2_dac_mux_enum_values[] = {
  804. "AIF2", "AIF3+2", "AIF2+3"
  805. };
  806. static SOC_ENUM_SINGLE_DECL(sun8i_aif2_dac_mux_enum,
  807. SUN8I_AIF3_PATH_CTRL,
  808. SUN8I_AIF3_PATH_CTRL_AIF2_DAC_SRC,
  809. sun8i_aif2_dac_mux_enum_values);
  810. static const struct snd_kcontrol_new sun8i_aif2_dac_mux_control =
  811. SOC_DAPM_ENUM("AIF2 DAC Source Playback Route",
  812. sun8i_aif2_dac_mux_enum);
  813. static SOC_ENUM_DOUBLE_DECL(sun8i_aif1_da0_stereo_mux_enum,
  814. SUN8I_AIF1_DACDAT_CTRL,
  815. SUN8I_AIF1_DACDAT_CTRL_AIF1_DA0L_SRC,
  816. SUN8I_AIF1_DACDAT_CTRL_AIF1_DA0R_SRC,
  817. sun8i_aif_stereo_mux_enum_values);
  818. static const struct snd_kcontrol_new sun8i_aif1_da0_stereo_mux_control =
  819. SOC_DAPM_ENUM("AIF1 DA0 Stereo Playback Route",
  820. sun8i_aif1_da0_stereo_mux_enum);
  821. static SOC_ENUM_DOUBLE_DECL(sun8i_aif2_dac_stereo_mux_enum,
  822. SUN8I_AIF2_DACDAT_CTRL,
  823. SUN8I_AIF2_DACDAT_CTRL_AIF2_DACL_SRC,
  824. SUN8I_AIF2_DACDAT_CTRL_AIF2_DACR_SRC,
  825. sun8i_aif_stereo_mux_enum_values);
  826. static const struct snd_kcontrol_new sun8i_aif2_dac_stereo_mux_control =
  827. SOC_DAPM_ENUM("AIF2 DAC Stereo Playback Route",
  828. sun8i_aif2_dac_stereo_mux_enum);
  829. static const struct snd_kcontrol_new sun8i_dac_mixer_controls[] = {
  830. SOC_DAPM_DOUBLE("AIF1 Slot 0 Digital DAC Playback Switch",
  831. SUN8I_DAC_MXR_SRC,
  832. SUN8I_DAC_MXR_SRC_DACL_MXR_SRC_AIF1DA0L,
  833. SUN8I_DAC_MXR_SRC_DACR_MXR_SRC_AIF1DA0R, 1, 0),
  834. SOC_DAPM_DOUBLE("AIF1 Slot 1 Digital DAC Playback Switch",
  835. SUN8I_DAC_MXR_SRC,
  836. SUN8I_DAC_MXR_SRC_DACL_MXR_SRC_AIF1DA1L,
  837. SUN8I_DAC_MXR_SRC_DACR_MXR_SRC_AIF1DA1R, 1, 0),
  838. SOC_DAPM_DOUBLE("AIF2 Digital DAC Playback Switch", SUN8I_DAC_MXR_SRC,
  839. SUN8I_DAC_MXR_SRC_DACL_MXR_SRC_AIF2DACL,
  840. SUN8I_DAC_MXR_SRC_DACR_MXR_SRC_AIF2DACR, 1, 0),
  841. SOC_DAPM_DOUBLE("ADC Digital DAC Playback Switch", SUN8I_DAC_MXR_SRC,
  842. SUN8I_DAC_MXR_SRC_DACL_MXR_SRC_ADCL,
  843. SUN8I_DAC_MXR_SRC_DACR_MXR_SRC_ADCR, 1, 0),
  844. };
  845. static const struct snd_soc_dapm_widget sun8i_codec_dapm_widgets[] = {
  846. /* System Clocks */
  847. SND_SOC_DAPM_CLOCK_SUPPLY("mod"),
  848. SND_SOC_DAPM_SUPPLY("AIF1CLK",
  849. SUN8I_SYSCLK_CTL,
  850. SUN8I_SYSCLK_CTL_AIF1CLK_ENA, 0, NULL, 0),
  851. SND_SOC_DAPM_SUPPLY("AIF2CLK",
  852. SUN8I_SYSCLK_CTL,
  853. SUN8I_SYSCLK_CTL_AIF2CLK_ENA, 0, NULL, 0),
  854. SND_SOC_DAPM_SUPPLY("SYSCLK",
  855. SUN8I_SYSCLK_CTL,
  856. SUN8I_SYSCLK_CTL_SYSCLK_ENA, 0, NULL, 0),
  857. /* Module Clocks */
  858. SND_SOC_DAPM_SUPPLY("CLK AIF1",
  859. SUN8I_MOD_CLK_ENA,
  860. SUN8I_MOD_CLK_ENA_AIF1, 0, NULL, 0),
  861. SND_SOC_DAPM_SUPPLY("CLK AIF2",
  862. SUN8I_MOD_CLK_ENA,
  863. SUN8I_MOD_CLK_ENA_AIF2, 0, NULL, 0),
  864. SND_SOC_DAPM_SUPPLY("CLK AIF3",
  865. SUN8I_MOD_CLK_ENA,
  866. SUN8I_MOD_CLK_ENA_AIF3, 0, NULL, 0),
  867. SND_SOC_DAPM_SUPPLY("CLK ADC",
  868. SUN8I_MOD_CLK_ENA,
  869. SUN8I_MOD_CLK_ENA_ADC, 0, NULL, 0),
  870. SND_SOC_DAPM_SUPPLY("CLK DAC",
  871. SUN8I_MOD_CLK_ENA,
  872. SUN8I_MOD_CLK_ENA_DAC, 0, NULL, 0),
  873. /* Module Resets */
  874. SND_SOC_DAPM_SUPPLY("RST AIF1",
  875. SUN8I_MOD_RST_CTL,
  876. SUN8I_MOD_RST_CTL_AIF1, 0, NULL, 0),
  877. SND_SOC_DAPM_SUPPLY("RST AIF2",
  878. SUN8I_MOD_RST_CTL,
  879. SUN8I_MOD_RST_CTL_AIF2, 0, NULL, 0),
  880. SND_SOC_DAPM_SUPPLY("RST AIF3",
  881. SUN8I_MOD_RST_CTL,
  882. SUN8I_MOD_RST_CTL_AIF3, 0, NULL, 0),
  883. SND_SOC_DAPM_SUPPLY("RST ADC",
  884. SUN8I_MOD_RST_CTL,
  885. SUN8I_MOD_RST_CTL_ADC, 0, NULL, 0),
  886. SND_SOC_DAPM_SUPPLY("RST DAC",
  887. SUN8I_MOD_RST_CTL,
  888. SUN8I_MOD_RST_CTL_DAC, 0, NULL, 0),
  889. /* Module Supplies */
  890. SND_SOC_DAPM_SUPPLY("ADC",
  891. SUN8I_ADC_DIG_CTRL,
  892. SUN8I_ADC_DIG_CTRL_ENAD, 0, NULL, 0),
  893. SND_SOC_DAPM_SUPPLY("DAC",
  894. SUN8I_DAC_DIG_CTRL,
  895. SUN8I_DAC_DIG_CTRL_ENDA, 0, NULL, 0),
  896. /* AIF "ADC" Outputs */
  897. SND_SOC_DAPM_AIF_OUT_E("AIF1 AD0L", "AIF1 Capture", 0,
  898. SUN8I_AIF1_ADCDAT_CTRL,
  899. SUN8I_AIF1_ADCDAT_CTRL_AIF1_AD0L_ENA, 0,
  900. sun8i_codec_aif_event,
  901. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  902. SND_SOC_DAPM_AIF_OUT("AIF1 AD0R", "AIF1 Capture", 1,
  903. SUN8I_AIF1_ADCDAT_CTRL,
  904. SUN8I_AIF1_ADCDAT_CTRL_AIF1_AD0R_ENA, 0),
  905. SND_SOC_DAPM_AIF_OUT_E("AIF2 ADCL", "AIF2 Capture", 0,
  906. SUN8I_AIF2_ADCDAT_CTRL,
  907. SUN8I_AIF2_ADCDAT_CTRL_AIF2_ADCL_ENA, 0,
  908. sun8i_codec_aif_event,
  909. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  910. SND_SOC_DAPM_AIF_OUT("AIF2 ADCR", "AIF2 Capture", 1,
  911. SUN8I_AIF2_ADCDAT_CTRL,
  912. SUN8I_AIF2_ADCDAT_CTRL_AIF2_ADCR_ENA, 0),
  913. SND_SOC_DAPM_AIF_OUT_E("AIF3 ADC", "AIF3 Capture", 0,
  914. SND_SOC_NOPM, 0, 0,
  915. sun8i_codec_aif_event,
  916. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  917. /* AIF "ADC" Mono/Stereo Muxes */
  918. SND_SOC_DAPM_MUX("AIF1 AD0L Stereo Mux", SND_SOC_NOPM, 0, 0,
  919. &sun8i_aif1_ad0_stereo_mux_control),
  920. SND_SOC_DAPM_MUX("AIF1 AD0R Stereo Mux", SND_SOC_NOPM, 0, 0,
  921. &sun8i_aif1_ad0_stereo_mux_control),
  922. SND_SOC_DAPM_MUX("AIF2 ADCL Stereo Mux", SND_SOC_NOPM, 0, 0,
  923. &sun8i_aif2_adc_stereo_mux_control),
  924. SND_SOC_DAPM_MUX("AIF2 ADCR Stereo Mux", SND_SOC_NOPM, 0, 0,
  925. &sun8i_aif2_adc_stereo_mux_control),
  926. /* AIF "ADC" Output Muxes */
  927. SND_SOC_DAPM_MUX("AIF3 ADC Source Capture Route", SND_SOC_NOPM, 0, 0,
  928. &sun8i_aif3_adc_mux_control),
  929. /* AIF "ADC" Mixers */
  930. SOC_MIXER_ARRAY("AIF1 AD0L Mixer", SND_SOC_NOPM, 0, 0,
  931. sun8i_aif1_ad0_mixer_controls),
  932. SOC_MIXER_ARRAY("AIF1 AD0R Mixer", SND_SOC_NOPM, 0, 0,
  933. sun8i_aif1_ad0_mixer_controls),
  934. SOC_MIXER_ARRAY("AIF2 ADCL Mixer", SND_SOC_NOPM, 0, 0,
  935. sun8i_aif2_adc_mixer_controls),
  936. SOC_MIXER_ARRAY("AIF2 ADCR Mixer", SND_SOC_NOPM, 0, 0,
  937. sun8i_aif2_adc_mixer_controls),
  938. /* AIF "DAC" Input Muxes */
  939. SND_SOC_DAPM_MUX("AIF2 DACL Source", SND_SOC_NOPM, 0, 0,
  940. &sun8i_aif2_dac_mux_control),
  941. SND_SOC_DAPM_MUX("AIF2 DACR Source", SND_SOC_NOPM, 0, 0,
  942. &sun8i_aif2_dac_mux_control),
  943. /* AIF "DAC" Mono/Stereo Muxes */
  944. SND_SOC_DAPM_MUX("AIF1 DA0L Stereo Mux", SND_SOC_NOPM, 0, 0,
  945. &sun8i_aif1_da0_stereo_mux_control),
  946. SND_SOC_DAPM_MUX("AIF1 DA0R Stereo Mux", SND_SOC_NOPM, 0, 0,
  947. &sun8i_aif1_da0_stereo_mux_control),
  948. SND_SOC_DAPM_MUX("AIF2 DACL Stereo Mux", SND_SOC_NOPM, 0, 0,
  949. &sun8i_aif2_dac_stereo_mux_control),
  950. SND_SOC_DAPM_MUX("AIF2 DACR Stereo Mux", SND_SOC_NOPM, 0, 0,
  951. &sun8i_aif2_dac_stereo_mux_control),
  952. /* AIF "DAC" Inputs */
  953. SND_SOC_DAPM_AIF_IN_E("AIF1 DA0L", "AIF1 Playback", 0,
  954. SUN8I_AIF1_DACDAT_CTRL,
  955. SUN8I_AIF1_DACDAT_CTRL_AIF1_DA0L_ENA, 0,
  956. sun8i_codec_aif_event,
  957. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  958. SND_SOC_DAPM_AIF_IN("AIF1 DA0R", "AIF1 Playback", 1,
  959. SUN8I_AIF1_DACDAT_CTRL,
  960. SUN8I_AIF1_DACDAT_CTRL_AIF1_DA0R_ENA, 0),
  961. SND_SOC_DAPM_AIF_IN_E("AIF2 DACL", "AIF2 Playback", 0,
  962. SUN8I_AIF2_DACDAT_CTRL,
  963. SUN8I_AIF2_DACDAT_CTRL_AIF2_DACL_ENA, 0,
  964. sun8i_codec_aif_event,
  965. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  966. SND_SOC_DAPM_AIF_IN("AIF2 DACR", "AIF2 Playback", 1,
  967. SUN8I_AIF2_DACDAT_CTRL,
  968. SUN8I_AIF2_DACDAT_CTRL_AIF2_DACR_ENA, 0),
  969. SND_SOC_DAPM_AIF_IN_E("AIF3 DAC", "AIF3 Playback", 0,
  970. SND_SOC_NOPM, 0, 0,
  971. sun8i_codec_aif_event,
  972. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  973. /* ADC Inputs (connected to analog codec DAPM context) */
  974. SND_SOC_DAPM_ADC("ADCL", NULL, SND_SOC_NOPM, 0, 0),
  975. SND_SOC_DAPM_ADC("ADCR", NULL, SND_SOC_NOPM, 0, 0),
  976. /* DAC Outputs (connected to analog codec DAPM context) */
  977. SND_SOC_DAPM_DAC("DACL", NULL, SND_SOC_NOPM, 0, 0),
  978. SND_SOC_DAPM_DAC("DACR", NULL, SND_SOC_NOPM, 0, 0),
  979. /* DAC Mixers */
  980. SOC_MIXER_ARRAY("DACL Mixer", SND_SOC_NOPM, 0, 0,
  981. sun8i_dac_mixer_controls),
  982. SOC_MIXER_ARRAY("DACR Mixer", SND_SOC_NOPM, 0, 0,
  983. sun8i_dac_mixer_controls),
  984. };
  985. static const struct snd_soc_dapm_route sun8i_codec_dapm_routes[] = {
  986. /* Clock Routes */
  987. { "AIF1CLK", NULL, "mod" },
  988. { "SYSCLK", NULL, "AIF1CLK" },
  989. { "CLK AIF1", NULL, "AIF1CLK" },
  990. { "CLK AIF1", NULL, "SYSCLK" },
  991. { "RST AIF1", NULL, "CLK AIF1" },
  992. { "AIF1 AD0L", NULL, "RST AIF1" },
  993. { "AIF1 AD0R", NULL, "RST AIF1" },
  994. { "AIF1 DA0L", NULL, "RST AIF1" },
  995. { "AIF1 DA0R", NULL, "RST AIF1" },
  996. { "CLK AIF2", NULL, "AIF2CLK" },
  997. { "CLK AIF2", NULL, "SYSCLK" },
  998. { "RST AIF2", NULL, "CLK AIF2" },
  999. { "AIF2 ADCL", NULL, "RST AIF2" },
  1000. { "AIF2 ADCR", NULL, "RST AIF2" },
  1001. { "AIF2 DACL", NULL, "RST AIF2" },
  1002. { "AIF2 DACR", NULL, "RST AIF2" },
  1003. { "CLK AIF3", NULL, "AIF1CLK" },
  1004. { "CLK AIF3", NULL, "SYSCLK" },
  1005. { "RST AIF3", NULL, "CLK AIF3" },
  1006. { "AIF3 ADC", NULL, "RST AIF3" },
  1007. { "AIF3 DAC", NULL, "RST AIF3" },
  1008. { "CLK ADC", NULL, "SYSCLK" },
  1009. { "RST ADC", NULL, "CLK ADC" },
  1010. { "ADC", NULL, "RST ADC" },
  1011. { "ADCL", NULL, "ADC" },
  1012. { "ADCR", NULL, "ADC" },
  1013. { "CLK DAC", NULL, "SYSCLK" },
  1014. { "RST DAC", NULL, "CLK DAC" },
  1015. { "DAC", NULL, "RST DAC" },
  1016. { "DACL", NULL, "DAC" },
  1017. { "DACR", NULL, "DAC" },
  1018. /* AIF "ADC" Output Routes */
  1019. { "AIF1 AD0L", NULL, "AIF1 AD0L Stereo Mux" },
  1020. { "AIF1 AD0R", NULL, "AIF1 AD0R Stereo Mux" },
  1021. { "AIF2 ADCL", NULL, "AIF2 ADCL Stereo Mux" },
  1022. { "AIF2 ADCR", NULL, "AIF2 ADCR Stereo Mux" },
  1023. { "AIF3 ADC", NULL, "AIF3 ADC Source Capture Route" },
  1024. /* AIF "ADC" Mono/Stereo Mux Routes */
  1025. { "AIF1 AD0L Stereo Mux", "Stereo", "AIF1 AD0L Mixer" },
  1026. { "AIF1 AD0L Stereo Mux", "Reverse Stereo", "AIF1 AD0R Mixer" },
  1027. { "AIF1 AD0L Stereo Mux", "Sum Mono", "AIF1 AD0L Mixer" },
  1028. { "AIF1 AD0L Stereo Mux", "Sum Mono", "AIF1 AD0R Mixer" },
  1029. { "AIF1 AD0L Stereo Mux", "Mix Mono", "AIF1 AD0L Mixer" },
  1030. { "AIF1 AD0L Stereo Mux", "Mix Mono", "AIF1 AD0R Mixer" },
  1031. { "AIF1 AD0R Stereo Mux", "Stereo", "AIF1 AD0R Mixer" },
  1032. { "AIF1 AD0R Stereo Mux", "Reverse Stereo", "AIF1 AD0L Mixer" },
  1033. { "AIF1 AD0R Stereo Mux", "Sum Mono", "AIF1 AD0L Mixer" },
  1034. { "AIF1 AD0R Stereo Mux", "Sum Mono", "AIF1 AD0R Mixer" },
  1035. { "AIF1 AD0R Stereo Mux", "Mix Mono", "AIF1 AD0L Mixer" },
  1036. { "AIF1 AD0R Stereo Mux", "Mix Mono", "AIF1 AD0R Mixer" },
  1037. { "AIF2 ADCL Stereo Mux", "Stereo", "AIF2 ADCL Mixer" },
  1038. { "AIF2 ADCL Stereo Mux", "Reverse Stereo", "AIF2 ADCR Mixer" },
  1039. { "AIF2 ADCL Stereo Mux", "Sum Mono", "AIF2 ADCL Mixer" },
  1040. { "AIF2 ADCL Stereo Mux", "Sum Mono", "AIF2 ADCR Mixer" },
  1041. { "AIF2 ADCL Stereo Mux", "Mix Mono", "AIF2 ADCL Mixer" },
  1042. { "AIF2 ADCL Stereo Mux", "Mix Mono", "AIF2 ADCR Mixer" },
  1043. { "AIF2 ADCR Stereo Mux", "Stereo", "AIF2 ADCR Mixer" },
  1044. { "AIF2 ADCR Stereo Mux", "Reverse Stereo", "AIF2 ADCL Mixer" },
  1045. { "AIF2 ADCR Stereo Mux", "Sum Mono", "AIF2 ADCL Mixer" },
  1046. { "AIF2 ADCR Stereo Mux", "Sum Mono", "AIF2 ADCR Mixer" },
  1047. { "AIF2 ADCR Stereo Mux", "Mix Mono", "AIF2 ADCL Mixer" },
  1048. { "AIF2 ADCR Stereo Mux", "Mix Mono", "AIF2 ADCR Mixer" },
  1049. /* AIF "ADC" Output Mux Routes */
  1050. { "AIF3 ADC Source Capture Route", "AIF2 ADCL", "AIF2 ADCL Mixer" },
  1051. { "AIF3 ADC Source Capture Route", "AIF2 ADCR", "AIF2 ADCR Mixer" },
  1052. /* AIF "ADC" Mixer Routes */
  1053. { "AIF1 AD0L Mixer", "AIF1 Slot 0 Digital ADC Capture Switch", "AIF1 DA0L Stereo Mux" },
  1054. { "AIF1 AD0L Mixer", "AIF2 Digital ADC Capture Switch", "AIF2 DACL Source" },
  1055. { "AIF1 AD0L Mixer", "AIF1 Data Digital ADC Capture Switch", "ADCL" },
  1056. { "AIF1 AD0L Mixer", "AIF2 Inv Digital ADC Capture Switch", "AIF2 DACR Source" },
  1057. { "AIF1 AD0R Mixer", "AIF1 Slot 0 Digital ADC Capture Switch", "AIF1 DA0R Stereo Mux" },
  1058. { "AIF1 AD0R Mixer", "AIF2 Digital ADC Capture Switch", "AIF2 DACR Source" },
  1059. { "AIF1 AD0R Mixer", "AIF1 Data Digital ADC Capture Switch", "ADCR" },
  1060. { "AIF1 AD0R Mixer", "AIF2 Inv Digital ADC Capture Switch", "AIF2 DACL Source" },
  1061. { "AIF2 ADCL Mixer", "AIF2 ADC Mixer AIF1 DA0 Capture Switch", "AIF1 DA0L Stereo Mux" },
  1062. { "AIF2 ADCL Mixer", "AIF2 ADC Mixer AIF2 DAC Rev Capture Switch", "AIF2 DACR Source" },
  1063. { "AIF2 ADCL Mixer", "AIF2 ADC Mixer ADC Capture Switch", "ADCL" },
  1064. { "AIF2 ADCR Mixer", "AIF2 ADC Mixer AIF1 DA0 Capture Switch", "AIF1 DA0R Stereo Mux" },
  1065. { "AIF2 ADCR Mixer", "AIF2 ADC Mixer AIF2 DAC Rev Capture Switch", "AIF2 DACL Source" },
  1066. { "AIF2 ADCR Mixer", "AIF2 ADC Mixer ADC Capture Switch", "ADCR" },
  1067. /* AIF "DAC" Input Mux Routes */
  1068. { "AIF2 DACL Source", "AIF2", "AIF2 DACL Stereo Mux" },
  1069. { "AIF2 DACL Source", "AIF3+2", "AIF3 DAC" },
  1070. { "AIF2 DACL Source", "AIF2+3", "AIF2 DACL Stereo Mux" },
  1071. { "AIF2 DACR Source", "AIF2", "AIF2 DACR Stereo Mux" },
  1072. { "AIF2 DACR Source", "AIF3+2", "AIF2 DACR Stereo Mux" },
  1073. { "AIF2 DACR Source", "AIF2+3", "AIF3 DAC" },
  1074. /* AIF "DAC" Mono/Stereo Mux Routes */
  1075. { "AIF1 DA0L Stereo Mux", "Stereo", "AIF1 DA0L" },
  1076. { "AIF1 DA0L Stereo Mux", "Reverse Stereo", "AIF1 DA0R" },
  1077. { "AIF1 DA0L Stereo Mux", "Sum Mono", "AIF1 DA0L" },
  1078. { "AIF1 DA0L Stereo Mux", "Sum Mono", "AIF1 DA0R" },
  1079. { "AIF1 DA0L Stereo Mux", "Mix Mono", "AIF1 DA0L" },
  1080. { "AIF1 DA0L Stereo Mux", "Mix Mono", "AIF1 DA0R" },
  1081. { "AIF1 DA0R Stereo Mux", "Stereo", "AIF1 DA0R" },
  1082. { "AIF1 DA0R Stereo Mux", "Reverse Stereo", "AIF1 DA0L" },
  1083. { "AIF1 DA0R Stereo Mux", "Sum Mono", "AIF1 DA0L" },
  1084. { "AIF1 DA0R Stereo Mux", "Sum Mono", "AIF1 DA0R" },
  1085. { "AIF1 DA0R Stereo Mux", "Mix Mono", "AIF1 DA0L" },
  1086. { "AIF1 DA0R Stereo Mux", "Mix Mono", "AIF1 DA0R" },
  1087. { "AIF2 DACL Stereo Mux", "Stereo", "AIF2 DACL" },
  1088. { "AIF2 DACL Stereo Mux", "Reverse Stereo", "AIF2 DACR" },
  1089. { "AIF2 DACL Stereo Mux", "Sum Mono", "AIF2 DACL" },
  1090. { "AIF2 DACL Stereo Mux", "Sum Mono", "AIF2 DACR" },
  1091. { "AIF2 DACL Stereo Mux", "Mix Mono", "AIF2 DACL" },
  1092. { "AIF2 DACL Stereo Mux", "Mix Mono", "AIF2 DACR" },
  1093. { "AIF2 DACR Stereo Mux", "Stereo", "AIF2 DACR" },
  1094. { "AIF2 DACR Stereo Mux", "Reverse Stereo", "AIF2 DACL" },
  1095. { "AIF2 DACR Stereo Mux", "Sum Mono", "AIF2 DACL" },
  1096. { "AIF2 DACR Stereo Mux", "Sum Mono", "AIF2 DACR" },
  1097. { "AIF2 DACR Stereo Mux", "Mix Mono", "AIF2 DACL" },
  1098. { "AIF2 DACR Stereo Mux", "Mix Mono", "AIF2 DACR" },
  1099. /* DAC Output Routes */
  1100. { "DACL", NULL, "DACL Mixer" },
  1101. { "DACR", NULL, "DACR Mixer" },
  1102. /* DAC Mixer Routes */
  1103. { "DACL Mixer", "AIF1 Slot 0 Digital DAC Playback Switch", "AIF1 DA0L Stereo Mux" },
  1104. { "DACL Mixer", "AIF2 Digital DAC Playback Switch", "AIF2 DACL Source" },
  1105. { "DACL Mixer", "ADC Digital DAC Playback Switch", "ADCL" },
  1106. { "DACR Mixer", "AIF1 Slot 0 Digital DAC Playback Switch", "AIF1 DA0R Stereo Mux" },
  1107. { "DACR Mixer", "AIF2 Digital DAC Playback Switch", "AIF2 DACR Source" },
  1108. { "DACR Mixer", "ADC Digital DAC Playback Switch", "ADCR" },
  1109. };
  1110. static const struct snd_soc_dapm_widget sun8i_codec_legacy_widgets[] = {
  1111. /* Legacy ADC Inputs (connected to analog codec DAPM context) */
  1112. SND_SOC_DAPM_ADC("AIF1 Slot 0 Left ADC", NULL, SND_SOC_NOPM, 0, 0),
  1113. SND_SOC_DAPM_ADC("AIF1 Slot 0 Right ADC", NULL, SND_SOC_NOPM, 0, 0),
  1114. /* Legacy DAC Outputs (connected to analog codec DAPM context) */
  1115. SND_SOC_DAPM_DAC("AIF1 Slot 0 Left", NULL, SND_SOC_NOPM, 0, 0),
  1116. SND_SOC_DAPM_DAC("AIF1 Slot 0 Right", NULL, SND_SOC_NOPM, 0, 0),
  1117. };
  1118. static const struct snd_soc_dapm_route sun8i_codec_legacy_routes[] = {
  1119. /* Legacy ADC Routes */
  1120. { "ADCL", NULL, "AIF1 Slot 0 Left ADC" },
  1121. { "ADCR", NULL, "AIF1 Slot 0 Right ADC" },
  1122. /* Legacy DAC Routes */
  1123. { "AIF1 Slot 0 Left", NULL, "DACL" },
  1124. { "AIF1 Slot 0 Right", NULL, "DACR" },
  1125. };
  1126. static int sun8i_codec_component_probe(struct snd_soc_component *component)
  1127. {
  1128. struct snd_soc_dapm_context *dapm = snd_soc_component_to_dapm(component);
  1129. struct sun8i_codec *scodec = snd_soc_component_get_drvdata(component);
  1130. int ret;
  1131. scodec->component = component;
  1132. /* Add widgets for backward compatibility with old device trees. */
  1133. if (scodec->quirks->legacy_widgets) {
  1134. ret = snd_soc_dapm_new_controls(dapm, sun8i_codec_legacy_widgets,
  1135. ARRAY_SIZE(sun8i_codec_legacy_widgets));
  1136. if (ret)
  1137. return ret;
  1138. ret = snd_soc_dapm_add_routes(dapm, sun8i_codec_legacy_routes,
  1139. ARRAY_SIZE(sun8i_codec_legacy_routes));
  1140. if (ret)
  1141. return ret;
  1142. }
  1143. /*
  1144. * AIF1CLK and AIF2CLK share a pair of clock parents: PLL_AUDIO ("mod")
  1145. * and MCLK (from the CPU DAI connected to AIF1). MCLK's parent is also
  1146. * PLL_AUDIO, so using it adds no additional flexibility. Use PLL_AUDIO
  1147. * directly to simplify the clock tree.
  1148. */
  1149. regmap_update_bits(scodec->regmap, SUN8I_SYSCLK_CTL,
  1150. SUN8I_SYSCLK_CTL_AIF1CLK_SRC_MASK |
  1151. SUN8I_SYSCLK_CTL_AIF2CLK_SRC_MASK,
  1152. SUN8I_SYSCLK_CTL_AIF1CLK_SRC_PLL |
  1153. SUN8I_SYSCLK_CTL_AIF2CLK_SRC_PLL);
  1154. /* Use AIF1CLK as the SYSCLK parent since AIF1 is used most often. */
  1155. regmap_update_bits(scodec->regmap, SUN8I_SYSCLK_CTL,
  1156. BIT(SUN8I_SYSCLK_CTL_SYSCLK_SRC),
  1157. SUN8I_SYSCLK_CTL_SYSCLK_SRC_AIF1CLK);
  1158. /* Program the default sample rate. */
  1159. sun8i_codec_update_sample_rate(scodec);
  1160. return 0;
  1161. }
  1162. static void sun8i_codec_set_hmic_bias(struct sun8i_codec *scodec, bool enable)
  1163. {
  1164. struct snd_soc_dapm_context *dapm = snd_soc_card_to_dapm(scodec->component->card);
  1165. int irq_mask = BIT(SUN8I_HMIC_CTRL1_HMIC_DATA_IRQ_EN);
  1166. if (enable)
  1167. snd_soc_dapm_force_enable_pin(dapm, "HBIAS");
  1168. else
  1169. snd_soc_dapm_disable_pin(dapm, "HBIAS");
  1170. snd_soc_dapm_sync(dapm);
  1171. regmap_update_bits(scodec->regmap, SUN8I_HMIC_CTRL1,
  1172. irq_mask, enable ? irq_mask : 0);
  1173. }
  1174. static void sun8i_codec_jack_work(struct work_struct *work)
  1175. {
  1176. struct sun8i_codec *scodec = container_of(work, struct sun8i_codec,
  1177. jack_work.work);
  1178. unsigned int mdata;
  1179. int type;
  1180. guard(mutex)(&scodec->jack_mutex);
  1181. if (scodec->jack_status == SUN8I_JACK_STATUS_DISCONNECTED) {
  1182. if (scodec->last_hmic_irq != SUN8I_HMIC_STS_JACK_IN_IRQ_ST)
  1183. return;
  1184. scodec->jack_last_sample = -1;
  1185. if (scodec->jack_type & SND_JACK_MICROPHONE) {
  1186. /*
  1187. * If we were in disconnected state, we enable HBIAS and
  1188. * wait 600ms before reading initial HDATA value.
  1189. */
  1190. scodec->jack_hbias_ready = ktime_add_ms(ktime_get(), 600);
  1191. sun8i_codec_set_hmic_bias(scodec, true);
  1192. queue_delayed_work(system_power_efficient_wq,
  1193. &scodec->jack_work,
  1194. msecs_to_jiffies(610));
  1195. scodec->jack_status = SUN8I_JACK_STATUS_WAITING_HBIAS;
  1196. } else {
  1197. snd_soc_jack_report(scodec->jack, SND_JACK_HEADPHONE,
  1198. scodec->jack_type);
  1199. scodec->jack_status = SUN8I_JACK_STATUS_CONNECTED;
  1200. }
  1201. } else if (scodec->jack_status == SUN8I_JACK_STATUS_WAITING_HBIAS) {
  1202. /*
  1203. * If we're waiting for HBIAS to stabilize, and we get plug-out
  1204. * interrupt and nothing more for > 100ms, just cancel the
  1205. * initialization.
  1206. */
  1207. if (scodec->last_hmic_irq == SUN8I_HMIC_STS_JACK_OUT_IRQ_ST) {
  1208. scodec->jack_status = SUN8I_JACK_STATUS_DISCONNECTED;
  1209. sun8i_codec_set_hmic_bias(scodec, false);
  1210. return;
  1211. }
  1212. /*
  1213. * If we're not done waiting for HBIAS to stabilize, wait more.
  1214. */
  1215. if (!ktime_after(ktime_get(), scodec->jack_hbias_ready)) {
  1216. s64 msecs = ktime_ms_delta(scodec->jack_hbias_ready,
  1217. ktime_get());
  1218. queue_delayed_work(system_power_efficient_wq,
  1219. &scodec->jack_work,
  1220. msecs_to_jiffies(msecs + 10));
  1221. return;
  1222. }
  1223. /*
  1224. * Everything is stabilized, determine jack type and report it.
  1225. */
  1226. regmap_read(scodec->regmap, SUN8I_HMIC_STS, &mdata);
  1227. mdata &= SUN8I_HMIC_STS_HMIC_DATA_MASK;
  1228. mdata >>= SUN8I_HMIC_STS_HMIC_DATA;
  1229. regmap_write(scodec->regmap, SUN8I_HMIC_STS, 0);
  1230. type = mdata < 16 ? SND_JACK_HEADPHONE : SND_JACK_HEADSET;
  1231. if (type == SND_JACK_HEADPHONE)
  1232. sun8i_codec_set_hmic_bias(scodec, false);
  1233. snd_soc_jack_report(scodec->jack, type, scodec->jack_type);
  1234. scodec->jack_status = SUN8I_JACK_STATUS_CONNECTED;
  1235. } else if (scodec->jack_status == SUN8I_JACK_STATUS_CONNECTED) {
  1236. if (scodec->last_hmic_irq != SUN8I_HMIC_STS_JACK_OUT_IRQ_ST)
  1237. return;
  1238. scodec->jack_status = SUN8I_JACK_STATUS_DISCONNECTED;
  1239. if (scodec->jack_type & SND_JACK_MICROPHONE)
  1240. sun8i_codec_set_hmic_bias(scodec, false);
  1241. snd_soc_jack_report(scodec->jack, 0, scodec->jack_type);
  1242. }
  1243. }
  1244. static irqreturn_t sun8i_codec_jack_irq(int irq, void *dev_id)
  1245. {
  1246. struct sun8i_codec *scodec = dev_id;
  1247. int type = SND_JACK_HEADSET;
  1248. unsigned int status, value;
  1249. guard(mutex)(&scodec->jack_mutex);
  1250. regmap_read(scodec->regmap, SUN8I_HMIC_STS, &status);
  1251. regmap_write(scodec->regmap, SUN8I_HMIC_STS, status);
  1252. /*
  1253. * De-bounce in/out interrupts via a delayed work re-scheduling to
  1254. * 100ms after each interrupt..
  1255. */
  1256. if (status & BIT(SUN8I_HMIC_STS_JACK_OUT_IRQ_ST)) {
  1257. /*
  1258. * Out interrupt has priority over in interrupt so that if
  1259. * we get both, we assume the disconnected state, which is
  1260. * safer.
  1261. */
  1262. scodec->last_hmic_irq = SUN8I_HMIC_STS_JACK_OUT_IRQ_ST;
  1263. mod_delayed_work(system_power_efficient_wq, &scodec->jack_work,
  1264. msecs_to_jiffies(100));
  1265. } else if (status & BIT(SUN8I_HMIC_STS_JACK_IN_IRQ_ST)) {
  1266. scodec->last_hmic_irq = SUN8I_HMIC_STS_JACK_IN_IRQ_ST;
  1267. mod_delayed_work(system_power_efficient_wq, &scodec->jack_work,
  1268. msecs_to_jiffies(100));
  1269. } else if (status & BIT(SUN8I_HMIC_STS_HMIC_DATA_IRQ_ST)) {
  1270. /*
  1271. * Ignore data interrupts until jack status turns to connected
  1272. * state, which is after HMIC enable stabilization is completed.
  1273. * Until then tha data are bogus.
  1274. */
  1275. if (scodec->jack_status != SUN8I_JACK_STATUS_CONNECTED)
  1276. return IRQ_HANDLED;
  1277. value = (status & SUN8I_HMIC_STS_HMIC_DATA_MASK) >>
  1278. SUN8I_HMIC_STS_HMIC_DATA;
  1279. /*
  1280. * Assumes 60 mV per ADC LSB increment, 2V bias voltage, 2.2kOhm
  1281. * bias resistor.
  1282. */
  1283. if (value == 0)
  1284. type |= SND_JACK_BTN_0;
  1285. else if (value == 1)
  1286. type |= SND_JACK_BTN_3;
  1287. else if (value <= 3)
  1288. type |= SND_JACK_BTN_1;
  1289. else if (value <= 8)
  1290. type |= SND_JACK_BTN_2;
  1291. /*
  1292. * De-bounce. Only report button after two consecutive A/D
  1293. * samples are identical.
  1294. */
  1295. if (scodec->jack_last_sample >= 0 &&
  1296. scodec->jack_last_sample == value)
  1297. snd_soc_jack_report(scodec->jack, type,
  1298. scodec->jack_type);
  1299. scodec->jack_last_sample = value;
  1300. }
  1301. return IRQ_HANDLED;
  1302. }
  1303. static int sun8i_codec_enable_jack_detect(struct snd_soc_component *component,
  1304. struct snd_soc_jack *jack, void *data)
  1305. {
  1306. struct sun8i_codec *scodec = snd_soc_component_get_drvdata(component);
  1307. struct platform_device *pdev = to_platform_device(component->dev);
  1308. int ret;
  1309. if (!scodec->quirks->jack_detection)
  1310. return 0;
  1311. scodec->jack = jack;
  1312. scodec->jack_irq = platform_get_irq(pdev, 0);
  1313. if (scodec->jack_irq < 0)
  1314. return scodec->jack_irq;
  1315. /* Reserved value required for jack IRQs to trigger. */
  1316. regmap_write(scodec->regmap, SUN8I_HMIC_CTRL1,
  1317. 0xf << SUN8I_HMIC_CTRL1_HMIC_N |
  1318. 0x0 << SUN8I_HMIC_CTRL1_MDATA_THRESHOLD_DB |
  1319. 0x4 << SUN8I_HMIC_CTRL1_HMIC_M);
  1320. /* Sample the ADC at 128 Hz; bypass smooth filter. */
  1321. regmap_write(scodec->regmap, SUN8I_HMIC_CTRL2,
  1322. 0x0 << SUN8I_HMIC_CTRL2_HMIC_SAMPLE |
  1323. 0x17 << SUN8I_HMIC_CTRL2_HMIC_MDATA_THRESHOLD |
  1324. 0x0 << SUN8I_HMIC_CTRL2_HMIC_SF);
  1325. /* Do not discard any MDATA, enable user written MDATA threshold. */
  1326. regmap_write(scodec->regmap, SUN8I_HMIC_STS, 0);
  1327. regmap_set_bits(scodec->regmap, SUN8I_HMIC_CTRL1,
  1328. BIT(SUN8I_HMIC_CTRL1_JACK_OUT_IRQ_EN) |
  1329. BIT(SUN8I_HMIC_CTRL1_JACK_IN_IRQ_EN));
  1330. ret = devm_request_threaded_irq(&pdev->dev, scodec->jack_irq,
  1331. NULL, sun8i_codec_jack_irq,
  1332. IRQF_ONESHOT,
  1333. dev_name(&pdev->dev), scodec);
  1334. if (ret)
  1335. return ret;
  1336. return 0;
  1337. }
  1338. static void sun8i_codec_disable_jack_detect(struct snd_soc_component *component)
  1339. {
  1340. struct sun8i_codec *scodec = snd_soc_component_get_drvdata(component);
  1341. if (!scodec->quirks->jack_detection)
  1342. return;
  1343. devm_free_irq(component->dev, scodec->jack_irq, scodec);
  1344. cancel_delayed_work_sync(&scodec->jack_work);
  1345. regmap_clear_bits(scodec->regmap, SUN8I_HMIC_CTRL1,
  1346. BIT(SUN8I_HMIC_CTRL1_JACK_OUT_IRQ_EN) |
  1347. BIT(SUN8I_HMIC_CTRL1_JACK_IN_IRQ_EN) |
  1348. BIT(SUN8I_HMIC_CTRL1_HMIC_DATA_IRQ_EN));
  1349. scodec->jack = NULL;
  1350. }
  1351. static int sun8i_codec_component_set_jack(struct snd_soc_component *component,
  1352. struct snd_soc_jack *jack, void *data)
  1353. {
  1354. int ret = 0;
  1355. if (jack)
  1356. ret = sun8i_codec_enable_jack_detect(component, jack, data);
  1357. else
  1358. sun8i_codec_disable_jack_detect(component);
  1359. return ret;
  1360. }
  1361. static const struct snd_soc_component_driver sun8i_soc_component = {
  1362. .controls = sun8i_codec_controls,
  1363. .num_controls = ARRAY_SIZE(sun8i_codec_controls),
  1364. .dapm_widgets = sun8i_codec_dapm_widgets,
  1365. .num_dapm_widgets = ARRAY_SIZE(sun8i_codec_dapm_widgets),
  1366. .dapm_routes = sun8i_codec_dapm_routes,
  1367. .num_dapm_routes = ARRAY_SIZE(sun8i_codec_dapm_routes),
  1368. .set_jack = sun8i_codec_component_set_jack,
  1369. .probe = sun8i_codec_component_probe,
  1370. .idle_bias_on = 1,
  1371. .suspend_bias_off = 1,
  1372. .endianness = 1,
  1373. };
  1374. static bool sun8i_codec_volatile_reg(struct device *dev, unsigned int reg)
  1375. {
  1376. return reg == SUN8I_HMIC_STS;
  1377. }
  1378. static const struct regmap_config sun8i_codec_regmap_config = {
  1379. .reg_bits = 32,
  1380. .reg_stride = 4,
  1381. .val_bits = 32,
  1382. .volatile_reg = sun8i_codec_volatile_reg,
  1383. .max_register = SUN8I_DAC_MXR_SRC,
  1384. .cache_type = REGCACHE_FLAT,
  1385. };
  1386. static int sun8i_codec_probe(struct platform_device *pdev)
  1387. {
  1388. struct sun8i_codec *scodec;
  1389. void __iomem *base;
  1390. int ret;
  1391. scodec = devm_kzalloc(&pdev->dev, sizeof(*scodec), GFP_KERNEL);
  1392. if (!scodec)
  1393. return -ENOMEM;
  1394. scodec->quirks = of_device_get_match_data(&pdev->dev);
  1395. INIT_DELAYED_WORK(&scodec->jack_work, sun8i_codec_jack_work);
  1396. mutex_init(&scodec->jack_mutex);
  1397. platform_set_drvdata(pdev, scodec);
  1398. if (scodec->quirks->bus_clock) {
  1399. scodec->clk_bus = devm_clk_get(&pdev->dev, "bus");
  1400. if (IS_ERR(scodec->clk_bus)) {
  1401. dev_err(&pdev->dev, "Failed to get the bus clock\n");
  1402. return PTR_ERR(scodec->clk_bus);
  1403. }
  1404. }
  1405. scodec->clk_module = devm_clk_get(&pdev->dev, "mod");
  1406. if (IS_ERR(scodec->clk_module)) {
  1407. dev_err(&pdev->dev, "Failed to get the module clock\n");
  1408. return PTR_ERR(scodec->clk_module);
  1409. }
  1410. base = devm_platform_ioremap_resource(pdev, 0);
  1411. if (IS_ERR(base)) {
  1412. dev_err(&pdev->dev, "Failed to map the registers\n");
  1413. return PTR_ERR(base);
  1414. }
  1415. scodec->regmap = devm_regmap_init_mmio(&pdev->dev, base,
  1416. &sun8i_codec_regmap_config);
  1417. if (IS_ERR(scodec->regmap)) {
  1418. dev_err(&pdev->dev, "Failed to create our regmap\n");
  1419. return PTR_ERR(scodec->regmap);
  1420. }
  1421. regcache_cache_only(scodec->regmap, true);
  1422. pm_runtime_enable(&pdev->dev);
  1423. if (!pm_runtime_enabled(&pdev->dev)) {
  1424. ret = sun8i_codec_runtime_resume(&pdev->dev);
  1425. if (ret)
  1426. goto err_pm_disable;
  1427. }
  1428. ret = devm_snd_soc_register_component(&pdev->dev, &sun8i_soc_component,
  1429. sun8i_codec_dais,
  1430. ARRAY_SIZE(sun8i_codec_dais));
  1431. if (ret) {
  1432. dev_err(&pdev->dev, "Failed to register codec\n");
  1433. goto err_suspend;
  1434. }
  1435. return ret;
  1436. err_suspend:
  1437. if (!pm_runtime_status_suspended(&pdev->dev))
  1438. sun8i_codec_runtime_suspend(&pdev->dev);
  1439. err_pm_disable:
  1440. pm_runtime_disable(&pdev->dev);
  1441. return ret;
  1442. }
  1443. static void sun8i_codec_remove(struct platform_device *pdev)
  1444. {
  1445. pm_runtime_disable(&pdev->dev);
  1446. if (!pm_runtime_status_suspended(&pdev->dev))
  1447. sun8i_codec_runtime_suspend(&pdev->dev);
  1448. }
  1449. static const struct sun8i_codec_quirks sun8i_a33_quirks = {
  1450. .bus_clock = true,
  1451. .legacy_widgets = true,
  1452. .lrck_inversion = true,
  1453. };
  1454. static const struct sun8i_codec_quirks sun50i_a64_quirks = {
  1455. .bus_clock = true,
  1456. .jack_detection = true,
  1457. };
  1458. static const struct of_device_id sun8i_codec_of_match[] = {
  1459. { .compatible = "allwinner,sun8i-a33-codec", .data = &sun8i_a33_quirks },
  1460. { .compatible = "allwinner,sun50i-a64-codec", .data = &sun50i_a64_quirks },
  1461. {}
  1462. };
  1463. MODULE_DEVICE_TABLE(of, sun8i_codec_of_match);
  1464. static const struct dev_pm_ops sun8i_codec_pm_ops = {
  1465. RUNTIME_PM_OPS(sun8i_codec_runtime_suspend,
  1466. sun8i_codec_runtime_resume, NULL)
  1467. };
  1468. static struct platform_driver sun8i_codec_driver = {
  1469. .driver = {
  1470. .name = "sun8i-codec",
  1471. .of_match_table = sun8i_codec_of_match,
  1472. .pm = pm_ptr(&sun8i_codec_pm_ops),
  1473. },
  1474. .probe = sun8i_codec_probe,
  1475. .remove = sun8i_codec_remove,
  1476. };
  1477. module_platform_driver(sun8i_codec_driver);
  1478. MODULE_DESCRIPTION("Allwinner A33 (sun8i) codec driver");
  1479. MODULE_AUTHOR("Mylène Josserand <mylene.josserand@free-electrons.com>");
  1480. MODULE_LICENSE("GPL");
  1481. MODULE_ALIAS("platform:sun8i-codec");