xref: /kernel/linux/linux-5.10/sound/pci/ice1712/ews.c (revision 8c2ecf20)
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
25enum {
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 */
33struct 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 */
42static 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
54static 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
60static 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
77static 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
96static 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
102static 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
117static 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 */
132static 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 */
158static 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 */
175static 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 */
184static 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 */
199static 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
218static 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
252static 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
257static 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
275static 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
280static 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 */
300static 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 */
306static 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 */
333static 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
343static 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
355static 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
364static 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
376static 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
385static 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
407static int snd_ice1712_6fire_write_pca(struct snd_ice1712 *ice, unsigned char reg, unsigned char data);
408
409static 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	if ((err = snd_i2c_bus_create(ice->card, "ICE1712 GPIO 1", NULL, &ice->i2c)) < 0) {
446		dev_err(ice->card->dev, "unable to create I2C bus\n");
447		return err;
448	}
449	ice->i2c->private_data = ice;
450	ice->i2c->hw_ops.bit = &snd_ice1712_ewx_cs8427_bit_ops;
451
452	/* create i2c devices */
453	switch (ice->eeprom.subvendor) {
454	case ICE1712_SUBDEVICE_DMX6FIRE:
455		err = snd_i2c_device_create(ice->i2c, "PCF9554",
456					    ICE1712_6FIRE_PCF9554_ADDR,
457					    &spec->i2cdevs[EWS_I2C_6FIRE]);
458		if (err < 0) {
459			dev_err(ice->card->dev,
460				"PCF9554 initialization failed\n");
461			return err;
462		}
463		snd_ice1712_6fire_write_pca(ice, PCF9554_REG_CONFIG, 0x80);
464		break;
465	case ICE1712_SUBDEVICE_EWS88MT:
466	case ICE1712_SUBDEVICE_EWS88MT_NEW:
467	case ICE1712_SUBDEVICE_PHASE88:
468	case ICE1712_SUBDEVICE_TS88:
469
470		err = snd_i2c_device_create(ice->i2c, "CS8404",
471					    ICE1712_EWS88MT_CS8404_ADDR,
472					    &spec->i2cdevs[EWS_I2C_CS8404]);
473		if (err < 0)
474			return err;
475		err = snd_i2c_device_create(ice->i2c, "PCF8574 (1st)",
476					    ICE1712_EWS88MT_INPUT_ADDR,
477					    &spec->i2cdevs[EWS_I2C_PCF1]);
478		if (err < 0)
479			return err;
480		err = snd_i2c_device_create(ice->i2c, "PCF8574 (2nd)",
481					    ICE1712_EWS88MT_OUTPUT_ADDR,
482					    &spec->i2cdevs[EWS_I2C_PCF2]);
483		if (err < 0)
484			return err;
485		/* Check if the front module is connected */
486		if ((err = snd_ice1712_ews88mt_chip_select(ice, 0x0f)) < 0)
487			return err;
488		break;
489	case ICE1712_SUBDEVICE_EWS88D:
490		err = snd_i2c_device_create(ice->i2c, "PCF8575",
491					    ICE1712_EWS88D_PCF_ADDR,
492					    &spec->i2cdevs[EWS_I2C_88D]);
493		if (err < 0)
494			return err;
495		break;
496	}
497
498	/* set up SPDIF interface */
499	switch (ice->eeprom.subvendor) {
500	case ICE1712_SUBDEVICE_EWX2496:
501		if ((err = snd_ice1712_init_cs8427(ice, CS8427_BASE_ADDR)) < 0)
502			return err;
503		snd_cs8427_reg_write(ice->cs8427, CS8427_REG_RECVERRMASK, CS8427_UNLOCK | CS8427_CONF | CS8427_BIP | CS8427_PAR);
504		break;
505	case ICE1712_SUBDEVICE_DMX6FIRE:
506		if ((err = snd_ice1712_init_cs8427(ice, ICE1712_6FIRE_CS8427_ADDR)) < 0)
507			return err;
508		snd_cs8427_reg_write(ice->cs8427, CS8427_REG_RECVERRMASK, CS8427_UNLOCK | CS8427_CONF | CS8427_BIP | CS8427_PAR);
509		break;
510	case ICE1712_SUBDEVICE_EWS88MT:
511	case ICE1712_SUBDEVICE_EWS88MT_NEW:
512	case ICE1712_SUBDEVICE_PHASE88:
513	case ICE1712_SUBDEVICE_TS88:
514	case ICE1712_SUBDEVICE_EWS88D:
515		/* set up CS8404 */
516		ice->spdif.ops.open = ews88_open_spdif;
517		ice->spdif.ops.setup_rate = ews88_setup_spdif;
518		ice->spdif.ops.default_get = ews88_spdif_default_get;
519		ice->spdif.ops.default_put = ews88_spdif_default_put;
520		ice->spdif.ops.stream_get = ews88_spdif_stream_get;
521		ice->spdif.ops.stream_put = ews88_spdif_stream_put;
522		/* Set spdif defaults */
523		snd_ice1712_ews_cs8404_spdif_write(ice, ice->spdif.cs8403_bits);
524		break;
525	}
526
527	/* no analog? */
528	switch (ice->eeprom.subvendor) {
529	case ICE1712_SUBDEVICE_EWS88D:
530		return 0;
531	}
532
533	/* analog section */
534	ak = ice->akm = kzalloc(sizeof(struct snd_akm4xxx), GFP_KERNEL);
535	if (! ak)
536		return -ENOMEM;
537	ice->akm_codecs = 1;
538
539	switch (ice->eeprom.subvendor) {
540	case ICE1712_SUBDEVICE_EWS88MT:
541	case ICE1712_SUBDEVICE_EWS88MT_NEW:
542	case ICE1712_SUBDEVICE_PHASE88:
543	case ICE1712_SUBDEVICE_TS88:
544		err = snd_ice1712_akm4xxx_init(ak, &akm_ews88mt, &akm_ews88mt_priv, ice);
545		break;
546	case ICE1712_SUBDEVICE_EWX2496:
547		err = snd_ice1712_akm4xxx_init(ak, &akm_ewx2496, &akm_ewx2496_priv, ice);
548		break;
549	case ICE1712_SUBDEVICE_DMX6FIRE:
550		err = snd_ice1712_akm4xxx_init(ak, &akm_6fire, &akm_6fire_priv, ice);
551		break;
552	default:
553		err = 0;
554	}
555
556	return err;
557}
558
559/*
560 * EWX 24/96 specific controls
561 */
562
563/* i/o sensitivity - this callback is shared among other devices, too */
564static int snd_ice1712_ewx_io_sense_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo){
565
566	static const char * const texts[2] = {
567		"+4dBu", "-10dBV",
568	};
569	return snd_ctl_enum_info(uinfo, 1, 2, texts);
570}
571
572static int snd_ice1712_ewx_io_sense_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
573{
574	struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
575	unsigned char mask = kcontrol->private_value & 0xff;
576
577	snd_ice1712_save_gpio_status(ice);
578	ucontrol->value.enumerated.item[0] = snd_ice1712_read(ice, ICE1712_IREG_GPIO_DATA) & mask ? 1 : 0;
579	snd_ice1712_restore_gpio_status(ice);
580	return 0;
581}
582
583static int snd_ice1712_ewx_io_sense_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
584{
585	struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
586	unsigned char mask = kcontrol->private_value & 0xff;
587	int val, nval;
588
589	if (kcontrol->private_value & (1 << 31))
590		return -EPERM;
591	nval = ucontrol->value.enumerated.item[0] ? mask : 0;
592	snd_ice1712_save_gpio_status(ice);
593	val = snd_ice1712_read(ice, ICE1712_IREG_GPIO_DATA);
594	nval |= val & ~mask;
595	snd_ice1712_write(ice, ICE1712_IREG_GPIO_DATA, nval);
596	snd_ice1712_restore_gpio_status(ice);
597	return val != nval;
598}
599
600static const struct snd_kcontrol_new snd_ice1712_ewx2496_controls[] = {
601	{
602		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
603		.name = "Input Sensitivity Switch",
604		.info = snd_ice1712_ewx_io_sense_info,
605		.get = snd_ice1712_ewx_io_sense_get,
606		.put = snd_ice1712_ewx_io_sense_put,
607		.private_value = ICE1712_EWX2496_AIN_SEL,
608	},
609	{
610		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
611		.name = "Output Sensitivity Switch",
612		.info = snd_ice1712_ewx_io_sense_info,
613		.get = snd_ice1712_ewx_io_sense_get,
614		.put = snd_ice1712_ewx_io_sense_put,
615		.private_value = ICE1712_EWX2496_AOUT_SEL,
616	},
617};
618
619
620/*
621 * EWS88MT specific controls
622 */
623/* analog output sensitivity;; address 0x48 bit 6 */
624static int snd_ice1712_ews88mt_output_sense_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
625{
626	struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
627	struct ews_spec *spec = ice->spec;
628	unsigned char data;
629
630	snd_i2c_lock(ice->i2c);
631	if (snd_i2c_readbytes(spec->i2cdevs[EWS_I2C_PCF2], &data, 1) != 1) {
632		snd_i2c_unlock(ice->i2c);
633		return -EIO;
634	}
635	snd_i2c_unlock(ice->i2c);
636	ucontrol->value.enumerated.item[0] = data & ICE1712_EWS88MT_OUTPUT_SENSE ? 1 : 0; /* high = -10dBV, low = +4dBu */
637	return 0;
638}
639
640/* analog output sensitivity;; address 0x48 bit 6 */
641static int snd_ice1712_ews88mt_output_sense_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
642{
643	struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
644	struct ews_spec *spec = ice->spec;
645	unsigned char data, ndata;
646
647	snd_i2c_lock(ice->i2c);
648	if (snd_i2c_readbytes(spec->i2cdevs[EWS_I2C_PCF2], &data, 1) != 1) {
649		snd_i2c_unlock(ice->i2c);
650		return -EIO;
651	}
652	ndata = (data & ~ICE1712_EWS88MT_OUTPUT_SENSE) | (ucontrol->value.enumerated.item[0] ? ICE1712_EWS88MT_OUTPUT_SENSE : 0);
653	if (ndata != data && snd_i2c_sendbytes(spec->i2cdevs[EWS_I2C_PCF2],
654					       &ndata, 1) != 1) {
655		snd_i2c_unlock(ice->i2c);
656		return -EIO;
657	}
658	snd_i2c_unlock(ice->i2c);
659	return ndata != data;
660}
661
662/* analog input sensitivity; address 0x46 */
663static int snd_ice1712_ews88mt_input_sense_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
664{
665	struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
666	struct ews_spec *spec = ice->spec;
667	int channel = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
668	unsigned char data;
669
670	if (snd_BUG_ON(channel < 0 || channel > 7))
671		return 0;
672	snd_i2c_lock(ice->i2c);
673	if (snd_i2c_readbytes(spec->i2cdevs[EWS_I2C_PCF1], &data, 1) != 1) {
674		snd_i2c_unlock(ice->i2c);
675		return -EIO;
676	}
677	/* reversed; high = +4dBu, low = -10dBV */
678	ucontrol->value.enumerated.item[0] = data & (1 << channel) ? 0 : 1;
679	snd_i2c_unlock(ice->i2c);
680	return 0;
681}
682
683/* analog output sensitivity; address 0x46 */
684static int snd_ice1712_ews88mt_input_sense_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
685{
686	struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
687	struct ews_spec *spec = ice->spec;
688	int channel = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
689	unsigned char data, ndata;
690
691	if (snd_BUG_ON(channel < 0 || channel > 7))
692		return 0;
693	snd_i2c_lock(ice->i2c);
694	if (snd_i2c_readbytes(spec->i2cdevs[EWS_I2C_PCF1], &data, 1) != 1) {
695		snd_i2c_unlock(ice->i2c);
696		return -EIO;
697	}
698	ndata = (data & ~(1 << channel)) | (ucontrol->value.enumerated.item[0] ? 0 : (1 << channel));
699	if (ndata != data && snd_i2c_sendbytes(spec->i2cdevs[EWS_I2C_PCF1],
700					       &ndata, 1) != 1) {
701		snd_i2c_unlock(ice->i2c);
702		return -EIO;
703	}
704	snd_i2c_unlock(ice->i2c);
705	return ndata != data;
706}
707
708static const struct snd_kcontrol_new snd_ice1712_ews88mt_input_sense = {
709	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
710	.name = "Input Sensitivity Switch",
711	.info = snd_ice1712_ewx_io_sense_info,
712	.get = snd_ice1712_ews88mt_input_sense_get,
713	.put = snd_ice1712_ews88mt_input_sense_put,
714	.count = 8,
715};
716
717static const struct snd_kcontrol_new snd_ice1712_ews88mt_output_sense = {
718	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
719	.name = "Output Sensitivity Switch",
720	.info = snd_ice1712_ewx_io_sense_info,
721	.get = snd_ice1712_ews88mt_output_sense_get,
722	.put = snd_ice1712_ews88mt_output_sense_put,
723};
724
725
726/*
727 * EWS88D specific controls
728 */
729
730#define snd_ice1712_ews88d_control_info		snd_ctl_boolean_mono_info
731
732static int snd_ice1712_ews88d_control_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
733{
734	struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
735	struct ews_spec *spec = ice->spec;
736	int shift = kcontrol->private_value & 0xff;
737	int invert = (kcontrol->private_value >> 8) & 1;
738	unsigned char data[2];
739
740	snd_i2c_lock(ice->i2c);
741	if (snd_i2c_readbytes(spec->i2cdevs[EWS_I2C_88D], data, 2) != 2) {
742		snd_i2c_unlock(ice->i2c);
743		return -EIO;
744	}
745	snd_i2c_unlock(ice->i2c);
746	data[0] = (data[shift >> 3] >> (shift & 7)) & 0x01;
747	if (invert)
748		data[0] ^= 0x01;
749	ucontrol->value.integer.value[0] = data[0];
750	return 0;
751}
752
753static int snd_ice1712_ews88d_control_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
754{
755	struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
756	struct ews_spec *spec = ice->spec;
757	int shift = kcontrol->private_value & 0xff;
758	int invert = (kcontrol->private_value >> 8) & 1;
759	unsigned char data[2], ndata[2];
760	int change;
761
762	snd_i2c_lock(ice->i2c);
763	if (snd_i2c_readbytes(spec->i2cdevs[EWS_I2C_88D], data, 2) != 2) {
764		snd_i2c_unlock(ice->i2c);
765		return -EIO;
766	}
767	ndata[shift >> 3] = data[shift >> 3] & ~(1 << (shift & 7));
768	if (invert) {
769		if (! ucontrol->value.integer.value[0])
770			ndata[shift >> 3] |= (1 << (shift & 7));
771	} else {
772		if (ucontrol->value.integer.value[0])
773			ndata[shift >> 3] |= (1 << (shift & 7));
774	}
775	change = (data[shift >> 3] != ndata[shift >> 3]);
776	if (change &&
777	    snd_i2c_sendbytes(spec->i2cdevs[EWS_I2C_88D], data, 2) != 2) {
778		snd_i2c_unlock(ice->i2c);
779		return -EIO;
780	}
781	snd_i2c_unlock(ice->i2c);
782	return change;
783}
784
785#define EWS88D_CONTROL(xiface, xname, xshift, xinvert, xaccess) \
786{ .iface = xiface,\
787  .name = xname,\
788  .access = xaccess,\
789  .info = snd_ice1712_ews88d_control_info,\
790  .get = snd_ice1712_ews88d_control_get,\
791  .put = snd_ice1712_ews88d_control_put,\
792  .private_value = xshift | (xinvert << 8),\
793}
794
795static const struct snd_kcontrol_new snd_ice1712_ews88d_controls[] = {
796	EWS88D_CONTROL(SNDRV_CTL_ELEM_IFACE_MIXER, "IEC958 Input Optical", 0, 1, 0), /* inverted */
797	EWS88D_CONTROL(SNDRV_CTL_ELEM_IFACE_MIXER, "ADAT Output Optical", 1, 0, 0),
798	EWS88D_CONTROL(SNDRV_CTL_ELEM_IFACE_MIXER, "ADAT External Master Clock", 2, 0, 0),
799	EWS88D_CONTROL(SNDRV_CTL_ELEM_IFACE_MIXER, "Enable ADAT", 3, 0, 0),
800	EWS88D_CONTROL(SNDRV_CTL_ELEM_IFACE_MIXER, "ADAT Through", 4, 1, 0),
801};
802
803
804/*
805 * DMX 6Fire specific controls
806 */
807
808static int snd_ice1712_6fire_read_pca(struct snd_ice1712 *ice, unsigned char reg)
809{
810	unsigned char byte;
811	struct ews_spec *spec = ice->spec;
812
813	snd_i2c_lock(ice->i2c);
814	byte = reg;
815	if (snd_i2c_sendbytes(spec->i2cdevs[EWS_I2C_6FIRE], &byte, 1) != 1) {
816		snd_i2c_unlock(ice->i2c);
817		dev_err(ice->card->dev, "cannot send pca\n");
818		return -EIO;
819	}
820
821	byte = 0;
822	if (snd_i2c_readbytes(spec->i2cdevs[EWS_I2C_6FIRE], &byte, 1) != 1) {
823		snd_i2c_unlock(ice->i2c);
824		dev_err(ice->card->dev, "cannot read pca\n");
825		return -EIO;
826	}
827	snd_i2c_unlock(ice->i2c);
828	return byte;
829}
830
831static int snd_ice1712_6fire_write_pca(struct snd_ice1712 *ice, unsigned char reg, unsigned char data)
832{
833	unsigned char bytes[2];
834	struct ews_spec *spec = ice->spec;
835
836	snd_i2c_lock(ice->i2c);
837	bytes[0] = reg;
838	bytes[1] = data;
839	if (snd_i2c_sendbytes(spec->i2cdevs[EWS_I2C_6FIRE], bytes, 2) != 2) {
840		snd_i2c_unlock(ice->i2c);
841		return -EIO;
842	}
843	snd_i2c_unlock(ice->i2c);
844	return 0;
845}
846
847#define snd_ice1712_6fire_control_info		snd_ctl_boolean_mono_info
848
849static int snd_ice1712_6fire_control_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
850{
851	struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
852	int shift = kcontrol->private_value & 0xff;
853	int invert = (kcontrol->private_value >> 8) & 1;
854	int data;
855
856	if ((data = snd_ice1712_6fire_read_pca(ice, PCF9554_REG_OUTPUT)) < 0)
857		return data;
858	data = (data >> shift) & 1;
859	if (invert)
860		data ^= 1;
861	ucontrol->value.integer.value[0] = data;
862	return 0;
863}
864
865static int snd_ice1712_6fire_control_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
866{
867	struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
868	int shift = kcontrol->private_value & 0xff;
869	int invert = (kcontrol->private_value >> 8) & 1;
870	int data, ndata;
871
872	if ((data = snd_ice1712_6fire_read_pca(ice, PCF9554_REG_OUTPUT)) < 0)
873		return data;
874	ndata = data & ~(1 << shift);
875	if (ucontrol->value.integer.value[0])
876		ndata |= (1 << shift);
877	if (invert)
878		ndata ^= (1 << shift);
879	if (data != ndata) {
880		snd_ice1712_6fire_write_pca(ice, PCF9554_REG_OUTPUT, (unsigned char)ndata);
881		return 1;
882	}
883	return 0;
884}
885
886static int snd_ice1712_6fire_select_input_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
887{
888	static const char * const texts[4] = {
889		"Internal", "Front Input", "Rear Input", "Wave Table"
890	};
891	return snd_ctl_enum_info(uinfo, 1, 4, texts);
892}
893
894static int snd_ice1712_6fire_select_input_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
895{
896	struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
897	int data;
898
899	if ((data = snd_ice1712_6fire_read_pca(ice, PCF9554_REG_OUTPUT)) < 0)
900		return data;
901	ucontrol->value.integer.value[0] = data & 3;
902	return 0;
903}
904
905static int snd_ice1712_6fire_select_input_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
906{
907	struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
908	int data, ndata;
909
910	if ((data = snd_ice1712_6fire_read_pca(ice, PCF9554_REG_OUTPUT)) < 0)
911		return data;
912	ndata = data & ~3;
913	ndata |= (ucontrol->value.integer.value[0] & 3);
914	if (data != ndata) {
915		snd_ice1712_6fire_write_pca(ice, PCF9554_REG_OUTPUT, (unsigned char)ndata);
916		return 1;
917	}
918	return 0;
919}
920
921
922#define DMX6FIRE_CONTROL(xname, xshift, xinvert) \
923{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER,\
924  .name = xname,\
925  .info = snd_ice1712_6fire_control_info,\
926  .get = snd_ice1712_6fire_control_get,\
927  .put = snd_ice1712_6fire_control_put,\
928  .private_value = xshift | (xinvert << 8),\
929}
930
931static const struct snd_kcontrol_new snd_ice1712_6fire_controls[] = {
932	{
933		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
934		.name = "Analog Input Select",
935		.info = snd_ice1712_6fire_select_input_info,
936		.get = snd_ice1712_6fire_select_input_get,
937		.put = snd_ice1712_6fire_select_input_put,
938	},
939	DMX6FIRE_CONTROL("Front Digital Input Switch", 2, 1),
940	// DMX6FIRE_CONTROL("Master Clock Select", 3, 0),
941	DMX6FIRE_CONTROL("Optical Digital Input Switch", 4, 0),
942	DMX6FIRE_CONTROL("Phono Analog Input Switch", 5, 0),
943	DMX6FIRE_CONTROL("Breakbox LED", 6, 0),
944};
945
946
947static int snd_ice1712_ews_add_controls(struct snd_ice1712 *ice)
948{
949	unsigned int idx;
950	int err;
951
952	/* all terratec cards have spdif, but cs8427 module builds it's own controls */
953	if (ice->cs8427 == NULL) {
954		err = snd_ice1712_spdif_build_controls(ice);
955		if (err < 0)
956			return err;
957	}
958
959	/* ak4524 controls */
960	switch (ice->eeprom.subvendor) {
961	case ICE1712_SUBDEVICE_EWX2496:
962	case ICE1712_SUBDEVICE_EWS88MT:
963	case ICE1712_SUBDEVICE_EWS88MT_NEW:
964	case ICE1712_SUBDEVICE_PHASE88:
965	case ICE1712_SUBDEVICE_TS88:
966	case ICE1712_SUBDEVICE_DMX6FIRE:
967		err = snd_ice1712_akm4xxx_build_controls(ice);
968		if (err < 0)
969			return err;
970		break;
971	}
972
973	/* card specific controls */
974	switch (ice->eeprom.subvendor) {
975	case ICE1712_SUBDEVICE_EWX2496:
976		for (idx = 0; idx < ARRAY_SIZE(snd_ice1712_ewx2496_controls); idx++) {
977			err = snd_ctl_add(ice->card, snd_ctl_new1(&snd_ice1712_ewx2496_controls[idx], ice));
978			if (err < 0)
979				return err;
980		}
981		break;
982	case ICE1712_SUBDEVICE_EWS88MT:
983	case ICE1712_SUBDEVICE_EWS88MT_NEW:
984	case ICE1712_SUBDEVICE_PHASE88:
985	case ICE1712_SUBDEVICE_TS88:
986		err = snd_ctl_add(ice->card, snd_ctl_new1(&snd_ice1712_ews88mt_input_sense, ice));
987		if (err < 0)
988			return err;
989		err = snd_ctl_add(ice->card, snd_ctl_new1(&snd_ice1712_ews88mt_output_sense, ice));
990		if (err < 0)
991			return err;
992		break;
993	case ICE1712_SUBDEVICE_EWS88D:
994		for (idx = 0; idx < ARRAY_SIZE(snd_ice1712_ews88d_controls); idx++) {
995			err = snd_ctl_add(ice->card, snd_ctl_new1(&snd_ice1712_ews88d_controls[idx], ice));
996			if (err < 0)
997				return err;
998		}
999		break;
1000	case ICE1712_SUBDEVICE_DMX6FIRE:
1001		for (idx = 0; idx < ARRAY_SIZE(snd_ice1712_6fire_controls); idx++) {
1002			err = snd_ctl_add(ice->card, snd_ctl_new1(&snd_ice1712_6fire_controls[idx], ice));
1003			if (err < 0)
1004				return err;
1005		}
1006		break;
1007	}
1008	return 0;
1009}
1010
1011
1012/* entry point */
1013struct snd_ice1712_card_info snd_ice1712_ews_cards[] = {
1014	{
1015		.subvendor = ICE1712_SUBDEVICE_EWX2496,
1016		.name = "TerraTec EWX24/96",
1017		.model = "ewx2496",
1018		.chip_init = snd_ice1712_ews_init,
1019		.build_controls = snd_ice1712_ews_add_controls,
1020	},
1021	{
1022		.subvendor = ICE1712_SUBDEVICE_EWS88MT,
1023		.name = "TerraTec EWS88MT",
1024		.model = "ews88mt",
1025		.chip_init = snd_ice1712_ews_init,
1026		.build_controls = snd_ice1712_ews_add_controls,
1027	},
1028	{
1029		.subvendor = ICE1712_SUBDEVICE_EWS88MT_NEW,
1030		.name = "TerraTec EWS88MT",
1031		.model = "ews88mt_new",
1032		.chip_init = snd_ice1712_ews_init,
1033		.build_controls = snd_ice1712_ews_add_controls,
1034	},
1035	{
1036		.subvendor = ICE1712_SUBDEVICE_PHASE88,
1037		.name = "TerraTec Phase88",
1038		.model = "phase88",
1039		.chip_init = snd_ice1712_ews_init,
1040		.build_controls = snd_ice1712_ews_add_controls,
1041	},
1042	{
1043		.subvendor = ICE1712_SUBDEVICE_TS88,
1044		.name = "terrasoniq TS88",
1045		.model = "phase88",
1046		.chip_init = snd_ice1712_ews_init,
1047		.build_controls = snd_ice1712_ews_add_controls,
1048	},
1049	{
1050		.subvendor = ICE1712_SUBDEVICE_EWS88D,
1051		.name = "TerraTec EWS88D",
1052		.model = "ews88d",
1053		.chip_init = snd_ice1712_ews_init,
1054		.build_controls = snd_ice1712_ews_add_controls,
1055	},
1056	{
1057		.subvendor = ICE1712_SUBDEVICE_DMX6FIRE,
1058		.name = "TerraTec DMX6Fire",
1059		.model = "dmx6fire",
1060		.chip_init = snd_ice1712_ews_init,
1061		.build_controls = snd_ice1712_ews_add_controls,
1062		.mpu401_1_name = "MIDI-Front DMX6fire",
1063		.mpu401_2_name = "Wavetable DMX6fire",
1064		.mpu401_2_info_flags = MPU401_INFO_OUTPUT,
1065	},
1066	{ } /* terminator */
1067};
1068