6d81a1c61d4147169dc143c2172d5c1807308fc5
[pandora-kernel.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
85 /*
86  * idx = STAC9460 volume register number, mute: 0 = mute, 1 = unmute
87  */
88 static int stac9460_dac_mute(struct snd_ice1712 *ice, int idx,
89                 unsigned char mute)
90 {
91         unsigned char new, old;
92         int change;
93         old = stac9460_get(ice, idx);
94         new = (~mute << 7 & 0x80) | (old & ~0x80);
95         change = (new != old);
96         if (change)
97                 /*printk ("Volume register 0x%02x: 0x%02x\n", idx, new);*/
98                 stac9460_put(ice, idx, new);
99         return change;
100 }
101
102 #define stac9460_dac_mute_info          snd_ctl_boolean_mono_info
103
104 static int stac9460_dac_mute_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
105 {
106         struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
107         unsigned char val;
108         int idx;
109
110         if (kcontrol->private_value)
111                 idx = STAC946X_MASTER_VOLUME;
112         else
113                 idx  = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id) + STAC946X_LF_VOLUME;
114         val = stac9460_get(ice, idx);
115         ucontrol->value.integer.value[0] = (~val >> 7) & 0x1;
116         return 0;
117 }
118
119 static int stac9460_dac_mute_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
120 {
121         struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
122         int idx, change;
123
124         if (kcontrol->private_value)
125                 idx = STAC946X_MASTER_VOLUME;
126         else
127                 idx  = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id) + STAC946X_LF_VOLUME;
128         /* due to possible conflicts with stac9460_set_rate_val, mutexing */
129         mutex_lock(&ice->spec.prodigy192.mute_mutex);
130         /*printk("Mute put: reg 0x%02x, ctrl value: 0x%02x\n", idx,
131                 ucontrol->value.integer.value[0]);*/
132         change = stac9460_dac_mute(ice, idx, ucontrol->value.integer.value[0]);
133         mutex_unlock(&ice->spec.prodigy192.mute_mutex);
134         return change;
135 }
136
137 /*
138  * DAC volume attenuation mixer control
139  */
140 static int stac9460_dac_vol_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
141 {
142         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
143         uinfo->count = 1;
144         uinfo->value.integer.min = 0;                   /* mute */
145         uinfo->value.integer.max = 0x7f;                /* 0dB */
146         return 0;
147 }
148
149 static int stac9460_dac_vol_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
150 {
151         struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
152         int idx;
153         unsigned char vol;
154
155         if (kcontrol->private_value)
156                 idx = STAC946X_MASTER_VOLUME;
157         else
158                 idx  = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id) + STAC946X_LF_VOLUME;
159         vol = stac9460_get(ice, idx) & 0x7f;
160         ucontrol->value.integer.value[0] = 0x7f - vol;
161
162         return 0;
163 }
164
165 static int stac9460_dac_vol_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
166 {
167         struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
168         int idx;
169         unsigned char tmp, ovol, nvol;
170         int change;
171
172         if (kcontrol->private_value)
173                 idx = STAC946X_MASTER_VOLUME;
174         else
175                 idx  = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id) + STAC946X_LF_VOLUME;
176         nvol = ucontrol->value.integer.value[0];
177         tmp = stac9460_get(ice, idx);
178         ovol = 0x7f - (tmp & 0x7f);
179         change = (ovol != nvol);
180         if (change) {
181                 ovol =  (0x7f - nvol) | (tmp & 0x80);
182                 /*printk("DAC Volume: reg 0x%02x: 0x%02x\n", idx, ovol);*/
183                 stac9460_put(ice, idx, (0x7f - nvol) | (tmp & 0x80));
184         }
185         return change;
186 }
187
188 /*
189  * ADC mute control
190  */
191 #define stac9460_adc_mute_info          snd_ctl_boolean_stereo_info
192
193 static int stac9460_adc_mute_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
194 {
195         struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
196         unsigned char val;
197         int i;
198
199         for (i = 0; i < 2; ++i) {
200                 val = stac9460_get(ice, STAC946X_MIC_L_VOLUME + i);
201                 ucontrol->value.integer.value[i] = ~val>>7 & 0x1;
202         }
203
204         return 0;
205 }
206
207 static int stac9460_adc_mute_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
208 {
209         struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
210         unsigned char new, old;
211         int i, reg;
212         int change;
213
214         for (i = 0; i < 2; ++i) {
215                 reg = STAC946X_MIC_L_VOLUME + i;
216                 old = stac9460_get(ice, reg);
217                 new = (~ucontrol->value.integer.value[i]<<7&0x80) | (old&~0x80);
218                 change = (new != old);
219                 if (change)
220                         stac9460_put(ice, reg, new);
221         }
222
223         return change;
224 }
225
226 /*
227  * ADC gain mixer control
228  */
229 static int stac9460_adc_vol_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
230 {
231         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
232         uinfo->count = 2;
233         uinfo->value.integer.min = 0;           /* 0dB */
234         uinfo->value.integer.max = 0x0f;        /* 22.5dB */
235         return 0;
236 }
237
238 static int stac9460_adc_vol_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
239 {
240         struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
241         int i, reg;
242         unsigned char vol;
243
244         for (i = 0; i < 2; ++i) {
245                 reg = STAC946X_MIC_L_VOLUME + i;
246                 vol = stac9460_get(ice, reg) & 0x0f;
247                 ucontrol->value.integer.value[i] = 0x0f - vol;
248         }
249
250         return 0;
251 }
252
253 static int stac9460_adc_vol_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
254 {
255         struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
256         int i, reg;
257         unsigned char ovol, nvol;
258         int change;
259
260         for (i = 0; i < 2; ++i) {
261                 reg = STAC946X_MIC_L_VOLUME + i;
262                 nvol = ucontrol->value.integer.value[i] & 0x0f;
263                 ovol = 0x0f - stac9460_get(ice, reg);
264                 change = ((ovol & 0x0f)  != nvol);
265                 if (change)
266                         stac9460_put(ice, reg, (0x0f - nvol) | (ovol & ~0x0f));
267         }
268
269         return change;
270 }
271
272 static int stac9460_mic_sw_info(struct snd_kcontrol *kcontrol,
273                                 struct snd_ctl_elem_info *uinfo)
274 {
275         static char *texts[2] = { "Line In", "Mic" };
276
277         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
278         uinfo->count = 1;
279         uinfo->value.enumerated.items = 2;
280
281         if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
282                 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
283         strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
284
285         return 0;
286 }
287
288
289 static int stac9460_mic_sw_get(struct snd_kcontrol *kcontrol,
290                         struct snd_ctl_elem_value *ucontrol)
291 {
292         struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
293         unsigned char val;
294                 
295         val = stac9460_get(ice, STAC946X_GENERAL_PURPOSE);
296         ucontrol->value.enumerated.item[0] = (val >> 7) & 0x1;
297         return 0;
298 }
299
300 static int stac9460_mic_sw_put(struct snd_kcontrol *kcontrol,
301                         struct snd_ctl_elem_value *ucontrol)
302 {
303         struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
304         unsigned char new, old;
305         int change;
306         old = stac9460_get(ice, STAC946X_GENERAL_PURPOSE);
307         new = (ucontrol->value.enumerated.item[0] << 7 & 0x80) | (old & ~0x80);
308         change = (new != old);
309         if (change)
310                 stac9460_put(ice, STAC946X_GENERAL_PURPOSE, new);
311         return change;
312 }
313 /*
314  * Handler for setting correct codec rate - called when rate change is detected
315  */
316 static void stac9460_set_rate_val(struct snd_akm4xxx *ak, unsigned int rate)
317 {
318         unsigned char old, new;
319         int idx;
320         unsigned char changed[7];
321         struct snd_ice1712 *ice = ak->private_data[0];
322
323         if (rate == 0)  /* no hint - S/PDIF input is master, simply return */
324                 return;
325         else if (rate <= 48000)
326                 new = 0x08;     /* 256x, base rate mode */
327         else if (rate <= 96000)
328                 new = 0x11;     /* 256x, mid rate mode */
329         else
330                 new = 0x12;     /* 128x, high rate mode */
331         old = stac9460_get(ice, STAC946X_MASTER_CLOCKING);
332         if (old == new)
333                 return;
334         /* change detected, setting master clock, muting first */
335         /* due to possible conflicts with mute controls - mutexing */
336         mutex_lock(&ice->spec.prodigy192.mute_mutex);
337         /* we have to remember current mute status for each DAC */
338         for (idx = 0; idx < 7 ; ++idx)
339                 changed[idx] = stac9460_dac_mute(ice,
340                                 STAC946X_MASTER_VOLUME + idx, 0);
341         /*printk("Rate change: %d, new MC: 0x%02x\n", rate, new);*/
342         stac9460_put(ice, STAC946X_MASTER_CLOCKING, new);
343         udelay(10);
344         /* unmuting - only originally unmuted dacs -
345          * i.e. those changed when muting */
346         for (idx = 0; idx < 7 ; ++idx) {
347                 if (changed[idx])
348                         stac9460_dac_mute(ice, STAC946X_MASTER_VOLUME + idx, 1);
349         }
350         mutex_unlock(&ice->spec.prodigy192.mute_mutex);
351 }
352
353 /* using akm infrastructure for setting rate of the codec */
354 static struct snd_akm4xxx akmlike_stac9460 __devinitdata = {
355         .type = NON_AKM,        /* special value */
356         .num_adcs = 6,          /* not used in any way, just for completeness */
357         .num_dacs = 2,
358         .ops = {
359                 .set_rate_val = stac9460_set_rate_val
360         }
361 };
362
363
364 static const DECLARE_TLV_DB_SCALE(db_scale_dac, -19125, 75, 0);
365 static const DECLARE_TLV_DB_SCALE(db_scale_adc, 0, 150, 0);
366
367 /*
368  * mixers
369  */
370
371 static struct snd_kcontrol_new stac_controls[] __devinitdata = {
372         {
373                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
374                 .name = "Master Playback Switch",
375                 .info = stac9460_dac_mute_info,
376                 .get = stac9460_dac_mute_get,
377                 .put = stac9460_dac_mute_put,
378                 .private_value = 1,
379                 .tlv = { .p = db_scale_dac }
380         },
381         {
382                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
383                 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
384                            SNDRV_CTL_ELEM_ACCESS_TLV_READ),
385                 .name = "Master Playback Volume",
386                 .info = stac9460_dac_vol_info,
387                 .get = stac9460_dac_vol_get,
388                 .put = stac9460_dac_vol_put,
389                 .private_value = 1,
390                 .tlv = { .p = db_scale_dac }
391         },
392         {
393                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
394                 .name = "DAC Switch",
395                 .count = 6,
396                 .info = stac9460_dac_mute_info,
397                 .get = stac9460_dac_mute_get,
398                 .put = stac9460_dac_mute_put,
399         },
400         {
401                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
402                 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
403                            SNDRV_CTL_ELEM_ACCESS_TLV_READ),
404                 .name = "DAC Volume",
405                 .count = 6,
406                 .info = stac9460_dac_vol_info,
407                 .get = stac9460_dac_vol_get,
408                 .put = stac9460_dac_vol_put,
409                 .tlv = { .p = db_scale_dac }
410         },
411         {
412                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
413                 .name = "ADC Capture Switch",
414                 .count = 1,
415                 .info = stac9460_adc_mute_info,
416                 .get = stac9460_adc_mute_get,
417                 .put = stac9460_adc_mute_put,
418
419         },
420         {
421                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
422                 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
423                            SNDRV_CTL_ELEM_ACCESS_TLV_READ),
424                 .name = "ADC Capture Volume",
425                 .count = 1,
426                 .info = stac9460_adc_vol_info,
427                 .get = stac9460_adc_vol_get,
428                 .put = stac9460_adc_vol_put,
429                 .tlv = { .p = db_scale_adc }
430         },
431         {
432                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
433                 .name = "Analog Capture Input",
434                 .info = stac9460_mic_sw_info,
435                 .get = stac9460_mic_sw_get,
436                 .put = stac9460_mic_sw_put,
437
438         },
439 };
440
441 /* AK4114 - ICE1724 connections on Prodigy192 + MI/ODI/O */
442 /* CDTO (pin 32) -- GPIO11 pin 86
443  * CDTI (pin 33) -- GPIO10 pin 77
444  * CCLK (pin 34) -- GPIO9 pin 76
445  * CSN  (pin 35) -- GPIO8 pin 75
446  */
447 #define AK4114_ADDR     0x00 /* C1-C0: Chip Address
448                               * (According to datasheet fixed to “00”)
449                               */
450
451 /*
452  * 4wire ak4114 protocol - writing data
453  */
454 static void write_data(struct snd_ice1712 *ice, unsigned int gpio,
455                        unsigned int data, int idx)
456 {
457         for (; idx >= 0; idx--) {
458                 /* drop clock */
459                 gpio &= ~VT1724_PRODIGY192_CCLK;
460                 snd_ice1712_gpio_write(ice, gpio);
461                 udelay(1);
462                 /* set data */
463                 if (data & (1 << idx))
464                         gpio |= VT1724_PRODIGY192_CDOUT;
465                 else
466                         gpio &= ~VT1724_PRODIGY192_CDOUT;
467                 snd_ice1712_gpio_write(ice, gpio);
468                 udelay(1);
469                 /* raise clock */
470                 gpio |= VT1724_PRODIGY192_CCLK;
471                 snd_ice1712_gpio_write(ice, gpio);
472                 udelay(1);
473         }
474 }
475
476 /*
477  * 4wire ak4114 protocol - reading data
478  */
479 static unsigned char read_data(struct snd_ice1712 *ice, unsigned int gpio,
480                                int idx)
481 {
482         unsigned char data = 0;
483
484         for (; idx >= 0; idx--) {
485                 /* drop clock */
486                 gpio &= ~VT1724_PRODIGY192_CCLK;
487                 snd_ice1712_gpio_write(ice, gpio);
488                 udelay(1);
489                 /* read data */
490                 if (snd_ice1712_gpio_read(ice) & VT1724_PRODIGY192_CDIN)
491                         data |= (1 << idx);
492                 udelay(1);
493                 /* raise clock */
494                 gpio |= VT1724_PRODIGY192_CCLK;
495                 snd_ice1712_gpio_write(ice, gpio);
496                 udelay(1);
497         }
498         return data;
499 }
500 /*
501  * 4wire ak4114 protocol - starting sequence
502  */
503 static unsigned int prodigy192_4wire_start(struct snd_ice1712 *ice)
504 {
505         unsigned int tmp;
506
507         snd_ice1712_save_gpio_status(ice);
508         tmp = snd_ice1712_gpio_read(ice);
509
510         tmp |= VT1724_PRODIGY192_CCLK; /* high at init */
511         tmp &= ~VT1724_PRODIGY192_CS; /* drop chip select */
512         snd_ice1712_gpio_write(ice, tmp);
513         udelay(1);
514         return tmp;
515 }
516
517 /*
518  * 4wire ak4114 protocol - final sequence
519  */
520 static void prodigy192_4wire_finish(struct snd_ice1712 *ice, unsigned int tmp)
521 {
522         tmp |= VT1724_PRODIGY192_CS; /* raise chip select */
523         snd_ice1712_gpio_write(ice, tmp);
524         udelay(1);
525         snd_ice1712_restore_gpio_status(ice);
526 }
527
528 /*
529  * Write data to addr register of ak4114
530  */
531 static void prodigy192_ak4114_write(void *private_data, unsigned char addr,
532                                unsigned char data)
533 {
534         struct snd_ice1712 *ice = private_data;
535         unsigned int tmp, addrdata;
536         tmp = prodigy192_4wire_start(ice);
537         addrdata = (AK4114_ADDR << 6) | 0x20 | (addr & 0x1f);
538         addrdata = (addrdata << 8) | data;
539         write_data(ice, tmp, addrdata, 15);
540         prodigy192_4wire_finish(ice, tmp);
541 }
542
543 /*
544  * Read data from addr register of ak4114
545  */
546 static unsigned char prodigy192_ak4114_read(void *private_data,
547                                             unsigned char addr)
548 {
549         struct snd_ice1712 *ice = private_data;
550         unsigned int tmp;
551         unsigned char data;
552
553         tmp = prodigy192_4wire_start(ice);
554         write_data(ice, tmp, (AK4114_ADDR << 6) | (addr & 0x1f), 7);
555         data = read_data(ice, tmp, 7);
556         prodigy192_4wire_finish(ice, tmp);
557         return data;
558 }
559
560
561 static int ak4114_input_sw_info(struct snd_kcontrol *kcontrol,
562                                 struct snd_ctl_elem_info *uinfo)
563 {
564         static char *texts[2] = { "Toslink", "Coax" };
565
566         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
567         uinfo->count = 1;
568         uinfo->value.enumerated.items = 2;
569         if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
570                 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
571         strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
572         return 0;
573 }
574
575
576 static int ak4114_input_sw_get(struct snd_kcontrol *kcontrol,
577                         struct snd_ctl_elem_value *ucontrol)
578 {
579         struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
580         unsigned char val;
581                 
582         val = prodigy192_ak4114_read(ice, AK4114_REG_IO1);
583         /* AK4114_IPS0 bit = 0 -> RX0 = Toslink
584          * AK4114_IPS0 bit = 1 -> RX1 = Coax
585          */
586         ucontrol->value.enumerated.item[0] = (val & AK4114_IPS0) ? 1 : 0;
587         return 0;
588 }
589
590 static int ak4114_input_sw_put(struct snd_kcontrol *kcontrol,
591                         struct snd_ctl_elem_value *ucontrol)
592 {
593         struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
594         unsigned char new, old, itemvalue;
595         int change;
596
597         old = prodigy192_ak4114_read(ice, AK4114_REG_IO1);
598         /* AK4114_IPS0 could be any bit */
599         itemvalue = (ucontrol->value.enumerated.item[0]) ? 0xff : 0x00;
600
601         new = (itemvalue & AK4114_IPS0) | (old & ~AK4114_IPS0);
602         change = (new != old);
603         if (change)
604                 prodigy192_ak4114_write(ice, AK4114_REG_IO1, new);
605         return change;
606 }
607
608
609 static struct snd_kcontrol_new ak4114_controls[] __devinitdata = {
610         {
611                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
612                 .name = "MIODIO IEC958 Capture Input",
613                 .info = ak4114_input_sw_info,
614                 .get = ak4114_input_sw_get,
615                 .put = ak4114_input_sw_put,
616
617         }
618 };
619
620
621 static int prodigy192_ak4114_init(struct snd_ice1712 *ice)
622 {
623         static const unsigned char ak4114_init_vals[] = {
624                 AK4114_RST | AK4114_PWN | AK4114_OCKS0 | AK4114_OCKS1,
625                 /* ice1724 expects I2S and provides clock,
626                  * DEM0 disables the deemphasis filter
627                  */
628                 AK4114_DIF_I24I2S | AK4114_DEM0 ,
629                 AK4114_TX1E,
630                 AK4114_EFH_1024 | AK4114_DIT, /* default input RX0 */
631                 0,
632                 0
633         };
634         static const unsigned char ak4114_init_txcsb[] = {
635                 0x41, 0x02, 0x2c, 0x00, 0x00
636         };
637
638         return snd_ak4114_create(ice->card,
639                                  prodigy192_ak4114_read,
640                                  prodigy192_ak4114_write,
641                                  ak4114_init_vals, ak4114_init_txcsb,
642                                  ice, &ice->spec.prodigy192.ak4114);
643 }
644
645 static void stac9460_proc_regs_read(struct snd_info_entry *entry,
646                 struct snd_info_buffer *buffer)
647 {
648         struct snd_ice1712 *ice = (struct snd_ice1712 *)entry->private_data;
649         int reg, val;
650         /* registers 0x0 - 0x14 */
651         for (reg = 0; reg <= 0x15; reg++) {
652                 val = stac9460_get(ice, reg);
653                 snd_iprintf(buffer, "0x%02x = 0x%02x\n", reg, val);
654         }
655 }
656
657
658 static void stac9460_proc_init(struct snd_ice1712 *ice)
659 {
660         struct snd_info_entry *entry;
661         if (!snd_card_proc_new(ice->card, "stac9460_codec", &entry))
662                 snd_info_set_text_ops(entry, ice, stac9460_proc_regs_read);
663 }
664
665
666 static int __devinit prodigy192_add_controls(struct snd_ice1712 *ice)
667 {
668         unsigned int i;
669         int err;
670
671         for (i = 0; i < ARRAY_SIZE(stac_controls); i++) {
672                 err = snd_ctl_add(ice->card,
673                                   snd_ctl_new1(&stac_controls[i], ice));
674                 if (err < 0)
675                         return err;
676         }
677         if (ice->spec.prodigy192.ak4114) {
678                 /* ak4114 is connected */
679                 for (i = 0; i < ARRAY_SIZE(ak4114_controls); i++) {
680                         err = snd_ctl_add(ice->card,
681                                           snd_ctl_new1(&ak4114_controls[i],
682                                                        ice));
683                         if (err < 0)
684                                 return err;
685                 }
686                 err = snd_ak4114_build(ice->spec.prodigy192.ak4114,
687                                 NULL, /* ak4114 in MIO/DI/O handles no IEC958 output */
688                                 ice->pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream);
689                 if (err < 0)
690                         return err;
691         }
692         stac9460_proc_init(ice);
693         return 0;
694 }
695
696 /*
697  * check for presence of MI/ODI/O add-on card with digital inputs
698  */
699 static int prodigy192_miodio_exists(struct snd_ice1712 *ice)
700 {
701
702         unsigned char orig_value;
703         const unsigned char test_data = 0xd1;   /* random value */
704         unsigned char addr = AK4114_REG_INT0_MASK; /* random SAFE address */
705         int exists = 0;
706
707         orig_value = prodigy192_ak4114_read(ice, addr);
708         prodigy192_ak4114_write(ice, addr, test_data);
709         if (prodigy192_ak4114_read(ice, addr) == test_data) {
710                 /* ak4114 seems to communicate, apparently exists */
711                 /* writing back original value */
712                 prodigy192_ak4114_write(ice, addr, orig_value);
713                 exists = 1;
714         }
715         return exists;
716 }
717
718 /*
719  * initialize the chip
720  */
721 static int __devinit prodigy192_init(struct snd_ice1712 *ice)
722 {
723         static const unsigned short stac_inits_prodigy[] = {
724                 STAC946X_RESET, 0,
725                 STAC946X_MASTER_CLOCKING, 0x11,
726 /*              STAC946X_MASTER_VOLUME, 0,
727                 STAC946X_LF_VOLUME, 0,
728                 STAC946X_RF_VOLUME, 0,
729                 STAC946X_LR_VOLUME, 0,
730                 STAC946X_RR_VOLUME, 0,
731                 STAC946X_CENTER_VOLUME, 0,
732                 STAC946X_LFE_VOLUME, 0,*/
733                 (unsigned short)-1
734         };
735         const unsigned short *p;
736         int err = 0;
737         struct snd_akm4xxx *ak;
738
739         /* prodigy 192 */
740         ice->num_total_dacs = 6;
741         ice->num_total_adcs = 2;
742         ice->vt1720 = 0;  /* ice1724, e.g. 23 GPIOs */
743         
744         /* initialize codec */
745         p = stac_inits_prodigy;
746         for (; *p != (unsigned short)-1; p += 2)
747                 stac9460_put(ice, p[0], p[1]);
748         /* reusing the akm codecs infrastructure,
749          * for setting rate on stac9460 */
750         ak = ice->akm = kmalloc(sizeof(struct snd_akm4xxx), GFP_KERNEL);
751         if (!ak)
752                 return -ENOMEM;
753         ice->akm_codecs = 1;
754         err = snd_ice1712_akm4xxx_init(ak, &akmlike_stac9460, NULL, ice);
755         if (err < 0)
756                 return err;
757
758         /* MI/ODI/O add on card with AK4114 */
759         if (prodigy192_miodio_exists(ice)) {
760                 err = prodigy192_ak4114_init(ice);
761                 /* from this moment if err = 0 then
762                  * ice->spec.prodigy192.ak4114 should not be null
763                  */
764                 snd_printdd("AK4114 initialized with status %d\n", err);
765         } else
766                 snd_printdd("AK4114 not found\n");
767         if (err < 0)
768                 return err;
769
770         mutex_init(&ice->spec.prodigy192.mute_mutex);
771
772         return 0;
773 }
774
775
776 /*
777  * Aureon boards don't provide the EEPROM data except for the vendor IDs.
778  * hence the driver needs to sets up it properly.
779  */
780
781 static unsigned char prodigy71_eeprom[] __devinitdata = {
782         [ICE_EEP2_SYSCONF]     = 0x6a,  /* 49MHz crystal, mpu401,
783                                          * spdif-in+ 1 stereo ADC,
784                                          * 3 stereo DACs
785                                          */
786         [ICE_EEP2_ACLINK]      = 0x80,  /* I2S */
787         [ICE_EEP2_I2S]         = 0xf8,  /* vol, 96k, 24bit, 192k */
788         [ICE_EEP2_SPDIF]       = 0xc3,  /* out-en, out-int, spdif-in */
789         [ICE_EEP2_GPIO_DIR]    = 0xff,
790         [ICE_EEP2_GPIO_DIR1]   = ~(VT1724_PRODIGY192_CDIN >> 8) ,
791         [ICE_EEP2_GPIO_DIR2]   = 0xbf,
792         [ICE_EEP2_GPIO_MASK]   = 0x00,
793         [ICE_EEP2_GPIO_MASK1]  = 0x00,
794         [ICE_EEP2_GPIO_MASK2]  = 0x00,
795         [ICE_EEP2_GPIO_STATE]  = 0x00,
796         [ICE_EEP2_GPIO_STATE1] = 0x00,
797         [ICE_EEP2_GPIO_STATE2] = 0x10,  /* GPIO20: 0 = CD drive dig. input
798                                          * passthrough,
799                                          * 1 = SPDIF-OUT from ice1724
800                                          */
801 };
802
803
804 /* entry point */
805 struct snd_ice1712_card_info snd_vt1724_prodigy192_cards[] __devinitdata = {
806         {
807                 .subvendor = VT1724_SUBDEVICE_PRODIGY192VE,
808                 .name = "Audiotrak Prodigy 192",
809                 .model = "prodigy192",
810                 .chip_init = prodigy192_init,
811                 .build_controls = prodigy192_add_controls,
812                 .eeprom_size = sizeof(prodigy71_eeprom),
813                 .eeprom_data = prodigy71_eeprom,
814                 /* the current MPU401 code loops infinitely
815                  * when opening midi device
816                  */
817                 .no_mpu401 = 1,
818         },
819         { } /* terminator */
820 };