xref: /openbmc/linux/sound/firewire/motu/amdtp-motu.c (revision a36954f5)
1 /*
2  * amdtp-motu.c - a part of driver for MOTU FireWire series
3  *
4  * Copyright (c) 2015-2017 Takashi Sakamoto <o-takashi@sakamocchi.jp>
5  *
6  * Licensed under the terms of the GNU General Public License, version 2.
7  */
8 
9 #include <linux/slab.h>
10 #include <sound/pcm.h>
11 #include "motu.h"
12 
13 #define CREATE_TRACE_POINTS
14 #include "amdtp-motu-trace.h"
15 
16 #define CIP_FMT_MOTU		0x02
17 #define CIP_FMT_MOTU_TX_V3	0x22
18 #define MOTU_FDF_AM824		0x22
19 
20 /*
21  * Nominally 3125 bytes/second, but the MIDI port's clock might be
22  * 1% too slow, and the bus clock 100 ppm too fast.
23  */
24 #define MIDI_BYTES_PER_SECOND	3093
25 
26 struct amdtp_motu {
27 	/* For timestamp processing.  */
28 	unsigned int quotient_ticks_per_event;
29 	unsigned int remainder_ticks_per_event;
30 	unsigned int next_ticks;
31 	unsigned int next_accumulated;
32 	unsigned int next_cycles;
33 	unsigned int next_seconds;
34 
35 	unsigned int pcm_chunks;
36 	unsigned int pcm_byte_offset;
37 
38 	struct snd_rawmidi_substream *midi;
39 	unsigned int midi_ports;
40 	unsigned int midi_flag_offset;
41 	unsigned int midi_byte_offset;
42 
43 	int midi_db_count;
44 	unsigned int midi_db_interval;
45 };
46 
47 int amdtp_motu_set_parameters(struct amdtp_stream *s, unsigned int rate,
48 			      unsigned int midi_ports,
49 			      struct snd_motu_packet_format *formats)
50 {
51 	static const struct {
52 		unsigned int quotient_ticks_per_event;
53 		unsigned int remainder_ticks_per_event;
54 	} params[] = {
55 		[CIP_SFC_44100]  = { 557, 123 },
56 		[CIP_SFC_48000]  = { 512,   0 },
57 		[CIP_SFC_88200]  = { 278, 282 },
58 		[CIP_SFC_96000]  = { 256,   0 },
59 		[CIP_SFC_176400] = { 139, 141 },
60 		[CIP_SFC_192000] = { 128,   0 },
61 	};
62 	struct amdtp_motu *p = s->protocol;
63 	unsigned int pcm_chunks, data_chunks, data_block_quadlets;
64 	unsigned int delay;
65 	unsigned int mode;
66 	int i, err;
67 
68 	if (amdtp_stream_running(s))
69 		return -EBUSY;
70 
71 	for (i = 0; i < ARRAY_SIZE(snd_motu_clock_rates); ++i) {
72 		if (snd_motu_clock_rates[i] == rate) {
73 			mode = i >> 1;
74 			break;
75 		}
76 	}
77 	if (i == ARRAY_SIZE(snd_motu_clock_rates))
78 		return -EINVAL;
79 
80 	pcm_chunks = formats->fixed_part_pcm_chunks[mode] +
81 		     formats->differed_part_pcm_chunks[mode];
82 	data_chunks = formats->msg_chunks + pcm_chunks;
83 
84 	/*
85 	 * Each data block includes SPH in its head. Data chunks follow with
86 	 * 3 byte alignment. Padding follows with zero to conform to quadlet
87 	 * alignment.
88 	 */
89 	data_block_quadlets = 1 + DIV_ROUND_UP(data_chunks * 3, 4);
90 
91 	err = amdtp_stream_set_parameters(s, rate, data_block_quadlets);
92 	if (err < 0)
93 		return err;
94 
95 	p->pcm_chunks = pcm_chunks;
96 	p->pcm_byte_offset = formats->pcm_byte_offset;
97 
98 	p->midi_ports = midi_ports;
99 	p->midi_flag_offset = formats->midi_flag_offset;
100 	p->midi_byte_offset = formats->midi_byte_offset;
101 
102 	p->midi_db_count = 0;
103 	p->midi_db_interval = rate / MIDI_BYTES_PER_SECOND;
104 
105 	/* IEEE 1394 bus requires. */
106 	delay = 0x2e00;
107 
108 	/* For no-data or empty packets to adjust PCM sampling frequency. */
109 	delay += 8000 * 3072 * s->syt_interval / rate;
110 
111 	p->next_seconds = 0;
112 	p->next_cycles = delay / 3072;
113 	p->quotient_ticks_per_event = params[s->sfc].quotient_ticks_per_event;
114 	p->remainder_ticks_per_event = params[s->sfc].remainder_ticks_per_event;
115 	p->next_ticks = delay % 3072;
116 	p->next_accumulated = 0;
117 
118 	return 0;
119 }
120 
121 static void read_pcm_s32(struct amdtp_stream *s,
122 			 struct snd_pcm_runtime *runtime,
123 			 __be32 *buffer, unsigned int data_blocks)
124 {
125 	struct amdtp_motu *p = s->protocol;
126 	unsigned int channels, remaining_frames, i, c;
127 	u8 *byte;
128 	u32 *dst;
129 
130 	channels = p->pcm_chunks;
131 	dst = (void *)runtime->dma_area +
132 			frames_to_bytes(runtime, s->pcm_buffer_pointer);
133 	remaining_frames = runtime->buffer_size - s->pcm_buffer_pointer;
134 
135 	for (i = 0; i < data_blocks; ++i) {
136 		byte = (u8 *)buffer + p->pcm_byte_offset;
137 
138 		for (c = 0; c < channels; ++c) {
139 			*dst = (byte[0] << 24) | (byte[1] << 16) | byte[2];
140 			byte += 3;
141 			dst++;
142 		}
143 		buffer += s->data_block_quadlets;
144 		if (--remaining_frames == 0)
145 			dst = (void *)runtime->dma_area;
146 	}
147 }
148 
149 static void write_pcm_s32(struct amdtp_stream *s,
150 			  struct snd_pcm_runtime *runtime,
151 			  __be32 *buffer, unsigned int data_blocks)
152 {
153 	struct amdtp_motu *p = s->protocol;
154 	unsigned int channels, remaining_frames, i, c;
155 	u8 *byte;
156 	const u32 *src;
157 
158 	channels = p->pcm_chunks;
159 	src = (void *)runtime->dma_area +
160 			frames_to_bytes(runtime, s->pcm_buffer_pointer);
161 	remaining_frames = runtime->buffer_size - s->pcm_buffer_pointer;
162 
163 	for (i = 0; i < data_blocks; ++i) {
164 		byte = (u8 *)buffer + p->pcm_byte_offset;
165 
166 		for (c = 0; c < channels; ++c) {
167 			byte[0] = (*src >> 24) & 0xff;
168 			byte[1] = (*src >> 16) & 0xff;
169 			byte[2] = (*src >>  8) & 0xff;
170 			byte += 3;
171 			src++;
172 		}
173 
174 		buffer += s->data_block_quadlets;
175 		if (--remaining_frames == 0)
176 			src = (void *)runtime->dma_area;
177 	}
178 }
179 
180 static void write_pcm_silence(struct amdtp_stream *s, __be32 *buffer,
181 			      unsigned int data_blocks)
182 {
183 	struct amdtp_motu *p = s->protocol;
184 	unsigned int channels, i, c;
185 	u8 *byte;
186 
187 	channels = p->pcm_chunks;
188 
189 	for (i = 0; i < data_blocks; ++i) {
190 		byte = (u8 *)buffer + p->pcm_byte_offset;
191 
192 		for (c = 0; c < channels; ++c) {
193 			byte[0] = 0;
194 			byte[1] = 0;
195 			byte[2] = 0;
196 			byte += 3;
197 		}
198 
199 		buffer += s->data_block_quadlets;
200 	}
201 }
202 
203 int amdtp_motu_add_pcm_hw_constraints(struct amdtp_stream *s,
204 				      struct snd_pcm_runtime *runtime)
205 {
206 	int err;
207 
208 	/* TODO: how to set an constraint for exactly 24bit PCM sample? */
209 	err = snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
210 	if (err < 0)
211 		return err;
212 
213 	return amdtp_stream_add_pcm_hw_constraints(s, runtime);
214 }
215 
216 void amdtp_motu_midi_trigger(struct amdtp_stream *s, unsigned int port,
217 			     struct snd_rawmidi_substream *midi)
218 {
219 	struct amdtp_motu *p = s->protocol;
220 
221 	if (port < p->midi_ports)
222 		WRITE_ONCE(p->midi, midi);
223 }
224 
225 static void write_midi_messages(struct amdtp_stream *s, __be32 *buffer,
226 				unsigned int data_blocks)
227 {
228 	struct amdtp_motu *p = s->protocol;
229 	struct snd_rawmidi_substream *midi = READ_ONCE(p->midi);
230 	u8 *b;
231 	int i;
232 
233 	for (i = 0; i < data_blocks; i++) {
234 		b = (u8 *)buffer;
235 
236 		if (midi && p->midi_db_count == 0 &&
237 		    snd_rawmidi_transmit(midi, b + p->midi_byte_offset, 1) == 1) {
238 			b[p->midi_flag_offset] = 0x01;
239 		} else {
240 			b[p->midi_byte_offset] = 0x00;
241 			b[p->midi_flag_offset] = 0x00;
242 		}
243 
244 		buffer += s->data_block_quadlets;
245 
246 		if (--p->midi_db_count < 0)
247 			p->midi_db_count = p->midi_db_interval;
248 	}
249 }
250 
251 static void read_midi_messages(struct amdtp_stream *s, __be32 *buffer,
252 			       unsigned int data_blocks)
253 {
254 	struct amdtp_motu *p = s->protocol;
255 	struct snd_rawmidi_substream *midi;
256 	u8 *b;
257 	int i;
258 
259 	for (i = 0; i < data_blocks; i++) {
260 		b = (u8 *)buffer;
261 		midi = READ_ONCE(p->midi);
262 
263 		if (midi && (b[p->midi_flag_offset] & 0x01))
264 			snd_rawmidi_receive(midi, b + p->midi_byte_offset, 1);
265 
266 		buffer += s->data_block_quadlets;
267 	}
268 }
269 
270 /* For tracepoints. */
271 static void __maybe_unused copy_sph(u32 *frames, __be32 *buffer,
272 				    unsigned int data_blocks,
273 				    unsigned int data_block_quadlets)
274 {
275 	unsigned int i;
276 
277 	for (i = 0; i < data_blocks; ++i) {
278 		*frames = be32_to_cpu(*buffer);
279 		buffer += data_block_quadlets;
280 		frames++;
281 	}
282 }
283 
284 /* For tracepoints. */
285 static void __maybe_unused copy_message(u64 *frames, __be32 *buffer,
286 					unsigned int data_blocks,
287 					unsigned int data_block_quadlets)
288 {
289 	unsigned int i;
290 
291 	/* This is just for v2/v3 protocol. */
292 	for (i = 0; i < data_blocks; ++i) {
293 		*frames = (be32_to_cpu(buffer[1]) << 16) |
294 			  (be32_to_cpu(buffer[2]) >> 16);
295 		buffer += data_block_quadlets;
296 		frames++;
297 	}
298 }
299 
300 static unsigned int process_tx_data_blocks(struct amdtp_stream *s,
301 				__be32 *buffer, unsigned int data_blocks,
302 				unsigned int *syt)
303 {
304 	struct amdtp_motu *p = s->protocol;
305 	struct snd_pcm_substream *pcm;
306 
307 	trace_in_data_block_sph(s, data_blocks, buffer);
308 	trace_in_data_block_message(s, data_blocks, buffer);
309 
310 	if (p->midi_ports)
311 		read_midi_messages(s, buffer, data_blocks);
312 
313 	pcm = ACCESS_ONCE(s->pcm);
314 	if (data_blocks > 0 && pcm)
315 		read_pcm_s32(s, pcm->runtime, buffer, data_blocks);
316 
317 	return data_blocks;
318 }
319 
320 static inline void compute_next_elapse_from_start(struct amdtp_motu *p)
321 {
322 	p->next_accumulated += p->remainder_ticks_per_event;
323 	if (p->next_accumulated >= 441) {
324 		p->next_accumulated -= 441;
325 		p->next_ticks++;
326 	}
327 
328 	p->next_ticks += p->quotient_ticks_per_event;
329 	if (p->next_ticks >= 3072) {
330 		p->next_ticks -= 3072;
331 		p->next_cycles++;
332 	}
333 
334 	if (p->next_cycles >= 8000) {
335 		p->next_cycles -= 8000;
336 		p->next_seconds++;
337 	}
338 
339 	if (p->next_seconds >= 128)
340 		p->next_seconds -= 128;
341 }
342 
343 static void write_sph(struct amdtp_stream *s, __be32 *buffer,
344 		      unsigned int data_blocks)
345 {
346 	struct amdtp_motu *p = s->protocol;
347 	unsigned int next_cycles;
348 	unsigned int i;
349 	u32 sph;
350 
351 	for (i = 0; i < data_blocks; i++) {
352 		next_cycles = (s->start_cycle + p->next_cycles) % 8000;
353 		sph = ((next_cycles << 12) | p->next_ticks) & 0x01ffffff;
354 		*buffer = cpu_to_be32(sph);
355 
356 		compute_next_elapse_from_start(p);
357 
358 		buffer += s->data_block_quadlets;
359 	}
360 }
361 
362 static unsigned int process_rx_data_blocks(struct amdtp_stream *s,
363 				__be32 *buffer, unsigned int data_blocks,
364 				unsigned int *syt)
365 {
366 	struct amdtp_motu *p = (struct amdtp_motu *)s->protocol;
367 	struct snd_pcm_substream *pcm;
368 
369 	/* Not used. */
370 	*syt = 0xffff;
371 
372 	/* TODO: how to interact control messages between userspace? */
373 
374 	if (p->midi_ports)
375 		write_midi_messages(s, buffer, data_blocks);
376 
377 	pcm = ACCESS_ONCE(s->pcm);
378 	if (pcm)
379 		write_pcm_s32(s, pcm->runtime, buffer, data_blocks);
380 	else
381 		write_pcm_silence(s, buffer, data_blocks);
382 
383 	write_sph(s, buffer, data_blocks);
384 
385 	trace_out_data_block_sph(s, data_blocks, buffer);
386 	trace_out_data_block_message(s, data_blocks, buffer);
387 
388 	return data_blocks;
389 }
390 
391 int amdtp_motu_init(struct amdtp_stream *s, struct fw_unit *unit,
392 		    enum amdtp_stream_direction dir,
393 		    const struct snd_motu_protocol *const protocol)
394 {
395 	amdtp_stream_process_data_blocks_t process_data_blocks;
396 	int fmt = CIP_FMT_MOTU;
397 	int flags = CIP_BLOCKING;
398 	int err;
399 
400 	if (dir == AMDTP_IN_STREAM) {
401 		process_data_blocks = process_tx_data_blocks;
402 
403 		/*
404 		 * Units of version 3 transmits packets with invalid CIP header
405 		 * against IEC 61883-1.
406 		 */
407 		if (protocol == &snd_motu_protocol_v3) {
408 			flags |= CIP_WRONG_DBS |
409 				 CIP_SKIP_DBC_ZERO_CHECK |
410 				 CIP_HEADER_WITHOUT_EOH;
411 			fmt = CIP_FMT_MOTU_TX_V3;
412 		}
413 	} else {
414 		process_data_blocks = process_rx_data_blocks;
415 		flags |= CIP_DBC_IS_END_EVENT;
416 	}
417 
418 	err = amdtp_stream_init(s, unit, dir, flags, fmt, process_data_blocks,
419 				sizeof(struct amdtp_motu));
420 	if (err < 0)
421 		return err;
422 
423 	s->sph = 1;
424 	s->fdf = MOTU_FDF_AM824;
425 
426 	return 0;
427 }
428