2 * PMac Burgundy 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
22 #include <sound/driver.h>
24 #include <linux/init.h>
25 #include <linux/slab.h>
26 #include <linux/delay.h>
27 #include <sound/core.h>
32 /* Waits for busy flag to clear */
34 snd_pmac_burgundy_busy_wait(struct snd_pmac
*chip
)
37 while ((in_le32(&chip
->awacs
->codec_ctrl
) & MASK_NEWECMD
) && timeout
--)
40 printk(KERN_DEBUG
"burgundy_busy_wait: timeout\n");
44 snd_pmac_burgundy_extend_wait(struct snd_pmac
*chip
)
48 while (!(in_le32(&chip
->awacs
->codec_stat
) & MASK_EXTEND
) && timeout
--)
51 printk(KERN_DEBUG
"burgundy_extend_wait: timeout #1\n");
53 while ((in_le32(&chip
->awacs
->codec_stat
) & MASK_EXTEND
) && timeout
--)
56 printk(KERN_DEBUG
"burgundy_extend_wait: timeout #2\n");
60 snd_pmac_burgundy_wcw(struct snd_pmac
*chip
, unsigned addr
, unsigned val
)
62 out_le32(&chip
->awacs
->codec_ctrl
, addr
+ 0x200c00 + (val
& 0xff));
63 snd_pmac_burgundy_busy_wait(chip
);
64 out_le32(&chip
->awacs
->codec_ctrl
, addr
+ 0x200d00 +((val
>>8) & 0xff));
65 snd_pmac_burgundy_busy_wait(chip
);
66 out_le32(&chip
->awacs
->codec_ctrl
, addr
+ 0x200e00 +((val
>>16) & 0xff));
67 snd_pmac_burgundy_busy_wait(chip
);
68 out_le32(&chip
->awacs
->codec_ctrl
, addr
+ 0x200f00 +((val
>>24) & 0xff));
69 snd_pmac_burgundy_busy_wait(chip
);
73 snd_pmac_burgundy_rcw(struct snd_pmac
*chip
, unsigned addr
)
78 spin_lock_irqsave(&chip
->reg_lock
, flags
);
80 out_le32(&chip
->awacs
->codec_ctrl
, addr
+ 0x100000);
81 snd_pmac_burgundy_busy_wait(chip
);
82 snd_pmac_burgundy_extend_wait(chip
);
83 val
+= (in_le32(&chip
->awacs
->codec_stat
) >> 4) & 0xff;
85 out_le32(&chip
->awacs
->codec_ctrl
, addr
+ 0x100100);
86 snd_pmac_burgundy_busy_wait(chip
);
87 snd_pmac_burgundy_extend_wait(chip
);
88 val
+= ((in_le32(&chip
->awacs
->codec_stat
)>>4) & 0xff) <<8;
90 out_le32(&chip
->awacs
->codec_ctrl
, addr
+ 0x100200);
91 snd_pmac_burgundy_busy_wait(chip
);
92 snd_pmac_burgundy_extend_wait(chip
);
93 val
+= ((in_le32(&chip
->awacs
->codec_stat
)>>4) & 0xff) <<16;
95 out_le32(&chip
->awacs
->codec_ctrl
, addr
+ 0x100300);
96 snd_pmac_burgundy_busy_wait(chip
);
97 snd_pmac_burgundy_extend_wait(chip
);
98 val
+= ((in_le32(&chip
->awacs
->codec_stat
)>>4) & 0xff) <<24;
100 spin_unlock_irqrestore(&chip
->reg_lock
, flags
);
106 snd_pmac_burgundy_wcb(struct snd_pmac
*chip
, unsigned int addr
, unsigned int val
)
108 out_le32(&chip
->awacs
->codec_ctrl
, addr
+ 0x300000 + (val
& 0xff));
109 snd_pmac_burgundy_busy_wait(chip
);
113 snd_pmac_burgundy_rcb(struct snd_pmac
*chip
, unsigned int addr
)
118 spin_lock_irqsave(&chip
->reg_lock
, flags
);
120 out_le32(&chip
->awacs
->codec_ctrl
, addr
+ 0x100000);
121 snd_pmac_burgundy_busy_wait(chip
);
122 snd_pmac_burgundy_extend_wait(chip
);
123 val
+= (in_le32(&chip
->awacs
->codec_stat
) >> 4) & 0xff;
125 spin_unlock_irqrestore(&chip
->reg_lock
, flags
);
131 * Burgundy volume: 0 - 100, stereo
134 snd_pmac_burgundy_write_volume(struct snd_pmac
*chip
, unsigned int address
,
135 long *volume
, int shift
)
137 int hardvolume
, lvolume
, rvolume
;
139 lvolume
= volume
[0] ? volume
[0] + BURGUNDY_VOLUME_OFFSET
: 0;
140 rvolume
= volume
[1] ? volume
[1] + BURGUNDY_VOLUME_OFFSET
: 0;
142 hardvolume
= lvolume
+ (rvolume
<< shift
);
144 hardvolume
|= hardvolume
<< 16;
146 snd_pmac_burgundy_wcw(chip
, address
, hardvolume
);
150 snd_pmac_burgundy_read_volume(struct snd_pmac
*chip
, unsigned int address
,
151 long *volume
, int shift
)
155 wvolume
= snd_pmac_burgundy_rcw(chip
, address
);
157 volume
[0] = wvolume
& 0xff;
158 if (volume
[0] >= BURGUNDY_VOLUME_OFFSET
)
159 volume
[0] -= BURGUNDY_VOLUME_OFFSET
;
162 volume
[1] = (wvolume
>> shift
) & 0xff;
163 if (volume
[1] >= BURGUNDY_VOLUME_OFFSET
)
164 volume
[1] -= BURGUNDY_VOLUME_OFFSET
;
173 #define BASE2ADDR(base) ((base) << 12)
174 #define ADDR2BASE(addr) ((addr) >> 12)
176 static int snd_pmac_burgundy_info_volume(struct snd_kcontrol
*kcontrol
,
177 struct snd_ctl_elem_info
*uinfo
)
179 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
181 uinfo
->value
.integer
.min
= 0;
182 uinfo
->value
.integer
.max
= 100;
186 static int snd_pmac_burgundy_get_volume(struct snd_kcontrol
*kcontrol
,
187 struct snd_ctl_elem_value
*ucontrol
)
189 struct snd_pmac
*chip
= snd_kcontrol_chip(kcontrol
);
190 unsigned int addr
= BASE2ADDR(kcontrol
->private_value
& 0xff);
191 int shift
= (kcontrol
->private_value
>> 8) & 0xff;
192 snd_pmac_burgundy_read_volume(chip
, addr
, ucontrol
->value
.integer
.value
,
197 static int snd_pmac_burgundy_put_volume(struct snd_kcontrol
*kcontrol
,
198 struct snd_ctl_elem_value
*ucontrol
)
200 struct snd_pmac
*chip
= snd_kcontrol_chip(kcontrol
);
201 unsigned int addr
= BASE2ADDR(kcontrol
->private_value
& 0xff);
202 int shift
= (kcontrol
->private_value
>> 8) & 0xff;
205 snd_pmac_burgundy_write_volume(chip
, addr
, ucontrol
->value
.integer
.value
,
207 snd_pmac_burgundy_read_volume(chip
, addr
, nvoices
, shift
);
208 return (nvoices
[0] != ucontrol
->value
.integer
.value
[0] ||
209 nvoices
[1] != ucontrol
->value
.integer
.value
[1]);
212 #define BURGUNDY_VOLUME(xname, xindex, addr, shift) \
213 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex,\
214 .info = snd_pmac_burgundy_info_volume,\
215 .get = snd_pmac_burgundy_get_volume,\
216 .put = snd_pmac_burgundy_put_volume,\
217 .private_value = ((ADDR2BASE(addr) & 0xff) | ((shift) << 8)) }
219 /* lineout/speaker */
221 static int snd_pmac_burgundy_info_switch_out(struct snd_kcontrol
*kcontrol
,
222 struct snd_ctl_elem_info
*uinfo
)
224 int stereo
= (kcontrol
->private_value
>> 24) & 1;
225 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_BOOLEAN
;
226 uinfo
->count
= stereo
+ 1;
227 uinfo
->value
.integer
.min
= 0;
228 uinfo
->value
.integer
.max
= 1;
232 static int snd_pmac_burgundy_get_switch_out(struct snd_kcontrol
*kcontrol
,
233 struct snd_ctl_elem_value
*ucontrol
)
235 struct snd_pmac
*chip
= snd_kcontrol_chip(kcontrol
);
236 int lmask
= kcontrol
->private_value
& 0xff;
237 int rmask
= (kcontrol
->private_value
>> 8) & 0xff;
238 int stereo
= (kcontrol
->private_value
>> 24) & 1;
239 int val
= snd_pmac_burgundy_rcb(chip
, MASK_ADDR_BURGUNDY_MORE_OUTPUTENABLES
);
240 ucontrol
->value
.integer
.value
[0] = (val
& lmask
) ? 1 : 0;
242 ucontrol
->value
.integer
.value
[1] = (val
& rmask
) ? 1 : 0;
246 static int snd_pmac_burgundy_put_switch_out(struct snd_kcontrol
*kcontrol
,
247 struct snd_ctl_elem_value
*ucontrol
)
249 struct snd_pmac
*chip
= snd_kcontrol_chip(kcontrol
);
250 int lmask
= kcontrol
->private_value
& 0xff;
251 int rmask
= (kcontrol
->private_value
>> 8) & 0xff;
252 int stereo
= (kcontrol
->private_value
>> 24) & 1;
254 oval
= snd_pmac_burgundy_rcb(chip
, MASK_ADDR_BURGUNDY_MORE_OUTPUTENABLES
);
255 val
= oval
& ~(lmask
| rmask
);
256 if (ucontrol
->value
.integer
.value
[0])
258 if (stereo
&& ucontrol
->value
.integer
.value
[1])
260 snd_pmac_burgundy_wcb(chip
, MASK_ADDR_BURGUNDY_MORE_OUTPUTENABLES
, val
);
264 #define BURGUNDY_OUTPUT_SWITCH(xname, xindex, lmask, rmask, stereo) \
265 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex,\
266 .info = snd_pmac_burgundy_info_switch_out,\
267 .get = snd_pmac_burgundy_get_switch_out,\
268 .put = snd_pmac_burgundy_put_switch_out,\
269 .private_value = ((lmask) | ((rmask) << 8) | ((stereo) << 24)) }
271 /* line/speaker output volume */
272 static int snd_pmac_burgundy_info_volume_out(struct snd_kcontrol
*kcontrol
,
273 struct snd_ctl_elem_info
*uinfo
)
275 int stereo
= (kcontrol
->private_value
>> 24) & 1;
276 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
277 uinfo
->count
= stereo
+ 1;
278 uinfo
->value
.integer
.min
= 0;
279 uinfo
->value
.integer
.max
= 15;
283 static int snd_pmac_burgundy_get_volume_out(struct snd_kcontrol
*kcontrol
,
284 struct snd_ctl_elem_value
*ucontrol
)
286 struct snd_pmac
*chip
= snd_kcontrol_chip(kcontrol
);
287 unsigned int addr
= BASE2ADDR(kcontrol
->private_value
& 0xff);
288 int stereo
= (kcontrol
->private_value
>> 24) & 1;
291 oval
= ~snd_pmac_burgundy_rcb(chip
, addr
) & 0xff;
292 ucontrol
->value
.integer
.value
[0] = oval
& 0xf;
294 ucontrol
->value
.integer
.value
[1] = (oval
>> 4) & 0xf;
298 static int snd_pmac_burgundy_put_volume_out(struct snd_kcontrol
*kcontrol
,
299 struct snd_ctl_elem_value
*ucontrol
)
301 struct snd_pmac
*chip
= snd_kcontrol_chip(kcontrol
);
302 unsigned int addr
= BASE2ADDR(kcontrol
->private_value
& 0xff);
303 int stereo
= (kcontrol
->private_value
>> 24) & 1;
306 oval
= ~snd_pmac_burgundy_rcb(chip
, addr
) & 0xff;
307 val
= ucontrol
->value
.integer
.value
[0];
309 val
|= ucontrol
->value
.integer
.value
[1] << 4;
311 val
|= ucontrol
->value
.integer
.value
[0] << 4;
313 snd_pmac_burgundy_wcb(chip
, addr
, val
);
317 #define BURGUNDY_OUTPUT_VOLUME(xname, xindex, addr, stereo) \
318 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex,\
319 .info = snd_pmac_burgundy_info_volume_out,\
320 .get = snd_pmac_burgundy_get_volume_out,\
321 .put = snd_pmac_burgundy_put_volume_out,\
322 .private_value = (ADDR2BASE(addr) | ((stereo) << 24)) }
324 static struct snd_kcontrol_new snd_pmac_burgundy_mixers
[] __initdata
= {
325 BURGUNDY_VOLUME("Master Playback Volume", 0, MASK_ADDR_BURGUNDY_MASTER_VOLUME
, 8),
326 BURGUNDY_VOLUME("Line Playback Volume", 0, MASK_ADDR_BURGUNDY_VOLLINE
, 16),
327 BURGUNDY_VOLUME("CD Playback Volume", 0, MASK_ADDR_BURGUNDY_VOLCD
, 16),
328 BURGUNDY_VOLUME("Mic Playback Volume", 0, MASK_ADDR_BURGUNDY_VOLMIC
, 16),
329 BURGUNDY_OUTPUT_VOLUME("PC Speaker Playback Volume", 0, MASK_ADDR_BURGUNDY_ATTENHP
, 0),
330 /*BURGUNDY_OUTPUT_VOLUME("PCM Playback Volume", 0, MASK_ADDR_BURGUNDY_ATTENLINEOUT, 1),*/
331 BURGUNDY_OUTPUT_VOLUME("Headphone Playback Volume", 0, MASK_ADDR_BURGUNDY_ATTENSPEAKER
, 1),
333 static struct snd_kcontrol_new snd_pmac_burgundy_master_sw __initdata
=
334 BURGUNDY_OUTPUT_SWITCH("Headphone Playback Switch", 0, BURGUNDY_OUTPUT_LEFT
, BURGUNDY_OUTPUT_RIGHT
, 1);
335 static struct snd_kcontrol_new snd_pmac_burgundy_speaker_sw __initdata
=
336 BURGUNDY_OUTPUT_SWITCH("PC Speaker Playback Switch", 0, BURGUNDY_OUTPUT_INTERN
, 0, 0);
339 #ifdef PMAC_SUPPORT_AUTOMUTE
343 static int snd_pmac_burgundy_detect_headphone(struct snd_pmac
*chip
)
345 return (in_le32(&chip
->awacs
->codec_stat
) & chip
->hp_stat_mask
) ? 1 : 0;
348 static void snd_pmac_burgundy_update_automute(struct snd_pmac
*chip
, int do_notify
)
350 if (chip
->auto_mute
) {
352 reg
= oreg
= snd_pmac_burgundy_rcb(chip
, MASK_ADDR_BURGUNDY_MORE_OUTPUTENABLES
);
353 reg
&= ~(BURGUNDY_OUTPUT_LEFT
| BURGUNDY_OUTPUT_RIGHT
| BURGUNDY_OUTPUT_INTERN
);
354 if (snd_pmac_burgundy_detect_headphone(chip
))
355 reg
|= BURGUNDY_OUTPUT_LEFT
| BURGUNDY_OUTPUT_RIGHT
;
357 reg
|= BURGUNDY_OUTPUT_INTERN
;
358 if (do_notify
&& reg
== oreg
)
360 snd_pmac_burgundy_wcb(chip
, MASK_ADDR_BURGUNDY_MORE_OUTPUTENABLES
, reg
);
362 snd_ctl_notify(chip
->card
, SNDRV_CTL_EVENT_MASK_VALUE
,
363 &chip
->master_sw_ctl
->id
);
364 snd_ctl_notify(chip
->card
, SNDRV_CTL_EVENT_MASK_VALUE
,
365 &chip
->speaker_sw_ctl
->id
);
366 snd_ctl_notify(chip
->card
, SNDRV_CTL_EVENT_MASK_VALUE
,
367 &chip
->hp_detect_ctl
->id
);
371 #endif /* PMAC_SUPPORT_AUTOMUTE */
375 * initialize burgundy
377 int __init
snd_pmac_burgundy_init(struct snd_pmac
*chip
)
381 /* Checks to see the chip is alive and kicking */
382 if ((in_le32(&chip
->awacs
->codec_ctrl
) & MASK_ERRCODE
) == 0xf0000) {
383 printk(KERN_WARNING
"pmac burgundy: disabled by MacOS :-(\n");
387 snd_pmac_burgundy_wcb(chip
, MASK_ADDR_BURGUNDY_OUTPUTENABLES
,
388 DEF_BURGUNDY_OUTPUTENABLES
);
389 snd_pmac_burgundy_wcb(chip
, MASK_ADDR_BURGUNDY_MORE_OUTPUTENABLES
,
390 DEF_BURGUNDY_MORE_OUTPUTENABLES
);
391 snd_pmac_burgundy_wcw(chip
, MASK_ADDR_BURGUNDY_OUTPUTSELECTS
,
392 DEF_BURGUNDY_OUTPUTSELECTS
);
394 snd_pmac_burgundy_wcb(chip
, MASK_ADDR_BURGUNDY_INPSEL21
,
395 DEF_BURGUNDY_INPSEL21
);
396 snd_pmac_burgundy_wcb(chip
, MASK_ADDR_BURGUNDY_INPSEL3
,
397 DEF_BURGUNDY_INPSEL3
);
398 snd_pmac_burgundy_wcb(chip
, MASK_ADDR_BURGUNDY_GAINCD
,
399 DEF_BURGUNDY_GAINCD
);
400 snd_pmac_burgundy_wcb(chip
, MASK_ADDR_BURGUNDY_GAINLINE
,
401 DEF_BURGUNDY_GAINLINE
);
402 snd_pmac_burgundy_wcb(chip
, MASK_ADDR_BURGUNDY_GAINMIC
,
403 DEF_BURGUNDY_GAINMIC
);
404 snd_pmac_burgundy_wcb(chip
, MASK_ADDR_BURGUNDY_GAINMODEM
,
405 DEF_BURGUNDY_GAINMODEM
);
407 snd_pmac_burgundy_wcb(chip
, MASK_ADDR_BURGUNDY_ATTENSPEAKER
,
408 DEF_BURGUNDY_ATTENSPEAKER
);
409 snd_pmac_burgundy_wcb(chip
, MASK_ADDR_BURGUNDY_ATTENLINEOUT
,
410 DEF_BURGUNDY_ATTENLINEOUT
);
411 snd_pmac_burgundy_wcb(chip
, MASK_ADDR_BURGUNDY_ATTENHP
,
412 DEF_BURGUNDY_ATTENHP
);
414 snd_pmac_burgundy_wcw(chip
, MASK_ADDR_BURGUNDY_MASTER_VOLUME
,
415 DEF_BURGUNDY_MASTER_VOLUME
);
416 snd_pmac_burgundy_wcw(chip
, MASK_ADDR_BURGUNDY_VOLCD
,
418 snd_pmac_burgundy_wcw(chip
, MASK_ADDR_BURGUNDY_VOLLINE
,
419 DEF_BURGUNDY_VOLLINE
);
420 snd_pmac_burgundy_wcw(chip
, MASK_ADDR_BURGUNDY_VOLMIC
,
421 DEF_BURGUNDY_VOLMIC
);
423 if (chip
->hp_stat_mask
== 0)
424 /* set headphone-jack detection bit */
425 chip
->hp_stat_mask
= 0x04;
428 * build burgundy mixers
430 strcpy(chip
->card
->mixername
, "PowerMac Burgundy");
432 for (i
= 0; i
< ARRAY_SIZE(snd_pmac_burgundy_mixers
); i
++) {
433 if ((err
= snd_ctl_add(chip
->card
, snd_ctl_new1(&snd_pmac_burgundy_mixers
[i
], chip
))) < 0)
436 chip
->master_sw_ctl
= snd_ctl_new1(&snd_pmac_burgundy_master_sw
, chip
);
437 if ((err
= snd_ctl_add(chip
->card
, chip
->master_sw_ctl
)) < 0)
439 chip
->speaker_sw_ctl
= snd_ctl_new1(&snd_pmac_burgundy_speaker_sw
, chip
);
440 if ((err
= snd_ctl_add(chip
->card
, chip
->speaker_sw_ctl
)) < 0)
442 #ifdef PMAC_SUPPORT_AUTOMUTE
443 if ((err
= snd_pmac_add_automute(chip
)) < 0)
446 chip
->detect_headphone
= snd_pmac_burgundy_detect_headphone
;
447 chip
->update_automute
= snd_pmac_burgundy_update_automute
;
448 snd_pmac_burgundy_update_automute(chip
, 0); /* update the status only */