blob: 675dd4c8eaecb5188b07f67091276b2e90ff5f9f [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"
25#include <linux/delay.h>
26#include <sound/pcm.h>
27#include <sound/control.h>
28#include <sound/asoundef.h>
29
30#define MONO_SUM_SCALE 0x19a8 /* 2^(-0.5) in 14-bit floating format */
31#define DAIONUM 7
32#define MAX_MULTI_CHN 8
33
34#define IEC958_DEFAULT_CON ((IEC958_AES0_NONAUDIO \
35 | IEC958_AES0_CON_NOT_COPYRIGHT) \
36 | ((IEC958_AES1_CON_MIXER \
37 | IEC958_AES1_CON_ORIGINAL) << 8) \
38 | (0x10 << 16) \
39 | ((IEC958_AES3_CON_FS_48000) << 24))
40
41static const struct ct_atc_chip_sub_details atc_sub_details[NUM_CTCARDS] = {
Takashi Iwai9fc20f02009-05-14 15:14:18 +020042 [CTSB0760] = {.subsys = PCI_SUBDEVICE_ID_CREATIVE_SB0760,
Wai Yew CHAY8cc72362009-05-14 08:05:58 +020043 .nm_model = "SB076x"},
Takashi Iwai9fc20f02009-05-14 15:14:18 +020044 [CTHENDRIX] = {.subsys = PCI_SUBDEVICE_ID_CREATIVE_HENDRIX,
Wai Yew CHAY8cc72362009-05-14 08:05:58 +020045 .nm_model = "Hendrix"},
Takashi Iwai9fc20f02009-05-14 15:14:18 +020046 [CTSB08801] = {.subsys = PCI_SUBDEVICE_ID_CREATIVE_SB08801,
Wai Yew CHAY8cc72362009-05-14 08:05:58 +020047 .nm_model = "SB0880"},
Takashi Iwai9fc20f02009-05-14 15:14:18 +020048 [CTSB08802] = {.subsys = PCI_SUBDEVICE_ID_CREATIVE_SB08802,
Wai Yew CHAY8cc72362009-05-14 08:05:58 +020049 .nm_model = "SB0880"},
Takashi Iwai9fc20f02009-05-14 15:14:18 +020050 [CTSB08803] = {.subsys = PCI_SUBDEVICE_ID_CREATIVE_SB08803,
Wai Yew CHAY8cc72362009-05-14 08:05:58 +020051 .nm_model = "SB0880"}
52};
53
54static struct ct_atc_chip_details atc_chip_details[] = {
Takashi Iwai9fc20f02009-05-14 15:14:18 +020055 {.vendor = PCI_VENDOR_ID_CREATIVE,
56 .device = PCI_DEVICE_ID_CREATIVE_20K1,
Wai Yew CHAY8cc72362009-05-14 08:05:58 +020057 .sub_details = NULL,
58 .nm_card = "X-Fi 20k1"},
Takashi Iwai9fc20f02009-05-14 15:14:18 +020059 {.vendor = PCI_VENDOR_ID_CREATIVE,
60 .device = PCI_DEVICE_ID_CREATIVE_20K2,
Wai Yew CHAY8cc72362009-05-14 08:05:58 +020061 .sub_details = atc_sub_details,
62 .nm_card = "X-Fi 20k2"},
63 {} /* terminator */
64};
65
66static struct {
67 int (*create)(struct ct_atc *atc,
68 enum CTALSADEVS device, const char *device_name);
69 int (*destroy)(void *alsa_dev);
70 const char *public_name;
71} alsa_dev_funcs[NUM_CTALSADEVS] = {
72 [FRONT] = { .create = ct_alsa_pcm_create,
73 .destroy = NULL,
74 .public_name = "Front/WaveIn"},
Wai Yew CHAY8cc72362009-05-14 08:05:58 +020075 [SURROUND] = { .create = ct_alsa_pcm_create,
76 .destroy = NULL,
77 .public_name = "Surround"},
Takashi Iwai8372d4982009-06-02 14:27:56 +020078 [CLFE] = { .create = ct_alsa_pcm_create,
79 .destroy = NULL,
80 .public_name = "Center/LFE"},
81 [SIDE] = { .create = ct_alsa_pcm_create,
82 .destroy = NULL,
83 .public_name = "Side"},
Wai Yew CHAY8cc72362009-05-14 08:05:58 +020084 [IEC958] = { .create = ct_alsa_pcm_create,
85 .destroy = NULL,
86 .public_name = "IEC958 Non-audio"},
87
88 [MIXER] = { .create = ct_alsa_mix_create,
89 .destroy = NULL,
90 .public_name = "Mixer"}
91};
92
93typedef int (*create_t)(void *, void **);
94typedef int (*destroy_t)(void *);
95
96static struct {
97 int (*create)(void *hw, void **rmgr);
98 int (*destroy)(void *mgr);
99} rsc_mgr_funcs[NUM_RSCTYP] = {
100 [SRC] = { .create = (create_t)src_mgr_create,
101 .destroy = (destroy_t)src_mgr_destroy },
102 [SRCIMP] = { .create = (create_t)srcimp_mgr_create,
103 .destroy = (destroy_t)srcimp_mgr_destroy },
104 [AMIXER] = { .create = (create_t)amixer_mgr_create,
105 .destroy = (destroy_t)amixer_mgr_destroy },
106 [SUM] = { .create = (create_t)sum_mgr_create,
107 .destroy = (destroy_t)sum_mgr_destroy },
108 [DAIO] = { .create = (create_t)daio_mgr_create,
109 .destroy = (destroy_t)daio_mgr_destroy }
110};
111
112static int
113atc_pcm_release_resources(struct ct_atc *atc, struct ct_atc_pcm *apcm);
114
115/* *
116 * Only mono and interleaved modes are supported now.
117 * Always allocates a contiguous channel block.
118 * */
119
120static int ct_map_audio_buffer(struct ct_atc *atc, struct ct_atc_pcm *apcm)
121{
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200122 struct snd_pcm_runtime *runtime;
123 struct ct_vm *vm;
124
125 if (NULL == apcm->substream)
126 return 0;
127
128 runtime = apcm->substream->runtime;
129 vm = atc->vm;
130
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200131 apcm->vm_block = vm->map(vm, runtime->dma_area, runtime->dma_bytes);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200132
133 if (NULL == apcm->vm_block)
134 return -ENOENT;
135
136 return 0;
137}
138
139static void ct_unmap_audio_buffer(struct ct_atc *atc, struct ct_atc_pcm *apcm)
140{
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200141 struct ct_vm *vm;
142
143 if (NULL == apcm->vm_block)
144 return;
145
146 vm = atc->vm;
147
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200148 vm->unmap(vm, apcm->vm_block);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200149
150 apcm->vm_block = NULL;
151}
152
153static unsigned long atc_get_ptp_phys(struct ct_atc *atc, int index)
154{
155 struct ct_vm *vm;
156 void *kvirt_addr;
157 unsigned long phys_addr;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200158
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200159 vm = atc->vm;
160 kvirt_addr = vm->get_ptp_virt(vm, index);
161 if (kvirt_addr == NULL)
162 phys_addr = (~0UL);
163 else
164 phys_addr = virt_to_phys(kvirt_addr);
165
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200166 return phys_addr;
167}
168
169static unsigned int convert_format(snd_pcm_format_t snd_format)
170{
171 switch (snd_format) {
172 case SNDRV_PCM_FORMAT_U8:
173 case SNDRV_PCM_FORMAT_S8:
174 return SRC_SF_U8;
175 case SNDRV_PCM_FORMAT_S16_LE:
176 case SNDRV_PCM_FORMAT_U16_LE:
177 return SRC_SF_S16;
178 case SNDRV_PCM_FORMAT_S24_3LE:
179 return SRC_SF_S24;
180 case SNDRV_PCM_FORMAT_S24_LE:
181 case SNDRV_PCM_FORMAT_S32_LE:
182 return SRC_SF_S32;
183 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
311 return 0;
312
313error1:
314 atc_pcm_release_resources(atc, apcm);
315 return err;
316}
317
318static int
319atc_pcm_release_resources(struct ct_atc *atc, struct ct_atc_pcm *apcm)
320{
321 struct src_mgr *src_mgr = atc->rsc_mgrs[SRC];
322 struct srcimp_mgr *srcimp_mgr = atc->rsc_mgrs[SRCIMP];
323 struct amixer_mgr *amixer_mgr = atc->rsc_mgrs[AMIXER];
324 struct sum_mgr *sum_mgr = atc->rsc_mgrs[SUM];
325 struct srcimp *srcimp = NULL;
326 int i = 0;
327
328 if (NULL != apcm->srcimps) {
329 for (i = 0; i < apcm->n_srcimp; i++) {
330 srcimp = apcm->srcimps[i];
331 srcimp->ops->unmap(srcimp);
332 srcimp_mgr->put_srcimp(srcimp_mgr, srcimp);
333 apcm->srcimps[i] = NULL;
334 }
335 kfree(apcm->srcimps);
336 apcm->srcimps = NULL;
337 }
338
339 if (NULL != apcm->srccs) {
340 for (i = 0; i < apcm->n_srcc; i++) {
341 src_mgr->put_src(src_mgr, apcm->srccs[i]);
342 apcm->srccs[i] = NULL;
343 }
344 kfree(apcm->srccs);
345 apcm->srccs = NULL;
346 }
347
348 if (NULL != apcm->amixers) {
349 for (i = 0; i < apcm->n_amixer; i++) {
350 amixer_mgr->put_amixer(amixer_mgr, apcm->amixers[i]);
351 apcm->amixers[i] = NULL;
352 }
353 kfree(apcm->amixers);
354 apcm->amixers = NULL;
355 }
356
357 if (NULL != apcm->mono) {
358 sum_mgr->put_sum(sum_mgr, apcm->mono);
359 apcm->mono = NULL;
360 }
361
362 if (NULL != apcm->src) {
363 src_mgr->put_src(src_mgr, apcm->src);
364 apcm->src = NULL;
365 }
366
367 if (NULL != apcm->vm_block) {
368 /* Undo device virtual mem map */
369 ct_unmap_audio_buffer(atc, apcm);
370 apcm->vm_block = NULL;
371 }
372
373 return 0;
374}
375
376static int atc_pcm_playback_start(struct ct_atc *atc, struct ct_atc_pcm *apcm)
377{
378 unsigned int max_cisz = 0;
379 struct src *src = apcm->src;
380
381 max_cisz = src->multi * src->rsc.msr;
382 max_cisz = 0x80 * (max_cisz < 8 ? max_cisz : 8);
383
384 src->ops->set_sa(src, apcm->vm_block->addr);
385 src->ops->set_la(src, apcm->vm_block->addr + apcm->vm_block->size);
386 src->ops->set_ca(src, apcm->vm_block->addr + max_cisz);
387 src->ops->set_cisz(src, max_cisz);
388
389 src->ops->set_bm(src, 1);
390 src->ops->set_state(src, SRC_STATE_INIT);
391 src->ops->commit_write(src);
392
393 return 0;
394}
395
396static int atc_pcm_stop(struct ct_atc *atc, struct ct_atc_pcm *apcm)
397{
398 struct src *src = NULL;
399 int i = 0;
400
401 src = apcm->src;
402 src->ops->set_bm(src, 0);
403 src->ops->set_state(src, SRC_STATE_OFF);
404 src->ops->commit_write(src);
405
406 if (NULL != apcm->srccs) {
407 for (i = 0; i < apcm->n_srcc; i++) {
408 src = apcm->srccs[i];
409 src->ops->set_bm(src, 0);
410 src->ops->set_state(src, SRC_STATE_OFF);
411 src->ops->commit_write(src);
412 }
413 }
414
415 apcm->started = 0;
416
417 return 0;
418}
419
420static int
421atc_pcm_playback_position(struct ct_atc *atc, struct ct_atc_pcm *apcm)
422{
423 struct src *src = apcm->src;
424 u32 size = 0, max_cisz = 0;
425 int position = 0;
426
427 position = src->ops->get_ca(src);
428
429 size = apcm->vm_block->size;
430 max_cisz = src->multi * src->rsc.msr;
431 max_cisz = 128 * (max_cisz < 8 ? max_cisz : 8);
432
433 return (position + size - max_cisz - apcm->vm_block->addr) % size;
434}
435
436struct src_node_conf_t {
437 unsigned int pitch;
438 unsigned int msr:8;
439 unsigned int mix_msr:8;
440 unsigned int imp_msr:8;
441 unsigned int vo:1;
442};
443
444static void setup_src_node_conf(struct ct_atc *atc, struct ct_atc_pcm *apcm,
445 struct src_node_conf_t *conf, int *n_srcc)
446{
447 unsigned int pitch = 0;
448
449 /* get pitch and convert to fixed-point 8.24 format. */
450 pitch = atc_get_pitch((atc->rsr * atc->msr),
451 apcm->substream->runtime->rate);
452 *n_srcc = 0;
453
454 if (1 == atc->msr) {
455 *n_srcc = apcm->substream->runtime->channels;
456 conf[0].pitch = pitch;
457 conf[0].mix_msr = conf[0].imp_msr = conf[0].msr = 1;
458 conf[0].vo = 1;
459 } else if (2 == atc->msr) {
460 if (0x8000000 < pitch) {
461 /* Need two-stage SRCs, SRCIMPs and
462 * AMIXERs for converting format */
463 conf[0].pitch = (atc->msr << 24);
464 conf[0].msr = conf[0].mix_msr = 1;
465 conf[0].imp_msr = atc->msr;
466 conf[0].vo = 0;
467 conf[1].pitch = atc_get_pitch(atc->rsr,
468 apcm->substream->runtime->rate);
469 conf[1].msr = conf[1].mix_msr = conf[1].imp_msr = 1;
470 conf[1].vo = 1;
471 *n_srcc = apcm->substream->runtime->channels * 2;
472 } else if (0x1000000 < pitch) {
473 /* Need one-stage SRCs, SRCIMPs and
474 * AMIXERs for converting format */
475 conf[0].pitch = pitch;
476 conf[0].msr = conf[0].mix_msr
477 = conf[0].imp_msr = atc->msr;
478 conf[0].vo = 1;
479 *n_srcc = apcm->substream->runtime->channels;
480 }
481 }
482}
483
484static int
485atc_pcm_capture_get_resources(struct ct_atc *atc, struct ct_atc_pcm *apcm)
486{
487 struct src_mgr *src_mgr = atc->rsc_mgrs[SRC];
488 struct srcimp_mgr *srcimp_mgr = atc->rsc_mgrs[SRCIMP];
489 struct amixer_mgr *amixer_mgr = atc->rsc_mgrs[AMIXER];
490 struct sum_mgr *sum_mgr = atc->rsc_mgrs[SUM];
491 struct src_desc src_dsc = {0};
492 struct src *src = NULL;
493 struct srcimp_desc srcimp_dsc = {0};
494 struct srcimp *srcimp = NULL;
495 struct amixer_desc mix_dsc = {0};
496 struct sum_desc sum_dsc = {0};
497 unsigned int pitch = 0;
498 int multi = 0, err = 0, i = 0;
499 int n_srcimp = 0, n_amixer = 0, n_srcc = 0, n_sum = 0;
500 struct src_node_conf_t src_node_conf[2] = {{0} };
501
Takashi Iwai822fa192009-06-02 14:12:17 +0200502 /* first release old resources */
503 atc->pcm_release_resources(atc, apcm);
504
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200505 /* The numbers of converting SRCs and SRCIMPs should be determined
506 * by pitch value. */
507
508 multi = apcm->substream->runtime->channels;
509
510 /* get pitch and convert to fixed-point 8.24 format. */
511 pitch = atc_get_pitch((atc->rsr * atc->msr),
512 apcm->substream->runtime->rate);
513
514 setup_src_node_conf(atc, apcm, src_node_conf, &n_srcc);
515 n_sum = (1 == multi) ? 1 : 0;
516 n_amixer += n_sum * 2 + n_srcc;
517 n_srcimp += n_srcc;
518 if ((multi > 1) && (0x8000000 >= pitch)) {
519 /* Need extra AMIXERs and SRCIMPs for special treatment
520 * of interleaved recording of conjugate channels */
521 n_amixer += multi * atc->msr;
522 n_srcimp += multi * atc->msr;
523 } else {
524 n_srcimp += multi;
525 }
526
527 if (n_srcc) {
528 apcm->srccs = kzalloc(sizeof(void *)*n_srcc, GFP_KERNEL);
529 if (NULL == apcm->srccs)
530 return -ENOMEM;
531 }
532 if (n_amixer) {
533 apcm->amixers = kzalloc(sizeof(void *)*n_amixer, GFP_KERNEL);
534 if (NULL == apcm->amixers) {
535 err = -ENOMEM;
536 goto error1;
537 }
538 }
539 apcm->srcimps = kzalloc(sizeof(void *)*n_srcimp, GFP_KERNEL);
540 if (NULL == apcm->srcimps) {
541 err = -ENOMEM;
542 goto error1;
543 }
544
545 /* Allocate SRCs for sample rate conversion if needed */
546 src_dsc.multi = 1;
547 src_dsc.mode = ARCRW;
548 for (i = 0, apcm->n_srcc = 0; i < n_srcc; i++) {
549 src_dsc.msr = src_node_conf[i/multi].msr;
550 err = src_mgr->get_src(src_mgr, &src_dsc,
551 (struct src **)&apcm->srccs[i]);
552 if (err)
553 goto error1;
554
555 src = apcm->srccs[i];
556 pitch = src_node_conf[i/multi].pitch;
557 src->ops->set_pitch(src, pitch);
558 src->ops->set_rom(src, select_rom(pitch));
559 src->ops->set_vo(src, src_node_conf[i/multi].vo);
560
561 apcm->n_srcc++;
562 }
563
564 /* Allocate AMIXERs for routing SRCs of conversion if needed */
565 for (i = 0, apcm->n_amixer = 0; i < n_amixer; i++) {
566 if (i < (n_sum*2))
567 mix_dsc.msr = atc->msr;
568 else if (i < (n_sum*2+n_srcc))
569 mix_dsc.msr = src_node_conf[(i-n_sum*2)/multi].mix_msr;
570 else
571 mix_dsc.msr = 1;
572
573 err = amixer_mgr->get_amixer(amixer_mgr, &mix_dsc,
574 (struct amixer **)&apcm->amixers[i]);
575 if (err)
576 goto error1;
577
578 apcm->n_amixer++;
579 }
580
581 /* Allocate a SUM resource to mix all input channels together */
582 sum_dsc.msr = atc->msr;
583 err = sum_mgr->get_sum(sum_mgr, &sum_dsc, (struct sum **)&apcm->mono);
584 if (err)
585 goto error1;
586
587 pitch = atc_get_pitch((atc->rsr * atc->msr),
588 apcm->substream->runtime->rate);
589 /* Allocate SRCIMP resources */
590 for (i = 0, apcm->n_srcimp = 0; i < n_srcimp; i++) {
591 if (i < (n_srcc))
592 srcimp_dsc.msr = src_node_conf[i/multi].imp_msr;
593 else if (1 == multi)
594 srcimp_dsc.msr = (pitch <= 0x8000000) ? atc->msr : 1;
595 else
596 srcimp_dsc.msr = 1;
597
598 err = srcimp_mgr->get_srcimp(srcimp_mgr, &srcimp_dsc, &srcimp);
599 if (err)
600 goto error1;
601
602 apcm->srcimps[i] = srcimp;
603 apcm->n_srcimp++;
604 }
605
606 /* Allocate a SRC for writing data to host memory */
607 src_dsc.multi = apcm->substream->runtime->channels;
608 src_dsc.msr = 1;
609 src_dsc.mode = MEMWR;
610 err = src_mgr->get_src(src_mgr, &src_dsc, (struct src **)&apcm->src);
611 if (err)
612 goto error1;
613
614 src = apcm->src;
615 src->ops->set_pitch(src, pitch);
616
617 /* Set up device virtual mem map */
618 err = ct_map_audio_buffer(atc, apcm);
619 if (err < 0)
620 goto error1;
621
622 return 0;
623
624error1:
625 atc_pcm_release_resources(atc, apcm);
626 return err;
627}
628
629static int atc_pcm_capture_prepare(struct ct_atc *atc, struct ct_atc_pcm *apcm)
630{
631 struct src *src = NULL;
632 struct amixer *amixer = NULL;
633 struct srcimp *srcimp = NULL;
634 struct ct_mixer *mixer = atc->mixer;
635 struct sum *mono = NULL;
636 struct rsc *out_ports[8] = {NULL};
637 int err = 0, i = 0, j = 0, n_sum = 0, multi = 0;
638 unsigned int pitch = 0;
639 int mix_base = 0, imp_base = 0;
640
641 if (NULL != apcm->src) {
642 /* Prepared pcm capture */
643 return 0;
644 }
645
646 /* Get needed resources. */
647 err = atc_pcm_capture_get_resources(atc, apcm);
648 if (err)
649 return err;
650
651 /* Connect resources */
652 mixer->get_output_ports(mixer, MIX_PCMO_FRONT,
653 &out_ports[0], &out_ports[1]);
654
655 multi = apcm->substream->runtime->channels;
656 if (1 == multi) {
657 mono = apcm->mono;
658 for (i = 0; i < 2; i++) {
659 amixer = apcm->amixers[i];
660 amixer->ops->setup(amixer, out_ports[i],
661 MONO_SUM_SCALE, mono);
662 }
663 out_ports[0] = &mono->rsc;
664 n_sum = 1;
665 mix_base = n_sum * 2;
666 }
667
668 for (i = 0; i < apcm->n_srcc; i++) {
669 src = apcm->srccs[i];
670 srcimp = apcm->srcimps[imp_base+i];
671 amixer = apcm->amixers[mix_base+i];
672 srcimp->ops->map(srcimp, src, out_ports[i%multi]);
673 amixer->ops->setup(amixer, &src->rsc, INIT_VOL, NULL);
674 out_ports[i%multi] = &amixer->rsc;
675 }
676
677 pitch = atc_get_pitch((atc->rsr * atc->msr),
678 apcm->substream->runtime->rate);
679
680 if ((multi > 1) && (pitch <= 0x8000000)) {
681 /* Special connection for interleaved
682 * recording with conjugate channels */
683 for (i = 0; i < multi; i++) {
684 out_ports[i]->ops->master(out_ports[i]);
685 for (j = 0; j < atc->msr; j++) {
686 amixer = apcm->amixers[apcm->n_srcc+j*multi+i];
687 amixer->ops->set_input(amixer, out_ports[i]);
688 amixer->ops->set_scale(amixer, INIT_VOL);
689 amixer->ops->set_sum(amixer, NULL);
690 amixer->ops->commit_raw_write(amixer);
691 out_ports[i]->ops->next_conj(out_ports[i]);
692
693 srcimp = apcm->srcimps[apcm->n_srcc+j*multi+i];
694 srcimp->ops->map(srcimp, apcm->src,
695 &amixer->rsc);
696 }
697 }
698 } else {
699 for (i = 0; i < multi; i++) {
700 srcimp = apcm->srcimps[apcm->n_srcc+i];
701 srcimp->ops->map(srcimp, apcm->src, out_ports[i]);
702 }
703 }
704
705 return 0;
706}
707
708static int atc_pcm_capture_start(struct ct_atc *atc, struct ct_atc_pcm *apcm)
709{
710 struct src *src = NULL;
711 struct src_mgr *src_mgr = atc->rsc_mgrs[SRC];
712 int i = 0, multi = 0;
713
714 if (apcm->started)
715 return 0;
716
717 apcm->started = 1;
718 multi = apcm->substream->runtime->channels;
719 /* Set up converting SRCs */
720 for (i = 0; i < apcm->n_srcc; i++) {
721 src = apcm->srccs[i];
722 src->ops->set_pm(src, ((i%multi) != (multi-1)));
723 src_mgr->src_disable(src_mgr, src);
724 }
725
726 /* Set up recording SRC */
727 src = apcm->src;
728 src->ops->set_sf(src, convert_format(apcm->substream->runtime->format));
729 src->ops->set_sa(src, apcm->vm_block->addr);
730 src->ops->set_la(src, apcm->vm_block->addr + apcm->vm_block->size);
731 src->ops->set_ca(src, apcm->vm_block->addr);
732 src_mgr->src_disable(src_mgr, src);
733
734 /* Disable relevant SRCs firstly */
735 src_mgr->commit_write(src_mgr);
736
737 /* Enable SRCs respectively */
738 for (i = 0; i < apcm->n_srcc; i++) {
739 src = apcm->srccs[i];
740 src->ops->set_state(src, SRC_STATE_RUN);
741 src->ops->commit_write(src);
742 src_mgr->src_enable_s(src_mgr, src);
743 }
744 src = apcm->src;
745 src->ops->set_bm(src, 1);
746 src->ops->set_state(src, SRC_STATE_RUN);
747 src->ops->commit_write(src);
748 src_mgr->src_enable_s(src_mgr, src);
749
750 /* Enable relevant SRCs synchronously */
751 src_mgr->commit_write(src_mgr);
752
753 return 0;
754}
755
756static int
757atc_pcm_capture_position(struct ct_atc *atc, struct ct_atc_pcm *apcm)
758{
759 struct src *src = apcm->src;
760
761 return src->ops->get_ca(src) - apcm->vm_block->addr;
762}
763
764static int spdif_passthru_playback_get_resources(struct ct_atc *atc,
765 struct ct_atc_pcm *apcm)
766{
767 struct src_mgr *src_mgr = atc->rsc_mgrs[SRC];
768 struct amixer_mgr *amixer_mgr = atc->rsc_mgrs[AMIXER];
769 struct src_desc desc = {0};
770 struct amixer_desc mix_dsc = {0};
771 struct src *src = NULL;
772 int err = 0;
773 int n_amixer = apcm->substream->runtime->channels, i = 0;
774 unsigned int pitch = 0, rsr = atc->pll_rate;
775
Takashi Iwai822fa192009-06-02 14:12:17 +0200776 /* first release old resources */
777 atc->pcm_release_resources(atc, apcm);
778
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200779 /* Get SRC resource */
780 desc.multi = apcm->substream->runtime->channels;
781 desc.msr = 1;
782 while (apcm->substream->runtime->rate > (rsr * desc.msr))
783 desc.msr <<= 1;
784
785 desc.mode = MEMRD;
786 err = src_mgr->get_src(src_mgr, &desc, (struct src **)&apcm->src);
787 if (err)
788 goto error1;
789
790 pitch = atc_get_pitch(apcm->substream->runtime->rate, (rsr * desc.msr));
791 src = apcm->src;
792 src->ops->set_pitch(src, pitch);
793 src->ops->set_rom(src, select_rom(pitch));
794 src->ops->set_sf(src, convert_format(apcm->substream->runtime->format));
795 src->ops->set_pm(src, (src->ops->next_interleave(src) != NULL));
796 src->ops->set_bp(src, 1);
797
798 /* Get AMIXER resource */
799 n_amixer = (n_amixer < 2) ? 2 : n_amixer;
800 apcm->amixers = kzalloc(sizeof(void *)*n_amixer, GFP_KERNEL);
801 if (NULL == apcm->amixers) {
802 err = -ENOMEM;
803 goto error1;
804 }
805 mix_dsc.msr = desc.msr;
806 for (i = 0, apcm->n_amixer = 0; i < n_amixer; i++) {
807 err = amixer_mgr->get_amixer(amixer_mgr, &mix_dsc,
808 (struct amixer **)&apcm->amixers[i]);
809 if (err)
810 goto error1;
811
812 apcm->n_amixer++;
813 }
814
815 /* Set up device virtual mem map */
816 err = ct_map_audio_buffer(atc, apcm);
817 if (err < 0)
818 goto error1;
819
820 return 0;
821
822error1:
823 atc_pcm_release_resources(atc, apcm);
824 return err;
825}
826
827static int
828spdif_passthru_playback_setup(struct ct_atc *atc, struct ct_atc_pcm *apcm)
829{
830 struct dao *dao = container_of(atc->daios[SPDIFOO], struct dao, daio);
831 unsigned long flags;
832 unsigned int rate = apcm->substream->runtime->rate;
833 unsigned int status = 0;
834 int err = 0;
835 unsigned char iec958_con_fs = 0;
836
837 switch (rate) {
838 case 48000:
839 iec958_con_fs = IEC958_AES3_CON_FS_48000;
840 break;
841 case 44100:
842 iec958_con_fs = IEC958_AES3_CON_FS_44100;
843 break;
844 case 32000:
845 iec958_con_fs = IEC958_AES3_CON_FS_32000;
846 break;
847 default:
848 return -ENOENT;
849 }
850
851 spin_lock_irqsave(&atc->atc_lock, flags);
852 dao->ops->get_spos(dao, &status);
853 if (((status >> 24) & IEC958_AES3_CON_FS) != iec958_con_fs) {
854 status &= ((~IEC958_AES3_CON_FS) << 24);
855 status |= (iec958_con_fs << 24);
856 dao->ops->set_spos(dao, status);
857 dao->ops->commit_write(dao);
858 }
859 if ((rate != atc->pll_rate) && (32000 != rate)) {
860 err = ((struct hw *)atc->hw)->pll_init(atc->hw, rate);
861 atc->pll_rate = err ? 0 : rate;
862 }
863 spin_unlock_irqrestore(&atc->atc_lock, flags);
864
865 return err;
866}
867
868static int
869spdif_passthru_playback_prepare(struct ct_atc *atc, struct ct_atc_pcm *apcm)
870{
871 struct src *src = NULL;
872 struct amixer *amixer = NULL;
873 struct dao *dao = NULL;
874 int err = 0;
875 int i = 0;
876 unsigned long flags;
877
878 if (NULL != apcm->src)
879 return 0;
880
881 /* Configure SPDIFOO and PLL to passthrough mode;
882 * determine pll_rate. */
883 err = spdif_passthru_playback_setup(atc, apcm);
884 if (err)
885 return err;
886
887 /* Get needed resources. */
888 err = spdif_passthru_playback_get_resources(atc, apcm);
889 if (err)
890 return err;
891
892 /* Connect resources */
893 src = apcm->src;
894 for (i = 0; i < apcm->n_amixer; i++) {
895 amixer = apcm->amixers[i];
896 amixer->ops->setup(amixer, &src->rsc, INIT_VOL, NULL);
897 src = src->ops->next_interleave(src);
898 if (NULL == src)
899 src = apcm->src;
900 }
901 /* Connect to SPDIFOO */
902 spin_lock_irqsave(&atc->atc_lock, flags);
903 dao = container_of(atc->daios[SPDIFOO], struct dao, daio);
904 amixer = apcm->amixers[0];
905 dao->ops->set_left_input(dao, &amixer->rsc);
906 amixer = apcm->amixers[1];
907 dao->ops->set_right_input(dao, &amixer->rsc);
908 spin_unlock_irqrestore(&atc->atc_lock, flags);
909
910 return 0;
911}
912
913static int atc_select_line_in(struct ct_atc *atc)
914{
915 struct hw *hw = atc->hw;
916 struct ct_mixer *mixer = atc->mixer;
917 struct src *src = NULL;
918
919 if (hw->is_adc_source_selected(hw, ADC_LINEIN))
920 return 0;
921
922 mixer->set_input_left(mixer, MIX_MIC_IN, NULL);
923 mixer->set_input_right(mixer, MIX_MIC_IN, NULL);
924
925 hw->select_adc_source(hw, ADC_LINEIN);
926
927 src = atc->srcs[2];
928 mixer->set_input_left(mixer, MIX_LINE_IN, &src->rsc);
929 src = atc->srcs[3];
930 mixer->set_input_right(mixer, MIX_LINE_IN, &src->rsc);
931
932 return 0;
933}
934
935static int atc_select_mic_in(struct ct_atc *atc)
936{
937 struct hw *hw = atc->hw;
938 struct ct_mixer *mixer = atc->mixer;
939 struct src *src = NULL;
940
941 if (hw->is_adc_source_selected(hw, ADC_MICIN))
942 return 0;
943
944 mixer->set_input_left(mixer, MIX_LINE_IN, NULL);
945 mixer->set_input_right(mixer, MIX_LINE_IN, NULL);
946
947 hw->select_adc_source(hw, ADC_MICIN);
948
949 src = atc->srcs[2];
950 mixer->set_input_left(mixer, MIX_MIC_IN, &src->rsc);
951 src = atc->srcs[3];
952 mixer->set_input_right(mixer, MIX_MIC_IN, &src->rsc);
953
954 return 0;
955}
956
957static int atc_have_digit_io_switch(struct ct_atc *atc)
958{
959 struct hw *hw = atc->hw;
960
961 return hw->have_digit_io_switch(hw);
962}
963
964static int atc_select_digit_io(struct ct_atc *atc)
965{
966 struct hw *hw = atc->hw;
967
968 if (hw->is_adc_source_selected(hw, ADC_NONE))
969 return 0;
970
971 hw->select_adc_source(hw, ADC_NONE);
972
973 return 0;
974}
975
976static int atc_daio_unmute(struct ct_atc *atc, unsigned char state, int type)
977{
978 struct daio_mgr *daio_mgr = atc->rsc_mgrs[DAIO];
979
980 if (state)
981 daio_mgr->daio_enable(daio_mgr, atc->daios[type]);
982 else
983 daio_mgr->daio_disable(daio_mgr, atc->daios[type]);
984
985 daio_mgr->commit_write(daio_mgr);
986
987 return 0;
988}
989
990static int
991atc_dao_get_status(struct ct_atc *atc, unsigned int *status, int type)
992{
993 struct dao *dao = container_of(atc->daios[type], struct dao, daio);
994 return dao->ops->get_spos(dao, status);
995}
996
997static int
998atc_dao_set_status(struct ct_atc *atc, unsigned int status, int type)
999{
1000 struct dao *dao = container_of(atc->daios[type], struct dao, daio);
1001
1002 dao->ops->set_spos(dao, status);
1003 dao->ops->commit_write(dao);
1004 return 0;
1005}
1006
1007static int atc_line_front_unmute(struct ct_atc *atc, unsigned char state)
1008{
1009 return atc_daio_unmute(atc, state, LINEO1);
1010}
1011
1012static int atc_line_surround_unmute(struct ct_atc *atc, unsigned char state)
1013{
1014 return atc_daio_unmute(atc, state, LINEO4);
1015}
1016
1017static int atc_line_clfe_unmute(struct ct_atc *atc, unsigned char state)
1018{
1019 return atc_daio_unmute(atc, state, LINEO3);
1020}
1021
1022static int atc_line_rear_unmute(struct ct_atc *atc, unsigned char state)
1023{
1024 return atc_daio_unmute(atc, state, LINEO2);
1025}
1026
1027static int atc_line_in_unmute(struct ct_atc *atc, unsigned char state)
1028{
1029 return atc_daio_unmute(atc, state, LINEIM);
1030}
1031
1032static int atc_spdif_out_unmute(struct ct_atc *atc, unsigned char state)
1033{
1034 return atc_daio_unmute(atc, state, SPDIFOO);
1035}
1036
1037static int atc_spdif_in_unmute(struct ct_atc *atc, unsigned char state)
1038{
1039 return atc_daio_unmute(atc, state, SPDIFIO);
1040}
1041
1042static int atc_spdif_out_get_status(struct ct_atc *atc, unsigned int *status)
1043{
1044 return atc_dao_get_status(atc, status, SPDIFOO);
1045}
1046
1047static int atc_spdif_out_set_status(struct ct_atc *atc, unsigned int status)
1048{
1049 return atc_dao_set_status(atc, status, SPDIFOO);
1050}
1051
1052static int atc_spdif_out_passthru(struct ct_atc *atc, unsigned char state)
1053{
1054 unsigned long flags;
1055 struct dao_desc da_dsc = {0};
1056 struct dao *dao = NULL;
1057 int err = 0;
1058 struct ct_mixer *mixer = atc->mixer;
1059 struct rsc *rscs[2] = {NULL};
1060 unsigned int spos = 0;
1061
1062 spin_lock_irqsave(&atc->atc_lock, flags);
1063 dao = container_of(atc->daios[SPDIFOO], struct dao, daio);
1064 da_dsc.msr = state ? 1 : atc->msr;
1065 da_dsc.passthru = state ? 1 : 0;
1066 err = dao->ops->reinit(dao, &da_dsc);
1067 if (state) {
1068 spos = IEC958_DEFAULT_CON;
1069 } else {
1070 mixer->get_output_ports(mixer, MIX_SPDIF_OUT,
1071 &rscs[0], &rscs[1]);
1072 dao->ops->set_left_input(dao, rscs[0]);
1073 dao->ops->set_right_input(dao, rscs[1]);
1074 /* Restore PLL to atc->rsr if needed. */
1075 if (atc->pll_rate != atc->rsr) {
1076 err = ((struct hw *)atc->hw)->pll_init(atc->hw,
1077 atc->rsr);
1078 atc->pll_rate = err ? 0 : atc->rsr;
1079 }
1080 }
1081 dao->ops->set_spos(dao, spos);
1082 dao->ops->commit_write(dao);
1083 spin_unlock_irqrestore(&atc->atc_lock, flags);
1084
1085 return err;
1086}
1087
1088static int ct_atc_destroy(struct ct_atc *atc)
1089{
1090 struct daio_mgr *daio_mgr = NULL;
1091 struct dao *dao = NULL;
1092 struct dai *dai = NULL;
1093 struct daio *daio = NULL;
1094 struct sum_mgr *sum_mgr = NULL;
1095 struct src_mgr *src_mgr = NULL;
1096 struct srcimp_mgr *srcimp_mgr = NULL;
1097 struct srcimp *srcimp = NULL;
1098 struct ct_mixer *mixer = NULL;
1099 int i = 0;
1100
1101 if (NULL == atc)
1102 return 0;
1103
1104 /* Stop hardware and disable all interrupts */
1105 if (NULL != atc->hw)
1106 ((struct hw *)atc->hw)->card_stop(atc->hw);
1107
1108 /* Destroy internal mixer objects */
1109 if (NULL != atc->mixer) {
1110 mixer = atc->mixer;
1111 mixer->set_input_left(mixer, MIX_LINE_IN, NULL);
1112 mixer->set_input_right(mixer, MIX_LINE_IN, NULL);
1113 mixer->set_input_left(mixer, MIX_MIC_IN, NULL);
1114 mixer->set_input_right(mixer, MIX_MIC_IN, NULL);
1115 mixer->set_input_left(mixer, MIX_SPDIF_IN, NULL);
1116 mixer->set_input_right(mixer, MIX_SPDIF_IN, NULL);
1117 ct_mixer_destroy(atc->mixer);
1118 }
1119
1120 if (NULL != atc->daios) {
1121 daio_mgr = (struct daio_mgr *)atc->rsc_mgrs[DAIO];
1122 for (i = 0; i < atc->n_daio; i++) {
1123 daio = atc->daios[i];
1124 if (daio->type < LINEIM) {
1125 dao = container_of(daio, struct dao, daio);
1126 dao->ops->clear_left_input(dao);
1127 dao->ops->clear_right_input(dao);
1128 } else {
1129 dai = container_of(daio, struct dai, daio);
1130 /* some thing to do for dai ... */
1131 }
1132 daio_mgr->put_daio(daio_mgr, daio);
1133 }
1134 kfree(atc->daios);
1135 }
1136
1137 if (NULL != atc->pcm) {
1138 sum_mgr = atc->rsc_mgrs[SUM];
1139 for (i = 0; i < atc->n_pcm; i++)
1140 sum_mgr->put_sum(sum_mgr, atc->pcm[i]);
1141
1142 kfree(atc->pcm);
1143 }
1144
1145 if (NULL != atc->srcs) {
1146 src_mgr = atc->rsc_mgrs[SRC];
1147 for (i = 0; i < atc->n_src; i++)
1148 src_mgr->put_src(src_mgr, atc->srcs[i]);
1149
1150 kfree(atc->srcs);
1151 }
1152
1153 if (NULL != atc->srcimps) {
1154 srcimp_mgr = atc->rsc_mgrs[SRCIMP];
1155 for (i = 0; i < atc->n_srcimp; i++) {
1156 srcimp = atc->srcimps[i];
1157 srcimp->ops->unmap(srcimp);
1158 srcimp_mgr->put_srcimp(srcimp_mgr, atc->srcimps[i]);
1159 }
1160 kfree(atc->srcimps);
1161 }
1162
1163 for (i = 0; i < NUM_RSCTYP; i++) {
1164 if ((NULL != rsc_mgr_funcs[i].destroy) &&
1165 (NULL != atc->rsc_mgrs[i]))
1166 rsc_mgr_funcs[i].destroy(atc->rsc_mgrs[i]);
1167
1168 }
1169
1170 if (NULL != atc->hw)
1171 destroy_hw_obj((struct hw *)atc->hw);
1172
1173 /* Destroy device virtual memory manager object */
1174 if (NULL != atc->vm) {
1175 ct_vm_destroy(atc->vm);
1176 atc->vm = NULL;
1177 }
1178
1179 kfree(atc);
1180
1181 return 0;
1182}
1183
1184static int atc_dev_free(struct snd_device *dev)
1185{
1186 struct ct_atc *atc = dev->device_data;
1187 return ct_atc_destroy(atc);
1188}
1189
1190static int atc_identify_card(struct ct_atc *atc)
1191{
Takashi Iwai35b053b2009-05-14 15:17:16 +02001192 u16 subsys;
1193 u8 revision;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001194 struct pci_dev *pci = atc->pci;
1195 const struct ct_atc_chip_details *d;
1196 enum CTCARDS i;
1197
Takashi Iwai35b053b2009-05-14 15:17:16 +02001198 subsys = pci->subsystem_device;
1199 revision = pci->revision;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001200 atc->chip_details = NULL;
1201 atc->model = NUM_CTCARDS;
1202 for (d = atc_chip_details; d->vendor; d++) {
1203 if (d->vendor != pci->vendor || d->device != pci->device)
1204 continue;
1205
1206 if (NULL == d->sub_details) {
1207 atc->chip_details = d;
1208 break;
1209 }
1210 for (i = 0; i < NUM_CTCARDS; i++) {
1211 if ((d->sub_details[i].subsys == subsys) ||
1212 (((subsys & 0x6000) == 0x6000) &&
1213 ((d->sub_details[i].subsys & 0x6000) == 0x6000))) {
1214 atc->model = i;
1215 break;
1216 }
1217 }
1218 if (i >= NUM_CTCARDS)
1219 continue;
1220
1221 atc->chip_details = d;
1222 break;
1223 /* not take revision into consideration now */
1224 }
1225 if (!d->vendor)
1226 return -ENOENT;
1227
1228 return 0;
1229}
1230
1231static int ct_create_alsa_devs(struct ct_atc *atc)
1232{
1233 enum CTALSADEVS i;
1234 struct hw *hw = atc->hw;
1235 int err;
1236
1237 switch (hw->get_chip_type(hw)) {
1238 case ATC20K1:
1239 alsa_dev_funcs[MIXER].public_name = "20K1";
1240 break;
1241 case ATC20K2:
1242 alsa_dev_funcs[MIXER].public_name = "20K2";
1243 break;
1244 default:
1245 alsa_dev_funcs[MIXER].public_name = "Unknown";
1246 break;
1247 }
1248
1249 for (i = 0; i < NUM_CTALSADEVS; i++) {
1250 if (NULL == alsa_dev_funcs[i].create)
1251 continue;
1252
1253 err = alsa_dev_funcs[i].create(atc, i,
1254 alsa_dev_funcs[i].public_name);
1255 if (err) {
Takashi Iwaib3e0afe2009-05-14 15:19:30 +02001256 printk(KERN_ERR "ctxfi: "
1257 "Creating alsa device %d failed!\n", i);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001258 return err;
1259 }
1260 }
1261
1262 return 0;
1263}
1264
1265static int atc_create_hw_devs(struct ct_atc *atc)
1266{
1267 struct hw *hw = NULL;
1268 struct card_conf info = {0};
1269 int i = 0, err = 0;
1270
1271 err = create_hw_obj(atc->pci, &hw);
1272 if (err) {
1273 printk(KERN_ERR "Failed to create hw obj!!!\n");
1274 return err;
1275 }
1276 atc->hw = hw;
1277
1278 /* Initialize card hardware. */
1279 info.rsr = atc->rsr;
1280 info.msr = atc->msr;
1281 info.vm_pgt_phys = atc_get_ptp_phys(atc, 0);
1282 err = hw->card_init(hw, &info);
1283 if (err < 0)
1284 return err;
1285
1286 for (i = 0; i < NUM_RSCTYP; i++) {
1287 if (NULL == rsc_mgr_funcs[i].create)
1288 continue;
1289
1290 err = rsc_mgr_funcs[i].create(atc->hw, &atc->rsc_mgrs[i]);
1291 if (err) {
Takashi Iwaib3e0afe2009-05-14 15:19:30 +02001292 printk(KERN_ERR "ctxfi: "
1293 "Failed to create rsc_mgr %d!!!\n", i);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001294 return err;
1295 }
1296 }
1297
1298 return 0;
1299}
1300
1301static int atc_get_resources(struct ct_atc *atc)
1302{
1303 struct daio_desc da_desc = {0};
1304 struct daio_mgr *daio_mgr = NULL;
1305 struct src_desc src_dsc = {0};
1306 struct src_mgr *src_mgr = NULL;
1307 struct srcimp_desc srcimp_dsc = {0};
1308 struct srcimp_mgr *srcimp_mgr = NULL;
1309 struct sum_desc sum_dsc = {0};
1310 struct sum_mgr *sum_mgr = NULL;
1311 int err = 0, i = 0;
Takashi Iwai35b053b2009-05-14 15:17:16 +02001312 unsigned short subsys_id;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001313
1314 atc->daios = kzalloc(sizeof(void *)*(DAIONUM), GFP_KERNEL);
1315 if (NULL == atc->daios)
1316 return -ENOMEM;
1317
1318 atc->srcs = kzalloc(sizeof(void *)*(2*2), GFP_KERNEL);
1319 if (NULL == atc->srcs)
1320 return -ENOMEM;
1321
1322 atc->srcimps = kzalloc(sizeof(void *)*(2*2), GFP_KERNEL);
1323 if (NULL == atc->srcimps)
1324 return -ENOMEM;
1325
1326 atc->pcm = kzalloc(sizeof(void *)*(2*4), GFP_KERNEL);
1327 if (NULL == atc->pcm)
1328 return -ENOMEM;
1329
1330 daio_mgr = (struct daio_mgr *)atc->rsc_mgrs[DAIO];
1331 da_desc.msr = atc->msr;
1332 for (i = 0, atc->n_daio = 0; i < DAIONUM-1; i++) {
1333 da_desc.type = i;
1334 err = daio_mgr->get_daio(daio_mgr, &da_desc,
1335 (struct daio **)&atc->daios[i]);
1336 if (err) {
Takashi Iwaib3e0afe2009-05-14 15:19:30 +02001337 printk(KERN_ERR "ctxfi: Failed to get DAIO "
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001338 "resource %d!!!\n", i);
1339 return err;
1340 }
1341 atc->n_daio++;
1342 }
Takashi Iwai35b053b2009-05-14 15:17:16 +02001343 subsys_id = atc->pci->subsystem_device;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001344 if ((subsys_id == 0x0029) || (subsys_id == 0x0031)) {
1345 /* SB073x cards */
1346 da_desc.type = SPDIFI1;
1347 } else {
1348 da_desc.type = SPDIFIO;
1349 }
1350 err = daio_mgr->get_daio(daio_mgr, &da_desc,
1351 (struct daio **)&atc->daios[i]);
1352 if (err) {
Takashi Iwaib3e0afe2009-05-14 15:19:30 +02001353 printk(KERN_ERR "ctxfi: Failed to get S/PDIF-in resource!!!\n");
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001354 return err;
1355 }
1356 atc->n_daio++;
1357
1358 src_mgr = atc->rsc_mgrs[SRC];
1359 src_dsc.multi = 1;
1360 src_dsc.msr = atc->msr;
1361 src_dsc.mode = ARCRW;
1362 for (i = 0, atc->n_src = 0; i < (2*2); i++) {
1363 err = src_mgr->get_src(src_mgr, &src_dsc,
1364 (struct src **)&atc->srcs[i]);
1365 if (err)
1366 return err;
1367
1368 atc->n_src++;
1369 }
1370
1371 srcimp_mgr = atc->rsc_mgrs[SRCIMP];
1372 srcimp_dsc.msr = 8; /* SRCIMPs for S/PDIFIn SRT */
1373 for (i = 0, atc->n_srcimp = 0; i < (2*1); i++) {
1374 err = srcimp_mgr->get_srcimp(srcimp_mgr, &srcimp_dsc,
1375 (struct srcimp **)&atc->srcimps[i]);
1376 if (err)
1377 return err;
1378
1379 atc->n_srcimp++;
1380 }
1381 srcimp_dsc.msr = 8; /* SRCIMPs for LINE/MICIn SRT */
1382 for (i = 0; i < (2*1); i++) {
1383 err = srcimp_mgr->get_srcimp(srcimp_mgr, &srcimp_dsc,
1384 (struct srcimp **)&atc->srcimps[2*1+i]);
1385 if (err)
1386 return err;
1387
1388 atc->n_srcimp++;
1389 }
1390
1391 sum_mgr = atc->rsc_mgrs[SUM];
1392 sum_dsc.msr = atc->msr;
1393 for (i = 0, atc->n_pcm = 0; i < (2*4); i++) {
1394 err = sum_mgr->get_sum(sum_mgr, &sum_dsc,
1395 (struct sum **)&atc->pcm[i]);
1396 if (err)
1397 return err;
1398
1399 atc->n_pcm++;
1400 }
1401
1402 err = ct_mixer_create(atc, (struct ct_mixer **)&atc->mixer);
1403 if (err) {
Takashi Iwaib3e0afe2009-05-14 15:19:30 +02001404 printk(KERN_ERR "ctxfi: Failed to create mixer obj!!!\n");
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001405 return err;
1406 }
1407
1408 return 0;
1409}
1410
1411static void
1412atc_connect_dai(struct src_mgr *src_mgr, struct dai *dai,
1413 struct src **srcs, struct srcimp **srcimps)
1414{
1415 struct rsc *rscs[2] = {NULL};
1416 struct src *src = NULL;
1417 struct srcimp *srcimp = NULL;
1418 int i = 0;
1419
1420 rscs[0] = &dai->daio.rscl;
1421 rscs[1] = &dai->daio.rscr;
1422 for (i = 0; i < 2; i++) {
1423 src = srcs[i];
1424 srcimp = srcimps[i];
1425 srcimp->ops->map(srcimp, src, rscs[i]);
1426 src_mgr->src_disable(src_mgr, src);
1427 }
1428
1429 src_mgr->commit_write(src_mgr); /* Actually disable SRCs */
1430
1431 src = srcs[0];
1432 src->ops->set_pm(src, 1);
1433 for (i = 0; i < 2; i++) {
1434 src = srcs[i];
1435 src->ops->set_state(src, SRC_STATE_RUN);
1436 src->ops->commit_write(src);
1437 src_mgr->src_enable_s(src_mgr, src);
1438 }
1439
1440 dai->ops->set_srt_srcl(dai, &(srcs[0]->rsc));
1441 dai->ops->set_srt_srcr(dai, &(srcs[1]->rsc));
1442
1443 dai->ops->set_enb_src(dai, 1);
1444 dai->ops->set_enb_srt(dai, 1);
1445 dai->ops->commit_write(dai);
1446
1447 src_mgr->commit_write(src_mgr); /* Synchronously enable SRCs */
1448}
1449
1450static void atc_connect_resources(struct ct_atc *atc)
1451{
1452 struct dai *dai = NULL;
1453 struct dao *dao = NULL;
1454 struct src *src = NULL;
1455 struct sum *sum = NULL;
1456 struct ct_mixer *mixer = NULL;
1457 struct rsc *rscs[2] = {NULL};
1458 int i = 0, j = 0;
1459
1460 mixer = atc->mixer;
1461
1462 for (i = MIX_WAVE_FRONT, j = LINEO1; i <= MIX_SPDIF_OUT; i++, j++) {
1463 mixer->get_output_ports(mixer, i, &rscs[0], &rscs[1]);
1464 dao = container_of(atc->daios[j], struct dao, daio);
1465 dao->ops->set_left_input(dao, rscs[0]);
1466 dao->ops->set_right_input(dao, rscs[1]);
1467 }
1468
1469 dai = container_of(atc->daios[LINEIM], struct dai, daio);
1470 atc_connect_dai(atc->rsc_mgrs[SRC], dai,
1471 (struct src **)&atc->srcs[2],
1472 (struct srcimp **)&atc->srcimps[2]);
1473 src = atc->srcs[2];
1474 mixer->set_input_left(mixer, MIX_LINE_IN, &src->rsc);
1475 src = atc->srcs[3];
1476 mixer->set_input_right(mixer, MIX_LINE_IN, &src->rsc);
1477
1478 dai = container_of(atc->daios[SPDIFIO], struct dai, daio);
1479 atc_connect_dai(atc->rsc_mgrs[SRC], dai,
1480 (struct src **)&atc->srcs[0],
1481 (struct srcimp **)&atc->srcimps[0]);
1482
1483 src = atc->srcs[0];
1484 mixer->set_input_left(mixer, MIX_SPDIF_IN, &src->rsc);
1485 src = atc->srcs[1];
1486 mixer->set_input_right(mixer, MIX_SPDIF_IN, &src->rsc);
1487
1488 for (i = MIX_PCMI_FRONT, j = 0; i <= MIX_PCMI_SURROUND; i++, j += 2) {
1489 sum = atc->pcm[j];
1490 mixer->set_input_left(mixer, i, &sum->rsc);
1491 sum = atc->pcm[j+1];
1492 mixer->set_input_right(mixer, i, &sum->rsc);
1493 }
1494}
1495
1496static void atc_set_ops(struct ct_atc *atc)
1497{
1498 /* Set operations */
1499 atc->map_audio_buffer = ct_map_audio_buffer;
1500 atc->unmap_audio_buffer = ct_unmap_audio_buffer;
1501 atc->pcm_playback_prepare = atc_pcm_playback_prepare;
1502 atc->pcm_release_resources = atc_pcm_release_resources;
1503 atc->pcm_playback_start = atc_pcm_playback_start;
1504 atc->pcm_playback_stop = atc_pcm_stop;
1505 atc->pcm_playback_position = atc_pcm_playback_position;
1506 atc->pcm_capture_prepare = atc_pcm_capture_prepare;
1507 atc->pcm_capture_start = atc_pcm_capture_start;
1508 atc->pcm_capture_stop = atc_pcm_stop;
1509 atc->pcm_capture_position = atc_pcm_capture_position;
1510 atc->spdif_passthru_playback_prepare = spdif_passthru_playback_prepare;
1511 atc->get_ptp_phys = atc_get_ptp_phys;
1512 atc->select_line_in = atc_select_line_in;
1513 atc->select_mic_in = atc_select_mic_in;
1514 atc->select_digit_io = atc_select_digit_io;
1515 atc->line_front_unmute = atc_line_front_unmute;
1516 atc->line_surround_unmute = atc_line_surround_unmute;
1517 atc->line_clfe_unmute = atc_line_clfe_unmute;
1518 atc->line_rear_unmute = atc_line_rear_unmute;
1519 atc->line_in_unmute = atc_line_in_unmute;
1520 atc->spdif_out_unmute = atc_spdif_out_unmute;
1521 atc->spdif_in_unmute = atc_spdif_in_unmute;
1522 atc->spdif_out_get_status = atc_spdif_out_get_status;
1523 atc->spdif_out_set_status = atc_spdif_out_set_status;
1524 atc->spdif_out_passthru = atc_spdif_out_passthru;
1525 atc->have_digit_io_switch = atc_have_digit_io_switch;
1526}
1527
1528/**
1529 * ct_atc_create - create and initialize a hardware manager
1530 * @card: corresponding alsa card object
1531 * @pci: corresponding kernel pci device object
1532 * @ratc: return created object address in it
1533 *
1534 * Creates and initializes a hardware manager.
1535 *
1536 * Creates kmallocated ct_atc structure. Initializes hardware.
1537 * Returns 0 if suceeds, or negative error code if fails.
1538 */
1539
1540int ct_atc_create(struct snd_card *card, struct pci_dev *pci,
1541 unsigned int rsr, unsigned int msr, struct ct_atc **ratc)
1542{
1543 struct ct_atc *atc = NULL;
1544 static struct snd_device_ops ops = {
1545 .dev_free = atc_dev_free,
1546 };
1547 int err = 0;
1548
1549 *ratc = NULL;
1550
1551 atc = kzalloc(sizeof(*atc), GFP_KERNEL);
1552 if (NULL == atc)
1553 return -ENOMEM;
1554
1555 atc->card = card;
1556 atc->pci = pci;
1557 atc->rsr = rsr;
1558 atc->msr = msr;
1559
1560 /* Set operations */
1561 atc_set_ops(atc);
1562
1563 spin_lock_init(&atc->atc_lock);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001564
1565 /* Find card model */
1566 err = atc_identify_card(atc);
1567 if (err < 0) {
1568 printk(KERN_ERR "ctatc: Card not recognised\n");
1569 goto error1;
1570 }
1571
1572 /* Set up device virtual memory management object */
1573 err = ct_vm_create(&atc->vm);
1574 if (err < 0)
1575 goto error1;
1576
1577 /* Create all atc hw devices */
1578 err = atc_create_hw_devs(atc);
1579 if (err < 0)
1580 goto error1;
1581
1582 /* Get resources */
1583 err = atc_get_resources(atc);
1584 if (err < 0)
1585 goto error1;
1586
1587 /* Build topology */
1588 atc_connect_resources(atc);
1589
1590 atc->create_alsa_devs = ct_create_alsa_devs;
1591
1592 err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, atc, &ops);
1593 if (err < 0)
1594 goto error1;
1595
1596 snd_card_set_dev(card, &pci->dev);
1597
1598 *ratc = atc;
1599 return 0;
1600
1601error1:
1602 ct_atc_destroy(atc);
Takashi Iwaib3e0afe2009-05-14 15:19:30 +02001603 printk(KERN_ERR "ctxfi: Something wrong!!!\n");
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001604 return err;
1605}
1606