axg-card.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. // SPDX-License-Identifier: (GPL-2.0 OR MIT)
  2. //
  3. // Copyright (c) 2018 BayLibre, SAS.
  4. // Author: Jerome Brunet <jbrunet@baylibre.com>
  5. #include <linux/module.h>
  6. #include <linux/of_platform.h>
  7. #include <sound/soc.h>
  8. #include <sound/soc-dai.h>
  9. #include "axg-tdm.h"
  10. #include "meson-card.h"
  11. struct axg_dai_link_tdm_mask {
  12. u32 tx;
  13. u32 rx;
  14. };
  15. struct axg_dai_link_tdm_data {
  16. unsigned int mclk_fs;
  17. unsigned int slots;
  18. unsigned int slot_width;
  19. u32 *tx_mask;
  20. u32 *rx_mask;
  21. struct axg_dai_link_tdm_mask *codec_masks;
  22. };
  23. /*
  24. * Base params for the codec to codec links
  25. * Those will be over-written by the CPU side of the link
  26. */
  27. static const struct snd_soc_pcm_stream codec_params = {
  28. .formats = SNDRV_PCM_FMTBIT_S24_LE,
  29. .rate_min = 5525,
  30. .rate_max = 192000,
  31. .channels_min = 1,
  32. .channels_max = 8,
  33. };
  34. static int axg_card_tdm_be_hw_params(struct snd_pcm_substream *substream,
  35. struct snd_pcm_hw_params *params)
  36. {
  37. struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
  38. struct meson_card *priv = snd_soc_card_get_drvdata(rtd->card);
  39. struct axg_dai_link_tdm_data *be =
  40. (struct axg_dai_link_tdm_data *)priv->link_data[rtd->num];
  41. return meson_card_i2s_set_sysclk(substream, params, be->mclk_fs);
  42. }
  43. static const struct snd_soc_ops axg_card_tdm_be_ops = {
  44. .hw_params = axg_card_tdm_be_hw_params,
  45. };
  46. static int axg_card_tdm_dai_init(struct snd_soc_pcm_runtime *rtd)
  47. {
  48. struct meson_card *priv = snd_soc_card_get_drvdata(rtd->card);
  49. struct axg_dai_link_tdm_data *be =
  50. (struct axg_dai_link_tdm_data *)priv->link_data[rtd->num];
  51. struct snd_soc_dai *codec_dai;
  52. int ret, i;
  53. for_each_rtd_codec_dais(rtd, i, codec_dai) {
  54. ret = snd_soc_dai_set_tdm_slot(codec_dai,
  55. be->codec_masks[i].tx,
  56. be->codec_masks[i].rx,
  57. be->slots, be->slot_width);
  58. if (ret && ret != -ENOTSUPP) {
  59. dev_err(codec_dai->dev,
  60. "setting tdm link slots failed\n");
  61. return ret;
  62. }
  63. }
  64. ret = axg_tdm_set_tdm_slots(asoc_rtd_to_cpu(rtd, 0), be->tx_mask, be->rx_mask,
  65. be->slots, be->slot_width);
  66. if (ret) {
  67. dev_err(asoc_rtd_to_cpu(rtd, 0)->dev, "setting tdm link slots failed\n");
  68. return ret;
  69. }
  70. return 0;
  71. }
  72. static int axg_card_tdm_dai_lb_init(struct snd_soc_pcm_runtime *rtd)
  73. {
  74. struct meson_card *priv = snd_soc_card_get_drvdata(rtd->card);
  75. struct axg_dai_link_tdm_data *be =
  76. (struct axg_dai_link_tdm_data *)priv->link_data[rtd->num];
  77. int ret;
  78. /* The loopback rx_mask is the pad tx_mask */
  79. ret = axg_tdm_set_tdm_slots(asoc_rtd_to_cpu(rtd, 0), NULL, be->tx_mask,
  80. be->slots, be->slot_width);
  81. if (ret) {
  82. dev_err(asoc_rtd_to_cpu(rtd, 0)->dev, "setting tdm link slots failed\n");
  83. return ret;
  84. }
  85. return 0;
  86. }
  87. static int axg_card_add_tdm_loopback(struct snd_soc_card *card,
  88. int *index)
  89. {
  90. struct meson_card *priv = snd_soc_card_get_drvdata(card);
  91. struct snd_soc_dai_link *pad = &card->dai_link[*index];
  92. struct snd_soc_dai_link *lb;
  93. struct snd_soc_dai_link_component *dlc;
  94. int ret;
  95. /* extend links */
  96. ret = meson_card_reallocate_links(card, card->num_links + 1);
  97. if (ret)
  98. return ret;
  99. lb = &card->dai_link[*index + 1];
  100. lb->name = devm_kasprintf(card->dev, GFP_KERNEL, "%s-lb", pad->name);
  101. if (!lb->name)
  102. return -ENOMEM;
  103. dlc = devm_kzalloc(card->dev, 2 * sizeof(*dlc), GFP_KERNEL);
  104. if (!dlc)
  105. return -ENOMEM;
  106. lb->cpus = &dlc[0];
  107. lb->codecs = &dlc[1];
  108. lb->num_cpus = 1;
  109. lb->num_codecs = 1;
  110. lb->stream_name = lb->name;
  111. lb->cpus->of_node = pad->cpus->of_node;
  112. lb->cpus->dai_name = "TDM Loopback";
  113. lb->codecs->name = "snd-soc-dummy";
  114. lb->codecs->dai_name = "snd-soc-dummy-dai";
  115. lb->dpcm_capture = 1;
  116. lb->no_pcm = 1;
  117. lb->ops = &axg_card_tdm_be_ops;
  118. lb->init = axg_card_tdm_dai_lb_init;
  119. /* Provide the same link data to the loopback */
  120. priv->link_data[*index + 1] = priv->link_data[*index];
  121. /*
  122. * axg_card_clean_references() will iterate over this link,
  123. * make sure the node count is balanced
  124. */
  125. of_node_get(lb->cpus->of_node);
  126. /* Let add_links continue where it should */
  127. *index += 1;
  128. return 0;
  129. }
  130. static int axg_card_parse_cpu_tdm_slots(struct snd_soc_card *card,
  131. struct snd_soc_dai_link *link,
  132. struct device_node *node,
  133. struct axg_dai_link_tdm_data *be)
  134. {
  135. char propname[32];
  136. u32 tx, rx;
  137. int i;
  138. be->tx_mask = devm_kcalloc(card->dev, AXG_TDM_NUM_LANES,
  139. sizeof(*be->tx_mask), GFP_KERNEL);
  140. be->rx_mask = devm_kcalloc(card->dev, AXG_TDM_NUM_LANES,
  141. sizeof(*be->rx_mask), GFP_KERNEL);
  142. if (!be->tx_mask || !be->rx_mask)
  143. return -ENOMEM;
  144. for (i = 0, tx = 0; i < AXG_TDM_NUM_LANES; i++) {
  145. snprintf(propname, 32, "dai-tdm-slot-tx-mask-%d", i);
  146. snd_soc_of_get_slot_mask(node, propname, &be->tx_mask[i]);
  147. tx = max(tx, be->tx_mask[i]);
  148. }
  149. /* Disable playback is the interface has no tx slots */
  150. if (!tx)
  151. link->dpcm_playback = 0;
  152. for (i = 0, rx = 0; i < AXG_TDM_NUM_LANES; i++) {
  153. snprintf(propname, 32, "dai-tdm-slot-rx-mask-%d", i);
  154. snd_soc_of_get_slot_mask(node, propname, &be->rx_mask[i]);
  155. rx = max(rx, be->rx_mask[i]);
  156. }
  157. /* Disable capture is the interface has no rx slots */
  158. if (!rx)
  159. link->dpcm_capture = 0;
  160. /* ... but the interface should at least have one of them */
  161. if (!tx && !rx) {
  162. dev_err(card->dev, "tdm link has no cpu slots\n");
  163. return -EINVAL;
  164. }
  165. of_property_read_u32(node, "dai-tdm-slot-num", &be->slots);
  166. if (!be->slots) {
  167. /*
  168. * If the slot number is not provided, set it such as it
  169. * accommodates the largest mask
  170. */
  171. be->slots = fls(max(tx, rx));
  172. } else if (be->slots < fls(max(tx, rx)) || be->slots > 32) {
  173. /*
  174. * Error if the slots can't accommodate the largest mask or
  175. * if it is just too big
  176. */
  177. dev_err(card->dev, "bad slot number\n");
  178. return -EINVAL;
  179. }
  180. of_property_read_u32(node, "dai-tdm-slot-width", &be->slot_width);
  181. return 0;
  182. }
  183. static int axg_card_parse_codecs_masks(struct snd_soc_card *card,
  184. struct snd_soc_dai_link *link,
  185. struct device_node *node,
  186. struct axg_dai_link_tdm_data *be)
  187. {
  188. struct axg_dai_link_tdm_mask *codec_mask;
  189. struct device_node *np;
  190. codec_mask = devm_kcalloc(card->dev, link->num_codecs,
  191. sizeof(*codec_mask), GFP_KERNEL);
  192. if (!codec_mask)
  193. return -ENOMEM;
  194. be->codec_masks = codec_mask;
  195. for_each_child_of_node(node, np) {
  196. snd_soc_of_get_slot_mask(np, "dai-tdm-slot-rx-mask",
  197. &codec_mask->rx);
  198. snd_soc_of_get_slot_mask(np, "dai-tdm-slot-tx-mask",
  199. &codec_mask->tx);
  200. codec_mask++;
  201. }
  202. return 0;
  203. }
  204. static int axg_card_parse_tdm(struct snd_soc_card *card,
  205. struct device_node *node,
  206. int *index)
  207. {
  208. struct meson_card *priv = snd_soc_card_get_drvdata(card);
  209. struct snd_soc_dai_link *link = &card->dai_link[*index];
  210. struct axg_dai_link_tdm_data *be;
  211. int ret;
  212. /* Allocate tdm link parameters */
  213. be = devm_kzalloc(card->dev, sizeof(*be), GFP_KERNEL);
  214. if (!be)
  215. return -ENOMEM;
  216. priv->link_data[*index] = be;
  217. /* Setup tdm link */
  218. link->ops = &axg_card_tdm_be_ops;
  219. link->init = axg_card_tdm_dai_init;
  220. link->dai_fmt = meson_card_parse_daifmt(node, link->cpus->of_node);
  221. of_property_read_u32(node, "mclk-fs", &be->mclk_fs);
  222. ret = axg_card_parse_cpu_tdm_slots(card, link, node, be);
  223. if (ret) {
  224. dev_err(card->dev, "error parsing tdm link slots\n");
  225. return ret;
  226. }
  227. ret = axg_card_parse_codecs_masks(card, link, node, be);
  228. if (ret)
  229. return ret;
  230. /* Add loopback if the pad dai has playback */
  231. if (link->dpcm_playback) {
  232. ret = axg_card_add_tdm_loopback(card, index);
  233. if (ret)
  234. return ret;
  235. }
  236. return 0;
  237. }
  238. static int axg_card_cpu_is_capture_fe(struct device_node *np)
  239. {
  240. return of_device_is_compatible(np, DT_PREFIX "axg-toddr");
  241. }
  242. static int axg_card_cpu_is_playback_fe(struct device_node *np)
  243. {
  244. return of_device_is_compatible(np, DT_PREFIX "axg-frddr");
  245. }
  246. static int axg_card_cpu_is_tdm_iface(struct device_node *np)
  247. {
  248. return of_device_is_compatible(np, DT_PREFIX "axg-tdm-iface");
  249. }
  250. static int axg_card_cpu_is_codec(struct device_node *np)
  251. {
  252. return of_device_is_compatible(np, DT_PREFIX "g12a-tohdmitx") ||
  253. of_device_is_compatible(np, DT_PREFIX "g12a-toacodec");
  254. }
  255. static int axg_card_add_link(struct snd_soc_card *card, struct device_node *np,
  256. int *index)
  257. {
  258. struct snd_soc_dai_link *dai_link = &card->dai_link[*index];
  259. struct snd_soc_dai_link_component *cpu;
  260. int ret;
  261. cpu = devm_kzalloc(card->dev, sizeof(*cpu), GFP_KERNEL);
  262. if (!cpu)
  263. return -ENOMEM;
  264. dai_link->cpus = cpu;
  265. dai_link->num_cpus = 1;
  266. ret = meson_card_parse_dai(card, np, &dai_link->cpus->of_node,
  267. &dai_link->cpus->dai_name);
  268. if (ret)
  269. return ret;
  270. if (axg_card_cpu_is_playback_fe(dai_link->cpus->of_node))
  271. return meson_card_set_fe_link(card, dai_link, np, true);
  272. else if (axg_card_cpu_is_capture_fe(dai_link->cpus->of_node))
  273. return meson_card_set_fe_link(card, dai_link, np, false);
  274. ret = meson_card_set_be_link(card, dai_link, np);
  275. if (ret)
  276. return ret;
  277. if (axg_card_cpu_is_codec(dai_link->cpus->of_node)) {
  278. dai_link->params = &codec_params;
  279. } else {
  280. dai_link->no_pcm = 1;
  281. snd_soc_dai_link_set_capabilities(dai_link);
  282. if (axg_card_cpu_is_tdm_iface(dai_link->cpus->of_node))
  283. ret = axg_card_parse_tdm(card, np, index);
  284. }
  285. return ret;
  286. }
  287. static const struct meson_card_match_data axg_card_match_data = {
  288. .add_link = axg_card_add_link,
  289. };
  290. static const struct of_device_id axg_card_of_match[] = {
  291. {
  292. .compatible = "amlogic,axg-sound-card",
  293. .data = &axg_card_match_data,
  294. }, {}
  295. };
  296. MODULE_DEVICE_TABLE(of, axg_card_of_match);
  297. static struct platform_driver axg_card_pdrv = {
  298. .probe = meson_card_probe,
  299. .remove = meson_card_remove,
  300. .driver = {
  301. .name = "axg-sound-card",
  302. .of_match_table = axg_card_of_match,
  303. },
  304. };
  305. module_platform_driver(axg_card_pdrv);
  306. MODULE_DESCRIPTION("Amlogic AXG ALSA machine driver");
  307. MODULE_AUTHOR("Jerome Brunet <jbrunet@baylibre.com>");
  308. MODULE_LICENSE("GPL v2");