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