1 // SPDX-License-Identifier: GPL-2.0-only
3 * amdtp-ff.c - a part of driver for RME Fireface series
5 * Copyright (c) 2015-2017 Takashi Sakamoto
12 unsigned int pcm_channels;
15 int amdtp_ff_set_parameters(struct amdtp_stream *s, unsigned int rate,
16 unsigned int pcm_channels)
18 struct amdtp_ff *p = s->protocol;
19 unsigned int data_channels;
21 if (amdtp_stream_running(s))
24 p->pcm_channels = pcm_channels;
25 data_channels = pcm_channels;
27 return amdtp_stream_set_parameters(s, rate, data_channels);
30 static void write_pcm_s32(struct amdtp_stream *s,
31 struct snd_pcm_substream *pcm,
32 __le32 *buffer, unsigned int frames)
34 struct amdtp_ff *p = s->protocol;
35 struct snd_pcm_runtime *runtime = pcm->runtime;
36 unsigned int channels, remaining_frames, i, c;
39 channels = p->pcm_channels;
40 src = (void *)runtime->dma_area +
41 frames_to_bytes(runtime, s->pcm_buffer_pointer);
42 remaining_frames = runtime->buffer_size - s->pcm_buffer_pointer;
44 for (i = 0; i < frames; ++i) {
45 for (c = 0; c < channels; ++c) {
46 buffer[c] = cpu_to_le32(*src);
49 buffer += s->data_block_quadlets;
50 if (--remaining_frames == 0)
51 src = (void *)runtime->dma_area;
55 static void read_pcm_s32(struct amdtp_stream *s,
56 struct snd_pcm_substream *pcm,
57 __le32 *buffer, unsigned int frames)
59 struct amdtp_ff *p = s->protocol;
60 struct snd_pcm_runtime *runtime = pcm->runtime;
61 unsigned int channels, remaining_frames, i, c;
64 channels = p->pcm_channels;
65 dst = (void *)runtime->dma_area +
66 frames_to_bytes(runtime, s->pcm_buffer_pointer);
67 remaining_frames = runtime->buffer_size - s->pcm_buffer_pointer;
69 for (i = 0; i < frames; ++i) {
70 for (c = 0; c < channels; ++c) {
71 *dst = le32_to_cpu(buffer[c]) & 0xffffff00;
74 buffer += s->data_block_quadlets;
75 if (--remaining_frames == 0)
76 dst = (void *)runtime->dma_area;
80 static void write_pcm_silence(struct amdtp_stream *s,
81 __le32 *buffer, unsigned int frames)
83 struct amdtp_ff *p = s->protocol;
84 unsigned int i, c, channels = p->pcm_channels;
86 for (i = 0; i < frames; ++i) {
87 for (c = 0; c < channels; ++c)
88 buffer[c] = cpu_to_le32(0x00000000);
89 buffer += s->data_block_quadlets;
93 int amdtp_ff_add_pcm_hw_constraints(struct amdtp_stream *s,
94 struct snd_pcm_runtime *runtime)
98 err = snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
102 return amdtp_stream_add_pcm_hw_constraints(s, runtime);
105 static unsigned int process_rx_data_blocks(struct amdtp_stream *s,
107 unsigned int data_blocks,
110 struct snd_pcm_substream *pcm = READ_ONCE(s->pcm);
111 unsigned int pcm_frames;
114 write_pcm_s32(s, pcm, (__le32 *)buffer, data_blocks);
115 pcm_frames = data_blocks;
117 write_pcm_silence(s, (__le32 *)buffer, data_blocks);
124 static unsigned int process_tx_data_blocks(struct amdtp_stream *s,
126 unsigned int data_blocks,
129 struct snd_pcm_substream *pcm = READ_ONCE(s->pcm);
130 unsigned int pcm_frames;
133 read_pcm_s32(s, pcm, (__le32 *)buffer, data_blocks);
134 pcm_frames = data_blocks;
142 int amdtp_ff_init(struct amdtp_stream *s, struct fw_unit *unit,
143 enum amdtp_stream_direction dir)
145 amdtp_stream_process_data_blocks_t process_data_blocks;
147 if (dir == AMDTP_IN_STREAM)
148 process_data_blocks = process_tx_data_blocks;
150 process_data_blocks = process_rx_data_blocks;
152 return amdtp_stream_init(s, unit, dir, CIP_NO_HEADER, 0,
153 process_data_blocks, sizeof(struct amdtp_ff));