ams-delta.c 16 KB

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