aiu-codec-ctrl.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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_CLK_SEL GENMASK(1, 0)
  13. #define CTRL_DATA_SEL_SHIFT 4
  14. #define CTRL_DATA_SEL (0x3 << CTRL_DATA_SEL_SHIFT)
  15. static const char * const aiu_codec_ctrl_mux_texts[] = {
  16. "DISABLED", "PCM", "I2S",
  17. };
  18. static int aiu_codec_ctrl_mux_put_enum(struct snd_kcontrol *kcontrol,
  19. struct snd_ctl_elem_value *ucontrol)
  20. {
  21. struct snd_soc_component *component =
  22. snd_soc_dapm_kcontrol_component(kcontrol);
  23. struct snd_soc_dapm_context *dapm =
  24. snd_soc_dapm_kcontrol_dapm(kcontrol);
  25. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  26. unsigned int mux, changed;
  27. mux = snd_soc_enum_item_to_val(e, ucontrol->value.enumerated.item[0]);
  28. changed = snd_soc_component_test_bits(component, e->reg,
  29. CTRL_DATA_SEL,
  30. FIELD_PREP(CTRL_DATA_SEL, mux));
  31. if (!changed)
  32. return 0;
  33. /* Force disconnect of the mux while updating */
  34. snd_soc_dapm_mux_update_power(dapm, kcontrol, 0, NULL, NULL);
  35. /* Reset the source first */
  36. snd_soc_component_update_bits(component, e->reg,
  37. CTRL_CLK_SEL |
  38. CTRL_DATA_SEL,
  39. FIELD_PREP(CTRL_CLK_SEL, 0) |
  40. FIELD_PREP(CTRL_DATA_SEL, 0));
  41. /* Set the appropriate source */
  42. snd_soc_component_update_bits(component, e->reg,
  43. CTRL_CLK_SEL |
  44. CTRL_DATA_SEL,
  45. FIELD_PREP(CTRL_CLK_SEL, mux) |
  46. FIELD_PREP(CTRL_DATA_SEL, mux));
  47. snd_soc_dapm_mux_update_power(dapm, kcontrol, mux, e, NULL);
  48. return 0;
  49. }
  50. static SOC_ENUM_SINGLE_DECL(aiu_hdmi_ctrl_mux_enum, AIU_HDMI_CLK_DATA_CTRL,
  51. CTRL_DATA_SEL_SHIFT,
  52. aiu_codec_ctrl_mux_texts);
  53. static const struct snd_kcontrol_new aiu_hdmi_ctrl_mux =
  54. SOC_DAPM_ENUM_EXT("HDMI Source", aiu_hdmi_ctrl_mux_enum,
  55. snd_soc_dapm_get_enum_double,
  56. aiu_codec_ctrl_mux_put_enum);
  57. static const struct snd_soc_dapm_widget aiu_hdmi_ctrl_widgets[] = {
  58. SND_SOC_DAPM_MUX("HDMI CTRL SRC", SND_SOC_NOPM, 0, 0,
  59. &aiu_hdmi_ctrl_mux),
  60. };
  61. static const struct snd_soc_dai_ops aiu_codec_ctrl_input_ops = {
  62. .hw_params = meson_codec_glue_input_hw_params,
  63. .set_fmt = meson_codec_glue_input_set_fmt,
  64. };
  65. static const struct snd_soc_dai_ops aiu_codec_ctrl_output_ops = {
  66. .startup = meson_codec_glue_output_startup,
  67. };
  68. #define AIU_CODEC_CTRL_FORMATS \
  69. (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
  70. SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_LE | \
  71. SNDRV_PCM_FMTBIT_S32_LE)
  72. #define AIU_CODEC_CTRL_STREAM(xname, xsuffix) \
  73. { \
  74. .stream_name = xname " " xsuffix, \
  75. .channels_min = 1, \
  76. .channels_max = 8, \
  77. .rate_min = 5512, \
  78. .rate_max = 192000, \
  79. .formats = AIU_CODEC_CTRL_FORMATS, \
  80. }
  81. #define AIU_CODEC_CTRL_INPUT(xname) { \
  82. .name = "CODEC CTRL " xname, \
  83. .playback = AIU_CODEC_CTRL_STREAM(xname, "Playback"), \
  84. .ops = &aiu_codec_ctrl_input_ops, \
  85. .probe = meson_codec_glue_input_dai_probe, \
  86. .remove = meson_codec_glue_input_dai_remove, \
  87. }
  88. #define AIU_CODEC_CTRL_OUTPUT(xname) { \
  89. .name = "CODEC CTRL " xname, \
  90. .capture = AIU_CODEC_CTRL_STREAM(xname, "Capture"), \
  91. .ops = &aiu_codec_ctrl_output_ops, \
  92. }
  93. static struct snd_soc_dai_driver aiu_hdmi_ctrl_dai_drv[] = {
  94. [CTRL_I2S] = AIU_CODEC_CTRL_INPUT("HDMI I2S IN"),
  95. [CTRL_PCM] = AIU_CODEC_CTRL_INPUT("HDMI PCM IN"),
  96. [CTRL_OUT] = AIU_CODEC_CTRL_OUTPUT("HDMI OUT"),
  97. };
  98. static const struct snd_soc_dapm_route aiu_hdmi_ctrl_routes[] = {
  99. { "HDMI CTRL SRC", "I2S", "HDMI I2S IN Playback" },
  100. { "HDMI CTRL SRC", "PCM", "HDMI PCM IN Playback" },
  101. { "HDMI OUT Capture", NULL, "HDMI CTRL SRC" },
  102. };
  103. static int aiu_hdmi_of_xlate_dai_name(struct snd_soc_component *component,
  104. struct of_phandle_args *args,
  105. const char **dai_name)
  106. {
  107. return aiu_of_xlate_dai_name(component, args, dai_name, AIU_HDMI);
  108. }
  109. static const struct snd_soc_component_driver aiu_hdmi_ctrl_component = {
  110. .name = "AIU HDMI Codec Control",
  111. .dapm_widgets = aiu_hdmi_ctrl_widgets,
  112. .num_dapm_widgets = ARRAY_SIZE(aiu_hdmi_ctrl_widgets),
  113. .dapm_routes = aiu_hdmi_ctrl_routes,
  114. .num_dapm_routes = ARRAY_SIZE(aiu_hdmi_ctrl_routes),
  115. .of_xlate_dai_name = aiu_hdmi_of_xlate_dai_name,
  116. .endianness = 1,
  117. .non_legacy_dai_naming = 1,
  118. };
  119. int aiu_hdmi_ctrl_register_component(struct device *dev)
  120. {
  121. return snd_soc_register_component(dev, &aiu_hdmi_ctrl_component,
  122. aiu_hdmi_ctrl_dai_drv,
  123. ARRAY_SIZE(aiu_hdmi_ctrl_dai_drv));
  124. }