audio-graph-scu-card.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. // SPDX-License-Identifier: GPL-2.0
  2. //
  3. // ASoC audio graph SCU sound card support
  4. //
  5. // Copyright (C) 2017 Renesas Solutions Corp.
  6. // Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
  7. //
  8. // based on
  9. // ${LINUX}/sound/soc/generic/simple-scu-card.c
  10. // ${LINUX}/sound/soc/generic/audio-graph-card.c
  11. #include <linux/clk.h>
  12. #include <linux/device.h>
  13. #include <linux/gpio.h>
  14. #include <linux/module.h>
  15. #include <linux/of.h>
  16. #include <linux/of_device.h>
  17. #include <linux/of_gpio.h>
  18. #include <linux/of_graph.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/string.h>
  21. #include <sound/jack.h>
  22. #include <sound/simple_card_utils.h>
  23. struct graph_card_data {
  24. struct snd_soc_card snd_card;
  25. struct snd_soc_codec_conf codec_conf;
  26. struct asoc_simple_dai *dai_props;
  27. struct snd_soc_dai_link *dai_link;
  28. struct asoc_simple_card_data adata;
  29. };
  30. #define graph_priv_to_card(priv) (&(priv)->snd_card)
  31. #define graph_priv_to_props(priv, i) ((priv)->dai_props + (i))
  32. #define graph_priv_to_dev(priv) (graph_priv_to_card(priv)->dev)
  33. #define graph_priv_to_link(priv, i) (graph_priv_to_card(priv)->dai_link + (i))
  34. static int asoc_graph_card_startup(struct snd_pcm_substream *substream)
  35. {
  36. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  37. struct graph_card_data *priv = snd_soc_card_get_drvdata(rtd->card);
  38. struct asoc_simple_dai *dai_props = graph_priv_to_props(priv, rtd->num);
  39. return asoc_simple_card_clk_enable(dai_props);
  40. }
  41. static void asoc_graph_card_shutdown(struct snd_pcm_substream *substream)
  42. {
  43. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  44. struct graph_card_data *priv = snd_soc_card_get_drvdata(rtd->card);
  45. struct asoc_simple_dai *dai_props = graph_priv_to_props(priv, rtd->num);
  46. asoc_simple_card_clk_disable(dai_props);
  47. }
  48. static const struct snd_soc_ops asoc_graph_card_ops = {
  49. .startup = asoc_graph_card_startup,
  50. .shutdown = asoc_graph_card_shutdown,
  51. };
  52. static int asoc_graph_card_dai_init(struct snd_soc_pcm_runtime *rtd)
  53. {
  54. struct graph_card_data *priv = snd_soc_card_get_drvdata(rtd->card);
  55. struct snd_soc_dai *dai;
  56. struct snd_soc_dai_link *dai_link;
  57. struct asoc_simple_dai *dai_props;
  58. int num = rtd->num;
  59. dai_link = graph_priv_to_link(priv, num);
  60. dai_props = graph_priv_to_props(priv, num);
  61. dai = dai_link->dynamic ?
  62. rtd->cpu_dai :
  63. rtd->codec_dai;
  64. return asoc_simple_card_init_dai(dai, dai_props);
  65. }
  66. static int asoc_graph_card_be_hw_params_fixup(struct snd_soc_pcm_runtime *rtd,
  67. struct snd_pcm_hw_params *params)
  68. {
  69. struct graph_card_data *priv = snd_soc_card_get_drvdata(rtd->card);
  70. asoc_simple_card_convert_fixup(&priv->adata, params);
  71. return 0;
  72. }
  73. static int asoc_graph_card_dai_link_of(struct device_node *ep,
  74. struct graph_card_data *priv,
  75. unsigned int daifmt,
  76. int idx, int is_fe)
  77. {
  78. struct device *dev = graph_priv_to_dev(priv);
  79. struct snd_soc_dai_link *dai_link = graph_priv_to_link(priv, idx);
  80. struct asoc_simple_dai *dai_props = graph_priv_to_props(priv, idx);
  81. struct snd_soc_card *card = graph_priv_to_card(priv);
  82. int ret;
  83. if (is_fe) {
  84. /* BE is dummy */
  85. dai_link->codec_of_node = NULL;
  86. dai_link->codec_dai_name = "snd-soc-dummy-dai";
  87. dai_link->codec_name = "snd-soc-dummy";
  88. /* FE settings */
  89. dai_link->dynamic = 1;
  90. dai_link->dpcm_merged_format = 1;
  91. ret = asoc_simple_card_parse_graph_cpu(ep, dai_link);
  92. if (ret)
  93. return ret;
  94. ret = asoc_simple_card_parse_clk_cpu(dev, ep, dai_link, dai_props);
  95. if (ret < 0)
  96. return ret;
  97. ret = asoc_simple_card_set_dailink_name(dev, dai_link,
  98. "fe.%s",
  99. dai_link->cpu_dai_name);
  100. if (ret < 0)
  101. return ret;
  102. /* card->num_links includes Codec */
  103. asoc_simple_card_canonicalize_cpu(dai_link,
  104. of_graph_get_endpoint_count(dai_link->cpu_of_node) == 1);
  105. } else {
  106. /* FE is dummy */
  107. dai_link->cpu_of_node = NULL;
  108. dai_link->cpu_dai_name = "snd-soc-dummy-dai";
  109. dai_link->cpu_name = "snd-soc-dummy";
  110. /* BE settings */
  111. dai_link->no_pcm = 1;
  112. dai_link->be_hw_params_fixup = asoc_graph_card_be_hw_params_fixup;
  113. ret = asoc_simple_card_parse_graph_codec(ep, dai_link);
  114. if (ret < 0)
  115. return ret;
  116. ret = asoc_simple_card_parse_clk_codec(dev, ep, dai_link, dai_props);
  117. if (ret < 0)
  118. return ret;
  119. ret = asoc_simple_card_set_dailink_name(dev, dai_link,
  120. "be.%s",
  121. dai_link->codec_dai_name);
  122. if (ret < 0)
  123. return ret;
  124. snd_soc_of_parse_audio_prefix(card,
  125. &priv->codec_conf,
  126. dai_link->codec_of_node,
  127. "prefix");
  128. }
  129. ret = asoc_simple_card_of_parse_tdm(ep, dai_props);
  130. if (ret)
  131. return ret;
  132. ret = asoc_simple_card_canonicalize_dailink(dai_link);
  133. if (ret < 0)
  134. return ret;
  135. dai_link->dai_fmt = daifmt;
  136. dai_link->dpcm_playback = 1;
  137. dai_link->dpcm_capture = 1;
  138. dai_link->ops = &asoc_graph_card_ops;
  139. dai_link->init = asoc_graph_card_dai_init;
  140. return 0;
  141. }
  142. static int asoc_graph_card_parse_of(struct graph_card_data *priv)
  143. {
  144. struct of_phandle_iterator it;
  145. struct device *dev = graph_priv_to_dev(priv);
  146. struct snd_soc_card *card = graph_priv_to_card(priv);
  147. struct device_node *node = dev->of_node;
  148. struct device_node *cpu_port;
  149. struct device_node *cpu_ep;
  150. struct device_node *codec_ep;
  151. struct device_node *rcpu_ep;
  152. struct device_node *codec_port;
  153. struct device_node *codec_port_old;
  154. unsigned int daifmt = 0;
  155. int dai_idx, ret;
  156. int rc, codec;
  157. if (!node)
  158. return -EINVAL;
  159. /*
  160. * we need to consider "widgets", "mclk-fs" around here
  161. * see simple-card
  162. */
  163. ret = asoc_simple_card_of_parse_routing(card, NULL, 0);
  164. if (ret < 0)
  165. return ret;
  166. asoc_simple_card_parse_convert(dev, NULL, &priv->adata);
  167. /*
  168. * it supports multi CPU, single CODEC only here
  169. * see asoc_graph_get_dais_count
  170. */
  171. /* find 1st codec */
  172. of_for_each_phandle(&it, rc, node, "dais", NULL, 0) {
  173. cpu_port = it.node;
  174. cpu_ep = of_get_next_child(cpu_port, NULL);
  175. codec_ep = of_graph_get_remote_endpoint(cpu_ep);
  176. rcpu_ep = of_graph_get_remote_endpoint(codec_ep);
  177. of_node_put(cpu_ep);
  178. of_node_put(codec_ep);
  179. of_node_put(rcpu_ep);
  180. if (!codec_ep)
  181. continue;
  182. if (rcpu_ep != cpu_ep) {
  183. dev_err(dev, "remote-endpoint missmatch (%s/%s/%s)\n",
  184. cpu_ep->name, codec_ep->name, rcpu_ep->name);
  185. ret = -EINVAL;
  186. goto parse_of_err;
  187. }
  188. ret = asoc_simple_card_parse_daifmt(dev, cpu_ep, codec_ep,
  189. NULL, &daifmt);
  190. if (ret < 0) {
  191. of_node_put(cpu_port);
  192. goto parse_of_err;
  193. }
  194. }
  195. dai_idx = 0;
  196. codec_port_old = NULL;
  197. for (codec = 0; codec < 2; codec++) {
  198. /*
  199. * To listup valid sounds continuously,
  200. * detect all CPU-dummy first, and
  201. * detect all dummy-Codec second
  202. */
  203. of_for_each_phandle(&it, rc, node, "dais", NULL, 0) {
  204. cpu_port = it.node;
  205. cpu_ep = of_get_next_child(cpu_port, NULL);
  206. codec_ep = of_graph_get_remote_endpoint(cpu_ep);
  207. codec_port = of_graph_get_port_parent(codec_ep);
  208. of_node_put(cpu_ep);
  209. of_node_put(codec_ep);
  210. of_node_put(codec_port);
  211. if (codec) {
  212. if (!codec_port)
  213. continue;
  214. if (codec_port_old == codec_port)
  215. continue;
  216. codec_port_old = codec_port;
  217. /* Back-End (= Codec) */
  218. ret = asoc_graph_card_dai_link_of(codec_ep, priv, daifmt, dai_idx++, 0);
  219. if (ret < 0) {
  220. of_node_put(cpu_port);
  221. goto parse_of_err;
  222. }
  223. } else {
  224. /* Front-End (= CPU) */
  225. ret = asoc_graph_card_dai_link_of(cpu_ep, priv, daifmt, dai_idx++, 1);
  226. if (ret < 0) {
  227. of_node_put(cpu_port);
  228. goto parse_of_err;
  229. }
  230. }
  231. }
  232. }
  233. ret = asoc_simple_card_parse_card_name(card, NULL);
  234. if (ret)
  235. goto parse_of_err;
  236. ret = 0;
  237. parse_of_err:
  238. return ret;
  239. }
  240. static int asoc_graph_get_dais_count(struct device *dev)
  241. {
  242. struct of_phandle_iterator it;
  243. struct device_node *node = dev->of_node;
  244. struct device_node *cpu_port;
  245. struct device_node *cpu_ep;
  246. struct device_node *codec_ep;
  247. struct device_node *codec_port;
  248. struct device_node *codec_port_old;
  249. int count = 0;
  250. int rc;
  251. codec_port_old = NULL;
  252. of_for_each_phandle(&it, rc, node, "dais", NULL, 0) {
  253. cpu_port = it.node;
  254. cpu_ep = of_get_next_child(cpu_port, NULL);
  255. codec_ep = of_graph_get_remote_endpoint(cpu_ep);
  256. codec_port = of_graph_get_port_parent(codec_ep);
  257. of_node_put(cpu_ep);
  258. of_node_put(codec_ep);
  259. of_node_put(codec_port);
  260. if (cpu_ep)
  261. count++;
  262. if (!codec_port)
  263. continue;
  264. if (codec_port_old == codec_port)
  265. continue;
  266. count++;
  267. codec_port_old = codec_port;
  268. }
  269. return count;
  270. }
  271. static int asoc_graph_card_probe(struct platform_device *pdev)
  272. {
  273. struct graph_card_data *priv;
  274. struct snd_soc_dai_link *dai_link;
  275. struct asoc_simple_dai *dai_props;
  276. struct device *dev = &pdev->dev;
  277. struct snd_soc_card *card;
  278. int num, ret;
  279. /* Allocate the private data and the DAI link array */
  280. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  281. if (!priv)
  282. return -ENOMEM;
  283. num = asoc_graph_get_dais_count(dev);
  284. if (num == 0)
  285. return -EINVAL;
  286. dai_props = devm_kcalloc(dev, num, sizeof(*dai_props), GFP_KERNEL);
  287. dai_link = devm_kcalloc(dev, num, sizeof(*dai_link), GFP_KERNEL);
  288. if (!dai_props || !dai_link)
  289. return -ENOMEM;
  290. priv->dai_props = dai_props;
  291. priv->dai_link = dai_link;
  292. /* Init snd_soc_card */
  293. card = graph_priv_to_card(priv);
  294. card->owner = THIS_MODULE;
  295. card->dev = dev;
  296. card->dai_link = priv->dai_link;
  297. card->num_links = num;
  298. card->codec_conf = &priv->codec_conf;
  299. card->num_configs = 1;
  300. ret = asoc_graph_card_parse_of(priv);
  301. if (ret < 0) {
  302. if (ret != -EPROBE_DEFER)
  303. dev_err(dev, "parse error %d\n", ret);
  304. goto err;
  305. }
  306. snd_soc_card_set_drvdata(card, priv);
  307. ret = devm_snd_soc_register_card(dev, card);
  308. if (ret < 0)
  309. goto err;
  310. return 0;
  311. err:
  312. asoc_simple_card_clean_reference(card);
  313. return ret;
  314. }
  315. static int asoc_graph_card_remove(struct platform_device *pdev)
  316. {
  317. struct snd_soc_card *card = platform_get_drvdata(pdev);
  318. return asoc_simple_card_clean_reference(card);
  319. }
  320. static const struct of_device_id asoc_graph_of_match[] = {
  321. { .compatible = "audio-graph-scu-card", },
  322. {},
  323. };
  324. MODULE_DEVICE_TABLE(of, asoc_graph_of_match);
  325. static struct platform_driver asoc_graph_card = {
  326. .driver = {
  327. .name = "asoc-audio-graph-scu-card",
  328. .pm = &snd_soc_pm_ops,
  329. .of_match_table = asoc_graph_of_match,
  330. },
  331. .probe = asoc_graph_card_probe,
  332. .remove = asoc_graph_card_remove,
  333. };
  334. module_platform_driver(asoc_graph_card);
  335. MODULE_ALIAS("platform:asoc-audio-graph-scu-card");
  336. MODULE_LICENSE("GPL v2");
  337. MODULE_DESCRIPTION("ASoC Audio Graph SCU Sound Card");
  338. MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");