2 * TSE-850 audio - ASoC driver for the Axentia TSE-850 with a PCM5142 codec
4 * Copyright (C) 2016 Axentia Technologies AB
6 * Author: Peter Rosin <peda@axentia.se>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
15 * IN1 +---o +------------+ o---+ OUT1
23 * DAC +----------->|Sum|---+
28 * IN2 +---o--+------------+--o---+ OUT2
31 * The 'loop1' gpio pin controlls two relays, which are either in loop
32 * position, meaning that input and output are directly connected, or
33 * they are in mixer position, meaning that the signal is passed through
34 * the 'Sum' mixer. Similarly for 'loop2'.
36 * In the above, the 'loop1' relays are inactive, thus feeding IN1 to the
37 * mixer (if 'add' is active) and feeding the mixer output to OUT1. The
38 * 'loop2' relays are active, short-cutting the TSE-850 from channel 2.
39 * IN1, IN2, OUT1 and OUT2 are TSE-850 connectors and DAC is the PCB name
40 * of the (filtered) output from the PCM5142 codec.
43 #include <linux/clk.h>
44 #include <linux/gpio.h>
45 #include <linux/module.h>
47 #include <linux/of_device.h>
48 #include <linux/of_gpio.h>
49 #include <linux/regulator/consumer.h>
51 #include <sound/soc.h>
52 #include <sound/pcm_params.h>
55 struct gpio_desc
*add
;
56 struct gpio_desc
*loop1
;
57 struct gpio_desc
*loop2
;
59 struct regulator
*ana
;
66 static int tse850_get_mux1(struct snd_kcontrol
*kctrl
,
67 struct snd_ctl_elem_value
*ucontrol
)
69 struct snd_soc_dapm_context
*dapm
= snd_soc_dapm_kcontrol_dapm(kctrl
);
70 struct snd_soc_card
*card
= dapm
->card
;
71 struct tse850_priv
*tse850
= snd_soc_card_get_drvdata(card
);
73 ucontrol
->value
.enumerated
.item
[0] = tse850
->loop1_cache
;
78 static int tse850_put_mux1(struct snd_kcontrol
*kctrl
,
79 struct snd_ctl_elem_value
*ucontrol
)
81 struct snd_soc_dapm_context
*dapm
= snd_soc_dapm_kcontrol_dapm(kctrl
);
82 struct snd_soc_card
*card
= dapm
->card
;
83 struct tse850_priv
*tse850
= snd_soc_card_get_drvdata(card
);
84 struct soc_enum
*e
= (struct soc_enum
*)kctrl
->private_value
;
85 unsigned int val
= ucontrol
->value
.enumerated
.item
[0];
90 gpiod_set_value_cansleep(tse850
->loop1
, val
);
91 tse850
->loop1_cache
= val
;
93 return snd_soc_dapm_put_enum_double(kctrl
, ucontrol
);
96 static int tse850_get_mux2(struct snd_kcontrol
*kctrl
,
97 struct snd_ctl_elem_value
*ucontrol
)
99 struct snd_soc_dapm_context
*dapm
= snd_soc_dapm_kcontrol_dapm(kctrl
);
100 struct snd_soc_card
*card
= dapm
->card
;
101 struct tse850_priv
*tse850
= snd_soc_card_get_drvdata(card
);
103 ucontrol
->value
.enumerated
.item
[0] = tse850
->loop2_cache
;
108 static int tse850_put_mux2(struct snd_kcontrol
*kctrl
,
109 struct snd_ctl_elem_value
*ucontrol
)
111 struct snd_soc_dapm_context
*dapm
= snd_soc_dapm_kcontrol_dapm(kctrl
);
112 struct snd_soc_card
*card
= dapm
->card
;
113 struct tse850_priv
*tse850
= snd_soc_card_get_drvdata(card
);
114 struct soc_enum
*e
= (struct soc_enum
*)kctrl
->private_value
;
115 unsigned int val
= ucontrol
->value
.enumerated
.item
[0];
120 gpiod_set_value_cansleep(tse850
->loop2
, val
);
121 tse850
->loop2_cache
= val
;
123 return snd_soc_dapm_put_enum_double(kctrl
, ucontrol
);
126 int tse850_get_mix(struct snd_kcontrol
*kctrl
,
127 struct snd_ctl_elem_value
*ucontrol
)
129 struct snd_soc_dapm_context
*dapm
= snd_soc_dapm_kcontrol_dapm(kctrl
);
130 struct snd_soc_card
*card
= dapm
->card
;
131 struct tse850_priv
*tse850
= snd_soc_card_get_drvdata(card
);
133 ucontrol
->value
.enumerated
.item
[0] = tse850
->add_cache
;
138 int tse850_put_mix(struct snd_kcontrol
*kctrl
,
139 struct snd_ctl_elem_value
*ucontrol
)
141 struct snd_soc_dapm_context
*dapm
= snd_soc_dapm_kcontrol_dapm(kctrl
);
142 struct snd_soc_card
*card
= dapm
->card
;
143 struct tse850_priv
*tse850
= snd_soc_card_get_drvdata(card
);
144 int connect
= !!ucontrol
->value
.integer
.value
[0];
146 if (tse850
->add_cache
== connect
)
150 * Hmmm, this gpiod_set_value_cansleep call should probably happen
151 * inside snd_soc_dapm_mixer_update_power in the loop.
153 gpiod_set_value_cansleep(tse850
->add
, connect
);
154 tse850
->add_cache
= connect
;
156 snd_soc_dapm_mixer_update_power(dapm
, kctrl
, connect
, NULL
);
160 int tse850_get_ana(struct snd_kcontrol
*kctrl
,
161 struct snd_ctl_elem_value
*ucontrol
)
163 struct snd_soc_dapm_context
*dapm
= snd_soc_dapm_kcontrol_dapm(kctrl
);
164 struct snd_soc_card
*card
= dapm
->card
;
165 struct tse850_priv
*tse850
= snd_soc_card_get_drvdata(card
);
168 ret
= regulator_get_voltage(tse850
->ana
);
173 * Map regulator output values like so:
174 * -11.5V to "Low" (enum 0)
175 * 11.5V-12.5V to "12V" (enum 1)
176 * 12.5V-13.5V to "13V" (enum 2)
178 * 18.5V-19.5V to "19V" (enum 8)
179 * 19.5V- to "20V" (enum 9)
183 else if (ret
> 20000000)
186 ret
= (ret
+ 500000) / 1000000;
188 ucontrol
->value
.enumerated
.item
[0] = ret
;
193 int tse850_put_ana(struct snd_kcontrol
*kctrl
,
194 struct snd_ctl_elem_value
*ucontrol
)
196 struct snd_soc_dapm_context
*dapm
= snd_soc_dapm_kcontrol_dapm(kctrl
);
197 struct snd_soc_card
*card
= dapm
->card
;
198 struct tse850_priv
*tse850
= snd_soc_card_get_drvdata(card
);
199 struct soc_enum
*e
= (struct soc_enum
*)kctrl
->private_value
;
200 unsigned int uV
= ucontrol
->value
.enumerated
.item
[0];
207 * Map enum zero (Low) to 2 volts on the regulator, do this since
208 * the ana regulator is supplied by the system 12V voltage and
209 * requesting anything below the system voltage causes the system
210 * voltage to be passed through the regulator. Also, the ana
211 * regulator induces noise when requesting voltages near the
212 * system voltage. So, by mapping Low to 2V, that noise is
213 * eliminated when all that is needed is 12V (the system voltage).
216 uV
= 11000000 + (1000000 * uV
);
220 ret
= regulator_set_voltage(tse850
->ana
, uV
, uV
);
224 return snd_soc_dapm_put_enum_double(kctrl
, ucontrol
);
227 static const char * const mux_text
[] = { "Mixer", "Loop" };
229 static const struct soc_enum mux_enum
=
230 SOC_ENUM_SINGLE(SND_SOC_NOPM
, 0, ARRAY_SIZE(mux_text
), mux_text
);
232 static const struct snd_kcontrol_new mux1
=
233 SOC_DAPM_ENUM_EXT("MUX1", mux_enum
, tse850_get_mux1
, tse850_put_mux1
);
235 static const struct snd_kcontrol_new mux2
=
236 SOC_DAPM_ENUM_EXT("MUX2", mux_enum
, tse850_get_mux2
, tse850_put_mux2
);
238 #define TSE850_DAPM_SINGLE_EXT(xname, reg, shift, max, invert, xget, xput) \
239 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
240 .info = snd_soc_info_volsw, \
243 .private_value = SOC_SINGLE_VALUE(reg, shift, max, invert, 0) }
245 static const struct snd_kcontrol_new mix
[] = {
246 TSE850_DAPM_SINGLE_EXT("IN Switch", SND_SOC_NOPM
, 0, 1, 0,
247 tse850_get_mix
, tse850_put_mix
),
250 static const char * const ana_text
[] = {
251 "Low", "12V", "13V", "14V", "15V", "16V", "17V", "18V", "19V", "20V"
254 static const struct soc_enum ana_enum
=
255 SOC_ENUM_SINGLE(SND_SOC_NOPM
, 0, ARRAY_SIZE(ana_text
), ana_text
);
257 static const struct snd_kcontrol_new out
=
258 SOC_DAPM_ENUM_EXT("ANA", ana_enum
, tse850_get_ana
, tse850_put_ana
);
260 static const struct snd_soc_dapm_widget tse850_dapm_widgets
[] = {
261 SND_SOC_DAPM_LINE("OUT1", NULL
),
262 SND_SOC_DAPM_LINE("OUT2", NULL
),
263 SND_SOC_DAPM_LINE("IN1", NULL
),
264 SND_SOC_DAPM_LINE("IN2", NULL
),
265 SND_SOC_DAPM_INPUT("DAC"),
266 SND_SOC_DAPM_AIF_IN("AIFINL", "Playback", 0, SND_SOC_NOPM
, 0, 0),
267 SND_SOC_DAPM_AIF_IN("AIFINR", "Playback", 1, SND_SOC_NOPM
, 0, 0),
268 SOC_MIXER_ARRAY("MIX", SND_SOC_NOPM
, 0, 0, mix
),
269 SND_SOC_DAPM_MUX("MUX1", SND_SOC_NOPM
, 0, 0, &mux1
),
270 SND_SOC_DAPM_MUX("MUX2", SND_SOC_NOPM
, 0, 0, &mux2
),
271 SND_SOC_DAPM_OUT_DRV("OUT", SND_SOC_NOPM
, 0, 0, &out
, 1),
275 * These connections are not entirely correct, since both IN1 and IN2
276 * are always fed to MIX (if the "IN switch" is set so), i.e. without
277 * regard to the loop1 and loop2 relays that according to this only
278 * control MUX1 and MUX2 but in fact also control how the input signals
280 * But, 1) I don't know how to do it right, and 2) it doesn't seem to
281 * matter in practice since nothing is powered in those sections anyway.
283 static const struct snd_soc_dapm_route tse850_intercon
[] = {
284 { "OUT1", NULL
, "MUX1" },
285 { "OUT2", NULL
, "MUX2" },
287 { "MUX1", "Loop", "IN1" },
288 { "MUX1", "Mixer", "OUT" },
290 { "MUX2", "Loop", "IN2" },
291 { "MUX2", "Mixer", "OUT" },
293 { "OUT", NULL
, "MIX" },
295 { "MIX", NULL
, "DAC" },
296 { "MIX", "IN Switch", "IN1" },
297 { "MIX", "IN Switch", "IN2" },
299 /* connect board input to the codec left channel output pin */
300 { "DAC", NULL
, "OUTL" },
303 static struct snd_soc_dai_link tse850_dailink
= {
305 .stream_name
= "TSE-850-PCM",
306 .codec_dai_name
= "pcm512x-hifi",
307 .dai_fmt
= SND_SOC_DAIFMT_I2S
308 | SND_SOC_DAIFMT_NB_NF
309 | SND_SOC_DAIFMT_CBM_CFS
,
312 static struct snd_soc_card tse850_card
= {
313 .name
= "TSE-850-ASoC",
314 .owner
= THIS_MODULE
,
315 .dai_link
= &tse850_dailink
,
317 .dapm_widgets
= tse850_dapm_widgets
,
318 .num_dapm_widgets
= ARRAY_SIZE(tse850_dapm_widgets
),
319 .dapm_routes
= tse850_intercon
,
320 .num_dapm_routes
= ARRAY_SIZE(tse850_intercon
),
321 .fully_routed
= true,
324 static int tse850_dt_init(struct platform_device
*pdev
)
326 struct device_node
*np
= pdev
->dev
.of_node
;
327 struct device_node
*codec_np
, *cpu_np
;
328 struct snd_soc_dai_link
*dailink
= &tse850_dailink
;
331 dev_err(&pdev
->dev
, "only device tree supported\n");
335 cpu_np
= of_parse_phandle(np
, "axentia,cpu-dai", 0);
337 dev_err(&pdev
->dev
, "failed to get cpu dai\n");
340 dailink
->cpu_of_node
= cpu_np
;
341 dailink
->platform_of_node
= cpu_np
;
344 codec_np
= of_parse_phandle(np
, "axentia,audio-codec", 0);
346 dev_err(&pdev
->dev
, "failed to get codec info\n");
349 dailink
->codec_of_node
= codec_np
;
350 of_node_put(codec_np
);
355 static int tse850_probe(struct platform_device
*pdev
)
357 struct snd_soc_card
*card
= &tse850_card
;
358 struct device
*dev
= card
->dev
= &pdev
->dev
;
359 struct tse850_priv
*tse850
;
362 tse850
= devm_kzalloc(dev
, sizeof(*tse850
), GFP_KERNEL
);
366 snd_soc_card_set_drvdata(card
, tse850
);
368 ret
= tse850_dt_init(pdev
);
370 dev_err(dev
, "failed to init dt info\n");
374 tse850
->add
= devm_gpiod_get(dev
, "axentia,add", GPIOD_OUT_HIGH
);
375 if (IS_ERR(tse850
->add
)) {
376 if (PTR_ERR(tse850
->add
) != -EPROBE_DEFER
)
377 dev_err(dev
, "failed to get 'add' gpio\n");
378 return PTR_ERR(tse850
->add
);
380 tse850
->add_cache
= 1;
382 tse850
->loop1
= devm_gpiod_get(dev
, "axentia,loop1", GPIOD_OUT_HIGH
);
383 if (IS_ERR(tse850
->loop1
)) {
384 if (PTR_ERR(tse850
->loop1
) != -EPROBE_DEFER
)
385 dev_err(dev
, "failed to get 'loop1' gpio\n");
386 return PTR_ERR(tse850
->loop1
);
388 tse850
->loop1_cache
= 1;
390 tse850
->loop2
= devm_gpiod_get(dev
, "axentia,loop2", GPIOD_OUT_HIGH
);
391 if (IS_ERR(tse850
->loop2
)) {
392 if (PTR_ERR(tse850
->loop2
) != -EPROBE_DEFER
)
393 dev_err(dev
, "failed to get 'loop2' gpio\n");
394 return PTR_ERR(tse850
->loop2
);
396 tse850
->loop2_cache
= 1;
398 tse850
->ana
= devm_regulator_get(dev
, "axentia,ana");
399 if (IS_ERR(tse850
->ana
)) {
400 if (PTR_ERR(tse850
->ana
) != -EPROBE_DEFER
)
401 dev_err(dev
, "failed to get 'ana' regulator\n");
402 return PTR_ERR(tse850
->ana
);
405 ret
= regulator_enable(tse850
->ana
);
407 dev_err(dev
, "failed to enable the 'ana' regulator\n");
411 ret
= snd_soc_register_card(card
);
413 dev_err(dev
, "snd_soc_register_card failed\n");
414 goto err_disable_ana
;
420 regulator_disable(tse850
->ana
);
424 static int tse850_remove(struct platform_device
*pdev
)
426 struct snd_soc_card
*card
= platform_get_drvdata(pdev
);
427 struct tse850_priv
*tse850
= snd_soc_card_get_drvdata(card
);
429 snd_soc_unregister_card(card
);
430 regulator_disable(tse850
->ana
);
435 static const struct of_device_id tse850_dt_ids
[] = {
436 { .compatible
= "axentia,tse850-pcm5142", },
439 MODULE_DEVICE_TABLE(of
, tse850_dt_ids
);
441 static struct platform_driver tse850_driver
= {
443 .name
= "axentia-tse850-pcm5142",
444 .of_match_table
= of_match_ptr(tse850_dt_ids
),
446 .probe
= tse850_probe
,
447 .remove
= tse850_remove
,
450 module_platform_driver(tse850_driver
);
452 /* Module information */
453 MODULE_AUTHOR("Peter Rosin <peda@axentia.se>");
454 MODULE_DESCRIPTION("ALSA SoC driver for TSE-850 with PCM5142 codec");
455 MODULE_LICENSE("GPL");