aboutsummaryrefslogtreecommitdiffstats
path: root/sound/soc/pxa/palm27x.c
blob: 48a73f64500bc6ce78e40b0de21e5ba0413ef521 (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
/*
 * linux/sound/soc/pxa/palm27x.c
 *
 * SoC Audio driver for Palm T|X, T5 and LifeDrive
 *
 * based on tosa.c
 *
 * Copyright (C) 2008 Marek Vasut <marek.vasut@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 */

#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/device.h>
#include <linux/gpio.h>
#include <linux/interrupt.h>
#include <linux/irq.h>

#include <sound/core.h>
#include <sound/pcm.h>
#include <sound/soc.h>
#include <sound/soc-dapm.h>

#include <asm/mach-types.h>
#include <mach/audio.h>
#include <mach/palmasoc.h>

#include "../codecs/wm9712.h"
#include "pxa2xx-pcm.h"
#include "pxa2xx-ac97.h"

static int palm27x_jack_func = 1;
static int palm27x_spk_func = 1;
static int palm27x_ep_gpio = -1;

static void palm27x_ext_control(struct snd_soc_codec *codec)
{
	if (!palm27x_spk_func)
		snd_soc_dapm_enable_pin(codec, "Speaker");
	else
		snd_soc_dapm_disable_pin(codec, "Speaker");

	if (!palm27x_jack_func)
		snd_soc_dapm_enable_pin(codec, "Headphone Jack");
	else
		snd_soc_dapm_disable_pin(codec, "Headphone Jack");

	snd_soc_dapm_sync(codec);
}

static int palm27x_startup(struct snd_pcm_substream *substream)
{
	struct snd_soc_pcm_runtime *rtd = substream->private_data;
	struct snd_soc_codec *codec = rtd->socdev->card->codec;

	/* check the jack status at stream startup */
	palm27x_ext_control(codec);
	return 0;
}

static struct snd_soc_ops palm27x_ops = {
	.startup = palm27x_startup,
};

static irqreturn_t palm27x_interrupt(int irq, void *v)
{
	palm27x_spk_func = gpio_get_value(palm27x_ep_gpio);
	palm27x_jack_func = !palm27x_spk_func;
	return IRQ_HANDLED;
}

static int palm27x_get_jack(struct snd_kcontrol *kcontrol,
	struct snd_ctl_elem_value *ucontrol)
{
	ucontrol->value.integer.value[0] = palm27x_jack_func;
	return 0;
}

static int palm27x_set_jack(struct snd_kcontrol *kcontrol,
	struct snd_ctl_elem_value *ucontrol)
{
	struct snd_soc_codec *codec =  snd_kcontrol_chip(kcontrol);

	if (palm27x_jack_func == ucontrol->value.integer.value[0])
		return 0;

	palm27x_jack_func = ucontrol->value.integer.value[0];
	palm27x_ext_control(codec);
	return 1;
}

static int palm27x_get_spk(struct snd_kcontrol *kcontrol,
	struct snd_ctl_elem_value *ucontrol)
{
	ucontrol->value.integer.value[0] = palm27x_spk_func;
	return 0;
}

static int palm27x_set_spk(struct snd_kcontrol *kcontrol,
	struct snd_ctl_elem_value *ucontrol)
{
	struct snd_soc_codec *codec =  snd_kcontrol_chip(kcontrol);

	if (palm27x_spk_func == ucontrol->value.integer.value[0])
		return 0;

	palm27x_spk_func = ucontrol->value.integer.value[0];
	palm27x_ext_control(codec);
	return 1;
}

/* PalmTX machine dapm widgets */
static const struct snd_soc_dapm_widget palm27x_dapm_widgets[] = {
	SND_SOC_DAPM_HP("Headphone Jack", NULL),
	SND_SOC_DAPM_SPK("Speaker", NULL),
};

/* PalmTX audio map */
static const struct snd_soc_dapm_route audio_map[] = {
	/* headphone connected to HPOUTL, HPOUTR */
	{"Headphone Jack", NULL, "HPOUTL"},
	{"Headphone Jack", NULL, "HPOUTR"},

	/* ext speaker connected to ROUT2, LOUT2 */
	{"Speaker", NULL, "LOUT2"},
	{"Speaker", NULL, "ROUT2"},
};

static const char *jack_function[] = {"Headphone", "Off"};
static const char *spk_function[] = {"On", "Off"};
static const struct soc_enum palm27x_enum[] = {
	SOC_ENUM_SINGLE_EXT(2, jack_function),
	SOC_ENUM_SINGLE_EXT(2, spk_function),
};

static const struct snd_kcontrol_new palm27x_controls[] = {
	SOC_ENUM_EXT("Jack Function", palm27x_enum[0], palm27x_get_jack,
		palm27x_set_jack),
	SOC_ENUM_EXT("Speaker Function", palm27x_enum[1], palm27x_get_spk,
		palm27x_set_spk),
};

static int palm27x_ac97_init(struct snd_soc_codec *codec)
{
	int err;

	snd_soc_dapm_nc_pin(codec, "OUT3");
	snd_soc_dapm_nc_pin(codec, "MONOOUT");

	/* add palm27x specific controls */
	err = snd_soc_add_controls(codec, palm27x_controls,
				ARRAY_SIZE(palm27x_controls));
	if (err < 0)
		return err;

	/* add palm27x specific widgets */
	snd_soc_dapm_new_controls(codec, palm27x_dapm_widgets,
				ARRAY_SIZE(palm27x_dapm_widgets));

	/* set up palm27x specific audio path audio_map */
	snd_soc_dapm_add_routes(codec, audio_map, ARRAY_SIZE(audio_map));

	snd_soc_dapm_sync(codec);
	return 0;
}

static struct snd_soc_dai_link palm27x_dai[] = {
{
	.name = "AC97 HiFi",
	.stream_name = "AC97 HiFi",
	.cpu_dai = &pxa_ac97_dai[PXA2XX_DAI_AC97_HIFI],
	.codec_dai = &wm9712_dai[WM9712_DAI_AC97_HIFI],
	.init = palm27x_ac97_init,
	.ops = &palm27x_ops,
},
{
	.name = "AC97 Aux",
	.stream_name = "AC97 Aux",
	.cpu_dai = &pxa_ac97_dai[PXA2XX_DAI_AC97_AUX],
	.codec_dai = &wm9712_dai[WM9712_DAI_AC97_AUX],
	.ops = &palm27x_ops,
},
};

static struct snd_soc_card palm27x_asoc = {
	.name = "Palm/PXA27x",
	.platform = &pxa2xx_soc_platform,
	.dai_link = palm27x_dai,
	.num_links = ARRAY_SIZE(palm27x_dai),
};

static struct snd_soc_device palm27x_snd_devdata = {
	.card = &palm27x_asoc,
	.codec_dev = &soc_codec_dev_wm9712,
};

static struct platform_device *palm27x_snd_device;

static int __init palm27x_asoc_init(void)
{
	int ret;

	if (!(machine_is_palmtx() || machine_is_palmt5() ||
		machine_is_palmld()))
		return -ENODEV;

	ret = gpio_request(palm27x_ep_gpio, "Headphone Jack");
	if (ret)
		return ret;
	ret = gpio_direction_input(palm27x_ep_gpio);
	if (ret)
		goto err_alloc;

	if (request_irq(gpio_to_irq(palm27x_ep_gpio), palm27x_interrupt,
			IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
			"Headphone jack", NULL))
		goto err_alloc;

	palm27x_snd_device = platform_device_alloc("soc-audio", -1);
	if (!palm27x_snd_device) {
		ret = -ENOMEM;
		goto err_dev;
	}

	platform_set_drvdata(palm27x_snd_device, &palm27x_snd_devdata);
	palm27x_snd_devdata.dev = &palm27x_snd_device->dev;
	ret = platform_device_add(palm27x_snd_device);

	if (ret != 0)
		goto put_device;

	return 0;

put_device:
	platform_device_put(palm27x_snd_device);
err_dev:
	free_irq(gpio_to_irq(palm27x_ep_gpio), NULL);
err_alloc:
	gpio_free(palm27x_ep_gpio);

	return ret;
}

static void __exit palm27x_asoc_exit(void)
{
	free_irq(gpio_to_irq(palm27x_ep_gpio), NULL);
	gpio_free(palm27x_ep_gpio);
	platform_device_unregister(palm27x_snd_device);
}

void __init palm27x_asoc_set_pdata(struct palm27x_asoc_info *data)
{
	palm27x_ep_gpio = data->jack_gpio;
}

module_init(palm27x_asoc_init);
module_exit(palm27x_asoc_exit);

/* Module information */
MODULE_AUTHOR("Marek Vasut <marek.vasut@gmail.com>");
MODULE_DESCRIPTION("ALSA SoC Palm T|X, T5 and LifeDrive");
MODULE_LICENSE("GPL");