d18a31e188a9a594ccbe8b0ed22a487bec783703
[pandora-kernel.git] / sound / pci / ice1712 / revo.c
1 /*
2  *   ALSA driver for ICEnsemble ICE1712 (Envy24)
3  *
4  *   Lowlevel functions for M-Audio Revolution 7.1
5  *
6  *      Copyright (c) 2003 Takashi Iwai <tiwai@suse.de>
7  *
8  *   This program is free software; you can redistribute it and/or modify
9  *   it under the terms of the GNU General Public License as published by
10  *   the Free Software Foundation; either version 2 of the License, or
11  *   (at your option) any later version.
12  *
13  *   This program is distributed in the hope that it will be useful,
14  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *   GNU General Public License for more details.
17  *
18  *   You should have received a copy of the GNU General Public License
19  *   along with this program; if not, write to the Free Software
20  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
21  *
22  */      
23
24 #include <sound/driver.h>
25 #include <asm/io.h>
26 #include <linux/delay.h>
27 #include <linux/interrupt.h>
28 #include <linux/init.h>
29 #include <linux/slab.h>
30 #include <sound/core.h>
31
32 #include "ice1712.h"
33 #include "envy24ht.h"
34 #include "revo.h"
35
36 static void revo_i2s_mclk_changed(struct snd_ice1712 *ice)
37 {
38         /* assert PRST# to converters; MT05 bit 7 */
39         outb(inb(ICEMT1724(ice, AC97_CMD)) | 0x80, ICEMT1724(ice, AC97_CMD));
40         mdelay(5);
41         /* deassert PRST# */
42         outb(inb(ICEMT1724(ice, AC97_CMD)) & ~0x80, ICEMT1724(ice, AC97_CMD));
43 }
44
45 /*
46  * change the rate of envy24HT, AK4355 and AK4381
47  */
48 static void revo_set_rate_val(struct snd_akm4xxx *ak, unsigned int rate)
49 {
50         unsigned char old, tmp, dfs;
51         int reg, shift;
52
53         if (rate == 0)  /* no hint - S/PDIF input is master, simply return */
54                 return;
55
56         /* adjust DFS on codecs */
57         if (rate > 96000)
58                 dfs = 2;
59         else if (rate > 48000)
60                 dfs = 1;
61         else
62                 dfs = 0;
63
64         if (ak->type == SND_AK4355 || ak->type == SND_AK4358) {
65                 reg = 2;
66                 shift = 4;
67         } else {
68                 reg = 1;
69                 shift = 3;
70         }
71         tmp = snd_akm4xxx_get(ak, 0, reg);
72         old = (tmp >> shift) & 0x03;
73         if (old == dfs)
74                 return;
75
76         /* reset DFS */
77         snd_akm4xxx_reset(ak, 1);
78         tmp = snd_akm4xxx_get(ak, 0, reg);
79         tmp &= ~(0x03 << shift);
80         tmp |= dfs << shift;
81         // snd_akm4xxx_write(ak, 0, reg, tmp);
82         snd_akm4xxx_set(ak, 0, reg, tmp); /* the value is written in reset(0) */
83         snd_akm4xxx_reset(ak, 0);
84 }
85
86 /*
87  * I2C access to the PT2258 volume controller on GPIO 6/7 (Revolution 5.1)
88  */
89
90 static void revo_i2c_start(struct snd_i2c_bus *bus)
91 {
92         struct snd_ice1712 *ice = bus->private_data;
93         snd_ice1712_save_gpio_status(ice);
94 }
95
96 static void revo_i2c_stop(struct snd_i2c_bus *bus)
97 {
98         struct snd_ice1712 *ice = bus->private_data;
99         snd_ice1712_restore_gpio_status(ice);
100 }
101
102 static void revo_i2c_direction(struct snd_i2c_bus *bus, int clock, int data)
103 {
104         struct snd_ice1712 *ice = bus->private_data;
105         unsigned int mask, val;
106
107         val = 0;
108         if (clock)
109                 val |= VT1724_REVO_I2C_CLOCK;   /* write SCL */
110         if (data)
111                 val |= VT1724_REVO_I2C_DATA;    /* write SDA */
112         mask = VT1724_REVO_I2C_CLOCK | VT1724_REVO_I2C_DATA;
113         ice->gpio.direction &= ~mask;
114         ice->gpio.direction |= val;
115         snd_ice1712_gpio_set_dir(ice, ice->gpio.direction);
116         snd_ice1712_gpio_set_mask(ice, ~mask);
117 }
118
119 static void revo_i2c_setlines(struct snd_i2c_bus *bus, int clk, int data)
120 {
121         struct snd_ice1712 *ice = bus->private_data;
122         unsigned int val = 0;
123
124         if (clk)
125                 val |= VT1724_REVO_I2C_CLOCK;
126         if (data)
127                 val |= VT1724_REVO_I2C_DATA;
128         snd_ice1712_gpio_write_bits(ice,
129                                     VT1724_REVO_I2C_DATA |
130                                     VT1724_REVO_I2C_CLOCK, val);
131         udelay(5);
132 }
133
134 static int revo_i2c_getdata(struct snd_i2c_bus *bus, int ack)
135 {
136         struct snd_ice1712 *ice = bus->private_data;
137         int bit;
138
139         if (ack)
140                 udelay(5);
141         bit = snd_ice1712_gpio_read_bits(ice, VT1724_REVO_I2C_DATA) ? 1 : 0;
142         return bit;
143 }
144
145 static struct snd_i2c_bit_ops revo51_bit_ops = {
146         .start = revo_i2c_start,
147         .stop = revo_i2c_stop,
148         .direction = revo_i2c_direction,
149         .setlines = revo_i2c_setlines,
150         .getdata = revo_i2c_getdata,
151 };
152
153 static int revo51_i2c_init(struct snd_ice1712 *ice,
154                            struct snd_pt2258 *pt)
155 {
156         int err;
157
158         /* create the I2C bus */
159         err = snd_i2c_bus_create(ice->card, "ICE1724 GPIO6", NULL, &ice->i2c);
160         if (err < 0)
161                 return err;
162
163         ice->i2c->private_data = ice;
164         ice->i2c->hw_ops.bit = &revo51_bit_ops;
165
166         /* create the I2C device */
167         err = snd_i2c_device_create(ice->i2c, "PT2258", 0x40,
168                                     &ice->spec.revo51.dev);
169         if (err < 0)
170                 return err;
171
172         pt->card = ice->card;
173         pt->i2c_bus = ice->i2c;
174         pt->i2c_dev = ice->spec.revo51.dev;
175         ice->spec.revo51.pt2258 = pt;
176
177         snd_pt2258_reset(pt);
178
179         return 0;
180 }
181
182 /*
183  * initialize the chips on M-Audio Revolution cards
184  */
185
186 #define AK_DAC(xname,xch) { .name = xname, .num_channels = xch }
187
188 static const struct snd_akm4xxx_dac_channel revo71_front[] = {
189         {
190                 .name = "PCM Playback Volume",
191                 .num_channels = 2,
192                 /* front channels DAC supports muting */
193                 .switch_name = "PCM Playback Switch",
194         },
195 };
196
197 static const struct snd_akm4xxx_dac_channel revo71_surround[] = {
198         AK_DAC("PCM Center Playback Volume", 1),
199         AK_DAC("PCM LFE Playback Volume", 1),
200         AK_DAC("PCM Side Playback Volume", 2),
201         AK_DAC("PCM Rear Playback Volume", 2),
202 };
203
204 static const struct snd_akm4xxx_dac_channel revo51_dac[] = {
205         AK_DAC("PCM Playback Volume", 2),
206         AK_DAC("PCM Center Playback Volume", 1),
207         AK_DAC("PCM LFE Playback Volume", 1),
208         AK_DAC("PCM Rear Playback Volume", 2),
209 };
210
211 static const char *revo51_adc_input_names[] = {
212         "Mic",
213         "Line",
214         "CD",
215         NULL
216 };
217
218 static const struct snd_akm4xxx_adc_channel revo51_adc[] = {
219         {
220                 .name = "PCM Capture Volume",
221                 .switch_name = "PCM Capture Switch",
222                 .num_channels = 2,
223                 .input_names = revo51_adc_input_names
224         },
225 };
226
227 static struct snd_akm4xxx akm_revo_front __devinitdata = {
228         .type = SND_AK4381,
229         .num_dacs = 2,
230         .ops = {
231                 .set_rate_val = revo_set_rate_val
232         },
233         .dac_info = revo71_front,
234 };
235
236 static struct snd_ak4xxx_private akm_revo_front_priv __devinitdata = {
237         .caddr = 1,
238         .cif = 0,
239         .data_mask = VT1724_REVO_CDOUT,
240         .clk_mask = VT1724_REVO_CCLK,
241         .cs_mask = VT1724_REVO_CS0 | VT1724_REVO_CS1 | VT1724_REVO_CS2,
242         .cs_addr = VT1724_REVO_CS0 | VT1724_REVO_CS2,
243         .cs_none = VT1724_REVO_CS0 | VT1724_REVO_CS1 | VT1724_REVO_CS2,
244         .add_flags = VT1724_REVO_CCLK, /* high at init */
245         .mask_flags = 0,
246 };
247
248 static struct snd_akm4xxx akm_revo_surround __devinitdata = {
249         .type = SND_AK4355,
250         .idx_offset = 1,
251         .num_dacs = 6,
252         .ops = {
253                 .set_rate_val = revo_set_rate_val
254         },
255         .dac_info = revo71_surround,
256 };
257
258 static struct snd_ak4xxx_private akm_revo_surround_priv __devinitdata = {
259         .caddr = 3,
260         .cif = 0,
261         .data_mask = VT1724_REVO_CDOUT,
262         .clk_mask = VT1724_REVO_CCLK,
263         .cs_mask = VT1724_REVO_CS0 | VT1724_REVO_CS1 | VT1724_REVO_CS2,
264         .cs_addr = VT1724_REVO_CS0 | VT1724_REVO_CS1,
265         .cs_none = VT1724_REVO_CS0 | VT1724_REVO_CS1 | VT1724_REVO_CS2,
266         .add_flags = VT1724_REVO_CCLK, /* high at init */
267         .mask_flags = 0,
268 };
269
270 static struct snd_akm4xxx akm_revo51 __devinitdata = {
271         .type = SND_AK4358,
272         .num_dacs = 6,
273         .ops = {
274                 .set_rate_val = revo_set_rate_val
275         },
276         .dac_info = revo51_dac,
277 };
278
279 static struct snd_ak4xxx_private akm_revo51_priv __devinitdata = {
280         .caddr = 2,
281         .cif = 0,
282         .data_mask = VT1724_REVO_CDOUT,
283         .clk_mask = VT1724_REVO_CCLK,
284         .cs_mask = VT1724_REVO_CS0 | VT1724_REVO_CS1,
285         .cs_addr = VT1724_REVO_CS1,
286         .cs_none = VT1724_REVO_CS0 | VT1724_REVO_CS1,
287         .add_flags = VT1724_REVO_CCLK, /* high at init */
288         .mask_flags = 0,
289 };
290
291 static struct snd_akm4xxx akm_revo51_adc __devinitdata = {
292         .type = SND_AK5365,
293         .num_adcs = 2,
294         .adc_info = revo51_adc,
295 };
296
297 static struct snd_ak4xxx_private akm_revo51_adc_priv __devinitdata = {
298         .caddr = 2,
299         .cif = 0,
300         .data_mask = VT1724_REVO_CDOUT,
301         .clk_mask = VT1724_REVO_CCLK,
302         .cs_mask = VT1724_REVO_CS0 | VT1724_REVO_CS1,
303         .cs_addr = VT1724_REVO_CS0,
304         .cs_none = VT1724_REVO_CS0 | VT1724_REVO_CS1,
305         .add_flags = VT1724_REVO_CCLK, /* high at init */
306         .mask_flags = 0,
307 };
308
309 static struct snd_pt2258 ptc_revo51_volume;
310
311 /* AK4358 for AP192 DAC, AK5385A for ADC */
312 static void ap192_set_rate_val(struct snd_akm4xxx *ak, unsigned int rate)
313 {
314         struct snd_ice1712 *ice = ak->private_data[0];
315
316         revo_set_rate_val(ak, rate);
317
318 #if 1 /* FIXME: do we need this procedure? */
319         /* reset DFS pin of AK5385A for ADC, too */
320         /* DFS0 (pin 18) -- GPIO10 pin 77 */
321         snd_ice1712_save_gpio_status(ice);
322         snd_ice1712_gpio_write_bits(ice, 1 << 10,
323                                     rate > 48000 ? (1 << 10) : 0);
324         snd_ice1712_restore_gpio_status(ice);
325 #endif
326 }
327
328 static const struct snd_akm4xxx_dac_channel ap192_dac[] = {
329         AK_DAC("PCM Playback Volume", 2)
330 };
331
332 static struct snd_akm4xxx akm_ap192 __devinitdata = {
333         .type = SND_AK4358,
334         .num_dacs = 2,
335         .ops = {
336                 .set_rate_val = ap192_set_rate_val
337         },
338         .dac_info = ap192_dac,
339 };
340
341 static struct snd_ak4xxx_private akm_ap192_priv __devinitdata = {
342         .caddr = 2,
343         .cif = 0,
344         .data_mask = VT1724_REVO_CDOUT,
345         .clk_mask = VT1724_REVO_CCLK,
346         .cs_mask = VT1724_REVO_CS0 | VT1724_REVO_CS3,
347         .cs_addr = VT1724_REVO_CS3,
348         .cs_none = VT1724_REVO_CS0 | VT1724_REVO_CS3,
349         .add_flags = VT1724_REVO_CCLK, /* high at init */
350         .mask_flags = 0,
351 };
352
353 #if 0
354 /* FIXME: ak4114 makes the sound much lower due to some confliction,
355  *        so let's disable it right now...
356  */
357 #define BUILD_AK4114_AP192
358 #endif
359
360 #ifdef BUILD_AK4114_AP192
361 /* AK4114 support on Audiophile 192 */
362 /* CDTO (pin 32) -- GPIO2 pin 52
363  * CDTI (pin 33) -- GPIO3 pin 53 (shared with AK4358)
364  * CCLK (pin 34) -- GPIO1 pin 51 (shared with AK4358)
365  * CSN  (pin 35) -- GPIO7 pin 59
366  */
367 #define AK4114_ADDR     0x00
368
369 static void write_data(struct snd_ice1712 *ice, unsigned int gpio,
370                        unsigned int data, int idx)
371 {
372         for (; idx >= 0; idx--) {
373                 /* drop clock */
374                 gpio &= ~VT1724_REVO_CCLK;
375                 snd_ice1712_gpio_write(ice, gpio);
376                 udelay(1);
377                 /* set data */
378                 if (data & (1 << idx))
379                         gpio |= VT1724_REVO_CDOUT;
380                 else
381                         gpio &= ~VT1724_REVO_CDOUT;
382                 snd_ice1712_gpio_write(ice, gpio);
383                 udelay(1);
384                 /* raise clock */
385                 gpio |= VT1724_REVO_CCLK;
386                 snd_ice1712_gpio_write(ice, gpio);
387                 udelay(1);
388         }
389 }
390
391 static unsigned char read_data(struct snd_ice1712 *ice, unsigned int gpio,
392                                int idx)
393 {
394         unsigned char data = 0;
395
396         for (; idx >= 0; idx--) {
397                 /* drop clock */
398                 gpio &= ~VT1724_REVO_CCLK;
399                 snd_ice1712_gpio_write(ice, gpio);
400                 udelay(1);
401                 /* read data */
402                 if (snd_ice1712_gpio_read(ice) & VT1724_REVO_CDIN)
403                         data |= (1 << idx);
404                 udelay(1);
405                 /* raise clock */
406                 gpio |= VT1724_REVO_CCLK;
407                 snd_ice1712_gpio_write(ice, gpio);
408                 udelay(1);
409         }
410         return data;
411 }
412
413 static unsigned int ap192_4wire_start(struct snd_ice1712 *ice)
414 {
415         unsigned int tmp;
416
417         snd_ice1712_save_gpio_status(ice);
418         tmp = snd_ice1712_gpio_read(ice);
419         tmp |= VT1724_REVO_CCLK; /* high at init */
420         tmp |= VT1724_REVO_CS0;
421         tmp &= ~VT1724_REVO_CS3;
422         snd_ice1712_gpio_write(ice, tmp);
423         udelay(1);
424         return tmp;
425 }
426
427 static void ap192_4wire_finish(struct snd_ice1712 *ice, unsigned int tmp)
428 {
429         tmp |= VT1724_REVO_CS3;
430         tmp |= VT1724_REVO_CS0;
431         snd_ice1712_gpio_write(ice, tmp);
432         udelay(1);
433         snd_ice1712_restore_gpio_status(ice);
434 }
435
436 static void ap192_ak4114_write(void *private_data, unsigned char addr,
437                                unsigned char data)
438 {
439         struct snd_ice1712 *ice = private_data;
440         unsigned int tmp, addrdata;
441
442         tmp = ap192_4wire_start(ice);
443         addrdata = (AK4114_ADDR << 6) | 0x20 | (addr & 0x1f);
444         addrdata = (addrdata << 8) | data;
445         write_data(ice, tmp, addrdata, 15);
446         ap192_4wire_finish(ice, tmp);
447 }
448
449 static unsigned char ap192_ak4114_read(void *private_data, unsigned char addr)
450 {
451         struct snd_ice1712 *ice = private_data;
452         unsigned int tmp;
453         unsigned char data;
454
455         tmp = ap192_4wire_start(ice);
456         write_data(ice, tmp, (AK4114_ADDR << 6) | (addr & 0x1f), 7);
457         data = read_data(ice, tmp, 7);
458         ap192_4wire_finish(ice, tmp);
459         return data;
460 }
461
462 static int __devinit ap192_ak4114_init(struct snd_ice1712 *ice)
463 {
464         static const unsigned char ak4114_init_vals[] = {
465                 AK4114_RST | AK4114_PWN | AK4114_OCKS0 | AK4114_OCKS1,
466                 AK4114_DIF_I24I2S,
467                 AK4114_TX1E,
468                 AK4114_EFH_1024 | AK4114_DIT | AK4114_IPS(1),
469                 0,
470                 0
471         };
472         static const unsigned char ak4114_init_txcsb[] = {
473                 0x41, 0x02, 0x2c, 0x00, 0x00
474         };
475         struct ak4114 *ak;
476         int err;
477
478         return snd_ak4114_create(ice->card,
479                                  ap192_ak4114_read,
480                                  ap192_ak4114_write,
481                                  ak4114_init_vals, ak4114_init_txcsb,
482                                  ice, &ak);
483 }
484 #endif /* BUILD_AK4114_AP192 */
485
486 static int __devinit revo_init(struct snd_ice1712 *ice)
487 {
488         struct snd_akm4xxx *ak;
489         int err;
490
491         /* determine I2C, DACs and ADCs */
492         switch (ice->eeprom.subvendor) {
493         case VT1724_SUBDEVICE_REVOLUTION71:
494                 ice->num_total_dacs = 8;
495                 ice->num_total_adcs = 2;
496                 ice->gpio.i2s_mclk_changed = revo_i2s_mclk_changed;
497                 break;
498         case VT1724_SUBDEVICE_REVOLUTION51:
499                 ice->num_total_dacs = 6;
500                 ice->num_total_adcs = 2;
501                 break;
502         case VT1724_SUBDEVICE_AUDIOPHILE192:
503                 ice->num_total_dacs = 2;
504                 ice->num_total_adcs = 2;
505                 break;
506         default:
507                 snd_BUG();
508                 return -EINVAL;
509         }
510
511         /* second stage of initialization, analog parts and others */
512         ak = ice->akm = kcalloc(2, sizeof(struct snd_akm4xxx), GFP_KERNEL);
513         if (! ak)
514                 return -ENOMEM;
515         ice->akm_codecs = 2;
516         switch (ice->eeprom.subvendor) {
517         case VT1724_SUBDEVICE_REVOLUTION71:
518                 ice->akm_codecs = 2;
519                 if ((err = snd_ice1712_akm4xxx_init(ak, &akm_revo_front, &akm_revo_front_priv, ice)) < 0)
520                         return err;
521                 if ((err = snd_ice1712_akm4xxx_init(ak + 1, &akm_revo_surround, &akm_revo_surround_priv, ice)) < 0)
522                         return err;
523                 /* unmute all codecs */
524                 snd_ice1712_gpio_write_bits(ice, VT1724_REVO_MUTE, VT1724_REVO_MUTE);
525                 break;
526         case VT1724_SUBDEVICE_REVOLUTION51:
527                 ice->akm_codecs = 2;
528                 err = snd_ice1712_akm4xxx_init(ak, &akm_revo51,
529                                                &akm_revo51_priv, ice);
530                 if (err < 0)
531                         return err;
532                 err = snd_ice1712_akm4xxx_init(ak+1, &akm_revo51_adc,
533                                                &akm_revo51_adc_priv, ice);
534                 if (err < 0)
535                         return err;
536                 err = revo51_i2c_init(ice, &ptc_revo51_volume);
537                 if (err < 0)
538                         return err;
539                 /* unmute all codecs */
540                 snd_ice1712_gpio_write_bits(ice, VT1724_REVO_MUTE,
541                                             VT1724_REVO_MUTE);
542                 break;
543         case VT1724_SUBDEVICE_AUDIOPHILE192:
544                 ice->akm_codecs = 1;
545                 err = snd_ice1712_akm4xxx_init(ak, &akm_ap192, &akm_ap192_priv,
546                                                ice);
547                 if (err < 0)
548                         return err;
549                 
550                 break;
551         }
552
553         return 0;
554 }
555
556
557 static int __devinit revo_add_controls(struct snd_ice1712 *ice)
558 {
559         int err;
560
561         switch (ice->eeprom.subvendor) {
562         case VT1724_SUBDEVICE_REVOLUTION71:
563                 err = snd_ice1712_akm4xxx_build_controls(ice);
564                 if (err < 0)
565                         return err;
566                 break;
567         case VT1724_SUBDEVICE_REVOLUTION51:
568                 err = snd_ice1712_akm4xxx_build_controls(ice);
569                 if (err < 0)
570                         return err;
571                 err = snd_pt2258_build_controls(ice->spec.revo51.pt2258);
572                 if (err < 0)
573                         return err;
574                 break;
575         case VT1724_SUBDEVICE_AUDIOPHILE192:
576                 err = snd_ice1712_akm4xxx_build_controls(ice);
577                 if (err < 0)
578                         return err;
579 #ifdef BUILD_AK4114_AP192
580                 err = ap192_ak4114_init(ice);
581                 if (err < 0)
582                         return err;
583 #endif
584                 break;
585         }
586         return 0;
587 }
588
589 /* entry point */
590 struct snd_ice1712_card_info snd_vt1724_revo_cards[] __devinitdata = {
591         {
592                 .subvendor = VT1724_SUBDEVICE_REVOLUTION71,
593                 .name = "M Audio Revolution-7.1",
594                 .model = "revo71",
595                 .chip_init = revo_init,
596                 .build_controls = revo_add_controls,
597         },
598         {
599                 .subvendor = VT1724_SUBDEVICE_REVOLUTION51,
600                 .name = "M Audio Revolution-5.1",
601                 .model = "revo51",
602                 .chip_init = revo_init,
603                 .build_controls = revo_add_controls,
604         },
605         {
606                 .subvendor = VT1724_SUBDEVICE_AUDIOPHILE192,
607                 .name = "M Audio Audiophile192",
608                 .model = "ap192",
609                 .chip_init = revo_init,
610                 .build_controls = revo_add_controls,
611         },
612         { } /* terminator */
613 };