audio-graph-card.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. // SPDX-License-Identifier: GPL-2.0
  2. //
  3. // ASoC audio graph sound card support
  4. //
  5. // Copyright (C) 2016 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/gpio.h>
  12. #include <linux/gpio/consumer.h>
  13. #include <linux/module.h>
  14. #include <linux/of.h>
  15. #include <linux/of_device.h>
  16. #include <linux/of_gpio.h>
  17. #include <linux/of_graph.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/string.h>
  20. #include <sound/simple_card_utils.h>
  21. struct graph_card_data {
  22. struct snd_soc_card snd_card;
  23. struct graph_dai_props {
  24. struct asoc_simple_dai cpu_dai;
  25. struct asoc_simple_dai codec_dai;
  26. unsigned int mclk_fs;
  27. } *dai_props;
  28. unsigned int mclk_fs;
  29. struct asoc_simple_jack hp_jack;
  30. struct asoc_simple_jack mic_jack;
  31. struct snd_soc_dai_link *dai_link;
  32. struct gpio_desc *pa_gpio;
  33. };
  34. static int asoc_graph_card_outdrv_event(struct snd_soc_dapm_widget *w,
  35. struct snd_kcontrol *kcontrol,
  36. int event)
  37. {
  38. struct snd_soc_dapm_context *dapm = w->dapm;
  39. struct graph_card_data *priv = snd_soc_card_get_drvdata(dapm->card);
  40. switch (event) {
  41. case SND_SOC_DAPM_POST_PMU:
  42. gpiod_set_value_cansleep(priv->pa_gpio, 1);
  43. break;
  44. case SND_SOC_DAPM_PRE_PMD:
  45. gpiod_set_value_cansleep(priv->pa_gpio, 0);
  46. break;
  47. default:
  48. return -EINVAL;
  49. }
  50. return 0;
  51. }
  52. static const struct snd_soc_dapm_widget asoc_graph_card_dapm_widgets[] = {
  53. SND_SOC_DAPM_OUT_DRV_E("Amplifier", SND_SOC_NOPM,
  54. 0, 0, NULL, 0, asoc_graph_card_outdrv_event,
  55. SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
  56. };
  57. #define graph_priv_to_card(priv) (&(priv)->snd_card)
  58. #define graph_priv_to_props(priv, i) ((priv)->dai_props + (i))
  59. #define graph_priv_to_dev(priv) (graph_priv_to_card(priv)->dev)
  60. #define graph_priv_to_link(priv, i) (graph_priv_to_card(priv)->dai_link + (i))
  61. static int asoc_graph_card_startup(struct snd_pcm_substream *substream)
  62. {
  63. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  64. struct graph_card_data *priv = snd_soc_card_get_drvdata(rtd->card);
  65. struct graph_dai_props *dai_props = graph_priv_to_props(priv, rtd->num);
  66. int ret;
  67. ret = asoc_simple_card_clk_enable(&dai_props->cpu_dai);
  68. if (ret)
  69. return ret;
  70. ret = asoc_simple_card_clk_enable(&dai_props->codec_dai);
  71. if (ret)
  72. asoc_simple_card_clk_disable(&dai_props->cpu_dai);
  73. return ret;
  74. }
  75. static void asoc_graph_card_shutdown(struct snd_pcm_substream *substream)
  76. {
  77. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  78. struct graph_card_data *priv = snd_soc_card_get_drvdata(rtd->card);
  79. struct graph_dai_props *dai_props = graph_priv_to_props(priv, rtd->num);
  80. asoc_simple_card_clk_disable(&dai_props->cpu_dai);
  81. asoc_simple_card_clk_disable(&dai_props->codec_dai);
  82. }
  83. static int asoc_graph_card_hw_params(struct snd_pcm_substream *substream,
  84. struct snd_pcm_hw_params *params)
  85. {
  86. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  87. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  88. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  89. struct graph_card_data *priv = snd_soc_card_get_drvdata(rtd->card);
  90. struct graph_dai_props *dai_props = graph_priv_to_props(priv, rtd->num);
  91. unsigned int mclk, mclk_fs = 0;
  92. int ret = 0;
  93. if (priv->mclk_fs)
  94. mclk_fs = priv->mclk_fs;
  95. else if (dai_props->mclk_fs)
  96. mclk_fs = dai_props->mclk_fs;
  97. if (mclk_fs) {
  98. mclk = params_rate(params) * mclk_fs;
  99. ret = snd_soc_dai_set_sysclk(codec_dai, 0, mclk,
  100. SND_SOC_CLOCK_IN);
  101. if (ret && ret != -ENOTSUPP)
  102. goto err;
  103. ret = snd_soc_dai_set_sysclk(cpu_dai, 0, mclk,
  104. SND_SOC_CLOCK_OUT);
  105. if (ret && ret != -ENOTSUPP)
  106. goto err;
  107. }
  108. return 0;
  109. err:
  110. return ret;
  111. }
  112. static const struct snd_soc_ops asoc_graph_card_ops = {
  113. .startup = asoc_graph_card_startup,
  114. .shutdown = asoc_graph_card_shutdown,
  115. .hw_params = asoc_graph_card_hw_params,
  116. };
  117. static int asoc_graph_card_dai_init(struct snd_soc_pcm_runtime *rtd)
  118. {
  119. struct graph_card_data *priv = snd_soc_card_get_drvdata(rtd->card);
  120. struct snd_soc_dai *codec = rtd->codec_dai;
  121. struct snd_soc_dai *cpu = rtd->cpu_dai;
  122. struct graph_dai_props *dai_props =
  123. graph_priv_to_props(priv, rtd->num);
  124. int ret;
  125. ret = asoc_simple_card_init_dai(codec, &dai_props->codec_dai);
  126. if (ret < 0)
  127. return ret;
  128. ret = asoc_simple_card_init_dai(cpu, &dai_props->cpu_dai);
  129. if (ret < 0)
  130. return ret;
  131. return 0;
  132. }
  133. static int asoc_graph_card_dai_link_of(struct device_node *cpu_port,
  134. struct graph_card_data *priv,
  135. int idx)
  136. {
  137. struct device *dev = graph_priv_to_dev(priv);
  138. struct snd_soc_dai_link *dai_link = graph_priv_to_link(priv, idx);
  139. struct graph_dai_props *dai_props = graph_priv_to_props(priv, idx);
  140. struct asoc_simple_dai *cpu_dai = &dai_props->cpu_dai;
  141. struct asoc_simple_dai *codec_dai = &dai_props->codec_dai;
  142. struct device_node *cpu_ep = of_get_next_child(cpu_port, NULL);
  143. struct device_node *codec_ep = of_graph_get_remote_endpoint(cpu_ep);
  144. struct device_node *rcpu_ep = of_graph_get_remote_endpoint(codec_ep);
  145. int ret;
  146. if (rcpu_ep != cpu_ep) {
  147. dev_err(dev, "remote-endpoint mismatch (%s/%s/%s)\n",
  148. cpu_ep->name, codec_ep->name, rcpu_ep->name);
  149. ret = -EINVAL;
  150. goto dai_link_of_err;
  151. }
  152. ret = asoc_simple_card_parse_daifmt(dev, cpu_ep, codec_ep,
  153. NULL, &dai_link->dai_fmt);
  154. if (ret < 0)
  155. goto dai_link_of_err;
  156. of_property_read_u32(rcpu_ep, "mclk-fs", &dai_props->mclk_fs);
  157. ret = asoc_simple_card_parse_graph_cpu(cpu_ep, dai_link);
  158. if (ret < 0)
  159. goto dai_link_of_err;
  160. ret = asoc_simple_card_parse_graph_codec(codec_ep, dai_link);
  161. if (ret < 0)
  162. goto dai_link_of_err;
  163. ret = asoc_simple_card_of_parse_tdm(cpu_ep, cpu_dai);
  164. if (ret < 0)
  165. goto dai_link_of_err;
  166. ret = asoc_simple_card_of_parse_tdm(codec_ep, codec_dai);
  167. if (ret < 0)
  168. goto dai_link_of_err;
  169. ret = asoc_simple_card_parse_clk_cpu(dev, cpu_ep, dai_link, cpu_dai);
  170. if (ret < 0)
  171. goto dai_link_of_err;
  172. ret = asoc_simple_card_parse_clk_codec(dev, codec_ep, dai_link, codec_dai);
  173. if (ret < 0)
  174. goto dai_link_of_err;
  175. ret = asoc_simple_card_canonicalize_dailink(dai_link);
  176. if (ret < 0)
  177. goto dai_link_of_err;
  178. ret = asoc_simple_card_set_dailink_name(dev, dai_link,
  179. "%s-%s",
  180. dai_link->cpu_dai_name,
  181. dai_link->codec_dai_name);
  182. if (ret < 0)
  183. goto dai_link_of_err;
  184. dai_link->ops = &asoc_graph_card_ops;
  185. dai_link->init = asoc_graph_card_dai_init;
  186. asoc_simple_card_canonicalize_cpu(dai_link,
  187. of_graph_get_endpoint_count(dai_link->cpu_of_node) == 1);
  188. dai_link_of_err:
  189. of_node_put(cpu_ep);
  190. of_node_put(rcpu_ep);
  191. of_node_put(codec_ep);
  192. return ret;
  193. }
  194. static int asoc_graph_card_parse_of(struct graph_card_data *priv)
  195. {
  196. struct of_phandle_iterator it;
  197. struct device *dev = graph_priv_to_dev(priv);
  198. struct snd_soc_card *card = graph_priv_to_card(priv);
  199. struct device_node *node = dev->of_node;
  200. int rc, idx = 0;
  201. int ret;
  202. ret = asoc_simple_card_of_parse_widgets(card, NULL);
  203. if (ret < 0)
  204. return ret;
  205. ret = asoc_simple_card_of_parse_routing(card, NULL, 1);
  206. if (ret < 0)
  207. return ret;
  208. /* Factor to mclk, used in hw_params() */
  209. of_property_read_u32(node, "mclk-fs", &priv->mclk_fs);
  210. of_for_each_phandle(&it, rc, node, "dais", NULL, 0) {
  211. ret = asoc_graph_card_dai_link_of(it.node, priv, idx++);
  212. if (ret < 0) {
  213. of_node_put(it.node);
  214. return ret;
  215. }
  216. }
  217. return asoc_simple_card_parse_card_name(card, NULL);
  218. }
  219. static int asoc_graph_get_dais_count(struct device *dev)
  220. {
  221. struct of_phandle_iterator it;
  222. struct device_node *node = dev->of_node;
  223. int count = 0;
  224. int rc;
  225. of_for_each_phandle(&it, rc, node, "dais", NULL, 0)
  226. count++;
  227. return count;
  228. }
  229. static int asoc_graph_soc_card_probe(struct snd_soc_card *card)
  230. {
  231. struct graph_card_data *priv = snd_soc_card_get_drvdata(card);
  232. int ret;
  233. ret = asoc_simple_card_init_hp(card, &priv->hp_jack, NULL);
  234. if (ret < 0)
  235. return ret;
  236. ret = asoc_simple_card_init_mic(card, &priv->mic_jack, NULL);
  237. if (ret < 0)
  238. return ret;
  239. return 0;
  240. }
  241. static int asoc_graph_card_probe(struct platform_device *pdev)
  242. {
  243. struct graph_card_data *priv;
  244. struct snd_soc_dai_link *dai_link;
  245. struct graph_dai_props *dai_props;
  246. struct device *dev = &pdev->dev;
  247. struct snd_soc_card *card;
  248. int num, ret;
  249. /* Allocate the private data and the DAI link array */
  250. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  251. if (!priv)
  252. return -ENOMEM;
  253. num = asoc_graph_get_dais_count(dev);
  254. if (num == 0)
  255. return -EINVAL;
  256. dai_props = devm_kcalloc(dev, num, sizeof(*dai_props), GFP_KERNEL);
  257. dai_link = devm_kcalloc(dev, num, sizeof(*dai_link), GFP_KERNEL);
  258. if (!dai_props || !dai_link)
  259. return -ENOMEM;
  260. priv->pa_gpio = devm_gpiod_get_optional(dev, "pa", GPIOD_OUT_LOW);
  261. if (IS_ERR(priv->pa_gpio)) {
  262. ret = PTR_ERR(priv->pa_gpio);
  263. dev_err(dev, "failed to get amplifier gpio: %d\n", ret);
  264. return ret;
  265. }
  266. priv->dai_props = dai_props;
  267. priv->dai_link = dai_link;
  268. /* Init snd_soc_card */
  269. card = graph_priv_to_card(priv);
  270. card->owner = THIS_MODULE;
  271. card->dev = dev;
  272. card->dai_link = dai_link;
  273. card->num_links = num;
  274. card->dapm_widgets = asoc_graph_card_dapm_widgets;
  275. card->num_dapm_widgets = ARRAY_SIZE(asoc_graph_card_dapm_widgets);
  276. card->probe = asoc_graph_soc_card_probe;
  277. ret = asoc_graph_card_parse_of(priv);
  278. if (ret < 0) {
  279. if (ret != -EPROBE_DEFER)
  280. dev_err(dev, "parse error %d\n", ret);
  281. goto err;
  282. }
  283. snd_soc_card_set_drvdata(card, priv);
  284. ret = devm_snd_soc_register_card(dev, card);
  285. if (ret < 0)
  286. goto err;
  287. return 0;
  288. err:
  289. asoc_simple_card_clean_reference(card);
  290. return ret;
  291. }
  292. static int asoc_graph_card_remove(struct platform_device *pdev)
  293. {
  294. struct snd_soc_card *card = platform_get_drvdata(pdev);
  295. return asoc_simple_card_clean_reference(card);
  296. }
  297. static const struct of_device_id asoc_graph_of_match[] = {
  298. { .compatible = "audio-graph-card", },
  299. {},
  300. };
  301. MODULE_DEVICE_TABLE(of, asoc_graph_of_match);
  302. static struct platform_driver asoc_graph_card = {
  303. .driver = {
  304. .name = "asoc-audio-graph-card",
  305. .pm = &snd_soc_pm_ops,
  306. .of_match_table = asoc_graph_of_match,
  307. },
  308. .probe = asoc_graph_card_probe,
  309. .remove = asoc_graph_card_remove,
  310. };
  311. module_platform_driver(asoc_graph_card);
  312. MODULE_ALIAS("platform:asoc-audio-graph-card");
  313. MODULE_LICENSE("GPL v2");
  314. MODULE_DESCRIPTION("ASoC Audio Graph Sound Card");
  315. MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");