omap-abe-twl6040.c 9.2 KB

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