Get rid of arch/mips64/kernel. 9116 lines of code gone.
[linux-2.6/linux-mips.git] / sound / ppc / tumbler.c
blobad914857ddb5e997bf517239c26f5a765bb18faa
1 /*
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>
32 #include <asm/io.h>
33 #include <asm/irq.h>
34 #ifdef CONFIG_PPC_HAS_FEATURE_CALLS
35 #include <asm/pmac_feature.h>
36 #endif
37 #include "pmac.h"
38 #include "tumbler_volume.h"
40 #define chip_t pmac_t
42 /* i2c address for tumbler */
43 #define TAS_I2C_ADDR 0x34
45 /* registers */
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
54 /* tas3001c */
55 #define TAS_REG_PCM TAS_REG_INPUT1
57 /* tas3004 */
58 #define TAS_REG_LMIX TAS_REG_INPUT1
59 #define TAS_REG_RMIX TAS_REG_INPUT2
61 /* mono volumes for tas3001c/tas3004 */
62 enum {
63 VOL_IDX_PCM_MONO, /* tas3001c only */
64 VOL_IDX_BASS, VOL_IDX_TREBLE,
65 VOL_IDX_LAST_MONO
68 /* stereo volumes for tas3004 */
69 enum {
70 VOL_IDX_PCM, VOL_IDX_PCM2, VOL_IDX_ADC,
71 VOL_IDX_LAST_MIX
74 typedef struct pmac_gpio {
75 #ifdef CONFIG_PPC_HAS_FEATURE_CALLS
76 unsigned int addr;
77 #else
78 void *addr;
79 #endif
80 int active_state;
81 } pmac_gpio_t;
83 typedef struct pmac_tumbler_t {
84 pmac_keywest_t i2c;
85 pmac_gpio_t audio_reset;
86 pmac_gpio_t amp_mute;
87 pmac_gpio_t hp_mute;
88 pmac_gpio_t hp_detect;
89 int headphone_irq;
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 */
94 int drc_range;
95 int drc_enable;
96 #ifdef CONFIG_PMAC_PBOOK
97 struct work_struct resume_workq;
98 #endif
99 } pmac_tumbler_t;
105 static int tumbler_init_client(pmac_keywest_t *i2c)
107 int err, count = 10;
108 do {
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);
112 if (err >= 0)
113 return err;
114 mdelay(10);
115 } while (count--);
116 return -ENXIO;
121 * gpio access
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 */
129 #else
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)
134 if (gp->addr) {
135 iounmap(gp->addr);
136 gp->addr = 0;
139 #endif /* CONFIG_PPC_HAS_FEATURE_CALLS */
141 static void write_audio_gpio(pmac_gpio_t *gp, int active)
143 if (! gp->addr)
144 return;
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)
151 int ret;
152 if (! gp->addr)
153 return 0;
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)
167 return -ENODEV;
169 if (! mix->master_switch[0])
170 left_vol = 0;
171 else {
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])
178 right_vol = 0;
179 else {
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");
196 return -EINVAL;
198 return 0;
202 /* output volume */
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;
206 uinfo->count = 2;
207 uinfo->value.integer.min = 0;
208 uinfo->value.integer.max = ARRAY_SIZE(master_volume_table) - 1;
209 return 0;
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];
219 return 0;
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;
226 int change;
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];
231 if (change) {
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);
236 return change;
239 /* output switch */
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];
247 return 0;
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;
254 int change;
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];
259 if (change) {
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);
264 return change;
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)
279 return -ENODEV;
281 if (mix->drc_enable) {
282 val[0] = 0xc1; /* enable, 3:1 compression */
283 if (mix->drc_range > TAS3001_DRC_MAX)
284 val[1] = 0xf0;
285 else if (mix->drc_range < 0)
286 val[1] = 0x91;
287 else
288 val[1] = mix->drc_range + 0x91;
289 } else {
290 val[0] = 0;
291 val[1] = 0;
294 if (snd_pmac_keywest_write(&mix->i2c, TAS_REG_DRC, 2, val) < 0) {
295 snd_printk("failed to set DRC\n");
296 return -EINVAL;
298 return 0;
302 * TAS3004
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)
312 return -ENODEV;
314 if (mix->drc_enable)
315 val[0] = 0x50; /* 3:1 above threshold */
316 else
317 val[0] = 0x51; /* disabled */
318 val[1] = 0x02; /* 1:1 below threshold */
319 if (mix->drc_range > 0xef)
320 val[2] = 0xef;
321 else if (mix->drc_range < 0)
322 val[2] = 0x00;
323 else
324 val[2] = mix->drc_range;
325 val[3] = 0xb0;
326 val[4] = 0x60;
327 val[5] = 0xa0;
329 if (snd_pmac_keywest_write(&mix->i2c, TAS_REG_DRC, 6, val) < 0) {
330 snd_printk("failed to set DRC\n");
331 return -EINVAL;
333 return 0;
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;
340 uinfo->count = 1;
341 uinfo->value.integer.min = 0;
342 uinfo->value.integer.max =
343 chip->model == PMAC_TUMBLER ? TAS3001_DRC_MAX : TAS3004_DRC_MAX;
344 return 0;
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);
350 pmac_tumbler_t *mix;
351 if (! (mix = chip->mixer_data))
352 return -ENODEV;
353 ucontrol->value.integer.value[0] = mix->drc_range;
354 return 0;
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);
360 pmac_tumbler_t *mix;
361 int change;
363 if (! (mix = chip->mixer_data))
364 return -ENODEV;
365 change = mix->drc_range != ucontrol->value.integer.value[0];
366 if (change) {
367 mix->drc_range = ucontrol->value.integer.value[0];
368 if (chip->model == PMAC_TUMBLER)
369 tumbler_set_drc(mix);
370 else
371 snapper_set_drc(mix);
373 return change;
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);
379 pmac_tumbler_t *mix;
380 if (! (mix = chip->mixer_data))
381 return -ENODEV;
382 ucontrol->value.integer.value[0] = mix->drc_enable;
383 return 0;
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);
389 pmac_tumbler_t *mix;
390 int change;
392 if (! (mix = chip->mixer_data))
393 return -ENODEV;
394 change = mix->drc_enable != ucontrol->value.integer.value[0];
395 if (change) {
396 mix->drc_enable = !!ucontrol->value.integer.value[0];
397 if (chip->model == PMAC_TUMBLER)
398 tumbler_set_drc(mix);
399 else
400 snapper_set_drc(mix);
402 return change;
407 * mono volumes
410 struct tumbler_mono_vol {
411 int index;
412 int reg;
413 int bytes;
414 unsigned int max;
415 unsigned int *table;
418 static int tumbler_set_mono_volume(pmac_tumbler_t *mix, struct tumbler_mono_vol *info)
420 unsigned char block[4];
421 unsigned int vol;
422 int i;
424 if (! mix->i2c.client)
425 return -ENODEV;
427 vol = mix->mono_vol[info->index];
428 if (vol >= info->max)
429 vol = info->max - 1;
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);
435 return -EINVAL;
437 return 0;
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;
445 uinfo->count = 1;
446 uinfo->value.integer.min = 0;
447 uinfo->value.integer.max = info->max - 1;
448 return 0;
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);
455 pmac_tumbler_t *mix;
456 if (! (mix = chip->mixer_data))
457 return -ENODEV;
458 ucontrol->value.integer.value[0] = mix->mono_vol[info->index];
459 return 0;
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);
466 pmac_tumbler_t *mix;
467 int change;
469 if (! (mix = chip->mixer_data))
470 return -ENODEV;
471 change = mix->mono_vol[info->index] != ucontrol->value.integer.value[0];
472 if (change) {
473 mix->mono_vol[info->index] = ucontrol->value.integer.value[0];
474 tumbler_set_mono_volume(mix, info);
476 return change;
479 /* TAS3001c mono volumes */
480 static struct tumbler_mono_vol tumbler_pcm_vol_info = {
481 .index = VOL_IDX_PCM_MONO,
482 .reg = TAS_REG_PCM,
483 .bytes = 3,
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,
490 .reg = TAS_REG_BASS,
491 .bytes = 1,
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,
499 .bytes = 1,
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,
507 .reg = TAS_REG_BASS,
508 .bytes = 1,
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,
516 .bytes = 1,
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,\
524 .name = xname, \
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,\
533 .name = xname, \
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)
547 int i, j, vol;
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);
564 return -EINVAL;
566 return 0;
569 static int snapper_set_mix_vol(pmac_tumbler_t *mix, int idx)
571 if (! mix->i2c.client)
572 return -ENODEV;
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)
575 return -EINVAL;
576 return 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;
582 uinfo->count = 2;
583 uinfo->value.integer.min = 0;
584 uinfo->value.integer.max = ARRAY_SIZE(mixer_volume_table) - 1;
585 return 0;
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);
592 pmac_tumbler_t *mix;
593 if (! (mix = chip->mixer_data))
594 return -ENODEV;
595 ucontrol->value.integer.value[0] = mix->mix_vol[idx][0];
596 ucontrol->value.integer.value[1] = mix->mix_vol[idx][1];
597 return 0;
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);
604 pmac_tumbler_t *mix;
605 int change;
607 if (! (mix = chip->mixer_data))
608 return -ENODEV;
609 change = mix->mix_vol[idx][0] != ucontrol->value.integer.value[0] ||
610 mix->mix_vol[idx][1] != ucontrol->value.integer.value[1];
611 if (change) {
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);
616 return change;
621 * mute switches
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);
629 pmac_tumbler_t *mix;
630 pmac_gpio_t *gp;
631 if (! (mix = chip->mixer_data))
632 return -ENODEV;
633 gp = (kcontrol->private_value == TUMBLER_MUTE_HP) ? &mix->hp_mute : &mix->amp_mute;
634 ucontrol->value.integer.value[0] = ! read_audio_gpio(gp);
635 return 0;
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);
641 pmac_tumbler_t *mix;
642 pmac_gpio_t *gp;
643 int val;
644 if (! (mix = chip->mixer_data))
645 return -ENODEV;
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]);
650 return 1;
652 return 0;
655 #define DEFINE_SNAPPER_MIX(xname,idx,ofs) { \
656 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,\
657 .name = xname, \
658 .info = snapper_info_mix, \
659 .get = snapper_get_mix, \
660 .put = snapper_put_mix, \
661 .index = idx,\
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,
691 .name = "DRC Range",
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,
723 .name = "DRC Range",
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
749 * auto-mute stuffs
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);
762 if (do_notify)
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)) {
773 /* mute speaker */
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);
776 } else {
777 /* unmute speaker */
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);
781 if (do_notify)
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);
795 return IRQ_HANDLED;
797 return IRQ_NONE;
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")))
806 return NULL;
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)
811 return np;
813 return NULL;
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")))
822 return NULL;
824 for (np = np->child; np; np = np->sibling) {
825 if (device_is_compatible(np, name))
826 return np;
828 return NULL;
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;
835 u32 *base;
837 if (is_compatible)
838 node = find_compatible_audio_device(device);
839 else
840 node = find_audio_device(device);
841 if (! node) {
842 snd_printdd("cannot find device %s\n", device);
843 return -ENODEV;
846 base = (u32 *)get_property(node, "AAPL,address", NULL);
847 if (! base) {
848 snd_printd("cannot find address for device %s\n", device);
849 return -ENODEV;
852 #ifdef CONFIG_PPC_HAS_FEATURE_CALLS
853 gp->addr = (*base) & 0x0000ffff;
854 #else
855 gp->addr = (void*)ioremap((unsigned long)(*base), 1);
856 #endif
857 base = (u32 *)get_property(node, "audio-gpio-active-state", NULL);
858 if (base)
859 gp->active_state = *base;
860 else
861 gp->active_state = 1;
864 return (node->n_intrs > 0) ? node->intrs[0].line : 0;
867 /* reset audio */
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);
873 mdelay(200);
874 write_audio_gpio(&mix->audio_reset, 1);
875 mdelay(100);
876 write_audio_gpio(&mix->audio_reset, 0);
877 mdelay(100);
880 #ifdef CONFIG_PMAC_PBOOK
881 /* resume mixer */
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");
894 } else
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);
901 } else {
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))
920 return;
921 printk(KERN_ERR "ALSA tumbler: cannot schedule resume-workqueue.\n");
923 #endif
925 /* initialize tumbler */
926 static int __init tumbler_init(pmac_t *chip)
928 int irq, err;
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);
936 if (irq < 0)
937 irq = tumbler_find_device("keywest-gpio15", &mix->hp_detect, 1);
939 tumbler_reset_audio(chip);
941 /* activate headphone status interrupts */
942 if (irq >= 0) {
943 unsigned char val;
944 if ((err = request_irq(irq, headphone_intr, 0,
945 "Tumbler Headphone Detection", chip)) < 0)
946 return err;
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;
953 return 0;
956 static void tumbler_cleanup(pmac_t *chip)
958 pmac_tumbler_t *mix = chip->mixer_data;
959 if (! mix)
960 return;
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);
969 kfree(mix);
970 chip->mixer_data = NULL;
973 /* exported */
974 int __init snd_pmac_tumbler_init(pmac_t *chip)
976 int i, err;
977 pmac_tumbler_t *mix;
978 u32 *paddr;
979 struct device_node *tas_node;
980 char *chipname;
982 #ifdef CONFIG_KMOD
983 request_module("i2c-keywest");
984 #endif /* CONFIG_KMOD */
986 mix = kmalloc(sizeof(*mix), GFP_KERNEL);
987 if (! mix)
988 return -ENOMEM;
989 memset(mix, 0, sizeof(*mix));
990 mix->headphone_irq = -1;
992 chip->mixer_data = mix;
993 chip->mixer_free = tumbler_cleanup;
995 /* set up TAS */
996 tas_node = find_devices("deq");
997 if (tas_node == NULL)
998 return -ENODEV;
1000 paddr = (u32 *)get_property(tas_node, "i2c-address", NULL);
1001 if (paddr)
1002 mix->i2c.addr = (*paddr) >> 1;
1003 else
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";
1010 } else {
1011 mix->i2c.name = "TAS3004";
1012 chipname = "Snapper";
1015 if ((err = snd_pmac_keywest_init(&mix->i2c)) < 0)
1016 return err;
1019 * build mixers
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)
1026 return err;
1028 } else {
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)
1031 return err;
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)
1036 return err;
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)
1039 return err;
1041 if ((err = tumbler_init(chip)) < 0)
1042 return err;
1044 #ifdef CONFIG_PMAC_PBOOK
1045 chip->resume = tumbler_resume;
1046 #endif
1048 #ifdef PMAC_SUPPORT_AUTOMUTE
1049 if (mix->headphone_irq >=0 && (err = snd_pmac_add_automute(chip)) < 0)
1050 return err;
1051 chip->detect_headphone = tumbler_detect_headphone;
1052 chip->update_automute = tumbler_update_automute;
1053 tumbler_update_automute(chip, 0); /* update the status only */
1054 #endif
1056 return 0;