aboutsummaryrefslogtreecommitdiffstats
path: root/sound/ppc/toonie.c
blob: 0f909193b4fb9d7c19e0b7a4870fd715885b5f21 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/*
 * Mac Mini "toonie" mixer control
 *
 * Copyright (c) 2005 by Benjamin Herrenschmidt <benh@kernel.crashing.org>
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 */

#include <sound/driver.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/i2c.h>
#include <linux/i2c-dev.h>
#include <linux/kmod.h>
#include <linux/slab.h>
#include <linux/interrupt.h>
#include <sound/core.h>
#include <asm/io.h>
#include <asm/irq.h>
#include <asm/machdep.h>
#include <asm/pmac_feature.h>
#include "pmac.h"

#undef DEBUG

#ifdef DEBUG
#define DBG(fmt...) printk(fmt)
#else
#define DBG(fmt...)
#endif

struct pmac_gpio {
	unsigned int addr;
	u8 active_val;
	u8 inactive_val;
	u8 active_state;
};

struct pmac_toonie
{
	struct pmac_gpio	hp_detect_gpio;
	struct pmac_gpio	hp_mute_gpio;
	struct pmac_gpio	amp_mute_gpio;
	int			hp_detect_irq;
	int			auto_mute_notify;
	struct work_struct	detect_work;
};


/*
 * gpio access
 */
#define do_gpio_write(gp, val) \
	pmac_call_feature(PMAC_FTR_WRITE_GPIO, NULL, (gp)->addr, val)
#define do_gpio_read(gp) \
	pmac_call_feature(PMAC_FTR_READ_GPIO, NULL, (gp)->addr, 0)
#define tumbler_gpio_free(gp) /* NOP */

static void write_audio_gpio(struct pmac_gpio *gp, int active)
{
	if (! gp->addr)
		return;
	active = active ? gp->active_val : gp->inactive_val;
	do_gpio_write(gp, active);
	DBG("(I) gpio %x write %d\n", gp->addr, active);
}

static int check_audio_gpio(struct pmac_gpio *gp)
{
	int ret;

	if (! gp->addr)
		return 0;

	ret = do_gpio_read(gp);

	return (ret & 0xd) == (gp->active_val & 0xd);
}

static int read_audio_gpio(struct pmac_gpio *gp)
{
	int ret;
	if (! gp->addr)
		return 0;
	ret = ((do_gpio_read(gp) & 0x02) !=0);
	return ret == gp->active_state;
}


enum { TOONIE_MUTE_HP, TOONIE_MUTE_AMP };

static int toonie_get_mute_switch(snd_kcontrol_t *kcontrol,
				  snd_ctl_elem_value_t *ucontrol)
{
	pmac_t *chip = snd_kcontrol_chip(kcontrol);
	struct pmac_toonie *mix = chip->mixer_data;
	struct pmac_gpio *gp;

	if (mix == NULL)
		return -ENODEV;
	switch(kcontrol->private_value) {
	case TOONIE_MUTE_HP:
		gp = &mix->hp_mute_gpio;
		break;
	case TOONIE_MUTE_AMP:
		gp = &mix->amp_mute_gpio;
		break;
	default:
		return -EINVAL;;
	}
	ucontrol->value.integer.value[0] = !check_audio_gpio(gp);
	return 0;
}

static int toonie_put_mute_switch(snd_kcontrol_t *kcontrol,
				   snd_ctl_elem_value_t *ucontrol)
{
	pmac_t *chip = snd_kcontrol_chip(kcontrol);
	struct pmac_toonie *mix = chip->mixer_data;
	struct pmac_gpio *gp;
	int val;

	if (chip->update_automute && chip->auto_mute)
		return 0; /* don't touch in the auto-mute mode */

	if (mix == NULL)
		return -ENODEV;

	switch(kcontrol->private_value) {
	case TOONIE_MUTE_HP:
		gp = &mix->hp_mute_gpio;
		break;
	case TOONIE_MUTE_AMP:
		gp = &mix->amp_mute_gpio;
		break;
	default:
		return -EINVAL;;
	}
	val = ! check_audio_gpio(gp);
	if (val != ucontrol->value.integer.value[0]) {
		write_audio_gpio(gp, ! ucontrol->value.integer.value[0]);
		return 1;
	}
	return 0;
}

