1/*
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
23#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
37struct awacs_amp {
38	unsigned char amp_master;
39	unsigned char amp_vol[2][2];
40	unsigned char amp_tone[2];
41};
42
43#define CHECK_CUDA_AMP() (sys_ctrler == SYS_CTRLER_CUDA)
44
45#endif /* PMAC_AMP_AVAIL */
46
47
48static void snd_pmac_screamer_wait(struct snd_pmac *chip)
49{
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
64snd_pmac_awacs_write(struct snd_pmac *chip, int val)
65{
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
80snd_pmac_awacs_write_reg(struct snd_pmac *chip, int reg, int val)
81{
82	snd_pmac_awacs_write(chip, val | (reg << 12));
83	chip->awacs_reg[reg] = val;
84}
85
86static void
87snd_pmac_awacs_write_noreg(struct snd_pmac *chip, int reg, int val)
88{
89	snd_pmac_awacs_write(chip, val | (reg << 12));
90}
91
92#ifdef CONFIG_PM
93/* Recalibrate chip */
94static void screamer_recalibrate(struct snd_pmac *chip)
95{
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 */
105		msleep(750);
106	snd_pmac_awacs_write_noreg(chip, 1,
107				   chip->awacs_reg[1] | MASK_RECALIBRATE |
108				   MASK_CMUTE | MASK_AMUTE);
109	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 */
121static void snd_pmac_awacs_set_format(struct snd_pmac *chip)
122{
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 */
135static int snd_pmac_awacs_info_volume(struct snd_kcontrol *kcontrol,
136				      struct snd_ctl_elem_info *uinfo)
137{
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}
144
145static int snd_pmac_awacs_get_volume(struct snd_kcontrol *kcontrol,
146				     struct snd_ctl_elem_value *ucontrol)
147{
148	struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
149	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
168static int snd_pmac_awacs_put_volume(struct snd_kcontrol *kcontrol,
169				     struct snd_ctl_elem_value *ucontrol)
170{
171	struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
172	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;
177	unsigned int vol[2];
178
179	vol[0] = ucontrol->value.integer.value[0];
180	vol[1] = ucontrol->value.integer.value[1];
181	if (vol[0] > 0x0f || vol[1] > 0x0f)
182		return -EINVAL;
183	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 */
211static int snd_pmac_awacs_get_switch(struct snd_kcontrol *kcontrol,
212				     struct snd_ctl_elem_value *ucontrol)
213{
214	struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
215	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
230static int snd_pmac_awacs_put_switch(struct snd_kcontrol *kcontrol,
231				     struct snd_ctl_elem_value *ucontrol)
232{
233	struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
234	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;
270	cuda_request(&req, NULL, 5, CUDA_PACKET, CUDA_GET_SET_IIC, 0x8a,
271			reg, val);
272	while (! req.complete)
273		cuda_poll();
274}
275
276/*
277 * level = 0 - 14, 7 = 0 dB
278 */
279static void awacs_amp_set_tone(struct awacs_amp *amp, int bass, int treble)
280{
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 */
293static int awacs_amp_set_vol(struct awacs_amp *amp, int index,
294			     int lvol, int rvol, int do_check)
295{
296	if (do_check && amp->amp_vol[index][0] == lvol &&
297			amp->amp_vol[index][1] == rvol)
298		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 */
309static void awacs_amp_set_master(struct awacs_amp *amp, int vol)
310{
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
319static void awacs_amp_free(struct snd_pmac *chip)
320{
321	struct awacs_amp *amp = chip->mixer_data;
322	if (!amp)
323		return;
324	kfree(amp);
325	chip->mixer_data = NULL;
326	chip->mixer_free = NULL;
327}
328
329
330/*
331 * mixer controls
332 */
333static int snd_pmac_awacs_info_volume_amp(struct snd_kcontrol *kcontrol,
334					  struct snd_ctl_elem_info *uinfo)
335{
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}
342
343static int snd_pmac_awacs_get_volume_amp(struct snd_kcontrol *kcontrol,
344					 struct snd_ctl_elem_value *ucontrol)
345{
346	struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
347	int index = kcontrol->private_value;
348	struct awacs_amp *amp = chip->mixer_data;
349
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
355static int snd_pmac_awacs_put_volume_amp(struct snd_kcontrol *kcontrol,
356					 struct snd_ctl_elem_value *ucontrol)
357{
358	struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
359	int index = kcontrol->private_value;
360	int vol[2];
361	struct awacs_amp *amp = chip->mixer_data;
362
363	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);
367	return awacs_amp_set_vol(amp, index, vol[0], vol[1], 1);
368}
369
370static int snd_pmac_awacs_get_switch_amp(struct snd_kcontrol *kcontrol,
371					 struct snd_ctl_elem_value *ucontrol)
372{
373	struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
374	int index = kcontrol->private_value;
375	struct awacs_amp *amp = chip->mixer_data;
376
377	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;
381	return 0;
382}
383
384static int snd_pmac_awacs_put_switch_amp(struct snd_kcontrol *kcontrol,
385					 struct snd_ctl_elem_value *ucontrol)
386{
387	struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
388	int index = kcontrol->private_value;
389	int vol[2];
390	struct awacs_amp *amp = chip->mixer_data;
391
392	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);
396	return awacs_amp_set_vol(amp, index, vol[0], vol[1], 1);
397}
398
399static int snd_pmac_awacs_info_tone_amp(struct snd_kcontrol *kcontrol,
400					struct snd_ctl_elem_info *uinfo)
401{
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}
408
409static int snd_pmac_awacs_get_tone_amp(struct snd_kcontrol *kcontrol,
410				       struct snd_ctl_elem_value *ucontrol)
411{
412	struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
413	int index = kcontrol->private_value;
414	struct awacs_amp *amp = chip->mixer_data;
415
416	ucontrol->value.integer.value[0] = amp->amp_tone[index];
417	return 0;
418}
419
420static int snd_pmac_awacs_put_tone_amp(struct snd_kcontrol *kcontrol,
421				       struct snd_ctl_elem_value *ucontrol)
422{
423	struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
424	int index = kcontrol->private_value;
425	struct awacs_amp *amp = chip->mixer_data;
426	unsigned int val;
427
428	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;
433		awacs_amp_set_tone(amp, amp->amp_tone[0], amp->amp_tone[1]);
434		return 1;
435	}
436	return 0;
437}
438
439static int snd_pmac_awacs_info_master_amp(struct snd_kcontrol *kcontrol,
440					  struct snd_ctl_elem_info *uinfo)
441{
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}
448
449static int snd_pmac_awacs_get_master_amp(struct snd_kcontrol *kcontrol,
450					 struct snd_ctl_elem_value *ucontrol)
451{
452	struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
453	struct awacs_amp *amp = chip->mixer_data;
454
455	ucontrol->value.integer.value[0] = amp->amp_master;
456	return 0;
457}
458
459static int snd_pmac_awacs_put_master_amp(struct snd_kcontrol *kcontrol,
460					 struct snd_ctl_elem_value *ucontrol)
461{
462	struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
463	struct awacs_amp *amp = chip->mixer_data;
464	unsigned int val;
465
466	val = ucontrol->value.integer.value[0];
467	if (val > 99)
468		return -EINVAL;
469	if (val != amp->amp_master) {
470		amp->amp_master = val;
471		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
480static struct snd_kcontrol_new snd_pmac_awacs_amp_vol[] __devinitdata = {
481	{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
482	  .name = "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
517static struct snd_kcontrol_new snd_pmac_awacs_amp_hp_sw __devinitdata = {
518	.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
526static struct snd_kcontrol_new snd_pmac_awacs_amp_spk_sw __devinitdata = {
527	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
528	.name = "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 */
541static int snd_pmac_screamer_mic_boost_info(struct snd_kcontrol *kcontrol,
542					    struct snd_ctl_elem_info *uinfo)
543{
544	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
545	uinfo->count = 1;
546	uinfo->value.integer.min = 0;
547	uinfo->value.integer.max = 3;
548	return 0;
549}
550
551static int snd_pmac_screamer_mic_boost_get(struct snd_kcontrol *kcontrol,
552					   struct snd_ctl_elem_value *ucontrol)
553{
554	struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
555	int val = 0;
556	unsigned long flags;
557
558	spin_lock_irqsave(&chip->reg_lock, flags);
559	if (chip->awacs_reg[6] & MASK_MIC_BOOST)
560		val |= 2;
561	if (chip->awacs_reg[0] & MASK_GAINLINE)
562		val |= 1;
563	spin_unlock_irqrestore(&chip->reg_lock, flags);
564	ucontrol->value.integer.value[0] = val;
565	return 0;
566}
567
568static int snd_pmac_screamer_mic_boost_put(struct snd_kcontrol *kcontrol,
569					   struct snd_ctl_elem_value *ucontrol)
570{
571	struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
572	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;
579	if (ucontrol->value.integer.value[0] & 1)
580		val0 |= MASK_GAINLINE;
581	if (ucontrol->value.integer.value[0] & 2)
582		val6 |= MASK_MIC_BOOST;
583	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 */
598static struct snd_kcontrol_new snd_pmac_awacs_mixers[] __devinitdata = {
599	AWACS_SWITCH("Master Capture Switch", 1, SHIFT_LOOPTHRU, 0),
600	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[] __devinitdata = {
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_lo[] __devinitdata = {
612	AWACS_VOLUME("Line out Playback Volume", 2, 6, 1),
613};
614
615static struct snd_kcontrol_new snd_pmac_screamer_mixers_imac[] __devinitdata = {
616	AWACS_VOLUME("Play-through Playback Volume", 5, 6, 1),
617	AWACS_SWITCH("CD Capture Switch", 0, SHIFT_MUX_CD, 0),
618};
619
620static struct snd_kcontrol_new snd_pmac_screamer_mixers_g4agp[] __devinitdata = {
621	AWACS_VOLUME("Line out Playback Volume", 2, 6, 1),
622	AWACS_VOLUME("Master Playback Volume", 5, 6, 1),
623	AWACS_SWITCH("CD Capture Switch", 0, SHIFT_MUX_CD, 0),
624	AWACS_SWITCH("Line Capture Switch", 0, SHIFT_MUX_MIC, 0),
625};
626
627static struct snd_kcontrol_new snd_pmac_awacs_mixers_pmac7500[] __devinitdata = {
628	AWACS_VOLUME("Line out Playback Volume", 2, 6, 1),
629	AWACS_SWITCH("CD Capture Switch", 0, SHIFT_MUX_CD, 0),
630	AWACS_SWITCH("Line Capture Switch", 0, SHIFT_MUX_MIC, 0),
631};
632
633static struct snd_kcontrol_new snd_pmac_awacs_mixers_pmac5500[] __devinitdata = {
634	AWACS_VOLUME("Headphone Playback Volume", 2, 6, 1),
635};
636
637static struct snd_kcontrol_new snd_pmac_awacs_mixers_pmac[] __devinitdata = {
638	AWACS_VOLUME("Master Playback Volume", 2, 6, 1),
639	AWACS_SWITCH("CD Capture Switch", 0, SHIFT_MUX_CD, 0),
640};
641
642static struct snd_kcontrol_new snd_pmac_awacs_mixers2[] __devinitdata = {
643	AWACS_SWITCH("Line Capture Switch", 0, SHIFT_MUX_LINE, 0),
644	AWACS_SWITCH("Mic Capture Switch", 0, SHIFT_MUX_MIC, 0),
645};
646
647static struct snd_kcontrol_new snd_pmac_screamer_mixers2[] __devinitdata = {
648	AWACS_SWITCH("Line Capture Switch", 0, SHIFT_MUX_MIC, 0),
649	AWACS_SWITCH("Mic Capture Switch", 0, SHIFT_MUX_LINE, 0),
650};
651
652static struct snd_kcontrol_new snd_pmac_awacs_mixers2_pmac5500[] __devinitdata = {
653	AWACS_SWITCH("CD Capture Switch", 0, SHIFT_MUX_CD, 0),
654};
655
656static struct snd_kcontrol_new snd_pmac_awacs_master_sw __devinitdata =
657AWACS_SWITCH("Master Playback Switch", 1, SHIFT_HDMUTE, 1);
658
659static struct snd_kcontrol_new snd_pmac_awacs_master_sw_imac __devinitdata =
660AWACS_SWITCH("Line out Playback Switch", 1, SHIFT_HDMUTE, 1);
661
662static struct snd_kcontrol_new snd_pmac_awacs_master_sw_pmac5500 __devinitdata =
663AWACS_SWITCH("Headphone Playback Switch", 1, SHIFT_HDMUTE, 1);
664
665static struct snd_kcontrol_new snd_pmac_awacs_mic_boost[] __devinitdata = {
666	AWACS_SWITCH("Mic Boost Capture Switch", 0, SHIFT_GAINLINE, 0),
667};
668
669static struct snd_kcontrol_new snd_pmac_screamer_mic_boost[] __devinitdata = {
670	{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
671	  .name = "Mic Boost Capture Volume",
672	  .info = snd_pmac_screamer_mic_boost_info,
673	  .get = snd_pmac_screamer_mic_boost_get,
674	  .put = snd_pmac_screamer_mic_boost_put,
675	},
676};
677
678static struct snd_kcontrol_new snd_pmac_awacs_mic_boost_pmac7500[] __devinitdata =
679{
680	AWACS_SWITCH("Line Boost Capture Switch", 0, SHIFT_GAINLINE, 0),
681};
682
683static struct snd_kcontrol_new snd_pmac_screamer_mic_boost_beige[] __devinitdata =
684{
685	AWACS_SWITCH("Line Boost Capture Switch", 0, SHIFT_GAINLINE, 0),
686	AWACS_SWITCH("CD Boost Capture Switch", 6, SHIFT_MIC_BOOST, 0),
687};
688
689static struct snd_kcontrol_new snd_pmac_screamer_mic_boost_imac[] __devinitdata =
690{
691	AWACS_SWITCH("Line Boost Capture Switch", 0, SHIFT_GAINLINE, 0),
692	AWACS_SWITCH("Mic Boost Capture Switch", 6, SHIFT_MIC_BOOST, 0),
693};
694
695static struct snd_kcontrol_new snd_pmac_awacs_speaker_vol[] __devinitdata = {
696	AWACS_VOLUME("Speaker Playback Volume", 4, 6, 1),
697};
698
699static struct snd_kcontrol_new snd_pmac_awacs_speaker_sw __devinitdata =
700AWACS_SWITCH("Speaker Playback Switch", 1, SHIFT_SPKMUTE, 1);
701
702static struct snd_kcontrol_new snd_pmac_awacs_speaker_sw_imac1 __devinitdata =
703AWACS_SWITCH("Speaker Playback Switch", 1, SHIFT_PAROUT1, 1);
704
705static struct snd_kcontrol_new snd_pmac_awacs_speaker_sw_imac2 __devinitdata =
706AWACS_SWITCH("Speaker Playback Switch", 1, SHIFT_PAROUT1, 0);
707
708
709/*
710 * add new mixer elements to the card
711 */
712static int build_mixers(struct snd_pmac *chip, int nums,
713			struct snd_kcontrol_new *mixers)
714{
715	int i, err;
716
717	for (i = 0; i < nums; i++) {
718		err = snd_ctl_add(chip->card, snd_ctl_new1(&mixers[i], chip));
719		if (err < 0)
720			return err;
721	}
722	return 0;
723}
724
725
726/*
727 * restore all registers
728 */
729static void awacs_restore_all_regs(struct snd_pmac *chip)
730{
731	snd_pmac_awacs_write_noreg(chip, 0, chip->awacs_reg[0]);
732	snd_pmac_awacs_write_noreg(chip, 1, chip->awacs_reg[1]);
733	snd_pmac_awacs_write_noreg(chip, 2, chip->awacs_reg[2]);
734	snd_pmac_awacs_write_noreg(chip, 4, chip->awacs_reg[4]);
735	if (chip->model == PMAC_SCREAMER) {
736		snd_pmac_awacs_write_noreg(chip, 5, chip->awacs_reg[5]);
737		snd_pmac_awacs_write_noreg(chip, 6, chip->awacs_reg[6]);
738		snd_pmac_awacs_write_noreg(chip, 7, chip->awacs_reg[7]);
739	}
740}
741
742#ifdef CONFIG_PM
743static void snd_pmac_awacs_suspend(struct snd_pmac *chip)
744{
745	snd_pmac_awacs_write_noreg(chip, 1, (chip->awacs_reg[1]
746					     | MASK_AMUTE | MASK_CMUTE));
747}
748
749static void snd_pmac_awacs_resume(struct snd_pmac *chip)
750{
751	if (of_machine_is_compatible("PowerBook3,1")
752	    || of_machine_is_compatible("PowerBook3,2")) {
753		msleep(100);
754		snd_pmac_awacs_write_reg(chip, 1,
755			chip->awacs_reg[1] & ~MASK_PAROUT);
756		msleep(300);
757	}
758
759	awacs_restore_all_regs(chip);
760	if (chip->model == PMAC_SCREAMER) {
761		/* reset power bits in reg 6 */
762		mdelay(5);
763		snd_pmac_awacs_write_noreg(chip, 6, chip->awacs_reg[6]);
764	}
765	screamer_recalibrate(chip);
766#ifdef PMAC_AMP_AVAIL
767	if (chip->mixer_data) {
768		struct awacs_amp *amp = chip->mixer_data;
769		awacs_amp_set_vol(amp, 0,
770				  amp->amp_vol[0][0], amp->amp_vol[0][1], 0);
771		awacs_amp_set_vol(amp, 1,
772				  amp->amp_vol[1][0], amp->amp_vol[1][1], 0);
773		awacs_amp_set_tone(amp, amp->amp_tone[0], amp->amp_tone[1]);
774		awacs_amp_set_master(amp, amp->amp_master);
775	}
776#endif
777}
778#endif /* CONFIG_PM */
779
780#define IS_PM7500 (of_machine_is_compatible("AAPL,7500") \
781		|| of_machine_is_compatible("AAPL,8500") \
782		|| of_machine_is_compatible("AAPL,9500"))
783#define IS_PM5500 (of_machine_is_compatible("AAPL,e411"))
784#define IS_BEIGE (of_machine_is_compatible("AAPL,Gossamer"))
785#define IS_IMAC1 (of_machine_is_compatible("PowerMac2,1"))
786#define IS_IMAC2 (of_machine_is_compatible("PowerMac2,2") \
787		|| of_machine_is_compatible("PowerMac4,1"))
788#define IS_G4AGP (of_machine_is_compatible("PowerMac3,1"))
789#define IS_LOMBARD (of_machine_is_compatible("PowerBook1,1"))
790
791static int imac1, imac2;
792
793#ifdef PMAC_SUPPORT_AUTOMUTE
794/*
795 * auto-mute stuffs
796 */
797static int snd_pmac_awacs_detect_headphone(struct snd_pmac *chip)
798{
799	return (in_le32(&chip->awacs->codec_stat) & chip->hp_stat_mask) ? 1 : 0;
800}
801
802#ifdef PMAC_AMP_AVAIL
803static int toggle_amp_mute(struct awacs_amp *amp, int index, int mute)
804{
805	int vol[2];
806	vol[0] = amp->amp_vol[index][0] & 31;
807	vol[1] = amp->amp_vol[index][1] & 31;
808	if (mute) {
809		vol[0] |= 32;
810		vol[1] |= 32;
811	}
812	return awacs_amp_set_vol(amp, index, vol[0], vol[1], 1);
813}
814#endif
815
816static void snd_pmac_awacs_update_automute(struct snd_pmac *chip, int do_notify)
817{
818	if (chip->auto_mute) {
819#ifdef PMAC_AMP_AVAIL
820		if (chip->mixer_data) {
821			struct awacs_amp *amp = chip->mixer_data;
822			int changed;
823			if (snd_pmac_awacs_detect_headphone(chip)) {
824				changed = toggle_amp_mute(amp, AMP_CH_HD, 0);
825				changed |= toggle_amp_mute(amp, AMP_CH_SPK, 1);
826			} else {
827				changed = toggle_amp_mute(amp, AMP_CH_HD, 1);
828				changed |= toggle_amp_mute(amp, AMP_CH_SPK, 0);
829			}
830			if (do_notify && ! changed)
831				return;
832		} else
833#endif
834		{
835			int reg = chip->awacs_reg[1]
836				| (MASK_HDMUTE | MASK_SPKMUTE);
837			if (imac1) {
838				reg &= ~MASK_SPKMUTE;
839				reg |= MASK_PAROUT1;
840			} else if (imac2) {
841				reg &= ~MASK_SPKMUTE;
842				reg &= ~MASK_PAROUT1;
843			}
844			if (snd_pmac_awacs_detect_headphone(chip))
845				reg &= ~MASK_HDMUTE;
846			else if (imac1)
847				reg &= ~MASK_PAROUT1;
848			else if (imac2)
849				reg |= MASK_PAROUT1;
850			else
851				reg &= ~MASK_SPKMUTE;
852			if (do_notify && reg == chip->awacs_reg[1])
853				return;
854			snd_pmac_awacs_write_reg(chip, 1, reg);
855		}
856		if (do_notify) {
857			snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
858				       &chip->master_sw_ctl->id);
859			snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
860				       &chip->speaker_sw_ctl->id);
861			snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
862				       &chip->hp_detect_ctl->id);
863		}
864	}
865}
866#endif /* PMAC_SUPPORT_AUTOMUTE */
867
868
869/*
870 * initialize chip
871 */
872int __devinit
873snd_pmac_awacs_init(struct snd_pmac *chip)
874{
875	int pm7500 = IS_PM7500;
876	int pm5500 = IS_PM5500;
877	int beige = IS_BEIGE;
878	int g4agp = IS_G4AGP;
879	int lombard = IS_LOMBARD;
880	int imac;
881	int err, vol;
882	struct snd_kcontrol *vmaster_sw, *vmaster_vol;
883	struct snd_kcontrol *master_vol, *speaker_vol;
884
885	imac1 = IS_IMAC1;
886	imac2 = IS_IMAC2;
887	imac = imac1 || imac2;
888	/* looks like MASK_GAINLINE triggers something, so we set here
889	 * as start-up
890	 */
891	chip->awacs_reg[0] = MASK_MUX_CD | 0xff | MASK_GAINLINE;
892	chip->awacs_reg[1] = MASK_CMUTE | MASK_AMUTE;
893	if (chip->has_iic || chip->device_id == 0x5 ||
894	    /* chip->_device_id == 0x8 || */
895	    chip->device_id == 0xb)
896		chip->awacs_reg[1] |= MASK_PAROUT;
897	/* get default volume from nvram */
898	// vol = (~nvram_read_byte(0x1308) & 7) << 1;
899	// vol = ((pmac_xpram_read( 8 ) & 7 ) << 1 );
900	vol = 0x0f; /* no, on alsa, muted as default */
901	vol = vol + (vol << 6);
902	chip->awacs_reg[2] = vol;
903	chip->awacs_reg[4] = vol;
904	if (chip->model == PMAC_SCREAMER) {
905		chip->awacs_reg[5] = vol;
906		chip->awacs_reg[6] = MASK_MIC_BOOST;
907		chip->awacs_reg[7] = 0;
908	}
909
910	awacs_restore_all_regs(chip);
911	chip->manufacturer = (in_le32(&chip->awacs->codec_stat) >> 8) & 0xf;
912	screamer_recalibrate(chip);
913
914	chip->revision = (in_le32(&chip->awacs->codec_stat) >> 12) & 0xf;
915#ifdef PMAC_AMP_AVAIL
916	if (chip->revision == 3 && chip->has_iic && CHECK_CUDA_AMP()) {
917		struct awacs_amp *amp = kzalloc(sizeof(*amp), GFP_KERNEL);
918		if (! amp)
919			return -ENOMEM;
920		chip->mixer_data = amp;
921		chip->mixer_free = awacs_amp_free;
922		/* mute and zero vol */
923		awacs_amp_set_vol(amp, 0, 63, 63, 0);
924		awacs_amp_set_vol(amp, 1, 63, 63, 0);
925		awacs_amp_set_tone(amp, 7, 7); /* 0 dB */
926		awacs_amp_set_master(amp, 79); /* 0 dB */
927	}
928#endif /* PMAC_AMP_AVAIL */
929
930	if (chip->hp_stat_mask == 0) {
931		/* set headphone-jack detection bit */
932		switch (chip->model) {
933		case PMAC_AWACS:
934			chip->hp_stat_mask = pm7500 || pm5500 ? MASK_HDPCONN
935				: MASK_LOCONN;
936			break;
937		case PMAC_SCREAMER:
938			switch (chip->device_id) {
939			case 0x08:
940			case 0x0B:
941				chip->hp_stat_mask = imac
942					? MASK_LOCONN_IMAC |
943					MASK_HDPLCONN_IMAC |
944					MASK_HDPRCONN_IMAC
945					: MASK_HDPCONN;
946				break;
947			case 0x00:
948			case 0x05:
949				chip->hp_stat_mask = MASK_LOCONN;
950				break;
951			default:
952				chip->hp_stat_mask = MASK_HDPCONN;
953				break;
954			}
955			break;
956		default:
957			snd_BUG();
958			break;
959		}
960	}
961
962	/*
963	 * build mixers
964	 */
965	strcpy(chip->card->mixername, "PowerMac AWACS");
966
967	err = build_mixers(chip, ARRAY_SIZE(snd_pmac_awacs_mixers),
968				snd_pmac_awacs_mixers);
969	if (err < 0)
970		return err;
971	if (beige || g4agp)
972		;
973	else if (chip->model == PMAC_SCREAMER || pm5500)
974		err = build_mixers(chip, ARRAY_SIZE(snd_pmac_screamer_mixers2),
975				   snd_pmac_screamer_mixers2);
976	else if (!pm7500)
977		err = build_mixers(chip, ARRAY_SIZE(snd_pmac_awacs_mixers2),
978				   snd_pmac_awacs_mixers2);
979	if (err < 0)
980		return err;
981	if (pm5500) {
982		err = build_mixers(chip,
983				   ARRAY_SIZE(snd_pmac_awacs_mixers2_pmac5500),
984				   snd_pmac_awacs_mixers2_pmac5500);
985		if (err < 0)
986			return err;
987	}
988	if (pm7500)
989		err = build_mixers(chip,
990				   ARRAY_SIZE(snd_pmac_awacs_mixers_pmac7500),
991				   snd_pmac_awacs_mixers_pmac7500);
992	else if (pm5500)
993		err = snd_ctl_add(chip->card,
994		    (master_vol = snd_ctl_new1(snd_pmac_awacs_mixers_pmac5500,
995						chip)));
996	else if (beige)
997		err = build_mixers(chip,
998				   ARRAY_SIZE(snd_pmac_screamer_mixers_beige),
999				   snd_pmac_screamer_mixers_beige);
1000	else if (imac || lombard) {
1001		err = snd_ctl_add(chip->card,
1002		    (master_vol = snd_ctl_new1(snd_pmac_screamer_mixers_lo,
1003						chip)));
1004		if (err < 0)
1005			return err;
1006		err = build_mixers(chip,
1007				   ARRAY_SIZE(snd_pmac_screamer_mixers_imac),
1008				   snd_pmac_screamer_mixers_imac);
1009	} else if (g4agp)
1010		err = build_mixers(chip,
1011				   ARRAY_SIZE(snd_pmac_screamer_mixers_g4agp),
1012				   snd_pmac_screamer_mixers_g4agp);
1013	else
1014		err = build_mixers(chip,
1015				   ARRAY_SIZE(snd_pmac_awacs_mixers_pmac),
1016				   snd_pmac_awacs_mixers_pmac);
1017	if (err < 0)
1018		return err;
1019	chip->master_sw_ctl = snd_ctl_new1((pm7500 || imac || g4agp || lombard)
1020			? &snd_pmac_awacs_master_sw_imac
1021			: pm5500
1022			? &snd_pmac_awacs_master_sw_pmac5500
1023			: &snd_pmac_awacs_master_sw, chip);
1024	err = snd_ctl_add(chip->card, chip->master_sw_ctl);
1025	if (err < 0)
1026		return err;
1027#ifdef PMAC_AMP_AVAIL
1028	if (chip->mixer_data) {
1029		/* use amplifier.  the signal is connected from route A
1030		 * to the amp.  the amp has its headphone and speaker
1031		 * volumes and mute switches, so we use them instead of
1032		 * screamer registers.
1033		 * in this case, it seems the route C is not used.
1034		 */
1035		err = build_mixers(chip, ARRAY_SIZE(snd_pmac_awacs_amp_vol),
1036					snd_pmac_awacs_amp_vol);
1037		if (err < 0)
1038			return err;
1039		/* overwrite */
1040		chip->master_sw_ctl = snd_ctl_new1(&snd_pmac_awacs_amp_hp_sw,
1041							chip);
1042		err = snd_ctl_add(chip->card, chip->master_sw_ctl);
1043		if (err < 0)
1044			return err;
1045		chip->speaker_sw_ctl = snd_ctl_new1(&snd_pmac_awacs_amp_spk_sw,
1046							chip);
1047		err = snd_ctl_add(chip->card, chip->speaker_sw_ctl);
1048		if (err < 0)
1049			return err;
1050	} else
1051#endif /* PMAC_AMP_AVAIL */
1052	{
1053		/* route A = headphone, route C = speaker */
1054		err = snd_ctl_add(chip->card,
1055		    (speaker_vol = snd_ctl_new1(snd_pmac_awacs_speaker_vol,
1056						chip)));
1057		if (err < 0)
1058			return err;
1059		chip->speaker_sw_ctl = snd_ctl_new1(imac1
1060				? &snd_pmac_awacs_speaker_sw_imac1
1061				: imac2
1062				? &snd_pmac_awacs_speaker_sw_imac2
1063				: &snd_pmac_awacs_speaker_sw, chip);
1064		err = snd_ctl_add(chip->card, chip->speaker_sw_ctl);
1065		if (err < 0)
1066			return err;
1067	}
1068
1069	if (pm5500 || imac || lombard) {
1070		vmaster_sw = snd_ctl_make_virtual_master(
1071			"Master Playback Switch", (unsigned int *) NULL);
1072		err = snd_ctl_add_slave_uncached(vmaster_sw,
1073						 chip->master_sw_ctl);
1074		if (err < 0)
1075			return err;
1076		err = snd_ctl_add_slave_uncached(vmaster_sw,
1077						  chip->speaker_sw_ctl);
1078		if (err < 0)
1079			return err;
1080		err = snd_ctl_add(chip->card, vmaster_sw);
1081		if (err < 0)
1082			return err;
1083		vmaster_vol = snd_ctl_make_virtual_master(
1084			"Master Playback Volume", (unsigned int *) NULL);
1085		err = snd_ctl_add_slave(vmaster_vol, master_vol);
1086		if (err < 0)
1087			return err;
1088		err = snd_ctl_add_slave(vmaster_vol, speaker_vol);
1089		if (err < 0)
1090			return err;
1091		err = snd_ctl_add(chip->card, vmaster_vol);
1092		if (err < 0)
1093			return err;
1094	}
1095
1096	if (beige || g4agp)
1097		err = build_mixers(chip,
1098				ARRAY_SIZE(snd_pmac_screamer_mic_boost_beige),
1099				snd_pmac_screamer_mic_boost_beige);
1100	else if (imac)
1101		err = build_mixers(chip,
1102				ARRAY_SIZE(snd_pmac_screamer_mic_boost_imac),
1103				snd_pmac_screamer_mic_boost_imac);
1104	else if (chip->model == PMAC_SCREAMER)
1105		err = build_mixers(chip,
1106				ARRAY_SIZE(snd_pmac_screamer_mic_boost),
1107				snd_pmac_screamer_mic_boost);
1108	else if (pm7500)
1109		err = build_mixers(chip,
1110				ARRAY_SIZE(snd_pmac_awacs_mic_boost_pmac7500),
1111				snd_pmac_awacs_mic_boost_pmac7500);
1112	else
1113		err = build_mixers(chip, ARRAY_SIZE(snd_pmac_awacs_mic_boost),
1114				snd_pmac_awacs_mic_boost);
1115	if (err < 0)
1116		return err;
1117
1118	/*
1119	 * set lowlevel callbacks
1120	 */
1121	chip->set_format = snd_pmac_awacs_set_format;
1122#ifdef CONFIG_PM
1123	chip->suspend = snd_pmac_awacs_suspend;
1124	chip->resume = snd_pmac_awacs_resume;
1125#endif
1126#ifdef PMAC_SUPPORT_AUTOMUTE
1127	err = snd_pmac_add_automute(chip);
1128	if (err < 0)
1129		return err;
1130	chip->detect_headphone = snd_pmac_awacs_detect_headphone;
1131	chip->update_automute = snd_pmac_awacs_update_automute;
1132	snd_pmac_awacs_update_automute(chip, 0); /* update the status only */
1133#endif
1134	if (chip->model == PMAC_SCREAMER) {
1135		snd_pmac_awacs_write_noreg(chip, 6, chip->awacs_reg[6]);
1136		snd_pmac_awacs_write_noreg(chip, 0, chip->awacs_reg[0]);
1137	}
1138
1139	return 0;
1140}
1141