Merge branch 'for-linus' of git://git.kernel.dk/data/git/linux-2.6-block
[sfrench/cifs-2.6.git] / sound / pci / ice1712 / prodigy192.c
1 /*
2  *   ALSA driver for ICEnsemble VT1724 (Envy24HT)
3  *
4  *   Lowlevel functions for AudioTrak Prodigy 192 cards
5  *   Supported IEC958 input from optional MI/ODI/O add-on card.
6  *
7  *   Specifics (SW, HW):
8  *   -------------------
9  *      * 49.5MHz crystal
10  *      * SPDIF-OUT on the card:
11  *        - coax (through isolation transformer)/toslink supplied by
12  *          74HC04 gates - 3 in parallel
13  *        - output switched between on-board CD drive dig-out connector
14  *          and ice1724 SPDTX pin, using 74HC02 NOR gates, controlled
15  *          by GPIO20 (0 = CD dig-out, 1 = SPDTX)
16  *      * SPDTX goes straight to MI/ODI/O card's SPDIF-OUT coax
17  *
18  *      * MI/ODI/O card: AK4114 based, used for iec958 input only
19  *              - toslink input -> RX0
20  *              - coax input -> RX1
21  *              - 4wire protocol:
22  *                      AK4114          ICE1724
23  *                      ------------------------------
24  *                      CDTO (pin 32) -- GPIO11 pin 86
25  *                      CDTI (pin 33) -- GPIO10 pin 77
26  *                      CCLK (pin 34) -- GPIO9 pin 76
27  *                      CSN  (pin 35) -- GPIO8 pin 75
28  *              - output data Mode 7 (24bit, I2S, slave)
29  *              - both MCKO1 and MCKO2 of ak4114 are fed to FPGA, which
30  *                outputs master clock to SPMCLKIN of ice1724.
31  *                Experimentally I found out that only a combination of
32  *                OCKS0=1, OCKS1=1 (128fs, 64fs output) and ice1724 -
33  *                VT1724_MT_I2S_MCLK_128X=0 (256fs input) yields correct
34  *                sampling rate. That means the the FPGA doubles the
35  *                MCK01 rate.
36  *
37  *      Copyright (c) 2003 Takashi Iwai <tiwai@suse.de>
38  *      Copyright (c) 2003 Dimitromanolakis Apostolos <apostol@cs.utoronto.ca>
39  *      Copyright (c) 2004 Kouichi ONO <co2b@ceres.dti.ne.jp>
40  *
41  *   This program is free software; you can redistribute it and/or modify
42  *   it under the terms of the GNU General Public License as published by
43  *   the Free Software Foundation; either version 2 of the License, or
44  *   (at your option) any later version.
45  *
46  *   This program is distributed in the hope that it will be useful,
47  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
48  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
49  *   GNU General Public License for more details.
50  *
51  *   You should have received a copy of the GNU General Public License
52  *   along with this program; if not, write to the Free Software
53  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
54  *
55  */      
56
57 #include <sound/driver.h>
58 #include <asm/io.h>
59 #include <linux/delay.h>
60 #include <linux/interrupt.h>
61 #include <linux/init.h>
62 #include <linux/slab.h>
63 #include <sound/core.h>
64
65 #include "ice1712.h"
66 #include "envy24ht.h"
67 #include "prodigy192.h"
68 #include "stac946x.h"
69 #include <sound/tlv.h>
70
71 static inline void stac9460_put(struct snd_ice1712 *ice, int reg, unsigned char val)
72 {
73         snd_vt1724_write_i2c(ice, PRODIGY192_STAC9460_ADDR, reg, val);
74 }
75
76 static inline unsigned char stac9460_get(struct snd_ice1712 *ice, int reg)
77 {
78         return snd_vt1724_read_i2c(ice, PRODIGY192_STAC9460_ADDR, reg);
79 }
80
81 /*
82  * DAC mute control
83  */
84 static int stac9460_dac_mute_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
85 {
86         uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
87         uinfo->count = 1;
88         uinfo->value.integer.min = 0;
89         uinfo->value.integer.max = 1;
90         return 0;
91 }
92
93 static int stac9460_dac_mute_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
94 {
95         struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
96         unsigned char val;
97         int idx;
98
99         if (kcontrol->private_value)
100                 idx = STAC946X_MASTER_VOLUME;
101         else
102                 idx  = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id) + STAC946X_LF_VOLUME;
103         val = stac9460_get(ice, idx);
104         ucontrol->value.integer.value[0] = (~val >> 7) & 0x1;
105         return 0;
106 }
107
108 static int stac9460_dac_mute_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
109 {
110         struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
111         unsigned char new, old;
112         int idx;
113         int change;
114
115         if (kcontrol->private_value)
116                 idx = STAC946X_MASTER_VOLUME;
117         else
118                 idx  = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id) + STAC946X_LF_VOLUME;
119         old = stac9460_get(ice, idx);
120         new = (~ucontrol->value.integer.value[0]<< 7 & 0x80) | (old & ~0x80);
121         change = (new != old);
122         if (change)
123                 stac9460_put(ice, idx, new);
124
125         return change;
126 }
127
128 /*
129  * DAC volume attenuation mixer control
130  */
131 static int stac9460_dac_vol_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
132 {
133         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
134         uinfo->count = 1;
135         uinfo->value.integer.min = 0;                   /* mute */
136         uinfo->value.integer.max = 0x7f;                /* 0dB */
137         return 0;
138 }
139
140 static int stac9460_dac_vol_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
141 {
142         struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
143         int idx;
144         unsigned char vol;
145
146         if (kcontrol->private_value)
147                 idx = STAC946X_MASTER_VOLUME;
148         else
149                 idx  = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id) + STAC946X_LF_VOLUME;
150         vol = stac9460_get(ice, idx) & 0x7f;
151         ucontrol->value.integer.value[0] = 0x7f - vol;
152
153         return 0;
154 }
155
156 static int stac9460_dac_vol_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
157 {
158         struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
159         int idx;
160         unsigned char tmp, ovol, nvol;
161         int change;
162
163         if (kcontrol->private_value)
164                 idx = STAC946X_MASTER_VOLUME;
165         else
166                 idx  = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id) + STAC946X_LF_VOLUME;
167         nvol = ucontrol->value.integer.value[0];
168         tmp = stac9460_get(ice, idx);
169         ovol = 0x7f - (tmp & 0x7f);
170         change = (ovol != nvol);
171         if (change) {
172                 stac9460_put(ice, idx, (0x7f - nvol) | (tmp & 0x80));
173         }
174         return change;
175 }
176
177 /*
178  * ADC mute control
179  */
180 static int stac9460_adc_mute_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
181 {
182         uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
183         uinfo->count = 2;
184         uinfo->value.integer.min = 0;
185         uinfo->value.integer.max = 1;
186         return 0;
187 }
188
189 static int stac9460_adc_mute_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
190 {
191         struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
192         unsigned char val;
193         int i;
194
195         for (i = 0; i < 2; ++i) {
196                 val = stac9460_get(ice, STAC946X_MIC_L_VOLUME + i);
197                 ucontrol->value.integer.value[i] = ~val>>7 & 0x1;
198         }
199
200         return 0;
201 }
202
203 static int stac9460_adc_mute_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
204 {
205         struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
206         unsigned char new, old;
207         int i, reg;
208         int change;
209
210         for (i = 0; i < 2; ++i) {
211                 reg = STAC946X_MIC_L_VOLUME + i;
212                 old = stac9460_get(ice, reg);
213                 new = (~ucontrol->value.integer.value[i]<<7&0x80) | (old&~0x80);
214                 change = (new != old);
215                 if (change)
216                         stac9460_put(ice, reg, new);
217         }
218
219         return change;
220 }
221
222 /*
223  * ADC gain mixer control
224  */
225 static int stac9460_adc_vol_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
226 {
227         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
228         uinfo->count = 2;
229         uinfo->value.integer.min = 0;           /* 0dB */
230         uinfo->value.integer.max = 0x0f;        /* 22.5dB */
231         return 0;
232 }
233
234 static int stac9460_adc_vol_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
235 {
236         struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
237         int i, reg;
238         unsigned char vol;
239
240         for (i = 0; i < 2; ++i) {
241                 reg = STAC946X_MIC_L_VOLUME + i;
242                 vol = stac9460_get(ice, reg) & 0x0f;
243                 ucontrol->value.integer.value[i] = 0x0f - vol;
244         }
245
246         return 0;
247 }
248
249 static int stac9460_adc_vol_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
250 {
251         struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
252         int i, reg;
253         unsigned char ovol, nvol;
254         int change;
255
256         for (i = 0; i < 2; ++i) {
257                 reg = STAC946X_MIC_L_VOLUME + i;
258                 nvol = ucontrol->value.integer.value[i];
259                 ovol = 0x0f - stac9460_get(ice, reg);
260                 change = ((ovol & 0x0f)  != nvol);
261                 if (change)
262                         stac9460_put(ice, reg, (0x0f - nvol) | (ovol & ~0x0f));
263         }
264
265         return change;
266 }
267
268 #if 0
269 /*
270  * Headphone Amplifier
271  */
272 static int aureon_set_headphone_amp(struct snd_ice1712 *ice, int enable)
273 {
274         unsigned int tmp, tmp2;
275
276         tmp2 = tmp = snd_ice1712_gpio_read(ice);
277         if (enable)
278                 tmp |= AUREON_HP_SEL;
279         else
280                 tmp &= ~ AUREON_HP_SEL;
281         if (tmp != tmp2) {
282                 snd_ice1712_gpio_write(ice, tmp);
283                 return 1;
284         }
285         return 0;
286 }
287
288 static int aureon_get_headphone_amp(struct snd_ice1712 *ice)
289 {
290         unsigned int tmp = snd_ice1712_gpio_read(ice);
291
292         return ( tmp & AUREON_HP_SEL )!= 0;
293 }
294
295 static int aureon_bool_info(struct snd_kcontrol *k, struct snd_ctl_elem_info *uinfo)
296 {
297         uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
298         uinfo->count = 1;
299         uinfo->value.integer.min = 0;
300         uinfo->value.integer.max = 1;
301         return 0;
302 }
303
304 static int aureon_hpamp_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
305 {
306         struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
307
308         ucontrol->value.integer.value[0] = aureon_get_headphone_amp(ice);
309         return 0;
310 }
311
312
313 static int aureon_hpamp_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
314 {
315         struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
316
317         return aureon_set_headphone_amp(ice,ucontrol->value.integer.value[0]);
318 }
319
320 /*
321  * Deemphasis
322  */
323 static int aureon_deemp_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
324 {
325         struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
326         ucontrol->value.integer.value[0] = (wm_get(ice, WM_DAC_CTRL2) & 0xf) == 0xf;
327         return 0;
328 }
329
330 static int aureon_deemp_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
331 {
332         struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
333         int temp, temp2;
334         temp2 = temp = wm_get(ice, WM_DAC_CTRL2);
335         if (ucontrol->value.integer.value[0])
336                 temp |= 0xf;
337         else
338                 temp &= ~0xf;
339         if (temp != temp2) {
340                 wm_put(ice, WM_DAC_CTRL2, temp);
341                 return 1;
342         }
343         return 0;
344 }
345
346 /*
347  * ADC Oversampling
348  */
349 static int aureon_oversampling_info(struct snd_kcontrol *k, struct snd_ctl_elem_info *uinfo)
350 {
351         static char *texts[2] = { "128x", "64x" };
352
353         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
354         uinfo->count = 1;
355         uinfo->value.enumerated.items = 2;
356
357         if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
358                 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
359         strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
360
361         return 0;
362 }
363
364 static int aureon_oversampling_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
365 {
366         struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
367         ucontrol->value.enumerated.item[0] = (wm_get(ice, WM_MASTER) & 0x8) == 0x8;
368         return 0;
369 }
370
371 static int aureon_oversampling_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
372 {
373         int temp, temp2;
374         struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
375
376         temp2 = temp = wm_get(ice, WM_MASTER);
377
378         if (ucontrol->value.enumerated.item[0])
379                 temp |= 0x8;
380         else
381                 temp &= ~0x8;
382
383         if (temp != temp2) {
384                 wm_put(ice, WM_MASTER, temp);
385                 return 1;
386         }
387         return 0;
388 }
389 #endif
390 static int stac9460_mic_sw_info(struct snd_kcontrol *kcontrol,
391                                 struct snd_ctl_elem_info *uinfo)
392 {
393         static char *texts[2] = { "Line In", "Mic" };
394
395         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
396         uinfo->count = 1;
397         uinfo->value.enumerated.items = 2;
398
399         if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
400                 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
401         strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
402
403         return 0;
404 }
405
406
407 static int stac9460_mic_sw_get(struct snd_kcontrol *kcontrol,
408                         struct snd_ctl_elem_value *ucontrol)
409 {
410         struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
411         unsigned char val;
412                 
413         val = stac9460_get(ice, STAC946X_GENERAL_PURPOSE);
414         ucontrol->value.enumerated.item[0] = (val >> 7) & 0x1;
415         return 0;
416 }
417
418 static int stac9460_mic_sw_put(struct snd_kcontrol *kcontrol,
419                         struct snd_ctl_elem_value *ucontrol)
420 {
421         struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
422         unsigned char new, old;
423         int change;
424         old = stac9460_get(ice, STAC946X_GENERAL_PURPOSE);
425         new = (ucontrol->value.enumerated.item[0] << 7 & 0x80) | (old & ~0x80);
426         change = (new != old);
427         if (change)
428                 stac9460_put(ice, STAC946X_GENERAL_PURPOSE, new);
429         return change;
430 }
431
432 static const DECLARE_TLV_DB_SCALE(db_scale_dac, -19125, 75, 0);
433 static const DECLARE_TLV_DB_SCALE(db_scale_adc, 0, 150, 0);
434
435 /*
436  * mixers
437  */
438
439 static struct snd_kcontrol_new stac_controls[] __devinitdata = {
440         {
441                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
442                 .name = "Master Playback Switch",
443                 .info = stac9460_dac_mute_info,
444                 .get = stac9460_dac_mute_get,
445                 .put = stac9460_dac_mute_put,
446                 .private_value = 1,
447                 .tlv = { .p = db_scale_dac }
448         },
449         {
450                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
451                 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
452                            SNDRV_CTL_ELEM_ACCESS_TLV_READ),
453                 .name = "Master Playback Volume",
454                 .info = stac9460_dac_vol_info,
455                 .get = stac9460_dac_vol_get,
456                 .put = stac9460_dac_vol_put,
457                 .private_value = 1,
458                 .tlv = { .p = db_scale_dac }
459         },
460         {
461                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
462                 .name = "DAC Switch",
463                 .count = 6,
464                 .info = stac9460_dac_mute_info,
465                 .get = stac9460_dac_mute_get,
466                 .put = stac9460_dac_mute_put,
467         },
468         {
469                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
470                 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
471                            SNDRV_CTL_ELEM_ACCESS_TLV_READ),
472                 .name = "DAC Volume",
473                 .count = 6,
474                 .info = stac9460_dac_vol_info,
475                 .get = stac9460_dac_vol_get,
476                 .put = stac9460_dac_vol_put,
477                 .tlv = { .p = db_scale_dac }
478         },
479         {
480                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
481                 .name = "ADC Capture Switch",
482                 .count = 1,
483                 .info = stac9460_adc_mute_info,
484                 .get = stac9460_adc_mute_get,
485                 .put = stac9460_adc_mute_put,
486
487         },
488         {
489                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
490                 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
491                            SNDRV_CTL_ELEM_ACCESS_TLV_READ),
492                 .name = "ADC Capture Volume",
493                 .count = 1,
494                 .info = stac9460_adc_vol_info,
495                 .get = stac9460_adc_vol_get,
496                 .put = stac9460_adc_vol_put,
497                 .tlv = { .p = db_scale_adc }
498         },
499         {
500                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
501                 .name = "Analog Capture Input",
502                 .info = stac9460_mic_sw_info,
503                 .get = stac9460_mic_sw_get,
504                 .put = stac9460_mic_sw_put,
505
506         },
507 #if 0
508         {
509                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
510                 .name = "Capture Route",
511                 .info = wm_adc_mux_info,
512                 .get = wm_adc_mux_get,
513                 .put = wm_adc_mux_put,
514         },
515         {
516                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
517                 .name = "Headphone Amplifier Switch",
518                 .info = aureon_bool_info,
519                 .get = aureon_hpamp_get,
520                 .put = aureon_hpamp_put
521         },
522         {
523                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
524                 .name = "DAC Deemphasis Switch",
525                 .info = aureon_bool_info,
526                 .get = aureon_deemp_get,
527                 .put = aureon_deemp_put
528         },
529         {
530                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
531                 .name = "ADC Oversampling",
532                 .info = aureon_oversampling_info,
533                 .get = aureon_oversampling_get,
534                 .put = aureon_oversampling_put
535         },
536 #endif
537 };
538
539
540 /* AK4114 - ICE1724 connections on Prodigy192 + MI/ODI/O */
541 /* CDTO (pin 32) -- GPIO11 pin 86
542  * CDTI (pin 33) -- GPIO10 pin 77
543  * CCLK (pin 34) -- GPIO9 pin 76
544  * CSN  (pin 35) -- GPIO8 pin 75
545  */
546 #define AK4114_ADDR     0x00 /* C1-C0: Chip Address
547                               * (According to datasheet fixed to “00”)
548                               */
549
550 /*
551  * 4wire ak4114 protocol - writing data
552  */
553 static void write_data(struct snd_ice1712 *ice, unsigned int gpio,
554                        unsigned int data, int idx)
555 {
556         for (; idx >= 0; idx--) {
557                 /* drop clock */
558                 gpio &= ~VT1724_PRODIGY192_CCLK;
559                 snd_ice1712_gpio_write(ice, gpio);
560                 udelay(1);
561                 /* set data */
562                 if (data & (1 << idx))
563                         gpio |= VT1724_PRODIGY192_CDOUT;
564                 else
565                         gpio &= ~VT1724_PRODIGY192_CDOUT;
566                 snd_ice1712_gpio_write(ice, gpio);
567                 udelay(1);
568                 /* raise clock */
569                 gpio |= VT1724_PRODIGY192_CCLK;
570                 snd_ice1712_gpio_write(ice, gpio);
571                 udelay(1);
572         }
573 }
574
575 /*
576  * 4wire ak4114 protocol - reading data
577  */
578 static unsigned char read_data(struct snd_ice1712 *ice, unsigned int gpio,
579                                int idx)
580 {
581         unsigned char data = 0;
582
583         for (; idx >= 0; idx--) {
584                 /* drop clock */
585                 gpio &= ~VT1724_PRODIGY192_CCLK;
586                 snd_ice1712_gpio_write(ice, gpio);
587                 udelay(1);
588                 /* read data */
589                 if (snd_ice1712_gpio_read(ice) & VT1724_PRODIGY192_CDIN)
590                         data |= (1 << idx);
591                 udelay(1);
592                 /* raise clock */
593                 gpio |= VT1724_PRODIGY192_CCLK;
594                 snd_ice1712_gpio_write(ice, gpio);
595                 udelay(1);
596         }
597         return data;
598 }
599 /*
600  * 4wire ak4114 protocol - starting sequence
601  */
602 static unsigned int prodigy192_4wire_start(struct snd_ice1712 *ice)
603 {
604         unsigned int tmp;
605
606         snd_ice1712_save_gpio_status(ice);
607         tmp = snd_ice1712_gpio_read(ice);
608
609         tmp |= VT1724_PRODIGY192_CCLK; /* high at init */
610         tmp &= ~VT1724_PRODIGY192_CS; /* drop chip select */
611         snd_ice1712_gpio_write(ice, tmp);
612         udelay(1);
613         return tmp;
614 }
615
616 /*
617  * 4wire ak4114 protocol - final sequence
618  */
619 static void prodigy192_4wire_finish(struct snd_ice1712 *ice, unsigned int tmp)
620 {
621         tmp |= VT1724_PRODIGY192_CS; /* raise chip select */
622         snd_ice1712_gpio_write(ice, tmp);
623         udelay(1);
624         snd_ice1712_restore_gpio_status(ice);
625 }
626
627 /*
628  * Write data to addr register of ak4114
629  */
630 static void prodigy192_ak4114_write(void *private_data, unsigned char addr,
631                                unsigned char data)
632 {
633         struct snd_ice1712 *ice = private_data;
634         unsigned int tmp, addrdata;
635         tmp = prodigy192_4wire_start(ice);
636         addrdata = (AK4114_ADDR << 6) | 0x20 | (addr & 0x1f);
637         addrdata = (addrdata << 8) | data;
638         write_data(ice, tmp, addrdata, 15);
639         prodigy192_4wire_finish(ice, tmp);
640 }
641
642 /*
643  * Read data from addr register of ak4114
644  */
645 static unsigned char prodigy192_ak4114_read(void *private_data,
646                                             unsigned char addr)
647 {
648         struct snd_ice1712 *ice = private_data;
649         unsigned int tmp;
650         unsigned char data;
651
652         tmp = prodigy192_4wire_start(ice);
653         write_data(ice, tmp, (AK4114_ADDR << 6) | (addr & 0x1f), 7);
654         data = read_data(ice, tmp, 7);
655         prodigy192_4wire_finish(ice, tmp);
656         return data;
657 }
658
659
660 static int ak4114_input_sw_info(struct snd_kcontrol *kcontrol,
661                                 struct snd_ctl_elem_info *uinfo)
662 {
663         static char *texts[2] = { "Toslink", "Coax" };
664
665         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
666         uinfo->count = 1;
667         uinfo->value.enumerated.items = 2;
668         if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
669                 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
670         strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
671         return 0;
672 }
673
674
675 static int ak4114_input_sw_get(struct snd_kcontrol *kcontrol,
676                         struct snd_ctl_elem_value *ucontrol)
677 {
678         struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
679         unsigned char val;
680                 
681         val = prodigy192_ak4114_read(ice, AK4114_REG_IO1);
682         /* AK4114_IPS0 bit = 0 -> RX0 = Toslink
683          * AK4114_IPS0 bit = 1 -> RX1 = Coax
684          */
685         ucontrol->value.enumerated.item[0] = (val & AK4114_IPS0) ? 1 : 0;
686         return 0;
687 }
688
689 static int ak4114_input_sw_put(struct snd_kcontrol *kcontrol,
690                         struct snd_ctl_elem_value *ucontrol)
691 {
692         struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
693         unsigned char new, old, itemvalue;
694         int change;
695
696         old = prodigy192_ak4114_read(ice, AK4114_REG_IO1);
697         /* AK4114_IPS0 could be any bit */
698         itemvalue = (ucontrol->value.enumerated.item[0]) ? 0xff : 0x00;
699
700         new = (itemvalue & AK4114_IPS0) | (old & ~AK4114_IPS0);
701         change = (new != old);
702         if (change)
703                 prodigy192_ak4114_write(ice, AK4114_REG_IO1, new);
704         return change;
705 }
706
707
708 static struct snd_kcontrol_new ak4114_controls[] __devinitdata = {
709         {
710                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
711                 .name = "MIODIO IEC958 Capture Input",
712                 .info = ak4114_input_sw_info,
713                 .get = ak4114_input_sw_get,
714                 .put = ak4114_input_sw_put,
715
716         }
717 };
718
719
720 static int prodigy192_ak4114_init(struct snd_ice1712 *ice)
721 {
722         static const unsigned char ak4114_init_vals[] = {
723                 AK4114_RST | AK4114_PWN | AK4114_OCKS0 | AK4114_OCKS1,
724                 /* ice1724 expects I2S and provides clock,
725                  * DEM0 disables the deemphasis filter
726                  */
727                 AK4114_DIF_I24I2S | AK4114_DEM0 ,
728                 AK4114_TX1E,
729                 AK4114_EFH_1024 | AK4114_DIT, /* default input RX0 */
730                 0,
731                 0
732         };
733         static const unsigned char ak4114_init_txcsb[] = {
734                 0x41, 0x02, 0x2c, 0x00, 0x00
735         };
736
737         return snd_ak4114_create(ice->card,
738                                  prodigy192_ak4114_read,
739                                  prodigy192_ak4114_write,
740                                  ak4114_init_vals, ak4114_init_txcsb,
741                                  ice, &ice->spec.prodigy192.ak4114);
742 }
743
744 static int __devinit prodigy192_add_controls(struct snd_ice1712 *ice)
745 {
746         unsigned int i;
747         int err;
748
749         for (i = 0; i < ARRAY_SIZE(stac_controls); i++) {
750                 err = snd_ctl_add(ice->card,
751                                   snd_ctl_new1(&stac_controls[i], ice));
752                 if (err < 0)
753                         return err;
754         }
755         if (ice->spec.prodigy192.ak4114) {
756                 /* ak4114 is connected */
757                 for (i = 0; i < ARRAY_SIZE(ak4114_controls); i++) {
758                         err = snd_ctl_add(ice->card,
759                                           snd_ctl_new1(&ak4114_controls[i],
760                                                        ice));
761                         if (err < 0)
762                                 return err;
763                 }
764                 err = snd_ak4114_build(ice->spec.prodigy192.ak4114,
765                                 NULL, /* ak4114 in MIO/DI/O handles no IEC958 output */
766                                 ice->pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream);
767                 if (err < 0)
768                         return err;
769         }
770         return 0;
771 }
772
773 /*
774  * check for presence of MI/ODI/O add-on card with digital inputs
775  */
776 static int prodigy192_miodio_exists(struct snd_ice1712 *ice)
777 {
778
779         unsigned char orig_value;
780         const unsigned char test_data = 0xd1;   /* random value */
781         unsigned char addr = AK4114_REG_INT0_MASK; /* random SAFE address */
782         int exists = 0;
783
784         orig_value = prodigy192_ak4114_read(ice, addr);
785         prodigy192_ak4114_write(ice, addr, test_data);
786         if (prodigy192_ak4114_read(ice, addr) == test_data) {
787                 /* ak4114 seems to communicate, apparently exists */
788                 /* writing back original value */
789                 prodigy192_ak4114_write(ice, addr, orig_value);
790                 exists = 1;
791         }
792         return exists;
793 }
794
795 /*
796  * initialize the chip
797  */
798 static int __devinit prodigy192_init(struct snd_ice1712 *ice)
799 {
800         static const unsigned short stac_inits_prodigy[] = {
801                 STAC946X_RESET, 0,
802 /*              STAC946X_MASTER_VOLUME, 0,
803                 STAC946X_LF_VOLUME, 0,
804                 STAC946X_RF_VOLUME, 0,
805                 STAC946X_LR_VOLUME, 0,
806                 STAC946X_RR_VOLUME, 0,
807                 STAC946X_CENTER_VOLUME, 0,
808                 STAC946X_LFE_VOLUME, 0,*/
809                 (unsigned short)-1
810         };
811         const unsigned short *p;
812         int err = 0;
813
814         /* prodigy 192 */
815         ice->num_total_dacs = 6;
816         ice->num_total_adcs = 2;
817         ice->vt1720 = 0;  /* ice1724, e.g. 23 GPIOs */
818         
819         /* initialize codec */
820         p = stac_inits_prodigy;
821         for (; *p != (unsigned short)-1; p += 2)
822                 stac9460_put(ice, p[0], p[1]);
823
824         /* MI/ODI/O add on card with AK4114 */
825         if (prodigy192_miodio_exists(ice)) {
826                 err = prodigy192_ak4114_init(ice);
827                 /* from this moment if err = 0 then
828                  * ice->spec.prodigy192.ak4114 should not be null
829                  */
830                 snd_printdd("AK4114 initialized with status %d\n", err);
831         } else
832                 snd_printdd("AK4114 not found\n");
833         if (err < 0)
834                 return err;
835
836         return 0;
837 }
838
839
840 /*
841  * Aureon boards don't provide the EEPROM data except for the vendor IDs.
842  * hence the driver needs to sets up it properly.
843  */
844
845 static unsigned char prodigy71_eeprom[] __devinitdata = {
846         [ICE_EEP2_SYSCONF]     = 0x6a,  /* 49MHz crystal, mpu401,
847                                          * spdif-in+ 1 stereo ADC,
848                                          * 3 stereo DACs
849                                          */
850         [ICE_EEP2_ACLINK]      = 0x80,  /* I2S */
851         [ICE_EEP2_I2S]         = 0xf8,  /* vol, 96k, 24bit, 192k */
852         [ICE_EEP2_SPDIF]       = 0xc3,  /* out-en, out-int, spdif-in */
853         [ICE_EEP2_GPIO_DIR]    = 0xff,
854         [ICE_EEP2_GPIO_DIR1]   = ~(VT1724_PRODIGY192_CDIN >> 8) ,
855         [ICE_EEP2_GPIO_DIR2]   = 0xbf,
856         [ICE_EEP2_GPIO_MASK]   = 0x00,
857         [ICE_EEP2_GPIO_MASK1]  = 0x00,
858         [ICE_EEP2_GPIO_MASK2]  = 0x00,
859         [ICE_EEP2_GPIO_STATE]  = 0x00,
860         [ICE_EEP2_GPIO_STATE1] = 0x00,
861         [ICE_EEP2_GPIO_STATE2] = 0x10,  /* GPIO20: 0 = CD drive dig. input
862                                          * passthrough,
863                                          * 1 = SPDIF-OUT from ice1724
864                                          */
865 };
866
867
868 /* entry point */
869 struct snd_ice1712_card_info snd_vt1724_prodigy192_cards[] __devinitdata = {
870         {
871                 .subvendor = VT1724_SUBDEVICE_PRODIGY192VE,
872                 .name = "Audiotrak Prodigy 192",
873                 .model = "prodigy192",
874                 .chip_init = prodigy192_init,
875                 .build_controls = prodigy192_add_controls,
876                 .eeprom_size = sizeof(prodigy71_eeprom),
877                 .eeprom_data = prodigy71_eeprom,
878         },
879         { } /* terminator */
880 };