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