1 // SPDX-License-Identifier: GPL-2.0
3 // TSE-850 audio - ASoC driver for the Axentia TSE-850 with a PCM5142 codec
5 // Copyright (C) 2016 Axentia Technologies AB
7 // Author: Peter Rosin <peda@axentia.se>
10 // IN1 +---o +------------+ o---+ OUT1
18 // DAC +----------->|Sum|---+
23 // IN2 +---o--+------------+--o---+ OUT2
26 // The 'loop1' gpio pin controls two relays, which are either in loop
27 // position, meaning that input and output are directly connected, or
28 // they are in mixer position, meaning that the signal is passed through
29 // the 'Sum' mixer. Similarly for 'loop2'.
31 // In the above, the 'loop1' relays are inactive, thus feeding IN1 to the
32 // mixer (if 'add' is active) and feeding the mixer output to OUT1. The
33 // 'loop2' relays are active, short-cutting the TSE-850 from channel 2.
34 // IN1, IN2, OUT1 and OUT2 are TSE-850 connectors and DAC is the PCB name
35 // of the (filtered) output from the PCM5142 codec.
37 #include <linux/clk.h>
38 #include <linux/gpio.h>
39 #include <linux/module.h>
41 #include <linux/of_gpio.h>
42 #include <linux/regulator/consumer.h>
44 #include <sound/soc.h>
45 #include <sound/pcm_params.h>
48 struct gpio_desc *add;
49 struct gpio_desc *loop1;
50 struct gpio_desc *loop2;
52 struct regulator *ana;
59 static int tse850_get_mux1(struct snd_kcontrol *kctrl,
60 struct snd_ctl_elem_value *ucontrol)
62 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kctrl);
63 struct snd_soc_card *card = dapm->card;
64 struct tse850_priv *tse850 = snd_soc_card_get_drvdata(card);
66 ucontrol->value.enumerated.item[0] = tse850->loop1_cache;
71 static int tse850_put_mux1(struct snd_kcontrol *kctrl,
72 struct snd_ctl_elem_value *ucontrol)
74 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kctrl);
75 struct snd_soc_card *card = dapm->card;
76 struct tse850_priv *tse850 = snd_soc_card_get_drvdata(card);
77 struct soc_enum *e = (struct soc_enum *)kctrl->private_value;
78 unsigned int val = ucontrol->value.enumerated.item[0];
83 gpiod_set_value_cansleep(tse850->loop1, val);
84 tse850->loop1_cache = val;
86 return snd_soc_dapm_put_enum_double(kctrl, ucontrol);
89 static int tse850_get_mux2(struct snd_kcontrol *kctrl,
90 struct snd_ctl_elem_value *ucontrol)
92 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kctrl);
93 struct snd_soc_card *card = dapm->card;
94 struct tse850_priv *tse850 = snd_soc_card_get_drvdata(card);
96 ucontrol->value.enumerated.item[0] = tse850->loop2_cache;
101 static int tse850_put_mux2(struct snd_kcontrol *kctrl,
102 struct snd_ctl_elem_value *ucontrol)
104 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kctrl);
105 struct snd_soc_card *card = dapm->card;
106 struct tse850_priv *tse850 = snd_soc_card_get_drvdata(card);
107 struct soc_enum *e = (struct soc_enum *)kctrl->private_value;
108 unsigned int val = ucontrol->value.enumerated.item[0];
113 gpiod_set_value_cansleep(tse850->loop2, val);
114 tse850->loop2_cache = val;
116 return snd_soc_dapm_put_enum_double(kctrl, ucontrol);
119 static int tse850_get_mix(struct snd_kcontrol *kctrl,
120 struct snd_ctl_elem_value *ucontrol)
122 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kctrl);
123 struct snd_soc_card *card = dapm->card;
124 struct tse850_priv *tse850 = snd_soc_card_get_drvdata(card);
126 ucontrol->value.enumerated.item[0] = tse850->add_cache;
131 static int tse850_put_mix(struct snd_kcontrol *kctrl,
132 struct snd_ctl_elem_value *ucontrol)
134 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kctrl);
135 struct snd_soc_card *card = dapm->card;
136 struct tse850_priv *tse850 = snd_soc_card_get_drvdata(card);
137 int connect = !!ucontrol->value.integer.value[0];
139 if (tse850->add_cache == connect)
143 * Hmmm, this gpiod_set_value_cansleep call should probably happen
144 * inside snd_soc_dapm_mixer_update_power in the loop.
146 gpiod_set_value_cansleep(tse850->add, connect);
147 tse850->add_cache = connect;
149 snd_soc_dapm_mixer_update_power(dapm, kctrl, connect, NULL);
153 static int tse850_get_ana(struct snd_kcontrol *kctrl,
154 struct snd_ctl_elem_value *ucontrol)
156 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kctrl);
157 struct snd_soc_card *card = dapm->card;
158 struct tse850_priv *tse850 = snd_soc_card_get_drvdata(card);
161 ret = regulator_get_voltage(tse850->ana);
166 * Map regulator output values like so:
167 * -11.5V to "Low" (enum 0)
168 * 11.5V-12.5V to "12V" (enum 1)
169 * 12.5V-13.5V to "13V" (enum 2)
171 * 18.5V-19.5V to "19V" (enum 8)
172 * 19.5V- to "20V" (enum 9)
176 else if (ret > 20000000)
179 ret = (ret + 500000) / 1000000;
181 ucontrol->value.enumerated.item[0] = ret;
186 static int tse850_put_ana(struct snd_kcontrol *kctrl,
187 struct snd_ctl_elem_value *ucontrol)
189 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kctrl);
190 struct snd_soc_card *card = dapm->card;
191 struct tse850_priv *tse850 = snd_soc_card_get_drvdata(card);
192 struct soc_enum *e = (struct soc_enum *)kctrl->private_value;
193 unsigned int uV = ucontrol->value.enumerated.item[0];
200 * Map enum zero (Low) to 2 volts on the regulator, do this since
201 * the ana regulator is supplied by the system 12V voltage and
202 * requesting anything below the system voltage causes the system
203 * voltage to be passed through the regulator. Also, the ana
204 * regulator induces noise when requesting voltages near the
205 * system voltage. So, by mapping Low to 2V, that noise is
206 * eliminated when all that is needed is 12V (the system voltage).
209 uV = 11000000 + (1000000 * uV);
213 ret = regulator_set_voltage(tse850->ana, uV, uV);
217 return snd_soc_dapm_put_enum_double(kctrl, ucontrol);
220 static const char * const mux_text[] = { "Mixer", "Loop" };
222 static const struct soc_enum mux_enum =
223 SOC_ENUM_SINGLE(SND_SOC_NOPM, 0, ARRAY_SIZE(mux_text), mux_text);
225 static const struct snd_kcontrol_new mux1 =
226 SOC_DAPM_ENUM_EXT("MUX1", mux_enum, tse850_get_mux1, tse850_put_mux1);
228 static const struct snd_kcontrol_new mux2 =
229 SOC_DAPM_ENUM_EXT("MUX2", mux_enum, tse850_get_mux2, tse850_put_mux2);
231 #define TSE850_DAPM_SINGLE_EXT(xname, reg, shift, max, invert, xget, xput) \
232 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
233 .info = snd_soc_info_volsw, \
236 .private_value = SOC_SINGLE_VALUE(reg, shift, max, invert, 0) }
238 static const struct snd_kcontrol_new mix[] = {
239 TSE850_DAPM_SINGLE_EXT("IN Switch", SND_SOC_NOPM, 0, 1, 0,
240 tse850_get_mix, tse850_put_mix),
243 static const char * const ana_text[] = {
244 "Low", "12V", "13V", "14V", "15V", "16V", "17V", "18V", "19V", "20V"
247 static const struct soc_enum ana_enum =
248 SOC_ENUM_SINGLE(SND_SOC_NOPM, 0, ARRAY_SIZE(ana_text), ana_text);
250 static const struct snd_kcontrol_new out =
251 SOC_DAPM_ENUM_EXT("ANA", ana_enum, tse850_get_ana, tse850_put_ana);
253 static const struct snd_soc_dapm_widget tse850_dapm_widgets[] = {
254 SND_SOC_DAPM_LINE("OUT1", NULL),
255 SND_SOC_DAPM_LINE("OUT2", NULL),
256 SND_SOC_DAPM_LINE("IN1", NULL),
257 SND_SOC_DAPM_LINE("IN2", NULL),
258 SND_SOC_DAPM_INPUT("DAC"),
259 SND_SOC_DAPM_AIF_IN("AIFINL", "Playback", 0, SND_SOC_NOPM, 0, 0),
260 SND_SOC_DAPM_AIF_IN("AIFINR", "Playback", 1, SND_SOC_NOPM, 0, 0),
261 SOC_MIXER_ARRAY("MIX", SND_SOC_NOPM, 0, 0, mix),
262 SND_SOC_DAPM_MUX("MUX1", SND_SOC_NOPM, 0, 0, &mux1),
263 SND_SOC_DAPM_MUX("MUX2", SND_SOC_NOPM, 0, 0, &mux2),
264 SND_SOC_DAPM_OUT_DRV("OUT", SND_SOC_NOPM, 0, 0, &out, 1),
268 * These connections are not entirely correct, since both IN1 and IN2
269 * are always fed to MIX (if the "IN switch" is set so), i.e. without
270 * regard to the loop1 and loop2 relays that according to this only
271 * control MUX1 and MUX2 but in fact also control how the input signals
273 * But, 1) I don't know how to do it right, and 2) it doesn't seem to
274 * matter in practice since nothing is powered in those sections anyway.
276 static const struct snd_soc_dapm_route tse850_intercon[] = {
277 { "OUT1", NULL, "MUX1" },
278 { "OUT2", NULL, "MUX2" },
280 { "MUX1", "Loop", "IN1" },
281 { "MUX1", "Mixer", "OUT" },
283 { "MUX2", "Loop", "IN2" },
284 { "MUX2", "Mixer", "OUT" },
286 { "OUT", NULL, "MIX" },
288 { "MIX", NULL, "DAC" },
289 { "MIX", "IN Switch", "IN1" },
290 { "MIX", "IN Switch", "IN2" },
292 /* connect board input to the codec left channel output pin */
293 { "DAC", NULL, "OUTL" },
296 SND_SOC_DAILINK_DEFS(pcm,
297 DAILINK_COMP_ARRAY(COMP_EMPTY()),
298 DAILINK_COMP_ARRAY(COMP_CODEC(NULL, "pcm512x-hifi")),
299 DAILINK_COMP_ARRAY(COMP_EMPTY()));
301 static struct snd_soc_dai_link tse850_dailink = {
303 .stream_name = "TSE-850-PCM",
304 .dai_fmt = SND_SOC_DAIFMT_I2S
305 | SND_SOC_DAIFMT_NB_NF
306 | SND_SOC_DAIFMT_CBP_CFC,
307 SND_SOC_DAILINK_REG(pcm),
310 static struct snd_soc_card tse850_card = {
311 .name = "TSE-850-ASoC",
312 .owner = THIS_MODULE,
313 .dai_link = &tse850_dailink,
315 .dapm_widgets = tse850_dapm_widgets,
316 .num_dapm_widgets = ARRAY_SIZE(tse850_dapm_widgets),
317 .dapm_routes = tse850_intercon,
318 .num_dapm_routes = ARRAY_SIZE(tse850_intercon),
319 .fully_routed = true,
322 static int tse850_dt_init(struct platform_device *pdev)
324 struct device_node *np = pdev->dev.of_node;
325 struct device_node *codec_np, *cpu_np;
326 struct snd_soc_dai_link *dailink = &tse850_dailink;
329 dev_err(&pdev->dev, "only device tree supported\n");
333 cpu_np = of_parse_phandle(np, "axentia,cpu-dai", 0);
335 dev_err(&pdev->dev, "failed to get cpu dai\n");
338 dailink->cpus->of_node = cpu_np;
339 dailink->platforms->of_node = cpu_np;
342 codec_np = of_parse_phandle(np, "axentia,audio-codec", 0);
344 dev_err(&pdev->dev, "failed to get codec info\n");
347 dailink->codecs->of_node = codec_np;
348 of_node_put(codec_np);
353 static int tse850_probe(struct platform_device *pdev)
355 struct snd_soc_card *card = &tse850_card;
356 struct device *dev = card->dev = &pdev->dev;
357 struct tse850_priv *tse850;
360 tse850 = devm_kzalloc(dev, sizeof(*tse850), GFP_KERNEL);
364 snd_soc_card_set_drvdata(card, tse850);
366 ret = tse850_dt_init(pdev);
368 dev_err(dev, "failed to init dt info\n");
372 tse850->add = devm_gpiod_get(dev, "axentia,add", GPIOD_OUT_HIGH);
373 if (IS_ERR(tse850->add))
374 return dev_err_probe(dev, PTR_ERR(tse850->add),
375 "failed to get 'add' gpio\n");
376 tse850->add_cache = 1;
378 tse850->loop1 = devm_gpiod_get(dev, "axentia,loop1", GPIOD_OUT_HIGH);
379 if (IS_ERR(tse850->loop1))
380 return dev_err_probe(dev, PTR_ERR(tse850->loop1),
381 "failed to get 'loop1' gpio\n");
382 tse850->loop1_cache = 1;
384 tse850->loop2 = devm_gpiod_get(dev, "axentia,loop2", GPIOD_OUT_HIGH);
385 if (IS_ERR(tse850->loop2))
386 return dev_err_probe(dev, PTR_ERR(tse850->loop2),
387 "failed to get 'loop2' gpio\n");
388 tse850->loop2_cache = 1;
390 tse850->ana = devm_regulator_get(dev, "axentia,ana");
391 if (IS_ERR(tse850->ana))
392 return dev_err_probe(dev, PTR_ERR(tse850->ana),
393 "failed to get 'ana' regulator\n");
395 ret = regulator_enable(tse850->ana);
397 dev_err(dev, "failed to enable the 'ana' regulator\n");
401 ret = snd_soc_register_card(card);
403 dev_err(dev, "snd_soc_register_card failed\n");
404 goto err_disable_ana;
410 regulator_disable(tse850->ana);
414 static void tse850_remove(struct platform_device *pdev)
416 struct snd_soc_card *card = platform_get_drvdata(pdev);
417 struct tse850_priv *tse850 = snd_soc_card_get_drvdata(card);
419 snd_soc_unregister_card(card);
420 regulator_disable(tse850->ana);
423 static const struct of_device_id tse850_dt_ids[] = {
424 { .compatible = "axentia,tse850-pcm5142", },
427 MODULE_DEVICE_TABLE(of, tse850_dt_ids);
429 static struct platform_driver tse850_driver = {
431 .name = "axentia-tse850-pcm5142",
432 .of_match_table = tse850_dt_ids,
434 .probe = tse850_probe,
435 .remove_new = tse850_remove,
438 module_platform_driver(tse850_driver);
440 /* Module information */
441 MODULE_AUTHOR("Peter Rosin <peda@axentia.se>");
442 MODULE_DESCRIPTION("ALSA SoC driver for TSE-850 with PCM5142 codec");
443 MODULE_LICENSE("GPL v2");