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