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