omap-twl4030.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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. struct omap_twl4030 {
  44. int jack_detect; /* board can detect jack events */
  45. struct snd_soc_jack hs_jack;
  46. };
  47. static int omap_twl4030_hw_params(struct snd_pcm_substream *substream,
  48. struct snd_pcm_hw_params *params)
  49. {
  50. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  51. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  52. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  53. struct snd_soc_codec *codec = rtd->codec;
  54. struct snd_soc_card *card = codec->card;
  55. unsigned int fmt;
  56. int ret;
  57. switch (params_channels(params)) {
  58. case 2: /* Stereo I2S mode */
  59. fmt = SND_SOC_DAIFMT_I2S |
  60. SND_SOC_DAIFMT_NB_NF |
  61. SND_SOC_DAIFMT_CBM_CFM;
  62. break;
  63. case 4: /* Four channel TDM mode */
  64. fmt = SND_SOC_DAIFMT_DSP_A |
  65. SND_SOC_DAIFMT_IB_NF |
  66. SND_SOC_DAIFMT_CBM_CFM;
  67. break;
  68. default:
  69. return -EINVAL;
  70. }
  71. /* Set codec DAI configuration */
  72. ret = snd_soc_dai_set_fmt(codec_dai, fmt);
  73. if (ret < 0) {
  74. dev_err(card->dev, "can't set codec DAI configuration\n");
  75. return ret;
  76. }
  77. /* Set cpu DAI configuration */
  78. ret = snd_soc_dai_set_fmt(cpu_dai, fmt);
  79. if (ret < 0) {
  80. dev_err(card->dev, "can't set cpu DAI configuration\n");
  81. return ret;
  82. }
  83. return 0;
  84. }
  85. static struct snd_soc_ops omap_twl4030_ops = {
  86. .hw_params = omap_twl4030_hw_params,
  87. };
  88. static const struct snd_soc_dapm_widget dapm_widgets[] = {
  89. SND_SOC_DAPM_SPK("Earpiece Spk", NULL),
  90. SND_SOC_DAPM_SPK("Handsfree Spk", NULL),
  91. SND_SOC_DAPM_HP("Headset Stereophone", NULL),
  92. SND_SOC_DAPM_SPK("Ext Spk", NULL),
  93. SND_SOC_DAPM_SPK("Carkit Spk", NULL),
  94. SND_SOC_DAPM_MIC("Main Mic", NULL),
  95. SND_SOC_DAPM_MIC("Sub Mic", NULL),
  96. SND_SOC_DAPM_MIC("Headset Mic", NULL),
  97. SND_SOC_DAPM_MIC("Carkit Mic", NULL),
  98. SND_SOC_DAPM_MIC("Digital0 Mic", NULL),
  99. SND_SOC_DAPM_MIC("Digital1 Mic", NULL),
  100. SND_SOC_DAPM_LINE("Line In", NULL),
  101. };
  102. static const struct snd_soc_dapm_route audio_map[] = {
  103. /* Headset Stereophone: HSOL, HSOR */
  104. {"Headset Stereophone", NULL, "HSOL"},
  105. {"Headset Stereophone", NULL, "HSOR"},
  106. /* External Speakers: HFL, HFR */
  107. {"Handsfree Spk", NULL, "HFL"},
  108. {"Handsfree Spk", NULL, "HFR"},
  109. /* External Speakers: PredrivL, PredrivR */
  110. {"Ext Spk", NULL, "PREDRIVEL"},
  111. {"Ext Spk", NULL, "PREDRIVER"},
  112. /* Carkit speakers: CARKITL, CARKITR */
  113. {"Carkit Spk", NULL, "CARKITL"},
  114. {"Carkit Spk", NULL, "CARKITR"},
  115. /* Earpiece */
  116. {"Earpiece Spk", NULL, "EARPIECE"},
  117. /* External Mics: MAINMIC, SUBMIC with bias */
  118. {"MAINMIC", NULL, "Main Mic"},
  119. {"Main Mic", NULL, "Mic Bias 1"},
  120. {"SUBMIC", NULL, "Sub Mic"},
  121. {"Sub Mic", NULL, "Mic Bias 2"},
  122. /* Headset Mic: HSMIC with bias */
  123. {"HSMIC", NULL, "Headset Mic"},
  124. {"Headset Mic", NULL, "Headset Mic Bias"},
  125. /* Digital Mics: DIGIMIC0, DIGIMIC1 with bias */
  126. {"DIGIMIC0", NULL, "Digital0 Mic"},
  127. {"Digital0 Mic", NULL, "Mic Bias 1"},
  128. {"DIGIMIC1", NULL, "Digital1 Mic"},
  129. {"Digital1 Mic", NULL, "Mic Bias 2"},
  130. /* Carkit In: CARKITMIC */
  131. {"CARKITMIC", NULL, "Carkit Mic"},
  132. /* Aux In: AUXL, AUXR */
  133. {"AUXL", NULL, "Line In"},
  134. {"AUXR", NULL, "Line In"},
  135. };
  136. /* Headset jack detection DAPM pins */
  137. static struct snd_soc_jack_pin hs_jack_pins[] = {
  138. {
  139. .pin = "Headset Mic",
  140. .mask = SND_JACK_MICROPHONE,
  141. },
  142. {
  143. .pin = "Headset Stereophone",
  144. .mask = SND_JACK_HEADPHONE,
  145. },
  146. };
  147. /* Headset jack detection gpios */
  148. static struct snd_soc_jack_gpio hs_jack_gpios[] = {
  149. {
  150. .name = "hsdet-gpio",
  151. .report = SND_JACK_HEADSET,
  152. .debounce_time = 200,
  153. },
  154. };
  155. static inline void twl4030_disconnect_pin(struct snd_soc_dapm_context *dapm,
  156. int connected, char *pin)
  157. {
  158. if (!connected)
  159. snd_soc_dapm_disable_pin(dapm, pin);
  160. }
  161. static int omap_twl4030_init(struct snd_soc_pcm_runtime *rtd)
  162. {
  163. struct snd_soc_codec *codec = rtd->codec;
  164. struct snd_soc_card *card = codec->card;
  165. struct snd_soc_dapm_context *dapm = &codec->dapm;
  166. struct omap_tw4030_pdata *pdata = dev_get_platdata(card->dev);
  167. struct omap_twl4030 *priv = snd_soc_card_get_drvdata(card);
  168. int ret = 0;
  169. /* Headset jack detection only if it is supported */
  170. if (priv->jack_detect > 0) {
  171. hs_jack_gpios[0].gpio = priv->jack_detect;
  172. ret = snd_soc_jack_new(codec, "Headset Jack", SND_JACK_HEADSET,
  173. &priv->hs_jack);
  174. if (ret)
  175. return ret;
  176. ret = snd_soc_jack_add_pins(&priv->hs_jack,
  177. ARRAY_SIZE(hs_jack_pins),
  178. hs_jack_pins);
  179. if (ret)
  180. return ret;
  181. ret = snd_soc_jack_add_gpios(&priv->hs_jack,
  182. ARRAY_SIZE(hs_jack_gpios),
  183. hs_jack_gpios);
  184. if (ret)
  185. return ret;
  186. }
  187. /*
  188. * NULL pdata means we booted with DT. In this case the routing is
  189. * provided and the card is fully routed, no need to mark pins.
  190. */
  191. if (!pdata || !pdata->custom_routing)
  192. return ret;
  193. /* Disable not connected paths if not used */
  194. twl4030_disconnect_pin(dapm, pdata->has_ear, "Earpiece Spk");
  195. twl4030_disconnect_pin(dapm, pdata->has_hf, "Handsfree Spk");
  196. twl4030_disconnect_pin(dapm, pdata->has_hs, "Headset Stereophone");
  197. twl4030_disconnect_pin(dapm, pdata->has_predriv, "Ext Spk");
  198. twl4030_disconnect_pin(dapm, pdata->has_carkit, "Carkit Spk");
  199. twl4030_disconnect_pin(dapm, pdata->has_mainmic, "Main Mic");
  200. twl4030_disconnect_pin(dapm, pdata->has_submic, "Sub Mic");
  201. twl4030_disconnect_pin(dapm, pdata->has_hsmic, "Headset Mic");
  202. twl4030_disconnect_pin(dapm, pdata->has_carkitmic, "Carkit Mic");
  203. twl4030_disconnect_pin(dapm, pdata->has_digimic0, "Digital0 Mic");
  204. twl4030_disconnect_pin(dapm, pdata->has_digimic1, "Digital1 Mic");
  205. twl4030_disconnect_pin(dapm, pdata->has_linein, "Line In");
  206. return ret;
  207. }
  208. /* Digital audio interface glue - connects codec <--> CPU */
  209. static struct snd_soc_dai_link omap_twl4030_dai_links[] = {
  210. {
  211. .name = "TWL4030 HiFi",
  212. .stream_name = "TWL4030 HiFi",
  213. .cpu_dai_name = "omap-mcbsp.2",
  214. .codec_dai_name = "twl4030-hifi",
  215. .platform_name = "omap-pcm-audio",
  216. .codec_name = "twl4030-codec",
  217. .init = omap_twl4030_init,
  218. .ops = &omap_twl4030_ops,
  219. },
  220. {
  221. .name = "TWL4030 Voice",
  222. .stream_name = "TWL4030 Voice",
  223. .cpu_dai_name = "omap-mcbsp.3",
  224. .codec_dai_name = "twl4030-voice",
  225. .platform_name = "omap-pcm-audio",
  226. .codec_name = "twl4030-codec",
  227. .dai_fmt = SND_SOC_DAIFMT_DSP_A | SND_SOC_DAIFMT_IB_NF |
  228. SND_SOC_DAIFMT_CBM_CFM,
  229. },
  230. };
  231. /* Audio machine driver */
  232. static struct snd_soc_card omap_twl4030_card = {
  233. .owner = THIS_MODULE,
  234. .dai_link = omap_twl4030_dai_links,
  235. .num_links = ARRAY_SIZE(omap_twl4030_dai_links),
  236. .dapm_widgets = dapm_widgets,
  237. .num_dapm_widgets = ARRAY_SIZE(dapm_widgets),
  238. .dapm_routes = audio_map,
  239. .num_dapm_routes = ARRAY_SIZE(audio_map),
  240. };
  241. static int omap_twl4030_probe(struct platform_device *pdev)
  242. {
  243. struct omap_tw4030_pdata *pdata = dev_get_platdata(&pdev->dev);
  244. struct device_node *node = pdev->dev.of_node;
  245. struct snd_soc_card *card = &omap_twl4030_card;
  246. struct omap_twl4030 *priv;
  247. int ret = 0;
  248. card->dev = &pdev->dev;
  249. priv = devm_kzalloc(&pdev->dev, sizeof(struct omap_twl4030), GFP_KERNEL);
  250. if (priv == NULL)
  251. return -ENOMEM;
  252. if (node) {
  253. struct device_node *dai_node;
  254. struct property *prop;
  255. if (snd_soc_of_parse_card_name(card, "ti,model")) {
  256. dev_err(&pdev->dev, "Card name is not provided\n");
  257. return -ENODEV;
  258. }
  259. dai_node = of_parse_phandle(node, "ti,mcbsp", 0);
  260. if (!dai_node) {
  261. dev_err(&pdev->dev, "McBSP node is not provided\n");
  262. return -EINVAL;
  263. }
  264. omap_twl4030_dai_links[0].cpu_dai_name = NULL;
  265. omap_twl4030_dai_links[0].cpu_of_node = dai_node;
  266. dai_node = of_parse_phandle(node, "ti,mcbsp-voice", 0);
  267. if (!dai_node) {
  268. card->num_links = 1;
  269. } else {
  270. omap_twl4030_dai_links[1].cpu_dai_name = NULL;
  271. omap_twl4030_dai_links[1].cpu_of_node = dai_node;
  272. }
  273. priv->jack_detect = of_get_named_gpio(node,
  274. "ti,jack-det-gpio", 0);
  275. /* Optional: audio routing can be provided */
  276. prop = of_find_property(node, "ti,audio-routing", NULL);
  277. if (prop) {
  278. ret = snd_soc_of_parse_audio_routing(card,
  279. "ti,audio-routing");
  280. if (ret)
  281. return ret;
  282. card->fully_routed = 1;
  283. }
  284. } else if (pdata) {
  285. if (pdata->card_name) {
  286. card->name = pdata->card_name;
  287. } else {
  288. dev_err(&pdev->dev, "Card name is not provided\n");
  289. return -ENODEV;
  290. }
  291. if (!pdata->voice_connected)
  292. card->num_links = 1;
  293. priv->jack_detect = pdata->jack_detect;
  294. } else {
  295. dev_err(&pdev->dev, "Missing pdata\n");
  296. return -ENODEV;
  297. }
  298. snd_soc_card_set_drvdata(card, priv);
  299. ret = snd_soc_register_card(card);
  300. if (ret) {
  301. dev_err(&pdev->dev, "snd_soc_register_card() failed: %d\n",
  302. ret);
  303. return ret;
  304. }
  305. return 0;
  306. }
  307. static int omap_twl4030_remove(struct platform_device *pdev)
  308. {
  309. struct snd_soc_card *card = platform_get_drvdata(pdev);
  310. struct omap_twl4030 *priv = snd_soc_card_get_drvdata(card);
  311. if (priv->jack_detect > 0)
  312. snd_soc_jack_free_gpios(&priv->hs_jack,
  313. ARRAY_SIZE(hs_jack_gpios),
  314. hs_jack_gpios);
  315. snd_soc_unregister_card(card);
  316. return 0;
  317. }
  318. static const struct of_device_id omap_twl4030_of_match[] = {
  319. {.compatible = "ti,omap-twl4030", },
  320. { },
  321. };
  322. MODULE_DEVICE_TABLE(of, omap_twl4030_of_match);
  323. static struct platform_driver omap_twl4030_driver = {
  324. .driver = {
  325. .name = "omap-twl4030",
  326. .owner = THIS_MODULE,
  327. .pm = &snd_soc_pm_ops,
  328. .of_match_table = omap_twl4030_of_match,
  329. },
  330. .probe = omap_twl4030_probe,
  331. .remove = omap_twl4030_remove,
  332. };
  333. module_platform_driver(omap_twl4030_driver);
  334. MODULE_AUTHOR("Peter Ujfalusi <peter.ujfalusi@ti.com>");
  335. MODULE_DESCRIPTION("ALSA SoC for TI SoC based boards with twl4030 codec");
  336. MODULE_LICENSE("GPL");
  337. MODULE_ALIAS("platform:omap-twl4030");