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