xref: /openbmc/linux/sound/ppc/tumbler.c (revision 1da177e4c3f41524e886b7f1b8a0c1fc7321cac2)
1 /*
2  * PMac Tumbler/Snapper lowlevel functions
3  *
4  * Copyright (c) by Takashi Iwai <tiwai@suse.de>
5  *
6  *   This program is free software; you can redistribute it and/or modify
7  *   it under the terms of the GNU General Public License as published by
8  *   the Free Software Foundation; either version 2 of the License, or
9  *   (at your option) any later version.
10  *
11  *   This program is distributed in the hope that it will be useful,
12  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *   GNU General Public License for more details.
15  *
16  *   You should have received a copy of the GNU General Public License
17  *   along with this program; if not, write to the Free Software
18  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19  *
20  *   Rene Rebe <rene.rebe@gmx.net>:
21  *     * update from shadow registers on wakeup and headphone plug
22  *     * automatically toggle DRC on headphone plug
23  *
24  */
25 
26 
27 #include <sound/driver.h>
28 #include <linux/init.h>
29 #include <linux/delay.h>
30 #include <linux/i2c.h>
31 #include <linux/i2c-dev.h>
32 #include <linux/kmod.h>
33 #include <linux/slab.h>
34 #include <linux/interrupt.h>
35 #include <sound/core.h>
36 #include <asm/io.h>
37 #include <asm/irq.h>
38 #ifdef CONFIG_PPC_HAS_FEATURE_CALLS
39 #include <asm/pmac_feature.h>
40 #endif
41 #include "pmac.h"
42 #include "tumbler_volume.h"
43 
44 /* i2c address for tumbler */
45 #define TAS_I2C_ADDR	0x34
46 
47 /* registers */
48 #define TAS_REG_MCS	0x01	/* main control */
49 #define TAS_REG_DRC	0x02
50 #define TAS_REG_VOL	0x04
51 #define TAS_REG_TREBLE	0x05
52 #define TAS_REG_BASS	0x06
53 #define TAS_REG_INPUT1	0x07
54 #define TAS_REG_INPUT2	0x08
55 
56 /* tas3001c */
57 #define TAS_REG_PCM	TAS_REG_INPUT1
58 
59 /* tas3004 */
60 #define TAS_REG_LMIX	TAS_REG_INPUT1
61 #define TAS_REG_RMIX	TAS_REG_INPUT2
62 #define TAS_REG_MCS2	0x43		/* main control 2 */
63 #define TAS_REG_ACS	0x40		/* analog control */
64 
65 /* mono volumes for tas3001c/tas3004 */
66 enum {
67 	VOL_IDX_PCM_MONO, /* tas3001c only */
68 	VOL_IDX_BASS, VOL_IDX_TREBLE,
69 	VOL_IDX_LAST_MONO
70 };
71 
72 /* stereo volumes for tas3004 */
73 enum {
74 	VOL_IDX_PCM, VOL_IDX_PCM2, VOL_IDX_ADC,
75 	VOL_IDX_LAST_MIX
76 };
77 
78 typedef struct pmac_gpio {
79 #ifdef CONFIG_PPC_HAS_FEATURE_CALLS
80 	unsigned int addr;
81 #else
82 	void __iomem *addr;
83 #endif
84 	int active_state;
85 } pmac_gpio_t;
86 
87 typedef struct pmac_tumbler_t {
88 	pmac_keywest_t i2c;
89 	pmac_gpio_t audio_reset;
90 	pmac_gpio_t amp_mute;
91 	pmac_gpio_t hp_mute;
92 	pmac_gpio_t hp_detect;
93 	int headphone_irq;
94 	unsigned int master_vol[2];
95 	unsigned int master_switch[2];
96 	unsigned int mono_vol[VOL_IDX_LAST_MONO];
97 	unsigned int mix_vol[VOL_IDX_LAST_MIX][2]; /* stereo volumes for tas3004 */
98 	int drc_range;
99 	int drc_enable;
100 	int capture_source;
101 } pmac_tumbler_t;
102 
103 
104 /*
105  */
106 
107 static int send_init_client(pmac_keywest_t *i2c, unsigned int *regs)
108 {
109 	while (*regs > 0) {
110 		int err, count = 10;
111 		do {
112 			err = i2c_smbus_write_byte_data(i2c->client,
113 							regs[0], regs[1]);
114 			if (err >= 0)
115 				break;
116 			mdelay(10);
117 		} while (count--);
118 		if (err < 0)
119 			return -ENXIO;
120 		regs += 2;
121 	}
122 	return 0;
123 }
124 
125 
126 static int tumbler_init_client(pmac_keywest_t *i2c)
127 {
128 	static unsigned int regs[] = {
129 		/* normal operation, SCLK=64fps, i2s output, i2s input, 16bit width */
130 		TAS_REG_MCS, (1<<6)|(2<<4)|(2<<2)|0,
131 		0, /* terminator */
132 	};
133 	return send_init_client(i2c, regs);
134 }
135 
136 static int snapper_init_client(pmac_keywest_t *i2c)
137 {
138 	static unsigned int regs[] = {
139 		/* normal operation, SCLK=64fps, i2s output, 16bit width */
140 		TAS_REG_MCS, (1<<6)|(2<<4)|0,
141 		/* normal operation, all-pass mode */
142 		TAS_REG_MCS2, (1<<1),
143 		/* normal output, no deemphasis, A input, power-up, line-in */
144 		TAS_REG_ACS, 0,
145 		0, /* terminator */
146 	};
147 	return send_init_client(i2c, regs);
148 }
149 
150 /*
151  * gpio access
152  */
153 #ifdef CONFIG_PPC_HAS_FEATURE_CALLS
154 #define do_gpio_write(gp, val) \
155 	pmac_call_feature(PMAC_FTR_WRITE_GPIO, NULL, (gp)->addr, val)
156 #define do_gpio_read(gp) \
157 	pmac_call_feature(PMAC_FTR_READ_GPIO, NULL, (gp)->addr, 0)
158 #define tumbler_gpio_free(gp) /* NOP */
159 #else
160 #define do_gpio_write(gp, val)	writeb(val, (gp)->addr)
161 #define do_gpio_read(gp)	readb((gp)->addr)
162 static inline void tumbler_gpio_free(pmac_gpio_t *gp)
163 {
164 	if (gp->addr) {
165 		iounmap(gp->addr);
166 		gp->addr = NULL;
167 	}
168 }
169 #endif /* CONFIG_PPC_HAS_FEATURE_CALLS */
170 
171 static void write_audio_gpio(pmac_gpio_t *gp, int active)
172 {
173 	if (! gp->addr)
174 		return;
175 	active = active ? gp->active_state : !gp->active_state;
176 	do_gpio_write(gp, active ? 0x05 : 0x04);
177 }
178 
179 static int read_audio_gpio(pmac_gpio_t *gp)
180 {
181 	int ret;
182 	if (! gp->addr)
183 		return 0;
184 	ret = ((do_gpio_read(gp) & 0x02) !=0);
185 	return ret == gp->active_state;
186 }
187 
188 /*
189  * update master volume
190  */
191 static int tumbler_set_master_volume(pmac_tumbler_t *mix)
192 {
193 	unsigned char block[6];
194 	unsigned int left_vol, right_vol;
195 
196 	if (! mix->i2c.client)
197 		return -ENODEV;
198 
199 	if (! mix->master_switch[0])
200 		left_vol = 0;
201 	else {
202 		left_vol = mix->master_vol[0];
203 		if (left_vol >= ARRAY_SIZE(master_volume_table))
204 			left_vol = ARRAY_SIZE(master_volume_table) - 1;
205 		left_vol = master_volume_table[left_vol];
206 	}
207 	if (! mix->master_switch[1])
208 		right_vol = 0;
209 	else {
210 		right_vol = mix->master_vol[1];
211 		if (right_vol >= ARRAY_SIZE(master_volume_table))
212 			right_vol = ARRAY_SIZE(master_volume_table) - 1;
213 		right_vol = master_volume_table[right_vol];
214 	}
215 
216 	block[0] = (left_vol >> 16) & 0xff;
217 	block[1] = (left_vol >> 8)  & 0xff;
218 	block[2] = (left_vol >> 0)  & 0xff;
219 
220 	block[3] = (right_vol >> 16) & 0xff;
221 	block[4] = (right_vol >> 8)  & 0xff;
222 	block[5] = (right_vol >> 0)  & 0xff;
223 
224 	if (i2c_smbus_write_block_data(mix->i2c.client, TAS_REG_VOL,
225 				       6, block) < 0) {
226 		snd_printk("failed to set volume \n");
227 		return -EINVAL;
228 	}
229 	return 0;
230 }
231 
232 
233 /* output volume */
234 static int tumbler_info_master_volume(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
235 {
236 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
237 	uinfo->count = 2;
238 	uinfo->value.integer.min = 0;
239 	uinfo->value.integer.max = ARRAY_SIZE(master_volume_table) - 1;
240 	return 0;
241 }
242 
243 static int tumbler_get_master_volume(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
244 {
245 	pmac_t *chip = snd_kcontrol_chip(kcontrol);
246 	pmac_tumbler_t *mix = chip->mixer_data;
247 	snd_assert(mix, return -ENODEV);
248 	ucontrol->value.integer.value[0] = mix->master_vol[0];
249 	ucontrol->value.integer.value[1] = mix->master_vol[1];
250 	return 0;
251 }
252 
253 static int tumbler_put_master_volume(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
254 {
255 	pmac_t *chip = snd_kcontrol_chip(kcontrol);
256 	pmac_tumbler_t *mix = chip->mixer_data;
257 	int change;
258 
259 	snd_assert(mix, return -ENODEV);
260 	change = mix->master_vol[0] != ucontrol->value.integer.value[0] ||
261 		mix->master_vol[1] != ucontrol->value.integer.value[1];
262 	if (change) {
263 		mix->master_vol[0] = ucontrol->value.integer.value[0];
264 		mix->master_vol[1] = ucontrol->value.integer.value[1];
265 		tumbler_set_master_volume(mix);
266 	}
267 	return change;
268 }
269 
270 /* output switch */
271 static int tumbler_get_master_switch(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
272 {
273 	pmac_t *chip = snd_kcontrol_chip(kcontrol);
274 	pmac_tumbler_t *mix = chip->mixer_data;
275 	snd_assert(mix, return -ENODEV);
276 	ucontrol->value.integer.value[0] = mix->master_switch[0];
277 	ucontrol->value.integer.value[1] = mix->master_switch[1];
278 	return 0;
279 }
280 
281 static int tumbler_put_master_switch(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
282 {
283 	pmac_t *chip = snd_kcontrol_chip(kcontrol);
284 	pmac_tumbler_t *mix = chip->mixer_data;
285 	int change;
286 
287 	snd_assert(mix, return -ENODEV);
288 	change = mix->master_switch[0] != ucontrol->value.integer.value[0] ||
289 		mix->master_switch[1] != ucontrol->value.integer.value[1];
290 	if (change) {
291 		mix->master_switch[0] = !!ucontrol->value.integer.value[0];
292 		mix->master_switch[1] = !!ucontrol->value.integer.value[1];
293 		tumbler_set_master_volume(mix);
294 	}
295 	return change;
296 }
297 
298 
299 /*
300  * TAS3001c dynamic range compression
301  */
302 
303 #define TAS3001_DRC_MAX		0x5f
304 
305 static int tumbler_set_drc(pmac_tumbler_t *mix)
306 {
307 	unsigned char val[2];
308 
309 	if (! mix->i2c.client)
310 		return -ENODEV;
311 
312 	if (mix->drc_enable) {
313 		val[0] = 0xc1; /* enable, 3:1 compression */
314 		if (mix->drc_range > TAS3001_DRC_MAX)
315 			val[1] = 0xf0;
316 		else if (mix->drc_range < 0)
317 			val[1] = 0x91;
318 		else
319 			val[1] = mix->drc_range + 0x91;
320 	} else {
321 		val[0] = 0;
322 		val[1] = 0;
323 	}
324 
325 	if (i2c_smbus_write_block_data(mix->i2c.client, TAS_REG_DRC,
326 				       2, val) < 0) {
327 		snd_printk("failed to set DRC\n");
328 		return -EINVAL;
329 	}
330 	return 0;
331 }
332 
333 /*
334  * TAS3004
335  */
336 
337 #define TAS3004_DRC_MAX		0xef
338 
339 static int snapper_set_drc(pmac_tumbler_t *mix)
340 {
341 	unsigned char val[6];
342 
343 	if (! mix->i2c.client)
344 		return -ENODEV;
345 
346 	if (mix->drc_enable)
347 		val[0] = 0x50; /* 3:1 above threshold */
348 	else
349 		val[0] = 0x51; /* disabled */
350 	val[1] = 0x02; /* 1:1 below threshold */
351 	if (mix->drc_range > 0xef)
352 		val[2] = 0xef;
353 	else if (mix->drc_range < 0)
354 		val[2] = 0x00;
355 	else
356 		val[2] = mix->drc_range;
357 	val[3] = 0xb0;
358 	val[4] = 0x60;
359 	val[5] = 0xa0;
360 
361 	if (i2c_smbus_write_block_data(mix->i2c.client, TAS_REG_DRC,
362 				       6, val) < 0) {
363 		snd_printk("failed to set DRC\n");
364 		return -EINVAL;
365 	}
366 	return 0;
367 }
368 
369 static int tumbler_info_drc_value(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
370 {
371 	pmac_t *chip = snd_kcontrol_chip(kcontrol);
372 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
373 	uinfo->count = 1;
374 	uinfo->value.integer.min = 0;
375 	uinfo->value.integer.max =
376 		chip->model == PMAC_TUMBLER ? TAS3001_DRC_MAX : TAS3004_DRC_MAX;
377 	return 0;
378 }
379 
380 static int tumbler_get_drc_value(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
381 {
382 	pmac_t *chip = snd_kcontrol_chip(kcontrol);
383 	pmac_tumbler_t *mix;
384 	if (! (mix = chip->mixer_data))
385 		return -ENODEV;
386 	ucontrol->value.integer.value[0] = mix->drc_range;
387 	return 0;
388 }
389 
390 static int tumbler_put_drc_value(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
391 {
392 	pmac_t *chip = snd_kcontrol_chip(kcontrol);
393 	pmac_tumbler_t *mix;
394 	int change;
395 
396 	if (! (mix = chip->mixer_data))
397 		return -ENODEV;
398 	change = mix->drc_range != ucontrol->value.integer.value[0];
399 	if (change) {
400 		mix->drc_range = ucontrol->value.integer.value[0];
401 		if (chip->model == PMAC_TUMBLER)
402 			tumbler_set_drc(mix);
403 		else
404 			snapper_set_drc(mix);
405 	}
406 	return change;
407 }
408 
409 static int tumbler_get_drc_switch(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
410 {
411 	pmac_t *chip = snd_kcontrol_chip(kcontrol);
412 	pmac_tumbler_t *mix;
413 	if (! (mix = chip->mixer_data))
414 		return -ENODEV;
415 	ucontrol->value.integer.value[0] = mix->drc_enable;
416 	return 0;
417 }
418 
419 static int tumbler_put_drc_switch(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
420 {
421 	pmac_t *chip = snd_kcontrol_chip(kcontrol);
422 	pmac_tumbler_t *mix;
423 	int change;
424 
425 	if (! (mix = chip->mixer_data))
426 		return -ENODEV;
427 	change = mix->drc_enable != ucontrol->value.integer.value[0];
428 	if (change) {
429 		mix->drc_enable = !!ucontrol->value.integer.value[0];
430 		if (chip->model == PMAC_TUMBLER)
431 			tumbler_set_drc(mix);
432 		else
433 			snapper_set_drc(mix);
434 	}
435 	return change;
436 }
437 
438 
439 /*
440  * mono volumes
441  */
442 
443 struct tumbler_mono_vol {
444 	int index;
445 	int reg;
446 	int bytes;
447 	unsigned int max;
448 	unsigned int *table;
449 };
450 
451 static int tumbler_set_mono_volume(pmac_tumbler_t *mix, struct tumbler_mono_vol *info)
452 {
453 	unsigned char block[4];
454 	unsigned int vol;
455 	int i;
456 
457 	if (! mix->i2c.client)
458 		return -ENODEV;
459 
460 	vol = mix->mono_vol[info->index];
461 	if (vol >= info->max)
462 		vol = info->max - 1;
463 	vol = info->table[vol];
464 	for (i = 0; i < info->bytes; i++)
465 		block[i] = (vol >> ((info->bytes - i - 1) * 8)) & 0xff;
466 	if (i2c_smbus_write_block_data(mix->i2c.client, info->reg,
467 				       info->bytes, block) < 0) {
468 		snd_printk("failed to set mono volume %d\n", info->index);
469 		return -EINVAL;
470 	}
471 	return 0;
472 }
473 
474 static int tumbler_info_mono(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
475 {
476 	struct tumbler_mono_vol *info = (struct tumbler_mono_vol *)kcontrol->private_value;
477 
478 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
479 	uinfo->count = 1;
480 	uinfo->value.integer.min = 0;
481 	uinfo->value.integer.max = info->max - 1;
482 	return 0;
483 }
484 
485 static int tumbler_get_mono(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
486 {
487 	struct tumbler_mono_vol *info = (struct tumbler_mono_vol *)kcontrol->private_value;
488 	pmac_t *chip = snd_kcontrol_chip(kcontrol);
489 	pmac_tumbler_t *mix;
490 	if (! (mix = chip->mixer_data))
491 		return -ENODEV;
492 	ucontrol->value.integer.value[0] = mix->mono_vol[info->index];
493 	return 0;
494 }
495 
496 static int tumbler_put_mono(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
497 {
498 	struct tumbler_mono_vol *info = (struct tumbler_mono_vol *)kcontrol->private_value;
499 	pmac_t *chip = snd_kcontrol_chip(kcontrol);
500 	pmac_tumbler_t *mix;
501 	int change;
502 
503 	if (! (mix = chip->mixer_data))
504 		return -ENODEV;
505 	change = mix->mono_vol[info->index] != ucontrol->value.integer.value[0];
506 	if (change) {
507 		mix->mono_vol[info->index] = ucontrol->value.integer.value[0];
508 		tumbler_set_mono_volume(mix, info);
509 	}
510 	return change;
511 }
512 
513 /* TAS3001c mono volumes */
514 static struct tumbler_mono_vol tumbler_pcm_vol_info = {
515 	.index = VOL_IDX_PCM_MONO,
516 	.reg = TAS_REG_PCM,
517 	.bytes = 3,
518 	.max = ARRAY_SIZE(mixer_volume_table),
519 	.table = mixer_volume_table,
520 };
521 
522 static struct tumbler_mono_vol tumbler_bass_vol_info = {
523 	.index = VOL_IDX_BASS,
524 	.reg = TAS_REG_BASS,
525 	.bytes = 1,
526 	.max = ARRAY_SIZE(bass_volume_table),
527 	.table = bass_volume_table,
528 };
529 
530 static struct tumbler_mono_vol tumbler_treble_vol_info = {
531 	.index = VOL_IDX_TREBLE,
532 	.reg = TAS_REG_TREBLE,
533 	.bytes = 1,
534 	.max = ARRAY_SIZE(treble_volume_table),
535 	.table = treble_volume_table,
536 };
537 
538 /* TAS3004 mono volumes */
539 static struct tumbler_mono_vol snapper_bass_vol_info = {
540 	.index = VOL_IDX_BASS,
541 	.reg = TAS_REG_BASS,
542 	.bytes = 1,
543 	.max = ARRAY_SIZE(snapper_bass_volume_table),
544 	.table = snapper_bass_volume_table,
545 };
546 
547 static struct tumbler_mono_vol snapper_treble_vol_info = {
548 	.index = VOL_IDX_TREBLE,
549 	.reg = TAS_REG_TREBLE,
550 	.bytes = 1,
551 	.max = ARRAY_SIZE(snapper_treble_volume_table),
552 	.table = snapper_treble_volume_table,
553 };
554 
555 
556 #define DEFINE_MONO(xname,type) { \
557 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,\
558 	.name = xname, \
559 	.info = tumbler_info_mono, \
560 	.get = tumbler_get_mono, \
561 	.put = tumbler_put_mono, \
562 	.private_value = (unsigned long)(&tumbler_##type##_vol_info), \
563 }
564 
565 #define DEFINE_SNAPPER_MONO(xname,type) { \
566 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,\
567 	.name = xname, \
568 	.info = tumbler_info_mono, \
569 	.get = tumbler_get_mono, \
570 	.put = tumbler_put_mono, \
571 	.private_value = (unsigned long)(&snapper_##type##_vol_info), \
572 }
573 
574 
575 /*
576  * snapper mixer volumes
577  */
578 
579 static int snapper_set_mix_vol1(pmac_tumbler_t *mix, int idx, int ch, int reg)
580 {
581 	int i, j, vol;
582 	unsigned char block[9];
583 
584 	vol = mix->mix_vol[idx][ch];
585 	if (vol >= ARRAY_SIZE(mixer_volume_table)) {
586 		vol = ARRAY_SIZE(mixer_volume_table) - 1;
587 		mix->mix_vol[idx][ch] = vol;
588 	}
589 
590 	for (i = 0; i < 3; i++) {
591 		vol = mix->mix_vol[i][ch];
592 		vol = mixer_volume_table[vol];
593 		for (j = 0; j < 3; j++)
594 			block[i * 3 + j] = (vol >> ((2 - j) * 8)) & 0xff;
595 	}
596 	if (i2c_smbus_write_block_data(mix->i2c.client, reg, 9, block) < 0) {
597 		snd_printk("failed to set mono volume %d\n", reg);
598 		return -EINVAL;
599 	}
600 	return 0;
601 }
602 
603 static int snapper_set_mix_vol(pmac_tumbler_t *mix, int idx)
604 {
605 	if (! mix->i2c.client)
606 		return -ENODEV;
607 	if (snapper_set_mix_vol1(mix, idx, 0, TAS_REG_LMIX) < 0 ||
608 	    snapper_set_mix_vol1(mix, idx, 1, TAS_REG_RMIX) < 0)
609 		return -EINVAL;
610 	return 0;
611 }
612 
613 static int snapper_info_mix(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
614 {
615 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
616 	uinfo->count = 2;
617 	uinfo->value.integer.min = 0;
618 	uinfo->value.integer.max = ARRAY_SIZE(mixer_volume_table) - 1;
619 	return 0;
620 }
621 
622 static int snapper_get_mix(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
623 {
624 	int idx = (int)kcontrol->private_value;
625 	pmac_t *chip = snd_kcontrol_chip(kcontrol);
626 	pmac_tumbler_t *mix;
627 	if (! (mix = chip->mixer_data))
628 		return -ENODEV;
629 	ucontrol->value.integer.value[0] = mix->mix_vol[idx][0];
630 	ucontrol->value.integer.value[1] = mix->mix_vol[idx][1];
631 	return 0;
632 }
633 
634 static int snapper_put_mix(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
635 {
636 	int idx = (int)kcontrol->private_value;
637 	pmac_t *chip = snd_kcontrol_chip(kcontrol);
638 	pmac_tumbler_t *mix;
639 	int change;
640 
641 	if (! (mix = chip->mixer_data))
642 		return -ENODEV;
643 	change = mix->mix_vol[idx][0] != ucontrol->value.integer.value[0] ||
644 		mix->mix_vol[idx][1] != ucontrol->value.integer.value[1];
645 	if (change) {
646 		mix->mix_vol[idx][0] = ucontrol->value.integer.value[0];
647 		mix->mix_vol[idx][1] = ucontrol->value.integer.value[1];
648 		snapper_set_mix_vol(mix, idx);
649 	}
650 	return change;
651 }
652 
653 
654 /*
655  * mute switches
656  */
657 
658 enum { TUMBLER_MUTE_HP, TUMBLER_MUTE_AMP };
659 
660 static int tumbler_get_mute_switch(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
661 {
662 	pmac_t *chip = snd_kcontrol_chip(kcontrol);
663 	pmac_tumbler_t *mix;
664 	pmac_gpio_t *gp;
665 	if (! (mix = chip->mixer_data))
666 		return -ENODEV;
667 	gp = (kcontrol->private_value == TUMBLER_MUTE_HP) ? &mix->hp_mute : &mix->amp_mute;
668 	ucontrol->value.integer.value[0] = ! read_audio_gpio(gp);
669 	return 0;
670 }
671 
672 static int tumbler_put_mute_switch(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
673 {
674 	pmac_t *chip = snd_kcontrol_chip(kcontrol);
675 	pmac_tumbler_t *mix;
676 	pmac_gpio_t *gp;
677 	int val;
678 #ifdef PMAC_SUPPORT_AUTOMUTE
679 	if (chip->update_automute && chip->auto_mute)
680 		return 0; /* don't touch in the auto-mute mode */
681 #endif
682 	if (! (mix = chip->mixer_data))
683 		return -ENODEV;
684 	gp = (kcontrol->private_value == TUMBLER_MUTE_HP) ? &mix->hp_mute : &mix->amp_mute;
685 	val = ! read_audio_gpio(gp);
686 	if (val != ucontrol->value.integer.value[0]) {
687 		write_audio_gpio(gp, ! ucontrol->value.integer.value[0]);
688 		return 1;
689 	}
690 	return 0;
691 }
692 
693 static int snapper_set_capture_source(pmac_tumbler_t *mix)
694 {
695 	if (! mix->i2c.client)
696 		return -ENODEV;
697 	return i2c_smbus_write_byte_data(mix->i2c.client, TAS_REG_ACS,
698 					 mix->capture_source ? 2 : 0);
699 }
700 
701 static int snapper_info_capture_source(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
702 {
703 	static char *texts[2] = {
704 		"Line", "Mic"
705 	};
706 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
707 	uinfo->count = 1;
708 	uinfo->value.enumerated.items = 2;
709 	if (uinfo->value.enumerated.item > 1)
710 		uinfo->value.enumerated.item = 1;
711 	strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
712 	return 0;
713 }
714 
715 static int snapper_get_capture_source(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
716 {
717 	pmac_t *chip = snd_kcontrol_chip(kcontrol);
718 	pmac_tumbler_t *mix = chip->mixer_data;
719 
720 	snd_assert(mix, return -ENODEV);
721 	ucontrol->value.integer.value[0] = mix->capture_source;
722 	return 0;
723 }
724 
725 static int snapper_put_capture_source(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
726 {
727 	pmac_t *chip = snd_kcontrol_chip(kcontrol);
728 	pmac_tumbler_t *mix = chip->mixer_data;
729 	int change;
730 
731 	snd_assert(mix, return -ENODEV);
732 	change = ucontrol->value.integer.value[0] != mix->capture_source;
733 	if (change) {
734 		mix->capture_source = !!ucontrol->value.integer.value[0];
735 		snapper_set_capture_source(mix);
736 	}
737 	return change;
738 }
739 
740 #define DEFINE_SNAPPER_MIX(xname,idx,ofs) { \
741 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,\
742 	.name = xname, \
743 	.info = snapper_info_mix, \
744 	.get = snapper_get_mix, \
745 	.put = snapper_put_mix, \
746 	.index = idx,\
747 	.private_value = ofs, \
748 }
749 
750 
751 /*
752  */
753 static snd_kcontrol_new_t tumbler_mixers[] __initdata = {
754 	{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
755 	  .name = "Master Playback Volume",
756 	  .info = tumbler_info_master_volume,
757 	  .get = tumbler_get_master_volume,
758 	  .put = tumbler_put_master_volume
759 	},
760 	{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
761 	  .name = "Master Playback Switch",
762 	  .info = snd_pmac_boolean_stereo_info,
763 	  .get = tumbler_get_master_switch,
764 	  .put = tumbler_put_master_switch
765 	},
766 	DEFINE_MONO("Tone Control - Bass", bass),
767 	DEFINE_MONO("Tone Control - Treble", treble),
768 	DEFINE_MONO("PCM Playback Volume", pcm),
769 	{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
770 	  .name = "DRC Range",
771 	  .info = tumbler_info_drc_value,
772 	  .get = tumbler_get_drc_value,
773 	  .put = tumbler_put_drc_value
774 	},
775 };
776 
777 static snd_kcontrol_new_t snapper_mixers[] __initdata = {
778 	{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
779 	  .name = "Master Playback Volume",
780 	  .info = tumbler_info_master_volume,
781 	  .get = tumbler_get_master_volume,
782 	  .put = tumbler_put_master_volume
783 	},
784 	{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
785 	  .name = "Master Playback Switch",
786 	  .info = snd_pmac_boolean_stereo_info,
787 	  .get = tumbler_get_master_switch,
788 	  .put = tumbler_put_master_switch
789 	},
790 	DEFINE_SNAPPER_MIX("PCM Playback Volume", 0, VOL_IDX_PCM),
791 	DEFINE_SNAPPER_MIX("PCM Playback Volume", 1, VOL_IDX_PCM2),
792 	DEFINE_SNAPPER_MIX("Monitor Mix Volume", 0, VOL_IDX_ADC),
793 	DEFINE_SNAPPER_MONO("Tone Control - Bass", bass),
794 	DEFINE_SNAPPER_MONO("Tone Control - Treble", treble),
795 	{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
796 	  .name = "DRC Range",
797 	  .info = tumbler_info_drc_value,
798 	  .get = tumbler_get_drc_value,
799 	  .put = tumbler_put_drc_value
800 	},
801 	{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
802 	  .name = "Input Source", /* FIXME: "Capture Source" doesn't work properly */
803 	  .info = snapper_info_capture_source,
804 	  .get = snapper_get_capture_source,
805 	  .put = snapper_put_capture_source
806 	},
807 };
808 
809 static snd_kcontrol_new_t tumbler_hp_sw __initdata = {
810 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
811 	.name = "Headphone Playback Switch",
812 	.info = snd_pmac_boolean_mono_info,
813 	.get = tumbler_get_mute_switch,
814 	.put = tumbler_put_mute_switch,
815 	.private_value = TUMBLER_MUTE_HP,
816 };
817 static snd_kcontrol_new_t tumbler_speaker_sw __initdata = {
818 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
819 	.name = "PC Speaker Playback Switch",
820 	.info = snd_pmac_boolean_mono_info,
821 	.get = tumbler_get_mute_switch,
822 	.put = tumbler_put_mute_switch,
823 	.private_value = TUMBLER_MUTE_AMP,
824 };
825 static snd_kcontrol_new_t tumbler_drc_sw __initdata = {
826 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
827 	.name = "DRC Switch",
828 	.info = snd_pmac_boolean_mono_info,
829 	.get = tumbler_get_drc_switch,
830 	.put = tumbler_put_drc_switch
831 };
832 
833 
834 #ifdef PMAC_SUPPORT_AUTOMUTE
835 /*
836  * auto-mute stuffs
837  */
838 static int tumbler_detect_headphone(pmac_t *chip)
839 {
840 	pmac_tumbler_t *mix = chip->mixer_data;
841 	return read_audio_gpio(&mix->hp_detect);
842 }
843 
844 static void check_mute(pmac_t *chip, pmac_gpio_t *gp, int val, int do_notify, snd_kcontrol_t *sw)
845 {
846 	//pmac_tumbler_t *mix = chip->mixer_data;
847 	if (val != read_audio_gpio(gp)) {
848 		write_audio_gpio(gp, val);
849 		if (do_notify)
850 			snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE, &sw->id);
851 	}
852 }
853 
854 static struct work_struct device_change;
855 
856 static void
857 device_change_handler(void *self)
858 {
859 	pmac_t *chip = (pmac_t*) self;
860 	pmac_tumbler_t *mix;
861 
862 	if (!chip)
863 		return;
864 
865 	mix = chip->mixer_data;
866 
867 	/* first set the DRC so the speaker do not explode -ReneR */
868 	if (chip->model == PMAC_TUMBLER)
869 		tumbler_set_drc(mix);
870 	else
871 		snapper_set_drc(mix);
872 
873 	/* reset the master volume so the correct amplification is applied */
874 	tumbler_set_master_volume(mix);
875 }
876 
877 static void tumbler_update_automute(pmac_t *chip, int do_notify)
878 {
879 	if (chip->auto_mute) {
880 		pmac_tumbler_t *mix = chip->mixer_data;
881 		snd_assert(mix, return);
882 		if (tumbler_detect_headphone(chip)) {
883 			/* mute speaker */
884 			check_mute(chip, &mix->amp_mute, 1, do_notify, chip->speaker_sw_ctl);
885 			check_mute(chip, &mix->hp_mute, 0, do_notify, chip->master_sw_ctl);
886 			mix->drc_enable = 0;
887 
888 		} else {
889 			/* unmute speaker */
890 			check_mute(chip, &mix->amp_mute, 0, do_notify, chip->speaker_sw_ctl);
891 			check_mute(chip, &mix->hp_mute, 1, do_notify, chip->master_sw_ctl);
892 			mix->drc_enable = 1;
893 		}
894 		if (do_notify) {
895 			snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
896 				       &chip->hp_detect_ctl->id);
897 			snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
898 			               &chip->drc_sw_ctl->id);
899 		}
900 
901 		/* finally we need to schedule an update of the mixer values
902 		   (master and DRC are enough for now) -ReneR */
903 		schedule_work(&device_change);
904 
905 	}
906 }
907 #endif /* PMAC_SUPPORT_AUTOMUTE */
908 
909 
910 /* interrupt - headphone plug changed */
911 static irqreturn_t headphone_intr(int irq, void *devid, struct pt_regs *regs)
912 {
913 	pmac_t *chip = devid;
914 	if (chip->update_automute && chip->initialized) {
915 		chip->update_automute(chip, 1);
916 		return IRQ_HANDLED;
917 	}
918 	return IRQ_NONE;
919 }
920 
921 /* look for audio-gpio device */
922 static struct device_node *find_audio_device(const char *name)
923 {
924 	struct device_node *np;
925 
926 	if (! (np = find_devices("gpio")))
927 		return NULL;
928 
929 	for (np = np->child; np; np = np->sibling) {
930 		char *property = get_property(np, "audio-gpio", NULL);
931 		if (property && strcmp(property, name) == 0)
932 			return np;
933 	}
934 	return NULL;
935 }
936 
937 /* look for audio-gpio device */
938 static struct device_node *find_compatible_audio_device(const char *name)
939 {
940 	struct device_node *np;
941 
942 	if (! (np = find_devices("gpio")))
943 		return NULL;
944 
945 	for (np = np->child; np; np = np->sibling) {
946 		if (device_is_compatible(np, name))
947 			return np;
948 	}
949 	return NULL;
950 }
951 
952 /* find an audio device and get its address */
953 static unsigned long tumbler_find_device(const char *device, pmac_gpio_t *gp, int is_compatible)
954 {
955 	struct device_node *node;
956 	u32 *base;
957 
958 	if (is_compatible)
959 		node = find_compatible_audio_device(device);
960 	else
961 		node = find_audio_device(device);
962 	if (! node) {
963 		snd_printdd("cannot find device %s\n", device);
964 		return -ENODEV;
965 	}
966 
967 	base = (u32 *)get_property(node, "AAPL,address", NULL);
968 	if (! base) {
969 		snd_printd("cannot find address for device %s\n", device);
970 		return -ENODEV;
971 	}
972 
973 #ifdef CONFIG_PPC_HAS_FEATURE_CALLS
974 	gp->addr = (*base) & 0x0000ffff;
975 #else
976 	gp->addr = ioremap((unsigned long)(*base), 1);
977 #endif
978 	base = (u32 *)get_property(node, "audio-gpio-active-state", NULL);
979 	if (base)
980 		gp->active_state = *base;
981 	else
982 		gp->active_state = 1;
983 
984 
985 	return (node->n_intrs > 0) ? node->intrs[0].line : 0;
986 }
987 
988 /* reset audio */
989 static void tumbler_reset_audio(pmac_t *chip)
990 {
991 	pmac_tumbler_t *mix = chip->mixer_data;
992 
993 	write_audio_gpio(&mix->audio_reset, 0);
994 	big_mdelay(200);
995 	write_audio_gpio(&mix->audio_reset, 1);
996 	big_mdelay(100);
997 	write_audio_gpio(&mix->audio_reset, 0);
998 	big_mdelay(100);
999 }
1000 
1001 #ifdef CONFIG_PMAC_PBOOK
1002 /* resume mixer */
1003 static void tumbler_resume(pmac_t *chip)
1004 {
1005 	pmac_tumbler_t *mix = chip->mixer_data;
1006 
1007 	snd_assert(mix, return);
1008 
1009 	tumbler_reset_audio(chip);
1010 	if (mix->i2c.client && mix->i2c.init_client) {
1011 		if (mix->i2c.init_client(&mix->i2c) < 0)
1012 			printk(KERN_ERR "tumbler_init_client error\n");
1013 	} else
1014 		printk(KERN_ERR "tumbler: i2c is not initialized\n");
1015 	if (chip->model == PMAC_TUMBLER) {
1016 		tumbler_set_mono_volume(mix, &tumbler_pcm_vol_info);
1017 		tumbler_set_mono_volume(mix, &tumbler_bass_vol_info);
1018 		tumbler_set_mono_volume(mix, &tumbler_treble_vol_info);
1019 		tumbler_set_drc(mix);
1020 	} else {
1021 		snapper_set_mix_vol(mix, VOL_IDX_PCM);
1022 		snapper_set_mix_vol(mix, VOL_IDX_PCM2);
1023 		snapper_set_mix_vol(mix, VOL_IDX_ADC);
1024 		tumbler_set_mono_volume(mix, &snapper_bass_vol_info);
1025 		tumbler_set_mono_volume(mix, &snapper_treble_vol_info);
1026 		snapper_set_drc(mix);
1027 		snapper_set_capture_source(mix);
1028 	}
1029 	tumbler_set_master_volume(mix);
1030 	if (chip->update_automute)
1031 		chip->update_automute(chip, 0);
1032 }
1033 #endif
1034 
1035 /* initialize tumbler */
1036 static int __init tumbler_init(pmac_t *chip)
1037 {
1038 	int irq, err;
1039 	pmac_tumbler_t *mix = chip->mixer_data;
1040 	snd_assert(mix, return -EINVAL);
1041 
1042 	tumbler_find_device("audio-hw-reset", &mix->audio_reset, 0);
1043 	tumbler_find_device("amp-mute", &mix->amp_mute, 0);
1044 	tumbler_find_device("headphone-mute", &mix->hp_mute, 0);
1045 	irq = tumbler_find_device("headphone-detect", &mix->hp_detect, 0);
1046 	if (irq < 0)
1047 		irq = tumbler_find_device("keywest-gpio15", &mix->hp_detect, 1);
1048 
1049 	tumbler_reset_audio(chip);
1050 
1051 	/* activate headphone status interrupts */
1052   	if (irq >= 0) {
1053 		unsigned char val;
1054 		if ((err = request_irq(irq, headphone_intr, 0,
1055 				       "Tumbler Headphone Detection", chip)) < 0)
1056 			return err;
1057 		/* activate headphone status interrupts */
1058 		val = do_gpio_read(&mix->hp_detect);
1059 		do_gpio_write(&mix->hp_detect, val | 0x80);
1060 	}
1061 	mix->headphone_irq = irq;
1062 
1063 	return 0;
1064 }
1065 
1066 static void tumbler_cleanup(pmac_t *chip)
1067 {
1068 	pmac_tumbler_t *mix = chip->mixer_data;
1069 	if (! mix)
1070 		return;
1071 
1072 	if (mix->headphone_irq >= 0)
1073 		free_irq(mix->headphone_irq, chip);
1074 	tumbler_gpio_free(&mix->audio_reset);
1075 	tumbler_gpio_free(&mix->amp_mute);
1076 	tumbler_gpio_free(&mix->hp_mute);
1077 	tumbler_gpio_free(&mix->hp_detect);
1078 	snd_pmac_keywest_cleanup(&mix->i2c);
1079 	kfree(mix);
1080 	chip->mixer_data = NULL;
1081 }
1082 
1083 /* exported */
1084 int __init snd_pmac_tumbler_init(pmac_t *chip)
1085 {
1086 	int i, err;
1087 	pmac_tumbler_t *mix;
1088 	u32 *paddr;
1089 	struct device_node *tas_node;
1090 	char *chipname;
1091 
1092 #ifdef CONFIG_KMOD
1093 	if (current->fs->root)
1094 		request_module("i2c-keywest");
1095 #endif /* CONFIG_KMOD */
1096 
1097 	mix = kmalloc(sizeof(*mix), GFP_KERNEL);
1098 	if (! mix)
1099 		return -ENOMEM;
1100 	memset(mix, 0, sizeof(*mix));
1101 	mix->headphone_irq = -1;
1102 
1103 	chip->mixer_data = mix;
1104 	chip->mixer_free = tumbler_cleanup;
1105 
1106 	if ((err = tumbler_init(chip)) < 0)
1107 		return err;
1108 
1109 	/* set up TAS */
1110 	tas_node = find_devices("deq");
1111 	if (tas_node == NULL)
1112 		return -ENODEV;
1113 
1114 	paddr = (u32 *)get_property(tas_node, "i2c-address", NULL);
1115 	if (paddr)
1116 		mix->i2c.addr = (*paddr) >> 1;
1117 	else
1118 		mix->i2c.addr = TAS_I2C_ADDR;
1119 
1120 	if (chip->model == PMAC_TUMBLER) {
1121 		mix->i2c.init_client = tumbler_init_client;
1122 		mix->i2c.name = "TAS3001c";
1123 		chipname = "Tumbler";
1124 	} else {
1125 		mix->i2c.init_client = snapper_init_client;
1126 		mix->i2c.name = "TAS3004";
1127 		chipname = "Snapper";
1128 	}
1129 
1130 	if ((err = snd_pmac_keywest_init(&mix->i2c)) < 0)
1131 		return err;
1132 
1133 	/*
1134 	 * build mixers
1135 	 */
1136 	sprintf(chip->card->mixername, "PowerMac %s", chipname);
1137 
1138 	if (chip->model == PMAC_TUMBLER) {
1139 		for (i = 0; i < ARRAY_SIZE(tumbler_mixers); i++) {
1140 			if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&tumbler_mixers[i], chip))) < 0)
1141 				return err;
1142 		}
1143 	} else {
1144 		for (i = 0; i < ARRAY_SIZE(snapper_mixers); i++) {
1145 			if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snapper_mixers[i], chip))) < 0)
1146 				return err;
1147 		}
1148 	}
1149 	chip->master_sw_ctl = snd_ctl_new1(&tumbler_hp_sw, chip);
1150 	if ((err = snd_ctl_add(chip->card, chip->master_sw_ctl)) < 0)
1151 		return err;
1152 	chip->speaker_sw_ctl = snd_ctl_new1(&tumbler_speaker_sw, chip);
1153 	if ((err = snd_ctl_add(chip->card, chip->speaker_sw_ctl)) < 0)
1154 		return err;
1155 	chip->drc_sw_ctl = snd_ctl_new1(&tumbler_drc_sw, chip);
1156 	if ((err = snd_ctl_add(chip->card, chip->drc_sw_ctl)) < 0)
1157 		return err;
1158 
1159 
1160 #ifdef CONFIG_PMAC_PBOOK
1161 	chip->resume = tumbler_resume;
1162 #endif
1163 
1164 	INIT_WORK(&device_change, device_change_handler, (void *)chip);
1165 
1166 #ifdef PMAC_SUPPORT_AUTOMUTE
1167 	if (mix->headphone_irq >=0 && (err = snd_pmac_add_automute(chip)) < 0)
1168 		return err;
1169 	chip->detect_headphone = tumbler_detect_headphone;
1170 	chip->update_automute = tumbler_update_automute;
1171 	tumbler_update_automute(chip, 0); /* update the status only */
1172 #endif
1173 
1174 	return 0;
1175 }
1176