| b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | // |
| 3 | // Copyright (c) 2019 BayLibre, SAS. |
| 4 | // Author: Jerome Brunet <jbrunet@baylibre.com> |
| 5 | |
| 6 | #include <linux/bitfield.h> |
| 7 | #include <linux/clk.h> |
| 8 | #include <linux/module.h> |
| 9 | #include <sound/pcm_params.h> |
| 10 | #include <linux/regmap.h> |
| 11 | #include <sound/soc.h> |
| 12 | #include <sound/soc-dai.h> |
| 13 | |
| 14 | #include <dt-bindings/sound/meson-g12a-tohdmitx.h> |
| 15 | #include "meson-codec-glue.h" |
| 16 | |
| 17 | #define G12A_TOHDMITX_DRV_NAME "g12a-tohdmitx" |
| 18 | |
| 19 | #define TOHDMITX_CTRL0 0x0 |
| 20 | #define CTRL0_ENABLE_SHIFT 31 |
| 21 | #define CTRL0_I2S_DAT_SEL_SHIFT 12 |
| 22 | #define CTRL0_I2S_DAT_SEL (0x3 << CTRL0_I2S_DAT_SEL_SHIFT) |
| 23 | #define CTRL0_I2S_LRCLK_SEL GENMASK(9, 8) |
| 24 | #define CTRL0_I2S_BLK_CAP_INV BIT(7) |
| 25 | #define CTRL0_I2S_BCLK_O_INV BIT(6) |
| 26 | #define CTRL0_I2S_BCLK_SEL GENMASK(5, 4) |
| 27 | #define CTRL0_SPDIF_CLK_CAP_INV BIT(3) |
| 28 | #define CTRL0_SPDIF_CLK_O_INV BIT(2) |
| 29 | #define CTRL0_SPDIF_SEL_SHIFT 1 |
| 30 | #define CTRL0_SPDIF_SEL (0x1 << CTRL0_SPDIF_SEL_SHIFT) |
| 31 | #define CTRL0_SPDIF_CLK_SEL BIT(0) |
| 32 | |
| 33 | static const char * const g12a_tohdmitx_i2s_mux_texts[] = { |
| 34 | "I2S A", "I2S B", "I2S C", |
| 35 | }; |
| 36 | |
| 37 | static int g12a_tohdmitx_i2s_mux_put_enum(struct snd_kcontrol *kcontrol, |
| 38 | struct snd_ctl_elem_value *ucontrol) |
| 39 | { |
| 40 | struct snd_soc_component *component = |
| 41 | snd_soc_dapm_kcontrol_component(kcontrol); |
| 42 | struct snd_soc_dapm_context *dapm = |
| 43 | snd_soc_dapm_kcontrol_dapm(kcontrol); |
| 44 | struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; |
| 45 | unsigned int mux, changed; |
| 46 | |
| 47 | if (ucontrol->value.enumerated.item[0] >= e->items) |
| 48 | return -EINVAL; |
| 49 | |
| 50 | mux = snd_soc_enum_item_to_val(e, ucontrol->value.enumerated.item[0]); |
| 51 | changed = snd_soc_component_test_bits(component, e->reg, |
| 52 | CTRL0_I2S_DAT_SEL, |
| 53 | FIELD_PREP(CTRL0_I2S_DAT_SEL, |
| 54 | mux)); |
| 55 | |
| 56 | if (!changed) |
| 57 | return 0; |
| 58 | |
| 59 | /* Force disconnect of the mux while updating */ |
| 60 | snd_soc_dapm_mux_update_power(dapm, kcontrol, 0, NULL, NULL); |
| 61 | |
| 62 | snd_soc_component_update_bits(component, e->reg, |
| 63 | CTRL0_I2S_DAT_SEL | |
| 64 | CTRL0_I2S_LRCLK_SEL | |
| 65 | CTRL0_I2S_BCLK_SEL, |
| 66 | FIELD_PREP(CTRL0_I2S_DAT_SEL, mux) | |
| 67 | FIELD_PREP(CTRL0_I2S_LRCLK_SEL, mux) | |
| 68 | FIELD_PREP(CTRL0_I2S_BCLK_SEL, mux)); |
| 69 | |
| 70 | snd_soc_dapm_mux_update_power(dapm, kcontrol, mux, e, NULL); |
| 71 | |
| 72 | return 1; |
| 73 | } |
| 74 | |
| 75 | static SOC_ENUM_SINGLE_DECL(g12a_tohdmitx_i2s_mux_enum, TOHDMITX_CTRL0, |
| 76 | CTRL0_I2S_DAT_SEL_SHIFT, |
| 77 | g12a_tohdmitx_i2s_mux_texts); |
| 78 | |
| 79 | static const struct snd_kcontrol_new g12a_tohdmitx_i2s_mux = |
| 80 | SOC_DAPM_ENUM_EXT("I2S Source", g12a_tohdmitx_i2s_mux_enum, |
| 81 | snd_soc_dapm_get_enum_double, |
| 82 | g12a_tohdmitx_i2s_mux_put_enum); |
| 83 | |
| 84 | static const char * const g12a_tohdmitx_spdif_mux_texts[] = { |
| 85 | "SPDIF A", "SPDIF B", |
| 86 | }; |
| 87 | |
| 88 | static int g12a_tohdmitx_spdif_mux_put_enum(struct snd_kcontrol *kcontrol, |
| 89 | struct snd_ctl_elem_value *ucontrol) |
| 90 | { |
| 91 | struct snd_soc_component *component = |
| 92 | snd_soc_dapm_kcontrol_component(kcontrol); |
| 93 | struct snd_soc_dapm_context *dapm = |
| 94 | snd_soc_dapm_kcontrol_dapm(kcontrol); |
| 95 | struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; |
| 96 | unsigned int mux, changed; |
| 97 | |
| 98 | if (ucontrol->value.enumerated.item[0] >= e->items) |
| 99 | return -EINVAL; |
| 100 | |
| 101 | mux = snd_soc_enum_item_to_val(e, ucontrol->value.enumerated.item[0]); |
| 102 | changed = snd_soc_component_test_bits(component, TOHDMITX_CTRL0, |
| 103 | CTRL0_SPDIF_SEL, |
| 104 | FIELD_PREP(CTRL0_SPDIF_SEL, mux)); |
| 105 | |
| 106 | if (!changed) |
| 107 | return 0; |
| 108 | |
| 109 | /* Force disconnect of the mux while updating */ |
| 110 | snd_soc_dapm_mux_update_power(dapm, kcontrol, 0, NULL, NULL); |
| 111 | |
| 112 | snd_soc_component_update_bits(component, TOHDMITX_CTRL0, |
| 113 | CTRL0_SPDIF_SEL | |
| 114 | CTRL0_SPDIF_CLK_SEL, |
| 115 | FIELD_PREP(CTRL0_SPDIF_SEL, mux) | |
| 116 | FIELD_PREP(CTRL0_SPDIF_CLK_SEL, mux)); |
| 117 | |
| 118 | snd_soc_dapm_mux_update_power(dapm, kcontrol, mux, e, NULL); |
| 119 | |
| 120 | return 1; |
| 121 | } |
| 122 | |
| 123 | static SOC_ENUM_SINGLE_DECL(g12a_tohdmitx_spdif_mux_enum, TOHDMITX_CTRL0, |
| 124 | CTRL0_SPDIF_SEL_SHIFT, |
| 125 | g12a_tohdmitx_spdif_mux_texts); |
| 126 | |
| 127 | static const struct snd_kcontrol_new g12a_tohdmitx_spdif_mux = |
| 128 | SOC_DAPM_ENUM_EXT("SPDIF Source", g12a_tohdmitx_spdif_mux_enum, |
| 129 | snd_soc_dapm_get_enum_double, |
| 130 | g12a_tohdmitx_spdif_mux_put_enum); |
| 131 | |
| 132 | static const struct snd_kcontrol_new g12a_tohdmitx_out_enable = |
| 133 | SOC_DAPM_SINGLE_AUTODISABLE("Switch", TOHDMITX_CTRL0, |
| 134 | CTRL0_ENABLE_SHIFT, 1, 0); |
| 135 | |
| 136 | static const struct snd_soc_dapm_widget g12a_tohdmitx_widgets[] = { |
| 137 | SND_SOC_DAPM_MUX("I2S SRC", SND_SOC_NOPM, 0, 0, |
| 138 | &g12a_tohdmitx_i2s_mux), |
| 139 | SND_SOC_DAPM_SWITCH("I2S OUT EN", SND_SOC_NOPM, 0, 0, |
| 140 | &g12a_tohdmitx_out_enable), |
| 141 | SND_SOC_DAPM_MUX("SPDIF SRC", SND_SOC_NOPM, 0, 0, |
| 142 | &g12a_tohdmitx_spdif_mux), |
| 143 | SND_SOC_DAPM_SWITCH("SPDIF OUT EN", SND_SOC_NOPM, 0, 0, |
| 144 | &g12a_tohdmitx_out_enable), |
| 145 | }; |
| 146 | |
| 147 | static const struct snd_soc_dai_ops g12a_tohdmitx_input_ops = { |
| 148 | .hw_params = meson_codec_glue_input_hw_params, |
| 149 | .set_fmt = meson_codec_glue_input_set_fmt, |
| 150 | }; |
| 151 | |
| 152 | static const struct snd_soc_dai_ops g12a_tohdmitx_output_ops = { |
| 153 | .startup = meson_codec_glue_output_startup, |
| 154 | }; |
| 155 | |
| 156 | #define TOHDMITX_SPDIF_FORMATS \ |
| 157 | (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \ |
| 158 | SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_LE) |
| 159 | |
| 160 | #define TOHDMITX_I2S_FORMATS \ |
| 161 | (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \ |
| 162 | SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_LE | \ |
| 163 | SNDRV_PCM_FMTBIT_S32_LE) |
| 164 | |
| 165 | #define TOHDMITX_STREAM(xname, xsuffix, xfmt, xchmax) \ |
| 166 | { \ |
| 167 | .stream_name = xname " " xsuffix, \ |
| 168 | .channels_min = 1, \ |
| 169 | .channels_max = (xchmax), \ |
| 170 | .rate_min = 8000, \ |
| 171 | .rate_max = 192000, \ |
| 172 | .formats = (xfmt), \ |
| 173 | } |
| 174 | |
| 175 | #define TOHDMITX_IN(xname, xid, xfmt, xchmax) { \ |
| 176 | .name = xname, \ |
| 177 | .id = (xid), \ |
| 178 | .playback = TOHDMITX_STREAM(xname, "Playback", xfmt, xchmax), \ |
| 179 | .ops = &g12a_tohdmitx_input_ops, \ |
| 180 | .probe = meson_codec_glue_input_dai_probe, \ |
| 181 | .remove = meson_codec_glue_input_dai_remove, \ |
| 182 | } |
| 183 | |
| 184 | #define TOHDMITX_OUT(xname, xid, xfmt, xchmax) { \ |
| 185 | .name = xname, \ |
| 186 | .id = (xid), \ |
| 187 | .capture = TOHDMITX_STREAM(xname, "Capture", xfmt, xchmax), \ |
| 188 | .ops = &g12a_tohdmitx_output_ops, \ |
| 189 | } |
| 190 | |
| 191 | static struct snd_soc_dai_driver g12a_tohdmitx_dai_drv[] = { |
| 192 | TOHDMITX_IN("I2S IN A", TOHDMITX_I2S_IN_A, |
| 193 | TOHDMITX_I2S_FORMATS, 8), |
| 194 | TOHDMITX_IN("I2S IN B", TOHDMITX_I2S_IN_B, |
| 195 | TOHDMITX_I2S_FORMATS, 8), |
| 196 | TOHDMITX_IN("I2S IN C", TOHDMITX_I2S_IN_C, |
| 197 | TOHDMITX_I2S_FORMATS, 8), |
| 198 | TOHDMITX_OUT("I2S OUT", TOHDMITX_I2S_OUT, |
| 199 | TOHDMITX_I2S_FORMATS, 8), |
| 200 | TOHDMITX_IN("SPDIF IN A", TOHDMITX_SPDIF_IN_A, |
| 201 | TOHDMITX_SPDIF_FORMATS, 2), |
| 202 | TOHDMITX_IN("SPDIF IN B", TOHDMITX_SPDIF_IN_B, |
| 203 | TOHDMITX_SPDIF_FORMATS, 2), |
| 204 | TOHDMITX_OUT("SPDIF OUT", TOHDMITX_SPDIF_OUT, |
| 205 | TOHDMITX_SPDIF_FORMATS, 2), |
| 206 | }; |
| 207 | |
| 208 | static int g12a_tohdmi_component_probe(struct snd_soc_component *c) |
| 209 | { |
| 210 | /* Initialize the static clock parameters */ |
| 211 | return snd_soc_component_write(c, TOHDMITX_CTRL0, |
| 212 | CTRL0_I2S_BLK_CAP_INV | CTRL0_SPDIF_CLK_CAP_INV); |
| 213 | } |
| 214 | |
| 215 | static const struct snd_soc_dapm_route g12a_tohdmitx_routes[] = { |
| 216 | { "I2S SRC", "I2S A", "I2S IN A Playback" }, |
| 217 | { "I2S SRC", "I2S B", "I2S IN B Playback" }, |
| 218 | { "I2S SRC", "I2S C", "I2S IN C Playback" }, |
| 219 | { "I2S OUT EN", "Switch", "I2S SRC" }, |
| 220 | { "I2S OUT Capture", NULL, "I2S OUT EN" }, |
| 221 | { "SPDIF SRC", "SPDIF A", "SPDIF IN A Playback" }, |
| 222 | { "SPDIF SRC", "SPDIF B", "SPDIF IN B Playback" }, |
| 223 | { "SPDIF OUT EN", "Switch", "SPDIF SRC" }, |
| 224 | { "SPDIF OUT Capture", NULL, "SPDIF OUT EN" }, |
| 225 | }; |
| 226 | |
| 227 | static const struct snd_soc_component_driver g12a_tohdmitx_component_drv = { |
| 228 | .probe = g12a_tohdmi_component_probe, |
| 229 | .dapm_widgets = g12a_tohdmitx_widgets, |
| 230 | .num_dapm_widgets = ARRAY_SIZE(g12a_tohdmitx_widgets), |
| 231 | .dapm_routes = g12a_tohdmitx_routes, |
| 232 | .num_dapm_routes = ARRAY_SIZE(g12a_tohdmitx_routes), |
| 233 | .endianness = 1, |
| 234 | .non_legacy_dai_naming = 1, |
| 235 | }; |
| 236 | |
| 237 | static const struct regmap_config g12a_tohdmitx_regmap_cfg = { |
| 238 | .reg_bits = 32, |
| 239 | .val_bits = 32, |
| 240 | .reg_stride = 4, |
| 241 | }; |
| 242 | |
| 243 | static const struct of_device_id g12a_tohdmitx_of_match[] = { |
| 244 | { .compatible = "amlogic,g12a-tohdmitx", }, |
| 245 | {} |
| 246 | }; |
| 247 | MODULE_DEVICE_TABLE(of, g12a_tohdmitx_of_match); |
| 248 | |
| 249 | static int g12a_tohdmitx_probe(struct platform_device *pdev) |
| 250 | { |
| 251 | struct device *dev = &pdev->dev; |
| 252 | void __iomem *regs; |
| 253 | struct regmap *map; |
| 254 | |
| 255 | regs = devm_platform_ioremap_resource(pdev, 0); |
| 256 | if (IS_ERR(regs)) |
| 257 | return PTR_ERR(regs); |
| 258 | |
| 259 | map = devm_regmap_init_mmio(dev, regs, &g12a_tohdmitx_regmap_cfg); |
| 260 | if (IS_ERR(map)) { |
| 261 | dev_err(dev, "failed to init regmap: %ld\n", |
| 262 | PTR_ERR(map)); |
| 263 | return PTR_ERR(map); |
| 264 | } |
| 265 | |
| 266 | return devm_snd_soc_register_component(dev, |
| 267 | &g12a_tohdmitx_component_drv, g12a_tohdmitx_dai_drv, |
| 268 | ARRAY_SIZE(g12a_tohdmitx_dai_drv)); |
| 269 | } |
| 270 | |
| 271 | static struct platform_driver g12a_tohdmitx_pdrv = { |
| 272 | .driver = { |
| 273 | .name = G12A_TOHDMITX_DRV_NAME, |
| 274 | .of_match_table = g12a_tohdmitx_of_match, |
| 275 | }, |
| 276 | .probe = g12a_tohdmitx_probe, |
| 277 | }; |
| 278 | module_platform_driver(g12a_tohdmitx_pdrv); |
| 279 | |
| 280 | MODULE_AUTHOR("Jerome Brunet <jbrunet@baylibre.com>"); |
| 281 | MODULE_DESCRIPTION("Amlogic G12a To HDMI Tx Control Codec Driver"); |
| 282 | MODULE_LICENSE("GPL v2"); |