blob: 106c48225bbadd2978a10b325fe80badcc7f086d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <asm/io.h>
24#include <asm/nvram.h>
25#include <linux/init.h>
26#include <linux/delay.h>
27#include <linux/slab.h>
28#include <sound/core.h>
29#include "pmac.h"
30
31
32#ifdef CONFIG_ADB_CUDA
33#define PMAC_AMP_AVAIL
34#endif
35
36#ifdef PMAC_AMP_AVAIL
Takashi Iwai65b29f52005-11-17 15:09:46 +010037struct awacs_amp {
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 unsigned char amp_master;
39 unsigned char amp_vol[2][2];
40 unsigned char amp_tone[2];
Takashi Iwai65b29f52005-11-17 15:09:46 +010041};
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43#define CHECK_CUDA_AMP() (sys_ctrler == SYS_CTRLER_CUDA)
44
45#endif /* PMAC_AMP_AVAIL */
46
47
Takashi Iwai65b29f52005-11-17 15:09:46 +010048static void snd_pmac_screamer_wait(struct snd_pmac *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049{
50 long timeout = 2000;
51 while (!(in_le32(&chip->awacs->codec_stat) & MASK_VALID)) {
52 mdelay(1);
53 if (! --timeout) {
54 snd_printd("snd_pmac_screamer_wait timeout\n");
55 break;
56 }
57 }
58}
59
60/*
61 * write AWACS register
62 */
63static void
Takashi Iwai65b29f52005-11-17 15:09:46 +010064snd_pmac_awacs_write(struct snd_pmac *chip, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -070065{
66 long timeout = 5000000;
67
68 if (chip->model == PMAC_SCREAMER)
69 snd_pmac_screamer_wait(chip);
70 out_le32(&chip->awacs->codec_ctrl, val | (chip->subframe << 22));
71 while (in_le32(&chip->awacs->codec_ctrl) & MASK_NEWECMD) {
72 if (! --timeout) {
73 snd_printd("snd_pmac_awacs_write timeout\n");
74 break;
75 }
76 }
77}
78
79static void
Takashi Iwai65b29f52005-11-17 15:09:46 +010080snd_pmac_awacs_write_reg(struct snd_pmac *chip, int reg, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -070081{
82 snd_pmac_awacs_write(chip, val | (reg << 12));
83 chip->awacs_reg[reg] = val;
84}
85
86static void
Takashi Iwai65b29f52005-11-17 15:09:46 +010087snd_pmac_awacs_write_noreg(struct snd_pmac *chip, int reg, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088{
89 snd_pmac_awacs_write(chip, val | (reg << 12));
90}
91
Benjamin Herrenschmidt8c870932005-06-27 14:36:34 -070092#ifdef CONFIG_PM
Linus Torvalds1da177e2005-04-16 15:20:36 -070093/* Recalibrate chip */
Takashi Iwai65b29f52005-11-17 15:09:46 +010094static void screamer_recalibrate(struct snd_pmac *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095{
96 if (chip->model != PMAC_SCREAMER)
97 return;
98
99 /* Sorry for the horrible delays... I hope to get that improved
100 * by making the whole PM process asynchronous in a future version
101 */
102 snd_pmac_awacs_write_noreg(chip, 1, chip->awacs_reg[1]);
103 if (chip->manufacturer == 0x1)
104 /* delay for broken crystal part */
Nishanth Aravamudan989a0b22005-07-09 10:53:24 +0200105 msleep(750);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 snd_pmac_awacs_write_noreg(chip, 1,
Takashi Iwai65b29f52005-11-17 15:09:46 +0100107 chip->awacs_reg[1] | MASK_RECALIBRATE |
108 MASK_CMUTE | MASK_AMUTE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 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 */
Takashi Iwai65b29f52005-11-17 15:09:46 +0100121static void snd_pmac_awacs_set_format(struct snd_pmac *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122{
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 */
Takashi Iwai65b29f52005-11-17 15:09:46 +0100135static int snd_pmac_awacs_info_volume(struct snd_kcontrol *kcontrol,
136 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137{
138 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
139 uinfo->count = 2;
140 uinfo->value.integer.min = 0;
141 uinfo->value.integer.max = 15;
142 return 0;
143}
Risto Suominen7ae44cf2008-04-16 19:39:27 +0200144
Takashi Iwai65b29f52005-11-17 15:09:46 +0100145static int snd_pmac_awacs_get_volume(struct snd_kcontrol *kcontrol,
146 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147{
Takashi Iwai65b29f52005-11-17 15:09:46 +0100148 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 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
Takashi Iwai65b29f52005-11-17 15:09:46 +0100168static int snd_pmac_awacs_put_volume(struct snd_kcontrol *kcontrol,
169 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170{
Takashi Iwai65b29f52005-11-17 15:09:46 +0100171 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 int reg = kcontrol->private_value & 0xff;
173 int lshift = (kcontrol->private_value >> 8) & 0xff;
174 int inverted = (kcontrol->private_value >> 16) & 1;
175 int val, oldval;
176 unsigned long flags;
Takashi Iwaid4079ac2007-11-15 16:14:12 +0100177 unsigned int vol[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
179 vol[0] = ucontrol->value.integer.value[0];
180 vol[1] = ucontrol->value.integer.value[1];
Takashi Iwaid4079ac2007-11-15 16:14:12 +0100181 if (vol[0] > 0x0f || vol[1] > 0x0f)
182 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 if (inverted) {
184 vol[0] = 0x0f - vol[0];
185 vol[1] = 0x0f - vol[1];
186 }
187 vol[0] &= 0x0f;
188 vol[1] &= 0x0f;
189 spin_lock_irqsave(&chip->reg_lock, flags);
190 oldval = chip->awacs_reg[reg];
191 val = oldval & ~(0xf | (0xf << lshift));
192 val |= vol[0] << lshift;
193 val |= vol[1];
194 if (oldval != val)
195 snd_pmac_awacs_write_reg(chip, reg, val);
196 spin_unlock_irqrestore(&chip->reg_lock, flags);
197 return oldval != reg;
198}
199
200
201#define AWACS_VOLUME(xname, xreg, xshift, xinverted) \
202{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = 0, \
203 .info = snd_pmac_awacs_info_volume, \
204 .get = snd_pmac_awacs_get_volume, \
205 .put = snd_pmac_awacs_put_volume, \
206 .private_value = (xreg) | ((xshift) << 8) | ((xinverted) << 16) }
207
208/*
209 * mute master/ogain for AWACS: mono
210 */
Takashi Iwai65b29f52005-11-17 15:09:46 +0100211static int snd_pmac_awacs_get_switch(struct snd_kcontrol *kcontrol,
212 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213{
Takashi Iwai65b29f52005-11-17 15:09:46 +0100214 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 int reg = kcontrol->private_value & 0xff;
216 int shift = (kcontrol->private_value >> 8) & 0xff;
217 int invert = (kcontrol->private_value >> 16) & 1;
218 int val;
219 unsigned long flags;
220
221 spin_lock_irqsave(&chip->reg_lock, flags);
222 val = (chip->awacs_reg[reg] >> shift) & 1;
223 spin_unlock_irqrestore(&chip->reg_lock, flags);
224 if (invert)
225 val = 1 - val;
226 ucontrol->value.integer.value[0] = val;
227 return 0;
228}
229
Takashi Iwai65b29f52005-11-17 15:09:46 +0100230static int snd_pmac_awacs_put_switch(struct snd_kcontrol *kcontrol,
231 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232{
Takashi Iwai65b29f52005-11-17 15:09:46 +0100233 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 int reg = kcontrol->private_value & 0xff;
235 int shift = (kcontrol->private_value >> 8) & 0xff;
236 int invert = (kcontrol->private_value >> 16) & 1;
237 int mask = 1 << shift;
238 int val, changed;
239 unsigned long flags;
240
241 spin_lock_irqsave(&chip->reg_lock, flags);
242 val = chip->awacs_reg[reg] & ~mask;
243 if (ucontrol->value.integer.value[0] != invert)
244 val |= mask;
245 changed = chip->awacs_reg[reg] != val;
246 if (changed)
247 snd_pmac_awacs_write_reg(chip, reg, val);
248 spin_unlock_irqrestore(&chip->reg_lock, flags);
249 return changed;
250}
251
252#define AWACS_SWITCH(xname, xreg, xshift, xinvert) \
253{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = 0, \
254 .info = snd_pmac_boolean_mono_info, \
255 .get = snd_pmac_awacs_get_switch, \
256 .put = snd_pmac_awacs_put_switch, \
257 .private_value = (xreg) | ((xshift) << 8) | ((xinvert) << 16) }
258
259
260#ifdef PMAC_AMP_AVAIL
261/*
262 * controls for perch/whisper extension cards, e.g. G3 desktop
263 *
264 * TDA7433 connected via i2c address 0x45 (= 0x8a),
265 * accessed through cuda
266 */
267static void awacs_set_cuda(int reg, int val)
268{
269 struct adb_request req;
Risto Suominen7ae44cf2008-04-16 19:39:27 +0200270 cuda_request(&req, NULL, 5, CUDA_PACKET, CUDA_GET_SET_IIC, 0x8a,
271 reg, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 while (! req.complete)
273 cuda_poll();
274}
275
276/*
277 * level = 0 - 14, 7 = 0 dB
278 */
Takashi Iwai65b29f52005-11-17 15:09:46 +0100279static void awacs_amp_set_tone(struct awacs_amp *amp, int bass, int treble)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280{
281 amp->amp_tone[0] = bass;
282 amp->amp_tone[1] = treble;
283 if (bass > 7)
284 bass = (14 - bass) + 8;
285 if (treble > 7)
286 treble = (14 - treble) + 8;
287 awacs_set_cuda(2, (bass << 4) | treble);
288}
289
290/*
291 * vol = 0 - 31 (attenuation), 32 = mute bit, stereo
292 */
Risto Suominen7ae44cf2008-04-16 19:39:27 +0200293static int awacs_amp_set_vol(struct awacs_amp *amp, int index,
294 int lvol, int rvol, int do_check)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295{
296 if (do_check && amp->amp_vol[index][0] == lvol &&
Risto Suominen7ae44cf2008-04-16 19:39:27 +0200297 amp->amp_vol[index][1] == rvol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 return 0;
299 awacs_set_cuda(3 + index, lvol);
300 awacs_set_cuda(5 + index, rvol);
301 amp->amp_vol[index][0] = lvol;
302 amp->amp_vol[index][1] = rvol;
303 return 1;
304}
305
306/*
307 * 0 = -79 dB, 79 = 0 dB, 99 = +20 dB
308 */
Takashi Iwai65b29f52005-11-17 15:09:46 +0100309static void awacs_amp_set_master(struct awacs_amp *amp, int vol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310{
311 amp->amp_master = vol;
312 if (vol <= 79)
313 vol = 32 + (79 - vol);
314 else
315 vol = 32 - (vol - 79);
316 awacs_set_cuda(1, vol);
317}
318
Takashi Iwai65b29f52005-11-17 15:09:46 +0100319static void awacs_amp_free(struct snd_pmac *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320{
Takashi Iwai65b29f52005-11-17 15:09:46 +0100321 struct awacs_amp *amp = chip->mixer_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 snd_assert(amp, return);
323 kfree(amp);
324 chip->mixer_data = NULL;
325 chip->mixer_free = NULL;
326}
327
328
329/*
330 * mixer controls
331 */
Takashi Iwai65b29f52005-11-17 15:09:46 +0100332static int snd_pmac_awacs_info_volume_amp(struct snd_kcontrol *kcontrol,
333 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334{
335 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
336 uinfo->count = 2;
337 uinfo->value.integer.min = 0;
338 uinfo->value.integer.max = 31;
339 return 0;
340}
Risto Suominen7ae44cf2008-04-16 19:39:27 +0200341
Takashi Iwai65b29f52005-11-17 15:09:46 +0100342static int snd_pmac_awacs_get_volume_amp(struct snd_kcontrol *kcontrol,
343 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344{
Takashi Iwai65b29f52005-11-17 15:09:46 +0100345 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 int index = kcontrol->private_value;
Takashi Iwai65b29f52005-11-17 15:09:46 +0100347 struct awacs_amp *amp = chip->mixer_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 snd_assert(amp, return -EINVAL);
349 snd_assert(index >= 0 && index <= 1, return -EINVAL);
350 ucontrol->value.integer.value[0] = 31 - (amp->amp_vol[index][0] & 31);
351 ucontrol->value.integer.value[1] = 31 - (amp->amp_vol[index][1] & 31);
352 return 0;
353}
354
Takashi Iwai65b29f52005-11-17 15:09:46 +0100355static int snd_pmac_awacs_put_volume_amp(struct snd_kcontrol *kcontrol,
356 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357{
Takashi Iwai65b29f52005-11-17 15:09:46 +0100358 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 int index = kcontrol->private_value;
360 int vol[2];
Takashi Iwai65b29f52005-11-17 15:09:46 +0100361 struct awacs_amp *amp = chip->mixer_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 snd_assert(amp, return -EINVAL);
363 snd_assert(index >= 0 && index <= 1, return -EINVAL);
364
Risto Suominen7ae44cf2008-04-16 19:39:27 +0200365 vol[0] = (31 - (ucontrol->value.integer.value[0] & 31))
366 | (amp->amp_vol[index][0] & 32);
367 vol[1] = (31 - (ucontrol->value.integer.value[1] & 31))
368 | (amp->amp_vol[index][1] & 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 return awacs_amp_set_vol(amp, index, vol[0], vol[1], 1);
370}
371
Takashi Iwai65b29f52005-11-17 15:09:46 +0100372static int snd_pmac_awacs_get_switch_amp(struct snd_kcontrol *kcontrol,
373 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374{
Takashi Iwai65b29f52005-11-17 15:09:46 +0100375 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 int index = kcontrol->private_value;
Takashi Iwai65b29f52005-11-17 15:09:46 +0100377 struct awacs_amp *amp = chip->mixer_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 snd_assert(amp, return -EINVAL);
379 snd_assert(index >= 0 && index <= 1, return -EINVAL);
Risto Suominen7ae44cf2008-04-16 19:39:27 +0200380 ucontrol->value.integer.value[0] = (amp->amp_vol[index][0] & 32)
381 ? 0 : 1;
382 ucontrol->value.integer.value[1] = (amp->amp_vol[index][1] & 32)
383 ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 return 0;
385}
386
Takashi Iwai65b29f52005-11-17 15:09:46 +0100387static int snd_pmac_awacs_put_switch_amp(struct snd_kcontrol *kcontrol,
388 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389{
Takashi Iwai65b29f52005-11-17 15:09:46 +0100390 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 int index = kcontrol->private_value;
392 int vol[2];
Takashi Iwai65b29f52005-11-17 15:09:46 +0100393 struct awacs_amp *amp = chip->mixer_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 snd_assert(amp, return -EINVAL);
395 snd_assert(index >= 0 && index <= 1, return -EINVAL);
396
Risto Suominen7ae44cf2008-04-16 19:39:27 +0200397 vol[0] = (ucontrol->value.integer.value[0] ? 0 : 32)
398 | (amp->amp_vol[index][0] & 31);
399 vol[1] = (ucontrol->value.integer.value[1] ? 0 : 32)
400 | (amp->amp_vol[index][1] & 31);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 return awacs_amp_set_vol(amp, index, vol[0], vol[1], 1);
402}
403
Takashi Iwai65b29f52005-11-17 15:09:46 +0100404static int snd_pmac_awacs_info_tone_amp(struct snd_kcontrol *kcontrol,
405 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406{
407 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
408 uinfo->count = 1;
409 uinfo->value.integer.min = 0;
410 uinfo->value.integer.max = 14;
411 return 0;
412}
Risto Suominen7ae44cf2008-04-16 19:39:27 +0200413
Takashi Iwai65b29f52005-11-17 15:09:46 +0100414static int snd_pmac_awacs_get_tone_amp(struct snd_kcontrol *kcontrol,
415 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416{
Takashi Iwai65b29f52005-11-17 15:09:46 +0100417 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 int index = kcontrol->private_value;
Takashi Iwai65b29f52005-11-17 15:09:46 +0100419 struct awacs_amp *amp = chip->mixer_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 snd_assert(amp, return -EINVAL);
421 snd_assert(index >= 0 && index <= 1, return -EINVAL);
422 ucontrol->value.integer.value[0] = amp->amp_tone[index];
423 return 0;
424}
425
Takashi Iwai65b29f52005-11-17 15:09:46 +0100426static int snd_pmac_awacs_put_tone_amp(struct snd_kcontrol *kcontrol,
427 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428{
Takashi Iwai65b29f52005-11-17 15:09:46 +0100429 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 int index = kcontrol->private_value;
Takashi Iwai65b29f52005-11-17 15:09:46 +0100431 struct awacs_amp *amp = chip->mixer_data;
Takashi Iwaid4079ac2007-11-15 16:14:12 +0100432 unsigned int val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 snd_assert(amp, return -EINVAL);
434 snd_assert(index >= 0 && index <= 1, return -EINVAL);
Takashi Iwaid4079ac2007-11-15 16:14:12 +0100435 val = ucontrol->value.integer.value[0];
436 if (val > 14)
437 return -EINVAL;
438 if (val != amp->amp_tone[index]) {
439 amp->amp_tone[index] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 awacs_amp_set_tone(amp, amp->amp_tone[0], amp->amp_tone[1]);
441 return 1;
442 }
443 return 0;
444}
445
Takashi Iwai65b29f52005-11-17 15:09:46 +0100446static int snd_pmac_awacs_info_master_amp(struct snd_kcontrol *kcontrol,
447 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448{
449 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
450 uinfo->count = 1;
451 uinfo->value.integer.min = 0;
452 uinfo->value.integer.max = 99;
453 return 0;
454}
Risto Suominen7ae44cf2008-04-16 19:39:27 +0200455
Takashi Iwai65b29f52005-11-17 15:09:46 +0100456static int snd_pmac_awacs_get_master_amp(struct snd_kcontrol *kcontrol,
457 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458{
Takashi Iwai65b29f52005-11-17 15:09:46 +0100459 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
460 struct awacs_amp *amp = chip->mixer_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 snd_assert(amp, return -EINVAL);
462 ucontrol->value.integer.value[0] = amp->amp_master;
463 return 0;
464}
465
Takashi Iwai65b29f52005-11-17 15:09:46 +0100466static int snd_pmac_awacs_put_master_amp(struct snd_kcontrol *kcontrol,
467 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468{
Takashi Iwai65b29f52005-11-17 15:09:46 +0100469 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
470 struct awacs_amp *amp = chip->mixer_data;
Takashi Iwaid4079ac2007-11-15 16:14:12 +0100471 unsigned int val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 snd_assert(amp, return -EINVAL);
Takashi Iwaid4079ac2007-11-15 16:14:12 +0100473 val = ucontrol->value.integer.value[0];
474 if (val > 99)
475 return -EINVAL;
476 if (val != amp->amp_master) {
477 amp->amp_master = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 awacs_amp_set_master(amp, amp->amp_master);
479 return 1;
480 }
481 return 0;
482}
483
484#define AMP_CH_SPK 0
485#define AMP_CH_HD 1
486
Takashi Iwai65b29f52005-11-17 15:09:46 +0100487static struct snd_kcontrol_new snd_pmac_awacs_amp_vol[] __initdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
489 .name = "PC Speaker Playback Volume",
490 .info = snd_pmac_awacs_info_volume_amp,
491 .get = snd_pmac_awacs_get_volume_amp,
492 .put = snd_pmac_awacs_put_volume_amp,
493 .private_value = AMP_CH_SPK,
494 },
495 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
496 .name = "Headphone Playback Volume",
497 .info = snd_pmac_awacs_info_volume_amp,
498 .get = snd_pmac_awacs_get_volume_amp,
499 .put = snd_pmac_awacs_put_volume_amp,
500 .private_value = AMP_CH_HD,
501 },
502 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
503 .name = "Tone Control - Bass",
504 .info = snd_pmac_awacs_info_tone_amp,
505 .get = snd_pmac_awacs_get_tone_amp,
506 .put = snd_pmac_awacs_put_tone_amp,
507 .private_value = 0,
508 },
509 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
510 .name = "Tone Control - Treble",
511 .info = snd_pmac_awacs_info_tone_amp,
512 .get = snd_pmac_awacs_get_tone_amp,
513 .put = snd_pmac_awacs_put_tone_amp,
514 .private_value = 1,
515 },
516 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
517 .name = "Amp Master Playback Volume",
518 .info = snd_pmac_awacs_info_master_amp,
519 .get = snd_pmac_awacs_get_master_amp,
520 .put = snd_pmac_awacs_put_master_amp,
521 },
522};
523
Takashi Iwai65b29f52005-11-17 15:09:46 +0100524static struct snd_kcontrol_new snd_pmac_awacs_amp_hp_sw __initdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
526 .name = "Headphone Playback Switch",
527 .info = snd_pmac_boolean_stereo_info,
528 .get = snd_pmac_awacs_get_switch_amp,
529 .put = snd_pmac_awacs_put_switch_amp,
530 .private_value = AMP_CH_HD,
531};
532
Takashi Iwai65b29f52005-11-17 15:09:46 +0100533static struct snd_kcontrol_new snd_pmac_awacs_amp_spk_sw __initdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
535 .name = "PC Speaker Playback Switch",
536 .info = snd_pmac_boolean_stereo_info,
537 .get = snd_pmac_awacs_get_switch_amp,
538 .put = snd_pmac_awacs_put_switch_amp,
539 .private_value = AMP_CH_SPK,
540};
541
542#endif /* PMAC_AMP_AVAIL */
543
544
545/*
546 * mic boost for screamer
547 */
Takashi Iwai65b29f52005-11-17 15:09:46 +0100548static int snd_pmac_screamer_mic_boost_info(struct snd_kcontrol *kcontrol,
549 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550{
551 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
552 uinfo->count = 1;
553 uinfo->value.integer.min = 0;
Risto Suominena8c2a6b2008-04-17 17:55:30 +0200554 uinfo->value.integer.max = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 return 0;
556}
557
Takashi Iwai65b29f52005-11-17 15:09:46 +0100558static int snd_pmac_screamer_mic_boost_get(struct snd_kcontrol *kcontrol,
559 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560{
Takashi Iwai65b29f52005-11-17 15:09:46 +0100561 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
Risto Suominena8c2a6b2008-04-17 17:55:30 +0200562 int val = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 unsigned long flags;
564
565 spin_lock_irqsave(&chip->reg_lock, flags);
566 if (chip->awacs_reg[6] & MASK_MIC_BOOST)
Risto Suominena8c2a6b2008-04-17 17:55:30 +0200567 val |= 2;
568 if (chip->awacs_reg[0] & MASK_GAINLINE)
569 val |= 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 spin_unlock_irqrestore(&chip->reg_lock, flags);
571 ucontrol->value.integer.value[0] = val;
572 return 0;
573}
574
Takashi Iwai65b29f52005-11-17 15:09:46 +0100575static int snd_pmac_screamer_mic_boost_put(struct snd_kcontrol *kcontrol,
576 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577{
Takashi Iwai65b29f52005-11-17 15:09:46 +0100578 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 int changed = 0;
580 int val0, val6;
581 unsigned long flags;
582
583 spin_lock_irqsave(&chip->reg_lock, flags);
584 val0 = chip->awacs_reg[0] & ~MASK_GAINLINE;
585 val6 = chip->awacs_reg[6] & ~MASK_MIC_BOOST;
Risto Suominena8c2a6b2008-04-17 17:55:30 +0200586 if (ucontrol->value.integer.value[0] & 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 val0 |= MASK_GAINLINE;
Risto Suominena8c2a6b2008-04-17 17:55:30 +0200588 if (ucontrol->value.integer.value[0] & 2)
589 val6 |= MASK_MIC_BOOST;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 if (val0 != chip->awacs_reg[0]) {
591 snd_pmac_awacs_write_reg(chip, 0, val0);
592 changed = 1;
593 }
594 if (val6 != chip->awacs_reg[6]) {
595 snd_pmac_awacs_write_reg(chip, 6, val6);
596 changed = 1;
597 }
598 spin_unlock_irqrestore(&chip->reg_lock, flags);
599 return changed;
600}
601
602/*
603 * lists of mixer elements
604 */
Takashi Iwai65b29f52005-11-17 15:09:46 +0100605static struct snd_kcontrol_new snd_pmac_awacs_mixers[] __initdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 AWACS_SWITCH("Master Capture Switch", 1, SHIFT_LOOPTHRU, 0),
Risto Suominena8c2a6b2008-04-17 17:55:30 +0200607 AWACS_VOLUME("Master Capture Volume", 0, 4, 0),
608/* AWACS_SWITCH("Unknown Playback Switch", 6, SHIFT_PAROUT0, 0), */
609};
610
611static struct snd_kcontrol_new snd_pmac_screamer_mixers_beige[] __initdata = {
612 AWACS_VOLUME("Master Playback Volume", 2, 6, 1),
613 AWACS_VOLUME("Play-through Playback Volume", 5, 6, 1),
614 AWACS_SWITCH("Line Capture Switch", 0, SHIFT_MUX_MIC, 0),
615 AWACS_SWITCH("CD Capture Switch", 0, SHIFT_MUX_LINE, 0),
616};
617
618static struct snd_kcontrol_new snd_pmac_screamer_mixers_imac[] __initdata = {
619 AWACS_VOLUME("Line out Playback Volume", 2, 6, 1),
620 AWACS_VOLUME("Master Playback Volume", 5, 6, 1),
621 AWACS_SWITCH("CD Capture Switch", 0, SHIFT_MUX_CD, 0),
622};
623
Risto Suominen4dbf95b2008-08-25 08:02:12 +0200624static struct snd_kcontrol_new snd_pmac_screamer_mixers_g4agp[] __initdata = {
625 AWACS_VOLUME("Line out Playback Volume", 2, 6, 1),
626 AWACS_VOLUME("Master Playback Volume", 5, 6, 1),
627 AWACS_SWITCH("CD Capture Switch", 0, SHIFT_MUX_CD, 0),
628 AWACS_SWITCH("Line Capture Switch", 0, SHIFT_MUX_MIC, 0),
629};
630
Risto Suominena8c2a6b2008-04-17 17:55:30 +0200631static struct snd_kcontrol_new snd_pmac_awacs_mixers_pmac7500[] __initdata = {
632 AWACS_VOLUME("Line out Playback Volume", 2, 6, 1),
633 AWACS_SWITCH("CD Capture Switch", 0, SHIFT_MUX_CD, 0),
634 AWACS_SWITCH("Line Capture Switch", 0, SHIFT_MUX_MIC, 0),
635};
636
637static struct snd_kcontrol_new snd_pmac_awacs_mixers_pmac[] __initdata = {
638 AWACS_VOLUME("Master Playback Volume", 2, 6, 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 AWACS_SWITCH("CD Capture Switch", 0, SHIFT_MUX_CD, 0),
640};
641
642/* FIXME: is this correct order?
643 * screamer (powerbook G3 pismo) seems to have different bits...
644 */
Takashi Iwai65b29f52005-11-17 15:09:46 +0100645static struct snd_kcontrol_new snd_pmac_awacs_mixers2[] __initdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 AWACS_SWITCH("Line Capture Switch", 0, SHIFT_MUX_LINE, 0),
647 AWACS_SWITCH("Mic Capture Switch", 0, SHIFT_MUX_MIC, 0),
648};
649
Takashi Iwai65b29f52005-11-17 15:09:46 +0100650static struct snd_kcontrol_new snd_pmac_screamer_mixers2[] __initdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 AWACS_SWITCH("Line Capture Switch", 0, SHIFT_MUX_MIC, 0),
652 AWACS_SWITCH("Mic Capture Switch", 0, SHIFT_MUX_LINE, 0),
653};
654
Takashi Iwai65b29f52005-11-17 15:09:46 +0100655static struct snd_kcontrol_new snd_pmac_awacs_master_sw __initdata =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656AWACS_SWITCH("Master Playback Switch", 1, SHIFT_HDMUTE, 1);
657
Risto Suominena8c2a6b2008-04-17 17:55:30 +0200658static struct snd_kcontrol_new snd_pmac_awacs_master_sw_imac __initdata =
659AWACS_SWITCH("Line out Playback Switch", 1, SHIFT_HDMUTE, 1);
660
Takashi Iwai65b29f52005-11-17 15:09:46 +0100661static struct snd_kcontrol_new snd_pmac_awacs_mic_boost[] __initdata = {
Risto Suominena8c2a6b2008-04-17 17:55:30 +0200662 AWACS_SWITCH("Mic Boost Capture Switch", 0, SHIFT_GAINLINE, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663};
664
Takashi Iwai65b29f52005-11-17 15:09:46 +0100665static struct snd_kcontrol_new snd_pmac_screamer_mic_boost[] __initdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Risto Suominena8c2a6b2008-04-17 17:55:30 +0200667 .name = "Mic Boost Capture Volume",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 .info = snd_pmac_screamer_mic_boost_info,
669 .get = snd_pmac_screamer_mic_boost_get,
670 .put = snd_pmac_screamer_mic_boost_put,
671 },
672};
673
Risto Suominena8c2a6b2008-04-17 17:55:30 +0200674static struct snd_kcontrol_new snd_pmac_awacs_mic_boost_pmac7500[] __initdata =
675{
676 AWACS_SWITCH("Line Boost Capture Switch", 0, SHIFT_GAINLINE, 0),
677};
678
679static struct snd_kcontrol_new snd_pmac_screamer_mic_boost_beige[] __initdata =
680{
681 AWACS_SWITCH("Line Boost Capture Switch", 0, SHIFT_GAINLINE, 0),
682 AWACS_SWITCH("CD Boost Capture Switch", 6, SHIFT_MIC_BOOST, 0),
683};
684
685static struct snd_kcontrol_new snd_pmac_screamer_mic_boost_imac[] __initdata =
686{
687 AWACS_SWITCH("Line Boost Capture Switch", 0, SHIFT_GAINLINE, 0),
688 AWACS_SWITCH("Mic Boost Capture Switch", 6, SHIFT_MIC_BOOST, 0),
689};
690
Takashi Iwai65b29f52005-11-17 15:09:46 +0100691static struct snd_kcontrol_new snd_pmac_awacs_speaker_vol[] __initdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 AWACS_VOLUME("PC Speaker Playback Volume", 4, 6, 1),
693};
Risto Suominena8c2a6b2008-04-17 17:55:30 +0200694
Takashi Iwai65b29f52005-11-17 15:09:46 +0100695static struct snd_kcontrol_new snd_pmac_awacs_speaker_sw __initdata =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696AWACS_SWITCH("PC Speaker Playback Switch", 1, SHIFT_SPKMUTE, 1);
697
Risto Suominen030b6552008-08-25 08:04:23 +0200698static struct snd_kcontrol_new snd_pmac_awacs_speaker_sw_imac1 __initdata =
699AWACS_SWITCH("PC Speaker Playback Switch", 1, SHIFT_PAROUT1, 1);
700
701static struct snd_kcontrol_new snd_pmac_awacs_speaker_sw_imac2 __initdata =
Risto Suominena8c2a6b2008-04-17 17:55:30 +0200702AWACS_SWITCH("PC Speaker Playback Switch", 1, SHIFT_PAROUT1, 0);
703
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
705/*
706 * add new mixer elements to the card
707 */
Risto Suominen7ae44cf2008-04-16 19:39:27 +0200708static int build_mixers(struct snd_pmac *chip, int nums,
709 struct snd_kcontrol_new *mixers)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710{
711 int i, err;
712
713 for (i = 0; i < nums; i++) {
Risto Suominen7ae44cf2008-04-16 19:39:27 +0200714 err = snd_ctl_add(chip->card, snd_ctl_new1(&mixers[i], chip));
715 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 return err;
717 }
718 return 0;
719}
720
721
722/*
723 * restore all registers
724 */
Takashi Iwai65b29f52005-11-17 15:09:46 +0100725static void awacs_restore_all_regs(struct snd_pmac *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726{
727 snd_pmac_awacs_write_noreg(chip, 0, chip->awacs_reg[0]);
728 snd_pmac_awacs_write_noreg(chip, 1, chip->awacs_reg[1]);
729 snd_pmac_awacs_write_noreg(chip, 2, chip->awacs_reg[2]);
730 snd_pmac_awacs_write_noreg(chip, 4, chip->awacs_reg[4]);
731 if (chip->model == PMAC_SCREAMER) {
732 snd_pmac_awacs_write_noreg(chip, 5, chip->awacs_reg[5]);
733 snd_pmac_awacs_write_noreg(chip, 6, chip->awacs_reg[6]);
734 snd_pmac_awacs_write_noreg(chip, 7, chip->awacs_reg[7]);
735 }
736}
737
Benjamin Herrenschmidt8c870932005-06-27 14:36:34 -0700738#ifdef CONFIG_PM
Takashi Iwai65b29f52005-11-17 15:09:46 +0100739static void snd_pmac_awacs_suspend(struct snd_pmac *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740{
741 snd_pmac_awacs_write_noreg(chip, 1, (chip->awacs_reg[1]
742 | MASK_AMUTE | MASK_CMUTE));
743}
744
Takashi Iwai65b29f52005-11-17 15:09:46 +0100745static void snd_pmac_awacs_resume(struct snd_pmac *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746{
747 if (machine_is_compatible("PowerBook3,1")
748 || machine_is_compatible("PowerBook3,2")) {
Nishanth Aravamudan989a0b22005-07-09 10:53:24 +0200749 msleep(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 snd_pmac_awacs_write_reg(chip, 1,
751 chip->awacs_reg[1] & ~MASK_PAROUT);
Nishanth Aravamudan989a0b22005-07-09 10:53:24 +0200752 msleep(300);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 }
754
755 awacs_restore_all_regs(chip);
756 if (chip->model == PMAC_SCREAMER) {
757 /* reset power bits in reg 6 */
758 mdelay(5);
759 snd_pmac_awacs_write_noreg(chip, 6, chip->awacs_reg[6]);
760 }
761 screamer_recalibrate(chip);
762#ifdef PMAC_AMP_AVAIL
763 if (chip->mixer_data) {
Takashi Iwai65b29f52005-11-17 15:09:46 +0100764 struct awacs_amp *amp = chip->mixer_data;
Risto Suominen7ae44cf2008-04-16 19:39:27 +0200765 awacs_amp_set_vol(amp, 0,
766 amp->amp_vol[0][0], amp->amp_vol[0][1], 0);
767 awacs_amp_set_vol(amp, 1,
768 amp->amp_vol[1][0], amp->amp_vol[1][1], 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 awacs_amp_set_tone(amp, amp->amp_tone[0], amp->amp_tone[1]);
770 awacs_amp_set_master(amp, amp->amp_master);
771 }
772#endif
773}
Benjamin Herrenschmidt8c870932005-06-27 14:36:34 -0700774#endif /* CONFIG_PM */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
Risto Suominena8c2a6b2008-04-17 17:55:30 +0200776#define IS_PM7500 (machine_is_compatible("AAPL,7500"))
777#define IS_BEIGE (machine_is_compatible("AAPL,Gossamer"))
Risto Suominen030b6552008-08-25 08:04:23 +0200778#define IS_IMAC1 (machine_is_compatible("PowerMac2,1"))
779#define IS_IMAC2 (machine_is_compatible("PowerMac2,2") \
Risto Suominena8c2a6b2008-04-17 17:55:30 +0200780 || machine_is_compatible("PowerMac4,1"))
Risto Suominen4dbf95b2008-08-25 08:02:12 +0200781#define IS_G4AGP (machine_is_compatible("PowerMac3,1"))
Risto Suominena8c2a6b2008-04-17 17:55:30 +0200782
Risto Suominen030b6552008-08-25 08:04:23 +0200783static int imac1, imac2;
Risto Suominena8c2a6b2008-04-17 17:55:30 +0200784
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785#ifdef PMAC_SUPPORT_AUTOMUTE
786/*
787 * auto-mute stuffs
788 */
Takashi Iwai65b29f52005-11-17 15:09:46 +0100789static int snd_pmac_awacs_detect_headphone(struct snd_pmac *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790{
791 return (in_le32(&chip->awacs->codec_stat) & chip->hp_stat_mask) ? 1 : 0;
792}
793
794#ifdef PMAC_AMP_AVAIL
Takashi Iwai65b29f52005-11-17 15:09:46 +0100795static int toggle_amp_mute(struct awacs_amp *amp, int index, int mute)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796{
797 int vol[2];
798 vol[0] = amp->amp_vol[index][0] & 31;
799 vol[1] = amp->amp_vol[index][1] & 31;
800 if (mute) {
801 vol[0] |= 32;
802 vol[1] |= 32;
803 }
804 return awacs_amp_set_vol(amp, index, vol[0], vol[1], 1);
805}
806#endif
807
Takashi Iwai65b29f52005-11-17 15:09:46 +0100808static void snd_pmac_awacs_update_automute(struct snd_pmac *chip, int do_notify)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809{
810 if (chip->auto_mute) {
811#ifdef PMAC_AMP_AVAIL
812 if (chip->mixer_data) {
Takashi Iwai65b29f52005-11-17 15:09:46 +0100813 struct awacs_amp *amp = chip->mixer_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 int changed;
815 if (snd_pmac_awacs_detect_headphone(chip)) {
816 changed = toggle_amp_mute(amp, AMP_CH_HD, 0);
817 changed |= toggle_amp_mute(amp, AMP_CH_SPK, 1);
818 } else {
819 changed = toggle_amp_mute(amp, AMP_CH_HD, 1);
820 changed |= toggle_amp_mute(amp, AMP_CH_SPK, 0);
821 }
822 if (do_notify && ! changed)
823 return;
824 } else
825#endif
826 {
Risto Suominena8c2a6b2008-04-17 17:55:30 +0200827 int reg = chip->awacs_reg[1]
828 | (MASK_HDMUTE | MASK_SPKMUTE);
Risto Suominen030b6552008-08-25 08:04:23 +0200829 if (imac1) {
830 reg &= ~MASK_SPKMUTE;
831 reg |= MASK_PAROUT1;
832 } else if (imac2) {
Risto Suominena8c2a6b2008-04-17 17:55:30 +0200833 reg &= ~MASK_SPKMUTE;
834 reg &= ~MASK_PAROUT1;
835 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 if (snd_pmac_awacs_detect_headphone(chip))
837 reg &= ~MASK_HDMUTE;
Risto Suominen030b6552008-08-25 08:04:23 +0200838 else if (imac1)
839 reg &= ~MASK_PAROUT1;
840 else if (imac2)
Risto Suominena8c2a6b2008-04-17 17:55:30 +0200841 reg |= MASK_PAROUT1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 else
843 reg &= ~MASK_SPKMUTE;
844 if (do_notify && reg == chip->awacs_reg[1])
845 return;
846 snd_pmac_awacs_write_reg(chip, 1, reg);
847 }
848 if (do_notify) {
849 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
850 &chip->master_sw_ctl->id);
851 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
852 &chip->speaker_sw_ctl->id);
853 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
854 &chip->hp_detect_ctl->id);
855 }
856 }
857}
858#endif /* PMAC_SUPPORT_AUTOMUTE */
859
860
861/*
862 * initialize chip
863 */
864int __init
Takashi Iwai65b29f52005-11-17 15:09:46 +0100865snd_pmac_awacs_init(struct snd_pmac *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866{
Risto Suominena8c2a6b2008-04-17 17:55:30 +0200867 int pm7500 = IS_PM7500;
868 int beige = IS_BEIGE;
Risto Suominen4dbf95b2008-08-25 08:02:12 +0200869 int g4agp = IS_G4AGP;
Risto Suominen030b6552008-08-25 08:04:23 +0200870 int imac;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 int err, vol;
872
Risto Suominen030b6552008-08-25 08:04:23 +0200873 imac1 = IS_IMAC1;
874 imac2 = IS_IMAC2;
875 imac = imac1 || imac2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 /* looks like MASK_GAINLINE triggers something, so we set here
877 * as start-up
878 */
879 chip->awacs_reg[0] = MASK_MUX_CD | 0xff | MASK_GAINLINE;
880 chip->awacs_reg[1] = MASK_CMUTE | MASK_AMUTE;
881 /* FIXME: Only machines with external SRS module need MASK_PAROUT */
882 if (chip->has_iic || chip->device_id == 0x5 ||
Risto Suominen7ae44cf2008-04-16 19:39:27 +0200883 /* chip->_device_id == 0x8 || */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 chip->device_id == 0xb)
885 chip->awacs_reg[1] |= MASK_PAROUT;
886 /* get default volume from nvram */
887 // vol = (~nvram_read_byte(0x1308) & 7) << 1;
888 // vol = ((pmac_xpram_read( 8 ) & 7 ) << 1 );
889 vol = 0x0f; /* no, on alsa, muted as default */
890 vol = vol + (vol << 6);
891 chip->awacs_reg[2] = vol;
892 chip->awacs_reg[4] = vol;
893 if (chip->model == PMAC_SCREAMER) {
Risto Suominen7ae44cf2008-04-16 19:39:27 +0200894 /* FIXME: screamer has loopthru vol control */
895 chip->awacs_reg[5] = vol;
896 /* FIXME: maybe should be vol << 3 for PCMCIA speaker */
897 chip->awacs_reg[6] = MASK_MIC_BOOST;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 chip->awacs_reg[7] = 0;
899 }
900
901 awacs_restore_all_regs(chip);
902 chip->manufacturer = (in_le32(&chip->awacs->codec_stat) >> 8) & 0xf;
903 screamer_recalibrate(chip);
904
905 chip->revision = (in_le32(&chip->awacs->codec_stat) >> 12) & 0xf;
906#ifdef PMAC_AMP_AVAIL
907 if (chip->revision == 3 && chip->has_iic && CHECK_CUDA_AMP()) {
Panagiotis Issaris59feddb2006-07-25 15:28:03 +0200908 struct awacs_amp *amp = kzalloc(sizeof(*amp), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 if (! amp)
910 return -ENOMEM;
911 chip->mixer_data = amp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 chip->mixer_free = awacs_amp_free;
Risto Suominen7ae44cf2008-04-16 19:39:27 +0200913 /* mute and zero vol */
914 awacs_amp_set_vol(amp, 0, 63, 63, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 awacs_amp_set_vol(amp, 1, 63, 63, 0);
916 awacs_amp_set_tone(amp, 7, 7); /* 0 dB */
917 awacs_amp_set_master(amp, 79); /* 0 dB */
918 }
919#endif /* PMAC_AMP_AVAIL */
920
921 if (chip->hp_stat_mask == 0) {
922 /* set headphone-jack detection bit */
923 switch (chip->model) {
924 case PMAC_AWACS:
Risto Suominena8c2a6b2008-04-17 17:55:30 +0200925 chip->hp_stat_mask = pm7500 ? MASK_HDPCONN
926 : MASK_LOCONN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 break;
928 case PMAC_SCREAMER:
929 switch (chip->device_id) {
930 case 0x08:
Risto Suominena8c2a6b2008-04-17 17:55:30 +0200931 case 0x0B:
932 chip->hp_stat_mask = imac
933 ? MASK_LOCONN_IMAC |
934 MASK_HDPLCONN_IMAC |
935 MASK_HDPRCONN_IMAC
936 : MASK_HDPCONN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 break;
938 case 0x00:
939 case 0x05:
Risto Suominena8c2a6b2008-04-17 17:55:30 +0200940 chip->hp_stat_mask = MASK_LOCONN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 break;
942 default:
Risto Suominena8c2a6b2008-04-17 17:55:30 +0200943 chip->hp_stat_mask = MASK_HDPCONN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 break;
945 }
946 break;
947 default:
948 snd_BUG();
949 break;
950 }
951 }
952
953 /*
954 * build mixers
955 */
956 strcpy(chip->card->mixername, "PowerMac AWACS");
957
Risto Suominen7ae44cf2008-04-16 19:39:27 +0200958 err = build_mixers(chip, ARRAY_SIZE(snd_pmac_awacs_mixers),
959 snd_pmac_awacs_mixers);
960 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 return err;
Risto Suominen4dbf95b2008-08-25 08:02:12 +0200962 if (beige || g4agp)
Risto Suominena8c2a6b2008-04-17 17:55:30 +0200963 ;
964 else if (chip->model == PMAC_SCREAMER)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 err = build_mixers(chip, ARRAY_SIZE(snd_pmac_screamer_mixers2),
966 snd_pmac_screamer_mixers2);
Risto Suominena8c2a6b2008-04-17 17:55:30 +0200967 else if (!pm7500)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 err = build_mixers(chip, ARRAY_SIZE(snd_pmac_awacs_mixers2),
969 snd_pmac_awacs_mixers2);
970 if (err < 0)
971 return err;
Risto Suominena8c2a6b2008-04-17 17:55:30 +0200972 if (pm7500)
973 err = build_mixers(chip,
974 ARRAY_SIZE(snd_pmac_awacs_mixers_pmac7500),
975 snd_pmac_awacs_mixers_pmac7500);
976 else if (beige)
977 err = build_mixers(chip,
978 ARRAY_SIZE(snd_pmac_screamer_mixers_beige),
979 snd_pmac_screamer_mixers_beige);
980 else if (imac)
981 err = build_mixers(chip,
982 ARRAY_SIZE(snd_pmac_screamer_mixers_imac),
983 snd_pmac_screamer_mixers_imac);
Risto Suominen4dbf95b2008-08-25 08:02:12 +0200984 else if (g4agp)
985 err = build_mixers(chip,
986 ARRAY_SIZE(snd_pmac_screamer_mixers_g4agp),
987 snd_pmac_screamer_mixers_g4agp);
Risto Suominena8c2a6b2008-04-17 17:55:30 +0200988 else
989 err = build_mixers(chip,
990 ARRAY_SIZE(snd_pmac_awacs_mixers_pmac),
991 snd_pmac_awacs_mixers_pmac);
992 if (err < 0)
993 return err;
Risto Suominen4dbf95b2008-08-25 08:02:12 +0200994 chip->master_sw_ctl = snd_ctl_new1((pm7500 || imac || g4agp)
Risto Suominena8c2a6b2008-04-17 17:55:30 +0200995 ? &snd_pmac_awacs_master_sw_imac
996 : &snd_pmac_awacs_master_sw, chip);
Risto Suominen7ae44cf2008-04-16 19:39:27 +0200997 err = snd_ctl_add(chip->card, chip->master_sw_ctl);
998 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 return err;
1000#ifdef PMAC_AMP_AVAIL
1001 if (chip->mixer_data) {
1002 /* use amplifier. the signal is connected from route A
1003 * to the amp. the amp has its headphone and speaker
1004 * volumes and mute switches, so we use them instead of
1005 * screamer registers.
1006 * in this case, it seems the route C is not used.
1007 */
Risto Suominen7ae44cf2008-04-16 19:39:27 +02001008 err = build_mixers(chip, ARRAY_SIZE(snd_pmac_awacs_amp_vol),
1009 snd_pmac_awacs_amp_vol);
1010 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 return err;
1012 /* overwrite */
Risto Suominen7ae44cf2008-04-16 19:39:27 +02001013 chip->master_sw_ctl = snd_ctl_new1(&snd_pmac_awacs_amp_hp_sw,
1014 chip);
1015 err = snd_ctl_add(chip->card, chip->master_sw_ctl);
1016 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 return err;
Risto Suominen7ae44cf2008-04-16 19:39:27 +02001018 chip->speaker_sw_ctl = snd_ctl_new1(&snd_pmac_awacs_amp_spk_sw,
1019 chip);
1020 err = snd_ctl_add(chip->card, chip->speaker_sw_ctl);
1021 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 return err;
1023 } else
1024#endif /* PMAC_AMP_AVAIL */
1025 {
1026 /* route A = headphone, route C = speaker */
Risto Suominen7ae44cf2008-04-16 19:39:27 +02001027 err = build_mixers(chip, ARRAY_SIZE(snd_pmac_awacs_speaker_vol),
1028 snd_pmac_awacs_speaker_vol);
1029 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 return err;
Risto Suominen030b6552008-08-25 08:04:23 +02001031 chip->speaker_sw_ctl = snd_ctl_new1(imac1
1032 ? &snd_pmac_awacs_speaker_sw_imac1
1033 : imac2
1034 ? &snd_pmac_awacs_speaker_sw_imac2
Risto Suominena8c2a6b2008-04-17 17:55:30 +02001035 : &snd_pmac_awacs_speaker_sw, chip);
Risto Suominen7ae44cf2008-04-16 19:39:27 +02001036 err = snd_ctl_add(chip->card, chip->speaker_sw_ctl);
1037 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 return err;
1039 }
1040
Risto Suominen4dbf95b2008-08-25 08:02:12 +02001041 if (beige || g4agp)
Risto Suominena8c2a6b2008-04-17 17:55:30 +02001042 err = build_mixers(chip,
1043 ARRAY_SIZE(snd_pmac_screamer_mic_boost_beige),
1044 snd_pmac_screamer_mic_boost_beige);
1045 else if (imac)
1046 err = build_mixers(chip,
1047 ARRAY_SIZE(snd_pmac_screamer_mic_boost_imac),
1048 snd_pmac_screamer_mic_boost_imac);
1049 else if (chip->model == PMAC_SCREAMER)
1050 err = build_mixers(chip,
1051 ARRAY_SIZE(snd_pmac_screamer_mic_boost),
1052 snd_pmac_screamer_mic_boost);
1053 else if (pm7500)
1054 err = build_mixers(chip,
1055 ARRAY_SIZE(snd_pmac_awacs_mic_boost_pmac7500),
1056 snd_pmac_awacs_mic_boost_pmac7500);
1057 else
1058 err = build_mixers(chip, ARRAY_SIZE(snd_pmac_awacs_mic_boost),
1059 snd_pmac_awacs_mic_boost);
1060 if (err < 0)
1061 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062
1063 /*
1064 * set lowlevel callbacks
1065 */
1066 chip->set_format = snd_pmac_awacs_set_format;
Benjamin Herrenschmidt8c870932005-06-27 14:36:34 -07001067#ifdef CONFIG_PM
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 chip->suspend = snd_pmac_awacs_suspend;
1069 chip->resume = snd_pmac_awacs_resume;
1070#endif
1071#ifdef PMAC_SUPPORT_AUTOMUTE
Risto Suominen7ae44cf2008-04-16 19:39:27 +02001072 err = snd_pmac_add_automute(chip);
1073 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 return err;
1075 chip->detect_headphone = snd_pmac_awacs_detect_headphone;
1076 chip->update_automute = snd_pmac_awacs_update_automute;
1077 snd_pmac_awacs_update_automute(chip, 0); /* update the status only */
1078#endif
1079 if (chip->model == PMAC_SCREAMER) {
1080 snd_pmac_awacs_write_noreg(chip, 6, chip->awacs_reg[6]);
1081 snd_pmac_awacs_write_noreg(chip, 0, chip->awacs_reg[0]);
1082 }
1083
1084 return 0;
1085}