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_LDACLMIXS (15)
68 #define SUN4I_CODEC_DAC_ACTL_RDACRMIXS (14)
69 #define SUN4I_CODEC_DAC_ACTL_LDACRMIXS (13)
70 #define SUN4I_CODEC_DAC_ACTL_DACPAS (8)
71 #define SUN4I_CODEC_DAC_ACTL_MIXPAS (7)
72 #define SUN4I_CODEC_DAC_ACTL_PA_MUTE (6)
73 #define SUN4I_CODEC_DAC_ACTL_PA_VOL (0)
74 #define SUN4I_CODEC_DAC_TUNE (0x14)
75 #define SUN4I_CODEC_DAC_DEBUG (0x18)
77 /* Codec ADC digital controls and FIFO registers */
78 #define SUN4I_CODEC_ADC_FIFOC (0x1c)
79 #define SUN4I_CODEC_ADC_FIFOC_ADC_FS (29)
80 #define SUN4I_CODEC_ADC_FIFOC_EN_AD (28)
81 #define SUN4I_CODEC_ADC_FIFOC_RX_FIFO_MODE (24)
82 #define SUN4I_CODEC_ADC_FIFOC_RX_TRIG_LEVEL (8)
83 #define SUN4I_CODEC_ADC_FIFOC_MONO_EN (7)
84 #define SUN4I_CODEC_ADC_FIFOC_RX_SAMPLE_BITS (6)
85 #define SUN4I_CODEC_ADC_FIFOC_ADC_DRQ_EN (4)
86 #define SUN4I_CODEC_ADC_FIFOC_FIFO_FLUSH (0)
87 #define SUN4I_CODEC_ADC_FIFOS (0x20)
88 #define SUN4I_CODEC_ADC_RXDATA (0x24)
90 /* Codec ADC side analog signal controls */
91 #define SUN4I_CODEC_ADC_ACTL (0x28)
92 #define SUN4I_CODEC_ADC_ACTL_ADC_R_EN (31)
93 #define SUN4I_CODEC_ADC_ACTL_ADC_L_EN (30)
94 #define SUN4I_CODEC_ADC_ACTL_PREG1EN (29)
95 #define SUN4I_CODEC_ADC_ACTL_PREG2EN (28)
96 #define SUN4I_CODEC_ADC_ACTL_VMICEN (27)
97 #define SUN4I_CODEC_ADC_ACTL_VADCG (20)
98 #define SUN4I_CODEC_ADC_ACTL_ADCIS (17)
99 #define SUN4I_CODEC_ADC_ACTL_PA_EN (4)
100 #define SUN4I_CODEC_ADC_ACTL_DDE (3)
101 #define SUN4I_CODEC_ADC_DEBUG (0x2c)
104 #define SUN4I_CODEC_DAC_TXCNT (0x30)
105 #define SUN4I_CODEC_ADC_RXCNT (0x34)
107 /* Calibration register (sun7i only) */
108 #define SUN7I_CODEC_AC_DAC_CAL (0x38)
110 /* Microphone controls (sun7i only) */
111 #define SUN7I_CODEC_AC_MIC_PHONE_CAL (0x3c)
114 * sun6i specific registers
116 * sun6i shares the same digital control and FIFO registers as sun4i,
117 * but only the DAC digital controls are at the same offset. The others
118 * have been moved around to accommodate extra analog controls.
121 /* Codec DAC digital controls and FIFO registers */
122 #define SUN6I_CODEC_ADC_FIFOC (0x10)
123 #define SUN6I_CODEC_ADC_FIFOC_EN_AD (28)
124 #define SUN6I_CODEC_ADC_FIFOS (0x14)
125 #define SUN6I_CODEC_ADC_RXDATA (0x18)
127 /* Output mixer and gain controls */
128 #define SUN6I_CODEC_OM_DACA_CTRL (0x20)
129 #define SUN6I_CODEC_OM_DACA_CTRL_DACAREN (31)
130 #define SUN6I_CODEC_OM_DACA_CTRL_DACALEN (30)
131 #define SUN6I_CODEC_OM_DACA_CTRL_RMIXEN (29)
132 #define SUN6I_CODEC_OM_DACA_CTRL_LMIXEN (28)
133 #define SUN6I_CODEC_OM_DACA_CTRL_RMIX_MIC1 (23)
134 #define SUN6I_CODEC_OM_DACA_CTRL_RMIX_MIC2 (22)
135 #define SUN6I_CODEC_OM_DACA_CTRL_RMIX_PHONE (21)
136 #define SUN6I_CODEC_OM_DACA_CTRL_RMIX_PHONEP (20)
137 #define SUN6I_CODEC_OM_DACA_CTRL_RMIX_LINEINR (19)
138 #define SUN6I_CODEC_OM_DACA_CTRL_RMIX_DACR (18)
139 #define SUN6I_CODEC_OM_DACA_CTRL_RMIX_DACL (17)
140 #define SUN6I_CODEC_OM_DACA_CTRL_LMIX_MIC1 (16)
141 #define SUN6I_CODEC_OM_DACA_CTRL_LMIX_MIC2 (15)
142 #define SUN6I_CODEC_OM_DACA_CTRL_LMIX_PHONE (14)
143 #define SUN6I_CODEC_OM_DACA_CTRL_LMIX_PHONEN (13)
144 #define SUN6I_CODEC_OM_DACA_CTRL_LMIX_LINEINL (12)
145 #define SUN6I_CODEC_OM_DACA_CTRL_LMIX_DACL (11)
146 #define SUN6I_CODEC_OM_DACA_CTRL_LMIX_DACR (10)
147 #define SUN6I_CODEC_OM_DACA_CTRL_RHPIS (9)
148 #define SUN6I_CODEC_OM_DACA_CTRL_LHPIS (8)
149 #define SUN6I_CODEC_OM_DACA_CTRL_RHPPAMUTE (7)
150 #define SUN6I_CODEC_OM_DACA_CTRL_LHPPAMUTE (6)
151 #define SUN6I_CODEC_OM_DACA_CTRL_HPVOL (0)
152 #define SUN6I_CODEC_OM_PA_CTRL (0x24)
153 #define SUN6I_CODEC_OM_PA_CTRL_HPPAEN (31)
154 #define SUN6I_CODEC_OM_PA_CTRL_HPCOM_CTL (29)
155 #define SUN6I_CODEC_OM_PA_CTRL_COMPTEN (28)
156 #define SUN6I_CODEC_OM_PA_CTRL_MIC1G (15)
157 #define SUN6I_CODEC_OM_PA_CTRL_MIC2G (12)
158 #define SUN6I_CODEC_OM_PA_CTRL_LINEING (9)
159 #define SUN6I_CODEC_OM_PA_CTRL_PHONEG (6)
160 #define SUN6I_CODEC_OM_PA_CTRL_PHONEPG (3)
161 #define SUN6I_CODEC_OM_PA_CTRL_PHONENG (0)
163 /* Microphone, line out and phone out controls */
164 #define SUN6I_CODEC_MIC_CTRL (0x28)
165 #define SUN6I_CODEC_MIC_CTRL_HBIASEN (31)
166 #define SUN6I_CODEC_MIC_CTRL_MBIASEN (30)
167 #define SUN6I_CODEC_MIC_CTRL_MIC1AMPEN (28)
168 #define SUN6I_CODEC_MIC_CTRL_MIC1BOOST (25)
169 #define SUN6I_CODEC_MIC_CTRL_MIC2AMPEN (24)
170 #define SUN6I_CODEC_MIC_CTRL_MIC2BOOST (21)
171 #define SUN6I_CODEC_MIC_CTRL_MIC2SLT (20)
172 #define SUN6I_CODEC_MIC_CTRL_LINEOUTLEN (19)
173 #define SUN6I_CODEC_MIC_CTRL_LINEOUTREN (18)
174 #define SUN6I_CODEC_MIC_CTRL_LINEOUTLSRC (17)
175 #define SUN6I_CODEC_MIC_CTRL_LINEOUTRSRC (16)
176 #define SUN6I_CODEC_MIC_CTRL_LINEOUTVC (11)
177 #define SUN6I_CODEC_MIC_CTRL_PHONEPREG (8)
179 /* ADC mixer controls */
180 #define SUN6I_CODEC_ADC_ACTL (0x2c)
181 #define SUN6I_CODEC_ADC_ACTL_ADCREN (31)
182 #define SUN6I_CODEC_ADC_ACTL_ADCLEN (30)
183 #define SUN6I_CODEC_ADC_ACTL_ADCRG (27)
184 #define SUN6I_CODEC_ADC_ACTL_ADCLG (24)
185 #define SUN6I_CODEC_ADC_ACTL_RADCMIX_MIC1 (13)
186 #define SUN6I_CODEC_ADC_ACTL_RADCMIX_MIC2 (12)
187 #define SUN6I_CODEC_ADC_ACTL_RADCMIX_PHONE (11)
188 #define SUN6I_CODEC_ADC_ACTL_RADCMIX_PHONEP (10)
189 #define SUN6I_CODEC_ADC_ACTL_RADCMIX_LINEINR (9)
190 #define SUN6I_CODEC_ADC_ACTL_RADCMIX_OMIXR (8)
191 #define SUN6I_CODEC_ADC_ACTL_RADCMIX_OMIXL (7)
192 #define SUN6I_CODEC_ADC_ACTL_LADCMIX_MIC1 (6)
193 #define SUN6I_CODEC_ADC_ACTL_LADCMIX_MIC2 (5)
194 #define SUN6I_CODEC_ADC_ACTL_LADCMIX_PHONE (4)
195 #define SUN6I_CODEC_ADC_ACTL_LADCMIX_PHONEN (3)
196 #define SUN6I_CODEC_ADC_ACTL_LADCMIX_LINEINL (2)
197 #define SUN6I_CODEC_ADC_ACTL_LADCMIX_OMIXL (1)
198 #define SUN6I_CODEC_ADC_ACTL_LADCMIX_OMIXR (0)
200 /* Analog performance tuning controls */
201 #define SUN6I_CODEC_ADDA_TUNE (0x30)
203 /* Calibration controls */
204 #define SUN6I_CODEC_CALIBRATION (0x34)
207 #define SUN6I_CODEC_DAC_TXCNT (0x40)
208 #define SUN6I_CODEC_ADC_RXCNT (0x44)
210 /* headset jack detection and button support registers */
211 #define SUN6I_CODEC_HMIC_CTL (0x50)
212 #define SUN6I_CODEC_HMIC_DATA (0x54)
214 /* TODO sun6i DAP (Digital Audio Processing) bits */
216 /* FIFO counters moved on A23 */
217 #define SUN8I_A23_CODEC_DAC_TXCNT (0x1c)
218 #define SUN8I_A23_CODEC_ADC_RXCNT (0x20)
220 /* TX FIFO moved on H3 */
221 #define SUN8I_H3_CODEC_DAC_TXDATA (0x20)
222 #define SUN8I_H3_CODEC_DAC_DBG (0x48)
223 #define SUN8I_H3_CODEC_ADC_DBG (0x4c)
225 /* TODO H3 DAP (Digital Audio Processing) bits */
229 struct regmap
*regmap
;
231 struct clk
*clk_module
;
232 struct reset_control
*rst
;
233 struct gpio_desc
*gpio_pa
;
235 /* ADC_FIFOC register is at different offset on different SoCs */
236 struct regmap_field
*reg_adc_fifoc
;
238 struct snd_dmaengine_dai_dma_data capture_dma_data
;
239 struct snd_dmaengine_dai_dma_data playback_dma_data
;
242 static void sun4i_codec_start_playback(struct sun4i_codec
*scodec
)
245 regmap_update_bits(scodec
->regmap
, SUN4I_CODEC_DAC_FIFOC
,
246 BIT(SUN4I_CODEC_DAC_FIFOC_FIFO_FLUSH
),
247 BIT(SUN4I_CODEC_DAC_FIFOC_FIFO_FLUSH
));
250 regmap_update_bits(scodec
->regmap
, SUN4I_CODEC_DAC_FIFOC
,
251 BIT(SUN4I_CODEC_DAC_FIFOC_DAC_DRQ_EN
),
252 BIT(SUN4I_CODEC_DAC_FIFOC_DAC_DRQ_EN
));
255 static void sun4i_codec_stop_playback(struct sun4i_codec
*scodec
)
257 /* Disable DAC DRQ */
258 regmap_update_bits(scodec
->regmap
, SUN4I_CODEC_DAC_FIFOC
,
259 BIT(SUN4I_CODEC_DAC_FIFOC_DAC_DRQ_EN
),
263 static void sun4i_codec_start_capture(struct sun4i_codec
*scodec
)
266 regmap_field_update_bits(scodec
->reg_adc_fifoc
,
267 BIT(SUN4I_CODEC_ADC_FIFOC_ADC_DRQ_EN
),
268 BIT(SUN4I_CODEC_ADC_FIFOC_ADC_DRQ_EN
));
271 static void sun4i_codec_stop_capture(struct sun4i_codec
*scodec
)
273 /* Disable ADC DRQ */
274 regmap_field_update_bits(scodec
->reg_adc_fifoc
,
275 BIT(SUN4I_CODEC_ADC_FIFOC_ADC_DRQ_EN
), 0);
278 static int sun4i_codec_trigger(struct snd_pcm_substream
*substream
, int cmd
,
279 struct snd_soc_dai
*dai
)
281 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
282 struct sun4i_codec
*scodec
= snd_soc_card_get_drvdata(rtd
->card
);
285 case SNDRV_PCM_TRIGGER_START
:
286 case SNDRV_PCM_TRIGGER_RESUME
:
287 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
288 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
289 sun4i_codec_start_playback(scodec
);
291 sun4i_codec_start_capture(scodec
);
294 case SNDRV_PCM_TRIGGER_STOP
:
295 case SNDRV_PCM_TRIGGER_SUSPEND
:
296 case SNDRV_PCM_TRIGGER_PAUSE_PUSH
:
297 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
298 sun4i_codec_stop_playback(scodec
);
300 sun4i_codec_stop_capture(scodec
);
310 static int sun4i_codec_prepare_capture(struct snd_pcm_substream
*substream
,
311 struct snd_soc_dai
*dai
)
313 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
314 struct sun4i_codec
*scodec
= snd_soc_card_get_drvdata(rtd
->card
);
318 regmap_field_update_bits(scodec
->reg_adc_fifoc
,
319 BIT(SUN4I_CODEC_ADC_FIFOC_FIFO_FLUSH
),
320 BIT(SUN4I_CODEC_ADC_FIFOC_FIFO_FLUSH
));
323 /* Set RX FIFO trigger level */
324 regmap_field_update_bits(scodec
->reg_adc_fifoc
,
325 0xf << SUN4I_CODEC_ADC_FIFOC_RX_TRIG_LEVEL
,
326 0x7 << SUN4I_CODEC_ADC_FIFOC_RX_TRIG_LEVEL
);
329 * FIXME: Undocumented in the datasheet, but
330 * Allwinner's code mentions that it is related
331 * related to microphone gain
333 if (of_device_is_compatible(scodec
->dev
->of_node
,
334 "allwinner,sun4i-a10-codec") ||
335 of_device_is_compatible(scodec
->dev
->of_node
,
336 "allwinner,sun7i-a20-codec")) {
337 regmap_update_bits(scodec
->regmap
, SUN4I_CODEC_ADC_ACTL
,
342 if (of_device_is_compatible(scodec
->dev
->of_node
,
343 "allwinner,sun7i-a20-codec"))
344 /* FIXME: Undocumented bits */
345 regmap_update_bits(scodec
->regmap
, SUN4I_CODEC_DAC_TUNE
,
352 static int sun4i_codec_prepare_playback(struct snd_pcm_substream
*substream
,
353 struct snd_soc_dai
*dai
)
355 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
356 struct sun4i_codec
*scodec
= snd_soc_card_get_drvdata(rtd
->card
);
359 /* Flush the TX FIFO */
360 regmap_update_bits(scodec
->regmap
, SUN4I_CODEC_DAC_FIFOC
,
361 BIT(SUN4I_CODEC_DAC_FIFOC_FIFO_FLUSH
),
362 BIT(SUN4I_CODEC_DAC_FIFOC_FIFO_FLUSH
));
364 /* Set TX FIFO Empty Trigger Level */
365 regmap_update_bits(scodec
->regmap
, SUN4I_CODEC_DAC_FIFOC
,
366 0x3f << SUN4I_CODEC_DAC_FIFOC_TX_TRIG_LEVEL
,
367 0xf << SUN4I_CODEC_DAC_FIFOC_TX_TRIG_LEVEL
);
369 if (substream
->runtime
->rate
> 32000)
370 /* Use 64 bits FIR filter */
373 /* Use 32 bits FIR filter */
374 val
= BIT(SUN4I_CODEC_DAC_FIFOC_FIR_VERSION
);
376 regmap_update_bits(scodec
->regmap
, SUN4I_CODEC_DAC_FIFOC
,
377 BIT(SUN4I_CODEC_DAC_FIFOC_FIR_VERSION
),
380 /* Send zeros when we have an underrun */
381 regmap_update_bits(scodec
->regmap
, SUN4I_CODEC_DAC_FIFOC
,
382 BIT(SUN4I_CODEC_DAC_FIFOC_SEND_LASAT
),
388 static int sun4i_codec_prepare(struct snd_pcm_substream
*substream
,
389 struct snd_soc_dai
*dai
)
391 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
392 return sun4i_codec_prepare_playback(substream
, dai
);
394 return sun4i_codec_prepare_capture(substream
, dai
);
397 static unsigned long sun4i_codec_get_mod_freq(struct snd_pcm_hw_params
*params
)
399 unsigned int rate
= params_rate(params
);
427 static int sun4i_codec_get_hw_rate(struct snd_pcm_hw_params
*params
)
429 unsigned int rate
= params_rate(params
);
469 static int sun4i_codec_hw_params_capture(struct sun4i_codec
*scodec
,
470 struct snd_pcm_hw_params
*params
,
473 /* Set ADC sample rate */
474 regmap_field_update_bits(scodec
->reg_adc_fifoc
,
475 7 << SUN4I_CODEC_ADC_FIFOC_ADC_FS
,
476 hwrate
<< SUN4I_CODEC_ADC_FIFOC_ADC_FS
);
478 /* Set the number of channels we want to use */
479 if (params_channels(params
) == 1)
480 regmap_field_update_bits(scodec
->reg_adc_fifoc
,
481 BIT(SUN4I_CODEC_ADC_FIFOC_MONO_EN
),
482 BIT(SUN4I_CODEC_ADC_FIFOC_MONO_EN
));
484 regmap_field_update_bits(scodec
->reg_adc_fifoc
,
485 BIT(SUN4I_CODEC_ADC_FIFOC_MONO_EN
),
488 /* Set the number of sample bits to either 16 or 24 bits */
489 if (hw_param_interval(params
, SNDRV_PCM_HW_PARAM_SAMPLE_BITS
)->min
== 32) {
490 regmap_field_update_bits(scodec
->reg_adc_fifoc
,
491 BIT(SUN4I_CODEC_ADC_FIFOC_RX_SAMPLE_BITS
),
492 BIT(SUN4I_CODEC_ADC_FIFOC_RX_SAMPLE_BITS
));
494 regmap_field_update_bits(scodec
->reg_adc_fifoc
,
495 BIT(SUN4I_CODEC_ADC_FIFOC_RX_FIFO_MODE
),
498 scodec
->capture_dma_data
.addr_width
= DMA_SLAVE_BUSWIDTH_4_BYTES
;
500 regmap_field_update_bits(scodec
->reg_adc_fifoc
,
501 BIT(SUN4I_CODEC_ADC_FIFOC_RX_SAMPLE_BITS
),
504 /* Fill most significant bits with valid data MSB */
505 regmap_field_update_bits(scodec
->reg_adc_fifoc
,
506 BIT(SUN4I_CODEC_ADC_FIFOC_RX_FIFO_MODE
),
507 BIT(SUN4I_CODEC_ADC_FIFOC_RX_FIFO_MODE
));
509 scodec
->capture_dma_data
.addr_width
= DMA_SLAVE_BUSWIDTH_2_BYTES
;
515 static int sun4i_codec_hw_params_playback(struct sun4i_codec
*scodec
,
516 struct snd_pcm_hw_params
*params
,
521 /* Set DAC sample rate */
522 regmap_update_bits(scodec
->regmap
, SUN4I_CODEC_DAC_FIFOC
,
523 7 << SUN4I_CODEC_DAC_FIFOC_DAC_FS
,
524 hwrate
<< SUN4I_CODEC_DAC_FIFOC_DAC_FS
);
526 /* Set the number of channels we want to use */
527 if (params_channels(params
) == 1)
528 val
= BIT(SUN4I_CODEC_DAC_FIFOC_MONO_EN
);
532 regmap_update_bits(scodec
->regmap
, SUN4I_CODEC_DAC_FIFOC
,
533 BIT(SUN4I_CODEC_DAC_FIFOC_MONO_EN
),
536 /* Set the number of sample bits to either 16 or 24 bits */
537 if (hw_param_interval(params
, SNDRV_PCM_HW_PARAM_SAMPLE_BITS
)->min
== 32) {
538 regmap_update_bits(scodec
->regmap
, SUN4I_CODEC_DAC_FIFOC
,
539 BIT(SUN4I_CODEC_DAC_FIFOC_TX_SAMPLE_BITS
),
540 BIT(SUN4I_CODEC_DAC_FIFOC_TX_SAMPLE_BITS
));
542 /* Set TX FIFO mode to padding the LSBs with 0 */
543 regmap_update_bits(scodec
->regmap
, SUN4I_CODEC_DAC_FIFOC
,
544 BIT(SUN4I_CODEC_DAC_FIFOC_TX_FIFO_MODE
),
547 scodec
->playback_dma_data
.addr_width
= DMA_SLAVE_BUSWIDTH_4_BYTES
;
549 regmap_update_bits(scodec
->regmap
, SUN4I_CODEC_DAC_FIFOC
,
550 BIT(SUN4I_CODEC_DAC_FIFOC_TX_SAMPLE_BITS
),
553 /* Set TX FIFO mode to repeat the MSB */
554 regmap_update_bits(scodec
->regmap
, SUN4I_CODEC_DAC_FIFOC
,
555 BIT(SUN4I_CODEC_DAC_FIFOC_TX_FIFO_MODE
),
556 BIT(SUN4I_CODEC_DAC_FIFOC_TX_FIFO_MODE
));
558 scodec
->playback_dma_data
.addr_width
= DMA_SLAVE_BUSWIDTH_2_BYTES
;
564 static int sun4i_codec_hw_params(struct snd_pcm_substream
*substream
,
565 struct snd_pcm_hw_params
*params
,
566 struct snd_soc_dai
*dai
)
568 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
569 struct sun4i_codec
*scodec
= snd_soc_card_get_drvdata(rtd
->card
);
570 unsigned long clk_freq
;
573 clk_freq
= sun4i_codec_get_mod_freq(params
);
577 ret
= clk_set_rate(scodec
->clk_module
, clk_freq
);
581 hwrate
= sun4i_codec_get_hw_rate(params
);
585 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
586 return sun4i_codec_hw_params_playback(scodec
, params
,
589 return sun4i_codec_hw_params_capture(scodec
, params
,
593 static int sun4i_codec_startup(struct snd_pcm_substream
*substream
,
594 struct snd_soc_dai
*dai
)
596 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
597 struct sun4i_codec
*scodec
= snd_soc_card_get_drvdata(rtd
->card
);
600 * Stop issuing DRQ when we have room for less than 16 samples
603 regmap_update_bits(scodec
->regmap
, SUN4I_CODEC_DAC_FIFOC
,
604 3 << SUN4I_CODEC_DAC_FIFOC_DRQ_CLR_CNT
,
605 3 << SUN4I_CODEC_DAC_FIFOC_DRQ_CLR_CNT
);
607 return clk_prepare_enable(scodec
->clk_module
);
610 static void sun4i_codec_shutdown(struct snd_pcm_substream
*substream
,
611 struct snd_soc_dai
*dai
)
613 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
614 struct sun4i_codec
*scodec
= snd_soc_card_get_drvdata(rtd
->card
);
616 clk_disable_unprepare(scodec
->clk_module
);
619 static const struct snd_soc_dai_ops sun4i_codec_dai_ops
= {
620 .startup
= sun4i_codec_startup
,
621 .shutdown
= sun4i_codec_shutdown
,
622 .trigger
= sun4i_codec_trigger
,
623 .hw_params
= sun4i_codec_hw_params
,
624 .prepare
= sun4i_codec_prepare
,
627 static struct snd_soc_dai_driver sun4i_codec_dai
= {
629 .ops
= &sun4i_codec_dai_ops
,
631 .stream_name
= "Codec Playback",
636 .rates
= SNDRV_PCM_RATE_8000_48000
|
637 SNDRV_PCM_RATE_96000
|
638 SNDRV_PCM_RATE_192000
,
639 .formats
= SNDRV_PCM_FMTBIT_S16_LE
|
640 SNDRV_PCM_FMTBIT_S32_LE
,
644 .stream_name
= "Codec Capture",
649 .rates
= SNDRV_PCM_RATE_8000_48000
|
650 SNDRV_PCM_RATE_96000
|
651 SNDRV_PCM_RATE_192000
|
653 .formats
= SNDRV_PCM_FMTBIT_S16_LE
|
654 SNDRV_PCM_FMTBIT_S32_LE
,
659 /*** sun4i Codec ***/
660 static const struct snd_kcontrol_new sun4i_codec_pa_mute
=
661 SOC_DAPM_SINGLE("Switch", SUN4I_CODEC_DAC_ACTL
,
662 SUN4I_CODEC_DAC_ACTL_PA_MUTE
, 1, 0);
664 static DECLARE_TLV_DB_SCALE(sun4i_codec_pa_volume_scale
, -6300, 100, 1);
666 static const struct snd_kcontrol_new sun4i_codec_controls
[] = {
667 SOC_SINGLE_TLV("Power Amplifier Volume", SUN4I_CODEC_DAC_ACTL
,
668 SUN4I_CODEC_DAC_ACTL_PA_VOL
, 0x3F, 0,
669 sun4i_codec_pa_volume_scale
),
672 static const struct snd_kcontrol_new sun4i_codec_left_mixer_controls
[] = {
673 SOC_DAPM_SINGLE("Left DAC Playback Switch", SUN4I_CODEC_DAC_ACTL
,
674 SUN4I_CODEC_DAC_ACTL_LDACLMIXS
, 1, 0),
677 static const struct snd_kcontrol_new sun4i_codec_right_mixer_controls
[] = {
678 SOC_DAPM_SINGLE("Right DAC Playback Switch", SUN4I_CODEC_DAC_ACTL
,
679 SUN4I_CODEC_DAC_ACTL_RDACRMIXS
, 1, 0),
680 SOC_DAPM_SINGLE("Left DAC Playback Switch", SUN4I_CODEC_DAC_ACTL
,
681 SUN4I_CODEC_DAC_ACTL_LDACRMIXS
, 1, 0),
684 static const struct snd_kcontrol_new sun4i_codec_pa_mixer_controls
[] = {
685 SOC_DAPM_SINGLE("DAC Playback Switch", SUN4I_CODEC_DAC_ACTL
,
686 SUN4I_CODEC_DAC_ACTL_DACPAS
, 1, 0),
687 SOC_DAPM_SINGLE("Mixer Playback Switch", SUN4I_CODEC_DAC_ACTL
,
688 SUN4I_CODEC_DAC_ACTL_MIXPAS
, 1, 0),
691 static const struct snd_soc_dapm_widget sun4i_codec_codec_dapm_widgets
[] = {
692 /* Digital parts of the ADCs */
693 SND_SOC_DAPM_SUPPLY("ADC", SUN4I_CODEC_ADC_FIFOC
,
694 SUN4I_CODEC_ADC_FIFOC_EN_AD
, 0,
697 /* Digital parts of the DACs */
698 SND_SOC_DAPM_SUPPLY("DAC", SUN4I_CODEC_DAC_DPC
,
699 SUN4I_CODEC_DAC_DPC_EN_DA
, 0,
702 /* Analog parts of the ADCs */
703 SND_SOC_DAPM_ADC("Left ADC", "Codec Capture", SUN4I_CODEC_ADC_ACTL
,
704 SUN4I_CODEC_ADC_ACTL_ADC_L_EN
, 0),
705 SND_SOC_DAPM_ADC("Right ADC", "Codec Capture", SUN4I_CODEC_ADC_ACTL
,
706 SUN4I_CODEC_ADC_ACTL_ADC_R_EN
, 0),
708 /* Analog parts of the DACs */
709 SND_SOC_DAPM_DAC("Left DAC", "Codec Playback", SUN4I_CODEC_DAC_ACTL
,
710 SUN4I_CODEC_DAC_ACTL_DACAENL
, 0),
711 SND_SOC_DAPM_DAC("Right DAC", "Codec Playback", SUN4I_CODEC_DAC_ACTL
,
712 SUN4I_CODEC_DAC_ACTL_DACAENR
, 0),
715 SND_SOC_DAPM_MIXER("Left Mixer", SND_SOC_NOPM
, 0, 0,
716 sun4i_codec_left_mixer_controls
,
717 ARRAY_SIZE(sun4i_codec_left_mixer_controls
)),
718 SND_SOC_DAPM_MIXER("Right Mixer", SND_SOC_NOPM
, 0, 0,
719 sun4i_codec_right_mixer_controls
,
720 ARRAY_SIZE(sun4i_codec_right_mixer_controls
)),
722 /* Global Mixer Enable */
723 SND_SOC_DAPM_SUPPLY("Mixer Enable", SUN4I_CODEC_DAC_ACTL
,
724 SUN4I_CODEC_DAC_ACTL_MIXEN
, 0, NULL
, 0),
727 SND_SOC_DAPM_SUPPLY("VMIC", SUN4I_CODEC_ADC_ACTL
,
728 SUN4I_CODEC_ADC_ACTL_VMICEN
, 0, NULL
, 0),
730 /* Mic Pre-Amplifiers */
731 SND_SOC_DAPM_PGA("MIC1 Pre-Amplifier", SUN4I_CODEC_ADC_ACTL
,
732 SUN4I_CODEC_ADC_ACTL_PREG1EN
, 0, NULL
, 0),
734 /* Power Amplifier */
735 SND_SOC_DAPM_MIXER("Power Amplifier", SUN4I_CODEC_ADC_ACTL
,
736 SUN4I_CODEC_ADC_ACTL_PA_EN
, 0,
737 sun4i_codec_pa_mixer_controls
,
738 ARRAY_SIZE(sun4i_codec_pa_mixer_controls
)),
739 SND_SOC_DAPM_SWITCH("Power Amplifier Mute", SND_SOC_NOPM
, 0, 0,
740 &sun4i_codec_pa_mute
),
742 SND_SOC_DAPM_INPUT("Mic1"),
744 SND_SOC_DAPM_OUTPUT("HP Right"),
745 SND_SOC_DAPM_OUTPUT("HP Left"),
748 static const struct snd_soc_dapm_route sun4i_codec_codec_dapm_routes
[] = {
749 /* Left ADC / DAC Routes */
750 { "Left ADC", NULL
, "ADC" },
751 { "Left DAC", NULL
, "DAC" },
753 /* Right ADC / DAC Routes */
754 { "Right ADC", NULL
, "ADC" },
755 { "Right DAC", NULL
, "DAC" },
757 /* Right Mixer Routes */
758 { "Right Mixer", NULL
, "Mixer Enable" },
759 { "Right Mixer", "Left DAC Playback Switch", "Left DAC" },
760 { "Right Mixer", "Right DAC Playback Switch", "Right DAC" },
762 /* Left Mixer Routes */
763 { "Left Mixer", NULL
, "Mixer Enable" },
764 { "Left Mixer", "Left DAC Playback Switch", "Left DAC" },
766 /* Power Amplifier Routes */
767 { "Power Amplifier", "Mixer Playback Switch", "Left Mixer" },
768 { "Power Amplifier", "Mixer Playback Switch", "Right Mixer" },
769 { "Power Amplifier", "DAC Playback Switch", "Left DAC" },
770 { "Power Amplifier", "DAC Playback Switch", "Right DAC" },
772 /* Headphone Output Routes */
773 { "Power Amplifier Mute", "Switch", "Power Amplifier" },
774 { "HP Right", NULL
, "Power Amplifier Mute" },
775 { "HP Left", NULL
, "Power Amplifier Mute" },
778 { "Left ADC", NULL
, "MIC1 Pre-Amplifier" },
779 { "Right ADC", NULL
, "MIC1 Pre-Amplifier" },
780 { "MIC1 Pre-Amplifier", NULL
, "Mic1"},
781 { "Mic1", NULL
, "VMIC" },
784 static const struct snd_soc_codec_driver sun4i_codec_codec
= {
785 .component_driver
= {
786 .controls
= sun4i_codec_controls
,
787 .num_controls
= ARRAY_SIZE(sun4i_codec_controls
),
788 .dapm_widgets
= sun4i_codec_codec_dapm_widgets
,
789 .num_dapm_widgets
= ARRAY_SIZE(sun4i_codec_codec_dapm_widgets
),
790 .dapm_routes
= sun4i_codec_codec_dapm_routes
,
791 .num_dapm_routes
= ARRAY_SIZE(sun4i_codec_codec_dapm_routes
),
795 /*** sun6i Codec ***/
798 static const struct snd_kcontrol_new sun6i_codec_mixer_controls
[] = {
799 SOC_DAPM_DOUBLE("DAC Playback Switch",
800 SUN6I_CODEC_OM_DACA_CTRL
,
801 SUN6I_CODEC_OM_DACA_CTRL_LMIX_DACL
,
802 SUN6I_CODEC_OM_DACA_CTRL_RMIX_DACR
, 1, 0),
803 SOC_DAPM_DOUBLE("DAC Reversed Playback Switch",
804 SUN6I_CODEC_OM_DACA_CTRL
,
805 SUN6I_CODEC_OM_DACA_CTRL_LMIX_DACR
,
806 SUN6I_CODEC_OM_DACA_CTRL_RMIX_DACL
, 1, 0),
807 SOC_DAPM_DOUBLE("Line In Playback Switch",
808 SUN6I_CODEC_OM_DACA_CTRL
,
809 SUN6I_CODEC_OM_DACA_CTRL_LMIX_LINEINL
,
810 SUN6I_CODEC_OM_DACA_CTRL_RMIX_LINEINR
, 1, 0),
811 SOC_DAPM_DOUBLE("Mic1 Playback Switch",
812 SUN6I_CODEC_OM_DACA_CTRL
,
813 SUN6I_CODEC_OM_DACA_CTRL_LMIX_MIC1
,
814 SUN6I_CODEC_OM_DACA_CTRL_RMIX_MIC1
, 1, 0),
815 SOC_DAPM_DOUBLE("Mic2 Playback Switch",
816 SUN6I_CODEC_OM_DACA_CTRL
,
817 SUN6I_CODEC_OM_DACA_CTRL_LMIX_MIC2
,
818 SUN6I_CODEC_OM_DACA_CTRL_RMIX_MIC2
, 1, 0),
821 /* ADC mixer controls */
822 static const struct snd_kcontrol_new sun6i_codec_adc_mixer_controls
[] = {
823 SOC_DAPM_DOUBLE("Mixer Capture Switch",
824 SUN6I_CODEC_ADC_ACTL
,
825 SUN6I_CODEC_ADC_ACTL_LADCMIX_OMIXL
,
826 SUN6I_CODEC_ADC_ACTL_RADCMIX_OMIXR
, 1, 0),
827 SOC_DAPM_DOUBLE("Mixer Reversed Capture Switch",
828 SUN6I_CODEC_ADC_ACTL
,
829 SUN6I_CODEC_ADC_ACTL_LADCMIX_OMIXR
,
830 SUN6I_CODEC_ADC_ACTL_RADCMIX_OMIXL
, 1, 0),
831 SOC_DAPM_DOUBLE("Line In Capture Switch",
832 SUN6I_CODEC_ADC_ACTL
,
833 SUN6I_CODEC_ADC_ACTL_LADCMIX_LINEINL
,
834 SUN6I_CODEC_ADC_ACTL_RADCMIX_LINEINR
, 1, 0),
835 SOC_DAPM_DOUBLE("Mic1 Capture Switch",
836 SUN6I_CODEC_ADC_ACTL
,
837 SUN6I_CODEC_ADC_ACTL_LADCMIX_MIC1
,
838 SUN6I_CODEC_ADC_ACTL_RADCMIX_MIC1
, 1, 0),
839 SOC_DAPM_DOUBLE("Mic2 Capture Switch",
840 SUN6I_CODEC_ADC_ACTL
,
841 SUN6I_CODEC_ADC_ACTL_LADCMIX_MIC2
,
842 SUN6I_CODEC_ADC_ACTL_RADCMIX_MIC2
, 1, 0),
845 /* headphone controls */
846 static const char * const sun6i_codec_hp_src_enum_text
[] = {
850 static SOC_ENUM_DOUBLE_DECL(sun6i_codec_hp_src_enum
,
851 SUN6I_CODEC_OM_DACA_CTRL
,
852 SUN6I_CODEC_OM_DACA_CTRL_LHPIS
,
853 SUN6I_CODEC_OM_DACA_CTRL_RHPIS
,
854 sun6i_codec_hp_src_enum_text
);
856 static const struct snd_kcontrol_new sun6i_codec_hp_src
[] = {
857 SOC_DAPM_ENUM("Headphone Source Playback Route",
858 sun6i_codec_hp_src_enum
),
861 /* microphone controls */
862 static const char * const sun6i_codec_mic2_src_enum_text
[] = {
866 static SOC_ENUM_SINGLE_DECL(sun6i_codec_mic2_src_enum
,
867 SUN6I_CODEC_MIC_CTRL
,
868 SUN6I_CODEC_MIC_CTRL_MIC2SLT
,
869 sun6i_codec_mic2_src_enum_text
);
871 static const struct snd_kcontrol_new sun6i_codec_mic2_src
[] = {
872 SOC_DAPM_ENUM("Mic2 Amplifier Source Route",
873 sun6i_codec_mic2_src_enum
),
876 /* line out controls */
877 static const char * const sun6i_codec_lineout_src_enum_text
[] = {
878 "Stereo", "Mono Differential",
881 static SOC_ENUM_DOUBLE_DECL(sun6i_codec_lineout_src_enum
,
882 SUN6I_CODEC_MIC_CTRL
,
883 SUN6I_CODEC_MIC_CTRL_LINEOUTLSRC
,
884 SUN6I_CODEC_MIC_CTRL_LINEOUTRSRC
,
885 sun6i_codec_lineout_src_enum_text
);
887 static const struct snd_kcontrol_new sun6i_codec_lineout_src
[] = {
888 SOC_DAPM_ENUM("Line Out Source Playback Route",
889 sun6i_codec_lineout_src_enum
),
892 /* volume / mute controls */
893 static const DECLARE_TLV_DB_SCALE(sun6i_codec_dvol_scale
, -7308, 116, 0);
894 static const DECLARE_TLV_DB_SCALE(sun6i_codec_hp_vol_scale
, -6300, 100, 1);
895 static const DECLARE_TLV_DB_SCALE(sun6i_codec_out_mixer_pregain_scale
,
897 static const DECLARE_TLV_DB_RANGE(sun6i_codec_lineout_vol_scale
,
898 0, 1, TLV_DB_SCALE_ITEM(TLV_DB_GAIN_MUTE
, 0, 1),
899 2, 31, TLV_DB_SCALE_ITEM(-4350, 150, 0),
901 static const DECLARE_TLV_DB_RANGE(sun6i_codec_mic_gain_scale
,
902 0, 0, TLV_DB_SCALE_ITEM(0, 0, 0),
903 1, 7, TLV_DB_SCALE_ITEM(2400, 300, 0),
906 static const struct snd_kcontrol_new sun6i_codec_codec_widgets
[] = {
907 SOC_SINGLE_TLV("DAC Playback Volume", SUN4I_CODEC_DAC_DPC
,
908 SUN4I_CODEC_DAC_DPC_DVOL
, 0x3f, 1,
909 sun6i_codec_dvol_scale
),
910 SOC_SINGLE_TLV("Headphone Playback Volume",
911 SUN6I_CODEC_OM_DACA_CTRL
,
912 SUN6I_CODEC_OM_DACA_CTRL_HPVOL
, 0x3f, 0,
913 sun6i_codec_hp_vol_scale
),
914 SOC_SINGLE_TLV("Line Out Playback Volume",
915 SUN6I_CODEC_MIC_CTRL
,
916 SUN6I_CODEC_MIC_CTRL_LINEOUTVC
, 0x1f, 0,
917 sun6i_codec_lineout_vol_scale
),
918 SOC_DOUBLE("Headphone Playback Switch",
919 SUN6I_CODEC_OM_DACA_CTRL
,
920 SUN6I_CODEC_OM_DACA_CTRL_LHPPAMUTE
,
921 SUN6I_CODEC_OM_DACA_CTRL_RHPPAMUTE
, 1, 0),
922 SOC_DOUBLE("Line Out Playback Switch",
923 SUN6I_CODEC_MIC_CTRL
,
924 SUN6I_CODEC_MIC_CTRL_LINEOUTLEN
,
925 SUN6I_CODEC_MIC_CTRL_LINEOUTREN
, 1, 0),
926 /* Mixer pre-gains */
927 SOC_SINGLE_TLV("Line In Playback Volume",
928 SUN6I_CODEC_OM_PA_CTRL
, SUN6I_CODEC_OM_PA_CTRL_LINEING
,
929 0x7, 0, sun6i_codec_out_mixer_pregain_scale
),
930 SOC_SINGLE_TLV("Mic1 Playback Volume",
931 SUN6I_CODEC_OM_PA_CTRL
, SUN6I_CODEC_OM_PA_CTRL_MIC1G
,
932 0x7, 0, sun6i_codec_out_mixer_pregain_scale
),
933 SOC_SINGLE_TLV("Mic2 Playback Volume",
934 SUN6I_CODEC_OM_PA_CTRL
, SUN6I_CODEC_OM_PA_CTRL_MIC2G
,
935 0x7, 0, sun6i_codec_out_mixer_pregain_scale
),
937 /* Microphone Amp boost gains */
938 SOC_SINGLE_TLV("Mic1 Boost Volume", SUN6I_CODEC_MIC_CTRL
,
939 SUN6I_CODEC_MIC_CTRL_MIC1BOOST
, 0x7, 0,
940 sun6i_codec_mic_gain_scale
),
941 SOC_SINGLE_TLV("Mic2 Boost Volume", SUN6I_CODEC_MIC_CTRL
,
942 SUN6I_CODEC_MIC_CTRL_MIC2BOOST
, 0x7, 0,
943 sun6i_codec_mic_gain_scale
),
944 SOC_DOUBLE_TLV("ADC Capture Volume",
945 SUN6I_CODEC_ADC_ACTL
, SUN6I_CODEC_ADC_ACTL_ADCLG
,
946 SUN6I_CODEC_ADC_ACTL_ADCRG
, 0x7, 0,
947 sun6i_codec_out_mixer_pregain_scale
),
950 static const struct snd_soc_dapm_widget sun6i_codec_codec_dapm_widgets
[] = {
951 /* Microphone inputs */
952 SND_SOC_DAPM_INPUT("MIC1"),
953 SND_SOC_DAPM_INPUT("MIC2"),
954 SND_SOC_DAPM_INPUT("MIC3"),
956 /* Microphone Bias */
957 SND_SOC_DAPM_SUPPLY("HBIAS", SUN6I_CODEC_MIC_CTRL
,
958 SUN6I_CODEC_MIC_CTRL_HBIASEN
, 0, NULL
, 0),
959 SND_SOC_DAPM_SUPPLY("MBIAS", SUN6I_CODEC_MIC_CTRL
,
960 SUN6I_CODEC_MIC_CTRL_MBIASEN
, 0, NULL
, 0),
963 SND_SOC_DAPM_MUX("Mic2 Amplifier Source Route",
964 SND_SOC_NOPM
, 0, 0, sun6i_codec_mic2_src
),
965 SND_SOC_DAPM_PGA("Mic1 Amplifier", SUN6I_CODEC_MIC_CTRL
,
966 SUN6I_CODEC_MIC_CTRL_MIC1AMPEN
, 0, NULL
, 0),
967 SND_SOC_DAPM_PGA("Mic2 Amplifier", SUN6I_CODEC_MIC_CTRL
,
968 SUN6I_CODEC_MIC_CTRL_MIC2AMPEN
, 0, NULL
, 0),
971 SND_SOC_DAPM_INPUT("LINEIN"),
973 /* Digital parts of the ADCs */
974 SND_SOC_DAPM_SUPPLY("ADC Enable", SUN6I_CODEC_ADC_FIFOC
,
975 SUN6I_CODEC_ADC_FIFOC_EN_AD
, 0,
978 /* Analog parts of the ADCs */
979 SND_SOC_DAPM_ADC("Left ADC", "Codec Capture", SUN6I_CODEC_ADC_ACTL
,
980 SUN6I_CODEC_ADC_ACTL_ADCLEN
, 0),
981 SND_SOC_DAPM_ADC("Right ADC", "Codec Capture", SUN6I_CODEC_ADC_ACTL
,
982 SUN6I_CODEC_ADC_ACTL_ADCREN
, 0),
985 SOC_MIXER_ARRAY("Left ADC Mixer", SND_SOC_NOPM
, 0, 0,
986 sun6i_codec_adc_mixer_controls
),
987 SOC_MIXER_ARRAY("Right ADC Mixer", SND_SOC_NOPM
, 0, 0,
988 sun6i_codec_adc_mixer_controls
),
990 /* Digital parts of the DACs */
991 SND_SOC_DAPM_SUPPLY("DAC Enable", SUN4I_CODEC_DAC_DPC
,
992 SUN4I_CODEC_DAC_DPC_EN_DA
, 0,
995 /* Analog parts of the DACs */
996 SND_SOC_DAPM_DAC("Left DAC", "Codec Playback",
997 SUN6I_CODEC_OM_DACA_CTRL
,
998 SUN6I_CODEC_OM_DACA_CTRL_DACALEN
, 0),
999 SND_SOC_DAPM_DAC("Right DAC", "Codec Playback",
1000 SUN6I_CODEC_OM_DACA_CTRL
,
1001 SUN6I_CODEC_OM_DACA_CTRL_DACAREN
, 0),
1004 SOC_MIXER_ARRAY("Left Mixer", SUN6I_CODEC_OM_DACA_CTRL
,
1005 SUN6I_CODEC_OM_DACA_CTRL_LMIXEN
, 0,
1006 sun6i_codec_mixer_controls
),
1007 SOC_MIXER_ARRAY("Right Mixer", SUN6I_CODEC_OM_DACA_CTRL
,
1008 SUN6I_CODEC_OM_DACA_CTRL_RMIXEN
, 0,
1009 sun6i_codec_mixer_controls
),
1011 /* Headphone output path */
1012 SND_SOC_DAPM_MUX("Headphone Source Playback Route",
1013 SND_SOC_NOPM
, 0, 0, sun6i_codec_hp_src
),
1014 SND_SOC_DAPM_OUT_DRV("Headphone Amp", SUN6I_CODEC_OM_PA_CTRL
,
1015 SUN6I_CODEC_OM_PA_CTRL_HPPAEN
, 0, NULL
, 0),
1016 SND_SOC_DAPM_SUPPLY("HPCOM Protection", SUN6I_CODEC_OM_PA_CTRL
,
1017 SUN6I_CODEC_OM_PA_CTRL_COMPTEN
, 0, NULL
, 0),
1018 SND_SOC_DAPM_REG(snd_soc_dapm_supply
, "HPCOM", SUN6I_CODEC_OM_PA_CTRL
,
1019 SUN6I_CODEC_OM_PA_CTRL_HPCOM_CTL
, 0x3, 0x3, 0),
1020 SND_SOC_DAPM_OUTPUT("HP"),
1023 SND_SOC_DAPM_MUX("Line Out Source Playback Route",
1024 SND_SOC_NOPM
, 0, 0, sun6i_codec_lineout_src
),
1025 SND_SOC_DAPM_OUTPUT("LINEOUT"),
1028 static const struct snd_soc_dapm_route sun6i_codec_codec_dapm_routes
[] = {
1030 { "Left DAC", NULL
, "DAC Enable" },
1031 { "Right DAC", NULL
, "DAC Enable" },
1033 /* Microphone Routes */
1034 { "Mic1 Amplifier", NULL
, "MIC1"},
1035 { "Mic2 Amplifier Source Route", "Mic2", "MIC2" },
1036 { "Mic2 Amplifier Source Route", "Mic3", "MIC3" },
1037 { "Mic2 Amplifier", NULL
, "Mic2 Amplifier Source Route"},
1039 /* Left Mixer Routes */
1040 { "Left Mixer", "DAC Playback Switch", "Left DAC" },
1041 { "Left Mixer", "DAC Reversed Playback Switch", "Right DAC" },
1042 { "Left Mixer", "Line In Playback Switch", "LINEIN" },
1043 { "Left Mixer", "Mic1 Playback Switch", "Mic1 Amplifier" },
1044 { "Left Mixer", "Mic2 Playback Switch", "Mic2 Amplifier" },
1046 /* Right Mixer Routes */
1047 { "Right Mixer", "DAC Playback Switch", "Right DAC" },
1048 { "Right Mixer", "DAC Reversed Playback Switch", "Left DAC" },
1049 { "Right Mixer", "Line In Playback Switch", "LINEIN" },
1050 { "Right Mixer", "Mic1 Playback Switch", "Mic1 Amplifier" },
1051 { "Right Mixer", "Mic2 Playback Switch", "Mic2 Amplifier" },
1053 /* Left ADC Mixer Routes */
1054 { "Left ADC Mixer", "Mixer Capture Switch", "Left Mixer" },
1055 { "Left ADC Mixer", "Mixer Reversed Capture Switch", "Right Mixer" },
1056 { "Left ADC Mixer", "Line In Capture Switch", "LINEIN" },
1057 { "Left ADC Mixer", "Mic1 Capture Switch", "Mic1 Amplifier" },
1058 { "Left ADC Mixer", "Mic2 Capture Switch", "Mic2 Amplifier" },
1060 /* Right ADC Mixer Routes */
1061 { "Right ADC Mixer", "Mixer Capture Switch", "Right Mixer" },
1062 { "Right ADC Mixer", "Mixer Reversed Capture Switch", "Left Mixer" },
1063 { "Right ADC Mixer", "Line In Capture Switch", "LINEIN" },
1064 { "Right ADC Mixer", "Mic1 Capture Switch", "Mic1 Amplifier" },
1065 { "Right ADC Mixer", "Mic2 Capture Switch", "Mic2 Amplifier" },
1067 /* Headphone Routes */
1068 { "Headphone Source Playback Route", "DAC", "Left DAC" },
1069 { "Headphone Source Playback Route", "DAC", "Right DAC" },
1070 { "Headphone Source Playback Route", "Mixer", "Left Mixer" },
1071 { "Headphone Source Playback Route", "Mixer", "Right Mixer" },
1072 { "Headphone Amp", NULL
, "Headphone Source Playback Route" },
1073 { "HP", NULL
, "Headphone Amp" },
1074 { "HPCOM", NULL
, "HPCOM Protection" },
1076 /* Line Out Routes */
1077 { "Line Out Source Playback Route", "Stereo", "Left Mixer" },
1078 { "Line Out Source Playback Route", "Stereo", "Right Mixer" },
1079 { "Line Out Source Playback Route", "Mono Differential", "Left Mixer" },
1080 { "Line Out Source Playback Route", "Mono Differential", "Right Mixer" },
1081 { "LINEOUT", NULL
, "Line Out Source Playback Route" },
1084 { "Left ADC", NULL
, "ADC Enable" },
1085 { "Right ADC", NULL
, "ADC Enable" },
1086 { "Left ADC", NULL
, "Left ADC Mixer" },
1087 { "Right ADC", NULL
, "Right ADC Mixer" },
1090 static const struct snd_soc_codec_driver sun6i_codec_codec
= {
1091 .component_driver
= {
1092 .controls
= sun6i_codec_codec_widgets
,
1093 .num_controls
= ARRAY_SIZE(sun6i_codec_codec_widgets
),
1094 .dapm_widgets
= sun6i_codec_codec_dapm_widgets
,
1095 .num_dapm_widgets
= ARRAY_SIZE(sun6i_codec_codec_dapm_widgets
),
1096 .dapm_routes
= sun6i_codec_codec_dapm_routes
,
1097 .num_dapm_routes
= ARRAY_SIZE(sun6i_codec_codec_dapm_routes
),
1101 /* sun8i A23 codec */
1102 static const struct snd_kcontrol_new sun8i_a23_codec_codec_controls
[] = {
1103 SOC_SINGLE_TLV("DAC Playback Volume", SUN4I_CODEC_DAC_DPC
,
1104 SUN4I_CODEC_DAC_DPC_DVOL
, 0x3f, 1,
1105 sun6i_codec_dvol_scale
),
1108 static const struct snd_soc_dapm_widget sun8i_a23_codec_codec_widgets
[] = {
1109 /* Digital parts of the ADCs */
1110 SND_SOC_DAPM_SUPPLY("ADC Enable", SUN6I_CODEC_ADC_FIFOC
,
1111 SUN6I_CODEC_ADC_FIFOC_EN_AD
, 0, NULL
, 0),
1112 /* Digital parts of the DACs */
1113 SND_SOC_DAPM_SUPPLY("DAC Enable", SUN4I_CODEC_DAC_DPC
,
1114 SUN4I_CODEC_DAC_DPC_EN_DA
, 0, NULL
, 0),
1118 static const struct snd_soc_codec_driver sun8i_a23_codec_codec
= {
1119 .component_driver
= {
1120 .controls
= sun8i_a23_codec_codec_controls
,
1121 .num_controls
= ARRAY_SIZE(sun8i_a23_codec_codec_controls
),
1122 .dapm_widgets
= sun8i_a23_codec_codec_widgets
,
1123 .num_dapm_widgets
= ARRAY_SIZE(sun8i_a23_codec_codec_widgets
),
1127 static const struct snd_soc_component_driver sun4i_codec_component
= {
1128 .name
= "sun4i-codec",
1131 #define SUN4I_CODEC_RATES SNDRV_PCM_RATE_8000_192000
1132 #define SUN4I_CODEC_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | \
1133 SNDRV_PCM_FMTBIT_S32_LE)
1135 static int sun4i_codec_dai_probe(struct snd_soc_dai
*dai
)
1137 struct snd_soc_card
*card
= snd_soc_dai_get_drvdata(dai
);
1138 struct sun4i_codec
*scodec
= snd_soc_card_get_drvdata(card
);
1140 snd_soc_dai_init_dma_data(dai
, &scodec
->playback_dma_data
,
1141 &scodec
->capture_dma_data
);
1146 static struct snd_soc_dai_driver dummy_cpu_dai
= {
1147 .name
= "sun4i-codec-cpu-dai",
1148 .probe
= sun4i_codec_dai_probe
,
1150 .stream_name
= "Playback",
1153 .rates
= SUN4I_CODEC_RATES
,
1154 .formats
= SUN4I_CODEC_FORMATS
,
1158 .stream_name
= "Capture",
1161 .rates
= SUN4I_CODEC_RATES
,
1162 .formats
= SUN4I_CODEC_FORMATS
,
1167 static struct snd_soc_dai_link
*sun4i_codec_create_link(struct device
*dev
,
1170 struct snd_soc_dai_link
*link
= devm_kzalloc(dev
, sizeof(*link
),
1176 link
->stream_name
= "CDC PCM";
1177 link
->codec_dai_name
= "Codec";
1178 link
->cpu_dai_name
= dev_name(dev
);
1179 link
->codec_name
= dev_name(dev
);
1180 link
->platform_name
= dev_name(dev
);
1181 link
->dai_fmt
= SND_SOC_DAIFMT_I2S
;
1188 static int sun4i_codec_spk_event(struct snd_soc_dapm_widget
*w
,
1189 struct snd_kcontrol
*k
, int event
)
1191 struct sun4i_codec
*scodec
= snd_soc_card_get_drvdata(w
->dapm
->card
);
1193 gpiod_set_value_cansleep(scodec
->gpio_pa
,
1194 !!SND_SOC_DAPM_EVENT_ON(event
));
1199 static const struct snd_soc_dapm_widget sun4i_codec_card_dapm_widgets
[] = {
1200 SND_SOC_DAPM_SPK("Speaker", sun4i_codec_spk_event
),
1203 static const struct snd_soc_dapm_route sun4i_codec_card_dapm_routes
[] = {
1204 { "Speaker", NULL
, "HP Right" },
1205 { "Speaker", NULL
, "HP Left" },
1208 static struct snd_soc_card
*sun4i_codec_create_card(struct device
*dev
)
1210 struct snd_soc_card
*card
;
1212 card
= devm_kzalloc(dev
, sizeof(*card
), GFP_KERNEL
);
1214 return ERR_PTR(-ENOMEM
);
1216 card
->dai_link
= sun4i_codec_create_link(dev
, &card
->num_links
);
1217 if (!card
->dai_link
)
1218 return ERR_PTR(-ENOMEM
);
1221 card
->name
= "sun4i-codec";
1222 card
->dapm_widgets
= sun4i_codec_card_dapm_widgets
;
1223 card
->num_dapm_widgets
= ARRAY_SIZE(sun4i_codec_card_dapm_widgets
);
1224 card
->dapm_routes
= sun4i_codec_card_dapm_routes
;
1225 card
->num_dapm_routes
= ARRAY_SIZE(sun4i_codec_card_dapm_routes
);
1230 static const struct snd_soc_dapm_widget sun6i_codec_card_dapm_widgets
[] = {
1231 SND_SOC_DAPM_HP("Headphone", NULL
),
1232 SND_SOC_DAPM_LINE("Line In", NULL
),
1233 SND_SOC_DAPM_LINE("Line Out", NULL
),
1234 SND_SOC_DAPM_MIC("Headset Mic", NULL
),
1235 SND_SOC_DAPM_MIC("Mic", NULL
),
1236 SND_SOC_DAPM_SPK("Speaker", sun4i_codec_spk_event
),
1239 static struct snd_soc_card
*sun6i_codec_create_card(struct device
*dev
)
1241 struct snd_soc_card
*card
;
1244 card
= devm_kzalloc(dev
, sizeof(*card
), GFP_KERNEL
);
1246 return ERR_PTR(-ENOMEM
);
1248 card
->dai_link
= sun4i_codec_create_link(dev
, &card
->num_links
);
1249 if (!card
->dai_link
)
1250 return ERR_PTR(-ENOMEM
);
1253 card
->name
= "A31 Audio Codec";
1254 card
->dapm_widgets
= sun6i_codec_card_dapm_widgets
;
1255 card
->num_dapm_widgets
= ARRAY_SIZE(sun6i_codec_card_dapm_widgets
);
1256 card
->fully_routed
= true;
1258 ret
= snd_soc_of_parse_audio_routing(card
, "allwinner,audio-routing");
1260 dev_warn(dev
, "failed to parse audio-routing: %d\n", ret
);
1265 /* Connect digital side enables to analog side widgets */
1266 static const struct snd_soc_dapm_route sun8i_codec_card_routes
[] = {
1268 { "Left ADC", NULL
, "ADC Enable" },
1269 { "Right ADC", NULL
, "ADC Enable" },
1270 { "Codec Capture", NULL
, "Left ADC" },
1271 { "Codec Capture", NULL
, "Right ADC" },
1274 { "Left DAC", NULL
, "DAC Enable" },
1275 { "Right DAC", NULL
, "DAC Enable" },
1276 { "Left DAC", NULL
, "Codec Playback" },
1277 { "Right DAC", NULL
, "Codec Playback" },
1280 static struct snd_soc_aux_dev aux_dev
= {
1281 .name
= "Codec Analog Controls",
1284 static struct snd_soc_card
*sun8i_a23_codec_create_card(struct device
*dev
)
1286 struct snd_soc_card
*card
;
1289 card
= devm_kzalloc(dev
, sizeof(*card
), GFP_KERNEL
);
1291 return ERR_PTR(-ENOMEM
);
1293 aux_dev
.codec_of_node
= of_parse_phandle(dev
->of_node
,
1294 "allwinner,codec-analog-controls",
1296 if (!aux_dev
.codec_of_node
) {
1297 dev_err(dev
, "Can't find analog controls for codec.\n");
1298 return ERR_PTR(-EINVAL
);
1301 card
->dai_link
= sun4i_codec_create_link(dev
, &card
->num_links
);
1302 if (!card
->dai_link
)
1303 return ERR_PTR(-ENOMEM
);
1306 card
->name
= "A23 Audio Codec";
1307 card
->dapm_widgets
= sun6i_codec_card_dapm_widgets
;
1308 card
->num_dapm_widgets
= ARRAY_SIZE(sun6i_codec_card_dapm_widgets
);
1309 card
->dapm_routes
= sun8i_codec_card_routes
;
1310 card
->num_dapm_routes
= ARRAY_SIZE(sun8i_codec_card_routes
);
1311 card
->aux_dev
= &aux_dev
;
1312 card
->num_aux_devs
= 1;
1313 card
->fully_routed
= true;
1315 ret
= snd_soc_of_parse_audio_routing(card
, "allwinner,audio-routing");
1317 dev_warn(dev
, "failed to parse audio-routing: %d\n", ret
);
1322 static struct snd_soc_card
*sun8i_h3_codec_create_card(struct device
*dev
)
1324 struct snd_soc_card
*card
;
1327 card
= devm_kzalloc(dev
, sizeof(*card
), GFP_KERNEL
);
1329 return ERR_PTR(-ENOMEM
);
1331 aux_dev
.codec_of_node
= of_parse_phandle(dev
->of_node
,
1332 "allwinner,codec-analog-controls",
1334 if (!aux_dev
.codec_of_node
) {
1335 dev_err(dev
, "Can't find analog controls for codec.\n");
1336 return ERR_PTR(-EINVAL
);
1339 card
->dai_link
= sun4i_codec_create_link(dev
, &card
->num_links
);
1340 if (!card
->dai_link
)
1341 return ERR_PTR(-ENOMEM
);
1344 card
->name
= "H3 Audio Codec";
1345 card
->dapm_widgets
= sun6i_codec_card_dapm_widgets
;
1346 card
->num_dapm_widgets
= ARRAY_SIZE(sun6i_codec_card_dapm_widgets
);
1347 card
->dapm_routes
= sun8i_codec_card_routes
;
1348 card
->num_dapm_routes
= ARRAY_SIZE(sun8i_codec_card_routes
);
1349 card
->aux_dev
= &aux_dev
;
1350 card
->num_aux_devs
= 1;
1351 card
->fully_routed
= true;
1353 ret
= snd_soc_of_parse_audio_routing(card
, "allwinner,audio-routing");
1355 dev_warn(dev
, "failed to parse audio-routing: %d\n", ret
);
1360 static struct snd_soc_card
*sun8i_v3s_codec_create_card(struct device
*dev
)
1362 struct snd_soc_card
*card
;
1365 card
= devm_kzalloc(dev
, sizeof(*card
), GFP_KERNEL
);
1367 return ERR_PTR(-ENOMEM
);
1369 aux_dev
.codec_of_node
= of_parse_phandle(dev
->of_node
,
1370 "allwinner,codec-analog-controls",
1372 if (!aux_dev
.codec_of_node
) {
1373 dev_err(dev
, "Can't find analog controls for codec.\n");
1374 return ERR_PTR(-EINVAL
);
1377 card
->dai_link
= sun4i_codec_create_link(dev
, &card
->num_links
);
1378 if (!card
->dai_link
)
1379 return ERR_PTR(-ENOMEM
);
1382 card
->name
= "V3s Audio Codec";
1383 card
->dapm_widgets
= sun6i_codec_card_dapm_widgets
;
1384 card
->num_dapm_widgets
= ARRAY_SIZE(sun6i_codec_card_dapm_widgets
);
1385 card
->dapm_routes
= sun8i_codec_card_routes
;
1386 card
->num_dapm_routes
= ARRAY_SIZE(sun8i_codec_card_routes
);
1387 card
->aux_dev
= &aux_dev
;
1388 card
->num_aux_devs
= 1;
1389 card
->fully_routed
= true;
1391 ret
= snd_soc_of_parse_audio_routing(card
, "allwinner,audio-routing");
1393 dev_warn(dev
, "failed to parse audio-routing: %d\n", ret
);
1398 static const struct regmap_config sun4i_codec_regmap_config
= {
1402 .max_register
= SUN4I_CODEC_ADC_RXCNT
,
1405 static const struct regmap_config sun6i_codec_regmap_config
= {
1409 .max_register
= SUN6I_CODEC_HMIC_DATA
,
1412 static const struct regmap_config sun7i_codec_regmap_config
= {
1416 .max_register
= SUN7I_CODEC_AC_MIC_PHONE_CAL
,
1419 static const struct regmap_config sun8i_a23_codec_regmap_config
= {
1423 .max_register
= SUN8I_A23_CODEC_ADC_RXCNT
,
1426 static const struct regmap_config sun8i_h3_codec_regmap_config
= {
1430 .max_register
= SUN8I_H3_CODEC_ADC_DBG
,
1433 static const struct regmap_config sun8i_v3s_codec_regmap_config
= {
1437 .max_register
= SUN8I_H3_CODEC_ADC_DBG
,
1440 struct sun4i_codec_quirks
{
1441 const struct regmap_config
*regmap_config
;
1442 const struct snd_soc_codec_driver
*codec
;
1443 struct snd_soc_card
* (*create_card
)(struct device
*dev
);
1444 struct reg_field reg_adc_fifoc
; /* used for regmap_field */
1445 unsigned int reg_dac_txdata
; /* TX FIFO offset for DMA config */
1446 unsigned int reg_adc_rxdata
; /* RX FIFO offset for DMA config */
1450 static const struct sun4i_codec_quirks sun4i_codec_quirks
= {
1451 .regmap_config
= &sun4i_codec_regmap_config
,
1452 .codec
= &sun4i_codec_codec
,
1453 .create_card
= sun4i_codec_create_card
,
1454 .reg_adc_fifoc
= REG_FIELD(SUN4I_CODEC_ADC_FIFOC
, 0, 31),
1455 .reg_dac_txdata
= SUN4I_CODEC_DAC_TXDATA
,
1456 .reg_adc_rxdata
= SUN4I_CODEC_ADC_RXDATA
,
1459 static const struct sun4i_codec_quirks sun6i_a31_codec_quirks
= {
1460 .regmap_config
= &sun6i_codec_regmap_config
,
1461 .codec
= &sun6i_codec_codec
,
1462 .create_card
= sun6i_codec_create_card
,
1463 .reg_adc_fifoc
= REG_FIELD(SUN6I_CODEC_ADC_FIFOC
, 0, 31),
1464 .reg_dac_txdata
= SUN4I_CODEC_DAC_TXDATA
,
1465 .reg_adc_rxdata
= SUN6I_CODEC_ADC_RXDATA
,
1469 static const struct sun4i_codec_quirks sun7i_codec_quirks
= {
1470 .regmap_config
= &sun7i_codec_regmap_config
,
1471 .codec
= &sun4i_codec_codec
,
1472 .create_card
= sun4i_codec_create_card
,
1473 .reg_adc_fifoc
= REG_FIELD(SUN4I_CODEC_ADC_FIFOC
, 0, 31),
1474 .reg_dac_txdata
= SUN4I_CODEC_DAC_TXDATA
,
1475 .reg_adc_rxdata
= SUN4I_CODEC_ADC_RXDATA
,
1478 static const struct sun4i_codec_quirks sun8i_a23_codec_quirks
= {
1479 .regmap_config
= &sun8i_a23_codec_regmap_config
,
1480 .codec
= &sun8i_a23_codec_codec
,
1481 .create_card
= sun8i_a23_codec_create_card
,
1482 .reg_adc_fifoc
= REG_FIELD(SUN6I_CODEC_ADC_FIFOC
, 0, 31),
1483 .reg_dac_txdata
= SUN4I_CODEC_DAC_TXDATA
,
1484 .reg_adc_rxdata
= SUN6I_CODEC_ADC_RXDATA
,
1488 static const struct sun4i_codec_quirks sun8i_h3_codec_quirks
= {
1489 .regmap_config
= &sun8i_h3_codec_regmap_config
,
1491 * TODO Share the codec structure with A23 for now.
1492 * This should be split out when adding digital audio
1493 * processing support for the H3.
1495 .codec
= &sun8i_a23_codec_codec
,
1496 .create_card
= sun8i_h3_codec_create_card
,
1497 .reg_adc_fifoc
= REG_FIELD(SUN6I_CODEC_ADC_FIFOC
, 0, 31),
1498 .reg_dac_txdata
= SUN8I_H3_CODEC_DAC_TXDATA
,
1499 .reg_adc_rxdata
= SUN6I_CODEC_ADC_RXDATA
,
1503 static const struct sun4i_codec_quirks sun8i_v3s_codec_quirks
= {
1504 .regmap_config
= &sun8i_v3s_codec_regmap_config
,
1506 * TODO The codec structure should be split out, like
1507 * H3, when adding digital audio processing support.
1509 .codec
= &sun8i_a23_codec_codec
,
1510 .create_card
= sun8i_v3s_codec_create_card
,
1511 .reg_adc_fifoc
= REG_FIELD(SUN6I_CODEC_ADC_FIFOC
, 0, 31),
1512 .reg_dac_txdata
= SUN8I_H3_CODEC_DAC_TXDATA
,
1513 .reg_adc_rxdata
= SUN6I_CODEC_ADC_RXDATA
,
1517 static const struct of_device_id sun4i_codec_of_match
[] = {
1519 .compatible
= "allwinner,sun4i-a10-codec",
1520 .data
= &sun4i_codec_quirks
,
1523 .compatible
= "allwinner,sun6i-a31-codec",
1524 .data
= &sun6i_a31_codec_quirks
,
1527 .compatible
= "allwinner,sun7i-a20-codec",
1528 .data
= &sun7i_codec_quirks
,
1531 .compatible
= "allwinner,sun8i-a23-codec",
1532 .data
= &sun8i_a23_codec_quirks
,
1535 .compatible
= "allwinner,sun8i-h3-codec",
1536 .data
= &sun8i_h3_codec_quirks
,
1539 .compatible
= "allwinner,sun8i-v3s-codec",
1540 .data
= &sun8i_v3s_codec_quirks
,
1544 MODULE_DEVICE_TABLE(of
, sun4i_codec_of_match
);
1546 static int sun4i_codec_probe(struct platform_device
*pdev
)
1548 struct snd_soc_card
*card
;
1549 struct sun4i_codec
*scodec
;
1550 const struct sun4i_codec_quirks
*quirks
;
1551 struct resource
*res
;
1555 scodec
= devm_kzalloc(&pdev
->dev
, sizeof(*scodec
), GFP_KERNEL
);
1559 scodec
->dev
= &pdev
->dev
;
1561 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1562 base
= devm_ioremap_resource(&pdev
->dev
, res
);
1564 dev_err(&pdev
->dev
, "Failed to map the registers\n");
1565 return PTR_ERR(base
);
1568 quirks
= of_device_get_match_data(&pdev
->dev
);
1569 if (quirks
== NULL
) {
1570 dev_err(&pdev
->dev
, "Failed to determine the quirks to use\n");
1574 scodec
->regmap
= devm_regmap_init_mmio(&pdev
->dev
, base
,
1575 quirks
->regmap_config
);
1576 if (IS_ERR(scodec
->regmap
)) {
1577 dev_err(&pdev
->dev
, "Failed to create our regmap\n");
1578 return PTR_ERR(scodec
->regmap
);
1581 /* Get the clocks from the DT */
1582 scodec
->clk_apb
= devm_clk_get(&pdev
->dev
, "apb");
1583 if (IS_ERR(scodec
->clk_apb
)) {
1584 dev_err(&pdev
->dev
, "Failed to get the APB clock\n");
1585 return PTR_ERR(scodec
->clk_apb
);
1588 scodec
->clk_module
= devm_clk_get(&pdev
->dev
, "codec");
1589 if (IS_ERR(scodec
->clk_module
)) {
1590 dev_err(&pdev
->dev
, "Failed to get the module clock\n");
1591 return PTR_ERR(scodec
->clk_module
);
1594 if (quirks
->has_reset
) {
1595 scodec
->rst
= devm_reset_control_get_exclusive(&pdev
->dev
,
1597 if (IS_ERR(scodec
->rst
)) {
1598 dev_err(&pdev
->dev
, "Failed to get reset control\n");
1599 return PTR_ERR(scodec
->rst
);
1603 scodec
->gpio_pa
= devm_gpiod_get_optional(&pdev
->dev
, "allwinner,pa",
1605 if (IS_ERR(scodec
->gpio_pa
)) {
1606 ret
= PTR_ERR(scodec
->gpio_pa
);
1607 if (ret
!= -EPROBE_DEFER
)
1608 dev_err(&pdev
->dev
, "Failed to get pa gpio: %d\n", ret
);
1612 /* reg_field setup */
1613 scodec
->reg_adc_fifoc
= devm_regmap_field_alloc(&pdev
->dev
,
1615 quirks
->reg_adc_fifoc
);
1616 if (IS_ERR(scodec
->reg_adc_fifoc
)) {
1617 ret
= PTR_ERR(scodec
->reg_adc_fifoc
);
1618 dev_err(&pdev
->dev
, "Failed to create regmap fields: %d\n",
1623 /* Enable the bus clock */
1624 if (clk_prepare_enable(scodec
->clk_apb
)) {
1625 dev_err(&pdev
->dev
, "Failed to enable the APB clock\n");
1629 /* Deassert the reset control */
1631 ret
= reset_control_deassert(scodec
->rst
);
1634 "Failed to deassert the reset control\n");
1635 goto err_clk_disable
;
1639 /* DMA configuration for TX FIFO */
1640 scodec
->playback_dma_data
.addr
= res
->start
+ quirks
->reg_dac_txdata
;
1641 scodec
->playback_dma_data
.maxburst
= 8;
1642 scodec
->playback_dma_data
.addr_width
= DMA_SLAVE_BUSWIDTH_2_BYTES
;
1644 /* DMA configuration for RX FIFO */
1645 scodec
->capture_dma_data
.addr
= res
->start
+ quirks
->reg_adc_rxdata
;
1646 scodec
->capture_dma_data
.maxburst
= 8;
1647 scodec
->capture_dma_data
.addr_width
= DMA_SLAVE_BUSWIDTH_2_BYTES
;
1649 ret
= snd_soc_register_codec(&pdev
->dev
, quirks
->codec
,
1650 &sun4i_codec_dai
, 1);
1652 dev_err(&pdev
->dev
, "Failed to register our codec\n");
1653 goto err_assert_reset
;
1656 ret
= devm_snd_soc_register_component(&pdev
->dev
,
1657 &sun4i_codec_component
,
1660 dev_err(&pdev
->dev
, "Failed to register our DAI\n");
1661 goto err_unregister_codec
;
1664 ret
= devm_snd_dmaengine_pcm_register(&pdev
->dev
, NULL
, 0);
1666 dev_err(&pdev
->dev
, "Failed to register against DMAEngine\n");
1667 goto err_unregister_codec
;
1670 card
= quirks
->create_card(&pdev
->dev
);
1672 ret
= PTR_ERR(card
);
1673 dev_err(&pdev
->dev
, "Failed to create our card\n");
1674 goto err_unregister_codec
;
1677 snd_soc_card_set_drvdata(card
, scodec
);
1679 ret
= snd_soc_register_card(card
);
1681 dev_err(&pdev
->dev
, "Failed to register our card\n");
1682 goto err_unregister_codec
;
1687 err_unregister_codec
:
1688 snd_soc_unregister_codec(&pdev
->dev
);
1691 reset_control_assert(scodec
->rst
);
1693 clk_disable_unprepare(scodec
->clk_apb
);
1697 static int sun4i_codec_remove(struct platform_device
*pdev
)
1699 struct snd_soc_card
*card
= platform_get_drvdata(pdev
);
1700 struct sun4i_codec
*scodec
= snd_soc_card_get_drvdata(card
);
1702 snd_soc_unregister_card(card
);
1703 snd_soc_unregister_codec(&pdev
->dev
);
1705 reset_control_assert(scodec
->rst
);
1706 clk_disable_unprepare(scodec
->clk_apb
);
1711 static struct platform_driver sun4i_codec_driver
= {
1713 .name
= "sun4i-codec",
1714 .of_match_table
= sun4i_codec_of_match
,
1716 .probe
= sun4i_codec_probe
,
1717 .remove
= sun4i_codec_remove
,
1719 module_platform_driver(sun4i_codec_driver
);
1721 MODULE_DESCRIPTION("Allwinner A10 codec driver");
1722 MODULE_AUTHOR("Emilio López <emilio@elopez.com.ar>");
1723 MODULE_AUTHOR("Jon Smirl <jonsmirl@gmail.com>");
1724 MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
1725 MODULE_AUTHOR("Chen-Yu Tsai <wens@csie.org>");
1726 MODULE_LICENSE("GPL");