1 // SPDX-License-Identifier: (GPL-2.0 OR MIT) 2 // 3 // Copyright (c) 2018 BayLibre, SAS. 4 // Author: Jerome Brunet <jbrunet@baylibre.com> 5 6 #include <linux/module.h> 7 #include <linux/of_platform.h> 8 #include <linux/regmap.h> 9 #include <sound/soc.h> 10 #include <sound/soc-dai.h> 11 12 #include "axg-tdm-formatter.h" 13 14 #define TDMIN_CTRL 0x00 15 #define TDMIN_CTRL_ENABLE BIT(31) 16 #define TDMIN_CTRL_I2S_MODE BIT(30) 17 #define TDMIN_CTRL_RST_OUT BIT(29) 18 #define TDMIN_CTRL_RST_IN BIT(28) 19 #define TDMIN_CTRL_WS_INV BIT(25) 20 #define TDMIN_CTRL_SEL_SHIFT 20 21 #define TDMIN_CTRL_IN_BIT_SKEW_MASK GENMASK(18, 16) 22 #define TDMIN_CTRL_IN_BIT_SKEW(x) ((x) << 16) 23 #define TDMIN_CTRL_LSB_FIRST BIT(5) 24 #define TDMIN_CTRL_BITNUM_MASK GENMASK(4, 0) 25 #define TDMIN_CTRL_BITNUM(x) ((x) << 0) 26 #define TDMIN_SWAP 0x04 27 #define TDMIN_MASK0 0x08 28 #define TDMIN_MASK1 0x0c 29 #define TDMIN_MASK2 0x10 30 #define TDMIN_MASK3 0x14 31 #define TDMIN_STAT 0x18 32 #define TDMIN_MUTE_VAL 0x1c 33 #define TDMIN_MUTE0 0x20 34 #define TDMIN_MUTE1 0x24 35 #define TDMIN_MUTE2 0x28 36 #define TDMIN_MUTE3 0x2c 37 38 static const struct regmap_config axg_tdmin_regmap_cfg = { 39 .reg_bits = 32, 40 .val_bits = 32, 41 .reg_stride = 4, 42 .max_register = TDMIN_MUTE3, 43 }; 44 45 static const char * const axg_tdmin_sel_texts[] = { 46 "IN 0", "IN 1", "IN 2", "IN 3", "IN 4", "IN 5", 47 }; 48 49 /* Change to special mux control to reset dapm */ 50 static SOC_ENUM_SINGLE_DECL(axg_tdmin_sel_enum, TDMIN_CTRL, 51 TDMIN_CTRL_SEL_SHIFT, axg_tdmin_sel_texts); 52 53 static const struct snd_kcontrol_new axg_tdmin_in_mux = 54 SOC_DAPM_ENUM("Input Source", axg_tdmin_sel_enum); 55 56 static struct snd_soc_dai * 57 axg_tdmin_get_be(struct snd_soc_dapm_widget *w) 58 { 59 struct snd_soc_dapm_path *p = NULL; 60 struct snd_soc_dai *be; 61 62 snd_soc_dapm_widget_for_each_source_path(w, p) { 63 if (!p->connect) 64 continue; 65 66 if (p->source->id == snd_soc_dapm_dai_out) 67 return (struct snd_soc_dai *)p->source->priv; 68 69 be = axg_tdmin_get_be(p->source); 70 if (be) 71 return be; 72 } 73 74 return NULL; 75 } 76 77 static struct axg_tdm_stream * 78 axg_tdmin_get_tdm_stream(struct snd_soc_dapm_widget *w) 79 { 80 struct snd_soc_dai *be = axg_tdmin_get_be(w); 81 82 if (!be) 83 return NULL; 84 85 return be->capture_dma_data; 86 } 87 88 static void axg_tdmin_enable(struct regmap *map) 89 { 90 /* Apply both reset */ 91 regmap_update_bits(map, TDMIN_CTRL, 92 TDMIN_CTRL_RST_OUT | TDMIN_CTRL_RST_IN, 0); 93 94 /* Clear out reset before in reset */ 95 regmap_update_bits(map, TDMIN_CTRL, 96 TDMIN_CTRL_RST_OUT, TDMIN_CTRL_RST_OUT); 97 regmap_update_bits(map, TDMIN_CTRL, 98 TDMIN_CTRL_RST_IN, TDMIN_CTRL_RST_IN); 99 100 /* Actually enable tdmin */ 101 regmap_update_bits(map, TDMIN_CTRL, 102 TDMIN_CTRL_ENABLE, TDMIN_CTRL_ENABLE); 103 } 104 105 static void axg_tdmin_disable(struct regmap *map) 106 { 107 regmap_update_bits(map, TDMIN_CTRL, TDMIN_CTRL_ENABLE, 0); 108 } 109 110 static int axg_tdmin_prepare(struct regmap *map, 111 const struct axg_tdm_formatter_hw *quirks, 112 struct axg_tdm_stream *ts) 113 { 114 unsigned int val, skew = quirks->skew_offset; 115 116 /* Set stream skew */ 117 switch (ts->iface->fmt & SND_SOC_DAIFMT_FORMAT_MASK) { 118 case SND_SOC_DAIFMT_I2S: 119 case SND_SOC_DAIFMT_DSP_A: 120 skew += 1; 121 break; 122 123 case SND_SOC_DAIFMT_LEFT_J: 124 case SND_SOC_DAIFMT_DSP_B: 125 break; 126 127 default: 128 pr_err("Unsupported format: %u\n", 129 ts->iface->fmt & SND_SOC_DAIFMT_FORMAT_MASK); 130 return -EINVAL; 131 } 132 133 val = TDMIN_CTRL_IN_BIT_SKEW(skew); 134 135 /* Set stream format mode */ 136 switch (ts->iface->fmt & SND_SOC_DAIFMT_FORMAT_MASK) { 137 case SND_SOC_DAIFMT_I2S: 138 case SND_SOC_DAIFMT_LEFT_J: 139 case SND_SOC_DAIFMT_RIGHT_J: 140 val |= TDMIN_CTRL_I2S_MODE; 141 break; 142 } 143 144 /* If the sample clock is inverted, invert it back for the formatter */ 145 if (axg_tdm_lrclk_invert(ts->iface->fmt)) 146 val |= TDMIN_CTRL_WS_INV; 147 148 /* Set the slot width */ 149 val |= TDMIN_CTRL_BITNUM(ts->iface->slot_width - 1); 150 151 /* 152 * The following also reset LSB_FIRST which result in the formatter 153 * placing the first bit received at bit 31 154 */ 155 regmap_update_bits(map, TDMIN_CTRL, 156 (TDMIN_CTRL_IN_BIT_SKEW_MASK | TDMIN_CTRL_WS_INV | 157 TDMIN_CTRL_I2S_MODE | TDMIN_CTRL_LSB_FIRST | 158 TDMIN_CTRL_BITNUM_MASK), val); 159 160 /* Set static swap mask configuration */ 161 regmap_write(map, TDMIN_SWAP, 0x76543210); 162 163 return axg_tdm_formatter_set_channel_masks(map, ts, TDMIN_MASK0); 164 } 165 166 static const struct snd_soc_dapm_widget axg_tdmin_dapm_widgets[] = { 167 SND_SOC_DAPM_AIF_IN("IN 0", NULL, 0, SND_SOC_NOPM, 0, 0), 168 SND_SOC_DAPM_AIF_IN("IN 1", NULL, 0, SND_SOC_NOPM, 0, 0), 169 SND_SOC_DAPM_AIF_IN("IN 2", NULL, 0, SND_SOC_NOPM, 0, 0), 170 SND_SOC_DAPM_AIF_IN("IN 3", NULL, 0, SND_SOC_NOPM, 0, 0), 171 SND_SOC_DAPM_AIF_IN("IN 4", NULL, 0, SND_SOC_NOPM, 0, 0), 172 SND_SOC_DAPM_AIF_IN("IN 5", NULL, 0, SND_SOC_NOPM, 0, 0), 173 SND_SOC_DAPM_MUX("SRC SEL", SND_SOC_NOPM, 0, 0, &axg_tdmin_in_mux), 174 SND_SOC_DAPM_PGA_E("DEC", SND_SOC_NOPM, 0, 0, NULL, 0, 175 axg_tdm_formatter_event, 176 (SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_PRE_PMD)), 177 SND_SOC_DAPM_AIF_OUT("OUT", NULL, 0, SND_SOC_NOPM, 0, 0), 178 }; 179 180 static const struct snd_soc_dapm_route axg_tdmin_dapm_routes[] = { 181 { "SRC SEL", "IN 0", "IN 0" }, 182 { "SRC SEL", "IN 1", "IN 1" }, 183 { "SRC SEL", "IN 2", "IN 2" }, 184 { "SRC SEL", "IN 3", "IN 3" }, 185 { "SRC SEL", "IN 4", "IN 4" }, 186 { "SRC SEL", "IN 5", "IN 5" }, 187 { "DEC", NULL, "SRC SEL" }, 188 { "OUT", NULL, "DEC" }, 189 }; 190 191 static const struct snd_soc_component_driver axg_tdmin_component_drv = { 192 .dapm_widgets = axg_tdmin_dapm_widgets, 193 .num_dapm_widgets = ARRAY_SIZE(axg_tdmin_dapm_widgets), 194 .dapm_routes = axg_tdmin_dapm_routes, 195 .num_dapm_routes = ARRAY_SIZE(axg_tdmin_dapm_routes), 196 }; 197 198 static const struct axg_tdm_formatter_ops axg_tdmin_ops = { 199 .get_stream = axg_tdmin_get_tdm_stream, 200 .prepare = axg_tdmin_prepare, 201 .enable = axg_tdmin_enable, 202 .disable = axg_tdmin_disable, 203 }; 204 205 static const struct axg_tdm_formatter_driver axg_tdmin_drv = { 206 .component_drv = &axg_tdmin_component_drv, 207 .regmap_cfg = &axg_tdmin_regmap_cfg, 208 .ops = &axg_tdmin_ops, 209 .quirks = &(const struct axg_tdm_formatter_hw) { 210 .invert_sclk = false, 211 .skew_offset = 2, 212 }, 213 }; 214 215 static const struct of_device_id axg_tdmin_of_match[] = { 216 { 217 .compatible = "amlogic,axg-tdmin", 218 .data = &axg_tdmin_drv, 219 }, {} 220 }; 221 MODULE_DEVICE_TABLE(of, axg_tdmin_of_match); 222 223 static struct platform_driver axg_tdmin_pdrv = { 224 .probe = axg_tdm_formatter_probe, 225 .driver = { 226 .name = "axg-tdmin", 227 .of_match_table = axg_tdmin_of_match, 228 }, 229 }; 230 module_platform_driver(axg_tdmin_pdrv); 231 232 MODULE_DESCRIPTION("Amlogic AXG TDM input formatter driver"); 233 MODULE_AUTHOR("Jerome Brunet <jbrunet@baylibre.com>"); 234 MODULE_LICENSE("GPL v2"); 235