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