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