blob: af632bd08323feaf0f9ab2d3098375e12f38d46b [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 "ctsrc.h"
22#include "ctamixer.h"
23#include "ctdaio.h"
Takashi Iwaib7bbf872009-06-05 16:11:07 +020024#include "cttimer.h"
Wai Yew CHAY8cc72362009-05-14 08:05:58 +020025#include <linux/delay.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
Wai Yew CHAY8cc72362009-05-14 08:05:58 +020027#include <sound/pcm.h>
28#include <sound/control.h>
29#include <sound/asoundef.h>
30
31#define MONO_SUM_SCALE 0x19a8 /* 2^(-0.5) in 14-bit floating format */
Wai Yew CHAY8cc72362009-05-14 08:05:58 +020032#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
Bill Pembertone23e7a12012-12-06 12:35:10 -050041static struct snd_pci_quirk subsys_20k1_list[] = {
Takashi Iwai94701952009-06-08 18:10:32 +020042 SND_PCI_QUIRK(PCI_VENDOR_ID_CREATIVE, 0x0022, "SB055x", CTSB055X),
43 SND_PCI_QUIRK(PCI_VENDOR_ID_CREATIVE, 0x002f, "SB055x", CTSB055X),
44 SND_PCI_QUIRK(PCI_VENDOR_ID_CREATIVE, 0x0029, "SB073x", CTSB073X),
45 SND_PCI_QUIRK(PCI_VENDOR_ID_CREATIVE, 0x0031, "SB073x", CTSB073X),
Takashi Iwai09521d22009-06-08 18:29:38 +020046 SND_PCI_QUIRK_MASK(PCI_VENDOR_ID_CREATIVE, 0xf000, 0x6000,
47 "UAA", CTUAA),
Takashi Iwai94701952009-06-08 18:10:32 +020048 { } /* terminator */
Wai Yew CHAY8cc72362009-05-14 08:05:58 +020049};
50
Bill Pembertone23e7a12012-12-06 12:35:10 -050051static struct snd_pci_quirk subsys_20k2_list[] = {
Takashi Iwai94701952009-06-08 18:10:32 +020052 SND_PCI_QUIRK(PCI_VENDOR_ID_CREATIVE, PCI_SUBDEVICE_ID_CREATIVE_SB0760,
53 "SB0760", CTSB0760),
Harry Butterworth55309212011-06-11 16:02:06 +080054 SND_PCI_QUIRK(PCI_VENDOR_ID_CREATIVE, PCI_SUBDEVICE_ID_CREATIVE_SB1270,
55 "SB1270", CTSB1270),
Takashi Iwai94701952009-06-08 18:10:32 +020056 SND_PCI_QUIRK(PCI_VENDOR_ID_CREATIVE, PCI_SUBDEVICE_ID_CREATIVE_SB08801,
57 "SB0880", CTSB0880),
58 SND_PCI_QUIRK(PCI_VENDOR_ID_CREATIVE, PCI_SUBDEVICE_ID_CREATIVE_SB08802,
59 "SB0880", CTSB0880),
60 SND_PCI_QUIRK(PCI_VENDOR_ID_CREATIVE, PCI_SUBDEVICE_ID_CREATIVE_SB08803,
61 "SB0880", CTSB0880),
Takashi Iwai09521d22009-06-08 18:29:38 +020062 SND_PCI_QUIRK_MASK(PCI_VENDOR_ID_CREATIVE, 0xf000,
63 PCI_SUBDEVICE_ID_CREATIVE_HENDRIX, "HENDRIX",
64 CTHENDRIX),
Takashi Iwai94701952009-06-08 18:10:32 +020065 { } /* terminator */
66};
67
68static const char *ct_subsys_name[NUM_CTCARDS] = {
Takashi Iwaia8f43102009-06-22 07:36:52 +020069 /* 20k1 models */
Takashi Iwai94701952009-06-08 18:10:32 +020070 [CTSB055X] = "SB055x",
71 [CTSB073X] = "SB073x",
Takashi Iwai94701952009-06-08 18:10:32 +020072 [CTUAA] = "UAA",
73 [CT20K1_UNKNOWN] = "Unknown",
Takashi Iwaia8f43102009-06-22 07:36:52 +020074 /* 20k2 models */
75 [CTSB0760] = "SB076x",
Takashi Iwai94701952009-06-08 18:10:32 +020076 [CTHENDRIX] = "Hendrix",
77 [CTSB0880] = "SB0880",
Harry Butterworth55309212011-06-11 16:02:06 +080078 [CTSB1270] = "SB1270",
Takashi Iwaia8f43102009-06-22 07:36:52 +020079 [CT20K2_UNKNOWN] = "Unknown",
Wai Yew CHAY8cc72362009-05-14 08:05:58 +020080};
81
82static struct {
83 int (*create)(struct ct_atc *atc,
84 enum CTALSADEVS device, const char *device_name);
85 int (*destroy)(void *alsa_dev);
86 const char *public_name;
87} alsa_dev_funcs[NUM_CTALSADEVS] = {
88 [FRONT] = { .create = ct_alsa_pcm_create,
89 .destroy = NULL,
90 .public_name = "Front/WaveIn"},
Wai Yew CHAY8cc72362009-05-14 08:05:58 +020091 [SURROUND] = { .create = ct_alsa_pcm_create,
92 .destroy = NULL,
93 .public_name = "Surround"},
Takashi Iwai8372d4982009-06-02 14:27:56 +020094 [CLFE] = { .create = ct_alsa_pcm_create,
95 .destroy = NULL,
96 .public_name = "Center/LFE"},
97 [SIDE] = { .create = ct_alsa_pcm_create,
98 .destroy = NULL,
99 .public_name = "Side"},
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200100 [IEC958] = { .create = ct_alsa_pcm_create,
101 .destroy = NULL,
102 .public_name = "IEC958 Non-audio"},
103
104 [MIXER] = { .create = ct_alsa_mix_create,
105 .destroy = NULL,
106 .public_name = "Mixer"}
107};
108
109typedef int (*create_t)(void *, void **);
110typedef int (*destroy_t)(void *);
111
112static struct {
113 int (*create)(void *hw, void **rmgr);
114 int (*destroy)(void *mgr);
115} rsc_mgr_funcs[NUM_RSCTYP] = {
116 [SRC] = { .create = (create_t)src_mgr_create,
117 .destroy = (destroy_t)src_mgr_destroy },
118 [SRCIMP] = { .create = (create_t)srcimp_mgr_create,
119 .destroy = (destroy_t)srcimp_mgr_destroy },
120 [AMIXER] = { .create = (create_t)amixer_mgr_create,
121 .destroy = (destroy_t)amixer_mgr_destroy },
122 [SUM] = { .create = (create_t)sum_mgr_create,
123 .destroy = (destroy_t)sum_mgr_destroy },
124 [DAIO] = { .create = (create_t)daio_mgr_create,
125 .destroy = (destroy_t)daio_mgr_destroy }
126};
127
128static int
129atc_pcm_release_resources(struct ct_atc *atc, struct ct_atc_pcm *apcm);
130
131/* *
132 * Only mono and interleaved modes are supported now.
133 * Always allocates a contiguous channel block.
134 * */
135
136static int ct_map_audio_buffer(struct ct_atc *atc, struct ct_atc_pcm *apcm)
137{
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200138 struct snd_pcm_runtime *runtime;
139 struct ct_vm *vm;
140
Takashi Iwai35ebf6e2009-07-22 17:12:34 +0200141 if (!apcm->substream)
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200142 return 0;
143
144 runtime = apcm->substream->runtime;
145 vm = atc->vm;
146
Takashi Iwaic76157d2009-06-02 15:26:19 +0200147 apcm->vm_block = vm->map(vm, apcm->substream, runtime->dma_bytes);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200148
Takashi Iwai35ebf6e2009-07-22 17:12:34 +0200149 if (!apcm->vm_block)
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200150 return -ENOENT;
151
152 return 0;
153}
154
155static void ct_unmap_audio_buffer(struct ct_atc *atc, struct ct_atc_pcm *apcm)
156{
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200157 struct ct_vm *vm;
158
Takashi Iwai35ebf6e2009-07-22 17:12:34 +0200159 if (!apcm->vm_block)
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200160 return;
161
162 vm = atc->vm;
163
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200164 vm->unmap(vm, apcm->vm_block);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200165
166 apcm->vm_block = NULL;
167}
168
169static unsigned long atc_get_ptp_phys(struct ct_atc *atc, int index)
170{
Jaroslav Kysela21956b62010-02-02 19:58:25 +0100171 return atc->vm->get_ptp_phys(atc->vm, index);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200172}
173
174static unsigned int convert_format(snd_pcm_format_t snd_format)
175{
176 switch (snd_format) {
177 case SNDRV_PCM_FORMAT_U8:
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200178 return SRC_SF_U8;
179 case SNDRV_PCM_FORMAT_S16_LE:
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200180 return SRC_SF_S16;
181 case SNDRV_PCM_FORMAT_S24_3LE:
182 return SRC_SF_S24;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200183 case SNDRV_PCM_FORMAT_S32_LE:
184 return SRC_SF_S32;
Takashi Iwaid2b9b962009-06-02 14:39:05 +0200185 case SNDRV_PCM_FORMAT_FLOAT_LE:
186 return SRC_SF_F32;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200187 default:
Takashi Iwaib3e0afe2009-05-14 15:19:30 +0200188 printk(KERN_ERR "ctxfi: not recognized snd format is %d \n",
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200189 snd_format);
190 return SRC_SF_S16;
191 }
192}
193
194static unsigned int
195atc_get_pitch(unsigned int input_rate, unsigned int output_rate)
196{
Takashi Iwai514eef92009-06-08 14:57:57 +0200197 unsigned int pitch;
198 int b;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200199
200 /* get pitch and convert to fixed-point 8.24 format. */
201 pitch = (input_rate / output_rate) << 24;
202 input_rate %= output_rate;
203 input_rate /= 100;
204 output_rate /= 100;
205 for (b = 31; ((b >= 0) && !(input_rate >> b)); )
206 b--;
207
208 if (b >= 0) {
209 input_rate <<= (31 - b);
210 input_rate /= output_rate;
211 b = 24 - (31 - b);
212 if (b >= 0)
213 input_rate <<= b;
214 else
215 input_rate >>= -b;
216
217 pitch |= input_rate;
218 }
219
220 return pitch;
221}
222
223static int select_rom(unsigned int pitch)
224{
Takashi Iwai35ebf6e2009-07-22 17:12:34 +0200225 if (pitch > 0x00428f5c && pitch < 0x01b851ec) {
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200226 /* 0.26 <= pitch <= 1.72 */
227 return 1;
Takashi Iwai35ebf6e2009-07-22 17:12:34 +0200228 } else if (pitch == 0x01d66666 || pitch == 0x01d66667) {
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200229 /* pitch == 1.8375 */
230 return 2;
Takashi Iwai35ebf6e2009-07-22 17:12:34 +0200231 } else if (pitch == 0x02000000) {
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200232 /* pitch == 2 */
233 return 3;
Roel Kluin84ed1a12009-10-23 16:03:08 +0200234 } else if (pitch <= 0x08000000) {
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200235 /* 0 <= pitch <= 8 */
236 return 0;
237 } else {
238 return -ENOENT;
239 }
240}
241
242static int atc_pcm_playback_prepare(struct ct_atc *atc, struct ct_atc_pcm *apcm)
243{
244 struct src_mgr *src_mgr = atc->rsc_mgrs[SRC];
245 struct amixer_mgr *amixer_mgr = atc->rsc_mgrs[AMIXER];
246 struct src_desc desc = {0};
247 struct amixer_desc mix_dsc = {0};
Takashi Iwai514eef92009-06-08 14:57:57 +0200248 struct src *src;
249 struct amixer *amixer;
250 int err;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200251 int n_amixer = apcm->substream->runtime->channels, i = 0;
252 int device = apcm->substream->pcm->device;
Takashi Iwai514eef92009-06-08 14:57:57 +0200253 unsigned int pitch;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200254
Takashi Iwai822fa192009-06-02 14:12:17 +0200255 /* first release old resources */
Wai Yew CHAY29959a02009-06-22 14:52:34 +0200256 atc_pcm_release_resources(atc, apcm);
Takashi Iwai822fa192009-06-02 14:12:17 +0200257
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200258 /* Get SRC resource */
259 desc.multi = apcm->substream->runtime->channels;
260 desc.msr = atc->msr;
261 desc.mode = MEMRD;
262 err = src_mgr->get_src(src_mgr, &desc, (struct src **)&apcm->src);
263 if (err)
264 goto error1;
265
266 pitch = atc_get_pitch(apcm->substream->runtime->rate,
267 (atc->rsr * atc->msr));
268 src = apcm->src;
269 src->ops->set_pitch(src, pitch);
270 src->ops->set_rom(src, select_rom(pitch));
271 src->ops->set_sf(src, convert_format(apcm->substream->runtime->format));
272 src->ops->set_pm(src, (src->ops->next_interleave(src) != NULL));
273
274 /* Get AMIXER resource */
275 n_amixer = (n_amixer < 2) ? 2 : n_amixer;
276 apcm->amixers = kzalloc(sizeof(void *)*n_amixer, GFP_KERNEL);
Takashi Iwai35ebf6e2009-07-22 17:12:34 +0200277 if (!apcm->amixers) {
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200278 err = -ENOMEM;
279 goto error1;
280 }
281 mix_dsc.msr = atc->msr;
282 for (i = 0, apcm->n_amixer = 0; i < n_amixer; i++) {
283 err = amixer_mgr->get_amixer(amixer_mgr, &mix_dsc,
284 (struct amixer **)&apcm->amixers[i]);
285 if (err)
286 goto error1;
287
288 apcm->n_amixer++;
289 }
290
291 /* Set up device virtual mem map */
292 err = ct_map_audio_buffer(atc, apcm);
293 if (err < 0)
294 goto error1;
295
296 /* Connect resources */
297 src = apcm->src;
298 for (i = 0; i < n_amixer; i++) {
299 amixer = apcm->amixers[i];
Takashi Iwai635c2652009-06-13 10:12:59 +0200300 mutex_lock(&atc->atc_mutex);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200301 amixer->ops->setup(amixer, &src->rsc,
302 INIT_VOL, atc->pcm[i+device*2]);
Takashi Iwai635c2652009-06-13 10:12:59 +0200303 mutex_unlock(&atc->atc_mutex);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200304 src = src->ops->next_interleave(src);
Takashi Iwai35ebf6e2009-07-22 17:12:34 +0200305 if (!src)
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200306 src = apcm->src;
307 }
308
Takashi Iwaib7bbf872009-06-05 16:11:07 +0200309 ct_timer_prepare(apcm->timer);
310
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200311 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];
Takashi Iwai514eef92009-06-08 14:57:57 +0200325 struct srcimp *srcimp;
326 int i;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200327
Takashi Iwai35ebf6e2009-07-22 17:12:34 +0200328 if (apcm->srcimps) {
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200329 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
Takashi Iwai35ebf6e2009-07-22 17:12:34 +0200339 if (apcm->srccs) {
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200340 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
Takashi Iwai35ebf6e2009-07-22 17:12:34 +0200348 if (apcm->amixers) {
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200349 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
Takashi Iwai35ebf6e2009-07-22 17:12:34 +0200357 if (apcm->mono) {
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200358 sum_mgr->put_sum(sum_mgr, apcm->mono);
359 apcm->mono = NULL;
360 }
361
Takashi Iwai35ebf6e2009-07-22 17:12:34 +0200362 if (apcm->src) {
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200363 src_mgr->put_src(src_mgr, apcm->src);
364 apcm->src = NULL;
365 }
366
Takashi Iwai35ebf6e2009-07-22 17:12:34 +0200367 if (apcm->vm_block) {
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200368 /* 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{
Takashi Iwai514eef92009-06-08 14:57:57 +0200378 unsigned int max_cisz;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200379 struct src *src = apcm->src;
380
Takashi Iwaic399f3b2009-06-09 08:16:20 +0200381 if (apcm->started)
382 return 0;
383 apcm->started = 1;
384
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200385 max_cisz = src->multi * src->rsc.msr;
386 max_cisz = 0x80 * (max_cisz < 8 ? max_cisz : 8);
387
388 src->ops->set_sa(src, apcm->vm_block->addr);
389 src->ops->set_la(src, apcm->vm_block->addr + apcm->vm_block->size);
390 src->ops->set_ca(src, apcm->vm_block->addr + max_cisz);
391 src->ops->set_cisz(src, max_cisz);
392
393 src->ops->set_bm(src, 1);
394 src->ops->set_state(src, SRC_STATE_INIT);
395 src->ops->commit_write(src);
396
Takashi Iwaib7bbf872009-06-05 16:11:07 +0200397 ct_timer_start(apcm->timer);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200398 return 0;
399}
400
401static int atc_pcm_stop(struct ct_atc *atc, struct ct_atc_pcm *apcm)
402{
Takashi Iwai514eef92009-06-08 14:57:57 +0200403 struct src *src;
404 int i;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200405
Takashi Iwaib7bbf872009-06-05 16:11:07 +0200406 ct_timer_stop(apcm->timer);
407
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200408 src = apcm->src;
409 src->ops->set_bm(src, 0);
410 src->ops->set_state(src, SRC_STATE_OFF);
411 src->ops->commit_write(src);
412
Takashi Iwai35ebf6e2009-07-22 17:12:34 +0200413 if (apcm->srccs) {
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200414 for (i = 0; i < apcm->n_srcc; i++) {
415 src = apcm->srccs[i];
416 src->ops->set_bm(src, 0);
417 src->ops->set_state(src, SRC_STATE_OFF);
418 src->ops->commit_write(src);
419 }
420 }
421
422 apcm->started = 0;
423
424 return 0;
425}
426
427static int
428atc_pcm_playback_position(struct ct_atc *atc, struct ct_atc_pcm *apcm)
429{
430 struct src *src = apcm->src;
Takashi Iwai514eef92009-06-08 14:57:57 +0200431 u32 size, max_cisz;
432 int position;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200433
Takashi Iwai5242bc72009-06-09 08:17:14 +0200434 if (!src)
435 return 0;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200436 position = src->ops->get_ca(src);
437
Sarah Bessmere240a462014-01-09 11:36:31 -0800438 if (position < apcm->vm_block->addr) {
439 snd_printdd("ctxfi: bad ca - ca=0x%08x, vba=0x%08x, vbs=0x%08x\n", position, apcm->vm_block->addr, apcm->vm_block->size);
440 position = apcm->vm_block->addr;
441 }
442
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200443 size = apcm->vm_block->size;
444 max_cisz = src->multi * src->rsc.msr;
445 max_cisz = 128 * (max_cisz < 8 ? max_cisz : 8);
446
447 return (position + size - max_cisz - apcm->vm_block->addr) % size;
448}
449
450struct src_node_conf_t {
451 unsigned int pitch;
452 unsigned int msr:8;
453 unsigned int mix_msr:8;
454 unsigned int imp_msr:8;
455 unsigned int vo:1;
456};
457
458static void setup_src_node_conf(struct ct_atc *atc, struct ct_atc_pcm *apcm,
459 struct src_node_conf_t *conf, int *n_srcc)
460{
Takashi Iwai514eef92009-06-08 14:57:57 +0200461 unsigned int pitch;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200462
463 /* get pitch and convert to fixed-point 8.24 format. */
464 pitch = atc_get_pitch((atc->rsr * atc->msr),
465 apcm->substream->runtime->rate);
466 *n_srcc = 0;
467
Harry Butterworth55309212011-06-11 16:02:06 +0800468 if (1 == atc->msr) { /* FIXME: do we really need SRC here if pitch==1 */
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200469 *n_srcc = apcm->substream->runtime->channels;
470 conf[0].pitch = pitch;
471 conf[0].mix_msr = conf[0].imp_msr = conf[0].msr = 1;
472 conf[0].vo = 1;
Harry Butterworth55309212011-06-11 16:02:06 +0800473 } else if (2 <= atc->msr) {
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200474 if (0x8000000 < pitch) {
475 /* Need two-stage SRCs, SRCIMPs and
476 * AMIXERs for converting format */
477 conf[0].pitch = (atc->msr << 24);
478 conf[0].msr = conf[0].mix_msr = 1;
479 conf[0].imp_msr = atc->msr;
480 conf[0].vo = 0;
481 conf[1].pitch = atc_get_pitch(atc->rsr,
482 apcm->substream->runtime->rate);
483 conf[1].msr = conf[1].mix_msr = conf[1].imp_msr = 1;
484 conf[1].vo = 1;
485 *n_srcc = apcm->substream->runtime->channels * 2;
486 } else if (0x1000000 < pitch) {
487 /* Need one-stage SRCs, SRCIMPs and
488 * AMIXERs for converting format */
489 conf[0].pitch = pitch;
490 conf[0].msr = conf[0].mix_msr
491 = conf[0].imp_msr = atc->msr;
492 conf[0].vo = 1;
493 *n_srcc = apcm->substream->runtime->channels;
494 }
495 }
496}
497
498static int
499atc_pcm_capture_get_resources(struct ct_atc *atc, struct ct_atc_pcm *apcm)
500{
501 struct src_mgr *src_mgr = atc->rsc_mgrs[SRC];
502 struct srcimp_mgr *srcimp_mgr = atc->rsc_mgrs[SRCIMP];
503 struct amixer_mgr *amixer_mgr = atc->rsc_mgrs[AMIXER];
504 struct sum_mgr *sum_mgr = atc->rsc_mgrs[SUM];
505 struct src_desc src_dsc = {0};
Takashi Iwai514eef92009-06-08 14:57:57 +0200506 struct src *src;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200507 struct srcimp_desc srcimp_dsc = {0};
Takashi Iwai514eef92009-06-08 14:57:57 +0200508 struct srcimp *srcimp;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200509 struct amixer_desc mix_dsc = {0};
510 struct sum_desc sum_dsc = {0};
Takashi Iwai514eef92009-06-08 14:57:57 +0200511 unsigned int pitch;
512 int multi, err, i;
513 int n_srcimp, n_amixer, n_srcc, n_sum;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200514 struct src_node_conf_t src_node_conf[2] = {{0} };
515
Takashi Iwai822fa192009-06-02 14:12:17 +0200516 /* first release old resources */
Takashi Iwaia5990dc2009-06-09 08:19:02 +0200517 atc_pcm_release_resources(atc, apcm);
Takashi Iwai822fa192009-06-02 14:12:17 +0200518
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200519 /* The numbers of converting SRCs and SRCIMPs should be determined
520 * by pitch value. */
521
522 multi = apcm->substream->runtime->channels;
523
524 /* get pitch and convert to fixed-point 8.24 format. */
525 pitch = atc_get_pitch((atc->rsr * atc->msr),
526 apcm->substream->runtime->rate);
527
528 setup_src_node_conf(atc, apcm, src_node_conf, &n_srcc);
529 n_sum = (1 == multi) ? 1 : 0;
Takashi Iwai514eef92009-06-08 14:57:57 +0200530 n_amixer = n_sum * 2 + n_srcc;
531 n_srcimp = n_srcc;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200532 if ((multi > 1) && (0x8000000 >= pitch)) {
533 /* Need extra AMIXERs and SRCIMPs for special treatment
534 * of interleaved recording of conjugate channels */
535 n_amixer += multi * atc->msr;
536 n_srcimp += multi * atc->msr;
537 } else {
538 n_srcimp += multi;
539 }
540
541 if (n_srcc) {
542 apcm->srccs = kzalloc(sizeof(void *)*n_srcc, GFP_KERNEL);
Takashi Iwai35ebf6e2009-07-22 17:12:34 +0200543 if (!apcm->srccs)
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200544 return -ENOMEM;
545 }
546 if (n_amixer) {
547 apcm->amixers = kzalloc(sizeof(void *)*n_amixer, GFP_KERNEL);
Takashi Iwai35ebf6e2009-07-22 17:12:34 +0200548 if (!apcm->amixers) {
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200549 err = -ENOMEM;
550 goto error1;
551 }
552 }
553 apcm->srcimps = kzalloc(sizeof(void *)*n_srcimp, GFP_KERNEL);
Takashi Iwai35ebf6e2009-07-22 17:12:34 +0200554 if (!apcm->srcimps) {
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200555 err = -ENOMEM;
556 goto error1;
557 }
558
559 /* Allocate SRCs for sample rate conversion if needed */
560 src_dsc.multi = 1;
561 src_dsc.mode = ARCRW;
562 for (i = 0, apcm->n_srcc = 0; i < n_srcc; i++) {
563 src_dsc.msr = src_node_conf[i/multi].msr;
564 err = src_mgr->get_src(src_mgr, &src_dsc,
565 (struct src **)&apcm->srccs[i]);
566 if (err)
567 goto error1;
568
569 src = apcm->srccs[i];
570 pitch = src_node_conf[i/multi].pitch;
571 src->ops->set_pitch(src, pitch);
572 src->ops->set_rom(src, select_rom(pitch));
573 src->ops->set_vo(src, src_node_conf[i/multi].vo);
574
575 apcm->n_srcc++;
576 }
577
578 /* Allocate AMIXERs for routing SRCs of conversion if needed */
579 for (i = 0, apcm->n_amixer = 0; i < n_amixer; i++) {
580 if (i < (n_sum*2))
581 mix_dsc.msr = atc->msr;
582 else if (i < (n_sum*2+n_srcc))
583 mix_dsc.msr = src_node_conf[(i-n_sum*2)/multi].mix_msr;
584 else
585 mix_dsc.msr = 1;
586
587 err = amixer_mgr->get_amixer(amixer_mgr, &mix_dsc,
588 (struct amixer **)&apcm->amixers[i]);
589 if (err)
590 goto error1;
591
592 apcm->n_amixer++;
593 }
594
595 /* Allocate a SUM resource to mix all input channels together */
596 sum_dsc.msr = atc->msr;
597 err = sum_mgr->get_sum(sum_mgr, &sum_dsc, (struct sum **)&apcm->mono);
598 if (err)
599 goto error1;
600
601 pitch = atc_get_pitch((atc->rsr * atc->msr),
602 apcm->substream->runtime->rate);
603 /* Allocate SRCIMP resources */
604 for (i = 0, apcm->n_srcimp = 0; i < n_srcimp; i++) {
605 if (i < (n_srcc))
606 srcimp_dsc.msr = src_node_conf[i/multi].imp_msr;
607 else if (1 == multi)
608 srcimp_dsc.msr = (pitch <= 0x8000000) ? atc->msr : 1;
609 else
610 srcimp_dsc.msr = 1;
611
612 err = srcimp_mgr->get_srcimp(srcimp_mgr, &srcimp_dsc, &srcimp);
613 if (err)
614 goto error1;
615
616 apcm->srcimps[i] = srcimp;
617 apcm->n_srcimp++;
618 }
619
620 /* Allocate a SRC for writing data to host memory */
621 src_dsc.multi = apcm->substream->runtime->channels;
622 src_dsc.msr = 1;
623 src_dsc.mode = MEMWR;
624 err = src_mgr->get_src(src_mgr, &src_dsc, (struct src **)&apcm->src);
625 if (err)
626 goto error1;
627
628 src = apcm->src;
629 src->ops->set_pitch(src, pitch);
630
631 /* Set up device virtual mem map */
632 err = ct_map_audio_buffer(atc, apcm);
633 if (err < 0)
634 goto error1;
635
636 return 0;
637
638error1:
639 atc_pcm_release_resources(atc, apcm);
640 return err;
641}
642
643static int atc_pcm_capture_prepare(struct ct_atc *atc, struct ct_atc_pcm *apcm)
644{
Takashi Iwai514eef92009-06-08 14:57:57 +0200645 struct src *src;
646 struct amixer *amixer;
647 struct srcimp *srcimp;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200648 struct ct_mixer *mixer = atc->mixer;
Takashi Iwai514eef92009-06-08 14:57:57 +0200649 struct sum *mono;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200650 struct rsc *out_ports[8] = {NULL};
Takashi Iwai514eef92009-06-08 14:57:57 +0200651 int err, i, j, n_sum, multi;
652 unsigned int pitch;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200653 int mix_base = 0, imp_base = 0;
654
Wai Yew CHAY29959a02009-06-22 14:52:34 +0200655 atc_pcm_release_resources(atc, apcm);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200656
657 /* Get needed resources. */
658 err = atc_pcm_capture_get_resources(atc, apcm);
659 if (err)
660 return err;
661
662 /* Connect resources */
663 mixer->get_output_ports(mixer, MIX_PCMO_FRONT,
664 &out_ports[0], &out_ports[1]);
665
666 multi = apcm->substream->runtime->channels;
667 if (1 == multi) {
668 mono = apcm->mono;
669 for (i = 0; i < 2; i++) {
670 amixer = apcm->amixers[i];
671 amixer->ops->setup(amixer, out_ports[i],
672 MONO_SUM_SCALE, mono);
673 }
674 out_ports[0] = &mono->rsc;
675 n_sum = 1;
676 mix_base = n_sum * 2;
677 }
678
679 for (i = 0; i < apcm->n_srcc; i++) {
680 src = apcm->srccs[i];
681 srcimp = apcm->srcimps[imp_base+i];
682 amixer = apcm->amixers[mix_base+i];
683 srcimp->ops->map(srcimp, src, out_ports[i%multi]);
684 amixer->ops->setup(amixer, &src->rsc, INIT_VOL, NULL);
685 out_ports[i%multi] = &amixer->rsc;
686 }
687
688 pitch = atc_get_pitch((atc->rsr * atc->msr),
689 apcm->substream->runtime->rate);
690
691 if ((multi > 1) && (pitch <= 0x8000000)) {
692 /* Special connection for interleaved
693 * recording with conjugate channels */
694 for (i = 0; i < multi; i++) {
695 out_ports[i]->ops->master(out_ports[i]);
696 for (j = 0; j < atc->msr; j++) {
697 amixer = apcm->amixers[apcm->n_srcc+j*multi+i];
698 amixer->ops->set_input(amixer, out_ports[i]);
699 amixer->ops->set_scale(amixer, INIT_VOL);
700 amixer->ops->set_sum(amixer, NULL);
701 amixer->ops->commit_raw_write(amixer);
702 out_ports[i]->ops->next_conj(out_ports[i]);
703
704 srcimp = apcm->srcimps[apcm->n_srcc+j*multi+i];
705 srcimp->ops->map(srcimp, apcm->src,
706 &amixer->rsc);
707 }
708 }
709 } else {
710 for (i = 0; i < multi; i++) {
711 srcimp = apcm->srcimps[apcm->n_srcc+i];
712 srcimp->ops->map(srcimp, apcm->src, out_ports[i]);
713 }
714 }
715
Takashi Iwaib7bbf872009-06-05 16:11:07 +0200716 ct_timer_prepare(apcm->timer);
717
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200718 return 0;
719}
720
721static int atc_pcm_capture_start(struct ct_atc *atc, struct ct_atc_pcm *apcm)
722{
Takashi Iwai514eef92009-06-08 14:57:57 +0200723 struct src *src;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200724 struct src_mgr *src_mgr = atc->rsc_mgrs[SRC];
Takashi Iwai514eef92009-06-08 14:57:57 +0200725 int i, multi;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200726
727 if (apcm->started)
728 return 0;
729
730 apcm->started = 1;
731 multi = apcm->substream->runtime->channels;
732 /* Set up converting SRCs */
733 for (i = 0; i < apcm->n_srcc; i++) {
734 src = apcm->srccs[i];
735 src->ops->set_pm(src, ((i%multi) != (multi-1)));
736 src_mgr->src_disable(src_mgr, src);
737 }
738
739 /* Set up recording SRC */
740 src = apcm->src;
741 src->ops->set_sf(src, convert_format(apcm->substream->runtime->format));
742 src->ops->set_sa(src, apcm->vm_block->addr);
743 src->ops->set_la(src, apcm->vm_block->addr + apcm->vm_block->size);
744 src->ops->set_ca(src, apcm->vm_block->addr);
745 src_mgr->src_disable(src_mgr, src);
746
747 /* Disable relevant SRCs firstly */
748 src_mgr->commit_write(src_mgr);
749
750 /* Enable SRCs respectively */
751 for (i = 0; i < apcm->n_srcc; i++) {
752 src = apcm->srccs[i];
753 src->ops->set_state(src, SRC_STATE_RUN);
754 src->ops->commit_write(src);
755 src_mgr->src_enable_s(src_mgr, src);
756 }
757 src = apcm->src;
758 src->ops->set_bm(src, 1);
759 src->ops->set_state(src, SRC_STATE_RUN);
760 src->ops->commit_write(src);
761 src_mgr->src_enable_s(src_mgr, src);
762
763 /* Enable relevant SRCs synchronously */
764 src_mgr->commit_write(src_mgr);
765
Takashi Iwaib7bbf872009-06-05 16:11:07 +0200766 ct_timer_start(apcm->timer);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200767 return 0;
768}
769
770static int
771atc_pcm_capture_position(struct ct_atc *atc, struct ct_atc_pcm *apcm)
772{
773 struct src *src = apcm->src;
774
Takashi Iwai5242bc72009-06-09 08:17:14 +0200775 if (!src)
776 return 0;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200777 return src->ops->get_ca(src) - apcm->vm_block->addr;
778}
779
780static int spdif_passthru_playback_get_resources(struct ct_atc *atc,
781 struct ct_atc_pcm *apcm)
782{
783 struct src_mgr *src_mgr = atc->rsc_mgrs[SRC];
784 struct amixer_mgr *amixer_mgr = atc->rsc_mgrs[AMIXER];
785 struct src_desc desc = {0};
786 struct amixer_desc mix_dsc = {0};
Takashi Iwai514eef92009-06-08 14:57:57 +0200787 struct src *src;
788 int err;
789 int n_amixer = apcm->substream->runtime->channels, i;
790 unsigned int pitch, rsr = atc->pll_rate;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200791
Takashi Iwai822fa192009-06-02 14:12:17 +0200792 /* first release old resources */
Takashi Iwaia5990dc2009-06-09 08:19:02 +0200793 atc_pcm_release_resources(atc, apcm);
Takashi Iwai822fa192009-06-02 14:12:17 +0200794
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200795 /* Get SRC resource */
796 desc.multi = apcm->substream->runtime->channels;
797 desc.msr = 1;
798 while (apcm->substream->runtime->rate > (rsr * desc.msr))
799 desc.msr <<= 1;
800
801 desc.mode = MEMRD;
802 err = src_mgr->get_src(src_mgr, &desc, (struct src **)&apcm->src);
803 if (err)
804 goto error1;
805
806 pitch = atc_get_pitch(apcm->substream->runtime->rate, (rsr * desc.msr));
807 src = apcm->src;
808 src->ops->set_pitch(src, pitch);
809 src->ops->set_rom(src, select_rom(pitch));
810 src->ops->set_sf(src, convert_format(apcm->substream->runtime->format));
811 src->ops->set_pm(src, (src->ops->next_interleave(src) != NULL));
812 src->ops->set_bp(src, 1);
813
814 /* Get AMIXER resource */
815 n_amixer = (n_amixer < 2) ? 2 : n_amixer;
816 apcm->amixers = kzalloc(sizeof(void *)*n_amixer, GFP_KERNEL);
Takashi Iwai35ebf6e2009-07-22 17:12:34 +0200817 if (!apcm->amixers) {
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200818 err = -ENOMEM;
819 goto error1;
820 }
821 mix_dsc.msr = desc.msr;
822 for (i = 0, apcm->n_amixer = 0; i < n_amixer; i++) {
823 err = amixer_mgr->get_amixer(amixer_mgr, &mix_dsc,
824 (struct amixer **)&apcm->amixers[i]);
825 if (err)
826 goto error1;
827
828 apcm->n_amixer++;
829 }
830
831 /* Set up device virtual mem map */
832 err = ct_map_audio_buffer(atc, apcm);
833 if (err < 0)
834 goto error1;
835
836 return 0;
837
838error1:
839 atc_pcm_release_resources(atc, apcm);
840 return err;
841}
842
Takashi Iwai514eef92009-06-08 14:57:57 +0200843static int atc_pll_init(struct ct_atc *atc, int rate)
844{
845 struct hw *hw = atc->hw;
846 int err;
847 err = hw->pll_init(hw, rate);
848 atc->pll_rate = err ? 0 : rate;
849 return err;
850}
851
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200852static int
853spdif_passthru_playback_setup(struct ct_atc *atc, struct ct_atc_pcm *apcm)
854{
855 struct dao *dao = container_of(atc->daios[SPDIFOO], struct dao, daio);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200856 unsigned int rate = apcm->substream->runtime->rate;
Takashi Iwai514eef92009-06-08 14:57:57 +0200857 unsigned int status;
Wai Yew CHAY29959a02009-06-22 14:52:34 +0200858 int err = 0;
Takashi Iwai514eef92009-06-08 14:57:57 +0200859 unsigned char iec958_con_fs;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200860
861 switch (rate) {
862 case 48000:
863 iec958_con_fs = IEC958_AES3_CON_FS_48000;
864 break;
865 case 44100:
866 iec958_con_fs = IEC958_AES3_CON_FS_44100;
867 break;
868 case 32000:
869 iec958_con_fs = IEC958_AES3_CON_FS_32000;
870 break;
871 default:
872 return -ENOENT;
873 }
874
Takashi Iwai635c2652009-06-13 10:12:59 +0200875 mutex_lock(&atc->atc_mutex);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200876 dao->ops->get_spos(dao, &status);
877 if (((status >> 24) & IEC958_AES3_CON_FS) != iec958_con_fs) {
Przemyslaw Bruski4c1847e2011-03-13 16:18:56 +0100878 status &= ~(IEC958_AES3_CON_FS << 24);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200879 status |= (iec958_con_fs << 24);
880 dao->ops->set_spos(dao, status);
881 dao->ops->commit_write(dao);
882 }
Takashi Iwai514eef92009-06-08 14:57:57 +0200883 if ((rate != atc->pll_rate) && (32000 != rate))
884 err = atc_pll_init(atc, rate);
Takashi Iwai635c2652009-06-13 10:12:59 +0200885 mutex_unlock(&atc->atc_mutex);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200886
887 return err;
888}
889
890static int
891spdif_passthru_playback_prepare(struct ct_atc *atc, struct ct_atc_pcm *apcm)
892{
Takashi Iwai514eef92009-06-08 14:57:57 +0200893 struct src *src;
894 struct amixer *amixer;
895 struct dao *dao;
896 int err;
897 int i;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200898
Wai Yew CHAY29959a02009-06-22 14:52:34 +0200899 atc_pcm_release_resources(atc, apcm);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200900
901 /* Configure SPDIFOO and PLL to passthrough mode;
902 * determine pll_rate. */
903 err = spdif_passthru_playback_setup(atc, apcm);
904 if (err)
905 return err;
906
907 /* Get needed resources. */
908 err = spdif_passthru_playback_get_resources(atc, apcm);
909 if (err)
910 return err;
911
912 /* Connect resources */
913 src = apcm->src;
914 for (i = 0; i < apcm->n_amixer; i++) {
915 amixer = apcm->amixers[i];
916 amixer->ops->setup(amixer, &src->rsc, INIT_VOL, NULL);
917 src = src->ops->next_interleave(src);
Takashi Iwai35ebf6e2009-07-22 17:12:34 +0200918 if (!src)
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200919 src = apcm->src;
920 }
921 /* Connect to SPDIFOO */
Takashi Iwai635c2652009-06-13 10:12:59 +0200922 mutex_lock(&atc->atc_mutex);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200923 dao = container_of(atc->daios[SPDIFOO], struct dao, daio);
924 amixer = apcm->amixers[0];
925 dao->ops->set_left_input(dao, &amixer->rsc);
926 amixer = apcm->amixers[1];
927 dao->ops->set_right_input(dao, &amixer->rsc);
Takashi Iwai635c2652009-06-13 10:12:59 +0200928 mutex_unlock(&atc->atc_mutex);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200929
Takashi Iwaib7bbf872009-06-05 16:11:07 +0200930 ct_timer_prepare(apcm->timer);
931
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200932 return 0;
933}
934
935static int atc_select_line_in(struct ct_atc *atc)
936{
937 struct hw *hw = atc->hw;
938 struct ct_mixer *mixer = atc->mixer;
Takashi Iwai514eef92009-06-08 14:57:57 +0200939 struct src *src;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200940
941 if (hw->is_adc_source_selected(hw, ADC_LINEIN))
942 return 0;
943
944 mixer->set_input_left(mixer, MIX_MIC_IN, NULL);
945 mixer->set_input_right(mixer, MIX_MIC_IN, NULL);
946
947 hw->select_adc_source(hw, ADC_LINEIN);
948
949 src = atc->srcs[2];
950 mixer->set_input_left(mixer, MIX_LINE_IN, &src->rsc);
951 src = atc->srcs[3];
952 mixer->set_input_right(mixer, MIX_LINE_IN, &src->rsc);
953
954 return 0;
955}
956
957static int atc_select_mic_in(struct ct_atc *atc)
958{
959 struct hw *hw = atc->hw;
960 struct ct_mixer *mixer = atc->mixer;
Takashi Iwai514eef92009-06-08 14:57:57 +0200961 struct src *src;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200962
963 if (hw->is_adc_source_selected(hw, ADC_MICIN))
964 return 0;
965
966 mixer->set_input_left(mixer, MIX_LINE_IN, NULL);
967 mixer->set_input_right(mixer, MIX_LINE_IN, NULL);
968
969 hw->select_adc_source(hw, ADC_MICIN);
970
971 src = atc->srcs[2];
972 mixer->set_input_left(mixer, MIX_MIC_IN, &src->rsc);
973 src = atc->srcs[3];
974 mixer->set_input_right(mixer, MIX_MIC_IN, &src->rsc);
975
976 return 0;
977}
978
Harry Butterworthb028b812011-06-11 17:41:13 +0800979static struct capabilities atc_capabilities(struct ct_atc *atc)
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200980{
981 struct hw *hw = atc->hw;
982
Harry Butterworthb028b812011-06-11 17:41:13 +0800983 return hw->capabilities(hw);
Harry Butterworth55309212011-06-11 16:02:06 +0800984}
985
986static int atc_output_switch_get(struct ct_atc *atc)
987{
988 struct hw *hw = atc->hw;
989
990 return hw->output_switch_get(hw);
991}
992
993static int atc_output_switch_put(struct ct_atc *atc, int position)
994{
995 struct hw *hw = atc->hw;
996
997 return hw->output_switch_put(hw, position);
998}
999
Harry Butterworth55309212011-06-11 16:02:06 +08001000static int atc_mic_source_switch_get(struct ct_atc *atc)
1001{
1002 struct hw *hw = atc->hw;
1003
1004 return hw->mic_source_switch_get(hw);
1005}
1006
1007static int atc_mic_source_switch_put(struct ct_atc *atc, int position)
1008{
1009 struct hw *hw = atc->hw;
1010
1011 return hw->mic_source_switch_put(hw, position);
1012}
1013
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001014static int atc_select_digit_io(struct ct_atc *atc)
1015{
1016 struct hw *hw = atc->hw;
1017
1018 if (hw->is_adc_source_selected(hw, ADC_NONE))
1019 return 0;
1020
1021 hw->select_adc_source(hw, ADC_NONE);
1022
1023 return 0;
1024}
1025
1026static int atc_daio_unmute(struct ct_atc *atc, unsigned char state, int type)
1027{
1028 struct daio_mgr *daio_mgr = atc->rsc_mgrs[DAIO];
1029
1030 if (state)
1031 daio_mgr->daio_enable(daio_mgr, atc->daios[type]);
1032 else
1033 daio_mgr->daio_disable(daio_mgr, atc->daios[type]);
1034
1035 daio_mgr->commit_write(daio_mgr);
1036
1037 return 0;
1038}
1039
1040static int
1041atc_dao_get_status(struct ct_atc *atc, unsigned int *status, int type)
1042{
1043 struct dao *dao = container_of(atc->daios[type], struct dao, daio);
1044 return dao->ops->get_spos(dao, status);
1045}
1046
1047static int
1048atc_dao_set_status(struct ct_atc *atc, unsigned int status, int type)
1049{
1050 struct dao *dao = container_of(atc->daios[type], struct dao, daio);
1051
1052 dao->ops->set_spos(dao, status);
1053 dao->ops->commit_write(dao);
1054 return 0;
1055}
1056
1057static int atc_line_front_unmute(struct ct_atc *atc, unsigned char state)
1058{
1059 return atc_daio_unmute(atc, state, LINEO1);
1060}
1061
1062static int atc_line_surround_unmute(struct ct_atc *atc, unsigned char state)
1063{
Sven Eckelmann3b046912009-10-01 20:06:39 +02001064 return atc_daio_unmute(atc, state, LINEO2);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001065}
1066
1067static int atc_line_clfe_unmute(struct ct_atc *atc, unsigned char state)
1068{
1069 return atc_daio_unmute(atc, state, LINEO3);
1070}
1071
1072static int atc_line_rear_unmute(struct ct_atc *atc, unsigned char state)
1073{
Sven Eckelmann3b046912009-10-01 20:06:39 +02001074 return atc_daio_unmute(atc, state, LINEO4);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001075}
1076
1077static int atc_line_in_unmute(struct ct_atc *atc, unsigned char state)
1078{
1079 return atc_daio_unmute(atc, state, LINEIM);
1080}
1081
Harry Butterworth55309212011-06-11 16:02:06 +08001082static int atc_mic_unmute(struct ct_atc *atc, unsigned char state)
1083{
1084 return atc_daio_unmute(atc, state, MIC);
1085}
1086
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001087static int atc_spdif_out_unmute(struct ct_atc *atc, unsigned char state)
1088{
1089 return atc_daio_unmute(atc, state, SPDIFOO);
1090}
1091
1092static int atc_spdif_in_unmute(struct ct_atc *atc, unsigned char state)
1093{
1094 return atc_daio_unmute(atc, state, SPDIFIO);
1095}
1096
1097static int atc_spdif_out_get_status(struct ct_atc *atc, unsigned int *status)
1098{
1099 return atc_dao_get_status(atc, status, SPDIFOO);
1100}
1101
1102static int atc_spdif_out_set_status(struct ct_atc *atc, unsigned int status)
1103{
1104 return atc_dao_set_status(atc, status, SPDIFOO);
1105}
1106
1107static int atc_spdif_out_passthru(struct ct_atc *atc, unsigned char state)
1108{
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001109 struct dao_desc da_dsc = {0};
Takashi Iwai514eef92009-06-08 14:57:57 +02001110 struct dao *dao;
1111 int err;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001112 struct ct_mixer *mixer = atc->mixer;
1113 struct rsc *rscs[2] = {NULL};
1114 unsigned int spos = 0;
1115
Takashi Iwai635c2652009-06-13 10:12:59 +02001116 mutex_lock(&atc->atc_mutex);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001117 dao = container_of(atc->daios[SPDIFOO], struct dao, daio);
1118 da_dsc.msr = state ? 1 : atc->msr;
1119 da_dsc.passthru = state ? 1 : 0;
1120 err = dao->ops->reinit(dao, &da_dsc);
1121 if (state) {
1122 spos = IEC958_DEFAULT_CON;
1123 } else {
1124 mixer->get_output_ports(mixer, MIX_SPDIF_OUT,
1125 &rscs[0], &rscs[1]);
1126 dao->ops->set_left_input(dao, rscs[0]);
1127 dao->ops->set_right_input(dao, rscs[1]);
1128 /* Restore PLL to atc->rsr if needed. */
Takashi Iwai514eef92009-06-08 14:57:57 +02001129 if (atc->pll_rate != atc->rsr)
1130 err = atc_pll_init(atc, atc->rsr);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001131 }
1132 dao->ops->set_spos(dao, spos);
1133 dao->ops->commit_write(dao);
Takashi Iwai635c2652009-06-13 10:12:59 +02001134 mutex_unlock(&atc->atc_mutex);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001135
1136 return err;
1137}
1138
Wai Yew CHAY29959a02009-06-22 14:52:34 +02001139static int atc_release_resources(struct ct_atc *atc)
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001140{
Wai Yew CHAY29959a02009-06-22 14:52:34 +02001141 int i;
1142 struct daio_mgr *daio_mgr = NULL;
1143 struct dao *dao = NULL;
1144 struct dai *dai = NULL;
1145 struct daio *daio = NULL;
1146 struct sum_mgr *sum_mgr = NULL;
1147 struct src_mgr *src_mgr = NULL;
1148 struct srcimp_mgr *srcimp_mgr = NULL;
1149 struct srcimp *srcimp = NULL;
1150 struct ct_mixer *mixer = NULL;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001151
Wai Yew CHAY29959a02009-06-22 14:52:34 +02001152 /* disconnect internal mixer objects */
Takashi Iwai35ebf6e2009-07-22 17:12:34 +02001153 if (atc->mixer) {
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001154 mixer = atc->mixer;
1155 mixer->set_input_left(mixer, MIX_LINE_IN, NULL);
1156 mixer->set_input_right(mixer, MIX_LINE_IN, NULL);
1157 mixer->set_input_left(mixer, MIX_MIC_IN, NULL);
1158 mixer->set_input_right(mixer, MIX_MIC_IN, NULL);
1159 mixer->set_input_left(mixer, MIX_SPDIF_IN, NULL);
1160 mixer->set_input_right(mixer, MIX_SPDIF_IN, NULL);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001161 }
1162
Takashi Iwai35ebf6e2009-07-22 17:12:34 +02001163 if (atc->daios) {
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001164 daio_mgr = (struct daio_mgr *)atc->rsc_mgrs[DAIO];
1165 for (i = 0; i < atc->n_daio; i++) {
1166 daio = atc->daios[i];
1167 if (daio->type < LINEIM) {
1168 dao = container_of(daio, struct dao, daio);
1169 dao->ops->clear_left_input(dao);
1170 dao->ops->clear_right_input(dao);
1171 } else {
1172 dai = container_of(daio, struct dai, daio);
1173 /* some thing to do for dai ... */
1174 }
1175 daio_mgr->put_daio(daio_mgr, daio);
1176 }
1177 kfree(atc->daios);
Wai Yew CHAY29959a02009-06-22 14:52:34 +02001178 atc->daios = NULL;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001179 }
1180
Takashi Iwai35ebf6e2009-07-22 17:12:34 +02001181 if (atc->pcm) {
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001182 sum_mgr = atc->rsc_mgrs[SUM];
1183 for (i = 0; i < atc->n_pcm; i++)
1184 sum_mgr->put_sum(sum_mgr, atc->pcm[i]);
1185
1186 kfree(atc->pcm);
Wai Yew CHAY29959a02009-06-22 14:52:34 +02001187 atc->pcm = NULL;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001188 }
1189
Takashi Iwai35ebf6e2009-07-22 17:12:34 +02001190 if (atc->srcs) {
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001191 src_mgr = atc->rsc_mgrs[SRC];
1192 for (i = 0; i < atc->n_src; i++)
1193 src_mgr->put_src(src_mgr, atc->srcs[i]);
1194
1195 kfree(atc->srcs);
Wai Yew CHAY29959a02009-06-22 14:52:34 +02001196 atc->srcs = NULL;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001197 }
1198
Takashi Iwai35ebf6e2009-07-22 17:12:34 +02001199 if (atc->srcimps) {
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001200 srcimp_mgr = atc->rsc_mgrs[SRCIMP];
1201 for (i = 0; i < atc->n_srcimp; i++) {
1202 srcimp = atc->srcimps[i];
1203 srcimp->ops->unmap(srcimp);
1204 srcimp_mgr->put_srcimp(srcimp_mgr, atc->srcimps[i]);
1205 }
1206 kfree(atc->srcimps);
Wai Yew CHAY29959a02009-06-22 14:52:34 +02001207 atc->srcimps = NULL;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001208 }
1209
Wai Yew CHAY29959a02009-06-22 14:52:34 +02001210 return 0;
1211}
1212
1213static int ct_atc_destroy(struct ct_atc *atc)
1214{
1215 int i = 0;
1216
Takashi Iwai35ebf6e2009-07-22 17:12:34 +02001217 if (!atc)
Wai Yew CHAY29959a02009-06-22 14:52:34 +02001218 return 0;
1219
1220 if (atc->timer) {
1221 ct_timer_free(atc->timer);
1222 atc->timer = NULL;
1223 }
1224
1225 atc_release_resources(atc);
1226
1227 /* Destroy internal mixer objects */
Takashi Iwai35ebf6e2009-07-22 17:12:34 +02001228 if (atc->mixer)
Wai Yew CHAY29959a02009-06-22 14:52:34 +02001229 ct_mixer_destroy(atc->mixer);
1230
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001231 for (i = 0; i < NUM_RSCTYP; i++) {
Takashi Iwai35ebf6e2009-07-22 17:12:34 +02001232 if (rsc_mgr_funcs[i].destroy && atc->rsc_mgrs[i])
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001233 rsc_mgr_funcs[i].destroy(atc->rsc_mgrs[i]);
1234
1235 }
1236
Takashi Iwai35ebf6e2009-07-22 17:12:34 +02001237 if (atc->hw)
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001238 destroy_hw_obj((struct hw *)atc->hw);
1239
1240 /* Destroy device virtual memory manager object */
Takashi Iwai35ebf6e2009-07-22 17:12:34 +02001241 if (atc->vm) {
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001242 ct_vm_destroy(atc->vm);
1243 atc->vm = NULL;
1244 }
1245
1246 kfree(atc);
1247
1248 return 0;
1249}
1250
1251static int atc_dev_free(struct snd_device *dev)
1252{
1253 struct ct_atc *atc = dev->device_data;
1254 return ct_atc_destroy(atc);
1255}
1256
Bill Pembertone23e7a12012-12-06 12:35:10 -05001257static int atc_identify_card(struct ct_atc *atc, unsigned int ssid)
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001258{
Takashi Iwai94701952009-06-08 18:10:32 +02001259 const struct snd_pci_quirk *p;
1260 const struct snd_pci_quirk *list;
Takashi Iwai408bffd2010-01-14 09:19:46 +01001261 u16 vendor_id, device_id;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001262
Takashi Iwai94701952009-06-08 18:10:32 +02001263 switch (atc->chip_type) {
1264 case ATC20K1:
1265 atc->chip_name = "20K1";
1266 list = subsys_20k1_list;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001267 break;
Takashi Iwai94701952009-06-08 18:10:32 +02001268 case ATC20K2:
1269 atc->chip_name = "20K2";
1270 list = subsys_20k2_list;
1271 break;
1272 default:
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001273 return -ENOENT;
Takashi Iwai94701952009-06-08 18:10:32 +02001274 }
Takashi Iwai408bffd2010-01-14 09:19:46 +01001275 if (ssid) {
1276 vendor_id = ssid >> 16;
1277 device_id = ssid & 0xffff;
1278 } else {
1279 vendor_id = atc->pci->subsystem_vendor;
1280 device_id = atc->pci->subsystem_device;
1281 }
1282 p = snd_pci_quirk_lookup_id(vendor_id, device_id, list);
Takashi Iwaia8f43102009-06-22 07:36:52 +02001283 if (p) {
1284 if (p->value < 0) {
1285 printk(KERN_ERR "ctxfi: "
1286 "Device %04x:%04x is black-listed\n",
Takashi Iwai408bffd2010-01-14 09:19:46 +01001287 vendor_id, device_id);
Takashi Iwaia8f43102009-06-22 07:36:52 +02001288 return -ENOENT;
1289 }
1290 atc->model = p->value;
1291 } else {
1292 if (atc->chip_type == ATC20K1)
1293 atc->model = CT20K1_UNKNOWN;
1294 else
1295 atc->model = CT20K2_UNKNOWN;
1296 }
Takashi Iwai94701952009-06-08 18:10:32 +02001297 atc->model_name = ct_subsys_name[atc->model];
1298 snd_printd("ctxfi: chip %s model %s (%04x:%04x) is found\n",
1299 atc->chip_name, atc->model_name,
Takashi Iwai408bffd2010-01-14 09:19:46 +01001300 vendor_id, device_id);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001301 return 0;
1302}
1303
Bill Pembertone23e7a12012-12-06 12:35:10 -05001304int ct_atc_create_alsa_devs(struct ct_atc *atc)
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001305{
1306 enum CTALSADEVS i;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001307 int err;
1308
Takashi Iwai94701952009-06-08 18:10:32 +02001309 alsa_dev_funcs[MIXER].public_name = atc->chip_name;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001310
1311 for (i = 0; i < NUM_CTALSADEVS; i++) {
Takashi Iwai35ebf6e2009-07-22 17:12:34 +02001312 if (!alsa_dev_funcs[i].create)
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001313 continue;
1314
1315 err = alsa_dev_funcs[i].create(atc, i,
1316 alsa_dev_funcs[i].public_name);
1317 if (err) {
Takashi Iwaib3e0afe2009-05-14 15:19:30 +02001318 printk(KERN_ERR "ctxfi: "
1319 "Creating alsa device %d failed!\n", i);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001320 return err;
1321 }
1322 }
1323
1324 return 0;
1325}
1326
Bill Pembertone23e7a12012-12-06 12:35:10 -05001327static int atc_create_hw_devs(struct ct_atc *atc)
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001328{
Takashi Iwai514eef92009-06-08 14:57:57 +02001329 struct hw *hw;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001330 struct card_conf info = {0};
Takashi Iwai514eef92009-06-08 14:57:57 +02001331 int i, err;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001332
Takashi Iwai94701952009-06-08 18:10:32 +02001333 err = create_hw_obj(atc->pci, atc->chip_type, atc->model, &hw);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001334 if (err) {
1335 printk(KERN_ERR "Failed to create hw obj!!!\n");
1336 return err;
1337 }
1338 atc->hw = hw;
1339
1340 /* Initialize card hardware. */
1341 info.rsr = atc->rsr;
1342 info.msr = atc->msr;
1343 info.vm_pgt_phys = atc_get_ptp_phys(atc, 0);
1344 err = hw->card_init(hw, &info);
1345 if (err < 0)
1346 return err;
1347
1348 for (i = 0; i < NUM_RSCTYP; i++) {
Takashi Iwai35ebf6e2009-07-22 17:12:34 +02001349 if (!rsc_mgr_funcs[i].create)
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001350 continue;
1351
1352 err = rsc_mgr_funcs[i].create(atc->hw, &atc->rsc_mgrs[i]);
1353 if (err) {
Takashi Iwaib3e0afe2009-05-14 15:19:30 +02001354 printk(KERN_ERR "ctxfi: "
1355 "Failed to create rsc_mgr %d!!!\n", i);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001356 return err;
1357 }
1358 }
1359
1360 return 0;
1361}
1362
Wai Yew CHAY29959a02009-06-22 14:52:34 +02001363static int atc_get_resources(struct ct_atc *atc)
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001364{
1365 struct daio_desc da_desc = {0};
Takashi Iwai514eef92009-06-08 14:57:57 +02001366 struct daio_mgr *daio_mgr;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001367 struct src_desc src_dsc = {0};
Takashi Iwai514eef92009-06-08 14:57:57 +02001368 struct src_mgr *src_mgr;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001369 struct srcimp_desc srcimp_dsc = {0};
Takashi Iwai514eef92009-06-08 14:57:57 +02001370 struct srcimp_mgr *srcimp_mgr;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001371 struct sum_desc sum_dsc = {0};
Takashi Iwai514eef92009-06-08 14:57:57 +02001372 struct sum_mgr *sum_mgr;
Harry Butterworth55309212011-06-11 16:02:06 +08001373 int err, i, num_srcs, num_daios;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001374
Harry Butterworth55309212011-06-11 16:02:06 +08001375 num_daios = ((atc->model == CTSB1270) ? 8 : 7);
1376 num_srcs = ((atc->model == CTSB1270) ? 6 : 4);
1377
1378 atc->daios = kzalloc(sizeof(void *)*num_daios, GFP_KERNEL);
Takashi Iwai35ebf6e2009-07-22 17:12:34 +02001379 if (!atc->daios)
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001380 return -ENOMEM;
1381
Harry Butterworth55309212011-06-11 16:02:06 +08001382 atc->srcs = kzalloc(sizeof(void *)*num_srcs, GFP_KERNEL);
Takashi Iwai35ebf6e2009-07-22 17:12:34 +02001383 if (!atc->srcs)
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001384 return -ENOMEM;
1385
Harry Butterworth55309212011-06-11 16:02:06 +08001386 atc->srcimps = kzalloc(sizeof(void *)*num_srcs, GFP_KERNEL);
Takashi Iwai35ebf6e2009-07-22 17:12:34 +02001387 if (!atc->srcimps)
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001388 return -ENOMEM;
1389
1390 atc->pcm = kzalloc(sizeof(void *)*(2*4), GFP_KERNEL);
Takashi Iwai35ebf6e2009-07-22 17:12:34 +02001391 if (!atc->pcm)
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001392 return -ENOMEM;
1393
1394 daio_mgr = (struct daio_mgr *)atc->rsc_mgrs[DAIO];
1395 da_desc.msr = atc->msr;
Harry Butterworth55309212011-06-11 16:02:06 +08001396 for (i = 0, atc->n_daio = 0; i < num_daios; i++) {
1397 da_desc.type = (atc->model != CTSB073X) ? i :
1398 ((i == SPDIFIO) ? SPDIFI1 : i);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001399 err = daio_mgr->get_daio(daio_mgr, &da_desc,
1400 (struct daio **)&atc->daios[i]);
1401 if (err) {
Takashi Iwaib3e0afe2009-05-14 15:19:30 +02001402 printk(KERN_ERR "ctxfi: Failed to get DAIO "
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001403 "resource %d!!!\n", i);
1404 return err;
1405 }
1406 atc->n_daio++;
1407 }
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001408
1409 src_mgr = atc->rsc_mgrs[SRC];
1410 src_dsc.multi = 1;
1411 src_dsc.msr = atc->msr;
1412 src_dsc.mode = ARCRW;
Harry Butterworth55309212011-06-11 16:02:06 +08001413 for (i = 0, atc->n_src = 0; i < num_srcs; i++) {
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001414 err = src_mgr->get_src(src_mgr, &src_dsc,
1415 (struct src **)&atc->srcs[i]);
1416 if (err)
1417 return err;
1418
1419 atc->n_src++;
1420 }
1421
1422 srcimp_mgr = atc->rsc_mgrs[SRCIMP];
Harry Butterworth55309212011-06-11 16:02:06 +08001423 srcimp_dsc.msr = 8;
1424 for (i = 0, atc->n_srcimp = 0; i < num_srcs; i++) {
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001425 err = srcimp_mgr->get_srcimp(srcimp_mgr, &srcimp_dsc,
1426 (struct srcimp **)&atc->srcimps[i]);
1427 if (err)
1428 return err;
1429
1430 atc->n_srcimp++;
1431 }
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001432
1433 sum_mgr = atc->rsc_mgrs[SUM];
1434 sum_dsc.msr = atc->msr;
1435 for (i = 0, atc->n_pcm = 0; i < (2*4); i++) {
1436 err = sum_mgr->get_sum(sum_mgr, &sum_dsc,
1437 (struct sum **)&atc->pcm[i]);
1438 if (err)
1439 return err;
1440
1441 atc->n_pcm++;
1442 }
1443
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001444 return 0;
1445}
1446
Wai Yew CHAY29959a02009-06-22 14:52:34 +02001447static void
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001448atc_connect_dai(struct src_mgr *src_mgr, struct dai *dai,
1449 struct src **srcs, struct srcimp **srcimps)
1450{
1451 struct rsc *rscs[2] = {NULL};
Takashi Iwai514eef92009-06-08 14:57:57 +02001452 struct src *src;
1453 struct srcimp *srcimp;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001454 int i = 0;
1455
1456 rscs[0] = &dai->daio.rscl;
1457 rscs[1] = &dai->daio.rscr;
1458 for (i = 0; i < 2; i++) {
1459 src = srcs[i];
1460 srcimp = srcimps[i];
1461 srcimp->ops->map(srcimp, src, rscs[i]);
1462 src_mgr->src_disable(src_mgr, src);
1463 }
1464
1465 src_mgr->commit_write(src_mgr); /* Actually disable SRCs */
1466
1467 src = srcs[0];
1468 src->ops->set_pm(src, 1);
1469 for (i = 0; i < 2; i++) {
1470 src = srcs[i];
1471 src->ops->set_state(src, SRC_STATE_RUN);
1472 src->ops->commit_write(src);
1473 src_mgr->src_enable_s(src_mgr, src);
1474 }
1475
1476 dai->ops->set_srt_srcl(dai, &(srcs[0]->rsc));
1477 dai->ops->set_srt_srcr(dai, &(srcs[1]->rsc));
1478
1479 dai->ops->set_enb_src(dai, 1);
1480 dai->ops->set_enb_srt(dai, 1);
1481 dai->ops->commit_write(dai);
1482
1483 src_mgr->commit_write(src_mgr); /* Synchronously enable SRCs */
1484}
1485
Wai Yew CHAY29959a02009-06-22 14:52:34 +02001486static void atc_connect_resources(struct ct_atc *atc)
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001487{
Takashi Iwai514eef92009-06-08 14:57:57 +02001488 struct dai *dai;
1489 struct dao *dao;
1490 struct src *src;
1491 struct sum *sum;
1492 struct ct_mixer *mixer;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001493 struct rsc *rscs[2] = {NULL};
Takashi Iwai514eef92009-06-08 14:57:57 +02001494 int i, j;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001495
1496 mixer = atc->mixer;
1497
1498 for (i = MIX_WAVE_FRONT, j = LINEO1; i <= MIX_SPDIF_OUT; i++, j++) {
1499 mixer->get_output_ports(mixer, i, &rscs[0], &rscs[1]);
1500 dao = container_of(atc->daios[j], struct dao, daio);
1501 dao->ops->set_left_input(dao, rscs[0]);
1502 dao->ops->set_right_input(dao, rscs[1]);
1503 }
1504
1505 dai = container_of(atc->daios[LINEIM], struct dai, daio);
1506 atc_connect_dai(atc->rsc_mgrs[SRC], dai,
1507 (struct src **)&atc->srcs[2],
1508 (struct srcimp **)&atc->srcimps[2]);
1509 src = atc->srcs[2];
1510 mixer->set_input_left(mixer, MIX_LINE_IN, &src->rsc);
1511 src = atc->srcs[3];
1512 mixer->set_input_right(mixer, MIX_LINE_IN, &src->rsc);
1513
Harry Butterworth55309212011-06-11 16:02:06 +08001514 if (atc->model == CTSB1270) {
1515 /* Titanium HD has a dedicated ADC for the Mic. */
1516 dai = container_of(atc->daios[MIC], struct dai, daio);
1517 atc_connect_dai(atc->rsc_mgrs[SRC], dai,
1518 (struct src **)&atc->srcs[4],
1519 (struct srcimp **)&atc->srcimps[4]);
1520 src = atc->srcs[4];
1521 mixer->set_input_left(mixer, MIX_MIC_IN, &src->rsc);
1522 src = atc->srcs[5];
1523 mixer->set_input_right(mixer, MIX_MIC_IN, &src->rsc);
1524 }
1525
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001526 dai = container_of(atc->daios[SPDIFIO], struct dai, daio);
1527 atc_connect_dai(atc->rsc_mgrs[SRC], dai,
1528 (struct src **)&atc->srcs[0],
1529 (struct srcimp **)&atc->srcimps[0]);
1530
1531 src = atc->srcs[0];
1532 mixer->set_input_left(mixer, MIX_SPDIF_IN, &src->rsc);
1533 src = atc->srcs[1];
1534 mixer->set_input_right(mixer, MIX_SPDIF_IN, &src->rsc);
1535
1536 for (i = MIX_PCMI_FRONT, j = 0; i <= MIX_PCMI_SURROUND; i++, j += 2) {
1537 sum = atc->pcm[j];
1538 mixer->set_input_left(mixer, i, &sum->rsc);
1539 sum = atc->pcm[j+1];
1540 mixer->set_input_right(mixer, i, &sum->rsc);
1541 }
1542}
1543
Takashi Iwaic7561cd2012-08-14 18:12:04 +02001544#ifdef CONFIG_PM_SLEEP
Takashi Iwai68cb2b52012-07-02 15:20:37 +02001545static int atc_suspend(struct ct_atc *atc)
Wai Yew CHAY29959a02009-06-22 14:52:34 +02001546{
1547 int i;
1548 struct hw *hw = atc->hw;
1549
1550 snd_power_change_state(atc->card, SNDRV_CTL_POWER_D3hot);
1551
1552 for (i = FRONT; i < NUM_PCMS; i++) {
1553 if (!atc->pcms[i])
1554 continue;
1555
1556 snd_pcm_suspend_all(atc->pcms[i]);
1557 }
1558
1559 atc_release_resources(atc);
1560
Takashi Iwai68cb2b52012-07-02 15:20:37 +02001561 hw->suspend(hw);
Wai Yew CHAY29959a02009-06-22 14:52:34 +02001562
1563 return 0;
1564}
1565
1566static int atc_hw_resume(struct ct_atc *atc)
1567{
1568 struct hw *hw = atc->hw;
1569 struct card_conf info = {0};
1570
1571 /* Re-initialize card hardware. */
1572 info.rsr = atc->rsr;
1573 info.msr = atc->msr;
1574 info.vm_pgt_phys = atc_get_ptp_phys(atc, 0);
1575 return hw->resume(hw, &info);
1576}
1577
1578static int atc_resources_resume(struct ct_atc *atc)
1579{
1580 struct ct_mixer *mixer;
1581 int err = 0;
1582
1583 /* Get resources */
1584 err = atc_get_resources(atc);
1585 if (err < 0) {
1586 atc_release_resources(atc);
1587 return err;
1588 }
1589
1590 /* Build topology */
1591 atc_connect_resources(atc);
1592
1593 mixer = atc->mixer;
1594 mixer->resume(mixer);
1595
1596 return 0;
1597}
1598
1599static int atc_resume(struct ct_atc *atc)
1600{
1601 int err = 0;
1602
1603 /* Do hardware resume. */
1604 err = atc_hw_resume(atc);
1605 if (err < 0) {
1606 printk(KERN_ERR "ctxfi: pci_enable_device failed, "
1607 "disabling device\n");
1608 snd_card_disconnect(atc->card);
1609 return err;
1610 }
1611
1612 err = atc_resources_resume(atc);
1613 if (err < 0)
1614 return err;
1615
1616 snd_power_change_state(atc->card, SNDRV_CTL_POWER_D0);
1617
1618 return 0;
1619}
1620#endif
1621
Bill Pembertone23e7a12012-12-06 12:35:10 -05001622static struct ct_atc atc_preset = {
Takashi Iwai2a36f672009-06-05 16:34:10 +02001623 .map_audio_buffer = ct_map_audio_buffer,
1624 .unmap_audio_buffer = ct_unmap_audio_buffer,
1625 .pcm_playback_prepare = atc_pcm_playback_prepare,
1626 .pcm_release_resources = atc_pcm_release_resources,
1627 .pcm_playback_start = atc_pcm_playback_start,
1628 .pcm_playback_stop = atc_pcm_stop,
1629 .pcm_playback_position = atc_pcm_playback_position,
1630 .pcm_capture_prepare = atc_pcm_capture_prepare,
1631 .pcm_capture_start = atc_pcm_capture_start,
1632 .pcm_capture_stop = atc_pcm_stop,
1633 .pcm_capture_position = atc_pcm_capture_position,
1634 .spdif_passthru_playback_prepare = spdif_passthru_playback_prepare,
1635 .get_ptp_phys = atc_get_ptp_phys,
1636 .select_line_in = atc_select_line_in,
1637 .select_mic_in = atc_select_mic_in,
1638 .select_digit_io = atc_select_digit_io,
1639 .line_front_unmute = atc_line_front_unmute,
1640 .line_surround_unmute = atc_line_surround_unmute,
1641 .line_clfe_unmute = atc_line_clfe_unmute,
1642 .line_rear_unmute = atc_line_rear_unmute,
1643 .line_in_unmute = atc_line_in_unmute,
Harry Butterworth55309212011-06-11 16:02:06 +08001644 .mic_unmute = atc_mic_unmute,
Takashi Iwai2a36f672009-06-05 16:34:10 +02001645 .spdif_out_unmute = atc_spdif_out_unmute,
1646 .spdif_in_unmute = atc_spdif_in_unmute,
1647 .spdif_out_get_status = atc_spdif_out_get_status,
1648 .spdif_out_set_status = atc_spdif_out_set_status,
1649 .spdif_out_passthru = atc_spdif_out_passthru,
Harry Butterworthb028b812011-06-11 17:41:13 +08001650 .capabilities = atc_capabilities,
Harry Butterworth55309212011-06-11 16:02:06 +08001651 .output_switch_get = atc_output_switch_get,
1652 .output_switch_put = atc_output_switch_put,
Harry Butterworth55309212011-06-11 16:02:06 +08001653 .mic_source_switch_get = atc_mic_source_switch_get,
1654 .mic_source_switch_put = atc_mic_source_switch_put,
Takashi Iwaic7561cd2012-08-14 18:12:04 +02001655#ifdef CONFIG_PM_SLEEP
Wai Yew CHAY29959a02009-06-22 14:52:34 +02001656 .suspend = atc_suspend,
1657 .resume = atc_resume,
1658#endif
Takashi Iwai2a36f672009-06-05 16:34:10 +02001659};
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001660
1661/**
1662 * ct_atc_create - create and initialize a hardware manager
1663 * @card: corresponding alsa card object
1664 * @pci: corresponding kernel pci device object
1665 * @ratc: return created object address in it
1666 *
1667 * Creates and initializes a hardware manager.
1668 *
1669 * Creates kmallocated ct_atc structure. Initializes hardware.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001670 * Returns 0 if succeeds, or negative error code if fails.
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001671 */
1672
Bill Pembertone23e7a12012-12-06 12:35:10 -05001673int ct_atc_create(struct snd_card *card, struct pci_dev *pci,
1674 unsigned int rsr, unsigned int msr,
1675 int chip_type, unsigned int ssid,
1676 struct ct_atc **ratc)
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001677{
Takashi Iwai514eef92009-06-08 14:57:57 +02001678 struct ct_atc *atc;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001679 static struct snd_device_ops ops = {
1680 .dev_free = atc_dev_free,
1681 };
Takashi Iwai514eef92009-06-08 14:57:57 +02001682 int err;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001683
1684 *ratc = NULL;
1685
1686 atc = kzalloc(sizeof(*atc), GFP_KERNEL);
Takashi Iwai35ebf6e2009-07-22 17:12:34 +02001687 if (!atc)
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001688 return -ENOMEM;
1689
Takashi Iwai2a36f672009-06-05 16:34:10 +02001690 /* Set operations */
1691 *atc = atc_preset;
1692
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001693 atc->card = card;
1694 atc->pci = pci;
1695 atc->rsr = rsr;
1696 atc->msr = msr;
Takashi Iwai94701952009-06-08 18:10:32 +02001697 atc->chip_type = chip_type;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001698
Takashi Iwai635c2652009-06-13 10:12:59 +02001699 mutex_init(&atc->atc_mutex);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001700
1701 /* Find card model */
Takashi Iwai408bffd2010-01-14 09:19:46 +01001702 err = atc_identify_card(atc, ssid);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001703 if (err < 0) {
1704 printk(KERN_ERR "ctatc: Card not recognised\n");
1705 goto error1;
1706 }
1707
1708 /* Set up device virtual memory management object */
Jaroslav Kysela21956b62010-02-02 19:58:25 +01001709 err = ct_vm_create(&atc->vm, pci);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001710 if (err < 0)
1711 goto error1;
1712
1713 /* Create all atc hw devices */
1714 err = atc_create_hw_devs(atc);
1715 if (err < 0)
1716 goto error1;
1717
Wai Yew CHAY29959a02009-06-22 14:52:34 +02001718 err = ct_mixer_create(atc, (struct ct_mixer **)&atc->mixer);
1719 if (err) {
1720 printk(KERN_ERR "ctxfi: Failed to create mixer obj!!!\n");
1721 goto error1;
1722 }
1723
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001724 /* Get resources */
1725 err = atc_get_resources(atc);
1726 if (err < 0)
1727 goto error1;
1728
1729 /* Build topology */
1730 atc_connect_resources(atc);
1731
Takashi Iwaib7bbf872009-06-05 16:11:07 +02001732 atc->timer = ct_timer_new(atc);
Julia Lawall4d8ce1c2012-08-19 09:02:56 +02001733 if (!atc->timer) {
1734 err = -ENOMEM;
Takashi Iwaib7bbf872009-06-05 16:11:07 +02001735 goto error1;
Julia Lawall4d8ce1c2012-08-19 09:02:56 +02001736 }
Takashi Iwaib7bbf872009-06-05 16:11:07 +02001737
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001738 err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, atc, &ops);
1739 if (err < 0)
1740 goto error1;
1741
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001742 *ratc = atc;
1743 return 0;
1744
1745error1:
1746 ct_atc_destroy(atc);
Takashi Iwaib3e0afe2009-05-14 15:19:30 +02001747 printk(KERN_ERR "ctxfi: Something wrong!!!\n");
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001748 return err;
1749}