omap-abe-twl6040.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * omap-abe-twl6040.c -- SoC audio for TI OMAP based boards with ABE and
  4. * twl6040 codec
  5. *
  6. * Author: Misael Lopez Cruz <misael.lopez@ti.com>
  7. */
  8. #include <linux/clk.h>
  9. #include <linux/platform_device.h>
  10. #include <linux/mfd/twl6040.h>
  11. #include <linux/module.h>
  12. #include <linux/of.h>
  13. #include <sound/core.h>
  14. #include <sound/pcm.h>
  15. #include <sound/soc.h>
  16. #include <sound/jack.h>
  17. #include "omap-dmic.h"
  18. #include "omap-mcpdm.h"
  19. #include "../codecs/twl6040.h"
  20. SND_SOC_DAILINK_DEFS(link0,
  21. DAILINK_COMP_ARRAY(COMP_EMPTY()),
  22. DAILINK_COMP_ARRAY(COMP_CODEC("twl6040-codec",
  23. "twl6040-legacy")),
  24. DAILINK_COMP_ARRAY(COMP_EMPTY()));
  25. SND_SOC_DAILINK_DEFS(link1,
  26. DAILINK_COMP_ARRAY(COMP_EMPTY()),
  27. DAILINK_COMP_ARRAY(COMP_CODEC("dmic-codec",
  28. "dmic-hifi")),
  29. DAILINK_COMP_ARRAY(COMP_EMPTY()));
  30. struct abe_twl6040 {
  31. struct snd_soc_card card;
  32. struct snd_soc_dai_link dai_links[2];
  33. int jack_detection; /* board can detect jack events */
  34. int mclk_freq; /* MCLK frequency speed for twl6040 */
  35. };
  36. static struct platform_device *dmic_codec_dev;
  37. static int omap_abe_hw_params(struct snd_pcm_substream *substream,
  38. struct snd_pcm_hw_params *params)
  39. {
  40. struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
  41. struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
  42. struct snd_soc_card *card = rtd->card;
  43. struct abe_twl6040 *priv = snd_soc_card_get_drvdata(card);
  44. int clk_id, freq;
  45. int ret;
  46. clk_id = twl6040_get_clk_id(codec_dai->component);
  47. if (clk_id == TWL6040_SYSCLK_SEL_HPPLL)
  48. freq = priv->mclk_freq;
  49. else if (clk_id == TWL6040_SYSCLK_SEL_LPPLL)
  50. freq = 32768;
  51. else
  52. return -EINVAL;
  53. /* set the codec mclk */
  54. ret = snd_soc_dai_set_sysclk(codec_dai, clk_id, freq,
  55. SND_SOC_CLOCK_IN);
  56. if (ret) {
  57. printk(KERN_ERR "can't set codec system clock\n");
  58. return ret;
  59. }
  60. return ret;
  61. }
  62. static const struct snd_soc_ops omap_abe_ops = {
  63. .hw_params = omap_abe_hw_params,
  64. };
  65. static int omap_abe_dmic_hw_params(struct snd_pcm_substream *substream,
  66. struct snd_pcm_hw_params *params)
  67. {
  68. struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
  69. struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
  70. int ret = 0;
  71. ret = snd_soc_dai_set_sysclk(cpu_dai, OMAP_DMIC_SYSCLK_PAD_CLKS,
  72. 19200000, SND_SOC_CLOCK_IN);
  73. if (ret < 0) {
  74. printk(KERN_ERR "can't set DMIC cpu system clock\n");
  75. return ret;
  76. }
  77. ret = snd_soc_dai_set_sysclk(cpu_dai, OMAP_DMIC_ABE_DMIC_CLK, 2400000,
  78. SND_SOC_CLOCK_OUT);
  79. if (ret < 0) {
  80. printk(KERN_ERR "can't set DMIC output clock\n");
  81. return ret;
  82. }
  83. return 0;
  84. }
  85. static struct snd_soc_ops omap_abe_dmic_ops = {
  86. .hw_params = omap_abe_dmic_hw_params,
  87. };
  88. /* Headset jack */
  89. static struct snd_soc_jack hs_jack;
  90. /*Headset jack detection DAPM pins */
  91. static struct snd_soc_jack_pin hs_jack_pins[] = {
  92. {
  93. .pin = "Headset Mic",
  94. .mask = SND_JACK_MICROPHONE,
  95. },
  96. {
  97. .pin = "Headset Stereophone",
  98. .mask = SND_JACK_HEADPHONE,
  99. },
  100. };
  101. /* SDP4430 machine DAPM */
  102. static const struct snd_soc_dapm_widget twl6040_dapm_widgets[] = {
  103. /* Outputs */
  104. SND_SOC_DAPM_HP("Headset Stereophone", NULL),
  105. SND_SOC_DAPM_SPK("Earphone Spk", NULL),
  106. SND_SOC_DAPM_SPK("Ext Spk", NULL),
  107. SND_SOC_DAPM_LINE("Line Out", NULL),
  108. SND_SOC_DAPM_SPK("Vibrator", NULL),
  109. /* Inputs */
  110. SND_SOC_DAPM_MIC("Headset Mic", NULL),
  111. SND_SOC_DAPM_MIC("Main Handset Mic", NULL),
  112. SND_SOC_DAPM_MIC("Sub Handset Mic", NULL),
  113. SND_SOC_DAPM_LINE("Line In", NULL),
  114. /* Digital microphones */
  115. SND_SOC_DAPM_MIC("Digital Mic", NULL),
  116. };
  117. static const struct snd_soc_dapm_route audio_map[] = {
  118. /* Routings for outputs */
  119. {"Headset Stereophone", NULL, "HSOL"},
  120. {"Headset Stereophone", NULL, "HSOR"},
  121. {"Earphone Spk", NULL, "EP"},
  122. {"Ext Spk", NULL, "HFL"},
  123. {"Ext Spk", NULL, "HFR"},
  124. {"Line Out", NULL, "AUXL"},
  125. {"Line Out", NULL, "AUXR"},
  126. {"Vibrator", NULL, "VIBRAL"},
  127. {"Vibrator", NULL, "VIBRAR"},
  128. /* Routings for inputs */
  129. {"HSMIC", NULL, "Headset Mic"},
  130. {"Headset Mic", NULL, "Headset Mic Bias"},
  131. {"MAINMIC", NULL, "Main Handset Mic"},
  132. {"Main Handset Mic", NULL, "Main Mic Bias"},
  133. {"SUBMIC", NULL, "Sub Handset Mic"},
  134. {"Sub Handset Mic", NULL, "Main Mic Bias"},
  135. {"AFML", NULL, "Line In"},
  136. {"AFMR", NULL, "Line In"},
  137. };
  138. static int omap_abe_twl6040_init(struct snd_soc_pcm_runtime *rtd)
  139. {
  140. struct snd_soc_component *component = asoc_rtd_to_codec(rtd, 0)->component;
  141. struct snd_soc_card *card = rtd->card;
  142. struct abe_twl6040 *priv = snd_soc_card_get_drvdata(card);
  143. int hs_trim;
  144. int ret = 0;
  145. /*
  146. * Configure McPDM offset cancellation based on the HSOTRIM value from
  147. * twl6040.
  148. */
  149. hs_trim = twl6040_get_trim_value(component, TWL6040_TRIM_HSOTRIM);
  150. omap_mcpdm_configure_dn_offsets(rtd, TWL6040_HSF_TRIM_LEFT(hs_trim),
  151. TWL6040_HSF_TRIM_RIGHT(hs_trim));
  152. /* Headset jack detection only if it is supported */
  153. if (priv->jack_detection) {
  154. ret = snd_soc_card_jack_new(rtd->card, "Headset Jack",
  155. SND_JACK_HEADSET, &hs_jack,
  156. hs_jack_pins,
  157. ARRAY_SIZE(hs_jack_pins));
  158. if (ret)
  159. return ret;
  160. twl6040_hs_jack_detect(component, &hs_jack, SND_JACK_HEADSET);
  161. }
  162. return 0;
  163. }
  164. static const struct snd_soc_dapm_route dmic_audio_map[] = {
  165. {"DMic", NULL, "Digital Mic"},
  166. {"Digital Mic", NULL, "Digital Mic1 Bias"},
  167. };
  168. static int omap_abe_dmic_init(struct snd_soc_pcm_runtime *rtd)
  169. {
  170. struct snd_soc_dapm_context *dapm = &rtd->card->dapm;
  171. return snd_soc_dapm_add_routes(dapm, dmic_audio_map,
  172. ARRAY_SIZE(dmic_audio_map));
  173. }
  174. static int omap_abe_probe(struct platform_device *pdev)
  175. {
  176. struct device_node *node = pdev->dev.of_node;
  177. struct snd_soc_card *card;
  178. struct device_node *dai_node;
  179. struct abe_twl6040 *priv;
  180. int num_links = 0;
  181. int ret = 0;
  182. if (!node) {
  183. dev_err(&pdev->dev, "of node is missing.\n");
  184. return -ENODEV;
  185. }
  186. priv = devm_kzalloc(&pdev->dev, sizeof(struct abe_twl6040), GFP_KERNEL);
  187. if (priv == NULL)
  188. return -ENOMEM;
  189. card = &priv->card;
  190. card->dev = &pdev->dev;
  191. card->owner = THIS_MODULE;
  192. card->dapm_widgets = twl6040_dapm_widgets;
  193. card->num_dapm_widgets = ARRAY_SIZE(twl6040_dapm_widgets);
  194. card->dapm_routes = audio_map;
  195. card->num_dapm_routes = ARRAY_SIZE(audio_map);
  196. if (snd_soc_of_parse_card_name(card, "ti,model")) {
  197. dev_err(&pdev->dev, "Card name is not provided\n");
  198. return -ENODEV;
  199. }
  200. ret = snd_soc_of_parse_audio_routing(card, "ti,audio-routing");
  201. if (ret) {
  202. dev_err(&pdev->dev, "Error while parsing DAPM routing\n");
  203. return ret;
  204. }
  205. dai_node = of_parse_phandle(node, "ti,mcpdm", 0);
  206. if (!dai_node) {
  207. dev_err(&pdev->dev, "McPDM node is not provided\n");
  208. return -EINVAL;
  209. }
  210. priv->dai_links[0].name = "DMIC";
  211. priv->dai_links[0].stream_name = "TWL6040";
  212. priv->dai_links[0].cpus = link0_cpus;
  213. priv->dai_links[0].num_cpus = 1;
  214. priv->dai_links[0].cpus->of_node = dai_node;
  215. priv->dai_links[0].platforms = link0_platforms;
  216. priv->dai_links[0].num_platforms = 1;
  217. priv->dai_links[0].platforms->of_node = dai_node;
  218. priv->dai_links[0].codecs = link0_codecs;
  219. priv->dai_links[0].num_codecs = 1;
  220. priv->dai_links[0].init = omap_abe_twl6040_init;
  221. priv->dai_links[0].ops = &omap_abe_ops;
  222. dai_node = of_parse_phandle(node, "ti,dmic", 0);
  223. if (dai_node) {
  224. num_links = 2;
  225. priv->dai_links[1].name = "TWL6040";
  226. priv->dai_links[1].stream_name = "DMIC Capture";
  227. priv->dai_links[1].cpus = link1_cpus;
  228. priv->dai_links[1].num_cpus = 1;
  229. priv->dai_links[1].cpus->of_node = dai_node;
  230. priv->dai_links[1].platforms = link1_platforms;
  231. priv->dai_links[1].num_platforms = 1;
  232. priv->dai_links[1].platforms->of_node = dai_node;
  233. priv->dai_links[1].codecs = link1_codecs;
  234. priv->dai_links[1].num_codecs = 1;
  235. priv->dai_links[1].init = omap_abe_dmic_init;
  236. priv->dai_links[1].ops = &omap_abe_dmic_ops;
  237. } else {
  238. num_links = 1;
  239. }
  240. priv->jack_detection = of_property_read_bool(node, "ti,jack-detection");
  241. of_property_read_u32(node, "ti,mclk-freq", &priv->mclk_freq);
  242. if (!priv->mclk_freq) {
  243. dev_err(&pdev->dev, "MCLK frequency not provided\n");
  244. return -EINVAL;
  245. }
  246. card->fully_routed = 1;
  247. if (!priv->mclk_freq) {
  248. dev_err(&pdev->dev, "MCLK frequency missing\n");
  249. return -ENODEV;
  250. }
  251. card->dai_link = priv->dai_links;
  252. card->num_links = num_links;
  253. snd_soc_card_set_drvdata(card, priv);
  254. ret = devm_snd_soc_register_card(&pdev->dev, card);
  255. if (ret)
  256. dev_err(&pdev->dev, "devm_snd_soc_register_card() failed: %d\n",
  257. ret);
  258. return ret;
  259. }
  260. static const struct of_device_id omap_abe_of_match[] = {
  261. {.compatible = "ti,abe-twl6040", },
  262. { },
  263. };
  264. MODULE_DEVICE_TABLE(of, omap_abe_of_match);
  265. static struct platform_driver omap_abe_driver = {
  266. .driver = {
  267. .name = "omap-abe-twl6040",
  268. .pm = &snd_soc_pm_ops,
  269. .of_match_table = omap_abe_of_match,
  270. },
  271. .probe = omap_abe_probe,
  272. };
  273. static int __init omap_abe_init(void)
  274. {
  275. int ret;
  276. dmic_codec_dev = platform_device_register_simple("dmic-codec", -1, NULL,
  277. 0);
  278. if (IS_ERR(dmic_codec_dev)) {
  279. pr_err("%s: dmic-codec device registration failed\n", __func__);
  280. return PTR_ERR(dmic_codec_dev);
  281. }
  282. ret = platform_driver_register(&omap_abe_driver);
  283. if (ret) {
  284. pr_err("%s: platform driver registration failed\n", __func__);
  285. platform_device_unregister(dmic_codec_dev);
  286. }
  287. return ret;
  288. }
  289. module_init(omap_abe_init);
  290. static void __exit omap_abe_exit(void)
  291. {
  292. platform_driver_unregister(&omap_abe_driver);
  293. platform_device_unregister(dmic_codec_dev);
  294. }
  295. module_exit(omap_abe_exit);
  296. MODULE_AUTHOR("Misael Lopez Cruz <misael.lopez@ti.com>");
  297. MODULE_DESCRIPTION("ALSA SoC for OMAP boards with ABE and twl6040 codec");
  298. MODULE_LICENSE("GPL");
  299. MODULE_ALIAS("platform:omap-abe-twl6040");