xref: /linux/sound/firewire/dice/dice-pcm.c (revision ebf68996de0ab250c5d520eb2291ab65643e9a1e)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * dice_pcm.c - a part of driver for DICE based devices
4  *
5  * Copyright (c) Clemens Ladisch <clemens@ladisch.de>
6  * Copyright (c) 2014 Takashi Sakamoto <o-takashi@sakamocchi.jp>
7  */
8 
9 #include "dice.h"
10 
11 static int dice_rate_constraint(struct snd_pcm_hw_params *params,
12 				struct snd_pcm_hw_rule *rule)
13 {
14 	struct snd_pcm_substream *substream = rule->private;
15 	struct snd_dice *dice = substream->private_data;
16 	unsigned int index = substream->pcm->device;
17 
18 	const struct snd_interval *c =
19 		hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_CHANNELS);
20 	struct snd_interval *r =
21 		hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
22 	struct snd_interval rates = {
23 		.min = UINT_MAX, .max = 0, .integer = 1
24 	};
25 	unsigned int *pcm_channels;
26 	enum snd_dice_rate_mode mode;
27 	unsigned int i, rate;
28 
29 	if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
30 		pcm_channels = dice->tx_pcm_chs[index];
31 	else
32 		pcm_channels = dice->rx_pcm_chs[index];
33 
34 	for (i = 0; i < ARRAY_SIZE(snd_dice_rates); ++i) {
35 		rate = snd_dice_rates[i];
36 		if (snd_dice_stream_get_rate_mode(dice, rate, &mode) < 0)
37 			continue;
38 
39 		if (!snd_interval_test(c, pcm_channels[mode]))
40 			continue;
41 
42 		rates.min = min(rates.min, rate);
43 		rates.max = max(rates.max, rate);
44 	}
45 
46 	return snd_interval_refine(r, &rates);
47 }
48 
49 static int dice_channels_constraint(struct snd_pcm_hw_params *params,
50 				    struct snd_pcm_hw_rule *rule)
51 {
52 	struct snd_pcm_substream *substream = rule->private;
53 	struct snd_dice *dice = substream->private_data;
54 	unsigned int index = substream->pcm->device;
55 
56 	const struct snd_interval *r =
57 		hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_RATE);
58 	struct snd_interval *c =
59 		hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
60 	struct snd_interval channels = {
61 		.min = UINT_MAX, .max = 0, .integer = 1
62 	};
63 	unsigned int *pcm_channels;
64 	enum snd_dice_rate_mode mode;
65 	unsigned int i, rate;
66 
67 	if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
68 		pcm_channels = dice->tx_pcm_chs[index];
69 	else
70 		pcm_channels = dice->rx_pcm_chs[index];
71 
72 	for (i = 0; i < ARRAY_SIZE(snd_dice_rates); ++i) {
73 		rate = snd_dice_rates[i];
74 		if (snd_dice_stream_get_rate_mode(dice, rate, &mode) < 0)
75 			continue;
76 
77 		if (!snd_interval_test(r, rate))
78 			continue;
79 
80 		channels.min = min(channels.min, pcm_channels[mode]);
81 		channels.max = max(channels.max, pcm_channels[mode]);
82 	}
83 
84 	return snd_interval_refine(c, &channels);
85 }
86 
87 static int limit_channels_and_rates(struct snd_dice *dice,
88 				    struct snd_pcm_runtime *runtime,
89 				    enum amdtp_stream_direction dir,
90 				    unsigned int index)
91 {
92 	struct snd_pcm_hardware *hw = &runtime->hw;
93 	unsigned int *pcm_channels;
94 	unsigned int i;
95 
96 	if (dir == AMDTP_IN_STREAM)
97 		pcm_channels = dice->tx_pcm_chs[index];
98 	else
99 		pcm_channels = dice->rx_pcm_chs[index];
100 
101 	hw->channels_min = UINT_MAX;
102 	hw->channels_max = 0;
103 
104 	for (i = 0; i < ARRAY_SIZE(snd_dice_rates); ++i) {
105 		enum snd_dice_rate_mode mode;
106 		unsigned int rate, channels;
107 
108 		rate = snd_dice_rates[i];
109 		if (snd_dice_stream_get_rate_mode(dice, rate, &mode) < 0)
110 			continue;
111 		hw->rates |= snd_pcm_rate_to_rate_bit(rate);
112 
113 		channels = pcm_channels[mode];
114 		if (channels == 0)
115 			continue;
116 		hw->channels_min = min(hw->channels_min, channels);
117 		hw->channels_max = max(hw->channels_max, channels);
118 	}
119 
120 	snd_pcm_limit_hw_rates(runtime);
121 
122 	return 0;
123 }
124 
125 static int init_hw_info(struct snd_dice *dice,
126 			struct snd_pcm_substream *substream)
127 {
128 	struct snd_pcm_runtime *runtime = substream->runtime;
129 	struct snd_pcm_hardware *hw = &runtime->hw;
130 	unsigned int index = substream->pcm->device;
131 	enum amdtp_stream_direction dir;
132 	struct amdtp_stream *stream;
133 	int err;
134 
135 	if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
136 		hw->formats = AM824_IN_PCM_FORMAT_BITS;
137 		dir = AMDTP_IN_STREAM;
138 		stream = &dice->tx_stream[index];
139 	} else {
140 		hw->formats = AM824_OUT_PCM_FORMAT_BITS;
141 		dir = AMDTP_OUT_STREAM;
142 		stream = &dice->rx_stream[index];
143 	}
144 
145 	err = limit_channels_and_rates(dice, substream->runtime, dir,
146 				       index);
147 	if (err < 0)
148 		return err;
149 
150 	err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
151 				  dice_rate_constraint, substream,
152 				  SNDRV_PCM_HW_PARAM_CHANNELS, -1);
153 	if (err < 0)
154 		return err;
155 	err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
156 				  dice_channels_constraint, substream,
157 				  SNDRV_PCM_HW_PARAM_RATE, -1);
158 	if (err < 0)
159 		return err;
160 
161 	return amdtp_am824_add_pcm_hw_constraints(stream, runtime);
162 }
163 
164 static int pcm_open(struct snd_pcm_substream *substream)
165 {
166 	struct snd_dice *dice = substream->private_data;
167 	unsigned int source;
168 	bool internal;
169 	int err;
170 
171 	err = snd_dice_stream_lock_try(dice);
172 	if (err < 0)
173 		goto end;
174 
175 	err = init_hw_info(dice, substream);
176 	if (err < 0)
177 		goto err_locked;
178 
179 	err = snd_dice_transaction_get_clock_source(dice, &source);
180 	if (err < 0)
181 		goto err_locked;
182 	switch (source) {
183 	case CLOCK_SOURCE_AES1:
184 	case CLOCK_SOURCE_AES2:
185 	case CLOCK_SOURCE_AES3:
186 	case CLOCK_SOURCE_AES4:
187 	case CLOCK_SOURCE_AES_ANY:
188 	case CLOCK_SOURCE_ADAT:
189 	case CLOCK_SOURCE_TDIF:
190 	case CLOCK_SOURCE_WC:
191 		internal = false;
192 		break;
193 	default:
194 		internal = true;
195 		break;
196 	}
197 
198 	/*
199 	 * When source of clock is not internal or any PCM streams are running,
200 	 * available sampling rate is limited at current sampling rate.
201 	 */
202 	if (!internal ||
203 	    amdtp_stream_pcm_running(&dice->tx_stream[0]) ||
204 	    amdtp_stream_pcm_running(&dice->tx_stream[1]) ||
205 	    amdtp_stream_pcm_running(&dice->rx_stream[0]) ||
206 	    amdtp_stream_pcm_running(&dice->rx_stream[1])) {
207 		unsigned int rate;
208 
209 		err = snd_dice_transaction_get_rate(dice, &rate);
210 		if (err < 0)
211 			goto err_locked;
212 		substream->runtime->hw.rate_min = rate;
213 		substream->runtime->hw.rate_max = rate;
214 	}
215 
216 	snd_pcm_set_sync(substream);
217 end:
218 	return err;
219 err_locked:
220 	snd_dice_stream_lock_release(dice);
221 	return err;
222 }
223 
224 static int pcm_close(struct snd_pcm_substream *substream)
225 {
226 	struct snd_dice *dice = substream->private_data;
227 
228 	snd_dice_stream_lock_release(dice);
229 
230 	return 0;
231 }
232 
233 static int capture_hw_params(struct snd_pcm_substream *substream,
234 			     struct snd_pcm_hw_params *hw_params)
235 {
236 	struct snd_dice *dice = substream->private_data;
237 	int err;
238 
239 	err = snd_pcm_lib_alloc_vmalloc_buffer(substream,
240 					       params_buffer_bytes(hw_params));
241 	if (err < 0)
242 		return err;
243 
244 	if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN) {
245 		mutex_lock(&dice->mutex);
246 		dice->substreams_counter++;
247 		mutex_unlock(&dice->mutex);
248 	}
249 
250 	return 0;
251 }
252 static int playback_hw_params(struct snd_pcm_substream *substream,
253 			      struct snd_pcm_hw_params *hw_params)
254 {
255 	struct snd_dice *dice = substream->private_data;
256 	int err;
257 
258 	err = snd_pcm_lib_alloc_vmalloc_buffer(substream,
259 					       params_buffer_bytes(hw_params));
260 	if (err < 0)
261 		return err;
262 
263 	if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN) {
264 		mutex_lock(&dice->mutex);
265 		dice->substreams_counter++;
266 		mutex_unlock(&dice->mutex);
267 	}
268 
269 	return 0;
270 }
271 
272 static int capture_hw_free(struct snd_pcm_substream *substream)
273 {
274 	struct snd_dice *dice = substream->private_data;
275 
276 	mutex_lock(&dice->mutex);
277 
278 	if (substream->runtime->status->state != SNDRV_PCM_STATE_OPEN)
279 		dice->substreams_counter--;
280 
281 	snd_dice_stream_stop_duplex(dice);
282 
283 	mutex_unlock(&dice->mutex);
284 
285 	return snd_pcm_lib_free_vmalloc_buffer(substream);
286 }
287 
288 static int playback_hw_free(struct snd_pcm_substream *substream)
289 {
290 	struct snd_dice *dice = substream->private_data;
291 
292 	mutex_lock(&dice->mutex);
293 
294 	if (substream->runtime->status->state != SNDRV_PCM_STATE_OPEN)
295 		dice->substreams_counter--;
296 
297 	snd_dice_stream_stop_duplex(dice);
298 
299 	mutex_unlock(&dice->mutex);
300 
301 	return snd_pcm_lib_free_vmalloc_buffer(substream);
302 }
303 
304 static int capture_prepare(struct snd_pcm_substream *substream)
305 {
306 	struct snd_dice *dice = substream->private_data;
307 	struct amdtp_stream *stream = &dice->tx_stream[substream->pcm->device];
308 	int err;
309 
310 	mutex_lock(&dice->mutex);
311 	err = snd_dice_stream_start_duplex(dice, substream->runtime->rate);
312 	mutex_unlock(&dice->mutex);
313 	if (err >= 0)
314 		amdtp_stream_pcm_prepare(stream);
315 
316 	return 0;
317 }
318 static int playback_prepare(struct snd_pcm_substream *substream)
319 {
320 	struct snd_dice *dice = substream->private_data;
321 	struct amdtp_stream *stream = &dice->rx_stream[substream->pcm->device];
322 	int err;
323 
324 	mutex_lock(&dice->mutex);
325 	err = snd_dice_stream_start_duplex(dice, substream->runtime->rate);
326 	mutex_unlock(&dice->mutex);
327 	if (err >= 0)
328 		amdtp_stream_pcm_prepare(stream);
329 
330 	return err;
331 }
332 
333 static int capture_trigger(struct snd_pcm_substream *substream, int cmd)
334 {
335 	struct snd_dice *dice = substream->private_data;
336 	struct amdtp_stream *stream = &dice->tx_stream[substream->pcm->device];
337 
338 	switch (cmd) {
339 	case SNDRV_PCM_TRIGGER_START:
340 		amdtp_stream_pcm_trigger(stream, substream);
341 		break;
342 	case SNDRV_PCM_TRIGGER_STOP:
343 		amdtp_stream_pcm_trigger(stream, NULL);
344 		break;
345 	default:
346 		return -EINVAL;
347 	}
348 
349 	return 0;
350 }
351 static int playback_trigger(struct snd_pcm_substream *substream, int cmd)
352 {
353 	struct snd_dice *dice = substream->private_data;
354 	struct amdtp_stream *stream = &dice->rx_stream[substream->pcm->device];
355 
356 	switch (cmd) {
357 	case SNDRV_PCM_TRIGGER_START:
358 		amdtp_stream_pcm_trigger(stream, substream);
359 		break;
360 	case SNDRV_PCM_TRIGGER_STOP:
361 		amdtp_stream_pcm_trigger(stream, NULL);
362 		break;
363 	default:
364 		return -EINVAL;
365 	}
366 
367 	return 0;
368 }
369 
370 static snd_pcm_uframes_t capture_pointer(struct snd_pcm_substream *substream)
371 {
372 	struct snd_dice *dice = substream->private_data;
373 	struct amdtp_stream *stream = &dice->tx_stream[substream->pcm->device];
374 
375 	return amdtp_stream_pcm_pointer(stream);
376 }
377 static snd_pcm_uframes_t playback_pointer(struct snd_pcm_substream *substream)
378 {
379 	struct snd_dice *dice = substream->private_data;
380 	struct amdtp_stream *stream = &dice->rx_stream[substream->pcm->device];
381 
382 	return amdtp_stream_pcm_pointer(stream);
383 }
384 
385 static int capture_ack(struct snd_pcm_substream *substream)
386 {
387 	struct snd_dice *dice = substream->private_data;
388 	struct amdtp_stream *stream = &dice->tx_stream[substream->pcm->device];
389 
390 	return amdtp_stream_pcm_ack(stream);
391 }
392 
393 static int playback_ack(struct snd_pcm_substream *substream)
394 {
395 	struct snd_dice *dice = substream->private_data;
396 	struct amdtp_stream *stream = &dice->rx_stream[substream->pcm->device];
397 
398 	return amdtp_stream_pcm_ack(stream);
399 }
400 
401 int snd_dice_create_pcm(struct snd_dice *dice)
402 {
403 	static const struct snd_pcm_ops capture_ops = {
404 		.open      = pcm_open,
405 		.close     = pcm_close,
406 		.ioctl     = snd_pcm_lib_ioctl,
407 		.hw_params = capture_hw_params,
408 		.hw_free   = capture_hw_free,
409 		.prepare   = capture_prepare,
410 		.trigger   = capture_trigger,
411 		.pointer   = capture_pointer,
412 		.ack       = capture_ack,
413 		.page      = snd_pcm_lib_get_vmalloc_page,
414 	};
415 	static const struct snd_pcm_ops playback_ops = {
416 		.open      = pcm_open,
417 		.close     = pcm_close,
418 		.ioctl     = snd_pcm_lib_ioctl,
419 		.hw_params = playback_hw_params,
420 		.hw_free   = playback_hw_free,
421 		.prepare   = playback_prepare,
422 		.trigger   = playback_trigger,
423 		.pointer   = playback_pointer,
424 		.ack       = playback_ack,
425 		.page      = snd_pcm_lib_get_vmalloc_page,
426 	};
427 	struct snd_pcm *pcm;
428 	unsigned int capture, playback;
429 	int i, j;
430 	int err;
431 
432 	for (i = 0; i < MAX_STREAMS; i++) {
433 		capture = playback = 0;
434 		for (j = 0; j < SND_DICE_RATE_MODE_COUNT; ++j) {
435 			if (dice->tx_pcm_chs[i][j] > 0)
436 				capture = 1;
437 			if (dice->rx_pcm_chs[i][j] > 0)
438 				playback = 1;
439 		}
440 
441 		err = snd_pcm_new(dice->card, "DICE", i, playback, capture,
442 				  &pcm);
443 		if (err < 0)
444 			return err;
445 		pcm->private_data = dice;
446 		strcpy(pcm->name, dice->card->shortname);
447 
448 		if (capture > 0)
449 			snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
450 					&capture_ops);
451 
452 		if (playback > 0)
453 			snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
454 					&playback_ops);
455 	}
456 
457 	return 0;
458 }
459