omap-twl4030.c 10 KB

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