imx-audmix.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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 consumer, and for record is provider */
  74. fmt |= tx ? SND_SOC_DAIFMT_BP_FP : SND_SOC_DAIFMT_BC_FC;
  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 consumer */
  108. fmt |= SND_SOC_DAIFMT_BC_FC;
  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 const 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 const 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. return -ENOMEM;
  182. ret = of_parse_phandle_with_args(audmix_np, "dais", NULL, i,
  183. &args);
  184. if (ret < 0) {
  185. dev_err(&pdev->dev, "of_parse_phandle_with_args failed\n");
  186. return ret;
  187. }
  188. cpu_pdev = of_find_device_by_node(args.np);
  189. if (!cpu_pdev) {
  190. dev_err(&pdev->dev, "failed to find SAI platform device\n");
  191. return -EINVAL;
  192. }
  193. put_device(&cpu_pdev->dev);
  194. dai_name = devm_kasprintf(&pdev->dev, GFP_KERNEL, "%s%s",
  195. fe_name_pref, args.np->full_name + 1);
  196. if (!dai_name)
  197. return -ENOMEM;
  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. if (!capture_dai_name)
  205. return -ENOMEM;
  206. }
  207. priv->dai[i].cpus = &dlc[0];
  208. priv->dai[i].codecs = &dlc[1];
  209. priv->dai[i].platforms = &dlc[2];
  210. priv->dai[i].num_cpus = 1;
  211. priv->dai[i].num_codecs = 1;
  212. priv->dai[i].num_platforms = 1;
  213. priv->dai[i].name = dai_name;
  214. priv->dai[i].stream_name = "HiFi-AUDMIX-FE";
  215. priv->dai[i].codecs->dai_name = "snd-soc-dummy-dai";
  216. priv->dai[i].codecs->name = "snd-soc-dummy";
  217. priv->dai[i].cpus->of_node = args.np;
  218. priv->dai[i].cpus->dai_name = dev_name(&cpu_pdev->dev);
  219. priv->dai[i].platforms->of_node = args.np;
  220. priv->dai[i].dynamic = 1;
  221. priv->dai[i].dpcm_playback = 1;
  222. priv->dai[i].dpcm_capture = (i == 0 ? 1 : 0);
  223. priv->dai[i].ignore_pmdown_time = 1;
  224. priv->dai[i].ops = &imx_audmix_fe_ops;
  225. /* Add AUDMIX Backend */
  226. be_name = devm_kasprintf(&pdev->dev, GFP_KERNEL,
  227. "audmix-%d", i);
  228. be_pb = devm_kasprintf(&pdev->dev, GFP_KERNEL,
  229. "AUDMIX-Playback-%d", i);
  230. be_cp = devm_kasprintf(&pdev->dev, GFP_KERNEL,
  231. "AUDMIX-Capture-%d", i);
  232. if (!be_name || !be_pb || !be_cp)
  233. return -ENOMEM;
  234. priv->dai[num_dai + i].cpus = &dlc[3];
  235. priv->dai[num_dai + i].codecs = &dlc[4];
  236. priv->dai[num_dai + i].platforms = &dlc[5];
  237. priv->dai[num_dai + i].num_cpus = 1;
  238. priv->dai[num_dai + i].num_codecs = 1;
  239. priv->dai[num_dai + i].num_platforms = 1;
  240. priv->dai[num_dai + i].name = be_name;
  241. priv->dai[num_dai + i].codecs->dai_name = "snd-soc-dummy-dai";
  242. priv->dai[num_dai + i].codecs->name = "snd-soc-dummy";
  243. priv->dai[num_dai + i].cpus->of_node = audmix_np;
  244. priv->dai[num_dai + i].cpus->dai_name = be_name;
  245. priv->dai[num_dai + i].platforms->name = "snd-soc-dummy";
  246. priv->dai[num_dai + i].no_pcm = 1;
  247. priv->dai[num_dai + i].dpcm_playback = 1;
  248. priv->dai[num_dai + i].dpcm_capture = 1;
  249. priv->dai[num_dai + i].ignore_pmdown_time = 1;
  250. priv->dai[num_dai + i].ops = &imx_audmix_be_ops;
  251. priv->dai_conf[i].dlc.of_node = args.np;
  252. priv->dai_conf[i].name_prefix = dai_name;
  253. priv->dapm_routes[i].source =
  254. devm_kasprintf(&pdev->dev, GFP_KERNEL, "%s %s",
  255. dai_name, "CPU-Playback");
  256. if (!priv->dapm_routes[i].source)
  257. return -ENOMEM;
  258. priv->dapm_routes[i].sink = be_pb;
  259. priv->dapm_routes[num_dai + i].source = be_pb;
  260. priv->dapm_routes[num_dai + i].sink = be_cp;
  261. priv->dapm_routes[2 * num_dai + i].source = be_cp;
  262. priv->dapm_routes[2 * num_dai + i].sink = capture_dai_name;
  263. }
  264. cpu_pdev = of_find_device_by_node(out_cpu_np);
  265. if (!cpu_pdev) {
  266. dev_err(&pdev->dev, "failed to find SAI platform device\n");
  267. return -EINVAL;
  268. }
  269. put_device(&cpu_pdev->dev);
  270. priv->cpu_mclk = devm_clk_get(&cpu_pdev->dev, "mclk1");
  271. if (IS_ERR(priv->cpu_mclk)) {
  272. ret = PTR_ERR(priv->cpu_mclk);
  273. dev_err(&cpu_pdev->dev, "failed to get DAI mclk1: %d\n", ret);
  274. return ret;
  275. }
  276. priv->audmix_pdev = audmix_pdev;
  277. priv->out_pdev = cpu_pdev;
  278. priv->card.dai_link = priv->dai;
  279. priv->card.num_links = priv->num_dai;
  280. priv->card.codec_conf = priv->dai_conf;
  281. priv->card.num_configs = priv->num_dai_conf;
  282. priv->card.dapm_routes = priv->dapm_routes;
  283. priv->card.num_dapm_routes = priv->num_dapm_routes;
  284. priv->card.dev = &pdev->dev;
  285. priv->card.owner = THIS_MODULE;
  286. priv->card.name = "imx-audmix";
  287. platform_set_drvdata(pdev, &priv->card);
  288. snd_soc_card_set_drvdata(&priv->card, priv);
  289. ret = devm_snd_soc_register_card(&pdev->dev, &priv->card);
  290. if (ret) {
  291. dev_err(&pdev->dev, "snd_soc_register_card failed\n");
  292. return ret;
  293. }
  294. return ret;
  295. }
  296. static struct platform_driver imx_audmix_driver = {
  297. .probe = imx_audmix_probe,
  298. .driver = {
  299. .name = "imx-audmix",
  300. .pm = &snd_soc_pm_ops,
  301. },
  302. };
  303. module_platform_driver(imx_audmix_driver);
  304. MODULE_DESCRIPTION("NXP AUDMIX ASoC machine driver");
  305. MODULE_AUTHOR("Viorel Suman <[email protected]>");
  306. MODULE_ALIAS("platform:imx-audmix");
  307. MODULE_LICENSE("GPL v2");