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,
93 struct pmac_keywest i2c;
94 struct pmac_gpio audio_reset;
95 struct pmac_gpio amp_mute;
96 struct pmac_gpio line_mute;
97 struct pmac_gpio line_detect;
98 struct pmac_gpio hp_mute;
99 struct pmac_gpio hp_detect;
102 unsigned int save_master_vol[2];
103 unsigned int master_vol[2];
104 unsigned int save_master_switch[2];
105 unsigned int master_switch[2];
106 unsigned int mono_vol[VOL_IDX_LAST_MONO];
107 unsigned int mix_vol[VOL_IDX_LAST_MIX][2]; /* stereo volumes for tas3004 */
112 int auto_mute_notify;
121 static int send_init_client(struct pmac_keywest *i2c, unsigned int *regs)
126 err = i2c_smbus_write_byte_data(i2c->client,
130 DBG("(W) i2c error %d\n", err);
141 static int tumbler_init_client(struct pmac_keywest *i2c)
143 static unsigned int regs[] = {
144 /* normal operation, SCLK=64fps, i2s output, i2s input, 16bit width */
145 TAS_REG_MCS, (1<<6)|(2<<4)|(2<<2)|0,
148 DBG("(I) tumbler init client\n");
149 return send_init_client(i2c, regs);
152 static int snapper_init_client(struct pmac_keywest *i2c)
154 static unsigned int regs[] = {
155 /* normal operation, SCLK=64fps, i2s output, 16bit width */
156 TAS_REG_MCS, (1<<6)|(2<<4)|0,
157 /* normal operation, all-pass mode */
158 TAS_REG_MCS2, (1<<1),
159 /* normal output, no deemphasis, A input, power-up, line-in */
163 DBG("(I) snapper init client\n");
164 return send_init_client(i2c, regs);
170 #define do_gpio_write(gp, val) \
171 pmac_call_feature(PMAC_FTR_WRITE_GPIO, NULL, (gp)->addr, val)
172 #define do_gpio_read(gp) \
173 pmac_call_feature(PMAC_FTR_READ_GPIO, NULL, (gp)->addr, 0)
174 #define tumbler_gpio_free(gp) /* NOP */
176 static void write_audio_gpio(struct pmac_gpio *gp, int active)
180 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 check_audio_gpio(struct pmac_gpio *gp)
192 ret = do_gpio_read(gp);
194 return (ret & 0xd) == (gp->active_val & 0xd);
197 static int read_audio_gpio(struct pmac_gpio *gp)
202 ret = ((do_gpio_read(gp) & 0x02) !=0);
203 return ret == gp->active_state;
207 * update master volume
209 static int tumbler_set_master_volume(struct pmac_tumbler *mix)
211 unsigned char block[6];
212 unsigned int left_vol, right_vol;
214 if (! mix->i2c.client)
217 if (! mix->master_switch[0])
220 left_vol = mix->master_vol[0];
221 if (left_vol >= ARRAY_SIZE(master_volume_table))
222 left_vol = ARRAY_SIZE(master_volume_table) - 1;
223 left_vol = master_volume_table[left_vol];
225 if (! mix->master_switch[1])
228 right_vol = mix->master_vol[1];
229 if (right_vol >= ARRAY_SIZE(master_volume_table))
230 right_vol = ARRAY_SIZE(master_volume_table) - 1;
231 right_vol = master_volume_table[right_vol];
234 block[0] = (left_vol >> 16) & 0xff;
235 block[1] = (left_vol >> 8) & 0xff;
236 block[2] = (left_vol >> 0) & 0xff;
238 block[3] = (right_vol >> 16) & 0xff;
239 block[4] = (right_vol >> 8) & 0xff;
240 block[5] = (right_vol >> 0) & 0xff;
242 if (i2c_smbus_write_block_data(mix->i2c.client, TAS_REG_VOL,
244 snd_printk("failed to set volume \n");
252 static int tumbler_info_master_volume(struct snd_kcontrol *kcontrol,
253 struct snd_ctl_elem_info *uinfo)
255 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
257 uinfo->value.integer.min = 0;
258 uinfo->value.integer.max = ARRAY_SIZE(master_volume_table) - 1;
262 static int tumbler_get_master_volume(struct snd_kcontrol *kcontrol,
263 struct snd_ctl_elem_value *ucontrol)
265 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
266 struct pmac_tumbler *mix = chip->mixer_data;
267 snd_assert(mix, return -ENODEV);
268 ucontrol->value.integer.value[0] = mix->master_vol[0];
269 ucontrol->value.integer.value[1] = mix->master_vol[1];
273 static int tumbler_put_master_volume(struct snd_kcontrol *kcontrol,
274 struct snd_ctl_elem_value *ucontrol)
276 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
277 struct pmac_tumbler *mix = chip->mixer_data;
280 snd_assert(mix, return -ENODEV);
281 change = mix->master_vol[0] != ucontrol->value.integer.value[0] ||
282 mix->master_vol[1] != ucontrol->value.integer.value[1];
284 mix->master_vol[0] = ucontrol->value.integer.value[0];
285 mix->master_vol[1] = ucontrol->value.integer.value[1];
286 tumbler_set_master_volume(mix);
292 static int tumbler_get_master_switch(struct snd_kcontrol *kcontrol,
293 struct snd_ctl_elem_value *ucontrol)
295 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
296 struct pmac_tumbler *mix = chip->mixer_data;
297 snd_assert(mix, return -ENODEV);
298 ucontrol->value.integer.value[0] = mix->master_switch[0];
299 ucontrol->value.integer.value[1] = mix->master_switch[1];
303 static int tumbler_put_master_switch(struct snd_kcontrol *kcontrol,
304 struct snd_ctl_elem_value *ucontrol)
306 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
307 struct pmac_tumbler *mix = chip->mixer_data;
310 snd_assert(mix, return -ENODEV);
311 change = mix->master_switch[0] != ucontrol->value.integer.value[0] ||
312 mix->master_switch[1] != ucontrol->value.integer.value[1];
314 mix->master_switch[0] = !!ucontrol->value.integer.value[0];
315 mix->master_switch[1] = !!ucontrol->value.integer.value[1];
316 tumbler_set_master_volume(mix);
323 * TAS3001c dynamic range compression
326 #define TAS3001_DRC_MAX 0x5f
328 static int tumbler_set_drc(struct pmac_tumbler *mix)
330 unsigned char val[2];
332 if (! mix->i2c.client)
335 if (mix->drc_enable) {
336 val[0] = 0xc1; /* enable, 3:1 compression */
337 if (mix->drc_range > TAS3001_DRC_MAX)
339 else if (mix->drc_range < 0)
342 val[1] = mix->drc_range + 0x91;
348 if (i2c_smbus_write_block_data(mix->i2c.client, TAS_REG_DRC,
350 snd_printk("failed to set DRC\n");
360 #define TAS3004_DRC_MAX 0xef
362 static int snapper_set_drc(struct pmac_tumbler *mix)
364 unsigned char val[6];
366 if (! mix->i2c.client)
370 val[0] = 0x50; /* 3:1 above threshold */
372 val[0] = 0x51; /* disabled */
373 val[1] = 0x02; /* 1:1 below threshold */
374 if (mix->drc_range > 0xef)
376 else if (mix->drc_range < 0)
379 val[2] = mix->drc_range;
384 if (i2c_smbus_write_block_data(mix->i2c.client, TAS_REG_DRC,
386 snd_printk("failed to set DRC\n");
392 static int tumbler_info_drc_value(struct snd_kcontrol *kcontrol,
393 struct snd_ctl_elem_info *uinfo)
395 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
396 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
398 uinfo->value.integer.min = 0;
399 uinfo->value.integer.max =
400 chip->model == PMAC_TUMBLER ? TAS3001_DRC_MAX : TAS3004_DRC_MAX;
404 static int tumbler_get_drc_value(struct snd_kcontrol *kcontrol,
405 struct snd_ctl_elem_value *ucontrol)
407 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
408 struct pmac_tumbler *mix;
409 if (! (mix = chip->mixer_data))
411 ucontrol->value.integer.value[0] = mix->drc_range;
415 static int tumbler_put_drc_value(struct snd_kcontrol *kcontrol,
416 struct snd_ctl_elem_value *ucontrol)
418 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
419 struct pmac_tumbler *mix;
422 if (! (mix = chip->mixer_data))
424 change = mix->drc_range != ucontrol->value.integer.value[0];
426 mix->drc_range = ucontrol->value.integer.value[0];
427 if (chip->model == PMAC_TUMBLER)
428 tumbler_set_drc(mix);
430 snapper_set_drc(mix);
435 static int tumbler_get_drc_switch(struct snd_kcontrol *kcontrol,
436 struct snd_ctl_elem_value *ucontrol)
438 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
439 struct pmac_tumbler *mix;
440 if (! (mix = chip->mixer_data))
442 ucontrol->value.integer.value[0] = mix->drc_enable;
446 static int tumbler_put_drc_switch(struct snd_kcontrol *kcontrol,
447 struct snd_ctl_elem_value *ucontrol)
449 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
450 struct pmac_tumbler *mix;
453 if (! (mix = chip->mixer_data))
455 change = mix->drc_enable != ucontrol->value.integer.value[0];
457 mix->drc_enable = !!ucontrol->value.integer.value[0];
458 if (chip->model == PMAC_TUMBLER)
459 tumbler_set_drc(mix);
461 snapper_set_drc(mix);
471 struct tumbler_mono_vol {
479 static int tumbler_set_mono_volume(struct pmac_tumbler *mix,
480 struct tumbler_mono_vol *info)
482 unsigned char block[4];
486 if (! mix->i2c.client)
489 vol = mix->mono_vol[info->index];
490 if (vol >= info->max)
492 vol = info->table[vol];
493 for (i = 0; i < info->bytes; i++)
494 block[i] = (vol >> ((info->bytes - i - 1) * 8)) & 0xff;
495 if (i2c_smbus_write_block_data(mix->i2c.client, info->reg,
496 info->bytes, block) < 0) {
497 snd_printk("failed to set mono volume %d\n", info->index);
503 static int tumbler_info_mono(struct snd_kcontrol *kcontrol,
504 struct snd_ctl_elem_info *uinfo)
506 struct tumbler_mono_vol *info = (struct tumbler_mono_vol *)kcontrol->private_value;
508 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
510 uinfo->value.integer.min = 0;
511 uinfo->value.integer.max = info->max - 1;
515 static int tumbler_get_mono(struct snd_kcontrol *kcontrol,
516 struct snd_ctl_elem_value *ucontrol)
518 struct tumbler_mono_vol *info = (struct tumbler_mono_vol *)kcontrol->private_value;
519 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
520 struct pmac_tumbler *mix;
521 if (! (mix = chip->mixer_data))
523 ucontrol->value.integer.value[0] = mix->mono_vol[info->index];
527 static int tumbler_put_mono(struct snd_kcontrol *kcontrol,
528 struct snd_ctl_elem_value *ucontrol)
530 struct tumbler_mono_vol *info = (struct tumbler_mono_vol *)kcontrol->private_value;
531 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
532 struct pmac_tumbler *mix;
535 if (! (mix = chip->mixer_data))
537 change = mix->mono_vol[info->index] != ucontrol->value.integer.value[0];
539 mix->mono_vol[info->index] = ucontrol->value.integer.value[0];
540 tumbler_set_mono_volume(mix, info);
545 /* TAS3001c mono volumes */
546 static struct tumbler_mono_vol tumbler_pcm_vol_info = {
547 .index = VOL_IDX_PCM_MONO,
550 .max = ARRAY_SIZE(mixer_volume_table),
551 .table = mixer_volume_table,
554 static struct tumbler_mono_vol tumbler_bass_vol_info = {
555 .index = VOL_IDX_BASS,
558 .max = ARRAY_SIZE(bass_volume_table),
559 .table = bass_volume_table,
562 static struct tumbler_mono_vol tumbler_treble_vol_info = {
563 .index = VOL_IDX_TREBLE,
564 .reg = TAS_REG_TREBLE,
566 .max = ARRAY_SIZE(treble_volume_table),
567 .table = treble_volume_table,
570 /* TAS3004 mono volumes */
571 static struct tumbler_mono_vol snapper_bass_vol_info = {
572 .index = VOL_IDX_BASS,
575 .max = ARRAY_SIZE(snapper_bass_volume_table),
576 .table = snapper_bass_volume_table,
579 static struct tumbler_mono_vol snapper_treble_vol_info = {
580 .index = VOL_IDX_TREBLE,
581 .reg = TAS_REG_TREBLE,
583 .max = ARRAY_SIZE(snapper_treble_volume_table),
584 .table = snapper_treble_volume_table,
588 #define DEFINE_MONO(xname,type) { \
589 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,\
591 .info = tumbler_info_mono, \
592 .get = tumbler_get_mono, \
593 .put = tumbler_put_mono, \
594 .private_value = (unsigned long)(&tumbler_##type##_vol_info), \
597 #define DEFINE_SNAPPER_MONO(xname,type) { \
598 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,\
600 .info = tumbler_info_mono, \
601 .get = tumbler_get_mono, \
602 .put = tumbler_put_mono, \
603 .private_value = (unsigned long)(&snapper_##type##_vol_info), \
608 * snapper mixer volumes
611 static int snapper_set_mix_vol1(struct pmac_tumbler *mix, int idx, int ch, int reg)
614 unsigned char block[9];
616 vol = mix->mix_vol[idx][ch];
617 if (vol >= ARRAY_SIZE(mixer_volume_table)) {
618 vol = ARRAY_SIZE(mixer_volume_table) - 1;
619 mix->mix_vol[idx][ch] = vol;
622 for (i = 0; i < 3; i++) {
623 vol = mix->mix_vol[i][ch];
624 vol = mixer_volume_table[vol];
625 for (j = 0; j < 3; j++)
626 block[i * 3 + j] = (vol >> ((2 - j) * 8)) & 0xff;
628 if (i2c_smbus_write_block_data(mix->i2c.client, reg, 9, block) < 0) {
629 snd_printk("failed to set mono volume %d\n", reg);
635 static int snapper_set_mix_vol(struct pmac_tumbler *mix, int idx)
637 if (! mix->i2c.client)
639 if (snapper_set_mix_vol1(mix, idx, 0, TAS_REG_LMIX) < 0 ||
640 snapper_set_mix_vol1(mix, idx, 1, TAS_REG_RMIX) < 0)
645 static int snapper_info_mix(struct snd_kcontrol *kcontrol,
646 struct snd_ctl_elem_info *uinfo)
648 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
650 uinfo->value.integer.min = 0;
651 uinfo->value.integer.max = ARRAY_SIZE(mixer_volume_table) - 1;
655 static int snapper_get_mix(struct snd_kcontrol *kcontrol,
656 struct snd_ctl_elem_value *ucontrol)
658 int idx = (int)kcontrol->private_value;
659 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
660 struct pmac_tumbler *mix;
661 if (! (mix = chip->mixer_data))
663 ucontrol->value.integer.value[0] = mix->mix_vol[idx][0];
664 ucontrol->value.integer.value[1] = mix->mix_vol[idx][1];
668 static int snapper_put_mix(struct snd_kcontrol *kcontrol,
669 struct snd_ctl_elem_value *ucontrol)
671 int idx = (int)kcontrol->private_value;
672 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
673 struct pmac_tumbler *mix;
676 if (! (mix = chip->mixer_data))
678 change = mix->mix_vol[idx][0] != ucontrol->value.integer.value[0] ||
679 mix->mix_vol[idx][1] != ucontrol->value.integer.value[1];
681 mix->mix_vol[idx][0] = ucontrol->value.integer.value[0];
682 mix->mix_vol[idx][1] = ucontrol->value.integer.value[1];
683 snapper_set_mix_vol(mix, idx);
690 * mute switches. FIXME: Turn that into software mute when both outputs are muted
691 * to avoid codec reset on ibook M7
694 enum { TUMBLER_MUTE_HP, TUMBLER_MUTE_AMP, TUMBLER_MUTE_LINE };
696 static int tumbler_get_mute_switch(struct snd_kcontrol *kcontrol,
697 struct snd_ctl_elem_value *ucontrol)
699 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
700 struct pmac_tumbler *mix;
701 struct pmac_gpio *gp;
702 if (! (mix = chip->mixer_data))
704 switch(kcontrol->private_value) {
705 case TUMBLER_MUTE_HP:
706 gp = &mix->hp_mute; break;
707 case TUMBLER_MUTE_AMP:
708 gp = &mix->amp_mute; break;
709 case TUMBLER_MUTE_LINE:
710 gp = &mix->line_mute; break;
716 ucontrol->value.integer.value[0] = !check_audio_gpio(gp);
720 static int tumbler_put_mute_switch(struct snd_kcontrol *kcontrol,
721 struct snd_ctl_elem_value *ucontrol)
723 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
724 struct pmac_tumbler *mix;
725 struct pmac_gpio *gp;
727 #ifdef PMAC_SUPPORT_AUTOMUTE
728 if (chip->update_automute && chip->auto_mute)
729 return 0; /* don't touch in the auto-mute mode */
731 if (! (mix = chip->mixer_data))
733 switch(kcontrol->private_value) {
734 case TUMBLER_MUTE_HP:
735 gp = &mix->hp_mute; break;
736 case TUMBLER_MUTE_AMP:
737 gp = &mix->amp_mute; break;
738 case TUMBLER_MUTE_LINE:
739 gp = &mix->line_mute; break;
745 val = ! check_audio_gpio(gp);
746 if (val != ucontrol->value.integer.value[0]) {
747 write_audio_gpio(gp, ! ucontrol->value.integer.value[0]);
753 static int snapper_set_capture_source(struct pmac_tumbler *mix)
755 if (! mix->i2c.client)
757 if (mix->capture_source)
758 mix->acs = mix->acs |= 2;
761 return i2c_smbus_write_byte_data(mix->i2c.client, TAS_REG_ACS, mix->acs);
764 static int snapper_info_capture_source(struct snd_kcontrol *kcontrol,
765 struct snd_ctl_elem_info *uinfo)
767 static char *texts[2] = {
770 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
772 uinfo->value.enumerated.items = 2;
773 if (uinfo->value.enumerated.item > 1)
774 uinfo->value.enumerated.item = 1;
775 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
779 static int snapper_get_capture_source(struct snd_kcontrol *kcontrol,
780 struct snd_ctl_elem_value *ucontrol)
782 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
783 struct pmac_tumbler *mix = chip->mixer_data;
785 snd_assert(mix, return -ENODEV);
786 ucontrol->value.integer.value[0] = mix->capture_source;
790 static int snapper_put_capture_source(struct snd_kcontrol *kcontrol,
791 struct snd_ctl_elem_value *ucontrol)
793 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
794 struct pmac_tumbler *mix = chip->mixer_data;
797 snd_assert(mix, return -ENODEV);
798 change = ucontrol->value.integer.value[0] != mix->capture_source;
800 mix->capture_source = !!ucontrol->value.integer.value[0];
801 snapper_set_capture_source(mix);
806 #define DEFINE_SNAPPER_MIX(xname,idx,ofs) { \
807 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,\
809 .info = snapper_info_mix, \
810 .get = snapper_get_mix, \
811 .put = snapper_put_mix, \
813 .private_value = ofs, \
819 static struct snd_kcontrol_new tumbler_mixers[] __initdata = {
820 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
821 .name = "Master Playback Volume",
822 .info = tumbler_info_master_volume,
823 .get = tumbler_get_master_volume,
824 .put = tumbler_put_master_volume
826 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
827 .name = "Master Playback Switch",
828 .info = snd_pmac_boolean_stereo_info,
829 .get = tumbler_get_master_switch,
830 .put = tumbler_put_master_switch
832 DEFINE_MONO("Tone Control - Bass", bass),
833 DEFINE_MONO("Tone Control - Treble", treble),
834 DEFINE_MONO("PCM Playback Volume", pcm),
835 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
837 .info = tumbler_info_drc_value,
838 .get = tumbler_get_drc_value,
839 .put = tumbler_put_drc_value
843 static struct snd_kcontrol_new snapper_mixers[] __initdata = {
844 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
845 .name = "Master Playback Volume",
846 .info = tumbler_info_master_volume,
847 .get = tumbler_get_master_volume,
848 .put = tumbler_put_master_volume
850 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
851 .name = "Master Playback Switch",
852 .info = snd_pmac_boolean_stereo_info,
853 .get = tumbler_get_master_switch,
854 .put = tumbler_put_master_switch
856 DEFINE_SNAPPER_MIX("PCM Playback Volume", 0, VOL_IDX_PCM),
857 DEFINE_SNAPPER_MIX("PCM Playback Volume", 1, VOL_IDX_PCM2),
858 DEFINE_SNAPPER_MIX("Monitor Mix Volume", 0, VOL_IDX_ADC),
859 DEFINE_SNAPPER_MONO("Tone Control - Bass", bass),
860 DEFINE_SNAPPER_MONO("Tone Control - Treble", treble),
861 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
863 .info = tumbler_info_drc_value,
864 .get = tumbler_get_drc_value,
865 .put = tumbler_put_drc_value
867 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
868 .name = "Input Source", /* FIXME: "Capture Source" doesn't work properly */
869 .info = snapper_info_capture_source,
870 .get = snapper_get_capture_source,
871 .put = snapper_put_capture_source
875 static struct snd_kcontrol_new tumbler_hp_sw __initdata = {
876 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
877 .name = "Headphone Playback Switch",
878 .info = snd_pmac_boolean_mono_info,
879 .get = tumbler_get_mute_switch,
880 .put = tumbler_put_mute_switch,
881 .private_value = TUMBLER_MUTE_HP,
883 static struct snd_kcontrol_new tumbler_speaker_sw __initdata = {
884 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
885 .name = "PC Speaker Playback Switch",
886 .info = snd_pmac_boolean_mono_info,
887 .get = tumbler_get_mute_switch,
888 .put = tumbler_put_mute_switch,
889 .private_value = TUMBLER_MUTE_AMP,
891 static struct snd_kcontrol_new tumbler_lineout_sw __initdata = {
892 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
893 .name = "Line Out Playback Switch",
894 .info = snd_pmac_boolean_mono_info,
895 .get = tumbler_get_mute_switch,
896 .put = tumbler_put_mute_switch,
897 .private_value = TUMBLER_MUTE_LINE,
899 static struct snd_kcontrol_new tumbler_drc_sw __initdata = {
900 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
901 .name = "DRC Switch",
902 .info = snd_pmac_boolean_mono_info,
903 .get = tumbler_get_drc_switch,
904 .put = tumbler_put_drc_switch
908 #ifdef PMAC_SUPPORT_AUTOMUTE
912 static int tumbler_detect_headphone(struct snd_pmac *chip)
914 struct pmac_tumbler *mix = chip->mixer_data;
917 if (mix->hp_detect.addr)
918 detect |= read_audio_gpio(&mix->hp_detect);
922 static int tumbler_detect_lineout(struct snd_pmac *chip)
924 struct pmac_tumbler *mix = chip->mixer_data;
927 if (mix->line_detect.addr)
928 detect |= read_audio_gpio(&mix->line_detect);
932 static void check_mute(struct snd_pmac *chip, struct pmac_gpio *gp, int val, int do_notify,
933 struct snd_kcontrol *sw)
935 if (check_audio_gpio(gp) != val) {
936 write_audio_gpio(gp, val);
938 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
943 static struct work_struct device_change;
945 static void device_change_handler(void *self)
947 struct snd_pmac *chip = self;
948 struct pmac_tumbler *mix;
949 int headphone, lineout;
954 mix = chip->mixer_data;
955 snd_assert(mix, return);
957 headphone = tumbler_detect_headphone(chip);
958 lineout = tumbler_detect_lineout(chip);
960 DBG("headphone: %d, lineout: %d\n", headphone, lineout);
962 if (headphone || lineout) {
963 /* unmute headphone/lineout & mute speaker */
965 check_mute(chip, &mix->hp_mute, 0, mix->auto_mute_notify,
966 chip->master_sw_ctl);
967 if (lineout && mix->line_mute.addr != 0)
968 check_mute(chip, &mix->line_mute, 0, mix->auto_mute_notify,
969 chip->lineout_sw_ctl);
970 if (mix->anded_reset)
972 check_mute(chip, &mix->amp_mute, 1, mix->auto_mute_notify,
973 chip->speaker_sw_ctl);
975 /* unmute speaker, mute others */
976 check_mute(chip, &mix->amp_mute, 0, mix->auto_mute_notify,
977 chip->speaker_sw_ctl);
978 if (mix->anded_reset)
980 check_mute(chip, &mix->hp_mute, 1, mix->auto_mute_notify,
981 chip->master_sw_ctl);
982 if (mix->line_mute.addr != 0)
983 check_mute(chip, &mix->line_mute, 1, mix->auto_mute_notify,
984 chip->lineout_sw_ctl);
986 if (mix->auto_mute_notify)
987 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
988 &chip->hp_detect_ctl->id);
990 #ifdef CONFIG_SND_POWERMAC_AUTO_DRC
991 mix->drc_enable = ! (headphone || lineout);
992 if (mix->auto_mute_notify)
993 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
994 &chip->drc_sw_ctl->id);
995 if (chip->model == PMAC_TUMBLER)
996 tumbler_set_drc(mix);
998 snapper_set_drc(mix);
1001 /* reset the master volume so the correct amplification is applied */
1002 tumbler_set_master_volume(mix);
1005 static void tumbler_update_automute(struct snd_pmac *chip, int do_notify)
1007 if (chip->auto_mute) {
1008 struct pmac_tumbler *mix;
1009 mix = chip->mixer_data;
1010 snd_assert(mix, return);
1011 mix->auto_mute_notify = do_notify;
1012 schedule_work(&device_change);
1015 #endif /* PMAC_SUPPORT_AUTOMUTE */
1018 /* interrupt - headphone plug changed */
1019 static irqreturn_t headphone_intr(int irq, void *devid, struct pt_regs *regs)
1021 struct snd_pmac *chip = devid;
1022 if (chip->update_automute && chip->initialized) {
1023 chip->update_automute(chip, 1);
1029 /* look for audio-gpio device */
1030 static struct device_node *find_audio_device(const char *name)
1032 struct device_node *np;
1034 if (! (np = find_devices("gpio")))
1037 for (np = np->child; np; np = np->sibling) {
1038 char *property = get_property(np, "audio-gpio", NULL);
1039 if (property && strcmp(property, name) == 0)
1045 /* look for audio-gpio device */
1046 static struct device_node *find_compatible_audio_device(const char *name)
1048 struct device_node *np;
1050 if (! (np = find_devices("gpio")))
1053 for (np = np->child; np; np = np->sibling) {
1054 if (device_is_compatible(np, name))
1060 /* find an audio device and get its address */
1061 static long tumbler_find_device(const char *device, const char *platform,
1062 struct pmac_gpio *gp, int is_compatible)
1064 struct device_node *node;
1068 node = find_compatible_audio_device(device);
1070 node = find_audio_device(device);
1072 DBG("(W) cannot find audio device %s !\n", device);
1073 snd_printdd("cannot find device %s\n", device);
1077 base = (u32 *)get_property(node, "AAPL,address", NULL);
1079 base = (u32 *)get_property(node, "reg", NULL);
1081 DBG("(E) cannot find address for device %s !\n", device);
1082 snd_printd("cannot find address for device %s\n", device);
1091 gp->addr = addr & 0x0000ffff;
1092 /* Try to find the active state, default to 0 ! */
1093 base = (u32 *)get_property(node, "audio-gpio-active-state", NULL);
1095 gp->active_state = *base;
1096 gp->active_val = (*base) ? 0x5 : 0x4;
1097 gp->inactive_val = (*base) ? 0x4 : 0x5;
1100 gp->active_state = 0;
1101 gp->active_val = 0x4;
1102 gp->inactive_val = 0x5;
1103 /* Here are some crude hacks to extract the GPIO polarity and
1104 * open collector informations out of the do-platform script
1105 * as we don't yet have an interpreter for these things
1108 prop = (u32 *)get_property(node, platform, NULL);
1110 if (prop[3] == 0x9 && prop[4] == 0x9) {
1111 gp->active_val = 0xd;
1112 gp->inactive_val = 0xc;
1114 if (prop[3] == 0x1 && prop[4] == 0x1) {
1115 gp->active_val = 0x5;
1116 gp->inactive_val = 0x4;
1121 DBG("(I) GPIO device %s found, offset: %x, active state: %d !\n",
1122 device, gp->addr, gp->active_state);
1124 return (node->n_intrs > 0) ? node->intrs[0].line : 0;
1128 static void tumbler_reset_audio(struct snd_pmac *chip)
1130 struct pmac_tumbler *mix = chip->mixer_data;
1132 if (mix->anded_reset) {
1133 DBG("(I) codec anded reset !\n");
1134 write_audio_gpio(&mix->hp_mute, 0);
1135 write_audio_gpio(&mix->amp_mute, 0);
1137 write_audio_gpio(&mix->hp_mute, 1);
1138 write_audio_gpio(&mix->amp_mute, 1);
1140 write_audio_gpio(&mix->hp_mute, 0);
1141 write_audio_gpio(&mix->amp_mute, 0);
1144 DBG("(I) codec normal reset !\n");
1146 write_audio_gpio(&mix->audio_reset, 0);
1148 write_audio_gpio(&mix->audio_reset, 1);
1150 write_audio_gpio(&mix->audio_reset, 0);
1157 static void tumbler_suspend(struct snd_pmac *chip)
1159 struct pmac_tumbler *mix = chip->mixer_data;
1161 if (mix->headphone_irq >= 0)
1162 disable_irq(mix->headphone_irq);
1163 if (mix->lineout_irq >= 0)
1164 disable_irq(mix->lineout_irq);
1165 mix->save_master_switch[0] = mix->master_switch[0];
1166 mix->save_master_switch[1] = mix->master_switch[1];
1167 mix->save_master_vol[0] = mix->master_vol[0];
1168 mix->save_master_vol[1] = mix->master_vol[1];
1169 mix->master_switch[0] = mix->master_switch[1] = 0;
1170 tumbler_set_master_volume(mix);
1171 if (!mix->anded_reset) {
1172 write_audio_gpio(&mix->amp_mute, 1);
1173 write_audio_gpio(&mix->hp_mute, 1);
1175 if (chip->model == PMAC_SNAPPER) {
1177 i2c_smbus_write_byte_data(mix->i2c.client, TAS_REG_ACS, mix->acs);
1179 if (mix->anded_reset) {
1180 write_audio_gpio(&mix->amp_mute, 1);
1181 write_audio_gpio(&mix->hp_mute, 1);
1183 write_audio_gpio(&mix->audio_reset, 1);
1187 static void tumbler_resume(struct snd_pmac *chip)
1189 struct pmac_tumbler *mix = chip->mixer_data;
1191 snd_assert(mix, return);
1194 mix->master_switch[0] = mix->save_master_switch[0];
1195 mix->master_switch[1] = mix->save_master_switch[1];
1196 mix->master_vol[0] = mix->save_master_vol[0];
1197 mix->master_vol[1] = mix->save_master_vol[1];
1198 tumbler_reset_audio(chip);
1199 if (mix->i2c.client && mix->i2c.init_client) {
1200 if (mix->i2c.init_client(&mix->i2c) < 0)
1201 printk(KERN_ERR "tumbler_init_client error\n");
1203 printk(KERN_ERR "tumbler: i2c is not initialized\n");
1204 if (chip->model == PMAC_TUMBLER) {
1205 tumbler_set_mono_volume(mix, &tumbler_pcm_vol_info);
1206 tumbler_set_mono_volume(mix, &tumbler_bass_vol_info);
1207 tumbler_set_mono_volume(mix, &tumbler_treble_vol_info);
1208 tumbler_set_drc(mix);
1210 snapper_set_mix_vol(mix, VOL_IDX_PCM);
1211 snapper_set_mix_vol(mix, VOL_IDX_PCM2);
1212 snapper_set_mix_vol(mix, VOL_IDX_ADC);
1213 tumbler_set_mono_volume(mix, &snapper_bass_vol_info);
1214 tumbler_set_mono_volume(mix, &snapper_treble_vol_info);
1215 snapper_set_drc(mix);
1216 snapper_set_capture_source(mix);
1218 tumbler_set_master_volume(mix);
1219 if (chip->update_automute)
1220 chip->update_automute(chip, 0);
1221 if (mix->headphone_irq >= 0) {
1224 enable_irq(mix->headphone_irq);
1225 /* activate headphone status interrupts */
1226 val = do_gpio_read(&mix->hp_detect);
1227 do_gpio_write(&mix->hp_detect, val | 0x80);
1229 if (mix->lineout_irq >= 0)
1230 enable_irq(mix->lineout_irq);
1234 /* initialize tumbler */
1235 static int __init tumbler_init(struct snd_pmac *chip)
1238 struct pmac_tumbler *mix = chip->mixer_data;
1239 snd_assert(mix, return -EINVAL);
1241 if (tumbler_find_device("audio-hw-reset",
1242 "platform-do-hw-reset",
1243 &mix->audio_reset, 0) < 0)
1244 tumbler_find_device("hw-reset",
1245 "platform-do-hw-reset",
1246 &mix->audio_reset, 1);
1247 if (tumbler_find_device("amp-mute",
1248 "platform-do-amp-mute",
1249 &mix->amp_mute, 0) < 0)
1250 tumbler_find_device("amp-mute",
1251 "platform-do-amp-mute",
1253 if (tumbler_find_device("headphone-mute",
1254 "platform-do-headphone-mute",
1255 &mix->hp_mute, 0) < 0)
1256 tumbler_find_device("headphone-mute",
1257 "platform-do-headphone-mute",
1259 if (tumbler_find_device("line-output-mute",
1260 "platform-do-lineout-mute",
1261 &mix->line_mute, 0) < 0)
1262 tumbler_find_device("line-output-mute",
1263 "platform-do-lineout-mute",
1264 &mix->line_mute, 1);
1265 irq = tumbler_find_device("headphone-detect",
1266 NULL, &mix->hp_detect, 0);
1268 irq = tumbler_find_device("headphone-detect",
1269 NULL, &mix->hp_detect, 1);
1271 irq = tumbler_find_device("keywest-gpio15",
1272 NULL, &mix->hp_detect, 1);
1273 mix->headphone_irq = irq;
1274 irq = tumbler_find_device("line-output-detect",
1275 NULL, &mix->line_detect, 0);
1277 irq = tumbler_find_device("line-output-detect",
1278 NULL, &mix->line_detect, 1);
1279 mix->lineout_irq = irq;
1281 tumbler_reset_audio(chip);
1286 static void tumbler_cleanup(struct snd_pmac *chip)
1288 struct pmac_tumbler *mix = chip->mixer_data;
1292 if (mix->headphone_irq >= 0)
1293 free_irq(mix->headphone_irq, chip);
1294 if (mix->lineout_irq >= 0)
1295 free_irq(mix->lineout_irq, chip);
1296 tumbler_gpio_free(&mix->audio_reset);
1297 tumbler_gpio_free(&mix->amp_mute);
1298 tumbler_gpio_free(&mix->hp_mute);
1299 tumbler_gpio_free(&mix->hp_detect);
1300 snd_pmac_keywest_cleanup(&mix->i2c);
1302 chip->mixer_data = NULL;
1306 int __init snd_pmac_tumbler_init(struct snd_pmac *chip)
1309 struct pmac_tumbler *mix;
1311 struct device_node *tas_node, *np;
1315 if (current->fs->root)
1316 request_module("i2c-keywest");
1317 #endif /* CONFIG_KMOD */
1319 mix = kmalloc(sizeof(*mix), GFP_KERNEL);
1322 memset(mix, 0, sizeof(*mix));
1323 mix->headphone_irq = -1;
1325 chip->mixer_data = mix;
1326 chip->mixer_free = tumbler_cleanup;
1327 mix->anded_reset = 0;
1328 mix->reset_on_sleep = 1;
1330 for (np = chip->node->child; np; np = np->sibling) {
1331 if (!strcmp(np->name, "sound")) {
1332 if (get_property(np, "has-anded-reset", NULL))
1333 mix->anded_reset = 1;
1334 if (get_property(np, "layout-id", NULL))
1335 mix->reset_on_sleep = 0;
1339 if ((err = tumbler_init(chip)) < 0)
1343 tas_node = find_devices("deq");
1344 if (tas_node == NULL)
1345 tas_node = find_devices("codec");
1346 if (tas_node == NULL)
1349 paddr = (u32 *)get_property(tas_node, "i2c-address", NULL);
1351 paddr = (u32 *)get_property(tas_node, "reg", NULL);
1353 mix->i2c.addr = (*paddr) >> 1;
1355 mix->i2c.addr = TAS_I2C_ADDR;
1357 DBG("(I) TAS i2c address is: %x\n", mix->i2c.addr);
1359 if (chip->model == PMAC_TUMBLER) {
1360 mix->i2c.init_client = tumbler_init_client;
1361 mix->i2c.name = "TAS3001c";
1362 chipname = "Tumbler";
1364 mix->i2c.init_client = snapper_init_client;
1365 mix->i2c.name = "TAS3004";
1366 chipname = "Snapper";
1369 if ((err = snd_pmac_keywest_init(&mix->i2c)) < 0)
1375 sprintf(chip->card->mixername, "PowerMac %s", chipname);
1377 if (chip->model == PMAC_TUMBLER) {
1378 for (i = 0; i < ARRAY_SIZE(tumbler_mixers); i++) {
1379 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&tumbler_mixers[i], chip))) < 0)
1383 for (i = 0; i < ARRAY_SIZE(snapper_mixers); i++) {
1384 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snapper_mixers[i], chip))) < 0)
1388 chip->master_sw_ctl = snd_ctl_new1(&tumbler_hp_sw, chip);
1389 if ((err = snd_ctl_add(chip->card, chip->master_sw_ctl)) < 0)
1391 chip->speaker_sw_ctl = snd_ctl_new1(&tumbler_speaker_sw, chip);
1392 if ((err = snd_ctl_add(chip->card, chip->speaker_sw_ctl)) < 0)
1394 if (mix->line_mute.addr != 0) {
1395 chip->lineout_sw_ctl = snd_ctl_new1(&tumbler_lineout_sw, chip);
1396 if ((err = snd_ctl_add(chip->card, chip->lineout_sw_ctl)) < 0)
1399 chip->drc_sw_ctl = snd_ctl_new1(&tumbler_drc_sw, chip);
1400 if ((err = snd_ctl_add(chip->card, chip->drc_sw_ctl)) < 0)
1403 /* set initial DRC range to 60% */
1404 if (chip->model == PMAC_TUMBLER)
1405 mix->drc_range = (TAS3001_DRC_MAX * 6) / 10;
1407 mix->drc_range = (TAS3004_DRC_MAX * 6) / 10;
1408 mix->drc_enable = 1; /* will be changed later if AUTO_DRC is set */
1409 if (chip->model == PMAC_TUMBLER)
1410 tumbler_set_drc(mix);
1412 snapper_set_drc(mix);
1415 chip->suspend = tumbler_suspend;
1416 chip->resume = tumbler_resume;
1419 INIT_WORK(&device_change, device_change_handler, (void *)chip);
1421 #ifdef PMAC_SUPPORT_AUTOMUTE
1422 if ((mix->headphone_irq >=0 || mix->lineout_irq >= 0)
1423 && (err = snd_pmac_add_automute(chip)) < 0)
1425 chip->detect_headphone = tumbler_detect_headphone;
1426 chip->update_automute = tumbler_update_automute;
1427 tumbler_update_automute(chip, 0); /* update the status only */
1429 /* activate headphone status interrupts */
1430 if (mix->headphone_irq >= 0) {
1432 if ((err = request_irq(mix->headphone_irq, headphone_intr, 0,
1433 "Sound Headphone Detection", chip)) < 0)
1435 /* activate headphone status interrupts */
1436 val = do_gpio_read(&mix->hp_detect);
1437 do_gpio_write(&mix->hp_detect, val | 0x80);
1439 if (mix->lineout_irq >= 0) {
1441 if ((err = request_irq(mix->lineout_irq, headphone_intr, 0,
1442 "Sound Lineout Detection", chip)) < 0)
1444 /* activate headphone status interrupts */
1445 val = do_gpio_read(&mix->line_detect);
1446 do_gpio_write(&mix->line_detect, val | 0x80);