blob: a6a39f7ef58d8e32cf6e6ff60d45bcbbb23b1e04 [file] [log] [blame]
Takashi Sakamoto6eb6c812014-11-29 00:59:14 +09001/*
2 * dice_stream.c - a part of driver for DICE based devices
3 *
4 * Copyright (c) Clemens Ladisch <clemens@ladisch.de>
5 * Copyright (c) 2014 Takashi Sakamoto <o-takashi@sakamocchi.jp>
6 *
7 * Licensed under the terms of the GNU General Public License, version 2.
8 */
9
10#include "dice.h"
11
Takashi Sakamoto288a8d02014-12-09 00:10:35 +090012#define CALLBACK_TIMEOUT 200
13
Takashi Sakamoto6eb6c812014-11-29 00:59:14 +090014const unsigned int snd_dice_rates[SND_DICE_RATES_COUNT] = {
15 /* mode 0 */
16 [0] = 32000,
17 [1] = 44100,
18 [2] = 48000,
19 /* mode 1 */
20 [3] = 88200,
21 [4] = 96000,
22 /* mode 2 */
23 [5] = 176400,
24 [6] = 192000,
25};
26
27int snd_dice_stream_get_rate_mode(struct snd_dice *dice, unsigned int rate,
28 unsigned int *mode)
29{
30 int i;
31
32 for (i = 0; i < ARRAY_SIZE(snd_dice_rates); i++) {
33 if (!(dice->clock_caps & BIT(i)))
34 continue;
35 if (snd_dice_rates[i] != rate)
36 continue;
37
38 *mode = (i - 1) / 2;
39 return 0;
40 }
41 return -EINVAL;
42}
43
Takashi Sakamoto9a028432014-12-09 00:10:36 +090044static void release_resources(struct snd_dice *dice,
45 struct fw_iso_resources *resources)
Takashi Sakamoto6eb6c812014-11-29 00:59:14 +090046{
Takashi Sakamoto3e93d422015-10-18 22:39:49 +090047 __be32 channel;
Takashi Sakamoto6eb6c812014-11-29 00:59:14 +090048
Takashi Sakamoto288a8d02014-12-09 00:10:35 +090049 /* Reset channel number */
Takashi Sakamoto6eb6c812014-11-29 00:59:14 +090050 channel = cpu_to_be32((u32)-1);
Takashi Sakamoto9a028432014-12-09 00:10:36 +090051 if (resources == &dice->tx_resources)
52 snd_dice_transaction_write_tx(dice, TX_ISOCHRONOUS,
Takashi Sakamoto3e93d422015-10-18 22:39:49 +090053 &channel, sizeof(channel));
Takashi Sakamoto9a028432014-12-09 00:10:36 +090054 else
55 snd_dice_transaction_write_rx(dice, RX_ISOCHRONOUS,
Takashi Sakamoto3e93d422015-10-18 22:39:49 +090056 &channel, sizeof(channel));
Takashi Sakamoto288a8d02014-12-09 00:10:35 +090057
Takashi Sakamoto9a028432014-12-09 00:10:36 +090058 fw_iso_resources_free(resources);
Takashi Sakamoto288a8d02014-12-09 00:10:35 +090059}
60
Takashi Sakamoto9a028432014-12-09 00:10:36 +090061static int keep_resources(struct snd_dice *dice,
62 struct fw_iso_resources *resources,
63 unsigned int max_payload_bytes)
Takashi Sakamoto288a8d02014-12-09 00:10:35 +090064{
Takashi Sakamoto3e93d422015-10-18 22:39:49 +090065 __be32 channel;
Takashi Sakamoto288a8d02014-12-09 00:10:35 +090066 int err;
67
Takashi Sakamoto9a028432014-12-09 00:10:36 +090068 err = fw_iso_resources_allocate(resources, max_payload_bytes,
Takashi Sakamoto288a8d02014-12-09 00:10:35 +090069 fw_parent_device(dice->unit)->max_speed);
70 if (err < 0)
71 goto end;
72
73 /* Set channel number */
Takashi Sakamoto9a028432014-12-09 00:10:36 +090074 channel = cpu_to_be32(resources->channel);
75 if (resources == &dice->tx_resources)
76 err = snd_dice_transaction_write_tx(dice, TX_ISOCHRONOUS,
Takashi Sakamoto3e93d422015-10-18 22:39:49 +090077 &channel, sizeof(channel));
Takashi Sakamoto9a028432014-12-09 00:10:36 +090078 else
79 err = snd_dice_transaction_write_rx(dice, RX_ISOCHRONOUS,
Takashi Sakamoto3e93d422015-10-18 22:39:49 +090080 &channel, sizeof(channel));
Takashi Sakamoto288a8d02014-12-09 00:10:35 +090081 if (err < 0)
Takashi Sakamoto9a028432014-12-09 00:10:36 +090082 release_resources(dice, resources);
Takashi Sakamoto288a8d02014-12-09 00:10:35 +090083end:
Takashi Sakamoto6eb6c812014-11-29 00:59:14 +090084 return err;
85}
86
Takashi Sakamoto9a028432014-12-09 00:10:36 +090087static void stop_stream(struct snd_dice *dice, struct amdtp_stream *stream)
Takashi Sakamoto6eb6c812014-11-29 00:59:14 +090088{
Takashi Sakamoto9a028432014-12-09 00:10:36 +090089 amdtp_stream_pcm_abort(stream);
90 amdtp_stream_stop(stream);
Takashi Sakamotoc50fb912014-11-29 00:59:15 +090091
Takashi Sakamoto9a028432014-12-09 00:10:36 +090092 if (stream == &dice->tx_stream)
93 release_resources(dice, &dice->tx_resources);
94 else
95 release_resources(dice, &dice->rx_resources);
Takashi Sakamoto288a8d02014-12-09 00:10:35 +090096}
97
Takashi Sakamoto9a028432014-12-09 00:10:36 +090098static int start_stream(struct snd_dice *dice, struct amdtp_stream *stream,
99 unsigned int rate)
Takashi Sakamoto288a8d02014-12-09 00:10:35 +0900100{
Takashi Sakamoto9a028432014-12-09 00:10:36 +0900101 struct fw_iso_resources *resources;
Takashi Sakamoto288a8d02014-12-09 00:10:35 +0900102 unsigned int i, mode, pcm_chs, midi_ports;
Takashi Sakamoto27ec83b2015-09-19 11:21:50 +0900103 bool double_pcm_frames;
Takashi Sakamoto288a8d02014-12-09 00:10:35 +0900104 int err;
105
106 err = snd_dice_stream_get_rate_mode(dice, rate, &mode);
107 if (err < 0)
108 goto end;
Takashi Sakamoto9a028432014-12-09 00:10:36 +0900109 if (stream == &dice->tx_stream) {
110 resources = &dice->tx_resources;
111 pcm_chs = dice->tx_channels[mode];
112 midi_ports = dice->tx_midi_ports[mode];
113 } else {
114 resources = &dice->rx_resources;
115 pcm_chs = dice->rx_channels[mode];
116 midi_ports = dice->rx_midi_ports[mode];
117 }
Takashi Sakamoto288a8d02014-12-09 00:10:35 +0900118
119 /*
120 * At 176.4/192.0 kHz, Dice has a quirk to transfer two PCM frames in
121 * one data block of AMDTP packet. Thus sampling transfer frequency is
122 * a half of PCM sampling frequency, i.e. PCM frames at 192.0 kHz are
123 * transferred on AMDTP packets at 96 kHz. Two successive samples of a
124 * channel are stored consecutively in the packet. This quirk is called
125 * as 'Dual Wire'.
126 * For this quirk, blocking mode is required and PCM buffer size should
127 * be aligned to SYT_INTERVAL.
128 */
Takashi Sakamoto27ec83b2015-09-19 11:21:50 +0900129 double_pcm_frames = mode > 1;
130 if (double_pcm_frames) {
Takashi Sakamoto288a8d02014-12-09 00:10:35 +0900131 rate /= 2;
132 pcm_chs *= 2;
Takashi Sakamoto288a8d02014-12-09 00:10:35 +0900133 }
134
Takashi Sakamoto51c29fd2015-09-19 11:21:56 +0900135 err = amdtp_am824_set_parameters(stream, rate, pcm_chs, midi_ports,
136 double_pcm_frames);
Takashi Sakamoto547e6312015-09-19 11:21:49 +0900137 if (err < 0)
138 goto end;
139
Takashi Sakamoto27ec83b2015-09-19 11:21:50 +0900140 if (double_pcm_frames) {
Takashi Sakamoto288a8d02014-12-09 00:10:35 +0900141 pcm_chs /= 2;
142
143 for (i = 0; i < pcm_chs; i++) {
Takashi Sakamotof65be912015-09-19 11:21:58 +0900144 amdtp_am824_set_pcm_position(stream, i, i * 2);
145 amdtp_am824_set_pcm_position(stream, i + pcm_chs,
146 i * 2 + 1);
Takashi Sakamoto288a8d02014-12-09 00:10:35 +0900147 }
148 }
149
Takashi Sakamoto9a028432014-12-09 00:10:36 +0900150 err = keep_resources(dice, resources,
151 amdtp_stream_get_max_payload(stream));
Takashi Sakamoto288a8d02014-12-09 00:10:35 +0900152 if (err < 0) {
153 dev_err(&dice->unit->device,
154 "fail to keep isochronous resources\n");
155 goto end;
156 }
157
Takashi Sakamoto9a028432014-12-09 00:10:36 +0900158 err = amdtp_stream_start(stream, resources->channel,
Takashi Sakamoto288a8d02014-12-09 00:10:35 +0900159 fw_parent_device(dice->unit)->max_speed);
160 if (err < 0)
Takashi Sakamoto9a028432014-12-09 00:10:36 +0900161 release_resources(dice, resources);
Takashi Sakamoto288a8d02014-12-09 00:10:35 +0900162end:
163 return err;
164}
165
Takashi Sakamoto9a028432014-12-09 00:10:36 +0900166static int get_sync_mode(struct snd_dice *dice, enum cip_flags *sync_mode)
Takashi Sakamoto288a8d02014-12-09 00:10:35 +0900167{
Takashi Sakamoto8fc01fc2014-12-09 00:10:37 +0900168 u32 source;
169 int err;
170
171 err = snd_dice_transaction_get_clock_source(dice, &source);
172 if (err < 0)
173 goto end;
174
175 switch (source) {
176 /* So-called 'SYT Match' modes, sync_to_syt value of packets received */
177 case CLOCK_SOURCE_ARX4: /* in 4th stream */
178 case CLOCK_SOURCE_ARX3: /* in 3rd stream */
179 case CLOCK_SOURCE_ARX2: /* in 2nd stream */
180 err = -ENOSYS;
181 break;
182 case CLOCK_SOURCE_ARX1: /* in 1st stream, which this driver uses */
183 *sync_mode = 0;
184 break;
185 default:
186 *sync_mode = CIP_SYNC_TO_DEVICE;
187 break;
188 }
189end:
190 return err;
Takashi Sakamoto9a028432014-12-09 00:10:36 +0900191}
192
193int snd_dice_stream_start_duplex(struct snd_dice *dice, unsigned int rate)
194{
195 struct amdtp_stream *master, *slave;
Takashi Sakamoto288a8d02014-12-09 00:10:35 +0900196 unsigned int curr_rate;
Takashi Sakamoto9a028432014-12-09 00:10:36 +0900197 enum cip_flags sync_mode;
198 int err = 0;
199
200 if (dice->substreams_counter == 0)
201 goto end;
202
203 err = get_sync_mode(dice, &sync_mode);
204 if (err < 0)
205 goto end;
206 if (sync_mode == CIP_SYNC_TO_DEVICE) {
207 master = &dice->tx_stream;
208 slave = &dice->rx_stream;
209 } else {
210 master = &dice->rx_stream;
211 slave = &dice->tx_stream;
212 }
Takashi Sakamoto288a8d02014-12-09 00:10:35 +0900213
214 /* Some packet queueing errors. */
Takashi Sakamoto9a028432014-12-09 00:10:36 +0900215 if (amdtp_streaming_error(master) || amdtp_streaming_error(slave))
216 stop_stream(dice, master);
Takashi Sakamoto288a8d02014-12-09 00:10:35 +0900217
218 /* Stop stream if rate is different. */
219 err = snd_dice_transaction_get_rate(dice, &curr_rate);
220 if (err < 0) {
221 dev_err(&dice->unit->device,
222 "fail to get sampling rate\n");
223 goto end;
224 }
Takashi Sakamotoa113ff82014-12-09 00:10:39 +0900225 if (rate == 0)
226 rate = curr_rate;
Takashi Sakamoto288a8d02014-12-09 00:10:35 +0900227 if (rate != curr_rate)
Takashi Sakamoto9a028432014-12-09 00:10:36 +0900228 stop_stream(dice, master);
Takashi Sakamoto288a8d02014-12-09 00:10:35 +0900229
Takashi Sakamoto9a028432014-12-09 00:10:36 +0900230 if (!amdtp_stream_running(master)) {
231 stop_stream(dice, slave);
Takashi Sakamoto288a8d02014-12-09 00:10:35 +0900232 snd_dice_transaction_clear_enable(dice);
233
Takashi Sakamoto9a028432014-12-09 00:10:36 +0900234 amdtp_stream_set_sync(sync_mode, master, slave);
235
Takashi Sakamoto288a8d02014-12-09 00:10:35 +0900236 err = snd_dice_transaction_set_rate(dice, rate);
237 if (err < 0) {
238 dev_err(&dice->unit->device,
239 "fail to set sampling rate\n");
240 goto end;
241 }
242
Takashi Sakamoto9a028432014-12-09 00:10:36 +0900243 /* Start both streams. */
244 err = start_stream(dice, master, rate);
Takashi Sakamoto288a8d02014-12-09 00:10:35 +0900245 if (err < 0) {
246 dev_err(&dice->unit->device,
Takashi Sakamoto9a028432014-12-09 00:10:36 +0900247 "fail to start AMDTP master stream\n");
248 goto end;
249 }
250 err = start_stream(dice, slave, rate);
251 if (err < 0) {
252 dev_err(&dice->unit->device,
253 "fail to start AMDTP slave stream\n");
254 stop_stream(dice, master);
Takashi Sakamoto288a8d02014-12-09 00:10:35 +0900255 goto end;
256 }
257 err = snd_dice_transaction_set_enable(dice);
258 if (err < 0) {
259 dev_err(&dice->unit->device,
260 "fail to enable interface\n");
Takashi Sakamoto9a028432014-12-09 00:10:36 +0900261 stop_stream(dice, master);
262 stop_stream(dice, slave);
Takashi Sakamoto288a8d02014-12-09 00:10:35 +0900263 goto end;
264 }
265
Takashi Sakamoto9a028432014-12-09 00:10:36 +0900266 /* Wait first callbacks */
267 if (!amdtp_stream_wait_callback(master, CALLBACK_TIMEOUT) ||
268 !amdtp_stream_wait_callback(slave, CALLBACK_TIMEOUT)) {
Takashi Sakamoto288a8d02014-12-09 00:10:35 +0900269 snd_dice_transaction_clear_enable(dice);
Takashi Sakamoto9a028432014-12-09 00:10:36 +0900270 stop_stream(dice, master);
271 stop_stream(dice, slave);
Takashi Sakamoto288a8d02014-12-09 00:10:35 +0900272 err = -ETIMEDOUT;
273 }
274 }
275end:
276 return err;
Takashi Sakamoto6eb6c812014-11-29 00:59:14 +0900277}
278
Takashi Sakamoto9a028432014-12-09 00:10:36 +0900279void snd_dice_stream_stop_duplex(struct snd_dice *dice)
Takashi Sakamoto6eb6c812014-11-29 00:59:14 +0900280{
Takashi Sakamoto9a028432014-12-09 00:10:36 +0900281 if (dice->substreams_counter > 0)
282 return;
283
Takashi Sakamoto288a8d02014-12-09 00:10:35 +0900284 snd_dice_transaction_clear_enable(dice);
Takashi Sakamoto9a028432014-12-09 00:10:36 +0900285
286 stop_stream(dice, &dice->tx_stream);
287 stop_stream(dice, &dice->rx_stream);
Takashi Sakamoto6eb6c812014-11-29 00:59:14 +0900288}
289
Takashi Sakamoto9a028432014-12-09 00:10:36 +0900290static int init_stream(struct snd_dice *dice, struct amdtp_stream *stream)
291{
292 int err;
293 struct fw_iso_resources *resources;
294 enum amdtp_stream_direction dir;
295
296 if (stream == &dice->tx_stream) {
297 resources = &dice->tx_resources;
298 dir = AMDTP_IN_STREAM;
299 } else {
300 resources = &dice->rx_resources;
301 dir = AMDTP_OUT_STREAM;
302 }
303
304 err = fw_iso_resources_init(resources, dice->unit);
305 if (err < 0)
306 goto end;
307 resources->channels_mask = 0x00000000ffffffffuLL;
308
Takashi Sakamoto59558152015-09-19 11:21:55 +0900309 err = amdtp_am824_init(stream, dice->unit, dir, CIP_BLOCKING);
Takashi Sakamoto9a028432014-12-09 00:10:36 +0900310 if (err < 0) {
311 amdtp_stream_destroy(stream);
312 fw_iso_resources_destroy(resources);
313 }
314end:
315 return err;
316}
317
Takashi Sakamotod23c2cc2015-02-21 23:54:59 +0900318/*
319 * This function should be called before starting streams or after stopping
320 * streams.
321 */
Takashi Sakamoto9a028432014-12-09 00:10:36 +0900322static void destroy_stream(struct snd_dice *dice, struct amdtp_stream *stream)
323{
Takashi Sakamotod23c2cc2015-02-21 23:54:59 +0900324 struct fw_iso_resources *resources;
Takashi Sakamoto9a028432014-12-09 00:10:36 +0900325
326 if (stream == &dice->tx_stream)
Takashi Sakamotod23c2cc2015-02-21 23:54:59 +0900327 resources = &dice->tx_resources;
Takashi Sakamoto9a028432014-12-09 00:10:36 +0900328 else
Takashi Sakamotod23c2cc2015-02-21 23:54:59 +0900329 resources = &dice->rx_resources;
330
331 amdtp_stream_destroy(stream);
332 fw_iso_resources_destroy(resources);
Takashi Sakamoto9a028432014-12-09 00:10:36 +0900333}
334
335int snd_dice_stream_init_duplex(struct snd_dice *dice)
Takashi Sakamoto6eb6c812014-11-29 00:59:14 +0900336{
337 int err;
338
Takashi Sakamoto9a028432014-12-09 00:10:36 +0900339 dice->substreams_counter = 0;
340
341 err = init_stream(dice, &dice->tx_stream);
Takashi Sakamoto6eb6c812014-11-29 00:59:14 +0900342 if (err < 0)
343 goto end;
Takashi Sakamoto6eb6c812014-11-29 00:59:14 +0900344
Takashi Sakamoto9a028432014-12-09 00:10:36 +0900345 err = init_stream(dice, &dice->rx_stream);
Takashi Sakamotod23c2cc2015-02-21 23:54:59 +0900346 if (err < 0)
347 destroy_stream(dice, &dice->tx_stream);
Takashi Sakamoto6eb6c812014-11-29 00:59:14 +0900348end:
349 return err;
Takashi Sakamoto6eb6c812014-11-29 00:59:14 +0900350}
351
Takashi Sakamoto9a028432014-12-09 00:10:36 +0900352void snd_dice_stream_destroy_duplex(struct snd_dice *dice)
Takashi Sakamoto6eb6c812014-11-29 00:59:14 +0900353{
Takashi Sakamoto288a8d02014-12-09 00:10:35 +0900354 snd_dice_transaction_clear_enable(dice);
Takashi Sakamoto9a028432014-12-09 00:10:36 +0900355
Takashi Sakamoto9a028432014-12-09 00:10:36 +0900356 destroy_stream(dice, &dice->tx_stream);
Takashi Sakamoto9a028432014-12-09 00:10:36 +0900357 destroy_stream(dice, &dice->rx_stream);
358
359 dice->substreams_counter = 0;
Takashi Sakamoto6eb6c812014-11-29 00:59:14 +0900360}
361
Takashi Sakamoto9a028432014-12-09 00:10:36 +0900362void snd_dice_stream_update_duplex(struct snd_dice *dice)
Takashi Sakamoto6eb6c812014-11-29 00:59:14 +0900363{
364 /*
365 * On a bus reset, the DICE firmware disables streaming and then goes
366 * off contemplating its own navel for hundreds of milliseconds before
367 * it can react to any of our attempts to reenable streaming. This
368 * means that we lose synchronization anyway, so we force our streams
369 * to stop so that the application can restart them in an orderly
370 * manner.
371 */
372 dice->global_enabled = false;
373
Takashi Sakamoto9a028432014-12-09 00:10:36 +0900374 stop_stream(dice, &dice->rx_stream);
375 stop_stream(dice, &dice->tx_stream);
Takashi Sakamoto288a8d02014-12-09 00:10:35 +0900376
Takashi Sakamoto6eb6c812014-11-29 00:59:14 +0900377 fw_iso_resources_update(&dice->rx_resources);
Takashi Sakamoto9a028432014-12-09 00:10:36 +0900378 fw_iso_resources_update(&dice->tx_resources);
Takashi Sakamoto6eb6c812014-11-29 00:59:14 +0900379}
380
381static void dice_lock_changed(struct snd_dice *dice)
382{
383 dice->dev_lock_changed = true;
384 wake_up(&dice->hwdep_wait);
385}
386
387int snd_dice_stream_lock_try(struct snd_dice *dice)
388{
389 int err;
390
391 spin_lock_irq(&dice->lock);
392
393 if (dice->dev_lock_count < 0) {
394 err = -EBUSY;
395 goto out;
396 }
397
398 if (dice->dev_lock_count++ == 0)
399 dice_lock_changed(dice);
400 err = 0;
401out:
402 spin_unlock_irq(&dice->lock);
403 return err;
404}
405
406void snd_dice_stream_lock_release(struct snd_dice *dice)
407{
408 spin_lock_irq(&dice->lock);
409
410 if (WARN_ON(dice->dev_lock_count <= 0))
411 goto out;
412
413 if (--dice->dev_lock_count == 0)
414 dice_lock_changed(dice);
415out:
416 spin_unlock_irq(&dice->lock);
417}