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