sun4i-codec.c 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright 2014 Emilio López <emilio@elopez.com.ar>
  4. * Copyright 2014 Jon Smirl <jonsmirl@gmail.com>
  5. * Copyright 2015 Maxime Ripard <maxime.ripard@free-electrons.com>
  6. * Copyright 2015 Adam Sampson <ats@offog.org>
  7. * Copyright 2016 Chen-Yu Tsai <wens@csie.org>
  8. *
  9. * Based on the Allwinner SDK driver, released under the GPL.
  10. */
  11. #include <linux/init.h>
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/delay.h>
  16. #include <linux/slab.h>
  17. #include <linux/clk.h>
  18. #include <linux/regmap.h>
  19. #include <linux/reset.h>
  20. #include <linux/gpio/consumer.h>
  21. #include <sound/core.h>
  22. #include <sound/pcm.h>
  23. #include <sound/pcm_params.h>
  24. #include <sound/soc.h>
  25. #include <sound/tlv.h>
  26. #include <sound/initval.h>
  27. #include <sound/dmaengine_pcm.h>
  28. /* Codec DAC digital controls and FIFO registers */
  29. #define SUN4I_CODEC_DAC_DPC (0x00)
  30. #define SUN4I_CODEC_DAC_DPC_EN_DA (31)
  31. #define SUN4I_CODEC_DAC_DPC_DVOL (12)
  32. #define SUN4I_CODEC_DAC_FIFOC (0x04)
  33. #define SUN4I_CODEC_DAC_FIFOC_DAC_FS (29)
  34. #define SUN4I_CODEC_DAC_FIFOC_FIR_VERSION (28)
  35. #define SUN4I_CODEC_DAC_FIFOC_SEND_LASAT (26)
  36. #define SUN4I_CODEC_DAC_FIFOC_TX_FIFO_MODE (24)
  37. #define SUN4I_CODEC_DAC_FIFOC_DRQ_CLR_CNT (21)
  38. #define SUN4I_CODEC_DAC_FIFOC_TX_TRIG_LEVEL (8)
  39. #define SUN4I_CODEC_DAC_FIFOC_MONO_EN (6)
  40. #define SUN4I_CODEC_DAC_FIFOC_TX_SAMPLE_BITS (5)
  41. #define SUN4I_CODEC_DAC_FIFOC_DAC_DRQ_EN (4)
  42. #define SUN4I_CODEC_DAC_FIFOC_FIFO_FLUSH (0)
  43. #define SUN4I_CODEC_DAC_FIFOS (0x08)
  44. #define SUN4I_CODEC_DAC_TXDATA (0x0c)
  45. /* Codec DAC side analog signal controls */
  46. #define SUN4I_CODEC_DAC_ACTL (0x10)
  47. #define SUN4I_CODEC_DAC_ACTL_DACAENR (31)
  48. #define SUN4I_CODEC_DAC_ACTL_DACAENL (30)
  49. #define SUN4I_CODEC_DAC_ACTL_MIXEN (29)
  50. #define SUN4I_CODEC_DAC_ACTL_LNG (26)
  51. #define SUN4I_CODEC_DAC_ACTL_FMG (23)
  52. #define SUN4I_CODEC_DAC_ACTL_MICG (20)
  53. #define SUN4I_CODEC_DAC_ACTL_LLNS (19)
  54. #define SUN4I_CODEC_DAC_ACTL_RLNS (18)
  55. #define SUN4I_CODEC_DAC_ACTL_LFMS (17)
  56. #define SUN4I_CODEC_DAC_ACTL_RFMS (16)
  57. #define SUN4I_CODEC_DAC_ACTL_LDACLMIXS (15)
  58. #define SUN4I_CODEC_DAC_ACTL_RDACRMIXS (14)
  59. #define SUN4I_CODEC_DAC_ACTL_LDACRMIXS (13)
  60. #define SUN4I_CODEC_DAC_ACTL_MIC1LS (12)
  61. #define SUN4I_CODEC_DAC_ACTL_MIC1RS (11)
  62. #define SUN4I_CODEC_DAC_ACTL_MIC2LS (10)
  63. #define SUN4I_CODEC_DAC_ACTL_MIC2RS (9)
  64. #define SUN4I_CODEC_DAC_ACTL_DACPAS (8)
  65. #define SUN4I_CODEC_DAC_ACTL_MIXPAS (7)
  66. #define SUN4I_CODEC_DAC_ACTL_PA_MUTE (6)
  67. #define SUN4I_CODEC_DAC_ACTL_PA_VOL (0)
  68. #define SUN4I_CODEC_DAC_TUNE (0x14)
  69. #define SUN4I_CODEC_DAC_DEBUG (0x18)
  70. /* Codec ADC digital controls and FIFO registers */
  71. #define SUN4I_CODEC_ADC_FIFOC (0x1c)
  72. #define SUN4I_CODEC_ADC_FIFOC_ADC_FS (29)
  73. #define SUN4I_CODEC_ADC_FIFOC_EN_AD (28)
  74. #define SUN4I_CODEC_ADC_FIFOC_RX_FIFO_MODE (24)
  75. #define SUN4I_CODEC_ADC_FIFOC_RX_TRIG_LEVEL (8)
  76. #define SUN4I_CODEC_ADC_FIFOC_MONO_EN (7)
  77. #define SUN4I_CODEC_ADC_FIFOC_RX_SAMPLE_BITS (6)
  78. #define SUN4I_CODEC_ADC_FIFOC_ADC_DRQ_EN (4)
  79. #define SUN4I_CODEC_ADC_FIFOC_FIFO_FLUSH (0)
  80. #define SUN4I_CODEC_ADC_FIFOS (0x20)
  81. #define SUN4I_CODEC_ADC_RXDATA (0x24)
  82. /* Codec ADC side analog signal controls */
  83. #define SUN4I_CODEC_ADC_ACTL (0x28)
  84. #define SUN4I_CODEC_ADC_ACTL_ADC_R_EN (31)
  85. #define SUN4I_CODEC_ADC_ACTL_ADC_L_EN (30)
  86. #define SUN4I_CODEC_ADC_ACTL_PREG1EN (29)
  87. #define SUN4I_CODEC_ADC_ACTL_PREG2EN (28)
  88. #define SUN4I_CODEC_ADC_ACTL_VMICEN (27)
  89. #define SUN4I_CODEC_ADC_ACTL_PREG1 (25)
  90. #define SUN4I_CODEC_ADC_ACTL_PREG2 (23)
  91. #define SUN4I_CODEC_ADC_ACTL_VADCG (20)
  92. #define SUN4I_CODEC_ADC_ACTL_ADCIS (17)
  93. #define SUN4I_CODEC_ADC_ACTL_LNPREG (13)
  94. #define SUN4I_CODEC_ADC_ACTL_PA_EN (4)
  95. #define SUN4I_CODEC_ADC_ACTL_DDE (3)
  96. #define SUN4I_CODEC_ADC_DEBUG (0x2c)
  97. /* FIFO counters */
  98. #define SUN4I_CODEC_DAC_TXCNT (0x30)
  99. #define SUN4I_CODEC_ADC_RXCNT (0x34)
  100. /* Calibration register (sun7i only) */
  101. #define SUN7I_CODEC_AC_DAC_CAL (0x38)
  102. /* Microphone controls (sun7i only) */
  103. #define SUN7I_CODEC_AC_MIC_PHONE_CAL (0x3c)
  104. #define SUN7I_CODEC_AC_MIC_PHONE_CAL_PREG1 (29)
  105. #define SUN7I_CODEC_AC_MIC_PHONE_CAL_PREG2 (26)
  106. /*
  107. * sun6i specific registers
  108. *
  109. * sun6i shares the same digital control and FIFO registers as sun4i,
  110. * but only the DAC digital controls are at the same offset. The others
  111. * have been moved around to accommodate extra analog controls.
  112. */
  113. /* Codec DAC digital controls and FIFO registers */
  114. #define SUN6I_CODEC_ADC_FIFOC (0x10)
  115. #define SUN6I_CODEC_ADC_FIFOC_EN_AD (28)
  116. #define SUN6I_CODEC_ADC_FIFOS (0x14)
  117. #define SUN6I_CODEC_ADC_RXDATA (0x18)
  118. /* Output mixer and gain controls */
  119. #define SUN6I_CODEC_OM_DACA_CTRL (0x20)
  120. #define SUN6I_CODEC_OM_DACA_CTRL_DACAREN (31)
  121. #define SUN6I_CODEC_OM_DACA_CTRL_DACALEN (30)
  122. #define SUN6I_CODEC_OM_DACA_CTRL_RMIXEN (29)
  123. #define SUN6I_CODEC_OM_DACA_CTRL_LMIXEN (28)
  124. #define SUN6I_CODEC_OM_DACA_CTRL_RMIX_MIC1 (23)
  125. #define SUN6I_CODEC_OM_DACA_CTRL_RMIX_MIC2 (22)
  126. #define SUN6I_CODEC_OM_DACA_CTRL_RMIX_PHONE (21)
  127. #define SUN6I_CODEC_OM_DACA_CTRL_RMIX_PHONEP (20)
  128. #define SUN6I_CODEC_OM_DACA_CTRL_RMIX_LINEINR (19)
  129. #define SUN6I_CODEC_OM_DACA_CTRL_RMIX_DACR (18)
  130. #define SUN6I_CODEC_OM_DACA_CTRL_RMIX_DACL (17)
  131. #define SUN6I_CODEC_OM_DACA_CTRL_LMIX_MIC1 (16)
  132. #define SUN6I_CODEC_OM_DACA_CTRL_LMIX_MIC2 (15)
  133. #define SUN6I_CODEC_OM_DACA_CTRL_LMIX_PHONE (14)
  134. #define SUN6I_CODEC_OM_DACA_CTRL_LMIX_PHONEN (13)
  135. #define SUN6I_CODEC_OM_DACA_CTRL_LMIX_LINEINL (12)
  136. #define SUN6I_CODEC_OM_DACA_CTRL_LMIX_DACL (11)
  137. #define SUN6I_CODEC_OM_DACA_CTRL_LMIX_DACR (10)
  138. #define SUN6I_CODEC_OM_DACA_CTRL_RHPIS (9)
  139. #define SUN6I_CODEC_OM_DACA_CTRL_LHPIS (8)
  140. #define SUN6I_CODEC_OM_DACA_CTRL_RHPPAMUTE (7)
  141. #define SUN6I_CODEC_OM_DACA_CTRL_LHPPAMUTE (6)
  142. #define SUN6I_CODEC_OM_DACA_CTRL_HPVOL (0)
  143. #define SUN6I_CODEC_OM_PA_CTRL (0x24)
  144. #define SUN6I_CODEC_OM_PA_CTRL_HPPAEN (31)
  145. #define SUN6I_CODEC_OM_PA_CTRL_HPCOM_CTL (29)
  146. #define SUN6I_CODEC_OM_PA_CTRL_COMPTEN (28)
  147. #define SUN6I_CODEC_OM_PA_CTRL_MIC1G (15)
  148. #define SUN6I_CODEC_OM_PA_CTRL_MIC2G (12)
  149. #define SUN6I_CODEC_OM_PA_CTRL_LINEING (9)
  150. #define SUN6I_CODEC_OM_PA_CTRL_PHONEG (6)
  151. #define SUN6I_CODEC_OM_PA_CTRL_PHONEPG (3)
  152. #define SUN6I_CODEC_OM_PA_CTRL_PHONENG (0)
  153. /* Microphone, line out and phone out controls */
  154. #define SUN6I_CODEC_MIC_CTRL (0x28)
  155. #define SUN6I_CODEC_MIC_CTRL_HBIASEN (31)
  156. #define SUN6I_CODEC_MIC_CTRL_MBIASEN (30)
  157. #define SUN6I_CODEC_MIC_CTRL_MIC1AMPEN (28)
  158. #define SUN6I_CODEC_MIC_CTRL_MIC1BOOST (25)
  159. #define SUN6I_CODEC_MIC_CTRL_MIC2AMPEN (24)
  160. #define SUN6I_CODEC_MIC_CTRL_MIC2BOOST (21)
  161. #define SUN6I_CODEC_MIC_CTRL_MIC2SLT (20)
  162. #define SUN6I_CODEC_MIC_CTRL_LINEOUTLEN (19)
  163. #define SUN6I_CODEC_MIC_CTRL_LINEOUTREN (18)
  164. #define SUN6I_CODEC_MIC_CTRL_LINEOUTLSRC (17)
  165. #define SUN6I_CODEC_MIC_CTRL_LINEOUTRSRC (16)
  166. #define SUN6I_CODEC_MIC_CTRL_LINEOUTVC (11)
  167. #define SUN6I_CODEC_MIC_CTRL_PHONEPREG (8)
  168. /* ADC mixer controls */
  169. #define SUN6I_CODEC_ADC_ACTL (0x2c)
  170. #define SUN6I_CODEC_ADC_ACTL_ADCREN (31)
  171. #define SUN6I_CODEC_ADC_ACTL_ADCLEN (30)
  172. #define SUN6I_CODEC_ADC_ACTL_ADCRG (27)
  173. #define SUN6I_CODEC_ADC_ACTL_ADCLG (24)
  174. #define SUN6I_CODEC_ADC_ACTL_RADCMIX_MIC1 (13)
  175. #define SUN6I_CODEC_ADC_ACTL_RADCMIX_MIC2 (12)
  176. #define SUN6I_CODEC_ADC_ACTL_RADCMIX_PHONE (11)
  177. #define SUN6I_CODEC_ADC_ACTL_RADCMIX_PHONEP (10)
  178. #define SUN6I_CODEC_ADC_ACTL_RADCMIX_LINEINR (9)
  179. #define SUN6I_CODEC_ADC_ACTL_RADCMIX_OMIXR (8)
  180. #define SUN6I_CODEC_ADC_ACTL_RADCMIX_OMIXL (7)
  181. #define SUN6I_CODEC_ADC_ACTL_LADCMIX_MIC1 (6)
  182. #define SUN6I_CODEC_ADC_ACTL_LADCMIX_MIC2 (5)
  183. #define SUN6I_CODEC_ADC_ACTL_LADCMIX_PHONE (4)
  184. #define SUN6I_CODEC_ADC_ACTL_LADCMIX_PHONEN (3)
  185. #define SUN6I_CODEC_ADC_ACTL_LADCMIX_LINEINL (2)
  186. #define SUN6I_CODEC_ADC_ACTL_LADCMIX_OMIXL (1)
  187. #define SUN6I_CODEC_ADC_ACTL_LADCMIX_OMIXR (0)
  188. /* Analog performance tuning controls */
  189. #define SUN6I_CODEC_ADDA_TUNE (0x30)
  190. /* Calibration controls */
  191. #define SUN6I_CODEC_CALIBRATION (0x34)
  192. /* FIFO counters */
  193. #define SUN6I_CODEC_DAC_TXCNT (0x40)
  194. #define SUN6I_CODEC_ADC_RXCNT (0x44)
  195. /* headset jack detection and button support registers */
  196. #define SUN6I_CODEC_HMIC_CTL (0x50)
  197. #define SUN6I_CODEC_HMIC_DATA (0x54)
  198. /* TODO sun6i DAP (Digital Audio Processing) bits */
  199. /* FIFO counters moved on A23 */
  200. #define SUN8I_A23_CODEC_DAC_TXCNT (0x1c)
  201. #define SUN8I_A23_CODEC_ADC_RXCNT (0x20)
  202. /* TX FIFO moved on H3 */
  203. #define SUN8I_H3_CODEC_DAC_TXDATA (0x20)
  204. #define SUN8I_H3_CODEC_DAC_DBG (0x48)
  205. #define SUN8I_H3_CODEC_ADC_DBG (0x4c)
  206. /* TODO H3 DAP (Digital Audio Processing) bits */
  207. struct sun4i_codec {
  208. struct device *dev;
  209. struct regmap *regmap;
  210. struct clk *clk_apb;
  211. struct clk *clk_module;
  212. struct reset_control *rst;
  213. struct gpio_desc *gpio_pa;
  214. /* ADC_FIFOC register is at different offset on different SoCs */
  215. struct regmap_field *reg_adc_fifoc;
  216. struct snd_dmaengine_dai_dma_data capture_dma_data;
  217. struct snd_dmaengine_dai_dma_data playback_dma_data;
  218. };
  219. static void sun4i_codec_start_playback(struct sun4i_codec *scodec)
  220. {
  221. /* Flush TX FIFO */
  222. regmap_set_bits(scodec->regmap, SUN4I_CODEC_DAC_FIFOC,
  223. BIT(SUN4I_CODEC_DAC_FIFOC_FIFO_FLUSH));
  224. /* Enable DAC DRQ */
  225. regmap_set_bits(scodec->regmap, SUN4I_CODEC_DAC_FIFOC,
  226. BIT(SUN4I_CODEC_DAC_FIFOC_DAC_DRQ_EN));
  227. }
  228. static void sun4i_codec_stop_playback(struct sun4i_codec *scodec)
  229. {
  230. /* Disable DAC DRQ */
  231. regmap_clear_bits(scodec->regmap, SUN4I_CODEC_DAC_FIFOC,
  232. BIT(SUN4I_CODEC_DAC_FIFOC_DAC_DRQ_EN));
  233. }
  234. static void sun4i_codec_start_capture(struct sun4i_codec *scodec)
  235. {
  236. /* Enable ADC DRQ */
  237. regmap_field_set_bits(scodec->reg_adc_fifoc,
  238. BIT(SUN4I_CODEC_ADC_FIFOC_ADC_DRQ_EN));
  239. }
  240. static void sun4i_codec_stop_capture(struct sun4i_codec *scodec)
  241. {
  242. /* Disable ADC DRQ */
  243. regmap_field_clear_bits(scodec->reg_adc_fifoc,
  244. BIT(SUN4I_CODEC_ADC_FIFOC_ADC_DRQ_EN));
  245. }
  246. static int sun4i_codec_trigger(struct snd_pcm_substream *substream, int cmd,
  247. struct snd_soc_dai *dai)
  248. {
  249. struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
  250. struct sun4i_codec *scodec = snd_soc_card_get_drvdata(rtd->card);
  251. switch (cmd) {
  252. case SNDRV_PCM_TRIGGER_START:
  253. case SNDRV_PCM_TRIGGER_RESUME:
  254. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  255. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  256. sun4i_codec_start_playback(scodec);
  257. else
  258. sun4i_codec_start_capture(scodec);
  259. break;
  260. case SNDRV_PCM_TRIGGER_STOP:
  261. case SNDRV_PCM_TRIGGER_SUSPEND:
  262. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  263. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  264. sun4i_codec_stop_playback(scodec);
  265. else
  266. sun4i_codec_stop_capture(scodec);
  267. break;
  268. default:
  269. return -EINVAL;
  270. }
  271. return 0;
  272. }
  273. static int sun4i_codec_prepare_capture(struct snd_pcm_substream *substream,
  274. struct snd_soc_dai *dai)
  275. {
  276. struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
  277. struct sun4i_codec *scodec = snd_soc_card_get_drvdata(rtd->card);
  278. /* Flush RX FIFO */
  279. regmap_field_set_bits(scodec->reg_adc_fifoc,
  280. BIT(SUN4I_CODEC_ADC_FIFOC_FIFO_FLUSH));
  281. /* Set RX FIFO trigger level */
  282. regmap_field_update_bits(scodec->reg_adc_fifoc,
  283. 0xf << SUN4I_CODEC_ADC_FIFOC_RX_TRIG_LEVEL,
  284. 0x7 << SUN4I_CODEC_ADC_FIFOC_RX_TRIG_LEVEL);
  285. /*
  286. * FIXME: Undocumented in the datasheet, but
  287. * Allwinner's code mentions that it is
  288. * related to microphone gain
  289. */
  290. if (of_device_is_compatible(scodec->dev->of_node,
  291. "allwinner,sun4i-a10-codec") ||
  292. of_device_is_compatible(scodec->dev->of_node,
  293. "allwinner,sun7i-a20-codec")) {
  294. regmap_update_bits(scodec->regmap, SUN4I_CODEC_ADC_ACTL,
  295. 0x3 << 25,
  296. 0x1 << 25);
  297. }
  298. if (of_device_is_compatible(scodec->dev->of_node,
  299. "allwinner,sun7i-a20-codec"))
  300. /* FIXME: Undocumented bits */
  301. regmap_update_bits(scodec->regmap, SUN4I_CODEC_DAC_TUNE,
  302. 0x3 << 8,
  303. 0x1 << 8);
  304. return 0;
  305. }
  306. static int sun4i_codec_prepare_playback(struct snd_pcm_substream *substream,
  307. struct snd_soc_dai *dai)
  308. {
  309. struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
  310. struct sun4i_codec *scodec = snd_soc_card_get_drvdata(rtd->card);
  311. u32 val;
  312. /* Flush the TX FIFO */
  313. regmap_set_bits(scodec->regmap, SUN4I_CODEC_DAC_FIFOC,
  314. BIT(SUN4I_CODEC_DAC_FIFOC_FIFO_FLUSH));
  315. /* Set TX FIFO Empty Trigger Level */
  316. regmap_update_bits(scodec->regmap, SUN4I_CODEC_DAC_FIFOC,
  317. 0x3f << SUN4I_CODEC_DAC_FIFOC_TX_TRIG_LEVEL,
  318. 0xf << SUN4I_CODEC_DAC_FIFOC_TX_TRIG_LEVEL);
  319. if (substream->runtime->rate > 32000)
  320. /* Use 64 bits FIR filter */
  321. val = 0;
  322. else
  323. /* Use 32 bits FIR filter */
  324. val = BIT(SUN4I_CODEC_DAC_FIFOC_FIR_VERSION);
  325. regmap_update_bits(scodec->regmap, SUN4I_CODEC_DAC_FIFOC,
  326. BIT(SUN4I_CODEC_DAC_FIFOC_FIR_VERSION),
  327. val);
  328. /* Send zeros when we have an underrun */
  329. regmap_clear_bits(scodec->regmap, SUN4I_CODEC_DAC_FIFOC,
  330. BIT(SUN4I_CODEC_DAC_FIFOC_SEND_LASAT));
  331. return 0;
  332. };
  333. static int sun4i_codec_prepare(struct snd_pcm_substream *substream,
  334. struct snd_soc_dai *dai)
  335. {
  336. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  337. return sun4i_codec_prepare_playback(substream, dai);
  338. return sun4i_codec_prepare_capture(substream, dai);
  339. }
  340. static unsigned long sun4i_codec_get_mod_freq(struct snd_pcm_hw_params *params)
  341. {
  342. unsigned int rate = params_rate(params);
  343. switch (rate) {
  344. case 176400:
  345. case 88200:
  346. case 44100:
  347. case 33075:
  348. case 22050:
  349. case 14700:
  350. case 11025:
  351. case 7350:
  352. return 22579200;
  353. case 192000:
  354. case 96000:
  355. case 48000:
  356. case 32000:
  357. case 24000:
  358. case 16000:
  359. case 12000:
  360. case 8000:
  361. return 24576000;
  362. default:
  363. return 0;
  364. }
  365. }
  366. static int sun4i_codec_get_hw_rate(struct snd_pcm_hw_params *params)
  367. {
  368. unsigned int rate = params_rate(params);
  369. switch (rate) {
  370. case 192000:
  371. case 176400:
  372. return 6;
  373. case 96000:
  374. case 88200:
  375. return 7;
  376. case 48000:
  377. case 44100:
  378. return 0;
  379. case 32000:
  380. case 33075:
  381. return 1;
  382. case 24000:
  383. case 22050:
  384. return 2;
  385. case 16000:
  386. case 14700:
  387. return 3;
  388. case 12000:
  389. case 11025:
  390. return 4;
  391. case 8000:
  392. case 7350:
  393. return 5;
  394. default:
  395. return -EINVAL;
  396. }
  397. }
  398. static int sun4i_codec_hw_params_capture(struct sun4i_codec *scodec,
  399. struct snd_pcm_hw_params *params,
  400. unsigned int hwrate)
  401. {
  402. /* Set ADC sample rate */
  403. regmap_field_update_bits(scodec->reg_adc_fifoc,
  404. 7 << SUN4I_CODEC_ADC_FIFOC_ADC_FS,
  405. hwrate << SUN4I_CODEC_ADC_FIFOC_ADC_FS);
  406. /* Set the number of channels we want to use */
  407. if (params_channels(params) == 1)
  408. regmap_field_set_bits(scodec->reg_adc_fifoc,
  409. BIT(SUN4I_CODEC_ADC_FIFOC_MONO_EN));
  410. else
  411. regmap_field_clear_bits(scodec->reg_adc_fifoc,
  412. BIT(SUN4I_CODEC_ADC_FIFOC_MONO_EN));
  413. /* Set the number of sample bits to either 16 or 24 bits */
  414. if (hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS)->min == 32) {
  415. regmap_field_set_bits(scodec->reg_adc_fifoc,
  416. BIT(SUN4I_CODEC_ADC_FIFOC_RX_SAMPLE_BITS));
  417. regmap_field_clear_bits(scodec->reg_adc_fifoc,
  418. BIT(SUN4I_CODEC_ADC_FIFOC_RX_FIFO_MODE));
  419. scodec->capture_dma_data.addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  420. } else {
  421. regmap_field_clear_bits(scodec->reg_adc_fifoc,
  422. BIT(SUN4I_CODEC_ADC_FIFOC_RX_SAMPLE_BITS));
  423. /* Fill most significant bits with valid data MSB */
  424. regmap_field_set_bits(scodec->reg_adc_fifoc,
  425. BIT(SUN4I_CODEC_ADC_FIFOC_RX_FIFO_MODE));
  426. scodec->capture_dma_data.addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
  427. }
  428. return 0;
  429. }
  430. static int sun4i_codec_hw_params_playback(struct sun4i_codec *scodec,
  431. struct snd_pcm_hw_params *params,
  432. unsigned int hwrate)
  433. {
  434. u32 val;
  435. /* Set DAC sample rate */
  436. regmap_update_bits(scodec->regmap, SUN4I_CODEC_DAC_FIFOC,
  437. 7 << SUN4I_CODEC_DAC_FIFOC_DAC_FS,
  438. hwrate << SUN4I_CODEC_DAC_FIFOC_DAC_FS);
  439. /* Set the number of channels we want to use */
  440. if (params_channels(params) == 1)
  441. val = BIT(SUN4I_CODEC_DAC_FIFOC_MONO_EN);
  442. else
  443. val = 0;
  444. regmap_update_bits(scodec->regmap, SUN4I_CODEC_DAC_FIFOC,
  445. BIT(SUN4I_CODEC_DAC_FIFOC_MONO_EN),
  446. val);
  447. /* Set the number of sample bits to either 16 or 24 bits */
  448. if (hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS)->min == 32) {
  449. regmap_set_bits(scodec->regmap, SUN4I_CODEC_DAC_FIFOC,
  450. BIT(SUN4I_CODEC_DAC_FIFOC_TX_SAMPLE_BITS));
  451. /* Set TX FIFO mode to padding the LSBs with 0 */
  452. regmap_clear_bits(scodec->regmap, SUN4I_CODEC_DAC_FIFOC,
  453. BIT(SUN4I_CODEC_DAC_FIFOC_TX_FIFO_MODE));
  454. scodec->playback_dma_data.addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  455. } else {
  456. regmap_clear_bits(scodec->regmap, SUN4I_CODEC_DAC_FIFOC,
  457. BIT(SUN4I_CODEC_DAC_FIFOC_TX_SAMPLE_BITS));
  458. /* Set TX FIFO mode to repeat the MSB */
  459. regmap_set_bits(scodec->regmap, SUN4I_CODEC_DAC_FIFOC,
  460. BIT(SUN4I_CODEC_DAC_FIFOC_TX_FIFO_MODE));
  461. scodec->playback_dma_data.addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
  462. }
  463. return 0;
  464. }
  465. static int sun4i_codec_hw_params(struct snd_pcm_substream *substream,
  466. struct snd_pcm_hw_params *params,
  467. struct snd_soc_dai *dai)
  468. {
  469. struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
  470. struct sun4i_codec *scodec = snd_soc_card_get_drvdata(rtd->card);
  471. unsigned long clk_freq;
  472. int ret, hwrate;
  473. clk_freq = sun4i_codec_get_mod_freq(params);
  474. if (!clk_freq)
  475. return -EINVAL;
  476. ret = clk_set_rate(scodec->clk_module, clk_freq);
  477. if (ret)
  478. return ret;
  479. hwrate = sun4i_codec_get_hw_rate(params);
  480. if (hwrate < 0)
  481. return hwrate;
  482. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  483. return sun4i_codec_hw_params_playback(scodec, params,
  484. hwrate);
  485. return sun4i_codec_hw_params_capture(scodec, params,
  486. hwrate);
  487. }
  488. static int sun4i_codec_startup(struct snd_pcm_substream *substream,
  489. struct snd_soc_dai *dai)
  490. {
  491. struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
  492. struct sun4i_codec *scodec = snd_soc_card_get_drvdata(rtd->card);
  493. /*
  494. * Stop issuing DRQ when we have room for less than 16 samples
  495. * in our TX FIFO
  496. */
  497. regmap_set_bits(scodec->regmap, SUN4I_CODEC_DAC_FIFOC,
  498. 3 << SUN4I_CODEC_DAC_FIFOC_DRQ_CLR_CNT);
  499. return clk_prepare_enable(scodec->clk_module);
  500. }
  501. static void sun4i_codec_shutdown(struct snd_pcm_substream *substream,
  502. struct snd_soc_dai *dai)
  503. {
  504. struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
  505. struct sun4i_codec *scodec = snd_soc_card_get_drvdata(rtd->card);
  506. clk_disable_unprepare(scodec->clk_module);
  507. }
  508. static const struct snd_soc_dai_ops sun4i_codec_dai_ops = {
  509. .startup = sun4i_codec_startup,
  510. .shutdown = sun4i_codec_shutdown,
  511. .trigger = sun4i_codec_trigger,
  512. .hw_params = sun4i_codec_hw_params,
  513. .prepare = sun4i_codec_prepare,
  514. };
  515. #define SUN4I_CODEC_RATES ( \
  516. SNDRV_PCM_RATE_8000_48000 | \
  517. SNDRV_PCM_RATE_12000 | \
  518. SNDRV_PCM_RATE_24000 | \
  519. SNDRV_PCM_RATE_96000 | \
  520. SNDRV_PCM_RATE_192000)
  521. static struct snd_soc_dai_driver sun4i_codec_dai = {
  522. .name = "Codec",
  523. .ops = &sun4i_codec_dai_ops,
  524. .playback = {
  525. .stream_name = "Codec Playback",
  526. .channels_min = 1,
  527. .channels_max = 2,
  528. .rate_min = 8000,
  529. .rate_max = 192000,
  530. .rates = SUN4I_CODEC_RATES,
  531. .formats = SNDRV_PCM_FMTBIT_S16_LE |
  532. SNDRV_PCM_FMTBIT_S32_LE,
  533. .sig_bits = 24,
  534. },
  535. .capture = {
  536. .stream_name = "Codec Capture",
  537. .channels_min = 1,
  538. .channels_max = 2,
  539. .rate_min = 8000,
  540. .rate_max = 48000,
  541. .rates = SUN4I_CODEC_RATES,
  542. .formats = SNDRV_PCM_FMTBIT_S16_LE |
  543. SNDRV_PCM_FMTBIT_S32_LE,
  544. .sig_bits = 24,
  545. },
  546. };
  547. /*** sun4i Codec ***/
  548. static const struct snd_kcontrol_new sun4i_codec_pa_mute =
  549. SOC_DAPM_SINGLE("Switch", SUN4I_CODEC_DAC_ACTL,
  550. SUN4I_CODEC_DAC_ACTL_PA_MUTE, 1, 0);
  551. static DECLARE_TLV_DB_SCALE(sun4i_codec_pa_volume_scale, -6300, 100, 1);
  552. static DECLARE_TLV_DB_SCALE(sun4i_codec_linein_loopback_gain_scale, -150, 150,
  553. 0);
  554. static DECLARE_TLV_DB_SCALE(sun4i_codec_linein_preamp_gain_scale, -1200, 300,
  555. 0);
  556. static DECLARE_TLV_DB_SCALE(sun4i_codec_fmin_loopback_gain_scale, -450, 150,
  557. 0);
  558. static DECLARE_TLV_DB_SCALE(sun4i_codec_micin_loopback_gain_scale, -450, 150,
  559. 0);
  560. static DECLARE_TLV_DB_RANGE(sun4i_codec_micin_preamp_gain_scale,
  561. 0, 0, TLV_DB_SCALE_ITEM(0, 0, 0),
  562. 1, 7, TLV_DB_SCALE_ITEM(3500, 300, 0));
  563. static DECLARE_TLV_DB_RANGE(sun7i_codec_micin_preamp_gain_scale,
  564. 0, 0, TLV_DB_SCALE_ITEM(0, 0, 0),
  565. 1, 7, TLV_DB_SCALE_ITEM(2400, 300, 0));
  566. static const struct snd_kcontrol_new sun4i_codec_controls[] = {
  567. SOC_SINGLE_TLV("Power Amplifier Volume", SUN4I_CODEC_DAC_ACTL,
  568. SUN4I_CODEC_DAC_ACTL_PA_VOL, 0x3F, 0,
  569. sun4i_codec_pa_volume_scale),
  570. SOC_SINGLE_TLV("Line Playback Volume", SUN4I_CODEC_DAC_ACTL,
  571. SUN4I_CODEC_DAC_ACTL_LNG, 1, 0,
  572. sun4i_codec_linein_loopback_gain_scale),
  573. SOC_SINGLE_TLV("Line Boost Volume", SUN4I_CODEC_ADC_ACTL,
  574. SUN4I_CODEC_ADC_ACTL_LNPREG, 7, 0,
  575. sun4i_codec_linein_preamp_gain_scale),
  576. SOC_SINGLE_TLV("FM Playback Volume", SUN4I_CODEC_DAC_ACTL,
  577. SUN4I_CODEC_DAC_ACTL_FMG, 3, 0,
  578. sun4i_codec_fmin_loopback_gain_scale),
  579. SOC_SINGLE_TLV("Mic Playback Volume", SUN4I_CODEC_DAC_ACTL,
  580. SUN4I_CODEC_DAC_ACTL_MICG, 7, 0,
  581. sun4i_codec_micin_loopback_gain_scale),
  582. SOC_SINGLE_TLV("Mic1 Boost Volume", SUN4I_CODEC_ADC_ACTL,
  583. SUN4I_CODEC_ADC_ACTL_PREG1, 3, 0,
  584. sun4i_codec_micin_preamp_gain_scale),
  585. SOC_SINGLE_TLV("Mic2 Boost Volume", SUN4I_CODEC_ADC_ACTL,
  586. SUN4I_CODEC_ADC_ACTL_PREG2, 3, 0,
  587. sun4i_codec_micin_preamp_gain_scale),
  588. };
  589. static const struct snd_kcontrol_new sun7i_codec_controls[] = {
  590. SOC_SINGLE_TLV("Power Amplifier Volume", SUN4I_CODEC_DAC_ACTL,
  591. SUN4I_CODEC_DAC_ACTL_PA_VOL, 0x3F, 0,
  592. sun4i_codec_pa_volume_scale),
  593. SOC_SINGLE_TLV("Line Playback Volume", SUN4I_CODEC_DAC_ACTL,
  594. SUN4I_CODEC_DAC_ACTL_LNG, 1, 0,
  595. sun4i_codec_linein_loopback_gain_scale),
  596. SOC_SINGLE_TLV("Line Boost Volume", SUN4I_CODEC_ADC_ACTL,
  597. SUN4I_CODEC_ADC_ACTL_LNPREG, 7, 0,
  598. sun4i_codec_linein_preamp_gain_scale),
  599. SOC_SINGLE_TLV("FM Playback Volume", SUN4I_CODEC_DAC_ACTL,
  600. SUN4I_CODEC_DAC_ACTL_FMG, 3, 0,
  601. sun4i_codec_fmin_loopback_gain_scale),
  602. SOC_SINGLE_TLV("Mic Playback Volume", SUN4I_CODEC_DAC_ACTL,
  603. SUN4I_CODEC_DAC_ACTL_MICG, 7, 0,
  604. sun4i_codec_micin_loopback_gain_scale),
  605. SOC_SINGLE_TLV("Mic1 Boost Volume", SUN7I_CODEC_AC_MIC_PHONE_CAL,
  606. SUN7I_CODEC_AC_MIC_PHONE_CAL_PREG1, 7, 0,
  607. sun7i_codec_micin_preamp_gain_scale),
  608. SOC_SINGLE_TLV("Mic2 Boost Volume", SUN7I_CODEC_AC_MIC_PHONE_CAL,
  609. SUN7I_CODEC_AC_MIC_PHONE_CAL_PREG2, 7, 0,
  610. sun7i_codec_micin_preamp_gain_scale),
  611. };
  612. static const struct snd_kcontrol_new sun4i_codec_mixer_controls[] = {
  613. SOC_DAPM_SINGLE("Left Mixer Left DAC Playback Switch",
  614. SUN4I_CODEC_DAC_ACTL, SUN4I_CODEC_DAC_ACTL_LDACLMIXS,
  615. 1, 0),
  616. SOC_DAPM_SINGLE("Right Mixer Right DAC Playback Switch",
  617. SUN4I_CODEC_DAC_ACTL, SUN4I_CODEC_DAC_ACTL_RDACRMIXS,
  618. 1, 0),
  619. SOC_DAPM_SINGLE("Right Mixer Left DAC Playback Switch",
  620. SUN4I_CODEC_DAC_ACTL,
  621. SUN4I_CODEC_DAC_ACTL_LDACRMIXS, 1, 0),
  622. SOC_DAPM_DOUBLE("Line Playback Switch", SUN4I_CODEC_DAC_ACTL,
  623. SUN4I_CODEC_DAC_ACTL_LLNS,
  624. SUN4I_CODEC_DAC_ACTL_RLNS, 1, 0),
  625. SOC_DAPM_DOUBLE("FM Playback Switch", SUN4I_CODEC_DAC_ACTL,
  626. SUN4I_CODEC_DAC_ACTL_LFMS,
  627. SUN4I_CODEC_DAC_ACTL_RFMS, 1, 0),
  628. SOC_DAPM_DOUBLE("Mic1 Playback Switch", SUN4I_CODEC_DAC_ACTL,
  629. SUN4I_CODEC_DAC_ACTL_MIC1LS,
  630. SUN4I_CODEC_DAC_ACTL_MIC1RS, 1, 0),
  631. SOC_DAPM_DOUBLE("Mic2 Playback Switch", SUN4I_CODEC_DAC_ACTL,
  632. SUN4I_CODEC_DAC_ACTL_MIC2LS,
  633. SUN4I_CODEC_DAC_ACTL_MIC2RS, 1, 0),
  634. };
  635. static const struct snd_kcontrol_new sun4i_codec_pa_mixer_controls[] = {
  636. SOC_DAPM_SINGLE("DAC Playback Switch", SUN4I_CODEC_DAC_ACTL,
  637. SUN4I_CODEC_DAC_ACTL_DACPAS, 1, 0),
  638. SOC_DAPM_SINGLE("Mixer Playback Switch", SUN4I_CODEC_DAC_ACTL,
  639. SUN4I_CODEC_DAC_ACTL_MIXPAS, 1, 0),
  640. };
  641. static const struct snd_soc_dapm_widget sun4i_codec_codec_dapm_widgets[] = {
  642. /* Digital parts of the ADCs */
  643. SND_SOC_DAPM_SUPPLY("ADC", SUN4I_CODEC_ADC_FIFOC,
  644. SUN4I_CODEC_ADC_FIFOC_EN_AD, 0,
  645. NULL, 0),
  646. /* Digital parts of the DACs */
  647. SND_SOC_DAPM_SUPPLY("DAC", SUN4I_CODEC_DAC_DPC,
  648. SUN4I_CODEC_DAC_DPC_EN_DA, 0,
  649. NULL, 0),
  650. /* Analog parts of the ADCs */
  651. SND_SOC_DAPM_ADC("Left ADC", "Codec Capture", SUN4I_CODEC_ADC_ACTL,
  652. SUN4I_CODEC_ADC_ACTL_ADC_L_EN, 0),
  653. SND_SOC_DAPM_ADC("Right ADC", "Codec Capture", SUN4I_CODEC_ADC_ACTL,
  654. SUN4I_CODEC_ADC_ACTL_ADC_R_EN, 0),
  655. /* Analog parts of the DACs */
  656. SND_SOC_DAPM_DAC("Left DAC", "Codec Playback", SUN4I_CODEC_DAC_ACTL,
  657. SUN4I_CODEC_DAC_ACTL_DACAENL, 0),
  658. SND_SOC_DAPM_DAC("Right DAC", "Codec Playback", SUN4I_CODEC_DAC_ACTL,
  659. SUN4I_CODEC_DAC_ACTL_DACAENR, 0),
  660. /* Mixers */
  661. SND_SOC_DAPM_MIXER("Left Mixer", SND_SOC_NOPM, 0, 0,
  662. sun4i_codec_mixer_controls,
  663. ARRAY_SIZE(sun4i_codec_mixer_controls)),
  664. SND_SOC_DAPM_MIXER("Right Mixer", SND_SOC_NOPM, 0, 0,
  665. sun4i_codec_mixer_controls,
  666. ARRAY_SIZE(sun4i_codec_mixer_controls)),
  667. /* Global Mixer Enable */
  668. SND_SOC_DAPM_SUPPLY("Mixer Enable", SUN4I_CODEC_DAC_ACTL,
  669. SUN4I_CODEC_DAC_ACTL_MIXEN, 0, NULL, 0),
  670. /* VMIC */
  671. SND_SOC_DAPM_SUPPLY("VMIC", SUN4I_CODEC_ADC_ACTL,
  672. SUN4I_CODEC_ADC_ACTL_VMICEN, 0, NULL, 0),
  673. /* Mic Pre-Amplifiers */
  674. SND_SOC_DAPM_PGA("MIC1 Pre-Amplifier", SUN4I_CODEC_ADC_ACTL,
  675. SUN4I_CODEC_ADC_ACTL_PREG1EN, 0, NULL, 0),
  676. SND_SOC_DAPM_PGA("MIC2 Pre-Amplifier", SUN4I_CODEC_ADC_ACTL,
  677. SUN4I_CODEC_ADC_ACTL_PREG2EN, 0, NULL, 0),
  678. /* Power Amplifier */
  679. SND_SOC_DAPM_MIXER("Power Amplifier", SUN4I_CODEC_ADC_ACTL,
  680. SUN4I_CODEC_ADC_ACTL_PA_EN, 0,
  681. sun4i_codec_pa_mixer_controls,
  682. ARRAY_SIZE(sun4i_codec_pa_mixer_controls)),
  683. SND_SOC_DAPM_SWITCH("Power Amplifier Mute", SND_SOC_NOPM, 0, 0,
  684. &sun4i_codec_pa_mute),
  685. SND_SOC_DAPM_INPUT("Line Right"),
  686. SND_SOC_DAPM_INPUT("Line Left"),
  687. SND_SOC_DAPM_INPUT("FM Right"),
  688. SND_SOC_DAPM_INPUT("FM Left"),
  689. SND_SOC_DAPM_INPUT("Mic1"),
  690. SND_SOC_DAPM_INPUT("Mic2"),
  691. SND_SOC_DAPM_OUTPUT("HP Right"),
  692. SND_SOC_DAPM_OUTPUT("HP Left"),
  693. };
  694. static const struct snd_soc_dapm_route sun4i_codec_codec_dapm_routes[] = {
  695. /* Left ADC / DAC Routes */
  696. { "Left ADC", NULL, "ADC" },
  697. { "Left DAC", NULL, "DAC" },
  698. /* Right ADC / DAC Routes */
  699. { "Right ADC", NULL, "ADC" },
  700. { "Right DAC", NULL, "DAC" },
  701. /* Right Mixer Routes */
  702. { "Right Mixer", NULL, "Mixer Enable" },
  703. { "Right Mixer", "Right Mixer Left DAC Playback Switch", "Left DAC" },
  704. { "Right Mixer", "Right Mixer Right DAC Playback Switch", "Right DAC" },
  705. { "Right Mixer", "Line Playback Switch", "Line Right" },
  706. { "Right Mixer", "FM Playback Switch", "FM Right" },
  707. { "Right Mixer", "Mic1 Playback Switch", "MIC1 Pre-Amplifier" },
  708. { "Right Mixer", "Mic2 Playback Switch", "MIC2 Pre-Amplifier" },
  709. /* Left Mixer Routes */
  710. { "Left Mixer", NULL, "Mixer Enable" },
  711. { "Left Mixer", "Left Mixer Left DAC Playback Switch", "Left DAC" },
  712. { "Left Mixer", "Line Playback Switch", "Line Left" },
  713. { "Left Mixer", "FM Playback Switch", "FM Left" },
  714. { "Left Mixer", "Mic1 Playback Switch", "MIC1 Pre-Amplifier" },
  715. { "Left Mixer", "Mic2 Playback Switch", "MIC2 Pre-Amplifier" },
  716. /* Power Amplifier Routes */
  717. { "Power Amplifier", "Mixer Playback Switch", "Left Mixer" },
  718. { "Power Amplifier", "Mixer Playback Switch", "Right Mixer" },
  719. { "Power Amplifier", "DAC Playback Switch", "Left DAC" },
  720. { "Power Amplifier", "DAC Playback Switch", "Right DAC" },
  721. /* Headphone Output Routes */
  722. { "Power Amplifier Mute", "Switch", "Power Amplifier" },
  723. { "HP Right", NULL, "Power Amplifier Mute" },
  724. { "HP Left", NULL, "Power Amplifier Mute" },
  725. /* Mic1 Routes */
  726. { "Left ADC", NULL, "MIC1 Pre-Amplifier" },
  727. { "Right ADC", NULL, "MIC1 Pre-Amplifier" },
  728. { "MIC1 Pre-Amplifier", NULL, "Mic1"},
  729. { "Mic1", NULL, "VMIC" },
  730. /* Mic2 Routes */
  731. { "Left ADC", NULL, "MIC2 Pre-Amplifier" },
  732. { "Right ADC", NULL, "MIC2 Pre-Amplifier" },
  733. { "MIC2 Pre-Amplifier", NULL, "Mic2"},
  734. { "Mic2", NULL, "VMIC" },
  735. };
  736. static const struct snd_soc_component_driver sun4i_codec_codec = {
  737. .controls = sun4i_codec_controls,
  738. .num_controls = ARRAY_SIZE(sun4i_codec_controls),
  739. .dapm_widgets = sun4i_codec_codec_dapm_widgets,
  740. .num_dapm_widgets = ARRAY_SIZE(sun4i_codec_codec_dapm_widgets),
  741. .dapm_routes = sun4i_codec_codec_dapm_routes,
  742. .num_dapm_routes = ARRAY_SIZE(sun4i_codec_codec_dapm_routes),
  743. .idle_bias_on = 1,
  744. .use_pmdown_time = 1,
  745. .endianness = 1,
  746. };
  747. static const struct snd_soc_component_driver sun7i_codec_codec = {
  748. .controls = sun7i_codec_controls,
  749. .num_controls = ARRAY_SIZE(sun7i_codec_controls),
  750. .dapm_widgets = sun4i_codec_codec_dapm_widgets,
  751. .num_dapm_widgets = ARRAY_SIZE(sun4i_codec_codec_dapm_widgets),
  752. .dapm_routes = sun4i_codec_codec_dapm_routes,
  753. .num_dapm_routes = ARRAY_SIZE(sun4i_codec_codec_dapm_routes),
  754. .idle_bias_on = 1,
  755. .use_pmdown_time = 1,
  756. .endianness = 1,
  757. };
  758. /*** sun6i Codec ***/
  759. /* mixer controls */
  760. static const struct snd_kcontrol_new sun6i_codec_mixer_controls[] = {
  761. SOC_DAPM_DOUBLE("DAC Playback Switch",
  762. SUN6I_CODEC_OM_DACA_CTRL,
  763. SUN6I_CODEC_OM_DACA_CTRL_LMIX_DACL,
  764. SUN6I_CODEC_OM_DACA_CTRL_RMIX_DACR, 1, 0),
  765. SOC_DAPM_DOUBLE("DAC Reversed Playback Switch",
  766. SUN6I_CODEC_OM_DACA_CTRL,
  767. SUN6I_CODEC_OM_DACA_CTRL_LMIX_DACR,
  768. SUN6I_CODEC_OM_DACA_CTRL_RMIX_DACL, 1, 0),
  769. SOC_DAPM_DOUBLE("Line In Playback Switch",
  770. SUN6I_CODEC_OM_DACA_CTRL,
  771. SUN6I_CODEC_OM_DACA_CTRL_LMIX_LINEINL,
  772. SUN6I_CODEC_OM_DACA_CTRL_RMIX_LINEINR, 1, 0),
  773. SOC_DAPM_DOUBLE("Mic1 Playback Switch",
  774. SUN6I_CODEC_OM_DACA_CTRL,
  775. SUN6I_CODEC_OM_DACA_CTRL_LMIX_MIC1,
  776. SUN6I_CODEC_OM_DACA_CTRL_RMIX_MIC1, 1, 0),
  777. SOC_DAPM_DOUBLE("Mic2 Playback Switch",
  778. SUN6I_CODEC_OM_DACA_CTRL,
  779. SUN6I_CODEC_OM_DACA_CTRL_LMIX_MIC2,
  780. SUN6I_CODEC_OM_DACA_CTRL_RMIX_MIC2, 1, 0),
  781. };
  782. /* ADC mixer controls */
  783. static const struct snd_kcontrol_new sun6i_codec_adc_mixer_controls[] = {
  784. SOC_DAPM_DOUBLE("Mixer Capture Switch",
  785. SUN6I_CODEC_ADC_ACTL,
  786. SUN6I_CODEC_ADC_ACTL_LADCMIX_OMIXL,
  787. SUN6I_CODEC_ADC_ACTL_RADCMIX_OMIXR, 1, 0),
  788. SOC_DAPM_DOUBLE("Mixer Reversed Capture Switch",
  789. SUN6I_CODEC_ADC_ACTL,
  790. SUN6I_CODEC_ADC_ACTL_LADCMIX_OMIXR,
  791. SUN6I_CODEC_ADC_ACTL_RADCMIX_OMIXL, 1, 0),
  792. SOC_DAPM_DOUBLE("Line In Capture Switch",
  793. SUN6I_CODEC_ADC_ACTL,
  794. SUN6I_CODEC_ADC_ACTL_LADCMIX_LINEINL,
  795. SUN6I_CODEC_ADC_ACTL_RADCMIX_LINEINR, 1, 0),
  796. SOC_DAPM_DOUBLE("Mic1 Capture Switch",
  797. SUN6I_CODEC_ADC_ACTL,
  798. SUN6I_CODEC_ADC_ACTL_LADCMIX_MIC1,
  799. SUN6I_CODEC_ADC_ACTL_RADCMIX_MIC1, 1, 0),
  800. SOC_DAPM_DOUBLE("Mic2 Capture Switch",
  801. SUN6I_CODEC_ADC_ACTL,
  802. SUN6I_CODEC_ADC_ACTL_LADCMIX_MIC2,
  803. SUN6I_CODEC_ADC_ACTL_RADCMIX_MIC2, 1, 0),
  804. };
  805. /* headphone controls */
  806. static const char * const sun6i_codec_hp_src_enum_text[] = {
  807. "DAC", "Mixer",
  808. };
  809. static SOC_ENUM_DOUBLE_DECL(sun6i_codec_hp_src_enum,
  810. SUN6I_CODEC_OM_DACA_CTRL,
  811. SUN6I_CODEC_OM_DACA_CTRL_LHPIS,
  812. SUN6I_CODEC_OM_DACA_CTRL_RHPIS,
  813. sun6i_codec_hp_src_enum_text);
  814. static const struct snd_kcontrol_new sun6i_codec_hp_src[] = {
  815. SOC_DAPM_ENUM("Headphone Source Playback Route",
  816. sun6i_codec_hp_src_enum),
  817. };
  818. /* microphone controls */
  819. static const char * const sun6i_codec_mic2_src_enum_text[] = {
  820. "Mic2", "Mic3",
  821. };
  822. static SOC_ENUM_SINGLE_DECL(sun6i_codec_mic2_src_enum,
  823. SUN6I_CODEC_MIC_CTRL,
  824. SUN6I_CODEC_MIC_CTRL_MIC2SLT,
  825. sun6i_codec_mic2_src_enum_text);
  826. static const struct snd_kcontrol_new sun6i_codec_mic2_src[] = {
  827. SOC_DAPM_ENUM("Mic2 Amplifier Source Route",
  828. sun6i_codec_mic2_src_enum),
  829. };
  830. /* line out controls */
  831. static const char * const sun6i_codec_lineout_src_enum_text[] = {
  832. "Stereo", "Mono Differential",
  833. };
  834. static SOC_ENUM_DOUBLE_DECL(sun6i_codec_lineout_src_enum,
  835. SUN6I_CODEC_MIC_CTRL,
  836. SUN6I_CODEC_MIC_CTRL_LINEOUTLSRC,
  837. SUN6I_CODEC_MIC_CTRL_LINEOUTRSRC,
  838. sun6i_codec_lineout_src_enum_text);
  839. static const struct snd_kcontrol_new sun6i_codec_lineout_src[] = {
  840. SOC_DAPM_ENUM("Line Out Source Playback Route",
  841. sun6i_codec_lineout_src_enum),
  842. };
  843. /* volume / mute controls */
  844. static const DECLARE_TLV_DB_SCALE(sun6i_codec_dvol_scale, -7308, 116, 0);
  845. static const DECLARE_TLV_DB_SCALE(sun6i_codec_hp_vol_scale, -6300, 100, 1);
  846. static const DECLARE_TLV_DB_SCALE(sun6i_codec_out_mixer_pregain_scale,
  847. -450, 150, 0);
  848. static const DECLARE_TLV_DB_RANGE(sun6i_codec_lineout_vol_scale,
  849. 0, 1, TLV_DB_SCALE_ITEM(TLV_DB_GAIN_MUTE, 0, 1),
  850. 2, 31, TLV_DB_SCALE_ITEM(-4350, 150, 0),
  851. );
  852. static const DECLARE_TLV_DB_RANGE(sun6i_codec_mic_gain_scale,
  853. 0, 0, TLV_DB_SCALE_ITEM(0, 0, 0),
  854. 1, 7, TLV_DB_SCALE_ITEM(2400, 300, 0),
  855. );
  856. static const struct snd_kcontrol_new sun6i_codec_codec_widgets[] = {
  857. SOC_SINGLE_TLV("DAC Playback Volume", SUN4I_CODEC_DAC_DPC,
  858. SUN4I_CODEC_DAC_DPC_DVOL, 0x3f, 1,
  859. sun6i_codec_dvol_scale),
  860. SOC_SINGLE_TLV("Headphone Playback Volume",
  861. SUN6I_CODEC_OM_DACA_CTRL,
  862. SUN6I_CODEC_OM_DACA_CTRL_HPVOL, 0x3f, 0,
  863. sun6i_codec_hp_vol_scale),
  864. SOC_SINGLE_TLV("Line Out Playback Volume",
  865. SUN6I_CODEC_MIC_CTRL,
  866. SUN6I_CODEC_MIC_CTRL_LINEOUTVC, 0x1f, 0,
  867. sun6i_codec_lineout_vol_scale),
  868. SOC_DOUBLE("Headphone Playback Switch",
  869. SUN6I_CODEC_OM_DACA_CTRL,
  870. SUN6I_CODEC_OM_DACA_CTRL_LHPPAMUTE,
  871. SUN6I_CODEC_OM_DACA_CTRL_RHPPAMUTE, 1, 0),
  872. SOC_DOUBLE("Line Out Playback Switch",
  873. SUN6I_CODEC_MIC_CTRL,
  874. SUN6I_CODEC_MIC_CTRL_LINEOUTLEN,
  875. SUN6I_CODEC_MIC_CTRL_LINEOUTREN, 1, 0),
  876. /* Mixer pre-gains */
  877. SOC_SINGLE_TLV("Line In Playback Volume",
  878. SUN6I_CODEC_OM_PA_CTRL, SUN6I_CODEC_OM_PA_CTRL_LINEING,
  879. 0x7, 0, sun6i_codec_out_mixer_pregain_scale),
  880. SOC_SINGLE_TLV("Mic1 Playback Volume",
  881. SUN6I_CODEC_OM_PA_CTRL, SUN6I_CODEC_OM_PA_CTRL_MIC1G,
  882. 0x7, 0, sun6i_codec_out_mixer_pregain_scale),
  883. SOC_SINGLE_TLV("Mic2 Playback Volume",
  884. SUN6I_CODEC_OM_PA_CTRL, SUN6I_CODEC_OM_PA_CTRL_MIC2G,
  885. 0x7, 0, sun6i_codec_out_mixer_pregain_scale),
  886. /* Microphone Amp boost gains */
  887. SOC_SINGLE_TLV("Mic1 Boost Volume", SUN6I_CODEC_MIC_CTRL,
  888. SUN6I_CODEC_MIC_CTRL_MIC1BOOST, 0x7, 0,
  889. sun6i_codec_mic_gain_scale),
  890. SOC_SINGLE_TLV("Mic2 Boost Volume", SUN6I_CODEC_MIC_CTRL,
  891. SUN6I_CODEC_MIC_CTRL_MIC2BOOST, 0x7, 0,
  892. sun6i_codec_mic_gain_scale),
  893. SOC_DOUBLE_TLV("ADC Capture Volume",
  894. SUN6I_CODEC_ADC_ACTL, SUN6I_CODEC_ADC_ACTL_ADCLG,
  895. SUN6I_CODEC_ADC_ACTL_ADCRG, 0x7, 0,
  896. sun6i_codec_out_mixer_pregain_scale),
  897. };
  898. static const struct snd_soc_dapm_widget sun6i_codec_codec_dapm_widgets[] = {
  899. /* Microphone inputs */
  900. SND_SOC_DAPM_INPUT("MIC1"),
  901. SND_SOC_DAPM_INPUT("MIC2"),
  902. SND_SOC_DAPM_INPUT("MIC3"),
  903. /* Microphone Bias */
  904. SND_SOC_DAPM_SUPPLY("HBIAS", SUN6I_CODEC_MIC_CTRL,
  905. SUN6I_CODEC_MIC_CTRL_HBIASEN, 0, NULL, 0),
  906. SND_SOC_DAPM_SUPPLY("MBIAS", SUN6I_CODEC_MIC_CTRL,
  907. SUN6I_CODEC_MIC_CTRL_MBIASEN, 0, NULL, 0),
  908. /* Mic input path */
  909. SND_SOC_DAPM_MUX("Mic2 Amplifier Source Route",
  910. SND_SOC_NOPM, 0, 0, sun6i_codec_mic2_src),
  911. SND_SOC_DAPM_PGA("Mic1 Amplifier", SUN6I_CODEC_MIC_CTRL,
  912. SUN6I_CODEC_MIC_CTRL_MIC1AMPEN, 0, NULL, 0),
  913. SND_SOC_DAPM_PGA("Mic2 Amplifier", SUN6I_CODEC_MIC_CTRL,
  914. SUN6I_CODEC_MIC_CTRL_MIC2AMPEN, 0, NULL, 0),
  915. /* Line In */
  916. SND_SOC_DAPM_INPUT("LINEIN"),
  917. /* Digital parts of the ADCs */
  918. SND_SOC_DAPM_SUPPLY("ADC Enable", SUN6I_CODEC_ADC_FIFOC,
  919. SUN6I_CODEC_ADC_FIFOC_EN_AD, 0,
  920. NULL, 0),
  921. /* Analog parts of the ADCs */
  922. SND_SOC_DAPM_ADC("Left ADC", "Codec Capture", SUN6I_CODEC_ADC_ACTL,
  923. SUN6I_CODEC_ADC_ACTL_ADCLEN, 0),
  924. SND_SOC_DAPM_ADC("Right ADC", "Codec Capture", SUN6I_CODEC_ADC_ACTL,
  925. SUN6I_CODEC_ADC_ACTL_ADCREN, 0),
  926. /* ADC Mixers */
  927. SOC_MIXER_ARRAY("Left ADC Mixer", SND_SOC_NOPM, 0, 0,
  928. sun6i_codec_adc_mixer_controls),
  929. SOC_MIXER_ARRAY("Right ADC Mixer", SND_SOC_NOPM, 0, 0,
  930. sun6i_codec_adc_mixer_controls),
  931. /* Digital parts of the DACs */
  932. SND_SOC_DAPM_SUPPLY("DAC Enable", SUN4I_CODEC_DAC_DPC,
  933. SUN4I_CODEC_DAC_DPC_EN_DA, 0,
  934. NULL, 0),
  935. /* Analog parts of the DACs */
  936. SND_SOC_DAPM_DAC("Left DAC", "Codec Playback",
  937. SUN6I_CODEC_OM_DACA_CTRL,
  938. SUN6I_CODEC_OM_DACA_CTRL_DACALEN, 0),
  939. SND_SOC_DAPM_DAC("Right DAC", "Codec Playback",
  940. SUN6I_CODEC_OM_DACA_CTRL,
  941. SUN6I_CODEC_OM_DACA_CTRL_DACAREN, 0),
  942. /* Mixers */
  943. SOC_MIXER_ARRAY("Left Mixer", SUN6I_CODEC_OM_DACA_CTRL,
  944. SUN6I_CODEC_OM_DACA_CTRL_LMIXEN, 0,
  945. sun6i_codec_mixer_controls),
  946. SOC_MIXER_ARRAY("Right Mixer", SUN6I_CODEC_OM_DACA_CTRL,
  947. SUN6I_CODEC_OM_DACA_CTRL_RMIXEN, 0,
  948. sun6i_codec_mixer_controls),
  949. /* Headphone output path */
  950. SND_SOC_DAPM_MUX("Headphone Source Playback Route",
  951. SND_SOC_NOPM, 0, 0, sun6i_codec_hp_src),
  952. SND_SOC_DAPM_OUT_DRV("Headphone Amp", SUN6I_CODEC_OM_PA_CTRL,
  953. SUN6I_CODEC_OM_PA_CTRL_HPPAEN, 0, NULL, 0),
  954. SND_SOC_DAPM_SUPPLY("HPCOM Protection", SUN6I_CODEC_OM_PA_CTRL,
  955. SUN6I_CODEC_OM_PA_CTRL_COMPTEN, 0, NULL, 0),
  956. SND_SOC_DAPM_REG(snd_soc_dapm_supply, "HPCOM", SUN6I_CODEC_OM_PA_CTRL,
  957. SUN6I_CODEC_OM_PA_CTRL_HPCOM_CTL, 0x3, 0x3, 0),
  958. SND_SOC_DAPM_OUTPUT("HP"),
  959. /* Line Out path */
  960. SND_SOC_DAPM_MUX("Line Out Source Playback Route",
  961. SND_SOC_NOPM, 0, 0, sun6i_codec_lineout_src),
  962. SND_SOC_DAPM_OUTPUT("LINEOUT"),
  963. };
  964. static const struct snd_soc_dapm_route sun6i_codec_codec_dapm_routes[] = {
  965. /* DAC Routes */
  966. { "Left DAC", NULL, "DAC Enable" },
  967. { "Right DAC", NULL, "DAC Enable" },
  968. /* Microphone Routes */
  969. { "Mic1 Amplifier", NULL, "MIC1"},
  970. { "Mic2 Amplifier Source Route", "Mic2", "MIC2" },
  971. { "Mic2 Amplifier Source Route", "Mic3", "MIC3" },
  972. { "Mic2 Amplifier", NULL, "Mic2 Amplifier Source Route"},
  973. /* Left Mixer Routes */
  974. { "Left Mixer", "DAC Playback Switch", "Left DAC" },
  975. { "Left Mixer", "DAC Reversed Playback Switch", "Right DAC" },
  976. { "Left Mixer", "Line In Playback Switch", "LINEIN" },
  977. { "Left Mixer", "Mic1 Playback Switch", "Mic1 Amplifier" },
  978. { "Left Mixer", "Mic2 Playback Switch", "Mic2 Amplifier" },
  979. /* Right Mixer Routes */
  980. { "Right Mixer", "DAC Playback Switch", "Right DAC" },
  981. { "Right Mixer", "DAC Reversed Playback Switch", "Left DAC" },
  982. { "Right Mixer", "Line In Playback Switch", "LINEIN" },
  983. { "Right Mixer", "Mic1 Playback Switch", "Mic1 Amplifier" },
  984. { "Right Mixer", "Mic2 Playback Switch", "Mic2 Amplifier" },
  985. /* Left ADC Mixer Routes */
  986. { "Left ADC Mixer", "Mixer Capture Switch", "Left Mixer" },
  987. { "Left ADC Mixer", "Mixer Reversed Capture Switch", "Right Mixer" },
  988. { "Left ADC Mixer", "Line In Capture Switch", "LINEIN" },
  989. { "Left ADC Mixer", "Mic1 Capture Switch", "Mic1 Amplifier" },
  990. { "Left ADC Mixer", "Mic2 Capture Switch", "Mic2 Amplifier" },
  991. /* Right ADC Mixer Routes */
  992. { "Right ADC Mixer", "Mixer Capture Switch", "Right Mixer" },
  993. { "Right ADC Mixer", "Mixer Reversed Capture Switch", "Left Mixer" },
  994. { "Right ADC Mixer", "Line In Capture Switch", "LINEIN" },
  995. { "Right ADC Mixer", "Mic1 Capture Switch", "Mic1 Amplifier" },
  996. { "Right ADC Mixer", "Mic2 Capture Switch", "Mic2 Amplifier" },
  997. /* Headphone Routes */
  998. { "Headphone Source Playback Route", "DAC", "Left DAC" },
  999. { "Headphone Source Playback Route", "DAC", "Right DAC" },
  1000. { "Headphone Source Playback Route", "Mixer", "Left Mixer" },
  1001. { "Headphone Source Playback Route", "Mixer", "Right Mixer" },
  1002. { "Headphone Amp", NULL, "Headphone Source Playback Route" },
  1003. { "HP", NULL, "Headphone Amp" },
  1004. { "HPCOM", NULL, "HPCOM Protection" },
  1005. /* Line Out Routes */
  1006. { "Line Out Source Playback Route", "Stereo", "Left Mixer" },
  1007. { "Line Out Source Playback Route", "Stereo", "Right Mixer" },
  1008. { "Line Out Source Playback Route", "Mono Differential", "Left Mixer" },
  1009. { "Line Out Source Playback Route", "Mono Differential", "Right Mixer" },
  1010. { "LINEOUT", NULL, "Line Out Source Playback Route" },
  1011. /* ADC Routes */
  1012. { "Left ADC", NULL, "ADC Enable" },
  1013. { "Right ADC", NULL, "ADC Enable" },
  1014. { "Left ADC", NULL, "Left ADC Mixer" },
  1015. { "Right ADC", NULL, "Right ADC Mixer" },
  1016. };
  1017. static const struct snd_soc_component_driver sun6i_codec_codec = {
  1018. .controls = sun6i_codec_codec_widgets,
  1019. .num_controls = ARRAY_SIZE(sun6i_codec_codec_widgets),
  1020. .dapm_widgets = sun6i_codec_codec_dapm_widgets,
  1021. .num_dapm_widgets = ARRAY_SIZE(sun6i_codec_codec_dapm_widgets),
  1022. .dapm_routes = sun6i_codec_codec_dapm_routes,
  1023. .num_dapm_routes = ARRAY_SIZE(sun6i_codec_codec_dapm_routes),
  1024. .idle_bias_on = 1,
  1025. .use_pmdown_time = 1,
  1026. .endianness = 1,
  1027. };
  1028. /* sun8i A23 codec */
  1029. static const struct snd_kcontrol_new sun8i_a23_codec_codec_controls[] = {
  1030. SOC_SINGLE_TLV("DAC Playback Volume", SUN4I_CODEC_DAC_DPC,
  1031. SUN4I_CODEC_DAC_DPC_DVOL, 0x3f, 1,
  1032. sun6i_codec_dvol_scale),
  1033. };
  1034. static const struct snd_soc_dapm_widget sun8i_a23_codec_codec_widgets[] = {
  1035. /* Digital parts of the ADCs */
  1036. SND_SOC_DAPM_SUPPLY("ADC Enable", SUN6I_CODEC_ADC_FIFOC,
  1037. SUN6I_CODEC_ADC_FIFOC_EN_AD, 0, NULL, 0),
  1038. /* Digital parts of the DACs */
  1039. SND_SOC_DAPM_SUPPLY("DAC Enable", SUN4I_CODEC_DAC_DPC,
  1040. SUN4I_CODEC_DAC_DPC_EN_DA, 0, NULL, 0),
  1041. };
  1042. static const struct snd_soc_component_driver sun8i_a23_codec_codec = {
  1043. .controls = sun8i_a23_codec_codec_controls,
  1044. .num_controls = ARRAY_SIZE(sun8i_a23_codec_codec_controls),
  1045. .dapm_widgets = sun8i_a23_codec_codec_widgets,
  1046. .num_dapm_widgets = ARRAY_SIZE(sun8i_a23_codec_codec_widgets),
  1047. .idle_bias_on = 1,
  1048. .use_pmdown_time = 1,
  1049. .endianness = 1,
  1050. };
  1051. static const struct snd_soc_component_driver sun4i_codec_component = {
  1052. .name = "sun4i-codec",
  1053. .legacy_dai_naming = 1,
  1054. #ifdef CONFIG_DEBUG_FS
  1055. .debugfs_prefix = "cpu",
  1056. #endif
  1057. };
  1058. #define SUN4I_CODEC_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | \
  1059. SNDRV_PCM_FMTBIT_S32_LE)
  1060. static int sun4i_codec_dai_probe(struct snd_soc_dai *dai)
  1061. {
  1062. struct snd_soc_card *card = snd_soc_dai_get_drvdata(dai);
  1063. struct sun4i_codec *scodec = snd_soc_card_get_drvdata(card);
  1064. snd_soc_dai_init_dma_data(dai, &scodec->playback_dma_data,
  1065. &scodec->capture_dma_data);
  1066. return 0;
  1067. }
  1068. static const struct snd_soc_dai_ops dummy_dai_ops = {
  1069. .probe = sun4i_codec_dai_probe,
  1070. };
  1071. static struct snd_soc_dai_driver dummy_cpu_dai = {
  1072. .name = "sun4i-codec-cpu-dai",
  1073. .playback = {
  1074. .stream_name = "Playback",
  1075. .channels_min = 1,
  1076. .channels_max = 2,
  1077. .rates = SUN4I_CODEC_RATES,
  1078. .formats = SUN4I_CODEC_FORMATS,
  1079. .sig_bits = 24,
  1080. },
  1081. .capture = {
  1082. .stream_name = "Capture",
  1083. .channels_min = 1,
  1084. .channels_max = 2,
  1085. .rates = SUN4I_CODEC_RATES,
  1086. .formats = SUN4I_CODEC_FORMATS,
  1087. .sig_bits = 24,
  1088. },
  1089. .ops = &dummy_dai_ops,
  1090. };
  1091. static struct snd_soc_dai_link *sun4i_codec_create_link(struct device *dev,
  1092. int *num_links)
  1093. {
  1094. struct snd_soc_dai_link *link = devm_kzalloc(dev, sizeof(*link),
  1095. GFP_KERNEL);
  1096. struct snd_soc_dai_link_component *dlc = devm_kzalloc(dev,
  1097. 3 * sizeof(*dlc), GFP_KERNEL);
  1098. if (!link || !dlc)
  1099. return NULL;
  1100. link->cpus = &dlc[0];
  1101. link->codecs = &dlc[1];
  1102. link->platforms = &dlc[2];
  1103. link->num_cpus = 1;
  1104. link->num_codecs = 1;
  1105. link->num_platforms = 1;
  1106. link->name = "cdc";
  1107. link->stream_name = "CDC PCM";
  1108. link->codecs->dai_name = "Codec";
  1109. link->cpus->dai_name = dev_name(dev);
  1110. link->codecs->name = dev_name(dev);
  1111. link->platforms->name = dev_name(dev);
  1112. link->dai_fmt = SND_SOC_DAIFMT_I2S;
  1113. *num_links = 1;
  1114. return link;
  1115. };
  1116. static int sun4i_codec_spk_event(struct snd_soc_dapm_widget *w,
  1117. struct snd_kcontrol *k, int event)
  1118. {
  1119. struct sun4i_codec *scodec = snd_soc_card_get_drvdata(w->dapm->card);
  1120. gpiod_set_value_cansleep(scodec->gpio_pa,
  1121. !!SND_SOC_DAPM_EVENT_ON(event));
  1122. if (SND_SOC_DAPM_EVENT_ON(event)) {
  1123. /*
  1124. * Need a delay to wait for DAC to push the data. 700ms seems
  1125. * to be the best compromise not to feel this delay while
  1126. * playing a sound.
  1127. */
  1128. msleep(700);
  1129. }
  1130. return 0;
  1131. }
  1132. static const struct snd_soc_dapm_widget sun4i_codec_card_dapm_widgets[] = {
  1133. SND_SOC_DAPM_SPK("Speaker", sun4i_codec_spk_event),
  1134. };
  1135. static const struct snd_soc_dapm_route sun4i_codec_card_dapm_routes[] = {
  1136. { "Speaker", NULL, "HP Right" },
  1137. { "Speaker", NULL, "HP Left" },
  1138. };
  1139. static struct snd_soc_card *sun4i_codec_create_card(struct device *dev)
  1140. {
  1141. struct snd_soc_card *card;
  1142. card = devm_kzalloc(dev, sizeof(*card), GFP_KERNEL);
  1143. if (!card)
  1144. return ERR_PTR(-ENOMEM);
  1145. card->dai_link = sun4i_codec_create_link(dev, &card->num_links);
  1146. if (!card->dai_link)
  1147. return ERR_PTR(-ENOMEM);
  1148. card->dev = dev;
  1149. card->owner = THIS_MODULE;
  1150. card->name = "sun4i-codec";
  1151. card->dapm_widgets = sun4i_codec_card_dapm_widgets;
  1152. card->num_dapm_widgets = ARRAY_SIZE(sun4i_codec_card_dapm_widgets);
  1153. card->dapm_routes = sun4i_codec_card_dapm_routes;
  1154. card->num_dapm_routes = ARRAY_SIZE(sun4i_codec_card_dapm_routes);
  1155. return card;
  1156. };
  1157. static const struct snd_soc_dapm_widget sun6i_codec_card_dapm_widgets[] = {
  1158. SND_SOC_DAPM_HP("Headphone", NULL),
  1159. SND_SOC_DAPM_LINE("Line In", NULL),
  1160. SND_SOC_DAPM_LINE("Line Out", NULL),
  1161. SND_SOC_DAPM_MIC("Headset Mic", NULL),
  1162. SND_SOC_DAPM_MIC("Mic", NULL),
  1163. SND_SOC_DAPM_SPK("Speaker", sun4i_codec_spk_event),
  1164. };
  1165. static struct snd_soc_card *sun6i_codec_create_card(struct device *dev)
  1166. {
  1167. struct snd_soc_card *card;
  1168. int ret;
  1169. card = devm_kzalloc(dev, sizeof(*card), GFP_KERNEL);
  1170. if (!card)
  1171. return ERR_PTR(-ENOMEM);
  1172. card->dai_link = sun4i_codec_create_link(dev, &card->num_links);
  1173. if (!card->dai_link)
  1174. return ERR_PTR(-ENOMEM);
  1175. card->dev = dev;
  1176. card->owner = THIS_MODULE;
  1177. card->name = "A31 Audio Codec";
  1178. card->dapm_widgets = sun6i_codec_card_dapm_widgets;
  1179. card->num_dapm_widgets = ARRAY_SIZE(sun6i_codec_card_dapm_widgets);
  1180. card->fully_routed = true;
  1181. ret = snd_soc_of_parse_audio_routing(card, "allwinner,audio-routing");
  1182. if (ret)
  1183. dev_warn(dev, "failed to parse audio-routing: %d\n", ret);
  1184. return card;
  1185. };
  1186. /* Connect digital side enables to analog side widgets */
  1187. static const struct snd_soc_dapm_route sun8i_codec_card_routes[] = {
  1188. /* ADC Routes */
  1189. { "Left ADC", NULL, "ADC Enable" },
  1190. { "Right ADC", NULL, "ADC Enable" },
  1191. { "Codec Capture", NULL, "Left ADC" },
  1192. { "Codec Capture", NULL, "Right ADC" },
  1193. /* DAC Routes */
  1194. { "Left DAC", NULL, "DAC Enable" },
  1195. { "Right DAC", NULL, "DAC Enable" },
  1196. { "Left DAC", NULL, "Codec Playback" },
  1197. { "Right DAC", NULL, "Codec Playback" },
  1198. };
  1199. static struct snd_soc_aux_dev aux_dev = {
  1200. .dlc = COMP_EMPTY(),
  1201. };
  1202. static struct snd_soc_card *sun8i_a23_codec_create_card(struct device *dev)
  1203. {
  1204. struct snd_soc_card *card;
  1205. int ret;
  1206. card = devm_kzalloc(dev, sizeof(*card), GFP_KERNEL);
  1207. if (!card)
  1208. return ERR_PTR(-ENOMEM);
  1209. aux_dev.dlc.of_node = of_parse_phandle(dev->of_node,
  1210. "allwinner,codec-analog-controls",
  1211. 0);
  1212. if (!aux_dev.dlc.of_node) {
  1213. dev_err(dev, "Can't find analog controls for codec.\n");
  1214. return ERR_PTR(-EINVAL);
  1215. }
  1216. card->dai_link = sun4i_codec_create_link(dev, &card->num_links);
  1217. if (!card->dai_link)
  1218. return ERR_PTR(-ENOMEM);
  1219. card->dev = dev;
  1220. card->owner = THIS_MODULE;
  1221. card->name = "A23 Audio Codec";
  1222. card->dapm_widgets = sun6i_codec_card_dapm_widgets;
  1223. card->num_dapm_widgets = ARRAY_SIZE(sun6i_codec_card_dapm_widgets);
  1224. card->dapm_routes = sun8i_codec_card_routes;
  1225. card->num_dapm_routes = ARRAY_SIZE(sun8i_codec_card_routes);
  1226. card->aux_dev = &aux_dev;
  1227. card->num_aux_devs = 1;
  1228. card->fully_routed = true;
  1229. ret = snd_soc_of_parse_audio_routing(card, "allwinner,audio-routing");
  1230. if (ret)
  1231. dev_warn(dev, "failed to parse audio-routing: %d\n", ret);
  1232. return card;
  1233. };
  1234. static struct snd_soc_card *sun8i_h3_codec_create_card(struct device *dev)
  1235. {
  1236. struct snd_soc_card *card;
  1237. int ret;
  1238. card = devm_kzalloc(dev, sizeof(*card), GFP_KERNEL);
  1239. if (!card)
  1240. return ERR_PTR(-ENOMEM);
  1241. aux_dev.dlc.of_node = of_parse_phandle(dev->of_node,
  1242. "allwinner,codec-analog-controls",
  1243. 0);
  1244. if (!aux_dev.dlc.of_node) {
  1245. dev_err(dev, "Can't find analog controls for codec.\n");
  1246. return ERR_PTR(-EINVAL);
  1247. }
  1248. card->dai_link = sun4i_codec_create_link(dev, &card->num_links);
  1249. if (!card->dai_link)
  1250. return ERR_PTR(-ENOMEM);
  1251. card->dev = dev;
  1252. card->owner = THIS_MODULE;
  1253. card->name = "H3 Audio Codec";
  1254. card->dapm_widgets = sun6i_codec_card_dapm_widgets;
  1255. card->num_dapm_widgets = ARRAY_SIZE(sun6i_codec_card_dapm_widgets);
  1256. card->dapm_routes = sun8i_codec_card_routes;
  1257. card->num_dapm_routes = ARRAY_SIZE(sun8i_codec_card_routes);
  1258. card->aux_dev = &aux_dev;
  1259. card->num_aux_devs = 1;
  1260. card->fully_routed = true;
  1261. ret = snd_soc_of_parse_audio_routing(card, "allwinner,audio-routing");
  1262. if (ret)
  1263. dev_warn(dev, "failed to parse audio-routing: %d\n", ret);
  1264. return card;
  1265. };
  1266. static struct snd_soc_card *sun8i_v3s_codec_create_card(struct device *dev)
  1267. {
  1268. struct snd_soc_card *card;
  1269. int ret;
  1270. card = devm_kzalloc(dev, sizeof(*card), GFP_KERNEL);
  1271. if (!card)
  1272. return ERR_PTR(-ENOMEM);
  1273. aux_dev.dlc.of_node = of_parse_phandle(dev->of_node,
  1274. "allwinner,codec-analog-controls",
  1275. 0);
  1276. if (!aux_dev.dlc.of_node) {
  1277. dev_err(dev, "Can't find analog controls for codec.\n");
  1278. return ERR_PTR(-EINVAL);
  1279. }
  1280. card->dai_link = sun4i_codec_create_link(dev, &card->num_links);
  1281. if (!card->dai_link)
  1282. return ERR_PTR(-ENOMEM);
  1283. card->dev = dev;
  1284. card->owner = THIS_MODULE;
  1285. card->name = "V3s Audio Codec";
  1286. card->dapm_widgets = sun6i_codec_card_dapm_widgets;
  1287. card->num_dapm_widgets = ARRAY_SIZE(sun6i_codec_card_dapm_widgets);
  1288. card->dapm_routes = sun8i_codec_card_routes;
  1289. card->num_dapm_routes = ARRAY_SIZE(sun8i_codec_card_routes);
  1290. card->aux_dev = &aux_dev;
  1291. card->num_aux_devs = 1;
  1292. card->fully_routed = true;
  1293. ret = snd_soc_of_parse_audio_routing(card, "allwinner,audio-routing");
  1294. if (ret)
  1295. dev_warn(dev, "failed to parse audio-routing: %d\n", ret);
  1296. return card;
  1297. };
  1298. static const struct regmap_config sun4i_codec_regmap_config = {
  1299. .reg_bits = 32,
  1300. .reg_stride = 4,
  1301. .val_bits = 32,
  1302. .max_register = SUN4I_CODEC_ADC_RXCNT,
  1303. };
  1304. static const struct regmap_config sun6i_codec_regmap_config = {
  1305. .reg_bits = 32,
  1306. .reg_stride = 4,
  1307. .val_bits = 32,
  1308. .max_register = SUN6I_CODEC_HMIC_DATA,
  1309. };
  1310. static const struct regmap_config sun7i_codec_regmap_config = {
  1311. .reg_bits = 32,
  1312. .reg_stride = 4,
  1313. .val_bits = 32,
  1314. .max_register = SUN7I_CODEC_AC_MIC_PHONE_CAL,
  1315. };
  1316. static const struct regmap_config sun8i_a23_codec_regmap_config = {
  1317. .reg_bits = 32,
  1318. .reg_stride = 4,
  1319. .val_bits = 32,
  1320. .max_register = SUN8I_A23_CODEC_ADC_RXCNT,
  1321. };
  1322. static const struct regmap_config sun8i_h3_codec_regmap_config = {
  1323. .reg_bits = 32,
  1324. .reg_stride = 4,
  1325. .val_bits = 32,
  1326. .max_register = SUN8I_H3_CODEC_ADC_DBG,
  1327. };
  1328. static const struct regmap_config sun8i_v3s_codec_regmap_config = {
  1329. .reg_bits = 32,
  1330. .reg_stride = 4,
  1331. .val_bits = 32,
  1332. .max_register = SUN8I_H3_CODEC_ADC_DBG,
  1333. };
  1334. struct sun4i_codec_quirks {
  1335. const struct regmap_config *regmap_config;
  1336. const struct snd_soc_component_driver *codec;
  1337. struct snd_soc_card * (*create_card)(struct device *dev);
  1338. struct reg_field reg_adc_fifoc; /* used for regmap_field */
  1339. unsigned int reg_dac_txdata; /* TX FIFO offset for DMA config */
  1340. unsigned int reg_adc_rxdata; /* RX FIFO offset for DMA config */
  1341. bool has_reset;
  1342. };
  1343. static const struct sun4i_codec_quirks sun4i_codec_quirks = {
  1344. .regmap_config = &sun4i_codec_regmap_config,
  1345. .codec = &sun4i_codec_codec,
  1346. .create_card = sun4i_codec_create_card,
  1347. .reg_adc_fifoc = REG_FIELD(SUN4I_CODEC_ADC_FIFOC, 0, 31),
  1348. .reg_dac_txdata = SUN4I_CODEC_DAC_TXDATA,
  1349. .reg_adc_rxdata = SUN4I_CODEC_ADC_RXDATA,
  1350. };
  1351. static const struct sun4i_codec_quirks sun6i_a31_codec_quirks = {
  1352. .regmap_config = &sun6i_codec_regmap_config,
  1353. .codec = &sun6i_codec_codec,
  1354. .create_card = sun6i_codec_create_card,
  1355. .reg_adc_fifoc = REG_FIELD(SUN6I_CODEC_ADC_FIFOC, 0, 31),
  1356. .reg_dac_txdata = SUN4I_CODEC_DAC_TXDATA,
  1357. .reg_adc_rxdata = SUN6I_CODEC_ADC_RXDATA,
  1358. .has_reset = true,
  1359. };
  1360. static const struct sun4i_codec_quirks sun7i_codec_quirks = {
  1361. .regmap_config = &sun7i_codec_regmap_config,
  1362. .codec = &sun7i_codec_codec,
  1363. .create_card = sun4i_codec_create_card,
  1364. .reg_adc_fifoc = REG_FIELD(SUN4I_CODEC_ADC_FIFOC, 0, 31),
  1365. .reg_dac_txdata = SUN4I_CODEC_DAC_TXDATA,
  1366. .reg_adc_rxdata = SUN4I_CODEC_ADC_RXDATA,
  1367. };
  1368. static const struct sun4i_codec_quirks sun8i_a23_codec_quirks = {
  1369. .regmap_config = &sun8i_a23_codec_regmap_config,
  1370. .codec = &sun8i_a23_codec_codec,
  1371. .create_card = sun8i_a23_codec_create_card,
  1372. .reg_adc_fifoc = REG_FIELD(SUN6I_CODEC_ADC_FIFOC, 0, 31),
  1373. .reg_dac_txdata = SUN4I_CODEC_DAC_TXDATA,
  1374. .reg_adc_rxdata = SUN6I_CODEC_ADC_RXDATA,
  1375. .has_reset = true,
  1376. };
  1377. static const struct sun4i_codec_quirks sun8i_h3_codec_quirks = {
  1378. .regmap_config = &sun8i_h3_codec_regmap_config,
  1379. /*
  1380. * TODO Share the codec structure with A23 for now.
  1381. * This should be split out when adding digital audio
  1382. * processing support for the H3.
  1383. */
  1384. .codec = &sun8i_a23_codec_codec,
  1385. .create_card = sun8i_h3_codec_create_card,
  1386. .reg_adc_fifoc = REG_FIELD(SUN6I_CODEC_ADC_FIFOC, 0, 31),
  1387. .reg_dac_txdata = SUN8I_H3_CODEC_DAC_TXDATA,
  1388. .reg_adc_rxdata = SUN6I_CODEC_ADC_RXDATA,
  1389. .has_reset = true,
  1390. };
  1391. static const struct sun4i_codec_quirks sun8i_v3s_codec_quirks = {
  1392. .regmap_config = &sun8i_v3s_codec_regmap_config,
  1393. /*
  1394. * TODO The codec structure should be split out, like
  1395. * H3, when adding digital audio processing support.
  1396. */
  1397. .codec = &sun8i_a23_codec_codec,
  1398. .create_card = sun8i_v3s_codec_create_card,
  1399. .reg_adc_fifoc = REG_FIELD(SUN6I_CODEC_ADC_FIFOC, 0, 31),
  1400. .reg_dac_txdata = SUN8I_H3_CODEC_DAC_TXDATA,
  1401. .reg_adc_rxdata = SUN6I_CODEC_ADC_RXDATA,
  1402. .has_reset = true,
  1403. };
  1404. static const struct of_device_id sun4i_codec_of_match[] = {
  1405. {
  1406. .compatible = "allwinner,sun4i-a10-codec",
  1407. .data = &sun4i_codec_quirks,
  1408. },
  1409. {
  1410. .compatible = "allwinner,sun6i-a31-codec",
  1411. .data = &sun6i_a31_codec_quirks,
  1412. },
  1413. {
  1414. .compatible = "allwinner,sun7i-a20-codec",
  1415. .data = &sun7i_codec_quirks,
  1416. },
  1417. {
  1418. .compatible = "allwinner,sun8i-a23-codec",
  1419. .data = &sun8i_a23_codec_quirks,
  1420. },
  1421. {
  1422. .compatible = "allwinner,sun8i-h3-codec",
  1423. .data = &sun8i_h3_codec_quirks,
  1424. },
  1425. {
  1426. .compatible = "allwinner,sun8i-v3s-codec",
  1427. .data = &sun8i_v3s_codec_quirks,
  1428. },
  1429. {}
  1430. };
  1431. MODULE_DEVICE_TABLE(of, sun4i_codec_of_match);
  1432. static int sun4i_codec_probe(struct platform_device *pdev)
  1433. {
  1434. struct snd_soc_card *card;
  1435. struct sun4i_codec *scodec;
  1436. const struct sun4i_codec_quirks *quirks;
  1437. struct resource *res;
  1438. void __iomem *base;
  1439. int ret;
  1440. scodec = devm_kzalloc(&pdev->dev, sizeof(*scodec), GFP_KERNEL);
  1441. if (!scodec)
  1442. return -ENOMEM;
  1443. scodec->dev = &pdev->dev;
  1444. base = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
  1445. if (IS_ERR(base))
  1446. return PTR_ERR(base);
  1447. quirks = of_device_get_match_data(&pdev->dev);
  1448. if (quirks == NULL) {
  1449. dev_err(&pdev->dev, "Failed to determine the quirks to use\n");
  1450. return -ENODEV;
  1451. }
  1452. scodec->regmap = devm_regmap_init_mmio(&pdev->dev, base,
  1453. quirks->regmap_config);
  1454. if (IS_ERR(scodec->regmap)) {
  1455. dev_err(&pdev->dev, "Failed to create our regmap\n");
  1456. return PTR_ERR(scodec->regmap);
  1457. }
  1458. /* Get the clocks from the DT */
  1459. scodec->clk_apb = devm_clk_get(&pdev->dev, "apb");
  1460. if (IS_ERR(scodec->clk_apb)) {
  1461. dev_err(&pdev->dev, "Failed to get the APB clock\n");
  1462. return PTR_ERR(scodec->clk_apb);
  1463. }
  1464. scodec->clk_module = devm_clk_get(&pdev->dev, "codec");
  1465. if (IS_ERR(scodec->clk_module)) {
  1466. dev_err(&pdev->dev, "Failed to get the module clock\n");
  1467. return PTR_ERR(scodec->clk_module);
  1468. }
  1469. if (quirks->has_reset) {
  1470. scodec->rst = devm_reset_control_get_exclusive(&pdev->dev,
  1471. NULL);
  1472. if (IS_ERR(scodec->rst)) {
  1473. dev_err(&pdev->dev, "Failed to get reset control\n");
  1474. return PTR_ERR(scodec->rst);
  1475. }
  1476. }
  1477. scodec->gpio_pa = devm_gpiod_get_optional(&pdev->dev, "allwinner,pa",
  1478. GPIOD_OUT_LOW);
  1479. if (IS_ERR(scodec->gpio_pa)) {
  1480. ret = PTR_ERR(scodec->gpio_pa);
  1481. dev_err_probe(&pdev->dev, ret, "Failed to get pa gpio\n");
  1482. return ret;
  1483. }
  1484. /* reg_field setup */
  1485. scodec->reg_adc_fifoc = devm_regmap_field_alloc(&pdev->dev,
  1486. scodec->regmap,
  1487. quirks->reg_adc_fifoc);
  1488. if (IS_ERR(scodec->reg_adc_fifoc)) {
  1489. ret = PTR_ERR(scodec->reg_adc_fifoc);
  1490. dev_err(&pdev->dev, "Failed to create regmap fields: %d\n",
  1491. ret);
  1492. return ret;
  1493. }
  1494. /* Enable the bus clock */
  1495. if (clk_prepare_enable(scodec->clk_apb)) {
  1496. dev_err(&pdev->dev, "Failed to enable the APB clock\n");
  1497. return -EINVAL;
  1498. }
  1499. /* Deassert the reset control */
  1500. if (scodec->rst) {
  1501. ret = reset_control_deassert(scodec->rst);
  1502. if (ret) {
  1503. dev_err(&pdev->dev,
  1504. "Failed to deassert the reset control\n");
  1505. goto err_clk_disable;
  1506. }
  1507. }
  1508. /* DMA configuration for TX FIFO */
  1509. scodec->playback_dma_data.addr = res->start + quirks->reg_dac_txdata;
  1510. scodec->playback_dma_data.maxburst = 8;
  1511. scodec->playback_dma_data.addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
  1512. /* DMA configuration for RX FIFO */
  1513. scodec->capture_dma_data.addr = res->start + quirks->reg_adc_rxdata;
  1514. scodec->capture_dma_data.maxburst = 8;
  1515. scodec->capture_dma_data.addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
  1516. ret = devm_snd_soc_register_component(&pdev->dev, quirks->codec,
  1517. &sun4i_codec_dai, 1);
  1518. if (ret) {
  1519. dev_err(&pdev->dev, "Failed to register our codec\n");
  1520. goto err_assert_reset;
  1521. }
  1522. ret = devm_snd_soc_register_component(&pdev->dev,
  1523. &sun4i_codec_component,
  1524. &dummy_cpu_dai, 1);
  1525. if (ret) {
  1526. dev_err(&pdev->dev, "Failed to register our DAI\n");
  1527. goto err_assert_reset;
  1528. }
  1529. ret = devm_snd_dmaengine_pcm_register(&pdev->dev, NULL, 0);
  1530. if (ret) {
  1531. dev_err(&pdev->dev, "Failed to register against DMAEngine\n");
  1532. goto err_assert_reset;
  1533. }
  1534. card = quirks->create_card(&pdev->dev);
  1535. if (IS_ERR(card)) {
  1536. ret = PTR_ERR(card);
  1537. dev_err(&pdev->dev, "Failed to create our card\n");
  1538. goto err_assert_reset;
  1539. }
  1540. snd_soc_card_set_drvdata(card, scodec);
  1541. ret = snd_soc_register_card(card);
  1542. if (ret) {
  1543. dev_err_probe(&pdev->dev, ret, "Failed to register our card\n");
  1544. goto err_assert_reset;
  1545. }
  1546. return 0;
  1547. err_assert_reset:
  1548. if (scodec->rst)
  1549. reset_control_assert(scodec->rst);
  1550. err_clk_disable:
  1551. clk_disable_unprepare(scodec->clk_apb);
  1552. return ret;
  1553. }
  1554. static void sun4i_codec_remove(struct platform_device *pdev)
  1555. {
  1556. struct snd_soc_card *card = platform_get_drvdata(pdev);
  1557. struct sun4i_codec *scodec = snd_soc_card_get_drvdata(card);
  1558. snd_soc_unregister_card(card);
  1559. if (scodec->rst)
  1560. reset_control_assert(scodec->rst);
  1561. clk_disable_unprepare(scodec->clk_apb);
  1562. }
  1563. static struct platform_driver sun4i_codec_driver = {
  1564. .driver = {
  1565. .name = "sun4i-codec",
  1566. .of_match_table = sun4i_codec_of_match,
  1567. },
  1568. .probe = sun4i_codec_probe,
  1569. .remove = sun4i_codec_remove,
  1570. };
  1571. module_platform_driver(sun4i_codec_driver);
  1572. MODULE_DESCRIPTION("Allwinner A10 codec driver");
  1573. MODULE_AUTHOR("Emilio López <emilio@elopez.com.ar>");
  1574. MODULE_AUTHOR("Jon Smirl <jonsmirl@gmail.com>");
  1575. MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
  1576. MODULE_AUTHOR("Chen-Yu Tsai <wens@csie.org>");
  1577. MODULE_LICENSE("GPL");