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 master_switch[2];
98 unsigned int mono_vol[VOL_IDX_LAST_MONO];
99 unsigned int mix_vol[VOL_IDX_LAST_MIX][2]; /* stereo volumes for tas3004 */
109 static int send_init_client(pmac_keywest_t *i2c, unsigned int *regs)
114 err = i2c_smbus_write_byte_data(i2c->client,
128 static int tumbler_init_client(pmac_keywest_t *i2c)
130 static unsigned int regs[] = {
131 /* normal operation, SCLK=64fps, i2s output, i2s input, 16bit width */
132 TAS_REG_MCS, (1<<6)|(2<<4)|(2<<2)|0,
135 return send_init_client(i2c, regs);
138 static int snapper_init_client(pmac_keywest_t *i2c)
140 static unsigned int regs[] = {
141 /* normal operation, SCLK=64fps, i2s output, 16bit width */
142 TAS_REG_MCS, (1<<6)|(2<<4)|0,
143 /* normal operation, all-pass mode */
144 TAS_REG_MCS2, (1<<1),
145 /* normal output, no deemphasis, A input, power-up, line-in */
149 return send_init_client(i2c, regs);
155 #ifdef CONFIG_PPC_HAS_FEATURE_CALLS
156 #define do_gpio_write(gp, val) \
157 pmac_call_feature(PMAC_FTR_WRITE_GPIO, NULL, (gp)->addr, val)
158 #define do_gpio_read(gp) \
159 pmac_call_feature(PMAC_FTR_READ_GPIO, NULL, (gp)->addr, 0)
160 #define tumbler_gpio_free(gp) /* NOP */
162 #define do_gpio_write(gp, val) writeb(val, (gp)->addr)
163 #define do_gpio_read(gp) readb((gp)->addr)
164 static inline void tumbler_gpio_free(pmac_gpio_t *gp)
171 #endif /* CONFIG_PPC_HAS_FEATURE_CALLS */
173 static void write_audio_gpio(pmac_gpio_t *gp, int active)
177 active = active ? gp->active_state : !gp->active_state;
178 do_gpio_write(gp, active ? 0x05 : 0x04);
181 static int read_audio_gpio(pmac_gpio_t *gp)
186 ret = ((do_gpio_read(gp) & 0x02) !=0);
187 return ret == gp->active_state;
191 * update master volume
193 static int tumbler_set_master_volume(pmac_tumbler_t *mix)
195 unsigned char block[6];
196 unsigned int left_vol, right_vol;
198 if (! mix->i2c.client)
201 if (! mix->master_switch[0])
204 left_vol = mix->master_vol[0];
205 if (left_vol >= ARRAY_SIZE(master_volume_table))
206 left_vol = ARRAY_SIZE(master_volume_table) - 1;
207 left_vol = master_volume_table[left_vol];
209 if (! mix->master_switch[1])
212 right_vol = mix->master_vol[1];
213 if (right_vol >= ARRAY_SIZE(master_volume_table))
214 right_vol = ARRAY_SIZE(master_volume_table) - 1;
215 right_vol = master_volume_table[right_vol];
218 block[0] = (left_vol >> 16) & 0xff;
219 block[1] = (left_vol >> 8) & 0xff;
220 block[2] = (left_vol >> 0) & 0xff;
222 block[3] = (right_vol >> 16) & 0xff;
223 block[4] = (right_vol >> 8) & 0xff;
224 block[5] = (right_vol >> 0) & 0xff;
226 if (i2c_smbus_write_block_data(mix->i2c.client, TAS_REG_VOL,
228 snd_printk("failed to set volume \n");
236 static int tumbler_info_master_volume(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
238 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
240 uinfo->value.integer.min = 0;
241 uinfo->value.integer.max = ARRAY_SIZE(master_volume_table) - 1;
245 static int tumbler_get_master_volume(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
247 pmac_t *chip = snd_kcontrol_chip(kcontrol);
248 pmac_tumbler_t *mix = chip->mixer_data;
249 snd_assert(mix, return -ENODEV);
250 ucontrol->value.integer.value[0] = mix->master_vol[0];
251 ucontrol->value.integer.value[1] = mix->master_vol[1];
255 static int tumbler_put_master_volume(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
257 pmac_t *chip = snd_kcontrol_chip(kcontrol);
258 pmac_tumbler_t *mix = chip->mixer_data;
261 snd_assert(mix, return -ENODEV);
262 change = mix->master_vol[0] != ucontrol->value.integer.value[0] ||
263 mix->master_vol[1] != ucontrol->value.integer.value[1];
265 mix->master_vol[0] = ucontrol->value.integer.value[0];
266 mix->master_vol[1] = ucontrol->value.integer.value[1];
267 tumbler_set_master_volume(mix);
273 static int tumbler_get_master_switch(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
275 pmac_t *chip = snd_kcontrol_chip(kcontrol);
276 pmac_tumbler_t *mix = chip->mixer_data;
277 snd_assert(mix, return -ENODEV);
278 ucontrol->value.integer.value[0] = mix->master_switch[0];
279 ucontrol->value.integer.value[1] = mix->master_switch[1];
283 static int tumbler_put_master_switch(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
285 pmac_t *chip = snd_kcontrol_chip(kcontrol);
286 pmac_tumbler_t *mix = chip->mixer_data;
289 snd_assert(mix, return -ENODEV);
290 change = mix->master_switch[0] != ucontrol->value.integer.value[0] ||
291 mix->master_switch[1] != ucontrol->value.integer.value[1];
293 mix->master_switch[0] = !!ucontrol->value.integer.value[0];
294 mix->master_switch[1] = !!ucontrol->value.integer.value[1];
295 tumbler_set_master_volume(mix);
302 * TAS3001c dynamic range compression
305 #define TAS3001_DRC_MAX 0x5f
307 static int tumbler_set_drc(pmac_tumbler_t *mix)
309 unsigned char val[2];
311 if (! mix->i2c.client)
314 if (mix->drc_enable) {
315 val[0] = 0xc1; /* enable, 3:1 compression */
316 if (mix->drc_range > TAS3001_DRC_MAX)
318 else if (mix->drc_range < 0)
321 val[1] = mix->drc_range + 0x91;
327 if (i2c_smbus_write_block_data(mix->i2c.client, TAS_REG_DRC,
329 snd_printk("failed to set DRC\n");
339 #define TAS3004_DRC_MAX 0xef
341 static int snapper_set_drc(pmac_tumbler_t *mix)
343 unsigned char val[6];
345 if (! mix->i2c.client)
349 val[0] = 0x50; /* 3:1 above threshold */
351 val[0] = 0x51; /* disabled */
352 val[1] = 0x02; /* 1:1 below threshold */
353 if (mix->drc_range > 0xef)
355 else if (mix->drc_range < 0)
358 val[2] = mix->drc_range;
363 if (i2c_smbus_write_block_data(mix->i2c.client, TAS_REG_DRC,
365 snd_printk("failed to set DRC\n");
371 static int tumbler_info_drc_value(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
373 pmac_t *chip = snd_kcontrol_chip(kcontrol);
374 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
376 uinfo->value.integer.min = 0;
377 uinfo->value.integer.max =
378 chip->model == PMAC_TUMBLER ? TAS3001_DRC_MAX : TAS3004_DRC_MAX;
382 static int tumbler_get_drc_value(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
384 pmac_t *chip = snd_kcontrol_chip(kcontrol);
386 if (! (mix = chip->mixer_data))
388 ucontrol->value.integer.value[0] = mix->drc_range;
392 static int tumbler_put_drc_value(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
394 pmac_t *chip = snd_kcontrol_chip(kcontrol);
398 if (! (mix = chip->mixer_data))
400 change = mix->drc_range != ucontrol->value.integer.value[0];
402 mix->drc_range = ucontrol->value.integer.value[0];
403 if (chip->model == PMAC_TUMBLER)
404 tumbler_set_drc(mix);
406 snapper_set_drc(mix);
411 static int tumbler_get_drc_switch(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
413 pmac_t *chip = snd_kcontrol_chip(kcontrol);
415 if (! (mix = chip->mixer_data))
417 ucontrol->value.integer.value[0] = mix->drc_enable;
421 static int tumbler_put_drc_switch(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
423 pmac_t *chip = snd_kcontrol_chip(kcontrol);
427 if (! (mix = chip->mixer_data))
429 change = mix->drc_enable != ucontrol->value.integer.value[0];
431 mix->drc_enable = !!ucontrol->value.integer.value[0];
432 if (chip->model == PMAC_TUMBLER)
433 tumbler_set_drc(mix);
435 snapper_set_drc(mix);
445 struct tumbler_mono_vol {
453 static int tumbler_set_mono_volume(pmac_tumbler_t *mix, struct tumbler_mono_vol *info)
455 unsigned char block[4];
459 if (! mix->i2c.client)
462 vol = mix->mono_vol[info->index];
463 if (vol >= info->max)
465 vol = info->table[vol];
466 for (i = 0; i < info->bytes; i++)
467 block[i] = (vol >> ((info->bytes - i - 1) * 8)) & 0xff;
468 if (i2c_smbus_write_block_data(mix->i2c.client, info->reg,
469 info->bytes, block) < 0) {
470 snd_printk("failed to set mono volume %d\n", info->index);
476 static int tumbler_info_mono(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
478 struct tumbler_mono_vol *info = (struct tumbler_mono_vol *)kcontrol->private_value;
480 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
482 uinfo->value.integer.min = 0;
483 uinfo->value.integer.max = info->max - 1;
487 static int tumbler_get_mono(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
489 struct tumbler_mono_vol *info = (struct tumbler_mono_vol *)kcontrol->private_value;
490 pmac_t *chip = snd_kcontrol_chip(kcontrol);
492 if (! (mix = chip->mixer_data))
494 ucontrol->value.integer.value[0] = mix->mono_vol[info->index];
498 static int tumbler_put_mono(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
500 struct tumbler_mono_vol *info = (struct tumbler_mono_vol *)kcontrol->private_value;
501 pmac_t *chip = snd_kcontrol_chip(kcontrol);
505 if (! (mix = chip->mixer_data))
507 change = mix->mono_vol[info->index] != ucontrol->value.integer.value[0];
509 mix->mono_vol[info->index] = ucontrol->value.integer.value[0];
510 tumbler_set_mono_volume(mix, info);
515 /* TAS3001c mono volumes */
516 static struct tumbler_mono_vol tumbler_pcm_vol_info = {
517 .index = VOL_IDX_PCM_MONO,
520 .max = ARRAY_SIZE(mixer_volume_table),
521 .table = mixer_volume_table,
524 static struct tumbler_mono_vol tumbler_bass_vol_info = {
525 .index = VOL_IDX_BASS,
528 .max = ARRAY_SIZE(bass_volume_table),
529 .table = bass_volume_table,
532 static struct tumbler_mono_vol tumbler_treble_vol_info = {
533 .index = VOL_IDX_TREBLE,
534 .reg = TAS_REG_TREBLE,
536 .max = ARRAY_SIZE(treble_volume_table),
537 .table = treble_volume_table,
540 /* TAS3004 mono volumes */
541 static struct tumbler_mono_vol snapper_bass_vol_info = {
542 .index = VOL_IDX_BASS,
545 .max = ARRAY_SIZE(snapper_bass_volume_table),
546 .table = snapper_bass_volume_table,
549 static struct tumbler_mono_vol snapper_treble_vol_info = {
550 .index = VOL_IDX_TREBLE,
551 .reg = TAS_REG_TREBLE,
553 .max = ARRAY_SIZE(snapper_treble_volume_table),
554 .table = snapper_treble_volume_table,
558 #define DEFINE_MONO(xname,type) { \
559 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,\
561 .info = tumbler_info_mono, \
562 .get = tumbler_get_mono, \
563 .put = tumbler_put_mono, \
564 .private_value = (unsigned long)(&tumbler_##type##_vol_info), \
567 #define DEFINE_SNAPPER_MONO(xname,type) { \
568 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,\
570 .info = tumbler_info_mono, \
571 .get = tumbler_get_mono, \
572 .put = tumbler_put_mono, \
573 .private_value = (unsigned long)(&snapper_##type##_vol_info), \
578 * snapper mixer volumes
581 static int snapper_set_mix_vol1(pmac_tumbler_t *mix, int idx, int ch, int reg)
584 unsigned char block[9];
586 vol = mix->mix_vol[idx][ch];
587 if (vol >= ARRAY_SIZE(mixer_volume_table)) {
588 vol = ARRAY_SIZE(mixer_volume_table) - 1;
589 mix->mix_vol[idx][ch] = vol;
592 for (i = 0; i < 3; i++) {
593 vol = mix->mix_vol[i][ch];
594 vol = mixer_volume_table[vol];
595 for (j = 0; j < 3; j++)
596 block[i * 3 + j] = (vol >> ((2 - j) * 8)) & 0xff;
598 if (i2c_smbus_write_block_data(mix->i2c.client, reg, 9, block) < 0) {
599 snd_printk("failed to set mono volume %d\n", reg);
605 static int snapper_set_mix_vol(pmac_tumbler_t *mix, int idx)
607 if (! mix->i2c.client)
609 if (snapper_set_mix_vol1(mix, idx, 0, TAS_REG_LMIX) < 0 ||
610 snapper_set_mix_vol1(mix, idx, 1, TAS_REG_RMIX) < 0)
615 static int snapper_info_mix(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
617 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
619 uinfo->value.integer.min = 0;
620 uinfo->value.integer.max = ARRAY_SIZE(mixer_volume_table) - 1;
624 static int snapper_get_mix(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
626 int idx = (int)kcontrol->private_value;
627 pmac_t *chip = snd_kcontrol_chip(kcontrol);
629 if (! (mix = chip->mixer_data))
631 ucontrol->value.integer.value[0] = mix->mix_vol[idx][0];
632 ucontrol->value.integer.value[1] = mix->mix_vol[idx][1];
636 static int snapper_put_mix(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
638 int idx = (int)kcontrol->private_value;
639 pmac_t *chip = snd_kcontrol_chip(kcontrol);
643 if (! (mix = chip->mixer_data))
645 change = mix->mix_vol[idx][0] != ucontrol->value.integer.value[0] ||
646 mix->mix_vol[idx][1] != ucontrol->value.integer.value[1];
648 mix->mix_vol[idx][0] = ucontrol->value.integer.value[0];
649 mix->mix_vol[idx][1] = ucontrol->value.integer.value[1];
650 snapper_set_mix_vol(mix, idx);
660 enum { TUMBLER_MUTE_HP, TUMBLER_MUTE_AMP };
662 static int tumbler_get_mute_switch(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
664 pmac_t *chip = snd_kcontrol_chip(kcontrol);
667 if (! (mix = chip->mixer_data))
669 gp = (kcontrol->private_value == TUMBLER_MUTE_HP) ? &mix->hp_mute : &mix->amp_mute;
670 ucontrol->value.integer.value[0] = ! read_audio_gpio(gp);
674 static int tumbler_put_mute_switch(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
676 pmac_t *chip = snd_kcontrol_chip(kcontrol);
680 #ifdef PMAC_SUPPORT_AUTOMUTE
681 if (chip->update_automute && chip->auto_mute)
682 return 0; /* don't touch in the auto-mute mode */
684 if (! (mix = chip->mixer_data))
686 gp = (kcontrol->private_value == TUMBLER_MUTE_HP) ? &mix->hp_mute : &mix->amp_mute;
687 val = ! read_audio_gpio(gp);
688 if (val != ucontrol->value.integer.value[0]) {
689 write_audio_gpio(gp, ! ucontrol->value.integer.value[0]);
695 static int snapper_set_capture_source(pmac_tumbler_t *mix)
697 if (! mix->i2c.client)
699 return i2c_smbus_write_byte_data(mix->i2c.client, TAS_REG_ACS,
700 mix->capture_source ? 2 : 0);
703 static int snapper_info_capture_source(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
705 static char *texts[2] = {
708 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
710 uinfo->value.enumerated.items = 2;
711 if (uinfo->value.enumerated.item > 1)
712 uinfo->value.enumerated.item = 1;
713 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
717 static int snapper_get_capture_source(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
719 pmac_t *chip = snd_kcontrol_chip(kcontrol);
720 pmac_tumbler_t *mix = chip->mixer_data;
722 snd_assert(mix, return -ENODEV);
723 ucontrol->value.integer.value[0] = mix->capture_source;
727 static int snapper_put_capture_source(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
729 pmac_t *chip = snd_kcontrol_chip(kcontrol);
730 pmac_tumbler_t *mix = chip->mixer_data;
733 snd_assert(mix, return -ENODEV);
734 change = ucontrol->value.integer.value[0] != mix->capture_source;
736 mix->capture_source = !!ucontrol->value.integer.value[0];
737 snapper_set_capture_source(mix);
742 #define DEFINE_SNAPPER_MIX(xname,idx,ofs) { \
743 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,\
745 .info = snapper_info_mix, \
746 .get = snapper_get_mix, \
747 .put = snapper_put_mix, \
749 .private_value = ofs, \
755 static snd_kcontrol_new_t tumbler_mixers[] __initdata = {
756 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
757 .name = "Master Playback Volume",
758 .info = tumbler_info_master_volume,
759 .get = tumbler_get_master_volume,
760 .put = tumbler_put_master_volume
762 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
763 .name = "Master Playback Switch",
764 .info = snd_pmac_boolean_stereo_info,
765 .get = tumbler_get_master_switch,
766 .put = tumbler_put_master_switch
768 DEFINE_MONO("Tone Control - Bass", bass),
769 DEFINE_MONO("Tone Control - Treble", treble),
770 DEFINE_MONO("PCM Playback Volume", pcm),
771 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
773 .info = tumbler_info_drc_value,
774 .get = tumbler_get_drc_value,
775 .put = tumbler_put_drc_value
779 static snd_kcontrol_new_t snapper_mixers[] __initdata = {
780 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
781 .name = "Master Playback Volume",
782 .info = tumbler_info_master_volume,
783 .get = tumbler_get_master_volume,
784 .put = tumbler_put_master_volume
786 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
787 .name = "Master Playback Switch",
788 .info = snd_pmac_boolean_stereo_info,
789 .get = tumbler_get_master_switch,
790 .put = tumbler_put_master_switch
792 DEFINE_SNAPPER_MIX("PCM Playback Volume", 0, VOL_IDX_PCM),
793 DEFINE_SNAPPER_MIX("PCM Playback Volume", 1, VOL_IDX_PCM2),
794 DEFINE_SNAPPER_MIX("Monitor Mix Volume", 0, VOL_IDX_ADC),
795 DEFINE_SNAPPER_MONO("Tone Control - Bass", bass),
796 DEFINE_SNAPPER_MONO("Tone Control - Treble", treble),
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,
827 static snd_kcontrol_new_t tumbler_drc_sw __initdata = {
828 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
829 .name = "DRC Switch",
830 .info = snd_pmac_boolean_mono_info,
831 .get = tumbler_get_drc_switch,
832 .put = tumbler_put_drc_switch
836 #ifdef PMAC_SUPPORT_AUTOMUTE
840 static int tumbler_detect_headphone(pmac_t *chip)
842 pmac_tumbler_t *mix = chip->mixer_data;
843 return read_audio_gpio(&mix->hp_detect);
846 static void check_mute(pmac_t *chip, pmac_gpio_t *gp, int val, int do_notify, snd_kcontrol_t *sw)
848 //pmac_tumbler_t *mix = chip->mixer_data;
849 if (val != read_audio_gpio(gp)) {
850 write_audio_gpio(gp, val);
852 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE, &sw->id);
856 static struct work_struct device_change;
859 device_change_handler(void *self)
861 pmac_t *chip = (pmac_t*) self;
867 mix = chip->mixer_data;
869 /* first set the DRC so the speaker do not explode -ReneR */
870 if (chip->model == PMAC_TUMBLER)
871 tumbler_set_drc(mix);
873 snapper_set_drc(mix);
875 /* reset the master volume so the correct amplification is applied */
876 tumbler_set_master_volume(mix);
879 static void tumbler_update_automute(pmac_t *chip, int do_notify)
881 if (chip->auto_mute) {
882 pmac_tumbler_t *mix = chip->mixer_data;
883 snd_assert(mix, return);
884 if (tumbler_detect_headphone(chip)) {
886 check_mute(chip, &mix->amp_mute, 1, do_notify, chip->speaker_sw_ctl);
887 check_mute(chip, &mix->hp_mute, 0, do_notify, chip->master_sw_ctl);
892 check_mute(chip, &mix->amp_mute, 0, do_notify, chip->speaker_sw_ctl);
893 check_mute(chip, &mix->hp_mute, 1, do_notify, chip->master_sw_ctl);
897 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
898 &chip->hp_detect_ctl->id);
899 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
900 &chip->drc_sw_ctl->id);
903 /* finally we need to schedule an update of the mixer values
904 (master and DRC are enough for now) -ReneR */
905 schedule_work(&device_change);
909 #endif /* PMAC_SUPPORT_AUTOMUTE */
912 /* interrupt - headphone plug changed */
913 static irqreturn_t headphone_intr(int irq, void *devid, struct pt_regs *regs)
915 pmac_t *chip = devid;
916 if (chip->update_automute && chip->initialized) {
917 chip->update_automute(chip, 1);
923 /* look for audio-gpio device */
924 static struct device_node *find_audio_device(const char *name)
926 struct device_node *np;
928 if (! (np = find_devices("gpio")))
931 for (np = np->child; np; np = np->sibling) {
932 char *property = get_property(np, "audio-gpio", NULL);
933 if (property && strcmp(property, name) == 0)
939 /* look for audio-gpio device */
940 static struct device_node *find_compatible_audio_device(const char *name)
942 struct device_node *np;
944 if (! (np = find_devices("gpio")))
947 for (np = np->child; np; np = np->sibling) {
948 if (device_is_compatible(np, name))
954 /* find an audio device and get its address */
955 static long tumbler_find_device(const char *device, pmac_gpio_t *gp, int is_compatible)
957 struct device_node *node;
961 node = find_compatible_audio_device(device);
963 node = find_audio_device(device);
965 snd_printdd("cannot find device %s\n", device);
969 base = (u32 *)get_property(node, "AAPL,address", NULL);
971 base = (u32 *)get_property(node, "reg", NULL);
973 snd_printd("cannot find address for device %s\n", device);
976 /* this only work if PPC_HAS_FEATURE_CALLS is set as we
977 * are only getting the low part of the address
985 #ifdef CONFIG_PPC_HAS_FEATURE_CALLS
986 gp->addr = addr & 0x0000ffff;
988 gp->addr = ioremap((unsigned long)addr, 1);
990 /* Try to find the active state, default to 0 ! */
991 base = (u32 *)get_property(node, "audio-gpio-active-state", NULL);
993 gp->active_state = *base;
995 gp->active_state = 0;
997 return (node->n_intrs > 0) ? node->intrs[0].line : 0;
1001 static void tumbler_reset_audio(pmac_t *chip)
1003 pmac_tumbler_t *mix = chip->mixer_data;
1005 write_audio_gpio(&mix->audio_reset, 0);
1007 write_audio_gpio(&mix->audio_reset, 1);
1009 write_audio_gpio(&mix->audio_reset, 0);
1013 #ifdef CONFIG_PMAC_PBOOK
1015 static void tumbler_resume(pmac_t *chip)
1017 pmac_tumbler_t *mix = chip->mixer_data;
1019 snd_assert(mix, return);
1021 tumbler_reset_audio(chip);
1022 if (mix->i2c.client && mix->i2c.init_client) {
1023 if (mix->i2c.init_client(&mix->i2c) < 0)
1024 printk(KERN_ERR "tumbler_init_client error\n");
1026 printk(KERN_ERR "tumbler: i2c is not initialized\n");
1027 if (chip->model == PMAC_TUMBLER) {
1028 tumbler_set_mono_volume(mix, &tumbler_pcm_vol_info);
1029 tumbler_set_mono_volume(mix, &tumbler_bass_vol_info);
1030 tumbler_set_mono_volume(mix, &tumbler_treble_vol_info);
1031 tumbler_set_drc(mix);
1033 snapper_set_mix_vol(mix, VOL_IDX_PCM);
1034 snapper_set_mix_vol(mix, VOL_IDX_PCM2);
1035 snapper_set_mix_vol(mix, VOL_IDX_ADC);
1036 tumbler_set_mono_volume(mix, &snapper_bass_vol_info);
1037 tumbler_set_mono_volume(mix, &snapper_treble_vol_info);
1038 snapper_set_drc(mix);
1039 snapper_set_capture_source(mix);
1041 tumbler_set_master_volume(mix);
1042 if (chip->update_automute)
1043 chip->update_automute(chip, 0);
1047 /* initialize tumbler */
1048 static int __init tumbler_init(pmac_t *chip)
1051 pmac_tumbler_t *mix = chip->mixer_data;
1052 snd_assert(mix, return -EINVAL);
1054 if (tumbler_find_device("audio-hw-reset", &mix->audio_reset, 0) < 0)
1055 tumbler_find_device("hw-reset", &mix->audio_reset, 1);
1056 if (tumbler_find_device("amp-mute", &mix->amp_mute, 0) < 0)
1057 tumbler_find_device("amp-mute", &mix->amp_mute, 1);
1058 if (tumbler_find_device("headphone-mute", &mix->hp_mute, 0) < 0)
1059 tumbler_find_device("headphone-mute", &mix->hp_mute, 1);
1060 irq = tumbler_find_device("headphone-detect", &mix->hp_detect, 0);
1062 irq = tumbler_find_device("headphone-detect", &mix->hp_detect, 1);
1064 irq = tumbler_find_device("keywest-gpio15", &mix->hp_detect, 1);
1066 tumbler_reset_audio(chip);
1068 /* activate headphone status interrupts */
1071 if ((err = request_irq(irq, headphone_intr, 0,
1072 "Tumbler Headphone Detection", chip)) < 0)
1074 /* activate headphone status interrupts */
1075 val = do_gpio_read(&mix->hp_detect);
1076 do_gpio_write(&mix->hp_detect, val | 0x80);
1078 mix->headphone_irq = irq;
1083 static void tumbler_cleanup(pmac_t *chip)
1085 pmac_tumbler_t *mix = chip->mixer_data;
1089 if (mix->headphone_irq >= 0)
1090 free_irq(mix->headphone_irq, chip);
1091 tumbler_gpio_free(&mix->audio_reset);
1092 tumbler_gpio_free(&mix->amp_mute);
1093 tumbler_gpio_free(&mix->hp_mute);
1094 tumbler_gpio_free(&mix->hp_detect);
1095 snd_pmac_keywest_cleanup(&mix->i2c);
1097 chip->mixer_data = NULL;
1101 int __init snd_pmac_tumbler_init(pmac_t *chip)
1104 pmac_tumbler_t *mix;
1106 struct device_node *tas_node;
1110 if (current->fs->root)
1111 request_module("i2c-keywest");
1112 #endif /* CONFIG_KMOD */
1114 mix = kmalloc(sizeof(*mix), GFP_KERNEL);
1117 memset(mix, 0, sizeof(*mix));
1118 mix->headphone_irq = -1;
1120 chip->mixer_data = mix;
1121 chip->mixer_free = tumbler_cleanup;
1123 if ((err = tumbler_init(chip)) < 0)
1127 tas_node = find_devices("deq");
1128 if (tas_node == NULL)
1129 tas_node = find_devices("codec");
1130 if (tas_node == NULL)
1133 paddr = (u32 *)get_property(tas_node, "i2c-address", NULL);
1135 paddr = (u32 *)get_property(tas_node, "reg", NULL);
1137 mix->i2c.addr = (*paddr) >> 1;
1139 mix->i2c.addr = TAS_I2C_ADDR;
1141 if (chip->model == PMAC_TUMBLER) {
1142 mix->i2c.init_client = tumbler_init_client;
1143 mix->i2c.name = "TAS3001c";
1144 chipname = "Tumbler";
1146 mix->i2c.init_client = snapper_init_client;
1147 mix->i2c.name = "TAS3004";
1148 chipname = "Snapper";
1151 if ((err = snd_pmac_keywest_init(&mix->i2c)) < 0)
1157 sprintf(chip->card->mixername, "PowerMac %s", chipname);
1159 if (chip->model == PMAC_TUMBLER) {
1160 for (i = 0; i < ARRAY_SIZE(tumbler_mixers); i++) {
1161 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&tumbler_mixers[i], chip))) < 0)
1165 for (i = 0; i < ARRAY_SIZE(snapper_mixers); i++) {
1166 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snapper_mixers[i], chip))) < 0)
1170 chip->master_sw_ctl = snd_ctl_new1(&tumbler_hp_sw, chip);
1171 if ((err = snd_ctl_add(chip->card, chip->master_sw_ctl)) < 0)
1173 chip->speaker_sw_ctl = snd_ctl_new1(&tumbler_speaker_sw, chip);
1174 if ((err = snd_ctl_add(chip->card, chip->speaker_sw_ctl)) < 0)
1176 chip->drc_sw_ctl = snd_ctl_new1(&tumbler_drc_sw, chip);
1177 if ((err = snd_ctl_add(chip->card, chip->drc_sw_ctl)) < 0)
1180 #ifdef CONFIG_PMAC_PBOOK
1181 chip->resume = tumbler_resume;
1184 INIT_WORK(&device_change, device_change_handler, (void *)chip);
1186 #ifdef PMAC_SUPPORT_AUTOMUTE
1187 if (mix->headphone_irq >=0 && (err = snd_pmac_add_automute(chip)) < 0)
1189 chip->detect_headphone = tumbler_detect_headphone;
1190 chip->update_automute = tumbler_update_automute;
1191 tumbler_update_automute(chip, 0); /* update the status only */