axg-card.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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 = snd_soc_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(snd_soc_rtd_to_cpu(rtd, 0), be->tx_mask, be->rx_mask,
  65. be->slots, be->slot_width);
  66. if (ret) {
  67. dev_err(snd_soc_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(snd_soc_rtd_to_cpu(rtd, 0), NULL, be->tx_mask,
  80. be->slots, be->slot_width);
  81. if (ret) {
  82. dev_err(snd_soc_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;
  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. pad = &card->dai_link[*index];
  100. lb = &card->dai_link[*index + 1];
  101. lb->name = devm_kasprintf(card->dev, GFP_KERNEL, "%s-lb", pad->name);
  102. if (!lb->name)
  103. return -ENOMEM;
  104. dlc = devm_kzalloc(card->dev, sizeof(*dlc), GFP_KERNEL);
  105. if (!dlc)
  106. return -ENOMEM;
  107. lb->cpus = dlc;
  108. lb->codecs = &snd_soc_dummy_dlc;
  109. lb->num_cpus = 1;
  110. lb->num_codecs = 1;
  111. lb->stream_name = lb->name;
  112. lb->cpus->of_node = pad->cpus->of_node;
  113. lb->cpus->dai_name = "TDM Loopback";
  114. lb->capture_only = 1;
  115. lb->no_pcm = 1;
  116. lb->ops = &axg_card_tdm_be_ops;
  117. lb->init = axg_card_tdm_dai_lb_init;
  118. /* Provide the same link data to the loopback */
  119. priv->link_data[*index + 1] = priv->link_data[*index];
  120. /*
  121. * axg_card_clean_references() will iterate over this link,
  122. * make sure the node count is balanced
  123. */
  124. of_node_get(lb->cpus->of_node);
  125. /* Let add_links continue where it should */
  126. *index += 1;
  127. return 0;
  128. }
  129. static int axg_card_parse_cpu_tdm_slots(struct snd_soc_card *card,
  130. struct snd_soc_dai_link *link,
  131. struct device_node *node,
  132. struct axg_dai_link_tdm_data *be)
  133. {
  134. char propname[32];
  135. u32 tx, rx;
  136. int i;
  137. be->tx_mask = devm_kcalloc(card->dev, AXG_TDM_NUM_LANES,
  138. sizeof(*be->tx_mask), GFP_KERNEL);
  139. be->rx_mask = devm_kcalloc(card->dev, AXG_TDM_NUM_LANES,
  140. sizeof(*be->rx_mask), GFP_KERNEL);
  141. if (!be->tx_mask || !be->rx_mask)
  142. return -ENOMEM;
  143. for (i = 0, tx = 0; i < AXG_TDM_NUM_LANES; i++) {
  144. snprintf(propname, 32, "dai-tdm-slot-tx-mask-%d", i);
  145. snd_soc_of_get_slot_mask(node, propname, &be->tx_mask[i]);
  146. tx = max(tx, be->tx_mask[i]);
  147. }
  148. /* Disable playback is the interface has no tx slots */
  149. if (!tx)
  150. link->capture_only = 1;
  151. for (i = 0, rx = 0; i < AXG_TDM_NUM_LANES; i++) {
  152. snprintf(propname, 32, "dai-tdm-slot-rx-mask-%d", i);
  153. snd_soc_of_get_slot_mask(node, propname, &be->rx_mask[i]);
  154. rx = max(rx, be->rx_mask[i]);
  155. }
  156. /* Disable capture is the interface has no rx slots */
  157. if (!rx)
  158. link->playback_only = 1;
  159. /* ... but the interface should at least have one direction */
  160. if (!tx && !rx) {
  161. dev_err(card->dev, "tdm link has no cpu slots\n");
  162. return -EINVAL;
  163. }
  164. of_property_read_u32(node, "dai-tdm-slot-num", &be->slots);
  165. if (!be->slots) {
  166. /*
  167. * If the slot number is not provided, set it such as it
  168. * accommodates the largest mask
  169. */
  170. be->slots = fls(max(tx, rx));
  171. } else if (be->slots < fls(max(tx, rx)) || be->slots > 32) {
  172. /*
  173. * Error if the slots can't accommodate the largest mask or
  174. * if it is just too big
  175. */
  176. dev_err(card->dev, "bad slot number\n");
  177. return -EINVAL;
  178. }
  179. of_property_read_u32(node, "dai-tdm-slot-width", &be->slot_width);
  180. return 0;
  181. }
  182. static int axg_card_parse_codecs_masks(struct snd_soc_card *card,
  183. struct snd_soc_dai_link *link,
  184. struct device_node *node,
  185. struct axg_dai_link_tdm_data *be)
  186. {
  187. struct axg_dai_link_tdm_mask *codec_mask;
  188. struct device_node *np;
  189. codec_mask = devm_kcalloc(card->dev, link->num_codecs,
  190. sizeof(*codec_mask), GFP_KERNEL);
  191. if (!codec_mask)
  192. return -ENOMEM;
  193. be->codec_masks = codec_mask;
  194. for_each_child_of_node(node, np) {
  195. snd_soc_of_get_slot_mask(np, "dai-tdm-slot-rx-mask",
  196. &codec_mask->rx);
  197. snd_soc_of_get_slot_mask(np, "dai-tdm-slot-tx-mask",
  198. &codec_mask->tx);
  199. codec_mask++;
  200. }
  201. return 0;
  202. }
  203. static int axg_card_parse_tdm(struct snd_soc_card *card,
  204. struct device_node *node,
  205. int *index)
  206. {
  207. struct meson_card *priv = snd_soc_card_get_drvdata(card);
  208. struct snd_soc_dai_link *link = &card->dai_link[*index];
  209. struct axg_dai_link_tdm_data *be;
  210. int ret;
  211. /* Allocate tdm link parameters */
  212. be = devm_kzalloc(card->dev, sizeof(*be), GFP_KERNEL);
  213. if (!be)
  214. return -ENOMEM;
  215. priv->link_data[*index] = be;
  216. /* Setup tdm link */
  217. link->ops = &axg_card_tdm_be_ops;
  218. link->init = axg_card_tdm_dai_init;
  219. link->dai_fmt = meson_card_parse_daifmt(node, link->cpus->of_node);
  220. of_property_read_u32(node, "mclk-fs", &be->mclk_fs);
  221. ret = axg_card_parse_cpu_tdm_slots(card, link, node, be);
  222. if (ret) {
  223. dev_err(card->dev, "error parsing tdm link slots\n");
  224. return ret;
  225. }
  226. ret = axg_card_parse_codecs_masks(card, link, node, be);
  227. if (ret)
  228. return ret;
  229. /* Add loopback if the pad dai has playback */
  230. if (!link->capture_only) {
  231. ret = axg_card_add_tdm_loopback(card, index);
  232. if (ret)
  233. return ret;
  234. }
  235. return 0;
  236. }
  237. static int axg_card_cpu_is_capture_fe(struct device_node *np)
  238. {
  239. return of_device_is_compatible(np, DT_PREFIX "axg-toddr");
  240. }
  241. static int axg_card_cpu_is_playback_fe(struct device_node *np)
  242. {
  243. return of_device_is_compatible(np, DT_PREFIX "axg-frddr");
  244. }
  245. static int axg_card_cpu_is_tdm_iface(struct device_node *np)
  246. {
  247. return of_device_is_compatible(np, DT_PREFIX "axg-tdm-iface");
  248. }
  249. static int axg_card_cpu_is_codec(struct device_node *np)
  250. {
  251. return of_device_is_compatible(np, DT_PREFIX "g12a-tohdmitx") ||
  252. of_device_is_compatible(np, DT_PREFIX "g12a-toacodec");
  253. }
  254. static int axg_card_add_link(struct snd_soc_card *card, struct device_node *np,
  255. int *index)
  256. {
  257. struct snd_soc_dai_link *dai_link = &card->dai_link[*index];
  258. struct snd_soc_dai_link_component *cpu;
  259. int ret;
  260. cpu = devm_kzalloc(card->dev, sizeof(*cpu), GFP_KERNEL);
  261. if (!cpu)
  262. return -ENOMEM;
  263. dai_link->cpus = cpu;
  264. dai_link->num_cpus = 1;
  265. dai_link->nonatomic = true;
  266. ret = meson_card_parse_dai(card, np, dai_link->cpus);
  267. if (ret)
  268. return ret;
  269. if (axg_card_cpu_is_playback_fe(dai_link->cpus->of_node))
  270. return meson_card_set_fe_link(card, dai_link, np, true);
  271. else if (axg_card_cpu_is_capture_fe(dai_link->cpus->of_node))
  272. return meson_card_set_fe_link(card, dai_link, np, false);
  273. ret = meson_card_set_be_link(card, dai_link, np);
  274. if (ret)
  275. return ret;
  276. if (axg_card_cpu_is_codec(dai_link->cpus->of_node)) {
  277. dai_link->c2c_params = &codec_params;
  278. dai_link->num_c2c_params = 1;
  279. } else {
  280. dai_link->no_pcm = 1;
  281. if (axg_card_cpu_is_tdm_iface(dai_link->cpus->of_node))
  282. ret = axg_card_parse_tdm(card, np, index);
  283. }
  284. return ret;
  285. }
  286. static const struct meson_card_match_data axg_card_match_data = {
  287. .add_link = axg_card_add_link,
  288. };
  289. static const struct of_device_id axg_card_of_match[] = {
  290. {
  291. .compatible = "amlogic,axg-sound-card",
  292. .data = &axg_card_match_data,
  293. }, {}
  294. };
  295. MODULE_DEVICE_TABLE(of, axg_card_of_match);
  296. static struct platform_driver axg_card_pdrv = {
  297. .probe = meson_card_probe,
  298. .remove = meson_card_remove,
  299. .driver = {
  300. .name = "axg-sound-card",
  301. .of_match_table = axg_card_of_match,
  302. },
  303. };
  304. module_platform_driver(axg_card_pdrv);
  305. MODULE_DESCRIPTION("Amlogic AXG ALSA machine driver");
  306. MODULE_AUTHOR("Jerome Brunet <jbrunet@baylibre.com>");
  307. MODULE_LICENSE("GPL v2");