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
20 * Rene Rebe <rene.rebe@gmx.net>:
21 * * update from shadow registers on wakeup and headphone plug
22 * * automatically toggle DRC on headphone plug
27 #include <sound/driver.h>
28 #include <linux/init.h>
29 #include <linux/delay.h>
30 #include <linux/i2c.h>
31 #include <linux/i2c-dev.h>
32 #include <linux/kmod.h>
33 #include <linux/slab.h>
34 #include <linux/interrupt.h>
35 #include <sound/core.h>
38 #ifdef CONFIG_PPC_HAS_FEATURE_CALLS
39 #include <asm/pmac_feature.h>
42 #include "tumbler_volume.h"
44 /* i2c address for tumbler */
45 #define TAS_I2C_ADDR 0x34
48 #define TAS_REG_MCS 0x01 /* main control */
49 #define TAS_REG_DRC 0x02
50 #define TAS_REG_VOL 0x04
51 #define TAS_REG_TREBLE 0x05
52 #define TAS_REG_BASS 0x06
53 #define TAS_REG_INPUT1 0x07
54 #define TAS_REG_INPUT2 0x08
57 #define TAS_REG_PCM TAS_REG_INPUT1
60 #define TAS_REG_LMIX TAS_REG_INPUT1
61 #define TAS_REG_RMIX TAS_REG_INPUT2
62 #define TAS_REG_MCS2 0x43 /* main control 2 */
63 #define TAS_REG_ACS 0x40 /* analog control */
65 /* mono volumes for tas3001c/tas3004 */
67 VOL_IDX_PCM_MONO, /* tas3001c only */
68 VOL_IDX_BASS, VOL_IDX_TREBLE,
72 /* stereo volumes for tas3004 */
74 VOL_IDX_PCM, VOL_IDX_PCM2, VOL_IDX_ADC,
78 typedef struct pmac_gpio {
79 #ifdef CONFIG_PPC_HAS_FEATURE_CALLS
87 typedef struct pmac_tumbler_t {
89 pmac_gpio_t audio_reset;
92 pmac_gpio_t hp_detect;
94 unsigned int master_vol[2];
95 unsigned int master_switch[2];
96 unsigned int mono_vol[VOL_IDX_LAST_MONO];
97 unsigned int mix_vol[VOL_IDX_LAST_MIX][2]; /* stereo volumes for tas3004 */
107 static int send_init_client(pmac_keywest_t *i2c, unsigned int *regs)
112 err = snd_pmac_keywest_write_byte(i2c, regs[0], regs[1]);
125 static int tumbler_init_client(pmac_keywest_t *i2c)
127 static unsigned int regs[] = {
128 /* normal operation, SCLK=64fps, i2s output, i2s input, 16bit width */
129 TAS_REG_MCS, (1<<6)|(2<<4)|(2<<2)|0,
132 return send_init_client(i2c, regs);
135 static int snapper_init_client(pmac_keywest_t *i2c)
137 static unsigned int regs[] = {
138 /* normal operation, SCLK=64fps, i2s output, 16bit width */
139 TAS_REG_MCS, (1<<6)|(2<<4)|0,
140 /* normal operation, all-pass mode */
141 TAS_REG_MCS2, (1<<1),
142 /* normal output, no deemphasis, A input, power-up, line-in */
146 return send_init_client(i2c, regs);
152 #ifdef CONFIG_PPC_HAS_FEATURE_CALLS
153 #define do_gpio_write(gp, val) \
154 pmac_call_feature(PMAC_FTR_WRITE_GPIO, NULL, (gp)->addr, val)
155 #define do_gpio_read(gp) \
156 pmac_call_feature(PMAC_FTR_READ_GPIO, NULL, (gp)->addr, 0)
157 #define tumbler_gpio_free(gp) /* NOP */
159 #define do_gpio_write(gp, val) writeb(val, (gp)->addr)
160 #define do_gpio_read(gp) readb((gp)->addr)
161 static inline void tumbler_gpio_free(pmac_gpio_t *gp)
168 #endif /* CONFIG_PPC_HAS_FEATURE_CALLS */
170 static void write_audio_gpio(pmac_gpio_t *gp, int active)
174 active = active ? gp->active_state : !gp->active_state;
175 do_gpio_write(gp, active ? 0x05 : 0x04);
178 static int read_audio_gpio(pmac_gpio_t *gp)
183 ret = ((do_gpio_read(gp) & 0x02) !=0);
184 return ret == gp->active_state;
188 * update master volume
190 static int tumbler_set_master_volume(pmac_tumbler_t *mix)
192 unsigned char block[6];
193 unsigned int left_vol, right_vol;
195 if (! mix->i2c.client)
198 if (! mix->master_switch[0])
201 left_vol = mix->master_vol[0];
202 if (left_vol >= ARRAY_SIZE(master_volume_table))
203 left_vol = ARRAY_SIZE(master_volume_table) - 1;
204 left_vol = master_volume_table[left_vol];
206 if (! mix->master_switch[1])
209 right_vol = mix->master_vol[1];
210 if (right_vol >= ARRAY_SIZE(master_volume_table))
211 right_vol = ARRAY_SIZE(master_volume_table) - 1;
212 right_vol = master_volume_table[right_vol];
215 block[0] = (left_vol >> 16) & 0xff;
216 block[1] = (left_vol >> 8) & 0xff;
217 block[2] = (left_vol >> 0) & 0xff;
219 block[3] = (right_vol >> 16) & 0xff;
220 block[4] = (right_vol >> 8) & 0xff;
221 block[5] = (right_vol >> 0) & 0xff;
223 if (snd_pmac_keywest_write(&mix->i2c, TAS_REG_VOL, 6, block) < 0) {
224 snd_printk("failed to set volume \n");
232 static int tumbler_info_master_volume(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
234 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
236 uinfo->value.integer.min = 0;
237 uinfo->value.integer.max = ARRAY_SIZE(master_volume_table) - 1;
241 static int tumbler_get_master_volume(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
243 pmac_t *chip = snd_kcontrol_chip(kcontrol);
244 pmac_tumbler_t *mix = chip->mixer_data;
245 snd_assert(mix, return -ENODEV);
246 ucontrol->value.integer.value[0] = mix->master_vol[0];
247 ucontrol->value.integer.value[1] = mix->master_vol[1];
251 static int tumbler_put_master_volume(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
253 pmac_t *chip = snd_kcontrol_chip(kcontrol);
254 pmac_tumbler_t *mix = chip->mixer_data;
257 snd_assert(mix, return -ENODEV);
258 change = mix->master_vol[0] != ucontrol->value.integer.value[0] ||
259 mix->master_vol[1] != ucontrol->value.integer.value[1];
261 mix->master_vol[0] = ucontrol->value.integer.value[0];
262 mix->master_vol[1] = ucontrol->value.integer.value[1];
263 tumbler_set_master_volume(mix);
269 static int tumbler_get_master_switch(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
271 pmac_t *chip = snd_kcontrol_chip(kcontrol);
272 pmac_tumbler_t *mix = chip->mixer_data;
273 snd_assert(mix, return -ENODEV);
274 ucontrol->value.integer.value[0] = mix->master_switch[0];
275 ucontrol->value.integer.value[1] = mix->master_switch[1];
279 static int tumbler_put_master_switch(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
281 pmac_t *chip = snd_kcontrol_chip(kcontrol);
282 pmac_tumbler_t *mix = chip->mixer_data;
285 snd_assert(mix, return -ENODEV);
286 change = mix->master_switch[0] != ucontrol->value.integer.value[0] ||
287 mix->master_switch[1] != ucontrol->value.integer.value[1];
289 mix->master_switch[0] = !!ucontrol->value.integer.value[0];
290 mix->master_switch[1] = !!ucontrol->value.integer.value[1];
291 tumbler_set_master_volume(mix);
298 * TAS3001c dynamic range compression
301 #define TAS3001_DRC_MAX 0x5f
303 static int tumbler_set_drc(pmac_tumbler_t *mix)
305 unsigned char val[2];
307 if (! mix->i2c.client)
310 if (mix->drc_enable) {
311 val[0] = 0xc1; /* enable, 3:1 compression */
312 if (mix->drc_range > TAS3001_DRC_MAX)
314 else if (mix->drc_range < 0)
317 val[1] = mix->drc_range + 0x91;
323 if (snd_pmac_keywest_write(&mix->i2c, TAS_REG_DRC, 2, val) < 0) {
324 snd_printk("failed to set DRC\n");
334 #define TAS3004_DRC_MAX 0xef
336 static int snapper_set_drc(pmac_tumbler_t *mix)
338 unsigned char val[6];
340 if (! mix->i2c.client)
344 val[0] = 0x50; /* 3:1 above threshold */
346 val[0] = 0x51; /* disabled */
347 val[1] = 0x02; /* 1:1 below threshold */
348 if (mix->drc_range > 0xef)
350 else if (mix->drc_range < 0)
353 val[2] = mix->drc_range;
358 if (snd_pmac_keywest_write(&mix->i2c, TAS_REG_DRC, 6, val) < 0) {
359 snd_printk("failed to set DRC\n");
365 static int tumbler_info_drc_value(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
367 pmac_t *chip = snd_kcontrol_chip(kcontrol);
368 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
370 uinfo->value.integer.min = 0;
371 uinfo->value.integer.max =
372 chip->model == PMAC_TUMBLER ? TAS3001_DRC_MAX : TAS3004_DRC_MAX;
376 static int tumbler_get_drc_value(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
378 pmac_t *chip = snd_kcontrol_chip(kcontrol);
380 if (! (mix = chip->mixer_data))
382 ucontrol->value.integer.value[0] = mix->drc_range;
386 static int tumbler_put_drc_value(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
388 pmac_t *chip = snd_kcontrol_chip(kcontrol);
392 if (! (mix = chip->mixer_data))
394 change = mix->drc_range != ucontrol->value.integer.value[0];
396 mix->drc_range = ucontrol->value.integer.value[0];
397 if (chip->model == PMAC_TUMBLER)
398 tumbler_set_drc(mix);
400 snapper_set_drc(mix);
405 static int tumbler_get_drc_switch(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
407 pmac_t *chip = snd_kcontrol_chip(kcontrol);
409 if (! (mix = chip->mixer_data))
411 ucontrol->value.integer.value[0] = mix->drc_enable;
415 static int tumbler_put_drc_switch(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
417 pmac_t *chip = snd_kcontrol_chip(kcontrol);
421 if (! (mix = chip->mixer_data))
423 change = mix->drc_enable != ucontrol->value.integer.value[0];
425 mix->drc_enable = !!ucontrol->value.integer.value[0];
426 if (chip->model == PMAC_TUMBLER)
427 tumbler_set_drc(mix);
429 snapper_set_drc(mix);
439 struct tumbler_mono_vol {
447 static int tumbler_set_mono_volume(pmac_tumbler_t *mix, struct tumbler_mono_vol *info)
449 unsigned char block[4];
453 if (! mix->i2c.client)
456 vol = mix->mono_vol[info->index];
457 if (vol >= info->max)
459 vol = info->table[vol];
460 for (i = 0; i < info->bytes; i++)
461 block[i] = (vol >> ((info->bytes - i - 1) * 8)) & 0xff;
462 if (snd_pmac_keywest_write(&mix->i2c, info->reg, info->bytes, block) < 0) {
463 snd_printk("failed to set mono volume %d\n", info->index);
469 static int tumbler_info_mono(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
471 struct tumbler_mono_vol *info = (struct tumbler_mono_vol *)kcontrol->private_value;
473 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
475 uinfo->value.integer.min = 0;
476 uinfo->value.integer.max = info->max - 1;
480 static int tumbler_get_mono(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
482 struct tumbler_mono_vol *info = (struct tumbler_mono_vol *)kcontrol->private_value;
483 pmac_t *chip = snd_kcontrol_chip(kcontrol);
485 if (! (mix = chip->mixer_data))
487 ucontrol->value.integer.value[0] = mix->mono_vol[info->index];
491 static int tumbler_put_mono(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
493 struct tumbler_mono_vol *info = (struct tumbler_mono_vol *)kcontrol->private_value;
494 pmac_t *chip = snd_kcontrol_chip(kcontrol);
498 if (! (mix = chip->mixer_data))
500 change = mix->mono_vol[info->index] != ucontrol->value.integer.value[0];
502 mix->mono_vol[info->index] = ucontrol->value.integer.value[0];
503 tumbler_set_mono_volume(mix, info);
508 /* TAS3001c mono volumes */
509 static struct tumbler_mono_vol tumbler_pcm_vol_info = {
510 .index = VOL_IDX_PCM_MONO,
513 .max = ARRAY_SIZE(mixer_volume_table),
514 .table = mixer_volume_table,
517 static struct tumbler_mono_vol tumbler_bass_vol_info = {
518 .index = VOL_IDX_BASS,
521 .max = ARRAY_SIZE(bass_volume_table),
522 .table = bass_volume_table,
525 static struct tumbler_mono_vol tumbler_treble_vol_info = {
526 .index = VOL_IDX_TREBLE,
527 .reg = TAS_REG_TREBLE,
529 .max = ARRAY_SIZE(treble_volume_table),
530 .table = treble_volume_table,
533 /* TAS3004 mono volumes */
534 static struct tumbler_mono_vol snapper_bass_vol_info = {
535 .index = VOL_IDX_BASS,
538 .max = ARRAY_SIZE(snapper_bass_volume_table),
539 .table = snapper_bass_volume_table,
542 static struct tumbler_mono_vol snapper_treble_vol_info = {
543 .index = VOL_IDX_TREBLE,
544 .reg = TAS_REG_TREBLE,
546 .max = ARRAY_SIZE(snapper_treble_volume_table),
547 .table = snapper_treble_volume_table,
551 #define DEFINE_MONO(xname,type) { \
552 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,\
554 .info = tumbler_info_mono, \
555 .get = tumbler_get_mono, \
556 .put = tumbler_put_mono, \
557 .private_value = (unsigned long)(&tumbler_##type##_vol_info), \
560 #define DEFINE_SNAPPER_MONO(xname,type) { \
561 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,\
563 .info = tumbler_info_mono, \
564 .get = tumbler_get_mono, \
565 .put = tumbler_put_mono, \
566 .private_value = (unsigned long)(&snapper_##type##_vol_info), \
571 * snapper mixer volumes
574 static int snapper_set_mix_vol1(pmac_tumbler_t *mix, int idx, int ch, int reg)
577 unsigned char block[9];
579 vol = mix->mix_vol[idx][ch];
580 if (vol >= ARRAY_SIZE(mixer_volume_table)) {
581 vol = ARRAY_SIZE(mixer_volume_table) - 1;
582 mix->mix_vol[idx][ch] = vol;
585 for (i = 0; i < 3; i++) {
586 vol = mix->mix_vol[i][ch];
587 vol = mixer_volume_table[vol];
588 for (j = 0; j < 3; j++)
589 block[i * 3 + j] = (vol >> ((2 - j) * 8)) & 0xff;
591 if (snd_pmac_keywest_write(&mix->i2c, reg, 9, block) < 0) {
592 snd_printk("failed to set mono volume %d\n", reg);
598 static int snapper_set_mix_vol(pmac_tumbler_t *mix, int idx)
600 if (! mix->i2c.client)
602 if (snapper_set_mix_vol1(mix, idx, 0, TAS_REG_LMIX) < 0 ||
603 snapper_set_mix_vol1(mix, idx, 1, TAS_REG_RMIX) < 0)
608 static int snapper_info_mix(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
610 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
612 uinfo->value.integer.min = 0;
613 uinfo->value.integer.max = ARRAY_SIZE(mixer_volume_table) - 1;
617 static int snapper_get_mix(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
619 int idx = (int)kcontrol->private_value;
620 pmac_t *chip = snd_kcontrol_chip(kcontrol);
622 if (! (mix = chip->mixer_data))
624 ucontrol->value.integer.value[0] = mix->mix_vol[idx][0];
625 ucontrol->value.integer.value[1] = mix->mix_vol[idx][1];
629 static int snapper_put_mix(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
631 int idx = (int)kcontrol->private_value;
632 pmac_t *chip = snd_kcontrol_chip(kcontrol);
636 if (! (mix = chip->mixer_data))
638 change = mix->mix_vol[idx][0] != ucontrol->value.integer.value[0] ||
639 mix->mix_vol[idx][1] != ucontrol->value.integer.value[1];
641 mix->mix_vol[idx][0] = ucontrol->value.integer.value[0];
642 mix->mix_vol[idx][1] = ucontrol->value.integer.value[1];
643 snapper_set_mix_vol(mix, idx);
653 enum { TUMBLER_MUTE_HP, TUMBLER_MUTE_AMP };
655 static int tumbler_get_mute_switch(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
657 pmac_t *chip = snd_kcontrol_chip(kcontrol);
660 if (! (mix = chip->mixer_data))
662 gp = (kcontrol->private_value == TUMBLER_MUTE_HP) ? &mix->hp_mute : &mix->amp_mute;
663 ucontrol->value.integer.value[0] = ! read_audio_gpio(gp);
667 static int tumbler_put_mute_switch(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
669 pmac_t *chip = snd_kcontrol_chip(kcontrol);
673 #ifdef PMAC_SUPPORT_AUTOMUTE
674 if (chip->update_automute && chip->auto_mute)
675 return 0; /* don't touch in the auto-mute mode */
677 if (! (mix = chip->mixer_data))
679 gp = (kcontrol->private_value == TUMBLER_MUTE_HP) ? &mix->hp_mute : &mix->amp_mute;
680 val = ! read_audio_gpio(gp);
681 if (val != ucontrol->value.integer.value[0]) {
682 write_audio_gpio(gp, ! ucontrol->value.integer.value[0]);
688 static int snapper_set_capture_source(pmac_tumbler_t *mix)
690 if (! mix->i2c.client)
692 return snd_pmac_keywest_write_byte(&mix->i2c, TAS_REG_ACS,
693 mix->capture_source ? 2 : 0);
696 static int snapper_info_capture_source(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
698 static char *texts[2] = {
701 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
703 uinfo->value.enumerated.items = 2;
704 if (uinfo->value.enumerated.item > 1)
705 uinfo->value.enumerated.item = 1;
706 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
710 static int snapper_get_capture_source(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
712 pmac_t *chip = snd_kcontrol_chip(kcontrol);
713 pmac_tumbler_t *mix = chip->mixer_data;
715 snd_assert(mix, return -ENODEV);
716 ucontrol->value.integer.value[0] = mix->capture_source;
720 static int snapper_put_capture_source(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
722 pmac_t *chip = snd_kcontrol_chip(kcontrol);
723 pmac_tumbler_t *mix = chip->mixer_data;
726 snd_assert(mix, return -ENODEV);
727 change = ucontrol->value.integer.value[0] != mix->capture_source;
729 mix->capture_source = !!ucontrol->value.integer.value[0];
730 snapper_set_capture_source(mix);
735 #define DEFINE_SNAPPER_MIX(xname,idx,ofs) { \
736 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,\
738 .info = snapper_info_mix, \
739 .get = snapper_get_mix, \
740 .put = snapper_put_mix, \
742 .private_value = ofs, \
748 static snd_kcontrol_new_t tumbler_mixers[] __initdata = {
749 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
750 .name = "Master Playback Volume",
751 .info = tumbler_info_master_volume,
752 .get = tumbler_get_master_volume,
753 .put = tumbler_put_master_volume
755 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
756 .name = "Master Playback Switch",
757 .info = snd_pmac_boolean_stereo_info,
758 .get = tumbler_get_master_switch,
759 .put = tumbler_put_master_switch
761 DEFINE_MONO("Tone Control - Bass", bass),
762 DEFINE_MONO("Tone Control - Treble", treble),
763 DEFINE_MONO("PCM Playback Volume", pcm),
764 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
766 .info = tumbler_info_drc_value,
767 .get = tumbler_get_drc_value,
768 .put = tumbler_put_drc_value
772 static snd_kcontrol_new_t snapper_mixers[] __initdata = {
773 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
774 .name = "Master Playback Volume",
775 .info = tumbler_info_master_volume,
776 .get = tumbler_get_master_volume,
777 .put = tumbler_put_master_volume
779 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
780 .name = "Master Playback Switch",
781 .info = snd_pmac_boolean_stereo_info,
782 .get = tumbler_get_master_switch,
783 .put = tumbler_put_master_switch
785 DEFINE_SNAPPER_MIX("PCM Playback Volume", 0, VOL_IDX_PCM),
786 DEFINE_SNAPPER_MIX("PCM Playback Volume", 1, VOL_IDX_PCM2),
787 DEFINE_SNAPPER_MIX("Monitor Mix Volume", 0, VOL_IDX_ADC),
788 DEFINE_SNAPPER_MONO("Tone Control - Bass", bass),
789 DEFINE_SNAPPER_MONO("Tone Control - Treble", treble),
790 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
792 .info = tumbler_info_drc_value,
793 .get = tumbler_get_drc_value,
794 .put = tumbler_put_drc_value
796 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
797 .name = "Input Source", /* FIXME: "Capture Source" doesn't work properly */
798 .info = snapper_info_capture_source,
799 .get = snapper_get_capture_source,
800 .put = snapper_put_capture_source
804 static snd_kcontrol_new_t tumbler_hp_sw __initdata = {
805 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
806 .name = "Headphone Playback Switch",
807 .info = snd_pmac_boolean_mono_info,
808 .get = tumbler_get_mute_switch,
809 .put = tumbler_put_mute_switch,
810 .private_value = TUMBLER_MUTE_HP,
812 static snd_kcontrol_new_t tumbler_speaker_sw __initdata = {
813 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
814 .name = "PC Speaker Playback Switch",
815 .info = snd_pmac_boolean_mono_info,
816 .get = tumbler_get_mute_switch,
817 .put = tumbler_put_mute_switch,
818 .private_value = TUMBLER_MUTE_AMP,
820 static snd_kcontrol_new_t tumbler_drc_sw __initdata = {
821 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
822 .name = "DRC Switch",
823 .info = snd_pmac_boolean_mono_info,
824 .get = tumbler_get_drc_switch,
825 .put = tumbler_put_drc_switch
829 #ifdef PMAC_SUPPORT_AUTOMUTE
833 static int tumbler_detect_headphone(pmac_t *chip)
835 pmac_tumbler_t *mix = chip->mixer_data;
836 return read_audio_gpio(&mix->hp_detect);
839 static void check_mute(pmac_t *chip, pmac_gpio_t *gp, int val, int do_notify, snd_kcontrol_t *sw)
841 //pmac_tumbler_t *mix = chip->mixer_data;
842 if (val != read_audio_gpio(gp)) {
843 write_audio_gpio(gp, val);
845 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE, &sw->id);
849 static struct work_struct device_change;
852 device_change_handler(void *self)
854 pmac_t *chip = (pmac_t*) self;
860 mix = chip->mixer_data;
862 /* first set the DRC so the speaker do not explode -ReneR */
863 if (chip->model == PMAC_TUMBLER)
864 tumbler_set_drc(mix);
866 snapper_set_drc(mix);
868 /* reset the master volume so the correct amplification is applied */
869 tumbler_set_master_volume(mix);
872 static void tumbler_update_automute(pmac_t *chip, int do_notify)
874 if (chip->auto_mute) {
875 pmac_tumbler_t *mix = chip->mixer_data;
876 snd_assert(mix, return);
877 if (tumbler_detect_headphone(chip)) {
879 check_mute(chip, &mix->amp_mute, 1, do_notify, chip->speaker_sw_ctl);
880 check_mute(chip, &mix->hp_mute, 0, do_notify, chip->master_sw_ctl);
885 check_mute(chip, &mix->amp_mute, 0, do_notify, chip->speaker_sw_ctl);
886 check_mute(chip, &mix->hp_mute, 1, do_notify, chip->master_sw_ctl);
890 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
891 &chip->hp_detect_ctl->id);
892 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
893 &chip->drc_sw_ctl->id);
896 /* finally we need to schedule an update of the mixer values
897 (master and DRC are enough for now) -ReneR */
898 schedule_work(&device_change);
902 #endif /* PMAC_SUPPORT_AUTOMUTE */
905 /* interrupt - headphone plug changed */
906 static irqreturn_t headphone_intr(int irq, void *devid, struct pt_regs *regs)
908 pmac_t *chip = devid;
909 if (chip->update_automute && chip->initialized) {
910 chip->update_automute(chip, 1);
916 /* look for audio-gpio device */
917 static struct device_node *find_audio_device(const char *name)
919 struct device_node *np;
921 if (! (np = find_devices("gpio")))
924 for (np = np->child; np; np = np->sibling) {
925 char *property = get_property(np, "audio-gpio", NULL);
926 if (property && strcmp(property, name) == 0)
932 /* look for audio-gpio device */
933 static struct device_node *find_compatible_audio_device(const char *name)
935 struct device_node *np;
937 if (! (np = find_devices("gpio")))
940 for (np = np->child; np; np = np->sibling) {
941 if (device_is_compatible(np, name))
947 /* find an audio device and get its address */
948 static unsigned long tumbler_find_device(const char *device, pmac_gpio_t *gp, int is_compatible)
950 struct device_node *node;
954 node = find_compatible_audio_device(device);
956 node = find_audio_device(device);
958 snd_printdd("cannot find device %s\n", device);
962 base = (u32 *)get_property(node, "AAPL,address", NULL);
964 snd_printd("cannot find address for device %s\n", device);
968 #ifdef CONFIG_PPC_HAS_FEATURE_CALLS
969 gp->addr = (*base) & 0x0000ffff;
971 gp->addr = (void*)ioremap((unsigned long)(*base), 1);
973 base = (u32 *)get_property(node, "audio-gpio-active-state", NULL);
975 gp->active_state = *base;
977 gp->active_state = 1;
980 return (node->n_intrs > 0) ? node->intrs[0].line : 0;
984 static void tumbler_reset_audio(pmac_t *chip)
986 pmac_tumbler_t *mix = chip->mixer_data;
988 write_audio_gpio(&mix->audio_reset, 0);
990 write_audio_gpio(&mix->audio_reset, 1);
992 write_audio_gpio(&mix->audio_reset, 0);
996 #ifdef CONFIG_PMAC_PBOOK
998 static void tumbler_resume(pmac_t *chip)
1000 pmac_tumbler_t *mix = chip->mixer_data;
1002 snd_assert(mix, return);
1004 tumbler_reset_audio(chip);
1005 if (mix->i2c.client && mix->i2c.init_client) {
1006 if (mix->i2c.init_client(&mix->i2c) < 0)
1007 printk(KERN_ERR "tumbler_init_client error\n");
1009 printk(KERN_ERR "tumbler: i2c is not initialized\n");
1010 if (chip->model == PMAC_TUMBLER) {
1011 tumbler_set_mono_volume(mix, &tumbler_pcm_vol_info);
1012 tumbler_set_mono_volume(mix, &tumbler_bass_vol_info);
1013 tumbler_set_mono_volume(mix, &tumbler_treble_vol_info);
1014 tumbler_set_drc(mix);
1016 snapper_set_mix_vol(mix, VOL_IDX_PCM);
1017 snapper_set_mix_vol(mix, VOL_IDX_PCM2);
1018 snapper_set_mix_vol(mix, VOL_IDX_ADC);
1019 tumbler_set_mono_volume(mix, &snapper_bass_vol_info);
1020 tumbler_set_mono_volume(mix, &snapper_treble_vol_info);
1021 snapper_set_drc(mix);
1022 snapper_set_capture_source(mix);
1024 tumbler_set_master_volume(mix);
1025 if (chip->update_automute)
1026 chip->update_automute(chip, 0);
1030 /* initialize tumbler */
1031 static int __init tumbler_init(pmac_t *chip)
1034 pmac_tumbler_t *mix = chip->mixer_data;
1035 snd_assert(mix, return -EINVAL);
1037 tumbler_find_device("audio-hw-reset", &mix->audio_reset, 0);
1038 tumbler_find_device("amp-mute", &mix->amp_mute, 0);
1039 tumbler_find_device("headphone-mute", &mix->hp_mute, 0);
1040 irq = tumbler_find_device("headphone-detect", &mix->hp_detect, 0);
1042 irq = tumbler_find_device("keywest-gpio15", &mix->hp_detect, 1);
1044 tumbler_reset_audio(chip);
1046 /* activate headphone status interrupts */
1049 if ((err = request_irq(irq, headphone_intr, 0,
1050 "Tumbler Headphone Detection", chip)) < 0)
1052 /* activate headphone status interrupts */
1053 val = do_gpio_read(&mix->hp_detect);
1054 do_gpio_write(&mix->hp_detect, val | 0x80);
1056 mix->headphone_irq = irq;
1061 static void tumbler_cleanup(pmac_t *chip)
1063 pmac_tumbler_t *mix = chip->mixer_data;
1067 if (mix->headphone_irq >= 0)
1068 free_irq(mix->headphone_irq, chip);
1069 tumbler_gpio_free(&mix->audio_reset);
1070 tumbler_gpio_free(&mix->amp_mute);
1071 tumbler_gpio_free(&mix->hp_mute);
1072 tumbler_gpio_free(&mix->hp_detect);
1073 snd_pmac_keywest_cleanup(&mix->i2c);
1075 chip->mixer_data = NULL;
1079 int __init snd_pmac_tumbler_init(pmac_t *chip)
1082 pmac_tumbler_t *mix;
1084 struct device_node *tas_node;
1088 if (current->fs->root)
1089 request_module("i2c-keywest");
1090 #endif /* CONFIG_KMOD */
1092 mix = kmalloc(sizeof(*mix), GFP_KERNEL);
1095 memset(mix, 0, sizeof(*mix));
1096 mix->headphone_irq = -1;
1098 chip->mixer_data = mix;
1099 chip->mixer_free = tumbler_cleanup;
1101 if ((err = tumbler_init(chip)) < 0)
1105 tas_node = find_devices("deq");
1106 if (tas_node == NULL)
1109 paddr = (u32 *)get_property(tas_node, "i2c-address", NULL);
1111 mix->i2c.addr = (*paddr) >> 1;
1113 mix->i2c.addr = TAS_I2C_ADDR;
1115 if (chip->model == PMAC_TUMBLER) {
1116 mix->i2c.init_client = tumbler_init_client;
1117 mix->i2c.name = "TAS3001c";
1118 chipname = "Tumbler";
1120 mix->i2c.init_client = snapper_init_client;
1121 mix->i2c.name = "TAS3004";
1122 chipname = "Snapper";
1125 if ((err = snd_pmac_keywest_init(&mix->i2c)) < 0)
1131 sprintf(chip->card->mixername, "PowerMac %s", chipname);
1133 if (chip->model == PMAC_TUMBLER) {
1134 for (i = 0; i < ARRAY_SIZE(tumbler_mixers); i++) {
1135 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&tumbler_mixers[i], chip))) < 0)
1139 for (i = 0; i < ARRAY_SIZE(snapper_mixers); i++) {
1140 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snapper_mixers[i], chip))) < 0)
1144 chip->master_sw_ctl = snd_ctl_new1(&tumbler_hp_sw, chip);
1145 if ((err = snd_ctl_add(chip->card, chip->master_sw_ctl)) < 0)
1147 chip->speaker_sw_ctl = snd_ctl_new1(&tumbler_speaker_sw, chip);
1148 if ((err = snd_ctl_add(chip->card, chip->speaker_sw_ctl)) < 0)
1150 chip->drc_sw_ctl = snd_ctl_new1(&tumbler_drc_sw, chip);
1151 if ((err = snd_ctl_add(chip->card, chip->drc_sw_ctl)) < 0)
1155 #ifdef CONFIG_PMAC_PBOOK
1156 chip->resume = tumbler_resume;
1159 INIT_WORK(&device_change, device_change_handler, (void *)chip);
1161 #ifdef PMAC_SUPPORT_AUTOMUTE
1162 if (mix->headphone_irq >=0 && (err = snd_pmac_add_automute(chip)) < 0)
1164 chip->detect_headphone = tumbler_detect_headphone;
1165 chip->update_automute = tumbler_update_automute;
1166 tumbler_update_automute(chip, 0); /* update the status only */