omap-abe-twl6040.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. /*
  2. * omap-abe-twl6040.c -- SoC audio for TI OMAP based boards with ABE and
  3. * twl6040 codec
  4. *
  5. * Author: Misael Lopez Cruz <misael.lopez@ti.com>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * version 2 as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  19. * 02110-1301 USA
  20. *
  21. */
  22. #include <linux/clk.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/mfd/twl6040.h>
  25. #include <linux/platform_data/omap-abe-twl6040.h>
  26. #include <linux/module.h>
  27. #include <linux/of.h>
  28. #include <sound/core.h>
  29. #include <sound/pcm.h>
  30. #include <sound/soc.h>
  31. #include <sound/jack.h>
  32. #include "omap-dmic.h"
  33. #include "omap-mcpdm.h"
  34. #include "omap-pcm.h"
  35. #include "../codecs/twl6040.h"
  36. struct abe_twl6040 {
  37. int jack_detection; /* board can detect jack events */
  38. int mclk_freq; /* MCLK frequency speed for twl6040 */
  39. struct platform_device *dmic_codec_dev;
  40. };
  41. static int omap_abe_hw_params(struct snd_pcm_substream *substream,
  42. struct snd_pcm_hw_params *params)
  43. {
  44. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  45. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  46. struct snd_soc_codec *codec = rtd->codec;
  47. struct snd_soc_card *card = codec->card;
  48. struct abe_twl6040 *priv = snd_soc_card_get_drvdata(card);
  49. int clk_id, freq;
  50. int ret;
  51. clk_id = twl6040_get_clk_id(rtd->codec);
  52. if (clk_id == TWL6040_SYSCLK_SEL_HPPLL)
  53. freq = priv->mclk_freq;
  54. else if (clk_id == TWL6040_SYSCLK_SEL_LPPLL)
  55. freq = 32768;
  56. else
  57. return -EINVAL;
  58. /* set the codec mclk */
  59. ret = snd_soc_dai_set_sysclk(codec_dai, clk_id, freq,
  60. SND_SOC_CLOCK_IN);
  61. if (ret) {
  62. printk(KERN_ERR "can't set codec system clock\n");
  63. return ret;
  64. }
  65. return ret;
  66. }
  67. static struct snd_soc_ops omap_abe_ops = {
  68. .hw_params = omap_abe_hw_params,
  69. };
  70. static int omap_abe_dmic_hw_params(struct snd_pcm_substream *substream,
  71. struct snd_pcm_hw_params *params)
  72. {
  73. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  74. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  75. int ret = 0;
  76. ret = snd_soc_dai_set_sysclk(cpu_dai, OMAP_DMIC_SYSCLK_PAD_CLKS,
  77. 19200000, SND_SOC_CLOCK_IN);
  78. if (ret < 0) {
  79. printk(KERN_ERR "can't set DMIC cpu system clock\n");
  80. return ret;
  81. }
  82. ret = snd_soc_dai_set_sysclk(cpu_dai, OMAP_DMIC_ABE_DMIC_CLK, 2400000,
  83. SND_SOC_CLOCK_OUT);
  84. if (ret < 0) {
  85. printk(KERN_ERR "can't set DMIC output clock\n");
  86. return ret;
  87. }
  88. return 0;
  89. }
  90. static struct snd_soc_ops omap_abe_dmic_ops = {
  91. .hw_params = omap_abe_dmic_hw_params,
  92. };
  93. /* Headset jack */
  94. static struct snd_soc_jack hs_jack;
  95. /*Headset jack detection DAPM pins */
  96. static struct snd_soc_jack_pin hs_jack_pins[] = {
  97. {
  98. .pin = "Headset Mic",
  99. .mask = SND_JACK_MICROPHONE,
  100. },
  101. {
  102. .pin = "Headset Stereophone",
  103. .mask = SND_JACK_HEADPHONE,
  104. },
  105. };
  106. /* SDP4430 machine DAPM */
  107. static const struct snd_soc_dapm_widget twl6040_dapm_widgets[] = {
  108. /* Outputs */
  109. SND_SOC_DAPM_HP("Headset Stereophone", NULL),
  110. SND_SOC_DAPM_SPK("Earphone Spk", NULL),
  111. SND_SOC_DAPM_SPK("Ext Spk", NULL),
  112. SND_SOC_DAPM_LINE("Line Out", NULL),
  113. SND_SOC_DAPM_SPK("Vibrator", NULL),
  114. /* Inputs */
  115. SND_SOC_DAPM_MIC("Headset Mic", NULL),
  116. SND_SOC_DAPM_MIC("Main Handset Mic", NULL),
  117. SND_SOC_DAPM_MIC("Sub Handset Mic", NULL),
  118. SND_SOC_DAPM_LINE("Line In", NULL),
  119. /* Digital microphones */
  120. SND_SOC_DAPM_MIC("Digital Mic", NULL),
  121. };
  122. static const struct snd_soc_dapm_route audio_map[] = {
  123. /* Routings for outputs */
  124. {"Headset Stereophone", NULL, "HSOL"},
  125. {"Headset Stereophone", NULL, "HSOR"},
  126. {"Earphone Spk", NULL, "EP"},
  127. {"Ext Spk", NULL, "HFL"},
  128. {"Ext Spk", NULL, "HFR"},
  129. {"Line Out", NULL, "AUXL"},
  130. {"Line Out", NULL, "AUXR"},
  131. {"Vibrator", NULL, "VIBRAL"},
  132. {"Vibrator", NULL, "VIBRAR"},
  133. /* Routings for inputs */
  134. {"HSMIC", NULL, "Headset Mic"},
  135. {"Headset Mic", NULL, "Headset Mic Bias"},
  136. {"MAINMIC", NULL, "Main Handset Mic"},
  137. {"Main Handset Mic", NULL, "Main Mic Bias"},
  138. {"SUBMIC", NULL, "Sub Handset Mic"},
  139. {"Sub Handset Mic", NULL, "Main Mic Bias"},
  140. {"AFML", NULL, "Line In"},
  141. {"AFMR", NULL, "Line In"},
  142. };
  143. static inline void twl6040_disconnect_pin(struct snd_soc_dapm_context *dapm,
  144. int connected, char *pin)
  145. {
  146. if (!connected)
  147. snd_soc_dapm_disable_pin(dapm, pin);
  148. }
  149. static int omap_abe_twl6040_init(struct snd_soc_pcm_runtime *rtd)
  150. {
  151. struct snd_soc_codec *codec = rtd->codec;
  152. struct snd_soc_card *card = codec->card;
  153. struct snd_soc_dapm_context *dapm = &codec->dapm;
  154. struct omap_abe_twl6040_data *pdata = dev_get_platdata(card->dev);
  155. struct abe_twl6040 *priv = snd_soc_card_get_drvdata(card);
  156. int hs_trim;
  157. int ret = 0;
  158. /*
  159. * Configure McPDM offset cancellation based on the HSOTRIM value from
  160. * twl6040.
  161. */
  162. hs_trim = twl6040_get_trim_value(codec, TWL6040_TRIM_HSOTRIM);
  163. omap_mcpdm_configure_dn_offsets(rtd, TWL6040_HSF_TRIM_LEFT(hs_trim),
  164. TWL6040_HSF_TRIM_RIGHT(hs_trim));
  165. /* Headset jack detection only if it is supported */
  166. if (priv->jack_detection) {
  167. ret = snd_soc_jack_new(codec, "Headset Jack",
  168. SND_JACK_HEADSET, &hs_jack);
  169. if (ret)
  170. return ret;
  171. ret = snd_soc_jack_add_pins(&hs_jack, ARRAY_SIZE(hs_jack_pins),
  172. hs_jack_pins);
  173. twl6040_hs_jack_detect(codec, &hs_jack, SND_JACK_HEADSET);
  174. }
  175. /*
  176. * NULL pdata means we booted with DT. In this case the routing is
  177. * provided and the card is fully routed, no need to mark pins.
  178. */
  179. if (!pdata)
  180. return ret;
  181. /* Disable not connected paths if not used */
  182. twl6040_disconnect_pin(dapm, pdata->has_hs, "Headset Stereophone");
  183. twl6040_disconnect_pin(dapm, pdata->has_hf, "Ext Spk");
  184. twl6040_disconnect_pin(dapm, pdata->has_ep, "Earphone Spk");
  185. twl6040_disconnect_pin(dapm, pdata->has_aux, "Line Out");
  186. twl6040_disconnect_pin(dapm, pdata->has_vibra, "Vibrator");
  187. twl6040_disconnect_pin(dapm, pdata->has_hsmic, "Headset Mic");
  188. twl6040_disconnect_pin(dapm, pdata->has_mainmic, "Main Handset Mic");
  189. twl6040_disconnect_pin(dapm, pdata->has_submic, "Sub Handset Mic");
  190. twl6040_disconnect_pin(dapm, pdata->has_afm, "Line In");
  191. return ret;
  192. }
  193. static const struct snd_soc_dapm_route dmic_audio_map[] = {
  194. {"DMic", NULL, "Digital Mic"},
  195. {"Digital Mic", NULL, "Digital Mic1 Bias"},
  196. };
  197. static int omap_abe_dmic_init(struct snd_soc_pcm_runtime *rtd)
  198. {
  199. struct snd_soc_codec *codec = rtd->codec;
  200. struct snd_soc_dapm_context *dapm = &codec->dapm;
  201. return snd_soc_dapm_add_routes(dapm, dmic_audio_map,
  202. ARRAY_SIZE(dmic_audio_map));
  203. }
  204. /* Digital audio interface glue - connects codec <--> CPU */
  205. static struct snd_soc_dai_link abe_twl6040_dai_links[] = {
  206. {
  207. .name = "TWL6040",
  208. .stream_name = "TWL6040",
  209. .cpu_dai_name = "omap-mcpdm",
  210. .codec_dai_name = "twl6040-legacy",
  211. .platform_name = "omap-pcm-audio",
  212. .codec_name = "twl6040-codec",
  213. .init = omap_abe_twl6040_init,
  214. .ops = &omap_abe_ops,
  215. },
  216. {
  217. .name = "DMIC",
  218. .stream_name = "DMIC Capture",
  219. .cpu_dai_name = "omap-dmic",
  220. .codec_dai_name = "dmic-hifi",
  221. .platform_name = "omap-pcm-audio",
  222. .codec_name = "dmic-codec",
  223. .init = omap_abe_dmic_init,
  224. .ops = &omap_abe_dmic_ops,
  225. },
  226. };
  227. /* Audio machine driver */
  228. static struct snd_soc_card omap_abe_card = {
  229. .owner = THIS_MODULE,
  230. .dapm_widgets = twl6040_dapm_widgets,
  231. .num_dapm_widgets = ARRAY_SIZE(twl6040_dapm_widgets),
  232. .dapm_routes = audio_map,
  233. .num_dapm_routes = ARRAY_SIZE(audio_map),
  234. };
  235. static int omap_abe_probe(struct platform_device *pdev)
  236. {
  237. struct omap_abe_twl6040_data *pdata = dev_get_platdata(&pdev->dev);
  238. struct device_node *node = pdev->dev.of_node;
  239. struct snd_soc_card *card = &omap_abe_card;
  240. struct abe_twl6040 *priv;
  241. int num_links = 0;
  242. int ret = 0;
  243. card->dev = &pdev->dev;
  244. priv = devm_kzalloc(&pdev->dev, sizeof(struct abe_twl6040), GFP_KERNEL);
  245. if (priv == NULL)
  246. return -ENOMEM;
  247. priv->dmic_codec_dev = ERR_PTR(-EINVAL);
  248. if (node) {
  249. struct device_node *dai_node;
  250. if (snd_soc_of_parse_card_name(card, "ti,model")) {
  251. dev_err(&pdev->dev, "Card name is not provided\n");
  252. return -ENODEV;
  253. }
  254. ret = snd_soc_of_parse_audio_routing(card,
  255. "ti,audio-routing");
  256. if (ret) {
  257. dev_err(&pdev->dev,
  258. "Error while parsing DAPM routing\n");
  259. return ret;
  260. }
  261. dai_node = of_parse_phandle(node, "ti,mcpdm", 0);
  262. if (!dai_node) {
  263. dev_err(&pdev->dev, "McPDM node is not provided\n");
  264. return -EINVAL;
  265. }
  266. abe_twl6040_dai_links[0].cpu_dai_name = NULL;
  267. abe_twl6040_dai_links[0].cpu_of_node = dai_node;
  268. dai_node = of_parse_phandle(node, "ti,dmic", 0);
  269. if (dai_node) {
  270. num_links = 2;
  271. abe_twl6040_dai_links[1].cpu_dai_name = NULL;
  272. abe_twl6040_dai_links[1].cpu_of_node = dai_node;
  273. priv->dmic_codec_dev = platform_device_register_simple(
  274. "dmic-codec", -1, NULL, 0);
  275. if (IS_ERR(priv->dmic_codec_dev)) {
  276. dev_err(&pdev->dev,
  277. "Can't instantiate dmic-codec\n");
  278. return PTR_ERR(priv->dmic_codec_dev);
  279. }
  280. } else {
  281. num_links = 1;
  282. }
  283. priv->jack_detection = of_property_read_bool(node,
  284. "ti,jack-detection");
  285. of_property_read_u32(node, "ti,mclk-freq",
  286. &priv->mclk_freq);
  287. if (!priv->mclk_freq) {
  288. dev_err(&pdev->dev, "MCLK frequency not provided\n");
  289. ret = -EINVAL;
  290. goto err_unregister;
  291. }
  292. omap_abe_card.fully_routed = 1;
  293. } else if (pdata) {
  294. if (pdata->card_name) {
  295. card->name = pdata->card_name;
  296. } else {
  297. dev_err(&pdev->dev, "Card name is not provided\n");
  298. return -ENODEV;
  299. }
  300. if (pdata->has_dmic)
  301. num_links = 2;
  302. else
  303. num_links = 1;
  304. priv->jack_detection = pdata->jack_detection;
  305. priv->mclk_freq = pdata->mclk_freq;
  306. } else {
  307. dev_err(&pdev->dev, "Missing pdata\n");
  308. return -ENODEV;
  309. }
  310. if (!priv->mclk_freq) {
  311. dev_err(&pdev->dev, "MCLK frequency missing\n");
  312. ret = -ENODEV;
  313. goto err_unregister;
  314. }
  315. card->dai_link = abe_twl6040_dai_links;
  316. card->num_links = num_links;
  317. snd_soc_card_set_drvdata(card, priv);
  318. ret = snd_soc_register_card(card);
  319. if (ret) {
  320. dev_err(&pdev->dev, "snd_soc_register_card() failed: %d\n",
  321. ret);
  322. goto err_unregister;
  323. }
  324. return 0;
  325. err_unregister:
  326. if (!IS_ERR(priv->dmic_codec_dev))
  327. platform_device_unregister(priv->dmic_codec_dev);
  328. return ret;
  329. }
  330. static int omap_abe_remove(struct platform_device *pdev)
  331. {
  332. struct snd_soc_card *card = platform_get_drvdata(pdev);
  333. struct abe_twl6040 *priv = snd_soc_card_get_drvdata(card);
  334. snd_soc_unregister_card(card);
  335. if (!IS_ERR(priv->dmic_codec_dev))
  336. platform_device_unregister(priv->dmic_codec_dev);
  337. return 0;
  338. }
  339. static const struct of_device_id omap_abe_of_match[] = {
  340. {.compatible = "ti,abe-twl6040", },
  341. { },
  342. };
  343. MODULE_DEVICE_TABLE(of, omap_abe_of_match);
  344. static struct platform_driver omap_abe_driver = {
  345. .driver = {
  346. .name = "omap-abe-twl6040",
  347. .owner = THIS_MODULE,
  348. .pm = &snd_soc_pm_ops,
  349. .of_match_table = omap_abe_of_match,
  350. },
  351. .probe = omap_abe_probe,
  352. .remove = omap_abe_remove,
  353. };
  354. module_platform_driver(omap_abe_driver);
  355. MODULE_AUTHOR("Misael Lopez Cruz <misael.lopez@ti.com>");
  356. MODULE_DESCRIPTION("ALSA SoC for OMAP boards with ABE and twl6040 codec");
  357. MODULE_LICENSE("GPL");
  358. MODULE_ALIAS("platform:omap-abe-twl6040");