Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Liam Girdwood | 1b49cb9 | 2006-10-12 14:33:09 +0200 | [diff] [blame] | 2 | /* |
| 3 | * tosa.c -- SoC audio for Tosa |
| 4 | * |
| 5 | * Copyright 2005 Wolfson Microelectronics PLC. |
| 6 | * Copyright 2005 Openedhand Ltd. |
| 7 | * |
Liam Girdwood | d331124 | 2008-10-12 13:17:36 +0100 | [diff] [blame] | 8 | * Authors: Liam Girdwood <lrg@slimlogic.co.uk> |
Liam Girdwood | 1b49cb9 | 2006-10-12 14:33:09 +0200 | [diff] [blame] | 9 | * Richard Purdie <richard@openedhand.com> |
| 10 | * |
Liam Girdwood | 1b49cb9 | 2006-10-12 14:33:09 +0200 | [diff] [blame] | 11 | * GPIO's |
| 12 | * 1 - Jack Insertion |
| 13 | * 5 - Hookswitch (headset answer/hang up switch) |
Liam Girdwood | 1b49cb9 | 2006-10-12 14:33:09 +0200 | [diff] [blame] | 14 | */ |
| 15 | |
| 16 | #include <linux/module.h> |
| 17 | #include <linux/moduleparam.h> |
| 18 | #include <linux/device.h> |
Dmitry Baryshkov | 4440cbd | 2008-06-14 11:42:57 +0100 | [diff] [blame] | 19 | #include <linux/gpio.h> |
Liam Girdwood | 1b49cb9 | 2006-10-12 14:33:09 +0200 | [diff] [blame] | 20 | |
Liam Girdwood | 1b49cb9 | 2006-10-12 14:33:09 +0200 | [diff] [blame] | 21 | #include <sound/core.h> |
| 22 | #include <sound/pcm.h> |
| 23 | #include <sound/soc.h> |
Liam Girdwood | 1b49cb9 | 2006-10-12 14:33:09 +0200 | [diff] [blame] | 24 | |
| 25 | #include <asm/mach-types.h> |
Russell King | a09e64f | 2008-08-05 16:14:15 +0100 | [diff] [blame] | 26 | #include <mach/tosa.h> |
Russell King | a09e64f | 2008-08-05 16:14:15 +0100 | [diff] [blame] | 27 | #include <mach/audio.h> |
Liam Girdwood | 1b49cb9 | 2006-10-12 14:33:09 +0200 | [diff] [blame] | 28 | |
Liam Girdwood | 1b49cb9 | 2006-10-12 14:33:09 +0200 | [diff] [blame] | 29 | #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 | |
| 36 | static int tosa_jack_func; |
| 37 | static int tosa_spk_func; |
| 38 | |
Lars-Peter Clausen | 079942a | 2014-03-12 15:27:39 +0100 | [diff] [blame] | 39 | static void tosa_ext_control(struct snd_soc_dapm_context *dapm) |
Liam Girdwood | 1b49cb9 | 2006-10-12 14:33:09 +0200 | [diff] [blame] | 40 | { |
Liam Girdwood | ce6120c | 2010-11-05 15:53:46 +0200 | [diff] [blame] | 41 | |
Charles Keepax | 26e24dd | 2014-02-18 15:22:28 +0000 | [diff] [blame] | 42 | snd_soc_dapm_mutex_lock(dapm); |
| 43 | |
Liam Girdwood | 1b49cb9 | 2006-10-12 14:33:09 +0200 | [diff] [blame] | 44 | /* set up jack connection */ |
| 45 | switch (tosa_jack_func) { |
| 46 | case TOSA_HP: |
Charles Keepax | 26e24dd | 2014-02-18 15:22:28 +0000 | [diff] [blame] | 47 | 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 Girdwood | 1b49cb9 | 2006-10-12 14:33:09 +0200 | [diff] [blame] | 50 | break; |
| 51 | case TOSA_MIC_INT: |
Charles Keepax | 26e24dd | 2014-02-18 15:22:28 +0000 | [diff] [blame] | 52 | 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 Girdwood | 1b49cb9 | 2006-10-12 14:33:09 +0200 | [diff] [blame] | 55 | break; |
| 56 | case TOSA_HEADSET: |
Charles Keepax | 26e24dd | 2014-02-18 15:22:28 +0000 | [diff] [blame] | 57 | 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 Girdwood | 1b49cb9 | 2006-10-12 14:33:09 +0200 | [diff] [blame] | 60 | break; |
| 61 | } |
| 62 | |
| 63 | if (tosa_spk_func == TOSA_SPK_ON) |
Charles Keepax | 26e24dd | 2014-02-18 15:22:28 +0000 | [diff] [blame] | 64 | snd_soc_dapm_enable_pin_unlocked(dapm, "Speaker"); |
Liam Girdwood | a530218 | 2008-07-07 13:35:17 +0100 | [diff] [blame] | 65 | else |
Charles Keepax | 26e24dd | 2014-02-18 15:22:28 +0000 | [diff] [blame] | 66 | snd_soc_dapm_disable_pin_unlocked(dapm, "Speaker"); |
Liam Girdwood | 1b49cb9 | 2006-10-12 14:33:09 +0200 | [diff] [blame] | 67 | |
Charles Keepax | 26e24dd | 2014-02-18 15:22:28 +0000 | [diff] [blame] | 68 | snd_soc_dapm_sync_unlocked(dapm); |
| 69 | |
| 70 | snd_soc_dapm_mutex_unlock(dapm); |
Liam Girdwood | 1b49cb9 | 2006-10-12 14:33:09 +0200 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | static int tosa_startup(struct snd_pcm_substream *substream) |
| 74 | { |
| 75 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
Liam Girdwood | 1b49cb9 | 2006-10-12 14:33:09 +0200 | [diff] [blame] | 76 | |
| 77 | /* check the jack status at stream startup */ |
Lars-Peter Clausen | 079942a | 2014-03-12 15:27:39 +0100 | [diff] [blame] | 78 | tosa_ext_control(&rtd->card->dapm); |
Mark Brown | 71a2956 | 2010-11-05 13:50:48 -0400 | [diff] [blame] | 79 | |
Liam Girdwood | 1b49cb9 | 2006-10-12 14:33:09 +0200 | [diff] [blame] | 80 | return 0; |
| 81 | } |
| 82 | |
Bhumika Goyal | 943b731 | 2017-03-14 01:16:40 +0530 | [diff] [blame] | 83 | static const struct snd_soc_ops tosa_ops = { |
Liam Girdwood | 1b49cb9 | 2006-10-12 14:33:09 +0200 | [diff] [blame] | 84 | .startup = tosa_startup, |
| 85 | }; |
| 86 | |
| 87 | static int tosa_get_jack(struct snd_kcontrol *kcontrol, |
| 88 | struct snd_ctl_elem_value *ucontrol) |
| 89 | { |
Takashi Iwai | 419396d | 2016-02-29 17:23:53 +0100 | [diff] [blame] | 90 | ucontrol->value.enumerated.item[0] = tosa_jack_func; |
Liam Girdwood | 1b49cb9 | 2006-10-12 14:33:09 +0200 | [diff] [blame] | 91 | return 0; |
| 92 | } |
| 93 | |
| 94 | static int tosa_set_jack(struct snd_kcontrol *kcontrol, |
| 95 | struct snd_ctl_elem_value *ucontrol) |
| 96 | { |
Lars-Peter Clausen | 079942a | 2014-03-12 15:27:39 +0100 | [diff] [blame] | 97 | struct snd_soc_card *card = snd_kcontrol_chip(kcontrol); |
Liam Girdwood | 1b49cb9 | 2006-10-12 14:33:09 +0200 | [diff] [blame] | 98 | |
Takashi Iwai | 419396d | 2016-02-29 17:23:53 +0100 | [diff] [blame] | 99 | if (tosa_jack_func == ucontrol->value.enumerated.item[0]) |
Liam Girdwood | 1b49cb9 | 2006-10-12 14:33:09 +0200 | [diff] [blame] | 100 | return 0; |
| 101 | |
Takashi Iwai | 419396d | 2016-02-29 17:23:53 +0100 | [diff] [blame] | 102 | tosa_jack_func = ucontrol->value.enumerated.item[0]; |
Lars-Peter Clausen | 079942a | 2014-03-12 15:27:39 +0100 | [diff] [blame] | 103 | tosa_ext_control(&card->dapm); |
Liam Girdwood | 1b49cb9 | 2006-10-12 14:33:09 +0200 | [diff] [blame] | 104 | return 1; |
| 105 | } |
| 106 | |
| 107 | static int tosa_get_spk(struct snd_kcontrol *kcontrol, |
| 108 | struct snd_ctl_elem_value *ucontrol) |
| 109 | { |
Takashi Iwai | 419396d | 2016-02-29 17:23:53 +0100 | [diff] [blame] | 110 | ucontrol->value.enumerated.item[0] = tosa_spk_func; |
Liam Girdwood | 1b49cb9 | 2006-10-12 14:33:09 +0200 | [diff] [blame] | 111 | return 0; |
| 112 | } |
| 113 | |
| 114 | static int tosa_set_spk(struct snd_kcontrol *kcontrol, |
| 115 | struct snd_ctl_elem_value *ucontrol) |
| 116 | { |
Lars-Peter Clausen | 079942a | 2014-03-12 15:27:39 +0100 | [diff] [blame] | 117 | struct snd_soc_card *card = snd_kcontrol_chip(kcontrol); |
Liam Girdwood | 1b49cb9 | 2006-10-12 14:33:09 +0200 | [diff] [blame] | 118 | |
Takashi Iwai | 419396d | 2016-02-29 17:23:53 +0100 | [diff] [blame] | 119 | if (tosa_spk_func == ucontrol->value.enumerated.item[0]) |
Liam Girdwood | 1b49cb9 | 2006-10-12 14:33:09 +0200 | [diff] [blame] | 120 | return 0; |
| 121 | |
Takashi Iwai | 419396d | 2016-02-29 17:23:53 +0100 | [diff] [blame] | 122 | tosa_spk_func = ucontrol->value.enumerated.item[0]; |
Lars-Peter Clausen | 079942a | 2014-03-12 15:27:39 +0100 | [diff] [blame] | 123 | tosa_ext_control(&card->dapm); |
Liam Girdwood | 1b49cb9 | 2006-10-12 14:33:09 +0200 | [diff] [blame] | 124 | return 1; |
| 125 | } |
| 126 | |
| 127 | /* tosa dapm event handlers */ |
Jarkko Nikula | 338c7ed | 2008-02-28 12:34:48 +0100 | [diff] [blame] | 128 | static int tosa_hp_event(struct snd_soc_dapm_widget *w, |
| 129 | struct snd_kcontrol *k, int event) |
Liam Girdwood | 1b49cb9 | 2006-10-12 14:33:09 +0200 | [diff] [blame] | 130 | { |
Codrut Grosu | ca87cfa | 2017-02-25 22:42:33 +0200 | [diff] [blame] | 131 | gpio_set_value(TOSA_GPIO_L_MUTE, SND_SOC_DAPM_EVENT_ON(event) ? 1 : 0); |
Liam Girdwood | 1b49cb9 | 2006-10-12 14:33:09 +0200 | [diff] [blame] | 132 | return 0; |
| 133 | } |
| 134 | |
| 135 | /* tosa machine dapm widgets */ |
| 136 | static const struct snd_soc_dapm_widget tosa_dapm_widgets[] = { |
| 137 | SND_SOC_DAPM_HP("Headphone Jack", tosa_hp_event), |
| 138 | SND_SOC_DAPM_HP("Headset Jack", NULL), |
| 139 | SND_SOC_DAPM_MIC("Mic (Internal)", NULL), |
| 140 | SND_SOC_DAPM_SPK("Speaker", NULL), |
| 141 | }; |
| 142 | |
| 143 | /* tosa audio map */ |
Dmitry Baryshkov | 76d39d0 | 2008-07-08 19:45:20 +0400 | [diff] [blame] | 144 | static const struct snd_soc_dapm_route audio_map[] = { |
Liam Girdwood | 1b49cb9 | 2006-10-12 14:33:09 +0200 | [diff] [blame] | 145 | |
| 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 Girdwood | 1b49cb9 | 2006-10-12 14:33:09 +0200 | [diff] [blame] | 163 | }; |
| 164 | |
Lars-Peter Clausen | 92be581 | 2016-11-12 14:23:13 +0100 | [diff] [blame] | 165 | static const char * const jack_function[] = {"Headphone", "Mic", "Line", |
| 166 | "Headset", "Off"}; |
| 167 | static const char * const spk_function[] = {"On", "Off"}; |
Liam Girdwood | 1b49cb9 | 2006-10-12 14:33:09 +0200 | [diff] [blame] | 168 | static const struct soc_enum tosa_enum[] = { |
| 169 | SOC_ENUM_SINGLE_EXT(5, jack_function), |
| 170 | SOC_ENUM_SINGLE_EXT(2, spk_function), |
| 171 | }; |
| 172 | |
| 173 | static 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 Girdwood | 1b49cb9 | 2006-10-12 14:33:09 +0200 | [diff] [blame] | 180 | static struct snd_soc_dai_link tosa_dai[] = { |
| 181 | { |
| 182 | .name = "AC97", |
| 183 | .stream_name = "AC97 HiFi", |
Dmitry Eremin-Solenikov | 4bfc4e2 | 2011-02-23 02:29:11 +0300 | [diff] [blame] | 184 | .cpu_dai_name = "pxa2xx-ac97", |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 185 | .codec_dai_name = "wm9712-hifi", |
| 186 | .platform_name = "pxa-pcm-audio", |
| 187 | .codec_name = "wm9712-codec", |
Liam Girdwood | cb4c048 | 2007-02-02 17:23:11 +0100 | [diff] [blame] | 188 | .ops = &tosa_ops, |
Liam Girdwood | 1b49cb9 | 2006-10-12 14:33:09 +0200 | [diff] [blame] | 189 | }, |
| 190 | { |
| 191 | .name = "AC97 Aux", |
| 192 | .stream_name = "AC97 Aux", |
Dmitry Eremin-Solenikov | 4bfc4e2 | 2011-02-23 02:29:11 +0300 | [diff] [blame] | 193 | .cpu_dai_name = "pxa2xx-ac97-aux", |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 194 | .codec_dai_name = "wm9712-aux", |
| 195 | .platform_name = "pxa-pcm-audio", |
| 196 | .codec_name = "wm9712-codec", |
Liam Girdwood | cb4c048 | 2007-02-02 17:23:11 +0100 | [diff] [blame] | 197 | .ops = &tosa_ops, |
Liam Girdwood | 1b49cb9 | 2006-10-12 14:33:09 +0200 | [diff] [blame] | 198 | }, |
| 199 | }; |
| 200 | |
Mark Brown | 8750654 | 2008-11-18 20:50:34 +0000 | [diff] [blame] | 201 | static struct snd_soc_card tosa = { |
Liam Girdwood | 1b49cb9 | 2006-10-12 14:33:09 +0200 | [diff] [blame] | 202 | .name = "Tosa", |
Axel Lin | 561c6a1 | 2011-12-22 09:44:43 +0800 | [diff] [blame] | 203 | .owner = THIS_MODULE, |
Liam Girdwood | 1b49cb9 | 2006-10-12 14:33:09 +0200 | [diff] [blame] | 204 | .dai_link = tosa_dai, |
| 205 | .num_links = ARRAY_SIZE(tosa_dai), |
Lars-Peter Clausen | 079942a | 2014-03-12 15:27:39 +0100 | [diff] [blame] | 206 | |
| 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 Clausen | c02e723 | 2015-05-07 21:27:48 +0200 | [diff] [blame] | 213 | .fully_routed = true, |
Liam Girdwood | 1b49cb9 | 2006-10-12 14:33:09 +0200 | [diff] [blame] | 214 | }; |
| 215 | |
Bill Pemberton | 570f6fe | 2012-12-07 09:26:17 -0500 | [diff] [blame] | 216 | static int tosa_probe(struct platform_device *pdev) |
Liam Girdwood | 1b49cb9 | 2006-10-12 14:33:09 +0200 | [diff] [blame] | 217 | { |
Axel Lin | f285b8c | 2011-12-15 10:57:22 +0800 | [diff] [blame] | 218 | struct snd_soc_card *card = ⤩ |
Liam Girdwood | 1b49cb9 | 2006-10-12 14:33:09 +0200 | [diff] [blame] | 219 | int ret; |
| 220 | |
Axel Lin | f285b8c | 2011-12-15 10:57:22 +0800 | [diff] [blame] | 221 | ret = gpio_request_one(TOSA_GPIO_L_MUTE, GPIOF_OUT_INIT_LOW, |
| 222 | "Headphone Jack"); |
| 223 | if (ret) |
| 224 | return ret; |
Liam Girdwood | 1b49cb9 | 2006-10-12 14:33:09 +0200 | [diff] [blame] | 225 | |
Axel Lin | f285b8c | 2011-12-15 10:57:22 +0800 | [diff] [blame] | 226 | card->dev = &pdev->dev; |
| 227 | |
Axel Lin | 2fd7076 | 2015-09-01 10:32:59 +0800 | [diff] [blame] | 228 | ret = devm_snd_soc_register_card(&pdev->dev, card); |
Axel Lin | f285b8c | 2011-12-15 10:57:22 +0800 | [diff] [blame] | 229 | if (ret) { |
| 230 | dev_err(&pdev->dev, "snd_soc_register_card() failed: %d\n", |
| 231 | ret); |
| 232 | gpio_free(TOSA_GPIO_L_MUTE); |
Dmitry Baryshkov | 4440cbd | 2008-06-14 11:42:57 +0100 | [diff] [blame] | 233 | } |
Liam Girdwood | 1b49cb9 | 2006-10-12 14:33:09 +0200 | [diff] [blame] | 234 | return ret; |
| 235 | } |
| 236 | |
Bill Pemberton | 570f6fe | 2012-12-07 09:26:17 -0500 | [diff] [blame] | 237 | static int tosa_remove(struct platform_device *pdev) |
Liam Girdwood | 1b49cb9 | 2006-10-12 14:33:09 +0200 | [diff] [blame] | 238 | { |
Axel Lin | f285b8c | 2011-12-15 10:57:22 +0800 | [diff] [blame] | 239 | gpio_free(TOSA_GPIO_L_MUTE); |
Axel Lin | f285b8c | 2011-12-15 10:57:22 +0800 | [diff] [blame] | 240 | return 0; |
Liam Girdwood | 1b49cb9 | 2006-10-12 14:33:09 +0200 | [diff] [blame] | 241 | } |
| 242 | |
Axel Lin | f285b8c | 2011-12-15 10:57:22 +0800 | [diff] [blame] | 243 | static struct platform_driver tosa_driver = { |
| 244 | .driver = { |
| 245 | .name = "tosa-audio", |
Dmitry Eremin-Solenikov | 7db1698 | 2013-10-17 14:01:37 +0400 | [diff] [blame] | 246 | .pm = &snd_soc_pm_ops, |
Axel Lin | f285b8c | 2011-12-15 10:57:22 +0800 | [diff] [blame] | 247 | }, |
| 248 | .probe = tosa_probe, |
Bill Pemberton | 570f6fe | 2012-12-07 09:26:17 -0500 | [diff] [blame] | 249 | .remove = tosa_remove, |
Axel Lin | f285b8c | 2011-12-15 10:57:22 +0800 | [diff] [blame] | 250 | }; |
| 251 | |
| 252 | module_platform_driver(tosa_driver); |
Liam Girdwood | 1b49cb9 | 2006-10-12 14:33:09 +0200 | [diff] [blame] | 253 | |
| 254 | /* Module information */ |
| 255 | MODULE_AUTHOR("Richard Purdie"); |
| 256 | MODULE_DESCRIPTION("ALSA SoC Tosa"); |
| 257 | MODULE_LICENSE("GPL"); |
Axel Lin | f285b8c | 2011-12-15 10:57:22 +0800 | [diff] [blame] | 258 | MODULE_ALIAS("platform:tosa-audio"); |