blob: 952fd94c266608e7bd8a202dd1879b2363ad08fe [file] [log] [blame]
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001/**
2 * Copyright (C) 2008, Creative Technology Ltd. All Rights Reserved.
3 *
4 * This source file is released under GPL v2 license (no other versions).
5 * See the COPYING file included in the main directory of this source
6 * distribution for the license terms and conditions.
7 *
8 * @File ctatc.c
9 *
10 * @Brief
11 * This file contains the implementation of the device resource management
12 * object.
13 *
14 * @Author Liu Chun
15 * @Date Mar 28 2008
16 */
17
18#include "ctatc.h"
19#include "ctpcm.h"
20#include "ctmixer.h"
Wai Yew CHAY8cc72362009-05-14 08:05:58 +020021#include "cthardware.h"
22#include "ctsrc.h"
23#include "ctamixer.h"
24#include "ctdaio.h"
Takashi Iwaib7bbf872009-06-05 16:11:07 +020025#include "cttimer.h"
Wai Yew CHAY8cc72362009-05-14 08:05:58 +020026#include <linux/delay.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090027#include <linux/slab.h>
Wai Yew CHAY8cc72362009-05-14 08:05:58 +020028#include <sound/pcm.h>
29#include <sound/control.h>
30#include <sound/asoundef.h>
31
32#define MONO_SUM_SCALE 0x19a8 /* 2^(-0.5) in 14-bit floating format */
Wai Yew CHAY8cc72362009-05-14 08:05:58 +020033#define MAX_MULTI_CHN 8
34
35#define IEC958_DEFAULT_CON ((IEC958_AES0_NONAUDIO \
36 | IEC958_AES0_CON_NOT_COPYRIGHT) \
37 | ((IEC958_AES1_CON_MIXER \
38 | IEC958_AES1_CON_ORIGINAL) << 8) \
39 | (0x10 << 16) \
40 | ((IEC958_AES3_CON_FS_48000) << 24))
41
Takashi Iwai94701952009-06-08 18:10:32 +020042static struct snd_pci_quirk __devinitdata subsys_20k1_list[] = {
43 SND_PCI_QUIRK(PCI_VENDOR_ID_CREATIVE, 0x0022, "SB055x", CTSB055X),
44 SND_PCI_QUIRK(PCI_VENDOR_ID_CREATIVE, 0x002f, "SB055x", CTSB055X),
45 SND_PCI_QUIRK(PCI_VENDOR_ID_CREATIVE, 0x0029, "SB073x", CTSB073X),
46 SND_PCI_QUIRK(PCI_VENDOR_ID_CREATIVE, 0x0031, "SB073x", CTSB073X),
Takashi Iwai09521d22009-06-08 18:29:38 +020047 SND_PCI_QUIRK_MASK(PCI_VENDOR_ID_CREATIVE, 0xf000, 0x6000,
48 "UAA", CTUAA),
Takashi Iwai94701952009-06-08 18:10:32 +020049 { } /* terminator */
Wai Yew CHAY8cc72362009-05-14 08:05:58 +020050};
51
Takashi Iwai94701952009-06-08 18:10:32 +020052static struct snd_pci_quirk __devinitdata subsys_20k2_list[] = {
53 SND_PCI_QUIRK(PCI_VENDOR_ID_CREATIVE, PCI_SUBDEVICE_ID_CREATIVE_SB0760,
54 "SB0760", CTSB0760),
Harry Butterworth55309212011-06-11 16:02:06 +080055 SND_PCI_QUIRK(PCI_VENDOR_ID_CREATIVE, PCI_SUBDEVICE_ID_CREATIVE_SB1270,
56 "SB1270", CTSB1270),
Takashi Iwai94701952009-06-08 18:10:32 +020057 SND_PCI_QUIRK(PCI_VENDOR_ID_CREATIVE, PCI_SUBDEVICE_ID_CREATIVE_SB08801,
58 "SB0880", CTSB0880),
59 SND_PCI_QUIRK(PCI_VENDOR_ID_CREATIVE, PCI_SUBDEVICE_ID_CREATIVE_SB08802,
60 "SB0880", CTSB0880),
61 SND_PCI_QUIRK(PCI_VENDOR_ID_CREATIVE, PCI_SUBDEVICE_ID_CREATIVE_SB08803,
62 "SB0880", CTSB0880),
Takashi Iwai09521d22009-06-08 18:29:38 +020063 SND_PCI_QUIRK_MASK(PCI_VENDOR_ID_CREATIVE, 0xf000,
64 PCI_SUBDEVICE_ID_CREATIVE_HENDRIX, "HENDRIX",
65 CTHENDRIX),
Takashi Iwai94701952009-06-08 18:10:32 +020066 { } /* terminator */
67};
68
69static const char *ct_subsys_name[NUM_CTCARDS] = {
Takashi Iwaia8f43102009-06-22 07:36:52 +020070 /* 20k1 models */
Takashi Iwai94701952009-06-08 18:10:32 +020071 [CTSB055X] = "SB055x",
72 [CTSB073X] = "SB073x",
Takashi Iwai94701952009-06-08 18:10:32 +020073 [CTUAA] = "UAA",
74 [CT20K1_UNKNOWN] = "Unknown",
Takashi Iwaia8f43102009-06-22 07:36:52 +020075 /* 20k2 models */
76 [CTSB0760] = "SB076x",
Takashi Iwai94701952009-06-08 18:10:32 +020077 [CTHENDRIX] = "Hendrix",
78 [CTSB0880] = "SB0880",
Harry Butterworth55309212011-06-11 16:02:06 +080079 [CTSB1270] = "SB1270",
Takashi Iwaia8f43102009-06-22 07:36:52 +020080 [CT20K2_UNKNOWN] = "Unknown",
Wai Yew CHAY8cc72362009-05-14 08:05:58 +020081};
82
83static struct {
84 int (*create)(struct ct_atc *atc,
85 enum CTALSADEVS device, const char *device_name);
86 int (*destroy)(void *alsa_dev);
87 const char *public_name;
88} alsa_dev_funcs[NUM_CTALSADEVS] = {
89 [FRONT] = { .create = ct_alsa_pcm_create,
90 .destroy = NULL,
91 .public_name = "Front/WaveIn"},
Wai Yew CHAY8cc72362009-05-14 08:05:58 +020092 [SURROUND] = { .create = ct_alsa_pcm_create,
93 .destroy = NULL,
94 .public_name = "Surround"},
Takashi Iwai8372d4982009-06-02 14:27:56 +020095 [CLFE] = { .create = ct_alsa_pcm_create,
96 .destroy = NULL,
97 .public_name = "Center/LFE"},
98 [SIDE] = { .create = ct_alsa_pcm_create,
99 .destroy = NULL,
100 .public_name = "Side"},
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200101 [IEC958] = { .create = ct_alsa_pcm_create,
102 .destroy = NULL,
103 .public_name = "IEC958 Non-audio"},
104
105 [MIXER] = { .create = ct_alsa_mix_create,
106 .destroy = NULL,
107 .public_name = "Mixer"}
108};
109
110typedef int (*create_t)(void *, void **);
111typedef int (*destroy_t)(void *);
112
113static struct {
114 int (*create)(void *hw, void **rmgr);
115 int (*destroy)(void *mgr);
116} rsc_mgr_funcs[NUM_RSCTYP] = {
117 [SRC] = { .create = (create_t)src_mgr_create,
118 .destroy = (destroy_t)src_mgr_destroy },
119 [SRCIMP] = { .create = (create_t)srcimp_mgr_create,
120 .destroy = (destroy_t)srcimp_mgr_destroy },
121 [AMIXER] = { .create = (create_t)amixer_mgr_create,
122 .destroy = (destroy_t)amixer_mgr_destroy },
123 [SUM] = { .create = (create_t)sum_mgr_create,
124 .destroy = (destroy_t)sum_mgr_destroy },
125 [DAIO] = { .create = (create_t)daio_mgr_create,
126 .destroy = (destroy_t)daio_mgr_destroy }
127};
128
129static int
130atc_pcm_release_resources(struct ct_atc *atc, struct ct_atc_pcm *apcm);
131
132/* *
133 * Only mono and interleaved modes are supported now.
134 * Always allocates a contiguous channel block.
135 * */
136
137static int ct_map_audio_buffer(struct ct_atc *atc, struct ct_atc_pcm *apcm)
138{
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200139 struct snd_pcm_runtime *runtime;
140 struct ct_vm *vm;
141
Takashi Iwai35ebf6e2009-07-22 17:12:34 +0200142 if (!apcm->substream)
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200143 return 0;
144
145 runtime = apcm->substream->runtime;
146 vm = atc->vm;
147
Takashi Iwaic76157d2009-06-02 15:26:19 +0200148 apcm->vm_block = vm->map(vm, apcm->substream, runtime->dma_bytes);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200149
Takashi Iwai35ebf6e2009-07-22 17:12:34 +0200150 if (!apcm->vm_block)
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200151 return -ENOENT;
152
153 return 0;
154}
155
156static void ct_unmap_audio_buffer(struct ct_atc *atc, struct ct_atc_pcm *apcm)
157{
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200158 struct ct_vm *vm;
159
Takashi Iwai35ebf6e2009-07-22 17:12:34 +0200160 if (!apcm->vm_block)
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200161 return;
162
163 vm = atc->vm;
164
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200165 vm->unmap(vm, apcm->vm_block);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200166
167 apcm->vm_block = NULL;
168}
169
170static unsigned long atc_get_ptp_phys(struct ct_atc *atc, int index)
171{
Jaroslav Kysela21956b62010-02-02 19:58:25 +0100172 return atc->vm->get_ptp_phys(atc->vm, index);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200173}
174
175static unsigned int convert_format(snd_pcm_format_t snd_format)
176{
177 switch (snd_format) {
178 case SNDRV_PCM_FORMAT_U8:
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200179 return SRC_SF_U8;
180 case SNDRV_PCM_FORMAT_S16_LE:
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200181 return SRC_SF_S16;
182 case SNDRV_PCM_FORMAT_S24_3LE:
183 return SRC_SF_S24;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200184 case SNDRV_PCM_FORMAT_S32_LE:
185 return SRC_SF_S32;
Takashi Iwaid2b9b962009-06-02 14:39:05 +0200186 case SNDRV_PCM_FORMAT_FLOAT_LE:
187 return SRC_SF_F32;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200188 default:
Takashi Iwaib3e0afe2009-05-14 15:19:30 +0200189 printk(KERN_ERR "ctxfi: not recognized snd format is %d \n",
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200190 snd_format);
191 return SRC_SF_S16;
192 }
193}
194
195static unsigned int
196atc_get_pitch(unsigned int input_rate, unsigned int output_rate)
197{
Takashi Iwai514eef92009-06-08 14:57:57 +0200198 unsigned int pitch;
199 int b;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200200
201 /* get pitch and convert to fixed-point 8.24 format. */
202 pitch = (input_rate / output_rate) << 24;
203 input_rate %= output_rate;
204 input_rate /= 100;
205 output_rate /= 100;
206 for (b = 31; ((b >= 0) && !(input_rate >> b)); )
207 b--;
208
209 if (b >= 0) {
210 input_rate <<= (31 - b);
211 input_rate /= output_rate;
212 b = 24 - (31 - b);
213 if (b >= 0)
214 input_rate <<= b;
215 else
216 input_rate >>= -b;
217
218 pitch |= input_rate;
219 }
220
221 return pitch;
222}
223
224static int select_rom(unsigned int pitch)
225{
Takashi Iwai35ebf6e2009-07-22 17:12:34 +0200226 if (pitch > 0x00428f5c && pitch < 0x01b851ec) {
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200227 /* 0.26 <= pitch <= 1.72 */
228 return 1;
Takashi Iwai35ebf6e2009-07-22 17:12:34 +0200229 } else if (pitch == 0x01d66666 || pitch == 0x01d66667) {
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200230 /* pitch == 1.8375 */
231 return 2;
Takashi Iwai35ebf6e2009-07-22 17:12:34 +0200232 } else if (pitch == 0x02000000) {
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200233 /* pitch == 2 */
234 return 3;
Roel Kluin84ed1a12009-10-23 16:03:08 +0200235 } else if (pitch <= 0x08000000) {
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200236 /* 0 <= pitch <= 8 */
237 return 0;
238 } else {
239 return -ENOENT;
240 }
241}
242
243static int atc_pcm_playback_prepare(struct ct_atc *atc, struct ct_atc_pcm *apcm)
244{
245 struct src_mgr *src_mgr = atc->rsc_mgrs[SRC];
246 struct amixer_mgr *amixer_mgr = atc->rsc_mgrs[AMIXER];
247 struct src_desc desc = {0};
248 struct amixer_desc mix_dsc = {0};
Takashi Iwai514eef92009-06-08 14:57:57 +0200249 struct src *src;
250 struct amixer *amixer;
251 int err;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200252 int n_amixer = apcm->substream->runtime->channels, i = 0;
253 int device = apcm->substream->pcm->device;
Takashi Iwai514eef92009-06-08 14:57:57 +0200254 unsigned int pitch;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200255
Takashi Iwai822fa192009-06-02 14:12:17 +0200256 /* first release old resources */
Wai Yew CHAY29959a02009-06-22 14:52:34 +0200257 atc_pcm_release_resources(atc, apcm);
Takashi Iwai822fa192009-06-02 14:12:17 +0200258
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200259 /* Get SRC resource */
260 desc.multi = apcm->substream->runtime->channels;
261 desc.msr = atc->msr;
262 desc.mode = MEMRD;
263 err = src_mgr->get_src(src_mgr, &desc, (struct src **)&apcm->src);
264 if (err)
265 goto error1;
266
267 pitch = atc_get_pitch(apcm->substream->runtime->rate,
268 (atc->rsr * atc->msr));
269 src = apcm->src;
270 src->ops->set_pitch(src, pitch);
271 src->ops->set_rom(src, select_rom(pitch));
272 src->ops->set_sf(src, convert_format(apcm->substream->runtime->format));
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 = kzalloc(sizeof(void *)*n_amixer, GFP_KERNEL);
Takashi Iwai35ebf6e2009-07-22 17:12:34 +0200278 if (!apcm->amixers) {
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200279 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];
Takashi Iwai635c2652009-06-13 10:12:59 +0200301 mutex_lock(&atc->atc_mutex);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200302 amixer->ops->setup(amixer, &src->rsc,
303 INIT_VOL, atc->pcm[i+device*2]);
Takashi Iwai635c2652009-06-13 10:12:59 +0200304 mutex_unlock(&atc->atc_mutex);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200305 src = src->ops->next_interleave(src);
Takashi Iwai35ebf6e2009-07-22 17:12:34 +0200306 if (!src)
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200307 src = apcm->src;
308 }
309
Takashi Iwaib7bbf872009-06-05 16:11:07 +0200310 ct_timer_prepare(apcm->timer);
311
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200312 return 0;
313
314error1:
315 atc_pcm_release_resources(atc, apcm);
316 return err;
317}
318
319static int
320atc_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];
Takashi Iwai514eef92009-06-08 14:57:57 +0200326 struct srcimp *srcimp;
327 int i;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200328
Takashi Iwai35ebf6e2009-07-22 17:12:34 +0200329 if (apcm->srcimps) {
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200330 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
Takashi Iwai35ebf6e2009-07-22 17:12:34 +0200340 if (apcm->srccs) {
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200341 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
Takashi Iwai35ebf6e2009-07-22 17:12:34 +0200349 if (apcm->amixers) {
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200350 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
Takashi Iwai35ebf6e2009-07-22 17:12:34 +0200358 if (apcm->mono) {
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200359 sum_mgr->put_sum(sum_mgr, apcm->mono);
360 apcm->mono = NULL;
361 }
362
Takashi Iwai35ebf6e2009-07-22 17:12:34 +0200363 if (apcm->src) {
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200364 src_mgr->put_src(src_mgr, apcm->src);
365 apcm->src = NULL;
366 }
367
Takashi Iwai35ebf6e2009-07-22 17:12:34 +0200368 if (apcm->vm_block) {
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200369 /* Undo device virtual mem map */
370 ct_unmap_audio_buffer(atc, apcm);
371 apcm->vm_block = NULL;
372 }
373
374 return 0;
375}
376
377static int atc_pcm_playback_start(struct ct_atc *atc, struct ct_atc_pcm *apcm)
378{
Takashi Iwai514eef92009-06-08 14:57:57 +0200379 unsigned int max_cisz;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200380 struct src *src = apcm->src;
381
Takashi Iwaic399f3b2009-06-09 08:16:20 +0200382 if (apcm->started)
383 return 0;
384 apcm->started = 1;
385
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200386 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
Takashi Iwaib7bbf872009-06-05 16:11:07 +0200398 ct_timer_start(apcm->timer);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200399 return 0;
400}
401
402static int atc_pcm_stop(struct ct_atc *atc, struct ct_atc_pcm *apcm)
403{
Takashi Iwai514eef92009-06-08 14:57:57 +0200404 struct src *src;
405 int i;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200406
Takashi Iwaib7bbf872009-06-05 16:11:07 +0200407 ct_timer_stop(apcm->timer);
408
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200409 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
Takashi Iwai35ebf6e2009-07-22 17:12:34 +0200414 if (apcm->srccs) {
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200415 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
428static int
429atc_pcm_playback_position(struct ct_atc *atc, struct ct_atc_pcm *apcm)
430{
431 struct src *src = apcm->src;
Takashi Iwai514eef92009-06-08 14:57:57 +0200432 u32 size, max_cisz;
433 int position;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200434
Takashi Iwai5242bc72009-06-09 08:17:14 +0200435 if (!src)
436 return 0;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200437 position = src->ops->get_ca(src);
438
439 size = apcm->vm_block->size;
440 max_cisz = src->multi * src->rsc.msr;
441 max_cisz = 128 * (max_cisz < 8 ? max_cisz : 8);
442
443 return (position + size - max_cisz - apcm->vm_block->addr) % size;
444}
445
446struct src_node_conf_t {
447 unsigned int pitch;
448 unsigned int msr:8;
449 unsigned int mix_msr:8;
450 unsigned int imp_msr:8;
451 unsigned int vo:1;
452};
453
454static void setup_src_node_conf(struct ct_atc *atc, struct ct_atc_pcm *apcm,
455 struct src_node_conf_t *conf, int *n_srcc)
456{
Takashi Iwai514eef92009-06-08 14:57:57 +0200457 unsigned int pitch;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200458
459 /* get pitch and convert to fixed-point 8.24 format. */
460 pitch = atc_get_pitch((atc->rsr * atc->msr),
461 apcm->substream->runtime->rate);
462 *n_srcc = 0;
463
Harry Butterworth55309212011-06-11 16:02:06 +0800464 if (1 == atc->msr) { /* FIXME: do we really need SRC here if pitch==1 */
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200465 *n_srcc = apcm->substream->runtime->channels;
466 conf[0].pitch = pitch;
467 conf[0].mix_msr = conf[0].imp_msr = conf[0].msr = 1;
468 conf[0].vo = 1;
Harry Butterworth55309212011-06-11 16:02:06 +0800469 } else if (2 <= atc->msr) {
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200470 if (0x8000000 < pitch) {
471 /* Need two-stage SRCs, SRCIMPs and
472 * AMIXERs for converting format */
473 conf[0].pitch = (atc->msr << 24);
474 conf[0].msr = conf[0].mix_msr = 1;
475 conf[0].imp_msr = atc->msr;
476 conf[0].vo = 0;
477 conf[1].pitch = atc_get_pitch(atc->rsr,
478 apcm->substream->runtime->rate);
479 conf[1].msr = conf[1].mix_msr = conf[1].imp_msr = 1;
480 conf[1].vo = 1;
481 *n_srcc = apcm->substream->runtime->channels * 2;
482 } else if (0x1000000 < pitch) {
483 /* Need one-stage SRCs, SRCIMPs and
484 * AMIXERs for converting format */
485 conf[0].pitch = pitch;
486 conf[0].msr = conf[0].mix_msr
487 = conf[0].imp_msr = atc->msr;
488 conf[0].vo = 1;
489 *n_srcc = apcm->substream->runtime->channels;
490 }
491 }
492}
493
494static int
495atc_pcm_capture_get_resources(struct ct_atc *atc, struct ct_atc_pcm *apcm)
496{
497 struct src_mgr *src_mgr = atc->rsc_mgrs[SRC];
498 struct srcimp_mgr *srcimp_mgr = atc->rsc_mgrs[SRCIMP];
499 struct amixer_mgr *amixer_mgr = atc->rsc_mgrs[AMIXER];
500 struct sum_mgr *sum_mgr = atc->rsc_mgrs[SUM];
501 struct src_desc src_dsc = {0};
Takashi Iwai514eef92009-06-08 14:57:57 +0200502 struct src *src;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200503 struct srcimp_desc srcimp_dsc = {0};
Takashi Iwai514eef92009-06-08 14:57:57 +0200504 struct srcimp *srcimp;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200505 struct amixer_desc mix_dsc = {0};
506 struct sum_desc sum_dsc = {0};
Takashi Iwai514eef92009-06-08 14:57:57 +0200507 unsigned int pitch;
508 int multi, err, i;
509 int n_srcimp, n_amixer, n_srcc, n_sum;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200510 struct src_node_conf_t src_node_conf[2] = {{0} };
511
Takashi Iwai822fa192009-06-02 14:12:17 +0200512 /* first release old resources */
Takashi Iwaia5990dc2009-06-09 08:19:02 +0200513 atc_pcm_release_resources(atc, apcm);
Takashi Iwai822fa192009-06-02 14:12:17 +0200514
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200515 /* The numbers of converting SRCs and SRCIMPs should be determined
516 * by pitch value. */
517
518 multi = apcm->substream->runtime->channels;
519
520 /* get pitch and convert to fixed-point 8.24 format. */
521 pitch = atc_get_pitch((atc->rsr * atc->msr),
522 apcm->substream->runtime->rate);
523
524 setup_src_node_conf(atc, apcm, src_node_conf, &n_srcc);
525 n_sum = (1 == multi) ? 1 : 0;
Takashi Iwai514eef92009-06-08 14:57:57 +0200526 n_amixer = n_sum * 2 + n_srcc;
527 n_srcimp = n_srcc;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200528 if ((multi > 1) && (0x8000000 >= pitch)) {
529 /* Need extra AMIXERs and SRCIMPs for special treatment
530 * of interleaved recording of conjugate channels */
531 n_amixer += multi * atc->msr;
532 n_srcimp += multi * atc->msr;
533 } else {
534 n_srcimp += multi;
535 }
536
537 if (n_srcc) {
538 apcm->srccs = kzalloc(sizeof(void *)*n_srcc, GFP_KERNEL);
Takashi Iwai35ebf6e2009-07-22 17:12:34 +0200539 if (!apcm->srccs)
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200540 return -ENOMEM;
541 }
542 if (n_amixer) {
543 apcm->amixers = kzalloc(sizeof(void *)*n_amixer, GFP_KERNEL);
Takashi Iwai35ebf6e2009-07-22 17:12:34 +0200544 if (!apcm->amixers) {
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200545 err = -ENOMEM;
546 goto error1;
547 }
548 }
549 apcm->srcimps = kzalloc(sizeof(void *)*n_srcimp, GFP_KERNEL);
Takashi Iwai35ebf6e2009-07-22 17:12:34 +0200550 if (!apcm->srcimps) {
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200551 err = -ENOMEM;
552 goto error1;
553 }
554
555 /* Allocate SRCs for sample rate conversion if needed */
556 src_dsc.multi = 1;
557 src_dsc.mode = ARCRW;
558 for (i = 0, apcm->n_srcc = 0; i < n_srcc; i++) {
559 src_dsc.msr = src_node_conf[i/multi].msr;
560 err = src_mgr->get_src(src_mgr, &src_dsc,
561 (struct src **)&apcm->srccs[i]);
562 if (err)
563 goto error1;
564
565 src = apcm->srccs[i];
566 pitch = src_node_conf[i/multi].pitch;
567 src->ops->set_pitch(src, pitch);
568 src->ops->set_rom(src, select_rom(pitch));
569 src->ops->set_vo(src, src_node_conf[i/multi].vo);
570
571 apcm->n_srcc++;
572 }
573
574 /* Allocate AMIXERs for routing SRCs of conversion if needed */
575 for (i = 0, apcm->n_amixer = 0; i < n_amixer; i++) {
576 if (i < (n_sum*2))
577 mix_dsc.msr = atc->msr;
578 else if (i < (n_sum*2+n_srcc))
579 mix_dsc.msr = src_node_conf[(i-n_sum*2)/multi].mix_msr;
580 else
581 mix_dsc.msr = 1;
582
583 err = amixer_mgr->get_amixer(amixer_mgr, &mix_dsc,
584 (struct amixer **)&apcm->amixers[i]);
585 if (err)
586 goto error1;
587
588 apcm->n_amixer++;
589 }
590
591 /* Allocate a SUM resource to mix all input channels together */
592 sum_dsc.msr = atc->msr;
593 err = sum_mgr->get_sum(sum_mgr, &sum_dsc, (struct sum **)&apcm->mono);
594 if (err)
595 goto error1;
596
597 pitch = atc_get_pitch((atc->rsr * atc->msr),
598 apcm->substream->runtime->rate);
599 /* Allocate SRCIMP resources */
600 for (i = 0, apcm->n_srcimp = 0; i < n_srcimp; i++) {
601 if (i < (n_srcc))
602 srcimp_dsc.msr = src_node_conf[i/multi].imp_msr;
603 else if (1 == multi)
604 srcimp_dsc.msr = (pitch <= 0x8000000) ? atc->msr : 1;
605 else
606 srcimp_dsc.msr = 1;
607
608 err = srcimp_mgr->get_srcimp(srcimp_mgr, &srcimp_dsc, &srcimp);
609 if (err)
610 goto error1;
611
612 apcm->srcimps[i] = srcimp;
613 apcm->n_srcimp++;
614 }
615
616 /* Allocate a SRC for writing data to host memory */
617 src_dsc.multi = apcm->substream->runtime->channels;
618 src_dsc.msr = 1;
619 src_dsc.mode = MEMWR;
620 err = src_mgr->get_src(src_mgr, &src_dsc, (struct src **)&apcm->src);
621 if (err)
622 goto error1;
623
624 src = apcm->src;
625 src->ops->set_pitch(src, pitch);
626
627 /* Set up device virtual mem map */
628 err = ct_map_audio_buffer(atc, apcm);
629 if (err < 0)
630 goto error1;
631
632 return 0;
633
634error1:
635 atc_pcm_release_resources(atc, apcm);
636 return err;
637}
638
639static int atc_pcm_capture_prepare(struct ct_atc *atc, struct ct_atc_pcm *apcm)
640{
Takashi Iwai514eef92009-06-08 14:57:57 +0200641 struct src *src;
642 struct amixer *amixer;
643 struct srcimp *srcimp;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200644 struct ct_mixer *mixer = atc->mixer;
Takashi Iwai514eef92009-06-08 14:57:57 +0200645 struct sum *mono;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200646 struct rsc *out_ports[8] = {NULL};
Takashi Iwai514eef92009-06-08 14:57:57 +0200647 int err, i, j, n_sum, multi;
648 unsigned int pitch;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200649 int mix_base = 0, imp_base = 0;
650
Wai Yew CHAY29959a02009-06-22 14:52:34 +0200651 atc_pcm_release_resources(atc, apcm);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200652
653 /* Get needed resources. */
654 err = atc_pcm_capture_get_resources(atc, apcm);
655 if (err)
656 return err;
657
658 /* Connect resources */
659 mixer->get_output_ports(mixer, MIX_PCMO_FRONT,
660 &out_ports[0], &out_ports[1]);
661
662 multi = apcm->substream->runtime->channels;
663 if (1 == multi) {
664 mono = apcm->mono;
665 for (i = 0; i < 2; i++) {
666 amixer = apcm->amixers[i];
667 amixer->ops->setup(amixer, out_ports[i],
668 MONO_SUM_SCALE, mono);
669 }
670 out_ports[0] = &mono->rsc;
671 n_sum = 1;
672 mix_base = n_sum * 2;
673 }
674
675 for (i = 0; i < apcm->n_srcc; i++) {
676 src = apcm->srccs[i];
677 srcimp = apcm->srcimps[imp_base+i];
678 amixer = apcm->amixers[mix_base+i];
679 srcimp->ops->map(srcimp, src, out_ports[i%multi]);
680 amixer->ops->setup(amixer, &src->rsc, INIT_VOL, NULL);
681 out_ports[i%multi] = &amixer->rsc;
682 }
683
684 pitch = atc_get_pitch((atc->rsr * atc->msr),
685 apcm->substream->runtime->rate);
686
687 if ((multi > 1) && (pitch <= 0x8000000)) {
688 /* Special connection for interleaved
689 * recording with conjugate channels */
690 for (i = 0; i < multi; i++) {
691 out_ports[i]->ops->master(out_ports[i]);
692 for (j = 0; j < atc->msr; j++) {
693 amixer = apcm->amixers[apcm->n_srcc+j*multi+i];
694 amixer->ops->set_input(amixer, out_ports[i]);
695 amixer->ops->set_scale(amixer, INIT_VOL);
696 amixer->ops->set_sum(amixer, NULL);
697 amixer->ops->commit_raw_write(amixer);
698 out_ports[i]->ops->next_conj(out_ports[i]);
699
700 srcimp = apcm->srcimps[apcm->n_srcc+j*multi+i];
701 srcimp->ops->map(srcimp, apcm->src,
702 &amixer->rsc);
703 }
704 }
705 } else {
706 for (i = 0; i < multi; i++) {
707 srcimp = apcm->srcimps[apcm->n_srcc+i];
708 srcimp->ops->map(srcimp, apcm->src, out_ports[i]);
709 }
710 }
711
Takashi Iwaib7bbf872009-06-05 16:11:07 +0200712 ct_timer_prepare(apcm->timer);
713
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200714 return 0;
715}
716
717static int atc_pcm_capture_start(struct ct_atc *atc, struct ct_atc_pcm *apcm)
718{
Takashi Iwai514eef92009-06-08 14:57:57 +0200719 struct src *src;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200720 struct src_mgr *src_mgr = atc->rsc_mgrs[SRC];
Takashi Iwai514eef92009-06-08 14:57:57 +0200721 int i, multi;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200722
723 if (apcm->started)
724 return 0;
725
726 apcm->started = 1;
727 multi = apcm->substream->runtime->channels;
728 /* Set up converting SRCs */
729 for (i = 0; i < apcm->n_srcc; i++) {
730 src = apcm->srccs[i];
731 src->ops->set_pm(src, ((i%multi) != (multi-1)));
732 src_mgr->src_disable(src_mgr, src);
733 }
734
735 /* Set up recording SRC */
736 src = apcm->src;
737 src->ops->set_sf(src, convert_format(apcm->substream->runtime->format));
738 src->ops->set_sa(src, apcm->vm_block->addr);
739 src->ops->set_la(src, apcm->vm_block->addr + apcm->vm_block->size);
740 src->ops->set_ca(src, apcm->vm_block->addr);
741 src_mgr->src_disable(src_mgr, src);
742
743 /* Disable relevant SRCs firstly */
744 src_mgr->commit_write(src_mgr);
745
746 /* Enable SRCs respectively */
747 for (i = 0; i < apcm->n_srcc; i++) {
748 src = apcm->srccs[i];
749 src->ops->set_state(src, SRC_STATE_RUN);
750 src->ops->commit_write(src);
751 src_mgr->src_enable_s(src_mgr, src);
752 }
753 src = apcm->src;
754 src->ops->set_bm(src, 1);
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 /* Enable relevant SRCs synchronously */
760 src_mgr->commit_write(src_mgr);
761
Takashi Iwaib7bbf872009-06-05 16:11:07 +0200762 ct_timer_start(apcm->timer);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200763 return 0;
764}
765
766static int
767atc_pcm_capture_position(struct ct_atc *atc, struct ct_atc_pcm *apcm)
768{
769 struct src *src = apcm->src;
770
Takashi Iwai5242bc72009-06-09 08:17:14 +0200771 if (!src)
772 return 0;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200773 return src->ops->get_ca(src) - apcm->vm_block->addr;
774}
775
776static int spdif_passthru_playback_get_resources(struct ct_atc *atc,
777 struct ct_atc_pcm *apcm)
778{
779 struct src_mgr *src_mgr = atc->rsc_mgrs[SRC];
780 struct amixer_mgr *amixer_mgr = atc->rsc_mgrs[AMIXER];
781 struct src_desc desc = {0};
782 struct amixer_desc mix_dsc = {0};
Takashi Iwai514eef92009-06-08 14:57:57 +0200783 struct src *src;
784 int err;
785 int n_amixer = apcm->substream->runtime->channels, i;
786 unsigned int pitch, rsr = atc->pll_rate;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200787
Takashi Iwai822fa192009-06-02 14:12:17 +0200788 /* first release old resources */
Takashi Iwaia5990dc2009-06-09 08:19:02 +0200789 atc_pcm_release_resources(atc, apcm);
Takashi Iwai822fa192009-06-02 14:12:17 +0200790
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200791 /* Get SRC resource */
792 desc.multi = apcm->substream->runtime->channels;
793 desc.msr = 1;
794 while (apcm->substream->runtime->rate > (rsr * desc.msr))
795 desc.msr <<= 1;
796
797 desc.mode = MEMRD;
798 err = src_mgr->get_src(src_mgr, &desc, (struct src **)&apcm->src);
799 if (err)
800 goto error1;
801
802 pitch = atc_get_pitch(apcm->substream->runtime->rate, (rsr * desc.msr));
803 src = apcm->src;
804 src->ops->set_pitch(src, pitch);
805 src->ops->set_rom(src, select_rom(pitch));
806 src->ops->set_sf(src, convert_format(apcm->substream->runtime->format));
807 src->ops->set_pm(src, (src->ops->next_interleave(src) != NULL));
808 src->ops->set_bp(src, 1);
809
810 /* Get AMIXER resource */
811 n_amixer = (n_amixer < 2) ? 2 : n_amixer;
812 apcm->amixers = kzalloc(sizeof(void *)*n_amixer, GFP_KERNEL);
Takashi Iwai35ebf6e2009-07-22 17:12:34 +0200813 if (!apcm->amixers) {
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200814 err = -ENOMEM;
815 goto error1;
816 }
817 mix_dsc.msr = desc.msr;
818 for (i = 0, apcm->n_amixer = 0; i < n_amixer; i++) {
819 err = amixer_mgr->get_amixer(amixer_mgr, &mix_dsc,
820 (struct amixer **)&apcm->amixers[i]);
821 if (err)
822 goto error1;
823
824 apcm->n_amixer++;
825 }
826
827 /* Set up device virtual mem map */
828 err = ct_map_audio_buffer(atc, apcm);
829 if (err < 0)
830 goto error1;
831
832 return 0;
833
834error1:
835 atc_pcm_release_resources(atc, apcm);
836 return err;
837}
838
Takashi Iwai514eef92009-06-08 14:57:57 +0200839static int atc_pll_init(struct ct_atc *atc, int rate)
840{
841 struct hw *hw = atc->hw;
842 int err;
843 err = hw->pll_init(hw, rate);
844 atc->pll_rate = err ? 0 : rate;
845 return err;
846}
847
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200848static int
849spdif_passthru_playback_setup(struct ct_atc *atc, struct ct_atc_pcm *apcm)
850{
851 struct dao *dao = container_of(atc->daios[SPDIFOO], struct dao, daio);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200852 unsigned int rate = apcm->substream->runtime->rate;
Takashi Iwai514eef92009-06-08 14:57:57 +0200853 unsigned int status;
Wai Yew CHAY29959a02009-06-22 14:52:34 +0200854 int err = 0;
Takashi Iwai514eef92009-06-08 14:57:57 +0200855 unsigned char iec958_con_fs;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200856
857 switch (rate) {
858 case 48000:
859 iec958_con_fs = IEC958_AES3_CON_FS_48000;
860 break;
861 case 44100:
862 iec958_con_fs = IEC958_AES3_CON_FS_44100;
863 break;
864 case 32000:
865 iec958_con_fs = IEC958_AES3_CON_FS_32000;
866 break;
867 default:
868 return -ENOENT;
869 }
870
Takashi Iwai635c2652009-06-13 10:12:59 +0200871 mutex_lock(&atc->atc_mutex);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200872 dao->ops->get_spos(dao, &status);
873 if (((status >> 24) & IEC958_AES3_CON_FS) != iec958_con_fs) {
Przemyslaw Bruski4c1847e2011-03-13 16:18:56 +0100874 status &= ~(IEC958_AES3_CON_FS << 24);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200875 status |= (iec958_con_fs << 24);
876 dao->ops->set_spos(dao, status);
877 dao->ops->commit_write(dao);
878 }
Takashi Iwai514eef92009-06-08 14:57:57 +0200879 if ((rate != atc->pll_rate) && (32000 != rate))
880 err = atc_pll_init(atc, rate);
Takashi Iwai635c2652009-06-13 10:12:59 +0200881 mutex_unlock(&atc->atc_mutex);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200882
883 return err;
884}
885
886static int
887spdif_passthru_playback_prepare(struct ct_atc *atc, struct ct_atc_pcm *apcm)
888{
Takashi Iwai514eef92009-06-08 14:57:57 +0200889 struct src *src;
890 struct amixer *amixer;
891 struct dao *dao;
892 int err;
893 int i;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200894
Wai Yew CHAY29959a02009-06-22 14:52:34 +0200895 atc_pcm_release_resources(atc, apcm);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200896
897 /* Configure SPDIFOO and PLL to passthrough mode;
898 * determine pll_rate. */
899 err = spdif_passthru_playback_setup(atc, apcm);
900 if (err)
901 return err;
902
903 /* Get needed resources. */
904 err = spdif_passthru_playback_get_resources(atc, apcm);
905 if (err)
906 return err;
907
908 /* Connect resources */
909 src = apcm->src;
910 for (i = 0; i < apcm->n_amixer; i++) {
911 amixer = apcm->amixers[i];
912 amixer->ops->setup(amixer, &src->rsc, INIT_VOL, NULL);
913 src = src->ops->next_interleave(src);
Takashi Iwai35ebf6e2009-07-22 17:12:34 +0200914 if (!src)
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200915 src = apcm->src;
916 }
917 /* Connect to SPDIFOO */
Takashi Iwai635c2652009-06-13 10:12:59 +0200918 mutex_lock(&atc->atc_mutex);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200919 dao = container_of(atc->daios[SPDIFOO], struct dao, daio);
920 amixer = apcm->amixers[0];
921 dao->ops->set_left_input(dao, &amixer->rsc);
922 amixer = apcm->amixers[1];
923 dao->ops->set_right_input(dao, &amixer->rsc);
Takashi Iwai635c2652009-06-13 10:12:59 +0200924 mutex_unlock(&atc->atc_mutex);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200925
Takashi Iwaib7bbf872009-06-05 16:11:07 +0200926 ct_timer_prepare(apcm->timer);
927
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200928 return 0;
929}
930
931static int atc_select_line_in(struct ct_atc *atc)
932{
933 struct hw *hw = atc->hw;
934 struct ct_mixer *mixer = atc->mixer;
Takashi Iwai514eef92009-06-08 14:57:57 +0200935 struct src *src;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200936
937 if (hw->is_adc_source_selected(hw, ADC_LINEIN))
938 return 0;
939
940 mixer->set_input_left(mixer, MIX_MIC_IN, NULL);
941 mixer->set_input_right(mixer, MIX_MIC_IN, NULL);
942
943 hw->select_adc_source(hw, ADC_LINEIN);
944
945 src = atc->srcs[2];
946 mixer->set_input_left(mixer, MIX_LINE_IN, &src->rsc);
947 src = atc->srcs[3];
948 mixer->set_input_right(mixer, MIX_LINE_IN, &src->rsc);
949
950 return 0;
951}
952
953static int atc_select_mic_in(struct ct_atc *atc)
954{
955 struct hw *hw = atc->hw;
956 struct ct_mixer *mixer = atc->mixer;
Takashi Iwai514eef92009-06-08 14:57:57 +0200957 struct src *src;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200958
959 if (hw->is_adc_source_selected(hw, ADC_MICIN))
960 return 0;
961
962 mixer->set_input_left(mixer, MIX_LINE_IN, NULL);
963 mixer->set_input_right(mixer, MIX_LINE_IN, NULL);
964
965 hw->select_adc_source(hw, ADC_MICIN);
966
967 src = atc->srcs[2];
968 mixer->set_input_left(mixer, MIX_MIC_IN, &src->rsc);
969 src = atc->srcs[3];
970 mixer->set_input_right(mixer, MIX_MIC_IN, &src->rsc);
971
972 return 0;
973}
974
975static int atc_have_digit_io_switch(struct ct_atc *atc)
976{
977 struct hw *hw = atc->hw;
978
979 return hw->have_digit_io_switch(hw);
980}
981
Harry Butterworth55309212011-06-11 16:02:06 +0800982static int atc_have_dedicated_mic(struct ct_atc *atc)
983{
984 struct hw *hw = atc->hw;
985
986 return hw->have_dedicated_mic(hw);
987}
988
989static int atc_have_output_switch(struct ct_atc *atc)
990{
991 struct hw *hw = atc->hw;
992
993 return hw->have_output_switch(hw);
994}
995
996static int atc_output_switch_get(struct ct_atc *atc)
997{
998 struct hw *hw = atc->hw;
999
1000 return hw->output_switch_get(hw);
1001}
1002
1003static int atc_output_switch_put(struct ct_atc *atc, int position)
1004{
1005 struct hw *hw = atc->hw;
1006
1007 return hw->output_switch_put(hw, position);
1008}
1009
1010static int atc_have_mic_source_switch(struct ct_atc *atc)
1011{
1012 struct hw *hw = atc->hw;
1013
1014 return hw->have_mic_source_switch(hw);
1015}
1016
1017static int atc_mic_source_switch_get(struct ct_atc *atc)
1018{
1019 struct hw *hw = atc->hw;
1020
1021 return hw->mic_source_switch_get(hw);
1022}
1023
1024static int atc_mic_source_switch_put(struct ct_atc *atc, int position)
1025{
1026 struct hw *hw = atc->hw;
1027
1028 return hw->mic_source_switch_put(hw, position);
1029}
1030
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001031static int atc_select_digit_io(struct ct_atc *atc)
1032{
1033 struct hw *hw = atc->hw;
1034
1035 if (hw->is_adc_source_selected(hw, ADC_NONE))
1036 return 0;
1037
1038 hw->select_adc_source(hw, ADC_NONE);
1039
1040 return 0;
1041}
1042
1043static int atc_daio_unmute(struct ct_atc *atc, unsigned char state, int type)
1044{
1045 struct daio_mgr *daio_mgr = atc->rsc_mgrs[DAIO];
1046
1047 if (state)
1048 daio_mgr->daio_enable(daio_mgr, atc->daios[type]);
1049 else
1050 daio_mgr->daio_disable(daio_mgr, atc->daios[type]);
1051
1052 daio_mgr->commit_write(daio_mgr);
1053
1054 return 0;
1055}
1056
1057static int
1058atc_dao_get_status(struct ct_atc *atc, unsigned int *status, int type)
1059{
1060 struct dao *dao = container_of(atc->daios[type], struct dao, daio);
1061 return dao->ops->get_spos(dao, status);
1062}
1063
1064static int
1065atc_dao_set_status(struct ct_atc *atc, unsigned int status, int type)
1066{
1067 struct dao *dao = container_of(atc->daios[type], struct dao, daio);
1068
1069 dao->ops->set_spos(dao, status);
1070 dao->ops->commit_write(dao);
1071 return 0;
1072}
1073
1074static int atc_line_front_unmute(struct ct_atc *atc, unsigned char state)
1075{
1076 return atc_daio_unmute(atc, state, LINEO1);
1077}
1078
1079static int atc_line_surround_unmute(struct ct_atc *atc, unsigned char state)
1080{
Sven Eckelmann3b046912009-10-01 20:06:39 +02001081 return atc_daio_unmute(atc, state, LINEO2);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001082}
1083
1084static int atc_line_clfe_unmute(struct ct_atc *atc, unsigned char state)
1085{
1086 return atc_daio_unmute(atc, state, LINEO3);
1087}
1088
1089static int atc_line_rear_unmute(struct ct_atc *atc, unsigned char state)
1090{
Sven Eckelmann3b046912009-10-01 20:06:39 +02001091 return atc_daio_unmute(atc, state, LINEO4);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001092}
1093
1094static int atc_line_in_unmute(struct ct_atc *atc, unsigned char state)
1095{
1096 return atc_daio_unmute(atc, state, LINEIM);
1097}
1098
Harry Butterworth55309212011-06-11 16:02:06 +08001099static int atc_mic_unmute(struct ct_atc *atc, unsigned char state)
1100{
1101 return atc_daio_unmute(atc, state, MIC);
1102}
1103
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001104static int atc_spdif_out_unmute(struct ct_atc *atc, unsigned char state)
1105{
1106 return atc_daio_unmute(atc, state, SPDIFOO);
1107}
1108
1109static int atc_spdif_in_unmute(struct ct_atc *atc, unsigned char state)
1110{
1111 return atc_daio_unmute(atc, state, SPDIFIO);
1112}
1113
1114static int atc_spdif_out_get_status(struct ct_atc *atc, unsigned int *status)
1115{
1116 return atc_dao_get_status(atc, status, SPDIFOO);
1117}
1118
1119static int atc_spdif_out_set_status(struct ct_atc *atc, unsigned int status)
1120{
1121 return atc_dao_set_status(atc, status, SPDIFOO);
1122}
1123
1124static int atc_spdif_out_passthru(struct ct_atc *atc, unsigned char state)
1125{
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001126 struct dao_desc da_dsc = {0};
Takashi Iwai514eef92009-06-08 14:57:57 +02001127 struct dao *dao;
1128 int err;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001129 struct ct_mixer *mixer = atc->mixer;
1130 struct rsc *rscs[2] = {NULL};
1131 unsigned int spos = 0;
1132
Takashi Iwai635c2652009-06-13 10:12:59 +02001133 mutex_lock(&atc->atc_mutex);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001134 dao = container_of(atc->daios[SPDIFOO], struct dao, daio);
1135 da_dsc.msr = state ? 1 : atc->msr;
1136 da_dsc.passthru = state ? 1 : 0;
1137 err = dao->ops->reinit(dao, &da_dsc);
1138 if (state) {
1139 spos = IEC958_DEFAULT_CON;
1140 } else {
1141 mixer->get_output_ports(mixer, MIX_SPDIF_OUT,
1142 &rscs[0], &rscs[1]);
1143 dao->ops->set_left_input(dao, rscs[0]);
1144 dao->ops->set_right_input(dao, rscs[1]);
1145 /* Restore PLL to atc->rsr if needed. */
Takashi Iwai514eef92009-06-08 14:57:57 +02001146 if (atc->pll_rate != atc->rsr)
1147 err = atc_pll_init(atc, atc->rsr);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001148 }
1149 dao->ops->set_spos(dao, spos);
1150 dao->ops->commit_write(dao);
Takashi Iwai635c2652009-06-13 10:12:59 +02001151 mutex_unlock(&atc->atc_mutex);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001152
1153 return err;
1154}
1155
Wai Yew CHAY29959a02009-06-22 14:52:34 +02001156static int atc_release_resources(struct ct_atc *atc)
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001157{
Wai Yew CHAY29959a02009-06-22 14:52:34 +02001158 int i;
1159 struct daio_mgr *daio_mgr = NULL;
1160 struct dao *dao = NULL;
1161 struct dai *dai = NULL;
1162 struct daio *daio = NULL;
1163 struct sum_mgr *sum_mgr = NULL;
1164 struct src_mgr *src_mgr = NULL;
1165 struct srcimp_mgr *srcimp_mgr = NULL;
1166 struct srcimp *srcimp = NULL;
1167 struct ct_mixer *mixer = NULL;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001168
Wai Yew CHAY29959a02009-06-22 14:52:34 +02001169 /* disconnect internal mixer objects */
Takashi Iwai35ebf6e2009-07-22 17:12:34 +02001170 if (atc->mixer) {
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001171 mixer = atc->mixer;
1172 mixer->set_input_left(mixer, MIX_LINE_IN, NULL);
1173 mixer->set_input_right(mixer, MIX_LINE_IN, NULL);
1174 mixer->set_input_left(mixer, MIX_MIC_IN, NULL);
1175 mixer->set_input_right(mixer, MIX_MIC_IN, NULL);
1176 mixer->set_input_left(mixer, MIX_SPDIF_IN, NULL);
1177 mixer->set_input_right(mixer, MIX_SPDIF_IN, NULL);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001178 }
1179
Takashi Iwai35ebf6e2009-07-22 17:12:34 +02001180 if (atc->daios) {
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001181 daio_mgr = (struct daio_mgr *)atc->rsc_mgrs[DAIO];
1182 for (i = 0; i < atc->n_daio; i++) {
1183 daio = atc->daios[i];
1184 if (daio->type < LINEIM) {
1185 dao = container_of(daio, struct dao, daio);
1186 dao->ops->clear_left_input(dao);
1187 dao->ops->clear_right_input(dao);
1188 } else {
1189 dai = container_of(daio, struct dai, daio);
1190 /* some thing to do for dai ... */
1191 }
1192 daio_mgr->put_daio(daio_mgr, daio);
1193 }
1194 kfree(atc->daios);
Wai Yew CHAY29959a02009-06-22 14:52:34 +02001195 atc->daios = NULL;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001196 }
1197
Takashi Iwai35ebf6e2009-07-22 17:12:34 +02001198 if (atc->pcm) {
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001199 sum_mgr = atc->rsc_mgrs[SUM];
1200 for (i = 0; i < atc->n_pcm; i++)
1201 sum_mgr->put_sum(sum_mgr, atc->pcm[i]);
1202
1203 kfree(atc->pcm);
Wai Yew CHAY29959a02009-06-22 14:52:34 +02001204 atc->pcm = NULL;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001205 }
1206
Takashi Iwai35ebf6e2009-07-22 17:12:34 +02001207 if (atc->srcs) {
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001208 src_mgr = atc->rsc_mgrs[SRC];
1209 for (i = 0; i < atc->n_src; i++)
1210 src_mgr->put_src(src_mgr, atc->srcs[i]);
1211
1212 kfree(atc->srcs);
Wai Yew CHAY29959a02009-06-22 14:52:34 +02001213 atc->srcs = NULL;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001214 }
1215
Takashi Iwai35ebf6e2009-07-22 17:12:34 +02001216 if (atc->srcimps) {
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001217 srcimp_mgr = atc->rsc_mgrs[SRCIMP];
1218 for (i = 0; i < atc->n_srcimp; i++) {
1219 srcimp = atc->srcimps[i];
1220 srcimp->ops->unmap(srcimp);
1221 srcimp_mgr->put_srcimp(srcimp_mgr, atc->srcimps[i]);
1222 }
1223 kfree(atc->srcimps);
Wai Yew CHAY29959a02009-06-22 14:52:34 +02001224 atc->srcimps = NULL;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001225 }
1226
Wai Yew CHAY29959a02009-06-22 14:52:34 +02001227 return 0;
1228}
1229
1230static int ct_atc_destroy(struct ct_atc *atc)
1231{
1232 int i = 0;
1233
Takashi Iwai35ebf6e2009-07-22 17:12:34 +02001234 if (!atc)
Wai Yew CHAY29959a02009-06-22 14:52:34 +02001235 return 0;
1236
1237 if (atc->timer) {
1238 ct_timer_free(atc->timer);
1239 atc->timer = NULL;
1240 }
1241
1242 atc_release_resources(atc);
1243
1244 /* Destroy internal mixer objects */
Takashi Iwai35ebf6e2009-07-22 17:12:34 +02001245 if (atc->mixer)
Wai Yew CHAY29959a02009-06-22 14:52:34 +02001246 ct_mixer_destroy(atc->mixer);
1247
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001248 for (i = 0; i < NUM_RSCTYP; i++) {
Takashi Iwai35ebf6e2009-07-22 17:12:34 +02001249 if (rsc_mgr_funcs[i].destroy && atc->rsc_mgrs[i])
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001250 rsc_mgr_funcs[i].destroy(atc->rsc_mgrs[i]);
1251
1252 }
1253
Takashi Iwai35ebf6e2009-07-22 17:12:34 +02001254 if (atc->hw)
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001255 destroy_hw_obj((struct hw *)atc->hw);
1256
1257 /* Destroy device virtual memory manager object */
Takashi Iwai35ebf6e2009-07-22 17:12:34 +02001258 if (atc->vm) {
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001259 ct_vm_destroy(atc->vm);
1260 atc->vm = NULL;
1261 }
1262
1263 kfree(atc);
1264
1265 return 0;
1266}
1267
1268static int atc_dev_free(struct snd_device *dev)
1269{
1270 struct ct_atc *atc = dev->device_data;
1271 return ct_atc_destroy(atc);
1272}
1273
Takashi Iwai408bffd2010-01-14 09:19:46 +01001274static int __devinit atc_identify_card(struct ct_atc *atc, unsigned int ssid)
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001275{
Takashi Iwai94701952009-06-08 18:10:32 +02001276 const struct snd_pci_quirk *p;
1277 const struct snd_pci_quirk *list;
Takashi Iwai408bffd2010-01-14 09:19:46 +01001278 u16 vendor_id, device_id;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001279
Takashi Iwai94701952009-06-08 18:10:32 +02001280 switch (atc->chip_type) {
1281 case ATC20K1:
1282 atc->chip_name = "20K1";
1283 list = subsys_20k1_list;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001284 break;
Takashi Iwai94701952009-06-08 18:10:32 +02001285 case ATC20K2:
1286 atc->chip_name = "20K2";
1287 list = subsys_20k2_list;
1288 break;
1289 default:
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001290 return -ENOENT;
Takashi Iwai94701952009-06-08 18:10:32 +02001291 }
Takashi Iwai408bffd2010-01-14 09:19:46 +01001292 if (ssid) {
1293 vendor_id = ssid >> 16;
1294 device_id = ssid & 0xffff;
1295 } else {
1296 vendor_id = atc->pci->subsystem_vendor;
1297 device_id = atc->pci->subsystem_device;
1298 }
1299 p = snd_pci_quirk_lookup_id(vendor_id, device_id, list);
Takashi Iwaia8f43102009-06-22 07:36:52 +02001300 if (p) {
1301 if (p->value < 0) {
1302 printk(KERN_ERR "ctxfi: "
1303 "Device %04x:%04x is black-listed\n",
Takashi Iwai408bffd2010-01-14 09:19:46 +01001304 vendor_id, device_id);
Takashi Iwaia8f43102009-06-22 07:36:52 +02001305 return -ENOENT;
1306 }
1307 atc->model = p->value;
1308 } else {
1309 if (atc->chip_type == ATC20K1)
1310 atc->model = CT20K1_UNKNOWN;
1311 else
1312 atc->model = CT20K2_UNKNOWN;
1313 }
Takashi Iwai94701952009-06-08 18:10:32 +02001314 atc->model_name = ct_subsys_name[atc->model];
1315 snd_printd("ctxfi: chip %s model %s (%04x:%04x) is found\n",
1316 atc->chip_name, atc->model_name,
Takashi Iwai408bffd2010-01-14 09:19:46 +01001317 vendor_id, device_id);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001318 return 0;
1319}
1320
Takashi Iwai2a36f672009-06-05 16:34:10 +02001321int __devinit ct_atc_create_alsa_devs(struct ct_atc *atc)
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001322{
1323 enum CTALSADEVS i;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001324 int err;
1325
Takashi Iwai94701952009-06-08 18:10:32 +02001326 alsa_dev_funcs[MIXER].public_name = atc->chip_name;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001327
1328 for (i = 0; i < NUM_CTALSADEVS; i++) {
Takashi Iwai35ebf6e2009-07-22 17:12:34 +02001329 if (!alsa_dev_funcs[i].create)
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001330 continue;
1331
1332 err = alsa_dev_funcs[i].create(atc, i,
1333 alsa_dev_funcs[i].public_name);
1334 if (err) {
Takashi Iwaib3e0afe2009-05-14 15:19:30 +02001335 printk(KERN_ERR "ctxfi: "
1336 "Creating alsa device %d failed!\n", i);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001337 return err;
1338 }
1339 }
1340
1341 return 0;
1342}
1343
Takashi Iwai2a36f672009-06-05 16:34:10 +02001344static int __devinit atc_create_hw_devs(struct ct_atc *atc)
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001345{
Takashi Iwai514eef92009-06-08 14:57:57 +02001346 struct hw *hw;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001347 struct card_conf info = {0};
Takashi Iwai514eef92009-06-08 14:57:57 +02001348 int i, err;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001349
Takashi Iwai94701952009-06-08 18:10:32 +02001350 err = create_hw_obj(atc->pci, atc->chip_type, atc->model, &hw);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001351 if (err) {
1352 printk(KERN_ERR "Failed to create hw obj!!!\n");
1353 return err;
1354 }
1355 atc->hw = hw;
1356
1357 /* Initialize card hardware. */
1358 info.rsr = atc->rsr;
1359 info.msr = atc->msr;
1360 info.vm_pgt_phys = atc_get_ptp_phys(atc, 0);
1361 err = hw->card_init(hw, &info);
1362 if (err < 0)
1363 return err;
1364
1365 for (i = 0; i < NUM_RSCTYP; i++) {
Takashi Iwai35ebf6e2009-07-22 17:12:34 +02001366 if (!rsc_mgr_funcs[i].create)
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001367 continue;
1368
1369 err = rsc_mgr_funcs[i].create(atc->hw, &atc->rsc_mgrs[i]);
1370 if (err) {
Takashi Iwaib3e0afe2009-05-14 15:19:30 +02001371 printk(KERN_ERR "ctxfi: "
1372 "Failed to create rsc_mgr %d!!!\n", i);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001373 return err;
1374 }
1375 }
1376
1377 return 0;
1378}
1379
Wai Yew CHAY29959a02009-06-22 14:52:34 +02001380static int atc_get_resources(struct ct_atc *atc)
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001381{
1382 struct daio_desc da_desc = {0};
Takashi Iwai514eef92009-06-08 14:57:57 +02001383 struct daio_mgr *daio_mgr;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001384 struct src_desc src_dsc = {0};
Takashi Iwai514eef92009-06-08 14:57:57 +02001385 struct src_mgr *src_mgr;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001386 struct srcimp_desc srcimp_dsc = {0};
Takashi Iwai514eef92009-06-08 14:57:57 +02001387 struct srcimp_mgr *srcimp_mgr;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001388 struct sum_desc sum_dsc = {0};
Takashi Iwai514eef92009-06-08 14:57:57 +02001389 struct sum_mgr *sum_mgr;
Harry Butterworth55309212011-06-11 16:02:06 +08001390 int err, i, num_srcs, num_daios;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001391
Harry Butterworth55309212011-06-11 16:02:06 +08001392 num_daios = ((atc->model == CTSB1270) ? 8 : 7);
1393 num_srcs = ((atc->model == CTSB1270) ? 6 : 4);
1394
1395 atc->daios = kzalloc(sizeof(void *)*num_daios, GFP_KERNEL);
Takashi Iwai35ebf6e2009-07-22 17:12:34 +02001396 if (!atc->daios)
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001397 return -ENOMEM;
1398
Harry Butterworth55309212011-06-11 16:02:06 +08001399 atc->srcs = kzalloc(sizeof(void *)*num_srcs, GFP_KERNEL);
Takashi Iwai35ebf6e2009-07-22 17:12:34 +02001400 if (!atc->srcs)
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001401 return -ENOMEM;
1402
Harry Butterworth55309212011-06-11 16:02:06 +08001403 atc->srcimps = kzalloc(sizeof(void *)*num_srcs, GFP_KERNEL);
Takashi Iwai35ebf6e2009-07-22 17:12:34 +02001404 if (!atc->srcimps)
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001405 return -ENOMEM;
1406
1407 atc->pcm = kzalloc(sizeof(void *)*(2*4), GFP_KERNEL);
Takashi Iwai35ebf6e2009-07-22 17:12:34 +02001408 if (!atc->pcm)
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001409 return -ENOMEM;
1410
1411 daio_mgr = (struct daio_mgr *)atc->rsc_mgrs[DAIO];
1412 da_desc.msr = atc->msr;
Harry Butterworth55309212011-06-11 16:02:06 +08001413 for (i = 0, atc->n_daio = 0; i < num_daios; i++) {
1414 da_desc.type = (atc->model != CTSB073X) ? i :
1415 ((i == SPDIFIO) ? SPDIFI1 : i);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001416 err = daio_mgr->get_daio(daio_mgr, &da_desc,
1417 (struct daio **)&atc->daios[i]);
1418 if (err) {
Takashi Iwaib3e0afe2009-05-14 15:19:30 +02001419 printk(KERN_ERR "ctxfi: Failed to get DAIO "
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001420 "resource %d!!!\n", i);
1421 return err;
1422 }
1423 atc->n_daio++;
1424 }
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001425
1426 src_mgr = atc->rsc_mgrs[SRC];
1427 src_dsc.multi = 1;
1428 src_dsc.msr = atc->msr;
1429 src_dsc.mode = ARCRW;
Harry Butterworth55309212011-06-11 16:02:06 +08001430 for (i = 0, atc->n_src = 0; i < num_srcs; i++) {
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001431 err = src_mgr->get_src(src_mgr, &src_dsc,
1432 (struct src **)&atc->srcs[i]);
1433 if (err)
1434 return err;
1435
1436 atc->n_src++;
1437 }
1438
1439 srcimp_mgr = atc->rsc_mgrs[SRCIMP];
Harry Butterworth55309212011-06-11 16:02:06 +08001440 srcimp_dsc.msr = 8;
1441 for (i = 0, atc->n_srcimp = 0; i < num_srcs; i++) {
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001442 err = srcimp_mgr->get_srcimp(srcimp_mgr, &srcimp_dsc,
1443 (struct srcimp **)&atc->srcimps[i]);
1444 if (err)
1445 return err;
1446
1447 atc->n_srcimp++;
1448 }
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001449
1450 sum_mgr = atc->rsc_mgrs[SUM];
1451 sum_dsc.msr = atc->msr;
1452 for (i = 0, atc->n_pcm = 0; i < (2*4); i++) {
1453 err = sum_mgr->get_sum(sum_mgr, &sum_dsc,
1454 (struct sum **)&atc->pcm[i]);
1455 if (err)
1456 return err;
1457
1458 atc->n_pcm++;
1459 }
1460
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001461 return 0;
1462}
1463
Wai Yew CHAY29959a02009-06-22 14:52:34 +02001464static void
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001465atc_connect_dai(struct src_mgr *src_mgr, struct dai *dai,
1466 struct src **srcs, struct srcimp **srcimps)
1467{
1468 struct rsc *rscs[2] = {NULL};
Takashi Iwai514eef92009-06-08 14:57:57 +02001469 struct src *src;
1470 struct srcimp *srcimp;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001471 int i = 0;
1472
1473 rscs[0] = &dai->daio.rscl;
1474 rscs[1] = &dai->daio.rscr;
1475 for (i = 0; i < 2; i++) {
1476 src = srcs[i];
1477 srcimp = srcimps[i];
1478 srcimp->ops->map(srcimp, src, rscs[i]);
1479 src_mgr->src_disable(src_mgr, src);
1480 }
1481
1482 src_mgr->commit_write(src_mgr); /* Actually disable SRCs */
1483
1484 src = srcs[0];
1485 src->ops->set_pm(src, 1);
1486 for (i = 0; i < 2; i++) {
1487 src = srcs[i];
1488 src->ops->set_state(src, SRC_STATE_RUN);
1489 src->ops->commit_write(src);
1490 src_mgr->src_enable_s(src_mgr, src);
1491 }
1492
1493 dai->ops->set_srt_srcl(dai, &(srcs[0]->rsc));
1494 dai->ops->set_srt_srcr(dai, &(srcs[1]->rsc));
1495
1496 dai->ops->set_enb_src(dai, 1);
1497 dai->ops->set_enb_srt(dai, 1);
1498 dai->ops->commit_write(dai);
1499
1500 src_mgr->commit_write(src_mgr); /* Synchronously enable SRCs */
1501}
1502
Wai Yew CHAY29959a02009-06-22 14:52:34 +02001503static void atc_connect_resources(struct ct_atc *atc)
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001504{
Takashi Iwai514eef92009-06-08 14:57:57 +02001505 struct dai *dai;
1506 struct dao *dao;
1507 struct src *src;
1508 struct sum *sum;
1509 struct ct_mixer *mixer;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001510 struct rsc *rscs[2] = {NULL};
Takashi Iwai514eef92009-06-08 14:57:57 +02001511 int i, j;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001512
1513 mixer = atc->mixer;
1514
1515 for (i = MIX_WAVE_FRONT, j = LINEO1; i <= MIX_SPDIF_OUT; i++, j++) {
1516 mixer->get_output_ports(mixer, i, &rscs[0], &rscs[1]);
1517 dao = container_of(atc->daios[j], struct dao, daio);
1518 dao->ops->set_left_input(dao, rscs[0]);
1519 dao->ops->set_right_input(dao, rscs[1]);
1520 }
1521
1522 dai = container_of(atc->daios[LINEIM], struct dai, daio);
1523 atc_connect_dai(atc->rsc_mgrs[SRC], dai,
1524 (struct src **)&atc->srcs[2],
1525 (struct srcimp **)&atc->srcimps[2]);
1526 src = atc->srcs[2];
1527 mixer->set_input_left(mixer, MIX_LINE_IN, &src->rsc);
1528 src = atc->srcs[3];
1529 mixer->set_input_right(mixer, MIX_LINE_IN, &src->rsc);
1530
Harry Butterworth55309212011-06-11 16:02:06 +08001531 if (atc->model == CTSB1270) {
1532 /* Titanium HD has a dedicated ADC for the Mic. */
1533 dai = container_of(atc->daios[MIC], struct dai, daio);
1534 atc_connect_dai(atc->rsc_mgrs[SRC], dai,
1535 (struct src **)&atc->srcs[4],
1536 (struct srcimp **)&atc->srcimps[4]);
1537 src = atc->srcs[4];
1538 mixer->set_input_left(mixer, MIX_MIC_IN, &src->rsc);
1539 src = atc->srcs[5];
1540 mixer->set_input_right(mixer, MIX_MIC_IN, &src->rsc);
1541 }
1542
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001543 dai = container_of(atc->daios[SPDIFIO], struct dai, daio);
1544 atc_connect_dai(atc->rsc_mgrs[SRC], dai,
1545 (struct src **)&atc->srcs[0],
1546 (struct srcimp **)&atc->srcimps[0]);
1547
1548 src = atc->srcs[0];
1549 mixer->set_input_left(mixer, MIX_SPDIF_IN, &src->rsc);
1550 src = atc->srcs[1];
1551 mixer->set_input_right(mixer, MIX_SPDIF_IN, &src->rsc);
1552
1553 for (i = MIX_PCMI_FRONT, j = 0; i <= MIX_PCMI_SURROUND; i++, j += 2) {
1554 sum = atc->pcm[j];
1555 mixer->set_input_left(mixer, i, &sum->rsc);
1556 sum = atc->pcm[j+1];
1557 mixer->set_input_right(mixer, i, &sum->rsc);
1558 }
1559}
1560
Wai Yew CHAY29959a02009-06-22 14:52:34 +02001561#ifdef CONFIG_PM
1562static int atc_suspend(struct ct_atc *atc, pm_message_t state)
1563{
1564 int i;
1565 struct hw *hw = atc->hw;
1566
1567 snd_power_change_state(atc->card, SNDRV_CTL_POWER_D3hot);
1568
1569 for (i = FRONT; i < NUM_PCMS; i++) {
1570 if (!atc->pcms[i])
1571 continue;
1572
1573 snd_pcm_suspend_all(atc->pcms[i]);
1574 }
1575
1576 atc_release_resources(atc);
1577
1578 hw->suspend(hw, state);
1579
1580 return 0;
1581}
1582
1583static int atc_hw_resume(struct ct_atc *atc)
1584{
1585 struct hw *hw = atc->hw;
1586 struct card_conf info = {0};
1587
1588 /* Re-initialize card hardware. */
1589 info.rsr = atc->rsr;
1590 info.msr = atc->msr;
1591 info.vm_pgt_phys = atc_get_ptp_phys(atc, 0);
1592 return hw->resume(hw, &info);
1593}
1594
1595static int atc_resources_resume(struct ct_atc *atc)
1596{
1597 struct ct_mixer *mixer;
1598 int err = 0;
1599
1600 /* Get resources */
1601 err = atc_get_resources(atc);
1602 if (err < 0) {
1603 atc_release_resources(atc);
1604 return err;
1605 }
1606
1607 /* Build topology */
1608 atc_connect_resources(atc);
1609
1610 mixer = atc->mixer;
1611 mixer->resume(mixer);
1612
1613 return 0;
1614}
1615
1616static int atc_resume(struct ct_atc *atc)
1617{
1618 int err = 0;
1619
1620 /* Do hardware resume. */
1621 err = atc_hw_resume(atc);
1622 if (err < 0) {
1623 printk(KERN_ERR "ctxfi: pci_enable_device failed, "
1624 "disabling device\n");
1625 snd_card_disconnect(atc->card);
1626 return err;
1627 }
1628
1629 err = atc_resources_resume(atc);
1630 if (err < 0)
1631 return err;
1632
1633 snd_power_change_state(atc->card, SNDRV_CTL_POWER_D0);
1634
1635 return 0;
1636}
1637#endif
1638
Takashi Iwai2a36f672009-06-05 16:34:10 +02001639static struct ct_atc atc_preset __devinitdata = {
1640 .map_audio_buffer = ct_map_audio_buffer,
1641 .unmap_audio_buffer = ct_unmap_audio_buffer,
1642 .pcm_playback_prepare = atc_pcm_playback_prepare,
1643 .pcm_release_resources = atc_pcm_release_resources,
1644 .pcm_playback_start = atc_pcm_playback_start,
1645 .pcm_playback_stop = atc_pcm_stop,
1646 .pcm_playback_position = atc_pcm_playback_position,
1647 .pcm_capture_prepare = atc_pcm_capture_prepare,
1648 .pcm_capture_start = atc_pcm_capture_start,
1649 .pcm_capture_stop = atc_pcm_stop,
1650 .pcm_capture_position = atc_pcm_capture_position,
1651 .spdif_passthru_playback_prepare = spdif_passthru_playback_prepare,
1652 .get_ptp_phys = atc_get_ptp_phys,
1653 .select_line_in = atc_select_line_in,
1654 .select_mic_in = atc_select_mic_in,
1655 .select_digit_io = atc_select_digit_io,
1656 .line_front_unmute = atc_line_front_unmute,
1657 .line_surround_unmute = atc_line_surround_unmute,
1658 .line_clfe_unmute = atc_line_clfe_unmute,
1659 .line_rear_unmute = atc_line_rear_unmute,
1660 .line_in_unmute = atc_line_in_unmute,
Harry Butterworth55309212011-06-11 16:02:06 +08001661 .mic_unmute = atc_mic_unmute,
Takashi Iwai2a36f672009-06-05 16:34:10 +02001662 .spdif_out_unmute = atc_spdif_out_unmute,
1663 .spdif_in_unmute = atc_spdif_in_unmute,
1664 .spdif_out_get_status = atc_spdif_out_get_status,
1665 .spdif_out_set_status = atc_spdif_out_set_status,
1666 .spdif_out_passthru = atc_spdif_out_passthru,
1667 .have_digit_io_switch = atc_have_digit_io_switch,
Harry Butterworth55309212011-06-11 16:02:06 +08001668 .have_dedicated_mic = atc_have_dedicated_mic,
1669 .have_output_switch = atc_have_output_switch,
1670 .output_switch_get = atc_output_switch_get,
1671 .output_switch_put = atc_output_switch_put,
1672 .have_mic_source_switch = atc_have_mic_source_switch,
1673 .mic_source_switch_get = atc_mic_source_switch_get,
1674 .mic_source_switch_put = atc_mic_source_switch_put,
Wai Yew CHAY29959a02009-06-22 14:52:34 +02001675#ifdef CONFIG_PM
1676 .suspend = atc_suspend,
1677 .resume = atc_resume,
1678#endif
Takashi Iwai2a36f672009-06-05 16:34:10 +02001679};
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001680
1681/**
1682 * ct_atc_create - create and initialize a hardware manager
1683 * @card: corresponding alsa card object
1684 * @pci: corresponding kernel pci device object
1685 * @ratc: return created object address in it
1686 *
1687 * Creates and initializes a hardware manager.
1688 *
1689 * Creates kmallocated ct_atc structure. Initializes hardware.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001690 * Returns 0 if succeeds, or negative error code if fails.
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001691 */
1692
Takashi Iwai2a36f672009-06-05 16:34:10 +02001693int __devinit ct_atc_create(struct snd_card *card, struct pci_dev *pci,
Takashi Iwai94701952009-06-08 18:10:32 +02001694 unsigned int rsr, unsigned int msr,
Takashi Iwai408bffd2010-01-14 09:19:46 +01001695 int chip_type, unsigned int ssid,
1696 struct ct_atc **ratc)
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001697{
Takashi Iwai514eef92009-06-08 14:57:57 +02001698 struct ct_atc *atc;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001699 static struct snd_device_ops ops = {
1700 .dev_free = atc_dev_free,
1701 };
Takashi Iwai514eef92009-06-08 14:57:57 +02001702 int err;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001703
1704 *ratc = NULL;
1705
1706 atc = kzalloc(sizeof(*atc), GFP_KERNEL);
Takashi Iwai35ebf6e2009-07-22 17:12:34 +02001707 if (!atc)
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001708 return -ENOMEM;
1709
Takashi Iwai2a36f672009-06-05 16:34:10 +02001710 /* Set operations */
1711 *atc = atc_preset;
1712
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001713 atc->card = card;
1714 atc->pci = pci;
1715 atc->rsr = rsr;
1716 atc->msr = msr;
Takashi Iwai94701952009-06-08 18:10:32 +02001717 atc->chip_type = chip_type;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001718
Takashi Iwai635c2652009-06-13 10:12:59 +02001719 mutex_init(&atc->atc_mutex);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001720
1721 /* Find card model */
Takashi Iwai408bffd2010-01-14 09:19:46 +01001722 err = atc_identify_card(atc, ssid);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001723 if (err < 0) {
1724 printk(KERN_ERR "ctatc: Card not recognised\n");
1725 goto error1;
1726 }
1727
1728 /* Set up device virtual memory management object */
Jaroslav Kysela21956b62010-02-02 19:58:25 +01001729 err = ct_vm_create(&atc->vm, pci);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001730 if (err < 0)
1731 goto error1;
1732
1733 /* Create all atc hw devices */
1734 err = atc_create_hw_devs(atc);
1735 if (err < 0)
1736 goto error1;
1737
Wai Yew CHAY29959a02009-06-22 14:52:34 +02001738 err = ct_mixer_create(atc, (struct ct_mixer **)&atc->mixer);
1739 if (err) {
1740 printk(KERN_ERR "ctxfi: Failed to create mixer obj!!!\n");
1741 goto error1;
1742 }
1743
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001744 /* Get resources */
1745 err = atc_get_resources(atc);
1746 if (err < 0)
1747 goto error1;
1748
1749 /* Build topology */
1750 atc_connect_resources(atc);
1751
Takashi Iwaib7bbf872009-06-05 16:11:07 +02001752 atc->timer = ct_timer_new(atc);
1753 if (!atc->timer)
1754 goto error1;
1755
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001756 err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, atc, &ops);
1757 if (err < 0)
1758 goto error1;
1759
1760 snd_card_set_dev(card, &pci->dev);
1761
1762 *ratc = atc;
1763 return 0;
1764
1765error1:
1766 ct_atc_destroy(atc);
Takashi Iwaib3e0afe2009-05-14 15:19:30 +02001767 printk(KERN_ERR "ctxfi: Something wrong!!!\n");
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001768 return err;
1769}