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