Linux-2.6.12-rc2
[linux-flexiantxendom0-natty.git] / sound / pci / emu10k1 / p16v.c
1 /*
2  *  Copyright (c) by James Courtier-Dutton <James@superbug.demon.co.uk>
3  *  Driver p16v chips
4  *  Version: 0.22
5  *
6  *  FEATURES currently supported:
7  *    Output fixed at S32_LE, 2 channel to hw:0,0
8  *    Rates: 44.1, 48, 96, 192.
9  *
10  *  Changelog:
11  *  0.8
12  *    Use separate card based buffer for periods table.
13  *  0.9
14  *    Use 2 channel output streams instead of 8 channel.
15  *       (8 channel output streams might be good for ASIO type output)
16  *    Corrected speaker output, so Front -> Front etc.
17  *  0.10
18  *    Fixed missed interrupts.
19  *  0.11
20  *    Add Sound card model number and names.
21  *    Add Analog volume controls.
22  *  0.12
23  *    Corrected playback interrupts. Now interrupt per period, instead of half period.
24  *  0.13
25  *    Use single trigger for multichannel.
26  *  0.14
27  *    Mic capture now works at fixed: S32_LE, 96000Hz, Stereo.
28  *  0.15
29  *    Force buffer_size / period_size == INTEGER.
30  *  0.16
31  *    Update p16v.c to work with changed alsa api.
32  *  0.17
33  *    Update p16v.c to work with changed alsa api. Removed boot_devs.
34  *  0.18
35  *    Merging with snd-emu10k1 driver.
36  *  0.19
37  *    One stereo channel at 24bit now works.
38  *  0.20
39  *    Added better register defines.
40  *  0.21
41  *    Integrated with snd-emu10k1 driver.
42  *  0.22
43  *    Removed #if 0 ... #endif
44  *
45  *
46  *  BUGS:
47  *    Some stability problems when unloading the snd-p16v kernel module.
48  *    --
49  *
50  *  TODO:
51  *    SPDIF out.
52  *    Find out how to change capture sample rates. E.g. To record SPDIF at 48000Hz.
53  *    Currently capture fixed at 48000Hz.
54  *
55  *    --
56  *  GENERAL INFO:
57  *    Model: SB0240
58  *    P16V Chip: CA0151-DBS
59  *    Audigy 2 Chip: CA0102-IAT
60  *    AC97 Codec: STAC 9721
61  *    ADC: Philips 1361T (Stereo 24bit)
62  *    DAC: CS4382-K (8-channel, 24bit, 192Khz)
63  *
64  *  This code was initally based on code from ALSA's emu10k1x.c which is:
65  *  Copyright (c) by Francisco Moraes <fmoraes@nc.rr.com>
66  *
67  *   This program is free software; you can redistribute it and/or modify
68  *   it under the terms of the GNU General Public License as published by
69  *   the Free Software Foundation; either version 2 of the License, or
70  *   (at your option) any later version.
71  *
72  *   This program is distributed in the hope that it will be useful,
73  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
74  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
75  *   GNU General Public License for more details.
76  *
77  *   You should have received a copy of the GNU General Public License
78  *   along with this program; if not, write to the Free Software
79  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
80  *
81  */
82 #include <sound/driver.h>
83 #include <linux/delay.h>
84 #include <linux/init.h>
85 #include <linux/interrupt.h>
86 #include <linux/pci.h>
87 #include <linux/slab.h>
88 #include <linux/moduleparam.h>
89 #include <sound/core.h>
90 #include <sound/initval.h>
91 #include <sound/pcm.h>
92 #include <sound/ac97_codec.h>
93 #include <sound/info.h>
94 #include <sound/emu10k1.h>
95 #include "p16v.h"
96
97 #define SET_CHANNEL 0  /* Testing channel outputs 0=Front, 1=Center/LFE, 2=Unknown, 3=Rear */
98 #define PCM_FRONT_CHANNEL 0
99 #define PCM_REAR_CHANNEL 1
100 #define PCM_CENTER_LFE_CHANNEL 2
101 #define PCM_UNKNOWN_CHANNEL 3
102 #define CONTROL_FRONT_CHANNEL 0
103 #define CONTROL_REAR_CHANNEL 3
104 #define CONTROL_CENTER_LFE_CHANNEL 1
105 #define CONTROL_UNKNOWN_CHANNEL 2
106
107 /* Card IDs:
108  * Class 0401: 1102:0004 (rev 04) Subsystem: 1102:2002 -> Audigy2 ZS 7.1 Model:SB0350
109  * Class 0401: 1102:0004 (rev 04) Subsystem: 1102:1007 -> Audigy2 6.1    Model:SB0240
110  * Class 0401: 1102:0004 (rev 04) Subsystem: 1102:1002 -> Audigy2 Platinum  Model:SB msb0240230009266
111  * Class 0401: 1102:0004 (rev 04) Subsystem: 1102:2007 -> Audigy4 Pro Model:SB0380 M1SB0380472001901E
112  *
113  */
114
115  /* hardware definition */
116 static snd_pcm_hardware_t snd_p16v_playback_hw = {
117         .info =                 (SNDRV_PCM_INFO_MMAP | 
118                                  SNDRV_PCM_INFO_INTERLEAVED |
119                                  SNDRV_PCM_INFO_BLOCK_TRANSFER |
120                                  SNDRV_PCM_INFO_MMAP_VALID),
121         .formats =              SNDRV_PCM_FMTBIT_S32_LE, /* Only supports 24-bit samples padded to 32 bits. */
122         .rates =                SNDRV_PCM_RATE_192000 | SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_48000 ,
123         .rate_min =             48000,
124         .rate_max =             192000,
125         .channels_min =         8, 
126         .channels_max =         8,
127         .buffer_bytes_max =     (32*1024),
128         .period_bytes_min =     64,
129         .period_bytes_max =     (16*1024),
130         .periods_min =          2,
131         .periods_max =          8,
132         .fifo_size =            0,
133 };
134
135 static void snd_p16v_pcm_free_substream(snd_pcm_runtime_t *runtime)
136 {
137         snd_pcm_t *epcm = runtime->private_data;
138   
139         if (epcm) {
140                 //snd_printk("epcm free: %p\n", epcm);
141                 kfree(epcm);
142         }
143 }
144
145 /* open_playback callback */
146 static int snd_p16v_pcm_open_playback_channel(snd_pcm_substream_t *substream, int channel_id)
147 {
148         emu10k1_t *emu = snd_pcm_substream_chip(substream);
149         emu10k1_voice_t *channel = &(emu->p16v_voices[channel_id]);
150         emu10k1_pcm_t *epcm;
151         snd_pcm_runtime_t *runtime = substream->runtime;
152         int err;
153
154         epcm = kcalloc(1, sizeof(*epcm), GFP_KERNEL);
155         //snd_printk("epcm kcalloc: %p\n", epcm);
156
157         if (epcm == NULL)
158                 return -ENOMEM;
159         epcm->emu = emu;
160         epcm->substream = substream;
161         //snd_printk("epcm device=%d, channel_id=%d\n", substream->pcm->device, channel_id);
162   
163         runtime->private_data = epcm;
164         runtime->private_free = snd_p16v_pcm_free_substream;
165   
166         runtime->hw = snd_p16v_playback_hw;
167
168         channel->emu = emu;
169         channel->number = channel_id;
170
171         channel->use=1;
172         //snd_printk("p16v: open channel_id=%d, channel=%p, use=0x%x\n", channel_id, channel, channel->use);
173         //printk("open:channel_id=%d, chip=%p, channel=%p\n",channel_id, chip, channel);
174         //channel->interrupt = snd_p16v_pcm_channel_interrupt;
175         channel->epcm=epcm;
176         if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
177                 return err;
178
179         return 0;
180 }
181
182 /* close callback */
183 static int snd_p16v_pcm_close_playback(snd_pcm_substream_t *substream)
184 {
185         emu10k1_t *emu = snd_pcm_substream_chip(substream);
186         //snd_pcm_runtime_t *runtime = substream->runtime;
187         //emu10k1_pcm_t *epcm = runtime->private_data;
188         emu->p16v_voices[substream->pcm->device - emu->p16v_device_offset].use=0;
189 /* FIXME: maybe zero others */
190         return 0;
191 }
192
193 static int snd_p16v_pcm_open_playback_front(snd_pcm_substream_t *substream)
194 {
195         return snd_p16v_pcm_open_playback_channel(substream, PCM_FRONT_CHANNEL);
196 }
197
198 /* hw_params callback */
199 static int snd_p16v_pcm_hw_params_playback(snd_pcm_substream_t *substream,
200                                       snd_pcm_hw_params_t * hw_params)
201 {
202         int result;
203         //snd_printk("hw_params alloc: substream=%p\n", substream);
204         result = snd_pcm_lib_malloc_pages(substream,
205                                         params_buffer_bytes(hw_params));
206         //snd_printk("hw_params alloc: result=%d\n", result);
207         //dump_stack();
208         return result;
209 }
210
211 /* hw_free callback */
212 static int snd_p16v_pcm_hw_free_playback(snd_pcm_substream_t *substream)
213 {
214         int result;
215         //snd_printk("hw_params free: substream=%p\n", substream);
216         result = snd_pcm_lib_free_pages(substream);
217         //snd_printk("hw_params free: result=%d\n", result);
218         //dump_stack();
219         return result;
220 }
221
222 /* prepare playback callback */
223 static int snd_p16v_pcm_prepare_playback(snd_pcm_substream_t *substream)
224 {
225         emu10k1_t *emu = snd_pcm_substream_chip(substream);
226         snd_pcm_runtime_t *runtime = substream->runtime;
227         //emu10k1_pcm_t *epcm = runtime->private_data;
228         int channel = substream->pcm->device - emu->p16v_device_offset;
229         u32 *table_base = (u32 *)(emu->p16v_buffer.area+(8*16*channel));
230         u32 period_size_bytes = frames_to_bytes(runtime, runtime->period_size);
231         int i;
232         u32 tmp;
233         
234         //snd_printk("prepare:channel_number=%d, rate=%d, format=0x%x, channels=%d, buffer_size=%ld, period_size=%ld, periods=%u, frames_to_bytes=%d\n",channel, runtime->rate, runtime->format, runtime->channels, runtime->buffer_size, runtime->period_size, runtime->periods, frames_to_bytes(runtime, 1));
235         //snd_printk("dma_addr=%x, dma_area=%p, table_base=%p\n",runtime->dma_addr, runtime->dma_area, table_base);
236         //snd_printk("dma_addr=%x, dma_area=%p, dma_bytes(size)=%x\n",emu->p16v_buffer.addr, emu->p16v_buffer.area, emu->p16v_buffer.bytes);
237         tmp = snd_emu10k1_ptr_read(emu, A_SPDIF_SAMPLERATE, channel);
238         switch (runtime->rate) {
239         case 44100:
240           snd_emu10k1_ptr_write(emu, A_SPDIF_SAMPLERATE, channel, (tmp & ~0xe000) | 0x8000); /* FIXME: This will change the capture rate as well! */
241           break;
242         case 48000:
243           snd_emu10k1_ptr_write(emu, A_SPDIF_SAMPLERATE, channel, (tmp & ~0xe000) | 0x0000); /* FIXME: This will change the capture rate as well! */
244           break;
245         case 96000:
246           snd_emu10k1_ptr_write(emu, A_SPDIF_SAMPLERATE, channel, (tmp & ~0xe000) | 0x4000); /* FIXME: This will change the capture rate as well! */
247           break;
248         case 192000:
249           snd_emu10k1_ptr_write(emu, A_SPDIF_SAMPLERATE, channel, (tmp & ~0xe000) | 0x2000); /* FIXME: This will change the capture rate as well! */
250           break;
251         default:
252           snd_emu10k1_ptr_write(emu, A_SPDIF_SAMPLERATE, channel, 0x0000); /* FIXME: This will change the capture rate as well! */
253           break;
254         }
255         /* FIXME: Check emu->buffer.size before actually writing to it. */
256         for(i=0; i < runtime->periods; i++) {
257                 table_base[i*2]=runtime->dma_addr+(i*period_size_bytes);
258                 table_base[(i*2)+1]=period_size_bytes<<16;
259         }
260  
261         snd_emu10k1_ptr20_write(emu, PLAYBACK_LIST_ADDR, channel, emu->p16v_buffer.addr+(8*16*channel));
262         snd_emu10k1_ptr20_write(emu, PLAYBACK_LIST_SIZE, channel, (runtime->periods - 1) << 19);
263         snd_emu10k1_ptr20_write(emu, PLAYBACK_LIST_PTR, channel, 0);
264         snd_emu10k1_ptr20_write(emu, PLAYBACK_DMA_ADDR, channel, runtime->dma_addr);
265         snd_emu10k1_ptr20_write(emu, PLAYBACK_PERIOD_SIZE, channel, frames_to_bytes(runtime, runtime->period_size)<<16); // buffer size in bytes
266         snd_emu10k1_ptr20_write(emu, PLAYBACK_POINTER, channel, 0);
267         snd_emu10k1_ptr20_write(emu, 0x07, channel, 0x0);
268         snd_emu10k1_ptr20_write(emu, 0x08, channel, 0);
269
270         return 0;
271 }
272
273 static void snd_p16v_intr_enable(emu10k1_t *emu, unsigned int intrenb)
274 {
275         unsigned long flags;
276         unsigned int enable;
277
278         spin_lock_irqsave(&emu->emu_lock, flags);
279         enable = inl(emu->port + INTE2) | intrenb;
280         outl(enable, emu->port + INTE2);
281         spin_unlock_irqrestore(&emu->emu_lock, flags);
282 }
283
284 static void snd_p16v_intr_disable(emu10k1_t *emu, unsigned int intrenb)
285 {
286         unsigned long flags;
287         unsigned int disable;
288
289         spin_lock_irqsave(&emu->emu_lock, flags);
290         disable = inl(emu->port + INTE2) & (~intrenb);
291         outl(disable, emu->port + INTE2);
292         spin_unlock_irqrestore(&emu->emu_lock, flags);
293 }
294
295 /* trigger_playback callback */
296 static int snd_p16v_pcm_trigger_playback(snd_pcm_substream_t *substream,
297                                     int cmd)
298 {
299         emu10k1_t *emu = snd_pcm_substream_chip(substream);
300         snd_pcm_runtime_t *runtime;
301         emu10k1_pcm_t *epcm;
302         int channel;
303         int result = 0;
304         struct list_head *pos;
305         snd_pcm_substream_t *s;
306         u32 basic = 0;
307         u32 inte = 0;
308         int running=0;
309
310         switch (cmd) {
311         case SNDRV_PCM_TRIGGER_START:
312                 running=1;
313                 break;
314         case SNDRV_PCM_TRIGGER_STOP:
315         default:
316                 running=0;
317                 break;
318         }
319         snd_pcm_group_for_each(pos, substream) {
320                 s = snd_pcm_group_substream_entry(pos);
321                 runtime = s->runtime;
322                 epcm = runtime->private_data;
323                 channel = substream->pcm->device-emu->p16v_device_offset;
324                 //snd_printk("p16v channel=%d\n",channel);
325                 epcm->running = running;
326                 basic |= (0x1<<channel);
327                 inte |= (INTE2_PLAYBACK_CH_0_LOOP<<channel);
328                 snd_pcm_trigger_done(s, substream);
329         }
330         //snd_printk("basic=0x%x, inte=0x%x\n",basic, inte);
331
332         switch (cmd) {
333         case SNDRV_PCM_TRIGGER_START:
334                 snd_p16v_intr_enable(emu, inte);
335                 snd_emu10k1_ptr20_write(emu, BASIC_INTERRUPT, 0, snd_emu10k1_ptr20_read(emu, BASIC_INTERRUPT, 0)| (basic));
336                 break;
337         case SNDRV_PCM_TRIGGER_STOP:
338                 snd_emu10k1_ptr20_write(emu, BASIC_INTERRUPT, 0, snd_emu10k1_ptr20_read(emu, BASIC_INTERRUPT, 0) & ~(basic));
339                 snd_p16v_intr_disable(emu, inte);
340                 break;
341         default:
342                 result = -EINVAL;
343                 break;
344         }
345         return result;
346 }
347
348 /* pointer_playback callback */
349 static snd_pcm_uframes_t
350 snd_p16v_pcm_pointer_playback(snd_pcm_substream_t *substream)
351 {
352         emu10k1_t *emu = snd_pcm_substream_chip(substream);
353         snd_pcm_runtime_t *runtime = substream->runtime;
354         emu10k1_pcm_t *epcm = runtime->private_data;
355         snd_pcm_uframes_t ptr, ptr1, ptr2,ptr3,ptr4 = 0;
356         int channel = substream->pcm->device - emu->p16v_device_offset;
357         if (!epcm->running)
358                 return 0;
359
360         ptr3 = snd_emu10k1_ptr20_read(emu, PLAYBACK_LIST_PTR, channel);
361         ptr1 = snd_emu10k1_ptr20_read(emu, PLAYBACK_POINTER, channel);
362         ptr4 = snd_emu10k1_ptr20_read(emu, PLAYBACK_LIST_PTR, channel);
363         if (ptr3 != ptr4) ptr1 = snd_emu10k1_ptr20_read(emu, PLAYBACK_POINTER, channel);
364         ptr2 = bytes_to_frames(runtime, ptr1);
365         ptr2+= (ptr4 >> 3) * runtime->period_size;
366         ptr=ptr2;
367         if (ptr >= runtime->buffer_size)
368                 ptr -= runtime->buffer_size;
369
370         return ptr;
371 }
372
373 /* operators */
374 static snd_pcm_ops_t snd_p16v_playback_front_ops = {
375         .open =        snd_p16v_pcm_open_playback_front,
376         .close =       snd_p16v_pcm_close_playback,
377         .ioctl =       snd_pcm_lib_ioctl,
378         .hw_params =   snd_p16v_pcm_hw_params_playback,
379         .hw_free =     snd_p16v_pcm_hw_free_playback,
380         .prepare =     snd_p16v_pcm_prepare_playback,
381         .trigger =     snd_p16v_pcm_trigger_playback,
382         .pointer =     snd_p16v_pcm_pointer_playback,
383 };
384
385 int snd_p16v_free(emu10k1_t *chip)
386 {
387         // release the data
388         if (chip->p16v_buffer.area) {
389                 snd_dma_free_pages(&chip->p16v_buffer);
390                 //snd_printk("period lables free: %p\n", &chip->p16v_buffer);
391         }
392         return 0;
393 }
394
395 static void snd_p16v_pcm_free(snd_pcm_t *pcm)
396 {
397         emu10k1_t *emu = pcm->private_data;
398         //snd_printk("snd_p16v_pcm_free pcm: called\n");
399         snd_pcm_lib_preallocate_free_for_all(pcm);
400         emu->pcm = NULL;
401 }
402
403 int snd_p16v_pcm(emu10k1_t *emu, int device, snd_pcm_t **rpcm)
404 {
405         snd_pcm_t *pcm;
406         snd_pcm_substream_t *substream;
407         int err;
408         int capture=0;
409   
410         //snd_printk("snd_p16v_pcm called. device=%d\n", device);
411         emu->p16v_device_offset = device;
412         if (rpcm)
413                 *rpcm = NULL;
414         //if (device == 0) capture=1; 
415         if ((err = snd_pcm_new(emu->card, "p16v", device, 1, capture, &pcm)) < 0)
416                 return err;
417   
418         pcm->private_data = emu;
419         pcm->private_free = snd_p16v_pcm_free;
420
421         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_p16v_playback_front_ops);
422
423         pcm->info_flags = 0;
424         pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX;
425         strcpy(pcm->name, "p16v");
426         emu->pcm = pcm;
427
428         for(substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream; 
429             substream; 
430             substream = substream->next) {
431                 if ((err = snd_pcm_lib_preallocate_pages(substream, 
432                                                          SNDRV_DMA_TYPE_DEV, 
433                                                          snd_dma_pci_data(emu->pci), 
434                                                          64*1024, 64*1024)) < 0) /* FIXME: 32*1024 for sound buffer, between 32and64 for Periods table. */
435                         return err;
436                 //snd_printk("preallocate playback substream: err=%d\n", err);
437         }
438
439         for (substream = pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream; 
440               substream; 
441               substream = substream->next) {
442                 if ((err = snd_pcm_lib_preallocate_pages(substream, 
443                                                    SNDRV_DMA_TYPE_DEV, 
444                                                    snd_dma_pci_data(emu->pci), 
445                                                    64*1024, 64*1024)) < 0)
446                         return err;
447                 //snd_printk("preallocate capture substream: err=%d\n", err);
448         }
449   
450         if (rpcm)
451                 *rpcm = pcm;
452   
453         return 0;
454 }
455
456 static int snd_p16v_volume_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
457 {
458         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
459         uinfo->count = 2;
460         uinfo->value.integer.min = 0;
461         uinfo->value.integer.max = 255;
462         return 0;
463 }
464
465 static int snd_p16v_volume_get(snd_kcontrol_t * kcontrol,
466                                        snd_ctl_elem_value_t * ucontrol, int reg, int high_low)
467 {
468         emu10k1_t *emu = snd_kcontrol_chip(kcontrol);
469         u32 value;
470
471         value = snd_emu10k1_ptr20_read(emu, reg, high_low);
472         if (high_low == 1) {
473                 ucontrol->value.integer.value[0] = 0xff - ((value >> 24) & 0xff); /* Left */
474                 ucontrol->value.integer.value[1] = 0xff - ((value >> 16) & 0xff); /* Right */
475         } else {
476                 ucontrol->value.integer.value[0] = 0xff - ((value >> 8) & 0xff); /* Left */
477                 ucontrol->value.integer.value[1] = 0xff - ((value >> 0) & 0xff); /* Right */
478         }
479         return 0;
480 }
481
482 static int snd_p16v_volume_get_spdif_front(snd_kcontrol_t * kcontrol,
483                                        snd_ctl_elem_value_t * ucontrol)
484 {
485         int high_low = 0;
486         int reg = PLAYBACK_VOLUME_MIXER7;
487         return snd_p16v_volume_get(kcontrol, ucontrol, reg, high_low);
488 }
489
490 static int snd_p16v_volume_get_spdif_center_lfe(snd_kcontrol_t * kcontrol,
491                                        snd_ctl_elem_value_t * ucontrol)
492 {
493         int high_low = 1;
494         int reg = PLAYBACK_VOLUME_MIXER7;
495         return snd_p16v_volume_get(kcontrol, ucontrol, reg, high_low);
496 }
497 static int snd_p16v_volume_get_spdif_unknown(snd_kcontrol_t * kcontrol,
498                                        snd_ctl_elem_value_t * ucontrol)
499 {
500         int high_low = 0;
501         int reg = PLAYBACK_VOLUME_MIXER8;
502         return snd_p16v_volume_get(kcontrol, ucontrol, reg, high_low);
503 }
504 static int snd_p16v_volume_get_spdif_rear(snd_kcontrol_t * kcontrol,
505                                        snd_ctl_elem_value_t * ucontrol)
506 {
507         int high_low = 1;
508         int reg = PLAYBACK_VOLUME_MIXER8;
509         return snd_p16v_volume_get(kcontrol, ucontrol, reg, high_low);
510 }
511
512 static int snd_p16v_volume_get_analog_front(snd_kcontrol_t * kcontrol,
513                                        snd_ctl_elem_value_t * ucontrol)
514 {
515         int high_low = 0;
516         int reg = PLAYBACK_VOLUME_MIXER9;
517         return snd_p16v_volume_get(kcontrol, ucontrol, reg, high_low);
518 }
519
520 static int snd_p16v_volume_get_analog_center_lfe(snd_kcontrol_t * kcontrol,
521                                        snd_ctl_elem_value_t * ucontrol)
522 {
523         int high_low = 1;
524         int reg = PLAYBACK_VOLUME_MIXER9;
525         return snd_p16v_volume_get(kcontrol, ucontrol, reg, high_low);
526 }
527 static int snd_p16v_volume_get_analog_rear(snd_kcontrol_t * kcontrol,
528                                        snd_ctl_elem_value_t * ucontrol)
529 {
530         int high_low = 1;
531         int reg = PLAYBACK_VOLUME_MIXER10;
532         return snd_p16v_volume_get(kcontrol, ucontrol, reg, high_low);
533 }
534
535 static int snd_p16v_volume_get_analog_unknown(snd_kcontrol_t * kcontrol,
536                                        snd_ctl_elem_value_t * ucontrol)
537 {
538         int high_low = 0;
539         int reg = PLAYBACK_VOLUME_MIXER10;
540         return snd_p16v_volume_get(kcontrol, ucontrol, reg, high_low);
541 }
542
543 static int snd_p16v_volume_put(snd_kcontrol_t * kcontrol,
544                                        snd_ctl_elem_value_t * ucontrol, int reg, int high_low)
545 {
546         emu10k1_t *emu = snd_kcontrol_chip(kcontrol);
547         u32 value;
548         value = snd_emu10k1_ptr20_read(emu, reg, 0);
549         //value = value & 0xffff;
550         if (high_low == 1) {
551                 value &= 0xffff;
552                 value = value | ((0xff - ucontrol->value.integer.value[0]) << 24) | ((0xff - ucontrol->value.integer.value[1]) << 16);
553         } else {
554                 value &= 0xffff0000;
555                 value = value | ((0xff - ucontrol->value.integer.value[0]) << 8) | ((0xff - ucontrol->value.integer.value[1]) );
556         }
557                 snd_emu10k1_ptr20_write(emu, reg, 0, value);
558         return 1;
559 }
560
561 static int snd_p16v_volume_put_spdif_front(snd_kcontrol_t * kcontrol,
562                                        snd_ctl_elem_value_t * ucontrol)
563 {
564         int high_low = 0;
565         int reg = PLAYBACK_VOLUME_MIXER7;
566         return snd_p16v_volume_put(kcontrol, ucontrol, reg, high_low);
567 }
568
569 static int snd_p16v_volume_put_spdif_center_lfe(snd_kcontrol_t * kcontrol,
570                                        snd_ctl_elem_value_t * ucontrol)
571 {
572         int high_low = 1;
573         int reg = PLAYBACK_VOLUME_MIXER7;
574         return snd_p16v_volume_put(kcontrol, ucontrol, reg, high_low);
575 }
576
577 static int snd_p16v_volume_put_spdif_unknown(snd_kcontrol_t * kcontrol,
578                                        snd_ctl_elem_value_t * ucontrol)
579 {
580         int high_low = 0;
581         int reg = PLAYBACK_VOLUME_MIXER8;
582         return snd_p16v_volume_put(kcontrol, ucontrol, reg, high_low);
583 }
584
585 static int snd_p16v_volume_put_spdif_rear(snd_kcontrol_t * kcontrol,
586                                        snd_ctl_elem_value_t * ucontrol)
587 {
588         int high_low = 1;
589         int reg = PLAYBACK_VOLUME_MIXER8;
590         return snd_p16v_volume_put(kcontrol, ucontrol, reg, high_low);
591 }
592
593 static int snd_p16v_volume_put_analog_front(snd_kcontrol_t * kcontrol,
594                                        snd_ctl_elem_value_t * ucontrol)
595 {
596         int high_low = 0;
597         int reg = PLAYBACK_VOLUME_MIXER9;
598         return snd_p16v_volume_put(kcontrol, ucontrol, reg, high_low);
599 }
600
601 static int snd_p16v_volume_put_analog_center_lfe(snd_kcontrol_t * kcontrol,
602                                        snd_ctl_elem_value_t * ucontrol)
603 {
604         int high_low = 1;
605         int reg = PLAYBACK_VOLUME_MIXER9;
606         return snd_p16v_volume_put(kcontrol, ucontrol, reg, high_low);
607 }
608
609 static int snd_p16v_volume_put_analog_rear(snd_kcontrol_t * kcontrol,
610                                        snd_ctl_elem_value_t * ucontrol)
611 {
612         int high_low = 1;
613         int reg = PLAYBACK_VOLUME_MIXER10;
614         return snd_p16v_volume_put(kcontrol, ucontrol, reg, high_low);
615 }
616
617 static int snd_p16v_volume_put_analog_unknown(snd_kcontrol_t * kcontrol,
618                                        snd_ctl_elem_value_t * ucontrol)
619 {
620         int high_low = 0;
621         int reg = PLAYBACK_VOLUME_MIXER10;
622         return snd_p16v_volume_put(kcontrol, ucontrol, reg, high_low);
623 }
624
625 static snd_kcontrol_new_t snd_p16v_volume_control_analog_front =
626 {
627         .iface =        SNDRV_CTL_ELEM_IFACE_MIXER,
628         .name =         "HD Analog Front Volume",
629         .info =         snd_p16v_volume_info,
630         .get =          snd_p16v_volume_get_analog_front,
631         .put =          snd_p16v_volume_put_analog_front
632 };
633
634 static snd_kcontrol_new_t snd_p16v_volume_control_analog_center_lfe =
635 {
636         .iface =        SNDRV_CTL_ELEM_IFACE_MIXER,
637         .name =         "HD Analog Center/LFE Volume",
638         .info =         snd_p16v_volume_info,
639         .get =          snd_p16v_volume_get_analog_center_lfe,
640         .put =          snd_p16v_volume_put_analog_center_lfe
641 };
642
643 static snd_kcontrol_new_t snd_p16v_volume_control_analog_unknown =
644 {
645         .iface =        SNDRV_CTL_ELEM_IFACE_MIXER,
646         .name =         "HD Analog Unknown Volume",
647         .info =         snd_p16v_volume_info,
648         .get =          snd_p16v_volume_get_analog_unknown,
649         .put =          snd_p16v_volume_put_analog_unknown
650 };
651
652 static snd_kcontrol_new_t snd_p16v_volume_control_analog_rear =
653 {
654         .iface =        SNDRV_CTL_ELEM_IFACE_MIXER,
655         .name =         "HD Analog Rear Volume",
656         .info =         snd_p16v_volume_info,
657         .get =          snd_p16v_volume_get_analog_rear,
658         .put =          snd_p16v_volume_put_analog_rear
659 };
660
661 static snd_kcontrol_new_t snd_p16v_volume_control_spdif_front =
662 {
663         .iface =        SNDRV_CTL_ELEM_IFACE_MIXER,
664         .name =         "HD SPDIF Front Volume",
665         .info =         snd_p16v_volume_info,
666         .get =          snd_p16v_volume_get_spdif_front,
667         .put =          snd_p16v_volume_put_spdif_front
668 };
669
670 static snd_kcontrol_new_t snd_p16v_volume_control_spdif_center_lfe =
671 {
672         .iface =        SNDRV_CTL_ELEM_IFACE_MIXER,
673         .name =         "HD SPDIF Center/LFE Volume",
674         .info =         snd_p16v_volume_info,
675         .get =          snd_p16v_volume_get_spdif_center_lfe,
676         .put =          snd_p16v_volume_put_spdif_center_lfe
677 };
678
679 static snd_kcontrol_new_t snd_p16v_volume_control_spdif_unknown =
680 {
681         .iface =        SNDRV_CTL_ELEM_IFACE_MIXER,
682         .name =         "HD SPDIF Unknown Volume",
683         .info =         snd_p16v_volume_info,
684         .get =          snd_p16v_volume_get_spdif_unknown,
685         .put =          snd_p16v_volume_put_spdif_unknown
686 };
687
688 static snd_kcontrol_new_t snd_p16v_volume_control_spdif_rear =
689 {
690         .iface =        SNDRV_CTL_ELEM_IFACE_MIXER,
691         .name =         "HD SPDIF Rear Volume",
692         .info =         snd_p16v_volume_info,
693         .get =          snd_p16v_volume_get_spdif_rear,
694         .put =          snd_p16v_volume_put_spdif_rear
695 };
696
697 int snd_p16v_mixer(emu10k1_t *emu)
698 {
699         int err;
700         snd_kcontrol_t *kctl;
701         snd_card_t *card = emu->card;
702         if ((kctl = snd_ctl_new1(&snd_p16v_volume_control_analog_front, emu)) == NULL)
703                 return -ENOMEM;
704         if ((err = snd_ctl_add(card, kctl)))
705                 return err;
706         if ((kctl = snd_ctl_new1(&snd_p16v_volume_control_analog_rear, emu)) == NULL)
707                 return -ENOMEM;
708         if ((err = snd_ctl_add(card, kctl)))
709                 return err;
710         if ((kctl = snd_ctl_new1(&snd_p16v_volume_control_analog_center_lfe, emu)) == NULL)
711                 return -ENOMEM;
712         if ((err = snd_ctl_add(card, kctl)))
713                 return err;
714         if ((kctl = snd_ctl_new1(&snd_p16v_volume_control_analog_unknown, emu)) == NULL)
715                 return -ENOMEM;
716         if ((err = snd_ctl_add(card, kctl)))
717                 return err;
718         if ((kctl = snd_ctl_new1(&snd_p16v_volume_control_spdif_front, emu)) == NULL)
719                 return -ENOMEM;
720         if ((err = snd_ctl_add(card, kctl)))
721                 return err;
722         if ((kctl = snd_ctl_new1(&snd_p16v_volume_control_spdif_rear, emu)) == NULL)
723                 return -ENOMEM;
724         if ((err = snd_ctl_add(card, kctl)))
725                 return err;
726         if ((kctl = snd_ctl_new1(&snd_p16v_volume_control_spdif_center_lfe, emu)) == NULL)
727                 return -ENOMEM;
728         if ((err = snd_ctl_add(card, kctl)))
729                 return err;
730         if ((kctl = snd_ctl_new1(&snd_p16v_volume_control_spdif_unknown, emu)) == NULL)
731                 return -ENOMEM;
732         if ((err = snd_ctl_add(card, kctl)))
733                 return err;
734         return 0;
735 }
736