[PATCH] w1_therm: removed duplicated family id.
[linux-2.6/verdex.git] / sound / ppc / toonie.c
blob082bc4babab5f8f564acddd39d356ca134a5b978
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/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"
36 #undef DEBUG
38 #ifdef DEBUG
39 #define DBG(fmt...) printk(fmt)
40 #else
41 #define DBG(fmt...)
42 #endif
44 struct pmac_gpio {
45 unsigned int addr;
46 u8 active_val;
47 u8 inactive_val;
48 u8 active_state;
51 struct pmac_toonie
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;
63 * gpio access
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 */
71 static void write_audio_gpio(struct pmac_gpio *gp, int active)
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);
80 static int check_audio_gpio(struct pmac_gpio *gp)
82 int ret;
84 if (! gp->addr)
85 return 0;
87 ret = do_gpio_read(gp);
89 return (ret & 0xd) == (gp->active_val & 0xd);
92 static int read_audio_gpio(struct pmac_gpio *gp)
94 int ret;
95 if (! gp->addr)
96 return 0;
97 ret = ((do_gpio_read(gp) & 0x02) !=0);
98 return ret == gp->active_state;
102 enum { TOONIE_MUTE_HP, TOONIE_MUTE_AMP };
104 static int toonie_get_mute_switch(snd_kcontrol_t *kcontrol,
105 snd_ctl_elem_value_t *ucontrol)
107 pmac_t *chip = snd_kcontrol_chip(kcontrol);
108 struct pmac_toonie *mix = chip->mixer_data;
109 struct pmac_gpio *gp;
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;;
123 ucontrol->value.integer.value[0] = !check_audio_gpio(gp);
124 return 0;
127 static int toonie_put_mute_switch(snd_kcontrol_t *kcontrol,
128 snd_ctl_elem_value_t *ucontrol)
130 pmac_t *chip = snd_kcontrol_chip(kcontrol);
131 struct pmac_toonie *mix = chip->mixer_data;
132 struct pmac_gpio *gp;
133 int val;
135 if (chip->update_automute && chip->auto_mute)
136 return 0; /* don't touch in the auto-mute mode */
138 if (mix == NULL)
139 return -ENODEV;
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;;
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;
156 return 0;
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,
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,
177 * auto-mute stuffs
179 static int toonie_detect_headphone(pmac_t *chip)
181 struct pmac_toonie *mix = chip->mixer_data;
182 int detect = 0;
184 if (mix->hp_detect_gpio.addr)
185 detect |= read_audio_gpio(&mix->hp_detect_gpio);
186 return detect;
189 static void toonie_check_mute(pmac_t *chip, struct pmac_gpio *gp, int val,
190 int do_notify, snd_kcontrol_t *sw)
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);
200 static void toonie_detect_handler(void *self)
202 pmac_t *chip = (pmac_t*) self;
203 struct pmac_toonie *mix;
204 int headphone;
206 if (!chip)
207 return;
209 mix = chip->mixer_data;
210 snd_assert(mix, return);
212 headphone = toonie_detect_headphone(chip);
214 DBG("headphone: %d, lineout: %d\n", headphone, lineout);
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);
229 if (mix->auto_mute_notify) {
230 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
231 &chip->hp_detect_ctl->id);
235 static void toonie_update_automute(pmac_t *chip, int do_notify)
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);
246 /* interrupt - headphone plug changed */
247 static irqreturn_t toonie_hp_intr(int irq, void *devid, struct pt_regs *regs)
249 pmac_t *chip = devid;
251 if (chip->update_automute && chip->initialized) {
252 chip->update_automute(chip, 1);
253 return IRQ_HANDLED;
255 return IRQ_NONE;
258 /* look for audio gpio device */
259 static int find_audio_gpio(const char *name, const char *platform,
260 struct pmac_gpio *gp)
262 struct device_node *np;
263 u32 *base, addr;
265 if (! (np = find_devices("gpio")))
266 return -ENODEV;
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;
275 if (np == NULL)
276 return -ENODEV;
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;
285 addr = *base;
286 if (addr < 0x50)
287 addr += 0x50;
288 } else
289 addr = *base;
291 gp->addr = addr & 0x0000ffff;
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
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;
315 if (prop[3] == 0x1 && prop[4] == 0x1) {
316 gp->active_val = 0x5;
317 gp->inactive_val = 0x4;
322 DBG("(I) GPIO device %s found, offset: %x, active state: %d !\n",
323 name, gp->addr, gp->active_state);
325 return (np->n_intrs > 0) ? np->intrs[0].line : 0;
328 static void toonie_cleanup(pmac_t *chip)
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;
339 int snd_pmac_toonie_init(pmac_t *chip)
341 struct pmac_toonie *mix;
343 mix = kmalloc(sizeof(*mix), GFP_KERNEL);
344 if (! mix)
345 return -ENOMEM;
347 chip->mixer_data = mix;
348 chip->mixer_free = toonie_cleanup;
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);
355 strcpy(chip->card->mixername, "PowerMac Toonie");
357 chip->master_sw_ctl = snd_ctl_new1(&toonie_hp_sw, chip);
358 snd_ctl_add(chip->card, chip->master_sw_ctl);
360 chip->speaker_sw_ctl = snd_ctl_new1(&toonie_speaker_sw, chip);
361 snd_ctl_add(chip->card, chip->speaker_sw_ctl);
363 INIT_WORK(&mix->detect_work, toonie_detect_handler, (void *)chip);
365 if (mix->hp_detect_irq >= 0) {
366 snd_pmac_add_automute(chip);
368 chip->detect_headphone = toonie_detect_headphone;
369 chip->update_automute = toonie_update_automute;
370 toonie_update_automute(chip, 0);
372 if (request_irq(mix->hp_detect_irq, toonie_hp_intr, 0,
373 "Sound Headphone Detection", chip) < 0)
374 mix->hp_detect_irq = -1;
377 return 0;