1 // SPDX-License-Identifier: GPL-2.0
3 // Socionext UniPhier AIO Compress Audio driver.
5 // Copyright (c) 2017-2018 Socionext Inc.
7 #include <linux/bitfield.h>
8 #include <linux/circ_buf.h>
9 #include <linux/dma-mapping.h>
10 #include <linux/errno.h>
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <sound/core.h>
14 #include <sound/pcm.h>
15 #include <sound/soc.h>
19 static int uniphier_aio_compr_prepare(struct snd_soc_component
*component
,
20 struct snd_compr_stream
*cstream
);
21 static int uniphier_aio_compr_hw_free(struct snd_soc_component
*component
,
22 struct snd_compr_stream
*cstream
);
24 static int uniphier_aio_comprdma_new(struct snd_soc_pcm_runtime
*rtd
)
26 struct snd_compr
*compr
= rtd
->compr
;
27 struct device
*dev
= compr
->card
->dev
;
28 struct uniphier_aio
*aio
= uniphier_priv(asoc_rtd_to_cpu(rtd
, 0));
29 struct uniphier_aio_sub
*sub
= &aio
->sub
[compr
->direction
];
30 size_t size
= AUD_RING_SIZE
;
31 int dma_dir
= DMA_FROM_DEVICE
, ret
;
33 ret
= dma_set_mask_and_coherent(dev
, DMA_BIT_MASK(33));
37 sub
->compr_area
= kzalloc(size
, GFP_KERNEL
);
41 if (sub
->swm
->dir
== PORT_DIR_OUTPUT
)
42 dma_dir
= DMA_TO_DEVICE
;
44 sub
->compr_addr
= dma_map_single(dev
, sub
->compr_area
, size
, dma_dir
);
45 if (dma_mapping_error(dev
, sub
->compr_addr
)) {
46 kfree(sub
->compr_area
);
47 sub
->compr_area
= NULL
;
52 sub
->compr_bytes
= size
;
57 static int uniphier_aio_comprdma_free(struct snd_soc_pcm_runtime
*rtd
)
59 struct snd_compr
*compr
= rtd
->compr
;
60 struct device
*dev
= compr
->card
->dev
;
61 struct uniphier_aio
*aio
= uniphier_priv(asoc_rtd_to_cpu(rtd
, 0));
62 struct uniphier_aio_sub
*sub
= &aio
->sub
[compr
->direction
];
63 int dma_dir
= DMA_FROM_DEVICE
;
65 if (sub
->swm
->dir
== PORT_DIR_OUTPUT
)
66 dma_dir
= DMA_TO_DEVICE
;
68 dma_unmap_single(dev
, sub
->compr_addr
, sub
->compr_bytes
, dma_dir
);
69 kfree(sub
->compr_area
);
70 sub
->compr_area
= NULL
;
75 static int uniphier_aio_compr_open(struct snd_soc_component
*component
,
76 struct snd_compr_stream
*cstream
)
78 struct snd_soc_pcm_runtime
*rtd
= cstream
->private_data
;
79 struct uniphier_aio
*aio
= uniphier_priv(asoc_rtd_to_cpu(rtd
, 0));
80 struct uniphier_aio_sub
*sub
= &aio
->sub
[cstream
->direction
];
86 sub
->cstream
= cstream
;
87 sub
->pass_through
= 1;
88 sub
->use_mmap
= false;
90 ret
= uniphier_aio_comprdma_new(rtd
);
101 static int uniphier_aio_compr_free(struct snd_soc_component
*component
,
102 struct snd_compr_stream
*cstream
)
104 struct snd_soc_pcm_runtime
*rtd
= cstream
->private_data
;
105 struct uniphier_aio
*aio
= uniphier_priv(asoc_rtd_to_cpu(rtd
, 0));
106 struct uniphier_aio_sub
*sub
= &aio
->sub
[cstream
->direction
];
109 ret
= uniphier_aio_compr_hw_free(component
, cstream
);
112 ret
= uniphier_aio_comprdma_free(rtd
);
121 static int uniphier_aio_compr_get_params(struct snd_soc_component
*component
,
122 struct snd_compr_stream
*cstream
,
123 struct snd_codec
*params
)
125 struct snd_soc_pcm_runtime
*rtd
= cstream
->private_data
;
126 struct uniphier_aio
*aio
= uniphier_priv(asoc_rtd_to_cpu(rtd
, 0));
127 struct uniphier_aio_sub
*sub
= &aio
->sub
[cstream
->direction
];
129 *params
= sub
->cparams
.codec
;
134 static int uniphier_aio_compr_set_params(struct snd_soc_component
*component
,
135 struct snd_compr_stream
*cstream
,
136 struct snd_compr_params
*params
)
138 struct snd_soc_pcm_runtime
*rtd
= cstream
->private_data
;
139 struct uniphier_aio
*aio
= uniphier_priv(asoc_rtd_to_cpu(rtd
, 0));
140 struct uniphier_aio_sub
*sub
= &aio
->sub
[cstream
->direction
];
141 struct device
*dev
= &aio
->chip
->pdev
->dev
;
144 if (params
->codec
.id
!= SND_AUDIOCODEC_IEC61937
) {
145 dev_err(dev
, "Codec ID is not supported(%d)\n",
149 if (params
->codec
.profile
!= SND_AUDIOPROFILE_IEC61937_SPDIF
) {
150 dev_err(dev
, "Codec profile is not supported(%d)\n",
151 params
->codec
.profile
);
155 /* IEC frame type will be changed after received valid data */
156 sub
->iec_pc
= IEC61937_PC_AAC
;
158 sub
->cparams
= *params
;
164 ret
= uniphier_aio_compr_prepare(component
, cstream
);
171 static int uniphier_aio_compr_hw_free(struct snd_soc_component
*component
,
172 struct snd_compr_stream
*cstream
)
174 struct snd_soc_pcm_runtime
*rtd
= cstream
->private_data
;
175 struct uniphier_aio
*aio
= uniphier_priv(asoc_rtd_to_cpu(rtd
, 0));
176 struct uniphier_aio_sub
*sub
= &aio
->sub
[cstream
->direction
];
183 static int uniphier_aio_compr_prepare(struct snd_soc_component
*component
,
184 struct snd_compr_stream
*cstream
)
186 struct snd_soc_pcm_runtime
*rtd
= cstream
->private_data
;
187 struct snd_compr_runtime
*runtime
= cstream
->runtime
;
188 struct uniphier_aio
*aio
= uniphier_priv(asoc_rtd_to_cpu(rtd
, 0));
189 struct uniphier_aio_sub
*sub
= &aio
->sub
[cstream
->direction
];
190 int bytes
= runtime
->fragment_size
;
194 ret
= aiodma_ch_set_param(sub
);
198 spin_lock_irqsave(&sub
->lock
, flags
);
199 ret
= aiodma_rb_set_buffer(sub
, sub
->compr_addr
,
200 sub
->compr_addr
+ sub
->compr_bytes
,
202 spin_unlock_irqrestore(&sub
->lock
, flags
);
206 ret
= aio_port_set_param(sub
, sub
->pass_through
, &sub
->params
);
209 ret
= aio_oport_set_stream_type(sub
, sub
->iec_pc
);
212 aio_port_set_enable(sub
, 1);
214 ret
= aio_if_set_param(sub
, sub
->pass_through
);
221 static int uniphier_aio_compr_trigger(struct snd_soc_component
*component
,
222 struct snd_compr_stream
*cstream
,
225 struct snd_soc_pcm_runtime
*rtd
= cstream
->private_data
;
226 struct snd_compr_runtime
*runtime
= cstream
->runtime
;
227 struct uniphier_aio
*aio
= uniphier_priv(asoc_rtd_to_cpu(rtd
, 0));
228 struct uniphier_aio_sub
*sub
= &aio
->sub
[cstream
->direction
];
229 struct device
*dev
= &aio
->chip
->pdev
->dev
;
230 int bytes
= runtime
->fragment_size
, ret
= 0;
233 spin_lock_irqsave(&sub
->lock
, flags
);
235 case SNDRV_PCM_TRIGGER_START
:
236 aiodma_rb_sync(sub
, sub
->compr_addr
, sub
->compr_bytes
, bytes
);
237 aiodma_ch_set_enable(sub
, 1);
241 case SNDRV_PCM_TRIGGER_STOP
:
243 aiodma_ch_set_enable(sub
, 0);
247 dev_warn(dev
, "Unknown trigger(%d)\n", cmd
);
250 spin_unlock_irqrestore(&sub
->lock
, flags
);
255 static int uniphier_aio_compr_pointer(struct snd_soc_component
*component
,
256 struct snd_compr_stream
*cstream
,
257 struct snd_compr_tstamp
*tstamp
)
259 struct snd_soc_pcm_runtime
*rtd
= cstream
->private_data
;
260 struct snd_compr_runtime
*runtime
= cstream
->runtime
;
261 struct uniphier_aio
*aio
= uniphier_priv(asoc_rtd_to_cpu(rtd
, 0));
262 struct uniphier_aio_sub
*sub
= &aio
->sub
[cstream
->direction
];
263 int bytes
= runtime
->fragment_size
;
267 spin_lock_irqsave(&sub
->lock
, flags
);
269 aiodma_rb_sync(sub
, sub
->compr_addr
, sub
->compr_bytes
, bytes
);
271 if (sub
->swm
->dir
== PORT_DIR_OUTPUT
) {
273 /* Size of AIO output format is double of IEC61937 */
274 tstamp
->copied_total
= sub
->rd_total
/ 2;
277 tstamp
->copied_total
= sub
->rd_total
;
279 tstamp
->byte_offset
= pos
;
281 spin_unlock_irqrestore(&sub
->lock
, flags
);
286 static int aio_compr_send_to_hw(struct uniphier_aio_sub
*sub
,
287 char __user
*buf
, size_t dstsize
)
289 u32 __user
*srcbuf
= (u32 __user
*)buf
;
290 u32
*dstbuf
= (u32
*)(sub
->compr_area
+ sub
->wr_offs
);
291 int src
= 0, dst
= 0, ret
;
292 u32 frm
, frm_a
, frm_b
;
294 while (dstsize
> 0) {
295 ret
= get_user(frm
, srcbuf
+ src
);
300 frm_a
= frm
& 0xffff;
301 frm_b
= (frm
>> 16) & 0xffff;
303 if (frm
== IEC61937_HEADER_SIGN
) {
306 /* Next data is Pc and Pd */
307 sub
->iec_header
= true;
309 u16 pc
= be16_to_cpu((__be16
)frm_a
);
311 if (sub
->iec_header
&& sub
->iec_pc
!= pc
) {
312 /* Force overwrite IEC frame type */
314 ret
= aio_oport_set_stream_type(sub
, pc
);
318 sub
->iec_header
= false;
320 dstbuf
[dst
++] = frm_a
;
321 dstbuf
[dst
++] = frm_b
;
323 dstsize
-= sizeof(u32
) * 2;
329 static int uniphier_aio_compr_copy(struct snd_soc_component
*component
,
330 struct snd_compr_stream
*cstream
,
331 char __user
*buf
, size_t count
)
333 struct snd_soc_pcm_runtime
*rtd
= cstream
->private_data
;
334 struct snd_compr_runtime
*runtime
= cstream
->runtime
;
335 struct device
*carddev
= rtd
->compr
->card
->dev
;
336 struct uniphier_aio
*aio
= uniphier_priv(asoc_rtd_to_cpu(rtd
, 0));
337 struct uniphier_aio_sub
*sub
= &aio
->sub
[cstream
->direction
];
338 size_t cnt
= min_t(size_t, count
, aio_rb_space_to_end(sub
) / 2);
339 int bytes
= runtime
->fragment_size
;
344 if (cnt
< sizeof(u32
))
347 if (sub
->swm
->dir
== PORT_DIR_OUTPUT
) {
348 dma_addr_t dmapos
= sub
->compr_addr
+ sub
->wr_offs
;
350 /* Size of AIO output format is double of IEC61937 */
353 dma_sync_single_for_cpu(carddev
, dmapos
, s
, DMA_TO_DEVICE
);
354 ret
= aio_compr_send_to_hw(sub
, buf
, s
);
355 dma_sync_single_for_device(carddev
, dmapos
, s
, DMA_TO_DEVICE
);
357 dma_addr_t dmapos
= sub
->compr_addr
+ sub
->rd_offs
;
361 dma_sync_single_for_cpu(carddev
, dmapos
, s
, DMA_FROM_DEVICE
);
362 ret
= copy_to_user(buf
, sub
->compr_area
+ sub
->rd_offs
, s
);
363 dma_sync_single_for_device(carddev
, dmapos
, s
, DMA_FROM_DEVICE
);
368 spin_lock_irqsave(&sub
->lock
, flags
);
370 sub
->threshold
= 2 * bytes
;
371 aiodma_rb_set_threshold(sub
, sub
->compr_bytes
, 2 * bytes
);
373 if (sub
->swm
->dir
== PORT_DIR_OUTPUT
) {
375 if (sub
->wr_offs
>= sub
->compr_bytes
)
376 sub
->wr_offs
-= sub
->compr_bytes
;
379 if (sub
->rd_offs
>= sub
->compr_bytes
)
380 sub
->rd_offs
-= sub
->compr_bytes
;
382 aiodma_rb_sync(sub
, sub
->compr_addr
, sub
->compr_bytes
, bytes
);
384 spin_unlock_irqrestore(&sub
->lock
, flags
);
389 static int uniphier_aio_compr_get_caps(struct snd_soc_component
*component
,
390 struct snd_compr_stream
*cstream
,
391 struct snd_compr_caps
*caps
)
393 caps
->num_codecs
= 1;
394 caps
->min_fragment_size
= AUD_MIN_FRAGMENT_SIZE
;
395 caps
->max_fragment_size
= AUD_MAX_FRAGMENT_SIZE
;
396 caps
->min_fragments
= AUD_MIN_FRAGMENT
;
397 caps
->max_fragments
= AUD_MAX_FRAGMENT
;
398 caps
->codecs
[0] = SND_AUDIOCODEC_IEC61937
;
403 static const struct snd_compr_codec_caps caps_iec
= {
404 .num_descriptors
= 1,
405 .descriptor
[0].max_ch
= 8,
406 .descriptor
[0].num_sample_rates
= 0,
407 .descriptor
[0].num_bitrates
= 0,
408 .descriptor
[0].profiles
= SND_AUDIOPROFILE_IEC61937_SPDIF
,
409 .descriptor
[0].modes
= SND_AUDIOMODE_IEC_AC3
|
410 SND_AUDIOMODE_IEC_MPEG1
|
411 SND_AUDIOMODE_IEC_MP3
|
412 SND_AUDIOMODE_IEC_DTS
,
413 .descriptor
[0].formats
= 0,
416 static int uniphier_aio_compr_get_codec_caps(struct snd_soc_component
*component
,
417 struct snd_compr_stream
*stream
,
418 struct snd_compr_codec_caps
*codec
)
420 if (codec
->codec
== SND_AUDIOCODEC_IEC61937
)
428 const struct snd_compress_ops uniphier_aio_compress_ops
= {
429 .open
= uniphier_aio_compr_open
,
430 .free
= uniphier_aio_compr_free
,
431 .get_params
= uniphier_aio_compr_get_params
,
432 .set_params
= uniphier_aio_compr_set_params
,
433 .trigger
= uniphier_aio_compr_trigger
,
434 .pointer
= uniphier_aio_compr_pointer
,
435 .copy
= uniphier_aio_compr_copy
,
436 .get_caps
= uniphier_aio_compr_get_caps
,
437 .get_codec_caps
= uniphier_aio_compr_get_codec_caps
,