sun4i-codec.c 50 KB

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