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