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