rsrc-card.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. /*
  2. * Renesas Sampling Rate Convert Sound Card for DPCM
  3. *
  4. * Copyright (C) 2015 Renesas Solutions Corp.
  5. * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
  6. *
  7. * based on ${LINUX}/sound/soc/generic/simple-card.c
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. */
  13. #include <linux/clk.h>
  14. #include <linux/device.h>
  15. #include <linux/module.h>
  16. #include <linux/of.h>
  17. #include <linux/of_device.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/string.h>
  20. #include <sound/jack.h>
  21. #include <sound/soc.h>
  22. #include <sound/soc-dai.h>
  23. struct rsrc_card_of_data {
  24. const char *prefix;
  25. const struct snd_soc_dapm_route *routes;
  26. int num_routes;
  27. };
  28. static const struct snd_soc_dapm_route routes_ssi0_ak4642[] = {
  29. {"ak4642 Playback", NULL, "DAI0 Playback"},
  30. {"DAI0 Capture", NULL, "ak4642 Capture"},
  31. };
  32. static const struct rsrc_card_of_data routes_of_ssi0_ak4642 = {
  33. .prefix = "ak4642",
  34. .routes = routes_ssi0_ak4642,
  35. .num_routes = ARRAY_SIZE(routes_ssi0_ak4642),
  36. };
  37. static const struct of_device_id rsrc_card_of_match[] = {
  38. { .compatible = "renesas,rsrc-card,lager", .data = &routes_of_ssi0_ak4642 },
  39. { .compatible = "renesas,rsrc-card,koelsch", .data = &routes_of_ssi0_ak4642 },
  40. { .compatible = "renesas,rsrc-card", },
  41. {},
  42. };
  43. MODULE_DEVICE_TABLE(of, rsrc_card_of_match);
  44. #define DAI_NAME_NUM 32
  45. struct rsrc_card_dai {
  46. unsigned int fmt;
  47. unsigned int sysclk;
  48. struct clk *clk;
  49. char dai_name[DAI_NAME_NUM];
  50. };
  51. #define IDX_CPU 0
  52. #define IDX_CODEC 1
  53. struct rsrc_card_priv {
  54. struct snd_soc_card snd_card;
  55. struct snd_soc_codec_conf codec_conf;
  56. struct rsrc_card_dai *dai_props;
  57. struct snd_soc_dai_link *dai_link;
  58. int dai_num;
  59. u32 convert_rate;
  60. };
  61. #define rsrc_priv_to_dev(priv) ((priv)->snd_card.dev)
  62. #define rsrc_priv_to_link(priv, i) ((priv)->snd_card.dai_link + (i))
  63. #define rsrc_priv_to_props(priv, i) ((priv)->dai_props + (i))
  64. #define rsrc_dev_to_of_data(dev) (of_match_device(rsrc_card_of_match, (dev))->data)
  65. static int rsrc_card_startup(struct snd_pcm_substream *substream)
  66. {
  67. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  68. struct rsrc_card_priv *priv = snd_soc_card_get_drvdata(rtd->card);
  69. struct rsrc_card_dai *dai_props =
  70. rsrc_priv_to_props(priv, rtd - rtd->card->rtd);
  71. return clk_prepare_enable(dai_props->clk);
  72. }
  73. static void rsrc_card_shutdown(struct snd_pcm_substream *substream)
  74. {
  75. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  76. struct rsrc_card_priv *priv = snd_soc_card_get_drvdata(rtd->card);
  77. struct rsrc_card_dai *dai_props =
  78. rsrc_priv_to_props(priv, rtd - rtd->card->rtd);
  79. clk_disable_unprepare(dai_props->clk);
  80. }
  81. static struct snd_soc_ops rsrc_card_ops = {
  82. .startup = rsrc_card_startup,
  83. .shutdown = rsrc_card_shutdown,
  84. };
  85. static int rsrc_card_dai_init(struct snd_soc_pcm_runtime *rtd)
  86. {
  87. struct rsrc_card_priv *priv = snd_soc_card_get_drvdata(rtd->card);
  88. struct snd_soc_dai *dai;
  89. struct snd_soc_dai_link *dai_link;
  90. struct rsrc_card_dai *dai_props;
  91. int num = rtd - rtd->card->rtd;
  92. int ret;
  93. dai_link = rsrc_priv_to_link(priv, num);
  94. dai_props = rsrc_priv_to_props(priv, num);
  95. dai = dai_link->dynamic ?
  96. rtd->cpu_dai :
  97. rtd->codec_dai;
  98. if (dai_props->fmt) {
  99. ret = snd_soc_dai_set_fmt(dai, dai_props->fmt);
  100. if (ret && ret != -ENOTSUPP) {
  101. dev_err(dai->dev, "set_fmt error\n");
  102. goto err;
  103. }
  104. }
  105. if (dai_props->sysclk) {
  106. ret = snd_soc_dai_set_sysclk(dai, 0, dai_props->sysclk, 0);
  107. if (ret && ret != -ENOTSUPP) {
  108. dev_err(dai->dev, "set_sysclk error\n");
  109. goto err;
  110. }
  111. }
  112. ret = 0;
  113. err:
  114. return ret;
  115. }
  116. static int rsrc_card_be_hw_params_fixup(struct snd_soc_pcm_runtime *rtd,
  117. struct snd_pcm_hw_params *params)
  118. {
  119. struct rsrc_card_priv *priv = snd_soc_card_get_drvdata(rtd->card);
  120. struct snd_interval *rate = hw_param_interval(params,
  121. SNDRV_PCM_HW_PARAM_RATE);
  122. if (!priv->convert_rate)
  123. return 0;
  124. rate->min = rate->max = priv->convert_rate;
  125. return 0;
  126. }
  127. static int rsrc_card_parse_daifmt(struct device_node *node,
  128. struct device_node *np,
  129. struct rsrc_card_priv *priv,
  130. int idx, bool is_fe)
  131. {
  132. struct rsrc_card_dai *dai_props = rsrc_priv_to_props(priv, idx);
  133. struct device_node *bitclkmaster = NULL;
  134. struct device_node *framemaster = NULL;
  135. struct device_node *codec = is_fe ? NULL : np;
  136. unsigned int daifmt;
  137. daifmt = snd_soc_of_parse_daifmt(node, NULL,
  138. &bitclkmaster, &framemaster);
  139. daifmt &= ~SND_SOC_DAIFMT_MASTER_MASK;
  140. if (!bitclkmaster && !framemaster)
  141. return -EINVAL;
  142. if (codec == bitclkmaster)
  143. daifmt |= (codec == framemaster) ?
  144. SND_SOC_DAIFMT_CBM_CFM : SND_SOC_DAIFMT_CBM_CFS;
  145. else
  146. daifmt |= (codec == framemaster) ?
  147. SND_SOC_DAIFMT_CBS_CFM : SND_SOC_DAIFMT_CBS_CFS;
  148. dai_props->fmt = daifmt;
  149. of_node_put(bitclkmaster);
  150. of_node_put(framemaster);
  151. return 0;
  152. }
  153. static int rsrc_card_parse_links(struct device_node *np,
  154. struct rsrc_card_priv *priv,
  155. int idx, bool is_fe)
  156. {
  157. struct snd_soc_dai_link *dai_link = rsrc_priv_to_link(priv, idx);
  158. struct rsrc_card_dai *dai_props = rsrc_priv_to_props(priv, idx);
  159. struct of_phandle_args args;
  160. int ret;
  161. /*
  162. * Get node via "sound-dai = <&phandle port>"
  163. * it will be used as xxx_of_node on soc_bind_dai_link()
  164. */
  165. ret = of_parse_phandle_with_args(np, "sound-dai",
  166. "#sound-dai-cells", 0, &args);
  167. if (ret)
  168. return ret;
  169. if (is_fe) {
  170. /* BE is dummy */
  171. dai_link->codec_of_node = NULL;
  172. dai_link->codec_dai_name = "snd-soc-dummy-dai";
  173. dai_link->codec_name = "snd-soc-dummy";
  174. /* FE settings */
  175. dai_link->dynamic = 1;
  176. dai_link->dpcm_merged_format = 1;
  177. dai_link->cpu_of_node = args.np;
  178. snd_soc_of_get_dai_name(np, &dai_link->cpu_dai_name);
  179. /* set dai_name */
  180. snprintf(dai_props->dai_name, DAI_NAME_NUM, "fe.%s",
  181. dai_link->cpu_dai_name);
  182. /*
  183. * In soc_bind_dai_link() will check cpu name after
  184. * of_node matching if dai_link has cpu_dai_name.
  185. * but, it will never match if name was created by
  186. * fmt_single_name() remove cpu_dai_name if cpu_args
  187. * was 0. See:
  188. * fmt_single_name()
  189. * fmt_multiple_name()
  190. */
  191. if (!args.args_count)
  192. dai_link->cpu_dai_name = NULL;
  193. } else {
  194. struct device *dev = rsrc_priv_to_dev(priv);
  195. const struct rsrc_card_of_data *of_data;
  196. of_data = rsrc_dev_to_of_data(dev);
  197. /* FE is dummy */
  198. dai_link->cpu_of_node = NULL;
  199. dai_link->cpu_dai_name = "snd-soc-dummy-dai";
  200. dai_link->cpu_name = "snd-soc-dummy";
  201. /* BE settings */
  202. dai_link->no_pcm = 1;
  203. dai_link->be_hw_params_fixup = rsrc_card_be_hw_params_fixup;
  204. dai_link->codec_of_node = args.np;
  205. snd_soc_of_get_dai_name(np, &dai_link->codec_dai_name);
  206. /* additional name prefix */
  207. if (of_data) {
  208. priv->codec_conf.of_node = dai_link->codec_of_node;
  209. priv->codec_conf.name_prefix = of_data->prefix;
  210. } else {
  211. snd_soc_of_parse_audio_prefix(&priv->snd_card,
  212. &priv->codec_conf,
  213. dai_link->codec_of_node,
  214. "audio-prefix");
  215. }
  216. /* set dai_name */
  217. snprintf(dai_props->dai_name, DAI_NAME_NUM, "be.%s",
  218. dai_link->codec_dai_name);
  219. }
  220. /* Simple Card assumes platform == cpu */
  221. dai_link->platform_of_node = dai_link->cpu_of_node;
  222. dai_link->dpcm_playback = 1;
  223. dai_link->dpcm_capture = 1;
  224. dai_link->name = dai_props->dai_name;
  225. dai_link->stream_name = dai_props->dai_name;
  226. dai_link->ops = &rsrc_card_ops;
  227. dai_link->init = rsrc_card_dai_init;
  228. return 0;
  229. }
  230. static int rsrc_card_parse_clk(struct device_node *np,
  231. struct rsrc_card_priv *priv,
  232. int idx, bool is_fe)
  233. {
  234. struct snd_soc_dai_link *dai_link = rsrc_priv_to_link(priv, idx);
  235. struct rsrc_card_dai *dai_props = rsrc_priv_to_props(priv, idx);
  236. struct clk *clk;
  237. struct device_node *of_np = is_fe ? dai_link->cpu_of_node :
  238. dai_link->codec_of_node;
  239. u32 val;
  240. /*
  241. * Parse dai->sysclk come from "clocks = <&xxx>"
  242. * (if system has common clock)
  243. * or "system-clock-frequency = <xxx>"
  244. * or device's module clock.
  245. */
  246. if (of_property_read_bool(np, "clocks")) {
  247. clk = of_clk_get(np, 0);
  248. if (IS_ERR(clk))
  249. return PTR_ERR(clk);
  250. dai_props->sysclk = clk_get_rate(clk);
  251. dai_props->clk = clk;
  252. } else if (!of_property_read_u32(np, "system-clock-frequency", &val)) {
  253. dai_props->sysclk = val;
  254. } else {
  255. clk = of_clk_get(of_np, 0);
  256. if (!IS_ERR(clk))
  257. dai_props->sysclk = clk_get_rate(clk);
  258. }
  259. return 0;
  260. }
  261. static int rsrc_card_dai_link_of(struct device_node *node,
  262. struct device_node *np,
  263. struct rsrc_card_priv *priv,
  264. int idx)
  265. {
  266. struct device *dev = rsrc_priv_to_dev(priv);
  267. struct rsrc_card_dai *dai_props = rsrc_priv_to_props(priv, idx);
  268. bool is_fe = false;
  269. int ret;
  270. if (0 == strcmp(np->name, "cpu"))
  271. is_fe = true;
  272. ret = rsrc_card_parse_daifmt(node, np, priv, idx, is_fe);
  273. if (ret < 0)
  274. return ret;
  275. ret = rsrc_card_parse_links(np, priv, idx, is_fe);
  276. if (ret < 0)
  277. return ret;
  278. ret = rsrc_card_parse_clk(np, priv, idx, is_fe);
  279. if (ret < 0)
  280. return ret;
  281. dev_dbg(dev, "\t%s / %04x / %d\n",
  282. dai_props->dai_name,
  283. dai_props->fmt,
  284. dai_props->sysclk);
  285. return ret;
  286. }
  287. static int rsrc_card_parse_of(struct device_node *node,
  288. struct rsrc_card_priv *priv,
  289. struct device *dev)
  290. {
  291. const struct rsrc_card_of_data *of_data = rsrc_dev_to_of_data(dev);
  292. struct rsrc_card_dai *props;
  293. struct snd_soc_dai_link *links;
  294. struct device_node *np;
  295. int ret;
  296. int i, num;
  297. if (!node)
  298. return -EINVAL;
  299. num = of_get_child_count(node);
  300. props = devm_kzalloc(dev, sizeof(*props) * num, GFP_KERNEL);
  301. links = devm_kzalloc(dev, sizeof(*links) * num, GFP_KERNEL);
  302. if (!props || !links)
  303. return -ENOMEM;
  304. priv->dai_props = props;
  305. priv->dai_link = links;
  306. priv->dai_num = num;
  307. /* Init snd_soc_card */
  308. priv->snd_card.owner = THIS_MODULE;
  309. priv->snd_card.dev = dev;
  310. priv->snd_card.dai_link = priv->dai_link;
  311. priv->snd_card.num_links = num;
  312. priv->snd_card.codec_conf = &priv->codec_conf;
  313. priv->snd_card.num_configs = 1;
  314. if (of_data) {
  315. priv->snd_card.of_dapm_routes = of_data->routes;
  316. priv->snd_card.num_of_dapm_routes = of_data->num_routes;
  317. } else {
  318. snd_soc_of_parse_audio_routing(&priv->snd_card,
  319. "audio-routing");
  320. }
  321. /* Parse the card name from DT */
  322. snd_soc_of_parse_card_name(&priv->snd_card, "card-name");
  323. /* sampling rate convert */
  324. of_property_read_u32(node, "convert-rate", &priv->convert_rate);
  325. dev_dbg(dev, "New rsrc-audio-card: %s (%d)\n",
  326. priv->snd_card.name ? priv->snd_card.name : "",
  327. priv->convert_rate);
  328. i = 0;
  329. for_each_child_of_node(node, np) {
  330. ret = rsrc_card_dai_link_of(node, np, priv, i);
  331. if (ret < 0)
  332. return ret;
  333. i++;
  334. }
  335. if (!priv->snd_card.name)
  336. priv->snd_card.name = priv->snd_card.dai_link->name;
  337. return 0;
  338. }
  339. /* Decrease the reference count of the device nodes */
  340. static int rsrc_card_unref(struct snd_soc_card *card)
  341. {
  342. struct snd_soc_dai_link *dai_link;
  343. int num_links;
  344. for (num_links = 0, dai_link = card->dai_link;
  345. num_links < card->num_links;
  346. num_links++, dai_link++) {
  347. of_node_put(dai_link->cpu_of_node);
  348. of_node_put(dai_link->codec_of_node);
  349. }
  350. return 0;
  351. }
  352. static int rsrc_card_probe(struct platform_device *pdev)
  353. {
  354. struct rsrc_card_priv *priv;
  355. struct device_node *np = pdev->dev.of_node;
  356. struct device *dev = &pdev->dev;
  357. int ret;
  358. /* Allocate the private data */
  359. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  360. if (!priv)
  361. return -ENOMEM;
  362. ret = rsrc_card_parse_of(np, priv, dev);
  363. if (ret < 0) {
  364. if (ret != -EPROBE_DEFER)
  365. dev_err(dev, "parse error %d\n", ret);
  366. goto err;
  367. }
  368. snd_soc_card_set_drvdata(&priv->snd_card, priv);
  369. ret = devm_snd_soc_register_card(&pdev->dev, &priv->snd_card);
  370. if (ret >= 0)
  371. return ret;
  372. err:
  373. rsrc_card_unref(&priv->snd_card);
  374. return ret;
  375. }
  376. static int rsrc_card_remove(struct platform_device *pdev)
  377. {
  378. struct snd_soc_card *card = platform_get_drvdata(pdev);
  379. return rsrc_card_unref(card);
  380. }
  381. static struct platform_driver rsrc_card = {
  382. .driver = {
  383. .name = "renesas-src-audio-card",
  384. .of_match_table = rsrc_card_of_match,
  385. },
  386. .probe = rsrc_card_probe,
  387. .remove = rsrc_card_remove,
  388. };
  389. module_platform_driver(rsrc_card);
  390. MODULE_ALIAS("platform:renesas-src-audio-card");
  391. MODULE_LICENSE("GPL");
  392. MODULE_DESCRIPTION("Renesas Sampling Rate Convert Sound Card");
  393. MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");