davinci-evm.c 13 KB

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