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_EN_AD (28)
73 #define SUN4I_CODEC_ADC_FIFOC_RX_FIFO_MODE (24)
74 #define SUN4I_CODEC_ADC_FIFOC_RX_TRIG_LEVEL (8)
75 #define SUN4I_CODEC_ADC_FIFOC_MONO_EN (7)
76 #define SUN4I_CODEC_ADC_FIFOC_RX_SAMPLE_BITS (6)
77 #define SUN4I_CODEC_ADC_FIFOC_ADC_DRQ_EN (4)
78 #define SUN4I_CODEC_ADC_FIFOC_FIFO_FLUSH (0)
79 #define SUN4I_CODEC_ADC_FIFOS (0x20)
80 #define SUN4I_CODEC_ADC_RXDATA (0x24)
81 #define SUN4I_CODEC_ADC_ACTL (0x28)
82 #define SUN4I_CODEC_ADC_ACTL_ADC_R_EN (31)
83 #define SUN4I_CODEC_ADC_ACTL_ADC_L_EN (30)
84 #define SUN4I_CODEC_ADC_ACTL_PREG1EN (29)
85 #define SUN4I_CODEC_ADC_ACTL_PREG2EN (28)
86 #define SUN4I_CODEC_ADC_ACTL_VMICEN (27)
87 #define SUN4I_CODEC_ADC_ACTL_VADCG (20)
88 #define SUN4I_CODEC_ADC_ACTL_ADCIS (17)
89 #define SUN4I_CODEC_ADC_ACTL_PA_EN (4)
90 #define SUN4I_CODEC_ADC_ACTL_DDE (3)
91 #define SUN4I_CODEC_ADC_DEBUG (0x2c)
93 /* Other various ADC registers */
94 #define SUN4I_CODEC_DAC_TXCNT (0x30)
95 #define SUN4I_CODEC_ADC_RXCNT (0x34)
96 #define SUN4I_CODEC_AC_SYS_VERI (0x38)
97 #define SUN4I_CODEC_AC_MIC_PHONE_CAL (0x3c)
101 struct regmap
*regmap
;
103 struct clk
*clk_module
;
105 struct snd_dmaengine_dai_dma_data playback_dma_data
;
108 static void sun4i_codec_start_playback(struct sun4i_codec
*scodec
)
111 * FIXME: according to the BSP, we might need to drive a PA
112 * GPIO high here on some boards
116 regmap_update_bits(scodec
->regmap
, SUN4I_CODEC_DAC_FIFOC
,
117 BIT(SUN4I_CODEC_DAC_FIFOC_FIFO_FLUSH
),
118 BIT(SUN4I_CODEC_DAC_FIFOC_FIFO_FLUSH
));
121 regmap_update_bits(scodec
->regmap
, SUN4I_CODEC_DAC_FIFOC
,
122 BIT(SUN4I_CODEC_DAC_FIFOC_DAC_DRQ_EN
),
123 BIT(SUN4I_CODEC_DAC_FIFOC_DAC_DRQ_EN
));
126 static void sun4i_codec_stop_playback(struct sun4i_codec
*scodec
)
129 * FIXME: according to the BSP, we might need to drive a PA
130 * GPIO low here on some boards
133 /* Disable DAC DRQ */
134 regmap_update_bits(scodec
->regmap
, SUN4I_CODEC_DAC_FIFOC
,
135 BIT(SUN4I_CODEC_DAC_FIFOC_DAC_DRQ_EN
),
139 static int sun4i_codec_trigger(struct snd_pcm_substream
*substream
, int cmd
,
140 struct snd_soc_dai
*dai
)
142 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
143 struct sun4i_codec
*scodec
= snd_soc_card_get_drvdata(rtd
->card
);
145 if (substream
->stream
!= SNDRV_PCM_STREAM_PLAYBACK
)
149 case SNDRV_PCM_TRIGGER_START
:
150 case SNDRV_PCM_TRIGGER_RESUME
:
151 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
152 sun4i_codec_start_playback(scodec
);
155 case SNDRV_PCM_TRIGGER_STOP
:
156 case SNDRV_PCM_TRIGGER_SUSPEND
:
157 case SNDRV_PCM_TRIGGER_PAUSE_PUSH
:
158 sun4i_codec_stop_playback(scodec
);
168 static int sun4i_codec_prepare(struct snd_pcm_substream
*substream
,
169 struct snd_soc_dai
*dai
)
171 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
172 struct sun4i_codec
*scodec
= snd_soc_card_get_drvdata(rtd
->card
);
175 if (substream
->stream
!= SNDRV_PCM_STREAM_PLAYBACK
)
178 /* Flush the TX FIFO */
179 regmap_update_bits(scodec
->regmap
, SUN4I_CODEC_DAC_FIFOC
,
180 BIT(SUN4I_CODEC_DAC_FIFOC_FIFO_FLUSH
),
181 BIT(SUN4I_CODEC_DAC_FIFOC_FIFO_FLUSH
));
183 /* Set TX FIFO Empty Trigger Level */
184 regmap_update_bits(scodec
->regmap
, SUN4I_CODEC_DAC_FIFOC
,
185 0x3f << SUN4I_CODEC_DAC_FIFOC_TX_TRIG_LEVEL
,
186 0xf << SUN4I_CODEC_DAC_FIFOC_TX_TRIG_LEVEL
);
188 if (substream
->runtime
->rate
> 32000)
189 /* Use 64 bits FIR filter */
192 /* Use 32 bits FIR filter */
193 val
= BIT(SUN4I_CODEC_DAC_FIFOC_FIR_VERSION
);
195 regmap_update_bits(scodec
->regmap
, SUN4I_CODEC_DAC_FIFOC
,
196 BIT(SUN4I_CODEC_DAC_FIFOC_FIR_VERSION
),
199 /* Send zeros when we have an underrun */
200 regmap_update_bits(scodec
->regmap
, SUN4I_CODEC_DAC_FIFOC
,
201 BIT(SUN4I_CODEC_DAC_FIFOC_SEND_LASAT
),
207 static unsigned long sun4i_codec_get_mod_freq(struct snd_pcm_hw_params
*params
)
209 unsigned int rate
= params_rate(params
);
237 static int sun4i_codec_get_hw_rate(struct snd_pcm_hw_params
*params
)
239 unsigned int rate
= params_rate(params
);
279 static int sun4i_codec_hw_params(struct snd_pcm_substream
*substream
,
280 struct snd_pcm_hw_params
*params
,
281 struct snd_soc_dai
*dai
)
283 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
284 struct sun4i_codec
*scodec
= snd_soc_card_get_drvdata(rtd
->card
);
285 unsigned long clk_freq
;
289 if (substream
->stream
!= SNDRV_PCM_STREAM_PLAYBACK
)
292 clk_freq
= sun4i_codec_get_mod_freq(params
);
296 ret
= clk_set_rate(scodec
->clk_module
, clk_freq
);
300 hwrate
= sun4i_codec_get_hw_rate(params
);
304 /* Set DAC sample rate */
305 regmap_update_bits(scodec
->regmap
, SUN4I_CODEC_DAC_FIFOC
,
306 7 << SUN4I_CODEC_DAC_FIFOC_DAC_FS
,
307 hwrate
<< SUN4I_CODEC_DAC_FIFOC_DAC_FS
);
309 /* Set the number of channels we want to use */
310 if (params_channels(params
) == 1)
311 val
= BIT(SUN4I_CODEC_DAC_FIFOC_MONO_EN
);
315 regmap_update_bits(scodec
->regmap
, SUN4I_CODEC_DAC_FIFOC
,
316 BIT(SUN4I_CODEC_DAC_FIFOC_MONO_EN
),
319 /* Set the number of sample bits to either 16 or 24 bits */
320 if (hw_param_interval(params
, SNDRV_PCM_HW_PARAM_SAMPLE_BITS
)->min
== 32) {
321 regmap_update_bits(scodec
->regmap
, SUN4I_CODEC_DAC_FIFOC
,
322 BIT(SUN4I_CODEC_DAC_FIFOC_TX_SAMPLE_BITS
),
323 BIT(SUN4I_CODEC_DAC_FIFOC_TX_SAMPLE_BITS
));
325 /* Set TX FIFO mode to padding the LSBs with 0 */
326 regmap_update_bits(scodec
->regmap
, SUN4I_CODEC_DAC_FIFOC
,
327 BIT(SUN4I_CODEC_DAC_FIFOC_TX_FIFO_MODE
),
330 scodec
->playback_dma_data
.addr_width
= DMA_SLAVE_BUSWIDTH_4_BYTES
;
332 regmap_update_bits(scodec
->regmap
, SUN4I_CODEC_DAC_FIFOC
,
333 BIT(SUN4I_CODEC_DAC_FIFOC_TX_SAMPLE_BITS
),
336 /* Set TX FIFO mode to repeat the MSB */
337 regmap_update_bits(scodec
->regmap
, SUN4I_CODEC_DAC_FIFOC
,
338 BIT(SUN4I_CODEC_DAC_FIFOC_TX_FIFO_MODE
),
339 BIT(SUN4I_CODEC_DAC_FIFOC_TX_FIFO_MODE
));
341 scodec
->playback_dma_data
.addr_width
= DMA_SLAVE_BUSWIDTH_2_BYTES
;
347 static int sun4i_codec_startup(struct snd_pcm_substream
*substream
,
348 struct snd_soc_dai
*dai
)
350 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
351 struct sun4i_codec
*scodec
= snd_soc_card_get_drvdata(rtd
->card
);
354 * Stop issuing DRQ when we have room for less than 16 samples
357 regmap_update_bits(scodec
->regmap
, SUN4I_CODEC_DAC_FIFOC
,
358 3 << SUN4I_CODEC_DAC_FIFOC_DRQ_CLR_CNT
,
359 3 << SUN4I_CODEC_DAC_FIFOC_DRQ_CLR_CNT
);
361 return clk_prepare_enable(scodec
->clk_module
);
364 static void sun4i_codec_shutdown(struct snd_pcm_substream
*substream
,
365 struct snd_soc_dai
*dai
)
367 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
368 struct sun4i_codec
*scodec
= snd_soc_card_get_drvdata(rtd
->card
);
370 clk_disable_unprepare(scodec
->clk_module
);
373 static const struct snd_soc_dai_ops sun4i_codec_dai_ops
= {
374 .startup
= sun4i_codec_startup
,
375 .shutdown
= sun4i_codec_shutdown
,
376 .trigger
= sun4i_codec_trigger
,
377 .hw_params
= sun4i_codec_hw_params
,
378 .prepare
= sun4i_codec_prepare
,
381 static struct snd_soc_dai_driver sun4i_codec_dai
= {
383 .ops
= &sun4i_codec_dai_ops
,
385 .stream_name
= "Codec Playback",
390 .rates
= SNDRV_PCM_RATE_8000_48000
|
391 SNDRV_PCM_RATE_96000
|
392 SNDRV_PCM_RATE_192000
,
393 .formats
= SNDRV_PCM_FMTBIT_S16_LE
|
394 SNDRV_PCM_FMTBIT_S32_LE
,
400 static const struct snd_kcontrol_new sun4i_codec_pa_mute
=
401 SOC_DAPM_SINGLE("Switch", SUN4I_CODEC_DAC_ACTL
,
402 SUN4I_CODEC_DAC_ACTL_PA_MUTE
, 1, 0);
404 static DECLARE_TLV_DB_SCALE(sun4i_codec_pa_volume_scale
, -6300, 100, 1);
406 static const struct snd_kcontrol_new sun4i_codec_widgets
[] = {
407 SOC_SINGLE_TLV("PA Volume", SUN4I_CODEC_DAC_ACTL
,
408 SUN4I_CODEC_DAC_ACTL_PA_VOL
, 0x3F, 0,
409 sun4i_codec_pa_volume_scale
),
412 static const struct snd_kcontrol_new sun4i_codec_left_mixer_controls
[] = {
413 SOC_DAPM_SINGLE("Left DAC Playback Switch", SUN4I_CODEC_DAC_ACTL
,
414 SUN4I_CODEC_DAC_ACTL_LDACLMIXS
, 1, 0),
417 static const struct snd_kcontrol_new sun4i_codec_right_mixer_controls
[] = {
418 SOC_DAPM_SINGLE("Right DAC Playback Switch", SUN4I_CODEC_DAC_ACTL
,
419 SUN4I_CODEC_DAC_ACTL_RDACRMIXS
, 1, 0),
420 SOC_DAPM_SINGLE("Left DAC Playback Switch", SUN4I_CODEC_DAC_ACTL
,
421 SUN4I_CODEC_DAC_ACTL_LDACRMIXS
, 1, 0),
424 static const struct snd_kcontrol_new sun4i_codec_pa_mixer_controls
[] = {
425 SOC_DAPM_SINGLE("DAC Playback Switch", SUN4I_CODEC_DAC_ACTL
,
426 SUN4I_CODEC_DAC_ACTL_DACPAS
, 1, 0),
427 SOC_DAPM_SINGLE("Mixer Playback Switch", SUN4I_CODEC_DAC_ACTL
,
428 SUN4I_CODEC_DAC_ACTL_MIXPAS
, 1, 0),
431 static const struct snd_soc_dapm_widget sun4i_codec_dapm_widgets
[] = {
432 /* Digital parts of the DACs */
433 SND_SOC_DAPM_SUPPLY("DAC", SUN4I_CODEC_DAC_DPC
,
434 SUN4I_CODEC_DAC_DPC_EN_DA
, 0,
437 /* Analog parts of the DACs */
438 SND_SOC_DAPM_DAC("Left DAC", "Codec Playback", SUN4I_CODEC_DAC_ACTL
,
439 SUN4I_CODEC_DAC_ACTL_DACAENL
, 0),
440 SND_SOC_DAPM_DAC("Right DAC", "Codec Playback", SUN4I_CODEC_DAC_ACTL
,
441 SUN4I_CODEC_DAC_ACTL_DACAENR
, 0),
444 SND_SOC_DAPM_MIXER("Left Mixer", SND_SOC_NOPM
, 0, 0,
445 sun4i_codec_left_mixer_controls
,
446 ARRAY_SIZE(sun4i_codec_left_mixer_controls
)),
447 SND_SOC_DAPM_MIXER("Right Mixer", SND_SOC_NOPM
, 0, 0,
448 sun4i_codec_right_mixer_controls
,
449 ARRAY_SIZE(sun4i_codec_right_mixer_controls
)),
451 /* Global Mixer Enable */
452 SND_SOC_DAPM_SUPPLY("Mixer Enable", SUN4I_CODEC_DAC_ACTL
,
453 SUN4I_CODEC_DAC_ACTL_MIXEN
, 0, NULL
, 0),
456 SND_SOC_DAPM_MIXER("Pre-Amplifier", SUN4I_CODEC_ADC_ACTL
,
457 SUN4I_CODEC_ADC_ACTL_PA_EN
, 0,
458 sun4i_codec_pa_mixer_controls
,
459 ARRAY_SIZE(sun4i_codec_pa_mixer_controls
)),
460 SND_SOC_DAPM_SWITCH("Pre-Amplifier Mute", SND_SOC_NOPM
, 0, 0,
461 &sun4i_codec_pa_mute
),
463 SND_SOC_DAPM_OUTPUT("HP Right"),
464 SND_SOC_DAPM_OUTPUT("HP Left"),
467 static const struct snd_soc_dapm_route sun4i_codec_dapm_routes
[] = {
468 /* Left DAC Routes */
469 { "Left DAC", NULL
, "DAC" },
471 /* Right DAC Routes */
472 { "Right DAC", NULL
, "DAC" },
474 /* Right Mixer Routes */
475 { "Right Mixer", NULL
, "Mixer Enable" },
476 { "Right Mixer", "Left DAC Playback Switch", "Left DAC" },
477 { "Right Mixer", "Right DAC Playback Switch", "Right DAC" },
479 /* Left Mixer Routes */
480 { "Left Mixer", NULL
, "Mixer Enable" },
481 { "Left Mixer", "Left DAC Playback Switch", "Left DAC" },
483 /* Pre-Amplifier Mixer Routes */
484 { "Pre-Amplifier", "Mixer Playback Switch", "Left Mixer" },
485 { "Pre-Amplifier", "Mixer Playback Switch", "Right Mixer" },
486 { "Pre-Amplifier", "DAC Playback Switch", "Left DAC" },
487 { "Pre-Amplifier", "DAC Playback Switch", "Right DAC" },
490 { "Pre-Amplifier Mute", "Switch", "Pre-Amplifier" },
491 { "HP Right", NULL
, "Pre-Amplifier Mute" },
492 { "HP Left", NULL
, "Pre-Amplifier Mute" },
495 static struct snd_soc_codec_driver sun4i_codec_codec
= {
496 .controls
= sun4i_codec_widgets
,
497 .num_controls
= ARRAY_SIZE(sun4i_codec_widgets
),
498 .dapm_widgets
= sun4i_codec_dapm_widgets
,
499 .num_dapm_widgets
= ARRAY_SIZE(sun4i_codec_dapm_widgets
),
500 .dapm_routes
= sun4i_codec_dapm_routes
,
501 .num_dapm_routes
= ARRAY_SIZE(sun4i_codec_dapm_routes
),
504 static const struct snd_soc_component_driver sun4i_codec_component
= {
505 .name
= "sun4i-codec",
508 #define SUN4I_CODEC_RATES SNDRV_PCM_RATE_8000_192000
509 #define SUN4I_CODEC_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | \
510 SNDRV_PCM_FMTBIT_S32_LE)
512 static int sun4i_codec_dai_probe(struct snd_soc_dai
*dai
)
514 struct snd_soc_card
*card
= snd_soc_dai_get_drvdata(dai
);
515 struct sun4i_codec
*scodec
= snd_soc_card_get_drvdata(card
);
517 snd_soc_dai_init_dma_data(dai
, &scodec
->playback_dma_data
,
523 static struct snd_soc_dai_driver dummy_cpu_dai
= {
524 .name
= "sun4i-codec-cpu-dai",
525 .probe
= sun4i_codec_dai_probe
,
527 .stream_name
= "Playback",
530 .rates
= SUN4I_CODEC_RATES
,
531 .formats
= SUN4I_CODEC_FORMATS
,
536 static const struct regmap_config sun4i_codec_regmap_config
= {
540 .max_register
= SUN4I_CODEC_AC_MIC_PHONE_CAL
,
543 static const struct of_device_id sun4i_codec_of_match
[] = {
544 { .compatible
= "allwinner,sun4i-a10-codec" },
545 { .compatible
= "allwinner,sun7i-a20-codec" },
548 MODULE_DEVICE_TABLE(of
, sun4i_codec_of_match
);
550 static struct snd_soc_dai_link
*sun4i_codec_create_link(struct device
*dev
,
553 struct snd_soc_dai_link
*link
= devm_kzalloc(dev
, sizeof(*link
),
559 link
->stream_name
= "CDC PCM";
560 link
->codec_dai_name
= "Codec";
561 link
->cpu_dai_name
= dev_name(dev
);
562 link
->codec_name
= dev_name(dev
);
563 link
->platform_name
= dev_name(dev
);
564 link
->dai_fmt
= SND_SOC_DAIFMT_I2S
;
571 static struct snd_soc_card
*sun4i_codec_create_card(struct device
*dev
)
573 struct snd_soc_card
*card
;
575 card
= devm_kzalloc(dev
, sizeof(*card
), GFP_KERNEL
);
579 card
->dai_link
= sun4i_codec_create_link(dev
, &card
->num_links
);
584 card
->name
= "sun4i-codec";
589 static int sun4i_codec_probe(struct platform_device
*pdev
)
591 struct snd_soc_card
*card
;
592 struct sun4i_codec
*scodec
;
593 struct resource
*res
;
597 scodec
= devm_kzalloc(&pdev
->dev
, sizeof(*scodec
), GFP_KERNEL
);
601 scodec
->dev
= &pdev
->dev
;
603 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
604 base
= devm_ioremap_resource(&pdev
->dev
, res
);
606 dev_err(&pdev
->dev
, "Failed to map the registers\n");
607 return PTR_ERR(base
);
610 scodec
->regmap
= devm_regmap_init_mmio(&pdev
->dev
, base
,
611 &sun4i_codec_regmap_config
);
612 if (IS_ERR(scodec
->regmap
)) {
613 dev_err(&pdev
->dev
, "Failed to create our regmap\n");
614 return PTR_ERR(scodec
->regmap
);
617 /* Get the clocks from the DT */
618 scodec
->clk_apb
= devm_clk_get(&pdev
->dev
, "apb");
619 if (IS_ERR(scodec
->clk_apb
)) {
620 dev_err(&pdev
->dev
, "Failed to get the APB clock\n");
621 return PTR_ERR(scodec
->clk_apb
);
624 scodec
->clk_module
= devm_clk_get(&pdev
->dev
, "codec");
625 if (IS_ERR(scodec
->clk_module
)) {
626 dev_err(&pdev
->dev
, "Failed to get the module clock\n");
627 return PTR_ERR(scodec
->clk_module
);
630 /* Enable the bus clock */
631 if (clk_prepare_enable(scodec
->clk_apb
)) {
632 dev_err(&pdev
->dev
, "Failed to enable the APB clock\n");
636 /* DMA configuration for TX FIFO */
637 scodec
->playback_dma_data
.addr
= res
->start
+ SUN4I_CODEC_DAC_TXDATA
;
638 scodec
->playback_dma_data
.maxburst
= 4;
639 scodec
->playback_dma_data
.addr_width
= DMA_SLAVE_BUSWIDTH_2_BYTES
;
641 ret
= snd_soc_register_codec(&pdev
->dev
, &sun4i_codec_codec
,
642 &sun4i_codec_dai
, 1);
644 dev_err(&pdev
->dev
, "Failed to register our codec\n");
645 goto err_clk_disable
;
648 ret
= devm_snd_soc_register_component(&pdev
->dev
,
649 &sun4i_codec_component
,
652 dev_err(&pdev
->dev
, "Failed to register our DAI\n");
653 goto err_unregister_codec
;
656 ret
= devm_snd_dmaengine_pcm_register(&pdev
->dev
, NULL
, 0);
658 dev_err(&pdev
->dev
, "Failed to register against DMAEngine\n");
659 goto err_unregister_codec
;
662 card
= sun4i_codec_create_card(&pdev
->dev
);
664 dev_err(&pdev
->dev
, "Failed to create our card\n");
665 goto err_unregister_codec
;
668 platform_set_drvdata(pdev
, card
);
669 snd_soc_card_set_drvdata(card
, scodec
);
671 ret
= snd_soc_register_card(card
);
673 dev_err(&pdev
->dev
, "Failed to register our card\n");
674 goto err_unregister_codec
;
679 err_unregister_codec
:
680 snd_soc_unregister_codec(&pdev
->dev
);
682 clk_disable_unprepare(scodec
->clk_apb
);
686 static int sun4i_codec_remove(struct platform_device
*pdev
)
688 struct snd_soc_card
*card
= platform_get_drvdata(pdev
);
689 struct sun4i_codec
*scodec
= snd_soc_card_get_drvdata(card
);
691 snd_soc_unregister_card(card
);
692 snd_soc_unregister_codec(&pdev
->dev
);
693 clk_disable_unprepare(scodec
->clk_apb
);
698 static struct platform_driver sun4i_codec_driver
= {
700 .name
= "sun4i-codec",
701 .of_match_table
= sun4i_codec_of_match
,
703 .probe
= sun4i_codec_probe
,
704 .remove
= sun4i_codec_remove
,
706 module_platform_driver(sun4i_codec_driver
);
708 MODULE_DESCRIPTION("Allwinner A10 codec driver");
709 MODULE_AUTHOR("Emilio López <emilio@elopez.com.ar>");
710 MODULE_AUTHOR("Jon Smirl <jonsmirl@gmail.com>");
711 MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
712 MODULE_LICENSE("GPL");