static snd_kcontrol_new_t toonie_hp_sw __initdata = {
	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
	.name = "Headphone Playback Switch",
	.info = snd_pmac_boolean_mono_info,
	.get = toonie_get_mute_switch,
	.put = toonie_put_mute_switch,
	.private_value = TOONIE_MUTE_HP,
};
static snd_kcontrol_new_t toonie_speaker_sw __initdata = {
	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
	.name = "PC Speaker Playback Switch",
	.info = snd_pmac_boolean_mono_info,
	.get = toonie_get_mute_switch,
	.put = toonie_put_mute_switch,
	.private_value = TOONIE_MUTE_AMP,
};

/*
 * auto-mute stuffs
 */
static int toonie_detect_headphone(pmac_t *chip)
{
	struct pmac_toonie *mix = chip->mixer_data;
	int detect = 0;

	if (mix->hp_detect_gpio.addr)
		detect |= read_audio_gpio(&mix->hp_detect_gpio);
	return detect;
}

static void toonie_check_mute(pmac_t *chip, struct pmac_gpio *gp, int val,
			      int do_notify, snd_kcontrol_t *sw)
{
	if (check_audio_gpio(gp) != val) {
		write_audio_gpio(gp, val);
		if (do_notify)
			snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
				       &sw->id);
	}
}

static void toonie_detect_handler(void *self)
{
	pmac_t *chip = (pmac_t*) self;
	struct pmac_toonie *mix;
	int headphone;

	if (!chip)
		return;

	mix = chip->mixer_data;
	snd_assert(mix, return);

	headphone = toonie_detect_headphone(chip);

	DBG("headphone: %d, lineout: %d\n", headphone, lineout);

	if (headphone) {
		/* unmute headphone/lineout & mute speaker */
		toonie_check_mute(chip, &mix->hp_mute_gpio, 0,
				  mix->auto_mute_notify, chip->master_sw_ctl);
		toonie_check_mute(chip, &mix->amp_mute_gpio, 1,
				  mix->auto_mute_notify, chip->speaker_sw_ctl);
	} else {
		/* unmute speaker, mute others */
		toonie_check_mute(chip, &mix->amp_mute_gpio, 0,
				  mix->auto_mute_notify, chip->speaker_sw_ctl);
		toonie_check_mute(chip, &mix->hp_mute_gpio, 1,
				  mix->auto_mute_notify, chip->master_sw_ctl);
	}
	if (mix->auto_mute_notify) {
		snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
				       &chip->hp_detect_ctl->id);
	}
}

static void toonie_update_automute(pmac_t *chip, int do_notify)
{
	if (chip->auto_mute) {
		struct pmac_toonie *mix;
		mix = chip->mixer_data;
		snd_assert(mix, return);
		mix->auto_mute_notify = do_notify;
		schedule_work(&mix->detect_work);
	}
}

/* interrupt - headphone plug changed */
static irqreturn_t toonie_hp_intr(int irq, void *devid, struct pt_regs *regs)
{
	pmac_t *chip = devid;

	if (chip->update_automute && chip->initialized) {
		chip->update_automute(chip, 1);
		return IRQ_HANDLED;
	}
	return IRQ_NONE;
}

/* look for audio gpio device */
static int find_audio_gpio(const char *name, const char *platform,
			   struct pmac_gpio *gp)
{
	struct device_node *np;
  	u32 *base, addr;

	if (! (np = find_devices("gpio")))
		return -ENODEV;

