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