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 controlls 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_device.h>
42 #include <linux/of_gpio.h>
43 #include <linux/regulator/consumer.h>
45 #include <sound/soc.h>
46 #include <sound/pcm_params.h>
49 struct gpio_desc
*add
;
50 struct gpio_desc
*loop1
;
51 struct gpio_desc
*loop2
;
53 struct regulator
*ana
;
60 static int tse850_get_mux1(struct snd_kcontrol
*kctrl
,
61 struct snd_ctl_elem_value
*ucontrol
)
63 struct snd_soc_dapm_context
*dapm
= snd_soc_dapm_kcontrol_dapm(kctrl
);
64 struct snd_soc_card
*card
= dapm
->card
;
65 struct tse850_priv
*tse850
= snd_soc_card_get_drvdata(card
);
67 ucontrol
->value
.enumerated
.item
[0] = tse850
->loop1_cache
;
72 static int tse850_put_mux1(struct snd_kcontrol
*kctrl
,
73 struct snd_ctl_elem_value
*ucontrol
)
75 struct snd_soc_dapm_context
*dapm
= snd_soc_dapm_kcontrol_dapm(kctrl
);
76 struct snd_soc_card
*card
= dapm
->card
;
77 struct tse850_priv
*tse850
= snd_soc_card_get_drvdata(card
);
78 struct soc_enum
*e
= (struct soc_enum
*)kctrl
->private_value
;
79 unsigned int val
= ucontrol
->value
.enumerated
.item
[0];
84 gpiod_set_value_cansleep(tse850
->loop1
, val
);
85 tse850
->loop1_cache
= val
;
87 return snd_soc_dapm_put_enum_double(kctrl
, ucontrol
);
90 static int tse850_get_mux2(struct snd_kcontrol
*kctrl
,
91 struct snd_ctl_elem_value
*ucontrol
)
93 struct snd_soc_dapm_context
*dapm
= snd_soc_dapm_kcontrol_dapm(kctrl
);
94 struct snd_soc_card
*card
= dapm
->card
;
95 struct tse850_priv
*tse850
= snd_soc_card_get_drvdata(card
);
97 ucontrol
->value
.enumerated
.item
[0] = tse850
->loop2_cache
;
102 static int tse850_put_mux2(struct snd_kcontrol
*kctrl
,
103 struct snd_ctl_elem_value
*ucontrol
)
105 struct snd_soc_dapm_context
*dapm
= snd_soc_dapm_kcontrol_dapm(kctrl
);
106 struct snd_soc_card
*card
= dapm
->card
;
107 struct tse850_priv
*tse850
= snd_soc_card_get_drvdata(card
);
108 struct soc_enum
*e
= (struct soc_enum
*)kctrl
->private_value
;
109 unsigned int val
= ucontrol
->value
.enumerated
.item
[0];
114 gpiod_set_value_cansleep(tse850
->loop2
, val
);
115 tse850
->loop2_cache
= val
;
117 return snd_soc_dapm_put_enum_double(kctrl
, ucontrol
);
120 static int tse850_get_mix(struct snd_kcontrol
*kctrl
,
121 struct snd_ctl_elem_value
*ucontrol
)
123 struct snd_soc_dapm_context
*dapm
= snd_soc_dapm_kcontrol_dapm(kctrl
);
124 struct snd_soc_card
*card
= dapm
->card
;
125 struct tse850_priv
*tse850
= snd_soc_card_get_drvdata(card
);
127 ucontrol
->value
.enumerated
.item
[0] = tse850
->add_cache
;
132 static int tse850_put_mix(struct snd_kcontrol
*kctrl
,
133 struct snd_ctl_elem_value
*ucontrol
)
135 struct snd_soc_dapm_context
*dapm
= snd_soc_dapm_kcontrol_dapm(kctrl
);
136 struct snd_soc_card
*card
= dapm
->card
;
137 struct tse850_priv
*tse850
= snd_soc_card_get_drvdata(card
);
138 int connect
= !!ucontrol
->value
.integer
.value
[0];
140 if (tse850
->add_cache
== connect
)
144 * Hmmm, this gpiod_set_value_cansleep call should probably happen
145 * inside snd_soc_dapm_mixer_update_power in the loop.
147 gpiod_set_value_cansleep(tse850
->add
, connect
);
148 tse850
->add_cache
= connect
;
150 snd_soc_dapm_mixer_update_power(dapm
, kctrl
, connect
, NULL
);
154 static int tse850_get_ana(struct snd_kcontrol
*kctrl
,
155 struct snd_ctl_elem_value
*ucontrol
)
157 struct snd_soc_dapm_context
*dapm
= snd_soc_dapm_kcontrol_dapm(kctrl
);
158 struct snd_soc_card
*card
= dapm
->card
;
159 struct tse850_priv
*tse850
= snd_soc_card_get_drvdata(card
);
162 ret
= regulator_get_voltage(tse850
->ana
);
167 * Map regulator output values like so:
168 * -11.5V to "Low" (enum 0)
169 * 11.5V-12.5V to "12V" (enum 1)
170 * 12.5V-13.5V to "13V" (enum 2)
172 * 18.5V-19.5V to "19V" (enum 8)
173 * 19.5V- to "20V" (enum 9)
177 else if (ret
> 20000000)
180 ret
= (ret
+ 500000) / 1000000;
182 ucontrol
->value
.enumerated
.item
[0] = ret
;
187 static int tse850_put_ana(struct snd_kcontrol
*kctrl
,
188 struct snd_ctl_elem_value
*ucontrol
)
190 struct snd_soc_dapm_context
*dapm
= snd_soc_dapm_kcontrol_dapm(kctrl
);
191 struct snd_soc_card
*card
= dapm
->card
;
192 struct tse850_priv
*tse850
= snd_soc_card_get_drvdata(card
);
193 struct soc_enum
*e
= (struct soc_enum
*)kctrl
->private_value
;
194 unsigned int uV
= ucontrol
->value
.enumerated
.item
[0];
201 * Map enum zero (Low) to 2 volts on the regulator, do this since
202 * the ana regulator is supplied by the system 12V voltage and
203 * requesting anything below the system voltage causes the system
204 * voltage to be passed through the regulator. Also, the ana
205 * regulator induces noise when requesting voltages near the
206 * system voltage. So, by mapping Low to 2V, that noise is
207 * eliminated when all that is needed is 12V (the system voltage).
210 uV
= 11000000 + (1000000 * uV
);
214 ret
= regulator_set_voltage(tse850
->ana
, uV
, uV
);
218 return snd_soc_dapm_put_enum_double(kctrl
, ucontrol
);
221 static const char * const mux_text
[] = { "Mixer", "Loop" };
223 static const struct soc_enum mux_enum
=
224 SOC_ENUM_SINGLE(SND_SOC_NOPM
, 0, ARRAY_SIZE(mux_text
), mux_text
);
226 static const struct snd_kcontrol_new mux1
=
227 SOC_DAPM_ENUM_EXT("MUX1", mux_enum
, tse850_get_mux1
, tse850_put_mux1
);
229 static const struct snd_kcontrol_new mux2
=
230 SOC_DAPM_ENUM_EXT("MUX2", mux_enum
, tse850_get_mux2
, tse850_put_mux2
);
232 #define TSE850_DAPM_SINGLE_EXT(xname, reg, shift, max, invert, xget, xput) \
233 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
234 .info = snd_soc_info_volsw, \
237 .private_value = SOC_SINGLE_VALUE(reg, shift, max, invert, 0) }
239 static const struct snd_kcontrol_new mix
[] = {
240 TSE850_DAPM_SINGLE_EXT("IN Switch", SND_SOC_NOPM
, 0, 1, 0,
241 tse850_get_mix
, tse850_put_mix
),
244 static const char * const ana_text
[] = {
245 "Low", "12V", "13V", "14V", "15V", "16V", "17V", "18V", "19V", "20V"
248 static const struct soc_enum ana_enum
=
249 SOC_ENUM_SINGLE(SND_SOC_NOPM
, 0, ARRAY_SIZE(ana_text
), ana_text
);
251 static const struct snd_kcontrol_new out
=
252 SOC_DAPM_ENUM_EXT("ANA", ana_enum
, tse850_get_ana
, tse850_put_ana
);
254 static const struct snd_soc_dapm_widget tse850_dapm_widgets
[] = {
255 SND_SOC_DAPM_LINE("OUT1", NULL
),
256 SND_SOC_DAPM_LINE("OUT2", NULL
),
257 SND_SOC_DAPM_LINE("IN1", NULL
),
258 SND_SOC_DAPM_LINE("IN2", NULL
),
259 SND_SOC_DAPM_INPUT("DAC"),
260 SND_SOC_DAPM_AIF_IN("AIFINL", "Playback", 0, SND_SOC_NOPM
, 0, 0),
261 SND_SOC_DAPM_AIF_IN("AIFINR", "Playback", 1, SND_SOC_NOPM
, 0, 0),
262 SOC_MIXER_ARRAY("MIX", SND_SOC_NOPM
, 0, 0, mix
),
263 SND_SOC_DAPM_MUX("MUX1", SND_SOC_NOPM
, 0, 0, &mux1
),
264 SND_SOC_DAPM_MUX("MUX2", SND_SOC_NOPM
, 0, 0, &mux2
),
265 SND_SOC_DAPM_OUT_DRV("OUT", SND_SOC_NOPM
, 0, 0, &out
, 1),
269 * These connections are not entirely correct, since both IN1 and IN2
270 * are always fed to MIX (if the "IN switch" is set so), i.e. without
271 * regard to the loop1 and loop2 relays that according to this only
272 * control MUX1 and MUX2 but in fact also control how the input signals
274 * But, 1) I don't know how to do it right, and 2) it doesn't seem to
275 * matter in practice since nothing is powered in those sections anyway.
277 static const struct snd_soc_dapm_route tse850_intercon
[] = {
278 { "OUT1", NULL
, "MUX1" },
279 { "OUT2", NULL
, "MUX2" },
281 { "MUX1", "Loop", "IN1" },
282 { "MUX1", "Mixer", "OUT" },
284 { "MUX2", "Loop", "IN2" },
285 { "MUX2", "Mixer", "OUT" },
287 { "OUT", NULL
, "MIX" },
289 { "MIX", NULL
, "DAC" },
290 { "MIX", "IN Switch", "IN1" },
291 { "MIX", "IN Switch", "IN2" },
293 /* connect board input to the codec left channel output pin */
294 { "DAC", NULL
, "OUTL" },
297 SND_SOC_DAILINK_DEFS(pcm
,
298 DAILINK_COMP_ARRAY(COMP_EMPTY()),
299 DAILINK_COMP_ARRAY(COMP_CODEC(NULL
, "pcm512x-hifi")),
300 DAILINK_COMP_ARRAY(COMP_EMPTY()));
302 static struct snd_soc_dai_link tse850_dailink
= {
304 .stream_name
= "TSE-850-PCM",
305 .dai_fmt
= SND_SOC_DAIFMT_I2S
306 | SND_SOC_DAIFMT_NB_NF
307 | SND_SOC_DAIFMT_CBM_CFS
,
308 SND_SOC_DAILINK_REG(pcm
),
311 static struct snd_soc_card tse850_card
= {
312 .name
= "TSE-850-ASoC",
313 .owner
= THIS_MODULE
,
314 .dai_link
= &tse850_dailink
,
316 .dapm_widgets
= tse850_dapm_widgets
,
317 .num_dapm_widgets
= ARRAY_SIZE(tse850_dapm_widgets
),
318 .dapm_routes
= tse850_intercon
,
319 .num_dapm_routes
= ARRAY_SIZE(tse850_intercon
),
320 .fully_routed
= true,
323 static int tse850_dt_init(struct platform_device
*pdev
)
325 struct device_node
*np
= pdev
->dev
.of_node
;
326 struct device_node
*codec_np
, *cpu_np
;
327 struct snd_soc_dai_link
*dailink
= &tse850_dailink
;
330 dev_err(&pdev
->dev
, "only device tree supported\n");
334 cpu_np
= of_parse_phandle(np
, "axentia,cpu-dai", 0);
336 dev_err(&pdev
->dev
, "failed to get cpu dai\n");
339 dailink
->cpus
->of_node
= cpu_np
;
340 dailink
->platforms
->of_node
= cpu_np
;
343 codec_np
= of_parse_phandle(np
, "axentia,audio-codec", 0);
345 dev_err(&pdev
->dev
, "failed to get codec info\n");
348 dailink
->codecs
->of_node
= codec_np
;
349 of_node_put(codec_np
);
354 static int tse850_probe(struct platform_device
*pdev
)
356 struct snd_soc_card
*card
= &tse850_card
;
357 struct device
*dev
= card
->dev
= &pdev
->dev
;
358 struct tse850_priv
*tse850
;
361 tse850
= devm_kzalloc(dev
, sizeof(*tse850
), GFP_KERNEL
);
365 snd_soc_card_set_drvdata(card
, tse850
);
367 ret
= tse850_dt_init(pdev
);
369 dev_err(dev
, "failed to init dt info\n");
373 tse850
->add
= devm_gpiod_get(dev
, "axentia,add", GPIOD_OUT_HIGH
);
374 if (IS_ERR(tse850
->add
)) {
375 if (PTR_ERR(tse850
->add
) != -EPROBE_DEFER
)
376 dev_err(dev
, "failed to get 'add' gpio\n");
377 return PTR_ERR(tse850
->add
);
379 tse850
->add_cache
= 1;
381 tse850
->loop1
= devm_gpiod_get(dev
, "axentia,loop1", GPIOD_OUT_HIGH
);
382 if (IS_ERR(tse850
->loop1
)) {
383 if (PTR_ERR(tse850
->loop1
) != -EPROBE_DEFER
)
384 dev_err(dev
, "failed to get 'loop1' gpio\n");
385 return PTR_ERR(tse850
->loop1
);
387 tse850
->loop1_cache
= 1;
389 tse850
->loop2
= devm_gpiod_get(dev
, "axentia,loop2", GPIOD_OUT_HIGH
);
390 if (IS_ERR(tse850
->loop2
)) {
391 if (PTR_ERR(tse850
->loop2
) != -EPROBE_DEFER
)
392 dev_err(dev
, "failed to get 'loop2' gpio\n");
393 return PTR_ERR(tse850
->loop2
);
395 tse850
->loop2_cache
= 1;
397 tse850
->ana
= devm_regulator_get(dev
, "axentia,ana");
398 if (IS_ERR(tse850
->ana
)) {
399 if (PTR_ERR(tse850
->ana
) != -EPROBE_DEFER
)
400 dev_err(dev
, "failed to get 'ana' regulator\n");
401 return PTR_ERR(tse850
->ana
);
404 ret
= regulator_enable(tse850
->ana
);
406 dev_err(dev
, "failed to enable the 'ana' regulator\n");
410 ret
= snd_soc_register_card(card
);
412 dev_err(dev
, "snd_soc_register_card failed\n");
413 goto err_disable_ana
;
419 regulator_disable(tse850
->ana
);
423 static int tse850_remove(struct platform_device
*pdev
)
425 struct snd_soc_card
*card
= platform_get_drvdata(pdev
);
426 struct tse850_priv
*tse850
= snd_soc_card_get_drvdata(card
);
428 snd_soc_unregister_card(card
);
429 regulator_disable(tse850
->ana
);
434 static const struct of_device_id tse850_dt_ids
[] = {
435 { .compatible
= "axentia,tse850-pcm5142", },
438 MODULE_DEVICE_TABLE(of
, tse850_dt_ids
);
440 static struct platform_driver tse850_driver
= {
442 .name
= "axentia-tse850-pcm5142",
443 .of_match_table
= of_match_ptr(tse850_dt_ids
),
445 .probe
= tse850_probe
,
446 .remove
= tse850_remove
,
449 module_platform_driver(tse850_driver
);
451 /* Module information */
452 MODULE_AUTHOR("Peter Rosin <peda@axentia.se>");
453 MODULE_DESCRIPTION("ALSA SoC driver for TSE-850 with PCM5142 codec");
454 MODULE_LICENSE("GPL v2");