blob: 2a4b438ce97c92856b83bcfa2358550b94e27054 [file] [log] [blame]
Liam Girdwood1b49cb92006-10-12 14:33:09 +02001/*
2 * tosa.c -- SoC audio for Tosa
3 *
4 * Copyright 2005 Wolfson Microelectronics PLC.
5 * Copyright 2005 Openedhand Ltd.
6 *
Liam Girdwoodd3311242008-10-12 13:17:36 +01007 * Authors: Liam Girdwood <lrg@slimlogic.co.uk>
Liam Girdwood1b49cb92006-10-12 14:33:09 +02008 * 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 *
Liam Girdwood1b49cb92006-10-12 14:33:09 +020015 * 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>
Dmitry Baryshkov4440cbd2008-06-14 11:42:57 +010024#include <linux/gpio.h>
Liam Girdwood1b49cb92006-10-12 14:33:09 +020025
Liam Girdwood1b49cb92006-10-12 14:33:09 +020026#include <sound/core.h>
27#include <sound/pcm.h>
28#include <sound/soc.h>
Liam Girdwood1b49cb92006-10-12 14:33:09 +020029
30#include <asm/mach-types.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010031#include <mach/tosa.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010032#include <mach/audio.h>
Liam Girdwood1b49cb92006-10-12 14:33:09 +020033
34#include "../codecs/wm9712.h"
Liam Girdwoodcb4c0482007-02-02 17:23:11 +010035#include "pxa2xx-ac97.h"
Liam Girdwood1b49cb92006-10-12 14:33:09 +020036
Liam Girdwood1b49cb92006-10-12 14:33:09 +020037#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{
Liam Girdwoodce6120c2010-11-05 15:53:46 +020049 struct snd_soc_dapm_context *dapm = &codec->dapm;
50
Charles Keepax26e24dd2014-02-18 15:22:28 +000051 snd_soc_dapm_mutex_lock(dapm);
52
Liam Girdwood1b49cb92006-10-12 14:33:09 +020053 /* set up jack connection */
54 switch (tosa_jack_func) {
55 case TOSA_HP:
Charles Keepax26e24dd2014-02-18 15:22:28 +000056 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");
Liam Girdwood1b49cb92006-10-12 14:33:09 +020059 break;
60 case TOSA_MIC_INT:
Charles Keepax26e24dd2014-02-18 15:22:28 +000061 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");
Liam Girdwood1b49cb92006-10-12 14:33:09 +020064 break;
65 case TOSA_HEADSET:
Charles Keepax26e24dd2014-02-18 15:22:28 +000066 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");
Liam Girdwood1b49cb92006-10-12 14:33:09 +020069 break;
70 }
71
72 if (tosa_spk_func == TOSA_SPK_ON)
Charles Keepax26e24dd2014-02-18 15:22:28 +000073 snd_soc_dapm_enable_pin_unlocked(dapm, "Speaker");
Liam Girdwooda5302182008-07-07 13:35:17 +010074 else
Charles Keepax26e24dd2014-02-18 15:22:28 +000075 snd_soc_dapm_disable_pin_unlocked(dapm, "Speaker");
Liam Girdwood1b49cb92006-10-12 14:33:09 +020076
Charles Keepax26e24dd2014-02-18 15:22:28 +000077 snd_soc_dapm_sync_unlocked(dapm);
78
79 snd_soc_dapm_mutex_unlock(dapm);
Liam Girdwood1b49cb92006-10-12 14:33:09 +020080}
81
82static int tosa_startup(struct snd_pcm_substream *substream)
83{
84 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Eric Miaocb990622010-11-02 05:10:07 +080085 struct snd_soc_codec *codec = rtd->codec;
Liam Girdwood1b49cb92006-10-12 14:33:09 +020086
Mark Brown71a29562010-11-05 13:50:48 -040087 mutex_lock(&codec->mutex);
88
Liam Girdwood1b49cb92006-10-12 14:33:09 +020089 /* check the jack status at stream startup */
90 tosa_ext_control(codec);
Mark Brown71a29562010-11-05 13:50:48 -040091
92 mutex_unlock(&codec->mutex);
93
Liam Girdwood1b49cb92006-10-12 14:33:09 +020094 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 */
Jarkko Nikula338c7ed2008-02-28 12:34:48 +0100142static int tosa_hp_event(struct snd_soc_dapm_widget *w,
143 struct snd_kcontrol *k, int event)
Liam Girdwood1b49cb92006-10-12 14:33:09 +0200144{
Dmitry Baryshkov4440cbd2008-06-14 11:42:57 +0100145 gpio_set_value(TOSA_GPIO_L_MUTE, SND_SOC_DAPM_EVENT_ON(event) ? 1 :0);
Liam Girdwood1b49cb92006-10-12 14:33:09 +0200146 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 */
Dmitry Baryshkov76d39d02008-07-08 19:45:20 +0400158static const struct snd_soc_dapm_route audio_map[] = {
Liam Girdwood1b49cb92006-10-12 14:33:09 +0200159
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"},
Liam Girdwood1b49cb92006-10-12 14:33:09 +0200177};
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
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000194static int tosa_ac97_init(struct snd_soc_pcm_runtime *rtd)
Liam Girdwood1b49cb92006-10-12 14:33:09 +0200195{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000196 struct snd_soc_codec *codec = rtd->codec;
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200197 struct snd_soc_dapm_context *dapm = &codec->dapm;
Philipp Zabeleb5f6d752009-03-12 11:07:54 +0100198 int err;
Liam Girdwood1b49cb92006-10-12 14:33:09 +0200199
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200200 snd_soc_dapm_nc_pin(dapm, "OUT3");
201 snd_soc_dapm_nc_pin(dapm, "MONOOUT");
Liam Girdwood1b49cb92006-10-12 14:33:09 +0200202
203 /* add tosa specific controls */
Liam Girdwood022658b2012-02-03 17:43:09 +0000204 err = snd_soc_add_codec_controls(codec, tosa_controls,
Philipp Zabeleb5f6d752009-03-12 11:07:54 +0100205 ARRAY_SIZE(tosa_controls));
206 if (err < 0)
207 return err;
Liam Girdwood1b49cb92006-10-12 14:33:09 +0200208
209 /* add tosa specific widgets */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200210 snd_soc_dapm_new_controls(dapm, tosa_dapm_widgets,
Mark Brown25191c42008-05-13 14:55:48 +0200211 ARRAY_SIZE(tosa_dapm_widgets));
Liam Girdwood1b49cb92006-10-12 14:33:09 +0200212
213 /* set up tosa specific audio path audio_map */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200214 snd_soc_dapm_add_routes(dapm, audio_map, ARRAY_SIZE(audio_map));
Liam Girdwood1b49cb92006-10-12 14:33:09 +0200215
Liam Girdwood1b49cb92006-10-12 14:33:09 +0200216 return 0;
217}
218
219static struct snd_soc_dai_link tosa_dai[] = {
220{
221 .name = "AC97",
222 .stream_name = "AC97 HiFi",
Dmitry Eremin-Solenikov4bfc4e22011-02-23 02:29:11 +0300223 .cpu_dai_name = "pxa2xx-ac97",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000224 .codec_dai_name = "wm9712-hifi",
225 .platform_name = "pxa-pcm-audio",
226 .codec_name = "wm9712-codec",
Liam Girdwood1b49cb92006-10-12 14:33:09 +0200227 .init = tosa_ac97_init,
Liam Girdwoodcb4c0482007-02-02 17:23:11 +0100228 .ops = &tosa_ops,
Liam Girdwood1b49cb92006-10-12 14:33:09 +0200229},
230{
231 .name = "AC97 Aux",
232 .stream_name = "AC97 Aux",
Dmitry Eremin-Solenikov4bfc4e22011-02-23 02:29:11 +0300233 .cpu_dai_name = "pxa2xx-ac97-aux",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000234 .codec_dai_name = "wm9712-aux",
235 .platform_name = "pxa-pcm-audio",
236 .codec_name = "wm9712-codec",
Liam Girdwoodcb4c0482007-02-02 17:23:11 +0100237 .ops = &tosa_ops,
Liam Girdwood1b49cb92006-10-12 14:33:09 +0200238},
239};
240
Mark Brown87506542008-11-18 20:50:34 +0000241static struct snd_soc_card tosa = {
Liam Girdwood1b49cb92006-10-12 14:33:09 +0200242 .name = "Tosa",
Axel Lin561c6a12011-12-22 09:44:43 +0800243 .owner = THIS_MODULE,
Liam Girdwood1b49cb92006-10-12 14:33:09 +0200244 .dai_link = tosa_dai,
245 .num_links = ARRAY_SIZE(tosa_dai),
Liam Girdwood1b49cb92006-10-12 14:33:09 +0200246};
247
Bill Pemberton570f6fe2012-12-07 09:26:17 -0500248static int tosa_probe(struct platform_device *pdev)
Liam Girdwood1b49cb92006-10-12 14:33:09 +0200249{
Axel Linf285b8c2011-12-15 10:57:22 +0800250 struct snd_soc_card *card = &tosa;
Liam Girdwood1b49cb92006-10-12 14:33:09 +0200251 int ret;
252
Axel Linf285b8c2011-12-15 10:57:22 +0800253 ret = gpio_request_one(TOSA_GPIO_L_MUTE, GPIOF_OUT_INIT_LOW,
254 "Headphone Jack");
255 if (ret)
256 return ret;
Liam Girdwood1b49cb92006-10-12 14:33:09 +0200257
Axel Linf285b8c2011-12-15 10:57:22 +0800258 card->dev = &pdev->dev;
259
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);
Dmitry Baryshkov4440cbd2008-06-14 11:42:57 +0100265 }
Liam Girdwood1b49cb92006-10-12 14:33:09 +0200266 return ret;
267}
268
Bill Pemberton570f6fe2012-12-07 09:26:17 -0500269static int tosa_remove(struct platform_device *pdev)
Liam Girdwood1b49cb92006-10-12 14:33:09 +0200270{
Axel Linf285b8c2011-12-15 10:57:22 +0800271 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;
Liam Girdwood1b49cb92006-10-12 14:33:09 +0200276}
277
Axel Linf285b8c2011-12-15 10:57:22 +0800278static struct platform_driver tosa_driver = {
279 .driver = {
280 .name = "tosa-audio",
281 .owner = THIS_MODULE,
Dmitry Eremin-Solenikov7db16982013-10-17 14:01:37 +0400282 .pm = &snd_soc_pm_ops,
Axel Linf285b8c2011-12-15 10:57:22 +0800283 },
284 .probe = tosa_probe,
Bill Pemberton570f6fe2012-12-07 09:26:17 -0500285 .remove = tosa_remove,
Axel Linf285b8c2011-12-15 10:57:22 +0800286};
287
288module_platform_driver(tosa_driver);
Liam Girdwood1b49cb92006-10-12 14:33:09 +0200289
290/* Module information */
291MODULE_AUTHOR("Richard Purdie");
292MODULE_DESCRIPTION("ALSA SoC Tosa");
293MODULE_LICENSE("GPL");
Axel Linf285b8c2011-12-15 10:57:22 +0800294MODULE_ALIAS("platform:tosa-audio");