simple-scu-card.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. // SPDX-License-Identifier: GPL-2.0
  2. //
  3. // ASoC simple SCU sound card support
  4. //
  5. // Copyright (C) 2015 Renesas Solutions Corp.
  6. // Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
  7. //
  8. // based on ${LINUX}/sound/soc/generic/simple-card.c
  9. #include <linux/clk.h>
  10. #include <linux/device.h>
  11. #include <linux/module.h>
  12. #include <linux/of.h>
  13. #include <linux/of_device.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/string.h>
  16. #include <sound/jack.h>
  17. #include <sound/soc.h>
  18. #include <sound/soc-dai.h>
  19. #include <sound/simple_card_utils.h>
  20. struct simple_card_data {
  21. struct snd_soc_card snd_card;
  22. struct snd_soc_codec_conf codec_conf;
  23. struct asoc_simple_dai *dai_props;
  24. struct snd_soc_dai_link *dai_link;
  25. struct asoc_simple_card_data adata;
  26. };
  27. #define simple_priv_to_card(priv) (&(priv)->snd_card)
  28. #define simple_priv_to_props(priv, i) ((priv)->dai_props + (i))
  29. #define simple_priv_to_dev(priv) (simple_priv_to_card(priv)->dev)
  30. #define simple_priv_to_link(priv, i) (simple_priv_to_card(priv)->dai_link + (i))
  31. #define DAI "sound-dai"
  32. #define CELL "#sound-dai-cells"
  33. #define PREFIX "simple-audio-card,"
  34. static int asoc_simple_card_startup(struct snd_pcm_substream *substream)
  35. {
  36. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  37. struct simple_card_data *priv = snd_soc_card_get_drvdata(rtd->card);
  38. struct asoc_simple_dai *dai_props =
  39. simple_priv_to_props(priv, rtd->num);
  40. return asoc_simple_card_clk_enable(dai_props);
  41. }
  42. static void asoc_simple_card_shutdown(struct snd_pcm_substream *substream)
  43. {
  44. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  45. struct simple_card_data *priv = snd_soc_card_get_drvdata(rtd->card);
  46. struct asoc_simple_dai *dai_props =
  47. simple_priv_to_props(priv, rtd->num);
  48. asoc_simple_card_clk_disable(dai_props);
  49. }
  50. static const struct snd_soc_ops asoc_simple_card_ops = {
  51. .startup = asoc_simple_card_startup,
  52. .shutdown = asoc_simple_card_shutdown,
  53. };
  54. static int asoc_simple_card_dai_init(struct snd_soc_pcm_runtime *rtd)
  55. {
  56. struct simple_card_data *priv = snd_soc_card_get_drvdata(rtd->card);
  57. struct snd_soc_dai *dai;
  58. struct snd_soc_dai_link *dai_link;
  59. struct asoc_simple_dai *dai_props;
  60. int num = rtd->num;
  61. dai_link = simple_priv_to_link(priv, num);
  62. dai_props = simple_priv_to_props(priv, num);
  63. dai = dai_link->dynamic ?
  64. rtd->cpu_dai :
  65. rtd->codec_dai;
  66. return asoc_simple_card_init_dai(dai, dai_props);
  67. }
  68. static int asoc_simple_card_be_hw_params_fixup(struct snd_soc_pcm_runtime *rtd,
  69. struct snd_pcm_hw_params *params)
  70. {
  71. struct simple_card_data *priv = snd_soc_card_get_drvdata(rtd->card);
  72. asoc_simple_card_convert_fixup(&priv->adata, params);
  73. return 0;
  74. }
  75. static int asoc_simple_card_dai_link_of(struct device_node *np,
  76. struct simple_card_data *priv,
  77. unsigned int daifmt,
  78. int idx, bool is_fe)
  79. {
  80. struct device *dev = simple_priv_to_dev(priv);
  81. struct snd_soc_dai_link *dai_link = simple_priv_to_link(priv, idx);
  82. struct asoc_simple_dai *dai_props = simple_priv_to_props(priv, idx);
  83. struct snd_soc_card *card = simple_priv_to_card(priv);
  84. int ret;
  85. if (is_fe) {
  86. int is_single_links = 0;
  87. /* BE is dummy */
  88. dai_link->codec_of_node = NULL;
  89. dai_link->codec_dai_name = "snd-soc-dummy-dai";
  90. dai_link->codec_name = "snd-soc-dummy";
  91. /* FE settings */
  92. dai_link->dynamic = 1;
  93. dai_link->dpcm_merged_format = 1;
  94. ret = asoc_simple_card_parse_cpu(np, dai_link, DAI, CELL,
  95. &is_single_links);
  96. if (ret)
  97. return ret;
  98. ret = asoc_simple_card_parse_clk_cpu(dev, np, dai_link, dai_props);
  99. if (ret < 0)
  100. return ret;
  101. ret = asoc_simple_card_set_dailink_name(dev, dai_link,
  102. "fe.%s",
  103. dai_link->cpu_dai_name);
  104. if (ret < 0)
  105. return ret;
  106. asoc_simple_card_canonicalize_cpu(dai_link, is_single_links);
  107. } else {
  108. /* FE is dummy */
  109. dai_link->cpu_of_node = NULL;
  110. dai_link->cpu_dai_name = "snd-soc-dummy-dai";
  111. dai_link->cpu_name = "snd-soc-dummy";
  112. /* BE settings */
  113. dai_link->no_pcm = 1;
  114. dai_link->be_hw_params_fixup = asoc_simple_card_be_hw_params_fixup;
  115. ret = asoc_simple_card_parse_codec(np, dai_link, DAI, CELL);
  116. if (ret < 0)
  117. return ret;
  118. ret = asoc_simple_card_parse_clk_codec(dev, np, dai_link, dai_props);
  119. if (ret < 0)
  120. return ret;
  121. ret = asoc_simple_card_set_dailink_name(dev, dai_link,
  122. "be.%s",
  123. dai_link->codec_dai_name);
  124. if (ret < 0)
  125. return ret;
  126. snd_soc_of_parse_audio_prefix(card,
  127. &priv->codec_conf,
  128. dai_link->codec_of_node,
  129. PREFIX "prefix");
  130. }
  131. ret = asoc_simple_card_of_parse_tdm(np, dai_props);
  132. if (ret)
  133. return ret;
  134. ret = asoc_simple_card_canonicalize_dailink(dai_link);
  135. if (ret < 0)
  136. return ret;
  137. dai_link->dai_fmt = daifmt;
  138. dai_link->dpcm_playback = 1;
  139. dai_link->dpcm_capture = 1;
  140. dai_link->ops = &asoc_simple_card_ops;
  141. dai_link->init = asoc_simple_card_dai_init;
  142. return 0;
  143. }
  144. static int asoc_simple_card_parse_of(struct simple_card_data *priv)
  145. {
  146. struct device *dev = simple_priv_to_dev(priv);
  147. struct device_node *np;
  148. struct snd_soc_card *card = simple_priv_to_card(priv);
  149. struct device_node *node = dev->of_node;
  150. unsigned int daifmt = 0;
  151. bool is_fe;
  152. int ret, i;
  153. if (!node)
  154. return -EINVAL;
  155. ret = asoc_simple_card_of_parse_widgets(card, PREFIX);
  156. if (ret < 0)
  157. return ret;
  158. ret = asoc_simple_card_of_parse_routing(card, PREFIX, 0);
  159. if (ret < 0)
  160. return ret;
  161. asoc_simple_card_parse_convert(dev, PREFIX, &priv->adata);
  162. /* find 1st codec */
  163. np = of_get_child_by_name(node, PREFIX "codec");
  164. if (!np)
  165. return -ENODEV;
  166. ret = asoc_simple_card_parse_daifmt(dev, node, np, PREFIX, &daifmt);
  167. if (ret < 0)
  168. return ret;
  169. i = 0;
  170. for_each_child_of_node(node, np) {
  171. is_fe = false;
  172. if (strcmp(np->name, PREFIX "cpu") == 0)
  173. is_fe = true;
  174. ret = asoc_simple_card_dai_link_of(np, priv, daifmt, i, is_fe);
  175. if (ret < 0)
  176. return ret;
  177. i++;
  178. }
  179. ret = asoc_simple_card_parse_card_name(card, PREFIX);
  180. if (ret < 0)
  181. return ret;
  182. return 0;
  183. }
  184. static int asoc_simple_card_probe(struct platform_device *pdev)
  185. {
  186. struct simple_card_data *priv;
  187. struct snd_soc_dai_link *dai_link;
  188. struct asoc_simple_dai *dai_props;
  189. struct snd_soc_card *card;
  190. struct device *dev = &pdev->dev;
  191. struct device_node *np = dev->of_node;
  192. int num, ret;
  193. /* Allocate the private data */
  194. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  195. if (!priv)
  196. return -ENOMEM;
  197. num = of_get_child_count(np);
  198. dai_props = devm_kcalloc(dev, num, sizeof(*dai_props), GFP_KERNEL);
  199. dai_link = devm_kcalloc(dev, num, sizeof(*dai_link), GFP_KERNEL);
  200. if (!dai_props || !dai_link)
  201. return -ENOMEM;
  202. priv->dai_props = dai_props;
  203. priv->dai_link = dai_link;
  204. /* Init snd_soc_card */
  205. card = simple_priv_to_card(priv);
  206. card->owner = THIS_MODULE;
  207. card->dev = dev;
  208. card->dai_link = priv->dai_link;
  209. card->num_links = num;
  210. card->codec_conf = &priv->codec_conf;
  211. card->num_configs = 1;
  212. ret = asoc_simple_card_parse_of(priv);
  213. if (ret < 0) {
  214. if (ret != -EPROBE_DEFER)
  215. dev_err(dev, "parse error %d\n", ret);
  216. goto err;
  217. }
  218. snd_soc_card_set_drvdata(card, priv);
  219. ret = devm_snd_soc_register_card(dev, card);
  220. if (ret < 0)
  221. goto err;
  222. return 0;
  223. err:
  224. asoc_simple_card_clean_reference(card);
  225. return ret;
  226. }
  227. static int asoc_simple_card_remove(struct platform_device *pdev)
  228. {
  229. struct snd_soc_card *card = platform_get_drvdata(pdev);
  230. return asoc_simple_card_clean_reference(card);
  231. }
  232. static const struct of_device_id asoc_simple_of_match[] = {
  233. { .compatible = "renesas,rsrc-card", },
  234. { .compatible = "simple-scu-audio-card", },
  235. {},
  236. };
  237. MODULE_DEVICE_TABLE(of, asoc_simple_of_match);
  238. static struct platform_driver asoc_simple_card = {
  239. .driver = {
  240. .name = "simple-scu-audio-card",
  241. .pm = &snd_soc_pm_ops,
  242. .of_match_table = asoc_simple_of_match,
  243. },
  244. .probe = asoc_simple_card_probe,
  245. .remove = asoc_simple_card_remove,
  246. };
  247. module_platform_driver(asoc_simple_card);
  248. MODULE_ALIAS("platform:asoc-simple-scu-card");
  249. MODULE_LICENSE("GPL v2");
  250. MODULE_DESCRIPTION("ASoC Simple SCU Sound Card");
  251. MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");