patch-2_6_7-vs1_9_1_12
[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
21
22 #include <sound/driver.h>
23 #include <linux/init.h>
24 #include <linux/delay.h>
25 #include <linux/i2c.h>
26 #include <linux/i2c-dev.h>
27 #include <linux/kmod.h>
28 #include <linux/slab.h>
29 #include <linux/interrupt.h>
30 #include <sound/core.h>
31 #include <asm/io.h>
32 #include <asm/irq.h>
33 #ifdef CONFIG_PPC_HAS_FEATURE_CALLS
34 #include <asm/pmac_feature.h>
35 #endif
36 #include "pmac.h"
37 #include "tumbler_volume.h"
38
39 #define chip_t pmac_t
40
41 /* i2c address for tumbler */
42 #define TAS_I2C_ADDR    0x34
43
44 /* registers */
45 #define TAS_REG_MCS     0x01    /* main control */
46 #define TAS_REG_DRC     0x02
47 #define TAS_REG_VOL     0x04
48 #define TAS_REG_TREBLE  0x05
49 #define TAS_REG_BASS    0x06
50 #define TAS_REG_INPUT1  0x07
51 #define TAS_REG_INPUT2  0x08
52
53 /* tas3001c */
54 #define TAS_REG_PCM     TAS_REG_INPUT1
55  
56 /* tas3004 */
57 #define TAS_REG_LMIX    TAS_REG_INPUT1
58 #define TAS_REG_RMIX    TAS_REG_INPUT2
59 #define TAS_REG_MCS2    0x43            /* main control 2 */
60 #define TAS_REG_ACS     0x40            /* analog control */
61
62 /* mono volumes for tas3001c/tas3004 */
63 enum {
64         VOL_IDX_PCM_MONO, /* tas3001c only */
65         VOL_IDX_BASS, VOL_IDX_TREBLE,
66         VOL_IDX_LAST_MONO
67 };
68
69 /* stereo volumes for tas3004 */
70 enum {
71         VOL_IDX_PCM, VOL_IDX_PCM2, VOL_IDX_ADC,
72         VOL_IDX_LAST_MIX
73 };
74
75 typedef struct pmac_gpio {
76 #ifdef CONFIG_PPC_HAS_FEATURE_CALLS
77         unsigned int addr;
78 #else
79         void *addr;
80 #endif
81         int active_state;
82 } pmac_gpio_t;
83
84 typedef struct pmac_tumbler_t {
85         pmac_keywest_t i2c;
86         pmac_gpio_t audio_reset;
87         pmac_gpio_t amp_mute;
88         pmac_gpio_t hp_mute;
89         pmac_gpio_t hp_detect;
90         int headphone_irq;
91         unsigned int master_vol[2];
92         unsigned int master_switch[2];
93         unsigned int mono_vol[VOL_IDX_LAST_MONO];
94         unsigned int mix_vol[VOL_IDX_LAST_MIX][2]; /* stereo volumes for tas3004 */
95         int drc_range;
96         int drc_enable;
97         int capture_source;
98 } pmac_tumbler_t;
99
100
101 /*
102  */
103
104 static int send_init_client(pmac_keywest_t *i2c, unsigned int *regs)
105 {
106         while (*regs > 0) {
107                 int err, count = 10;
108                 do {
109                         err =  snd_pmac_keywest_write_byte(i2c, regs[0], regs[1]);
110                         if (err >= 0)
111                                 break;
112                         mdelay(10);
113                 } while (count--);
114                 if (err < 0)
115                         return -ENXIO;
116                 regs += 2;
117         }
118         return 0;
119 }
120
121
122 static int tumbler_init_client(pmac_keywest_t *i2c)
123 {
124         static unsigned int regs[] = {
125                 /* normal operation, SCLK=64fps, i2s output, i2s input, 16bit width */
126                 TAS_REG_MCS, (1<<6)|(2<<4)|(2<<2)|0,
127                 0, /* terminator */
128         };
129         return send_init_client(i2c, regs);
130 }
131
132 static int snapper_init_client(pmac_keywest_t *i2c)
133 {
134         static unsigned int regs[] = {
135                 /* normal operation, SCLK=64fps, i2s output, 16bit width */
136                 TAS_REG_MCS, (1<<6)|(2<<4)|0,
137                 /* normal operation, all-pass mode */
138                 TAS_REG_MCS2, (1<<1),
139                 /* normal output, no deemphasis, A input, power-up, line-in */
140                 TAS_REG_ACS, 0,
141                 0, /* terminator */
142         };
143         return send_init_client(i2c, regs);
144 }
145         
146 /*
147  * gpio access
148  */
149 #ifdef CONFIG_PPC_HAS_FEATURE_CALLS
150 #define do_gpio_write(gp, val) \
151         pmac_call_feature(PMAC_FTR_WRITE_GPIO, NULL, (gp)->addr, val)
152 #define do_gpio_read(gp) \
153         pmac_call_feature(PMAC_FTR_READ_GPIO, NULL, (gp)->addr, 0)
154 #define tumbler_gpio_free(gp) /* NOP */
155 #else
156 #define do_gpio_write(gp, val)  writeb(val, (gp)->addr)
157 #define do_gpio_read(gp)        readb((gp)->addr)
158 static inline void tumbler_gpio_free(pmac_gpio_t *gp)
159 {
160         if (gp->addr) {
161                 iounmap(gp->addr);
162                 gp->addr = 0;
163         }
164 }
165 #endif /* CONFIG_PPC_HAS_FEATURE_CALLS */
166
167 static void write_audio_gpio(pmac_gpio_t *gp, int active)
168 {
169         if (! gp->addr)
170                 return;
171         active = active ? gp->active_state : !gp->active_state;
172         do_gpio_write(gp, active ? 0x05 : 0x04);
173 }
174
175 static int read_audio_gpio(pmac_gpio_t *gp)
176 {
177         int ret;
178         if (! gp->addr)
179                 return 0;
180         ret = ((do_gpio_read(gp) & 0x02) !=0);
181         return ret == gp->active_state;
182 }
183
184 /*
185  * update master volume
186  */
187 static int tumbler_set_master_volume(pmac_tumbler_t *mix)
188 {
189         unsigned char block[6];
190         unsigned int left_vol, right_vol;
191   
192         if (! mix->i2c.client)
193                 return -ENODEV;
194   
195         if (! mix->master_switch[0])
196                 left_vol = 0;
197         else {
198                 left_vol = mix->master_vol[0];
199                 if (left_vol >= ARRAY_SIZE(master_volume_table))
200                         left_vol = ARRAY_SIZE(master_volume_table) - 1;
201                 left_vol = master_volume_table[left_vol];
202         }
203         if (! mix->master_switch[1])
204                 right_vol = 0;
205         else {
206                 right_vol = mix->master_vol[1];
207                 if (right_vol >= ARRAY_SIZE(master_volume_table))
208                         right_vol = ARRAY_SIZE(master_volume_table) - 1;
209                 right_vol = master_volume_table[right_vol];
210         }
211
212         block[0] = (left_vol >> 16) & 0xff;
213         block[1] = (left_vol >> 8)  & 0xff;
214         block[2] = (left_vol >> 0)  & 0xff;
215
216         block[3] = (right_vol >> 16) & 0xff;
217         block[4] = (right_vol >> 8)  & 0xff;
218         block[5] = (right_vol >> 0)  & 0xff;
219   
220         if (snd_pmac_keywest_write(&mix->i2c, TAS_REG_VOL, 6, block) < 0) {
221                 snd_printk("failed to set volume \n");  
222                 return -EINVAL; 
223         }
224         return 0;
225 }
226
227
228 /* output volume */
229 static int tumbler_info_master_volume(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
230 {
231         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
232         uinfo->count = 2;
233         uinfo->value.integer.min = 0;
234         uinfo->value.integer.max = ARRAY_SIZE(master_volume_table) - 1;
235         return 0;
236 }
237
238 static int tumbler_get_master_volume(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
239 {
240         pmac_t *chip = snd_kcontrol_chip(kcontrol);
241         pmac_tumbler_t *mix = chip->mixer_data;
242         snd_assert(mix, return -ENODEV);
243         ucontrol->value.integer.value[0] = mix->master_vol[0];
244         ucontrol->value.integer.value[1] = mix->master_vol[1];
245         return 0;
246 }
247
248 static int tumbler_put_master_volume(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
249 {
250         pmac_t *chip = snd_kcontrol_chip(kcontrol);
251         pmac_tumbler_t *mix = chip->mixer_data;
252         int change;
253
254         snd_assert(mix, return -ENODEV);
255         change = mix->master_vol[0] != ucontrol->value.integer.value[0] ||
256                 mix->master_vol[1] != ucontrol->value.integer.value[1];
257         if (change) {
258                 mix->master_vol[0] = ucontrol->value.integer.value[0];
259                 mix->master_vol[1] = ucontrol->value.integer.value[1];
260                 tumbler_set_master_volume(mix);
261         }
262         return change;
263 }
264
265 /* output switch */
266 static int tumbler_get_master_switch(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
267 {
268         pmac_t *chip = snd_kcontrol_chip(kcontrol);
269         pmac_tumbler_t *mix = chip->mixer_data;
270         snd_assert(mix, return -ENODEV);
271         ucontrol->value.integer.value[0] = mix->master_switch[0];
272         ucontrol->value.integer.value[1] = mix->master_switch[1];
273         return 0;
274 }
275
276 static int tumbler_put_master_switch(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
277 {
278         pmac_t *chip = snd_kcontrol_chip(kcontrol);
279         pmac_tumbler_t *mix = chip->mixer_data;
280         int change;
281
282         snd_assert(mix, return -ENODEV);
283         change = mix->master_switch[0] != ucontrol->value.integer.value[0] ||
284                 mix->master_switch[1] != ucontrol->value.integer.value[1];
285         if (change) {
286                 mix->master_switch[0] = !!ucontrol->value.integer.value[0];
287                 mix->master_switch[1] = !!ucontrol->value.integer.value[1];
288                 tumbler_set_master_volume(mix);
289         }
290         return change;
291 }
292
293
294 /*
295  * TAS3001c dynamic range compression
296  */
297
298 #define TAS3001_DRC_MAX         0x5f
299
300 static int tumbler_set_drc(pmac_tumbler_t *mix)
301 {
302         unsigned char val[2];
303
304         if (! mix->i2c.client)
305                 return -ENODEV;
306   
307         if (mix->drc_enable) {
308                 val[0] = 0xc1; /* enable, 3:1 compression */
309                 if (mix->drc_range > TAS3001_DRC_MAX)
310                         val[1] = 0xf0;
311                 else if (mix->drc_range < 0)
312                         val[1] = 0x91;
313                 else
314                         val[1] = mix->drc_range + 0x91;
315         } else {
316                 val[0] = 0;
317                 val[1] = 0;
318         }
319
320         if (snd_pmac_keywest_write(&mix->i2c, TAS_REG_DRC, 2, val) < 0) {
321                 snd_printk("failed to set DRC\n");  
322                 return -EINVAL; 
323         }
324         return 0;
325 }
326
327 /*
328  * TAS3004
329  */
330
331 #define TAS3004_DRC_MAX         0xef
332
333 static int snapper_set_drc(pmac_tumbler_t *mix)
334 {
335         unsigned char val[6];
336
337         if (! mix->i2c.client)
338                 return -ENODEV;
339   
340         if (mix->drc_enable)
341                 val[0] = 0x50; /* 3:1 above threshold */
342         else
343                 val[0] = 0x51; /* disabled */
344         val[1] = 0x02; /* 1:1 below threshold */
345         if (mix->drc_range > 0xef)
346                 val[2] = 0xef;
347         else if (mix->drc_range < 0)
348                 val[2] = 0x00;
349         else
350                 val[2] = mix->drc_range;
351         val[3] = 0xb0;
352         val[4] = 0x60;
353         val[5] = 0xa0;
354
355         if (snd_pmac_keywest_write(&mix->i2c, TAS_REG_DRC, 6, val) < 0) {
356                 snd_printk("failed to set DRC\n");  
357                 return -EINVAL; 
358         }
359         return 0;
360 }
361
362 static int tumbler_info_drc_value(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
363 {
364         pmac_t *chip = snd_kcontrol_chip(kcontrol);
365         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
366         uinfo->count = 1;
367         uinfo->value.integer.min = 0;
368         uinfo->value.integer.max =
369                 chip->model == PMAC_TUMBLER ? TAS3001_DRC_MAX : TAS3004_DRC_MAX;
370         return 0;
371 }
372
373 static int tumbler_get_drc_value(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
374 {
375         pmac_t *chip = snd_kcontrol_chip(kcontrol);
376         pmac_tumbler_t *mix;
377         if (! (mix = chip->mixer_data))
378                 return -ENODEV;
379         ucontrol->value.integer.value[0] = mix->drc_range;
380         return 0;
381 }
382
383 static int tumbler_put_drc_value(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
384 {
385         pmac_t *chip = snd_kcontrol_chip(kcontrol);
386         pmac_tumbler_t *mix;
387         int change;
388
389         if (! (mix = chip->mixer_data))
390                 return -ENODEV;
391         change = mix->drc_range != ucontrol->value.integer.value[0];
392         if (change) {
393                 mix->drc_range = ucontrol->value.integer.value[0];
394                 if (chip->model == PMAC_TUMBLER)
395                         tumbler_set_drc(mix);
396                 else
397                         snapper_set_drc(mix);
398         }
399         return change;
400 }
401
402 static int tumbler_get_drc_switch(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
403 {
404         pmac_t *chip = snd_kcontrol_chip(kcontrol);
405         pmac_tumbler_t *mix;
406         if (! (mix = chip->mixer_data))
407                 return -ENODEV;
408         ucontrol->value.integer.value[0] = mix->drc_enable;
409         return 0;
410 }
411
412 static int tumbler_put_drc_switch(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
413 {
414         pmac_t *chip = snd_kcontrol_chip(kcontrol);
415         pmac_tumbler_t *mix;
416         int change;
417
418         if (! (mix = chip->mixer_data))
419                 return -ENODEV;
420         change = mix->drc_enable != ucontrol->value.integer.value[0];
421         if (change) {
422                 mix->drc_enable = !!ucontrol->value.integer.value[0];
423                 if (chip->model == PMAC_TUMBLER)
424                         tumbler_set_drc(mix);
425                 else
426                         snapper_set_drc(mix);
427         }
428         return change;
429 }
430
431
432 /*
433  * mono volumes
434  */
435
436 struct tumbler_mono_vol {
437         int index;
438         int reg;
439         int bytes;
440         unsigned int max;
441         unsigned int *table;
442 };
443
444 static int tumbler_set_mono_volume(pmac_tumbler_t *mix, struct tumbler_mono_vol *info)
445 {
446         unsigned char block[4];
447         unsigned int vol;
448         int i;
449   
450         if (! mix->i2c.client)
451                 return -ENODEV;
452   
453         vol = mix->mono_vol[info->index];
454         if (vol >= info->max)
455                 vol = info->max - 1;
456         vol = info->table[vol];
457         for (i = 0; i < info->bytes; i++)
458                 block[i] = (vol >> ((info->bytes - i - 1) * 8)) & 0xff;
459         if (snd_pmac_keywest_write(&mix->i2c, info->reg, info->bytes, block) < 0) {
460                 snd_printk("failed to set mono volume %d\n", info->index);  
461                 return -EINVAL; 
462         }
463         return 0;
464 }
465
466 static int tumbler_info_mono(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
467 {
468         struct tumbler_mono_vol *info = (struct tumbler_mono_vol *)kcontrol->private_value;
469
470         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
471         uinfo->count = 1;
472         uinfo->value.integer.min = 0;
473         uinfo->value.integer.max = info->max - 1;
474         return 0;
475 }
476
477 static int tumbler_get_mono(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
478 {
479         struct tumbler_mono_vol *info = (struct tumbler_mono_vol *)kcontrol->private_value;
480         pmac_t *chip = snd_kcontrol_chip(kcontrol);
481         pmac_tumbler_t *mix;
482         if (! (mix = chip->mixer_data))
483                 return -ENODEV;
484         ucontrol->value.integer.value[0] = mix->mono_vol[info->index];
485         return 0;
486 }
487
488 static int tumbler_put_mono(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
489 {
490         struct tumbler_mono_vol *info = (struct tumbler_mono_vol *)kcontrol->private_value;
491         pmac_t *chip = snd_kcontrol_chip(kcontrol);
492         pmac_tumbler_t *mix;
493         int change;
494
495         if (! (mix = chip->mixer_data))
496                 return -ENODEV;
497         change = mix->mono_vol[info->index] != ucontrol->value.integer.value[0];
498         if (change) {
499                 mix->mono_vol[info->index] = ucontrol->value.integer.value[0];
500                 tumbler_set_mono_volume(mix, info);
501         }
502         return change;
503 }
504
505 /* TAS3001c mono volumes */
506 static struct tumbler_mono_vol tumbler_pcm_vol_info = {
507         .index = VOL_IDX_PCM_MONO,
508         .reg = TAS_REG_PCM,
509         .bytes = 3,
510         .max = ARRAY_SIZE(mixer_volume_table),
511         .table = mixer_volume_table,
512 };
513
514 static struct tumbler_mono_vol tumbler_bass_vol_info = {
515         .index = VOL_IDX_BASS,
516         .reg = TAS_REG_BASS,
517         .bytes = 1,
518         .max = ARRAY_SIZE(bass_volume_table),
519         .table = bass_volume_table,
520 };
521
522 static struct tumbler_mono_vol tumbler_treble_vol_info = {
523         .index = VOL_IDX_TREBLE,
524         .reg = TAS_REG_TREBLE,
525         .bytes = 1,
526         .max = ARRAY_SIZE(treble_volume_table),
527         .table = treble_volume_table,
528 };
529
530 /* TAS3004 mono volumes */
531 static struct tumbler_mono_vol snapper_bass_vol_info = {
532         .index = VOL_IDX_BASS,
533         .reg = TAS_REG_BASS,
534         .bytes = 1,
535         .max = ARRAY_SIZE(snapper_bass_volume_table),
536         .table = snapper_bass_volume_table,
537 };
538
539 static struct tumbler_mono_vol snapper_treble_vol_info = {
540         .index = VOL_IDX_TREBLE,
541         .reg = TAS_REG_TREBLE,
542         .bytes = 1,
543         .max = ARRAY_SIZE(snapper_treble_volume_table),
544         .table = snapper_treble_volume_table,
545 };
546
547
548 #define DEFINE_MONO(xname,type) { \
549         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,\
550         .name = xname, \
551         .info = tumbler_info_mono, \
552         .get = tumbler_get_mono, \
553         .put = tumbler_put_mono, \
554         .private_value = (unsigned long)(&tumbler_##type##_vol_info), \
555 }
556
557 #define DEFINE_SNAPPER_MONO(xname,type) { \
558         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,\
559         .name = xname, \
560         .info = tumbler_info_mono, \
561         .get = tumbler_get_mono, \
562         .put = tumbler_put_mono, \
563         .private_value = (unsigned long)(&snapper_##type##_vol_info), \
564 }
565
566
567 /*
568  * snapper mixer volumes
569  */
570
571 static int snapper_set_mix_vol1(pmac_tumbler_t *mix, int idx, int ch, int reg)
572 {
573         int i, j, vol;
574         unsigned char block[9];
575
576         vol = mix->mix_vol[idx][ch];
577         if (vol >= ARRAY_SIZE(mixer_volume_table)) {
578                 vol = ARRAY_SIZE(mixer_volume_table) - 1;
579                 mix->mix_vol[idx][ch] = vol;
580         }
581
582         for (i = 0; i < 3; i++) {
583                 vol = mix->mix_vol[i][ch];
584                 vol = mixer_volume_table[vol];
585                 for (j = 0; j < 3; j++)
586                         block[i * 3 + j] = (vol >> ((2 - j) * 8)) & 0xff;
587         }
588         if (snd_pmac_keywest_write(&mix->i2c, reg, 9, block) < 0) {
589                 snd_printk("failed to set mono volume %d\n", reg);  
590                 return -EINVAL; 
591         }
592         return 0;
593 }
594
595 static int snapper_set_mix_vol(pmac_tumbler_t *mix, int idx)
596 {
597         if (! mix->i2c.client)
598                 return -ENODEV;
599         if (snapper_set_mix_vol1(mix, idx, 0, TAS_REG_LMIX) < 0 ||
600             snapper_set_mix_vol1(mix, idx, 1, TAS_REG_RMIX) < 0)
601                 return -EINVAL;
602         return 0;
603 }
604
605 static int snapper_info_mix(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
606 {
607         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
608         uinfo->count = 2;
609         uinfo->value.integer.min = 0;
610         uinfo->value.integer.max = ARRAY_SIZE(mixer_volume_table) - 1;
611         return 0;
612 }
613
614 static int snapper_get_mix(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
615 {
616         int idx = (int)kcontrol->private_value;
617         pmac_t *chip = snd_kcontrol_chip(kcontrol);
618         pmac_tumbler_t *mix;
619         if (! (mix = chip->mixer_data))
620                 return -ENODEV;
621         ucontrol->value.integer.value[0] = mix->mix_vol[idx][0];
622         ucontrol->value.integer.value[1] = mix->mix_vol[idx][1];
623         return 0;
624 }
625
626 static int snapper_put_mix(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
627 {
628         int idx = (int)kcontrol->private_value;
629         pmac_t *chip = snd_kcontrol_chip(kcontrol);
630         pmac_tumbler_t *mix;
631         int change;
632
633         if (! (mix = chip->mixer_data))
634                 return -ENODEV;
635         change = mix->mix_vol[idx][0] != ucontrol->value.integer.value[0] ||
636                 mix->mix_vol[idx][1] != ucontrol->value.integer.value[1];
637         if (change) {
638                 mix->mix_vol[idx][0] = ucontrol->value.integer.value[0];
639                 mix->mix_vol[idx][1] = ucontrol->value.integer.value[1];
640                 snapper_set_mix_vol(mix, idx);
641         }
642         return change;
643 }
644
645
646 /*
647  * mute switches
648  */
649
650 enum { TUMBLER_MUTE_HP, TUMBLER_MUTE_AMP };
651
652 static int tumbler_get_mute_switch(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
653 {
654         pmac_t *chip = snd_kcontrol_chip(kcontrol);
655         pmac_tumbler_t *mix;
656         pmac_gpio_t *gp;
657         if (! (mix = chip->mixer_data))
658                 return -ENODEV;
659         gp = (kcontrol->private_value == TUMBLER_MUTE_HP) ? &mix->hp_mute : &mix->amp_mute;
660         ucontrol->value.integer.value[0] = ! read_audio_gpio(gp);
661         return 0;
662 }
663
664 static int tumbler_put_mute_switch(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
665 {
666         pmac_t *chip = snd_kcontrol_chip(kcontrol);
667         pmac_tumbler_t *mix;
668         pmac_gpio_t *gp;
669         int val;
670 #ifdef PMAC_SUPPORT_AUTOMUTE
671         if (chip->update_automute && chip->auto_mute)
672                 return 0; /* don't touch in the auto-mute mode */
673 #endif  
674         if (! (mix = chip->mixer_data))
675                 return -ENODEV;
676         gp = (kcontrol->private_value == TUMBLER_MUTE_HP) ? &mix->hp_mute : &mix->amp_mute;
677         val = ! read_audio_gpio(gp);
678         if (val != ucontrol->value.integer.value[0]) {
679                 write_audio_gpio(gp, ! ucontrol->value.integer.value[0]);
680                 return 1;
681         }
682         return 0;
683 }
684
685 static int snapper_set_capture_source(pmac_tumbler_t *mix)
686 {
687         if (! mix->i2c.client)
688                 return -ENODEV;
689         return snd_pmac_keywest_write_byte(&mix->i2c, TAS_REG_ACS,
690                                            mix->capture_source ? 2 : 0);
691 }
692
693 static int snapper_info_capture_source(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
694 {
695         static char *texts[2] = {
696                 "Line", "Mic"
697         };
698         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
699         uinfo->count = 1;
700         uinfo->value.enumerated.items = 2;
701         if (uinfo->value.enumerated.item > 1)
702                 uinfo->value.enumerated.item = 1;
703         strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
704         return 0;
705 }
706
707 static int snapper_get_capture_source(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
708 {
709         pmac_t *chip = snd_kcontrol_chip(kcontrol);
710         pmac_tumbler_t *mix = chip->mixer_data;
711
712         snd_assert(mix, return -ENODEV);
713         ucontrol->value.integer.value[0] = mix->capture_source;
714         return 0;
715 }
716
717 static int snapper_put_capture_source(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
718 {
719         pmac_t *chip = snd_kcontrol_chip(kcontrol);
720         pmac_tumbler_t *mix = chip->mixer_data;
721         int change;
722
723         snd_assert(mix, return -ENODEV);
724         change = ucontrol->value.integer.value[0] != mix->capture_source;
725         if (change) {
726                 mix->capture_source = !!ucontrol->value.integer.value[0];
727                 snapper_set_capture_source(mix);
728         }
729         return change;
730 }
731
732 #define DEFINE_SNAPPER_MIX(xname,idx,ofs) { \
733         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,\
734         .name = xname, \
735         .info = snapper_info_mix, \
736         .get = snapper_get_mix, \
737         .put = snapper_put_mix, \
738         .index = idx,\
739         .private_value = ofs, \
740 }
741
742
743 /*
744  */
745 static snd_kcontrol_new_t tumbler_mixers[] __initdata = {
746         { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
747           .name = "Master Playback Volume",
748           .info = tumbler_info_master_volume,
749           .get = tumbler_get_master_volume,
750           .put = tumbler_put_master_volume
751         },
752         { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
753           .name = "Master Playback Switch",
754           .info = snd_pmac_boolean_stereo_info,
755           .get = tumbler_get_master_switch,
756           .put = tumbler_put_master_switch
757         },
758         DEFINE_MONO("Tone Control - Bass", bass),
759         DEFINE_MONO("Tone Control - Treble", treble),
760         DEFINE_MONO("PCM Playback Volume", pcm),
761         { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
762           .name = "DRC Switch",
763           .info = snd_pmac_boolean_mono_info,
764           .get = tumbler_get_drc_switch,
765           .put = tumbler_put_drc_switch
766         },
767         { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
768           .name = "DRC Range",
769           .info = tumbler_info_drc_value,
770           .get = tumbler_get_drc_value,
771           .put = tumbler_put_drc_value
772         },
773 };
774
775 static snd_kcontrol_new_t snapper_mixers[] __initdata = {
776         { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
777           .name = "Master Playback Volume",
778           .info = tumbler_info_master_volume,
779           .get = tumbler_get_master_volume,
780           .put = tumbler_put_master_volume
781         },
782         { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
783           .name = "Master Playback Switch",
784           .info = snd_pmac_boolean_stereo_info,
785           .get = tumbler_get_master_switch,
786           .put = tumbler_put_master_switch
787         },
788         DEFINE_SNAPPER_MIX("PCM Playback Volume", 0, VOL_IDX_PCM),
789         DEFINE_SNAPPER_MIX("PCM Playback Volume", 1, VOL_IDX_PCM2),
790         DEFINE_SNAPPER_MIX("Monitor Mix Volume", 0, VOL_IDX_ADC),
791         DEFINE_SNAPPER_MONO("Tone Control - Bass", bass),
792         DEFINE_SNAPPER_MONO("Tone Control - Treble", treble),
793         { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
794           .name = "DRC Switch",
795           .info = snd_pmac_boolean_mono_info,
796           .get = tumbler_get_drc_switch,
797           .put = tumbler_put_drc_switch
798         },
799         { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
800           .name = "DRC Range",
801           .info = tumbler_info_drc_value,
802           .get = tumbler_get_drc_value,
803           .put = tumbler_put_drc_value
804         },
805         { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
806           .name = "Input Source", /* FIXME: "Capture Source" doesn't work properly */
807           .info = snapper_info_capture_source,
808           .get = snapper_get_capture_source,
809           .put = snapper_put_capture_source
810         },
811 };
812
813 static snd_kcontrol_new_t tumbler_hp_sw __initdata = {
814         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
815         .name = "Headphone Playback Switch",
816         .info = snd_pmac_boolean_mono_info,
817         .get = tumbler_get_mute_switch,
818         .put = tumbler_put_mute_switch,
819         .private_value = TUMBLER_MUTE_HP,
820 };
821 static snd_kcontrol_new_t tumbler_speaker_sw __initdata = {
822         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
823         .name = "PC Speaker Playback Switch",
824         .info = snd_pmac_boolean_mono_info,
825         .get = tumbler_get_mute_switch,
826         .put = tumbler_put_mute_switch,
827         .private_value = TUMBLER_MUTE_AMP,
828 };
829
830 #ifdef PMAC_SUPPORT_AUTOMUTE
831 /*
832  * auto-mute stuffs
833  */
834 static int tumbler_detect_headphone(pmac_t *chip)
835 {
836         pmac_tumbler_t *mix = chip->mixer_data;
837         return read_audio_gpio(&mix->hp_detect);
838 }
839
840 static void check_mute(pmac_t *chip, pmac_gpio_t *gp, int val, int do_notify, snd_kcontrol_t *sw)
841 {
842         //pmac_tumbler_t *mix = chip->mixer_data;
843         if (val != read_audio_gpio(gp)) {
844                 write_audio_gpio(gp, val);
845                 if (do_notify)
846                         snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE, &sw->id);
847         }
848 }
849
850 static void tumbler_update_automute(pmac_t *chip, int do_notify)
851 {
852         if (chip->auto_mute) {
853                 pmac_tumbler_t *mix = chip->mixer_data;
854                 snd_assert(mix, return);
855                 if (tumbler_detect_headphone(chip)) {
856                         /* mute speaker */
857                         check_mute(chip, &mix->amp_mute, 1, do_notify, chip->speaker_sw_ctl);
858                         check_mute(chip, &mix->hp_mute, 0, do_notify, chip->master_sw_ctl);
859                 } else {
860                         /* unmute speaker */
861                         check_mute(chip, &mix->amp_mute, 0, do_notify, chip->speaker_sw_ctl);
862                         check_mute(chip, &mix->hp_mute, 1, do_notify, chip->master_sw_ctl);
863                 }
864                 if (do_notify)
865                         snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
866                                        &chip->hp_detect_ctl->id);
867         }
868 }
869 #endif /* PMAC_SUPPORT_AUTOMUTE */
870
871
872 /* interrupt - headphone plug changed */
873 static irqreturn_t headphone_intr(int irq, void *devid, struct pt_regs *regs)
874 {
875         pmac_t *chip = snd_magic_cast(pmac_t, devid, return IRQ_NONE);
876         if (chip->update_automute && chip->initialized) {
877                 chip->update_automute(chip, 1);
878                 return IRQ_HANDLED;
879         }
880         return IRQ_NONE;
881 }
882
883 /* look for audio-gpio device */
884 static struct device_node *find_audio_device(const char *name)
885 {
886         struct device_node *np;
887   
888         if (! (np = find_devices("gpio")))
889                 return NULL;
890   
891         for (np = np->child; np; np = np->sibling) {
892                 char *property = get_property(np, "audio-gpio", NULL);
893                 if (property && strcmp(property, name) == 0)
894                         return np;
895         }  
896         return NULL;
897 }
898
899 /* look for audio-gpio device */
900 static struct device_node *find_compatible_audio_device(const char *name)
901 {
902         struct device_node *np;
903   
904         if (! (np = find_devices("gpio")))
905                 return NULL;
906   
907         for (np = np->child; np; np = np->sibling) {
908                 if (device_is_compatible(np, name))
909                         return np;
910         }  
911         return NULL;
912 }
913
914 /* find an audio device and get its address */
915 static unsigned long tumbler_find_device(const char *device, pmac_gpio_t *gp, int is_compatible)
916 {
917         struct device_node *node;
918         u32 *base;
919
920         if (is_compatible)
921                 node = find_compatible_audio_device(device);
922         else
923                 node = find_audio_device(device);
924         if (! node) {
925                 snd_printdd("cannot find device %s\n", device);
926                 return -ENODEV;
927         }
928
929         base = (u32 *)get_property(node, "AAPL,address", NULL);
930         if (! base) {
931                 snd_printd("cannot find address for device %s\n", device);
932                 return -ENODEV;
933         }
934
935 #ifdef CONFIG_PPC_HAS_FEATURE_CALLS
936         gp->addr = (*base) & 0x0000ffff;
937 #else
938         gp->addr = (void*)ioremap((unsigned long)(*base), 1);
939 #endif
940         base = (u32 *)get_property(node, "audio-gpio-active-state", NULL);
941         if (base)
942                 gp->active_state = *base;
943         else
944                 gp->active_state = 1;
945
946
947         return (node->n_intrs > 0) ? node->intrs[0].line : 0;
948 }
949
950 /* reset audio */
951 static void tumbler_reset_audio(pmac_t *chip)
952 {
953         pmac_tumbler_t *mix = chip->mixer_data;
954
955         write_audio_gpio(&mix->audio_reset, 0);
956         big_mdelay(200);
957         write_audio_gpio(&mix->audio_reset, 1);
958         big_mdelay(100);
959         write_audio_gpio(&mix->audio_reset, 0);
960         big_mdelay(100);
961 }
962
963 #ifdef CONFIG_PMAC_PBOOK
964 /* resume mixer */
965 static void tumbler_resume(pmac_t *chip)
966 {
967         pmac_tumbler_t *mix = chip->mixer_data;
968
969         snd_assert(mix, return);
970
971         tumbler_reset_audio(chip);
972         if (mix->i2c.client && mix->i2c.init_client) {
973                 if (mix->i2c.init_client(&mix->i2c) < 0)
974                         printk(KERN_ERR "tumbler_init_client error\n");
975         } else
976                 printk(KERN_ERR "tumbler: i2c is not initialized\n");
977         if (chip->model == PMAC_TUMBLER) {
978                 tumbler_set_mono_volume(mix, &tumbler_pcm_vol_info);
979                 tumbler_set_mono_volume(mix, &tumbler_bass_vol_info);
980                 tumbler_set_mono_volume(mix, &tumbler_treble_vol_info);
981                 tumbler_set_drc(mix);
982         } else {
983                 snapper_set_mix_vol(mix, VOL_IDX_PCM);
984                 snapper_set_mix_vol(mix, VOL_IDX_PCM2);
985                 snapper_set_mix_vol(mix, VOL_IDX_ADC);
986                 tumbler_set_mono_volume(mix, &snapper_bass_vol_info);
987                 tumbler_set_mono_volume(mix, &snapper_treble_vol_info);
988                 snapper_set_drc(mix);
989                 snapper_set_capture_source(mix);
990         }
991         tumbler_set_master_volume(mix);
992         if (chip->update_automute)
993                 chip->update_automute(chip, 0);
994 }
995 #endif
996
997 /* initialize tumbler */
998 static int __init tumbler_init(pmac_t *chip)
999 {
1000         int irq, err;
1001         pmac_tumbler_t *mix = chip->mixer_data;
1002         snd_assert(mix, return -EINVAL);
1003
1004         tumbler_find_device("audio-hw-reset", &mix->audio_reset, 0);
1005         tumbler_find_device("amp-mute", &mix->amp_mute, 0);
1006         tumbler_find_device("headphone-mute", &mix->hp_mute, 0);
1007         irq = tumbler_find_device("headphone-detect", &mix->hp_detect, 0);
1008         if (irq < 0)
1009                 irq = tumbler_find_device("keywest-gpio15", &mix->hp_detect, 1);
1010
1011         tumbler_reset_audio(chip);
1012
1013         /* activate headphone status interrupts */
1014         if (irq >= 0) {
1015                 unsigned char val;
1016                 if ((err = request_irq(irq, headphone_intr, 0,
1017                                        "Tumbler Headphone Detection", chip)) < 0)
1018                         return err;
1019                 /* activate headphone status interrupts */
1020                 val = do_gpio_read(&mix->hp_detect);
1021                 do_gpio_write(&mix->hp_detect, val | 0x80);
1022         }
1023         mix->headphone_irq = irq;
1024   
1025         return 0;
1026 }
1027
1028 static void tumbler_cleanup(pmac_t *chip)
1029 {
1030         pmac_tumbler_t *mix = chip->mixer_data;
1031         if (! mix)
1032                 return;
1033
1034         if (mix->headphone_irq >= 0)
1035                 free_irq(mix->headphone_irq, chip);
1036         tumbler_gpio_free(&mix->audio_reset);
1037         tumbler_gpio_free(&mix->amp_mute);
1038         tumbler_gpio_free(&mix->hp_mute);
1039         tumbler_gpio_free(&mix->hp_detect);
1040         snd_pmac_keywest_cleanup(&mix->i2c);
1041         kfree(mix);
1042         chip->mixer_data = NULL;
1043 }
1044
1045 /* exported */
1046 int __init snd_pmac_tumbler_init(pmac_t *chip)
1047 {
1048         int i, err;
1049         pmac_tumbler_t *mix;
1050         u32 *paddr;
1051         struct device_node *tas_node;
1052         char *chipname;
1053
1054 #ifdef CONFIG_KMOD
1055         if (current->fs->root)
1056                 request_module("i2c-keywest");
1057 #endif /* CONFIG_KMOD */        
1058
1059         mix = kmalloc(sizeof(*mix), GFP_KERNEL);
1060         if (! mix)
1061                 return -ENOMEM;
1062         memset(mix, 0, sizeof(*mix));
1063         mix->headphone_irq = -1;
1064
1065         chip->mixer_data = mix;
1066         chip->mixer_free = tumbler_cleanup;
1067
1068         if ((err = tumbler_init(chip)) < 0)
1069                 return err;
1070
1071         /* set up TAS */
1072         tas_node = find_devices("deq");
1073         if (tas_node == NULL)
1074                 return -ENODEV;
1075
1076         paddr = (u32 *)get_property(tas_node, "i2c-address", NULL);
1077         if (paddr)
1078                 mix->i2c.addr = (*paddr) >> 1;
1079         else
1080                 mix->i2c.addr = TAS_I2C_ADDR;
1081
1082         if (chip->model == PMAC_TUMBLER) {
1083                 mix->i2c.init_client = tumbler_init_client;
1084                 mix->i2c.name = "TAS3001c";
1085                 chipname = "Tumbler";
1086         } else {
1087                 mix->i2c.init_client = snapper_init_client;
1088                 mix->i2c.name = "TAS3004";
1089                 chipname = "Snapper";
1090         }
1091
1092         if ((err = snd_pmac_keywest_init(&mix->i2c)) < 0)
1093                 return err;
1094
1095         /*
1096          * build mixers
1097          */
1098         sprintf(chip->card->mixername, "PowerMac %s", chipname);
1099
1100         if (chip->model == PMAC_TUMBLER) {
1101                 for (i = 0; i < ARRAY_SIZE(tumbler_mixers); i++) {
1102                         if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&tumbler_mixers[i], chip))) < 0)
1103                                 return err;
1104                 }
1105         } else {
1106                 for (i = 0; i < ARRAY_SIZE(snapper_mixers); i++) {
1107                         if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snapper_mixers[i], chip))) < 0)
1108                                 return err;
1109                 }
1110         }
1111         chip->master_sw_ctl = snd_ctl_new1(&tumbler_hp_sw, chip);
1112         if ((err = snd_ctl_add(chip->card, chip->master_sw_ctl)) < 0)
1113                 return err;
1114         chip->speaker_sw_ctl = snd_ctl_new1(&tumbler_speaker_sw, chip);
1115         if ((err = snd_ctl_add(chip->card, chip->speaker_sw_ctl)) < 0)
1116                 return err;
1117
1118 #ifdef CONFIG_PMAC_PBOOK
1119         chip->resume = tumbler_resume;
1120 #endif
1121
1122 #ifdef PMAC_SUPPORT_AUTOMUTE
1123         if (mix->headphone_irq >=0 && (err = snd_pmac_add_automute(chip)) < 0)
1124                 return err;
1125         chip->detect_headphone = tumbler_detect_headphone;
1126         chip->update_automute = tumbler_update_automute;
1127         tumbler_update_automute(chip, 0); /* update the status only */
1128 #endif
1129
1130         return 0;
1131 }