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