ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / sound / ppc / burgundy.c
1 /*
2  * PMac Burgundy 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 #include <sound/driver.h>
23 #include <asm/io.h>
24 #include <linux/init.h>
25 #include <linux/slab.h>
26 #include <linux/delay.h>
27 #include <sound/core.h>
28 #include "pmac.h"
29 #include "burgundy.h"
30
31 #define chip_t pmac_t
32
33
34 /* Waits for busy flag to clear */
35 inline static void
36 snd_pmac_burgundy_busy_wait(pmac_t *chip)
37 {
38         int timeout = 50;
39         while ((in_le32(&chip->awacs->codec_ctrl) & MASK_NEWECMD) && timeout--)
40                 udelay(1);
41         if (! timeout)
42                 printk(KERN_DEBUG "burgundy_busy_wait: timeout\n");
43 }
44
45 inline static void
46 snd_pmac_burgundy_extend_wait(pmac_t *chip)
47 {
48         int timeout;
49         timeout = 50;
50         while (!(in_le32(&chip->awacs->codec_stat) & MASK_EXTEND) && timeout--)
51                 udelay(1);
52         if (! timeout)
53                 printk(KERN_DEBUG "burgundy_extend_wait: timeout #1\n");
54         timeout = 50;
55         while ((in_le32(&chip->awacs->codec_stat) & MASK_EXTEND) && timeout--)
56                 udelay(1);
57         if (! timeout)
58                 printk(KERN_DEBUG "burgundy_extend_wait: timeout #2\n");
59 }
60
61 static void
62 snd_pmac_burgundy_wcw(pmac_t *chip, unsigned addr, unsigned val)
63 {
64         out_le32(&chip->awacs->codec_ctrl, addr + 0x200c00 + (val & 0xff));
65         snd_pmac_burgundy_busy_wait(chip);
66         out_le32(&chip->awacs->codec_ctrl, addr + 0x200d00 +((val>>8) & 0xff));
67         snd_pmac_burgundy_busy_wait(chip);
68         out_le32(&chip->awacs->codec_ctrl, addr + 0x200e00 +((val>>16) & 0xff));
69         snd_pmac_burgundy_busy_wait(chip);
70         out_le32(&chip->awacs->codec_ctrl, addr + 0x200f00 +((val>>24) & 0xff));
71         snd_pmac_burgundy_busy_wait(chip);
72 }
73
74 static unsigned
75 snd_pmac_burgundy_rcw(pmac_t *chip, unsigned addr)
76 {
77         unsigned val = 0;
78         unsigned long flags;
79
80         spin_lock_irqsave(&chip->reg_lock, flags);
81
82         out_le32(&chip->awacs->codec_ctrl, addr + 0x100000);
83         snd_pmac_burgundy_busy_wait(chip);
84         snd_pmac_burgundy_extend_wait(chip);
85         val += (in_le32(&chip->awacs->codec_stat) >> 4) & 0xff;
86
87         out_le32(&chip->awacs->codec_ctrl, addr + 0x100100);
88         snd_pmac_burgundy_busy_wait(chip);
89         snd_pmac_burgundy_extend_wait(chip);
90         val += ((in_le32(&chip->awacs->codec_stat)>>4) & 0xff) <<8;
91
92         out_le32(&chip->awacs->codec_ctrl, addr + 0x100200);
93         snd_pmac_burgundy_busy_wait(chip);
94         snd_pmac_burgundy_extend_wait(chip);
95         val += ((in_le32(&chip->awacs->codec_stat)>>4) & 0xff) <<16;
96
97         out_le32(&chip->awacs->codec_ctrl, addr + 0x100300);
98         snd_pmac_burgundy_busy_wait(chip);
99         snd_pmac_burgundy_extend_wait(chip);
100         val += ((in_le32(&chip->awacs->codec_stat)>>4) & 0xff) <<24;
101
102         spin_unlock_irqrestore(&chip->reg_lock, flags);
103
104         return val;
105 }
106
107 static void
108 snd_pmac_burgundy_wcb(pmac_t *chip, unsigned int addr, unsigned int val)
109 {
110         out_le32(&chip->awacs->codec_ctrl, addr + 0x300000 + (val & 0xff));
111         snd_pmac_burgundy_busy_wait(chip);
112 }
113
114 static unsigned
115 snd_pmac_burgundy_rcb(pmac_t *chip, unsigned int addr)
116 {
117         unsigned val = 0;
118         unsigned long flags;
119
120         spin_lock_irqsave(&chip->reg_lock, flags);
121
122         out_le32(&chip->awacs->codec_ctrl, addr + 0x100000);
123         snd_pmac_burgundy_busy_wait(chip);
124         snd_pmac_burgundy_extend_wait(chip);
125         val += (in_le32(&chip->awacs->codec_stat) >> 4) & 0xff;
126
127         spin_unlock_irqrestore(&chip->reg_lock, flags);
128
129         return val;
130 }
131
132 /*
133  * Burgundy volume: 0 - 100, stereo
134  */
135 static void
136 snd_pmac_burgundy_write_volume(pmac_t *chip, unsigned int address, long *volume, int shift)
137 {
138         int hardvolume, lvolume, rvolume;
139
140         lvolume = volume[0] ? volume[0] + BURGUNDY_VOLUME_OFFSET : 0;
141         rvolume = volume[1] ? volume[1] + BURGUNDY_VOLUME_OFFSET : 0;
142
143         hardvolume = lvolume + (rvolume << shift);
144         if (shift == 8)
145                 hardvolume |= hardvolume << 16;
146
147         snd_pmac_burgundy_wcw(chip, address, hardvolume);
148 }
149
150 static void
151 snd_pmac_burgundy_read_volume(pmac_t *chip, unsigned int address, long *volume, int shift)
152 {
153         int wvolume;
154
155         wvolume = snd_pmac_burgundy_rcw(chip, address);
156
157         volume[0] = wvolume & 0xff;
158         if (volume[0] >= BURGUNDY_VOLUME_OFFSET)
159                 volume[0] -= BURGUNDY_VOLUME_OFFSET;
160         else
161                 volume[0] = 0;
162         volume[1] = (wvolume >> shift) & 0xff;
163         if (volume[1] >= BURGUNDY_VOLUME_OFFSET)
164                 volume[1] -= BURGUNDY_VOLUME_OFFSET;
165         else
166                 volume[1] = 0;
167 }
168
169
170 /*
171  */
172
173 #define BASE2ADDR(base) ((base) << 12)
174 #define ADDR2BASE(addr) ((addr) >> 12)
175
176 static int snd_pmac_burgundy_info_volume(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
177 {
178         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
179         uinfo->count = 2;
180         uinfo->value.integer.min = 0;
181         uinfo->value.integer.max = 100;
182         return 0;
183 }
184
185 static int snd_pmac_burgundy_get_volume(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
186 {
187         pmac_t *chip = snd_kcontrol_chip(kcontrol);
188         unsigned int addr = BASE2ADDR(kcontrol->private_value & 0xff);
189         int shift = (kcontrol->private_value >> 8) & 0xff;
190         snd_pmac_burgundy_read_volume(chip, addr, ucontrol->value.integer.value, shift);
191         return 0;
192 }
193
194 static int snd_pmac_burgundy_put_volume(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
195 {
196         pmac_t *chip = snd_kcontrol_chip(kcontrol);
197         unsigned int addr = BASE2ADDR(kcontrol->private_value & 0xff);
198         int shift = (kcontrol->private_value >> 8) & 0xff;
199         long nvoices[2];
200
201         snd_pmac_burgundy_write_volume(chip, addr, ucontrol->value.integer.value, shift);
202         snd_pmac_burgundy_read_volume(chip, addr, nvoices, shift);
203         return (nvoices[0] != ucontrol->value.integer.value[0] ||
204                 nvoices[1] != ucontrol->value.integer.value[1]);
205 }
206
207 #define BURGUNDY_VOLUME(xname, xindex, addr, shift) \
208 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex,\
209   .info = snd_pmac_burgundy_info_volume,\
210   .get = snd_pmac_burgundy_get_volume,\
211   .put = snd_pmac_burgundy_put_volume,\
212   .private_value = ((ADDR2BASE(addr) & 0xff) | ((shift) << 8)) }
213
214 /* lineout/speaker */
215
216 static int snd_pmac_burgundy_info_switch_out(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
217 {
218         int stereo = (kcontrol->private_value >> 24) & 1;
219         uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
220         uinfo->count = stereo + 1;
221         uinfo->value.integer.min = 0;
222         uinfo->value.integer.max = 1;
223         return 0;
224 }
225
226 static int snd_pmac_burgundy_get_switch_out(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
227 {
228         pmac_t *chip = snd_kcontrol_chip(kcontrol);
229         int lmask = kcontrol->private_value & 0xff;
230         int rmask = (kcontrol->private_value >> 8) & 0xff;
231         int stereo = (kcontrol->private_value >> 24) & 1;
232         int val = snd_pmac_burgundy_rcb(chip, MASK_ADDR_BURGUNDY_MORE_OUTPUTENABLES);
233         ucontrol->value.integer.value[0] = (val & lmask) ? 1 : 0;
234         if (stereo)
235                 ucontrol->value.integer.value[1] = (val & rmask) ? 1 : 0;
236         return 0;
237 }
238
239 static int snd_pmac_burgundy_put_switch_out(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
240 {
241         pmac_t *chip = snd_kcontrol_chip(kcontrol);
242         int lmask = kcontrol->private_value & 0xff;
243         int rmask = (kcontrol->private_value >> 8) & 0xff;
244         int stereo = (kcontrol->private_value >> 24) & 1;
245         int val, oval;
246         oval = snd_pmac_burgundy_rcb(chip, MASK_ADDR_BURGUNDY_MORE_OUTPUTENABLES);
247         val = oval & ~(lmask | rmask);
248         if (ucontrol->value.integer.value[0])
249                 val |= lmask;
250         if (stereo && ucontrol->value.integer.value[1])
251                 val |= rmask;
252         snd_pmac_burgundy_wcb(chip, MASK_ADDR_BURGUNDY_MORE_OUTPUTENABLES, val);
253         return val != oval;
254 }
255
256 #define BURGUNDY_OUTPUT_SWITCH(xname, xindex, lmask, rmask, stereo) \
257 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex,\
258   .info = snd_pmac_burgundy_info_switch_out,\
259   .get = snd_pmac_burgundy_get_switch_out,\
260   .put = snd_pmac_burgundy_put_switch_out,\
261   .private_value = ((lmask) | ((rmask) << 8) | ((stereo) << 24)) }
262
263 /* line/speaker output volume */
264 static int snd_pmac_burgundy_info_volume_out(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
265 {
266         int stereo = (kcontrol->private_value >> 24) & 1;
267         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
268         uinfo->count = stereo + 1;
269         uinfo->value.integer.min = 0;
270         uinfo->value.integer.max = 15;
271         return 0;
272 }
273
274 static int snd_pmac_burgundy_get_volume_out(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
275 {
276         pmac_t *chip = snd_kcontrol_chip(kcontrol);
277         unsigned int addr = BASE2ADDR(kcontrol->private_value & 0xff);
278         int stereo = (kcontrol->private_value >> 24) & 1;
279         int oval;
280
281         oval = ~snd_pmac_burgundy_rcb(chip, addr) & 0xff;
282         ucontrol->value.integer.value[0] = oval & 0xf;
283         if (stereo)
284                 ucontrol->value.integer.value[1] = (oval >> 4) & 0xf;
285         return 0;
286 }
287
288 static int snd_pmac_burgundy_put_volume_out(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
289 {
290         pmac_t *chip = snd_kcontrol_chip(kcontrol);
291         unsigned int addr = BASE2ADDR(kcontrol->private_value & 0xff);
292         int stereo = (kcontrol->private_value >> 24) & 1;
293         int oval, val;
294
295         oval = ~snd_pmac_burgundy_rcb(chip, addr) & 0xff;
296         val = ucontrol->value.integer.value[0];
297         if (stereo)
298                 val |= ucontrol->value.integer.value[1] << 4;
299         else
300                 val |= ucontrol->value.integer.value[0] << 4;
301         val = ~val & 0xff;
302         snd_pmac_burgundy_wcb(chip, addr, val);
303         return val != oval;
304 }
305
306 #define BURGUNDY_OUTPUT_VOLUME(xname, xindex, addr, stereo) \
307 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex,\
308   .info = snd_pmac_burgundy_info_volume_out,\
309   .get = snd_pmac_burgundy_get_volume_out,\
310   .put = snd_pmac_burgundy_put_volume_out,\
311   .private_value = (ADDR2BASE(addr) | ((stereo) << 24)) }
312
313 static snd_kcontrol_new_t snd_pmac_burgundy_mixers[] __initdata = {
314         BURGUNDY_VOLUME("Master Playback Volume", 0, MASK_ADDR_BURGUNDY_MASTER_VOLUME, 8),
315         BURGUNDY_VOLUME("Line Playback Volume", 0, MASK_ADDR_BURGUNDY_VOLLINE, 16),
316         BURGUNDY_VOLUME("CD Playback Volume", 0, MASK_ADDR_BURGUNDY_VOLCD, 16),
317         BURGUNDY_VOLUME("Mic Playback Volume", 0, MASK_ADDR_BURGUNDY_VOLMIC, 16),
318         BURGUNDY_OUTPUT_VOLUME("PC Speaker Playback Volume", 0, MASK_ADDR_BURGUNDY_ATTENHP, 0),
319         /*BURGUNDY_OUTPUT_VOLUME("PCM Playback Volume", 0, MASK_ADDR_BURGUNDY_ATTENLINEOUT, 1),*/
320         BURGUNDY_OUTPUT_VOLUME("Headphone Playback Volume", 0, MASK_ADDR_BURGUNDY_ATTENSPEAKER, 1),
321 };      
322 static snd_kcontrol_new_t snd_pmac_burgundy_master_sw __initdata = 
323 BURGUNDY_OUTPUT_SWITCH("Headphone Playback Switch", 0, BURGUNDY_OUTPUT_LEFT, BURGUNDY_OUTPUT_RIGHT, 1);
324 static snd_kcontrol_new_t snd_pmac_burgundy_speaker_sw __initdata = 
325 BURGUNDY_OUTPUT_SWITCH("PC Speaker Playback Switch", 0, BURGUNDY_OUTPUT_INTERN, 0, 0);
326
327 #define num_controls(ary) (sizeof(ary) / sizeof(snd_kcontrol_new_t))
328
329
330 #ifdef PMAC_SUPPORT_AUTOMUTE
331 /*
332  * auto-mute stuffs
333  */
334 static int snd_pmac_burgundy_detect_headphone(pmac_t *chip)
335 {
336         return (in_le32(&chip->awacs->codec_stat) & chip->hp_stat_mask) ? 1 : 0;
337 }
338
339 static void snd_pmac_burgundy_update_automute(pmac_t *chip, int do_notify)
340 {
341         if (chip->auto_mute) {
342                 int reg, oreg;
343                 reg = oreg = snd_pmac_burgundy_rcb(chip, MASK_ADDR_BURGUNDY_MORE_OUTPUTENABLES);
344                 reg &= ~(BURGUNDY_OUTPUT_LEFT | BURGUNDY_OUTPUT_RIGHT | BURGUNDY_OUTPUT_INTERN);
345                 if (snd_pmac_burgundy_detect_headphone(chip))
346                         reg |= BURGUNDY_OUTPUT_LEFT | BURGUNDY_OUTPUT_RIGHT;
347                 else
348                         reg |= BURGUNDY_OUTPUT_INTERN;
349                 if (do_notify && reg == oreg)
350                         return;
351                 snd_pmac_burgundy_wcb(chip, MASK_ADDR_BURGUNDY_MORE_OUTPUTENABLES, reg);
352                 if (do_notify) {
353                         snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
354                                        &chip->master_sw_ctl->id);
355                         snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
356                                        &chip->speaker_sw_ctl->id);
357                         snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
358                                        &chip->hp_detect_ctl->id);
359                 }
360         }
361 }
362 #endif /* PMAC_SUPPORT_AUTOMUTE */
363
364
365 /*
366  * initialize burgundy
367  */
368 int __init snd_pmac_burgundy_init(pmac_t *chip)
369 {
370         int i, err;
371
372         /* Checks to see the chip is alive and kicking */
373         if ((in_le32(&chip->awacs->codec_ctrl) & MASK_ERRCODE) == 0xf0000) {
374                 printk(KERN_WARNING "pmac burgundy: disabled by MacOS :-(\n");
375                 return 1;
376         }
377
378         snd_pmac_burgundy_wcb(chip, MASK_ADDR_BURGUNDY_OUTPUTENABLES,
379                            DEF_BURGUNDY_OUTPUTENABLES);
380         snd_pmac_burgundy_wcb(chip, MASK_ADDR_BURGUNDY_MORE_OUTPUTENABLES,
381                            DEF_BURGUNDY_MORE_OUTPUTENABLES);
382         snd_pmac_burgundy_wcw(chip, MASK_ADDR_BURGUNDY_OUTPUTSELECTS,
383                            DEF_BURGUNDY_OUTPUTSELECTS);
384
385         snd_pmac_burgundy_wcb(chip, MASK_ADDR_BURGUNDY_INPSEL21,
386                            DEF_BURGUNDY_INPSEL21);
387         snd_pmac_burgundy_wcb(chip, MASK_ADDR_BURGUNDY_INPSEL3,
388                            DEF_BURGUNDY_INPSEL3);
389         snd_pmac_burgundy_wcb(chip, MASK_ADDR_BURGUNDY_GAINCD,
390                            DEF_BURGUNDY_GAINCD);
391         snd_pmac_burgundy_wcb(chip, MASK_ADDR_BURGUNDY_GAINLINE,
392                            DEF_BURGUNDY_GAINLINE);
393         snd_pmac_burgundy_wcb(chip, MASK_ADDR_BURGUNDY_GAINMIC,
394                            DEF_BURGUNDY_GAINMIC);
395         snd_pmac_burgundy_wcb(chip, MASK_ADDR_BURGUNDY_GAINMODEM,
396                            DEF_BURGUNDY_GAINMODEM);
397
398         snd_pmac_burgundy_wcb(chip, MASK_ADDR_BURGUNDY_ATTENSPEAKER,
399                            DEF_BURGUNDY_ATTENSPEAKER);
400         snd_pmac_burgundy_wcb(chip, MASK_ADDR_BURGUNDY_ATTENLINEOUT,
401                            DEF_BURGUNDY_ATTENLINEOUT);
402         snd_pmac_burgundy_wcb(chip, MASK_ADDR_BURGUNDY_ATTENHP,
403                            DEF_BURGUNDY_ATTENHP);
404
405         snd_pmac_burgundy_wcw(chip, MASK_ADDR_BURGUNDY_MASTER_VOLUME,
406                            DEF_BURGUNDY_MASTER_VOLUME);
407         snd_pmac_burgundy_wcw(chip, MASK_ADDR_BURGUNDY_VOLCD,
408                            DEF_BURGUNDY_VOLCD);
409         snd_pmac_burgundy_wcw(chip, MASK_ADDR_BURGUNDY_VOLLINE,
410                            DEF_BURGUNDY_VOLLINE);
411         snd_pmac_burgundy_wcw(chip, MASK_ADDR_BURGUNDY_VOLMIC,
412                            DEF_BURGUNDY_VOLMIC);
413
414         if (chip->hp_stat_mask == 0)
415                 /* set headphone-jack detection bit */
416                 chip->hp_stat_mask = 0x04;
417
418         /*
419          * build burgundy mixers
420          */
421         strcpy(chip->card->mixername, "PowerMac Burgundy");
422
423         for (i = 0; i < num_controls(snd_pmac_burgundy_mixers); i++) {
424                 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_pmac_burgundy_mixers[i], chip))) < 0)
425                         return err;
426         }
427         chip->master_sw_ctl = snd_ctl_new1(&snd_pmac_burgundy_master_sw, chip);
428         if ((err = snd_ctl_add(chip->card, chip->master_sw_ctl)) < 0)
429                 return err;
430         chip->speaker_sw_ctl = snd_ctl_new1(&snd_pmac_burgundy_speaker_sw, chip);
431         if ((err = snd_ctl_add(chip->card, chip->speaker_sw_ctl)) < 0)
432                 return err;
433 #ifdef PMAC_SUPPORT_AUTOMUTE
434         if ((err = snd_pmac_add_automute(chip)) < 0)
435                 return err;
436
437         chip->detect_headphone = snd_pmac_burgundy_detect_headphone;
438         chip->update_automute = snd_pmac_burgundy_update_automute;
439         snd_pmac_burgundy_update_automute(chip, 0); /* update the status only */
440 #endif
441
442         return 0;
443 }