cx20442.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  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 <sound/core.h>
  17. #include <sound/initval.h>
  18. #include <sound/soc-dapm.h>
  19. #include "cx20442.h"
  20. struct cx20442_priv {
  21. struct snd_soc_codec codec;
  22. u8 reg_cache[1];
  23. };
  24. #define CX20442_PM 0x0
  25. #define CX20442_TELIN 0
  26. #define CX20442_TELOUT 1
  27. #define CX20442_MIC 2
  28. #define CX20442_SPKOUT 3
  29. #define CX20442_AGC 4
  30. static const struct snd_soc_dapm_widget cx20442_dapm_widgets[] = {
  31. SND_SOC_DAPM_OUTPUT("TELOUT"),
  32. SND_SOC_DAPM_OUTPUT("SPKOUT"),
  33. SND_SOC_DAPM_OUTPUT("AGCOUT"),
  34. SND_SOC_DAPM_MIXER("SPKOUT Mixer", SND_SOC_NOPM, 0, 0, NULL, 0),
  35. SND_SOC_DAPM_PGA("TELOUT Amp", CX20442_PM, CX20442_TELOUT, 0, NULL, 0),
  36. SND_SOC_DAPM_PGA("SPKOUT Amp", CX20442_PM, CX20442_SPKOUT, 0, NULL, 0),
  37. SND_SOC_DAPM_PGA("SPKOUT AGC", CX20442_PM, CX20442_AGC, 0, NULL, 0),
  38. SND_SOC_DAPM_DAC("DAC", "Playback", SND_SOC_NOPM, 0, 0),
  39. SND_SOC_DAPM_ADC("ADC", "Capture", SND_SOC_NOPM, 0, 0),
  40. SND_SOC_DAPM_MIXER("Input Mixer", SND_SOC_NOPM, 0, 0, NULL, 0),
  41. SND_SOC_DAPM_MICBIAS("TELIN Bias", CX20442_PM, CX20442_TELIN, 0),
  42. SND_SOC_DAPM_MICBIAS("MIC Bias", CX20442_PM, CX20442_MIC, 0),
  43. SND_SOC_DAPM_PGA("MIC AGC", CX20442_PM, CX20442_AGC, 0, NULL, 0),
  44. SND_SOC_DAPM_INPUT("TELIN"),
  45. SND_SOC_DAPM_INPUT("MIC"),
  46. SND_SOC_DAPM_INPUT("AGCIN"),
  47. };
  48. static const struct snd_soc_dapm_route cx20442_audio_map[] = {
  49. {"TELOUT", NULL, "TELOUT Amp"},
  50. {"SPKOUT", NULL, "SPKOUT Mixer"},
  51. {"SPKOUT Mixer", NULL, "SPKOUT Amp"},
  52. {"TELOUT Amp", NULL, "DAC"},
  53. {"SPKOUT Amp", NULL, "DAC"},
  54. {"SPKOUT Mixer", NULL, "SPKOUT AGC"},
  55. {"SPKOUT AGC", NULL, "AGCIN"},
  56. {"AGCOUT", NULL, "MIC AGC"},
  57. {"MIC AGC", NULL, "MIC"},
  58. {"MIC Bias", NULL, "MIC"},
  59. {"Input Mixer", NULL, "MIC Bias"},
  60. {"TELIN Bias", NULL, "TELIN"},
  61. {"Input Mixer", NULL, "TELIN Bias"},
  62. {"ADC", NULL, "Input Mixer"},
  63. };
  64. static int cx20442_add_widgets(struct snd_soc_codec *codec)
  65. {
  66. snd_soc_dapm_new_controls(codec, cx20442_dapm_widgets,
  67. ARRAY_SIZE(cx20442_dapm_widgets));
  68. snd_soc_dapm_add_routes(codec, cx20442_audio_map,
  69. ARRAY_SIZE(cx20442_audio_map));
  70. snd_soc_dapm_new_widgets(codec);
  71. return 0;
  72. }
  73. static unsigned int cx20442_read_reg_cache(struct snd_soc_codec *codec,
  74. unsigned int reg)
  75. {
  76. u8 *reg_cache = codec->reg_cache;
  77. if (reg >= codec->reg_cache_size)
  78. return -EINVAL;
  79. return reg_cache[reg];
  80. }
  81. enum v253_vls {
  82. V253_VLS_NONE = 0,
  83. V253_VLS_T,
  84. V253_VLS_L,
  85. V253_VLS_LT,
  86. V253_VLS_S,
  87. V253_VLS_ST,
  88. V253_VLS_M,
  89. V253_VLS_MST,
  90. V253_VLS_S1,
  91. V253_VLS_S1T,
  92. V253_VLS_MS1T,
  93. V253_VLS_M1,
  94. V253_VLS_M1ST,
  95. V253_VLS_M1S1T,
  96. V253_VLS_H,
  97. V253_VLS_HT,
  98. V253_VLS_MS,
  99. V253_VLS_MS1,
  100. V253_VLS_M1S,
  101. V253_VLS_M1S1,
  102. V253_VLS_TEST,
  103. };
  104. static int cx20442_pm_to_v253_vls(u8 value)
  105. {
  106. switch (value & ~(1 << CX20442_AGC)) {
  107. case 0:
  108. return V253_VLS_T;
  109. case (1 << CX20442_SPKOUT):
  110. case (1 << CX20442_MIC):
  111. case (1 << CX20442_SPKOUT) | (1 << CX20442_MIC):
  112. return V253_VLS_M1S1;
  113. case (1 << CX20442_TELOUT):
  114. case (1 << CX20442_TELIN):
  115. case (1 << CX20442_TELOUT) | (1 << CX20442_TELIN):
  116. return V253_VLS_L;
  117. case (1 << CX20442_TELOUT) | (1 << CX20442_MIC):
  118. return V253_VLS_NONE;
  119. }
  120. return -EINVAL;
  121. }
  122. static int cx20442_pm_to_v253_vsp(u8 value)
  123. {
  124. switch (value & ~(1 << CX20442_AGC)) {
  125. case (1 << CX20442_SPKOUT):
  126. case (1 << CX20442_MIC):
  127. case (1 << CX20442_SPKOUT) | (1 << CX20442_MIC):
  128. return (bool)(value & (1 << CX20442_AGC));
  129. }
  130. return (value & (1 << CX20442_AGC)) ? -EINVAL : 0;
  131. }
  132. static int cx20442_write(struct snd_soc_codec *codec, unsigned int reg,
  133. unsigned int value)
  134. {
  135. u8 *reg_cache = codec->reg_cache;
  136. int vls, vsp, old, len;
  137. char buf[18];
  138. if (reg >= codec->reg_cache_size)
  139. return -EINVAL;
  140. /* hw_write and control_data pointers required for talking to the modem
  141. * are expected to be set by the line discipline initialization code */
  142. if (!codec->hw_write || !codec->control_data)
  143. return -EIO;
  144. old = reg_cache[reg];
  145. reg_cache[reg] = value;
  146. vls = cx20442_pm_to_v253_vls(value);
  147. if (vls < 0)
  148. return vls;
  149. vsp = cx20442_pm_to_v253_vsp(value);
  150. if (vsp < 0)
  151. return vsp;
  152. if ((vls == V253_VLS_T) ||
  153. (vls == cx20442_pm_to_v253_vls(old))) {
  154. if (vsp == cx20442_pm_to_v253_vsp(old))
  155. return 0;
  156. len = snprintf(buf, ARRAY_SIZE(buf), "at+vsp=%d\r", vsp);
  157. } else if (vsp == cx20442_pm_to_v253_vsp(old))
  158. len = snprintf(buf, ARRAY_SIZE(buf), "at+vls=%d\r", vls);
  159. else
  160. len = snprintf(buf, ARRAY_SIZE(buf),
  161. "at+vls=%d;+vsp=%d\r", vls, vsp);
  162. if (unlikely(len > (ARRAY_SIZE(buf) - 1)))
  163. return -ENOMEM;
  164. dev_dbg(codec->dev, "%s: %s\n", __func__, buf);
  165. if (codec->hw_write(codec->control_data, buf, len) != len)
  166. return -EIO;
  167. return 0;
  168. }
  169. /* Moved up here as line discipline referres it during initialization */
  170. static struct snd_soc_codec *cx20442_codec;
  171. /*
  172. * Line discpline related code
  173. *
  174. * Any of the callback functions below can be used in two ways:
  175. * 1) registerd by a machine driver as one of line discipline operations,
  176. * 2) called from a machine's provided line discipline callback function
  177. * in case when extra machine specific code must be run as well.
  178. */
  179. /* Modem init: echo off, digital speaker off, quiet off, voice mode */
  180. static const char *v253_init = "ate0m0q0+fclass=8\r";
  181. /* Line discipline .open() */
  182. static int v253_open(struct tty_struct *tty)
  183. {
  184. struct snd_soc_codec *codec = cx20442_codec;
  185. int ret, len = strlen(v253_init);
  186. /* Doesn't make sense without write callback */
  187. if (!tty->ops->write)
  188. return -EINVAL;
  189. /* Pass the codec structure address for use by other ldisc callbacks */
  190. tty->disc_data = codec;
  191. if (tty->ops->write(tty, v253_init, len) != len) {
  192. ret = -EIO;
  193. goto err;
  194. }
  195. /* Actual setup will be performed after the modem responds. */
  196. return 0;
  197. err:
  198. tty->disc_data = NULL;
  199. return ret;
  200. }
  201. /* Line discipline .close() */
  202. static void v253_close(struct tty_struct *tty)
  203. {
  204. struct snd_soc_codec *codec = tty->disc_data;
  205. tty->disc_data = NULL;
  206. if (!codec)
  207. return;
  208. /* Prevent the codec driver from further accessing the modem */
  209. codec->hw_write = NULL;
  210. codec->control_data = NULL;
  211. codec->pop_time = 0;
  212. }
  213. /* Line discipline .hangup() */
  214. static int v253_hangup(struct tty_struct *tty)
  215. {
  216. v253_close(tty);
  217. return 0;
  218. }
  219. /* Line discipline .receive_buf() */
  220. static void v253_receive(struct tty_struct *tty,
  221. const unsigned char *cp, char *fp, int count)
  222. {
  223. struct snd_soc_codec *codec = tty->disc_data;
  224. if (!codec)
  225. return;
  226. if (!codec->control_data) {
  227. /* First modem response, complete setup procedure */
  228. /* Set up codec driver access to modem controls */
  229. codec->control_data = tty;
  230. codec->hw_write = (hw_write_t)tty->ops->write;
  231. codec->pop_time = 1;
  232. }
  233. }
  234. /* Line discipline .write_wakeup() */
  235. static void v253_wakeup(struct tty_struct *tty)
  236. {
  237. }
  238. struct tty_ldisc_ops v253_ops = {
  239. .magic = TTY_LDISC_MAGIC,
  240. .name = "cx20442",
  241. .owner = THIS_MODULE,
  242. .open = v253_open,
  243. .close = v253_close,
  244. .hangup = v253_hangup,
  245. .receive_buf = v253_receive,
  246. .write_wakeup = v253_wakeup,
  247. };
  248. EXPORT_SYMBOL_GPL(v253_ops);
  249. /*
  250. * Codec DAI
  251. */
  252. struct snd_soc_dai cx20442_dai = {
  253. .name = "CX20442",
  254. .playback = {
  255. .stream_name = "Playback",
  256. .channels_min = 1,
  257. .channels_max = 1,
  258. .rates = SNDRV_PCM_RATE_8000,
  259. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  260. },
  261. .capture = {
  262. .stream_name = "Capture",
  263. .channels_min = 1,
  264. .channels_max = 1,
  265. .rates = SNDRV_PCM_RATE_8000,
  266. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  267. },
  268. };
  269. EXPORT_SYMBOL_GPL(cx20442_dai);
  270. static int cx20442_codec_probe(struct platform_device *pdev)
  271. {
  272. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  273. struct snd_soc_codec *codec;
  274. int ret;
  275. if (!cx20442_codec) {
  276. dev_err(&pdev->dev, "cx20442 not yet discovered\n");
  277. return -ENODEV;
  278. }
  279. codec = cx20442_codec;
  280. socdev->card->codec = codec;
  281. /* register pcms */
  282. ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
  283. if (ret < 0) {
  284. dev_err(&pdev->dev, "failed to create pcms\n");
  285. goto pcm_err;
  286. }
  287. cx20442_add_widgets(codec);
  288. ret = snd_soc_init_card(socdev);
  289. if (ret < 0) {
  290. dev_err(&pdev->dev, "failed to register card\n");
  291. goto card_err;
  292. }
  293. return ret;
  294. card_err:
  295. snd_soc_free_pcms(socdev);
  296. snd_soc_dapm_free(socdev);
  297. pcm_err:
  298. return ret;
  299. }
  300. /* power down chip */
  301. static int cx20442_codec_remove(struct platform_device *pdev)
  302. {
  303. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  304. snd_soc_free_pcms(socdev);
  305. snd_soc_dapm_free(socdev);
  306. return 0;
  307. }
  308. struct snd_soc_codec_device cx20442_codec_dev = {
  309. .probe = cx20442_codec_probe,
  310. .remove = cx20442_codec_remove,
  311. };
  312. EXPORT_SYMBOL_GPL(cx20442_codec_dev);
  313. static int cx20442_register(struct cx20442_priv *cx20442)
  314. {
  315. struct snd_soc_codec *codec = &cx20442->codec;
  316. int ret;
  317. mutex_init(&codec->mutex);
  318. INIT_LIST_HEAD(&codec->dapm_widgets);
  319. INIT_LIST_HEAD(&codec->dapm_paths);
  320. codec->name = "CX20442";
  321. codec->owner = THIS_MODULE;
  322. codec->private_data = cx20442;
  323. codec->dai = &cx20442_dai;
  324. codec->num_dai = 1;
  325. codec->reg_cache = &cx20442->reg_cache;
  326. codec->reg_cache_size = ARRAY_SIZE(cx20442->reg_cache);
  327. codec->read = cx20442_read_reg_cache;
  328. codec->write = cx20442_write;
  329. codec->bias_level = SND_SOC_BIAS_OFF;
  330. cx20442_dai.dev = codec->dev;
  331. cx20442_codec = codec;
  332. ret = snd_soc_register_codec(codec);
  333. if (ret != 0) {
  334. dev_err(codec->dev, "Failed to register codec: %d\n", ret);
  335. goto err;
  336. }
  337. ret = snd_soc_register_dai(&cx20442_dai);
  338. if (ret != 0) {
  339. dev_err(codec->dev, "Failed to register DAI: %d\n", ret);
  340. goto err_codec;
  341. }
  342. return 0;
  343. err_codec:
  344. snd_soc_unregister_codec(codec);
  345. err:
  346. cx20442_codec = NULL;
  347. kfree(cx20442);
  348. return ret;
  349. }
  350. static void cx20442_unregister(struct cx20442_priv *cx20442)
  351. {
  352. snd_soc_unregister_dai(&cx20442_dai);
  353. snd_soc_unregister_codec(&cx20442->codec);
  354. cx20442_codec = NULL;
  355. kfree(cx20442);
  356. }
  357. static int cx20442_platform_probe(struct platform_device *pdev)
  358. {
  359. struct cx20442_priv *cx20442;
  360. struct snd_soc_codec *codec;
  361. cx20442 = kzalloc(sizeof(struct cx20442_priv), GFP_KERNEL);
  362. if (cx20442 == NULL)
  363. return -ENOMEM;
  364. codec = &cx20442->codec;
  365. codec->control_data = NULL;
  366. codec->hw_write = NULL;
  367. codec->pop_time = 0;
  368. codec->dev = &pdev->dev;
  369. platform_set_drvdata(pdev, cx20442);
  370. return cx20442_register(cx20442);
  371. }
  372. static int __exit cx20442_platform_remove(struct platform_device *pdev)
  373. {
  374. struct cx20442_priv *cx20442 = platform_get_drvdata(pdev);
  375. cx20442_unregister(cx20442);
  376. return 0;
  377. }
  378. static struct platform_driver cx20442_platform_driver = {
  379. .driver = {
  380. .name = "cx20442",
  381. .owner = THIS_MODULE,
  382. },
  383. .probe = cx20442_platform_probe,
  384. .remove = __exit_p(cx20442_platform_remove),
  385. };
  386. static int __init cx20442_init(void)
  387. {
  388. return platform_driver_register(&cx20442_platform_driver);
  389. }
  390. module_init(cx20442_init);
  391. static void __exit cx20442_exit(void)
  392. {
  393. platform_driver_unregister(&cx20442_platform_driver);
  394. }
  395. module_exit(cx20442_exit);
  396. MODULE_DESCRIPTION("ASoC CX20442-11 voice modem codec driver");
  397. MODULE_AUTHOR("Janusz Krzysztofik");
  398. MODULE_LICENSE("GPL");
  399. MODULE_ALIAS("platform:cx20442");