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