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>
44 #include "tumbler_volume.h"
46 /* i2c address for tumbler */
47 #define TAS_I2C_ADDR 0x34
50 #define TAS_REG_MCS 0x01 /* main control */
51 #define TAS_REG_DRC 0x02
52 #define TAS_REG_VOL 0x04
53 #define TAS_REG_TREBLE 0x05
54 #define TAS_REG_BASS 0x06
55 #define TAS_REG_INPUT1 0x07
56 #define TAS_REG_INPUT2 0x08
59 #define TAS_REG_PCM TAS_REG_INPUT1
62 #define TAS_REG_LMIX TAS_REG_INPUT1
63 #define TAS_REG_RMIX TAS_REG_INPUT2
64 #define TAS_REG_MCS2 0x43 /* main control 2 */
65 #define TAS_REG_ACS 0x40 /* analog control */
67 /* mono volumes for tas3001c/tas3004 */
69 VOL_IDX_PCM_MONO, /* tas3001c only */
70 VOL_IDX_BASS, VOL_IDX_TREBLE,
74 /* stereo volumes for tas3004 */
76 VOL_IDX_PCM, VOL_IDX_PCM2, VOL_IDX_ADC,
80 typedef struct pmac_gpio {
81 #ifdef CONFIG_PPC_HAS_FEATURE_CALLS
89 typedef struct pmac_tumbler_t {
91 pmac_gpio_t audio_reset;
94 pmac_gpio_t hp_detect;
96 unsigned int master_vol[2];
97 unsigned int save_master_switch[2];
98 unsigned int master_switch[2];
99 unsigned int mono_vol[VOL_IDX_LAST_MONO];
100 unsigned int mix_vol[VOL_IDX_LAST_MIX][2]; /* stereo volumes for tas3004 */
105 int auto_mute_notify;
114 static int send_init_client(pmac_keywest_t *i2c, unsigned int *regs)
119 err = i2c_smbus_write_byte_data(i2c->client,
133 static int tumbler_init_client(pmac_keywest_t *i2c)
135 static unsigned int regs[] = {
136 /* normal operation, SCLK=64fps, i2s output, i2s input, 16bit width */
137 TAS_REG_MCS, (1<<6)|(2<<4)|(2<<2)|0,
140 return send_init_client(i2c, regs);
143 static int snapper_init_client(pmac_keywest_t *i2c)
145 static unsigned int regs[] = {
146 /* normal operation, SCLK=64fps, i2s output, 16bit width */
147 TAS_REG_MCS, (1<<6)|(2<<4)|0,
148 /* normal operation, all-pass mode */
149 TAS_REG_MCS2, (1<<1),
150 /* normal output, no deemphasis, A input, power-up, line-in */
154 return send_init_client(i2c, regs);
160 #ifdef CONFIG_PPC_HAS_FEATURE_CALLS
161 #define do_gpio_write(gp, val) \
162 pmac_call_feature(PMAC_FTR_WRITE_GPIO, NULL, (gp)->addr, val)
163 #define do_gpio_read(gp) \
164 pmac_call_feature(PMAC_FTR_READ_GPIO, NULL, (gp)->addr, 0)
165 #define tumbler_gpio_free(gp) /* NOP */
167 #define do_gpio_write(gp, val) writeb(val, (gp)->addr)
168 #define do_gpio_read(gp) readb((gp)->addr)
169 static inline void tumbler_gpio_free(pmac_gpio_t *gp)
176 #endif /* CONFIG_PPC_HAS_FEATURE_CALLS */
178 static void write_audio_gpio(pmac_gpio_t *gp, int active)
182 active = active ? gp->active_state : !gp->active_state;
183 do_gpio_write(gp, active ? 0x05 : 0x04);
186 static int read_audio_gpio(pmac_gpio_t *gp)
191 ret = ((do_gpio_read(gp) & 0x02) !=0);
192 return ret == gp->active_state;
196 * update master volume
198 static int tumbler_set_master_volume(pmac_tumbler_t *mix)
200 unsigned char block[6];
201 unsigned int left_vol, right_vol;
203 if (! mix->i2c.client)
206 if (! mix->master_switch[0])
209 left_vol = mix->master_vol[0];
210 if (left_vol >= ARRAY_SIZE(master_volume_table))
211 left_vol = ARRAY_SIZE(master_volume_table) - 1;
212 left_vol = master_volume_table[left_vol];
214 if (! mix->master_switch[1])
217 right_vol = mix->master_vol[1];
218 if (right_vol >= ARRAY_SIZE(master_volume_table))
219 right_vol = ARRAY_SIZE(master_volume_table) - 1;
220 right_vol = master_volume_table[right_vol];
223 block[0] = (left_vol >> 16) & 0xff;
224 block[1] = (left_vol >> 8) & 0xff;
225 block[2] = (left_vol >> 0) & 0xff;
227 block[3] = (right_vol >> 16) & 0xff;
228 block[4] = (right_vol >> 8) & 0xff;
229 block[5] = (right_vol >> 0) & 0xff;
231 if (i2c_smbus_write_block_data(mix->i2c.client, TAS_REG_VOL,
233 snd_printk("failed to set volume \n");
241 static int tumbler_info_master_volume(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
243 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
245 uinfo->value.integer.min = 0;
246 uinfo->value.integer.max = ARRAY_SIZE(master_volume_table) - 1;
250 static int tumbler_get_master_volume(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
252 pmac_t *chip = snd_kcontrol_chip(kcontrol);
253 pmac_tumbler_t *mix = chip->mixer_data;
254 snd_assert(mix, return -ENODEV);
255 ucontrol->value.integer.value[0] = mix->master_vol[0];
256 ucontrol->value.integer.value[1] = mix->master_vol[1];
260 static int tumbler_put_master_volume(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
262 pmac_t *chip = snd_kcontrol_chip(kcontrol);
263 pmac_tumbler_t *mix = chip->mixer_data;
266 snd_assert(mix, return -ENODEV);
267 change = mix->master_vol[0] != ucontrol->value.integer.value[0] ||
268 mix->master_vol[1] != ucontrol->value.integer.value[1];
270 mix->master_vol[0] = ucontrol->value.integer.value[0];
271 mix->master_vol[1] = ucontrol->value.integer.value[1];
272 tumbler_set_master_volume(mix);
278 static int tumbler_get_master_switch(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
280 pmac_t *chip = snd_kcontrol_chip(kcontrol);
281 pmac_tumbler_t *mix = chip->mixer_data;
282 snd_assert(mix, return -ENODEV);
283 ucontrol->value.integer.value[0] = mix->master_switch[0];
284 ucontrol->value.integer.value[1] = mix->master_switch[1];
288 static int tumbler_put_master_switch(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
290 pmac_t *chip = snd_kcontrol_chip(kcontrol);
291 pmac_tumbler_t *mix = chip->mixer_data;
294 snd_assert(mix, return -ENODEV);
295 change = mix->master_switch[0] != ucontrol->value.integer.value[0] ||
296 mix->master_switch[1] != ucontrol->value.integer.value[1];
298 mix->master_switch[0] = !!ucontrol->value.integer.value[0];
299 mix->master_switch[1] = !!ucontrol->value.integer.value[1];
300 tumbler_set_master_volume(mix);
307 * TAS3001c dynamic range compression
310 #define TAS3001_DRC_MAX 0x5f
312 static int tumbler_set_drc(pmac_tumbler_t *mix)
314 unsigned char val[2];
316 if (! mix->i2c.client)
319 if (mix->drc_enable) {
320 val[0] = 0xc1; /* enable, 3:1 compression */
321 if (mix->drc_range > TAS3001_DRC_MAX)
323 else if (mix->drc_range < 0)
326 val[1] = mix->drc_range + 0x91;
332 if (i2c_smbus_write_block_data(mix->i2c.client, TAS_REG_DRC,
334 snd_printk("failed to set DRC\n");
344 #define TAS3004_DRC_MAX 0xef
346 static int snapper_set_drc(pmac_tumbler_t *mix)
348 unsigned char val[6];
350 if (! mix->i2c.client)
354 val[0] = 0x50; /* 3:1 above threshold */
356 val[0] = 0x51; /* disabled */
357 val[1] = 0x02; /* 1:1 below threshold */
358 if (mix->drc_range > 0xef)
360 else if (mix->drc_range < 0)
363 val[2] = mix->drc_range;
368 if (i2c_smbus_write_block_data(mix->i2c.client, TAS_REG_DRC,
370 snd_printk("failed to set DRC\n");
376 static int tumbler_info_drc_value(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
378 pmac_t *chip = snd_kcontrol_chip(kcontrol);
379 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
381 uinfo->value.integer.min = 0;
382 uinfo->value.integer.max =
383 chip->model == PMAC_TUMBLER ? TAS3001_DRC_MAX : TAS3004_DRC_MAX;
387 static int tumbler_get_drc_value(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
389 pmac_t *chip = snd_kcontrol_chip(kcontrol);
391 if (! (mix = chip->mixer_data))
393 ucontrol->value.integer.value[0] = mix->drc_range;
397 static int tumbler_put_drc_value(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
399 pmac_t *chip = snd_kcontrol_chip(kcontrol);
403 if (! (mix = chip->mixer_data))
405 change = mix->drc_range != ucontrol->value.integer.value[0];
407 mix->drc_range = ucontrol->value.integer.value[0];
408 if (chip->model == PMAC_TUMBLER)
409 tumbler_set_drc(mix);
411 snapper_set_drc(mix);
416 static int tumbler_get_drc_switch(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
418 pmac_t *chip = snd_kcontrol_chip(kcontrol);
420 if (! (mix = chip->mixer_data))
422 ucontrol->value.integer.value[0] = mix->drc_enable;
426 static int tumbler_put_drc_switch(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
428 pmac_t *chip = snd_kcontrol_chip(kcontrol);
432 if (! (mix = chip->mixer_data))
434 change = mix->drc_enable != ucontrol->value.integer.value[0];
436 mix->drc_enable = !!ucontrol->value.integer.value[0];
437 if (chip->model == PMAC_TUMBLER)
438 tumbler_set_drc(mix);
440 snapper_set_drc(mix);
450 struct tumbler_mono_vol {
458 static int tumbler_set_mono_volume(pmac_tumbler_t *mix, struct tumbler_mono_vol *info)
460 unsigned char block[4];
464 if (! mix->i2c.client)
467 vol = mix->mono_vol[info->index];
468 if (vol >= info->max)
470 vol = info->table[vol];
471 for (i = 0; i < info->bytes; i++)
472 block[i] = (vol >> ((info->bytes - i - 1) * 8)) & 0xff;
473 if (i2c_smbus_write_block_data(mix->i2c.client, info->reg,
474 info->bytes, block) < 0) {
475 snd_printk("failed to set mono volume %d\n", info->index);
481 static int tumbler_info_mono(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
483 struct tumbler_mono_vol *info = (struct tumbler_mono_vol *)kcontrol->private_value;
485 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
487 uinfo->value.integer.min = 0;
488 uinfo->value.integer.max = info->max - 1;
492 static int tumbler_get_mono(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
494 struct tumbler_mono_vol *info = (struct tumbler_mono_vol *)kcontrol->private_value;
495 pmac_t *chip = snd_kcontrol_chip(kcontrol);
497 if (! (mix = chip->mixer_data))
499 ucontrol->value.integer.value[0] = mix->mono_vol[info->index];
503 static int tumbler_put_mono(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
505 struct tumbler_mono_vol *info = (struct tumbler_mono_vol *)kcontrol->private_value;
506 pmac_t *chip = snd_kcontrol_chip(kcontrol);
510 if (! (mix = chip->mixer_data))
512 change = mix->mono_vol[info->index] != ucontrol->value.integer.value[0];
514 mix->mono_vol[info->index] = ucontrol->value.integer.value[0];
515 tumbler_set_mono_volume(mix, info);
520 /* TAS3001c mono volumes */
521 static struct tumbler_mono_vol tumbler_pcm_vol_info = {
522 .index = VOL_IDX_PCM_MONO,
525 .max = ARRAY_SIZE(mixer_volume_table),
526 .table = mixer_volume_table,
529 static struct tumbler_mono_vol tumbler_bass_vol_info = {
530 .index = VOL_IDX_BASS,
533 .max = ARRAY_SIZE(bass_volume_table),
534 .table = bass_volume_table,
537 static struct tumbler_mono_vol tumbler_treble_vol_info = {
538 .index = VOL_IDX_TREBLE,
539 .reg = TAS_REG_TREBLE,
541 .max = ARRAY_SIZE(treble_volume_table),
542 .table = treble_volume_table,
545 /* TAS3004 mono volumes */
546 static struct tumbler_mono_vol snapper_bass_vol_info = {
547 .index = VOL_IDX_BASS,
550 .max = ARRAY_SIZE(snapper_bass_volume_table),
551 .table = snapper_bass_volume_table,
554 static struct tumbler_mono_vol snapper_treble_vol_info = {
555 .index = VOL_IDX_TREBLE,
556 .reg = TAS_REG_TREBLE,
558 .max = ARRAY_SIZE(snapper_treble_volume_table),
559 .table = snapper_treble_volume_table,
563 #define DEFINE_MONO(xname,type) { \
564 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,\
566 .info = tumbler_info_mono, \
567 .get = tumbler_get_mono, \
568 .put = tumbler_put_mono, \
569 .private_value = (unsigned long)(&tumbler_##type##_vol_info), \
572 #define DEFINE_SNAPPER_MONO(xname,type) { \
573 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,\
575 .info = tumbler_info_mono, \
576 .get = tumbler_get_mono, \
577 .put = tumbler_put_mono, \
578 .private_value = (unsigned long)(&snapper_##type##_vol_info), \
583 * snapper mixer volumes
586 static int snapper_set_mix_vol1(pmac_tumbler_t *mix, int idx, int ch, int reg)
589 unsigned char block[9];
591 vol = mix->mix_vol[idx][ch];
592 if (vol >= ARRAY_SIZE(mixer_volume_table)) {
593 vol = ARRAY_SIZE(mixer_volume_table) - 1;
594 mix->mix_vol[idx][ch] = vol;
597 for (i = 0; i < 3; i++) {
598 vol = mix->mix_vol[i][ch];
599 vol = mixer_volume_table[vol];
600 for (j = 0; j < 3; j++)
601 block[i * 3 + j] = (vol >> ((2 - j) * 8)) & 0xff;
603 if (i2c_smbus_write_block_data(mix->i2c.client, reg, 9, block) < 0) {
604 snd_printk("failed to set mono volume %d\n", reg);
610 static int snapper_set_mix_vol(pmac_tumbler_t *mix, int idx)
612 if (! mix->i2c.client)
614 if (snapper_set_mix_vol1(mix, idx, 0, TAS_REG_LMIX) < 0 ||
615 snapper_set_mix_vol1(mix, idx, 1, TAS_REG_RMIX) < 0)
620 static int snapper_info_mix(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
622 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
624 uinfo->value.integer.min = 0;
625 uinfo->value.integer.max = ARRAY_SIZE(mixer_volume_table) - 1;
629 static int snapper_get_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);
634 if (! (mix = chip->mixer_data))
636 ucontrol->value.integer.value[0] = mix->mix_vol[idx][0];
637 ucontrol->value.integer.value[1] = mix->mix_vol[idx][1];
641 static int snapper_put_mix(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
643 int idx = (int)kcontrol->private_value;
644 pmac_t *chip = snd_kcontrol_chip(kcontrol);
648 if (! (mix = chip->mixer_data))
650 change = mix->mix_vol[idx][0] != ucontrol->value.integer.value[0] ||
651 mix->mix_vol[idx][1] != ucontrol->value.integer.value[1];
653 mix->mix_vol[idx][0] = ucontrol->value.integer.value[0];
654 mix->mix_vol[idx][1] = ucontrol->value.integer.value[1];
655 snapper_set_mix_vol(mix, idx);
662 * mute switches. FIXME: Turn that into software mute when both outputs are muted
663 * to avoid codec reset on ibook M7
666 enum { TUMBLER_MUTE_HP, TUMBLER_MUTE_AMP };
668 static int tumbler_get_mute_switch(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
670 pmac_t *chip = snd_kcontrol_chip(kcontrol);
673 if (! (mix = chip->mixer_data))
675 gp = (kcontrol->private_value == TUMBLER_MUTE_HP) ? &mix->hp_mute : &mix->amp_mute;
676 ucontrol->value.integer.value[0] = ! read_audio_gpio(gp);
680 static int tumbler_put_mute_switch(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
682 pmac_t *chip = snd_kcontrol_chip(kcontrol);
686 #ifdef PMAC_SUPPORT_AUTOMUTE
687 if (chip->update_automute && chip->auto_mute)
688 return 0; /* don't touch in the auto-mute mode */
690 if (! (mix = chip->mixer_data))
692 gp = (kcontrol->private_value == TUMBLER_MUTE_HP) ? &mix->hp_mute : &mix->amp_mute;
693 val = ! read_audio_gpio(gp);
694 if (val != ucontrol->value.integer.value[0]) {
695 write_audio_gpio(gp, ! ucontrol->value.integer.value[0]);
701 static int snapper_set_capture_source(pmac_tumbler_t *mix)
703 if (! mix->i2c.client)
705 if (mix->capture_source)
706 mix->acs = mix->acs |= 2;
709 return i2c_smbus_write_byte_data(mix->i2c.client, TAS_REG_ACS, mix->acs);
712 static int snapper_info_capture_source(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
714 static char *texts[2] = {
717 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
719 uinfo->value.enumerated.items = 2;
720 if (uinfo->value.enumerated.item > 1)
721 uinfo->value.enumerated.item = 1;
722 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
726 static int snapper_get_capture_source(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
728 pmac_t *chip = snd_kcontrol_chip(kcontrol);
729 pmac_tumbler_t *mix = chip->mixer_data;
731 snd_assert(mix, return -ENODEV);
732 ucontrol->value.integer.value[0] = mix->capture_source;
736 static int snapper_put_capture_source(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
738 pmac_t *chip = snd_kcontrol_chip(kcontrol);
739 pmac_tumbler_t *mix = chip->mixer_data;
742 snd_assert(mix, return -ENODEV);
743 change = ucontrol->value.integer.value[0] != mix->capture_source;
745 mix->capture_source = !!ucontrol->value.integer.value[0];
746 snapper_set_capture_source(mix);
751 #define DEFINE_SNAPPER_MIX(xname,idx,ofs) { \
752 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,\
754 .info = snapper_info_mix, \
755 .get = snapper_get_mix, \
756 .put = snapper_put_mix, \
758 .private_value = ofs, \
764 static snd_kcontrol_new_t tumbler_mixers[] __initdata = {
765 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
766 .name = "Master Playback Volume",
767 .info = tumbler_info_master_volume,
768 .get = tumbler_get_master_volume,
769 .put = tumbler_put_master_volume
771 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
772 .name = "Master Playback Switch",
773 .info = snd_pmac_boolean_stereo_info,
774 .get = tumbler_get_master_switch,
775 .put = tumbler_put_master_switch
777 DEFINE_MONO("Tone Control - Bass", bass),
778 DEFINE_MONO("Tone Control - Treble", treble),
779 DEFINE_MONO("PCM Playback Volume", pcm),
780 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
782 .info = tumbler_info_drc_value,
783 .get = tumbler_get_drc_value,
784 .put = tumbler_put_drc_value
788 static snd_kcontrol_new_t snapper_mixers[] __initdata = {
789 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
790 .name = "Master Playback Volume",
791 .info = tumbler_info_master_volume,
792 .get = tumbler_get_master_volume,
793 .put = tumbler_put_master_volume
795 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
796 .name = "Master Playback Switch",
797 .info = snd_pmac_boolean_stereo_info,
798 .get = tumbler_get_master_switch,
799 .put = tumbler_put_master_switch
801 DEFINE_SNAPPER_MIX("PCM Playback Volume", 0, VOL_IDX_PCM),
802 DEFINE_SNAPPER_MIX("PCM Playback Volume", 1, VOL_IDX_PCM2),
803 DEFINE_SNAPPER_MIX("Monitor Mix Volume", 0, VOL_IDX_ADC),
804 DEFINE_SNAPPER_MONO("Tone Control - Bass", bass),
805 DEFINE_SNAPPER_MONO("Tone Control - Treble", treble),
806 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
808 .info = tumbler_info_drc_value,
809 .get = tumbler_get_drc_value,
810 .put = tumbler_put_drc_value
812 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
813 .name = "Input Source", /* FIXME: "Capture Source" doesn't work properly */
814 .info = snapper_info_capture_source,
815 .get = snapper_get_capture_source,
816 .put = snapper_put_capture_source
820 static snd_kcontrol_new_t tumbler_hp_sw __initdata = {
821 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
822 .name = "Headphone Playback Switch",
823 .info = snd_pmac_boolean_mono_info,
824 .get = tumbler_get_mute_switch,
825 .put = tumbler_put_mute_switch,
826 .private_value = TUMBLER_MUTE_HP,
828 static snd_kcontrol_new_t tumbler_speaker_sw __initdata = {
829 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
830 .name = "PC Speaker Playback Switch",
831 .info = snd_pmac_boolean_mono_info,
832 .get = tumbler_get_mute_switch,
833 .put = tumbler_put_mute_switch,
834 .private_value = TUMBLER_MUTE_AMP,
836 static snd_kcontrol_new_t tumbler_drc_sw __initdata = {
837 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
838 .name = "DRC Switch",
839 .info = snd_pmac_boolean_mono_info,
840 .get = tumbler_get_drc_switch,
841 .put = tumbler_put_drc_switch
845 #ifdef PMAC_SUPPORT_AUTOMUTE
849 static int tumbler_detect_headphone(pmac_t *chip)
851 pmac_tumbler_t *mix = chip->mixer_data;
852 return read_audio_gpio(&mix->hp_detect);
855 static void check_mute(pmac_t *chip, pmac_gpio_t *gp, int val, int do_notify, snd_kcontrol_t *sw)
857 //pmac_tumbler_t *mix = chip->mixer_data;
858 if (val != read_audio_gpio(gp)) {
859 write_audio_gpio(gp, val);
861 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE, &sw->id);
865 static struct work_struct device_change;
867 static void device_change_handler(void *self)
869 pmac_t *chip = (pmac_t*) self;
875 mix = chip->mixer_data;
876 snd_assert(mix, return);
878 if (tumbler_detect_headphone(chip)) {
880 check_mute(chip, &mix->hp_mute, 0, mix->auto_mute_notify,
881 chip->master_sw_ctl);
882 if (mix->anded_reset)
884 check_mute(chip, &mix->amp_mute, 1, mix->auto_mute_notify,
885 chip->speaker_sw_ctl);
889 check_mute(chip, &mix->amp_mute, 0, mix->auto_mute_notify,
890 chip->speaker_sw_ctl);
891 if (mix->anded_reset)
893 check_mute(chip, &mix->hp_mute, 1, mix->auto_mute_notify,
894 chip->master_sw_ctl);
897 if (mix->auto_mute_notify) {
898 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
899 &chip->hp_detect_ctl->id);
900 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
901 &chip->drc_sw_ctl->id);
904 /* first set the DRC so the speaker do not explode -ReneR */
905 if (chip->model == PMAC_TUMBLER)
906 tumbler_set_drc(mix);
908 snapper_set_drc(mix);
910 /* reset the master volume so the correct amplification is applied */
911 tumbler_set_master_volume(mix);
914 static void tumbler_update_automute(pmac_t *chip, int do_notify)
916 if (chip->auto_mute) {
918 mix = chip->mixer_data;
919 snd_assert(mix, return);
920 mix->auto_mute_notify = do_notify;
921 schedule_work(&device_change);
924 #endif /* PMAC_SUPPORT_AUTOMUTE */
927 /* interrupt - headphone plug changed */
928 static irqreturn_t headphone_intr(int irq, void *devid, struct pt_regs *regs)
930 pmac_t *chip = devid;
931 if (chip->update_automute && chip->initialized) {
932 chip->update_automute(chip, 1);
938 /* look for audio-gpio device */
939 static struct device_node *find_audio_device(const char *name)
941 struct device_node *np;
943 if (! (np = find_devices("gpio")))
946 for (np = np->child; np; np = np->sibling) {
947 char *property = get_property(np, "audio-gpio", NULL);
948 if (property && strcmp(property, name) == 0)
954 /* look for audio-gpio device */
955 static struct device_node *find_compatible_audio_device(const char *name)
957 struct device_node *np;
959 if (! (np = find_devices("gpio")))
962 for (np = np->child; np; np = np->sibling) {
963 if (device_is_compatible(np, name))
969 /* find an audio device and get its address */
970 static long tumbler_find_device(const char *device, pmac_gpio_t *gp, int is_compatible)
972 struct device_node *node;
976 node = find_compatible_audio_device(device);
978 node = find_audio_device(device);
980 snd_printdd("cannot find device %s\n", device);
984 base = (u32 *)get_property(node, "AAPL,address", NULL);
986 base = (u32 *)get_property(node, "reg", NULL);
988 snd_printd("cannot find address for device %s\n", device);
991 /* this only work if PPC_HAS_FEATURE_CALLS is set as we
992 * are only getting the low part of the address
1000 #ifdef CONFIG_PPC_HAS_FEATURE_CALLS
1001 gp->addr = addr & 0x0000ffff;
1003 gp->addr = ioremap((unsigned long)addr, 1);
1005 /* Try to find the active state, default to 0 ! */
1006 base = (u32 *)get_property(node, "audio-gpio-active-state", NULL);
1008 gp->active_state = *base;
1010 gp->active_state = 0;
1012 return (node->n_intrs > 0) ? node->intrs[0].line : 0;
1016 static void tumbler_reset_audio(pmac_t *chip)
1018 pmac_tumbler_t *mix = chip->mixer_data;
1020 if (mix->anded_reset) {
1021 write_audio_gpio(&mix->hp_mute, 0);
1022 write_audio_gpio(&mix->amp_mute, 0);
1024 write_audio_gpio(&mix->hp_mute, 1);
1025 write_audio_gpio(&mix->amp_mute, 1);
1027 write_audio_gpio(&mix->hp_mute, 0);
1028 write_audio_gpio(&mix->amp_mute, 0);
1031 write_audio_gpio(&mix->audio_reset, 0);
1033 write_audio_gpio(&mix->audio_reset, 1);
1035 write_audio_gpio(&mix->audio_reset, 0);
1040 #ifdef CONFIG_PMAC_PBOOK
1042 static void tumbler_suspend(pmac_t *chip)
1044 pmac_tumbler_t *mix = chip->mixer_data;
1046 if (mix->headphone_irq >= 0)
1047 disable_irq(mix->headphone_irq);
1048 mix->save_master_switch[0] = mix->master_switch[0];
1049 mix->save_master_switch[1] = mix->master_switch[1];
1050 mix->master_switch[0] = mix->master_switch[1] = 0;
1051 tumbler_set_master_volume(mix);
1052 if (!mix->anded_reset) {
1053 write_audio_gpio(&mix->amp_mute, 1);
1054 write_audio_gpio(&mix->hp_mute, 1);
1056 if (chip->model == PMAC_SNAPPER) {
1058 i2c_smbus_write_byte_data(mix->i2c.client, TAS_REG_ACS, mix->acs);
1060 if (mix->anded_reset) {
1061 write_audio_gpio(&mix->amp_mute, 1);
1062 write_audio_gpio(&mix->hp_mute, 1);
1064 write_audio_gpio(&mix->audio_reset, 1);
1068 static void tumbler_resume(pmac_t *chip)
1070 pmac_tumbler_t *mix = chip->mixer_data;
1072 snd_assert(mix, return);
1075 mix->master_switch[0] = mix->save_master_switch[0];
1076 mix->master_switch[1] = mix->save_master_switch[1];
1077 tumbler_reset_audio(chip);
1078 if (mix->i2c.client && mix->i2c.init_client) {
1079 if (mix->i2c.init_client(&mix->i2c) < 0)
1080 printk(KERN_ERR "tumbler_init_client error\n");
1082 printk(KERN_ERR "tumbler: i2c is not initialized\n");
1083 if (chip->model == PMAC_TUMBLER) {
1084 tumbler_set_mono_volume(mix, &tumbler_pcm_vol_info);
1085 tumbler_set_mono_volume(mix, &tumbler_bass_vol_info);
1086 tumbler_set_mono_volume(mix, &tumbler_treble_vol_info);
1087 tumbler_set_drc(mix);
1089 snapper_set_mix_vol(mix, VOL_IDX_PCM);
1090 snapper_set_mix_vol(mix, VOL_IDX_PCM2);
1091 snapper_set_mix_vol(mix, VOL_IDX_ADC);
1092 tumbler_set_mono_volume(mix, &snapper_bass_vol_info);
1093 tumbler_set_mono_volume(mix, &snapper_treble_vol_info);
1094 snapper_set_drc(mix);
1095 snapper_set_capture_source(mix);
1097 tumbler_set_master_volume(mix);
1098 if (chip->update_automute)
1099 chip->update_automute(chip, 0);
1100 if (mix->headphone_irq >= 0)
1101 enable_irq(mix->headphone_irq);
1105 /* initialize tumbler */
1106 static int __init tumbler_init(pmac_t *chip)
1109 pmac_tumbler_t *mix = chip->mixer_data;
1110 snd_assert(mix, return -EINVAL);
1112 if (tumbler_find_device("audio-hw-reset", &mix->audio_reset, 0) < 0)
1113 tumbler_find_device("hw-reset", &mix->audio_reset, 1);
1114 if (tumbler_find_device("amp-mute", &mix->amp_mute, 0) < 0)
1115 tumbler_find_device("amp-mute", &mix->amp_mute, 1);
1116 if (tumbler_find_device("headphone-mute", &mix->hp_mute, 0) < 0)
1117 tumbler_find_device("headphone-mute", &mix->hp_mute, 1);
1118 irq = tumbler_find_device("headphone-detect", &mix->hp_detect, 0);
1120 irq = tumbler_find_device("headphone-detect", &mix->hp_detect, 1);
1122 irq = tumbler_find_device("keywest-gpio15", &mix->hp_detect, 1);
1124 tumbler_reset_audio(chip);
1126 /* activate headphone status interrupts */
1129 if ((err = request_irq(irq, headphone_intr, 0,
1130 "Tumbler Headphone Detection", chip)) < 0)
1132 /* activate headphone status interrupts */
1133 val = do_gpio_read(&mix->hp_detect);
1134 do_gpio_write(&mix->hp_detect, val | 0x80);
1136 mix->headphone_irq = irq;
1141 static void tumbler_cleanup(pmac_t *chip)
1143 pmac_tumbler_t *mix = chip->mixer_data;
1147 if (mix->headphone_irq >= 0)
1148 free_irq(mix->headphone_irq, chip);
1149 tumbler_gpio_free(&mix->audio_reset);
1150 tumbler_gpio_free(&mix->amp_mute);
1151 tumbler_gpio_free(&mix->hp_mute);
1152 tumbler_gpio_free(&mix->hp_detect);
1153 snd_pmac_keywest_cleanup(&mix->i2c);
1155 chip->mixer_data = NULL;
1159 int __init snd_pmac_tumbler_init(pmac_t *chip)
1162 pmac_tumbler_t *mix;
1164 struct device_node *tas_node, *np;
1168 if (current->fs->root)
1169 request_module("i2c-keywest");
1170 #endif /* CONFIG_KMOD */
1172 mix = kmalloc(sizeof(*mix), GFP_KERNEL);
1175 memset(mix, 0, sizeof(*mix));
1176 mix->headphone_irq = -1;
1178 chip->mixer_data = mix;
1179 chip->mixer_free = tumbler_cleanup;
1180 mix->anded_reset = 0;
1181 mix->reset_on_sleep = 1;
1183 for (np = chip->node->child; np; np = np->sibling) {
1184 if (!strcmp(np->name, "sound")) {
1185 if (get_property(np, "has-anded-reset", NULL))
1186 mix->anded_reset = 1;
1187 if (get_property(np, "layout-id", NULL))
1188 mix->reset_on_sleep = 0;
1192 if ((err = tumbler_init(chip)) < 0)
1196 tas_node = find_devices("deq");
1197 if (tas_node == NULL)
1198 tas_node = find_devices("codec");
1199 if (tas_node == NULL)
1202 paddr = (u32 *)get_property(tas_node, "i2c-address", NULL);
1204 paddr = (u32 *)get_property(tas_node, "reg", NULL);
1206 mix->i2c.addr = (*paddr) >> 1;
1208 mix->i2c.addr = TAS_I2C_ADDR;
1210 if (chip->model == PMAC_TUMBLER) {
1211 mix->i2c.init_client = tumbler_init_client;
1212 mix->i2c.name = "TAS3001c";
1213 chipname = "Tumbler";
1215 mix->i2c.init_client = snapper_init_client;
1216 mix->i2c.name = "TAS3004";
1217 chipname = "Snapper";
1220 if ((err = snd_pmac_keywest_init(&mix->i2c)) < 0)
1226 sprintf(chip->card->mixername, "PowerMac %s", chipname);
1228 if (chip->model == PMAC_TUMBLER) {
1229 for (i = 0; i < ARRAY_SIZE(tumbler_mixers); i++) {
1230 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&tumbler_mixers[i], chip))) < 0)
1234 for (i = 0; i < ARRAY_SIZE(snapper_mixers); i++) {
1235 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snapper_mixers[i], chip))) < 0)
1239 chip->master_sw_ctl = snd_ctl_new1(&tumbler_hp_sw, chip);
1240 if ((err = snd_ctl_add(chip->card, chip->master_sw_ctl)) < 0)
1242 chip->speaker_sw_ctl = snd_ctl_new1(&tumbler_speaker_sw, chip);
1243 if ((err = snd_ctl_add(chip->card, chip->speaker_sw_ctl)) < 0)
1245 chip->drc_sw_ctl = snd_ctl_new1(&tumbler_drc_sw, chip);
1246 if ((err = snd_ctl_add(chip->card, chip->drc_sw_ctl)) < 0)
1249 #ifdef CONFIG_PMAC_PBOOK
1250 chip->suspend = tumbler_suspend;
1251 chip->resume = tumbler_resume;
1254 INIT_WORK(&device_change, device_change_handler, (void *)chip);
1256 #ifdef PMAC_SUPPORT_AUTOMUTE
1257 if (mix->headphone_irq >=0 && (err = snd_pmac_add_automute(chip)) < 0)
1259 chip->detect_headphone = tumbler_detect_headphone;
1260 chip->update_automute = tumbler_update_automute;
1261 tumbler_update_automute(chip, 0); /* update the status only */