imx-audmix.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright 2017 NXP
  4. *
  5. * The code contained herein is licensed under the GNU General Public
  6. * License. You may obtain a copy of the GNU General Public License
  7. * Version 2 or later at the following locations:
  8. *
  9. * https://www.opensource.org/licenses/gpl-license.html
  10. * https://www.gnu.org/copyleft/gpl.html
  11. */
  12. #include <linux/module.h>
  13. #include <linux/of_platform.h>
  14. #include <linux/clk.h>
  15. #include <sound/soc.h>
  16. #include <sound/soc-dapm.h>
  17. #include <linux/pm_runtime.h>
  18. #include "fsl_sai.h"
  19. #include "fsl_audmix.h"
  20. struct imx_audmix {
  21. struct platform_device *pdev;
  22. struct snd_soc_card card;
  23. struct platform_device *audmix_pdev;
  24. struct platform_device *out_pdev;
  25. struct clk *cpu_mclk;
  26. int num_dai;
  27. struct snd_soc_dai_link *dai;
  28. int num_dai_conf;
  29. struct snd_soc_codec_conf *dai_conf;
  30. int num_dapm_routes;
  31. struct snd_soc_dapm_route *dapm_routes;
  32. };
  33. static const u32 imx_audmix_rates[] = {
  34. 8000, 12000, 16000, 24000, 32000, 48000, 64000, 96000,
  35. };
  36. static const struct snd_pcm_hw_constraint_list imx_audmix_rate_constraints = {
  37. .count = ARRAY_SIZE(imx_audmix_rates),
  38. .list = imx_audmix_rates,
  39. };
  40. static int imx_audmix_fe_startup(struct snd_pcm_substream *substream)
  41. {
  42. struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
  43. struct imx_audmix *priv = snd_soc_card_get_drvdata(rtd->card);
  44. struct snd_pcm_runtime *runtime = substream->runtime;
  45. struct device *dev = rtd->card->dev;
  46. unsigned long clk_rate = clk_get_rate(priv->cpu_mclk);
  47. int ret;
  48. if (clk_rate % 24576000 == 0) {
  49. ret = snd_pcm_hw_constraint_list(runtime, 0,
  50. SNDRV_PCM_HW_PARAM_RATE,
  51. &imx_audmix_rate_constraints);
  52. if (ret < 0)
  53. return ret;
  54. } else {
  55. dev_warn(dev, "mclk may be not supported %lu\n", clk_rate);
  56. }
  57. ret = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_CHANNELS,
  58. 1, 8);
  59. if (ret < 0)
  60. return ret;
  61. return snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT,
  62. FSL_AUDMIX_FORMATS);
  63. }
  64. static int imx_audmix_fe_hw_params(struct snd_pcm_substream *substream,
  65. struct snd_pcm_hw_params *params)
  66. {
  67. struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
  68. struct device *dev = rtd->card->dev;
  69. bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
  70. unsigned int fmt = SND_SOC_DAIFMT_DSP_A | SND_SOC_DAIFMT_NB_NF;
  71. u32 channels = params_channels(params);
  72. int ret, dir;
  73. /* For playback the AUDMIX is slave, and for record is master */
  74. fmt |= tx ? SND_SOC_DAIFMT_CBS_CFS : SND_SOC_DAIFMT_CBM_CFM;
  75. dir = tx ? SND_SOC_CLOCK_OUT : SND_SOC_CLOCK_IN;
  76. /* set DAI configuration */
  77. ret = snd_soc_dai_set_fmt(asoc_rtd_to_cpu(rtd, 0), fmt);
  78. if (ret) {
  79. dev_err(dev, "failed to set cpu dai fmt: %d\n", ret);
  80. return ret;
  81. }
  82. ret = snd_soc_dai_set_sysclk(asoc_rtd_to_cpu(rtd, 0), FSL_SAI_CLK_MAST1, 0, dir);
  83. if (ret) {
  84. dev_err(dev, "failed to set cpu sysclk: %d\n", ret);
  85. return ret;
  86. }
  87. /*
  88. * Per datasheet, AUDMIX expects 8 slots and 32 bits
  89. * for every slot in TDM mode.
  90. */
  91. ret = snd_soc_dai_set_tdm_slot(asoc_rtd_to_cpu(rtd, 0), BIT(channels) - 1,
  92. BIT(channels) - 1, 8, 32);
  93. if (ret)
  94. dev_err(dev, "failed to set cpu dai tdm slot: %d\n", ret);
  95. return ret;
  96. }
  97. static int imx_audmix_be_hw_params(struct snd_pcm_substream *substream,
  98. struct snd_pcm_hw_params *params)
  99. {
  100. struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
  101. struct device *dev = rtd->card->dev;
  102. bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
  103. unsigned int fmt = SND_SOC_DAIFMT_DSP_A | SND_SOC_DAIFMT_NB_NF;
  104. int ret;
  105. if (!tx)
  106. return 0;
  107. /* For playback the AUDMIX is slave */
  108. fmt |= SND_SOC_DAIFMT_CBM_CFM;
  109. /* set AUDMIX DAI configuration */
  110. ret = snd_soc_dai_set_fmt(asoc_rtd_to_cpu(rtd, 0), fmt);
  111. if (ret)
  112. dev_err(dev, "failed to set AUDMIX DAI fmt: %d\n", ret);
  113. return ret;
  114. }
  115. static struct snd_soc_ops imx_audmix_fe_ops = {
  116. .startup = imx_audmix_fe_startup,
  117. .hw_params = imx_audmix_fe_hw_params,
  118. };
  119. static struct snd_soc_ops imx_audmix_be_ops = {
  120. .hw_params = imx_audmix_be_hw_params,
  121. };
  122. static int imx_audmix_probe(struct platform_device *pdev)
  123. {
  124. struct device_node *np = pdev->dev.of_node;
  125. struct device_node *audmix_np = NULL, *out_cpu_np = NULL;
  126. struct platform_device *audmix_pdev = NULL;
  127. struct platform_device *cpu_pdev;
  128. struct of_phandle_args args;
  129. struct imx_audmix *priv;
  130. int i, num_dai, ret;
  131. const char *fe_name_pref = "HiFi-AUDMIX-FE-";
  132. char *be_name, *be_pb, *be_cp, *dai_name, *capture_dai_name;
  133. if (pdev->dev.parent) {
  134. audmix_np = pdev->dev.parent->of_node;
  135. } else {
  136. dev_err(&pdev->dev, "Missing parent device.\n");
  137. return -EINVAL;
  138. }
  139. if (!audmix_np) {
  140. dev_err(&pdev->dev, "Missing DT node for parent device.\n");
  141. return -EINVAL;
  142. }
  143. audmix_pdev = of_find_device_by_node(audmix_np);
  144. if (!audmix_pdev) {
  145. dev_err(&pdev->dev, "Missing AUDMIX platform device for %s\n",
  146. np->full_name);
  147. return -EINVAL;
  148. }
  149. put_device(&audmix_pdev->dev);
  150. num_dai = of_count_phandle_with_args(audmix_np, "dais", NULL);
  151. if (num_dai != FSL_AUDMIX_MAX_DAIS) {
  152. dev_err(&pdev->dev, "Need 2 dais to be provided for %s\n",
  153. audmix_np->full_name);
  154. return -EINVAL;
  155. }
  156. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  157. if (!priv)
  158. return -ENOMEM;
  159. priv->num_dai = 2 * num_dai;
  160. priv->dai = devm_kcalloc(&pdev->dev, priv->num_dai,
  161. sizeof(struct snd_soc_dai_link), GFP_KERNEL);
  162. if (!priv->dai)
  163. return -ENOMEM;
  164. priv->num_dai_conf = num_dai;
  165. priv->dai_conf = devm_kcalloc(&pdev->dev, priv->num_dai_conf,
  166. sizeof(struct snd_soc_codec_conf),
  167. GFP_KERNEL);
  168. if (!priv->dai_conf)
  169. return -ENOMEM;
  170. priv->num_dapm_routes = 3 * num_dai;
  171. priv->dapm_routes = devm_kcalloc(&pdev->dev, priv->num_dapm_routes,
  172. sizeof(struct snd_soc_dapm_route),
  173. GFP_KERNEL);
  174. if (!priv->dapm_routes)
  175. return -ENOMEM;
  176. for (i = 0; i < num_dai; i++) {
  177. struct snd_soc_dai_link_component *dlc;
  178. /* for CPU/Codec/Platform x 2 */
  179. dlc = devm_kcalloc(&pdev->dev, 6, sizeof(*dlc), GFP_KERNEL);
  180. if (!dlc) {
  181. dev_err(&pdev->dev, "failed to allocate dai_link\n");
  182. return -ENOMEM;
  183. }
  184. ret = of_parse_phandle_with_args(audmix_np, "dais", NULL, i,
  185. &args);
  186. if (ret < 0) {
  187. dev_err(&pdev->dev, "of_parse_phandle_with_args failed\n");
  188. return ret;
  189. }
  190. cpu_pdev = of_find_device_by_node(args.np);
  191. if (!cpu_pdev) {
  192. dev_err(&pdev->dev, "failed to find SAI platform device\n");
  193. return -EINVAL;
  194. }
  195. put_device(&cpu_pdev->dev);
  196. dai_name = devm_kasprintf(&pdev->dev, GFP_KERNEL, "%s%s",
  197. fe_name_pref, args.np->full_name + 1);
  198. dev_info(pdev->dev.parent, "DAI FE name:%s\n", dai_name);
  199. if (i == 0) {
  200. out_cpu_np = args.np;
  201. capture_dai_name =
  202. devm_kasprintf(&pdev->dev, GFP_KERNEL, "%s %s",
  203. dai_name, "CPU-Capture");
  204. }
  205. priv->dai[i].cpus = &dlc[0];
  206. priv->dai[i].codecs = &dlc[1];
  207. priv->dai[i].platforms = &dlc[2];
  208. priv->dai[i].num_cpus = 1;
  209. priv->dai[i].num_codecs = 1;
  210. priv->dai[i].num_platforms = 1;
  211. priv->dai[i].name = dai_name;
  212. priv->dai[i].stream_name = "HiFi-AUDMIX-FE";
  213. priv->dai[i].codecs->dai_name = "snd-soc-dummy-dai";
  214. priv->dai[i].codecs->name = "snd-soc-dummy";
  215. priv->dai[i].cpus->of_node = args.np;
  216. priv->dai[i].cpus->dai_name = dev_name(&cpu_pdev->dev);
  217. priv->dai[i].platforms->of_node = args.np;
  218. priv->dai[i].dynamic = 1;
  219. priv->dai[i].dpcm_playback = 1;
  220. priv->dai[i].dpcm_capture = (i == 0 ? 1 : 0);
  221. priv->dai[i].ignore_pmdown_time = 1;
  222. priv->dai[i].ops = &imx_audmix_fe_ops;
  223. /* Add AUDMIX Backend */
  224. be_name = devm_kasprintf(&pdev->dev, GFP_KERNEL,
  225. "audmix-%d", i);
  226. be_pb = devm_kasprintf(&pdev->dev, GFP_KERNEL,
  227. "AUDMIX-Playback-%d", i);
  228. be_cp = devm_kasprintf(&pdev->dev, GFP_KERNEL,
  229. "AUDMIX-Capture-%d", i);
  230. priv->dai[num_dai + i].cpus = &dlc[3];
  231. priv->dai[num_dai + i].codecs = &dlc[4];
  232. priv->dai[num_dai + i].platforms = &dlc[5];
  233. priv->dai[num_dai + i].num_cpus = 1;
  234. priv->dai[num_dai + i].num_codecs = 1;
  235. priv->dai[num_dai + i].num_platforms = 1;
  236. priv->dai[num_dai + i].name = be_name;
  237. priv->dai[num_dai + i].codecs->dai_name = "snd-soc-dummy-dai";
  238. priv->dai[num_dai + i].codecs->name = "snd-soc-dummy";
  239. priv->dai[num_dai + i].cpus->of_node = audmix_np;
  240. priv->dai[num_dai + i].cpus->dai_name = be_name;
  241. priv->dai[num_dai + i].platforms->name = "snd-soc-dummy";
  242. priv->dai[num_dai + i].no_pcm = 1;
  243. priv->dai[num_dai + i].dpcm_playback = 1;
  244. priv->dai[num_dai + i].dpcm_capture = 1;
  245. priv->dai[num_dai + i].ignore_pmdown_time = 1;
  246. priv->dai[num_dai + i].ops = &imx_audmix_be_ops;
  247. priv->dai_conf[i].dlc.of_node = args.np;
  248. priv->dai_conf[i].name_prefix = dai_name;
  249. priv->dapm_routes[i].source =
  250. devm_kasprintf(&pdev->dev, GFP_KERNEL, "%s %s",
  251. dai_name, "CPU-Playback");
  252. priv->dapm_routes[i].sink = be_pb;
  253. priv->dapm_routes[num_dai + i].source = be_pb;
  254. priv->dapm_routes[num_dai + i].sink = be_cp;
  255. priv->dapm_routes[2 * num_dai + i].source = be_cp;
  256. priv->dapm_routes[2 * num_dai + i].sink = capture_dai_name;
  257. }
  258. cpu_pdev = of_find_device_by_node(out_cpu_np);
  259. if (!cpu_pdev) {
  260. dev_err(&pdev->dev, "failed to find SAI platform device\n");
  261. return -EINVAL;
  262. }
  263. put_device(&cpu_pdev->dev);
  264. priv->cpu_mclk = devm_clk_get(&cpu_pdev->dev, "mclk1");
  265. if (IS_ERR(priv->cpu_mclk)) {
  266. ret = PTR_ERR(priv->cpu_mclk);
  267. dev_err(&cpu_pdev->dev, "failed to get DAI mclk1: %d\n", ret);
  268. return -EINVAL;
  269. }
  270. priv->audmix_pdev = audmix_pdev;
  271. priv->out_pdev = cpu_pdev;
  272. priv->card.dai_link = priv->dai;
  273. priv->card.num_links = priv->num_dai;
  274. priv->card.codec_conf = priv->dai_conf;
  275. priv->card.num_configs = priv->num_dai_conf;
  276. priv->card.dapm_routes = priv->dapm_routes;
  277. priv->card.num_dapm_routes = priv->num_dapm_routes;
  278. priv->card.dev = &pdev->dev;
  279. priv->card.owner = THIS_MODULE;
  280. priv->card.name = "imx-audmix";
  281. platform_set_drvdata(pdev, &priv->card);
  282. snd_soc_card_set_drvdata(&priv->card, priv);
  283. ret = devm_snd_soc_register_card(&pdev->dev, &priv->card);
  284. if (ret) {
  285. dev_err(&pdev->dev, "snd_soc_register_card failed\n");
  286. return ret;
  287. }
  288. return ret;
  289. }
  290. static struct platform_driver imx_audmix_driver = {
  291. .probe = imx_audmix_probe,
  292. .driver = {
  293. .name = "imx-audmix",
  294. .pm = &snd_soc_pm_ops,
  295. },
  296. };
  297. module_platform_driver(imx_audmix_driver);
  298. MODULE_DESCRIPTION("NXP AUDMIX ASoC machine driver");
  299. MODULE_AUTHOR("Viorel Suman <viorel.suman@nxp.com>");
  300. MODULE_ALIAS("platform:imx-audmix");
  301. MODULE_LICENSE("GPL v2");