cx20442.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * cx20442.c -- CX20442 ALSA Soc Audio driver
  4. *
  5. * Copyright 2009 Janusz Krzysztofik <jkrzyszt@tis.icnet.pl>
  6. *
  7. * Initially based on sound/soc/codecs/wm8400.c
  8. * Copyright 2008, 2009 Wolfson Microelectronics PLC.
  9. * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
  10. */
  11. #include <linux/tty.h>
  12. #include <linux/slab.h>
  13. #include <linux/module.h>
  14. #include <linux/regulator/consumer.h>
  15. #include <sound/core.h>
  16. #include <sound/initval.h>
  17. #include <sound/soc.h>
  18. #include "cx20442.h"
  19. struct cx20442_priv {
  20. struct tty_struct *tty;
  21. struct regulator *por;
  22. u8 reg_cache;
  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 unsigned int cx20442_read_reg_cache(struct snd_soc_component *component,
  65. unsigned int reg)
  66. {
  67. struct cx20442_priv *cx20442 = snd_soc_component_get_drvdata(component);
  68. if (reg >= 1)
  69. return -EINVAL;
  70. return cx20442->reg_cache;
  71. }
  72. enum v253_vls {
  73. V253_VLS_NONE = 0,
  74. V253_VLS_T,
  75. V253_VLS_L,
  76. V253_VLS_LT,
  77. V253_VLS_S,
  78. V253_VLS_ST,
  79. V253_VLS_M,
  80. V253_VLS_MST,
  81. V253_VLS_S1,
  82. V253_VLS_S1T,
  83. V253_VLS_MS1T,
  84. V253_VLS_M1,
  85. V253_VLS_M1ST,
  86. V253_VLS_M1S1T,
  87. V253_VLS_H,
  88. V253_VLS_HT,
  89. V253_VLS_MS,
  90. V253_VLS_MS1,
  91. V253_VLS_M1S,
  92. V253_VLS_M1S1,
  93. V253_VLS_TEST,
  94. };
  95. static int cx20442_pm_to_v253_vls(u8 value)
  96. {
  97. switch (value & ~(1 << CX20442_AGC)) {
  98. case 0:
  99. return V253_VLS_T;
  100. case (1 << CX20442_SPKOUT):
  101. case (1 << CX20442_MIC):
  102. case (1 << CX20442_SPKOUT) | (1 << CX20442_MIC):
  103. return V253_VLS_M1S1;
  104. case (1 << CX20442_TELOUT):
  105. case (1 << CX20442_TELIN):
  106. case (1 << CX20442_TELOUT) | (1 << CX20442_TELIN):
  107. return V253_VLS_L;
  108. case (1 << CX20442_TELOUT) | (1 << CX20442_MIC):
  109. return V253_VLS_NONE;
  110. }
  111. return -EINVAL;
  112. }
  113. static int cx20442_pm_to_v253_vsp(u8 value)
  114. {
  115. switch (value & ~(1 << CX20442_AGC)) {
  116. case (1 << CX20442_SPKOUT):
  117. case (1 << CX20442_MIC):
  118. case (1 << CX20442_SPKOUT) | (1 << CX20442_MIC):
  119. return (bool)(value & (1 << CX20442_AGC));
  120. }
  121. return (value & (1 << CX20442_AGC)) ? -EINVAL : 0;
  122. }
  123. static int cx20442_write(struct snd_soc_component *component, unsigned int reg,
  124. unsigned int value)
  125. {
  126. struct cx20442_priv *cx20442 = snd_soc_component_get_drvdata(component);
  127. int vls, vsp, old, len;
  128. char buf[18];
  129. if (reg >= 1)
  130. return -EINVAL;
  131. /* tty and write pointers required for talking to the modem
  132. * are expected to be set by the line discipline initialization code */
  133. if (!cx20442->tty || !cx20442->tty->ops->write)
  134. return -EIO;
  135. old = cx20442->reg_cache;
  136. cx20442->reg_cache = value;
  137. vls = cx20442_pm_to_v253_vls(value);
  138. if (vls < 0)
  139. return vls;
  140. vsp = cx20442_pm_to_v253_vsp(value);
  141. if (vsp < 0)
  142. return vsp;
  143. if ((vls == V253_VLS_T) ||
  144. (vls == cx20442_pm_to_v253_vls(old))) {
  145. if (vsp == cx20442_pm_to_v253_vsp(old))
  146. return 0;
  147. len = snprintf(buf, ARRAY_SIZE(buf), "at+vsp=%d\r", vsp);
  148. } else if (vsp == cx20442_pm_to_v253_vsp(old))
  149. len = snprintf(buf, ARRAY_SIZE(buf), "at+vls=%d\r", vls);
  150. else
  151. len = snprintf(buf, ARRAY_SIZE(buf),
  152. "at+vls=%d;+vsp=%d\r", vls, vsp);
  153. if (unlikely(len > (ARRAY_SIZE(buf) - 1)))
  154. return -ENOMEM;
  155. dev_dbg(component->dev, "%s: %s\n", __func__, buf);
  156. if (cx20442->tty->ops->write(cx20442->tty, buf, len) != len)
  157. return -EIO;
  158. return 0;
  159. }
  160. /*
  161. * Line discpline related code
  162. *
  163. * Any of the callback functions below can be used in two ways:
  164. * 1) registerd by a machine driver as one of line discipline operations,
  165. * 2) called from a machine's provided line discipline callback function
  166. * in case when extra machine specific code must be run as well.
  167. */
  168. /* Modem init: echo off, digital speaker off, quiet off, voice mode */
  169. static const char *v253_init = "ate0m0q0+fclass=8\r";
  170. /* Line discipline .open() */
  171. static int v253_open(struct tty_struct *tty)
  172. {
  173. int ret, len = strlen(v253_init);
  174. /* Doesn't make sense without write callback */
  175. if (!tty->ops->write)
  176. return -EINVAL;
  177. /* Won't work if no codec pointer has been passed by a card driver */
  178. if (!tty->disc_data)
  179. return -ENODEV;
  180. tty->receive_room = 16;
  181. if (tty->ops->write(tty, v253_init, len) != len) {
  182. ret = -EIO;
  183. goto err;
  184. }
  185. /* Actual setup will be performed after the modem responds. */
  186. return 0;
  187. err:
  188. tty->disc_data = NULL;
  189. return ret;
  190. }
  191. /* Line discipline .close() */
  192. static void v253_close(struct tty_struct *tty)
  193. {
  194. struct snd_soc_component *component = tty->disc_data;
  195. struct cx20442_priv *cx20442;
  196. tty->disc_data = NULL;
  197. if (!component)
  198. return;
  199. cx20442 = snd_soc_component_get_drvdata(component);
  200. /* Prevent the codec driver from further accessing the modem */
  201. cx20442->tty = NULL;
  202. component->card->pop_time = 0;
  203. }
  204. /* Line discipline .hangup() */
  205. static int v253_hangup(struct tty_struct *tty)
  206. {
  207. v253_close(tty);
  208. return 0;
  209. }
  210. /* Line discipline .receive_buf() */
  211. static void v253_receive(struct tty_struct *tty,
  212. const unsigned char *cp, char *fp, int count)
  213. {
  214. struct snd_soc_component *component = tty->disc_data;
  215. struct cx20442_priv *cx20442;
  216. if (!component)
  217. return;
  218. cx20442 = snd_soc_component_get_drvdata(component);
  219. if (!cx20442->tty) {
  220. /* First modem response, complete setup procedure */
  221. /* Set up codec driver access to modem controls */
  222. cx20442->tty = tty;
  223. component->card->pop_time = 1;
  224. }
  225. }
  226. /* Line discipline .write_wakeup() */
  227. static void v253_wakeup(struct tty_struct *tty)
  228. {
  229. }
  230. struct tty_ldisc_ops v253_ops = {
  231. .magic = TTY_LDISC_MAGIC,
  232. .name = "cx20442",
  233. .owner = THIS_MODULE,
  234. .open = v253_open,
  235. .close = v253_close,
  236. .hangup = v253_hangup,
  237. .receive_buf = v253_receive,
  238. .write_wakeup = v253_wakeup,
  239. };
  240. EXPORT_SYMBOL_GPL(v253_ops);
  241. /*
  242. * Codec DAI
  243. */
  244. static struct snd_soc_dai_driver cx20442_dai = {
  245. .name = "cx20442-voice",
  246. .playback = {
  247. .stream_name = "Playback",
  248. .channels_min = 1,
  249. .channels_max = 1,
  250. .rates = SNDRV_PCM_RATE_8000,
  251. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  252. },
  253. .capture = {
  254. .stream_name = "Capture",
  255. .channels_min = 1,
  256. .channels_max = 1,
  257. .rates = SNDRV_PCM_RATE_8000,
  258. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  259. },
  260. };
  261. static int cx20442_set_bias_level(struct snd_soc_component *component,
  262. enum snd_soc_bias_level level)
  263. {
  264. struct cx20442_priv *cx20442 = snd_soc_component_get_drvdata(component);
  265. int err = 0;
  266. switch (level) {
  267. case SND_SOC_BIAS_PREPARE:
  268. if (snd_soc_component_get_bias_level(component) != SND_SOC_BIAS_STANDBY)
  269. break;
  270. if (IS_ERR(cx20442->por))
  271. err = PTR_ERR(cx20442->por);
  272. else
  273. err = regulator_enable(cx20442->por);
  274. break;
  275. case SND_SOC_BIAS_STANDBY:
  276. if (snd_soc_component_get_bias_level(component) != SND_SOC_BIAS_PREPARE)
  277. break;
  278. if (IS_ERR(cx20442->por))
  279. err = PTR_ERR(cx20442->por);
  280. else
  281. err = regulator_disable(cx20442->por);
  282. break;
  283. default:
  284. break;
  285. }
  286. return err;
  287. }
  288. static int cx20442_component_probe(struct snd_soc_component *component)
  289. {
  290. struct cx20442_priv *cx20442;
  291. cx20442 = kzalloc(sizeof(struct cx20442_priv), GFP_KERNEL);
  292. if (cx20442 == NULL)
  293. return -ENOMEM;
  294. cx20442->por = regulator_get(component->dev, "POR");
  295. if (IS_ERR(cx20442->por)) {
  296. int err = PTR_ERR(cx20442->por);
  297. dev_warn(component->dev, "failed to get POR supply (%d)", err);
  298. /*
  299. * When running on a non-dt platform and requested regulator
  300. * is not available, regulator_get() never returns
  301. * -EPROBE_DEFER as it is not able to justify if the regulator
  302. * may still appear later. On the other hand, the board can
  303. * still set full constraints flag at late_initcall in order
  304. * to instruct regulator_get() to return a dummy one if
  305. * sufficient. Hence, if we get -ENODEV here, let's convert
  306. * it to -EPROBE_DEFER and wait for the board to decide or
  307. * let Deferred Probe infrastructure handle this error.
  308. */
  309. if (err == -ENODEV)
  310. err = -EPROBE_DEFER;
  311. kfree(cx20442);
  312. return err;
  313. }
  314. cx20442->tty = NULL;
  315. snd_soc_component_set_drvdata(component, cx20442);
  316. component->card->pop_time = 0;
  317. return 0;
  318. }
  319. /* power down chip */
  320. static void cx20442_component_remove(struct snd_soc_component *component)
  321. {
  322. struct cx20442_priv *cx20442 = snd_soc_component_get_drvdata(component);
  323. if (cx20442->tty) {
  324. struct tty_struct *tty = cx20442->tty;
  325. tty_hangup(tty);
  326. }
  327. if (!IS_ERR(cx20442->por)) {
  328. /* should be already in STANDBY, hence disabled */
  329. regulator_put(cx20442->por);
  330. }
  331. snd_soc_component_set_drvdata(component, NULL);
  332. kfree(cx20442);
  333. }
  334. static const struct snd_soc_component_driver cx20442_component_dev = {
  335. .probe = cx20442_component_probe,
  336. .remove = cx20442_component_remove,
  337. .set_bias_level = cx20442_set_bias_level,
  338. .read = cx20442_read_reg_cache,
  339. .write = cx20442_write,
  340. .dapm_widgets = cx20442_dapm_widgets,
  341. .num_dapm_widgets = ARRAY_SIZE(cx20442_dapm_widgets),
  342. .dapm_routes = cx20442_audio_map,
  343. .num_dapm_routes = ARRAY_SIZE(cx20442_audio_map),
  344. .idle_bias_on = 1,
  345. .use_pmdown_time = 1,
  346. .endianness = 1,
  347. .non_legacy_dai_naming = 1,
  348. };
  349. static int cx20442_platform_probe(struct platform_device *pdev)
  350. {
  351. return devm_snd_soc_register_component(&pdev->dev,
  352. &cx20442_component_dev, &cx20442_dai, 1);
  353. }
  354. static struct platform_driver cx20442_platform_driver = {
  355. .driver = {
  356. .name = "cx20442-codec",
  357. },
  358. .probe = cx20442_platform_probe,
  359. };
  360. module_platform_driver(cx20442_platform_driver);
  361. MODULE_DESCRIPTION("ASoC CX20442-11 voice modem codec driver");
  362. MODULE_AUTHOR("Janusz Krzysztofik");
  363. MODULE_LICENSE("GPL");
  364. MODULE_ALIAS("platform:cx20442-codec");