Merge branch 'pm-pci'
[sfrench/cifs-2.6.git] / sound / ppc / tumbler.c
1 /*
2  * PMac Tumbler/Snapper lowlevel functions
3  *
4  * Copyright (c) by Takashi Iwai <tiwai@suse.de>
5  *
6  *   This program is free software; you can redistribute it and/or modify
7  *   it under the terms of the GNU General Public License as published by
8  *   the Free Software Foundation; either version 2 of the License, or
9  *   (at your option) any later version.
10  *
11  *   This program is distributed in the hope that it will be useful,
12  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *   GNU General Public License for more details.
15  *
16  *   You should have received a copy of the GNU General Public License
17  *   along with this program; if not, write to the Free Software
18  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19  *
20  *   Rene Rebe <rene.rebe@gmx.net>:
21  *     * update from shadow registers on wakeup and headphone plug
22  *     * automatically toggle DRC on headphone plug
23  *      
24  */
25
26
27 #include <linux/init.h>
28 #include <linux/delay.h>
29 #include <linux/i2c.h>
30 #include <linux/kmod.h>
31 #include <linux/slab.h>
32 #include <linux/interrupt.h>
33 #include <linux/string.h>
34 #include <linux/of_irq.h>
35 #include <linux/io.h>
36 #include <sound/core.h>
37 #include <asm/irq.h>
38 #include <asm/machdep.h>
39 #include <asm/pmac_feature.h>
40 #include "pmac.h"
41 #include "tumbler_volume.h"
42
43 #undef DEBUG
44
45 #ifdef DEBUG
46 #define DBG(fmt...) printk(KERN_DEBUG fmt)
47 #else
48 #define DBG(fmt...)
49 #endif
50
51 #define IS_G4DA (of_machine_is_compatible("PowerMac3,4"))
52
53 /* i2c address for tumbler */
54 #define TAS_I2C_ADDR    0x34
55
56 /* registers */
57 #define TAS_REG_MCS     0x01    /* main control */
58 #define TAS_REG_DRC     0x02
59 #define TAS_REG_VOL     0x04
60 #define TAS_REG_TREBLE  0x05
61 #define TAS_REG_BASS    0x06
62 #define TAS_REG_INPUT1  0x07
63 #define TAS_REG_INPUT2  0x08
64
65 /* tas3001c */
66 #define TAS_REG_PCM     TAS_REG_INPUT1
67  
68 /* tas3004 */
69 #define TAS_REG_LMIX    TAS_REG_INPUT1
70 #define TAS_REG_RMIX    TAS_REG_INPUT2
71 #define TAS_REG_MCS2    0x43            /* main control 2 */
72 #define TAS_REG_ACS     0x40            /* analog control */
73
74 /* mono volumes for tas3001c/tas3004 */
75 enum {
76         VOL_IDX_PCM_MONO, /* tas3001c only */
77         VOL_IDX_BASS, VOL_IDX_TREBLE,
78         VOL_IDX_LAST_MONO
79 };
80
81 /* stereo volumes for tas3004 */
82 enum {
83         VOL_IDX_PCM, VOL_IDX_PCM2, VOL_IDX_ADC,
84         VOL_IDX_LAST_MIX
85 };
86
87 struct pmac_gpio {
88         unsigned int addr;
89         u8 active_val;
90         u8 inactive_val;
91         u8 active_state;
92 };
93
94 struct pmac_tumbler {
95         struct pmac_keywest i2c;
96         struct pmac_gpio audio_reset;
97         struct pmac_gpio amp_mute;
98         struct pmac_gpio line_mute;
99         struct pmac_gpio line_detect;
100         struct pmac_gpio hp_mute;
101         struct pmac_gpio hp_detect;
102         int headphone_irq;
103         int lineout_irq;
104         unsigned int save_master_vol[2];
105         unsigned int master_vol[2];
106         unsigned int save_master_switch[2];
107         unsigned int master_switch[2];
108         unsigned int mono_vol[VOL_IDX_LAST_MONO];
109         unsigned int mix_vol[VOL_IDX_LAST_MIX][2]; /* stereo volumes for tas3004 */
110         int drc_range;
111         int drc_enable;
112         int capture_source;
113         int anded_reset;
114         int auto_mute_notify;
115         int reset_on_sleep;
116         u8  acs;
117 };
118
119
120 /*
121  */
122
123 static int send_init_client(struct pmac_keywest *i2c, unsigned int *regs)
124 {
125         while (*regs > 0) {
126                 int err, count = 10;
127                 do {
128                         err = i2c_smbus_write_byte_data(i2c->client,
129                                                         regs[0], regs[1]);
130                         if (err >= 0)
131                                 break;
132                         DBG("(W) i2c error %d\n", err);
133                         mdelay(10);
134                 } while (count--);
135                 if (err < 0)
136                         return -ENXIO;
137                 regs += 2;
138         }
139         return 0;
140 }
141
142
143 static int tumbler_init_client(struct pmac_keywest *i2c)
144 {
145         static unsigned int regs[] = {
146                 /* normal operation, SCLK=64fps, i2s output, i2s input, 16bit width */
147                 TAS_REG_MCS, (1<<6)|(2<<4)|(2<<2)|0,
148                 0, /* terminator */
149         };
150         DBG("(I) tumbler init client\n");
151         return send_init_client(i2c, regs);
152 }
153
154 static int snapper_init_client(struct pmac_keywest *i2c)
155 {
156         static unsigned int regs[] = {
157                 /* normal operation, SCLK=64fps, i2s output, 16bit width */
158                 TAS_REG_MCS, (1<<6)|(2<<4)|0,
159                 /* normal operation, all-pass mode */
160                 TAS_REG_MCS2, (1<<1),
161                 /* normal output, no deemphasis, A input, power-up, line-in */
162                 TAS_REG_ACS, 0,
163                 0, /* terminator */
164         };
165         DBG("(I) snapper init client\n");
166         return send_init_client(i2c, regs);
167 }
168         
169 /*
170  * gpio access
171  */
172 #define do_gpio_write(gp, val) \
173         pmac_call_feature(PMAC_FTR_WRITE_GPIO, NULL, (gp)->addr, val)
174 #define do_gpio_read(gp) \
175         pmac_call_feature(PMAC_FTR_READ_GPIO, NULL, (gp)->addr, 0)
176 #define tumbler_gpio_free(gp) /* NOP */
177
178 static void write_audio_gpio(struct pmac_gpio *gp, int active)
179 {
180         if (! gp->addr)
181                 return;
182         active = active ? gp->active_val : gp->inactive_val;
183         do_gpio_write(gp, active);
184         DBG("(I) gpio %x write %d\n", gp->addr, active);
185 }
186
187 static int check_audio_gpio(struct pmac_gpio *gp)
188 {
189         int ret;
190
191         if (! gp->addr)
192                 return 0;
193
194         ret = do_gpio_read(gp);
195
196         return (ret & 0x1) == (gp->active_val & 0x1);
197 }
198
199 static int read_audio_gpio(struct pmac_gpio *gp)
200 {
201         int ret;
202         if (! gp->addr)
203                 return 0;
204         ret = do_gpio_read(gp);
205         ret = (ret & 0x02) !=0;
206         return ret == gp->active_state;
207 }
208
209 /*
210  * update master volume
211  */
212 static int tumbler_set_master_volume(struct pmac_tumbler *mix)
213 {
214         unsigned char block[6];
215         unsigned int left_vol, right_vol;
216   
217         if (! mix->i2c.client)
218                 return -ENODEV;
219   
220         if (! mix->master_switch[0])
221                 left_vol = 0;
222         else {
223                 left_vol = mix->master_vol[0];
224                 if (left_vol >= ARRAY_SIZE(master_volume_table))
225                         left_vol = ARRAY_SIZE(master_volume_table) - 1;
226                 left_vol = master_volume_table[left_vol];
227         }
228         if (! mix->master_switch[1])
229                 right_vol = 0;
230         else {
231                 right_vol = mix->master_vol[1];
232                 if (right_vol >= ARRAY_SIZE(master_volume_table))
233                         right_vol = ARRAY_SIZE(master_volume_table) - 1;
234                 right_vol = master_volume_table[right_vol];
235         }
236
237         block[0] = (left_vol >> 16) & 0xff;
238         block[1] = (left_vol >> 8)  & 0xff;
239         block[2] = (left_vol >> 0)  & 0xff;
240
241         block[3] = (right_vol >> 16) & 0xff;
242         block[4] = (right_vol >> 8)  & 0xff;
243         block[5] = (right_vol >> 0)  & 0xff;
244   
245         if (i2c_smbus_write_i2c_block_data(mix->i2c.client, TAS_REG_VOL, 6,
246                                            block) < 0) {
247                 snd_printk(KERN_ERR "failed to set volume \n");
248                 return -EINVAL;
249         }
250         DBG("(I) succeeded to set volume (%u, %u)\n", left_vol, right_vol);
251         return 0;
252 }
253
254
255 /* output volume */
256 static int tumbler_info_master_volume(struct snd_kcontrol *kcontrol,
257                                       struct snd_ctl_elem_info *uinfo)
258 {
259         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
260         uinfo->count = 2;
261         uinfo->value.integer.min = 0;
262         uinfo->value.integer.max = ARRAY_SIZE(master_volume_table) - 1;
263         return 0;
264 }
265
266 static int tumbler_get_master_volume(struct snd_kcontrol *kcontrol,
267                                      struct snd_ctl_elem_value *ucontrol)
268 {
269         struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
270         struct pmac_tumbler *mix = chip->mixer_data;
271
272         ucontrol->value.integer.value[0] = mix->master_vol[0];
273         ucontrol->value.integer.value[1] = mix->master_vol[1];
274         return 0;
275 }
276
277 static int tumbler_put_master_volume(struct snd_kcontrol *kcontrol,
278                                      struct snd_ctl_elem_value *ucontrol)
279 {
280         struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
281         struct pmac_tumbler *mix = chip->mixer_data;
282         unsigned int vol[2];
283         int change;
284
285         vol[0] = ucontrol->value.integer.value[0];
286         vol[1] = ucontrol->value.integer.value[1];
287         if (vol[0] >= ARRAY_SIZE(master_volume_table) ||
288             vol[1] >= ARRAY_SIZE(master_volume_table))
289                 return -EINVAL;
290         change = mix->master_vol[0] != vol[0] ||
291                 mix->master_vol[1] != vol[1];
292         if (change) {
293                 mix->master_vol[0] = vol[0];
294                 mix->master_vol[1] = vol[1];
295                 tumbler_set_master_volume(mix);
296         }
297         return change;
298 }
299
300 /* output switch */
301 static int tumbler_get_master_switch(struct snd_kcontrol *kcontrol,
302                                      struct snd_ctl_elem_value *ucontrol)
303 {
304         struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
305         struct pmac_tumbler *mix = chip->mixer_data;
306
307         ucontrol->value.integer.value[0] = mix->master_switch[0];
308         ucontrol->value.integer.value[1] = mix->master_switch[1];
309         return 0;
310 }
311
312 static int tumbler_put_master_switch(struct snd_kcontrol *kcontrol,
313                                      struct snd_ctl_elem_value *ucontrol)
314 {
315         struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
316         struct pmac_tumbler *mix = chip->mixer_data;
317         int change;
318
319         change = mix->master_switch[0] != ucontrol->value.integer.value[0] ||
320                 mix->master_switch[1] != ucontrol->value.integer.value[1];
321         if (change) {
322                 mix->master_switch[0] = !!ucontrol->value.integer.value[0];
323                 mix->master_switch[1] = !!ucontrol->value.integer.value[1];
324                 tumbler_set_master_volume(mix);
325         }
326         return change;
327 }
328
329
330 /*
331  * TAS3001c dynamic range compression
332  */
333
334 #define TAS3001_DRC_MAX         0x5f
335
336 static int tumbler_set_drc(struct pmac_tumbler *mix)
337 {
338         unsigned char val[2];
339
340         if (! mix->i2c.client)
341                 return -ENODEV;
342   
343         if (mix->drc_enable) {
344                 val[0] = 0xc1; /* enable, 3:1 compression */
345                 if (mix->drc_range > TAS3001_DRC_MAX)
346                         val[1] = 0xf0;
347                 else if (mix->drc_range < 0)
348                         val[1] = 0x91;
349                 else
350                         val[1] = mix->drc_range + 0x91;
351         } else {
352                 val[0] = 0;
353                 val[1] = 0;
354         }
355
356         if (i2c_smbus_write_i2c_block_data(mix->i2c.client, TAS_REG_DRC,
357                                            2, val) < 0) {
358                 snd_printk(KERN_ERR "failed to set DRC\n");
359                 return -EINVAL;
360         }
361         DBG("(I) succeeded to set DRC (%u, %u)\n", val[0], val[1]);
362         return 0;
363 }
364
365 /*
366  * TAS3004
367  */
368
369 #define TAS3004_DRC_MAX         0xef
370
371 static int snapper_set_drc(struct pmac_tumbler *mix)
372 {
373         unsigned char val[6];
374
375         if (! mix->i2c.client)
376                 return -ENODEV;
377   
378         if (mix->drc_enable)
379                 val[0] = 0x50; /* 3:1 above threshold */
380         else
381                 val[0] = 0x51; /* disabled */
382         val[1] = 0x02; /* 1:1 below threshold */
383         if (mix->drc_range > 0xef)
384                 val[2] = 0xef;
385         else if (mix->drc_range < 0)
386                 val[2] = 0x00;
387         else
388                 val[2] = mix->drc_range;
389         val[3] = 0xb0;
390         val[4] = 0x60;
391         val[5] = 0xa0;
392
393         if (i2c_smbus_write_i2c_block_data(mix->i2c.client, TAS_REG_DRC,
394                                            6, val) < 0) {
395                 snd_printk(KERN_ERR "failed to set DRC\n");
396                 return -EINVAL;
397         }
398         DBG("(I) succeeded to set DRC (%u, %u)\n", val[0], val[1]);
399         return 0;
400 }
401
402 static int tumbler_info_drc_value(struct snd_kcontrol *kcontrol,
403                                   struct snd_ctl_elem_info *uinfo)
404 {
405         struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
406         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
407         uinfo->count = 1;
408         uinfo->value.integer.min = 0;
409         uinfo->value.integer.max =
410                 chip->model == PMAC_TUMBLER ? TAS3001_DRC_MAX : TAS3004_DRC_MAX;
411         return 0;
412 }
413
414 static int tumbler_get_drc_value(struct snd_kcontrol *kcontrol,
415                                  struct snd_ctl_elem_value *ucontrol)
416 {
417         struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
418         struct pmac_tumbler *mix;
419         if (! (mix = chip->mixer_data))
420                 return -ENODEV;
421         ucontrol->value.integer.value[0] = mix->drc_range;
422         return 0;
423 }
424
425 static int tumbler_put_drc_value(struct snd_kcontrol *kcontrol,
426                                  struct snd_ctl_elem_value *ucontrol)
427 {
428         struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
429         struct pmac_tumbler *mix;
430         unsigned int val;
431         int change;
432
433         if (! (mix = chip->mixer_data))
434                 return -ENODEV;
435         val = ucontrol->value.integer.value[0];
436         if (chip->model == PMAC_TUMBLER) {
437                 if (val > TAS3001_DRC_MAX)
438                         return -EINVAL;
439         } else {
440                 if (val > TAS3004_DRC_MAX)
441                         return -EINVAL;
442         }
443         change = mix->drc_range != val;
444         if (change) {
445                 mix->drc_range = val;
446                 if (chip->model == PMAC_TUMBLER)
447                         tumbler_set_drc(mix);
448                 else
449                         snapper_set_drc(mix);
450         }
451         return change;
452 }
453
454 static int tumbler_get_drc_switch(struct snd_kcontrol *kcontrol,
455                                   struct snd_ctl_elem_value *ucontrol)
456 {
457         struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
458         struct pmac_tumbler *mix;
459         if (! (mix = chip->mixer_data))
460                 return -ENODEV;
461         ucontrol->value.integer.value[0] = mix->drc_enable;
462         return 0;
463 }
464
465 static int tumbler_put_drc_switch(struct snd_kcontrol *kcontrol,
466                                   struct snd_ctl_elem_value *ucontrol)
467 {
468         struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
469         struct pmac_tumbler *mix;
470         int change;
471
472         if (! (mix = chip->mixer_data))
473                 return -ENODEV;
474         change = mix->drc_enable != ucontrol->value.integer.value[0];
475         if (change) {
476                 mix->drc_enable = !!ucontrol->value.integer.value[0];
477                 if (chip->model == PMAC_TUMBLER)
478                         tumbler_set_drc(mix);
479                 else
480                         snapper_set_drc(mix);
481         }
482         return change;
483 }
484
485
486 /*
487  * mono volumes
488  */
489
490 struct tumbler_mono_vol {
491         int index;
492         int reg;
493         int bytes;
494         unsigned int max;
495         unsigned int *table;
496 };
497
498 static int tumbler_set_mono_volume(struct pmac_tumbler *mix,
499                                    struct tumbler_mono_vol *info)
500 {
501         unsigned char block[4];
502         unsigned int vol;
503         int i;
504   
505         if (! mix->i2c.client)
506                 return -ENODEV;
507   
508         vol = mix->mono_vol[info->index];
509         if (vol >= info->max)
510                 vol = info->max - 1;
511         vol = info->table[vol];
512         for (i = 0; i < info->bytes; i++)
513                 block[i] = (vol >> ((info->bytes - i - 1) * 8)) & 0xff;
514         if (i2c_smbus_write_i2c_block_data(mix->i2c.client, info->reg,
515                                            info->bytes, block) < 0) {
516                 snd_printk(KERN_ERR "failed to set mono volume %d\n",
517                            info->index);
518                 return -EINVAL;
519         }
520         return 0;
521 }
522
523 static int tumbler_info_mono(struct snd_kcontrol *kcontrol,
524                              struct snd_ctl_elem_info *uinfo)
525 {
526         struct tumbler_mono_vol *info = (struct tumbler_mono_vol *)kcontrol->private_value;
527
528         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
529         uinfo->count = 1;
530         uinfo->value.integer.min = 0;
531         uinfo->value.integer.max = info->max - 1;
532         return 0;
533 }
534
535 static int tumbler_get_mono(struct snd_kcontrol *kcontrol,
536                             struct snd_ctl_elem_value *ucontrol)
537 {
538         struct tumbler_mono_vol *info = (struct tumbler_mono_vol *)kcontrol->private_value;
539         struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
540         struct pmac_tumbler *mix;
541         if (! (mix = chip->mixer_data))
542                 return -ENODEV;
543         ucontrol->value.integer.value[0] = mix->mono_vol[info->index];
544         return 0;
545 }
546
547 static int tumbler_put_mono(struct snd_kcontrol *kcontrol,
548                             struct snd_ctl_elem_value *ucontrol)
549 {
550         struct tumbler_mono_vol *info = (struct tumbler_mono_vol *)kcontrol->private_value;
551         struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
552         struct pmac_tumbler *mix;
553         unsigned int vol;
554         int change;
555
556         if (! (mix = chip->mixer_data))
557                 return -ENODEV;
558         vol = ucontrol->value.integer.value[0];
559         if (vol >= info->max)
560                 return -EINVAL;
561         change = mix->mono_vol[info->index] != vol;
562         if (change) {
563                 mix->mono_vol[info->index] = vol;
564                 tumbler_set_mono_volume(mix, info);
565         }
566         return change;
567 }
568
569 /* TAS3001c mono volumes */
570 static struct tumbler_mono_vol tumbler_pcm_vol_info = {
571         .index = VOL_IDX_PCM_MONO,
572         .reg = TAS_REG_PCM,
573         .bytes = 3,
574         .max = ARRAY_SIZE(mixer_volume_table),
575         .table = mixer_volume_table,
576 };
577
578 static struct tumbler_mono_vol tumbler_bass_vol_info = {
579         .index = VOL_IDX_BASS,
580         .reg = TAS_REG_BASS,
581         .bytes = 1,
582         .max = ARRAY_SIZE(bass_volume_table),
583         .table = bass_volume_table,
584 };
585
586 static struct tumbler_mono_vol tumbler_treble_vol_info = {
587         .index = VOL_IDX_TREBLE,
588         .reg = TAS_REG_TREBLE,
589         .bytes = 1,
590         .max = ARRAY_SIZE(treble_volume_table),
591         .table = treble_volume_table,
592 };
593
594 /* TAS3004 mono volumes */
595 static struct tumbler_mono_vol snapper_bass_vol_info = {
596         .index = VOL_IDX_BASS,
597         .reg = TAS_REG_BASS,
598         .bytes = 1,
599         .max = ARRAY_SIZE(snapper_bass_volume_table),
600         .table = snapper_bass_volume_table,
601 };
602
603 static struct tumbler_mono_vol snapper_treble_vol_info = {
604         .index = VOL_IDX_TREBLE,
605         .reg = TAS_REG_TREBLE,
606         .bytes = 1,
607         .max = ARRAY_SIZE(snapper_treble_volume_table),
608         .table = snapper_treble_volume_table,
609 };
610
611
612 #define DEFINE_MONO(xname,type) { \
613         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,\
614         .name = xname, \
615         .info = tumbler_info_mono, \
616         .get = tumbler_get_mono, \
617         .put = tumbler_put_mono, \
618         .private_value = (unsigned long)(&tumbler_##type##_vol_info), \
619 }
620
621 #define DEFINE_SNAPPER_MONO(xname,type) { \
622         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,\
623         .name = xname, \
624         .info = tumbler_info_mono, \
625         .get = tumbler_get_mono, \
626         .put = tumbler_put_mono, \
627         .private_value = (unsigned long)(&snapper_##type##_vol_info), \
628 }
629
630
631 /*
632  * snapper mixer volumes
633  */
634
635 static int snapper_set_mix_vol1(struct pmac_tumbler *mix, int idx, int ch, int reg)
636 {
637         int i, j, vol;
638         unsigned char block[9];
639
640         vol = mix->mix_vol[idx][ch];
641         if (vol >= ARRAY_SIZE(mixer_volume_table)) {
642                 vol = ARRAY_SIZE(mixer_volume_table) - 1;
643                 mix->mix_vol[idx][ch] = vol;
644         }
645
646         for (i = 0; i < 3; i++) {
647                 vol = mix->mix_vol[i][ch];
648                 vol = mixer_volume_table[vol];
649                 for (j = 0; j < 3; j++)
650                         block[i * 3 + j] = (vol >> ((2 - j) * 8)) & 0xff;
651         }
652         if (i2c_smbus_write_i2c_block_data(mix->i2c.client, reg,
653                                            9, block) < 0) {
654                 snd_printk(KERN_ERR "failed to set mono volume %d\n", reg);
655                 return -EINVAL;
656         }
657         return 0;
658 }
659
660 static int snapper_set_mix_vol(struct pmac_tumbler *mix, int idx)
661 {
662         if (! mix->i2c.client)
663                 return -ENODEV;
664         if (snapper_set_mix_vol1(mix, idx, 0, TAS_REG_LMIX) < 0 ||
665             snapper_set_mix_vol1(mix, idx, 1, TAS_REG_RMIX) < 0)
666                 return -EINVAL;
667         return 0;
668 }
669
670 static int snapper_info_mix(struct snd_kcontrol *kcontrol,
671                             struct snd_ctl_elem_info *uinfo)
672 {
673         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
674         uinfo->count = 2;
675         uinfo->value.integer.min = 0;
676         uinfo->value.integer.max = ARRAY_SIZE(mixer_volume_table) - 1;
677         return 0;
678 }
679
680 static int snapper_get_mix(struct snd_kcontrol *kcontrol,
681                            struct snd_ctl_elem_value *ucontrol)
682 {
683         int idx = (int)kcontrol->private_value;
684         struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
685         struct pmac_tumbler *mix;
686         if (! (mix = chip->mixer_data))
687                 return -ENODEV;
688         ucontrol->value.integer.value[0] = mix->mix_vol[idx][0];
689         ucontrol->value.integer.value[1] = mix->mix_vol[idx][1];
690         return 0;
691 }
692
693 static int snapper_put_mix(struct snd_kcontrol *kcontrol,
694                            struct snd_ctl_elem_value *ucontrol)
695 {
696         int idx = (int)kcontrol->private_value;
697         struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
698         struct pmac_tumbler *mix;
699         unsigned int vol[2];
700         int change;
701
702         if (! (mix = chip->mixer_data))
703                 return -ENODEV;
704         vol[0] = ucontrol->value.integer.value[0];
705         vol[1] = ucontrol->value.integer.value[1];
706         if (vol[0] >= ARRAY_SIZE(mixer_volume_table) ||
707             vol[1] >= ARRAY_SIZE(mixer_volume_table))
708                 return -EINVAL;
709         change = mix->mix_vol[idx][0] != vol[0] ||
710                 mix->mix_vol[idx][1] != vol[1];
711         if (change) {
712                 mix->mix_vol[idx][0] = vol[0];
713                 mix->mix_vol[idx][1] = vol[1];
714                 snapper_set_mix_vol(mix, idx);
715         }
716         return change;
717 }
718
719
720 /*
721  * mute switches. FIXME: Turn that into software mute when both outputs are muted
722  * to avoid codec reset on ibook M7
723  */
724
725 enum { TUMBLER_MUTE_HP, TUMBLER_MUTE_AMP, TUMBLER_MUTE_LINE };
726
727 static int tumbler_get_mute_switch(struct snd_kcontrol *kcontrol,
728                                    struct snd_ctl_elem_value *ucontrol)
729 {
730         struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
731         struct pmac_tumbler *mix;
732         struct pmac_gpio *gp;
733         if (! (mix = chip->mixer_data))
734                 return -ENODEV;
735         switch(kcontrol->private_value) {
736         case TUMBLER_MUTE_HP:
737                 gp = &mix->hp_mute;     break;
738         case TUMBLER_MUTE_AMP:
739                 gp = &mix->amp_mute;    break;
740         case TUMBLER_MUTE_LINE:
741                 gp = &mix->line_mute;   break;
742         default:
743                 gp = NULL;
744         }
745         if (gp == NULL)
746                 return -EINVAL;
747         ucontrol->value.integer.value[0] = !check_audio_gpio(gp);
748         return 0;
749 }
750
751 static int tumbler_put_mute_switch(struct snd_kcontrol *kcontrol,
752                                    struct snd_ctl_elem_value *ucontrol)
753 {
754         struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
755         struct pmac_tumbler *mix;
756         struct pmac_gpio *gp;
757         int val;
758 #ifdef PMAC_SUPPORT_AUTOMUTE
759         if (chip->update_automute && chip->auto_mute)
760                 return 0; /* don't touch in the auto-mute mode */
761 #endif  
762         if (! (mix = chip->mixer_data))
763                 return -ENODEV;
764         switch(kcontrol->private_value) {
765         case TUMBLER_MUTE_HP:
766                 gp = &mix->hp_mute;     break;
767         case TUMBLER_MUTE_AMP:
768                 gp = &mix->amp_mute;    break;
769         case TUMBLER_MUTE_LINE:
770                 gp = &mix->line_mute;   break;
771         default:
772                 gp = NULL;
773         }
774         if (gp == NULL)
775                 return -EINVAL;
776         val = ! check_audio_gpio(gp);
777         if (val != ucontrol->value.integer.value[0]) {
778                 write_audio_gpio(gp, ! ucontrol->value.integer.value[0]);
779                 return 1;
780         }
781         return 0;
782 }
783
784 static int snapper_set_capture_source(struct pmac_tumbler *mix)
785 {
786         if (! mix->i2c.client)
787                 return -ENODEV;
788         if (mix->capture_source)
789                 mix->acs |= 2;
790         else
791                 mix->acs &= ~2;
792         return i2c_smbus_write_byte_data(mix->i2c.client, TAS_REG_ACS, mix->acs);
793 }
794
795 static int snapper_info_capture_source(struct snd_kcontrol *kcontrol,
796                                        struct snd_ctl_elem_info *uinfo)
797 {
798         static const char * const texts[2] = {
799                 "Line", "Mic"
800         };
801
802         return snd_ctl_enum_info(uinfo, 1, 2, texts);
803 }
804
805 static int snapper_get_capture_source(struct snd_kcontrol *kcontrol,
806                                       struct snd_ctl_elem_value *ucontrol)
807 {
808         struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
809         struct pmac_tumbler *mix = chip->mixer_data;
810
811         ucontrol->value.enumerated.item[0] = mix->capture_source;
812         return 0;
813 }
814
815 static int snapper_put_capture_source(struct snd_kcontrol *kcontrol,
816                                       struct snd_ctl_elem_value *ucontrol)
817 {
818         struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
819         struct pmac_tumbler *mix = chip->mixer_data;
820         int change;
821
822         change = ucontrol->value.enumerated.item[0] != mix->capture_source;
823         if (change) {
824                 mix->capture_source = !!ucontrol->value.enumerated.item[0];
825                 snapper_set_capture_source(mix);
826         }
827         return change;
828 }
829
830 #define DEFINE_SNAPPER_MIX(xname,idx,ofs) { \
831         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,\
832         .name = xname, \
833         .info = snapper_info_mix, \
834         .get = snapper_get_mix, \
835         .put = snapper_put_mix, \
836         .index = idx,\
837         .private_value = ofs, \
838 }
839
840
841 /*
842  */
843 static struct snd_kcontrol_new tumbler_mixers[] = {
844         { .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_MONO("Tone Control - Bass", bass),
857         DEFINE_MONO("Tone Control - Treble", treble),
858         DEFINE_MONO("PCM Playback Volume", pcm),
859         { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
860           .name = "DRC Range",
861           .info = tumbler_info_drc_value,
862           .get = tumbler_get_drc_value,
863           .put = tumbler_put_drc_value
864         },
865 };
866
867 static struct snd_kcontrol_new snapper_mixers[] = {
868         { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
869           .name = "Master Playback Volume",
870           .info = tumbler_info_master_volume,
871           .get = tumbler_get_master_volume,
872           .put = tumbler_put_master_volume
873         },
874         { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
875           .name = "Master Playback Switch",
876           .info = snd_pmac_boolean_stereo_info,
877           .get = tumbler_get_master_switch,
878           .put = tumbler_put_master_switch
879         },
880         DEFINE_SNAPPER_MIX("PCM Playback Volume", 0, VOL_IDX_PCM),
881         /* Alternative PCM is assigned to Mic analog loopback on iBook G4 */
882         DEFINE_SNAPPER_MIX("Mic Playback Volume", 0, VOL_IDX_PCM2),
883         DEFINE_SNAPPER_MIX("Monitor Mix Volume", 0, VOL_IDX_ADC),
884         DEFINE_SNAPPER_MONO("Tone Control - Bass", bass),
885         DEFINE_SNAPPER_MONO("Tone Control - Treble", treble),
886         { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
887           .name = "DRC Range",
888           .info = tumbler_info_drc_value,
889           .get = tumbler_get_drc_value,
890           .put = tumbler_put_drc_value
891         },
892         { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
893           .name = "Input Source", /* FIXME: "Capture Source" doesn't work properly */
894           .info = snapper_info_capture_source,
895           .get = snapper_get_capture_source,
896           .put = snapper_put_capture_source
897         },
898 };
899
900 static const struct snd_kcontrol_new tumbler_hp_sw = {
901         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
902         .name = "Headphone Playback Switch",
903         .info = snd_pmac_boolean_mono_info,
904         .get = tumbler_get_mute_switch,
905         .put = tumbler_put_mute_switch,
906         .private_value = TUMBLER_MUTE_HP,
907 };
908 static const struct snd_kcontrol_new tumbler_speaker_sw = {
909         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
910         .name = "Speaker Playback Switch",
911         .info = snd_pmac_boolean_mono_info,
912         .get = tumbler_get_mute_switch,
913         .put = tumbler_put_mute_switch,
914         .private_value = TUMBLER_MUTE_AMP,
915 };
916 static const struct snd_kcontrol_new tumbler_lineout_sw = {
917         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
918         .name = "Line Out Playback Switch",
919         .info = snd_pmac_boolean_mono_info,
920         .get = tumbler_get_mute_switch,
921         .put = tumbler_put_mute_switch,
922         .private_value = TUMBLER_MUTE_LINE,
923 };
924 static const struct snd_kcontrol_new tumbler_drc_sw = {
925         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
926         .name = "DRC Switch",
927         .info = snd_pmac_boolean_mono_info,
928         .get = tumbler_get_drc_switch,
929         .put = tumbler_put_drc_switch
930 };
931
932
933 #ifdef PMAC_SUPPORT_AUTOMUTE
934 /*
935  * auto-mute stuffs
936  */
937 static int tumbler_detect_headphone(struct snd_pmac *chip)
938 {
939         struct pmac_tumbler *mix = chip->mixer_data;
940         int detect = 0;
941
942         if (mix->hp_detect.addr)
943                 detect |= read_audio_gpio(&mix->hp_detect);
944         return detect;
945 }
946
947 static int tumbler_detect_lineout(struct snd_pmac *chip)
948 {
949         struct pmac_tumbler *mix = chip->mixer_data;
950         int detect = 0;
951
952         if (mix->line_detect.addr)
953                 detect |= read_audio_gpio(&mix->line_detect);
954         return detect;
955 }
956
957 static void check_mute(struct snd_pmac *chip, struct pmac_gpio *gp, int val, int do_notify,
958                        struct snd_kcontrol *sw)
959 {
960         if (check_audio_gpio(gp) != val) {
961                 write_audio_gpio(gp, val);
962                 if (do_notify)
963                         snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
964                                        &sw->id);
965         }
966 }
967
968 static struct work_struct device_change;
969 static struct snd_pmac *device_change_chip;
970
971 static void device_change_handler(struct work_struct *work)
972 {
973         struct snd_pmac *chip = device_change_chip;
974         struct pmac_tumbler *mix;
975         int headphone, lineout;
976
977         if (!chip)
978                 return;
979
980         mix = chip->mixer_data;
981         if (snd_BUG_ON(!mix))
982                 return;
983
984         headphone = tumbler_detect_headphone(chip);
985         lineout = tumbler_detect_lineout(chip);
986
987         DBG("headphone: %d, lineout: %d\n", headphone, lineout);
988
989         if (headphone || lineout) {
990                 /* unmute headphone/lineout & mute speaker */
991                 if (headphone)
992                         check_mute(chip, &mix->hp_mute, 0, mix->auto_mute_notify,
993                                    chip->master_sw_ctl);
994                 if (lineout && mix->line_mute.addr != 0)
995                         check_mute(chip, &mix->line_mute, 0, mix->auto_mute_notify,
996                                    chip->lineout_sw_ctl);
997                 if (mix->anded_reset)
998                         msleep(10);
999                 check_mute(chip, &mix->amp_mute, !IS_G4DA, mix->auto_mute_notify,
1000                            chip->speaker_sw_ctl);
1001         } else {
1002                 /* unmute speaker, mute others */
1003                 check_mute(chip, &mix->amp_mute, 0, mix->auto_mute_notify,
1004                            chip->speaker_sw_ctl);
1005                 if (mix->anded_reset)
1006                         msleep(10);
1007                 check_mute(chip, &mix->hp_mute, 1, mix->auto_mute_notify,
1008                            chip->master_sw_ctl);
1009                 if (mix->line_mute.addr != 0)
1010                         check_mute(chip, &mix->line_mute, 1, mix->auto_mute_notify,
1011                                    chip->lineout_sw_ctl);
1012         }
1013         if (mix->auto_mute_notify)
1014                 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
1015                                        &chip->hp_detect_ctl->id);
1016
1017 #ifdef CONFIG_SND_POWERMAC_AUTO_DRC
1018         mix->drc_enable = ! (headphone || lineout);
1019         if (mix->auto_mute_notify)
1020                 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
1021                                &chip->drc_sw_ctl->id);
1022         if (chip->model == PMAC_TUMBLER)
1023                 tumbler_set_drc(mix);
1024         else
1025                 snapper_set_drc(mix);
1026 #endif
1027
1028         /* reset the master volume so the correct amplification is applied */
1029         tumbler_set_master_volume(mix);
1030 }
1031
1032 static void tumbler_update_automute(struct snd_pmac *chip, int do_notify)
1033 {
1034         if (chip->auto_mute) {
1035                 struct pmac_tumbler *mix;
1036                 mix = chip->mixer_data;
1037                 if (snd_BUG_ON(!mix))
1038                         return;
1039                 mix->auto_mute_notify = do_notify;
1040                 schedule_work(&device_change);
1041         }
1042 }
1043 #endif /* PMAC_SUPPORT_AUTOMUTE */
1044
1045
1046 /* interrupt - headphone plug changed */
1047 static irqreturn_t headphone_intr(int irq, void *devid)
1048 {
1049         struct snd_pmac *chip = devid;
1050         if (chip->update_automute && chip->initialized) {
1051                 chip->update_automute(chip, 1);
1052                 return IRQ_HANDLED;
1053         }
1054         return IRQ_NONE;
1055 }
1056
1057 /* look for audio-gpio device */
1058 static struct device_node *find_audio_device(const char *name)
1059 {
1060         struct device_node *gpiop;
1061         struct device_node *np;
1062   
1063         gpiop = of_find_node_by_name(NULL, "gpio");
1064         if (! gpiop)
1065                 return NULL;
1066   
1067         for (np = of_get_next_child(gpiop, NULL); np;
1068                         np = of_get_next_child(gpiop, np)) {
1069                 const char *property = of_get_property(np, "audio-gpio", NULL);
1070                 if (property && strcmp(property, name) == 0)
1071                         break;
1072         }  
1073         of_node_put(gpiop);
1074         return np;
1075 }
1076
1077 /* look for audio-gpio device */
1078 static struct device_node *find_compatible_audio_device(const char *name)
1079 {
1080         struct device_node *gpiop;
1081         struct device_node *np;
1082   
1083         gpiop = of_find_node_by_name(NULL, "gpio");
1084         if (!gpiop)
1085                 return NULL;
1086   
1087         for (np = of_get_next_child(gpiop, NULL); np;
1088                         np = of_get_next_child(gpiop, np)) {
1089                 if (of_device_is_compatible(np, name))
1090                         break;
1091         }  
1092         of_node_put(gpiop);
1093         return np;
1094 }
1095
1096 /* find an audio device and get its address */
1097 static long tumbler_find_device(const char *device, const char *platform,
1098                                 struct pmac_gpio *gp, int is_compatible)
1099 {
1100         struct device_node *node;
1101         const u32 *base;
1102         u32 addr;
1103         long ret;
1104
1105         if (is_compatible)
1106                 node = find_compatible_audio_device(device);
1107         else
1108                 node = find_audio_device(device);
1109         if (! node) {
1110                 DBG("(W) cannot find audio device %s !\n", device);
1111                 snd_printdd("cannot find device %s\n", device);
1112                 return -ENODEV;
1113         }
1114
1115         base = of_get_property(node, "AAPL,address", NULL);
1116         if (! base) {
1117                 base = of_get_property(node, "reg", NULL);
1118                 if (!base) {
1119                         DBG("(E) cannot find address for device %s !\n", device);
1120                         snd_printd("cannot find address for device %s\n", device);
1121                         of_node_put(node);
1122                         return -ENODEV;
1123                 }
1124                 addr = *base;
1125                 if (addr < 0x50)
1126                         addr += 0x50;
1127         } else
1128                 addr = *base;
1129
1130         gp->addr = addr & 0x0000ffff;
1131         /* Try to find the active state, default to 0 ! */
1132         base = of_get_property(node, "audio-gpio-active-state", NULL);
1133         if (base) {
1134                 gp->active_state = *base;
1135                 gp->active_val = (*base) ? 0x5 : 0x4;
1136                 gp->inactive_val = (*base) ? 0x4 : 0x5;
1137         } else {
1138                 const u32 *prop = NULL;
1139                 gp->active_state = IS_G4DA
1140                                 && !strncmp(device, "keywest-gpio1", 13);
1141                 gp->active_val = 0x4;
1142                 gp->inactive_val = 0x5;
1143                 /* Here are some crude hacks to extract the GPIO polarity and
1144                  * open collector informations out of the do-platform script
1145                  * as we don't yet have an interpreter for these things
1146                  */
1147                 if (platform)
1148                         prop = of_get_property(node, platform, NULL);
1149                 if (prop) {
1150                         if (prop[3] == 0x9 && prop[4] == 0x9) {
1151                                 gp->active_val = 0xd;
1152                                 gp->inactive_val = 0xc;
1153                         }
1154                         if (prop[3] == 0x1 && prop[4] == 0x1) {
1155                                 gp->active_val = 0x5;
1156                                 gp->inactive_val = 0x4;
1157                         }
1158                 }
1159         }
1160
1161         DBG("(I) GPIO device %s found, offset: %x, active state: %d !\n",
1162             device, gp->addr, gp->active_state);
1163
1164         ret = irq_of_parse_and_map(node, 0);
1165         of_node_put(node);
1166         return ret;
1167 }
1168
1169 /* reset audio */
1170 static void tumbler_reset_audio(struct snd_pmac *chip)
1171 {
1172         struct pmac_tumbler *mix = chip->mixer_data;
1173
1174         if (mix->anded_reset) {
1175                 DBG("(I) codec anded reset !\n");
1176                 write_audio_gpio(&mix->hp_mute, 0);
1177                 write_audio_gpio(&mix->amp_mute, 0);
1178                 msleep(200);
1179                 write_audio_gpio(&mix->hp_mute, 1);
1180                 write_audio_gpio(&mix->amp_mute, 1);
1181                 msleep(100);
1182                 write_audio_gpio(&mix->hp_mute, 0);
1183                 write_audio_gpio(&mix->amp_mute, 0);
1184                 msleep(100);
1185         } else {
1186                 DBG("(I) codec normal reset !\n");
1187
1188                 write_audio_gpio(&mix->audio_reset, 0);
1189                 msleep(200);
1190                 write_audio_gpio(&mix->audio_reset, 1);
1191                 msleep(100);
1192                 write_audio_gpio(&mix->audio_reset, 0);
1193                 msleep(100);
1194         }
1195 }
1196
1197 #ifdef CONFIG_PM
1198 /* suspend mixer */
1199 static void tumbler_suspend(struct snd_pmac *chip)
1200 {
1201         struct pmac_tumbler *mix = chip->mixer_data;
1202
1203         if (mix->headphone_irq >= 0)
1204                 disable_irq(mix->headphone_irq);
1205         if (mix->lineout_irq >= 0)
1206                 disable_irq(mix->lineout_irq);
1207         mix->save_master_switch[0] = mix->master_switch[0];
1208         mix->save_master_switch[1] = mix->master_switch[1];
1209         mix->save_master_vol[0] = mix->master_vol[0];
1210         mix->save_master_vol[1] = mix->master_vol[1];
1211         mix->master_switch[0] = mix->master_switch[1] = 0;
1212         tumbler_set_master_volume(mix);
1213         if (!mix->anded_reset) {
1214                 write_audio_gpio(&mix->amp_mute, 1);
1215                 write_audio_gpio(&mix->hp_mute, 1);
1216         }
1217         if (chip->model == PMAC_SNAPPER) {
1218                 mix->acs |= 1;
1219                 i2c_smbus_write_byte_data(mix->i2c.client, TAS_REG_ACS, mix->acs);
1220         }
1221         if (mix->anded_reset) {
1222                 write_audio_gpio(&mix->amp_mute, 1);
1223                 write_audio_gpio(&mix->hp_mute, 1);
1224         } else
1225                 write_audio_gpio(&mix->audio_reset, 1);
1226 }
1227
1228 /* resume mixer */
1229 static void tumbler_resume(struct snd_pmac *chip)
1230 {
1231         struct pmac_tumbler *mix = chip->mixer_data;
1232
1233         mix->acs &= ~1;
1234         mix->master_switch[0] = mix->save_master_switch[0];
1235         mix->master_switch[1] = mix->save_master_switch[1];
1236         mix->master_vol[0] = mix->save_master_vol[0];
1237         mix->master_vol[1] = mix->save_master_vol[1];
1238         tumbler_reset_audio(chip);
1239         if (mix->i2c.client && mix->i2c.init_client) {
1240                 if (mix->i2c.init_client(&mix->i2c) < 0)
1241                         printk(KERN_ERR "tumbler_init_client error\n");
1242         } else
1243                 printk(KERN_ERR "tumbler: i2c is not initialized\n");
1244         if (chip->model == PMAC_TUMBLER) {
1245                 tumbler_set_mono_volume(mix, &tumbler_pcm_vol_info);
1246                 tumbler_set_mono_volume(mix, &tumbler_bass_vol_info);
1247                 tumbler_set_mono_volume(mix, &tumbler_treble_vol_info);
1248                 tumbler_set_drc(mix);
1249         } else {
1250                 snapper_set_mix_vol(mix, VOL_IDX_PCM);
1251                 snapper_set_mix_vol(mix, VOL_IDX_PCM2);
1252                 snapper_set_mix_vol(mix, VOL_IDX_ADC);
1253                 tumbler_set_mono_volume(mix, &snapper_bass_vol_info);
1254                 tumbler_set_mono_volume(mix, &snapper_treble_vol_info);
1255                 snapper_set_drc(mix);
1256                 snapper_set_capture_source(mix);
1257         }
1258         tumbler_set_master_volume(mix);
1259         if (chip->update_automute)
1260                 chip->update_automute(chip, 0);
1261         if (mix->headphone_irq >= 0) {
1262                 unsigned char val;
1263
1264                 enable_irq(mix->headphone_irq);
1265                 /* activate headphone status interrupts */
1266                 val = do_gpio_read(&mix->hp_detect);
1267                 do_gpio_write(&mix->hp_detect, val | 0x80);
1268         }
1269         if (mix->lineout_irq >= 0)
1270                 enable_irq(mix->lineout_irq);
1271 }
1272 #endif
1273
1274 /* initialize tumbler */
1275 static int tumbler_init(struct snd_pmac *chip)
1276 {
1277         int irq;
1278         struct pmac_tumbler *mix = chip->mixer_data;
1279
1280         if (tumbler_find_device("audio-hw-reset",
1281                                 "platform-do-hw-reset",
1282                                 &mix->audio_reset, 0) < 0)
1283                 tumbler_find_device("hw-reset",
1284                                     "platform-do-hw-reset",
1285                                     &mix->audio_reset, 1);
1286         if (tumbler_find_device("amp-mute",
1287                                 "platform-do-amp-mute",
1288                                 &mix->amp_mute, 0) < 0)
1289                 tumbler_find_device("amp-mute",
1290                                     "platform-do-amp-mute",
1291                                     &mix->amp_mute, 1);
1292         if (tumbler_find_device("headphone-mute",
1293                                 "platform-do-headphone-mute",
1294                                 &mix->hp_mute, 0) < 0)
1295                 tumbler_find_device("headphone-mute",
1296                                     "platform-do-headphone-mute",
1297                                     &mix->hp_mute, 1);
1298         if (tumbler_find_device("line-output-mute",
1299                                 "platform-do-lineout-mute",
1300                                 &mix->line_mute, 0) < 0)
1301                 tumbler_find_device("line-output-mute",
1302                                    "platform-do-lineout-mute",
1303                                     &mix->line_mute, 1);
1304         irq = tumbler_find_device("headphone-detect",
1305                                   NULL, &mix->hp_detect, 0);
1306         if (irq <= 0)
1307                 irq = tumbler_find_device("headphone-detect",
1308                                           NULL, &mix->hp_detect, 1);
1309         if (irq <= 0)
1310                 irq = tumbler_find_device("keywest-gpio15",
1311                                           NULL, &mix->hp_detect, 1);
1312         mix->headphone_irq = irq;
1313         irq = tumbler_find_device("line-output-detect",
1314                                   NULL, &mix->line_detect, 0);
1315         if (irq <= 0)
1316                 irq = tumbler_find_device("line-output-detect",
1317                                           NULL, &mix->line_detect, 1);
1318         if (IS_G4DA && irq <= 0)
1319                 irq = tumbler_find_device("keywest-gpio16",
1320                                           NULL, &mix->line_detect, 1);
1321         mix->lineout_irq = irq;
1322
1323         tumbler_reset_audio(chip);
1324   
1325         return 0;
1326 }
1327
1328 static void tumbler_cleanup(struct snd_pmac *chip)
1329 {
1330         struct pmac_tumbler *mix = chip->mixer_data;
1331         if (! mix)
1332                 return;
1333
1334         if (mix->headphone_irq >= 0)
1335                 free_irq(mix->headphone_irq, chip);
1336         if (mix->lineout_irq >= 0)
1337                 free_irq(mix->lineout_irq, chip);
1338         tumbler_gpio_free(&mix->audio_reset);
1339         tumbler_gpio_free(&mix->amp_mute);
1340         tumbler_gpio_free(&mix->hp_mute);
1341         tumbler_gpio_free(&mix->hp_detect);
1342         snd_pmac_keywest_cleanup(&mix->i2c);
1343         kfree(mix);
1344         chip->mixer_data = NULL;
1345 }
1346
1347 /* exported */
1348 int snd_pmac_tumbler_init(struct snd_pmac *chip)
1349 {
1350         int i, err;
1351         struct pmac_tumbler *mix;
1352         const u32 *paddr;
1353         struct device_node *tas_node, *np;
1354         char *chipname;
1355
1356         request_module("i2c-powermac");
1357
1358         mix = kzalloc(sizeof(*mix), GFP_KERNEL);
1359         if (! mix)
1360                 return -ENOMEM;
1361         mix->headphone_irq = -1;
1362
1363         chip->mixer_data = mix;
1364         chip->mixer_free = tumbler_cleanup;
1365         mix->anded_reset = 0;
1366         mix->reset_on_sleep = 1;
1367
1368         for (np = chip->node->child; np; np = np->sibling) {
1369                 if (!strcmp(np->name, "sound")) {
1370                         if (of_get_property(np, "has-anded-reset", NULL))
1371                                 mix->anded_reset = 1;
1372                         if (of_get_property(np, "layout-id", NULL))
1373                                 mix->reset_on_sleep = 0;
1374                         break;
1375                 }
1376         }
1377         if ((err = tumbler_init(chip)) < 0)
1378                 return err;
1379
1380         /* set up TAS */
1381         tas_node = of_find_node_by_name(NULL, "deq");
1382         if (tas_node == NULL)
1383                 tas_node = of_find_node_by_name(NULL, "codec");
1384         if (tas_node == NULL)
1385                 return -ENODEV;
1386
1387         paddr = of_get_property(tas_node, "i2c-address", NULL);
1388         if (paddr == NULL)
1389                 paddr = of_get_property(tas_node, "reg", NULL);
1390         if (paddr)
1391                 mix->i2c.addr = (*paddr) >> 1;
1392         else
1393                 mix->i2c.addr = TAS_I2C_ADDR;
1394         of_node_put(tas_node);
1395
1396         DBG("(I) TAS i2c address is: %x\n", mix->i2c.addr);
1397
1398         if (chip->model == PMAC_TUMBLER) {
1399                 mix->i2c.init_client = tumbler_init_client;
1400                 mix->i2c.name = "TAS3001c";
1401                 chipname = "Tumbler";
1402         } else {
1403                 mix->i2c.init_client = snapper_init_client;
1404                 mix->i2c.name = "TAS3004";
1405                 chipname = "Snapper";
1406         }
1407
1408         if ((err = snd_pmac_keywest_init(&mix->i2c)) < 0)
1409                 return err;
1410
1411         /*
1412          * build mixers
1413          */
1414         sprintf(chip->card->mixername, "PowerMac %s", chipname);
1415
1416         if (chip->model == PMAC_TUMBLER) {
1417                 for (i = 0; i < ARRAY_SIZE(tumbler_mixers); i++) {
1418                         if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&tumbler_mixers[i], chip))) < 0)
1419                                 return err;
1420                 }
1421         } else {
1422                 for (i = 0; i < ARRAY_SIZE(snapper_mixers); i++) {
1423                         if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snapper_mixers[i], chip))) < 0)
1424                                 return err;
1425                 }
1426         }
1427         chip->master_sw_ctl = snd_ctl_new1(&tumbler_hp_sw, chip);
1428         if ((err = snd_ctl_add(chip->card, chip->master_sw_ctl)) < 0)
1429                 return err;
1430         chip->speaker_sw_ctl = snd_ctl_new1(&tumbler_speaker_sw, chip);
1431         if ((err = snd_ctl_add(chip->card, chip->speaker_sw_ctl)) < 0)
1432                 return err;
1433         if (mix->line_mute.addr != 0) {
1434                 chip->lineout_sw_ctl = snd_ctl_new1(&tumbler_lineout_sw, chip);
1435                 if ((err = snd_ctl_add(chip->card, chip->lineout_sw_ctl)) < 0)
1436                         return err;
1437         }
1438         chip->drc_sw_ctl = snd_ctl_new1(&tumbler_drc_sw, chip);
1439         if ((err = snd_ctl_add(chip->card, chip->drc_sw_ctl)) < 0)
1440                 return err;
1441
1442         /* set initial DRC range to 60% */
1443         if (chip->model == PMAC_TUMBLER)
1444                 mix->drc_range = (TAS3001_DRC_MAX * 6) / 10;
1445         else
1446                 mix->drc_range = (TAS3004_DRC_MAX * 6) / 10;
1447         mix->drc_enable = 1; /* will be changed later if AUTO_DRC is set */
1448         if (chip->model == PMAC_TUMBLER)
1449                 tumbler_set_drc(mix);
1450         else
1451                 snapper_set_drc(mix);
1452
1453 #ifdef CONFIG_PM
1454         chip->suspend = tumbler_suspend;
1455         chip->resume = tumbler_resume;
1456 #endif
1457
1458         INIT_WORK(&device_change, device_change_handler);
1459         device_change_chip = chip;
1460
1461 #ifdef PMAC_SUPPORT_AUTOMUTE
1462         if ((mix->headphone_irq >=0 || mix->lineout_irq >= 0)
1463             && (err = snd_pmac_add_automute(chip)) < 0)
1464                 return err;
1465         chip->detect_headphone = tumbler_detect_headphone;
1466         chip->update_automute = tumbler_update_automute;
1467         tumbler_update_automute(chip, 0); /* update the status only */
1468
1469         /* activate headphone status interrupts */
1470         if (mix->headphone_irq >= 0) {
1471                 unsigned char val;
1472                 if ((err = request_irq(mix->headphone_irq, headphone_intr, 0,
1473                                        "Sound Headphone Detection", chip)) < 0)
1474                         return 0;
1475                 /* activate headphone status interrupts */
1476                 val = do_gpio_read(&mix->hp_detect);
1477                 do_gpio_write(&mix->hp_detect, val | 0x80);
1478         }
1479         if (mix->lineout_irq >= 0) {
1480                 unsigned char val;
1481                 if ((err = request_irq(mix->lineout_irq, headphone_intr, 0,
1482                                        "Sound Lineout Detection", chip)) < 0)
1483                         return 0;
1484                 /* activate headphone status interrupts */
1485                 val = do_gpio_read(&mix->line_detect);
1486                 do_gpio_write(&mix->line_detect, val | 0x80);
1487         }
1488 #endif
1489
1490         return 0;
1491 }