Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
c1017a4c | 2 | * Copyright (c) by Jaroslav Kysela <perex@perex.cz> |
1da177e4 LT |
3 | * Routines for control of YMF724/740/744/754 chips |
4 | * | |
1da177e4 LT |
5 | * This program is free software; you can redistribute it and/or modify |
6 | * it under the terms of the GNU General Public License as published by | |
7 | * the Free Software Foundation; either version 2 of the License, or | |
8 | * (at your option) any later version. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License | |
16 | * along with this program; if not, write to the Free Software | |
17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
18 | * | |
19 | */ | |
20 | ||
1da177e4 | 21 | #include <linux/delay.h> |
102fa906 | 22 | #include <linux/firmware.h> |
1da177e4 LT |
23 | #include <linux/init.h> |
24 | #include <linux/interrupt.h> | |
25 | #include <linux/pci.h> | |
26 | #include <linux/sched.h> | |
27 | #include <linux/slab.h> | |
28 | #include <linux/vmalloc.h> | |
b82a82d0 | 29 | #include <linux/mutex.h> |
1da177e4 LT |
30 | |
31 | #include <sound/core.h> | |
32 | #include <sound/control.h> | |
33 | #include <sound/info.h> | |
33925186 | 34 | #include <sound/tlv.h> |
1da177e4 LT |
35 | #include <sound/ymfpci.h> |
36 | #include <sound/asoundef.h> | |
37 | #include <sound/mpu401.h> | |
38 | ||
39 | #include <asm/io.h> | |
102fa906 | 40 | #include <asm/byteorder.h> |
1da177e4 LT |
41 | |
42 | /* | |
43 | * common I/O routines | |
44 | */ | |
45 | ||
208a1b4c | 46 | static void snd_ymfpci_irq_wait(struct snd_ymfpci *chip); |
1da177e4 | 47 | |
208a1b4c | 48 | static inline u8 snd_ymfpci_readb(struct snd_ymfpci *chip, u32 offset) |
1da177e4 LT |
49 | { |
50 | return readb(chip->reg_area_virt + offset); | |
51 | } | |
52 | ||
208a1b4c | 53 | static inline void snd_ymfpci_writeb(struct snd_ymfpci *chip, u32 offset, u8 val) |
1da177e4 LT |
54 | { |
55 | writeb(val, chip->reg_area_virt + offset); | |
56 | } | |
57 | ||
208a1b4c | 58 | static inline u16 snd_ymfpci_readw(struct snd_ymfpci *chip, u32 offset) |
1da177e4 LT |
59 | { |
60 | return readw(chip->reg_area_virt + offset); | |
61 | } | |
62 | ||
208a1b4c | 63 | static inline void snd_ymfpci_writew(struct snd_ymfpci *chip, u32 offset, u16 val) |
1da177e4 LT |
64 | { |
65 | writew(val, chip->reg_area_virt + offset); | |
66 | } | |
67 | ||
208a1b4c | 68 | static inline u32 snd_ymfpci_readl(struct snd_ymfpci *chip, u32 offset) |
1da177e4 LT |
69 | { |
70 | return readl(chip->reg_area_virt + offset); | |
71 | } | |
72 | ||
208a1b4c | 73 | static inline void snd_ymfpci_writel(struct snd_ymfpci *chip, u32 offset, u32 val) |
1da177e4 LT |
74 | { |
75 | writel(val, chip->reg_area_virt + offset); | |
76 | } | |
77 | ||
208a1b4c | 78 | static int snd_ymfpci_codec_ready(struct snd_ymfpci *chip, int secondary) |
1da177e4 | 79 | { |
ef21ca24 | 80 | unsigned long end_time; |
1da177e4 LT |
81 | u32 reg = secondary ? YDSXGR_SECSTATUSADR : YDSXGR_PRISTATUSADR; |
82 | ||
ef21ca24 | 83 | end_time = jiffies + msecs_to_jiffies(750); |
1da177e4 LT |
84 | do { |
85 | if ((snd_ymfpci_readw(chip, reg) & 0x8000) == 0) | |
86 | return 0; | |
8433a509 | 87 | schedule_timeout_uninterruptible(1); |
ef21ca24 | 88 | } while (time_before(jiffies, end_time)); |
99b359ba | 89 | snd_printk(KERN_ERR "codec_ready: codec %i is not ready [0x%x]\n", secondary, snd_ymfpci_readw(chip, reg)); |
1da177e4 LT |
90 | return -EBUSY; |
91 | } | |
92 | ||
208a1b4c | 93 | static void snd_ymfpci_codec_write(struct snd_ac97 *ac97, u16 reg, u16 val) |
1da177e4 | 94 | { |
208a1b4c | 95 | struct snd_ymfpci *chip = ac97->private_data; |
1da177e4 LT |
96 | u32 cmd; |
97 | ||
98 | snd_ymfpci_codec_ready(chip, 0); | |
99 | cmd = ((YDSXG_AC97WRITECMD | reg) << 16) | val; | |
100 | snd_ymfpci_writel(chip, YDSXGR_AC97CMDDATA, cmd); | |
101 | } | |
102 | ||
208a1b4c | 103 | static u16 snd_ymfpci_codec_read(struct snd_ac97 *ac97, u16 reg) |
1da177e4 | 104 | { |
208a1b4c | 105 | struct snd_ymfpci *chip = ac97->private_data; |
1da177e4 LT |
106 | |
107 | if (snd_ymfpci_codec_ready(chip, 0)) | |
108 | return ~0; | |
109 | snd_ymfpci_writew(chip, YDSXGR_AC97CMDADR, YDSXG_AC97READCMD | reg); | |
110 | if (snd_ymfpci_codec_ready(chip, 0)) | |
111 | return ~0; | |
112 | if (chip->device_id == PCI_DEVICE_ID_YAMAHA_744 && chip->rev < 2) { | |
113 | int i; | |
114 | for (i = 0; i < 600; i++) | |
115 | snd_ymfpci_readw(chip, YDSXGR_PRISTATUSDATA); | |
116 | } | |
117 | return snd_ymfpci_readw(chip, YDSXGR_PRISTATUSDATA); | |
118 | } | |
119 | ||
120 | /* | |
121 | * Misc routines | |
122 | */ | |
123 | ||
124 | static u32 snd_ymfpci_calc_delta(u32 rate) | |
125 | { | |
126 | switch (rate) { | |
127 | case 8000: return 0x02aaab00; | |
128 | case 11025: return 0x03accd00; | |
129 | case 16000: return 0x05555500; | |
130 | case 22050: return 0x07599a00; | |
131 | case 32000: return 0x0aaaab00; | |
132 | case 44100: return 0x0eb33300; | |
133 | default: return ((rate << 16) / 375) << 5; | |
134 | } | |
135 | } | |
136 | ||
137 | static u32 def_rate[8] = { | |
138 | 100, 2000, 8000, 11025, 16000, 22050, 32000, 48000 | |
139 | }; | |
140 | ||
141 | static u32 snd_ymfpci_calc_lpfK(u32 rate) | |
142 | { | |
143 | u32 i; | |
144 | static u32 val[8] = { | |
145 | 0x00570000, 0x06AA0000, 0x18B20000, 0x20930000, | |
146 | 0x2B9A0000, 0x35A10000, 0x3EAA0000, 0x40000000 | |
147 | }; | |
148 | ||
149 | if (rate == 44100) | |
150 | return 0x40000000; /* FIXME: What's the right value? */ | |
151 | for (i = 0; i < 8; i++) | |
152 | if (rate <= def_rate[i]) | |
153 | return val[i]; | |
154 | return val[0]; | |
155 | } | |
156 | ||
157 | static u32 snd_ymfpci_calc_lpfQ(u32 rate) | |
158 | { | |
159 | u32 i; | |
160 | static u32 val[8] = { | |
161 | 0x35280000, 0x34A70000, 0x32020000, 0x31770000, | |
162 | 0x31390000, 0x31C90000, 0x33D00000, 0x40000000 | |
163 | }; | |
164 | ||
165 | if (rate == 44100) | |
166 | return 0x370A0000; | |
167 | for (i = 0; i < 8; i++) | |
168 | if (rate <= def_rate[i]) | |
169 | return val[i]; | |
170 | return val[0]; | |
171 | } | |
172 | ||
173 | /* | |
174 | * Hardware start management | |
175 | */ | |
176 | ||
208a1b4c | 177 | static void snd_ymfpci_hw_start(struct snd_ymfpci *chip) |
1da177e4 LT |
178 | { |
179 | unsigned long flags; | |
180 | ||
181 | spin_lock_irqsave(&chip->reg_lock, flags); | |
182 | if (chip->start_count++ > 0) | |
183 | goto __end; | |
184 | snd_ymfpci_writel(chip, YDSXGR_MODE, | |
185 | snd_ymfpci_readl(chip, YDSXGR_MODE) | 3); | |
186 | chip->active_bank = snd_ymfpci_readl(chip, YDSXGR_CTRLSELECT) & 1; | |
187 | __end: | |
188 | spin_unlock_irqrestore(&chip->reg_lock, flags); | |
189 | } | |
190 | ||
208a1b4c | 191 | static void snd_ymfpci_hw_stop(struct snd_ymfpci *chip) |
1da177e4 LT |
192 | { |
193 | unsigned long flags; | |
194 | long timeout = 1000; | |
195 | ||
196 | spin_lock_irqsave(&chip->reg_lock, flags); | |
197 | if (--chip->start_count > 0) | |
198 | goto __end; | |
199 | snd_ymfpci_writel(chip, YDSXGR_MODE, | |
200 | snd_ymfpci_readl(chip, YDSXGR_MODE) & ~3); | |
201 | while (timeout-- > 0) { | |
202 | if ((snd_ymfpci_readl(chip, YDSXGR_STATUS) & 2) == 0) | |
203 | break; | |
204 | } | |
205 | if (atomic_read(&chip->interrupt_sleep_count)) { | |
206 | atomic_set(&chip->interrupt_sleep_count, 0); | |
207 | wake_up(&chip->interrupt_sleep); | |
208 | } | |
209 | __end: | |
210 | spin_unlock_irqrestore(&chip->reg_lock, flags); | |
211 | } | |
212 | ||
213 | /* | |
214 | * Playback voice management | |
215 | */ | |
216 | ||
208a1b4c TI |
217 | static int voice_alloc(struct snd_ymfpci *chip, |
218 | enum snd_ymfpci_voice_type type, int pair, | |
219 | struct snd_ymfpci_voice **rvoice) | |
1da177e4 | 220 | { |
208a1b4c | 221 | struct snd_ymfpci_voice *voice, *voice2; |
1da177e4 LT |
222 | int idx; |
223 | ||
224 | *rvoice = NULL; | |
225 | for (idx = 0; idx < YDSXG_PLAYBACK_VOICES; idx += pair ? 2 : 1) { | |
226 | voice = &chip->voices[idx]; | |
227 | voice2 = pair ? &chip->voices[idx+1] : NULL; | |
228 | if (voice->use || (voice2 && voice2->use)) | |
229 | continue; | |
230 | voice->use = 1; | |
231 | if (voice2) | |
232 | voice2->use = 1; | |
233 | switch (type) { | |
234 | case YMFPCI_PCM: | |
235 | voice->pcm = 1; | |
236 | if (voice2) | |
237 | voice2->pcm = 1; | |
238 | break; | |
239 | case YMFPCI_SYNTH: | |
240 | voice->synth = 1; | |
241 | break; | |
242 | case YMFPCI_MIDI: | |
243 | voice->midi = 1; | |
244 | break; | |
245 | } | |
246 | snd_ymfpci_hw_start(chip); | |
247 | if (voice2) | |
248 | snd_ymfpci_hw_start(chip); | |
249 | *rvoice = voice; | |
250 | return 0; | |
251 | } | |
252 | return -ENOMEM; | |
253 | } | |
254 | ||
208a1b4c TI |
255 | static int snd_ymfpci_voice_alloc(struct snd_ymfpci *chip, |
256 | enum snd_ymfpci_voice_type type, int pair, | |
257 | struct snd_ymfpci_voice **rvoice) | |
1da177e4 LT |
258 | { |
259 | unsigned long flags; | |
260 | int result; | |
261 | ||
da3cec35 TI |
262 | if (snd_BUG_ON(!rvoice)) |
263 | return -EINVAL; | |
264 | if (snd_BUG_ON(pair && type != YMFPCI_PCM)) | |
265 | return -EINVAL; | |
1da177e4 LT |
266 | |
267 | spin_lock_irqsave(&chip->voice_lock, flags); | |
268 | for (;;) { | |
269 | result = voice_alloc(chip, type, pair, rvoice); | |
270 | if (result == 0 || type != YMFPCI_PCM) | |
271 | break; | |
272 | /* TODO: synth/midi voice deallocation */ | |
273 | break; | |
274 | } | |
275 | spin_unlock_irqrestore(&chip->voice_lock, flags); | |
276 | return result; | |
277 | } | |
278 | ||
208a1b4c | 279 | static int snd_ymfpci_voice_free(struct snd_ymfpci *chip, struct snd_ymfpci_voice *pvoice) |
1da177e4 LT |
280 | { |
281 | unsigned long flags; | |
282 | ||
da3cec35 TI |
283 | if (snd_BUG_ON(!pvoice)) |
284 | return -EINVAL; | |
1da177e4 LT |
285 | snd_ymfpci_hw_stop(chip); |
286 | spin_lock_irqsave(&chip->voice_lock, flags); | |
9ed1261e TK |
287 | if (pvoice->number == chip->src441_used) { |
288 | chip->src441_used = -1; | |
289 | pvoice->ypcm->use_441_slot = 0; | |
290 | } | |
1da177e4 LT |
291 | pvoice->use = pvoice->pcm = pvoice->synth = pvoice->midi = 0; |
292 | pvoice->ypcm = NULL; | |
293 | pvoice->interrupt = NULL; | |
294 | spin_unlock_irqrestore(&chip->voice_lock, flags); | |
295 | return 0; | |
296 | } | |
297 | ||
298 | /* | |
299 | * PCM part | |
300 | */ | |
301 | ||
208a1b4c | 302 | static void snd_ymfpci_pcm_interrupt(struct snd_ymfpci *chip, struct snd_ymfpci_voice *voice) |
1da177e4 | 303 | { |
208a1b4c | 304 | struct snd_ymfpci_pcm *ypcm; |
1da177e4 LT |
305 | u32 pos, delta; |
306 | ||
307 | if ((ypcm = voice->ypcm) == NULL) | |
308 | return; | |
309 | if (ypcm->substream == NULL) | |
310 | return; | |
311 | spin_lock(&chip->reg_lock); | |
312 | if (ypcm->running) { | |
313 | pos = le32_to_cpu(voice->bank[chip->active_bank].start); | |
314 | if (pos < ypcm->last_pos) | |
315 | delta = pos + (ypcm->buffer_size - ypcm->last_pos); | |
316 | else | |
317 | delta = pos - ypcm->last_pos; | |
318 | ypcm->period_pos += delta; | |
319 | ypcm->last_pos = pos; | |
320 | if (ypcm->period_pos >= ypcm->period_size) { | |
ee419653 TI |
321 | /* |
322 | printk(KERN_DEBUG | |
323 | "done - active_bank = 0x%x, start = 0x%x\n", | |
324 | chip->active_bank, | |
325 | voice->bank[chip->active_bank].start); | |
326 | */ | |
1da177e4 LT |
327 | ypcm->period_pos %= ypcm->period_size; |
328 | spin_unlock(&chip->reg_lock); | |
329 | snd_pcm_period_elapsed(ypcm->substream); | |
330 | spin_lock(&chip->reg_lock); | |
331 | } | |
9bcf6551 CL |
332 | |
333 | if (unlikely(ypcm->update_pcm_vol)) { | |
334 | unsigned int subs = ypcm->substream->number; | |
335 | unsigned int next_bank = 1 - chip->active_bank; | |
208a1b4c | 336 | struct snd_ymfpci_playback_bank *bank; |
9bcf6551 CL |
337 | u32 volume; |
338 | ||
339 | bank = &voice->bank[next_bank]; | |
340 | volume = cpu_to_le32(chip->pcm_mixer[subs].left << 15); | |
341 | bank->left_gain_end = volume; | |
342 | if (ypcm->output_rear) | |
343 | bank->eff2_gain_end = volume; | |
344 | if (ypcm->voices[1]) | |
345 | bank = &ypcm->voices[1]->bank[next_bank]; | |
346 | volume = cpu_to_le32(chip->pcm_mixer[subs].right << 15); | |
347 | bank->right_gain_end = volume; | |
348 | if (ypcm->output_rear) | |
349 | bank->eff3_gain_end = volume; | |
350 | ypcm->update_pcm_vol--; | |
351 | } | |
1da177e4 LT |
352 | } |
353 | spin_unlock(&chip->reg_lock); | |
354 | } | |
355 | ||
208a1b4c | 356 | static void snd_ymfpci_pcm_capture_interrupt(struct snd_pcm_substream *substream) |
1da177e4 | 357 | { |
208a1b4c TI |
358 | struct snd_pcm_runtime *runtime = substream->runtime; |
359 | struct snd_ymfpci_pcm *ypcm = runtime->private_data; | |
360 | struct snd_ymfpci *chip = ypcm->chip; | |
1da177e4 LT |
361 | u32 pos, delta; |
362 | ||
363 | spin_lock(&chip->reg_lock); | |
364 | if (ypcm->running) { | |
365 | pos = le32_to_cpu(chip->bank_capture[ypcm->capture_bank_number][chip->active_bank]->start) >> ypcm->shift; | |
366 | if (pos < ypcm->last_pos) | |
367 | delta = pos + (ypcm->buffer_size - ypcm->last_pos); | |
368 | else | |
369 | delta = pos - ypcm->last_pos; | |
370 | ypcm->period_pos += delta; | |
371 | ypcm->last_pos = pos; | |
372 | if (ypcm->period_pos >= ypcm->period_size) { | |
373 | ypcm->period_pos %= ypcm->period_size; | |
ee419653 TI |
374 | /* |
375 | printk(KERN_DEBUG | |
376 | "done - active_bank = 0x%x, start = 0x%x\n", | |
377 | chip->active_bank, | |
378 | voice->bank[chip->active_bank].start); | |
379 | */ | |
1da177e4 LT |
380 | spin_unlock(&chip->reg_lock); |
381 | snd_pcm_period_elapsed(substream); | |
382 | spin_lock(&chip->reg_lock); | |
383 | } | |
384 | } | |
385 | spin_unlock(&chip->reg_lock); | |
386 | } | |
387 | ||
208a1b4c | 388 | static int snd_ymfpci_playback_trigger(struct snd_pcm_substream *substream, |
1da177e4 LT |
389 | int cmd) |
390 | { | |
208a1b4c TI |
391 | struct snd_ymfpci *chip = snd_pcm_substream_chip(substream); |
392 | struct snd_ymfpci_pcm *ypcm = substream->runtime->private_data; | |
177a7cdb | 393 | struct snd_kcontrol *kctl = NULL; |
1da177e4 LT |
394 | int result = 0; |
395 | ||
396 | spin_lock(&chip->reg_lock); | |
397 | if (ypcm->voices[0] == NULL) { | |
398 | result = -EINVAL; | |
399 | goto __unlock; | |
400 | } | |
401 | switch (cmd) { | |
402 | case SNDRV_PCM_TRIGGER_START: | |
403 | case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: | |
404 | case SNDRV_PCM_TRIGGER_RESUME: | |
405 | chip->ctrl_playback[ypcm->voices[0]->number + 1] = cpu_to_le32(ypcm->voices[0]->bank_addr); | |
9ed1261e | 406 | if (ypcm->voices[1] != NULL && !ypcm->use_441_slot) |
1da177e4 LT |
407 | chip->ctrl_playback[ypcm->voices[1]->number + 1] = cpu_to_le32(ypcm->voices[1]->bank_addr); |
408 | ypcm->running = 1; | |
409 | break; | |
410 | case SNDRV_PCM_TRIGGER_STOP: | |
177a7cdb CL |
411 | if (substream->pcm == chip->pcm && !ypcm->use_441_slot) { |
412 | kctl = chip->pcm_mixer[substream->number].ctl; | |
413 | kctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE; | |
414 | } | |
415 | /* fall through */ | |
1da177e4 LT |
416 | case SNDRV_PCM_TRIGGER_PAUSE_PUSH: |
417 | case SNDRV_PCM_TRIGGER_SUSPEND: | |
418 | chip->ctrl_playback[ypcm->voices[0]->number + 1] = 0; | |
9ed1261e | 419 | if (ypcm->voices[1] != NULL && !ypcm->use_441_slot) |
1da177e4 LT |
420 | chip->ctrl_playback[ypcm->voices[1]->number + 1] = 0; |
421 | ypcm->running = 0; | |
422 | break; | |
423 | default: | |
424 | result = -EINVAL; | |
425 | break; | |
426 | } | |
427 | __unlock: | |
428 | spin_unlock(&chip->reg_lock); | |
177a7cdb CL |
429 | if (kctl) |
430 | snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_INFO, &kctl->id); | |
1da177e4 LT |
431 | return result; |
432 | } | |
208a1b4c | 433 | static int snd_ymfpci_capture_trigger(struct snd_pcm_substream *substream, |
1da177e4 LT |
434 | int cmd) |
435 | { | |
208a1b4c TI |
436 | struct snd_ymfpci *chip = snd_pcm_substream_chip(substream); |
437 | struct snd_ymfpci_pcm *ypcm = substream->runtime->private_data; | |
1da177e4 LT |
438 | int result = 0; |
439 | u32 tmp; | |
440 | ||
441 | spin_lock(&chip->reg_lock); | |
442 | switch (cmd) { | |
443 | case SNDRV_PCM_TRIGGER_START: | |
444 | case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: | |
445 | case SNDRV_PCM_TRIGGER_RESUME: | |
446 | tmp = snd_ymfpci_readl(chip, YDSXGR_MAPOFREC) | (1 << ypcm->capture_bank_number); | |
447 | snd_ymfpci_writel(chip, YDSXGR_MAPOFREC, tmp); | |
448 | ypcm->running = 1; | |
449 | break; | |
450 | case SNDRV_PCM_TRIGGER_STOP: | |
451 | case SNDRV_PCM_TRIGGER_PAUSE_PUSH: | |
452 | case SNDRV_PCM_TRIGGER_SUSPEND: | |
453 | tmp = snd_ymfpci_readl(chip, YDSXGR_MAPOFREC) & ~(1 << ypcm->capture_bank_number); | |
454 | snd_ymfpci_writel(chip, YDSXGR_MAPOFREC, tmp); | |
455 | ypcm->running = 0; | |
456 | break; | |
457 | default: | |
458 | result = -EINVAL; | |
459 | break; | |
460 | } | |
461 | spin_unlock(&chip->reg_lock); | |
462 | return result; | |
463 | } | |
464 | ||
208a1b4c | 465 | static int snd_ymfpci_pcm_voice_alloc(struct snd_ymfpci_pcm *ypcm, int voices) |
1da177e4 LT |
466 | { |
467 | int err; | |
468 | ||
469 | if (ypcm->voices[1] != NULL && voices < 2) { | |
470 | snd_ymfpci_voice_free(ypcm->chip, ypcm->voices[1]); | |
471 | ypcm->voices[1] = NULL; | |
472 | } | |
473 | if (voices == 1 && ypcm->voices[0] != NULL) | |
474 | return 0; /* already allocated */ | |
475 | if (voices == 2 && ypcm->voices[0] != NULL && ypcm->voices[1] != NULL) | |
476 | return 0; /* already allocated */ | |
477 | if (voices > 1) { | |
478 | if (ypcm->voices[0] != NULL && ypcm->voices[1] == NULL) { | |
479 | snd_ymfpci_voice_free(ypcm->chip, ypcm->voices[0]); | |
480 | ypcm->voices[0] = NULL; | |
481 | } | |
482 | } | |
483 | err = snd_ymfpci_voice_alloc(ypcm->chip, YMFPCI_PCM, voices > 1, &ypcm->voices[0]); | |
484 | if (err < 0) | |
485 | return err; | |
486 | ypcm->voices[0]->ypcm = ypcm; | |
487 | ypcm->voices[0]->interrupt = snd_ymfpci_pcm_interrupt; | |
488 | if (voices > 1) { | |
489 | ypcm->voices[1] = &ypcm->chip->voices[ypcm->voices[0]->number + 1]; | |
490 | ypcm->voices[1]->ypcm = ypcm; | |
491 | } | |
492 | return 0; | |
493 | } | |
494 | ||
208a1b4c TI |
495 | static void snd_ymfpci_pcm_init_voice(struct snd_ymfpci_pcm *ypcm, unsigned int voiceidx, |
496 | struct snd_pcm_runtime *runtime, | |
9bcf6551 | 497 | int has_pcm_volume) |
1da177e4 | 498 | { |
208a1b4c | 499 | struct snd_ymfpci_voice *voice = ypcm->voices[voiceidx]; |
1da177e4 | 500 | u32 format; |
9bcf6551 CL |
501 | u32 delta = snd_ymfpci_calc_delta(runtime->rate); |
502 | u32 lpfQ = snd_ymfpci_calc_lpfQ(runtime->rate); | |
503 | u32 lpfK = snd_ymfpci_calc_lpfK(runtime->rate); | |
208a1b4c | 504 | struct snd_ymfpci_playback_bank *bank; |
1da177e4 | 505 | unsigned int nbank; |
9bcf6551 CL |
506 | u32 vol_left, vol_right; |
507 | u8 use_left, use_right; | |
9ed1261e | 508 | unsigned long flags; |
1da177e4 | 509 | |
da3cec35 TI |
510 | if (snd_BUG_ON(!voice)) |
511 | return; | |
9bcf6551 CL |
512 | if (runtime->channels == 1) { |
513 | use_left = 1; | |
514 | use_right = 1; | |
515 | } else { | |
516 | use_left = (voiceidx & 1) == 0; | |
517 | use_right = !use_left; | |
518 | } | |
519 | if (has_pcm_volume) { | |
520 | vol_left = cpu_to_le32(ypcm->chip->pcm_mixer | |
521 | [ypcm->substream->number].left << 15); | |
522 | vol_right = cpu_to_le32(ypcm->chip->pcm_mixer | |
523 | [ypcm->substream->number].right << 15); | |
524 | } else { | |
525 | vol_left = cpu_to_le32(0x40000000); | |
526 | vol_right = cpu_to_le32(0x40000000); | |
527 | } | |
9ed1261e | 528 | spin_lock_irqsave(&ypcm->chip->voice_lock, flags); |
9bcf6551 CL |
529 | format = runtime->channels == 2 ? 0x00010000 : 0; |
530 | if (snd_pcm_format_width(runtime->format) == 8) | |
531 | format |= 0x80000000; | |
9ed1261e TK |
532 | else if (ypcm->chip->device_id == PCI_DEVICE_ID_YAMAHA_754 && |
533 | runtime->rate == 44100 && runtime->channels == 2 && | |
534 | voiceidx == 0 && (ypcm->chip->src441_used == -1 || | |
535 | ypcm->chip->src441_used == voice->number)) { | |
536 | ypcm->chip->src441_used = voice->number; | |
537 | ypcm->use_441_slot = 1; | |
538 | format |= 0x10000000; | |
9ed1261e TK |
539 | } |
540 | if (ypcm->chip->src441_used == voice->number && | |
541 | (format & 0x10000000) == 0) { | |
542 | ypcm->chip->src441_used = -1; | |
543 | ypcm->use_441_slot = 0; | |
544 | } | |
9bcf6551 CL |
545 | if (runtime->channels == 2 && (voiceidx & 1) != 0) |
546 | format |= 1; | |
9ed1261e | 547 | spin_unlock_irqrestore(&ypcm->chip->voice_lock, flags); |
1da177e4 LT |
548 | for (nbank = 0; nbank < 2; nbank++) { |
549 | bank = &voice->bank[nbank]; | |
9bcf6551 | 550 | memset(bank, 0, sizeof(*bank)); |
1da177e4 | 551 | bank->format = cpu_to_le32(format); |
9bcf6551 CL |
552 | bank->base = cpu_to_le32(runtime->dma_addr); |
553 | bank->loop_end = cpu_to_le32(ypcm->buffer_size); | |
1da177e4 | 554 | bank->lpfQ = cpu_to_le32(lpfQ); |
1da177e4 LT |
555 | bank->delta = |
556 | bank->delta_end = cpu_to_le32(delta); | |
557 | bank->lpfK = | |
558 | bank->lpfK_end = cpu_to_le32(lpfK); | |
9bcf6551 CL |
559 | bank->eg_gain = |
560 | bank->eg_gain_end = cpu_to_le32(0x40000000); | |
561 | ||
562 | if (ypcm->output_front) { | |
563 | if (use_left) { | |
564 | bank->left_gain = | |
565 | bank->left_gain_end = vol_left; | |
566 | } | |
567 | if (use_right) { | |
1da177e4 | 568 | bank->right_gain = |
9bcf6551 | 569 | bank->right_gain_end = vol_right; |
1da177e4 | 570 | } |
9bcf6551 CL |
571 | } |
572 | if (ypcm->output_rear) { | |
5a25c5cf JK |
573 | if (!ypcm->swap_rear) { |
574 | if (use_left) { | |
575 | bank->eff2_gain = | |
576 | bank->eff2_gain_end = vol_left; | |
577 | } | |
578 | if (use_right) { | |
579 | bank->eff3_gain = | |
580 | bank->eff3_gain_end = vol_right; | |
581 | } | |
582 | } else { | |
583 | /* The SPDIF out channels seem to be swapped, so we have | |
584 | * to swap them here, too. The rear analog out channels | |
585 | * will be wrong, but otherwise AC3 would not work. | |
586 | */ | |
587 | if (use_left) { | |
588 | bank->eff3_gain = | |
589 | bank->eff3_gain_end = vol_left; | |
590 | } | |
591 | if (use_right) { | |
592 | bank->eff2_gain = | |
593 | bank->eff2_gain_end = vol_right; | |
594 | } | |
595 | } | |
596 | } | |
1da177e4 LT |
597 | } |
598 | } | |
599 | ||
208a1b4c | 600 | static int __devinit snd_ymfpci_ac3_init(struct snd_ymfpci *chip) |
1da177e4 LT |
601 | { |
602 | if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(chip->pci), | |
603 | 4096, &chip->ac3_tmp_base) < 0) | |
604 | return -ENOMEM; | |
605 | ||
606 | chip->bank_effect[3][0]->base = | |
607 | chip->bank_effect[3][1]->base = cpu_to_le32(chip->ac3_tmp_base.addr); | |
608 | chip->bank_effect[3][0]->loop_end = | |
609 | chip->bank_effect[3][1]->loop_end = cpu_to_le32(1024); | |
610 | chip->bank_effect[4][0]->base = | |
611 | chip->bank_effect[4][1]->base = cpu_to_le32(chip->ac3_tmp_base.addr + 2048); | |
612 | chip->bank_effect[4][0]->loop_end = | |
613 | chip->bank_effect[4][1]->loop_end = cpu_to_le32(1024); | |
614 | ||
615 | spin_lock_irq(&chip->reg_lock); | |
616 | snd_ymfpci_writel(chip, YDSXGR_MAPOFEFFECT, | |
617 | snd_ymfpci_readl(chip, YDSXGR_MAPOFEFFECT) | 3 << 3); | |
618 | spin_unlock_irq(&chip->reg_lock); | |
619 | return 0; | |
620 | } | |
621 | ||
208a1b4c | 622 | static int snd_ymfpci_ac3_done(struct snd_ymfpci *chip) |
1da177e4 LT |
623 | { |
624 | spin_lock_irq(&chip->reg_lock); | |
625 | snd_ymfpci_writel(chip, YDSXGR_MAPOFEFFECT, | |
626 | snd_ymfpci_readl(chip, YDSXGR_MAPOFEFFECT) & ~(3 << 3)); | |
627 | spin_unlock_irq(&chip->reg_lock); | |
628 | // snd_ymfpci_irq_wait(chip); | |
629 | if (chip->ac3_tmp_base.area) { | |
630 | snd_dma_free_pages(&chip->ac3_tmp_base); | |
631 | chip->ac3_tmp_base.area = NULL; | |
632 | } | |
633 | return 0; | |
634 | } | |
635 | ||
208a1b4c TI |
636 | static int snd_ymfpci_playback_hw_params(struct snd_pcm_substream *substream, |
637 | struct snd_pcm_hw_params *hw_params) | |
1da177e4 | 638 | { |
208a1b4c TI |
639 | struct snd_pcm_runtime *runtime = substream->runtime; |
640 | struct snd_ymfpci_pcm *ypcm = runtime->private_data; | |
1da177e4 LT |
641 | int err; |
642 | ||
643 | if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0) | |
644 | return err; | |
645 | if ((err = snd_ymfpci_pcm_voice_alloc(ypcm, params_channels(hw_params))) < 0) | |
646 | return err; | |
647 | return 0; | |
648 | } | |
649 | ||
208a1b4c | 650 | static int snd_ymfpci_playback_hw_free(struct snd_pcm_substream *substream) |
1da177e4 | 651 | { |
208a1b4c TI |
652 | struct snd_ymfpci *chip = snd_pcm_substream_chip(substream); |
653 | struct snd_pcm_runtime *runtime = substream->runtime; | |
654 | struct snd_ymfpci_pcm *ypcm; | |
1da177e4 LT |
655 | |
656 | if (runtime->private_data == NULL) | |
657 | return 0; | |
658 | ypcm = runtime->private_data; | |
659 | ||
660 | /* wait, until the PCI operations are not finished */ | |
661 | snd_ymfpci_irq_wait(chip); | |
662 | snd_pcm_lib_free_pages(substream); | |
663 | if (ypcm->voices[1]) { | |
664 | snd_ymfpci_voice_free(chip, ypcm->voices[1]); | |
665 | ypcm->voices[1] = NULL; | |
666 | } | |
667 | if (ypcm->voices[0]) { | |
668 | snd_ymfpci_voice_free(chip, ypcm->voices[0]); | |
669 | ypcm->voices[0] = NULL; | |
670 | } | |
671 | return 0; | |
672 | } | |
673 | ||
208a1b4c | 674 | static int snd_ymfpci_playback_prepare(struct snd_pcm_substream *substream) |
1da177e4 | 675 | { |
208a1b4c TI |
676 | struct snd_ymfpci *chip = snd_pcm_substream_chip(substream); |
677 | struct snd_pcm_runtime *runtime = substream->runtime; | |
678 | struct snd_ymfpci_pcm *ypcm = runtime->private_data; | |
177a7cdb | 679 | struct snd_kcontrol *kctl; |
1da177e4 LT |
680 | unsigned int nvoice; |
681 | ||
682 | ypcm->period_size = runtime->period_size; | |
683 | ypcm->buffer_size = runtime->buffer_size; | |
684 | ypcm->period_pos = 0; | |
685 | ypcm->last_pos = 0; | |
686 | for (nvoice = 0; nvoice < runtime->channels; nvoice++) | |
9bcf6551 CL |
687 | snd_ymfpci_pcm_init_voice(ypcm, nvoice, runtime, |
688 | substream->pcm == chip->pcm); | |
177a7cdb CL |
689 | |
690 | if (substream->pcm == chip->pcm && !ypcm->use_441_slot) { | |
691 | kctl = chip->pcm_mixer[substream->number].ctl; | |
692 | kctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE; | |
693 | snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_INFO, &kctl->id); | |
694 | } | |
1da177e4 LT |
695 | return 0; |
696 | } | |
697 | ||
208a1b4c TI |
698 | static int snd_ymfpci_capture_hw_params(struct snd_pcm_substream *substream, |
699 | struct snd_pcm_hw_params *hw_params) | |
1da177e4 LT |
700 | { |
701 | return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params)); | |
702 | } | |
703 | ||
208a1b4c | 704 | static int snd_ymfpci_capture_hw_free(struct snd_pcm_substream *substream) |
1da177e4 | 705 | { |
208a1b4c | 706 | struct snd_ymfpci *chip = snd_pcm_substream_chip(substream); |
1da177e4 LT |
707 | |
708 | /* wait, until the PCI operations are not finished */ | |
709 | snd_ymfpci_irq_wait(chip); | |
710 | return snd_pcm_lib_free_pages(substream); | |
711 | } | |
712 | ||
208a1b4c | 713 | static int snd_ymfpci_capture_prepare(struct snd_pcm_substream *substream) |
1da177e4 | 714 | { |
208a1b4c TI |
715 | struct snd_ymfpci *chip = snd_pcm_substream_chip(substream); |
716 | struct snd_pcm_runtime *runtime = substream->runtime; | |
717 | struct snd_ymfpci_pcm *ypcm = runtime->private_data; | |
718 | struct snd_ymfpci_capture_bank * bank; | |
1da177e4 LT |
719 | int nbank; |
720 | u32 rate, format; | |
721 | ||
722 | ypcm->period_size = runtime->period_size; | |
723 | ypcm->buffer_size = runtime->buffer_size; | |
724 | ypcm->period_pos = 0; | |
725 | ypcm->last_pos = 0; | |
726 | ypcm->shift = 0; | |
727 | rate = ((48000 * 4096) / runtime->rate) - 1; | |
728 | format = 0; | |
729 | if (runtime->channels == 2) { | |
730 | format |= 2; | |
731 | ypcm->shift++; | |
732 | } | |
733 | if (snd_pcm_format_width(runtime->format) == 8) | |
734 | format |= 1; | |
735 | else | |
736 | ypcm->shift++; | |
737 | switch (ypcm->capture_bank_number) { | |
738 | case 0: | |
739 | snd_ymfpci_writel(chip, YDSXGR_RECFORMAT, format); | |
740 | snd_ymfpci_writel(chip, YDSXGR_RECSLOTSR, rate); | |
741 | break; | |
742 | case 1: | |
743 | snd_ymfpci_writel(chip, YDSXGR_ADCFORMAT, format); | |
744 | snd_ymfpci_writel(chip, YDSXGR_ADCSLOTSR, rate); | |
745 | break; | |
746 | } | |
747 | for (nbank = 0; nbank < 2; nbank++) { | |
748 | bank = chip->bank_capture[ypcm->capture_bank_number][nbank]; | |
749 | bank->base = cpu_to_le32(runtime->dma_addr); | |
750 | bank->loop_end = cpu_to_le32(ypcm->buffer_size << ypcm->shift); | |
751 | bank->start = 0; | |
752 | bank->num_of_loops = 0; | |
753 | } | |
754 | return 0; | |
755 | } | |
756 | ||
208a1b4c | 757 | static snd_pcm_uframes_t snd_ymfpci_playback_pointer(struct snd_pcm_substream *substream) |
1da177e4 | 758 | { |
208a1b4c TI |
759 | struct snd_ymfpci *chip = snd_pcm_substream_chip(substream); |
760 | struct snd_pcm_runtime *runtime = substream->runtime; | |
761 | struct snd_ymfpci_pcm *ypcm = runtime->private_data; | |
762 | struct snd_ymfpci_voice *voice = ypcm->voices[0]; | |
1da177e4 LT |
763 | |
764 | if (!(ypcm->running && voice)) | |
765 | return 0; | |
766 | return le32_to_cpu(voice->bank[chip->active_bank].start); | |
767 | } | |
768 | ||
208a1b4c | 769 | static snd_pcm_uframes_t snd_ymfpci_capture_pointer(struct snd_pcm_substream *substream) |
1da177e4 | 770 | { |
208a1b4c TI |
771 | struct snd_ymfpci *chip = snd_pcm_substream_chip(substream); |
772 | struct snd_pcm_runtime *runtime = substream->runtime; | |
773 | struct snd_ymfpci_pcm *ypcm = runtime->private_data; | |
1da177e4 LT |
774 | |
775 | if (!ypcm->running) | |
776 | return 0; | |
777 | return le32_to_cpu(chip->bank_capture[ypcm->capture_bank_number][chip->active_bank]->start) >> ypcm->shift; | |
778 | } | |
779 | ||
208a1b4c | 780 | static void snd_ymfpci_irq_wait(struct snd_ymfpci *chip) |
1da177e4 LT |
781 | { |
782 | wait_queue_t wait; | |
783 | int loops = 4; | |
784 | ||
785 | while (loops-- > 0) { | |
786 | if ((snd_ymfpci_readl(chip, YDSXGR_MODE) & 3) == 0) | |
787 | continue; | |
788 | init_waitqueue_entry(&wait, current); | |
789 | add_wait_queue(&chip->interrupt_sleep, &wait); | |
790 | atomic_inc(&chip->interrupt_sleep_count); | |
8433a509 | 791 | schedule_timeout_uninterruptible(msecs_to_jiffies(50)); |
1da177e4 LT |
792 | remove_wait_queue(&chip->interrupt_sleep, &wait); |
793 | } | |
794 | } | |
795 | ||
7d12e780 | 796 | static irqreturn_t snd_ymfpci_interrupt(int irq, void *dev_id) |
1da177e4 | 797 | { |
208a1b4c | 798 | struct snd_ymfpci *chip = dev_id; |
1da177e4 | 799 | u32 status, nvoice, mode; |
208a1b4c | 800 | struct snd_ymfpci_voice *voice; |
1da177e4 LT |
801 | |
802 | status = snd_ymfpci_readl(chip, YDSXGR_STATUS); | |
803 | if (status & 0x80000000) { | |
804 | chip->active_bank = snd_ymfpci_readl(chip, YDSXGR_CTRLSELECT) & 1; | |
805 | spin_lock(&chip->voice_lock); | |
806 | for (nvoice = 0; nvoice < YDSXG_PLAYBACK_VOICES; nvoice++) { | |
807 | voice = &chip->voices[nvoice]; | |
808 | if (voice->interrupt) | |
809 | voice->interrupt(chip, voice); | |
810 | } | |
811 | for (nvoice = 0; nvoice < YDSXG_CAPTURE_VOICES; nvoice++) { | |
812 | if (chip->capture_substream[nvoice]) | |
813 | snd_ymfpci_pcm_capture_interrupt(chip->capture_substream[nvoice]); | |
814 | } | |
815 | #if 0 | |
816 | for (nvoice = 0; nvoice < YDSXG_EFFECT_VOICES; nvoice++) { | |
817 | if (chip->effect_substream[nvoice]) | |
818 | snd_ymfpci_pcm_effect_interrupt(chip->effect_substream[nvoice]); | |
819 | } | |
820 | #endif | |
821 | spin_unlock(&chip->voice_lock); | |
822 | spin_lock(&chip->reg_lock); | |
823 | snd_ymfpci_writel(chip, YDSXGR_STATUS, 0x80000000); | |
824 | mode = snd_ymfpci_readl(chip, YDSXGR_MODE) | 2; | |
825 | snd_ymfpci_writel(chip, YDSXGR_MODE, mode); | |
826 | spin_unlock(&chip->reg_lock); | |
827 | ||
828 | if (atomic_read(&chip->interrupt_sleep_count)) { | |
829 | atomic_set(&chip->interrupt_sleep_count, 0); | |
830 | wake_up(&chip->interrupt_sleep); | |
831 | } | |
832 | } | |
833 | ||
834 | status = snd_ymfpci_readw(chip, YDSXGR_INTFLAG); | |
835 | if (status & 1) { | |
836 | if (chip->timer) | |
6e2efaac | 837 | snd_timer_interrupt(chip->timer, chip->timer_ticks); |
1da177e4 LT |
838 | } |
839 | snd_ymfpci_writew(chip, YDSXGR_INTFLAG, status); | |
840 | ||
841 | if (chip->rawmidi) | |
7d12e780 | 842 | snd_mpu401_uart_interrupt(irq, chip->rawmidi->private_data); |
1da177e4 LT |
843 | return IRQ_HANDLED; |
844 | } | |
845 | ||
208a1b4c | 846 | static struct snd_pcm_hardware snd_ymfpci_playback = |
1da177e4 LT |
847 | { |
848 | .info = (SNDRV_PCM_INFO_MMAP | | |
849 | SNDRV_PCM_INFO_MMAP_VALID | | |
850 | SNDRV_PCM_INFO_INTERLEAVED | | |
851 | SNDRV_PCM_INFO_BLOCK_TRANSFER | | |
852 | SNDRV_PCM_INFO_PAUSE | | |
853 | SNDRV_PCM_INFO_RESUME), | |
854 | .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE, | |
855 | .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000, | |
856 | .rate_min = 8000, | |
857 | .rate_max = 48000, | |
858 | .channels_min = 1, | |
859 | .channels_max = 2, | |
860 | .buffer_bytes_max = 256 * 1024, /* FIXME: enough? */ | |
861 | .period_bytes_min = 64, | |
862 | .period_bytes_max = 256 * 1024, /* FIXME: enough? */ | |
863 | .periods_min = 3, | |
864 | .periods_max = 1024, | |
865 | .fifo_size = 0, | |
866 | }; | |
867 | ||
208a1b4c | 868 | static struct snd_pcm_hardware snd_ymfpci_capture = |
1da177e4 LT |
869 | { |
870 | .info = (SNDRV_PCM_INFO_MMAP | | |
871 | SNDRV_PCM_INFO_MMAP_VALID | | |
872 | SNDRV_PCM_INFO_INTERLEAVED | | |
873 | SNDRV_PCM_INFO_BLOCK_TRANSFER | | |
874 | SNDRV_PCM_INFO_PAUSE | | |
875 | SNDRV_PCM_INFO_RESUME), | |
876 | .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE, | |
877 | .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000, | |
878 | .rate_min = 8000, | |
879 | .rate_max = 48000, | |
880 | .channels_min = 1, | |
881 | .channels_max = 2, | |
882 | .buffer_bytes_max = 256 * 1024, /* FIXME: enough? */ | |
883 | .period_bytes_min = 64, | |
884 | .period_bytes_max = 256 * 1024, /* FIXME: enough? */ | |
885 | .periods_min = 3, | |
886 | .periods_max = 1024, | |
887 | .fifo_size = 0, | |
888 | }; | |
889 | ||
208a1b4c | 890 | static void snd_ymfpci_pcm_free_substream(struct snd_pcm_runtime *runtime) |
1da177e4 | 891 | { |
4d572776 | 892 | kfree(runtime->private_data); |
1da177e4 LT |
893 | } |
894 | ||
208a1b4c | 895 | static int snd_ymfpci_playback_open_1(struct snd_pcm_substream *substream) |
1da177e4 | 896 | { |
208a1b4c TI |
897 | struct snd_ymfpci *chip = snd_pcm_substream_chip(substream); |
898 | struct snd_pcm_runtime *runtime = substream->runtime; | |
899 | struct snd_ymfpci_pcm *ypcm; | |
1da177e4 | 900 | |
e560d8d8 | 901 | ypcm = kzalloc(sizeof(*ypcm), GFP_KERNEL); |
1da177e4 LT |
902 | if (ypcm == NULL) |
903 | return -ENOMEM; | |
904 | ypcm->chip = chip; | |
905 | ypcm->type = PLAYBACK_VOICE; | |
906 | ypcm->substream = substream; | |
907 | runtime->hw = snd_ymfpci_playback; | |
908 | runtime->private_data = ypcm; | |
909 | runtime->private_free = snd_ymfpci_pcm_free_substream; | |
910 | /* FIXME? True value is 256/48 = 5.33333 ms */ | |
911 | snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME, 5333, UINT_MAX); | |
912 | return 0; | |
913 | } | |
914 | ||
915 | /* call with spinlock held */ | |
208a1b4c | 916 | static void ymfpci_open_extension(struct snd_ymfpci *chip) |
1da177e4 LT |
917 | { |
918 | if (! chip->rear_opened) { | |
919 | if (! chip->spdif_opened) /* set AC3 */ | |
920 | snd_ymfpci_writel(chip, YDSXGR_MODE, | |
921 | snd_ymfpci_readl(chip, YDSXGR_MODE) | (1 << 30)); | |
922 | /* enable second codec (4CHEN) */ | |
923 | snd_ymfpci_writew(chip, YDSXGR_SECCONFIG, | |
924 | (snd_ymfpci_readw(chip, YDSXGR_SECCONFIG) & ~0x0330) | 0x0010); | |
925 | } | |
926 | } | |
927 | ||
928 | /* call with spinlock held */ | |
208a1b4c | 929 | static void ymfpci_close_extension(struct snd_ymfpci *chip) |
1da177e4 LT |
930 | { |
931 | if (! chip->rear_opened) { | |
932 | if (! chip->spdif_opened) | |
933 | snd_ymfpci_writel(chip, YDSXGR_MODE, | |
934 | snd_ymfpci_readl(chip, YDSXGR_MODE) & ~(1 << 30)); | |
935 | snd_ymfpci_writew(chip, YDSXGR_SECCONFIG, | |
936 | (snd_ymfpci_readw(chip, YDSXGR_SECCONFIG) & ~0x0330) & ~0x0010); | |
937 | } | |
938 | } | |
939 | ||
208a1b4c | 940 | static int snd_ymfpci_playback_open(struct snd_pcm_substream *substream) |
1da177e4 | 941 | { |
208a1b4c TI |
942 | struct snd_ymfpci *chip = snd_pcm_substream_chip(substream); |
943 | struct snd_pcm_runtime *runtime = substream->runtime; | |
944 | struct snd_ymfpci_pcm *ypcm; | |
1da177e4 LT |
945 | int err; |
946 | ||
947 | if ((err = snd_ymfpci_playback_open_1(substream)) < 0) | |
948 | return err; | |
949 | ypcm = runtime->private_data; | |
950 | ypcm->output_front = 1; | |
951 | ypcm->output_rear = chip->mode_dup4ch ? 1 : 0; | |
d9301263 | 952 | ypcm->swap_rear = 0; |
1da177e4 LT |
953 | spin_lock_irq(&chip->reg_lock); |
954 | if (ypcm->output_rear) { | |
955 | ymfpci_open_extension(chip); | |
956 | chip->rear_opened++; | |
957 | } | |
958 | spin_unlock_irq(&chip->reg_lock); | |
959 | return 0; | |
960 | } | |
961 | ||
208a1b4c | 962 | static int snd_ymfpci_playback_spdif_open(struct snd_pcm_substream *substream) |
1da177e4 | 963 | { |
208a1b4c TI |
964 | struct snd_ymfpci *chip = snd_pcm_substream_chip(substream); |
965 | struct snd_pcm_runtime *runtime = substream->runtime; | |
966 | struct snd_ymfpci_pcm *ypcm; | |
1da177e4 LT |
967 | int err; |
968 | ||
969 | if ((err = snd_ymfpci_playback_open_1(substream)) < 0) | |
970 | return err; | |
971 | ypcm = runtime->private_data; | |
972 | ypcm->output_front = 0; | |
973 | ypcm->output_rear = 1; | |
d9301263 | 974 | ypcm->swap_rear = 1; |
1da177e4 LT |
975 | spin_lock_irq(&chip->reg_lock); |
976 | snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTCTRL, | |
977 | snd_ymfpci_readw(chip, YDSXGR_SPDIFOUTCTRL) | 2); | |
978 | ymfpci_open_extension(chip); | |
979 | chip->spdif_pcm_bits = chip->spdif_bits; | |
980 | snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_pcm_bits); | |
981 | chip->spdif_opened++; | |
982 | spin_unlock_irq(&chip->reg_lock); | |
983 | ||
984 | chip->spdif_pcm_ctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE; | |
985 | snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE | | |
986 | SNDRV_CTL_EVENT_MASK_INFO, &chip->spdif_pcm_ctl->id); | |
987 | return 0; | |
988 | } | |
989 | ||
208a1b4c | 990 | static int snd_ymfpci_playback_4ch_open(struct snd_pcm_substream *substream) |
1da177e4 | 991 | { |
208a1b4c TI |
992 | struct snd_ymfpci *chip = snd_pcm_substream_chip(substream); |
993 | struct snd_pcm_runtime *runtime = substream->runtime; | |
994 | struct snd_ymfpci_pcm *ypcm; | |
1da177e4 LT |
995 | int err; |
996 | ||
997 | if ((err = snd_ymfpci_playback_open_1(substream)) < 0) | |
998 | return err; | |
999 | ypcm = runtime->private_data; | |
1000 | ypcm->output_front = 0; | |
1001 | ypcm->output_rear = 1; | |
d9301263 | 1002 | ypcm->swap_rear = 0; |
1da177e4 LT |
1003 | spin_lock_irq(&chip->reg_lock); |
1004 | ymfpci_open_extension(chip); | |
1005 | chip->rear_opened++; | |
1006 | spin_unlock_irq(&chip->reg_lock); | |
1007 | return 0; | |
1008 | } | |
1009 | ||
208a1b4c | 1010 | static int snd_ymfpci_capture_open(struct snd_pcm_substream *substream, |
1da177e4 LT |
1011 | u32 capture_bank_number) |
1012 | { | |
208a1b4c TI |
1013 | struct snd_ymfpci *chip = snd_pcm_substream_chip(substream); |
1014 | struct snd_pcm_runtime *runtime = substream->runtime; | |
1015 | struct snd_ymfpci_pcm *ypcm; | |
1da177e4 | 1016 | |
e560d8d8 | 1017 | ypcm = kzalloc(sizeof(*ypcm), GFP_KERNEL); |
1da177e4 LT |
1018 | if (ypcm == NULL) |
1019 | return -ENOMEM; | |
1020 | ypcm->chip = chip; | |
1021 | ypcm->type = capture_bank_number + CAPTURE_REC; | |
1022 | ypcm->substream = substream; | |
1023 | ypcm->capture_bank_number = capture_bank_number; | |
1024 | chip->capture_substream[capture_bank_number] = substream; | |
1025 | runtime->hw = snd_ymfpci_capture; | |
1026 | /* FIXME? True value is 256/48 = 5.33333 ms */ | |
1027 | snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME, 5333, UINT_MAX); | |
1028 | runtime->private_data = ypcm; | |
1029 | runtime->private_free = snd_ymfpci_pcm_free_substream; | |
1030 | snd_ymfpci_hw_start(chip); | |
1031 | return 0; | |
1032 | } | |
1033 | ||
208a1b4c | 1034 | static int snd_ymfpci_capture_rec_open(struct snd_pcm_substream *substream) |
1da177e4 LT |
1035 | { |
1036 | return snd_ymfpci_capture_open(substream, 0); | |
1037 | } | |
1038 | ||
208a1b4c | 1039 | static int snd_ymfpci_capture_ac97_open(struct snd_pcm_substream *substream) |
1da177e4 LT |
1040 | { |
1041 | return snd_ymfpci_capture_open(substream, 1); | |
1042 | } | |
1043 | ||
208a1b4c | 1044 | static int snd_ymfpci_playback_close_1(struct snd_pcm_substream *substream) |
1da177e4 LT |
1045 | { |
1046 | return 0; | |
1047 | } | |
1048 | ||
208a1b4c | 1049 | static int snd_ymfpci_playback_close(struct snd_pcm_substream *substream) |
1da177e4 | 1050 | { |
208a1b4c TI |
1051 | struct snd_ymfpci *chip = snd_pcm_substream_chip(substream); |
1052 | struct snd_ymfpci_pcm *ypcm = substream->runtime->private_data; | |
1da177e4 LT |
1053 | |
1054 | spin_lock_irq(&chip->reg_lock); | |
1055 | if (ypcm->output_rear && chip->rear_opened > 0) { | |
1056 | chip->rear_opened--; | |
1057 | ymfpci_close_extension(chip); | |
1058 | } | |
1059 | spin_unlock_irq(&chip->reg_lock); | |
1060 | return snd_ymfpci_playback_close_1(substream); | |
1061 | } | |
1062 | ||
208a1b4c | 1063 | static int snd_ymfpci_playback_spdif_close(struct snd_pcm_substream *substream) |
1da177e4 | 1064 | { |
208a1b4c | 1065 | struct snd_ymfpci *chip = snd_pcm_substream_chip(substream); |
1da177e4 LT |
1066 | |
1067 | spin_lock_irq(&chip->reg_lock); | |
1068 | chip->spdif_opened = 0; | |
1069 | ymfpci_close_extension(chip); | |
1070 | snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTCTRL, | |
1071 | snd_ymfpci_readw(chip, YDSXGR_SPDIFOUTCTRL) & ~2); | |
1072 | snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_bits); | |
1073 | spin_unlock_irq(&chip->reg_lock); | |
1074 | chip->spdif_pcm_ctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE; | |
1075 | snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE | | |
1076 | SNDRV_CTL_EVENT_MASK_INFO, &chip->spdif_pcm_ctl->id); | |
1077 | return snd_ymfpci_playback_close_1(substream); | |
1078 | } | |
1079 | ||
208a1b4c | 1080 | static int snd_ymfpci_playback_4ch_close(struct snd_pcm_substream *substream) |
1da177e4 | 1081 | { |
208a1b4c | 1082 | struct snd_ymfpci *chip = snd_pcm_substream_chip(substream); |
1da177e4 LT |
1083 | |
1084 | spin_lock_irq(&chip->reg_lock); | |
1085 | if (chip->rear_opened > 0) { | |
1086 | chip->rear_opened--; | |
1087 | ymfpci_close_extension(chip); | |
1088 | } | |
1089 | spin_unlock_irq(&chip->reg_lock); | |
1090 | return snd_ymfpci_playback_close_1(substream); | |
1091 | } | |
1092 | ||
208a1b4c | 1093 | static int snd_ymfpci_capture_close(struct snd_pcm_substream *substream) |
1da177e4 | 1094 | { |
208a1b4c TI |
1095 | struct snd_ymfpci *chip = snd_pcm_substream_chip(substream); |
1096 | struct snd_pcm_runtime *runtime = substream->runtime; | |
1097 | struct snd_ymfpci_pcm *ypcm = runtime->private_data; | |
1da177e4 LT |
1098 | |
1099 | if (ypcm != NULL) { | |
1100 | chip->capture_substream[ypcm->capture_bank_number] = NULL; | |
1101 | snd_ymfpci_hw_stop(chip); | |
1102 | } | |
1103 | return 0; | |
1104 | } | |
1105 | ||
208a1b4c | 1106 | static struct snd_pcm_ops snd_ymfpci_playback_ops = { |
1da177e4 LT |
1107 | .open = snd_ymfpci_playback_open, |
1108 | .close = snd_ymfpci_playback_close, | |
1109 | .ioctl = snd_pcm_lib_ioctl, | |
1110 | .hw_params = snd_ymfpci_playback_hw_params, | |
1111 | .hw_free = snd_ymfpci_playback_hw_free, | |
1112 | .prepare = snd_ymfpci_playback_prepare, | |
1113 | .trigger = snd_ymfpci_playback_trigger, | |
1114 | .pointer = snd_ymfpci_playback_pointer, | |
1115 | }; | |
1116 | ||
208a1b4c | 1117 | static struct snd_pcm_ops snd_ymfpci_capture_rec_ops = { |
1da177e4 LT |
1118 | .open = snd_ymfpci_capture_rec_open, |
1119 | .close = snd_ymfpci_capture_close, | |
1120 | .ioctl = snd_pcm_lib_ioctl, | |
1121 | .hw_params = snd_ymfpci_capture_hw_params, | |
1122 | .hw_free = snd_ymfpci_capture_hw_free, | |
1123 | .prepare = snd_ymfpci_capture_prepare, | |
1124 | .trigger = snd_ymfpci_capture_trigger, | |
1125 | .pointer = snd_ymfpci_capture_pointer, | |
1126 | }; | |
1127 | ||
208a1b4c | 1128 | int __devinit snd_ymfpci_pcm(struct snd_ymfpci *chip, int device, struct snd_pcm ** rpcm) |
1da177e4 | 1129 | { |
208a1b4c | 1130 | struct snd_pcm *pcm; |
1da177e4 LT |
1131 | int err; |
1132 | ||
1133 | if (rpcm) | |
1134 | *rpcm = NULL; | |
1135 | if ((err = snd_pcm_new(chip->card, "YMFPCI", device, 32, 1, &pcm)) < 0) | |
1136 | return err; | |
1137 | pcm->private_data = chip; | |
1da177e4 LT |
1138 | |
1139 | snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ymfpci_playback_ops); | |
1140 | snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ymfpci_capture_rec_ops); | |
1141 | ||
1142 | /* global setup */ | |
1143 | pcm->info_flags = 0; | |
1144 | strcpy(pcm->name, "YMFPCI"); | |
1145 | chip->pcm = pcm; | |
1146 | ||
1147 | snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV, | |
1148 | snd_dma_pci_data(chip->pci), 64*1024, 256*1024); | |
1149 | ||
1150 | if (rpcm) | |
1151 | *rpcm = pcm; | |
1152 | return 0; | |
1153 | } | |
1154 | ||
208a1b4c | 1155 | static struct snd_pcm_ops snd_ymfpci_capture_ac97_ops = { |
1da177e4 LT |
1156 | .open = snd_ymfpci_capture_ac97_open, |
1157 | .close = snd_ymfpci_capture_close, | |
1158 | .ioctl = snd_pcm_lib_ioctl, | |
1159 | .hw_params = snd_ymfpci_capture_hw_params, | |
1160 | .hw_free = snd_ymfpci_capture_hw_free, | |
1161 | .prepare = snd_ymfpci_capture_prepare, | |
1162 | .trigger = snd_ymfpci_capture_trigger, | |
1163 | .pointer = snd_ymfpci_capture_pointer, | |
1164 | }; | |
1165 | ||
208a1b4c | 1166 | int __devinit snd_ymfpci_pcm2(struct snd_ymfpci *chip, int device, struct snd_pcm ** rpcm) |
1da177e4 | 1167 | { |
208a1b4c | 1168 | struct snd_pcm *pcm; |
1da177e4 LT |
1169 | int err; |
1170 | ||
1171 | if (rpcm) | |
1172 | *rpcm = NULL; | |
1173 | if ((err = snd_pcm_new(chip->card, "YMFPCI - PCM2", device, 0, 1, &pcm)) < 0) | |
1174 | return err; | |
1175 | pcm->private_data = chip; | |
1da177e4 LT |
1176 | |
1177 | snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ymfpci_capture_ac97_ops); | |
1178 | ||
1179 | /* global setup */ | |
1180 | pcm->info_flags = 0; | |
1181 | sprintf(pcm->name, "YMFPCI - %s", | |
1182 | chip->device_id == PCI_DEVICE_ID_YAMAHA_754 ? "Direct Recording" : "AC'97"); | |
1183 | chip->pcm2 = pcm; | |
1184 | ||
1185 | snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV, | |
1186 | snd_dma_pci_data(chip->pci), 64*1024, 256*1024); | |
1187 | ||
1188 | if (rpcm) | |
1189 | *rpcm = pcm; | |
1190 | return 0; | |
1191 | } | |
1192 | ||
208a1b4c | 1193 | static struct snd_pcm_ops snd_ymfpci_playback_spdif_ops = { |
1da177e4 LT |
1194 | .open = snd_ymfpci_playback_spdif_open, |
1195 | .close = snd_ymfpci_playback_spdif_close, | |
1196 | .ioctl = snd_pcm_lib_ioctl, | |
1197 | .hw_params = snd_ymfpci_playback_hw_params, | |
1198 | .hw_free = snd_ymfpci_playback_hw_free, | |
1199 | .prepare = snd_ymfpci_playback_prepare, | |
1200 | .trigger = snd_ymfpci_playback_trigger, | |
1201 | .pointer = snd_ymfpci_playback_pointer, | |
1202 | }; | |
1203 | ||
208a1b4c | 1204 | int __devinit snd_ymfpci_pcm_spdif(struct snd_ymfpci *chip, int device, struct snd_pcm ** rpcm) |
1da177e4 | 1205 | { |
208a1b4c | 1206 | struct snd_pcm *pcm; |
1da177e4 LT |
1207 | int err; |
1208 | ||
1209 | if (rpcm) | |
1210 | *rpcm = NULL; | |
1211 | if ((err = snd_pcm_new(chip->card, "YMFPCI - IEC958", device, 1, 0, &pcm)) < 0) | |
1212 | return err; | |
1213 | pcm->private_data = chip; | |
1da177e4 LT |
1214 | |
1215 | snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ymfpci_playback_spdif_ops); | |
1216 | ||
1217 | /* global setup */ | |
1218 | pcm->info_flags = 0; | |
1219 | strcpy(pcm->name, "YMFPCI - IEC958"); | |
1220 | chip->pcm_spdif = pcm; | |
1221 | ||
1222 | snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV, | |
1223 | snd_dma_pci_data(chip->pci), 64*1024, 256*1024); | |
1224 | ||
1225 | if (rpcm) | |
1226 | *rpcm = pcm; | |
1227 | return 0; | |
1228 | } | |
1229 | ||
208a1b4c | 1230 | static struct snd_pcm_ops snd_ymfpci_playback_4ch_ops = { |
1da177e4 LT |
1231 | .open = snd_ymfpci_playback_4ch_open, |
1232 | .close = snd_ymfpci_playback_4ch_close, | |
1233 | .ioctl = snd_pcm_lib_ioctl, | |
1234 | .hw_params = snd_ymfpci_playback_hw_params, | |
1235 | .hw_free = snd_ymfpci_playback_hw_free, | |
1236 | .prepare = snd_ymfpci_playback_prepare, | |
1237 | .trigger = snd_ymfpci_playback_trigger, | |
1238 | .pointer = snd_ymfpci_playback_pointer, | |
1239 | }; | |
1240 | ||
208a1b4c | 1241 | int __devinit snd_ymfpci_pcm_4ch(struct snd_ymfpci *chip, int device, struct snd_pcm ** rpcm) |
1da177e4 | 1242 | { |
208a1b4c | 1243 | struct snd_pcm *pcm; |
1da177e4 LT |
1244 | int err; |
1245 | ||
1246 | if (rpcm) | |
1247 | *rpcm = NULL; | |
1248 | if ((err = snd_pcm_new(chip->card, "YMFPCI - Rear", device, 1, 0, &pcm)) < 0) | |
1249 | return err; | |
1250 | pcm->private_data = chip; | |
1da177e4 LT |
1251 | |
1252 | snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ymfpci_playback_4ch_ops); | |
1253 | ||
1254 | /* global setup */ | |
1255 | pcm->info_flags = 0; | |
1256 | strcpy(pcm->name, "YMFPCI - Rear PCM"); | |
1257 | chip->pcm_4ch = pcm; | |
1258 | ||
1259 | snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV, | |
1260 | snd_dma_pci_data(chip->pci), 64*1024, 256*1024); | |
1261 | ||
1262 | if (rpcm) | |
1263 | *rpcm = pcm; | |
1264 | return 0; | |
1265 | } | |
1266 | ||
208a1b4c | 1267 | static int snd_ymfpci_spdif_default_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo) |
1da177e4 LT |
1268 | { |
1269 | uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958; | |
1270 | uinfo->count = 1; | |
1271 | return 0; | |
1272 | } | |
1273 | ||
208a1b4c TI |
1274 | static int snd_ymfpci_spdif_default_get(struct snd_kcontrol *kcontrol, |
1275 | struct snd_ctl_elem_value *ucontrol) | |
1da177e4 | 1276 | { |
208a1b4c | 1277 | struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol); |
1da177e4 LT |
1278 | |
1279 | spin_lock_irq(&chip->reg_lock); | |
1280 | ucontrol->value.iec958.status[0] = (chip->spdif_bits >> 0) & 0xff; | |
1281 | ucontrol->value.iec958.status[1] = (chip->spdif_bits >> 8) & 0xff; | |
fc80a202 | 1282 | ucontrol->value.iec958.status[3] = IEC958_AES3_CON_FS_48000; |
1da177e4 LT |
1283 | spin_unlock_irq(&chip->reg_lock); |
1284 | return 0; | |
1285 | } | |
1286 | ||
208a1b4c TI |
1287 | static int snd_ymfpci_spdif_default_put(struct snd_kcontrol *kcontrol, |
1288 | struct snd_ctl_elem_value *ucontrol) | |
1da177e4 | 1289 | { |
208a1b4c | 1290 | struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol); |
1da177e4 LT |
1291 | unsigned int val; |
1292 | int change; | |
1293 | ||
1294 | val = ((ucontrol->value.iec958.status[0] & 0x3e) << 0) | | |
1295 | (ucontrol->value.iec958.status[1] << 8); | |
1296 | spin_lock_irq(&chip->reg_lock); | |
1297 | change = chip->spdif_bits != val; | |
1298 | chip->spdif_bits = val; | |
1299 | if ((snd_ymfpci_readw(chip, YDSXGR_SPDIFOUTCTRL) & 1) && chip->pcm_spdif == NULL) | |
1300 | snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_bits); | |
1301 | spin_unlock_irq(&chip->reg_lock); | |
1302 | return change; | |
1303 | } | |
1304 | ||
208a1b4c | 1305 | static struct snd_kcontrol_new snd_ymfpci_spdif_default __devinitdata = |
1da177e4 LT |
1306 | { |
1307 | .iface = SNDRV_CTL_ELEM_IFACE_PCM, | |
1308 | .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT), | |
1309 | .info = snd_ymfpci_spdif_default_info, | |
1310 | .get = snd_ymfpci_spdif_default_get, | |
1311 | .put = snd_ymfpci_spdif_default_put | |
1312 | }; | |
1313 | ||
208a1b4c | 1314 | static int snd_ymfpci_spdif_mask_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo) |
1da177e4 LT |
1315 | { |
1316 | uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958; | |
1317 | uinfo->count = 1; | |
1318 | return 0; | |
1319 | } | |
1320 | ||
208a1b4c TI |
1321 | static int snd_ymfpci_spdif_mask_get(struct snd_kcontrol *kcontrol, |
1322 | struct snd_ctl_elem_value *ucontrol) | |
1da177e4 | 1323 | { |
208a1b4c | 1324 | struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol); |
1da177e4 LT |
1325 | |
1326 | spin_lock_irq(&chip->reg_lock); | |
1327 | ucontrol->value.iec958.status[0] = 0x3e; | |
1328 | ucontrol->value.iec958.status[1] = 0xff; | |
1329 | spin_unlock_irq(&chip->reg_lock); | |
1330 | return 0; | |
1331 | } | |
1332 | ||
208a1b4c | 1333 | static struct snd_kcontrol_new snd_ymfpci_spdif_mask __devinitdata = |
1da177e4 LT |
1334 | { |
1335 | .access = SNDRV_CTL_ELEM_ACCESS_READ, | |
1336 | .iface = SNDRV_CTL_ELEM_IFACE_PCM, | |
1337 | .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK), | |
1338 | .info = snd_ymfpci_spdif_mask_info, | |
1339 | .get = snd_ymfpci_spdif_mask_get, | |
1340 | }; | |
1341 | ||
208a1b4c | 1342 | static int snd_ymfpci_spdif_stream_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo) |
1da177e4 LT |
1343 | { |
1344 | uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958; | |
1345 | uinfo->count = 1; | |
1346 | return 0; | |
1347 | } | |
1348 | ||
208a1b4c TI |
1349 | static int snd_ymfpci_spdif_stream_get(struct snd_kcontrol *kcontrol, |
1350 | struct snd_ctl_elem_value *ucontrol) | |
1da177e4 | 1351 | { |
208a1b4c | 1352 | struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol); |
1da177e4 LT |
1353 | |
1354 | spin_lock_irq(&chip->reg_lock); | |
1355 | ucontrol->value.iec958.status[0] = (chip->spdif_pcm_bits >> 0) & 0xff; | |
1356 | ucontrol->value.iec958.status[1] = (chip->spdif_pcm_bits >> 8) & 0xff; | |
fc80a202 | 1357 | ucontrol->value.iec958.status[3] = IEC958_AES3_CON_FS_48000; |
1da177e4 LT |
1358 | spin_unlock_irq(&chip->reg_lock); |
1359 | return 0; | |
1360 | } | |
1361 | ||
208a1b4c TI |
1362 | static int snd_ymfpci_spdif_stream_put(struct snd_kcontrol *kcontrol, |
1363 | struct snd_ctl_elem_value *ucontrol) | |
1da177e4 | 1364 | { |
208a1b4c | 1365 | struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol); |
1da177e4 LT |
1366 | unsigned int val; |
1367 | int change; | |
1368 | ||
1369 | val = ((ucontrol->value.iec958.status[0] & 0x3e) << 0) | | |
1370 | (ucontrol->value.iec958.status[1] << 8); | |
1371 | spin_lock_irq(&chip->reg_lock); | |
1372 | change = chip->spdif_pcm_bits != val; | |
1373 | chip->spdif_pcm_bits = val; | |
1374 | if ((snd_ymfpci_readw(chip, YDSXGR_SPDIFOUTCTRL) & 2)) | |
1375 | snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_pcm_bits); | |
1376 | spin_unlock_irq(&chip->reg_lock); | |
1377 | return change; | |
1378 | } | |
1379 | ||
208a1b4c | 1380 | static struct snd_kcontrol_new snd_ymfpci_spdif_stream __devinitdata = |
1da177e4 LT |
1381 | { |
1382 | .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE, | |
1383 | .iface = SNDRV_CTL_ELEM_IFACE_PCM, | |
1384 | .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PCM_STREAM), | |
1385 | .info = snd_ymfpci_spdif_stream_info, | |
1386 | .get = snd_ymfpci_spdif_stream_get, | |
1387 | .put = snd_ymfpci_spdif_stream_put | |
1388 | }; | |
1389 | ||
208a1b4c | 1390 | static int snd_ymfpci_drec_source_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *info) |
1da177e4 LT |
1391 | { |
1392 | static char *texts[3] = {"AC'97", "IEC958", "ZV Port"}; | |
1393 | ||
1394 | info->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; | |
1395 | info->count = 1; | |
1396 | info->value.enumerated.items = 3; | |
1397 | if (info->value.enumerated.item > 2) | |
1398 | info->value.enumerated.item = 2; | |
1399 | strcpy(info->value.enumerated.name, texts[info->value.enumerated.item]); | |
1400 | return 0; | |
1401 | } | |
1402 | ||
208a1b4c | 1403 | static int snd_ymfpci_drec_source_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *value) |
1da177e4 | 1404 | { |
208a1b4c | 1405 | struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol); |
1da177e4 LT |
1406 | u16 reg; |
1407 | ||
1408 | spin_lock_irq(&chip->reg_lock); | |
1409 | reg = snd_ymfpci_readw(chip, YDSXGR_GLOBALCTRL); | |
1410 | spin_unlock_irq(&chip->reg_lock); | |
1411 | if (!(reg & 0x100)) | |
1412 | value->value.enumerated.item[0] = 0; | |
1413 | else | |
1414 | value->value.enumerated.item[0] = 1 + ((reg & 0x200) != 0); | |
1415 | return 0; | |
1416 | } | |
1417 | ||
208a1b4c | 1418 | static int snd_ymfpci_drec_source_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *value) |
1da177e4 | 1419 | { |
208a1b4c | 1420 | struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol); |
1da177e4 LT |
1421 | u16 reg, old_reg; |
1422 | ||
1423 | spin_lock_irq(&chip->reg_lock); | |
1424 | old_reg = snd_ymfpci_readw(chip, YDSXGR_GLOBALCTRL); | |
1425 | if (value->value.enumerated.item[0] == 0) | |
1426 | reg = old_reg & ~0x100; | |
1427 | else | |
1428 | reg = (old_reg & ~0x300) | 0x100 | ((value->value.enumerated.item[0] == 2) << 9); | |
1429 | snd_ymfpci_writew(chip, YDSXGR_GLOBALCTRL, reg); | |
1430 | spin_unlock_irq(&chip->reg_lock); | |
1431 | return reg != old_reg; | |
1432 | } | |
1433 | ||
208a1b4c | 1434 | static struct snd_kcontrol_new snd_ymfpci_drec_source __devinitdata = { |
1da177e4 LT |
1435 | .access = SNDRV_CTL_ELEM_ACCESS_READWRITE, |
1436 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, | |
1437 | .name = "Direct Recording Source", | |
1438 | .info = snd_ymfpci_drec_source_info, | |
1439 | .get = snd_ymfpci_drec_source_get, | |
1440 | .put = snd_ymfpci_drec_source_put | |
1441 | }; | |
1442 | ||
1443 | /* | |
1444 | * Mixer controls | |
1445 | */ | |
1446 | ||
d602c885 | 1447 | #define YMFPCI_SINGLE(xname, xindex, reg, shift) \ |
1da177e4 LT |
1448 | { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \ |
1449 | .info = snd_ymfpci_info_single, \ | |
1450 | .get = snd_ymfpci_get_single, .put = snd_ymfpci_put_single, \ | |
d602c885 | 1451 | .private_value = ((reg) | ((shift) << 16)) } |
1da177e4 | 1452 | |
a5ce8890 | 1453 | #define snd_ymfpci_info_single snd_ctl_boolean_mono_info |
1da177e4 | 1454 | |
208a1b4c TI |
1455 | static int snd_ymfpci_get_single(struct snd_kcontrol *kcontrol, |
1456 | struct snd_ctl_elem_value *ucontrol) | |
1da177e4 | 1457 | { |
208a1b4c | 1458 | struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol); |
d602c885 GM |
1459 | int reg = kcontrol->private_value & 0xffff; |
1460 | unsigned int shift = (kcontrol->private_value >> 16) & 0xff; | |
1461 | unsigned int mask = 1; | |
1da177e4 | 1462 | |
d602c885 | 1463 | switch (reg) { |
1da177e4 LT |
1464 | case YDSXGR_SPDIFOUTCTRL: break; |
1465 | case YDSXGR_SPDIFINCTRL: break; | |
1466 | default: return -EINVAL; | |
1467 | } | |
d602c885 GM |
1468 | ucontrol->value.integer.value[0] = |
1469 | (snd_ymfpci_readl(chip, reg) >> shift) & mask; | |
1da177e4 LT |
1470 | return 0; |
1471 | } | |
1472 | ||
208a1b4c TI |
1473 | static int snd_ymfpci_put_single(struct snd_kcontrol *kcontrol, |
1474 | struct snd_ctl_elem_value *ucontrol) | |
1da177e4 | 1475 | { |
208a1b4c | 1476 | struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol); |
d602c885 GM |
1477 | int reg = kcontrol->private_value & 0xffff; |
1478 | unsigned int shift = (kcontrol->private_value >> 16) & 0xff; | |
1479 | unsigned int mask = 1; | |
1da177e4 LT |
1480 | int change; |
1481 | unsigned int val, oval; | |
1482 | ||
d602c885 | 1483 | switch (reg) { |
1da177e4 LT |
1484 | case YDSXGR_SPDIFOUTCTRL: break; |
1485 | case YDSXGR_SPDIFINCTRL: break; | |
1486 | default: return -EINVAL; | |
1487 | } | |
1488 | val = (ucontrol->value.integer.value[0] & mask); | |
1da177e4 LT |
1489 | val <<= shift; |
1490 | spin_lock_irq(&chip->reg_lock); | |
1491 | oval = snd_ymfpci_readl(chip, reg); | |
1492 | val = (oval & ~(mask << shift)) | val; | |
1493 | change = val != oval; | |
1494 | snd_ymfpci_writel(chip, reg, val); | |
1495 | spin_unlock_irq(&chip->reg_lock); | |
1496 | return change; | |
1497 | } | |
1498 | ||
0cb29ea0 | 1499 | static const DECLARE_TLV_DB_LINEAR(db_scale_native, TLV_DB_GAIN_MUTE, 0); |
33925186 | 1500 | |
1da177e4 LT |
1501 | #define YMFPCI_DOUBLE(xname, xindex, reg) \ |
1502 | { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \ | |
33925186 | 1503 | .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ, \ |
1da177e4 LT |
1504 | .info = snd_ymfpci_info_double, \ |
1505 | .get = snd_ymfpci_get_double, .put = snd_ymfpci_put_double, \ | |
33925186 TI |
1506 | .private_value = reg, \ |
1507 | .tlv = { .p = db_scale_native } } | |
1da177e4 | 1508 | |
208a1b4c | 1509 | static int snd_ymfpci_info_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo) |
1da177e4 LT |
1510 | { |
1511 | unsigned int reg = kcontrol->private_value; | |
1da177e4 LT |
1512 | |
1513 | if (reg < 0x80 || reg >= 0xc0) | |
1514 | return -EINVAL; | |
467a8c2f | 1515 | uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; |
1da177e4 LT |
1516 | uinfo->count = 2; |
1517 | uinfo->value.integer.min = 0; | |
467a8c2f | 1518 | uinfo->value.integer.max = 16383; |
1da177e4 LT |
1519 | return 0; |
1520 | } | |
1521 | ||
208a1b4c | 1522 | static int snd_ymfpci_get_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) |
1da177e4 | 1523 | { |
208a1b4c | 1524 | struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol); |
1da177e4 | 1525 | unsigned int reg = kcontrol->private_value; |
467a8c2f | 1526 | unsigned int shift_left = 0, shift_right = 16, mask = 16383; |
1da177e4 LT |
1527 | unsigned int val; |
1528 | ||
1529 | if (reg < 0x80 || reg >= 0xc0) | |
1530 | return -EINVAL; | |
1531 | spin_lock_irq(&chip->reg_lock); | |
1532 | val = snd_ymfpci_readl(chip, reg); | |
1533 | spin_unlock_irq(&chip->reg_lock); | |
1534 | ucontrol->value.integer.value[0] = (val >> shift_left) & mask; | |
1535 | ucontrol->value.integer.value[1] = (val >> shift_right) & mask; | |
1da177e4 LT |
1536 | return 0; |
1537 | } | |
1538 | ||
208a1b4c | 1539 | static int snd_ymfpci_put_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) |
1da177e4 | 1540 | { |
208a1b4c | 1541 | struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol); |
1da177e4 | 1542 | unsigned int reg = kcontrol->private_value; |
467a8c2f | 1543 | unsigned int shift_left = 0, shift_right = 16, mask = 16383; |
1da177e4 LT |
1544 | int change; |
1545 | unsigned int val1, val2, oval; | |
1546 | ||
1547 | if (reg < 0x80 || reg >= 0xc0) | |
1548 | return -EINVAL; | |
1549 | val1 = ucontrol->value.integer.value[0] & mask; | |
1550 | val2 = ucontrol->value.integer.value[1] & mask; | |
1da177e4 LT |
1551 | val1 <<= shift_left; |
1552 | val2 <<= shift_right; | |
1553 | spin_lock_irq(&chip->reg_lock); | |
1554 | oval = snd_ymfpci_readl(chip, reg); | |
1555 | val1 = (oval & ~((mask << shift_left) | (mask << shift_right))) | val1 | val2; | |
1556 | change = val1 != oval; | |
1557 | snd_ymfpci_writel(chip, reg, val1); | |
1558 | spin_unlock_irq(&chip->reg_lock); | |
1559 | return change; | |
1560 | } | |
1561 | ||
177a7cdb CL |
1562 | static int snd_ymfpci_put_nativedacvol(struct snd_kcontrol *kcontrol, |
1563 | struct snd_ctl_elem_value *ucontrol) | |
1564 | { | |
1565 | struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol); | |
1566 | unsigned int reg = YDSXGR_NATIVEDACOUTVOL; | |
1567 | unsigned int reg2 = YDSXGR_BUF441OUTVOL; | |
1568 | int change; | |
1569 | unsigned int value, oval; | |
1570 | ||
1571 | value = ucontrol->value.integer.value[0] & 0x3fff; | |
1572 | value |= (ucontrol->value.integer.value[1] & 0x3fff) << 16; | |
1573 | spin_lock_irq(&chip->reg_lock); | |
1574 | oval = snd_ymfpci_readl(chip, reg); | |
1575 | change = value != oval; | |
1576 | snd_ymfpci_writel(chip, reg, value); | |
1577 | snd_ymfpci_writel(chip, reg2, value); | |
1578 | spin_unlock_irq(&chip->reg_lock); | |
1579 | return change; | |
1580 | } | |
1581 | ||
1da177e4 LT |
1582 | /* |
1583 | * 4ch duplication | |
1584 | */ | |
a5ce8890 | 1585 | #define snd_ymfpci_info_dup4ch snd_ctl_boolean_mono_info |
1da177e4 | 1586 | |
208a1b4c | 1587 | static int snd_ymfpci_get_dup4ch(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) |
1da177e4 | 1588 | { |
208a1b4c | 1589 | struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol); |
1da177e4 LT |
1590 | ucontrol->value.integer.value[0] = chip->mode_dup4ch; |
1591 | return 0; | |
1592 | } | |
1593 | ||
208a1b4c | 1594 | static int snd_ymfpci_put_dup4ch(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) |
1da177e4 | 1595 | { |
208a1b4c | 1596 | struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol); |
1da177e4 LT |
1597 | int change; |
1598 | change = (ucontrol->value.integer.value[0] != chip->mode_dup4ch); | |
1599 | if (change) | |
1600 | chip->mode_dup4ch = !!ucontrol->value.integer.value[0]; | |
1601 | return change; | |
1602 | } | |
1603 | ||
1604 | ||
208a1b4c | 1605 | static struct snd_kcontrol_new snd_ymfpci_controls[] __devinitdata = { |
177a7cdb CL |
1606 | { |
1607 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, | |
1608 | .name = "Wave Playback Volume", | |
1609 | .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | | |
1610 | SNDRV_CTL_ELEM_ACCESS_TLV_READ, | |
1611 | .info = snd_ymfpci_info_double, | |
1612 | .get = snd_ymfpci_get_double, | |
1613 | .put = snd_ymfpci_put_nativedacvol, | |
1614 | .private_value = YDSXGR_NATIVEDACOUTVOL, | |
1615 | .tlv = { .p = db_scale_native }, | |
1616 | }, | |
1da177e4 LT |
1617 | YMFPCI_DOUBLE("Wave Capture Volume", 0, YDSXGR_NATIVEDACLOOPVOL), |
1618 | YMFPCI_DOUBLE("Digital Capture Volume", 0, YDSXGR_NATIVEDACINVOL), | |
1619 | YMFPCI_DOUBLE("Digital Capture Volume", 1, YDSXGR_NATIVEADCINVOL), | |
1620 | YMFPCI_DOUBLE("ADC Playback Volume", 0, YDSXGR_PRIADCOUTVOL), | |
1621 | YMFPCI_DOUBLE("ADC Capture Volume", 0, YDSXGR_PRIADCLOOPVOL), | |
1622 | YMFPCI_DOUBLE("ADC Playback Volume", 1, YDSXGR_SECADCOUTVOL), | |
1623 | YMFPCI_DOUBLE("ADC Capture Volume", 1, YDSXGR_SECADCLOOPVOL), | |
1624 | YMFPCI_DOUBLE("FM Legacy Volume", 0, YDSXGR_LEGACYOUTVOL), | |
1625 | YMFPCI_DOUBLE(SNDRV_CTL_NAME_IEC958("AC97 ", PLAYBACK,VOLUME), 0, YDSXGR_ZVOUTVOL), | |
1626 | YMFPCI_DOUBLE(SNDRV_CTL_NAME_IEC958("", CAPTURE,VOLUME), 0, YDSXGR_ZVLOOPVOL), | |
1627 | YMFPCI_DOUBLE(SNDRV_CTL_NAME_IEC958("AC97 ",PLAYBACK,VOLUME), 1, YDSXGR_SPDIFOUTVOL), | |
1628 | YMFPCI_DOUBLE(SNDRV_CTL_NAME_IEC958("",CAPTURE,VOLUME), 1, YDSXGR_SPDIFLOOPVOL), | |
d602c885 GM |
1629 | YMFPCI_SINGLE(SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH), 0, YDSXGR_SPDIFOUTCTRL, 0), |
1630 | YMFPCI_SINGLE(SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH), 0, YDSXGR_SPDIFINCTRL, 0), | |
1631 | YMFPCI_SINGLE(SNDRV_CTL_NAME_IEC958("Loop",NONE,NONE), 0, YDSXGR_SPDIFINCTRL, 4), | |
1da177e4 LT |
1632 | { |
1633 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, | |
1634 | .name = "4ch Duplication", | |
1635 | .info = snd_ymfpci_info_dup4ch, | |
1636 | .get = snd_ymfpci_get_dup4ch, | |
1637 | .put = snd_ymfpci_put_dup4ch, | |
1638 | }, | |
1639 | }; | |
1640 | ||
1641 | ||
1642 | /* | |
1643 | * GPIO | |
1644 | */ | |
1645 | ||
208a1b4c | 1646 | static int snd_ymfpci_get_gpio_out(struct snd_ymfpci *chip, int pin) |
1da177e4 LT |
1647 | { |
1648 | u16 reg, mode; | |
1649 | unsigned long flags; | |
1650 | ||
1651 | spin_lock_irqsave(&chip->reg_lock, flags); | |
1652 | reg = snd_ymfpci_readw(chip, YDSXGR_GPIOFUNCENABLE); | |
1653 | reg &= ~(1 << (pin + 8)); | |
1654 | reg |= (1 << pin); | |
1655 | snd_ymfpci_writew(chip, YDSXGR_GPIOFUNCENABLE, reg); | |
1656 | /* set the level mode for input line */ | |
1657 | mode = snd_ymfpci_readw(chip, YDSXGR_GPIOTYPECONFIG); | |
1658 | mode &= ~(3 << (pin * 2)); | |
1659 | snd_ymfpci_writew(chip, YDSXGR_GPIOTYPECONFIG, mode); | |
1660 | snd_ymfpci_writew(chip, YDSXGR_GPIOFUNCENABLE, reg | (1 << (pin + 8))); | |
1661 | mode = snd_ymfpci_readw(chip, YDSXGR_GPIOINSTATUS); | |
1662 | spin_unlock_irqrestore(&chip->reg_lock, flags); | |
1663 | return (mode >> pin) & 1; | |
1664 | } | |
1665 | ||
208a1b4c | 1666 | static int snd_ymfpci_set_gpio_out(struct snd_ymfpci *chip, int pin, int enable) |
1da177e4 LT |
1667 | { |
1668 | u16 reg; | |
1669 | unsigned long flags; | |
1670 | ||
1671 | spin_lock_irqsave(&chip->reg_lock, flags); | |
1672 | reg = snd_ymfpci_readw(chip, YDSXGR_GPIOFUNCENABLE); | |
1673 | reg &= ~(1 << pin); | |
1674 | reg &= ~(1 << (pin + 8)); | |
1675 | snd_ymfpci_writew(chip, YDSXGR_GPIOFUNCENABLE, reg); | |
1676 | snd_ymfpci_writew(chip, YDSXGR_GPIOOUTCTRL, enable << pin); | |
1677 | snd_ymfpci_writew(chip, YDSXGR_GPIOFUNCENABLE, reg | (1 << (pin + 8))); | |
1678 | spin_unlock_irqrestore(&chip->reg_lock, flags); | |
1679 | ||
1680 | return 0; | |
1681 | } | |
1682 | ||
a5ce8890 | 1683 | #define snd_ymfpci_gpio_sw_info snd_ctl_boolean_mono_info |
1da177e4 | 1684 | |
208a1b4c | 1685 | static int snd_ymfpci_gpio_sw_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) |
1da177e4 | 1686 | { |
208a1b4c | 1687 | struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol); |
1da177e4 LT |
1688 | int pin = (int)kcontrol->private_value; |
1689 | ucontrol->value.integer.value[0] = snd_ymfpci_get_gpio_out(chip, pin); | |
1690 | return 0; | |
1691 | } | |
1692 | ||
208a1b4c | 1693 | static int snd_ymfpci_gpio_sw_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) |
1da177e4 | 1694 | { |
208a1b4c | 1695 | struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol); |
1da177e4 LT |
1696 | int pin = (int)kcontrol->private_value; |
1697 | ||
1698 | if (snd_ymfpci_get_gpio_out(chip, pin) != ucontrol->value.integer.value[0]) { | |
1699 | snd_ymfpci_set_gpio_out(chip, pin, !!ucontrol->value.integer.value[0]); | |
1700 | ucontrol->value.integer.value[0] = snd_ymfpci_get_gpio_out(chip, pin); | |
1701 | return 1; | |
1702 | } | |
1703 | return 0; | |
1704 | } | |
1705 | ||
208a1b4c | 1706 | static struct snd_kcontrol_new snd_ymfpci_rear_shared __devinitdata = { |
1da177e4 LT |
1707 | .name = "Shared Rear/Line-In Switch", |
1708 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, | |
1709 | .info = snd_ymfpci_gpio_sw_info, | |
1710 | .get = snd_ymfpci_gpio_sw_get, | |
1711 | .put = snd_ymfpci_gpio_sw_put, | |
1712 | .private_value = 2, | |
1713 | }; | |
1714 | ||
9bcf6551 CL |
1715 | /* |
1716 | * PCM voice volume | |
1717 | */ | |
1718 | ||
208a1b4c TI |
1719 | static int snd_ymfpci_pcm_vol_info(struct snd_kcontrol *kcontrol, |
1720 | struct snd_ctl_elem_info *uinfo) | |
9bcf6551 CL |
1721 | { |
1722 | uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; | |
1723 | uinfo->count = 2; | |
1724 | uinfo->value.integer.min = 0; | |
1725 | uinfo->value.integer.max = 0x8000; | |
1726 | return 0; | |
1727 | } | |
1728 | ||
208a1b4c TI |
1729 | static int snd_ymfpci_pcm_vol_get(struct snd_kcontrol *kcontrol, |
1730 | struct snd_ctl_elem_value *ucontrol) | |
9bcf6551 | 1731 | { |
208a1b4c | 1732 | struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol); |
9bcf6551 CL |
1733 | unsigned int subs = kcontrol->id.subdevice; |
1734 | ||
1735 | ucontrol->value.integer.value[0] = chip->pcm_mixer[subs].left; | |
1736 | ucontrol->value.integer.value[1] = chip->pcm_mixer[subs].right; | |
1737 | return 0; | |
1738 | } | |
1739 | ||
208a1b4c TI |
1740 | static int snd_ymfpci_pcm_vol_put(struct snd_kcontrol *kcontrol, |
1741 | struct snd_ctl_elem_value *ucontrol) | |
9bcf6551 | 1742 | { |
208a1b4c | 1743 | struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol); |
9bcf6551 | 1744 | unsigned int subs = kcontrol->id.subdevice; |
208a1b4c | 1745 | struct snd_pcm_substream *substream; |
9bcf6551 CL |
1746 | unsigned long flags; |
1747 | ||
1748 | if (ucontrol->value.integer.value[0] != chip->pcm_mixer[subs].left || | |
1749 | ucontrol->value.integer.value[1] != chip->pcm_mixer[subs].right) { | |
1750 | chip->pcm_mixer[subs].left = ucontrol->value.integer.value[0]; | |
1751 | chip->pcm_mixer[subs].right = ucontrol->value.integer.value[1]; | |
4e98d6a7 TI |
1752 | if (chip->pcm_mixer[subs].left > 0x8000) |
1753 | chip->pcm_mixer[subs].left = 0x8000; | |
1754 | if (chip->pcm_mixer[subs].right > 0x8000) | |
1755 | chip->pcm_mixer[subs].right = 0x8000; | |
9bcf6551 | 1756 | |
208a1b4c | 1757 | substream = (struct snd_pcm_substream *)kcontrol->private_value; |
9bcf6551 CL |
1758 | spin_lock_irqsave(&chip->voice_lock, flags); |
1759 | if (substream->runtime && substream->runtime->private_data) { | |
208a1b4c | 1760 | struct snd_ymfpci_pcm *ypcm = substream->runtime->private_data; |
9ed1261e TK |
1761 | if (!ypcm->use_441_slot) |
1762 | ypcm->update_pcm_vol = 2; | |
9bcf6551 CL |
1763 | } |
1764 | spin_unlock_irqrestore(&chip->voice_lock, flags); | |
1765 | return 1; | |
1766 | } | |
1767 | return 0; | |
1768 | } | |
1769 | ||
208a1b4c | 1770 | static struct snd_kcontrol_new snd_ymfpci_pcm_volume __devinitdata = { |
9bcf6551 CL |
1771 | .iface = SNDRV_CTL_ELEM_IFACE_PCM, |
1772 | .name = "PCM Playback Volume", | |
1773 | .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | | |
1774 | SNDRV_CTL_ELEM_ACCESS_INACTIVE, | |
1775 | .info = snd_ymfpci_pcm_vol_info, | |
1776 | .get = snd_ymfpci_pcm_vol_get, | |
1777 | .put = snd_ymfpci_pcm_vol_put, | |
1778 | }; | |
1779 | ||
1da177e4 LT |
1780 | |
1781 | /* | |
1782 | * Mixer routines | |
1783 | */ | |
1784 | ||
208a1b4c | 1785 | static void snd_ymfpci_mixer_free_ac97_bus(struct snd_ac97_bus *bus) |
1da177e4 | 1786 | { |
208a1b4c | 1787 | struct snd_ymfpci *chip = bus->private_data; |
1da177e4 LT |
1788 | chip->ac97_bus = NULL; |
1789 | } | |
1790 | ||
208a1b4c | 1791 | static void snd_ymfpci_mixer_free_ac97(struct snd_ac97 *ac97) |
1da177e4 | 1792 | { |
208a1b4c | 1793 | struct snd_ymfpci *chip = ac97->private_data; |
1da177e4 LT |
1794 | chip->ac97 = NULL; |
1795 | } | |
1796 | ||
d9301263 | 1797 | int __devinit snd_ymfpci_mixer(struct snd_ymfpci *chip, int rear_switch) |
1da177e4 | 1798 | { |
208a1b4c TI |
1799 | struct snd_ac97_template ac97; |
1800 | struct snd_kcontrol *kctl; | |
1801 | struct snd_pcm_substream *substream; | |
1da177e4 LT |
1802 | unsigned int idx; |
1803 | int err; | |
208a1b4c | 1804 | static struct snd_ac97_bus_ops ops = { |
1da177e4 LT |
1805 | .write = snd_ymfpci_codec_write, |
1806 | .read = snd_ymfpci_codec_read, | |
1807 | }; | |
1808 | ||
1809 | if ((err = snd_ac97_bus(chip->card, 0, &ops, chip, &chip->ac97_bus)) < 0) | |
1810 | return err; | |
1811 | chip->ac97_bus->private_free = snd_ymfpci_mixer_free_ac97_bus; | |
1812 | chip->ac97_bus->no_vra = 1; /* YMFPCI doesn't need VRA */ | |
1813 | ||
1814 | memset(&ac97, 0, sizeof(ac97)); | |
1815 | ac97.private_data = chip; | |
1816 | ac97.private_free = snd_ymfpci_mixer_free_ac97; | |
1817 | if ((err = snd_ac97_mixer(chip->ac97_bus, &ac97, &chip->ac97)) < 0) | |
1818 | return err; | |
1819 | ||
1820 | /* to be sure */ | |
1821 | snd_ac97_update_bits(chip->ac97, AC97_EXTENDED_STATUS, | |
1822 | AC97_EA_VRA|AC97_EA_VRM, 0); | |
1823 | ||
1824 | for (idx = 0; idx < ARRAY_SIZE(snd_ymfpci_controls); idx++) { | |
1825 | if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_ymfpci_controls[idx], chip))) < 0) | |
1826 | return err; | |
1827 | } | |
1828 | ||
1829 | /* add S/PDIF control */ | |
da3cec35 TI |
1830 | if (snd_BUG_ON(!chip->pcm_spdif)) |
1831 | return -ENXIO; | |
1da177e4 LT |
1832 | if ((err = snd_ctl_add(chip->card, kctl = snd_ctl_new1(&snd_ymfpci_spdif_default, chip))) < 0) |
1833 | return err; | |
1834 | kctl->id.device = chip->pcm_spdif->device; | |
1835 | if ((err = snd_ctl_add(chip->card, kctl = snd_ctl_new1(&snd_ymfpci_spdif_mask, chip))) < 0) | |
1836 | return err; | |
1837 | kctl->id.device = chip->pcm_spdif->device; | |
1838 | if ((err = snd_ctl_add(chip->card, kctl = snd_ctl_new1(&snd_ymfpci_spdif_stream, chip))) < 0) | |
1839 | return err; | |
1840 | kctl->id.device = chip->pcm_spdif->device; | |
1841 | chip->spdif_pcm_ctl = kctl; | |
1842 | ||
1843 | /* direct recording source */ | |
1844 | if (chip->device_id == PCI_DEVICE_ID_YAMAHA_754 && | |
1845 | (err = snd_ctl_add(chip->card, kctl = snd_ctl_new1(&snd_ymfpci_drec_source, chip))) < 0) | |
1846 | return err; | |
1847 | ||
1848 | /* | |
1849 | * shared rear/line-in | |
1850 | */ | |
1851 | if (rear_switch) { | |
1852 | if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_ymfpci_rear_shared, chip))) < 0) | |
1853 | return err; | |
1854 | } | |
1855 | ||
9bcf6551 CL |
1856 | /* per-voice volume */ |
1857 | substream = chip->pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream; | |
1858 | for (idx = 0; idx < 32; ++idx) { | |
1859 | kctl = snd_ctl_new1(&snd_ymfpci_pcm_volume, chip); | |
1860 | if (!kctl) | |
1861 | return -ENOMEM; | |
1862 | kctl->id.device = chip->pcm->device; | |
1863 | kctl->id.subdevice = idx; | |
1864 | kctl->private_value = (unsigned long)substream; | |
1865 | if ((err = snd_ctl_add(chip->card, kctl)) < 0) | |
1866 | return err; | |
1867 | chip->pcm_mixer[idx].left = 0x8000; | |
1868 | chip->pcm_mixer[idx].right = 0x8000; | |
1869 | chip->pcm_mixer[idx].ctl = kctl; | |
1870 | substream = substream->next; | |
1871 | } | |
1872 | ||
1da177e4 LT |
1873 | return 0; |
1874 | } | |
1875 | ||
1876 | ||
1877 | /* | |
1878 | * timer | |
1879 | */ | |
1880 | ||
208a1b4c | 1881 | static int snd_ymfpci_timer_start(struct snd_timer *timer) |
1da177e4 | 1882 | { |
208a1b4c | 1883 | struct snd_ymfpci *chip; |
1da177e4 LT |
1884 | unsigned long flags; |
1885 | unsigned int count; | |
1886 | ||
1887 | chip = snd_timer_chip(timer); | |
1da177e4 | 1888 | spin_lock_irqsave(&chip->reg_lock, flags); |
6e2efaac CL |
1889 | if (timer->sticks > 1) { |
1890 | chip->timer_ticks = timer->sticks; | |
1891 | count = timer->sticks - 1; | |
1892 | } else { | |
1893 | /* | |
1894 | * Divisor 1 is not allowed; fake it by using divisor 2 and | |
1895 | * counting two ticks for each interrupt. | |
1896 | */ | |
1897 | chip->timer_ticks = 2; | |
1898 | count = 2 - 1; | |
1899 | } | |
1da177e4 LT |
1900 | snd_ymfpci_writew(chip, YDSXGR_TIMERCOUNT, count); |
1901 | snd_ymfpci_writeb(chip, YDSXGR_TIMERCTRL, 0x03); | |
1902 | spin_unlock_irqrestore(&chip->reg_lock, flags); | |
1903 | return 0; | |
1904 | } | |
1905 | ||
208a1b4c | 1906 | static int snd_ymfpci_timer_stop(struct snd_timer *timer) |
1da177e4 | 1907 | { |
208a1b4c | 1908 | struct snd_ymfpci *chip; |
1da177e4 LT |
1909 | unsigned long flags; |
1910 | ||
1911 | chip = snd_timer_chip(timer); | |
1912 | spin_lock_irqsave(&chip->reg_lock, flags); | |
1913 | snd_ymfpci_writeb(chip, YDSXGR_TIMERCTRL, 0x00); | |
1914 | spin_unlock_irqrestore(&chip->reg_lock, flags); | |
1915 | return 0; | |
1916 | } | |
1917 | ||
208a1b4c | 1918 | static int snd_ymfpci_timer_precise_resolution(struct snd_timer *timer, |
1da177e4 LT |
1919 | unsigned long *num, unsigned long *den) |
1920 | { | |
1921 | *num = 1; | |
6e2efaac | 1922 | *den = 96000; |
1da177e4 LT |
1923 | return 0; |
1924 | } | |
1925 | ||
208a1b4c | 1926 | static struct snd_timer_hardware snd_ymfpci_timer_hw = { |
1da177e4 | 1927 | .flags = SNDRV_TIMER_HW_AUTO, |
6e2efaac CL |
1928 | .resolution = 10417, /* 1 / 96 kHz = 10.41666...us */ |
1929 | .ticks = 0x10000, | |
1da177e4 LT |
1930 | .start = snd_ymfpci_timer_start, |
1931 | .stop = snd_ymfpci_timer_stop, | |
1932 | .precise_resolution = snd_ymfpci_timer_precise_resolution, | |
1933 | }; | |
1934 | ||
208a1b4c | 1935 | int __devinit snd_ymfpci_timer(struct snd_ymfpci *chip, int device) |
1da177e4 | 1936 | { |
208a1b4c TI |
1937 | struct snd_timer *timer = NULL; |
1938 | struct snd_timer_id tid; | |
1da177e4 LT |
1939 | int err; |
1940 | ||
1941 | tid.dev_class = SNDRV_TIMER_CLASS_CARD; | |
1942 | tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE; | |
1943 | tid.card = chip->card->number; | |
1944 | tid.device = device; | |
1945 | tid.subdevice = 0; | |
1946 | if ((err = snd_timer_new(chip->card, "YMFPCI", &tid, &timer)) >= 0) { | |
1947 | strcpy(timer->name, "YMFPCI timer"); | |
1948 | timer->private_data = chip; | |
1949 | timer->hw = snd_ymfpci_timer_hw; | |
1950 | } | |
1951 | chip->timer = timer; | |
1952 | return err; | |
1953 | } | |
1954 | ||
1955 | ||
1956 | /* | |
1957 | * proc interface | |
1958 | */ | |
1959 | ||
208a1b4c TI |
1960 | static void snd_ymfpci_proc_read(struct snd_info_entry *entry, |
1961 | struct snd_info_buffer *buffer) | |
1da177e4 | 1962 | { |
208a1b4c | 1963 | struct snd_ymfpci *chip = entry->private_data; |
1da177e4 LT |
1964 | int i; |
1965 | ||
1966 | snd_iprintf(buffer, "YMFPCI\n\n"); | |
1967 | for (i = 0; i <= YDSXGR_WORKBASE; i += 4) | |
1968 | snd_iprintf(buffer, "%04x: %04x\n", i, snd_ymfpci_readl(chip, i)); | |
1969 | } | |
1970 | ||
208a1b4c | 1971 | static int __devinit snd_ymfpci_proc_init(struct snd_card *card, struct snd_ymfpci *chip) |
1da177e4 | 1972 | { |
208a1b4c | 1973 | struct snd_info_entry *entry; |
1da177e4 LT |
1974 | |
1975 | if (! snd_card_proc_new(card, "ymfpci", &entry)) | |
bf850204 | 1976 | snd_info_set_text_ops(entry, chip, snd_ymfpci_proc_read); |
1da177e4 LT |
1977 | return 0; |
1978 | } | |
1979 | ||
1980 | /* | |
1981 | * initialization routines | |
1982 | */ | |
1983 | ||
1984 | static void snd_ymfpci_aclink_reset(struct pci_dev * pci) | |
1985 | { | |
1986 | u8 cmd; | |
1987 | ||
1988 | pci_read_config_byte(pci, PCIR_DSXG_CTRL, &cmd); | |
1989 | #if 0 // force to reset | |
1990 | if (cmd & 0x03) { | |
1991 | #endif | |
1992 | pci_write_config_byte(pci, PCIR_DSXG_CTRL, cmd & 0xfc); | |
1993 | pci_write_config_byte(pci, PCIR_DSXG_CTRL, cmd | 0x03); | |
1994 | pci_write_config_byte(pci, PCIR_DSXG_CTRL, cmd & 0xfc); | |
1995 | pci_write_config_word(pci, PCIR_DSXG_PWRCTRL1, 0); | |
1996 | pci_write_config_word(pci, PCIR_DSXG_PWRCTRL2, 0); | |
1997 | #if 0 | |
1998 | } | |
1999 | #endif | |
2000 | } | |
2001 | ||
208a1b4c | 2002 | static void snd_ymfpci_enable_dsp(struct snd_ymfpci *chip) |
1da177e4 LT |
2003 | { |
2004 | snd_ymfpci_writel(chip, YDSXGR_CONFIG, 0x00000001); | |
2005 | } | |
2006 | ||
208a1b4c | 2007 | static void snd_ymfpci_disable_dsp(struct snd_ymfpci *chip) |
1da177e4 LT |
2008 | { |
2009 | u32 val; | |
2010 | int timeout = 1000; | |
2011 | ||
2012 | val = snd_ymfpci_readl(chip, YDSXGR_CONFIG); | |
2013 | if (val) | |
2014 | snd_ymfpci_writel(chip, YDSXGR_CONFIG, 0x00000000); | |
2015 | while (timeout-- > 0) { | |
2016 | val = snd_ymfpci_readl(chip, YDSXGR_STATUS); | |
2017 | if ((val & 0x00000002) == 0) | |
2018 | break; | |
2019 | } | |
2020 | } | |
2021 | ||
102fa906 CL |
2022 | static int snd_ymfpci_request_firmware(struct snd_ymfpci *chip) |
2023 | { | |
2024 | int err, is_1e; | |
2025 | const char *name; | |
2026 | ||
2027 | err = request_firmware(&chip->dsp_microcode, "yamaha/ds1_dsp.fw", | |
2028 | &chip->pci->dev); | |
2029 | if (err >= 0) { | |
b82a82d0 | 2030 | if (chip->dsp_microcode->size != YDSXG_DSPLENGTH) { |
102fa906 CL |
2031 | snd_printk(KERN_ERR "DSP microcode has wrong size\n"); |
2032 | err = -EINVAL; | |
2033 | } | |
2034 | } | |
b7dd2b34 | 2035 | if (err < 0) |
102fa906 | 2036 | return err; |
102fa906 CL |
2037 | is_1e = chip->device_id == PCI_DEVICE_ID_YAMAHA_724F || |
2038 | chip->device_id == PCI_DEVICE_ID_YAMAHA_740C || | |
2039 | chip->device_id == PCI_DEVICE_ID_YAMAHA_744 || | |
2040 | chip->device_id == PCI_DEVICE_ID_YAMAHA_754; | |
2041 | name = is_1e ? "yamaha/ds1e_ctrl.fw" : "yamaha/ds1_ctrl.fw"; | |
2042 | err = request_firmware(&chip->controller_microcode, name, | |
2043 | &chip->pci->dev); | |
2044 | if (err >= 0) { | |
b82a82d0 | 2045 | if (chip->controller_microcode->size != YDSXG_CTRLLENGTH) { |
102fa906 CL |
2046 | snd_printk(KERN_ERR "controller microcode" |
2047 | " has wrong size\n"); | |
2048 | err = -EINVAL; | |
2049 | } | |
2050 | } | |
b7dd2b34 | 2051 | if (err < 0) |
102fa906 | 2052 | return err; |
102fa906 CL |
2053 | return 0; |
2054 | } | |
7e0af29d CL |
2055 | |
2056 | MODULE_FIRMWARE("yamaha/ds1_dsp.fw"); | |
2057 | MODULE_FIRMWARE("yamaha/ds1_ctrl.fw"); | |
2058 | MODULE_FIRMWARE("yamaha/ds1e_ctrl.fw"); | |
2059 | ||
208a1b4c | 2060 | static void snd_ymfpci_download_image(struct snd_ymfpci *chip) |
1da177e4 LT |
2061 | { |
2062 | int i; | |
2063 | u16 ctrl; | |
b82a82d0 | 2064 | const __le32 *inst; |
1da177e4 LT |
2065 | |
2066 | snd_ymfpci_writel(chip, YDSXGR_NATIVEDACOUTVOL, 0x00000000); | |
2067 | snd_ymfpci_disable_dsp(chip); | |
2068 | snd_ymfpci_writel(chip, YDSXGR_MODE, 0x00010000); | |
2069 | snd_ymfpci_writel(chip, YDSXGR_MODE, 0x00000000); | |
2070 | snd_ymfpci_writel(chip, YDSXGR_MAPOFREC, 0x00000000); | |
2071 | snd_ymfpci_writel(chip, YDSXGR_MAPOFEFFECT, 0x00000000); | |
2072 | snd_ymfpci_writel(chip, YDSXGR_PLAYCTRLBASE, 0x00000000); | |
2073 | snd_ymfpci_writel(chip, YDSXGR_RECCTRLBASE, 0x00000000); | |
2074 | snd_ymfpci_writel(chip, YDSXGR_EFFCTRLBASE, 0x00000000); | |
2075 | ctrl = snd_ymfpci_readw(chip, YDSXGR_GLOBALCTRL); | |
2076 | snd_ymfpci_writew(chip, YDSXGR_GLOBALCTRL, ctrl & ~0x0007); | |
2077 | ||
2078 | /* setup DSP instruction code */ | |
b82a82d0 | 2079 | inst = (const __le32 *)chip->dsp_microcode->data; |
1da177e4 | 2080 | for (i = 0; i < YDSXG_DSPLENGTH / 4; i++) |
b82a82d0 DW |
2081 | snd_ymfpci_writel(chip, YDSXGR_DSPINSTRAM + (i << 2), |
2082 | le32_to_cpu(inst[i])); | |
1da177e4 LT |
2083 | |
2084 | /* setup control instruction code */ | |
b82a82d0 | 2085 | inst = (const __le32 *)chip->controller_microcode->data; |
1da177e4 | 2086 | for (i = 0; i < YDSXG_CTRLLENGTH / 4; i++) |
b82a82d0 DW |
2087 | snd_ymfpci_writel(chip, YDSXGR_CTRLINSTRAM + (i << 2), |
2088 | le32_to_cpu(inst[i])); | |
1da177e4 LT |
2089 | |
2090 | snd_ymfpci_enable_dsp(chip); | |
2091 | } | |
2092 | ||
208a1b4c | 2093 | static int __devinit snd_ymfpci_memalloc(struct snd_ymfpci *chip) |
1da177e4 LT |
2094 | { |
2095 | long size, playback_ctrl_size; | |
2096 | int voice, bank, reg; | |
2097 | u8 *ptr; | |
2098 | dma_addr_t ptr_addr; | |
2099 | ||
2100 | playback_ctrl_size = 4 + 4 * YDSXG_PLAYBACK_VOICES; | |
2101 | chip->bank_size_playback = snd_ymfpci_readl(chip, YDSXGR_PLAYCTRLSIZE) << 2; | |
2102 | chip->bank_size_capture = snd_ymfpci_readl(chip, YDSXGR_RECCTRLSIZE) << 2; | |
2103 | chip->bank_size_effect = snd_ymfpci_readl(chip, YDSXGR_EFFCTRLSIZE) << 2; | |
2104 | chip->work_size = YDSXG_DEFAULT_WORK_SIZE; | |
2105 | ||
7ab39926 CL |
2106 | size = ALIGN(playback_ctrl_size, 0x100) + |
2107 | ALIGN(chip->bank_size_playback * 2 * YDSXG_PLAYBACK_VOICES, 0x100) + | |
2108 | ALIGN(chip->bank_size_capture * 2 * YDSXG_CAPTURE_VOICES, 0x100) + | |
2109 | ALIGN(chip->bank_size_effect * 2 * YDSXG_EFFECT_VOICES, 0x100) + | |
1da177e4 LT |
2110 | chip->work_size; |
2111 | /* work_ptr must be aligned to 256 bytes, but it's already | |
2112 | covered with the kernel page allocation mechanism */ | |
2113 | if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(chip->pci), | |
2114 | size, &chip->work_ptr) < 0) | |
2115 | return -ENOMEM; | |
2116 | ptr = chip->work_ptr.area; | |
2117 | ptr_addr = chip->work_ptr.addr; | |
2118 | memset(ptr, 0, size); /* for sure */ | |
2119 | ||
2120 | chip->bank_base_playback = ptr; | |
2121 | chip->bank_base_playback_addr = ptr_addr; | |
2122 | chip->ctrl_playback = (u32 *)ptr; | |
2123 | chip->ctrl_playback[0] = cpu_to_le32(YDSXG_PLAYBACK_VOICES); | |
7ab39926 CL |
2124 | ptr += ALIGN(playback_ctrl_size, 0x100); |
2125 | ptr_addr += ALIGN(playback_ctrl_size, 0x100); | |
1da177e4 LT |
2126 | for (voice = 0; voice < YDSXG_PLAYBACK_VOICES; voice++) { |
2127 | chip->voices[voice].number = voice; | |
208a1b4c | 2128 | chip->voices[voice].bank = (struct snd_ymfpci_playback_bank *)ptr; |
1da177e4 LT |
2129 | chip->voices[voice].bank_addr = ptr_addr; |
2130 | for (bank = 0; bank < 2; bank++) { | |
208a1b4c | 2131 | chip->bank_playback[voice][bank] = (struct snd_ymfpci_playback_bank *)ptr; |
1da177e4 LT |
2132 | ptr += chip->bank_size_playback; |
2133 | ptr_addr += chip->bank_size_playback; | |
2134 | } | |
2135 | } | |
7ab39926 CL |
2136 | ptr = (char *)ALIGN((unsigned long)ptr, 0x100); |
2137 | ptr_addr = ALIGN(ptr_addr, 0x100); | |
1da177e4 LT |
2138 | chip->bank_base_capture = ptr; |
2139 | chip->bank_base_capture_addr = ptr_addr; | |
2140 | for (voice = 0; voice < YDSXG_CAPTURE_VOICES; voice++) | |
2141 | for (bank = 0; bank < 2; bank++) { | |
208a1b4c | 2142 | chip->bank_capture[voice][bank] = (struct snd_ymfpci_capture_bank *)ptr; |
1da177e4 LT |
2143 | ptr += chip->bank_size_capture; |
2144 | ptr_addr += chip->bank_size_capture; | |
2145 | } | |
7ab39926 CL |
2146 | ptr = (char *)ALIGN((unsigned long)ptr, 0x100); |
2147 | ptr_addr = ALIGN(ptr_addr, 0x100); | |
1da177e4 LT |
2148 | chip->bank_base_effect = ptr; |
2149 | chip->bank_base_effect_addr = ptr_addr; | |
2150 | for (voice = 0; voice < YDSXG_EFFECT_VOICES; voice++) | |
2151 | for (bank = 0; bank < 2; bank++) { | |
208a1b4c | 2152 | chip->bank_effect[voice][bank] = (struct snd_ymfpci_effect_bank *)ptr; |
1da177e4 LT |
2153 | ptr += chip->bank_size_effect; |
2154 | ptr_addr += chip->bank_size_effect; | |
2155 | } | |
7ab39926 CL |
2156 | ptr = (char *)ALIGN((unsigned long)ptr, 0x100); |
2157 | ptr_addr = ALIGN(ptr_addr, 0x100); | |
1da177e4 LT |
2158 | chip->work_base = ptr; |
2159 | chip->work_base_addr = ptr_addr; | |
2160 | ||
da3cec35 TI |
2161 | snd_BUG_ON(ptr + chip->work_size != |
2162 | chip->work_ptr.area + chip->work_ptr.bytes); | |
1da177e4 LT |
2163 | |
2164 | snd_ymfpci_writel(chip, YDSXGR_PLAYCTRLBASE, chip->bank_base_playback_addr); | |
2165 | snd_ymfpci_writel(chip, YDSXGR_RECCTRLBASE, chip->bank_base_capture_addr); | |
2166 | snd_ymfpci_writel(chip, YDSXGR_EFFCTRLBASE, chip->bank_base_effect_addr); | |
2167 | snd_ymfpci_writel(chip, YDSXGR_WORKBASE, chip->work_base_addr); | |
2168 | snd_ymfpci_writel(chip, YDSXGR_WORKSIZE, chip->work_size >> 2); | |
2169 | ||
2170 | /* S/PDIF output initialization */ | |
2171 | chip->spdif_bits = chip->spdif_pcm_bits = SNDRV_PCM_DEFAULT_CON_SPDIF & 0xffff; | |
2172 | snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTCTRL, 0); | |
2173 | snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_bits); | |
2174 | ||
2175 | /* S/PDIF input initialization */ | |
2176 | snd_ymfpci_writew(chip, YDSXGR_SPDIFINCTRL, 0); | |
2177 | ||
2178 | /* digital mixer setup */ | |
2179 | for (reg = 0x80; reg < 0xc0; reg += 4) | |
2180 | snd_ymfpci_writel(chip, reg, 0); | |
2181 | snd_ymfpci_writel(chip, YDSXGR_NATIVEDACOUTVOL, 0x3fff3fff); | |
4a3b6983 | 2182 | snd_ymfpci_writel(chip, YDSXGR_BUF441OUTVOL, 0x3fff3fff); |
1da177e4 LT |
2183 | snd_ymfpci_writel(chip, YDSXGR_ZVOUTVOL, 0x3fff3fff); |
2184 | snd_ymfpci_writel(chip, YDSXGR_SPDIFOUTVOL, 0x3fff3fff); | |
2185 | snd_ymfpci_writel(chip, YDSXGR_NATIVEADCINVOL, 0x3fff3fff); | |
2186 | snd_ymfpci_writel(chip, YDSXGR_NATIVEDACINVOL, 0x3fff3fff); | |
2187 | snd_ymfpci_writel(chip, YDSXGR_PRIADCLOOPVOL, 0x3fff3fff); | |
2188 | snd_ymfpci_writel(chip, YDSXGR_LEGACYOUTVOL, 0x3fff3fff); | |
2189 | ||
2190 | return 0; | |
2191 | } | |
2192 | ||
208a1b4c | 2193 | static int snd_ymfpci_free(struct snd_ymfpci *chip) |
1da177e4 LT |
2194 | { |
2195 | u16 ctrl; | |
2196 | ||
da3cec35 TI |
2197 | if (snd_BUG_ON(!chip)) |
2198 | return -EINVAL; | |
1da177e4 LT |
2199 | |
2200 | if (chip->res_reg_area) { /* don't touch busy hardware */ | |
2201 | snd_ymfpci_writel(chip, YDSXGR_NATIVEDACOUTVOL, 0); | |
2202 | snd_ymfpci_writel(chip, YDSXGR_BUF441OUTVOL, 0); | |
2203 | snd_ymfpci_writel(chip, YDSXGR_LEGACYOUTVOL, 0); | |
2204 | snd_ymfpci_writel(chip, YDSXGR_STATUS, ~0); | |
2205 | snd_ymfpci_disable_dsp(chip); | |
2206 | snd_ymfpci_writel(chip, YDSXGR_PLAYCTRLBASE, 0); | |
2207 | snd_ymfpci_writel(chip, YDSXGR_RECCTRLBASE, 0); | |
2208 | snd_ymfpci_writel(chip, YDSXGR_EFFCTRLBASE, 0); | |
2209 | snd_ymfpci_writel(chip, YDSXGR_WORKBASE, 0); | |
2210 | snd_ymfpci_writel(chip, YDSXGR_WORKSIZE, 0); | |
2211 | ctrl = snd_ymfpci_readw(chip, YDSXGR_GLOBALCTRL); | |
2212 | snd_ymfpci_writew(chip, YDSXGR_GLOBALCTRL, ctrl & ~0x0007); | |
2213 | } | |
2214 | ||
2215 | snd_ymfpci_ac3_done(chip); | |
2216 | ||
2217 | /* Set PCI device to D3 state */ | |
2218 | #if 0 | |
2219 | /* FIXME: temporarily disabled, otherwise we cannot fire up | |
2220 | * the chip again unless reboot. ACPI bug? | |
2221 | */ | |
2222 | pci_set_power_state(chip->pci, 3); | |
2223 | #endif | |
2224 | ||
2225 | #ifdef CONFIG_PM | |
2226 | vfree(chip->saved_regs); | |
2227 | #endif | |
95866d38 TI |
2228 | if (chip->irq >= 0) |
2229 | free_irq(chip->irq, chip); | |
b1d5776d TI |
2230 | release_and_free_resource(chip->mpu_res); |
2231 | release_and_free_resource(chip->fm_res); | |
1da177e4 LT |
2232 | snd_ymfpci_free_gameport(chip); |
2233 | if (chip->reg_area_virt) | |
2234 | iounmap(chip->reg_area_virt); | |
2235 | if (chip->work_ptr.area) | |
2236 | snd_dma_free_pages(&chip->work_ptr); | |
2237 | ||
b1d5776d | 2238 | release_and_free_resource(chip->res_reg_area); |
1da177e4 LT |
2239 | |
2240 | pci_write_config_word(chip->pci, 0x40, chip->old_legacy_ctrl); | |
2241 | ||
2242 | pci_disable_device(chip->pci); | |
b7dd2b34 TI |
2243 | release_firmware(chip->dsp_microcode); |
2244 | release_firmware(chip->controller_microcode); | |
1da177e4 LT |
2245 | kfree(chip); |
2246 | return 0; | |
2247 | } | |
2248 | ||
208a1b4c | 2249 | static int snd_ymfpci_dev_free(struct snd_device *device) |
1da177e4 | 2250 | { |
208a1b4c | 2251 | struct snd_ymfpci *chip = device->device_data; |
1da177e4 LT |
2252 | return snd_ymfpci_free(chip); |
2253 | } | |
2254 | ||
2255 | #ifdef CONFIG_PM | |
2256 | static int saved_regs_index[] = { | |
2257 | /* spdif */ | |
2258 | YDSXGR_SPDIFOUTCTRL, | |
2259 | YDSXGR_SPDIFOUTSTATUS, | |
2260 | YDSXGR_SPDIFINCTRL, | |
2261 | /* volumes */ | |
2262 | YDSXGR_PRIADCLOOPVOL, | |
2263 | YDSXGR_NATIVEDACINVOL, | |
2264 | YDSXGR_NATIVEDACOUTVOL, | |
9ed1261e | 2265 | YDSXGR_BUF441OUTVOL, |
1da177e4 LT |
2266 | YDSXGR_NATIVEADCINVOL, |
2267 | YDSXGR_SPDIFLOOPVOL, | |
2268 | YDSXGR_SPDIFOUTVOL, | |
2269 | YDSXGR_ZVOUTVOL, | |
2270 | YDSXGR_LEGACYOUTVOL, | |
2271 | /* address bases */ | |
2272 | YDSXGR_PLAYCTRLBASE, | |
2273 | YDSXGR_RECCTRLBASE, | |
2274 | YDSXGR_EFFCTRLBASE, | |
2275 | YDSXGR_WORKBASE, | |
2276 | /* capture set up */ | |
2277 | YDSXGR_MAPOFREC, | |
2278 | YDSXGR_RECFORMAT, | |
2279 | YDSXGR_RECSLOTSR, | |
2280 | YDSXGR_ADCFORMAT, | |
2281 | YDSXGR_ADCSLOTSR, | |
2282 | }; | |
2283 | #define YDSXGR_NUM_SAVED_REGS ARRAY_SIZE(saved_regs_index) | |
2284 | ||
ded46235 | 2285 | int snd_ymfpci_suspend(struct pci_dev *pci, pm_message_t state) |
1da177e4 | 2286 | { |
ded46235 TI |
2287 | struct snd_card *card = pci_get_drvdata(pci); |
2288 | struct snd_ymfpci *chip = card->private_data; | |
1da177e4 LT |
2289 | unsigned int i; |
2290 | ||
ded46235 | 2291 | snd_power_change_state(card, SNDRV_CTL_POWER_D3hot); |
1da177e4 LT |
2292 | snd_pcm_suspend_all(chip->pcm); |
2293 | snd_pcm_suspend_all(chip->pcm2); | |
2294 | snd_pcm_suspend_all(chip->pcm_spdif); | |
2295 | snd_pcm_suspend_all(chip->pcm_4ch); | |
2296 | snd_ac97_suspend(chip->ac97); | |
2297 | for (i = 0; i < YDSXGR_NUM_SAVED_REGS; i++) | |
2298 | chip->saved_regs[i] = snd_ymfpci_readl(chip, saved_regs_index[i]); | |
2299 | chip->saved_ydsxgr_mode = snd_ymfpci_readl(chip, YDSXGR_MODE); | |
2300 | snd_ymfpci_writel(chip, YDSXGR_NATIVEDACOUTVOL, 0); | |
4a3b6983 | 2301 | snd_ymfpci_writel(chip, YDSXGR_BUF441OUTVOL, 0); |
1da177e4 | 2302 | snd_ymfpci_disable_dsp(chip); |
ded46235 TI |
2303 | pci_disable_device(pci); |
2304 | pci_save_state(pci); | |
30b35399 | 2305 | pci_set_power_state(pci, pci_choose_state(pci, state)); |
1da177e4 LT |
2306 | return 0; |
2307 | } | |
2308 | ||
ded46235 | 2309 | int snd_ymfpci_resume(struct pci_dev *pci) |
1da177e4 | 2310 | { |
ded46235 TI |
2311 | struct snd_card *card = pci_get_drvdata(pci); |
2312 | struct snd_ymfpci *chip = card->private_data; | |
1da177e4 LT |
2313 | unsigned int i; |
2314 | ||
30b35399 | 2315 | pci_set_power_state(pci, PCI_D0); |
ded46235 | 2316 | pci_restore_state(pci); |
30b35399 TI |
2317 | if (pci_enable_device(pci) < 0) { |
2318 | printk(KERN_ERR "ymfpci: pci_enable_device failed, " | |
2319 | "disabling device\n"); | |
2320 | snd_card_disconnect(card); | |
2321 | return -EIO; | |
2322 | } | |
ded46235 TI |
2323 | pci_set_master(pci); |
2324 | snd_ymfpci_aclink_reset(pci); | |
1da177e4 LT |
2325 | snd_ymfpci_codec_ready(chip, 0); |
2326 | snd_ymfpci_download_image(chip); | |
2327 | udelay(100); | |
2328 | ||
2329 | for (i = 0; i < YDSXGR_NUM_SAVED_REGS; i++) | |
2330 | snd_ymfpci_writel(chip, saved_regs_index[i], chip->saved_regs[i]); | |
2331 | ||
2332 | snd_ac97_resume(chip->ac97); | |
2333 | ||
2334 | /* start hw again */ | |
2335 | if (chip->start_count > 0) { | |
2336 | spin_lock_irq(&chip->reg_lock); | |
2337 | snd_ymfpci_writel(chip, YDSXGR_MODE, chip->saved_ydsxgr_mode); | |
2338 | chip->active_bank = snd_ymfpci_readl(chip, YDSXGR_CTRLSELECT); | |
2339 | spin_unlock_irq(&chip->reg_lock); | |
2340 | } | |
ded46235 | 2341 | snd_power_change_state(card, SNDRV_CTL_POWER_D0); |
1da177e4 LT |
2342 | return 0; |
2343 | } | |
2344 | #endif /* CONFIG_PM */ | |
2345 | ||
208a1b4c | 2346 | int __devinit snd_ymfpci_create(struct snd_card *card, |
1da177e4 LT |
2347 | struct pci_dev * pci, |
2348 | unsigned short old_legacy_ctrl, | |
208a1b4c | 2349 | struct snd_ymfpci ** rchip) |
1da177e4 | 2350 | { |
208a1b4c | 2351 | struct snd_ymfpci *chip; |
1da177e4 | 2352 | int err; |
208a1b4c | 2353 | static struct snd_device_ops ops = { |
1da177e4 LT |
2354 | .dev_free = snd_ymfpci_dev_free, |
2355 | }; | |
2356 | ||
2357 | *rchip = NULL; | |
2358 | ||
2359 | /* enable PCI device */ | |
2360 | if ((err = pci_enable_device(pci)) < 0) | |
2361 | return err; | |
2362 | ||
e560d8d8 | 2363 | chip = kzalloc(sizeof(*chip), GFP_KERNEL); |
1da177e4 LT |
2364 | if (chip == NULL) { |
2365 | pci_disable_device(pci); | |
2366 | return -ENOMEM; | |
2367 | } | |
2368 | chip->old_legacy_ctrl = old_legacy_ctrl; | |
2369 | spin_lock_init(&chip->reg_lock); | |
2370 | spin_lock_init(&chip->voice_lock); | |
2371 | init_waitqueue_head(&chip->interrupt_sleep); | |
2372 | atomic_set(&chip->interrupt_sleep_count, 0); | |
2373 | chip->card = card; | |
2374 | chip->pci = pci; | |
2375 | chip->irq = -1; | |
2376 | chip->device_id = pci->device; | |
44c10138 | 2377 | chip->rev = pci->revision; |
1da177e4 LT |
2378 | chip->reg_area_phys = pci_resource_start(pci, 0); |
2379 | chip->reg_area_virt = ioremap_nocache(chip->reg_area_phys, 0x8000); | |
2380 | pci_set_master(pci); | |
9ed1261e | 2381 | chip->src441_used = -1; |
1da177e4 LT |
2382 | |
2383 | if ((chip->res_reg_area = request_mem_region(chip->reg_area_phys, 0x8000, "YMFPCI")) == NULL) { | |
99b359ba | 2384 | snd_printk(KERN_ERR "unable to grab memory region 0x%lx-0x%lx\n", chip->reg_area_phys, chip->reg_area_phys + 0x8000 - 1); |
1da177e4 LT |
2385 | snd_ymfpci_free(chip); |
2386 | return -EBUSY; | |
2387 | } | |
437a5a46 TI |
2388 | if (request_irq(pci->irq, snd_ymfpci_interrupt, IRQF_SHARED, |
2389 | "YMFPCI", chip)) { | |
99b359ba | 2390 | snd_printk(KERN_ERR "unable to grab IRQ %d\n", pci->irq); |
1da177e4 LT |
2391 | snd_ymfpci_free(chip); |
2392 | return -EBUSY; | |
2393 | } | |
2394 | chip->irq = pci->irq; | |
2395 | ||
2396 | snd_ymfpci_aclink_reset(pci); | |
2397 | if (snd_ymfpci_codec_ready(chip, 0) < 0) { | |
2398 | snd_ymfpci_free(chip); | |
2399 | return -EIO; | |
2400 | } | |
2401 | ||
102fa906 CL |
2402 | err = snd_ymfpci_request_firmware(chip); |
2403 | if (err < 0) { | |
2404 | snd_printk(KERN_ERR "firmware request failed: %d\n", err); | |
2405 | snd_ymfpci_free(chip); | |
2406 | return err; | |
2407 | } | |
1da177e4 LT |
2408 | snd_ymfpci_download_image(chip); |
2409 | ||
2410 | udelay(100); /* seems we need a delay after downloading image.. */ | |
2411 | ||
2412 | if (snd_ymfpci_memalloc(chip) < 0) { | |
2413 | snd_ymfpci_free(chip); | |
2414 | return -EIO; | |
2415 | } | |
2416 | ||
2417 | if ((err = snd_ymfpci_ac3_init(chip)) < 0) { | |
2418 | snd_ymfpci_free(chip); | |
2419 | return err; | |
2420 | } | |
2421 | ||
2422 | #ifdef CONFIG_PM | |
2423 | chip->saved_regs = vmalloc(YDSXGR_NUM_SAVED_REGS * sizeof(u32)); | |
2424 | if (chip->saved_regs == NULL) { | |
2425 | snd_ymfpci_free(chip); | |
2426 | return -ENOMEM; | |
2427 | } | |
1da177e4 LT |
2428 | #endif |
2429 | ||
2430 | if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) { | |
2431 | snd_ymfpci_free(chip); | |
2432 | return err; | |
2433 | } | |
2434 | ||
2435 | snd_ymfpci_proc_init(card, chip); | |
2436 | ||
2437 | snd_card_set_dev(card, &pci->dev); | |
2438 | ||
2439 | *rchip = chip; | |
2440 | return 0; | |
2441 | } |