Janusz Krzysztofik | 6d7f68a | 2009-07-29 13:18:53 +0200 | [diff] [blame] | 1 | /* |
| 2 | * ams-delta.c -- SoC audio for Amstrad E3 (Delta) videophone |
| 3 | * |
| 4 | * Copyright (C) 2009 Janusz Krzysztofik <jkrzyszt@tis.icnet.pl> |
| 5 | * |
| 6 | * Initially based on sound/soc/omap/osk5912.x |
| 7 | * Copyright (C) 2008 Mistral Solutions |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or |
| 10 | * modify it under the terms of the GNU General Public License |
| 11 | * version 2 as published by the Free Software Foundation. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, but |
| 14 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 | * General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA |
| 21 | * 02110-1301 USA |
| 22 | * |
| 23 | */ |
| 24 | |
Janusz Krzysztofik | d65777d | 2018-05-18 23:09:51 +0200 | [diff] [blame] | 25 | #include <linux/gpio/consumer.h> |
Janusz Krzysztofik | 6d7f68a | 2009-07-29 13:18:53 +0200 | [diff] [blame] | 26 | #include <linux/spinlock.h> |
| 27 | #include <linux/tty.h> |
Paul Gortmaker | da155d5 | 2011-07-15 12:38:28 -0400 | [diff] [blame] | 28 | #include <linux/module.h> |
Janusz Krzysztofik | 6d7f68a | 2009-07-29 13:18:53 +0200 | [diff] [blame] | 29 | |
Liam Girdwood | ce6120c | 2010-11-05 15:53:46 +0200 | [diff] [blame] | 30 | #include <sound/soc.h> |
Janusz Krzysztofik | 6d7f68a | 2009-07-29 13:18:53 +0200 | [diff] [blame] | 31 | #include <sound/jack.h> |
| 32 | |
| 33 | #include <asm/mach-types.h> |
| 34 | |
Arnd Bergmann | 2203747 | 2012-08-24 15:21:06 +0200 | [diff] [blame] | 35 | #include <linux/platform_data/asoc-ti-mcbsp.h> |
Janusz Krzysztofik | 6d7f68a | 2009-07-29 13:18:53 +0200 | [diff] [blame] | 36 | |
| 37 | #include "omap-mcbsp.h" |
Janusz Krzysztofik | 6d7f68a | 2009-07-29 13:18:53 +0200 | [diff] [blame] | 38 | #include "../codecs/cx20442.h" |
| 39 | |
Janusz Krzysztofik | 6d7f68a | 2009-07-29 13:18:53 +0200 | [diff] [blame] | 40 | /* Board specific DAPM widgets */ |
Janusz Krzysztofik | 0262462 | 2009-10-21 04:40:55 +0200 | [diff] [blame] | 41 | static const struct snd_soc_dapm_widget ams_delta_dapm_widgets[] = { |
Janusz Krzysztofik | 6d7f68a | 2009-07-29 13:18:53 +0200 | [diff] [blame] | 42 | /* Handset */ |
| 43 | SND_SOC_DAPM_MIC("Mouthpiece", NULL), |
| 44 | SND_SOC_DAPM_HP("Earpiece", NULL), |
| 45 | /* Handsfree/Speakerphone */ |
| 46 | SND_SOC_DAPM_MIC("Microphone", NULL), |
| 47 | SND_SOC_DAPM_SPK("Speaker", NULL), |
| 48 | }; |
| 49 | |
| 50 | /* How they are connected to codec pins */ |
| 51 | static const struct snd_soc_dapm_route ams_delta_audio_map[] = { |
| 52 | {"TELIN", NULL, "Mouthpiece"}, |
| 53 | {"Earpiece", NULL, "TELOUT"}, |
| 54 | |
| 55 | {"MIC", NULL, "Microphone"}, |
| 56 | {"Speaker", NULL, "SPKOUT"}, |
| 57 | }; |
| 58 | |
| 59 | /* |
| 60 | * Controls, functional after the modem line discipline is activated. |
| 61 | */ |
| 62 | |
| 63 | /* Virtual switch: audio input/output constellations */ |
| 64 | static const char *ams_delta_audio_mode[] = |
| 65 | {"Mixed", "Handset", "Handsfree", "Speakerphone"}; |
| 66 | |
| 67 | /* Selection <-> pin translation */ |
| 68 | #define AMS_DELTA_MOUTHPIECE 0 |
| 69 | #define AMS_DELTA_EARPIECE 1 |
| 70 | #define AMS_DELTA_MICROPHONE 2 |
| 71 | #define AMS_DELTA_SPEAKER 3 |
| 72 | #define AMS_DELTA_AGC 4 |
| 73 | |
| 74 | #define AMS_DELTA_MIXED ((1 << AMS_DELTA_EARPIECE) | \ |
| 75 | (1 << AMS_DELTA_MICROPHONE)) |
| 76 | #define AMS_DELTA_HANDSET ((1 << AMS_DELTA_MOUTHPIECE) | \ |
| 77 | (1 << AMS_DELTA_EARPIECE)) |
| 78 | #define AMS_DELTA_HANDSFREE ((1 << AMS_DELTA_MICROPHONE) | \ |
| 79 | (1 << AMS_DELTA_SPEAKER)) |
| 80 | #define AMS_DELTA_SPEAKERPHONE (AMS_DELTA_HANDSFREE | (1 << AMS_DELTA_AGC)) |
| 81 | |
Janusz Krzysztofik | 0262462 | 2009-10-21 04:40:55 +0200 | [diff] [blame] | 82 | static const unsigned short ams_delta_audio_mode_pins[] = { |
Janusz Krzysztofik | 6d7f68a | 2009-07-29 13:18:53 +0200 | [diff] [blame] | 83 | AMS_DELTA_MIXED, |
| 84 | AMS_DELTA_HANDSET, |
| 85 | AMS_DELTA_HANDSFREE, |
| 86 | AMS_DELTA_SPEAKERPHONE, |
| 87 | }; |
| 88 | |
| 89 | static unsigned short ams_delta_audio_agc; |
| 90 | |
Lars-Peter Clausen | 74a1672 | 2014-03-12 15:27:30 +0100 | [diff] [blame] | 91 | /* |
| 92 | * Used for passing a codec structure pointer |
| 93 | * from the board initialization code to the tty line discipline. |
| 94 | */ |
Kuninori Morimoto | d0fdfe3 | 2018-01-29 04:15:57 +0000 | [diff] [blame] | 95 | static struct snd_soc_component *cx20442_codec; |
Lars-Peter Clausen | 74a1672 | 2014-03-12 15:27:30 +0100 | [diff] [blame] | 96 | |
Janusz Krzysztofik | 6d7f68a | 2009-07-29 13:18:53 +0200 | [diff] [blame] | 97 | static int ams_delta_set_audio_mode(struct snd_kcontrol *kcontrol, |
| 98 | struct snd_ctl_elem_value *ucontrol) |
| 99 | { |
Lars-Peter Clausen | 74a1672 | 2014-03-12 15:27:30 +0100 | [diff] [blame] | 100 | struct snd_soc_card *card = snd_kcontrol_chip(kcontrol); |
| 101 | struct snd_soc_dapm_context *dapm = &card->dapm; |
Janusz Krzysztofik | 6d7f68a | 2009-07-29 13:18:53 +0200 | [diff] [blame] | 102 | struct soc_enum *control = (struct soc_enum *)kcontrol->private_value; |
| 103 | unsigned short pins; |
| 104 | int pin, changed = 0; |
| 105 | |
| 106 | /* Refuse any mode changes if we are not able to control the codec. */ |
Kuninori Morimoto | d0fdfe3 | 2018-01-29 04:15:57 +0000 | [diff] [blame] | 107 | if (!cx20442_codec->card->pop_time) |
Janusz Krzysztofik | 6d7f68a | 2009-07-29 13:18:53 +0200 | [diff] [blame] | 108 | return -EUNATCH; |
| 109 | |
Takashi Iwai | 9a8d38d | 2014-02-18 08:11:42 +0100 | [diff] [blame] | 110 | if (ucontrol->value.enumerated.item[0] >= control->items) |
Janusz Krzysztofik | 6d7f68a | 2009-07-29 13:18:53 +0200 | [diff] [blame] | 111 | return -EINVAL; |
| 112 | |
Charles Keepax | 03510ca | 2014-02-18 15:22:22 +0000 | [diff] [blame] | 113 | snd_soc_dapm_mutex_lock(dapm); |
Janusz Krzysztofik | 6d7f68a | 2009-07-29 13:18:53 +0200 | [diff] [blame] | 114 | |
| 115 | /* Translate selection to bitmap */ |
| 116 | pins = ams_delta_audio_mode_pins[ucontrol->value.enumerated.item[0]]; |
| 117 | |
| 118 | /* Setup pins after corresponding bits if changed */ |
| 119 | pin = !!(pins & (1 << AMS_DELTA_MOUTHPIECE)); |
Charles Keepax | 03510ca | 2014-02-18 15:22:22 +0000 | [diff] [blame] | 120 | |
Liam Girdwood | ce6120c | 2010-11-05 15:53:46 +0200 | [diff] [blame] | 121 | if (pin != snd_soc_dapm_get_pin_status(dapm, "Mouthpiece")) { |
Janusz Krzysztofik | 6d7f68a | 2009-07-29 13:18:53 +0200 | [diff] [blame] | 122 | changed = 1; |
| 123 | if (pin) |
Charles Keepax | 03510ca | 2014-02-18 15:22:22 +0000 | [diff] [blame] | 124 | snd_soc_dapm_enable_pin_unlocked(dapm, "Mouthpiece"); |
Janusz Krzysztofik | 6d7f68a | 2009-07-29 13:18:53 +0200 | [diff] [blame] | 125 | else |
Charles Keepax | 03510ca | 2014-02-18 15:22:22 +0000 | [diff] [blame] | 126 | snd_soc_dapm_disable_pin_unlocked(dapm, "Mouthpiece"); |
Janusz Krzysztofik | 6d7f68a | 2009-07-29 13:18:53 +0200 | [diff] [blame] | 127 | } |
| 128 | pin = !!(pins & (1 << AMS_DELTA_EARPIECE)); |
Liam Girdwood | ce6120c | 2010-11-05 15:53:46 +0200 | [diff] [blame] | 129 | if (pin != snd_soc_dapm_get_pin_status(dapm, "Earpiece")) { |
Janusz Krzysztofik | 6d7f68a | 2009-07-29 13:18:53 +0200 | [diff] [blame] | 130 | changed = 1; |
| 131 | if (pin) |
Charles Keepax | 03510ca | 2014-02-18 15:22:22 +0000 | [diff] [blame] | 132 | snd_soc_dapm_enable_pin_unlocked(dapm, "Earpiece"); |
Janusz Krzysztofik | 6d7f68a | 2009-07-29 13:18:53 +0200 | [diff] [blame] | 133 | else |
Charles Keepax | 03510ca | 2014-02-18 15:22:22 +0000 | [diff] [blame] | 134 | snd_soc_dapm_disable_pin_unlocked(dapm, "Earpiece"); |
Janusz Krzysztofik | 6d7f68a | 2009-07-29 13:18:53 +0200 | [diff] [blame] | 135 | } |
| 136 | pin = !!(pins & (1 << AMS_DELTA_MICROPHONE)); |
Liam Girdwood | ce6120c | 2010-11-05 15:53:46 +0200 | [diff] [blame] | 137 | if (pin != snd_soc_dapm_get_pin_status(dapm, "Microphone")) { |
Janusz Krzysztofik | 6d7f68a | 2009-07-29 13:18:53 +0200 | [diff] [blame] | 138 | changed = 1; |
| 139 | if (pin) |
Charles Keepax | 03510ca | 2014-02-18 15:22:22 +0000 | [diff] [blame] | 140 | snd_soc_dapm_enable_pin_unlocked(dapm, "Microphone"); |
Janusz Krzysztofik | 6d7f68a | 2009-07-29 13:18:53 +0200 | [diff] [blame] | 141 | else |
Charles Keepax | 03510ca | 2014-02-18 15:22:22 +0000 | [diff] [blame] | 142 | snd_soc_dapm_disable_pin_unlocked(dapm, "Microphone"); |
Janusz Krzysztofik | 6d7f68a | 2009-07-29 13:18:53 +0200 | [diff] [blame] | 143 | } |
| 144 | pin = !!(pins & (1 << AMS_DELTA_SPEAKER)); |
Liam Girdwood | ce6120c | 2010-11-05 15:53:46 +0200 | [diff] [blame] | 145 | if (pin != snd_soc_dapm_get_pin_status(dapm, "Speaker")) { |
Janusz Krzysztofik | 6d7f68a | 2009-07-29 13:18:53 +0200 | [diff] [blame] | 146 | changed = 1; |
| 147 | if (pin) |
Charles Keepax | 03510ca | 2014-02-18 15:22:22 +0000 | [diff] [blame] | 148 | snd_soc_dapm_enable_pin_unlocked(dapm, "Speaker"); |
Janusz Krzysztofik | 6d7f68a | 2009-07-29 13:18:53 +0200 | [diff] [blame] | 149 | else |
Charles Keepax | 03510ca | 2014-02-18 15:22:22 +0000 | [diff] [blame] | 150 | snd_soc_dapm_disable_pin_unlocked(dapm, "Speaker"); |
Janusz Krzysztofik | 6d7f68a | 2009-07-29 13:18:53 +0200 | [diff] [blame] | 151 | } |
| 152 | pin = !!(pins & (1 << AMS_DELTA_AGC)); |
| 153 | if (pin != ams_delta_audio_agc) { |
| 154 | ams_delta_audio_agc = pin; |
| 155 | changed = 1; |
| 156 | if (pin) |
Charles Keepax | 03510ca | 2014-02-18 15:22:22 +0000 | [diff] [blame] | 157 | snd_soc_dapm_enable_pin_unlocked(dapm, "AGCIN"); |
Janusz Krzysztofik | 6d7f68a | 2009-07-29 13:18:53 +0200 | [diff] [blame] | 158 | else |
Charles Keepax | 03510ca | 2014-02-18 15:22:22 +0000 | [diff] [blame] | 159 | snd_soc_dapm_disable_pin_unlocked(dapm, "AGCIN"); |
Janusz Krzysztofik | 6d7f68a | 2009-07-29 13:18:53 +0200 | [diff] [blame] | 160 | } |
Janusz Krzysztofik | 6d7f68a | 2009-07-29 13:18:53 +0200 | [diff] [blame] | 161 | |
Charles Keepax | 03510ca | 2014-02-18 15:22:22 +0000 | [diff] [blame] | 162 | if (changed) |
| 163 | snd_soc_dapm_sync_unlocked(dapm); |
| 164 | |
| 165 | snd_soc_dapm_mutex_unlock(dapm); |
Janusz Krzysztofik | 6d7f68a | 2009-07-29 13:18:53 +0200 | [diff] [blame] | 166 | |
| 167 | return changed; |
| 168 | } |
| 169 | |
| 170 | static int ams_delta_get_audio_mode(struct snd_kcontrol *kcontrol, |
| 171 | struct snd_ctl_elem_value *ucontrol) |
| 172 | { |
Lars-Peter Clausen | 74a1672 | 2014-03-12 15:27:30 +0100 | [diff] [blame] | 173 | struct snd_soc_card *card = snd_kcontrol_chip(kcontrol); |
| 174 | struct snd_soc_dapm_context *dapm = &card->dapm; |
Janusz Krzysztofik | 6d7f68a | 2009-07-29 13:18:53 +0200 | [diff] [blame] | 175 | unsigned short pins, mode; |
| 176 | |
Liam Girdwood | ce6120c | 2010-11-05 15:53:46 +0200 | [diff] [blame] | 177 | pins = ((snd_soc_dapm_get_pin_status(dapm, "Mouthpiece") << |
Janusz Krzysztofik | 6d7f68a | 2009-07-29 13:18:53 +0200 | [diff] [blame] | 178 | AMS_DELTA_MOUTHPIECE) | |
Liam Girdwood | ce6120c | 2010-11-05 15:53:46 +0200 | [diff] [blame] | 179 | (snd_soc_dapm_get_pin_status(dapm, "Earpiece") << |
Janusz Krzysztofik | 6d7f68a | 2009-07-29 13:18:53 +0200 | [diff] [blame] | 180 | AMS_DELTA_EARPIECE)); |
| 181 | if (pins) |
Liam Girdwood | ce6120c | 2010-11-05 15:53:46 +0200 | [diff] [blame] | 182 | pins |= (snd_soc_dapm_get_pin_status(dapm, "Microphone") << |
Janusz Krzysztofik | 6d7f68a | 2009-07-29 13:18:53 +0200 | [diff] [blame] | 183 | AMS_DELTA_MICROPHONE); |
| 184 | else |
Liam Girdwood | ce6120c | 2010-11-05 15:53:46 +0200 | [diff] [blame] | 185 | pins = ((snd_soc_dapm_get_pin_status(dapm, "Microphone") << |
Janusz Krzysztofik | 6d7f68a | 2009-07-29 13:18:53 +0200 | [diff] [blame] | 186 | AMS_DELTA_MICROPHONE) | |
Liam Girdwood | ce6120c | 2010-11-05 15:53:46 +0200 | [diff] [blame] | 187 | (snd_soc_dapm_get_pin_status(dapm, "Speaker") << |
Janusz Krzysztofik | 6d7f68a | 2009-07-29 13:18:53 +0200 | [diff] [blame] | 188 | AMS_DELTA_SPEAKER) | |
| 189 | (ams_delta_audio_agc << AMS_DELTA_AGC)); |
| 190 | |
| 191 | for (mode = 0; mode < ARRAY_SIZE(ams_delta_audio_mode); mode++) |
| 192 | if (pins == ams_delta_audio_mode_pins[mode]) |
| 193 | break; |
| 194 | |
| 195 | if (mode >= ARRAY_SIZE(ams_delta_audio_mode)) |
| 196 | return -EINVAL; |
| 197 | |
| 198 | ucontrol->value.enumerated.item[0] = mode; |
| 199 | |
| 200 | return 0; |
| 201 | } |
| 202 | |
Arnd Bergmann | c9fbc1c | 2019-03-07 11:10:49 +0100 | [diff] [blame] | 203 | static SOC_ENUM_SINGLE_EXT_DECL(ams_delta_audio_enum, |
Takashi Iwai | 7cb5e1b | 2014-02-18 10:47:34 +0100 | [diff] [blame] | 204 | ams_delta_audio_mode); |
Janusz Krzysztofik | 6d7f68a | 2009-07-29 13:18:53 +0200 | [diff] [blame] | 205 | |
| 206 | static const struct snd_kcontrol_new ams_delta_audio_controls[] = { |
Takashi Iwai | 7cb5e1b | 2014-02-18 10:47:34 +0100 | [diff] [blame] | 207 | SOC_ENUM_EXT("Audio Mode", ams_delta_audio_enum, |
Janusz Krzysztofik | 6d7f68a | 2009-07-29 13:18:53 +0200 | [diff] [blame] | 208 | ams_delta_get_audio_mode, ams_delta_set_audio_mode), |
| 209 | }; |
| 210 | |
| 211 | /* Hook switch */ |
| 212 | static struct snd_soc_jack ams_delta_hook_switch; |
| 213 | static struct snd_soc_jack_gpio ams_delta_hook_switch_gpios[] = { |
| 214 | { |
Janusz Krzysztofik | 6d7f68a | 2009-07-29 13:18:53 +0200 | [diff] [blame] | 215 | .name = "hook_switch", |
| 216 | .report = SND_JACK_HEADSET, |
| 217 | .invert = 1, |
| 218 | .debounce_time = 150, |
| 219 | } |
| 220 | }; |
| 221 | |
| 222 | /* After we are able to control the codec over the modem, |
| 223 | * the hook switch can be used for dynamic DAPM reconfiguration. */ |
| 224 | static struct snd_soc_jack_pin ams_delta_hook_switch_pins[] = { |
| 225 | /* Handset */ |
| 226 | { |
| 227 | .pin = "Mouthpiece", |
| 228 | .mask = SND_JACK_MICROPHONE, |
| 229 | }, |
| 230 | { |
| 231 | .pin = "Earpiece", |
| 232 | .mask = SND_JACK_HEADPHONE, |
| 233 | }, |
| 234 | /* Handsfree */ |
| 235 | { |
| 236 | .pin = "Microphone", |
| 237 | .mask = SND_JACK_MICROPHONE, |
| 238 | .invert = 1, |
| 239 | }, |
| 240 | { |
| 241 | .pin = "Speaker", |
| 242 | .mask = SND_JACK_HEADPHONE, |
| 243 | .invert = 1, |
| 244 | }, |
| 245 | }; |
| 246 | |
| 247 | |
| 248 | /* |
| 249 | * Modem line discipline, required for making above controls functional. |
| 250 | * Activated from userspace with ldattach, possibly invoked from udev rule. |
| 251 | */ |
| 252 | |
| 253 | /* To actually apply any modem controlled configuration changes to the codec, |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 254 | * we must connect codec DAI pins to the modem for a moment. Be careful not |
Janusz Krzysztofik | 6d7f68a | 2009-07-29 13:18:53 +0200 | [diff] [blame] | 255 | * to interfere with our digital mute function that shares the same hardware. */ |
| 256 | static struct timer_list cx81801_timer; |
| 257 | static bool cx81801_cmd_pending; |
| 258 | static bool ams_delta_muted; |
| 259 | static DEFINE_SPINLOCK(ams_delta_lock); |
Janusz Krzysztofik | d65777d | 2018-05-18 23:09:51 +0200 | [diff] [blame] | 260 | static struct gpio_desc *gpiod_modem_codec; |
Janusz Krzysztofik | 6d7f68a | 2009-07-29 13:18:53 +0200 | [diff] [blame] | 261 | |
Kees Cook | 7211ec6 | 2017-10-25 08:09:27 -0700 | [diff] [blame] | 262 | static void cx81801_timeout(struct timer_list *unused) |
Janusz Krzysztofik | 6d7f68a | 2009-07-29 13:18:53 +0200 | [diff] [blame] | 263 | { |
| 264 | int muted; |
| 265 | |
| 266 | spin_lock(&ams_delta_lock); |
| 267 | cx81801_cmd_pending = 0; |
| 268 | muted = ams_delta_muted; |
| 269 | spin_unlock(&ams_delta_lock); |
| 270 | |
| 271 | /* Reconnect the codec DAI back from the modem to the CPU DAI |
| 272 | * only if digital mute still off */ |
| 273 | if (!muted) |
Janusz Krzysztofik | d65777d | 2018-05-18 23:09:51 +0200 | [diff] [blame] | 274 | gpiod_set_value(gpiod_modem_codec, 0); |
Janusz Krzysztofik | 6d7f68a | 2009-07-29 13:18:53 +0200 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | /* Line discipline .open() */ |
| 278 | static int cx81801_open(struct tty_struct *tty) |
| 279 | { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 280 | int ret; |
| 281 | |
| 282 | if (!cx20442_codec) |
| 283 | return -ENODEV; |
| 284 | |
| 285 | /* |
| 286 | * Pass the codec structure pointer for use by other ldisc callbacks, |
| 287 | * both the card and the codec specific parts. |
| 288 | */ |
| 289 | tty->disc_data = cx20442_codec; |
| 290 | |
| 291 | ret = v253_ops.open(tty); |
| 292 | |
| 293 | if (ret < 0) |
| 294 | tty->disc_data = NULL; |
| 295 | |
| 296 | return ret; |
Janusz Krzysztofik | 6d7f68a | 2009-07-29 13:18:53 +0200 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | /* Line discipline .close() */ |
| 300 | static void cx81801_close(struct tty_struct *tty) |
| 301 | { |
Kuninori Morimoto | d0fdfe3 | 2018-01-29 04:15:57 +0000 | [diff] [blame] | 302 | struct snd_soc_component *component = tty->disc_data; |
| 303 | struct snd_soc_dapm_context *dapm = &component->card->dapm; |
Janusz Krzysztofik | 6d7f68a | 2009-07-29 13:18:53 +0200 | [diff] [blame] | 304 | |
| 305 | del_timer_sync(&cx81801_timer); |
| 306 | |
Janusz Krzysztofik | 6d7f68a | 2009-07-29 13:18:53 +0200 | [diff] [blame] | 307 | /* Prevent the hook switch from further changing the DAPM pins */ |
| 308 | INIT_LIST_HEAD(&ams_delta_hook_switch.pins); |
| 309 | |
Kuninori Morimoto | d0fdfe3 | 2018-01-29 04:15:57 +0000 | [diff] [blame] | 310 | if (!component) |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 311 | return; |
| 312 | |
| 313 | v253_ops.close(tty); |
| 314 | |
Janusz Krzysztofik | 6d7f68a | 2009-07-29 13:18:53 +0200 | [diff] [blame] | 315 | /* Revert back to default audio input/output constellation */ |
Charles Keepax | 03510ca | 2014-02-18 15:22:22 +0000 | [diff] [blame] | 316 | snd_soc_dapm_mutex_lock(dapm); |
| 317 | |
| 318 | snd_soc_dapm_disable_pin_unlocked(dapm, "Mouthpiece"); |
| 319 | snd_soc_dapm_enable_pin_unlocked(dapm, "Earpiece"); |
| 320 | snd_soc_dapm_enable_pin_unlocked(dapm, "Microphone"); |
| 321 | snd_soc_dapm_disable_pin_unlocked(dapm, "Speaker"); |
| 322 | snd_soc_dapm_disable_pin_unlocked(dapm, "AGCIN"); |
| 323 | |
| 324 | snd_soc_dapm_sync_unlocked(dapm); |
| 325 | |
Lars-Peter Clausen | e95d73c | 2014-03-12 15:27:29 +0100 | [diff] [blame] | 326 | snd_soc_dapm_mutex_unlock(dapm); |
Janusz Krzysztofik | 6d7f68a | 2009-07-29 13:18:53 +0200 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | /* Line discipline .hangup() */ |
| 330 | static int cx81801_hangup(struct tty_struct *tty) |
| 331 | { |
| 332 | cx81801_close(tty); |
| 333 | return 0; |
| 334 | } |
| 335 | |
Joe Perches | dbc6221 | 2011-06-23 11:39:19 -0700 | [diff] [blame] | 336 | /* Line discipline .receive_buf() */ |
Janusz Krzysztofik | 6d7f68a | 2009-07-29 13:18:53 +0200 | [diff] [blame] | 337 | static void cx81801_receive(struct tty_struct *tty, |
| 338 | const unsigned char *cp, char *fp, int count) |
| 339 | { |
Kuninori Morimoto | d0fdfe3 | 2018-01-29 04:15:57 +0000 | [diff] [blame] | 340 | struct snd_soc_component *component = tty->disc_data; |
Janusz Krzysztofik | 6d7f68a | 2009-07-29 13:18:53 +0200 | [diff] [blame] | 341 | const unsigned char *c; |
| 342 | int apply, ret; |
| 343 | |
Kuninori Morimoto | d0fdfe3 | 2018-01-29 04:15:57 +0000 | [diff] [blame] | 344 | if (!component) |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 345 | return; |
| 346 | |
Kuninori Morimoto | d0fdfe3 | 2018-01-29 04:15:57 +0000 | [diff] [blame] | 347 | if (!component->card->pop_time) { |
Janusz Krzysztofik | 6d7f68a | 2009-07-29 13:18:53 +0200 | [diff] [blame] | 348 | /* First modem response, complete setup procedure */ |
| 349 | |
| 350 | /* Initialize timer used for config pulse generation */ |
Kees Cook | 7211ec6 | 2017-10-25 08:09:27 -0700 | [diff] [blame] | 351 | timer_setup(&cx81801_timer, cx81801_timeout, 0); |
Janusz Krzysztofik | 6d7f68a | 2009-07-29 13:18:53 +0200 | [diff] [blame] | 352 | |
| 353 | v253_ops.receive_buf(tty, cp, fp, count); |
| 354 | |
| 355 | /* Link hook switch to DAPM pins */ |
| 356 | ret = snd_soc_jack_add_pins(&ams_delta_hook_switch, |
| 357 | ARRAY_SIZE(ams_delta_hook_switch_pins), |
| 358 | ams_delta_hook_switch_pins); |
| 359 | if (ret) |
Kuninori Morimoto | d0fdfe3 | 2018-01-29 04:15:57 +0000 | [diff] [blame] | 360 | dev_warn(component->dev, |
Janusz Krzysztofik | 6d7f68a | 2009-07-29 13:18:53 +0200 | [diff] [blame] | 361 | "Failed to link hook switch to DAPM pins, " |
| 362 | "will continue with hook switch unlinked.\n"); |
| 363 | |
| 364 | return; |
| 365 | } |
| 366 | |
| 367 | v253_ops.receive_buf(tty, cp, fp, count); |
| 368 | |
| 369 | for (c = &cp[count - 1]; c >= cp; c--) { |
| 370 | if (*c != '\r') |
| 371 | continue; |
| 372 | /* Complete modem response received, apply config to codec */ |
| 373 | |
| 374 | spin_lock_bh(&ams_delta_lock); |
| 375 | mod_timer(&cx81801_timer, jiffies + msecs_to_jiffies(150)); |
| 376 | apply = !ams_delta_muted && !cx81801_cmd_pending; |
| 377 | cx81801_cmd_pending = 1; |
| 378 | spin_unlock_bh(&ams_delta_lock); |
| 379 | |
| 380 | /* Apply config pulse by connecting the codec to the modem |
| 381 | * if not already done */ |
| 382 | if (apply) |
Janusz Krzysztofik | d65777d | 2018-05-18 23:09:51 +0200 | [diff] [blame] | 383 | gpiod_set_value(gpiod_modem_codec, 1); |
Janusz Krzysztofik | 6d7f68a | 2009-07-29 13:18:53 +0200 | [diff] [blame] | 384 | break; |
| 385 | } |
| 386 | } |
| 387 | |
| 388 | /* Line discipline .write_wakeup() */ |
| 389 | static void cx81801_wakeup(struct tty_struct *tty) |
| 390 | { |
| 391 | v253_ops.write_wakeup(tty); |
| 392 | } |
| 393 | |
| 394 | static struct tty_ldisc_ops cx81801_ops = { |
| 395 | .magic = TTY_LDISC_MAGIC, |
| 396 | .name = "cx81801", |
| 397 | .owner = THIS_MODULE, |
| 398 | .open = cx81801_open, |
| 399 | .close = cx81801_close, |
| 400 | .hangup = cx81801_hangup, |
| 401 | .receive_buf = cx81801_receive, |
| 402 | .write_wakeup = cx81801_wakeup, |
| 403 | }; |
| 404 | |
| 405 | |
| 406 | /* |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 407 | * Even if not very useful, the sound card can still work without any of the |
Janusz Krzysztofik | 6d7f68a | 2009-07-29 13:18:53 +0200 | [diff] [blame] | 408 | * above functonality activated. You can still control its audio input/output |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 409 | * constellation and speakerphone gain from userspace by issuing AT commands |
Janusz Krzysztofik | 6d7f68a | 2009-07-29 13:18:53 +0200 | [diff] [blame] | 410 | * over the modem port. |
| 411 | */ |
| 412 | |
Lars-Peter Clausen | 3aa273e | 2015-01-01 17:16:16 +0100 | [diff] [blame] | 413 | static struct snd_soc_ops ams_delta_ops; |
Janusz Krzysztofik | 6d7f68a | 2009-07-29 13:18:53 +0200 | [diff] [blame] | 414 | |
| 415 | |
Janusz Krzysztofik | 6d7f68a | 2009-07-29 13:18:53 +0200 | [diff] [blame] | 416 | /* Digital mute implemented using modem/CPU multiplexer. |
| 417 | * Shares hardware with codec config pulse generation */ |
| 418 | static bool ams_delta_muted = 1; |
| 419 | |
| 420 | static int ams_delta_digital_mute(struct snd_soc_dai *dai, int mute) |
| 421 | { |
| 422 | int apply; |
| 423 | |
| 424 | if (ams_delta_muted == mute) |
| 425 | return 0; |
| 426 | |
| 427 | spin_lock_bh(&ams_delta_lock); |
| 428 | ams_delta_muted = mute; |
| 429 | apply = !cx81801_cmd_pending; |
| 430 | spin_unlock_bh(&ams_delta_lock); |
| 431 | |
| 432 | if (apply) |
Janusz Krzysztofik | d65777d | 2018-05-18 23:09:51 +0200 | [diff] [blame] | 433 | gpiod_set_value(gpiod_modem_codec, !!mute); |
Janusz Krzysztofik | 6d7f68a | 2009-07-29 13:18:53 +0200 | [diff] [blame] | 434 | return 0; |
| 435 | } |
| 436 | |
| 437 | /* Our codec DAI probably doesn't have its own .ops structure */ |
Lars-Peter Clausen | 85e7652 | 2011-11-23 11:40:40 +0100 | [diff] [blame] | 438 | static const struct snd_soc_dai_ops ams_delta_dai_ops = { |
Janusz Krzysztofik | 6d7f68a | 2009-07-29 13:18:53 +0200 | [diff] [blame] | 439 | .digital_mute = ams_delta_digital_mute, |
| 440 | }; |
| 441 | |
| 442 | /* Will be used if the codec ever has its own digital_mute function */ |
| 443 | static int ams_delta_startup(struct snd_pcm_substream *substream) |
| 444 | { |
| 445 | return ams_delta_digital_mute(NULL, 0); |
| 446 | } |
| 447 | |
| 448 | static void ams_delta_shutdown(struct snd_pcm_substream *substream) |
| 449 | { |
| 450 | ams_delta_digital_mute(NULL, 1); |
| 451 | } |
| 452 | |
| 453 | |
| 454 | /* |
| 455 | * Card initialization |
| 456 | */ |
| 457 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 458 | static int ams_delta_cx20442_init(struct snd_soc_pcm_runtime *rtd) |
Janusz Krzysztofik | 6d7f68a | 2009-07-29 13:18:53 +0200 | [diff] [blame] | 459 | { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 460 | struct snd_soc_dai *codec_dai = rtd->codec_dai; |
| 461 | struct snd_soc_card *card = rtd->card; |
Lars-Peter Clausen | 74a1672 | 2014-03-12 15:27:30 +0100 | [diff] [blame] | 462 | struct snd_soc_dapm_context *dapm = &card->dapm; |
Janusz Krzysztofik | 6d7f68a | 2009-07-29 13:18:53 +0200 | [diff] [blame] | 463 | int ret; |
| 464 | /* Codec is ready, now add/activate board specific controls */ |
| 465 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 466 | /* Store a pointer to the codec structure for tty ldisc use */ |
Kuninori Morimoto | d0fdfe3 | 2018-01-29 04:15:57 +0000 | [diff] [blame] | 467 | cx20442_codec = rtd->codec_dai->component; |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 468 | |
Janusz Krzysztofik | 6d7f68a | 2009-07-29 13:18:53 +0200 | [diff] [blame] | 469 | /* Add hook switch - can be used to control the codec from userspace |
| 470 | * even if line discipline fails */ |
Lars-Peter Clausen | df8c661 | 2015-03-04 10:33:25 +0100 | [diff] [blame] | 471 | ret = snd_soc_card_jack_new(card, "hook_switch", SND_JACK_HEADSET, |
| 472 | &ams_delta_hook_switch, NULL, 0); |
Janusz Krzysztofik | 6d7f68a | 2009-07-29 13:18:53 +0200 | [diff] [blame] | 473 | if (ret) |
| 474 | dev_warn(card->dev, |
| 475 | "Failed to allocate resources for hook switch, " |
| 476 | "will continue without one.\n"); |
| 477 | else { |
Janusz Krzysztofik | d65777d | 2018-05-18 23:09:51 +0200 | [diff] [blame] | 478 | ret = snd_soc_jack_add_gpiods(card->dev, &ams_delta_hook_switch, |
Janusz Krzysztofik | 6d7f68a | 2009-07-29 13:18:53 +0200 | [diff] [blame] | 479 | ARRAY_SIZE(ams_delta_hook_switch_gpios), |
| 480 | ams_delta_hook_switch_gpios); |
| 481 | if (ret) |
| 482 | dev_warn(card->dev, |
| 483 | "Failed to set up hook switch GPIO line, " |
| 484 | "will continue with hook switch inactive.\n"); |
| 485 | } |
| 486 | |
Janusz Krzysztofik | d65777d | 2018-05-18 23:09:51 +0200 | [diff] [blame] | 487 | gpiod_modem_codec = devm_gpiod_get(card->dev, "modem_codec", |
| 488 | GPIOD_OUT_HIGH); |
| 489 | if (IS_ERR(gpiod_modem_codec)) { |
| 490 | dev_warn(card->dev, "Failed to obtain modem_codec GPIO\n"); |
| 491 | return 0; |
| 492 | } |
| 493 | |
| 494 | /* Set up digital mute if not provided by the codec */ |
| 495 | if (!codec_dai->driver->ops) { |
| 496 | codec_dai->driver->ops = &ams_delta_dai_ops; |
| 497 | } else { |
| 498 | ams_delta_ops.startup = ams_delta_startup; |
| 499 | ams_delta_ops.shutdown = ams_delta_shutdown; |
| 500 | } |
| 501 | |
Janusz Krzysztofik | 6d7f68a | 2009-07-29 13:18:53 +0200 | [diff] [blame] | 502 | /* Register optional line discipline for over the modem control */ |
Janusz Krzysztofik | b7b8f9b | 2009-08-06 13:07:32 +0200 | [diff] [blame] | 503 | ret = tty_register_ldisc(N_V253, &cx81801_ops); |
Janusz Krzysztofik | 6d7f68a | 2009-07-29 13:18:53 +0200 | [diff] [blame] | 504 | if (ret) { |
| 505 | dev_warn(card->dev, |
| 506 | "Failed to register line discipline, " |
| 507 | "will continue without any controls.\n"); |
| 508 | return 0; |
| 509 | } |
| 510 | |
Janusz Krzysztofik | 6d7f68a | 2009-07-29 13:18:53 +0200 | [diff] [blame] | 511 | /* Set up initial pin constellation */ |
Liam Girdwood | ce6120c | 2010-11-05 15:53:46 +0200 | [diff] [blame] | 512 | snd_soc_dapm_disable_pin(dapm, "Mouthpiece"); |
Liam Girdwood | ce6120c | 2010-11-05 15:53:46 +0200 | [diff] [blame] | 513 | snd_soc_dapm_disable_pin(dapm, "Speaker"); |
| 514 | snd_soc_dapm_disable_pin(dapm, "AGCIN"); |
| 515 | snd_soc_dapm_disable_pin(dapm, "AGCOUT"); |
Janusz Krzysztofik | 6d7f68a | 2009-07-29 13:18:53 +0200 | [diff] [blame] | 516 | |
Janusz Krzysztofik | 6d7f68a | 2009-07-29 13:18:53 +0200 | [diff] [blame] | 517 | return 0; |
| 518 | } |
| 519 | |
| 520 | /* DAI glue - connects codec <--> CPU */ |
Kuninori Morimoto | acb9a2d | 2019-06-06 13:12:48 +0900 | [diff] [blame^] | 521 | SND_SOC_DAILINK_DEFS(cx20442, |
| 522 | DAILINK_COMP_ARRAY(COMP_CPU("omap-mcbsp.1")), |
| 523 | DAILINK_COMP_ARRAY(COMP_CODEC("cx20442-codec", "cx20442-voice")), |
| 524 | DAILINK_COMP_ARRAY(COMP_PLATFORM("omap-mcbsp.1"))); |
| 525 | |
Janusz Krzysztofik | 6d7f68a | 2009-07-29 13:18:53 +0200 | [diff] [blame] | 526 | static struct snd_soc_dai_link ams_delta_dai_link = { |
| 527 | .name = "CX20442", |
| 528 | .stream_name = "CX20442", |
Janusz Krzysztofik | 6d7f68a | 2009-07-29 13:18:53 +0200 | [diff] [blame] | 529 | .init = ams_delta_cx20442_init, |
| 530 | .ops = &ams_delta_ops, |
Lars-Peter Clausen | 3aa273e | 2015-01-01 17:16:16 +0100 | [diff] [blame] | 531 | .dai_fmt = SND_SOC_DAIFMT_DSP_A | SND_SOC_DAIFMT_NB_NF | |
| 532 | SND_SOC_DAIFMT_CBM_CFM, |
Kuninori Morimoto | acb9a2d | 2019-06-06 13:12:48 +0900 | [diff] [blame^] | 533 | SND_SOC_DAILINK_REG(cx20442), |
Janusz Krzysztofik | 6d7f68a | 2009-07-29 13:18:53 +0200 | [diff] [blame] | 534 | }; |
| 535 | |
| 536 | /* Audio card driver */ |
| 537 | static struct snd_soc_card ams_delta_audio_card = { |
| 538 | .name = "AMS_DELTA", |
Axel Lin | b425b88 | 2011-12-22 11:08:59 +0800 | [diff] [blame] | 539 | .owner = THIS_MODULE, |
Janusz Krzysztofik | 6d7f68a | 2009-07-29 13:18:53 +0200 | [diff] [blame] | 540 | .dai_link = &ams_delta_dai_link, |
| 541 | .num_links = 1, |
Lars-Peter Clausen | 74a1672 | 2014-03-12 15:27:30 +0100 | [diff] [blame] | 542 | |
| 543 | .controls = ams_delta_audio_controls, |
| 544 | .num_controls = ARRAY_SIZE(ams_delta_audio_controls), |
| 545 | .dapm_widgets = ams_delta_dapm_widgets, |
| 546 | .num_dapm_widgets = ARRAY_SIZE(ams_delta_dapm_widgets), |
| 547 | .dapm_routes = ams_delta_audio_map, |
| 548 | .num_dapm_routes = ARRAY_SIZE(ams_delta_audio_map), |
Janusz Krzysztofik | 6d7f68a | 2009-07-29 13:18:53 +0200 | [diff] [blame] | 549 | }; |
| 550 | |
Janusz Krzysztofik | 6d7f68a | 2009-07-29 13:18:53 +0200 | [diff] [blame] | 551 | /* Module init/exit */ |
Bill Pemberton | 7ff6000 | 2012-12-07 09:26:29 -0500 | [diff] [blame] | 552 | static int ams_delta_probe(struct platform_device *pdev) |
Janusz Krzysztofik | 6d7f68a | 2009-07-29 13:18:53 +0200 | [diff] [blame] | 553 | { |
Janusz Krzysztofik | b764de2 | 2012-10-03 12:46:57 +0200 | [diff] [blame] | 554 | struct snd_soc_card *card = &ams_delta_audio_card; |
Janusz Krzysztofik | 6d7f68a | 2009-07-29 13:18:53 +0200 | [diff] [blame] | 555 | int ret; |
| 556 | |
Janusz Krzysztofik | b764de2 | 2012-10-03 12:46:57 +0200 | [diff] [blame] | 557 | card->dev = &pdev->dev; |
Janusz Krzysztofik | 6d7f68a | 2009-07-29 13:18:53 +0200 | [diff] [blame] | 558 | |
Janusz Krzysztofik | b764de2 | 2012-10-03 12:46:57 +0200 | [diff] [blame] | 559 | ret = snd_soc_register_card(card); |
| 560 | if (ret) { |
| 561 | dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n", ret); |
| 562 | card->dev = NULL; |
| 563 | return ret; |
| 564 | } |
Janusz Krzysztofik | 6d7f68a | 2009-07-29 13:18:53 +0200 | [diff] [blame] | 565 | return 0; |
Janusz Krzysztofik | 6d7f68a | 2009-07-29 13:18:53 +0200 | [diff] [blame] | 566 | } |
Janusz Krzysztofik | 6d7f68a | 2009-07-29 13:18:53 +0200 | [diff] [blame] | 567 | |
Bill Pemberton | 7ff6000 | 2012-12-07 09:26:29 -0500 | [diff] [blame] | 568 | static int ams_delta_remove(struct platform_device *pdev) |
Janusz Krzysztofik | 6d7f68a | 2009-07-29 13:18:53 +0200 | [diff] [blame] | 569 | { |
Janusz Krzysztofik | b764de2 | 2012-10-03 12:46:57 +0200 | [diff] [blame] | 570 | struct snd_soc_card *card = platform_get_drvdata(pdev); |
| 571 | |
Janusz Krzysztofik | b7b8f9b | 2009-08-06 13:07:32 +0200 | [diff] [blame] | 572 | if (tty_unregister_ldisc(N_V253) != 0) |
Janusz Krzysztofik | b764de2 | 2012-10-03 12:46:57 +0200 | [diff] [blame] | 573 | dev_warn(&pdev->dev, |
Janusz Krzysztofik | b7b8f9b | 2009-08-06 13:07:32 +0200 | [diff] [blame] | 574 | "failed to unregister V253 line discipline\n"); |
Janusz Krzysztofik | 6d7f68a | 2009-07-29 13:18:53 +0200 | [diff] [blame] | 575 | |
Janusz Krzysztofik | b764de2 | 2012-10-03 12:46:57 +0200 | [diff] [blame] | 576 | snd_soc_unregister_card(card); |
| 577 | card->dev = NULL; |
| 578 | return 0; |
Janusz Krzysztofik | 6d7f68a | 2009-07-29 13:18:53 +0200 | [diff] [blame] | 579 | } |
Janusz Krzysztofik | b764de2 | 2012-10-03 12:46:57 +0200 | [diff] [blame] | 580 | |
| 581 | #define DRV_NAME "ams-delta-audio" |
| 582 | |
| 583 | static struct platform_driver ams_delta_driver = { |
| 584 | .driver = { |
| 585 | .name = DRV_NAME, |
Janusz Krzysztofik | b764de2 | 2012-10-03 12:46:57 +0200 | [diff] [blame] | 586 | }, |
| 587 | .probe = ams_delta_probe, |
Bill Pemberton | 7ff6000 | 2012-12-07 09:26:29 -0500 | [diff] [blame] | 588 | .remove = ams_delta_remove, |
Janusz Krzysztofik | b764de2 | 2012-10-03 12:46:57 +0200 | [diff] [blame] | 589 | }; |
| 590 | |
| 591 | module_platform_driver(ams_delta_driver); |
Janusz Krzysztofik | 6d7f68a | 2009-07-29 13:18:53 +0200 | [diff] [blame] | 592 | |
| 593 | MODULE_AUTHOR("Janusz Krzysztofik <jkrzyszt@tis.icnet.pl>"); |
| 594 | MODULE_DESCRIPTION("ALSA SoC driver for Amstrad E3 (Delta) videophone"); |
| 595 | MODULE_LICENSE("GPL"); |
Janusz Krzysztofik | b764de2 | 2012-10-03 12:46:57 +0200 | [diff] [blame] | 596 | MODULE_ALIAS("platform:" DRV_NAME); |