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_MICG (20)
68 #define SUN4I_CODEC_DAC_ACTL_LDACLMIXS (15)
69 #define SUN4I_CODEC_DAC_ACTL_RDACRMIXS (14)
70 #define SUN4I_CODEC_DAC_ACTL_LDACRMIXS (13)
71 #define SUN4I_CODEC_DAC_ACTL_DACPAS (8)
72 #define SUN4I_CODEC_DAC_ACTL_MIXPAS (7)
73 #define SUN4I_CODEC_DAC_ACTL_PA_MUTE (6)
74 #define SUN4I_CODEC_DAC_ACTL_PA_VOL (0)
75 #define SUN4I_CODEC_DAC_TUNE (0x14)
76 #define SUN4I_CODEC_DAC_DEBUG (0x18)
78 /* Codec ADC digital controls and FIFO registers */
79 #define SUN4I_CODEC_ADC_FIFOC (0x1c)
80 #define SUN4I_CODEC_ADC_FIFOC_ADC_FS (29)
81 #define SUN4I_CODEC_ADC_FIFOC_EN_AD (28)
82 #define SUN4I_CODEC_ADC_FIFOC_RX_FIFO_MODE (24)
83 #define SUN4I_CODEC_ADC_FIFOC_RX_TRIG_LEVEL (8)
84 #define SUN4I_CODEC_ADC_FIFOC_MONO_EN (7)
85 #define SUN4I_CODEC_ADC_FIFOC_RX_SAMPLE_BITS (6)
86 #define SUN4I_CODEC_ADC_FIFOC_ADC_DRQ_EN (4)
87 #define SUN4I_CODEC_ADC_FIFOC_FIFO_FLUSH (0)
88 #define SUN4I_CODEC_ADC_FIFOS (0x20)
89 #define SUN4I_CODEC_ADC_RXDATA (0x24)
91 /* Codec ADC side analog signal controls */
92 #define SUN4I_CODEC_ADC_ACTL (0x28)
93 #define SUN4I_CODEC_ADC_ACTL_ADC_R_EN (31)
94 #define SUN4I_CODEC_ADC_ACTL_ADC_L_EN (30)
95 #define SUN4I_CODEC_ADC_ACTL_PREG1EN (29)
96 #define SUN4I_CODEC_ADC_ACTL_PREG2EN (28)
97 #define SUN4I_CODEC_ADC_ACTL_VMICEN (27)
98 #define SUN4I_CODEC_ADC_ACTL_VADCG (20)
99 #define SUN4I_CODEC_ADC_ACTL_ADCIS (17)
100 #define SUN4I_CODEC_ADC_ACTL_PA_EN (4)
101 #define SUN4I_CODEC_ADC_ACTL_DDE (3)
102 #define SUN4I_CODEC_ADC_DEBUG (0x2c)
105 #define SUN4I_CODEC_DAC_TXCNT (0x30)
106 #define SUN4I_CODEC_ADC_RXCNT (0x34)
108 /* Calibration register (sun7i only) */
109 #define SUN7I_CODEC_AC_DAC_CAL (0x38)
111 /* Microphone controls (sun7i only) */
112 #define SUN7I_CODEC_AC_MIC_PHONE_CAL (0x3c)
115 * sun6i specific registers
117 * sun6i shares the same digital control and FIFO registers as sun4i,
118 * but only the DAC digital controls are at the same offset. The others
119 * have been moved around to accommodate extra analog controls.
122 /* Codec DAC digital controls and FIFO registers */
123 #define SUN6I_CODEC_ADC_FIFOC (0x10)
124 #define SUN6I_CODEC_ADC_FIFOC_EN_AD (28)
125 #define SUN6I_CODEC_ADC_FIFOS (0x14)
126 #define SUN6I_CODEC_ADC_RXDATA (0x18)
128 /* Output mixer and gain controls */
129 #define SUN6I_CODEC_OM_DACA_CTRL (0x20)
130 #define SUN6I_CODEC_OM_DACA_CTRL_DACAREN (31)
131 #define SUN6I_CODEC_OM_DACA_CTRL_DACALEN (30)
132 #define SUN6I_CODEC_OM_DACA_CTRL_RMIXEN (29)
133 #define SUN6I_CODEC_OM_DACA_CTRL_LMIXEN (28)
134 #define SUN6I_CODEC_OM_DACA_CTRL_RMIX_MIC1 (23)
135 #define SUN6I_CODEC_OM_DACA_CTRL_RMIX_MIC2 (22)
136 #define SUN6I_CODEC_OM_DACA_CTRL_RMIX_PHONE (21)
137 #define SUN6I_CODEC_OM_DACA_CTRL_RMIX_PHONEP (20)
138 #define SUN6I_CODEC_OM_DACA_CTRL_RMIX_LINEINR (19)
139 #define SUN6I_CODEC_OM_DACA_CTRL_RMIX_DACR (18)
140 #define SUN6I_CODEC_OM_DACA_CTRL_RMIX_DACL (17)
141 #define SUN6I_CODEC_OM_DACA_CTRL_LMIX_MIC1 (16)
142 #define SUN6I_CODEC_OM_DACA_CTRL_LMIX_MIC2 (15)
143 #define SUN6I_CODEC_OM_DACA_CTRL_LMIX_PHONE (14)
144 #define SUN6I_CODEC_OM_DACA_CTRL_LMIX_PHONEN (13)
145 #define SUN6I_CODEC_OM_DACA_CTRL_LMIX_LINEINL (12)
146 #define SUN6I_CODEC_OM_DACA_CTRL_LMIX_DACL (11)
147 #define SUN6I_CODEC_OM_DACA_CTRL_LMIX_DACR (10)
148 #define SUN6I_CODEC_OM_DACA_CTRL_RHPIS (9)
149 #define SUN6I_CODEC_OM_DACA_CTRL_LHPIS (8)
150 #define SUN6I_CODEC_OM_DACA_CTRL_RHPPAMUTE (7)
151 #define SUN6I_CODEC_OM_DACA_CTRL_LHPPAMUTE (6)
152 #define SUN6I_CODEC_OM_DACA_CTRL_HPVOL (0)
153 #define SUN6I_CODEC_OM_PA_CTRL (0x24)
154 #define SUN6I_CODEC_OM_PA_CTRL_HPPAEN (31)
155 #define SUN6I_CODEC_OM_PA_CTRL_HPCOM_CTL (29)
156 #define SUN6I_CODEC_OM_PA_CTRL_COMPTEN (28)
157 #define SUN6I_CODEC_OM_PA_CTRL_MIC1G (15)
158 #define SUN6I_CODEC_OM_PA_CTRL_MIC2G (12)
159 #define SUN6I_CODEC_OM_PA_CTRL_LINEING (9)
160 #define SUN6I_CODEC_OM_PA_CTRL_PHONEG (6)
161 #define SUN6I_CODEC_OM_PA_CTRL_PHONEPG (3)
162 #define SUN6I_CODEC_OM_PA_CTRL_PHONENG (0)
164 /* Microphone, line out and phone out controls */
165 #define SUN6I_CODEC_MIC_CTRL (0x28)
166 #define SUN6I_CODEC_MIC_CTRL_HBIASEN (31)
167 #define SUN6I_CODEC_MIC_CTRL_MBIASEN (30)
168 #define SUN6I_CODEC_MIC_CTRL_MIC1AMPEN (28)
169 #define SUN6I_CODEC_MIC_CTRL_MIC1BOOST (25)
170 #define SUN6I_CODEC_MIC_CTRL_MIC2AMPEN (24)
171 #define SUN6I_CODEC_MIC_CTRL_MIC2BOOST (21)
172 #define SUN6I_CODEC_MIC_CTRL_MIC2SLT (20)
173 #define SUN6I_CODEC_MIC_CTRL_LINEOUTLEN (19)
174 #define SUN6I_CODEC_MIC_CTRL_LINEOUTREN (18)
175 #define SUN6I_CODEC_MIC_CTRL_LINEOUTLSRC (17)
176 #define SUN6I_CODEC_MIC_CTRL_LINEOUTRSRC (16)
177 #define SUN6I_CODEC_MIC_CTRL_LINEOUTVC (11)
178 #define SUN6I_CODEC_MIC_CTRL_PHONEPREG (8)
180 /* ADC mixer controls */
181 #define SUN6I_CODEC_ADC_ACTL (0x2c)
182 #define SUN6I_CODEC_ADC_ACTL_ADCREN (31)
183 #define SUN6I_CODEC_ADC_ACTL_ADCLEN (30)
184 #define SUN6I_CODEC_ADC_ACTL_ADCRG (27)
185 #define SUN6I_CODEC_ADC_ACTL_ADCLG (24)
186 #define SUN6I_CODEC_ADC_ACTL_RADCMIX_MIC1 (13)
187 #define SUN6I_CODEC_ADC_ACTL_RADCMIX_MIC2 (12)
188 #define SUN6I_CODEC_ADC_ACTL_RADCMIX_PHONE (11)
189 #define SUN6I_CODEC_ADC_ACTL_RADCMIX_PHONEP (10)
190 #define SUN6I_CODEC_ADC_ACTL_RADCMIX_LINEINR (9)
191 #define SUN6I_CODEC_ADC_ACTL_RADCMIX_OMIXR (8)
192 #define SUN6I_CODEC_ADC_ACTL_RADCMIX_OMIXL (7)
193 #define SUN6I_CODEC_ADC_ACTL_LADCMIX_MIC1 (6)
194 #define SUN6I_CODEC_ADC_ACTL_LADCMIX_MIC2 (5)
195 #define SUN6I_CODEC_ADC_ACTL_LADCMIX_PHONE (4)
196 #define SUN6I_CODEC_ADC_ACTL_LADCMIX_PHONEN (3)
197 #define SUN6I_CODEC_ADC_ACTL_LADCMIX_LINEINL (2)
198 #define SUN6I_CODEC_ADC_ACTL_LADCMIX_OMIXL (1)
199 #define SUN6I_CODEC_ADC_ACTL_LADCMIX_OMIXR (0)
201 /* Analog performance tuning controls */
202 #define SUN6I_CODEC_ADDA_TUNE (0x30)
204 /* Calibration controls */
205 #define SUN6I_CODEC_CALIBRATION (0x34)
208 #define SUN6I_CODEC_DAC_TXCNT (0x40)
209 #define SUN6I_CODEC_ADC_RXCNT (0x44)
211 /* headset jack detection and button support registers */
212 #define SUN6I_CODEC_HMIC_CTL (0x50)
213 #define SUN6I_CODEC_HMIC_DATA (0x54)
215 /* TODO sun6i DAP (Digital Audio Processing) bits */
217 /* FIFO counters moved on A23 */
218 #define SUN8I_A23_CODEC_DAC_TXCNT (0x1c)
219 #define SUN8I_A23_CODEC_ADC_RXCNT (0x20)
221 /* TX FIFO moved on H3 */
222 #define SUN8I_H3_CODEC_DAC_TXDATA (0x20)
223 #define SUN8I_H3_CODEC_DAC_DBG (0x48)
224 #define SUN8I_H3_CODEC_ADC_DBG (0x4c)
226 /* TODO H3 DAP (Digital Audio Processing) bits */
230 struct regmap *regmap;
232 struct clk *clk_module;
233 struct reset_control *rst;
234 struct gpio_desc *gpio_pa;
236 /* ADC_FIFOC register is at different offset on different SoCs */
237 struct regmap_field *reg_adc_fifoc;
239 struct snd_dmaengine_dai_dma_data capture_dma_data;
240 struct snd_dmaengine_dai_dma_data playback_dma_data;
243 static void sun4i_codec_start_playback(struct sun4i_codec *scodec)
246 regmap_update_bits(scodec->regmap, SUN4I_CODEC_DAC_FIFOC,
247 BIT(SUN4I_CODEC_DAC_FIFOC_FIFO_FLUSH),
248 BIT(SUN4I_CODEC_DAC_FIFOC_FIFO_FLUSH));
251 regmap_update_bits(scodec->regmap, SUN4I_CODEC_DAC_FIFOC,
252 BIT(SUN4I_CODEC_DAC_FIFOC_DAC_DRQ_EN),
253 BIT(SUN4I_CODEC_DAC_FIFOC_DAC_DRQ_EN));
256 static void sun4i_codec_stop_playback(struct sun4i_codec *scodec)
258 /* Disable DAC DRQ */
259 regmap_update_bits(scodec->regmap, SUN4I_CODEC_DAC_FIFOC,
260 BIT(SUN4I_CODEC_DAC_FIFOC_DAC_DRQ_EN),
264 static void sun4i_codec_start_capture(struct sun4i_codec *scodec)
267 regmap_field_update_bits(scodec->reg_adc_fifoc,
268 BIT(SUN4I_CODEC_ADC_FIFOC_ADC_DRQ_EN),
269 BIT(SUN4I_CODEC_ADC_FIFOC_ADC_DRQ_EN));
272 static void sun4i_codec_stop_capture(struct sun4i_codec *scodec)
274 /* Disable ADC DRQ */
275 regmap_field_update_bits(scodec->reg_adc_fifoc,
276 BIT(SUN4I_CODEC_ADC_FIFOC_ADC_DRQ_EN), 0);
279 static int sun4i_codec_trigger(struct snd_pcm_substream *substream, int cmd,
280 struct snd_soc_dai *dai)
282 struct snd_soc_pcm_runtime *rtd = substream->private_data;
283 struct sun4i_codec *scodec = snd_soc_card_get_drvdata(rtd->card);
286 case SNDRV_PCM_TRIGGER_START:
287 case SNDRV_PCM_TRIGGER_RESUME:
288 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
289 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
290 sun4i_codec_start_playback(scodec);
292 sun4i_codec_start_capture(scodec);
295 case SNDRV_PCM_TRIGGER_STOP:
296 case SNDRV_PCM_TRIGGER_SUSPEND:
297 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
298 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
299 sun4i_codec_stop_playback(scodec);
301 sun4i_codec_stop_capture(scodec);
311 static int sun4i_codec_prepare_capture(struct snd_pcm_substream *substream,
312 struct snd_soc_dai *dai)
314 struct snd_soc_pcm_runtime *rtd = substream->private_data;
315 struct sun4i_codec *scodec = snd_soc_card_get_drvdata(rtd->card);
319 regmap_field_update_bits(scodec->reg_adc_fifoc,
320 BIT(SUN4I_CODEC_ADC_FIFOC_FIFO_FLUSH),
321 BIT(SUN4I_CODEC_ADC_FIFOC_FIFO_FLUSH));
324 /* Set RX FIFO trigger level */
325 regmap_field_update_bits(scodec->reg_adc_fifoc,
326 0xf << SUN4I_CODEC_ADC_FIFOC_RX_TRIG_LEVEL,
327 0x7 << SUN4I_CODEC_ADC_FIFOC_RX_TRIG_LEVEL);
330 * FIXME: Undocumented in the datasheet, but
331 * Allwinner's code mentions that it is related
332 * related to microphone gain
334 if (of_device_is_compatible(scodec->dev->of_node,
335 "allwinner,sun4i-a10-codec") ||
336 of_device_is_compatible(scodec->dev->of_node,
337 "allwinner,sun7i-a20-codec")) {
338 regmap_update_bits(scodec->regmap, SUN4I_CODEC_ADC_ACTL,
343 if (of_device_is_compatible(scodec->dev->of_node,
344 "allwinner,sun7i-a20-codec"))
345 /* FIXME: Undocumented bits */
346 regmap_update_bits(scodec->regmap, SUN4I_CODEC_DAC_TUNE,
353 static int sun4i_codec_prepare_playback(struct snd_pcm_substream *substream,
354 struct snd_soc_dai *dai)
356 struct snd_soc_pcm_runtime *rtd = substream->private_data;
357 struct sun4i_codec *scodec = snd_soc_card_get_drvdata(rtd->card);
360 /* Flush the TX FIFO */
361 regmap_update_bits(scodec->regmap, SUN4I_CODEC_DAC_FIFOC,
362 BIT(SUN4I_CODEC_DAC_FIFOC_FIFO_FLUSH),
363 BIT(SUN4I_CODEC_DAC_FIFOC_FIFO_FLUSH));
365 /* Set TX FIFO Empty Trigger Level */
366 regmap_update_bits(scodec->regmap, SUN4I_CODEC_DAC_FIFOC,
367 0x3f << SUN4I_CODEC_DAC_FIFOC_TX_TRIG_LEVEL,
368 0xf << SUN4I_CODEC_DAC_FIFOC_TX_TRIG_LEVEL);
370 if (substream->runtime->rate > 32000)
371 /* Use 64 bits FIR filter */
374 /* Use 32 bits FIR filter */
375 val = BIT(SUN4I_CODEC_DAC_FIFOC_FIR_VERSION);
377 regmap_update_bits(scodec->regmap, SUN4I_CODEC_DAC_FIFOC,
378 BIT(SUN4I_CODEC_DAC_FIFOC_FIR_VERSION),
381 /* Send zeros when we have an underrun */
382 regmap_update_bits(scodec->regmap, SUN4I_CODEC_DAC_FIFOC,
383 BIT(SUN4I_CODEC_DAC_FIFOC_SEND_LASAT),
389 static int sun4i_codec_prepare(struct snd_pcm_substream *substream,
390 struct snd_soc_dai *dai)
392 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
393 return sun4i_codec_prepare_playback(substream, dai);
395 return sun4i_codec_prepare_capture(substream, dai);
398 static unsigned long sun4i_codec_get_mod_freq(struct snd_pcm_hw_params *params)
400 unsigned int rate = params_rate(params);
428 static int sun4i_codec_get_hw_rate(struct snd_pcm_hw_params *params)
430 unsigned int rate = params_rate(params);
470 static int sun4i_codec_hw_params_capture(struct sun4i_codec *scodec,
471 struct snd_pcm_hw_params *params,
474 /* Set ADC sample rate */
475 regmap_field_update_bits(scodec->reg_adc_fifoc,
476 7 << SUN4I_CODEC_ADC_FIFOC_ADC_FS,
477 hwrate << SUN4I_CODEC_ADC_FIFOC_ADC_FS);
479 /* Set the number of channels we want to use */
480 if (params_channels(params) == 1)
481 regmap_field_update_bits(scodec->reg_adc_fifoc,
482 BIT(SUN4I_CODEC_ADC_FIFOC_MONO_EN),
483 BIT(SUN4I_CODEC_ADC_FIFOC_MONO_EN));
485 regmap_field_update_bits(scodec->reg_adc_fifoc,
486 BIT(SUN4I_CODEC_ADC_FIFOC_MONO_EN),
489 /* Set the number of sample bits to either 16 or 24 bits */
490 if (hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS)->min == 32) {
491 regmap_field_update_bits(scodec->reg_adc_fifoc,
492 BIT(SUN4I_CODEC_ADC_FIFOC_RX_SAMPLE_BITS),
493 BIT(SUN4I_CODEC_ADC_FIFOC_RX_SAMPLE_BITS));
495 regmap_field_update_bits(scodec->reg_adc_fifoc,
496 BIT(SUN4I_CODEC_ADC_FIFOC_RX_FIFO_MODE),
499 scodec->capture_dma_data.addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
501 regmap_field_update_bits(scodec->reg_adc_fifoc,
502 BIT(SUN4I_CODEC_ADC_FIFOC_RX_SAMPLE_BITS),
505 /* Fill most significant bits with valid data MSB */
506 regmap_field_update_bits(scodec->reg_adc_fifoc,
507 BIT(SUN4I_CODEC_ADC_FIFOC_RX_FIFO_MODE),
508 BIT(SUN4I_CODEC_ADC_FIFOC_RX_FIFO_MODE));
510 scodec->capture_dma_data.addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
516 static int sun4i_codec_hw_params_playback(struct sun4i_codec *scodec,
517 struct snd_pcm_hw_params *params,
522 /* Set DAC sample rate */
523 regmap_update_bits(scodec->regmap, SUN4I_CODEC_DAC_FIFOC,
524 7 << SUN4I_CODEC_DAC_FIFOC_DAC_FS,
525 hwrate << SUN4I_CODEC_DAC_FIFOC_DAC_FS);
527 /* Set the number of channels we want to use */
528 if (params_channels(params) == 1)
529 val = BIT(SUN4I_CODEC_DAC_FIFOC_MONO_EN);
533 regmap_update_bits(scodec->regmap, SUN4I_CODEC_DAC_FIFOC,
534 BIT(SUN4I_CODEC_DAC_FIFOC_MONO_EN),
537 /* Set the number of sample bits to either 16 or 24 bits */
538 if (hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS)->min == 32) {
539 regmap_update_bits(scodec->regmap, SUN4I_CODEC_DAC_FIFOC,
540 BIT(SUN4I_CODEC_DAC_FIFOC_TX_SAMPLE_BITS),
541 BIT(SUN4I_CODEC_DAC_FIFOC_TX_SAMPLE_BITS));
543 /* Set TX FIFO mode to padding the LSBs with 0 */
544 regmap_update_bits(scodec->regmap, SUN4I_CODEC_DAC_FIFOC,
545 BIT(SUN4I_CODEC_DAC_FIFOC_TX_FIFO_MODE),
548 scodec->playback_dma_data.addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
550 regmap_update_bits(scodec->regmap, SUN4I_CODEC_DAC_FIFOC,
551 BIT(SUN4I_CODEC_DAC_FIFOC_TX_SAMPLE_BITS),
554 /* Set TX FIFO mode to repeat the MSB */
555 regmap_update_bits(scodec->regmap, SUN4I_CODEC_DAC_FIFOC,
556 BIT(SUN4I_CODEC_DAC_FIFOC_TX_FIFO_MODE),
557 BIT(SUN4I_CODEC_DAC_FIFOC_TX_FIFO_MODE));
559 scodec->playback_dma_data.addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
565 static int sun4i_codec_hw_params(struct snd_pcm_substream *substream,
566 struct snd_pcm_hw_params *params,
567 struct snd_soc_dai *dai)
569 struct snd_soc_pcm_runtime *rtd = substream->private_data;
570 struct sun4i_codec *scodec = snd_soc_card_get_drvdata(rtd->card);
571 unsigned long clk_freq;
574 clk_freq = sun4i_codec_get_mod_freq(params);
578 ret = clk_set_rate(scodec->clk_module, clk_freq);
582 hwrate = sun4i_codec_get_hw_rate(params);
586 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
587 return sun4i_codec_hw_params_playback(scodec, params,
590 return sun4i_codec_hw_params_capture(scodec, params,
595 static unsigned int sun4i_codec_src_rates[] = {
596 8000, 11025, 12000, 16000, 22050, 24000, 32000,
597 44100, 48000, 96000, 192000
601 static struct snd_pcm_hw_constraint_list sun4i_codec_constraints = {
602 .count = ARRAY_SIZE(sun4i_codec_src_rates),
603 .list = sun4i_codec_src_rates,
607 static int sun4i_codec_startup(struct snd_pcm_substream *substream,
608 struct snd_soc_dai *dai)
610 struct snd_soc_pcm_runtime *rtd = substream->private_data;
611 struct sun4i_codec *scodec = snd_soc_card_get_drvdata(rtd->card);
613 snd_pcm_hw_constraint_list(substream->runtime, 0,
614 SNDRV_PCM_HW_PARAM_RATE, &sun4i_codec_constraints);
617 * Stop issuing DRQ when we have room for less than 16 samples
620 regmap_update_bits(scodec->regmap, SUN4I_CODEC_DAC_FIFOC,
621 3 << SUN4I_CODEC_DAC_FIFOC_DRQ_CLR_CNT,
622 3 << SUN4I_CODEC_DAC_FIFOC_DRQ_CLR_CNT);
624 return clk_prepare_enable(scodec->clk_module);
627 static void sun4i_codec_shutdown(struct snd_pcm_substream *substream,
628 struct snd_soc_dai *dai)
630 struct snd_soc_pcm_runtime *rtd = substream->private_data;
631 struct sun4i_codec *scodec = snd_soc_card_get_drvdata(rtd->card);
633 clk_disable_unprepare(scodec->clk_module);
636 static const struct snd_soc_dai_ops sun4i_codec_dai_ops = {
637 .startup = sun4i_codec_startup,
638 .shutdown = sun4i_codec_shutdown,
639 .trigger = sun4i_codec_trigger,
640 .hw_params = sun4i_codec_hw_params,
641 .prepare = sun4i_codec_prepare,
644 static struct snd_soc_dai_driver sun4i_codec_dai = {
646 .ops = &sun4i_codec_dai_ops,
648 .stream_name = "Codec Playback",
653 .rates = SNDRV_PCM_RATE_CONTINUOUS,
654 .formats = SNDRV_PCM_FMTBIT_S16_LE |
655 SNDRV_PCM_FMTBIT_S32_LE,
659 .stream_name = "Codec Capture",
664 .rates = SNDRV_PCM_RATE_CONTINUOUS,
665 .formats = SNDRV_PCM_FMTBIT_S16_LE |
666 SNDRV_PCM_FMTBIT_S32_LE,
671 /*** sun4i Codec ***/
672 static const struct snd_kcontrol_new sun4i_codec_pa_mute =
673 SOC_DAPM_SINGLE("Switch", SUN4I_CODEC_DAC_ACTL,
674 SUN4I_CODEC_DAC_ACTL_PA_MUTE, 1, 0);
676 static DECLARE_TLV_DB_SCALE(sun4i_codec_pa_volume_scale, -6300, 100, 1);
677 static DECLARE_TLV_DB_SCALE(sun4i_codec_micin_loopback_gain_scale, -450, 150,
680 static const struct snd_kcontrol_new sun4i_codec_controls[] = {
681 SOC_SINGLE_TLV("Power Amplifier Volume", SUN4I_CODEC_DAC_ACTL,
682 SUN4I_CODEC_DAC_ACTL_PA_VOL, 0x3F, 0,
683 sun4i_codec_pa_volume_scale),
686 static const struct snd_kcontrol_new sun4i_codec_left_mixer_controls[] = {
687 SOC_DAPM_SINGLE("Left DAC Playback Switch", SUN4I_CODEC_DAC_ACTL,
688 SUN4I_CODEC_DAC_ACTL_LDACLMIXS, 1, 0),
691 static const struct snd_kcontrol_new sun4i_codec_right_mixer_controls[] = {
692 SOC_DAPM_SINGLE("Right DAC Playback Switch", SUN4I_CODEC_DAC_ACTL,
693 SUN4I_CODEC_DAC_ACTL_RDACRMIXS, 1, 0),
694 SOC_DAPM_SINGLE("Left DAC Playback Switch", SUN4I_CODEC_DAC_ACTL,
695 SUN4I_CODEC_DAC_ACTL_LDACRMIXS, 1, 0),
698 static const struct snd_kcontrol_new sun4i_codec_pa_mixer_controls[] = {
699 SOC_DAPM_SINGLE("DAC Playback Switch", SUN4I_CODEC_DAC_ACTL,
700 SUN4I_CODEC_DAC_ACTL_DACPAS, 1, 0),
701 SOC_DAPM_SINGLE("Mixer Playback Switch", SUN4I_CODEC_DAC_ACTL,
702 SUN4I_CODEC_DAC_ACTL_MIXPAS, 1, 0),
705 static const struct snd_soc_dapm_widget sun4i_codec_codec_dapm_widgets[] = {
706 /* Digital parts of the ADCs */
707 SND_SOC_DAPM_SUPPLY("ADC", SUN4I_CODEC_ADC_FIFOC,
708 SUN4I_CODEC_ADC_FIFOC_EN_AD, 0,
711 /* Digital parts of the DACs */
712 SND_SOC_DAPM_SUPPLY("DAC", SUN4I_CODEC_DAC_DPC,
713 SUN4I_CODEC_DAC_DPC_EN_DA, 0,
716 /* Analog parts of the ADCs */
717 SND_SOC_DAPM_ADC("Left ADC", "Codec Capture", SUN4I_CODEC_ADC_ACTL,
718 SUN4I_CODEC_ADC_ACTL_ADC_L_EN, 0),
719 SND_SOC_DAPM_ADC("Right ADC", "Codec Capture", SUN4I_CODEC_ADC_ACTL,
720 SUN4I_CODEC_ADC_ACTL_ADC_R_EN, 0),
722 /* Analog parts of the DACs */
723 SND_SOC_DAPM_DAC("Left DAC", "Codec Playback", SUN4I_CODEC_DAC_ACTL,
724 SUN4I_CODEC_DAC_ACTL_DACAENL, 0),
725 SND_SOC_DAPM_DAC("Right DAC", "Codec Playback", SUN4I_CODEC_DAC_ACTL,
726 SUN4I_CODEC_DAC_ACTL_DACAENR, 0),
729 SND_SOC_DAPM_MIXER("Left Mixer", SND_SOC_NOPM, 0, 0,
730 sun4i_codec_left_mixer_controls,
731 ARRAY_SIZE(sun4i_codec_left_mixer_controls)),
732 SND_SOC_DAPM_MIXER("Right Mixer", SND_SOC_NOPM, 0, 0,
733 sun4i_codec_right_mixer_controls,
734 ARRAY_SIZE(sun4i_codec_right_mixer_controls)),
736 /* Global Mixer Enable */
737 SND_SOC_DAPM_SUPPLY("Mixer Enable", SUN4I_CODEC_DAC_ACTL,
738 SUN4I_CODEC_DAC_ACTL_MIXEN, 0, NULL, 0),
741 SND_SOC_DAPM_SUPPLY("VMIC", SUN4I_CODEC_ADC_ACTL,
742 SUN4I_CODEC_ADC_ACTL_VMICEN, 0, NULL, 0),
744 /* Mic Pre-Amplifiers */
745 SND_SOC_DAPM_PGA("MIC1 Pre-Amplifier", SUN4I_CODEC_ADC_ACTL,
746 SUN4I_CODEC_ADC_ACTL_PREG1EN, 0, NULL, 0),
747 SND_SOC_DAPM_PGA("MIC2 Pre-Amplifier", SUN4I_CODEC_ADC_ACTL,
748 SUN4I_CODEC_ADC_ACTL_PREG2EN, 0, NULL, 0),
750 /* Power Amplifier */
751 SND_SOC_DAPM_MIXER("Power Amplifier", SUN4I_CODEC_ADC_ACTL,
752 SUN4I_CODEC_ADC_ACTL_PA_EN, 0,
753 sun4i_codec_pa_mixer_controls,
754 ARRAY_SIZE(sun4i_codec_pa_mixer_controls)),
755 SND_SOC_DAPM_SWITCH("Power Amplifier Mute", SND_SOC_NOPM, 0, 0,
756 &sun4i_codec_pa_mute),
758 SND_SOC_DAPM_INPUT("Mic1"),
759 SND_SOC_DAPM_INPUT("Mic2"),
761 SND_SOC_DAPM_OUTPUT("HP Right"),
762 SND_SOC_DAPM_OUTPUT("HP Left"),
765 static const struct snd_soc_dapm_route sun4i_codec_codec_dapm_routes[] = {
766 /* Left ADC / DAC Routes */
767 { "Left ADC", NULL, "ADC" },
768 { "Left DAC", NULL, "DAC" },
770 /* Right ADC / DAC Routes */
771 { "Right ADC", NULL, "ADC" },
772 { "Right DAC", NULL, "DAC" },
774 /* Right Mixer Routes */
775 { "Right Mixer", NULL, "Mixer Enable" },
776 { "Right Mixer", "Left DAC Playback Switch", "Left DAC" },
777 { "Right Mixer", "Right DAC Playback Switch", "Right DAC" },
779 /* Left Mixer Routes */
780 { "Left Mixer", NULL, "Mixer Enable" },
781 { "Left Mixer", "Left DAC Playback Switch", "Left DAC" },
783 /* Power Amplifier Routes */
784 { "Power Amplifier", "Mixer Playback Switch", "Left Mixer" },
785 { "Power Amplifier", "Mixer Playback Switch", "Right Mixer" },
786 { "Power Amplifier", "DAC Playback Switch", "Left DAC" },
787 { "Power Amplifier", "DAC Playback Switch", "Right DAC" },
789 /* Headphone Output Routes */
790 { "Power Amplifier Mute", "Switch", "Power Amplifier" },
791 { "HP Right", NULL, "Power Amplifier Mute" },
792 { "HP Left", NULL, "Power Amplifier Mute" },
795 { "Left ADC", NULL, "MIC1 Pre-Amplifier" },
796 { "Right ADC", NULL, "MIC1 Pre-Amplifier" },
797 { "MIC1 Pre-Amplifier", NULL, "Mic1"},
798 { "Mic1", NULL, "VMIC" },
801 { "Left ADC", NULL, "MIC2 Pre-Amplifier" },
802 { "Right ADC", NULL, "MIC2 Pre-Amplifier" },
803 { "MIC2 Pre-Amplifier", NULL, "Mic2"},
804 { "Mic2", NULL, "VMIC" },
807 static const struct snd_soc_component_driver sun4i_codec_codec = {
808 .controls = sun4i_codec_controls,
809 .num_controls = ARRAY_SIZE(sun4i_codec_controls),
810 .dapm_widgets = sun4i_codec_codec_dapm_widgets,
811 .num_dapm_widgets = ARRAY_SIZE(sun4i_codec_codec_dapm_widgets),
812 .dapm_routes = sun4i_codec_codec_dapm_routes,
813 .num_dapm_routes = ARRAY_SIZE(sun4i_codec_codec_dapm_routes),
815 .use_pmdown_time = 1,
817 .non_legacy_dai_naming = 1,
820 /*** sun6i Codec ***/
823 static const struct snd_kcontrol_new sun6i_codec_mixer_controls[] = {
824 SOC_DAPM_DOUBLE("DAC Playback Switch",
825 SUN6I_CODEC_OM_DACA_CTRL,
826 SUN6I_CODEC_OM_DACA_CTRL_LMIX_DACL,
827 SUN6I_CODEC_OM_DACA_CTRL_RMIX_DACR, 1, 0),
828 SOC_DAPM_DOUBLE("DAC Reversed Playback Switch",
829 SUN6I_CODEC_OM_DACA_CTRL,
830 SUN6I_CODEC_OM_DACA_CTRL_LMIX_DACR,
831 SUN6I_CODEC_OM_DACA_CTRL_RMIX_DACL, 1, 0),
832 SOC_DAPM_DOUBLE("Line In Playback Switch",
833 SUN6I_CODEC_OM_DACA_CTRL,
834 SUN6I_CODEC_OM_DACA_CTRL_LMIX_LINEINL,
835 SUN6I_CODEC_OM_DACA_CTRL_RMIX_LINEINR, 1, 0),
836 SOC_DAPM_DOUBLE("Mic1 Playback Switch",
837 SUN6I_CODEC_OM_DACA_CTRL,
838 SUN6I_CODEC_OM_DACA_CTRL_LMIX_MIC1,
839 SUN6I_CODEC_OM_DACA_CTRL_RMIX_MIC1, 1, 0),
840 SOC_DAPM_DOUBLE("Mic2 Playback Switch",
841 SUN6I_CODEC_OM_DACA_CTRL,
842 SUN6I_CODEC_OM_DACA_CTRL_LMIX_MIC2,
843 SUN6I_CODEC_OM_DACA_CTRL_RMIX_MIC2, 1, 0),
846 /* ADC mixer controls */
847 static const struct snd_kcontrol_new sun6i_codec_adc_mixer_controls[] = {
848 SOC_DAPM_DOUBLE("Mixer Capture Switch",
849 SUN6I_CODEC_ADC_ACTL,
850 SUN6I_CODEC_ADC_ACTL_LADCMIX_OMIXL,
851 SUN6I_CODEC_ADC_ACTL_RADCMIX_OMIXR, 1, 0),
852 SOC_DAPM_DOUBLE("Mixer Reversed Capture Switch",
853 SUN6I_CODEC_ADC_ACTL,
854 SUN6I_CODEC_ADC_ACTL_LADCMIX_OMIXR,
855 SUN6I_CODEC_ADC_ACTL_RADCMIX_OMIXL, 1, 0),
856 SOC_DAPM_DOUBLE("Line In Capture Switch",
857 SUN6I_CODEC_ADC_ACTL,
858 SUN6I_CODEC_ADC_ACTL_LADCMIX_LINEINL,
859 SUN6I_CODEC_ADC_ACTL_RADCMIX_LINEINR, 1, 0),
860 SOC_DAPM_DOUBLE("Mic1 Capture Switch",
861 SUN6I_CODEC_ADC_ACTL,
862 SUN6I_CODEC_ADC_ACTL_LADCMIX_MIC1,
863 SUN6I_CODEC_ADC_ACTL_RADCMIX_MIC1, 1, 0),
864 SOC_DAPM_DOUBLE("Mic2 Capture Switch",
865 SUN6I_CODEC_ADC_ACTL,
866 SUN6I_CODEC_ADC_ACTL_LADCMIX_MIC2,
867 SUN6I_CODEC_ADC_ACTL_RADCMIX_MIC2, 1, 0),
870 /* headphone controls */
871 static const char * const sun6i_codec_hp_src_enum_text[] = {
875 static SOC_ENUM_DOUBLE_DECL(sun6i_codec_hp_src_enum,
876 SUN6I_CODEC_OM_DACA_CTRL,
877 SUN6I_CODEC_OM_DACA_CTRL_LHPIS,
878 SUN6I_CODEC_OM_DACA_CTRL_RHPIS,
879 sun6i_codec_hp_src_enum_text);
881 static const struct snd_kcontrol_new sun6i_codec_hp_src[] = {
882 SOC_DAPM_ENUM("Headphone Source Playback Route",
883 sun6i_codec_hp_src_enum),
886 /* microphone controls */
887 static const char * const sun6i_codec_mic2_src_enum_text[] = {
891 static SOC_ENUM_SINGLE_DECL(sun6i_codec_mic2_src_enum,
892 SUN6I_CODEC_MIC_CTRL,
893 SUN6I_CODEC_MIC_CTRL_MIC2SLT,
894 sun6i_codec_mic2_src_enum_text);
896 static const struct snd_kcontrol_new sun6i_codec_mic2_src[] = {
897 SOC_DAPM_ENUM("Mic2 Amplifier Source Route",
898 sun6i_codec_mic2_src_enum),
901 /* line out controls */
902 static const char * const sun6i_codec_lineout_src_enum_text[] = {
903 "Stereo", "Mono Differential",
906 static SOC_ENUM_DOUBLE_DECL(sun6i_codec_lineout_src_enum,
907 SUN6I_CODEC_MIC_CTRL,
908 SUN6I_CODEC_MIC_CTRL_LINEOUTLSRC,
909 SUN6I_CODEC_MIC_CTRL_LINEOUTRSRC,
910 sun6i_codec_lineout_src_enum_text);
912 static const struct snd_kcontrol_new sun6i_codec_lineout_src[] = {
913 SOC_DAPM_ENUM("Line Out Source Playback Route",
914 sun6i_codec_lineout_src_enum),
917 /* volume / mute controls */
918 static const DECLARE_TLV_DB_SCALE(sun6i_codec_dvol_scale, -7308, 116, 0);
919 static const DECLARE_TLV_DB_SCALE(sun6i_codec_hp_vol_scale, -6300, 100, 1);
920 static const DECLARE_TLV_DB_SCALE(sun6i_codec_out_mixer_pregain_scale,
922 static const DECLARE_TLV_DB_RANGE(sun6i_codec_lineout_vol_scale,
923 0, 1, TLV_DB_SCALE_ITEM(TLV_DB_GAIN_MUTE, 0, 1),
924 2, 31, TLV_DB_SCALE_ITEM(-4350, 150, 0),
926 static const DECLARE_TLV_DB_RANGE(sun6i_codec_mic_gain_scale,
927 0, 0, TLV_DB_SCALE_ITEM(0, 0, 0),
928 1, 7, TLV_DB_SCALE_ITEM(2400, 300, 0),
931 static const struct snd_kcontrol_new sun6i_codec_codec_widgets[] = {
932 SOC_SINGLE_TLV("DAC Playback Volume", SUN4I_CODEC_DAC_DPC,
933 SUN4I_CODEC_DAC_DPC_DVOL, 0x3f, 1,
934 sun6i_codec_dvol_scale),
935 SOC_SINGLE_TLV("Headphone Playback Volume",
936 SUN6I_CODEC_OM_DACA_CTRL,
937 SUN6I_CODEC_OM_DACA_CTRL_HPVOL, 0x3f, 0,
938 sun6i_codec_hp_vol_scale),
939 SOC_SINGLE_TLV("Line Out Playback Volume",
940 SUN6I_CODEC_MIC_CTRL,
941 SUN6I_CODEC_MIC_CTRL_LINEOUTVC, 0x1f, 0,
942 sun6i_codec_lineout_vol_scale),
943 SOC_DOUBLE("Headphone Playback Switch",
944 SUN6I_CODEC_OM_DACA_CTRL,
945 SUN6I_CODEC_OM_DACA_CTRL_LHPPAMUTE,
946 SUN6I_CODEC_OM_DACA_CTRL_RHPPAMUTE, 1, 0),
947 SOC_DOUBLE("Line Out Playback Switch",
948 SUN6I_CODEC_MIC_CTRL,
949 SUN6I_CODEC_MIC_CTRL_LINEOUTLEN,
950 SUN6I_CODEC_MIC_CTRL_LINEOUTREN, 1, 0),
951 /* Mixer pre-gains */
952 SOC_SINGLE_TLV("Line In Playback Volume",
953 SUN6I_CODEC_OM_PA_CTRL, SUN6I_CODEC_OM_PA_CTRL_LINEING,
954 0x7, 0, sun6i_codec_out_mixer_pregain_scale),
955 SOC_SINGLE_TLV("Mic1 Playback Volume",
956 SUN6I_CODEC_OM_PA_CTRL, SUN6I_CODEC_OM_PA_CTRL_MIC1G,
957 0x7, 0, sun6i_codec_out_mixer_pregain_scale),
958 SOC_SINGLE_TLV("Mic2 Playback Volume",
959 SUN6I_CODEC_OM_PA_CTRL, SUN6I_CODEC_OM_PA_CTRL_MIC2G,
960 0x7, 0, sun6i_codec_out_mixer_pregain_scale),
962 /* Microphone Amp boost gains */
963 SOC_SINGLE_TLV("Mic1 Boost Volume", SUN6I_CODEC_MIC_CTRL,
964 SUN6I_CODEC_MIC_CTRL_MIC1BOOST, 0x7, 0,
965 sun6i_codec_mic_gain_scale),
966 SOC_SINGLE_TLV("Mic2 Boost Volume", SUN6I_CODEC_MIC_CTRL,
967 SUN6I_CODEC_MIC_CTRL_MIC2BOOST, 0x7, 0,
968 sun6i_codec_mic_gain_scale),
969 SOC_DOUBLE_TLV("ADC Capture Volume",
970 SUN6I_CODEC_ADC_ACTL, SUN6I_CODEC_ADC_ACTL_ADCLG,
971 SUN6I_CODEC_ADC_ACTL_ADCRG, 0x7, 0,
972 sun6i_codec_out_mixer_pregain_scale),
975 static const struct snd_soc_dapm_widget sun6i_codec_codec_dapm_widgets[] = {
976 /* Microphone inputs */
977 SND_SOC_DAPM_INPUT("MIC1"),
978 SND_SOC_DAPM_INPUT("MIC2"),
979 SND_SOC_DAPM_INPUT("MIC3"),
981 /* Microphone Bias */
982 SND_SOC_DAPM_SUPPLY("HBIAS", SUN6I_CODEC_MIC_CTRL,
983 SUN6I_CODEC_MIC_CTRL_HBIASEN, 0, NULL, 0),
984 SND_SOC_DAPM_SUPPLY("MBIAS", SUN6I_CODEC_MIC_CTRL,
985 SUN6I_CODEC_MIC_CTRL_MBIASEN, 0, NULL, 0),
988 SND_SOC_DAPM_MUX("Mic2 Amplifier Source Route",
989 SND_SOC_NOPM, 0, 0, sun6i_codec_mic2_src),
990 SND_SOC_DAPM_PGA("Mic1 Amplifier", SUN6I_CODEC_MIC_CTRL,
991 SUN6I_CODEC_MIC_CTRL_MIC1AMPEN, 0, NULL, 0),
992 SND_SOC_DAPM_PGA("Mic2 Amplifier", SUN6I_CODEC_MIC_CTRL,
993 SUN6I_CODEC_MIC_CTRL_MIC2AMPEN, 0, NULL, 0),
996 SND_SOC_DAPM_INPUT("LINEIN"),
998 /* Digital parts of the ADCs */
999 SND_SOC_DAPM_SUPPLY("ADC Enable", SUN6I_CODEC_ADC_FIFOC,
1000 SUN6I_CODEC_ADC_FIFOC_EN_AD, 0,
1003 /* Analog parts of the ADCs */
1004 SND_SOC_DAPM_ADC("Left ADC", "Codec Capture", SUN6I_CODEC_ADC_ACTL,
1005 SUN6I_CODEC_ADC_ACTL_ADCLEN, 0),
1006 SND_SOC_DAPM_ADC("Right ADC", "Codec Capture", SUN6I_CODEC_ADC_ACTL,
1007 SUN6I_CODEC_ADC_ACTL_ADCREN, 0),
1010 SOC_MIXER_ARRAY("Left ADC Mixer", SND_SOC_NOPM, 0, 0,
1011 sun6i_codec_adc_mixer_controls),
1012 SOC_MIXER_ARRAY("Right ADC Mixer", SND_SOC_NOPM, 0, 0,
1013 sun6i_codec_adc_mixer_controls),
1015 /* Digital parts of the DACs */
1016 SND_SOC_DAPM_SUPPLY("DAC Enable", SUN4I_CODEC_DAC_DPC,
1017 SUN4I_CODEC_DAC_DPC_EN_DA, 0,
1020 /* Analog parts of the DACs */
1021 SND_SOC_DAPM_DAC("Left DAC", "Codec Playback",
1022 SUN6I_CODEC_OM_DACA_CTRL,
1023 SUN6I_CODEC_OM_DACA_CTRL_DACALEN, 0),
1024 SND_SOC_DAPM_DAC("Right DAC", "Codec Playback",
1025 SUN6I_CODEC_OM_DACA_CTRL,
1026 SUN6I_CODEC_OM_DACA_CTRL_DACAREN, 0),
1029 SOC_MIXER_ARRAY("Left Mixer", SUN6I_CODEC_OM_DACA_CTRL,
1030 SUN6I_CODEC_OM_DACA_CTRL_LMIXEN, 0,
1031 sun6i_codec_mixer_controls),
1032 SOC_MIXER_ARRAY("Right Mixer", SUN6I_CODEC_OM_DACA_CTRL,
1033 SUN6I_CODEC_OM_DACA_CTRL_RMIXEN, 0,
1034 sun6i_codec_mixer_controls),
1036 /* Headphone output path */
1037 SND_SOC_DAPM_MUX("Headphone Source Playback Route",
1038 SND_SOC_NOPM, 0, 0, sun6i_codec_hp_src),
1039 SND_SOC_DAPM_OUT_DRV("Headphone Amp", SUN6I_CODEC_OM_PA_CTRL,
1040 SUN6I_CODEC_OM_PA_CTRL_HPPAEN, 0, NULL, 0),
1041 SND_SOC_DAPM_SUPPLY("HPCOM Protection", SUN6I_CODEC_OM_PA_CTRL,
1042 SUN6I_CODEC_OM_PA_CTRL_COMPTEN, 0, NULL, 0),
1043 SND_SOC_DAPM_REG(snd_soc_dapm_supply, "HPCOM", SUN6I_CODEC_OM_PA_CTRL,
1044 SUN6I_CODEC_OM_PA_CTRL_HPCOM_CTL, 0x3, 0x3, 0),
1045 SND_SOC_DAPM_OUTPUT("HP"),
1048 SND_SOC_DAPM_MUX("Line Out Source Playback Route",
1049 SND_SOC_NOPM, 0, 0, sun6i_codec_lineout_src),
1050 SND_SOC_DAPM_OUTPUT("LINEOUT"),
1053 static const struct snd_soc_dapm_route sun6i_codec_codec_dapm_routes[] = {
1055 { "Left DAC", NULL, "DAC Enable" },
1056 { "Right DAC", NULL, "DAC Enable" },
1058 /* Microphone Routes */
1059 { "Mic1 Amplifier", NULL, "MIC1"},
1060 { "Mic2 Amplifier Source Route", "Mic2", "MIC2" },
1061 { "Mic2 Amplifier Source Route", "Mic3", "MIC3" },
1062 { "Mic2 Amplifier", NULL, "Mic2 Amplifier Source Route"},
1064 /* Left Mixer Routes */
1065 { "Left Mixer", "DAC Playback Switch", "Left DAC" },
1066 { "Left Mixer", "DAC Reversed Playback Switch", "Right DAC" },
1067 { "Left Mixer", "Line In Playback Switch", "LINEIN" },
1068 { "Left Mixer", "Mic1 Playback Switch", "Mic1 Amplifier" },
1069 { "Left Mixer", "Mic2 Playback Switch", "Mic2 Amplifier" },
1071 /* Right Mixer Routes */
1072 { "Right Mixer", "DAC Playback Switch", "Right DAC" },
1073 { "Right Mixer", "DAC Reversed Playback Switch", "Left DAC" },
1074 { "Right Mixer", "Line In Playback Switch", "LINEIN" },
1075 { "Right Mixer", "Mic1 Playback Switch", "Mic1 Amplifier" },
1076 { "Right Mixer", "Mic2 Playback Switch", "Mic2 Amplifier" },
1078 /* Left ADC Mixer Routes */
1079 { "Left ADC Mixer", "Mixer Capture Switch", "Left Mixer" },
1080 { "Left ADC Mixer", "Mixer Reversed Capture Switch", "Right Mixer" },
1081 { "Left ADC Mixer", "Line In Capture Switch", "LINEIN" },
1082 { "Left ADC Mixer", "Mic1 Capture Switch", "Mic1 Amplifier" },
1083 { "Left ADC Mixer", "Mic2 Capture Switch", "Mic2 Amplifier" },
1085 /* Right ADC Mixer Routes */
1086 { "Right ADC Mixer", "Mixer Capture Switch", "Right Mixer" },
1087 { "Right ADC Mixer", "Mixer Reversed Capture Switch", "Left Mixer" },
1088 { "Right ADC Mixer", "Line In Capture Switch", "LINEIN" },
1089 { "Right ADC Mixer", "Mic1 Capture Switch", "Mic1 Amplifier" },
1090 { "Right ADC Mixer", "Mic2 Capture Switch", "Mic2 Amplifier" },
1092 /* Headphone Routes */
1093 { "Headphone Source Playback Route", "DAC", "Left DAC" },
1094 { "Headphone Source Playback Route", "DAC", "Right DAC" },
1095 { "Headphone Source Playback Route", "Mixer", "Left Mixer" },
1096 { "Headphone Source Playback Route", "Mixer", "Right Mixer" },
1097 { "Headphone Amp", NULL, "Headphone Source Playback Route" },
1098 { "HP", NULL, "Headphone Amp" },
1099 { "HPCOM", NULL, "HPCOM Protection" },
1101 /* Line Out Routes */
1102 { "Line Out Source Playback Route", "Stereo", "Left Mixer" },
1103 { "Line Out Source Playback Route", "Stereo", "Right Mixer" },
1104 { "Line Out Source Playback Route", "Mono Differential", "Left Mixer" },
1105 { "Line Out Source Playback Route", "Mono Differential", "Right Mixer" },
1106 { "LINEOUT", NULL, "Line Out Source Playback Route" },
1109 { "Left ADC", NULL, "ADC Enable" },
1110 { "Right ADC", NULL, "ADC Enable" },
1111 { "Left ADC", NULL, "Left ADC Mixer" },
1112 { "Right ADC", NULL, "Right ADC Mixer" },
1115 static const struct snd_soc_component_driver sun6i_codec_codec = {
1116 .controls = sun6i_codec_codec_widgets,
1117 .num_controls = ARRAY_SIZE(sun6i_codec_codec_widgets),
1118 .dapm_widgets = sun6i_codec_codec_dapm_widgets,
1119 .num_dapm_widgets = ARRAY_SIZE(sun6i_codec_codec_dapm_widgets),
1120 .dapm_routes = sun6i_codec_codec_dapm_routes,
1121 .num_dapm_routes = ARRAY_SIZE(sun6i_codec_codec_dapm_routes),
1123 .use_pmdown_time = 1,
1125 .non_legacy_dai_naming = 1,
1128 /* sun8i A23 codec */
1129 static const struct snd_kcontrol_new sun8i_a23_codec_codec_controls[] = {
1130 SOC_SINGLE_TLV("DAC Playback Volume", SUN4I_CODEC_DAC_DPC,
1131 SUN4I_CODEC_DAC_DPC_DVOL, 0x3f, 1,
1132 sun6i_codec_dvol_scale),
1135 static const struct snd_soc_dapm_widget sun8i_a23_codec_codec_widgets[] = {
1136 /* Digital parts of the ADCs */
1137 SND_SOC_DAPM_SUPPLY("ADC Enable", SUN6I_CODEC_ADC_FIFOC,
1138 SUN6I_CODEC_ADC_FIFOC_EN_AD, 0, NULL, 0),
1139 /* Digital parts of the DACs */
1140 SND_SOC_DAPM_SUPPLY("DAC Enable", SUN4I_CODEC_DAC_DPC,
1141 SUN4I_CODEC_DAC_DPC_EN_DA, 0, NULL, 0),
1145 static const struct snd_soc_component_driver sun8i_a23_codec_codec = {
1146 .controls = sun8i_a23_codec_codec_controls,
1147 .num_controls = ARRAY_SIZE(sun8i_a23_codec_codec_controls),
1148 .dapm_widgets = sun8i_a23_codec_codec_widgets,
1149 .num_dapm_widgets = ARRAY_SIZE(sun8i_a23_codec_codec_widgets),
1151 .use_pmdown_time = 1,
1153 .non_legacy_dai_naming = 1,
1156 static const struct snd_soc_component_driver sun4i_codec_component = {
1157 .name = "sun4i-codec",
1160 #define SUN4I_CODEC_RATES SNDRV_PCM_RATE_CONTINUOUS
1161 #define SUN4I_CODEC_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | \
1162 SNDRV_PCM_FMTBIT_S32_LE)
1164 static int sun4i_codec_dai_probe(struct snd_soc_dai *dai)
1166 struct snd_soc_card *card = snd_soc_dai_get_drvdata(dai);
1167 struct sun4i_codec *scodec = snd_soc_card_get_drvdata(card);
1169 snd_soc_dai_init_dma_data(dai, &scodec->playback_dma_data,
1170 &scodec->capture_dma_data);
1175 static struct snd_soc_dai_driver dummy_cpu_dai = {
1176 .name = "sun4i-codec-cpu-dai",
1177 .probe = sun4i_codec_dai_probe,
1179 .stream_name = "Playback",
1182 .rates = SUN4I_CODEC_RATES,
1183 .formats = SUN4I_CODEC_FORMATS,
1187 .stream_name = "Capture",
1190 .rates = SUN4I_CODEC_RATES,
1191 .formats = SUN4I_CODEC_FORMATS,
1196 static struct snd_soc_dai_link *sun4i_codec_create_link(struct device *dev,
1199 struct snd_soc_dai_link *link = devm_kzalloc(dev, sizeof(*link),
1205 link->stream_name = "CDC PCM";
1206 link->codec_dai_name = "Codec";
1207 link->cpu_dai_name = dev_name(dev);
1208 link->codec_name = dev_name(dev);
1209 link->platform_name = dev_name(dev);
1210 link->dai_fmt = SND_SOC_DAIFMT_I2S;
1217 static int sun4i_codec_spk_event(struct snd_soc_dapm_widget *w,
1218 struct snd_kcontrol *k, int event)
1220 struct sun4i_codec *scodec = snd_soc_card_get_drvdata(w->dapm->card);
1222 gpiod_set_value_cansleep(scodec->gpio_pa,
1223 !!SND_SOC_DAPM_EVENT_ON(event));
1228 static const struct snd_soc_dapm_widget sun4i_codec_card_dapm_widgets[] = {
1229 SND_SOC_DAPM_SPK("Speaker", sun4i_codec_spk_event),
1232 static const struct snd_soc_dapm_route sun4i_codec_card_dapm_routes[] = {
1233 { "Speaker", NULL, "HP Right" },
1234 { "Speaker", NULL, "HP Left" },
1237 static struct snd_soc_card *sun4i_codec_create_card(struct device *dev)
1239 struct snd_soc_card *card;
1241 card = devm_kzalloc(dev, sizeof(*card), GFP_KERNEL);
1243 return ERR_PTR(-ENOMEM);
1245 card->dai_link = sun4i_codec_create_link(dev, &card->num_links);
1246 if (!card->dai_link)
1247 return ERR_PTR(-ENOMEM);
1250 card->name = "sun4i-codec";
1251 card->dapm_widgets = sun4i_codec_card_dapm_widgets;
1252 card->num_dapm_widgets = ARRAY_SIZE(sun4i_codec_card_dapm_widgets);
1253 card->dapm_routes = sun4i_codec_card_dapm_routes;
1254 card->num_dapm_routes = ARRAY_SIZE(sun4i_codec_card_dapm_routes);
1259 static const struct snd_soc_dapm_widget sun6i_codec_card_dapm_widgets[] = {
1260 SND_SOC_DAPM_HP("Headphone", NULL),
1261 SND_SOC_DAPM_LINE("Line In", NULL),
1262 SND_SOC_DAPM_LINE("Line Out", NULL),
1263 SND_SOC_DAPM_MIC("Headset Mic", NULL),
1264 SND_SOC_DAPM_MIC("Mic", NULL),
1265 SND_SOC_DAPM_SPK("Speaker", sun4i_codec_spk_event),
1268 static struct snd_soc_card *sun6i_codec_create_card(struct device *dev)
1270 struct snd_soc_card *card;
1273 card = devm_kzalloc(dev, sizeof(*card), GFP_KERNEL);
1275 return ERR_PTR(-ENOMEM);
1277 card->dai_link = sun4i_codec_create_link(dev, &card->num_links);
1278 if (!card->dai_link)
1279 return ERR_PTR(-ENOMEM);
1282 card->name = "A31 Audio Codec";
1283 card->dapm_widgets = sun6i_codec_card_dapm_widgets;
1284 card->num_dapm_widgets = ARRAY_SIZE(sun6i_codec_card_dapm_widgets);
1285 card->fully_routed = true;
1287 ret = snd_soc_of_parse_audio_routing(card, "allwinner,audio-routing");
1289 dev_warn(dev, "failed to parse audio-routing: %d\n", ret);
1294 /* Connect digital side enables to analog side widgets */
1295 static const struct snd_soc_dapm_route sun8i_codec_card_routes[] = {
1297 { "Left ADC", NULL, "ADC Enable" },
1298 { "Right ADC", NULL, "ADC Enable" },
1299 { "Codec Capture", NULL, "Left ADC" },
1300 { "Codec Capture", NULL, "Right ADC" },
1303 { "Left DAC", NULL, "DAC Enable" },
1304 { "Right DAC", NULL, "DAC Enable" },
1305 { "Left DAC", NULL, "Codec Playback" },
1306 { "Right DAC", NULL, "Codec Playback" },
1309 static struct snd_soc_aux_dev aux_dev = {
1310 .name = "Codec Analog Controls",
1313 static struct snd_soc_card *sun8i_a23_codec_create_card(struct device *dev)
1315 struct snd_soc_card *card;
1318 card = devm_kzalloc(dev, sizeof(*card), GFP_KERNEL);
1320 return ERR_PTR(-ENOMEM);
1322 aux_dev.codec_of_node = of_parse_phandle(dev->of_node,
1323 "allwinner,codec-analog-controls",
1325 if (!aux_dev.codec_of_node) {
1326 dev_err(dev, "Can't find analog controls for codec.\n");
1327 return ERR_PTR(-EINVAL);
1330 card->dai_link = sun4i_codec_create_link(dev, &card->num_links);
1331 if (!card->dai_link)
1332 return ERR_PTR(-ENOMEM);
1335 card->name = "A23 Audio Codec";
1336 card->dapm_widgets = sun6i_codec_card_dapm_widgets;
1337 card->num_dapm_widgets = ARRAY_SIZE(sun6i_codec_card_dapm_widgets);
1338 card->dapm_routes = sun8i_codec_card_routes;
1339 card->num_dapm_routes = ARRAY_SIZE(sun8i_codec_card_routes);
1340 card->aux_dev = &aux_dev;
1341 card->num_aux_devs = 1;
1342 card->fully_routed = true;
1344 ret = snd_soc_of_parse_audio_routing(card, "allwinner,audio-routing");
1346 dev_warn(dev, "failed to parse audio-routing: %d\n", ret);
1351 static struct snd_soc_card *sun8i_h3_codec_create_card(struct device *dev)
1353 struct snd_soc_card *card;
1356 card = devm_kzalloc(dev, sizeof(*card), GFP_KERNEL);
1358 return ERR_PTR(-ENOMEM);
1360 aux_dev.codec_of_node = of_parse_phandle(dev->of_node,
1361 "allwinner,codec-analog-controls",
1363 if (!aux_dev.codec_of_node) {
1364 dev_err(dev, "Can't find analog controls for codec.\n");
1365 return ERR_PTR(-EINVAL);
1368 card->dai_link = sun4i_codec_create_link(dev, &card->num_links);
1369 if (!card->dai_link)
1370 return ERR_PTR(-ENOMEM);
1373 card->name = "H3 Audio Codec";
1374 card->dapm_widgets = sun6i_codec_card_dapm_widgets;
1375 card->num_dapm_widgets = ARRAY_SIZE(sun6i_codec_card_dapm_widgets);
1376 card->dapm_routes = sun8i_codec_card_routes;
1377 card->num_dapm_routes = ARRAY_SIZE(sun8i_codec_card_routes);
1378 card->aux_dev = &aux_dev;
1379 card->num_aux_devs = 1;
1380 card->fully_routed = true;
1382 ret = snd_soc_of_parse_audio_routing(card, "allwinner,audio-routing");
1384 dev_warn(dev, "failed to parse audio-routing: %d\n", ret);
1389 static struct snd_soc_card *sun8i_v3s_codec_create_card(struct device *dev)
1391 struct snd_soc_card *card;
1394 card = devm_kzalloc(dev, sizeof(*card), GFP_KERNEL);
1396 return ERR_PTR(-ENOMEM);
1398 aux_dev.codec_of_node = of_parse_phandle(dev->of_node,
1399 "allwinner,codec-analog-controls",
1401 if (!aux_dev.codec_of_node) {
1402 dev_err(dev, "Can't find analog controls for codec.\n");
1403 return ERR_PTR(-EINVAL);
1406 card->dai_link = sun4i_codec_create_link(dev, &card->num_links);
1407 if (!card->dai_link)
1408 return ERR_PTR(-ENOMEM);
1411 card->name = "V3s Audio Codec";
1412 card->dapm_widgets = sun6i_codec_card_dapm_widgets;
1413 card->num_dapm_widgets = ARRAY_SIZE(sun6i_codec_card_dapm_widgets);
1414 card->dapm_routes = sun8i_codec_card_routes;
1415 card->num_dapm_routes = ARRAY_SIZE(sun8i_codec_card_routes);
1416 card->aux_dev = &aux_dev;
1417 card->num_aux_devs = 1;
1418 card->fully_routed = true;
1420 ret = snd_soc_of_parse_audio_routing(card, "allwinner,audio-routing");
1422 dev_warn(dev, "failed to parse audio-routing: %d\n", ret);
1427 static const struct regmap_config sun4i_codec_regmap_config = {
1431 .max_register = SUN4I_CODEC_ADC_RXCNT,
1434 static const struct regmap_config sun6i_codec_regmap_config = {
1438 .max_register = SUN6I_CODEC_HMIC_DATA,
1441 static const struct regmap_config sun7i_codec_regmap_config = {
1445 .max_register = SUN7I_CODEC_AC_MIC_PHONE_CAL,
1448 static const struct regmap_config sun8i_a23_codec_regmap_config = {
1452 .max_register = SUN8I_A23_CODEC_ADC_RXCNT,
1455 static const struct regmap_config sun8i_h3_codec_regmap_config = {
1459 .max_register = SUN8I_H3_CODEC_ADC_DBG,
1462 static const struct regmap_config sun8i_v3s_codec_regmap_config = {
1466 .max_register = SUN8I_H3_CODEC_ADC_DBG,
1469 struct sun4i_codec_quirks {
1470 const struct regmap_config *regmap_config;
1471 const struct snd_soc_component_driver *codec;
1472 struct snd_soc_card * (*create_card)(struct device *dev);
1473 struct reg_field reg_adc_fifoc; /* used for regmap_field */
1474 unsigned int reg_dac_txdata; /* TX FIFO offset for DMA config */
1475 unsigned int reg_adc_rxdata; /* RX FIFO offset for DMA config */
1479 static const struct sun4i_codec_quirks sun4i_codec_quirks = {
1480 .regmap_config = &sun4i_codec_regmap_config,
1481 .codec = &sun4i_codec_codec,
1482 .create_card = sun4i_codec_create_card,
1483 .reg_adc_fifoc = REG_FIELD(SUN4I_CODEC_ADC_FIFOC, 0, 31),
1484 .reg_dac_txdata = SUN4I_CODEC_DAC_TXDATA,
1485 .reg_adc_rxdata = SUN4I_CODEC_ADC_RXDATA,
1488 static const struct sun4i_codec_quirks sun6i_a31_codec_quirks = {
1489 .regmap_config = &sun6i_codec_regmap_config,
1490 .codec = &sun6i_codec_codec,
1491 .create_card = sun6i_codec_create_card,
1492 .reg_adc_fifoc = REG_FIELD(SUN6I_CODEC_ADC_FIFOC, 0, 31),
1493 .reg_dac_txdata = SUN4I_CODEC_DAC_TXDATA,
1494 .reg_adc_rxdata = SUN6I_CODEC_ADC_RXDATA,
1498 static const struct sun4i_codec_quirks sun7i_codec_quirks = {
1499 .regmap_config = &sun7i_codec_regmap_config,
1500 .codec = &sun4i_codec_codec,
1501 .create_card = sun4i_codec_create_card,
1502 .reg_adc_fifoc = REG_FIELD(SUN4I_CODEC_ADC_FIFOC, 0, 31),
1503 .reg_dac_txdata = SUN4I_CODEC_DAC_TXDATA,
1504 .reg_adc_rxdata = SUN4I_CODEC_ADC_RXDATA,
1507 static const struct sun4i_codec_quirks sun8i_a23_codec_quirks = {
1508 .regmap_config = &sun8i_a23_codec_regmap_config,
1509 .codec = &sun8i_a23_codec_codec,
1510 .create_card = sun8i_a23_codec_create_card,
1511 .reg_adc_fifoc = REG_FIELD(SUN6I_CODEC_ADC_FIFOC, 0, 31),
1512 .reg_dac_txdata = SUN4I_CODEC_DAC_TXDATA,
1513 .reg_adc_rxdata = SUN6I_CODEC_ADC_RXDATA,
1517 static const struct sun4i_codec_quirks sun8i_h3_codec_quirks = {
1518 .regmap_config = &sun8i_h3_codec_regmap_config,
1520 * TODO Share the codec structure with A23 for now.
1521 * This should be split out when adding digital audio
1522 * processing support for the H3.
1524 .codec = &sun8i_a23_codec_codec,
1525 .create_card = sun8i_h3_codec_create_card,
1526 .reg_adc_fifoc = REG_FIELD(SUN6I_CODEC_ADC_FIFOC, 0, 31),
1527 .reg_dac_txdata = SUN8I_H3_CODEC_DAC_TXDATA,
1528 .reg_adc_rxdata = SUN6I_CODEC_ADC_RXDATA,
1532 static const struct sun4i_codec_quirks sun8i_v3s_codec_quirks = {
1533 .regmap_config = &sun8i_v3s_codec_regmap_config,
1535 * TODO The codec structure should be split out, like
1536 * H3, when adding digital audio processing support.
1538 .codec = &sun8i_a23_codec_codec,
1539 .create_card = sun8i_v3s_codec_create_card,
1540 .reg_adc_fifoc = REG_FIELD(SUN6I_CODEC_ADC_FIFOC, 0, 31),
1541 .reg_dac_txdata = SUN8I_H3_CODEC_DAC_TXDATA,
1542 .reg_adc_rxdata = SUN6I_CODEC_ADC_RXDATA,
1546 static const struct of_device_id sun4i_codec_of_match[] = {
1548 .compatible = "allwinner,sun4i-a10-codec",
1549 .data = &sun4i_codec_quirks,
1552 .compatible = "allwinner,sun6i-a31-codec",
1553 .data = &sun6i_a31_codec_quirks,
1556 .compatible = "allwinner,sun7i-a20-codec",
1557 .data = &sun7i_codec_quirks,
1560 .compatible = "allwinner,sun8i-a23-codec",
1561 .data = &sun8i_a23_codec_quirks,
1564 .compatible = "allwinner,sun8i-h3-codec",
1565 .data = &sun8i_h3_codec_quirks,
1568 .compatible = "allwinner,sun8i-v3s-codec",
1569 .data = &sun8i_v3s_codec_quirks,
1573 MODULE_DEVICE_TABLE(of, sun4i_codec_of_match);
1575 static int sun4i_codec_probe(struct platform_device *pdev)
1577 struct snd_soc_card *card;
1578 struct sun4i_codec *scodec;
1579 const struct sun4i_codec_quirks *quirks;
1580 struct resource *res;
1584 scodec = devm_kzalloc(&pdev->dev, sizeof(*scodec), GFP_KERNEL);
1588 scodec->dev = &pdev->dev;
1590 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1591 base = devm_ioremap_resource(&pdev->dev, res);
1593 dev_err(&pdev->dev, "Failed to map the registers\n");
1594 return PTR_ERR(base);
1597 quirks = of_device_get_match_data(&pdev->dev);
1598 if (quirks == NULL) {
1599 dev_err(&pdev->dev, "Failed to determine the quirks to use\n");
1603 scodec->regmap = devm_regmap_init_mmio(&pdev->dev, base,
1604 quirks->regmap_config);
1605 if (IS_ERR(scodec->regmap)) {
1606 dev_err(&pdev->dev, "Failed to create our regmap\n");
1607 return PTR_ERR(scodec->regmap);
1610 /* Get the clocks from the DT */
1611 scodec->clk_apb = devm_clk_get(&pdev->dev, "apb");
1612 if (IS_ERR(scodec->clk_apb)) {
1613 dev_err(&pdev->dev, "Failed to get the APB clock\n");
1614 return PTR_ERR(scodec->clk_apb);
1617 scodec->clk_module = devm_clk_get(&pdev->dev, "codec");
1618 if (IS_ERR(scodec->clk_module)) {
1619 dev_err(&pdev->dev, "Failed to get the module clock\n");
1620 return PTR_ERR(scodec->clk_module);
1623 if (quirks->has_reset) {
1624 scodec->rst = devm_reset_control_get_exclusive(&pdev->dev,
1626 if (IS_ERR(scodec->rst)) {
1627 dev_err(&pdev->dev, "Failed to get reset control\n");
1628 return PTR_ERR(scodec->rst);
1632 scodec->gpio_pa = devm_gpiod_get_optional(&pdev->dev, "allwinner,pa",
1634 if (IS_ERR(scodec->gpio_pa)) {
1635 ret = PTR_ERR(scodec->gpio_pa);
1636 if (ret != -EPROBE_DEFER)
1637 dev_err(&pdev->dev, "Failed to get pa gpio: %d\n", ret);
1641 /* reg_field setup */
1642 scodec->reg_adc_fifoc = devm_regmap_field_alloc(&pdev->dev,
1644 quirks->reg_adc_fifoc);
1645 if (IS_ERR(scodec->reg_adc_fifoc)) {
1646 ret = PTR_ERR(scodec->reg_adc_fifoc);
1647 dev_err(&pdev->dev, "Failed to create regmap fields: %d\n",
1652 /* Enable the bus clock */
1653 if (clk_prepare_enable(scodec->clk_apb)) {
1654 dev_err(&pdev->dev, "Failed to enable the APB clock\n");
1658 /* Deassert the reset control */
1660 ret = reset_control_deassert(scodec->rst);
1663 "Failed to deassert the reset control\n");
1664 goto err_clk_disable;
1668 /* DMA configuration for TX FIFO */
1669 scodec->playback_dma_data.addr = res->start + quirks->reg_dac_txdata;
1670 scodec->playback_dma_data.maxburst = 8;
1671 scodec->playback_dma_data.addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
1673 /* DMA configuration for RX FIFO */
1674 scodec->capture_dma_data.addr = res->start + quirks->reg_adc_rxdata;
1675 scodec->capture_dma_data.maxburst = 8;
1676 scodec->capture_dma_data.addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
1678 ret = devm_snd_soc_register_component(&pdev->dev, quirks->codec,
1679 &sun4i_codec_dai, 1);
1681 dev_err(&pdev->dev, "Failed to register our codec\n");
1682 goto err_assert_reset;
1685 ret = devm_snd_soc_register_component(&pdev->dev,
1686 &sun4i_codec_component,
1689 dev_err(&pdev->dev, "Failed to register our DAI\n");
1690 goto err_assert_reset;
1693 ret = devm_snd_dmaengine_pcm_register(&pdev->dev, NULL, 0);
1695 dev_err(&pdev->dev, "Failed to register against DMAEngine\n");
1696 goto err_assert_reset;
1699 card = quirks->create_card(&pdev->dev);
1701 ret = PTR_ERR(card);
1702 dev_err(&pdev->dev, "Failed to create our card\n");
1703 goto err_assert_reset;
1706 snd_soc_card_set_drvdata(card, scodec);
1708 ret = snd_soc_register_card(card);
1710 dev_err(&pdev->dev, "Failed to register our card\n");
1711 goto err_assert_reset;
1718 reset_control_assert(scodec->rst);
1720 clk_disable_unprepare(scodec->clk_apb);
1724 static int sun4i_codec_remove(struct platform_device *pdev)
1726 struct snd_soc_card *card = platform_get_drvdata(pdev);
1727 struct sun4i_codec *scodec = snd_soc_card_get_drvdata(card);
1729 snd_soc_unregister_card(card);
1731 reset_control_assert(scodec->rst);
1732 clk_disable_unprepare(scodec->clk_apb);
1737 static struct platform_driver sun4i_codec_driver = {
1739 .name = "sun4i-codec",
1740 .of_match_table = sun4i_codec_of_match,
1742 .probe = sun4i_codec_probe,
1743 .remove = sun4i_codec_remove,
1745 module_platform_driver(sun4i_codec_driver);
1747 MODULE_DESCRIPTION("Allwinner A10 codec driver");
1748 MODULE_AUTHOR("Emilio López <emilio@elopez.com.ar>");
1749 MODULE_AUTHOR("Jon Smirl <jonsmirl@gmail.com>");
1750 MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
1751 MODULE_AUTHOR("Chen-Yu Tsai <wens@csie.org>");
1752 MODULE_LICENSE("GPL");