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>
8 * Based on the Allwinner SDK driver, released under the GPL.
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.
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.
21 #include <linux/init.h>
22 #include <linux/kernel.h>
23 #include <linux/module.h>
24 #include <linux/platform_device.h>
25 #include <linux/delay.h>
26 #include <linux/slab.h>
28 #include <linux/of_address.h>
29 #include <linux/of_device.h>
30 #include <linux/of_platform.h>
31 #include <linux/clk.h>
32 #include <linux/regmap.h>
33 #include <linux/reset.h>
34 #include <linux/gpio/consumer.h>
36 #include <sound/core.h>
37 #include <sound/pcm.h>
38 #include <sound/pcm_params.h>
39 #include <sound/soc.h>
40 #include <sound/tlv.h>
41 #include <sound/initval.h>
42 #include <sound/dmaengine_pcm.h>
44 /* Codec DAC digital controls and FIFO registers */
45 #define SUN4I_CODEC_DAC_DPC (0x00)
46 #define SUN4I_CODEC_DAC_DPC_EN_DA (31)
47 #define SUN4I_CODEC_DAC_DPC_DVOL (12)
48 #define SUN4I_CODEC_DAC_FIFOC (0x04)
49 #define SUN4I_CODEC_DAC_FIFOC_DAC_FS (29)
50 #define SUN4I_CODEC_DAC_FIFOC_FIR_VERSION (28)
51 #define SUN4I_CODEC_DAC_FIFOC_SEND_LASAT (26)
52 #define SUN4I_CODEC_DAC_FIFOC_TX_FIFO_MODE (24)
53 #define SUN4I_CODEC_DAC_FIFOC_DRQ_CLR_CNT (21)
54 #define SUN4I_CODEC_DAC_FIFOC_TX_TRIG_LEVEL (8)
55 #define SUN4I_CODEC_DAC_FIFOC_MONO_EN (6)
56 #define SUN4I_CODEC_DAC_FIFOC_TX_SAMPLE_BITS (5)
57 #define SUN4I_CODEC_DAC_FIFOC_DAC_DRQ_EN (4)
58 #define SUN4I_CODEC_DAC_FIFOC_FIFO_FLUSH (0)
59 #define SUN4I_CODEC_DAC_FIFOS (0x08)
60 #define SUN4I_CODEC_DAC_TXDATA (0x0c)
62 /* Codec DAC side analog signal controls */
63 #define SUN4I_CODEC_DAC_ACTL (0x10)
64 #define SUN4I_CODEC_DAC_ACTL_DACAENR (31)
65 #define SUN4I_CODEC_DAC_ACTL_DACAENL (30)
66 #define SUN4I_CODEC_DAC_ACTL_MIXEN (29)
67 #define SUN4I_CODEC_DAC_ACTL_FMG (23)
68 #define SUN4I_CODEC_DAC_ACTL_MICG (20)
69 #define SUN4I_CODEC_DAC_ACTL_LFMS (17)
70 #define SUN4I_CODEC_DAC_ACTL_RFMS (16)
71 #define SUN4I_CODEC_DAC_ACTL_LDACLMIXS (15)
72 #define SUN4I_CODEC_DAC_ACTL_RDACRMIXS (14)
73 #define SUN4I_CODEC_DAC_ACTL_LDACRMIXS (13)
74 #define SUN4I_CODEC_DAC_ACTL_MIC1LS (12)
75 #define SUN4I_CODEC_DAC_ACTL_MIC1RS (11)
76 #define SUN4I_CODEC_DAC_ACTL_MIC2LS (10)
77 #define SUN4I_CODEC_DAC_ACTL_MIC2RS (9)
78 #define SUN4I_CODEC_DAC_ACTL_DACPAS (8)
79 #define SUN4I_CODEC_DAC_ACTL_MIXPAS (7)
80 #define SUN4I_CODEC_DAC_ACTL_PA_MUTE (6)
81 #define SUN4I_CODEC_DAC_ACTL_PA_VOL (0)
82 #define SUN4I_CODEC_DAC_TUNE (0x14)
83 #define SUN4I_CODEC_DAC_DEBUG (0x18)
85 /* Codec ADC digital controls and FIFO registers */
86 #define SUN4I_CODEC_ADC_FIFOC (0x1c)
87 #define SUN4I_CODEC_ADC_FIFOC_ADC_FS (29)
88 #define SUN4I_CODEC_ADC_FIFOC_EN_AD (28)
89 #define SUN4I_CODEC_ADC_FIFOC_RX_FIFO_MODE (24)
90 #define SUN4I_CODEC_ADC_FIFOC_RX_TRIG_LEVEL (8)
91 #define SUN4I_CODEC_ADC_FIFOC_MONO_EN (7)
92 #define SUN4I_CODEC_ADC_FIFOC_RX_SAMPLE_BITS (6)
93 #define SUN4I_CODEC_ADC_FIFOC_ADC_DRQ_EN (4)
94 #define SUN4I_CODEC_ADC_FIFOC_FIFO_FLUSH (0)
95 #define SUN4I_CODEC_ADC_FIFOS (0x20)
96 #define SUN4I_CODEC_ADC_RXDATA (0x24)
98 /* Codec ADC side analog signal controls */
99 #define SUN4I_CODEC_ADC_ACTL (0x28)
100 #define SUN4I_CODEC_ADC_ACTL_ADC_R_EN (31)
101 #define SUN4I_CODEC_ADC_ACTL_ADC_L_EN (30)
102 #define SUN4I_CODEC_ADC_ACTL_PREG1EN (29)
103 #define SUN4I_CODEC_ADC_ACTL_PREG2EN (28)
104 #define SUN4I_CODEC_ADC_ACTL_VMICEN (27)
105 #define SUN4I_CODEC_ADC_ACTL_PREG1 (25)
106 #define SUN4I_CODEC_ADC_ACTL_PREG2 (23)
107 #define SUN4I_CODEC_ADC_ACTL_VADCG (20)
108 #define SUN4I_CODEC_ADC_ACTL_ADCIS (17)
109 #define SUN4I_CODEC_ADC_ACTL_PA_EN (4)
110 #define SUN4I_CODEC_ADC_ACTL_DDE (3)
111 #define SUN4I_CODEC_ADC_DEBUG (0x2c)
114 #define SUN4I_CODEC_DAC_TXCNT (0x30)
115 #define SUN4I_CODEC_ADC_RXCNT (0x34)
117 /* Calibration register (sun7i only) */
118 #define SUN7I_CODEC_AC_DAC_CAL (0x38)
120 /* Microphone controls (sun7i only) */
121 #define SUN7I_CODEC_AC_MIC_PHONE_CAL (0x3c)
123 #define SUN7I_CODEC_AC_MIC_PHONE_CAL_PREG1 (29)
124 #define SUN7I_CODEC_AC_MIC_PHONE_CAL_PREG2 (26)
127 * sun6i specific registers
129 * sun6i shares the same digital control and FIFO registers as sun4i,
130 * but only the DAC digital controls are at the same offset. The others
131 * have been moved around to accommodate extra analog controls.
134 /* Codec DAC digital controls and FIFO registers */
135 #define SUN6I_CODEC_ADC_FIFOC (0x10)
136 #define SUN6I_CODEC_ADC_FIFOC_EN_AD (28)
137 #define SUN6I_CODEC_ADC_FIFOS (0x14)
138 #define SUN6I_CODEC_ADC_RXDATA (0x18)
140 /* Output mixer and gain controls */
141 #define SUN6I_CODEC_OM_DACA_CTRL (0x20)
142 #define SUN6I_CODEC_OM_DACA_CTRL_DACAREN (31)
143 #define SUN6I_CODEC_OM_DACA_CTRL_DACALEN (30)
144 #define SUN6I_CODEC_OM_DACA_CTRL_RMIXEN (29)
145 #define SUN6I_CODEC_OM_DACA_CTRL_LMIXEN (28)
146 #define SUN6I_CODEC_OM_DACA_CTRL_RMIX_MIC1 (23)
147 #define SUN6I_CODEC_OM_DACA_CTRL_RMIX_MIC2 (22)
148 #define SUN6I_CODEC_OM_DACA_CTRL_RMIX_PHONE (21)
149 #define SUN6I_CODEC_OM_DACA_CTRL_RMIX_PHONEP (20)
150 #define SUN6I_CODEC_OM_DACA_CTRL_RMIX_LINEINR (19)
151 #define SUN6I_CODEC_OM_DACA_CTRL_RMIX_DACR (18)
152 #define SUN6I_CODEC_OM_DACA_CTRL_RMIX_DACL (17)
153 #define SUN6I_CODEC_OM_DACA_CTRL_LMIX_MIC1 (16)
154 #define SUN6I_CODEC_OM_DACA_CTRL_LMIX_MIC2 (15)
155 #define SUN6I_CODEC_OM_DACA_CTRL_LMIX_PHONE (14)
156 #define SUN6I_CODEC_OM_DACA_CTRL_LMIX_PHONEN (13)
157 #define SUN6I_CODEC_OM_DACA_CTRL_LMIX_LINEINL (12)
158 #define SUN6I_CODEC_OM_DACA_CTRL_LMIX_DACL (11)
159 #define SUN6I_CODEC_OM_DACA_CTRL_LMIX_DACR (10)
160 #define SUN6I_CODEC_OM_DACA_CTRL_RHPIS (9)
161 #define SUN6I_CODEC_OM_DACA_CTRL_LHPIS (8)
162 #define SUN6I_CODEC_OM_DACA_CTRL_RHPPAMUTE (7)
163 #define SUN6I_CODEC_OM_DACA_CTRL_LHPPAMUTE (6)
164 #define SUN6I_CODEC_OM_DACA_CTRL_HPVOL (0)
165 #define SUN6I_CODEC_OM_PA_CTRL (0x24)
166 #define SUN6I_CODEC_OM_PA_CTRL_HPPAEN (31)
167 #define SUN6I_CODEC_OM_PA_CTRL_HPCOM_CTL (29)
168 #define SUN6I_CODEC_OM_PA_CTRL_COMPTEN (28)
169 #define SUN6I_CODEC_OM_PA_CTRL_MIC1G (15)
170 #define SUN6I_CODEC_OM_PA_CTRL_MIC2G (12)
171 #define SUN6I_CODEC_OM_PA_CTRL_LINEING (9)
172 #define SUN6I_CODEC_OM_PA_CTRL_PHONEG (6)
173 #define SUN6I_CODEC_OM_PA_CTRL_PHONEPG (3)
174 #define SUN6I_CODEC_OM_PA_CTRL_PHONENG (0)
176 /* Microphone, line out and phone out controls */
177 #define SUN6I_CODEC_MIC_CTRL (0x28)
178 #define SUN6I_CODEC_MIC_CTRL_HBIASEN (31)
179 #define SUN6I_CODEC_MIC_CTRL_MBIASEN (30)
180 #define SUN6I_CODEC_MIC_CTRL_MIC1AMPEN (28)
181 #define SUN6I_CODEC_MIC_CTRL_MIC1BOOST (25)
182 #define SUN6I_CODEC_MIC_CTRL_MIC2AMPEN (24)
183 #define SUN6I_CODEC_MIC_CTRL_MIC2BOOST (21)
184 #define SUN6I_CODEC_MIC_CTRL_MIC2SLT (20)
185 #define SUN6I_CODEC_MIC_CTRL_LINEOUTLEN (19)
186 #define SUN6I_CODEC_MIC_CTRL_LINEOUTREN (18)
187 #define SUN6I_CODEC_MIC_CTRL_LINEOUTLSRC (17)
188 #define SUN6I_CODEC_MIC_CTRL_LINEOUTRSRC (16)
189 #define SUN6I_CODEC_MIC_CTRL_LINEOUTVC (11)
190 #define SUN6I_CODEC_MIC_CTRL_PHONEPREG (8)
192 /* ADC mixer controls */
193 #define SUN6I_CODEC_ADC_ACTL (0x2c)
194 #define SUN6I_CODEC_ADC_ACTL_ADCREN (31)
195 #define SUN6I_CODEC_ADC_ACTL_ADCLEN (30)
196 #define SUN6I_CODEC_ADC_ACTL_ADCRG (27)
197 #define SUN6I_CODEC_ADC_ACTL_ADCLG (24)
198 #define SUN6I_CODEC_ADC_ACTL_RADCMIX_MIC1 (13)
199 #define SUN6I_CODEC_ADC_ACTL_RADCMIX_MIC2 (12)
200 #define SUN6I_CODEC_ADC_ACTL_RADCMIX_PHONE (11)
201 #define SUN6I_CODEC_ADC_ACTL_RADCMIX_PHONEP (10)
202 #define SUN6I_CODEC_ADC_ACTL_RADCMIX_LINEINR (9)
203 #define SUN6I_CODEC_ADC_ACTL_RADCMIX_OMIXR (8)
204 #define SUN6I_CODEC_ADC_ACTL_RADCMIX_OMIXL (7)
205 #define SUN6I_CODEC_ADC_ACTL_LADCMIX_MIC1 (6)
206 #define SUN6I_CODEC_ADC_ACTL_LADCMIX_MIC2 (5)
207 #define SUN6I_CODEC_ADC_ACTL_LADCMIX_PHONE (4)
208 #define SUN6I_CODEC_ADC_ACTL_LADCMIX_PHONEN (3)
209 #define SUN6I_CODEC_ADC_ACTL_LADCMIX_LINEINL (2)
210 #define SUN6I_CODEC_ADC_ACTL_LADCMIX_OMIXL (1)
211 #define SUN6I_CODEC_ADC_ACTL_LADCMIX_OMIXR (0)
213 /* Analog performance tuning controls */
214 #define SUN6I_CODEC_ADDA_TUNE (0x30)
216 /* Calibration controls */
217 #define SUN6I_CODEC_CALIBRATION (0x34)
220 #define SUN6I_CODEC_DAC_TXCNT (0x40)
221 #define SUN6I_CODEC_ADC_RXCNT (0x44)
223 /* headset jack detection and button support registers */
224 #define SUN6I_CODEC_HMIC_CTL (0x50)
225 #define SUN6I_CODEC_HMIC_DATA (0x54)
227 /* TODO sun6i DAP (Digital Audio Processing) bits */
229 /* FIFO counters moved on A23 */
230 #define SUN8I_A23_CODEC_DAC_TXCNT (0x1c)
231 #define SUN8I_A23_CODEC_ADC_RXCNT (0x20)
233 /* TX FIFO moved on H3 */
234 #define SUN8I_H3_CODEC_DAC_TXDATA (0x20)
235 #define SUN8I_H3_CODEC_DAC_DBG (0x48)
236 #define SUN8I_H3_CODEC_ADC_DBG (0x4c)
238 /* TODO H3 DAP (Digital Audio Processing) bits */
242 struct regmap *regmap;
244 struct clk *clk_module;
245 struct reset_control *rst;
246 struct gpio_desc *gpio_pa;
248 /* ADC_FIFOC register is at different offset on different SoCs */
249 struct regmap_field *reg_adc_fifoc;
251 struct snd_dmaengine_dai_dma_data capture_dma_data;
252 struct snd_dmaengine_dai_dma_data playback_dma_data;
255 static void sun4i_codec_start_playback(struct sun4i_codec *scodec)
258 regmap_update_bits(scodec->regmap, SUN4I_CODEC_DAC_FIFOC,
259 BIT(SUN4I_CODEC_DAC_FIFOC_FIFO_FLUSH),
260 BIT(SUN4I_CODEC_DAC_FIFOC_FIFO_FLUSH));
263 regmap_update_bits(scodec->regmap, SUN4I_CODEC_DAC_FIFOC,
264 BIT(SUN4I_CODEC_DAC_FIFOC_DAC_DRQ_EN),
265 BIT(SUN4I_CODEC_DAC_FIFOC_DAC_DRQ_EN));
268 static void sun4i_codec_stop_playback(struct sun4i_codec *scodec)
270 /* Disable DAC DRQ */
271 regmap_update_bits(scodec->regmap, SUN4I_CODEC_DAC_FIFOC,
272 BIT(SUN4I_CODEC_DAC_FIFOC_DAC_DRQ_EN),
276 static void sun4i_codec_start_capture(struct sun4i_codec *scodec)
279 regmap_field_update_bits(scodec->reg_adc_fifoc,
280 BIT(SUN4I_CODEC_ADC_FIFOC_ADC_DRQ_EN),
281 BIT(SUN4I_CODEC_ADC_FIFOC_ADC_DRQ_EN));
284 static void sun4i_codec_stop_capture(struct sun4i_codec *scodec)
286 /* Disable ADC DRQ */
287 regmap_field_update_bits(scodec->reg_adc_fifoc,
288 BIT(SUN4I_CODEC_ADC_FIFOC_ADC_DRQ_EN), 0);
291 static int sun4i_codec_trigger(struct snd_pcm_substream *substream, int cmd,
292 struct snd_soc_dai *dai)
294 struct snd_soc_pcm_runtime *rtd = substream->private_data;
295 struct sun4i_codec *scodec = snd_soc_card_get_drvdata(rtd->card);
298 case SNDRV_PCM_TRIGGER_START:
299 case SNDRV_PCM_TRIGGER_RESUME:
300 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
301 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
302 sun4i_codec_start_playback(scodec);
304 sun4i_codec_start_capture(scodec);
307 case SNDRV_PCM_TRIGGER_STOP:
308 case SNDRV_PCM_TRIGGER_SUSPEND:
309 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
310 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
311 sun4i_codec_stop_playback(scodec);
313 sun4i_codec_stop_capture(scodec);
323 static int sun4i_codec_prepare_capture(struct snd_pcm_substream *substream,
324 struct snd_soc_dai *dai)
326 struct snd_soc_pcm_runtime *rtd = substream->private_data;
327 struct sun4i_codec *scodec = snd_soc_card_get_drvdata(rtd->card);
331 regmap_field_update_bits(scodec->reg_adc_fifoc,
332 BIT(SUN4I_CODEC_ADC_FIFOC_FIFO_FLUSH),
333 BIT(SUN4I_CODEC_ADC_FIFOC_FIFO_FLUSH));
336 /* Set RX FIFO trigger level */
337 regmap_field_update_bits(scodec->reg_adc_fifoc,
338 0xf << SUN4I_CODEC_ADC_FIFOC_RX_TRIG_LEVEL,
339 0x7 << SUN4I_CODEC_ADC_FIFOC_RX_TRIG_LEVEL);
342 * FIXME: Undocumented in the datasheet, but
343 * Allwinner's code mentions that it is related
344 * related to microphone gain
346 if (of_device_is_compatible(scodec->dev->of_node,
347 "allwinner,sun4i-a10-codec") ||
348 of_device_is_compatible(scodec->dev->of_node,
349 "allwinner,sun7i-a20-codec")) {
350 regmap_update_bits(scodec->regmap, SUN4I_CODEC_ADC_ACTL,
355 if (of_device_is_compatible(scodec->dev->of_node,
356 "allwinner,sun7i-a20-codec"))
357 /* FIXME: Undocumented bits */
358 regmap_update_bits(scodec->regmap, SUN4I_CODEC_DAC_TUNE,
365 static int sun4i_codec_prepare_playback(struct snd_pcm_substream *substream,
366 struct snd_soc_dai *dai)
368 struct snd_soc_pcm_runtime *rtd = substream->private_data;
369 struct sun4i_codec *scodec = snd_soc_card_get_drvdata(rtd->card);
372 /* Flush the TX FIFO */
373 regmap_update_bits(scodec->regmap, SUN4I_CODEC_DAC_FIFOC,
374 BIT(SUN4I_CODEC_DAC_FIFOC_FIFO_FLUSH),
375 BIT(SUN4I_CODEC_DAC_FIFOC_FIFO_FLUSH));
377 /* Set TX FIFO Empty Trigger Level */
378 regmap_update_bits(scodec->regmap, SUN4I_CODEC_DAC_FIFOC,
379 0x3f << SUN4I_CODEC_DAC_FIFOC_TX_TRIG_LEVEL,
380 0xf << SUN4I_CODEC_DAC_FIFOC_TX_TRIG_LEVEL);
382 if (substream->runtime->rate > 32000)
383 /* Use 64 bits FIR filter */
386 /* Use 32 bits FIR filter */
387 val = BIT(SUN4I_CODEC_DAC_FIFOC_FIR_VERSION);
389 regmap_update_bits(scodec->regmap, SUN4I_CODEC_DAC_FIFOC,
390 BIT(SUN4I_CODEC_DAC_FIFOC_FIR_VERSION),
393 /* Send zeros when we have an underrun */
394 regmap_update_bits(scodec->regmap, SUN4I_CODEC_DAC_FIFOC,
395 BIT(SUN4I_CODEC_DAC_FIFOC_SEND_LASAT),
401 static int sun4i_codec_prepare(struct snd_pcm_substream *substream,
402 struct snd_soc_dai *dai)
404 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
405 return sun4i_codec_prepare_playback(substream, dai);
407 return sun4i_codec_prepare_capture(substream, dai);
410 static unsigned long sun4i_codec_get_mod_freq(struct snd_pcm_hw_params *params)
412 unsigned int rate = params_rate(params);
440 static int sun4i_codec_get_hw_rate(struct snd_pcm_hw_params *params)
442 unsigned int rate = params_rate(params);
482 static int sun4i_codec_hw_params_capture(struct sun4i_codec *scodec,
483 struct snd_pcm_hw_params *params,
486 /* Set ADC sample rate */
487 regmap_field_update_bits(scodec->reg_adc_fifoc,
488 7 << SUN4I_CODEC_ADC_FIFOC_ADC_FS,
489 hwrate << SUN4I_CODEC_ADC_FIFOC_ADC_FS);
491 /* Set the number of channels we want to use */
492 if (params_channels(params) == 1)
493 regmap_field_update_bits(scodec->reg_adc_fifoc,
494 BIT(SUN4I_CODEC_ADC_FIFOC_MONO_EN),
495 BIT(SUN4I_CODEC_ADC_FIFOC_MONO_EN));
497 regmap_field_update_bits(scodec->reg_adc_fifoc,
498 BIT(SUN4I_CODEC_ADC_FIFOC_MONO_EN),
501 /* Set the number of sample bits to either 16 or 24 bits */
502 if (hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS)->min == 32) {
503 regmap_field_update_bits(scodec->reg_adc_fifoc,
504 BIT(SUN4I_CODEC_ADC_FIFOC_RX_SAMPLE_BITS),
505 BIT(SUN4I_CODEC_ADC_FIFOC_RX_SAMPLE_BITS));
507 regmap_field_update_bits(scodec->reg_adc_fifoc,
508 BIT(SUN4I_CODEC_ADC_FIFOC_RX_FIFO_MODE),
511 scodec->capture_dma_data.addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
513 regmap_field_update_bits(scodec->reg_adc_fifoc,
514 BIT(SUN4I_CODEC_ADC_FIFOC_RX_SAMPLE_BITS),
517 /* Fill most significant bits with valid data MSB */
518 regmap_field_update_bits(scodec->reg_adc_fifoc,
519 BIT(SUN4I_CODEC_ADC_FIFOC_RX_FIFO_MODE),
520 BIT(SUN4I_CODEC_ADC_FIFOC_RX_FIFO_MODE));
522 scodec->capture_dma_data.addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
528 static int sun4i_codec_hw_params_playback(struct sun4i_codec *scodec,
529 struct snd_pcm_hw_params *params,
534 /* Set DAC sample rate */
535 regmap_update_bits(scodec->regmap, SUN4I_CODEC_DAC_FIFOC,
536 7 << SUN4I_CODEC_DAC_FIFOC_DAC_FS,
537 hwrate << SUN4I_CODEC_DAC_FIFOC_DAC_FS);
539 /* Set the number of channels we want to use */
540 if (params_channels(params) == 1)
541 val = BIT(SUN4I_CODEC_DAC_FIFOC_MONO_EN);
545 regmap_update_bits(scodec->regmap, SUN4I_CODEC_DAC_FIFOC,
546 BIT(SUN4I_CODEC_DAC_FIFOC_MONO_EN),
549 /* Set the number of sample bits to either 16 or 24 bits */
550 if (hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS)->min == 32) {
551 regmap_update_bits(scodec->regmap, SUN4I_CODEC_DAC_FIFOC,
552 BIT(SUN4I_CODEC_DAC_FIFOC_TX_SAMPLE_BITS),
553 BIT(SUN4I_CODEC_DAC_FIFOC_TX_SAMPLE_BITS));
555 /* Set TX FIFO mode to padding the LSBs with 0 */
556 regmap_update_bits(scodec->regmap, SUN4I_CODEC_DAC_FIFOC,
557 BIT(SUN4I_CODEC_DAC_FIFOC_TX_FIFO_MODE),
560 scodec->playback_dma_data.addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
562 regmap_update_bits(scodec->regmap, SUN4I_CODEC_DAC_FIFOC,
563 BIT(SUN4I_CODEC_DAC_FIFOC_TX_SAMPLE_BITS),
566 /* Set TX FIFO mode to repeat the MSB */
567 regmap_update_bits(scodec->regmap, SUN4I_CODEC_DAC_FIFOC,
568 BIT(SUN4I_CODEC_DAC_FIFOC_TX_FIFO_MODE),
569 BIT(SUN4I_CODEC_DAC_FIFOC_TX_FIFO_MODE));
571 scodec->playback_dma_data.addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
577 static int sun4i_codec_hw_params(struct snd_pcm_substream *substream,
578 struct snd_pcm_hw_params *params,
579 struct snd_soc_dai *dai)
581 struct snd_soc_pcm_runtime *rtd = substream->private_data;
582 struct sun4i_codec *scodec = snd_soc_card_get_drvdata(rtd->card);
583 unsigned long clk_freq;
586 clk_freq = sun4i_codec_get_mod_freq(params);
590 ret = clk_set_rate(scodec->clk_module, clk_freq);
594 hwrate = sun4i_codec_get_hw_rate(params);
598 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
599 return sun4i_codec_hw_params_playback(scodec, params,
602 return sun4i_codec_hw_params_capture(scodec, params,
607 static unsigned int sun4i_codec_src_rates[] = {
608 8000, 11025, 12000, 16000, 22050, 24000, 32000,
609 44100, 48000, 96000, 192000
613 static struct snd_pcm_hw_constraint_list sun4i_codec_constraints = {
614 .count = ARRAY_SIZE(sun4i_codec_src_rates),
615 .list = sun4i_codec_src_rates,
619 static int sun4i_codec_startup(struct snd_pcm_substream *substream,
620 struct snd_soc_dai *dai)
622 struct snd_soc_pcm_runtime *rtd = substream->private_data;
623 struct sun4i_codec *scodec = snd_soc_card_get_drvdata(rtd->card);
625 snd_pcm_hw_constraint_list(substream->runtime, 0,
626 SNDRV_PCM_HW_PARAM_RATE, &sun4i_codec_constraints);
629 * Stop issuing DRQ when we have room for less than 16 samples
632 regmap_update_bits(scodec->regmap, SUN4I_CODEC_DAC_FIFOC,
633 3 << SUN4I_CODEC_DAC_FIFOC_DRQ_CLR_CNT,
634 3 << SUN4I_CODEC_DAC_FIFOC_DRQ_CLR_CNT);
636 return clk_prepare_enable(scodec->clk_module);
639 static void sun4i_codec_shutdown(struct snd_pcm_substream *substream,
640 struct snd_soc_dai *dai)
642 struct snd_soc_pcm_runtime *rtd = substream->private_data;
643 struct sun4i_codec *scodec = snd_soc_card_get_drvdata(rtd->card);
645 clk_disable_unprepare(scodec->clk_module);
648 static const struct snd_soc_dai_ops sun4i_codec_dai_ops = {
649 .startup = sun4i_codec_startup,
650 .shutdown = sun4i_codec_shutdown,
651 .trigger = sun4i_codec_trigger,
652 .hw_params = sun4i_codec_hw_params,
653 .prepare = sun4i_codec_prepare,
656 static struct snd_soc_dai_driver sun4i_codec_dai = {
658 .ops = &sun4i_codec_dai_ops,
660 .stream_name = "Codec Playback",
665 .rates = SNDRV_PCM_RATE_CONTINUOUS,
666 .formats = SNDRV_PCM_FMTBIT_S16_LE |
667 SNDRV_PCM_FMTBIT_S32_LE,
671 .stream_name = "Codec Capture",
676 .rates = SNDRV_PCM_RATE_CONTINUOUS,
677 .formats = SNDRV_PCM_FMTBIT_S16_LE |
678 SNDRV_PCM_FMTBIT_S32_LE,
683 /*** sun4i Codec ***/
684 static const struct snd_kcontrol_new sun4i_codec_pa_mute =
685 SOC_DAPM_SINGLE("Switch", SUN4I_CODEC_DAC_ACTL,
686 SUN4I_CODEC_DAC_ACTL_PA_MUTE, 1, 0);
688 static DECLARE_TLV_DB_SCALE(sun4i_codec_pa_volume_scale, -6300, 100, 1);
689 static DECLARE_TLV_DB_SCALE(sun4i_codec_fmin_loopback_gain_scale, -450, 150,
691 static DECLARE_TLV_DB_SCALE(sun4i_codec_micin_loopback_gain_scale, -450, 150,
693 static DECLARE_TLV_DB_RANGE(sun4i_codec_micin_preamp_gain_scale,
694 0, 0, TLV_DB_SCALE_ITEM(0, 0, 0),
695 1, 7, TLV_DB_SCALE_ITEM(3500, 300, 0));
696 static DECLARE_TLV_DB_RANGE(sun7i_codec_micin_preamp_gain_scale,
697 0, 0, TLV_DB_SCALE_ITEM(0, 0, 0),
698 1, 7, TLV_DB_SCALE_ITEM(2400, 300, 0));
700 static const struct snd_kcontrol_new sun4i_codec_controls[] = {
701 SOC_SINGLE_TLV("Power Amplifier Volume", SUN4I_CODEC_DAC_ACTL,
702 SUN4I_CODEC_DAC_ACTL_PA_VOL, 0x3F, 0,
703 sun4i_codec_pa_volume_scale),
704 SOC_SINGLE_TLV("FM Playback Volume", SUN4I_CODEC_DAC_ACTL,
705 SUN4I_CODEC_DAC_ACTL_FMG, 3, 0,
706 sun4i_codec_fmin_loopback_gain_scale),
707 SOC_SINGLE_TLV("Mic Playback Volume", SUN4I_CODEC_DAC_ACTL,
708 SUN4I_CODEC_DAC_ACTL_MICG, 7, 0,
709 sun4i_codec_micin_loopback_gain_scale),
710 SOC_SINGLE_TLV("Mic1 Boost Volume", SUN4I_CODEC_ADC_ACTL,
711 SUN4I_CODEC_ADC_ACTL_PREG1, 3, 0,
712 sun4i_codec_micin_preamp_gain_scale),
713 SOC_SINGLE_TLV("Mic2 Boost Volume", SUN4I_CODEC_ADC_ACTL,
714 SUN4I_CODEC_ADC_ACTL_PREG2, 3, 0,
715 sun4i_codec_micin_preamp_gain_scale),
718 static const struct snd_kcontrol_new sun7i_codec_controls[] = {
719 SOC_SINGLE_TLV("Power Amplifier Volume", SUN4I_CODEC_DAC_ACTL,
720 SUN4I_CODEC_DAC_ACTL_PA_VOL, 0x3F, 0,
721 sun4i_codec_pa_volume_scale),
722 SOC_SINGLE_TLV("FM Playback Volume", SUN4I_CODEC_DAC_ACTL,
723 SUN4I_CODEC_DAC_ACTL_FMG, 3, 0,
724 sun4i_codec_fmin_loopback_gain_scale),
725 SOC_SINGLE_TLV("Mic Playback Volume", SUN4I_CODEC_DAC_ACTL,
726 SUN4I_CODEC_DAC_ACTL_MICG, 7, 0,
727 sun4i_codec_micin_loopback_gain_scale),
728 SOC_SINGLE_TLV("Mic1 Boost Volume", SUN7I_CODEC_AC_MIC_PHONE_CAL,
729 SUN7I_CODEC_AC_MIC_PHONE_CAL_PREG1, 7, 0,
730 sun7i_codec_micin_preamp_gain_scale),
731 SOC_SINGLE_TLV("Mic2 Boost Volume", SUN7I_CODEC_AC_MIC_PHONE_CAL,
732 SUN7I_CODEC_AC_MIC_PHONE_CAL_PREG2, 7, 0,
733 sun7i_codec_micin_preamp_gain_scale),
736 static const struct snd_kcontrol_new sun4i_codec_mixer_controls[] = {
737 SOC_DAPM_SINGLE("Left Mixer Left DAC Playback Switch",
738 SUN4I_CODEC_DAC_ACTL, SUN4I_CODEC_DAC_ACTL_LDACLMIXS,
740 SOC_DAPM_SINGLE("Right Mixer Right DAC Playback Switch",
741 SUN4I_CODEC_DAC_ACTL, SUN4I_CODEC_DAC_ACTL_RDACRMIXS,
743 SOC_DAPM_SINGLE("Right Mixer Left DAC Playback Switch",
744 SUN4I_CODEC_DAC_ACTL,
745 SUN4I_CODEC_DAC_ACTL_LDACRMIXS, 1, 0),
746 SOC_DAPM_DOUBLE("FM Playback Switch", SUN4I_CODEC_DAC_ACTL,
747 SUN4I_CODEC_DAC_ACTL_LFMS,
748 SUN4I_CODEC_DAC_ACTL_RFMS, 1, 0),
749 SOC_DAPM_DOUBLE("Mic1 Playback Switch", SUN4I_CODEC_DAC_ACTL,
750 SUN4I_CODEC_DAC_ACTL_MIC1LS,
751 SUN4I_CODEC_DAC_ACTL_MIC1RS, 1, 0),
752 SOC_DAPM_DOUBLE("Mic2 Playback Switch", SUN4I_CODEC_DAC_ACTL,
753 SUN4I_CODEC_DAC_ACTL_MIC2LS,
754 SUN4I_CODEC_DAC_ACTL_MIC2RS, 1, 0),
757 static const struct snd_kcontrol_new sun4i_codec_pa_mixer_controls[] = {
758 SOC_DAPM_SINGLE("DAC Playback Switch", SUN4I_CODEC_DAC_ACTL,
759 SUN4I_CODEC_DAC_ACTL_DACPAS, 1, 0),
760 SOC_DAPM_SINGLE("Mixer Playback Switch", SUN4I_CODEC_DAC_ACTL,
761 SUN4I_CODEC_DAC_ACTL_MIXPAS, 1, 0),
764 static const struct snd_soc_dapm_widget sun4i_codec_codec_dapm_widgets[] = {
765 /* Digital parts of the ADCs */
766 SND_SOC_DAPM_SUPPLY("ADC", SUN4I_CODEC_ADC_FIFOC,
767 SUN4I_CODEC_ADC_FIFOC_EN_AD, 0,
770 /* Digital parts of the DACs */
771 SND_SOC_DAPM_SUPPLY("DAC", SUN4I_CODEC_DAC_DPC,
772 SUN4I_CODEC_DAC_DPC_EN_DA, 0,
775 /* Analog parts of the ADCs */
776 SND_SOC_DAPM_ADC("Left ADC", "Codec Capture", SUN4I_CODEC_ADC_ACTL,
777 SUN4I_CODEC_ADC_ACTL_ADC_L_EN, 0),
778 SND_SOC_DAPM_ADC("Right ADC", "Codec Capture", SUN4I_CODEC_ADC_ACTL,
779 SUN4I_CODEC_ADC_ACTL_ADC_R_EN, 0),
781 /* Analog parts of the DACs */
782 SND_SOC_DAPM_DAC("Left DAC", "Codec Playback", SUN4I_CODEC_DAC_ACTL,
783 SUN4I_CODEC_DAC_ACTL_DACAENL, 0),
784 SND_SOC_DAPM_DAC("Right DAC", "Codec Playback", SUN4I_CODEC_DAC_ACTL,
785 SUN4I_CODEC_DAC_ACTL_DACAENR, 0),
788 SND_SOC_DAPM_MIXER("Left Mixer", SND_SOC_NOPM, 0, 0,
789 sun4i_codec_mixer_controls,
790 ARRAY_SIZE(sun4i_codec_mixer_controls)),
791 SND_SOC_DAPM_MIXER("Right Mixer", SND_SOC_NOPM, 0, 0,
792 sun4i_codec_mixer_controls,
793 ARRAY_SIZE(sun4i_codec_mixer_controls)),
795 /* Global Mixer Enable */
796 SND_SOC_DAPM_SUPPLY("Mixer Enable", SUN4I_CODEC_DAC_ACTL,
797 SUN4I_CODEC_DAC_ACTL_MIXEN, 0, NULL, 0),
800 SND_SOC_DAPM_SUPPLY("VMIC", SUN4I_CODEC_ADC_ACTL,
801 SUN4I_CODEC_ADC_ACTL_VMICEN, 0, NULL, 0),
803 /* Mic Pre-Amplifiers */
804 SND_SOC_DAPM_PGA("MIC1 Pre-Amplifier", SUN4I_CODEC_ADC_ACTL,
805 SUN4I_CODEC_ADC_ACTL_PREG1EN, 0, NULL, 0),
806 SND_SOC_DAPM_PGA("MIC2 Pre-Amplifier", SUN4I_CODEC_ADC_ACTL,
807 SUN4I_CODEC_ADC_ACTL_PREG2EN, 0, NULL, 0),
809 /* Power Amplifier */
810 SND_SOC_DAPM_MIXER("Power Amplifier", SUN4I_CODEC_ADC_ACTL,
811 SUN4I_CODEC_ADC_ACTL_PA_EN, 0,
812 sun4i_codec_pa_mixer_controls,
813 ARRAY_SIZE(sun4i_codec_pa_mixer_controls)),
814 SND_SOC_DAPM_SWITCH("Power Amplifier Mute", SND_SOC_NOPM, 0, 0,
815 &sun4i_codec_pa_mute),
817 SND_SOC_DAPM_INPUT("FM Right"),
818 SND_SOC_DAPM_INPUT("FM Left"),
819 SND_SOC_DAPM_INPUT("Mic1"),
820 SND_SOC_DAPM_INPUT("Mic2"),
822 SND_SOC_DAPM_OUTPUT("HP Right"),
823 SND_SOC_DAPM_OUTPUT("HP Left"),
826 static const struct snd_soc_dapm_route sun4i_codec_codec_dapm_routes[] = {
827 /* Left ADC / DAC Routes */
828 { "Left ADC", NULL, "ADC" },
829 { "Left DAC", NULL, "DAC" },
831 /* Right ADC / DAC Routes */
832 { "Right ADC", NULL, "ADC" },
833 { "Right DAC", NULL, "DAC" },
835 /* Right Mixer Routes */
836 { "Right Mixer", NULL, "Mixer Enable" },
837 { "Right Mixer", "Right Mixer Left DAC Playback Switch", "Left DAC" },
838 { "Right Mixer", "Right Mixer Right DAC Playback Switch", "Right DAC" },
839 { "Right Mixer", "FM Playback Switch", "FM Right" },
840 { "Right Mixer", "Mic1 Playback Switch", "MIC1 Pre-Amplifier" },
841 { "Right Mixer", "Mic2 Playback Switch", "MIC2 Pre-Amplifier" },
843 /* Left Mixer Routes */
844 { "Left Mixer", NULL, "Mixer Enable" },
845 { "Left Mixer", "Left Mixer Left DAC Playback Switch", "Left DAC" },
846 { "Left Mixer", "FM Playback Switch", "FM Left" },
847 { "Left Mixer", "Mic1 Playback Switch", "MIC1 Pre-Amplifier" },
848 { "Left Mixer", "Mic2 Playback Switch", "MIC2 Pre-Amplifier" },
850 /* Power Amplifier Routes */
851 { "Power Amplifier", "Mixer Playback Switch", "Left Mixer" },
852 { "Power Amplifier", "Mixer Playback Switch", "Right Mixer" },
853 { "Power Amplifier", "DAC Playback Switch", "Left DAC" },
854 { "Power Amplifier", "DAC Playback Switch", "Right DAC" },
856 /* Headphone Output Routes */
857 { "Power Amplifier Mute", "Switch", "Power Amplifier" },
858 { "HP Right", NULL, "Power Amplifier Mute" },
859 { "HP Left", NULL, "Power Amplifier Mute" },
862 { "Left ADC", NULL, "MIC1 Pre-Amplifier" },
863 { "Right ADC", NULL, "MIC1 Pre-Amplifier" },
864 { "MIC1 Pre-Amplifier", NULL, "Mic1"},
865 { "Mic1", NULL, "VMIC" },
868 { "Left ADC", NULL, "MIC2 Pre-Amplifier" },
869 { "Right ADC", NULL, "MIC2 Pre-Amplifier" },
870 { "MIC2 Pre-Amplifier", NULL, "Mic2"},
871 { "Mic2", NULL, "VMIC" },
874 static const struct snd_soc_component_driver sun4i_codec_codec = {
875 .controls = sun4i_codec_controls,
876 .num_controls = ARRAY_SIZE(sun4i_codec_controls),
877 .dapm_widgets = sun4i_codec_codec_dapm_widgets,
878 .num_dapm_widgets = ARRAY_SIZE(sun4i_codec_codec_dapm_widgets),
879 .dapm_routes = sun4i_codec_codec_dapm_routes,
880 .num_dapm_routes = ARRAY_SIZE(sun4i_codec_codec_dapm_routes),
882 .use_pmdown_time = 1,
884 .non_legacy_dai_naming = 1,
887 static const struct snd_soc_component_driver sun7i_codec_codec = {
888 .controls = sun7i_codec_controls,
889 .num_controls = ARRAY_SIZE(sun7i_codec_controls),
890 .dapm_widgets = sun4i_codec_codec_dapm_widgets,
891 .num_dapm_widgets = ARRAY_SIZE(sun4i_codec_codec_dapm_widgets),
892 .dapm_routes = sun4i_codec_codec_dapm_routes,
893 .num_dapm_routes = ARRAY_SIZE(sun4i_codec_codec_dapm_routes),
895 .use_pmdown_time = 1,
897 .non_legacy_dai_naming = 1,
900 /*** sun6i Codec ***/
903 static const struct snd_kcontrol_new sun6i_codec_mixer_controls[] = {
904 SOC_DAPM_DOUBLE("DAC Playback Switch",
905 SUN6I_CODEC_OM_DACA_CTRL,
906 SUN6I_CODEC_OM_DACA_CTRL_LMIX_DACL,
907 SUN6I_CODEC_OM_DACA_CTRL_RMIX_DACR, 1, 0),
908 SOC_DAPM_DOUBLE("DAC Reversed Playback Switch",
909 SUN6I_CODEC_OM_DACA_CTRL,
910 SUN6I_CODEC_OM_DACA_CTRL_LMIX_DACR,
911 SUN6I_CODEC_OM_DACA_CTRL_RMIX_DACL, 1, 0),
912 SOC_DAPM_DOUBLE("Line In Playback Switch",
913 SUN6I_CODEC_OM_DACA_CTRL,
914 SUN6I_CODEC_OM_DACA_CTRL_LMIX_LINEINL,
915 SUN6I_CODEC_OM_DACA_CTRL_RMIX_LINEINR, 1, 0),
916 SOC_DAPM_DOUBLE("Mic1 Playback Switch",
917 SUN6I_CODEC_OM_DACA_CTRL,
918 SUN6I_CODEC_OM_DACA_CTRL_LMIX_MIC1,
919 SUN6I_CODEC_OM_DACA_CTRL_RMIX_MIC1, 1, 0),
920 SOC_DAPM_DOUBLE("Mic2 Playback Switch",
921 SUN6I_CODEC_OM_DACA_CTRL,
922 SUN6I_CODEC_OM_DACA_CTRL_LMIX_MIC2,
923 SUN6I_CODEC_OM_DACA_CTRL_RMIX_MIC2, 1, 0),
926 /* ADC mixer controls */
927 static const struct snd_kcontrol_new sun6i_codec_adc_mixer_controls[] = {
928 SOC_DAPM_DOUBLE("Mixer Capture Switch",
929 SUN6I_CODEC_ADC_ACTL,
930 SUN6I_CODEC_ADC_ACTL_LADCMIX_OMIXL,
931 SUN6I_CODEC_ADC_ACTL_RADCMIX_OMIXR, 1, 0),
932 SOC_DAPM_DOUBLE("Mixer Reversed Capture Switch",
933 SUN6I_CODEC_ADC_ACTL,
934 SUN6I_CODEC_ADC_ACTL_LADCMIX_OMIXR,
935 SUN6I_CODEC_ADC_ACTL_RADCMIX_OMIXL, 1, 0),
936 SOC_DAPM_DOUBLE("Line In Capture Switch",
937 SUN6I_CODEC_ADC_ACTL,
938 SUN6I_CODEC_ADC_ACTL_LADCMIX_LINEINL,
939 SUN6I_CODEC_ADC_ACTL_RADCMIX_LINEINR, 1, 0),
940 SOC_DAPM_DOUBLE("Mic1 Capture Switch",
941 SUN6I_CODEC_ADC_ACTL,
942 SUN6I_CODEC_ADC_ACTL_LADCMIX_MIC1,
943 SUN6I_CODEC_ADC_ACTL_RADCMIX_MIC1, 1, 0),
944 SOC_DAPM_DOUBLE("Mic2 Capture Switch",
945 SUN6I_CODEC_ADC_ACTL,
946 SUN6I_CODEC_ADC_ACTL_LADCMIX_MIC2,
947 SUN6I_CODEC_ADC_ACTL_RADCMIX_MIC2, 1, 0),
950 /* headphone controls */
951 static const char * const sun6i_codec_hp_src_enum_text[] = {
955 static SOC_ENUM_DOUBLE_DECL(sun6i_codec_hp_src_enum,
956 SUN6I_CODEC_OM_DACA_CTRL,
957 SUN6I_CODEC_OM_DACA_CTRL_LHPIS,
958 SUN6I_CODEC_OM_DACA_CTRL_RHPIS,
959 sun6i_codec_hp_src_enum_text);
961 static const struct snd_kcontrol_new sun6i_codec_hp_src[] = {
962 SOC_DAPM_ENUM("Headphone Source Playback Route",
963 sun6i_codec_hp_src_enum),
966 /* microphone controls */
967 static const char * const sun6i_codec_mic2_src_enum_text[] = {
971 static SOC_ENUM_SINGLE_DECL(sun6i_codec_mic2_src_enum,
972 SUN6I_CODEC_MIC_CTRL,
973 SUN6I_CODEC_MIC_CTRL_MIC2SLT,
974 sun6i_codec_mic2_src_enum_text);
976 static const struct snd_kcontrol_new sun6i_codec_mic2_src[] = {
977 SOC_DAPM_ENUM("Mic2 Amplifier Source Route",
978 sun6i_codec_mic2_src_enum),
981 /* line out controls */
982 static const char * const sun6i_codec_lineout_src_enum_text[] = {
983 "Stereo", "Mono Differential",
986 static SOC_ENUM_DOUBLE_DECL(sun6i_codec_lineout_src_enum,
987 SUN6I_CODEC_MIC_CTRL,
988 SUN6I_CODEC_MIC_CTRL_LINEOUTLSRC,
989 SUN6I_CODEC_MIC_CTRL_LINEOUTRSRC,
990 sun6i_codec_lineout_src_enum_text);
992 static const struct snd_kcontrol_new sun6i_codec_lineout_src[] = {
993 SOC_DAPM_ENUM("Line Out Source Playback Route",
994 sun6i_codec_lineout_src_enum),
997 /* volume / mute controls */
998 static const DECLARE_TLV_DB_SCALE(sun6i_codec_dvol_scale, -7308, 116, 0);
999 static const DECLARE_TLV_DB_SCALE(sun6i_codec_hp_vol_scale, -6300, 100, 1);
1000 static const DECLARE_TLV_DB_SCALE(sun6i_codec_out_mixer_pregain_scale,
1002 static const DECLARE_TLV_DB_RANGE(sun6i_codec_lineout_vol_scale,
1003 0, 1, TLV_DB_SCALE_ITEM(TLV_DB_GAIN_MUTE, 0, 1),
1004 2, 31, TLV_DB_SCALE_ITEM(-4350, 150, 0),
1006 static const DECLARE_TLV_DB_RANGE(sun6i_codec_mic_gain_scale,
1007 0, 0, TLV_DB_SCALE_ITEM(0, 0, 0),
1008 1, 7, TLV_DB_SCALE_ITEM(2400, 300, 0),
1011 static const struct snd_kcontrol_new sun6i_codec_codec_widgets[] = {
1012 SOC_SINGLE_TLV("DAC Playback Volume", SUN4I_CODEC_DAC_DPC,
1013 SUN4I_CODEC_DAC_DPC_DVOL, 0x3f, 1,
1014 sun6i_codec_dvol_scale),
1015 SOC_SINGLE_TLV("Headphone Playback Volume",
1016 SUN6I_CODEC_OM_DACA_CTRL,
1017 SUN6I_CODEC_OM_DACA_CTRL_HPVOL, 0x3f, 0,
1018 sun6i_codec_hp_vol_scale),
1019 SOC_SINGLE_TLV("Line Out Playback Volume",
1020 SUN6I_CODEC_MIC_CTRL,
1021 SUN6I_CODEC_MIC_CTRL_LINEOUTVC, 0x1f, 0,
1022 sun6i_codec_lineout_vol_scale),
1023 SOC_DOUBLE("Headphone Playback Switch",
1024 SUN6I_CODEC_OM_DACA_CTRL,
1025 SUN6I_CODEC_OM_DACA_CTRL_LHPPAMUTE,
1026 SUN6I_CODEC_OM_DACA_CTRL_RHPPAMUTE, 1, 0),
1027 SOC_DOUBLE("Line Out Playback Switch",
1028 SUN6I_CODEC_MIC_CTRL,
1029 SUN6I_CODEC_MIC_CTRL_LINEOUTLEN,
1030 SUN6I_CODEC_MIC_CTRL_LINEOUTREN, 1, 0),
1031 /* Mixer pre-gains */
1032 SOC_SINGLE_TLV("Line In Playback Volume",
1033 SUN6I_CODEC_OM_PA_CTRL, SUN6I_CODEC_OM_PA_CTRL_LINEING,
1034 0x7, 0, sun6i_codec_out_mixer_pregain_scale),
1035 SOC_SINGLE_TLV("Mic1 Playback Volume",
1036 SUN6I_CODEC_OM_PA_CTRL, SUN6I_CODEC_OM_PA_CTRL_MIC1G,
1037 0x7, 0, sun6i_codec_out_mixer_pregain_scale),
1038 SOC_SINGLE_TLV("Mic2 Playback Volume",
1039 SUN6I_CODEC_OM_PA_CTRL, SUN6I_CODEC_OM_PA_CTRL_MIC2G,
1040 0x7, 0, sun6i_codec_out_mixer_pregain_scale),
1042 /* Microphone Amp boost gains */
1043 SOC_SINGLE_TLV("Mic1 Boost Volume", SUN6I_CODEC_MIC_CTRL,
1044 SUN6I_CODEC_MIC_CTRL_MIC1BOOST, 0x7, 0,
1045 sun6i_codec_mic_gain_scale),
1046 SOC_SINGLE_TLV("Mic2 Boost Volume", SUN6I_CODEC_MIC_CTRL,
1047 SUN6I_CODEC_MIC_CTRL_MIC2BOOST, 0x7, 0,
1048 sun6i_codec_mic_gain_scale),
1049 SOC_DOUBLE_TLV("ADC Capture Volume",
1050 SUN6I_CODEC_ADC_ACTL, SUN6I_CODEC_ADC_ACTL_ADCLG,
1051 SUN6I_CODEC_ADC_ACTL_ADCRG, 0x7, 0,
1052 sun6i_codec_out_mixer_pregain_scale),
1055 static const struct snd_soc_dapm_widget sun6i_codec_codec_dapm_widgets[] = {
1056 /* Microphone inputs */
1057 SND_SOC_DAPM_INPUT("MIC1"),
1058 SND_SOC_DAPM_INPUT("MIC2"),
1059 SND_SOC_DAPM_INPUT("MIC3"),
1061 /* Microphone Bias */
1062 SND_SOC_DAPM_SUPPLY("HBIAS", SUN6I_CODEC_MIC_CTRL,
1063 SUN6I_CODEC_MIC_CTRL_HBIASEN, 0, NULL, 0),
1064 SND_SOC_DAPM_SUPPLY("MBIAS", SUN6I_CODEC_MIC_CTRL,
1065 SUN6I_CODEC_MIC_CTRL_MBIASEN, 0, NULL, 0),
1067 /* Mic input path */
1068 SND_SOC_DAPM_MUX("Mic2 Amplifier Source Route",
1069 SND_SOC_NOPM, 0, 0, sun6i_codec_mic2_src),
1070 SND_SOC_DAPM_PGA("Mic1 Amplifier", SUN6I_CODEC_MIC_CTRL,
1071 SUN6I_CODEC_MIC_CTRL_MIC1AMPEN, 0, NULL, 0),
1072 SND_SOC_DAPM_PGA("Mic2 Amplifier", SUN6I_CODEC_MIC_CTRL,
1073 SUN6I_CODEC_MIC_CTRL_MIC2AMPEN, 0, NULL, 0),
1076 SND_SOC_DAPM_INPUT("LINEIN"),
1078 /* Digital parts of the ADCs */
1079 SND_SOC_DAPM_SUPPLY("ADC Enable", SUN6I_CODEC_ADC_FIFOC,
1080 SUN6I_CODEC_ADC_FIFOC_EN_AD, 0,
1083 /* Analog parts of the ADCs */
1084 SND_SOC_DAPM_ADC("Left ADC", "Codec Capture", SUN6I_CODEC_ADC_ACTL,
1085 SUN6I_CODEC_ADC_ACTL_ADCLEN, 0),
1086 SND_SOC_DAPM_ADC("Right ADC", "Codec Capture", SUN6I_CODEC_ADC_ACTL,
1087 SUN6I_CODEC_ADC_ACTL_ADCREN, 0),
1090 SOC_MIXER_ARRAY("Left ADC Mixer", SND_SOC_NOPM, 0, 0,
1091 sun6i_codec_adc_mixer_controls),
1092 SOC_MIXER_ARRAY("Right ADC Mixer", SND_SOC_NOPM, 0, 0,
1093 sun6i_codec_adc_mixer_controls),
1095 /* Digital parts of the DACs */
1096 SND_SOC_DAPM_SUPPLY("DAC Enable", SUN4I_CODEC_DAC_DPC,
1097 SUN4I_CODEC_DAC_DPC_EN_DA, 0,
1100 /* Analog parts of the DACs */
1101 SND_SOC_DAPM_DAC("Left DAC", "Codec Playback",
1102 SUN6I_CODEC_OM_DACA_CTRL,
1103 SUN6I_CODEC_OM_DACA_CTRL_DACALEN, 0),
1104 SND_SOC_DAPM_DAC("Right DAC", "Codec Playback",
1105 SUN6I_CODEC_OM_DACA_CTRL,
1106 SUN6I_CODEC_OM_DACA_CTRL_DACAREN, 0),
1109 SOC_MIXER_ARRAY("Left Mixer", SUN6I_CODEC_OM_DACA_CTRL,
1110 SUN6I_CODEC_OM_DACA_CTRL_LMIXEN, 0,
1111 sun6i_codec_mixer_controls),
1112 SOC_MIXER_ARRAY("Right Mixer", SUN6I_CODEC_OM_DACA_CTRL,
1113 SUN6I_CODEC_OM_DACA_CTRL_RMIXEN, 0,
1114 sun6i_codec_mixer_controls),
1116 /* Headphone output path */
1117 SND_SOC_DAPM_MUX("Headphone Source Playback Route",
1118 SND_SOC_NOPM, 0, 0, sun6i_codec_hp_src),
1119 SND_SOC_DAPM_OUT_DRV("Headphone Amp", SUN6I_CODEC_OM_PA_CTRL,
1120 SUN6I_CODEC_OM_PA_CTRL_HPPAEN, 0, NULL, 0),
1121 SND_SOC_DAPM_SUPPLY("HPCOM Protection", SUN6I_CODEC_OM_PA_CTRL,
1122 SUN6I_CODEC_OM_PA_CTRL_COMPTEN, 0, NULL, 0),
1123 SND_SOC_DAPM_REG(snd_soc_dapm_supply, "HPCOM", SUN6I_CODEC_OM_PA_CTRL,
1124 SUN6I_CODEC_OM_PA_CTRL_HPCOM_CTL, 0x3, 0x3, 0),
1125 SND_SOC_DAPM_OUTPUT("HP"),
1128 SND_SOC_DAPM_MUX("Line Out Source Playback Route",
1129 SND_SOC_NOPM, 0, 0, sun6i_codec_lineout_src),
1130 SND_SOC_DAPM_OUTPUT("LINEOUT"),
1133 static const struct snd_soc_dapm_route sun6i_codec_codec_dapm_routes[] = {
1135 { "Left DAC", NULL, "DAC Enable" },
1136 { "Right DAC", NULL, "DAC Enable" },
1138 /* Microphone Routes */
1139 { "Mic1 Amplifier", NULL, "MIC1"},
1140 { "Mic2 Amplifier Source Route", "Mic2", "MIC2" },
1141 { "Mic2 Amplifier Source Route", "Mic3", "MIC3" },
1142 { "Mic2 Amplifier", NULL, "Mic2 Amplifier Source Route"},
1144 /* Left Mixer Routes */
1145 { "Left Mixer", "DAC Playback Switch", "Left DAC" },
1146 { "Left Mixer", "DAC Reversed Playback Switch", "Right DAC" },
1147 { "Left Mixer", "Line In Playback Switch", "LINEIN" },
1148 { "Left Mixer", "Mic1 Playback Switch", "Mic1 Amplifier" },
1149 { "Left Mixer", "Mic2 Playback Switch", "Mic2 Amplifier" },
1151 /* Right Mixer Routes */
1152 { "Right Mixer", "DAC Playback Switch", "Right DAC" },
1153 { "Right Mixer", "DAC Reversed Playback Switch", "Left DAC" },
1154 { "Right Mixer", "Line In Playback Switch", "LINEIN" },
1155 { "Right Mixer", "Mic1 Playback Switch", "Mic1 Amplifier" },
1156 { "Right Mixer", "Mic2 Playback Switch", "Mic2 Amplifier" },
1158 /* Left ADC Mixer Routes */
1159 { "Left ADC Mixer", "Mixer Capture Switch", "Left Mixer" },
1160 { "Left ADC Mixer", "Mixer Reversed Capture Switch", "Right Mixer" },
1161 { "Left ADC Mixer", "Line In Capture Switch", "LINEIN" },
1162 { "Left ADC Mixer", "Mic1 Capture Switch", "Mic1 Amplifier" },
1163 { "Left ADC Mixer", "Mic2 Capture Switch", "Mic2 Amplifier" },
1165 /* Right ADC Mixer Routes */
1166 { "Right ADC Mixer", "Mixer Capture Switch", "Right Mixer" },
1167 { "Right ADC Mixer", "Mixer Reversed Capture Switch", "Left Mixer" },
1168 { "Right ADC Mixer", "Line In Capture Switch", "LINEIN" },
1169 { "Right ADC Mixer", "Mic1 Capture Switch", "Mic1 Amplifier" },
1170 { "Right ADC Mixer", "Mic2 Capture Switch", "Mic2 Amplifier" },
1172 /* Headphone Routes */
1173 { "Headphone Source Playback Route", "DAC", "Left DAC" },
1174 { "Headphone Source Playback Route", "DAC", "Right DAC" },
1175 { "Headphone Source Playback Route", "Mixer", "Left Mixer" },
1176 { "Headphone Source Playback Route", "Mixer", "Right Mixer" },
1177 { "Headphone Amp", NULL, "Headphone Source Playback Route" },
1178 { "HP", NULL, "Headphone Amp" },
1179 { "HPCOM", NULL, "HPCOM Protection" },
1181 /* Line Out Routes */
1182 { "Line Out Source Playback Route", "Stereo", "Left Mixer" },
1183 { "Line Out Source Playback Route", "Stereo", "Right Mixer" },
1184 { "Line Out Source Playback Route", "Mono Differential", "Left Mixer" },
1185 { "Line Out Source Playback Route", "Mono Differential", "Right Mixer" },
1186 { "LINEOUT", NULL, "Line Out Source Playback Route" },
1189 { "Left ADC", NULL, "ADC Enable" },
1190 { "Right ADC", NULL, "ADC Enable" },
1191 { "Left ADC", NULL, "Left ADC Mixer" },
1192 { "Right ADC", NULL, "Right ADC Mixer" },
1195 static const struct snd_soc_component_driver sun6i_codec_codec = {
1196 .controls = sun6i_codec_codec_widgets,
1197 .num_controls = ARRAY_SIZE(sun6i_codec_codec_widgets),
1198 .dapm_widgets = sun6i_codec_codec_dapm_widgets,
1199 .num_dapm_widgets = ARRAY_SIZE(sun6i_codec_codec_dapm_widgets),
1200 .dapm_routes = sun6i_codec_codec_dapm_routes,
1201 .num_dapm_routes = ARRAY_SIZE(sun6i_codec_codec_dapm_routes),
1203 .use_pmdown_time = 1,
1205 .non_legacy_dai_naming = 1,
1208 /* sun8i A23 codec */
1209 static const struct snd_kcontrol_new sun8i_a23_codec_codec_controls[] = {
1210 SOC_SINGLE_TLV("DAC Playback Volume", SUN4I_CODEC_DAC_DPC,
1211 SUN4I_CODEC_DAC_DPC_DVOL, 0x3f, 1,
1212 sun6i_codec_dvol_scale),
1215 static const struct snd_soc_dapm_widget sun8i_a23_codec_codec_widgets[] = {
1216 /* Digital parts of the ADCs */
1217 SND_SOC_DAPM_SUPPLY("ADC Enable", SUN6I_CODEC_ADC_FIFOC,
1218 SUN6I_CODEC_ADC_FIFOC_EN_AD, 0, NULL, 0),
1219 /* Digital parts of the DACs */
1220 SND_SOC_DAPM_SUPPLY("DAC Enable", SUN4I_CODEC_DAC_DPC,
1221 SUN4I_CODEC_DAC_DPC_EN_DA, 0, NULL, 0),
1225 static const struct snd_soc_component_driver sun8i_a23_codec_codec = {
1226 .controls = sun8i_a23_codec_codec_controls,
1227 .num_controls = ARRAY_SIZE(sun8i_a23_codec_codec_controls),
1228 .dapm_widgets = sun8i_a23_codec_codec_widgets,
1229 .num_dapm_widgets = ARRAY_SIZE(sun8i_a23_codec_codec_widgets),
1231 .use_pmdown_time = 1,
1233 .non_legacy_dai_naming = 1,
1236 static const struct snd_soc_component_driver sun4i_codec_component = {
1237 .name = "sun4i-codec",
1240 #define SUN4I_CODEC_RATES SNDRV_PCM_RATE_CONTINUOUS
1241 #define SUN4I_CODEC_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | \
1242 SNDRV_PCM_FMTBIT_S32_LE)
1244 static int sun4i_codec_dai_probe(struct snd_soc_dai *dai)
1246 struct snd_soc_card *card = snd_soc_dai_get_drvdata(dai);
1247 struct sun4i_codec *scodec = snd_soc_card_get_drvdata(card);
1249 snd_soc_dai_init_dma_data(dai, &scodec->playback_dma_data,
1250 &scodec->capture_dma_data);
1255 static struct snd_soc_dai_driver dummy_cpu_dai = {
1256 .name = "sun4i-codec-cpu-dai",
1257 .probe = sun4i_codec_dai_probe,
1259 .stream_name = "Playback",
1262 .rates = SUN4I_CODEC_RATES,
1263 .formats = SUN4I_CODEC_FORMATS,
1267 .stream_name = "Capture",
1270 .rates = SUN4I_CODEC_RATES,
1271 .formats = SUN4I_CODEC_FORMATS,
1276 static struct snd_soc_dai_link *sun4i_codec_create_link(struct device *dev,
1279 struct snd_soc_dai_link *link = devm_kzalloc(dev, sizeof(*link),
1285 link->stream_name = "CDC PCM";
1286 link->codec_dai_name = "Codec";
1287 link->cpu_dai_name = dev_name(dev);
1288 link->codec_name = dev_name(dev);
1289 link->platform_name = dev_name(dev);
1290 link->dai_fmt = SND_SOC_DAIFMT_I2S;
1297 static int sun4i_codec_spk_event(struct snd_soc_dapm_widget *w,
1298 struct snd_kcontrol *k, int event)
1300 struct sun4i_codec *scodec = snd_soc_card_get_drvdata(w->dapm->card);
1302 gpiod_set_value_cansleep(scodec->gpio_pa,
1303 !!SND_SOC_DAPM_EVENT_ON(event));
1308 static const struct snd_soc_dapm_widget sun4i_codec_card_dapm_widgets[] = {
1309 SND_SOC_DAPM_SPK("Speaker", sun4i_codec_spk_event),
1312 static const struct snd_soc_dapm_route sun4i_codec_card_dapm_routes[] = {
1313 { "Speaker", NULL, "HP Right" },
1314 { "Speaker", NULL, "HP Left" },
1317 static struct snd_soc_card *sun4i_codec_create_card(struct device *dev)
1319 struct snd_soc_card *card;
1321 card = devm_kzalloc(dev, sizeof(*card), GFP_KERNEL);
1323 return ERR_PTR(-ENOMEM);
1325 card->dai_link = sun4i_codec_create_link(dev, &card->num_links);
1326 if (!card->dai_link)
1327 return ERR_PTR(-ENOMEM);
1330 card->name = "sun4i-codec";
1331 card->dapm_widgets = sun4i_codec_card_dapm_widgets;
1332 card->num_dapm_widgets = ARRAY_SIZE(sun4i_codec_card_dapm_widgets);
1333 card->dapm_routes = sun4i_codec_card_dapm_routes;
1334 card->num_dapm_routes = ARRAY_SIZE(sun4i_codec_card_dapm_routes);
1339 static const struct snd_soc_dapm_widget sun6i_codec_card_dapm_widgets[] = {
1340 SND_SOC_DAPM_HP("Headphone", NULL),
1341 SND_SOC_DAPM_LINE("Line In", NULL),
1342 SND_SOC_DAPM_LINE("Line Out", NULL),
1343 SND_SOC_DAPM_MIC("Headset Mic", NULL),
1344 SND_SOC_DAPM_MIC("Mic", NULL),
1345 SND_SOC_DAPM_SPK("Speaker", sun4i_codec_spk_event),
1348 static struct snd_soc_card *sun6i_codec_create_card(struct device *dev)
1350 struct snd_soc_card *card;
1353 card = devm_kzalloc(dev, sizeof(*card), GFP_KERNEL);
1355 return ERR_PTR(-ENOMEM);
1357 card->dai_link = sun4i_codec_create_link(dev, &card->num_links);
1358 if (!card->dai_link)
1359 return ERR_PTR(-ENOMEM);
1362 card->name = "A31 Audio Codec";
1363 card->dapm_widgets = sun6i_codec_card_dapm_widgets;
1364 card->num_dapm_widgets = ARRAY_SIZE(sun6i_codec_card_dapm_widgets);
1365 card->fully_routed = true;
1367 ret = snd_soc_of_parse_audio_routing(card, "allwinner,audio-routing");
1369 dev_warn(dev, "failed to parse audio-routing: %d\n", ret);
1374 /* Connect digital side enables to analog side widgets */
1375 static const struct snd_soc_dapm_route sun8i_codec_card_routes[] = {
1377 { "Left ADC", NULL, "ADC Enable" },
1378 { "Right ADC", NULL, "ADC Enable" },
1379 { "Codec Capture", NULL, "Left ADC" },
1380 { "Codec Capture", NULL, "Right ADC" },
1383 { "Left DAC", NULL, "DAC Enable" },
1384 { "Right DAC", NULL, "DAC Enable" },
1385 { "Left DAC", NULL, "Codec Playback" },
1386 { "Right DAC", NULL, "Codec Playback" },
1389 static struct snd_soc_aux_dev aux_dev = {
1390 .name = "Codec Analog Controls",
1393 static struct snd_soc_card *sun8i_a23_codec_create_card(struct device *dev)
1395 struct snd_soc_card *card;
1398 card = devm_kzalloc(dev, sizeof(*card), GFP_KERNEL);
1400 return ERR_PTR(-ENOMEM);
1402 aux_dev.codec_of_node = of_parse_phandle(dev->of_node,
1403 "allwinner,codec-analog-controls",
1405 if (!aux_dev.codec_of_node) {
1406 dev_err(dev, "Can't find analog controls for codec.\n");
1407 return ERR_PTR(-EINVAL);
1410 card->dai_link = sun4i_codec_create_link(dev, &card->num_links);
1411 if (!card->dai_link)
1412 return ERR_PTR(-ENOMEM);
1415 card->name = "A23 Audio Codec";
1416 card->dapm_widgets = sun6i_codec_card_dapm_widgets;
1417 card->num_dapm_widgets = ARRAY_SIZE(sun6i_codec_card_dapm_widgets);
1418 card->dapm_routes = sun8i_codec_card_routes;
1419 card->num_dapm_routes = ARRAY_SIZE(sun8i_codec_card_routes);
1420 card->aux_dev = &aux_dev;
1421 card->num_aux_devs = 1;
1422 card->fully_routed = true;
1424 ret = snd_soc_of_parse_audio_routing(card, "allwinner,audio-routing");
1426 dev_warn(dev, "failed to parse audio-routing: %d\n", ret);
1431 static struct snd_soc_card *sun8i_h3_codec_create_card(struct device *dev)
1433 struct snd_soc_card *card;
1436 card = devm_kzalloc(dev, sizeof(*card), GFP_KERNEL);
1438 return ERR_PTR(-ENOMEM);
1440 aux_dev.codec_of_node = of_parse_phandle(dev->of_node,
1441 "allwinner,codec-analog-controls",
1443 if (!aux_dev.codec_of_node) {
1444 dev_err(dev, "Can't find analog controls for codec.\n");
1445 return ERR_PTR(-EINVAL);
1448 card->dai_link = sun4i_codec_create_link(dev, &card->num_links);
1449 if (!card->dai_link)
1450 return ERR_PTR(-ENOMEM);
1453 card->name = "H3 Audio Codec";
1454 card->dapm_widgets = sun6i_codec_card_dapm_widgets;
1455 card->num_dapm_widgets = ARRAY_SIZE(sun6i_codec_card_dapm_widgets);
1456 card->dapm_routes = sun8i_codec_card_routes;
1457 card->num_dapm_routes = ARRAY_SIZE(sun8i_codec_card_routes);
1458 card->aux_dev = &aux_dev;
1459 card->num_aux_devs = 1;
1460 card->fully_routed = true;
1462 ret = snd_soc_of_parse_audio_routing(card, "allwinner,audio-routing");
1464 dev_warn(dev, "failed to parse audio-routing: %d\n", ret);
1469 static struct snd_soc_card *sun8i_v3s_codec_create_card(struct device *dev)
1471 struct snd_soc_card *card;
1474 card = devm_kzalloc(dev, sizeof(*card), GFP_KERNEL);
1476 return ERR_PTR(-ENOMEM);
1478 aux_dev.codec_of_node = of_parse_phandle(dev->of_node,
1479 "allwinner,codec-analog-controls",
1481 if (!aux_dev.codec_of_node) {
1482 dev_err(dev, "Can't find analog controls for codec.\n");
1483 return ERR_PTR(-EINVAL);
1486 card->dai_link = sun4i_codec_create_link(dev, &card->num_links);
1487 if (!card->dai_link)
1488 return ERR_PTR(-ENOMEM);
1491 card->name = "V3s Audio Codec";
1492 card->dapm_widgets = sun6i_codec_card_dapm_widgets;
1493 card->num_dapm_widgets = ARRAY_SIZE(sun6i_codec_card_dapm_widgets);
1494 card->dapm_routes = sun8i_codec_card_routes;
1495 card->num_dapm_routes = ARRAY_SIZE(sun8i_codec_card_routes);
1496 card->aux_dev = &aux_dev;
1497 card->num_aux_devs = 1;
1498 card->fully_routed = true;
1500 ret = snd_soc_of_parse_audio_routing(card, "allwinner,audio-routing");
1502 dev_warn(dev, "failed to parse audio-routing: %d\n", ret);
1507 static const struct regmap_config sun4i_codec_regmap_config = {
1511 .max_register = SUN4I_CODEC_ADC_RXCNT,
1514 static const struct regmap_config sun6i_codec_regmap_config = {
1518 .max_register = SUN6I_CODEC_HMIC_DATA,
1521 static const struct regmap_config sun7i_codec_regmap_config = {
1525 .max_register = SUN7I_CODEC_AC_MIC_PHONE_CAL,
1528 static const struct regmap_config sun8i_a23_codec_regmap_config = {
1532 .max_register = SUN8I_A23_CODEC_ADC_RXCNT,
1535 static const struct regmap_config sun8i_h3_codec_regmap_config = {
1539 .max_register = SUN8I_H3_CODEC_ADC_DBG,
1542 static const struct regmap_config sun8i_v3s_codec_regmap_config = {
1546 .max_register = SUN8I_H3_CODEC_ADC_DBG,
1549 struct sun4i_codec_quirks {
1550 const struct regmap_config *regmap_config;
1551 const struct snd_soc_component_driver *codec;
1552 struct snd_soc_card * (*create_card)(struct device *dev);
1553 struct reg_field reg_adc_fifoc; /* used for regmap_field */
1554 unsigned int reg_dac_txdata; /* TX FIFO offset for DMA config */
1555 unsigned int reg_adc_rxdata; /* RX FIFO offset for DMA config */
1559 static const struct sun4i_codec_quirks sun4i_codec_quirks = {
1560 .regmap_config = &sun4i_codec_regmap_config,
1561 .codec = &sun4i_codec_codec,
1562 .create_card = sun4i_codec_create_card,
1563 .reg_adc_fifoc = REG_FIELD(SUN4I_CODEC_ADC_FIFOC, 0, 31),
1564 .reg_dac_txdata = SUN4I_CODEC_DAC_TXDATA,
1565 .reg_adc_rxdata = SUN4I_CODEC_ADC_RXDATA,
1568 static const struct sun4i_codec_quirks sun6i_a31_codec_quirks = {
1569 .regmap_config = &sun6i_codec_regmap_config,
1570 .codec = &sun6i_codec_codec,
1571 .create_card = sun6i_codec_create_card,
1572 .reg_adc_fifoc = REG_FIELD(SUN6I_CODEC_ADC_FIFOC, 0, 31),
1573 .reg_dac_txdata = SUN4I_CODEC_DAC_TXDATA,
1574 .reg_adc_rxdata = SUN6I_CODEC_ADC_RXDATA,
1578 static const struct sun4i_codec_quirks sun7i_codec_quirks = {
1579 .regmap_config = &sun7i_codec_regmap_config,
1580 .codec = &sun7i_codec_codec,
1581 .create_card = sun4i_codec_create_card,
1582 .reg_adc_fifoc = REG_FIELD(SUN4I_CODEC_ADC_FIFOC, 0, 31),
1583 .reg_dac_txdata = SUN4I_CODEC_DAC_TXDATA,
1584 .reg_adc_rxdata = SUN4I_CODEC_ADC_RXDATA,
1587 static const struct sun4i_codec_quirks sun8i_a23_codec_quirks = {
1588 .regmap_config = &sun8i_a23_codec_regmap_config,
1589 .codec = &sun8i_a23_codec_codec,
1590 .create_card = sun8i_a23_codec_create_card,
1591 .reg_adc_fifoc = REG_FIELD(SUN6I_CODEC_ADC_FIFOC, 0, 31),
1592 .reg_dac_txdata = SUN4I_CODEC_DAC_TXDATA,
1593 .reg_adc_rxdata = SUN6I_CODEC_ADC_RXDATA,
1597 static const struct sun4i_codec_quirks sun8i_h3_codec_quirks = {
1598 .regmap_config = &sun8i_h3_codec_regmap_config,
1600 * TODO Share the codec structure with A23 for now.
1601 * This should be split out when adding digital audio
1602 * processing support for the H3.
1604 .codec = &sun8i_a23_codec_codec,
1605 .create_card = sun8i_h3_codec_create_card,
1606 .reg_adc_fifoc = REG_FIELD(SUN6I_CODEC_ADC_FIFOC, 0, 31),
1607 .reg_dac_txdata = SUN8I_H3_CODEC_DAC_TXDATA,
1608 .reg_adc_rxdata = SUN6I_CODEC_ADC_RXDATA,
1612 static const struct sun4i_codec_quirks sun8i_v3s_codec_quirks = {
1613 .regmap_config = &sun8i_v3s_codec_regmap_config,
1615 * TODO The codec structure should be split out, like
1616 * H3, when adding digital audio processing support.
1618 .codec = &sun8i_a23_codec_codec,
1619 .create_card = sun8i_v3s_codec_create_card,
1620 .reg_adc_fifoc = REG_FIELD(SUN6I_CODEC_ADC_FIFOC, 0, 31),
1621 .reg_dac_txdata = SUN8I_H3_CODEC_DAC_TXDATA,
1622 .reg_adc_rxdata = SUN6I_CODEC_ADC_RXDATA,
1626 static const struct of_device_id sun4i_codec_of_match[] = {
1628 .compatible = "allwinner,sun4i-a10-codec",
1629 .data = &sun4i_codec_quirks,
1632 .compatible = "allwinner,sun6i-a31-codec",
1633 .data = &sun6i_a31_codec_quirks,
1636 .compatible = "allwinner,sun7i-a20-codec",
1637 .data = &sun7i_codec_quirks,
1640 .compatible = "allwinner,sun8i-a23-codec",
1641 .data = &sun8i_a23_codec_quirks,
1644 .compatible = "allwinner,sun8i-h3-codec",
1645 .data = &sun8i_h3_codec_quirks,
1648 .compatible = "allwinner,sun8i-v3s-codec",
1649 .data = &sun8i_v3s_codec_quirks,
1653 MODULE_DEVICE_TABLE(of, sun4i_codec_of_match);
1655 static int sun4i_codec_probe(struct platform_device *pdev)
1657 struct snd_soc_card *card;
1658 struct sun4i_codec *scodec;
1659 const struct sun4i_codec_quirks *quirks;
1660 struct resource *res;
1664 scodec = devm_kzalloc(&pdev->dev, sizeof(*scodec), GFP_KERNEL);
1668 scodec->dev = &pdev->dev;
1670 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1671 base = devm_ioremap_resource(&pdev->dev, res);
1673 dev_err(&pdev->dev, "Failed to map the registers\n");
1674 return PTR_ERR(base);
1677 quirks = of_device_get_match_data(&pdev->dev);
1678 if (quirks == NULL) {
1679 dev_err(&pdev->dev, "Failed to determine the quirks to use\n");
1683 scodec->regmap = devm_regmap_init_mmio(&pdev->dev, base,
1684 quirks->regmap_config);
1685 if (IS_ERR(scodec->regmap)) {
1686 dev_err(&pdev->dev, "Failed to create our regmap\n");
1687 return PTR_ERR(scodec->regmap);
1690 /* Get the clocks from the DT */
1691 scodec->clk_apb = devm_clk_get(&pdev->dev, "apb");
1692 if (IS_ERR(scodec->clk_apb)) {
1693 dev_err(&pdev->dev, "Failed to get the APB clock\n");
1694 return PTR_ERR(scodec->clk_apb);
1697 scodec->clk_module = devm_clk_get(&pdev->dev, "codec");
1698 if (IS_ERR(scodec->clk_module)) {
1699 dev_err(&pdev->dev, "Failed to get the module clock\n");
1700 return PTR_ERR(scodec->clk_module);
1703 if (quirks->has_reset) {
1704 scodec->rst = devm_reset_control_get_exclusive(&pdev->dev,
1706 if (IS_ERR(scodec->rst)) {
1707 dev_err(&pdev->dev, "Failed to get reset control\n");
1708 return PTR_ERR(scodec->rst);
1712 scodec->gpio_pa = devm_gpiod_get_optional(&pdev->dev, "allwinner,pa",
1714 if (IS_ERR(scodec->gpio_pa)) {
1715 ret = PTR_ERR(scodec->gpio_pa);
1716 if (ret != -EPROBE_DEFER)
1717 dev_err(&pdev->dev, "Failed to get pa gpio: %d\n", ret);
1721 /* reg_field setup */
1722 scodec->reg_adc_fifoc = devm_regmap_field_alloc(&pdev->dev,
1724 quirks->reg_adc_fifoc);
1725 if (IS_ERR(scodec->reg_adc_fifoc)) {
1726 ret = PTR_ERR(scodec->reg_adc_fifoc);
1727 dev_err(&pdev->dev, "Failed to create regmap fields: %d\n",
1732 /* Enable the bus clock */
1733 if (clk_prepare_enable(scodec->clk_apb)) {
1734 dev_err(&pdev->dev, "Failed to enable the APB clock\n");
1738 /* Deassert the reset control */
1740 ret = reset_control_deassert(scodec->rst);
1743 "Failed to deassert the reset control\n");
1744 goto err_clk_disable;
1748 /* DMA configuration for TX FIFO */
1749 scodec->playback_dma_data.addr = res->start + quirks->reg_dac_txdata;
1750 scodec->playback_dma_data.maxburst = 8;
1751 scodec->playback_dma_data.addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
1753 /* DMA configuration for RX FIFO */
1754 scodec->capture_dma_data.addr = res->start + quirks->reg_adc_rxdata;
1755 scodec->capture_dma_data.maxburst = 8;
1756 scodec->capture_dma_data.addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
1758 ret = devm_snd_soc_register_component(&pdev->dev, quirks->codec,
1759 &sun4i_codec_dai, 1);
1761 dev_err(&pdev->dev, "Failed to register our codec\n");
1762 goto err_assert_reset;
1765 ret = devm_snd_soc_register_component(&pdev->dev,
1766 &sun4i_codec_component,
1769 dev_err(&pdev->dev, "Failed to register our DAI\n");
1770 goto err_assert_reset;
1773 ret = devm_snd_dmaengine_pcm_register(&pdev->dev, NULL, 0);
1775 dev_err(&pdev->dev, "Failed to register against DMAEngine\n");
1776 goto err_assert_reset;
1779 card = quirks->create_card(&pdev->dev);
1781 ret = PTR_ERR(card);
1782 dev_err(&pdev->dev, "Failed to create our card\n");
1783 goto err_assert_reset;
1786 snd_soc_card_set_drvdata(card, scodec);
1788 ret = snd_soc_register_card(card);
1790 dev_err(&pdev->dev, "Failed to register our card\n");
1791 goto err_assert_reset;
1798 reset_control_assert(scodec->rst);
1800 clk_disable_unprepare(scodec->clk_apb);
1804 static int sun4i_codec_remove(struct platform_device *pdev)
1806 struct snd_soc_card *card = platform_get_drvdata(pdev);
1807 struct sun4i_codec *scodec = snd_soc_card_get_drvdata(card);
1809 snd_soc_unregister_card(card);
1811 reset_control_assert(scodec->rst);
1812 clk_disable_unprepare(scodec->clk_apb);
1817 static struct platform_driver sun4i_codec_driver = {
1819 .name = "sun4i-codec",
1820 .of_match_table = sun4i_codec_of_match,
1822 .probe = sun4i_codec_probe,
1823 .remove = sun4i_codec_remove,
1825 module_platform_driver(sun4i_codec_driver);
1827 MODULE_DESCRIPTION("Allwinner A10 codec driver");
1828 MODULE_AUTHOR("Emilio López <emilio@elopez.com.ar>");
1829 MODULE_AUTHOR("Jon Smirl <jonsmirl@gmail.com>");
1830 MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
1831 MODULE_AUTHOR("Chen-Yu Tsai <wens@csie.org>");
1832 MODULE_LICENSE("GPL");