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
22 #include <sound/driver.h>
23 #include <linux/init.h>
24 #include <linux/delay.h>
25 #include <linux/i2c.h>
26 #include <linux/i2c-dev.h>
27 #include <linux/kmod.h>
28 #include <linux/slab.h>
29 #include <linux/interrupt.h>
30 #include <linux/workqueue.h>
31 #include <sound/core.h>
34 #ifdef CONFIG_PPC_HAS_FEATURE_CALLS
35 #include <asm/pmac_feature.h>
38 #include "tumbler_volume.h"
42 /* i2c address for tumbler */
43 #define TAS_I2C_ADDR 0x34
46 #define TAS_REG_MCS 0x01
47 #define TAS_REG_DRC 0x02
48 #define TAS_REG_VOL 0x04
49 #define TAS_REG_TREBLE 0x05
50 #define TAS_REG_BASS 0x06
51 #define TAS_REG_INPUT1 0x07
52 #define TAS_REG_INPUT2 0x08
55 #define TAS_REG_PCM TAS_REG_INPUT1
58 #define TAS_REG_LMIX TAS_REG_INPUT1
59 #define TAS_REG_RMIX TAS_REG_INPUT2
61 /* mono volumes for tas3001c/tas3004 */
63 VOL_IDX_PCM_MONO
, /* tas3001c only */
64 VOL_IDX_BASS
, VOL_IDX_TREBLE
,
68 /* stereo volumes for tas3004 */
70 VOL_IDX_PCM
, VOL_IDX_PCM2
, VOL_IDX_ADC
,
74 typedef struct pmac_gpio
{
75 #ifdef CONFIG_PPC_HAS_FEATURE_CALLS
83 typedef struct pmac_tumbler_t
{
85 pmac_gpio_t audio_reset
;
88 pmac_gpio_t hp_detect
;
90 unsigned int master_vol
[2];
91 unsigned int master_switch
[2];
92 unsigned int mono_vol
[VOL_IDX_LAST_MONO
];
93 unsigned int mix_vol
[VOL_IDX_LAST_MIX
][2]; /* stereo volumes for tas3004 */
96 #ifdef CONFIG_PMAC_PBOOK
97 struct work_struct resume_workq
;
105 static int tumbler_init_client(pmac_keywest_t
*i2c
)
109 /* normal operation, SCLK=64fps, i2s output, i2s input, 16bit width */
110 err
= snd_pmac_keywest_write_byte(i2c
, TAS_REG_MCS
,
111 (1<<6)+(2<<4)+(2<<2)+0);
123 #ifdef CONFIG_PPC_HAS_FEATURE_CALLS
124 #define do_gpio_write(gp, val) \
125 pmac_call_feature(PMAC_FTR_WRITE_GPIO, NULL, (gp)->addr, val)
126 #define do_gpio_read(gp) \
127 pmac_call_feature(PMAC_FTR_READ_GPIO, NULL, (gp)->addr, 0)
128 #define tumbler_gpio_free(gp) /* NOP */
130 #define do_gpio_write(gp, val) writeb(val, (gp)->addr)
131 #define do_gpio_read(gp) readb((gp)->addr)
132 static inline void tumbler_gpio_free(pmac_gpio_t
*gp
)
139 #endif /* CONFIG_PPC_HAS_FEATURE_CALLS */
141 static void write_audio_gpio(pmac_gpio_t
*gp
, int active
)
145 active
= active
? gp
->active_state
: !gp
->active_state
;
146 do_gpio_write(gp
, active
? 0x05 : 0x04);
149 static int read_audio_gpio(pmac_gpio_t
*gp
)
154 ret
= ((do_gpio_read(gp
) & 0x02) !=0);
155 return ret
== gp
->active_state
;
159 * update master volume
161 static int tumbler_set_master_volume(pmac_tumbler_t
*mix
)
163 unsigned char block
[6];
164 unsigned int left_vol
, right_vol
;
166 if (! mix
->i2c
.client
)
169 if (! mix
->master_switch
[0])
172 left_vol
= mix
->master_vol
[0];
173 if (left_vol
>= ARRAY_SIZE(master_volume_table
))
174 left_vol
= ARRAY_SIZE(master_volume_table
) - 1;
175 left_vol
= master_volume_table
[left_vol
];
177 if (! mix
->master_switch
[1])
180 right_vol
= mix
->master_vol
[1];
181 if (right_vol
>= ARRAY_SIZE(master_volume_table
))
182 right_vol
= ARRAY_SIZE(master_volume_table
) - 1;
183 right_vol
= master_volume_table
[right_vol
];
186 block
[0] = (left_vol
>> 16) & 0xff;
187 block
[1] = (left_vol
>> 8) & 0xff;
188 block
[2] = (left_vol
>> 0) & 0xff;
190 block
[3] = (right_vol
>> 16) & 0xff;
191 block
[4] = (right_vol
>> 8) & 0xff;
192 block
[5] = (right_vol
>> 0) & 0xff;
194 if (snd_pmac_keywest_write(&mix
->i2c
, TAS_REG_VOL
, 6, block
) < 0) {
195 snd_printk("failed to set volume \n");
203 static int tumbler_info_master_volume(snd_kcontrol_t
*kcontrol
, snd_ctl_elem_info_t
*uinfo
)
205 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
207 uinfo
->value
.integer
.min
= 0;
208 uinfo
->value
.integer
.max
= ARRAY_SIZE(master_volume_table
) - 1;
212 static int tumbler_get_master_volume(snd_kcontrol_t
*kcontrol
, snd_ctl_elem_value_t
*ucontrol
)
214 pmac_t
*chip
= snd_kcontrol_chip(kcontrol
);
215 pmac_tumbler_t
*mix
= chip
->mixer_data
;
216 snd_assert(mix
, return -ENODEV
);
217 ucontrol
->value
.integer
.value
[0] = mix
->master_vol
[0];
218 ucontrol
->value
.integer
.value
[1] = mix
->master_vol
[1];
222 static int tumbler_put_master_volume(snd_kcontrol_t
*kcontrol
, snd_ctl_elem_value_t
*ucontrol
)
224 pmac_t
*chip
= snd_kcontrol_chip(kcontrol
);
225 pmac_tumbler_t
*mix
= chip
->mixer_data
;
228 snd_assert(mix
, return -ENODEV
);
229 change
= mix
->master_vol
[0] != ucontrol
->value
.integer
.value
[0] ||
230 mix
->master_vol
[1] != ucontrol
->value
.integer
.value
[1];
232 mix
->master_vol
[0] = ucontrol
->value
.integer
.value
[0];
233 mix
->master_vol
[1] = ucontrol
->value
.integer
.value
[1];
234 tumbler_set_master_volume(mix
);
240 static int tumbler_get_master_switch(snd_kcontrol_t
*kcontrol
, snd_ctl_elem_value_t
*ucontrol
)
242 pmac_t
*chip
= snd_kcontrol_chip(kcontrol
);
243 pmac_tumbler_t
*mix
= chip
->mixer_data
;
244 snd_assert(mix
, return -ENODEV
);
245 ucontrol
->value
.integer
.value
[0] = mix
->master_switch
[0];
246 ucontrol
->value
.integer
.value
[1] = mix
->master_switch
[1];
250 static int tumbler_put_master_switch(snd_kcontrol_t
*kcontrol
, snd_ctl_elem_value_t
*ucontrol
)
252 pmac_t
*chip
= snd_kcontrol_chip(kcontrol
);
253 pmac_tumbler_t
*mix
= chip
->mixer_data
;
256 snd_assert(mix
, return -ENODEV
);
257 change
= mix
->master_switch
[0] != ucontrol
->value
.integer
.value
[0] ||
258 mix
->master_switch
[1] != ucontrol
->value
.integer
.value
[1];
260 mix
->master_switch
[0] = !!ucontrol
->value
.integer
.value
[0];
261 mix
->master_switch
[1] = !!ucontrol
->value
.integer
.value
[1];
262 tumbler_set_master_volume(mix
);
269 * TAS3001c dynamic range compression
272 #define TAS3001_DRC_MAX 0x5f
274 static int tumbler_set_drc(pmac_tumbler_t
*mix
)
276 unsigned char val
[2];
278 if (! mix
->i2c
.client
)
281 if (mix
->drc_enable
) {
282 val
[0] = 0xc1; /* enable, 3:1 compression */
283 if (mix
->drc_range
> TAS3001_DRC_MAX
)
285 else if (mix
->drc_range
< 0)
288 val
[1] = mix
->drc_range
+ 0x91;
294 if (snd_pmac_keywest_write(&mix
->i2c
, TAS_REG_DRC
, 2, val
) < 0) {
295 snd_printk("failed to set DRC\n");
305 #define TAS3004_DRC_MAX 0xef
307 static int snapper_set_drc(pmac_tumbler_t
*mix
)
309 unsigned char val
[6];
311 if (! mix
->i2c
.client
)
315 val
[0] = 0x50; /* 3:1 above threshold */
317 val
[0] = 0x51; /* disabled */
318 val
[1] = 0x02; /* 1:1 below threshold */
319 if (mix
->drc_range
> 0xef)
321 else if (mix
->drc_range
< 0)
324 val
[2] = mix
->drc_range
;
329 if (snd_pmac_keywest_write(&mix
->i2c
, TAS_REG_DRC
, 6, val
) < 0) {
330 snd_printk("failed to set DRC\n");
336 static int tumbler_info_drc_value(snd_kcontrol_t
*kcontrol
, snd_ctl_elem_info_t
*uinfo
)
338 pmac_t
*chip
= snd_kcontrol_chip(kcontrol
);
339 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
341 uinfo
->value
.integer
.min
= 0;
342 uinfo
->value
.integer
.max
=
343 chip
->model
== PMAC_TUMBLER
? TAS3001_DRC_MAX
: TAS3004_DRC_MAX
;
347 static int tumbler_get_drc_value(snd_kcontrol_t
*kcontrol
, snd_ctl_elem_value_t
*ucontrol
)
349 pmac_t
*chip
= snd_kcontrol_chip(kcontrol
);
351 if (! (mix
= chip
->mixer_data
))
353 ucontrol
->value
.integer
.value
[0] = mix
->drc_range
;
357 static int tumbler_put_drc_value(snd_kcontrol_t
*kcontrol
, snd_ctl_elem_value_t
*ucontrol
)
359 pmac_t
*chip
= snd_kcontrol_chip(kcontrol
);
363 if (! (mix
= chip
->mixer_data
))
365 change
= mix
->drc_range
!= ucontrol
->value
.integer
.value
[0];
367 mix
->drc_range
= ucontrol
->value
.integer
.value
[0];
368 if (chip
->model
== PMAC_TUMBLER
)
369 tumbler_set_drc(mix
);
371 snapper_set_drc(mix
);
376 static int tumbler_get_drc_switch(snd_kcontrol_t
*kcontrol
, snd_ctl_elem_value_t
*ucontrol
)
378 pmac_t
*chip
= snd_kcontrol_chip(kcontrol
);
380 if (! (mix
= chip
->mixer_data
))
382 ucontrol
->value
.integer
.value
[0] = mix
->drc_enable
;
386 static int tumbler_put_drc_switch(snd_kcontrol_t
*kcontrol
, snd_ctl_elem_value_t
*ucontrol
)
388 pmac_t
*chip
= snd_kcontrol_chip(kcontrol
);
392 if (! (mix
= chip
->mixer_data
))
394 change
= mix
->drc_enable
!= ucontrol
->value
.integer
.value
[0];
396 mix
->drc_enable
= !!ucontrol
->value
.integer
.value
[0];
397 if (chip
->model
== PMAC_TUMBLER
)
398 tumbler_set_drc(mix
);
400 snapper_set_drc(mix
);
410 struct tumbler_mono_vol
{
418 static int tumbler_set_mono_volume(pmac_tumbler_t
*mix
, struct tumbler_mono_vol
*info
)
420 unsigned char block
[4];
424 if (! mix
->i2c
.client
)
427 vol
= mix
->mono_vol
[info
->index
];
428 if (vol
>= info
->max
)
430 vol
= info
->table
[vol
];
431 for (i
= 0; i
< info
->bytes
; i
++)
432 block
[i
] = (vol
>> ((info
->bytes
- i
- 1) * 8)) & 0xff;
433 if (snd_pmac_keywest_write(&mix
->i2c
, info
->reg
, info
->bytes
, block
) < 0) {
434 snd_printk("failed to set mono volume %d\n", info
->index
);
440 static int tumbler_info_mono(snd_kcontrol_t
*kcontrol
, snd_ctl_elem_info_t
*uinfo
)
442 struct tumbler_mono_vol
*info
= (struct tumbler_mono_vol
*)kcontrol
->private_value
;
444 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
446 uinfo
->value
.integer
.min
= 0;
447 uinfo
->value
.integer
.max
= info
->max
- 1;
451 static int tumbler_get_mono(snd_kcontrol_t
*kcontrol
, snd_ctl_elem_value_t
*ucontrol
)
453 struct tumbler_mono_vol
*info
= (struct tumbler_mono_vol
*)kcontrol
->private_value
;
454 pmac_t
*chip
= snd_kcontrol_chip(kcontrol
);
456 if (! (mix
= chip
->mixer_data
))
458 ucontrol
->value
.integer
.value
[0] = mix
->mono_vol
[info
->index
];
462 static int tumbler_put_mono(snd_kcontrol_t
*kcontrol
, snd_ctl_elem_value_t
*ucontrol
)
464 struct tumbler_mono_vol
*info
= (struct tumbler_mono_vol
*)kcontrol
->private_value
;
465 pmac_t
*chip
= snd_kcontrol_chip(kcontrol
);
469 if (! (mix
= chip
->mixer_data
))
471 change
= mix
->mono_vol
[info
->index
] != ucontrol
->value
.integer
.value
[0];
473 mix
->mono_vol
[info
->index
] = ucontrol
->value
.integer
.value
[0];
474 tumbler_set_mono_volume(mix
, info
);
479 /* TAS3001c mono volumes */
480 static struct tumbler_mono_vol tumbler_pcm_vol_info
= {
481 .index
= VOL_IDX_PCM_MONO
,
484 .max
= ARRAY_SIZE(mixer_volume_table
),
485 .table
= mixer_volume_table
,
488 static struct tumbler_mono_vol tumbler_bass_vol_info
= {
489 .index
= VOL_IDX_BASS
,
492 .max
= ARRAY_SIZE(bass_volume_table
),
493 .table
= bass_volume_table
,
496 static struct tumbler_mono_vol tumbler_treble_vol_info
= {
497 .index
= VOL_IDX_TREBLE
,
498 .reg
= TAS_REG_TREBLE
,
500 .max
= ARRAY_SIZE(treble_volume_table
),
501 .table
= treble_volume_table
,
504 /* TAS3004 mono volumes */
505 static struct tumbler_mono_vol snapper_bass_vol_info
= {
506 .index
= VOL_IDX_BASS
,
509 .max
= ARRAY_SIZE(snapper_bass_volume_table
),
510 .table
= snapper_bass_volume_table
,
513 static struct tumbler_mono_vol snapper_treble_vol_info
= {
514 .index
= VOL_IDX_TREBLE
,
515 .reg
= TAS_REG_TREBLE
,
517 .max
= ARRAY_SIZE(snapper_treble_volume_table
),
518 .table
= snapper_treble_volume_table
,
522 #define DEFINE_MONO(xname,type) { \
523 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,\
525 .info = tumbler_info_mono, \
526 .get = tumbler_get_mono, \
527 .put = tumbler_put_mono, \
528 .private_value = (unsigned long)(&tumbler_##type##_vol_info), \
531 #define DEFINE_SNAPPER_MONO(xname,type) { \
532 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,\
534 .info = tumbler_info_mono, \
535 .get = tumbler_get_mono, \
536 .put = tumbler_put_mono, \
537 .private_value = (unsigned long)(&snapper_##type##_vol_info), \
542 * snapper mixer volumes
545 static int snapper_set_mix_vol1(pmac_tumbler_t
*mix
, int idx
, int ch
, int reg
)
548 unsigned char block
[9];
550 vol
= mix
->mix_vol
[idx
][ch
];
551 if (vol
>= ARRAY_SIZE(mixer_volume_table
)) {
552 vol
= ARRAY_SIZE(mixer_volume_table
) - 1;
553 mix
->mix_vol
[idx
][ch
] = vol
;
556 for (i
= 0; i
< 3; i
++) {
557 vol
= mix
->mix_vol
[i
][ch
];
558 vol
= mixer_volume_table
[vol
];
559 for (j
= 0; j
< 3; j
++)
560 block
[i
* 3 + j
] = (vol
>> ((2 - j
) * 8)) & 0xff;
562 if (snd_pmac_keywest_write(&mix
->i2c
, reg
, 9, block
) < 0) {
563 snd_printk("failed to set mono volume %d\n", reg
);
569 static int snapper_set_mix_vol(pmac_tumbler_t
*mix
, int idx
)
571 if (! mix
->i2c
.client
)
573 if (snapper_set_mix_vol1(mix
, idx
, 0, TAS_REG_LMIX
) < 0 ||
574 snapper_set_mix_vol1(mix
, idx
, 1, TAS_REG_RMIX
) < 0)
579 static int snapper_info_mix(snd_kcontrol_t
*kcontrol
, snd_ctl_elem_info_t
*uinfo
)
581 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
583 uinfo
->value
.integer
.min
= 0;
584 uinfo
->value
.integer
.max
= ARRAY_SIZE(mixer_volume_table
) - 1;
588 static int snapper_get_mix(snd_kcontrol_t
*kcontrol
, snd_ctl_elem_value_t
*ucontrol
)
590 int idx
= (int)kcontrol
->private_value
;
591 pmac_t
*chip
= snd_kcontrol_chip(kcontrol
);
593 if (! (mix
= chip
->mixer_data
))
595 ucontrol
->value
.integer
.value
[0] = mix
->mix_vol
[idx
][0];
596 ucontrol
->value
.integer
.value
[1] = mix
->mix_vol
[idx
][1];
600 static int snapper_put_mix(snd_kcontrol_t
*kcontrol
, snd_ctl_elem_value_t
*ucontrol
)
602 int idx
= (int)kcontrol
->private_value
;
603 pmac_t
*chip
= snd_kcontrol_chip(kcontrol
);
607 if (! (mix
= chip
->mixer_data
))
609 change
= mix
->mix_vol
[idx
][0] != ucontrol
->value
.integer
.value
[0] ||
610 mix
->mix_vol
[idx
][1] != ucontrol
->value
.integer
.value
[1];
612 mix
->mix_vol
[idx
][0] = ucontrol
->value
.integer
.value
[0];
613 mix
->mix_vol
[idx
][1] = ucontrol
->value
.integer
.value
[1];
614 snapper_set_mix_vol(mix
, idx
);
624 enum { TUMBLER_MUTE_HP
, TUMBLER_MUTE_AMP
};
626 static int tumbler_get_mute_switch(snd_kcontrol_t
*kcontrol
, snd_ctl_elem_value_t
*ucontrol
)
628 pmac_t
*chip
= snd_kcontrol_chip(kcontrol
);
631 if (! (mix
= chip
->mixer_data
))
633 gp
= (kcontrol
->private_value
== TUMBLER_MUTE_HP
) ? &mix
->hp_mute
: &mix
->amp_mute
;
634 ucontrol
->value
.integer
.value
[0] = ! read_audio_gpio(gp
);
638 static int tumbler_put_mute_switch(snd_kcontrol_t
*kcontrol
, snd_ctl_elem_value_t
*ucontrol
)
640 pmac_t
*chip
= snd_kcontrol_chip(kcontrol
);
644 if (! (mix
= chip
->mixer_data
))
646 gp
= (kcontrol
->private_value
== TUMBLER_MUTE_HP
) ? &mix
->hp_mute
: &mix
->amp_mute
;
647 val
= ! read_audio_gpio(gp
);
648 if (val
!= ucontrol
->value
.integer
.value
[0]) {
649 write_audio_gpio(gp
, ! ucontrol
->value
.integer
.value
[0]);
655 #define DEFINE_SNAPPER_MIX(xname,idx,ofs) { \
656 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,\
658 .info = snapper_info_mix, \
659 .get = snapper_get_mix, \
660 .put = snapper_put_mix, \
662 .private_value = ofs, \
668 static snd_kcontrol_new_t tumbler_mixers
[] __initdata
= {
669 { .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
670 .name
= "Master Playback Volume",
671 .info
= tumbler_info_master_volume
,
672 .get
= tumbler_get_master_volume
,
673 .put
= tumbler_put_master_volume
675 { .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
676 .name
= "Master Playback Switch",
677 .info
= snd_pmac_boolean_stereo_info
,
678 .get
= tumbler_get_master_switch
,
679 .put
= tumbler_put_master_switch
681 DEFINE_MONO("Tone Control - Bass", bass
),
682 DEFINE_MONO("Tone Control - Treble", treble
),
683 DEFINE_MONO("PCM Playback Volume", pcm
),
684 { .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
685 .name
= "DRC Switch",
686 .info
= snd_pmac_boolean_mono_info
,
687 .get
= tumbler_get_drc_switch
,
688 .put
= tumbler_put_drc_switch
690 { .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
692 .info
= tumbler_info_drc_value
,
693 .get
= tumbler_get_drc_value
,
694 .put
= tumbler_put_drc_value
698 static snd_kcontrol_new_t snapper_mixers
[] __initdata
= {
699 { .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
700 .name
= "Master Playback Volume",
701 .info
= tumbler_info_master_volume
,
702 .get
= tumbler_get_master_volume
,
703 .put
= tumbler_put_master_volume
705 { .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
706 .name
= "Master Playback Switch",
707 .info
= snd_pmac_boolean_stereo_info
,
708 .get
= tumbler_get_master_switch
,
709 .put
= tumbler_put_master_switch
711 DEFINE_SNAPPER_MIX("PCM Playback Volume", 0, VOL_IDX_PCM
),
712 DEFINE_SNAPPER_MIX("PCM Playback Volume", 1, VOL_IDX_PCM2
),
713 DEFINE_SNAPPER_MIX("Monitor Mix Volume", 0, VOL_IDX_ADC
),
714 DEFINE_SNAPPER_MONO("Tone Control - Bass", bass
),
715 DEFINE_SNAPPER_MONO("Tone Control - Treble", treble
),
716 { .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
717 .name
= "DRC Switch",
718 .info
= snd_pmac_boolean_mono_info
,
719 .get
= tumbler_get_drc_switch
,
720 .put
= tumbler_put_drc_switch
722 { .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
724 .info
= tumbler_info_drc_value
,
725 .get
= tumbler_get_drc_value
,
726 .put
= tumbler_put_drc_value
730 static snd_kcontrol_new_t tumbler_hp_sw __initdata
= {
731 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
732 .name
= "Headphone Playback Switch",
733 .info
= snd_pmac_boolean_mono_info
,
734 .get
= tumbler_get_mute_switch
,
735 .put
= tumbler_put_mute_switch
,
736 .private_value
= TUMBLER_MUTE_HP
,
738 static snd_kcontrol_new_t tumbler_speaker_sw __initdata
= {
739 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
740 .name
= "PC Speaker Playback Switch",
741 .info
= snd_pmac_boolean_mono_info
,
742 .get
= tumbler_get_mute_switch
,
743 .put
= tumbler_put_mute_switch
,
744 .private_value
= TUMBLER_MUTE_AMP
,
747 #ifdef PMAC_SUPPORT_AUTOMUTE
751 static int tumbler_detect_headphone(pmac_t
*chip
)
753 pmac_tumbler_t
*mix
= chip
->mixer_data
;
754 return read_audio_gpio(&mix
->hp_detect
);
757 static void check_mute(pmac_t
*chip
, pmac_gpio_t
*gp
, int val
, int do_notify
, snd_kcontrol_t
*sw
)
759 //pmac_tumbler_t *mix = chip->mixer_data;
760 if (val
!= read_audio_gpio(gp
)) {
761 write_audio_gpio(gp
, val
);
763 snd_ctl_notify(chip
->card
, SNDRV_CTL_EVENT_MASK_VALUE
, &sw
->id
);
767 static void tumbler_update_automute(pmac_t
*chip
, int do_notify
)
769 if (chip
->auto_mute
) {
770 pmac_tumbler_t
*mix
= chip
->mixer_data
;
771 snd_assert(mix
, return);
772 if (tumbler_detect_headphone(chip
)) {
774 check_mute(chip
, &mix
->amp_mute
, 1, do_notify
, chip
->speaker_sw_ctl
);
775 check_mute(chip
, &mix
->hp_mute
, 0, do_notify
, chip
->master_sw_ctl
);
778 check_mute(chip
, &mix
->amp_mute
, 0, do_notify
, chip
->speaker_sw_ctl
);
779 check_mute(chip
, &mix
->hp_mute
, 1, do_notify
, chip
->master_sw_ctl
);
782 snd_ctl_notify(chip
->card
, SNDRV_CTL_EVENT_MASK_VALUE
,
783 &chip
->hp_detect_ctl
->id
);
786 #endif /* PMAC_SUPPORT_AUTOMUTE */
789 /* interrupt - headphone plug changed */
790 static irqreturn_t
headphone_intr(int irq
, void *devid
, struct pt_regs
*regs
)
792 pmac_t
*chip
= snd_magic_cast(pmac_t
, devid
, return);
793 if (chip
->update_automute
&& chip
->initialized
) {
794 chip
->update_automute(chip
, 1);
800 /* look for audio-gpio device */
801 static struct device_node
*find_audio_device(const char *name
)
803 struct device_node
*np
;
805 if (! (np
= find_devices("gpio")))
808 for (np
= np
->child
; np
; np
= np
->sibling
) {
809 char *property
= get_property(np
, "audio-gpio", NULL
);
810 if (property
&& strcmp(property
, name
) == 0)
816 /* look for audio-gpio device */
817 static struct device_node
*find_compatible_audio_device(const char *name
)
819 struct device_node
*np
;
821 if (! (np
= find_devices("gpio")))
824 for (np
= np
->child
; np
; np
= np
->sibling
) {
825 if (device_is_compatible(np
, name
))
831 /* find an audio device and get its address */
832 static unsigned long tumbler_find_device(const char *device
, pmac_gpio_t
*gp
, int is_compatible
)
834 struct device_node
*node
;
838 node
= find_compatible_audio_device(device
);
840 node
= find_audio_device(device
);
842 snd_printdd("cannot find device %s\n", device
);
846 base
= (u32
*)get_property(node
, "AAPL,address", NULL
);
848 snd_printd("cannot find address for device %s\n", device
);
852 #ifdef CONFIG_PPC_HAS_FEATURE_CALLS
853 gp
->addr
= (*base
) & 0x0000ffff;
855 gp
->addr
= (void*)ioremap((unsigned long)(*base
), 1);
857 base
= (u32
*)get_property(node
, "audio-gpio-active-state", NULL
);
859 gp
->active_state
= *base
;
861 gp
->active_state
= 1;
864 return (node
->n_intrs
> 0) ? node
->intrs
[0].line
: 0;
868 static void tumbler_reset_audio(pmac_t
*chip
)
870 pmac_tumbler_t
*mix
= chip
->mixer_data
;
872 write_audio_gpio(&mix
->audio_reset
, 0);
874 write_audio_gpio(&mix
->audio_reset
, 1);
876 write_audio_gpio(&mix
->audio_reset
, 0);
880 #ifdef CONFIG_PMAC_PBOOK
882 /* we call the i2c transfer in a workqueue because it may need either schedule()
883 * or completion from timer interrupts.
885 static void tumbler_resume_work(void *arg
)
887 pmac_t
*chip
= (pmac_t
*)arg
;
888 pmac_tumbler_t
*mix
= chip
->mixer_data
;
890 tumbler_reset_audio(chip
);
891 if (mix
->i2c
.client
) {
892 if (tumbler_init_client(&mix
->i2c
) < 0)
893 printk(KERN_ERR
"tumbler_init_client error\n");
895 printk(KERN_ERR
"tumbler: i2c is not initialized\n");
896 if (chip
->model
== PMAC_TUMBLER
) {
897 tumbler_set_mono_volume(mix
, &tumbler_pcm_vol_info
);
898 tumbler_set_mono_volume(mix
, &tumbler_bass_vol_info
);
899 tumbler_set_mono_volume(mix
, &tumbler_treble_vol_info
);
900 tumbler_set_drc(mix
);
902 snapper_set_mix_vol(mix
, VOL_IDX_PCM
);
903 snapper_set_mix_vol(mix
, VOL_IDX_PCM2
);
904 snapper_set_mix_vol(mix
, VOL_IDX_ADC
);
905 tumbler_set_mono_volume(mix
, &tumbler_bass_vol_info
);
906 tumbler_set_mono_volume(mix
, &tumbler_treble_vol_info
);
907 snapper_set_drc(mix
);
909 tumbler_set_master_volume(mix
);
910 if (chip
->update_automute
)
911 chip
->update_automute(chip
, 0);
914 static void tumbler_resume(pmac_t
*chip
)
916 pmac_tumbler_t
*mix
= chip
->mixer_data
;
917 snd_assert(mix
, return);
918 INIT_WORK(&mix
->resume_workq
, tumbler_resume_work
, chip
);
919 if (schedule_work(&mix
->resume_workq
))
921 printk(KERN_ERR
"ALSA tumbler: cannot schedule resume-workqueue.\n");
925 /* initialize tumbler */
926 static int __init
tumbler_init(pmac_t
*chip
)
929 pmac_tumbler_t
*mix
= chip
->mixer_data
;
930 snd_assert(mix
, return -EINVAL
);
932 tumbler_find_device("audio-hw-reset", &mix
->audio_reset
, 0);
933 tumbler_find_device("amp-mute", &mix
->amp_mute
, 0);
934 tumbler_find_device("headphone-mute", &mix
->hp_mute
, 0);
935 irq
= tumbler_find_device("headphone-detect", &mix
->hp_detect
, 0);
937 irq
= tumbler_find_device("keywest-gpio15", &mix
->hp_detect
, 1);
939 tumbler_reset_audio(chip
);
941 /* activate headphone status interrupts */
944 if ((err
= request_irq(irq
, headphone_intr
, 0,
945 "Tumbler Headphone Detection", chip
)) < 0)
947 /* activate headphone status interrupts */
948 val
= do_gpio_read(&mix
->hp_detect
);
949 do_gpio_write(&mix
->hp_detect
, val
| 0x80);
951 mix
->headphone_irq
= irq
;
956 static void tumbler_cleanup(pmac_t
*chip
)
958 pmac_tumbler_t
*mix
= chip
->mixer_data
;
962 if (mix
->headphone_irq
>= 0)
963 free_irq(mix
->headphone_irq
, chip
);
964 tumbler_gpio_free(&mix
->audio_reset
);
965 tumbler_gpio_free(&mix
->amp_mute
);
966 tumbler_gpio_free(&mix
->hp_mute
);
967 tumbler_gpio_free(&mix
->hp_detect
);
968 snd_pmac_keywest_cleanup(&mix
->i2c
);
970 chip
->mixer_data
= NULL
;
974 int __init
snd_pmac_tumbler_init(pmac_t
*chip
)
979 struct device_node
*tas_node
;
983 request_module("i2c-keywest");
984 #endif /* CONFIG_KMOD */
986 mix
= kmalloc(sizeof(*mix
), GFP_KERNEL
);
989 memset(mix
, 0, sizeof(*mix
));
990 mix
->headphone_irq
= -1;
992 chip
->mixer_data
= mix
;
993 chip
->mixer_free
= tumbler_cleanup
;
996 tas_node
= find_devices("deq");
997 if (tas_node
== NULL
)
1000 paddr
= (u32
*)get_property(tas_node
, "i2c-address", NULL
);
1002 mix
->i2c
.addr
= (*paddr
) >> 1;
1004 mix
->i2c
.addr
= TAS_I2C_ADDR
;
1006 mix
->i2c
.init_client
= tumbler_init_client
;
1007 if (chip
->model
== PMAC_TUMBLER
) {
1008 mix
->i2c
.name
= "TAS3001c";
1009 chipname
= "Tumbler";
1011 mix
->i2c
.name
= "TAS3004";
1012 chipname
= "Snapper";
1015 if ((err
= snd_pmac_keywest_init(&mix
->i2c
)) < 0)
1021 sprintf(chip
->card
->mixername
, "PowerMac %s", chipname
);
1023 if (chip
->model
== PMAC_TUMBLER
) {
1024 for (i
= 0; i
< ARRAY_SIZE(tumbler_mixers
); i
++) {
1025 if ((err
= snd_ctl_add(chip
->card
, snd_ctl_new1(&tumbler_mixers
[i
], chip
))) < 0)
1029 for (i
= 0; i
< ARRAY_SIZE(snapper_mixers
); i
++) {
1030 if ((err
= snd_ctl_add(chip
->card
, snd_ctl_new1(&snapper_mixers
[i
], chip
))) < 0)
1034 chip
->master_sw_ctl
= snd_ctl_new1(&tumbler_hp_sw
, chip
);
1035 if ((err
= snd_ctl_add(chip
->card
, chip
->master_sw_ctl
)) < 0)
1037 chip
->speaker_sw_ctl
= snd_ctl_new1(&tumbler_speaker_sw
, chip
);
1038 if ((err
= snd_ctl_add(chip
->card
, chip
->speaker_sw_ctl
)) < 0)
1041 if ((err
= tumbler_init(chip
)) < 0)
1044 #ifdef CONFIG_PMAC_PBOOK
1045 chip
->resume
= tumbler_resume
;
1048 #ifdef PMAC_SUPPORT_AUTOMUTE
1049 if (mix
->headphone_irq
>=0 && (err
= snd_pmac_add_automute(chip
)) < 0)
1051 chip
->detect_headphone
= tumbler_detect_headphone
;
1052 chip
->update_automute
= tumbler_update_automute
;
1053 tumbler_update_automute(chip
, 0); /* update the status only */