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