gx-card.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // SPDX-License-Identifier: (GPL-2.0 OR MIT)
  2. //
  3. // Copyright (c) 2020 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 "meson-card.h"
  10. struct gx_dai_link_i2s_data {
  11. unsigned int mclk_fs;
  12. };
  13. /*
  14. * Base params for the codec to codec links
  15. * Those will be over-written by the CPU side of the link
  16. */
  17. static const struct snd_soc_pcm_stream codec_params = {
  18. .formats = SNDRV_PCM_FMTBIT_S24_LE,
  19. .rate_min = 5525,
  20. .rate_max = 192000,
  21. .channels_min = 1,
  22. .channels_max = 8,
  23. };
  24. static int gx_card_i2s_be_hw_params(struct snd_pcm_substream *substream,
  25. struct snd_pcm_hw_params *params)
  26. {
  27. struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
  28. struct meson_card *priv = snd_soc_card_get_drvdata(rtd->card);
  29. struct gx_dai_link_i2s_data *be =
  30. (struct gx_dai_link_i2s_data *)priv->link_data[rtd->num];
  31. return meson_card_i2s_set_sysclk(substream, params, be->mclk_fs);
  32. }
  33. static const struct snd_soc_ops gx_card_i2s_be_ops = {
  34. .hw_params = gx_card_i2s_be_hw_params,
  35. };
  36. static int gx_card_parse_i2s(struct snd_soc_card *card,
  37. struct device_node *node,
  38. int *index)
  39. {
  40. struct meson_card *priv = snd_soc_card_get_drvdata(card);
  41. struct snd_soc_dai_link *link = &card->dai_link[*index];
  42. struct gx_dai_link_i2s_data *be;
  43. /* Allocate i2s link parameters */
  44. be = devm_kzalloc(card->dev, sizeof(*be), GFP_KERNEL);
  45. if (!be)
  46. return -ENOMEM;
  47. priv->link_data[*index] = be;
  48. /* Setup i2s link */
  49. link->ops = &gx_card_i2s_be_ops;
  50. link->dai_fmt = meson_card_parse_daifmt(node, link->cpus->of_node);
  51. of_property_read_u32(node, "mclk-fs", &be->mclk_fs);
  52. return 0;
  53. }
  54. static int gx_card_cpu_identify(struct snd_soc_dai_link_component *c,
  55. char *match)
  56. {
  57. if (of_device_is_compatible(c->of_node, DT_PREFIX "aiu")) {
  58. if (strstr(c->dai_name, match))
  59. return 1;
  60. }
  61. /* dai not matched */
  62. return 0;
  63. }
  64. static int gx_card_add_link(struct snd_soc_card *card, struct device_node *np,
  65. int *index)
  66. {
  67. struct snd_soc_dai_link *dai_link = &card->dai_link[*index];
  68. struct snd_soc_dai_link_component *cpu;
  69. int ret;
  70. cpu = devm_kzalloc(card->dev, sizeof(*cpu), GFP_KERNEL);
  71. if (!cpu)
  72. return -ENOMEM;
  73. dai_link->cpus = cpu;
  74. dai_link->num_cpus = 1;
  75. ret = meson_card_parse_dai(card, np, &dai_link->cpus->of_node,
  76. &dai_link->cpus->dai_name);
  77. if (ret)
  78. return ret;
  79. if (gx_card_cpu_identify(dai_link->cpus, "FIFO"))
  80. return meson_card_set_fe_link(card, dai_link, np, true);
  81. ret = meson_card_set_be_link(card, dai_link, np);
  82. if (ret)
  83. return ret;
  84. /* Or apply codec to codec params if necessary */
  85. if (gx_card_cpu_identify(dai_link->cpus, "CODEC CTRL")) {
  86. dai_link->params = &codec_params;
  87. } else {
  88. dai_link->no_pcm = 1;
  89. snd_soc_dai_link_set_capabilities(dai_link);
  90. /* Check if the cpu is the i2s encoder and parse i2s data */
  91. if (gx_card_cpu_identify(dai_link->cpus, "I2S Encoder"))
  92. ret = gx_card_parse_i2s(card, np, index);
  93. }
  94. return ret;
  95. }
  96. static const struct meson_card_match_data gx_card_match_data = {
  97. .add_link = gx_card_add_link,
  98. };
  99. static const struct of_device_id gx_card_of_match[] = {
  100. {
  101. .compatible = "amlogic,gx-sound-card",
  102. .data = &gx_card_match_data,
  103. }, {}
  104. };
  105. MODULE_DEVICE_TABLE(of, gx_card_of_match);
  106. static struct platform_driver gx_card_pdrv = {
  107. .probe = meson_card_probe,
  108. .remove = meson_card_remove,
  109. .driver = {
  110. .name = "gx-sound-card",
  111. .of_match_table = gx_card_of_match,
  112. },
  113. };
  114. module_platform_driver(gx_card_pdrv);
  115. MODULE_DESCRIPTION("Amlogic GX ALSA machine driver");
  116. MODULE_AUTHOR("Jerome Brunet <jbrunet@baylibre.com>");
  117. MODULE_LICENSE("GPL v2");