aiu-acodec-ctrl.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. // SPDX-License-Identifier: GPL-2.0
  2. //
  3. // Copyright (c) 2020 BayLibre, SAS.
  4. // Author: Jerome Brunet <jbrunet@baylibre.com>
  5. #include <linux/bitfield.h>
  6. #include <sound/pcm_params.h>
  7. #include <sound/soc.h>
  8. #include <sound/soc-dai.h>
  9. #include <dt-bindings/sound/meson-aiu.h>
  10. #include "aiu.h"
  11. #include "meson-codec-glue.h"
  12. #define CTRL_DIN_EN 15
  13. #define CTRL_CLK_INV BIT(14)
  14. #define CTRL_LRCLK_INV BIT(13)
  15. #define CTRL_I2S_IN_BCLK_SRC BIT(11)
  16. #define CTRL_DIN_LRCLK_SRC_SHIFT 6
  17. #define CTRL_DIN_LRCLK_SRC (0x3 << CTRL_DIN_LRCLK_SRC_SHIFT)
  18. #define CTRL_BCLK_MCLK_SRC GENMASK(5, 4)
  19. #define CTRL_DIN_SKEW GENMASK(3, 2)
  20. #define CTRL_I2S_OUT_LANE_SRC 0
  21. #define AIU_ACODEC_OUT_CHMAX 2
  22. static const char * const aiu_acodec_ctrl_mux_texts[] = {
  23. "DISABLED", "I2S", "PCM",
  24. };
  25. static int aiu_acodec_ctrl_mux_put_enum(struct snd_kcontrol *kcontrol,
  26. struct snd_ctl_elem_value *ucontrol)
  27. {
  28. struct snd_soc_component *component =
  29. snd_soc_dapm_kcontrol_component(kcontrol);
  30. struct snd_soc_dapm_context *dapm =
  31. snd_soc_dapm_kcontrol_dapm(kcontrol);
  32. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  33. unsigned int mux, changed;
  34. mux = snd_soc_enum_item_to_val(e, ucontrol->value.enumerated.item[0]);
  35. changed = snd_soc_component_test_bits(component, e->reg,
  36. CTRL_DIN_LRCLK_SRC,
  37. FIELD_PREP(CTRL_DIN_LRCLK_SRC,
  38. mux));
  39. if (!changed)
  40. return 0;
  41. /* Force disconnect of the mux while updating */
  42. snd_soc_dapm_mux_update_power(dapm, kcontrol, 0, NULL, NULL);
  43. snd_soc_component_update_bits(component, e->reg,
  44. CTRL_DIN_LRCLK_SRC |
  45. CTRL_BCLK_MCLK_SRC,
  46. FIELD_PREP(CTRL_DIN_LRCLK_SRC, mux) |
  47. FIELD_PREP(CTRL_BCLK_MCLK_SRC, mux));
  48. snd_soc_dapm_mux_update_power(dapm, kcontrol, mux, e, NULL);
  49. return 0;
  50. }
  51. static SOC_ENUM_SINGLE_DECL(aiu_acodec_ctrl_mux_enum, AIU_ACODEC_CTRL,
  52. CTRL_DIN_LRCLK_SRC_SHIFT,
  53. aiu_acodec_ctrl_mux_texts);
  54. static const struct snd_kcontrol_new aiu_acodec_ctrl_mux =
  55. SOC_DAPM_ENUM_EXT("ACodec Source", aiu_acodec_ctrl_mux_enum,
  56. snd_soc_dapm_get_enum_double,
  57. aiu_acodec_ctrl_mux_put_enum);
  58. static const struct snd_kcontrol_new aiu_acodec_ctrl_out_enable =
  59. SOC_DAPM_SINGLE_AUTODISABLE("Switch", AIU_ACODEC_CTRL,
  60. CTRL_DIN_EN, 1, 0);
  61. static const struct snd_soc_dapm_widget aiu_acodec_ctrl_widgets[] = {
  62. SND_SOC_DAPM_MUX("ACODEC SRC", SND_SOC_NOPM, 0, 0,
  63. &aiu_acodec_ctrl_mux),
  64. SND_SOC_DAPM_SWITCH("ACODEC OUT EN", SND_SOC_NOPM, 0, 0,
  65. &aiu_acodec_ctrl_out_enable),
  66. };
  67. static int aiu_acodec_ctrl_input_hw_params(struct snd_pcm_substream *substream,
  68. struct snd_pcm_hw_params *params,
  69. struct snd_soc_dai *dai)
  70. {
  71. struct meson_codec_glue_input *data;
  72. int ret;
  73. ret = meson_codec_glue_input_hw_params(substream, params, dai);
  74. if (ret)
  75. return ret;
  76. /* The glue will provide 1 lane out of the 4 to the output */
  77. data = meson_codec_glue_input_get_data(dai);
  78. data->params.channels_min = min_t(unsigned int, AIU_ACODEC_OUT_CHMAX,
  79. data->params.channels_min);
  80. data->params.channels_max = min_t(unsigned int, AIU_ACODEC_OUT_CHMAX,
  81. data->params.channels_max);
  82. return 0;
  83. }
  84. static const struct snd_soc_dai_ops aiu_acodec_ctrl_input_ops = {
  85. .hw_params = aiu_acodec_ctrl_input_hw_params,
  86. .set_fmt = meson_codec_glue_input_set_fmt,
  87. };
  88. static const struct snd_soc_dai_ops aiu_acodec_ctrl_output_ops = {
  89. .startup = meson_codec_glue_output_startup,
  90. };
  91. #define AIU_ACODEC_CTRL_FORMATS \
  92. (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
  93. SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_LE | \
  94. SNDRV_PCM_FMTBIT_S32_LE)
  95. #define AIU_ACODEC_STREAM(xname, xsuffix, xchmax) \
  96. { \
  97. .stream_name = xname " " xsuffix, \
  98. .channels_min = 1, \
  99. .channels_max = (xchmax), \
  100. .rate_min = 5512, \
  101. .rate_max = 192000, \
  102. .formats = AIU_ACODEC_CTRL_FORMATS, \
  103. }
  104. #define AIU_ACODEC_INPUT(xname) { \
  105. .name = "ACODEC CTRL " xname, \
  106. .playback = AIU_ACODEC_STREAM(xname, "Playback", 8), \
  107. .ops = &aiu_acodec_ctrl_input_ops, \
  108. .probe = meson_codec_glue_input_dai_probe, \
  109. .remove = meson_codec_glue_input_dai_remove, \
  110. }
  111. #define AIU_ACODEC_OUTPUT(xname) { \
  112. .name = "ACODEC CTRL " xname, \
  113. .capture = AIU_ACODEC_STREAM(xname, "Capture", AIU_ACODEC_OUT_CHMAX), \
  114. .ops = &aiu_acodec_ctrl_output_ops, \
  115. }
  116. static struct snd_soc_dai_driver aiu_acodec_ctrl_dai_drv[] = {
  117. [CTRL_I2S] = AIU_ACODEC_INPUT("ACODEC I2S IN"),
  118. [CTRL_PCM] = AIU_ACODEC_INPUT("ACODEC PCM IN"),
  119. [CTRL_OUT] = AIU_ACODEC_OUTPUT("ACODEC OUT"),
  120. };
  121. static const struct snd_soc_dapm_route aiu_acodec_ctrl_routes[] = {
  122. { "ACODEC SRC", "I2S", "ACODEC I2S IN Playback" },
  123. { "ACODEC SRC", "PCM", "ACODEC PCM IN Playback" },
  124. { "ACODEC OUT EN", "Switch", "ACODEC SRC" },
  125. { "ACODEC OUT Capture", NULL, "ACODEC OUT EN" },
  126. };
  127. static const struct snd_kcontrol_new aiu_acodec_ctrl_controls[] = {
  128. SOC_SINGLE("ACODEC I2S Lane Select", AIU_ACODEC_CTRL,
  129. CTRL_I2S_OUT_LANE_SRC, 3, 0),
  130. };
  131. static int aiu_acodec_of_xlate_dai_name(struct snd_soc_component *component,
  132. struct of_phandle_args *args,
  133. const char **dai_name)
  134. {
  135. return aiu_of_xlate_dai_name(component, args, dai_name, AIU_ACODEC);
  136. }
  137. static int aiu_acodec_ctrl_component_probe(struct snd_soc_component *component)
  138. {
  139. /*
  140. * NOTE: Din Skew setting
  141. * According to the documentation, the following update adds one delay
  142. * to the din line. Without this, the output saturates. This happens
  143. * regardless of the link format (i2s or left_j) so it is not clear what
  144. * it actually does but it seems to be required
  145. */
  146. snd_soc_component_update_bits(component, AIU_ACODEC_CTRL,
  147. CTRL_DIN_SKEW,
  148. FIELD_PREP(CTRL_DIN_SKEW, 2));
  149. return 0;
  150. }
  151. static const struct snd_soc_component_driver aiu_acodec_ctrl_component = {
  152. .name = "AIU Internal DAC Codec Control",
  153. .probe = aiu_acodec_ctrl_component_probe,
  154. .controls = aiu_acodec_ctrl_controls,
  155. .num_controls = ARRAY_SIZE(aiu_acodec_ctrl_controls),
  156. .dapm_widgets = aiu_acodec_ctrl_widgets,
  157. .num_dapm_widgets = ARRAY_SIZE(aiu_acodec_ctrl_widgets),
  158. .dapm_routes = aiu_acodec_ctrl_routes,
  159. .num_dapm_routes = ARRAY_SIZE(aiu_acodec_ctrl_routes),
  160. .of_xlate_dai_name = aiu_acodec_of_xlate_dai_name,
  161. .endianness = 1,
  162. .non_legacy_dai_naming = 1,
  163. };
  164. int aiu_acodec_ctrl_register_component(struct device *dev)
  165. {
  166. return snd_soc_register_component(dev, &aiu_acodec_ctrl_component,
  167. aiu_acodec_ctrl_dai_drv,
  168. ARRAY_SIZE(aiu_acodec_ctrl_dai_drv));
  169. }