blob: 1bff80cde0a2f88000eaa1711867b882adcae433 [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 */
33#define DAIONUM 7
34#define MAX_MULTI_CHN 8
35
36#define IEC958_DEFAULT_CON ((IEC958_AES0_NONAUDIO \
37 | IEC958_AES0_CON_NOT_COPYRIGHT) \
38 | ((IEC958_AES1_CON_MIXER \
39 | IEC958_AES1_CON_ORIGINAL) << 8) \
40 | (0x10 << 16) \
41 | ((IEC958_AES3_CON_FS_48000) << 24))
42
Takashi Iwai94701952009-06-08 18:10:32 +020043static struct snd_pci_quirk __devinitdata subsys_20k1_list[] = {
44 SND_PCI_QUIRK(PCI_VENDOR_ID_CREATIVE, 0x0022, "SB055x", CTSB055X),
45 SND_PCI_QUIRK(PCI_VENDOR_ID_CREATIVE, 0x002f, "SB055x", CTSB055X),
46 SND_PCI_QUIRK(PCI_VENDOR_ID_CREATIVE, 0x0029, "SB073x", CTSB073X),
47 SND_PCI_QUIRK(PCI_VENDOR_ID_CREATIVE, 0x0031, "SB073x", CTSB073X),
Takashi Iwai09521d22009-06-08 18:29:38 +020048 SND_PCI_QUIRK_MASK(PCI_VENDOR_ID_CREATIVE, 0xf000, 0x6000,
49 "UAA", CTUAA),
Takashi Iwai94701952009-06-08 18:10:32 +020050 { } /* terminator */
Wai Yew CHAY8cc72362009-05-14 08:05:58 +020051};
52
Takashi Iwai94701952009-06-08 18:10:32 +020053static struct snd_pci_quirk __devinitdata subsys_20k2_list[] = {
54 SND_PCI_QUIRK(PCI_VENDOR_ID_CREATIVE, PCI_SUBDEVICE_ID_CREATIVE_SB0760,
55 "SB0760", CTSB0760),
56 SND_PCI_QUIRK(PCI_VENDOR_ID_CREATIVE, PCI_SUBDEVICE_ID_CREATIVE_SB08801,
57 "SB0880", CTSB0880),
58 SND_PCI_QUIRK(PCI_VENDOR_ID_CREATIVE, PCI_SUBDEVICE_ID_CREATIVE_SB08802,
59 "SB0880", CTSB0880),
60 SND_PCI_QUIRK(PCI_VENDOR_ID_CREATIVE, PCI_SUBDEVICE_ID_CREATIVE_SB08803,
61 "SB0880", CTSB0880),
Takashi Iwai09521d22009-06-08 18:29:38 +020062 SND_PCI_QUIRK_MASK(PCI_VENDOR_ID_CREATIVE, 0xf000,
63 PCI_SUBDEVICE_ID_CREATIVE_HENDRIX, "HENDRIX",
64 CTHENDRIX),
Takashi Iwai94701952009-06-08 18:10:32 +020065 { } /* terminator */
66};
67
68static const char *ct_subsys_name[NUM_CTCARDS] = {
Takashi Iwaia8f43102009-06-22 07:36:52 +020069 /* 20k1 models */
Takashi Iwai94701952009-06-08 18:10:32 +020070 [CTSB055X] = "SB055x",
71 [CTSB073X] = "SB073x",
Takashi Iwai94701952009-06-08 18:10:32 +020072 [CTUAA] = "UAA",
73 [CT20K1_UNKNOWN] = "Unknown",
Takashi Iwaia8f43102009-06-22 07:36:52 +020074 /* 20k2 models */
75 [CTSB0760] = "SB076x",
Takashi Iwai94701952009-06-08 18:10:32 +020076 [CTHENDRIX] = "Hendrix",
77 [CTSB0880] = "SB0880",
Takashi Iwaia8f43102009-06-22 07:36:52 +020078 [CT20K2_UNKNOWN] = "Unknown",
Wai Yew CHAY8cc72362009-05-14 08:05:58 +020079};
80
81static struct {
82 int (*create)(struct ct_atc *atc,
83 enum CTALSADEVS device, const char *device_name);
84 int (*destroy)(void *alsa_dev);
85 const char *public_name;
86} alsa_dev_funcs[NUM_CTALSADEVS] = {
87 [FRONT] = { .create = ct_alsa_pcm_create,
88 .destroy = NULL,
89 .public_name = "Front/WaveIn"},
Wai Yew CHAY8cc72362009-05-14 08:05:58 +020090 [SURROUND] = { .create = ct_alsa_pcm_create,
91 .destroy = NULL,
92 .public_name = "Surround"},
Takashi Iwai8372d4982009-06-02 14:27:56 +020093 [CLFE] = { .create = ct_alsa_pcm_create,
94 .destroy = NULL,
95 .public_name = "Center/LFE"},
96 [SIDE] = { .create = ct_alsa_pcm_create,
97 .destroy = NULL,
98 .public_name = "Side"},
Wai Yew CHAY8cc72362009-05-14 08:05:58 +020099 [IEC958] = { .create = ct_alsa_pcm_create,
100 .destroy = NULL,
101 .public_name = "IEC958 Non-audio"},
102
103 [MIXER] = { .create = ct_alsa_mix_create,
104 .destroy = NULL,
105 .public_name = "Mixer"}
106};
107
108typedef int (*create_t)(void *, void **);
109typedef int (*destroy_t)(void *);
110
111static struct {
112 int (*create)(void *hw, void **rmgr);
113 int (*destroy)(void *mgr);
114} rsc_mgr_funcs[NUM_RSCTYP] = {
115 [SRC] = { .create = (create_t)src_mgr_create,
116 .destroy = (destroy_t)src_mgr_destroy },
117 [SRCIMP] = { .create = (create_t)srcimp_mgr_create,
118 .destroy = (destroy_t)srcimp_mgr_destroy },
119 [AMIXER] = { .create = (create_t)amixer_mgr_create,
120 .destroy = (destroy_t)amixer_mgr_destroy },
121 [SUM] = { .create = (create_t)sum_mgr_create,
122 .destroy = (destroy_t)sum_mgr_destroy },
123 [DAIO] = { .create = (create_t)daio_mgr_create,
124 .destroy = (destroy_t)daio_mgr_destroy }
125};
126
127static int
128atc_pcm_release_resources(struct ct_atc *atc, struct ct_atc_pcm *apcm);
129
130/* *
131 * Only mono and interleaved modes are supported now.
132 * Always allocates a contiguous channel block.
133 * */
134
135static int ct_map_audio_buffer(struct ct_atc *atc, struct ct_atc_pcm *apcm)
136{
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200137 struct snd_pcm_runtime *runtime;
138 struct ct_vm *vm;
139
Takashi Iwai35ebf6e2009-07-22 17:12:34 +0200140 if (!apcm->substream)
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200141 return 0;
142
143 runtime = apcm->substream->runtime;
144 vm = atc->vm;
145
Takashi Iwaic76157d2009-06-02 15:26:19 +0200146 apcm->vm_block = vm->map(vm, apcm->substream, runtime->dma_bytes);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200147
Takashi Iwai35ebf6e2009-07-22 17:12:34 +0200148 if (!apcm->vm_block)
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200149 return -ENOENT;
150
151 return 0;
152}
153
154static void ct_unmap_audio_buffer(struct ct_atc *atc, struct ct_atc_pcm *apcm)
155{
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200156 struct ct_vm *vm;
157
Takashi Iwai35ebf6e2009-07-22 17:12:34 +0200158 if (!apcm->vm_block)
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200159 return;
160
161 vm = atc->vm;
162
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200163 vm->unmap(vm, apcm->vm_block);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200164
165 apcm->vm_block = NULL;
166}
167
168static unsigned long atc_get_ptp_phys(struct ct_atc *atc, int index)
169{
Jaroslav Kysela21956b62010-02-02 19:58:25 +0100170 return atc->vm->get_ptp_phys(atc->vm, index);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200171}
172
173static unsigned int convert_format(snd_pcm_format_t snd_format)
174{
175 switch (snd_format) {
176 case SNDRV_PCM_FORMAT_U8:
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200177 return SRC_SF_U8;
178 case SNDRV_PCM_FORMAT_S16_LE:
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200179 return SRC_SF_S16;
180 case SNDRV_PCM_FORMAT_S24_3LE:
181 return SRC_SF_S24;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200182 case SNDRV_PCM_FORMAT_S32_LE:
183 return SRC_SF_S32;
Takashi Iwaid2b9b962009-06-02 14:39:05 +0200184 case SNDRV_PCM_FORMAT_FLOAT_LE:
185 return SRC_SF_F32;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200186 default:
Takashi Iwaib3e0afe2009-05-14 15:19:30 +0200187 printk(KERN_ERR "ctxfi: not recognized snd format is %d \n",
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200188 snd_format);
189 return SRC_SF_S16;
190 }
191}
192
193static unsigned int
194atc_get_pitch(unsigned int input_rate, unsigned int output_rate)
195{
Takashi Iwai514eef92009-06-08 14:57:57 +0200196 unsigned int pitch;
197 int b;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200198
199 /* get pitch and convert to fixed-point 8.24 format. */
200 pitch = (input_rate / output_rate) << 24;
201 input_rate %= output_rate;
202 input_rate /= 100;
203 output_rate /= 100;
204 for (b = 31; ((b >= 0) && !(input_rate >> b)); )
205 b--;
206
207 if (b >= 0) {
208 input_rate <<= (31 - b);
209 input_rate /= output_rate;
210 b = 24 - (31 - b);
211 if (b >= 0)
212 input_rate <<= b;
213 else
214 input_rate >>= -b;
215
216 pitch |= input_rate;
217 }
218
219 return pitch;
220}
221
222static int select_rom(unsigned int pitch)
223{
Takashi Iwai35ebf6e2009-07-22 17:12:34 +0200224 if (pitch > 0x00428f5c && pitch < 0x01b851ec) {
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200225 /* 0.26 <= pitch <= 1.72 */
226 return 1;
Takashi Iwai35ebf6e2009-07-22 17:12:34 +0200227 } else if (pitch == 0x01d66666 || pitch == 0x01d66667) {
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200228 /* pitch == 1.8375 */
229 return 2;
Takashi Iwai35ebf6e2009-07-22 17:12:34 +0200230 } else if (pitch == 0x02000000) {
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200231 /* pitch == 2 */
232 return 3;
Roel Kluin84ed1a12009-10-23 16:03:08 +0200233 } else if (pitch <= 0x08000000) {
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200234 /* 0 <= pitch <= 8 */
235 return 0;
236 } else {
237 return -ENOENT;
238 }
239}
240
241static int atc_pcm_playback_prepare(struct ct_atc *atc, struct ct_atc_pcm *apcm)
242{
243 struct src_mgr *src_mgr = atc->rsc_mgrs[SRC];
244 struct amixer_mgr *amixer_mgr = atc->rsc_mgrs[AMIXER];
245 struct src_desc desc = {0};
246 struct amixer_desc mix_dsc = {0};
Takashi Iwai514eef92009-06-08 14:57:57 +0200247 struct src *src;
248 struct amixer *amixer;
249 int err;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200250 int n_amixer = apcm->substream->runtime->channels, i = 0;
251 int device = apcm->substream->pcm->device;
Takashi Iwai514eef92009-06-08 14:57:57 +0200252 unsigned int pitch;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200253
Takashi Iwai822fa192009-06-02 14:12:17 +0200254 /* first release old resources */
Wai Yew CHAY29959a02009-06-22 14:52:34 +0200255 atc_pcm_release_resources(atc, apcm);
Takashi Iwai822fa192009-06-02 14:12:17 +0200256
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200257 /* Get SRC resource */
258 desc.multi = apcm->substream->runtime->channels;
259 desc.msr = atc->msr;
260 desc.mode = MEMRD;
261 err = src_mgr->get_src(src_mgr, &desc, (struct src **)&apcm->src);
262 if (err)
263 goto error1;
264
265 pitch = atc_get_pitch(apcm->substream->runtime->rate,
266 (atc->rsr * atc->msr));
267 src = apcm->src;
268 src->ops->set_pitch(src, pitch);
269 src->ops->set_rom(src, select_rom(pitch));
270 src->ops->set_sf(src, convert_format(apcm->substream->runtime->format));
271 src->ops->set_pm(src, (src->ops->next_interleave(src) != NULL));
272
273 /* Get AMIXER resource */
274 n_amixer = (n_amixer < 2) ? 2 : n_amixer;
275 apcm->amixers = kzalloc(sizeof(void *)*n_amixer, GFP_KERNEL);
Takashi Iwai35ebf6e2009-07-22 17:12:34 +0200276 if (!apcm->amixers) {
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200277 err = -ENOMEM;
278 goto error1;
279 }
280 mix_dsc.msr = atc->msr;
281 for (i = 0, apcm->n_amixer = 0; i < n_amixer; i++) {
282 err = amixer_mgr->get_amixer(amixer_mgr, &mix_dsc,
283 (struct amixer **)&apcm->amixers[i]);
284 if (err)
285 goto error1;
286
287 apcm->n_amixer++;
288 }
289
290 /* Set up device virtual mem map */
291 err = ct_map_audio_buffer(atc, apcm);
292 if (err < 0)
293 goto error1;
294
295 /* Connect resources */
296 src = apcm->src;
297 for (i = 0; i < n_amixer; i++) {
298 amixer = apcm->amixers[i];
Takashi Iwai635c2652009-06-13 10:12:59 +0200299 mutex_lock(&atc->atc_mutex);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200300 amixer->ops->setup(amixer, &src->rsc,
301 INIT_VOL, atc->pcm[i+device*2]);
Takashi Iwai635c2652009-06-13 10:12:59 +0200302 mutex_unlock(&atc->atc_mutex);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200303 src = src->ops->next_interleave(src);
Takashi Iwai35ebf6e2009-07-22 17:12:34 +0200304 if (!src)
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200305 src = apcm->src;
306 }
307
Takashi Iwaib7bbf872009-06-05 16:11:07 +0200308 ct_timer_prepare(apcm->timer);
309
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200310 return 0;
311
312error1:
313 atc_pcm_release_resources(atc, apcm);
314 return err;
315}
316
317static int
318atc_pcm_release_resources(struct ct_atc *atc, struct ct_atc_pcm *apcm)
319{
320 struct src_mgr *src_mgr = atc->rsc_mgrs[SRC];
321 struct srcimp_mgr *srcimp_mgr = atc->rsc_mgrs[SRCIMP];
322 struct amixer_mgr *amixer_mgr = atc->rsc_mgrs[AMIXER];
323 struct sum_mgr *sum_mgr = atc->rsc_mgrs[SUM];
Takashi Iwai514eef92009-06-08 14:57:57 +0200324 struct srcimp *srcimp;
325 int i;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200326
Takashi Iwai35ebf6e2009-07-22 17:12:34 +0200327 if (apcm->srcimps) {
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200328 for (i = 0; i < apcm->n_srcimp; i++) {
329 srcimp = apcm->srcimps[i];
330 srcimp->ops->unmap(srcimp);
331 srcimp_mgr->put_srcimp(srcimp_mgr, srcimp);
332 apcm->srcimps[i] = NULL;
333 }
334 kfree(apcm->srcimps);
335 apcm->srcimps = NULL;
336 }
337
Takashi Iwai35ebf6e2009-07-22 17:12:34 +0200338 if (apcm->srccs) {
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200339 for (i = 0; i < apcm->n_srcc; i++) {
340 src_mgr->put_src(src_mgr, apcm->srccs[i]);
341 apcm->srccs[i] = NULL;
342 }
343 kfree(apcm->srccs);
344 apcm->srccs = NULL;
345 }
346
Takashi Iwai35ebf6e2009-07-22 17:12:34 +0200347 if (apcm->amixers) {
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200348 for (i = 0; i < apcm->n_amixer; i++) {
349 amixer_mgr->put_amixer(amixer_mgr, apcm->amixers[i]);
350 apcm->amixers[i] = NULL;
351 }
352 kfree(apcm->amixers);
353 apcm->amixers = NULL;
354 }
355
Takashi Iwai35ebf6e2009-07-22 17:12:34 +0200356 if (apcm->mono) {
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200357 sum_mgr->put_sum(sum_mgr, apcm->mono);
358 apcm->mono = NULL;
359 }
360
Takashi Iwai35ebf6e2009-07-22 17:12:34 +0200361 if (apcm->src) {
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200362 src_mgr->put_src(src_mgr, apcm->src);
363 apcm->src = NULL;
364 }
365
Takashi Iwai35ebf6e2009-07-22 17:12:34 +0200366 if (apcm->vm_block) {
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200367 /* Undo device virtual mem map */
368 ct_unmap_audio_buffer(atc, apcm);
369 apcm->vm_block = NULL;
370 }
371
372 return 0;
373}
374
375static int atc_pcm_playback_start(struct ct_atc *atc, struct ct_atc_pcm *apcm)
376{
Takashi Iwai514eef92009-06-08 14:57:57 +0200377 unsigned int max_cisz;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200378 struct src *src = apcm->src;
379
Takashi Iwaic399f3b2009-06-09 08:16:20 +0200380 if (apcm->started)
381 return 0;
382 apcm->started = 1;
383
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200384 max_cisz = src->multi * src->rsc.msr;
385 max_cisz = 0x80 * (max_cisz < 8 ? max_cisz : 8);
386
387 src->ops->set_sa(src, apcm->vm_block->addr);
388 src->ops->set_la(src, apcm->vm_block->addr + apcm->vm_block->size);
389 src->ops->set_ca(src, apcm->vm_block->addr + max_cisz);
390 src->ops->set_cisz(src, max_cisz);
391
392 src->ops->set_bm(src, 1);
393 src->ops->set_state(src, SRC_STATE_INIT);
394 src->ops->commit_write(src);
395
Takashi Iwaib7bbf872009-06-05 16:11:07 +0200396 ct_timer_start(apcm->timer);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200397 return 0;
398}
399
400static int atc_pcm_stop(struct ct_atc *atc, struct ct_atc_pcm *apcm)
401{
Takashi Iwai514eef92009-06-08 14:57:57 +0200402 struct src *src;
403 int i;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200404
Takashi Iwaib7bbf872009-06-05 16:11:07 +0200405 ct_timer_stop(apcm->timer);
406
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200407 src = apcm->src;
408 src->ops->set_bm(src, 0);
409 src->ops->set_state(src, SRC_STATE_OFF);
410 src->ops->commit_write(src);
411
Takashi Iwai35ebf6e2009-07-22 17:12:34 +0200412 if (apcm->srccs) {
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200413 for (i = 0; i < apcm->n_srcc; i++) {
414 src = apcm->srccs[i];
415 src->ops->set_bm(src, 0);
416 src->ops->set_state(src, SRC_STATE_OFF);
417 src->ops->commit_write(src);
418 }
419 }
420
421 apcm->started = 0;
422
423 return 0;
424}
425
426static int
427atc_pcm_playback_position(struct ct_atc *atc, struct ct_atc_pcm *apcm)
428{
429 struct src *src = apcm->src;
Takashi Iwai514eef92009-06-08 14:57:57 +0200430 u32 size, max_cisz;
431 int position;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200432
Takashi Iwai5242bc72009-06-09 08:17:14 +0200433 if (!src)
434 return 0;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200435 position = src->ops->get_ca(src);
436
437 size = apcm->vm_block->size;
438 max_cisz = src->multi * src->rsc.msr;
439 max_cisz = 128 * (max_cisz < 8 ? max_cisz : 8);
440
441 return (position + size - max_cisz - apcm->vm_block->addr) % size;
442}
443
444struct src_node_conf_t {
445 unsigned int pitch;
446 unsigned int msr:8;
447 unsigned int mix_msr:8;
448 unsigned int imp_msr:8;
449 unsigned int vo:1;
450};
451
452static void setup_src_node_conf(struct ct_atc *atc, struct ct_atc_pcm *apcm,
453 struct src_node_conf_t *conf, int *n_srcc)
454{
Takashi Iwai514eef92009-06-08 14:57:57 +0200455 unsigned int pitch;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200456
457 /* get pitch and convert to fixed-point 8.24 format. */
458 pitch = atc_get_pitch((atc->rsr * atc->msr),
459 apcm->substream->runtime->rate);
460 *n_srcc = 0;
461
462 if (1 == atc->msr) {
463 *n_srcc = apcm->substream->runtime->channels;
464 conf[0].pitch = pitch;
465 conf[0].mix_msr = conf[0].imp_msr = conf[0].msr = 1;
466 conf[0].vo = 1;
467 } else if (2 == atc->msr) {
468 if (0x8000000 < pitch) {
469 /* Need two-stage SRCs, SRCIMPs and
470 * AMIXERs for converting format */
471 conf[0].pitch = (atc->msr << 24);
472 conf[0].msr = conf[0].mix_msr = 1;
473 conf[0].imp_msr = atc->msr;
474 conf[0].vo = 0;
475 conf[1].pitch = atc_get_pitch(atc->rsr,
476 apcm->substream->runtime->rate);
477 conf[1].msr = conf[1].mix_msr = conf[1].imp_msr = 1;
478 conf[1].vo = 1;
479 *n_srcc = apcm->substream->runtime->channels * 2;
480 } else if (0x1000000 < pitch) {
481 /* Need one-stage SRCs, SRCIMPs and
482 * AMIXERs for converting format */
483 conf[0].pitch = pitch;
484 conf[0].msr = conf[0].mix_msr
485 = conf[0].imp_msr = atc->msr;
486 conf[0].vo = 1;
487 *n_srcc = apcm->substream->runtime->channels;
488 }
489 }
490}
491
492static int
493atc_pcm_capture_get_resources(struct ct_atc *atc, struct ct_atc_pcm *apcm)
494{
495 struct src_mgr *src_mgr = atc->rsc_mgrs[SRC];
496 struct srcimp_mgr *srcimp_mgr = atc->rsc_mgrs[SRCIMP];
497 struct amixer_mgr *amixer_mgr = atc->rsc_mgrs[AMIXER];
498 struct sum_mgr *sum_mgr = atc->rsc_mgrs[SUM];
499 struct src_desc src_dsc = {0};
Takashi Iwai514eef92009-06-08 14:57:57 +0200500 struct src *src;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200501 struct srcimp_desc srcimp_dsc = {0};
Takashi Iwai514eef92009-06-08 14:57:57 +0200502 struct srcimp *srcimp;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200503 struct amixer_desc mix_dsc = {0};
504 struct sum_desc sum_dsc = {0};
Takashi Iwai514eef92009-06-08 14:57:57 +0200505 unsigned int pitch;
506 int multi, err, i;
507 int n_srcimp, n_amixer, n_srcc, n_sum;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200508 struct src_node_conf_t src_node_conf[2] = {{0} };
509
Takashi Iwai822fa192009-06-02 14:12:17 +0200510 /* first release old resources */
Takashi Iwaia5990dc2009-06-09 08:19:02 +0200511 atc_pcm_release_resources(atc, apcm);
Takashi Iwai822fa192009-06-02 14:12:17 +0200512
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200513 /* The numbers of converting SRCs and SRCIMPs should be determined
514 * by pitch value. */
515
516 multi = apcm->substream->runtime->channels;
517
518 /* get pitch and convert to fixed-point 8.24 format. */
519 pitch = atc_get_pitch((atc->rsr * atc->msr),
520 apcm->substream->runtime->rate);
521
522 setup_src_node_conf(atc, apcm, src_node_conf, &n_srcc);
523 n_sum = (1 == multi) ? 1 : 0;
Takashi Iwai514eef92009-06-08 14:57:57 +0200524 n_amixer = n_sum * 2 + n_srcc;
525 n_srcimp = n_srcc;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200526 if ((multi > 1) && (0x8000000 >= pitch)) {
527 /* Need extra AMIXERs and SRCIMPs for special treatment
528 * of interleaved recording of conjugate channels */
529 n_amixer += multi * atc->msr;
530 n_srcimp += multi * atc->msr;
531 } else {
532 n_srcimp += multi;
533 }
534
535 if (n_srcc) {
536 apcm->srccs = kzalloc(sizeof(void *)*n_srcc, GFP_KERNEL);
Takashi Iwai35ebf6e2009-07-22 17:12:34 +0200537 if (!apcm->srccs)
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200538 return -ENOMEM;
539 }
540 if (n_amixer) {
541 apcm->amixers = kzalloc(sizeof(void *)*n_amixer, GFP_KERNEL);
Takashi Iwai35ebf6e2009-07-22 17:12:34 +0200542 if (!apcm->amixers) {
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200543 err = -ENOMEM;
544 goto error1;
545 }
546 }
547 apcm->srcimps = kzalloc(sizeof(void *)*n_srcimp, GFP_KERNEL);
Takashi Iwai35ebf6e2009-07-22 17:12:34 +0200548 if (!apcm->srcimps) {
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200549 err = -ENOMEM;
550 goto error1;
551 }
552
553 /* Allocate SRCs for sample rate conversion if needed */
554 src_dsc.multi = 1;
555 src_dsc.mode = ARCRW;
556 for (i = 0, apcm->n_srcc = 0; i < n_srcc; i++) {
557 src_dsc.msr = src_node_conf[i/multi].msr;
558 err = src_mgr->get_src(src_mgr, &src_dsc,
559 (struct src **)&apcm->srccs[i]);
560 if (err)
561 goto error1;
562
563 src = apcm->srccs[i];
564 pitch = src_node_conf[i/multi].pitch;
565 src->ops->set_pitch(src, pitch);
566 src->ops->set_rom(src, select_rom(pitch));
567 src->ops->set_vo(src, src_node_conf[i/multi].vo);
568
569 apcm->n_srcc++;
570 }
571
572 /* Allocate AMIXERs for routing SRCs of conversion if needed */
573 for (i = 0, apcm->n_amixer = 0; i < n_amixer; i++) {
574 if (i < (n_sum*2))
575 mix_dsc.msr = atc->msr;
576 else if (i < (n_sum*2+n_srcc))
577 mix_dsc.msr = src_node_conf[(i-n_sum*2)/multi].mix_msr;
578 else
579 mix_dsc.msr = 1;
580
581 err = amixer_mgr->get_amixer(amixer_mgr, &mix_dsc,
582 (struct amixer **)&apcm->amixers[i]);
583 if (err)
584 goto error1;
585
586 apcm->n_amixer++;
587 }
588
589 /* Allocate a SUM resource to mix all input channels together */
590 sum_dsc.msr = atc->msr;
591 err = sum_mgr->get_sum(sum_mgr, &sum_dsc, (struct sum **)&apcm->mono);
592 if (err)
593 goto error1;
594
595 pitch = atc_get_pitch((atc->rsr * atc->msr),
596 apcm->substream->runtime->rate);
597 /* Allocate SRCIMP resources */
598 for (i = 0, apcm->n_srcimp = 0; i < n_srcimp; i++) {
599 if (i < (n_srcc))
600 srcimp_dsc.msr = src_node_conf[i/multi].imp_msr;
601 else if (1 == multi)
602 srcimp_dsc.msr = (pitch <= 0x8000000) ? atc->msr : 1;
603 else
604 srcimp_dsc.msr = 1;
605
606 err = srcimp_mgr->get_srcimp(srcimp_mgr, &srcimp_dsc, &srcimp);
607 if (err)
608 goto error1;
609
610 apcm->srcimps[i] = srcimp;
611 apcm->n_srcimp++;
612 }
613
614 /* Allocate a SRC for writing data to host memory */
615 src_dsc.multi = apcm->substream->runtime->channels;
616 src_dsc.msr = 1;
617 src_dsc.mode = MEMWR;
618 err = src_mgr->get_src(src_mgr, &src_dsc, (struct src **)&apcm->src);
619 if (err)
620 goto error1;
621
622 src = apcm->src;
623 src->ops->set_pitch(src, pitch);
624
625 /* Set up device virtual mem map */
626 err = ct_map_audio_buffer(atc, apcm);
627 if (err < 0)
628 goto error1;
629
630 return 0;
631
632error1:
633 atc_pcm_release_resources(atc, apcm);
634 return err;
635}
636
637static int atc_pcm_capture_prepare(struct ct_atc *atc, struct ct_atc_pcm *apcm)
638{
Takashi Iwai514eef92009-06-08 14:57:57 +0200639 struct src *src;
640 struct amixer *amixer;
641 struct srcimp *srcimp;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200642 struct ct_mixer *mixer = atc->mixer;
Takashi Iwai514eef92009-06-08 14:57:57 +0200643 struct sum *mono;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200644 struct rsc *out_ports[8] = {NULL};
Takashi Iwai514eef92009-06-08 14:57:57 +0200645 int err, i, j, n_sum, multi;
646 unsigned int pitch;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200647 int mix_base = 0, imp_base = 0;
648
Wai Yew CHAY29959a02009-06-22 14:52:34 +0200649 atc_pcm_release_resources(atc, apcm);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200650
651 /* Get needed resources. */
652 err = atc_pcm_capture_get_resources(atc, apcm);
653 if (err)
654 return err;
655
656 /* Connect resources */
657 mixer->get_output_ports(mixer, MIX_PCMO_FRONT,
658 &out_ports[0], &out_ports[1]);
659
660 multi = apcm->substream->runtime->channels;
661 if (1 == multi) {
662 mono = apcm->mono;
663 for (i = 0; i < 2; i++) {
664 amixer = apcm->amixers[i];
665 amixer->ops->setup(amixer, out_ports[i],
666 MONO_SUM_SCALE, mono);
667 }
668 out_ports[0] = &mono->rsc;
669 n_sum = 1;
670 mix_base = n_sum * 2;
671 }
672
673 for (i = 0; i < apcm->n_srcc; i++) {
674 src = apcm->srccs[i];
675 srcimp = apcm->srcimps[imp_base+i];
676 amixer = apcm->amixers[mix_base+i];
677 srcimp->ops->map(srcimp, src, out_ports[i%multi]);
678 amixer->ops->setup(amixer, &src->rsc, INIT_VOL, NULL);
679 out_ports[i%multi] = &amixer->rsc;
680 }
681
682 pitch = atc_get_pitch((atc->rsr * atc->msr),
683 apcm->substream->runtime->rate);
684
685 if ((multi > 1) && (pitch <= 0x8000000)) {
686 /* Special connection for interleaved
687 * recording with conjugate channels */
688 for (i = 0; i < multi; i++) {
689 out_ports[i]->ops->master(out_ports[i]);
690 for (j = 0; j < atc->msr; j++) {
691 amixer = apcm->amixers[apcm->n_srcc+j*multi+i];
692 amixer->ops->set_input(amixer, out_ports[i]);
693 amixer->ops->set_scale(amixer, INIT_VOL);
694 amixer->ops->set_sum(amixer, NULL);
695 amixer->ops->commit_raw_write(amixer);
696 out_ports[i]->ops->next_conj(out_ports[i]);
697
698 srcimp = apcm->srcimps[apcm->n_srcc+j*multi+i];
699 srcimp->ops->map(srcimp, apcm->src,
700 &amixer->rsc);
701 }
702 }
703 } else {
704 for (i = 0; i < multi; i++) {
705 srcimp = apcm->srcimps[apcm->n_srcc+i];
706 srcimp->ops->map(srcimp, apcm->src, out_ports[i]);
707 }
708 }
709
Takashi Iwaib7bbf872009-06-05 16:11:07 +0200710 ct_timer_prepare(apcm->timer);
711
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200712 return 0;
713}
714
715static int atc_pcm_capture_start(struct ct_atc *atc, struct ct_atc_pcm *apcm)
716{
Takashi Iwai514eef92009-06-08 14:57:57 +0200717 struct src *src;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200718 struct src_mgr *src_mgr = atc->rsc_mgrs[SRC];
Takashi Iwai514eef92009-06-08 14:57:57 +0200719 int i, multi;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200720
721 if (apcm->started)
722 return 0;
723
724 apcm->started = 1;
725 multi = apcm->substream->runtime->channels;
726 /* Set up converting SRCs */
727 for (i = 0; i < apcm->n_srcc; i++) {
728 src = apcm->srccs[i];
729 src->ops->set_pm(src, ((i%multi) != (multi-1)));
730 src_mgr->src_disable(src_mgr, src);
731 }
732
733 /* Set up recording SRC */
734 src = apcm->src;
735 src->ops->set_sf(src, convert_format(apcm->substream->runtime->format));
736 src->ops->set_sa(src, apcm->vm_block->addr);
737 src->ops->set_la(src, apcm->vm_block->addr + apcm->vm_block->size);
738 src->ops->set_ca(src, apcm->vm_block->addr);
739 src_mgr->src_disable(src_mgr, src);
740
741 /* Disable relevant SRCs firstly */
742 src_mgr->commit_write(src_mgr);
743
744 /* Enable SRCs respectively */
745 for (i = 0; i < apcm->n_srcc; i++) {
746 src = apcm->srccs[i];
747 src->ops->set_state(src, SRC_STATE_RUN);
748 src->ops->commit_write(src);
749 src_mgr->src_enable_s(src_mgr, src);
750 }
751 src = apcm->src;
752 src->ops->set_bm(src, 1);
753 src->ops->set_state(src, SRC_STATE_RUN);
754 src->ops->commit_write(src);
755 src_mgr->src_enable_s(src_mgr, src);
756
757 /* Enable relevant SRCs synchronously */
758 src_mgr->commit_write(src_mgr);
759
Takashi Iwaib7bbf872009-06-05 16:11:07 +0200760 ct_timer_start(apcm->timer);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200761 return 0;
762}
763
764static int
765atc_pcm_capture_position(struct ct_atc *atc, struct ct_atc_pcm *apcm)
766{
767 struct src *src = apcm->src;
768
Takashi Iwai5242bc72009-06-09 08:17:14 +0200769 if (!src)
770 return 0;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200771 return src->ops->get_ca(src) - apcm->vm_block->addr;
772}
773
774static int spdif_passthru_playback_get_resources(struct ct_atc *atc,
775 struct ct_atc_pcm *apcm)
776{
777 struct src_mgr *src_mgr = atc->rsc_mgrs[SRC];
778 struct amixer_mgr *amixer_mgr = atc->rsc_mgrs[AMIXER];
779 struct src_desc desc = {0};
780 struct amixer_desc mix_dsc = {0};
Takashi Iwai514eef92009-06-08 14:57:57 +0200781 struct src *src;
782 int err;
783 int n_amixer = apcm->substream->runtime->channels, i;
784 unsigned int pitch, rsr = atc->pll_rate;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200785
Takashi Iwai822fa192009-06-02 14:12:17 +0200786 /* first release old resources */
Takashi Iwaia5990dc2009-06-09 08:19:02 +0200787 atc_pcm_release_resources(atc, apcm);
Takashi Iwai822fa192009-06-02 14:12:17 +0200788
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200789 /* Get SRC resource */
790 desc.multi = apcm->substream->runtime->channels;
791 desc.msr = 1;
792 while (apcm->substream->runtime->rate > (rsr * desc.msr))
793 desc.msr <<= 1;
794
795 desc.mode = MEMRD;
796 err = src_mgr->get_src(src_mgr, &desc, (struct src **)&apcm->src);
797 if (err)
798 goto error1;
799
800 pitch = atc_get_pitch(apcm->substream->runtime->rate, (rsr * desc.msr));
801 src = apcm->src;
802 src->ops->set_pitch(src, pitch);
803 src->ops->set_rom(src, select_rom(pitch));
804 src->ops->set_sf(src, convert_format(apcm->substream->runtime->format));
805 src->ops->set_pm(src, (src->ops->next_interleave(src) != NULL));
806 src->ops->set_bp(src, 1);
807
808 /* Get AMIXER resource */
809 n_amixer = (n_amixer < 2) ? 2 : n_amixer;
810 apcm->amixers = kzalloc(sizeof(void *)*n_amixer, GFP_KERNEL);
Takashi Iwai35ebf6e2009-07-22 17:12:34 +0200811 if (!apcm->amixers) {
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200812 err = -ENOMEM;
813 goto error1;
814 }
815 mix_dsc.msr = desc.msr;
816 for (i = 0, apcm->n_amixer = 0; i < n_amixer; i++) {
817 err = amixer_mgr->get_amixer(amixer_mgr, &mix_dsc,
818 (struct amixer **)&apcm->amixers[i]);
819 if (err)
820 goto error1;
821
822 apcm->n_amixer++;
823 }
824
825 /* Set up device virtual mem map */
826 err = ct_map_audio_buffer(atc, apcm);
827 if (err < 0)
828 goto error1;
829
830 return 0;
831
832error1:
833 atc_pcm_release_resources(atc, apcm);
834 return err;
835}
836
Takashi Iwai514eef92009-06-08 14:57:57 +0200837static int atc_pll_init(struct ct_atc *atc, int rate)
838{
839 struct hw *hw = atc->hw;
840 int err;
841 err = hw->pll_init(hw, rate);
842 atc->pll_rate = err ? 0 : rate;
843 return err;
844}
845
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200846static int
847spdif_passthru_playback_setup(struct ct_atc *atc, struct ct_atc_pcm *apcm)
848{
849 struct dao *dao = container_of(atc->daios[SPDIFOO], struct dao, daio);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200850 unsigned int rate = apcm->substream->runtime->rate;
Takashi Iwai514eef92009-06-08 14:57:57 +0200851 unsigned int status;
Wai Yew CHAY29959a02009-06-22 14:52:34 +0200852 int err = 0;
Takashi Iwai514eef92009-06-08 14:57:57 +0200853 unsigned char iec958_con_fs;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200854
855 switch (rate) {
856 case 48000:
857 iec958_con_fs = IEC958_AES3_CON_FS_48000;
858 break;
859 case 44100:
860 iec958_con_fs = IEC958_AES3_CON_FS_44100;
861 break;
862 case 32000:
863 iec958_con_fs = IEC958_AES3_CON_FS_32000;
864 break;
865 default:
866 return -ENOENT;
867 }
868
Takashi Iwai635c2652009-06-13 10:12:59 +0200869 mutex_lock(&atc->atc_mutex);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200870 dao->ops->get_spos(dao, &status);
871 if (((status >> 24) & IEC958_AES3_CON_FS) != iec958_con_fs) {
872 status &= ((~IEC958_AES3_CON_FS) << 24);
873 status |= (iec958_con_fs << 24);
874 dao->ops->set_spos(dao, status);
875 dao->ops->commit_write(dao);
876 }
Takashi Iwai514eef92009-06-08 14:57:57 +0200877 if ((rate != atc->pll_rate) && (32000 != rate))
878 err = atc_pll_init(atc, rate);
Takashi Iwai635c2652009-06-13 10:12:59 +0200879 mutex_unlock(&atc->atc_mutex);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200880
881 return err;
882}
883
884static int
885spdif_passthru_playback_prepare(struct ct_atc *atc, struct ct_atc_pcm *apcm)
886{
Takashi Iwai514eef92009-06-08 14:57:57 +0200887 struct src *src;
888 struct amixer *amixer;
889 struct dao *dao;
890 int err;
891 int i;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200892
Wai Yew CHAY29959a02009-06-22 14:52:34 +0200893 atc_pcm_release_resources(atc, apcm);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200894
895 /* Configure SPDIFOO and PLL to passthrough mode;
896 * determine pll_rate. */
897 err = spdif_passthru_playback_setup(atc, apcm);
898 if (err)
899 return err;
900
901 /* Get needed resources. */
902 err = spdif_passthru_playback_get_resources(atc, apcm);
903 if (err)
904 return err;
905
906 /* Connect resources */
907 src = apcm->src;
908 for (i = 0; i < apcm->n_amixer; i++) {
909 amixer = apcm->amixers[i];
910 amixer->ops->setup(amixer, &src->rsc, INIT_VOL, NULL);
911 src = src->ops->next_interleave(src);
Takashi Iwai35ebf6e2009-07-22 17:12:34 +0200912 if (!src)
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200913 src = apcm->src;
914 }
915 /* Connect to SPDIFOO */
Takashi Iwai635c2652009-06-13 10:12:59 +0200916 mutex_lock(&atc->atc_mutex);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200917 dao = container_of(atc->daios[SPDIFOO], struct dao, daio);
918 amixer = apcm->amixers[0];
919 dao->ops->set_left_input(dao, &amixer->rsc);
920 amixer = apcm->amixers[1];
921 dao->ops->set_right_input(dao, &amixer->rsc);
Takashi Iwai635c2652009-06-13 10:12:59 +0200922 mutex_unlock(&atc->atc_mutex);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200923
Takashi Iwaib7bbf872009-06-05 16:11:07 +0200924 ct_timer_prepare(apcm->timer);
925
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200926 return 0;
927}
928
929static int atc_select_line_in(struct ct_atc *atc)
930{
931 struct hw *hw = atc->hw;
932 struct ct_mixer *mixer = atc->mixer;
Takashi Iwai514eef92009-06-08 14:57:57 +0200933 struct src *src;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200934
935 if (hw->is_adc_source_selected(hw, ADC_LINEIN))
936 return 0;
937
938 mixer->set_input_left(mixer, MIX_MIC_IN, NULL);
939 mixer->set_input_right(mixer, MIX_MIC_IN, NULL);
940
941 hw->select_adc_source(hw, ADC_LINEIN);
942
943 src = atc->srcs[2];
944 mixer->set_input_left(mixer, MIX_LINE_IN, &src->rsc);
945 src = atc->srcs[3];
946 mixer->set_input_right(mixer, MIX_LINE_IN, &src->rsc);
947
948 return 0;
949}
950
951static int atc_select_mic_in(struct ct_atc *atc)
952{
953 struct hw *hw = atc->hw;
954 struct ct_mixer *mixer = atc->mixer;
Takashi Iwai514eef92009-06-08 14:57:57 +0200955 struct src *src;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200956
957 if (hw->is_adc_source_selected(hw, ADC_MICIN))
958 return 0;
959
960 mixer->set_input_left(mixer, MIX_LINE_IN, NULL);
961 mixer->set_input_right(mixer, MIX_LINE_IN, NULL);
962
963 hw->select_adc_source(hw, ADC_MICIN);
964
965 src = atc->srcs[2];
966 mixer->set_input_left(mixer, MIX_MIC_IN, &src->rsc);
967 src = atc->srcs[3];
968 mixer->set_input_right(mixer, MIX_MIC_IN, &src->rsc);
969
970 return 0;
971}
972
973static int atc_have_digit_io_switch(struct ct_atc *atc)
974{
975 struct hw *hw = atc->hw;
976
977 return hw->have_digit_io_switch(hw);
978}
979
980static int atc_select_digit_io(struct ct_atc *atc)
981{
982 struct hw *hw = atc->hw;
983
984 if (hw->is_adc_source_selected(hw, ADC_NONE))
985 return 0;
986
987 hw->select_adc_source(hw, ADC_NONE);
988
989 return 0;
990}
991
992static int atc_daio_unmute(struct ct_atc *atc, unsigned char state, int type)
993{
994 struct daio_mgr *daio_mgr = atc->rsc_mgrs[DAIO];
995
996 if (state)
997 daio_mgr->daio_enable(daio_mgr, atc->daios[type]);
998 else
999 daio_mgr->daio_disable(daio_mgr, atc->daios[type]);
1000
1001 daio_mgr->commit_write(daio_mgr);
1002
1003 return 0;
1004}
1005
1006static int
1007atc_dao_get_status(struct ct_atc *atc, unsigned int *status, int type)
1008{
1009 struct dao *dao = container_of(atc->daios[type], struct dao, daio);
1010 return dao->ops->get_spos(dao, status);
1011}
1012
1013static int
1014atc_dao_set_status(struct ct_atc *atc, unsigned int status, int type)
1015{
1016 struct dao *dao = container_of(atc->daios[type], struct dao, daio);
1017
1018 dao->ops->set_spos(dao, status);
1019 dao->ops->commit_write(dao);
1020 return 0;
1021}
1022
1023static int atc_line_front_unmute(struct ct_atc *atc, unsigned char state)
1024{
1025 return atc_daio_unmute(atc, state, LINEO1);
1026}
1027
1028static int atc_line_surround_unmute(struct ct_atc *atc, unsigned char state)
1029{
Sven Eckelmann3b046912009-10-01 20:06:39 +02001030 return atc_daio_unmute(atc, state, LINEO2);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001031}
1032
1033static int atc_line_clfe_unmute(struct ct_atc *atc, unsigned char state)
1034{
1035 return atc_daio_unmute(atc, state, LINEO3);
1036}
1037
1038static int atc_line_rear_unmute(struct ct_atc *atc, unsigned char state)
1039{
Sven Eckelmann3b046912009-10-01 20:06:39 +02001040 return atc_daio_unmute(atc, state, LINEO4);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001041}
1042
1043static int atc_line_in_unmute(struct ct_atc *atc, unsigned char state)
1044{
1045 return atc_daio_unmute(atc, state, LINEIM);
1046}
1047
1048static int atc_spdif_out_unmute(struct ct_atc *atc, unsigned char state)
1049{
1050 return atc_daio_unmute(atc, state, SPDIFOO);
1051}
1052
1053static int atc_spdif_in_unmute(struct ct_atc *atc, unsigned char state)
1054{
1055 return atc_daio_unmute(atc, state, SPDIFIO);
1056}
1057
1058static int atc_spdif_out_get_status(struct ct_atc *atc, unsigned int *status)
1059{
1060 return atc_dao_get_status(atc, status, SPDIFOO);
1061}
1062
1063static int atc_spdif_out_set_status(struct ct_atc *atc, unsigned int status)
1064{
1065 return atc_dao_set_status(atc, status, SPDIFOO);
1066}
1067
1068static int atc_spdif_out_passthru(struct ct_atc *atc, unsigned char state)
1069{
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001070 struct dao_desc da_dsc = {0};
Takashi Iwai514eef92009-06-08 14:57:57 +02001071 struct dao *dao;
1072 int err;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001073 struct ct_mixer *mixer = atc->mixer;
1074 struct rsc *rscs[2] = {NULL};
1075 unsigned int spos = 0;
1076
Takashi Iwai635c2652009-06-13 10:12:59 +02001077 mutex_lock(&atc->atc_mutex);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001078 dao = container_of(atc->daios[SPDIFOO], struct dao, daio);
1079 da_dsc.msr = state ? 1 : atc->msr;
1080 da_dsc.passthru = state ? 1 : 0;
1081 err = dao->ops->reinit(dao, &da_dsc);
1082 if (state) {
1083 spos = IEC958_DEFAULT_CON;
1084 } else {
1085 mixer->get_output_ports(mixer, MIX_SPDIF_OUT,
1086 &rscs[0], &rscs[1]);
1087 dao->ops->set_left_input(dao, rscs[0]);
1088 dao->ops->set_right_input(dao, rscs[1]);
1089 /* Restore PLL to atc->rsr if needed. */
Takashi Iwai514eef92009-06-08 14:57:57 +02001090 if (atc->pll_rate != atc->rsr)
1091 err = atc_pll_init(atc, atc->rsr);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001092 }
1093 dao->ops->set_spos(dao, spos);
1094 dao->ops->commit_write(dao);
Takashi Iwai635c2652009-06-13 10:12:59 +02001095 mutex_unlock(&atc->atc_mutex);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001096
1097 return err;
1098}
1099
Wai Yew CHAY29959a02009-06-22 14:52:34 +02001100static int atc_release_resources(struct ct_atc *atc)
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001101{
Wai Yew CHAY29959a02009-06-22 14:52:34 +02001102 int i;
1103 struct daio_mgr *daio_mgr = NULL;
1104 struct dao *dao = NULL;
1105 struct dai *dai = NULL;
1106 struct daio *daio = NULL;
1107 struct sum_mgr *sum_mgr = NULL;
1108 struct src_mgr *src_mgr = NULL;
1109 struct srcimp_mgr *srcimp_mgr = NULL;
1110 struct srcimp *srcimp = NULL;
1111 struct ct_mixer *mixer = NULL;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001112
Wai Yew CHAY29959a02009-06-22 14:52:34 +02001113 /* disconnect internal mixer objects */
Takashi Iwai35ebf6e2009-07-22 17:12:34 +02001114 if (atc->mixer) {
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001115 mixer = atc->mixer;
1116 mixer->set_input_left(mixer, MIX_LINE_IN, NULL);
1117 mixer->set_input_right(mixer, MIX_LINE_IN, NULL);
1118 mixer->set_input_left(mixer, MIX_MIC_IN, NULL);
1119 mixer->set_input_right(mixer, MIX_MIC_IN, NULL);
1120 mixer->set_input_left(mixer, MIX_SPDIF_IN, NULL);
1121 mixer->set_input_right(mixer, MIX_SPDIF_IN, NULL);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001122 }
1123
Takashi Iwai35ebf6e2009-07-22 17:12:34 +02001124 if (atc->daios) {
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001125 daio_mgr = (struct daio_mgr *)atc->rsc_mgrs[DAIO];
1126 for (i = 0; i < atc->n_daio; i++) {
1127 daio = atc->daios[i];
1128 if (daio->type < LINEIM) {
1129 dao = container_of(daio, struct dao, daio);
1130 dao->ops->clear_left_input(dao);
1131 dao->ops->clear_right_input(dao);
1132 } else {
1133 dai = container_of(daio, struct dai, daio);
1134 /* some thing to do for dai ... */
1135 }
1136 daio_mgr->put_daio(daio_mgr, daio);
1137 }
1138 kfree(atc->daios);
Wai Yew CHAY29959a02009-06-22 14:52:34 +02001139 atc->daios = NULL;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001140 }
1141
Takashi Iwai35ebf6e2009-07-22 17:12:34 +02001142 if (atc->pcm) {
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001143 sum_mgr = atc->rsc_mgrs[SUM];
1144 for (i = 0; i < atc->n_pcm; i++)
1145 sum_mgr->put_sum(sum_mgr, atc->pcm[i]);
1146
1147 kfree(atc->pcm);
Wai Yew CHAY29959a02009-06-22 14:52:34 +02001148 atc->pcm = NULL;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001149 }
1150
Takashi Iwai35ebf6e2009-07-22 17:12:34 +02001151 if (atc->srcs) {
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001152 src_mgr = atc->rsc_mgrs[SRC];
1153 for (i = 0; i < atc->n_src; i++)
1154 src_mgr->put_src(src_mgr, atc->srcs[i]);
1155
1156 kfree(atc->srcs);
Wai Yew CHAY29959a02009-06-22 14:52:34 +02001157 atc->srcs = NULL;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001158 }
1159
Takashi Iwai35ebf6e2009-07-22 17:12:34 +02001160 if (atc->srcimps) {
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001161 srcimp_mgr = atc->rsc_mgrs[SRCIMP];
1162 for (i = 0; i < atc->n_srcimp; i++) {
1163 srcimp = atc->srcimps[i];
1164 srcimp->ops->unmap(srcimp);
1165 srcimp_mgr->put_srcimp(srcimp_mgr, atc->srcimps[i]);
1166 }
1167 kfree(atc->srcimps);
Wai Yew CHAY29959a02009-06-22 14:52:34 +02001168 atc->srcimps = NULL;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001169 }
1170
Wai Yew CHAY29959a02009-06-22 14:52:34 +02001171 return 0;
1172}
1173
1174static int ct_atc_destroy(struct ct_atc *atc)
1175{
1176 int i = 0;
1177
Takashi Iwai35ebf6e2009-07-22 17:12:34 +02001178 if (!atc)
Wai Yew CHAY29959a02009-06-22 14:52:34 +02001179 return 0;
1180
1181 if (atc->timer) {
1182 ct_timer_free(atc->timer);
1183 atc->timer = NULL;
1184 }
1185
1186 atc_release_resources(atc);
1187
1188 /* Destroy internal mixer objects */
Takashi Iwai35ebf6e2009-07-22 17:12:34 +02001189 if (atc->mixer)
Wai Yew CHAY29959a02009-06-22 14:52:34 +02001190 ct_mixer_destroy(atc->mixer);
1191
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001192 for (i = 0; i < NUM_RSCTYP; i++) {
Takashi Iwai35ebf6e2009-07-22 17:12:34 +02001193 if (rsc_mgr_funcs[i].destroy && atc->rsc_mgrs[i])
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001194 rsc_mgr_funcs[i].destroy(atc->rsc_mgrs[i]);
1195
1196 }
1197
Takashi Iwai35ebf6e2009-07-22 17:12:34 +02001198 if (atc->hw)
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001199 destroy_hw_obj((struct hw *)atc->hw);
1200
1201 /* Destroy device virtual memory manager object */
Takashi Iwai35ebf6e2009-07-22 17:12:34 +02001202 if (atc->vm) {
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001203 ct_vm_destroy(atc->vm);
1204 atc->vm = NULL;
1205 }
1206
1207 kfree(atc);
1208
1209 return 0;
1210}
1211
1212static int atc_dev_free(struct snd_device *dev)
1213{
1214 struct ct_atc *atc = dev->device_data;
1215 return ct_atc_destroy(atc);
1216}
1217
Takashi Iwai408bffd2010-01-14 09:19:46 +01001218static int __devinit atc_identify_card(struct ct_atc *atc, unsigned int ssid)
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001219{
Takashi Iwai94701952009-06-08 18:10:32 +02001220 const struct snd_pci_quirk *p;
1221 const struct snd_pci_quirk *list;
Takashi Iwai408bffd2010-01-14 09:19:46 +01001222 u16 vendor_id, device_id;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001223
Takashi Iwai94701952009-06-08 18:10:32 +02001224 switch (atc->chip_type) {
1225 case ATC20K1:
1226 atc->chip_name = "20K1";
1227 list = subsys_20k1_list;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001228 break;
Takashi Iwai94701952009-06-08 18:10:32 +02001229 case ATC20K2:
1230 atc->chip_name = "20K2";
1231 list = subsys_20k2_list;
1232 break;
1233 default:
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001234 return -ENOENT;
Takashi Iwai94701952009-06-08 18:10:32 +02001235 }
Takashi Iwai408bffd2010-01-14 09:19:46 +01001236 if (ssid) {
1237 vendor_id = ssid >> 16;
1238 device_id = ssid & 0xffff;
1239 } else {
1240 vendor_id = atc->pci->subsystem_vendor;
1241 device_id = atc->pci->subsystem_device;
1242 }
1243 p = snd_pci_quirk_lookup_id(vendor_id, device_id, list);
Takashi Iwaia8f43102009-06-22 07:36:52 +02001244 if (p) {
1245 if (p->value < 0) {
1246 printk(KERN_ERR "ctxfi: "
1247 "Device %04x:%04x is black-listed\n",
Takashi Iwai408bffd2010-01-14 09:19:46 +01001248 vendor_id, device_id);
Takashi Iwaia8f43102009-06-22 07:36:52 +02001249 return -ENOENT;
1250 }
1251 atc->model = p->value;
1252 } else {
1253 if (atc->chip_type == ATC20K1)
1254 atc->model = CT20K1_UNKNOWN;
1255 else
1256 atc->model = CT20K2_UNKNOWN;
1257 }
Takashi Iwai94701952009-06-08 18:10:32 +02001258 atc->model_name = ct_subsys_name[atc->model];
1259 snd_printd("ctxfi: chip %s model %s (%04x:%04x) is found\n",
1260 atc->chip_name, atc->model_name,
Takashi Iwai408bffd2010-01-14 09:19:46 +01001261 vendor_id, device_id);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001262 return 0;
1263}
1264
Takashi Iwai2a36f672009-06-05 16:34:10 +02001265int __devinit ct_atc_create_alsa_devs(struct ct_atc *atc)
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001266{
1267 enum CTALSADEVS i;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001268 int err;
1269
Takashi Iwai94701952009-06-08 18:10:32 +02001270 alsa_dev_funcs[MIXER].public_name = atc->chip_name;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001271
1272 for (i = 0; i < NUM_CTALSADEVS; i++) {
Takashi Iwai35ebf6e2009-07-22 17:12:34 +02001273 if (!alsa_dev_funcs[i].create)
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001274 continue;
1275
1276 err = alsa_dev_funcs[i].create(atc, i,
1277 alsa_dev_funcs[i].public_name);
1278 if (err) {
Takashi Iwaib3e0afe2009-05-14 15:19:30 +02001279 printk(KERN_ERR "ctxfi: "
1280 "Creating alsa device %d failed!\n", i);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001281 return err;
1282 }
1283 }
1284
1285 return 0;
1286}
1287
Takashi Iwai2a36f672009-06-05 16:34:10 +02001288static int __devinit atc_create_hw_devs(struct ct_atc *atc)
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001289{
Takashi Iwai514eef92009-06-08 14:57:57 +02001290 struct hw *hw;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001291 struct card_conf info = {0};
Takashi Iwai514eef92009-06-08 14:57:57 +02001292 int i, err;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001293
Takashi Iwai94701952009-06-08 18:10:32 +02001294 err = create_hw_obj(atc->pci, atc->chip_type, atc->model, &hw);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001295 if (err) {
1296 printk(KERN_ERR "Failed to create hw obj!!!\n");
1297 return err;
1298 }
1299 atc->hw = hw;
1300
1301 /* Initialize card hardware. */
1302 info.rsr = atc->rsr;
1303 info.msr = atc->msr;
1304 info.vm_pgt_phys = atc_get_ptp_phys(atc, 0);
1305 err = hw->card_init(hw, &info);
1306 if (err < 0)
1307 return err;
1308
1309 for (i = 0; i < NUM_RSCTYP; i++) {
Takashi Iwai35ebf6e2009-07-22 17:12:34 +02001310 if (!rsc_mgr_funcs[i].create)
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001311 continue;
1312
1313 err = rsc_mgr_funcs[i].create(atc->hw, &atc->rsc_mgrs[i]);
1314 if (err) {
Takashi Iwaib3e0afe2009-05-14 15:19:30 +02001315 printk(KERN_ERR "ctxfi: "
1316 "Failed to create rsc_mgr %d!!!\n", i);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001317 return err;
1318 }
1319 }
1320
1321 return 0;
1322}
1323
Wai Yew CHAY29959a02009-06-22 14:52:34 +02001324static int atc_get_resources(struct ct_atc *atc)
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001325{
1326 struct daio_desc da_desc = {0};
Takashi Iwai514eef92009-06-08 14:57:57 +02001327 struct daio_mgr *daio_mgr;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001328 struct src_desc src_dsc = {0};
Takashi Iwai514eef92009-06-08 14:57:57 +02001329 struct src_mgr *src_mgr;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001330 struct srcimp_desc srcimp_dsc = {0};
Takashi Iwai514eef92009-06-08 14:57:57 +02001331 struct srcimp_mgr *srcimp_mgr;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001332 struct sum_desc sum_dsc = {0};
Takashi Iwai514eef92009-06-08 14:57:57 +02001333 struct sum_mgr *sum_mgr;
1334 int err, i;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001335
1336 atc->daios = kzalloc(sizeof(void *)*(DAIONUM), GFP_KERNEL);
Takashi Iwai35ebf6e2009-07-22 17:12:34 +02001337 if (!atc->daios)
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001338 return -ENOMEM;
1339
1340 atc->srcs = kzalloc(sizeof(void *)*(2*2), GFP_KERNEL);
Takashi Iwai35ebf6e2009-07-22 17:12:34 +02001341 if (!atc->srcs)
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001342 return -ENOMEM;
1343
1344 atc->srcimps = kzalloc(sizeof(void *)*(2*2), GFP_KERNEL);
Takashi Iwai35ebf6e2009-07-22 17:12:34 +02001345 if (!atc->srcimps)
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001346 return -ENOMEM;
1347
1348 atc->pcm = kzalloc(sizeof(void *)*(2*4), GFP_KERNEL);
Takashi Iwai35ebf6e2009-07-22 17:12:34 +02001349 if (!atc->pcm)
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001350 return -ENOMEM;
1351
1352 daio_mgr = (struct daio_mgr *)atc->rsc_mgrs[DAIO];
1353 da_desc.msr = atc->msr;
1354 for (i = 0, atc->n_daio = 0; i < DAIONUM-1; i++) {
1355 da_desc.type = i;
1356 err = daio_mgr->get_daio(daio_mgr, &da_desc,
1357 (struct daio **)&atc->daios[i]);
1358 if (err) {
Takashi Iwaib3e0afe2009-05-14 15:19:30 +02001359 printk(KERN_ERR "ctxfi: Failed to get DAIO "
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001360 "resource %d!!!\n", i);
1361 return err;
1362 }
1363 atc->n_daio++;
1364 }
Takashi Iwai94701952009-06-08 18:10:32 +02001365 if (atc->model == CTSB073X)
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001366 da_desc.type = SPDIFI1;
Takashi Iwai94701952009-06-08 18:10:32 +02001367 else
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001368 da_desc.type = SPDIFIO;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001369 err = daio_mgr->get_daio(daio_mgr, &da_desc,
1370 (struct daio **)&atc->daios[i]);
1371 if (err) {
Takashi Iwaib3e0afe2009-05-14 15:19:30 +02001372 printk(KERN_ERR "ctxfi: Failed to get S/PDIF-in resource!!!\n");
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001373 return err;
1374 }
1375 atc->n_daio++;
1376
1377 src_mgr = atc->rsc_mgrs[SRC];
1378 src_dsc.multi = 1;
1379 src_dsc.msr = atc->msr;
1380 src_dsc.mode = ARCRW;
1381 for (i = 0, atc->n_src = 0; i < (2*2); i++) {
1382 err = src_mgr->get_src(src_mgr, &src_dsc,
1383 (struct src **)&atc->srcs[i]);
1384 if (err)
1385 return err;
1386
1387 atc->n_src++;
1388 }
1389
1390 srcimp_mgr = atc->rsc_mgrs[SRCIMP];
1391 srcimp_dsc.msr = 8; /* SRCIMPs for S/PDIFIn SRT */
1392 for (i = 0, atc->n_srcimp = 0; i < (2*1); i++) {
1393 err = srcimp_mgr->get_srcimp(srcimp_mgr, &srcimp_dsc,
1394 (struct srcimp **)&atc->srcimps[i]);
1395 if (err)
1396 return err;
1397
1398 atc->n_srcimp++;
1399 }
1400 srcimp_dsc.msr = 8; /* SRCIMPs for LINE/MICIn SRT */
1401 for (i = 0; i < (2*1); i++) {
1402 err = srcimp_mgr->get_srcimp(srcimp_mgr, &srcimp_dsc,
1403 (struct srcimp **)&atc->srcimps[2*1+i]);
1404 if (err)
1405 return err;
1406
1407 atc->n_srcimp++;
1408 }
1409
1410 sum_mgr = atc->rsc_mgrs[SUM];
1411 sum_dsc.msr = atc->msr;
1412 for (i = 0, atc->n_pcm = 0; i < (2*4); i++) {
1413 err = sum_mgr->get_sum(sum_mgr, &sum_dsc,
1414 (struct sum **)&atc->pcm[i]);
1415 if (err)
1416 return err;
1417
1418 atc->n_pcm++;
1419 }
1420
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001421 return 0;
1422}
1423
Wai Yew CHAY29959a02009-06-22 14:52:34 +02001424static void
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001425atc_connect_dai(struct src_mgr *src_mgr, struct dai *dai,
1426 struct src **srcs, struct srcimp **srcimps)
1427{
1428 struct rsc *rscs[2] = {NULL};
Takashi Iwai514eef92009-06-08 14:57:57 +02001429 struct src *src;
1430 struct srcimp *srcimp;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001431 int i = 0;
1432
1433 rscs[0] = &dai->daio.rscl;
1434 rscs[1] = &dai->daio.rscr;
1435 for (i = 0; i < 2; i++) {
1436 src = srcs[i];
1437 srcimp = srcimps[i];
1438 srcimp->ops->map(srcimp, src, rscs[i]);
1439 src_mgr->src_disable(src_mgr, src);
1440 }
1441
1442 src_mgr->commit_write(src_mgr); /* Actually disable SRCs */
1443
1444 src = srcs[0];
1445 src->ops->set_pm(src, 1);
1446 for (i = 0; i < 2; i++) {
1447 src = srcs[i];
1448 src->ops->set_state(src, SRC_STATE_RUN);
1449 src->ops->commit_write(src);
1450 src_mgr->src_enable_s(src_mgr, src);
1451 }
1452
1453 dai->ops->set_srt_srcl(dai, &(srcs[0]->rsc));
1454 dai->ops->set_srt_srcr(dai, &(srcs[1]->rsc));
1455
1456 dai->ops->set_enb_src(dai, 1);
1457 dai->ops->set_enb_srt(dai, 1);
1458 dai->ops->commit_write(dai);
1459
1460 src_mgr->commit_write(src_mgr); /* Synchronously enable SRCs */
1461}
1462
Wai Yew CHAY29959a02009-06-22 14:52:34 +02001463static void atc_connect_resources(struct ct_atc *atc)
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001464{
Takashi Iwai514eef92009-06-08 14:57:57 +02001465 struct dai *dai;
1466 struct dao *dao;
1467 struct src *src;
1468 struct sum *sum;
1469 struct ct_mixer *mixer;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001470 struct rsc *rscs[2] = {NULL};
Takashi Iwai514eef92009-06-08 14:57:57 +02001471 int i, j;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001472
1473 mixer = atc->mixer;
1474
1475 for (i = MIX_WAVE_FRONT, j = LINEO1; i <= MIX_SPDIF_OUT; i++, j++) {
1476 mixer->get_output_ports(mixer, i, &rscs[0], &rscs[1]);
1477 dao = container_of(atc->daios[j], struct dao, daio);
1478 dao->ops->set_left_input(dao, rscs[0]);
1479 dao->ops->set_right_input(dao, rscs[1]);
1480 }
1481
1482 dai = container_of(atc->daios[LINEIM], struct dai, daio);
1483 atc_connect_dai(atc->rsc_mgrs[SRC], dai,
1484 (struct src **)&atc->srcs[2],
1485 (struct srcimp **)&atc->srcimps[2]);
1486 src = atc->srcs[2];
1487 mixer->set_input_left(mixer, MIX_LINE_IN, &src->rsc);
1488 src = atc->srcs[3];
1489 mixer->set_input_right(mixer, MIX_LINE_IN, &src->rsc);
1490
1491 dai = container_of(atc->daios[SPDIFIO], struct dai, daio);
1492 atc_connect_dai(atc->rsc_mgrs[SRC], dai,
1493 (struct src **)&atc->srcs[0],
1494 (struct srcimp **)&atc->srcimps[0]);
1495
1496 src = atc->srcs[0];
1497 mixer->set_input_left(mixer, MIX_SPDIF_IN, &src->rsc);
1498 src = atc->srcs[1];
1499 mixer->set_input_right(mixer, MIX_SPDIF_IN, &src->rsc);
1500
1501 for (i = MIX_PCMI_FRONT, j = 0; i <= MIX_PCMI_SURROUND; i++, j += 2) {
1502 sum = atc->pcm[j];
1503 mixer->set_input_left(mixer, i, &sum->rsc);
1504 sum = atc->pcm[j+1];
1505 mixer->set_input_right(mixer, i, &sum->rsc);
1506 }
1507}
1508
Wai Yew CHAY29959a02009-06-22 14:52:34 +02001509#ifdef CONFIG_PM
1510static int atc_suspend(struct ct_atc *atc, pm_message_t state)
1511{
1512 int i;
1513 struct hw *hw = atc->hw;
1514
1515 snd_power_change_state(atc->card, SNDRV_CTL_POWER_D3hot);
1516
1517 for (i = FRONT; i < NUM_PCMS; i++) {
1518 if (!atc->pcms[i])
1519 continue;
1520
1521 snd_pcm_suspend_all(atc->pcms[i]);
1522 }
1523
1524 atc_release_resources(atc);
1525
1526 hw->suspend(hw, state);
1527
1528 return 0;
1529}
1530
1531static int atc_hw_resume(struct ct_atc *atc)
1532{
1533 struct hw *hw = atc->hw;
1534 struct card_conf info = {0};
1535
1536 /* Re-initialize card hardware. */
1537 info.rsr = atc->rsr;
1538 info.msr = atc->msr;
1539 info.vm_pgt_phys = atc_get_ptp_phys(atc, 0);
1540 return hw->resume(hw, &info);
1541}
1542
1543static int atc_resources_resume(struct ct_atc *atc)
1544{
1545 struct ct_mixer *mixer;
1546 int err = 0;
1547
1548 /* Get resources */
1549 err = atc_get_resources(atc);
1550 if (err < 0) {
1551 atc_release_resources(atc);
1552 return err;
1553 }
1554
1555 /* Build topology */
1556 atc_connect_resources(atc);
1557
1558 mixer = atc->mixer;
1559 mixer->resume(mixer);
1560
1561 return 0;
1562}
1563
1564static int atc_resume(struct ct_atc *atc)
1565{
1566 int err = 0;
1567
1568 /* Do hardware resume. */
1569 err = atc_hw_resume(atc);
1570 if (err < 0) {
1571 printk(KERN_ERR "ctxfi: pci_enable_device failed, "
1572 "disabling device\n");
1573 snd_card_disconnect(atc->card);
1574 return err;
1575 }
1576
1577 err = atc_resources_resume(atc);
1578 if (err < 0)
1579 return err;
1580
1581 snd_power_change_state(atc->card, SNDRV_CTL_POWER_D0);
1582
1583 return 0;
1584}
1585#endif
1586
Takashi Iwai2a36f672009-06-05 16:34:10 +02001587static struct ct_atc atc_preset __devinitdata = {
1588 .map_audio_buffer = ct_map_audio_buffer,
1589 .unmap_audio_buffer = ct_unmap_audio_buffer,
1590 .pcm_playback_prepare = atc_pcm_playback_prepare,
1591 .pcm_release_resources = atc_pcm_release_resources,
1592 .pcm_playback_start = atc_pcm_playback_start,
1593 .pcm_playback_stop = atc_pcm_stop,
1594 .pcm_playback_position = atc_pcm_playback_position,
1595 .pcm_capture_prepare = atc_pcm_capture_prepare,
1596 .pcm_capture_start = atc_pcm_capture_start,
1597 .pcm_capture_stop = atc_pcm_stop,
1598 .pcm_capture_position = atc_pcm_capture_position,
1599 .spdif_passthru_playback_prepare = spdif_passthru_playback_prepare,
1600 .get_ptp_phys = atc_get_ptp_phys,
1601 .select_line_in = atc_select_line_in,
1602 .select_mic_in = atc_select_mic_in,
1603 .select_digit_io = atc_select_digit_io,
1604 .line_front_unmute = atc_line_front_unmute,
1605 .line_surround_unmute = atc_line_surround_unmute,
1606 .line_clfe_unmute = atc_line_clfe_unmute,
1607 .line_rear_unmute = atc_line_rear_unmute,
1608 .line_in_unmute = atc_line_in_unmute,
1609 .spdif_out_unmute = atc_spdif_out_unmute,
1610 .spdif_in_unmute = atc_spdif_in_unmute,
1611 .spdif_out_get_status = atc_spdif_out_get_status,
1612 .spdif_out_set_status = atc_spdif_out_set_status,
1613 .spdif_out_passthru = atc_spdif_out_passthru,
1614 .have_digit_io_switch = atc_have_digit_io_switch,
Wai Yew CHAY29959a02009-06-22 14:52:34 +02001615#ifdef CONFIG_PM
1616 .suspend = atc_suspend,
1617 .resume = atc_resume,
1618#endif
Takashi Iwai2a36f672009-06-05 16:34:10 +02001619};
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001620
1621/**
1622 * ct_atc_create - create and initialize a hardware manager
1623 * @card: corresponding alsa card object
1624 * @pci: corresponding kernel pci device object
1625 * @ratc: return created object address in it
1626 *
1627 * Creates and initializes a hardware manager.
1628 *
1629 * Creates kmallocated ct_atc structure. Initializes hardware.
1630 * Returns 0 if suceeds, or negative error code if fails.
1631 */
1632
Takashi Iwai2a36f672009-06-05 16:34:10 +02001633int __devinit ct_atc_create(struct snd_card *card, struct pci_dev *pci,
Takashi Iwai94701952009-06-08 18:10:32 +02001634 unsigned int rsr, unsigned int msr,
Takashi Iwai408bffd2010-01-14 09:19:46 +01001635 int chip_type, unsigned int ssid,
1636 struct ct_atc **ratc)
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001637{
Takashi Iwai514eef92009-06-08 14:57:57 +02001638 struct ct_atc *atc;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001639 static struct snd_device_ops ops = {
1640 .dev_free = atc_dev_free,
1641 };
Takashi Iwai514eef92009-06-08 14:57:57 +02001642 int err;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001643
1644 *ratc = NULL;
1645
1646 atc = kzalloc(sizeof(*atc), GFP_KERNEL);
Takashi Iwai35ebf6e2009-07-22 17:12:34 +02001647 if (!atc)
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001648 return -ENOMEM;
1649
Takashi Iwai2a36f672009-06-05 16:34:10 +02001650 /* Set operations */
1651 *atc = atc_preset;
1652
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001653 atc->card = card;
1654 atc->pci = pci;
1655 atc->rsr = rsr;
1656 atc->msr = msr;
Takashi Iwai94701952009-06-08 18:10:32 +02001657 atc->chip_type = chip_type;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001658
Takashi Iwai635c2652009-06-13 10:12:59 +02001659 mutex_init(&atc->atc_mutex);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001660
1661 /* Find card model */
Takashi Iwai408bffd2010-01-14 09:19:46 +01001662 err = atc_identify_card(atc, ssid);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001663 if (err < 0) {
1664 printk(KERN_ERR "ctatc: Card not recognised\n");
1665 goto error1;
1666 }
1667
1668 /* Set up device virtual memory management object */
Jaroslav Kysela21956b62010-02-02 19:58:25 +01001669 err = ct_vm_create(&atc->vm, pci);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001670 if (err < 0)
1671 goto error1;
1672
1673 /* Create all atc hw devices */
1674 err = atc_create_hw_devs(atc);
1675 if (err < 0)
1676 goto error1;
1677
Wai Yew CHAY29959a02009-06-22 14:52:34 +02001678 err = ct_mixer_create(atc, (struct ct_mixer **)&atc->mixer);
1679 if (err) {
1680 printk(KERN_ERR "ctxfi: Failed to create mixer obj!!!\n");
1681 goto error1;
1682 }
1683
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001684 /* Get resources */
1685 err = atc_get_resources(atc);
1686 if (err < 0)
1687 goto error1;
1688
1689 /* Build topology */
1690 atc_connect_resources(atc);
1691
Takashi Iwaib7bbf872009-06-05 16:11:07 +02001692 atc->timer = ct_timer_new(atc);
1693 if (!atc->timer)
1694 goto error1;
1695
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001696 err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, atc, &ops);
1697 if (err < 0)
1698 goto error1;
1699
1700 snd_card_set_dev(card, &pci->dev);
1701
1702 *ratc = atc;
1703 return 0;
1704
1705error1:
1706 ct_atc_destroy(atc);
Takashi Iwaib3e0afe2009-05-14 15:19:30 +02001707 printk(KERN_ERR "ctxfi: Something wrong!!!\n");
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001708 return err;
1709}