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