distcleaned, added images/uImage
[wrt350n-kernel.git] / sound / pci / ice1712 / revo.c
blobddd5fc8d4fe12e9bbd3e61db39e95b8e07381826
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 } revo51;
41 static void revo_i2s_mclk_changed(struct snd_ice1712 *ice)
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));
51 * change the rate of envy24HT, AK4355 and AK4381
53 static void revo_set_rate_val(struct snd_akm4xxx *ak, unsigned int rate)
55 unsigned char old, tmp, dfs;
56 int reg, shift;
58 if (rate == 0) /* no hint - S/PDIF input is master, simply return */
59 return;
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;
69 if (ak->type == SND_AK4355 || ak->type == SND_AK4358) {
70 reg = 2;
71 shift = 4;
72 } else {
73 reg = 1;
74 shift = 3;
76 tmp = snd_akm4xxx_get(ak, 0, reg);
77 old = (tmp >> shift) & 0x03;
78 if (old == dfs)
79 return;
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);
92 * I2C access to the PT2258 volume controller on GPIO 6/7 (Revolution 5.1)
95 static void revo_i2c_start(struct snd_i2c_bus *bus)
97 struct snd_ice1712 *ice = bus->private_data;
98 snd_ice1712_save_gpio_status(ice);
101 static void revo_i2c_stop(struct snd_i2c_bus *bus)
103 struct snd_ice1712 *ice = bus->private_data;
104 snd_ice1712_restore_gpio_status(ice);
107 static void revo_i2c_direction(struct snd_i2c_bus *bus, int clock, int data)
109 struct snd_ice1712 *ice = bus->private_data;
110 unsigned int mask, val;
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);
124 static void revo_i2c_setlines(struct snd_i2c_bus *bus, int clk, int data)
126 struct snd_ice1712 *ice = bus->private_data;
127 unsigned int val = 0;
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);
139 static int revo_i2c_getdata(struct snd_i2c_bus *bus, int ack)
141 struct snd_ice1712 *ice = bus->private_data;
142 int bit;
144 if (ack)
145 udelay(5);
146 bit = snd_ice1712_gpio_read_bits(ice, VT1724_REVO_I2C_DATA) ? 1 : 0;
147 return bit;
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,
158 static int revo51_i2c_init(struct snd_ice1712 *ice,
159 struct snd_pt2258 *pt)
161 struct revo51_spec *spec;
162 int err;
164 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
165 if (!spec)
166 return -ENOMEM;
167 ice->spec = spec;
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;
174 ice->i2c->private_data = ice;
175 ice->i2c->hw_ops.bit = &revo51_bit_ops;
177 /* create the I2C device */
178 err = snd_i2c_device_create(ice->i2c, "PT2258", 0x40, &spec->dev);
179 if (err < 0)
180 return err;
182 pt->card = ice->card;
183 pt->i2c_bus = ice->i2c;
184 pt->i2c_dev = spec->dev;
185 spec->pt2258 = pt;
187 snd_pt2258_reset(pt);
189 return 0;
193 * initialize the chips on M-Audio Revolution cards
196 #define AK_DAC(xname,xch) { .name = xname, .num_channels = xch }
198 static const struct snd_akm4xxx_dac_channel revo71_front[] = {
200 .name = "PCM Playback Volume",
201 .num_channels = 2,
202 /* front channels DAC supports muting */
203 .switch_name = "PCM Playback Switch",
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),
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),
221 static const char *revo51_adc_input_names[] = {
222 "Mic",
223 "Line",
224 "CD",
225 NULL
228 static const struct snd_akm4xxx_adc_channel revo51_adc[] = {
230 .name = "PCM Capture Volume",
231 .switch_name = "PCM Capture Switch",
232 .num_channels = 2,
233 .input_names = revo51_adc_input_names
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
243 .dac_info = revo71_front,
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,
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
265 .dac_info = revo71_surround,
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,
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
286 .dac_info = revo51_dac,
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,
301 static struct snd_akm4xxx akm_revo51_adc __devinitdata = {
302 .type = SND_AK5365,
303 .num_adcs = 2,
304 .adc_info = revo51_adc,
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,
319 static struct snd_pt2258 ptc_revo51_volume;
321 /* AK4358 for AP192 DAC, AK5385A for ADC */
322 static void ap192_set_rate_val(struct snd_akm4xxx *ak, unsigned int rate)
324 struct snd_ice1712 *ice = ak->private_data[0];
326 revo_set_rate_val(ak, rate);
328 #if 1 /* FIXME: do we need this procedure? */
329 /* reset DFS pin of AK5385A for ADC, too */
330 /* DFS0 (pin 18) -- GPIO10 pin 77 */
331 snd_ice1712_save_gpio_status(ice);
332 snd_ice1712_gpio_write_bits(ice, 1 << 10,
333 rate > 48000 ? (1 << 10) : 0);
334 snd_ice1712_restore_gpio_status(ice);
335 #endif
338 static const struct snd_akm4xxx_dac_channel ap192_dac[] = {
339 AK_DAC("PCM Playback Volume", 2)
342 static struct snd_akm4xxx akm_ap192 __devinitdata = {
343 .type = SND_AK4358,
344 .num_dacs = 2,
345 .ops = {
346 .set_rate_val = ap192_set_rate_val
348 .dac_info = ap192_dac,
351 static struct snd_ak4xxx_private akm_ap192_priv __devinitdata = {
352 .caddr = 2,
353 .cif = 0,
354 .data_mask = VT1724_REVO_CDOUT,
355 .clk_mask = VT1724_REVO_CCLK,
356 .cs_mask = VT1724_REVO_CS0 | VT1724_REVO_CS3,
357 .cs_addr = VT1724_REVO_CS3,
358 .cs_none = VT1724_REVO_CS0 | VT1724_REVO_CS3,
359 .add_flags = VT1724_REVO_CCLK, /* high at init */
360 .mask_flags = 0,
363 #if 0
364 /* FIXME: ak4114 makes the sound much lower due to some confliction,
365 * so let's disable it right now...
367 #define BUILD_AK4114_AP192
368 #endif
370 #ifdef BUILD_AK4114_AP192
371 /* AK4114 support on Audiophile 192 */
372 /* CDTO (pin 32) -- GPIO2 pin 52
373 * CDTI (pin 33) -- GPIO3 pin 53 (shared with AK4358)
374 * CCLK (pin 34) -- GPIO1 pin 51 (shared with AK4358)
375 * CSN (pin 35) -- GPIO7 pin 59
377 #define AK4114_ADDR 0x00
379 static void write_data(struct snd_ice1712 *ice, unsigned int gpio,
380 unsigned int data, int idx)
382 for (; idx >= 0; idx--) {
383 /* drop clock */
384 gpio &= ~VT1724_REVO_CCLK;
385 snd_ice1712_gpio_write(ice, gpio);
386 udelay(1);
387 /* set data */
388 if (data & (1 << idx))
389 gpio |= VT1724_REVO_CDOUT;
390 else
391 gpio &= ~VT1724_REVO_CDOUT;
392 snd_ice1712_gpio_write(ice, gpio);
393 udelay(1);
394 /* raise clock */
395 gpio |= VT1724_REVO_CCLK;
396 snd_ice1712_gpio_write(ice, gpio);
397 udelay(1);
401 static unsigned char read_data(struct snd_ice1712 *ice, unsigned int gpio,
402 int idx)
404 unsigned char data = 0;
406 for (; idx >= 0; idx--) {
407 /* drop clock */
408 gpio &= ~VT1724_REVO_CCLK;
409 snd_ice1712_gpio_write(ice, gpio);
410 udelay(1);
411 /* read data */
412 if (snd_ice1712_gpio_read(ice) & VT1724_REVO_CDIN)
413 data |= (1 << idx);
414 udelay(1);
415 /* raise clock */
416 gpio |= VT1724_REVO_CCLK;
417 snd_ice1712_gpio_write(ice, gpio);
418 udelay(1);
420 return data;
423 static unsigned int ap192_4wire_start(struct snd_ice1712 *ice)
425 unsigned int tmp;
427 snd_ice1712_save_gpio_status(ice);
428 tmp = snd_ice1712_gpio_read(ice);
429 tmp |= VT1724_REVO_CCLK; /* high at init */
430 tmp |= VT1724_REVO_CS0;
431 tmp &= ~VT1724_REVO_CS3;
432 snd_ice1712_gpio_write(ice, tmp);
433 udelay(1);
434 return tmp;
437 static void ap192_4wire_finish(struct snd_ice1712 *ice, unsigned int tmp)
439 tmp |= VT1724_REVO_CS3;
440 tmp |= VT1724_REVO_CS0;
441 snd_ice1712_gpio_write(ice, tmp);
442 udelay(1);
443 snd_ice1712_restore_gpio_status(ice);
446 static void ap192_ak4114_write(void *private_data, unsigned char addr,
447 unsigned char data)
449 struct snd_ice1712 *ice = private_data;
450 unsigned int tmp, addrdata;
452 tmp = ap192_4wire_start(ice);
453 addrdata = (AK4114_ADDR << 6) | 0x20 | (addr & 0x1f);
454 addrdata = (addrdata << 8) | data;
455 write_data(ice, tmp, addrdata, 15);
456 ap192_4wire_finish(ice, tmp);
459 static unsigned char ap192_ak4114_read(void *private_data, unsigned char addr)
461 struct snd_ice1712 *ice = private_data;
462 unsigned int tmp;
463 unsigned char data;
465 tmp = ap192_4wire_start(ice);
466 write_data(ice, tmp, (AK4114_ADDR << 6) | (addr & 0x1f), 7);
467 data = read_data(ice, tmp, 7);
468 ap192_4wire_finish(ice, tmp);
469 return data;
472 static int __devinit ap192_ak4114_init(struct snd_ice1712 *ice)
474 static const unsigned char ak4114_init_vals[] = {
475 AK4114_RST | AK4114_PWN | AK4114_OCKS0 | AK4114_OCKS1,
476 AK4114_DIF_I24I2S,
477 AK4114_TX1E,
478 AK4114_EFH_1024 | AK4114_DIT | AK4114_IPS(1),
482 static const unsigned char ak4114_init_txcsb[] = {
483 0x41, 0x02, 0x2c, 0x00, 0x00
485 struct ak4114 *ak;
486 int err;
488 return snd_ak4114_create(ice->card,
489 ap192_ak4114_read,
490 ap192_ak4114_write,
491 ak4114_init_vals, ak4114_init_txcsb,
492 ice, &ak);
494 #endif /* BUILD_AK4114_AP192 */
496 static int __devinit revo_init(struct snd_ice1712 *ice)
498 struct snd_akm4xxx *ak;
499 int err;
501 /* determine I2C, DACs and ADCs */
502 switch (ice->eeprom.subvendor) {
503 case VT1724_SUBDEVICE_REVOLUTION71:
504 ice->num_total_dacs = 8;
505 ice->num_total_adcs = 2;
506 ice->gpio.i2s_mclk_changed = revo_i2s_mclk_changed;
507 break;
508 case VT1724_SUBDEVICE_REVOLUTION51:
509 ice->num_total_dacs = 6;
510 ice->num_total_adcs = 2;
511 break;
512 case VT1724_SUBDEVICE_AUDIOPHILE192:
513 ice->num_total_dacs = 2;
514 ice->num_total_adcs = 2;
515 break;
516 default:
517 snd_BUG();
518 return -EINVAL;
521 /* second stage of initialization, analog parts and others */
522 ak = ice->akm = kcalloc(2, sizeof(struct snd_akm4xxx), GFP_KERNEL);
523 if (! ak)
524 return -ENOMEM;
525 ice->akm_codecs = 2;
526 switch (ice->eeprom.subvendor) {
527 case VT1724_SUBDEVICE_REVOLUTION71:
528 ice->akm_codecs = 2;
529 if ((err = snd_ice1712_akm4xxx_init(ak, &akm_revo_front, &akm_revo_front_priv, ice)) < 0)
530 return err;
531 if ((err = snd_ice1712_akm4xxx_init(ak + 1, &akm_revo_surround, &akm_revo_surround_priv, ice)) < 0)
532 return err;
533 /* unmute all codecs */
534 snd_ice1712_gpio_write_bits(ice, VT1724_REVO_MUTE, VT1724_REVO_MUTE);
535 break;
536 case VT1724_SUBDEVICE_REVOLUTION51:
537 ice->akm_codecs = 2;
538 err = snd_ice1712_akm4xxx_init(ak, &akm_revo51,
539 &akm_revo51_priv, ice);
540 if (err < 0)
541 return err;
542 err = snd_ice1712_akm4xxx_init(ak+1, &akm_revo51_adc,
543 &akm_revo51_adc_priv, ice);
544 if (err < 0)
545 return err;
546 err = revo51_i2c_init(ice, &ptc_revo51_volume);
547 if (err < 0)
548 return err;
549 /* unmute all codecs */
550 snd_ice1712_gpio_write_bits(ice, VT1724_REVO_MUTE,
551 VT1724_REVO_MUTE);
552 break;
553 case VT1724_SUBDEVICE_AUDIOPHILE192:
554 ice->akm_codecs = 1;
555 err = snd_ice1712_akm4xxx_init(ak, &akm_ap192, &akm_ap192_priv,
556 ice);
557 if (err < 0)
558 return err;
560 break;
563 return 0;
567 static int __devinit revo_add_controls(struct snd_ice1712 *ice)
569 struct revo51_spec *spec;
570 int err;
572 switch (ice->eeprom.subvendor) {
573 case VT1724_SUBDEVICE_REVOLUTION71:
574 err = snd_ice1712_akm4xxx_build_controls(ice);
575 if (err < 0)
576 return err;
577 break;
578 case VT1724_SUBDEVICE_REVOLUTION51:
579 err = snd_ice1712_akm4xxx_build_controls(ice);
580 if (err < 0)
581 return err;
582 spec = ice->spec;
583 err = snd_pt2258_build_controls(spec->pt2258);
584 if (err < 0)
585 return err;
586 break;
587 case VT1724_SUBDEVICE_AUDIOPHILE192:
588 err = snd_ice1712_akm4xxx_build_controls(ice);
589 if (err < 0)
590 return err;
591 #ifdef BUILD_AK4114_AP192
592 err = ap192_ak4114_init(ice);
593 if (err < 0)
594 return err;
595 #endif
596 break;
598 return 0;
601 /* entry point */
602 struct snd_ice1712_card_info snd_vt1724_revo_cards[] __devinitdata = {
604 .subvendor = VT1724_SUBDEVICE_REVOLUTION71,
605 .name = "M Audio Revolution-7.1",
606 .model = "revo71",
607 .chip_init = revo_init,
608 .build_controls = revo_add_controls,
611 .subvendor = VT1724_SUBDEVICE_REVOLUTION51,
612 .name = "M Audio Revolution-5.1",
613 .model = "revo51",
614 .chip_init = revo_init,
615 .build_controls = revo_add_controls,
618 .subvendor = VT1724_SUBDEVICE_AUDIOPHILE192,
619 .name = "M Audio Audiophile192",
620 .model = "ap192",
621 .chip_init = revo_init,
622 .build_controls = revo_add_controls,
624 { } /* terminator */