xref: /linux/sound/pci/au88x0/au88x0_pcm.c (revision 9a4df8251ac0aed09d67405c7dfa59f14bde82ed)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  */
4 
5 /*
6  * Vortex PCM ALSA driver.
7  *
8  * Supports ADB and WT DMA. Unfortunately, WT channels do not run yet.
9  * It remains stuck,and DMA transfers do not happen.
10  */
11 #include <sound/asoundef.h>
12 #include <linux/time.h>
13 #include <sound/core.h>
14 #include <sound/pcm.h>
15 #include <sound/pcm_params.h>
16 #include "au88x0.h"
17 
18 #define VORTEX_PCM_TYPE(x) (x->name[40])
19 
20 /* hardware definition */
21 static const struct snd_pcm_hardware snd_vortex_playback_hw_adb = {
22 	.info =
23 	    (SNDRV_PCM_INFO_MMAP | /* SNDRV_PCM_INFO_RESUME | */
24 	     SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_INTERLEAVED |
25 	     SNDRV_PCM_INFO_MMAP_VALID),
26 	.formats =
27 	    SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U8 |
28 	    SNDRV_PCM_FMTBIT_MU_LAW | SNDRV_PCM_FMTBIT_A_LAW,
29 	.rates = SNDRV_PCM_RATE_CONTINUOUS,
30 	.rate_min = 5000,
31 	.rate_max = 48000,
32 	.channels_min = 1,
33 	.channels_max = 2,
34 	.buffer_bytes_max = 0x10000,
35 	.period_bytes_min = 0x20,
36 	.period_bytes_max = 0x1000,
37 	.periods_min = 2,
38 	.periods_max = 1024,
39 };
40 
41 #ifndef CHIP_AU8820
42 static const struct snd_pcm_hardware snd_vortex_playback_hw_a3d = {
43 	.info =
44 	    (SNDRV_PCM_INFO_MMAP | /* SNDRV_PCM_INFO_RESUME | */
45 	     SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_INTERLEAVED |
46 	     SNDRV_PCM_INFO_MMAP_VALID),
47 	.formats =
48 	    SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U8 |
49 	    SNDRV_PCM_FMTBIT_MU_LAW | SNDRV_PCM_FMTBIT_A_LAW,
50 	.rates = SNDRV_PCM_RATE_CONTINUOUS,
51 	.rate_min = 5000,
52 	.rate_max = 48000,
53 	.channels_min = 1,
54 	.channels_max = 1,
55 	.buffer_bytes_max = 0x10000,
56 	.period_bytes_min = 0x100,
57 	.period_bytes_max = 0x1000,
58 	.periods_min = 2,
59 	.periods_max = 64,
60 };
61 #endif
62 static const struct snd_pcm_hardware snd_vortex_playback_hw_spdif = {
63 	.info =
64 	    (SNDRV_PCM_INFO_MMAP | /* SNDRV_PCM_INFO_RESUME | */
65 	     SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_INTERLEAVED |
66 	     SNDRV_PCM_INFO_MMAP_VALID),
67 	.formats =
68 	    SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U8 |
69 	    SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE | SNDRV_PCM_FMTBIT_MU_LAW |
70 	    SNDRV_PCM_FMTBIT_A_LAW,
71 	.rates =
72 	    SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000,
73 	.rate_min = 32000,
74 	.rate_max = 48000,
75 	.channels_min = 1,
76 	.channels_max = 2,
77 	.buffer_bytes_max = 0x10000,
78 	.period_bytes_min = 0x100,
79 	.period_bytes_max = 0x1000,
80 	.periods_min = 2,
81 	.periods_max = 64,
82 };
83 
84 #ifndef CHIP_AU8810
85 static const struct snd_pcm_hardware snd_vortex_playback_hw_wt = {
86 	.info = (SNDRV_PCM_INFO_MMAP |
87 		 SNDRV_PCM_INFO_INTERLEAVED |
88 		 SNDRV_PCM_INFO_BLOCK_TRANSFER | SNDRV_PCM_INFO_MMAP_VALID),
89 	.formats = SNDRV_PCM_FMTBIT_S16_LE,
90 	.rates = SNDRV_PCM_RATE_8000_48000 | SNDRV_PCM_RATE_CONTINUOUS,	// SNDRV_PCM_RATE_48000,
91 	.rate_min = 8000,
92 	.rate_max = 48000,
93 	.channels_min = 1,
94 	.channels_max = 2,
95 	.buffer_bytes_max = 0x10000,
96 	.period_bytes_min = 0x0400,
97 	.period_bytes_max = 0x1000,
98 	.periods_min = 2,
99 	.periods_max = 64,
100 };
101 #endif
102 #ifdef CHIP_AU8830
103 static const unsigned int au8830_channels[3] = {
104 	1, 2, 4,
105 };
106 
107 static const struct snd_pcm_hw_constraint_list hw_constraints_au8830_channels = {
108 	.count = ARRAY_SIZE(au8830_channels),
109 	.list = au8830_channels,
110 	.mask = 0,
111 };
112 #endif
113 
114 static void vortex_notify_pcm_vol_change(struct snd_card *card,
115 			struct snd_kcontrol *kctl, int activate)
116 {
117 	if (activate)
118 		kctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
119 	else
120 		kctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
121 	snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE |
122 				SNDRV_CTL_EVENT_MASK_INFO, &(kctl->id));
123 }
124 
125 /* open callback */
126 static int snd_vortex_pcm_open(struct snd_pcm_substream *substream)
127 {
128 	vortex_t *vortex = snd_pcm_substream_chip(substream);
129 	struct snd_pcm_runtime *runtime = substream->runtime;
130 	int err;
131 
132 	/* Force equal size periods */
133 	if ((err =
134 	     snd_pcm_hw_constraint_integer(runtime,
135 					   SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
136 		return err;
137 	/* Avoid PAGE_SIZE boundary to fall inside of a period. */
138 	if ((err =
139 	     snd_pcm_hw_constraint_pow2(runtime, 0,
140 					SNDRV_PCM_HW_PARAM_PERIOD_BYTES)) < 0)
141 		return err;
142 
143 	snd_pcm_hw_constraint_step(runtime, 0,
144 					SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 64);
145 
146 	if (VORTEX_PCM_TYPE(substream->pcm) != VORTEX_PCM_WT) {
147 #ifndef CHIP_AU8820
148 		if (VORTEX_PCM_TYPE(substream->pcm) == VORTEX_PCM_A3D) {
149 			runtime->hw = snd_vortex_playback_hw_a3d;
150 		}
151 #endif
152 		if (VORTEX_PCM_TYPE(substream->pcm) == VORTEX_PCM_SPDIF) {
153 			runtime->hw = snd_vortex_playback_hw_spdif;
154 			switch (vortex->spdif_sr) {
155 			case 32000:
156 				runtime->hw.rates = SNDRV_PCM_RATE_32000;
157 				break;
158 			case 44100:
159 				runtime->hw.rates = SNDRV_PCM_RATE_44100;
160 				break;
161 			case 48000:
162 				runtime->hw.rates = SNDRV_PCM_RATE_48000;
163 				break;
164 			}
165 		}
166 		if (VORTEX_PCM_TYPE(substream->pcm) == VORTEX_PCM_ADB
167 		    || VORTEX_PCM_TYPE(substream->pcm) == VORTEX_PCM_I2S)
168 			runtime->hw = snd_vortex_playback_hw_adb;
169 #ifdef CHIP_AU8830
170 		if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
171 			VORTEX_IS_QUAD(vortex) &&
172 			VORTEX_PCM_TYPE(substream->pcm) == VORTEX_PCM_ADB) {
173 			runtime->hw.channels_max = 4;
174 			snd_pcm_hw_constraint_list(runtime, 0,
175 				SNDRV_PCM_HW_PARAM_CHANNELS,
176 				&hw_constraints_au8830_channels);
177 		}
178 #endif
179 		substream->runtime->private_data = NULL;
180 	}
181 #ifndef CHIP_AU8810
182 	else {
183 		runtime->hw = snd_vortex_playback_hw_wt;
184 		substream->runtime->private_data = NULL;
185 	}
186 #endif
187 	return 0;
188 }
189 
190 /* close callback */
191 static int snd_vortex_pcm_close(struct snd_pcm_substream *substream)
192 {
193 	//vortex_t *chip = snd_pcm_substream_chip(substream);
194 	stream_t *stream = (stream_t *) substream->runtime->private_data;
195 
196 	// the hardware-specific codes will be here
197 	if (stream != NULL) {
198 		stream->substream = NULL;
199 		stream->nr_ch = 0;
200 	}
201 	substream->runtime->private_data = NULL;
202 	return 0;
203 }
204 
205 /* hw_params callback */
206 static int
207 snd_vortex_pcm_hw_params(struct snd_pcm_substream *substream,
208 			 struct snd_pcm_hw_params *hw_params)
209 {
210 	vortex_t *chip = snd_pcm_substream_chip(substream);
211 	stream_t *stream = (stream_t *) (substream->runtime->private_data);
212 
213 	/*
214 	   pr_info( "Vortex: periods %d, period_bytes %d, channels = %d\n", params_periods(hw_params),
215 	   params_period_bytes(hw_params), params_channels(hw_params));
216 	 */
217 	spin_lock_irq(&chip->lock);
218 	// Make audio routes and config buffer DMA.
219 	if (VORTEX_PCM_TYPE(substream->pcm) != VORTEX_PCM_WT) {
220 		int dma, type = VORTEX_PCM_TYPE(substream->pcm);
221 		/* Dealloc any routes. */
222 		if (stream != NULL)
223 			vortex_adb_allocroute(chip, stream->dma,
224 					      stream->nr_ch, stream->dir,
225 					      stream->type,
226 					      substream->number);
227 		/* Alloc routes. */
228 		dma =
229 		    vortex_adb_allocroute(chip, -1,
230 					  params_channels(hw_params),
231 					  substream->stream, type,
232 					  substream->number);
233 		if (dma < 0) {
234 			spin_unlock_irq(&chip->lock);
235 			return dma;
236 		}
237 		stream = substream->runtime->private_data = &chip->dma_adb[dma];
238 		stream->substream = substream;
239 		/* Setup Buffers. */
240 		vortex_adbdma_setbuffers(chip, dma,
241 					 params_period_bytes(hw_params),
242 					 params_periods(hw_params));
243 		if (VORTEX_PCM_TYPE(substream->pcm) == VORTEX_PCM_ADB) {
244 			chip->pcm_vol[substream->number].active = 1;
245 			vortex_notify_pcm_vol_change(chip->card,
246 				chip->pcm_vol[substream->number].kctl, 1);
247 		}
248 	}
249 #ifndef CHIP_AU8810
250 	else {
251 		/* if (stream != NULL)
252 		   vortex_wt_allocroute(chip, substream->number, 0); */
253 		vortex_wt_allocroute(chip, substream->number,
254 				     params_channels(hw_params));
255 		stream = substream->runtime->private_data =
256 		    &chip->dma_wt[substream->number];
257 		stream->dma = substream->number;
258 		stream->substream = substream;
259 		vortex_wtdma_setbuffers(chip, substream->number,
260 					params_period_bytes(hw_params),
261 					params_periods(hw_params));
262 	}
263 #endif
264 	spin_unlock_irq(&chip->lock);
265 	return 0;
266 }
267 
268 /* hw_free callback */
269 static int snd_vortex_pcm_hw_free(struct snd_pcm_substream *substream)
270 {
271 	vortex_t *chip = snd_pcm_substream_chip(substream);
272 	stream_t *stream = (stream_t *) (substream->runtime->private_data);
273 
274 	spin_lock_irq(&chip->lock);
275 	// Delete audio routes.
276 	if (VORTEX_PCM_TYPE(substream->pcm) != VORTEX_PCM_WT) {
277 		if (stream != NULL) {
278 			if (VORTEX_PCM_TYPE(substream->pcm) == VORTEX_PCM_ADB) {
279 				chip->pcm_vol[substream->number].active = 0;
280 				vortex_notify_pcm_vol_change(chip->card,
281 					chip->pcm_vol[substream->number].kctl,
282 					0);
283 			}
284 			vortex_adb_allocroute(chip, stream->dma,
285 					      stream->nr_ch, stream->dir,
286 					      stream->type,
287 					      substream->number);
288 		}
289 	}
290 #ifndef CHIP_AU8810
291 	else {
292 		if (stream != NULL)
293 			vortex_wt_allocroute(chip, stream->dma, 0);
294 	}
295 #endif
296 	substream->runtime->private_data = NULL;
297 	spin_unlock_irq(&chip->lock);
298 
299 	return 0;
300 }
301 
302 /* prepare callback */
303 static int snd_vortex_pcm_prepare(struct snd_pcm_substream *substream)
304 {
305 	vortex_t *chip = snd_pcm_substream_chip(substream);
306 	struct snd_pcm_runtime *runtime = substream->runtime;
307 	stream_t *stream = (stream_t *) substream->runtime->private_data;
308 	int dma = stream->dma, fmt, dir;
309 
310 	// set up the hardware with the current configuration.
311 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
312 		dir = 1;
313 	else
314 		dir = 0;
315 	fmt = vortex_alsafmt_aspfmt(runtime->format, chip);
316 	spin_lock_irq(&chip->lock);
317 	if (VORTEX_PCM_TYPE(substream->pcm) != VORTEX_PCM_WT) {
318 		vortex_adbdma_setmode(chip, dma, 1, dir, fmt,
319 				runtime->channels == 1 ? 0 : 1, 0);
320 		vortex_adbdma_setstartbuffer(chip, dma, 0);
321 		if (VORTEX_PCM_TYPE(substream->pcm) != VORTEX_PCM_SPDIF)
322 			vortex_adb_setsrc(chip, dma, runtime->rate, dir);
323 	}
324 #ifndef CHIP_AU8810
325 	else {
326 		vortex_wtdma_setmode(chip, dma, 1, fmt, 0, 0);
327 		// FIXME: Set rate (i guess using vortex_wt_writereg() somehow).
328 		vortex_wtdma_setstartbuffer(chip, dma, 0);
329 	}
330 #endif
331 	spin_unlock_irq(&chip->lock);
332 	return 0;
333 }
334 
335 /* trigger callback */
336 static int snd_vortex_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
337 {
338 	vortex_t *chip = snd_pcm_substream_chip(substream);
339 	stream_t *stream = (stream_t *) substream->runtime->private_data;
340 	int dma = stream->dma;
341 
342 	spin_lock(&chip->lock);
343 	switch (cmd) {
344 	case SNDRV_PCM_TRIGGER_START:
345 		// do something to start the PCM engine
346 		//printk(KERN_INFO "vortex: start %d\n", dma);
347 		stream->fifo_enabled = 1;
348 		if (VORTEX_PCM_TYPE(substream->pcm) != VORTEX_PCM_WT) {
349 			vortex_adbdma_resetup(chip, dma);
350 			vortex_adbdma_startfifo(chip, dma);
351 		}
352 #ifndef CHIP_AU8810
353 		else {
354 			dev_info(chip->card->dev, "wt start %d\n", dma);
355 			vortex_wtdma_startfifo(chip, dma);
356 		}
357 #endif
358 		break;
359 	case SNDRV_PCM_TRIGGER_STOP:
360 		// do something to stop the PCM engine
361 		//printk(KERN_INFO "vortex: stop %d\n", dma);
362 		stream->fifo_enabled = 0;
363 		if (VORTEX_PCM_TYPE(substream->pcm) != VORTEX_PCM_WT)
364 			vortex_adbdma_stopfifo(chip, dma);
365 #ifndef CHIP_AU8810
366 		else {
367 			dev_info(chip->card->dev, "wt stop %d\n", dma);
368 			vortex_wtdma_stopfifo(chip, dma);
369 		}
370 #endif
371 		break;
372 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
373 		//printk(KERN_INFO "vortex: pause %d\n", dma);
374 		if (VORTEX_PCM_TYPE(substream->pcm) != VORTEX_PCM_WT)
375 			vortex_adbdma_pausefifo(chip, dma);
376 #ifndef CHIP_AU8810
377 		else
378 			vortex_wtdma_pausefifo(chip, dma);
379 #endif
380 		break;
381 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
382 		//printk(KERN_INFO "vortex: resume %d\n", dma);
383 		if (VORTEX_PCM_TYPE(substream->pcm) != VORTEX_PCM_WT)
384 			vortex_adbdma_resumefifo(chip, dma);
385 #ifndef CHIP_AU8810
386 		else
387 			vortex_wtdma_resumefifo(chip, dma);
388 #endif
389 		break;
390 	default:
391 		spin_unlock(&chip->lock);
392 		return -EINVAL;
393 	}
394 	spin_unlock(&chip->lock);
395 	return 0;
396 }
397 
398 /* pointer callback */
399 static snd_pcm_uframes_t snd_vortex_pcm_pointer(struct snd_pcm_substream *substream)
400 {
401 	vortex_t *chip = snd_pcm_substream_chip(substream);
402 	stream_t *stream = (stream_t *) substream->runtime->private_data;
403 	int dma = stream->dma;
404 	snd_pcm_uframes_t current_ptr = 0;
405 
406 	spin_lock(&chip->lock);
407 	if (VORTEX_PCM_TYPE(substream->pcm) != VORTEX_PCM_WT)
408 		current_ptr = vortex_adbdma_getlinearpos(chip, dma);
409 #ifndef CHIP_AU8810
410 	else
411 		current_ptr = vortex_wtdma_getlinearpos(chip, dma);
412 #endif
413 	//printk(KERN_INFO "vortex: pointer = 0x%x\n", current_ptr);
414 	spin_unlock(&chip->lock);
415 	current_ptr = bytes_to_frames(substream->runtime, current_ptr);
416 	if (current_ptr >= substream->runtime->buffer_size)
417 		current_ptr = 0;
418 	return current_ptr;
419 }
420 
421 /* operators */
422 static const struct snd_pcm_ops snd_vortex_playback_ops = {
423 	.open = snd_vortex_pcm_open,
424 	.close = snd_vortex_pcm_close,
425 	.ioctl = snd_pcm_lib_ioctl,
426 	.hw_params = snd_vortex_pcm_hw_params,
427 	.hw_free = snd_vortex_pcm_hw_free,
428 	.prepare = snd_vortex_pcm_prepare,
429 	.trigger = snd_vortex_pcm_trigger,
430 	.pointer = snd_vortex_pcm_pointer,
431 };
432 
433 /*
434 *  definitions of capture are omitted here...
435 */
436 
437 static char *vortex_pcm_prettyname[VORTEX_PCM_LAST] = {
438 	CARD_NAME " ADB",
439 	CARD_NAME " SPDIF",
440 	CARD_NAME " A3D",
441 	CARD_NAME " WT",
442 	CARD_NAME " I2S",
443 };
444 static char *vortex_pcm_name[VORTEX_PCM_LAST] = {
445 	"adb",
446 	"spdif",
447 	"a3d",
448 	"wt",
449 	"i2s",
450 };
451 
452 /* SPDIF kcontrol */
453 
454 static int snd_vortex_spdif_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
455 {
456 	uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
457 	uinfo->count = 1;
458 	return 0;
459 }
460 
461 static int snd_vortex_spdif_mask_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
462 {
463 	ucontrol->value.iec958.status[0] = 0xff;
464 	ucontrol->value.iec958.status[1] = 0xff;
465 	ucontrol->value.iec958.status[2] = 0xff;
466 	ucontrol->value.iec958.status[3] = IEC958_AES3_CON_FS;
467 	return 0;
468 }
469 
470 static int snd_vortex_spdif_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
471 {
472 	vortex_t *vortex = snd_kcontrol_chip(kcontrol);
473 	ucontrol->value.iec958.status[0] = 0x00;
474 	ucontrol->value.iec958.status[1] = IEC958_AES1_CON_ORIGINAL|IEC958_AES1_CON_DIGDIGCONV_ID;
475 	ucontrol->value.iec958.status[2] = 0x00;
476 	switch (vortex->spdif_sr) {
477 	case 32000: ucontrol->value.iec958.status[3] = IEC958_AES3_CON_FS_32000; break;
478 	case 44100: ucontrol->value.iec958.status[3] = IEC958_AES3_CON_FS_44100; break;
479 	case 48000: ucontrol->value.iec958.status[3] = IEC958_AES3_CON_FS_48000; break;
480 	}
481 	return 0;
482 }
483 
484 static int snd_vortex_spdif_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
485 {
486 	vortex_t *vortex = snd_kcontrol_chip(kcontrol);
487 	int spdif_sr = 48000;
488 	switch (ucontrol->value.iec958.status[3] & IEC958_AES3_CON_FS) {
489 	case IEC958_AES3_CON_FS_32000: spdif_sr = 32000; break;
490 	case IEC958_AES3_CON_FS_44100: spdif_sr = 44100; break;
491 	case IEC958_AES3_CON_FS_48000: spdif_sr = 48000; break;
492 	}
493 	if (spdif_sr == vortex->spdif_sr)
494 		return 0;
495 	vortex->spdif_sr = spdif_sr;
496 	vortex_spdif_init(vortex, vortex->spdif_sr, 1);
497 	return 1;
498 }
499 
500 /* spdif controls */
501 static struct snd_kcontrol_new snd_vortex_mixer_spdif[] = {
502 	{
503 		.iface =	SNDRV_CTL_ELEM_IFACE_PCM,
504 		.name =		SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
505 		.info =		snd_vortex_spdif_info,
506 		.get =		snd_vortex_spdif_get,
507 		.put =		snd_vortex_spdif_put,
508 	},
509 	{
510 		.access =	SNDRV_CTL_ELEM_ACCESS_READ,
511 		.iface =	SNDRV_CTL_ELEM_IFACE_PCM,
512 		.name =		SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
513 		.info =		snd_vortex_spdif_info,
514 		.get =		snd_vortex_spdif_mask_get
515 	},
516 };
517 
518 /* subdevice PCM Volume control */
519 
520 static int snd_vortex_pcm_vol_info(struct snd_kcontrol *kcontrol,
521 				struct snd_ctl_elem_info *uinfo)
522 {
523 	vortex_t *vortex = snd_kcontrol_chip(kcontrol);
524 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
525 	uinfo->count = (VORTEX_IS_QUAD(vortex) ? 4 : 2);
526 	uinfo->value.integer.min = -128;
527 	uinfo->value.integer.max = 32;
528 	return 0;
529 }
530 
531 static int snd_vortex_pcm_vol_get(struct snd_kcontrol *kcontrol,
532 				struct snd_ctl_elem_value *ucontrol)
533 {
534 	int i;
535 	vortex_t *vortex = snd_kcontrol_chip(kcontrol);
536 	int subdev = kcontrol->id.subdevice;
537 	struct pcm_vol *p = &vortex->pcm_vol[subdev];
538 	int max_chn = (VORTEX_IS_QUAD(vortex) ? 4 : 2);
539 	for (i = 0; i < max_chn; i++)
540 		ucontrol->value.integer.value[i] = p->vol[i];
541 	return 0;
542 }
543 
544 static int snd_vortex_pcm_vol_put(struct snd_kcontrol *kcontrol,
545 				struct snd_ctl_elem_value *ucontrol)
546 {
547 	int i;
548 	int changed = 0;
549 	int mixin;
550 	unsigned char vol;
551 	vortex_t *vortex = snd_kcontrol_chip(kcontrol);
552 	int subdev = kcontrol->id.subdevice;
553 	struct pcm_vol *p = &vortex->pcm_vol[subdev];
554 	int max_chn = (VORTEX_IS_QUAD(vortex) ? 4 : 2);
555 	for (i = 0; i < max_chn; i++) {
556 		if (p->vol[i] != ucontrol->value.integer.value[i]) {
557 			p->vol[i] = ucontrol->value.integer.value[i];
558 			if (p->active) {
559 				switch (vortex->dma_adb[p->dma].nr_ch) {
560 				case 1:
561 					mixin = p->mixin[0];
562 					break;
563 				case 2:
564 				default:
565 					mixin = p->mixin[(i < 2) ? i : (i - 2)];
566 					break;
567 				case 4:
568 					mixin = p->mixin[i];
569 					break;
570 				}
571 				vol = p->vol[i];
572 				vortex_mix_setinputvolumebyte(vortex,
573 					vortex->mixplayb[i], mixin, vol);
574 			}
575 			changed = 1;
576 		}
577 	}
578 	return changed;
579 }
580 
581 static const DECLARE_TLV_DB_MINMAX(vortex_pcm_vol_db_scale, -9600, 2400);
582 
583 static const struct snd_kcontrol_new snd_vortex_pcm_vol = {
584 	.iface = SNDRV_CTL_ELEM_IFACE_PCM,
585 	.name = "PCM Playback Volume",
586 	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
587 		SNDRV_CTL_ELEM_ACCESS_TLV_READ |
588 		SNDRV_CTL_ELEM_ACCESS_INACTIVE,
589 	.info = snd_vortex_pcm_vol_info,
590 	.get = snd_vortex_pcm_vol_get,
591 	.put = snd_vortex_pcm_vol_put,
592 	.tlv = { .p = vortex_pcm_vol_db_scale },
593 };
594 
595 /* create a pcm device */
596 static int snd_vortex_new_pcm(vortex_t *chip, int idx, int nr)
597 {
598 	struct snd_pcm *pcm;
599 	struct snd_kcontrol *kctl;
600 	int i;
601 	int err, nr_capt;
602 
603 	if (!chip || idx < 0 || idx >= VORTEX_PCM_LAST)
604 		return -ENODEV;
605 
606 	/* idx indicates which kind of PCM device. ADB, SPDIF, I2S and A3D share the
607 	 * same dma engine. WT uses it own separate dma engine which can't capture. */
608 	if (idx == VORTEX_PCM_ADB)
609 		nr_capt = nr;
610 	else
611 		nr_capt = 0;
612 	err = snd_pcm_new(chip->card, vortex_pcm_prettyname[idx], idx, nr,
613 			  nr_capt, &pcm);
614 	if (err < 0)
615 		return err;
616 	snprintf(pcm->name, sizeof(pcm->name),
617 		"%s %s", CARD_NAME_SHORT, vortex_pcm_name[idx]);
618 	chip->pcm[idx] = pcm;
619 	// This is an evil hack, but it saves a lot of duplicated code.
620 	VORTEX_PCM_TYPE(pcm) = idx;
621 	pcm->private_data = chip;
622 	/* set operators */
623 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
624 			&snd_vortex_playback_ops);
625 	if (idx == VORTEX_PCM_ADB)
626 		snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
627 				&snd_vortex_playback_ops);
628 
629 	/* pre-allocation of Scatter-Gather buffers */
630 
631 	snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
632 				       &chip->pci_dev->dev, 0x10000, 0x10000);
633 
634 	switch (VORTEX_PCM_TYPE(pcm)) {
635 	case VORTEX_PCM_ADB:
636 		err = snd_pcm_add_chmap_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
637 					     snd_pcm_std_chmaps,
638 					     VORTEX_IS_QUAD(chip) ? 4 : 2,
639 					     0, NULL);
640 		if (err < 0)
641 			return err;
642 		err = snd_pcm_add_chmap_ctls(pcm, SNDRV_PCM_STREAM_CAPTURE,
643 					     snd_pcm_std_chmaps, 2, 0, NULL);
644 		if (err < 0)
645 			return err;
646 		break;
647 #ifdef CHIP_AU8830
648 	case VORTEX_PCM_A3D:
649 		err = snd_pcm_add_chmap_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
650 					     snd_pcm_std_chmaps, 1, 0, NULL);
651 		if (err < 0)
652 			return err;
653 		break;
654 #endif
655 	}
656 
657 	if (VORTEX_PCM_TYPE(pcm) == VORTEX_PCM_SPDIF) {
658 		for (i = 0; i < ARRAY_SIZE(snd_vortex_mixer_spdif); i++) {
659 			kctl = snd_ctl_new1(&snd_vortex_mixer_spdif[i], chip);
660 			if (!kctl)
661 				return -ENOMEM;
662 			if ((err = snd_ctl_add(chip->card, kctl)) < 0)
663 				return err;
664 		}
665 	}
666 	if (VORTEX_PCM_TYPE(pcm) == VORTEX_PCM_ADB) {
667 		for (i = 0; i < NR_PCM; i++) {
668 			chip->pcm_vol[i].active = 0;
669 			chip->pcm_vol[i].dma = -1;
670 			kctl = snd_ctl_new1(&snd_vortex_pcm_vol, chip);
671 			if (!kctl)
672 				return -ENOMEM;
673 			chip->pcm_vol[i].kctl = kctl;
674 			kctl->id.device = 0;
675 			kctl->id.subdevice = i;
676 			err = snd_ctl_add(chip->card, kctl);
677 			if (err < 0)
678 				return err;
679 		}
680 	}
681 	return 0;
682 }
683