omap-twl4030.c 11 KB

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