ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / sound / ppc / awacs.c
1 /*
2  * PMac AWACS lowlevel functions
3  *
4  * Copyright (c) by Takashi Iwai <tiwai@suse.de>
5  * code based on dmasound.c.
6  *
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.
11  *
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.
16  *
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
20  */
21
22
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"
31
32 #define chip_t pmac_t
33
34
35 #ifdef CONFIG_ADB_CUDA
36 #define PMAC_AMP_AVAIL
37 #endif
38
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;
45
46 #define CHECK_CUDA_AMP() (sys_ctrler == SYS_CTRLER_CUDA)
47
48 #endif /* PMAC_AMP_AVAIL */
49
50
51 static void snd_pmac_screamer_wait(pmac_t *chip)
52 {
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;
59                 }
60         }
61 }
62
63 /*
64  * write AWACS register
65  */
66 static void
67 snd_pmac_awacs_write(pmac_t *chip, int val)
68 {
69         long timeout = 5000000;
70
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;
78                 }
79         }
80 }
81
82 static void
83 snd_pmac_awacs_write_reg(pmac_t *chip, int reg, int val)
84 {
85         snd_pmac_awacs_write(chip, val | (reg << 12));
86         chip->awacs_reg[reg] = val;
87 }
88
89 static void
90 snd_pmac_awacs_write_noreg(pmac_t *chip, int reg, int val)
91 {
92         snd_pmac_awacs_write(chip, val | (reg << 12));
93 }
94
95 #ifdef CONFIG_PMAC_PBOOK
96 /* Recalibrate chip */
97 static void screamer_recalibrate(pmac_t *chip)
98 {
99         if (chip->model != PMAC_SCREAMER)
100                 return;
101
102         /* Sorry for the horrible delays... I hope to get that improved
103          * by making the whole PM process asynchronous in a future version
104          */
105         snd_pmac_awacs_write_noreg(chip, 1, chip->awacs_reg[1]);
106         if (chip->manufacturer == 0x1)
107                 /* delay for broken crystal part */
108                 big_mdelay(750);
109         snd_pmac_awacs_write_noreg(chip, 1,
110                                    chip->awacs_reg[1] | MASK_RECALIBRATE | MASK_CMUTE | MASK_AMUTE);
111         snd_pmac_awacs_write_noreg(chip, 1, chip->awacs_reg[1]);
112         snd_pmac_awacs_write_noreg(chip, 6, chip->awacs_reg[6]);
113 }
114
115 #else
116 #define screamer_recalibrate(chip) /* NOP */
117 #endif
118
119
120 /*
121  * additional callback to set the pcm format
122  */
123 static void snd_pmac_awacs_set_format(pmac_t *chip)
124 {
125         chip->awacs_reg[1] &= ~MASK_SAMPLERATE;
126         chip->awacs_reg[1] |= chip->rate_index << 3;
127         snd_pmac_awacs_write_reg(chip, 1, chip->awacs_reg[1]);
128 }
129
130
131 /*
132  * AWACS volume callbacks
133  */
134 /*
135  * volumes: 0-15 stereo
136  */
137 static int snd_pmac_awacs_info_volume(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
138 {
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;
144 }
145  
146 static int snd_pmac_awacs_get_volume(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
147 {
148         pmac_t *chip = snd_kcontrol_chip(kcontrol);
149         int reg = kcontrol->private_value & 0xff;
150         int lshift = (kcontrol->private_value >> 8) & 0xff;
151         int inverted = (kcontrol->private_value >> 16) & 1;
152         unsigned long flags;
153         int vol[2];
154
155         spin_lock_irqsave(&chip->reg_lock, flags);
156         vol[0] = (chip->awacs_reg[reg] >> lshift) & 0xf;
157         vol[1] = chip->awacs_reg[reg] & 0xf;
158         spin_unlock_irqrestore(&chip->reg_lock, flags);
159         if (inverted) {
160                 vol[0] = 0x0f - vol[0];
161                 vol[1] = 0x0f - vol[1];
162         }
163         ucontrol->value.integer.value[0] = vol[0];
164         ucontrol->value.integer.value[1] = vol[1];
165         return 0;
166 }
167
168 static int snd_pmac_awacs_put_volume(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
169 {
170         pmac_t *chip = snd_kcontrol_chip(kcontrol);
171         int reg = kcontrol->private_value & 0xff;
172         int lshift = (kcontrol->private_value >> 8) & 0xff;
173         int inverted = (kcontrol->private_value >> 16) & 1;
174         int val, oldval;
175         unsigned long flags;
176         int vol[2];
177
178         vol[0] = ucontrol->value.integer.value[0];
179         vol[1] = ucontrol->value.integer.value[1];
180         if (inverted) {
181                 vol[0] = 0x0f - vol[0];
182                 vol[1] = 0x0f - vol[1];
183         }
184         vol[0] &= 0x0f;
185         vol[1] &= 0x0f;
186         spin_lock_irqsave(&chip->reg_lock, flags);
187         oldval = chip->awacs_reg[reg];
188         val = oldval & ~(0xf | (0xf << lshift));
189         val |= vol[0] << lshift;
190         val |= vol[1];
191         if (oldval != val)
192                 snd_pmac_awacs_write_reg(chip, reg, val);
193         spin_unlock_irqrestore(&chip->reg_lock, flags);
194         return oldval != reg;
195 }
196
197
198 #define AWACS_VOLUME(xname, xreg, xshift, xinverted) \
199 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = 0, \
200   .info = snd_pmac_awacs_info_volume, \
201   .get = snd_pmac_awacs_get_volume, \
202   .put = snd_pmac_awacs_put_volume, \
203   .private_value = (xreg) | ((xshift) << 8) | ((xinverted) << 16) }
204
205 /*
206  * mute master/ogain for AWACS: mono
207  */
208 static int snd_pmac_awacs_get_switch(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
209 {
210         pmac_t *chip = snd_kcontrol_chip(kcontrol);
211         int reg = kcontrol->private_value & 0xff;
212         int shift = (kcontrol->private_value >> 8) & 0xff;
213         int invert = (kcontrol->private_value >> 16) & 1;
214         int val;
215         unsigned long flags;
216
217         spin_lock_irqsave(&chip->reg_lock, flags);
218         val = (chip->awacs_reg[reg] >> shift) & 1;
219         spin_unlock_irqrestore(&chip->reg_lock, flags);
220         if (invert)
221                 val = 1 - val;
222         ucontrol->value.integer.value[0] = val;
223         return 0;
224 }
225
226 static int snd_pmac_awacs_put_switch(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
227 {
228         pmac_t *chip = snd_kcontrol_chip(kcontrol);
229         int reg = kcontrol->private_value & 0xff;
230         int shift = (kcontrol->private_value >> 8) & 0xff;
231         int invert = (kcontrol->private_value >> 16) & 1;
232         int mask = 1 << shift;
233         int val, changed;
234         unsigned long flags;
235
236         spin_lock_irqsave(&chip->reg_lock, flags);
237         val = chip->awacs_reg[reg] & ~mask;
238         if (ucontrol->value.integer.value[0] != invert)
239                 val |= mask;
240         changed = chip->awacs_reg[reg] != val;
241         if (changed)
242                 snd_pmac_awacs_write_reg(chip, reg, val);
243         spin_unlock_irqrestore(&chip->reg_lock, flags);
244         return changed;
245 }
246
247 #define AWACS_SWITCH(xname, xreg, xshift, xinvert) \
248 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = 0, \
249   .info = snd_pmac_boolean_mono_info, \
250   .get = snd_pmac_awacs_get_switch, \
251   .put = snd_pmac_awacs_put_switch, \
252   .private_value = (xreg) | ((xshift) << 8) | ((xinvert) << 16) }
253
254
255 #ifdef PMAC_AMP_AVAIL
256 /*
257  * controls for perch/whisper extension cards, e.g. G3 desktop
258  *
259  * TDA7433 connected via i2c address 0x45 (= 0x8a),
260  * accessed through cuda
261  */
262 static void awacs_set_cuda(int reg, int val)
263 {
264         struct adb_request req;
265         cuda_request(&req, NULL, 5, CUDA_PACKET, CUDA_GET_SET_IIC, 0x8a, reg, val);
266         while (! req.complete)
267                 cuda_poll();
268 }
269
270 /*
271  * level = 0 - 14, 7 = 0 dB
272  */
273 static void awacs_amp_set_tone(awacs_amp_t *amp, int bass, int treble)
274 {
275         amp->amp_tone[0] = bass;
276         amp->amp_tone[1] = treble;
277         if (bass > 7)
278                 bass = (14 - bass) + 8;
279         if (treble > 7)
280                 treble = (14 - treble) + 8;
281         awacs_set_cuda(2, (bass << 4) | treble);
282 }
283
284 /*
285  * vol = 0 - 31 (attenuation), 32 = mute bit, stereo
286  */
287 static int awacs_amp_set_vol(awacs_amp_t *amp, int index, int lvol, int rvol, int do_check)
288 {
289         if (do_check && amp->amp_vol[index][0] == lvol &&
290             amp->amp_vol[index][1] == rvol)
291                 return 0;
292         awacs_set_cuda(3 + index, lvol);
293         awacs_set_cuda(5 + index, rvol);
294         amp->amp_vol[index][0] = lvol;
295         amp->amp_vol[index][1] = rvol;
296         return 1;
297 }
298
299 /*
300  * 0 = -79 dB, 79 = 0 dB, 99 = +20 dB
301  */
302 static void awacs_amp_set_master(awacs_amp_t *amp, int vol)
303 {
304         amp->amp_master = vol;
305         if (vol <= 79)
306                 vol = 32 + (79 - vol);
307         else
308                 vol = 32 - (vol - 79);
309         awacs_set_cuda(1, vol);
310 }
311
312 static void awacs_amp_free(pmac_t *chip)
313 {
314         awacs_amp_t *amp = chip->mixer_data;
315         snd_assert(amp, return);
316         kfree(amp);
317         chip->mixer_data = NULL;
318         chip->mixer_free = NULL;
319 }
320
321
322 /*
323  * mixer controls
324  */
325 static int snd_pmac_awacs_info_volume_amp(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
326 {
327         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
328         uinfo->count = 2;
329         uinfo->value.integer.min = 0;
330         uinfo->value.integer.max = 31;
331         return 0;
332 }
333  
334 static int snd_pmac_awacs_get_volume_amp(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
335 {
336         pmac_t *chip = snd_kcontrol_chip(kcontrol);
337         int index = kcontrol->private_value;
338         awacs_amp_t *amp = chip->mixer_data;
339         snd_assert(amp, return -EINVAL);
340         snd_assert(index >= 0 && index <= 1, return -EINVAL);
341         ucontrol->value.integer.value[0] = 31 - (amp->amp_vol[index][0] & 31);
342         ucontrol->value.integer.value[1] = 31 - (amp->amp_vol[index][1] & 31);
343         return 0;
344 }
345
346 static int snd_pmac_awacs_put_volume_amp(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
347 {
348         pmac_t *chip = snd_kcontrol_chip(kcontrol);
349         int index = kcontrol->private_value;
350         int vol[2];
351         awacs_amp_t *amp = chip->mixer_data;
352         snd_assert(amp, return -EINVAL);
353         snd_assert(index >= 0 && index <= 1, return -EINVAL);
354
355         vol[0] = (31 - (ucontrol->value.integer.value[0] & 31)) | (amp->amp_vol[index][0] & 32);
356         vol[1] = (31 - (ucontrol->value.integer.value[1] & 31)) | (amp->amp_vol[index][1] & 32);
357         return awacs_amp_set_vol(amp, index, vol[0], vol[1], 1);
358 }
359
360 static int snd_pmac_awacs_get_switch_amp(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
361 {
362         pmac_t *chip = snd_kcontrol_chip(kcontrol);
363         int index = kcontrol->private_value;
364         awacs_amp_t *amp = chip->mixer_data;
365         snd_assert(amp, return -EINVAL);
366         snd_assert(index >= 0 && index <= 1, return -EINVAL);
367         ucontrol->value.integer.value[0] = (amp->amp_vol[index][0] & 32) ? 0 : 1;
368         ucontrol->value.integer.value[1] = (amp->amp_vol[index][1] & 32) ? 0 : 1;
369         return 0;
370 }
371
372 static int snd_pmac_awacs_put_switch_amp(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
373 {
374         pmac_t *chip = snd_kcontrol_chip(kcontrol);
375         int index = kcontrol->private_value;
376         int vol[2];
377         awacs_amp_t *amp = chip->mixer_data;
378         snd_assert(amp, return -EINVAL);
379         snd_assert(index >= 0 && index <= 1, return -EINVAL);
380
381         vol[0] = (ucontrol->value.integer.value[0] ? 0 : 32) | (amp->amp_vol[index][0] & 31);
382         vol[1] = (ucontrol->value.integer.value[1] ? 0 : 32) | (amp->amp_vol[index][1] & 31);
383         return awacs_amp_set_vol(amp, index, vol[0], vol[1], 1);
384 }
385
386 static int snd_pmac_awacs_info_tone_amp(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
387 {
388         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
389         uinfo->count = 1;
390         uinfo->value.integer.min = 0;
391         uinfo->value.integer.max = 14;
392         return 0;
393 }
394  
395 static int snd_pmac_awacs_get_tone_amp(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
396 {
397         pmac_t *chip = snd_kcontrol_chip(kcontrol);
398         int index = kcontrol->private_value;
399         awacs_amp_t *amp = chip->mixer_data;
400         snd_assert(amp, return -EINVAL);
401         snd_assert(index >= 0 && index <= 1, return -EINVAL);
402         ucontrol->value.integer.value[0] = amp->amp_tone[index];
403         return 0;
404 }
405
406 static int snd_pmac_awacs_put_tone_amp(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
407 {
408         pmac_t *chip = snd_kcontrol_chip(kcontrol);
409         int index = kcontrol->private_value;
410         awacs_amp_t *amp = chip->mixer_data;
411         snd_assert(amp, return -EINVAL);
412         snd_assert(index >= 0 && index <= 1, return -EINVAL);
413         if (ucontrol->value.integer.value[0] != amp->amp_tone[index]) {
414                 amp->amp_tone[index] = ucontrol->value.integer.value[0];
415                 awacs_amp_set_tone(amp, amp->amp_tone[0], amp->amp_tone[1]);
416                 return 1;
417         }
418         return 0;
419 }
420
421 static int snd_pmac_awacs_info_master_amp(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
422 {
423         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
424         uinfo->count = 1;
425         uinfo->value.integer.min = 0;
426         uinfo->value.integer.max = 99;
427         return 0;
428 }
429  
430 static int snd_pmac_awacs_get_master_amp(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
431 {
432         pmac_t *chip = snd_kcontrol_chip(kcontrol);
433         awacs_amp_t *amp = chip->mixer_data;
434         snd_assert(amp, return -EINVAL);
435         ucontrol->value.integer.value[0] = amp->amp_master;
436         return 0;
437 }
438
439 static int snd_pmac_awacs_put_master_amp(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
440 {
441         pmac_t *chip = snd_kcontrol_chip(kcontrol);
442         awacs_amp_t *amp = chip->mixer_data;
443         snd_assert(amp, return -EINVAL);
444         if (ucontrol->value.integer.value[0] != amp->amp_master) {
445                 amp->amp_master = ucontrol->value.integer.value[0];
446                 awacs_amp_set_master(amp, amp->amp_master);
447                 return 1;
448         }
449         return 0;
450 }
451
452 #define AMP_CH_SPK      0
453 #define AMP_CH_HD       1
454
455 static snd_kcontrol_new_t snd_pmac_awacs_amp_vol[] __initdata = {
456         { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
457           .name = "PC Speaker Playback Volume",
458           .info = snd_pmac_awacs_info_volume_amp,
459           .get = snd_pmac_awacs_get_volume_amp,
460           .put = snd_pmac_awacs_put_volume_amp,
461           .private_value = AMP_CH_SPK,
462         },
463         { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
464           .name = "Headphone Playback Volume",
465           .info = snd_pmac_awacs_info_volume_amp,
466           .get = snd_pmac_awacs_get_volume_amp,
467           .put = snd_pmac_awacs_put_volume_amp,
468           .private_value = AMP_CH_HD,
469         },
470         { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
471           .name = "Tone Control - Bass",
472           .info = snd_pmac_awacs_info_tone_amp,
473           .get = snd_pmac_awacs_get_tone_amp,
474           .put = snd_pmac_awacs_put_tone_amp,
475           .private_value = 0,
476         },
477         { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
478           .name = "Tone Control - Treble",
479           .info = snd_pmac_awacs_info_tone_amp,
480           .get = snd_pmac_awacs_get_tone_amp,
481           .put = snd_pmac_awacs_put_tone_amp,
482           .private_value = 1,
483         },
484         { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
485           .name = "Amp Master Playback Volume",
486           .info = snd_pmac_awacs_info_master_amp,
487           .get = snd_pmac_awacs_get_master_amp,
488           .put = snd_pmac_awacs_put_master_amp,
489         },
490 };
491
492 static snd_kcontrol_new_t snd_pmac_awacs_amp_hp_sw __initdata = {
493         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
494         .name = "Headphone Playback Switch",
495         .info = snd_pmac_boolean_stereo_info,
496         .get = snd_pmac_awacs_get_switch_amp,
497         .put = snd_pmac_awacs_put_switch_amp,
498         .private_value = AMP_CH_HD,
499 };
500
501 static snd_kcontrol_new_t snd_pmac_awacs_amp_spk_sw __initdata = {
502         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
503         .name = "PC Speaker 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_SPK,
508 };
509
510 #endif /* PMAC_AMP_AVAIL */
511
512
513 /*
514  * mic boost for screamer
515  */
516 static int snd_pmac_screamer_mic_boost_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
517 {
518         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
519         uinfo->count = 1;
520         uinfo->value.integer.min = 0;
521         uinfo->value.integer.max = 2;
522         return 0;
523 }
524
525 static int snd_pmac_screamer_mic_boost_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
526 {
527         pmac_t *chip = snd_kcontrol_chip(kcontrol);
528         int val;
529         unsigned long flags;
530
531         spin_lock_irqsave(&chip->reg_lock, flags);
532         if (chip->awacs_reg[6] & MASK_MIC_BOOST)
533                 val = 2;
534         else if (chip->awacs_reg[0] & MASK_GAINLINE)
535                 val = 1;
536         else
537                 val = 0;
538         spin_unlock_irqrestore(&chip->reg_lock, flags);
539         ucontrol->value.integer.value[0] = val;
540         return 0;
541 }
542
543 static int snd_pmac_screamer_mic_boost_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
544 {
545         pmac_t *chip = snd_kcontrol_chip(kcontrol);
546         int changed = 0;
547         int val0, val6;
548         unsigned long flags;
549
550         spin_lock_irqsave(&chip->reg_lock, flags);
551         val0 = chip->awacs_reg[0] & ~MASK_GAINLINE;
552         val6 = chip->awacs_reg[6] & ~MASK_MIC_BOOST;
553         if (ucontrol->value.integer.value[0] > 0) {
554                 val0 |= MASK_GAINLINE;
555                 if (ucontrol->value.integer.value[0] > 1)
556                         val6 |= MASK_MIC_BOOST;
557         }
558         if (val0 != chip->awacs_reg[0]) {
559                 snd_pmac_awacs_write_reg(chip, 0, val0);
560                 changed = 1;
561         }
562         if (val6 != chip->awacs_reg[6]) {
563                 snd_pmac_awacs_write_reg(chip, 6, val6);
564                 changed = 1;
565         }
566         spin_unlock_irqrestore(&chip->reg_lock, flags);
567         return changed;
568 }
569
570 /*
571  * lists of mixer elements
572  */
573 static snd_kcontrol_new_t snd_pmac_awacs_mixers[] __initdata = {
574         AWACS_VOLUME("Master Playback Volume", 2, 6, 1),
575         AWACS_SWITCH("Master Capture Switch", 1, SHIFT_LOOPTHRU, 0),
576         AWACS_VOLUME("Capture Volume", 0, 4, 0),
577         AWACS_SWITCH("Line Capture Switch", 0, SHIFT_MUX_LINE, 0),
578         AWACS_SWITCH("CD Capture Switch", 0, SHIFT_MUX_CD, 0),
579         AWACS_SWITCH("Mic Capture Switch", 0, SHIFT_MUX_MIC, 0),
580 };
581
582 static snd_kcontrol_new_t snd_pmac_awacs_master_sw __initdata =
583 AWACS_SWITCH("Master Playback Switch", 1, SHIFT_HDMUTE, 1);
584
585 static snd_kcontrol_new_t snd_pmac_awacs_mic_boost[] __initdata = {
586         AWACS_SWITCH("Mic Boost", 0, SHIFT_GAINLINE, 0),
587 };
588
589 static snd_kcontrol_new_t snd_pmac_screamer_mic_boost[] __initdata = {
590         { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
591           .name = "Mic Boost",
592           .info = snd_pmac_screamer_mic_boost_info,
593           .get = snd_pmac_screamer_mic_boost_get,
594           .put = snd_pmac_screamer_mic_boost_put,
595         },
596 };
597
598 static snd_kcontrol_new_t snd_pmac_awacs_speaker_vol[] __initdata = {
599         AWACS_VOLUME("PC Speaker Playback Volume", 4, 6, 1),
600 };
601 static snd_kcontrol_new_t snd_pmac_awacs_speaker_sw __initdata =
602 AWACS_SWITCH("PC Speaker Playback Switch", 1, SHIFT_SPKMUTE, 1);
603
604
605 #define num_controls(ary) (sizeof(ary) / sizeof(snd_kcontrol_new_t))
606
607 /*
608  * add new mixer elements to the card
609  */
610 static int build_mixers(pmac_t *chip, int nums, snd_kcontrol_new_t *mixers)
611 {
612         int i, err;
613
614         for (i = 0; i < nums; i++) {
615                 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&mixers[i], chip))) < 0)
616                         return err;
617         }
618         return 0;
619 }
620
621
622 /*
623  * restore all registers
624  */
625 static void awacs_restore_all_regs(pmac_t *chip)
626 {
627         snd_pmac_awacs_write_noreg(chip, 0, chip->awacs_reg[0]);
628         snd_pmac_awacs_write_noreg(chip, 1, chip->awacs_reg[1]);
629         snd_pmac_awacs_write_noreg(chip, 2, chip->awacs_reg[2]);
630         snd_pmac_awacs_write_noreg(chip, 4, chip->awacs_reg[4]);
631         if (chip->model == PMAC_SCREAMER) {
632                 snd_pmac_awacs_write_noreg(chip, 5, chip->awacs_reg[5]);
633                 snd_pmac_awacs_write_noreg(chip, 6, chip->awacs_reg[6]);
634                 snd_pmac_awacs_write_noreg(chip, 7, chip->awacs_reg[7]);
635         }
636 }
637
638 #ifdef CONFIG_PMAC_PBOOK
639 static void snd_pmac_awacs_suspend(pmac_t *chip)
640 {
641         snd_pmac_awacs_write_noreg(chip, 1, (chip->awacs_reg[1]
642                                              | MASK_AMUTE | MASK_CMUTE));
643 }
644
645 static void snd_pmac_awacs_resume(pmac_t *chip)
646 {
647         if (machine_is_compatible("PowerBook3,1")
648             || machine_is_compatible("PowerBook3,2")) {
649                 big_mdelay(100);
650                 snd_pmac_awacs_write_reg(chip, 1,
651                         chip->awacs_reg[1] & ~MASK_PAROUT);
652                 big_mdelay(300);
653         }
654
655         awacs_restore_all_regs(chip);
656         if (chip->model == PMAC_SCREAMER) {
657                 /* reset power bits in reg 6 */
658                 mdelay(5);
659                 snd_pmac_awacs_write_noreg(chip, 6, chip->awacs_reg[6]);
660         }
661         screamer_recalibrate(chip);
662 #ifdef PMAC_AMP_AVAIL
663         if (chip->mixer_data) {
664                 awacs_amp_t *amp = chip->mixer_data;
665                 awacs_amp_set_vol(amp, 0, amp->amp_vol[0][0], amp->amp_vol[0][1], 0);
666                 awacs_amp_set_vol(amp, 1, amp->amp_vol[1][0], amp->amp_vol[1][1], 0);
667                 awacs_amp_set_tone(amp, amp->amp_tone[0], amp->amp_tone[1]);
668                 awacs_amp_set_master(amp, amp->amp_master);
669         }
670 #endif
671 }
672 #endif /* CONFIG_PMAC_PBOOK */
673
674 #ifdef PMAC_SUPPORT_AUTOMUTE
675 /*
676  * auto-mute stuffs
677  */
678 static int snd_pmac_awacs_detect_headphone(pmac_t *chip)
679 {
680         return (in_le32(&chip->awacs->codec_stat) & chip->hp_stat_mask) ? 1 : 0;
681 }
682
683 #ifdef PMAC_AMP_AVAIL
684 static int toggle_amp_mute(awacs_amp_t *amp, int index, int mute)
685 {
686         int vol[2];
687         vol[0] = amp->amp_vol[index][0] & 31;
688         vol[1] = amp->amp_vol[index][1] & 31;
689         if (mute) {
690                 vol[0] |= 32;
691                 vol[1] |= 32;
692         }
693         return awacs_amp_set_vol(amp, index, vol[0], vol[1], 1);
694 }
695 #endif
696
697 static void snd_pmac_awacs_update_automute(pmac_t *chip, int do_notify)
698 {
699         if (chip->auto_mute) {
700 #ifdef PMAC_AMP_AVAIL
701                 if (chip->mixer_data) {
702                         awacs_amp_t *amp = chip->mixer_data;
703                         int changed;
704                         if (snd_pmac_awacs_detect_headphone(chip)) {
705                                 changed = toggle_amp_mute(amp, AMP_CH_HD, 0);
706                                 changed |= toggle_amp_mute(amp, AMP_CH_SPK, 1);
707                         } else {
708                                 changed = toggle_amp_mute(amp, AMP_CH_HD, 1);
709                                 changed |= toggle_amp_mute(amp, AMP_CH_SPK, 0);
710                         }
711                         if (do_notify && ! changed)
712                                 return;
713                 } else
714 #endif
715                 {
716                         int reg = chip->awacs_reg[1] | (MASK_HDMUTE|MASK_SPKMUTE);
717                         if (snd_pmac_awacs_detect_headphone(chip))
718                                 reg &= ~MASK_HDMUTE;
719                         else
720                                 reg &= ~MASK_SPKMUTE;
721                         if (do_notify && reg == chip->awacs_reg[1])
722                                 return;
723                         snd_pmac_awacs_write_reg(chip, 1, reg);
724                 }
725                 if (do_notify) {
726                         snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
727                                        &chip->master_sw_ctl->id);
728                         snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
729                                        &chip->speaker_sw_ctl->id);
730                         snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
731                                        &chip->hp_detect_ctl->id);
732                 }
733         }
734 }
735 #endif /* PMAC_SUPPORT_AUTOMUTE */
736
737
738 /*
739  * initialize chip
740  */
741 int __init
742 snd_pmac_awacs_init(pmac_t *chip)
743 {
744         int err, vol;
745
746         /* looks like MASK_GAINLINE triggers something, so we set here
747          * as start-up
748          */
749         chip->awacs_reg[0] = MASK_MUX_CD | 0xff | MASK_GAINLINE;
750         chip->awacs_reg[1] = MASK_CMUTE | MASK_AMUTE;
751         /* FIXME: Only machines with external SRS module need MASK_PAROUT */
752         if (chip->has_iic || chip->device_id == 0x5 ||
753             /*chip->_device_id == 0x8 || */
754             chip->device_id == 0xb)
755                 chip->awacs_reg[1] |= MASK_PAROUT;
756         /* get default volume from nvram */
757         // vol = (~nvram_read_byte(0x1308) & 7) << 1;
758         // vol = ((pmac_xpram_read( 8 ) & 7 ) << 1 );
759         vol = 0x0f; /* no, on alsa, muted as default */
760         vol = vol + (vol << 6);
761         chip->awacs_reg[2] = vol;
762         chip->awacs_reg[4] = vol;
763         if (chip->model == PMAC_SCREAMER) {
764                 chip->awacs_reg[5] = vol; /* FIXME: screamer has loopthru vol control */
765                 chip->awacs_reg[6] = MASK_MIC_BOOST; /* FIXME: maybe should be vol << 3 for PCMCIA speaker */
766                 chip->awacs_reg[7] = 0;
767         }
768
769         awacs_restore_all_regs(chip);
770         chip->manufacturer = (in_le32(&chip->awacs->codec_stat) >> 8) & 0xf;
771         screamer_recalibrate(chip);
772
773         chip->revision = (in_le32(&chip->awacs->codec_stat) >> 12) & 0xf;
774 #ifdef PMAC_AMP_AVAIL
775         if (chip->revision == 3 && chip->has_iic && CHECK_CUDA_AMP()) {
776                 awacs_amp_t *amp = kmalloc(sizeof(*amp), GFP_KERNEL);
777                 if (! amp)
778                         return -ENOMEM;
779                 chip->mixer_data = amp;
780                 memset(amp, 0, sizeof(*amp));
781                 chip->mixer_free = awacs_amp_free;
782                 awacs_amp_set_vol(amp, 0, 63, 63, 0); /* mute and zero vol */
783                 awacs_amp_set_vol(amp, 1, 63, 63, 0);
784                 awacs_amp_set_tone(amp, 7, 7); /* 0 dB */
785                 awacs_amp_set_master(amp, 79); /* 0 dB */
786         }
787 #endif /* PMAC_AMP_AVAIL */
788
789         if (chip->hp_stat_mask == 0) {
790                 /* set headphone-jack detection bit */
791                 switch (chip->model) {
792                 case PMAC_AWACS:
793                         chip->hp_stat_mask = 0x04;
794                         break;
795                 case PMAC_SCREAMER:
796                         switch (chip->device_id) {
797                         case 0x08:
798                                 /* 1 = side jack, 2 = front jack */
799                                 chip->hp_stat_mask = 0x03;
800                                 break;
801                         case 0x00:
802                         case 0x05:
803                                 chip->hp_stat_mask = 0x04;
804                                 break;
805                         default:
806                                 chip->hp_stat_mask = 0x08;
807                                 break;
808                         }
809                         break;
810                 default:
811                         snd_BUG();
812                         break;
813                 }
814         }
815
816         /*
817          * build mixers
818          */
819         strcpy(chip->card->mixername, "PowerMac AWACS");
820
821         if ((err = build_mixers(chip, num_controls(snd_pmac_awacs_mixers),
822                                 snd_pmac_awacs_mixers)) < 0)
823                 return err;
824         chip->master_sw_ctl = snd_ctl_new1(&snd_pmac_awacs_master_sw, chip);
825         if ((err = snd_ctl_add(chip->card, chip->master_sw_ctl)) < 0)
826                 return err;
827 #ifdef PMAC_AMP_AVAIL
828         if (chip->mixer_data) {
829                 /* use amplifier.  the signal is connected from route A
830                  * to the amp.  the amp has its headphone and speaker
831                  * volumes and mute switches, so we use them instead of
832                  * screamer registers.
833                  * in this case, it seems the route C is not used.
834                  */
835                 if ((err = build_mixers(chip, num_controls(snd_pmac_awacs_amp_vol),
836                                         snd_pmac_awacs_amp_vol)) < 0)
837                         return err;
838                 /* overwrite */
839                 chip->master_sw_ctl = snd_ctl_new1(&snd_pmac_awacs_amp_hp_sw, chip);
840                 if ((err = snd_ctl_add(chip->card, chip->master_sw_ctl)) < 0)
841                         return err;
842                 chip->speaker_sw_ctl = snd_ctl_new1(&snd_pmac_awacs_amp_spk_sw, chip);
843                 if ((err = snd_ctl_add(chip->card, chip->speaker_sw_ctl)) < 0)
844                         return err;
845         } else
846 #endif /* PMAC_AMP_AVAIL */
847         {
848                 /* route A = headphone, route C = speaker */
849                 if ((err = build_mixers(chip, num_controls(snd_pmac_awacs_speaker_vol),
850                                         snd_pmac_awacs_speaker_vol)) < 0)
851                         return err;
852                 chip->speaker_sw_ctl = snd_ctl_new1(&snd_pmac_awacs_speaker_sw, chip);
853                 if ((err = snd_ctl_add(chip->card, chip->speaker_sw_ctl)) < 0)
854                         return err;
855         }
856
857         if (chip->model == PMAC_SCREAMER) {
858                 if ((err = build_mixers(chip, num_controls(snd_pmac_screamer_mic_boost),
859                                         snd_pmac_screamer_mic_boost)) < 0)
860                         return err;
861         } else {
862                 if ((err = build_mixers(chip, num_controls(snd_pmac_awacs_mic_boost),
863                                         snd_pmac_awacs_mic_boost)) < 0)
864                         return err;
865         }
866
867         /*
868          * set lowlevel callbacks
869          */
870         chip->set_format = snd_pmac_awacs_set_format;
871 #ifdef CONFIG_PMAC_PBOOK
872         chip->suspend = snd_pmac_awacs_suspend;
873         chip->resume = snd_pmac_awacs_resume;
874 #endif
875 #ifdef PMAC_SUPPORT_AUTOMUTE
876         if ((err = snd_pmac_add_automute(chip)) < 0)
877                 return err;
878         chip->detect_headphone = snd_pmac_awacs_detect_headphone;
879         chip->update_automute = snd_pmac_awacs_update_automute;
880         snd_pmac_awacs_update_automute(chip, 0); /* update the status only */
881 #endif
882         if (chip->model == PMAC_SCREAMER) {
883                 snd_pmac_awacs_write_noreg(chip, 6, chip->awacs_reg[6]);
884                 snd_pmac_awacs_write_noreg(chip, 0, chip->awacs_reg[0]);
885         }
886
887         return 0;
888 }