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