omap-twl4030.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. /*
  2. * omap-twl4030.c -- SoC audio for TI SoC based boards with twl4030 codec
  3. *
  4. * Copyright (C) 2012 Texas Instruments Incorporated - http://www.ti.com
  5. * All rights reserved.
  6. *
  7. * Author: Peter Ujfalusi <peter.ujfalusi@ti.com>
  8. *
  9. * This driver replaces the following machine drivers:
  10. * omap3beagle (Author: Steve Sakoman <steve@sakoman.com>)
  11. * omap3evm (Author: Anuj Aggarwal <anuj.aggarwal@ti.com>)
  12. * overo (Author: Steve Sakoman <steve@sakoman.com>)
  13. * igep0020 (Author: Enric Balletbo i Serra <eballetbo@iseebcn.com>)
  14. * zoom2 (Author: Misael Lopez Cruz <misael.lopez@ti.com>)
  15. * sdp3430 (Author: Misael Lopez Cruz <misael.lopez@ti.com>)
  16. *
  17. * This program is free software; you can redistribute it and/or
  18. * modify it under the terms of the GNU General Public License
  19. * version 2 as published by the Free Software Foundation.
  20. *
  21. * This program is distributed in the hope that it will be useful, but
  22. * WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  24. * General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU General Public License
  27. * along with this program; if not, write to the Free Software
  28. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  29. * 02110-1301 USA
  30. *
  31. */
  32. #include <linux/platform_device.h>
  33. #include <linux/platform_data/omap-twl4030.h>
  34. #include <linux/module.h>
  35. #include <linux/of.h>
  36. #include <linux/gpio.h>
  37. #include <linux/of_gpio.h>
  38. #include <sound/core.h>
  39. #include <sound/pcm.h>
  40. #include <sound/soc.h>
  41. #include <sound/jack.h>
  42. #include "omap-mcbsp.h"
  43. #include "omap-pcm.h"
  44. struct omap_twl4030 {
  45. int jack_detect; /* board can detect jack events */
  46. struct snd_soc_jack hs_jack;
  47. };
  48. static int omap_twl4030_hw_params(struct snd_pcm_substream *substream,
  49. struct snd_pcm_hw_params *params)
  50. {
  51. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  52. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  53. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  54. struct snd_soc_codec *codec = rtd->codec;
  55. struct snd_soc_card *card = codec->card;
  56. unsigned int fmt;
  57. int ret;
  58. switch (params_channels(params)) {
  59. case 2: /* Stereo I2S mode */
  60. fmt = SND_SOC_DAIFMT_I2S |
  61. SND_SOC_DAIFMT_NB_NF |
  62. SND_SOC_DAIFMT_CBM_CFM;
  63. break;
  64. case 4: /* Four channel TDM mode */
  65. fmt = SND_SOC_DAIFMT_DSP_A |
  66. SND_SOC_DAIFMT_IB_NF |
  67. SND_SOC_DAIFMT_CBM_CFM;
  68. break;
  69. default:
  70. return -EINVAL;
  71. }
  72. /* Set codec DAI configuration */
  73. ret = snd_soc_dai_set_fmt(codec_dai, fmt);
  74. if (ret < 0) {
  75. dev_err(card->dev, "can't set codec DAI configuration\n");
  76. return ret;
  77. }
  78. /* Set cpu DAI configuration */
  79. ret = snd_soc_dai_set_fmt(cpu_dai, fmt);
  80. if (ret < 0) {
  81. dev_err(card->dev, "can't set cpu DAI configuration\n");
  82. return ret;
  83. }
  84. return 0;
  85. }
  86. static struct snd_soc_ops omap_twl4030_ops = {
  87. .hw_params = omap_twl4030_hw_params,
  88. };
  89. static const struct snd_soc_dapm_widget dapm_widgets[] = {
  90. SND_SOC_DAPM_SPK("Earpiece Spk", NULL),
  91. SND_SOC_DAPM_SPK("Handsfree Spk", NULL),
  92. SND_SOC_DAPM_HP("Headset Stereophone", NULL),
  93. SND_SOC_DAPM_SPK("Ext Spk", NULL),
  94. SND_SOC_DAPM_SPK("Carkit Spk", NULL),
  95. SND_SOC_DAPM_MIC("Main Mic", NULL),
  96. SND_SOC_DAPM_MIC("Sub Mic", NULL),
  97. SND_SOC_DAPM_MIC("Headset Mic", NULL),
  98. SND_SOC_DAPM_MIC("Carkit Mic", NULL),
  99. SND_SOC_DAPM_MIC("Digital0 Mic", NULL),
  100. SND_SOC_DAPM_MIC("Digital1 Mic", NULL),
  101. SND_SOC_DAPM_LINE("Line In", NULL),
  102. };
  103. static const struct snd_soc_dapm_route audio_map[] = {
  104. /* Headset Stereophone: HSOL, HSOR */
  105. {"Headset Stereophone", NULL, "HSOL"},
  106. {"Headset Stereophone", NULL, "HSOR"},
  107. /* External Speakers: HFL, HFR */
  108. {"Handsfree Spk", NULL, "HFL"},
  109. {"Handsfree Spk", NULL, "HFR"},
  110. /* External Speakers: PredrivL, PredrivR */
  111. {"Ext Spk", NULL, "PREDRIVEL"},
  112. {"Ext Spk", NULL, "PREDRIVER"},
  113. /* Carkit speakers: CARKITL, CARKITR */
  114. {"Carkit Spk", NULL, "CARKITL"},
  115. {"Carkit Spk", NULL, "CARKITR"},
  116. /* Earpiece */
  117. {"Earpiece Spk", NULL, "EARPIECE"},
  118. /* External Mics: MAINMIC, SUBMIC with bias */
  119. {"MAINMIC", NULL, "Main Mic"},
  120. {"Main Mic", NULL, "Mic Bias 1"},
  121. {"SUBMIC", NULL, "Sub Mic"},
  122. {"Sub Mic", NULL, "Mic Bias 2"},
  123. /* Headset Mic: HSMIC with bias */
  124. {"HSMIC", NULL, "Headset Mic"},
  125. {"Headset Mic", NULL, "Headset Mic Bias"},
  126. /* Digital Mics: DIGIMIC0, DIGIMIC1 with bias */
  127. {"DIGIMIC0", NULL, "Digital0 Mic"},
  128. {"Digital0 Mic", NULL, "Mic Bias 1"},
  129. {"DIGIMIC1", NULL, "Digital1 Mic"},
  130. {"Digital1 Mic", NULL, "Mic Bias 2"},
  131. /* Carkit In: CARKITMIC */
  132. {"CARKITMIC", NULL, "Carkit Mic"},
  133. /* Aux In: AUXL, AUXR */
  134. {"AUXL", NULL, "Line In"},
  135. {"AUXR", NULL, "Line In"},
  136. };
  137. /* Headset jack detection DAPM pins */
  138. static struct snd_soc_jack_pin hs_jack_pins[] = {
  139. {
  140. .pin = "Headset Mic",
  141. .mask = SND_JACK_MICROPHONE,
  142. },
  143. {
  144. .pin = "Headset Stereophone",
  145. .mask = SND_JACK_HEADPHONE,
  146. },
  147. };
  148. /* Headset jack detection gpios */
  149. static struct snd_soc_jack_gpio hs_jack_gpios[] = {
  150. {
  151. .name = "hsdet-gpio",
  152. .report = SND_JACK_HEADSET,
  153. .debounce_time = 200,
  154. },
  155. };
  156. static inline void twl4030_disconnect_pin(struct snd_soc_dapm_context *dapm,
  157. int connected, char *pin)
  158. {
  159. if (!connected)
  160. snd_soc_dapm_disable_pin(dapm, pin);
  161. }
  162. static int omap_twl4030_init(struct snd_soc_pcm_runtime *rtd)
  163. {
  164. struct snd_soc_codec *codec = rtd->codec;
  165. struct snd_soc_card *card = codec->card;
  166. struct snd_soc_dapm_context *dapm = &codec->dapm;
  167. struct omap_tw4030_pdata *pdata = dev_get_platdata(card->dev);
  168. struct omap_twl4030 *priv = snd_soc_card_get_drvdata(card);
  169. int ret = 0;
  170. /* Headset jack detection only if it is supported */
  171. if (priv->jack_detect > 0) {
  172. hs_jack_gpios[0].gpio = priv->jack_detect;
  173. ret = snd_soc_jack_new(codec, "Headset Jack", SND_JACK_HEADSET,
  174. &priv->hs_jack);
  175. if (ret)
  176. return ret;
  177. ret = snd_soc_jack_add_pins(&priv->hs_jack,
  178. ARRAY_SIZE(hs_jack_pins),
  179. hs_jack_pins);
  180. if (ret)
  181. return ret;
  182. ret = snd_soc_jack_add_gpios(&priv->hs_jack,
  183. ARRAY_SIZE(hs_jack_gpios),
  184. hs_jack_gpios);
  185. if (ret)
  186. return ret;
  187. }
  188. /*
  189. * NULL pdata means we booted with DT. In this case the routing is
  190. * provided and the card is fully routed, no need to mark pins.
  191. */
  192. if (!pdata || !pdata->custom_routing)
  193. return ret;
  194. /* Disable not connected paths if not used */
  195. twl4030_disconnect_pin(dapm, pdata->has_ear, "Earpiece Spk");
  196. twl4030_disconnect_pin(dapm, pdata->has_hf, "Handsfree Spk");
  197. twl4030_disconnect_pin(dapm, pdata->has_hs, "Headset Stereophone");
  198. twl4030_disconnect_pin(dapm, pdata->has_predriv, "Ext Spk");
  199. twl4030_disconnect_pin(dapm, pdata->has_carkit, "Carkit Spk");
  200. twl4030_disconnect_pin(dapm, pdata->has_mainmic, "Main Mic");
  201. twl4030_disconnect_pin(dapm, pdata->has_submic, "Sub Mic");
  202. twl4030_disconnect_pin(dapm, pdata->has_hsmic, "Headset Mic");
  203. twl4030_disconnect_pin(dapm, pdata->has_carkitmic, "Carkit Mic");
  204. twl4030_disconnect_pin(dapm, pdata->has_digimic0, "Digital0 Mic");
  205. twl4030_disconnect_pin(dapm, pdata->has_digimic1, "Digital1 Mic");
  206. twl4030_disconnect_pin(dapm, pdata->has_linein, "Line In");
  207. return ret;
  208. }
  209. /* Digital audio interface glue - connects codec <--> CPU */
  210. static struct snd_soc_dai_link omap_twl4030_dai_links[] = {
  211. {
  212. .name = "TWL4030 HiFi",
  213. .stream_name = "TWL4030 HiFi",
  214. .cpu_dai_name = "omap-mcbsp.2",
  215. .codec_dai_name = "twl4030-hifi",
  216. .platform_name = "omap-pcm-audio",
  217. .codec_name = "twl4030-codec",
  218. .init = omap_twl4030_init,
  219. .ops = &omap_twl4030_ops,
  220. },
  221. {
  222. .name = "TWL4030 Voice",
  223. .stream_name = "TWL4030 Voice",
  224. .cpu_dai_name = "omap-mcbsp.3",
  225. .codec_dai_name = "twl4030-voice",
  226. .platform_name = "omap-pcm-audio",
  227. .codec_name = "twl4030-codec",
  228. .dai_fmt = SND_SOC_DAIFMT_DSP_A | SND_SOC_DAIFMT_IB_NF |
  229. SND_SOC_DAIFMT_CBM_CFM,
  230. },
  231. };
  232. /* Audio machine driver */
  233. static struct snd_soc_card omap_twl4030_card = {
  234. .owner = THIS_MODULE,
  235. .dai_link = omap_twl4030_dai_links,
  236. .num_links = ARRAY_SIZE(omap_twl4030_dai_links),
  237. .dapm_widgets = dapm_widgets,
  238. .num_dapm_widgets = ARRAY_SIZE(dapm_widgets),
  239. .dapm_routes = audio_map,
  240. .num_dapm_routes = ARRAY_SIZE(audio_map),
  241. };
  242. static int omap_twl4030_probe(struct platform_device *pdev)
  243. {
  244. struct omap_tw4030_pdata *pdata = dev_get_platdata(&pdev->dev);
  245. struct device_node *node = pdev->dev.of_node;
  246. struct snd_soc_card *card = &omap_twl4030_card;
  247. struct omap_twl4030 *priv;
  248. int ret = 0;
  249. card->dev = &pdev->dev;
  250. priv = devm_kzalloc(&pdev->dev, sizeof(struct omap_twl4030), GFP_KERNEL);
  251. if (priv == NULL)
  252. return -ENOMEM;
  253. if (node) {
  254. struct device_node *dai_node;
  255. struct property *prop;
  256. if (snd_soc_of_parse_card_name(card, "ti,model")) {
  257. dev_err(&pdev->dev, "Card name is not provided\n");
  258. return -ENODEV;
  259. }
  260. dai_node = of_parse_phandle(node, "ti,mcbsp", 0);
  261. if (!dai_node) {
  262. dev_err(&pdev->dev, "McBSP node is not provided\n");
  263. return -EINVAL;
  264. }
  265. omap_twl4030_dai_links[0].cpu_dai_name = NULL;
  266. omap_twl4030_dai_links[0].cpu_of_node = dai_node;
  267. dai_node = of_parse_phandle(node, "ti,mcbsp-voice", 0);
  268. if (!dai_node) {
  269. card->num_links = 1;
  270. } else {
  271. omap_twl4030_dai_links[1].cpu_dai_name = NULL;
  272. omap_twl4030_dai_links[1].cpu_of_node = dai_node;
  273. }
  274. priv->jack_detect = of_get_named_gpio(node,
  275. "ti,jack-det-gpio", 0);
  276. /* Optional: audio routing can be provided */
  277. prop = of_find_property(node, "ti,audio-routing", NULL);
  278. if (prop) {
  279. ret = snd_soc_of_parse_audio_routing(card,
  280. "ti,audio-routing");
  281. if (ret)
  282. return ret;
  283. card->fully_routed = 1;
  284. }
  285. } else if (pdata) {
  286. if (pdata->card_name) {
  287. card->name = pdata->card_name;
  288. } else {
  289. dev_err(&pdev->dev, "Card name is not provided\n");
  290. return -ENODEV;
  291. }
  292. if (!pdata->voice_connected)
  293. card->num_links = 1;
  294. priv->jack_detect = pdata->jack_detect;
  295. } else {
  296. dev_err(&pdev->dev, "Missing pdata\n");
  297. return -ENODEV;
  298. }
  299. snd_soc_card_set_drvdata(card, priv);
  300. ret = snd_soc_register_card(card);
  301. if (ret) {
  302. dev_err(&pdev->dev, "snd_soc_register_card() failed: %d\n",
  303. ret);
  304. return ret;
  305. }
  306. return 0;
  307. }
  308. static int omap_twl4030_remove(struct platform_device *pdev)
  309. {
  310. struct snd_soc_card *card = platform_get_drvdata(pdev);
  311. struct omap_twl4030 *priv = snd_soc_card_get_drvdata(card);
  312. if (priv->jack_detect > 0)
  313. snd_soc_jack_free_gpios(&priv->hs_jack,
  314. ARRAY_SIZE(hs_jack_gpios),
  315. hs_jack_gpios);
  316. snd_soc_unregister_card(card);
  317. return 0;
  318. }
  319. static const struct of_device_id omap_twl4030_of_match[] = {
  320. {.compatible = "ti,omap-twl4030", },
  321. { },
  322. };
  323. MODULE_DEVICE_TABLE(of, omap_twl4030_of_match);
  324. static struct platform_driver omap_twl4030_driver = {
  325. .driver = {
  326. .name = "omap-twl4030",
  327. .owner = THIS_MODULE,
  328. .pm = &snd_soc_pm_ops,
  329. .of_match_table = omap_twl4030_of_match,
  330. },
  331. .probe = omap_twl4030_probe,
  332. .remove = omap_twl4030_remove,
  333. };
  334. module_platform_driver(omap_twl4030_driver);
  335. MODULE_AUTHOR("Peter Ujfalusi <peter.ujfalusi@ti.com>");
  336. MODULE_DESCRIPTION("ALSA SoC for TI SoC based boards with twl4030 codec");
  337. MODULE_LICENSE("GPL");
  338. MODULE_ALIAS("platform:omap-twl4030");