blob: 45465907439002b4bf879462f4c6f97955dc9255 [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
Sudip Mukherjee66640892014-09-29 14:33:21 +0530109typedef int (*create_t)(struct hw *, void **);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200110typedef int (*destroy_t)(void *);
111
112static struct {
Sudip Mukherjee66640892014-09-29 14:33:21 +0530113 int (*create)(struct hw *hw, void **rmgr);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200114 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
Sudip Mukherjeea45c4d52014-09-29 14:33:25 +0530174static unsigned int convert_format(snd_pcm_format_t snd_format,
175 struct snd_card *card)
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200176{
177 switch (snd_format) {
178 case SNDRV_PCM_FORMAT_U8:
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200179 return SRC_SF_U8;
180 case SNDRV_PCM_FORMAT_S16_LE:
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200181 return SRC_SF_S16;
182 case SNDRV_PCM_FORMAT_S24_3LE:
183 return SRC_SF_S24;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200184 case SNDRV_PCM_FORMAT_S32_LE:
185 return SRC_SF_S32;
Takashi Iwaid2b9b962009-06-02 14:39:05 +0200186 case SNDRV_PCM_FORMAT_FLOAT_LE:
187 return SRC_SF_F32;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200188 default:
Sudip Mukherjee0cae90a2014-09-29 14:33:26 +0530189 dev_err(card->dev, "not recognized snd format is %d\n",
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200190 snd_format);
191 return SRC_SF_S16;
192 }
193}
194
195static unsigned int
196atc_get_pitch(unsigned int input_rate, unsigned int output_rate)
197{
Takashi Iwai514eef92009-06-08 14:57:57 +0200198 unsigned int pitch;
199 int b;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200200
201 /* get pitch and convert to fixed-point 8.24 format. */
202 pitch = (input_rate / output_rate) << 24;
203 input_rate %= output_rate;
204 input_rate /= 100;
205 output_rate /= 100;
206 for (b = 31; ((b >= 0) && !(input_rate >> b)); )
207 b--;
208
209 if (b >= 0) {
210 input_rate <<= (31 - b);
211 input_rate /= output_rate;
212 b = 24 - (31 - b);
213 if (b >= 0)
214 input_rate <<= b;
215 else
216 input_rate >>= -b;
217
218 pitch |= input_rate;
219 }
220
221 return pitch;
222}
223
224static int select_rom(unsigned int pitch)
225{
Takashi Iwai35ebf6e2009-07-22 17:12:34 +0200226 if (pitch > 0x00428f5c && pitch < 0x01b851ec) {
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200227 /* 0.26 <= pitch <= 1.72 */
228 return 1;
Takashi Iwai35ebf6e2009-07-22 17:12:34 +0200229 } else if (pitch == 0x01d66666 || pitch == 0x01d66667) {
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200230 /* pitch == 1.8375 */
231 return 2;
Takashi Iwai35ebf6e2009-07-22 17:12:34 +0200232 } else if (pitch == 0x02000000) {
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200233 /* pitch == 2 */
234 return 3;
Roel Kluin84ed1a12009-10-23 16:03:08 +0200235 } else if (pitch <= 0x08000000) {
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200236 /* 0 <= pitch <= 8 */
237 return 0;
238 } else {
239 return -ENOENT;
240 }
241}
242
243static int atc_pcm_playback_prepare(struct ct_atc *atc, struct ct_atc_pcm *apcm)
244{
245 struct src_mgr *src_mgr = atc->rsc_mgrs[SRC];
246 struct amixer_mgr *amixer_mgr = atc->rsc_mgrs[AMIXER];
247 struct src_desc desc = {0};
248 struct amixer_desc mix_dsc = {0};
Takashi Iwai514eef92009-06-08 14:57:57 +0200249 struct src *src;
250 struct amixer *amixer;
251 int err;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200252 int n_amixer = apcm->substream->runtime->channels, i = 0;
253 int device = apcm->substream->pcm->device;
Takashi Iwai514eef92009-06-08 14:57:57 +0200254 unsigned int pitch;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200255
Takashi Iwai822fa192009-06-02 14:12:17 +0200256 /* first release old resources */
Wai Yew CHAY29959a02009-06-22 14:52:34 +0200257 atc_pcm_release_resources(atc, apcm);
Takashi Iwai822fa192009-06-02 14:12:17 +0200258
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));
Sudip Mukherjeea45c4d52014-09-29 14:33:25 +0530272 src->ops->set_sf(src, convert_format(apcm->substream->runtime->format,
273 atc->card));
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200274 src->ops->set_pm(src, (src->ops->next_interleave(src) != NULL));
275
276 /* Get AMIXER resource */
277 n_amixer = (n_amixer < 2) ? 2 : n_amixer;
278 apcm->amixers = kzalloc(sizeof(void *)*n_amixer, GFP_KERNEL);
Takashi Iwai35ebf6e2009-07-22 17:12:34 +0200279 if (!apcm->amixers) {
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200280 err = -ENOMEM;
281 goto error1;
282 }
283 mix_dsc.msr = atc->msr;
284 for (i = 0, apcm->n_amixer = 0; i < n_amixer; i++) {
285 err = amixer_mgr->get_amixer(amixer_mgr, &mix_dsc,
286 (struct amixer **)&apcm->amixers[i]);
287 if (err)
288 goto error1;
289
290 apcm->n_amixer++;
291 }
292
293 /* Set up device virtual mem map */
294 err = ct_map_audio_buffer(atc, apcm);
295 if (err < 0)
296 goto error1;
297
298 /* Connect resources */
299 src = apcm->src;
300 for (i = 0; i < n_amixer; i++) {
301 amixer = apcm->amixers[i];
Takashi Iwai635c2652009-06-13 10:12:59 +0200302 mutex_lock(&atc->atc_mutex);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200303 amixer->ops->setup(amixer, &src->rsc,
304 INIT_VOL, atc->pcm[i+device*2]);
Takashi Iwai635c2652009-06-13 10:12:59 +0200305 mutex_unlock(&atc->atc_mutex);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200306 src = src->ops->next_interleave(src);
Takashi Iwai35ebf6e2009-07-22 17:12:34 +0200307 if (!src)
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200308 src = apcm->src;
309 }
310
Takashi Iwaib7bbf872009-06-05 16:11:07 +0200311 ct_timer_prepare(apcm->timer);
312
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200313 return 0;
314
315error1:
316 atc_pcm_release_resources(atc, apcm);
317 return err;
318}
319
320static int
321atc_pcm_release_resources(struct ct_atc *atc, struct ct_atc_pcm *apcm)
322{
323 struct src_mgr *src_mgr = atc->rsc_mgrs[SRC];
324 struct srcimp_mgr *srcimp_mgr = atc->rsc_mgrs[SRCIMP];
325 struct amixer_mgr *amixer_mgr = atc->rsc_mgrs[AMIXER];
326 struct sum_mgr *sum_mgr = atc->rsc_mgrs[SUM];
Takashi Iwai514eef92009-06-08 14:57:57 +0200327 struct srcimp *srcimp;
328 int i;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200329
Takashi Iwai35ebf6e2009-07-22 17:12:34 +0200330 if (apcm->srcimps) {
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200331 for (i = 0; i < apcm->n_srcimp; i++) {
332 srcimp = apcm->srcimps[i];
333 srcimp->ops->unmap(srcimp);
334 srcimp_mgr->put_srcimp(srcimp_mgr, srcimp);
335 apcm->srcimps[i] = NULL;
336 }
337 kfree(apcm->srcimps);
338 apcm->srcimps = NULL;
339 }
340
Takashi Iwai35ebf6e2009-07-22 17:12:34 +0200341 if (apcm->srccs) {
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200342 for (i = 0; i < apcm->n_srcc; i++) {
343 src_mgr->put_src(src_mgr, apcm->srccs[i]);
344 apcm->srccs[i] = NULL;
345 }
346 kfree(apcm->srccs);
347 apcm->srccs = NULL;
348 }
349
Takashi Iwai35ebf6e2009-07-22 17:12:34 +0200350 if (apcm->amixers) {
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200351 for (i = 0; i < apcm->n_amixer; i++) {
352 amixer_mgr->put_amixer(amixer_mgr, apcm->amixers[i]);
353 apcm->amixers[i] = NULL;
354 }
355 kfree(apcm->amixers);
356 apcm->amixers = NULL;
357 }
358
Takashi Iwai35ebf6e2009-07-22 17:12:34 +0200359 if (apcm->mono) {
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200360 sum_mgr->put_sum(sum_mgr, apcm->mono);
361 apcm->mono = NULL;
362 }
363
Takashi Iwai35ebf6e2009-07-22 17:12:34 +0200364 if (apcm->src) {
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200365 src_mgr->put_src(src_mgr, apcm->src);
366 apcm->src = NULL;
367 }
368
Takashi Iwai35ebf6e2009-07-22 17:12:34 +0200369 if (apcm->vm_block) {
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200370 /* Undo device virtual mem map */
371 ct_unmap_audio_buffer(atc, apcm);
372 apcm->vm_block = NULL;
373 }
374
375 return 0;
376}
377
378static int atc_pcm_playback_start(struct ct_atc *atc, struct ct_atc_pcm *apcm)
379{
Takashi Iwai514eef92009-06-08 14:57:57 +0200380 unsigned int max_cisz;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200381 struct src *src = apcm->src;
382
Takashi Iwaic399f3b2009-06-09 08:16:20 +0200383 if (apcm->started)
384 return 0;
385 apcm->started = 1;
386
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200387 max_cisz = src->multi * src->rsc.msr;
388 max_cisz = 0x80 * (max_cisz < 8 ? max_cisz : 8);
389
390 src->ops->set_sa(src, apcm->vm_block->addr);
391 src->ops->set_la(src, apcm->vm_block->addr + apcm->vm_block->size);
392 src->ops->set_ca(src, apcm->vm_block->addr + max_cisz);
393 src->ops->set_cisz(src, max_cisz);
394
395 src->ops->set_bm(src, 1);
396 src->ops->set_state(src, SRC_STATE_INIT);
397 src->ops->commit_write(src);
398
Takashi Iwaib7bbf872009-06-05 16:11:07 +0200399 ct_timer_start(apcm->timer);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200400 return 0;
401}
402
403static int atc_pcm_stop(struct ct_atc *atc, struct ct_atc_pcm *apcm)
404{
Takashi Iwai514eef92009-06-08 14:57:57 +0200405 struct src *src;
406 int i;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200407
Takashi Iwaib7bbf872009-06-05 16:11:07 +0200408 ct_timer_stop(apcm->timer);
409
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200410 src = apcm->src;
411 src->ops->set_bm(src, 0);
412 src->ops->set_state(src, SRC_STATE_OFF);
413 src->ops->commit_write(src);
414
Takashi Iwai35ebf6e2009-07-22 17:12:34 +0200415 if (apcm->srccs) {
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200416 for (i = 0; i < apcm->n_srcc; i++) {
417 src = apcm->srccs[i];
418 src->ops->set_bm(src, 0);
419 src->ops->set_state(src, SRC_STATE_OFF);
420 src->ops->commit_write(src);
421 }
422 }
423
424 apcm->started = 0;
425
426 return 0;
427}
428
429static int
430atc_pcm_playback_position(struct ct_atc *atc, struct ct_atc_pcm *apcm)
431{
432 struct src *src = apcm->src;
Takashi Iwai514eef92009-06-08 14:57:57 +0200433 u32 size, max_cisz;
434 int position;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200435
Takashi Iwai5242bc72009-06-09 08:17:14 +0200436 if (!src)
437 return 0;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200438 position = src->ops->get_ca(src);
439
Sarah Bessmere240a462014-01-09 11:36:31 -0800440 if (position < apcm->vm_block->addr) {
441 snd_printdd("ctxfi: bad ca - ca=0x%08x, vba=0x%08x, vbs=0x%08x\n", position, apcm->vm_block->addr, apcm->vm_block->size);
442 position = apcm->vm_block->addr;
443 }
444
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200445 size = apcm->vm_block->size;
446 max_cisz = src->multi * src->rsc.msr;
447 max_cisz = 128 * (max_cisz < 8 ? max_cisz : 8);
448
449 return (position + size - max_cisz - apcm->vm_block->addr) % size;
450}
451
452struct src_node_conf_t {
453 unsigned int pitch;
454 unsigned int msr:8;
455 unsigned int mix_msr:8;
456 unsigned int imp_msr:8;
457 unsigned int vo:1;
458};
459
460static void setup_src_node_conf(struct ct_atc *atc, struct ct_atc_pcm *apcm,
461 struct src_node_conf_t *conf, int *n_srcc)
462{
Takashi Iwai514eef92009-06-08 14:57:57 +0200463 unsigned int pitch;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200464
465 /* get pitch and convert to fixed-point 8.24 format. */
466 pitch = atc_get_pitch((atc->rsr * atc->msr),
467 apcm->substream->runtime->rate);
468 *n_srcc = 0;
469
Harry Butterworth55309212011-06-11 16:02:06 +0800470 if (1 == atc->msr) { /* FIXME: do we really need SRC here if pitch==1 */
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200471 *n_srcc = apcm->substream->runtime->channels;
472 conf[0].pitch = pitch;
473 conf[0].mix_msr = conf[0].imp_msr = conf[0].msr = 1;
474 conf[0].vo = 1;
Harry Butterworth55309212011-06-11 16:02:06 +0800475 } else if (2 <= atc->msr) {
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200476 if (0x8000000 < pitch) {
477 /* Need two-stage SRCs, SRCIMPs and
478 * AMIXERs for converting format */
479 conf[0].pitch = (atc->msr << 24);
480 conf[0].msr = conf[0].mix_msr = 1;
481 conf[0].imp_msr = atc->msr;
482 conf[0].vo = 0;
483 conf[1].pitch = atc_get_pitch(atc->rsr,
484 apcm->substream->runtime->rate);
485 conf[1].msr = conf[1].mix_msr = conf[1].imp_msr = 1;
486 conf[1].vo = 1;
487 *n_srcc = apcm->substream->runtime->channels * 2;
488 } else if (0x1000000 < pitch) {
489 /* Need one-stage SRCs, SRCIMPs and
490 * AMIXERs for converting format */
491 conf[0].pitch = pitch;
492 conf[0].msr = conf[0].mix_msr
493 = conf[0].imp_msr = atc->msr;
494 conf[0].vo = 1;
495 *n_srcc = apcm->substream->runtime->channels;
496 }
497 }
498}
499
500static int
501atc_pcm_capture_get_resources(struct ct_atc *atc, struct ct_atc_pcm *apcm)
502{
503 struct src_mgr *src_mgr = atc->rsc_mgrs[SRC];
504 struct srcimp_mgr *srcimp_mgr = atc->rsc_mgrs[SRCIMP];
505 struct amixer_mgr *amixer_mgr = atc->rsc_mgrs[AMIXER];
506 struct sum_mgr *sum_mgr = atc->rsc_mgrs[SUM];
507 struct src_desc src_dsc = {0};
Takashi Iwai514eef92009-06-08 14:57:57 +0200508 struct src *src;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200509 struct srcimp_desc srcimp_dsc = {0};
Takashi Iwai514eef92009-06-08 14:57:57 +0200510 struct srcimp *srcimp;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200511 struct amixer_desc mix_dsc = {0};
512 struct sum_desc sum_dsc = {0};
Takashi Iwai514eef92009-06-08 14:57:57 +0200513 unsigned int pitch;
514 int multi, err, i;
515 int n_srcimp, n_amixer, n_srcc, n_sum;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200516 struct src_node_conf_t src_node_conf[2] = {{0} };
517
Takashi Iwai822fa192009-06-02 14:12:17 +0200518 /* first release old resources */
Takashi Iwaia5990dc2009-06-09 08:19:02 +0200519 atc_pcm_release_resources(atc, apcm);
Takashi Iwai822fa192009-06-02 14:12:17 +0200520
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200521 /* The numbers of converting SRCs and SRCIMPs should be determined
522 * by pitch value. */
523
524 multi = apcm->substream->runtime->channels;
525
526 /* get pitch and convert to fixed-point 8.24 format. */
527 pitch = atc_get_pitch((atc->rsr * atc->msr),
528 apcm->substream->runtime->rate);
529
530 setup_src_node_conf(atc, apcm, src_node_conf, &n_srcc);
531 n_sum = (1 == multi) ? 1 : 0;
Takashi Iwai514eef92009-06-08 14:57:57 +0200532 n_amixer = n_sum * 2 + n_srcc;
533 n_srcimp = n_srcc;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200534 if ((multi > 1) && (0x8000000 >= pitch)) {
535 /* Need extra AMIXERs and SRCIMPs for special treatment
536 * of interleaved recording of conjugate channels */
537 n_amixer += multi * atc->msr;
538 n_srcimp += multi * atc->msr;
539 } else {
540 n_srcimp += multi;
541 }
542
543 if (n_srcc) {
544 apcm->srccs = kzalloc(sizeof(void *)*n_srcc, GFP_KERNEL);
Takashi Iwai35ebf6e2009-07-22 17:12:34 +0200545 if (!apcm->srccs)
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200546 return -ENOMEM;
547 }
548 if (n_amixer) {
549 apcm->amixers = kzalloc(sizeof(void *)*n_amixer, GFP_KERNEL);
Takashi Iwai35ebf6e2009-07-22 17:12:34 +0200550 if (!apcm->amixers) {
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200551 err = -ENOMEM;
552 goto error1;
553 }
554 }
555 apcm->srcimps = kzalloc(sizeof(void *)*n_srcimp, GFP_KERNEL);
Takashi Iwai35ebf6e2009-07-22 17:12:34 +0200556 if (!apcm->srcimps) {
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200557 err = -ENOMEM;
558 goto error1;
559 }
560
561 /* Allocate SRCs for sample rate conversion if needed */
562 src_dsc.multi = 1;
563 src_dsc.mode = ARCRW;
564 for (i = 0, apcm->n_srcc = 0; i < n_srcc; i++) {
565 src_dsc.msr = src_node_conf[i/multi].msr;
566 err = src_mgr->get_src(src_mgr, &src_dsc,
567 (struct src **)&apcm->srccs[i]);
568 if (err)
569 goto error1;
570
571 src = apcm->srccs[i];
572 pitch = src_node_conf[i/multi].pitch;
573 src->ops->set_pitch(src, pitch);
574 src->ops->set_rom(src, select_rom(pitch));
575 src->ops->set_vo(src, src_node_conf[i/multi].vo);
576
577 apcm->n_srcc++;
578 }
579
580 /* Allocate AMIXERs for routing SRCs of conversion if needed */
581 for (i = 0, apcm->n_amixer = 0; i < n_amixer; i++) {
582 if (i < (n_sum*2))
583 mix_dsc.msr = atc->msr;
584 else if (i < (n_sum*2+n_srcc))
585 mix_dsc.msr = src_node_conf[(i-n_sum*2)/multi].mix_msr;
586 else
587 mix_dsc.msr = 1;
588
589 err = amixer_mgr->get_amixer(amixer_mgr, &mix_dsc,
590 (struct amixer **)&apcm->amixers[i]);
591 if (err)
592 goto error1;
593
594 apcm->n_amixer++;
595 }
596
597 /* Allocate a SUM resource to mix all input channels together */
598 sum_dsc.msr = atc->msr;
599 err = sum_mgr->get_sum(sum_mgr, &sum_dsc, (struct sum **)&apcm->mono);
600 if (err)
601 goto error1;
602
603 pitch = atc_get_pitch((atc->rsr * atc->msr),
604 apcm->substream->runtime->rate);
605 /* Allocate SRCIMP resources */
606 for (i = 0, apcm->n_srcimp = 0; i < n_srcimp; i++) {
607 if (i < (n_srcc))
608 srcimp_dsc.msr = src_node_conf[i/multi].imp_msr;
609 else if (1 == multi)
610 srcimp_dsc.msr = (pitch <= 0x8000000) ? atc->msr : 1;
611 else
612 srcimp_dsc.msr = 1;
613
614 err = srcimp_mgr->get_srcimp(srcimp_mgr, &srcimp_dsc, &srcimp);
615 if (err)
616 goto error1;
617
618 apcm->srcimps[i] = srcimp;
619 apcm->n_srcimp++;
620 }
621
622 /* Allocate a SRC for writing data to host memory */
623 src_dsc.multi = apcm->substream->runtime->channels;
624 src_dsc.msr = 1;
625 src_dsc.mode = MEMWR;
626 err = src_mgr->get_src(src_mgr, &src_dsc, (struct src **)&apcm->src);
627 if (err)
628 goto error1;
629
630 src = apcm->src;
631 src->ops->set_pitch(src, pitch);
632
633 /* Set up device virtual mem map */
634 err = ct_map_audio_buffer(atc, apcm);
635 if (err < 0)
636 goto error1;
637
638 return 0;
639
640error1:
641 atc_pcm_release_resources(atc, apcm);
642 return err;
643}
644
645static int atc_pcm_capture_prepare(struct ct_atc *atc, struct ct_atc_pcm *apcm)
646{
Takashi Iwai514eef92009-06-08 14:57:57 +0200647 struct src *src;
648 struct amixer *amixer;
649 struct srcimp *srcimp;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200650 struct ct_mixer *mixer = atc->mixer;
Takashi Iwai514eef92009-06-08 14:57:57 +0200651 struct sum *mono;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200652 struct rsc *out_ports[8] = {NULL};
Takashi Iwai514eef92009-06-08 14:57:57 +0200653 int err, i, j, n_sum, multi;
654 unsigned int pitch;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200655 int mix_base = 0, imp_base = 0;
656
Wai Yew CHAY29959a02009-06-22 14:52:34 +0200657 atc_pcm_release_resources(atc, apcm);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200658
659 /* Get needed resources. */
660 err = atc_pcm_capture_get_resources(atc, apcm);
661 if (err)
662 return err;
663
664 /* Connect resources */
665 mixer->get_output_ports(mixer, MIX_PCMO_FRONT,
666 &out_ports[0], &out_ports[1]);
667
668 multi = apcm->substream->runtime->channels;
669 if (1 == multi) {
670 mono = apcm->mono;
671 for (i = 0; i < 2; i++) {
672 amixer = apcm->amixers[i];
673 amixer->ops->setup(amixer, out_ports[i],
674 MONO_SUM_SCALE, mono);
675 }
676 out_ports[0] = &mono->rsc;
677 n_sum = 1;
678 mix_base = n_sum * 2;
679 }
680
681 for (i = 0; i < apcm->n_srcc; i++) {
682 src = apcm->srccs[i];
683 srcimp = apcm->srcimps[imp_base+i];
684 amixer = apcm->amixers[mix_base+i];
685 srcimp->ops->map(srcimp, src, out_ports[i%multi]);
686 amixer->ops->setup(amixer, &src->rsc, INIT_VOL, NULL);
687 out_ports[i%multi] = &amixer->rsc;
688 }
689
690 pitch = atc_get_pitch((atc->rsr * atc->msr),
691 apcm->substream->runtime->rate);
692
693 if ((multi > 1) && (pitch <= 0x8000000)) {
694 /* Special connection for interleaved
695 * recording with conjugate channels */
696 for (i = 0; i < multi; i++) {
697 out_ports[i]->ops->master(out_ports[i]);
698 for (j = 0; j < atc->msr; j++) {
699 amixer = apcm->amixers[apcm->n_srcc+j*multi+i];
700 amixer->ops->set_input(amixer, out_ports[i]);
701 amixer->ops->set_scale(amixer, INIT_VOL);
702 amixer->ops->set_sum(amixer, NULL);
703 amixer->ops->commit_raw_write(amixer);
704 out_ports[i]->ops->next_conj(out_ports[i]);
705
706 srcimp = apcm->srcimps[apcm->n_srcc+j*multi+i];
707 srcimp->ops->map(srcimp, apcm->src,
708 &amixer->rsc);
709 }
710 }
711 } else {
712 for (i = 0; i < multi; i++) {
713 srcimp = apcm->srcimps[apcm->n_srcc+i];
714 srcimp->ops->map(srcimp, apcm->src, out_ports[i]);
715 }
716 }
717
Takashi Iwaib7bbf872009-06-05 16:11:07 +0200718 ct_timer_prepare(apcm->timer);
719
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200720 return 0;
721}
722
723static int atc_pcm_capture_start(struct ct_atc *atc, struct ct_atc_pcm *apcm)
724{
Takashi Iwai514eef92009-06-08 14:57:57 +0200725 struct src *src;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200726 struct src_mgr *src_mgr = atc->rsc_mgrs[SRC];
Takashi Iwai514eef92009-06-08 14:57:57 +0200727 int i, multi;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200728
729 if (apcm->started)
730 return 0;
731
732 apcm->started = 1;
733 multi = apcm->substream->runtime->channels;
734 /* Set up converting SRCs */
735 for (i = 0; i < apcm->n_srcc; i++) {
736 src = apcm->srccs[i];
737 src->ops->set_pm(src, ((i%multi) != (multi-1)));
738 src_mgr->src_disable(src_mgr, src);
739 }
740
741 /* Set up recording SRC */
742 src = apcm->src;
Sudip Mukherjeea45c4d52014-09-29 14:33:25 +0530743 src->ops->set_sf(src, convert_format(apcm->substream->runtime->format,
744 atc->card));
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200745 src->ops->set_sa(src, apcm->vm_block->addr);
746 src->ops->set_la(src, apcm->vm_block->addr + apcm->vm_block->size);
747 src->ops->set_ca(src, apcm->vm_block->addr);
748 src_mgr->src_disable(src_mgr, src);
749
750 /* Disable relevant SRCs firstly */
751 src_mgr->commit_write(src_mgr);
752
753 /* Enable SRCs respectively */
754 for (i = 0; i < apcm->n_srcc; i++) {
755 src = apcm->srccs[i];
756 src->ops->set_state(src, SRC_STATE_RUN);
757 src->ops->commit_write(src);
758 src_mgr->src_enable_s(src_mgr, src);
759 }
760 src = apcm->src;
761 src->ops->set_bm(src, 1);
762 src->ops->set_state(src, SRC_STATE_RUN);
763 src->ops->commit_write(src);
764 src_mgr->src_enable_s(src_mgr, src);
765
766 /* Enable relevant SRCs synchronously */
767 src_mgr->commit_write(src_mgr);
768
Takashi Iwaib7bbf872009-06-05 16:11:07 +0200769 ct_timer_start(apcm->timer);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200770 return 0;
771}
772
773static int
774atc_pcm_capture_position(struct ct_atc *atc, struct ct_atc_pcm *apcm)
775{
776 struct src *src = apcm->src;
777
Takashi Iwai5242bc72009-06-09 08:17:14 +0200778 if (!src)
779 return 0;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200780 return src->ops->get_ca(src) - apcm->vm_block->addr;
781}
782
783static int spdif_passthru_playback_get_resources(struct ct_atc *atc,
784 struct ct_atc_pcm *apcm)
785{
786 struct src_mgr *src_mgr = atc->rsc_mgrs[SRC];
787 struct amixer_mgr *amixer_mgr = atc->rsc_mgrs[AMIXER];
788 struct src_desc desc = {0};
789 struct amixer_desc mix_dsc = {0};
Takashi Iwai514eef92009-06-08 14:57:57 +0200790 struct src *src;
791 int err;
792 int n_amixer = apcm->substream->runtime->channels, i;
793 unsigned int pitch, rsr = atc->pll_rate;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200794
Takashi Iwai822fa192009-06-02 14:12:17 +0200795 /* first release old resources */
Takashi Iwaia5990dc2009-06-09 08:19:02 +0200796 atc_pcm_release_resources(atc, apcm);
Takashi Iwai822fa192009-06-02 14:12:17 +0200797
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200798 /* Get SRC resource */
799 desc.multi = apcm->substream->runtime->channels;
800 desc.msr = 1;
801 while (apcm->substream->runtime->rate > (rsr * desc.msr))
802 desc.msr <<= 1;
803
804 desc.mode = MEMRD;
805 err = src_mgr->get_src(src_mgr, &desc, (struct src **)&apcm->src);
806 if (err)
807 goto error1;
808
809 pitch = atc_get_pitch(apcm->substream->runtime->rate, (rsr * desc.msr));
810 src = apcm->src;
811 src->ops->set_pitch(src, pitch);
812 src->ops->set_rom(src, select_rom(pitch));
Sudip Mukherjeea45c4d52014-09-29 14:33:25 +0530813 src->ops->set_sf(src, convert_format(apcm->substream->runtime->format,
814 atc->card));
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200815 src->ops->set_pm(src, (src->ops->next_interleave(src) != NULL));
816 src->ops->set_bp(src, 1);
817
818 /* Get AMIXER resource */
819 n_amixer = (n_amixer < 2) ? 2 : n_amixer;
820 apcm->amixers = kzalloc(sizeof(void *)*n_amixer, GFP_KERNEL);
Takashi Iwai35ebf6e2009-07-22 17:12:34 +0200821 if (!apcm->amixers) {
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200822 err = -ENOMEM;
823 goto error1;
824 }
825 mix_dsc.msr = desc.msr;
826 for (i = 0, apcm->n_amixer = 0; i < n_amixer; i++) {
827 err = amixer_mgr->get_amixer(amixer_mgr, &mix_dsc,
828 (struct amixer **)&apcm->amixers[i]);
829 if (err)
830 goto error1;
831
832 apcm->n_amixer++;
833 }
834
835 /* Set up device virtual mem map */
836 err = ct_map_audio_buffer(atc, apcm);
837 if (err < 0)
838 goto error1;
839
840 return 0;
841
842error1:
843 atc_pcm_release_resources(atc, apcm);
844 return err;
845}
846
Takashi Iwai514eef92009-06-08 14:57:57 +0200847static int atc_pll_init(struct ct_atc *atc, int rate)
848{
849 struct hw *hw = atc->hw;
850 int err;
851 err = hw->pll_init(hw, rate);
852 atc->pll_rate = err ? 0 : rate;
853 return err;
854}
855
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200856static int
857spdif_passthru_playback_setup(struct ct_atc *atc, struct ct_atc_pcm *apcm)
858{
859 struct dao *dao = container_of(atc->daios[SPDIFOO], struct dao, daio);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200860 unsigned int rate = apcm->substream->runtime->rate;
Takashi Iwai514eef92009-06-08 14:57:57 +0200861 unsigned int status;
Wai Yew CHAY29959a02009-06-22 14:52:34 +0200862 int err = 0;
Takashi Iwai514eef92009-06-08 14:57:57 +0200863 unsigned char iec958_con_fs;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200864
865 switch (rate) {
866 case 48000:
867 iec958_con_fs = IEC958_AES3_CON_FS_48000;
868 break;
869 case 44100:
870 iec958_con_fs = IEC958_AES3_CON_FS_44100;
871 break;
872 case 32000:
873 iec958_con_fs = IEC958_AES3_CON_FS_32000;
874 break;
875 default:
876 return -ENOENT;
877 }
878
Takashi Iwai635c2652009-06-13 10:12:59 +0200879 mutex_lock(&atc->atc_mutex);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200880 dao->ops->get_spos(dao, &status);
881 if (((status >> 24) & IEC958_AES3_CON_FS) != iec958_con_fs) {
Przemyslaw Bruski4c1847e2011-03-13 16:18:56 +0100882 status &= ~(IEC958_AES3_CON_FS << 24);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200883 status |= (iec958_con_fs << 24);
884 dao->ops->set_spos(dao, status);
885 dao->ops->commit_write(dao);
886 }
Takashi Iwai514eef92009-06-08 14:57:57 +0200887 if ((rate != atc->pll_rate) && (32000 != rate))
888 err = atc_pll_init(atc, rate);
Takashi Iwai635c2652009-06-13 10:12:59 +0200889 mutex_unlock(&atc->atc_mutex);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200890
891 return err;
892}
893
894static int
895spdif_passthru_playback_prepare(struct ct_atc *atc, struct ct_atc_pcm *apcm)
896{
Takashi Iwai514eef92009-06-08 14:57:57 +0200897 struct src *src;
898 struct amixer *amixer;
899 struct dao *dao;
900 int err;
901 int i;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200902
Wai Yew CHAY29959a02009-06-22 14:52:34 +0200903 atc_pcm_release_resources(atc, apcm);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200904
905 /* Configure SPDIFOO and PLL to passthrough mode;
906 * determine pll_rate. */
907 err = spdif_passthru_playback_setup(atc, apcm);
908 if (err)
909 return err;
910
911 /* Get needed resources. */
912 err = spdif_passthru_playback_get_resources(atc, apcm);
913 if (err)
914 return err;
915
916 /* Connect resources */
917 src = apcm->src;
918 for (i = 0; i < apcm->n_amixer; i++) {
919 amixer = apcm->amixers[i];
920 amixer->ops->setup(amixer, &src->rsc, INIT_VOL, NULL);
921 src = src->ops->next_interleave(src);
Takashi Iwai35ebf6e2009-07-22 17:12:34 +0200922 if (!src)
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200923 src = apcm->src;
924 }
925 /* Connect to SPDIFOO */
Takashi Iwai635c2652009-06-13 10:12:59 +0200926 mutex_lock(&atc->atc_mutex);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200927 dao = container_of(atc->daios[SPDIFOO], struct dao, daio);
928 amixer = apcm->amixers[0];
929 dao->ops->set_left_input(dao, &amixer->rsc);
930 amixer = apcm->amixers[1];
931 dao->ops->set_right_input(dao, &amixer->rsc);
Takashi Iwai635c2652009-06-13 10:12:59 +0200932 mutex_unlock(&atc->atc_mutex);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200933
Takashi Iwaib7bbf872009-06-05 16:11:07 +0200934 ct_timer_prepare(apcm->timer);
935
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200936 return 0;
937}
938
939static int atc_select_line_in(struct ct_atc *atc)
940{
941 struct hw *hw = atc->hw;
942 struct ct_mixer *mixer = atc->mixer;
Takashi Iwai514eef92009-06-08 14:57:57 +0200943 struct src *src;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200944
945 if (hw->is_adc_source_selected(hw, ADC_LINEIN))
946 return 0;
947
948 mixer->set_input_left(mixer, MIX_MIC_IN, NULL);
949 mixer->set_input_right(mixer, MIX_MIC_IN, NULL);
950
951 hw->select_adc_source(hw, ADC_LINEIN);
952
953 src = atc->srcs[2];
954 mixer->set_input_left(mixer, MIX_LINE_IN, &src->rsc);
955 src = atc->srcs[3];
956 mixer->set_input_right(mixer, MIX_LINE_IN, &src->rsc);
957
958 return 0;
959}
960
961static int atc_select_mic_in(struct ct_atc *atc)
962{
963 struct hw *hw = atc->hw;
964 struct ct_mixer *mixer = atc->mixer;
Takashi Iwai514eef92009-06-08 14:57:57 +0200965 struct src *src;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200966
967 if (hw->is_adc_source_selected(hw, ADC_MICIN))
968 return 0;
969
970 mixer->set_input_left(mixer, MIX_LINE_IN, NULL);
971 mixer->set_input_right(mixer, MIX_LINE_IN, NULL);
972
973 hw->select_adc_source(hw, ADC_MICIN);
974
975 src = atc->srcs[2];
976 mixer->set_input_left(mixer, MIX_MIC_IN, &src->rsc);
977 src = atc->srcs[3];
978 mixer->set_input_right(mixer, MIX_MIC_IN, &src->rsc);
979
980 return 0;
981}
982
Harry Butterworthb028b812011-06-11 17:41:13 +0800983static struct capabilities atc_capabilities(struct ct_atc *atc)
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200984{
985 struct hw *hw = atc->hw;
986
Harry Butterworthb028b812011-06-11 17:41:13 +0800987 return hw->capabilities(hw);
Harry Butterworth55309212011-06-11 16:02:06 +0800988}
989
990static int atc_output_switch_get(struct ct_atc *atc)
991{
992 struct hw *hw = atc->hw;
993
994 return hw->output_switch_get(hw);
995}
996
997static int atc_output_switch_put(struct ct_atc *atc, int position)
998{
999 struct hw *hw = atc->hw;
1000
1001 return hw->output_switch_put(hw, position);
1002}
1003
Harry Butterworth55309212011-06-11 16:02:06 +08001004static int atc_mic_source_switch_get(struct ct_atc *atc)
1005{
1006 struct hw *hw = atc->hw;
1007
1008 return hw->mic_source_switch_get(hw);
1009}
1010
1011static int atc_mic_source_switch_put(struct ct_atc *atc, int position)
1012{
1013 struct hw *hw = atc->hw;
1014
1015 return hw->mic_source_switch_put(hw, position);
1016}
1017
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001018static int atc_select_digit_io(struct ct_atc *atc)
1019{
1020 struct hw *hw = atc->hw;
1021
1022 if (hw->is_adc_source_selected(hw, ADC_NONE))
1023 return 0;
1024
1025 hw->select_adc_source(hw, ADC_NONE);
1026
1027 return 0;
1028}
1029
1030static int atc_daio_unmute(struct ct_atc *atc, unsigned char state, int type)
1031{
1032 struct daio_mgr *daio_mgr = atc->rsc_mgrs[DAIO];
1033
1034 if (state)
1035 daio_mgr->daio_enable(daio_mgr, atc->daios[type]);
1036 else
1037 daio_mgr->daio_disable(daio_mgr, atc->daios[type]);
1038
1039 daio_mgr->commit_write(daio_mgr);
1040
1041 return 0;
1042}
1043
1044static int
1045atc_dao_get_status(struct ct_atc *atc, unsigned int *status, int type)
1046{
1047 struct dao *dao = container_of(atc->daios[type], struct dao, daio);
1048 return dao->ops->get_spos(dao, status);
1049}
1050
1051static int
1052atc_dao_set_status(struct ct_atc *atc, unsigned int status, int type)
1053{
1054 struct dao *dao = container_of(atc->daios[type], struct dao, daio);
1055
1056 dao->ops->set_spos(dao, status);
1057 dao->ops->commit_write(dao);
1058 return 0;
1059}
1060
1061static int atc_line_front_unmute(struct ct_atc *atc, unsigned char state)
1062{
1063 return atc_daio_unmute(atc, state, LINEO1);
1064}
1065
1066static int atc_line_surround_unmute(struct ct_atc *atc, unsigned char state)
1067{
Sven Eckelmann3b046912009-10-01 20:06:39 +02001068 return atc_daio_unmute(atc, state, LINEO2);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001069}
1070
1071static int atc_line_clfe_unmute(struct ct_atc *atc, unsigned char state)
1072{
1073 return atc_daio_unmute(atc, state, LINEO3);
1074}
1075
1076static int atc_line_rear_unmute(struct ct_atc *atc, unsigned char state)
1077{
Sven Eckelmann3b046912009-10-01 20:06:39 +02001078 return atc_daio_unmute(atc, state, LINEO4);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001079}
1080
1081static int atc_line_in_unmute(struct ct_atc *atc, unsigned char state)
1082{
1083 return atc_daio_unmute(atc, state, LINEIM);
1084}
1085
Harry Butterworth55309212011-06-11 16:02:06 +08001086static int atc_mic_unmute(struct ct_atc *atc, unsigned char state)
1087{
1088 return atc_daio_unmute(atc, state, MIC);
1089}
1090
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001091static int atc_spdif_out_unmute(struct ct_atc *atc, unsigned char state)
1092{
1093 return atc_daio_unmute(atc, state, SPDIFOO);
1094}
1095
1096static int atc_spdif_in_unmute(struct ct_atc *atc, unsigned char state)
1097{
1098 return atc_daio_unmute(atc, state, SPDIFIO);
1099}
1100
1101static int atc_spdif_out_get_status(struct ct_atc *atc, unsigned int *status)
1102{
1103 return atc_dao_get_status(atc, status, SPDIFOO);
1104}
1105
1106static int atc_spdif_out_set_status(struct ct_atc *atc, unsigned int status)
1107{
1108 return atc_dao_set_status(atc, status, SPDIFOO);
1109}
1110
1111static int atc_spdif_out_passthru(struct ct_atc *atc, unsigned char state)
1112{
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001113 struct dao_desc da_dsc = {0};
Takashi Iwai514eef92009-06-08 14:57:57 +02001114 struct dao *dao;
1115 int err;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001116 struct ct_mixer *mixer = atc->mixer;
1117 struct rsc *rscs[2] = {NULL};
1118 unsigned int spos = 0;
1119
Takashi Iwai635c2652009-06-13 10:12:59 +02001120 mutex_lock(&atc->atc_mutex);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001121 dao = container_of(atc->daios[SPDIFOO], struct dao, daio);
1122 da_dsc.msr = state ? 1 : atc->msr;
1123 da_dsc.passthru = state ? 1 : 0;
1124 err = dao->ops->reinit(dao, &da_dsc);
1125 if (state) {
1126 spos = IEC958_DEFAULT_CON;
1127 } else {
1128 mixer->get_output_ports(mixer, MIX_SPDIF_OUT,
1129 &rscs[0], &rscs[1]);
1130 dao->ops->set_left_input(dao, rscs[0]);
1131 dao->ops->set_right_input(dao, rscs[1]);
1132 /* Restore PLL to atc->rsr if needed. */
Takashi Iwai514eef92009-06-08 14:57:57 +02001133 if (atc->pll_rate != atc->rsr)
1134 err = atc_pll_init(atc, atc->rsr);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001135 }
1136 dao->ops->set_spos(dao, spos);
1137 dao->ops->commit_write(dao);
Takashi Iwai635c2652009-06-13 10:12:59 +02001138 mutex_unlock(&atc->atc_mutex);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001139
1140 return err;
1141}
1142
Wai Yew CHAY29959a02009-06-22 14:52:34 +02001143static int atc_release_resources(struct ct_atc *atc)
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001144{
Wai Yew CHAY29959a02009-06-22 14:52:34 +02001145 int i;
1146 struct daio_mgr *daio_mgr = NULL;
1147 struct dao *dao = NULL;
1148 struct dai *dai = NULL;
1149 struct daio *daio = NULL;
1150 struct sum_mgr *sum_mgr = NULL;
1151 struct src_mgr *src_mgr = NULL;
1152 struct srcimp_mgr *srcimp_mgr = NULL;
1153 struct srcimp *srcimp = NULL;
1154 struct ct_mixer *mixer = NULL;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001155
Wai Yew CHAY29959a02009-06-22 14:52:34 +02001156 /* disconnect internal mixer objects */
Takashi Iwai35ebf6e2009-07-22 17:12:34 +02001157 if (atc->mixer) {
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001158 mixer = atc->mixer;
1159 mixer->set_input_left(mixer, MIX_LINE_IN, NULL);
1160 mixer->set_input_right(mixer, MIX_LINE_IN, NULL);
1161 mixer->set_input_left(mixer, MIX_MIC_IN, NULL);
1162 mixer->set_input_right(mixer, MIX_MIC_IN, NULL);
1163 mixer->set_input_left(mixer, MIX_SPDIF_IN, NULL);
1164 mixer->set_input_right(mixer, MIX_SPDIF_IN, NULL);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001165 }
1166
Takashi Iwai35ebf6e2009-07-22 17:12:34 +02001167 if (atc->daios) {
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001168 daio_mgr = (struct daio_mgr *)atc->rsc_mgrs[DAIO];
1169 for (i = 0; i < atc->n_daio; i++) {
1170 daio = atc->daios[i];
1171 if (daio->type < LINEIM) {
1172 dao = container_of(daio, struct dao, daio);
1173 dao->ops->clear_left_input(dao);
1174 dao->ops->clear_right_input(dao);
1175 } else {
1176 dai = container_of(daio, struct dai, daio);
1177 /* some thing to do for dai ... */
1178 }
1179 daio_mgr->put_daio(daio_mgr, daio);
1180 }
1181 kfree(atc->daios);
Wai Yew CHAY29959a02009-06-22 14:52:34 +02001182 atc->daios = NULL;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001183 }
1184
Takashi Iwai35ebf6e2009-07-22 17:12:34 +02001185 if (atc->pcm) {
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001186 sum_mgr = atc->rsc_mgrs[SUM];
1187 for (i = 0; i < atc->n_pcm; i++)
1188 sum_mgr->put_sum(sum_mgr, atc->pcm[i]);
1189
1190 kfree(atc->pcm);
Wai Yew CHAY29959a02009-06-22 14:52:34 +02001191 atc->pcm = NULL;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001192 }
1193
Takashi Iwai35ebf6e2009-07-22 17:12:34 +02001194 if (atc->srcs) {
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001195 src_mgr = atc->rsc_mgrs[SRC];
1196 for (i = 0; i < atc->n_src; i++)
1197 src_mgr->put_src(src_mgr, atc->srcs[i]);
1198
1199 kfree(atc->srcs);
Wai Yew CHAY29959a02009-06-22 14:52:34 +02001200 atc->srcs = NULL;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001201 }
1202
Takashi Iwai35ebf6e2009-07-22 17:12:34 +02001203 if (atc->srcimps) {
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001204 srcimp_mgr = atc->rsc_mgrs[SRCIMP];
1205 for (i = 0; i < atc->n_srcimp; i++) {
1206 srcimp = atc->srcimps[i];
1207 srcimp->ops->unmap(srcimp);
1208 srcimp_mgr->put_srcimp(srcimp_mgr, atc->srcimps[i]);
1209 }
1210 kfree(atc->srcimps);
Wai Yew CHAY29959a02009-06-22 14:52:34 +02001211 atc->srcimps = NULL;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001212 }
1213
Wai Yew CHAY29959a02009-06-22 14:52:34 +02001214 return 0;
1215}
1216
1217static int ct_atc_destroy(struct ct_atc *atc)
1218{
1219 int i = 0;
1220
Takashi Iwai35ebf6e2009-07-22 17:12:34 +02001221 if (!atc)
Wai Yew CHAY29959a02009-06-22 14:52:34 +02001222 return 0;
1223
1224 if (atc->timer) {
1225 ct_timer_free(atc->timer);
1226 atc->timer = NULL;
1227 }
1228
1229 atc_release_resources(atc);
1230
1231 /* Destroy internal mixer objects */
Takashi Iwai35ebf6e2009-07-22 17:12:34 +02001232 if (atc->mixer)
Wai Yew CHAY29959a02009-06-22 14:52:34 +02001233 ct_mixer_destroy(atc->mixer);
1234
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001235 for (i = 0; i < NUM_RSCTYP; i++) {
Takashi Iwai35ebf6e2009-07-22 17:12:34 +02001236 if (rsc_mgr_funcs[i].destroy && atc->rsc_mgrs[i])
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001237 rsc_mgr_funcs[i].destroy(atc->rsc_mgrs[i]);
1238
1239 }
1240
Takashi Iwai35ebf6e2009-07-22 17:12:34 +02001241 if (atc->hw)
Sudip Mukherjeeb6bfe862014-09-29 14:33:22 +05301242 destroy_hw_obj(atc->hw);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001243
1244 /* Destroy device virtual memory manager object */
Takashi Iwai35ebf6e2009-07-22 17:12:34 +02001245 if (atc->vm) {
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001246 ct_vm_destroy(atc->vm);
1247 atc->vm = NULL;
1248 }
1249
1250 kfree(atc);
1251
1252 return 0;
1253}
1254
1255static int atc_dev_free(struct snd_device *dev)
1256{
1257 struct ct_atc *atc = dev->device_data;
1258 return ct_atc_destroy(atc);
1259}
1260
Bill Pembertone23e7a12012-12-06 12:35:10 -05001261static int atc_identify_card(struct ct_atc *atc, unsigned int ssid)
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001262{
Takashi Iwai94701952009-06-08 18:10:32 +02001263 const struct snd_pci_quirk *p;
1264 const struct snd_pci_quirk *list;
Takashi Iwai408bffd2010-01-14 09:19:46 +01001265 u16 vendor_id, device_id;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001266
Takashi Iwai94701952009-06-08 18:10:32 +02001267 switch (atc->chip_type) {
1268 case ATC20K1:
1269 atc->chip_name = "20K1";
1270 list = subsys_20k1_list;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001271 break;
Takashi Iwai94701952009-06-08 18:10:32 +02001272 case ATC20K2:
1273 atc->chip_name = "20K2";
1274 list = subsys_20k2_list;
1275 break;
1276 default:
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001277 return -ENOENT;
Takashi Iwai94701952009-06-08 18:10:32 +02001278 }
Takashi Iwai408bffd2010-01-14 09:19:46 +01001279 if (ssid) {
1280 vendor_id = ssid >> 16;
1281 device_id = ssid & 0xffff;
1282 } else {
1283 vendor_id = atc->pci->subsystem_vendor;
1284 device_id = atc->pci->subsystem_device;
1285 }
1286 p = snd_pci_quirk_lookup_id(vendor_id, device_id, list);
Takashi Iwaia8f43102009-06-22 07:36:52 +02001287 if (p) {
1288 if (p->value < 0) {
Sudip Mukherjee0cae90a2014-09-29 14:33:26 +05301289 dev_err(atc->card->dev,
1290 "Device %04x:%04x is black-listed\n",
1291 vendor_id, device_id);
Takashi Iwaia8f43102009-06-22 07:36:52 +02001292 return -ENOENT;
1293 }
1294 atc->model = p->value;
1295 } else {
1296 if (atc->chip_type == ATC20K1)
1297 atc->model = CT20K1_UNKNOWN;
1298 else
1299 atc->model = CT20K2_UNKNOWN;
1300 }
Takashi Iwai94701952009-06-08 18:10:32 +02001301 atc->model_name = ct_subsys_name[atc->model];
1302 snd_printd("ctxfi: chip %s model %s (%04x:%04x) is found\n",
1303 atc->chip_name, atc->model_name,
Takashi Iwai408bffd2010-01-14 09:19:46 +01001304 vendor_id, device_id);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001305 return 0;
1306}
1307
Bill Pembertone23e7a12012-12-06 12:35:10 -05001308int ct_atc_create_alsa_devs(struct ct_atc *atc)
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001309{
1310 enum CTALSADEVS i;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001311 int err;
1312
Takashi Iwai94701952009-06-08 18:10:32 +02001313 alsa_dev_funcs[MIXER].public_name = atc->chip_name;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001314
1315 for (i = 0; i < NUM_CTALSADEVS; i++) {
Takashi Iwai35ebf6e2009-07-22 17:12:34 +02001316 if (!alsa_dev_funcs[i].create)
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001317 continue;
1318
1319 err = alsa_dev_funcs[i].create(atc, i,
1320 alsa_dev_funcs[i].public_name);
1321 if (err) {
Sudip Mukherjee0cae90a2014-09-29 14:33:26 +05301322 dev_err(atc->card->dev,
1323 "Creating alsa device %d failed!\n", i);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001324 return err;
1325 }
1326 }
1327
1328 return 0;
1329}
1330
Bill Pembertone23e7a12012-12-06 12:35:10 -05001331static int atc_create_hw_devs(struct ct_atc *atc)
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001332{
Takashi Iwai514eef92009-06-08 14:57:57 +02001333 struct hw *hw;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001334 struct card_conf info = {0};
Takashi Iwai514eef92009-06-08 14:57:57 +02001335 int i, err;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001336
Takashi Iwai94701952009-06-08 18:10:32 +02001337 err = create_hw_obj(atc->pci, atc->chip_type, atc->model, &hw);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001338 if (err) {
Sudip Mukherjee0cae90a2014-09-29 14:33:26 +05301339 dev_err(atc->card->dev, "Failed to create hw obj!!!\n");
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001340 return err;
1341 }
Sudip Mukherjeee5347f92014-09-29 14:33:24 +05301342 hw->card = atc->card;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001343 atc->hw = hw;
1344
1345 /* Initialize card hardware. */
1346 info.rsr = atc->rsr;
1347 info.msr = atc->msr;
1348 info.vm_pgt_phys = atc_get_ptp_phys(atc, 0);
1349 err = hw->card_init(hw, &info);
1350 if (err < 0)
1351 return err;
1352
1353 for (i = 0; i < NUM_RSCTYP; i++) {
Takashi Iwai35ebf6e2009-07-22 17:12:34 +02001354 if (!rsc_mgr_funcs[i].create)
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001355 continue;
1356
1357 err = rsc_mgr_funcs[i].create(atc->hw, &atc->rsc_mgrs[i]);
1358 if (err) {
Sudip Mukherjee0cae90a2014-09-29 14:33:26 +05301359 dev_err(atc->card->dev,
1360 "Failed to create rsc_mgr %d!!!\n", i);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001361 return err;
1362 }
1363 }
1364
1365 return 0;
1366}
1367
Wai Yew CHAY29959a02009-06-22 14:52:34 +02001368static int atc_get_resources(struct ct_atc *atc)
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001369{
1370 struct daio_desc da_desc = {0};
Takashi Iwai514eef92009-06-08 14:57:57 +02001371 struct daio_mgr *daio_mgr;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001372 struct src_desc src_dsc = {0};
Takashi Iwai514eef92009-06-08 14:57:57 +02001373 struct src_mgr *src_mgr;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001374 struct srcimp_desc srcimp_dsc = {0};
Takashi Iwai514eef92009-06-08 14:57:57 +02001375 struct srcimp_mgr *srcimp_mgr;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001376 struct sum_desc sum_dsc = {0};
Takashi Iwai514eef92009-06-08 14:57:57 +02001377 struct sum_mgr *sum_mgr;
Harry Butterworth55309212011-06-11 16:02:06 +08001378 int err, i, num_srcs, num_daios;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001379
Harry Butterworth55309212011-06-11 16:02:06 +08001380 num_daios = ((atc->model == CTSB1270) ? 8 : 7);
1381 num_srcs = ((atc->model == CTSB1270) ? 6 : 4);
1382
1383 atc->daios = kzalloc(sizeof(void *)*num_daios, GFP_KERNEL);
Takashi Iwai35ebf6e2009-07-22 17:12:34 +02001384 if (!atc->daios)
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001385 return -ENOMEM;
1386
Harry Butterworth55309212011-06-11 16:02:06 +08001387 atc->srcs = kzalloc(sizeof(void *)*num_srcs, GFP_KERNEL);
Takashi Iwai35ebf6e2009-07-22 17:12:34 +02001388 if (!atc->srcs)
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001389 return -ENOMEM;
1390
Harry Butterworth55309212011-06-11 16:02:06 +08001391 atc->srcimps = kzalloc(sizeof(void *)*num_srcs, GFP_KERNEL);
Takashi Iwai35ebf6e2009-07-22 17:12:34 +02001392 if (!atc->srcimps)
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001393 return -ENOMEM;
1394
1395 atc->pcm = kzalloc(sizeof(void *)*(2*4), GFP_KERNEL);
Takashi Iwai35ebf6e2009-07-22 17:12:34 +02001396 if (!atc->pcm)
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001397 return -ENOMEM;
1398
1399 daio_mgr = (struct daio_mgr *)atc->rsc_mgrs[DAIO];
1400 da_desc.msr = atc->msr;
Harry Butterworth55309212011-06-11 16:02:06 +08001401 for (i = 0, atc->n_daio = 0; i < num_daios; i++) {
1402 da_desc.type = (atc->model != CTSB073X) ? i :
1403 ((i == SPDIFIO) ? SPDIFI1 : i);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001404 err = daio_mgr->get_daio(daio_mgr, &da_desc,
1405 (struct daio **)&atc->daios[i]);
1406 if (err) {
Sudip Mukherjee0cae90a2014-09-29 14:33:26 +05301407 dev_err(atc->card->dev,
1408 "Failed to get DAIO resource %d!!!\n",
Sudip Mukherjee62afa852014-08-26 19:01:43 +05301409 i);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001410 return err;
1411 }
1412 atc->n_daio++;
1413 }
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001414
1415 src_mgr = atc->rsc_mgrs[SRC];
1416 src_dsc.multi = 1;
1417 src_dsc.msr = atc->msr;
1418 src_dsc.mode = ARCRW;
Harry Butterworth55309212011-06-11 16:02:06 +08001419 for (i = 0, atc->n_src = 0; i < num_srcs; i++) {
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001420 err = src_mgr->get_src(src_mgr, &src_dsc,
1421 (struct src **)&atc->srcs[i]);
1422 if (err)
1423 return err;
1424
1425 atc->n_src++;
1426 }
1427
1428 srcimp_mgr = atc->rsc_mgrs[SRCIMP];
Harry Butterworth55309212011-06-11 16:02:06 +08001429 srcimp_dsc.msr = 8;
1430 for (i = 0, atc->n_srcimp = 0; i < num_srcs; i++) {
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001431 err = srcimp_mgr->get_srcimp(srcimp_mgr, &srcimp_dsc,
1432 (struct srcimp **)&atc->srcimps[i]);
1433 if (err)
1434 return err;
1435
1436 atc->n_srcimp++;
1437 }
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001438
1439 sum_mgr = atc->rsc_mgrs[SUM];
1440 sum_dsc.msr = atc->msr;
1441 for (i = 0, atc->n_pcm = 0; i < (2*4); i++) {
1442 err = sum_mgr->get_sum(sum_mgr, &sum_dsc,
1443 (struct sum **)&atc->pcm[i]);
1444 if (err)
1445 return err;
1446
1447 atc->n_pcm++;
1448 }
1449
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001450 return 0;
1451}
1452
Wai Yew CHAY29959a02009-06-22 14:52:34 +02001453static void
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001454atc_connect_dai(struct src_mgr *src_mgr, struct dai *dai,
1455 struct src **srcs, struct srcimp **srcimps)
1456{
1457 struct rsc *rscs[2] = {NULL};
Takashi Iwai514eef92009-06-08 14:57:57 +02001458 struct src *src;
1459 struct srcimp *srcimp;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001460 int i = 0;
1461
1462 rscs[0] = &dai->daio.rscl;
1463 rscs[1] = &dai->daio.rscr;
1464 for (i = 0; i < 2; i++) {
1465 src = srcs[i];
1466 srcimp = srcimps[i];
1467 srcimp->ops->map(srcimp, src, rscs[i]);
1468 src_mgr->src_disable(src_mgr, src);
1469 }
1470
1471 src_mgr->commit_write(src_mgr); /* Actually disable SRCs */
1472
1473 src = srcs[0];
1474 src->ops->set_pm(src, 1);
1475 for (i = 0; i < 2; i++) {
1476 src = srcs[i];
1477 src->ops->set_state(src, SRC_STATE_RUN);
1478 src->ops->commit_write(src);
1479 src_mgr->src_enable_s(src_mgr, src);
1480 }
1481
1482 dai->ops->set_srt_srcl(dai, &(srcs[0]->rsc));
1483 dai->ops->set_srt_srcr(dai, &(srcs[1]->rsc));
1484
1485 dai->ops->set_enb_src(dai, 1);
1486 dai->ops->set_enb_srt(dai, 1);
1487 dai->ops->commit_write(dai);
1488
1489 src_mgr->commit_write(src_mgr); /* Synchronously enable SRCs */
1490}
1491
Wai Yew CHAY29959a02009-06-22 14:52:34 +02001492static void atc_connect_resources(struct ct_atc *atc)
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001493{
Takashi Iwai514eef92009-06-08 14:57:57 +02001494 struct dai *dai;
1495 struct dao *dao;
1496 struct src *src;
1497 struct sum *sum;
1498 struct ct_mixer *mixer;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001499 struct rsc *rscs[2] = {NULL};
Takashi Iwai514eef92009-06-08 14:57:57 +02001500 int i, j;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001501
1502 mixer = atc->mixer;
1503
1504 for (i = MIX_WAVE_FRONT, j = LINEO1; i <= MIX_SPDIF_OUT; i++, j++) {
1505 mixer->get_output_ports(mixer, i, &rscs[0], &rscs[1]);
1506 dao = container_of(atc->daios[j], struct dao, daio);
1507 dao->ops->set_left_input(dao, rscs[0]);
1508 dao->ops->set_right_input(dao, rscs[1]);
1509 }
1510
1511 dai = container_of(atc->daios[LINEIM], struct dai, daio);
1512 atc_connect_dai(atc->rsc_mgrs[SRC], dai,
1513 (struct src **)&atc->srcs[2],
1514 (struct srcimp **)&atc->srcimps[2]);
1515 src = atc->srcs[2];
1516 mixer->set_input_left(mixer, MIX_LINE_IN, &src->rsc);
1517 src = atc->srcs[3];
1518 mixer->set_input_right(mixer, MIX_LINE_IN, &src->rsc);
1519
Harry Butterworth55309212011-06-11 16:02:06 +08001520 if (atc->model == CTSB1270) {
1521 /* Titanium HD has a dedicated ADC for the Mic. */
1522 dai = container_of(atc->daios[MIC], struct dai, daio);
1523 atc_connect_dai(atc->rsc_mgrs[SRC], dai,
1524 (struct src **)&atc->srcs[4],
1525 (struct srcimp **)&atc->srcimps[4]);
1526 src = atc->srcs[4];
1527 mixer->set_input_left(mixer, MIX_MIC_IN, &src->rsc);
1528 src = atc->srcs[5];
1529 mixer->set_input_right(mixer, MIX_MIC_IN, &src->rsc);
1530 }
1531
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001532 dai = container_of(atc->daios[SPDIFIO], struct dai, daio);
1533 atc_connect_dai(atc->rsc_mgrs[SRC], dai,
1534 (struct src **)&atc->srcs[0],
1535 (struct srcimp **)&atc->srcimps[0]);
1536
1537 src = atc->srcs[0];
1538 mixer->set_input_left(mixer, MIX_SPDIF_IN, &src->rsc);
1539 src = atc->srcs[1];
1540 mixer->set_input_right(mixer, MIX_SPDIF_IN, &src->rsc);
1541
1542 for (i = MIX_PCMI_FRONT, j = 0; i <= MIX_PCMI_SURROUND; i++, j += 2) {
1543 sum = atc->pcm[j];
1544 mixer->set_input_left(mixer, i, &sum->rsc);
1545 sum = atc->pcm[j+1];
1546 mixer->set_input_right(mixer, i, &sum->rsc);
1547 }
1548}
1549
Takashi Iwaic7561cd2012-08-14 18:12:04 +02001550#ifdef CONFIG_PM_SLEEP
Takashi Iwai68cb2b52012-07-02 15:20:37 +02001551static int atc_suspend(struct ct_atc *atc)
Wai Yew CHAY29959a02009-06-22 14:52:34 +02001552{
1553 int i;
1554 struct hw *hw = atc->hw;
1555
1556 snd_power_change_state(atc->card, SNDRV_CTL_POWER_D3hot);
1557
1558 for (i = FRONT; i < NUM_PCMS; i++) {
1559 if (!atc->pcms[i])
1560 continue;
1561
1562 snd_pcm_suspend_all(atc->pcms[i]);
1563 }
1564
1565 atc_release_resources(atc);
1566
Takashi Iwai68cb2b52012-07-02 15:20:37 +02001567 hw->suspend(hw);
Wai Yew CHAY29959a02009-06-22 14:52:34 +02001568
1569 return 0;
1570}
1571
1572static int atc_hw_resume(struct ct_atc *atc)
1573{
1574 struct hw *hw = atc->hw;
1575 struct card_conf info = {0};
1576
1577 /* Re-initialize card hardware. */
1578 info.rsr = atc->rsr;
1579 info.msr = atc->msr;
1580 info.vm_pgt_phys = atc_get_ptp_phys(atc, 0);
1581 return hw->resume(hw, &info);
1582}
1583
1584static int atc_resources_resume(struct ct_atc *atc)
1585{
1586 struct ct_mixer *mixer;
1587 int err = 0;
1588
1589 /* Get resources */
1590 err = atc_get_resources(atc);
1591 if (err < 0) {
1592 atc_release_resources(atc);
1593 return err;
1594 }
1595
1596 /* Build topology */
1597 atc_connect_resources(atc);
1598
1599 mixer = atc->mixer;
1600 mixer->resume(mixer);
1601
1602 return 0;
1603}
1604
1605static int atc_resume(struct ct_atc *atc)
1606{
1607 int err = 0;
1608
1609 /* Do hardware resume. */
1610 err = atc_hw_resume(atc);
1611 if (err < 0) {
Sudip Mukherjee0cae90a2014-09-29 14:33:26 +05301612 dev_err(atc->card->dev,
1613 "pci_enable_device failed, disabling device\n");
Wai Yew CHAY29959a02009-06-22 14:52:34 +02001614 snd_card_disconnect(atc->card);
1615 return err;
1616 }
1617
1618 err = atc_resources_resume(atc);
1619 if (err < 0)
1620 return err;
1621
1622 snd_power_change_state(atc->card, SNDRV_CTL_POWER_D0);
1623
1624 return 0;
1625}
1626#endif
1627
Bill Pembertone23e7a12012-12-06 12:35:10 -05001628static struct ct_atc atc_preset = {
Takashi Iwai2a36f672009-06-05 16:34:10 +02001629 .map_audio_buffer = ct_map_audio_buffer,
1630 .unmap_audio_buffer = ct_unmap_audio_buffer,
1631 .pcm_playback_prepare = atc_pcm_playback_prepare,
1632 .pcm_release_resources = atc_pcm_release_resources,
1633 .pcm_playback_start = atc_pcm_playback_start,
1634 .pcm_playback_stop = atc_pcm_stop,
1635 .pcm_playback_position = atc_pcm_playback_position,
1636 .pcm_capture_prepare = atc_pcm_capture_prepare,
1637 .pcm_capture_start = atc_pcm_capture_start,
1638 .pcm_capture_stop = atc_pcm_stop,
1639 .pcm_capture_position = atc_pcm_capture_position,
1640 .spdif_passthru_playback_prepare = spdif_passthru_playback_prepare,
1641 .get_ptp_phys = atc_get_ptp_phys,
1642 .select_line_in = atc_select_line_in,
1643 .select_mic_in = atc_select_mic_in,
1644 .select_digit_io = atc_select_digit_io,
1645 .line_front_unmute = atc_line_front_unmute,
1646 .line_surround_unmute = atc_line_surround_unmute,
1647 .line_clfe_unmute = atc_line_clfe_unmute,
1648 .line_rear_unmute = atc_line_rear_unmute,
1649 .line_in_unmute = atc_line_in_unmute,
Harry Butterworth55309212011-06-11 16:02:06 +08001650 .mic_unmute = atc_mic_unmute,
Takashi Iwai2a36f672009-06-05 16:34:10 +02001651 .spdif_out_unmute = atc_spdif_out_unmute,
1652 .spdif_in_unmute = atc_spdif_in_unmute,
1653 .spdif_out_get_status = atc_spdif_out_get_status,
1654 .spdif_out_set_status = atc_spdif_out_set_status,
1655 .spdif_out_passthru = atc_spdif_out_passthru,
Harry Butterworthb028b812011-06-11 17:41:13 +08001656 .capabilities = atc_capabilities,
Harry Butterworth55309212011-06-11 16:02:06 +08001657 .output_switch_get = atc_output_switch_get,
1658 .output_switch_put = atc_output_switch_put,
Harry Butterworth55309212011-06-11 16:02:06 +08001659 .mic_source_switch_get = atc_mic_source_switch_get,
1660 .mic_source_switch_put = atc_mic_source_switch_put,
Takashi Iwaic7561cd2012-08-14 18:12:04 +02001661#ifdef CONFIG_PM_SLEEP
Wai Yew CHAY29959a02009-06-22 14:52:34 +02001662 .suspend = atc_suspend,
1663 .resume = atc_resume,
1664#endif
Takashi Iwai2a36f672009-06-05 16:34:10 +02001665};
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001666
1667/**
1668 * ct_atc_create - create and initialize a hardware manager
1669 * @card: corresponding alsa card object
1670 * @pci: corresponding kernel pci device object
1671 * @ratc: return created object address in it
1672 *
1673 * Creates and initializes a hardware manager.
1674 *
1675 * Creates kmallocated ct_atc structure. Initializes hardware.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001676 * Returns 0 if succeeds, or negative error code if fails.
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001677 */
1678
Bill Pembertone23e7a12012-12-06 12:35:10 -05001679int ct_atc_create(struct snd_card *card, struct pci_dev *pci,
1680 unsigned int rsr, unsigned int msr,
1681 int chip_type, unsigned int ssid,
1682 struct ct_atc **ratc)
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001683{
Takashi Iwai514eef92009-06-08 14:57:57 +02001684 struct ct_atc *atc;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001685 static struct snd_device_ops ops = {
1686 .dev_free = atc_dev_free,
1687 };
Takashi Iwai514eef92009-06-08 14:57:57 +02001688 int err;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001689
1690 *ratc = NULL;
1691
1692 atc = kzalloc(sizeof(*atc), GFP_KERNEL);
Takashi Iwai35ebf6e2009-07-22 17:12:34 +02001693 if (!atc)
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001694 return -ENOMEM;
1695
Takashi Iwai2a36f672009-06-05 16:34:10 +02001696 /* Set operations */
1697 *atc = atc_preset;
1698
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001699 atc->card = card;
1700 atc->pci = pci;
1701 atc->rsr = rsr;
1702 atc->msr = msr;
Takashi Iwai94701952009-06-08 18:10:32 +02001703 atc->chip_type = chip_type;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001704
Takashi Iwai635c2652009-06-13 10:12:59 +02001705 mutex_init(&atc->atc_mutex);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001706
1707 /* Find card model */
Takashi Iwai408bffd2010-01-14 09:19:46 +01001708 err = atc_identify_card(atc, ssid);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001709 if (err < 0) {
Sudip Mukherjee0cae90a2014-09-29 14:33:26 +05301710 dev_err(card->dev, "ctatc: Card not recognised\n");
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001711 goto error1;
1712 }
1713
1714 /* Set up device virtual memory management object */
Jaroslav Kysela21956b62010-02-02 19:58:25 +01001715 err = ct_vm_create(&atc->vm, pci);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001716 if (err < 0)
1717 goto error1;
1718
1719 /* Create all atc hw devices */
1720 err = atc_create_hw_devs(atc);
1721 if (err < 0)
1722 goto error1;
1723
Wai Yew CHAY29959a02009-06-22 14:52:34 +02001724 err = ct_mixer_create(atc, (struct ct_mixer **)&atc->mixer);
1725 if (err) {
Sudip Mukherjee0cae90a2014-09-29 14:33:26 +05301726 dev_err(card->dev, "Failed to create mixer obj!!!\n");
Wai Yew CHAY29959a02009-06-22 14:52:34 +02001727 goto error1;
1728 }
1729
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001730 /* Get resources */
1731 err = atc_get_resources(atc);
1732 if (err < 0)
1733 goto error1;
1734
1735 /* Build topology */
1736 atc_connect_resources(atc);
1737
Takashi Iwaib7bbf872009-06-05 16:11:07 +02001738 atc->timer = ct_timer_new(atc);
Julia Lawall4d8ce1c2012-08-19 09:02:56 +02001739 if (!atc->timer) {
1740 err = -ENOMEM;
Takashi Iwaib7bbf872009-06-05 16:11:07 +02001741 goto error1;
Julia Lawall4d8ce1c2012-08-19 09:02:56 +02001742 }
Takashi Iwaib7bbf872009-06-05 16:11:07 +02001743
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001744 err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, atc, &ops);
1745 if (err < 0)
1746 goto error1;
1747
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001748 *ratc = atc;
1749 return 0;
1750
1751error1:
1752 ct_atc_destroy(atc);
Sudip Mukherjee0cae90a2014-09-29 14:33:26 +05301753 dev_err(card->dev, "Something wrong!!!\n");
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001754 return err;
1755}