cx20442.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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 <sound/core.h>
  16. #include <sound/initval.h>
  17. #include <sound/soc-dapm.h>
  18. #include "cx20442.h"
  19. struct cx20442_priv {
  20. struct snd_soc_codec codec;
  21. u8 reg_cache[1];
  22. };
  23. #define CX20442_PM 0x0
  24. #define CX20442_TELIN 0
  25. #define CX20442_TELOUT 1
  26. #define CX20442_MIC 2
  27. #define CX20442_SPKOUT 3
  28. #define CX20442_AGC 4
  29. static const struct snd_soc_dapm_widget cx20442_dapm_widgets[] = {
  30. SND_SOC_DAPM_OUTPUT("TELOUT"),
  31. SND_SOC_DAPM_OUTPUT("SPKOUT"),
  32. SND_SOC_DAPM_OUTPUT("AGCOUT"),
  33. SND_SOC_DAPM_MIXER("SPKOUT Mixer", SND_SOC_NOPM, 0, 0, NULL, 0),
  34. SND_SOC_DAPM_PGA("TELOUT Amp", CX20442_PM, CX20442_TELOUT, 0, NULL, 0),
  35. SND_SOC_DAPM_PGA("SPKOUT Amp", CX20442_PM, CX20442_SPKOUT, 0, NULL, 0),
  36. SND_SOC_DAPM_PGA("SPKOUT AGC", CX20442_PM, CX20442_AGC, 0, NULL, 0),
  37. SND_SOC_DAPM_DAC("DAC", "Playback", SND_SOC_NOPM, 0, 0),
  38. SND_SOC_DAPM_ADC("ADC", "Capture", SND_SOC_NOPM, 0, 0),
  39. SND_SOC_DAPM_MIXER("Input Mixer", SND_SOC_NOPM, 0, 0, NULL, 0),
  40. SND_SOC_DAPM_MICBIAS("TELIN Bias", CX20442_PM, CX20442_TELIN, 0),
  41. SND_SOC_DAPM_MICBIAS("MIC Bias", CX20442_PM, CX20442_MIC, 0),
  42. SND_SOC_DAPM_PGA("MIC AGC", CX20442_PM, CX20442_AGC, 0, NULL, 0),
  43. SND_SOC_DAPM_INPUT("TELIN"),
  44. SND_SOC_DAPM_INPUT("MIC"),
  45. SND_SOC_DAPM_INPUT("AGCIN"),
  46. };
  47. static const struct snd_soc_dapm_route cx20442_audio_map[] = {
  48. {"TELOUT", NULL, "TELOUT Amp"},
  49. {"SPKOUT", NULL, "SPKOUT Mixer"},
  50. {"SPKOUT Mixer", NULL, "SPKOUT Amp"},
  51. {"TELOUT Amp", NULL, "DAC"},
  52. {"SPKOUT Amp", NULL, "DAC"},
  53. {"SPKOUT Mixer", NULL, "SPKOUT AGC"},
  54. {"SPKOUT AGC", NULL, "AGCIN"},
  55. {"AGCOUT", NULL, "MIC AGC"},
  56. {"MIC AGC", NULL, "MIC"},
  57. {"MIC Bias", NULL, "MIC"},
  58. {"Input Mixer", NULL, "MIC Bias"},
  59. {"TELIN Bias", NULL, "TELIN"},
  60. {"Input Mixer", NULL, "TELIN Bias"},
  61. {"ADC", NULL, "Input Mixer"},
  62. };
  63. static int cx20442_add_widgets(struct snd_soc_codec *codec)
  64. {
  65. snd_soc_dapm_new_controls(codec, cx20442_dapm_widgets,
  66. ARRAY_SIZE(cx20442_dapm_widgets));
  67. snd_soc_dapm_add_routes(codec, cx20442_audio_map,
  68. ARRAY_SIZE(cx20442_audio_map));
  69. snd_soc_dapm_new_widgets(codec);
  70. return 0;
  71. }
  72. static unsigned int cx20442_read_reg_cache(struct snd_soc_codec *codec,
  73. unsigned int reg)
  74. {
  75. u8 *reg_cache = codec->reg_cache;
  76. if (reg >= codec->reg_cache_size)
  77. return -EINVAL;
  78. return reg_cache[reg];
  79. }
  80. enum v253_vls {
  81. V253_VLS_NONE = 0,
  82. V253_VLS_T,
  83. V253_VLS_L,
  84. V253_VLS_LT,
  85. V253_VLS_S,
  86. V253_VLS_ST,
  87. V253_VLS_M,
  88. V253_VLS_MST,
  89. V253_VLS_S1,
  90. V253_VLS_S1T,
  91. V253_VLS_MS1T,
  92. V253_VLS_M1,
  93. V253_VLS_M1ST,
  94. V253_VLS_M1S1T,
  95. V253_VLS_H,
  96. V253_VLS_HT,
  97. V253_VLS_MS,
  98. V253_VLS_MS1,
  99. V253_VLS_M1S,
  100. V253_VLS_M1S1,
  101. V253_VLS_TEST,
  102. };
  103. static int cx20442_pm_to_v253_vls(u8 value)
  104. {
  105. switch (value & ~(1 << CX20442_AGC)) {
  106. case 0:
  107. return V253_VLS_T;
  108. case (1 << CX20442_SPKOUT):
  109. return V253_VLS_S1;
  110. case (1 << CX20442_MIC):
  111. return V253_VLS_M1;
  112. case (1 << CX20442_SPKOUT) | (1 << CX20442_MIC):
  113. return V253_VLS_M1S1;
  114. case (1 << CX20442_TELOUT):
  115. case (1 << CX20442_TELIN):
  116. case (1 << CX20442_TELOUT) | (1 << CX20442_TELIN):
  117. return V253_VLS_L;
  118. case (1 << CX20442_TELOUT) | (1 << CX20442_MIC):
  119. return V253_VLS_NONE;
  120. }
  121. return -EINVAL;
  122. }
  123. static int cx20442_pm_to_v253_vsp(u8 value)
  124. {
  125. switch (value & ~(1 << CX20442_AGC)) {
  126. case (1 << CX20442_SPKOUT):
  127. case (1 << CX20442_MIC):
  128. case (1 << CX20442_SPKOUT) | (1 << CX20442_MIC):
  129. return (bool)(value & (1 << CX20442_AGC));
  130. }
  131. return (value & (1 << CX20442_AGC)) ? -EINVAL : 0;
  132. }
  133. static int cx20442_write(struct snd_soc_codec *codec, unsigned int reg,
  134. unsigned int value)
  135. {
  136. u8 *reg_cache = codec->reg_cache;
  137. int vls, vsp, old, len;
  138. char buf[18];
  139. if (reg >= codec->reg_cache_size)
  140. return -EINVAL;
  141. /* hw_write and control_data pointers required for talking to the modem
  142. * are expected to be set by the machine driver's line discipline
  143. * initialization code */
  144. if (!codec->hw_write || !codec->control_data)
  145. return -EIO;
  146. old = reg_cache[reg];
  147. reg_cache[reg] = value;
  148. vls = cx20442_pm_to_v253_vls(value);
  149. if (vls < 0)
  150. return vls;
  151. vsp = cx20442_pm_to_v253_vsp(value);
  152. if (vsp < 0)
  153. return vsp;
  154. if ((vls == V253_VLS_T) ||
  155. (vls == cx20442_pm_to_v253_vls(old))) {
  156. if (vsp == cx20442_pm_to_v253_vsp(old))
  157. return 0;
  158. len = snprintf(buf, ARRAY_SIZE(buf), "at+vsp=%d\r", vsp);
  159. } else if (vsp == cx20442_pm_to_v253_vsp(old))
  160. len = snprintf(buf, ARRAY_SIZE(buf), "at+vls=%d\r", vls);
  161. else
  162. len = snprintf(buf, ARRAY_SIZE(buf),
  163. "at+vls=%d;+vsp=%d\r", vls, vsp);
  164. if (unlikely(len > (ARRAY_SIZE(buf) - 1)))
  165. return -ENOMEM;
  166. if (codec->hw_write(codec->control_data, buf, len) != len)
  167. return -EIO;
  168. return 0;
  169. }
  170. struct snd_soc_dai cx20442_dai = {
  171. .name = "CX20442",
  172. .playback = {
  173. .stream_name = "Playback",
  174. .channels_min = 1,
  175. .channels_max = 1,
  176. .rates = SNDRV_PCM_RATE_8000,
  177. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  178. },
  179. .capture = {
  180. .stream_name = "Capture",
  181. .channels_min = 1,
  182. .channels_max = 1,
  183. .rates = SNDRV_PCM_RATE_8000,
  184. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  185. },
  186. };
  187. EXPORT_SYMBOL_GPL(cx20442_dai);
  188. static struct snd_soc_codec *cx20442_codec;
  189. static int cx20442_codec_probe(struct platform_device *pdev)
  190. {
  191. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  192. struct snd_soc_codec *codec;
  193. int ret;
  194. if (!cx20442_codec) {
  195. dev_err(&pdev->dev, "cx20442 not yet discovered\n");
  196. return -ENODEV;
  197. }
  198. codec = cx20442_codec;
  199. socdev->card->codec = codec;
  200. /* register pcms */
  201. ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
  202. if (ret < 0) {
  203. dev_err(&pdev->dev, "failed to create pcms\n");
  204. goto pcm_err;
  205. }
  206. cx20442_add_widgets(codec);
  207. ret = snd_soc_init_card(socdev);
  208. if (ret < 0) {
  209. dev_err(&pdev->dev, "failed to register card\n");
  210. goto card_err;
  211. }
  212. return ret;
  213. card_err:
  214. snd_soc_free_pcms(socdev);
  215. snd_soc_dapm_free(socdev);
  216. pcm_err:
  217. return ret;
  218. }
  219. /* power down chip */
  220. static int cx20442_codec_remove(struct platform_device *pdev)
  221. {
  222. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  223. snd_soc_free_pcms(socdev);
  224. snd_soc_dapm_free(socdev);
  225. return 0;
  226. }
  227. struct snd_soc_codec_device cx20442_codec_dev = {
  228. .probe = cx20442_codec_probe,
  229. .remove = cx20442_codec_remove,
  230. };
  231. EXPORT_SYMBOL_GPL(cx20442_codec_dev);
  232. static int cx20442_register(struct cx20442_priv *cx20442)
  233. {
  234. struct snd_soc_codec *codec = &cx20442->codec;
  235. int ret;
  236. mutex_init(&codec->mutex);
  237. INIT_LIST_HEAD(&codec->dapm_widgets);
  238. INIT_LIST_HEAD(&codec->dapm_paths);
  239. codec->name = "CX20442";
  240. codec->owner = THIS_MODULE;
  241. codec->private_data = cx20442;
  242. codec->dai = &cx20442_dai;
  243. codec->num_dai = 1;
  244. codec->reg_cache = &cx20442->reg_cache;
  245. codec->reg_cache_size = ARRAY_SIZE(cx20442->reg_cache);
  246. codec->read = cx20442_read_reg_cache;
  247. codec->write = cx20442_write;
  248. codec->bias_level = SND_SOC_BIAS_OFF;
  249. cx20442_dai.dev = codec->dev;
  250. cx20442_codec = codec;
  251. ret = snd_soc_register_codec(codec);
  252. if (ret != 0) {
  253. dev_err(&codec->dev, "Failed to register codec: %d\n", ret);
  254. goto err;
  255. }
  256. ret = snd_soc_register_dai(&cx20442_dai);
  257. if (ret != 0) {
  258. dev_err(&codec->dev, "Failed to register DAI: %d\n", ret);
  259. goto err_codec;
  260. }
  261. return 0;
  262. err_codec:
  263. snd_soc_unregister_codec(codec);
  264. err:
  265. cx20442_codec = NULL;
  266. kfree(cx20442);
  267. return ret;
  268. }
  269. static void cx20442_unregister(struct cx20442_priv *cx20442)
  270. {
  271. snd_soc_unregister_dai(&cx20442_dai);
  272. snd_soc_unregister_codec(&cx20442->codec);
  273. cx20442_codec = NULL;
  274. kfree(cx20442);
  275. }
  276. static int cx20442_platform_probe(struct platform_device *pdev)
  277. {
  278. struct cx20442_priv *cx20442;
  279. struct snd_soc_codec *codec;
  280. cx20442 = kzalloc(sizeof(struct cx20442_priv), GFP_KERNEL);
  281. if (cx20442 == NULL)
  282. return -ENOMEM;
  283. codec = &cx20442->codec;
  284. codec->control_data = NULL;
  285. codec->hw_write = NULL;
  286. codec->pop_time = 0;
  287. codec->dev = &pdev->dev;
  288. platform_set_drvdata(pdev, cx20442);
  289. return cx20442_register(cx20442);
  290. }
  291. static int __exit cx20442_platform_remove(struct platform_device *pdev)
  292. {
  293. struct cx20442_priv *cx20442 = platform_get_drvdata(pdev);
  294. cx20442_unregister(cx20442);
  295. return 0;
  296. }
  297. static struct platform_driver cx20442_platform_driver = {
  298. .driver = {
  299. .name = "cx20442",
  300. .owner = THIS_MODULE,
  301. },
  302. .probe = cx20442_platform_probe,
  303. .remove = __exit_p(cx20442_platform_remove),
  304. };
  305. static int __init cx20442_init(void)
  306. {
  307. return platform_driver_register(&cx20442_platform_driver);
  308. }
  309. module_init(cx20442_init);
  310. static void __exit cx20442_exit(void)
  311. {
  312. platform_driver_unregister(&cx20442_platform_driver);
  313. }
  314. module_exit(cx20442_exit);
  315. MODULE_DESCRIPTION("ASoC CX20442-11 voice modem codec driver");
  316. MODULE_AUTHOR("Janusz Krzysztofik");
  317. MODULE_LICENSE("GPL");
  318. MODULE_ALIAS("platform:cx20442");