blob: 838fc113c441113ed36302f252561500f380892e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -070038#include <asm/machdep.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <asm/pmac_feature.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include "pmac.h"
41#include "tumbler_volume.h"
42
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -070043#undef DEBUG
44
45#ifdef DEBUG
46#define DBG(fmt...) printk(fmt)
47#else
48#define DBG(fmt...)
49#endif
50
Linus Torvalds1da177e2005-04-16 15:20:36 -070051/* i2c address for tumbler */
52#define TAS_I2C_ADDR 0x34
53
54/* registers */
55#define TAS_REG_MCS 0x01 /* main control */
56#define TAS_REG_DRC 0x02
57#define TAS_REG_VOL 0x04
58#define TAS_REG_TREBLE 0x05
59#define TAS_REG_BASS 0x06
60#define TAS_REG_INPUT1 0x07
61#define TAS_REG_INPUT2 0x08
62
63/* tas3001c */
64#define TAS_REG_PCM TAS_REG_INPUT1
65
66/* tas3004 */
67#define TAS_REG_LMIX TAS_REG_INPUT1
68#define TAS_REG_RMIX TAS_REG_INPUT2
69#define TAS_REG_MCS2 0x43 /* main control 2 */
70#define TAS_REG_ACS 0x40 /* analog control */
71
72/* mono volumes for tas3001c/tas3004 */
73enum {
74 VOL_IDX_PCM_MONO, /* tas3001c only */
75 VOL_IDX_BASS, VOL_IDX_TREBLE,
76 VOL_IDX_LAST_MONO
77};
78
79/* stereo volumes for tas3004 */
80enum {
81 VOL_IDX_PCM, VOL_IDX_PCM2, VOL_IDX_ADC,
82 VOL_IDX_LAST_MIX
83};
84
Takashi Iwai65b29f52005-11-17 15:09:46 +010085struct pmac_gpio {
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 unsigned int addr;
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -070087 u8 active_val;
88 u8 inactive_val;
89 u8 active_state;
Takashi Iwai65b29f52005-11-17 15:09:46 +010090};
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Takashi Iwai65b29f52005-11-17 15:09:46 +010092struct pmac_tumbler {
93 struct pmac_keywest i2c;
94 struct pmac_gpio audio_reset;
95 struct pmac_gpio amp_mute;
96 struct pmac_gpio line_mute;
97 struct pmac_gpio line_detect;
98 struct pmac_gpio hp_mute;
99 struct pmac_gpio hp_detect;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 int headphone_irq;
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -0700101 int lineout_irq;
Colin Leroy085e6fc2005-05-01 08:58:43 -0700102 unsigned int save_master_vol[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 unsigned int master_vol[2];
Benjamin Herrenschmidtb20af5f2005-04-16 15:24:32 -0700104 unsigned int save_master_switch[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 unsigned int master_switch[2];
106 unsigned int mono_vol[VOL_IDX_LAST_MONO];
107 unsigned int mix_vol[VOL_IDX_LAST_MIX][2]; /* stereo volumes for tas3004 */
108 int drc_range;
109 int drc_enable;
110 int capture_source;
Benjamin Herrenschmidtb20af5f2005-04-16 15:24:32 -0700111 int anded_reset;
112 int auto_mute_notify;
113 int reset_on_sleep;
114 u8 acs;
Takashi Iwai65b29f52005-11-17 15:09:46 +0100115};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
117
118/*
119 */
120
Takashi Iwai65b29f52005-11-17 15:09:46 +0100121static int send_init_client(struct pmac_keywest *i2c, unsigned int *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122{
123 while (*regs > 0) {
124 int err, count = 10;
125 do {
126 err = i2c_smbus_write_byte_data(i2c->client,
127 regs[0], regs[1]);
128 if (err >= 0)
129 break;
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -0700130 DBG("(W) i2c error %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 mdelay(10);
132 } while (count--);
133 if (err < 0)
134 return -ENXIO;
135 regs += 2;
136 }
137 return 0;
138}
139
140
Takashi Iwai65b29f52005-11-17 15:09:46 +0100141static int tumbler_init_client(struct pmac_keywest *i2c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142{
143 static unsigned int regs[] = {
144 /* normal operation, SCLK=64fps, i2s output, i2s input, 16bit width */
145 TAS_REG_MCS, (1<<6)|(2<<4)|(2<<2)|0,
146 0, /* terminator */
147 };
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -0700148 DBG("(I) tumbler init client\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 return send_init_client(i2c, regs);
150}
151
Takashi Iwai65b29f52005-11-17 15:09:46 +0100152static int snapper_init_client(struct pmac_keywest *i2c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
154 static unsigned int regs[] = {
155 /* normal operation, SCLK=64fps, i2s output, 16bit width */
156 TAS_REG_MCS, (1<<6)|(2<<4)|0,
157 /* normal operation, all-pass mode */
158 TAS_REG_MCS2, (1<<1),
159 /* normal output, no deemphasis, A input, power-up, line-in */
160 TAS_REG_ACS, 0,
161 0, /* terminator */
162 };
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -0700163 DBG("(I) snapper init client\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 return send_init_client(i2c, regs);
165}
166
167/*
168 * gpio access
169 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170#define do_gpio_write(gp, val) \
171 pmac_call_feature(PMAC_FTR_WRITE_GPIO, NULL, (gp)->addr, val)
172#define do_gpio_read(gp) \
173 pmac_call_feature(PMAC_FTR_READ_GPIO, NULL, (gp)->addr, 0)
174#define tumbler_gpio_free(gp) /* NOP */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
Takashi Iwai65b29f52005-11-17 15:09:46 +0100176static void write_audio_gpio(struct pmac_gpio *gp, int active)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177{
178 if (! gp->addr)
179 return;
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -0700180 active = active ? gp->active_val : gp->inactive_val;
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -0700181 do_gpio_write(gp, active);
182 DBG("(I) gpio %x write %d\n", gp->addr, active);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183}
184
Takashi Iwai65b29f52005-11-17 15:09:46 +0100185static int check_audio_gpio(struct pmac_gpio *gp)
Benjamin Herrenschmidt4be8dc72005-05-01 08:58:43 -0700186{
187 int ret;
188
189 if (! gp->addr)
190 return 0;
191
192 ret = do_gpio_read(gp);
193
194 return (ret & 0xd) == (gp->active_val & 0xd);
195}
196
Takashi Iwai65b29f52005-11-17 15:09:46 +0100197static int read_audio_gpio(struct pmac_gpio *gp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198{
199 int ret;
200 if (! gp->addr)
201 return 0;
202 ret = ((do_gpio_read(gp) & 0x02) !=0);
203 return ret == gp->active_state;
204}
205
206/*
207 * update master volume
208 */
Takashi Iwai65b29f52005-11-17 15:09:46 +0100209static int tumbler_set_master_volume(struct pmac_tumbler *mix)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210{
211 unsigned char block[6];
212 unsigned int left_vol, right_vol;
213
214 if (! mix->i2c.client)
215 return -ENODEV;
216
217 if (! mix->master_switch[0])
218 left_vol = 0;
219 else {
220 left_vol = mix->master_vol[0];
221 if (left_vol >= ARRAY_SIZE(master_volume_table))
222 left_vol = ARRAY_SIZE(master_volume_table) - 1;
223 left_vol = master_volume_table[left_vol];
224 }
225 if (! mix->master_switch[1])
226 right_vol = 0;
227 else {
228 right_vol = mix->master_vol[1];
229 if (right_vol >= ARRAY_SIZE(master_volume_table))
230 right_vol = ARRAY_SIZE(master_volume_table) - 1;
231 right_vol = master_volume_table[right_vol];
232 }
233
234 block[0] = (left_vol >> 16) & 0xff;
235 block[1] = (left_vol >> 8) & 0xff;
236 block[2] = (left_vol >> 0) & 0xff;
237
238 block[3] = (right_vol >> 16) & 0xff;
239 block[4] = (right_vol >> 8) & 0xff;
240 block[5] = (right_vol >> 0) & 0xff;
241
Benjamin Herrenschmidt367636e2006-02-08 15:04:18 +1100242 if (i2c_smbus_write_i2c_block_data(mix->i2c.client, TAS_REG_VOL, 6,
243 block) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 snd_printk("failed to set volume \n");
245 return -EINVAL;
246 }
247 return 0;
248}
249
250
251/* output volume */
Takashi Iwai65b29f52005-11-17 15:09:46 +0100252static int tumbler_info_master_volume(struct snd_kcontrol *kcontrol,
253 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254{
255 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
256 uinfo->count = 2;
257 uinfo->value.integer.min = 0;
258 uinfo->value.integer.max = ARRAY_SIZE(master_volume_table) - 1;
259 return 0;
260}
261
Takashi Iwai65b29f52005-11-17 15:09:46 +0100262static int tumbler_get_master_volume(struct snd_kcontrol *kcontrol,
263 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264{
Takashi Iwai65b29f52005-11-17 15:09:46 +0100265 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
266 struct pmac_tumbler *mix = chip->mixer_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 snd_assert(mix, return -ENODEV);
268 ucontrol->value.integer.value[0] = mix->master_vol[0];
269 ucontrol->value.integer.value[1] = mix->master_vol[1];
270 return 0;
271}
272
Takashi Iwai65b29f52005-11-17 15:09:46 +0100273static int tumbler_put_master_volume(struct snd_kcontrol *kcontrol,
274 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275{
Takashi Iwai65b29f52005-11-17 15:09:46 +0100276 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
277 struct pmac_tumbler *mix = chip->mixer_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 int change;
279
280 snd_assert(mix, return -ENODEV);
281 change = mix->master_vol[0] != ucontrol->value.integer.value[0] ||
282 mix->master_vol[1] != ucontrol->value.integer.value[1];
283 if (change) {
284 mix->master_vol[0] = ucontrol->value.integer.value[0];
285 mix->master_vol[1] = ucontrol->value.integer.value[1];
286 tumbler_set_master_volume(mix);
287 }
288 return change;
289}
290
291/* output switch */
Takashi Iwai65b29f52005-11-17 15:09:46 +0100292static int tumbler_get_master_switch(struct snd_kcontrol *kcontrol,
293 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294{
Takashi Iwai65b29f52005-11-17 15:09:46 +0100295 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
296 struct pmac_tumbler *mix = chip->mixer_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 snd_assert(mix, return -ENODEV);
298 ucontrol->value.integer.value[0] = mix->master_switch[0];
299 ucontrol->value.integer.value[1] = mix->master_switch[1];
300 return 0;
301}
302
Takashi Iwai65b29f52005-11-17 15:09:46 +0100303static int tumbler_put_master_switch(struct snd_kcontrol *kcontrol,
304 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305{
Takashi Iwai65b29f52005-11-17 15:09:46 +0100306 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
307 struct pmac_tumbler *mix = chip->mixer_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 int change;
309
310 snd_assert(mix, return -ENODEV);
311 change = mix->master_switch[0] != ucontrol->value.integer.value[0] ||
312 mix->master_switch[1] != ucontrol->value.integer.value[1];
313 if (change) {
314 mix->master_switch[0] = !!ucontrol->value.integer.value[0];
315 mix->master_switch[1] = !!ucontrol->value.integer.value[1];
316 tumbler_set_master_volume(mix);
317 }
318 return change;
319}
320
321
322/*
323 * TAS3001c dynamic range compression
324 */
325
326#define TAS3001_DRC_MAX 0x5f
327
Takashi Iwai65b29f52005-11-17 15:09:46 +0100328static int tumbler_set_drc(struct pmac_tumbler *mix)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329{
330 unsigned char val[2];
331
332 if (! mix->i2c.client)
333 return -ENODEV;
334
335 if (mix->drc_enable) {
336 val[0] = 0xc1; /* enable, 3:1 compression */
337 if (mix->drc_range > TAS3001_DRC_MAX)
338 val[1] = 0xf0;
339 else if (mix->drc_range < 0)
340 val[1] = 0x91;
341 else
342 val[1] = mix->drc_range + 0x91;
343 } else {
344 val[0] = 0;
345 val[1] = 0;
346 }
347
Benjamin Herrenschmidt367636e2006-02-08 15:04:18 +1100348 if (i2c_smbus_write_i2c_block_data(mix->i2c.client, TAS_REG_DRC,
349 2, val) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 snd_printk("failed to set DRC\n");
351 return -EINVAL;
352 }
353 return 0;
354}
355
356/*
357 * TAS3004
358 */
359
360#define TAS3004_DRC_MAX 0xef
361
Takashi Iwai65b29f52005-11-17 15:09:46 +0100362static int snapper_set_drc(struct pmac_tumbler *mix)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363{
364 unsigned char val[6];
365
366 if (! mix->i2c.client)
367 return -ENODEV;
368
369 if (mix->drc_enable)
370 val[0] = 0x50; /* 3:1 above threshold */
371 else
372 val[0] = 0x51; /* disabled */
373 val[1] = 0x02; /* 1:1 below threshold */
374 if (mix->drc_range > 0xef)
375 val[2] = 0xef;
376 else if (mix->drc_range < 0)
377 val[2] = 0x00;
378 else
379 val[2] = mix->drc_range;
380 val[3] = 0xb0;
381 val[4] = 0x60;
382 val[5] = 0xa0;
383
Benjamin Herrenschmidt367636e2006-02-08 15:04:18 +1100384 if (i2c_smbus_write_i2c_block_data(mix->i2c.client, TAS_REG_DRC,
385 6, val) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 snd_printk("failed to set DRC\n");
387 return -EINVAL;
388 }
389 return 0;
390}
391
Takashi Iwai65b29f52005-11-17 15:09:46 +0100392static int tumbler_info_drc_value(struct snd_kcontrol *kcontrol,
393 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394{
Takashi Iwai65b29f52005-11-17 15:09:46 +0100395 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
397 uinfo->count = 1;
398 uinfo->value.integer.min = 0;
399 uinfo->value.integer.max =
400 chip->model == PMAC_TUMBLER ? TAS3001_DRC_MAX : TAS3004_DRC_MAX;
401 return 0;
402}
403
Takashi Iwai65b29f52005-11-17 15:09:46 +0100404static int tumbler_get_drc_value(struct snd_kcontrol *kcontrol,
405 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406{
Takashi Iwai65b29f52005-11-17 15:09:46 +0100407 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
408 struct pmac_tumbler *mix;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 if (! (mix = chip->mixer_data))
410 return -ENODEV;
411 ucontrol->value.integer.value[0] = mix->drc_range;
412 return 0;
413}
414
Takashi Iwai65b29f52005-11-17 15:09:46 +0100415static int tumbler_put_drc_value(struct snd_kcontrol *kcontrol,
416 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417{
Takashi Iwai65b29f52005-11-17 15:09:46 +0100418 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
419 struct pmac_tumbler *mix;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 int change;
421
422 if (! (mix = chip->mixer_data))
423 return -ENODEV;
424 change = mix->drc_range != ucontrol->value.integer.value[0];
425 if (change) {
426 mix->drc_range = ucontrol->value.integer.value[0];
427 if (chip->model == PMAC_TUMBLER)
428 tumbler_set_drc(mix);
429 else
430 snapper_set_drc(mix);
431 }
432 return change;
433}
434
Takashi Iwai65b29f52005-11-17 15:09:46 +0100435static int tumbler_get_drc_switch(struct snd_kcontrol *kcontrol,
436 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437{
Takashi Iwai65b29f52005-11-17 15:09:46 +0100438 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
439 struct pmac_tumbler *mix;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 if (! (mix = chip->mixer_data))
441 return -ENODEV;
442 ucontrol->value.integer.value[0] = mix->drc_enable;
443 return 0;
444}
445
Takashi Iwai65b29f52005-11-17 15:09:46 +0100446static int tumbler_put_drc_switch(struct snd_kcontrol *kcontrol,
447 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448{
Takashi Iwai65b29f52005-11-17 15:09:46 +0100449 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
450 struct pmac_tumbler *mix;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 int change;
452
453 if (! (mix = chip->mixer_data))
454 return -ENODEV;
455 change = mix->drc_enable != ucontrol->value.integer.value[0];
456 if (change) {
457 mix->drc_enable = !!ucontrol->value.integer.value[0];
458 if (chip->model == PMAC_TUMBLER)
459 tumbler_set_drc(mix);
460 else
461 snapper_set_drc(mix);
462 }
463 return change;
464}
465
466
467/*
468 * mono volumes
469 */
470
471struct tumbler_mono_vol {
472 int index;
473 int reg;
474 int bytes;
475 unsigned int max;
476 unsigned int *table;
477};
478
Takashi Iwai65b29f52005-11-17 15:09:46 +0100479static int tumbler_set_mono_volume(struct pmac_tumbler *mix,
480 struct tumbler_mono_vol *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481{
482 unsigned char block[4];
483 unsigned int vol;
484 int i;
485
486 if (! mix->i2c.client)
487 return -ENODEV;
488
489 vol = mix->mono_vol[info->index];
490 if (vol >= info->max)
491 vol = info->max - 1;
492 vol = info->table[vol];
493 for (i = 0; i < info->bytes; i++)
494 block[i] = (vol >> ((info->bytes - i - 1) * 8)) & 0xff;
Benjamin Herrenschmidt367636e2006-02-08 15:04:18 +1100495 if (i2c_smbus_write_i2c_block_data(mix->i2c.client, info->reg,
496 info->bytes, block) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 snd_printk("failed to set mono volume %d\n", info->index);
498 return -EINVAL;
499 }
500 return 0;
501}
502
Takashi Iwai65b29f52005-11-17 15:09:46 +0100503static int tumbler_info_mono(struct snd_kcontrol *kcontrol,
504 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505{
506 struct tumbler_mono_vol *info = (struct tumbler_mono_vol *)kcontrol->private_value;
507
508 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
509 uinfo->count = 1;
510 uinfo->value.integer.min = 0;
511 uinfo->value.integer.max = info->max - 1;
512 return 0;
513}
514
Takashi Iwai65b29f52005-11-17 15:09:46 +0100515static int tumbler_get_mono(struct snd_kcontrol *kcontrol,
516 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517{
518 struct tumbler_mono_vol *info = (struct tumbler_mono_vol *)kcontrol->private_value;
Takashi Iwai65b29f52005-11-17 15:09:46 +0100519 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
520 struct pmac_tumbler *mix;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 if (! (mix = chip->mixer_data))
522 return -ENODEV;
523 ucontrol->value.integer.value[0] = mix->mono_vol[info->index];
524 return 0;
525}
526
Takashi Iwai65b29f52005-11-17 15:09:46 +0100527static int tumbler_put_mono(struct snd_kcontrol *kcontrol,
528 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529{
530 struct tumbler_mono_vol *info = (struct tumbler_mono_vol *)kcontrol->private_value;
Takashi Iwai65b29f52005-11-17 15:09:46 +0100531 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
532 struct pmac_tumbler *mix;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 int change;
534
535 if (! (mix = chip->mixer_data))
536 return -ENODEV;
537 change = mix->mono_vol[info->index] != ucontrol->value.integer.value[0];
538 if (change) {
539 mix->mono_vol[info->index] = ucontrol->value.integer.value[0];
540 tumbler_set_mono_volume(mix, info);
541 }
542 return change;
543}
544
545/* TAS3001c mono volumes */
546static struct tumbler_mono_vol tumbler_pcm_vol_info = {
547 .index = VOL_IDX_PCM_MONO,
548 .reg = TAS_REG_PCM,
549 .bytes = 3,
550 .max = ARRAY_SIZE(mixer_volume_table),
551 .table = mixer_volume_table,
552};
553
554static struct tumbler_mono_vol tumbler_bass_vol_info = {
555 .index = VOL_IDX_BASS,
556 .reg = TAS_REG_BASS,
557 .bytes = 1,
558 .max = ARRAY_SIZE(bass_volume_table),
559 .table = bass_volume_table,
560};
561
562static struct tumbler_mono_vol tumbler_treble_vol_info = {
563 .index = VOL_IDX_TREBLE,
564 .reg = TAS_REG_TREBLE,
565 .bytes = 1,
566 .max = ARRAY_SIZE(treble_volume_table),
567 .table = treble_volume_table,
568};
569
570/* TAS3004 mono volumes */
571static struct tumbler_mono_vol snapper_bass_vol_info = {
572 .index = VOL_IDX_BASS,
573 .reg = TAS_REG_BASS,
574 .bytes = 1,
575 .max = ARRAY_SIZE(snapper_bass_volume_table),
576 .table = snapper_bass_volume_table,
577};
578
579static struct tumbler_mono_vol snapper_treble_vol_info = {
580 .index = VOL_IDX_TREBLE,
581 .reg = TAS_REG_TREBLE,
582 .bytes = 1,
583 .max = ARRAY_SIZE(snapper_treble_volume_table),
584 .table = snapper_treble_volume_table,
585};
586
587
588#define DEFINE_MONO(xname,type) { \
589 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,\
590 .name = xname, \
591 .info = tumbler_info_mono, \
592 .get = tumbler_get_mono, \
593 .put = tumbler_put_mono, \
594 .private_value = (unsigned long)(&tumbler_##type##_vol_info), \
595}
596
597#define DEFINE_SNAPPER_MONO(xname,type) { \
598 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,\
599 .name = xname, \
600 .info = tumbler_info_mono, \
601 .get = tumbler_get_mono, \
602 .put = tumbler_put_mono, \
603 .private_value = (unsigned long)(&snapper_##type##_vol_info), \
604}
605
606
607/*
608 * snapper mixer volumes
609 */
610
Takashi Iwai65b29f52005-11-17 15:09:46 +0100611static int snapper_set_mix_vol1(struct pmac_tumbler *mix, int idx, int ch, int reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612{
613 int i, j, vol;
614 unsigned char block[9];
615
616 vol = mix->mix_vol[idx][ch];
617 if (vol >= ARRAY_SIZE(mixer_volume_table)) {
618 vol = ARRAY_SIZE(mixer_volume_table) - 1;
619 mix->mix_vol[idx][ch] = vol;
620 }
621
622 for (i = 0; i < 3; i++) {
623 vol = mix->mix_vol[i][ch];
624 vol = mixer_volume_table[vol];
625 for (j = 0; j < 3; j++)
626 block[i * 3 + j] = (vol >> ((2 - j) * 8)) & 0xff;
627 }
Benjamin Herrenschmidt367636e2006-02-08 15:04:18 +1100628 if (i2c_smbus_write_i2c_block_data(mix->i2c.client, reg,
629 9, block) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 snd_printk("failed to set mono volume %d\n", reg);
631 return -EINVAL;
632 }
633 return 0;
634}
635
Takashi Iwai65b29f52005-11-17 15:09:46 +0100636static int snapper_set_mix_vol(struct pmac_tumbler *mix, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637{
638 if (! mix->i2c.client)
639 return -ENODEV;
640 if (snapper_set_mix_vol1(mix, idx, 0, TAS_REG_LMIX) < 0 ||
641 snapper_set_mix_vol1(mix, idx, 1, TAS_REG_RMIX) < 0)
642 return -EINVAL;
643 return 0;
644}
645
Takashi Iwai65b29f52005-11-17 15:09:46 +0100646static int snapper_info_mix(struct snd_kcontrol *kcontrol,
647 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648{
649 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
650 uinfo->count = 2;
651 uinfo->value.integer.min = 0;
652 uinfo->value.integer.max = ARRAY_SIZE(mixer_volume_table) - 1;
653 return 0;
654}
655
Takashi Iwai65b29f52005-11-17 15:09:46 +0100656static int snapper_get_mix(struct snd_kcontrol *kcontrol,
657 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658{
659 int idx = (int)kcontrol->private_value;
Takashi Iwai65b29f52005-11-17 15:09:46 +0100660 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
661 struct pmac_tumbler *mix;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 if (! (mix = chip->mixer_data))
663 return -ENODEV;
664 ucontrol->value.integer.value[0] = mix->mix_vol[idx][0];
665 ucontrol->value.integer.value[1] = mix->mix_vol[idx][1];
666 return 0;
667}
668
Takashi Iwai65b29f52005-11-17 15:09:46 +0100669static int snapper_put_mix(struct snd_kcontrol *kcontrol,
670 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671{
672 int idx = (int)kcontrol->private_value;
Takashi Iwai65b29f52005-11-17 15:09:46 +0100673 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
674 struct pmac_tumbler *mix;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 int change;
676
677 if (! (mix = chip->mixer_data))
678 return -ENODEV;
679 change = mix->mix_vol[idx][0] != ucontrol->value.integer.value[0] ||
680 mix->mix_vol[idx][1] != ucontrol->value.integer.value[1];
681 if (change) {
682 mix->mix_vol[idx][0] = ucontrol->value.integer.value[0];
683 mix->mix_vol[idx][1] = ucontrol->value.integer.value[1];
684 snapper_set_mix_vol(mix, idx);
685 }
686 return change;
687}
688
689
690/*
Benjamin Herrenschmidtb20af5f2005-04-16 15:24:32 -0700691 * mute switches. FIXME: Turn that into software mute when both outputs are muted
692 * to avoid codec reset on ibook M7
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 */
694
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -0700695enum { TUMBLER_MUTE_HP, TUMBLER_MUTE_AMP, TUMBLER_MUTE_LINE };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
Takashi Iwai65b29f52005-11-17 15:09:46 +0100697static int tumbler_get_mute_switch(struct snd_kcontrol *kcontrol,
698 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699{
Takashi Iwai65b29f52005-11-17 15:09:46 +0100700 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
701 struct pmac_tumbler *mix;
702 struct pmac_gpio *gp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 if (! (mix = chip->mixer_data))
704 return -ENODEV;
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -0700705 switch(kcontrol->private_value) {
706 case TUMBLER_MUTE_HP:
707 gp = &mix->hp_mute; break;
708 case TUMBLER_MUTE_AMP:
709 gp = &mix->amp_mute; break;
710 case TUMBLER_MUTE_LINE:
711 gp = &mix->line_mute; break;
712 default:
713 gp = NULL;
714 }
715 if (gp == NULL)
716 return -EINVAL;
Benjamin Herrenschmidt4be8dc72005-05-01 08:58:43 -0700717 ucontrol->value.integer.value[0] = !check_audio_gpio(gp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 return 0;
719}
720
Takashi Iwai65b29f52005-11-17 15:09:46 +0100721static int tumbler_put_mute_switch(struct snd_kcontrol *kcontrol,
722 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723{
Takashi Iwai65b29f52005-11-17 15:09:46 +0100724 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
725 struct pmac_tumbler *mix;
726 struct pmac_gpio *gp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 int val;
728#ifdef PMAC_SUPPORT_AUTOMUTE
729 if (chip->update_automute && chip->auto_mute)
730 return 0; /* don't touch in the auto-mute mode */
731#endif
732 if (! (mix = chip->mixer_data))
733 return -ENODEV;
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -0700734 switch(kcontrol->private_value) {
735 case TUMBLER_MUTE_HP:
736 gp = &mix->hp_mute; break;
737 case TUMBLER_MUTE_AMP:
738 gp = &mix->amp_mute; break;
739 case TUMBLER_MUTE_LINE:
740 gp = &mix->line_mute; break;
741 default:
742 gp = NULL;
743 }
744 if (gp == NULL)
745 return -EINVAL;
Benjamin Herrenschmidt4be8dc72005-05-01 08:58:43 -0700746 val = ! check_audio_gpio(gp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 if (val != ucontrol->value.integer.value[0]) {
748 write_audio_gpio(gp, ! ucontrol->value.integer.value[0]);
749 return 1;
750 }
751 return 0;
752}
753
Takashi Iwai65b29f52005-11-17 15:09:46 +0100754static int snapper_set_capture_source(struct pmac_tumbler *mix)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755{
756 if (! mix->i2c.client)
757 return -ENODEV;
Benjamin Herrenschmidtb20af5f2005-04-16 15:24:32 -0700758 if (mix->capture_source)
759 mix->acs = mix->acs |= 2;
760 else
761 mix->acs &= ~2;
762 return i2c_smbus_write_byte_data(mix->i2c.client, TAS_REG_ACS, mix->acs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763}
764
Takashi Iwai65b29f52005-11-17 15:09:46 +0100765static int snapper_info_capture_source(struct snd_kcontrol *kcontrol,
766 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767{
768 static char *texts[2] = {
769 "Line", "Mic"
770 };
771 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
772 uinfo->count = 1;
773 uinfo->value.enumerated.items = 2;
774 if (uinfo->value.enumerated.item > 1)
775 uinfo->value.enumerated.item = 1;
776 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
777 return 0;
778}
779
Takashi Iwai65b29f52005-11-17 15:09:46 +0100780static int snapper_get_capture_source(struct snd_kcontrol *kcontrol,
781 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782{
Takashi Iwai65b29f52005-11-17 15:09:46 +0100783 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
784 struct pmac_tumbler *mix = chip->mixer_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
786 snd_assert(mix, return -ENODEV);
787 ucontrol->value.integer.value[0] = mix->capture_source;
788 return 0;
789}
790
Takashi Iwai65b29f52005-11-17 15:09:46 +0100791static int snapper_put_capture_source(struct snd_kcontrol *kcontrol,
792 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793{
Takashi Iwai65b29f52005-11-17 15:09:46 +0100794 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
795 struct pmac_tumbler *mix = chip->mixer_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 int change;
797
798 snd_assert(mix, return -ENODEV);
799 change = ucontrol->value.integer.value[0] != mix->capture_source;
800 if (change) {
801 mix->capture_source = !!ucontrol->value.integer.value[0];
802 snapper_set_capture_source(mix);
803 }
804 return change;
805}
806
807#define DEFINE_SNAPPER_MIX(xname,idx,ofs) { \
808 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,\
809 .name = xname, \
810 .info = snapper_info_mix, \
811 .get = snapper_get_mix, \
812 .put = snapper_put_mix, \
813 .index = idx,\
814 .private_value = ofs, \
815}
816
817
818/*
819 */
Takashi Iwai65b29f52005-11-17 15:09:46 +0100820static struct snd_kcontrol_new tumbler_mixers[] __initdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
822 .name = "Master Playback Volume",
823 .info = tumbler_info_master_volume,
824 .get = tumbler_get_master_volume,
825 .put = tumbler_put_master_volume
826 },
827 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
828 .name = "Master Playback Switch",
829 .info = snd_pmac_boolean_stereo_info,
830 .get = tumbler_get_master_switch,
831 .put = tumbler_put_master_switch
832 },
833 DEFINE_MONO("Tone Control - Bass", bass),
834 DEFINE_MONO("Tone Control - Treble", treble),
835 DEFINE_MONO("PCM Playback Volume", pcm),
836 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
837 .name = "DRC Range",
838 .info = tumbler_info_drc_value,
839 .get = tumbler_get_drc_value,
840 .put = tumbler_put_drc_value
841 },
842};
843
Takashi Iwai65b29f52005-11-17 15:09:46 +0100844static struct snd_kcontrol_new snapper_mixers[] __initdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
846 .name = "Master Playback Volume",
847 .info = tumbler_info_master_volume,
848 .get = tumbler_get_master_volume,
849 .put = tumbler_put_master_volume
850 },
851 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
852 .name = "Master Playback Switch",
853 .info = snd_pmac_boolean_stereo_info,
854 .get = tumbler_get_master_switch,
855 .put = tumbler_put_master_switch
856 },
857 DEFINE_SNAPPER_MIX("PCM Playback Volume", 0, VOL_IDX_PCM),
858 DEFINE_SNAPPER_MIX("PCM Playback Volume", 1, VOL_IDX_PCM2),
859 DEFINE_SNAPPER_MIX("Monitor Mix Volume", 0, VOL_IDX_ADC),
860 DEFINE_SNAPPER_MONO("Tone Control - Bass", bass),
861 DEFINE_SNAPPER_MONO("Tone Control - Treble", treble),
862 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
863 .name = "DRC Range",
864 .info = tumbler_info_drc_value,
865 .get = tumbler_get_drc_value,
866 .put = tumbler_put_drc_value
867 },
868 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
869 .name = "Input Source", /* FIXME: "Capture Source" doesn't work properly */
870 .info = snapper_info_capture_source,
871 .get = snapper_get_capture_source,
872 .put = snapper_put_capture_source
873 },
874};
875
Takashi Iwai65b29f52005-11-17 15:09:46 +0100876static struct snd_kcontrol_new tumbler_hp_sw __initdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
878 .name = "Headphone Playback Switch",
879 .info = snd_pmac_boolean_mono_info,
880 .get = tumbler_get_mute_switch,
881 .put = tumbler_put_mute_switch,
882 .private_value = TUMBLER_MUTE_HP,
883};
Takashi Iwai65b29f52005-11-17 15:09:46 +0100884static struct snd_kcontrol_new tumbler_speaker_sw __initdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
886 .name = "PC Speaker Playback Switch",
887 .info = snd_pmac_boolean_mono_info,
888 .get = tumbler_get_mute_switch,
889 .put = tumbler_put_mute_switch,
890 .private_value = TUMBLER_MUTE_AMP,
891};
Takashi Iwai65b29f52005-11-17 15:09:46 +0100892static struct snd_kcontrol_new tumbler_lineout_sw __initdata = {
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -0700893 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
894 .name = "Line Out Playback Switch",
895 .info = snd_pmac_boolean_mono_info,
896 .get = tumbler_get_mute_switch,
897 .put = tumbler_put_mute_switch,
898 .private_value = TUMBLER_MUTE_LINE,
899};
Takashi Iwai65b29f52005-11-17 15:09:46 +0100900static struct snd_kcontrol_new tumbler_drc_sw __initdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
902 .name = "DRC Switch",
903 .info = snd_pmac_boolean_mono_info,
904 .get = tumbler_get_drc_switch,
905 .put = tumbler_put_drc_switch
906};
907
908
909#ifdef PMAC_SUPPORT_AUTOMUTE
910/*
911 * auto-mute stuffs
912 */
Takashi Iwai65b29f52005-11-17 15:09:46 +0100913static int tumbler_detect_headphone(struct snd_pmac *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914{
Takashi Iwai65b29f52005-11-17 15:09:46 +0100915 struct pmac_tumbler *mix = chip->mixer_data;
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -0700916 int detect = 0;
917
918 if (mix->hp_detect.addr)
919 detect |= read_audio_gpio(&mix->hp_detect);
920 return detect;
921}
922
Takashi Iwai65b29f52005-11-17 15:09:46 +0100923static int tumbler_detect_lineout(struct snd_pmac *chip)
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -0700924{
Takashi Iwai65b29f52005-11-17 15:09:46 +0100925 struct pmac_tumbler *mix = chip->mixer_data;
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -0700926 int detect = 0;
927
928 if (mix->line_detect.addr)
929 detect |= read_audio_gpio(&mix->line_detect);
930 return detect;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931}
932
Takashi Iwai65b29f52005-11-17 15:09:46 +0100933static void check_mute(struct snd_pmac *chip, struct pmac_gpio *gp, int val, int do_notify,
934 struct snd_kcontrol *sw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935{
Benjamin Herrenschmidt4be8dc72005-05-01 08:58:43 -0700936 if (check_audio_gpio(gp) != val) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 write_audio_gpio(gp, val);
938 if (do_notify)
Benjamin Herrenschmidt4be8dc72005-05-01 08:58:43 -0700939 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
940 &sw->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 }
942}
943
944static struct work_struct device_change;
945
Benjamin Herrenschmidtb20af5f2005-04-16 15:24:32 -0700946static void device_change_handler(void *self)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947{
Takashi Iwai65b29f52005-11-17 15:09:46 +0100948 struct snd_pmac *chip = self;
949 struct pmac_tumbler *mix;
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -0700950 int headphone, lineout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
952 if (!chip)
953 return;
954
955 mix = chip->mixer_data;
Benjamin Herrenschmidtb20af5f2005-04-16 15:24:32 -0700956 snd_assert(mix, return);
957
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -0700958 headphone = tumbler_detect_headphone(chip);
959 lineout = tumbler_detect_lineout(chip);
960
961 DBG("headphone: %d, lineout: %d\n", headphone, lineout);
962
963 if (headphone || lineout) {
964 /* unmute headphone/lineout & mute speaker */
965 if (headphone)
966 check_mute(chip, &mix->hp_mute, 0, mix->auto_mute_notify,
967 chip->master_sw_ctl);
968 if (lineout && mix->line_mute.addr != 0)
969 check_mute(chip, &mix->line_mute, 0, mix->auto_mute_notify,
970 chip->lineout_sw_ctl);
Benjamin Herrenschmidtb20af5f2005-04-16 15:24:32 -0700971 if (mix->anded_reset)
Nishanth Aravamudan989a0b22005-07-09 10:53:24 +0200972 msleep(10);
Benjamin Herrenschmidtb20af5f2005-04-16 15:24:32 -0700973 check_mute(chip, &mix->amp_mute, 1, mix->auto_mute_notify,
974 chip->speaker_sw_ctl);
Benjamin Herrenschmidtb20af5f2005-04-16 15:24:32 -0700975 } else {
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -0700976 /* unmute speaker, mute others */
Benjamin Herrenschmidtb20af5f2005-04-16 15:24:32 -0700977 check_mute(chip, &mix->amp_mute, 0, mix->auto_mute_notify,
978 chip->speaker_sw_ctl);
979 if (mix->anded_reset)
Nishanth Aravamudan989a0b22005-07-09 10:53:24 +0200980 msleep(10);
Benjamin Herrenschmidtb20af5f2005-04-16 15:24:32 -0700981 check_mute(chip, &mix->hp_mute, 1, mix->auto_mute_notify,
982 chip->master_sw_ctl);
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -0700983 if (mix->line_mute.addr != 0)
984 check_mute(chip, &mix->line_mute, 1, mix->auto_mute_notify,
985 chip->lineout_sw_ctl);
Benjamin Herrenschmidtb20af5f2005-04-16 15:24:32 -0700986 }
Takashi Iwaif3283852005-09-07 17:08:40 +0200987 if (mix->auto_mute_notify)
Benjamin Herrenschmidtb20af5f2005-04-16 15:24:32 -0700988 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
989 &chip->hp_detect_ctl->id);
Takashi Iwaif3283852005-09-07 17:08:40 +0200990
991#ifdef CONFIG_SND_POWERMAC_AUTO_DRC
992 mix->drc_enable = ! (headphone || lineout);
993 if (mix->auto_mute_notify)
Benjamin Herrenschmidtb20af5f2005-04-16 15:24:32 -0700994 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
995 &chip->drc_sw_ctl->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 if (chip->model == PMAC_TUMBLER)
997 tumbler_set_drc(mix);
998 else
999 snapper_set_drc(mix);
Takashi Iwaif3283852005-09-07 17:08:40 +02001000#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
1002 /* reset the master volume so the correct amplification is applied */
1003 tumbler_set_master_volume(mix);
1004}
1005
Takashi Iwai65b29f52005-11-17 15:09:46 +01001006static void tumbler_update_automute(struct snd_pmac *chip, int do_notify)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007{
1008 if (chip->auto_mute) {
Takashi Iwai65b29f52005-11-17 15:09:46 +01001009 struct pmac_tumbler *mix;
Benjamin Herrenschmidtb20af5f2005-04-16 15:24:32 -07001010 mix = chip->mixer_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 snd_assert(mix, return);
Benjamin Herrenschmidtb20af5f2005-04-16 15:24:32 -07001012 mix->auto_mute_notify = do_notify;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 schedule_work(&device_change);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 }
1015}
1016#endif /* PMAC_SUPPORT_AUTOMUTE */
1017
1018
1019/* interrupt - headphone plug changed */
1020static irqreturn_t headphone_intr(int irq, void *devid, struct pt_regs *regs)
1021{
Takashi Iwai65b29f52005-11-17 15:09:46 +01001022 struct snd_pmac *chip = devid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 if (chip->update_automute && chip->initialized) {
1024 chip->update_automute(chip, 1);
1025 return IRQ_HANDLED;
1026 }
1027 return IRQ_NONE;
1028}
1029
1030/* look for audio-gpio device */
1031static struct device_node *find_audio_device(const char *name)
1032{
1033 struct device_node *np;
1034
1035 if (! (np = find_devices("gpio")))
1036 return NULL;
1037
1038 for (np = np->child; np; np = np->sibling) {
1039 char *property = get_property(np, "audio-gpio", NULL);
1040 if (property && strcmp(property, name) == 0)
1041 return np;
1042 }
1043 return NULL;
1044}
1045
1046/* look for audio-gpio device */
1047static struct device_node *find_compatible_audio_device(const char *name)
1048{
1049 struct device_node *np;
1050
1051 if (! (np = find_devices("gpio")))
1052 return NULL;
1053
1054 for (np = np->child; np; np = np->sibling) {
1055 if (device_is_compatible(np, name))
1056 return np;
1057 }
1058 return NULL;
1059}
1060
1061/* find an audio device and get its address */
Takashi Iwai65b29f52005-11-17 15:09:46 +01001062static long tumbler_find_device(const char *device, const char *platform,
1063 struct pmac_gpio *gp, int is_compatible)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064{
1065 struct device_node *node;
Benjamin Herrenschmidtb75550e2005-04-16 15:24:31 -07001066 u32 *base, addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067
1068 if (is_compatible)
1069 node = find_compatible_audio_device(device);
1070 else
1071 node = find_audio_device(device);
1072 if (! node) {
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -07001073 DBG("(W) cannot find audio device %s !\n", device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 snd_printdd("cannot find device %s\n", device);
1075 return -ENODEV;
1076 }
1077
1078 base = (u32 *)get_property(node, "AAPL,address", NULL);
1079 if (! base) {
Benjamin Herrenschmidtb75550e2005-04-16 15:24:31 -07001080 base = (u32 *)get_property(node, "reg", NULL);
1081 if (!base) {
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -07001082 DBG("(E) cannot find address for device %s !\n", device);
Benjamin Herrenschmidtb75550e2005-04-16 15:24:31 -07001083 snd_printd("cannot find address for device %s\n", device);
1084 return -ENODEV;
1085 }
Benjamin Herrenschmidtb75550e2005-04-16 15:24:31 -07001086 addr = *base;
1087 if (addr < 0x50)
1088 addr += 0x50;
1089 } else
1090 addr = *base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
Benjamin Herrenschmidtb75550e2005-04-16 15:24:31 -07001092 gp->addr = addr & 0x0000ffff;
Benjamin Herrenschmidtb75550e2005-04-16 15:24:31 -07001093 /* Try to find the active state, default to 0 ! */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 base = (u32 *)get_property(node, "audio-gpio-active-state", NULL);
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -07001095 if (base) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 gp->active_state = *base;
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -07001097 gp->active_val = (*base) ? 0x5 : 0x4;
1098 gp->inactive_val = (*base) ? 0x4 : 0x5;
1099 } else {
1100 u32 *prop = NULL;
Benjamin Herrenschmidtb75550e2005-04-16 15:24:31 -07001101 gp->active_state = 0;
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -07001102 gp->active_val = 0x4;
1103 gp->inactive_val = 0x5;
1104 /* Here are some crude hacks to extract the GPIO polarity and
1105 * open collector informations out of the do-platform script
1106 * as we don't yet have an interpreter for these things
1107 */
1108 if (platform)
1109 prop = (u32 *)get_property(node, platform, NULL);
1110 if (prop) {
1111 if (prop[3] == 0x9 && prop[4] == 0x9) {
1112 gp->active_val = 0xd;
1113 gp->inactive_val = 0xc;
1114 }
1115 if (prop[3] == 0x1 && prop[4] == 0x1) {
1116 gp->active_val = 0x5;
1117 gp->inactive_val = 0x4;
1118 }
1119 }
1120 }
1121
1122 DBG("(I) GPIO device %s found, offset: %x, active state: %d !\n",
1123 device, gp->addr, gp->active_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124
1125 return (node->n_intrs > 0) ? node->intrs[0].line : 0;
1126}
1127
1128/* reset audio */
Takashi Iwai65b29f52005-11-17 15:09:46 +01001129static void tumbler_reset_audio(struct snd_pmac *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130{
Takashi Iwai65b29f52005-11-17 15:09:46 +01001131 struct pmac_tumbler *mix = chip->mixer_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
Benjamin Herrenschmidtb20af5f2005-04-16 15:24:32 -07001133 if (mix->anded_reset) {
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -07001134 DBG("(I) codec anded reset !\n");
Benjamin Herrenschmidtb20af5f2005-04-16 15:24:32 -07001135 write_audio_gpio(&mix->hp_mute, 0);
1136 write_audio_gpio(&mix->amp_mute, 0);
Nishanth Aravamudan989a0b22005-07-09 10:53:24 +02001137 msleep(200);
Benjamin Herrenschmidtb20af5f2005-04-16 15:24:32 -07001138 write_audio_gpio(&mix->hp_mute, 1);
1139 write_audio_gpio(&mix->amp_mute, 1);
Nishanth Aravamudan989a0b22005-07-09 10:53:24 +02001140 msleep(100);
Benjamin Herrenschmidtb20af5f2005-04-16 15:24:32 -07001141 write_audio_gpio(&mix->hp_mute, 0);
1142 write_audio_gpio(&mix->amp_mute, 0);
Nishanth Aravamudan989a0b22005-07-09 10:53:24 +02001143 msleep(100);
Benjamin Herrenschmidtb20af5f2005-04-16 15:24:32 -07001144 } else {
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -07001145 DBG("(I) codec normal reset !\n");
1146
Benjamin Herrenschmidtb20af5f2005-04-16 15:24:32 -07001147 write_audio_gpio(&mix->audio_reset, 0);
Nishanth Aravamudan989a0b22005-07-09 10:53:24 +02001148 msleep(200);
Benjamin Herrenschmidtb20af5f2005-04-16 15:24:32 -07001149 write_audio_gpio(&mix->audio_reset, 1);
Nishanth Aravamudan989a0b22005-07-09 10:53:24 +02001150 msleep(100);
Benjamin Herrenschmidtb20af5f2005-04-16 15:24:32 -07001151 write_audio_gpio(&mix->audio_reset, 0);
Nishanth Aravamudan989a0b22005-07-09 10:53:24 +02001152 msleep(100);
Benjamin Herrenschmidtb20af5f2005-04-16 15:24:32 -07001153 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154}
1155
Benjamin Herrenschmidt8c870932005-06-27 14:36:34 -07001156#ifdef CONFIG_PM
Benjamin Herrenschmidtb20af5f2005-04-16 15:24:32 -07001157/* suspend mixer */
Takashi Iwai65b29f52005-11-17 15:09:46 +01001158static void tumbler_suspend(struct snd_pmac *chip)
Benjamin Herrenschmidtb20af5f2005-04-16 15:24:32 -07001159{
Takashi Iwai65b29f52005-11-17 15:09:46 +01001160 struct pmac_tumbler *mix = chip->mixer_data;
Benjamin Herrenschmidtb20af5f2005-04-16 15:24:32 -07001161
1162 if (mix->headphone_irq >= 0)
1163 disable_irq(mix->headphone_irq);
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -07001164 if (mix->lineout_irq >= 0)
1165 disable_irq(mix->lineout_irq);
Benjamin Herrenschmidtb20af5f2005-04-16 15:24:32 -07001166 mix->save_master_switch[0] = mix->master_switch[0];
1167 mix->save_master_switch[1] = mix->master_switch[1];
Colin Leroy085e6fc2005-05-01 08:58:43 -07001168 mix->save_master_vol[0] = mix->master_vol[0];
1169 mix->save_master_vol[1] = mix->master_vol[1];
Benjamin Herrenschmidtb20af5f2005-04-16 15:24:32 -07001170 mix->master_switch[0] = mix->master_switch[1] = 0;
1171 tumbler_set_master_volume(mix);
1172 if (!mix->anded_reset) {
1173 write_audio_gpio(&mix->amp_mute, 1);
1174 write_audio_gpio(&mix->hp_mute, 1);
1175 }
1176 if (chip->model == PMAC_SNAPPER) {
1177 mix->acs |= 1;
1178 i2c_smbus_write_byte_data(mix->i2c.client, TAS_REG_ACS, mix->acs);
1179 }
1180 if (mix->anded_reset) {
1181 write_audio_gpio(&mix->amp_mute, 1);
1182 write_audio_gpio(&mix->hp_mute, 1);
1183 } else
1184 write_audio_gpio(&mix->audio_reset, 1);
1185}
1186
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187/* resume mixer */
Takashi Iwai65b29f52005-11-17 15:09:46 +01001188static void tumbler_resume(struct snd_pmac *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189{
Takashi Iwai65b29f52005-11-17 15:09:46 +01001190 struct pmac_tumbler *mix = chip->mixer_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191
1192 snd_assert(mix, return);
1193
Benjamin Herrenschmidtb20af5f2005-04-16 15:24:32 -07001194 mix->acs &= ~1;
1195 mix->master_switch[0] = mix->save_master_switch[0];
1196 mix->master_switch[1] = mix->save_master_switch[1];
Colin Leroy085e6fc2005-05-01 08:58:43 -07001197 mix->master_vol[0] = mix->save_master_vol[0];
1198 mix->master_vol[1] = mix->save_master_vol[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 tumbler_reset_audio(chip);
1200 if (mix->i2c.client && mix->i2c.init_client) {
1201 if (mix->i2c.init_client(&mix->i2c) < 0)
1202 printk(KERN_ERR "tumbler_init_client error\n");
1203 } else
1204 printk(KERN_ERR "tumbler: i2c is not initialized\n");
1205 if (chip->model == PMAC_TUMBLER) {
1206 tumbler_set_mono_volume(mix, &tumbler_pcm_vol_info);
1207 tumbler_set_mono_volume(mix, &tumbler_bass_vol_info);
1208 tumbler_set_mono_volume(mix, &tumbler_treble_vol_info);
1209 tumbler_set_drc(mix);
1210 } else {
1211 snapper_set_mix_vol(mix, VOL_IDX_PCM);
1212 snapper_set_mix_vol(mix, VOL_IDX_PCM2);
1213 snapper_set_mix_vol(mix, VOL_IDX_ADC);
1214 tumbler_set_mono_volume(mix, &snapper_bass_vol_info);
1215 tumbler_set_mono_volume(mix, &snapper_treble_vol_info);
1216 snapper_set_drc(mix);
1217 snapper_set_capture_source(mix);
1218 }
1219 tumbler_set_master_volume(mix);
1220 if (chip->update_automute)
1221 chip->update_automute(chip, 0);
Guido Guentherc0ce5c52005-11-15 17:28:05 +11001222 if (mix->headphone_irq >= 0) {
1223 unsigned char val;
1224
Benjamin Herrenschmidtb20af5f2005-04-16 15:24:32 -07001225 enable_irq(mix->headphone_irq);
Guido Guentherc0ce5c52005-11-15 17:28:05 +11001226 /* activate headphone status interrupts */
1227 val = do_gpio_read(&mix->hp_detect);
1228 do_gpio_write(&mix->hp_detect, val | 0x80);
1229 }
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -07001230 if (mix->lineout_irq >= 0)
1231 enable_irq(mix->lineout_irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232}
1233#endif
1234
1235/* initialize tumbler */
Takashi Iwai65b29f52005-11-17 15:09:46 +01001236static int __init tumbler_init(struct snd_pmac *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237{
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -07001238 int irq;
Takashi Iwai65b29f52005-11-17 15:09:46 +01001239 struct pmac_tumbler *mix = chip->mixer_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 snd_assert(mix, return -EINVAL);
1241
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -07001242 if (tumbler_find_device("audio-hw-reset",
1243 "platform-do-hw-reset",
1244 &mix->audio_reset, 0) < 0)
1245 tumbler_find_device("hw-reset",
1246 "platform-do-hw-reset",
1247 &mix->audio_reset, 1);
1248 if (tumbler_find_device("amp-mute",
1249 "platform-do-amp-mute",
1250 &mix->amp_mute, 0) < 0)
1251 tumbler_find_device("amp-mute",
1252 "platform-do-amp-mute",
1253 &mix->amp_mute, 1);
1254 if (tumbler_find_device("headphone-mute",
1255 "platform-do-headphone-mute",
1256 &mix->hp_mute, 0) < 0)
1257 tumbler_find_device("headphone-mute",
1258 "platform-do-headphone-mute",
1259 &mix->hp_mute, 1);
1260 if (tumbler_find_device("line-output-mute",
1261 "platform-do-lineout-mute",
1262 &mix->line_mute, 0) < 0)
1263 tumbler_find_device("line-output-mute",
1264 "platform-do-lineout-mute",
1265 &mix->line_mute, 1);
1266 irq = tumbler_find_device("headphone-detect",
1267 NULL, &mix->hp_detect, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 if (irq < 0)
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -07001269 irq = tumbler_find_device("headphone-detect",
1270 NULL, &mix->hp_detect, 1);
Benjamin Herrenschmidtb75550e2005-04-16 15:24:31 -07001271 if (irq < 0)
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -07001272 irq = tumbler_find_device("keywest-gpio15",
1273 NULL, &mix->hp_detect, 1);
1274 mix->headphone_irq = irq;
1275 irq = tumbler_find_device("line-output-detect",
1276 NULL, &mix->line_detect, 0);
1277 if (irq < 0)
1278 irq = tumbler_find_device("line-output-detect",
1279 NULL, &mix->line_detect, 1);
1280 mix->lineout_irq = irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281
1282 tumbler_reset_audio(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283
1284 return 0;
1285}
1286
Takashi Iwai65b29f52005-11-17 15:09:46 +01001287static void tumbler_cleanup(struct snd_pmac *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288{
Takashi Iwai65b29f52005-11-17 15:09:46 +01001289 struct pmac_tumbler *mix = chip->mixer_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 if (! mix)
1291 return;
1292
1293 if (mix->headphone_irq >= 0)
1294 free_irq(mix->headphone_irq, chip);
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -07001295 if (mix->lineout_irq >= 0)
1296 free_irq(mix->lineout_irq, chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 tumbler_gpio_free(&mix->audio_reset);
1298 tumbler_gpio_free(&mix->amp_mute);
1299 tumbler_gpio_free(&mix->hp_mute);
1300 tumbler_gpio_free(&mix->hp_detect);
1301 snd_pmac_keywest_cleanup(&mix->i2c);
1302 kfree(mix);
1303 chip->mixer_data = NULL;
1304}
1305
1306/* exported */
Takashi Iwai65b29f52005-11-17 15:09:46 +01001307int __init snd_pmac_tumbler_init(struct snd_pmac *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308{
1309 int i, err;
Takashi Iwai65b29f52005-11-17 15:09:46 +01001310 struct pmac_tumbler *mix;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 u32 *paddr;
Benjamin Herrenschmidtb20af5f2005-04-16 15:24:32 -07001312 struct device_node *tas_node, *np;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 char *chipname;
1314
1315#ifdef CONFIG_KMOD
1316 if (current->fs->root)
1317 request_module("i2c-keywest");
1318#endif /* CONFIG_KMOD */
1319
1320 mix = kmalloc(sizeof(*mix), GFP_KERNEL);
1321 if (! mix)
1322 return -ENOMEM;
1323 memset(mix, 0, sizeof(*mix));
1324 mix->headphone_irq = -1;
1325
1326 chip->mixer_data = mix;
1327 chip->mixer_free = tumbler_cleanup;
Benjamin Herrenschmidtb20af5f2005-04-16 15:24:32 -07001328 mix->anded_reset = 0;
1329 mix->reset_on_sleep = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330
Benjamin Herrenschmidtb20af5f2005-04-16 15:24:32 -07001331 for (np = chip->node->child; np; np = np->sibling) {
1332 if (!strcmp(np->name, "sound")) {
1333 if (get_property(np, "has-anded-reset", NULL))
1334 mix->anded_reset = 1;
1335 if (get_property(np, "layout-id", NULL))
1336 mix->reset_on_sleep = 0;
1337 break;
1338 }
1339 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 if ((err = tumbler_init(chip)) < 0)
1341 return err;
1342
1343 /* set up TAS */
1344 tas_node = find_devices("deq");
1345 if (tas_node == NULL)
Benjamin Herrenschmidtb75550e2005-04-16 15:24:31 -07001346 tas_node = find_devices("codec");
1347 if (tas_node == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 return -ENODEV;
1349
1350 paddr = (u32 *)get_property(tas_node, "i2c-address", NULL);
Benjamin Herrenschmidtb75550e2005-04-16 15:24:31 -07001351 if (paddr == NULL)
1352 paddr = (u32 *)get_property(tas_node, "reg", NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 if (paddr)
1354 mix->i2c.addr = (*paddr) >> 1;
1355 else
1356 mix->i2c.addr = TAS_I2C_ADDR;
1357
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -07001358 DBG("(I) TAS i2c address is: %x\n", mix->i2c.addr);
1359
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 if (chip->model == PMAC_TUMBLER) {
1361 mix->i2c.init_client = tumbler_init_client;
1362 mix->i2c.name = "TAS3001c";
1363 chipname = "Tumbler";
1364 } else {
1365 mix->i2c.init_client = snapper_init_client;
1366 mix->i2c.name = "TAS3004";
1367 chipname = "Snapper";
1368 }
1369
1370 if ((err = snd_pmac_keywest_init(&mix->i2c)) < 0)
1371 return err;
1372
1373 /*
1374 * build mixers
1375 */
1376 sprintf(chip->card->mixername, "PowerMac %s", chipname);
1377
1378 if (chip->model == PMAC_TUMBLER) {
1379 for (i = 0; i < ARRAY_SIZE(tumbler_mixers); i++) {
1380 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&tumbler_mixers[i], chip))) < 0)
1381 return err;
1382 }
1383 } else {
1384 for (i = 0; i < ARRAY_SIZE(snapper_mixers); i++) {
1385 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snapper_mixers[i], chip))) < 0)
1386 return err;
1387 }
1388 }
1389 chip->master_sw_ctl = snd_ctl_new1(&tumbler_hp_sw, chip);
1390 if ((err = snd_ctl_add(chip->card, chip->master_sw_ctl)) < 0)
1391 return err;
1392 chip->speaker_sw_ctl = snd_ctl_new1(&tumbler_speaker_sw, chip);
1393 if ((err = snd_ctl_add(chip->card, chip->speaker_sw_ctl)) < 0)
1394 return err;
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -07001395 if (mix->line_mute.addr != 0) {
1396 chip->lineout_sw_ctl = snd_ctl_new1(&tumbler_lineout_sw, chip);
1397 if ((err = snd_ctl_add(chip->card, chip->lineout_sw_ctl)) < 0)
1398 return err;
1399 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 chip->drc_sw_ctl = snd_ctl_new1(&tumbler_drc_sw, chip);
1401 if ((err = snd_ctl_add(chip->card, chip->drc_sw_ctl)) < 0)
1402 return err;
1403
Takashi Iwaif3283852005-09-07 17:08:40 +02001404 /* set initial DRC range to 60% */
1405 if (chip->model == PMAC_TUMBLER)
1406 mix->drc_range = (TAS3001_DRC_MAX * 6) / 10;
1407 else
1408 mix->drc_range = (TAS3004_DRC_MAX * 6) / 10;
1409 mix->drc_enable = 1; /* will be changed later if AUTO_DRC is set */
1410 if (chip->model == PMAC_TUMBLER)
1411 tumbler_set_drc(mix);
1412 else
1413 snapper_set_drc(mix);
1414
Benjamin Herrenschmidt8c870932005-06-27 14:36:34 -07001415#ifdef CONFIG_PM
Benjamin Herrenschmidtb20af5f2005-04-16 15:24:32 -07001416 chip->suspend = tumbler_suspend;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 chip->resume = tumbler_resume;
1418#endif
1419
1420 INIT_WORK(&device_change, device_change_handler, (void *)chip);
1421
1422#ifdef PMAC_SUPPORT_AUTOMUTE
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -07001423 if ((mix->headphone_irq >=0 || mix->lineout_irq >= 0)
1424 && (err = snd_pmac_add_automute(chip)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 return err;
1426 chip->detect_headphone = tumbler_detect_headphone;
1427 chip->update_automute = tumbler_update_automute;
1428 tumbler_update_automute(chip, 0); /* update the status only */
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -07001429
1430 /* activate headphone status interrupts */
1431 if (mix->headphone_irq >= 0) {
1432 unsigned char val;
1433 if ((err = request_irq(mix->headphone_irq, headphone_intr, 0,
1434 "Sound Headphone Detection", chip)) < 0)
1435 return 0;
1436 /* activate headphone status interrupts */
1437 val = do_gpio_read(&mix->hp_detect);
1438 do_gpio_write(&mix->hp_detect, val | 0x80);
1439 }
1440 if (mix->lineout_irq >= 0) {
1441 unsigned char val;
1442 if ((err = request_irq(mix->lineout_irq, headphone_intr, 0,
1443 "Sound Lineout Detection", chip)) < 0)
1444 return 0;
1445 /* activate headphone status interrupts */
1446 val = do_gpio_read(&mix->line_detect);
1447 do_gpio_write(&mix->line_detect, val | 0x80);
1448 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449#endif
1450
1451 return 0;
1452}