xref: /linux/sound/pci/ctxfi/ctatc.c (revision 24bce201d79807b668bf9d9e0aca801c5c0d5f78)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2008, Creative Technology Ltd. All Rights Reserved.
4  *
5  * @File    ctatc.c
6  *
7  * @Brief
8  * This file contains the implementation of the device resource management
9  * object.
10  *
11  * @Author Liu Chun
12  * @Date Mar 28 2008
13  */
14 
15 #include "ctatc.h"
16 #include "ctpcm.h"
17 #include "ctmixer.h"
18 #include "ctsrc.h"
19 #include "ctamixer.h"
20 #include "ctdaio.h"
21 #include "cttimer.h"
22 #include <linux/delay.h>
23 #include <linux/slab.h>
24 #include <sound/pcm.h>
25 #include <sound/control.h>
26 #include <sound/asoundef.h>
27 
28 #define MONO_SUM_SCALE	0x19a8	/* 2^(-0.5) in 14-bit floating format */
29 #define MAX_MULTI_CHN	8
30 
31 #define IEC958_DEFAULT_CON ((IEC958_AES0_NONAUDIO \
32 			    | IEC958_AES0_CON_NOT_COPYRIGHT) \
33 			    | ((IEC958_AES1_CON_MIXER \
34 			    | IEC958_AES1_CON_ORIGINAL) << 8) \
35 			    | (0x10 << 16) \
36 			    | ((IEC958_AES3_CON_FS_48000) << 24))
37 
38 static const struct snd_pci_quirk subsys_20k1_list[] = {
39 	SND_PCI_QUIRK(PCI_VENDOR_ID_CREATIVE, 0x0021, "SB046x", CTSB046X),
40 	SND_PCI_QUIRK(PCI_VENDOR_ID_CREATIVE, 0x0022, "SB055x", CTSB055X),
41 	SND_PCI_QUIRK(PCI_VENDOR_ID_CREATIVE, 0x002f, "SB055x", CTSB055X),
42 	SND_PCI_QUIRK(PCI_VENDOR_ID_CREATIVE, 0x0029, "SB073x", CTSB073X),
43 	SND_PCI_QUIRK(PCI_VENDOR_ID_CREATIVE, 0x0031, "SB073x", CTSB073X),
44 	SND_PCI_QUIRK_MASK(PCI_VENDOR_ID_CREATIVE, 0xf000, 0x6000,
45 			   "UAA", CTUAA),
46 	{ } /* terminator */
47 };
48 
49 static const struct snd_pci_quirk subsys_20k2_list[] = {
50 	SND_PCI_QUIRK(PCI_VENDOR_ID_CREATIVE, PCI_SUBDEVICE_ID_CREATIVE_SB0760,
51 		      "SB0760", CTSB0760),
52 	SND_PCI_QUIRK(PCI_VENDOR_ID_CREATIVE, PCI_SUBDEVICE_ID_CREATIVE_SB1270,
53 		      "SB1270", CTSB1270),
54 	SND_PCI_QUIRK(PCI_VENDOR_ID_CREATIVE, PCI_SUBDEVICE_ID_CREATIVE_SB08801,
55 		      "SB0880", CTSB0880),
56 	SND_PCI_QUIRK(PCI_VENDOR_ID_CREATIVE, PCI_SUBDEVICE_ID_CREATIVE_SB08802,
57 		      "SB0880", CTSB0880),
58 	SND_PCI_QUIRK(PCI_VENDOR_ID_CREATIVE, PCI_SUBDEVICE_ID_CREATIVE_SB08803,
59 		      "SB0880", CTSB0880),
60 	SND_PCI_QUIRK_MASK(PCI_VENDOR_ID_CREATIVE, 0xf000,
61 			   PCI_SUBDEVICE_ID_CREATIVE_HENDRIX, "HENDRIX",
62 			   CTHENDRIX),
63 	{ } /* terminator */
64 };
65 
66 static const char *ct_subsys_name[NUM_CTCARDS] = {
67 	/* 20k1 models */
68 	[CTSB046X]	= "SB046x",
69 	[CTSB055X]	= "SB055x",
70 	[CTSB073X]	= "SB073x",
71 	[CTUAA]		= "UAA",
72 	[CT20K1_UNKNOWN] = "Unknown",
73 	/* 20k2 models */
74 	[CTSB0760]	= "SB076x",
75 	[CTHENDRIX]	= "Hendrix",
76 	[CTSB0880]	= "SB0880",
77 	[CTSB1270]      = "SB1270",
78 	[CT20K2_UNKNOWN] = "Unknown",
79 };
80 
81 static struct {
82 	int (*create)(struct ct_atc *atc,
83 			enum CTALSADEVS device, const char *device_name);
84 	int (*destroy)(void *alsa_dev);
85 	const char *public_name;
86 } alsa_dev_funcs[NUM_CTALSADEVS] = {
87 	[FRONT]		= { .create = ct_alsa_pcm_create,
88 			    .destroy = NULL,
89 			    .public_name = "Front/WaveIn"},
90 	[SURROUND]	= { .create = ct_alsa_pcm_create,
91 			    .destroy = NULL,
92 			    .public_name = "Surround"},
93 	[CLFE]		= { .create = ct_alsa_pcm_create,
94 			    .destroy = NULL,
95 			    .public_name = "Center/LFE"},
96 	[SIDE]		= { .create = ct_alsa_pcm_create,
97 			    .destroy = NULL,
98 			    .public_name = "Side"},
99 	[IEC958]	= { .create = ct_alsa_pcm_create,
100 			    .destroy = NULL,
101 			    .public_name = "IEC958 Non-audio"},
102 
103 	[MIXER]		= { .create = ct_alsa_mix_create,
104 			    .destroy = NULL,
105 			    .public_name = "Mixer"}
106 };
107 
108 typedef int (*create_t)(struct hw *, void **);
109 typedef int (*destroy_t)(void *);
110 
111 static struct {
112 	int (*create)(struct hw *hw, void **rmgr);
113 	int (*destroy)(void *mgr);
114 } rsc_mgr_funcs[NUM_RSCTYP] = {
115 	[SRC] 		= { .create 	= (create_t)src_mgr_create,
116 			    .destroy 	= (destroy_t)src_mgr_destroy	},
117 	[SRCIMP] 	= { .create 	= (create_t)srcimp_mgr_create,
118 			    .destroy 	= (destroy_t)srcimp_mgr_destroy	},
119 	[AMIXER]	= { .create	= (create_t)amixer_mgr_create,
120 			    .destroy	= (destroy_t)amixer_mgr_destroy	},
121 	[SUM]		= { .create	= (create_t)sum_mgr_create,
122 			    .destroy	= (destroy_t)sum_mgr_destroy	},
123 	[DAIO]		= { .create	= (create_t)daio_mgr_create,
124 			    .destroy	= (destroy_t)daio_mgr_destroy	}
125 };
126 
127 static int
128 atc_pcm_release_resources(struct ct_atc *atc, struct ct_atc_pcm *apcm);
129 
130 /* *
131  * Only mono and interleaved modes are supported now.
132  * Always allocates a contiguous channel block.
133  * */
134 
135 static int ct_map_audio_buffer(struct ct_atc *atc, struct ct_atc_pcm *apcm)
136 {
137 	struct snd_pcm_runtime *runtime;
138 	struct ct_vm *vm;
139 
140 	if (!apcm->substream)
141 		return 0;
142 
143 	runtime = apcm->substream->runtime;
144 	vm = atc->vm;
145 
146 	apcm->vm_block = vm->map(vm, apcm->substream, runtime->dma_bytes);
147 
148 	if (!apcm->vm_block)
149 		return -ENOENT;
150 
151 	return 0;
152 }
153 
154 static void ct_unmap_audio_buffer(struct ct_atc *atc, struct ct_atc_pcm *apcm)
155 {
156 	struct ct_vm *vm;
157 
158 	if (!apcm->vm_block)
159 		return;
160 
161 	vm = atc->vm;
162 
163 	vm->unmap(vm, apcm->vm_block);
164 
165 	apcm->vm_block = NULL;
166 }
167 
168 static unsigned long atc_get_ptp_phys(struct ct_atc *atc, int index)
169 {
170 	return atc->vm->get_ptp_phys(atc->vm, index);
171 }
172 
173 static unsigned int convert_format(snd_pcm_format_t snd_format,
174 				   struct snd_card *card)
175 {
176 	switch (snd_format) {
177 	case SNDRV_PCM_FORMAT_U8:
178 		return SRC_SF_U8;
179 	case SNDRV_PCM_FORMAT_S16_LE:
180 		return SRC_SF_S16;
181 	case SNDRV_PCM_FORMAT_S24_3LE:
182 		return SRC_SF_S24;
183 	case SNDRV_PCM_FORMAT_S32_LE:
184 		return SRC_SF_S32;
185 	case SNDRV_PCM_FORMAT_FLOAT_LE:
186 		return SRC_SF_F32;
187 	default:
188 		dev_err(card->dev, "not recognized snd format is %d\n",
189 			snd_format);
190 		return SRC_SF_S16;
191 	}
192 }
193 
194 static unsigned int
195 atc_get_pitch(unsigned int input_rate, unsigned int output_rate)
196 {
197 	unsigned int pitch;
198 	int b;
199 
200 	/* get pitch and convert to fixed-point 8.24 format. */
201 	pitch = (input_rate / output_rate) << 24;
202 	input_rate %= output_rate;
203 	input_rate /= 100;
204 	output_rate /= 100;
205 	for (b = 31; ((b >= 0) && !(input_rate >> b)); )
206 		b--;
207 
208 	if (b >= 0) {
209 		input_rate <<= (31 - b);
210 		input_rate /= output_rate;
211 		b = 24 - (31 - b);
212 		if (b >= 0)
213 			input_rate <<= b;
214 		else
215 			input_rate >>= -b;
216 
217 		pitch |= input_rate;
218 	}
219 
220 	return pitch;
221 }
222 
223 static int select_rom(unsigned int pitch)
224 {
225 	if (pitch > 0x00428f5c && pitch < 0x01b851ec) {
226 		/* 0.26 <= pitch <= 1.72 */
227 		return 1;
228 	} else if (pitch == 0x01d66666 || pitch == 0x01d66667) {
229 		/* pitch == 1.8375 */
230 		return 2;
231 	} else if (pitch == 0x02000000) {
232 		/* pitch == 2 */
233 		return 3;
234 	} else if (pitch <= 0x08000000) {
235 		/* 0 <= pitch <= 8 */
236 		return 0;
237 	} else {
238 		return -ENOENT;
239 	}
240 }
241 
242 static int atc_pcm_playback_prepare(struct ct_atc *atc, struct ct_atc_pcm *apcm)
243 {
244 	struct src_mgr *src_mgr = atc->rsc_mgrs[SRC];
245 	struct amixer_mgr *amixer_mgr = atc->rsc_mgrs[AMIXER];
246 	struct src_desc desc = {0};
247 	struct amixer_desc mix_dsc = {0};
248 	struct src *src;
249 	struct amixer *amixer;
250 	int err;
251 	int n_amixer = apcm->substream->runtime->channels, i = 0;
252 	int device = apcm->substream->pcm->device;
253 	unsigned int pitch;
254 
255 	/* first release old resources */
256 	atc_pcm_release_resources(atc, apcm);
257 
258 	/* Get SRC resource */
259 	desc.multi = apcm->substream->runtime->channels;
260 	desc.msr = atc->msr;
261 	desc.mode = MEMRD;
262 	err = src_mgr->get_src(src_mgr, &desc, (struct src **)&apcm->src);
263 	if (err)
264 		goto error1;
265 
266 	pitch = atc_get_pitch(apcm->substream->runtime->rate,
267 						(atc->rsr * atc->msr));
268 	src = apcm->src;
269 	src->ops->set_pitch(src, pitch);
270 	src->ops->set_rom(src, select_rom(pitch));
271 	src->ops->set_sf(src, convert_format(apcm->substream->runtime->format,
272 					     atc->card));
273 	src->ops->set_pm(src, (src->ops->next_interleave(src) != NULL));
274 
275 	/* Get AMIXER resource */
276 	n_amixer = (n_amixer < 2) ? 2 : n_amixer;
277 	apcm->amixers = kcalloc(n_amixer, sizeof(void *), GFP_KERNEL);
278 	if (!apcm->amixers) {
279 		err = -ENOMEM;
280 		goto error1;
281 	}
282 	mix_dsc.msr = atc->msr;
283 	for (i = 0, apcm->n_amixer = 0; i < n_amixer; i++) {
284 		err = amixer_mgr->get_amixer(amixer_mgr, &mix_dsc,
285 					(struct amixer **)&apcm->amixers[i]);
286 		if (err)
287 			goto error1;
288 
289 		apcm->n_amixer++;
290 	}
291 
292 	/* Set up device virtual mem map */
293 	err = ct_map_audio_buffer(atc, apcm);
294 	if (err < 0)
295 		goto error1;
296 
297 	/* Connect resources */
298 	src = apcm->src;
299 	for (i = 0; i < n_amixer; i++) {
300 		amixer = apcm->amixers[i];
301 		mutex_lock(&atc->atc_mutex);
302 		amixer->ops->setup(amixer, &src->rsc,
303 					INIT_VOL, atc->pcm[i+device*2]);
304 		mutex_unlock(&atc->atc_mutex);
305 		src = src->ops->next_interleave(src);
306 		if (!src)
307 			src = apcm->src;
308 	}
309 
310 	ct_timer_prepare(apcm->timer);
311 
312 	return 0;
313 
314 error1:
315 	atc_pcm_release_resources(atc, apcm);
316 	return err;
317 }
318 
319 static int
320 atc_pcm_release_resources(struct ct_atc *atc, struct ct_atc_pcm *apcm)
321 {
322 	struct src_mgr *src_mgr = atc->rsc_mgrs[SRC];
323 	struct srcimp_mgr *srcimp_mgr = atc->rsc_mgrs[SRCIMP];
324 	struct amixer_mgr *amixer_mgr = atc->rsc_mgrs[AMIXER];
325 	struct sum_mgr *sum_mgr = atc->rsc_mgrs[SUM];
326 	struct srcimp *srcimp;
327 	int i;
328 
329 	if (apcm->srcimps) {
330 		for (i = 0; i < apcm->n_srcimp; i++) {
331 			srcimp = apcm->srcimps[i];
332 			srcimp->ops->unmap(srcimp);
333 			srcimp_mgr->put_srcimp(srcimp_mgr, srcimp);
334 			apcm->srcimps[i] = NULL;
335 		}
336 		kfree(apcm->srcimps);
337 		apcm->srcimps = NULL;
338 	}
339 
340 	if (apcm->srccs) {
341 		for (i = 0; i < apcm->n_srcc; i++) {
342 			src_mgr->put_src(src_mgr, apcm->srccs[i]);
343 			apcm->srccs[i] = NULL;
344 		}
345 		kfree(apcm->srccs);
346 		apcm->srccs = NULL;
347 	}
348 
349 	if (apcm->amixers) {
350 		for (i = 0; i < apcm->n_amixer; i++) {
351 			amixer_mgr->put_amixer(amixer_mgr, apcm->amixers[i]);
352 			apcm->amixers[i] = NULL;
353 		}
354 		kfree(apcm->amixers);
355 		apcm->amixers = NULL;
356 	}
357 
358 	if (apcm->mono) {
359 		sum_mgr->put_sum(sum_mgr, apcm->mono);
360 		apcm->mono = NULL;
361 	}
362 
363 	if (apcm->src) {
364 		src_mgr->put_src(src_mgr, apcm->src);
365 		apcm->src = NULL;
366 	}
367 
368 	if (apcm->vm_block) {
369 		/* Undo device virtual mem map */
370 		ct_unmap_audio_buffer(atc, apcm);
371 		apcm->vm_block = NULL;
372 	}
373 
374 	return 0;
375 }
376 
377 static int atc_pcm_playback_start(struct ct_atc *atc, struct ct_atc_pcm *apcm)
378 {
379 	unsigned int max_cisz;
380 	struct src *src = apcm->src;
381 
382 	if (apcm->started)
383 		return 0;
384 	apcm->started = 1;
385 
386 	max_cisz = src->multi * src->rsc.msr;
387 	max_cisz = 0x80 * (max_cisz < 8 ? max_cisz : 8);
388 
389 	src->ops->set_sa(src, apcm->vm_block->addr);
390 	src->ops->set_la(src, apcm->vm_block->addr + apcm->vm_block->size);
391 	src->ops->set_ca(src, apcm->vm_block->addr + max_cisz);
392 	src->ops->set_cisz(src, max_cisz);
393 
394 	src->ops->set_bm(src, 1);
395 	src->ops->set_state(src, SRC_STATE_INIT);
396 	src->ops->commit_write(src);
397 
398 	ct_timer_start(apcm->timer);
399 	return 0;
400 }
401 
402 static int atc_pcm_stop(struct ct_atc *atc, struct ct_atc_pcm *apcm)
403 {
404 	struct src *src;
405 	int i;
406 
407 	ct_timer_stop(apcm->timer);
408 
409 	src = apcm->src;
410 	src->ops->set_bm(src, 0);
411 	src->ops->set_state(src, SRC_STATE_OFF);
412 	src->ops->commit_write(src);
413 
414 	if (apcm->srccs) {
415 		for (i = 0; i < apcm->n_srcc; i++) {
416 			src = apcm->srccs[i];
417 			src->ops->set_bm(src, 0);
418 			src->ops->set_state(src, SRC_STATE_OFF);
419 			src->ops->commit_write(src);
420 		}
421 	}
422 
423 	apcm->started = 0;
424 
425 	return 0;
426 }
427 
428 static int
429 atc_pcm_playback_position(struct ct_atc *atc, struct ct_atc_pcm *apcm)
430 {
431 	struct src *src = apcm->src;
432 	u32 size, max_cisz;
433 	int position;
434 
435 	if (!src)
436 		return 0;
437 	position = src->ops->get_ca(src);
438 
439 	if (position < apcm->vm_block->addr) {
440 		dev_dbg(atc->card->dev,
441 			"bad ca - ca=0x%08x, vba=0x%08x, vbs=0x%08x\n",
442 			position, apcm->vm_block->addr, apcm->vm_block->size);
443 		position = apcm->vm_block->addr;
444 	}
445 
446 	size = apcm->vm_block->size;
447 	max_cisz = src->multi * src->rsc.msr;
448 	max_cisz = 128 * (max_cisz < 8 ? max_cisz : 8);
449 
450 	return (position + size - max_cisz - apcm->vm_block->addr) % size;
451 }
452 
453 struct src_node_conf_t {
454 	unsigned int pitch;
455 	unsigned int msr:8;
456 	unsigned int mix_msr:8;
457 	unsigned int imp_msr:8;
458 	unsigned int vo:1;
459 };
460 
461 static void setup_src_node_conf(struct ct_atc *atc, struct ct_atc_pcm *apcm,
462 				struct src_node_conf_t *conf, int *n_srcc)
463 {
464 	unsigned int pitch;
465 
466 	/* get pitch and convert to fixed-point 8.24 format. */
467 	pitch = atc_get_pitch((atc->rsr * atc->msr),
468 				apcm->substream->runtime->rate);
469 	*n_srcc = 0;
470 
471 	if (1 == atc->msr) { /* FIXME: do we really need SRC here if pitch==1 */
472 		*n_srcc = apcm->substream->runtime->channels;
473 		conf[0].pitch = pitch;
474 		conf[0].mix_msr = conf[0].imp_msr = conf[0].msr = 1;
475 		conf[0].vo = 1;
476 	} else if (2 <= atc->msr) {
477 		if (0x8000000 < pitch) {
478 			/* Need two-stage SRCs, SRCIMPs and
479 			 * AMIXERs for converting format */
480 			conf[0].pitch = (atc->msr << 24);
481 			conf[0].msr = conf[0].mix_msr = 1;
482 			conf[0].imp_msr = atc->msr;
483 			conf[0].vo = 0;
484 			conf[1].pitch = atc_get_pitch(atc->rsr,
485 					apcm->substream->runtime->rate);
486 			conf[1].msr = conf[1].mix_msr = conf[1].imp_msr = 1;
487 			conf[1].vo = 1;
488 			*n_srcc = apcm->substream->runtime->channels * 2;
489 		} else if (0x1000000 < pitch) {
490 			/* Need one-stage SRCs, SRCIMPs and
491 			 * AMIXERs for converting format */
492 			conf[0].pitch = pitch;
493 			conf[0].msr = conf[0].mix_msr
494 				    = conf[0].imp_msr = atc->msr;
495 			conf[0].vo = 1;
496 			*n_srcc = apcm->substream->runtime->channels;
497 		}
498 	}
499 }
500 
501 static int
502 atc_pcm_capture_get_resources(struct ct_atc *atc, struct ct_atc_pcm *apcm)
503 {
504 	struct src_mgr *src_mgr = atc->rsc_mgrs[SRC];
505 	struct srcimp_mgr *srcimp_mgr = atc->rsc_mgrs[SRCIMP];
506 	struct amixer_mgr *amixer_mgr = atc->rsc_mgrs[AMIXER];
507 	struct sum_mgr *sum_mgr = atc->rsc_mgrs[SUM];
508 	struct src_desc src_dsc = {0};
509 	struct src *src;
510 	struct srcimp_desc srcimp_dsc = {0};
511 	struct srcimp *srcimp;
512 	struct amixer_desc mix_dsc = {0};
513 	struct sum_desc sum_dsc = {0};
514 	unsigned int pitch;
515 	int multi, err, i;
516 	int n_srcimp, n_amixer, n_srcc, n_sum;
517 	struct src_node_conf_t src_node_conf[2] = {{0} };
518 
519 	/* first release old resources */
520 	atc_pcm_release_resources(atc, apcm);
521 
522 	/* The numbers of converting SRCs and SRCIMPs should be determined
523 	 * by pitch value. */
524 
525 	multi = apcm->substream->runtime->channels;
526 
527 	/* get pitch and convert to fixed-point 8.24 format. */
528 	pitch = atc_get_pitch((atc->rsr * atc->msr),
529 				apcm->substream->runtime->rate);
530 
531 	setup_src_node_conf(atc, apcm, src_node_conf, &n_srcc);
532 	n_sum = (1 == multi) ? 1 : 0;
533 	n_amixer = n_sum * 2 + n_srcc;
534 	n_srcimp = n_srcc;
535 	if ((multi > 1) && (0x8000000 >= pitch)) {
536 		/* Need extra AMIXERs and SRCIMPs for special treatment
537 		 * of interleaved recording of conjugate channels */
538 		n_amixer += multi * atc->msr;
539 		n_srcimp += multi * atc->msr;
540 	} else {
541 		n_srcimp += multi;
542 	}
543 
544 	if (n_srcc) {
545 		apcm->srccs = kcalloc(n_srcc, sizeof(void *), GFP_KERNEL);
546 		if (!apcm->srccs)
547 			return -ENOMEM;
548 	}
549 	if (n_amixer) {
550 		apcm->amixers = kcalloc(n_amixer, sizeof(void *), GFP_KERNEL);
551 		if (!apcm->amixers) {
552 			err = -ENOMEM;
553 			goto error1;
554 		}
555 	}
556 	apcm->srcimps = kcalloc(n_srcimp, sizeof(void *), GFP_KERNEL);
557 	if (!apcm->srcimps) {
558 		err = -ENOMEM;
559 		goto error1;
560 	}
561 
562 	/* Allocate SRCs for sample rate conversion if needed */
563 	src_dsc.multi = 1;
564 	src_dsc.mode = ARCRW;
565 	for (i = 0, apcm->n_srcc = 0; i < n_srcc; i++) {
566 		src_dsc.msr = src_node_conf[i/multi].msr;
567 		err = src_mgr->get_src(src_mgr, &src_dsc,
568 					(struct src **)&apcm->srccs[i]);
569 		if (err)
570 			goto error1;
571 
572 		src = apcm->srccs[i];
573 		pitch = src_node_conf[i/multi].pitch;
574 		src->ops->set_pitch(src, pitch);
575 		src->ops->set_rom(src, select_rom(pitch));
576 		src->ops->set_vo(src, src_node_conf[i/multi].vo);
577 
578 		apcm->n_srcc++;
579 	}
580 
581 	/* Allocate AMIXERs for routing SRCs of conversion if needed */
582 	for (i = 0, apcm->n_amixer = 0; i < n_amixer; i++) {
583 		if (i < (n_sum*2))
584 			mix_dsc.msr = atc->msr;
585 		else if (i < (n_sum*2+n_srcc))
586 			mix_dsc.msr = src_node_conf[(i-n_sum*2)/multi].mix_msr;
587 		else
588 			mix_dsc.msr = 1;
589 
590 		err = amixer_mgr->get_amixer(amixer_mgr, &mix_dsc,
591 					(struct amixer **)&apcm->amixers[i]);
592 		if (err)
593 			goto error1;
594 
595 		apcm->n_amixer++;
596 	}
597 
598 	/* Allocate a SUM resource to mix all input channels together */
599 	sum_dsc.msr = atc->msr;
600 	err = sum_mgr->get_sum(sum_mgr, &sum_dsc, (struct sum **)&apcm->mono);
601 	if (err)
602 		goto error1;
603 
604 	pitch = atc_get_pitch((atc->rsr * atc->msr),
605 				apcm->substream->runtime->rate);
606 	/* Allocate SRCIMP resources */
607 	for (i = 0, apcm->n_srcimp = 0; i < n_srcimp; i++) {
608 		if (i < (n_srcc))
609 			srcimp_dsc.msr = src_node_conf[i/multi].imp_msr;
610 		else if (1 == multi)
611 			srcimp_dsc.msr = (pitch <= 0x8000000) ? atc->msr : 1;
612 		else
613 			srcimp_dsc.msr = 1;
614 
615 		err = srcimp_mgr->get_srcimp(srcimp_mgr, &srcimp_dsc, &srcimp);
616 		if (err)
617 			goto error1;
618 
619 		apcm->srcimps[i] = srcimp;
620 		apcm->n_srcimp++;
621 	}
622 
623 	/* Allocate a SRC for writing data to host memory */
624 	src_dsc.multi = apcm->substream->runtime->channels;
625 	src_dsc.msr = 1;
626 	src_dsc.mode = MEMWR;
627 	err = src_mgr->get_src(src_mgr, &src_dsc, (struct src **)&apcm->src);
628 	if (err)
629 		goto error1;
630 
631 	src = apcm->src;
632 	src->ops->set_pitch(src, pitch);
633 
634 	/* Set up device virtual mem map */
635 	err = ct_map_audio_buffer(atc, apcm);
636 	if (err < 0)
637 		goto error1;
638 
639 	return 0;
640 
641 error1:
642 	atc_pcm_release_resources(atc, apcm);
643 	return err;
644 }
645 
646 static int atc_pcm_capture_prepare(struct ct_atc *atc, struct ct_atc_pcm *apcm)
647 {
648 	struct src *src;
649 	struct amixer *amixer;
650 	struct srcimp *srcimp;
651 	struct ct_mixer *mixer = atc->mixer;
652 	struct sum *mono;
653 	struct rsc *out_ports[8] = {NULL};
654 	int err, i, j, n_sum, multi;
655 	unsigned int pitch;
656 	int mix_base = 0, imp_base = 0;
657 
658 	atc_pcm_release_resources(atc, apcm);
659 
660 	/* Get needed resources. */
661 	err = atc_pcm_capture_get_resources(atc, apcm);
662 	if (err)
663 		return err;
664 
665 	/* Connect resources */
666 	mixer->get_output_ports(mixer, MIX_PCMO_FRONT,
667 				&out_ports[0], &out_ports[1]);
668 
669 	multi = apcm->substream->runtime->channels;
670 	if (1 == multi) {
671 		mono = apcm->mono;
672 		for (i = 0; i < 2; i++) {
673 			amixer = apcm->amixers[i];
674 			amixer->ops->setup(amixer, out_ports[i],
675 						MONO_SUM_SCALE, mono);
676 		}
677 		out_ports[0] = &mono->rsc;
678 		n_sum = 1;
679 		mix_base = n_sum * 2;
680 	}
681 
682 	for (i = 0; i < apcm->n_srcc; i++) {
683 		src = apcm->srccs[i];
684 		srcimp = apcm->srcimps[imp_base+i];
685 		amixer = apcm->amixers[mix_base+i];
686 		srcimp->ops->map(srcimp, src, out_ports[i%multi]);
687 		amixer->ops->setup(amixer, &src->rsc, INIT_VOL, NULL);
688 		out_ports[i%multi] = &amixer->rsc;
689 	}
690 
691 	pitch = atc_get_pitch((atc->rsr * atc->msr),
692 				apcm->substream->runtime->rate);
693 
694 	if ((multi > 1) && (pitch <= 0x8000000)) {
695 		/* Special connection for interleaved
696 		 * recording with conjugate channels */
697 		for (i = 0; i < multi; i++) {
698 			out_ports[i]->ops->master(out_ports[i]);
699 			for (j = 0; j < atc->msr; j++) {
700 				amixer = apcm->amixers[apcm->n_srcc+j*multi+i];
701 				amixer->ops->set_input(amixer, out_ports[i]);
702 				amixer->ops->set_scale(amixer, INIT_VOL);
703 				amixer->ops->set_sum(amixer, NULL);
704 				amixer->ops->commit_raw_write(amixer);
705 				out_ports[i]->ops->next_conj(out_ports[i]);
706 
707 				srcimp = apcm->srcimps[apcm->n_srcc+j*multi+i];
708 				srcimp->ops->map(srcimp, apcm->src,
709 							&amixer->rsc);
710 			}
711 		}
712 	} else {
713 		for (i = 0; i < multi; i++) {
714 			srcimp = apcm->srcimps[apcm->n_srcc+i];
715 			srcimp->ops->map(srcimp, apcm->src, out_ports[i]);
716 		}
717 	}
718 
719 	ct_timer_prepare(apcm->timer);
720 
721 	return 0;
722 }
723 
724 static int atc_pcm_capture_start(struct ct_atc *atc, struct ct_atc_pcm *apcm)
725 {
726 	struct src *src;
727 	struct src_mgr *src_mgr = atc->rsc_mgrs[SRC];
728 	int i, multi;
729 
730 	if (apcm->started)
731 		return 0;
732 
733 	apcm->started = 1;
734 	multi = apcm->substream->runtime->channels;
735 	/* Set up converting SRCs */
736 	for (i = 0; i < apcm->n_srcc; i++) {
737 		src = apcm->srccs[i];
738 		src->ops->set_pm(src, ((i%multi) != (multi-1)));
739 		src_mgr->src_disable(src_mgr, src);
740 	}
741 
742 	/*  Set up recording SRC */
743 	src = apcm->src;
744 	src->ops->set_sf(src, convert_format(apcm->substream->runtime->format,
745 					     atc->card));
746 	src->ops->set_sa(src, apcm->vm_block->addr);
747 	src->ops->set_la(src, apcm->vm_block->addr + apcm->vm_block->size);
748 	src->ops->set_ca(src, apcm->vm_block->addr);
749 	src_mgr->src_disable(src_mgr, src);
750 
751 	/* Disable relevant SRCs firstly */
752 	src_mgr->commit_write(src_mgr);
753 
754 	/* Enable SRCs respectively */
755 	for (i = 0; i < apcm->n_srcc; i++) {
756 		src = apcm->srccs[i];
757 		src->ops->set_state(src, SRC_STATE_RUN);
758 		src->ops->commit_write(src);
759 		src_mgr->src_enable_s(src_mgr, src);
760 	}
761 	src = apcm->src;
762 	src->ops->set_bm(src, 1);
763 	src->ops->set_state(src, SRC_STATE_RUN);
764 	src->ops->commit_write(src);
765 	src_mgr->src_enable_s(src_mgr, src);
766 
767 	/* Enable relevant SRCs synchronously */
768 	src_mgr->commit_write(src_mgr);
769 
770 	ct_timer_start(apcm->timer);
771 	return 0;
772 }
773 
774 static int
775 atc_pcm_capture_position(struct ct_atc *atc, struct ct_atc_pcm *apcm)
776 {
777 	struct src *src = apcm->src;
778 
779 	if (!src)
780 		return 0;
781 	return src->ops->get_ca(src) - apcm->vm_block->addr;
782 }
783 
784 static int spdif_passthru_playback_get_resources(struct ct_atc *atc,
785 						 struct ct_atc_pcm *apcm)
786 {
787 	struct src_mgr *src_mgr = atc->rsc_mgrs[SRC];
788 	struct amixer_mgr *amixer_mgr = atc->rsc_mgrs[AMIXER];
789 	struct src_desc desc = {0};
790 	struct amixer_desc mix_dsc = {0};
791 	struct src *src;
792 	int err;
793 	int n_amixer = apcm->substream->runtime->channels, i;
794 	unsigned int pitch, rsr = atc->pll_rate;
795 
796 	/* first release old resources */
797 	atc_pcm_release_resources(atc, apcm);
798 
799 	/* Get SRC resource */
800 	desc.multi = apcm->substream->runtime->channels;
801 	desc.msr = 1;
802 	while (apcm->substream->runtime->rate > (rsr * desc.msr))
803 		desc.msr <<= 1;
804 
805 	desc.mode = MEMRD;
806 	err = src_mgr->get_src(src_mgr, &desc, (struct src **)&apcm->src);
807 	if (err)
808 		goto error1;
809 
810 	pitch = atc_get_pitch(apcm->substream->runtime->rate, (rsr * desc.msr));
811 	src = apcm->src;
812 	src->ops->set_pitch(src, pitch);
813 	src->ops->set_rom(src, select_rom(pitch));
814 	src->ops->set_sf(src, convert_format(apcm->substream->runtime->format,
815 					     atc->card));
816 	src->ops->set_pm(src, (src->ops->next_interleave(src) != NULL));
817 	src->ops->set_bp(src, 1);
818 
819 	/* Get AMIXER resource */
820 	n_amixer = (n_amixer < 2) ? 2 : n_amixer;
821 	apcm->amixers = kcalloc(n_amixer, sizeof(void *), GFP_KERNEL);
822 	if (!apcm->amixers) {
823 		err = -ENOMEM;
824 		goto error1;
825 	}
826 	mix_dsc.msr = desc.msr;
827 	for (i = 0, apcm->n_amixer = 0; i < n_amixer; i++) {
828 		err = amixer_mgr->get_amixer(amixer_mgr, &mix_dsc,
829 					(struct amixer **)&apcm->amixers[i]);
830 		if (err)
831 			goto error1;
832 
833 		apcm->n_amixer++;
834 	}
835 
836 	/* Set up device virtual mem map */
837 	err = ct_map_audio_buffer(atc, apcm);
838 	if (err < 0)
839 		goto error1;
840 
841 	return 0;
842 
843 error1:
844 	atc_pcm_release_resources(atc, apcm);
845 	return err;
846 }
847 
848 static int atc_pll_init(struct ct_atc *atc, int rate)
849 {
850 	struct hw *hw = atc->hw;
851 	int err;
852 	err = hw->pll_init(hw, rate);
853 	atc->pll_rate = err ? 0 : rate;
854 	return err;
855 }
856 
857 static int
858 spdif_passthru_playback_setup(struct ct_atc *atc, struct ct_atc_pcm *apcm)
859 {
860 	struct dao *dao = container_of(atc->daios[SPDIFOO], struct dao, daio);
861 	unsigned int rate = apcm->substream->runtime->rate;
862 	unsigned int status;
863 	int err = 0;
864 	unsigned char iec958_con_fs;
865 
866 	switch (rate) {
867 	case 48000:
868 		iec958_con_fs = IEC958_AES3_CON_FS_48000;
869 		break;
870 	case 44100:
871 		iec958_con_fs = IEC958_AES3_CON_FS_44100;
872 		break;
873 	case 32000:
874 		iec958_con_fs = IEC958_AES3_CON_FS_32000;
875 		break;
876 	default:
877 		return -ENOENT;
878 	}
879 
880 	mutex_lock(&atc->atc_mutex);
881 	dao->ops->get_spos(dao, &status);
882 	if (((status >> 24) & IEC958_AES3_CON_FS) != iec958_con_fs) {
883 		status &= ~(IEC958_AES3_CON_FS << 24);
884 		status |= (iec958_con_fs << 24);
885 		dao->ops->set_spos(dao, status);
886 		dao->ops->commit_write(dao);
887 	}
888 	if ((rate != atc->pll_rate) && (32000 != rate))
889 		err = atc_pll_init(atc, rate);
890 	mutex_unlock(&atc->atc_mutex);
891 
892 	return err;
893 }
894 
895 static int
896 spdif_passthru_playback_prepare(struct ct_atc *atc, struct ct_atc_pcm *apcm)
897 {
898 	struct src *src;
899 	struct amixer *amixer;
900 	struct dao *dao;
901 	int err;
902 	int i;
903 
904 	atc_pcm_release_resources(atc, apcm);
905 
906 	/* Configure SPDIFOO and PLL to passthrough mode;
907 	 * determine pll_rate. */
908 	err = spdif_passthru_playback_setup(atc, apcm);
909 	if (err)
910 		return err;
911 
912 	/* Get needed resources. */
913 	err = spdif_passthru_playback_get_resources(atc, apcm);
914 	if (err)
915 		return err;
916 
917 	/* Connect resources */
918 	src = apcm->src;
919 	for (i = 0; i < apcm->n_amixer; i++) {
920 		amixer = apcm->amixers[i];
921 		amixer->ops->setup(amixer, &src->rsc, INIT_VOL, NULL);
922 		src = src->ops->next_interleave(src);
923 		if (!src)
924 			src = apcm->src;
925 	}
926 	/* Connect to SPDIFOO */
927 	mutex_lock(&atc->atc_mutex);
928 	dao = container_of(atc->daios[SPDIFOO], struct dao, daio);
929 	amixer = apcm->amixers[0];
930 	dao->ops->set_left_input(dao, &amixer->rsc);
931 	amixer = apcm->amixers[1];
932 	dao->ops->set_right_input(dao, &amixer->rsc);
933 	mutex_unlock(&atc->atc_mutex);
934 
935 	ct_timer_prepare(apcm->timer);
936 
937 	return 0;
938 }
939 
940 static int atc_select_line_in(struct ct_atc *atc)
941 {
942 	struct hw *hw = atc->hw;
943 	struct ct_mixer *mixer = atc->mixer;
944 	struct src *src;
945 
946 	if (hw->is_adc_source_selected(hw, ADC_LINEIN))
947 		return 0;
948 
949 	mixer->set_input_left(mixer, MIX_MIC_IN, NULL);
950 	mixer->set_input_right(mixer, MIX_MIC_IN, NULL);
951 
952 	hw->select_adc_source(hw, ADC_LINEIN);
953 
954 	src = atc->srcs[2];
955 	mixer->set_input_left(mixer, MIX_LINE_IN, &src->rsc);
956 	src = atc->srcs[3];
957 	mixer->set_input_right(mixer, MIX_LINE_IN, &src->rsc);
958 
959 	return 0;
960 }
961 
962 static int atc_select_mic_in(struct ct_atc *atc)
963 {
964 	struct hw *hw = atc->hw;
965 	struct ct_mixer *mixer = atc->mixer;
966 	struct src *src;
967 
968 	if (hw->is_adc_source_selected(hw, ADC_MICIN))
969 		return 0;
970 
971 	mixer->set_input_left(mixer, MIX_LINE_IN, NULL);
972 	mixer->set_input_right(mixer, MIX_LINE_IN, NULL);
973 
974 	hw->select_adc_source(hw, ADC_MICIN);
975 
976 	src = atc->srcs[2];
977 	mixer->set_input_left(mixer, MIX_MIC_IN, &src->rsc);
978 	src = atc->srcs[3];
979 	mixer->set_input_right(mixer, MIX_MIC_IN, &src->rsc);
980 
981 	return 0;
982 }
983 
984 static struct capabilities atc_capabilities(struct ct_atc *atc)
985 {
986 	struct hw *hw = atc->hw;
987 
988 	return hw->capabilities(hw);
989 }
990 
991 static int atc_output_switch_get(struct ct_atc *atc)
992 {
993 	struct hw *hw = atc->hw;
994 
995 	return hw->output_switch_get(hw);
996 }
997 
998 static int atc_output_switch_put(struct ct_atc *atc, int position)
999 {
1000 	struct hw *hw = atc->hw;
1001 
1002 	return hw->output_switch_put(hw, position);
1003 }
1004 
1005 static int atc_mic_source_switch_get(struct ct_atc *atc)
1006 {
1007 	struct hw *hw = atc->hw;
1008 
1009 	return hw->mic_source_switch_get(hw);
1010 }
1011 
1012 static int atc_mic_source_switch_put(struct ct_atc *atc, int position)
1013 {
1014 	struct hw *hw = atc->hw;
1015 
1016 	return hw->mic_source_switch_put(hw, position);
1017 }
1018 
1019 static int atc_select_digit_io(struct ct_atc *atc)
1020 {
1021 	struct hw *hw = atc->hw;
1022 
1023 	if (hw->is_adc_source_selected(hw, ADC_NONE))
1024 		return 0;
1025 
1026 	hw->select_adc_source(hw, ADC_NONE);
1027 
1028 	return 0;
1029 }
1030 
1031 static int atc_daio_unmute(struct ct_atc *atc, unsigned char state, int type)
1032 {
1033 	struct daio_mgr *daio_mgr = atc->rsc_mgrs[DAIO];
1034 
1035 	if (state)
1036 		daio_mgr->daio_enable(daio_mgr, atc->daios[type]);
1037 	else
1038 		daio_mgr->daio_disable(daio_mgr, atc->daios[type]);
1039 
1040 	daio_mgr->commit_write(daio_mgr);
1041 
1042 	return 0;
1043 }
1044 
1045 static int
1046 atc_dao_get_status(struct ct_atc *atc, unsigned int *status, int type)
1047 {
1048 	struct dao *dao = container_of(atc->daios[type], struct dao, daio);
1049 	return dao->ops->get_spos(dao, status);
1050 }
1051 
1052 static int
1053 atc_dao_set_status(struct ct_atc *atc, unsigned int status, int type)
1054 {
1055 	struct dao *dao = container_of(atc->daios[type], struct dao, daio);
1056 
1057 	dao->ops->set_spos(dao, status);
1058 	dao->ops->commit_write(dao);
1059 	return 0;
1060 }
1061 
1062 static int atc_line_front_unmute(struct ct_atc *atc, unsigned char state)
1063 {
1064 	return atc_daio_unmute(atc, state, LINEO1);
1065 }
1066 
1067 static int atc_line_surround_unmute(struct ct_atc *atc, unsigned char state)
1068 {
1069 	return atc_daio_unmute(atc, state, LINEO2);
1070 }
1071 
1072 static int atc_line_clfe_unmute(struct ct_atc *atc, unsigned char state)
1073 {
1074 	return atc_daio_unmute(atc, state, LINEO3);
1075 }
1076 
1077 static int atc_line_rear_unmute(struct ct_atc *atc, unsigned char state)
1078 {
1079 	return atc_daio_unmute(atc, state, LINEO4);
1080 }
1081 
1082 static int atc_line_in_unmute(struct ct_atc *atc, unsigned char state)
1083 {
1084 	return atc_daio_unmute(atc, state, LINEIM);
1085 }
1086 
1087 static int atc_mic_unmute(struct ct_atc *atc, unsigned char state)
1088 {
1089 	return atc_daio_unmute(atc, state, MIC);
1090 }
1091 
1092 static int atc_spdif_out_unmute(struct ct_atc *atc, unsigned char state)
1093 {
1094 	return atc_daio_unmute(atc, state, SPDIFOO);
1095 }
1096 
1097 static int atc_spdif_in_unmute(struct ct_atc *atc, unsigned char state)
1098 {
1099 	return atc_daio_unmute(atc, state, SPDIFIO);
1100 }
1101 
1102 static int atc_spdif_out_get_status(struct ct_atc *atc, unsigned int *status)
1103 {
1104 	return atc_dao_get_status(atc, status, SPDIFOO);
1105 }
1106 
1107 static int atc_spdif_out_set_status(struct ct_atc *atc, unsigned int status)
1108 {
1109 	return atc_dao_set_status(atc, status, SPDIFOO);
1110 }
1111 
1112 static int atc_spdif_out_passthru(struct ct_atc *atc, unsigned char state)
1113 {
1114 	struct dao_desc da_dsc = {0};
1115 	struct dao *dao;
1116 	int err;
1117 	struct ct_mixer *mixer = atc->mixer;
1118 	struct rsc *rscs[2] = {NULL};
1119 	unsigned int spos = 0;
1120 
1121 	mutex_lock(&atc->atc_mutex);
1122 	dao = container_of(atc->daios[SPDIFOO], struct dao, daio);
1123 	da_dsc.msr = state ? 1 : atc->msr;
1124 	da_dsc.passthru = state ? 1 : 0;
1125 	err = dao->ops->reinit(dao, &da_dsc);
1126 	if (state) {
1127 		spos = IEC958_DEFAULT_CON;
1128 	} else {
1129 		mixer->get_output_ports(mixer, MIX_SPDIF_OUT,
1130 					&rscs[0], &rscs[1]);
1131 		dao->ops->set_left_input(dao, rscs[0]);
1132 		dao->ops->set_right_input(dao, rscs[1]);
1133 		/* Restore PLL to atc->rsr if needed. */
1134 		if (atc->pll_rate != atc->rsr)
1135 			err = atc_pll_init(atc, atc->rsr);
1136 	}
1137 	dao->ops->set_spos(dao, spos);
1138 	dao->ops->commit_write(dao);
1139 	mutex_unlock(&atc->atc_mutex);
1140 
1141 	return err;
1142 }
1143 
1144 static int atc_release_resources(struct ct_atc *atc)
1145 {
1146 	int i;
1147 	struct daio_mgr *daio_mgr = NULL;
1148 	struct dao *dao = NULL;
1149 	struct daio *daio = NULL;
1150 	struct sum_mgr *sum_mgr = NULL;
1151 	struct src_mgr *src_mgr = NULL;
1152 	struct srcimp_mgr *srcimp_mgr = NULL;
1153 	struct srcimp *srcimp = NULL;
1154 	struct ct_mixer *mixer = NULL;
1155 
1156 	/* disconnect internal mixer objects */
1157 	if (atc->mixer) {
1158 		mixer = atc->mixer;
1159 		mixer->set_input_left(mixer, MIX_LINE_IN, NULL);
1160 		mixer->set_input_right(mixer, MIX_LINE_IN, NULL);
1161 		mixer->set_input_left(mixer, MIX_MIC_IN, NULL);
1162 		mixer->set_input_right(mixer, MIX_MIC_IN, NULL);
1163 		mixer->set_input_left(mixer, MIX_SPDIF_IN, NULL);
1164 		mixer->set_input_right(mixer, MIX_SPDIF_IN, NULL);
1165 	}
1166 
1167 	if (atc->daios) {
1168 		daio_mgr = (struct daio_mgr *)atc->rsc_mgrs[DAIO];
1169 		for (i = 0; i < atc->n_daio; i++) {
1170 			daio = atc->daios[i];
1171 			if (daio->type < LINEIM) {
1172 				dao = container_of(daio, struct dao, daio);
1173 				dao->ops->clear_left_input(dao);
1174 				dao->ops->clear_right_input(dao);
1175 			}
1176 			daio_mgr->put_daio(daio_mgr, daio);
1177 		}
1178 		kfree(atc->daios);
1179 		atc->daios = NULL;
1180 	}
1181 
1182 	if (atc->pcm) {
1183 		sum_mgr = atc->rsc_mgrs[SUM];
1184 		for (i = 0; i < atc->n_pcm; i++)
1185 			sum_mgr->put_sum(sum_mgr, atc->pcm[i]);
1186 
1187 		kfree(atc->pcm);
1188 		atc->pcm = NULL;
1189 	}
1190 
1191 	if (atc->srcs) {
1192 		src_mgr = atc->rsc_mgrs[SRC];
1193 		for (i = 0; i < atc->n_src; i++)
1194 			src_mgr->put_src(src_mgr, atc->srcs[i]);
1195 
1196 		kfree(atc->srcs);
1197 		atc->srcs = NULL;
1198 	}
1199 
1200 	if (atc->srcimps) {
1201 		srcimp_mgr = atc->rsc_mgrs[SRCIMP];
1202 		for (i = 0; i < atc->n_srcimp; i++) {
1203 			srcimp = atc->srcimps[i];
1204 			srcimp->ops->unmap(srcimp);
1205 			srcimp_mgr->put_srcimp(srcimp_mgr, atc->srcimps[i]);
1206 		}
1207 		kfree(atc->srcimps);
1208 		atc->srcimps = NULL;
1209 	}
1210 
1211 	return 0;
1212 }
1213 
1214 static int ct_atc_destroy(struct ct_atc *atc)
1215 {
1216 	int i = 0;
1217 
1218 	if (!atc)
1219 		return 0;
1220 
1221 	if (atc->timer) {
1222 		ct_timer_free(atc->timer);
1223 		atc->timer = NULL;
1224 	}
1225 
1226 	atc_release_resources(atc);
1227 
1228 	/* Destroy internal mixer objects */
1229 	if (atc->mixer)
1230 		ct_mixer_destroy(atc->mixer);
1231 
1232 	for (i = 0; i < NUM_RSCTYP; i++) {
1233 		if (rsc_mgr_funcs[i].destroy && atc->rsc_mgrs[i])
1234 			rsc_mgr_funcs[i].destroy(atc->rsc_mgrs[i]);
1235 
1236 	}
1237 
1238 	if (atc->hw)
1239 		destroy_hw_obj(atc->hw);
1240 
1241 	/* Destroy device virtual memory manager object */
1242 	if (atc->vm) {
1243 		ct_vm_destroy(atc->vm);
1244 		atc->vm = NULL;
1245 	}
1246 
1247 	kfree(atc);
1248 
1249 	return 0;
1250 }
1251 
1252 static int atc_dev_free(struct snd_device *dev)
1253 {
1254 	struct ct_atc *atc = dev->device_data;
1255 	return ct_atc_destroy(atc);
1256 }
1257 
1258 static int atc_identify_card(struct ct_atc *atc, unsigned int ssid)
1259 {
1260 	const struct snd_pci_quirk *p;
1261 	const struct snd_pci_quirk *list;
1262 	u16 vendor_id, device_id;
1263 
1264 	switch (atc->chip_type) {
1265 	case ATC20K1:
1266 		atc->chip_name = "20K1";
1267 		list = subsys_20k1_list;
1268 		break;
1269 	case ATC20K2:
1270 		atc->chip_name = "20K2";
1271 		list = subsys_20k2_list;
1272 		break;
1273 	default:
1274 		return -ENOENT;
1275 	}
1276 	if (ssid) {
1277 		vendor_id = ssid >> 16;
1278 		device_id = ssid & 0xffff;
1279 	} else {
1280 		vendor_id = atc->pci->subsystem_vendor;
1281 		device_id = atc->pci->subsystem_device;
1282 	}
1283 	p = snd_pci_quirk_lookup_id(vendor_id, device_id, list);
1284 	if (p) {
1285 		if (p->value < 0) {
1286 			dev_err(atc->card->dev,
1287 				"Device %04x:%04x is on the denylist\n",
1288 				vendor_id, device_id);
1289 			return -ENOENT;
1290 		}
1291 		atc->model = p->value;
1292 	} else {
1293 		if (atc->chip_type == ATC20K1)
1294 			atc->model = CT20K1_UNKNOWN;
1295 		else
1296 			atc->model = CT20K2_UNKNOWN;
1297 	}
1298 	atc->model_name = ct_subsys_name[atc->model];
1299 	dev_info(atc->card->dev, "chip %s model %s (%04x:%04x) is found\n",
1300 		   atc->chip_name, atc->model_name,
1301 		   vendor_id, device_id);
1302 	return 0;
1303 }
1304 
1305 int ct_atc_create_alsa_devs(struct ct_atc *atc)
1306 {
1307 	enum CTALSADEVS i;
1308 	int err;
1309 
1310 	alsa_dev_funcs[MIXER].public_name = atc->chip_name;
1311 
1312 	for (i = 0; i < NUM_CTALSADEVS; i++) {
1313 		if (!alsa_dev_funcs[i].create)
1314 			continue;
1315 
1316 		err = alsa_dev_funcs[i].create(atc, i,
1317 				alsa_dev_funcs[i].public_name);
1318 		if (err) {
1319 			dev_err(atc->card->dev,
1320 				"Creating alsa device %d failed!\n", i);
1321 			return err;
1322 		}
1323 	}
1324 
1325 	return 0;
1326 }
1327 
1328 static int atc_create_hw_devs(struct ct_atc *atc)
1329 {
1330 	struct hw *hw;
1331 	struct card_conf info = {0};
1332 	int i, err;
1333 
1334 	err = create_hw_obj(atc->pci, atc->chip_type, atc->model, &hw);
1335 	if (err) {
1336 		dev_err(atc->card->dev, "Failed to create hw obj!!!\n");
1337 		return err;
1338 	}
1339 	hw->card = atc->card;
1340 	atc->hw = hw;
1341 
1342 	/* Initialize card hardware. */
1343 	info.rsr = atc->rsr;
1344 	info.msr = atc->msr;
1345 	info.vm_pgt_phys = atc_get_ptp_phys(atc, 0);
1346 	err = hw->card_init(hw, &info);
1347 	if (err < 0)
1348 		return err;
1349 
1350 	for (i = 0; i < NUM_RSCTYP; i++) {
1351 		if (!rsc_mgr_funcs[i].create)
1352 			continue;
1353 
1354 		err = rsc_mgr_funcs[i].create(atc->hw, &atc->rsc_mgrs[i]);
1355 		if (err) {
1356 			dev_err(atc->card->dev,
1357 				"Failed to create rsc_mgr %d!!!\n", i);
1358 			return err;
1359 		}
1360 	}
1361 
1362 	return 0;
1363 }
1364 
1365 static int atc_get_resources(struct ct_atc *atc)
1366 {
1367 	struct daio_desc da_desc = {0};
1368 	struct daio_mgr *daio_mgr;
1369 	struct src_desc src_dsc = {0};
1370 	struct src_mgr *src_mgr;
1371 	struct srcimp_desc srcimp_dsc = {0};
1372 	struct srcimp_mgr *srcimp_mgr;
1373 	struct sum_desc sum_dsc = {0};
1374 	struct sum_mgr *sum_mgr;
1375 	int err, i, num_srcs, num_daios;
1376 
1377 	num_daios = ((atc->model == CTSB1270) ? 8 : 7);
1378 	num_srcs = ((atc->model == CTSB1270) ? 6 : 4);
1379 
1380 	atc->daios = kcalloc(num_daios, sizeof(void *), GFP_KERNEL);
1381 	if (!atc->daios)
1382 		return -ENOMEM;
1383 
1384 	atc->srcs = kcalloc(num_srcs, sizeof(void *), GFP_KERNEL);
1385 	if (!atc->srcs)
1386 		return -ENOMEM;
1387 
1388 	atc->srcimps = kcalloc(num_srcs, sizeof(void *), GFP_KERNEL);
1389 	if (!atc->srcimps)
1390 		return -ENOMEM;
1391 
1392 	atc->pcm = kcalloc(2 * 4, sizeof(void *), GFP_KERNEL);
1393 	if (!atc->pcm)
1394 		return -ENOMEM;
1395 
1396 	daio_mgr = (struct daio_mgr *)atc->rsc_mgrs[DAIO];
1397 	da_desc.msr = atc->msr;
1398 	for (i = 0, atc->n_daio = 0; i < num_daios; i++) {
1399 		da_desc.type = (atc->model != CTSB073X) ? i :
1400 			     ((i == SPDIFIO) ? SPDIFI1 : i);
1401 		err = daio_mgr->get_daio(daio_mgr, &da_desc,
1402 					(struct daio **)&atc->daios[i]);
1403 		if (err) {
1404 			dev_err(atc->card->dev,
1405 				"Failed to get DAIO resource %d!!!\n",
1406 				i);
1407 			return err;
1408 		}
1409 		atc->n_daio++;
1410 	}
1411 
1412 	src_mgr = atc->rsc_mgrs[SRC];
1413 	src_dsc.multi = 1;
1414 	src_dsc.msr = atc->msr;
1415 	src_dsc.mode = ARCRW;
1416 	for (i = 0, atc->n_src = 0; i < num_srcs; i++) {
1417 		err = src_mgr->get_src(src_mgr, &src_dsc,
1418 					(struct src **)&atc->srcs[i]);
1419 		if (err)
1420 			return err;
1421 
1422 		atc->n_src++;
1423 	}
1424 
1425 	srcimp_mgr = atc->rsc_mgrs[SRCIMP];
1426 	srcimp_dsc.msr = 8;
1427 	for (i = 0, atc->n_srcimp = 0; i < num_srcs; i++) {
1428 		err = srcimp_mgr->get_srcimp(srcimp_mgr, &srcimp_dsc,
1429 					(struct srcimp **)&atc->srcimps[i]);
1430 		if (err)
1431 			return err;
1432 
1433 		atc->n_srcimp++;
1434 	}
1435 
1436 	sum_mgr = atc->rsc_mgrs[SUM];
1437 	sum_dsc.msr = atc->msr;
1438 	for (i = 0, atc->n_pcm = 0; i < (2*4); i++) {
1439 		err = sum_mgr->get_sum(sum_mgr, &sum_dsc,
1440 					(struct sum **)&atc->pcm[i]);
1441 		if (err)
1442 			return err;
1443 
1444 		atc->n_pcm++;
1445 	}
1446 
1447 	return 0;
1448 }
1449 
1450 static void
1451 atc_connect_dai(struct src_mgr *src_mgr, struct dai *dai,
1452 		struct src **srcs, struct srcimp **srcimps)
1453 {
1454 	struct rsc *rscs[2] = {NULL};
1455 	struct src *src;
1456 	struct srcimp *srcimp;
1457 	int i = 0;
1458 
1459 	rscs[0] = &dai->daio.rscl;
1460 	rscs[1] = &dai->daio.rscr;
1461 	for (i = 0; i < 2; i++) {
1462 		src = srcs[i];
1463 		srcimp = srcimps[i];
1464 		srcimp->ops->map(srcimp, src, rscs[i]);
1465 		src_mgr->src_disable(src_mgr, src);
1466 	}
1467 
1468 	src_mgr->commit_write(src_mgr); /* Actually disable SRCs */
1469 
1470 	src = srcs[0];
1471 	src->ops->set_pm(src, 1);
1472 	for (i = 0; i < 2; i++) {
1473 		src = srcs[i];
1474 		src->ops->set_state(src, SRC_STATE_RUN);
1475 		src->ops->commit_write(src);
1476 		src_mgr->src_enable_s(src_mgr, src);
1477 	}
1478 
1479 	dai->ops->set_srt_srcl(dai, &(srcs[0]->rsc));
1480 	dai->ops->set_srt_srcr(dai, &(srcs[1]->rsc));
1481 
1482 	dai->ops->set_enb_src(dai, 1);
1483 	dai->ops->set_enb_srt(dai, 1);
1484 	dai->ops->commit_write(dai);
1485 
1486 	src_mgr->commit_write(src_mgr); /* Synchronously enable SRCs */
1487 }
1488 
1489 static void atc_connect_resources(struct ct_atc *atc)
1490 {
1491 	struct dai *dai;
1492 	struct dao *dao;
1493 	struct src *src;
1494 	struct sum *sum;
1495 	struct ct_mixer *mixer;
1496 	struct rsc *rscs[2] = {NULL};
1497 	int i, j;
1498 
1499 	mixer = atc->mixer;
1500 
1501 	for (i = MIX_WAVE_FRONT, j = LINEO1; i <= MIX_SPDIF_OUT; i++, j++) {
1502 		mixer->get_output_ports(mixer, i, &rscs[0], &rscs[1]);
1503 		dao = container_of(atc->daios[j], struct dao, daio);
1504 		dao->ops->set_left_input(dao, rscs[0]);
1505 		dao->ops->set_right_input(dao, rscs[1]);
1506 	}
1507 
1508 	dai = container_of(atc->daios[LINEIM], struct dai, daio);
1509 	atc_connect_dai(atc->rsc_mgrs[SRC], dai,
1510 			(struct src **)&atc->srcs[2],
1511 			(struct srcimp **)&atc->srcimps[2]);
1512 	src = atc->srcs[2];
1513 	mixer->set_input_left(mixer, MIX_LINE_IN, &src->rsc);
1514 	src = atc->srcs[3];
1515 	mixer->set_input_right(mixer, MIX_LINE_IN, &src->rsc);
1516 
1517 	if (atc->model == CTSB1270) {
1518 		/* Titanium HD has a dedicated ADC for the Mic. */
1519 		dai = container_of(atc->daios[MIC], struct dai, daio);
1520 		atc_connect_dai(atc->rsc_mgrs[SRC], dai,
1521 			(struct src **)&atc->srcs[4],
1522 			(struct srcimp **)&atc->srcimps[4]);
1523 		src = atc->srcs[4];
1524 		mixer->set_input_left(mixer, MIX_MIC_IN, &src->rsc);
1525 		src = atc->srcs[5];
1526 		mixer->set_input_right(mixer, MIX_MIC_IN, &src->rsc);
1527 	}
1528 
1529 	dai = container_of(atc->daios[SPDIFIO], struct dai, daio);
1530 	atc_connect_dai(atc->rsc_mgrs[SRC], dai,
1531 			(struct src **)&atc->srcs[0],
1532 			(struct srcimp **)&atc->srcimps[0]);
1533 
1534 	src = atc->srcs[0];
1535 	mixer->set_input_left(mixer, MIX_SPDIF_IN, &src->rsc);
1536 	src = atc->srcs[1];
1537 	mixer->set_input_right(mixer, MIX_SPDIF_IN, &src->rsc);
1538 
1539 	for (i = MIX_PCMI_FRONT, j = 0; i <= MIX_PCMI_SURROUND; i++, j += 2) {
1540 		sum = atc->pcm[j];
1541 		mixer->set_input_left(mixer, i, &sum->rsc);
1542 		sum = atc->pcm[j+1];
1543 		mixer->set_input_right(mixer, i, &sum->rsc);
1544 	}
1545 }
1546 
1547 #ifdef CONFIG_PM_SLEEP
1548 static int atc_suspend(struct ct_atc *atc)
1549 {
1550 	struct hw *hw = atc->hw;
1551 
1552 	snd_power_change_state(atc->card, SNDRV_CTL_POWER_D3hot);
1553 
1554 	atc_release_resources(atc);
1555 
1556 	hw->suspend(hw);
1557 
1558 	return 0;
1559 }
1560 
1561 static int atc_hw_resume(struct ct_atc *atc)
1562 {
1563 	struct hw *hw = atc->hw;
1564 	struct card_conf info = {0};
1565 
1566 	/* Re-initialize card hardware. */
1567 	info.rsr = atc->rsr;
1568 	info.msr = atc->msr;
1569 	info.vm_pgt_phys = atc_get_ptp_phys(atc, 0);
1570 	return hw->resume(hw, &info);
1571 }
1572 
1573 static int atc_resources_resume(struct ct_atc *atc)
1574 {
1575 	struct ct_mixer *mixer;
1576 	int err = 0;
1577 
1578 	/* Get resources */
1579 	err = atc_get_resources(atc);
1580 	if (err < 0) {
1581 		atc_release_resources(atc);
1582 		return err;
1583 	}
1584 
1585 	/* Build topology */
1586 	atc_connect_resources(atc);
1587 
1588 	mixer = atc->mixer;
1589 	mixer->resume(mixer);
1590 
1591 	return 0;
1592 }
1593 
1594 static int atc_resume(struct ct_atc *atc)
1595 {
1596 	int err = 0;
1597 
1598 	/* Do hardware resume. */
1599 	err = atc_hw_resume(atc);
1600 	if (err < 0) {
1601 		dev_err(atc->card->dev,
1602 			"pci_enable_device failed, disabling device\n");
1603 		snd_card_disconnect(atc->card);
1604 		return err;
1605 	}
1606 
1607 	err = atc_resources_resume(atc);
1608 	if (err < 0)
1609 		return err;
1610 
1611 	snd_power_change_state(atc->card, SNDRV_CTL_POWER_D0);
1612 
1613 	return 0;
1614 }
1615 #endif
1616 
1617 static const struct ct_atc atc_preset = {
1618 	.map_audio_buffer = ct_map_audio_buffer,
1619 	.unmap_audio_buffer = ct_unmap_audio_buffer,
1620 	.pcm_playback_prepare = atc_pcm_playback_prepare,
1621 	.pcm_release_resources = atc_pcm_release_resources,
1622 	.pcm_playback_start = atc_pcm_playback_start,
1623 	.pcm_playback_stop = atc_pcm_stop,
1624 	.pcm_playback_position = atc_pcm_playback_position,
1625 	.pcm_capture_prepare = atc_pcm_capture_prepare,
1626 	.pcm_capture_start = atc_pcm_capture_start,
1627 	.pcm_capture_stop = atc_pcm_stop,
1628 	.pcm_capture_position = atc_pcm_capture_position,
1629 	.spdif_passthru_playback_prepare = spdif_passthru_playback_prepare,
1630 	.get_ptp_phys = atc_get_ptp_phys,
1631 	.select_line_in = atc_select_line_in,
1632 	.select_mic_in = atc_select_mic_in,
1633 	.select_digit_io = atc_select_digit_io,
1634 	.line_front_unmute = atc_line_front_unmute,
1635 	.line_surround_unmute = atc_line_surround_unmute,
1636 	.line_clfe_unmute = atc_line_clfe_unmute,
1637 	.line_rear_unmute = atc_line_rear_unmute,
1638 	.line_in_unmute = atc_line_in_unmute,
1639 	.mic_unmute = atc_mic_unmute,
1640 	.spdif_out_unmute = atc_spdif_out_unmute,
1641 	.spdif_in_unmute = atc_spdif_in_unmute,
1642 	.spdif_out_get_status = atc_spdif_out_get_status,
1643 	.spdif_out_set_status = atc_spdif_out_set_status,
1644 	.spdif_out_passthru = atc_spdif_out_passthru,
1645 	.capabilities = atc_capabilities,
1646 	.output_switch_get = atc_output_switch_get,
1647 	.output_switch_put = atc_output_switch_put,
1648 	.mic_source_switch_get = atc_mic_source_switch_get,
1649 	.mic_source_switch_put = atc_mic_source_switch_put,
1650 #ifdef CONFIG_PM_SLEEP
1651 	.suspend = atc_suspend,
1652 	.resume = atc_resume,
1653 #endif
1654 };
1655 
1656 /**
1657  *  ct_atc_create - create and initialize a hardware manager
1658  *  @card: corresponding alsa card object
1659  *  @pci: corresponding kernel pci device object
1660  *  @rsr: reference sampling rate
1661  *  @msr: master sampling rate
1662  *  @chip_type: CHIPTYP enum values
1663  *  @ssid: vendor ID (upper 16 bits) and device ID (lower 16 bits)
1664  *  @ratc: return created object address in it
1665  *
1666  *  Creates and initializes a hardware manager.
1667  *
1668  *  Creates kmallocated ct_atc structure. Initializes hardware.
1669  *  Returns 0 if succeeds, or negative error code if fails.
1670  */
1671 
1672 int ct_atc_create(struct snd_card *card, struct pci_dev *pci,
1673 		  unsigned int rsr, unsigned int msr,
1674 		  int chip_type, unsigned int ssid,
1675 		  struct ct_atc **ratc)
1676 {
1677 	struct ct_atc *atc;
1678 	static const struct snd_device_ops ops = {
1679 		.dev_free = atc_dev_free,
1680 	};
1681 	int err;
1682 
1683 	*ratc = NULL;
1684 
1685 	atc = kzalloc(sizeof(*atc), GFP_KERNEL);
1686 	if (!atc)
1687 		return -ENOMEM;
1688 
1689 	/* Set operations */
1690 	*atc = atc_preset;
1691 
1692 	atc->card = card;
1693 	atc->pci = pci;
1694 	atc->rsr = rsr;
1695 	atc->msr = msr;
1696 	atc->chip_type = chip_type;
1697 
1698 	mutex_init(&atc->atc_mutex);
1699 
1700 	/* Find card model */
1701 	err = atc_identify_card(atc, ssid);
1702 	if (err < 0) {
1703 		dev_err(card->dev, "ctatc: Card not recognised\n");
1704 		goto error1;
1705 	}
1706 
1707 	/* Set up device virtual memory management object */
1708 	err = ct_vm_create(&atc->vm, pci);
1709 	if (err < 0)
1710 		goto error1;
1711 
1712 	/* Create all atc hw devices */
1713 	err = atc_create_hw_devs(atc);
1714 	if (err < 0)
1715 		goto error1;
1716 
1717 	err = ct_mixer_create(atc, (struct ct_mixer **)&atc->mixer);
1718 	if (err) {
1719 		dev_err(card->dev, "Failed to create mixer obj!!!\n");
1720 		goto error1;
1721 	}
1722 
1723 	/* Get resources */
1724 	err = atc_get_resources(atc);
1725 	if (err < 0)
1726 		goto error1;
1727 
1728 	/* Build topology */
1729 	atc_connect_resources(atc);
1730 
1731 	atc->timer = ct_timer_new(atc);
1732 	if (!atc->timer) {
1733 		err = -ENOMEM;
1734 		goto error1;
1735 	}
1736 
1737 	err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, atc, &ops);
1738 	if (err < 0)
1739 		goto error1;
1740 
1741 	*ratc = atc;
1742 	return 0;
1743 
1744 error1:
1745 	ct_atc_destroy(atc);
1746 	dev_err(card->dev, "Something wrong!!!\n");
1747 	return err;
1748 }
1749