2 * PMac Tumbler/Snapper lowlevel functions
4 * Copyright (c) by Takashi Iwai <tiwai@suse.de>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <sound/driver.h>
23 #include <linux/init.h>
24 #include <linux/delay.h>
25 #include <linux/i2c.h>
26 #include <linux/i2c-dev.h>
27 #include <linux/kmod.h>
28 #include <linux/slab.h>
29 #include <linux/interrupt.h>
30 #include <sound/core.h>
33 #ifdef CONFIG_PPC_HAS_FEATURE_CALLS
34 #include <asm/pmac_feature.h>
37 #include "tumbler_volume.h"
41 /* i2c address for tumbler */
42 #define TAS_I2C_ADDR 0x34
45 #define TAS_REG_MCS 0x01 /* main control */
46 #define TAS_REG_DRC 0x02
47 #define TAS_REG_VOL 0x04
48 #define TAS_REG_TREBLE 0x05
49 #define TAS_REG_BASS 0x06
50 #define TAS_REG_INPUT1 0x07
51 #define TAS_REG_INPUT2 0x08
54 #define TAS_REG_PCM TAS_REG_INPUT1
57 #define TAS_REG_LMIX TAS_REG_INPUT1
58 #define TAS_REG_RMIX TAS_REG_INPUT2
59 #define TAS_REG_MCS2 0x43 /* main control 2 */
60 #define TAS_REG_ACS 0x40 /* analog control */
62 /* mono volumes for tas3001c/tas3004 */
64 VOL_IDX_PCM_MONO, /* tas3001c only */
65 VOL_IDX_BASS, VOL_IDX_TREBLE,
69 /* stereo volumes for tas3004 */
71 VOL_IDX_PCM, VOL_IDX_PCM2, VOL_IDX_ADC,
75 typedef struct pmac_gpio {
76 #ifdef CONFIG_PPC_HAS_FEATURE_CALLS
84 typedef struct pmac_tumbler_t {
86 pmac_gpio_t audio_reset;
89 pmac_gpio_t hp_detect;
91 unsigned int master_vol[2];
92 unsigned int master_switch[2];
93 unsigned int mono_vol[VOL_IDX_LAST_MONO];
94 unsigned int mix_vol[VOL_IDX_LAST_MIX][2]; /* stereo volumes for tas3004 */
104 static int send_init_client(pmac_keywest_t *i2c, unsigned int *regs)
109 err = snd_pmac_keywest_write_byte(i2c, regs[0], regs[1]);
122 static int tumbler_init_client(pmac_keywest_t *i2c)
124 static unsigned int regs[] = {
125 /* normal operation, SCLK=64fps, i2s output, i2s input, 16bit width */
126 TAS_REG_MCS, (1<<6)|(2<<4)|(2<<2)|0,
129 return send_init_client(i2c, regs);
132 static int snapper_init_client(pmac_keywest_t *i2c)
134 static unsigned int regs[] = {
135 /* normal operation, SCLK=64fps, i2s output, 16bit width */
136 TAS_REG_MCS, (1<<6)|(2<<4)|0,
137 /* normal operation, all-pass mode */
138 TAS_REG_MCS2, (1<<1),
139 /* normal output, no deemphasis, A input, power-up, line-in */
143 return send_init_client(i2c, regs);
149 #ifdef CONFIG_PPC_HAS_FEATURE_CALLS
150 #define do_gpio_write(gp, val) \
151 pmac_call_feature(PMAC_FTR_WRITE_GPIO, NULL, (gp)->addr, val)
152 #define do_gpio_read(gp) \
153 pmac_call_feature(PMAC_FTR_READ_GPIO, NULL, (gp)->addr, 0)
154 #define tumbler_gpio_free(gp) /* NOP */
156 #define do_gpio_write(gp, val) writeb(val, (gp)->addr)
157 #define do_gpio_read(gp) readb((gp)->addr)
158 static inline void tumbler_gpio_free(pmac_gpio_t *gp)
165 #endif /* CONFIG_PPC_HAS_FEATURE_CALLS */
167 static void write_audio_gpio(pmac_gpio_t *gp, int active)
171 active = active ? gp->active_state : !gp->active_state;
172 do_gpio_write(gp, active ? 0x05 : 0x04);
175 static int read_audio_gpio(pmac_gpio_t *gp)
180 ret = ((do_gpio_read(gp) & 0x02) !=0);
181 return ret == gp->active_state;
185 * update master volume
187 static int tumbler_set_master_volume(pmac_tumbler_t *mix)
189 unsigned char block[6];
190 unsigned int left_vol, right_vol;
192 if (! mix->i2c.client)
195 if (! mix->master_switch[0])
198 left_vol = mix->master_vol[0];
199 if (left_vol >= ARRAY_SIZE(master_volume_table))
200 left_vol = ARRAY_SIZE(master_volume_table) - 1;
201 left_vol = master_volume_table[left_vol];
203 if (! mix->master_switch[1])
206 right_vol = mix->master_vol[1];
207 if (right_vol >= ARRAY_SIZE(master_volume_table))
208 right_vol = ARRAY_SIZE(master_volume_table) - 1;
209 right_vol = master_volume_table[right_vol];
212 block[0] = (left_vol >> 16) & 0xff;
213 block[1] = (left_vol >> 8) & 0xff;
214 block[2] = (left_vol >> 0) & 0xff;
216 block[3] = (right_vol >> 16) & 0xff;
217 block[4] = (right_vol >> 8) & 0xff;
218 block[5] = (right_vol >> 0) & 0xff;
220 if (snd_pmac_keywest_write(&mix->i2c, TAS_REG_VOL, 6, block) < 0) {
221 snd_printk("failed to set volume \n");
229 static int tumbler_info_master_volume(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
231 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
233 uinfo->value.integer.min = 0;
234 uinfo->value.integer.max = ARRAY_SIZE(master_volume_table) - 1;
238 static int tumbler_get_master_volume(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
240 pmac_t *chip = snd_kcontrol_chip(kcontrol);
241 pmac_tumbler_t *mix = chip->mixer_data;
242 snd_assert(mix, return -ENODEV);
243 ucontrol->value.integer.value[0] = mix->master_vol[0];
244 ucontrol->value.integer.value[1] = mix->master_vol[1];
248 static int tumbler_put_master_volume(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
250 pmac_t *chip = snd_kcontrol_chip(kcontrol);
251 pmac_tumbler_t *mix = chip->mixer_data;
254 snd_assert(mix, return -ENODEV);
255 change = mix->master_vol[0] != ucontrol->value.integer.value[0] ||
256 mix->master_vol[1] != ucontrol->value.integer.value[1];
258 mix->master_vol[0] = ucontrol->value.integer.value[0];
259 mix->master_vol[1] = ucontrol->value.integer.value[1];
260 tumbler_set_master_volume(mix);
266 static int tumbler_get_master_switch(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
268 pmac_t *chip = snd_kcontrol_chip(kcontrol);
269 pmac_tumbler_t *mix = chip->mixer_data;
270 snd_assert(mix, return -ENODEV);
271 ucontrol->value.integer.value[0] = mix->master_switch[0];
272 ucontrol->value.integer.value[1] = mix->master_switch[1];
276 static int tumbler_put_master_switch(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
278 pmac_t *chip = snd_kcontrol_chip(kcontrol);
279 pmac_tumbler_t *mix = chip->mixer_data;
282 snd_assert(mix, return -ENODEV);
283 change = mix->master_switch[0] != ucontrol->value.integer.value[0] ||
284 mix->master_switch[1] != ucontrol->value.integer.value[1];
286 mix->master_switch[0] = !!ucontrol->value.integer.value[0];
287 mix->master_switch[1] = !!ucontrol->value.integer.value[1];
288 tumbler_set_master_volume(mix);
295 * TAS3001c dynamic range compression
298 #define TAS3001_DRC_MAX 0x5f
300 static int tumbler_set_drc(pmac_tumbler_t *mix)
302 unsigned char val[2];
304 if (! mix->i2c.client)
307 if (mix->drc_enable) {
308 val[0] = 0xc1; /* enable, 3:1 compression */
309 if (mix->drc_range > TAS3001_DRC_MAX)
311 else if (mix->drc_range < 0)
314 val[1] = mix->drc_range + 0x91;
320 if (snd_pmac_keywest_write(&mix->i2c, TAS_REG_DRC, 2, val) < 0) {
321 snd_printk("failed to set DRC\n");
331 #define TAS3004_DRC_MAX 0xef
333 static int snapper_set_drc(pmac_tumbler_t *mix)
335 unsigned char val[6];
337 if (! mix->i2c.client)
341 val[0] = 0x50; /* 3:1 above threshold */
343 val[0] = 0x51; /* disabled */
344 val[1] = 0x02; /* 1:1 below threshold */
345 if (mix->drc_range > 0xef)
347 else if (mix->drc_range < 0)
350 val[2] = mix->drc_range;
355 if (snd_pmac_keywest_write(&mix->i2c, TAS_REG_DRC, 6, val) < 0) {
356 snd_printk("failed to set DRC\n");
362 static int tumbler_info_drc_value(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
364 pmac_t *chip = snd_kcontrol_chip(kcontrol);
365 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
367 uinfo->value.integer.min = 0;
368 uinfo->value.integer.max =
369 chip->model == PMAC_TUMBLER ? TAS3001_DRC_MAX : TAS3004_DRC_MAX;
373 static int tumbler_get_drc_value(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
375 pmac_t *chip = snd_kcontrol_chip(kcontrol);
377 if (! (mix = chip->mixer_data))
379 ucontrol->value.integer.value[0] = mix->drc_range;
383 static int tumbler_put_drc_value(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
385 pmac_t *chip = snd_kcontrol_chip(kcontrol);
389 if (! (mix = chip->mixer_data))
391 change = mix->drc_range != ucontrol->value.integer.value[0];
393 mix->drc_range = ucontrol->value.integer.value[0];
394 if (chip->model == PMAC_TUMBLER)
395 tumbler_set_drc(mix);
397 snapper_set_drc(mix);
402 static int tumbler_get_drc_switch(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
404 pmac_t *chip = snd_kcontrol_chip(kcontrol);
406 if (! (mix = chip->mixer_data))
408 ucontrol->value.integer.value[0] = mix->drc_enable;
412 static int tumbler_put_drc_switch(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
414 pmac_t *chip = snd_kcontrol_chip(kcontrol);
418 if (! (mix = chip->mixer_data))
420 change = mix->drc_enable != ucontrol->value.integer.value[0];
422 mix->drc_enable = !!ucontrol->value.integer.value[0];
423 if (chip->model == PMAC_TUMBLER)
424 tumbler_set_drc(mix);
426 snapper_set_drc(mix);
436 struct tumbler_mono_vol {
444 static int tumbler_set_mono_volume(pmac_tumbler_t *mix, struct tumbler_mono_vol *info)
446 unsigned char block[4];
450 if (! mix->i2c.client)
453 vol = mix->mono_vol[info->index];
454 if (vol >= info->max)
456 vol = info->table[vol];
457 for (i = 0; i < info->bytes; i++)
458 block[i] = (vol >> ((info->bytes - i - 1) * 8)) & 0xff;
459 if (snd_pmac_keywest_write(&mix->i2c, info->reg, info->bytes, block) < 0) {
460 snd_printk("failed to set mono volume %d\n", info->index);
466 static int tumbler_info_mono(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
468 struct tumbler_mono_vol *info = (struct tumbler_mono_vol *)kcontrol->private_value;
470 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
472 uinfo->value.integer.min = 0;
473 uinfo->value.integer.max = info->max - 1;
477 static int tumbler_get_mono(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
479 struct tumbler_mono_vol *info = (struct tumbler_mono_vol *)kcontrol->private_value;
480 pmac_t *chip = snd_kcontrol_chip(kcontrol);
482 if (! (mix = chip->mixer_data))
484 ucontrol->value.integer.value[0] = mix->mono_vol[info->index];
488 static int tumbler_put_mono(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
490 struct tumbler_mono_vol *info = (struct tumbler_mono_vol *)kcontrol->private_value;
491 pmac_t *chip = snd_kcontrol_chip(kcontrol);
495 if (! (mix = chip->mixer_data))
497 change = mix->mono_vol[info->index] != ucontrol->value.integer.value[0];
499 mix->mono_vol[info->index] = ucontrol->value.integer.value[0];
500 tumbler_set_mono_volume(mix, info);
505 /* TAS3001c mono volumes */
506 static struct tumbler_mono_vol tumbler_pcm_vol_info = {
507 .index = VOL_IDX_PCM_MONO,
510 .max = ARRAY_SIZE(mixer_volume_table),
511 .table = mixer_volume_table,
514 static struct tumbler_mono_vol tumbler_bass_vol_info = {
515 .index = VOL_IDX_BASS,
518 .max = ARRAY_SIZE(bass_volume_table),
519 .table = bass_volume_table,
522 static struct tumbler_mono_vol tumbler_treble_vol_info = {
523 .index = VOL_IDX_TREBLE,
524 .reg = TAS_REG_TREBLE,
526 .max = ARRAY_SIZE(treble_volume_table),
527 .table = treble_volume_table,
530 /* TAS3004 mono volumes */
531 static struct tumbler_mono_vol snapper_bass_vol_info = {
532 .index = VOL_IDX_BASS,
535 .max = ARRAY_SIZE(snapper_bass_volume_table),
536 .table = snapper_bass_volume_table,
539 static struct tumbler_mono_vol snapper_treble_vol_info = {
540 .index = VOL_IDX_TREBLE,
541 .reg = TAS_REG_TREBLE,
543 .max = ARRAY_SIZE(snapper_treble_volume_table),
544 .table = snapper_treble_volume_table,
548 #define DEFINE_MONO(xname,type) { \
549 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,\
551 .info = tumbler_info_mono, \
552 .get = tumbler_get_mono, \
553 .put = tumbler_put_mono, \
554 .private_value = (unsigned long)(&tumbler_##type##_vol_info), \
557 #define DEFINE_SNAPPER_MONO(xname,type) { \
558 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,\
560 .info = tumbler_info_mono, \
561 .get = tumbler_get_mono, \
562 .put = tumbler_put_mono, \
563 .private_value = (unsigned long)(&snapper_##type##_vol_info), \
568 * snapper mixer volumes
571 static int snapper_set_mix_vol1(pmac_tumbler_t *mix, int idx, int ch, int reg)
574 unsigned char block[9];
576 vol = mix->mix_vol[idx][ch];
577 if (vol >= ARRAY_SIZE(mixer_volume_table)) {
578 vol = ARRAY_SIZE(mixer_volume_table) - 1;
579 mix->mix_vol[idx][ch] = vol;
582 for (i = 0; i < 3; i++) {
583 vol = mix->mix_vol[i][ch];
584 vol = mixer_volume_table[vol];
585 for (j = 0; j < 3; j++)
586 block[i * 3 + j] = (vol >> ((2 - j) * 8)) & 0xff;
588 if (snd_pmac_keywest_write(&mix->i2c, reg, 9, block) < 0) {
589 snd_printk("failed to set mono volume %d\n", reg);
595 static int snapper_set_mix_vol(pmac_tumbler_t *mix, int idx)
597 if (! mix->i2c.client)
599 if (snapper_set_mix_vol1(mix, idx, 0, TAS_REG_LMIX) < 0 ||
600 snapper_set_mix_vol1(mix, idx, 1, TAS_REG_RMIX) < 0)
605 static int snapper_info_mix(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
607 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
609 uinfo->value.integer.min = 0;
610 uinfo->value.integer.max = ARRAY_SIZE(mixer_volume_table) - 1;
614 static int snapper_get_mix(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
616 int idx = (int)kcontrol->private_value;
617 pmac_t *chip = snd_kcontrol_chip(kcontrol);
619 if (! (mix = chip->mixer_data))
621 ucontrol->value.integer.value[0] = mix->mix_vol[idx][0];
622 ucontrol->value.integer.value[1] = mix->mix_vol[idx][1];
626 static int snapper_put_mix(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
628 int idx = (int)kcontrol->private_value;
629 pmac_t *chip = snd_kcontrol_chip(kcontrol);
633 if (! (mix = chip->mixer_data))
635 change = mix->mix_vol[idx][0] != ucontrol->value.integer.value[0] ||
636 mix->mix_vol[idx][1] != ucontrol->value.integer.value[1];
638 mix->mix_vol[idx][0] = ucontrol->value.integer.value[0];
639 mix->mix_vol[idx][1] = ucontrol->value.integer.value[1];
640 snapper_set_mix_vol(mix, idx);
650 enum { TUMBLER_MUTE_HP, TUMBLER_MUTE_AMP };
652 static int tumbler_get_mute_switch(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
654 pmac_t *chip = snd_kcontrol_chip(kcontrol);
657 if (! (mix = chip->mixer_data))
659 gp = (kcontrol->private_value == TUMBLER_MUTE_HP) ? &mix->hp_mute : &mix->amp_mute;
660 ucontrol->value.integer.value[0] = ! read_audio_gpio(gp);
664 static int tumbler_put_mute_switch(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
666 pmac_t *chip = snd_kcontrol_chip(kcontrol);
670 #ifdef PMAC_SUPPORT_AUTOMUTE
671 if (chip->update_automute && chip->auto_mute)
672 return 0; /* don't touch in the auto-mute mode */
674 if (! (mix = chip->mixer_data))
676 gp = (kcontrol->private_value == TUMBLER_MUTE_HP) ? &mix->hp_mute : &mix->amp_mute;
677 val = ! read_audio_gpio(gp);
678 if (val != ucontrol->value.integer.value[0]) {
679 write_audio_gpio(gp, ! ucontrol->value.integer.value[0]);
685 static int snapper_set_capture_source(pmac_tumbler_t *mix)
687 return snd_pmac_keywest_write_byte(&mix->i2c, TAS_REG_ACS,
688 mix->capture_source ? 2 : 0);
691 static int snapper_info_capture_source(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
693 static char *texts[2] = {
696 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
698 uinfo->value.enumerated.items = 2;
699 if (uinfo->value.enumerated.item > 1)
700 uinfo->value.enumerated.item = 1;
701 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
705 static int snapper_get_capture_source(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
707 pmac_t *chip = snd_kcontrol_chip(kcontrol);
708 pmac_tumbler_t *mix = chip->mixer_data;
710 snd_assert(mix, return -ENODEV);
711 ucontrol->value.integer.value[0] = mix->capture_source;
715 static int snapper_put_capture_source(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
717 pmac_t *chip = snd_kcontrol_chip(kcontrol);
718 pmac_tumbler_t *mix = chip->mixer_data;
721 snd_assert(mix, return -ENODEV);
722 change = ucontrol->value.integer.value[0] != mix->capture_source;
724 mix->capture_source = !!ucontrol->value.integer.value[0];
725 snapper_set_capture_source(mix);
730 #define DEFINE_SNAPPER_MIX(xname,idx,ofs) { \
731 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,\
733 .info = snapper_info_mix, \
734 .get = snapper_get_mix, \
735 .put = snapper_put_mix, \
737 .private_value = ofs, \
743 static snd_kcontrol_new_t tumbler_mixers[] __initdata = {
744 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
745 .name = "Master Playback Volume",
746 .info = tumbler_info_master_volume,
747 .get = tumbler_get_master_volume,
748 .put = tumbler_put_master_volume
750 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
751 .name = "Master Playback Switch",
752 .info = snd_pmac_boolean_stereo_info,
753 .get = tumbler_get_master_switch,
754 .put = tumbler_put_master_switch
756 DEFINE_MONO("Tone Control - Bass", bass),
757 DEFINE_MONO("Tone Control - Treble", treble),
758 DEFINE_MONO("PCM Playback Volume", pcm),
759 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
760 .name = "DRC Switch",
761 .info = snd_pmac_boolean_mono_info,
762 .get = tumbler_get_drc_switch,
763 .put = tumbler_put_drc_switch
765 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
767 .info = tumbler_info_drc_value,
768 .get = tumbler_get_drc_value,
769 .put = tumbler_put_drc_value
773 static snd_kcontrol_new_t snapper_mixers[] __initdata = {
774 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
775 .name = "Master Playback Volume",
776 .info = tumbler_info_master_volume,
777 .get = tumbler_get_master_volume,
778 .put = tumbler_put_master_volume
780 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
781 .name = "Master Playback Switch",
782 .info = snd_pmac_boolean_stereo_info,
783 .get = tumbler_get_master_switch,
784 .put = tumbler_put_master_switch
786 DEFINE_SNAPPER_MIX("PCM Playback Volume", 0, VOL_IDX_PCM),
787 DEFINE_SNAPPER_MIX("PCM Playback Volume", 1, VOL_IDX_PCM2),
788 DEFINE_SNAPPER_MIX("Monitor Mix Volume", 0, VOL_IDX_ADC),
789 DEFINE_SNAPPER_MONO("Tone Control - Bass", bass),
790 DEFINE_SNAPPER_MONO("Tone Control - Treble", treble),
791 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
792 .name = "DRC Switch",
793 .info = snd_pmac_boolean_mono_info,
794 .get = tumbler_get_drc_switch,
795 .put = tumbler_put_drc_switch
797 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
799 .info = tumbler_info_drc_value,
800 .get = tumbler_get_drc_value,
801 .put = tumbler_put_drc_value
803 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
804 .name = "Input Source", /* FIXME: "Capture Source" doesn't work properly */
805 .info = snapper_info_capture_source,
806 .get = snapper_get_capture_source,
807 .put = snapper_put_capture_source
811 static snd_kcontrol_new_t tumbler_hp_sw __initdata = {
812 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
813 .name = "Headphone Playback Switch",
814 .info = snd_pmac_boolean_mono_info,
815 .get = tumbler_get_mute_switch,
816 .put = tumbler_put_mute_switch,
817 .private_value = TUMBLER_MUTE_HP,
819 static snd_kcontrol_new_t tumbler_speaker_sw __initdata = {
820 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
821 .name = "PC Speaker Playback Switch",
822 .info = snd_pmac_boolean_mono_info,
823 .get = tumbler_get_mute_switch,
824 .put = tumbler_put_mute_switch,
825 .private_value = TUMBLER_MUTE_AMP,
828 #ifdef PMAC_SUPPORT_AUTOMUTE
832 static int tumbler_detect_headphone(pmac_t *chip)
834 pmac_tumbler_t *mix = chip->mixer_data;
835 return read_audio_gpio(&mix->hp_detect);
838 static void check_mute(pmac_t *chip, pmac_gpio_t *gp, int val, int do_notify, snd_kcontrol_t *sw)
840 //pmac_tumbler_t *mix = chip->mixer_data;
841 if (val != read_audio_gpio(gp)) {
842 write_audio_gpio(gp, val);
844 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE, &sw->id);
848 static void tumbler_update_automute(pmac_t *chip, int do_notify)
850 if (chip->auto_mute) {
851 pmac_tumbler_t *mix = chip->mixer_data;
852 snd_assert(mix, return);
853 if (tumbler_detect_headphone(chip)) {
855 check_mute(chip, &mix->amp_mute, 1, do_notify, chip->speaker_sw_ctl);
856 check_mute(chip, &mix->hp_mute, 0, do_notify, chip->master_sw_ctl);
859 check_mute(chip, &mix->amp_mute, 0, do_notify, chip->speaker_sw_ctl);
860 check_mute(chip, &mix->hp_mute, 1, do_notify, chip->master_sw_ctl);
863 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
864 &chip->hp_detect_ctl->id);
867 #endif /* PMAC_SUPPORT_AUTOMUTE */
870 /* interrupt - headphone plug changed */
871 static irqreturn_t headphone_intr(int irq, void *devid, struct pt_regs *regs)
873 pmac_t *chip = snd_magic_cast(pmac_t, devid, return);
874 if (chip->update_automute && chip->initialized) {
875 chip->update_automute(chip, 1);
881 /* look for audio-gpio device */
882 static struct device_node *find_audio_device(const char *name)
884 struct device_node *np;
886 if (! (np = find_devices("gpio")))
889 for (np = np->child; np; np = np->sibling) {
890 char *property = get_property(np, "audio-gpio", NULL);
891 if (property && strcmp(property, name) == 0)
897 /* look for audio-gpio device */
898 static struct device_node *find_compatible_audio_device(const char *name)
900 struct device_node *np;
902 if (! (np = find_devices("gpio")))
905 for (np = np->child; np; np = np->sibling) {
906 if (device_is_compatible(np, name))
912 /* find an audio device and get its address */
913 static unsigned long tumbler_find_device(const char *device, pmac_gpio_t *gp, int is_compatible)
915 struct device_node *node;
919 node = find_compatible_audio_device(device);
921 node = find_audio_device(device);
923 snd_printdd("cannot find device %s\n", device);
927 base = (u32 *)get_property(node, "AAPL,address", NULL);
929 snd_printd("cannot find address for device %s\n", device);
933 #ifdef CONFIG_PPC_HAS_FEATURE_CALLS
934 gp->addr = (*base) & 0x0000ffff;
936 gp->addr = (void*)ioremap((unsigned long)(*base), 1);
938 base = (u32 *)get_property(node, "audio-gpio-active-state", NULL);
940 gp->active_state = *base;
942 gp->active_state = 1;
945 return (node->n_intrs > 0) ? node->intrs[0].line : 0;
949 static void tumbler_reset_audio(pmac_t *chip)
951 pmac_tumbler_t *mix = chip->mixer_data;
953 write_audio_gpio(&mix->audio_reset, 0);
955 write_audio_gpio(&mix->audio_reset, 1);
957 write_audio_gpio(&mix->audio_reset, 0);
961 #ifdef CONFIG_PMAC_PBOOK
963 static void tumbler_resume(pmac_t *chip)
965 pmac_tumbler_t *mix = chip->mixer_data;
967 snd_assert(mix, return);
969 tumbler_reset_audio(chip);
970 if (mix->i2c.client && mix->i2c.init_client) {
971 if (mix->i2c.init_client(&mix->i2c) < 0)
972 printk(KERN_ERR "tumbler_init_client error\n");
974 printk(KERN_ERR "tumbler: i2c is not initialized\n");
975 if (chip->model == PMAC_TUMBLER) {
976 tumbler_set_mono_volume(mix, &tumbler_pcm_vol_info);
977 tumbler_set_mono_volume(mix, &tumbler_bass_vol_info);
978 tumbler_set_mono_volume(mix, &tumbler_treble_vol_info);
979 tumbler_set_drc(mix);
981 snapper_set_mix_vol(mix, VOL_IDX_PCM);
982 snapper_set_mix_vol(mix, VOL_IDX_PCM2);
983 snapper_set_mix_vol(mix, VOL_IDX_ADC);
984 tumbler_set_mono_volume(mix, &snapper_bass_vol_info);
985 tumbler_set_mono_volume(mix, &snapper_treble_vol_info);
986 snapper_set_drc(mix);
987 snapper_set_capture_source(mix);
989 tumbler_set_master_volume(mix);
990 if (chip->update_automute)
991 chip->update_automute(chip, 0);
995 /* initialize tumbler */
996 static int __init tumbler_init(pmac_t *chip)
999 pmac_tumbler_t *mix = chip->mixer_data;
1000 snd_assert(mix, return -EINVAL);
1002 tumbler_find_device("audio-hw-reset", &mix->audio_reset, 0);
1003 tumbler_find_device("amp-mute", &mix->amp_mute, 0);
1004 tumbler_find_device("headphone-mute", &mix->hp_mute, 0);
1005 irq = tumbler_find_device("headphone-detect", &mix->hp_detect, 0);
1007 irq = tumbler_find_device("keywest-gpio15", &mix->hp_detect, 1);
1009 tumbler_reset_audio(chip);
1011 /* activate headphone status interrupts */
1014 if ((err = request_irq(irq, headphone_intr, 0,
1015 "Tumbler Headphone Detection", chip)) < 0)
1017 /* activate headphone status interrupts */
1018 val = do_gpio_read(&mix->hp_detect);
1019 do_gpio_write(&mix->hp_detect, val | 0x80);
1021 mix->headphone_irq = irq;
1026 static void tumbler_cleanup(pmac_t *chip)
1028 pmac_tumbler_t *mix = chip->mixer_data;
1032 if (mix->headphone_irq >= 0)
1033 free_irq(mix->headphone_irq, chip);
1034 tumbler_gpio_free(&mix->audio_reset);
1035 tumbler_gpio_free(&mix->amp_mute);
1036 tumbler_gpio_free(&mix->hp_mute);
1037 tumbler_gpio_free(&mix->hp_detect);
1038 snd_pmac_keywest_cleanup(&mix->i2c);
1040 chip->mixer_data = NULL;
1044 int __init snd_pmac_tumbler_init(pmac_t *chip)
1047 pmac_tumbler_t *mix;
1049 struct device_node *tas_node;
1053 if (current->fs->root)
1054 request_module("i2c-keywest");
1055 #endif /* CONFIG_KMOD */
1057 mix = kmalloc(sizeof(*mix), GFP_KERNEL);
1060 memset(mix, 0, sizeof(*mix));
1061 mix->headphone_irq = -1;
1063 chip->mixer_data = mix;
1064 chip->mixer_free = tumbler_cleanup;
1066 if ((err = tumbler_init(chip)) < 0)
1070 tas_node = find_devices("deq");
1071 if (tas_node == NULL)
1074 paddr = (u32 *)get_property(tas_node, "i2c-address", NULL);
1076 mix->i2c.addr = (*paddr) >> 1;
1078 mix->i2c.addr = TAS_I2C_ADDR;
1080 if (chip->model == PMAC_TUMBLER) {
1081 mix->i2c.init_client = tumbler_init_client;
1082 mix->i2c.name = "TAS3001c";
1083 chipname = "Tumbler";
1085 mix->i2c.init_client = snapper_init_client;
1086 mix->i2c.name = "TAS3004";
1087 chipname = "Snapper";
1090 if ((err = snd_pmac_keywest_init(&mix->i2c)) < 0)
1096 sprintf(chip->card->mixername, "PowerMac %s", chipname);
1098 if (chip->model == PMAC_TUMBLER) {
1099 for (i = 0; i < ARRAY_SIZE(tumbler_mixers); i++) {
1100 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&tumbler_mixers[i], chip))) < 0)
1104 for (i = 0; i < ARRAY_SIZE(snapper_mixers); i++) {
1105 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snapper_mixers[i], chip))) < 0)
1109 chip->master_sw_ctl = snd_ctl_new1(&tumbler_hp_sw, chip);
1110 if ((err = snd_ctl_add(chip->card, chip->master_sw_ctl)) < 0)
1112 chip->speaker_sw_ctl = snd_ctl_new1(&tumbler_speaker_sw, chip);
1113 if ((err = snd_ctl_add(chip->card, chip->speaker_sw_ctl)) < 0)
1116 #ifdef CONFIG_PMAC_PBOOK
1117 chip->resume = tumbler_resume;
1120 #ifdef PMAC_SUPPORT_AUTOMUTE
1121 if (mix->headphone_irq >=0 && (err = snd_pmac_add_automute(chip)) < 0)
1123 chip->detect_headphone = tumbler_detect_headphone;
1124 chip->update_automute = tumbler_update_automute;
1125 tumbler_update_automute(chip, 0); /* update the status only */