davinci-evm.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. /*
  2. * ASoC driver for TI DAVINCI EVM platform
  3. *
  4. * Author: Vladimir Barinov, <vbarinov@embeddedalley.com>
  5. * Copyright: (C) 2007 MontaVista Software, Inc., <source@mvista.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/module.h>
  12. #include <linux/moduleparam.h>
  13. #include <linux/timer.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/platform_data/edma.h>
  17. #include <linux/i2c.h>
  18. #include <linux/of_platform.h>
  19. #include <linux/clk.h>
  20. #include <sound/core.h>
  21. #include <sound/pcm.h>
  22. #include <sound/soc.h>
  23. #include <asm/dma.h>
  24. #include <asm/mach-types.h>
  25. #include <linux/edma.h>
  26. #include "davinci-pcm.h"
  27. #include "davinci-i2s.h"
  28. struct snd_soc_card_drvdata_davinci {
  29. struct clk *mclk;
  30. unsigned sysclk;
  31. };
  32. static int evm_startup(struct snd_pcm_substream *substream)
  33. {
  34. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  35. struct snd_soc_card *soc_card = rtd->card;
  36. struct snd_soc_card_drvdata_davinci *drvdata =
  37. snd_soc_card_get_drvdata(soc_card);
  38. if (drvdata->mclk)
  39. return clk_prepare_enable(drvdata->mclk);
  40. return 0;
  41. }
  42. static void evm_shutdown(struct snd_pcm_substream *substream)
  43. {
  44. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  45. struct snd_soc_card *soc_card = rtd->card;
  46. struct snd_soc_card_drvdata_davinci *drvdata =
  47. snd_soc_card_get_drvdata(soc_card);
  48. if (drvdata->mclk)
  49. clk_disable_unprepare(drvdata->mclk);
  50. }
  51. static int evm_hw_params(struct snd_pcm_substream *substream,
  52. struct snd_pcm_hw_params *params)
  53. {
  54. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  55. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  56. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  57. struct snd_soc_card *soc_card = rtd->card;
  58. int ret = 0;
  59. unsigned sysclk = ((struct snd_soc_card_drvdata_davinci *)
  60. snd_soc_card_get_drvdata(soc_card))->sysclk;
  61. /* set the codec system clock */
  62. ret = snd_soc_dai_set_sysclk(codec_dai, 0, sysclk, SND_SOC_CLOCK_OUT);
  63. if (ret < 0)
  64. return ret;
  65. /* set the CPU system clock */
  66. ret = snd_soc_dai_set_sysclk(cpu_dai, 0, sysclk, SND_SOC_CLOCK_OUT);
  67. if (ret < 0)
  68. return ret;
  69. return 0;
  70. }
  71. static struct snd_soc_ops evm_ops = {
  72. .startup = evm_startup,
  73. .shutdown = evm_shutdown,
  74. .hw_params = evm_hw_params,
  75. };
  76. /* davinci-evm machine dapm widgets */
  77. static const struct snd_soc_dapm_widget aic3x_dapm_widgets[] = {
  78. SND_SOC_DAPM_HP("Headphone Jack", NULL),
  79. SND_SOC_DAPM_LINE("Line Out", NULL),
  80. SND_SOC_DAPM_MIC("Mic Jack", NULL),
  81. SND_SOC_DAPM_LINE("Line In", NULL),
  82. };
  83. /* davinci-evm machine audio_mapnections to the codec pins */
  84. static const struct snd_soc_dapm_route audio_map[] = {
  85. /* Headphone connected to HPLOUT, HPROUT */
  86. {"Headphone Jack", NULL, "HPLOUT"},
  87. {"Headphone Jack", NULL, "HPROUT"},
  88. /* Line Out connected to LLOUT, RLOUT */
  89. {"Line Out", NULL, "LLOUT"},
  90. {"Line Out", NULL, "RLOUT"},
  91. /* Mic connected to (MIC3L | MIC3R) */
  92. {"MIC3L", NULL, "Mic Bias"},
  93. {"MIC3R", NULL, "Mic Bias"},
  94. {"Mic Bias", NULL, "Mic Jack"},
  95. /* Line In connected to (LINE1L | LINE2L), (LINE1R | LINE2R) */
  96. {"LINE1L", NULL, "Line In"},
  97. {"LINE2L", NULL, "Line In"},
  98. {"LINE1R", NULL, "Line In"},
  99. {"LINE2R", NULL, "Line In"},
  100. };
  101. /* Logic for a aic3x as connected on a davinci-evm */
  102. static int evm_aic3x_init(struct snd_soc_pcm_runtime *rtd)
  103. {
  104. struct snd_soc_card *card = rtd->card;
  105. struct snd_soc_codec *codec = rtd->codec;
  106. struct device_node *np = card->dev->of_node;
  107. int ret;
  108. /* Add davinci-evm specific widgets */
  109. snd_soc_dapm_new_controls(&card->dapm, aic3x_dapm_widgets,
  110. ARRAY_SIZE(aic3x_dapm_widgets));
  111. if (np) {
  112. ret = snd_soc_of_parse_audio_routing(card, "ti,audio-routing");
  113. if (ret)
  114. return ret;
  115. } else {
  116. /* Set up davinci-evm specific audio path audio_map */
  117. snd_soc_dapm_add_routes(&card->dapm, audio_map,
  118. ARRAY_SIZE(audio_map));
  119. }
  120. /* not connected */
  121. snd_soc_dapm_nc_pin(&codec->dapm, "MONO_LOUT");
  122. snd_soc_dapm_nc_pin(&codec->dapm, "HPLCOM");
  123. snd_soc_dapm_nc_pin(&codec->dapm, "HPRCOM");
  124. return 0;
  125. }
  126. /* davinci-evm digital audio interface glue - connects codec <--> CPU */
  127. static struct snd_soc_dai_link dm6446_evm_dai = {
  128. .name = "TLV320AIC3X",
  129. .stream_name = "AIC3X",
  130. .cpu_dai_name = "davinci-mcbsp",
  131. .codec_dai_name = "tlv320aic3x-hifi",
  132. .codec_name = "tlv320aic3x-codec.1-001b",
  133. .platform_name = "davinci-mcbsp",
  134. .init = evm_aic3x_init,
  135. .ops = &evm_ops,
  136. .dai_fmt = SND_SOC_DAIFMT_DSP_B | SND_SOC_DAIFMT_CBM_CFM |
  137. SND_SOC_DAIFMT_IB_NF,
  138. };
  139. static struct snd_soc_dai_link dm355_evm_dai = {
  140. .name = "TLV320AIC3X",
  141. .stream_name = "AIC3X",
  142. .cpu_dai_name = "davinci-mcbsp.1",
  143. .codec_dai_name = "tlv320aic3x-hifi",
  144. .codec_name = "tlv320aic3x-codec.1-001b",
  145. .platform_name = "davinci-mcbsp.1",
  146. .init = evm_aic3x_init,
  147. .ops = &evm_ops,
  148. .dai_fmt = SND_SOC_DAIFMT_DSP_B | SND_SOC_DAIFMT_CBM_CFM |
  149. SND_SOC_DAIFMT_IB_NF,
  150. };
  151. static struct snd_soc_dai_link dm365_evm_dai = {
  152. #ifdef CONFIG_SND_DM365_AIC3X_CODEC
  153. .name = "TLV320AIC3X",
  154. .stream_name = "AIC3X",
  155. .cpu_dai_name = "davinci-mcbsp",
  156. .codec_dai_name = "tlv320aic3x-hifi",
  157. .codec_name = "tlv320aic3x-codec.1-0018",
  158. .platform_name = "davinci-mcbsp",
  159. .init = evm_aic3x_init,
  160. .ops = &evm_ops,
  161. .dai_fmt = SND_SOC_DAIFMT_DSP_B | SND_SOC_DAIFMT_CBM_CFM |
  162. SND_SOC_DAIFMT_IB_NF,
  163. #elif defined(CONFIG_SND_DM365_VOICE_CODEC)
  164. .name = "Voice Codec - CQ93VC",
  165. .stream_name = "CQ93",
  166. .cpu_dai_name = "davinci-vcif",
  167. .codec_dai_name = "cq93vc-hifi",
  168. .codec_name = "cq93vc-codec",
  169. .platform_name = "davinci-vcif",
  170. #endif
  171. };
  172. static struct snd_soc_dai_link dm6467_evm_dai[] = {
  173. {
  174. .name = "TLV320AIC3X",
  175. .stream_name = "AIC3X",
  176. .cpu_dai_name= "davinci-mcasp.0",
  177. .codec_dai_name = "tlv320aic3x-hifi",
  178. .platform_name = "davinci-mcasp.0",
  179. .codec_name = "tlv320aic3x-codec.0-001a",
  180. .init = evm_aic3x_init,
  181. .ops = &evm_ops,
  182. .dai_fmt = SND_SOC_DAIFMT_DSP_B | SND_SOC_DAIFMT_CBM_CFM |
  183. SND_SOC_DAIFMT_IB_NF,
  184. },
  185. {
  186. .name = "McASP",
  187. .stream_name = "spdif",
  188. .cpu_dai_name= "davinci-mcasp.1",
  189. .codec_dai_name = "dit-hifi",
  190. .codec_name = "spdif_dit",
  191. .platform_name = "davinci-mcasp.1",
  192. .dai_fmt = SND_SOC_DAIFMT_DSP_B | SND_SOC_DAIFMT_CBM_CFM |
  193. SND_SOC_DAIFMT_IB_NF,
  194. },
  195. };
  196. static struct snd_soc_dai_link da830_evm_dai = {
  197. .name = "TLV320AIC3X",
  198. .stream_name = "AIC3X",
  199. .cpu_dai_name = "davinci-mcasp.1",
  200. .codec_dai_name = "tlv320aic3x-hifi",
  201. .codec_name = "tlv320aic3x-codec.1-0018",
  202. .platform_name = "davinci-mcasp.1",
  203. .init = evm_aic3x_init,
  204. .ops = &evm_ops,
  205. .dai_fmt = SND_SOC_DAIFMT_DSP_B | SND_SOC_DAIFMT_CBM_CFM |
  206. SND_SOC_DAIFMT_IB_NF,
  207. };
  208. static struct snd_soc_dai_link da850_evm_dai = {
  209. .name = "TLV320AIC3X",
  210. .stream_name = "AIC3X",
  211. .cpu_dai_name= "davinci-mcasp.0",
  212. .codec_dai_name = "tlv320aic3x-hifi",
  213. .codec_name = "tlv320aic3x-codec.1-0018",
  214. .platform_name = "davinci-mcasp.0",
  215. .init = evm_aic3x_init,
  216. .ops = &evm_ops,
  217. .dai_fmt = SND_SOC_DAIFMT_DSP_B | SND_SOC_DAIFMT_CBM_CFM |
  218. SND_SOC_DAIFMT_IB_NF,
  219. };
  220. /* davinci dm6446 evm audio machine driver */
  221. /*
  222. * ASP0 in DM6446 EVM is clocked by U55, as configured by
  223. * board-dm644x-evm.c using GPIOs from U18. There are six
  224. * options; here we "know" we use a 48 KHz sample rate.
  225. */
  226. static struct snd_soc_card_drvdata_davinci dm6446_snd_soc_card_drvdata = {
  227. .sysclk = 12288000,
  228. };
  229. static struct snd_soc_card dm6446_snd_soc_card_evm = {
  230. .name = "DaVinci DM6446 EVM",
  231. .owner = THIS_MODULE,
  232. .dai_link = &dm6446_evm_dai,
  233. .num_links = 1,
  234. .drvdata = &dm6446_snd_soc_card_drvdata,
  235. };
  236. /* davinci dm355 evm audio machine driver */
  237. /* ASP1 on DM355 EVM is clocked by an external oscillator */
  238. static struct snd_soc_card_drvdata_davinci dm355_snd_soc_card_drvdata = {
  239. .sysclk = 27000000,
  240. };
  241. static struct snd_soc_card dm355_snd_soc_card_evm = {
  242. .name = "DaVinci DM355 EVM",
  243. .owner = THIS_MODULE,
  244. .dai_link = &dm355_evm_dai,
  245. .num_links = 1,
  246. .drvdata = &dm355_snd_soc_card_drvdata,
  247. };
  248. /* davinci dm365 evm audio machine driver */
  249. static struct snd_soc_card_drvdata_davinci dm365_snd_soc_card_drvdata = {
  250. .sysclk = 27000000,
  251. };
  252. static struct snd_soc_card dm365_snd_soc_card_evm = {
  253. .name = "DaVinci DM365 EVM",
  254. .owner = THIS_MODULE,
  255. .dai_link = &dm365_evm_dai,
  256. .num_links = 1,
  257. .drvdata = &dm365_snd_soc_card_drvdata,
  258. };
  259. /* davinci dm6467 evm audio machine driver */
  260. static struct snd_soc_card_drvdata_davinci dm6467_snd_soc_card_drvdata = {
  261. .sysclk = 27000000,
  262. };
  263. static struct snd_soc_card dm6467_snd_soc_card_evm = {
  264. .name = "DaVinci DM6467 EVM",
  265. .owner = THIS_MODULE,
  266. .dai_link = dm6467_evm_dai,
  267. .num_links = ARRAY_SIZE(dm6467_evm_dai),
  268. .drvdata = &dm6467_snd_soc_card_drvdata,
  269. };
  270. static struct snd_soc_card_drvdata_davinci da830_snd_soc_card_drvdata = {
  271. .sysclk = 24576000,
  272. };
  273. static struct snd_soc_card da830_snd_soc_card = {
  274. .name = "DA830/OMAP-L137 EVM",
  275. .owner = THIS_MODULE,
  276. .dai_link = &da830_evm_dai,
  277. .num_links = 1,
  278. .drvdata = &da830_snd_soc_card_drvdata,
  279. };
  280. static struct snd_soc_card_drvdata_davinci da850_snd_soc_card_drvdata = {
  281. .sysclk = 24576000,
  282. };
  283. static struct snd_soc_card da850_snd_soc_card = {
  284. .name = "DA850/OMAP-L138 EVM",
  285. .owner = THIS_MODULE,
  286. .dai_link = &da850_evm_dai,
  287. .num_links = 1,
  288. .drvdata = &da850_snd_soc_card_drvdata,
  289. };
  290. #if defined(CONFIG_OF)
  291. /*
  292. * The struct is used as place holder. It will be completely
  293. * filled with data from dt node.
  294. */
  295. static struct snd_soc_dai_link evm_dai_tlv320aic3x = {
  296. .name = "TLV320AIC3X",
  297. .stream_name = "AIC3X",
  298. .codec_dai_name = "tlv320aic3x-hifi",
  299. .ops = &evm_ops,
  300. .init = evm_aic3x_init,
  301. .dai_fmt = SND_SOC_DAIFMT_DSP_B | SND_SOC_DAIFMT_CBM_CFM |
  302. SND_SOC_DAIFMT_IB_NF,
  303. };
  304. static const struct of_device_id davinci_evm_dt_ids[] = {
  305. {
  306. .compatible = "ti,da830-evm-audio",
  307. .data = (void *) &evm_dai_tlv320aic3x,
  308. },
  309. { /* sentinel */ }
  310. };
  311. MODULE_DEVICE_TABLE(of, davinci_evm_dt_ids);
  312. /* davinci evm audio machine driver */
  313. static struct snd_soc_card evm_soc_card = {
  314. .owner = THIS_MODULE,
  315. .num_links = 1,
  316. };
  317. static int davinci_evm_probe(struct platform_device *pdev)
  318. {
  319. struct device_node *np = pdev->dev.of_node;
  320. const struct of_device_id *match =
  321. of_match_device(of_match_ptr(davinci_evm_dt_ids), &pdev->dev);
  322. struct snd_soc_dai_link *dai = (struct snd_soc_dai_link *) match->data;
  323. struct snd_soc_card_drvdata_davinci *drvdata = NULL;
  324. struct clk *mclk;
  325. int ret = 0;
  326. evm_soc_card.dai_link = dai;
  327. dai->codec_of_node = of_parse_phandle(np, "ti,audio-codec", 0);
  328. if (!dai->codec_of_node)
  329. return -EINVAL;
  330. dai->cpu_of_node = of_parse_phandle(np, "ti,mcasp-controller", 0);
  331. if (!dai->cpu_of_node)
  332. return -EINVAL;
  333. dai->platform_of_node = dai->cpu_of_node;
  334. evm_soc_card.dev = &pdev->dev;
  335. ret = snd_soc_of_parse_card_name(&evm_soc_card, "ti,model");
  336. if (ret)
  337. return ret;
  338. mclk = devm_clk_get(&pdev->dev, "mclk");
  339. if (PTR_ERR(mclk) == -EPROBE_DEFER) {
  340. return -EPROBE_DEFER;
  341. } else if (IS_ERR(mclk)) {
  342. dev_dbg(&pdev->dev, "mclk not found.\n");
  343. mclk = NULL;
  344. }
  345. drvdata = devm_kzalloc(&pdev->dev, sizeof(*drvdata), GFP_KERNEL);
  346. if (!drvdata)
  347. return -ENOMEM;
  348. drvdata->mclk = mclk;
  349. ret = of_property_read_u32(np, "ti,codec-clock-rate", &drvdata->sysclk);
  350. if (ret < 0) {
  351. if (!drvdata->mclk) {
  352. dev_err(&pdev->dev,
  353. "No clock or clock rate defined.\n");
  354. return -EINVAL;
  355. }
  356. drvdata->sysclk = clk_get_rate(drvdata->mclk);
  357. } else if (drvdata->mclk) {
  358. unsigned int requestd_rate = drvdata->sysclk;
  359. clk_set_rate(drvdata->mclk, drvdata->sysclk);
  360. drvdata->sysclk = clk_get_rate(drvdata->mclk);
  361. if (drvdata->sysclk != requestd_rate)
  362. dev_warn(&pdev->dev,
  363. "Could not get requested rate %u using %u.\n",
  364. requestd_rate, drvdata->sysclk);
  365. }
  366. snd_soc_card_set_drvdata(&evm_soc_card, drvdata);
  367. ret = devm_snd_soc_register_card(&pdev->dev, &evm_soc_card);
  368. if (ret)
  369. dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n", ret);
  370. return ret;
  371. }
  372. static int davinci_evm_remove(struct platform_device *pdev)
  373. {
  374. struct snd_soc_card *card = platform_get_drvdata(pdev);
  375. snd_soc_unregister_card(card);
  376. return 0;
  377. }
  378. static struct platform_driver davinci_evm_driver = {
  379. .probe = davinci_evm_probe,
  380. .remove = davinci_evm_remove,
  381. .driver = {
  382. .name = "davinci_evm",
  383. .owner = THIS_MODULE,
  384. .pm = &snd_soc_pm_ops,
  385. .of_match_table = of_match_ptr(davinci_evm_dt_ids),
  386. },
  387. };
  388. #endif
  389. static struct platform_device *evm_snd_device;
  390. static int __init evm_init(void)
  391. {
  392. struct snd_soc_card *evm_snd_dev_data;
  393. int index;
  394. int ret;
  395. /*
  396. * If dtb is there, the devices will be created dynamically.
  397. * Only register platfrom driver structure.
  398. */
  399. #if defined(CONFIG_OF)
  400. if (of_have_populated_dt())
  401. return platform_driver_register(&davinci_evm_driver);
  402. #endif
  403. if (machine_is_davinci_evm()) {
  404. evm_snd_dev_data = &dm6446_snd_soc_card_evm;
  405. index = 0;
  406. } else if (machine_is_davinci_dm355_evm()) {
  407. evm_snd_dev_data = &dm355_snd_soc_card_evm;
  408. index = 1;
  409. } else if (machine_is_davinci_dm365_evm()) {
  410. evm_snd_dev_data = &dm365_snd_soc_card_evm;
  411. index = 0;
  412. } else if (machine_is_davinci_dm6467_evm()) {
  413. evm_snd_dev_data = &dm6467_snd_soc_card_evm;
  414. index = 0;
  415. } else if (machine_is_davinci_da830_evm()) {
  416. evm_snd_dev_data = &da830_snd_soc_card;
  417. index = 1;
  418. } else if (machine_is_davinci_da850_evm()) {
  419. evm_snd_dev_data = &da850_snd_soc_card;
  420. index = 0;
  421. } else
  422. return -EINVAL;
  423. evm_snd_device = platform_device_alloc("soc-audio", index);
  424. if (!evm_snd_device)
  425. return -ENOMEM;
  426. platform_set_drvdata(evm_snd_device, evm_snd_dev_data);
  427. ret = platform_device_add(evm_snd_device);
  428. if (ret)
  429. platform_device_put(evm_snd_device);
  430. return ret;
  431. }
  432. static void __exit evm_exit(void)
  433. {
  434. #if defined(CONFIG_OF)
  435. if (of_have_populated_dt()) {
  436. platform_driver_unregister(&davinci_evm_driver);
  437. return;
  438. }
  439. #endif
  440. platform_device_unregister(evm_snd_device);
  441. }
  442. module_init(evm_init);
  443. module_exit(evm_exit);
  444. MODULE_AUTHOR("Vladimir Barinov");
  445. MODULE_DESCRIPTION("TI DAVINCI EVM ASoC driver");
  446. MODULE_LICENSE("GPL");