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