]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - sound/soc/pxa/tosa.c
ASoC: pxa: tosa: Update locking around use of DAPM pin API
[mirror_ubuntu-bionic-kernel.git] / sound / soc / pxa / tosa.c
CommitLineData
1b49cb98
LG
1/*
2 * tosa.c -- SoC audio for Tosa
3 *
4 * Copyright 2005 Wolfson Microelectronics PLC.
5 * Copyright 2005 Openedhand Ltd.
6 *
d331124d 7 * Authors: Liam Girdwood <lrg@slimlogic.co.uk>
1b49cb98
LG
8 * Richard Purdie <richard@openedhand.com>
9 *
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the
12 * Free Software Foundation; either version 2 of the License, or (at your
13 * option) any later version.
14 *
1b49cb98
LG
15 * GPIO's
16 * 1 - Jack Insertion
17 * 5 - Hookswitch (headset answer/hang up switch)
18 *
19 */
20
21#include <linux/module.h>
22#include <linux/moduleparam.h>
23#include <linux/device.h>
4440cbd6 24#include <linux/gpio.h>
1b49cb98 25
1b49cb98
LG
26#include <sound/core.h>
27#include <sound/pcm.h>
28#include <sound/soc.h>
1b49cb98
LG
29
30#include <asm/mach-types.h>
a09e64fb 31#include <mach/tosa.h>
a09e64fb 32#include <mach/audio.h>
1b49cb98
LG
33
34#include "../codecs/wm9712.h"
cb4c048b 35#include "pxa2xx-ac97.h"
1b49cb98 36
1b49cb98
LG
37#define TOSA_HP 0
38#define TOSA_MIC_INT 1
39#define TOSA_HEADSET 2
40#define TOSA_HP_OFF 3
41#define TOSA_SPK_ON 0
42#define TOSA_SPK_OFF 1
43
44static int tosa_jack_func;
45static int tosa_spk_func;
46
47static void tosa_ext_control(struct snd_soc_codec *codec)
48{
ce6120cc
LG
49 struct snd_soc_dapm_context *dapm = &codec->dapm;
50
26e24ddc
CK
51 snd_soc_dapm_mutex_lock(dapm);
52
1b49cb98
LG
53 /* set up jack connection */
54 switch (tosa_jack_func) {
55 case TOSA_HP:
26e24ddc
CK
56 snd_soc_dapm_disable_pin_unlocked(dapm, "Mic (Internal)");
57 snd_soc_dapm_enable_pin_unlocked(dapm, "Headphone Jack");
58 snd_soc_dapm_disable_pin_unlocked(dapm, "Headset Jack");
1b49cb98
LG
59 break;
60 case TOSA_MIC_INT:
26e24ddc
CK
61 snd_soc_dapm_enable_pin_unlocked(dapm, "Mic (Internal)");
62 snd_soc_dapm_disable_pin_unlocked(dapm, "Headphone Jack");
63 snd_soc_dapm_disable_pin_unlocked(dapm, "Headset Jack");
1b49cb98
LG
64 break;
65 case TOSA_HEADSET:
26e24ddc
CK
66 snd_soc_dapm_disable_pin_unlocked(dapm, "Mic (Internal)");
67 snd_soc_dapm_disable_pin_unlocked(dapm, "Headphone Jack");
68 snd_soc_dapm_enable_pin_unlocked(dapm, "Headset Jack");
1b49cb98
LG
69 break;
70 }
71
72 if (tosa_spk_func == TOSA_SPK_ON)
26e24ddc 73 snd_soc_dapm_enable_pin_unlocked(dapm, "Speaker");
a5302181 74 else
26e24ddc
CK
75 snd_soc_dapm_disable_pin_unlocked(dapm, "Speaker");
76
77 snd_soc_dapm_sync_unlocked(dapm);
1b49cb98 78
26e24ddc 79 snd_soc_dapm_mutex_unlock(dapm);
1b49cb98
LG
80}
81
82static int tosa_startup(struct snd_pcm_substream *substream)
83{
84 struct snd_soc_pcm_runtime *rtd = substream->private_data;
cb990622 85 struct snd_soc_codec *codec = rtd->codec;
1b49cb98 86
71a29560
MB
87 mutex_lock(&codec->mutex);
88
1b49cb98
LG
89 /* check the jack status at stream startup */
90 tosa_ext_control(codec);
71a29560
MB
91
92 mutex_unlock(&codec->mutex);
93
1b49cb98
LG
94 return 0;
95}
96
97static struct snd_soc_ops tosa_ops = {
98 .startup = tosa_startup,
99};
100
101static int tosa_get_jack(struct snd_kcontrol *kcontrol,
102 struct snd_ctl_elem_value *ucontrol)
103{
104 ucontrol->value.integer.value[0] = tosa_jack_func;
105 return 0;
106}
107
108static int tosa_set_jack(struct snd_kcontrol *kcontrol,
109 struct snd_ctl_elem_value *ucontrol)
110{
111 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
112
113 if (tosa_jack_func == ucontrol->value.integer.value[0])
114 return 0;
115
116 tosa_jack_func = ucontrol->value.integer.value[0];
117 tosa_ext_control(codec);
118 return 1;
119}
120
121static int tosa_get_spk(struct snd_kcontrol *kcontrol,
122 struct snd_ctl_elem_value *ucontrol)
123{
124 ucontrol->value.integer.value[0] = tosa_spk_func;
125 return 0;
126}
127
128static int tosa_set_spk(struct snd_kcontrol *kcontrol,
129 struct snd_ctl_elem_value *ucontrol)
130{
131 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
132
133 if (tosa_spk_func == ucontrol->value.integer.value[0])
134 return 0;
135
136 tosa_spk_func = ucontrol->value.integer.value[0];
137 tosa_ext_control(codec);
138 return 1;
139}
140
141/* tosa dapm event handlers */
338c7ed0
JN
142static int tosa_hp_event(struct snd_soc_dapm_widget *w,
143 struct snd_kcontrol *k, int event)
1b49cb98 144{
4440cbd6 145 gpio_set_value(TOSA_GPIO_L_MUTE, SND_SOC_DAPM_EVENT_ON(event) ? 1 :0);
1b49cb98
LG
146 return 0;
147}
148
149/* tosa machine dapm widgets */
150static const struct snd_soc_dapm_widget tosa_dapm_widgets[] = {
151SND_SOC_DAPM_HP("Headphone Jack", tosa_hp_event),
152SND_SOC_DAPM_HP("Headset Jack", NULL),
153SND_SOC_DAPM_MIC("Mic (Internal)", NULL),
154SND_SOC_DAPM_SPK("Speaker", NULL),
155};
156
157/* tosa audio map */
76d39d0a 158static const struct snd_soc_dapm_route audio_map[] = {
1b49cb98
LG
159
160 /* headphone connected to HPOUTL, HPOUTR */
161 {"Headphone Jack", NULL, "HPOUTL"},
162 {"Headphone Jack", NULL, "HPOUTR"},
163
164 /* ext speaker connected to LOUT2, ROUT2 */
165 {"Speaker", NULL, "LOUT2"},
166 {"Speaker", NULL, "ROUT2"},
167
168 /* internal mic is connected to mic1, mic2 differential - with bias */
169 {"MIC1", NULL, "Mic Bias"},
170 {"MIC2", NULL, "Mic Bias"},
171 {"Mic Bias", NULL, "Mic (Internal)"},
172
173 /* headset is connected to HPOUTR, and LINEINR with bias */
174 {"Headset Jack", NULL, "HPOUTR"},
175 {"LINEINR", NULL, "Mic Bias"},
176 {"Mic Bias", NULL, "Headset Jack"},
1b49cb98
LG
177};
178
179static const char *jack_function[] = {"Headphone", "Mic", "Line", "Headset",
180 "Off"};
181static const char *spk_function[] = {"On", "Off"};
182static const struct soc_enum tosa_enum[] = {
183 SOC_ENUM_SINGLE_EXT(5, jack_function),
184 SOC_ENUM_SINGLE_EXT(2, spk_function),
185};
186
187static const struct snd_kcontrol_new tosa_controls[] = {
188 SOC_ENUM_EXT("Jack Function", tosa_enum[0], tosa_get_jack,
189 tosa_set_jack),
190 SOC_ENUM_EXT("Speaker Function", tosa_enum[1], tosa_get_spk,
191 tosa_set_spk),
192};
193
f0fba2ad 194static int tosa_ac97_init(struct snd_soc_pcm_runtime *rtd)
1b49cb98 195{
f0fba2ad 196 struct snd_soc_codec *codec = rtd->codec;
ce6120cc 197 struct snd_soc_dapm_context *dapm = &codec->dapm;
eb5f6d75 198 int err;
1b49cb98 199
ce6120cc
LG
200 snd_soc_dapm_nc_pin(dapm, "OUT3");
201 snd_soc_dapm_nc_pin(dapm, "MONOOUT");
1b49cb98
LG
202
203 /* add tosa specific controls */
022658be 204 err = snd_soc_add_codec_controls(codec, tosa_controls,
eb5f6d75
PZ
205 ARRAY_SIZE(tosa_controls));
206 if (err < 0)
207 return err;
1b49cb98
LG
208
209 /* add tosa specific widgets */
ce6120cc 210 snd_soc_dapm_new_controls(dapm, tosa_dapm_widgets,
25191c45 211 ARRAY_SIZE(tosa_dapm_widgets));
1b49cb98
LG
212
213 /* set up tosa specific audio path audio_map */
ce6120cc 214 snd_soc_dapm_add_routes(dapm, audio_map, ARRAY_SIZE(audio_map));
1b49cb98 215
1b49cb98
LG
216 return 0;
217}
218
219static struct snd_soc_dai_link tosa_dai[] = {
220{
221 .name = "AC97",
222 .stream_name = "AC97 HiFi",
4bfc4e25 223 .cpu_dai_name = "pxa2xx-ac97",
f0fba2ad
LG
224 .codec_dai_name = "wm9712-hifi",
225 .platform_name = "pxa-pcm-audio",
226 .codec_name = "wm9712-codec",
1b49cb98 227 .init = tosa_ac97_init,
cb4c048b 228 .ops = &tosa_ops,
1b49cb98
LG
229},
230{
231 .name = "AC97 Aux",
232 .stream_name = "AC97 Aux",
4bfc4e25 233 .cpu_dai_name = "pxa2xx-ac97-aux",
f0fba2ad
LG
234 .codec_dai_name = "wm9712-aux",
235 .platform_name = "pxa-pcm-audio",
236 .codec_name = "wm9712-codec",
cb4c048b 237 .ops = &tosa_ops,
1b49cb98
LG
238},
239};
240
87506549 241static struct snd_soc_card tosa = {
1b49cb98 242 .name = "Tosa",
561c6a17 243 .owner = THIS_MODULE,
1b49cb98
LG
244 .dai_link = tosa_dai,
245 .num_links = ARRAY_SIZE(tosa_dai),
1b49cb98
LG
246};
247
570f6fe1 248static int tosa_probe(struct platform_device *pdev)
1b49cb98 249{
f285b8c8 250 struct snd_soc_card *card = &tosa;
1b49cb98
LG
251 int ret;
252
f285b8c8
AL
253 ret = gpio_request_one(TOSA_GPIO_L_MUTE, GPIOF_OUT_INIT_LOW,
254 "Headphone Jack");
255 if (ret)
256 return ret;
4440cbd6 257
f285b8c8 258 card->dev = &pdev->dev;
4440cbd6 259
f285b8c8
AL
260 ret = snd_soc_register_card(card);
261 if (ret) {
262 dev_err(&pdev->dev, "snd_soc_register_card() failed: %d\n",
263 ret);
264 gpio_free(TOSA_GPIO_L_MUTE);
265 }
1b49cb98
LG
266 return ret;
267}
268
570f6fe1 269static int tosa_remove(struct platform_device *pdev)
1b49cb98 270{
f285b8c8
AL
271 struct snd_soc_card *card = platform_get_drvdata(pdev);
272
273 gpio_free(TOSA_GPIO_L_MUTE);
274 snd_soc_unregister_card(card);
275 return 0;
1b49cb98
LG
276}
277
f285b8c8
AL
278static struct platform_driver tosa_driver = {
279 .driver = {
280 .name = "tosa-audio",
281 .owner = THIS_MODULE,
7db1698f 282 .pm = &snd_soc_pm_ops,
f285b8c8
AL
283 },
284 .probe = tosa_probe,
570f6fe1 285 .remove = tosa_remove,
f285b8c8
AL
286};
287
288module_platform_driver(tosa_driver);
1b49cb98
LG
289
290/* Module information */
291MODULE_AUTHOR("Richard Purdie");
292MODULE_DESCRIPTION("ALSA SoC Tosa");
293MODULE_LICENSE("GPL");
f285b8c8 294MODULE_ALIAS("platform:tosa-audio");