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/kmod.h>
32 #include <linux/slab.h>
33 #include <linux/interrupt.h>
34 #include <sound/core.h>
37 #include <asm/machdep.h>
38 #include <asm/pmac_feature.h>
40 #include "tumbler_volume.h"
45 #define DBG(fmt...) printk(fmt)
50 /* i2c address for tumbler */
51 #define TAS_I2C_ADDR 0x34
54 #define TAS_REG_MCS 0x01 /* main control */
55 #define TAS_REG_DRC 0x02
56 #define TAS_REG_VOL 0x04
57 #define TAS_REG_TREBLE 0x05
58 #define TAS_REG_BASS 0x06
59 #define TAS_REG_INPUT1 0x07
60 #define TAS_REG_INPUT2 0x08
63 #define TAS_REG_PCM TAS_REG_INPUT1
66 #define TAS_REG_LMIX TAS_REG_INPUT1
67 #define TAS_REG_RMIX TAS_REG_INPUT2
68 #define TAS_REG_MCS2 0x43 /* main control 2 */
69 #define TAS_REG_ACS 0x40 /* analog control */
71 /* mono volumes for tas3001c/tas3004 */
73 VOL_IDX_PCM_MONO, /* tas3001c only */
74 VOL_IDX_BASS, VOL_IDX_TREBLE,
78 /* stereo volumes for tas3004 */
80 VOL_IDX_PCM, VOL_IDX_PCM2, VOL_IDX_ADC,
92 struct pmac_keywest i2c;
93 struct pmac_gpio audio_reset;
94 struct pmac_gpio amp_mute;
95 struct pmac_gpio line_mute;
96 struct pmac_gpio line_detect;
97 struct pmac_gpio hp_mute;
98 struct pmac_gpio hp_detect;
101 unsigned int save_master_vol[2];
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(struct pmac_keywest *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(struct pmac_keywest *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(struct pmac_keywest *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(struct pmac_gpio *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(struct pmac_gpio *gp)
191 ret = do_gpio_read(gp);
193 printk("%s addr %x ret %x av %x\n",__FUNCTION__,gp->addr,ret,gp->active_val);
194 return (ret & 0x1) == (gp->active_val & 0x1);
197 static int read_audio_gpio(struct pmac_gpio *gp)
202 ret = do_gpio_read(gp);
203 printk("%s addr %x ret %x av %x\n",__FUNCTION__,gp->addr,ret,gp->active_val);
204 ret = (ret & 0x02) !=0;
205 return ret == gp->active_state;
209 * update master volume
211 static int tumbler_set_master_volume(struct pmac_tumbler *mix)
213 unsigned char block[6];
214 unsigned int left_vol, right_vol;
216 if (! mix->i2c.client)
219 if (! mix->master_switch[0])
222 left_vol = mix->master_vol[0];
223 if (left_vol >= ARRAY_SIZE(master_volume_table))
224 left_vol = ARRAY_SIZE(master_volume_table) - 1;
225 left_vol = master_volume_table[left_vol];
227 if (! mix->master_switch[1])
230 right_vol = mix->master_vol[1];
231 if (right_vol >= ARRAY_SIZE(master_volume_table))
232 right_vol = ARRAY_SIZE(master_volume_table) - 1;
233 right_vol = master_volume_table[right_vol];
236 block[0] = (left_vol >> 16) & 0xff;
237 block[1] = (left_vol >> 8) & 0xff;
238 block[2] = (left_vol >> 0) & 0xff;
240 block[3] = (right_vol >> 16) & 0xff;
241 block[4] = (right_vol >> 8) & 0xff;
242 block[5] = (right_vol >> 0) & 0xff;
244 if (i2c_smbus_write_i2c_block_data(mix->i2c.client, TAS_REG_VOL, 6,
246 snd_printk("failed to set volume \n");
254 static int tumbler_info_master_volume(struct snd_kcontrol *kcontrol,
255 struct snd_ctl_elem_info *uinfo)
257 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
259 uinfo->value.integer.min = 0;
260 uinfo->value.integer.max = ARRAY_SIZE(master_volume_table) - 1;
264 static int tumbler_get_master_volume(struct snd_kcontrol *kcontrol,
265 struct snd_ctl_elem_value *ucontrol)
267 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
268 struct pmac_tumbler *mix = chip->mixer_data;
269 snd_assert(mix, return -ENODEV);
270 ucontrol->value.integer.value[0] = mix->master_vol[0];
271 ucontrol->value.integer.value[1] = mix->master_vol[1];
275 static int tumbler_put_master_volume(struct snd_kcontrol *kcontrol,
276 struct snd_ctl_elem_value *ucontrol)
278 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
279 struct pmac_tumbler *mix = chip->mixer_data;
282 snd_assert(mix, return -ENODEV);
283 change = mix->master_vol[0] != ucontrol->value.integer.value[0] ||
284 mix->master_vol[1] != ucontrol->value.integer.value[1];
286 mix->master_vol[0] = ucontrol->value.integer.value[0];
287 mix->master_vol[1] = ucontrol->value.integer.value[1];
288 tumbler_set_master_volume(mix);
294 static int tumbler_get_master_switch(struct snd_kcontrol *kcontrol,
295 struct snd_ctl_elem_value *ucontrol)
297 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
298 struct pmac_tumbler *mix = chip->mixer_data;
299 snd_assert(mix, return -ENODEV);
300 ucontrol->value.integer.value[0] = mix->master_switch[0];
301 ucontrol->value.integer.value[1] = mix->master_switch[1];
305 static int tumbler_put_master_switch(struct snd_kcontrol *kcontrol,
306 struct snd_ctl_elem_value *ucontrol)
308 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
309 struct pmac_tumbler *mix = chip->mixer_data;
312 snd_assert(mix, return -ENODEV);
313 change = mix->master_switch[0] != ucontrol->value.integer.value[0] ||
314 mix->master_switch[1] != ucontrol->value.integer.value[1];
316 mix->master_switch[0] = !!ucontrol->value.integer.value[0];
317 mix->master_switch[1] = !!ucontrol->value.integer.value[1];
318 tumbler_set_master_volume(mix);
325 * TAS3001c dynamic range compression
328 #define TAS3001_DRC_MAX 0x5f
330 static int tumbler_set_drc(struct pmac_tumbler *mix)
332 unsigned char val[2];
334 if (! mix->i2c.client)
337 if (mix->drc_enable) {
338 val[0] = 0xc1; /* enable, 3:1 compression */
339 if (mix->drc_range > TAS3001_DRC_MAX)
341 else if (mix->drc_range < 0)
344 val[1] = mix->drc_range + 0x91;
350 if (i2c_smbus_write_i2c_block_data(mix->i2c.client, TAS_REG_DRC,
352 snd_printk("failed to set DRC\n");
362 #define TAS3004_DRC_MAX 0xef
364 static int snapper_set_drc(struct pmac_tumbler *mix)
366 unsigned char val[6];
368 if (! mix->i2c.client)
372 val[0] = 0x50; /* 3:1 above threshold */
374 val[0] = 0x51; /* disabled */
375 val[1] = 0x02; /* 1:1 below threshold */
376 if (mix->drc_range > 0xef)
378 else if (mix->drc_range < 0)
381 val[2] = mix->drc_range;
386 if (i2c_smbus_write_i2c_block_data(mix->i2c.client, TAS_REG_DRC,
388 snd_printk("failed to set DRC\n");
394 static int tumbler_info_drc_value(struct snd_kcontrol *kcontrol,
395 struct snd_ctl_elem_info *uinfo)
397 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
398 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
400 uinfo->value.integer.min = 0;
401 uinfo->value.integer.max =
402 chip->model == PMAC_TUMBLER ? TAS3001_DRC_MAX : TAS3004_DRC_MAX;
406 static int tumbler_get_drc_value(struct snd_kcontrol *kcontrol,
407 struct snd_ctl_elem_value *ucontrol)
409 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
410 struct pmac_tumbler *mix;
411 if (! (mix = chip->mixer_data))
413 ucontrol->value.integer.value[0] = mix->drc_range;
417 static int tumbler_put_drc_value(struct snd_kcontrol *kcontrol,
418 struct snd_ctl_elem_value *ucontrol)
420 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
421 struct pmac_tumbler *mix;
424 if (! (mix = chip->mixer_data))
426 change = mix->drc_range != ucontrol->value.integer.value[0];
428 mix->drc_range = ucontrol->value.integer.value[0];
429 if (chip->model == PMAC_TUMBLER)
430 tumbler_set_drc(mix);
432 snapper_set_drc(mix);
437 static int tumbler_get_drc_switch(struct snd_kcontrol *kcontrol,
438 struct snd_ctl_elem_value *ucontrol)
440 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
441 struct pmac_tumbler *mix;
442 if (! (mix = chip->mixer_data))
444 ucontrol->value.integer.value[0] = mix->drc_enable;
448 static int tumbler_put_drc_switch(struct snd_kcontrol *kcontrol,
449 struct snd_ctl_elem_value *ucontrol)
451 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
452 struct pmac_tumbler *mix;
455 if (! (mix = chip->mixer_data))
457 change = mix->drc_enable != ucontrol->value.integer.value[0];
459 mix->drc_enable = !!ucontrol->value.integer.value[0];
460 if (chip->model == PMAC_TUMBLER)
461 tumbler_set_drc(mix);
463 snapper_set_drc(mix);
473 struct tumbler_mono_vol {
481 static int tumbler_set_mono_volume(struct pmac_tumbler *mix,
482 struct tumbler_mono_vol *info)
484 unsigned char block[4];
488 if (! mix->i2c.client)
491 vol = mix->mono_vol[info->index];
492 if (vol >= info->max)
494 vol = info->table[vol];
495 for (i = 0; i < info->bytes; i++)
496 block[i] = (vol >> ((info->bytes - i - 1) * 8)) & 0xff;
497 if (i2c_smbus_write_i2c_block_data(mix->i2c.client, info->reg,
498 info->bytes, block) < 0) {
499 snd_printk("failed to set mono volume %d\n", info->index);
505 static int tumbler_info_mono(struct snd_kcontrol *kcontrol,
506 struct snd_ctl_elem_info *uinfo)
508 struct tumbler_mono_vol *info = (struct tumbler_mono_vol *)kcontrol->private_value;
510 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
512 uinfo->value.integer.min = 0;
513 uinfo->value.integer.max = info->max - 1;
517 static int tumbler_get_mono(struct snd_kcontrol *kcontrol,
518 struct snd_ctl_elem_value *ucontrol)
520 struct tumbler_mono_vol *info = (struct tumbler_mono_vol *)kcontrol->private_value;
521 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
522 struct pmac_tumbler *mix;
523 if (! (mix = chip->mixer_data))
525 ucontrol->value.integer.value[0] = mix->mono_vol[info->index];
529 static int tumbler_put_mono(struct snd_kcontrol *kcontrol,
530 struct snd_ctl_elem_value *ucontrol)
532 struct tumbler_mono_vol *info = (struct tumbler_mono_vol *)kcontrol->private_value;
533 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
534 struct pmac_tumbler *mix;
537 if (! (mix = chip->mixer_data))
539 change = mix->mono_vol[info->index] != ucontrol->value.integer.value[0];
541 mix->mono_vol[info->index] = ucontrol->value.integer.value[0];
542 tumbler_set_mono_volume(mix, info);
547 /* TAS3001c mono volumes */
548 static struct tumbler_mono_vol tumbler_pcm_vol_info = {
549 .index = VOL_IDX_PCM_MONO,
552 .max = ARRAY_SIZE(mixer_volume_table),
553 .table = mixer_volume_table,
556 static struct tumbler_mono_vol tumbler_bass_vol_info = {
557 .index = VOL_IDX_BASS,
560 .max = ARRAY_SIZE(bass_volume_table),
561 .table = bass_volume_table,
564 static struct tumbler_mono_vol tumbler_treble_vol_info = {
565 .index = VOL_IDX_TREBLE,
566 .reg = TAS_REG_TREBLE,
568 .max = ARRAY_SIZE(treble_volume_table),
569 .table = treble_volume_table,
572 /* TAS3004 mono volumes */
573 static struct tumbler_mono_vol snapper_bass_vol_info = {
574 .index = VOL_IDX_BASS,
577 .max = ARRAY_SIZE(snapper_bass_volume_table),
578 .table = snapper_bass_volume_table,
581 static struct tumbler_mono_vol snapper_treble_vol_info = {
582 .index = VOL_IDX_TREBLE,
583 .reg = TAS_REG_TREBLE,
585 .max = ARRAY_SIZE(snapper_treble_volume_table),
586 .table = snapper_treble_volume_table,
590 #define DEFINE_MONO(xname,type) { \
591 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,\
593 .info = tumbler_info_mono, \
594 .get = tumbler_get_mono, \
595 .put = tumbler_put_mono, \
596 .private_value = (unsigned long)(&tumbler_##type##_vol_info), \
599 #define DEFINE_SNAPPER_MONO(xname,type) { \
600 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,\
602 .info = tumbler_info_mono, \
603 .get = tumbler_get_mono, \
604 .put = tumbler_put_mono, \
605 .private_value = (unsigned long)(&snapper_##type##_vol_info), \
610 * snapper mixer volumes
613 static int snapper_set_mix_vol1(struct pmac_tumbler *mix, int idx, int ch, int reg)
616 unsigned char block[9];
618 vol = mix->mix_vol[idx][ch];
619 if (vol >= ARRAY_SIZE(mixer_volume_table)) {
620 vol = ARRAY_SIZE(mixer_volume_table) - 1;
621 mix->mix_vol[idx][ch] = vol;
624 for (i = 0; i < 3; i++) {
625 vol = mix->mix_vol[i][ch];
626 vol = mixer_volume_table[vol];
627 for (j = 0; j < 3; j++)
628 block[i * 3 + j] = (vol >> ((2 - j) * 8)) & 0xff;
630 if (i2c_smbus_write_i2c_block_data(mix->i2c.client, reg,
632 snd_printk("failed to set mono volume %d\n", reg);
638 static int snapper_set_mix_vol(struct pmac_tumbler *mix, int idx)
640 if (! mix->i2c.client)
642 if (snapper_set_mix_vol1(mix, idx, 0, TAS_REG_LMIX) < 0 ||
643 snapper_set_mix_vol1(mix, idx, 1, TAS_REG_RMIX) < 0)
648 static int snapper_info_mix(struct snd_kcontrol *kcontrol,
649 struct snd_ctl_elem_info *uinfo)
651 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
653 uinfo->value.integer.min = 0;
654 uinfo->value.integer.max = ARRAY_SIZE(mixer_volume_table) - 1;
658 static int snapper_get_mix(struct snd_kcontrol *kcontrol,
659 struct snd_ctl_elem_value *ucontrol)
661 int idx = (int)kcontrol->private_value;
662 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
663 struct pmac_tumbler *mix;
664 if (! (mix = chip->mixer_data))
666 ucontrol->value.integer.value[0] = mix->mix_vol[idx][0];
667 ucontrol->value.integer.value[1] = mix->mix_vol[idx][1];
671 static int snapper_put_mix(struct snd_kcontrol *kcontrol,
672 struct snd_ctl_elem_value *ucontrol)
674 int idx = (int)kcontrol->private_value;
675 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
676 struct pmac_tumbler *mix;
679 if (! (mix = chip->mixer_data))
681 change = mix->mix_vol[idx][0] != ucontrol->value.integer.value[0] ||
682 mix->mix_vol[idx][1] != ucontrol->value.integer.value[1];
684 mix->mix_vol[idx][0] = ucontrol->value.integer.value[0];
685 mix->mix_vol[idx][1] = ucontrol->value.integer.value[1];
686 snapper_set_mix_vol(mix, idx);
693 * mute switches. FIXME: Turn that into software mute when both outputs are muted
694 * to avoid codec reset on ibook M7
697 enum { TUMBLER_MUTE_HP, TUMBLER_MUTE_AMP, TUMBLER_MUTE_LINE };
699 static int tumbler_get_mute_switch(struct snd_kcontrol *kcontrol,
700 struct snd_ctl_elem_value *ucontrol)
702 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
703 struct pmac_tumbler *mix;
704 struct pmac_gpio *gp;
705 if (! (mix = chip->mixer_data))
707 switch(kcontrol->private_value) {
708 case TUMBLER_MUTE_HP:
709 gp = &mix->hp_mute; break;
710 case TUMBLER_MUTE_AMP:
711 gp = &mix->amp_mute; break;
712 case TUMBLER_MUTE_LINE:
713 gp = &mix->line_mute; break;
719 ucontrol->value.integer.value[0] = !check_audio_gpio(gp);
723 static int tumbler_put_mute_switch(struct snd_kcontrol *kcontrol,
724 struct snd_ctl_elem_value *ucontrol)
726 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
727 struct pmac_tumbler *mix;
728 struct pmac_gpio *gp;
730 #ifdef PMAC_SUPPORT_AUTOMUTE
731 if (chip->update_automute && chip->auto_mute)
732 return 0; /* don't touch in the auto-mute mode */
734 if (! (mix = chip->mixer_data))
736 switch(kcontrol->private_value) {
737 case TUMBLER_MUTE_HP:
738 gp = &mix->hp_mute; break;
739 case TUMBLER_MUTE_AMP:
740 gp = &mix->amp_mute; break;
741 case TUMBLER_MUTE_LINE:
742 gp = &mix->line_mute; break;
748 val = ! check_audio_gpio(gp);
749 if (val != ucontrol->value.integer.value[0]) {
750 write_audio_gpio(gp, ! ucontrol->value.integer.value[0]);
756 static int snapper_set_capture_source(struct pmac_tumbler *mix)
758 if (! mix->i2c.client)
760 if (mix->capture_source)
761 mix->acs = mix->acs |= 2;
764 return i2c_smbus_write_byte_data(mix->i2c.client, TAS_REG_ACS, mix->acs);
767 static int snapper_info_capture_source(struct snd_kcontrol *kcontrol,
768 struct snd_ctl_elem_info *uinfo)
770 static char *texts[2] = {
773 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
775 uinfo->value.enumerated.items = 2;
776 if (uinfo->value.enumerated.item > 1)
777 uinfo->value.enumerated.item = 1;
778 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
782 static int snapper_get_capture_source(struct snd_kcontrol *kcontrol,
783 struct snd_ctl_elem_value *ucontrol)
785 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
786 struct pmac_tumbler *mix = chip->mixer_data;
788 snd_assert(mix, return -ENODEV);
789 ucontrol->value.integer.value[0] = mix->capture_source;
793 static int snapper_put_capture_source(struct snd_kcontrol *kcontrol,
794 struct snd_ctl_elem_value *ucontrol)
796 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
797 struct pmac_tumbler *mix = chip->mixer_data;
800 snd_assert(mix, return -ENODEV);
801 change = ucontrol->value.integer.value[0] != mix->capture_source;
803 mix->capture_source = !!ucontrol->value.integer.value[0];
804 snapper_set_capture_source(mix);
809 #define DEFINE_SNAPPER_MIX(xname,idx,ofs) { \
810 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,\
812 .info = snapper_info_mix, \
813 .get = snapper_get_mix, \
814 .put = snapper_put_mix, \
816 .private_value = ofs, \
822 static struct snd_kcontrol_new tumbler_mixers[] __initdata = {
823 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
824 .name = "Master Playback Volume",
825 .info = tumbler_info_master_volume,
826 .get = tumbler_get_master_volume,
827 .put = tumbler_put_master_volume
829 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
830 .name = "Master Playback Switch",
831 .info = snd_pmac_boolean_stereo_info,
832 .get = tumbler_get_master_switch,
833 .put = tumbler_put_master_switch
835 DEFINE_MONO("Tone Control - Bass", bass),
836 DEFINE_MONO("Tone Control - Treble", treble),
837 DEFINE_MONO("PCM Playback Volume", pcm),
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
846 static struct snd_kcontrol_new snapper_mixers[] __initdata = {
847 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
848 .name = "Master Playback Volume",
849 .info = tumbler_info_master_volume,
850 .get = tumbler_get_master_volume,
851 .put = tumbler_put_master_volume
853 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
854 .name = "Master Playback Switch",
855 .info = snd_pmac_boolean_stereo_info,
856 .get = tumbler_get_master_switch,
857 .put = tumbler_put_master_switch
859 DEFINE_SNAPPER_MIX("PCM Playback Volume", 0, VOL_IDX_PCM),
860 DEFINE_SNAPPER_MIX("PCM Playback Volume", 1, VOL_IDX_PCM2),
861 DEFINE_SNAPPER_MIX("Monitor Mix Volume", 0, VOL_IDX_ADC),
862 DEFINE_SNAPPER_MONO("Tone Control - Bass", bass),
863 DEFINE_SNAPPER_MONO("Tone Control - Treble", treble),
864 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
866 .info = tumbler_info_drc_value,
867 .get = tumbler_get_drc_value,
868 .put = tumbler_put_drc_value
870 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
871 .name = "Input Source", /* FIXME: "Capture Source" doesn't work properly */
872 .info = snapper_info_capture_source,
873 .get = snapper_get_capture_source,
874 .put = snapper_put_capture_source
878 static struct snd_kcontrol_new tumbler_hp_sw __initdata = {
879 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
880 .name = "Headphone Playback Switch",
881 .info = snd_pmac_boolean_mono_info,
882 .get = tumbler_get_mute_switch,
883 .put = tumbler_put_mute_switch,
884 .private_value = TUMBLER_MUTE_HP,
886 static struct snd_kcontrol_new tumbler_speaker_sw __initdata = {
887 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
888 .name = "PC Speaker Playback Switch",
889 .info = snd_pmac_boolean_mono_info,
890 .get = tumbler_get_mute_switch,
891 .put = tumbler_put_mute_switch,
892 .private_value = TUMBLER_MUTE_AMP,
894 static struct snd_kcontrol_new tumbler_lineout_sw __initdata = {
895 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
896 .name = "Line Out Playback Switch",
897 .info = snd_pmac_boolean_mono_info,
898 .get = tumbler_get_mute_switch,
899 .put = tumbler_put_mute_switch,
900 .private_value = TUMBLER_MUTE_LINE,
902 static struct snd_kcontrol_new tumbler_drc_sw __initdata = {
903 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
904 .name = "DRC Switch",
905 .info = snd_pmac_boolean_mono_info,
906 .get = tumbler_get_drc_switch,
907 .put = tumbler_put_drc_switch
911 #ifdef PMAC_SUPPORT_AUTOMUTE
915 static int tumbler_detect_headphone(struct snd_pmac *chip)
917 struct pmac_tumbler *mix = chip->mixer_data;
920 if (mix->hp_detect.addr)
921 detect |= read_audio_gpio(&mix->hp_detect);
925 static int tumbler_detect_lineout(struct snd_pmac *chip)
927 struct pmac_tumbler *mix = chip->mixer_data;
930 if (mix->line_detect.addr)
931 detect |= read_audio_gpio(&mix->line_detect);
935 static void check_mute(struct snd_pmac *chip, struct pmac_gpio *gp, int val, int do_notify,
936 struct snd_kcontrol *sw)
938 if (check_audio_gpio(gp) != val) {
939 write_audio_gpio(gp, val);
941 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
946 static struct work_struct device_change;
948 static void device_change_handler(void *self)
950 struct snd_pmac *chip = self;
951 struct pmac_tumbler *mix;
952 int headphone, lineout;
957 mix = chip->mixer_data;
958 snd_assert(mix, return);
960 headphone = tumbler_detect_headphone(chip);
961 lineout = tumbler_detect_lineout(chip);
963 printk("headphone: %d, lineout: %d\n", headphone, lineout);
965 if (headphone || lineout) {
966 /* unmute headphone/lineout & mute speaker */
968 check_mute(chip, &mix->hp_mute, 0, mix->auto_mute_notify,
969 chip->master_sw_ctl);
970 if (lineout && mix->line_mute.addr != 0)
971 check_mute(chip, &mix->line_mute, 0, mix->auto_mute_notify,
972 chip->lineout_sw_ctl);
973 if (mix->anded_reset)
975 check_mute(chip, &mix->amp_mute, 1, mix->auto_mute_notify,
976 chip->speaker_sw_ctl);
978 /* unmute speaker, mute others */
979 check_mute(chip, &mix->amp_mute, 0, mix->auto_mute_notify,
980 chip->speaker_sw_ctl);
981 if (mix->anded_reset)
983 check_mute(chip, &mix->hp_mute, 1, mix->auto_mute_notify,
984 chip->master_sw_ctl);
985 if (mix->line_mute.addr != 0)
986 check_mute(chip, &mix->line_mute, 1, mix->auto_mute_notify,
987 chip->lineout_sw_ctl);
989 if (mix->auto_mute_notify)
990 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
991 &chip->hp_detect_ctl->id);
993 #ifdef CONFIG_SND_POWERMAC_AUTO_DRC
994 mix->drc_enable = ! (headphone || lineout);
995 if (mix->auto_mute_notify)
996 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
997 &chip->drc_sw_ctl->id);
998 if (chip->model == PMAC_TUMBLER)
999 tumbler_set_drc(mix);
1001 snapper_set_drc(mix);
1004 /* reset the master volume so the correct amplification is applied */
1005 tumbler_set_master_volume(mix);
1008 static void tumbler_update_automute(struct snd_pmac *chip, int do_notify)
1010 if (chip->auto_mute) {
1011 struct pmac_tumbler *mix;
1012 mix = chip->mixer_data;
1013 snd_assert(mix, return);
1014 mix->auto_mute_notify = do_notify;
1015 schedule_work(&device_change);
1018 #endif /* PMAC_SUPPORT_AUTOMUTE */
1021 /* interrupt - headphone plug changed */
1022 static irqreturn_t headphone_intr(int irq, void *devid, struct pt_regs *regs)
1024 struct snd_pmac *chip = devid;
1025 if (chip->update_automute && chip->initialized) {
1026 chip->update_automute(chip, 1);
1032 /* look for audio-gpio device */
1033 static struct device_node *find_audio_device(const char *name)
1035 struct device_node *np;
1037 if (! (np = find_devices("gpio")))
1040 for (np = np->child; np; np = np->sibling) {
1041 char *property = get_property(np, "audio-gpio", NULL);
1042 if (property && strcmp(property, name) == 0)
1048 /* look for audio-gpio device */
1049 static struct device_node *find_compatible_audio_device(const char *name)
1051 struct device_node *np;
1053 if (! (np = find_devices("gpio")))
1056 for (np = np->child; np; np = np->sibling) {
1057 if (device_is_compatible(np, name))
1063 /* find an audio device and get its address */
1064 static long tumbler_find_device(const char *device, const char *platform,
1065 struct pmac_gpio *gp, int is_compatible)
1067 struct device_node *node;
1071 node = find_compatible_audio_device(device);
1073 node = find_audio_device(device);
1075 DBG("(W) cannot find audio device %s !\n", device);
1076 snd_printdd("cannot find device %s\n", device);
1080 base = (u32 *)get_property(node, "AAPL,address", NULL);
1082 base = (u32 *)get_property(node, "reg", NULL);
1084 DBG("(E) cannot find address for device %s !\n", device);
1085 snd_printd("cannot find address for device %s\n", device);
1094 gp->addr = addr & 0x0000ffff;
1095 /* Try to find the active state, default to 0 ! */
1096 base = (u32 *)get_property(node, "audio-gpio-active-state", NULL);
1098 gp->active_state = *base;
1099 gp->active_val = (*base) ? 0x5 : 0x4;
1100 gp->inactive_val = (*base) ? 0x4 : 0x5;
1103 gp->active_state = 0;
1104 gp->active_val = 0x4;
1105 gp->inactive_val = 0x5;
1106 /* Here are some crude hacks to extract the GPIO polarity and
1107 * open collector informations out of the do-platform script
1108 * as we don't yet have an interpreter for these things
1111 prop = (u32 *)get_property(node, platform, NULL);
1113 if (prop[3] == 0x9 && prop[4] == 0x9) {
1114 gp->active_val = 0xd;
1115 gp->inactive_val = 0xc;
1117 if (prop[3] == 0x1 && prop[4] == 0x1) {
1118 gp->active_val = 0x5;
1119 gp->inactive_val = 0x4;
1124 DBG("(I) GPIO device %s found, offset: %x, active state: %d !\n",
1125 device, gp->addr, gp->active_state);
1127 return (node->n_intrs > 0) ? node->intrs[0].line : 0;
1131 static void tumbler_reset_audio(struct snd_pmac *chip)
1133 struct pmac_tumbler *mix = chip->mixer_data;
1135 if (mix->anded_reset) {
1136 DBG("(I) codec anded reset !\n");
1137 write_audio_gpio(&mix->hp_mute, 0);
1138 write_audio_gpio(&mix->amp_mute, 0);
1140 write_audio_gpio(&mix->hp_mute, 1);
1141 write_audio_gpio(&mix->amp_mute, 1);
1143 write_audio_gpio(&mix->hp_mute, 0);
1144 write_audio_gpio(&mix->amp_mute, 0);
1147 DBG("(I) codec normal reset !\n");
1149 write_audio_gpio(&mix->audio_reset, 0);
1151 write_audio_gpio(&mix->audio_reset, 1);
1153 write_audio_gpio(&mix->audio_reset, 0);
1160 static void tumbler_suspend(struct snd_pmac *chip)
1162 struct pmac_tumbler *mix = chip->mixer_data;
1164 if (mix->headphone_irq >= 0)
1165 disable_irq(mix->headphone_irq);
1166 if (mix->lineout_irq >= 0)
1167 disable_irq(mix->lineout_irq);
1168 mix->save_master_switch[0] = mix->master_switch[0];
1169 mix->save_master_switch[1] = mix->master_switch[1];
1170 mix->save_master_vol[0] = mix->master_vol[0];
1171 mix->save_master_vol[1] = mix->master_vol[1];
1172 mix->master_switch[0] = mix->master_switch[1] = 0;
1173 tumbler_set_master_volume(mix);
1174 if (!mix->anded_reset) {
1175 write_audio_gpio(&mix->amp_mute, 1);
1176 write_audio_gpio(&mix->hp_mute, 1);
1178 if (chip->model == PMAC_SNAPPER) {
1180 i2c_smbus_write_byte_data(mix->i2c.client, TAS_REG_ACS, mix->acs);
1182 if (mix->anded_reset) {
1183 write_audio_gpio(&mix->amp_mute, 1);
1184 write_audio_gpio(&mix->hp_mute, 1);
1186 write_audio_gpio(&mix->audio_reset, 1);
1190 static void tumbler_resume(struct snd_pmac *chip)
1192 struct pmac_tumbler *mix = chip->mixer_data;
1194 snd_assert(mix, return);
1197 mix->master_switch[0] = mix->save_master_switch[0];
1198 mix->master_switch[1] = mix->save_master_switch[1];
1199 mix->master_vol[0] = mix->save_master_vol[0];
1200 mix->master_vol[1] = mix->save_master_vol[1];
1201 tumbler_reset_audio(chip);
1202 if (mix->i2c.client && mix->i2c.init_client) {
1203 if (mix->i2c.init_client(&mix->i2c) < 0)
1204 printk(KERN_ERR "tumbler_init_client error\n");
1206 printk(KERN_ERR "tumbler: i2c is not initialized\n");
1207 if (chip->model == PMAC_TUMBLER) {
1208 tumbler_set_mono_volume(mix, &tumbler_pcm_vol_info);
1209 tumbler_set_mono_volume(mix, &tumbler_bass_vol_info);
1210 tumbler_set_mono_volume(mix, &tumbler_treble_vol_info);
1211 tumbler_set_drc(mix);
1213 snapper_set_mix_vol(mix, VOL_IDX_PCM);
1214 snapper_set_mix_vol(mix, VOL_IDX_PCM2);
1215 snapper_set_mix_vol(mix, VOL_IDX_ADC);
1216 tumbler_set_mono_volume(mix, &snapper_bass_vol_info);
1217 tumbler_set_mono_volume(mix, &snapper_treble_vol_info);
1218 snapper_set_drc(mix);
1219 snapper_set_capture_source(mix);
1221 tumbler_set_master_volume(mix);
1222 if (chip->update_automute)
1223 chip->update_automute(chip, 0);
1224 if (mix->headphone_irq >= 0) {
1227 enable_irq(mix->headphone_irq);
1228 /* activate headphone status interrupts */
1229 val = do_gpio_read(&mix->hp_detect);
1230 do_gpio_write(&mix->hp_detect, val | 0x80);
1232 if (mix->lineout_irq >= 0)
1233 enable_irq(mix->lineout_irq);
1237 /* initialize tumbler */
1238 static int __init tumbler_init(struct snd_pmac *chip)
1241 struct pmac_tumbler *mix = chip->mixer_data;
1242 snd_assert(mix, return -EINVAL);
1244 if (tumbler_find_device("audio-hw-reset",
1245 "platform-do-hw-reset",
1246 &mix->audio_reset, 0) < 0)
1247 tumbler_find_device("hw-reset",
1248 "platform-do-hw-reset",
1249 &mix->audio_reset, 1);
1250 if (tumbler_find_device("amp-mute",
1251 "platform-do-amp-mute",
1252 &mix->amp_mute, 0) < 0)
1253 tumbler_find_device("amp-mute",
1254 "platform-do-amp-mute",
1256 if (tumbler_find_device("headphone-mute",
1257 "platform-do-headphone-mute",
1258 &mix->hp_mute, 0) < 0)
1259 tumbler_find_device("headphone-mute",
1260 "platform-do-headphone-mute",
1262 if (tumbler_find_device("line-output-mute",
1263 "platform-do-lineout-mute",
1264 &mix->line_mute, 0) < 0)
1265 tumbler_find_device("line-output-mute",
1266 "platform-do-lineout-mute",
1267 &mix->line_mute, 1);
1268 irq = tumbler_find_device("headphone-detect",
1269 NULL, &mix->hp_detect, 0);
1271 irq = tumbler_find_device("headphone-detect",
1272 NULL, &mix->hp_detect, 1);
1274 irq = tumbler_find_device("keywest-gpio15",
1275 NULL, &mix->hp_detect, 1);
1276 mix->headphone_irq = irq;
1277 irq = tumbler_find_device("line-output-detect",
1278 NULL, &mix->line_detect, 0);
1280 irq = tumbler_find_device("line-output-detect",
1281 NULL, &mix->line_detect, 1);
1282 mix->lineout_irq = irq;
1284 tumbler_reset_audio(chip);
1289 static void tumbler_cleanup(struct snd_pmac *chip)
1291 struct pmac_tumbler *mix = chip->mixer_data;
1295 if (mix->headphone_irq >= 0)
1296 free_irq(mix->headphone_irq, chip);
1297 if (mix->lineout_irq >= 0)
1298 free_irq(mix->lineout_irq, chip);
1299 tumbler_gpio_free(&mix->audio_reset);
1300 tumbler_gpio_free(&mix->amp_mute);
1301 tumbler_gpio_free(&mix->hp_mute);
1302 tumbler_gpio_free(&mix->hp_detect);
1303 snd_pmac_keywest_cleanup(&mix->i2c);
1305 chip->mixer_data = NULL;
1309 int __init snd_pmac_tumbler_init(struct snd_pmac *chip)
1312 struct pmac_tumbler *mix;
1314 struct device_node *tas_node, *np;
1318 if (current->fs->root)
1319 request_module("i2c-powermac");
1320 #endif /* CONFIG_KMOD */
1322 mix = kmalloc(sizeof(*mix), GFP_KERNEL);
1325 memset(mix, 0, sizeof(*mix));
1326 mix->headphone_irq = -1;
1328 chip->mixer_data = mix;
1329 chip->mixer_free = tumbler_cleanup;
1330 mix->anded_reset = 0;
1331 mix->reset_on_sleep = 1;
1333 for (np = chip->node->child; np; np = np->sibling) {
1334 if (!strcmp(np->name, "sound")) {
1335 if (get_property(np, "has-anded-reset", NULL))
1336 mix->anded_reset = 1;
1337 if (get_property(np, "layout-id", NULL))
1338 mix->reset_on_sleep = 0;
1342 if ((err = tumbler_init(chip)) < 0)
1346 tas_node = find_devices("deq");
1347 if (tas_node == NULL)
1348 tas_node = find_devices("codec");
1349 if (tas_node == NULL)
1352 paddr = (u32 *)get_property(tas_node, "i2c-address", NULL);
1354 paddr = (u32 *)get_property(tas_node, "reg", NULL);
1356 mix->i2c.addr = (*paddr) >> 1;
1358 mix->i2c.addr = TAS_I2C_ADDR;
1360 DBG("(I) TAS i2c address is: %x\n", mix->i2c.addr);
1362 if (chip->model == PMAC_TUMBLER) {
1363 mix->i2c.init_client = tumbler_init_client;
1364 mix->i2c.name = "TAS3001c";
1365 chipname = "Tumbler";
1367 mix->i2c.init_client = snapper_init_client;
1368 mix->i2c.name = "TAS3004";
1369 chipname = "Snapper";
1372 if ((err = snd_pmac_keywest_init(&mix->i2c)) < 0)
1378 sprintf(chip->card->mixername, "PowerMac %s", chipname);
1380 if (chip->model == PMAC_TUMBLER) {
1381 for (i = 0; i < ARRAY_SIZE(tumbler_mixers); i++) {
1382 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&tumbler_mixers[i], chip))) < 0)
1386 for (i = 0; i < ARRAY_SIZE(snapper_mixers); i++) {
1387 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snapper_mixers[i], chip))) < 0)
1391 chip->master_sw_ctl = snd_ctl_new1(&tumbler_hp_sw, chip);
1392 if ((err = snd_ctl_add(chip->card, chip->master_sw_ctl)) < 0)
1394 chip->speaker_sw_ctl = snd_ctl_new1(&tumbler_speaker_sw, chip);
1395 if ((err = snd_ctl_add(chip->card, chip->speaker_sw_ctl)) < 0)
1397 if (mix->line_mute.addr != 0) {
1398 chip->lineout_sw_ctl = snd_ctl_new1(&tumbler_lineout_sw, chip);
1399 if ((err = snd_ctl_add(chip->card, chip->lineout_sw_ctl)) < 0)
1402 chip->drc_sw_ctl = snd_ctl_new1(&tumbler_drc_sw, chip);
1403 if ((err = snd_ctl_add(chip->card, chip->drc_sw_ctl)) < 0)
1406 /* set initial DRC range to 60% */
1407 if (chip->model == PMAC_TUMBLER)
1408 mix->drc_range = (TAS3001_DRC_MAX * 6) / 10;
1410 mix->drc_range = (TAS3004_DRC_MAX * 6) / 10;
1411 mix->drc_enable = 1; /* will be changed later if AUTO_DRC is set */
1412 if (chip->model == PMAC_TUMBLER)
1413 tumbler_set_drc(mix);
1415 snapper_set_drc(mix);
1418 chip->suspend = tumbler_suspend;
1419 chip->resume = tumbler_resume;
1422 INIT_WORK(&device_change, device_change_handler, (void *)chip);
1424 #ifdef PMAC_SUPPORT_AUTOMUTE
1425 if ((mix->headphone_irq >=0 || mix->lineout_irq >= 0)
1426 && (err = snd_pmac_add_automute(chip)) < 0)
1428 chip->detect_headphone = tumbler_detect_headphone;
1429 chip->update_automute = tumbler_update_automute;
1430 tumbler_update_automute(chip, 0); /* update the status only */
1432 /* activate headphone status interrupts */
1433 if (mix->headphone_irq >= 0) {
1435 if ((err = request_irq(mix->headphone_irq, headphone_intr, 0,
1436 "Sound Headphone Detection", chip)) < 0)
1438 /* activate headphone status interrupts */
1439 val = do_gpio_read(&mix->hp_detect);
1440 do_gpio_write(&mix->hp_detect, val | 0x80);
1442 if (mix->lineout_irq >= 0) {
1444 if ((err = request_irq(mix->lineout_irq, headphone_intr, 0,
1445 "Sound Lineout Detection", chip)) < 0)
1447 /* activate headphone status interrupts */
1448 val = do_gpio_read(&mix->line_detect);
1449 do_gpio_write(&mix->line_detect, val | 0x80);