	for (np = np->child; np; np = np->sibling) {
		char *property = get_property(np, "audio-gpio", NULL);
		if (property && strcmp(property, name) == 0)
			break;
		if (device_is_compatible(np, name))
			break;
	}
	if (np == NULL)
		return -ENODEV;

	base = (u32 *)get_property(np, "AAPL,address", NULL);
	if (! base) {
		base = (u32 *)get_property(np, "reg", NULL);
		if (!base) {
			DBG("(E) cannot find address for device %s !\n", device);
			snd_printd("cannot find address for device %s\n", device);
			return -ENODEV;
		}
		addr = *base;
		if (addr < 0x50)
			addr += 0x50;
	} else
		addr = *base;

	gp->addr = addr & 0x0000ffff;

	/* Try to find the active state, default to 0 ! */
	base = (u32 *)get_property(np, "audio-gpio-active-state", NULL);
	if (base) {
		gp->active_state = *base;
		gp->active_val = (*base) ? 0x5 : 0x4;
		gp->inactive_val = (*base) ? 0x4 : 0x5;
	} else {
		u32 *prop = NULL;
		gp->active_state = 0;
		gp->active_val = 0x4;
		gp->inactive_val = 0x5;
		/* Here are some crude hacks to extract the GPIO polarity and
		 * open collector informations out of the do-platform script
		 * as we don't yet have an interpreter for these things
		 */
		if (platform)
			prop = (u32 *)get_property(np, platform, NULL);
		if (prop) {
			if (prop[3] == 0x9 && prop[4] == 0x9) {
				gp->active_val = 0xd;
				gp->inactive_val = 0xc;
			}
			if (prop[3] == 0x1 && prop[4] == 0x1) {
				gp->active_val = 0x5;
				gp->inactive_val = 0x4;
			}
		}
	}

	DBG("(I) GPIO device %s found, offset: %x, active state: %d !\n",
	    device, gp->addr, gp->active_state);

	return (np->n_intrs > 0) ? np->intrs[0].line : 0;
}

static void toonie_cleanup(pmac_t *chip)
{
	struct pmac_toonie *mix = chip->mixer_data;
	if (! mix)
		return;
	if (mix->hp_detect_irq >= 0)
		free_irq(mix->hp_detect_irq, chip);
	kfree(mix);
	chip->mixer_data = NULL;
}

int snd_pmac_toonie_init(pmac_t *chip)
{
	struct pmac_toonie *mix;

	mix = kmalloc(sizeof(*mix), GFP_KERNEL);
	if (! mix)
		return -ENOMEM;

	chip->mixer_data = mix;
	chip->mixer_free = toonie_cleanup;

	find_audio_gpio("headphone-mute", NULL, &mix->hp_mute_gpio);
	find_audio_gpio("amp-mute", NULL, &mix->amp_mute_gpio);
	mix->hp_detect_irq = find_audio_gpio("headphone-detect",
					     NULL, &mix->hp_detect_gpio);

	strcpy(chip->card->mixername, "PowerMac Toonie");

	chip->master_sw_ctl = snd_ctl_new1(&toonie_hp_sw, chip);
	snd_ctl_add(chip->card, chip->master_sw_ctl);

	chip->speaker_sw_ctl = snd_ctl_new1(&toonie_speaker_sw, chip);
	snd_ctl_add(chip->card, chip->speaker_sw_ctl);

	INIT_WORK(&mix->detect_work, toonie_detect_handler, (void *)chip);

	if (mix->hp_detect_irq >= 0) {
		snd_pmac_add_automute(chip);

		chip->detect_headphone = toonie_detect_headphone;
		chip->update_automute = toonie_update_automute;
		toonie_update_automute(chip, 0);

		if (request_irq(mix->hp_detect_irq, toonie_hp_intr, 0,
				"Sound Headphone Detection", chip) < 0)
			mix->hp_detect_irq = -1;
	}

	return 0;
}