Merge tag 'powerpc-5.11-3' of git://git.kernel.org/pub/scm/linux/kernel/git/powerpc...
[linux/fpc-iii.git] / sound / soc / atmel / tse850-pcm5142.c
blob59e2edb22b3adc809c19e2adc902aee153e1ee85
1 // SPDX-License-Identifier: GPL-2.0
2 //
3 // TSE-850 audio - ASoC driver for the Axentia TSE-850 with a PCM5142 codec
4 //
5 // Copyright (C) 2016 Axentia Technologies AB
6 //
7 // Author: Peter Rosin <peda@axentia.se>
8 //
9 // loop1 relays
10 // IN1 +---o +------------+ o---+ OUT1
11 // \ /
12 // + +
13 // | / |
14 // +--o +--. |
15 // | add | |
16 // | V |
17 // | .---. |
18 // DAC +----------->|Sum|---+
19 // | '---' |
20 // | |
21 // + +
23 // IN2 +---o--+------------+--o---+ OUT2
24 // loop2 relays
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>
40 #include <linux/of.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>
48 struct tse850_priv {
49 struct gpio_desc *add;
50 struct gpio_desc *loop1;
51 struct gpio_desc *loop2;
53 struct regulator *ana;
55 int add_cache;
56 int loop1_cache;
57 int loop2_cache;
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;
69 return 0;
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];
81 if (val >= e->items)
82 return -EINVAL;
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;
99 return 0;
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];
111 if (val >= e->items)
112 return -EINVAL;
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;
129 return 0;
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)
141 return 0;
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);
151 return 1;
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);
160 int ret;
162 ret = regulator_get_voltage(tse850->ana);
163 if (ret < 0)
164 return ret;
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)
171 * ...
172 * 18.5V-19.5V to "19V" (enum 8)
173 * 19.5V- to "20V" (enum 9)
175 if (ret < 11000000)
176 ret = 11000000;
177 else if (ret > 20000000)
178 ret = 20000000;
179 ret -= 11000000;
180 ret = (ret + 500000) / 1000000;
182 ucontrol->value.enumerated.item[0] = ret;
184 return 0;
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];
195 int ret;
197 if (uV >= e->items)
198 return -EINVAL;
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).
209 if (uV)
210 uV = 11000000 + (1000000 * uV);
211 else
212 uV = 2000000;
214 ret = regulator_set_voltage(tse850->ana, uV, uV);
215 if (ret < 0)
216 return ret;
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, \
235 .get = xget, \
236 .put = xput, \
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
273 * are routed.
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 = {
303 .name = "TSE-850",
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,
315 .num_links = 1,
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;
329 if (!np) {
330 dev_err(&pdev->dev, "only device tree supported\n");
331 return -EINVAL;
334 cpu_np = of_parse_phandle(np, "axentia,cpu-dai", 0);
335 if (!cpu_np) {
336 dev_err(&pdev->dev, "failed to get cpu dai\n");
337 return -EINVAL;
339 dailink->cpus->of_node = cpu_np;
340 dailink->platforms->of_node = cpu_np;
341 of_node_put(cpu_np);
343 codec_np = of_parse_phandle(np, "axentia,audio-codec", 0);
344 if (!codec_np) {
345 dev_err(&pdev->dev, "failed to get codec info\n");
346 return -EINVAL;
348 dailink->codecs->of_node = codec_np;
349 of_node_put(codec_np);
351 return 0;
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;
359 int ret;
361 tse850 = devm_kzalloc(dev, sizeof(*tse850), GFP_KERNEL);
362 if (!tse850)
363 return -ENOMEM;
365 snd_soc_card_set_drvdata(card, tse850);
367 ret = tse850_dt_init(pdev);
368 if (ret) {
369 dev_err(dev, "failed to init dt info\n");
370 return ret;
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);
405 if (ret < 0) {
406 dev_err(dev, "failed to enable the 'ana' regulator\n");
407 return ret;
410 ret = snd_soc_register_card(card);
411 if (ret) {
412 dev_err(dev, "snd_soc_register_card failed\n");
413 goto err_disable_ana;
416 return 0;
418 err_disable_ana:
419 regulator_disable(tse850->ana);
420 return ret;
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);
431 return 0;
434 static const struct of_device_id tse850_dt_ids[] = {
435 { .compatible = "axentia,tse850-pcm5142", },
436 { /* sentinel */ }
438 MODULE_DEVICE_TABLE(of, tse850_dt_ids);
440 static struct platform_driver tse850_driver = {
441 .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");