blob: 268ecc4c285607c1cceea9b544058944a9000a2f [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:
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200173 return SRC_SF_U8;
174 case SNDRV_PCM_FORMAT_S16_LE:
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200175 return SRC_SF_S16;
176 case SNDRV_PCM_FORMAT_S24_3LE:
177 return SRC_SF_S24;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200178 case SNDRV_PCM_FORMAT_S32_LE:
179 return SRC_SF_S32;
Takashi Iwaid2b9b962009-06-02 14:39:05 +0200180 case SNDRV_PCM_FORMAT_FLOAT_LE:
181 return SRC_SF_F32;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200182 default:
Takashi Iwaib3e0afe2009-05-14 15:19:30 +0200183 printk(KERN_ERR "ctxfi: not recognized snd format is %d \n",
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200184 snd_format);
185 return SRC_SF_S16;
186 }
187}
188
189static unsigned int
190atc_get_pitch(unsigned int input_rate, unsigned int output_rate)
191{
192 unsigned int pitch = 0;
193 int b = 0;
194
195 /* get pitch and convert to fixed-point 8.24 format. */
196 pitch = (input_rate / output_rate) << 24;
197 input_rate %= output_rate;
198 input_rate /= 100;
199 output_rate /= 100;
200 for (b = 31; ((b >= 0) && !(input_rate >> b)); )
201 b--;
202
203 if (b >= 0) {
204 input_rate <<= (31 - b);
205 input_rate /= output_rate;
206 b = 24 - (31 - b);
207 if (b >= 0)
208 input_rate <<= b;
209 else
210 input_rate >>= -b;
211
212 pitch |= input_rate;
213 }
214
215 return pitch;
216}
217
218static int select_rom(unsigned int pitch)
219{
220 if ((pitch > 0x00428f5c) && (pitch < 0x01b851ec)) {
221 /* 0.26 <= pitch <= 1.72 */
222 return 1;
223 } else if ((0x01d66666 == pitch) || (0x01d66667 == pitch)) {
224 /* pitch == 1.8375 */
225 return 2;
226 } else if (0x02000000 == pitch) {
227 /* pitch == 2 */
228 return 3;
229 } else if ((pitch >= 0x0) && (pitch <= 0x08000000)) {
230 /* 0 <= pitch <= 8 */
231 return 0;
232 } else {
233 return -ENOENT;
234 }
235}
236
237static int atc_pcm_playback_prepare(struct ct_atc *atc, struct ct_atc_pcm *apcm)
238{
239 struct src_mgr *src_mgr = atc->rsc_mgrs[SRC];
240 struct amixer_mgr *amixer_mgr = atc->rsc_mgrs[AMIXER];
241 struct src_desc desc = {0};
242 struct amixer_desc mix_dsc = {0};
243 struct src *src = NULL;
244 struct amixer *amixer = NULL;
245 int err = 0;
246 int n_amixer = apcm->substream->runtime->channels, i = 0;
247 int device = apcm->substream->pcm->device;
248 unsigned int pitch = 0;
249 unsigned long flags;
250
251 if (NULL != apcm->src) {
252 /* Prepared pcm playback */
253 return 0;
254 }
255
Takashi Iwai822fa192009-06-02 14:12:17 +0200256 /* first release old resources */
257 atc->pcm_release_resources(atc, apcm);
258
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200259 /* Get SRC resource */
260 desc.multi = apcm->substream->runtime->channels;
261 desc.msr = atc->msr;
262 desc.mode = MEMRD;
263 err = src_mgr->get_src(src_mgr, &desc, (struct src **)&apcm->src);
264 if (err)
265 goto error1;
266
267 pitch = atc_get_pitch(apcm->substream->runtime->rate,
268 (atc->rsr * atc->msr));
269 src = apcm->src;
270 src->ops->set_pitch(src, pitch);
271 src->ops->set_rom(src, select_rom(pitch));
272 src->ops->set_sf(src, convert_format(apcm->substream->runtime->format));
273 src->ops->set_pm(src, (src->ops->next_interleave(src) != NULL));
274
275 /* Get AMIXER resource */
276 n_amixer = (n_amixer < 2) ? 2 : n_amixer;
277 apcm->amixers = kzalloc(sizeof(void *)*n_amixer, GFP_KERNEL);
278 if (NULL == apcm->amixers) {
279 err = -ENOMEM;
280 goto error1;
281 }
282 mix_dsc.msr = atc->msr;
283 for (i = 0, apcm->n_amixer = 0; i < n_amixer; i++) {
284 err = amixer_mgr->get_amixer(amixer_mgr, &mix_dsc,
285 (struct amixer **)&apcm->amixers[i]);
286 if (err)
287 goto error1;
288
289 apcm->n_amixer++;
290 }
291
292 /* Set up device virtual mem map */
293 err = ct_map_audio_buffer(atc, apcm);
294 if (err < 0)
295 goto error1;
296
297 /* Connect resources */
298 src = apcm->src;
299 for (i = 0; i < n_amixer; i++) {
300 amixer = apcm->amixers[i];
301 spin_lock_irqsave(&atc->atc_lock, flags);
302 amixer->ops->setup(amixer, &src->rsc,
303 INIT_VOL, atc->pcm[i+device*2]);
304 spin_unlock_irqrestore(&atc->atc_lock, flags);
305 src = src->ops->next_interleave(src);
306 if (NULL == src)
307 src = apcm->src;
308 }
309
310 return 0;
311
312error1:
313 atc_pcm_release_resources(atc, apcm);
314 return err;
315}
316
317static int
318atc_pcm_release_resources(struct ct_atc *atc, struct ct_atc_pcm *apcm)
319{
320 struct src_mgr *src_mgr = atc->rsc_mgrs[SRC];
321 struct srcimp_mgr *srcimp_mgr = atc->rsc_mgrs[SRCIMP];
322 struct amixer_mgr *amixer_mgr = atc->rsc_mgrs[AMIXER];
323 struct sum_mgr *sum_mgr = atc->rsc_mgrs[SUM];
324 struct srcimp *srcimp = NULL;
325 int i = 0;
326
327 if (NULL != apcm->srcimps) {
328 for (i = 0; i < apcm->n_srcimp; i++) {
329 srcimp = apcm->srcimps[i];
330 srcimp->ops->unmap(srcimp);
331 srcimp_mgr->put_srcimp(srcimp_mgr, srcimp);
332 apcm->srcimps[i] = NULL;
333 }
334 kfree(apcm->srcimps);
335 apcm->srcimps = NULL;
336 }
337
338 if (NULL != apcm->srccs) {
339 for (i = 0; i < apcm->n_srcc; i++) {
340 src_mgr->put_src(src_mgr, apcm->srccs[i]);
341 apcm->srccs[i] = NULL;
342 }
343 kfree(apcm->srccs);
344 apcm->srccs = NULL;
345 }
346
347 if (NULL != apcm->amixers) {
348 for (i = 0; i < apcm->n_amixer; i++) {
349 amixer_mgr->put_amixer(amixer_mgr, apcm->amixers[i]);
350 apcm->amixers[i] = NULL;
351 }
352 kfree(apcm->amixers);
353 apcm->amixers = NULL;
354 }
355
356 if (NULL != apcm->mono) {
357 sum_mgr->put_sum(sum_mgr, apcm->mono);
358 apcm->mono = NULL;
359 }
360
361 if (NULL != apcm->src) {
362 src_mgr->put_src(src_mgr, apcm->src);
363 apcm->src = NULL;
364 }
365
366 if (NULL != apcm->vm_block) {
367 /* Undo device virtual mem map */
368 ct_unmap_audio_buffer(atc, apcm);
369 apcm->vm_block = NULL;
370 }
371
372 return 0;
373}
374
375static int atc_pcm_playback_start(struct ct_atc *atc, struct ct_atc_pcm *apcm)
376{
377 unsigned int max_cisz = 0;
378 struct src *src = apcm->src;
379
380 max_cisz = src->multi * src->rsc.msr;
381 max_cisz = 0x80 * (max_cisz < 8 ? max_cisz : 8);
382
383 src->ops->set_sa(src, apcm->vm_block->addr);
384 src->ops->set_la(src, apcm->vm_block->addr + apcm->vm_block->size);
385 src->ops->set_ca(src, apcm->vm_block->addr + max_cisz);
386 src->ops->set_cisz(src, max_cisz);
387
388 src->ops->set_bm(src, 1);
389 src->ops->set_state(src, SRC_STATE_INIT);
390 src->ops->commit_write(src);
391
392 return 0;
393}
394
395static int atc_pcm_stop(struct ct_atc *atc, struct ct_atc_pcm *apcm)
396{
397 struct src *src = NULL;
398 int i = 0;
399
400 src = apcm->src;
401 src->ops->set_bm(src, 0);
402 src->ops->set_state(src, SRC_STATE_OFF);
403 src->ops->commit_write(src);
404
405 if (NULL != apcm->srccs) {
406 for (i = 0; i < apcm->n_srcc; i++) {
407 src = apcm->srccs[i];
408 src->ops->set_bm(src, 0);
409 src->ops->set_state(src, SRC_STATE_OFF);
410 src->ops->commit_write(src);
411 }
412 }
413
414 apcm->started = 0;
415
416 return 0;
417}
418
419static int
420atc_pcm_playback_position(struct ct_atc *atc, struct ct_atc_pcm *apcm)
421{
422 struct src *src = apcm->src;
423 u32 size = 0, max_cisz = 0;
424 int position = 0;
425
426 position = src->ops->get_ca(src);
427
428 size = apcm->vm_block->size;
429 max_cisz = src->multi * src->rsc.msr;
430 max_cisz = 128 * (max_cisz < 8 ? max_cisz : 8);
431
432 return (position + size - max_cisz - apcm->vm_block->addr) % size;
433}
434
435struct src_node_conf_t {
436 unsigned int pitch;
437 unsigned int msr:8;
438 unsigned int mix_msr:8;
439 unsigned int imp_msr:8;
440 unsigned int vo:1;
441};
442
443static void setup_src_node_conf(struct ct_atc *atc, struct ct_atc_pcm *apcm,
444 struct src_node_conf_t *conf, int *n_srcc)
445{
446 unsigned int pitch = 0;
447
448 /* get pitch and convert to fixed-point 8.24 format. */
449 pitch = atc_get_pitch((atc->rsr * atc->msr),
450 apcm->substream->runtime->rate);
451 *n_srcc = 0;
452
453 if (1 == atc->msr) {
454 *n_srcc = apcm->substream->runtime->channels;
455 conf[0].pitch = pitch;
456 conf[0].mix_msr = conf[0].imp_msr = conf[0].msr = 1;
457 conf[0].vo = 1;
458 } else if (2 == atc->msr) {
459 if (0x8000000 < pitch) {
460 /* Need two-stage SRCs, SRCIMPs and
461 * AMIXERs for converting format */
462 conf[0].pitch = (atc->msr << 24);
463 conf[0].msr = conf[0].mix_msr = 1;
464 conf[0].imp_msr = atc->msr;
465 conf[0].vo = 0;
466 conf[1].pitch = atc_get_pitch(atc->rsr,
467 apcm->substream->runtime->rate);
468 conf[1].msr = conf[1].mix_msr = conf[1].imp_msr = 1;
469 conf[1].vo = 1;
470 *n_srcc = apcm->substream->runtime->channels * 2;
471 } else if (0x1000000 < pitch) {
472 /* Need one-stage SRCs, SRCIMPs and
473 * AMIXERs for converting format */
474 conf[0].pitch = pitch;
475 conf[0].msr = conf[0].mix_msr
476 = conf[0].imp_msr = atc->msr;
477 conf[0].vo = 1;
478 *n_srcc = apcm->substream->runtime->channels;
479 }
480 }
481}
482
483static int
484atc_pcm_capture_get_resources(struct ct_atc *atc, struct ct_atc_pcm *apcm)
485{
486 struct src_mgr *src_mgr = atc->rsc_mgrs[SRC];
487 struct srcimp_mgr *srcimp_mgr = atc->rsc_mgrs[SRCIMP];
488 struct amixer_mgr *amixer_mgr = atc->rsc_mgrs[AMIXER];
489 struct sum_mgr *sum_mgr = atc->rsc_mgrs[SUM];
490 struct src_desc src_dsc = {0};
491 struct src *src = NULL;
492 struct srcimp_desc srcimp_dsc = {0};
493 struct srcimp *srcimp = NULL;
494 struct amixer_desc mix_dsc = {0};
495 struct sum_desc sum_dsc = {0};
496 unsigned int pitch = 0;
497 int multi = 0, err = 0, i = 0;
498 int n_srcimp = 0, n_amixer = 0, n_srcc = 0, n_sum = 0;
499 struct src_node_conf_t src_node_conf[2] = {{0} };
500
Takashi Iwai822fa192009-06-02 14:12:17 +0200501 /* first release old resources */
502 atc->pcm_release_resources(atc, apcm);
503
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200504 /* The numbers of converting SRCs and SRCIMPs should be determined
505 * by pitch value. */
506
507 multi = apcm->substream->runtime->channels;
508
509 /* get pitch and convert to fixed-point 8.24 format. */
510 pitch = atc_get_pitch((atc->rsr * atc->msr),
511 apcm->substream->runtime->rate);
512
513 setup_src_node_conf(atc, apcm, src_node_conf, &n_srcc);
514 n_sum = (1 == multi) ? 1 : 0;
515 n_amixer += n_sum * 2 + n_srcc;
516 n_srcimp += n_srcc;
517 if ((multi > 1) && (0x8000000 >= pitch)) {
518 /* Need extra AMIXERs and SRCIMPs for special treatment
519 * of interleaved recording of conjugate channels */
520 n_amixer += multi * atc->msr;
521 n_srcimp += multi * atc->msr;
522 } else {
523 n_srcimp += multi;
524 }
525
526 if (n_srcc) {
527 apcm->srccs = kzalloc(sizeof(void *)*n_srcc, GFP_KERNEL);
528 if (NULL == apcm->srccs)
529 return -ENOMEM;
530 }
531 if (n_amixer) {
532 apcm->amixers = kzalloc(sizeof(void *)*n_amixer, GFP_KERNEL);
533 if (NULL == apcm->amixers) {
534 err = -ENOMEM;
535 goto error1;
536 }
537 }
538 apcm->srcimps = kzalloc(sizeof(void *)*n_srcimp, GFP_KERNEL);
539 if (NULL == apcm->srcimps) {
540 err = -ENOMEM;
541 goto error1;
542 }
543
544 /* Allocate SRCs for sample rate conversion if needed */
545 src_dsc.multi = 1;
546 src_dsc.mode = ARCRW;
547 for (i = 0, apcm->n_srcc = 0; i < n_srcc; i++) {
548 src_dsc.msr = src_node_conf[i/multi].msr;
549 err = src_mgr->get_src(src_mgr, &src_dsc,
550 (struct src **)&apcm->srccs[i]);
551 if (err)
552 goto error1;
553
554 src = apcm->srccs[i];
555 pitch = src_node_conf[i/multi].pitch;
556 src->ops->set_pitch(src, pitch);
557 src->ops->set_rom(src, select_rom(pitch));
558 src->ops->set_vo(src, src_node_conf[i/multi].vo);
559
560 apcm->n_srcc++;
561 }
562
563 /* Allocate AMIXERs for routing SRCs of conversion if needed */
564 for (i = 0, apcm->n_amixer = 0; i < n_amixer; i++) {
565 if (i < (n_sum*2))
566 mix_dsc.msr = atc->msr;
567 else if (i < (n_sum*2+n_srcc))
568 mix_dsc.msr = src_node_conf[(i-n_sum*2)/multi].mix_msr;
569 else
570 mix_dsc.msr = 1;
571
572 err = amixer_mgr->get_amixer(amixer_mgr, &mix_dsc,
573 (struct amixer **)&apcm->amixers[i]);
574 if (err)
575 goto error1;
576
577 apcm->n_amixer++;
578 }
579
580 /* Allocate a SUM resource to mix all input channels together */
581 sum_dsc.msr = atc->msr;
582 err = sum_mgr->get_sum(sum_mgr, &sum_dsc, (struct sum **)&apcm->mono);
583 if (err)
584 goto error1;
585
586 pitch = atc_get_pitch((atc->rsr * atc->msr),
587 apcm->substream->runtime->rate);
588 /* Allocate SRCIMP resources */
589 for (i = 0, apcm->n_srcimp = 0; i < n_srcimp; i++) {
590 if (i < (n_srcc))
591 srcimp_dsc.msr = src_node_conf[i/multi].imp_msr;
592 else if (1 == multi)
593 srcimp_dsc.msr = (pitch <= 0x8000000) ? atc->msr : 1;
594 else
595 srcimp_dsc.msr = 1;
596
597 err = srcimp_mgr->get_srcimp(srcimp_mgr, &srcimp_dsc, &srcimp);
598 if (err)
599 goto error1;
600
601 apcm->srcimps[i] = srcimp;
602 apcm->n_srcimp++;
603 }
604
605 /* Allocate a SRC for writing data to host memory */
606 src_dsc.multi = apcm->substream->runtime->channels;
607 src_dsc.msr = 1;
608 src_dsc.mode = MEMWR;
609 err = src_mgr->get_src(src_mgr, &src_dsc, (struct src **)&apcm->src);
610 if (err)
611 goto error1;
612
613 src = apcm->src;
614 src->ops->set_pitch(src, pitch);
615
616 /* Set up device virtual mem map */
617 err = ct_map_audio_buffer(atc, apcm);
618 if (err < 0)
619 goto error1;
620
621 return 0;
622
623error1:
624 atc_pcm_release_resources(atc, apcm);
625 return err;
626}
627
628static int atc_pcm_capture_prepare(struct ct_atc *atc, struct ct_atc_pcm *apcm)
629{
630 struct src *src = NULL;
631 struct amixer *amixer = NULL;
632 struct srcimp *srcimp = NULL;
633 struct ct_mixer *mixer = atc->mixer;
634 struct sum *mono = NULL;
635 struct rsc *out_ports[8] = {NULL};
636 int err = 0, i = 0, j = 0, n_sum = 0, multi = 0;
637 unsigned int pitch = 0;
638 int mix_base = 0, imp_base = 0;
639
640 if (NULL != apcm->src) {
641 /* Prepared pcm capture */
642 return 0;
643 }
644
645 /* Get needed resources. */
646 err = atc_pcm_capture_get_resources(atc, apcm);
647 if (err)
648 return err;
649
650 /* Connect resources */
651 mixer->get_output_ports(mixer, MIX_PCMO_FRONT,
652 &out_ports[0], &out_ports[1]);
653
654 multi = apcm->substream->runtime->channels;
655 if (1 == multi) {
656 mono = apcm->mono;
657 for (i = 0; i < 2; i++) {
658 amixer = apcm->amixers[i];
659 amixer->ops->setup(amixer, out_ports[i],
660 MONO_SUM_SCALE, mono);
661 }
662 out_ports[0] = &mono->rsc;
663 n_sum = 1;
664 mix_base = n_sum * 2;
665 }
666
667 for (i = 0; i < apcm->n_srcc; i++) {
668 src = apcm->srccs[i];
669 srcimp = apcm->srcimps[imp_base+i];
670 amixer = apcm->amixers[mix_base+i];
671 srcimp->ops->map(srcimp, src, out_ports[i%multi]);
672 amixer->ops->setup(amixer, &src->rsc, INIT_VOL, NULL);
673 out_ports[i%multi] = &amixer->rsc;
674 }
675
676 pitch = atc_get_pitch((atc->rsr * atc->msr),
677 apcm->substream->runtime->rate);
678
679 if ((multi > 1) && (pitch <= 0x8000000)) {
680 /* Special connection for interleaved
681 * recording with conjugate channels */
682 for (i = 0; i < multi; i++) {
683 out_ports[i]->ops->master(out_ports[i]);
684 for (j = 0; j < atc->msr; j++) {
685 amixer = apcm->amixers[apcm->n_srcc+j*multi+i];
686 amixer->ops->set_input(amixer, out_ports[i]);
687 amixer->ops->set_scale(amixer, INIT_VOL);
688 amixer->ops->set_sum(amixer, NULL);
689 amixer->ops->commit_raw_write(amixer);
690 out_ports[i]->ops->next_conj(out_ports[i]);
691
692 srcimp = apcm->srcimps[apcm->n_srcc+j*multi+i];
693 srcimp->ops->map(srcimp, apcm->src,
694 &amixer->rsc);
695 }
696 }
697 } else {
698 for (i = 0; i < multi; i++) {
699 srcimp = apcm->srcimps[apcm->n_srcc+i];
700 srcimp->ops->map(srcimp, apcm->src, out_ports[i]);
701 }
702 }
703
704 return 0;
705}
706
707static int atc_pcm_capture_start(struct ct_atc *atc, struct ct_atc_pcm *apcm)
708{
709 struct src *src = NULL;
710 struct src_mgr *src_mgr = atc->rsc_mgrs[SRC];
711 int i = 0, multi = 0;
712
713 if (apcm->started)
714 return 0;
715
716 apcm->started = 1;
717 multi = apcm->substream->runtime->channels;
718 /* Set up converting SRCs */
719 for (i = 0; i < apcm->n_srcc; i++) {
720 src = apcm->srccs[i];
721 src->ops->set_pm(src, ((i%multi) != (multi-1)));
722 src_mgr->src_disable(src_mgr, src);
723 }
724
725 /* Set up recording SRC */
726 src = apcm->src;
727 src->ops->set_sf(src, convert_format(apcm->substream->runtime->format));
728 src->ops->set_sa(src, apcm->vm_block->addr);
729 src->ops->set_la(src, apcm->vm_block->addr + apcm->vm_block->size);
730 src->ops->set_ca(src, apcm->vm_block->addr);
731 src_mgr->src_disable(src_mgr, src);
732
733 /* Disable relevant SRCs firstly */
734 src_mgr->commit_write(src_mgr);
735
736 /* Enable SRCs respectively */
737 for (i = 0; i < apcm->n_srcc; i++) {
738 src = apcm->srccs[i];
739 src->ops->set_state(src, SRC_STATE_RUN);
740 src->ops->commit_write(src);
741 src_mgr->src_enable_s(src_mgr, src);
742 }
743 src = apcm->src;
744 src->ops->set_bm(src, 1);
745 src->ops->set_state(src, SRC_STATE_RUN);
746 src->ops->commit_write(src);
747 src_mgr->src_enable_s(src_mgr, src);
748
749 /* Enable relevant SRCs synchronously */
750 src_mgr->commit_write(src_mgr);
751
752 return 0;
753}
754
755static int
756atc_pcm_capture_position(struct ct_atc *atc, struct ct_atc_pcm *apcm)
757{
758 struct src *src = apcm->src;
759
760 return src->ops->get_ca(src) - apcm->vm_block->addr;
761}
762
763static int spdif_passthru_playback_get_resources(struct ct_atc *atc,
764 struct ct_atc_pcm *apcm)
765{
766 struct src_mgr *src_mgr = atc->rsc_mgrs[SRC];
767 struct amixer_mgr *amixer_mgr = atc->rsc_mgrs[AMIXER];
768 struct src_desc desc = {0};
769 struct amixer_desc mix_dsc = {0};
770 struct src *src = NULL;
771 int err = 0;
772 int n_amixer = apcm->substream->runtime->channels, i = 0;
773 unsigned int pitch = 0, rsr = atc->pll_rate;
774
Takashi Iwai822fa192009-06-02 14:12:17 +0200775 /* first release old resources */
776 atc->pcm_release_resources(atc, apcm);
777
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200778 /* Get SRC resource */
779 desc.multi = apcm->substream->runtime->channels;
780 desc.msr = 1;
781 while (apcm->substream->runtime->rate > (rsr * desc.msr))
782 desc.msr <<= 1;
783
784 desc.mode = MEMRD;
785 err = src_mgr->get_src(src_mgr, &desc, (struct src **)&apcm->src);
786 if (err)
787 goto error1;
788
789 pitch = atc_get_pitch(apcm->substream->runtime->rate, (rsr * desc.msr));
790 src = apcm->src;
791 src->ops->set_pitch(src, pitch);
792 src->ops->set_rom(src, select_rom(pitch));
793 src->ops->set_sf(src, convert_format(apcm->substream->runtime->format));
794 src->ops->set_pm(src, (src->ops->next_interleave(src) != NULL));
795 src->ops->set_bp(src, 1);
796
797 /* Get AMIXER resource */
798 n_amixer = (n_amixer < 2) ? 2 : n_amixer;
799 apcm->amixers = kzalloc(sizeof(void *)*n_amixer, GFP_KERNEL);
800 if (NULL == apcm->amixers) {
801 err = -ENOMEM;
802 goto error1;
803 }
804 mix_dsc.msr = desc.msr;
805 for (i = 0, apcm->n_amixer = 0; i < n_amixer; i++) {
806 err = amixer_mgr->get_amixer(amixer_mgr, &mix_dsc,
807 (struct amixer **)&apcm->amixers[i]);
808 if (err)
809 goto error1;
810
811 apcm->n_amixer++;
812 }
813
814 /* Set up device virtual mem map */
815 err = ct_map_audio_buffer(atc, apcm);
816 if (err < 0)
817 goto error1;
818
819 return 0;
820
821error1:
822 atc_pcm_release_resources(atc, apcm);
823 return err;
824}
825
826static int
827spdif_passthru_playback_setup(struct ct_atc *atc, struct ct_atc_pcm *apcm)
828{
829 struct dao *dao = container_of(atc->daios[SPDIFOO], struct dao, daio);
830 unsigned long flags;
831 unsigned int rate = apcm->substream->runtime->rate;
832 unsigned int status = 0;
833 int err = 0;
834 unsigned char iec958_con_fs = 0;
835
836 switch (rate) {
837 case 48000:
838 iec958_con_fs = IEC958_AES3_CON_FS_48000;
839 break;
840 case 44100:
841 iec958_con_fs = IEC958_AES3_CON_FS_44100;
842 break;
843 case 32000:
844 iec958_con_fs = IEC958_AES3_CON_FS_32000;
845 break;
846 default:
847 return -ENOENT;
848 }
849
850 spin_lock_irqsave(&atc->atc_lock, flags);
851 dao->ops->get_spos(dao, &status);
852 if (((status >> 24) & IEC958_AES3_CON_FS) != iec958_con_fs) {
853 status &= ((~IEC958_AES3_CON_FS) << 24);
854 status |= (iec958_con_fs << 24);
855 dao->ops->set_spos(dao, status);
856 dao->ops->commit_write(dao);
857 }
858 if ((rate != atc->pll_rate) && (32000 != rate)) {
859 err = ((struct hw *)atc->hw)->pll_init(atc->hw, rate);
860 atc->pll_rate = err ? 0 : rate;
861 }
862 spin_unlock_irqrestore(&atc->atc_lock, flags);
863
864 return err;
865}
866
867static int
868spdif_passthru_playback_prepare(struct ct_atc *atc, struct ct_atc_pcm *apcm)
869{
870 struct src *src = NULL;
871 struct amixer *amixer = NULL;
872 struct dao *dao = NULL;
873 int err = 0;
874 int i = 0;
875 unsigned long flags;
876
877 if (NULL != apcm->src)
878 return 0;
879
880 /* Configure SPDIFOO and PLL to passthrough mode;
881 * determine pll_rate. */
882 err = spdif_passthru_playback_setup(atc, apcm);
883 if (err)
884 return err;
885
886 /* Get needed resources. */
887 err = spdif_passthru_playback_get_resources(atc, apcm);
888 if (err)
889 return err;
890
891 /* Connect resources */
892 src = apcm->src;
893 for (i = 0; i < apcm->n_amixer; i++) {
894 amixer = apcm->amixers[i];
895 amixer->ops->setup(amixer, &src->rsc, INIT_VOL, NULL);
896 src = src->ops->next_interleave(src);
897 if (NULL == src)
898 src = apcm->src;
899 }
900 /* Connect to SPDIFOO */
901 spin_lock_irqsave(&atc->atc_lock, flags);
902 dao = container_of(atc->daios[SPDIFOO], struct dao, daio);
903 amixer = apcm->amixers[0];
904 dao->ops->set_left_input(dao, &amixer->rsc);
905 amixer = apcm->amixers[1];
906 dao->ops->set_right_input(dao, &amixer->rsc);
907 spin_unlock_irqrestore(&atc->atc_lock, flags);
908
909 return 0;
910}
911
912static int atc_select_line_in(struct ct_atc *atc)
913{
914 struct hw *hw = atc->hw;
915 struct ct_mixer *mixer = atc->mixer;
916 struct src *src = NULL;
917
918 if (hw->is_adc_source_selected(hw, ADC_LINEIN))
919 return 0;
920
921 mixer->set_input_left(mixer, MIX_MIC_IN, NULL);
922 mixer->set_input_right(mixer, MIX_MIC_IN, NULL);
923
924 hw->select_adc_source(hw, ADC_LINEIN);
925
926 src = atc->srcs[2];
927 mixer->set_input_left(mixer, MIX_LINE_IN, &src->rsc);
928 src = atc->srcs[3];
929 mixer->set_input_right(mixer, MIX_LINE_IN, &src->rsc);
930
931 return 0;
932}
933
934static int atc_select_mic_in(struct ct_atc *atc)
935{
936 struct hw *hw = atc->hw;
937 struct ct_mixer *mixer = atc->mixer;
938 struct src *src = NULL;
939
940 if (hw->is_adc_source_selected(hw, ADC_MICIN))
941 return 0;
942
943 mixer->set_input_left(mixer, MIX_LINE_IN, NULL);
944 mixer->set_input_right(mixer, MIX_LINE_IN, NULL);
945
946 hw->select_adc_source(hw, ADC_MICIN);
947
948 src = atc->srcs[2];
949 mixer->set_input_left(mixer, MIX_MIC_IN, &src->rsc);
950 src = atc->srcs[3];
951 mixer->set_input_right(mixer, MIX_MIC_IN, &src->rsc);
952
953 return 0;
954}
955
956static int atc_have_digit_io_switch(struct ct_atc *atc)
957{
958 struct hw *hw = atc->hw;
959
960 return hw->have_digit_io_switch(hw);
961}
962
963static int atc_select_digit_io(struct ct_atc *atc)
964{
965 struct hw *hw = atc->hw;
966
967 if (hw->is_adc_source_selected(hw, ADC_NONE))
968 return 0;
969
970 hw->select_adc_source(hw, ADC_NONE);
971
972 return 0;
973}
974
975static int atc_daio_unmute(struct ct_atc *atc, unsigned char state, int type)
976{
977 struct daio_mgr *daio_mgr = atc->rsc_mgrs[DAIO];
978
979 if (state)
980 daio_mgr->daio_enable(daio_mgr, atc->daios[type]);
981 else
982 daio_mgr->daio_disable(daio_mgr, atc->daios[type]);
983
984 daio_mgr->commit_write(daio_mgr);
985
986 return 0;
987}
988
989static int
990atc_dao_get_status(struct ct_atc *atc, unsigned int *status, int type)
991{
992 struct dao *dao = container_of(atc->daios[type], struct dao, daio);
993 return dao->ops->get_spos(dao, status);
994}
995
996static int
997atc_dao_set_status(struct ct_atc *atc, unsigned int status, int type)
998{
999 struct dao *dao = container_of(atc->daios[type], struct dao, daio);
1000
1001 dao->ops->set_spos(dao, status);
1002 dao->ops->commit_write(dao);
1003 return 0;
1004}
1005
1006static int atc_line_front_unmute(struct ct_atc *atc, unsigned char state)
1007{
1008 return atc_daio_unmute(atc, state, LINEO1);
1009}
1010
1011static int atc_line_surround_unmute(struct ct_atc *atc, unsigned char state)
1012{
1013 return atc_daio_unmute(atc, state, LINEO4);
1014}
1015
1016static int atc_line_clfe_unmute(struct ct_atc *atc, unsigned char state)
1017{
1018 return atc_daio_unmute(atc, state, LINEO3);
1019}
1020
1021static int atc_line_rear_unmute(struct ct_atc *atc, unsigned char state)
1022{
1023 return atc_daio_unmute(atc, state, LINEO2);
1024}
1025
1026static int atc_line_in_unmute(struct ct_atc *atc, unsigned char state)
1027{
1028 return atc_daio_unmute(atc, state, LINEIM);
1029}
1030
1031static int atc_spdif_out_unmute(struct ct_atc *atc, unsigned char state)
1032{
1033 return atc_daio_unmute(atc, state, SPDIFOO);
1034}
1035
1036static int atc_spdif_in_unmute(struct ct_atc *atc, unsigned char state)
1037{
1038 return atc_daio_unmute(atc, state, SPDIFIO);
1039}
1040
1041static int atc_spdif_out_get_status(struct ct_atc *atc, unsigned int *status)
1042{
1043 return atc_dao_get_status(atc, status, SPDIFOO);
1044}
1045
1046static int atc_spdif_out_set_status(struct ct_atc *atc, unsigned int status)
1047{
1048 return atc_dao_set_status(atc, status, SPDIFOO);
1049}
1050
1051static int atc_spdif_out_passthru(struct ct_atc *atc, unsigned char state)
1052{
1053 unsigned long flags;
1054 struct dao_desc da_dsc = {0};
1055 struct dao *dao = NULL;
1056 int err = 0;
1057 struct ct_mixer *mixer = atc->mixer;
1058 struct rsc *rscs[2] = {NULL};
1059 unsigned int spos = 0;
1060
1061 spin_lock_irqsave(&atc->atc_lock, flags);
1062 dao = container_of(atc->daios[SPDIFOO], struct dao, daio);
1063 da_dsc.msr = state ? 1 : atc->msr;
1064 da_dsc.passthru = state ? 1 : 0;
1065 err = dao->ops->reinit(dao, &da_dsc);
1066 if (state) {
1067 spos = IEC958_DEFAULT_CON;
1068 } else {
1069 mixer->get_output_ports(mixer, MIX_SPDIF_OUT,
1070 &rscs[0], &rscs[1]);
1071 dao->ops->set_left_input(dao, rscs[0]);
1072 dao->ops->set_right_input(dao, rscs[1]);
1073 /* Restore PLL to atc->rsr if needed. */
1074 if (atc->pll_rate != atc->rsr) {
1075 err = ((struct hw *)atc->hw)->pll_init(atc->hw,
1076 atc->rsr);
1077 atc->pll_rate = err ? 0 : atc->rsr;
1078 }
1079 }
1080 dao->ops->set_spos(dao, spos);
1081 dao->ops->commit_write(dao);
1082 spin_unlock_irqrestore(&atc->atc_lock, flags);
1083
1084 return err;
1085}
1086
1087static int ct_atc_destroy(struct ct_atc *atc)
1088{
1089 struct daio_mgr *daio_mgr = NULL;
1090 struct dao *dao = NULL;
1091 struct dai *dai = NULL;
1092 struct daio *daio = NULL;
1093 struct sum_mgr *sum_mgr = NULL;
1094 struct src_mgr *src_mgr = NULL;
1095 struct srcimp_mgr *srcimp_mgr = NULL;
1096 struct srcimp *srcimp = NULL;
1097 struct ct_mixer *mixer = NULL;
1098 int i = 0;
1099
1100 if (NULL == atc)
1101 return 0;
1102
1103 /* Stop hardware and disable all interrupts */
1104 if (NULL != atc->hw)
1105 ((struct hw *)atc->hw)->card_stop(atc->hw);
1106
1107 /* Destroy internal mixer objects */
1108 if (NULL != atc->mixer) {
1109 mixer = atc->mixer;
1110 mixer->set_input_left(mixer, MIX_LINE_IN, NULL);
1111 mixer->set_input_right(mixer, MIX_LINE_IN, NULL);
1112 mixer->set_input_left(mixer, MIX_MIC_IN, NULL);
1113 mixer->set_input_right(mixer, MIX_MIC_IN, NULL);
1114 mixer->set_input_left(mixer, MIX_SPDIF_IN, NULL);
1115 mixer->set_input_right(mixer, MIX_SPDIF_IN, NULL);
1116 ct_mixer_destroy(atc->mixer);
1117 }
1118
1119 if (NULL != atc->daios) {
1120 daio_mgr = (struct daio_mgr *)atc->rsc_mgrs[DAIO];
1121 for (i = 0; i < atc->n_daio; i++) {
1122 daio = atc->daios[i];
1123 if (daio->type < LINEIM) {
1124 dao = container_of(daio, struct dao, daio);
1125 dao->ops->clear_left_input(dao);
1126 dao->ops->clear_right_input(dao);
1127 } else {
1128 dai = container_of(daio, struct dai, daio);
1129 /* some thing to do for dai ... */
1130 }
1131 daio_mgr->put_daio(daio_mgr, daio);
1132 }
1133 kfree(atc->daios);
1134 }
1135
1136 if (NULL != atc->pcm) {
1137 sum_mgr = atc->rsc_mgrs[SUM];
1138 for (i = 0; i < atc->n_pcm; i++)
1139 sum_mgr->put_sum(sum_mgr, atc->pcm[i]);
1140
1141 kfree(atc->pcm);
1142 }
1143
1144 if (NULL != atc->srcs) {
1145 src_mgr = atc->rsc_mgrs[SRC];
1146 for (i = 0; i < atc->n_src; i++)
1147 src_mgr->put_src(src_mgr, atc->srcs[i]);
1148
1149 kfree(atc->srcs);
1150 }
1151
1152 if (NULL != atc->srcimps) {
1153 srcimp_mgr = atc->rsc_mgrs[SRCIMP];
1154 for (i = 0; i < atc->n_srcimp; i++) {
1155 srcimp = atc->srcimps[i];
1156 srcimp->ops->unmap(srcimp);
1157 srcimp_mgr->put_srcimp(srcimp_mgr, atc->srcimps[i]);
1158 }
1159 kfree(atc->srcimps);
1160 }
1161
1162 for (i = 0; i < NUM_RSCTYP; i++) {
1163 if ((NULL != rsc_mgr_funcs[i].destroy) &&
1164 (NULL != atc->rsc_mgrs[i]))
1165 rsc_mgr_funcs[i].destroy(atc->rsc_mgrs[i]);
1166
1167 }
1168
1169 if (NULL != atc->hw)
1170 destroy_hw_obj((struct hw *)atc->hw);
1171
1172 /* Destroy device virtual memory manager object */
1173 if (NULL != atc->vm) {
1174 ct_vm_destroy(atc->vm);
1175 atc->vm = NULL;
1176 }
1177
1178 kfree(atc);
1179
1180 return 0;
1181}
1182
1183static int atc_dev_free(struct snd_device *dev)
1184{
1185 struct ct_atc *atc = dev->device_data;
1186 return ct_atc_destroy(atc);
1187}
1188
1189static int atc_identify_card(struct ct_atc *atc)
1190{
Takashi Iwai35b053b2009-05-14 15:17:16 +02001191 u16 subsys;
1192 u8 revision;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001193 struct pci_dev *pci = atc->pci;
1194 const struct ct_atc_chip_details *d;
1195 enum CTCARDS i;
1196
Takashi Iwai35b053b2009-05-14 15:17:16 +02001197 subsys = pci->subsystem_device;
1198 revision = pci->revision;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001199 atc->chip_details = NULL;
1200 atc->model = NUM_CTCARDS;
1201 for (d = atc_chip_details; d->vendor; d++) {
1202 if (d->vendor != pci->vendor || d->device != pci->device)
1203 continue;
1204
1205 if (NULL == d->sub_details) {
1206 atc->chip_details = d;
1207 break;
1208 }
1209 for (i = 0; i < NUM_CTCARDS; i++) {
1210 if ((d->sub_details[i].subsys == subsys) ||
1211 (((subsys & 0x6000) == 0x6000) &&
1212 ((d->sub_details[i].subsys & 0x6000) == 0x6000))) {
1213 atc->model = i;
1214 break;
1215 }
1216 }
1217 if (i >= NUM_CTCARDS)
1218 continue;
1219
1220 atc->chip_details = d;
1221 break;
1222 /* not take revision into consideration now */
1223 }
1224 if (!d->vendor)
1225 return -ENOENT;
1226
1227 return 0;
1228}
1229
1230static int ct_create_alsa_devs(struct ct_atc *atc)
1231{
1232 enum CTALSADEVS i;
1233 struct hw *hw = atc->hw;
1234 int err;
1235
1236 switch (hw->get_chip_type(hw)) {
1237 case ATC20K1:
1238 alsa_dev_funcs[MIXER].public_name = "20K1";
1239 break;
1240 case ATC20K2:
1241 alsa_dev_funcs[MIXER].public_name = "20K2";
1242 break;
1243 default:
1244 alsa_dev_funcs[MIXER].public_name = "Unknown";
1245 break;
1246 }
1247
1248 for (i = 0; i < NUM_CTALSADEVS; i++) {
1249 if (NULL == alsa_dev_funcs[i].create)
1250 continue;
1251
1252 err = alsa_dev_funcs[i].create(atc, i,
1253 alsa_dev_funcs[i].public_name);
1254 if (err) {
Takashi Iwaib3e0afe2009-05-14 15:19:30 +02001255 printk(KERN_ERR "ctxfi: "
1256 "Creating alsa device %d failed!\n", i);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001257 return err;
1258 }
1259 }
1260
1261 return 0;
1262}
1263
1264static int atc_create_hw_devs(struct ct_atc *atc)
1265{
1266 struct hw *hw = NULL;
1267 struct card_conf info = {0};
1268 int i = 0, err = 0;
1269
1270 err = create_hw_obj(atc->pci, &hw);
1271 if (err) {
1272 printk(KERN_ERR "Failed to create hw obj!!!\n");
1273 return err;
1274 }
1275 atc->hw = hw;
1276
1277 /* Initialize card hardware. */
1278 info.rsr = atc->rsr;
1279 info.msr = atc->msr;
1280 info.vm_pgt_phys = atc_get_ptp_phys(atc, 0);
1281 err = hw->card_init(hw, &info);
1282 if (err < 0)
1283 return err;
1284
1285 for (i = 0; i < NUM_RSCTYP; i++) {
1286 if (NULL == rsc_mgr_funcs[i].create)
1287 continue;
1288
1289 err = rsc_mgr_funcs[i].create(atc->hw, &atc->rsc_mgrs[i]);
1290 if (err) {
Takashi Iwaib3e0afe2009-05-14 15:19:30 +02001291 printk(KERN_ERR "ctxfi: "
1292 "Failed to create rsc_mgr %d!!!\n", i);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001293 return err;
1294 }
1295 }
1296
1297 return 0;
1298}
1299
1300static int atc_get_resources(struct ct_atc *atc)
1301{
1302 struct daio_desc da_desc = {0};
1303 struct daio_mgr *daio_mgr = NULL;
1304 struct src_desc src_dsc = {0};
1305 struct src_mgr *src_mgr = NULL;
1306 struct srcimp_desc srcimp_dsc = {0};
1307 struct srcimp_mgr *srcimp_mgr = NULL;
1308 struct sum_desc sum_dsc = {0};
1309 struct sum_mgr *sum_mgr = NULL;
1310 int err = 0, i = 0;
Takashi Iwai35b053b2009-05-14 15:17:16 +02001311 unsigned short subsys_id;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001312
1313 atc->daios = kzalloc(sizeof(void *)*(DAIONUM), GFP_KERNEL);
1314 if (NULL == atc->daios)
1315 return -ENOMEM;
1316
1317 atc->srcs = kzalloc(sizeof(void *)*(2*2), GFP_KERNEL);
1318 if (NULL == atc->srcs)
1319 return -ENOMEM;
1320
1321 atc->srcimps = kzalloc(sizeof(void *)*(2*2), GFP_KERNEL);
1322 if (NULL == atc->srcimps)
1323 return -ENOMEM;
1324
1325 atc->pcm = kzalloc(sizeof(void *)*(2*4), GFP_KERNEL);
1326 if (NULL == atc->pcm)
1327 return -ENOMEM;
1328
1329 daio_mgr = (struct daio_mgr *)atc->rsc_mgrs[DAIO];
1330 da_desc.msr = atc->msr;
1331 for (i = 0, atc->n_daio = 0; i < DAIONUM-1; i++) {
1332 da_desc.type = i;
1333 err = daio_mgr->get_daio(daio_mgr, &da_desc,
1334 (struct daio **)&atc->daios[i]);
1335 if (err) {
Takashi Iwaib3e0afe2009-05-14 15:19:30 +02001336 printk(KERN_ERR "ctxfi: Failed to get DAIO "
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001337 "resource %d!!!\n", i);
1338 return err;
1339 }
1340 atc->n_daio++;
1341 }
Takashi Iwai35b053b2009-05-14 15:17:16 +02001342 subsys_id = atc->pci->subsystem_device;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001343 if ((subsys_id == 0x0029) || (subsys_id == 0x0031)) {
1344 /* SB073x cards */
1345 da_desc.type = SPDIFI1;
1346 } else {
1347 da_desc.type = SPDIFIO;
1348 }
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 S/PDIF-in resource!!!\n");
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001353 return err;
1354 }
1355 atc->n_daio++;
1356
1357 src_mgr = atc->rsc_mgrs[SRC];
1358 src_dsc.multi = 1;
1359 src_dsc.msr = atc->msr;
1360 src_dsc.mode = ARCRW;
1361 for (i = 0, atc->n_src = 0; i < (2*2); i++) {
1362 err = src_mgr->get_src(src_mgr, &src_dsc,
1363 (struct src **)&atc->srcs[i]);
1364 if (err)
1365 return err;
1366
1367 atc->n_src++;
1368 }
1369
1370 srcimp_mgr = atc->rsc_mgrs[SRCIMP];
1371 srcimp_dsc.msr = 8; /* SRCIMPs for S/PDIFIn SRT */
1372 for (i = 0, atc->n_srcimp = 0; i < (2*1); i++) {
1373 err = srcimp_mgr->get_srcimp(srcimp_mgr, &srcimp_dsc,
1374 (struct srcimp **)&atc->srcimps[i]);
1375 if (err)
1376 return err;
1377
1378 atc->n_srcimp++;
1379 }
1380 srcimp_dsc.msr = 8; /* SRCIMPs for LINE/MICIn SRT */
1381 for (i = 0; i < (2*1); i++) {
1382 err = srcimp_mgr->get_srcimp(srcimp_mgr, &srcimp_dsc,
1383 (struct srcimp **)&atc->srcimps[2*1+i]);
1384 if (err)
1385 return err;
1386
1387 atc->n_srcimp++;
1388 }
1389
1390 sum_mgr = atc->rsc_mgrs[SUM];
1391 sum_dsc.msr = atc->msr;
1392 for (i = 0, atc->n_pcm = 0; i < (2*4); i++) {
1393 err = sum_mgr->get_sum(sum_mgr, &sum_dsc,
1394 (struct sum **)&atc->pcm[i]);
1395 if (err)
1396 return err;
1397
1398 atc->n_pcm++;
1399 }
1400
1401 err = ct_mixer_create(atc, (struct ct_mixer **)&atc->mixer);
1402 if (err) {
Takashi Iwaib3e0afe2009-05-14 15:19:30 +02001403 printk(KERN_ERR "ctxfi: Failed to create mixer obj!!!\n");
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001404 return err;
1405 }
1406
1407 return 0;
1408}
1409
1410static void
1411atc_connect_dai(struct src_mgr *src_mgr, struct dai *dai,
1412 struct src **srcs, struct srcimp **srcimps)
1413{
1414 struct rsc *rscs[2] = {NULL};
1415 struct src *src = NULL;
1416 struct srcimp *srcimp = NULL;
1417 int i = 0;
1418
1419 rscs[0] = &dai->daio.rscl;
1420 rscs[1] = &dai->daio.rscr;
1421 for (i = 0; i < 2; i++) {
1422 src = srcs[i];
1423 srcimp = srcimps[i];
1424 srcimp->ops->map(srcimp, src, rscs[i]);
1425 src_mgr->src_disable(src_mgr, src);
1426 }
1427
1428 src_mgr->commit_write(src_mgr); /* Actually disable SRCs */
1429
1430 src = srcs[0];
1431 src->ops->set_pm(src, 1);
1432 for (i = 0; i < 2; i++) {
1433 src = srcs[i];
1434 src->ops->set_state(src, SRC_STATE_RUN);
1435 src->ops->commit_write(src);
1436 src_mgr->src_enable_s(src_mgr, src);
1437 }
1438
1439 dai->ops->set_srt_srcl(dai, &(srcs[0]->rsc));
1440 dai->ops->set_srt_srcr(dai, &(srcs[1]->rsc));
1441
1442 dai->ops->set_enb_src(dai, 1);
1443 dai->ops->set_enb_srt(dai, 1);
1444 dai->ops->commit_write(dai);
1445
1446 src_mgr->commit_write(src_mgr); /* Synchronously enable SRCs */
1447}
1448
1449static void atc_connect_resources(struct ct_atc *atc)
1450{
1451 struct dai *dai = NULL;
1452 struct dao *dao = NULL;
1453 struct src *src = NULL;
1454 struct sum *sum = NULL;
1455 struct ct_mixer *mixer = NULL;
1456 struct rsc *rscs[2] = {NULL};
1457 int i = 0, j = 0;
1458
1459 mixer = atc->mixer;
1460
1461 for (i = MIX_WAVE_FRONT, j = LINEO1; i <= MIX_SPDIF_OUT; i++, j++) {
1462 mixer->get_output_ports(mixer, i, &rscs[0], &rscs[1]);
1463 dao = container_of(atc->daios[j], struct dao, daio);
1464 dao->ops->set_left_input(dao, rscs[0]);
1465 dao->ops->set_right_input(dao, rscs[1]);
1466 }
1467
1468 dai = container_of(atc->daios[LINEIM], struct dai, daio);
1469 atc_connect_dai(atc->rsc_mgrs[SRC], dai,
1470 (struct src **)&atc->srcs[2],
1471 (struct srcimp **)&atc->srcimps[2]);
1472 src = atc->srcs[2];
1473 mixer->set_input_left(mixer, MIX_LINE_IN, &src->rsc);
1474 src = atc->srcs[3];
1475 mixer->set_input_right(mixer, MIX_LINE_IN, &src->rsc);
1476
1477 dai = container_of(atc->daios[SPDIFIO], struct dai, daio);
1478 atc_connect_dai(atc->rsc_mgrs[SRC], dai,
1479 (struct src **)&atc->srcs[0],
1480 (struct srcimp **)&atc->srcimps[0]);
1481
1482 src = atc->srcs[0];
1483 mixer->set_input_left(mixer, MIX_SPDIF_IN, &src->rsc);
1484 src = atc->srcs[1];
1485 mixer->set_input_right(mixer, MIX_SPDIF_IN, &src->rsc);
1486
1487 for (i = MIX_PCMI_FRONT, j = 0; i <= MIX_PCMI_SURROUND; i++, j += 2) {
1488 sum = atc->pcm[j];
1489 mixer->set_input_left(mixer, i, &sum->rsc);
1490 sum = atc->pcm[j+1];
1491 mixer->set_input_right(mixer, i, &sum->rsc);
1492 }
1493}
1494
1495static void atc_set_ops(struct ct_atc *atc)
1496{
1497 /* Set operations */
1498 atc->map_audio_buffer = ct_map_audio_buffer;
1499 atc->unmap_audio_buffer = ct_unmap_audio_buffer;
1500 atc->pcm_playback_prepare = atc_pcm_playback_prepare;
1501 atc->pcm_release_resources = atc_pcm_release_resources;
1502 atc->pcm_playback_start = atc_pcm_playback_start;
1503 atc->pcm_playback_stop = atc_pcm_stop;
1504 atc->pcm_playback_position = atc_pcm_playback_position;
1505 atc->pcm_capture_prepare = atc_pcm_capture_prepare;
1506 atc->pcm_capture_start = atc_pcm_capture_start;
1507 atc->pcm_capture_stop = atc_pcm_stop;
1508 atc->pcm_capture_position = atc_pcm_capture_position;
1509 atc->spdif_passthru_playback_prepare = spdif_passthru_playback_prepare;
1510 atc->get_ptp_phys = atc_get_ptp_phys;
1511 atc->select_line_in = atc_select_line_in;
1512 atc->select_mic_in = atc_select_mic_in;
1513 atc->select_digit_io = atc_select_digit_io;
1514 atc->line_front_unmute = atc_line_front_unmute;
1515 atc->line_surround_unmute = atc_line_surround_unmute;
1516 atc->line_clfe_unmute = atc_line_clfe_unmute;
1517 atc->line_rear_unmute = atc_line_rear_unmute;
1518 atc->line_in_unmute = atc_line_in_unmute;
1519 atc->spdif_out_unmute = atc_spdif_out_unmute;
1520 atc->spdif_in_unmute = atc_spdif_in_unmute;
1521 atc->spdif_out_get_status = atc_spdif_out_get_status;
1522 atc->spdif_out_set_status = atc_spdif_out_set_status;
1523 atc->spdif_out_passthru = atc_spdif_out_passthru;
1524 atc->have_digit_io_switch = atc_have_digit_io_switch;
1525}
1526
1527/**
1528 * ct_atc_create - create and initialize a hardware manager
1529 * @card: corresponding alsa card object
1530 * @pci: corresponding kernel pci device object
1531 * @ratc: return created object address in it
1532 *
1533 * Creates and initializes a hardware manager.
1534 *
1535 * Creates kmallocated ct_atc structure. Initializes hardware.
1536 * Returns 0 if suceeds, or negative error code if fails.
1537 */
1538
1539int ct_atc_create(struct snd_card *card, struct pci_dev *pci,
1540 unsigned int rsr, unsigned int msr, struct ct_atc **ratc)
1541{
1542 struct ct_atc *atc = NULL;
1543 static struct snd_device_ops ops = {
1544 .dev_free = atc_dev_free,
1545 };
1546 int err = 0;
1547
1548 *ratc = NULL;
1549
1550 atc = kzalloc(sizeof(*atc), GFP_KERNEL);
1551 if (NULL == atc)
1552 return -ENOMEM;
1553
1554 atc->card = card;
1555 atc->pci = pci;
1556 atc->rsr = rsr;
1557 atc->msr = msr;
1558
1559 /* Set operations */
1560 atc_set_ops(atc);
1561
1562 spin_lock_init(&atc->atc_lock);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001563
1564 /* Find card model */
1565 err = atc_identify_card(atc);
1566 if (err < 0) {
1567 printk(KERN_ERR "ctatc: Card not recognised\n");
1568 goto error1;
1569 }
1570
1571 /* Set up device virtual memory management object */
1572 err = ct_vm_create(&atc->vm);
1573 if (err < 0)
1574 goto error1;
1575
1576 /* Create all atc hw devices */
1577 err = atc_create_hw_devs(atc);
1578 if (err < 0)
1579 goto error1;
1580
1581 /* Get resources */
1582 err = atc_get_resources(atc);
1583 if (err < 0)
1584 goto error1;
1585
1586 /* Build topology */
1587 atc_connect_resources(atc);
1588
1589 atc->create_alsa_devs = ct_create_alsa_devs;
1590
1591 err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, atc, &ops);
1592 if (err < 0)
1593 goto error1;
1594
1595 snd_card_set_dev(card, &pci->dev);
1596
1597 *ratc = atc;
1598 return 0;
1599
1600error1:
1601 ct_atc_destroy(atc);
Takashi Iwaib3e0afe2009-05-14 15:19:30 +02001602 printk(KERN_ERR "ctxfi: Something wrong!!!\n");
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001603 return err;
1604}
1605