blob: 480cb1e905b61af9a5c3c12aaed35da17376ecd5 [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>
27#include <sound/pcm.h>
28#include <sound/control.h>
29#include <sound/asoundef.h>
30
31#define MONO_SUM_SCALE 0x19a8 /* 2^(-0.5) in 14-bit floating format */
32#define DAIONUM 7
33#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),
55 SND_PCI_QUIRK(PCI_VENDOR_ID_CREATIVE, PCI_SUBDEVICE_ID_CREATIVE_SB08801,
56 "SB0880", CTSB0880),
57 SND_PCI_QUIRK(PCI_VENDOR_ID_CREATIVE, PCI_SUBDEVICE_ID_CREATIVE_SB08802,
58 "SB0880", CTSB0880),
59 SND_PCI_QUIRK(PCI_VENDOR_ID_CREATIVE, PCI_SUBDEVICE_ID_CREATIVE_SB08803,
60 "SB0880", CTSB0880),
Takashi Iwai09521d22009-06-08 18:29:38 +020061 SND_PCI_QUIRK_MASK(PCI_VENDOR_ID_CREATIVE, 0xf000,
62 PCI_SUBDEVICE_ID_CREATIVE_HENDRIX, "HENDRIX",
63 CTHENDRIX),
Takashi Iwai94701952009-06-08 18:10:32 +020064 { } /* terminator */
65};
66
67static const char *ct_subsys_name[NUM_CTCARDS] = {
Takashi Iwaia8f43102009-06-22 07:36:52 +020068 /* 20k1 models */
Takashi Iwai94701952009-06-08 18:10:32 +020069 [CTSB055X] = "SB055x",
70 [CTSB073X] = "SB073x",
Takashi Iwai94701952009-06-08 18:10:32 +020071 [CTUAA] = "UAA",
72 [CT20K1_UNKNOWN] = "Unknown",
Takashi Iwaia8f43102009-06-22 07:36:52 +020073 /* 20k2 models */
74 [CTSB0760] = "SB076x",
Takashi Iwai94701952009-06-08 18:10:32 +020075 [CTHENDRIX] = "Hendrix",
76 [CTSB0880] = "SB0880",
Takashi Iwaia8f43102009-06-22 07:36:52 +020077 [CT20K2_UNKNOWN] = "Unknown",
Wai Yew CHAY8cc72362009-05-14 08:05:58 +020078};
79
80static struct {
81 int (*create)(struct ct_atc *atc,
82 enum CTALSADEVS device, const char *device_name);
83 int (*destroy)(void *alsa_dev);
84 const char *public_name;
85} alsa_dev_funcs[NUM_CTALSADEVS] = {
86 [FRONT] = { .create = ct_alsa_pcm_create,
87 .destroy = NULL,
88 .public_name = "Front/WaveIn"},
Wai Yew CHAY8cc72362009-05-14 08:05:58 +020089 [SURROUND] = { .create = ct_alsa_pcm_create,
90 .destroy = NULL,
91 .public_name = "Surround"},
Takashi Iwai8372d4982009-06-02 14:27:56 +020092 [CLFE] = { .create = ct_alsa_pcm_create,
93 .destroy = NULL,
94 .public_name = "Center/LFE"},
95 [SIDE] = { .create = ct_alsa_pcm_create,
96 .destroy = NULL,
97 .public_name = "Side"},
Wai Yew CHAY8cc72362009-05-14 08:05:58 +020098 [IEC958] = { .create = ct_alsa_pcm_create,
99 .destroy = NULL,
100 .public_name = "IEC958 Non-audio"},
101
102 [MIXER] = { .create = ct_alsa_mix_create,
103 .destroy = NULL,
104 .public_name = "Mixer"}
105};
106
107typedef int (*create_t)(void *, void **);
108typedef int (*destroy_t)(void *);
109
110static struct {
111 int (*create)(void *hw, void **rmgr);
112 int (*destroy)(void *mgr);
113} rsc_mgr_funcs[NUM_RSCTYP] = {
114 [SRC] = { .create = (create_t)src_mgr_create,
115 .destroy = (destroy_t)src_mgr_destroy },
116 [SRCIMP] = { .create = (create_t)srcimp_mgr_create,
117 .destroy = (destroy_t)srcimp_mgr_destroy },
118 [AMIXER] = { .create = (create_t)amixer_mgr_create,
119 .destroy = (destroy_t)amixer_mgr_destroy },
120 [SUM] = { .create = (create_t)sum_mgr_create,
121 .destroy = (destroy_t)sum_mgr_destroy },
122 [DAIO] = { .create = (create_t)daio_mgr_create,
123 .destroy = (destroy_t)daio_mgr_destroy }
124};
125
126static int
127atc_pcm_release_resources(struct ct_atc *atc, struct ct_atc_pcm *apcm);
128
129/* *
130 * Only mono and interleaved modes are supported now.
131 * Always allocates a contiguous channel block.
132 * */
133
134static int ct_map_audio_buffer(struct ct_atc *atc, struct ct_atc_pcm *apcm)
135{
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200136 struct snd_pcm_runtime *runtime;
137 struct ct_vm *vm;
138
Takashi Iwai35ebf6e2009-07-22 17:12:34 +0200139 if (!apcm->substream)
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200140 return 0;
141
142 runtime = apcm->substream->runtime;
143 vm = atc->vm;
144
Takashi Iwaic76157d2009-06-02 15:26:19 +0200145 apcm->vm_block = vm->map(vm, apcm->substream, runtime->dma_bytes);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200146
Takashi Iwai35ebf6e2009-07-22 17:12:34 +0200147 if (!apcm->vm_block)
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200148 return -ENOENT;
149
150 return 0;
151}
152
153static void ct_unmap_audio_buffer(struct ct_atc *atc, struct ct_atc_pcm *apcm)
154{
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200155 struct ct_vm *vm;
156
Takashi Iwai35ebf6e2009-07-22 17:12:34 +0200157 if (!apcm->vm_block)
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200158 return;
159
160 vm = atc->vm;
161
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200162 vm->unmap(vm, apcm->vm_block);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200163
164 apcm->vm_block = NULL;
165}
166
167static unsigned long atc_get_ptp_phys(struct ct_atc *atc, int index)
168{
Jaroslav Kysela21956b62010-02-02 19:58:25 +0100169 return atc->vm->get_ptp_phys(atc->vm, index);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200170}
171
172static unsigned int convert_format(snd_pcm_format_t snd_format)
173{
174 switch (snd_format) {
175 case SNDRV_PCM_FORMAT_U8:
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200176 return SRC_SF_U8;
177 case SNDRV_PCM_FORMAT_S16_LE:
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200178 return SRC_SF_S16;
179 case SNDRV_PCM_FORMAT_S24_3LE:
180 return SRC_SF_S24;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200181 case SNDRV_PCM_FORMAT_S32_LE:
182 return SRC_SF_S32;
Takashi Iwaid2b9b962009-06-02 14:39:05 +0200183 case SNDRV_PCM_FORMAT_FLOAT_LE:
184 return SRC_SF_F32;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200185 default:
Takashi Iwaib3e0afe2009-05-14 15:19:30 +0200186 printk(KERN_ERR "ctxfi: not recognized snd format is %d \n",
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200187 snd_format);
188 return SRC_SF_S16;
189 }
190}
191
192static unsigned int
193atc_get_pitch(unsigned int input_rate, unsigned int output_rate)
194{
Takashi Iwai514eef92009-06-08 14:57:57 +0200195 unsigned int pitch;
196 int b;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200197
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
221static int select_rom(unsigned int pitch)
222{
Takashi Iwai35ebf6e2009-07-22 17:12:34 +0200223 if (pitch > 0x00428f5c && pitch < 0x01b851ec) {
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200224 /* 0.26 <= pitch <= 1.72 */
225 return 1;
Takashi Iwai35ebf6e2009-07-22 17:12:34 +0200226 } else if (pitch == 0x01d66666 || pitch == 0x01d66667) {
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200227 /* pitch == 1.8375 */
228 return 2;
Takashi Iwai35ebf6e2009-07-22 17:12:34 +0200229 } else if (pitch == 0x02000000) {
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200230 /* pitch == 2 */
231 return 3;
Roel Kluin84ed1a12009-10-23 16:03:08 +0200232 } else if (pitch <= 0x08000000) {
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200233 /* 0 <= pitch <= 8 */
234 return 0;
235 } else {
236 return -ENOENT;
237 }
238}
239
240static 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};
Takashi Iwai514eef92009-06-08 14:57:57 +0200246 struct src *src;
247 struct amixer *amixer;
248 int err;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200249 int n_amixer = apcm->substream->runtime->channels, i = 0;
250 int device = apcm->substream->pcm->device;
Takashi Iwai514eef92009-06-08 14:57:57 +0200251 unsigned int pitch;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200252
Takashi Iwai822fa192009-06-02 14:12:17 +0200253 /* first release old resources */
Wai Yew CHAY29959a02009-06-22 14:52:34 +0200254 atc_pcm_release_resources(atc, apcm);
Takashi Iwai822fa192009-06-02 14:12:17 +0200255
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200256 /* 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 src->ops->set_pm(src, (src->ops->next_interleave(src) != NULL));
271
272 /* Get AMIXER resource */
273 n_amixer = (n_amixer < 2) ? 2 : n_amixer;
274 apcm->amixers = kzalloc(sizeof(void *)*n_amixer, GFP_KERNEL);
Takashi Iwai35ebf6e2009-07-22 17:12:34 +0200275 if (!apcm->amixers) {
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200276 err = -ENOMEM;
277 goto error1;
278 }
279 mix_dsc.msr = atc->msr;
280 for (i = 0, apcm->n_amixer = 0; i < n_amixer; i++) {
281 err = amixer_mgr->get_amixer(amixer_mgr, &mix_dsc,
282 (struct amixer **)&apcm->amixers[i]);
283 if (err)
284 goto error1;
285
286 apcm->n_amixer++;
287 }
288
289 /* Set up device virtual mem map */
290 err = ct_map_audio_buffer(atc, apcm);
291 if (err < 0)
292 goto error1;
293
294 /* Connect resources */
295 src = apcm->src;
296 for (i = 0; i < n_amixer; i++) {
297 amixer = apcm->amixers[i];
Takashi Iwai635c2652009-06-13 10:12:59 +0200298 mutex_lock(&atc->atc_mutex);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200299 amixer->ops->setup(amixer, &src->rsc,
300 INIT_VOL, atc->pcm[i+device*2]);
Takashi Iwai635c2652009-06-13 10:12:59 +0200301 mutex_unlock(&atc->atc_mutex);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200302 src = src->ops->next_interleave(src);
Takashi Iwai35ebf6e2009-07-22 17:12:34 +0200303 if (!src)
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200304 src = apcm->src;
305 }
306
Takashi Iwaib7bbf872009-06-05 16:11:07 +0200307 ct_timer_prepare(apcm->timer);
308
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200309 return 0;
310
311error1:
312 atc_pcm_release_resources(atc, apcm);
313 return err;
314}
315
316static int
317atc_pcm_release_resources(struct ct_atc *atc, struct ct_atc_pcm *apcm)
318{
319 struct src_mgr *src_mgr = atc->rsc_mgrs[SRC];
320 struct srcimp_mgr *srcimp_mgr = atc->rsc_mgrs[SRCIMP];
321 struct amixer_mgr *amixer_mgr = atc->rsc_mgrs[AMIXER];
322 struct sum_mgr *sum_mgr = atc->rsc_mgrs[SUM];
Takashi Iwai514eef92009-06-08 14:57:57 +0200323 struct srcimp *srcimp;
324 int i;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200325
Takashi Iwai35ebf6e2009-07-22 17:12:34 +0200326 if (apcm->srcimps) {
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200327 for (i = 0; i < apcm->n_srcimp; i++) {
328 srcimp = apcm->srcimps[i];
329 srcimp->ops->unmap(srcimp);
330 srcimp_mgr->put_srcimp(srcimp_mgr, srcimp);
331 apcm->srcimps[i] = NULL;
332 }
333 kfree(apcm->srcimps);
334 apcm->srcimps = NULL;
335 }
336
Takashi Iwai35ebf6e2009-07-22 17:12:34 +0200337 if (apcm->srccs) {
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200338 for (i = 0; i < apcm->n_srcc; i++) {
339 src_mgr->put_src(src_mgr, apcm->srccs[i]);
340 apcm->srccs[i] = NULL;
341 }
342 kfree(apcm->srccs);
343 apcm->srccs = NULL;
344 }
345
Takashi Iwai35ebf6e2009-07-22 17:12:34 +0200346 if (apcm->amixers) {
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200347 for (i = 0; i < apcm->n_amixer; i++) {
348 amixer_mgr->put_amixer(amixer_mgr, apcm->amixers[i]);
349 apcm->amixers[i] = NULL;
350 }
351 kfree(apcm->amixers);
352 apcm->amixers = NULL;
353 }
354
Takashi Iwai35ebf6e2009-07-22 17:12:34 +0200355 if (apcm->mono) {
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200356 sum_mgr->put_sum(sum_mgr, apcm->mono);
357 apcm->mono = NULL;
358 }
359
Takashi Iwai35ebf6e2009-07-22 17:12:34 +0200360 if (apcm->src) {
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200361 src_mgr->put_src(src_mgr, apcm->src);
362 apcm->src = NULL;
363 }
364
Takashi Iwai35ebf6e2009-07-22 17:12:34 +0200365 if (apcm->vm_block) {
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200366 /* Undo device virtual mem map */
367 ct_unmap_audio_buffer(atc, apcm);
368 apcm->vm_block = NULL;
369 }
370
371 return 0;
372}
373
374static int atc_pcm_playback_start(struct ct_atc *atc, struct ct_atc_pcm *apcm)
375{
Takashi Iwai514eef92009-06-08 14:57:57 +0200376 unsigned int max_cisz;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200377 struct src *src = apcm->src;
378
Takashi Iwaic399f3b2009-06-09 08:16:20 +0200379 if (apcm->started)
380 return 0;
381 apcm->started = 1;
382
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200383 max_cisz = src->multi * src->rsc.msr;
384 max_cisz = 0x80 * (max_cisz < 8 ? max_cisz : 8);
385
386 src->ops->set_sa(src, apcm->vm_block->addr);
387 src->ops->set_la(src, apcm->vm_block->addr + apcm->vm_block->size);
388 src->ops->set_ca(src, apcm->vm_block->addr + max_cisz);
389 src->ops->set_cisz(src, max_cisz);
390
391 src->ops->set_bm(src, 1);
392 src->ops->set_state(src, SRC_STATE_INIT);
393 src->ops->commit_write(src);
394
Takashi Iwaib7bbf872009-06-05 16:11:07 +0200395 ct_timer_start(apcm->timer);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200396 return 0;
397}
398
399static int atc_pcm_stop(struct ct_atc *atc, struct ct_atc_pcm *apcm)
400{
Takashi Iwai514eef92009-06-08 14:57:57 +0200401 struct src *src;
402 int i;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200403
Takashi Iwaib7bbf872009-06-05 16:11:07 +0200404 ct_timer_stop(apcm->timer);
405
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200406 src = apcm->src;
407 src->ops->set_bm(src, 0);
408 src->ops->set_state(src, SRC_STATE_OFF);
409 src->ops->commit_write(src);
410
Takashi Iwai35ebf6e2009-07-22 17:12:34 +0200411 if (apcm->srccs) {
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200412 for (i = 0; i < apcm->n_srcc; i++) {
413 src = apcm->srccs[i];
414 src->ops->set_bm(src, 0);
415 src->ops->set_state(src, SRC_STATE_OFF);
416 src->ops->commit_write(src);
417 }
418 }
419
420 apcm->started = 0;
421
422 return 0;
423}
424
425static int
426atc_pcm_playback_position(struct ct_atc *atc, struct ct_atc_pcm *apcm)
427{
428 struct src *src = apcm->src;
Takashi Iwai514eef92009-06-08 14:57:57 +0200429 u32 size, max_cisz;
430 int position;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200431
Takashi Iwai5242bc72009-06-09 08:17:14 +0200432 if (!src)
433 return 0;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200434 position = src->ops->get_ca(src);
435
436 size = apcm->vm_block->size;
437 max_cisz = src->multi * src->rsc.msr;
438 max_cisz = 128 * (max_cisz < 8 ? max_cisz : 8);
439
440 return (position + size - max_cisz - apcm->vm_block->addr) % size;
441}
442
443struct src_node_conf_t {
444 unsigned int pitch;
445 unsigned int msr:8;
446 unsigned int mix_msr:8;
447 unsigned int imp_msr:8;
448 unsigned int vo:1;
449};
450
451static void setup_src_node_conf(struct ct_atc *atc, struct ct_atc_pcm *apcm,
452 struct src_node_conf_t *conf, int *n_srcc)
453{
Takashi Iwai514eef92009-06-08 14:57:57 +0200454 unsigned int pitch;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200455
456 /* get pitch and convert to fixed-point 8.24 format. */
457 pitch = atc_get_pitch((atc->rsr * atc->msr),
458 apcm->substream->runtime->rate);
459 *n_srcc = 0;
460
461 if (1 == atc->msr) {
462 *n_srcc = apcm->substream->runtime->channels;
463 conf[0].pitch = pitch;
464 conf[0].mix_msr = conf[0].imp_msr = conf[0].msr = 1;
465 conf[0].vo = 1;
466 } else if (2 == atc->msr) {
467 if (0x8000000 < pitch) {
468 /* Need two-stage SRCs, SRCIMPs and
469 * AMIXERs for converting format */
470 conf[0].pitch = (atc->msr << 24);
471 conf[0].msr = conf[0].mix_msr = 1;
472 conf[0].imp_msr = atc->msr;
473 conf[0].vo = 0;
474 conf[1].pitch = atc_get_pitch(atc->rsr,
475 apcm->substream->runtime->rate);
476 conf[1].msr = conf[1].mix_msr = conf[1].imp_msr = 1;
477 conf[1].vo = 1;
478 *n_srcc = apcm->substream->runtime->channels * 2;
479 } else if (0x1000000 < pitch) {
480 /* Need one-stage SRCs, SRCIMPs and
481 * AMIXERs for converting format */
482 conf[0].pitch = pitch;
483 conf[0].msr = conf[0].mix_msr
484 = conf[0].imp_msr = atc->msr;
485 conf[0].vo = 1;
486 *n_srcc = apcm->substream->runtime->channels;
487 }
488 }
489}
490
491static int
492atc_pcm_capture_get_resources(struct ct_atc *atc, struct ct_atc_pcm *apcm)
493{
494 struct src_mgr *src_mgr = atc->rsc_mgrs[SRC];
495 struct srcimp_mgr *srcimp_mgr = atc->rsc_mgrs[SRCIMP];
496 struct amixer_mgr *amixer_mgr = atc->rsc_mgrs[AMIXER];
497 struct sum_mgr *sum_mgr = atc->rsc_mgrs[SUM];
498 struct src_desc src_dsc = {0};
Takashi Iwai514eef92009-06-08 14:57:57 +0200499 struct src *src;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200500 struct srcimp_desc srcimp_dsc = {0};
Takashi Iwai514eef92009-06-08 14:57:57 +0200501 struct srcimp *srcimp;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200502 struct amixer_desc mix_dsc = {0};
503 struct sum_desc sum_dsc = {0};
Takashi Iwai514eef92009-06-08 14:57:57 +0200504 unsigned int pitch;
505 int multi, err, i;
506 int n_srcimp, n_amixer, n_srcc, n_sum;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200507 struct src_node_conf_t src_node_conf[2] = {{0} };
508
Takashi Iwai822fa192009-06-02 14:12:17 +0200509 /* first release old resources */
Takashi Iwaia5990dc2009-06-09 08:19:02 +0200510 atc_pcm_release_resources(atc, apcm);
Takashi Iwai822fa192009-06-02 14:12:17 +0200511
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200512 /* The numbers of converting SRCs and SRCIMPs should be determined
513 * by pitch value. */
514
515 multi = apcm->substream->runtime->channels;
516
517 /* get pitch and convert to fixed-point 8.24 format. */
518 pitch = atc_get_pitch((atc->rsr * atc->msr),
519 apcm->substream->runtime->rate);
520
521 setup_src_node_conf(atc, apcm, src_node_conf, &n_srcc);
522 n_sum = (1 == multi) ? 1 : 0;
Takashi Iwai514eef92009-06-08 14:57:57 +0200523 n_amixer = n_sum * 2 + n_srcc;
524 n_srcimp = n_srcc;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200525 if ((multi > 1) && (0x8000000 >= pitch)) {
526 /* Need extra AMIXERs and SRCIMPs for special treatment
527 * of interleaved recording of conjugate channels */
528 n_amixer += multi * atc->msr;
529 n_srcimp += multi * atc->msr;
530 } else {
531 n_srcimp += multi;
532 }
533
534 if (n_srcc) {
535 apcm->srccs = kzalloc(sizeof(void *)*n_srcc, GFP_KERNEL);
Takashi Iwai35ebf6e2009-07-22 17:12:34 +0200536 if (!apcm->srccs)
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200537 return -ENOMEM;
538 }
539 if (n_amixer) {
540 apcm->amixers = kzalloc(sizeof(void *)*n_amixer, GFP_KERNEL);
Takashi Iwai35ebf6e2009-07-22 17:12:34 +0200541 if (!apcm->amixers) {
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200542 err = -ENOMEM;
543 goto error1;
544 }
545 }
546 apcm->srcimps = kzalloc(sizeof(void *)*n_srcimp, GFP_KERNEL);
Takashi Iwai35ebf6e2009-07-22 17:12:34 +0200547 if (!apcm->srcimps) {
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200548 err = -ENOMEM;
549 goto error1;
550 }
551
552 /* Allocate SRCs for sample rate conversion if needed */
553 src_dsc.multi = 1;
554 src_dsc.mode = ARCRW;
555 for (i = 0, apcm->n_srcc = 0; i < n_srcc; i++) {
556 src_dsc.msr = src_node_conf[i/multi].msr;
557 err = src_mgr->get_src(src_mgr, &src_dsc,
558 (struct src **)&apcm->srccs[i]);
559 if (err)
560 goto error1;
561
562 src = apcm->srccs[i];
563 pitch = src_node_conf[i/multi].pitch;
564 src->ops->set_pitch(src, pitch);
565 src->ops->set_rom(src, select_rom(pitch));
566 src->ops->set_vo(src, src_node_conf[i/multi].vo);
567
568 apcm->n_srcc++;
569 }
570
571 /* Allocate AMIXERs for routing SRCs of conversion if needed */
572 for (i = 0, apcm->n_amixer = 0; i < n_amixer; i++) {
573 if (i < (n_sum*2))
574 mix_dsc.msr = atc->msr;
575 else if (i < (n_sum*2+n_srcc))
576 mix_dsc.msr = src_node_conf[(i-n_sum*2)/multi].mix_msr;
577 else
578 mix_dsc.msr = 1;
579
580 err = amixer_mgr->get_amixer(amixer_mgr, &mix_dsc,
581 (struct amixer **)&apcm->amixers[i]);
582 if (err)
583 goto error1;
584
585 apcm->n_amixer++;
586 }
587
588 /* Allocate a SUM resource to mix all input channels together */
589 sum_dsc.msr = atc->msr;
590 err = sum_mgr->get_sum(sum_mgr, &sum_dsc, (struct sum **)&apcm->mono);
591 if (err)
592 goto error1;
593
594 pitch = atc_get_pitch((atc->rsr * atc->msr),
595 apcm->substream->runtime->rate);
596 /* Allocate SRCIMP resources */
597 for (i = 0, apcm->n_srcimp = 0; i < n_srcimp; i++) {
598 if (i < (n_srcc))
599 srcimp_dsc.msr = src_node_conf[i/multi].imp_msr;
600 else if (1 == multi)
601 srcimp_dsc.msr = (pitch <= 0x8000000) ? atc->msr : 1;
602 else
603 srcimp_dsc.msr = 1;
604
605 err = srcimp_mgr->get_srcimp(srcimp_mgr, &srcimp_dsc, &srcimp);
606 if (err)
607 goto error1;
608
609 apcm->srcimps[i] = srcimp;
610 apcm->n_srcimp++;
611 }
612
613 /* Allocate a SRC for writing data to host memory */
614 src_dsc.multi = apcm->substream->runtime->channels;
615 src_dsc.msr = 1;
616 src_dsc.mode = MEMWR;
617 err = src_mgr->get_src(src_mgr, &src_dsc, (struct src **)&apcm->src);
618 if (err)
619 goto error1;
620
621 src = apcm->src;
622 src->ops->set_pitch(src, pitch);
623
624 /* Set up device virtual mem map */
625 err = ct_map_audio_buffer(atc, apcm);
626 if (err < 0)
627 goto error1;
628
629 return 0;
630
631error1:
632 atc_pcm_release_resources(atc, apcm);
633 return err;
634}
635
636static int atc_pcm_capture_prepare(struct ct_atc *atc, struct ct_atc_pcm *apcm)
637{
Takashi Iwai514eef92009-06-08 14:57:57 +0200638 struct src *src;
639 struct amixer *amixer;
640 struct srcimp *srcimp;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200641 struct ct_mixer *mixer = atc->mixer;
Takashi Iwai514eef92009-06-08 14:57:57 +0200642 struct sum *mono;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200643 struct rsc *out_ports[8] = {NULL};
Takashi Iwai514eef92009-06-08 14:57:57 +0200644 int err, i, j, n_sum, multi;
645 unsigned int pitch;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200646 int mix_base = 0, imp_base = 0;
647
Wai Yew CHAY29959a02009-06-22 14:52:34 +0200648 atc_pcm_release_resources(atc, apcm);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200649
650 /* Get needed resources. */
651 err = atc_pcm_capture_get_resources(atc, apcm);
652 if (err)
653 return err;
654
655 /* Connect resources */
656 mixer->get_output_ports(mixer, MIX_PCMO_FRONT,
657 &out_ports[0], &out_ports[1]);
658
659 multi = apcm->substream->runtime->channels;
660 if (1 == multi) {
661 mono = apcm->mono;
662 for (i = 0; i < 2; i++) {
663 amixer = apcm->amixers[i];
664 amixer->ops->setup(amixer, out_ports[i],
665 MONO_SUM_SCALE, mono);
666 }
667 out_ports[0] = &mono->rsc;
668 n_sum = 1;
669 mix_base = n_sum * 2;
670 }
671
672 for (i = 0; i < apcm->n_srcc; i++) {
673 src = apcm->srccs[i];
674 srcimp = apcm->srcimps[imp_base+i];
675 amixer = apcm->amixers[mix_base+i];
676 srcimp->ops->map(srcimp, src, out_ports[i%multi]);
677 amixer->ops->setup(amixer, &src->rsc, INIT_VOL, NULL);
678 out_ports[i%multi] = &amixer->rsc;
679 }
680
681 pitch = atc_get_pitch((atc->rsr * atc->msr),
682 apcm->substream->runtime->rate);
683
684 if ((multi > 1) && (pitch <= 0x8000000)) {
685 /* Special connection for interleaved
686 * recording with conjugate channels */
687 for (i = 0; i < multi; i++) {
688 out_ports[i]->ops->master(out_ports[i]);
689 for (j = 0; j < atc->msr; j++) {
690 amixer = apcm->amixers[apcm->n_srcc+j*multi+i];
691 amixer->ops->set_input(amixer, out_ports[i]);
692 amixer->ops->set_scale(amixer, INIT_VOL);
693 amixer->ops->set_sum(amixer, NULL);
694 amixer->ops->commit_raw_write(amixer);
695 out_ports[i]->ops->next_conj(out_ports[i]);
696
697 srcimp = apcm->srcimps[apcm->n_srcc+j*multi+i];
698 srcimp->ops->map(srcimp, apcm->src,
699 &amixer->rsc);
700 }
701 }
702 } else {
703 for (i = 0; i < multi; i++) {
704 srcimp = apcm->srcimps[apcm->n_srcc+i];
705 srcimp->ops->map(srcimp, apcm->src, out_ports[i]);
706 }
707 }
708
Takashi Iwaib7bbf872009-06-05 16:11:07 +0200709 ct_timer_prepare(apcm->timer);
710
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200711 return 0;
712}
713
714static int atc_pcm_capture_start(struct ct_atc *atc, struct ct_atc_pcm *apcm)
715{
Takashi Iwai514eef92009-06-08 14:57:57 +0200716 struct src *src;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200717 struct src_mgr *src_mgr = atc->rsc_mgrs[SRC];
Takashi Iwai514eef92009-06-08 14:57:57 +0200718 int i, multi;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200719
720 if (apcm->started)
721 return 0;
722
723 apcm->started = 1;
724 multi = apcm->substream->runtime->channels;
725 /* Set up converting SRCs */
726 for (i = 0; i < apcm->n_srcc; i++) {
727 src = apcm->srccs[i];
728 src->ops->set_pm(src, ((i%multi) != (multi-1)));
729 src_mgr->src_disable(src_mgr, src);
730 }
731
732 /* Set up recording SRC */
733 src = apcm->src;
734 src->ops->set_sf(src, convert_format(apcm->substream->runtime->format));
735 src->ops->set_sa(src, apcm->vm_block->addr);
736 src->ops->set_la(src, apcm->vm_block->addr + apcm->vm_block->size);
737 src->ops->set_ca(src, apcm->vm_block->addr);
738 src_mgr->src_disable(src_mgr, src);
739
740 /* Disable relevant SRCs firstly */
741 src_mgr->commit_write(src_mgr);
742
743 /* Enable SRCs respectively */
744 for (i = 0; i < apcm->n_srcc; i++) {
745 src = apcm->srccs[i];
746 src->ops->set_state(src, SRC_STATE_RUN);
747 src->ops->commit_write(src);
748 src_mgr->src_enable_s(src_mgr, src);
749 }
750 src = apcm->src;
751 src->ops->set_bm(src, 1);
752 src->ops->set_state(src, SRC_STATE_RUN);
753 src->ops->commit_write(src);
754 src_mgr->src_enable_s(src_mgr, src);
755
756 /* Enable relevant SRCs synchronously */
757 src_mgr->commit_write(src_mgr);
758
Takashi Iwaib7bbf872009-06-05 16:11:07 +0200759 ct_timer_start(apcm->timer);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200760 return 0;
761}
762
763static int
764atc_pcm_capture_position(struct ct_atc *atc, struct ct_atc_pcm *apcm)
765{
766 struct src *src = apcm->src;
767
Takashi Iwai5242bc72009-06-09 08:17:14 +0200768 if (!src)
769 return 0;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200770 return src->ops->get_ca(src) - apcm->vm_block->addr;
771}
772
773static int spdif_passthru_playback_get_resources(struct ct_atc *atc,
774 struct ct_atc_pcm *apcm)
775{
776 struct src_mgr *src_mgr = atc->rsc_mgrs[SRC];
777 struct amixer_mgr *amixer_mgr = atc->rsc_mgrs[AMIXER];
778 struct src_desc desc = {0};
779 struct amixer_desc mix_dsc = {0};
Takashi Iwai514eef92009-06-08 14:57:57 +0200780 struct src *src;
781 int err;
782 int n_amixer = apcm->substream->runtime->channels, i;
783 unsigned int pitch, rsr = atc->pll_rate;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200784
Takashi Iwai822fa192009-06-02 14:12:17 +0200785 /* first release old resources */
Takashi Iwaia5990dc2009-06-09 08:19:02 +0200786 atc_pcm_release_resources(atc, apcm);
Takashi Iwai822fa192009-06-02 14:12:17 +0200787
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200788 /* Get SRC resource */
789 desc.multi = apcm->substream->runtime->channels;
790 desc.msr = 1;
791 while (apcm->substream->runtime->rate > (rsr * desc.msr))
792 desc.msr <<= 1;
793
794 desc.mode = MEMRD;
795 err = src_mgr->get_src(src_mgr, &desc, (struct src **)&apcm->src);
796 if (err)
797 goto error1;
798
799 pitch = atc_get_pitch(apcm->substream->runtime->rate, (rsr * desc.msr));
800 src = apcm->src;
801 src->ops->set_pitch(src, pitch);
802 src->ops->set_rom(src, select_rom(pitch));
803 src->ops->set_sf(src, convert_format(apcm->substream->runtime->format));
804 src->ops->set_pm(src, (src->ops->next_interleave(src) != NULL));
805 src->ops->set_bp(src, 1);
806
807 /* Get AMIXER resource */
808 n_amixer = (n_amixer < 2) ? 2 : n_amixer;
809 apcm->amixers = kzalloc(sizeof(void *)*n_amixer, GFP_KERNEL);
Takashi Iwai35ebf6e2009-07-22 17:12:34 +0200810 if (!apcm->amixers) {
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200811 err = -ENOMEM;
812 goto error1;
813 }
814 mix_dsc.msr = desc.msr;
815 for (i = 0, apcm->n_amixer = 0; i < n_amixer; i++) {
816 err = amixer_mgr->get_amixer(amixer_mgr, &mix_dsc,
817 (struct amixer **)&apcm->amixers[i]);
818 if (err)
819 goto error1;
820
821 apcm->n_amixer++;
822 }
823
824 /* Set up device virtual mem map */
825 err = ct_map_audio_buffer(atc, apcm);
826 if (err < 0)
827 goto error1;
828
829 return 0;
830
831error1:
832 atc_pcm_release_resources(atc, apcm);
833 return err;
834}
835
Takashi Iwai514eef92009-06-08 14:57:57 +0200836static int atc_pll_init(struct ct_atc *atc, int rate)
837{
838 struct hw *hw = atc->hw;
839 int err;
840 err = hw->pll_init(hw, rate);
841 atc->pll_rate = err ? 0 : rate;
842 return err;
843}
844
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200845static int
846spdif_passthru_playback_setup(struct ct_atc *atc, struct ct_atc_pcm *apcm)
847{
848 struct dao *dao = container_of(atc->daios[SPDIFOO], struct dao, daio);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200849 unsigned int rate = apcm->substream->runtime->rate;
Takashi Iwai514eef92009-06-08 14:57:57 +0200850 unsigned int status;
Wai Yew CHAY29959a02009-06-22 14:52:34 +0200851 int err = 0;
Takashi Iwai514eef92009-06-08 14:57:57 +0200852 unsigned char iec958_con_fs;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200853
854 switch (rate) {
855 case 48000:
856 iec958_con_fs = IEC958_AES3_CON_FS_48000;
857 break;
858 case 44100:
859 iec958_con_fs = IEC958_AES3_CON_FS_44100;
860 break;
861 case 32000:
862 iec958_con_fs = IEC958_AES3_CON_FS_32000;
863 break;
864 default:
865 return -ENOENT;
866 }
867
Takashi Iwai635c2652009-06-13 10:12:59 +0200868 mutex_lock(&atc->atc_mutex);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200869 dao->ops->get_spos(dao, &status);
870 if (((status >> 24) & IEC958_AES3_CON_FS) != iec958_con_fs) {
871 status &= ((~IEC958_AES3_CON_FS) << 24);
872 status |= (iec958_con_fs << 24);
873 dao->ops->set_spos(dao, status);
874 dao->ops->commit_write(dao);
875 }
Takashi Iwai514eef92009-06-08 14:57:57 +0200876 if ((rate != atc->pll_rate) && (32000 != rate))
877 err = atc_pll_init(atc, rate);
Takashi Iwai635c2652009-06-13 10:12:59 +0200878 mutex_unlock(&atc->atc_mutex);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200879
880 return err;
881}
882
883static int
884spdif_passthru_playback_prepare(struct ct_atc *atc, struct ct_atc_pcm *apcm)
885{
Takashi Iwai514eef92009-06-08 14:57:57 +0200886 struct src *src;
887 struct amixer *amixer;
888 struct dao *dao;
889 int err;
890 int i;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200891
Wai Yew CHAY29959a02009-06-22 14:52:34 +0200892 atc_pcm_release_resources(atc, apcm);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200893
894 /* Configure SPDIFOO and PLL to passthrough mode;
895 * determine pll_rate. */
896 err = spdif_passthru_playback_setup(atc, apcm);
897 if (err)
898 return err;
899
900 /* Get needed resources. */
901 err = spdif_passthru_playback_get_resources(atc, apcm);
902 if (err)
903 return err;
904
905 /* Connect resources */
906 src = apcm->src;
907 for (i = 0; i < apcm->n_amixer; i++) {
908 amixer = apcm->amixers[i];
909 amixer->ops->setup(amixer, &src->rsc, INIT_VOL, NULL);
910 src = src->ops->next_interleave(src);
Takashi Iwai35ebf6e2009-07-22 17:12:34 +0200911 if (!src)
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200912 src = apcm->src;
913 }
914 /* Connect to SPDIFOO */
Takashi Iwai635c2652009-06-13 10:12:59 +0200915 mutex_lock(&atc->atc_mutex);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200916 dao = container_of(atc->daios[SPDIFOO], struct dao, daio);
917 amixer = apcm->amixers[0];
918 dao->ops->set_left_input(dao, &amixer->rsc);
919 amixer = apcm->amixers[1];
920 dao->ops->set_right_input(dao, &amixer->rsc);
Takashi Iwai635c2652009-06-13 10:12:59 +0200921 mutex_unlock(&atc->atc_mutex);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200922
Takashi Iwaib7bbf872009-06-05 16:11:07 +0200923 ct_timer_prepare(apcm->timer);
924
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200925 return 0;
926}
927
928static int atc_select_line_in(struct ct_atc *atc)
929{
930 struct hw *hw = atc->hw;
931 struct ct_mixer *mixer = atc->mixer;
Takashi Iwai514eef92009-06-08 14:57:57 +0200932 struct src *src;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200933
934 if (hw->is_adc_source_selected(hw, ADC_LINEIN))
935 return 0;
936
937 mixer->set_input_left(mixer, MIX_MIC_IN, NULL);
938 mixer->set_input_right(mixer, MIX_MIC_IN, NULL);
939
940 hw->select_adc_source(hw, ADC_LINEIN);
941
942 src = atc->srcs[2];
943 mixer->set_input_left(mixer, MIX_LINE_IN, &src->rsc);
944 src = atc->srcs[3];
945 mixer->set_input_right(mixer, MIX_LINE_IN, &src->rsc);
946
947 return 0;
948}
949
950static int atc_select_mic_in(struct ct_atc *atc)
951{
952 struct hw *hw = atc->hw;
953 struct ct_mixer *mixer = atc->mixer;
Takashi Iwai514eef92009-06-08 14:57:57 +0200954 struct src *src;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200955
956 if (hw->is_adc_source_selected(hw, ADC_MICIN))
957 return 0;
958
959 mixer->set_input_left(mixer, MIX_LINE_IN, NULL);
960 mixer->set_input_right(mixer, MIX_LINE_IN, NULL);
961
962 hw->select_adc_source(hw, ADC_MICIN);
963
964 src = atc->srcs[2];
965 mixer->set_input_left(mixer, MIX_MIC_IN, &src->rsc);
966 src = atc->srcs[3];
967 mixer->set_input_right(mixer, MIX_MIC_IN, &src->rsc);
968
969 return 0;
970}
971
972static int atc_have_digit_io_switch(struct ct_atc *atc)
973{
974 struct hw *hw = atc->hw;
975
976 return hw->have_digit_io_switch(hw);
977}
978
979static int atc_select_digit_io(struct ct_atc *atc)
980{
981 struct hw *hw = atc->hw;
982
983 if (hw->is_adc_source_selected(hw, ADC_NONE))
984 return 0;
985
986 hw->select_adc_source(hw, ADC_NONE);
987
988 return 0;
989}
990
991static int atc_daio_unmute(struct ct_atc *atc, unsigned char state, int type)
992{
993 struct daio_mgr *daio_mgr = atc->rsc_mgrs[DAIO];
994
995 if (state)
996 daio_mgr->daio_enable(daio_mgr, atc->daios[type]);
997 else
998 daio_mgr->daio_disable(daio_mgr, atc->daios[type]);
999
1000 daio_mgr->commit_write(daio_mgr);
1001
1002 return 0;
1003}
1004
1005static int
1006atc_dao_get_status(struct ct_atc *atc, unsigned int *status, int type)
1007{
1008 struct dao *dao = container_of(atc->daios[type], struct dao, daio);
1009 return dao->ops->get_spos(dao, status);
1010}
1011
1012static int
1013atc_dao_set_status(struct ct_atc *atc, unsigned int status, int type)
1014{
1015 struct dao *dao = container_of(atc->daios[type], struct dao, daio);
1016
1017 dao->ops->set_spos(dao, status);
1018 dao->ops->commit_write(dao);
1019 return 0;
1020}
1021
1022static int atc_line_front_unmute(struct ct_atc *atc, unsigned char state)
1023{
1024 return atc_daio_unmute(atc, state, LINEO1);
1025}
1026
1027static int atc_line_surround_unmute(struct ct_atc *atc, unsigned char state)
1028{
Sven Eckelmann3b046912009-10-01 20:06:39 +02001029 return atc_daio_unmute(atc, state, LINEO2);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001030}
1031
1032static int atc_line_clfe_unmute(struct ct_atc *atc, unsigned char state)
1033{
1034 return atc_daio_unmute(atc, state, LINEO3);
1035}
1036
1037static int atc_line_rear_unmute(struct ct_atc *atc, unsigned char state)
1038{
Sven Eckelmann3b046912009-10-01 20:06:39 +02001039 return atc_daio_unmute(atc, state, LINEO4);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001040}
1041
1042static int atc_line_in_unmute(struct ct_atc *atc, unsigned char state)
1043{
1044 return atc_daio_unmute(atc, state, LINEIM);
1045}
1046
1047static int atc_spdif_out_unmute(struct ct_atc *atc, unsigned char state)
1048{
1049 return atc_daio_unmute(atc, state, SPDIFOO);
1050}
1051
1052static int atc_spdif_in_unmute(struct ct_atc *atc, unsigned char state)
1053{
1054 return atc_daio_unmute(atc, state, SPDIFIO);
1055}
1056
1057static int atc_spdif_out_get_status(struct ct_atc *atc, unsigned int *status)
1058{
1059 return atc_dao_get_status(atc, status, SPDIFOO);
1060}
1061
1062static int atc_spdif_out_set_status(struct ct_atc *atc, unsigned int status)
1063{
1064 return atc_dao_set_status(atc, status, SPDIFOO);
1065}
1066
1067static int atc_spdif_out_passthru(struct ct_atc *atc, unsigned char state)
1068{
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001069 struct dao_desc da_dsc = {0};
Takashi Iwai514eef92009-06-08 14:57:57 +02001070 struct dao *dao;
1071 int err;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001072 struct ct_mixer *mixer = atc->mixer;
1073 struct rsc *rscs[2] = {NULL};
1074 unsigned int spos = 0;
1075
Takashi Iwai635c2652009-06-13 10:12:59 +02001076 mutex_lock(&atc->atc_mutex);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001077 dao = container_of(atc->daios[SPDIFOO], struct dao, daio);
1078 da_dsc.msr = state ? 1 : atc->msr;
1079 da_dsc.passthru = state ? 1 : 0;
1080 err = dao->ops->reinit(dao, &da_dsc);
1081 if (state) {
1082 spos = IEC958_DEFAULT_CON;
1083 } else {
1084 mixer->get_output_ports(mixer, MIX_SPDIF_OUT,
1085 &rscs[0], &rscs[1]);
1086 dao->ops->set_left_input(dao, rscs[0]);
1087 dao->ops->set_right_input(dao, rscs[1]);
1088 /* Restore PLL to atc->rsr if needed. */
Takashi Iwai514eef92009-06-08 14:57:57 +02001089 if (atc->pll_rate != atc->rsr)
1090 err = atc_pll_init(atc, atc->rsr);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001091 }
1092 dao->ops->set_spos(dao, spos);
1093 dao->ops->commit_write(dao);
Takashi Iwai635c2652009-06-13 10:12:59 +02001094 mutex_unlock(&atc->atc_mutex);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001095
1096 return err;
1097}
1098
Wai Yew CHAY29959a02009-06-22 14:52:34 +02001099static int atc_release_resources(struct ct_atc *atc)
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001100{
Wai Yew CHAY29959a02009-06-22 14:52:34 +02001101 int i;
1102 struct daio_mgr *daio_mgr = NULL;
1103 struct dao *dao = NULL;
1104 struct dai *dai = NULL;
1105 struct daio *daio = NULL;
1106 struct sum_mgr *sum_mgr = NULL;
1107 struct src_mgr *src_mgr = NULL;
1108 struct srcimp_mgr *srcimp_mgr = NULL;
1109 struct srcimp *srcimp = NULL;
1110 struct ct_mixer *mixer = NULL;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001111
Wai Yew CHAY29959a02009-06-22 14:52:34 +02001112 /* disconnect internal mixer objects */
Takashi Iwai35ebf6e2009-07-22 17:12:34 +02001113 if (atc->mixer) {
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001114 mixer = atc->mixer;
1115 mixer->set_input_left(mixer, MIX_LINE_IN, NULL);
1116 mixer->set_input_right(mixer, MIX_LINE_IN, NULL);
1117 mixer->set_input_left(mixer, MIX_MIC_IN, NULL);
1118 mixer->set_input_right(mixer, MIX_MIC_IN, NULL);
1119 mixer->set_input_left(mixer, MIX_SPDIF_IN, NULL);
1120 mixer->set_input_right(mixer, MIX_SPDIF_IN, NULL);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001121 }
1122
Takashi Iwai35ebf6e2009-07-22 17:12:34 +02001123 if (atc->daios) {
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001124 daio_mgr = (struct daio_mgr *)atc->rsc_mgrs[DAIO];
1125 for (i = 0; i < atc->n_daio; i++) {
1126 daio = atc->daios[i];
1127 if (daio->type < LINEIM) {
1128 dao = container_of(daio, struct dao, daio);
1129 dao->ops->clear_left_input(dao);
1130 dao->ops->clear_right_input(dao);
1131 } else {
1132 dai = container_of(daio, struct dai, daio);
1133 /* some thing to do for dai ... */
1134 }
1135 daio_mgr->put_daio(daio_mgr, daio);
1136 }
1137 kfree(atc->daios);
Wai Yew CHAY29959a02009-06-22 14:52:34 +02001138 atc->daios = NULL;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001139 }
1140
Takashi Iwai35ebf6e2009-07-22 17:12:34 +02001141 if (atc->pcm) {
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001142 sum_mgr = atc->rsc_mgrs[SUM];
1143 for (i = 0; i < atc->n_pcm; i++)
1144 sum_mgr->put_sum(sum_mgr, atc->pcm[i]);
1145
1146 kfree(atc->pcm);
Wai Yew CHAY29959a02009-06-22 14:52:34 +02001147 atc->pcm = NULL;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001148 }
1149
Takashi Iwai35ebf6e2009-07-22 17:12:34 +02001150 if (atc->srcs) {
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001151 src_mgr = atc->rsc_mgrs[SRC];
1152 for (i = 0; i < atc->n_src; i++)
1153 src_mgr->put_src(src_mgr, atc->srcs[i]);
1154
1155 kfree(atc->srcs);
Wai Yew CHAY29959a02009-06-22 14:52:34 +02001156 atc->srcs = NULL;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001157 }
1158
Takashi Iwai35ebf6e2009-07-22 17:12:34 +02001159 if (atc->srcimps) {
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001160 srcimp_mgr = atc->rsc_mgrs[SRCIMP];
1161 for (i = 0; i < atc->n_srcimp; i++) {
1162 srcimp = atc->srcimps[i];
1163 srcimp->ops->unmap(srcimp);
1164 srcimp_mgr->put_srcimp(srcimp_mgr, atc->srcimps[i]);
1165 }
1166 kfree(atc->srcimps);
Wai Yew CHAY29959a02009-06-22 14:52:34 +02001167 atc->srcimps = NULL;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001168 }
1169
Wai Yew CHAY29959a02009-06-22 14:52:34 +02001170 return 0;
1171}
1172
1173static int ct_atc_destroy(struct ct_atc *atc)
1174{
1175 int i = 0;
1176
Takashi Iwai35ebf6e2009-07-22 17:12:34 +02001177 if (!atc)
Wai Yew CHAY29959a02009-06-22 14:52:34 +02001178 return 0;
1179
1180 if (atc->timer) {
1181 ct_timer_free(atc->timer);
1182 atc->timer = NULL;
1183 }
1184
1185 atc_release_resources(atc);
1186
1187 /* Destroy internal mixer objects */
Takashi Iwai35ebf6e2009-07-22 17:12:34 +02001188 if (atc->mixer)
Wai Yew CHAY29959a02009-06-22 14:52:34 +02001189 ct_mixer_destroy(atc->mixer);
1190
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001191 for (i = 0; i < NUM_RSCTYP; i++) {
Takashi Iwai35ebf6e2009-07-22 17:12:34 +02001192 if (rsc_mgr_funcs[i].destroy && atc->rsc_mgrs[i])
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001193 rsc_mgr_funcs[i].destroy(atc->rsc_mgrs[i]);
1194
1195 }
1196
Takashi Iwai35ebf6e2009-07-22 17:12:34 +02001197 if (atc->hw)
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001198 destroy_hw_obj((struct hw *)atc->hw);
1199
1200 /* Destroy device virtual memory manager object */
Takashi Iwai35ebf6e2009-07-22 17:12:34 +02001201 if (atc->vm) {
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001202 ct_vm_destroy(atc->vm);
1203 atc->vm = NULL;
1204 }
1205
1206 kfree(atc);
1207
1208 return 0;
1209}
1210
1211static int atc_dev_free(struct snd_device *dev)
1212{
1213 struct ct_atc *atc = dev->device_data;
1214 return ct_atc_destroy(atc);
1215}
1216
Takashi Iwai408bffd2010-01-14 09:19:46 +01001217static int __devinit atc_identify_card(struct ct_atc *atc, unsigned int ssid)
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001218{
Takashi Iwai94701952009-06-08 18:10:32 +02001219 const struct snd_pci_quirk *p;
1220 const struct snd_pci_quirk *list;
Takashi Iwai408bffd2010-01-14 09:19:46 +01001221 u16 vendor_id, device_id;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001222
Takashi Iwai94701952009-06-08 18:10:32 +02001223 switch (atc->chip_type) {
1224 case ATC20K1:
1225 atc->chip_name = "20K1";
1226 list = subsys_20k1_list;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001227 break;
Takashi Iwai94701952009-06-08 18:10:32 +02001228 case ATC20K2:
1229 atc->chip_name = "20K2";
1230 list = subsys_20k2_list;
1231 break;
1232 default:
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001233 return -ENOENT;
Takashi Iwai94701952009-06-08 18:10:32 +02001234 }
Takashi Iwai408bffd2010-01-14 09:19:46 +01001235 if (ssid) {
1236 vendor_id = ssid >> 16;
1237 device_id = ssid & 0xffff;
1238 } else {
1239 vendor_id = atc->pci->subsystem_vendor;
1240 device_id = atc->pci->subsystem_device;
1241 }
1242 p = snd_pci_quirk_lookup_id(vendor_id, device_id, list);
Takashi Iwaia8f43102009-06-22 07:36:52 +02001243 if (p) {
1244 if (p->value < 0) {
1245 printk(KERN_ERR "ctxfi: "
1246 "Device %04x:%04x is black-listed\n",
Takashi Iwai408bffd2010-01-14 09:19:46 +01001247 vendor_id, device_id);
Takashi Iwaia8f43102009-06-22 07:36:52 +02001248 return -ENOENT;
1249 }
1250 atc->model = p->value;
1251 } else {
1252 if (atc->chip_type == ATC20K1)
1253 atc->model = CT20K1_UNKNOWN;
1254 else
1255 atc->model = CT20K2_UNKNOWN;
1256 }
Takashi Iwai94701952009-06-08 18:10:32 +02001257 atc->model_name = ct_subsys_name[atc->model];
1258 snd_printd("ctxfi: chip %s model %s (%04x:%04x) is found\n",
1259 atc->chip_name, atc->model_name,
Takashi Iwai408bffd2010-01-14 09:19:46 +01001260 vendor_id, device_id);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001261 return 0;
1262}
1263
Takashi Iwai2a36f672009-06-05 16:34:10 +02001264int __devinit ct_atc_create_alsa_devs(struct ct_atc *atc)
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001265{
1266 enum CTALSADEVS i;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001267 int err;
1268
Takashi Iwai94701952009-06-08 18:10:32 +02001269 alsa_dev_funcs[MIXER].public_name = atc->chip_name;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001270
1271 for (i = 0; i < NUM_CTALSADEVS; i++) {
Takashi Iwai35ebf6e2009-07-22 17:12:34 +02001272 if (!alsa_dev_funcs[i].create)
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001273 continue;
1274
1275 err = alsa_dev_funcs[i].create(atc, i,
1276 alsa_dev_funcs[i].public_name);
1277 if (err) {
Takashi Iwaib3e0afe2009-05-14 15:19:30 +02001278 printk(KERN_ERR "ctxfi: "
1279 "Creating alsa device %d failed!\n", i);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001280 return err;
1281 }
1282 }
1283
1284 return 0;
1285}
1286
Takashi Iwai2a36f672009-06-05 16:34:10 +02001287static int __devinit atc_create_hw_devs(struct ct_atc *atc)
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001288{
Takashi Iwai514eef92009-06-08 14:57:57 +02001289 struct hw *hw;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001290 struct card_conf info = {0};
Takashi Iwai514eef92009-06-08 14:57:57 +02001291 int i, err;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001292
Takashi Iwai94701952009-06-08 18:10:32 +02001293 err = create_hw_obj(atc->pci, atc->chip_type, atc->model, &hw);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001294 if (err) {
1295 printk(KERN_ERR "Failed to create hw obj!!!\n");
1296 return err;
1297 }
1298 atc->hw = hw;
1299
1300 /* Initialize card hardware. */
1301 info.rsr = atc->rsr;
1302 info.msr = atc->msr;
1303 info.vm_pgt_phys = atc_get_ptp_phys(atc, 0);
1304 err = hw->card_init(hw, &info);
1305 if (err < 0)
1306 return err;
1307
1308 for (i = 0; i < NUM_RSCTYP; i++) {
Takashi Iwai35ebf6e2009-07-22 17:12:34 +02001309 if (!rsc_mgr_funcs[i].create)
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001310 continue;
1311
1312 err = rsc_mgr_funcs[i].create(atc->hw, &atc->rsc_mgrs[i]);
1313 if (err) {
Takashi Iwaib3e0afe2009-05-14 15:19:30 +02001314 printk(KERN_ERR "ctxfi: "
1315 "Failed to create rsc_mgr %d!!!\n", i);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001316 return err;
1317 }
1318 }
1319
1320 return 0;
1321}
1322
Wai Yew CHAY29959a02009-06-22 14:52:34 +02001323static int atc_get_resources(struct ct_atc *atc)
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001324{
1325 struct daio_desc da_desc = {0};
Takashi Iwai514eef92009-06-08 14:57:57 +02001326 struct daio_mgr *daio_mgr;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001327 struct src_desc src_dsc = {0};
Takashi Iwai514eef92009-06-08 14:57:57 +02001328 struct src_mgr *src_mgr;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001329 struct srcimp_desc srcimp_dsc = {0};
Takashi Iwai514eef92009-06-08 14:57:57 +02001330 struct srcimp_mgr *srcimp_mgr;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001331 struct sum_desc sum_dsc = {0};
Takashi Iwai514eef92009-06-08 14:57:57 +02001332 struct sum_mgr *sum_mgr;
1333 int err, i;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001334
1335 atc->daios = kzalloc(sizeof(void *)*(DAIONUM), GFP_KERNEL);
Takashi Iwai35ebf6e2009-07-22 17:12:34 +02001336 if (!atc->daios)
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001337 return -ENOMEM;
1338
1339 atc->srcs = kzalloc(sizeof(void *)*(2*2), GFP_KERNEL);
Takashi Iwai35ebf6e2009-07-22 17:12:34 +02001340 if (!atc->srcs)
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001341 return -ENOMEM;
1342
1343 atc->srcimps = kzalloc(sizeof(void *)*(2*2), GFP_KERNEL);
Takashi Iwai35ebf6e2009-07-22 17:12:34 +02001344 if (!atc->srcimps)
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001345 return -ENOMEM;
1346
1347 atc->pcm = kzalloc(sizeof(void *)*(2*4), GFP_KERNEL);
Takashi Iwai35ebf6e2009-07-22 17:12:34 +02001348 if (!atc->pcm)
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001349 return -ENOMEM;
1350
1351 daio_mgr = (struct daio_mgr *)atc->rsc_mgrs[DAIO];
1352 da_desc.msr = atc->msr;
1353 for (i = 0, atc->n_daio = 0; i < DAIONUM-1; i++) {
1354 da_desc.type = i;
1355 err = daio_mgr->get_daio(daio_mgr, &da_desc,
1356 (struct daio **)&atc->daios[i]);
1357 if (err) {
Takashi Iwaib3e0afe2009-05-14 15:19:30 +02001358 printk(KERN_ERR "ctxfi: Failed to get DAIO "
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001359 "resource %d!!!\n", i);
1360 return err;
1361 }
1362 atc->n_daio++;
1363 }
Takashi Iwai94701952009-06-08 18:10:32 +02001364 if (atc->model == CTSB073X)
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001365 da_desc.type = SPDIFI1;
Takashi Iwai94701952009-06-08 18:10:32 +02001366 else
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001367 da_desc.type = SPDIFIO;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001368 err = daio_mgr->get_daio(daio_mgr, &da_desc,
1369 (struct daio **)&atc->daios[i]);
1370 if (err) {
Takashi Iwaib3e0afe2009-05-14 15:19:30 +02001371 printk(KERN_ERR "ctxfi: Failed to get S/PDIF-in resource!!!\n");
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001372 return err;
1373 }
1374 atc->n_daio++;
1375
1376 src_mgr = atc->rsc_mgrs[SRC];
1377 src_dsc.multi = 1;
1378 src_dsc.msr = atc->msr;
1379 src_dsc.mode = ARCRW;
1380 for (i = 0, atc->n_src = 0; i < (2*2); i++) {
1381 err = src_mgr->get_src(src_mgr, &src_dsc,
1382 (struct src **)&atc->srcs[i]);
1383 if (err)
1384 return err;
1385
1386 atc->n_src++;
1387 }
1388
1389 srcimp_mgr = atc->rsc_mgrs[SRCIMP];
1390 srcimp_dsc.msr = 8; /* SRCIMPs for S/PDIFIn SRT */
1391 for (i = 0, atc->n_srcimp = 0; i < (2*1); i++) {
1392 err = srcimp_mgr->get_srcimp(srcimp_mgr, &srcimp_dsc,
1393 (struct srcimp **)&atc->srcimps[i]);
1394 if (err)
1395 return err;
1396
1397 atc->n_srcimp++;
1398 }
1399 srcimp_dsc.msr = 8; /* SRCIMPs for LINE/MICIn SRT */
1400 for (i = 0; i < (2*1); i++) {
1401 err = srcimp_mgr->get_srcimp(srcimp_mgr, &srcimp_dsc,
1402 (struct srcimp **)&atc->srcimps[2*1+i]);
1403 if (err)
1404 return err;
1405
1406 atc->n_srcimp++;
1407 }
1408
1409 sum_mgr = atc->rsc_mgrs[SUM];
1410 sum_dsc.msr = atc->msr;
1411 for (i = 0, atc->n_pcm = 0; i < (2*4); i++) {
1412 err = sum_mgr->get_sum(sum_mgr, &sum_dsc,
1413 (struct sum **)&atc->pcm[i]);
1414 if (err)
1415 return err;
1416
1417 atc->n_pcm++;
1418 }
1419
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001420 return 0;
1421}
1422
Wai Yew CHAY29959a02009-06-22 14:52:34 +02001423static void
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001424atc_connect_dai(struct src_mgr *src_mgr, struct dai *dai,
1425 struct src **srcs, struct srcimp **srcimps)
1426{
1427 struct rsc *rscs[2] = {NULL};
Takashi Iwai514eef92009-06-08 14:57:57 +02001428 struct src *src;
1429 struct srcimp *srcimp;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001430 int i = 0;
1431
1432 rscs[0] = &dai->daio.rscl;
1433 rscs[1] = &dai->daio.rscr;
1434 for (i = 0; i < 2; i++) {
1435 src = srcs[i];
1436 srcimp = srcimps[i];
1437 srcimp->ops->map(srcimp, src, rscs[i]);
1438 src_mgr->src_disable(src_mgr, src);
1439 }
1440
1441 src_mgr->commit_write(src_mgr); /* Actually disable SRCs */
1442
1443 src = srcs[0];
1444 src->ops->set_pm(src, 1);
1445 for (i = 0; i < 2; i++) {
1446 src = srcs[i];
1447 src->ops->set_state(src, SRC_STATE_RUN);
1448 src->ops->commit_write(src);
1449 src_mgr->src_enable_s(src_mgr, src);
1450 }
1451
1452 dai->ops->set_srt_srcl(dai, &(srcs[0]->rsc));
1453 dai->ops->set_srt_srcr(dai, &(srcs[1]->rsc));
1454
1455 dai->ops->set_enb_src(dai, 1);
1456 dai->ops->set_enb_srt(dai, 1);
1457 dai->ops->commit_write(dai);
1458
1459 src_mgr->commit_write(src_mgr); /* Synchronously enable SRCs */
1460}
1461
Wai Yew CHAY29959a02009-06-22 14:52:34 +02001462static void atc_connect_resources(struct ct_atc *atc)
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001463{
Takashi Iwai514eef92009-06-08 14:57:57 +02001464 struct dai *dai;
1465 struct dao *dao;
1466 struct src *src;
1467 struct sum *sum;
1468 struct ct_mixer *mixer;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001469 struct rsc *rscs[2] = {NULL};
Takashi Iwai514eef92009-06-08 14:57:57 +02001470 int i, j;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001471
1472 mixer = atc->mixer;
1473
1474 for (i = MIX_WAVE_FRONT, j = LINEO1; i <= MIX_SPDIF_OUT; i++, j++) {
1475 mixer->get_output_ports(mixer, i, &rscs[0], &rscs[1]);
1476 dao = container_of(atc->daios[j], struct dao, daio);
1477 dao->ops->set_left_input(dao, rscs[0]);
1478 dao->ops->set_right_input(dao, rscs[1]);
1479 }
1480
1481 dai = container_of(atc->daios[LINEIM], struct dai, daio);
1482 atc_connect_dai(atc->rsc_mgrs[SRC], dai,
1483 (struct src **)&atc->srcs[2],
1484 (struct srcimp **)&atc->srcimps[2]);
1485 src = atc->srcs[2];
1486 mixer->set_input_left(mixer, MIX_LINE_IN, &src->rsc);
1487 src = atc->srcs[3];
1488 mixer->set_input_right(mixer, MIX_LINE_IN, &src->rsc);
1489
1490 dai = container_of(atc->daios[SPDIFIO], struct dai, daio);
1491 atc_connect_dai(atc->rsc_mgrs[SRC], dai,
1492 (struct src **)&atc->srcs[0],
1493 (struct srcimp **)&atc->srcimps[0]);
1494
1495 src = atc->srcs[0];
1496 mixer->set_input_left(mixer, MIX_SPDIF_IN, &src->rsc);
1497 src = atc->srcs[1];
1498 mixer->set_input_right(mixer, MIX_SPDIF_IN, &src->rsc);
1499
1500 for (i = MIX_PCMI_FRONT, j = 0; i <= MIX_PCMI_SURROUND; i++, j += 2) {
1501 sum = atc->pcm[j];
1502 mixer->set_input_left(mixer, i, &sum->rsc);
1503 sum = atc->pcm[j+1];
1504 mixer->set_input_right(mixer, i, &sum->rsc);
1505 }
1506}
1507
Wai Yew CHAY29959a02009-06-22 14:52:34 +02001508#ifdef CONFIG_PM
1509static int atc_suspend(struct ct_atc *atc, pm_message_t state)
1510{
1511 int i;
1512 struct hw *hw = atc->hw;
1513
1514 snd_power_change_state(atc->card, SNDRV_CTL_POWER_D3hot);
1515
1516 for (i = FRONT; i < NUM_PCMS; i++) {
1517 if (!atc->pcms[i])
1518 continue;
1519
1520 snd_pcm_suspend_all(atc->pcms[i]);
1521 }
1522
1523 atc_release_resources(atc);
1524
1525 hw->suspend(hw, state);
1526
1527 return 0;
1528}
1529
1530static int atc_hw_resume(struct ct_atc *atc)
1531{
1532 struct hw *hw = atc->hw;
1533 struct card_conf info = {0};
1534
1535 /* Re-initialize card hardware. */
1536 info.rsr = atc->rsr;
1537 info.msr = atc->msr;
1538 info.vm_pgt_phys = atc_get_ptp_phys(atc, 0);
1539 return hw->resume(hw, &info);
1540}
1541
1542static int atc_resources_resume(struct ct_atc *atc)
1543{
1544 struct ct_mixer *mixer;
1545 int err = 0;
1546
1547 /* Get resources */
1548 err = atc_get_resources(atc);
1549 if (err < 0) {
1550 atc_release_resources(atc);
1551 return err;
1552 }
1553
1554 /* Build topology */
1555 atc_connect_resources(atc);
1556
1557 mixer = atc->mixer;
1558 mixer->resume(mixer);
1559
1560 return 0;
1561}
1562
1563static int atc_resume(struct ct_atc *atc)
1564{
1565 int err = 0;
1566
1567 /* Do hardware resume. */
1568 err = atc_hw_resume(atc);
1569 if (err < 0) {
1570 printk(KERN_ERR "ctxfi: pci_enable_device failed, "
1571 "disabling device\n");
1572 snd_card_disconnect(atc->card);
1573 return err;
1574 }
1575
1576 err = atc_resources_resume(atc);
1577 if (err < 0)
1578 return err;
1579
1580 snd_power_change_state(atc->card, SNDRV_CTL_POWER_D0);
1581
1582 return 0;
1583}
1584#endif
1585
Takashi Iwai2a36f672009-06-05 16:34:10 +02001586static struct ct_atc atc_preset __devinitdata = {
1587 .map_audio_buffer = ct_map_audio_buffer,
1588 .unmap_audio_buffer = ct_unmap_audio_buffer,
1589 .pcm_playback_prepare = atc_pcm_playback_prepare,
1590 .pcm_release_resources = atc_pcm_release_resources,
1591 .pcm_playback_start = atc_pcm_playback_start,
1592 .pcm_playback_stop = atc_pcm_stop,
1593 .pcm_playback_position = atc_pcm_playback_position,
1594 .pcm_capture_prepare = atc_pcm_capture_prepare,
1595 .pcm_capture_start = atc_pcm_capture_start,
1596 .pcm_capture_stop = atc_pcm_stop,
1597 .pcm_capture_position = atc_pcm_capture_position,
1598 .spdif_passthru_playback_prepare = spdif_passthru_playback_prepare,
1599 .get_ptp_phys = atc_get_ptp_phys,
1600 .select_line_in = atc_select_line_in,
1601 .select_mic_in = atc_select_mic_in,
1602 .select_digit_io = atc_select_digit_io,
1603 .line_front_unmute = atc_line_front_unmute,
1604 .line_surround_unmute = atc_line_surround_unmute,
1605 .line_clfe_unmute = atc_line_clfe_unmute,
1606 .line_rear_unmute = atc_line_rear_unmute,
1607 .line_in_unmute = atc_line_in_unmute,
1608 .spdif_out_unmute = atc_spdif_out_unmute,
1609 .spdif_in_unmute = atc_spdif_in_unmute,
1610 .spdif_out_get_status = atc_spdif_out_get_status,
1611 .spdif_out_set_status = atc_spdif_out_set_status,
1612 .spdif_out_passthru = atc_spdif_out_passthru,
1613 .have_digit_io_switch = atc_have_digit_io_switch,
Wai Yew CHAY29959a02009-06-22 14:52:34 +02001614#ifdef CONFIG_PM
1615 .suspend = atc_suspend,
1616 .resume = atc_resume,
1617#endif
Takashi Iwai2a36f672009-06-05 16:34:10 +02001618};
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001619
1620/**
1621 * ct_atc_create - create and initialize a hardware manager
1622 * @card: corresponding alsa card object
1623 * @pci: corresponding kernel pci device object
1624 * @ratc: return created object address in it
1625 *
1626 * Creates and initializes a hardware manager.
1627 *
1628 * Creates kmallocated ct_atc structure. Initializes hardware.
1629 * Returns 0 if suceeds, or negative error code if fails.
1630 */
1631
Takashi Iwai2a36f672009-06-05 16:34:10 +02001632int __devinit ct_atc_create(struct snd_card *card, struct pci_dev *pci,
Takashi Iwai94701952009-06-08 18:10:32 +02001633 unsigned int rsr, unsigned int msr,
Takashi Iwai408bffd2010-01-14 09:19:46 +01001634 int chip_type, unsigned int ssid,
1635 struct ct_atc **ratc)
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001636{
Takashi Iwai514eef92009-06-08 14:57:57 +02001637 struct ct_atc *atc;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001638 static struct snd_device_ops ops = {
1639 .dev_free = atc_dev_free,
1640 };
Takashi Iwai514eef92009-06-08 14:57:57 +02001641 int err;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001642
1643 *ratc = NULL;
1644
1645 atc = kzalloc(sizeof(*atc), GFP_KERNEL);
Takashi Iwai35ebf6e2009-07-22 17:12:34 +02001646 if (!atc)
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001647 return -ENOMEM;
1648
Takashi Iwai2a36f672009-06-05 16:34:10 +02001649 /* Set operations */
1650 *atc = atc_preset;
1651
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001652 atc->card = card;
1653 atc->pci = pci;
1654 atc->rsr = rsr;
1655 atc->msr = msr;
Takashi Iwai94701952009-06-08 18:10:32 +02001656 atc->chip_type = chip_type;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001657
Takashi Iwai635c2652009-06-13 10:12:59 +02001658 mutex_init(&atc->atc_mutex);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001659
1660 /* Find card model */
Takashi Iwai408bffd2010-01-14 09:19:46 +01001661 err = atc_identify_card(atc, ssid);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001662 if (err < 0) {
1663 printk(KERN_ERR "ctatc: Card not recognised\n");
1664 goto error1;
1665 }
1666
1667 /* Set up device virtual memory management object */
Jaroslav Kysela21956b62010-02-02 19:58:25 +01001668 err = ct_vm_create(&atc->vm, pci);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001669 if (err < 0)
1670 goto error1;
1671
1672 /* Create all atc hw devices */
1673 err = atc_create_hw_devs(atc);
1674 if (err < 0)
1675 goto error1;
1676
Wai Yew CHAY29959a02009-06-22 14:52:34 +02001677 err = ct_mixer_create(atc, (struct ct_mixer **)&atc->mixer);
1678 if (err) {
1679 printk(KERN_ERR "ctxfi: Failed to create mixer obj!!!\n");
1680 goto error1;
1681 }
1682
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001683 /* Get resources */
1684 err = atc_get_resources(atc);
1685 if (err < 0)
1686 goto error1;
1687
1688 /* Build topology */
1689 atc_connect_resources(atc);
1690
Takashi Iwaib7bbf872009-06-05 16:11:07 +02001691 atc->timer = ct_timer_new(atc);
1692 if (!atc->timer)
1693 goto error1;
1694
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001695 err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, atc, &ops);
1696 if (err < 0)
1697 goto error1;
1698
1699 snd_card_set_dev(card, &pci->dev);
1700
1701 *ratc = atc;
1702 return 0;
1703
1704error1:
1705 ct_atc_destroy(atc);
Takashi Iwaib3e0afe2009-05-14 15:19:30 +02001706 printk(KERN_ERR "ctxfi: Something wrong!!!\n");
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001707 return err;
1708}