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