blob: d89c23e135dcba4e9442d8c00cef7224927e9f48 [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;
Takashi Iwai5e246b82008-08-08 17:12:47 +0200322 if (!amp)
323 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 kfree(amp);
325 chip->mixer_data = NULL;
326 chip->mixer_free = NULL;
327}
328
329
330/*
331 * mixer controls
332 */
Takashi Iwai65b29f52005-11-17 15:09:46 +0100333static int snd_pmac_awacs_info_volume_amp(struct snd_kcontrol *kcontrol,
334 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335{
336 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
337 uinfo->count = 2;
338 uinfo->value.integer.min = 0;
339 uinfo->value.integer.max = 31;
340 return 0;
341}
Risto Suominen7ae44cf2008-04-16 19:39:27 +0200342
Takashi Iwai65b29f52005-11-17 15:09:46 +0100343static int snd_pmac_awacs_get_volume_amp(struct snd_kcontrol *kcontrol,
344 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345{
Takashi Iwai65b29f52005-11-17 15:09:46 +0100346 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 int index = kcontrol->private_value;
Takashi Iwai65b29f52005-11-17 15:09:46 +0100348 struct awacs_amp *amp = chip->mixer_data;
Takashi Iwai5e246b82008-08-08 17:12:47 +0200349
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 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
Risto Suominen7ae44cf2008-04-16 19:39:27 +0200363 vol[0] = (31 - (ucontrol->value.integer.value[0] & 31))
364 | (amp->amp_vol[index][0] & 32);
365 vol[1] = (31 - (ucontrol->value.integer.value[1] & 31))
366 | (amp->amp_vol[index][1] & 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 return awacs_amp_set_vol(amp, index, vol[0], vol[1], 1);
368}
369
Takashi Iwai65b29f52005-11-17 15:09:46 +0100370static int snd_pmac_awacs_get_switch_amp(struct snd_kcontrol *kcontrol,
371 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372{
Takashi Iwai65b29f52005-11-17 15:09:46 +0100373 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 int index = kcontrol->private_value;
Takashi Iwai65b29f52005-11-17 15:09:46 +0100375 struct awacs_amp *amp = chip->mixer_data;
Takashi Iwai5e246b82008-08-08 17:12:47 +0200376
Risto Suominen7ae44cf2008-04-16 19:39:27 +0200377 ucontrol->value.integer.value[0] = (amp->amp_vol[index][0] & 32)
378 ? 0 : 1;
379 ucontrol->value.integer.value[1] = (amp->amp_vol[index][1] & 32)
380 ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 return 0;
382}
383
Takashi Iwai65b29f52005-11-17 15:09:46 +0100384static int snd_pmac_awacs_put_switch_amp(struct snd_kcontrol *kcontrol,
385 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386{
Takashi Iwai65b29f52005-11-17 15:09:46 +0100387 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 int index = kcontrol->private_value;
389 int vol[2];
Takashi Iwai65b29f52005-11-17 15:09:46 +0100390 struct awacs_amp *amp = chip->mixer_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
Risto Suominen7ae44cf2008-04-16 19:39:27 +0200392 vol[0] = (ucontrol->value.integer.value[0] ? 0 : 32)
393 | (amp->amp_vol[index][0] & 31);
394 vol[1] = (ucontrol->value.integer.value[1] ? 0 : 32)
395 | (amp->amp_vol[index][1] & 31);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 return awacs_amp_set_vol(amp, index, vol[0], vol[1], 1);
397}
398
Takashi Iwai65b29f52005-11-17 15:09:46 +0100399static int snd_pmac_awacs_info_tone_amp(struct snd_kcontrol *kcontrol,
400 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401{
402 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
403 uinfo->count = 1;
404 uinfo->value.integer.min = 0;
405 uinfo->value.integer.max = 14;
406 return 0;
407}
Risto Suominen7ae44cf2008-04-16 19:39:27 +0200408
Takashi Iwai65b29f52005-11-17 15:09:46 +0100409static int snd_pmac_awacs_get_tone_amp(struct snd_kcontrol *kcontrol,
410 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411{
Takashi Iwai65b29f52005-11-17 15:09:46 +0100412 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 int index = kcontrol->private_value;
Takashi Iwai65b29f52005-11-17 15:09:46 +0100414 struct awacs_amp *amp = chip->mixer_data;
Takashi Iwai5e246b82008-08-08 17:12:47 +0200415
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 ucontrol->value.integer.value[0] = amp->amp_tone[index];
417 return 0;
418}
419
Takashi Iwai65b29f52005-11-17 15:09:46 +0100420static int snd_pmac_awacs_put_tone_amp(struct snd_kcontrol *kcontrol,
421 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422{
Takashi Iwai65b29f52005-11-17 15:09:46 +0100423 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 int index = kcontrol->private_value;
Takashi Iwai65b29f52005-11-17 15:09:46 +0100425 struct awacs_amp *amp = chip->mixer_data;
Takashi Iwaid4079ac2007-11-15 16:14:12 +0100426 unsigned int val;
Takashi Iwai5e246b82008-08-08 17:12:47 +0200427
Takashi Iwaid4079ac2007-11-15 16:14:12 +0100428 val = ucontrol->value.integer.value[0];
429 if (val > 14)
430 return -EINVAL;
431 if (val != amp->amp_tone[index]) {
432 amp->amp_tone[index] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 awacs_amp_set_tone(amp, amp->amp_tone[0], amp->amp_tone[1]);
434 return 1;
435 }
436 return 0;
437}
438
Takashi Iwai65b29f52005-11-17 15:09:46 +0100439static int snd_pmac_awacs_info_master_amp(struct snd_kcontrol *kcontrol,
440 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441{
442 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
443 uinfo->count = 1;
444 uinfo->value.integer.min = 0;
445 uinfo->value.integer.max = 99;
446 return 0;
447}
Risto Suominen7ae44cf2008-04-16 19:39:27 +0200448
Takashi Iwai65b29f52005-11-17 15:09:46 +0100449static int snd_pmac_awacs_get_master_amp(struct snd_kcontrol *kcontrol,
450 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451{
Takashi Iwai65b29f52005-11-17 15:09:46 +0100452 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
453 struct awacs_amp *amp = chip->mixer_data;
Takashi Iwai5e246b82008-08-08 17:12:47 +0200454
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 ucontrol->value.integer.value[0] = amp->amp_master;
456 return 0;
457}
458
Takashi Iwai65b29f52005-11-17 15:09:46 +0100459static int snd_pmac_awacs_put_master_amp(struct snd_kcontrol *kcontrol,
460 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461{
Takashi Iwai65b29f52005-11-17 15:09:46 +0100462 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
463 struct awacs_amp *amp = chip->mixer_data;
Takashi Iwaid4079ac2007-11-15 16:14:12 +0100464 unsigned int val;
Takashi Iwai5e246b82008-08-08 17:12:47 +0200465
Takashi Iwaid4079ac2007-11-15 16:14:12 +0100466 val = ucontrol->value.integer.value[0];
467 if (val > 99)
468 return -EINVAL;
469 if (val != amp->amp_master) {
470 amp->amp_master = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 awacs_amp_set_master(amp, amp->amp_master);
472 return 1;
473 }
474 return 0;
475}
476
477#define AMP_CH_SPK 0
478#define AMP_CH_HD 1
479
Takashi Iwai65b29f52005-11-17 15:09:46 +0100480static struct snd_kcontrol_new snd_pmac_awacs_amp_vol[] __initdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
482 .name = "PC Speaker Playback Volume",
483 .info = snd_pmac_awacs_info_volume_amp,
484 .get = snd_pmac_awacs_get_volume_amp,
485 .put = snd_pmac_awacs_put_volume_amp,
486 .private_value = AMP_CH_SPK,
487 },
488 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
489 .name = "Headphone 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_HD,
494 },
495 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
496 .name = "Tone Control - Bass",
497 .info = snd_pmac_awacs_info_tone_amp,
498 .get = snd_pmac_awacs_get_tone_amp,
499 .put = snd_pmac_awacs_put_tone_amp,
500 .private_value = 0,
501 },
502 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
503 .name = "Tone Control - Treble",
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 = 1,
508 },
509 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
510 .name = "Amp Master Playback Volume",
511 .info = snd_pmac_awacs_info_master_amp,
512 .get = snd_pmac_awacs_get_master_amp,
513 .put = snd_pmac_awacs_put_master_amp,
514 },
515};
516
Takashi Iwai65b29f52005-11-17 15:09:46 +0100517static struct snd_kcontrol_new snd_pmac_awacs_amp_hp_sw __initdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
519 .name = "Headphone Playback Switch",
520 .info = snd_pmac_boolean_stereo_info,
521 .get = snd_pmac_awacs_get_switch_amp,
522 .put = snd_pmac_awacs_put_switch_amp,
523 .private_value = AMP_CH_HD,
524};
525
Takashi Iwai65b29f52005-11-17 15:09:46 +0100526static struct snd_kcontrol_new snd_pmac_awacs_amp_spk_sw __initdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
528 .name = "PC Speaker Playback Switch",
529 .info = snd_pmac_boolean_stereo_info,
530 .get = snd_pmac_awacs_get_switch_amp,
531 .put = snd_pmac_awacs_put_switch_amp,
532 .private_value = AMP_CH_SPK,
533};
534
535#endif /* PMAC_AMP_AVAIL */
536
537
538/*
539 * mic boost for screamer
540 */
Takashi Iwai65b29f52005-11-17 15:09:46 +0100541static int snd_pmac_screamer_mic_boost_info(struct snd_kcontrol *kcontrol,
542 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543{
544 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
545 uinfo->count = 1;
546 uinfo->value.integer.min = 0;
Risto Suominena8c2a6b2008-04-17 17:55:30 +0200547 uinfo->value.integer.max = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 return 0;
549}
550
Takashi Iwai65b29f52005-11-17 15:09:46 +0100551static int snd_pmac_screamer_mic_boost_get(struct snd_kcontrol *kcontrol,
552 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553{
Takashi Iwai65b29f52005-11-17 15:09:46 +0100554 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
Risto Suominena8c2a6b2008-04-17 17:55:30 +0200555 int val = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 unsigned long flags;
557
558 spin_lock_irqsave(&chip->reg_lock, flags);
559 if (chip->awacs_reg[6] & MASK_MIC_BOOST)
Risto Suominena8c2a6b2008-04-17 17:55:30 +0200560 val |= 2;
561 if (chip->awacs_reg[0] & MASK_GAINLINE)
562 val |= 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 spin_unlock_irqrestore(&chip->reg_lock, flags);
564 ucontrol->value.integer.value[0] = val;
565 return 0;
566}
567
Takashi Iwai65b29f52005-11-17 15:09:46 +0100568static int snd_pmac_screamer_mic_boost_put(struct snd_kcontrol *kcontrol,
569 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570{
Takashi Iwai65b29f52005-11-17 15:09:46 +0100571 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 int changed = 0;
573 int val0, val6;
574 unsigned long flags;
575
576 spin_lock_irqsave(&chip->reg_lock, flags);
577 val0 = chip->awacs_reg[0] & ~MASK_GAINLINE;
578 val6 = chip->awacs_reg[6] & ~MASK_MIC_BOOST;
Risto Suominena8c2a6b2008-04-17 17:55:30 +0200579 if (ucontrol->value.integer.value[0] & 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 val0 |= MASK_GAINLINE;
Risto Suominena8c2a6b2008-04-17 17:55:30 +0200581 if (ucontrol->value.integer.value[0] & 2)
582 val6 |= MASK_MIC_BOOST;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 if (val0 != chip->awacs_reg[0]) {
584 snd_pmac_awacs_write_reg(chip, 0, val0);
585 changed = 1;
586 }
587 if (val6 != chip->awacs_reg[6]) {
588 snd_pmac_awacs_write_reg(chip, 6, val6);
589 changed = 1;
590 }
591 spin_unlock_irqrestore(&chip->reg_lock, flags);
592 return changed;
593}
594
595/*
596 * lists of mixer elements
597 */
Takashi Iwai65b29f52005-11-17 15:09:46 +0100598static struct snd_kcontrol_new snd_pmac_awacs_mixers[] __initdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 AWACS_SWITCH("Master Capture Switch", 1, SHIFT_LOOPTHRU, 0),
Risto Suominena8c2a6b2008-04-17 17:55:30 +0200600 AWACS_VOLUME("Master Capture Volume", 0, 4, 0),
601/* AWACS_SWITCH("Unknown Playback Switch", 6, SHIFT_PAROUT0, 0), */
602};
603
604static struct snd_kcontrol_new snd_pmac_screamer_mixers_beige[] __initdata = {
605 AWACS_VOLUME("Master Playback Volume", 2, 6, 1),
606 AWACS_VOLUME("Play-through Playback Volume", 5, 6, 1),
607 AWACS_SWITCH("Line Capture Switch", 0, SHIFT_MUX_MIC, 0),
608 AWACS_SWITCH("CD Capture Switch", 0, SHIFT_MUX_LINE, 0),
609};
610
611static struct snd_kcontrol_new snd_pmac_screamer_mixers_imac[] __initdata = {
612 AWACS_VOLUME("Line out Playback Volume", 2, 6, 1),
613 AWACS_VOLUME("Master Playback Volume", 5, 6, 1),
614 AWACS_SWITCH("CD Capture Switch", 0, SHIFT_MUX_CD, 0),
615};
616
Risto Suominen4dbf95b2008-08-25 08:02:12 +0200617static struct snd_kcontrol_new snd_pmac_screamer_mixers_g4agp[] __initdata = {
618 AWACS_VOLUME("Line out Playback Volume", 2, 6, 1),
619 AWACS_VOLUME("Master Playback Volume", 5, 6, 1),
620 AWACS_SWITCH("CD Capture Switch", 0, SHIFT_MUX_CD, 0),
621 AWACS_SWITCH("Line Capture Switch", 0, SHIFT_MUX_MIC, 0),
622};
623
Risto Suominena8c2a6b2008-04-17 17:55:30 +0200624static struct snd_kcontrol_new snd_pmac_awacs_mixers_pmac7500[] __initdata = {
625 AWACS_VOLUME("Line out Playback Volume", 2, 6, 1),
626 AWACS_SWITCH("CD Capture Switch", 0, SHIFT_MUX_CD, 0),
627 AWACS_SWITCH("Line Capture Switch", 0, SHIFT_MUX_MIC, 0),
628};
629
630static struct snd_kcontrol_new snd_pmac_awacs_mixers_pmac[] __initdata = {
631 AWACS_VOLUME("Master Playback Volume", 2, 6, 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 AWACS_SWITCH("CD Capture Switch", 0, SHIFT_MUX_CD, 0),
633};
634
635/* FIXME: is this correct order?
636 * screamer (powerbook G3 pismo) seems to have different bits...
637 */
Takashi Iwai65b29f52005-11-17 15:09:46 +0100638static struct snd_kcontrol_new snd_pmac_awacs_mixers2[] __initdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 AWACS_SWITCH("Line Capture Switch", 0, SHIFT_MUX_LINE, 0),
640 AWACS_SWITCH("Mic Capture Switch", 0, SHIFT_MUX_MIC, 0),
641};
642
Takashi Iwai65b29f52005-11-17 15:09:46 +0100643static struct snd_kcontrol_new snd_pmac_screamer_mixers2[] __initdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 AWACS_SWITCH("Line Capture Switch", 0, SHIFT_MUX_MIC, 0),
645 AWACS_SWITCH("Mic Capture Switch", 0, SHIFT_MUX_LINE, 0),
646};
647
Takashi Iwai65b29f52005-11-17 15:09:46 +0100648static struct snd_kcontrol_new snd_pmac_awacs_master_sw __initdata =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649AWACS_SWITCH("Master Playback Switch", 1, SHIFT_HDMUTE, 1);
650
Risto Suominena8c2a6b2008-04-17 17:55:30 +0200651static struct snd_kcontrol_new snd_pmac_awacs_master_sw_imac __initdata =
652AWACS_SWITCH("Line out Playback Switch", 1, SHIFT_HDMUTE, 1);
653
Takashi Iwai65b29f52005-11-17 15:09:46 +0100654static struct snd_kcontrol_new snd_pmac_awacs_mic_boost[] __initdata = {
Risto Suominena8c2a6b2008-04-17 17:55:30 +0200655 AWACS_SWITCH("Mic Boost Capture Switch", 0, SHIFT_GAINLINE, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656};
657
Takashi Iwai65b29f52005-11-17 15:09:46 +0100658static struct snd_kcontrol_new snd_pmac_screamer_mic_boost[] __initdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Risto Suominena8c2a6b2008-04-17 17:55:30 +0200660 .name = "Mic Boost Capture Volume",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 .info = snd_pmac_screamer_mic_boost_info,
662 .get = snd_pmac_screamer_mic_boost_get,
663 .put = snd_pmac_screamer_mic_boost_put,
664 },
665};
666
Risto Suominena8c2a6b2008-04-17 17:55:30 +0200667static struct snd_kcontrol_new snd_pmac_awacs_mic_boost_pmac7500[] __initdata =
668{
669 AWACS_SWITCH("Line Boost Capture Switch", 0, SHIFT_GAINLINE, 0),
670};
671
672static struct snd_kcontrol_new snd_pmac_screamer_mic_boost_beige[] __initdata =
673{
674 AWACS_SWITCH("Line Boost Capture Switch", 0, SHIFT_GAINLINE, 0),
675 AWACS_SWITCH("CD Boost Capture Switch", 6, SHIFT_MIC_BOOST, 0),
676};
677
678static struct snd_kcontrol_new snd_pmac_screamer_mic_boost_imac[] __initdata =
679{
680 AWACS_SWITCH("Line Boost Capture Switch", 0, SHIFT_GAINLINE, 0),
681 AWACS_SWITCH("Mic Boost Capture Switch", 6, SHIFT_MIC_BOOST, 0),
682};
683
Takashi Iwai65b29f52005-11-17 15:09:46 +0100684static struct snd_kcontrol_new snd_pmac_awacs_speaker_vol[] __initdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 AWACS_VOLUME("PC Speaker Playback Volume", 4, 6, 1),
686};
Risto Suominena8c2a6b2008-04-17 17:55:30 +0200687
Takashi Iwai65b29f52005-11-17 15:09:46 +0100688static struct snd_kcontrol_new snd_pmac_awacs_speaker_sw __initdata =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689AWACS_SWITCH("PC Speaker Playback Switch", 1, SHIFT_SPKMUTE, 1);
690
Risto Suominen030b6552008-08-25 08:04:23 +0200691static struct snd_kcontrol_new snd_pmac_awacs_speaker_sw_imac1 __initdata =
692AWACS_SWITCH("PC Speaker Playback Switch", 1, SHIFT_PAROUT1, 1);
693
694static struct snd_kcontrol_new snd_pmac_awacs_speaker_sw_imac2 __initdata =
Risto Suominena8c2a6b2008-04-17 17:55:30 +0200695AWACS_SWITCH("PC Speaker Playback Switch", 1, SHIFT_PAROUT1, 0);
696
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
698/*
699 * add new mixer elements to the card
700 */
Risto Suominen7ae44cf2008-04-16 19:39:27 +0200701static int build_mixers(struct snd_pmac *chip, int nums,
702 struct snd_kcontrol_new *mixers)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703{
704 int i, err;
705
706 for (i = 0; i < nums; i++) {
Risto Suominen7ae44cf2008-04-16 19:39:27 +0200707 err = snd_ctl_add(chip->card, snd_ctl_new1(&mixers[i], chip));
708 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 return err;
710 }
711 return 0;
712}
713
714
715/*
716 * restore all registers
717 */
Takashi Iwai65b29f52005-11-17 15:09:46 +0100718static void awacs_restore_all_regs(struct snd_pmac *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719{
720 snd_pmac_awacs_write_noreg(chip, 0, chip->awacs_reg[0]);
721 snd_pmac_awacs_write_noreg(chip, 1, chip->awacs_reg[1]);
722 snd_pmac_awacs_write_noreg(chip, 2, chip->awacs_reg[2]);
723 snd_pmac_awacs_write_noreg(chip, 4, chip->awacs_reg[4]);
724 if (chip->model == PMAC_SCREAMER) {
725 snd_pmac_awacs_write_noreg(chip, 5, chip->awacs_reg[5]);
726 snd_pmac_awacs_write_noreg(chip, 6, chip->awacs_reg[6]);
727 snd_pmac_awacs_write_noreg(chip, 7, chip->awacs_reg[7]);
728 }
729}
730
Benjamin Herrenschmidt8c870932005-06-27 14:36:34 -0700731#ifdef CONFIG_PM
Takashi Iwai65b29f52005-11-17 15:09:46 +0100732static void snd_pmac_awacs_suspend(struct snd_pmac *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733{
734 snd_pmac_awacs_write_noreg(chip, 1, (chip->awacs_reg[1]
735 | MASK_AMUTE | MASK_CMUTE));
736}
737
Takashi Iwai65b29f52005-11-17 15:09:46 +0100738static void snd_pmac_awacs_resume(struct snd_pmac *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739{
740 if (machine_is_compatible("PowerBook3,1")
741 || machine_is_compatible("PowerBook3,2")) {
Nishanth Aravamudan989a0b22005-07-09 10:53:24 +0200742 msleep(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 snd_pmac_awacs_write_reg(chip, 1,
744 chip->awacs_reg[1] & ~MASK_PAROUT);
Nishanth Aravamudan989a0b22005-07-09 10:53:24 +0200745 msleep(300);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 }
747
748 awacs_restore_all_regs(chip);
749 if (chip->model == PMAC_SCREAMER) {
750 /* reset power bits in reg 6 */
751 mdelay(5);
752 snd_pmac_awacs_write_noreg(chip, 6, chip->awacs_reg[6]);
753 }
754 screamer_recalibrate(chip);
755#ifdef PMAC_AMP_AVAIL
756 if (chip->mixer_data) {
Takashi Iwai65b29f52005-11-17 15:09:46 +0100757 struct awacs_amp *amp = chip->mixer_data;
Risto Suominen7ae44cf2008-04-16 19:39:27 +0200758 awacs_amp_set_vol(amp, 0,
759 amp->amp_vol[0][0], amp->amp_vol[0][1], 0);
760 awacs_amp_set_vol(amp, 1,
761 amp->amp_vol[1][0], amp->amp_vol[1][1], 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 awacs_amp_set_tone(amp, amp->amp_tone[0], amp->amp_tone[1]);
763 awacs_amp_set_master(amp, amp->amp_master);
764 }
765#endif
766}
Benjamin Herrenschmidt8c870932005-06-27 14:36:34 -0700767#endif /* CONFIG_PM */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
Risto Suominena8c2a6b2008-04-17 17:55:30 +0200769#define IS_PM7500 (machine_is_compatible("AAPL,7500"))
Risto Suominenb0a8a8f2009-01-20 22:01:13 +0200770#define IS_PM5500 (machine_is_compatible("AAPL,e411"))
Risto Suominena8c2a6b2008-04-17 17:55:30 +0200771#define IS_BEIGE (machine_is_compatible("AAPL,Gossamer"))
Risto Suominen030b6552008-08-25 08:04:23 +0200772#define IS_IMAC1 (machine_is_compatible("PowerMac2,1"))
773#define IS_IMAC2 (machine_is_compatible("PowerMac2,2") \
Risto Suominena8c2a6b2008-04-17 17:55:30 +0200774 || machine_is_compatible("PowerMac4,1"))
Risto Suominen4dbf95b2008-08-25 08:02:12 +0200775#define IS_G4AGP (machine_is_compatible("PowerMac3,1"))
Risto Suominen573934b2009-01-20 22:01:14 +0200776#define IS_LOMBARD (machine_is_compatible("PowerBook1,1"))
Risto Suominena8c2a6b2008-04-17 17:55:30 +0200777
Risto Suominen030b6552008-08-25 08:04:23 +0200778static int imac1, imac2;
Risto Suominena8c2a6b2008-04-17 17:55:30 +0200779
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780#ifdef PMAC_SUPPORT_AUTOMUTE
781/*
782 * auto-mute stuffs
783 */
Takashi Iwai65b29f52005-11-17 15:09:46 +0100784static int snd_pmac_awacs_detect_headphone(struct snd_pmac *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785{
786 return (in_le32(&chip->awacs->codec_stat) & chip->hp_stat_mask) ? 1 : 0;
787}
788
789#ifdef PMAC_AMP_AVAIL
Takashi Iwai65b29f52005-11-17 15:09:46 +0100790static int toggle_amp_mute(struct awacs_amp *amp, int index, int mute)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791{
792 int vol[2];
793 vol[0] = amp->amp_vol[index][0] & 31;
794 vol[1] = amp->amp_vol[index][1] & 31;
795 if (mute) {
796 vol[0] |= 32;
797 vol[1] |= 32;
798 }
799 return awacs_amp_set_vol(amp, index, vol[0], vol[1], 1);
800}
801#endif
802
Takashi Iwai65b29f52005-11-17 15:09:46 +0100803static void snd_pmac_awacs_update_automute(struct snd_pmac *chip, int do_notify)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804{
805 if (chip->auto_mute) {
806#ifdef PMAC_AMP_AVAIL
807 if (chip->mixer_data) {
Takashi Iwai65b29f52005-11-17 15:09:46 +0100808 struct awacs_amp *amp = chip->mixer_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 int changed;
810 if (snd_pmac_awacs_detect_headphone(chip)) {
811 changed = toggle_amp_mute(amp, AMP_CH_HD, 0);
812 changed |= toggle_amp_mute(amp, AMP_CH_SPK, 1);
813 } else {
814 changed = toggle_amp_mute(amp, AMP_CH_HD, 1);
815 changed |= toggle_amp_mute(amp, AMP_CH_SPK, 0);
816 }
817 if (do_notify && ! changed)
818 return;
819 } else
820#endif
821 {
Risto Suominena8c2a6b2008-04-17 17:55:30 +0200822 int reg = chip->awacs_reg[1]
823 | (MASK_HDMUTE | MASK_SPKMUTE);
Risto Suominen030b6552008-08-25 08:04:23 +0200824 if (imac1) {
825 reg &= ~MASK_SPKMUTE;
826 reg |= MASK_PAROUT1;
827 } else if (imac2) {
Risto Suominena8c2a6b2008-04-17 17:55:30 +0200828 reg &= ~MASK_SPKMUTE;
829 reg &= ~MASK_PAROUT1;
830 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 if (snd_pmac_awacs_detect_headphone(chip))
832 reg &= ~MASK_HDMUTE;
Risto Suominen030b6552008-08-25 08:04:23 +0200833 else if (imac1)
834 reg &= ~MASK_PAROUT1;
835 else if (imac2)
Risto Suominena8c2a6b2008-04-17 17:55:30 +0200836 reg |= MASK_PAROUT1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 else
838 reg &= ~MASK_SPKMUTE;
839 if (do_notify && reg == chip->awacs_reg[1])
840 return;
841 snd_pmac_awacs_write_reg(chip, 1, reg);
842 }
843 if (do_notify) {
844 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
845 &chip->master_sw_ctl->id);
846 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
847 &chip->speaker_sw_ctl->id);
848 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
849 &chip->hp_detect_ctl->id);
850 }
851 }
852}
853#endif /* PMAC_SUPPORT_AUTOMUTE */
854
855
856/*
857 * initialize chip
858 */
859int __init
Takashi Iwai65b29f52005-11-17 15:09:46 +0100860snd_pmac_awacs_init(struct snd_pmac *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861{
Risto Suominena8c2a6b2008-04-17 17:55:30 +0200862 int pm7500 = IS_PM7500;
Risto Suominenb0a8a8f2009-01-20 22:01:13 +0200863 int pm5500 = IS_PM5500;
Risto Suominena8c2a6b2008-04-17 17:55:30 +0200864 int beige = IS_BEIGE;
Risto Suominen4dbf95b2008-08-25 08:02:12 +0200865 int g4agp = IS_G4AGP;
Risto Suominen573934b2009-01-20 22:01:14 +0200866 int lombard = IS_LOMBARD;
Risto Suominen030b6552008-08-25 08:04:23 +0200867 int imac;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 int err, vol;
869
Risto Suominen030b6552008-08-25 08:04:23 +0200870 imac1 = IS_IMAC1;
871 imac2 = IS_IMAC2;
872 imac = imac1 || imac2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 /* looks like MASK_GAINLINE triggers something, so we set here
874 * as start-up
875 */
876 chip->awacs_reg[0] = MASK_MUX_CD | 0xff | MASK_GAINLINE;
877 chip->awacs_reg[1] = MASK_CMUTE | MASK_AMUTE;
878 /* FIXME: Only machines with external SRS module need MASK_PAROUT */
879 if (chip->has_iic || chip->device_id == 0x5 ||
Risto Suominen7ae44cf2008-04-16 19:39:27 +0200880 /* chip->_device_id == 0x8 || */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 chip->device_id == 0xb)
882 chip->awacs_reg[1] |= MASK_PAROUT;
883 /* get default volume from nvram */
884 // vol = (~nvram_read_byte(0x1308) & 7) << 1;
885 // vol = ((pmac_xpram_read( 8 ) & 7 ) << 1 );
886 vol = 0x0f; /* no, on alsa, muted as default */
887 vol = vol + (vol << 6);
888 chip->awacs_reg[2] = vol;
889 chip->awacs_reg[4] = vol;
890 if (chip->model == PMAC_SCREAMER) {
Risto Suominen7ae44cf2008-04-16 19:39:27 +0200891 /* FIXME: screamer has loopthru vol control */
892 chip->awacs_reg[5] = vol;
893 /* FIXME: maybe should be vol << 3 for PCMCIA speaker */
894 chip->awacs_reg[6] = MASK_MIC_BOOST;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 chip->awacs_reg[7] = 0;
896 }
897
898 awacs_restore_all_regs(chip);
899 chip->manufacturer = (in_le32(&chip->awacs->codec_stat) >> 8) & 0xf;
900 screamer_recalibrate(chip);
901
902 chip->revision = (in_le32(&chip->awacs->codec_stat) >> 12) & 0xf;
903#ifdef PMAC_AMP_AVAIL
904 if (chip->revision == 3 && chip->has_iic && CHECK_CUDA_AMP()) {
Panagiotis Issaris59feddb2006-07-25 15:28:03 +0200905 struct awacs_amp *amp = kzalloc(sizeof(*amp), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 if (! amp)
907 return -ENOMEM;
908 chip->mixer_data = amp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 chip->mixer_free = awacs_amp_free;
Risto Suominen7ae44cf2008-04-16 19:39:27 +0200910 /* mute and zero vol */
911 awacs_amp_set_vol(amp, 0, 63, 63, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 awacs_amp_set_vol(amp, 1, 63, 63, 0);
913 awacs_amp_set_tone(amp, 7, 7); /* 0 dB */
914 awacs_amp_set_master(amp, 79); /* 0 dB */
915 }
916#endif /* PMAC_AMP_AVAIL */
917
918 if (chip->hp_stat_mask == 0) {
919 /* set headphone-jack detection bit */
920 switch (chip->model) {
921 case PMAC_AWACS:
Risto Suominenb0a8a8f2009-01-20 22:01:13 +0200922 chip->hp_stat_mask = pm7500 || pm5500 ? MASK_HDPCONN
Risto Suominena8c2a6b2008-04-17 17:55:30 +0200923 : MASK_LOCONN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 break;
925 case PMAC_SCREAMER:
926 switch (chip->device_id) {
927 case 0x08:
Risto Suominena8c2a6b2008-04-17 17:55:30 +0200928 case 0x0B:
929 chip->hp_stat_mask = imac
930 ? MASK_LOCONN_IMAC |
931 MASK_HDPLCONN_IMAC |
932 MASK_HDPRCONN_IMAC
933 : MASK_HDPCONN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 break;
935 case 0x00:
936 case 0x05:
Risto Suominena8c2a6b2008-04-17 17:55:30 +0200937 chip->hp_stat_mask = MASK_LOCONN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 break;
939 default:
Risto Suominena8c2a6b2008-04-17 17:55:30 +0200940 chip->hp_stat_mask = MASK_HDPCONN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 break;
942 }
943 break;
944 default:
945 snd_BUG();
946 break;
947 }
948 }
949
950 /*
951 * build mixers
952 */
953 strcpy(chip->card->mixername, "PowerMac AWACS");
954
Risto Suominen7ae44cf2008-04-16 19:39:27 +0200955 err = build_mixers(chip, ARRAY_SIZE(snd_pmac_awacs_mixers),
956 snd_pmac_awacs_mixers);
957 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 return err;
Risto Suominen4dbf95b2008-08-25 08:02:12 +0200959 if (beige || g4agp)
Risto Suominena8c2a6b2008-04-17 17:55:30 +0200960 ;
Risto Suominenb0a8a8f2009-01-20 22:01:13 +0200961 else if (chip->model == PMAC_SCREAMER || pm5500)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 err = build_mixers(chip, ARRAY_SIZE(snd_pmac_screamer_mixers2),
963 snd_pmac_screamer_mixers2);
Risto Suominena8c2a6b2008-04-17 17:55:30 +0200964 else if (!pm7500)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 err = build_mixers(chip, ARRAY_SIZE(snd_pmac_awacs_mixers2),
966 snd_pmac_awacs_mixers2);
967 if (err < 0)
968 return err;
Risto Suominena8c2a6b2008-04-17 17:55:30 +0200969 if (pm7500)
970 err = build_mixers(chip,
971 ARRAY_SIZE(snd_pmac_awacs_mixers_pmac7500),
972 snd_pmac_awacs_mixers_pmac7500);
973 else if (beige)
974 err = build_mixers(chip,
975 ARRAY_SIZE(snd_pmac_screamer_mixers_beige),
976 snd_pmac_screamer_mixers_beige);
Risto Suominen573934b2009-01-20 22:01:14 +0200977 else if (imac || lombard)
Risto Suominena8c2a6b2008-04-17 17:55:30 +0200978 err = build_mixers(chip,
979 ARRAY_SIZE(snd_pmac_screamer_mixers_imac),
980 snd_pmac_screamer_mixers_imac);
Risto Suominen4dbf95b2008-08-25 08:02:12 +0200981 else if (g4agp)
982 err = build_mixers(chip,
983 ARRAY_SIZE(snd_pmac_screamer_mixers_g4agp),
984 snd_pmac_screamer_mixers_g4agp);
Risto Suominena8c2a6b2008-04-17 17:55:30 +0200985 else
986 err = build_mixers(chip,
987 ARRAY_SIZE(snd_pmac_awacs_mixers_pmac),
988 snd_pmac_awacs_mixers_pmac);
989 if (err < 0)
990 return err;
Risto Suominen573934b2009-01-20 22:01:14 +0200991 chip->master_sw_ctl = snd_ctl_new1((pm7500 || imac || g4agp || lombard)
Risto Suominena8c2a6b2008-04-17 17:55:30 +0200992 ? &snd_pmac_awacs_master_sw_imac
993 : &snd_pmac_awacs_master_sw, chip);
Risto Suominen7ae44cf2008-04-16 19:39:27 +0200994 err = snd_ctl_add(chip->card, chip->master_sw_ctl);
995 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 return err;
997#ifdef PMAC_AMP_AVAIL
998 if (chip->mixer_data) {
999 /* use amplifier. the signal is connected from route A
1000 * to the amp. the amp has its headphone and speaker
1001 * volumes and mute switches, so we use them instead of
1002 * screamer registers.
1003 * in this case, it seems the route C is not used.
1004 */
Risto Suominen7ae44cf2008-04-16 19:39:27 +02001005 err = build_mixers(chip, ARRAY_SIZE(snd_pmac_awacs_amp_vol),
1006 snd_pmac_awacs_amp_vol);
1007 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 return err;
1009 /* overwrite */
Risto Suominen7ae44cf2008-04-16 19:39:27 +02001010 chip->master_sw_ctl = snd_ctl_new1(&snd_pmac_awacs_amp_hp_sw,
1011 chip);
1012 err = snd_ctl_add(chip->card, chip->master_sw_ctl);
1013 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 return err;
Risto Suominen7ae44cf2008-04-16 19:39:27 +02001015 chip->speaker_sw_ctl = snd_ctl_new1(&snd_pmac_awacs_amp_spk_sw,
1016 chip);
1017 err = snd_ctl_add(chip->card, chip->speaker_sw_ctl);
1018 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 return err;
1020 } else
1021#endif /* PMAC_AMP_AVAIL */
1022 {
1023 /* route A = headphone, route C = speaker */
Risto Suominen7ae44cf2008-04-16 19:39:27 +02001024 err = build_mixers(chip, ARRAY_SIZE(snd_pmac_awacs_speaker_vol),
1025 snd_pmac_awacs_speaker_vol);
1026 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 return err;
Risto Suominen030b6552008-08-25 08:04:23 +02001028 chip->speaker_sw_ctl = snd_ctl_new1(imac1
1029 ? &snd_pmac_awacs_speaker_sw_imac1
1030 : imac2
1031 ? &snd_pmac_awacs_speaker_sw_imac2
Risto Suominena8c2a6b2008-04-17 17:55:30 +02001032 : &snd_pmac_awacs_speaker_sw, chip);
Risto Suominen7ae44cf2008-04-16 19:39:27 +02001033 err = snd_ctl_add(chip->card, chip->speaker_sw_ctl);
1034 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 return err;
1036 }
1037
Risto Suominen4dbf95b2008-08-25 08:02:12 +02001038 if (beige || g4agp)
Risto Suominena8c2a6b2008-04-17 17:55:30 +02001039 err = build_mixers(chip,
1040 ARRAY_SIZE(snd_pmac_screamer_mic_boost_beige),
1041 snd_pmac_screamer_mic_boost_beige);
1042 else if (imac)
1043 err = build_mixers(chip,
1044 ARRAY_SIZE(snd_pmac_screamer_mic_boost_imac),
1045 snd_pmac_screamer_mic_boost_imac);
1046 else if (chip->model == PMAC_SCREAMER)
1047 err = build_mixers(chip,
1048 ARRAY_SIZE(snd_pmac_screamer_mic_boost),
1049 snd_pmac_screamer_mic_boost);
1050 else if (pm7500)
1051 err = build_mixers(chip,
1052 ARRAY_SIZE(snd_pmac_awacs_mic_boost_pmac7500),
1053 snd_pmac_awacs_mic_boost_pmac7500);
1054 else
1055 err = build_mixers(chip, ARRAY_SIZE(snd_pmac_awacs_mic_boost),
1056 snd_pmac_awacs_mic_boost);
1057 if (err < 0)
1058 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059
1060 /*
1061 * set lowlevel callbacks
1062 */
1063 chip->set_format = snd_pmac_awacs_set_format;
Benjamin Herrenschmidt8c870932005-06-27 14:36:34 -07001064#ifdef CONFIG_PM
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 chip->suspend = snd_pmac_awacs_suspend;
1066 chip->resume = snd_pmac_awacs_resume;
1067#endif
1068#ifdef PMAC_SUPPORT_AUTOMUTE
Risto Suominen7ae44cf2008-04-16 19:39:27 +02001069 err = snd_pmac_add_automute(chip);
1070 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 return err;
1072 chip->detect_headphone = snd_pmac_awacs_detect_headphone;
1073 chip->update_automute = snd_pmac_awacs_update_automute;
1074 snd_pmac_awacs_update_automute(chip, 0); /* update the status only */
1075#endif
1076 if (chip->model == PMAC_SCREAMER) {
1077 snd_pmac_awacs_write_noreg(chip, 6, chip->awacs_reg[6]);
1078 snd_pmac_awacs_write_noreg(chip, 0, chip->awacs_reg[0]);
1079 }
1080
1081 return 0;
1082}