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;
181 do_gpio_write(gp, active);
182 DBG("(I) gpio %x write %d\n", gp->addr, active);
185 static int read_audio_gpio(pmac_gpio_t *gp)
190 ret = ((do_gpio_read(gp) & 0x02) !=0);
191 return ret == gp->active_state;
195 * update master volume
197 static int tumbler_set_master_volume(pmac_tumbler_t *mix)
199 unsigned char block[6];
200 unsigned int left_vol, right_vol;
202 if (! mix->i2c.client)
205 if (! mix->master_switch[0])
208 left_vol = mix->master_vol[0];
209 if (left_vol >= ARRAY_SIZE(master_volume_table))
210 left_vol = ARRAY_SIZE(master_volume_table) - 1;
211 left_vol = master_volume_table[left_vol];
213 if (! mix->master_switch[1])
216 right_vol = mix->master_vol[1];
217 if (right_vol >= ARRAY_SIZE(master_volume_table))
218 right_vol = ARRAY_SIZE(master_volume_table) - 1;
219 right_vol = master_volume_table[right_vol];
222 block[0] = (left_vol >> 16) & 0xff;
223 block[1] = (left_vol >> 8) & 0xff;
224 block[2] = (left_vol >> 0) & 0xff;
226 block[3] = (right_vol >> 16) & 0xff;
227 block[4] = (right_vol >> 8) & 0xff;
228 block[5] = (right_vol >> 0) & 0xff;
230 if (i2c_smbus_write_block_data(mix->i2c.client, TAS_REG_VOL,
232 snd_printk("failed to set volume \n");
240 static int tumbler_info_master_volume(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
242 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
244 uinfo->value.integer.min = 0;
245 uinfo->value.integer.max = ARRAY_SIZE(master_volume_table) - 1;
249 static int tumbler_get_master_volume(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
251 pmac_t *chip = snd_kcontrol_chip(kcontrol);
252 pmac_tumbler_t *mix = chip->mixer_data;
253 snd_assert(mix, return -ENODEV);
254 ucontrol->value.integer.value[0] = mix->master_vol[0];
255 ucontrol->value.integer.value[1] = mix->master_vol[1];
259 static int tumbler_put_master_volume(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
261 pmac_t *chip = snd_kcontrol_chip(kcontrol);
262 pmac_tumbler_t *mix = chip->mixer_data;
265 snd_assert(mix, return -ENODEV);
266 change = mix->master_vol[0] != ucontrol->value.integer.value[0] ||
267 mix->master_vol[1] != ucontrol->value.integer.value[1];
269 mix->master_vol[0] = ucontrol->value.integer.value[0];
270 mix->master_vol[1] = ucontrol->value.integer.value[1];
271 tumbler_set_master_volume(mix);
277 static int tumbler_get_master_switch(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
279 pmac_t *chip = snd_kcontrol_chip(kcontrol);
280 pmac_tumbler_t *mix = chip->mixer_data;
281 snd_assert(mix, return -ENODEV);
282 ucontrol->value.integer.value[0] = mix->master_switch[0];
283 ucontrol->value.integer.value[1] = mix->master_switch[1];
287 static int tumbler_put_master_switch(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
289 pmac_t *chip = snd_kcontrol_chip(kcontrol);
290 pmac_tumbler_t *mix = chip->mixer_data;
293 snd_assert(mix, return -ENODEV);
294 change = mix->master_switch[0] != ucontrol->value.integer.value[0] ||
295 mix->master_switch[1] != ucontrol->value.integer.value[1];
297 mix->master_switch[0] = !!ucontrol->value.integer.value[0];
298 mix->master_switch[1] = !!ucontrol->value.integer.value[1];
299 tumbler_set_master_volume(mix);
306 * TAS3001c dynamic range compression
309 #define TAS3001_DRC_MAX 0x5f
311 static int tumbler_set_drc(pmac_tumbler_t *mix)
313 unsigned char val[2];
315 if (! mix->i2c.client)
318 if (mix->drc_enable) {
319 val[0] = 0xc1; /* enable, 3:1 compression */
320 if (mix->drc_range > TAS3001_DRC_MAX)
322 else if (mix->drc_range < 0)
325 val[1] = mix->drc_range + 0x91;
331 if (i2c_smbus_write_block_data(mix->i2c.client, TAS_REG_DRC,
333 snd_printk("failed to set DRC\n");
343 #define TAS3004_DRC_MAX 0xef
345 static int snapper_set_drc(pmac_tumbler_t *mix)
347 unsigned char val[6];
349 if (! mix->i2c.client)
353 val[0] = 0x50; /* 3:1 above threshold */
355 val[0] = 0x51; /* disabled */
356 val[1] = 0x02; /* 1:1 below threshold */
357 if (mix->drc_range > 0xef)
359 else if (mix->drc_range < 0)
362 val[2] = mix->drc_range;
367 if (i2c_smbus_write_block_data(mix->i2c.client, TAS_REG_DRC,
369 snd_printk("failed to set DRC\n");
375 static int tumbler_info_drc_value(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
377 pmac_t *chip = snd_kcontrol_chip(kcontrol);
378 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
380 uinfo->value.integer.min = 0;
381 uinfo->value.integer.max =
382 chip->model == PMAC_TUMBLER ? TAS3001_DRC_MAX : TAS3004_DRC_MAX;
386 static int tumbler_get_drc_value(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
388 pmac_t *chip = snd_kcontrol_chip(kcontrol);
390 if (! (mix = chip->mixer_data))
392 ucontrol->value.integer.value[0] = mix->drc_range;
396 static int tumbler_put_drc_value(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
398 pmac_t *chip = snd_kcontrol_chip(kcontrol);
402 if (! (mix = chip->mixer_data))
404 change = mix->drc_range != ucontrol->value.integer.value[0];
406 mix->drc_range = ucontrol->value.integer.value[0];
407 if (chip->model == PMAC_TUMBLER)
408 tumbler_set_drc(mix);
410 snapper_set_drc(mix);
415 static int tumbler_get_drc_switch(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
417 pmac_t *chip = snd_kcontrol_chip(kcontrol);
419 if (! (mix = chip->mixer_data))
421 ucontrol->value.integer.value[0] = mix->drc_enable;
425 static int tumbler_put_drc_switch(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
427 pmac_t *chip = snd_kcontrol_chip(kcontrol);
431 if (! (mix = chip->mixer_data))
433 change = mix->drc_enable != ucontrol->value.integer.value[0];
435 mix->drc_enable = !!ucontrol->value.integer.value[0];
436 if (chip->model == PMAC_TUMBLER)
437 tumbler_set_drc(mix);
439 snapper_set_drc(mix);
449 struct tumbler_mono_vol {
457 static int tumbler_set_mono_volume(pmac_tumbler_t *mix, struct tumbler_mono_vol *info)
459 unsigned char block[4];
463 if (! mix->i2c.client)
466 vol = mix->mono_vol[info->index];
467 if (vol >= info->max)
469 vol = info->table[vol];
470 for (i = 0; i < info->bytes; i++)
471 block[i] = (vol >> ((info->bytes - i - 1) * 8)) & 0xff;
472 if (i2c_smbus_write_block_data(mix->i2c.client, info->reg,
473 info->bytes, block) < 0) {
474 snd_printk("failed to set mono volume %d\n", info->index);
480 static int tumbler_info_mono(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
482 struct tumbler_mono_vol *info = (struct tumbler_mono_vol *)kcontrol->private_value;
484 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
486 uinfo->value.integer.min = 0;
487 uinfo->value.integer.max = info->max - 1;
491 static int tumbler_get_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);
496 if (! (mix = chip->mixer_data))
498 ucontrol->value.integer.value[0] = mix->mono_vol[info->index];
502 static int tumbler_put_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);
509 if (! (mix = chip->mixer_data))
511 change = mix->mono_vol[info->index] != ucontrol->value.integer.value[0];
513 mix->mono_vol[info->index] = ucontrol->value.integer.value[0];
514 tumbler_set_mono_volume(mix, info);
519 /* TAS3001c mono volumes */
520 static struct tumbler_mono_vol tumbler_pcm_vol_info = {
521 .index = VOL_IDX_PCM_MONO,
524 .max = ARRAY_SIZE(mixer_volume_table),
525 .table = mixer_volume_table,
528 static struct tumbler_mono_vol tumbler_bass_vol_info = {
529 .index = VOL_IDX_BASS,
532 .max = ARRAY_SIZE(bass_volume_table),
533 .table = bass_volume_table,
536 static struct tumbler_mono_vol tumbler_treble_vol_info = {
537 .index = VOL_IDX_TREBLE,
538 .reg = TAS_REG_TREBLE,
540 .max = ARRAY_SIZE(treble_volume_table),
541 .table = treble_volume_table,
544 /* TAS3004 mono volumes */
545 static struct tumbler_mono_vol snapper_bass_vol_info = {
546 .index = VOL_IDX_BASS,
549 .max = ARRAY_SIZE(snapper_bass_volume_table),
550 .table = snapper_bass_volume_table,
553 static struct tumbler_mono_vol snapper_treble_vol_info = {
554 .index = VOL_IDX_TREBLE,
555 .reg = TAS_REG_TREBLE,
557 .max = ARRAY_SIZE(snapper_treble_volume_table),
558 .table = snapper_treble_volume_table,
562 #define DEFINE_MONO(xname,type) { \
563 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,\
565 .info = tumbler_info_mono, \
566 .get = tumbler_get_mono, \
567 .put = tumbler_put_mono, \
568 .private_value = (unsigned long)(&tumbler_##type##_vol_info), \
571 #define DEFINE_SNAPPER_MONO(xname,type) { \
572 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,\
574 .info = tumbler_info_mono, \
575 .get = tumbler_get_mono, \
576 .put = tumbler_put_mono, \
577 .private_value = (unsigned long)(&snapper_##type##_vol_info), \
582 * snapper mixer volumes
585 static int snapper_set_mix_vol1(pmac_tumbler_t *mix, int idx, int ch, int reg)
588 unsigned char block[9];
590 vol = mix->mix_vol[idx][ch];
591 if (vol >= ARRAY_SIZE(mixer_volume_table)) {
592 vol = ARRAY_SIZE(mixer_volume_table) - 1;
593 mix->mix_vol[idx][ch] = vol;
596 for (i = 0; i < 3; i++) {
597 vol = mix->mix_vol[i][ch];
598 vol = mixer_volume_table[vol];
599 for (j = 0; j < 3; j++)
600 block[i * 3 + j] = (vol >> ((2 - j) * 8)) & 0xff;
602 if (i2c_smbus_write_block_data(mix->i2c.client, reg, 9, block) < 0) {
603 snd_printk("failed to set mono volume %d\n", reg);
609 static int snapper_set_mix_vol(pmac_tumbler_t *mix, int idx)
611 if (! mix->i2c.client)
613 if (snapper_set_mix_vol1(mix, idx, 0, TAS_REG_LMIX) < 0 ||
614 snapper_set_mix_vol1(mix, idx, 1, TAS_REG_RMIX) < 0)
619 static int snapper_info_mix(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
621 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
623 uinfo->value.integer.min = 0;
624 uinfo->value.integer.max = ARRAY_SIZE(mixer_volume_table) - 1;
628 static int snapper_get_mix(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
630 int idx = (int)kcontrol->private_value;
631 pmac_t *chip = snd_kcontrol_chip(kcontrol);
633 if (! (mix = chip->mixer_data))
635 ucontrol->value.integer.value[0] = mix->mix_vol[idx][0];
636 ucontrol->value.integer.value[1] = mix->mix_vol[idx][1];
640 static int snapper_put_mix(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
642 int idx = (int)kcontrol->private_value;
643 pmac_t *chip = snd_kcontrol_chip(kcontrol);
647 if (! (mix = chip->mixer_data))
649 change = mix->mix_vol[idx][0] != ucontrol->value.integer.value[0] ||
650 mix->mix_vol[idx][1] != ucontrol->value.integer.value[1];
652 mix->mix_vol[idx][0] = ucontrol->value.integer.value[0];
653 mix->mix_vol[idx][1] = ucontrol->value.integer.value[1];
654 snapper_set_mix_vol(mix, idx);
661 * mute switches. FIXME: Turn that into software mute when both outputs are muted
662 * to avoid codec reset on ibook M7
665 enum { TUMBLER_MUTE_HP, TUMBLER_MUTE_AMP, TUMBLER_MUTE_LINE };
667 static int tumbler_get_mute_switch(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
669 pmac_t *chip = snd_kcontrol_chip(kcontrol);
672 if (! (mix = chip->mixer_data))
674 switch(kcontrol->private_value) {
675 case TUMBLER_MUTE_HP:
676 gp = &mix->hp_mute; break;
677 case TUMBLER_MUTE_AMP:
678 gp = &mix->amp_mute; break;
679 case TUMBLER_MUTE_LINE:
680 gp = &mix->line_mute; break;
686 ucontrol->value.integer.value[0] = ! read_audio_gpio(gp);
690 static int tumbler_put_mute_switch(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
692 pmac_t *chip = snd_kcontrol_chip(kcontrol);
696 #ifdef PMAC_SUPPORT_AUTOMUTE
697 if (chip->update_automute && chip->auto_mute)
698 return 0; /* don't touch in the auto-mute mode */
700 if (! (mix = chip->mixer_data))
702 switch(kcontrol->private_value) {
703 case TUMBLER_MUTE_HP:
704 gp = &mix->hp_mute; break;
705 case TUMBLER_MUTE_AMP:
706 gp = &mix->amp_mute; break;
707 case TUMBLER_MUTE_LINE:
708 gp = &mix->line_mute; break;
714 val = ! read_audio_gpio(gp);
715 if (val != ucontrol->value.integer.value[0]) {
716 write_audio_gpio(gp, ! ucontrol->value.integer.value[0]);
722 static int snapper_set_capture_source(pmac_tumbler_t *mix)
724 if (! mix->i2c.client)
726 if (mix->capture_source)
727 mix->acs = mix->acs |= 2;
730 return i2c_smbus_write_byte_data(mix->i2c.client, TAS_REG_ACS, mix->acs);
733 static int snapper_info_capture_source(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
735 static char *texts[2] = {
738 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
740 uinfo->value.enumerated.items = 2;
741 if (uinfo->value.enumerated.item > 1)
742 uinfo->value.enumerated.item = 1;
743 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
747 static int snapper_get_capture_source(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
749 pmac_t *chip = snd_kcontrol_chip(kcontrol);
750 pmac_tumbler_t *mix = chip->mixer_data;
752 snd_assert(mix, return -ENODEV);
753 ucontrol->value.integer.value[0] = mix->capture_source;
757 static int snapper_put_capture_source(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
759 pmac_t *chip = snd_kcontrol_chip(kcontrol);
760 pmac_tumbler_t *mix = chip->mixer_data;
763 snd_assert(mix, return -ENODEV);
764 change = ucontrol->value.integer.value[0] != mix->capture_source;
766 mix->capture_source = !!ucontrol->value.integer.value[0];
767 snapper_set_capture_source(mix);
772 #define DEFINE_SNAPPER_MIX(xname,idx,ofs) { \
773 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,\
775 .info = snapper_info_mix, \
776 .get = snapper_get_mix, \
777 .put = snapper_put_mix, \
779 .private_value = ofs, \
785 static snd_kcontrol_new_t tumbler_mixers[] __initdata = {
786 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
787 .name = "Master Playback Volume",
788 .info = tumbler_info_master_volume,
789 .get = tumbler_get_master_volume,
790 .put = tumbler_put_master_volume
792 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
793 .name = "Master Playback Switch",
794 .info = snd_pmac_boolean_stereo_info,
795 .get = tumbler_get_master_switch,
796 .put = tumbler_put_master_switch
798 DEFINE_MONO("Tone Control - Bass", bass),
799 DEFINE_MONO("Tone Control - Treble", treble),
800 DEFINE_MONO("PCM Playback Volume", pcm),
801 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
803 .info = tumbler_info_drc_value,
804 .get = tumbler_get_drc_value,
805 .put = tumbler_put_drc_value
809 static snd_kcontrol_new_t snapper_mixers[] __initdata = {
810 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
811 .name = "Master Playback Volume",
812 .info = tumbler_info_master_volume,
813 .get = tumbler_get_master_volume,
814 .put = tumbler_put_master_volume
816 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
817 .name = "Master Playback Switch",
818 .info = snd_pmac_boolean_stereo_info,
819 .get = tumbler_get_master_switch,
820 .put = tumbler_put_master_switch
822 DEFINE_SNAPPER_MIX("PCM Playback Volume", 0, VOL_IDX_PCM),
823 DEFINE_SNAPPER_MIX("PCM Playback Volume", 1, VOL_IDX_PCM2),
824 DEFINE_SNAPPER_MIX("Monitor Mix Volume", 0, VOL_IDX_ADC),
825 DEFINE_SNAPPER_MONO("Tone Control - Bass", bass),
826 DEFINE_SNAPPER_MONO("Tone Control - Treble", treble),
827 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
829 .info = tumbler_info_drc_value,
830 .get = tumbler_get_drc_value,
831 .put = tumbler_put_drc_value
833 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
834 .name = "Input Source", /* FIXME: "Capture Source" doesn't work properly */
835 .info = snapper_info_capture_source,
836 .get = snapper_get_capture_source,
837 .put = snapper_put_capture_source
841 static snd_kcontrol_new_t tumbler_hp_sw __initdata = {
842 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
843 .name = "Headphone Playback Switch",
844 .info = snd_pmac_boolean_mono_info,
845 .get = tumbler_get_mute_switch,
846 .put = tumbler_put_mute_switch,
847 .private_value = TUMBLER_MUTE_HP,
849 static snd_kcontrol_new_t tumbler_speaker_sw __initdata = {
850 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
851 .name = "PC Speaker Playback Switch",
852 .info = snd_pmac_boolean_mono_info,
853 .get = tumbler_get_mute_switch,
854 .put = tumbler_put_mute_switch,
855 .private_value = TUMBLER_MUTE_AMP,
857 static snd_kcontrol_new_t tumbler_lineout_sw __initdata = {
858 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
859 .name = "Line Out Playback Switch",
860 .info = snd_pmac_boolean_mono_info,
861 .get = tumbler_get_mute_switch,
862 .put = tumbler_put_mute_switch,
863 .private_value = TUMBLER_MUTE_LINE,
865 static snd_kcontrol_new_t tumbler_drc_sw __initdata = {
866 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
867 .name = "DRC Switch",
868 .info = snd_pmac_boolean_mono_info,
869 .get = tumbler_get_drc_switch,
870 .put = tumbler_put_drc_switch
874 #ifdef PMAC_SUPPORT_AUTOMUTE
878 static int tumbler_detect_headphone(pmac_t *chip)
880 pmac_tumbler_t *mix = chip->mixer_data;
883 if (mix->hp_detect.addr)
884 detect |= read_audio_gpio(&mix->hp_detect);
888 static int tumbler_detect_lineout(pmac_t *chip)
890 pmac_tumbler_t *mix = chip->mixer_data;
893 if (mix->line_detect.addr)
894 detect |= read_audio_gpio(&mix->line_detect);
898 static void check_mute(pmac_t *chip, pmac_gpio_t *gp, int val, int do_notify, snd_kcontrol_t *sw)
900 //pmac_tumbler_t *mix = chip->mixer_data;
901 if (val != read_audio_gpio(gp)) {
902 write_audio_gpio(gp, val);
904 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE, &sw->id);
908 static struct work_struct device_change;
910 static void device_change_handler(void *self)
912 pmac_t *chip = (pmac_t*) self;
914 int headphone, lineout;
919 mix = chip->mixer_data;
920 snd_assert(mix, return);
922 headphone = tumbler_detect_headphone(chip);
923 lineout = tumbler_detect_lineout(chip);
925 DBG("headphone: %d, lineout: %d\n", headphone, lineout);
927 if (headphone || lineout) {
928 /* unmute headphone/lineout & mute speaker */
930 check_mute(chip, &mix->hp_mute, 0, mix->auto_mute_notify,
931 chip->master_sw_ctl);
932 if (lineout && mix->line_mute.addr != 0)
933 check_mute(chip, &mix->line_mute, 0, mix->auto_mute_notify,
934 chip->lineout_sw_ctl);
935 if (mix->anded_reset)
937 check_mute(chip, &mix->amp_mute, 1, mix->auto_mute_notify,
938 chip->speaker_sw_ctl);
941 /* unmute speaker, mute others */
942 check_mute(chip, &mix->amp_mute, 0, mix->auto_mute_notify,
943 chip->speaker_sw_ctl);
944 if (mix->anded_reset)
946 check_mute(chip, &mix->hp_mute, 1, mix->auto_mute_notify,
947 chip->master_sw_ctl);
948 if (mix->line_mute.addr != 0)
949 check_mute(chip, &mix->line_mute, 1, mix->auto_mute_notify,
950 chip->lineout_sw_ctl);
953 if (mix->auto_mute_notify) {
954 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
955 &chip->hp_detect_ctl->id);
956 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
957 &chip->drc_sw_ctl->id);
960 /* first set the DRC so the speaker do not explode -ReneR */
961 if (chip->model == PMAC_TUMBLER)
962 tumbler_set_drc(mix);
964 snapper_set_drc(mix);
966 /* reset the master volume so the correct amplification is applied */
967 tumbler_set_master_volume(mix);
970 static void tumbler_update_automute(pmac_t *chip, int do_notify)
972 if (chip->auto_mute) {
974 mix = chip->mixer_data;
975 snd_assert(mix, return);
976 mix->auto_mute_notify = do_notify;
977 schedule_work(&device_change);
980 #endif /* PMAC_SUPPORT_AUTOMUTE */
983 /* interrupt - headphone plug changed */
984 static irqreturn_t headphone_intr(int irq, void *devid, struct pt_regs *regs)
986 pmac_t *chip = devid;
987 if (chip->update_automute && chip->initialized) {
988 chip->update_automute(chip, 1);
994 /* look for audio-gpio device */
995 static struct device_node *find_audio_device(const char *name)
997 struct device_node *np;
999 if (! (np = find_devices("gpio")))
1002 for (np = np->child; np; np = np->sibling) {
1003 char *property = get_property(np, "audio-gpio", NULL);
1004 if (property && strcmp(property, name) == 0)
1010 /* look for audio-gpio device */
1011 static struct device_node *find_compatible_audio_device(const char *name)
1013 struct device_node *np;
1015 if (! (np = find_devices("gpio")))
1018 for (np = np->child; np; np = np->sibling) {
1019 if (device_is_compatible(np, name))
1025 /* find an audio device and get its address */
1026 static long tumbler_find_device(const char *device, const char *platform, pmac_gpio_t *gp, int is_compatible)
1028 struct device_node *node;
1032 node = find_compatible_audio_device(device);
1034 node = find_audio_device(device);
1036 DBG("(W) cannot find audio device %s !\n", device);
1037 snd_printdd("cannot find device %s\n", device);
1041 base = (u32 *)get_property(node, "AAPL,address", NULL);
1043 base = (u32 *)get_property(node, "reg", NULL);
1045 DBG("(E) cannot find address for device %s !\n", device);
1046 snd_printd("cannot find address for device %s\n", device);
1055 gp->addr = addr & 0x0000ffff;
1056 /* Try to find the active state, default to 0 ! */
1057 base = (u32 *)get_property(node, "audio-gpio-active-state", NULL);
1059 gp->active_state = *base;
1060 gp->active_val = (*base) ? 0x5 : 0x4;
1061 gp->inactive_val = (*base) ? 0x4 : 0x5;
1064 gp->active_state = 0;
1065 gp->active_val = 0x4;
1066 gp->inactive_val = 0x5;
1067 /* Here are some crude hacks to extract the GPIO polarity and
1068 * open collector informations out of the do-platform script
1069 * as we don't yet have an interpreter for these things
1072 prop = (u32 *)get_property(node, platform, NULL);
1074 if (prop[3] == 0x9 && prop[4] == 0x9) {
1075 gp->active_val = 0xd;
1076 gp->inactive_val = 0xc;
1078 if (prop[3] == 0x1 && prop[4] == 0x1) {
1079 gp->active_val = 0x5;
1080 gp->inactive_val = 0x4;
1085 DBG("(I) GPIO device %s found, offset: %x, active state: %d !\n",
1086 device, gp->addr, gp->active_state);
1088 return (node->n_intrs > 0) ? node->intrs[0].line : 0;
1092 static void tumbler_reset_audio(pmac_t *chip)
1094 pmac_tumbler_t *mix = chip->mixer_data;
1096 if (mix->anded_reset) {
1097 DBG("(I) codec anded reset !\n");
1098 write_audio_gpio(&mix->hp_mute, 0);
1099 write_audio_gpio(&mix->amp_mute, 0);
1101 write_audio_gpio(&mix->hp_mute, 1);
1102 write_audio_gpio(&mix->amp_mute, 1);
1104 write_audio_gpio(&mix->hp_mute, 0);
1105 write_audio_gpio(&mix->amp_mute, 0);
1108 DBG("(I) codec normal reset !\n");
1110 write_audio_gpio(&mix->audio_reset, 0);
1112 write_audio_gpio(&mix->audio_reset, 1);
1114 write_audio_gpio(&mix->audio_reset, 0);
1119 #ifdef CONFIG_PMAC_PBOOK
1121 static void tumbler_suspend(pmac_t *chip)
1123 pmac_tumbler_t *mix = chip->mixer_data;
1125 if (mix->headphone_irq >= 0)
1126 disable_irq(mix->headphone_irq);
1127 if (mix->lineout_irq >= 0)
1128 disable_irq(mix->lineout_irq);
1129 mix->save_master_switch[0] = mix->master_switch[0];
1130 mix->save_master_switch[1] = mix->master_switch[1];
1131 mix->master_switch[0] = mix->master_switch[1] = 0;
1132 tumbler_set_master_volume(mix);
1133 if (!mix->anded_reset) {
1134 write_audio_gpio(&mix->amp_mute, 1);
1135 write_audio_gpio(&mix->hp_mute, 1);
1137 if (chip->model == PMAC_SNAPPER) {
1139 i2c_smbus_write_byte_data(mix->i2c.client, TAS_REG_ACS, mix->acs);
1141 if (mix->anded_reset) {
1142 write_audio_gpio(&mix->amp_mute, 1);
1143 write_audio_gpio(&mix->hp_mute, 1);
1145 write_audio_gpio(&mix->audio_reset, 1);
1149 static void tumbler_resume(pmac_t *chip)
1151 pmac_tumbler_t *mix = chip->mixer_data;
1153 snd_assert(mix, return);
1156 mix->master_switch[0] = mix->save_master_switch[0];
1157 mix->master_switch[1] = mix->save_master_switch[1];
1158 tumbler_reset_audio(chip);
1159 if (mix->i2c.client && mix->i2c.init_client) {
1160 if (mix->i2c.init_client(&mix->i2c) < 0)
1161 printk(KERN_ERR "tumbler_init_client error\n");
1163 printk(KERN_ERR "tumbler: i2c is not initialized\n");
1164 if (chip->model == PMAC_TUMBLER) {
1165 tumbler_set_mono_volume(mix, &tumbler_pcm_vol_info);
1166 tumbler_set_mono_volume(mix, &tumbler_bass_vol_info);
1167 tumbler_set_mono_volume(mix, &tumbler_treble_vol_info);
1168 tumbler_set_drc(mix);
1170 snapper_set_mix_vol(mix, VOL_IDX_PCM);
1171 snapper_set_mix_vol(mix, VOL_IDX_PCM2);
1172 snapper_set_mix_vol(mix, VOL_IDX_ADC);
1173 tumbler_set_mono_volume(mix, &snapper_bass_vol_info);
1174 tumbler_set_mono_volume(mix, &snapper_treble_vol_info);
1175 snapper_set_drc(mix);
1176 snapper_set_capture_source(mix);
1178 tumbler_set_master_volume(mix);
1179 if (chip->update_automute)
1180 chip->update_automute(chip, 0);
1181 if (mix->headphone_irq >= 0)
1182 enable_irq(mix->headphone_irq);
1183 if (mix->lineout_irq >= 0)
1184 enable_irq(mix->lineout_irq);
1188 /* initialize tumbler */
1189 static int __init tumbler_init(pmac_t *chip)
1192 pmac_tumbler_t *mix = chip->mixer_data;
1193 snd_assert(mix, return -EINVAL);
1195 if (tumbler_find_device("audio-hw-reset",
1196 "platform-do-hw-reset",
1197 &mix->audio_reset, 0) < 0)
1198 tumbler_find_device("hw-reset",
1199 "platform-do-hw-reset",
1200 &mix->audio_reset, 1);
1201 if (tumbler_find_device("amp-mute",
1202 "platform-do-amp-mute",
1203 &mix->amp_mute, 0) < 0)
1204 tumbler_find_device("amp-mute",
1205 "platform-do-amp-mute",
1207 if (tumbler_find_device("headphone-mute",
1208 "platform-do-headphone-mute",
1209 &mix->hp_mute, 0) < 0)
1210 tumbler_find_device("headphone-mute",
1211 "platform-do-headphone-mute",
1213 if (tumbler_find_device("line-output-mute",
1214 "platform-do-lineout-mute",
1215 &mix->line_mute, 0) < 0)
1216 tumbler_find_device("line-output-mute",
1217 "platform-do-lineout-mute",
1218 &mix->line_mute, 1);
1219 irq = tumbler_find_device("headphone-detect",
1220 NULL, &mix->hp_detect, 0);
1222 irq = tumbler_find_device("headphone-detect",
1223 NULL, &mix->hp_detect, 1);
1225 irq = tumbler_find_device("keywest-gpio15",
1226 NULL, &mix->hp_detect, 1);
1227 mix->headphone_irq = irq;
1228 irq = tumbler_find_device("line-output-detect",
1229 NULL, &mix->line_detect, 0);
1231 irq = tumbler_find_device("line-output-detect",
1232 NULL, &mix->line_detect, 1);
1233 mix->lineout_irq = irq;
1235 tumbler_reset_audio(chip);
1240 static void tumbler_cleanup(pmac_t *chip)
1242 pmac_tumbler_t *mix = chip->mixer_data;
1246 if (mix->headphone_irq >= 0)
1247 free_irq(mix->headphone_irq, chip);
1248 if (mix->lineout_irq >= 0)
1249 free_irq(mix->lineout_irq, chip);
1250 tumbler_gpio_free(&mix->audio_reset);
1251 tumbler_gpio_free(&mix->amp_mute);
1252 tumbler_gpio_free(&mix->hp_mute);
1253 tumbler_gpio_free(&mix->hp_detect);
1254 snd_pmac_keywest_cleanup(&mix->i2c);
1256 chip->mixer_data = NULL;
1260 int __init snd_pmac_tumbler_init(pmac_t *chip)
1263 pmac_tumbler_t *mix;
1265 struct device_node *tas_node, *np;
1269 if (current->fs->root)
1270 request_module("i2c-keywest");
1271 #endif /* CONFIG_KMOD */
1273 mix = kmalloc(sizeof(*mix), GFP_KERNEL);
1276 memset(mix, 0, sizeof(*mix));
1277 mix->headphone_irq = -1;
1279 chip->mixer_data = mix;
1280 chip->mixer_free = tumbler_cleanup;
1281 mix->anded_reset = 0;
1282 mix->reset_on_sleep = 1;
1284 for (np = chip->node->child; np; np = np->sibling) {
1285 if (!strcmp(np->name, "sound")) {
1286 if (get_property(np, "has-anded-reset", NULL))
1287 mix->anded_reset = 1;
1288 if (get_property(np, "layout-id", NULL))
1289 mix->reset_on_sleep = 0;
1293 if ((err = tumbler_init(chip)) < 0)
1297 tas_node = find_devices("deq");
1298 if (tas_node == NULL)
1299 tas_node = find_devices("codec");
1300 if (tas_node == NULL)
1303 paddr = (u32 *)get_property(tas_node, "i2c-address", NULL);
1305 paddr = (u32 *)get_property(tas_node, "reg", NULL);
1307 mix->i2c.addr = (*paddr) >> 1;
1309 mix->i2c.addr = TAS_I2C_ADDR;
1311 DBG("(I) TAS i2c address is: %x\n", mix->i2c.addr);
1313 if (chip->model == PMAC_TUMBLER) {
1314 mix->i2c.init_client = tumbler_init_client;
1315 mix->i2c.name = "TAS3001c";
1316 chipname = "Tumbler";
1318 mix->i2c.init_client = snapper_init_client;
1319 mix->i2c.name = "TAS3004";
1320 chipname = "Snapper";
1323 if ((err = snd_pmac_keywest_init(&mix->i2c)) < 0)
1329 sprintf(chip->card->mixername, "PowerMac %s", chipname);
1331 if (chip->model == PMAC_TUMBLER) {
1332 for (i = 0; i < ARRAY_SIZE(tumbler_mixers); i++) {
1333 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&tumbler_mixers[i], chip))) < 0)
1337 for (i = 0; i < ARRAY_SIZE(snapper_mixers); i++) {
1338 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snapper_mixers[i], chip))) < 0)
1342 chip->master_sw_ctl = snd_ctl_new1(&tumbler_hp_sw, chip);
1343 if ((err = snd_ctl_add(chip->card, chip->master_sw_ctl)) < 0)
1345 chip->speaker_sw_ctl = snd_ctl_new1(&tumbler_speaker_sw, chip);
1346 if ((err = snd_ctl_add(chip->card, chip->speaker_sw_ctl)) < 0)
1348 if (mix->line_mute.addr != 0) {
1349 chip->lineout_sw_ctl = snd_ctl_new1(&tumbler_lineout_sw, chip);
1350 if ((err = snd_ctl_add(chip->card, chip->lineout_sw_ctl)) < 0)
1353 chip->drc_sw_ctl = snd_ctl_new1(&tumbler_drc_sw, chip);
1354 if ((err = snd_ctl_add(chip->card, chip->drc_sw_ctl)) < 0)
1357 #ifdef CONFIG_PMAC_PBOOK
1358 chip->suspend = tumbler_suspend;
1359 chip->resume = tumbler_resume;
1362 INIT_WORK(&device_change, device_change_handler, (void *)chip);
1364 #ifdef PMAC_SUPPORT_AUTOMUTE
1365 if ((mix->headphone_irq >=0 || mix->lineout_irq >= 0)
1366 && (err = snd_pmac_add_automute(chip)) < 0)
1368 chip->detect_headphone = tumbler_detect_headphone;
1369 chip->update_automute = tumbler_update_automute;
1370 tumbler_update_automute(chip, 0); /* update the status only */
1372 /* activate headphone status interrupts */
1373 if (mix->headphone_irq >= 0) {
1375 if ((err = request_irq(mix->headphone_irq, headphone_intr, 0,
1376 "Sound Headphone Detection", chip)) < 0)
1378 /* activate headphone status interrupts */
1379 val = do_gpio_read(&mix->hp_detect);
1380 do_gpio_write(&mix->hp_detect, val | 0x80);
1382 if (mix->lineout_irq >= 0) {
1384 if ((err = request_irq(mix->lineout_irq, headphone_intr, 0,
1385 "Sound Lineout Detection", chip)) < 0)
1387 /* activate headphone status interrupts */
1388 val = do_gpio_read(&mix->line_detect);
1389 do_gpio_write(&mix->line_detect, val | 0x80);