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 #include <asm/machdep.h>
39 #include <asm/pmac_feature.h>
41 #include "tumbler_volume.h"
46 #define DBG(fmt...) printk(fmt)
51 /* i2c address for tumbler */
52 #define TAS_I2C_ADDR 0x34
55 #define TAS_REG_MCS 0x01 /* main control */
56 #define TAS_REG_DRC 0x02
57 #define TAS_REG_VOL 0x04
58 #define TAS_REG_TREBLE 0x05
59 #define TAS_REG_BASS 0x06
60 #define TAS_REG_INPUT1 0x07
61 #define TAS_REG_INPUT2 0x08
64 #define TAS_REG_PCM TAS_REG_INPUT1
67 #define TAS_REG_LMIX TAS_REG_INPUT1
68 #define TAS_REG_RMIX TAS_REG_INPUT2
69 #define TAS_REG_MCS2 0x43 /* main control 2 */
70 #define TAS_REG_ACS 0x40 /* analog control */
72 /* mono volumes for tas3001c/tas3004 */
74 VOL_IDX_PCM_MONO, /* tas3001c only */
75 VOL_IDX_BASS, VOL_IDX_TREBLE,
79 /* stereo volumes for tas3004 */
81 VOL_IDX_PCM, VOL_IDX_PCM2, VOL_IDX_ADC,
85 typedef struct pmac_gpio {
92 typedef struct pmac_tumbler_t {
94 pmac_gpio_t audio_reset;
96 pmac_gpio_t line_mute;
97 pmac_gpio_t line_detect;
99 pmac_gpio_t hp_detect;
102 unsigned int master_vol[2];
103 unsigned int save_master_switch[2];
104 unsigned int master_switch[2];
105 unsigned int mono_vol[VOL_IDX_LAST_MONO];
106 unsigned int mix_vol[VOL_IDX_LAST_MIX][2]; /* stereo volumes for tas3004 */
111 int auto_mute_notify;
120 static int send_init_client(pmac_keywest_t *i2c, unsigned int *regs)
125 err = i2c_smbus_write_byte_data(i2c->client,
129 DBG("(W) i2c error %d\n", err);
140 static int tumbler_init_client(pmac_keywest_t *i2c)
142 static unsigned int regs[] = {
143 /* normal operation, SCLK=64fps, i2s output, i2s input, 16bit width */
144 TAS_REG_MCS, (1<<6)|(2<<4)|(2<<2)|0,
147 DBG("(I) tumbler init client\n");
148 return send_init_client(i2c, regs);
151 static int snapper_init_client(pmac_keywest_t *i2c)
153 static unsigned int regs[] = {
154 /* normal operation, SCLK=64fps, i2s output, 16bit width */
155 TAS_REG_MCS, (1<<6)|(2<<4)|0,
156 /* normal operation, all-pass mode */
157 TAS_REG_MCS2, (1<<1),
158 /* normal output, no deemphasis, A input, power-up, line-in */
162 DBG("(I) snapper init client\n");
163 return send_init_client(i2c, regs);
169 #define do_gpio_write(gp, val) \
170 pmac_call_feature(PMAC_FTR_WRITE_GPIO, NULL, (gp)->addr, val)
171 #define do_gpio_read(gp) \
172 pmac_call_feature(PMAC_FTR_READ_GPIO, NULL, (gp)->addr, 0)
173 #define tumbler_gpio_free(gp) /* NOP */
175 static void write_audio_gpio(pmac_gpio_t *gp, int active)
179 active = active ? gp->active_val : gp->inactive_val;
180 do_gpio_write(gp, active);
181 DBG("(I) gpio %x write %d\n", gp->addr, active);
184 static int check_audio_gpio(pmac_gpio_t *gp)
191 ret = do_gpio_read(gp);
193 return (ret & 0xd) == (gp->active_val & 0xd);
196 static int read_audio_gpio(pmac_gpio_t *gp)
201 ret = ((do_gpio_read(gp) & 0x02) !=0);
202 return ret == gp->active_state;
206 * update master volume
208 static int tumbler_set_master_volume(pmac_tumbler_t *mix)
210 unsigned char block[6];
211 unsigned int left_vol, right_vol;
213 if (! mix->i2c.client)
216 if (! mix->master_switch[0])
219 left_vol = mix->master_vol[0];
220 if (left_vol >= ARRAY_SIZE(master_volume_table))
221 left_vol = ARRAY_SIZE(master_volume_table) - 1;
222 left_vol = master_volume_table[left_vol];
224 if (! mix->master_switch[1])
227 right_vol = mix->master_vol[1];
228 if (right_vol >= ARRAY_SIZE(master_volume_table))
229 right_vol = ARRAY_SIZE(master_volume_table) - 1;
230 right_vol = master_volume_table[right_vol];
233 block[0] = (left_vol >> 16) & 0xff;
234 block[1] = (left_vol >> 8) & 0xff;
235 block[2] = (left_vol >> 0) & 0xff;
237 block[3] = (right_vol >> 16) & 0xff;
238 block[4] = (right_vol >> 8) & 0xff;
239 block[5] = (right_vol >> 0) & 0xff;
241 if (i2c_smbus_write_block_data(mix->i2c.client, TAS_REG_VOL,
243 snd_printk("failed to set volume \n");
251 static int tumbler_info_master_volume(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
253 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
255 uinfo->value.integer.min = 0;
256 uinfo->value.integer.max = ARRAY_SIZE(master_volume_table) - 1;
260 static int tumbler_get_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;
264 snd_assert(mix, return -ENODEV);
265 ucontrol->value.integer.value[0] = mix->master_vol[0];
266 ucontrol->value.integer.value[1] = mix->master_vol[1];
270 static int tumbler_put_master_volume(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
272 pmac_t *chip = snd_kcontrol_chip(kcontrol);
273 pmac_tumbler_t *mix = chip->mixer_data;
276 snd_assert(mix, return -ENODEV);
277 change = mix->master_vol[0] != ucontrol->value.integer.value[0] ||
278 mix->master_vol[1] != ucontrol->value.integer.value[1];
280 mix->master_vol[0] = ucontrol->value.integer.value[0];
281 mix->master_vol[1] = ucontrol->value.integer.value[1];
282 tumbler_set_master_volume(mix);
288 static int tumbler_get_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;
292 snd_assert(mix, return -ENODEV);
293 ucontrol->value.integer.value[0] = mix->master_switch[0];
294 ucontrol->value.integer.value[1] = mix->master_switch[1];
298 static int tumbler_put_master_switch(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
300 pmac_t *chip = snd_kcontrol_chip(kcontrol);
301 pmac_tumbler_t *mix = chip->mixer_data;
304 snd_assert(mix, return -ENODEV);
305 change = mix->master_switch[0] != ucontrol->value.integer.value[0] ||
306 mix->master_switch[1] != ucontrol->value.integer.value[1];
308 mix->master_switch[0] = !!ucontrol->value.integer.value[0];
309 mix->master_switch[1] = !!ucontrol->value.integer.value[1];
310 tumbler_set_master_volume(mix);
317 * TAS3001c dynamic range compression
320 #define TAS3001_DRC_MAX 0x5f
322 static int tumbler_set_drc(pmac_tumbler_t *mix)
324 unsigned char val[2];
326 if (! mix->i2c.client)
329 if (mix->drc_enable) {
330 val[0] = 0xc1; /* enable, 3:1 compression */
331 if (mix->drc_range > TAS3001_DRC_MAX)
333 else if (mix->drc_range < 0)
336 val[1] = mix->drc_range + 0x91;
342 if (i2c_smbus_write_block_data(mix->i2c.client, TAS_REG_DRC,
344 snd_printk("failed to set DRC\n");
354 #define TAS3004_DRC_MAX 0xef
356 static int snapper_set_drc(pmac_tumbler_t *mix)
358 unsigned char val[6];
360 if (! mix->i2c.client)
364 val[0] = 0x50; /* 3:1 above threshold */
366 val[0] = 0x51; /* disabled */
367 val[1] = 0x02; /* 1:1 below threshold */
368 if (mix->drc_range > 0xef)
370 else if (mix->drc_range < 0)
373 val[2] = mix->drc_range;
378 if (i2c_smbus_write_block_data(mix->i2c.client, TAS_REG_DRC,
380 snd_printk("failed to set DRC\n");
386 static int tumbler_info_drc_value(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
388 pmac_t *chip = snd_kcontrol_chip(kcontrol);
389 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
391 uinfo->value.integer.min = 0;
392 uinfo->value.integer.max =
393 chip->model == PMAC_TUMBLER ? TAS3001_DRC_MAX : TAS3004_DRC_MAX;
397 static int tumbler_get_drc_value(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
399 pmac_t *chip = snd_kcontrol_chip(kcontrol);
401 if (! (mix = chip->mixer_data))
403 ucontrol->value.integer.value[0] = mix->drc_range;
407 static int tumbler_put_drc_value(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
409 pmac_t *chip = snd_kcontrol_chip(kcontrol);
413 if (! (mix = chip->mixer_data))
415 change = mix->drc_range != ucontrol->value.integer.value[0];
417 mix->drc_range = ucontrol->value.integer.value[0];
418 if (chip->model == PMAC_TUMBLER)
419 tumbler_set_drc(mix);
421 snapper_set_drc(mix);
426 static int tumbler_get_drc_switch(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
428 pmac_t *chip = snd_kcontrol_chip(kcontrol);
430 if (! (mix = chip->mixer_data))
432 ucontrol->value.integer.value[0] = mix->drc_enable;
436 static int tumbler_put_drc_switch(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
438 pmac_t *chip = snd_kcontrol_chip(kcontrol);
442 if (! (mix = chip->mixer_data))
444 change = mix->drc_enable != ucontrol->value.integer.value[0];
446 mix->drc_enable = !!ucontrol->value.integer.value[0];
447 if (chip->model == PMAC_TUMBLER)
448 tumbler_set_drc(mix);
450 snapper_set_drc(mix);
460 struct tumbler_mono_vol {
468 static int tumbler_set_mono_volume(pmac_tumbler_t *mix, struct tumbler_mono_vol *info)
470 unsigned char block[4];
474 if (! mix->i2c.client)
477 vol = mix->mono_vol[info->index];
478 if (vol >= info->max)
480 vol = info->table[vol];
481 for (i = 0; i < info->bytes; i++)
482 block[i] = (vol >> ((info->bytes - i - 1) * 8)) & 0xff;
483 if (i2c_smbus_write_block_data(mix->i2c.client, info->reg,
484 info->bytes, block) < 0) {
485 snd_printk("failed to set mono volume %d\n", info->index);
491 static int tumbler_info_mono(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
493 struct tumbler_mono_vol *info = (struct tumbler_mono_vol *)kcontrol->private_value;
495 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
497 uinfo->value.integer.min = 0;
498 uinfo->value.integer.max = info->max - 1;
502 static int tumbler_get_mono(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
504 struct tumbler_mono_vol *info = (struct tumbler_mono_vol *)kcontrol->private_value;
505 pmac_t *chip = snd_kcontrol_chip(kcontrol);
507 if (! (mix = chip->mixer_data))
509 ucontrol->value.integer.value[0] = mix->mono_vol[info->index];
513 static int tumbler_put_mono(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
515 struct tumbler_mono_vol *info = (struct tumbler_mono_vol *)kcontrol->private_value;
516 pmac_t *chip = snd_kcontrol_chip(kcontrol);
520 if (! (mix = chip->mixer_data))
522 change = mix->mono_vol[info->index] != ucontrol->value.integer.value[0];
524 mix->mono_vol[info->index] = ucontrol->value.integer.value[0];
525 tumbler_set_mono_volume(mix, info);
530 /* TAS3001c mono volumes */
531 static struct tumbler_mono_vol tumbler_pcm_vol_info = {
532 .index = VOL_IDX_PCM_MONO,
535 .max = ARRAY_SIZE(mixer_volume_table),
536 .table = mixer_volume_table,
539 static struct tumbler_mono_vol tumbler_bass_vol_info = {
540 .index = VOL_IDX_BASS,
543 .max = ARRAY_SIZE(bass_volume_table),
544 .table = bass_volume_table,
547 static struct tumbler_mono_vol tumbler_treble_vol_info = {
548 .index = VOL_IDX_TREBLE,
549 .reg = TAS_REG_TREBLE,
551 .max = ARRAY_SIZE(treble_volume_table),
552 .table = treble_volume_table,
555 /* TAS3004 mono volumes */
556 static struct tumbler_mono_vol snapper_bass_vol_info = {
557 .index = VOL_IDX_BASS,
560 .max = ARRAY_SIZE(snapper_bass_volume_table),
561 .table = snapper_bass_volume_table,
564 static struct tumbler_mono_vol snapper_treble_vol_info = {
565 .index = VOL_IDX_TREBLE,
566 .reg = TAS_REG_TREBLE,
568 .max = ARRAY_SIZE(snapper_treble_volume_table),
569 .table = snapper_treble_volume_table,
573 #define DEFINE_MONO(xname,type) { \
574 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,\
576 .info = tumbler_info_mono, \
577 .get = tumbler_get_mono, \
578 .put = tumbler_put_mono, \
579 .private_value = (unsigned long)(&tumbler_##type##_vol_info), \
582 #define DEFINE_SNAPPER_MONO(xname,type) { \
583 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,\
585 .info = tumbler_info_mono, \
586 .get = tumbler_get_mono, \
587 .put = tumbler_put_mono, \
588 .private_value = (unsigned long)(&snapper_##type##_vol_info), \
593 * snapper mixer volumes
596 static int snapper_set_mix_vol1(pmac_tumbler_t *mix, int idx, int ch, int reg)
599 unsigned char block[9];
601 vol = mix->mix_vol[idx][ch];
602 if (vol >= ARRAY_SIZE(mixer_volume_table)) {
603 vol = ARRAY_SIZE(mixer_volume_table) - 1;
604 mix->mix_vol[idx][ch] = vol;
607 for (i = 0; i < 3; i++) {
608 vol = mix->mix_vol[i][ch];
609 vol = mixer_volume_table[vol];
610 for (j = 0; j < 3; j++)
611 block[i * 3 + j] = (vol >> ((2 - j) * 8)) & 0xff;
613 if (i2c_smbus_write_block_data(mix->i2c.client, reg, 9, block) < 0) {
614 snd_printk("failed to set mono volume %d\n", reg);
620 static int snapper_set_mix_vol(pmac_tumbler_t *mix, int idx)
622 if (! mix->i2c.client)
624 if (snapper_set_mix_vol1(mix, idx, 0, TAS_REG_LMIX) < 0 ||
625 snapper_set_mix_vol1(mix, idx, 1, TAS_REG_RMIX) < 0)
630 static int snapper_info_mix(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
632 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
634 uinfo->value.integer.min = 0;
635 uinfo->value.integer.max = ARRAY_SIZE(mixer_volume_table) - 1;
639 static int snapper_get_mix(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
641 int idx = (int)kcontrol->private_value;
642 pmac_t *chip = snd_kcontrol_chip(kcontrol);
644 if (! (mix = chip->mixer_data))
646 ucontrol->value.integer.value[0] = mix->mix_vol[idx][0];
647 ucontrol->value.integer.value[1] = mix->mix_vol[idx][1];
651 static int snapper_put_mix(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
653 int idx = (int)kcontrol->private_value;
654 pmac_t *chip = snd_kcontrol_chip(kcontrol);
658 if (! (mix = chip->mixer_data))
660 change = mix->mix_vol[idx][0] != ucontrol->value.integer.value[0] ||
661 mix->mix_vol[idx][1] != ucontrol->value.integer.value[1];
663 mix->mix_vol[idx][0] = ucontrol->value.integer.value[0];
664 mix->mix_vol[idx][1] = ucontrol->value.integer.value[1];
665 snapper_set_mix_vol(mix, idx);
672 * mute switches. FIXME: Turn that into software mute when both outputs are muted
673 * to avoid codec reset on ibook M7
676 enum { TUMBLER_MUTE_HP, TUMBLER_MUTE_AMP, TUMBLER_MUTE_LINE };
678 static int tumbler_get_mute_switch(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
680 pmac_t *chip = snd_kcontrol_chip(kcontrol);
683 if (! (mix = chip->mixer_data))
685 switch(kcontrol->private_value) {
686 case TUMBLER_MUTE_HP:
687 gp = &mix->hp_mute; break;
688 case TUMBLER_MUTE_AMP:
689 gp = &mix->amp_mute; break;
690 case TUMBLER_MUTE_LINE:
691 gp = &mix->line_mute; break;
697 ucontrol->value.integer.value[0] = !check_audio_gpio(gp);
701 static int tumbler_put_mute_switch(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
703 pmac_t *chip = snd_kcontrol_chip(kcontrol);
707 #ifdef PMAC_SUPPORT_AUTOMUTE
708 if (chip->update_automute && chip->auto_mute)
709 return 0; /* don't touch in the auto-mute mode */
711 if (! (mix = chip->mixer_data))
713 switch(kcontrol->private_value) {
714 case TUMBLER_MUTE_HP:
715 gp = &mix->hp_mute; break;
716 case TUMBLER_MUTE_AMP:
717 gp = &mix->amp_mute; break;
718 case TUMBLER_MUTE_LINE:
719 gp = &mix->line_mute; break;
725 val = ! check_audio_gpio(gp);
726 if (val != ucontrol->value.integer.value[0]) {
727 write_audio_gpio(gp, ! ucontrol->value.integer.value[0]);
733 static int snapper_set_capture_source(pmac_tumbler_t *mix)
735 if (! mix->i2c.client)
737 if (mix->capture_source)
738 mix->acs = mix->acs |= 2;
741 return i2c_smbus_write_byte_data(mix->i2c.client, TAS_REG_ACS, mix->acs);
744 static int snapper_info_capture_source(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
746 static char *texts[2] = {
749 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
751 uinfo->value.enumerated.items = 2;
752 if (uinfo->value.enumerated.item > 1)
753 uinfo->value.enumerated.item = 1;
754 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
758 static int snapper_get_capture_source(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
760 pmac_t *chip = snd_kcontrol_chip(kcontrol);
761 pmac_tumbler_t *mix = chip->mixer_data;
763 snd_assert(mix, return -ENODEV);
764 ucontrol->value.integer.value[0] = mix->capture_source;
768 static int snapper_put_capture_source(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
770 pmac_t *chip = snd_kcontrol_chip(kcontrol);
771 pmac_tumbler_t *mix = chip->mixer_data;
774 snd_assert(mix, return -ENODEV);
775 change = ucontrol->value.integer.value[0] != mix->capture_source;
777 mix->capture_source = !!ucontrol->value.integer.value[0];
778 snapper_set_capture_source(mix);
783 #define DEFINE_SNAPPER_MIX(xname,idx,ofs) { \
784 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,\
786 .info = snapper_info_mix, \
787 .get = snapper_get_mix, \
788 .put = snapper_put_mix, \
790 .private_value = ofs, \
796 static snd_kcontrol_new_t tumbler_mixers[] __initdata = {
797 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
798 .name = "Master Playback Volume",
799 .info = tumbler_info_master_volume,
800 .get = tumbler_get_master_volume,
801 .put = tumbler_put_master_volume
803 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
804 .name = "Master Playback Switch",
805 .info = snd_pmac_boolean_stereo_info,
806 .get = tumbler_get_master_switch,
807 .put = tumbler_put_master_switch
809 DEFINE_MONO("Tone Control - Bass", bass),
810 DEFINE_MONO("Tone Control - Treble", treble),
811 DEFINE_MONO("PCM Playback Volume", pcm),
812 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
814 .info = tumbler_info_drc_value,
815 .get = tumbler_get_drc_value,
816 .put = tumbler_put_drc_value
820 static snd_kcontrol_new_t snapper_mixers[] __initdata = {
821 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
822 .name = "Master Playback Volume",
823 .info = tumbler_info_master_volume,
824 .get = tumbler_get_master_volume,
825 .put = tumbler_put_master_volume
827 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
828 .name = "Master Playback Switch",
829 .info = snd_pmac_boolean_stereo_info,
830 .get = tumbler_get_master_switch,
831 .put = tumbler_put_master_switch
833 DEFINE_SNAPPER_MIX("PCM Playback Volume", 0, VOL_IDX_PCM),
834 DEFINE_SNAPPER_MIX("PCM Playback Volume", 1, VOL_IDX_PCM2),
835 DEFINE_SNAPPER_MIX("Monitor Mix Volume", 0, VOL_IDX_ADC),
836 DEFINE_SNAPPER_MONO("Tone Control - Bass", bass),
837 DEFINE_SNAPPER_MONO("Tone Control - Treble", treble),
838 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
840 .info = tumbler_info_drc_value,
841 .get = tumbler_get_drc_value,
842 .put = tumbler_put_drc_value
844 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
845 .name = "Input Source", /* FIXME: "Capture Source" doesn't work properly */
846 .info = snapper_info_capture_source,
847 .get = snapper_get_capture_source,
848 .put = snapper_put_capture_source
852 static snd_kcontrol_new_t tumbler_hp_sw __initdata = {
853 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
854 .name = "Headphone Playback Switch",
855 .info = snd_pmac_boolean_mono_info,
856 .get = tumbler_get_mute_switch,
857 .put = tumbler_put_mute_switch,
858 .private_value = TUMBLER_MUTE_HP,
860 static snd_kcontrol_new_t tumbler_speaker_sw __initdata = {
861 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
862 .name = "PC Speaker Playback Switch",
863 .info = snd_pmac_boolean_mono_info,
864 .get = tumbler_get_mute_switch,
865 .put = tumbler_put_mute_switch,
866 .private_value = TUMBLER_MUTE_AMP,
868 static snd_kcontrol_new_t tumbler_lineout_sw __initdata = {
869 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
870 .name = "Line Out Playback Switch",
871 .info = snd_pmac_boolean_mono_info,
872 .get = tumbler_get_mute_switch,
873 .put = tumbler_put_mute_switch,
874 .private_value = TUMBLER_MUTE_LINE,
876 static snd_kcontrol_new_t tumbler_drc_sw __initdata = {
877 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
878 .name = "DRC Switch",
879 .info = snd_pmac_boolean_mono_info,
880 .get = tumbler_get_drc_switch,
881 .put = tumbler_put_drc_switch
885 #ifdef PMAC_SUPPORT_AUTOMUTE
889 static int tumbler_detect_headphone(pmac_t *chip)
891 pmac_tumbler_t *mix = chip->mixer_data;
894 if (mix->hp_detect.addr)
895 detect |= read_audio_gpio(&mix->hp_detect);
899 static int tumbler_detect_lineout(pmac_t *chip)
901 pmac_tumbler_t *mix = chip->mixer_data;
904 if (mix->line_detect.addr)
905 detect |= read_audio_gpio(&mix->line_detect);
909 static void check_mute(pmac_t *chip, pmac_gpio_t *gp, int val, int do_notify, snd_kcontrol_t *sw)
911 if (check_audio_gpio(gp) != val) {
912 write_audio_gpio(gp, val);
914 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
919 static struct work_struct device_change;
921 static void device_change_handler(void *self)
923 pmac_t *chip = (pmac_t*) self;
925 int headphone, lineout;
930 mix = chip->mixer_data;
931 snd_assert(mix, return);
933 headphone = tumbler_detect_headphone(chip);
934 lineout = tumbler_detect_lineout(chip);
936 DBG("headphone: %d, lineout: %d\n", headphone, lineout);
938 if (headphone || lineout) {
939 /* unmute headphone/lineout & mute speaker */
941 check_mute(chip, &mix->hp_mute, 0, mix->auto_mute_notify,
942 chip->master_sw_ctl);
943 if (lineout && mix->line_mute.addr != 0)
944 check_mute(chip, &mix->line_mute, 0, mix->auto_mute_notify,
945 chip->lineout_sw_ctl);
946 if (mix->anded_reset)
948 check_mute(chip, &mix->amp_mute, 1, mix->auto_mute_notify,
949 chip->speaker_sw_ctl);
952 /* unmute speaker, mute others */
953 check_mute(chip, &mix->amp_mute, 0, mix->auto_mute_notify,
954 chip->speaker_sw_ctl);
955 if (mix->anded_reset)
957 check_mute(chip, &mix->hp_mute, 1, mix->auto_mute_notify,
958 chip->master_sw_ctl);
959 if (mix->line_mute.addr != 0)
960 check_mute(chip, &mix->line_mute, 1, mix->auto_mute_notify,
961 chip->lineout_sw_ctl);
964 if (mix->auto_mute_notify) {
965 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
966 &chip->hp_detect_ctl->id);
967 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
968 &chip->drc_sw_ctl->id);
971 /* first set the DRC so the speaker do not explode -ReneR */
972 if (chip->model == PMAC_TUMBLER)
973 tumbler_set_drc(mix);
975 snapper_set_drc(mix);
977 /* reset the master volume so the correct amplification is applied */
978 tumbler_set_master_volume(mix);
981 static void tumbler_update_automute(pmac_t *chip, int do_notify)
983 if (chip->auto_mute) {
985 mix = chip->mixer_data;
986 snd_assert(mix, return);
987 mix->auto_mute_notify = do_notify;
988 schedule_work(&device_change);
991 #endif /* PMAC_SUPPORT_AUTOMUTE */
994 /* interrupt - headphone plug changed */
995 static irqreturn_t headphone_intr(int irq, void *devid, struct pt_regs *regs)
997 pmac_t *chip = devid;
998 if (chip->update_automute && chip->initialized) {
999 chip->update_automute(chip, 1);
1005 /* look for audio-gpio device */
1006 static struct device_node *find_audio_device(const char *name)
1008 struct device_node *np;
1010 if (! (np = find_devices("gpio")))
1013 for (np = np->child; np; np = np->sibling) {
1014 char *property = get_property(np, "audio-gpio", NULL);
1015 if (property && strcmp(property, name) == 0)
1021 /* look for audio-gpio device */
1022 static struct device_node *find_compatible_audio_device(const char *name)
1024 struct device_node *np;
1026 if (! (np = find_devices("gpio")))
1029 for (np = np->child; np; np = np->sibling) {
1030 if (device_is_compatible(np, name))
1036 /* find an audio device and get its address */
1037 static long tumbler_find_device(const char *device, const char *platform, pmac_gpio_t *gp, int is_compatible)
1039 struct device_node *node;
1043 node = find_compatible_audio_device(device);
1045 node = find_audio_device(device);
1047 DBG("(W) cannot find audio device %s !\n", device);
1048 snd_printdd("cannot find device %s\n", device);
1052 base = (u32 *)get_property(node, "AAPL,address", NULL);
1054 base = (u32 *)get_property(node, "reg", NULL);
1056 DBG("(E) cannot find address for device %s !\n", device);
1057 snd_printd("cannot find address for device %s\n", device);
1066 gp->addr = addr & 0x0000ffff;
1067 /* Try to find the active state, default to 0 ! */
1068 base = (u32 *)get_property(node, "audio-gpio-active-state", NULL);
1070 gp->active_state = *base;
1071 gp->active_val = (*base) ? 0x5 : 0x4;
1072 gp->inactive_val = (*base) ? 0x4 : 0x5;
1075 gp->active_state = 0;
1076 gp->active_val = 0x4;
1077 gp->inactive_val = 0x5;
1078 /* Here are some crude hacks to extract the GPIO polarity and
1079 * open collector informations out of the do-platform script
1080 * as we don't yet have an interpreter for these things
1083 prop = (u32 *)get_property(node, platform, NULL);
1085 if (prop[3] == 0x9 && prop[4] == 0x9) {
1086 gp->active_val = 0xd;
1087 gp->inactive_val = 0xc;
1089 if (prop[3] == 0x1 && prop[4] == 0x1) {
1090 gp->active_val = 0x5;
1091 gp->inactive_val = 0x4;
1096 DBG("(I) GPIO device %s found, offset: %x, active state: %d !\n",
1097 device, gp->addr, gp->active_state);
1099 return (node->n_intrs > 0) ? node->intrs[0].line : 0;
1103 static void tumbler_reset_audio(pmac_t *chip)
1105 pmac_tumbler_t *mix = chip->mixer_data;
1107 if (mix->anded_reset) {
1108 DBG("(I) codec anded reset !\n");
1109 write_audio_gpio(&mix->hp_mute, 0);
1110 write_audio_gpio(&mix->amp_mute, 0);
1112 write_audio_gpio(&mix->hp_mute, 1);
1113 write_audio_gpio(&mix->amp_mute, 1);
1115 write_audio_gpio(&mix->hp_mute, 0);
1116 write_audio_gpio(&mix->amp_mute, 0);
1119 DBG("(I) codec normal reset !\n");
1121 write_audio_gpio(&mix->audio_reset, 0);
1123 write_audio_gpio(&mix->audio_reset, 1);
1125 write_audio_gpio(&mix->audio_reset, 0);
1130 #ifdef CONFIG_PMAC_PBOOK
1132 static void tumbler_suspend(pmac_t *chip)
1134 pmac_tumbler_t *mix = chip->mixer_data;
1136 if (mix->headphone_irq >= 0)
1137 disable_irq(mix->headphone_irq);
1138 if (mix->lineout_irq >= 0)
1139 disable_irq(mix->lineout_irq);
1140 mix->save_master_switch[0] = mix->master_switch[0];
1141 mix->save_master_switch[1] = mix->master_switch[1];
1142 mix->master_switch[0] = mix->master_switch[1] = 0;
1143 tumbler_set_master_volume(mix);
1144 if (!mix->anded_reset) {
1145 write_audio_gpio(&mix->amp_mute, 1);
1146 write_audio_gpio(&mix->hp_mute, 1);
1148 if (chip->model == PMAC_SNAPPER) {
1150 i2c_smbus_write_byte_data(mix->i2c.client, TAS_REG_ACS, mix->acs);
1152 if (mix->anded_reset) {
1153 write_audio_gpio(&mix->amp_mute, 1);
1154 write_audio_gpio(&mix->hp_mute, 1);
1156 write_audio_gpio(&mix->audio_reset, 1);
1160 static void tumbler_resume(pmac_t *chip)
1162 pmac_tumbler_t *mix = chip->mixer_data;
1164 snd_assert(mix, return);
1167 mix->master_switch[0] = mix->save_master_switch[0];
1168 mix->master_switch[1] = mix->save_master_switch[1];
1169 tumbler_reset_audio(chip);
1170 if (mix->i2c.client && mix->i2c.init_client) {
1171 if (mix->i2c.init_client(&mix->i2c) < 0)
1172 printk(KERN_ERR "tumbler_init_client error\n");
1174 printk(KERN_ERR "tumbler: i2c is not initialized\n");
1175 if (chip->model == PMAC_TUMBLER) {
1176 tumbler_set_mono_volume(mix, &tumbler_pcm_vol_info);
1177 tumbler_set_mono_volume(mix, &tumbler_bass_vol_info);
1178 tumbler_set_mono_volume(mix, &tumbler_treble_vol_info);
1179 tumbler_set_drc(mix);
1181 snapper_set_mix_vol(mix, VOL_IDX_PCM);
1182 snapper_set_mix_vol(mix, VOL_IDX_PCM2);
1183 snapper_set_mix_vol(mix, VOL_IDX_ADC);
1184 tumbler_set_mono_volume(mix, &snapper_bass_vol_info);
1185 tumbler_set_mono_volume(mix, &snapper_treble_vol_info);
1186 snapper_set_drc(mix);
1187 snapper_set_capture_source(mix);
1189 tumbler_set_master_volume(mix);
1190 if (chip->update_automute)
1191 chip->update_automute(chip, 0);
1192 if (mix->headphone_irq >= 0)
1193 enable_irq(mix->headphone_irq);
1194 if (mix->lineout_irq >= 0)
1195 enable_irq(mix->lineout_irq);
1199 /* initialize tumbler */
1200 static int __init tumbler_init(pmac_t *chip)
1203 pmac_tumbler_t *mix = chip->mixer_data;
1204 snd_assert(mix, return -EINVAL);
1206 if (tumbler_find_device("audio-hw-reset",
1207 "platform-do-hw-reset",
1208 &mix->audio_reset, 0) < 0)
1209 tumbler_find_device("hw-reset",
1210 "platform-do-hw-reset",
1211 &mix->audio_reset, 1);
1212 if (tumbler_find_device("amp-mute",
1213 "platform-do-amp-mute",
1214 &mix->amp_mute, 0) < 0)
1215 tumbler_find_device("amp-mute",
1216 "platform-do-amp-mute",
1218 if (tumbler_find_device("headphone-mute",
1219 "platform-do-headphone-mute",
1220 &mix->hp_mute, 0) < 0)
1221 tumbler_find_device("headphone-mute",
1222 "platform-do-headphone-mute",
1224 if (tumbler_find_device("line-output-mute",
1225 "platform-do-lineout-mute",
1226 &mix->line_mute, 0) < 0)
1227 tumbler_find_device("line-output-mute",
1228 "platform-do-lineout-mute",
1229 &mix->line_mute, 1);
1230 irq = tumbler_find_device("headphone-detect",
1231 NULL, &mix->hp_detect, 0);
1233 irq = tumbler_find_device("headphone-detect",
1234 NULL, &mix->hp_detect, 1);
1236 irq = tumbler_find_device("keywest-gpio15",
1237 NULL, &mix->hp_detect, 1);
1238 mix->headphone_irq = irq;
1239 irq = tumbler_find_device("line-output-detect",
1240 NULL, &mix->line_detect, 0);
1242 irq = tumbler_find_device("line-output-detect",
1243 NULL, &mix->line_detect, 1);
1244 mix->lineout_irq = irq;
1246 tumbler_reset_audio(chip);
1251 static void tumbler_cleanup(pmac_t *chip)
1253 pmac_tumbler_t *mix = chip->mixer_data;
1257 if (mix->headphone_irq >= 0)
1258 free_irq(mix->headphone_irq, chip);
1259 if (mix->lineout_irq >= 0)
1260 free_irq(mix->lineout_irq, chip);
1261 tumbler_gpio_free(&mix->audio_reset);
1262 tumbler_gpio_free(&mix->amp_mute);
1263 tumbler_gpio_free(&mix->hp_mute);
1264 tumbler_gpio_free(&mix->hp_detect);
1265 snd_pmac_keywest_cleanup(&mix->i2c);
1267 chip->mixer_data = NULL;
1271 int __init snd_pmac_tumbler_init(pmac_t *chip)
1274 pmac_tumbler_t *mix;
1276 struct device_node *tas_node, *np;
1280 if (current->fs->root)
1281 request_module("i2c-keywest");
1282 #endif /* CONFIG_KMOD */
1284 mix = kmalloc(sizeof(*mix), GFP_KERNEL);
1287 memset(mix, 0, sizeof(*mix));
1288 mix->headphone_irq = -1;
1290 chip->mixer_data = mix;
1291 chip->mixer_free = tumbler_cleanup;
1292 mix->anded_reset = 0;
1293 mix->reset_on_sleep = 1;
1295 for (np = chip->node->child; np; np = np->sibling) {
1296 if (!strcmp(np->name, "sound")) {
1297 if (get_property(np, "has-anded-reset", NULL))
1298 mix->anded_reset = 1;
1299 if (get_property(np, "layout-id", NULL))
1300 mix->reset_on_sleep = 0;
1304 if ((err = tumbler_init(chip)) < 0)
1308 tas_node = find_devices("deq");
1309 if (tas_node == NULL)
1310 tas_node = find_devices("codec");
1311 if (tas_node == NULL)
1314 paddr = (u32 *)get_property(tas_node, "i2c-address", NULL);
1316 paddr = (u32 *)get_property(tas_node, "reg", NULL);
1318 mix->i2c.addr = (*paddr) >> 1;
1320 mix->i2c.addr = TAS_I2C_ADDR;
1322 DBG("(I) TAS i2c address is: %x\n", mix->i2c.addr);
1324 if (chip->model == PMAC_TUMBLER) {
1325 mix->i2c.init_client = tumbler_init_client;
1326 mix->i2c.name = "TAS3001c";
1327 chipname = "Tumbler";
1329 mix->i2c.init_client = snapper_init_client;
1330 mix->i2c.name = "TAS3004";
1331 chipname = "Snapper";
1334 if ((err = snd_pmac_keywest_init(&mix->i2c)) < 0)
1340 sprintf(chip->card->mixername, "PowerMac %s", chipname);
1342 if (chip->model == PMAC_TUMBLER) {
1343 for (i = 0; i < ARRAY_SIZE(tumbler_mixers); i++) {
1344 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&tumbler_mixers[i], chip))) < 0)
1348 for (i = 0; i < ARRAY_SIZE(snapper_mixers); i++) {
1349 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snapper_mixers[i], chip))) < 0)
1353 chip->master_sw_ctl = snd_ctl_new1(&tumbler_hp_sw, chip);
1354 if ((err = snd_ctl_add(chip->card, chip->master_sw_ctl)) < 0)
1356 chip->speaker_sw_ctl = snd_ctl_new1(&tumbler_speaker_sw, chip);
1357 if ((err = snd_ctl_add(chip->card, chip->speaker_sw_ctl)) < 0)
1359 if (mix->line_mute.addr != 0) {
1360 chip->lineout_sw_ctl = snd_ctl_new1(&tumbler_lineout_sw, chip);
1361 if ((err = snd_ctl_add(chip->card, chip->lineout_sw_ctl)) < 0)
1364 chip->drc_sw_ctl = snd_ctl_new1(&tumbler_drc_sw, chip);
1365 if ((err = snd_ctl_add(chip->card, chip->drc_sw_ctl)) < 0)
1368 #ifdef CONFIG_PMAC_PBOOK
1369 chip->suspend = tumbler_suspend;
1370 chip->resume = tumbler_resume;
1373 INIT_WORK(&device_change, device_change_handler, (void *)chip);
1375 #ifdef PMAC_SUPPORT_AUTOMUTE
1376 if ((mix->headphone_irq >=0 || mix->lineout_irq >= 0)
1377 && (err = snd_pmac_add_automute(chip)) < 0)
1379 chip->detect_headphone = tumbler_detect_headphone;
1380 chip->update_automute = tumbler_update_automute;
1381 tumbler_update_automute(chip, 0); /* update the status only */
1383 /* activate headphone status interrupts */
1384 if (mix->headphone_irq >= 0) {
1386 if ((err = request_irq(mix->headphone_irq, headphone_intr, 0,
1387 "Sound Headphone Detection", chip)) < 0)
1389 /* activate headphone status interrupts */
1390 val = do_gpio_read(&mix->hp_detect);
1391 do_gpio_write(&mix->hp_detect, val | 0x80);
1393 if (mix->lineout_irq >= 0) {
1395 if ((err = request_irq(mix->lineout_irq, headphone_intr, 0,
1396 "Sound Lineout Detection", chip)) < 0)
1398 /* activate headphone status interrupts */
1399 val = do_gpio_read(&mix->line_detect);
1400 do_gpio_write(&mix->line_detect, val | 0x80);