blob: 8b0df330b487128a120d4fab24b62f9be170d7e5 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Liam Girdwood1b49cb92006-10-12 14:33:09 +02002/*
3 * tosa.c -- SoC audio for Tosa
4 *
5 * Copyright 2005 Wolfson Microelectronics PLC.
6 * Copyright 2005 Openedhand Ltd.
7 *
Liam Girdwoodd3311242008-10-12 13:17:36 +01008 * Authors: Liam Girdwood <lrg@slimlogic.co.uk>
Liam Girdwood1b49cb92006-10-12 14:33:09 +02009 * Richard Purdie <richard@openedhand.com>
10 *
Liam Girdwood1b49cb92006-10-12 14:33:09 +020011 * GPIO's
12 * 1 - Jack Insertion
13 * 5 - Hookswitch (headset answer/hang up switch)
Liam Girdwood1b49cb92006-10-12 14:33:09 +020014 */
15
16#include <linux/module.h>
17#include <linux/moduleparam.h>
18#include <linux/device.h>
Dmitry Baryshkov4440cbd2008-06-14 11:42:57 +010019#include <linux/gpio.h>
Liam Girdwood1b49cb92006-10-12 14:33:09 +020020
Liam Girdwood1b49cb92006-10-12 14:33:09 +020021#include <sound/core.h>
22#include <sound/pcm.h>
23#include <sound/soc.h>
Liam Girdwood1b49cb92006-10-12 14:33:09 +020024
25#include <asm/mach-types.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010026#include <mach/tosa.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010027#include <mach/audio.h>
Liam Girdwood1b49cb92006-10-12 14:33:09 +020028
Liam Girdwood1b49cb92006-10-12 14:33:09 +020029#define TOSA_HP 0
30#define TOSA_MIC_INT 1
31#define TOSA_HEADSET 2
32#define TOSA_HP_OFF 3
33#define TOSA_SPK_ON 0
34#define TOSA_SPK_OFF 1
35
36static int tosa_jack_func;
37static int tosa_spk_func;
38
Lars-Peter Clausen079942a2014-03-12 15:27:39 +010039static void tosa_ext_control(struct snd_soc_dapm_context *dapm)
Liam Girdwood1b49cb92006-10-12 14:33:09 +020040{
Liam Girdwoodce6120c2010-11-05 15:53:46 +020041
Charles Keepax26e24dd2014-02-18 15:22:28 +000042 snd_soc_dapm_mutex_lock(dapm);
43
Liam Girdwood1b49cb92006-10-12 14:33:09 +020044 /* set up jack connection */
45 switch (tosa_jack_func) {
46 case TOSA_HP:
Charles Keepax26e24dd2014-02-18 15:22:28 +000047 snd_soc_dapm_disable_pin_unlocked(dapm, "Mic (Internal)");
48 snd_soc_dapm_enable_pin_unlocked(dapm, "Headphone Jack");
49 snd_soc_dapm_disable_pin_unlocked(dapm, "Headset Jack");
Liam Girdwood1b49cb92006-10-12 14:33:09 +020050 break;
51 case TOSA_MIC_INT:
Charles Keepax26e24dd2014-02-18 15:22:28 +000052 snd_soc_dapm_enable_pin_unlocked(dapm, "Mic (Internal)");
53 snd_soc_dapm_disable_pin_unlocked(dapm, "Headphone Jack");
54 snd_soc_dapm_disable_pin_unlocked(dapm, "Headset Jack");
Liam Girdwood1b49cb92006-10-12 14:33:09 +020055 break;
56 case TOSA_HEADSET:
Charles Keepax26e24dd2014-02-18 15:22:28 +000057 snd_soc_dapm_disable_pin_unlocked(dapm, "Mic (Internal)");
58 snd_soc_dapm_disable_pin_unlocked(dapm, "Headphone Jack");
59 snd_soc_dapm_enable_pin_unlocked(dapm, "Headset Jack");
Liam Girdwood1b49cb92006-10-12 14:33:09 +020060 break;
61 }
62
63 if (tosa_spk_func == TOSA_SPK_ON)
Charles Keepax26e24dd2014-02-18 15:22:28 +000064 snd_soc_dapm_enable_pin_unlocked(dapm, "Speaker");
Liam Girdwooda5302182008-07-07 13:35:17 +010065 else
Charles Keepax26e24dd2014-02-18 15:22:28 +000066 snd_soc_dapm_disable_pin_unlocked(dapm, "Speaker");
Liam Girdwood1b49cb92006-10-12 14:33:09 +020067
Charles Keepax26e24dd2014-02-18 15:22:28 +000068 snd_soc_dapm_sync_unlocked(dapm);
69
70 snd_soc_dapm_mutex_unlock(dapm);
Liam Girdwood1b49cb92006-10-12 14:33:09 +020071}
72
73static int tosa_startup(struct snd_pcm_substream *substream)
74{
75 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwood1b49cb92006-10-12 14:33:09 +020076
77 /* check the jack status at stream startup */
Lars-Peter Clausen079942a2014-03-12 15:27:39 +010078 tosa_ext_control(&rtd->card->dapm);
Mark Brown71a29562010-11-05 13:50:48 -040079
Liam Girdwood1b49cb92006-10-12 14:33:09 +020080 return 0;
81}
82
Bhumika Goyal943b7312017-03-14 01:16:40 +053083static const struct snd_soc_ops tosa_ops = {
Liam Girdwood1b49cb92006-10-12 14:33:09 +020084 .startup = tosa_startup,
85};
86
87static int tosa_get_jack(struct snd_kcontrol *kcontrol,
88 struct snd_ctl_elem_value *ucontrol)
89{
Takashi Iwai419396d2016-02-29 17:23:53 +010090 ucontrol->value.enumerated.item[0] = tosa_jack_func;
Liam Girdwood1b49cb92006-10-12 14:33:09 +020091 return 0;
92}
93
94static int tosa_set_jack(struct snd_kcontrol *kcontrol,
95 struct snd_ctl_elem_value *ucontrol)
96{
Lars-Peter Clausen079942a2014-03-12 15:27:39 +010097 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Liam Girdwood1b49cb92006-10-12 14:33:09 +020098
Takashi Iwai419396d2016-02-29 17:23:53 +010099 if (tosa_jack_func == ucontrol->value.enumerated.item[0])
Liam Girdwood1b49cb92006-10-12 14:33:09 +0200100 return 0;
101
Takashi Iwai419396d2016-02-29 17:23:53 +0100102 tosa_jack_func = ucontrol->value.enumerated.item[0];
Lars-Peter Clausen079942a2014-03-12 15:27:39 +0100103 tosa_ext_control(&card->dapm);
Liam Girdwood1b49cb92006-10-12 14:33:09 +0200104 return 1;
105}
106
107static int tosa_get_spk(struct snd_kcontrol *kcontrol,
108 struct snd_ctl_elem_value *ucontrol)
109{
Takashi Iwai419396d2016-02-29 17:23:53 +0100110 ucontrol->value.enumerated.item[0] = tosa_spk_func;
Liam Girdwood1b49cb92006-10-12 14:33:09 +0200111 return 0;
112}
113
114static int tosa_set_spk(struct snd_kcontrol *kcontrol,
115 struct snd_ctl_elem_value *ucontrol)
116{
Lars-Peter Clausen079942a2014-03-12 15:27:39 +0100117 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Liam Girdwood1b49cb92006-10-12 14:33:09 +0200118
Takashi Iwai419396d2016-02-29 17:23:53 +0100119 if (tosa_spk_func == ucontrol->value.enumerated.item[0])
Liam Girdwood1b49cb92006-10-12 14:33:09 +0200120 return 0;
121
Takashi Iwai419396d2016-02-29 17:23:53 +0100122 tosa_spk_func = ucontrol->value.enumerated.item[0];
Lars-Peter Clausen079942a2014-03-12 15:27:39 +0100123 tosa_ext_control(&card->dapm);
Liam Girdwood1b49cb92006-10-12 14:33:09 +0200124 return 1;
125}
126
127/* tosa dapm event handlers */
Jarkko Nikula338c7ed2008-02-28 12:34:48 +0100128static int tosa_hp_event(struct snd_soc_dapm_widget *w,
129 struct snd_kcontrol *k, int event)
Liam Girdwood1b49cb92006-10-12 14:33:09 +0200130{
Codrut Grosuca87cfa2017-02-25 22:42:33 +0200131 gpio_set_value(TOSA_GPIO_L_MUTE, SND_SOC_DAPM_EVENT_ON(event) ? 1 : 0);
Liam Girdwood1b49cb92006-10-12 14:33:09 +0200132 return 0;
133}
134
135/* tosa machine dapm widgets */
136static const struct snd_soc_dapm_widget tosa_dapm_widgets[] = {
137SND_SOC_DAPM_HP("Headphone Jack", tosa_hp_event),
138SND_SOC_DAPM_HP("Headset Jack", NULL),
139SND_SOC_DAPM_MIC("Mic (Internal)", NULL),
140SND_SOC_DAPM_SPK("Speaker", NULL),
141};
142
143/* tosa audio map */
Dmitry Baryshkov76d39d02008-07-08 19:45:20 +0400144static const struct snd_soc_dapm_route audio_map[] = {
Liam Girdwood1b49cb92006-10-12 14:33:09 +0200145
146 /* headphone connected to HPOUTL, HPOUTR */
147 {"Headphone Jack", NULL, "HPOUTL"},
148 {"Headphone Jack", NULL, "HPOUTR"},
149
150 /* ext speaker connected to LOUT2, ROUT2 */
151 {"Speaker", NULL, "LOUT2"},
152 {"Speaker", NULL, "ROUT2"},
153
154 /* internal mic is connected to mic1, mic2 differential - with bias */
155 {"MIC1", NULL, "Mic Bias"},
156 {"MIC2", NULL, "Mic Bias"},
157 {"Mic Bias", NULL, "Mic (Internal)"},
158
159 /* headset is connected to HPOUTR, and LINEINR with bias */
160 {"Headset Jack", NULL, "HPOUTR"},
161 {"LINEINR", NULL, "Mic Bias"},
162 {"Mic Bias", NULL, "Headset Jack"},
Liam Girdwood1b49cb92006-10-12 14:33:09 +0200163};
164
Lars-Peter Clausen92be5812016-11-12 14:23:13 +0100165static const char * const jack_function[] = {"Headphone", "Mic", "Line",
166 "Headset", "Off"};
167static const char * const spk_function[] = {"On", "Off"};
Liam Girdwood1b49cb92006-10-12 14:33:09 +0200168static const struct soc_enum tosa_enum[] = {
169 SOC_ENUM_SINGLE_EXT(5, jack_function),
170 SOC_ENUM_SINGLE_EXT(2, spk_function),
171};
172
173static const struct snd_kcontrol_new tosa_controls[] = {
174 SOC_ENUM_EXT("Jack Function", tosa_enum[0], tosa_get_jack,
175 tosa_set_jack),
176 SOC_ENUM_EXT("Speaker Function", tosa_enum[1], tosa_get_spk,
177 tosa_set_spk),
178};
179
Liam Girdwood1b49cb92006-10-12 14:33:09 +0200180static struct snd_soc_dai_link tosa_dai[] = {
181{
182 .name = "AC97",
183 .stream_name = "AC97 HiFi",
Dmitry Eremin-Solenikov4bfc4e22011-02-23 02:29:11 +0300184 .cpu_dai_name = "pxa2xx-ac97",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000185 .codec_dai_name = "wm9712-hifi",
186 .platform_name = "pxa-pcm-audio",
187 .codec_name = "wm9712-codec",
Liam Girdwoodcb4c0482007-02-02 17:23:11 +0100188 .ops = &tosa_ops,
Liam Girdwood1b49cb92006-10-12 14:33:09 +0200189},
190{
191 .name = "AC97 Aux",
192 .stream_name = "AC97 Aux",
Dmitry Eremin-Solenikov4bfc4e22011-02-23 02:29:11 +0300193 .cpu_dai_name = "pxa2xx-ac97-aux",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000194 .codec_dai_name = "wm9712-aux",
195 .platform_name = "pxa-pcm-audio",
196 .codec_name = "wm9712-codec",
Liam Girdwoodcb4c0482007-02-02 17:23:11 +0100197 .ops = &tosa_ops,
Liam Girdwood1b49cb92006-10-12 14:33:09 +0200198},
199};
200
Mark Brown87506542008-11-18 20:50:34 +0000201static struct snd_soc_card tosa = {
Liam Girdwood1b49cb92006-10-12 14:33:09 +0200202 .name = "Tosa",
Axel Lin561c6a12011-12-22 09:44:43 +0800203 .owner = THIS_MODULE,
Liam Girdwood1b49cb92006-10-12 14:33:09 +0200204 .dai_link = tosa_dai,
205 .num_links = ARRAY_SIZE(tosa_dai),
Lars-Peter Clausen079942a2014-03-12 15:27:39 +0100206
207 .controls = tosa_controls,
208 .num_controls = ARRAY_SIZE(tosa_controls),
209 .dapm_widgets = tosa_dapm_widgets,
210 .num_dapm_widgets = ARRAY_SIZE(tosa_dapm_widgets),
211 .dapm_routes = audio_map,
212 .num_dapm_routes = ARRAY_SIZE(audio_map),
Lars-Peter Clausenc02e7232015-05-07 21:27:48 +0200213 .fully_routed = true,
Liam Girdwood1b49cb92006-10-12 14:33:09 +0200214};
215
Bill Pemberton570f6fe2012-12-07 09:26:17 -0500216static int tosa_probe(struct platform_device *pdev)
Liam Girdwood1b49cb92006-10-12 14:33:09 +0200217{
Axel Linf285b8c2011-12-15 10:57:22 +0800218 struct snd_soc_card *card = &tosa;
Liam Girdwood1b49cb92006-10-12 14:33:09 +0200219 int ret;
220
Axel Linf285b8c2011-12-15 10:57:22 +0800221 ret = gpio_request_one(TOSA_GPIO_L_MUTE, GPIOF_OUT_INIT_LOW,
222 "Headphone Jack");
223 if (ret)
224 return ret;
Liam Girdwood1b49cb92006-10-12 14:33:09 +0200225
Axel Linf285b8c2011-12-15 10:57:22 +0800226 card->dev = &pdev->dev;
227
Axel Lin2fd70762015-09-01 10:32:59 +0800228 ret = devm_snd_soc_register_card(&pdev->dev, card);
Axel Linf285b8c2011-12-15 10:57:22 +0800229 if (ret) {
230 dev_err(&pdev->dev, "snd_soc_register_card() failed: %d\n",
231 ret);
232 gpio_free(TOSA_GPIO_L_MUTE);
Dmitry Baryshkov4440cbd2008-06-14 11:42:57 +0100233 }
Liam Girdwood1b49cb92006-10-12 14:33:09 +0200234 return ret;
235}
236
Bill Pemberton570f6fe2012-12-07 09:26:17 -0500237static int tosa_remove(struct platform_device *pdev)
Liam Girdwood1b49cb92006-10-12 14:33:09 +0200238{
Axel Linf285b8c2011-12-15 10:57:22 +0800239 gpio_free(TOSA_GPIO_L_MUTE);
Axel Linf285b8c2011-12-15 10:57:22 +0800240 return 0;
Liam Girdwood1b49cb92006-10-12 14:33:09 +0200241}
242
Axel Linf285b8c2011-12-15 10:57:22 +0800243static struct platform_driver tosa_driver = {
244 .driver = {
245 .name = "tosa-audio",
Dmitry Eremin-Solenikov7db16982013-10-17 14:01:37 +0400246 .pm = &snd_soc_pm_ops,
Axel Linf285b8c2011-12-15 10:57:22 +0800247 },
248 .probe = tosa_probe,
Bill Pemberton570f6fe2012-12-07 09:26:17 -0500249 .remove = tosa_remove,
Axel Linf285b8c2011-12-15 10:57:22 +0800250};
251
252module_platform_driver(tosa_driver);
Liam Girdwood1b49cb92006-10-12 14:33:09 +0200253
254/* Module information */
255MODULE_AUTHOR("Richard Purdie");
256MODULE_DESCRIPTION("ALSA SoC Tosa");
257MODULE_LICENSE("GPL");
Axel Linf285b8c2011-12-15 10:57:22 +0800258MODULE_ALIAS("platform:tosa-audio");