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>
6 * Based on the Allwinner SDK driver, released under the GPL.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
19 #include <linux/init.h>
20 #include <linux/kernel.h>
21 #include <linux/module.h>
22 #include <linux/platform_device.h>
23 #include <linux/delay.h>
24 #include <linux/slab.h>
26 #include <linux/of_platform.h>
27 #include <linux/of_address.h>
28 #include <linux/clk.h>
29 #include <linux/regmap.h>
31 #include <sound/core.h>
32 #include <sound/pcm.h>
33 #include <sound/pcm_params.h>
34 #include <sound/soc.h>
35 #include <sound/tlv.h>
36 #include <sound/initval.h>
37 #include <sound/dmaengine_pcm.h>
39 /* Codec DAC register offsets and bit fields */
40 #define SUN4I_CODEC_DAC_DPC (0x00)
41 #define SUN4I_CODEC_DAC_DPC_EN_DA (31)
42 #define SUN4I_CODEC_DAC_DPC_DVOL (12)
43 #define SUN4I_CODEC_DAC_FIFOC (0x04)
44 #define SUN4I_CODEC_DAC_FIFOC_DAC_FS (29)
45 #define SUN4I_CODEC_DAC_FIFOC_FIR_VERSION (28)
46 #define SUN4I_CODEC_DAC_FIFOC_SEND_LASAT (26)
47 #define SUN4I_CODEC_DAC_FIFOC_TX_FIFO_MODE (24)
48 #define SUN4I_CODEC_DAC_FIFOC_DRQ_CLR_CNT (21)
49 #define SUN4I_CODEC_DAC_FIFOC_TX_TRIG_LEVEL (8)
50 #define SUN4I_CODEC_DAC_FIFOC_MONO_EN (6)
51 #define SUN4I_CODEC_DAC_FIFOC_TX_SAMPLE_BITS (5)
52 #define SUN4I_CODEC_DAC_FIFOC_DAC_DRQ_EN (4)
53 #define SUN4I_CODEC_DAC_FIFOC_FIFO_FLUSH (0)
54 #define SUN4I_CODEC_DAC_FIFOS (0x08)
55 #define SUN4I_CODEC_DAC_TXDATA (0x0c)
56 #define SUN4I_CODEC_DAC_ACTL (0x10)
57 #define SUN4I_CODEC_DAC_ACTL_DACAENR (31)
58 #define SUN4I_CODEC_DAC_ACTL_DACAENL (30)
59 #define SUN4I_CODEC_DAC_ACTL_MIXEN (29)
60 #define SUN4I_CODEC_DAC_ACTL_LDACLMIXS (15)
61 #define SUN4I_CODEC_DAC_ACTL_RDACRMIXS (14)
62 #define SUN4I_CODEC_DAC_ACTL_LDACRMIXS (13)
63 #define SUN4I_CODEC_DAC_ACTL_DACPAS (8)
64 #define SUN4I_CODEC_DAC_ACTL_MIXPAS (7)
65 #define SUN4I_CODEC_DAC_ACTL_PA_MUTE (6)
66 #define SUN4I_CODEC_DAC_ACTL_PA_VOL (0)
67 #define SUN4I_CODEC_DAC_TUNE (0x14)
68 #define SUN4I_CODEC_DAC_DEBUG (0x18)
70 /* Codec ADC register offsets and bit fields */
71 #define SUN4I_CODEC_ADC_FIFOC (0x1c)
72 #define SUN4I_CODEC_ADC_FIFOC_ADC_FS (29)
73 #define SUN4I_CODEC_ADC_FIFOC_EN_AD (28)
74 #define SUN4I_CODEC_ADC_FIFOC_RX_FIFO_MODE (24)
75 #define SUN4I_CODEC_ADC_FIFOC_RX_TRIG_LEVEL (8)
76 #define SUN4I_CODEC_ADC_FIFOC_MONO_EN (7)
77 #define SUN4I_CODEC_ADC_FIFOC_RX_SAMPLE_BITS (6)
78 #define SUN4I_CODEC_ADC_FIFOC_ADC_DRQ_EN (4)
79 #define SUN4I_CODEC_ADC_FIFOC_FIFO_FLUSH (0)
80 #define SUN4I_CODEC_ADC_FIFOS (0x20)
81 #define SUN4I_CODEC_ADC_RXDATA (0x24)
82 #define SUN4I_CODEC_ADC_ACTL (0x28)
83 #define SUN4I_CODEC_ADC_ACTL_ADC_R_EN (31)
84 #define SUN4I_CODEC_ADC_ACTL_ADC_L_EN (30)
85 #define SUN4I_CODEC_ADC_ACTL_PREG1EN (29)
86 #define SUN4I_CODEC_ADC_ACTL_PREG2EN (28)
87 #define SUN4I_CODEC_ADC_ACTL_VMICEN (27)
88 #define SUN4I_CODEC_ADC_ACTL_VADCG (20)
89 #define SUN4I_CODEC_ADC_ACTL_ADCIS (17)
90 #define SUN4I_CODEC_ADC_ACTL_PA_EN (4)
91 #define SUN4I_CODEC_ADC_ACTL_DDE (3)
92 #define SUN4I_CODEC_ADC_DEBUG (0x2c)
94 /* Other various ADC registers */
95 #define SUN4I_CODEC_DAC_TXCNT (0x30)
96 #define SUN4I_CODEC_ADC_RXCNT (0x34)
97 #define SUN4I_CODEC_AC_SYS_VERI (0x38)
98 #define SUN4I_CODEC_AC_MIC_PHONE_CAL (0x3c)
102 struct regmap
*regmap
;
104 struct clk
*clk_module
;
106 struct snd_dmaengine_dai_dma_data capture_dma_data
;
107 struct snd_dmaengine_dai_dma_data playback_dma_data
;
110 static void sun4i_codec_start_playback(struct sun4i_codec
*scodec
)
113 regmap_update_bits(scodec
->regmap
, SUN4I_CODEC_DAC_FIFOC
,
114 BIT(SUN4I_CODEC_DAC_FIFOC_FIFO_FLUSH
),
115 BIT(SUN4I_CODEC_DAC_FIFOC_FIFO_FLUSH
));
118 regmap_update_bits(scodec
->regmap
, SUN4I_CODEC_DAC_FIFOC
,
119 BIT(SUN4I_CODEC_DAC_FIFOC_DAC_DRQ_EN
),
120 BIT(SUN4I_CODEC_DAC_FIFOC_DAC_DRQ_EN
));
123 static void sun4i_codec_stop_playback(struct sun4i_codec
*scodec
)
125 /* Disable DAC DRQ */
126 regmap_update_bits(scodec
->regmap
, SUN4I_CODEC_DAC_FIFOC
,
127 BIT(SUN4I_CODEC_DAC_FIFOC_DAC_DRQ_EN
),
131 static void sun4i_codec_start_capture(struct sun4i_codec
*scodec
)
134 regmap_update_bits(scodec
->regmap
, SUN4I_CODEC_ADC_FIFOC
,
135 BIT(SUN4I_CODEC_ADC_FIFOC_ADC_DRQ_EN
),
136 BIT(SUN4I_CODEC_ADC_FIFOC_ADC_DRQ_EN
));
139 static void sun4i_codec_stop_capture(struct sun4i_codec
*scodec
)
141 /* Disable ADC DRQ */
142 regmap_update_bits(scodec
->regmap
, SUN4I_CODEC_ADC_FIFOC
,
143 BIT(SUN4I_CODEC_ADC_FIFOC_ADC_DRQ_EN
), 0);
146 static int sun4i_codec_trigger(struct snd_pcm_substream
*substream
, int cmd
,
147 struct snd_soc_dai
*dai
)
149 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
150 struct sun4i_codec
*scodec
= snd_soc_card_get_drvdata(rtd
->card
);
153 case SNDRV_PCM_TRIGGER_START
:
154 case SNDRV_PCM_TRIGGER_RESUME
:
155 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
156 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
157 sun4i_codec_start_playback(scodec
);
159 sun4i_codec_start_capture(scodec
);
162 case SNDRV_PCM_TRIGGER_STOP
:
163 case SNDRV_PCM_TRIGGER_SUSPEND
:
164 case SNDRV_PCM_TRIGGER_PAUSE_PUSH
:
165 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
166 sun4i_codec_stop_playback(scodec
);
168 sun4i_codec_stop_capture(scodec
);
178 static int sun4i_codec_prepare_capture(struct snd_pcm_substream
*substream
,
179 struct snd_soc_dai
*dai
)
181 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
182 struct sun4i_codec
*scodec
= snd_soc_card_get_drvdata(rtd
->card
);
186 regmap_update_bits(scodec
->regmap
, SUN4I_CODEC_ADC_FIFOC
,
187 BIT(SUN4I_CODEC_ADC_FIFOC_FIFO_FLUSH
),
188 BIT(SUN4I_CODEC_ADC_FIFOC_FIFO_FLUSH
));
191 /* Set RX FIFO trigger level */
192 regmap_update_bits(scodec
->regmap
, SUN4I_CODEC_ADC_FIFOC
,
193 0xf << SUN4I_CODEC_ADC_FIFOC_RX_TRIG_LEVEL
,
194 0x7 << SUN4I_CODEC_ADC_FIFOC_RX_TRIG_LEVEL
);
197 * FIXME: Undocumented in the datasheet, but
198 * Allwinner's code mentions that it is related
199 * related to microphone gain
201 regmap_update_bits(scodec
->regmap
, SUN4I_CODEC_ADC_ACTL
,
205 if (of_device_is_compatible(scodec
->dev
->of_node
,
206 "allwinner,sun7i-a20-codec"))
207 /* FIXME: Undocumented bits */
208 regmap_update_bits(scodec
->regmap
, SUN4I_CODEC_DAC_TUNE
,
212 /* Fill most significant bits with valid data MSB */
213 regmap_update_bits(scodec
->regmap
, SUN4I_CODEC_ADC_FIFOC
,
214 BIT(SUN4I_CODEC_ADC_FIFOC_RX_FIFO_MODE
),
215 BIT(SUN4I_CODEC_ADC_FIFOC_RX_FIFO_MODE
));
220 static int sun4i_codec_prepare_playback(struct snd_pcm_substream
*substream
,
221 struct snd_soc_dai
*dai
)
223 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
224 struct sun4i_codec
*scodec
= snd_soc_card_get_drvdata(rtd
->card
);
227 /* Flush the TX FIFO */
228 regmap_update_bits(scodec
->regmap
, SUN4I_CODEC_DAC_FIFOC
,
229 BIT(SUN4I_CODEC_DAC_FIFOC_FIFO_FLUSH
),
230 BIT(SUN4I_CODEC_DAC_FIFOC_FIFO_FLUSH
));
232 /* Set TX FIFO Empty Trigger Level */
233 regmap_update_bits(scodec
->regmap
, SUN4I_CODEC_DAC_FIFOC
,
234 0x3f << SUN4I_CODEC_DAC_FIFOC_TX_TRIG_LEVEL
,
235 0xf << SUN4I_CODEC_DAC_FIFOC_TX_TRIG_LEVEL
);
237 if (substream
->runtime
->rate
> 32000)
238 /* Use 64 bits FIR filter */
241 /* Use 32 bits FIR filter */
242 val
= BIT(SUN4I_CODEC_DAC_FIFOC_FIR_VERSION
);
244 regmap_update_bits(scodec
->regmap
, SUN4I_CODEC_DAC_FIFOC
,
245 BIT(SUN4I_CODEC_DAC_FIFOC_FIR_VERSION
),
248 /* Send zeros when we have an underrun */
249 regmap_update_bits(scodec
->regmap
, SUN4I_CODEC_DAC_FIFOC
,
250 BIT(SUN4I_CODEC_DAC_FIFOC_SEND_LASAT
),
256 static int sun4i_codec_prepare(struct snd_pcm_substream
*substream
,
257 struct snd_soc_dai
*dai
)
259 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
260 return sun4i_codec_prepare_playback(substream
, dai
);
262 return sun4i_codec_prepare_capture(substream
, dai
);
265 static unsigned long sun4i_codec_get_mod_freq(struct snd_pcm_hw_params
*params
)
267 unsigned int rate
= params_rate(params
);
295 static int sun4i_codec_get_hw_rate(struct snd_pcm_hw_params
*params
)
297 unsigned int rate
= params_rate(params
);
337 static int sun4i_codec_hw_params_capture(struct sun4i_codec
*scodec
,
338 struct snd_pcm_hw_params
*params
,
341 /* Set ADC sample rate */
342 regmap_update_bits(scodec
->regmap
, SUN4I_CODEC_ADC_FIFOC
,
343 7 << SUN4I_CODEC_ADC_FIFOC_ADC_FS
,
344 hwrate
<< SUN4I_CODEC_ADC_FIFOC_ADC_FS
);
346 /* Set the number of channels we want to use */
347 if (params_channels(params
) == 1)
348 regmap_update_bits(scodec
->regmap
, SUN4I_CODEC_ADC_FIFOC
,
349 BIT(SUN4I_CODEC_ADC_FIFOC_MONO_EN
),
350 BIT(SUN4I_CODEC_ADC_FIFOC_MONO_EN
));
352 regmap_update_bits(scodec
->regmap
, SUN4I_CODEC_ADC_FIFOC
,
353 BIT(SUN4I_CODEC_ADC_FIFOC_MONO_EN
), 0);
358 static int sun4i_codec_hw_params_playback(struct sun4i_codec
*scodec
,
359 struct snd_pcm_hw_params
*params
,
364 /* Set DAC sample rate */
365 regmap_update_bits(scodec
->regmap
, SUN4I_CODEC_DAC_FIFOC
,
366 7 << SUN4I_CODEC_DAC_FIFOC_DAC_FS
,
367 hwrate
<< SUN4I_CODEC_DAC_FIFOC_DAC_FS
);
369 /* Set the number of channels we want to use */
370 if (params_channels(params
) == 1)
371 val
= BIT(SUN4I_CODEC_DAC_FIFOC_MONO_EN
);
375 regmap_update_bits(scodec
->regmap
, SUN4I_CODEC_DAC_FIFOC
,
376 BIT(SUN4I_CODEC_DAC_FIFOC_MONO_EN
),
379 /* Set the number of sample bits to either 16 or 24 bits */
380 if (hw_param_interval(params
, SNDRV_PCM_HW_PARAM_SAMPLE_BITS
)->min
== 32) {
381 regmap_update_bits(scodec
->regmap
, SUN4I_CODEC_DAC_FIFOC
,
382 BIT(SUN4I_CODEC_DAC_FIFOC_TX_SAMPLE_BITS
),
383 BIT(SUN4I_CODEC_DAC_FIFOC_TX_SAMPLE_BITS
));
385 /* Set TX FIFO mode to padding the LSBs with 0 */
386 regmap_update_bits(scodec
->regmap
, SUN4I_CODEC_DAC_FIFOC
,
387 BIT(SUN4I_CODEC_DAC_FIFOC_TX_FIFO_MODE
),
390 scodec
->playback_dma_data
.addr_width
= DMA_SLAVE_BUSWIDTH_4_BYTES
;
392 regmap_update_bits(scodec
->regmap
, SUN4I_CODEC_DAC_FIFOC
,
393 BIT(SUN4I_CODEC_DAC_FIFOC_TX_SAMPLE_BITS
),
396 /* Set TX FIFO mode to repeat the MSB */
397 regmap_update_bits(scodec
->regmap
, SUN4I_CODEC_DAC_FIFOC
,
398 BIT(SUN4I_CODEC_DAC_FIFOC_TX_FIFO_MODE
),
399 BIT(SUN4I_CODEC_DAC_FIFOC_TX_FIFO_MODE
));
401 scodec
->playback_dma_data
.addr_width
= DMA_SLAVE_BUSWIDTH_2_BYTES
;
407 static int sun4i_codec_hw_params(struct snd_pcm_substream
*substream
,
408 struct snd_pcm_hw_params
*params
,
409 struct snd_soc_dai
*dai
)
411 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
412 struct sun4i_codec
*scodec
= snd_soc_card_get_drvdata(rtd
->card
);
413 unsigned long clk_freq
;
416 clk_freq
= sun4i_codec_get_mod_freq(params
);
420 ret
= clk_set_rate(scodec
->clk_module
, clk_freq
);
424 hwrate
= sun4i_codec_get_hw_rate(params
);
428 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
429 return sun4i_codec_hw_params_playback(scodec
, params
,
432 return sun4i_codec_hw_params_capture(scodec
, params
,
436 static int sun4i_codec_startup(struct snd_pcm_substream
*substream
,
437 struct snd_soc_dai
*dai
)
439 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
440 struct sun4i_codec
*scodec
= snd_soc_card_get_drvdata(rtd
->card
);
443 * Stop issuing DRQ when we have room for less than 16 samples
446 regmap_update_bits(scodec
->regmap
, SUN4I_CODEC_DAC_FIFOC
,
447 3 << SUN4I_CODEC_DAC_FIFOC_DRQ_CLR_CNT
,
448 3 << SUN4I_CODEC_DAC_FIFOC_DRQ_CLR_CNT
);
450 return clk_prepare_enable(scodec
->clk_module
);
453 static void sun4i_codec_shutdown(struct snd_pcm_substream
*substream
,
454 struct snd_soc_dai
*dai
)
456 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
457 struct sun4i_codec
*scodec
= snd_soc_card_get_drvdata(rtd
->card
);
459 clk_disable_unprepare(scodec
->clk_module
);
462 static const struct snd_soc_dai_ops sun4i_codec_dai_ops
= {
463 .startup
= sun4i_codec_startup
,
464 .shutdown
= sun4i_codec_shutdown
,
465 .trigger
= sun4i_codec_trigger
,
466 .hw_params
= sun4i_codec_hw_params
,
467 .prepare
= sun4i_codec_prepare
,
470 static struct snd_soc_dai_driver sun4i_codec_dai
= {
472 .ops
= &sun4i_codec_dai_ops
,
474 .stream_name
= "Codec Playback",
479 .rates
= SNDRV_PCM_RATE_8000_48000
|
480 SNDRV_PCM_RATE_96000
|
481 SNDRV_PCM_RATE_192000
,
482 .formats
= SNDRV_PCM_FMTBIT_S16_LE
|
483 SNDRV_PCM_FMTBIT_S32_LE
,
487 .stream_name
= "Codec Capture",
492 .rates
= SNDRV_PCM_RATE_8000_48000
|
493 SNDRV_PCM_RATE_96000
|
494 SNDRV_PCM_RATE_192000
|
496 .formats
= SNDRV_PCM_FMTBIT_S16_LE
|
497 SNDRV_PCM_FMTBIT_S32_LE
,
503 static const struct snd_kcontrol_new sun4i_codec_pa_mute
=
504 SOC_DAPM_SINGLE("Switch", SUN4I_CODEC_DAC_ACTL
,
505 SUN4I_CODEC_DAC_ACTL_PA_MUTE
, 1, 0);
507 static DECLARE_TLV_DB_SCALE(sun4i_codec_pa_volume_scale
, -6300, 100, 1);
509 static const struct snd_kcontrol_new sun4i_codec_widgets
[] = {
510 SOC_SINGLE_TLV("PA Volume", SUN4I_CODEC_DAC_ACTL
,
511 SUN4I_CODEC_DAC_ACTL_PA_VOL
, 0x3F, 0,
512 sun4i_codec_pa_volume_scale
),
515 static const struct snd_kcontrol_new sun4i_codec_left_mixer_controls
[] = {
516 SOC_DAPM_SINGLE("Left DAC Playback Switch", SUN4I_CODEC_DAC_ACTL
,
517 SUN4I_CODEC_DAC_ACTL_LDACLMIXS
, 1, 0),
520 static const struct snd_kcontrol_new sun4i_codec_right_mixer_controls
[] = {
521 SOC_DAPM_SINGLE("Right DAC Playback Switch", SUN4I_CODEC_DAC_ACTL
,
522 SUN4I_CODEC_DAC_ACTL_RDACRMIXS
, 1, 0),
523 SOC_DAPM_SINGLE("Left DAC Playback Switch", SUN4I_CODEC_DAC_ACTL
,
524 SUN4I_CODEC_DAC_ACTL_LDACRMIXS
, 1, 0),
527 static const struct snd_kcontrol_new sun4i_codec_pa_mixer_controls
[] = {
528 SOC_DAPM_SINGLE("DAC Playback Switch", SUN4I_CODEC_DAC_ACTL
,
529 SUN4I_CODEC_DAC_ACTL_DACPAS
, 1, 0),
530 SOC_DAPM_SINGLE("Mixer Playback Switch", SUN4I_CODEC_DAC_ACTL
,
531 SUN4I_CODEC_DAC_ACTL_MIXPAS
, 1, 0),
534 static const struct snd_soc_dapm_widget sun4i_codec_dapm_widgets
[] = {
535 /* Digital parts of the ADCs */
536 SND_SOC_DAPM_SUPPLY("ADC", SUN4I_CODEC_ADC_FIFOC
,
537 SUN4I_CODEC_ADC_FIFOC_EN_AD
, 0,
540 /* Digital parts of the DACs */
541 SND_SOC_DAPM_SUPPLY("DAC", SUN4I_CODEC_DAC_DPC
,
542 SUN4I_CODEC_DAC_DPC_EN_DA
, 0,
545 /* Analog parts of the ADCs */
546 SND_SOC_DAPM_ADC("Left ADC", "Codec Capture", SUN4I_CODEC_ADC_ACTL
,
547 SUN4I_CODEC_ADC_ACTL_ADC_L_EN
, 0),
548 SND_SOC_DAPM_ADC("Right ADC", "Codec Capture", SUN4I_CODEC_ADC_ACTL
,
549 SUN4I_CODEC_ADC_ACTL_ADC_R_EN
, 0),
551 /* Analog parts of the DACs */
552 SND_SOC_DAPM_DAC("Left DAC", "Codec Playback", SUN4I_CODEC_DAC_ACTL
,
553 SUN4I_CODEC_DAC_ACTL_DACAENL
, 0),
554 SND_SOC_DAPM_DAC("Right DAC", "Codec Playback", SUN4I_CODEC_DAC_ACTL
,
555 SUN4I_CODEC_DAC_ACTL_DACAENR
, 0),
558 SND_SOC_DAPM_MIXER("Left Mixer", SND_SOC_NOPM
, 0, 0,
559 sun4i_codec_left_mixer_controls
,
560 ARRAY_SIZE(sun4i_codec_left_mixer_controls
)),
561 SND_SOC_DAPM_MIXER("Right Mixer", SND_SOC_NOPM
, 0, 0,
562 sun4i_codec_right_mixer_controls
,
563 ARRAY_SIZE(sun4i_codec_right_mixer_controls
)),
565 /* Global Mixer Enable */
566 SND_SOC_DAPM_SUPPLY("Mixer Enable", SUN4I_CODEC_DAC_ACTL
,
567 SUN4I_CODEC_DAC_ACTL_MIXEN
, 0, NULL
, 0),
570 SND_SOC_DAPM_SUPPLY("VMIC", SUN4I_CODEC_ADC_ACTL
,
571 SUN4I_CODEC_ADC_ACTL_VMICEN
, 0, NULL
, 0),
573 /* Mic Pre-Amplifiers */
574 SND_SOC_DAPM_PGA("MIC1 Pre-Amplifier", SUN4I_CODEC_ADC_ACTL
,
575 SUN4I_CODEC_ADC_ACTL_PREG1EN
, 0, NULL
, 0),
578 SND_SOC_DAPM_MIXER("Pre-Amplifier", SUN4I_CODEC_ADC_ACTL
,
579 SUN4I_CODEC_ADC_ACTL_PA_EN
, 0,
580 sun4i_codec_pa_mixer_controls
,
581 ARRAY_SIZE(sun4i_codec_pa_mixer_controls
)),
582 SND_SOC_DAPM_SWITCH("Pre-Amplifier Mute", SND_SOC_NOPM
, 0, 0,
583 &sun4i_codec_pa_mute
),
585 SND_SOC_DAPM_INPUT("Mic1"),
587 SND_SOC_DAPM_OUTPUT("HP Right"),
588 SND_SOC_DAPM_OUTPUT("HP Left"),
591 static const struct snd_soc_dapm_route sun4i_codec_dapm_routes
[] = {
592 /* Left ADC / DAC Routes */
593 { "Left ADC", NULL
, "ADC" },
594 { "Left DAC", NULL
, "DAC" },
596 /* Right ADC / DAC Routes */
597 { "Right ADC", NULL
, "ADC" },
598 { "Right DAC", NULL
, "DAC" },
600 /* Right Mixer Routes */
601 { "Right Mixer", NULL
, "Mixer Enable" },
602 { "Right Mixer", "Left DAC Playback Switch", "Left DAC" },
603 { "Right Mixer", "Right DAC Playback Switch", "Right DAC" },
605 /* Left Mixer Routes */
606 { "Left Mixer", NULL
, "Mixer Enable" },
607 { "Left Mixer", "Left DAC Playback Switch", "Left DAC" },
609 /* Pre-Amplifier Mixer Routes */
610 { "Pre-Amplifier", "Mixer Playback Switch", "Left Mixer" },
611 { "Pre-Amplifier", "Mixer Playback Switch", "Right Mixer" },
612 { "Pre-Amplifier", "DAC Playback Switch", "Left DAC" },
613 { "Pre-Amplifier", "DAC Playback Switch", "Right DAC" },
616 { "Pre-Amplifier Mute", "Switch", "Pre-Amplifier" },
617 { "HP Right", NULL
, "Pre-Amplifier Mute" },
618 { "HP Left", NULL
, "Pre-Amplifier Mute" },
621 { "Left ADC", NULL
, "MIC1 Pre-Amplifier" },
622 { "Right ADC", NULL
, "MIC1 Pre-Amplifier" },
623 { "MIC1 Pre-Amplifier", NULL
, "Mic1"},
624 { "Mic1", NULL
, "VMIC" },
627 static struct snd_soc_codec_driver sun4i_codec_codec
= {
628 .controls
= sun4i_codec_widgets
,
629 .num_controls
= ARRAY_SIZE(sun4i_codec_widgets
),
630 .dapm_widgets
= sun4i_codec_dapm_widgets
,
631 .num_dapm_widgets
= ARRAY_SIZE(sun4i_codec_dapm_widgets
),
632 .dapm_routes
= sun4i_codec_dapm_routes
,
633 .num_dapm_routes
= ARRAY_SIZE(sun4i_codec_dapm_routes
),
636 static const struct snd_soc_component_driver sun4i_codec_component
= {
637 .name
= "sun4i-codec",
640 #define SUN4I_CODEC_RATES SNDRV_PCM_RATE_8000_192000
641 #define SUN4I_CODEC_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | \
642 SNDRV_PCM_FMTBIT_S32_LE)
644 static int sun4i_codec_dai_probe(struct snd_soc_dai
*dai
)
646 struct snd_soc_card
*card
= snd_soc_dai_get_drvdata(dai
);
647 struct sun4i_codec
*scodec
= snd_soc_card_get_drvdata(card
);
649 snd_soc_dai_init_dma_data(dai
, &scodec
->playback_dma_data
,
650 &scodec
->capture_dma_data
);
655 static struct snd_soc_dai_driver dummy_cpu_dai
= {
656 .name
= "sun4i-codec-cpu-dai",
657 .probe
= sun4i_codec_dai_probe
,
659 .stream_name
= "Playback",
662 .rates
= SUN4I_CODEC_RATES
,
663 .formats
= SUN4I_CODEC_FORMATS
,
667 .stream_name
= "Capture",
670 .rates
= SUN4I_CODEC_RATES
,
671 .formats
= SUN4I_CODEC_FORMATS
,
676 static const struct regmap_config sun4i_codec_regmap_config
= {
680 .max_register
= SUN4I_CODEC_AC_MIC_PHONE_CAL
,
683 static const struct of_device_id sun4i_codec_of_match
[] = {
684 { .compatible
= "allwinner,sun4i-a10-codec" },
685 { .compatible
= "allwinner,sun7i-a20-codec" },
688 MODULE_DEVICE_TABLE(of
, sun4i_codec_of_match
);
690 static struct snd_soc_dai_link
*sun4i_codec_create_link(struct device
*dev
,
693 struct snd_soc_dai_link
*link
= devm_kzalloc(dev
, sizeof(*link
),
699 link
->stream_name
= "CDC PCM";
700 link
->codec_dai_name
= "Codec";
701 link
->cpu_dai_name
= dev_name(dev
);
702 link
->codec_name
= dev_name(dev
);
703 link
->platform_name
= dev_name(dev
);
704 link
->dai_fmt
= SND_SOC_DAIFMT_I2S
;
711 static struct snd_soc_card
*sun4i_codec_create_card(struct device
*dev
)
713 struct snd_soc_card
*card
;
715 card
= devm_kzalloc(dev
, sizeof(*card
), GFP_KERNEL
);
719 card
->dai_link
= sun4i_codec_create_link(dev
, &card
->num_links
);
724 card
->name
= "sun4i-codec";
729 static int sun4i_codec_probe(struct platform_device
*pdev
)
731 struct snd_soc_card
*card
;
732 struct sun4i_codec
*scodec
;
733 struct resource
*res
;
737 scodec
= devm_kzalloc(&pdev
->dev
, sizeof(*scodec
), GFP_KERNEL
);
741 scodec
->dev
= &pdev
->dev
;
743 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
744 base
= devm_ioremap_resource(&pdev
->dev
, res
);
746 dev_err(&pdev
->dev
, "Failed to map the registers\n");
747 return PTR_ERR(base
);
750 scodec
->regmap
= devm_regmap_init_mmio(&pdev
->dev
, base
,
751 &sun4i_codec_regmap_config
);
752 if (IS_ERR(scodec
->regmap
)) {
753 dev_err(&pdev
->dev
, "Failed to create our regmap\n");
754 return PTR_ERR(scodec
->regmap
);
757 /* Get the clocks from the DT */
758 scodec
->clk_apb
= devm_clk_get(&pdev
->dev
, "apb");
759 if (IS_ERR(scodec
->clk_apb
)) {
760 dev_err(&pdev
->dev
, "Failed to get the APB clock\n");
761 return PTR_ERR(scodec
->clk_apb
);
764 scodec
->clk_module
= devm_clk_get(&pdev
->dev
, "codec");
765 if (IS_ERR(scodec
->clk_module
)) {
766 dev_err(&pdev
->dev
, "Failed to get the module clock\n");
767 return PTR_ERR(scodec
->clk_module
);
770 /* Enable the bus clock */
771 if (clk_prepare_enable(scodec
->clk_apb
)) {
772 dev_err(&pdev
->dev
, "Failed to enable the APB clock\n");
776 /* DMA configuration for TX FIFO */
777 scodec
->playback_dma_data
.addr
= res
->start
+ SUN4I_CODEC_DAC_TXDATA
;
778 scodec
->playback_dma_data
.maxburst
= 4;
779 scodec
->playback_dma_data
.addr_width
= DMA_SLAVE_BUSWIDTH_2_BYTES
;
781 /* DMA configuration for RX FIFO */
782 scodec
->capture_dma_data
.addr
= res
->start
+ SUN4I_CODEC_ADC_RXDATA
;
783 scodec
->capture_dma_data
.maxburst
= 4;
784 scodec
->capture_dma_data
.addr_width
= DMA_SLAVE_BUSWIDTH_2_BYTES
;
786 ret
= snd_soc_register_codec(&pdev
->dev
, &sun4i_codec_codec
,
787 &sun4i_codec_dai
, 1);
789 dev_err(&pdev
->dev
, "Failed to register our codec\n");
790 goto err_clk_disable
;
793 ret
= devm_snd_soc_register_component(&pdev
->dev
,
794 &sun4i_codec_component
,
797 dev_err(&pdev
->dev
, "Failed to register our DAI\n");
798 goto err_unregister_codec
;
801 ret
= devm_snd_dmaengine_pcm_register(&pdev
->dev
, NULL
, 0);
803 dev_err(&pdev
->dev
, "Failed to register against DMAEngine\n");
804 goto err_unregister_codec
;
807 card
= sun4i_codec_create_card(&pdev
->dev
);
809 dev_err(&pdev
->dev
, "Failed to create our card\n");
810 goto err_unregister_codec
;
813 platform_set_drvdata(pdev
, card
);
814 snd_soc_card_set_drvdata(card
, scodec
);
816 ret
= snd_soc_register_card(card
);
818 dev_err(&pdev
->dev
, "Failed to register our card\n");
819 goto err_unregister_codec
;
824 err_unregister_codec
:
825 snd_soc_unregister_codec(&pdev
->dev
);
827 clk_disable_unprepare(scodec
->clk_apb
);
831 static int sun4i_codec_remove(struct platform_device
*pdev
)
833 struct snd_soc_card
*card
= platform_get_drvdata(pdev
);
834 struct sun4i_codec
*scodec
= snd_soc_card_get_drvdata(card
);
836 snd_soc_unregister_card(card
);
837 snd_soc_unregister_codec(&pdev
->dev
);
838 clk_disable_unprepare(scodec
->clk_apb
);
843 static struct platform_driver sun4i_codec_driver
= {
845 .name
= "sun4i-codec",
846 .of_match_table
= sun4i_codec_of_match
,
848 .probe
= sun4i_codec_probe
,
849 .remove
= sun4i_codec_remove
,
851 module_platform_driver(sun4i_codec_driver
);
853 MODULE_DESCRIPTION("Allwinner A10 codec driver");
854 MODULE_AUTHOR("Emilio López <emilio@elopez.com.ar>");
855 MODULE_AUTHOR("Jon Smirl <jonsmirl@gmail.com>");
856 MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
857 MODULE_LICENSE("GPL");