xref: /openbmc/linux/sound/pci/ice1712/quartet.c (revision 4f6cce39)
1 /*
2  *   ALSA driver for ICEnsemble VT1724 (Envy24HT)
3  *
4  *   Lowlevel functions for Infrasonic Quartet
5  *
6  *	Copyright (c) 2009 Pavel Hofman <pavel.hofman@ivitera.com>
7  *
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 <linux/delay.h>
26 #include <linux/interrupt.h>
27 #include <linux/init.h>
28 #include <linux/slab.h>
29 #include <sound/core.h>
30 #include <sound/tlv.h>
31 #include <sound/info.h>
32 
33 #include "ice1712.h"
34 #include "envy24ht.h"
35 #include <sound/ak4113.h>
36 #include "quartet.h"
37 
38 struct qtet_spec {
39 	struct ak4113 *ak4113;
40 	unsigned int scr;	/* system control register */
41 	unsigned int mcr;	/* monitoring control register */
42 	unsigned int cpld;	/* cpld register */
43 };
44 
45 struct qtet_kcontrol_private {
46 	unsigned int bit;
47 	void (*set_register)(struct snd_ice1712 *ice, unsigned int val);
48 	unsigned int (*get_register)(struct snd_ice1712 *ice);
49 	const char * const texts[2];
50 };
51 
52 enum {
53 	IN12_SEL = 0,
54 	IN34_SEL,
55 	AIN34_SEL,
56 	COAX_OUT,
57 	IN12_MON12,
58 	IN12_MON34,
59 	IN34_MON12,
60 	IN34_MON34,
61 	OUT12_MON34,
62 	OUT34_MON12,
63 };
64 
65 static const char * const ext_clock_names[3] = {"IEC958 In", "Word Clock 1xFS",
66 	"Word Clock 256xFS"};
67 
68 /* chip address on I2C bus */
69 #define AK4113_ADDR		0x26	/* S/PDIF receiver */
70 
71 /* chip address on SPI bus */
72 #define AK4620_ADDR		0x02	/* ADC/DAC */
73 
74 
75 /*
76  * GPIO pins
77  */
78 
79 /* GPIO0 - O - DATA0, def. 0 */
80 #define GPIO_D0			(1<<0)
81 /* GPIO1 - I/O - DATA1, Jack Detect Input0 (0:present, 1:missing), def. 1 */
82 #define GPIO_D1_JACKDTC0	(1<<1)
83 /* GPIO2 - I/O - DATA2, Jack Detect Input1 (0:present, 1:missing), def. 1 */
84 #define GPIO_D2_JACKDTC1	(1<<2)
85 /* GPIO3 - I/O - DATA3, def. 1 */
86 #define GPIO_D3			(1<<3)
87 /* GPIO4 - I/O - DATA4, SPI CDTO, def. 1 */
88 #define GPIO_D4_SPI_CDTO	(1<<4)
89 /* GPIO5 - I/O - DATA5, SPI CCLK, def. 1 */
90 #define GPIO_D5_SPI_CCLK	(1<<5)
91 /* GPIO6 - I/O - DATA6, Cable Detect Input (0:detected, 1:not detected */
92 #define GPIO_D6_CD		(1<<6)
93 /* GPIO7 - I/O - DATA7, Device Detect Input (0:detected, 1:not detected */
94 #define GPIO_D7_DD		(1<<7)
95 /* GPIO8 - O - CPLD Chip Select, def. 1 */
96 #define GPIO_CPLD_CSN		(1<<8)
97 /* GPIO9 - O - CPLD register read/write (0:write, 1:read), def. 0 */
98 #define GPIO_CPLD_RW		(1<<9)
99 /* GPIO10 - O - SPI Chip Select for CODEC#0, def. 1 */
100 #define GPIO_SPI_CSN0		(1<<10)
101 /* GPIO11 - O - SPI Chip Select for CODEC#1, def. 1 */
102 #define GPIO_SPI_CSN1		(1<<11)
103 /* GPIO12 - O - Ex. Register Output Enable (0:enable, 1:disable), def. 1,
104  * init 0 */
105 #define GPIO_EX_GPIOE		(1<<12)
106 /* GPIO13 - O - Ex. Register0 Chip Select for System Control Register,
107  * def. 1 */
108 #define GPIO_SCR		(1<<13)
109 /* GPIO14 - O - Ex. Register1 Chip Select for Monitor Control Register,
110  * def. 1 */
111 #define GPIO_MCR		(1<<14)
112 
113 #define GPIO_SPI_ALL		(GPIO_D4_SPI_CDTO | GPIO_D5_SPI_CCLK |\
114 		GPIO_SPI_CSN0 | GPIO_SPI_CSN1)
115 
116 #define GPIO_DATA_MASK		(GPIO_D0 | GPIO_D1_JACKDTC0 | \
117 		GPIO_D2_JACKDTC1 | GPIO_D3 | \
118 		GPIO_D4_SPI_CDTO | GPIO_D5_SPI_CCLK | \
119 		GPIO_D6_CD | GPIO_D7_DD)
120 
121 /* System Control Register GPIO_SCR data bits */
122 /* Mic/Line select relay (0:line, 1:mic) */
123 #define SCR_RELAY		GPIO_D0
124 /* Phantom power drive control (0:5V, 1:48V) */
125 #define SCR_PHP_V		GPIO_D1_JACKDTC0
126 /* H/W mute control (0:Normal, 1:Mute) */
127 #define SCR_MUTE		GPIO_D2_JACKDTC1
128 /* Phantom power control (0:Phantom on, 1:off) */
129 #define SCR_PHP			GPIO_D3
130 /* Analog input 1/2 Source Select */
131 #define SCR_AIN12_SEL0		GPIO_D4_SPI_CDTO
132 #define SCR_AIN12_SEL1		GPIO_D5_SPI_CCLK
133 /* Analog input 3/4 Source Select (0:line, 1:hi-z) */
134 #define SCR_AIN34_SEL		GPIO_D6_CD
135 /* Codec Power Down (0:power down, 1:normal) */
136 #define SCR_CODEC_PDN		GPIO_D7_DD
137 
138 #define SCR_AIN12_LINE		(0)
139 #define SCR_AIN12_MIC		(SCR_AIN12_SEL0)
140 #define SCR_AIN12_LOWCUT	(SCR_AIN12_SEL1 | SCR_AIN12_SEL0)
141 
142 /* Monitor Control Register GPIO_MCR data bits */
143 /* Input 1/2 to Monitor 1/2 (0:off, 1:on) */
144 #define MCR_IN12_MON12		GPIO_D0
145 /* Input 1/2 to Monitor 3/4 (0:off, 1:on) */
146 #define MCR_IN12_MON34		GPIO_D1_JACKDTC0
147 /* Input 3/4 to Monitor 1/2 (0:off, 1:on) */
148 #define MCR_IN34_MON12		GPIO_D2_JACKDTC1
149 /* Input 3/4 to Monitor 3/4 (0:off, 1:on) */
150 #define MCR_IN34_MON34		GPIO_D3
151 /* Output to Monitor 1/2 (0:off, 1:on) */
152 #define MCR_OUT34_MON12		GPIO_D4_SPI_CDTO
153 /* Output to Monitor 3/4 (0:off, 1:on) */
154 #define MCR_OUT12_MON34		GPIO_D5_SPI_CCLK
155 
156 /* CPLD Register DATA bits */
157 /* Clock Rate Select */
158 #define CPLD_CKS0		GPIO_D0
159 #define CPLD_CKS1		GPIO_D1_JACKDTC0
160 #define CPLD_CKS2		GPIO_D2_JACKDTC1
161 /* Sync Source Select (0:Internal, 1:External) */
162 #define CPLD_SYNC_SEL		GPIO_D3
163 /* Word Clock FS Select (0:FS, 1:256FS) */
164 #define CPLD_WORD_SEL		GPIO_D4_SPI_CDTO
165 /* Coaxial Output Source (IS-Link) (0:SPDIF, 1:I2S) */
166 #define CPLD_COAX_OUT		GPIO_D5_SPI_CCLK
167 /* Input 1/2 Source Select (0:Analog12, 1:An34) */
168 #define CPLD_IN12_SEL		GPIO_D6_CD
169 /* Input 3/4 Source Select (0:Analog34, 1:Digital In) */
170 #define CPLD_IN34_SEL		GPIO_D7_DD
171 
172 /* internal clock (CPLD_SYNC_SEL = 0) options */
173 #define CPLD_CKS_44100HZ	(0)
174 #define CPLD_CKS_48000HZ	(CPLD_CKS0)
175 #define CPLD_CKS_88200HZ	(CPLD_CKS1)
176 #define CPLD_CKS_96000HZ	(CPLD_CKS1 | CPLD_CKS0)
177 #define CPLD_CKS_176400HZ	(CPLD_CKS2)
178 #define CPLD_CKS_192000HZ	(CPLD_CKS2 | CPLD_CKS0)
179 
180 #define CPLD_CKS_MASK		(CPLD_CKS0 | CPLD_CKS1 | CPLD_CKS2)
181 
182 /* external clock (CPLD_SYNC_SEL = 1) options */
183 /* external clock - SPDIF */
184 #define CPLD_EXT_SPDIF	(0 | CPLD_SYNC_SEL)
185 /* external clock - WordClock 1xfs */
186 #define CPLD_EXT_WORDCLOCK_1FS	(CPLD_CKS1 | CPLD_SYNC_SEL)
187 /* external clock - WordClock 256xfs */
188 #define CPLD_EXT_WORDCLOCK_256FS	(CPLD_CKS1 | CPLD_WORD_SEL |\
189 		CPLD_SYNC_SEL)
190 
191 #define EXT_SPDIF_TYPE			0
192 #define EXT_WORDCLOCK_1FS_TYPE		1
193 #define EXT_WORDCLOCK_256FS_TYPE	2
194 
195 #define AK4620_DFS0		(1<<0)
196 #define AK4620_DFS1		(1<<1)
197 #define AK4620_CKS0		(1<<2)
198 #define AK4620_CKS1		(1<<3)
199 /* Clock and Format Control register */
200 #define AK4620_DFS_REG		0x02
201 
202 /* Deem and Volume Control register */
203 #define AK4620_DEEMVOL_REG	0x03
204 #define AK4620_SMUTE		(1<<7)
205 
206 /*
207  * Conversion from int value to its binary form. Used for debugging.
208  * The output buffer must be allocated prior to calling the function.
209  */
210 static char *get_binary(char *buffer, int value)
211 {
212 	int i, j, pos;
213 	pos = 0;
214 	for (i = 0; i < 4; ++i) {
215 		for (j = 0; j < 8; ++j) {
216 			if (value & (1 << (31-(i*8 + j))))
217 				buffer[pos] = '1';
218 			else
219 				buffer[pos] = '0';
220 			pos++;
221 		}
222 		if (i < 3) {
223 			buffer[pos] = ' ';
224 			pos++;
225 		}
226 	}
227 	buffer[pos] = '\0';
228 	return buffer;
229 }
230 
231 /*
232  * Initial setup of the conversion array GPIO <-> rate
233  */
234 static unsigned int qtet_rates[] = {
235 	44100, 48000, 88200,
236 	96000, 176400, 192000,
237 };
238 
239 static unsigned int cks_vals[] = {
240 	CPLD_CKS_44100HZ, CPLD_CKS_48000HZ, CPLD_CKS_88200HZ,
241 	CPLD_CKS_96000HZ, CPLD_CKS_176400HZ, CPLD_CKS_192000HZ,
242 };
243 
244 static struct snd_pcm_hw_constraint_list qtet_rates_info = {
245 	.count = ARRAY_SIZE(qtet_rates),
246 	.list = qtet_rates,
247 	.mask = 0,
248 };
249 
250 static void qtet_ak4113_write(void *private_data, unsigned char reg,
251 		unsigned char val)
252 {
253 	snd_vt1724_write_i2c((struct snd_ice1712 *)private_data, AK4113_ADDR,
254 			reg, val);
255 }
256 
257 static unsigned char qtet_ak4113_read(void *private_data, unsigned char reg)
258 {
259 	return snd_vt1724_read_i2c((struct snd_ice1712 *)private_data,
260 			AK4113_ADDR, reg);
261 }
262 
263 
264 /*
265  * AK4620 section
266  */
267 
268 /*
269  * Write data to addr register of ak4620
270  */
271 static void qtet_akm_write(struct snd_akm4xxx *ak, int chip,
272 		unsigned char addr, unsigned char data)
273 {
274 	unsigned int tmp, orig_dir;
275 	int idx;
276 	unsigned int addrdata;
277 	struct snd_ice1712 *ice = ak->private_data[0];
278 
279 	if (snd_BUG_ON(chip < 0 || chip >= 4))
280 		return;
281 	/*dev_dbg(ice->card->dev, "Writing to AK4620: chip=%d, addr=0x%x,
282 	  data=0x%x\n", chip, addr, data);*/
283 	orig_dir = ice->gpio.get_dir(ice);
284 	ice->gpio.set_dir(ice, orig_dir | GPIO_SPI_ALL);
285 	/* set mask - only SPI bits */
286 	ice->gpio.set_mask(ice, ~GPIO_SPI_ALL);
287 
288 	tmp = ice->gpio.get_data(ice);
289 	/* high all */
290 	tmp |= GPIO_SPI_ALL;
291 	ice->gpio.set_data(ice, tmp);
292 	udelay(100);
293 	/* drop chip select */
294 	if (chip)
295 		/* CODEC 1 */
296 		tmp &= ~GPIO_SPI_CSN1;
297 	else
298 		tmp &= ~GPIO_SPI_CSN0;
299 	ice->gpio.set_data(ice, tmp);
300 	udelay(100);
301 
302 	/* build I2C address + data byte */
303 	addrdata = (AK4620_ADDR << 6) | 0x20 | (addr & 0x1f);
304 	addrdata = (addrdata << 8) | data;
305 	for (idx = 15; idx >= 0; idx--) {
306 		/* drop clock */
307 		tmp &= ~GPIO_D5_SPI_CCLK;
308 		ice->gpio.set_data(ice, tmp);
309 		udelay(100);
310 		/* set data */
311 		if (addrdata & (1 << idx))
312 			tmp |= GPIO_D4_SPI_CDTO;
313 		else
314 			tmp &= ~GPIO_D4_SPI_CDTO;
315 		ice->gpio.set_data(ice, tmp);
316 		udelay(100);
317 		/* raise clock */
318 		tmp |= GPIO_D5_SPI_CCLK;
319 		ice->gpio.set_data(ice, tmp);
320 		udelay(100);
321 	}
322 	/* all back to 1 */
323 	tmp |= GPIO_SPI_ALL;
324 	ice->gpio.set_data(ice, tmp);
325 	udelay(100);
326 
327 	/* return all gpios to non-writable */
328 	ice->gpio.set_mask(ice, 0xffffff);
329 	/* restore GPIOs direction */
330 	ice->gpio.set_dir(ice, orig_dir);
331 }
332 
333 static void qtet_akm_set_regs(struct snd_akm4xxx *ak, unsigned char addr,
334 		unsigned char mask, unsigned char value)
335 {
336 	unsigned char tmp;
337 	int chip;
338 	for (chip = 0; chip < ak->num_chips; chip++) {
339 		tmp = snd_akm4xxx_get(ak, chip, addr);
340 		/* clear the bits */
341 		tmp &= ~mask;
342 		/* set the new bits */
343 		tmp |= value;
344 		snd_akm4xxx_write(ak, chip, addr, tmp);
345 	}
346 }
347 
348 /*
349  * change the rate of AK4620
350  */
351 static void qtet_akm_set_rate_val(struct snd_akm4xxx *ak, unsigned int rate)
352 {
353 	unsigned char ak4620_dfs;
354 
355 	if (rate == 0)  /* no hint - S/PDIF input is master or the new spdif
356 			   input rate undetected, simply return */
357 		return;
358 
359 	/* adjust DFS on codecs - see datasheet */
360 	if (rate > 108000)
361 		ak4620_dfs = AK4620_DFS1 | AK4620_CKS1;
362 	else if (rate > 54000)
363 		ak4620_dfs = AK4620_DFS0 | AK4620_CKS0;
364 	else
365 		ak4620_dfs = 0;
366 
367 	/* set new value */
368 	qtet_akm_set_regs(ak, AK4620_DFS_REG, AK4620_DFS0 | AK4620_DFS1 |
369 			AK4620_CKS0 | AK4620_CKS1, ak4620_dfs);
370 }
371 
372 #define AK_CONTROL(xname, xch)	{ .name = xname, .num_channels = xch }
373 
374 #define PCM_12_PLAYBACK_VOLUME	"PCM 1/2 Playback Volume"
375 #define PCM_34_PLAYBACK_VOLUME	"PCM 3/4 Playback Volume"
376 #define PCM_12_CAPTURE_VOLUME	"PCM 1/2 Capture Volume"
377 #define PCM_34_CAPTURE_VOLUME	"PCM 3/4 Capture Volume"
378 
379 static const struct snd_akm4xxx_dac_channel qtet_dac[] = {
380 	AK_CONTROL(PCM_12_PLAYBACK_VOLUME, 2),
381 	AK_CONTROL(PCM_34_PLAYBACK_VOLUME, 2),
382 };
383 
384 static const struct snd_akm4xxx_adc_channel qtet_adc[] = {
385 	AK_CONTROL(PCM_12_CAPTURE_VOLUME, 2),
386 	AK_CONTROL(PCM_34_CAPTURE_VOLUME, 2),
387 };
388 
389 static struct snd_akm4xxx akm_qtet_dac = {
390 	.type = SND_AK4620,
391 	.num_dacs = 4,	/* DAC1 - Output 12
392 	*/
393 	.num_adcs = 4,	/* ADC1 - Input 12
394 	*/
395 	.ops = {
396 		.write = qtet_akm_write,
397 		.set_rate_val = qtet_akm_set_rate_val,
398 	},
399 	.dac_info = qtet_dac,
400 	.adc_info = qtet_adc,
401 };
402 
403 /* Communication routines with the CPLD */
404 
405 
406 /* Writes data to external register reg, both reg and data are
407  * GPIO representations */
408 static void reg_write(struct snd_ice1712 *ice, unsigned int reg,
409 		unsigned int data)
410 {
411 	unsigned int tmp;
412 
413 	mutex_lock(&ice->gpio_mutex);
414 	/* set direction of used GPIOs*/
415 	/* all outputs */
416 	tmp = 0x00ffff;
417 	ice->gpio.set_dir(ice, tmp);
418 	/* mask - writable bits */
419 	ice->gpio.set_mask(ice, ~(tmp));
420 	/* write the data */
421 	tmp = ice->gpio.get_data(ice);
422 	tmp &= ~GPIO_DATA_MASK;
423 	tmp |= data;
424 	ice->gpio.set_data(ice, tmp);
425 	udelay(100);
426 	/* drop output enable */
427 	tmp &=  ~GPIO_EX_GPIOE;
428 	ice->gpio.set_data(ice, tmp);
429 	udelay(100);
430 	/* drop the register gpio */
431 	tmp &= ~reg;
432 	ice->gpio.set_data(ice, tmp);
433 	udelay(100);
434 	/* raise the register GPIO */
435 	tmp |= reg;
436 	ice->gpio.set_data(ice, tmp);
437 	udelay(100);
438 
439 	/* raise all data gpios */
440 	tmp |= GPIO_DATA_MASK;
441 	ice->gpio.set_data(ice, tmp);
442 	/* mask - immutable bits */
443 	ice->gpio.set_mask(ice, 0xffffff);
444 	/* outputs only 8-15 */
445 	ice->gpio.set_dir(ice, 0x00ff00);
446 	mutex_unlock(&ice->gpio_mutex);
447 }
448 
449 static unsigned int get_scr(struct snd_ice1712 *ice)
450 {
451 	struct qtet_spec *spec = ice->spec;
452 	return spec->scr;
453 }
454 
455 static unsigned int get_mcr(struct snd_ice1712 *ice)
456 {
457 	struct qtet_spec *spec = ice->spec;
458 	return spec->mcr;
459 }
460 
461 static unsigned int get_cpld(struct snd_ice1712 *ice)
462 {
463 	struct qtet_spec *spec = ice->spec;
464 	return spec->cpld;
465 }
466 
467 static void set_scr(struct snd_ice1712 *ice, unsigned int val)
468 {
469 	struct qtet_spec *spec = ice->spec;
470 	reg_write(ice, GPIO_SCR, val);
471 	spec->scr = val;
472 }
473 
474 static void set_mcr(struct snd_ice1712 *ice, unsigned int val)
475 {
476 	struct qtet_spec *spec = ice->spec;
477 	reg_write(ice, GPIO_MCR, val);
478 	spec->mcr = val;
479 }
480 
481 static void set_cpld(struct snd_ice1712 *ice, unsigned int val)
482 {
483 	struct qtet_spec *spec = ice->spec;
484 	reg_write(ice, GPIO_CPLD_CSN, val);
485 	spec->cpld = val;
486 }
487 
488 static void proc_regs_read(struct snd_info_entry *entry,
489 		struct snd_info_buffer *buffer)
490 {
491 	struct snd_ice1712 *ice = entry->private_data;
492 	char bin_buffer[36];
493 
494 	snd_iprintf(buffer, "SCR:	%s\n", get_binary(bin_buffer,
495 				get_scr(ice)));
496 	snd_iprintf(buffer, "MCR:	%s\n", get_binary(bin_buffer,
497 				get_mcr(ice)));
498 	snd_iprintf(buffer, "CPLD:	%s\n", get_binary(bin_buffer,
499 				get_cpld(ice)));
500 }
501 
502 static void proc_init(struct snd_ice1712 *ice)
503 {
504 	struct snd_info_entry *entry;
505 	if (!snd_card_proc_new(ice->card, "quartet", &entry))
506 		snd_info_set_text_ops(entry, ice, proc_regs_read);
507 }
508 
509 static int qtet_mute_get(struct snd_kcontrol *kcontrol,
510 		struct snd_ctl_elem_value *ucontrol)
511 {
512 	struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
513 	unsigned int val;
514 	val = get_scr(ice) & SCR_MUTE;
515 	ucontrol->value.integer.value[0] = (val) ? 0 : 1;
516 	return 0;
517 }
518 
519 static int qtet_mute_put(struct snd_kcontrol *kcontrol,
520 		struct snd_ctl_elem_value *ucontrol)
521 {
522 	struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
523 	unsigned int old, new, smute;
524 	old = get_scr(ice) & SCR_MUTE;
525 	if (ucontrol->value.integer.value[0]) {
526 		/* unmute */
527 		new = 0;
528 		/* un-smuting DAC */
529 		smute = 0;
530 	} else {
531 		/* mute */
532 		new = SCR_MUTE;
533 		/* smuting DAC */
534 		smute = AK4620_SMUTE;
535 	}
536 	if (old != new) {
537 		struct snd_akm4xxx *ak = ice->akm;
538 		set_scr(ice, (get_scr(ice) & ~SCR_MUTE) | new);
539 		/* set smute */
540 		qtet_akm_set_regs(ak, AK4620_DEEMVOL_REG, AK4620_SMUTE, smute);
541 		return 1;
542 	}
543 	/* no change */
544 	return 0;
545 }
546 
547 static int qtet_ain12_enum_info(struct snd_kcontrol *kcontrol,
548 		struct snd_ctl_elem_info *uinfo)
549 {
550 	static const char * const texts[3] =
551 		{"Line In 1/2", "Mic", "Mic + Low-cut"};
552 	return snd_ctl_enum_info(uinfo, 1, ARRAY_SIZE(texts), texts);
553 }
554 
555 static int qtet_ain12_sw_get(struct snd_kcontrol *kcontrol,
556 		struct snd_ctl_elem_value *ucontrol)
557 {
558 	struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
559 	unsigned int val, result;
560 	val = get_scr(ice) & (SCR_AIN12_SEL1 | SCR_AIN12_SEL0);
561 	switch (val) {
562 	case SCR_AIN12_LINE:
563 		result = 0;
564 		break;
565 	case SCR_AIN12_MIC:
566 		result = 1;
567 		break;
568 	case SCR_AIN12_LOWCUT:
569 		result = 2;
570 		break;
571 	default:
572 		/* BUG - no other combinations allowed */
573 		snd_BUG();
574 		result = 0;
575 	}
576 	ucontrol->value.integer.value[0] = result;
577 	return 0;
578 }
579 
580 static int qtet_ain12_sw_put(struct snd_kcontrol *kcontrol,
581 		struct snd_ctl_elem_value *ucontrol)
582 {
583 	struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
584 	unsigned int old, new, tmp, masked_old;
585 	old = new = get_scr(ice);
586 	masked_old = old & (SCR_AIN12_SEL1 | SCR_AIN12_SEL0);
587 	tmp = ucontrol->value.integer.value[0];
588 	if (tmp == 2)
589 		tmp = 3;	/* binary 10 is not supported */
590 	tmp <<= 4;	/* shifting to SCR_AIN12_SEL0 */
591 	if (tmp != masked_old) {
592 		/* change requested */
593 		switch (tmp) {
594 		case SCR_AIN12_LINE:
595 			new = old & ~(SCR_AIN12_SEL1 | SCR_AIN12_SEL0);
596 			set_scr(ice, new);
597 			/* turn off relay */
598 			new &= ~SCR_RELAY;
599 			set_scr(ice, new);
600 			break;
601 		case SCR_AIN12_MIC:
602 			/* turn on relay */
603 			new = old | SCR_RELAY;
604 			set_scr(ice, new);
605 			new = (new & ~SCR_AIN12_SEL1) | SCR_AIN12_SEL0;
606 			set_scr(ice, new);
607 			break;
608 		case SCR_AIN12_LOWCUT:
609 			/* turn on relay */
610 			new = old | SCR_RELAY;
611 			set_scr(ice, new);
612 			new |= SCR_AIN12_SEL1 | SCR_AIN12_SEL0;
613 			set_scr(ice, new);
614 			break;
615 		default:
616 			snd_BUG();
617 		}
618 		return 1;
619 	}
620 	/* no change */
621 	return 0;
622 }
623 
624 static int qtet_php_get(struct snd_kcontrol *kcontrol,
625 		struct snd_ctl_elem_value *ucontrol)
626 {
627 	struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
628 	unsigned int val;
629 	/* if phantom voltage =48V, phantom on */
630 	val = get_scr(ice) & SCR_PHP_V;
631 	ucontrol->value.integer.value[0] = val ? 1 : 0;
632 	return 0;
633 }
634 
635 static int qtet_php_put(struct snd_kcontrol *kcontrol,
636 		struct snd_ctl_elem_value *ucontrol)
637 {
638 	struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
639 	unsigned int old, new;
640 	old = new = get_scr(ice);
641 	if (ucontrol->value.integer.value[0] /* phantom on requested */
642 			&& (~old & SCR_PHP_V)) /* 0 = voltage 5V */ {
643 		/* is off, turn on */
644 		/* turn voltage on first, = 1 */
645 		new = old | SCR_PHP_V;
646 		set_scr(ice, new);
647 		/* turn phantom on, = 0 */
648 		new &= ~SCR_PHP;
649 		set_scr(ice, new);
650 	} else if (!ucontrol->value.integer.value[0] && (old & SCR_PHP_V)) {
651 		/* phantom off requested and 1 = voltage 48V */
652 		/* is on, turn off */
653 		/* turn voltage off first, = 0 */
654 		new = old & ~SCR_PHP_V;
655 		set_scr(ice, new);
656 		/* turn phantom off, = 1 */
657 		new |= SCR_PHP;
658 		set_scr(ice, new);
659 	}
660 	if (old != new)
661 		return 1;
662 	/* no change */
663 	return 0;
664 }
665 
666 #define PRIV_SW(xid, xbit, xreg)	[xid] = {.bit = xbit,\
667 	.set_register = set_##xreg,\
668 	.get_register = get_##xreg, }
669 
670 
671 #define PRIV_ENUM2(xid, xbit, xreg, xtext1, xtext2)	[xid] = {.bit = xbit,\
672 	.set_register = set_##xreg,\
673 	.get_register = get_##xreg,\
674 	.texts = {xtext1, xtext2} }
675 
676 static struct qtet_kcontrol_private qtet_privates[] = {
677 	PRIV_ENUM2(IN12_SEL, CPLD_IN12_SEL, cpld, "An In 1/2", "An In 3/4"),
678 	PRIV_ENUM2(IN34_SEL, CPLD_IN34_SEL, cpld, "An In 3/4", "IEC958 In"),
679 	PRIV_ENUM2(AIN34_SEL, SCR_AIN34_SEL, scr, "Line In 3/4", "Hi-Z"),
680 	PRIV_ENUM2(COAX_OUT, CPLD_COAX_OUT, cpld, "IEC958", "I2S"),
681 	PRIV_SW(IN12_MON12, MCR_IN12_MON12, mcr),
682 	PRIV_SW(IN12_MON34, MCR_IN12_MON34, mcr),
683 	PRIV_SW(IN34_MON12, MCR_IN34_MON12, mcr),
684 	PRIV_SW(IN34_MON34, MCR_IN34_MON34, mcr),
685 	PRIV_SW(OUT12_MON34, MCR_OUT12_MON34, mcr),
686 	PRIV_SW(OUT34_MON12, MCR_OUT34_MON12, mcr),
687 };
688 
689 static int qtet_enum_info(struct snd_kcontrol *kcontrol,
690 		struct snd_ctl_elem_info *uinfo)
691 {
692 	struct qtet_kcontrol_private private =
693 		qtet_privates[kcontrol->private_value];
694 	return snd_ctl_enum_info(uinfo, 1, ARRAY_SIZE(private.texts),
695 				 private.texts);
696 }
697 
698 static int qtet_sw_get(struct snd_kcontrol *kcontrol,
699 		struct snd_ctl_elem_value *ucontrol)
700 {
701 	struct qtet_kcontrol_private private =
702 		qtet_privates[kcontrol->private_value];
703 	struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
704 	ucontrol->value.integer.value[0] =
705 		(private.get_register(ice) & private.bit) ? 1 : 0;
706 	return 0;
707 }
708 
709 static int qtet_sw_put(struct snd_kcontrol *kcontrol,
710 		struct snd_ctl_elem_value *ucontrol)
711 {
712 	struct qtet_kcontrol_private private =
713 		qtet_privates[kcontrol->private_value];
714 	struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
715 	unsigned int old, new;
716 	old = private.get_register(ice);
717 	if (ucontrol->value.integer.value[0])
718 		new = old | private.bit;
719 	else
720 		new = old & ~private.bit;
721 	if (old != new) {
722 		private.set_register(ice, new);
723 		return 1;
724 	}
725 	/* no change */
726 	return 0;
727 }
728 
729 #define qtet_sw_info	snd_ctl_boolean_mono_info
730 
731 #define QTET_CONTROL(xname, xtype, xpriv)	\
732 	{.iface = SNDRV_CTL_ELEM_IFACE_MIXER,\
733 	.name = xname,\
734 	.info = qtet_##xtype##_info,\
735 	.get = qtet_sw_get,\
736 	.put = qtet_sw_put,\
737 	.private_value = xpriv }
738 
739 static struct snd_kcontrol_new qtet_controls[] = {
740 	{
741 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
742 		.name = "Master Playback Switch",
743 		.info = qtet_sw_info,
744 		.get = qtet_mute_get,
745 		.put = qtet_mute_put,
746 		.private_value = 0
747 	},
748 	{
749 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
750 		.name = "Phantom Power",
751 		.info = qtet_sw_info,
752 		.get = qtet_php_get,
753 		.put = qtet_php_put,
754 		.private_value = 0
755 	},
756 	{
757 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
758 		.name = "Analog In 1/2 Capture Switch",
759 		.info = qtet_ain12_enum_info,
760 		.get = qtet_ain12_sw_get,
761 		.put = qtet_ain12_sw_put,
762 		.private_value = 0
763 	},
764 	QTET_CONTROL("Analog In 3/4 Capture Switch", enum, AIN34_SEL),
765 	QTET_CONTROL("PCM In 1/2 Capture Switch", enum, IN12_SEL),
766 	QTET_CONTROL("PCM In 3/4 Capture Switch", enum, IN34_SEL),
767 	QTET_CONTROL("Coax Output Source", enum, COAX_OUT),
768 	QTET_CONTROL("Analog In 1/2 to Monitor 1/2", sw, IN12_MON12),
769 	QTET_CONTROL("Analog In 1/2 to Monitor 3/4", sw, IN12_MON34),
770 	QTET_CONTROL("Analog In 3/4 to Monitor 1/2", sw, IN34_MON12),
771 	QTET_CONTROL("Analog In 3/4 to Monitor 3/4", sw, IN34_MON34),
772 	QTET_CONTROL("Output 1/2 to Monitor 3/4", sw, OUT12_MON34),
773 	QTET_CONTROL("Output 3/4 to Monitor 1/2", sw, OUT34_MON12),
774 };
775 
776 static char *slave_vols[] = {
777 	PCM_12_PLAYBACK_VOLUME,
778 	PCM_34_PLAYBACK_VOLUME,
779 	NULL
780 };
781 
782 static
783 DECLARE_TLV_DB_SCALE(qtet_master_db_scale, -6350, 50, 1);
784 
785 static struct snd_kcontrol *ctl_find(struct snd_card *card,
786 				     const char *name)
787 {
788 	struct snd_ctl_elem_id sid;
789 	memset(&sid, 0, sizeof(sid));
790 	/* FIXME: strcpy is bad. */
791 	strcpy(sid.name, name);
792 	sid.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
793 	return snd_ctl_find_id(card, &sid);
794 }
795 
796 static void add_slaves(struct snd_card *card,
797 		       struct snd_kcontrol *master, char * const *list)
798 {
799 	for (; *list; list++) {
800 		struct snd_kcontrol *slave = ctl_find(card, *list);
801 		if (slave)
802 			snd_ctl_add_slave(master, slave);
803 	}
804 }
805 
806 static int qtet_add_controls(struct snd_ice1712 *ice)
807 {
808 	struct qtet_spec *spec = ice->spec;
809 	int err, i;
810 	struct snd_kcontrol *vmaster;
811 	err = snd_ice1712_akm4xxx_build_controls(ice);
812 	if (err < 0)
813 		return err;
814 	for (i = 0; i < ARRAY_SIZE(qtet_controls); i++) {
815 		err = snd_ctl_add(ice->card,
816 				snd_ctl_new1(&qtet_controls[i], ice));
817 		if (err < 0)
818 			return err;
819 	}
820 
821 	/* Create virtual master control */
822 	vmaster = snd_ctl_make_virtual_master("Master Playback Volume",
823 			qtet_master_db_scale);
824 	if (!vmaster)
825 		return -ENOMEM;
826 	add_slaves(ice->card, vmaster, slave_vols);
827 	err = snd_ctl_add(ice->card, vmaster);
828 	if (err < 0)
829 		return err;
830 	/* only capture SPDIF over AK4113 */
831 	return snd_ak4113_build(spec->ak4113,
832 			ice->pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream);
833 }
834 
835 static inline int qtet_is_spdif_master(struct snd_ice1712 *ice)
836 {
837 	/* CPLD_SYNC_SEL: 0 = internal, 1 = external (i.e. spdif master) */
838 	return (get_cpld(ice) & CPLD_SYNC_SEL) ? 1 : 0;
839 }
840 
841 static unsigned int qtet_get_rate(struct snd_ice1712 *ice)
842 {
843 	int i;
844 	unsigned char result;
845 
846 	result =  get_cpld(ice) & CPLD_CKS_MASK;
847 	for (i = 0; i < ARRAY_SIZE(cks_vals); i++)
848 		if (cks_vals[i] == result)
849 			return qtet_rates[i];
850 	return 0;
851 }
852 
853 static int get_cks_val(int rate)
854 {
855 	int i;
856 	for (i = 0; i < ARRAY_SIZE(qtet_rates); i++)
857 		if (qtet_rates[i] == rate)
858 			return cks_vals[i];
859 	return 0;
860 }
861 
862 /* setting new rate */
863 static void qtet_set_rate(struct snd_ice1712 *ice, unsigned int rate)
864 {
865 	unsigned int new;
866 	unsigned char val;
867 	/* switching ice1724 to external clock - supplied by ext. circuits */
868 	val = inb(ICEMT1724(ice, RATE));
869 	outb(val | VT1724_SPDIF_MASTER, ICEMT1724(ice, RATE));
870 
871 	new =  (get_cpld(ice) & ~CPLD_CKS_MASK) | get_cks_val(rate);
872 	/* switch to internal clock, drop CPLD_SYNC_SEL */
873 	new &= ~CPLD_SYNC_SEL;
874 	/* dev_dbg(ice->card->dev, "QT - set_rate: old %x, new %x\n",
875 	   get_cpld(ice), new); */
876 	set_cpld(ice, new);
877 }
878 
879 static inline unsigned char qtet_set_mclk(struct snd_ice1712 *ice,
880 		unsigned int rate)
881 {
882 	/* no change in master clock */
883 	return 0;
884 }
885 
886 /* setting clock to external - SPDIF */
887 static int qtet_set_spdif_clock(struct snd_ice1712 *ice, int type)
888 {
889 	unsigned int old, new;
890 
891 	old = new = get_cpld(ice);
892 	new &= ~(CPLD_CKS_MASK | CPLD_WORD_SEL);
893 	switch (type) {
894 	case EXT_SPDIF_TYPE:
895 		new |= CPLD_EXT_SPDIF;
896 		break;
897 	case EXT_WORDCLOCK_1FS_TYPE:
898 		new |= CPLD_EXT_WORDCLOCK_1FS;
899 		break;
900 	case EXT_WORDCLOCK_256FS_TYPE:
901 		new |= CPLD_EXT_WORDCLOCK_256FS;
902 		break;
903 	default:
904 		snd_BUG();
905 	}
906 	if (old != new) {
907 		set_cpld(ice, new);
908 		/* changed */
909 		return 1;
910 	}
911 	return 0;
912 }
913 
914 static int qtet_get_spdif_master_type(struct snd_ice1712 *ice)
915 {
916 	unsigned int val;
917 	int result;
918 	val = get_cpld(ice);
919 	/* checking only rate/clock-related bits */
920 	val &= (CPLD_CKS_MASK | CPLD_WORD_SEL | CPLD_SYNC_SEL);
921 	if (!(val & CPLD_SYNC_SEL)) {
922 		/* switched to internal clock, is not any external type */
923 		result = -1;
924 	} else {
925 		switch (val) {
926 		case (CPLD_EXT_SPDIF):
927 			result = EXT_SPDIF_TYPE;
928 			break;
929 		case (CPLD_EXT_WORDCLOCK_1FS):
930 			result = EXT_WORDCLOCK_1FS_TYPE;
931 			break;
932 		case (CPLD_EXT_WORDCLOCK_256FS):
933 			result = EXT_WORDCLOCK_256FS_TYPE;
934 			break;
935 		default:
936 			/* undefined combination of external clock setup */
937 			snd_BUG();
938 			result = 0;
939 		}
940 	}
941 	return result;
942 }
943 
944 /* Called when ak4113 detects change in the input SPDIF stream */
945 static void qtet_ak4113_change(struct ak4113 *ak4113, unsigned char c0,
946 		unsigned char c1)
947 {
948 	struct snd_ice1712 *ice = ak4113->change_callback_private;
949 	int rate;
950 	if ((qtet_get_spdif_master_type(ice) == EXT_SPDIF_TYPE) &&
951 			c1) {
952 		/* only for SPDIF master mode, rate was changed */
953 		rate = snd_ak4113_external_rate(ak4113);
954 		/* dev_dbg(ice->card->dev, "ak4113 - input rate changed to %d\n",
955 		   rate); */
956 		qtet_akm_set_rate_val(ice->akm, rate);
957 	}
958 }
959 
960 /*
961  * If clock slaved to SPDIF-IN, setting runtime rate
962  * to the detected external rate
963  */
964 static void qtet_spdif_in_open(struct snd_ice1712 *ice,
965 		struct snd_pcm_substream *substream)
966 {
967 	struct qtet_spec *spec = ice->spec;
968 	struct snd_pcm_runtime *runtime = substream->runtime;
969 	int rate;
970 
971 	if (qtet_get_spdif_master_type(ice) != EXT_SPDIF_TYPE)
972 		/* not external SPDIF, no rate limitation */
973 		return;
974 	/* only external SPDIF can detect incoming sample rate */
975 	rate = snd_ak4113_external_rate(spec->ak4113);
976 	if (rate >= runtime->hw.rate_min && rate <= runtime->hw.rate_max) {
977 		runtime->hw.rate_min = rate;
978 		runtime->hw.rate_max = rate;
979 	}
980 }
981 
982 /*
983  * initialize the chip
984  */
985 static int qtet_init(struct snd_ice1712 *ice)
986 {
987 	static const unsigned char ak4113_init_vals[] = {
988 		/* AK4113_REG_PWRDN */	AK4113_RST | AK4113_PWN |
989 			AK4113_OCKS0 | AK4113_OCKS1,
990 		/* AK4113_REQ_FORMAT */	AK4113_DIF_I24I2S | AK4113_VTX |
991 			AK4113_DEM_OFF | AK4113_DEAU,
992 		/* AK4113_REG_IO0 */	AK4113_OPS2 | AK4113_TXE |
993 			AK4113_XTL_24_576M,
994 		/* AK4113_REG_IO1 */	AK4113_EFH_1024LRCLK | AK4113_IPS(0),
995 		/* AK4113_REG_INT0_MASK */	0,
996 		/* AK4113_REG_INT1_MASK */	0,
997 		/* AK4113_REG_DATDTS */		0,
998 	};
999 	int err;
1000 	struct qtet_spec *spec;
1001 	struct snd_akm4xxx *ak;
1002 	unsigned char val;
1003 
1004 	/* switching ice1724 to external clock - supplied by ext. circuits */
1005 	val = inb(ICEMT1724(ice, RATE));
1006 	outb(val | VT1724_SPDIF_MASTER, ICEMT1724(ice, RATE));
1007 
1008 	spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1009 	if (!spec)
1010 		return -ENOMEM;
1011 	/* qtet is clocked by Xilinx array */
1012 	ice->hw_rates = &qtet_rates_info;
1013 	ice->is_spdif_master = qtet_is_spdif_master;
1014 	ice->get_rate = qtet_get_rate;
1015 	ice->set_rate = qtet_set_rate;
1016 	ice->set_mclk = qtet_set_mclk;
1017 	ice->set_spdif_clock = qtet_set_spdif_clock;
1018 	ice->get_spdif_master_type = qtet_get_spdif_master_type;
1019 	ice->ext_clock_names = ext_clock_names;
1020 	ice->ext_clock_count = ARRAY_SIZE(ext_clock_names);
1021 	/* since Qtet can detect correct SPDIF-in rate, all streams can be
1022 	 * limited to this specific rate */
1023 	ice->spdif.ops.open = ice->pro_open = qtet_spdif_in_open;
1024 	ice->spec = spec;
1025 
1026 	/* Mute Off */
1027 	/* SCR Initialize*/
1028 	/* keep codec power down first */
1029 	set_scr(ice, SCR_PHP);
1030 	udelay(1);
1031 	/* codec power up */
1032 	set_scr(ice, SCR_PHP | SCR_CODEC_PDN);
1033 
1034 	/* MCR Initialize */
1035 	set_mcr(ice, 0);
1036 
1037 	/* CPLD Initialize */
1038 	set_cpld(ice, 0);
1039 
1040 
1041 	ice->num_total_dacs = 2;
1042 	ice->num_total_adcs = 2;
1043 
1044 	ice->akm = kcalloc(2, sizeof(struct snd_akm4xxx), GFP_KERNEL);
1045 	ak = ice->akm;
1046 	if (!ak)
1047 		return -ENOMEM;
1048 	/* only one codec with two chips */
1049 	ice->akm_codecs = 1;
1050 	err = snd_ice1712_akm4xxx_init(ak, &akm_qtet_dac, NULL, ice);
1051 	if (err < 0)
1052 		return err;
1053 	err = snd_ak4113_create(ice->card,
1054 			qtet_ak4113_read,
1055 			qtet_ak4113_write,
1056 			ak4113_init_vals,
1057 			ice, &spec->ak4113);
1058 	if (err < 0)
1059 		return err;
1060 	/* callback for codecs rate setting */
1061 	spec->ak4113->change_callback = qtet_ak4113_change;
1062 	spec->ak4113->change_callback_private = ice;
1063 	/* AK41143 in Quartet can detect external rate correctly
1064 	 * (i.e. check_flags = 0) */
1065 	spec->ak4113->check_flags = 0;
1066 
1067 	proc_init(ice);
1068 
1069 	qtet_set_rate(ice, 44100);
1070 	return 0;
1071 }
1072 
1073 static unsigned char qtet_eeprom[] = {
1074 	[ICE_EEP2_SYSCONF]     = 0x28,	/* clock 256(24MHz), mpu401, 1xADC,
1075 					   1xDACs, SPDIF in */
1076 	[ICE_EEP2_ACLINK]      = 0x80,	/* I2S */
1077 	[ICE_EEP2_I2S]         = 0x78,	/* 96k, 24bit, 192k */
1078 	[ICE_EEP2_SPDIF]       = 0xc3,	/* out-en, out-int, in, out-ext */
1079 	[ICE_EEP2_GPIO_DIR]    = 0x00,	/* 0-7 inputs, switched to output
1080 					   only during output operations */
1081 	[ICE_EEP2_GPIO_DIR1]   = 0xff,  /* 8-15 outputs */
1082 	[ICE_EEP2_GPIO_DIR2]   = 0x00,
1083 	[ICE_EEP2_GPIO_MASK]   = 0xff,	/* changed only for OUT operations */
1084 	[ICE_EEP2_GPIO_MASK1]  = 0x00,
1085 	[ICE_EEP2_GPIO_MASK2]  = 0xff,
1086 
1087 	[ICE_EEP2_GPIO_STATE]  = 0x00, /* inputs */
1088 	[ICE_EEP2_GPIO_STATE1] = 0x7d, /* all 1, but GPIO_CPLD_RW
1089 					  and GPIO15 always zero */
1090 	[ICE_EEP2_GPIO_STATE2] = 0x00, /* inputs */
1091 };
1092 
1093 /* entry point */
1094 struct snd_ice1712_card_info snd_vt1724_qtet_cards[] = {
1095 	{
1096 		.subvendor = VT1724_SUBDEVICE_QTET,
1097 		.name = "Infrasonic Quartet",
1098 		.model = "quartet",
1099 		.chip_init = qtet_init,
1100 		.build_controls = qtet_add_controls,
1101 		.eeprom_size = sizeof(qtet_eeprom),
1102 		.eeprom_data = qtet_eeprom,
1103 	},
1104 	{ } /* terminator */
1105 };
1106