blob: 70e4ebc70260deea11b06670272e863b29da7340 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/kmod.h>
32#include <linux/slab.h>
33#include <linux/interrupt.h>
34#include <sound/core.h>
35#include <asm/io.h>
36#include <asm/irq.h>
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -070037#include <asm/machdep.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <asm/pmac_feature.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include "pmac.h"
40#include "tumbler_volume.h"
41
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -070042#undef DEBUG
43
44#ifdef DEBUG
45#define DBG(fmt...) printk(fmt)
46#else
47#define DBG(fmt...)
48#endif
49
Linus Torvalds1da177e2005-04-16 15:20:36 -070050/* i2c address for tumbler */
51#define TAS_I2C_ADDR 0x34
52
53/* registers */
54#define TAS_REG_MCS 0x01 /* main control */
55#define TAS_REG_DRC 0x02
56#define TAS_REG_VOL 0x04
57#define TAS_REG_TREBLE 0x05
58#define TAS_REG_BASS 0x06
59#define TAS_REG_INPUT1 0x07
60#define TAS_REG_INPUT2 0x08
61
62/* tas3001c */
63#define TAS_REG_PCM TAS_REG_INPUT1
64
65/* tas3004 */
66#define TAS_REG_LMIX TAS_REG_INPUT1
67#define TAS_REG_RMIX TAS_REG_INPUT2
68#define TAS_REG_MCS2 0x43 /* main control 2 */
69#define TAS_REG_ACS 0x40 /* analog control */
70
71/* mono volumes for tas3001c/tas3004 */
72enum {
73 VOL_IDX_PCM_MONO, /* tas3001c only */
74 VOL_IDX_BASS, VOL_IDX_TREBLE,
75 VOL_IDX_LAST_MONO
76};
77
78/* stereo volumes for tas3004 */
79enum {
80 VOL_IDX_PCM, VOL_IDX_PCM2, VOL_IDX_ADC,
81 VOL_IDX_LAST_MIX
82};
83
Takashi Iwai65b29f52005-11-17 15:09:46 +010084struct pmac_gpio {
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 unsigned int addr;
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -070086 u8 active_val;
87 u8 inactive_val;
88 u8 active_state;
Takashi Iwai65b29f52005-11-17 15:09:46 +010089};
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Takashi Iwai65b29f52005-11-17 15:09:46 +010091struct pmac_tumbler {
92 struct pmac_keywest i2c;
93 struct pmac_gpio audio_reset;
94 struct pmac_gpio amp_mute;
95 struct pmac_gpio line_mute;
96 struct pmac_gpio line_detect;
97 struct pmac_gpio hp_mute;
98 struct pmac_gpio hp_detect;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 int headphone_irq;
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -0700100 int lineout_irq;
Colin Leroy085e6fc2005-05-01 08:58:43 -0700101 unsigned int save_master_vol[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 unsigned int master_vol[2];
Benjamin Herrenschmidtb20af5f2005-04-16 15:24:32 -0700103 unsigned int save_master_switch[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 unsigned int master_switch[2];
105 unsigned int mono_vol[VOL_IDX_LAST_MONO];
106 unsigned int mix_vol[VOL_IDX_LAST_MIX][2]; /* stereo volumes for tas3004 */
107 int drc_range;
108 int drc_enable;
109 int capture_source;
Benjamin Herrenschmidtb20af5f2005-04-16 15:24:32 -0700110 int anded_reset;
111 int auto_mute_notify;
112 int reset_on_sleep;
113 u8 acs;
Takashi Iwai65b29f52005-11-17 15:09:46 +0100114};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
116
117/*
118 */
119
Takashi Iwai65b29f52005-11-17 15:09:46 +0100120static int send_init_client(struct pmac_keywest *i2c, unsigned int *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121{
122 while (*regs > 0) {
123 int err, count = 10;
124 do {
125 err = i2c_smbus_write_byte_data(i2c->client,
126 regs[0], regs[1]);
127 if (err >= 0)
128 break;
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -0700129 DBG("(W) i2c error %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 mdelay(10);
131 } while (count--);
132 if (err < 0)
133 return -ENXIO;
134 regs += 2;
135 }
136 return 0;
137}
138
139
Takashi Iwai65b29f52005-11-17 15:09:46 +0100140static int tumbler_init_client(struct pmac_keywest *i2c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141{
142 static unsigned int regs[] = {
143 /* normal operation, SCLK=64fps, i2s output, i2s input, 16bit width */
144 TAS_REG_MCS, (1<<6)|(2<<4)|(2<<2)|0,
145 0, /* terminator */
146 };
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -0700147 DBG("(I) tumbler init client\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 return send_init_client(i2c, regs);
149}
150
Takashi Iwai65b29f52005-11-17 15:09:46 +0100151static int snapper_init_client(struct pmac_keywest *i2c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152{
153 static unsigned int regs[] = {
154 /* normal operation, SCLK=64fps, i2s output, 16bit width */
155 TAS_REG_MCS, (1<<6)|(2<<4)|0,
156 /* normal operation, all-pass mode */
157 TAS_REG_MCS2, (1<<1),
158 /* normal output, no deemphasis, A input, power-up, line-in */
159 TAS_REG_ACS, 0,
160 0, /* terminator */
161 };
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -0700162 DBG("(I) snapper init client\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 return send_init_client(i2c, regs);
164}
165
166/*
167 * gpio access
168 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169#define do_gpio_write(gp, val) \
170 pmac_call_feature(PMAC_FTR_WRITE_GPIO, NULL, (gp)->addr, val)
171#define do_gpio_read(gp) \
172 pmac_call_feature(PMAC_FTR_READ_GPIO, NULL, (gp)->addr, 0)
173#define tumbler_gpio_free(gp) /* NOP */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
Takashi Iwai65b29f52005-11-17 15:09:46 +0100175static void write_audio_gpio(struct pmac_gpio *gp, int active)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176{
177 if (! gp->addr)
178 return;
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -0700179 active = active ? gp->active_val : gp->inactive_val;
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -0700180 do_gpio_write(gp, active);
181 DBG("(I) gpio %x write %d\n", gp->addr, active);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182}
183
Takashi Iwai65b29f52005-11-17 15:09:46 +0100184static int check_audio_gpio(struct pmac_gpio *gp)
Benjamin Herrenschmidt4be8dc72005-05-01 08:58:43 -0700185{
186 int ret;
187
188 if (! gp->addr)
189 return 0;
190
191 ret = do_gpio_read(gp);
192
193 return (ret & 0xd) == (gp->active_val & 0xd);
194}
195
Takashi Iwai65b29f52005-11-17 15:09:46 +0100196static int read_audio_gpio(struct pmac_gpio *gp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197{
198 int ret;
199 if (! gp->addr)
200 return 0;
201 ret = ((do_gpio_read(gp) & 0x02) !=0);
202 return ret == gp->active_state;
203}
204
205/*
206 * update master volume
207 */
Takashi Iwai65b29f52005-11-17 15:09:46 +0100208static int tumbler_set_master_volume(struct pmac_tumbler *mix)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209{
210 unsigned char block[6];
211 unsigned int left_vol, right_vol;
212
213 if (! mix->i2c.client)
214 return -ENODEV;
215
216 if (! mix->master_switch[0])
217 left_vol = 0;
218 else {
219 left_vol = mix->master_vol[0];
220 if (left_vol >= ARRAY_SIZE(master_volume_table))
221 left_vol = ARRAY_SIZE(master_volume_table) - 1;
222 left_vol = master_volume_table[left_vol];
223 }
224 if (! mix->master_switch[1])
225 right_vol = 0;
226 else {
227 right_vol = mix->master_vol[1];
228 if (right_vol >= ARRAY_SIZE(master_volume_table))
229 right_vol = ARRAY_SIZE(master_volume_table) - 1;
230 right_vol = master_volume_table[right_vol];
231 }
232
233 block[0] = (left_vol >> 16) & 0xff;
234 block[1] = (left_vol >> 8) & 0xff;
235 block[2] = (left_vol >> 0) & 0xff;
236
237 block[3] = (right_vol >> 16) & 0xff;
238 block[4] = (right_vol >> 8) & 0xff;
239 block[5] = (right_vol >> 0) & 0xff;
240
Benjamin Herrenschmidt367636e2006-02-08 15:04:18 +1100241 if (i2c_smbus_write_i2c_block_data(mix->i2c.client, TAS_REG_VOL, 6,
242 block) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 snd_printk("failed to set volume \n");
244 return -EINVAL;
245 }
246 return 0;
247}
248
249
250/* output volume */
Takashi Iwai65b29f52005-11-17 15:09:46 +0100251static int tumbler_info_master_volume(struct snd_kcontrol *kcontrol,
252 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253{
254 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
255 uinfo->count = 2;
256 uinfo->value.integer.min = 0;
257 uinfo->value.integer.max = ARRAY_SIZE(master_volume_table) - 1;
258 return 0;
259}
260
Takashi Iwai65b29f52005-11-17 15:09:46 +0100261static int tumbler_get_master_volume(struct snd_kcontrol *kcontrol,
262 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263{
Takashi Iwai65b29f52005-11-17 15:09:46 +0100264 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
265 struct pmac_tumbler *mix = chip->mixer_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 snd_assert(mix, return -ENODEV);
267 ucontrol->value.integer.value[0] = mix->master_vol[0];
268 ucontrol->value.integer.value[1] = mix->master_vol[1];
269 return 0;
270}
271
Takashi Iwai65b29f52005-11-17 15:09:46 +0100272static int tumbler_put_master_volume(struct snd_kcontrol *kcontrol,
273 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274{
Takashi Iwai65b29f52005-11-17 15:09:46 +0100275 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
276 struct pmac_tumbler *mix = chip->mixer_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 int change;
278
279 snd_assert(mix, return -ENODEV);
280 change = mix->master_vol[0] != ucontrol->value.integer.value[0] ||
281 mix->master_vol[1] != ucontrol->value.integer.value[1];
282 if (change) {
283 mix->master_vol[0] = ucontrol->value.integer.value[0];
284 mix->master_vol[1] = ucontrol->value.integer.value[1];
285 tumbler_set_master_volume(mix);
286 }
287 return change;
288}
289
290/* output switch */
Takashi Iwai65b29f52005-11-17 15:09:46 +0100291static int tumbler_get_master_switch(struct snd_kcontrol *kcontrol,
292 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293{
Takashi Iwai65b29f52005-11-17 15:09:46 +0100294 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
295 struct pmac_tumbler *mix = chip->mixer_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 snd_assert(mix, return -ENODEV);
297 ucontrol->value.integer.value[0] = mix->master_switch[0];
298 ucontrol->value.integer.value[1] = mix->master_switch[1];
299 return 0;
300}
301
Takashi Iwai65b29f52005-11-17 15:09:46 +0100302static int tumbler_put_master_switch(struct snd_kcontrol *kcontrol,
303 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304{
Takashi Iwai65b29f52005-11-17 15:09:46 +0100305 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
306 struct pmac_tumbler *mix = chip->mixer_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 int change;
308
309 snd_assert(mix, return -ENODEV);
310 change = mix->master_switch[0] != ucontrol->value.integer.value[0] ||
311 mix->master_switch[1] != ucontrol->value.integer.value[1];
312 if (change) {
313 mix->master_switch[0] = !!ucontrol->value.integer.value[0];
314 mix->master_switch[1] = !!ucontrol->value.integer.value[1];
315 tumbler_set_master_volume(mix);
316 }
317 return change;
318}
319
320
321/*
322 * TAS3001c dynamic range compression
323 */
324
325#define TAS3001_DRC_MAX 0x5f
326
Takashi Iwai65b29f52005-11-17 15:09:46 +0100327static int tumbler_set_drc(struct pmac_tumbler *mix)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328{
329 unsigned char val[2];
330
331 if (! mix->i2c.client)
332 return -ENODEV;
333
334 if (mix->drc_enable) {
335 val[0] = 0xc1; /* enable, 3:1 compression */
336 if (mix->drc_range > TAS3001_DRC_MAX)
337 val[1] = 0xf0;
338 else if (mix->drc_range < 0)
339 val[1] = 0x91;
340 else
341 val[1] = mix->drc_range + 0x91;
342 } else {
343 val[0] = 0;
344 val[1] = 0;
345 }
346
Benjamin Herrenschmidt367636e2006-02-08 15:04:18 +1100347 if (i2c_smbus_write_i2c_block_data(mix->i2c.client, TAS_REG_DRC,
348 2, val) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 snd_printk("failed to set DRC\n");
350 return -EINVAL;
351 }
352 return 0;
353}
354
355/*
356 * TAS3004
357 */
358
359#define TAS3004_DRC_MAX 0xef
360
Takashi Iwai65b29f52005-11-17 15:09:46 +0100361static int snapper_set_drc(struct pmac_tumbler *mix)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362{
363 unsigned char val[6];
364
365 if (! mix->i2c.client)
366 return -ENODEV;
367
368 if (mix->drc_enable)
369 val[0] = 0x50; /* 3:1 above threshold */
370 else
371 val[0] = 0x51; /* disabled */
372 val[1] = 0x02; /* 1:1 below threshold */
373 if (mix->drc_range > 0xef)
374 val[2] = 0xef;
375 else if (mix->drc_range < 0)
376 val[2] = 0x00;
377 else
378 val[2] = mix->drc_range;
379 val[3] = 0xb0;
380 val[4] = 0x60;
381 val[5] = 0xa0;
382
Benjamin Herrenschmidt367636e2006-02-08 15:04:18 +1100383 if (i2c_smbus_write_i2c_block_data(mix->i2c.client, TAS_REG_DRC,
384 6, val) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 snd_printk("failed to set DRC\n");
386 return -EINVAL;
387 }
388 return 0;
389}
390
Takashi Iwai65b29f52005-11-17 15:09:46 +0100391static int tumbler_info_drc_value(struct snd_kcontrol *kcontrol,
392 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393{
Takashi Iwai65b29f52005-11-17 15:09:46 +0100394 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
396 uinfo->count = 1;
397 uinfo->value.integer.min = 0;
398 uinfo->value.integer.max =
399 chip->model == PMAC_TUMBLER ? TAS3001_DRC_MAX : TAS3004_DRC_MAX;
400 return 0;
401}
402
Takashi Iwai65b29f52005-11-17 15:09:46 +0100403static int tumbler_get_drc_value(struct snd_kcontrol *kcontrol,
404 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405{
Takashi Iwai65b29f52005-11-17 15:09:46 +0100406 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
407 struct pmac_tumbler *mix;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 if (! (mix = chip->mixer_data))
409 return -ENODEV;
410 ucontrol->value.integer.value[0] = mix->drc_range;
411 return 0;
412}
413
Takashi Iwai65b29f52005-11-17 15:09:46 +0100414static int tumbler_put_drc_value(struct snd_kcontrol *kcontrol,
415 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416{
Takashi Iwai65b29f52005-11-17 15:09:46 +0100417 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
418 struct pmac_tumbler *mix;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 int change;
420
421 if (! (mix = chip->mixer_data))
422 return -ENODEV;
423 change = mix->drc_range != ucontrol->value.integer.value[0];
424 if (change) {
425 mix->drc_range = ucontrol->value.integer.value[0];
426 if (chip->model == PMAC_TUMBLER)
427 tumbler_set_drc(mix);
428 else
429 snapper_set_drc(mix);
430 }
431 return change;
432}
433
Takashi Iwai65b29f52005-11-17 15:09:46 +0100434static int tumbler_get_drc_switch(struct snd_kcontrol *kcontrol,
435 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436{
Takashi Iwai65b29f52005-11-17 15:09:46 +0100437 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
438 struct pmac_tumbler *mix;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 if (! (mix = chip->mixer_data))
440 return -ENODEV;
441 ucontrol->value.integer.value[0] = mix->drc_enable;
442 return 0;
443}
444
Takashi Iwai65b29f52005-11-17 15:09:46 +0100445static int tumbler_put_drc_switch(struct snd_kcontrol *kcontrol,
446 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447{
Takashi Iwai65b29f52005-11-17 15:09:46 +0100448 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
449 struct pmac_tumbler *mix;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 int change;
451
452 if (! (mix = chip->mixer_data))
453 return -ENODEV;
454 change = mix->drc_enable != ucontrol->value.integer.value[0];
455 if (change) {
456 mix->drc_enable = !!ucontrol->value.integer.value[0];
457 if (chip->model == PMAC_TUMBLER)
458 tumbler_set_drc(mix);
459 else
460 snapper_set_drc(mix);
461 }
462 return change;
463}
464
465
466/*
467 * mono volumes
468 */
469
470struct tumbler_mono_vol {
471 int index;
472 int reg;
473 int bytes;
474 unsigned int max;
475 unsigned int *table;
476};
477
Takashi Iwai65b29f52005-11-17 15:09:46 +0100478static int tumbler_set_mono_volume(struct pmac_tumbler *mix,
479 struct tumbler_mono_vol *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480{
481 unsigned char block[4];
482 unsigned int vol;
483 int i;
484
485 if (! mix->i2c.client)
486 return -ENODEV;
487
488 vol = mix->mono_vol[info->index];
489 if (vol >= info->max)
490 vol = info->max - 1;
491 vol = info->table[vol];
492 for (i = 0; i < info->bytes; i++)
493 block[i] = (vol >> ((info->bytes - i - 1) * 8)) & 0xff;
Benjamin Herrenschmidt367636e2006-02-08 15:04:18 +1100494 if (i2c_smbus_write_i2c_block_data(mix->i2c.client, info->reg,
495 info->bytes, block) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 snd_printk("failed to set mono volume %d\n", info->index);
497 return -EINVAL;
498 }
499 return 0;
500}
501
Takashi Iwai65b29f52005-11-17 15:09:46 +0100502static int tumbler_info_mono(struct snd_kcontrol *kcontrol,
503 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504{
505 struct tumbler_mono_vol *info = (struct tumbler_mono_vol *)kcontrol->private_value;
506
507 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
508 uinfo->count = 1;
509 uinfo->value.integer.min = 0;
510 uinfo->value.integer.max = info->max - 1;
511 return 0;
512}
513
Takashi Iwai65b29f52005-11-17 15:09:46 +0100514static int tumbler_get_mono(struct snd_kcontrol *kcontrol,
515 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516{
517 struct tumbler_mono_vol *info = (struct tumbler_mono_vol *)kcontrol->private_value;
Takashi Iwai65b29f52005-11-17 15:09:46 +0100518 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
519 struct pmac_tumbler *mix;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 if (! (mix = chip->mixer_data))
521 return -ENODEV;
522 ucontrol->value.integer.value[0] = mix->mono_vol[info->index];
523 return 0;
524}
525
Takashi Iwai65b29f52005-11-17 15:09:46 +0100526static int tumbler_put_mono(struct snd_kcontrol *kcontrol,
527 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528{
529 struct tumbler_mono_vol *info = (struct tumbler_mono_vol *)kcontrol->private_value;
Takashi Iwai65b29f52005-11-17 15:09:46 +0100530 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
531 struct pmac_tumbler *mix;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 int change;
533
534 if (! (mix = chip->mixer_data))
535 return -ENODEV;
536 change = mix->mono_vol[info->index] != ucontrol->value.integer.value[0];
537 if (change) {
538 mix->mono_vol[info->index] = ucontrol->value.integer.value[0];
539 tumbler_set_mono_volume(mix, info);
540 }
541 return change;
542}
543
544/* TAS3001c mono volumes */
545static struct tumbler_mono_vol tumbler_pcm_vol_info = {
546 .index = VOL_IDX_PCM_MONO,
547 .reg = TAS_REG_PCM,
548 .bytes = 3,
549 .max = ARRAY_SIZE(mixer_volume_table),
550 .table = mixer_volume_table,
551};
552
553static struct tumbler_mono_vol tumbler_bass_vol_info = {
554 .index = VOL_IDX_BASS,
555 .reg = TAS_REG_BASS,
556 .bytes = 1,
557 .max = ARRAY_SIZE(bass_volume_table),
558 .table = bass_volume_table,
559};
560
561static struct tumbler_mono_vol tumbler_treble_vol_info = {
562 .index = VOL_IDX_TREBLE,
563 .reg = TAS_REG_TREBLE,
564 .bytes = 1,
565 .max = ARRAY_SIZE(treble_volume_table),
566 .table = treble_volume_table,
567};
568
569/* TAS3004 mono volumes */
570static struct tumbler_mono_vol snapper_bass_vol_info = {
571 .index = VOL_IDX_BASS,
572 .reg = TAS_REG_BASS,
573 .bytes = 1,
574 .max = ARRAY_SIZE(snapper_bass_volume_table),
575 .table = snapper_bass_volume_table,
576};
577
578static struct tumbler_mono_vol snapper_treble_vol_info = {
579 .index = VOL_IDX_TREBLE,
580 .reg = TAS_REG_TREBLE,
581 .bytes = 1,
582 .max = ARRAY_SIZE(snapper_treble_volume_table),
583 .table = snapper_treble_volume_table,
584};
585
586
587#define DEFINE_MONO(xname,type) { \
588 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,\
589 .name = xname, \
590 .info = tumbler_info_mono, \
591 .get = tumbler_get_mono, \
592 .put = tumbler_put_mono, \
593 .private_value = (unsigned long)(&tumbler_##type##_vol_info), \
594}
595
596#define DEFINE_SNAPPER_MONO(xname,type) { \
597 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,\
598 .name = xname, \
599 .info = tumbler_info_mono, \
600 .get = tumbler_get_mono, \
601 .put = tumbler_put_mono, \
602 .private_value = (unsigned long)(&snapper_##type##_vol_info), \
603}
604
605
606/*
607 * snapper mixer volumes
608 */
609
Takashi Iwai65b29f52005-11-17 15:09:46 +0100610static int snapper_set_mix_vol1(struct pmac_tumbler *mix, int idx, int ch, int reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611{
612 int i, j, vol;
613 unsigned char block[9];
614
615 vol = mix->mix_vol[idx][ch];
616 if (vol >= ARRAY_SIZE(mixer_volume_table)) {
617 vol = ARRAY_SIZE(mixer_volume_table) - 1;
618 mix->mix_vol[idx][ch] = vol;
619 }
620
621 for (i = 0; i < 3; i++) {
622 vol = mix->mix_vol[i][ch];
623 vol = mixer_volume_table[vol];
624 for (j = 0; j < 3; j++)
625 block[i * 3 + j] = (vol >> ((2 - j) * 8)) & 0xff;
626 }
Benjamin Herrenschmidt367636e2006-02-08 15:04:18 +1100627 if (i2c_smbus_write_i2c_block_data(mix->i2c.client, reg,
628 9, block) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 snd_printk("failed to set mono volume %d\n", reg);
630 return -EINVAL;
631 }
632 return 0;
633}
634
Takashi Iwai65b29f52005-11-17 15:09:46 +0100635static int snapper_set_mix_vol(struct pmac_tumbler *mix, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636{
637 if (! mix->i2c.client)
638 return -ENODEV;
639 if (snapper_set_mix_vol1(mix, idx, 0, TAS_REG_LMIX) < 0 ||
640 snapper_set_mix_vol1(mix, idx, 1, TAS_REG_RMIX) < 0)
641 return -EINVAL;
642 return 0;
643}
644
Takashi Iwai65b29f52005-11-17 15:09:46 +0100645static int snapper_info_mix(struct snd_kcontrol *kcontrol,
646 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647{
648 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
649 uinfo->count = 2;
650 uinfo->value.integer.min = 0;
651 uinfo->value.integer.max = ARRAY_SIZE(mixer_volume_table) - 1;
652 return 0;
653}
654
Takashi Iwai65b29f52005-11-17 15:09:46 +0100655static int snapper_get_mix(struct snd_kcontrol *kcontrol,
656 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657{
658 int idx = (int)kcontrol->private_value;
Takashi Iwai65b29f52005-11-17 15:09:46 +0100659 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
660 struct pmac_tumbler *mix;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 if (! (mix = chip->mixer_data))
662 return -ENODEV;
663 ucontrol->value.integer.value[0] = mix->mix_vol[idx][0];
664 ucontrol->value.integer.value[1] = mix->mix_vol[idx][1];
665 return 0;
666}
667
Takashi Iwai65b29f52005-11-17 15:09:46 +0100668static int snapper_put_mix(struct snd_kcontrol *kcontrol,
669 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670{
671 int idx = (int)kcontrol->private_value;
Takashi Iwai65b29f52005-11-17 15:09:46 +0100672 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
673 struct pmac_tumbler *mix;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 int change;
675
676 if (! (mix = chip->mixer_data))
677 return -ENODEV;
678 change = mix->mix_vol[idx][0] != ucontrol->value.integer.value[0] ||
679 mix->mix_vol[idx][1] != ucontrol->value.integer.value[1];
680 if (change) {
681 mix->mix_vol[idx][0] = ucontrol->value.integer.value[0];
682 mix->mix_vol[idx][1] = ucontrol->value.integer.value[1];
683 snapper_set_mix_vol(mix, idx);
684 }
685 return change;
686}
687
688
689/*
Benjamin Herrenschmidtb20af5f2005-04-16 15:24:32 -0700690 * mute switches. FIXME: Turn that into software mute when both outputs are muted
691 * to avoid codec reset on ibook M7
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 */
693
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -0700694enum { TUMBLER_MUTE_HP, TUMBLER_MUTE_AMP, TUMBLER_MUTE_LINE };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
Takashi Iwai65b29f52005-11-17 15:09:46 +0100696static int tumbler_get_mute_switch(struct snd_kcontrol *kcontrol,
697 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698{
Takashi Iwai65b29f52005-11-17 15:09:46 +0100699 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
700 struct pmac_tumbler *mix;
701 struct pmac_gpio *gp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 if (! (mix = chip->mixer_data))
703 return -ENODEV;
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -0700704 switch(kcontrol->private_value) {
705 case TUMBLER_MUTE_HP:
706 gp = &mix->hp_mute; break;
707 case TUMBLER_MUTE_AMP:
708 gp = &mix->amp_mute; break;
709 case TUMBLER_MUTE_LINE:
710 gp = &mix->line_mute; break;
711 default:
712 gp = NULL;
713 }
714 if (gp == NULL)
715 return -EINVAL;
Benjamin Herrenschmidt4be8dc72005-05-01 08:58:43 -0700716 ucontrol->value.integer.value[0] = !check_audio_gpio(gp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 return 0;
718}
719
Takashi Iwai65b29f52005-11-17 15:09:46 +0100720static int tumbler_put_mute_switch(struct snd_kcontrol *kcontrol,
721 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722{
Takashi Iwai65b29f52005-11-17 15:09:46 +0100723 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
724 struct pmac_tumbler *mix;
725 struct pmac_gpio *gp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 int val;
727#ifdef PMAC_SUPPORT_AUTOMUTE
728 if (chip->update_automute && chip->auto_mute)
729 return 0; /* don't touch in the auto-mute mode */
730#endif
731 if (! (mix = chip->mixer_data))
732 return -ENODEV;
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -0700733 switch(kcontrol->private_value) {
734 case TUMBLER_MUTE_HP:
735 gp = &mix->hp_mute; break;
736 case TUMBLER_MUTE_AMP:
737 gp = &mix->amp_mute; break;
738 case TUMBLER_MUTE_LINE:
739 gp = &mix->line_mute; break;
740 default:
741 gp = NULL;
742 }
743 if (gp == NULL)
744 return -EINVAL;
Benjamin Herrenschmidt4be8dc72005-05-01 08:58:43 -0700745 val = ! check_audio_gpio(gp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 if (val != ucontrol->value.integer.value[0]) {
747 write_audio_gpio(gp, ! ucontrol->value.integer.value[0]);
748 return 1;
749 }
750 return 0;
751}
752
Takashi Iwai65b29f52005-11-17 15:09:46 +0100753static int snapper_set_capture_source(struct pmac_tumbler *mix)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754{
755 if (! mix->i2c.client)
756 return -ENODEV;
Benjamin Herrenschmidtb20af5f2005-04-16 15:24:32 -0700757 if (mix->capture_source)
758 mix->acs = mix->acs |= 2;
759 else
760 mix->acs &= ~2;
761 return i2c_smbus_write_byte_data(mix->i2c.client, TAS_REG_ACS, mix->acs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762}
763
Takashi Iwai65b29f52005-11-17 15:09:46 +0100764static int snapper_info_capture_source(struct snd_kcontrol *kcontrol,
765 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766{
767 static char *texts[2] = {
768 "Line", "Mic"
769 };
770 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
771 uinfo->count = 1;
772 uinfo->value.enumerated.items = 2;
773 if (uinfo->value.enumerated.item > 1)
774 uinfo->value.enumerated.item = 1;
775 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
776 return 0;
777}
778
Takashi Iwai65b29f52005-11-17 15:09:46 +0100779static int snapper_get_capture_source(struct snd_kcontrol *kcontrol,
780 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781{
Takashi Iwai65b29f52005-11-17 15:09:46 +0100782 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
783 struct pmac_tumbler *mix = chip->mixer_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
785 snd_assert(mix, return -ENODEV);
786 ucontrol->value.integer.value[0] = mix->capture_source;
787 return 0;
788}
789
Takashi Iwai65b29f52005-11-17 15:09:46 +0100790static int snapper_put_capture_source(struct snd_kcontrol *kcontrol,
791 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792{
Takashi Iwai65b29f52005-11-17 15:09:46 +0100793 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
794 struct pmac_tumbler *mix = chip->mixer_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 int change;
796
797 snd_assert(mix, return -ENODEV);
798 change = ucontrol->value.integer.value[0] != mix->capture_source;
799 if (change) {
800 mix->capture_source = !!ucontrol->value.integer.value[0];
801 snapper_set_capture_source(mix);
802 }
803 return change;
804}
805
806#define DEFINE_SNAPPER_MIX(xname,idx,ofs) { \
807 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,\
808 .name = xname, \
809 .info = snapper_info_mix, \
810 .get = snapper_get_mix, \
811 .put = snapper_put_mix, \
812 .index = idx,\
813 .private_value = ofs, \
814}
815
816
817/*
818 */
Takashi Iwai65b29f52005-11-17 15:09:46 +0100819static struct snd_kcontrol_new tumbler_mixers[] __initdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
821 .name = "Master Playback Volume",
822 .info = tumbler_info_master_volume,
823 .get = tumbler_get_master_volume,
824 .put = tumbler_put_master_volume
825 },
826 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
827 .name = "Master Playback Switch",
828 .info = snd_pmac_boolean_stereo_info,
829 .get = tumbler_get_master_switch,
830 .put = tumbler_put_master_switch
831 },
832 DEFINE_MONO("Tone Control - Bass", bass),
833 DEFINE_MONO("Tone Control - Treble", treble),
834 DEFINE_MONO("PCM Playback Volume", pcm),
835 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
836 .name = "DRC Range",
837 .info = tumbler_info_drc_value,
838 .get = tumbler_get_drc_value,
839 .put = tumbler_put_drc_value
840 },
841};
842
Takashi Iwai65b29f52005-11-17 15:09:46 +0100843static struct snd_kcontrol_new snapper_mixers[] __initdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
845 .name = "Master Playback Volume",
846 .info = tumbler_info_master_volume,
847 .get = tumbler_get_master_volume,
848 .put = tumbler_put_master_volume
849 },
850 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
851 .name = "Master Playback Switch",
852 .info = snd_pmac_boolean_stereo_info,
853 .get = tumbler_get_master_switch,
854 .put = tumbler_put_master_switch
855 },
856 DEFINE_SNAPPER_MIX("PCM Playback Volume", 0, VOL_IDX_PCM),
857 DEFINE_SNAPPER_MIX("PCM Playback Volume", 1, VOL_IDX_PCM2),
858 DEFINE_SNAPPER_MIX("Monitor Mix Volume", 0, VOL_IDX_ADC),
859 DEFINE_SNAPPER_MONO("Tone Control - Bass", bass),
860 DEFINE_SNAPPER_MONO("Tone Control - Treble", treble),
861 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
862 .name = "DRC Range",
863 .info = tumbler_info_drc_value,
864 .get = tumbler_get_drc_value,
865 .put = tumbler_put_drc_value
866 },
867 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
868 .name = "Input Source", /* FIXME: "Capture Source" doesn't work properly */
869 .info = snapper_info_capture_source,
870 .get = snapper_get_capture_source,
871 .put = snapper_put_capture_source
872 },
873};
874
Takashi Iwai65b29f52005-11-17 15:09:46 +0100875static struct snd_kcontrol_new tumbler_hp_sw __initdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
877 .name = "Headphone Playback Switch",
878 .info = snd_pmac_boolean_mono_info,
879 .get = tumbler_get_mute_switch,
880 .put = tumbler_put_mute_switch,
881 .private_value = TUMBLER_MUTE_HP,
882};
Takashi Iwai65b29f52005-11-17 15:09:46 +0100883static struct snd_kcontrol_new tumbler_speaker_sw __initdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
885 .name = "PC Speaker Playback Switch",
886 .info = snd_pmac_boolean_mono_info,
887 .get = tumbler_get_mute_switch,
888 .put = tumbler_put_mute_switch,
889 .private_value = TUMBLER_MUTE_AMP,
890};
Takashi Iwai65b29f52005-11-17 15:09:46 +0100891static struct snd_kcontrol_new tumbler_lineout_sw __initdata = {
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -0700892 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
893 .name = "Line Out Playback Switch",
894 .info = snd_pmac_boolean_mono_info,
895 .get = tumbler_get_mute_switch,
896 .put = tumbler_put_mute_switch,
897 .private_value = TUMBLER_MUTE_LINE,
898};
Takashi Iwai65b29f52005-11-17 15:09:46 +0100899static struct snd_kcontrol_new tumbler_drc_sw __initdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
901 .name = "DRC Switch",
902 .info = snd_pmac_boolean_mono_info,
903 .get = tumbler_get_drc_switch,
904 .put = tumbler_put_drc_switch
905};
906
907
908#ifdef PMAC_SUPPORT_AUTOMUTE
909/*
910 * auto-mute stuffs
911 */
Takashi Iwai65b29f52005-11-17 15:09:46 +0100912static int tumbler_detect_headphone(struct snd_pmac *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913{
Takashi Iwai65b29f52005-11-17 15:09:46 +0100914 struct pmac_tumbler *mix = chip->mixer_data;
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -0700915 int detect = 0;
916
917 if (mix->hp_detect.addr)
918 detect |= read_audio_gpio(&mix->hp_detect);
919 return detect;
920}
921
Takashi Iwai65b29f52005-11-17 15:09:46 +0100922static int tumbler_detect_lineout(struct snd_pmac *chip)
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -0700923{
Takashi Iwai65b29f52005-11-17 15:09:46 +0100924 struct pmac_tumbler *mix = chip->mixer_data;
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -0700925 int detect = 0;
926
927 if (mix->line_detect.addr)
928 detect |= read_audio_gpio(&mix->line_detect);
929 return detect;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930}
931
Takashi Iwai65b29f52005-11-17 15:09:46 +0100932static void check_mute(struct snd_pmac *chip, struct pmac_gpio *gp, int val, int do_notify,
933 struct snd_kcontrol *sw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934{
Benjamin Herrenschmidt4be8dc72005-05-01 08:58:43 -0700935 if (check_audio_gpio(gp) != val) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 write_audio_gpio(gp, val);
937 if (do_notify)
Benjamin Herrenschmidt4be8dc72005-05-01 08:58:43 -0700938 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
939 &sw->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 }
941}
942
943static struct work_struct device_change;
944
Benjamin Herrenschmidtb20af5f2005-04-16 15:24:32 -0700945static void device_change_handler(void *self)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946{
Takashi Iwai65b29f52005-11-17 15:09:46 +0100947 struct snd_pmac *chip = self;
948 struct pmac_tumbler *mix;
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -0700949 int headphone, lineout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
951 if (!chip)
952 return;
953
954 mix = chip->mixer_data;
Benjamin Herrenschmidtb20af5f2005-04-16 15:24:32 -0700955 snd_assert(mix, return);
956
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -0700957 headphone = tumbler_detect_headphone(chip);
958 lineout = tumbler_detect_lineout(chip);
959
960 DBG("headphone: %d, lineout: %d\n", headphone, lineout);
961
962 if (headphone || lineout) {
963 /* unmute headphone/lineout & mute speaker */
964 if (headphone)
965 check_mute(chip, &mix->hp_mute, 0, mix->auto_mute_notify,
966 chip->master_sw_ctl);
967 if (lineout && mix->line_mute.addr != 0)
968 check_mute(chip, &mix->line_mute, 0, mix->auto_mute_notify,
969 chip->lineout_sw_ctl);
Benjamin Herrenschmidtb20af5f2005-04-16 15:24:32 -0700970 if (mix->anded_reset)
Nishanth Aravamudan989a0b22005-07-09 10:53:24 +0200971 msleep(10);
Benjamin Herrenschmidtb20af5f2005-04-16 15:24:32 -0700972 check_mute(chip, &mix->amp_mute, 1, mix->auto_mute_notify,
973 chip->speaker_sw_ctl);
Benjamin Herrenschmidtb20af5f2005-04-16 15:24:32 -0700974 } else {
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -0700975 /* unmute speaker, mute others */
Benjamin Herrenschmidtb20af5f2005-04-16 15:24:32 -0700976 check_mute(chip, &mix->amp_mute, 0, mix->auto_mute_notify,
977 chip->speaker_sw_ctl);
978 if (mix->anded_reset)
Nishanth Aravamudan989a0b22005-07-09 10:53:24 +0200979 msleep(10);
Benjamin Herrenschmidtb20af5f2005-04-16 15:24:32 -0700980 check_mute(chip, &mix->hp_mute, 1, mix->auto_mute_notify,
981 chip->master_sw_ctl);
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -0700982 if (mix->line_mute.addr != 0)
983 check_mute(chip, &mix->line_mute, 1, mix->auto_mute_notify,
984 chip->lineout_sw_ctl);
Benjamin Herrenschmidtb20af5f2005-04-16 15:24:32 -0700985 }
Takashi Iwaif3283852005-09-07 17:08:40 +0200986 if (mix->auto_mute_notify)
Benjamin Herrenschmidtb20af5f2005-04-16 15:24:32 -0700987 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
988 &chip->hp_detect_ctl->id);
Takashi Iwaif3283852005-09-07 17:08:40 +0200989
990#ifdef CONFIG_SND_POWERMAC_AUTO_DRC
991 mix->drc_enable = ! (headphone || lineout);
992 if (mix->auto_mute_notify)
Benjamin Herrenschmidtb20af5f2005-04-16 15:24:32 -0700993 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
994 &chip->drc_sw_ctl->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 if (chip->model == PMAC_TUMBLER)
996 tumbler_set_drc(mix);
997 else
998 snapper_set_drc(mix);
Takashi Iwaif3283852005-09-07 17:08:40 +0200999#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
1001 /* reset the master volume so the correct amplification is applied */
1002 tumbler_set_master_volume(mix);
1003}
1004
Takashi Iwai65b29f52005-11-17 15:09:46 +01001005static void tumbler_update_automute(struct snd_pmac *chip, int do_notify)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006{
1007 if (chip->auto_mute) {
Takashi Iwai65b29f52005-11-17 15:09:46 +01001008 struct pmac_tumbler *mix;
Benjamin Herrenschmidtb20af5f2005-04-16 15:24:32 -07001009 mix = chip->mixer_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 snd_assert(mix, return);
Benjamin Herrenschmidtb20af5f2005-04-16 15:24:32 -07001011 mix->auto_mute_notify = do_notify;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 schedule_work(&device_change);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 }
1014}
1015#endif /* PMAC_SUPPORT_AUTOMUTE */
1016
1017
1018/* interrupt - headphone plug changed */
1019static irqreturn_t headphone_intr(int irq, void *devid, struct pt_regs *regs)
1020{
Takashi Iwai65b29f52005-11-17 15:09:46 +01001021 struct snd_pmac *chip = devid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 if (chip->update_automute && chip->initialized) {
1023 chip->update_automute(chip, 1);
1024 return IRQ_HANDLED;
1025 }
1026 return IRQ_NONE;
1027}
1028
1029/* look for audio-gpio device */
1030static struct device_node *find_audio_device(const char *name)
1031{
1032 struct device_node *np;
1033
1034 if (! (np = find_devices("gpio")))
1035 return NULL;
1036
1037 for (np = np->child; np; np = np->sibling) {
1038 char *property = get_property(np, "audio-gpio", NULL);
1039 if (property && strcmp(property, name) == 0)
1040 return np;
1041 }
1042 return NULL;
1043}
1044
1045/* look for audio-gpio device */
1046static struct device_node *find_compatible_audio_device(const char *name)
1047{
1048 struct device_node *np;
1049
1050 if (! (np = find_devices("gpio")))
1051 return NULL;
1052
1053 for (np = np->child; np; np = np->sibling) {
1054 if (device_is_compatible(np, name))
1055 return np;
1056 }
1057 return NULL;
1058}
1059
1060/* find an audio device and get its address */
Takashi Iwai65b29f52005-11-17 15:09:46 +01001061static long tumbler_find_device(const char *device, const char *platform,
1062 struct pmac_gpio *gp, int is_compatible)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063{
1064 struct device_node *node;
Benjamin Herrenschmidtb75550e2005-04-16 15:24:31 -07001065 u32 *base, addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
1067 if (is_compatible)
1068 node = find_compatible_audio_device(device);
1069 else
1070 node = find_audio_device(device);
1071 if (! node) {
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -07001072 DBG("(W) cannot find audio device %s !\n", device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 snd_printdd("cannot find device %s\n", device);
1074 return -ENODEV;
1075 }
1076
1077 base = (u32 *)get_property(node, "AAPL,address", NULL);
1078 if (! base) {
Benjamin Herrenschmidtb75550e2005-04-16 15:24:31 -07001079 base = (u32 *)get_property(node, "reg", NULL);
1080 if (!base) {
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -07001081 DBG("(E) cannot find address for device %s !\n", device);
Benjamin Herrenschmidtb75550e2005-04-16 15:24:31 -07001082 snd_printd("cannot find address for device %s\n", device);
1083 return -ENODEV;
1084 }
Benjamin Herrenschmidtb75550e2005-04-16 15:24:31 -07001085 addr = *base;
1086 if (addr < 0x50)
1087 addr += 0x50;
1088 } else
1089 addr = *base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
Benjamin Herrenschmidtb75550e2005-04-16 15:24:31 -07001091 gp->addr = addr & 0x0000ffff;
Benjamin Herrenschmidtb75550e2005-04-16 15:24:31 -07001092 /* Try to find the active state, default to 0 ! */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 base = (u32 *)get_property(node, "audio-gpio-active-state", NULL);
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -07001094 if (base) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 gp->active_state = *base;
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -07001096 gp->active_val = (*base) ? 0x5 : 0x4;
1097 gp->inactive_val = (*base) ? 0x4 : 0x5;
1098 } else {
1099 u32 *prop = NULL;
Benjamin Herrenschmidtb75550e2005-04-16 15:24:31 -07001100 gp->active_state = 0;
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -07001101 gp->active_val = 0x4;
1102 gp->inactive_val = 0x5;
1103 /* Here are some crude hacks to extract the GPIO polarity and
1104 * open collector informations out of the do-platform script
1105 * as we don't yet have an interpreter for these things
1106 */
1107 if (platform)
1108 prop = (u32 *)get_property(node, platform, NULL);
1109 if (prop) {
1110 if (prop[3] == 0x9 && prop[4] == 0x9) {
1111 gp->active_val = 0xd;
1112 gp->inactive_val = 0xc;
1113 }
1114 if (prop[3] == 0x1 && prop[4] == 0x1) {
1115 gp->active_val = 0x5;
1116 gp->inactive_val = 0x4;
1117 }
1118 }
1119 }
1120
1121 DBG("(I) GPIO device %s found, offset: %x, active state: %d !\n",
1122 device, gp->addr, gp->active_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123
1124 return (node->n_intrs > 0) ? node->intrs[0].line : 0;
1125}
1126
1127/* reset audio */
Takashi Iwai65b29f52005-11-17 15:09:46 +01001128static void tumbler_reset_audio(struct snd_pmac *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129{
Takashi Iwai65b29f52005-11-17 15:09:46 +01001130 struct pmac_tumbler *mix = chip->mixer_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131
Benjamin Herrenschmidtb20af5f2005-04-16 15:24:32 -07001132 if (mix->anded_reset) {
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -07001133 DBG("(I) codec anded reset !\n");
Benjamin Herrenschmidtb20af5f2005-04-16 15:24:32 -07001134 write_audio_gpio(&mix->hp_mute, 0);
1135 write_audio_gpio(&mix->amp_mute, 0);
Nishanth Aravamudan989a0b22005-07-09 10:53:24 +02001136 msleep(200);
Benjamin Herrenschmidtb20af5f2005-04-16 15:24:32 -07001137 write_audio_gpio(&mix->hp_mute, 1);
1138 write_audio_gpio(&mix->amp_mute, 1);
Nishanth Aravamudan989a0b22005-07-09 10:53:24 +02001139 msleep(100);
Benjamin Herrenschmidtb20af5f2005-04-16 15:24:32 -07001140 write_audio_gpio(&mix->hp_mute, 0);
1141 write_audio_gpio(&mix->amp_mute, 0);
Nishanth Aravamudan989a0b22005-07-09 10:53:24 +02001142 msleep(100);
Benjamin Herrenschmidtb20af5f2005-04-16 15:24:32 -07001143 } else {
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -07001144 DBG("(I) codec normal reset !\n");
1145
Benjamin Herrenschmidtb20af5f2005-04-16 15:24:32 -07001146 write_audio_gpio(&mix->audio_reset, 0);
Nishanth Aravamudan989a0b22005-07-09 10:53:24 +02001147 msleep(200);
Benjamin Herrenschmidtb20af5f2005-04-16 15:24:32 -07001148 write_audio_gpio(&mix->audio_reset, 1);
Nishanth Aravamudan989a0b22005-07-09 10:53:24 +02001149 msleep(100);
Benjamin Herrenschmidtb20af5f2005-04-16 15:24:32 -07001150 write_audio_gpio(&mix->audio_reset, 0);
Nishanth Aravamudan989a0b22005-07-09 10:53:24 +02001151 msleep(100);
Benjamin Herrenschmidtb20af5f2005-04-16 15:24:32 -07001152 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153}
1154
Benjamin Herrenschmidt8c870932005-06-27 14:36:34 -07001155#ifdef CONFIG_PM
Benjamin Herrenschmidtb20af5f2005-04-16 15:24:32 -07001156/* suspend mixer */
Takashi Iwai65b29f52005-11-17 15:09:46 +01001157static void tumbler_suspend(struct snd_pmac *chip)
Benjamin Herrenschmidtb20af5f2005-04-16 15:24:32 -07001158{
Takashi Iwai65b29f52005-11-17 15:09:46 +01001159 struct pmac_tumbler *mix = chip->mixer_data;
Benjamin Herrenschmidtb20af5f2005-04-16 15:24:32 -07001160
1161 if (mix->headphone_irq >= 0)
1162 disable_irq(mix->headphone_irq);
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -07001163 if (mix->lineout_irq >= 0)
1164 disable_irq(mix->lineout_irq);
Benjamin Herrenschmidtb20af5f2005-04-16 15:24:32 -07001165 mix->save_master_switch[0] = mix->master_switch[0];
1166 mix->save_master_switch[1] = mix->master_switch[1];
Colin Leroy085e6fc2005-05-01 08:58:43 -07001167 mix->save_master_vol[0] = mix->master_vol[0];
1168 mix->save_master_vol[1] = mix->master_vol[1];
Benjamin Herrenschmidtb20af5f2005-04-16 15:24:32 -07001169 mix->master_switch[0] = mix->master_switch[1] = 0;
1170 tumbler_set_master_volume(mix);
1171 if (!mix->anded_reset) {
1172 write_audio_gpio(&mix->amp_mute, 1);
1173 write_audio_gpio(&mix->hp_mute, 1);
1174 }
1175 if (chip->model == PMAC_SNAPPER) {
1176 mix->acs |= 1;
1177 i2c_smbus_write_byte_data(mix->i2c.client, TAS_REG_ACS, mix->acs);
1178 }
1179 if (mix->anded_reset) {
1180 write_audio_gpio(&mix->amp_mute, 1);
1181 write_audio_gpio(&mix->hp_mute, 1);
1182 } else
1183 write_audio_gpio(&mix->audio_reset, 1);
1184}
1185
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186/* resume mixer */
Takashi Iwai65b29f52005-11-17 15:09:46 +01001187static void tumbler_resume(struct snd_pmac *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188{
Takashi Iwai65b29f52005-11-17 15:09:46 +01001189 struct pmac_tumbler *mix = chip->mixer_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
1191 snd_assert(mix, return);
1192
Benjamin Herrenschmidtb20af5f2005-04-16 15:24:32 -07001193 mix->acs &= ~1;
1194 mix->master_switch[0] = mix->save_master_switch[0];
1195 mix->master_switch[1] = mix->save_master_switch[1];
Colin Leroy085e6fc2005-05-01 08:58:43 -07001196 mix->master_vol[0] = mix->save_master_vol[0];
1197 mix->master_vol[1] = mix->save_master_vol[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 tumbler_reset_audio(chip);
1199 if (mix->i2c.client && mix->i2c.init_client) {
1200 if (mix->i2c.init_client(&mix->i2c) < 0)
1201 printk(KERN_ERR "tumbler_init_client error\n");
1202 } else
1203 printk(KERN_ERR "tumbler: i2c is not initialized\n");
1204 if (chip->model == PMAC_TUMBLER) {
1205 tumbler_set_mono_volume(mix, &tumbler_pcm_vol_info);
1206 tumbler_set_mono_volume(mix, &tumbler_bass_vol_info);
1207 tumbler_set_mono_volume(mix, &tumbler_treble_vol_info);
1208 tumbler_set_drc(mix);
1209 } else {
1210 snapper_set_mix_vol(mix, VOL_IDX_PCM);
1211 snapper_set_mix_vol(mix, VOL_IDX_PCM2);
1212 snapper_set_mix_vol(mix, VOL_IDX_ADC);
1213 tumbler_set_mono_volume(mix, &snapper_bass_vol_info);
1214 tumbler_set_mono_volume(mix, &snapper_treble_vol_info);
1215 snapper_set_drc(mix);
1216 snapper_set_capture_source(mix);
1217 }
1218 tumbler_set_master_volume(mix);
1219 if (chip->update_automute)
1220 chip->update_automute(chip, 0);
Guido Guentherc0ce5c52005-11-15 17:28:05 +11001221 if (mix->headphone_irq >= 0) {
1222 unsigned char val;
1223
Benjamin Herrenschmidtb20af5f2005-04-16 15:24:32 -07001224 enable_irq(mix->headphone_irq);
Guido Guentherc0ce5c52005-11-15 17:28:05 +11001225 /* activate headphone status interrupts */
1226 val = do_gpio_read(&mix->hp_detect);
1227 do_gpio_write(&mix->hp_detect, val | 0x80);
1228 }
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -07001229 if (mix->lineout_irq >= 0)
1230 enable_irq(mix->lineout_irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231}
1232#endif
1233
1234/* initialize tumbler */
Takashi Iwai65b29f52005-11-17 15:09:46 +01001235static int __init tumbler_init(struct snd_pmac *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236{
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -07001237 int irq;
Takashi Iwai65b29f52005-11-17 15:09:46 +01001238 struct pmac_tumbler *mix = chip->mixer_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 snd_assert(mix, return -EINVAL);
1240
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -07001241 if (tumbler_find_device("audio-hw-reset",
1242 "platform-do-hw-reset",
1243 &mix->audio_reset, 0) < 0)
1244 tumbler_find_device("hw-reset",
1245 "platform-do-hw-reset",
1246 &mix->audio_reset, 1);
1247 if (tumbler_find_device("amp-mute",
1248 "platform-do-amp-mute",
1249 &mix->amp_mute, 0) < 0)
1250 tumbler_find_device("amp-mute",
1251 "platform-do-amp-mute",
1252 &mix->amp_mute, 1);
1253 if (tumbler_find_device("headphone-mute",
1254 "platform-do-headphone-mute",
1255 &mix->hp_mute, 0) < 0)
1256 tumbler_find_device("headphone-mute",
1257 "platform-do-headphone-mute",
1258 &mix->hp_mute, 1);
1259 if (tumbler_find_device("line-output-mute",
1260 "platform-do-lineout-mute",
1261 &mix->line_mute, 0) < 0)
1262 tumbler_find_device("line-output-mute",
1263 "platform-do-lineout-mute",
1264 &mix->line_mute, 1);
1265 irq = tumbler_find_device("headphone-detect",
1266 NULL, &mix->hp_detect, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 if (irq < 0)
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -07001268 irq = tumbler_find_device("headphone-detect",
1269 NULL, &mix->hp_detect, 1);
Benjamin Herrenschmidtb75550e2005-04-16 15:24:31 -07001270 if (irq < 0)
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -07001271 irq = tumbler_find_device("keywest-gpio15",
1272 NULL, &mix->hp_detect, 1);
1273 mix->headphone_irq = irq;
1274 irq = tumbler_find_device("line-output-detect",
1275 NULL, &mix->line_detect, 0);
1276 if (irq < 0)
1277 irq = tumbler_find_device("line-output-detect",
1278 NULL, &mix->line_detect, 1);
1279 mix->lineout_irq = irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280
1281 tumbler_reset_audio(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282
1283 return 0;
1284}
1285
Takashi Iwai65b29f52005-11-17 15:09:46 +01001286static void tumbler_cleanup(struct snd_pmac *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287{
Takashi Iwai65b29f52005-11-17 15:09:46 +01001288 struct pmac_tumbler *mix = chip->mixer_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 if (! mix)
1290 return;
1291
1292 if (mix->headphone_irq >= 0)
1293 free_irq(mix->headphone_irq, chip);
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -07001294 if (mix->lineout_irq >= 0)
1295 free_irq(mix->lineout_irq, chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 tumbler_gpio_free(&mix->audio_reset);
1297 tumbler_gpio_free(&mix->amp_mute);
1298 tumbler_gpio_free(&mix->hp_mute);
1299 tumbler_gpio_free(&mix->hp_detect);
1300 snd_pmac_keywest_cleanup(&mix->i2c);
1301 kfree(mix);
1302 chip->mixer_data = NULL;
1303}
1304
1305/* exported */
Takashi Iwai65b29f52005-11-17 15:09:46 +01001306int __init snd_pmac_tumbler_init(struct snd_pmac *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307{
1308 int i, err;
Takashi Iwai65b29f52005-11-17 15:09:46 +01001309 struct pmac_tumbler *mix;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 u32 *paddr;
Benjamin Herrenschmidtb20af5f2005-04-16 15:24:32 -07001311 struct device_node *tas_node, *np;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 char *chipname;
1313
1314#ifdef CONFIG_KMOD
1315 if (current->fs->root)
Benjamin Herrenschmidt4d6c5882006-04-21 15:04:22 +10001316 request_module("i2c-powermac");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317#endif /* CONFIG_KMOD */
1318
1319 mix = kmalloc(sizeof(*mix), GFP_KERNEL);
1320 if (! mix)
1321 return -ENOMEM;
1322 memset(mix, 0, sizeof(*mix));
1323 mix->headphone_irq = -1;
1324
1325 chip->mixer_data = mix;
1326 chip->mixer_free = tumbler_cleanup;
Benjamin Herrenschmidtb20af5f2005-04-16 15:24:32 -07001327 mix->anded_reset = 0;
1328 mix->reset_on_sleep = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329
Benjamin Herrenschmidtb20af5f2005-04-16 15:24:32 -07001330 for (np = chip->node->child; np; np = np->sibling) {
1331 if (!strcmp(np->name, "sound")) {
1332 if (get_property(np, "has-anded-reset", NULL))
1333 mix->anded_reset = 1;
1334 if (get_property(np, "layout-id", NULL))
1335 mix->reset_on_sleep = 0;
1336 break;
1337 }
1338 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 if ((err = tumbler_init(chip)) < 0)
1340 return err;
1341
1342 /* set up TAS */
1343 tas_node = find_devices("deq");
1344 if (tas_node == NULL)
Benjamin Herrenschmidtb75550e2005-04-16 15:24:31 -07001345 tas_node = find_devices("codec");
1346 if (tas_node == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 return -ENODEV;
1348
1349 paddr = (u32 *)get_property(tas_node, "i2c-address", NULL);
Benjamin Herrenschmidtb75550e2005-04-16 15:24:31 -07001350 if (paddr == NULL)
1351 paddr = (u32 *)get_property(tas_node, "reg", NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 if (paddr)
1353 mix->i2c.addr = (*paddr) >> 1;
1354 else
1355 mix->i2c.addr = TAS_I2C_ADDR;
1356
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -07001357 DBG("(I) TAS i2c address is: %x\n", mix->i2c.addr);
1358
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 if (chip->model == PMAC_TUMBLER) {
1360 mix->i2c.init_client = tumbler_init_client;
1361 mix->i2c.name = "TAS3001c";
1362 chipname = "Tumbler";
1363 } else {
1364 mix->i2c.init_client = snapper_init_client;
1365 mix->i2c.name = "TAS3004";
1366 chipname = "Snapper";
1367 }
1368
1369 if ((err = snd_pmac_keywest_init(&mix->i2c)) < 0)
1370 return err;
1371
1372 /*
1373 * build mixers
1374 */
1375 sprintf(chip->card->mixername, "PowerMac %s", chipname);
1376
1377 if (chip->model == PMAC_TUMBLER) {
1378 for (i = 0; i < ARRAY_SIZE(tumbler_mixers); i++) {
1379 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&tumbler_mixers[i], chip))) < 0)
1380 return err;
1381 }
1382 } else {
1383 for (i = 0; i < ARRAY_SIZE(snapper_mixers); i++) {
1384 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snapper_mixers[i], chip))) < 0)
1385 return err;
1386 }
1387 }
1388 chip->master_sw_ctl = snd_ctl_new1(&tumbler_hp_sw, chip);
1389 if ((err = snd_ctl_add(chip->card, chip->master_sw_ctl)) < 0)
1390 return err;
1391 chip->speaker_sw_ctl = snd_ctl_new1(&tumbler_speaker_sw, chip);
1392 if ((err = snd_ctl_add(chip->card, chip->speaker_sw_ctl)) < 0)
1393 return err;
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -07001394 if (mix->line_mute.addr != 0) {
1395 chip->lineout_sw_ctl = snd_ctl_new1(&tumbler_lineout_sw, chip);
1396 if ((err = snd_ctl_add(chip->card, chip->lineout_sw_ctl)) < 0)
1397 return err;
1398 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 chip->drc_sw_ctl = snd_ctl_new1(&tumbler_drc_sw, chip);
1400 if ((err = snd_ctl_add(chip->card, chip->drc_sw_ctl)) < 0)
1401 return err;
1402
Takashi Iwaif3283852005-09-07 17:08:40 +02001403 /* set initial DRC range to 60% */
1404 if (chip->model == PMAC_TUMBLER)
1405 mix->drc_range = (TAS3001_DRC_MAX * 6) / 10;
1406 else
1407 mix->drc_range = (TAS3004_DRC_MAX * 6) / 10;
1408 mix->drc_enable = 1; /* will be changed later if AUTO_DRC is set */
1409 if (chip->model == PMAC_TUMBLER)
1410 tumbler_set_drc(mix);
1411 else
1412 snapper_set_drc(mix);
1413
Benjamin Herrenschmidt8c870932005-06-27 14:36:34 -07001414#ifdef CONFIG_PM
Benjamin Herrenschmidtb20af5f2005-04-16 15:24:32 -07001415 chip->suspend = tumbler_suspend;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 chip->resume = tumbler_resume;
1417#endif
1418
1419 INIT_WORK(&device_change, device_change_handler, (void *)chip);
1420
1421#ifdef PMAC_SUPPORT_AUTOMUTE
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -07001422 if ((mix->headphone_irq >=0 || mix->lineout_irq >= 0)
1423 && (err = snd_pmac_add_automute(chip)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 return err;
1425 chip->detect_headphone = tumbler_detect_headphone;
1426 chip->update_automute = tumbler_update_automute;
1427 tumbler_update_automute(chip, 0); /* update the status only */
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -07001428
1429 /* activate headphone status interrupts */
1430 if (mix->headphone_irq >= 0) {
1431 unsigned char val;
1432 if ((err = request_irq(mix->headphone_irq, headphone_intr, 0,
1433 "Sound Headphone Detection", chip)) < 0)
1434 return 0;
1435 /* activate headphone status interrupts */
1436 val = do_gpio_read(&mix->hp_detect);
1437 do_gpio_write(&mix->hp_detect, val | 0x80);
1438 }
1439 if (mix->lineout_irq >= 0) {
1440 unsigned char val;
1441 if ((err = request_irq(mix->lineout_irq, headphone_intr, 0,
1442 "Sound Lineout Detection", chip)) < 0)
1443 return 0;
1444 /* activate headphone status interrupts */
1445 val = do_gpio_read(&mix->line_detect);
1446 do_gpio_write(&mix->line_detect, val | 0x80);
1447 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448#endif
1449
1450 return 0;
1451}