[PATCH] ppc32: Small build fix for alsa powermac
[linux-2.6.git] / sound / ppc / toonie.c
1 /*
2  * Mac Mini "toonie" mixer control
3  *
4  * Copyright (c) 2005 by Benjamin Herrenschmidt <benh@kernel.crashing.org>
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 #include <sound/driver.h>
22 #include <linux/init.h>
23 #include <linux/delay.h>
24 #include <linux/i2c.h>
25 #include <linux/i2c-dev.h>
26 #include <linux/kmod.h>
27 #include <linux/slab.h>
28 #include <linux/interrupt.h>
29 #include <sound/core.h>
30 #include <asm/io.h>
31 #include <asm/irq.h>
32 #include <asm/machdep.h>
33 #include <asm/pmac_feature.h>
34 #include "pmac.h"
35
36 #undef DEBUG
37
38 #ifdef DEBUG
39 #define DBG(fmt...) printk(fmt)
40 #else
41 #define DBG(fmt...)
42 #endif
43
44 struct pmac_gpio {
45         unsigned int addr;
46         u8 active_val;
47         u8 inactive_val;
48         u8 active_state;
49 };
50
51 struct pmac_toonie
52 {
53         struct pmac_gpio        hp_detect_gpio;
54         struct pmac_gpio        hp_mute_gpio;
55         struct pmac_gpio        amp_mute_gpio;
56         int                     hp_detect_irq;
57         int                     auto_mute_notify;
58         struct work_struct      detect_work;
59 };
60
61
62 /*
63  * gpio access
64  */
65 #define do_gpio_write(gp, val) \
66         pmac_call_feature(PMAC_FTR_WRITE_GPIO, NULL, (gp)->addr, val)
67 #define do_gpio_read(gp) \
68         pmac_call_feature(PMAC_FTR_READ_GPIO, NULL, (gp)->addr, 0)
69 #define tumbler_gpio_free(gp) /* NOP */
70
71 static void write_audio_gpio(struct pmac_gpio *gp, int active)
72 {
73         if (! gp->addr)
74                 return;
75         active = active ? gp->active_val : gp->inactive_val;
76         do_gpio_write(gp, active);
77         DBG("(I) gpio %x write %d\n", gp->addr, active);
78 }
79
80 static int check_audio_gpio(struct pmac_gpio *gp)
81 {
82         int ret;
83
84         if (! gp->addr)
85                 return 0;
86
87         ret = do_gpio_read(gp);
88
89         return (ret & 0xd) == (gp->active_val & 0xd);
90 }
91
92 static int read_audio_gpio(struct pmac_gpio *gp)
93 {
94         int ret;
95         if (! gp->addr)
96                 return 0;
97         ret = ((do_gpio_read(gp) & 0x02) !=0);
98         return ret == gp->active_state;
99 }
100
101
102 enum { TOONIE_MUTE_HP, TOONIE_MUTE_AMP };
103
104 static int toonie_get_mute_switch(snd_kcontrol_t *kcontrol,
105                                   snd_ctl_elem_value_t *ucontrol)
106 {
107         pmac_t *chip = snd_kcontrol_chip(kcontrol);
108         struct pmac_toonie *mix = chip->mixer_data;
109         struct pmac_gpio *gp;
110
111         if (mix == NULL)
112                 return -ENODEV;
113         switch(kcontrol->private_value) {
114         case TOONIE_MUTE_HP:
115                 gp = &mix->hp_mute_gpio;
116                 break;
117         case TOONIE_MUTE_AMP:
118                 gp = &mix->amp_mute_gpio;
119                 break;
120         default:
121                 return -EINVAL;;
122         }
123         ucontrol->value.integer.value[0] = !check_audio_gpio(gp);
124         return 0;
125 }
126
127 static int toonie_put_mute_switch(snd_kcontrol_t *kcontrol,
128                                    snd_ctl_elem_value_t *ucontrol)
129 {
130         pmac_t *chip = snd_kcontrol_chip(kcontrol);
131         struct pmac_toonie *mix = chip->mixer_data;
132         struct pmac_gpio *gp;
133         int val;
134
135         if (chip->update_automute && chip->auto_mute)
136                 return 0; /* don't touch in the auto-mute mode */
137
138         if (mix == NULL)
139                 return -ENODEV;
140
141         switch(kcontrol->private_value) {
142         case TOONIE_MUTE_HP:
143                 gp = &mix->hp_mute_gpio;
144                 break;
145         case TOONIE_MUTE_AMP:
146                 gp = &mix->amp_mute_gpio;
147                 break;
148         default:
149                 return -EINVAL;;
150         }
151         val = ! check_audio_gpio(gp);
152         if (val != ucontrol->value.integer.value[0]) {
153                 write_audio_gpio(gp, ! ucontrol->value.integer.value[0]);
154                 return 1;
155         }
156         return 0;
157 }
158
159 static snd_kcontrol_new_t toonie_hp_sw __initdata = {
160         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
161         .name = "Headphone Playback Switch",
162         .info = snd_pmac_boolean_mono_info,
163         .get = toonie_get_mute_switch,
164         .put = toonie_put_mute_switch,
165         .private_value = TOONIE_MUTE_HP,
166 };
167 static snd_kcontrol_new_t toonie_speaker_sw __initdata = {
168         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
169         .name = "PC Speaker Playback Switch",
170         .info = snd_pmac_boolean_mono_info,
171         .get = toonie_get_mute_switch,
172         .put = toonie_put_mute_switch,
173         .private_value = TOONIE_MUTE_AMP,
174 };
175
176 /*
177  * auto-mute stuffs
178  */
179 static int toonie_detect_headphone(pmac_t *chip)
180 {
181         struct pmac_toonie *mix = chip->mixer_data;
182         int detect = 0;
183
184         if (mix->hp_detect_gpio.addr)
185                 detect |= read_audio_gpio(&mix->hp_detect_gpio);
186         return detect;
187 }
188
189 static void toonie_check_mute(pmac_t *chip, struct pmac_gpio *gp, int val,
190                               int do_notify, snd_kcontrol_t *sw)
191 {
192         if (check_audio_gpio(gp) != val) {
193                 write_audio_gpio(gp, val);
194                 if (do_notify)
195                         snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
196                                        &sw->id);
197         }
198 }
199
200 static void toonie_detect_handler(void *self)
201 {
202         pmac_t *chip = (pmac_t*) self;
203         struct pmac_toonie *mix;
204         int headphone;
205
206         if (!chip)
207                 return;
208
209         mix = chip->mixer_data;
210         snd_assert(mix, return);
211
212         headphone = toonie_detect_headphone(chip);
213
214         DBG("headphone: %d, lineout: %d\n", headphone, lineout);
215
216         if (headphone) {
217                 /* unmute headphone/lineout & mute speaker */
218                 toonie_check_mute(chip, &mix->hp_mute_gpio, 0,
219                                   mix->auto_mute_notify, chip->master_sw_ctl);
220                 toonie_check_mute(chip, &mix->amp_mute_gpio, 1,
221                                   mix->auto_mute_notify, chip->speaker_sw_ctl);
222         } else {
223                 /* unmute speaker, mute others */
224                 toonie_check_mute(chip, &mix->amp_mute_gpio, 0,
225                                   mix->auto_mute_notify, chip->speaker_sw_ctl);
226                 toonie_check_mute(chip, &mix->hp_mute_gpio, 1,
227                                   mix->auto_mute_notify, chip->master_sw_ctl);
228         }
229         if (mix->auto_mute_notify) {
230                 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
231                                        &chip->hp_detect_ctl->id);
232         }
233 }
234
235 static void toonie_update_automute(pmac_t *chip, int do_notify)
236 {
237         if (chip->auto_mute) {
238                 struct pmac_toonie *mix;
239                 mix = chip->mixer_data;
240                 snd_assert(mix, return);
241                 mix->auto_mute_notify = do_notify;
242                 schedule_work(&mix->detect_work);
243         }
244 }
245
246 /* interrupt - headphone plug changed */
247 static irqreturn_t toonie_hp_intr(int irq, void *devid, struct pt_regs *regs)
248 {
249         pmac_t *chip = devid;
250
251         if (chip->update_automute && chip->initialized) {
252                 chip->update_automute(chip, 1);
253                 return IRQ_HANDLED;
254         }
255         return IRQ_NONE;
256 }
257
258 /* look for audio gpio device */
259 static int find_audio_gpio(const char *name, const char *platform,
260                            struct pmac_gpio *gp)
261 {
262         struct device_node *np;
263         u32 *base, addr;
264
265         if (! (np = find_devices("gpio")))
266                 return -ENODEV;
267
268         for (np = np->child; np; np = np->sibling) {
269                 char *property = get_property(np, "audio-gpio", NULL);
270                 if (property && strcmp(property, name) == 0)
271                         break;
272                 if (device_is_compatible(np, name))
273                         break;
274         }
275         if (np == NULL)
276                 return -ENODEV;
277
278         base = (u32 *)get_property(np, "AAPL,address", NULL);
279         if (! base) {
280                 base = (u32 *)get_property(np, "reg", NULL);
281                 if (!base) {
282                         DBG("(E) cannot find address for device %s !\n", name);
283                         return -ENODEV;
284                 }
285                 addr = *base;
286                 if (addr < 0x50)
287                         addr += 0x50;
288         } else
289                 addr = *base;
290
291         gp->addr = addr & 0x0000ffff;
292
293         /* Try to find the active state, default to 0 ! */
294         base = (u32 *)get_property(np, "audio-gpio-active-state", NULL);
295         if (base) {
296                 gp->active_state = *base;
297                 gp->active_val = (*base) ? 0x5 : 0x4;
298                 gp->inactive_val = (*base) ? 0x4 : 0x5;
299         } else {
300                 u32 *prop = NULL;
301                 gp->active_state = 0;
302                 gp->active_val = 0x4;
303                 gp->inactive_val = 0x5;
304                 /* Here are some crude hacks to extract the GPIO polarity and
305                  * open collector informations out of the do-platform script
306                  * as we don't yet have an interpreter for these things
307                  */
308                 if (platform)
309                         prop = (u32 *)get_property(np, platform, NULL);
310                 if (prop) {
311                         if (prop[3] == 0x9 && prop[4] == 0x9) {
312                                 gp->active_val = 0xd;
313                                 gp->inactive_val = 0xc;
314                         }
315                         if (prop[3] == 0x1 && prop[4] == 0x1) {
316                                 gp->active_val = 0x5;
317                                 gp->inactive_val = 0x4;
318                         }
319                 }
320         }
321
322         DBG("(I) GPIO device %s found, offset: %x, active state: %d !\n",
323             device, gp->addr, gp->active_state);
324
325         return (np->n_intrs > 0) ? np->intrs[0].line : 0;
326 }
327
328 static void toonie_cleanup(pmac_t *chip)
329 {
330         struct pmac_toonie *mix = chip->mixer_data;
331         if (! mix)
332                 return;
333         if (mix->hp_detect_irq >= 0)
334                 free_irq(mix->hp_detect_irq, chip);
335         kfree(mix);
336         chip->mixer_data = NULL;
337 }
338
339 int snd_pmac_toonie_init(pmac_t *chip)
340 {
341         struct pmac_toonie *mix;
342
343         mix = kmalloc(sizeof(*mix), GFP_KERNEL);
344         if (! mix)
345                 return -ENOMEM;
346
347         chip->mixer_data = mix;
348         chip->mixer_free = toonie_cleanup;
349
350         find_audio_gpio("headphone-mute", NULL, &mix->hp_mute_gpio);
351         find_audio_gpio("amp-mute", NULL, &mix->amp_mute_gpio);
352         mix->hp_detect_irq = find_audio_gpio("headphone-detect",
353                                              NULL, &mix->hp_detect_gpio);
354
355         strcpy(chip->card->mixername, "PowerMac Toonie");
356
357         chip->master_sw_ctl = snd_ctl_new1(&toonie_hp_sw, chip);
358         snd_ctl_add(chip->card, chip->master_sw_ctl);
359
360         chip->speaker_sw_ctl = snd_ctl_new1(&toonie_speaker_sw, chip);
361         snd_ctl_add(chip->card, chip->speaker_sw_ctl);
362
363         INIT_WORK(&mix->detect_work, toonie_detect_handler, (void *)chip);
364
365         if (mix->hp_detect_irq >= 0) {
366                 snd_pmac_add_automute(chip);
367
368                 chip->detect_headphone = toonie_detect_headphone;
369                 chip->update_automute = toonie_update_automute;
370                 toonie_update_automute(chip, 0);
371
372                 if (request_irq(mix->hp_detect_irq, toonie_hp_intr, 0,
373                                 "Sound Headphone Detection", chip) < 0)
374                         mix->hp_detect_irq = -1;
375         }
376
377         return 0;
378 }
379