Linux-2.6.12-rc2
[linux-flexiantxendom0-natty.git] / sound / isa / cs423x / cs4236_lib.c
1 /*
2  *  Copyright (c) by Jaroslav Kysela <perex@suse.cz>
3  *  Routines for control of CS4235/4236B/4237B/4238B/4239 chips
4  *
5  *  Note:
6  *     -----
7  *
8  *  Bugs:
9  *     -----
10  *
11  *   This program is free software; you can redistribute it and/or modify
12  *   it under the terms of the GNU General Public License as published by
13  *   the Free Software Foundation; either version 2 of the License, or
14  *   (at your option) any later version.
15  *
16  *   This program is distributed in the hope that it will be useful,
17  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
18  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  *   GNU General Public License for more details.
20  *
21  *   You should have received a copy of the GNU General Public License
22  *   along with this program; if not, write to the Free Software
23  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
24  *
25  */
26
27 /*
28  *  Indirect control registers (CS4236B+)
29  * 
30  *  C0
31  *     D8: WSS reset (all chips)
32  *
33  *  C1 (all chips except CS4236)
34  *     D7-D5: version 
35  *     D4-D0: chip id
36  *             11101 - CS4235
37  *             01011 - CS4236B
38  *             01000 - CS4237B
39  *             01001 - CS4238B
40  *             11110 - CS4239
41  *
42  *  C2
43  *     D7-D4: 3D Space (CS4235,CS4237B,CS4238B,CS4239)
44  *     D3-D0: 3D Center (CS4237B); 3D Volume (CS4238B)
45  * 
46  *  C3
47  *     D7: 3D Enable (CS4237B)
48  *     D6: 3D Mono Enable (CS4237B)
49  *     D5: 3D Serial Output (CS4237B,CS4238B)
50  *     D4: 3D Enable (CS4235,CS4238B,CS4239)
51  *
52  *  C4
53  *     D7: consumer serial port enable (CS4237B,CS4238B)
54  *     D6: channels status block reset (CS4237B,CS4238B)
55  *     D5: user bit in sub-frame of digital audio data (CS4237B,CS4238B)
56  *     D4: validity bit bit in sub-frame of digital audio data (CS4237B,CS4238B)
57  * 
58  *  C5  lower channel status (digital serial data description) (CS4237B,CS4238B)
59  *     D7-D6: first two bits of category code
60  *     D5: lock
61  *     D4-D3: pre-emphasis (0 = none, 1 = 50/15us)
62  *     D2: copy/copyright (0 = copy inhibited)
63  *     D1: 0 = digital audio / 1 = non-digital audio
64  *     
65  *  C6  upper channel status (digital serial data description) (CS4237B,CS4238B)
66  *     D7-D6: sample frequency (0 = 44.1kHz)
67  *     D5: generation status (0 = no indication, 1 = original/commercially precaptureed data)
68  *     D4-D0: category code (upper bits)
69  *
70  *  C7  reserved (must write 0)
71  *
72  *  C8  wavetable control
73  *     D7: volume control interrupt enable (CS4235,CS4239)
74  *     D6: hardware volume control format (CS4235,CS4239)
75  *     D3: wavetable serial port enable (all chips)
76  *     D2: DSP serial port switch (all chips)
77  *     D1: disable MCLK (all chips)
78  *     D0: force BRESET low (all chips)
79  *
80  */
81
82 #include <sound/driver.h>
83 #include <asm/io.h>
84 #include <linux/delay.h>
85 #include <linux/init.h>
86 #include <linux/time.h>
87 #include <linux/wait.h>
88 #include <sound/core.h>
89 #include <sound/cs4231.h>
90 #include <sound/asoundef.h>
91
92 MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>");
93 MODULE_DESCRIPTION("Routines for control of CS4235/4236B/4237B/4238B/4239 chips");
94 MODULE_LICENSE("GPL");
95
96 /*
97  *
98  */
99
100 static unsigned char snd_cs4236_ext_map[18] = {
101         /* CS4236_LEFT_LINE */          0xff,
102         /* CS4236_RIGHT_LINE */         0xff,
103         /* CS4236_LEFT_MIC */           0xdf,
104         /* CS4236_RIGHT_MIC */          0xdf,
105         /* CS4236_LEFT_MIX_CTRL */      0xe0 | 0x18,
106         /* CS4236_RIGHT_MIX_CTRL */     0xe0,
107         /* CS4236_LEFT_FM */            0xbf,
108         /* CS4236_RIGHT_FM */           0xbf,
109         /* CS4236_LEFT_DSP */           0xbf,
110         /* CS4236_RIGHT_DSP */          0xbf,
111         /* CS4236_RIGHT_LOOPBACK */     0xbf,
112         /* CS4236_DAC_MUTE */           0xe0,
113         /* CS4236_ADC_RATE */           0x01,   /* 48kHz */
114         /* CS4236_DAC_RATE */           0x01,   /* 48kHz */
115         /* CS4236_LEFT_MASTER */        0xbf,
116         /* CS4236_RIGHT_MASTER */       0xbf,
117         /* CS4236_LEFT_WAVE */          0xbf,
118         /* CS4236_RIGHT_WAVE */         0xbf
119 };
120
121 /*
122  *
123  */
124
125 static void snd_cs4236_ctrl_out(cs4231_t *chip, unsigned char reg, unsigned char val)
126 {
127         outb(reg, chip->cport + 3);
128         outb(chip->cimage[reg] = val, chip->cport + 4);
129 }
130
131 static unsigned char snd_cs4236_ctrl_in(cs4231_t *chip, unsigned char reg)
132 {
133         outb(reg, chip->cport + 3);
134         return inb(chip->cport + 4);
135 }
136
137 /*
138  *  PCM
139  */
140
141 #define CLOCKS 8
142
143 static ratnum_t clocks[CLOCKS] = {
144         { .num = 16934400, .den_min = 353, .den_max = 353, .den_step = 1 },
145         { .num = 16934400, .den_min = 529, .den_max = 529, .den_step = 1 },
146         { .num = 16934400, .den_min = 617, .den_max = 617, .den_step = 1 },
147         { .num = 16934400, .den_min = 1058, .den_max = 1058, .den_step = 1 },
148         { .num = 16934400, .den_min = 1764, .den_max = 1764, .den_step = 1 },
149         { .num = 16934400, .den_min = 2117, .den_max = 2117, .den_step = 1 },
150         { .num = 16934400, .den_min = 2558, .den_max = 2558, .den_step = 1 },
151         { .num = 16934400/16, .den_min = 21, .den_max = 192, .den_step = 1 }
152 };
153
154 static snd_pcm_hw_constraint_ratnums_t hw_constraints_clocks = {
155         .nrats = CLOCKS,
156         .rats = clocks,
157 };
158
159 static int snd_cs4236_xrate(snd_pcm_runtime_t *runtime)
160 {
161         return snd_pcm_hw_constraint_ratnums(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
162                                              &hw_constraints_clocks);
163 }
164
165 static unsigned char divisor_to_rate_register(unsigned int divisor)
166 {
167         switch (divisor) {
168         case 353:       return 1;
169         case 529:       return 2;
170         case 617:       return 3;
171         case 1058:      return 4;
172         case 1764:      return 5;
173         case 2117:      return 6;
174         case 2558:      return 7;
175         default:
176                 snd_runtime_check(divisor >= 21 && divisor <= 192, return 192);
177                 return divisor;
178         }
179 }
180
181 static void snd_cs4236_playback_format(cs4231_t *chip, snd_pcm_hw_params_t *params, unsigned char pdfr)
182 {
183         unsigned long flags;
184         unsigned char rate = divisor_to_rate_register(params->rate_den);
185         
186         spin_lock_irqsave(&chip->reg_lock, flags);
187         /* set fast playback format change and clean playback FIFO */
188         snd_cs4231_out(chip, CS4231_ALT_FEATURE_1, chip->image[CS4231_ALT_FEATURE_1] | 0x10);
189         snd_cs4231_out(chip, CS4231_PLAYBK_FORMAT, pdfr & 0xf0);
190         snd_cs4231_out(chip, CS4231_ALT_FEATURE_1, chip->image[CS4231_ALT_FEATURE_1] & ~0x10);
191         snd_cs4236_ext_out(chip, CS4236_DAC_RATE, rate);
192         spin_unlock_irqrestore(&chip->reg_lock, flags);
193 }
194
195 static void snd_cs4236_capture_format(cs4231_t *chip, snd_pcm_hw_params_t *params, unsigned char cdfr)
196 {
197         unsigned long flags;
198         unsigned char rate = divisor_to_rate_register(params->rate_den);
199         
200         spin_lock_irqsave(&chip->reg_lock, flags);
201         /* set fast capture format change and clean capture FIFO */
202         snd_cs4231_out(chip, CS4231_ALT_FEATURE_1, chip->image[CS4231_ALT_FEATURE_1] | 0x20);
203         snd_cs4231_out(chip, CS4231_REC_FORMAT, cdfr & 0xf0);
204         snd_cs4231_out(chip, CS4231_ALT_FEATURE_1, chip->image[CS4231_ALT_FEATURE_1] & ~0x20);
205         snd_cs4236_ext_out(chip, CS4236_ADC_RATE, rate);
206         spin_unlock_irqrestore(&chip->reg_lock, flags);
207 }
208
209 #ifdef CONFIG_PM
210
211 static void snd_cs4236_suspend(cs4231_t *chip)
212 {
213         int reg;
214         unsigned long flags;
215         
216         spin_lock_irqsave(&chip->reg_lock, flags);
217         for (reg = 0; reg < 32; reg++)
218                 chip->image[reg] = snd_cs4231_in(chip, reg);
219         for (reg = 0; reg < 18; reg++)
220                 chip->eimage[reg] = snd_cs4236_ext_in(chip, CS4236_I23VAL(reg));
221         for (reg = 2; reg < 9; reg++)
222                 chip->cimage[reg] = snd_cs4236_ctrl_in(chip, reg);
223         spin_unlock_irqrestore(&chip->reg_lock, flags);
224 }
225
226 static void snd_cs4236_resume(cs4231_t *chip)
227 {
228         int reg;
229         unsigned long flags;
230         
231         snd_cs4231_mce_up(chip);
232         spin_lock_irqsave(&chip->reg_lock, flags);
233         for (reg = 0; reg < 32; reg++) {
234                 switch (reg) {
235                 case CS4236_EXT_REG:
236                 case CS4231_VERSION:
237                 case 27:        /* why? CS4235 - master left */
238                 case 29:        /* why? CS4235 - master right */
239                         break;
240                 default:
241                         snd_cs4231_out(chip, reg, chip->image[reg]);
242                         break;
243                 }
244         }
245         for (reg = 0; reg < 18; reg++)
246                 snd_cs4236_ext_out(chip, CS4236_I23VAL(reg), chip->eimage[reg]);
247         for (reg = 2; reg < 9; reg++) {
248                 switch (reg) {
249                 case 7:
250                         break;
251                 default:
252                         snd_cs4236_ctrl_out(chip, reg, chip->cimage[reg]);
253                 }
254         }
255         spin_unlock_irqrestore(&chip->reg_lock, flags);
256         snd_cs4231_mce_down(chip);
257 }
258
259 #endif /* CONFIG_PM */
260
261 int snd_cs4236_create(snd_card_t * card,
262                       unsigned long port,
263                       unsigned long cport,
264                       int irq, int dma1, int dma2,
265                       unsigned short hardware,
266                       unsigned short hwshare,
267                       cs4231_t ** rchip)
268 {
269         cs4231_t *chip;
270         unsigned char ver1, ver2;
271         unsigned int reg;
272         int err;
273
274         *rchip = NULL;
275         if (hardware == CS4231_HW_DETECT)
276                 hardware = CS4231_HW_DETECT3;
277         if (cport < 0x100) {
278                 snd_printk("please, specify control port for CS4236+ chips\n");
279                 return -ENODEV;
280         }
281         if ((err = snd_cs4231_create(card, port, cport, irq, dma1, dma2, hardware, hwshare, &chip)) < 0)
282                 return err;
283
284         if (!(chip->hardware & CS4231_HW_CS4236B_MASK)) {
285                 snd_printk("CS4236+: MODE3 and extended registers not available, hardware=0x%x\n",chip->hardware);
286                 snd_device_free(card, chip);
287                 return -ENODEV;
288         }
289 #if 0
290         {
291                 int idx;
292                 for (idx = 0; idx < 8; idx++)
293                         snd_printk("CD%i = 0x%x\n", idx, inb(chip->cport + idx));
294                 for (idx = 0; idx < 9; idx++)
295                         snd_printk("C%i = 0x%x\n", idx, snd_cs4236_ctrl_in(chip, idx));
296         }
297 #endif
298         ver1 = snd_cs4236_ctrl_in(chip, 1);
299         ver2 = snd_cs4236_ext_in(chip, CS4236_VERSION);
300         snd_printdd("CS4236: [0x%lx] C1 (version) = 0x%x, ext = 0x%x\n", cport, ver1, ver2);
301         if (ver1 != ver2) {
302                 snd_printk("CS4236+ chip detected, but control port 0x%lx is not valid\n", cport);
303                 snd_device_free(card, chip);
304                 return -ENODEV;
305         }
306         snd_cs4236_ctrl_out(chip, 0, 0x00);
307         snd_cs4236_ctrl_out(chip, 2, 0xff);
308         snd_cs4236_ctrl_out(chip, 3, 0x00);
309         snd_cs4236_ctrl_out(chip, 4, 0x80);
310         snd_cs4236_ctrl_out(chip, 5, ((IEC958_AES1_CON_PCM_CODER & 3) << 6) | IEC958_AES0_CON_EMPHASIS_NONE);
311         snd_cs4236_ctrl_out(chip, 6, IEC958_AES1_CON_PCM_CODER >> 2);
312         snd_cs4236_ctrl_out(chip, 7, 0x00);
313         /* 0x8c for C8 is valid for Turtle Beach Malibu - the IEC-958 output */
314         /* is working with this setup, other hardware should have */
315         /* different signal paths and this value should be selectable */
316         /* in the future */
317         snd_cs4236_ctrl_out(chip, 8, 0x8c);
318         chip->rate_constraint = snd_cs4236_xrate;
319         chip->set_playback_format = snd_cs4236_playback_format;
320         chip->set_capture_format = snd_cs4236_capture_format;
321 #ifdef CONFIG_PM
322         chip->suspend = snd_cs4236_suspend;
323         chip->resume = snd_cs4236_resume;
324 #endif
325
326         /* initialize extended registers */
327         for (reg = 0; reg < sizeof(snd_cs4236_ext_map); reg++)
328                 snd_cs4236_ext_out(chip, CS4236_I23VAL(reg), snd_cs4236_ext_map[reg]);
329
330         /* initialize compatible but more featured registers */
331         snd_cs4231_out(chip, CS4231_LEFT_INPUT, 0x40);
332         snd_cs4231_out(chip, CS4231_RIGHT_INPUT, 0x40);
333         snd_cs4231_out(chip, CS4231_AUX1_LEFT_INPUT, 0xff);
334         snd_cs4231_out(chip, CS4231_AUX1_RIGHT_INPUT, 0xff);
335         snd_cs4231_out(chip, CS4231_AUX2_LEFT_INPUT, 0xdf);
336         snd_cs4231_out(chip, CS4231_AUX2_RIGHT_INPUT, 0xdf);
337         snd_cs4231_out(chip, CS4231_RIGHT_LINE_IN, 0xff);
338         snd_cs4231_out(chip, CS4231_LEFT_LINE_IN, 0xff);
339         snd_cs4231_out(chip, CS4231_RIGHT_LINE_IN, 0xff);
340         switch (chip->hardware) {
341         case CS4231_HW_CS4235:
342         case CS4231_HW_CS4239:
343                 snd_cs4231_out(chip, CS4235_LEFT_MASTER, 0xff);
344                 snd_cs4231_out(chip, CS4235_RIGHT_MASTER, 0xff);
345                 break;
346         }
347
348         *rchip = chip;
349         return 0;
350 }
351
352 int snd_cs4236_pcm(cs4231_t *chip, int device, snd_pcm_t **rpcm)
353 {
354         snd_pcm_t *pcm;
355         int err;
356         
357         if ((err = snd_cs4231_pcm(chip, device, &pcm)) < 0)
358                 return err;
359         pcm->info_flags &= ~SNDRV_PCM_INFO_JOINT_DUPLEX;
360         if (rpcm)
361                 *rpcm = pcm;
362         return 0;
363 }
364
365 /*
366  *  MIXER
367  */
368
369 #define CS4236_SINGLE(xname, xindex, reg, shift, mask, invert) \
370 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
371   .info = snd_cs4236_info_single, \
372   .get = snd_cs4236_get_single, .put = snd_cs4236_put_single, \
373   .private_value = reg | (shift << 8) | (mask << 16) | (invert << 24) }
374
375 static int snd_cs4236_info_single(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
376 {
377         int mask = (kcontrol->private_value >> 16) & 0xff;
378
379         uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
380         uinfo->count = 1;
381         uinfo->value.integer.min = 0;
382         uinfo->value.integer.max = mask;
383         return 0;
384 }
385
386 static int snd_cs4236_get_single(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
387 {
388         cs4231_t *chip = snd_kcontrol_chip(kcontrol);
389         unsigned long flags;
390         int reg = kcontrol->private_value & 0xff;
391         int shift = (kcontrol->private_value >> 8) & 0xff;
392         int mask = (kcontrol->private_value >> 16) & 0xff;
393         int invert = (kcontrol->private_value >> 24) & 0xff;
394         
395         spin_lock_irqsave(&chip->reg_lock, flags);
396         ucontrol->value.integer.value[0] = (chip->eimage[CS4236_REG(reg)] >> shift) & mask;
397         spin_unlock_irqrestore(&chip->reg_lock, flags);
398         if (invert)
399                 ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
400         return 0;
401 }
402
403 static int snd_cs4236_put_single(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
404 {
405         cs4231_t *chip = snd_kcontrol_chip(kcontrol);
406         unsigned long flags;
407         int reg = kcontrol->private_value & 0xff;
408         int shift = (kcontrol->private_value >> 8) & 0xff;
409         int mask = (kcontrol->private_value >> 16) & 0xff;
410         int invert = (kcontrol->private_value >> 24) & 0xff;
411         int change;
412         unsigned short val;
413         
414         val = (ucontrol->value.integer.value[0] & mask);
415         if (invert)
416                 val = mask - val;
417         val <<= shift;
418         spin_lock_irqsave(&chip->reg_lock, flags);
419         val = (chip->eimage[CS4236_REG(reg)] & ~(mask << shift)) | val;
420         change = val != chip->eimage[CS4236_REG(reg)];
421         snd_cs4236_ext_out(chip, reg, val);
422         spin_unlock_irqrestore(&chip->reg_lock, flags);
423         return change;
424 }
425
426 #define CS4236_SINGLEC(xname, xindex, reg, shift, mask, invert) \
427 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
428   .info = snd_cs4236_info_single, \
429   .get = snd_cs4236_get_singlec, .put = snd_cs4236_put_singlec, \
430   .private_value = reg | (shift << 8) | (mask << 16) | (invert << 24) }
431
432 static int snd_cs4236_get_singlec(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
433 {
434         cs4231_t *chip = snd_kcontrol_chip(kcontrol);
435         unsigned long flags;
436         int reg = kcontrol->private_value & 0xff;
437         int shift = (kcontrol->private_value >> 8) & 0xff;
438         int mask = (kcontrol->private_value >> 16) & 0xff;
439         int invert = (kcontrol->private_value >> 24) & 0xff;
440         
441         spin_lock_irqsave(&chip->reg_lock, flags);
442         ucontrol->value.integer.value[0] = (chip->cimage[reg] >> shift) & mask;
443         spin_unlock_irqrestore(&chip->reg_lock, flags);
444         if (invert)
445                 ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
446         return 0;
447 }
448
449 static int snd_cs4236_put_singlec(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
450 {
451         cs4231_t *chip = snd_kcontrol_chip(kcontrol);
452         unsigned long flags;
453         int reg = kcontrol->private_value & 0xff;
454         int shift = (kcontrol->private_value >> 8) & 0xff;
455         int mask = (kcontrol->private_value >> 16) & 0xff;
456         int invert = (kcontrol->private_value >> 24) & 0xff;
457         int change;
458         unsigned short val;
459         
460         val = (ucontrol->value.integer.value[0] & mask);
461         if (invert)
462                 val = mask - val;
463         val <<= shift;
464         spin_lock_irqsave(&chip->reg_lock, flags);
465         val = (chip->cimage[reg] & ~(mask << shift)) | val;
466         change = val != chip->cimage[reg];
467         snd_cs4236_ctrl_out(chip, reg, val);
468         spin_unlock_irqrestore(&chip->reg_lock, flags);
469         return change;
470 }
471
472 #define CS4236_DOUBLE(xname, xindex, left_reg, right_reg, shift_left, shift_right, mask, invert) \
473 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
474   .info = snd_cs4236_info_double, \
475   .get = snd_cs4236_get_double, .put = snd_cs4236_put_double, \
476   .private_value = left_reg | (right_reg << 8) | (shift_left << 16) | (shift_right << 19) | (mask << 24) | (invert << 22) }
477
478 static int snd_cs4236_info_double(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
479 {
480         int mask = (kcontrol->private_value >> 24) & 0xff;
481
482         uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
483         uinfo->count = 2;
484         uinfo->value.integer.min = 0;
485         uinfo->value.integer.max = mask;
486         return 0;
487 }
488
489 static int snd_cs4236_get_double(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
490 {
491         cs4231_t *chip = snd_kcontrol_chip(kcontrol);
492         unsigned long flags;
493         int left_reg = kcontrol->private_value & 0xff;
494         int right_reg = (kcontrol->private_value >> 8) & 0xff;
495         int shift_left = (kcontrol->private_value >> 16) & 0x07;
496         int shift_right = (kcontrol->private_value >> 19) & 0x07;
497         int mask = (kcontrol->private_value >> 24) & 0xff;
498         int invert = (kcontrol->private_value >> 22) & 1;
499         
500         spin_lock_irqsave(&chip->reg_lock, flags);
501         ucontrol->value.integer.value[0] = (chip->eimage[CS4236_REG(left_reg)] >> shift_left) & mask;
502         ucontrol->value.integer.value[1] = (chip->eimage[CS4236_REG(right_reg)] >> shift_right) & mask;
503         spin_unlock_irqrestore(&chip->reg_lock, flags);
504         if (invert) {
505                 ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
506                 ucontrol->value.integer.value[1] = mask - ucontrol->value.integer.value[1];
507         }
508         return 0;
509 }
510
511 static int snd_cs4236_put_double(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
512 {
513         cs4231_t *chip = snd_kcontrol_chip(kcontrol);
514         unsigned long flags;
515         int left_reg = kcontrol->private_value & 0xff;
516         int right_reg = (kcontrol->private_value >> 8) & 0xff;
517         int shift_left = (kcontrol->private_value >> 16) & 0x07;
518         int shift_right = (kcontrol->private_value >> 19) & 0x07;
519         int mask = (kcontrol->private_value >> 24) & 0xff;
520         int invert = (kcontrol->private_value >> 22) & 1;
521         int change;
522         unsigned short val1, val2;
523         
524         val1 = ucontrol->value.integer.value[0] & mask;
525         val2 = ucontrol->value.integer.value[1] & mask;
526         if (invert) {
527                 val1 = mask - val1;
528                 val2 = mask - val2;
529         }
530         val1 <<= shift_left;
531         val2 <<= shift_right;
532         spin_lock_irqsave(&chip->reg_lock, flags);
533         if (left_reg != right_reg) {
534                 val1 = (chip->eimage[CS4236_REG(left_reg)] & ~(mask << shift_left)) | val1;
535                 val2 = (chip->eimage[CS4236_REG(right_reg)] & ~(mask << shift_right)) | val2;
536                 change = val1 != chip->eimage[CS4236_REG(left_reg)] || val2 != chip->eimage[CS4236_REG(right_reg)];
537                 snd_cs4236_ext_out(chip, left_reg, val1);
538                 snd_cs4236_ext_out(chip, right_reg, val2);
539         } else {
540                 val1 = (chip->eimage[CS4236_REG(left_reg)] & ~((mask << shift_left) | (mask << shift_right))) | val1 | val2;
541                 change = val1 != chip->eimage[CS4236_REG(left_reg)];
542                 snd_cs4236_ext_out(chip, left_reg, val1);
543         }
544         spin_unlock_irqrestore(&chip->reg_lock, flags);
545         return change;
546 }
547
548 #define CS4236_DOUBLE1(xname, xindex, left_reg, right_reg, shift_left, shift_right, mask, invert) \
549 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
550   .info = snd_cs4236_info_double, \
551   .get = snd_cs4236_get_double1, .put = snd_cs4236_put_double1, \
552   .private_value = left_reg | (right_reg << 8) | (shift_left << 16) | (shift_right << 19) | (mask << 24) | (invert << 22) }
553
554 static int snd_cs4236_get_double1(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
555 {
556         cs4231_t *chip = snd_kcontrol_chip(kcontrol);
557         unsigned long flags;
558         int left_reg = kcontrol->private_value & 0xff;
559         int right_reg = (kcontrol->private_value >> 8) & 0xff;
560         int shift_left = (kcontrol->private_value >> 16) & 0x07;
561         int shift_right = (kcontrol->private_value >> 19) & 0x07;
562         int mask = (kcontrol->private_value >> 24) & 0xff;
563         int invert = (kcontrol->private_value >> 22) & 1;
564         
565         spin_lock_irqsave(&chip->reg_lock, flags);
566         ucontrol->value.integer.value[0] = (chip->image[left_reg] >> shift_left) & mask;
567         ucontrol->value.integer.value[1] = (chip->eimage[CS4236_REG(right_reg)] >> shift_right) & mask;
568         spin_unlock_irqrestore(&chip->reg_lock, flags);
569         if (invert) {
570                 ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
571                 ucontrol->value.integer.value[1] = mask - ucontrol->value.integer.value[1];
572         }
573         return 0;
574 }
575
576 static int snd_cs4236_put_double1(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
577 {
578         cs4231_t *chip = snd_kcontrol_chip(kcontrol);
579         unsigned long flags;
580         int left_reg = kcontrol->private_value & 0xff;
581         int right_reg = (kcontrol->private_value >> 8) & 0xff;
582         int shift_left = (kcontrol->private_value >> 16) & 0x07;
583         int shift_right = (kcontrol->private_value >> 19) & 0x07;
584         int mask = (kcontrol->private_value >> 24) & 0xff;
585         int invert = (kcontrol->private_value >> 22) & 1;
586         int change;
587         unsigned short val1, val2;
588         
589         val1 = ucontrol->value.integer.value[0] & mask;
590         val2 = ucontrol->value.integer.value[1] & mask;
591         if (invert) {
592                 val1 = mask - val1;
593                 val2 = mask - val2;
594         }
595         val1 <<= shift_left;
596         val2 <<= shift_right;
597         spin_lock_irqsave(&chip->reg_lock, flags);
598         val1 = (chip->image[left_reg] & ~(mask << shift_left)) | val1;
599         val2 = (chip->eimage[CS4236_REG(right_reg)] & ~(mask << shift_right)) | val2;
600         change = val1 != chip->image[left_reg] || val2 != chip->eimage[CS4236_REG(right_reg)];
601         snd_cs4231_out(chip, left_reg, val1);
602         snd_cs4236_ext_out(chip, right_reg, val2);
603         spin_unlock_irqrestore(&chip->reg_lock, flags);
604         return change;
605 }
606
607 #define CS4236_MASTER_DIGITAL(xname, xindex) \
608 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
609   .info = snd_cs4236_info_double, \
610   .get = snd_cs4236_get_master_digital, .put = snd_cs4236_put_master_digital, \
611   .private_value = 71 << 24 }
612
613 static inline int snd_cs4236_mixer_master_digital_invert_volume(int vol)
614 {
615         return (vol < 64) ? 63 - vol : 64 + (71 - vol);
616 }        
617
618 static int snd_cs4236_get_master_digital(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
619 {
620         cs4231_t *chip = snd_kcontrol_chip(kcontrol);
621         unsigned long flags;
622         
623         spin_lock_irqsave(&chip->reg_lock, flags);
624         ucontrol->value.integer.value[0] = snd_cs4236_mixer_master_digital_invert_volume(chip->eimage[CS4236_REG(CS4236_LEFT_MASTER)] & 0x7f);
625         ucontrol->value.integer.value[1] = snd_cs4236_mixer_master_digital_invert_volume(chip->eimage[CS4236_REG(CS4236_RIGHT_MASTER)] & 0x7f);
626         spin_unlock_irqrestore(&chip->reg_lock, flags);
627         return 0;
628 }
629
630 static int snd_cs4236_put_master_digital(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
631 {
632         cs4231_t *chip = snd_kcontrol_chip(kcontrol);
633         unsigned long flags;
634         int change;
635         unsigned short val1, val2;
636         
637         val1 = snd_cs4236_mixer_master_digital_invert_volume(ucontrol->value.integer.value[0] & 0x7f);
638         val2 = snd_cs4236_mixer_master_digital_invert_volume(ucontrol->value.integer.value[1] & 0x7f);
639         spin_lock_irqsave(&chip->reg_lock, flags);
640         val1 = (chip->eimage[CS4236_REG(CS4236_LEFT_MASTER)] & ~0x7f) | val1;
641         val2 = (chip->eimage[CS4236_REG(CS4236_RIGHT_MASTER)] & ~0x7f) | val2;
642         change = val1 != chip->eimage[CS4236_REG(CS4236_LEFT_MASTER)] || val2 != chip->eimage[CS4236_REG(CS4236_RIGHT_MASTER)];
643         snd_cs4236_ext_out(chip, CS4236_LEFT_MASTER, val1);
644         snd_cs4236_ext_out(chip, CS4236_RIGHT_MASTER, val1);
645         spin_unlock_irqrestore(&chip->reg_lock, flags);
646         return change;
647 }
648
649 #define CS4235_OUTPUT_ACCU(xname, xindex) \
650 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
651   .info = snd_cs4236_info_double, \
652   .get = snd_cs4235_get_output_accu, .put = snd_cs4235_put_output_accu, \
653   .private_value = 3 << 24 }
654
655 static inline int snd_cs4235_mixer_output_accu_get_volume(int vol)
656 {
657         switch ((vol >> 5) & 3) {
658         case 0: return 1;
659         case 1: return 3;
660         case 2: return 2;
661         case 3: return 0;
662         }
663         return 3;
664 }
665
666 static inline int snd_cs4235_mixer_output_accu_set_volume(int vol)
667 {
668         switch (vol & 3) {
669         case 0: return 3 << 5;
670         case 1: return 0 << 5;
671         case 2: return 2 << 5;
672         case 3: return 1 << 5;
673         }
674         return 1 << 5;
675 }
676
677 static int snd_cs4235_get_output_accu(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
678 {
679         cs4231_t *chip = snd_kcontrol_chip(kcontrol);
680         unsigned long flags;
681         
682         spin_lock_irqsave(&chip->reg_lock, flags);
683         ucontrol->value.integer.value[0] = snd_cs4235_mixer_output_accu_get_volume(chip->image[CS4235_LEFT_MASTER]);
684         ucontrol->value.integer.value[1] = snd_cs4235_mixer_output_accu_get_volume(chip->image[CS4235_RIGHT_MASTER]);
685         spin_unlock_irqrestore(&chip->reg_lock, flags);
686         return 0;
687 }
688
689 static int snd_cs4235_put_output_accu(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
690 {
691         cs4231_t *chip = snd_kcontrol_chip(kcontrol);
692         unsigned long flags;
693         int change;
694         unsigned short val1, val2;
695         
696         val1 = snd_cs4235_mixer_output_accu_set_volume(ucontrol->value.integer.value[0]);
697         val2 = snd_cs4235_mixer_output_accu_set_volume(ucontrol->value.integer.value[1]);
698         spin_lock_irqsave(&chip->reg_lock, flags);
699         val1 = (chip->image[CS4235_LEFT_MASTER] & ~(3 << 5)) | val1;
700         val2 = (chip->image[CS4235_RIGHT_MASTER] & ~(3 << 5)) | val2;
701         change = val1 != chip->image[CS4235_LEFT_MASTER] || val2 != chip->image[CS4235_RIGHT_MASTER];
702         snd_cs4231_out(chip, CS4235_LEFT_MASTER, val1);
703         snd_cs4231_out(chip, CS4235_RIGHT_MASTER, val2);
704         spin_unlock_irqrestore(&chip->reg_lock, flags);
705         return change;
706 }
707
708 static snd_kcontrol_new_t snd_cs4236_controls[] = {
709
710 CS4236_DOUBLE("Master Digital Playback Switch", 0, CS4236_LEFT_MASTER, CS4236_RIGHT_MASTER, 7, 7, 1, 1),
711 CS4236_DOUBLE("Master Digital Capture Switch", 0, CS4236_DAC_MUTE, CS4236_DAC_MUTE, 7, 6, 1, 1),
712 CS4236_MASTER_DIGITAL("Master Digital Volume", 0),
713
714 CS4236_DOUBLE("Capture Boost Volume", 0, CS4236_LEFT_MIX_CTRL, CS4236_RIGHT_MIX_CTRL, 5, 5, 3, 1),
715
716 CS4231_DOUBLE("PCM Playback Switch", 0, CS4231_LEFT_OUTPUT, CS4231_RIGHT_OUTPUT, 7, 7, 1, 1),
717 CS4231_DOUBLE("PCM Playback Volume", 0, CS4231_LEFT_OUTPUT, CS4231_RIGHT_OUTPUT, 0, 0, 63, 1),
718
719 CS4236_DOUBLE("DSP Playback Switch", 0, CS4236_LEFT_DSP, CS4236_RIGHT_DSP, 7, 7, 1, 1),
720 CS4236_DOUBLE("DSP Playback Volume", 0, CS4236_LEFT_DSP, CS4236_RIGHT_DSP, 0, 0, 63, 1),
721
722 CS4236_DOUBLE("FM Playback Switch", 0, CS4236_LEFT_FM, CS4236_RIGHT_FM, 7, 7, 1, 1),
723 CS4236_DOUBLE("FM Playback Volume", 0, CS4236_LEFT_FM, CS4236_RIGHT_FM, 0, 0, 63, 1),
724
725 CS4236_DOUBLE("Wavetable Playback Switch", 0, CS4236_LEFT_WAVE, CS4236_RIGHT_WAVE, 7, 7, 1, 1),
726 CS4236_DOUBLE("Wavetable Playback Volume", 0, CS4236_LEFT_WAVE, CS4236_RIGHT_WAVE, 0, 0, 63, 1),
727
728 CS4231_DOUBLE("Synth Playback Switch", 0, CS4231_LEFT_LINE_IN, CS4231_RIGHT_LINE_IN, 7, 7, 1, 1),
729 CS4231_DOUBLE("Synth Volume", 0, CS4231_LEFT_LINE_IN, CS4231_RIGHT_LINE_IN, 0, 0, 31, 1),
730 CS4231_DOUBLE("Synth Capture Switch", 0, CS4231_LEFT_LINE_IN, CS4231_RIGHT_LINE_IN, 6, 6, 1, 1),
731 CS4231_DOUBLE("Synth Capture Bypass", 0, CS4231_LEFT_LINE_IN, CS4231_RIGHT_LINE_IN, 5, 5, 1, 1),
732
733 CS4236_DOUBLE("Mic Playback Switch", 0, CS4236_LEFT_MIC, CS4236_RIGHT_MIC, 6, 6, 1, 1),
734 CS4236_DOUBLE("Mic Capture Switch", 0, CS4236_LEFT_MIC, CS4236_RIGHT_MIC, 7, 7, 1, 1),
735 CS4236_DOUBLE("Mic Volume", 0, CS4236_LEFT_MIC, CS4236_RIGHT_MIC, 0, 0, 31, 1),
736 CS4236_DOUBLE("Mic Playback Boost", 0, CS4236_LEFT_MIC, CS4236_RIGHT_MIC, 5, 5, 1, 0),
737
738 CS4231_DOUBLE("Line Playback Switch", 0, CS4231_AUX1_LEFT_INPUT, CS4231_AUX1_RIGHT_INPUT, 7, 7, 1, 1),
739 CS4231_DOUBLE("Line Volume", 0, CS4231_AUX1_LEFT_INPUT, CS4231_AUX1_RIGHT_INPUT, 0, 0, 31, 1),
740 CS4231_DOUBLE("Line Capture Switch", 0, CS4231_AUX1_LEFT_INPUT, CS4231_AUX1_RIGHT_INPUT, 6, 6, 1, 1),
741 CS4231_DOUBLE("Line Capture Bypass", 0, CS4231_AUX1_LEFT_INPUT, CS4231_AUX1_RIGHT_INPUT, 5, 5, 1, 1),
742
743 CS4231_DOUBLE("CD Playback Switch", 0, CS4231_AUX2_LEFT_INPUT, CS4231_AUX2_RIGHT_INPUT, 7, 7, 1, 1),
744 CS4231_DOUBLE("CD Volume", 0, CS4231_AUX2_LEFT_INPUT, CS4231_AUX2_RIGHT_INPUT, 0, 0, 31, 1),
745 CS4231_DOUBLE("CD Capture Switch", 0, CS4231_AUX2_LEFT_INPUT, CS4231_AUX2_RIGHT_INPUT, 6, 6, 1, 1),
746
747 CS4236_DOUBLE1("Mono Output Playback Switch", 0, CS4231_MONO_CTRL, CS4236_RIGHT_MIX_CTRL, 6, 7, 1, 1),
748 CS4236_DOUBLE1("Mono Playback Switch", 0, CS4231_MONO_CTRL, CS4236_LEFT_MIX_CTRL, 7, 7, 1, 1),
749 CS4231_SINGLE("Mono Playback Volume", 0, CS4231_MONO_CTRL, 0, 15, 1),
750 CS4231_SINGLE("Mono Playback Bypass", 0, CS4231_MONO_CTRL, 5, 1, 0),
751
752 CS4231_DOUBLE("Capture Volume", 0, CS4231_LEFT_INPUT, CS4231_RIGHT_INPUT, 0, 0, 15, 0),
753 CS4231_DOUBLE("Analog Loopback Capture Switch", 0, CS4231_LEFT_INPUT, CS4231_RIGHT_INPUT, 7, 7, 1, 0),
754
755 CS4231_SINGLE("Digital Loopback Playback Switch", 0, CS4231_LOOPBACK, 0, 1, 0),
756 CS4236_DOUBLE1("Digital Loopback Playback Volume", 0, CS4231_LOOPBACK, CS4236_RIGHT_LOOPBACK, 2, 0, 63, 1)
757 };
758
759 static snd_kcontrol_new_t snd_cs4235_controls[] = {
760
761 CS4231_DOUBLE("Master Switch", 0, CS4235_LEFT_MASTER, CS4235_RIGHT_MASTER, 7, 7, 1, 1),
762 CS4231_DOUBLE("Master Volume", 0, CS4235_LEFT_MASTER, CS4235_RIGHT_MASTER, 0, 0, 31, 1),
763
764 CS4235_OUTPUT_ACCU("Playback Volume", 0),
765
766 CS4236_DOUBLE("Master Digital Playback Switch", 0, CS4236_LEFT_MASTER, CS4236_RIGHT_MASTER, 7, 7, 1, 1),
767 CS4236_DOUBLE("Master Digital Capture Switch", 0, CS4236_DAC_MUTE, CS4236_DAC_MUTE, 7, 6, 1, 1),
768 CS4236_MASTER_DIGITAL("Master Digital Volume", 0),
769
770 CS4231_DOUBLE("Master Digital Playback Switch", 1, CS4231_LEFT_LINE_IN, CS4231_RIGHT_LINE_IN, 7, 7, 1, 1),
771 CS4231_DOUBLE("Master Digital Capture Switch", 1, CS4231_LEFT_LINE_IN, CS4231_RIGHT_LINE_IN, 6, 6, 1, 1),
772 CS4231_DOUBLE("Master Digital Volume", 1, CS4231_LEFT_LINE_IN, CS4231_RIGHT_LINE_IN, 0, 0, 31, 1),
773
774 CS4236_DOUBLE("Capture Volume", 0, CS4236_LEFT_MIX_CTRL, CS4236_RIGHT_MIX_CTRL, 5, 5, 3, 1),
775
776 CS4231_DOUBLE("PCM Switch", 0, CS4231_LEFT_OUTPUT, CS4231_RIGHT_OUTPUT, 7, 7, 1, 1),
777 CS4231_DOUBLE("PCM Volume", 0, CS4231_LEFT_OUTPUT, CS4231_RIGHT_OUTPUT, 0, 0, 63, 1),
778
779 CS4236_DOUBLE("DSP Switch", 0, CS4236_LEFT_DSP, CS4236_RIGHT_DSP, 7, 7, 1, 1),
780
781 CS4236_DOUBLE("FM Switch", 0, CS4236_LEFT_FM, CS4236_RIGHT_FM, 7, 7, 1, 1),
782
783 CS4236_DOUBLE("Wavetable Switch", 0, CS4236_LEFT_WAVE, CS4236_RIGHT_WAVE, 7, 7, 1, 1),
784
785 CS4236_DOUBLE("Mic Capture Switch", 0, CS4236_LEFT_MIC, CS4236_RIGHT_MIC, 7, 7, 1, 1),
786 CS4236_DOUBLE("Mic Playback Switch", 0, CS4236_LEFT_MIC, CS4236_RIGHT_MIC, 6, 6, 1, 1),
787 CS4236_SINGLE("Mic Volume", 0, CS4236_LEFT_MIC, 0, 31, 1),
788 CS4236_SINGLE("Mic Playback Boost", 0, CS4236_LEFT_MIC, 5, 1, 0),
789
790 CS4231_DOUBLE("Aux Playback Switch", 0, CS4231_AUX1_LEFT_INPUT, CS4231_AUX1_RIGHT_INPUT, 7, 7, 1, 1),
791 CS4231_DOUBLE("Aux Capture Switch", 0, CS4231_AUX1_LEFT_INPUT, CS4231_AUX1_RIGHT_INPUT, 6, 6, 1, 1),
792 CS4231_DOUBLE("Aux Volume", 0, CS4231_AUX1_LEFT_INPUT, CS4231_AUX1_RIGHT_INPUT, 0, 0, 31, 1),
793
794 CS4231_DOUBLE("Aux Playback Switch", 1, CS4231_AUX2_LEFT_INPUT, CS4231_AUX2_RIGHT_INPUT, 7, 7, 1, 1),
795 CS4231_DOUBLE("Aux Capture Switch", 1, CS4231_AUX2_LEFT_INPUT, CS4231_AUX2_RIGHT_INPUT, 6, 6, 1, 1),
796 CS4231_DOUBLE("Aux Volume", 1, CS4231_AUX2_LEFT_INPUT, CS4231_AUX2_RIGHT_INPUT, 0, 0, 31, 1),
797
798 CS4236_DOUBLE1("Master Mono Switch", 0, CS4231_MONO_CTRL, CS4236_RIGHT_MIX_CTRL, 6, 7, 1, 1),
799
800 CS4236_DOUBLE1("Mono Switch", 0, CS4231_MONO_CTRL, CS4236_LEFT_MIX_CTRL, 7, 7, 1, 1),
801 CS4231_SINGLE("Mono Volume", 0, CS4231_MONO_CTRL, 0, 15, 1),
802
803 CS4231_DOUBLE("Analog Loopback Switch", 0, CS4231_LEFT_INPUT, CS4231_RIGHT_INPUT, 7, 7, 1, 0),
804 };
805
806 #define CS4236_IEC958_ENABLE(xname, xindex) \
807 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
808   .info = snd_cs4236_info_single, \
809   .get = snd_cs4236_get_iec958_switch, .put = snd_cs4236_put_iec958_switch, \
810   .private_value = 1 << 16 }
811
812 static int snd_cs4236_get_iec958_switch(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
813 {
814         cs4231_t *chip = snd_kcontrol_chip(kcontrol);
815         unsigned long flags;
816         
817         spin_lock_irqsave(&chip->reg_lock, flags);
818         ucontrol->value.integer.value[0] = chip->image[CS4231_ALT_FEATURE_1] & 0x02 ? 1 : 0;
819 #if 0
820         printk("get valid: ALT = 0x%x, C3 = 0x%x, C4 = 0x%x, C5 = 0x%x, C6 = 0x%x, C8 = 0x%x\n",
821                         snd_cs4231_in(chip, CS4231_ALT_FEATURE_1),
822                         snd_cs4236_ctrl_in(chip, 3),
823                         snd_cs4236_ctrl_in(chip, 4),
824                         snd_cs4236_ctrl_in(chip, 5),
825                         snd_cs4236_ctrl_in(chip, 6),
826                         snd_cs4236_ctrl_in(chip, 8));
827 #endif
828         spin_unlock_irqrestore(&chip->reg_lock, flags);
829         return 0;
830 }
831
832 static int snd_cs4236_put_iec958_switch(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
833 {
834         cs4231_t *chip = snd_kcontrol_chip(kcontrol);
835         unsigned long flags;
836         int change;
837         unsigned short enable, val;
838         
839         enable = ucontrol->value.integer.value[0] & 1;
840
841         down(&chip->mce_mutex);
842         snd_cs4231_mce_up(chip);
843         spin_lock_irqsave(&chip->reg_lock, flags);
844         val = (chip->image[CS4231_ALT_FEATURE_1] & ~0x0e) | (0<<2) | (enable << 1);
845         change = val != chip->image[CS4231_ALT_FEATURE_1];
846         snd_cs4231_out(chip, CS4231_ALT_FEATURE_1, val);
847         val = snd_cs4236_ctrl_in(chip, 4) | 0xc0;
848         snd_cs4236_ctrl_out(chip, 4, val);
849         udelay(100);
850         val &= ~0x40;
851         snd_cs4236_ctrl_out(chip, 4, val);
852         spin_unlock_irqrestore(&chip->reg_lock, flags);
853         snd_cs4231_mce_down(chip);
854         up(&chip->mce_mutex);
855
856 #if 0
857         printk("set valid: ALT = 0x%x, C3 = 0x%x, C4 = 0x%x, C5 = 0x%x, C6 = 0x%x, C8 = 0x%x\n",
858                         snd_cs4231_in(chip, CS4231_ALT_FEATURE_1),
859                         snd_cs4236_ctrl_in(chip, 3),
860                         snd_cs4236_ctrl_in(chip, 4),
861                         snd_cs4236_ctrl_in(chip, 5),
862                         snd_cs4236_ctrl_in(chip, 6),
863                         snd_cs4236_ctrl_in(chip, 8));
864 #endif
865         return change;
866 }
867
868 static snd_kcontrol_new_t snd_cs4236_iec958_controls[] = {
869 CS4236_IEC958_ENABLE("IEC958 Output Enable", 0),
870 CS4236_SINGLEC("IEC958 Output Validity", 0, 4, 4, 1, 0),
871 CS4236_SINGLEC("IEC958 Output User", 0, 4, 5, 1, 0),
872 CS4236_SINGLEC("IEC958 Output CSBR", 0, 4, 6, 1, 0),
873 CS4236_SINGLEC("IEC958 Output Channel Status Low", 0, 5, 1, 127, 0),
874 CS4236_SINGLEC("IEC958 Output Channel Status High", 0, 6, 0, 255, 0)
875 };
876
877 static snd_kcontrol_new_t snd_cs4236_3d_controls_cs4235[] = {
878 CS4236_SINGLEC("3D Control - Switch", 0, 3, 4, 1, 0),
879 CS4236_SINGLEC("3D Control - Space", 0, 2, 4, 15, 1)
880 };
881
882 static snd_kcontrol_new_t snd_cs4236_3d_controls_cs4237[] = {
883 CS4236_SINGLEC("3D Control - Switch", 0, 3, 7, 1, 0),
884 CS4236_SINGLEC("3D Control - Space", 0, 2, 4, 15, 1),
885 CS4236_SINGLEC("3D Control - Center", 0, 2, 0, 15, 1),
886 CS4236_SINGLEC("3D Control - Mono", 0, 3, 6, 1, 0),
887 CS4236_SINGLEC("3D Control - IEC958", 0, 3, 5, 1, 0)
888 };
889
890 static snd_kcontrol_new_t snd_cs4236_3d_controls_cs4238[] = {
891 CS4236_SINGLEC("3D Control - Switch", 0, 3, 4, 1, 0),
892 CS4236_SINGLEC("3D Control - Space", 0, 2, 4, 15, 1),
893 CS4236_SINGLEC("3D Control - Volume", 0, 2, 0, 15, 1),
894 CS4236_SINGLEC("3D Control - IEC958", 0, 3, 5, 1, 0)
895 };
896
897 int snd_cs4236_mixer(cs4231_t *chip)
898 {
899         snd_card_t *card;
900         unsigned int idx, count;
901         int err;
902         snd_kcontrol_new_t *kcontrol;
903
904         snd_assert(chip != NULL && chip->card != NULL, return -EINVAL);
905         card = chip->card;
906         strcpy(card->mixername, snd_cs4231_chip_id(chip));
907
908         if (chip->hardware == CS4231_HW_CS4235 ||
909             chip->hardware == CS4231_HW_CS4239) {
910                 for (idx = 0; idx < ARRAY_SIZE(snd_cs4235_controls); idx++) {
911                         if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_cs4235_controls[idx], chip))) < 0)
912                                 return err;
913                 }
914         } else {
915                 for (idx = 0; idx < ARRAY_SIZE(snd_cs4236_controls); idx++) {
916                         if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_cs4236_controls[idx], chip))) < 0)
917                                 return err;
918                 }
919         }
920         switch (chip->hardware) {
921         case CS4231_HW_CS4235:
922         case CS4231_HW_CS4239:
923                 count = ARRAY_SIZE(snd_cs4236_3d_controls_cs4235);
924                 kcontrol = snd_cs4236_3d_controls_cs4235;
925                 break;
926         case CS4231_HW_CS4237B:
927                 count = ARRAY_SIZE(snd_cs4236_3d_controls_cs4237);
928                 kcontrol = snd_cs4236_3d_controls_cs4237;
929                 break;
930         case CS4231_HW_CS4238B:
931                 count = ARRAY_SIZE(snd_cs4236_3d_controls_cs4238);
932                 kcontrol = snd_cs4236_3d_controls_cs4238;
933                 break;
934         default:
935                 count = 0;
936                 kcontrol = NULL;
937         }
938         for (idx = 0; idx < count; idx++, kcontrol++) {
939                 if ((err = snd_ctl_add(card, snd_ctl_new1(kcontrol, chip))) < 0)
940                         return err;
941         }
942         if (chip->hardware == CS4231_HW_CS4237B ||
943             chip->hardware == CS4231_HW_CS4238B) {
944                 for (idx = 0; idx < ARRAY_SIZE(snd_cs4236_iec958_controls); idx++) {
945                         if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_cs4236_iec958_controls[idx], chip))) < 0)
946                                 return err;
947                 }
948         }
949         return 0;
950 }
951
952 EXPORT_SYMBOL(snd_cs4236_create);
953 EXPORT_SYMBOL(snd_cs4236_pcm);
954 EXPORT_SYMBOL(snd_cs4236_mixer);
955
956 /*
957  *  INIT part
958  */
959
960 static int __init alsa_cs4236_init(void)
961 {
962         return 0;
963 }
964
965 static void __exit alsa_cs4236_exit(void)
966 {
967 }
968
969 module_init(alsa_cs4236_init)
970 module_exit(alsa_cs4236_exit)