Merge git://git.kernel.org/pub/scm/linux/kernel/git/mingo/linux-2.6-sched
[linux-2.6] / sound / pci / ice1712 / ews.c
1 /*
2  *   ALSA driver for ICEnsemble ICE1712 (Envy24)
3  *
4  *   Lowlevel functions for Terratec EWS88MT/D, EWX24/96, DMX 6Fire
5  *
6  *      Copyright (c) 2000 Jaroslav Kysela <perex@perex.cz>
7  *                    2002 Takashi Iwai <tiwai@suse.de>
8  *
9  *   This program is free software; you can redistribute it and/or modify
10  *   it under the terms of the GNU General Public License as published by
11  *   the Free Software Foundation; either version 2 of the License, or
12  *   (at your option) any later version.
13  *
14  *   This program is distributed in the hope that it will be useful,
15  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *   GNU General Public License for more details.
18  *
19  *   You should have received a copy of the GNU General Public License
20  *   along with this program; if not, write to the Free Software
21  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
22  *
23  */      
24
25 #include <asm/io.h>
26 #include <linux/delay.h>
27 #include <linux/interrupt.h>
28 #include <linux/init.h>
29 #include <linux/slab.h>
30 #include <sound/core.h>
31 #include <sound/cs8427.h>
32 #include <sound/asoundef.h>
33
34 #include "ice1712.h"
35 #include "ews.h"
36
37 #define SND_CS8404
38 #include <sound/cs8403.h>
39
40 enum {
41         EWS_I2C_CS8404 = 0, EWS_I2C_PCF1, EWS_I2C_PCF2,
42         EWS_I2C_88D = 0,
43         EWS_I2C_6FIRE = 0
44 };
45         
46
47 /* additional i2c devices for EWS boards */
48 struct ews_spec {
49         struct snd_i2c_device *i2cdevs[3];
50 };
51
52 /*
53  * access via i2c mode (for EWX 24/96, EWS 88MT&D)
54  */
55
56 /* send SDA and SCL */
57 static void ewx_i2c_setlines(struct snd_i2c_bus *bus, int clk, int data)
58 {
59         struct snd_ice1712 *ice = bus->private_data;
60         unsigned char tmp = 0;
61         if (clk)
62                 tmp |= ICE1712_EWX2496_SERIAL_CLOCK;
63         if (data)
64                 tmp |= ICE1712_EWX2496_SERIAL_DATA;
65         snd_ice1712_write(ice, ICE1712_IREG_GPIO_DATA, tmp);
66         udelay(5);
67 }
68
69 static int ewx_i2c_getclock(struct snd_i2c_bus *bus)
70 {
71         struct snd_ice1712 *ice = bus->private_data;
72         return snd_ice1712_read(ice, ICE1712_IREG_GPIO_DATA) & ICE1712_EWX2496_SERIAL_CLOCK ? 1 : 0;
73 }
74
75 static int ewx_i2c_getdata(struct snd_i2c_bus *bus, int ack)
76 {
77         struct snd_ice1712 *ice = bus->private_data;
78         int bit;
79         /* set RW pin to low */
80         snd_ice1712_write(ice, ICE1712_IREG_GPIO_WRITE_MASK, ~ICE1712_EWX2496_RW);
81         snd_ice1712_write(ice, ICE1712_IREG_GPIO_DATA, 0);
82         if (ack)
83                 udelay(5);
84         bit = snd_ice1712_read(ice, ICE1712_IREG_GPIO_DATA) & ICE1712_EWX2496_SERIAL_DATA ? 1 : 0;
85         /* set RW pin to high */
86         snd_ice1712_write(ice, ICE1712_IREG_GPIO_DATA, ICE1712_EWX2496_RW);
87         /* reset write mask */
88         snd_ice1712_write(ice, ICE1712_IREG_GPIO_WRITE_MASK, ~ICE1712_EWX2496_SERIAL_CLOCK);
89         return bit;
90 }
91
92 static void ewx_i2c_start(struct snd_i2c_bus *bus)
93 {
94         struct snd_ice1712 *ice = bus->private_data;
95         unsigned char mask;
96
97         snd_ice1712_save_gpio_status(ice);
98         /* set RW high */
99         mask = ICE1712_EWX2496_RW;
100         switch (ice->eeprom.subvendor) {
101         case ICE1712_SUBDEVICE_EWX2496:
102                 mask |= ICE1712_EWX2496_AK4524_CS; /* CS high also */
103                 break;
104         case ICE1712_SUBDEVICE_DMX6FIRE:
105                 mask |= ICE1712_6FIRE_AK4524_CS_MASK; /* CS high also */
106                 break;
107         }
108         snd_ice1712_gpio_write_bits(ice, mask, mask);
109 }
110
111 static void ewx_i2c_stop(struct snd_i2c_bus *bus)
112 {
113         struct snd_ice1712 *ice = bus->private_data;
114         snd_ice1712_restore_gpio_status(ice);
115 }
116
117 static void ewx_i2c_direction(struct snd_i2c_bus *bus, int clock, int data)
118 {
119         struct snd_ice1712 *ice = bus->private_data;
120         unsigned char mask = 0;
121
122         if (clock)
123                 mask |= ICE1712_EWX2496_SERIAL_CLOCK; /* write SCL */
124         if (data)
125                 mask |= ICE1712_EWX2496_SERIAL_DATA; /* write SDA */
126         ice->gpio.direction &= ~(ICE1712_EWX2496_SERIAL_CLOCK|ICE1712_EWX2496_SERIAL_DATA);
127         ice->gpio.direction |= mask;
128         snd_ice1712_write(ice, ICE1712_IREG_GPIO_DIRECTION, ice->gpio.direction);
129         snd_ice1712_write(ice, ICE1712_IREG_GPIO_WRITE_MASK, ~mask);
130 }
131
132 static struct snd_i2c_bit_ops snd_ice1712_ewx_cs8427_bit_ops = {
133         .start = ewx_i2c_start,
134         .stop = ewx_i2c_stop,
135         .direction = ewx_i2c_direction,
136         .setlines = ewx_i2c_setlines,
137         .getclock = ewx_i2c_getclock,
138         .getdata = ewx_i2c_getdata,
139 };
140
141
142 /*
143  * AK4524 access
144  */
145
146 /* AK4524 chip select; address 0x48 bit 0-3 */
147 static int snd_ice1712_ews88mt_chip_select(struct snd_ice1712 *ice, int chip_mask)
148 {
149         struct ews_spec *spec = ice->spec;
150         unsigned char data, ndata;
151
152         snd_assert(chip_mask >= 0 && chip_mask <= 0x0f, return -EINVAL);
153         snd_i2c_lock(ice->i2c);
154         if (snd_i2c_readbytes(spec->i2cdevs[EWS_I2C_PCF2], &data, 1) != 1)
155                 goto __error;
156         ndata = (data & 0xf0) | chip_mask;
157         if (ndata != data)
158                 if (snd_i2c_sendbytes(spec->i2cdevs[EWS_I2C_PCF2], &ndata, 1)
159                     != 1)
160                         goto __error;
161         snd_i2c_unlock(ice->i2c);
162         return 0;
163
164      __error:
165         snd_i2c_unlock(ice->i2c);
166         snd_printk(KERN_ERR "AK4524 chip select failed, check cable to the front module\n");
167         return -EIO;
168 }
169
170 /* start callback for EWS88MT, needs to select a certain chip mask */
171 static void ews88mt_ak4524_lock(struct snd_akm4xxx *ak, int chip)
172 {
173         struct snd_ice1712 *ice = ak->private_data[0];
174         unsigned char tmp;
175         /* assert AK4524 CS */
176         if (snd_ice1712_ews88mt_chip_select(ice, ~(1 << chip) & 0x0f) < 0)
177                 snd_printk(KERN_ERR "fatal error (ews88mt chip select)\n");
178         snd_ice1712_save_gpio_status(ice);
179         tmp = ICE1712_EWS88_SERIAL_DATA |
180                 ICE1712_EWS88_SERIAL_CLOCK |
181                 ICE1712_EWS88_RW;
182         snd_ice1712_write(ice, ICE1712_IREG_GPIO_DIRECTION,
183                           ice->gpio.direction | tmp);
184         snd_ice1712_write(ice, ICE1712_IREG_GPIO_WRITE_MASK, ~tmp);
185 }
186
187 /* stop callback for EWS88MT, needs to deselect chip mask */
188 static void ews88mt_ak4524_unlock(struct snd_akm4xxx *ak, int chip)
189 {
190         struct snd_ice1712 *ice = ak->private_data[0];
191         snd_ice1712_restore_gpio_status(ice);
192         udelay(1);
193         snd_ice1712_ews88mt_chip_select(ice, 0x0f);
194 }
195
196 /* start callback for EWX24/96 */
197 static void ewx2496_ak4524_lock(struct snd_akm4xxx *ak, int chip)
198 {
199         struct snd_ice1712 *ice = ak->private_data[0];
200         unsigned char tmp;
201         snd_ice1712_save_gpio_status(ice);
202         tmp =  ICE1712_EWX2496_SERIAL_DATA |
203                 ICE1712_EWX2496_SERIAL_CLOCK |
204                 ICE1712_EWX2496_AK4524_CS |
205                 ICE1712_EWX2496_RW;
206         snd_ice1712_write(ice, ICE1712_IREG_GPIO_DIRECTION,
207                           ice->gpio.direction | tmp);
208         snd_ice1712_write(ice, ICE1712_IREG_GPIO_WRITE_MASK, ~tmp);
209 }
210
211 /* start callback for DMX 6fire */
212 static void dmx6fire_ak4524_lock(struct snd_akm4xxx *ak, int chip)
213 {
214         struct snd_ak4xxx_private *priv = (void *)ak->private_value[0];
215         struct snd_ice1712 *ice = ak->private_data[0];
216         unsigned char tmp;
217         snd_ice1712_save_gpio_status(ice);
218         tmp = priv->cs_mask = priv->cs_addr = (1 << chip) & ICE1712_6FIRE_AK4524_CS_MASK;
219         tmp |= ICE1712_6FIRE_SERIAL_DATA |
220                 ICE1712_6FIRE_SERIAL_CLOCK |
221                 ICE1712_6FIRE_RW;
222         snd_ice1712_write(ice, ICE1712_IREG_GPIO_DIRECTION,
223                           ice->gpio.direction | tmp);
224         snd_ice1712_write(ice, ICE1712_IREG_GPIO_WRITE_MASK, ~tmp);
225 }
226
227 /*
228  * CS8404 interface on EWS88MT/D
229  */
230
231 static void snd_ice1712_ews_cs8404_spdif_write(struct snd_ice1712 *ice, unsigned char bits)
232 {
233         struct ews_spec *spec = ice->spec;
234         unsigned char bytes[2];
235
236         snd_i2c_lock(ice->i2c);
237         switch (ice->eeprom.subvendor) {
238         case ICE1712_SUBDEVICE_EWS88MT:
239         case ICE1712_SUBDEVICE_EWS88MT_NEW:
240         case ICE1712_SUBDEVICE_PHASE88:
241                 if (snd_i2c_sendbytes(spec->i2cdevs[EWS_I2C_CS8404], &bits, 1)
242                     != 1)
243                         goto _error;
244                 break;
245         case ICE1712_SUBDEVICE_EWS88D:
246                 if (snd_i2c_readbytes(spec->i2cdevs[EWS_I2C_88D], bytes, 2)
247                     != 2)
248                         goto _error;
249                 if (bits != bytes[1]) {
250                         bytes[1] = bits;
251                         if (snd_i2c_sendbytes(spec->i2cdevs[EWS_I2C_88D],
252                                               bytes, 2) != 2)
253                                 goto _error;
254                 }
255                 break;
256         }
257  _error:
258         snd_i2c_unlock(ice->i2c);
259 }
260
261 /*
262  */
263
264 static void ews88_spdif_default_get(struct snd_ice1712 *ice, struct snd_ctl_elem_value *ucontrol)
265 {
266         snd_cs8404_decode_spdif_bits(&ucontrol->value.iec958, ice->spdif.cs8403_bits);
267 }
268
269 static int ews88_spdif_default_put(struct snd_ice1712 *ice, struct snd_ctl_elem_value *ucontrol)
270 {
271         unsigned int val;
272         int change;
273
274         val = snd_cs8404_encode_spdif_bits(&ucontrol->value.iec958);
275         spin_lock_irq(&ice->reg_lock);
276         change = ice->spdif.cs8403_bits != val;
277         ice->spdif.cs8403_bits = val;
278         if (change && ice->playback_pro_substream == NULL) {
279                 spin_unlock_irq(&ice->reg_lock);
280                 snd_ice1712_ews_cs8404_spdif_write(ice, val);
281         } else {
282                 spin_unlock_irq(&ice->reg_lock);
283         }
284         return change;
285 }
286
287 static void ews88_spdif_stream_get(struct snd_ice1712 *ice, struct snd_ctl_elem_value *ucontrol)
288 {
289         snd_cs8404_decode_spdif_bits(&ucontrol->value.iec958, ice->spdif.cs8403_stream_bits);
290 }
291
292 static int ews88_spdif_stream_put(struct snd_ice1712 *ice, struct snd_ctl_elem_value *ucontrol)
293 {
294         unsigned int val;
295         int change;
296
297         val = snd_cs8404_encode_spdif_bits(&ucontrol->value.iec958);
298         spin_lock_irq(&ice->reg_lock);
299         change = ice->spdif.cs8403_stream_bits != val;
300         ice->spdif.cs8403_stream_bits = val;
301         if (change && ice->playback_pro_substream != NULL) {
302                 spin_unlock_irq(&ice->reg_lock);
303                 snd_ice1712_ews_cs8404_spdif_write(ice, val);
304         } else {
305                 spin_unlock_irq(&ice->reg_lock);
306         }
307         return change;
308 }
309
310
311 /* open callback */
312 static void ews88_open_spdif(struct snd_ice1712 *ice, struct snd_pcm_substream *substream)
313 {
314         ice->spdif.cs8403_stream_bits = ice->spdif.cs8403_bits;
315 }
316
317 /* set up SPDIF for EWS88MT / EWS88D */
318 static void ews88_setup_spdif(struct snd_ice1712 *ice, int rate)
319 {
320         unsigned long flags;
321         unsigned char tmp;
322         int change;
323
324         spin_lock_irqsave(&ice->reg_lock, flags);
325         tmp = ice->spdif.cs8403_stream_bits;
326         if (tmp & 0x10)         /* consumer */
327                 tmp &= (tmp & 0x01) ? ~0x06 : ~0x60;
328         switch (rate) {
329         case 32000: tmp |= (tmp & 0x01) ? 0x02 : 0x00; break;
330         case 44100: tmp |= (tmp & 0x01) ? 0x06 : 0x40; break;
331         case 48000: tmp |= (tmp & 0x01) ? 0x04 : 0x20; break;
332         default: tmp |= (tmp & 0x01) ? 0x06 : 0x40; break;
333         }
334         change = ice->spdif.cs8403_stream_bits != tmp;
335         ice->spdif.cs8403_stream_bits = tmp;
336         spin_unlock_irqrestore(&ice->reg_lock, flags);
337         if (change)
338                 snd_ctl_notify(ice->card, SNDRV_CTL_EVENT_MASK_VALUE, &ice->spdif.stream_ctl->id);
339         snd_ice1712_ews_cs8404_spdif_write(ice, tmp);
340 }
341
342
343 /*
344  */
345 static struct snd_akm4xxx akm_ews88mt __devinitdata = {
346         .num_adcs = 8,
347         .num_dacs = 8,
348         .type = SND_AK4524,
349         .ops = {
350                 .lock = ews88mt_ak4524_lock,
351                 .unlock = ews88mt_ak4524_unlock
352         }
353 };
354
355 static struct snd_ak4xxx_private akm_ews88mt_priv __devinitdata = {
356         .caddr = 2,
357         .cif = 1, /* CIF high */
358         .data_mask = ICE1712_EWS88_SERIAL_DATA,
359         .clk_mask = ICE1712_EWS88_SERIAL_CLOCK,
360         .cs_mask = 0,
361         .cs_addr = 0,
362         .cs_none = 0, /* no chip select on gpio */
363         .add_flags = ICE1712_EWS88_RW, /* set rw bit high */
364         .mask_flags = 0,
365 };
366
367 static struct snd_akm4xxx akm_ewx2496 __devinitdata = {
368         .num_adcs = 2,
369         .num_dacs = 2,
370         .type = SND_AK4524,
371         .ops = {
372                 .lock = ewx2496_ak4524_lock
373         }
374 };
375
376 static struct snd_ak4xxx_private akm_ewx2496_priv __devinitdata = {
377         .caddr = 2,
378         .cif = 1, /* CIF high */
379         .data_mask = ICE1712_EWS88_SERIAL_DATA,
380         .clk_mask = ICE1712_EWS88_SERIAL_CLOCK,
381         .cs_mask = ICE1712_EWX2496_AK4524_CS,
382         .cs_addr = ICE1712_EWX2496_AK4524_CS,
383         .cs_none = 0,
384         .add_flags = ICE1712_EWS88_RW, /* set rw bit high */
385         .mask_flags = 0,
386 };
387
388 static struct snd_akm4xxx akm_6fire __devinitdata = {
389         .num_adcs = 6,
390         .num_dacs = 6,
391         .type = SND_AK4524,
392         .ops = {
393                 .lock = dmx6fire_ak4524_lock
394         }
395 };
396
397 static struct snd_ak4xxx_private akm_6fire_priv __devinitdata = {
398         .caddr = 2,
399         .cif = 1, /* CIF high */
400         .data_mask = ICE1712_6FIRE_SERIAL_DATA,
401         .clk_mask = ICE1712_6FIRE_SERIAL_CLOCK,
402         .cs_mask = 0,
403         .cs_addr = 0, /* set later */
404         .cs_none = 0,
405         .add_flags = ICE1712_6FIRE_RW, /* set rw bit high */
406         .mask_flags = 0,
407 };
408
409 /*
410  * initialize the chip
411  */
412
413 /* 6fire specific */
414 #define PCF9554_REG_INPUT      0
415 #define PCF9554_REG_OUTPUT     1
416 #define PCF9554_REG_POLARITY   2
417 #define PCF9554_REG_CONFIG     3
418
419 static int snd_ice1712_6fire_write_pca(struct snd_ice1712 *ice, unsigned char reg, unsigned char data);
420
421 static int __devinit snd_ice1712_ews_init(struct snd_ice1712 *ice)
422 {
423         int err;
424         struct snd_akm4xxx *ak;
425         struct ews_spec *spec;
426
427         /* set the analog DACs */
428         switch (ice->eeprom.subvendor) {
429         case ICE1712_SUBDEVICE_EWX2496:
430                 ice->num_total_dacs = 2;
431                 ice->num_total_adcs = 2;
432                 break;  
433         case ICE1712_SUBDEVICE_EWS88MT:
434         case ICE1712_SUBDEVICE_EWS88MT_NEW:
435         case ICE1712_SUBDEVICE_PHASE88:
436                 ice->num_total_dacs = 8;
437                 ice->num_total_adcs = 8;
438                 break;
439         case ICE1712_SUBDEVICE_EWS88D:
440                 /* Note: not analog but ADAT I/O */
441                 ice->num_total_dacs = 8;
442                 ice->num_total_adcs = 8;
443                 break;
444         case ICE1712_SUBDEVICE_DMX6FIRE:
445                 ice->num_total_dacs = 6;
446                 ice->num_total_adcs = 6;
447                 break;
448         }
449
450         spec = kzalloc(sizeof(*spec), GFP_KERNEL);
451         if (!spec)
452                 return -ENOMEM;
453         ice->spec = spec;
454
455         /* create i2c */
456         if ((err = snd_i2c_bus_create(ice->card, "ICE1712 GPIO 1", NULL, &ice->i2c)) < 0) {
457                 snd_printk(KERN_ERR "unable to create I2C bus\n");
458                 return err;
459         }
460         ice->i2c->private_data = ice;
461         ice->i2c->hw_ops.bit = &snd_ice1712_ewx_cs8427_bit_ops;
462
463         /* create i2c devices */
464         switch (ice->eeprom.subvendor) {
465         case ICE1712_SUBDEVICE_DMX6FIRE:
466                 err = snd_i2c_device_create(ice->i2c, "PCF9554",
467                                             ICE1712_6FIRE_PCF9554_ADDR,
468                                             &spec->i2cdevs[EWS_I2C_6FIRE]);
469                 if (err < 0) {
470                         snd_printk(KERN_ERR "PCF9554 initialization failed\n");
471                         return err;
472                 }
473                 snd_ice1712_6fire_write_pca(ice, PCF9554_REG_CONFIG, 0x80);
474                 break;
475         case ICE1712_SUBDEVICE_EWS88MT:
476         case ICE1712_SUBDEVICE_EWS88MT_NEW:
477         case ICE1712_SUBDEVICE_PHASE88:
478                 err = snd_i2c_device_create(ice->i2c, "CS8404",
479                                             ICE1712_EWS88MT_CS8404_ADDR,
480                                             &spec->i2cdevs[EWS_I2C_CS8404]);
481                 if (err < 0)
482                         return err;
483                 err = snd_i2c_device_create(ice->i2c, "PCF8574 (1st)",
484                                             ICE1712_EWS88MT_INPUT_ADDR,
485                                             &spec->i2cdevs[EWS_I2C_PCF1]);
486                 if (err < 0)
487                         return err;
488                 err = snd_i2c_device_create(ice->i2c, "PCF8574 (2nd)",
489                                             ICE1712_EWS88MT_OUTPUT_ADDR,
490                                             &spec->i2cdevs[EWS_I2C_PCF2]);
491                 if (err < 0)
492                         return err;
493                 /* Check if the front module is connected */
494                 if ((err = snd_ice1712_ews88mt_chip_select(ice, 0x0f)) < 0)
495                         return err;
496                 break;
497         case ICE1712_SUBDEVICE_EWS88D:
498                 err = snd_i2c_device_create(ice->i2c, "PCF8575",
499                                             ICE1712_EWS88D_PCF_ADDR,
500                                             &spec->i2cdevs[EWS_I2C_88D]);
501                 if (err < 0)
502                         return err;
503                 break;
504         }
505
506         /* set up SPDIF interface */
507         switch (ice->eeprom.subvendor) {
508         case ICE1712_SUBDEVICE_EWX2496:
509                 if ((err = snd_ice1712_init_cs8427(ice, CS8427_BASE_ADDR)) < 0)
510                         return err;
511                 snd_cs8427_reg_write(ice->cs8427, CS8427_REG_RECVERRMASK, CS8427_UNLOCK | CS8427_CONF | CS8427_BIP | CS8427_PAR);
512                 break;
513         case ICE1712_SUBDEVICE_DMX6FIRE:
514                 if ((err = snd_ice1712_init_cs8427(ice, ICE1712_6FIRE_CS8427_ADDR)) < 0)
515                         return err;
516                 snd_cs8427_reg_write(ice->cs8427, CS8427_REG_RECVERRMASK, CS8427_UNLOCK | CS8427_CONF | CS8427_BIP | CS8427_PAR);
517                 break;
518         case ICE1712_SUBDEVICE_EWS88MT:
519         case ICE1712_SUBDEVICE_EWS88MT_NEW:
520         case ICE1712_SUBDEVICE_PHASE88:
521         case ICE1712_SUBDEVICE_EWS88D:
522                 /* set up CS8404 */
523                 ice->spdif.ops.open = ews88_open_spdif;
524                 ice->spdif.ops.setup_rate = ews88_setup_spdif;
525                 ice->spdif.ops.default_get = ews88_spdif_default_get;
526                 ice->spdif.ops.default_put = ews88_spdif_default_put;
527                 ice->spdif.ops.stream_get = ews88_spdif_stream_get;
528                 ice->spdif.ops.stream_put = ews88_spdif_stream_put;
529                 /* Set spdif defaults */
530                 snd_ice1712_ews_cs8404_spdif_write(ice, ice->spdif.cs8403_bits);
531                 break;
532         }
533
534         /* no analog? */
535         switch (ice->eeprom.subvendor) {
536         case ICE1712_SUBDEVICE_EWS88D:
537                 return 0;
538         }
539
540         /* analog section */
541         ak = ice->akm = kzalloc(sizeof(struct snd_akm4xxx), GFP_KERNEL);
542         if (! ak)
543                 return -ENOMEM;
544         ice->akm_codecs = 1;
545
546         switch (ice->eeprom.subvendor) {
547         case ICE1712_SUBDEVICE_EWS88MT:
548         case ICE1712_SUBDEVICE_EWS88MT_NEW:
549         case ICE1712_SUBDEVICE_PHASE88:
550                 err = snd_ice1712_akm4xxx_init(ak, &akm_ews88mt, &akm_ews88mt_priv, ice);
551                 break;
552         case ICE1712_SUBDEVICE_EWX2496:
553                 err = snd_ice1712_akm4xxx_init(ak, &akm_ewx2496, &akm_ewx2496_priv, ice);
554                 break;
555         case ICE1712_SUBDEVICE_DMX6FIRE:
556                 err = snd_ice1712_akm4xxx_init(ak, &akm_6fire, &akm_6fire_priv, ice);
557                 break;
558         default:
559                 err = 0;
560         }
561
562         return err;
563 }
564
565 /*
566  * EWX 24/96 specific controls
567  */
568
569 /* i/o sensitivity - this callback is shared among other devices, too */
570 static int snd_ice1712_ewx_io_sense_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo){
571
572         static char *texts[2] = {
573                 "+4dBu", "-10dBV",
574         };
575         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
576         uinfo->count = 1;
577         uinfo->value.enumerated.items = 2;
578         if (uinfo->value.enumerated.item >= 2)
579                 uinfo->value.enumerated.item = 1;
580         strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
581         return 0;
582 }
583
584 static int snd_ice1712_ewx_io_sense_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
585 {
586         struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
587         unsigned char mask = kcontrol->private_value & 0xff;
588         
589         snd_ice1712_save_gpio_status(ice);
590         ucontrol->value.enumerated.item[0] = snd_ice1712_read(ice, ICE1712_IREG_GPIO_DATA) & mask ? 1 : 0;
591         snd_ice1712_restore_gpio_status(ice);
592         return 0;
593 }
594
595 static int snd_ice1712_ewx_io_sense_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
596 {
597         struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
598         unsigned char mask = kcontrol->private_value & 0xff;
599         int val, nval;
600
601         if (kcontrol->private_value & (1 << 31))
602                 return -EPERM;
603         nval = ucontrol->value.enumerated.item[0] ? mask : 0;
604         snd_ice1712_save_gpio_status(ice);
605         val = snd_ice1712_read(ice, ICE1712_IREG_GPIO_DATA);
606         nval |= val & ~mask;
607         snd_ice1712_write(ice, ICE1712_IREG_GPIO_DATA, nval);
608         snd_ice1712_restore_gpio_status(ice);
609         return val != nval;
610 }
611
612 static struct snd_kcontrol_new snd_ice1712_ewx2496_controls[] __devinitdata = {
613         {
614                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
615                 .name = "Input Sensitivity Switch",
616                 .info = snd_ice1712_ewx_io_sense_info,
617                 .get = snd_ice1712_ewx_io_sense_get,
618                 .put = snd_ice1712_ewx_io_sense_put,
619                 .private_value = ICE1712_EWX2496_AIN_SEL,
620         },
621         {
622                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
623                 .name = "Output Sensitivity Switch",
624                 .info = snd_ice1712_ewx_io_sense_info,
625                 .get = snd_ice1712_ewx_io_sense_get,
626                 .put = snd_ice1712_ewx_io_sense_put,
627                 .private_value = ICE1712_EWX2496_AOUT_SEL,
628         },
629 };
630
631
632 /*
633  * EWS88MT specific controls
634  */
635 /* analog output sensitivity;; address 0x48 bit 6 */
636 static int snd_ice1712_ews88mt_output_sense_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
637 {
638         struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
639         struct ews_spec *spec = ice->spec;
640         unsigned char data;
641
642         snd_i2c_lock(ice->i2c);
643         if (snd_i2c_readbytes(spec->i2cdevs[EWS_I2C_PCF2], &data, 1) != 1) {
644                 snd_i2c_unlock(ice->i2c);
645                 return -EIO;
646         }
647         snd_i2c_unlock(ice->i2c);
648         ucontrol->value.enumerated.item[0] = data & ICE1712_EWS88MT_OUTPUT_SENSE ? 1 : 0; /* high = -10dBV, low = +4dBu */
649         return 0;
650 }
651
652 /* analog output sensitivity;; address 0x48 bit 6 */
653 static int snd_ice1712_ews88mt_output_sense_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
654 {
655         struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
656         struct ews_spec *spec = ice->spec;
657         unsigned char data, ndata;
658
659         snd_i2c_lock(ice->i2c);
660         if (snd_i2c_readbytes(spec->i2cdevs[EWS_I2C_PCF2], &data, 1) != 1) {
661                 snd_i2c_unlock(ice->i2c);
662                 return -EIO;
663         }
664         ndata = (data & ~ICE1712_EWS88MT_OUTPUT_SENSE) | (ucontrol->value.enumerated.item[0] ? ICE1712_EWS88MT_OUTPUT_SENSE : 0);
665         if (ndata != data && snd_i2c_sendbytes(spec->i2cdevs[EWS_I2C_PCF2],
666                                                &ndata, 1) != 1) {
667                 snd_i2c_unlock(ice->i2c);
668                 return -EIO;
669         }
670         snd_i2c_unlock(ice->i2c);
671         return ndata != data;
672 }
673
674 /* analog input sensitivity; address 0x46 */
675 static int snd_ice1712_ews88mt_input_sense_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
676 {
677         struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
678         struct ews_spec *spec = ice->spec;
679         int channel = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
680         unsigned char data;
681
682         snd_assert(channel >= 0 && channel <= 7, return 0);
683         snd_i2c_lock(ice->i2c);
684         if (snd_i2c_readbytes(spec->i2cdevs[EWS_I2C_PCF1], &data, 1) != 1) {
685                 snd_i2c_unlock(ice->i2c);
686                 return -EIO;
687         }
688         /* reversed; high = +4dBu, low = -10dBV */
689         ucontrol->value.enumerated.item[0] = data & (1 << channel) ? 0 : 1;
690         snd_i2c_unlock(ice->i2c);
691         return 0;
692 }
693
694 /* analog output sensitivity; address 0x46 */
695 static int snd_ice1712_ews88mt_input_sense_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
696 {
697         struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
698         struct ews_spec *spec = ice->spec;
699         int channel = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
700         unsigned char data, ndata;
701
702         snd_assert(channel >= 0 && channel <= 7, return 0);
703         snd_i2c_lock(ice->i2c);
704         if (snd_i2c_readbytes(spec->i2cdevs[EWS_I2C_PCF1], &data, 1) != 1) {
705                 snd_i2c_unlock(ice->i2c);
706                 return -EIO;
707         }
708         ndata = (data & ~(1 << channel)) | (ucontrol->value.enumerated.item[0] ? 0 : (1 << channel));
709         if (ndata != data && snd_i2c_sendbytes(spec->i2cdevs[EWS_I2C_PCF1],
710                                                &ndata, 1) != 1) {
711                 snd_i2c_unlock(ice->i2c);
712                 return -EIO;
713         }
714         snd_i2c_unlock(ice->i2c);
715         return ndata != data;
716 }
717
718 static struct snd_kcontrol_new snd_ice1712_ews88mt_input_sense __devinitdata = {
719         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
720         .name = "Input Sensitivity Switch",
721         .info = snd_ice1712_ewx_io_sense_info,
722         .get = snd_ice1712_ews88mt_input_sense_get,
723         .put = snd_ice1712_ews88mt_input_sense_put,
724         .count = 8,
725 };
726
727 static struct snd_kcontrol_new snd_ice1712_ews88mt_output_sense __devinitdata = {
728         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
729         .name = "Output Sensitivity Switch",
730         .info = snd_ice1712_ewx_io_sense_info,
731         .get = snd_ice1712_ews88mt_output_sense_get,
732         .put = snd_ice1712_ews88mt_output_sense_put,
733 };
734
735
736 /*
737  * EWS88D specific controls
738  */
739
740 #define snd_ice1712_ews88d_control_info         snd_ctl_boolean_mono_info
741
742 static int snd_ice1712_ews88d_control_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
743 {
744         struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
745         struct ews_spec *spec = ice->spec;
746         int shift = kcontrol->private_value & 0xff;
747         int invert = (kcontrol->private_value >> 8) & 1;
748         unsigned char data[2];
749         
750         snd_i2c_lock(ice->i2c);
751         if (snd_i2c_readbytes(spec->i2cdevs[EWS_I2C_88D], data, 2) != 2) {
752                 snd_i2c_unlock(ice->i2c);
753                 return -EIO;
754         }
755         snd_i2c_unlock(ice->i2c);
756         data[0] = (data[shift >> 3] >> (shift & 7)) & 0x01;
757         if (invert)
758                 data[0] ^= 0x01;
759         ucontrol->value.integer.value[0] = data[0];
760         return 0;
761 }
762
763 static int snd_ice1712_ews88d_control_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
764 {
765         struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
766         struct ews_spec *spec = ice->spec;
767         int shift = kcontrol->private_value & 0xff;
768         int invert = (kcontrol->private_value >> 8) & 1;
769         unsigned char data[2], ndata[2];
770         int change;
771
772         snd_i2c_lock(ice->i2c);
773         if (snd_i2c_readbytes(spec->i2cdevs[EWS_I2C_88D], data, 2) != 2) {
774                 snd_i2c_unlock(ice->i2c);
775                 return -EIO;
776         }
777         ndata[shift >> 3] = data[shift >> 3] & ~(1 << (shift & 7));
778         if (invert) {
779                 if (! ucontrol->value.integer.value[0])
780                         ndata[shift >> 3] |= (1 << (shift & 7));
781         } else {
782                 if (ucontrol->value.integer.value[0])
783                         ndata[shift >> 3] |= (1 << (shift & 7));
784         }
785         change = (data[shift >> 3] != ndata[shift >> 3]);
786         if (change &&
787             snd_i2c_sendbytes(spec->i2cdevs[EWS_I2C_88D], data, 2) != 2) {
788                 snd_i2c_unlock(ice->i2c);
789                 return -EIO;
790         }
791         snd_i2c_unlock(ice->i2c);
792         return change;
793 }
794
795 #define EWS88D_CONTROL(xiface, xname, xshift, xinvert, xaccess) \
796 { .iface = xiface,\
797   .name = xname,\
798   .access = xaccess,\
799   .info = snd_ice1712_ews88d_control_info,\
800   .get = snd_ice1712_ews88d_control_get,\
801   .put = snd_ice1712_ews88d_control_put,\
802   .private_value = xshift | (xinvert << 8),\
803 }
804
805 static struct snd_kcontrol_new snd_ice1712_ews88d_controls[] __devinitdata = {
806         EWS88D_CONTROL(SNDRV_CTL_ELEM_IFACE_MIXER, "IEC958 Input Optical", 0, 1, 0), /* inverted */
807         EWS88D_CONTROL(SNDRV_CTL_ELEM_IFACE_MIXER, "ADAT Output Optical", 1, 0, 0),
808         EWS88D_CONTROL(SNDRV_CTL_ELEM_IFACE_MIXER, "ADAT External Master Clock", 2, 0, 0),
809         EWS88D_CONTROL(SNDRV_CTL_ELEM_IFACE_MIXER, "Enable ADAT", 3, 0, 0),
810         EWS88D_CONTROL(SNDRV_CTL_ELEM_IFACE_MIXER, "ADAT Through", 4, 1, 0),
811 };
812
813
814 /*
815  * DMX 6Fire specific controls
816  */
817
818 static int snd_ice1712_6fire_read_pca(struct snd_ice1712 *ice, unsigned char reg)
819 {
820         unsigned char byte;
821         struct ews_spec *spec = ice->spec;
822
823         snd_i2c_lock(ice->i2c);
824         byte = reg;
825         snd_i2c_sendbytes(spec->i2cdevs[EWS_I2C_6FIRE], &byte, 1);
826         byte = 0;
827         if (snd_i2c_readbytes(spec->i2cdevs[EWS_I2C_6FIRE], &byte, 1) != 1) {
828                 snd_i2c_unlock(ice->i2c);
829                 printk(KERN_ERR "cannot read pca\n");
830                 return -EIO;
831         }
832         snd_i2c_unlock(ice->i2c);
833         return byte;
834 }
835
836 static int snd_ice1712_6fire_write_pca(struct snd_ice1712 *ice, unsigned char reg, unsigned char data)
837 {
838         unsigned char bytes[2];
839         struct ews_spec *spec = ice->spec;
840
841         snd_i2c_lock(ice->i2c);
842         bytes[0] = reg;
843         bytes[1] = data;
844         if (snd_i2c_sendbytes(spec->i2cdevs[EWS_I2C_6FIRE], bytes, 2) != 2) {
845                 snd_i2c_unlock(ice->i2c);
846                 return -EIO;
847         }
848         snd_i2c_unlock(ice->i2c);
849         return 0;
850 }
851
852 #define snd_ice1712_6fire_control_info          snd_ctl_boolean_mono_info
853
854 static int snd_ice1712_6fire_control_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
855 {
856         struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
857         int shift = kcontrol->private_value & 0xff;
858         int invert = (kcontrol->private_value >> 8) & 1;
859         int data;
860         
861         if ((data = snd_ice1712_6fire_read_pca(ice, PCF9554_REG_OUTPUT)) < 0)
862                 return data;
863         data = (data >> shift) & 1;
864         if (invert)
865                 data ^= 1;
866         ucontrol->value.integer.value[0] = data;
867         return 0;
868 }
869
870 static int snd_ice1712_6fire_control_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
871 {
872         struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
873         int shift = kcontrol->private_value & 0xff;
874         int invert = (kcontrol->private_value >> 8) & 1;
875         int data, ndata;
876         
877         if ((data = snd_ice1712_6fire_read_pca(ice, PCF9554_REG_OUTPUT)) < 0)
878                 return data;
879         ndata = data & ~(1 << shift);
880         if (ucontrol->value.integer.value[0])
881                 ndata |= (1 << shift);
882         if (invert)
883                 ndata ^= (1 << shift);
884         if (data != ndata) {
885                 snd_ice1712_6fire_write_pca(ice, PCF9554_REG_OUTPUT, (unsigned char)ndata);
886                 return 1;
887         }
888         return 0;
889 }
890
891 static int snd_ice1712_6fire_select_input_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
892 {
893         static char *texts[4] = {
894                 "Internal", "Front Input", "Rear Input", "Wave Table"
895         };
896         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
897         uinfo->count = 1;
898         uinfo->value.enumerated.items = 4;
899         if (uinfo->value.enumerated.item >= 4)
900                 uinfo->value.enumerated.item = 1;
901         strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
902         return 0;
903 }
904      
905 static int snd_ice1712_6fire_select_input_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
906 {
907         struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
908         int data;
909         
910         if ((data = snd_ice1712_6fire_read_pca(ice, PCF9554_REG_OUTPUT)) < 0)
911                 return data;
912         ucontrol->value.integer.value[0] = data & 3;
913         return 0;
914 }
915
916 static int snd_ice1712_6fire_select_input_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
917 {
918         struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
919         int data, ndata;
920         
921         if ((data = snd_ice1712_6fire_read_pca(ice, PCF9554_REG_OUTPUT)) < 0)
922                 return data;
923         ndata = data & ~3;
924         ndata |= (ucontrol->value.integer.value[0] & 3);
925         if (data != ndata) {
926                 snd_ice1712_6fire_write_pca(ice, PCF9554_REG_OUTPUT, (unsigned char)ndata);
927                 return 1;
928         }
929         return 0;
930 }
931
932
933 #define DMX6FIRE_CONTROL(xname, xshift, xinvert) \
934 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,\
935   .name = xname,\
936   .info = snd_ice1712_6fire_control_info,\
937   .get = snd_ice1712_6fire_control_get,\
938   .put = snd_ice1712_6fire_control_put,\
939   .private_value = xshift | (xinvert << 8),\
940 }
941
942 static struct snd_kcontrol_new snd_ice1712_6fire_controls[] __devinitdata = {
943         {
944                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
945                 .name = "Analog Input Select",
946                 .info = snd_ice1712_6fire_select_input_info,
947                 .get = snd_ice1712_6fire_select_input_get,
948                 .put = snd_ice1712_6fire_select_input_put,
949         },
950         DMX6FIRE_CONTROL("Front Digital Input Switch", 2, 1),
951         // DMX6FIRE_CONTROL("Master Clock Select", 3, 0),
952         DMX6FIRE_CONTROL("Optical Digital Input Switch", 4, 0),
953         DMX6FIRE_CONTROL("Phono Analog Input Switch", 5, 0),
954         DMX6FIRE_CONTROL("Breakbox LED", 6, 0),
955 };
956
957
958 static int __devinit snd_ice1712_ews_add_controls(struct snd_ice1712 *ice)
959 {
960         unsigned int idx;
961         int err;
962         
963         /* all terratec cards have spdif, but cs8427 module builds it's own controls */
964         if (ice->cs8427 == NULL) {
965                 err = snd_ice1712_spdif_build_controls(ice);
966                 if (err < 0)
967                         return err;
968         }
969
970         /* ak4524 controls */
971         switch (ice->eeprom.subvendor) {
972         case ICE1712_SUBDEVICE_EWX2496:
973         case ICE1712_SUBDEVICE_EWS88MT:
974         case ICE1712_SUBDEVICE_EWS88MT_NEW:
975         case ICE1712_SUBDEVICE_PHASE88:
976         case ICE1712_SUBDEVICE_DMX6FIRE:
977                 err = snd_ice1712_akm4xxx_build_controls(ice);
978                 if (err < 0)
979                         return err;
980                 break;
981         }
982
983         /* card specific controls */
984         switch (ice->eeprom.subvendor) {
985         case ICE1712_SUBDEVICE_EWX2496:
986                 for (idx = 0; idx < ARRAY_SIZE(snd_ice1712_ewx2496_controls); idx++) {
987                         err = snd_ctl_add(ice->card, snd_ctl_new1(&snd_ice1712_ewx2496_controls[idx], ice));
988                         if (err < 0)
989                                 return err;
990                 }
991                 break;
992         case ICE1712_SUBDEVICE_EWS88MT:
993         case ICE1712_SUBDEVICE_EWS88MT_NEW:
994         case ICE1712_SUBDEVICE_PHASE88:
995                 err = snd_ctl_add(ice->card, snd_ctl_new1(&snd_ice1712_ews88mt_input_sense, ice));
996                 if (err < 0)
997                         return err;
998                 err = snd_ctl_add(ice->card, snd_ctl_new1(&snd_ice1712_ews88mt_output_sense, ice));
999                 if (err < 0)
1000                         return err;
1001                 break;
1002         case ICE1712_SUBDEVICE_EWS88D:
1003                 for (idx = 0; idx < ARRAY_SIZE(snd_ice1712_ews88d_controls); idx++) {
1004                         err = snd_ctl_add(ice->card, snd_ctl_new1(&snd_ice1712_ews88d_controls[idx], ice));
1005                         if (err < 0)
1006                                 return err;
1007                 }
1008                 break;
1009         case ICE1712_SUBDEVICE_DMX6FIRE:
1010                 for (idx = 0; idx < ARRAY_SIZE(snd_ice1712_6fire_controls); idx++) {
1011                         err = snd_ctl_add(ice->card, snd_ctl_new1(&snd_ice1712_6fire_controls[idx], ice));
1012                         if (err < 0)
1013                                 return err;
1014                 }
1015                 break;
1016         }
1017         return 0;
1018 }
1019
1020
1021 /* entry point */
1022 struct snd_ice1712_card_info snd_ice1712_ews_cards[] __devinitdata = {
1023         {
1024                 .subvendor = ICE1712_SUBDEVICE_EWX2496,
1025                 .name = "TerraTec EWX24/96",
1026                 .model = "ewx2496",
1027                 .chip_init = snd_ice1712_ews_init,
1028                 .build_controls = snd_ice1712_ews_add_controls,
1029         },
1030         {
1031                 .subvendor = ICE1712_SUBDEVICE_EWS88MT,
1032                 .name = "TerraTec EWS88MT",
1033                 .model = "ews88mt",
1034                 .chip_init = snd_ice1712_ews_init,
1035                 .build_controls = snd_ice1712_ews_add_controls,
1036         },
1037         {
1038                 .subvendor = ICE1712_SUBDEVICE_EWS88MT_NEW,
1039                 .name = "TerraTec EWS88MT",
1040                 .model = "ews88mt_new",
1041                 .chip_init = snd_ice1712_ews_init,
1042                 .build_controls = snd_ice1712_ews_add_controls,
1043         },
1044         {
1045                 .subvendor = ICE1712_SUBDEVICE_PHASE88,
1046                 .name = "TerraTec Phase88",
1047                 .model = "phase88",
1048                 .chip_init = snd_ice1712_ews_init,
1049                 .build_controls = snd_ice1712_ews_add_controls,
1050         },
1051         {
1052                 .subvendor = ICE1712_SUBDEVICE_EWS88D,
1053                 .name = "TerraTec EWS88D",
1054                 .model = "ews88d",
1055                 .chip_init = snd_ice1712_ews_init,
1056                 .build_controls = snd_ice1712_ews_add_controls,
1057         },
1058         {
1059                 .subvendor = ICE1712_SUBDEVICE_DMX6FIRE,
1060                 .name = "TerraTec DMX6Fire",
1061                 .model = "dmx6fire",
1062                 .chip_init = snd_ice1712_ews_init,
1063                 .build_controls = snd_ice1712_ews_add_controls,
1064                 .mpu401_1_name = "MIDI-Front DMX6fire",
1065                 .mpu401_2_name = "Wavetable DMX6fire",
1066                 .mpu401_2_info_flags = MPU401_INFO_OUTPUT,
1067         },
1068         { } /* terminator */
1069 };