1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * ALSA SoC SPDIF Audio Layer
5 * Copyright 2015 Andrea Venturi <be17068@iperbole.bo.it>
6 * Copyright 2015 Marcus Cooper <codekipper@gmail.com>
8 * Based on the Allwinner SDK driver, released under the GPL.
11 #include <linux/clk.h>
12 #include <linux/delay.h>
13 #include <linux/device.h>
14 #include <linux/kernel.h>
15 #include <linux/init.h>
16 #include <linux/regmap.h>
18 #include <linux/ioport.h>
19 #include <linux/module.h>
20 #include <linux/platform_device.h>
21 #include <linux/pm_runtime.h>
22 #include <linux/reset.h>
23 #include <linux/spinlock.h>
24 #include <sound/asoundef.h>
25 #include <sound/dmaengine_pcm.h>
26 #include <sound/pcm_params.h>
27 #include <sound/soc.h>
29 #define SUN4I_SPDIF_CTL (0x00)
30 #define SUN4I_SPDIF_CTL_MCLKDIV(v) ((v) << 4) /* v even */
31 #define SUN4I_SPDIF_CTL_MCLKOUTEN BIT(2)
32 #define SUN4I_SPDIF_CTL_GEN BIT(1)
33 #define SUN4I_SPDIF_CTL_RESET BIT(0)
35 #define SUN4I_SPDIF_TXCFG (0x04)
36 #define SUN4I_SPDIF_TXCFG_SINGLEMOD BIT(31)
37 #define SUN4I_SPDIF_TXCFG_ASS BIT(17)
38 #define SUN4I_SPDIF_TXCFG_NONAUDIO BIT(16)
39 #define SUN4I_SPDIF_TXCFG_TXRATIO(v) ((v) << 4)
40 #define SUN4I_SPDIF_TXCFG_TXRATIO_MASK GENMASK(8, 4)
41 #define SUN4I_SPDIF_TXCFG_FMTRVD GENMASK(3, 2)
42 #define SUN4I_SPDIF_TXCFG_FMT16BIT (0 << 2)
43 #define SUN4I_SPDIF_TXCFG_FMT20BIT (1 << 2)
44 #define SUN4I_SPDIF_TXCFG_FMT24BIT (2 << 2)
45 #define SUN4I_SPDIF_TXCFG_CHSTMODE BIT(1)
46 #define SUN4I_SPDIF_TXCFG_TXEN BIT(0)
48 #define SUN4I_SPDIF_RXCFG (0x08)
49 #define SUN4I_SPDIF_RXCFG_LOCKFLAG BIT(4)
50 #define SUN4I_SPDIF_RXCFG_CHSTSRC BIT(3)
51 #define SUN4I_SPDIF_RXCFG_CHSTCP BIT(1)
52 #define SUN4I_SPDIF_RXCFG_RXEN BIT(0)
54 #define SUN4I_SPDIF_TXFIFO (0x0C)
56 #define SUN4I_SPDIF_RXFIFO (0x10)
58 #define SUN4I_SPDIF_FCTL (0x14)
59 #define SUN4I_SPDIF_FCTL_FIFOSRC BIT(31)
60 #define SUN4I_SPDIF_FCTL_FTX BIT(17)
61 #define SUN4I_SPDIF_FCTL_FRX BIT(16)
62 #define SUN4I_SPDIF_FCTL_TXTL(v) ((v) << 8)
63 #define SUN4I_SPDIF_FCTL_TXTL_MASK GENMASK(12, 8)
64 #define SUN4I_SPDIF_FCTL_RXTL(v) ((v) << 3)
65 #define SUN4I_SPDIF_FCTL_RXTL_MASK GENMASK(7, 3)
66 #define SUN4I_SPDIF_FCTL_TXIM BIT(2)
67 #define SUN4I_SPDIF_FCTL_RXOM(v) ((v) << 0)
68 #define SUN4I_SPDIF_FCTL_RXOM_MASK GENMASK(1, 0)
70 #define SUN50I_H6_SPDIF_FCTL (0x14)
71 #define SUN50I_H6_SPDIF_FCTL_HUB_EN BIT(31)
72 #define SUN50I_H6_SPDIF_FCTL_FTX BIT(30)
73 #define SUN50I_H6_SPDIF_FCTL_FRX BIT(29)
74 #define SUN50I_H6_SPDIF_FCTL_TXTL(v) ((v) << 12)
75 #define SUN50I_H6_SPDIF_FCTL_TXTL_MASK GENMASK(19, 12)
76 #define SUN50I_H6_SPDIF_FCTL_RXTL(v) ((v) << 4)
77 #define SUN50I_H6_SPDIF_FCTL_RXTL_MASK GENMASK(10, 4)
78 #define SUN50I_H6_SPDIF_FCTL_TXIM BIT(2)
79 #define SUN50I_H6_SPDIF_FCTL_RXOM(v) ((v) << 0)
80 #define SUN50I_H6_SPDIF_FCTL_RXOM_MASK GENMASK(1, 0)
82 #define SUN4I_SPDIF_FSTA (0x18)
83 #define SUN4I_SPDIF_FSTA_TXE BIT(14)
84 #define SUN4I_SPDIF_FSTA_TXECNTSHT (8)
85 #define SUN4I_SPDIF_FSTA_RXA BIT(6)
86 #define SUN4I_SPDIF_FSTA_RXACNTSHT (0)
88 #define SUN4I_SPDIF_INT (0x1C)
89 #define SUN4I_SPDIF_INT_RXLOCKEN BIT(18)
90 #define SUN4I_SPDIF_INT_RXUNLOCKEN BIT(17)
91 #define SUN4I_SPDIF_INT_RXPARERREN BIT(16)
92 #define SUN4I_SPDIF_INT_TXDRQEN BIT(7)
93 #define SUN4I_SPDIF_INT_TXUIEN BIT(6)
94 #define SUN4I_SPDIF_INT_TXOIEN BIT(5)
95 #define SUN4I_SPDIF_INT_TXEIEN BIT(4)
96 #define SUN4I_SPDIF_INT_RXDRQEN BIT(2)
97 #define SUN4I_SPDIF_INT_RXOIEN BIT(1)
98 #define SUN4I_SPDIF_INT_RXAIEN BIT(0)
100 #define SUN4I_SPDIF_ISTA (0x20)
101 #define SUN4I_SPDIF_ISTA_RXLOCKSTA BIT(18)
102 #define SUN4I_SPDIF_ISTA_RXUNLOCKSTA BIT(17)
103 #define SUN4I_SPDIF_ISTA_RXPARERRSTA BIT(16)
104 #define SUN4I_SPDIF_ISTA_TXUSTA BIT(6)
105 #define SUN4I_SPDIF_ISTA_TXOSTA BIT(5)
106 #define SUN4I_SPDIF_ISTA_TXESTA BIT(4)
107 #define SUN4I_SPDIF_ISTA_RXOSTA BIT(1)
108 #define SUN4I_SPDIF_ISTA_RXASTA BIT(0)
110 #define SUN8I_SPDIF_TXFIFO (0x20)
112 #define SUN4I_SPDIF_TXCNT (0x24)
114 #define SUN4I_SPDIF_RXCNT (0x28)
116 #define SUN4I_SPDIF_TXCHSTA0 (0x2C)
117 #define SUN4I_SPDIF_TXCHSTA0_CLK(v) ((v) << 28)
118 #define SUN4I_SPDIF_TXCHSTA0_SAMFREQ(v) ((v) << 24)
119 #define SUN4I_SPDIF_TXCHSTA0_SAMFREQ_MASK GENMASK(27, 24)
120 #define SUN4I_SPDIF_TXCHSTA0_CHNUM(v) ((v) << 20)
121 #define SUN4I_SPDIF_TXCHSTA0_CHNUM_MASK GENMASK(23, 20)
122 #define SUN4I_SPDIF_TXCHSTA0_SRCNUM(v) ((v) << 16)
123 #define SUN4I_SPDIF_TXCHSTA0_CATACOD(v) ((v) << 8)
124 #define SUN4I_SPDIF_TXCHSTA0_MODE(v) ((v) << 6)
125 #define SUN4I_SPDIF_TXCHSTA0_EMPHASIS(v) ((v) << 3)
126 #define SUN4I_SPDIF_TXCHSTA0_CP BIT(2)
127 #define SUN4I_SPDIF_TXCHSTA0_AUDIO BIT(1)
128 #define SUN4I_SPDIF_TXCHSTA0_PRO BIT(0)
130 #define SUN4I_SPDIF_TXCHSTA1 (0x30)
131 #define SUN4I_SPDIF_TXCHSTA1_CGMSA(v) ((v) << 8)
132 #define SUN4I_SPDIF_TXCHSTA1_ORISAMFREQ(v) ((v) << 4)
133 #define SUN4I_SPDIF_TXCHSTA1_ORISAMFREQ_MASK GENMASK(7, 4)
134 #define SUN4I_SPDIF_TXCHSTA1_SAMWORDLEN(v) ((v) << 1)
135 #define SUN4I_SPDIF_TXCHSTA1_MAXWORDLEN BIT(0)
137 #define SUN4I_SPDIF_RXCHSTA0 (0x34)
138 #define SUN4I_SPDIF_RXCHSTA0_CLK(v) ((v) << 28)
139 #define SUN4I_SPDIF_RXCHSTA0_SAMFREQ(v) ((v) << 24)
140 #define SUN4I_SPDIF_RXCHSTA0_CHNUM(v) ((v) << 20)
141 #define SUN4I_SPDIF_RXCHSTA0_SRCNUM(v) ((v) << 16)
142 #define SUN4I_SPDIF_RXCHSTA0_CATACOD(v) ((v) << 8)
143 #define SUN4I_SPDIF_RXCHSTA0_MODE(v) ((v) << 6)
144 #define SUN4I_SPDIF_RXCHSTA0_EMPHASIS(v) ((v) << 3)
145 #define SUN4I_SPDIF_RXCHSTA0_CP BIT(2)
146 #define SUN4I_SPDIF_RXCHSTA0_AUDIO BIT(1)
147 #define SUN4I_SPDIF_RXCHSTA0_PRO BIT(0)
149 #define SUN4I_SPDIF_RXCHSTA1 (0x38)
150 #define SUN4I_SPDIF_RXCHSTA1_CGMSA(v) ((v) << 8)
151 #define SUN4I_SPDIF_RXCHSTA1_ORISAMFREQ(v) ((v) << 4)
152 #define SUN4I_SPDIF_RXCHSTA1_SAMWORDLEN(v) ((v) << 1)
153 #define SUN4I_SPDIF_RXCHSTA1_MAXWORDLEN BIT(0)
155 /* Defines for Sampling Frequency */
156 #define SUN4I_SPDIF_SAMFREQ_44_1KHZ 0x0
157 #define SUN4I_SPDIF_SAMFREQ_NOT_INDICATED 0x1
158 #define SUN4I_SPDIF_SAMFREQ_48KHZ 0x2
159 #define SUN4I_SPDIF_SAMFREQ_32KHZ 0x3
160 #define SUN4I_SPDIF_SAMFREQ_22_05KHZ 0x4
161 #define SUN4I_SPDIF_SAMFREQ_24KHZ 0x6
162 #define SUN4I_SPDIF_SAMFREQ_88_2KHZ 0x8
163 #define SUN4I_SPDIF_SAMFREQ_76_8KHZ 0x9
164 #define SUN4I_SPDIF_SAMFREQ_96KHZ 0xa
165 #define SUN4I_SPDIF_SAMFREQ_176_4KHZ 0xc
166 #define SUN4I_SPDIF_SAMFREQ_192KHZ 0xe
169 * struct sun4i_spdif_quirks - Differences between SoC variants.
171 * @reg_dac_txdata: TX FIFO offset for DMA config.
172 * @has_reset: SoC needs reset deasserted.
173 * @val_fctl_ftx: TX FIFO flush bitmask.
175 struct sun4i_spdif_quirks
{
176 unsigned int reg_dac_txdata
;
178 unsigned int val_fctl_ftx
;
181 struct sun4i_spdif_dev
{
182 struct platform_device
*pdev
;
183 struct clk
*spdif_clk
;
185 struct reset_control
*rst
;
186 struct snd_soc_dai_driver cpu_dai_drv
;
187 struct regmap
*regmap
;
188 struct snd_dmaengine_dai_dma_data dma_params_tx
;
189 const struct sun4i_spdif_quirks
*quirks
;
193 static void sun4i_spdif_configure(struct sun4i_spdif_dev
*host
)
195 const struct sun4i_spdif_quirks
*quirks
= host
->quirks
;
197 /* soft reset SPDIF */
198 regmap_write(host
->regmap
, SUN4I_SPDIF_CTL
, SUN4I_SPDIF_CTL_RESET
);
201 regmap_update_bits(host
->regmap
, SUN4I_SPDIF_FCTL
,
202 quirks
->val_fctl_ftx
, quirks
->val_fctl_ftx
);
204 /* clear TX counter */
205 regmap_write(host
->regmap
, SUN4I_SPDIF_TXCNT
, 0);
208 static void sun4i_snd_txctrl_on(struct snd_pcm_substream
*substream
,
209 struct sun4i_spdif_dev
*host
)
211 if (substream
->runtime
->channels
== 1)
212 regmap_update_bits(host
->regmap
, SUN4I_SPDIF_TXCFG
,
213 SUN4I_SPDIF_TXCFG_SINGLEMOD
,
214 SUN4I_SPDIF_TXCFG_SINGLEMOD
);
216 /* SPDIF TX ENABLE */
217 regmap_update_bits(host
->regmap
, SUN4I_SPDIF_TXCFG
,
218 SUN4I_SPDIF_TXCFG_TXEN
, SUN4I_SPDIF_TXCFG_TXEN
);
221 regmap_update_bits(host
->regmap
, SUN4I_SPDIF_INT
,
222 SUN4I_SPDIF_INT_TXDRQEN
, SUN4I_SPDIF_INT_TXDRQEN
);
225 regmap_update_bits(host
->regmap
, SUN4I_SPDIF_CTL
,
226 SUN4I_SPDIF_CTL_GEN
, SUN4I_SPDIF_CTL_GEN
);
229 static void sun4i_snd_txctrl_off(struct snd_pcm_substream
*substream
,
230 struct sun4i_spdif_dev
*host
)
232 /* SPDIF TX DISABLE */
233 regmap_update_bits(host
->regmap
, SUN4I_SPDIF_TXCFG
,
234 SUN4I_SPDIF_TXCFG_TXEN
, 0);
237 regmap_update_bits(host
->regmap
, SUN4I_SPDIF_INT
,
238 SUN4I_SPDIF_INT_TXDRQEN
, 0);
241 regmap_update_bits(host
->regmap
, SUN4I_SPDIF_CTL
,
242 SUN4I_SPDIF_CTL_GEN
, 0);
245 static int sun4i_spdif_startup(struct snd_pcm_substream
*substream
,
246 struct snd_soc_dai
*cpu_dai
)
248 struct snd_soc_pcm_runtime
*rtd
= snd_soc_substream_to_rtd(substream
);
249 struct sun4i_spdif_dev
*host
= snd_soc_dai_get_drvdata(snd_soc_rtd_to_cpu(rtd
, 0));
251 if (substream
->stream
!= SNDRV_PCM_STREAM_PLAYBACK
)
254 sun4i_spdif_configure(host
);
259 static int sun4i_spdif_hw_params(struct snd_pcm_substream
*substream
,
260 struct snd_pcm_hw_params
*params
,
261 struct snd_soc_dai
*cpu_dai
)
265 unsigned long rate
= params_rate(params
);
267 unsigned int mclk
= 0;
269 struct sun4i_spdif_dev
*host
= snd_soc_dai_get_drvdata(cpu_dai
);
270 struct platform_device
*pdev
= host
->pdev
;
272 /* Add the PCM and raw data select interface */
273 switch (params_channels(params
)) {
274 case 1: /* PCM mode */
278 case 4: /* raw data mode */
279 fmt
= SUN4I_SPDIF_TXCFG_NONAUDIO
;
285 switch (params_format(params
)) {
286 case SNDRV_PCM_FORMAT_S16_LE
:
287 fmt
|= SUN4I_SPDIF_TXCFG_FMT16BIT
;
289 case SNDRV_PCM_FORMAT_S20_3LE
:
290 fmt
|= SUN4I_SPDIF_TXCFG_FMT20BIT
;
292 case SNDRV_PCM_FORMAT_S24_LE
:
293 fmt
|= SUN4I_SPDIF_TXCFG_FMT24BIT
;
317 ret
= clk_set_rate(host
->spdif_clk
, mclk
);
320 "Setting SPDIF clock rate for %d Hz failed!\n", mclk
);
324 regmap_update_bits(host
->regmap
, SUN4I_SPDIF_FCTL
,
325 SUN4I_SPDIF_FCTL_TXIM
, SUN4I_SPDIF_FCTL_TXIM
);
352 reg_val
|= SUN4I_SPDIF_TXCFG_ASS
;
353 reg_val
|= fmt
; /* set non audio and bit depth */
354 reg_val
|= SUN4I_SPDIF_TXCFG_CHSTMODE
;
355 reg_val
|= SUN4I_SPDIF_TXCFG_TXRATIO(mclk_div
- 1);
356 regmap_write(host
->regmap
, SUN4I_SPDIF_TXCFG
, reg_val
);
361 static int sun4i_spdif_trigger(struct snd_pcm_substream
*substream
, int cmd
,
362 struct snd_soc_dai
*dai
)
365 struct sun4i_spdif_dev
*host
= snd_soc_dai_get_drvdata(dai
);
367 if (substream
->stream
!= SNDRV_PCM_STREAM_PLAYBACK
)
371 case SNDRV_PCM_TRIGGER_START
:
372 case SNDRV_PCM_TRIGGER_RESUME
:
373 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
374 sun4i_snd_txctrl_on(substream
, host
);
377 case SNDRV_PCM_TRIGGER_STOP
:
378 case SNDRV_PCM_TRIGGER_SUSPEND
:
379 case SNDRV_PCM_TRIGGER_PAUSE_PUSH
:
380 sun4i_snd_txctrl_off(substream
, host
);
390 static int sun4i_spdif_info(struct snd_kcontrol
*kcontrol
,
391 struct snd_ctl_elem_info
*uinfo
)
393 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_IEC958
;
399 static int sun4i_spdif_get_status_mask(struct snd_kcontrol
*kcontrol
,
400 struct snd_ctl_elem_value
*ucontrol
)
402 u8
*status
= ucontrol
->value
.iec958
.status
;
414 static int sun4i_spdif_get_status(struct snd_kcontrol
*kcontrol
,
415 struct snd_ctl_elem_value
*ucontrol
)
417 struct snd_soc_dai
*cpu_dai
= snd_kcontrol_chip(kcontrol
);
418 struct sun4i_spdif_dev
*host
= snd_soc_dai_get_drvdata(cpu_dai
);
419 u8
*status
= ucontrol
->value
.iec958
.status
;
423 spin_lock_irqsave(&host
->lock
, flags
);
425 regmap_read(host
->regmap
, SUN4I_SPDIF_TXCHSTA0
, ®
);
427 status
[0] = reg
& 0xff;
428 status
[1] = (reg
>> 8) & 0xff;
429 status
[2] = (reg
>> 16) & 0xff;
430 status
[3] = (reg
>> 24) & 0xff;
432 regmap_read(host
->regmap
, SUN4I_SPDIF_TXCHSTA1
, ®
);
434 status
[4] = reg
& 0xff;
435 status
[5] = (reg
>> 8) & 0x3;
437 spin_unlock_irqrestore(&host
->lock
, flags
);
442 static int sun4i_spdif_set_status(struct snd_kcontrol
*kcontrol
,
443 struct snd_ctl_elem_value
*ucontrol
)
445 struct snd_soc_dai
*cpu_dai
= snd_kcontrol_chip(kcontrol
);
446 struct sun4i_spdif_dev
*host
= snd_soc_dai_get_drvdata(cpu_dai
);
447 u8
*status
= ucontrol
->value
.iec958
.status
;
452 spin_lock_irqsave(&host
->lock
, flags
);
454 reg
= (u32
)status
[3] << 24;
455 reg
|= (u32
)status
[2] << 16;
456 reg
|= (u32
)status
[1] << 8;
457 reg
|= (u32
)status
[0];
459 regmap_update_bits_check(host
->regmap
, SUN4I_SPDIF_TXCHSTA0
,
460 GENMASK(31,0), reg
, &chg0
);
462 reg
= (u32
)status
[5] << 8;
463 reg
|= (u32
)status
[4];
465 regmap_update_bits_check(host
->regmap
, SUN4I_SPDIF_TXCHSTA1
,
466 GENMASK(9,0), reg
, &chg1
);
468 reg
= SUN4I_SPDIF_TXCFG_CHSTMODE
;
469 if (status
[0] & IEC958_AES0_NONAUDIO
)
470 reg
|= SUN4I_SPDIF_TXCFG_NONAUDIO
;
472 regmap_update_bits(host
->regmap
, SUN4I_SPDIF_TXCFG
,
473 SUN4I_SPDIF_TXCFG_CHSTMODE
|
474 SUN4I_SPDIF_TXCFG_NONAUDIO
, reg
);
476 spin_unlock_irqrestore(&host
->lock
, flags
);
481 static struct snd_kcontrol_new sun4i_spdif_controls
[] = {
483 .access
= SNDRV_CTL_ELEM_ACCESS_READ
,
484 .iface
= SNDRV_CTL_ELEM_IFACE_PCM
,
485 .name
= SNDRV_CTL_NAME_IEC958("", PLAYBACK
, MASK
),
486 .info
= sun4i_spdif_info
,
487 .get
= sun4i_spdif_get_status_mask
490 .iface
= SNDRV_CTL_ELEM_IFACE_PCM
,
491 .name
= SNDRV_CTL_NAME_IEC958("", PLAYBACK
, DEFAULT
),
492 .info
= sun4i_spdif_info
,
493 .get
= sun4i_spdif_get_status
,
494 .put
= sun4i_spdif_set_status
498 static int sun4i_spdif_soc_dai_probe(struct snd_soc_dai
*dai
)
500 struct sun4i_spdif_dev
*host
= snd_soc_dai_get_drvdata(dai
);
502 snd_soc_dai_init_dma_data(dai
, &host
->dma_params_tx
, NULL
);
503 snd_soc_add_dai_controls(dai
, sun4i_spdif_controls
,
504 ARRAY_SIZE(sun4i_spdif_controls
));
509 static const struct snd_soc_dai_ops sun4i_spdif_dai_ops
= {
510 .probe
= sun4i_spdif_soc_dai_probe
,
511 .startup
= sun4i_spdif_startup
,
512 .trigger
= sun4i_spdif_trigger
,
513 .hw_params
= sun4i_spdif_hw_params
,
516 static const struct regmap_config sun4i_spdif_regmap_config
= {
520 .max_register
= SUN4I_SPDIF_RXCHSTA1
,
523 #define SUN4I_RATES SNDRV_PCM_RATE_8000_192000
525 #define SUN4I_FORMATS (SNDRV_PCM_FORMAT_S16_LE | \
526 SNDRV_PCM_FORMAT_S20_3LE | \
527 SNDRV_PCM_FORMAT_S24_LE)
529 static struct snd_soc_dai_driver sun4i_spdif_dai
= {
533 .rates
= SUN4I_RATES
,
534 .formats
= SUN4I_FORMATS
,
536 .ops
= &sun4i_spdif_dai_ops
,
540 static const struct sun4i_spdif_quirks sun4i_a10_spdif_quirks
= {
541 .reg_dac_txdata
= SUN4I_SPDIF_TXFIFO
,
542 .val_fctl_ftx
= SUN4I_SPDIF_FCTL_FTX
,
545 static const struct sun4i_spdif_quirks sun6i_a31_spdif_quirks
= {
546 .reg_dac_txdata
= SUN4I_SPDIF_TXFIFO
,
547 .val_fctl_ftx
= SUN4I_SPDIF_FCTL_FTX
,
551 static const struct sun4i_spdif_quirks sun8i_h3_spdif_quirks
= {
552 .reg_dac_txdata
= SUN8I_SPDIF_TXFIFO
,
553 .val_fctl_ftx
= SUN4I_SPDIF_FCTL_FTX
,
557 static const struct sun4i_spdif_quirks sun50i_h6_spdif_quirks
= {
558 .reg_dac_txdata
= SUN8I_SPDIF_TXFIFO
,
559 .val_fctl_ftx
= SUN50I_H6_SPDIF_FCTL_FTX
,
563 static const struct of_device_id sun4i_spdif_of_match
[] = {
565 .compatible
= "allwinner,sun4i-a10-spdif",
566 .data
= &sun4i_a10_spdif_quirks
,
569 .compatible
= "allwinner,sun6i-a31-spdif",
570 .data
= &sun6i_a31_spdif_quirks
,
573 .compatible
= "allwinner,sun8i-h3-spdif",
574 .data
= &sun8i_h3_spdif_quirks
,
577 .compatible
= "allwinner,sun50i-h6-spdif",
578 .data
= &sun50i_h6_spdif_quirks
,
581 .compatible
= "allwinner,sun50i-h616-spdif",
582 /* Essentially the same as the H6, but without RX */
583 .data
= &sun50i_h6_spdif_quirks
,
587 MODULE_DEVICE_TABLE(of
, sun4i_spdif_of_match
);
589 static const struct snd_soc_component_driver sun4i_spdif_component
= {
590 .name
= "sun4i-spdif",
591 .legacy_dai_naming
= 1,
594 static int sun4i_spdif_runtime_suspend(struct device
*dev
)
596 struct sun4i_spdif_dev
*host
= dev_get_drvdata(dev
);
598 clk_disable_unprepare(host
->spdif_clk
);
599 clk_disable_unprepare(host
->apb_clk
);
604 static int sun4i_spdif_runtime_resume(struct device
*dev
)
606 struct sun4i_spdif_dev
*host
= dev_get_drvdata(dev
);
609 ret
= clk_prepare_enable(host
->spdif_clk
);
612 ret
= clk_prepare_enable(host
->apb_clk
);
614 clk_disable_unprepare(host
->spdif_clk
);
619 static int sun4i_spdif_probe(struct platform_device
*pdev
)
621 struct sun4i_spdif_dev
*host
;
622 struct resource
*res
;
623 const struct sun4i_spdif_quirks
*quirks
;
627 dev_dbg(&pdev
->dev
, "Entered %s\n", __func__
);
629 host
= devm_kzalloc(&pdev
->dev
, sizeof(*host
), GFP_KERNEL
);
634 spin_lock_init(&host
->lock
);
636 /* Initialize this copy of the CPU DAI driver structure */
637 memcpy(&host
->cpu_dai_drv
, &sun4i_spdif_dai
, sizeof(sun4i_spdif_dai
));
638 host
->cpu_dai_drv
.name
= dev_name(&pdev
->dev
);
640 /* Get the addresses */
641 base
= devm_platform_get_and_ioremap_resource(pdev
, 0, &res
);
643 return PTR_ERR(base
);
645 quirks
= of_device_get_match_data(&pdev
->dev
);
646 if (quirks
== NULL
) {
647 dev_err(&pdev
->dev
, "Failed to determine the quirks to use\n");
650 host
->quirks
= quirks
;
652 host
->regmap
= devm_regmap_init_mmio(&pdev
->dev
, base
,
653 &sun4i_spdif_regmap_config
);
656 host
->apb_clk
= devm_clk_get(&pdev
->dev
, "apb");
657 if (IS_ERR(host
->apb_clk
)) {
658 dev_err(&pdev
->dev
, "failed to get a apb clock.\n");
659 return PTR_ERR(host
->apb_clk
);
662 host
->spdif_clk
= devm_clk_get(&pdev
->dev
, "spdif");
663 if (IS_ERR(host
->spdif_clk
)) {
664 dev_err(&pdev
->dev
, "failed to get a spdif clock.\n");
665 return PTR_ERR(host
->spdif_clk
);
668 host
->dma_params_tx
.addr
= res
->start
+ quirks
->reg_dac_txdata
;
669 host
->dma_params_tx
.maxburst
= 8;
670 host
->dma_params_tx
.addr_width
= DMA_SLAVE_BUSWIDTH_2_BYTES
;
672 platform_set_drvdata(pdev
, host
);
674 if (quirks
->has_reset
) {
675 host
->rst
= devm_reset_control_get_optional_exclusive(&pdev
->dev
,
677 if (PTR_ERR(host
->rst
) == -EPROBE_DEFER
) {
679 dev_err(&pdev
->dev
, "Failed to get reset: %d\n", ret
);
682 if (!IS_ERR(host
->rst
))
683 reset_control_deassert(host
->rst
);
686 ret
= devm_snd_soc_register_component(&pdev
->dev
,
687 &sun4i_spdif_component
, &sun4i_spdif_dai
, 1);
691 pm_runtime_enable(&pdev
->dev
);
692 if (!pm_runtime_enabled(&pdev
->dev
)) {
693 ret
= sun4i_spdif_runtime_resume(&pdev
->dev
);
698 ret
= devm_snd_dmaengine_pcm_register(&pdev
->dev
, NULL
, 0);
703 if (!pm_runtime_status_suspended(&pdev
->dev
))
704 sun4i_spdif_runtime_suspend(&pdev
->dev
);
706 pm_runtime_disable(&pdev
->dev
);
710 static void sun4i_spdif_remove(struct platform_device
*pdev
)
712 pm_runtime_disable(&pdev
->dev
);
713 if (!pm_runtime_status_suspended(&pdev
->dev
))
714 sun4i_spdif_runtime_suspend(&pdev
->dev
);
717 static const struct dev_pm_ops sun4i_spdif_pm
= {
718 SET_RUNTIME_PM_OPS(sun4i_spdif_runtime_suspend
,
719 sun4i_spdif_runtime_resume
, NULL
)
722 static struct platform_driver sun4i_spdif_driver
= {
724 .name
= "sun4i-spdif",
725 .of_match_table
= sun4i_spdif_of_match
,
726 .pm
= &sun4i_spdif_pm
,
728 .probe
= sun4i_spdif_probe
,
729 .remove
= sun4i_spdif_remove
,
732 module_platform_driver(sun4i_spdif_driver
);
734 MODULE_AUTHOR("Marcus Cooper <codekipper@gmail.com>");
735 MODULE_AUTHOR("Andrea Venturi <be17068@iperbole.bo.it>");
736 MODULE_DESCRIPTION("Allwinner sun4i SPDIF SoC Interface");
737 MODULE_LICENSE("GPL");
738 MODULE_ALIAS("platform:sun4i-spdif");