blob: d74bfabe5300c9e20893bf3edbb4a48a75525ac1 [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
85typedef struct 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090} pmac_gpio_t;
91
92typedef struct pmac_tumbler_t {
93 pmac_keywest_t i2c;
94 pmac_gpio_t audio_reset;
95 pmac_gpio_t amp_mute;
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -070096 pmac_gpio_t line_mute;
97 pmac_gpio_t line_detect;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 pmac_gpio_t hp_mute;
99 pmac_gpio_t hp_detect;
100 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115} pmac_tumbler_t;
116
117
118/*
119 */
120
121static int send_init_client(pmac_keywest_t *i2c, unsigned int *regs)
122{
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
141static int tumbler_init_client(pmac_keywest_t *i2c)
142{
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
152static int snapper_init_client(pmac_keywest_t *i2c)
153{
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
176static void write_audio_gpio(pmac_gpio_t *gp, int active)
177{
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
Benjamin Herrenschmidt4be8dc72005-05-01 08:58:43 -0700185static int check_audio_gpio(pmac_gpio_t *gp)
186{
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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197static int read_audio_gpio(pmac_gpio_t *gp)
198{
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 */
209static int tumbler_set_master_volume(pmac_tumbler_t *mix)
210{
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
242 if (i2c_smbus_write_block_data(mix->i2c.client, TAS_REG_VOL,
243 6, block) < 0) {
244 snd_printk("failed to set volume \n");
245 return -EINVAL;
246 }
247 return 0;
248}
249
250
251/* output volume */
252static int tumbler_info_master_volume(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
253{
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
261static int tumbler_get_master_volume(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
262{
263 pmac_t *chip = snd_kcontrol_chip(kcontrol);
264 pmac_tumbler_t *mix = chip->mixer_data;
265 snd_assert(mix, return -ENODEV);
266 ucontrol->value.integer.value[0] = mix->master_vol[0];
267 ucontrol->value.integer.value[1] = mix->master_vol[1];
268 return 0;
269}
270
271static int tumbler_put_master_volume(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
272{
273 pmac_t *chip = snd_kcontrol_chip(kcontrol);
274 pmac_tumbler_t *mix = chip->mixer_data;
275 int change;
276
277 snd_assert(mix, return -ENODEV);
278 change = mix->master_vol[0] != ucontrol->value.integer.value[0] ||
279 mix->master_vol[1] != ucontrol->value.integer.value[1];
280 if (change) {
281 mix->master_vol[0] = ucontrol->value.integer.value[0];
282 mix->master_vol[1] = ucontrol->value.integer.value[1];
283 tumbler_set_master_volume(mix);
284 }
285 return change;
286}
287
288/* output switch */
289static int tumbler_get_master_switch(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
290{
291 pmac_t *chip = snd_kcontrol_chip(kcontrol);
292 pmac_tumbler_t *mix = chip->mixer_data;
293 snd_assert(mix, return -ENODEV);
294 ucontrol->value.integer.value[0] = mix->master_switch[0];
295 ucontrol->value.integer.value[1] = mix->master_switch[1];
296 return 0;
297}
298
299static int tumbler_put_master_switch(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
300{
301 pmac_t *chip = snd_kcontrol_chip(kcontrol);
302 pmac_tumbler_t *mix = chip->mixer_data;
303 int change;
304
305 snd_assert(mix, return -ENODEV);
306 change = mix->master_switch[0] != ucontrol->value.integer.value[0] ||
307 mix->master_switch[1] != ucontrol->value.integer.value[1];
308 if (change) {
309 mix->master_switch[0] = !!ucontrol->value.integer.value[0];
310 mix->master_switch[1] = !!ucontrol->value.integer.value[1];
311 tumbler_set_master_volume(mix);
312 }
313 return change;
314}
315
316
317/*
318 * TAS3001c dynamic range compression
319 */
320
321#define TAS3001_DRC_MAX 0x5f
322
323static int tumbler_set_drc(pmac_tumbler_t *mix)
324{
325 unsigned char val[2];
326
327 if (! mix->i2c.client)
328 return -ENODEV;
329
330 if (mix->drc_enable) {
331 val[0] = 0xc1; /* enable, 3:1 compression */
332 if (mix->drc_range > TAS3001_DRC_MAX)
333 val[1] = 0xf0;
334 else if (mix->drc_range < 0)
335 val[1] = 0x91;
336 else
337 val[1] = mix->drc_range + 0x91;
338 } else {
339 val[0] = 0;
340 val[1] = 0;
341 }
342
343 if (i2c_smbus_write_block_data(mix->i2c.client, TAS_REG_DRC,
344 2, val) < 0) {
345 snd_printk("failed to set DRC\n");
346 return -EINVAL;
347 }
348 return 0;
349}
350
351/*
352 * TAS3004
353 */
354
355#define TAS3004_DRC_MAX 0xef
356
357static int snapper_set_drc(pmac_tumbler_t *mix)
358{
359 unsigned char val[6];
360
361 if (! mix->i2c.client)
362 return -ENODEV;
363
364 if (mix->drc_enable)
365 val[0] = 0x50; /* 3:1 above threshold */
366 else
367 val[0] = 0x51; /* disabled */
368 val[1] = 0x02; /* 1:1 below threshold */
369 if (mix->drc_range > 0xef)
370 val[2] = 0xef;
371 else if (mix->drc_range < 0)
372 val[2] = 0x00;
373 else
374 val[2] = mix->drc_range;
375 val[3] = 0xb0;
376 val[4] = 0x60;
377 val[5] = 0xa0;
378
379 if (i2c_smbus_write_block_data(mix->i2c.client, TAS_REG_DRC,
380 6, val) < 0) {
381 snd_printk("failed to set DRC\n");
382 return -EINVAL;
383 }
384 return 0;
385}
386
387static int tumbler_info_drc_value(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
388{
389 pmac_t *chip = snd_kcontrol_chip(kcontrol);
390 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
391 uinfo->count = 1;
392 uinfo->value.integer.min = 0;
393 uinfo->value.integer.max =
394 chip->model == PMAC_TUMBLER ? TAS3001_DRC_MAX : TAS3004_DRC_MAX;
395 return 0;
396}
397
398static int tumbler_get_drc_value(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
399{
400 pmac_t *chip = snd_kcontrol_chip(kcontrol);
401 pmac_tumbler_t *mix;
402 if (! (mix = chip->mixer_data))
403 return -ENODEV;
404 ucontrol->value.integer.value[0] = mix->drc_range;
405 return 0;
406}
407
408static int tumbler_put_drc_value(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
409{
410 pmac_t *chip = snd_kcontrol_chip(kcontrol);
411 pmac_tumbler_t *mix;
412 int change;
413
414 if (! (mix = chip->mixer_data))
415 return -ENODEV;
416 change = mix->drc_range != ucontrol->value.integer.value[0];
417 if (change) {
418 mix->drc_range = ucontrol->value.integer.value[0];
419 if (chip->model == PMAC_TUMBLER)
420 tumbler_set_drc(mix);
421 else
422 snapper_set_drc(mix);
423 }
424 return change;
425}
426
427static int tumbler_get_drc_switch(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
428{
429 pmac_t *chip = snd_kcontrol_chip(kcontrol);
430 pmac_tumbler_t *mix;
431 if (! (mix = chip->mixer_data))
432 return -ENODEV;
433 ucontrol->value.integer.value[0] = mix->drc_enable;
434 return 0;
435}
436
437static int tumbler_put_drc_switch(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
438{
439 pmac_t *chip = snd_kcontrol_chip(kcontrol);
440 pmac_tumbler_t *mix;
441 int change;
442
443 if (! (mix = chip->mixer_data))
444 return -ENODEV;
445 change = mix->drc_enable != ucontrol->value.integer.value[0];
446 if (change) {
447 mix->drc_enable = !!ucontrol->value.integer.value[0];
448 if (chip->model == PMAC_TUMBLER)
449 tumbler_set_drc(mix);
450 else
451 snapper_set_drc(mix);
452 }
453 return change;
454}
455
456
457/*
458 * mono volumes
459 */
460
461struct tumbler_mono_vol {
462 int index;
463 int reg;
464 int bytes;
465 unsigned int max;
466 unsigned int *table;
467};
468
469static int tumbler_set_mono_volume(pmac_tumbler_t *mix, struct tumbler_mono_vol *info)
470{
471 unsigned char block[4];
472 unsigned int vol;
473 int i;
474
475 if (! mix->i2c.client)
476 return -ENODEV;
477
478 vol = mix->mono_vol[info->index];
479 if (vol >= info->max)
480 vol = info->max - 1;
481 vol = info->table[vol];
482 for (i = 0; i < info->bytes; i++)
483 block[i] = (vol >> ((info->bytes - i - 1) * 8)) & 0xff;
484 if (i2c_smbus_write_block_data(mix->i2c.client, info->reg,
485 info->bytes, block) < 0) {
486 snd_printk("failed to set mono volume %d\n", info->index);
487 return -EINVAL;
488 }
489 return 0;
490}
491
492static int tumbler_info_mono(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
493{
494 struct tumbler_mono_vol *info = (struct tumbler_mono_vol *)kcontrol->private_value;
495
496 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
497 uinfo->count = 1;
498 uinfo->value.integer.min = 0;
499 uinfo->value.integer.max = info->max - 1;
500 return 0;
501}
502
503static int tumbler_get_mono(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
504{
505 struct tumbler_mono_vol *info = (struct tumbler_mono_vol *)kcontrol->private_value;
506 pmac_t *chip = snd_kcontrol_chip(kcontrol);
507 pmac_tumbler_t *mix;
508 if (! (mix = chip->mixer_data))
509 return -ENODEV;
510 ucontrol->value.integer.value[0] = mix->mono_vol[info->index];
511 return 0;
512}
513
514static int tumbler_put_mono(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
515{
516 struct tumbler_mono_vol *info = (struct tumbler_mono_vol *)kcontrol->private_value;
517 pmac_t *chip = snd_kcontrol_chip(kcontrol);
518 pmac_tumbler_t *mix;
519 int change;
520
521 if (! (mix = chip->mixer_data))
522 return -ENODEV;
523 change = mix->mono_vol[info->index] != ucontrol->value.integer.value[0];
524 if (change) {
525 mix->mono_vol[info->index] = ucontrol->value.integer.value[0];
526 tumbler_set_mono_volume(mix, info);
527 }
528 return change;
529}
530
531/* TAS3001c mono volumes */
532static struct tumbler_mono_vol tumbler_pcm_vol_info = {
533 .index = VOL_IDX_PCM_MONO,
534 .reg = TAS_REG_PCM,
535 .bytes = 3,
536 .max = ARRAY_SIZE(mixer_volume_table),
537 .table = mixer_volume_table,
538};
539
540static struct tumbler_mono_vol tumbler_bass_vol_info = {
541 .index = VOL_IDX_BASS,
542 .reg = TAS_REG_BASS,
543 .bytes = 1,
544 .max = ARRAY_SIZE(bass_volume_table),
545 .table = bass_volume_table,
546};
547
548static struct tumbler_mono_vol tumbler_treble_vol_info = {
549 .index = VOL_IDX_TREBLE,
550 .reg = TAS_REG_TREBLE,
551 .bytes = 1,
552 .max = ARRAY_SIZE(treble_volume_table),
553 .table = treble_volume_table,
554};
555
556/* TAS3004 mono volumes */
557static struct tumbler_mono_vol snapper_bass_vol_info = {
558 .index = VOL_IDX_BASS,
559 .reg = TAS_REG_BASS,
560 .bytes = 1,
561 .max = ARRAY_SIZE(snapper_bass_volume_table),
562 .table = snapper_bass_volume_table,
563};
564
565static struct tumbler_mono_vol snapper_treble_vol_info = {
566 .index = VOL_IDX_TREBLE,
567 .reg = TAS_REG_TREBLE,
568 .bytes = 1,
569 .max = ARRAY_SIZE(snapper_treble_volume_table),
570 .table = snapper_treble_volume_table,
571};
572
573
574#define DEFINE_MONO(xname,type) { \
575 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,\
576 .name = xname, \
577 .info = tumbler_info_mono, \
578 .get = tumbler_get_mono, \
579 .put = tumbler_put_mono, \
580 .private_value = (unsigned long)(&tumbler_##type##_vol_info), \
581}
582
583#define DEFINE_SNAPPER_MONO(xname,type) { \
584 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,\
585 .name = xname, \
586 .info = tumbler_info_mono, \
587 .get = tumbler_get_mono, \
588 .put = tumbler_put_mono, \
589 .private_value = (unsigned long)(&snapper_##type##_vol_info), \
590}
591
592
593/*
594 * snapper mixer volumes
595 */
596
597static int snapper_set_mix_vol1(pmac_tumbler_t *mix, int idx, int ch, int reg)
598{
599 int i, j, vol;
600 unsigned char block[9];
601
602 vol = mix->mix_vol[idx][ch];
603 if (vol >= ARRAY_SIZE(mixer_volume_table)) {
604 vol = ARRAY_SIZE(mixer_volume_table) - 1;
605 mix->mix_vol[idx][ch] = vol;
606 }
607
608 for (i = 0; i < 3; i++) {
609 vol = mix->mix_vol[i][ch];
610 vol = mixer_volume_table[vol];
611 for (j = 0; j < 3; j++)
612 block[i * 3 + j] = (vol >> ((2 - j) * 8)) & 0xff;
613 }
614 if (i2c_smbus_write_block_data(mix->i2c.client, reg, 9, block) < 0) {
615 snd_printk("failed to set mono volume %d\n", reg);
616 return -EINVAL;
617 }
618 return 0;
619}
620
621static int snapper_set_mix_vol(pmac_tumbler_t *mix, int idx)
622{
623 if (! mix->i2c.client)
624 return -ENODEV;
625 if (snapper_set_mix_vol1(mix, idx, 0, TAS_REG_LMIX) < 0 ||
626 snapper_set_mix_vol1(mix, idx, 1, TAS_REG_RMIX) < 0)
627 return -EINVAL;
628 return 0;
629}
630
631static int snapper_info_mix(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
632{
633 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
634 uinfo->count = 2;
635 uinfo->value.integer.min = 0;
636 uinfo->value.integer.max = ARRAY_SIZE(mixer_volume_table) - 1;
637 return 0;
638}
639
640static int snapper_get_mix(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
641{
642 int idx = (int)kcontrol->private_value;
643 pmac_t *chip = snd_kcontrol_chip(kcontrol);
644 pmac_tumbler_t *mix;
645 if (! (mix = chip->mixer_data))
646 return -ENODEV;
647 ucontrol->value.integer.value[0] = mix->mix_vol[idx][0];
648 ucontrol->value.integer.value[1] = mix->mix_vol[idx][1];
649 return 0;
650}
651
652static int snapper_put_mix(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
653{
654 int idx = (int)kcontrol->private_value;
655 pmac_t *chip = snd_kcontrol_chip(kcontrol);
656 pmac_tumbler_t *mix;
657 int change;
658
659 if (! (mix = chip->mixer_data))
660 return -ENODEV;
661 change = mix->mix_vol[idx][0] != ucontrol->value.integer.value[0] ||
662 mix->mix_vol[idx][1] != ucontrol->value.integer.value[1];
663 if (change) {
664 mix->mix_vol[idx][0] = ucontrol->value.integer.value[0];
665 mix->mix_vol[idx][1] = ucontrol->value.integer.value[1];
666 snapper_set_mix_vol(mix, idx);
667 }
668 return change;
669}
670
671
672/*
Benjamin Herrenschmidtb20af5f2005-04-16 15:24:32 -0700673 * mute switches. FIXME: Turn that into software mute when both outputs are muted
674 * to avoid codec reset on ibook M7
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 */
676
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -0700677enum { TUMBLER_MUTE_HP, TUMBLER_MUTE_AMP, TUMBLER_MUTE_LINE };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
679static int tumbler_get_mute_switch(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
680{
681 pmac_t *chip = snd_kcontrol_chip(kcontrol);
682 pmac_tumbler_t *mix;
683 pmac_gpio_t *gp;
684 if (! (mix = chip->mixer_data))
685 return -ENODEV;
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -0700686 switch(kcontrol->private_value) {
687 case TUMBLER_MUTE_HP:
688 gp = &mix->hp_mute; break;
689 case TUMBLER_MUTE_AMP:
690 gp = &mix->amp_mute; break;
691 case TUMBLER_MUTE_LINE:
692 gp = &mix->line_mute; break;
693 default:
694 gp = NULL;
695 }
696 if (gp == NULL)
697 return -EINVAL;
Benjamin Herrenschmidt4be8dc72005-05-01 08:58:43 -0700698 ucontrol->value.integer.value[0] = !check_audio_gpio(gp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 return 0;
700}
701
702static int tumbler_put_mute_switch(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
703{
704 pmac_t *chip = snd_kcontrol_chip(kcontrol);
705 pmac_tumbler_t *mix;
706 pmac_gpio_t *gp;
707 int val;
708#ifdef PMAC_SUPPORT_AUTOMUTE
709 if (chip->update_automute && chip->auto_mute)
710 return 0; /* don't touch in the auto-mute mode */
711#endif
712 if (! (mix = chip->mixer_data))
713 return -ENODEV;
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -0700714 switch(kcontrol->private_value) {
715 case TUMBLER_MUTE_HP:
716 gp = &mix->hp_mute; break;
717 case TUMBLER_MUTE_AMP:
718 gp = &mix->amp_mute; break;
719 case TUMBLER_MUTE_LINE:
720 gp = &mix->line_mute; break;
721 default:
722 gp = NULL;
723 }
724 if (gp == NULL)
725 return -EINVAL;
Benjamin Herrenschmidt4be8dc72005-05-01 08:58:43 -0700726 val = ! check_audio_gpio(gp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 if (val != ucontrol->value.integer.value[0]) {
728 write_audio_gpio(gp, ! ucontrol->value.integer.value[0]);
729 return 1;
730 }
731 return 0;
732}
733
734static int snapper_set_capture_source(pmac_tumbler_t *mix)
735{
736 if (! mix->i2c.client)
737 return -ENODEV;
Benjamin Herrenschmidtb20af5f2005-04-16 15:24:32 -0700738 if (mix->capture_source)
739 mix->acs = mix->acs |= 2;
740 else
741 mix->acs &= ~2;
742 return i2c_smbus_write_byte_data(mix->i2c.client, TAS_REG_ACS, mix->acs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743}
744
745static int snapper_info_capture_source(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
746{
747 static char *texts[2] = {
748 "Line", "Mic"
749 };
750 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
751 uinfo->count = 1;
752 uinfo->value.enumerated.items = 2;
753 if (uinfo->value.enumerated.item > 1)
754 uinfo->value.enumerated.item = 1;
755 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
756 return 0;
757}
758
759static int snapper_get_capture_source(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
760{
761 pmac_t *chip = snd_kcontrol_chip(kcontrol);
762 pmac_tumbler_t *mix = chip->mixer_data;
763
764 snd_assert(mix, return -ENODEV);
765 ucontrol->value.integer.value[0] = mix->capture_source;
766 return 0;
767}
768
769static int snapper_put_capture_source(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
770{
771 pmac_t *chip = snd_kcontrol_chip(kcontrol);
772 pmac_tumbler_t *mix = chip->mixer_data;
773 int change;
774
775 snd_assert(mix, return -ENODEV);
776 change = ucontrol->value.integer.value[0] != mix->capture_source;
777 if (change) {
778 mix->capture_source = !!ucontrol->value.integer.value[0];
779 snapper_set_capture_source(mix);
780 }
781 return change;
782}
783
784#define DEFINE_SNAPPER_MIX(xname,idx,ofs) { \
785 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,\
786 .name = xname, \
787 .info = snapper_info_mix, \
788 .get = snapper_get_mix, \
789 .put = snapper_put_mix, \
790 .index = idx,\
791 .private_value = ofs, \
792}
793
794
795/*
796 */
797static snd_kcontrol_new_t tumbler_mixers[] __initdata = {
798 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
799 .name = "Master Playback Volume",
800 .info = tumbler_info_master_volume,
801 .get = tumbler_get_master_volume,
802 .put = tumbler_put_master_volume
803 },
804 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
805 .name = "Master Playback Switch",
806 .info = snd_pmac_boolean_stereo_info,
807 .get = tumbler_get_master_switch,
808 .put = tumbler_put_master_switch
809 },
810 DEFINE_MONO("Tone Control - Bass", bass),
811 DEFINE_MONO("Tone Control - Treble", treble),
812 DEFINE_MONO("PCM Playback Volume", pcm),
813 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
814 .name = "DRC Range",
815 .info = tumbler_info_drc_value,
816 .get = tumbler_get_drc_value,
817 .put = tumbler_put_drc_value
818 },
819};
820
821static snd_kcontrol_new_t snapper_mixers[] __initdata = {
822 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
823 .name = "Master Playback Volume",
824 .info = tumbler_info_master_volume,
825 .get = tumbler_get_master_volume,
826 .put = tumbler_put_master_volume
827 },
828 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
829 .name = "Master Playback Switch",
830 .info = snd_pmac_boolean_stereo_info,
831 .get = tumbler_get_master_switch,
832 .put = tumbler_put_master_switch
833 },
834 DEFINE_SNAPPER_MIX("PCM Playback Volume", 0, VOL_IDX_PCM),
835 DEFINE_SNAPPER_MIX("PCM Playback Volume", 1, VOL_IDX_PCM2),
836 DEFINE_SNAPPER_MIX("Monitor Mix Volume", 0, VOL_IDX_ADC),
837 DEFINE_SNAPPER_MONO("Tone Control - Bass", bass),
838 DEFINE_SNAPPER_MONO("Tone Control - Treble", treble),
839 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
840 .name = "DRC Range",
841 .info = tumbler_info_drc_value,
842 .get = tumbler_get_drc_value,
843 .put = tumbler_put_drc_value
844 },
845 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
846 .name = "Input Source", /* FIXME: "Capture Source" doesn't work properly */
847 .info = snapper_info_capture_source,
848 .get = snapper_get_capture_source,
849 .put = snapper_put_capture_source
850 },
851};
852
853static snd_kcontrol_new_t tumbler_hp_sw __initdata = {
854 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
855 .name = "Headphone Playback Switch",
856 .info = snd_pmac_boolean_mono_info,
857 .get = tumbler_get_mute_switch,
858 .put = tumbler_put_mute_switch,
859 .private_value = TUMBLER_MUTE_HP,
860};
861static snd_kcontrol_new_t tumbler_speaker_sw __initdata = {
862 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
863 .name = "PC Speaker Playback Switch",
864 .info = snd_pmac_boolean_mono_info,
865 .get = tumbler_get_mute_switch,
866 .put = tumbler_put_mute_switch,
867 .private_value = TUMBLER_MUTE_AMP,
868};
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -0700869static snd_kcontrol_new_t tumbler_lineout_sw __initdata = {
870 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
871 .name = "Line Out Playback Switch",
872 .info = snd_pmac_boolean_mono_info,
873 .get = tumbler_get_mute_switch,
874 .put = tumbler_put_mute_switch,
875 .private_value = TUMBLER_MUTE_LINE,
876};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877static snd_kcontrol_new_t tumbler_drc_sw __initdata = {
878 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
879 .name = "DRC Switch",
880 .info = snd_pmac_boolean_mono_info,
881 .get = tumbler_get_drc_switch,
882 .put = tumbler_put_drc_switch
883};
884
885
886#ifdef PMAC_SUPPORT_AUTOMUTE
887/*
888 * auto-mute stuffs
889 */
890static int tumbler_detect_headphone(pmac_t *chip)
891{
892 pmac_tumbler_t *mix = chip->mixer_data;
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -0700893 int detect = 0;
894
895 if (mix->hp_detect.addr)
896 detect |= read_audio_gpio(&mix->hp_detect);
897 return detect;
898}
899
900static int tumbler_detect_lineout(pmac_t *chip)
901{
902 pmac_tumbler_t *mix = chip->mixer_data;
903 int detect = 0;
904
905 if (mix->line_detect.addr)
906 detect |= read_audio_gpio(&mix->line_detect);
907 return detect;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908}
909
910static void check_mute(pmac_t *chip, pmac_gpio_t *gp, int val, int do_notify, snd_kcontrol_t *sw)
911{
Benjamin Herrenschmidt4be8dc72005-05-01 08:58:43 -0700912 if (check_audio_gpio(gp) != val) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 write_audio_gpio(gp, val);
914 if (do_notify)
Benjamin Herrenschmidt4be8dc72005-05-01 08:58:43 -0700915 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
916 &sw->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 }
918}
919
920static struct work_struct device_change;
921
Benjamin Herrenschmidtb20af5f2005-04-16 15:24:32 -0700922static void device_change_handler(void *self)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923{
924 pmac_t *chip = (pmac_t*) self;
925 pmac_tumbler_t *mix;
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -0700926 int headphone, lineout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
928 if (!chip)
929 return;
930
931 mix = chip->mixer_data;
Benjamin Herrenschmidtb20af5f2005-04-16 15:24:32 -0700932 snd_assert(mix, return);
933
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -0700934 headphone = tumbler_detect_headphone(chip);
935 lineout = tumbler_detect_lineout(chip);
936
937 DBG("headphone: %d, lineout: %d\n", headphone, lineout);
938
939 if (headphone || lineout) {
940 /* unmute headphone/lineout & mute speaker */
941 if (headphone)
942 check_mute(chip, &mix->hp_mute, 0, mix->auto_mute_notify,
943 chip->master_sw_ctl);
944 if (lineout && mix->line_mute.addr != 0)
945 check_mute(chip, &mix->line_mute, 0, mix->auto_mute_notify,
946 chip->lineout_sw_ctl);
Benjamin Herrenschmidtb20af5f2005-04-16 15:24:32 -0700947 if (mix->anded_reset)
Nishanth Aravamudan989a0b22005-07-09 10:53:24 +0200948 msleep(10);
Benjamin Herrenschmidtb20af5f2005-04-16 15:24:32 -0700949 check_mute(chip, &mix->amp_mute, 1, mix->auto_mute_notify,
950 chip->speaker_sw_ctl);
Benjamin Herrenschmidtb20af5f2005-04-16 15:24:32 -0700951 } else {
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -0700952 /* unmute speaker, mute others */
Benjamin Herrenschmidtb20af5f2005-04-16 15:24:32 -0700953 check_mute(chip, &mix->amp_mute, 0, mix->auto_mute_notify,
954 chip->speaker_sw_ctl);
955 if (mix->anded_reset)
Nishanth Aravamudan989a0b22005-07-09 10:53:24 +0200956 msleep(10);
Benjamin Herrenschmidtb20af5f2005-04-16 15:24:32 -0700957 check_mute(chip, &mix->hp_mute, 1, mix->auto_mute_notify,
958 chip->master_sw_ctl);
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -0700959 if (mix->line_mute.addr != 0)
960 check_mute(chip, &mix->line_mute, 1, mix->auto_mute_notify,
961 chip->lineout_sw_ctl);
Benjamin Herrenschmidtb20af5f2005-04-16 15:24:32 -0700962 }
Takashi Iwaif3283852005-09-07 17:08:40 +0200963 if (mix->auto_mute_notify)
Benjamin Herrenschmidtb20af5f2005-04-16 15:24:32 -0700964 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
965 &chip->hp_detect_ctl->id);
Takashi Iwaif3283852005-09-07 17:08:40 +0200966
967#ifdef CONFIG_SND_POWERMAC_AUTO_DRC
968 mix->drc_enable = ! (headphone || lineout);
969 if (mix->auto_mute_notify)
Benjamin Herrenschmidtb20af5f2005-04-16 15:24:32 -0700970 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
971 &chip->drc_sw_ctl->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 if (chip->model == PMAC_TUMBLER)
973 tumbler_set_drc(mix);
974 else
975 snapper_set_drc(mix);
Takashi Iwaif3283852005-09-07 17:08:40 +0200976#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
978 /* reset the master volume so the correct amplification is applied */
979 tumbler_set_master_volume(mix);
980}
981
982static void tumbler_update_automute(pmac_t *chip, int do_notify)
983{
984 if (chip->auto_mute) {
Benjamin Herrenschmidtb20af5f2005-04-16 15:24:32 -0700985 pmac_tumbler_t *mix;
986 mix = chip->mixer_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 snd_assert(mix, return);
Benjamin Herrenschmidtb20af5f2005-04-16 15:24:32 -0700988 mix->auto_mute_notify = do_notify;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 schedule_work(&device_change);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 }
991}
992#endif /* PMAC_SUPPORT_AUTOMUTE */
993
994
995/* interrupt - headphone plug changed */
996static irqreturn_t headphone_intr(int irq, void *devid, struct pt_regs *regs)
997{
998 pmac_t *chip = devid;
999 if (chip->update_automute && chip->initialized) {
1000 chip->update_automute(chip, 1);
1001 return IRQ_HANDLED;
1002 }
1003 return IRQ_NONE;
1004}
1005
1006/* look for audio-gpio device */
1007static struct device_node *find_audio_device(const char *name)
1008{
1009 struct device_node *np;
1010
1011 if (! (np = find_devices("gpio")))
1012 return NULL;
1013
1014 for (np = np->child; np; np = np->sibling) {
1015 char *property = get_property(np, "audio-gpio", NULL);
1016 if (property && strcmp(property, name) == 0)
1017 return np;
1018 }
1019 return NULL;
1020}
1021
1022/* look for audio-gpio device */
1023static struct device_node *find_compatible_audio_device(const char *name)
1024{
1025 struct device_node *np;
1026
1027 if (! (np = find_devices("gpio")))
1028 return NULL;
1029
1030 for (np = np->child; np; np = np->sibling) {
1031 if (device_is_compatible(np, name))
1032 return np;
1033 }
1034 return NULL;
1035}
1036
1037/* find an audio device and get its address */
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -07001038static long tumbler_find_device(const char *device, const char *platform, pmac_gpio_t *gp, int is_compatible)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039{
1040 struct device_node *node;
Benjamin Herrenschmidtb75550e2005-04-16 15:24:31 -07001041 u32 *base, addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
1043 if (is_compatible)
1044 node = find_compatible_audio_device(device);
1045 else
1046 node = find_audio_device(device);
1047 if (! node) {
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -07001048 DBG("(W) cannot find audio device %s !\n", device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 snd_printdd("cannot find device %s\n", device);
1050 return -ENODEV;
1051 }
1052
1053 base = (u32 *)get_property(node, "AAPL,address", NULL);
1054 if (! base) {
Benjamin Herrenschmidtb75550e2005-04-16 15:24:31 -07001055 base = (u32 *)get_property(node, "reg", NULL);
1056 if (!base) {
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -07001057 DBG("(E) cannot find address for device %s !\n", device);
Benjamin Herrenschmidtb75550e2005-04-16 15:24:31 -07001058 snd_printd("cannot find address for device %s\n", device);
1059 return -ENODEV;
1060 }
Benjamin Herrenschmidtb75550e2005-04-16 15:24:31 -07001061 addr = *base;
1062 if (addr < 0x50)
1063 addr += 0x50;
1064 } else
1065 addr = *base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
Benjamin Herrenschmidtb75550e2005-04-16 15:24:31 -07001067 gp->addr = addr & 0x0000ffff;
Benjamin Herrenschmidtb75550e2005-04-16 15:24:31 -07001068 /* Try to find the active state, default to 0 ! */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 base = (u32 *)get_property(node, "audio-gpio-active-state", NULL);
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -07001070 if (base) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 gp->active_state = *base;
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -07001072 gp->active_val = (*base) ? 0x5 : 0x4;
1073 gp->inactive_val = (*base) ? 0x4 : 0x5;
1074 } else {
1075 u32 *prop = NULL;
Benjamin Herrenschmidtb75550e2005-04-16 15:24:31 -07001076 gp->active_state = 0;
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -07001077 gp->active_val = 0x4;
1078 gp->inactive_val = 0x5;
1079 /* Here are some crude hacks to extract the GPIO polarity and
1080 * open collector informations out of the do-platform script
1081 * as we don't yet have an interpreter for these things
1082 */
1083 if (platform)
1084 prop = (u32 *)get_property(node, platform, NULL);
1085 if (prop) {
1086 if (prop[3] == 0x9 && prop[4] == 0x9) {
1087 gp->active_val = 0xd;
1088 gp->inactive_val = 0xc;
1089 }
1090 if (prop[3] == 0x1 && prop[4] == 0x1) {
1091 gp->active_val = 0x5;
1092 gp->inactive_val = 0x4;
1093 }
1094 }
1095 }
1096
1097 DBG("(I) GPIO device %s found, offset: %x, active state: %d !\n",
1098 device, gp->addr, gp->active_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
1100 return (node->n_intrs > 0) ? node->intrs[0].line : 0;
1101}
1102
1103/* reset audio */
1104static void tumbler_reset_audio(pmac_t *chip)
1105{
1106 pmac_tumbler_t *mix = chip->mixer_data;
1107
Benjamin Herrenschmidtb20af5f2005-04-16 15:24:32 -07001108 if (mix->anded_reset) {
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -07001109 DBG("(I) codec anded reset !\n");
Benjamin Herrenschmidtb20af5f2005-04-16 15:24:32 -07001110 write_audio_gpio(&mix->hp_mute, 0);
1111 write_audio_gpio(&mix->amp_mute, 0);
Nishanth Aravamudan989a0b22005-07-09 10:53:24 +02001112 msleep(200);
Benjamin Herrenschmidtb20af5f2005-04-16 15:24:32 -07001113 write_audio_gpio(&mix->hp_mute, 1);
1114 write_audio_gpio(&mix->amp_mute, 1);
Nishanth Aravamudan989a0b22005-07-09 10:53:24 +02001115 msleep(100);
Benjamin Herrenschmidtb20af5f2005-04-16 15:24:32 -07001116 write_audio_gpio(&mix->hp_mute, 0);
1117 write_audio_gpio(&mix->amp_mute, 0);
Nishanth Aravamudan989a0b22005-07-09 10:53:24 +02001118 msleep(100);
Benjamin Herrenschmidtb20af5f2005-04-16 15:24:32 -07001119 } else {
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -07001120 DBG("(I) codec normal reset !\n");
1121
Benjamin Herrenschmidtb20af5f2005-04-16 15:24:32 -07001122 write_audio_gpio(&mix->audio_reset, 0);
Nishanth Aravamudan989a0b22005-07-09 10:53:24 +02001123 msleep(200);
Benjamin Herrenschmidtb20af5f2005-04-16 15:24:32 -07001124 write_audio_gpio(&mix->audio_reset, 1);
Nishanth Aravamudan989a0b22005-07-09 10:53:24 +02001125 msleep(100);
Benjamin Herrenschmidtb20af5f2005-04-16 15:24:32 -07001126 write_audio_gpio(&mix->audio_reset, 0);
Nishanth Aravamudan989a0b22005-07-09 10:53:24 +02001127 msleep(100);
Benjamin Herrenschmidtb20af5f2005-04-16 15:24:32 -07001128 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129}
1130
Benjamin Herrenschmidt8c870932005-06-27 14:36:34 -07001131#ifdef CONFIG_PM
Benjamin Herrenschmidtb20af5f2005-04-16 15:24:32 -07001132/* suspend mixer */
1133static void tumbler_suspend(pmac_t *chip)
1134{
1135 pmac_tumbler_t *mix = chip->mixer_data;
1136
1137 if (mix->headphone_irq >= 0)
1138 disable_irq(mix->headphone_irq);
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -07001139 if (mix->lineout_irq >= 0)
1140 disable_irq(mix->lineout_irq);
Benjamin Herrenschmidtb20af5f2005-04-16 15:24:32 -07001141 mix->save_master_switch[0] = mix->master_switch[0];
1142 mix->save_master_switch[1] = mix->master_switch[1];
Colin Leroy085e6fc2005-05-01 08:58:43 -07001143 mix->save_master_vol[0] = mix->master_vol[0];
1144 mix->save_master_vol[1] = mix->master_vol[1];
Benjamin Herrenschmidtb20af5f2005-04-16 15:24:32 -07001145 mix->master_switch[0] = mix->master_switch[1] = 0;
1146 tumbler_set_master_volume(mix);
1147 if (!mix->anded_reset) {
1148 write_audio_gpio(&mix->amp_mute, 1);
1149 write_audio_gpio(&mix->hp_mute, 1);
1150 }
1151 if (chip->model == PMAC_SNAPPER) {
1152 mix->acs |= 1;
1153 i2c_smbus_write_byte_data(mix->i2c.client, TAS_REG_ACS, mix->acs);
1154 }
1155 if (mix->anded_reset) {
1156 write_audio_gpio(&mix->amp_mute, 1);
1157 write_audio_gpio(&mix->hp_mute, 1);
1158 } else
1159 write_audio_gpio(&mix->audio_reset, 1);
1160}
1161
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162/* resume mixer */
1163static void tumbler_resume(pmac_t *chip)
1164{
1165 pmac_tumbler_t *mix = chip->mixer_data;
1166
1167 snd_assert(mix, return);
1168
Benjamin Herrenschmidtb20af5f2005-04-16 15:24:32 -07001169 mix->acs &= ~1;
1170 mix->master_switch[0] = mix->save_master_switch[0];
1171 mix->master_switch[1] = mix->save_master_switch[1];
Colin Leroy085e6fc2005-05-01 08:58:43 -07001172 mix->master_vol[0] = mix->save_master_vol[0];
1173 mix->master_vol[1] = mix->save_master_vol[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 tumbler_reset_audio(chip);
1175 if (mix->i2c.client && mix->i2c.init_client) {
1176 if (mix->i2c.init_client(&mix->i2c) < 0)
1177 printk(KERN_ERR "tumbler_init_client error\n");
1178 } else
1179 printk(KERN_ERR "tumbler: i2c is not initialized\n");
1180 if (chip->model == PMAC_TUMBLER) {
1181 tumbler_set_mono_volume(mix, &tumbler_pcm_vol_info);
1182 tumbler_set_mono_volume(mix, &tumbler_bass_vol_info);
1183 tumbler_set_mono_volume(mix, &tumbler_treble_vol_info);
1184 tumbler_set_drc(mix);
1185 } else {
1186 snapper_set_mix_vol(mix, VOL_IDX_PCM);
1187 snapper_set_mix_vol(mix, VOL_IDX_PCM2);
1188 snapper_set_mix_vol(mix, VOL_IDX_ADC);
1189 tumbler_set_mono_volume(mix, &snapper_bass_vol_info);
1190 tumbler_set_mono_volume(mix, &snapper_treble_vol_info);
1191 snapper_set_drc(mix);
1192 snapper_set_capture_source(mix);
1193 }
1194 tumbler_set_master_volume(mix);
1195 if (chip->update_automute)
1196 chip->update_automute(chip, 0);
Guido Guentherc0ce5c52005-11-15 17:28:05 +11001197 if (mix->headphone_irq >= 0) {
1198 unsigned char val;
1199
Benjamin Herrenschmidtb20af5f2005-04-16 15:24:32 -07001200 enable_irq(mix->headphone_irq);
Guido Guentherc0ce5c52005-11-15 17:28:05 +11001201 /* activate headphone status interrupts */
1202 val = do_gpio_read(&mix->hp_detect);
1203 do_gpio_write(&mix->hp_detect, val | 0x80);
1204 }
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -07001205 if (mix->lineout_irq >= 0)
1206 enable_irq(mix->lineout_irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207}
1208#endif
1209
1210/* initialize tumbler */
1211static int __init tumbler_init(pmac_t *chip)
1212{
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -07001213 int irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 pmac_tumbler_t *mix = chip->mixer_data;
1215 snd_assert(mix, return -EINVAL);
1216
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -07001217 if (tumbler_find_device("audio-hw-reset",
1218 "platform-do-hw-reset",
1219 &mix->audio_reset, 0) < 0)
1220 tumbler_find_device("hw-reset",
1221 "platform-do-hw-reset",
1222 &mix->audio_reset, 1);
1223 if (tumbler_find_device("amp-mute",
1224 "platform-do-amp-mute",
1225 &mix->amp_mute, 0) < 0)
1226 tumbler_find_device("amp-mute",
1227 "platform-do-amp-mute",
1228 &mix->amp_mute, 1);
1229 if (tumbler_find_device("headphone-mute",
1230 "platform-do-headphone-mute",
1231 &mix->hp_mute, 0) < 0)
1232 tumbler_find_device("headphone-mute",
1233 "platform-do-headphone-mute",
1234 &mix->hp_mute, 1);
1235 if (tumbler_find_device("line-output-mute",
1236 "platform-do-lineout-mute",
1237 &mix->line_mute, 0) < 0)
1238 tumbler_find_device("line-output-mute",
1239 "platform-do-lineout-mute",
1240 &mix->line_mute, 1);
1241 irq = tumbler_find_device("headphone-detect",
1242 NULL, &mix->hp_detect, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 if (irq < 0)
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -07001244 irq = tumbler_find_device("headphone-detect",
1245 NULL, &mix->hp_detect, 1);
Benjamin Herrenschmidtb75550e2005-04-16 15:24:31 -07001246 if (irq < 0)
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -07001247 irq = tumbler_find_device("keywest-gpio15",
1248 NULL, &mix->hp_detect, 1);
1249 mix->headphone_irq = irq;
1250 irq = tumbler_find_device("line-output-detect",
1251 NULL, &mix->line_detect, 0);
1252 if (irq < 0)
1253 irq = tumbler_find_device("line-output-detect",
1254 NULL, &mix->line_detect, 1);
1255 mix->lineout_irq = irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256
1257 tumbler_reset_audio(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
1259 return 0;
1260}
1261
1262static void tumbler_cleanup(pmac_t *chip)
1263{
1264 pmac_tumbler_t *mix = chip->mixer_data;
1265 if (! mix)
1266 return;
1267
1268 if (mix->headphone_irq >= 0)
1269 free_irq(mix->headphone_irq, chip);
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -07001270 if (mix->lineout_irq >= 0)
1271 free_irq(mix->lineout_irq, chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 tumbler_gpio_free(&mix->audio_reset);
1273 tumbler_gpio_free(&mix->amp_mute);
1274 tumbler_gpio_free(&mix->hp_mute);
1275 tumbler_gpio_free(&mix->hp_detect);
1276 snd_pmac_keywest_cleanup(&mix->i2c);
1277 kfree(mix);
1278 chip->mixer_data = NULL;
1279}
1280
1281/* exported */
1282int __init snd_pmac_tumbler_init(pmac_t *chip)
1283{
1284 int i, err;
1285 pmac_tumbler_t *mix;
1286 u32 *paddr;
Benjamin Herrenschmidtb20af5f2005-04-16 15:24:32 -07001287 struct device_node *tas_node, *np;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 char *chipname;
1289
1290#ifdef CONFIG_KMOD
1291 if (current->fs->root)
1292 request_module("i2c-keywest");
1293#endif /* CONFIG_KMOD */
1294
1295 mix = kmalloc(sizeof(*mix), GFP_KERNEL);
1296 if (! mix)
1297 return -ENOMEM;
1298 memset(mix, 0, sizeof(*mix));
1299 mix->headphone_irq = -1;
1300
1301 chip->mixer_data = mix;
1302 chip->mixer_free = tumbler_cleanup;
Benjamin Herrenschmidtb20af5f2005-04-16 15:24:32 -07001303 mix->anded_reset = 0;
1304 mix->reset_on_sleep = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
Benjamin Herrenschmidtb20af5f2005-04-16 15:24:32 -07001306 for (np = chip->node->child; np; np = np->sibling) {
1307 if (!strcmp(np->name, "sound")) {
1308 if (get_property(np, "has-anded-reset", NULL))
1309 mix->anded_reset = 1;
1310 if (get_property(np, "layout-id", NULL))
1311 mix->reset_on_sleep = 0;
1312 break;
1313 }
1314 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 if ((err = tumbler_init(chip)) < 0)
1316 return err;
1317
1318 /* set up TAS */
1319 tas_node = find_devices("deq");
1320 if (tas_node == NULL)
Benjamin Herrenschmidtb75550e2005-04-16 15:24:31 -07001321 tas_node = find_devices("codec");
1322 if (tas_node == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 return -ENODEV;
1324
1325 paddr = (u32 *)get_property(tas_node, "i2c-address", NULL);
Benjamin Herrenschmidtb75550e2005-04-16 15:24:31 -07001326 if (paddr == NULL)
1327 paddr = (u32 *)get_property(tas_node, "reg", NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 if (paddr)
1329 mix->i2c.addr = (*paddr) >> 1;
1330 else
1331 mix->i2c.addr = TAS_I2C_ADDR;
1332
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -07001333 DBG("(I) TAS i2c address is: %x\n", mix->i2c.addr);
1334
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 if (chip->model == PMAC_TUMBLER) {
1336 mix->i2c.init_client = tumbler_init_client;
1337 mix->i2c.name = "TAS3001c";
1338 chipname = "Tumbler";
1339 } else {
1340 mix->i2c.init_client = snapper_init_client;
1341 mix->i2c.name = "TAS3004";
1342 chipname = "Snapper";
1343 }
1344
1345 if ((err = snd_pmac_keywest_init(&mix->i2c)) < 0)
1346 return err;
1347
1348 /*
1349 * build mixers
1350 */
1351 sprintf(chip->card->mixername, "PowerMac %s", chipname);
1352
1353 if (chip->model == PMAC_TUMBLER) {
1354 for (i = 0; i < ARRAY_SIZE(tumbler_mixers); i++) {
1355 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&tumbler_mixers[i], chip))) < 0)
1356 return err;
1357 }
1358 } else {
1359 for (i = 0; i < ARRAY_SIZE(snapper_mixers); i++) {
1360 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snapper_mixers[i], chip))) < 0)
1361 return err;
1362 }
1363 }
1364 chip->master_sw_ctl = snd_ctl_new1(&tumbler_hp_sw, chip);
1365 if ((err = snd_ctl_add(chip->card, chip->master_sw_ctl)) < 0)
1366 return err;
1367 chip->speaker_sw_ctl = snd_ctl_new1(&tumbler_speaker_sw, chip);
1368 if ((err = snd_ctl_add(chip->card, chip->speaker_sw_ctl)) < 0)
1369 return err;
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -07001370 if (mix->line_mute.addr != 0) {
1371 chip->lineout_sw_ctl = snd_ctl_new1(&tumbler_lineout_sw, chip);
1372 if ((err = snd_ctl_add(chip->card, chip->lineout_sw_ctl)) < 0)
1373 return err;
1374 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 chip->drc_sw_ctl = snd_ctl_new1(&tumbler_drc_sw, chip);
1376 if ((err = snd_ctl_add(chip->card, chip->drc_sw_ctl)) < 0)
1377 return err;
1378
Takashi Iwaif3283852005-09-07 17:08:40 +02001379 /* set initial DRC range to 60% */
1380 if (chip->model == PMAC_TUMBLER)
1381 mix->drc_range = (TAS3001_DRC_MAX * 6) / 10;
1382 else
1383 mix->drc_range = (TAS3004_DRC_MAX * 6) / 10;
1384 mix->drc_enable = 1; /* will be changed later if AUTO_DRC is set */
1385 if (chip->model == PMAC_TUMBLER)
1386 tumbler_set_drc(mix);
1387 else
1388 snapper_set_drc(mix);
1389
Benjamin Herrenschmidt8c870932005-06-27 14:36:34 -07001390#ifdef CONFIG_PM
Benjamin Herrenschmidtb20af5f2005-04-16 15:24:32 -07001391 chip->suspend = tumbler_suspend;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 chip->resume = tumbler_resume;
1393#endif
1394
1395 INIT_WORK(&device_change, device_change_handler, (void *)chip);
1396
1397#ifdef PMAC_SUPPORT_AUTOMUTE
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -07001398 if ((mix->headphone_irq >=0 || mix->lineout_irq >= 0)
1399 && (err = snd_pmac_add_automute(chip)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 return err;
1401 chip->detect_headphone = tumbler_detect_headphone;
1402 chip->update_automute = tumbler_update_automute;
1403 tumbler_update_automute(chip, 0); /* update the status only */
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -07001404
1405 /* activate headphone status interrupts */
1406 if (mix->headphone_irq >= 0) {
1407 unsigned char val;
1408 if ((err = request_irq(mix->headphone_irq, headphone_intr, 0,
1409 "Sound Headphone Detection", chip)) < 0)
1410 return 0;
1411 /* activate headphone status interrupts */
1412 val = do_gpio_read(&mix->hp_detect);
1413 do_gpio_write(&mix->hp_detect, val | 0x80);
1414 }
1415 if (mix->lineout_irq >= 0) {
1416 unsigned char val;
1417 if ((err = request_irq(mix->lineout_irq, headphone_intr, 0,
1418 "Sound Lineout Detection", chip)) < 0)
1419 return 0;
1420 /* activate headphone status interrupts */
1421 val = do_gpio_read(&mix->line_detect);
1422 do_gpio_write(&mix->line_detect, val | 0x80);
1423 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424#endif
1425
1426 return 0;
1427}