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