omap-abe-twl6040.c 9.3 KB

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