sun4i-codec.c 58 KB

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