axg-toddr.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. // SPDX-License-Identifier: (GPL-2.0 OR MIT)
  2. //
  3. // Copyright (c) 2018 BayLibre, SAS.
  4. // Author: Jerome Brunet <jbrunet@baylibre.com>
  5. /* This driver implements the frontend capture DAI of AXG based SoCs */
  6. #include <linux/clk.h>
  7. #include <linux/regmap.h>
  8. #include <linux/module.h>
  9. #include <linux/of_platform.h>
  10. #include <sound/pcm_params.h>
  11. #include <sound/soc.h>
  12. #include <sound/soc-dai.h>
  13. #include "axg-fifo.h"
  14. #define CTRL0_TODDR_SEL_RESAMPLE BIT(30)
  15. #define CTRL0_TODDR_EXT_SIGNED BIT(29)
  16. #define CTRL0_TODDR_PP_MODE BIT(28)
  17. #define CTRL0_TODDR_TYPE_MASK GENMASK(15, 13)
  18. #define CTRL0_TODDR_TYPE(x) ((x) << 13)
  19. #define CTRL0_TODDR_MSB_POS_MASK GENMASK(12, 8)
  20. #define CTRL0_TODDR_MSB_POS(x) ((x) << 8)
  21. #define CTRL0_TODDR_LSB_POS_MASK GENMASK(7, 3)
  22. #define CTRL0_TODDR_LSB_POS(x) ((x) << 3)
  23. static int axg_toddr_pcm_new(struct snd_soc_pcm_runtime *rtd,
  24. struct snd_soc_dai *dai)
  25. {
  26. return axg_fifo_pcm_new(rtd, SNDRV_PCM_STREAM_CAPTURE);
  27. }
  28. static int axg_toddr_dai_hw_params(struct snd_pcm_substream *substream,
  29. struct snd_pcm_hw_params *params,
  30. struct snd_soc_dai *dai)
  31. {
  32. struct axg_fifo *fifo = snd_soc_dai_get_drvdata(dai);
  33. unsigned int type, width, msb = 31;
  34. /*
  35. * NOTE:
  36. * Almost all backend will place the MSB at bit 31, except SPDIF Input
  37. * which will put it at index 28. When adding support for the SPDIF
  38. * Input, we'll need to find which type of backend we are connected to.
  39. */
  40. switch (params_physical_width(params)) {
  41. case 8:
  42. type = 0; /* 8 samples of 8 bits */
  43. break;
  44. case 16:
  45. type = 2; /* 4 samples of 16 bits - right justified */
  46. break;
  47. case 32:
  48. type = 4; /* 2 samples of 32 bits - right justified */
  49. break;
  50. default:
  51. return -EINVAL;
  52. }
  53. width = params_width(params);
  54. regmap_update_bits(fifo->map, FIFO_CTRL0,
  55. CTRL0_TODDR_TYPE_MASK |
  56. CTRL0_TODDR_MSB_POS_MASK |
  57. CTRL0_TODDR_LSB_POS_MASK,
  58. CTRL0_TODDR_TYPE(type) |
  59. CTRL0_TODDR_MSB_POS(msb) |
  60. CTRL0_TODDR_LSB_POS(msb - (width - 1)));
  61. return 0;
  62. }
  63. static int axg_toddr_dai_startup(struct snd_pcm_substream *substream,
  64. struct snd_soc_dai *dai)
  65. {
  66. struct axg_fifo *fifo = snd_soc_dai_get_drvdata(dai);
  67. unsigned int fifo_threshold;
  68. int ret;
  69. /* Enable pclk to access registers and clock the fifo ip */
  70. ret = clk_prepare_enable(fifo->pclk);
  71. if (ret)
  72. return ret;
  73. /* Select orginal data - resampling not supported ATM */
  74. regmap_update_bits(fifo->map, FIFO_CTRL0, CTRL0_TODDR_SEL_RESAMPLE, 0);
  75. /* Only signed format are supported ATM */
  76. regmap_update_bits(fifo->map, FIFO_CTRL0, CTRL0_TODDR_EXT_SIGNED,
  77. CTRL0_TODDR_EXT_SIGNED);
  78. /* Apply single buffer mode to the interface */
  79. regmap_update_bits(fifo->map, FIFO_CTRL0, CTRL0_TODDR_PP_MODE, 0);
  80. /* TODDR does not have a configurable fifo depth */
  81. fifo_threshold = AXG_FIFO_MIN_CNT - 1;
  82. regmap_update_bits(fifo->map, FIFO_CTRL1, CTRL1_THRESHOLD_MASK,
  83. CTRL1_THRESHOLD(fifo_threshold));
  84. return 0;
  85. }
  86. static void axg_toddr_dai_shutdown(struct snd_pcm_substream *substream,
  87. struct snd_soc_dai *dai)
  88. {
  89. struct axg_fifo *fifo = snd_soc_dai_get_drvdata(dai);
  90. clk_disable_unprepare(fifo->pclk);
  91. }
  92. static const struct snd_soc_dai_ops axg_toddr_ops = {
  93. .hw_params = axg_toddr_dai_hw_params,
  94. .startup = axg_toddr_dai_startup,
  95. .shutdown = axg_toddr_dai_shutdown,
  96. };
  97. static struct snd_soc_dai_driver axg_toddr_dai_drv = {
  98. .name = "TODDR",
  99. .capture = {
  100. .stream_name = "Capture",
  101. .channels_min = 1,
  102. .channels_max = AXG_FIFO_CH_MAX,
  103. .rates = AXG_FIFO_RATES,
  104. .formats = AXG_FIFO_FORMATS,
  105. },
  106. .ops = &axg_toddr_ops,
  107. .pcm_new = axg_toddr_pcm_new,
  108. };
  109. static const char * const axg_toddr_sel_texts[] = {
  110. "IN 0", "IN 1", "IN 2", "IN 3", "IN 4", "IN 6"
  111. };
  112. static const unsigned int axg_toddr_sel_values[] = {
  113. 0, 1, 2, 3, 4, 6
  114. };
  115. static SOC_VALUE_ENUM_SINGLE_DECL(axg_toddr_sel_enum, FIFO_CTRL0,
  116. CTRL0_SEL_SHIFT, CTRL0_SEL_MASK,
  117. axg_toddr_sel_texts, axg_toddr_sel_values);
  118. static const struct snd_kcontrol_new axg_toddr_in_mux =
  119. SOC_DAPM_ENUM("Input Source", axg_toddr_sel_enum);
  120. static const struct snd_soc_dapm_widget axg_toddr_dapm_widgets[] = {
  121. SND_SOC_DAPM_MUX("SRC SEL", SND_SOC_NOPM, 0, 0, &axg_toddr_in_mux),
  122. SND_SOC_DAPM_AIF_IN("IN 0", NULL, 0, SND_SOC_NOPM, 0, 0),
  123. SND_SOC_DAPM_AIF_IN("IN 1", NULL, 0, SND_SOC_NOPM, 0, 0),
  124. SND_SOC_DAPM_AIF_IN("IN 2", NULL, 0, SND_SOC_NOPM, 0, 0),
  125. SND_SOC_DAPM_AIF_IN("IN 3", NULL, 0, SND_SOC_NOPM, 0, 0),
  126. SND_SOC_DAPM_AIF_IN("IN 4", NULL, 0, SND_SOC_NOPM, 0, 0),
  127. SND_SOC_DAPM_AIF_IN("IN 6", NULL, 0, SND_SOC_NOPM, 0, 0),
  128. };
  129. static const struct snd_soc_dapm_route axg_toddr_dapm_routes[] = {
  130. { "Capture", NULL, "SRC SEL" },
  131. { "SRC SEL", "IN 0", "IN 0" },
  132. { "SRC SEL", "IN 1", "IN 1" },
  133. { "SRC SEL", "IN 2", "IN 2" },
  134. { "SRC SEL", "IN 3", "IN 3" },
  135. { "SRC SEL", "IN 4", "IN 4" },
  136. { "SRC SEL", "IN 6", "IN 6" },
  137. };
  138. static const struct snd_soc_component_driver axg_toddr_component_drv = {
  139. .dapm_widgets = axg_toddr_dapm_widgets,
  140. .num_dapm_widgets = ARRAY_SIZE(axg_toddr_dapm_widgets),
  141. .dapm_routes = axg_toddr_dapm_routes,
  142. .num_dapm_routes = ARRAY_SIZE(axg_toddr_dapm_routes),
  143. .ops = &axg_fifo_pcm_ops
  144. };
  145. static const struct axg_fifo_match_data axg_toddr_match_data = {
  146. .component_drv = &axg_toddr_component_drv,
  147. .dai_drv = &axg_toddr_dai_drv
  148. };
  149. static const struct of_device_id axg_toddr_of_match[] = {
  150. {
  151. .compatible = "amlogic,axg-toddr",
  152. .data = &axg_toddr_match_data,
  153. }, {}
  154. };
  155. MODULE_DEVICE_TABLE(of, axg_toddr_of_match);
  156. static struct platform_driver axg_toddr_pdrv = {
  157. .probe = axg_fifo_probe,
  158. .driver = {
  159. .name = "axg-toddr",
  160. .of_match_table = axg_toddr_of_match,
  161. },
  162. };
  163. module_platform_driver(axg_toddr_pdrv);
  164. MODULE_DESCRIPTION("Amlogic AXG capture fifo driver");
  165. MODULE_AUTHOR("Jerome Brunet <jbrunet@baylibre.com>");
  166. MODULE_LICENSE("GPL v2");