cx20442.c 10 KB

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