blob: 7898a375df0e6925e0e9d20b4dd866771629ced8 [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
42static const struct ct_atc_chip_sub_details atc_sub_details[NUM_CTCARDS] = {
Takashi Iwai9fc20f02009-05-14 15:14:18 +020043 [CTSB0760] = {.subsys = PCI_SUBDEVICE_ID_CREATIVE_SB0760,
Wai Yew CHAY8cc72362009-05-14 08:05:58 +020044 .nm_model = "SB076x"},
Takashi Iwai9fc20f02009-05-14 15:14:18 +020045 [CTHENDRIX] = {.subsys = PCI_SUBDEVICE_ID_CREATIVE_HENDRIX,
Wai Yew CHAY8cc72362009-05-14 08:05:58 +020046 .nm_model = "Hendrix"},
Takashi Iwai9fc20f02009-05-14 15:14:18 +020047 [CTSB08801] = {.subsys = PCI_SUBDEVICE_ID_CREATIVE_SB08801,
Wai Yew CHAY8cc72362009-05-14 08:05:58 +020048 .nm_model = "SB0880"},
Takashi Iwai9fc20f02009-05-14 15:14:18 +020049 [CTSB08802] = {.subsys = PCI_SUBDEVICE_ID_CREATIVE_SB08802,
Wai Yew CHAY8cc72362009-05-14 08:05:58 +020050 .nm_model = "SB0880"},
Takashi Iwai9fc20f02009-05-14 15:14:18 +020051 [CTSB08803] = {.subsys = PCI_SUBDEVICE_ID_CREATIVE_SB08803,
Wai Yew CHAY8cc72362009-05-14 08:05:58 +020052 .nm_model = "SB0880"}
53};
54
55static struct ct_atc_chip_details atc_chip_details[] = {
Takashi Iwai9fc20f02009-05-14 15:14:18 +020056 {.vendor = PCI_VENDOR_ID_CREATIVE,
57 .device = PCI_DEVICE_ID_CREATIVE_20K1,
Wai Yew CHAY8cc72362009-05-14 08:05:58 +020058 .sub_details = NULL,
59 .nm_card = "X-Fi 20k1"},
Takashi Iwai9fc20f02009-05-14 15:14:18 +020060 {.vendor = PCI_VENDOR_ID_CREATIVE,
61 .device = PCI_DEVICE_ID_CREATIVE_20K2,
Wai Yew CHAY8cc72362009-05-14 08:05:58 +020062 .sub_details = atc_sub_details,
63 .nm_card = "X-Fi 20k2"},
64 {} /* terminator */
65};
66
67static struct {
68 int (*create)(struct ct_atc *atc,
69 enum CTALSADEVS device, const char *device_name);
70 int (*destroy)(void *alsa_dev);
71 const char *public_name;
72} alsa_dev_funcs[NUM_CTALSADEVS] = {
73 [FRONT] = { .create = ct_alsa_pcm_create,
74 .destroy = NULL,
75 .public_name = "Front/WaveIn"},
Wai Yew CHAY8cc72362009-05-14 08:05:58 +020076 [SURROUND] = { .create = ct_alsa_pcm_create,
77 .destroy = NULL,
78 .public_name = "Surround"},
Takashi Iwai8372d4982009-06-02 14:27:56 +020079 [CLFE] = { .create = ct_alsa_pcm_create,
80 .destroy = NULL,
81 .public_name = "Center/LFE"},
82 [SIDE] = { .create = ct_alsa_pcm_create,
83 .destroy = NULL,
84 .public_name = "Side"},
Wai Yew CHAY8cc72362009-05-14 08:05:58 +020085 [IEC958] = { .create = ct_alsa_pcm_create,
86 .destroy = NULL,
87 .public_name = "IEC958 Non-audio"},
88
89 [MIXER] = { .create = ct_alsa_mix_create,
90 .destroy = NULL,
91 .public_name = "Mixer"}
92};
93
94typedef int (*create_t)(void *, void **);
95typedef int (*destroy_t)(void *);
96
97static struct {
98 int (*create)(void *hw, void **rmgr);
99 int (*destroy)(void *mgr);
100} rsc_mgr_funcs[NUM_RSCTYP] = {
101 [SRC] = { .create = (create_t)src_mgr_create,
102 .destroy = (destroy_t)src_mgr_destroy },
103 [SRCIMP] = { .create = (create_t)srcimp_mgr_create,
104 .destroy = (destroy_t)srcimp_mgr_destroy },
105 [AMIXER] = { .create = (create_t)amixer_mgr_create,
106 .destroy = (destroy_t)amixer_mgr_destroy },
107 [SUM] = { .create = (create_t)sum_mgr_create,
108 .destroy = (destroy_t)sum_mgr_destroy },
109 [DAIO] = { .create = (create_t)daio_mgr_create,
110 .destroy = (destroy_t)daio_mgr_destroy }
111};
112
113static int
114atc_pcm_release_resources(struct ct_atc *atc, struct ct_atc_pcm *apcm);
115
116/* *
117 * Only mono and interleaved modes are supported now.
118 * Always allocates a contiguous channel block.
119 * */
120
121static int ct_map_audio_buffer(struct ct_atc *atc, struct ct_atc_pcm *apcm)
122{
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200123 struct snd_pcm_runtime *runtime;
124 struct ct_vm *vm;
125
126 if (NULL == apcm->substream)
127 return 0;
128
129 runtime = apcm->substream->runtime;
130 vm = atc->vm;
131
Takashi Iwaic76157d2009-06-02 15:26:19 +0200132 apcm->vm_block = vm->map(vm, apcm->substream, runtime->dma_bytes);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200133
134 if (NULL == apcm->vm_block)
135 return -ENOENT;
136
137 return 0;
138}
139
140static void ct_unmap_audio_buffer(struct ct_atc *atc, struct ct_atc_pcm *apcm)
141{
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200142 struct ct_vm *vm;
143
144 if (NULL == apcm->vm_block)
145 return;
146
147 vm = atc->vm;
148
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200149 vm->unmap(vm, apcm->vm_block);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200150
151 apcm->vm_block = NULL;
152}
153
154static unsigned long atc_get_ptp_phys(struct ct_atc *atc, int index)
155{
156 struct ct_vm *vm;
157 void *kvirt_addr;
158 unsigned long phys_addr;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200159
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200160 vm = atc->vm;
161 kvirt_addr = vm->get_ptp_virt(vm, index);
162 if (kvirt_addr == NULL)
163 phys_addr = (~0UL);
164 else
165 phys_addr = virt_to_phys(kvirt_addr);
166
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200167 return phys_addr;
168}
169
170static unsigned int convert_format(snd_pcm_format_t snd_format)
171{
172 switch (snd_format) {
173 case SNDRV_PCM_FORMAT_U8:
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200174 return SRC_SF_U8;
175 case SNDRV_PCM_FORMAT_S16_LE:
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200176 return SRC_SF_S16;
177 case SNDRV_PCM_FORMAT_S24_3LE:
178 return SRC_SF_S24;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200179 case SNDRV_PCM_FORMAT_S32_LE:
180 return SRC_SF_S32;
Takashi Iwaid2b9b962009-06-02 14:39:05 +0200181 case SNDRV_PCM_FORMAT_FLOAT_LE:
182 return SRC_SF_F32;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200183 default:
Takashi Iwaib3e0afe2009-05-14 15:19:30 +0200184 printk(KERN_ERR "ctxfi: not recognized snd format is %d \n",
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200185 snd_format);
186 return SRC_SF_S16;
187 }
188}
189
190static unsigned int
191atc_get_pitch(unsigned int input_rate, unsigned int output_rate)
192{
Takashi Iwai514eef92009-06-08 14:57:57 +0200193 unsigned int pitch;
194 int b;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200195
196 /* get pitch and convert to fixed-point 8.24 format. */
197 pitch = (input_rate / output_rate) << 24;
198 input_rate %= output_rate;
199 input_rate /= 100;
200 output_rate /= 100;
201 for (b = 31; ((b >= 0) && !(input_rate >> b)); )
202 b--;
203
204 if (b >= 0) {
205 input_rate <<= (31 - b);
206 input_rate /= output_rate;
207 b = 24 - (31 - b);
208 if (b >= 0)
209 input_rate <<= b;
210 else
211 input_rate >>= -b;
212
213 pitch |= input_rate;
214 }
215
216 return pitch;
217}
218
219static int select_rom(unsigned int pitch)
220{
221 if ((pitch > 0x00428f5c) && (pitch < 0x01b851ec)) {
222 /* 0.26 <= pitch <= 1.72 */
223 return 1;
224 } else if ((0x01d66666 == pitch) || (0x01d66667 == pitch)) {
225 /* pitch == 1.8375 */
226 return 2;
227 } else if (0x02000000 == pitch) {
228 /* pitch == 2 */
229 return 3;
230 } else if ((pitch >= 0x0) && (pitch <= 0x08000000)) {
231 /* 0 <= pitch <= 8 */
232 return 0;
233 } else {
234 return -ENOENT;
235 }
236}
237
238static int atc_pcm_playback_prepare(struct ct_atc *atc, struct ct_atc_pcm *apcm)
239{
240 struct src_mgr *src_mgr = atc->rsc_mgrs[SRC];
241 struct amixer_mgr *amixer_mgr = atc->rsc_mgrs[AMIXER];
242 struct src_desc desc = {0};
243 struct amixer_desc mix_dsc = {0};
Takashi Iwai514eef92009-06-08 14:57:57 +0200244 struct src *src;
245 struct amixer *amixer;
246 int err;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200247 int n_amixer = apcm->substream->runtime->channels, i = 0;
248 int device = apcm->substream->pcm->device;
Takashi Iwai514eef92009-06-08 14:57:57 +0200249 unsigned int pitch;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200250 unsigned long flags;
251
252 if (NULL != apcm->src) {
253 /* Prepared pcm playback */
254 return 0;
255 }
256
Takashi Iwai822fa192009-06-02 14:12:17 +0200257 /* first release old resources */
258 atc->pcm_release_resources(atc, apcm);
259
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200260 /* Get SRC resource */
261 desc.multi = apcm->substream->runtime->channels;
262 desc.msr = atc->msr;
263 desc.mode = MEMRD;
264 err = src_mgr->get_src(src_mgr, &desc, (struct src **)&apcm->src);
265 if (err)
266 goto error1;
267
268 pitch = atc_get_pitch(apcm->substream->runtime->rate,
269 (atc->rsr * atc->msr));
270 src = apcm->src;
271 src->ops->set_pitch(src, pitch);
272 src->ops->set_rom(src, select_rom(pitch));
273 src->ops->set_sf(src, convert_format(apcm->substream->runtime->format));
274 src->ops->set_pm(src, (src->ops->next_interleave(src) != NULL));
275
276 /* Get AMIXER resource */
277 n_amixer = (n_amixer < 2) ? 2 : n_amixer;
278 apcm->amixers = kzalloc(sizeof(void *)*n_amixer, GFP_KERNEL);
279 if (NULL == apcm->amixers) {
280 err = -ENOMEM;
281 goto error1;
282 }
283 mix_dsc.msr = atc->msr;
284 for (i = 0, apcm->n_amixer = 0; i < n_amixer; i++) {
285 err = amixer_mgr->get_amixer(amixer_mgr, &mix_dsc,
286 (struct amixer **)&apcm->amixers[i]);
287 if (err)
288 goto error1;
289
290 apcm->n_amixer++;
291 }
292
293 /* Set up device virtual mem map */
294 err = ct_map_audio_buffer(atc, apcm);
295 if (err < 0)
296 goto error1;
297
298 /* Connect resources */
299 src = apcm->src;
300 for (i = 0; i < n_amixer; i++) {
301 amixer = apcm->amixers[i];
302 spin_lock_irqsave(&atc->atc_lock, flags);
303 amixer->ops->setup(amixer, &src->rsc,
304 INIT_VOL, atc->pcm[i+device*2]);
305 spin_unlock_irqrestore(&atc->atc_lock, flags);
306 src = src->ops->next_interleave(src);
307 if (NULL == src)
308 src = apcm->src;
309 }
310
Takashi Iwaib7bbf872009-06-05 16:11:07 +0200311 ct_timer_prepare(apcm->timer);
312
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200313 return 0;
314
315error1:
316 atc_pcm_release_resources(atc, apcm);
317 return err;
318}
319
320static int
321atc_pcm_release_resources(struct ct_atc *atc, struct ct_atc_pcm *apcm)
322{
323 struct src_mgr *src_mgr = atc->rsc_mgrs[SRC];
324 struct srcimp_mgr *srcimp_mgr = atc->rsc_mgrs[SRCIMP];
325 struct amixer_mgr *amixer_mgr = atc->rsc_mgrs[AMIXER];
326 struct sum_mgr *sum_mgr = atc->rsc_mgrs[SUM];
Takashi Iwai514eef92009-06-08 14:57:57 +0200327 struct srcimp *srcimp;
328 int i;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200329
330 if (NULL != apcm->srcimps) {
331 for (i = 0; i < apcm->n_srcimp; i++) {
332 srcimp = apcm->srcimps[i];
333 srcimp->ops->unmap(srcimp);
334 srcimp_mgr->put_srcimp(srcimp_mgr, srcimp);
335 apcm->srcimps[i] = NULL;
336 }
337 kfree(apcm->srcimps);
338 apcm->srcimps = NULL;
339 }
340
341 if (NULL != apcm->srccs) {
342 for (i = 0; i < apcm->n_srcc; i++) {
343 src_mgr->put_src(src_mgr, apcm->srccs[i]);
344 apcm->srccs[i] = NULL;
345 }
346 kfree(apcm->srccs);
347 apcm->srccs = NULL;
348 }
349
350 if (NULL != apcm->amixers) {
351 for (i = 0; i < apcm->n_amixer; i++) {
352 amixer_mgr->put_amixer(amixer_mgr, apcm->amixers[i]);
353 apcm->amixers[i] = NULL;
354 }
355 kfree(apcm->amixers);
356 apcm->amixers = NULL;
357 }
358
359 if (NULL != apcm->mono) {
360 sum_mgr->put_sum(sum_mgr, apcm->mono);
361 apcm->mono = NULL;
362 }
363
364 if (NULL != apcm->src) {
365 src_mgr->put_src(src_mgr, apcm->src);
366 apcm->src = NULL;
367 }
368
369 if (NULL != apcm->vm_block) {
370 /* Undo device virtual mem map */
371 ct_unmap_audio_buffer(atc, apcm);
372 apcm->vm_block = NULL;
373 }
374
375 return 0;
376}
377
378static int atc_pcm_playback_start(struct ct_atc *atc, struct ct_atc_pcm *apcm)
379{
Takashi Iwai514eef92009-06-08 14:57:57 +0200380 unsigned int max_cisz;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200381 struct src *src = apcm->src;
382
383 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
411 if (NULL != apcm->srccs) {
412 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
432 position = src->ops->get_ca(src);
433
434 size = apcm->vm_block->size;
435 max_cisz = src->multi * src->rsc.msr;
436 max_cisz = 128 * (max_cisz < 8 ? max_cisz : 8);
437
438 return (position + size - max_cisz - apcm->vm_block->addr) % size;
439}
440
441struct src_node_conf_t {
442 unsigned int pitch;
443 unsigned int msr:8;
444 unsigned int mix_msr:8;
445 unsigned int imp_msr:8;
446 unsigned int vo:1;
447};
448
449static void setup_src_node_conf(struct ct_atc *atc, struct ct_atc_pcm *apcm,
450 struct src_node_conf_t *conf, int *n_srcc)
451{
Takashi Iwai514eef92009-06-08 14:57:57 +0200452 unsigned int pitch;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200453
454 /* get pitch and convert to fixed-point 8.24 format. */
455 pitch = atc_get_pitch((atc->rsr * atc->msr),
456 apcm->substream->runtime->rate);
457 *n_srcc = 0;
458
459 if (1 == atc->msr) {
460 *n_srcc = apcm->substream->runtime->channels;
461 conf[0].pitch = pitch;
462 conf[0].mix_msr = conf[0].imp_msr = conf[0].msr = 1;
463 conf[0].vo = 1;
464 } else if (2 == atc->msr) {
465 if (0x8000000 < pitch) {
466 /* Need two-stage SRCs, SRCIMPs and
467 * AMIXERs for converting format */
468 conf[0].pitch = (atc->msr << 24);
469 conf[0].msr = conf[0].mix_msr = 1;
470 conf[0].imp_msr = atc->msr;
471 conf[0].vo = 0;
472 conf[1].pitch = atc_get_pitch(atc->rsr,
473 apcm->substream->runtime->rate);
474 conf[1].msr = conf[1].mix_msr = conf[1].imp_msr = 1;
475 conf[1].vo = 1;
476 *n_srcc = apcm->substream->runtime->channels * 2;
477 } else if (0x1000000 < pitch) {
478 /* Need one-stage SRCs, SRCIMPs and
479 * AMIXERs for converting format */
480 conf[0].pitch = pitch;
481 conf[0].msr = conf[0].mix_msr
482 = conf[0].imp_msr = atc->msr;
483 conf[0].vo = 1;
484 *n_srcc = apcm->substream->runtime->channels;
485 }
486 }
487}
488
489static int
490atc_pcm_capture_get_resources(struct ct_atc *atc, struct ct_atc_pcm *apcm)
491{
492 struct src_mgr *src_mgr = atc->rsc_mgrs[SRC];
493 struct srcimp_mgr *srcimp_mgr = atc->rsc_mgrs[SRCIMP];
494 struct amixer_mgr *amixer_mgr = atc->rsc_mgrs[AMIXER];
495 struct sum_mgr *sum_mgr = atc->rsc_mgrs[SUM];
496 struct src_desc src_dsc = {0};
Takashi Iwai514eef92009-06-08 14:57:57 +0200497 struct src *src;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200498 struct srcimp_desc srcimp_dsc = {0};
Takashi Iwai514eef92009-06-08 14:57:57 +0200499 struct srcimp *srcimp;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200500 struct amixer_desc mix_dsc = {0};
501 struct sum_desc sum_dsc = {0};
Takashi Iwai514eef92009-06-08 14:57:57 +0200502 unsigned int pitch;
503 int multi, err, i;
504 int n_srcimp, n_amixer, n_srcc, n_sum;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200505 struct src_node_conf_t src_node_conf[2] = {{0} };
506
Takashi Iwai822fa192009-06-02 14:12:17 +0200507 /* first release old resources */
508 atc->pcm_release_resources(atc, apcm);
509
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200510 /* The numbers of converting SRCs and SRCIMPs should be determined
511 * by pitch value. */
512
513 multi = apcm->substream->runtime->channels;
514
515 /* get pitch and convert to fixed-point 8.24 format. */
516 pitch = atc_get_pitch((atc->rsr * atc->msr),
517 apcm->substream->runtime->rate);
518
519 setup_src_node_conf(atc, apcm, src_node_conf, &n_srcc);
520 n_sum = (1 == multi) ? 1 : 0;
Takashi Iwai514eef92009-06-08 14:57:57 +0200521 n_amixer = n_sum * 2 + n_srcc;
522 n_srcimp = n_srcc;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200523 if ((multi > 1) && (0x8000000 >= pitch)) {
524 /* Need extra AMIXERs and SRCIMPs for special treatment
525 * of interleaved recording of conjugate channels */
526 n_amixer += multi * atc->msr;
527 n_srcimp += multi * atc->msr;
528 } else {
529 n_srcimp += multi;
530 }
531
532 if (n_srcc) {
533 apcm->srccs = kzalloc(sizeof(void *)*n_srcc, GFP_KERNEL);
534 if (NULL == apcm->srccs)
535 return -ENOMEM;
536 }
537 if (n_amixer) {
538 apcm->amixers = kzalloc(sizeof(void *)*n_amixer, GFP_KERNEL);
539 if (NULL == apcm->amixers) {
540 err = -ENOMEM;
541 goto error1;
542 }
543 }
544 apcm->srcimps = kzalloc(sizeof(void *)*n_srcimp, GFP_KERNEL);
545 if (NULL == apcm->srcimps) {
546 err = -ENOMEM;
547 goto error1;
548 }
549
550 /* Allocate SRCs for sample rate conversion if needed */
551 src_dsc.multi = 1;
552 src_dsc.mode = ARCRW;
553 for (i = 0, apcm->n_srcc = 0; i < n_srcc; i++) {
554 src_dsc.msr = src_node_conf[i/multi].msr;
555 err = src_mgr->get_src(src_mgr, &src_dsc,
556 (struct src **)&apcm->srccs[i]);
557 if (err)
558 goto error1;
559
560 src = apcm->srccs[i];
561 pitch = src_node_conf[i/multi].pitch;
562 src->ops->set_pitch(src, pitch);
563 src->ops->set_rom(src, select_rom(pitch));
564 src->ops->set_vo(src, src_node_conf[i/multi].vo);
565
566 apcm->n_srcc++;
567 }
568
569 /* Allocate AMIXERs for routing SRCs of conversion if needed */
570 for (i = 0, apcm->n_amixer = 0; i < n_amixer; i++) {
571 if (i < (n_sum*2))
572 mix_dsc.msr = atc->msr;
573 else if (i < (n_sum*2+n_srcc))
574 mix_dsc.msr = src_node_conf[(i-n_sum*2)/multi].mix_msr;
575 else
576 mix_dsc.msr = 1;
577
578 err = amixer_mgr->get_amixer(amixer_mgr, &mix_dsc,
579 (struct amixer **)&apcm->amixers[i]);
580 if (err)
581 goto error1;
582
583 apcm->n_amixer++;
584 }
585
586 /* Allocate a SUM resource to mix all input channels together */
587 sum_dsc.msr = atc->msr;
588 err = sum_mgr->get_sum(sum_mgr, &sum_dsc, (struct sum **)&apcm->mono);
589 if (err)
590 goto error1;
591
592 pitch = atc_get_pitch((atc->rsr * atc->msr),
593 apcm->substream->runtime->rate);
594 /* Allocate SRCIMP resources */
595 for (i = 0, apcm->n_srcimp = 0; i < n_srcimp; i++) {
596 if (i < (n_srcc))
597 srcimp_dsc.msr = src_node_conf[i/multi].imp_msr;
598 else if (1 == multi)
599 srcimp_dsc.msr = (pitch <= 0x8000000) ? atc->msr : 1;
600 else
601 srcimp_dsc.msr = 1;
602
603 err = srcimp_mgr->get_srcimp(srcimp_mgr, &srcimp_dsc, &srcimp);
604 if (err)
605 goto error1;
606
607 apcm->srcimps[i] = srcimp;
608 apcm->n_srcimp++;
609 }
610
611 /* Allocate a SRC for writing data to host memory */
612 src_dsc.multi = apcm->substream->runtime->channels;
613 src_dsc.msr = 1;
614 src_dsc.mode = MEMWR;
615 err = src_mgr->get_src(src_mgr, &src_dsc, (struct src **)&apcm->src);
616 if (err)
617 goto error1;
618
619 src = apcm->src;
620 src->ops->set_pitch(src, pitch);
621
622 /* Set up device virtual mem map */
623 err = ct_map_audio_buffer(atc, apcm);
624 if (err < 0)
625 goto error1;
626
627 return 0;
628
629error1:
630 atc_pcm_release_resources(atc, apcm);
631 return err;
632}
633
634static int atc_pcm_capture_prepare(struct ct_atc *atc, struct ct_atc_pcm *apcm)
635{
Takashi Iwai514eef92009-06-08 14:57:57 +0200636 struct src *src;
637 struct amixer *amixer;
638 struct srcimp *srcimp;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200639 struct ct_mixer *mixer = atc->mixer;
Takashi Iwai514eef92009-06-08 14:57:57 +0200640 struct sum *mono;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200641 struct rsc *out_ports[8] = {NULL};
Takashi Iwai514eef92009-06-08 14:57:57 +0200642 int err, i, j, n_sum, multi;
643 unsigned int pitch;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200644 int mix_base = 0, imp_base = 0;
645
646 if (NULL != apcm->src) {
647 /* Prepared pcm capture */
648 return 0;
649 }
650
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
769 return src->ops->get_ca(src) - apcm->vm_block->addr;
770}
771
772static int spdif_passthru_playback_get_resources(struct ct_atc *atc,
773 struct ct_atc_pcm *apcm)
774{
775 struct src_mgr *src_mgr = atc->rsc_mgrs[SRC];
776 struct amixer_mgr *amixer_mgr = atc->rsc_mgrs[AMIXER];
777 struct src_desc desc = {0};
778 struct amixer_desc mix_dsc = {0};
Takashi Iwai514eef92009-06-08 14:57:57 +0200779 struct src *src;
780 int err;
781 int n_amixer = apcm->substream->runtime->channels, i;
782 unsigned int pitch, rsr = atc->pll_rate;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200783
Takashi Iwai822fa192009-06-02 14:12:17 +0200784 /* first release old resources */
785 atc->pcm_release_resources(atc, apcm);
786
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200787 /* Get SRC resource */
788 desc.multi = apcm->substream->runtime->channels;
789 desc.msr = 1;
790 while (apcm->substream->runtime->rate > (rsr * desc.msr))
791 desc.msr <<= 1;
792
793 desc.mode = MEMRD;
794 err = src_mgr->get_src(src_mgr, &desc, (struct src **)&apcm->src);
795 if (err)
796 goto error1;
797
798 pitch = atc_get_pitch(apcm->substream->runtime->rate, (rsr * desc.msr));
799 src = apcm->src;
800 src->ops->set_pitch(src, pitch);
801 src->ops->set_rom(src, select_rom(pitch));
802 src->ops->set_sf(src, convert_format(apcm->substream->runtime->format));
803 src->ops->set_pm(src, (src->ops->next_interleave(src) != NULL));
804 src->ops->set_bp(src, 1);
805
806 /* Get AMIXER resource */
807 n_amixer = (n_amixer < 2) ? 2 : n_amixer;
808 apcm->amixers = kzalloc(sizeof(void *)*n_amixer, GFP_KERNEL);
809 if (NULL == apcm->amixers) {
810 err = -ENOMEM;
811 goto error1;
812 }
813 mix_dsc.msr = desc.msr;
814 for (i = 0, apcm->n_amixer = 0; i < n_amixer; i++) {
815 err = amixer_mgr->get_amixer(amixer_mgr, &mix_dsc,
816 (struct amixer **)&apcm->amixers[i]);
817 if (err)
818 goto error1;
819
820 apcm->n_amixer++;
821 }
822
823 /* Set up device virtual mem map */
824 err = ct_map_audio_buffer(atc, apcm);
825 if (err < 0)
826 goto error1;
827
828 return 0;
829
830error1:
831 atc_pcm_release_resources(atc, apcm);
832 return err;
833}
834
Takashi Iwai514eef92009-06-08 14:57:57 +0200835static int atc_pll_init(struct ct_atc *atc, int rate)
836{
837 struct hw *hw = atc->hw;
838 int err;
839 err = hw->pll_init(hw, rate);
840 atc->pll_rate = err ? 0 : rate;
841 return err;
842}
843
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200844static int
845spdif_passthru_playback_setup(struct ct_atc *atc, struct ct_atc_pcm *apcm)
846{
847 struct dao *dao = container_of(atc->daios[SPDIFOO], struct dao, daio);
848 unsigned long flags;
849 unsigned int rate = apcm->substream->runtime->rate;
Takashi Iwai514eef92009-06-08 14:57:57 +0200850 unsigned int status;
851 int err;
852 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
868 spin_lock_irqsave(&atc->atc_lock, flags);
869 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);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200878 spin_unlock_irqrestore(&atc->atc_lock, flags);
879
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 unsigned long flags;
892
893 if (NULL != apcm->src)
894 return 0;
895
896 /* Configure SPDIFOO and PLL to passthrough mode;
897 * determine pll_rate. */
898 err = spdif_passthru_playback_setup(atc, apcm);
899 if (err)
900 return err;
901
902 /* Get needed resources. */
903 err = spdif_passthru_playback_get_resources(atc, apcm);
904 if (err)
905 return err;
906
907 /* Connect resources */
908 src = apcm->src;
909 for (i = 0; i < apcm->n_amixer; i++) {
910 amixer = apcm->amixers[i];
911 amixer->ops->setup(amixer, &src->rsc, INIT_VOL, NULL);
912 src = src->ops->next_interleave(src);
913 if (NULL == src)
914 src = apcm->src;
915 }
916 /* Connect to SPDIFOO */
917 spin_lock_irqsave(&atc->atc_lock, flags);
918 dao = container_of(atc->daios[SPDIFOO], struct dao, daio);
919 amixer = apcm->amixers[0];
920 dao->ops->set_left_input(dao, &amixer->rsc);
921 amixer = apcm->amixers[1];
922 dao->ops->set_right_input(dao, &amixer->rsc);
923 spin_unlock_irqrestore(&atc->atc_lock, flags);
924
Takashi Iwaib7bbf872009-06-05 16:11:07 +0200925 ct_timer_prepare(apcm->timer);
926
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200927 return 0;
928}
929
930static int atc_select_line_in(struct ct_atc *atc)
931{
932 struct hw *hw = atc->hw;
933 struct ct_mixer *mixer = atc->mixer;
Takashi Iwai514eef92009-06-08 14:57:57 +0200934 struct src *src;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200935
936 if (hw->is_adc_source_selected(hw, ADC_LINEIN))
937 return 0;
938
939 mixer->set_input_left(mixer, MIX_MIC_IN, NULL);
940 mixer->set_input_right(mixer, MIX_MIC_IN, NULL);
941
942 hw->select_adc_source(hw, ADC_LINEIN);
943
944 src = atc->srcs[2];
945 mixer->set_input_left(mixer, MIX_LINE_IN, &src->rsc);
946 src = atc->srcs[3];
947 mixer->set_input_right(mixer, MIX_LINE_IN, &src->rsc);
948
949 return 0;
950}
951
952static int atc_select_mic_in(struct ct_atc *atc)
953{
954 struct hw *hw = atc->hw;
955 struct ct_mixer *mixer = atc->mixer;
Takashi Iwai514eef92009-06-08 14:57:57 +0200956 struct src *src;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200957
958 if (hw->is_adc_source_selected(hw, ADC_MICIN))
959 return 0;
960
961 mixer->set_input_left(mixer, MIX_LINE_IN, NULL);
962 mixer->set_input_right(mixer, MIX_LINE_IN, NULL);
963
964 hw->select_adc_source(hw, ADC_MICIN);
965
966 src = atc->srcs[2];
967 mixer->set_input_left(mixer, MIX_MIC_IN, &src->rsc);
968 src = atc->srcs[3];
969 mixer->set_input_right(mixer, MIX_MIC_IN, &src->rsc);
970
971 return 0;
972}
973
974static int atc_have_digit_io_switch(struct ct_atc *atc)
975{
976 struct hw *hw = atc->hw;
977
978 return hw->have_digit_io_switch(hw);
979}
980
981static int atc_select_digit_io(struct ct_atc *atc)
982{
983 struct hw *hw = atc->hw;
984
985 if (hw->is_adc_source_selected(hw, ADC_NONE))
986 return 0;
987
988 hw->select_adc_source(hw, ADC_NONE);
989
990 return 0;
991}
992
993static int atc_daio_unmute(struct ct_atc *atc, unsigned char state, int type)
994{
995 struct daio_mgr *daio_mgr = atc->rsc_mgrs[DAIO];
996
997 if (state)
998 daio_mgr->daio_enable(daio_mgr, atc->daios[type]);
999 else
1000 daio_mgr->daio_disable(daio_mgr, atc->daios[type]);
1001
1002 daio_mgr->commit_write(daio_mgr);
1003
1004 return 0;
1005}
1006
1007static int
1008atc_dao_get_status(struct ct_atc *atc, unsigned int *status, int type)
1009{
1010 struct dao *dao = container_of(atc->daios[type], struct dao, daio);
1011 return dao->ops->get_spos(dao, status);
1012}
1013
1014static int
1015atc_dao_set_status(struct ct_atc *atc, unsigned int status, int type)
1016{
1017 struct dao *dao = container_of(atc->daios[type], struct dao, daio);
1018
1019 dao->ops->set_spos(dao, status);
1020 dao->ops->commit_write(dao);
1021 return 0;
1022}
1023
1024static int atc_line_front_unmute(struct ct_atc *atc, unsigned char state)
1025{
1026 return atc_daio_unmute(atc, state, LINEO1);
1027}
1028
1029static int atc_line_surround_unmute(struct ct_atc *atc, unsigned char state)
1030{
1031 return atc_daio_unmute(atc, state, LINEO4);
1032}
1033
1034static int atc_line_clfe_unmute(struct ct_atc *atc, unsigned char state)
1035{
1036 return atc_daio_unmute(atc, state, LINEO3);
1037}
1038
1039static int atc_line_rear_unmute(struct ct_atc *atc, unsigned char state)
1040{
1041 return atc_daio_unmute(atc, state, LINEO2);
1042}
1043
1044static int atc_line_in_unmute(struct ct_atc *atc, unsigned char state)
1045{
1046 return atc_daio_unmute(atc, state, LINEIM);
1047}
1048
1049static int atc_spdif_out_unmute(struct ct_atc *atc, unsigned char state)
1050{
1051 return atc_daio_unmute(atc, state, SPDIFOO);
1052}
1053
1054static int atc_spdif_in_unmute(struct ct_atc *atc, unsigned char state)
1055{
1056 return atc_daio_unmute(atc, state, SPDIFIO);
1057}
1058
1059static int atc_spdif_out_get_status(struct ct_atc *atc, unsigned int *status)
1060{
1061 return atc_dao_get_status(atc, status, SPDIFOO);
1062}
1063
1064static int atc_spdif_out_set_status(struct ct_atc *atc, unsigned int status)
1065{
1066 return atc_dao_set_status(atc, status, SPDIFOO);
1067}
1068
1069static int atc_spdif_out_passthru(struct ct_atc *atc, unsigned char state)
1070{
1071 unsigned long flags;
1072 struct dao_desc da_dsc = {0};
Takashi Iwai514eef92009-06-08 14:57:57 +02001073 struct dao *dao;
1074 int err;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001075 struct ct_mixer *mixer = atc->mixer;
1076 struct rsc *rscs[2] = {NULL};
1077 unsigned int spos = 0;
1078
1079 spin_lock_irqsave(&atc->atc_lock, flags);
1080 dao = container_of(atc->daios[SPDIFOO], struct dao, daio);
1081 da_dsc.msr = state ? 1 : atc->msr;
1082 da_dsc.passthru = state ? 1 : 0;
1083 err = dao->ops->reinit(dao, &da_dsc);
1084 if (state) {
1085 spos = IEC958_DEFAULT_CON;
1086 } else {
1087 mixer->get_output_ports(mixer, MIX_SPDIF_OUT,
1088 &rscs[0], &rscs[1]);
1089 dao->ops->set_left_input(dao, rscs[0]);
1090 dao->ops->set_right_input(dao, rscs[1]);
1091 /* Restore PLL to atc->rsr if needed. */
Takashi Iwai514eef92009-06-08 14:57:57 +02001092 if (atc->pll_rate != atc->rsr)
1093 err = atc_pll_init(atc, atc->rsr);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001094 }
1095 dao->ops->set_spos(dao, spos);
1096 dao->ops->commit_write(dao);
1097 spin_unlock_irqrestore(&atc->atc_lock, flags);
1098
1099 return err;
1100}
1101
1102static int ct_atc_destroy(struct ct_atc *atc)
1103{
Takashi Iwai514eef92009-06-08 14:57:57 +02001104 struct daio_mgr *daio_mgr;
1105 struct dao *dao;
1106 struct dai *dai;
1107 struct daio *daio;
1108 struct sum_mgr *sum_mgr;
1109 struct src_mgr *src_mgr;
1110 struct srcimp_mgr *srcimp_mgr;
1111 struct srcimp *srcimp;
1112 struct ct_mixer *mixer;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001113 int i = 0;
1114
1115 if (NULL == atc)
1116 return 0;
1117
Takashi Iwaib7bbf872009-06-05 16:11:07 +02001118 if (atc->timer) {
1119 ct_timer_free(atc->timer);
1120 atc->timer = NULL;
1121 }
1122
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001123 /* Stop hardware and disable all interrupts */
1124 if (NULL != atc->hw)
1125 ((struct hw *)atc->hw)->card_stop(atc->hw);
1126
1127 /* Destroy internal mixer objects */
1128 if (NULL != atc->mixer) {
1129 mixer = atc->mixer;
1130 mixer->set_input_left(mixer, MIX_LINE_IN, NULL);
1131 mixer->set_input_right(mixer, MIX_LINE_IN, NULL);
1132 mixer->set_input_left(mixer, MIX_MIC_IN, NULL);
1133 mixer->set_input_right(mixer, MIX_MIC_IN, NULL);
1134 mixer->set_input_left(mixer, MIX_SPDIF_IN, NULL);
1135 mixer->set_input_right(mixer, MIX_SPDIF_IN, NULL);
1136 ct_mixer_destroy(atc->mixer);
1137 }
1138
1139 if (NULL != atc->daios) {
1140 daio_mgr = (struct daio_mgr *)atc->rsc_mgrs[DAIO];
1141 for (i = 0; i < atc->n_daio; i++) {
1142 daio = atc->daios[i];
1143 if (daio->type < LINEIM) {
1144 dao = container_of(daio, struct dao, daio);
1145 dao->ops->clear_left_input(dao);
1146 dao->ops->clear_right_input(dao);
1147 } else {
1148 dai = container_of(daio, struct dai, daio);
1149 /* some thing to do for dai ... */
1150 }
1151 daio_mgr->put_daio(daio_mgr, daio);
1152 }
1153 kfree(atc->daios);
1154 }
1155
1156 if (NULL != atc->pcm) {
1157 sum_mgr = atc->rsc_mgrs[SUM];
1158 for (i = 0; i < atc->n_pcm; i++)
1159 sum_mgr->put_sum(sum_mgr, atc->pcm[i]);
1160
1161 kfree(atc->pcm);
1162 }
1163
1164 if (NULL != atc->srcs) {
1165 src_mgr = atc->rsc_mgrs[SRC];
1166 for (i = 0; i < atc->n_src; i++)
1167 src_mgr->put_src(src_mgr, atc->srcs[i]);
1168
1169 kfree(atc->srcs);
1170 }
1171
1172 if (NULL != atc->srcimps) {
1173 srcimp_mgr = atc->rsc_mgrs[SRCIMP];
1174 for (i = 0; i < atc->n_srcimp; i++) {
1175 srcimp = atc->srcimps[i];
1176 srcimp->ops->unmap(srcimp);
1177 srcimp_mgr->put_srcimp(srcimp_mgr, atc->srcimps[i]);
1178 }
1179 kfree(atc->srcimps);
1180 }
1181
1182 for (i = 0; i < NUM_RSCTYP; i++) {
1183 if ((NULL != rsc_mgr_funcs[i].destroy) &&
1184 (NULL != atc->rsc_mgrs[i]))
1185 rsc_mgr_funcs[i].destroy(atc->rsc_mgrs[i]);
1186
1187 }
1188
1189 if (NULL != atc->hw)
1190 destroy_hw_obj((struct hw *)atc->hw);
1191
1192 /* Destroy device virtual memory manager object */
1193 if (NULL != atc->vm) {
1194 ct_vm_destroy(atc->vm);
1195 atc->vm = NULL;
1196 }
1197
1198 kfree(atc);
1199
1200 return 0;
1201}
1202
1203static int atc_dev_free(struct snd_device *dev)
1204{
1205 struct ct_atc *atc = dev->device_data;
1206 return ct_atc_destroy(atc);
1207}
1208
Takashi Iwai2a36f672009-06-05 16:34:10 +02001209static int __devinit atc_identify_card(struct ct_atc *atc)
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001210{
Takashi Iwai35b053b2009-05-14 15:17:16 +02001211 u16 subsys;
1212 u8 revision;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001213 struct pci_dev *pci = atc->pci;
1214 const struct ct_atc_chip_details *d;
1215 enum CTCARDS i;
1216
Takashi Iwai35b053b2009-05-14 15:17:16 +02001217 subsys = pci->subsystem_device;
1218 revision = pci->revision;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001219 atc->chip_details = NULL;
1220 atc->model = NUM_CTCARDS;
1221 for (d = atc_chip_details; d->vendor; d++) {
1222 if (d->vendor != pci->vendor || d->device != pci->device)
1223 continue;
1224
1225 if (NULL == d->sub_details) {
1226 atc->chip_details = d;
1227 break;
1228 }
1229 for (i = 0; i < NUM_CTCARDS; i++) {
1230 if ((d->sub_details[i].subsys == subsys) ||
1231 (((subsys & 0x6000) == 0x6000) &&
1232 ((d->sub_details[i].subsys & 0x6000) == 0x6000))) {
1233 atc->model = i;
1234 break;
1235 }
1236 }
1237 if (i >= NUM_CTCARDS)
1238 continue;
1239
1240 atc->chip_details = d;
1241 break;
1242 /* not take revision into consideration now */
1243 }
1244 if (!d->vendor)
1245 return -ENOENT;
1246
1247 return 0;
1248}
1249
Takashi Iwai2a36f672009-06-05 16:34:10 +02001250int __devinit ct_atc_create_alsa_devs(struct ct_atc *atc)
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001251{
1252 enum CTALSADEVS i;
1253 struct hw *hw = atc->hw;
1254 int err;
1255
1256 switch (hw->get_chip_type(hw)) {
1257 case ATC20K1:
1258 alsa_dev_funcs[MIXER].public_name = "20K1";
1259 break;
1260 case ATC20K2:
1261 alsa_dev_funcs[MIXER].public_name = "20K2";
1262 break;
1263 default:
1264 alsa_dev_funcs[MIXER].public_name = "Unknown";
1265 break;
1266 }
1267
1268 for (i = 0; i < NUM_CTALSADEVS; i++) {
1269 if (NULL == alsa_dev_funcs[i].create)
1270 continue;
1271
1272 err = alsa_dev_funcs[i].create(atc, i,
1273 alsa_dev_funcs[i].public_name);
1274 if (err) {
Takashi Iwaib3e0afe2009-05-14 15:19:30 +02001275 printk(KERN_ERR "ctxfi: "
1276 "Creating alsa device %d failed!\n", i);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001277 return err;
1278 }
1279 }
1280
1281 return 0;
1282}
1283
Takashi Iwai2a36f672009-06-05 16:34:10 +02001284static int __devinit atc_create_hw_devs(struct ct_atc *atc)
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001285{
Takashi Iwai514eef92009-06-08 14:57:57 +02001286 struct hw *hw;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001287 struct card_conf info = {0};
Takashi Iwai514eef92009-06-08 14:57:57 +02001288 int i, err;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001289
1290 err = create_hw_obj(atc->pci, &hw);
1291 if (err) {
1292 printk(KERN_ERR "Failed to create hw obj!!!\n");
1293 return err;
1294 }
1295 atc->hw = hw;
1296
1297 /* Initialize card hardware. */
1298 info.rsr = atc->rsr;
1299 info.msr = atc->msr;
1300 info.vm_pgt_phys = atc_get_ptp_phys(atc, 0);
1301 err = hw->card_init(hw, &info);
1302 if (err < 0)
1303 return err;
1304
1305 for (i = 0; i < NUM_RSCTYP; i++) {
1306 if (NULL == rsc_mgr_funcs[i].create)
1307 continue;
1308
1309 err = rsc_mgr_funcs[i].create(atc->hw, &atc->rsc_mgrs[i]);
1310 if (err) {
Takashi Iwaib3e0afe2009-05-14 15:19:30 +02001311 printk(KERN_ERR "ctxfi: "
1312 "Failed to create rsc_mgr %d!!!\n", i);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001313 return err;
1314 }
1315 }
1316
1317 return 0;
1318}
1319
Takashi Iwai2a36f672009-06-05 16:34:10 +02001320static int __devinit atc_get_resources(struct ct_atc *atc)
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001321{
1322 struct daio_desc da_desc = {0};
Takashi Iwai514eef92009-06-08 14:57:57 +02001323 struct daio_mgr *daio_mgr;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001324 struct src_desc src_dsc = {0};
Takashi Iwai514eef92009-06-08 14:57:57 +02001325 struct src_mgr *src_mgr;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001326 struct srcimp_desc srcimp_dsc = {0};
Takashi Iwai514eef92009-06-08 14:57:57 +02001327 struct srcimp_mgr *srcimp_mgr;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001328 struct sum_desc sum_dsc = {0};
Takashi Iwai514eef92009-06-08 14:57:57 +02001329 struct sum_mgr *sum_mgr;
1330 int err, i;
Takashi Iwai35b053b2009-05-14 15:17:16 +02001331 unsigned short subsys_id;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001332
1333 atc->daios = kzalloc(sizeof(void *)*(DAIONUM), GFP_KERNEL);
1334 if (NULL == atc->daios)
1335 return -ENOMEM;
1336
1337 atc->srcs = kzalloc(sizeof(void *)*(2*2), GFP_KERNEL);
1338 if (NULL == atc->srcs)
1339 return -ENOMEM;
1340
1341 atc->srcimps = kzalloc(sizeof(void *)*(2*2), GFP_KERNEL);
1342 if (NULL == atc->srcimps)
1343 return -ENOMEM;
1344
1345 atc->pcm = kzalloc(sizeof(void *)*(2*4), GFP_KERNEL);
1346 if (NULL == atc->pcm)
1347 return -ENOMEM;
1348
1349 daio_mgr = (struct daio_mgr *)atc->rsc_mgrs[DAIO];
1350 da_desc.msr = atc->msr;
1351 for (i = 0, atc->n_daio = 0; i < DAIONUM-1; i++) {
1352 da_desc.type = i;
1353 err = daio_mgr->get_daio(daio_mgr, &da_desc,
1354 (struct daio **)&atc->daios[i]);
1355 if (err) {
Takashi Iwaib3e0afe2009-05-14 15:19:30 +02001356 printk(KERN_ERR "ctxfi: Failed to get DAIO "
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001357 "resource %d!!!\n", i);
1358 return err;
1359 }
1360 atc->n_daio++;
1361 }
Takashi Iwai35b053b2009-05-14 15:17:16 +02001362 subsys_id = atc->pci->subsystem_device;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001363 if ((subsys_id == 0x0029) || (subsys_id == 0x0031)) {
1364 /* SB073x cards */
1365 da_desc.type = SPDIFI1;
1366 } else {
1367 da_desc.type = SPDIFIO;
1368 }
1369 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
1421 err = ct_mixer_create(atc, (struct ct_mixer **)&atc->mixer);
1422 if (err) {
Takashi Iwaib3e0afe2009-05-14 15:19:30 +02001423 printk(KERN_ERR "ctxfi: Failed to create mixer obj!!!\n");
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001424 return err;
1425 }
1426
1427 return 0;
1428}
1429
Takashi Iwai2a36f672009-06-05 16:34:10 +02001430static void __devinit
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001431atc_connect_dai(struct src_mgr *src_mgr, struct dai *dai,
1432 struct src **srcs, struct srcimp **srcimps)
1433{
1434 struct rsc *rscs[2] = {NULL};
Takashi Iwai514eef92009-06-08 14:57:57 +02001435 struct src *src;
1436 struct srcimp *srcimp;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001437 int i = 0;
1438
1439 rscs[0] = &dai->daio.rscl;
1440 rscs[1] = &dai->daio.rscr;
1441 for (i = 0; i < 2; i++) {
1442 src = srcs[i];
1443 srcimp = srcimps[i];
1444 srcimp->ops->map(srcimp, src, rscs[i]);
1445 src_mgr->src_disable(src_mgr, src);
1446 }
1447
1448 src_mgr->commit_write(src_mgr); /* Actually disable SRCs */
1449
1450 src = srcs[0];
1451 src->ops->set_pm(src, 1);
1452 for (i = 0; i < 2; i++) {
1453 src = srcs[i];
1454 src->ops->set_state(src, SRC_STATE_RUN);
1455 src->ops->commit_write(src);
1456 src_mgr->src_enable_s(src_mgr, src);
1457 }
1458
1459 dai->ops->set_srt_srcl(dai, &(srcs[0]->rsc));
1460 dai->ops->set_srt_srcr(dai, &(srcs[1]->rsc));
1461
1462 dai->ops->set_enb_src(dai, 1);
1463 dai->ops->set_enb_srt(dai, 1);
1464 dai->ops->commit_write(dai);
1465
1466 src_mgr->commit_write(src_mgr); /* Synchronously enable SRCs */
1467}
1468
Takashi Iwai2a36f672009-06-05 16:34:10 +02001469static void __devinit atc_connect_resources(struct ct_atc *atc)
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001470{
Takashi Iwai514eef92009-06-08 14:57:57 +02001471 struct dai *dai;
1472 struct dao *dao;
1473 struct src *src;
1474 struct sum *sum;
1475 struct ct_mixer *mixer;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001476 struct rsc *rscs[2] = {NULL};
Takashi Iwai514eef92009-06-08 14:57:57 +02001477 int i, j;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001478
1479 mixer = atc->mixer;
1480
1481 for (i = MIX_WAVE_FRONT, j = LINEO1; i <= MIX_SPDIF_OUT; i++, j++) {
1482 mixer->get_output_ports(mixer, i, &rscs[0], &rscs[1]);
1483 dao = container_of(atc->daios[j], struct dao, daio);
1484 dao->ops->set_left_input(dao, rscs[0]);
1485 dao->ops->set_right_input(dao, rscs[1]);
1486 }
1487
1488 dai = container_of(atc->daios[LINEIM], struct dai, daio);
1489 atc_connect_dai(atc->rsc_mgrs[SRC], dai,
1490 (struct src **)&atc->srcs[2],
1491 (struct srcimp **)&atc->srcimps[2]);
1492 src = atc->srcs[2];
1493 mixer->set_input_left(mixer, MIX_LINE_IN, &src->rsc);
1494 src = atc->srcs[3];
1495 mixer->set_input_right(mixer, MIX_LINE_IN, &src->rsc);
1496
1497 dai = container_of(atc->daios[SPDIFIO], struct dai, daio);
1498 atc_connect_dai(atc->rsc_mgrs[SRC], dai,
1499 (struct src **)&atc->srcs[0],
1500 (struct srcimp **)&atc->srcimps[0]);
1501
1502 src = atc->srcs[0];
1503 mixer->set_input_left(mixer, MIX_SPDIF_IN, &src->rsc);
1504 src = atc->srcs[1];
1505 mixer->set_input_right(mixer, MIX_SPDIF_IN, &src->rsc);
1506
1507 for (i = MIX_PCMI_FRONT, j = 0; i <= MIX_PCMI_SURROUND; i++, j += 2) {
1508 sum = atc->pcm[j];
1509 mixer->set_input_left(mixer, i, &sum->rsc);
1510 sum = atc->pcm[j+1];
1511 mixer->set_input_right(mixer, i, &sum->rsc);
1512 }
1513}
1514
Takashi Iwai2a36f672009-06-05 16:34:10 +02001515static struct ct_atc atc_preset __devinitdata = {
1516 .map_audio_buffer = ct_map_audio_buffer,
1517 .unmap_audio_buffer = ct_unmap_audio_buffer,
1518 .pcm_playback_prepare = atc_pcm_playback_prepare,
1519 .pcm_release_resources = atc_pcm_release_resources,
1520 .pcm_playback_start = atc_pcm_playback_start,
1521 .pcm_playback_stop = atc_pcm_stop,
1522 .pcm_playback_position = atc_pcm_playback_position,
1523 .pcm_capture_prepare = atc_pcm_capture_prepare,
1524 .pcm_capture_start = atc_pcm_capture_start,
1525 .pcm_capture_stop = atc_pcm_stop,
1526 .pcm_capture_position = atc_pcm_capture_position,
1527 .spdif_passthru_playback_prepare = spdif_passthru_playback_prepare,
1528 .get_ptp_phys = atc_get_ptp_phys,
1529 .select_line_in = atc_select_line_in,
1530 .select_mic_in = atc_select_mic_in,
1531 .select_digit_io = atc_select_digit_io,
1532 .line_front_unmute = atc_line_front_unmute,
1533 .line_surround_unmute = atc_line_surround_unmute,
1534 .line_clfe_unmute = atc_line_clfe_unmute,
1535 .line_rear_unmute = atc_line_rear_unmute,
1536 .line_in_unmute = atc_line_in_unmute,
1537 .spdif_out_unmute = atc_spdif_out_unmute,
1538 .spdif_in_unmute = atc_spdif_in_unmute,
1539 .spdif_out_get_status = atc_spdif_out_get_status,
1540 .spdif_out_set_status = atc_spdif_out_set_status,
1541 .spdif_out_passthru = atc_spdif_out_passthru,
1542 .have_digit_io_switch = atc_have_digit_io_switch,
1543};
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001544
1545/**
1546 * ct_atc_create - create and initialize a hardware manager
1547 * @card: corresponding alsa card object
1548 * @pci: corresponding kernel pci device object
1549 * @ratc: return created object address in it
1550 *
1551 * Creates and initializes a hardware manager.
1552 *
1553 * Creates kmallocated ct_atc structure. Initializes hardware.
1554 * Returns 0 if suceeds, or negative error code if fails.
1555 */
1556
Takashi Iwai2a36f672009-06-05 16:34:10 +02001557int __devinit ct_atc_create(struct snd_card *card, struct pci_dev *pci,
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001558 unsigned int rsr, unsigned int msr, struct ct_atc **ratc)
1559{
Takashi Iwai514eef92009-06-08 14:57:57 +02001560 struct ct_atc *atc;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001561 static struct snd_device_ops ops = {
1562 .dev_free = atc_dev_free,
1563 };
Takashi Iwai514eef92009-06-08 14:57:57 +02001564 int err;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001565
1566 *ratc = NULL;
1567
1568 atc = kzalloc(sizeof(*atc), GFP_KERNEL);
1569 if (NULL == atc)
1570 return -ENOMEM;
1571
Takashi Iwai2a36f672009-06-05 16:34:10 +02001572 /* Set operations */
1573 *atc = atc_preset;
1574
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001575 atc->card = card;
1576 atc->pci = pci;
1577 atc->rsr = rsr;
1578 atc->msr = msr;
1579
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001580 spin_lock_init(&atc->atc_lock);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001581
1582 /* Find card model */
1583 err = atc_identify_card(atc);
1584 if (err < 0) {
1585 printk(KERN_ERR "ctatc: Card not recognised\n");
1586 goto error1;
1587 }
1588
1589 /* Set up device virtual memory management object */
1590 err = ct_vm_create(&atc->vm);
1591 if (err < 0)
1592 goto error1;
1593
1594 /* Create all atc hw devices */
1595 err = atc_create_hw_devs(atc);
1596 if (err < 0)
1597 goto error1;
1598
1599 /* Get resources */
1600 err = atc_get_resources(atc);
1601 if (err < 0)
1602 goto error1;
1603
1604 /* Build topology */
1605 atc_connect_resources(atc);
1606
Takashi Iwaib7bbf872009-06-05 16:11:07 +02001607 atc->timer = ct_timer_new(atc);
1608 if (!atc->timer)
1609 goto error1;
1610
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001611 err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, atc, &ops);
1612 if (err < 0)
1613 goto error1;
1614
1615 snd_card_set_dev(card, &pci->dev);
1616
1617 *ratc = atc;
1618 return 0;
1619
1620error1:
1621 ct_atc_destroy(atc);
Takashi Iwaib3e0afe2009-05-14 15:19:30 +02001622 printk(KERN_ERR "ctxfi: Something wrong!!!\n");
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001623 return err;
1624}