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