aiu-acodec-ctrl.c 6.4 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 = snd_soc_dapm_kcontrol_to_component(kcontrol);
  29. struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_to_dapm(kcontrol);
  30. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  31. unsigned int mux, changed;
  32. mux = snd_soc_enum_item_to_val(e, ucontrol->value.enumerated.item[0]);
  33. changed = snd_soc_component_test_bits(component, e->reg,
  34. CTRL_DIN_LRCLK_SRC,
  35. FIELD_PREP(CTRL_DIN_LRCLK_SRC,
  36. mux));
  37. if (!changed)
  38. return 0;
  39. /* Force disconnect of the mux while updating */
  40. snd_soc_dapm_mux_update_power(dapm, kcontrol, 0, NULL, NULL);
  41. snd_soc_component_update_bits(component, e->reg,
  42. CTRL_DIN_LRCLK_SRC |
  43. CTRL_BCLK_MCLK_SRC,
  44. FIELD_PREP(CTRL_DIN_LRCLK_SRC, mux) |
  45. FIELD_PREP(CTRL_BCLK_MCLK_SRC, mux));
  46. snd_soc_dapm_mux_update_power(dapm, kcontrol, mux, e, NULL);
  47. return 1;
  48. }
  49. static SOC_ENUM_SINGLE_DECL(aiu_acodec_ctrl_mux_enum, AIU_ACODEC_CTRL,
  50. CTRL_DIN_LRCLK_SRC_SHIFT,
  51. aiu_acodec_ctrl_mux_texts);
  52. static const struct snd_kcontrol_new aiu_acodec_ctrl_mux =
  53. SOC_DAPM_ENUM_EXT("ACodec Source", aiu_acodec_ctrl_mux_enum,
  54. snd_soc_dapm_get_enum_double,
  55. aiu_acodec_ctrl_mux_put_enum);
  56. static const struct snd_kcontrol_new aiu_acodec_ctrl_out_enable =
  57. SOC_DAPM_SINGLE_AUTODISABLE("Switch", AIU_ACODEC_CTRL,
  58. CTRL_DIN_EN, 1, 0);
  59. static const struct snd_soc_dapm_widget aiu_acodec_ctrl_widgets[] = {
  60. SND_SOC_DAPM_MUX("ACODEC SRC", SND_SOC_NOPM, 0, 0,
  61. &aiu_acodec_ctrl_mux),
  62. SND_SOC_DAPM_SWITCH("ACODEC OUT EN", SND_SOC_NOPM, 0, 0,
  63. &aiu_acodec_ctrl_out_enable),
  64. };
  65. static int aiu_acodec_ctrl_input_hw_params(struct snd_pcm_substream *substream,
  66. struct snd_pcm_hw_params *params,
  67. struct snd_soc_dai *dai)
  68. {
  69. struct meson_codec_glue_input *data;
  70. int ret;
  71. ret = meson_codec_glue_input_hw_params(substream, params, dai);
  72. if (ret)
  73. return ret;
  74. /* The glue will provide 1 lane out of the 4 to the output */
  75. data = meson_codec_glue_input_get_data(dai);
  76. data->params.channels_min = min_t(unsigned int, AIU_ACODEC_OUT_CHMAX,
  77. data->params.channels_min);
  78. data->params.channels_max = min_t(unsigned int, AIU_ACODEC_OUT_CHMAX,
  79. data->params.channels_max);
  80. return 0;
  81. }
  82. static const struct snd_soc_dai_ops aiu_acodec_ctrl_input_ops = {
  83. .probe = meson_codec_glue_input_dai_probe,
  84. .remove = meson_codec_glue_input_dai_remove,
  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. }
  109. #define AIU_ACODEC_OUTPUT(xname) { \
  110. .name = "ACODEC CTRL " xname, \
  111. .capture = AIU_ACODEC_STREAM(xname, "Capture", AIU_ACODEC_OUT_CHMAX), \
  112. .ops = &aiu_acodec_ctrl_output_ops, \
  113. }
  114. static struct snd_soc_dai_driver aiu_acodec_ctrl_dai_drv[] = {
  115. [CTRL_I2S] = AIU_ACODEC_INPUT("ACODEC I2S IN"),
  116. [CTRL_PCM] = AIU_ACODEC_INPUT("ACODEC PCM IN"),
  117. [CTRL_OUT] = AIU_ACODEC_OUTPUT("ACODEC OUT"),
  118. };
  119. static const struct snd_soc_dapm_route aiu_acodec_ctrl_routes[] = {
  120. { "ACODEC SRC", "I2S", "ACODEC I2S IN Playback" },
  121. { "ACODEC SRC", "PCM", "ACODEC PCM IN Playback" },
  122. { "ACODEC OUT EN", "Switch", "ACODEC SRC" },
  123. { "ACODEC OUT Capture", NULL, "ACODEC OUT EN" },
  124. };
  125. static const struct snd_kcontrol_new aiu_acodec_ctrl_controls[] = {
  126. SOC_SINGLE("ACODEC I2S Lane Select", AIU_ACODEC_CTRL,
  127. CTRL_I2S_OUT_LANE_SRC, 3, 0),
  128. };
  129. static int aiu_acodec_of_xlate_dai_name(struct snd_soc_component *component,
  130. const struct of_phandle_args *args,
  131. const char **dai_name)
  132. {
  133. return aiu_of_xlate_dai_name(component, args, dai_name, AIU_ACODEC);
  134. }
  135. static int aiu_acodec_ctrl_component_probe(struct snd_soc_component *component)
  136. {
  137. /*
  138. * NOTE: Din Skew setting
  139. * According to the documentation, the following update adds one delay
  140. * to the din line. Without this, the output saturates. This happens
  141. * regardless of the link format (i2s or left_j) so it is not clear what
  142. * it actually does but it seems to be required
  143. */
  144. snd_soc_component_update_bits(component, AIU_ACODEC_CTRL,
  145. CTRL_DIN_SKEW,
  146. FIELD_PREP(CTRL_DIN_SKEW, 2));
  147. return 0;
  148. }
  149. static const struct snd_soc_component_driver aiu_acodec_ctrl_component = {
  150. .name = "AIU Internal DAC Codec Control",
  151. .probe = aiu_acodec_ctrl_component_probe,
  152. .controls = aiu_acodec_ctrl_controls,
  153. .num_controls = ARRAY_SIZE(aiu_acodec_ctrl_controls),
  154. .dapm_widgets = aiu_acodec_ctrl_widgets,
  155. .num_dapm_widgets = ARRAY_SIZE(aiu_acodec_ctrl_widgets),
  156. .dapm_routes = aiu_acodec_ctrl_routes,
  157. .num_dapm_routes = ARRAY_SIZE(aiu_acodec_ctrl_routes),
  158. .of_xlate_dai_name = aiu_acodec_of_xlate_dai_name,
  159. .endianness = 1,
  160. #ifdef CONFIG_DEBUG_FS
  161. .debugfs_prefix = "acodec",
  162. #endif
  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. }