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