cx20442.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. /*
  2. * cx20442.c -- CX20442 ALSA Soc Audio driver
  3. *
  4. * Copyright 2009 Janusz Krzysztofik <jkrzyszt@tis.icnet.pl>
  5. *
  6. * Initially based on sound/soc/codecs/wm8400.c
  7. * Copyright 2008, 2009 Wolfson Microelectronics PLC.
  8. * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
  9. *
  10. * This program is free software; you can redistribute it and/or modify it
  11. * under the terms of the GNU General Public License as published by the
  12. * Free Software Foundation; either version 2 of the License, or (at your
  13. * option) any later version.
  14. */
  15. #include <linux/tty.h>
  16. #include <linux/slab.h>
  17. #include <sound/core.h>
  18. #include <sound/initval.h>
  19. #include <sound/soc.h>
  20. #include "cx20442.h"
  21. struct cx20442_priv {
  22. enum snd_soc_control_type control_type;
  23. void *control_data;
  24. u8 reg_cache[1];
  25. };
  26. #define CX20442_PM 0x0
  27. #define CX20442_TELIN 0
  28. #define CX20442_TELOUT 1
  29. #define CX20442_MIC 2
  30. #define CX20442_SPKOUT 3
  31. #define CX20442_AGC 4
  32. static const struct snd_soc_dapm_widget cx20442_dapm_widgets[] = {
  33. SND_SOC_DAPM_OUTPUT("TELOUT"),
  34. SND_SOC_DAPM_OUTPUT("SPKOUT"),
  35. SND_SOC_DAPM_OUTPUT("AGCOUT"),
  36. SND_SOC_DAPM_MIXER("SPKOUT Mixer", SND_SOC_NOPM, 0, 0, NULL, 0),
  37. SND_SOC_DAPM_PGA("TELOUT Amp", CX20442_PM, CX20442_TELOUT, 0, NULL, 0),
  38. SND_SOC_DAPM_PGA("SPKOUT Amp", CX20442_PM, CX20442_SPKOUT, 0, NULL, 0),
  39. SND_SOC_DAPM_PGA("SPKOUT AGC", CX20442_PM, CX20442_AGC, 0, NULL, 0),
  40. SND_SOC_DAPM_DAC("DAC", "Playback", SND_SOC_NOPM, 0, 0),
  41. SND_SOC_DAPM_ADC("ADC", "Capture", SND_SOC_NOPM, 0, 0),
  42. SND_SOC_DAPM_MIXER("Input Mixer", SND_SOC_NOPM, 0, 0, NULL, 0),
  43. SND_SOC_DAPM_MICBIAS("TELIN Bias", CX20442_PM, CX20442_TELIN, 0),
  44. SND_SOC_DAPM_MICBIAS("MIC Bias", CX20442_PM, CX20442_MIC, 0),
  45. SND_SOC_DAPM_PGA("MIC AGC", CX20442_PM, CX20442_AGC, 0, NULL, 0),
  46. SND_SOC_DAPM_INPUT("TELIN"),
  47. SND_SOC_DAPM_INPUT("MIC"),
  48. SND_SOC_DAPM_INPUT("AGCIN"),
  49. };
  50. static const struct snd_soc_dapm_route cx20442_audio_map[] = {
  51. {"TELOUT", NULL, "TELOUT Amp"},
  52. {"SPKOUT", NULL, "SPKOUT Mixer"},
  53. {"SPKOUT Mixer", NULL, "SPKOUT Amp"},
  54. {"TELOUT Amp", NULL, "DAC"},
  55. {"SPKOUT Amp", NULL, "DAC"},
  56. {"SPKOUT Mixer", NULL, "SPKOUT AGC"},
  57. {"SPKOUT AGC", NULL, "AGCIN"},
  58. {"AGCOUT", NULL, "MIC AGC"},
  59. {"MIC AGC", NULL, "MIC"},
  60. {"MIC Bias", NULL, "MIC"},
  61. {"Input Mixer", NULL, "MIC Bias"},
  62. {"TELIN Bias", NULL, "TELIN"},
  63. {"Input Mixer", NULL, "TELIN Bias"},
  64. {"ADC", NULL, "Input Mixer"},
  65. };
  66. static int cx20442_add_widgets(struct snd_soc_codec *codec)
  67. {
  68. struct snd_soc_dapm_context *dapm = &codec->dapm;
  69. snd_soc_dapm_new_controls(dapm, cx20442_dapm_widgets,
  70. ARRAY_SIZE(cx20442_dapm_widgets));
  71. snd_soc_dapm_add_routes(dapm, cx20442_audio_map,
  72. ARRAY_SIZE(cx20442_audio_map));
  73. return 0;
  74. }
  75. static unsigned int cx20442_read_reg_cache(struct snd_soc_codec *codec,
  76. unsigned int reg)
  77. {
  78. u8 *reg_cache = codec->reg_cache;
  79. if (reg >= codec->driver->reg_cache_size)
  80. return -EINVAL;
  81. return reg_cache[reg];
  82. }
  83. enum v253_vls {
  84. V253_VLS_NONE = 0,
  85. V253_VLS_T,
  86. V253_VLS_L,
  87. V253_VLS_LT,
  88. V253_VLS_S,
  89. V253_VLS_ST,
  90. V253_VLS_M,
  91. V253_VLS_MST,
  92. V253_VLS_S1,
  93. V253_VLS_S1T,
  94. V253_VLS_MS1T,
  95. V253_VLS_M1,
  96. V253_VLS_M1ST,
  97. V253_VLS_M1S1T,
  98. V253_VLS_H,
  99. V253_VLS_HT,
  100. V253_VLS_MS,
  101. V253_VLS_MS1,
  102. V253_VLS_M1S,
  103. V253_VLS_M1S1,
  104. V253_VLS_TEST,
  105. };
  106. static int cx20442_pm_to_v253_vls(u8 value)
  107. {
  108. switch (value & ~(1 << CX20442_AGC)) {
  109. case 0:
  110. return V253_VLS_T;
  111. case (1 << CX20442_SPKOUT):
  112. case (1 << CX20442_MIC):
  113. case (1 << CX20442_SPKOUT) | (1 << CX20442_MIC):
  114. return V253_VLS_M1S1;
  115. case (1 << CX20442_TELOUT):
  116. case (1 << CX20442_TELIN):
  117. case (1 << CX20442_TELOUT) | (1 << CX20442_TELIN):
  118. return V253_VLS_L;
  119. case (1 << CX20442_TELOUT) | (1 << CX20442_MIC):
  120. return V253_VLS_NONE;
  121. }
  122. return -EINVAL;
  123. }
  124. static int cx20442_pm_to_v253_vsp(u8 value)
  125. {
  126. switch (value & ~(1 << CX20442_AGC)) {
  127. case (1 << CX20442_SPKOUT):
  128. case (1 << CX20442_MIC):
  129. case (1 << CX20442_SPKOUT) | (1 << CX20442_MIC):
  130. return (bool)(value & (1 << CX20442_AGC));
  131. }
  132. return (value & (1 << CX20442_AGC)) ? -EINVAL : 0;
  133. }
  134. static int cx20442_write(struct snd_soc_codec *codec, unsigned int reg,
  135. unsigned int value)
  136. {
  137. struct cx20442_priv *cx20442 = snd_soc_codec_get_drvdata(codec);
  138. u8 *reg_cache = codec->reg_cache;
  139. int vls, vsp, old, len;
  140. char buf[18];
  141. if (reg >= codec->driver->reg_cache_size)
  142. return -EINVAL;
  143. /* hw_write and control_data pointers required for talking to the modem
  144. * are expected to be set by the line discipline initialization code */
  145. if (!codec->hw_write || !cx20442->control_data)
  146. return -EIO;
  147. old = reg_cache[reg];
  148. reg_cache[reg] = value;
  149. vls = cx20442_pm_to_v253_vls(value);
  150. if (vls < 0)
  151. return vls;
  152. vsp = cx20442_pm_to_v253_vsp(value);
  153. if (vsp < 0)
  154. return vsp;
  155. if ((vls == V253_VLS_T) ||
  156. (vls == cx20442_pm_to_v253_vls(old))) {
  157. if (vsp == cx20442_pm_to_v253_vsp(old))
  158. return 0;
  159. len = snprintf(buf, ARRAY_SIZE(buf), "at+vsp=%d\r", vsp);
  160. } else if (vsp == cx20442_pm_to_v253_vsp(old))
  161. len = snprintf(buf, ARRAY_SIZE(buf), "at+vls=%d\r", vls);
  162. else
  163. len = snprintf(buf, ARRAY_SIZE(buf),
  164. "at+vls=%d;+vsp=%d\r", vls, vsp);
  165. if (unlikely(len > (ARRAY_SIZE(buf) - 1)))
  166. return -ENOMEM;
  167. dev_dbg(codec->dev, "%s: %s\n", __func__, buf);
  168. if (codec->hw_write(cx20442->control_data, buf, len) != len)
  169. return -EIO;
  170. return 0;
  171. }
  172. /*
  173. * Line discpline related code
  174. *
  175. * Any of the callback functions below can be used in two ways:
  176. * 1) registerd by a machine driver as one of line discipline operations,
  177. * 2) called from a machine's provided line discipline callback function
  178. * in case when extra machine specific code must be run as well.
  179. */
  180. /* Modem init: echo off, digital speaker off, quiet off, voice mode */
  181. static const char *v253_init = "ate0m0q0+fclass=8\r";
  182. /* Line discipline .open() */
  183. static int v253_open(struct tty_struct *tty)
  184. {
  185. int ret, len = strlen(v253_init);
  186. /* Doesn't make sense without write callback */
  187. if (!tty->ops->write)
  188. return -EINVAL;
  189. /* Won't work if no codec pointer has been passed by a card driver */
  190. if (!tty->disc_data)
  191. return -ENODEV;
  192. if (tty->ops->write(tty, v253_init, len) != len) {
  193. ret = -EIO;
  194. goto err;
  195. }
  196. /* Actual setup will be performed after the modem responds. */
  197. return 0;
  198. err:
  199. tty->disc_data = NULL;
  200. return ret;
  201. }
  202. /* Line discipline .close() */
  203. static void v253_close(struct tty_struct *tty)
  204. {
  205. struct snd_soc_codec *codec = tty->disc_data;
  206. struct cx20442_priv *cx20442;
  207. tty->disc_data = NULL;
  208. if (!codec)
  209. return;
  210. cx20442 = snd_soc_codec_get_drvdata(codec);
  211. /* Prevent the codec driver from further accessing the modem */
  212. codec->hw_write = NULL;
  213. cx20442->control_data = NULL;
  214. codec->card->pop_time = 0;
  215. }
  216. /* Line discipline .hangup() */
  217. static int v253_hangup(struct tty_struct *tty)
  218. {
  219. v253_close(tty);
  220. return 0;
  221. }
  222. /* Line discipline .receive_buf() */
  223. static void v253_receive(struct tty_struct *tty,
  224. const unsigned char *cp, char *fp, int count)
  225. {
  226. struct snd_soc_codec *codec = tty->disc_data;
  227. struct cx20442_priv *cx20442;
  228. if (!codec)
  229. return;
  230. cx20442 = snd_soc_codec_get_drvdata(codec);
  231. if (!cx20442->control_data) {
  232. /* First modem response, complete setup procedure */
  233. /* Set up codec driver access to modem controls */
  234. cx20442->control_data = tty;
  235. codec->hw_write = (hw_write_t)tty->ops->write;
  236. codec->card->pop_time = 1;
  237. }
  238. }
  239. /* Line discipline .write_wakeup() */
  240. static void v253_wakeup(struct tty_struct *tty)
  241. {
  242. }
  243. struct tty_ldisc_ops v253_ops = {
  244. .magic = TTY_LDISC_MAGIC,
  245. .name = "cx20442",
  246. .owner = THIS_MODULE,
  247. .open = v253_open,
  248. .close = v253_close,
  249. .hangup = v253_hangup,
  250. .receive_buf = v253_receive,
  251. .write_wakeup = v253_wakeup,
  252. };
  253. EXPORT_SYMBOL_GPL(v253_ops);
  254. /*
  255. * Codec DAI
  256. */
  257. static struct snd_soc_dai_driver cx20442_dai = {
  258. .name = "cx20442-voice",
  259. .playback = {
  260. .stream_name = "Playback",
  261. .channels_min = 1,
  262. .channels_max = 1,
  263. .rates = SNDRV_PCM_RATE_8000,
  264. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  265. },
  266. .capture = {
  267. .stream_name = "Capture",
  268. .channels_min = 1,
  269. .channels_max = 1,
  270. .rates = SNDRV_PCM_RATE_8000,
  271. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  272. },
  273. };
  274. static int cx20442_codec_probe(struct snd_soc_codec *codec)
  275. {
  276. struct cx20442_priv *cx20442;
  277. cx20442 = kzalloc(sizeof(struct cx20442_priv), GFP_KERNEL);
  278. if (cx20442 == NULL)
  279. return -ENOMEM;
  280. snd_soc_codec_set_drvdata(codec, cx20442);
  281. cx20442_add_widgets(codec);
  282. cx20442->control_data = NULL;
  283. codec->hw_write = NULL;
  284. codec->card->pop_time = 0;
  285. return 0;
  286. }
  287. /* power down chip */
  288. static int cx20442_codec_remove(struct snd_soc_codec *codec)
  289. {
  290. struct cx20442_priv *cx20442 = snd_soc_codec_get_drvdata(codec);
  291. if (cx20442->control_data) {
  292. struct tty_struct *tty = cx20442->control_data;
  293. tty_hangup(tty);
  294. }
  295. kfree(cx20442);
  296. return 0;
  297. }
  298. static struct snd_soc_codec_driver cx20442_codec_dev = {
  299. .probe = cx20442_codec_probe,
  300. .remove = cx20442_codec_remove,
  301. .reg_cache_size = 1,
  302. .reg_word_size = sizeof(u8),
  303. .read = cx20442_read_reg_cache,
  304. .write = cx20442_write,
  305. };
  306. static int cx20442_platform_probe(struct platform_device *pdev)
  307. {
  308. return snd_soc_register_codec(&pdev->dev,
  309. &cx20442_codec_dev, &cx20442_dai, 1);
  310. }
  311. static int __exit cx20442_platform_remove(struct platform_device *pdev)
  312. {
  313. snd_soc_unregister_codec(&pdev->dev);
  314. return 0;
  315. }
  316. static struct platform_driver cx20442_platform_driver = {
  317. .driver = {
  318. .name = "cx20442-codec",
  319. .owner = THIS_MODULE,
  320. },
  321. .probe = cx20442_platform_probe,
  322. .remove = __exit_p(cx20442_platform_remove),
  323. };
  324. static int __init cx20442_init(void)
  325. {
  326. return platform_driver_register(&cx20442_platform_driver);
  327. }
  328. module_init(cx20442_init);
  329. static void __exit cx20442_exit(void)
  330. {
  331. platform_driver_unregister(&cx20442_platform_driver);
  332. }
  333. module_exit(cx20442_exit);
  334. MODULE_DESCRIPTION("ASoC CX20442-11 voice modem codec driver");
  335. MODULE_AUTHOR("Janusz Krzysztofik");
  336. MODULE_LICENSE("GPL");
  337. MODULE_ALIAS("platform:cx20442-codec");