blob: 10b741977dd7b1610d130ea2e5d107ac4515fd67 [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{
193 unsigned int pitch = 0;
194 int b = 0;
195
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};
244 struct src *src = NULL;
245 struct amixer *amixer = NULL;
246 int err = 0;
247 int n_amixer = apcm->substream->runtime->channels, i = 0;
248 int device = apcm->substream->pcm->device;
249 unsigned int pitch = 0;
250 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];
327 struct srcimp *srcimp = NULL;
328 int i = 0;
329
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{
380 unsigned int max_cisz = 0;
381 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{
401 struct src *src = NULL;
402 int i = 0;
403
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;
429 u32 size = 0, max_cisz = 0;
430 int position = 0;
431
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{
452 unsigned int pitch = 0;
453
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};
497 struct src *src = NULL;
498 struct srcimp_desc srcimp_dsc = {0};
499 struct srcimp *srcimp = NULL;
500 struct amixer_desc mix_dsc = {0};
501 struct sum_desc sum_dsc = {0};
502 unsigned int pitch = 0;
503 int multi = 0, err = 0, i = 0;
504 int n_srcimp = 0, n_amixer = 0, n_srcc = 0, n_sum = 0;
505 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;
521 n_amixer += n_sum * 2 + n_srcc;
522 n_srcimp += n_srcc;
523 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{
636 struct src *src = NULL;
637 struct amixer *amixer = NULL;
638 struct srcimp *srcimp = NULL;
639 struct ct_mixer *mixer = atc->mixer;
640 struct sum *mono = NULL;
641 struct rsc *out_ports[8] = {NULL};
642 int err = 0, i = 0, j = 0, n_sum = 0, multi = 0;
643 unsigned int pitch = 0;
644 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{
717 struct src *src = NULL;
718 struct src_mgr *src_mgr = atc->rsc_mgrs[SRC];
719 int i = 0, multi = 0;
720
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};
779 struct src *src = NULL;
780 int err = 0;
781 int n_amixer = apcm->substream->runtime->channels, i = 0;
782 unsigned int pitch = 0, rsr = atc->pll_rate;
783
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
835static int
836spdif_passthru_playback_setup(struct ct_atc *atc, struct ct_atc_pcm *apcm)
837{
838 struct dao *dao = container_of(atc->daios[SPDIFOO], struct dao, daio);
839 unsigned long flags;
840 unsigned int rate = apcm->substream->runtime->rate;
841 unsigned int status = 0;
842 int err = 0;
843 unsigned char iec958_con_fs = 0;
844
845 switch (rate) {
846 case 48000:
847 iec958_con_fs = IEC958_AES3_CON_FS_48000;
848 break;
849 case 44100:
850 iec958_con_fs = IEC958_AES3_CON_FS_44100;
851 break;
852 case 32000:
853 iec958_con_fs = IEC958_AES3_CON_FS_32000;
854 break;
855 default:
856 return -ENOENT;
857 }
858
859 spin_lock_irqsave(&atc->atc_lock, flags);
860 dao->ops->get_spos(dao, &status);
861 if (((status >> 24) & IEC958_AES3_CON_FS) != iec958_con_fs) {
862 status &= ((~IEC958_AES3_CON_FS) << 24);
863 status |= (iec958_con_fs << 24);
864 dao->ops->set_spos(dao, status);
865 dao->ops->commit_write(dao);
866 }
867 if ((rate != atc->pll_rate) && (32000 != rate)) {
868 err = ((struct hw *)atc->hw)->pll_init(atc->hw, rate);
869 atc->pll_rate = err ? 0 : rate;
870 }
871 spin_unlock_irqrestore(&atc->atc_lock, flags);
872
873 return err;
874}
875
876static int
877spdif_passthru_playback_prepare(struct ct_atc *atc, struct ct_atc_pcm *apcm)
878{
879 struct src *src = NULL;
880 struct amixer *amixer = NULL;
881 struct dao *dao = NULL;
882 int err = 0;
883 int i = 0;
884 unsigned long flags;
885
886 if (NULL != apcm->src)
887 return 0;
888
889 /* Configure SPDIFOO and PLL to passthrough mode;
890 * determine pll_rate. */
891 err = spdif_passthru_playback_setup(atc, apcm);
892 if (err)
893 return err;
894
895 /* Get needed resources. */
896 err = spdif_passthru_playback_get_resources(atc, apcm);
897 if (err)
898 return err;
899
900 /* Connect resources */
901 src = apcm->src;
902 for (i = 0; i < apcm->n_amixer; i++) {
903 amixer = apcm->amixers[i];
904 amixer->ops->setup(amixer, &src->rsc, INIT_VOL, NULL);
905 src = src->ops->next_interleave(src);
906 if (NULL == src)
907 src = apcm->src;
908 }
909 /* Connect to SPDIFOO */
910 spin_lock_irqsave(&atc->atc_lock, flags);
911 dao = container_of(atc->daios[SPDIFOO], struct dao, daio);
912 amixer = apcm->amixers[0];
913 dao->ops->set_left_input(dao, &amixer->rsc);
914 amixer = apcm->amixers[1];
915 dao->ops->set_right_input(dao, &amixer->rsc);
916 spin_unlock_irqrestore(&atc->atc_lock, flags);
917
Takashi Iwaib7bbf872009-06-05 16:11:07 +0200918 ct_timer_prepare(apcm->timer);
919
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200920 return 0;
921}
922
923static int atc_select_line_in(struct ct_atc *atc)
924{
925 struct hw *hw = atc->hw;
926 struct ct_mixer *mixer = atc->mixer;
927 struct src *src = NULL;
928
929 if (hw->is_adc_source_selected(hw, ADC_LINEIN))
930 return 0;
931
932 mixer->set_input_left(mixer, MIX_MIC_IN, NULL);
933 mixer->set_input_right(mixer, MIX_MIC_IN, NULL);
934
935 hw->select_adc_source(hw, ADC_LINEIN);
936
937 src = atc->srcs[2];
938 mixer->set_input_left(mixer, MIX_LINE_IN, &src->rsc);
939 src = atc->srcs[3];
940 mixer->set_input_right(mixer, MIX_LINE_IN, &src->rsc);
941
942 return 0;
943}
944
945static int atc_select_mic_in(struct ct_atc *atc)
946{
947 struct hw *hw = atc->hw;
948 struct ct_mixer *mixer = atc->mixer;
949 struct src *src = NULL;
950
951 if (hw->is_adc_source_selected(hw, ADC_MICIN))
952 return 0;
953
954 mixer->set_input_left(mixer, MIX_LINE_IN, NULL);
955 mixer->set_input_right(mixer, MIX_LINE_IN, NULL);
956
957 hw->select_adc_source(hw, ADC_MICIN);
958
959 src = atc->srcs[2];
960 mixer->set_input_left(mixer, MIX_MIC_IN, &src->rsc);
961 src = atc->srcs[3];
962 mixer->set_input_right(mixer, MIX_MIC_IN, &src->rsc);
963
964 return 0;
965}
966
967static int atc_have_digit_io_switch(struct ct_atc *atc)
968{
969 struct hw *hw = atc->hw;
970
971 return hw->have_digit_io_switch(hw);
972}
973
974static int atc_select_digit_io(struct ct_atc *atc)
975{
976 struct hw *hw = atc->hw;
977
978 if (hw->is_adc_source_selected(hw, ADC_NONE))
979 return 0;
980
981 hw->select_adc_source(hw, ADC_NONE);
982
983 return 0;
984}
985
986static int atc_daio_unmute(struct ct_atc *atc, unsigned char state, int type)
987{
988 struct daio_mgr *daio_mgr = atc->rsc_mgrs[DAIO];
989
990 if (state)
991 daio_mgr->daio_enable(daio_mgr, atc->daios[type]);
992 else
993 daio_mgr->daio_disable(daio_mgr, atc->daios[type]);
994
995 daio_mgr->commit_write(daio_mgr);
996
997 return 0;
998}
999
1000static int
1001atc_dao_get_status(struct ct_atc *atc, unsigned int *status, int type)
1002{
1003 struct dao *dao = container_of(atc->daios[type], struct dao, daio);
1004 return dao->ops->get_spos(dao, status);
1005}
1006
1007static int
1008atc_dao_set_status(struct ct_atc *atc, unsigned int status, int type)
1009{
1010 struct dao *dao = container_of(atc->daios[type], struct dao, daio);
1011
1012 dao->ops->set_spos(dao, status);
1013 dao->ops->commit_write(dao);
1014 return 0;
1015}
1016
1017static int atc_line_front_unmute(struct ct_atc *atc, unsigned char state)
1018{
1019 return atc_daio_unmute(atc, state, LINEO1);
1020}
1021
1022static int atc_line_surround_unmute(struct ct_atc *atc, unsigned char state)
1023{
1024 return atc_daio_unmute(atc, state, LINEO4);
1025}
1026
1027static int atc_line_clfe_unmute(struct ct_atc *atc, unsigned char state)
1028{
1029 return atc_daio_unmute(atc, state, LINEO3);
1030}
1031
1032static int atc_line_rear_unmute(struct ct_atc *atc, unsigned char state)
1033{
1034 return atc_daio_unmute(atc, state, LINEO2);
1035}
1036
1037static int atc_line_in_unmute(struct ct_atc *atc, unsigned char state)
1038{
1039 return atc_daio_unmute(atc, state, LINEIM);
1040}
1041
1042static int atc_spdif_out_unmute(struct ct_atc *atc, unsigned char state)
1043{
1044 return atc_daio_unmute(atc, state, SPDIFOO);
1045}
1046
1047static int atc_spdif_in_unmute(struct ct_atc *atc, unsigned char state)
1048{
1049 return atc_daio_unmute(atc, state, SPDIFIO);
1050}
1051
1052static int atc_spdif_out_get_status(struct ct_atc *atc, unsigned int *status)
1053{
1054 return atc_dao_get_status(atc, status, SPDIFOO);
1055}
1056
1057static int atc_spdif_out_set_status(struct ct_atc *atc, unsigned int status)
1058{
1059 return atc_dao_set_status(atc, status, SPDIFOO);
1060}
1061
1062static int atc_spdif_out_passthru(struct ct_atc *atc, unsigned char state)
1063{
1064 unsigned long flags;
1065 struct dao_desc da_dsc = {0};
1066 struct dao *dao = NULL;
1067 int err = 0;
1068 struct ct_mixer *mixer = atc->mixer;
1069 struct rsc *rscs[2] = {NULL};
1070 unsigned int spos = 0;
1071
1072 spin_lock_irqsave(&atc->atc_lock, flags);
1073 dao = container_of(atc->daios[SPDIFOO], struct dao, daio);
1074 da_dsc.msr = state ? 1 : atc->msr;
1075 da_dsc.passthru = state ? 1 : 0;
1076 err = dao->ops->reinit(dao, &da_dsc);
1077 if (state) {
1078 spos = IEC958_DEFAULT_CON;
1079 } else {
1080 mixer->get_output_ports(mixer, MIX_SPDIF_OUT,
1081 &rscs[0], &rscs[1]);
1082 dao->ops->set_left_input(dao, rscs[0]);
1083 dao->ops->set_right_input(dao, rscs[1]);
1084 /* Restore PLL to atc->rsr if needed. */
1085 if (atc->pll_rate != atc->rsr) {
1086 err = ((struct hw *)atc->hw)->pll_init(atc->hw,
1087 atc->rsr);
1088 atc->pll_rate = err ? 0 : atc->rsr;
1089 }
1090 }
1091 dao->ops->set_spos(dao, spos);
1092 dao->ops->commit_write(dao);
1093 spin_unlock_irqrestore(&atc->atc_lock, flags);
1094
1095 return err;
1096}
1097
1098static int ct_atc_destroy(struct ct_atc *atc)
1099{
1100 struct daio_mgr *daio_mgr = NULL;
1101 struct dao *dao = NULL;
1102 struct dai *dai = NULL;
1103 struct daio *daio = NULL;
1104 struct sum_mgr *sum_mgr = NULL;
1105 struct src_mgr *src_mgr = NULL;
1106 struct srcimp_mgr *srcimp_mgr = NULL;
1107 struct srcimp *srcimp = NULL;
1108 struct ct_mixer *mixer = NULL;
1109 int i = 0;
1110
1111 if (NULL == atc)
1112 return 0;
1113
Takashi Iwaib7bbf872009-06-05 16:11:07 +02001114 if (atc->timer) {
1115 ct_timer_free(atc->timer);
1116 atc->timer = NULL;
1117 }
1118
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001119 /* Stop hardware and disable all interrupts */
1120 if (NULL != atc->hw)
1121 ((struct hw *)atc->hw)->card_stop(atc->hw);
1122
1123 /* Destroy internal mixer objects */
1124 if (NULL != atc->mixer) {
1125 mixer = atc->mixer;
1126 mixer->set_input_left(mixer, MIX_LINE_IN, NULL);
1127 mixer->set_input_right(mixer, MIX_LINE_IN, NULL);
1128 mixer->set_input_left(mixer, MIX_MIC_IN, NULL);
1129 mixer->set_input_right(mixer, MIX_MIC_IN, NULL);
1130 mixer->set_input_left(mixer, MIX_SPDIF_IN, NULL);
1131 mixer->set_input_right(mixer, MIX_SPDIF_IN, NULL);
1132 ct_mixer_destroy(atc->mixer);
1133 }
1134
1135 if (NULL != atc->daios) {
1136 daio_mgr = (struct daio_mgr *)atc->rsc_mgrs[DAIO];
1137 for (i = 0; i < atc->n_daio; i++) {
1138 daio = atc->daios[i];
1139 if (daio->type < LINEIM) {
1140 dao = container_of(daio, struct dao, daio);
1141 dao->ops->clear_left_input(dao);
1142 dao->ops->clear_right_input(dao);
1143 } else {
1144 dai = container_of(daio, struct dai, daio);
1145 /* some thing to do for dai ... */
1146 }
1147 daio_mgr->put_daio(daio_mgr, daio);
1148 }
1149 kfree(atc->daios);
1150 }
1151
1152 if (NULL != atc->pcm) {
1153 sum_mgr = atc->rsc_mgrs[SUM];
1154 for (i = 0; i < atc->n_pcm; i++)
1155 sum_mgr->put_sum(sum_mgr, atc->pcm[i]);
1156
1157 kfree(atc->pcm);
1158 }
1159
1160 if (NULL != atc->srcs) {
1161 src_mgr = atc->rsc_mgrs[SRC];
1162 for (i = 0; i < atc->n_src; i++)
1163 src_mgr->put_src(src_mgr, atc->srcs[i]);
1164
1165 kfree(atc->srcs);
1166 }
1167
1168 if (NULL != atc->srcimps) {
1169 srcimp_mgr = atc->rsc_mgrs[SRCIMP];
1170 for (i = 0; i < atc->n_srcimp; i++) {
1171 srcimp = atc->srcimps[i];
1172 srcimp->ops->unmap(srcimp);
1173 srcimp_mgr->put_srcimp(srcimp_mgr, atc->srcimps[i]);
1174 }
1175 kfree(atc->srcimps);
1176 }
1177
1178 for (i = 0; i < NUM_RSCTYP; i++) {
1179 if ((NULL != rsc_mgr_funcs[i].destroy) &&
1180 (NULL != atc->rsc_mgrs[i]))
1181 rsc_mgr_funcs[i].destroy(atc->rsc_mgrs[i]);
1182
1183 }
1184
1185 if (NULL != atc->hw)
1186 destroy_hw_obj((struct hw *)atc->hw);
1187
1188 /* Destroy device virtual memory manager object */
1189 if (NULL != atc->vm) {
1190 ct_vm_destroy(atc->vm);
1191 atc->vm = NULL;
1192 }
1193
1194 kfree(atc);
1195
1196 return 0;
1197}
1198
1199static int atc_dev_free(struct snd_device *dev)
1200{
1201 struct ct_atc *atc = dev->device_data;
1202 return ct_atc_destroy(atc);
1203}
1204
1205static int atc_identify_card(struct ct_atc *atc)
1206{
Takashi Iwai35b053b2009-05-14 15:17:16 +02001207 u16 subsys;
1208 u8 revision;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001209 struct pci_dev *pci = atc->pci;
1210 const struct ct_atc_chip_details *d;
1211 enum CTCARDS i;
1212
Takashi Iwai35b053b2009-05-14 15:17:16 +02001213 subsys = pci->subsystem_device;
1214 revision = pci->revision;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001215 atc->chip_details = NULL;
1216 atc->model = NUM_CTCARDS;
1217 for (d = atc_chip_details; d->vendor; d++) {
1218 if (d->vendor != pci->vendor || d->device != pci->device)
1219 continue;
1220
1221 if (NULL == d->sub_details) {
1222 atc->chip_details = d;
1223 break;
1224 }
1225 for (i = 0; i < NUM_CTCARDS; i++) {
1226 if ((d->sub_details[i].subsys == subsys) ||
1227 (((subsys & 0x6000) == 0x6000) &&
1228 ((d->sub_details[i].subsys & 0x6000) == 0x6000))) {
1229 atc->model = i;
1230 break;
1231 }
1232 }
1233 if (i >= NUM_CTCARDS)
1234 continue;
1235
1236 atc->chip_details = d;
1237 break;
1238 /* not take revision into consideration now */
1239 }
1240 if (!d->vendor)
1241 return -ENOENT;
1242
1243 return 0;
1244}
1245
1246static int ct_create_alsa_devs(struct ct_atc *atc)
1247{
1248 enum CTALSADEVS i;
1249 struct hw *hw = atc->hw;
1250 int err;
1251
1252 switch (hw->get_chip_type(hw)) {
1253 case ATC20K1:
1254 alsa_dev_funcs[MIXER].public_name = "20K1";
1255 break;
1256 case ATC20K2:
1257 alsa_dev_funcs[MIXER].public_name = "20K2";
1258 break;
1259 default:
1260 alsa_dev_funcs[MIXER].public_name = "Unknown";
1261 break;
1262 }
1263
1264 for (i = 0; i < NUM_CTALSADEVS; i++) {
1265 if (NULL == alsa_dev_funcs[i].create)
1266 continue;
1267
1268 err = alsa_dev_funcs[i].create(atc, i,
1269 alsa_dev_funcs[i].public_name);
1270 if (err) {
Takashi Iwaib3e0afe2009-05-14 15:19:30 +02001271 printk(KERN_ERR "ctxfi: "
1272 "Creating alsa device %d failed!\n", i);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001273 return err;
1274 }
1275 }
1276
1277 return 0;
1278}
1279
1280static int atc_create_hw_devs(struct ct_atc *atc)
1281{
1282 struct hw *hw = NULL;
1283 struct card_conf info = {0};
1284 int i = 0, err = 0;
1285
1286 err = create_hw_obj(atc->pci, &hw);
1287 if (err) {
1288 printk(KERN_ERR "Failed to create hw obj!!!\n");
1289 return err;
1290 }
1291 atc->hw = hw;
1292
1293 /* Initialize card hardware. */
1294 info.rsr = atc->rsr;
1295 info.msr = atc->msr;
1296 info.vm_pgt_phys = atc_get_ptp_phys(atc, 0);
1297 err = hw->card_init(hw, &info);
1298 if (err < 0)
1299 return err;
1300
1301 for (i = 0; i < NUM_RSCTYP; i++) {
1302 if (NULL == rsc_mgr_funcs[i].create)
1303 continue;
1304
1305 err = rsc_mgr_funcs[i].create(atc->hw, &atc->rsc_mgrs[i]);
1306 if (err) {
Takashi Iwaib3e0afe2009-05-14 15:19:30 +02001307 printk(KERN_ERR "ctxfi: "
1308 "Failed to create rsc_mgr %d!!!\n", i);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001309 return err;
1310 }
1311 }
1312
1313 return 0;
1314}
1315
1316static int atc_get_resources(struct ct_atc *atc)
1317{
1318 struct daio_desc da_desc = {0};
1319 struct daio_mgr *daio_mgr = NULL;
1320 struct src_desc src_dsc = {0};
1321 struct src_mgr *src_mgr = NULL;
1322 struct srcimp_desc srcimp_dsc = {0};
1323 struct srcimp_mgr *srcimp_mgr = NULL;
1324 struct sum_desc sum_dsc = {0};
1325 struct sum_mgr *sum_mgr = NULL;
1326 int err = 0, i = 0;
Takashi Iwai35b053b2009-05-14 15:17:16 +02001327 unsigned short subsys_id;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001328
1329 atc->daios = kzalloc(sizeof(void *)*(DAIONUM), GFP_KERNEL);
1330 if (NULL == atc->daios)
1331 return -ENOMEM;
1332
1333 atc->srcs = kzalloc(sizeof(void *)*(2*2), GFP_KERNEL);
1334 if (NULL == atc->srcs)
1335 return -ENOMEM;
1336
1337 atc->srcimps = kzalloc(sizeof(void *)*(2*2), GFP_KERNEL);
1338 if (NULL == atc->srcimps)
1339 return -ENOMEM;
1340
1341 atc->pcm = kzalloc(sizeof(void *)*(2*4), GFP_KERNEL);
1342 if (NULL == atc->pcm)
1343 return -ENOMEM;
1344
1345 daio_mgr = (struct daio_mgr *)atc->rsc_mgrs[DAIO];
1346 da_desc.msr = atc->msr;
1347 for (i = 0, atc->n_daio = 0; i < DAIONUM-1; i++) {
1348 da_desc.type = i;
1349 err = daio_mgr->get_daio(daio_mgr, &da_desc,
1350 (struct daio **)&atc->daios[i]);
1351 if (err) {
Takashi Iwaib3e0afe2009-05-14 15:19:30 +02001352 printk(KERN_ERR "ctxfi: Failed to get DAIO "
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001353 "resource %d!!!\n", i);
1354 return err;
1355 }
1356 atc->n_daio++;
1357 }
Takashi Iwai35b053b2009-05-14 15:17:16 +02001358 subsys_id = atc->pci->subsystem_device;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001359 if ((subsys_id == 0x0029) || (subsys_id == 0x0031)) {
1360 /* SB073x cards */
1361 da_desc.type = SPDIFI1;
1362 } else {
1363 da_desc.type = SPDIFIO;
1364 }
1365 err = daio_mgr->get_daio(daio_mgr, &da_desc,
1366 (struct daio **)&atc->daios[i]);
1367 if (err) {
Takashi Iwaib3e0afe2009-05-14 15:19:30 +02001368 printk(KERN_ERR "ctxfi: Failed to get S/PDIF-in resource!!!\n");
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001369 return err;
1370 }
1371 atc->n_daio++;
1372
1373 src_mgr = atc->rsc_mgrs[SRC];
1374 src_dsc.multi = 1;
1375 src_dsc.msr = atc->msr;
1376 src_dsc.mode = ARCRW;
1377 for (i = 0, atc->n_src = 0; i < (2*2); i++) {
1378 err = src_mgr->get_src(src_mgr, &src_dsc,
1379 (struct src **)&atc->srcs[i]);
1380 if (err)
1381 return err;
1382
1383 atc->n_src++;
1384 }
1385
1386 srcimp_mgr = atc->rsc_mgrs[SRCIMP];
1387 srcimp_dsc.msr = 8; /* SRCIMPs for S/PDIFIn SRT */
1388 for (i = 0, atc->n_srcimp = 0; i < (2*1); i++) {
1389 err = srcimp_mgr->get_srcimp(srcimp_mgr, &srcimp_dsc,
1390 (struct srcimp **)&atc->srcimps[i]);
1391 if (err)
1392 return err;
1393
1394 atc->n_srcimp++;
1395 }
1396 srcimp_dsc.msr = 8; /* SRCIMPs for LINE/MICIn SRT */
1397 for (i = 0; i < (2*1); i++) {
1398 err = srcimp_mgr->get_srcimp(srcimp_mgr, &srcimp_dsc,
1399 (struct srcimp **)&atc->srcimps[2*1+i]);
1400 if (err)
1401 return err;
1402
1403 atc->n_srcimp++;
1404 }
1405
1406 sum_mgr = atc->rsc_mgrs[SUM];
1407 sum_dsc.msr = atc->msr;
1408 for (i = 0, atc->n_pcm = 0; i < (2*4); i++) {
1409 err = sum_mgr->get_sum(sum_mgr, &sum_dsc,
1410 (struct sum **)&atc->pcm[i]);
1411 if (err)
1412 return err;
1413
1414 atc->n_pcm++;
1415 }
1416
1417 err = ct_mixer_create(atc, (struct ct_mixer **)&atc->mixer);
1418 if (err) {
Takashi Iwaib3e0afe2009-05-14 15:19:30 +02001419 printk(KERN_ERR "ctxfi: Failed to create mixer obj!!!\n");
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001420 return err;
1421 }
1422
1423 return 0;
1424}
1425
1426static void
1427atc_connect_dai(struct src_mgr *src_mgr, struct dai *dai,
1428 struct src **srcs, struct srcimp **srcimps)
1429{
1430 struct rsc *rscs[2] = {NULL};
1431 struct src *src = NULL;
1432 struct srcimp *srcimp = NULL;
1433 int i = 0;
1434
1435 rscs[0] = &dai->daio.rscl;
1436 rscs[1] = &dai->daio.rscr;
1437 for (i = 0; i < 2; i++) {
1438 src = srcs[i];
1439 srcimp = srcimps[i];
1440 srcimp->ops->map(srcimp, src, rscs[i]);
1441 src_mgr->src_disable(src_mgr, src);
1442 }
1443
1444 src_mgr->commit_write(src_mgr); /* Actually disable SRCs */
1445
1446 src = srcs[0];
1447 src->ops->set_pm(src, 1);
1448 for (i = 0; i < 2; i++) {
1449 src = srcs[i];
1450 src->ops->set_state(src, SRC_STATE_RUN);
1451 src->ops->commit_write(src);
1452 src_mgr->src_enable_s(src_mgr, src);
1453 }
1454
1455 dai->ops->set_srt_srcl(dai, &(srcs[0]->rsc));
1456 dai->ops->set_srt_srcr(dai, &(srcs[1]->rsc));
1457
1458 dai->ops->set_enb_src(dai, 1);
1459 dai->ops->set_enb_srt(dai, 1);
1460 dai->ops->commit_write(dai);
1461
1462 src_mgr->commit_write(src_mgr); /* Synchronously enable SRCs */
1463}
1464
1465static void atc_connect_resources(struct ct_atc *atc)
1466{
1467 struct dai *dai = NULL;
1468 struct dao *dao = NULL;
1469 struct src *src = NULL;
1470 struct sum *sum = NULL;
1471 struct ct_mixer *mixer = NULL;
1472 struct rsc *rscs[2] = {NULL};
1473 int i = 0, j = 0;
1474
1475 mixer = atc->mixer;
1476
1477 for (i = MIX_WAVE_FRONT, j = LINEO1; i <= MIX_SPDIF_OUT; i++, j++) {
1478 mixer->get_output_ports(mixer, i, &rscs[0], &rscs[1]);
1479 dao = container_of(atc->daios[j], struct dao, daio);
1480 dao->ops->set_left_input(dao, rscs[0]);
1481 dao->ops->set_right_input(dao, rscs[1]);
1482 }
1483
1484 dai = container_of(atc->daios[LINEIM], struct dai, daio);
1485 atc_connect_dai(atc->rsc_mgrs[SRC], dai,
1486 (struct src **)&atc->srcs[2],
1487 (struct srcimp **)&atc->srcimps[2]);
1488 src = atc->srcs[2];
1489 mixer->set_input_left(mixer, MIX_LINE_IN, &src->rsc);
1490 src = atc->srcs[3];
1491 mixer->set_input_right(mixer, MIX_LINE_IN, &src->rsc);
1492
1493 dai = container_of(atc->daios[SPDIFIO], struct dai, daio);
1494 atc_connect_dai(atc->rsc_mgrs[SRC], dai,
1495 (struct src **)&atc->srcs[0],
1496 (struct srcimp **)&atc->srcimps[0]);
1497
1498 src = atc->srcs[0];
1499 mixer->set_input_left(mixer, MIX_SPDIF_IN, &src->rsc);
1500 src = atc->srcs[1];
1501 mixer->set_input_right(mixer, MIX_SPDIF_IN, &src->rsc);
1502
1503 for (i = MIX_PCMI_FRONT, j = 0; i <= MIX_PCMI_SURROUND; i++, j += 2) {
1504 sum = atc->pcm[j];
1505 mixer->set_input_left(mixer, i, &sum->rsc);
1506 sum = atc->pcm[j+1];
1507 mixer->set_input_right(mixer, i, &sum->rsc);
1508 }
1509}
1510
1511static void atc_set_ops(struct ct_atc *atc)
1512{
1513 /* Set operations */
1514 atc->map_audio_buffer = ct_map_audio_buffer;
1515 atc->unmap_audio_buffer = ct_unmap_audio_buffer;
1516 atc->pcm_playback_prepare = atc_pcm_playback_prepare;
1517 atc->pcm_release_resources = atc_pcm_release_resources;
1518 atc->pcm_playback_start = atc_pcm_playback_start;
1519 atc->pcm_playback_stop = atc_pcm_stop;
1520 atc->pcm_playback_position = atc_pcm_playback_position;
1521 atc->pcm_capture_prepare = atc_pcm_capture_prepare;
1522 atc->pcm_capture_start = atc_pcm_capture_start;
1523 atc->pcm_capture_stop = atc_pcm_stop;
1524 atc->pcm_capture_position = atc_pcm_capture_position;
1525 atc->spdif_passthru_playback_prepare = spdif_passthru_playback_prepare;
1526 atc->get_ptp_phys = atc_get_ptp_phys;
1527 atc->select_line_in = atc_select_line_in;
1528 atc->select_mic_in = atc_select_mic_in;
1529 atc->select_digit_io = atc_select_digit_io;
1530 atc->line_front_unmute = atc_line_front_unmute;
1531 atc->line_surround_unmute = atc_line_surround_unmute;
1532 atc->line_clfe_unmute = atc_line_clfe_unmute;
1533 atc->line_rear_unmute = atc_line_rear_unmute;
1534 atc->line_in_unmute = atc_line_in_unmute;
1535 atc->spdif_out_unmute = atc_spdif_out_unmute;
1536 atc->spdif_in_unmute = atc_spdif_in_unmute;
1537 atc->spdif_out_get_status = atc_spdif_out_get_status;
1538 atc->spdif_out_set_status = atc_spdif_out_set_status;
1539 atc->spdif_out_passthru = atc_spdif_out_passthru;
1540 atc->have_digit_io_switch = atc_have_digit_io_switch;
1541}
1542
1543/**
1544 * ct_atc_create - create and initialize a hardware manager
1545 * @card: corresponding alsa card object
1546 * @pci: corresponding kernel pci device object
1547 * @ratc: return created object address in it
1548 *
1549 * Creates and initializes a hardware manager.
1550 *
1551 * Creates kmallocated ct_atc structure. Initializes hardware.
1552 * Returns 0 if suceeds, or negative error code if fails.
1553 */
1554
1555int ct_atc_create(struct snd_card *card, struct pci_dev *pci,
1556 unsigned int rsr, unsigned int msr, struct ct_atc **ratc)
1557{
1558 struct ct_atc *atc = NULL;
1559 static struct snd_device_ops ops = {
1560 .dev_free = atc_dev_free,
1561 };
1562 int err = 0;
1563
1564 *ratc = NULL;
1565
1566 atc = kzalloc(sizeof(*atc), GFP_KERNEL);
1567 if (NULL == atc)
1568 return -ENOMEM;
1569
1570 atc->card = card;
1571 atc->pci = pci;
1572 atc->rsr = rsr;
1573 atc->msr = msr;
1574
1575 /* Set operations */
1576 atc_set_ops(atc);
1577
1578 spin_lock_init(&atc->atc_lock);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001579
1580 /* Find card model */
1581 err = atc_identify_card(atc);
1582 if (err < 0) {
1583 printk(KERN_ERR "ctatc: Card not recognised\n");
1584 goto error1;
1585 }
1586
1587 /* Set up device virtual memory management object */
1588 err = ct_vm_create(&atc->vm);
1589 if (err < 0)
1590 goto error1;
1591
1592 /* Create all atc hw devices */
1593 err = atc_create_hw_devs(atc);
1594 if (err < 0)
1595 goto error1;
1596
1597 /* Get resources */
1598 err = atc_get_resources(atc);
1599 if (err < 0)
1600 goto error1;
1601
1602 /* Build topology */
1603 atc_connect_resources(atc);
1604
Takashi Iwaib7bbf872009-06-05 16:11:07 +02001605 atc->timer = ct_timer_new(atc);
1606 if (!atc->timer)
1607 goto error1;
1608
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001609 atc->create_alsa_devs = ct_create_alsa_devs;
1610
1611 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}