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 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(pmac_keywest_t
*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(pmac_keywest_t
*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(pmac_keywest_t
*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(pmac_gpio_t
*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(pmac_gpio_t
*gp
)
192 ret
= do_gpio_read(gp
);
194 return (ret
& 0xd) == (gp
->active_val
& 0xd);
197 static int read_audio_gpio(pmac_gpio_t
*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(pmac_tumbler_t
*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(snd_kcontrol_t
*kcontrol
, snd_ctl_elem_info_t
*uinfo
)
254 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
256 uinfo
->value
.integer
.min
= 0;
257 uinfo
->value
.integer
.max
= ARRAY_SIZE(master_volume_table
) - 1;
261 static int tumbler_get_master_volume(snd_kcontrol_t
*kcontrol
, snd_ctl_elem_value_t
*ucontrol
)
263 pmac_t
*chip
= snd_kcontrol_chip(kcontrol
);
264 pmac_tumbler_t
*mix
= chip
->mixer_data
;
265 snd_assert(mix
, return -ENODEV
);
266 ucontrol
->value
.integer
.value
[0] = mix
->master_vol
[0];
267 ucontrol
->value
.integer
.value
[1] = mix
->master_vol
[1];
271 static int tumbler_put_master_volume(snd_kcontrol_t
*kcontrol
, snd_ctl_elem_value_t
*ucontrol
)
273 pmac_t
*chip
= snd_kcontrol_chip(kcontrol
);
274 pmac_tumbler_t
*mix
= chip
->mixer_data
;
277 snd_assert(mix
, return -ENODEV
);
278 change
= mix
->master_vol
[0] != ucontrol
->value
.integer
.value
[0] ||
279 mix
->master_vol
[1] != ucontrol
->value
.integer
.value
[1];
281 mix
->master_vol
[0] = ucontrol
->value
.integer
.value
[0];
282 mix
->master_vol
[1] = ucontrol
->value
.integer
.value
[1];
283 tumbler_set_master_volume(mix
);
289 static int tumbler_get_master_switch(snd_kcontrol_t
*kcontrol
, snd_ctl_elem_value_t
*ucontrol
)
291 pmac_t
*chip
= snd_kcontrol_chip(kcontrol
);
292 pmac_tumbler_t
*mix
= chip
->mixer_data
;
293 snd_assert(mix
, return -ENODEV
);
294 ucontrol
->value
.integer
.value
[0] = mix
->master_switch
[0];
295 ucontrol
->value
.integer
.value
[1] = mix
->master_switch
[1];
299 static int tumbler_put_master_switch(snd_kcontrol_t
*kcontrol
, snd_ctl_elem_value_t
*ucontrol
)
301 pmac_t
*chip
= snd_kcontrol_chip(kcontrol
);
302 pmac_tumbler_t
*mix
= chip
->mixer_data
;
305 snd_assert(mix
, return -ENODEV
);
306 change
= mix
->master_switch
[0] != ucontrol
->value
.integer
.value
[0] ||
307 mix
->master_switch
[1] != ucontrol
->value
.integer
.value
[1];
309 mix
->master_switch
[0] = !!ucontrol
->value
.integer
.value
[0];
310 mix
->master_switch
[1] = !!ucontrol
->value
.integer
.value
[1];
311 tumbler_set_master_volume(mix
);
318 * TAS3001c dynamic range compression
321 #define TAS3001_DRC_MAX 0x5f
323 static int tumbler_set_drc(pmac_tumbler_t
*mix
)
325 unsigned char val
[2];
327 if (! mix
->i2c
.client
)
330 if (mix
->drc_enable
) {
331 val
[0] = 0xc1; /* enable, 3:1 compression */
332 if (mix
->drc_range
> TAS3001_DRC_MAX
)
334 else if (mix
->drc_range
< 0)
337 val
[1] = mix
->drc_range
+ 0x91;
343 if (i2c_smbus_write_block_data(mix
->i2c
.client
, TAS_REG_DRC
,
345 snd_printk("failed to set DRC\n");
355 #define TAS3004_DRC_MAX 0xef
357 static int snapper_set_drc(pmac_tumbler_t
*mix
)
359 unsigned char val
[6];
361 if (! mix
->i2c
.client
)
365 val
[0] = 0x50; /* 3:1 above threshold */
367 val
[0] = 0x51; /* disabled */
368 val
[1] = 0x02; /* 1:1 below threshold */
369 if (mix
->drc_range
> 0xef)
371 else if (mix
->drc_range
< 0)
374 val
[2] = mix
->drc_range
;
379 if (i2c_smbus_write_block_data(mix
->i2c
.client
, TAS_REG_DRC
,
381 snd_printk("failed to set DRC\n");
387 static int tumbler_info_drc_value(snd_kcontrol_t
*kcontrol
, snd_ctl_elem_info_t
*uinfo
)
389 pmac_t
*chip
= snd_kcontrol_chip(kcontrol
);
390 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
392 uinfo
->value
.integer
.min
= 0;
393 uinfo
->value
.integer
.max
=
394 chip
->model
== PMAC_TUMBLER
? TAS3001_DRC_MAX
: TAS3004_DRC_MAX
;
398 static int tumbler_get_drc_value(snd_kcontrol_t
*kcontrol
, snd_ctl_elem_value_t
*ucontrol
)
400 pmac_t
*chip
= snd_kcontrol_chip(kcontrol
);
402 if (! (mix
= chip
->mixer_data
))
404 ucontrol
->value
.integer
.value
[0] = mix
->drc_range
;
408 static int tumbler_put_drc_value(snd_kcontrol_t
*kcontrol
, snd_ctl_elem_value_t
*ucontrol
)
410 pmac_t
*chip
= snd_kcontrol_chip(kcontrol
);
414 if (! (mix
= chip
->mixer_data
))
416 change
= mix
->drc_range
!= ucontrol
->value
.integer
.value
[0];
418 mix
->drc_range
= ucontrol
->value
.integer
.value
[0];
419 if (chip
->model
== PMAC_TUMBLER
)
420 tumbler_set_drc(mix
);
422 snapper_set_drc(mix
);
427 static int tumbler_get_drc_switch(snd_kcontrol_t
*kcontrol
, snd_ctl_elem_value_t
*ucontrol
)
429 pmac_t
*chip
= snd_kcontrol_chip(kcontrol
);
431 if (! (mix
= chip
->mixer_data
))
433 ucontrol
->value
.integer
.value
[0] = mix
->drc_enable
;
437 static int tumbler_put_drc_switch(snd_kcontrol_t
*kcontrol
, snd_ctl_elem_value_t
*ucontrol
)
439 pmac_t
*chip
= snd_kcontrol_chip(kcontrol
);
443 if (! (mix
= chip
->mixer_data
))
445 change
= mix
->drc_enable
!= ucontrol
->value
.integer
.value
[0];
447 mix
->drc_enable
= !!ucontrol
->value
.integer
.value
[0];
448 if (chip
->model
== PMAC_TUMBLER
)
449 tumbler_set_drc(mix
);
451 snapper_set_drc(mix
);
461 struct tumbler_mono_vol
{
469 static int tumbler_set_mono_volume(pmac_tumbler_t
*mix
, struct tumbler_mono_vol
*info
)
471 unsigned char block
[4];
475 if (! mix
->i2c
.client
)
478 vol
= mix
->mono_vol
[info
->index
];
479 if (vol
>= info
->max
)
481 vol
= info
->table
[vol
];
482 for (i
= 0; i
< info
->bytes
; i
++)
483 block
[i
] = (vol
>> ((info
->bytes
- i
- 1) * 8)) & 0xff;
484 if (i2c_smbus_write_block_data(mix
->i2c
.client
, info
->reg
,
485 info
->bytes
, block
) < 0) {
486 snd_printk("failed to set mono volume %d\n", info
->index
);
492 static int tumbler_info_mono(snd_kcontrol_t
*kcontrol
, snd_ctl_elem_info_t
*uinfo
)
494 struct tumbler_mono_vol
*info
= (struct tumbler_mono_vol
*)kcontrol
->private_value
;
496 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
498 uinfo
->value
.integer
.min
= 0;
499 uinfo
->value
.integer
.max
= info
->max
- 1;
503 static int tumbler_get_mono(snd_kcontrol_t
*kcontrol
, snd_ctl_elem_value_t
*ucontrol
)
505 struct tumbler_mono_vol
*info
= (struct tumbler_mono_vol
*)kcontrol
->private_value
;
506 pmac_t
*chip
= snd_kcontrol_chip(kcontrol
);
508 if (! (mix
= chip
->mixer_data
))
510 ucontrol
->value
.integer
.value
[0] = mix
->mono_vol
[info
->index
];
514 static int tumbler_put_mono(snd_kcontrol_t
*kcontrol
, snd_ctl_elem_value_t
*ucontrol
)
516 struct tumbler_mono_vol
*info
= (struct tumbler_mono_vol
*)kcontrol
->private_value
;
517 pmac_t
*chip
= snd_kcontrol_chip(kcontrol
);
521 if (! (mix
= chip
->mixer_data
))
523 change
= mix
->mono_vol
[info
->index
] != ucontrol
->value
.integer
.value
[0];
525 mix
->mono_vol
[info
->index
] = ucontrol
->value
.integer
.value
[0];
526 tumbler_set_mono_volume(mix
, info
);
531 /* TAS3001c mono volumes */
532 static struct tumbler_mono_vol tumbler_pcm_vol_info
= {
533 .index
= VOL_IDX_PCM_MONO
,
536 .max
= ARRAY_SIZE(mixer_volume_table
),
537 .table
= mixer_volume_table
,
540 static struct tumbler_mono_vol tumbler_bass_vol_info
= {
541 .index
= VOL_IDX_BASS
,
544 .max
= ARRAY_SIZE(bass_volume_table
),
545 .table
= bass_volume_table
,
548 static struct tumbler_mono_vol tumbler_treble_vol_info
= {
549 .index
= VOL_IDX_TREBLE
,
550 .reg
= TAS_REG_TREBLE
,
552 .max
= ARRAY_SIZE(treble_volume_table
),
553 .table
= treble_volume_table
,
556 /* TAS3004 mono volumes */
557 static struct tumbler_mono_vol snapper_bass_vol_info
= {
558 .index
= VOL_IDX_BASS
,
561 .max
= ARRAY_SIZE(snapper_bass_volume_table
),
562 .table
= snapper_bass_volume_table
,
565 static struct tumbler_mono_vol snapper_treble_vol_info
= {
566 .index
= VOL_IDX_TREBLE
,
567 .reg
= TAS_REG_TREBLE
,
569 .max
= ARRAY_SIZE(snapper_treble_volume_table
),
570 .table
= snapper_treble_volume_table
,
574 #define DEFINE_MONO(xname,type) { \
575 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,\
577 .info = tumbler_info_mono, \
578 .get = tumbler_get_mono, \
579 .put = tumbler_put_mono, \
580 .private_value = (unsigned long)(&tumbler_##type##_vol_info), \
583 #define DEFINE_SNAPPER_MONO(xname,type) { \
584 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,\
586 .info = tumbler_info_mono, \
587 .get = tumbler_get_mono, \
588 .put = tumbler_put_mono, \
589 .private_value = (unsigned long)(&snapper_##type##_vol_info), \
594 * snapper mixer volumes
597 static int snapper_set_mix_vol1(pmac_tumbler_t
*mix
, int idx
, int ch
, int reg
)
600 unsigned char block
[9];
602 vol
= mix
->mix_vol
[idx
][ch
];
603 if (vol
>= ARRAY_SIZE(mixer_volume_table
)) {
604 vol
= ARRAY_SIZE(mixer_volume_table
) - 1;
605 mix
->mix_vol
[idx
][ch
] = vol
;
608 for (i
= 0; i
< 3; i
++) {
609 vol
= mix
->mix_vol
[i
][ch
];
610 vol
= mixer_volume_table
[vol
];
611 for (j
= 0; j
< 3; j
++)
612 block
[i
* 3 + j
] = (vol
>> ((2 - j
) * 8)) & 0xff;
614 if (i2c_smbus_write_block_data(mix
->i2c
.client
, reg
, 9, block
) < 0) {
615 snd_printk("failed to set mono volume %d\n", reg
);
621 static int snapper_set_mix_vol(pmac_tumbler_t
*mix
, int idx
)
623 if (! mix
->i2c
.client
)
625 if (snapper_set_mix_vol1(mix
, idx
, 0, TAS_REG_LMIX
) < 0 ||
626 snapper_set_mix_vol1(mix
, idx
, 1, TAS_REG_RMIX
) < 0)
631 static int snapper_info_mix(snd_kcontrol_t
*kcontrol
, snd_ctl_elem_info_t
*uinfo
)
633 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
635 uinfo
->value
.integer
.min
= 0;
636 uinfo
->value
.integer
.max
= ARRAY_SIZE(mixer_volume_table
) - 1;
640 static int snapper_get_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
);
645 if (! (mix
= chip
->mixer_data
))
647 ucontrol
->value
.integer
.value
[0] = mix
->mix_vol
[idx
][0];
648 ucontrol
->value
.integer
.value
[1] = mix
->mix_vol
[idx
][1];
652 static int snapper_put_mix(snd_kcontrol_t
*kcontrol
, snd_ctl_elem_value_t
*ucontrol
)
654 int idx
= (int)kcontrol
->private_value
;
655 pmac_t
*chip
= snd_kcontrol_chip(kcontrol
);
659 if (! (mix
= chip
->mixer_data
))
661 change
= mix
->mix_vol
[idx
][0] != ucontrol
->value
.integer
.value
[0] ||
662 mix
->mix_vol
[idx
][1] != ucontrol
->value
.integer
.value
[1];
664 mix
->mix_vol
[idx
][0] = ucontrol
->value
.integer
.value
[0];
665 mix
->mix_vol
[idx
][1] = ucontrol
->value
.integer
.value
[1];
666 snapper_set_mix_vol(mix
, idx
);
673 * mute switches. FIXME: Turn that into software mute when both outputs are muted
674 * to avoid codec reset on ibook M7
677 enum { TUMBLER_MUTE_HP
, TUMBLER_MUTE_AMP
, TUMBLER_MUTE_LINE
};
679 static int tumbler_get_mute_switch(snd_kcontrol_t
*kcontrol
, snd_ctl_elem_value_t
*ucontrol
)
681 pmac_t
*chip
= snd_kcontrol_chip(kcontrol
);
684 if (! (mix
= chip
->mixer_data
))
686 switch(kcontrol
->private_value
) {
687 case TUMBLER_MUTE_HP
:
688 gp
= &mix
->hp_mute
; break;
689 case TUMBLER_MUTE_AMP
:
690 gp
= &mix
->amp_mute
; break;
691 case TUMBLER_MUTE_LINE
:
692 gp
= &mix
->line_mute
; break;
698 ucontrol
->value
.integer
.value
[0] = !check_audio_gpio(gp
);
702 static int tumbler_put_mute_switch(snd_kcontrol_t
*kcontrol
, snd_ctl_elem_value_t
*ucontrol
)
704 pmac_t
*chip
= snd_kcontrol_chip(kcontrol
);
708 #ifdef PMAC_SUPPORT_AUTOMUTE
709 if (chip
->update_automute
&& chip
->auto_mute
)
710 return 0; /* don't touch in the auto-mute mode */
712 if (! (mix
= chip
->mixer_data
))
714 switch(kcontrol
->private_value
) {
715 case TUMBLER_MUTE_HP
:
716 gp
= &mix
->hp_mute
; break;
717 case TUMBLER_MUTE_AMP
:
718 gp
= &mix
->amp_mute
; break;
719 case TUMBLER_MUTE_LINE
:
720 gp
= &mix
->line_mute
; break;
726 val
= ! check_audio_gpio(gp
);
727 if (val
!= ucontrol
->value
.integer
.value
[0]) {
728 write_audio_gpio(gp
, ! ucontrol
->value
.integer
.value
[0]);
734 static int snapper_set_capture_source(pmac_tumbler_t
*mix
)
736 if (! mix
->i2c
.client
)
738 if (mix
->capture_source
)
739 mix
->acs
= mix
->acs
|= 2;
742 return i2c_smbus_write_byte_data(mix
->i2c
.client
, TAS_REG_ACS
, mix
->acs
);
745 static int snapper_info_capture_source(snd_kcontrol_t
*kcontrol
, snd_ctl_elem_info_t
*uinfo
)
747 static char *texts
[2] = {
750 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_ENUMERATED
;
752 uinfo
->value
.enumerated
.items
= 2;
753 if (uinfo
->value
.enumerated
.item
> 1)
754 uinfo
->value
.enumerated
.item
= 1;
755 strcpy(uinfo
->value
.enumerated
.name
, texts
[uinfo
->value
.enumerated
.item
]);
759 static int snapper_get_capture_source(snd_kcontrol_t
*kcontrol
, snd_ctl_elem_value_t
*ucontrol
)
761 pmac_t
*chip
= snd_kcontrol_chip(kcontrol
);
762 pmac_tumbler_t
*mix
= chip
->mixer_data
;
764 snd_assert(mix
, return -ENODEV
);
765 ucontrol
->value
.integer
.value
[0] = mix
->capture_source
;
769 static int snapper_put_capture_source(snd_kcontrol_t
*kcontrol
, snd_ctl_elem_value_t
*ucontrol
)
771 pmac_t
*chip
= snd_kcontrol_chip(kcontrol
);
772 pmac_tumbler_t
*mix
= chip
->mixer_data
;
775 snd_assert(mix
, return -ENODEV
);
776 change
= ucontrol
->value
.integer
.value
[0] != mix
->capture_source
;
778 mix
->capture_source
= !!ucontrol
->value
.integer
.value
[0];
779 snapper_set_capture_source(mix
);
784 #define DEFINE_SNAPPER_MIX(xname,idx,ofs) { \
785 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,\
787 .info = snapper_info_mix, \
788 .get = snapper_get_mix, \
789 .put = snapper_put_mix, \
791 .private_value = ofs, \
797 static snd_kcontrol_new_t tumbler_mixers
[] __initdata
= {
798 { .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
799 .name
= "Master Playback Volume",
800 .info
= tumbler_info_master_volume
,
801 .get
= tumbler_get_master_volume
,
802 .put
= tumbler_put_master_volume
804 { .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
805 .name
= "Master Playback Switch",
806 .info
= snd_pmac_boolean_stereo_info
,
807 .get
= tumbler_get_master_switch
,
808 .put
= tumbler_put_master_switch
810 DEFINE_MONO("Tone Control - Bass", bass
),
811 DEFINE_MONO("Tone Control - Treble", treble
),
812 DEFINE_MONO("PCM Playback Volume", pcm
),
813 { .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
815 .info
= tumbler_info_drc_value
,
816 .get
= tumbler_get_drc_value
,
817 .put
= tumbler_put_drc_value
821 static snd_kcontrol_new_t snapper_mixers
[] __initdata
= {
822 { .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
823 .name
= "Master Playback Volume",
824 .info
= tumbler_info_master_volume
,
825 .get
= tumbler_get_master_volume
,
826 .put
= tumbler_put_master_volume
828 { .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
829 .name
= "Master Playback Switch",
830 .info
= snd_pmac_boolean_stereo_info
,
831 .get
= tumbler_get_master_switch
,
832 .put
= tumbler_put_master_switch
834 DEFINE_SNAPPER_MIX("PCM Playback Volume", 0, VOL_IDX_PCM
),
835 DEFINE_SNAPPER_MIX("PCM Playback Volume", 1, VOL_IDX_PCM2
),
836 DEFINE_SNAPPER_MIX("Monitor Mix Volume", 0, VOL_IDX_ADC
),
837 DEFINE_SNAPPER_MONO("Tone Control - Bass", bass
),
838 DEFINE_SNAPPER_MONO("Tone Control - Treble", treble
),
839 { .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
841 .info
= tumbler_info_drc_value
,
842 .get
= tumbler_get_drc_value
,
843 .put
= tumbler_put_drc_value
845 { .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
846 .name
= "Input Source", /* FIXME: "Capture Source" doesn't work properly */
847 .info
= snapper_info_capture_source
,
848 .get
= snapper_get_capture_source
,
849 .put
= snapper_put_capture_source
853 static snd_kcontrol_new_t tumbler_hp_sw __initdata
= {
854 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
855 .name
= "Headphone Playback Switch",
856 .info
= snd_pmac_boolean_mono_info
,
857 .get
= tumbler_get_mute_switch
,
858 .put
= tumbler_put_mute_switch
,
859 .private_value
= TUMBLER_MUTE_HP
,
861 static snd_kcontrol_new_t tumbler_speaker_sw __initdata
= {
862 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
863 .name
= "PC Speaker Playback Switch",
864 .info
= snd_pmac_boolean_mono_info
,
865 .get
= tumbler_get_mute_switch
,
866 .put
= tumbler_put_mute_switch
,
867 .private_value
= TUMBLER_MUTE_AMP
,
869 static snd_kcontrol_new_t tumbler_lineout_sw __initdata
= {
870 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
871 .name
= "Line Out Playback Switch",
872 .info
= snd_pmac_boolean_mono_info
,
873 .get
= tumbler_get_mute_switch
,
874 .put
= tumbler_put_mute_switch
,
875 .private_value
= TUMBLER_MUTE_LINE
,
877 static snd_kcontrol_new_t tumbler_drc_sw __initdata
= {
878 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
879 .name
= "DRC Switch",
880 .info
= snd_pmac_boolean_mono_info
,
881 .get
= tumbler_get_drc_switch
,
882 .put
= tumbler_put_drc_switch
886 #ifdef PMAC_SUPPORT_AUTOMUTE
890 static int tumbler_detect_headphone(pmac_t
*chip
)
892 pmac_tumbler_t
*mix
= chip
->mixer_data
;
895 if (mix
->hp_detect
.addr
)
896 detect
|= read_audio_gpio(&mix
->hp_detect
);
900 static int tumbler_detect_lineout(pmac_t
*chip
)
902 pmac_tumbler_t
*mix
= chip
->mixer_data
;
905 if (mix
->line_detect
.addr
)
906 detect
|= read_audio_gpio(&mix
->line_detect
);
910 static void check_mute(pmac_t
*chip
, pmac_gpio_t
*gp
, int val
, int do_notify
, snd_kcontrol_t
*sw
)
912 if (check_audio_gpio(gp
) != val
) {
913 write_audio_gpio(gp
, val
);
915 snd_ctl_notify(chip
->card
, SNDRV_CTL_EVENT_MASK_VALUE
,
920 static struct work_struct device_change
;
922 static void device_change_handler(void *self
)
924 pmac_t
*chip
= (pmac_t
*) self
;
926 int headphone
, lineout
;
931 mix
= chip
->mixer_data
;
932 snd_assert(mix
, return);
934 headphone
= tumbler_detect_headphone(chip
);
935 lineout
= tumbler_detect_lineout(chip
);
937 DBG("headphone: %d, lineout: %d\n", headphone
, lineout
);
939 if (headphone
|| lineout
) {
940 /* unmute headphone/lineout & mute speaker */
942 check_mute(chip
, &mix
->hp_mute
, 0, mix
->auto_mute_notify
,
943 chip
->master_sw_ctl
);
944 if (lineout
&& mix
->line_mute
.addr
!= 0)
945 check_mute(chip
, &mix
->line_mute
, 0, mix
->auto_mute_notify
,
946 chip
->lineout_sw_ctl
);
947 if (mix
->anded_reset
)
949 check_mute(chip
, &mix
->amp_mute
, 1, mix
->auto_mute_notify
,
950 chip
->speaker_sw_ctl
);
953 /* unmute speaker, mute others */
954 check_mute(chip
, &mix
->amp_mute
, 0, mix
->auto_mute_notify
,
955 chip
->speaker_sw_ctl
);
956 if (mix
->anded_reset
)
958 check_mute(chip
, &mix
->hp_mute
, 1, mix
->auto_mute_notify
,
959 chip
->master_sw_ctl
);
960 if (mix
->line_mute
.addr
!= 0)
961 check_mute(chip
, &mix
->line_mute
, 1, mix
->auto_mute_notify
,
962 chip
->lineout_sw_ctl
);
965 if (mix
->auto_mute_notify
) {
966 snd_ctl_notify(chip
->card
, SNDRV_CTL_EVENT_MASK_VALUE
,
967 &chip
->hp_detect_ctl
->id
);
968 snd_ctl_notify(chip
->card
, SNDRV_CTL_EVENT_MASK_VALUE
,
969 &chip
->drc_sw_ctl
->id
);
972 /* first set the DRC so the speaker do not explode -ReneR */
973 if (chip
->model
== PMAC_TUMBLER
)
974 tumbler_set_drc(mix
);
976 snapper_set_drc(mix
);
978 /* reset the master volume so the correct amplification is applied */
979 tumbler_set_master_volume(mix
);
982 static void tumbler_update_automute(pmac_t
*chip
, int do_notify
)
984 if (chip
->auto_mute
) {
986 mix
= chip
->mixer_data
;
987 snd_assert(mix
, return);
988 mix
->auto_mute_notify
= do_notify
;
989 schedule_work(&device_change
);
992 #endif /* PMAC_SUPPORT_AUTOMUTE */
995 /* interrupt - headphone plug changed */
996 static irqreturn_t
headphone_intr(int irq
, void *devid
, struct pt_regs
*regs
)
998 pmac_t
*chip
= devid
;
999 if (chip
->update_automute
&& chip
->initialized
) {
1000 chip
->update_automute(chip
, 1);
1006 /* look for audio-gpio device */
1007 static struct device_node
*find_audio_device(const char *name
)
1009 struct device_node
*np
;
1011 if (! (np
= find_devices("gpio")))
1014 for (np
= np
->child
; np
; np
= np
->sibling
) {
1015 char *property
= get_property(np
, "audio-gpio", NULL
);
1016 if (property
&& strcmp(property
, name
) == 0)
1022 /* look for audio-gpio device */
1023 static struct device_node
*find_compatible_audio_device(const char *name
)
1025 struct device_node
*np
;
1027 if (! (np
= find_devices("gpio")))
1030 for (np
= np
->child
; np
; np
= np
->sibling
) {
1031 if (device_is_compatible(np
, name
))
1037 /* find an audio device and get its address */
1038 static long tumbler_find_device(const char *device
, const char *platform
, pmac_gpio_t
*gp
, int is_compatible
)
1040 struct device_node
*node
;
1044 node
= find_compatible_audio_device(device
);
1046 node
= find_audio_device(device
);
1048 DBG("(W) cannot find audio device %s !\n", device
);
1049 snd_printdd("cannot find device %s\n", device
);
1053 base
= (u32
*)get_property(node
, "AAPL,address", NULL
);
1055 base
= (u32
*)get_property(node
, "reg", NULL
);
1057 DBG("(E) cannot find address for device %s !\n", device
);
1058 snd_printd("cannot find address for device %s\n", device
);
1067 gp
->addr
= addr
& 0x0000ffff;
1068 /* Try to find the active state, default to 0 ! */
1069 base
= (u32
*)get_property(node
, "audio-gpio-active-state", NULL
);
1071 gp
->active_state
= *base
;
1072 gp
->active_val
= (*base
) ? 0x5 : 0x4;
1073 gp
->inactive_val
= (*base
) ? 0x4 : 0x5;
1076 gp
->active_state
= 0;
1077 gp
->active_val
= 0x4;
1078 gp
->inactive_val
= 0x5;
1079 /* Here are some crude hacks to extract the GPIO polarity and
1080 * open collector informations out of the do-platform script
1081 * as we don't yet have an interpreter for these things
1084 prop
= (u32
*)get_property(node
, platform
, NULL
);
1086 if (prop
[3] == 0x9 && prop
[4] == 0x9) {
1087 gp
->active_val
= 0xd;
1088 gp
->inactive_val
= 0xc;
1090 if (prop
[3] == 0x1 && prop
[4] == 0x1) {
1091 gp
->active_val
= 0x5;
1092 gp
->inactive_val
= 0x4;
1097 DBG("(I) GPIO device %s found, offset: %x, active state: %d !\n",
1098 device
, gp
->addr
, gp
->active_state
);
1100 return (node
->n_intrs
> 0) ? node
->intrs
[0].line
: 0;
1104 static void tumbler_reset_audio(pmac_t
*chip
)
1106 pmac_tumbler_t
*mix
= chip
->mixer_data
;
1108 if (mix
->anded_reset
) {
1109 DBG("(I) codec anded reset !\n");
1110 write_audio_gpio(&mix
->hp_mute
, 0);
1111 write_audio_gpio(&mix
->amp_mute
, 0);
1113 write_audio_gpio(&mix
->hp_mute
, 1);
1114 write_audio_gpio(&mix
->amp_mute
, 1);
1116 write_audio_gpio(&mix
->hp_mute
, 0);
1117 write_audio_gpio(&mix
->amp_mute
, 0);
1120 DBG("(I) codec normal reset !\n");
1122 write_audio_gpio(&mix
->audio_reset
, 0);
1124 write_audio_gpio(&mix
->audio_reset
, 1);
1126 write_audio_gpio(&mix
->audio_reset
, 0);
1131 #ifdef CONFIG_PMAC_PBOOK
1133 static void tumbler_suspend(pmac_t
*chip
)
1135 pmac_tumbler_t
*mix
= chip
->mixer_data
;
1137 if (mix
->headphone_irq
>= 0)
1138 disable_irq(mix
->headphone_irq
);
1139 if (mix
->lineout_irq
>= 0)
1140 disable_irq(mix
->lineout_irq
);
1141 mix
->save_master_switch
[0] = mix
->master_switch
[0];
1142 mix
->save_master_switch
[1] = mix
->master_switch
[1];
1143 mix
->save_master_vol
[0] = mix
->master_vol
[0];
1144 mix
->save_master_vol
[1] = mix
->master_vol
[1];
1145 mix
->master_switch
[0] = mix
->master_switch
[1] = 0;
1146 tumbler_set_master_volume(mix
);
1147 if (!mix
->anded_reset
) {
1148 write_audio_gpio(&mix
->amp_mute
, 1);
1149 write_audio_gpio(&mix
->hp_mute
, 1);
1151 if (chip
->model
== PMAC_SNAPPER
) {
1153 i2c_smbus_write_byte_data(mix
->i2c
.client
, TAS_REG_ACS
, mix
->acs
);
1155 if (mix
->anded_reset
) {
1156 write_audio_gpio(&mix
->amp_mute
, 1);
1157 write_audio_gpio(&mix
->hp_mute
, 1);
1159 write_audio_gpio(&mix
->audio_reset
, 1);
1163 static void tumbler_resume(pmac_t
*chip
)
1165 pmac_tumbler_t
*mix
= chip
->mixer_data
;
1167 snd_assert(mix
, return);
1170 mix
->master_switch
[0] = mix
->save_master_switch
[0];
1171 mix
->master_switch
[1] = mix
->save_master_switch
[1];
1172 mix
->master_vol
[0] = mix
->save_master_vol
[0];
1173 mix
->master_vol
[1] = mix
->save_master_vol
[1];
1174 tumbler_reset_audio(chip
);
1175 if (mix
->i2c
.client
&& mix
->i2c
.init_client
) {
1176 if (mix
->i2c
.init_client(&mix
->i2c
) < 0)
1177 printk(KERN_ERR
"tumbler_init_client error\n");
1179 printk(KERN_ERR
"tumbler: i2c is not initialized\n");
1180 if (chip
->model
== PMAC_TUMBLER
) {
1181 tumbler_set_mono_volume(mix
, &tumbler_pcm_vol_info
);
1182 tumbler_set_mono_volume(mix
, &tumbler_bass_vol_info
);
1183 tumbler_set_mono_volume(mix
, &tumbler_treble_vol_info
);
1184 tumbler_set_drc(mix
);
1186 snapper_set_mix_vol(mix
, VOL_IDX_PCM
);
1187 snapper_set_mix_vol(mix
, VOL_IDX_PCM2
);
1188 snapper_set_mix_vol(mix
, VOL_IDX_ADC
);
1189 tumbler_set_mono_volume(mix
, &snapper_bass_vol_info
);
1190 tumbler_set_mono_volume(mix
, &snapper_treble_vol_info
);
1191 snapper_set_drc(mix
);
1192 snapper_set_capture_source(mix
);
1194 tumbler_set_master_volume(mix
);
1195 if (chip
->update_automute
)
1196 chip
->update_automute(chip
, 0);
1197 if (mix
->headphone_irq
>= 0)
1198 enable_irq(mix
->headphone_irq
);
1199 if (mix
->lineout_irq
>= 0)
1200 enable_irq(mix
->lineout_irq
);
1204 /* initialize tumbler */
1205 static int __init
tumbler_init(pmac_t
*chip
)
1208 pmac_tumbler_t
*mix
= chip
->mixer_data
;
1209 snd_assert(mix
, return -EINVAL
);
1211 if (tumbler_find_device("audio-hw-reset",
1212 "platform-do-hw-reset",
1213 &mix
->audio_reset
, 0) < 0)
1214 tumbler_find_device("hw-reset",
1215 "platform-do-hw-reset",
1216 &mix
->audio_reset
, 1);
1217 if (tumbler_find_device("amp-mute",
1218 "platform-do-amp-mute",
1219 &mix
->amp_mute
, 0) < 0)
1220 tumbler_find_device("amp-mute",
1221 "platform-do-amp-mute",
1223 if (tumbler_find_device("headphone-mute",
1224 "platform-do-headphone-mute",
1225 &mix
->hp_mute
, 0) < 0)
1226 tumbler_find_device("headphone-mute",
1227 "platform-do-headphone-mute",
1229 if (tumbler_find_device("line-output-mute",
1230 "platform-do-lineout-mute",
1231 &mix
->line_mute
, 0) < 0)
1232 tumbler_find_device("line-output-mute",
1233 "platform-do-lineout-mute",
1234 &mix
->line_mute
, 1);
1235 irq
= tumbler_find_device("headphone-detect",
1236 NULL
, &mix
->hp_detect
, 0);
1238 irq
= tumbler_find_device("headphone-detect",
1239 NULL
, &mix
->hp_detect
, 1);
1241 irq
= tumbler_find_device("keywest-gpio15",
1242 NULL
, &mix
->hp_detect
, 1);
1243 mix
->headphone_irq
= irq
;
1244 irq
= tumbler_find_device("line-output-detect",
1245 NULL
, &mix
->line_detect
, 0);
1247 irq
= tumbler_find_device("line-output-detect",
1248 NULL
, &mix
->line_detect
, 1);
1249 mix
->lineout_irq
= irq
;
1251 tumbler_reset_audio(chip
);
1256 static void tumbler_cleanup(pmac_t
*chip
)
1258 pmac_tumbler_t
*mix
= chip
->mixer_data
;
1262 if (mix
->headphone_irq
>= 0)
1263 free_irq(mix
->headphone_irq
, chip
);
1264 if (mix
->lineout_irq
>= 0)
1265 free_irq(mix
->lineout_irq
, chip
);
1266 tumbler_gpio_free(&mix
->audio_reset
);
1267 tumbler_gpio_free(&mix
->amp_mute
);
1268 tumbler_gpio_free(&mix
->hp_mute
);
1269 tumbler_gpio_free(&mix
->hp_detect
);
1270 snd_pmac_keywest_cleanup(&mix
->i2c
);
1272 chip
->mixer_data
= NULL
;
1276 int __init
snd_pmac_tumbler_init(pmac_t
*chip
)
1279 pmac_tumbler_t
*mix
;
1281 struct device_node
*tas_node
, *np
;
1285 if (current
->fs
->root
)
1286 request_module("i2c-keywest");
1287 #endif /* CONFIG_KMOD */
1289 mix
= kmalloc(sizeof(*mix
), GFP_KERNEL
);
1292 memset(mix
, 0, sizeof(*mix
));
1293 mix
->headphone_irq
= -1;
1295 chip
->mixer_data
= mix
;
1296 chip
->mixer_free
= tumbler_cleanup
;
1297 mix
->anded_reset
= 0;
1298 mix
->reset_on_sleep
= 1;
1300 for (np
= chip
->node
->child
; np
; np
= np
->sibling
) {
1301 if (!strcmp(np
->name
, "sound")) {
1302 if (get_property(np
, "has-anded-reset", NULL
))
1303 mix
->anded_reset
= 1;
1304 if (get_property(np
, "layout-id", NULL
))
1305 mix
->reset_on_sleep
= 0;
1309 if ((err
= tumbler_init(chip
)) < 0)
1313 tas_node
= find_devices("deq");
1314 if (tas_node
== NULL
)
1315 tas_node
= find_devices("codec");
1316 if (tas_node
== NULL
)
1319 paddr
= (u32
*)get_property(tas_node
, "i2c-address", NULL
);
1321 paddr
= (u32
*)get_property(tas_node
, "reg", NULL
);
1323 mix
->i2c
.addr
= (*paddr
) >> 1;
1325 mix
->i2c
.addr
= TAS_I2C_ADDR
;
1327 DBG("(I) TAS i2c address is: %x\n", mix
->i2c
.addr
);
1329 if (chip
->model
== PMAC_TUMBLER
) {
1330 mix
->i2c
.init_client
= tumbler_init_client
;
1331 mix
->i2c
.name
= "TAS3001c";
1332 chipname
= "Tumbler";
1334 mix
->i2c
.init_client
= snapper_init_client
;
1335 mix
->i2c
.name
= "TAS3004";
1336 chipname
= "Snapper";
1339 if ((err
= snd_pmac_keywest_init(&mix
->i2c
)) < 0)
1345 sprintf(chip
->card
->mixername
, "PowerMac %s", chipname
);
1347 if (chip
->model
== PMAC_TUMBLER
) {
1348 for (i
= 0; i
< ARRAY_SIZE(tumbler_mixers
); i
++) {
1349 if ((err
= snd_ctl_add(chip
->card
, snd_ctl_new1(&tumbler_mixers
[i
], chip
))) < 0)
1353 for (i
= 0; i
< ARRAY_SIZE(snapper_mixers
); i
++) {
1354 if ((err
= snd_ctl_add(chip
->card
, snd_ctl_new1(&snapper_mixers
[i
], chip
))) < 0)
1358 chip
->master_sw_ctl
= snd_ctl_new1(&tumbler_hp_sw
, chip
);
1359 if ((err
= snd_ctl_add(chip
->card
, chip
->master_sw_ctl
)) < 0)
1361 chip
->speaker_sw_ctl
= snd_ctl_new1(&tumbler_speaker_sw
, chip
);
1362 if ((err
= snd_ctl_add(chip
->card
, chip
->speaker_sw_ctl
)) < 0)
1364 if (mix
->line_mute
.addr
!= 0) {
1365 chip
->lineout_sw_ctl
= snd_ctl_new1(&tumbler_lineout_sw
, chip
);
1366 if ((err
= snd_ctl_add(chip
->card
, chip
->lineout_sw_ctl
)) < 0)
1369 chip
->drc_sw_ctl
= snd_ctl_new1(&tumbler_drc_sw
, chip
);
1370 if ((err
= snd_ctl_add(chip
->card
, chip
->drc_sw_ctl
)) < 0)
1373 #ifdef CONFIG_PMAC_PBOOK
1374 chip
->suspend
= tumbler_suspend
;
1375 chip
->resume
= tumbler_resume
;
1378 INIT_WORK(&device_change
, device_change_handler
, (void *)chip
);
1380 #ifdef PMAC_SUPPORT_AUTOMUTE
1381 if ((mix
->headphone_irq
>=0 || mix
->lineout_irq
>= 0)
1382 && (err
= snd_pmac_add_automute(chip
)) < 0)
1384 chip
->detect_headphone
= tumbler_detect_headphone
;
1385 chip
->update_automute
= tumbler_update_automute
;
1386 tumbler_update_automute(chip
, 0); /* update the status only */
1388 /* activate headphone status interrupts */
1389 if (mix
->headphone_irq
>= 0) {
1391 if ((err
= request_irq(mix
->headphone_irq
, headphone_intr
, 0,
1392 "Sound Headphone Detection", chip
)) < 0)
1394 /* activate headphone status interrupts */
1395 val
= do_gpio_read(&mix
->hp_detect
);
1396 do_gpio_write(&mix
->hp_detect
, val
| 0x80);
1398 if (mix
->lineout_irq
>= 0) {
1400 if ((err
= request_irq(mix
->lineout_irq
, headphone_intr
, 0,
1401 "Sound Lineout Detection", chip
)) < 0)
1403 /* activate headphone status interrupts */
1404 val
= do_gpio_read(&mix
->line_detect
);
1405 do_gpio_write(&mix
->line_detect
, val
| 0x80);