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