omap-abe-twl6040.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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 "../codecs/twl6040.h"
  35. struct abe_twl6040 {
  36. int jack_detection; /* board can detect jack events */
  37. int mclk_freq; /* MCLK frequency speed for twl6040 */
  38. struct platform_device *dmic_codec_dev;
  39. };
  40. static int omap_abe_hw_params(struct snd_pcm_substream *substream,
  41. struct snd_pcm_hw_params *params)
  42. {
  43. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  44. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  45. struct snd_soc_codec *codec = rtd->codec;
  46. struct snd_soc_card *card = codec->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(rtd->codec);
  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 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 inline void twl6040_disconnect_pin(struct snd_soc_dapm_context *dapm,
  143. int connected, char *pin)
  144. {
  145. if (!connected)
  146. snd_soc_dapm_disable_pin(dapm, pin);
  147. }
  148. static int omap_abe_twl6040_init(struct snd_soc_pcm_runtime *rtd)
  149. {
  150. struct snd_soc_codec *codec = rtd->codec;
  151. struct snd_soc_card *card = codec->card;
  152. struct snd_soc_dapm_context *dapm = &codec->dapm;
  153. struct omap_abe_twl6040_data *pdata = dev_get_platdata(card->dev);
  154. struct abe_twl6040 *priv = snd_soc_card_get_drvdata(card);
  155. int hs_trim;
  156. int ret = 0;
  157. /*
  158. * Configure McPDM offset cancellation based on the HSOTRIM value from
  159. * twl6040.
  160. */
  161. hs_trim = twl6040_get_trim_value(codec, TWL6040_TRIM_HSOTRIM);
  162. omap_mcpdm_configure_dn_offsets(rtd, TWL6040_HSF_TRIM_LEFT(hs_trim),
  163. TWL6040_HSF_TRIM_RIGHT(hs_trim));
  164. /* Headset jack detection only if it is supported */
  165. if (priv->jack_detection) {
  166. ret = snd_soc_jack_new(codec, "Headset Jack",
  167. SND_JACK_HEADSET, &hs_jack);
  168. if (ret)
  169. return ret;
  170. ret = snd_soc_jack_add_pins(&hs_jack, ARRAY_SIZE(hs_jack_pins),
  171. hs_jack_pins);
  172. twl6040_hs_jack_detect(codec, &hs_jack, SND_JACK_HEADSET);
  173. }
  174. /*
  175. * NULL pdata means we booted with DT. In this case the routing is
  176. * provided and the card is fully routed, no need to mark pins.
  177. */
  178. if (!pdata)
  179. return ret;
  180. /* Disable not connected paths if not used */
  181. twl6040_disconnect_pin(dapm, pdata->has_hs, "Headset Stereophone");
  182. twl6040_disconnect_pin(dapm, pdata->has_hf, "Ext Spk");
  183. twl6040_disconnect_pin(dapm, pdata->has_ep, "Earphone Spk");
  184. twl6040_disconnect_pin(dapm, pdata->has_aux, "Line Out");
  185. twl6040_disconnect_pin(dapm, pdata->has_vibra, "Vibrator");
  186. twl6040_disconnect_pin(dapm, pdata->has_hsmic, "Headset Mic");
  187. twl6040_disconnect_pin(dapm, pdata->has_mainmic, "Main Handset Mic");
  188. twl6040_disconnect_pin(dapm, pdata->has_submic, "Sub Handset Mic");
  189. twl6040_disconnect_pin(dapm, pdata->has_afm, "Line In");
  190. return ret;
  191. }
  192. static const struct snd_soc_dapm_route dmic_audio_map[] = {
  193. {"DMic", NULL, "Digital Mic"},
  194. {"Digital Mic", NULL, "Digital Mic1 Bias"},
  195. };
  196. static int omap_abe_dmic_init(struct snd_soc_pcm_runtime *rtd)
  197. {
  198. struct snd_soc_codec *codec = rtd->codec;
  199. struct snd_soc_dapm_context *dapm = &codec->dapm;
  200. return snd_soc_dapm_add_routes(dapm, dmic_audio_map,
  201. ARRAY_SIZE(dmic_audio_map));
  202. }
  203. /* Digital audio interface glue - connects codec <--> CPU */
  204. static struct snd_soc_dai_link abe_twl6040_dai_links[] = {
  205. {
  206. .name = "TWL6040",
  207. .stream_name = "TWL6040",
  208. .cpu_dai_name = "omap-mcpdm",
  209. .codec_dai_name = "twl6040-legacy",
  210. .platform_name = "omap-pcm-audio",
  211. .codec_name = "twl6040-codec",
  212. .init = omap_abe_twl6040_init,
  213. .ops = &omap_abe_ops,
  214. },
  215. {
  216. .name = "DMIC",
  217. .stream_name = "DMIC Capture",
  218. .cpu_dai_name = "omap-dmic",
  219. .codec_dai_name = "dmic-hifi",
  220. .platform_name = "omap-pcm-audio",
  221. .codec_name = "dmic-codec",
  222. .init = omap_abe_dmic_init,
  223. .ops = &omap_abe_dmic_ops,
  224. },
  225. };
  226. /* Audio machine driver */
  227. static struct snd_soc_card omap_abe_card = {
  228. .owner = THIS_MODULE,
  229. .dapm_widgets = twl6040_dapm_widgets,
  230. .num_dapm_widgets = ARRAY_SIZE(twl6040_dapm_widgets),
  231. .dapm_routes = audio_map,
  232. .num_dapm_routes = ARRAY_SIZE(audio_map),
  233. };
  234. static int omap_abe_probe(struct platform_device *pdev)
  235. {
  236. struct omap_abe_twl6040_data *pdata = dev_get_platdata(&pdev->dev);
  237. struct device_node *node = pdev->dev.of_node;
  238. struct snd_soc_card *card = &omap_abe_card;
  239. struct abe_twl6040 *priv;
  240. int num_links = 0;
  241. int ret = 0;
  242. card->dev = &pdev->dev;
  243. priv = devm_kzalloc(&pdev->dev, sizeof(struct abe_twl6040), GFP_KERNEL);
  244. if (priv == NULL)
  245. return -ENOMEM;
  246. priv->dmic_codec_dev = ERR_PTR(-EINVAL);
  247. if (node) {
  248. struct device_node *dai_node;
  249. if (snd_soc_of_parse_card_name(card, "ti,model")) {
  250. dev_err(&pdev->dev, "Card name is not provided\n");
  251. return -ENODEV;
  252. }
  253. ret = snd_soc_of_parse_audio_routing(card,
  254. "ti,audio-routing");
  255. if (ret) {
  256. dev_err(&pdev->dev,
  257. "Error while parsing DAPM routing\n");
  258. return ret;
  259. }
  260. dai_node = of_parse_phandle(node, "ti,mcpdm", 0);
  261. if (!dai_node) {
  262. dev_err(&pdev->dev, "McPDM node is not provided\n");
  263. return -EINVAL;
  264. }
  265. abe_twl6040_dai_links[0].cpu_dai_name = NULL;
  266. abe_twl6040_dai_links[0].cpu_of_node = dai_node;
  267. dai_node = of_parse_phandle(node, "ti,dmic", 0);
  268. if (dai_node) {
  269. num_links = 2;
  270. abe_twl6040_dai_links[1].cpu_dai_name = NULL;
  271. abe_twl6040_dai_links[1].cpu_of_node = dai_node;
  272. priv->dmic_codec_dev = platform_device_register_simple(
  273. "dmic-codec", -1, NULL, 0);
  274. if (IS_ERR(priv->dmic_codec_dev)) {
  275. dev_err(&pdev->dev,
  276. "Can't instantiate dmic-codec\n");
  277. return PTR_ERR(priv->dmic_codec_dev);
  278. }
  279. } else {
  280. num_links = 1;
  281. }
  282. priv->jack_detection = of_property_read_bool(node,
  283. "ti,jack-detection");
  284. of_property_read_u32(node, "ti,mclk-freq",
  285. &priv->mclk_freq);
  286. if (!priv->mclk_freq) {
  287. dev_err(&pdev->dev, "MCLK frequency not provided\n");
  288. ret = -EINVAL;
  289. goto err_unregister;
  290. }
  291. omap_abe_card.fully_routed = 1;
  292. } else if (pdata) {
  293. if (pdata->card_name) {
  294. card->name = pdata->card_name;
  295. } else {
  296. dev_err(&pdev->dev, "Card name is not provided\n");
  297. return -ENODEV;
  298. }
  299. if (pdata->has_dmic)
  300. num_links = 2;
  301. else
  302. num_links = 1;
  303. priv->jack_detection = pdata->jack_detection;
  304. priv->mclk_freq = pdata->mclk_freq;
  305. } else {
  306. dev_err(&pdev->dev, "Missing pdata\n");
  307. return -ENODEV;
  308. }
  309. if (!priv->mclk_freq) {
  310. dev_err(&pdev->dev, "MCLK frequency missing\n");
  311. ret = -ENODEV;
  312. goto err_unregister;
  313. }
  314. card->dai_link = abe_twl6040_dai_links;
  315. card->num_links = num_links;
  316. snd_soc_card_set_drvdata(card, priv);
  317. ret = snd_soc_register_card(card);
  318. if (ret) {
  319. dev_err(&pdev->dev, "snd_soc_register_card() failed: %d\n",
  320. ret);
  321. goto err_unregister;
  322. }
  323. return 0;
  324. err_unregister:
  325. if (!IS_ERR(priv->dmic_codec_dev))
  326. platform_device_unregister(priv->dmic_codec_dev);
  327. return ret;
  328. }
  329. static int omap_abe_remove(struct platform_device *pdev)
  330. {
  331. struct snd_soc_card *card = platform_get_drvdata(pdev);
  332. struct abe_twl6040 *priv = snd_soc_card_get_drvdata(card);
  333. snd_soc_unregister_card(card);
  334. if (!IS_ERR(priv->dmic_codec_dev))
  335. platform_device_unregister(priv->dmic_codec_dev);
  336. return 0;
  337. }
  338. static const struct of_device_id omap_abe_of_match[] = {
  339. {.compatible = "ti,abe-twl6040", },
  340. { },
  341. };
  342. MODULE_DEVICE_TABLE(of, omap_abe_of_match);
  343. static struct platform_driver omap_abe_driver = {
  344. .driver = {
  345. .name = "omap-abe-twl6040",
  346. .owner = THIS_MODULE,
  347. .pm = &snd_soc_pm_ops,
  348. .of_match_table = omap_abe_of_match,
  349. },
  350. .probe = omap_abe_probe,
  351. .remove = omap_abe_remove,
  352. };
  353. module_platform_driver(omap_abe_driver);
  354. MODULE_AUTHOR("Misael Lopez Cruz <misael.lopez@ti.com>");
  355. MODULE_DESCRIPTION("ALSA SoC for OMAP boards with ABE and twl6040 codec");
  356. MODULE_LICENSE("GPL");
  357. MODULE_ALIAS("platform:omap-abe-twl6040");