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