blob: e4938b0cddbea818d436906a5fd5615516e74ce5 [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
Takashi Sakamotodfabc0e2016-02-08 22:54:20 +090013#define NOTIFICATION_TIMEOUT_MS (2 * MSEC_PER_SEC)
Takashi Sakamoto288a8d02014-12-09 00:10:35 +090014
Takashi Sakamoto6eb6c812014-11-29 00:59:14 +090015const unsigned int snd_dice_rates[SND_DICE_RATES_COUNT] = {
16 /* mode 0 */
17 [0] = 32000,
18 [1] = 44100,
19 [2] = 48000,
20 /* mode 1 */
21 [3] = 88200,
22 [4] = 96000,
23 /* mode 2 */
24 [5] = 176400,
25 [6] = 192000,
26};
27
Takashi Sakamotodfabc0e2016-02-08 22:54:20 +090028/*
29 * This operation has an effect to synchronize GLOBAL_STATUS/GLOBAL_SAMPLE_RATE
30 * to GLOBAL_STATUS. Especially, just after powering on, these are different.
31 */
32static int ensure_phase_lock(struct snd_dice *dice)
33{
34 __be32 reg;
35 int err;
36
37 err = snd_dice_transaction_read_global(dice, GLOBAL_CLOCK_SELECT,
38 &reg, sizeof(reg));
39 if (err < 0)
40 return err;
41
42 if (completion_done(&dice->clock_accepted))
43 reinit_completion(&dice->clock_accepted);
44
45 err = snd_dice_transaction_write_global(dice, GLOBAL_CLOCK_SELECT,
46 &reg, sizeof(reg));
47 if (err < 0)
48 return err;
49
50 if (wait_for_completion_timeout(&dice->clock_accepted,
51 msecs_to_jiffies(NOTIFICATION_TIMEOUT_MS)) == 0)
52 return -ETIMEDOUT;
53
54 return 0;
55}
56
Takashi Sakamoto9a028432014-12-09 00:10:36 +090057static void release_resources(struct snd_dice *dice,
58 struct fw_iso_resources *resources)
Takashi Sakamoto6eb6c812014-11-29 00:59:14 +090059{
Takashi Sakamoto3e93d422015-10-18 22:39:49 +090060 __be32 channel;
Takashi Sakamoto6eb6c812014-11-29 00:59:14 +090061
Takashi Sakamoto288a8d02014-12-09 00:10:35 +090062 /* Reset channel number */
Takashi Sakamoto6eb6c812014-11-29 00:59:14 +090063 channel = cpu_to_be32((u32)-1);
Takashi Sakamoto9a028432014-12-09 00:10:36 +090064 if (resources == &dice->tx_resources)
65 snd_dice_transaction_write_tx(dice, TX_ISOCHRONOUS,
Takashi Sakamoto3e93d422015-10-18 22:39:49 +090066 &channel, sizeof(channel));
Takashi Sakamoto9a028432014-12-09 00:10:36 +090067 else
68 snd_dice_transaction_write_rx(dice, RX_ISOCHRONOUS,
Takashi Sakamoto3e93d422015-10-18 22:39:49 +090069 &channel, sizeof(channel));
Takashi Sakamoto288a8d02014-12-09 00:10:35 +090070
Takashi Sakamoto9a028432014-12-09 00:10:36 +090071 fw_iso_resources_free(resources);
Takashi Sakamoto288a8d02014-12-09 00:10:35 +090072}
73
Takashi Sakamoto9a028432014-12-09 00:10:36 +090074static int keep_resources(struct snd_dice *dice,
75 struct fw_iso_resources *resources,
76 unsigned int max_payload_bytes)
Takashi Sakamoto288a8d02014-12-09 00:10:35 +090077{
Takashi Sakamoto3e93d422015-10-18 22:39:49 +090078 __be32 channel;
Takashi Sakamoto288a8d02014-12-09 00:10:35 +090079 int err;
80
Takashi Sakamoto9a028432014-12-09 00:10:36 +090081 err = fw_iso_resources_allocate(resources, max_payload_bytes,
Takashi Sakamoto288a8d02014-12-09 00:10:35 +090082 fw_parent_device(dice->unit)->max_speed);
83 if (err < 0)
84 goto end;
85
86 /* Set channel number */
Takashi Sakamoto9a028432014-12-09 00:10:36 +090087 channel = cpu_to_be32(resources->channel);
88 if (resources == &dice->tx_resources)
89 err = snd_dice_transaction_write_tx(dice, TX_ISOCHRONOUS,
Takashi Sakamoto3e93d422015-10-18 22:39:49 +090090 &channel, sizeof(channel));
Takashi Sakamoto9a028432014-12-09 00:10:36 +090091 else
92 err = snd_dice_transaction_write_rx(dice, RX_ISOCHRONOUS,
Takashi Sakamoto3e93d422015-10-18 22:39:49 +090093 &channel, sizeof(channel));
Takashi Sakamoto288a8d02014-12-09 00:10:35 +090094 if (err < 0)
Takashi Sakamoto9a028432014-12-09 00:10:36 +090095 release_resources(dice, resources);
Takashi Sakamoto288a8d02014-12-09 00:10:35 +090096end:
Takashi Sakamoto6eb6c812014-11-29 00:59:14 +090097 return err;
98}
99
Takashi Sakamoto9a028432014-12-09 00:10:36 +0900100static void stop_stream(struct snd_dice *dice, struct amdtp_stream *stream)
Takashi Sakamoto6eb6c812014-11-29 00:59:14 +0900101{
Takashi Sakamoto9a028432014-12-09 00:10:36 +0900102 amdtp_stream_pcm_abort(stream);
103 amdtp_stream_stop(stream);
Takashi Sakamotoc50fb912014-11-29 00:59:15 +0900104
Takashi Sakamoto9a028432014-12-09 00:10:36 +0900105 if (stream == &dice->tx_stream)
106 release_resources(dice, &dice->tx_resources);
107 else
108 release_resources(dice, &dice->rx_resources);
Takashi Sakamoto288a8d02014-12-09 00:10:35 +0900109}
110
Takashi Sakamoto9a028432014-12-09 00:10:36 +0900111static int start_stream(struct snd_dice *dice, struct amdtp_stream *stream,
112 unsigned int rate)
Takashi Sakamoto288a8d02014-12-09 00:10:35 +0900113{
Takashi Sakamoto9a028432014-12-09 00:10:36 +0900114 struct fw_iso_resources *resources;
Takashi Sakamoto1bc8e122016-02-08 22:54:16 +0900115 __be32 reg[2];
Takashi Sakamoto6f688262016-02-08 22:54:19 +0900116 unsigned int i, pcm_chs, midi_ports;
Takashi Sakamoto27ec83b2015-09-19 11:21:50 +0900117 bool double_pcm_frames;
Takashi Sakamoto288a8d02014-12-09 00:10:35 +0900118 int err;
119
Takashi Sakamoto9a028432014-12-09 00:10:36 +0900120 if (stream == &dice->tx_stream) {
121 resources = &dice->tx_resources;
Takashi Sakamoto1bc8e122016-02-08 22:54:16 +0900122 err = snd_dice_transaction_read_tx(dice, TX_NUMBER_AUDIO,
123 reg, sizeof(reg));
Takashi Sakamoto9a028432014-12-09 00:10:36 +0900124 } else {
125 resources = &dice->rx_resources;
Takashi Sakamoto1bc8e122016-02-08 22:54:16 +0900126 err = snd_dice_transaction_read_rx(dice, RX_NUMBER_AUDIO,
127 reg, sizeof(reg));
Takashi Sakamoto9a028432014-12-09 00:10:36 +0900128 }
Takashi Sakamoto288a8d02014-12-09 00:10:35 +0900129
Takashi Sakamoto1bc8e122016-02-08 22:54:16 +0900130 if (err < 0)
131 goto end;
132
133 pcm_chs = be32_to_cpu(reg[0]);
134 midi_ports = be32_to_cpu(reg[1]);
135
Takashi Sakamoto288a8d02014-12-09 00:10:35 +0900136 /*
137 * At 176.4/192.0 kHz, Dice has a quirk to transfer two PCM frames in
138 * one data block of AMDTP packet. Thus sampling transfer frequency is
139 * a half of PCM sampling frequency, i.e. PCM frames at 192.0 kHz are
140 * transferred on AMDTP packets at 96 kHz. Two successive samples of a
141 * channel are stored consecutively in the packet. This quirk is called
142 * as 'Dual Wire'.
143 * For this quirk, blocking mode is required and PCM buffer size should
144 * be aligned to SYT_INTERVAL.
145 */
Takashi Sakamoto6f688262016-02-08 22:54:19 +0900146 double_pcm_frames = rate > 96000;
Takashi Sakamoto27ec83b2015-09-19 11:21:50 +0900147 if (double_pcm_frames) {
Takashi Sakamoto288a8d02014-12-09 00:10:35 +0900148 rate /= 2;
149 pcm_chs *= 2;
Takashi Sakamoto288a8d02014-12-09 00:10:35 +0900150 }
151
Takashi Sakamoto51c29fd2015-09-19 11:21:56 +0900152 err = amdtp_am824_set_parameters(stream, rate, pcm_chs, midi_ports,
153 double_pcm_frames);
Takashi Sakamoto547e6312015-09-19 11:21:49 +0900154 if (err < 0)
155 goto end;
156
Takashi Sakamoto27ec83b2015-09-19 11:21:50 +0900157 if (double_pcm_frames) {
Takashi Sakamoto288a8d02014-12-09 00:10:35 +0900158 pcm_chs /= 2;
159
160 for (i = 0; i < pcm_chs; i++) {
Takashi Sakamotof65be912015-09-19 11:21:58 +0900161 amdtp_am824_set_pcm_position(stream, i, i * 2);
162 amdtp_am824_set_pcm_position(stream, i + pcm_chs,
163 i * 2 + 1);
Takashi Sakamoto288a8d02014-12-09 00:10:35 +0900164 }
165 }
166
Takashi Sakamoto9a028432014-12-09 00:10:36 +0900167 err = keep_resources(dice, resources,
168 amdtp_stream_get_max_payload(stream));
Takashi Sakamoto288a8d02014-12-09 00:10:35 +0900169 if (err < 0) {
170 dev_err(&dice->unit->device,
171 "fail to keep isochronous resources\n");
172 goto end;
173 }
174
Takashi Sakamoto9a028432014-12-09 00:10:36 +0900175 err = amdtp_stream_start(stream, resources->channel,
Takashi Sakamoto288a8d02014-12-09 00:10:35 +0900176 fw_parent_device(dice->unit)->max_speed);
177 if (err < 0)
Takashi Sakamoto9a028432014-12-09 00:10:36 +0900178 release_resources(dice, resources);
Takashi Sakamoto288a8d02014-12-09 00:10:35 +0900179end:
180 return err;
181}
182
Takashi Sakamoto9a028432014-12-09 00:10:36 +0900183static int get_sync_mode(struct snd_dice *dice, enum cip_flags *sync_mode)
Takashi Sakamoto288a8d02014-12-09 00:10:35 +0900184{
Takashi Sakamoto8fc01fc2014-12-09 00:10:37 +0900185 u32 source;
186 int err;
187
188 err = snd_dice_transaction_get_clock_source(dice, &source);
189 if (err < 0)
190 goto end;
191
192 switch (source) {
193 /* So-called 'SYT Match' modes, sync_to_syt value of packets received */
194 case CLOCK_SOURCE_ARX4: /* in 4th stream */
195 case CLOCK_SOURCE_ARX3: /* in 3rd stream */
196 case CLOCK_SOURCE_ARX2: /* in 2nd stream */
197 err = -ENOSYS;
198 break;
199 case CLOCK_SOURCE_ARX1: /* in 1st stream, which this driver uses */
200 *sync_mode = 0;
201 break;
202 default:
203 *sync_mode = CIP_SYNC_TO_DEVICE;
204 break;
205 }
206end:
207 return err;
Takashi Sakamoto9a028432014-12-09 00:10:36 +0900208}
209
210int snd_dice_stream_start_duplex(struct snd_dice *dice, unsigned int rate)
211{
212 struct amdtp_stream *master, *slave;
Takashi Sakamoto288a8d02014-12-09 00:10:35 +0900213 unsigned int curr_rate;
Takashi Sakamoto9a028432014-12-09 00:10:36 +0900214 enum cip_flags sync_mode;
215 int err = 0;
216
217 if (dice->substreams_counter == 0)
218 goto end;
219
220 err = get_sync_mode(dice, &sync_mode);
221 if (err < 0)
222 goto end;
223 if (sync_mode == CIP_SYNC_TO_DEVICE) {
224 master = &dice->tx_stream;
225 slave = &dice->rx_stream;
226 } else {
227 master = &dice->rx_stream;
228 slave = &dice->tx_stream;
229 }
Takashi Sakamoto288a8d02014-12-09 00:10:35 +0900230
231 /* Some packet queueing errors. */
Takashi Sakamoto9a028432014-12-09 00:10:36 +0900232 if (amdtp_streaming_error(master) || amdtp_streaming_error(slave))
233 stop_stream(dice, master);
Takashi Sakamoto288a8d02014-12-09 00:10:35 +0900234
235 /* Stop stream if rate is different. */
236 err = snd_dice_transaction_get_rate(dice, &curr_rate);
237 if (err < 0) {
238 dev_err(&dice->unit->device,
239 "fail to get sampling rate\n");
240 goto end;
241 }
Takashi Sakamotoa113ff82014-12-09 00:10:39 +0900242 if (rate == 0)
243 rate = curr_rate;
Takashi Sakamoto1bc8e122016-02-08 22:54:16 +0900244 if (rate != curr_rate) {
245 err = -EINVAL;
246 goto end;
247 }
Takashi Sakamoto288a8d02014-12-09 00:10:35 +0900248
Takashi Sakamoto9a028432014-12-09 00:10:36 +0900249 if (!amdtp_stream_running(master)) {
250 stop_stream(dice, slave);
Takashi Sakamoto288a8d02014-12-09 00:10:35 +0900251 snd_dice_transaction_clear_enable(dice);
252
Takashi Sakamoto9a028432014-12-09 00:10:36 +0900253 amdtp_stream_set_sync(sync_mode, master, slave);
254
Takashi Sakamotodfabc0e2016-02-08 22:54:20 +0900255 err = ensure_phase_lock(dice);
Takashi Sakamoto288a8d02014-12-09 00:10:35 +0900256 if (err < 0) {
257 dev_err(&dice->unit->device,
Takashi Sakamotodfabc0e2016-02-08 22:54:20 +0900258 "fail to ensure phase lock\n");
Takashi Sakamoto288a8d02014-12-09 00:10:35 +0900259 goto end;
260 }
261
Takashi Sakamoto9a028432014-12-09 00:10:36 +0900262 /* Start both streams. */
263 err = start_stream(dice, master, rate);
Takashi Sakamoto288a8d02014-12-09 00:10:35 +0900264 if (err < 0) {
265 dev_err(&dice->unit->device,
Takashi Sakamoto9a028432014-12-09 00:10:36 +0900266 "fail to start AMDTP master stream\n");
267 goto end;
268 }
269 err = start_stream(dice, slave, rate);
270 if (err < 0) {
271 dev_err(&dice->unit->device,
272 "fail to start AMDTP slave stream\n");
273 stop_stream(dice, master);
Takashi Sakamoto288a8d02014-12-09 00:10:35 +0900274 goto end;
275 }
276 err = snd_dice_transaction_set_enable(dice);
277 if (err < 0) {
278 dev_err(&dice->unit->device,
279 "fail to enable interface\n");
Takashi Sakamoto9a028432014-12-09 00:10:36 +0900280 stop_stream(dice, master);
281 stop_stream(dice, slave);
Takashi Sakamoto288a8d02014-12-09 00:10:35 +0900282 goto end;
283 }
284
Takashi Sakamoto9a028432014-12-09 00:10:36 +0900285 /* Wait first callbacks */
286 if (!amdtp_stream_wait_callback(master, CALLBACK_TIMEOUT) ||
287 !amdtp_stream_wait_callback(slave, CALLBACK_TIMEOUT)) {
Takashi Sakamoto288a8d02014-12-09 00:10:35 +0900288 snd_dice_transaction_clear_enable(dice);
Takashi Sakamoto9a028432014-12-09 00:10:36 +0900289 stop_stream(dice, master);
290 stop_stream(dice, slave);
Takashi Sakamoto288a8d02014-12-09 00:10:35 +0900291 err = -ETIMEDOUT;
292 }
293 }
294end:
295 return err;
Takashi Sakamoto6eb6c812014-11-29 00:59:14 +0900296}
297
Takashi Sakamoto9a028432014-12-09 00:10:36 +0900298void snd_dice_stream_stop_duplex(struct snd_dice *dice)
Takashi Sakamoto6eb6c812014-11-29 00:59:14 +0900299{
Takashi Sakamoto9a028432014-12-09 00:10:36 +0900300 if (dice->substreams_counter > 0)
301 return;
302
Takashi Sakamoto288a8d02014-12-09 00:10:35 +0900303 snd_dice_transaction_clear_enable(dice);
Takashi Sakamoto9a028432014-12-09 00:10:36 +0900304
305 stop_stream(dice, &dice->tx_stream);
306 stop_stream(dice, &dice->rx_stream);
Takashi Sakamoto6eb6c812014-11-29 00:59:14 +0900307}
308
Takashi Sakamoto9a028432014-12-09 00:10:36 +0900309static int init_stream(struct snd_dice *dice, struct amdtp_stream *stream)
310{
311 int err;
312 struct fw_iso_resources *resources;
313 enum amdtp_stream_direction dir;
314
315 if (stream == &dice->tx_stream) {
316 resources = &dice->tx_resources;
317 dir = AMDTP_IN_STREAM;
318 } else {
319 resources = &dice->rx_resources;
320 dir = AMDTP_OUT_STREAM;
321 }
322
323 err = fw_iso_resources_init(resources, dice->unit);
324 if (err < 0)
325 goto end;
326 resources->channels_mask = 0x00000000ffffffffuLL;
327
Takashi Sakamoto59558152015-09-19 11:21:55 +0900328 err = amdtp_am824_init(stream, dice->unit, dir, CIP_BLOCKING);
Takashi Sakamoto9a028432014-12-09 00:10:36 +0900329 if (err < 0) {
330 amdtp_stream_destroy(stream);
331 fw_iso_resources_destroy(resources);
332 }
333end:
334 return err;
335}
336
Takashi Sakamotod23c2cc2015-02-21 23:54:59 +0900337/*
338 * This function should be called before starting streams or after stopping
339 * streams.
340 */
Takashi Sakamoto9a028432014-12-09 00:10:36 +0900341static void destroy_stream(struct snd_dice *dice, struct amdtp_stream *stream)
342{
Takashi Sakamotod23c2cc2015-02-21 23:54:59 +0900343 struct fw_iso_resources *resources;
Takashi Sakamoto9a028432014-12-09 00:10:36 +0900344
345 if (stream == &dice->tx_stream)
Takashi Sakamotod23c2cc2015-02-21 23:54:59 +0900346 resources = &dice->tx_resources;
Takashi Sakamoto9a028432014-12-09 00:10:36 +0900347 else
Takashi Sakamotod23c2cc2015-02-21 23:54:59 +0900348 resources = &dice->rx_resources;
349
350 amdtp_stream_destroy(stream);
351 fw_iso_resources_destroy(resources);
Takashi Sakamoto9a028432014-12-09 00:10:36 +0900352}
353
354int snd_dice_stream_init_duplex(struct snd_dice *dice)
Takashi Sakamoto6eb6c812014-11-29 00:59:14 +0900355{
356 int err;
357
Takashi Sakamoto9a028432014-12-09 00:10:36 +0900358 dice->substreams_counter = 0;
359
360 err = init_stream(dice, &dice->tx_stream);
Takashi Sakamoto6eb6c812014-11-29 00:59:14 +0900361 if (err < 0)
362 goto end;
Takashi Sakamoto6eb6c812014-11-29 00:59:14 +0900363
Takashi Sakamoto9a028432014-12-09 00:10:36 +0900364 err = init_stream(dice, &dice->rx_stream);
Takashi Sakamotod23c2cc2015-02-21 23:54:59 +0900365 if (err < 0)
366 destroy_stream(dice, &dice->tx_stream);
Takashi Sakamoto6eb6c812014-11-29 00:59:14 +0900367end:
368 return err;
Takashi Sakamoto6eb6c812014-11-29 00:59:14 +0900369}
370
Takashi Sakamoto9a028432014-12-09 00:10:36 +0900371void snd_dice_stream_destroy_duplex(struct snd_dice *dice)
Takashi Sakamoto6eb6c812014-11-29 00:59:14 +0900372{
Takashi Sakamoto288a8d02014-12-09 00:10:35 +0900373 snd_dice_transaction_clear_enable(dice);
Takashi Sakamoto9a028432014-12-09 00:10:36 +0900374
Takashi Sakamoto9a028432014-12-09 00:10:36 +0900375 destroy_stream(dice, &dice->tx_stream);
Takashi Sakamoto9a028432014-12-09 00:10:36 +0900376 destroy_stream(dice, &dice->rx_stream);
377
378 dice->substreams_counter = 0;
Takashi Sakamoto6eb6c812014-11-29 00:59:14 +0900379}
380
Takashi Sakamoto9a028432014-12-09 00:10:36 +0900381void snd_dice_stream_update_duplex(struct snd_dice *dice)
Takashi Sakamoto6eb6c812014-11-29 00:59:14 +0900382{
383 /*
384 * On a bus reset, the DICE firmware disables streaming and then goes
385 * off contemplating its own navel for hundreds of milliseconds before
386 * it can react to any of our attempts to reenable streaming. This
387 * means that we lose synchronization anyway, so we force our streams
388 * to stop so that the application can restart them in an orderly
389 * manner.
390 */
391 dice->global_enabled = false;
392
Takashi Sakamoto9a028432014-12-09 00:10:36 +0900393 stop_stream(dice, &dice->rx_stream);
394 stop_stream(dice, &dice->tx_stream);
Takashi Sakamoto288a8d02014-12-09 00:10:35 +0900395
Takashi Sakamoto6eb6c812014-11-29 00:59:14 +0900396 fw_iso_resources_update(&dice->rx_resources);
Takashi Sakamoto9a028432014-12-09 00:10:36 +0900397 fw_iso_resources_update(&dice->tx_resources);
Takashi Sakamoto6eb6c812014-11-29 00:59:14 +0900398}
399
400static void dice_lock_changed(struct snd_dice *dice)
401{
402 dice->dev_lock_changed = true;
403 wake_up(&dice->hwdep_wait);
404}
405
406int snd_dice_stream_lock_try(struct snd_dice *dice)
407{
408 int err;
409
410 spin_lock_irq(&dice->lock);
411
412 if (dice->dev_lock_count < 0) {
413 err = -EBUSY;
414 goto out;
415 }
416
417 if (dice->dev_lock_count++ == 0)
418 dice_lock_changed(dice);
419 err = 0;
420out:
421 spin_unlock_irq(&dice->lock);
422 return err;
423}
424
425void snd_dice_stream_lock_release(struct snd_dice *dice)
426{
427 spin_lock_irq(&dice->lock);
428
429 if (WARN_ON(dice->dev_lock_count <= 0))
430 goto out;
431
432 if (--dice->dev_lock_count == 0)
433 dice_lock_changed(dice);
434out:
435 spin_unlock_irq(&dice->lock);
436}