Get rid of arch/mips64/kernel. 9116 lines of code gone.
[linux-2.6/linux-mips.git] / sound / ppc / awacs.c
blob1a4ed40a06917452a3362903b38651eaf1deedc8
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"
32 #define chip_t pmac_t
35 #ifdef CONFIG_ADB_CUDA
36 #define PMAC_AMP_AVAIL
37 #endif
39 #ifdef PMAC_AMP_AVAIL
40 typedef struct awacs_amp {
41 unsigned char amp_master;
42 unsigned char amp_vol[2][2];
43 unsigned char amp_tone[2];
44 } awacs_amp_t;
46 #define CHECK_CUDA_AMP() (sys_ctrler == SYS_CTRLER_CUDA)
48 #endif /* PMAC_AMP_AVAIL */
51 static void snd_pmac_screamer_wait(pmac_t *chip)
53 long timeout = 2000;
54 while (!(in_le32(&chip->awacs->codec_stat) & MASK_VALID)) {
55 mdelay(1);
56 if (! --timeout) {
57 snd_printd("snd_pmac_screamer_wait timeout\n");
58 break;
64 * write AWACS register
66 static void
67 snd_pmac_awacs_write(pmac_t *chip, int val)
69 long timeout = 5000000;
71 if (chip->model == PMAC_SCREAMER)
72 snd_pmac_screamer_wait(chip);
73 out_le32(&chip->awacs->codec_ctrl, val | (chip->subframe << 22));
74 while (in_le32(&chip->awacs->codec_ctrl) & MASK_NEWECMD) {
75 if (! --timeout) {
76 snd_printd("snd_pmac_awacs_write timeout\n");
77 break;
82 static void
83 snd_pmac_awacs_write_reg(pmac_t *chip, int reg, int val)
85 snd_pmac_awacs_write(chip, val | (reg << 12));
86 chip->awacs_reg[reg] = val;
89 static void
90 snd_pmac_awacs_write_noreg(pmac_t *chip, int reg, int val)
92 snd_pmac_awacs_write(chip, val | (reg << 12));
95 static void do_mdelay(int msec, int can_schedule)
97 if (can_schedule) {
98 set_current_state(TASK_UNINTERRUPTIBLE);
99 schedule_timeout((msec * HZ + 999) / 1000);
100 } else
101 mdelay(msec);
104 #ifdef CONFIG_PMAC_PBOOK
105 /* Recalibrate chip */
106 static void screamer_recalibrate(pmac_t *chip, int can_schedule)
108 if (chip->model != PMAC_SCREAMER)
109 return;
111 /* Sorry for the horrible delays... I hope to get that improved
112 * by making the whole PM process asynchronous in a future version
114 snd_pmac_awacs_write_noreg(chip, 1, chip->awacs_reg[1]);
115 if (chip->manufacturer == 0x1)
116 /* delay for broken crystal part */
117 do_mdelay(750, can_schedule);
118 snd_pmac_awacs_write_noreg(chip, 1,
119 chip->awacs_reg[1] | MASK_RECALIBRATE | MASK_CMUTE | MASK_AMUTE);
120 snd_pmac_awacs_write_noreg(chip, 1, chip->awacs_reg[1]);
121 snd_pmac_awacs_write_noreg(chip, 6, chip->awacs_reg[6]);
124 #else
125 #define screamer_recalibrate(chip, can_schedule) /* NOP */
126 #endif
130 * additional callback to set the pcm format
132 static void snd_pmac_awacs_set_format(pmac_t *chip)
134 chip->awacs_reg[1] &= ~MASK_SAMPLERATE;
135 chip->awacs_reg[1] |= chip->rate_index << 3;
136 snd_pmac_awacs_write_reg(chip, 1, chip->awacs_reg[1]);
141 * AWACS volume callbacks
144 * volumes: 0-15 stereo
146 static int snd_pmac_awacs_info_volume(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
148 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
149 uinfo->count = 2;
150 uinfo->value.integer.min = 0;
151 uinfo->value.integer.max = 15;
152 return 0;
155 static int snd_pmac_awacs_get_volume(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
157 pmac_t *chip = snd_kcontrol_chip(kcontrol);
158 int reg = kcontrol->private_value & 0xff;
159 int lshift = (kcontrol->private_value >> 8) & 0xff;
160 int inverted = (kcontrol->private_value >> 16) & 1;
161 unsigned long flags;
162 int vol[2];
164 spin_lock_irqsave(&chip->reg_lock, flags);
165 vol[0] = (chip->awacs_reg[reg] >> lshift) & 0xf;
166 vol[1] = chip->awacs_reg[reg] & 0xf;
167 spin_unlock_irqrestore(&chip->reg_lock, flags);
168 if (inverted) {
169 vol[0] = 0x0f - vol[0];
170 vol[1] = 0x0f - vol[1];
172 ucontrol->value.integer.value[0] = vol[0];
173 ucontrol->value.integer.value[1] = vol[1];
174 return 0;
177 static int snd_pmac_awacs_put_volume(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
179 pmac_t *chip = snd_kcontrol_chip(kcontrol);
180 int reg = kcontrol->private_value & 0xff;
181 int lshift = (kcontrol->private_value >> 8) & 0xff;
182 int inverted = (kcontrol->private_value >> 16) & 1;
183 int val, oldval;
184 unsigned long flags;
185 int vol[2];
187 vol[0] = ucontrol->value.integer.value[0];
188 vol[1] = ucontrol->value.integer.value[1];
189 if (inverted) {
190 vol[0] = 0x0f - vol[0];
191 vol[1] = 0x0f - vol[1];
193 vol[0] &= 0x0f;
194 vol[1] &= 0x0f;
195 spin_lock_irqsave(&chip->reg_lock, flags);
196 oldval = chip->awacs_reg[reg];
197 val = oldval & ~(0xf | (0xf << lshift));
198 val |= vol[0] << lshift;
199 val |= vol[1];
200 if (oldval != val)
201 snd_pmac_awacs_write_reg(chip, reg, val);
202 spin_unlock_irqrestore(&chip->reg_lock, flags);
203 return oldval != reg;
207 #define AWACS_VOLUME(xname, xreg, xshift, xinverted) \
208 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = 0, \
209 .info = snd_pmac_awacs_info_volume, \
210 .get = snd_pmac_awacs_get_volume, \
211 .put = snd_pmac_awacs_put_volume, \
212 .private_value = (xreg) | ((xshift) << 8) | ((xinverted) << 16) }
215 * mute master/ogain for AWACS: mono
217 static int snd_pmac_awacs_get_switch(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
219 pmac_t *chip = snd_kcontrol_chip(kcontrol);
220 int reg = kcontrol->private_value & 0xff;
221 int shift = (kcontrol->private_value >> 8) & 0xff;
222 int invert = (kcontrol->private_value >> 16) & 1;
223 int val;
224 unsigned long flags;
226 spin_lock_irqsave(&chip->reg_lock, flags);
227 val = (chip->awacs_reg[reg] >> shift) & 1;
228 spin_unlock_irqrestore(&chip->reg_lock, flags);
229 if (invert)
230 val = 1 - val;
231 ucontrol->value.integer.value[0] = val;
232 return 0;
235 static int snd_pmac_awacs_put_switch(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
237 pmac_t *chip = snd_kcontrol_chip(kcontrol);
238 int reg = kcontrol->private_value & 0xff;
239 int shift = (kcontrol->private_value >> 8) & 0xff;
240 int invert = (kcontrol->private_value >> 16) & 1;
241 int mask = 1 << shift;
242 int val, changed;
243 unsigned long flags;
245 spin_lock_irqsave(&chip->reg_lock, flags);
246 val = chip->awacs_reg[reg] & ~mask;
247 if (ucontrol->value.integer.value[0] != invert)
248 val |= mask;
249 changed = chip->awacs_reg[reg] != val;
250 if (changed)
251 snd_pmac_awacs_write_reg(chip, reg, val);
252 spin_unlock_irqrestore(&chip->reg_lock, flags);
253 return changed;
256 #define AWACS_SWITCH(xname, xreg, xshift, xinvert) \
257 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = 0, \
258 .info = snd_pmac_boolean_mono_info, \
259 .get = snd_pmac_awacs_get_switch, \
260 .put = snd_pmac_awacs_put_switch, \
261 .private_value = (xreg) | ((xshift) << 8) | ((xinvert) << 16) }
264 #ifdef PMAC_AMP_AVAIL
266 * controls for perch/whisper extension cards, e.g. G3 desktop
268 * TDA7433 connected via i2c address 0x45 (= 0x8a),
269 * accessed through cuda
271 static void awacs_set_cuda(int reg, int val)
273 struct adb_request req;
274 cuda_request(&req, NULL, 5, CUDA_PACKET, CUDA_GET_SET_IIC, 0x8a, reg, val);
275 while (! req.complete)
276 cuda_poll();
280 * level = 0 - 14, 7 = 0 dB
282 static void awacs_amp_set_tone(awacs_amp_t *amp, int bass, int treble)
284 amp->amp_tone[0] = bass;
285 amp->amp_tone[1] = treble;
286 if (bass > 7)
287 bass = (14 - bass) + 8;
288 if (treble > 7)
289 treble = (14 - treble) + 8;
290 awacs_set_cuda(2, (bass << 4) | treble);
294 * vol = 0 - 31 (attenuation), 32 = mute bit, stereo
296 static int awacs_amp_set_vol(awacs_amp_t *amp, int index, int lvol, int rvol, int do_check)
298 if (do_check && amp->amp_vol[index][0] == lvol &&
299 amp->amp_vol[index][1] == rvol)
300 return 0;
301 awacs_set_cuda(3 + index, lvol);
302 awacs_set_cuda(5 + index, rvol);
303 amp->amp_vol[index][0] = lvol;
304 amp->amp_vol[index][1] = rvol;
305 return 1;
309 * 0 = -79 dB, 79 = 0 dB, 99 = +20 dB
311 static void awacs_amp_set_master(awacs_amp_t *amp, int vol)
313 amp->amp_master = vol;
314 if (vol <= 79)
315 vol = 32 + (79 - vol);
316 else
317 vol = 32 - (vol - 79);
318 awacs_set_cuda(1, vol);
321 static void awacs_amp_free(pmac_t *chip)
323 awacs_amp_t *amp = chip->mixer_data;
324 snd_assert(amp, return);
325 kfree(amp);
326 chip->mixer_data = NULL;
327 chip->mixer_free = NULL;
332 * mixer controls
334 static int snd_pmac_awacs_info_volume_amp(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
336 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
337 uinfo->count = 2;
338 uinfo->value.integer.min = 0;
339 uinfo->value.integer.max = 31;
340 return 0;
343 static int snd_pmac_awacs_get_volume_amp(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
345 pmac_t *chip = snd_kcontrol_chip(kcontrol);
346 int index = kcontrol->private_value;
347 awacs_amp_t *amp = chip->mixer_data;
348 snd_assert(amp, return -EINVAL);
349 snd_assert(index >= 0 && index <= 1, return -EINVAL);
350 ucontrol->value.integer.value[0] = 31 - (amp->amp_vol[index][0] & 31);
351 ucontrol->value.integer.value[1] = 31 - (amp->amp_vol[index][1] & 31);
352 return 0;
355 static int snd_pmac_awacs_put_volume_amp(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
357 pmac_t *chip = snd_kcontrol_chip(kcontrol);
358 int index = kcontrol->private_value;
359 int vol[2];
360 awacs_amp_t *amp = chip->mixer_data;
361 snd_assert(amp, return -EINVAL);
362 snd_assert(index >= 0 && index <= 1, return -EINVAL);
364 vol[0] = (31 - (ucontrol->value.integer.value[0] & 31)) | (amp->amp_vol[index][0] & 32);
365 vol[1] = (31 - (ucontrol->value.integer.value[1] & 31)) | (amp->amp_vol[index][1] & 32);
366 return awacs_amp_set_vol(amp, index, vol[0], vol[1], 1);
369 static int snd_pmac_awacs_get_switch_amp(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
371 pmac_t *chip = snd_kcontrol_chip(kcontrol);
372 int index = kcontrol->private_value;
373 awacs_amp_t *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(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
383 pmac_t *chip = snd_kcontrol_chip(kcontrol);
384 int index = kcontrol->private_value;
385 int vol[2];
386 awacs_amp_t *amp = chip->mixer_data;
387 snd_assert(amp, return -EINVAL);
388 snd_assert(index >= 0 && index <= 1, return -EINVAL);
390 vol[0] = (ucontrol->value.integer.value[0] ? 0 : 32) | (amp->amp_vol[index][0] & 31);
391 vol[1] = (ucontrol->value.integer.value[1] ? 0 : 32) | (amp->amp_vol[index][1] & 31);
392 return awacs_amp_set_vol(amp, index, vol[0], vol[1], 1);
395 static int snd_pmac_awacs_info_tone_amp(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
397 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
398 uinfo->count = 1;
399 uinfo->value.integer.min = 0;
400 uinfo->value.integer.max = 14;
401 return 0;
404 static int snd_pmac_awacs_get_tone_amp(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
406 pmac_t *chip = snd_kcontrol_chip(kcontrol);
407 int index = kcontrol->private_value;
408 awacs_amp_t *amp = chip->mixer_data;
409 snd_assert(amp, return -EINVAL);
410 snd_assert(index >= 0 && index <= 1, return -EINVAL);
411 ucontrol->value.integer.value[0] = amp->amp_tone[index];
412 return 0;
415 static int snd_pmac_awacs_put_tone_amp(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
417 pmac_t *chip = snd_kcontrol_chip(kcontrol);
418 int index = kcontrol->private_value;
419 awacs_amp_t *amp = chip->mixer_data;
420 snd_assert(amp, return -EINVAL);
421 snd_assert(index >= 0 && index <= 1, return -EINVAL);
422 if (ucontrol->value.integer.value[0] != amp->amp_tone[index]) {
423 amp->amp_tone[index] = ucontrol->value.integer.value[0];
424 awacs_amp_set_tone(amp, amp->amp_tone[0], amp->amp_tone[1]);
425 return 1;
427 return 0;
430 static int snd_pmac_awacs_info_master_amp(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
432 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
433 uinfo->count = 1;
434 uinfo->value.integer.min = 0;
435 uinfo->value.integer.max = 99;
436 return 0;
439 static int snd_pmac_awacs_get_master_amp(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
441 pmac_t *chip = snd_kcontrol_chip(kcontrol);
442 awacs_amp_t *amp = chip->mixer_data;
443 snd_assert(amp, return -EINVAL);
444 ucontrol->value.integer.value[0] = amp->amp_master;
445 return 0;
448 static int snd_pmac_awacs_put_master_amp(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
450 pmac_t *chip = snd_kcontrol_chip(kcontrol);
451 awacs_amp_t *amp = chip->mixer_data;
452 snd_assert(amp, return -EINVAL);
453 if (ucontrol->value.integer.value[0] != amp->amp_master) {
454 amp->amp_master = ucontrol->value.integer.value[0];
455 awacs_amp_set_master(amp, amp->amp_master);
456 return 1;
458 return 0;
461 #define AMP_CH_SPK 0
462 #define AMP_CH_HD 1
464 static snd_kcontrol_new_t snd_pmac_awacs_amp_vol[] __initdata = {
465 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
466 .name = "PC Speaker Playback Volume",
467 .info = snd_pmac_awacs_info_volume_amp,
468 .get = snd_pmac_awacs_get_volume_amp,
469 .put = snd_pmac_awacs_put_volume_amp,
470 .private_value = AMP_CH_SPK,
472 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
473 .name = "Headphone 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_HD,
479 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
480 .name = "Tone Control - Bass",
481 .info = snd_pmac_awacs_info_tone_amp,
482 .get = snd_pmac_awacs_get_tone_amp,
483 .put = snd_pmac_awacs_put_tone_amp,
484 .private_value = 0,
486 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
487 .name = "Tone Control - Treble",
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 = 1,
493 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
494 .name = "Amp Master Playback Volume",
495 .info = snd_pmac_awacs_info_master_amp,
496 .get = snd_pmac_awacs_get_master_amp,
497 .put = snd_pmac_awacs_put_master_amp,
501 static snd_kcontrol_new_t snd_pmac_awacs_amp_hp_sw __initdata = {
502 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
503 .name = "Headphone Playback Switch",
504 .info = snd_pmac_boolean_stereo_info,
505 .get = snd_pmac_awacs_get_switch_amp,
506 .put = snd_pmac_awacs_put_switch_amp,
507 .private_value = AMP_CH_HD,
510 static snd_kcontrol_new_t snd_pmac_awacs_amp_spk_sw __initdata = {
511 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
512 .name = "PC Speaker Playback Switch",
513 .info = snd_pmac_boolean_stereo_info,
514 .get = snd_pmac_awacs_get_switch_amp,
515 .put = snd_pmac_awacs_put_switch_amp,
516 .private_value = AMP_CH_SPK,
519 #endif /* PMAC_AMP_AVAIL */
523 * mic boost for screamer
525 static int snd_pmac_screamer_mic_boost_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
527 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
528 uinfo->count = 1;
529 uinfo->value.integer.min = 0;
530 uinfo->value.integer.max = 2;
531 return 0;
534 static int snd_pmac_screamer_mic_boost_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
536 pmac_t *chip = snd_kcontrol_chip(kcontrol);
537 int val;
538 unsigned long flags;
540 spin_lock_irqsave(&chip->reg_lock, flags);
541 if (chip->awacs_reg[6] & MASK_MIC_BOOST)
542 val = 2;
543 else if (chip->awacs_reg[0] & MASK_GAINLINE)
544 val = 1;
545 else
546 val = 0;
547 spin_unlock_irqrestore(&chip->reg_lock, flags);
548 ucontrol->value.integer.value[0] = val;
549 return 0;
552 static int snd_pmac_screamer_mic_boost_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
554 pmac_t *chip = snd_kcontrol_chip(kcontrol);
555 int changed = 0;
556 int val0, val6;
557 unsigned long flags;
559 spin_lock_irqsave(&chip->reg_lock, flags);
560 val0 = chip->awacs_reg[0] & ~MASK_GAINLINE;
561 val6 = chip->awacs_reg[6] & ~MASK_MIC_BOOST;
562 if (ucontrol->value.integer.value[0] > 0) {
563 val0 |= MASK_GAINLINE;
564 if (ucontrol->value.integer.value[0] > 1)
565 val6 |= MASK_MIC_BOOST;
567 if (val0 != chip->awacs_reg[0]) {
568 snd_pmac_awacs_write_reg(chip, 0, val0);
569 changed = 1;
571 if (val6 != chip->awacs_reg[6]) {
572 snd_pmac_awacs_write_reg(chip, 6, val6);
573 changed = 1;
575 spin_unlock_irqrestore(&chip->reg_lock, flags);
576 return changed;
580 * lists of mixer elements
582 static snd_kcontrol_new_t snd_pmac_awacs_mixers[] __initdata = {
583 AWACS_VOLUME("Master Playback Volume", 2, 6, 1),
584 AWACS_SWITCH("Master Capture Switch", 1, SHIFT_LOOPTHRU, 0),
585 AWACS_VOLUME("Capture Volume", 0, 4, 0),
586 AWACS_SWITCH("Line Capture Switch", 0, SHIFT_MUX_LINE, 0),
587 AWACS_SWITCH("CD Capture Switch", 0, SHIFT_MUX_CD, 0),
588 AWACS_SWITCH("Mic Capture Switch", 0, SHIFT_MUX_MIC, 0),
591 static snd_kcontrol_new_t snd_pmac_awacs_master_sw __initdata =
592 AWACS_SWITCH("Master Playback Switch", 1, SHIFT_HDMUTE, 1);
594 static snd_kcontrol_new_t snd_pmac_awacs_mic_boost[] __initdata = {
595 AWACS_SWITCH("Mic Boost", 0, SHIFT_GAINLINE, 0),
598 static snd_kcontrol_new_t snd_pmac_screamer_mic_boost[] __initdata = {
599 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
600 .name = "Mic Boost",
601 .info = snd_pmac_screamer_mic_boost_info,
602 .get = snd_pmac_screamer_mic_boost_get,
603 .put = snd_pmac_screamer_mic_boost_put,
607 static snd_kcontrol_new_t snd_pmac_awacs_speaker_vol[] __initdata = {
608 AWACS_VOLUME("PC Speaker Playback Volume", 4, 6, 1),
610 static snd_kcontrol_new_t snd_pmac_awacs_speaker_sw __initdata =
611 AWACS_SWITCH("PC Speaker Playback Switch", 1, SHIFT_SPKMUTE, 1);
614 #define num_controls(ary) (sizeof(ary) / sizeof(snd_kcontrol_new_t))
617 * add new mixer elements to the card
619 static int build_mixers(pmac_t *chip, int nums, snd_kcontrol_new_t *mixers)
621 int i, err;
623 for (i = 0; i < nums; i++) {
624 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&mixers[i], chip))) < 0)
625 return err;
627 return 0;
632 * restore all registers
634 static void awacs_restore_all_regs(pmac_t *chip, int can_schedule)
636 snd_pmac_awacs_write_noreg(chip, 0, chip->awacs_reg[0]);
637 snd_pmac_awacs_write_noreg(chip, 1, chip->awacs_reg[1]);
638 snd_pmac_awacs_write_noreg(chip, 2, chip->awacs_reg[2]);
639 snd_pmac_awacs_write_noreg(chip, 4, chip->awacs_reg[4]);
640 if (chip->model == PMAC_SCREAMER) {
641 snd_pmac_awacs_write_noreg(chip, 5, chip->awacs_reg[5]);
642 snd_pmac_awacs_write_noreg(chip, 6, chip->awacs_reg[6]);
643 snd_pmac_awacs_write_noreg(chip, 7, chip->awacs_reg[7]);
647 #ifdef CONFIG_PMAC_PBOOK
648 static void snd_pmac_awacs_suspend(pmac_t *chip)
650 snd_pmac_awacs_write_noreg(chip, 1, (chip->awacs_reg[1]
651 | MASK_AMUTE | MASK_CMUTE));
654 static void snd_pmac_awacs_resume(pmac_t *chip)
656 if (machine_is_compatible("PowerBook3,1")
657 || machine_is_compatible("PowerBook3,2")) {
658 do_mdelay(100, 0);
659 snd_pmac_awacs_write_reg(chip, 1,
660 chip->awacs_reg[1] & ~MASK_PAROUT);
661 do_mdelay(300, 0);
664 awacs_restore_all_regs(chip, 0);
665 if (chip->model == PMAC_SCREAMER) {
666 /* reset power bits in reg 6 */
667 mdelay(5);
668 snd_pmac_awacs_write_noreg(chip, 6, chip->awacs_reg[6]);
670 screamer_recalibrate(chip, 0);
671 #ifdef PMAC_AMP_AVAIL
672 if (chip->mixer_data) {
673 awacs_amp_t *amp = chip->mixer_data;
674 awacs_amp_set_vol(amp, 0, amp->amp_vol[0][0], amp->amp_vol[0][1], 0);
675 awacs_amp_set_vol(amp, 1, amp->amp_vol[1][0], amp->amp_vol[1][1], 0);
676 awacs_amp_set_tone(amp, amp->amp_tone[0], amp->amp_tone[1]);
677 awacs_amp_set_master(amp, amp->amp_master);
679 #endif
681 #endif /* CONFIG_PMAC_PBOOK */
683 #ifdef PMAC_SUPPORT_AUTOMUTE
685 * auto-mute stuffs
687 static int snd_pmac_awacs_detect_headphone(pmac_t *chip)
689 return (in_le32(&chip->awacs->codec_stat) & chip->hp_stat_mask) ? 1 : 0;
692 #ifdef PMAC_AMP_AVAIL
693 static int toggle_amp_mute(awacs_amp_t *amp, int index, int mute)
695 int vol[2];
696 vol[0] = amp->amp_vol[index][0] & 31;
697 vol[1] = amp->amp_vol[index][1] & 31;
698 if (mute) {
699 vol[0] |= 32;
700 vol[1] |= 32;
702 return awacs_amp_set_vol(amp, index, vol[0], vol[1], 1);
704 #endif
706 static void snd_pmac_awacs_update_automute(pmac_t *chip, int do_notify)
708 if (chip->auto_mute) {
709 #ifdef PMAC_AMP_AVAIL
710 if (chip->mixer_data) {
711 awacs_amp_t *amp = chip->mixer_data;
712 int changed;
713 if (snd_pmac_awacs_detect_headphone(chip)) {
714 changed = toggle_amp_mute(amp, AMP_CH_HD, 0);
715 changed |= toggle_amp_mute(amp, AMP_CH_SPK, 1);
716 } else {
717 changed = toggle_amp_mute(amp, AMP_CH_HD, 1);
718 changed |= toggle_amp_mute(amp, AMP_CH_SPK, 0);
720 if (do_notify && ! changed)
721 return;
722 } else
723 #endif
725 int reg = chip->awacs_reg[1] | (MASK_HDMUTE|MASK_SPKMUTE);
726 if (snd_pmac_awacs_detect_headphone(chip))
727 reg &= ~MASK_HDMUTE;
728 else
729 reg &= ~MASK_SPKMUTE;
730 if (do_notify && reg == chip->awacs_reg[1])
731 return;
732 snd_pmac_awacs_write_reg(chip, 1, reg);
734 if (do_notify) {
735 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
736 &chip->master_sw_ctl->id);
737 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
738 &chip->speaker_sw_ctl->id);
739 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
740 &chip->hp_detect_ctl->id);
744 #endif /* PMAC_SUPPORT_AUTOMUTE */
748 * initialize chip
750 int __init
751 snd_pmac_awacs_init(pmac_t *chip)
753 int err, vol;
755 /* looks like MASK_GAINLINE triggers something, so we set here
756 * as start-up
758 chip->awacs_reg[0] = MASK_MUX_CD | 0xff | MASK_GAINLINE;
759 chip->awacs_reg[1] = MASK_CMUTE | MASK_AMUTE;
760 /* FIXME: Only machines with external SRS module need MASK_PAROUT */
761 if (chip->has_iic || chip->device_id == 0x5 ||
762 /*chip->_device_id == 0x8 || */
763 chip->device_id == 0xb)
764 chip->awacs_reg[1] |= MASK_PAROUT;
765 /* get default volume from nvram */
766 // vol = (~nvram_read_byte(0x1308) & 7) << 1;
767 // vol = ((pmac_xpram_read( 8 ) & 7 ) << 1 );
768 vol = 0x0f; /* no, on alsa, muted as default */
769 vol = vol + (vol << 6);
770 chip->awacs_reg[2] = vol;
771 chip->awacs_reg[4] = vol;
772 if (chip->model == PMAC_SCREAMER) {
773 chip->awacs_reg[5] = vol; /* FIXME: screamer has loopthru vol control */
774 chip->awacs_reg[6] = MASK_MIC_BOOST; /* FIXME: maybe should be vol << 3 for PCMCIA speaker */
775 chip->awacs_reg[7] = 0;
778 awacs_restore_all_regs(chip, 1);
779 chip->manufacturer = (in_le32(&chip->awacs->codec_stat) >> 8) & 0xf;
780 screamer_recalibrate(chip, 1);
782 chip->revision = (in_le32(&chip->awacs->codec_stat) >> 12) & 0xf;
783 #ifdef PMAC_AMP_AVAIL
784 if (chip->revision == 3 && chip->has_iic && CHECK_CUDA_AMP()) {
785 awacs_amp_t *amp = kmalloc(sizeof(*amp), GFP_KERNEL);
786 if (! amp)
787 return -ENOMEM;
788 chip->mixer_data = amp;
789 memset(amp, 0, sizeof(*amp));
790 chip->mixer_free = awacs_amp_free;
791 awacs_amp_set_vol(amp, 0, 63, 63, 0); /* mute and zero vol */
792 awacs_amp_set_vol(amp, 1, 63, 63, 0);
793 awacs_amp_set_tone(amp, 7, 7); /* 0 dB */
794 awacs_amp_set_master(amp, 79); /* 0 dB */
796 #endif /* PMAC_AMP_AVAIL */
798 if (chip->hp_stat_mask == 0) {
799 /* set headphone-jack detection bit */
800 switch (chip->model) {
801 case PMAC_AWACS:
802 chip->hp_stat_mask = 0x04;
803 break;
804 case PMAC_SCREAMER:
805 switch (chip->device_id) {
806 case 0x08:
807 /* 1 = side jack, 2 = front jack */
808 chip->hp_stat_mask = 0x03;
809 break;
810 case 0x00:
811 case 0x05:
812 chip->hp_stat_mask = 0x04;
813 break;
814 default:
815 chip->hp_stat_mask = 0x08;
816 break;
818 break;
819 default:
820 snd_BUG();
821 break;
826 * build mixers
828 strcpy(chip->card->mixername, "PowerMac AWACS");
830 if ((err = build_mixers(chip, num_controls(snd_pmac_awacs_mixers),
831 snd_pmac_awacs_mixers)) < 0)
832 return err;
833 chip->master_sw_ctl = snd_ctl_new1(&snd_pmac_awacs_master_sw, chip);
834 if ((err = snd_ctl_add(chip->card, chip->master_sw_ctl)) < 0)
835 return err;
836 #ifdef PMAC_AMP_AVAIL
837 if (chip->mixer_data) {
838 /* use amplifier. the signal is connected from route A
839 * to the amp. the amp has its headphone and speaker
840 * volumes and mute switches, so we use them instead of
841 * screamer registers.
842 * in this case, it seems the route C is not used.
844 if ((err = build_mixers(chip, num_controls(snd_pmac_awacs_amp_vol),
845 snd_pmac_awacs_amp_vol)) < 0)
846 return err;
847 /* overwrite */
848 chip->master_sw_ctl = snd_ctl_new1(&snd_pmac_awacs_amp_hp_sw, chip);
849 if ((err = snd_ctl_add(chip->card, chip->master_sw_ctl)) < 0)
850 return err;
851 chip->speaker_sw_ctl = snd_ctl_new1(&snd_pmac_awacs_amp_spk_sw, chip);
852 if ((err = snd_ctl_add(chip->card, chip->speaker_sw_ctl)) < 0)
853 return err;
854 } else
855 #endif /* PMAC_AMP_AVAIL */
857 /* route A = headphone, route C = speaker */
858 if ((err = build_mixers(chip, num_controls(snd_pmac_awacs_speaker_vol),
859 snd_pmac_awacs_speaker_vol)) < 0)
860 return err;
861 chip->speaker_sw_ctl = snd_ctl_new1(&snd_pmac_awacs_speaker_sw, chip);
862 if ((err = snd_ctl_add(chip->card, chip->speaker_sw_ctl)) < 0)
863 return err;
866 if (chip->model == PMAC_SCREAMER) {
867 if ((err = build_mixers(chip, num_controls(snd_pmac_screamer_mic_boost),
868 snd_pmac_screamer_mic_boost)) < 0)
869 return err;
870 } else {
871 if ((err = build_mixers(chip, num_controls(snd_pmac_awacs_mic_boost),
872 snd_pmac_awacs_mic_boost)) < 0)
873 return err;
877 * set lowlevel callbacks
879 chip->set_format = snd_pmac_awacs_set_format;
880 #ifdef CONFIG_PMAC_PBOOK
881 chip->suspend = snd_pmac_awacs_suspend;
882 chip->resume = snd_pmac_awacs_resume;
883 #endif
884 #ifdef PMAC_SUPPORT_AUTOMUTE
885 if ((err = snd_pmac_add_automute(chip)) < 0)
886 return err;
887 chip->detect_headphone = snd_pmac_awacs_detect_headphone;
888 chip->update_automute = snd_pmac_awacs_update_automute;
889 snd_pmac_awacs_update_automute(chip, 0); /* update the status only */
890 #endif
891 if (chip->model == PMAC_SCREAMER) {
892 snd_pmac_awacs_write_noreg(chip, 6, chip->awacs_reg[6]);
893 snd_pmac_awacs_write_noreg(chip, 0, chip->awacs_reg[0]);
896 return 0;