[PATCH] PG_uncached is ia64 only
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / sound / ppc / awacs.c
blob82d791be74993574aa8b3865b23ca269e90adb16
1 /*
2 * PMac AWACS lowlevel functions
4 * Copyright (c) by Takashi Iwai <tiwai@suse.de>
5 * code based on dmasound.c.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <sound/driver.h>
24 #include <asm/io.h>
25 #include <asm/nvram.h>
26 #include <linux/init.h>
27 #include <linux/delay.h>
28 #include <linux/slab.h>
29 #include <sound/core.h>
30 #include "pmac.h"
33 #ifdef CONFIG_ADB_CUDA
34 #define PMAC_AMP_AVAIL
35 #endif
37 #ifdef PMAC_AMP_AVAIL
38 struct awacs_amp {
39 unsigned char amp_master;
40 unsigned char amp_vol[2][2];
41 unsigned char amp_tone[2];
44 #define CHECK_CUDA_AMP() (sys_ctrler == SYS_CTRLER_CUDA)
46 #endif /* PMAC_AMP_AVAIL */
49 static void snd_pmac_screamer_wait(struct snd_pmac *chip)
51 long timeout = 2000;
52 while (!(in_le32(&chip->awacs->codec_stat) & MASK_VALID)) {
53 mdelay(1);
54 if (! --timeout) {
55 snd_printd("snd_pmac_screamer_wait timeout\n");
56 break;
62 * write AWACS register
64 static void
65 snd_pmac_awacs_write(struct snd_pmac *chip, int val)
67 long timeout = 5000000;
69 if (chip->model == PMAC_SCREAMER)
70 snd_pmac_screamer_wait(chip);
71 out_le32(&chip->awacs->codec_ctrl, val | (chip->subframe << 22));
72 while (in_le32(&chip->awacs->codec_ctrl) & MASK_NEWECMD) {
73 if (! --timeout) {
74 snd_printd("snd_pmac_awacs_write timeout\n");
75 break;
80 static void
81 snd_pmac_awacs_write_reg(struct snd_pmac *chip, int reg, int val)
83 snd_pmac_awacs_write(chip, val | (reg << 12));
84 chip->awacs_reg[reg] = val;
87 static void
88 snd_pmac_awacs_write_noreg(struct snd_pmac *chip, int reg, int val)
90 snd_pmac_awacs_write(chip, val | (reg << 12));
93 #ifdef CONFIG_PM
94 /* Recalibrate chip */
95 static void screamer_recalibrate(struct snd_pmac *chip)
97 if (chip->model != PMAC_SCREAMER)
98 return;
100 /* Sorry for the horrible delays... I hope to get that improved
101 * by making the whole PM process asynchronous in a future version
103 snd_pmac_awacs_write_noreg(chip, 1, chip->awacs_reg[1]);
104 if (chip->manufacturer == 0x1)
105 /* delay for broken crystal part */
106 msleep(750);
107 snd_pmac_awacs_write_noreg(chip, 1,
108 chip->awacs_reg[1] | MASK_RECALIBRATE |
109 MASK_CMUTE | MASK_AMUTE);
110 snd_pmac_awacs_write_noreg(chip, 1, chip->awacs_reg[1]);
111 snd_pmac_awacs_write_noreg(chip, 6, chip->awacs_reg[6]);
114 #else
115 #define screamer_recalibrate(chip) /* NOP */
116 #endif
120 * additional callback to set the pcm format
122 static void snd_pmac_awacs_set_format(struct snd_pmac *chip)
124 chip->awacs_reg[1] &= ~MASK_SAMPLERATE;
125 chip->awacs_reg[1] |= chip->rate_index << 3;
126 snd_pmac_awacs_write_reg(chip, 1, chip->awacs_reg[1]);
131 * AWACS volume callbacks
134 * volumes: 0-15 stereo
136 static int snd_pmac_awacs_info_volume(struct snd_kcontrol *kcontrol,
137 struct snd_ctl_elem_info *uinfo)
139 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
140 uinfo->count = 2;
141 uinfo->value.integer.min = 0;
142 uinfo->value.integer.max = 15;
143 return 0;
146 static int snd_pmac_awacs_get_volume(struct snd_kcontrol *kcontrol,
147 struct snd_ctl_elem_value *ucontrol)
149 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
150 int reg = kcontrol->private_value & 0xff;
151 int lshift = (kcontrol->private_value >> 8) & 0xff;
152 int inverted = (kcontrol->private_value >> 16) & 1;
153 unsigned long flags;
154 int vol[2];
156 spin_lock_irqsave(&chip->reg_lock, flags);
157 vol[0] = (chip->awacs_reg[reg] >> lshift) & 0xf;
158 vol[1] = chip->awacs_reg[reg] & 0xf;
159 spin_unlock_irqrestore(&chip->reg_lock, flags);
160 if (inverted) {
161 vol[0] = 0x0f - vol[0];
162 vol[1] = 0x0f - vol[1];
164 ucontrol->value.integer.value[0] = vol[0];
165 ucontrol->value.integer.value[1] = vol[1];
166 return 0;
169 static int snd_pmac_awacs_put_volume(struct snd_kcontrol *kcontrol,
170 struct snd_ctl_elem_value *ucontrol)
172 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
173 int reg = kcontrol->private_value & 0xff;
174 int lshift = (kcontrol->private_value >> 8) & 0xff;
175 int inverted = (kcontrol->private_value >> 16) & 1;
176 int val, oldval;
177 unsigned long flags;
178 int vol[2];
180 vol[0] = ucontrol->value.integer.value[0];
181 vol[1] = ucontrol->value.integer.value[1];
182 if (inverted) {
183 vol[0] = 0x0f - vol[0];
184 vol[1] = 0x0f - vol[1];
186 vol[0] &= 0x0f;
187 vol[1] &= 0x0f;
188 spin_lock_irqsave(&chip->reg_lock, flags);
189 oldval = chip->awacs_reg[reg];
190 val = oldval & ~(0xf | (0xf << lshift));
191 val |= vol[0] << lshift;
192 val |= vol[1];
193 if (oldval != val)
194 snd_pmac_awacs_write_reg(chip, reg, val);
195 spin_unlock_irqrestore(&chip->reg_lock, flags);
196 return oldval != reg;
200 #define AWACS_VOLUME(xname, xreg, xshift, xinverted) \
201 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = 0, \
202 .info = snd_pmac_awacs_info_volume, \
203 .get = snd_pmac_awacs_get_volume, \
204 .put = snd_pmac_awacs_put_volume, \
205 .private_value = (xreg) | ((xshift) << 8) | ((xinverted) << 16) }
208 * mute master/ogain for AWACS: mono
210 static int snd_pmac_awacs_get_switch(struct snd_kcontrol *kcontrol,
211 struct snd_ctl_elem_value *ucontrol)
213 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
214 int reg = kcontrol->private_value & 0xff;
215 int shift = (kcontrol->private_value >> 8) & 0xff;
216 int invert = (kcontrol->private_value >> 16) & 1;
217 int val;
218 unsigned long flags;
220 spin_lock_irqsave(&chip->reg_lock, flags);
221 val = (chip->awacs_reg[reg] >> shift) & 1;
222 spin_unlock_irqrestore(&chip->reg_lock, flags);
223 if (invert)
224 val = 1 - val;
225 ucontrol->value.integer.value[0] = val;
226 return 0;
229 static int snd_pmac_awacs_put_switch(struct snd_kcontrol *kcontrol,
230 struct snd_ctl_elem_value *ucontrol)
232 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
233 int reg = kcontrol->private_value & 0xff;
234 int shift = (kcontrol->private_value >> 8) & 0xff;
235 int invert = (kcontrol->private_value >> 16) & 1;
236 int mask = 1 << shift;
237 int val, changed;
238 unsigned long flags;
240 spin_lock_irqsave(&chip->reg_lock, flags);
241 val = chip->awacs_reg[reg] & ~mask;
242 if (ucontrol->value.integer.value[0] != invert)
243 val |= mask;
244 changed = chip->awacs_reg[reg] != val;
245 if (changed)
246 snd_pmac_awacs_write_reg(chip, reg, val);
247 spin_unlock_irqrestore(&chip->reg_lock, flags);
248 return changed;
251 #define AWACS_SWITCH(xname, xreg, xshift, xinvert) \
252 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = 0, \
253 .info = snd_pmac_boolean_mono_info, \
254 .get = snd_pmac_awacs_get_switch, \
255 .put = snd_pmac_awacs_put_switch, \
256 .private_value = (xreg) | ((xshift) << 8) | ((xinvert) << 16) }
259 #ifdef PMAC_AMP_AVAIL
261 * controls for perch/whisper extension cards, e.g. G3 desktop
263 * TDA7433 connected via i2c address 0x45 (= 0x8a),
264 * accessed through cuda
266 static void awacs_set_cuda(int reg, int val)
268 struct adb_request req;
269 cuda_request(&req, NULL, 5, CUDA_PACKET, CUDA_GET_SET_IIC, 0x8a, reg, val);
270 while (! req.complete)
271 cuda_poll();
275 * level = 0 - 14, 7 = 0 dB
277 static void awacs_amp_set_tone(struct awacs_amp *amp, int bass, int treble)
279 amp->amp_tone[0] = bass;
280 amp->amp_tone[1] = treble;
281 if (bass > 7)
282 bass = (14 - bass) + 8;
283 if (treble > 7)
284 treble = (14 - treble) + 8;
285 awacs_set_cuda(2, (bass << 4) | treble);
289 * vol = 0 - 31 (attenuation), 32 = mute bit, stereo
291 static int awacs_amp_set_vol(struct awacs_amp *amp, int index, int lvol, int rvol,
292 int do_check)
294 if (do_check && amp->amp_vol[index][0] == lvol &&
295 amp->amp_vol[index][1] == rvol)
296 return 0;
297 awacs_set_cuda(3 + index, lvol);
298 awacs_set_cuda(5 + index, rvol);
299 amp->amp_vol[index][0] = lvol;
300 amp->amp_vol[index][1] = rvol;
301 return 1;
305 * 0 = -79 dB, 79 = 0 dB, 99 = +20 dB
307 static void awacs_amp_set_master(struct awacs_amp *amp, int vol)
309 amp->amp_master = vol;
310 if (vol <= 79)
311 vol = 32 + (79 - vol);
312 else
313 vol = 32 - (vol - 79);
314 awacs_set_cuda(1, vol);
317 static void awacs_amp_free(struct snd_pmac *chip)
319 struct awacs_amp *amp = chip->mixer_data;
320 snd_assert(amp, return);
321 kfree(amp);
322 chip->mixer_data = NULL;
323 chip->mixer_free = NULL;
328 * mixer controls
330 static int snd_pmac_awacs_info_volume_amp(struct snd_kcontrol *kcontrol,
331 struct snd_ctl_elem_info *uinfo)
333 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
334 uinfo->count = 2;
335 uinfo->value.integer.min = 0;
336 uinfo->value.integer.max = 31;
337 return 0;
340 static int snd_pmac_awacs_get_volume_amp(struct snd_kcontrol *kcontrol,
341 struct snd_ctl_elem_value *ucontrol)
343 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
344 int index = kcontrol->private_value;
345 struct awacs_amp *amp = chip->mixer_data;
346 snd_assert(amp, return -EINVAL);
347 snd_assert(index >= 0 && index <= 1, return -EINVAL);
348 ucontrol->value.integer.value[0] = 31 - (amp->amp_vol[index][0] & 31);
349 ucontrol->value.integer.value[1] = 31 - (amp->amp_vol[index][1] & 31);
350 return 0;
353 static int snd_pmac_awacs_put_volume_amp(struct snd_kcontrol *kcontrol,
354 struct snd_ctl_elem_value *ucontrol)
356 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
357 int index = kcontrol->private_value;
358 int vol[2];
359 struct awacs_amp *amp = chip->mixer_data;
360 snd_assert(amp, return -EINVAL);
361 snd_assert(index >= 0 && index <= 1, return -EINVAL);
363 vol[0] = (31 - (ucontrol->value.integer.value[0] & 31)) | (amp->amp_vol[index][0] & 32);
364 vol[1] = (31 - (ucontrol->value.integer.value[1] & 31)) | (amp->amp_vol[index][1] & 32);
365 return awacs_amp_set_vol(amp, index, vol[0], vol[1], 1);
368 static int snd_pmac_awacs_get_switch_amp(struct snd_kcontrol *kcontrol,
369 struct snd_ctl_elem_value *ucontrol)
371 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
372 int index = kcontrol->private_value;
373 struct awacs_amp *amp = chip->mixer_data;
374 snd_assert(amp, return -EINVAL);
375 snd_assert(index >= 0 && index <= 1, return -EINVAL);
376 ucontrol->value.integer.value[0] = (amp->amp_vol[index][0] & 32) ? 0 : 1;
377 ucontrol->value.integer.value[1] = (amp->amp_vol[index][1] & 32) ? 0 : 1;
378 return 0;
381 static int snd_pmac_awacs_put_switch_amp(struct snd_kcontrol *kcontrol,
382 struct snd_ctl_elem_value *ucontrol)
384 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
385 int index = kcontrol->private_value;
386 int vol[2];
387 struct awacs_amp *amp = chip->mixer_data;
388 snd_assert(amp, return -EINVAL);
389 snd_assert(index >= 0 && index <= 1, return -EINVAL);
391 vol[0] = (ucontrol->value.integer.value[0] ? 0 : 32) | (amp->amp_vol[index][0] & 31);
392 vol[1] = (ucontrol->value.integer.value[1] ? 0 : 32) | (amp->amp_vol[index][1] & 31);
393 return awacs_amp_set_vol(amp, index, vol[0], vol[1], 1);
396 static int snd_pmac_awacs_info_tone_amp(struct snd_kcontrol *kcontrol,
397 struct snd_ctl_elem_info *uinfo)
399 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
400 uinfo->count = 1;
401 uinfo->value.integer.min = 0;
402 uinfo->value.integer.max = 14;
403 return 0;
406 static int snd_pmac_awacs_get_tone_amp(struct snd_kcontrol *kcontrol,
407 struct snd_ctl_elem_value *ucontrol)
409 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
410 int index = kcontrol->private_value;
411 struct awacs_amp *amp = chip->mixer_data;
412 snd_assert(amp, return -EINVAL);
413 snd_assert(index >= 0 && index <= 1, return -EINVAL);
414 ucontrol->value.integer.value[0] = amp->amp_tone[index];
415 return 0;
418 static int snd_pmac_awacs_put_tone_amp(struct snd_kcontrol *kcontrol,
419 struct snd_ctl_elem_value *ucontrol)
421 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
422 int index = kcontrol->private_value;
423 struct awacs_amp *amp = chip->mixer_data;
424 snd_assert(amp, return -EINVAL);
425 snd_assert(index >= 0 && index <= 1, return -EINVAL);
426 if (ucontrol->value.integer.value[0] != amp->amp_tone[index]) {
427 amp->amp_tone[index] = ucontrol->value.integer.value[0];
428 awacs_amp_set_tone(amp, amp->amp_tone[0], amp->amp_tone[1]);
429 return 1;
431 return 0;
434 static int snd_pmac_awacs_info_master_amp(struct snd_kcontrol *kcontrol,
435 struct snd_ctl_elem_info *uinfo)
437 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
438 uinfo->count = 1;
439 uinfo->value.integer.min = 0;
440 uinfo->value.integer.max = 99;
441 return 0;
444 static int snd_pmac_awacs_get_master_amp(struct snd_kcontrol *kcontrol,
445 struct snd_ctl_elem_value *ucontrol)
447 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
448 struct awacs_amp *amp = chip->mixer_data;
449 snd_assert(amp, return -EINVAL);
450 ucontrol->value.integer.value[0] = amp->amp_master;
451 return 0;
454 static int snd_pmac_awacs_put_master_amp(struct snd_kcontrol *kcontrol,
455 struct snd_ctl_elem_value *ucontrol)
457 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
458 struct awacs_amp *amp = chip->mixer_data;
459 snd_assert(amp, return -EINVAL);
460 if (ucontrol->value.integer.value[0] != amp->amp_master) {
461 amp->amp_master = ucontrol->value.integer.value[0];
462 awacs_amp_set_master(amp, amp->amp_master);
463 return 1;
465 return 0;
468 #define AMP_CH_SPK 0
469 #define AMP_CH_HD 1
471 static struct snd_kcontrol_new snd_pmac_awacs_amp_vol[] __initdata = {
472 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
473 .name = "PC Speaker Playback Volume",
474 .info = snd_pmac_awacs_info_volume_amp,
475 .get = snd_pmac_awacs_get_volume_amp,
476 .put = snd_pmac_awacs_put_volume_amp,
477 .private_value = AMP_CH_SPK,
479 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
480 .name = "Headphone Playback Volume",
481 .info = snd_pmac_awacs_info_volume_amp,
482 .get = snd_pmac_awacs_get_volume_amp,
483 .put = snd_pmac_awacs_put_volume_amp,
484 .private_value = AMP_CH_HD,
486 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
487 .name = "Tone Control - Bass",
488 .info = snd_pmac_awacs_info_tone_amp,
489 .get = snd_pmac_awacs_get_tone_amp,
490 .put = snd_pmac_awacs_put_tone_amp,
491 .private_value = 0,
493 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
494 .name = "Tone Control - Treble",
495 .info = snd_pmac_awacs_info_tone_amp,
496 .get = snd_pmac_awacs_get_tone_amp,
497 .put = snd_pmac_awacs_put_tone_amp,
498 .private_value = 1,
500 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
501 .name = "Amp Master Playback Volume",
502 .info = snd_pmac_awacs_info_master_amp,
503 .get = snd_pmac_awacs_get_master_amp,
504 .put = snd_pmac_awacs_put_master_amp,
508 static struct snd_kcontrol_new snd_pmac_awacs_amp_hp_sw __initdata = {
509 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
510 .name = "Headphone Playback Switch",
511 .info = snd_pmac_boolean_stereo_info,
512 .get = snd_pmac_awacs_get_switch_amp,
513 .put = snd_pmac_awacs_put_switch_amp,
514 .private_value = AMP_CH_HD,
517 static struct snd_kcontrol_new snd_pmac_awacs_amp_spk_sw __initdata = {
518 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
519 .name = "PC Speaker Playback Switch",
520 .info = snd_pmac_boolean_stereo_info,
521 .get = snd_pmac_awacs_get_switch_amp,
522 .put = snd_pmac_awacs_put_switch_amp,
523 .private_value = AMP_CH_SPK,
526 #endif /* PMAC_AMP_AVAIL */
530 * mic boost for screamer
532 static int snd_pmac_screamer_mic_boost_info(struct snd_kcontrol *kcontrol,
533 struct snd_ctl_elem_info *uinfo)
535 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
536 uinfo->count = 1;
537 uinfo->value.integer.min = 0;
538 uinfo->value.integer.max = 2;
539 return 0;
542 static int snd_pmac_screamer_mic_boost_get(struct snd_kcontrol *kcontrol,
543 struct snd_ctl_elem_value *ucontrol)
545 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
546 int val;
547 unsigned long flags;
549 spin_lock_irqsave(&chip->reg_lock, flags);
550 if (chip->awacs_reg[6] & MASK_MIC_BOOST)
551 val = 2;
552 else if (chip->awacs_reg[0] & MASK_GAINLINE)
553 val = 1;
554 else
555 val = 0;
556 spin_unlock_irqrestore(&chip->reg_lock, flags);
557 ucontrol->value.integer.value[0] = val;
558 return 0;
561 static int snd_pmac_screamer_mic_boost_put(struct snd_kcontrol *kcontrol,
562 struct snd_ctl_elem_value *ucontrol)
564 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
565 int changed = 0;
566 int val0, val6;
567 unsigned long flags;
569 spin_lock_irqsave(&chip->reg_lock, flags);
570 val0 = chip->awacs_reg[0] & ~MASK_GAINLINE;
571 val6 = chip->awacs_reg[6] & ~MASK_MIC_BOOST;
572 if (ucontrol->value.integer.value[0] > 0) {
573 val0 |= MASK_GAINLINE;
574 if (ucontrol->value.integer.value[0] > 1)
575 val6 |= MASK_MIC_BOOST;
577 if (val0 != chip->awacs_reg[0]) {
578 snd_pmac_awacs_write_reg(chip, 0, val0);
579 changed = 1;
581 if (val6 != chip->awacs_reg[6]) {
582 snd_pmac_awacs_write_reg(chip, 6, val6);
583 changed = 1;
585 spin_unlock_irqrestore(&chip->reg_lock, flags);
586 return changed;
590 * lists of mixer elements
592 static struct snd_kcontrol_new snd_pmac_awacs_mixers[] __initdata = {
593 AWACS_VOLUME("Master Playback Volume", 2, 6, 1),
594 AWACS_SWITCH("Master Capture Switch", 1, SHIFT_LOOPTHRU, 0),
595 AWACS_VOLUME("Capture Volume", 0, 4, 0),
596 AWACS_SWITCH("CD Capture Switch", 0, SHIFT_MUX_CD, 0),
599 /* FIXME: is this correct order?
600 * screamer (powerbook G3 pismo) seems to have different bits...
602 static struct snd_kcontrol_new snd_pmac_awacs_mixers2[] __initdata = {
603 AWACS_SWITCH("Line Capture Switch", 0, SHIFT_MUX_LINE, 0),
604 AWACS_SWITCH("Mic Capture Switch", 0, SHIFT_MUX_MIC, 0),
607 static struct snd_kcontrol_new snd_pmac_screamer_mixers2[] __initdata = {
608 AWACS_SWITCH("Line Capture Switch", 0, SHIFT_MUX_MIC, 0),
609 AWACS_SWITCH("Mic Capture Switch", 0, SHIFT_MUX_LINE, 0),
612 static struct snd_kcontrol_new snd_pmac_awacs_master_sw __initdata =
613 AWACS_SWITCH("Master Playback Switch", 1, SHIFT_HDMUTE, 1);
615 static struct snd_kcontrol_new snd_pmac_awacs_mic_boost[] __initdata = {
616 AWACS_SWITCH("Mic Boost", 0, SHIFT_GAINLINE, 0),
619 static struct snd_kcontrol_new snd_pmac_screamer_mic_boost[] __initdata = {
620 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
621 .name = "Mic Boost",
622 .info = snd_pmac_screamer_mic_boost_info,
623 .get = snd_pmac_screamer_mic_boost_get,
624 .put = snd_pmac_screamer_mic_boost_put,
628 static struct snd_kcontrol_new snd_pmac_awacs_speaker_vol[] __initdata = {
629 AWACS_VOLUME("PC Speaker Playback Volume", 4, 6, 1),
631 static struct snd_kcontrol_new snd_pmac_awacs_speaker_sw __initdata =
632 AWACS_SWITCH("PC Speaker Playback Switch", 1, SHIFT_SPKMUTE, 1);
636 * add new mixer elements to the card
638 static int build_mixers(struct snd_pmac *chip, int nums, struct snd_kcontrol_new *mixers)
640 int i, err;
642 for (i = 0; i < nums; i++) {
643 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&mixers[i], chip))) < 0)
644 return err;
646 return 0;
651 * restore all registers
653 static void awacs_restore_all_regs(struct snd_pmac *chip)
655 snd_pmac_awacs_write_noreg(chip, 0, chip->awacs_reg[0]);
656 snd_pmac_awacs_write_noreg(chip, 1, chip->awacs_reg[1]);
657 snd_pmac_awacs_write_noreg(chip, 2, chip->awacs_reg[2]);
658 snd_pmac_awacs_write_noreg(chip, 4, chip->awacs_reg[4]);
659 if (chip->model == PMAC_SCREAMER) {
660 snd_pmac_awacs_write_noreg(chip, 5, chip->awacs_reg[5]);
661 snd_pmac_awacs_write_noreg(chip, 6, chip->awacs_reg[6]);
662 snd_pmac_awacs_write_noreg(chip, 7, chip->awacs_reg[7]);
666 #ifdef CONFIG_PM
667 static void snd_pmac_awacs_suspend(struct snd_pmac *chip)
669 snd_pmac_awacs_write_noreg(chip, 1, (chip->awacs_reg[1]
670 | MASK_AMUTE | MASK_CMUTE));
673 static void snd_pmac_awacs_resume(struct snd_pmac *chip)
675 if (machine_is_compatible("PowerBook3,1")
676 || machine_is_compatible("PowerBook3,2")) {
677 msleep(100);
678 snd_pmac_awacs_write_reg(chip, 1,
679 chip->awacs_reg[1] & ~MASK_PAROUT);
680 msleep(300);
683 awacs_restore_all_regs(chip);
684 if (chip->model == PMAC_SCREAMER) {
685 /* reset power bits in reg 6 */
686 mdelay(5);
687 snd_pmac_awacs_write_noreg(chip, 6, chip->awacs_reg[6]);
689 screamer_recalibrate(chip);
690 #ifdef PMAC_AMP_AVAIL
691 if (chip->mixer_data) {
692 struct awacs_amp *amp = chip->mixer_data;
693 awacs_amp_set_vol(amp, 0, amp->amp_vol[0][0], amp->amp_vol[0][1], 0);
694 awacs_amp_set_vol(amp, 1, amp->amp_vol[1][0], amp->amp_vol[1][1], 0);
695 awacs_amp_set_tone(amp, amp->amp_tone[0], amp->amp_tone[1]);
696 awacs_amp_set_master(amp, amp->amp_master);
698 #endif
700 #endif /* CONFIG_PM */
702 #ifdef PMAC_SUPPORT_AUTOMUTE
704 * auto-mute stuffs
706 static int snd_pmac_awacs_detect_headphone(struct snd_pmac *chip)
708 return (in_le32(&chip->awacs->codec_stat) & chip->hp_stat_mask) ? 1 : 0;
711 #ifdef PMAC_AMP_AVAIL
712 static int toggle_amp_mute(struct awacs_amp *amp, int index, int mute)
714 int vol[2];
715 vol[0] = amp->amp_vol[index][0] & 31;
716 vol[1] = amp->amp_vol[index][1] & 31;
717 if (mute) {
718 vol[0] |= 32;
719 vol[1] |= 32;
721 return awacs_amp_set_vol(amp, index, vol[0], vol[1], 1);
723 #endif
725 static void snd_pmac_awacs_update_automute(struct snd_pmac *chip, int do_notify)
727 if (chip->auto_mute) {
728 #ifdef PMAC_AMP_AVAIL
729 if (chip->mixer_data) {
730 struct awacs_amp *amp = chip->mixer_data;
731 int changed;
732 if (snd_pmac_awacs_detect_headphone(chip)) {
733 changed = toggle_amp_mute(amp, AMP_CH_HD, 0);
734 changed |= toggle_amp_mute(amp, AMP_CH_SPK, 1);
735 } else {
736 changed = toggle_amp_mute(amp, AMP_CH_HD, 1);
737 changed |= toggle_amp_mute(amp, AMP_CH_SPK, 0);
739 if (do_notify && ! changed)
740 return;
741 } else
742 #endif
744 int reg = chip->awacs_reg[1] | (MASK_HDMUTE|MASK_SPKMUTE);
745 if (snd_pmac_awacs_detect_headphone(chip))
746 reg &= ~MASK_HDMUTE;
747 else
748 reg &= ~MASK_SPKMUTE;
749 if (do_notify && reg == chip->awacs_reg[1])
750 return;
751 snd_pmac_awacs_write_reg(chip, 1, reg);
753 if (do_notify) {
754 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
755 &chip->master_sw_ctl->id);
756 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
757 &chip->speaker_sw_ctl->id);
758 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
759 &chip->hp_detect_ctl->id);
763 #endif /* PMAC_SUPPORT_AUTOMUTE */
767 * initialize chip
769 int __init
770 snd_pmac_awacs_init(struct snd_pmac *chip)
772 int err, vol;
774 /* looks like MASK_GAINLINE triggers something, so we set here
775 * as start-up
777 chip->awacs_reg[0] = MASK_MUX_CD | 0xff | MASK_GAINLINE;
778 chip->awacs_reg[1] = MASK_CMUTE | MASK_AMUTE;
779 /* FIXME: Only machines with external SRS module need MASK_PAROUT */
780 if (chip->has_iic || chip->device_id == 0x5 ||
781 /*chip->_device_id == 0x8 || */
782 chip->device_id == 0xb)
783 chip->awacs_reg[1] |= MASK_PAROUT;
784 /* get default volume from nvram */
785 // vol = (~nvram_read_byte(0x1308) & 7) << 1;
786 // vol = ((pmac_xpram_read( 8 ) & 7 ) << 1 );
787 vol = 0x0f; /* no, on alsa, muted as default */
788 vol = vol + (vol << 6);
789 chip->awacs_reg[2] = vol;
790 chip->awacs_reg[4] = vol;
791 if (chip->model == PMAC_SCREAMER) {
792 chip->awacs_reg[5] = vol; /* FIXME: screamer has loopthru vol control */
793 chip->awacs_reg[6] = MASK_MIC_BOOST; /* FIXME: maybe should be vol << 3 for PCMCIA speaker */
794 chip->awacs_reg[7] = 0;
797 awacs_restore_all_regs(chip);
798 chip->manufacturer = (in_le32(&chip->awacs->codec_stat) >> 8) & 0xf;
799 screamer_recalibrate(chip);
801 chip->revision = (in_le32(&chip->awacs->codec_stat) >> 12) & 0xf;
802 #ifdef PMAC_AMP_AVAIL
803 if (chip->revision == 3 && chip->has_iic && CHECK_CUDA_AMP()) {
804 struct awacs_amp *amp = kmalloc(sizeof(*amp), GFP_KERNEL);
805 if (! amp)
806 return -ENOMEM;
807 chip->mixer_data = amp;
808 memset(amp, 0, sizeof(*amp));
809 chip->mixer_free = awacs_amp_free;
810 awacs_amp_set_vol(amp, 0, 63, 63, 0); /* mute and zero vol */
811 awacs_amp_set_vol(amp, 1, 63, 63, 0);
812 awacs_amp_set_tone(amp, 7, 7); /* 0 dB */
813 awacs_amp_set_master(amp, 79); /* 0 dB */
815 #endif /* PMAC_AMP_AVAIL */
817 if (chip->hp_stat_mask == 0) {
818 /* set headphone-jack detection bit */
819 switch (chip->model) {
820 case PMAC_AWACS:
821 chip->hp_stat_mask = 0x04;
822 break;
823 case PMAC_SCREAMER:
824 switch (chip->device_id) {
825 case 0x08:
826 /* 1 = side jack, 2 = front jack */
827 chip->hp_stat_mask = 0x03;
828 break;
829 case 0x00:
830 case 0x05:
831 chip->hp_stat_mask = 0x04;
832 break;
833 default:
834 chip->hp_stat_mask = 0x08;
835 break;
837 break;
838 default:
839 snd_BUG();
840 break;
845 * build mixers
847 strcpy(chip->card->mixername, "PowerMac AWACS");
849 if ((err = build_mixers(chip, ARRAY_SIZE(snd_pmac_awacs_mixers),
850 snd_pmac_awacs_mixers)) < 0)
851 return err;
852 if (chip->model == PMAC_SCREAMER)
853 err = build_mixers(chip, ARRAY_SIZE(snd_pmac_screamer_mixers2),
854 snd_pmac_screamer_mixers2);
855 else
856 err = build_mixers(chip, ARRAY_SIZE(snd_pmac_awacs_mixers2),
857 snd_pmac_awacs_mixers2);
858 if (err < 0)
859 return err;
860 chip->master_sw_ctl = snd_ctl_new1(&snd_pmac_awacs_master_sw, chip);
861 if ((err = snd_ctl_add(chip->card, chip->master_sw_ctl)) < 0)
862 return err;
863 #ifdef PMAC_AMP_AVAIL
864 if (chip->mixer_data) {
865 /* use amplifier. the signal is connected from route A
866 * to the amp. the amp has its headphone and speaker
867 * volumes and mute switches, so we use them instead of
868 * screamer registers.
869 * in this case, it seems the route C is not used.
871 if ((err = build_mixers(chip, ARRAY_SIZE(snd_pmac_awacs_amp_vol),
872 snd_pmac_awacs_amp_vol)) < 0)
873 return err;
874 /* overwrite */
875 chip->master_sw_ctl = snd_ctl_new1(&snd_pmac_awacs_amp_hp_sw, chip);
876 if ((err = snd_ctl_add(chip->card, chip->master_sw_ctl)) < 0)
877 return err;
878 chip->speaker_sw_ctl = snd_ctl_new1(&snd_pmac_awacs_amp_spk_sw, chip);
879 if ((err = snd_ctl_add(chip->card, chip->speaker_sw_ctl)) < 0)
880 return err;
881 } else
882 #endif /* PMAC_AMP_AVAIL */
884 /* route A = headphone, route C = speaker */
885 if ((err = build_mixers(chip, ARRAY_SIZE(snd_pmac_awacs_speaker_vol),
886 snd_pmac_awacs_speaker_vol)) < 0)
887 return err;
888 chip->speaker_sw_ctl = snd_ctl_new1(&snd_pmac_awacs_speaker_sw, chip);
889 if ((err = snd_ctl_add(chip->card, chip->speaker_sw_ctl)) < 0)
890 return err;
893 if (chip->model == PMAC_SCREAMER) {
894 if ((err = build_mixers(chip, ARRAY_SIZE(snd_pmac_screamer_mic_boost),
895 snd_pmac_screamer_mic_boost)) < 0)
896 return err;
897 } else {
898 if ((err = build_mixers(chip, ARRAY_SIZE(snd_pmac_awacs_mic_boost),
899 snd_pmac_awacs_mic_boost)) < 0)
900 return err;
904 * set lowlevel callbacks
906 chip->set_format = snd_pmac_awacs_set_format;
907 #ifdef CONFIG_PM
908 chip->suspend = snd_pmac_awacs_suspend;
909 chip->resume = snd_pmac_awacs_resume;
910 #endif
911 #ifdef PMAC_SUPPORT_AUTOMUTE
912 if ((err = snd_pmac_add_automute(chip)) < 0)
913 return err;
914 chip->detect_headphone = snd_pmac_awacs_detect_headphone;
915 chip->update_automute = snd_pmac_awacs_update_automute;
916 snd_pmac_awacs_update_automute(chip, 0); /* update the status only */
917 #endif
918 if (chip->model == PMAC_SCREAMER) {
919 snd_pmac_awacs_write_noreg(chip, 6, chip->awacs_reg[6]);
920 snd_pmac_awacs_write_noreg(chip, 0, chip->awacs_reg[0]);
923 return 0;