sh_eth: fix EESIPR values for SH77{34|63}
[linux/fpc-iii.git] / sound / soc / atmel / tse850-pcm5142.c
blobac6a814c8ecf3658ffb20e1aa5bc7dce63b039c9
1 /*
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.
14 * loop1 relays
15 * IN1 +---o +------------+ o---+ OUT1
16 * \ /
17 * + +
18 * | / |
19 * +--o +--. |
20 * | add | |
21 * | V |
22 * | .---. |
23 * DAC +----------->|Sum|---+
24 * | '---' |
25 * | |
26 * + +
28 * IN2 +---o--+------------+--o---+ OUT2
29 * loop2 relays
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>
46 #include <linux/of.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>
54 #include "atmel_ssc_dai.h"
56 struct tse850_priv {
57 int ssc_id;
59 struct gpio_desc *add;
60 struct gpio_desc *loop1;
61 struct gpio_desc *loop2;
63 struct regulator *ana;
65 int add_cache;
66 int loop1_cache;
67 int loop2_cache;
70 static int tse850_get_mux1(struct snd_kcontrol *kctrl,
71 struct snd_ctl_elem_value *ucontrol)
73 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kctrl);
74 struct snd_soc_card *card = dapm->card;
75 struct tse850_priv *tse850 = snd_soc_card_get_drvdata(card);
77 ucontrol->value.enumerated.item[0] = tse850->loop1_cache;
79 return 0;
82 static int tse850_put_mux1(struct snd_kcontrol *kctrl,
83 struct snd_ctl_elem_value *ucontrol)
85 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kctrl);
86 struct snd_soc_card *card = dapm->card;
87 struct tse850_priv *tse850 = snd_soc_card_get_drvdata(card);
88 struct soc_enum *e = (struct soc_enum *)kctrl->private_value;
89 unsigned int val = ucontrol->value.enumerated.item[0];
91 if (val >= e->items)
92 return -EINVAL;
94 gpiod_set_value_cansleep(tse850->loop1, val);
95 tse850->loop1_cache = val;
97 return snd_soc_dapm_put_enum_double(kctrl, ucontrol);
100 static int tse850_get_mux2(struct snd_kcontrol *kctrl,
101 struct snd_ctl_elem_value *ucontrol)
103 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kctrl);
104 struct snd_soc_card *card = dapm->card;
105 struct tse850_priv *tse850 = snd_soc_card_get_drvdata(card);
107 ucontrol->value.enumerated.item[0] = tse850->loop2_cache;
109 return 0;
112 static int tse850_put_mux2(struct snd_kcontrol *kctrl,
113 struct snd_ctl_elem_value *ucontrol)
115 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kctrl);
116 struct snd_soc_card *card = dapm->card;
117 struct tse850_priv *tse850 = snd_soc_card_get_drvdata(card);
118 struct soc_enum *e = (struct soc_enum *)kctrl->private_value;
119 unsigned int val = ucontrol->value.enumerated.item[0];
121 if (val >= e->items)
122 return -EINVAL;
124 gpiod_set_value_cansleep(tse850->loop2, val);
125 tse850->loop2_cache = val;
127 return snd_soc_dapm_put_enum_double(kctrl, ucontrol);
130 int tse850_get_mix(struct snd_kcontrol *kctrl,
131 struct snd_ctl_elem_value *ucontrol)
133 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kctrl);
134 struct snd_soc_card *card = dapm->card;
135 struct tse850_priv *tse850 = snd_soc_card_get_drvdata(card);
137 ucontrol->value.enumerated.item[0] = tse850->add_cache;
139 return 0;
142 int tse850_put_mix(struct snd_kcontrol *kctrl,
143 struct snd_ctl_elem_value *ucontrol)
145 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kctrl);
146 struct snd_soc_card *card = dapm->card;
147 struct tse850_priv *tse850 = snd_soc_card_get_drvdata(card);
148 int connect = !!ucontrol->value.integer.value[0];
150 if (tse850->add_cache == connect)
151 return 0;
154 * Hmmm, this gpiod_set_value_cansleep call should probably happen
155 * inside snd_soc_dapm_mixer_update_power in the loop.
157 gpiod_set_value_cansleep(tse850->add, connect);
158 tse850->add_cache = connect;
160 snd_soc_dapm_mixer_update_power(dapm, kctrl, connect, NULL);
161 return 1;
164 int tse850_get_ana(struct snd_kcontrol *kctrl,
165 struct snd_ctl_elem_value *ucontrol)
167 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kctrl);
168 struct snd_soc_card *card = dapm->card;
169 struct tse850_priv *tse850 = snd_soc_card_get_drvdata(card);
170 int ret;
172 ret = regulator_get_voltage(tse850->ana);
173 if (ret < 0)
174 return ret;
177 * Map regulator output values like so:
178 * -11.5V to "Low" (enum 0)
179 * 11.5V-12.5V to "12V" (enum 1)
180 * 12.5V-13.5V to "13V" (enum 2)
181 * ...
182 * 18.5V-19.5V to "19V" (enum 8)
183 * 19.5V- to "20V" (enum 9)
185 if (ret < 11000000)
186 ret = 11000000;
187 else if (ret > 20000000)
188 ret = 20000000;
189 ret -= 11000000;
190 ret = (ret + 500000) / 1000000;
192 ucontrol->value.enumerated.item[0] = ret;
194 return 0;
197 int tse850_put_ana(struct snd_kcontrol *kctrl,
198 struct snd_ctl_elem_value *ucontrol)
200 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kctrl);
201 struct snd_soc_card *card = dapm->card;
202 struct tse850_priv *tse850 = snd_soc_card_get_drvdata(card);
203 struct soc_enum *e = (struct soc_enum *)kctrl->private_value;
204 unsigned int uV = ucontrol->value.enumerated.item[0];
205 int ret;
207 if (uV >= e->items)
208 return -EINVAL;
211 * Map enum zero (Low) to 2 volts on the regulator, do this since
212 * the ana regulator is supplied by the system 12V voltage and
213 * requesting anything below the system voltage causes the system
214 * voltage to be passed through the regulator. Also, the ana
215 * regulator induces noise when requesting voltages near the
216 * system voltage. So, by mapping Low to 2V, that noise is
217 * eliminated when all that is needed is 12V (the system voltage).
219 if (uV)
220 uV = 11000000 + (1000000 * uV);
221 else
222 uV = 2000000;
224 ret = regulator_set_voltage(tse850->ana, uV, uV);
225 if (ret < 0)
226 return ret;
228 return snd_soc_dapm_put_enum_double(kctrl, ucontrol);
231 static const char * const mux_text[] = { "Mixer", "Loop" };
233 static const struct soc_enum mux_enum =
234 SOC_ENUM_SINGLE(SND_SOC_NOPM, 0, 2, mux_text);
236 static const struct snd_kcontrol_new mux1 =
237 SOC_DAPM_ENUM_EXT("MUX1", mux_enum, tse850_get_mux1, tse850_put_mux1);
239 static const struct snd_kcontrol_new mux2 =
240 SOC_DAPM_ENUM_EXT("MUX2", mux_enum, tse850_get_mux2, tse850_put_mux2);
242 #define TSE850_DAPM_SINGLE_EXT(xname, reg, shift, max, invert, xget, xput) \
243 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
244 .info = snd_soc_info_volsw, \
245 .get = xget, \
246 .put = xput, \
247 .private_value = SOC_SINGLE_VALUE(reg, shift, max, invert, 0) }
249 static const struct snd_kcontrol_new mix[] = {
250 TSE850_DAPM_SINGLE_EXT("IN Switch", SND_SOC_NOPM, 0, 1, 0,
251 tse850_get_mix, tse850_put_mix),
254 static const char * const ana_text[] = {
255 "Low", "12V", "13V", "14V", "15V", "16V", "17V", "18V", "19V", "20V"
258 static const struct soc_enum ana_enum =
259 SOC_ENUM_SINGLE(SND_SOC_NOPM, 0, 9, ana_text);
261 static const struct snd_kcontrol_new out =
262 SOC_DAPM_ENUM_EXT("ANA", ana_enum, tse850_get_ana, tse850_put_ana);
264 static const struct snd_soc_dapm_widget tse850_dapm_widgets[] = {
265 SND_SOC_DAPM_LINE("OUT1", NULL),
266 SND_SOC_DAPM_LINE("OUT2", NULL),
267 SND_SOC_DAPM_LINE("IN1", NULL),
268 SND_SOC_DAPM_LINE("IN2", NULL),
269 SND_SOC_DAPM_INPUT("DAC"),
270 SND_SOC_DAPM_AIF_IN("AIFINL", "Playback", 0, SND_SOC_NOPM, 0, 0),
271 SND_SOC_DAPM_AIF_IN("AIFINR", "Playback", 1, SND_SOC_NOPM, 0, 0),
272 SOC_MIXER_ARRAY("MIX", SND_SOC_NOPM, 0, 0, mix),
273 SND_SOC_DAPM_MUX("MUX1", SND_SOC_NOPM, 0, 0, &mux1),
274 SND_SOC_DAPM_MUX("MUX2", SND_SOC_NOPM, 0, 0, &mux2),
275 SND_SOC_DAPM_OUT_DRV("OUT", SND_SOC_NOPM, 0, 0, &out, 1),
279 * These connections are not entirely correct, since both IN1 and IN2
280 * are always fed to MIX (if the "IN switch" is set so), i.e. without
281 * regard to the loop1 and loop2 relays that according to this only
282 * control MUX1 and MUX2 but in fact also control how the input signals
283 * are routed.
284 * But, 1) I don't know how to do it right, and 2) it doesn't seem to
285 * matter in practice since nothing is powered in those sections anyway.
287 static const struct snd_soc_dapm_route tse850_intercon[] = {
288 { "OUT1", NULL, "MUX1" },
289 { "OUT2", NULL, "MUX2" },
291 { "MUX1", "Loop", "IN1" },
292 { "MUX1", "Mixer", "OUT" },
294 { "MUX2", "Loop", "IN2" },
295 { "MUX2", "Mixer", "OUT" },
297 { "OUT", NULL, "MIX" },
299 { "MIX", NULL, "DAC" },
300 { "MIX", "IN Switch", "IN1" },
301 { "MIX", "IN Switch", "IN2" },
303 /* connect board input to the codec left channel output pin */
304 { "DAC", NULL, "OUTL" },
307 static struct snd_soc_dai_link tse850_dailink = {
308 .name = "TSE-850",
309 .stream_name = "TSE-850-PCM",
310 .codec_dai_name = "pcm512x-hifi",
311 .dai_fmt = SND_SOC_DAIFMT_I2S
312 | SND_SOC_DAIFMT_NB_NF
313 | SND_SOC_DAIFMT_CBM_CFS,
316 static struct snd_soc_card tse850_card = {
317 .name = "TSE-850-ASoC",
318 .owner = THIS_MODULE,
319 .dai_link = &tse850_dailink,
320 .num_links = 1,
321 .dapm_widgets = tse850_dapm_widgets,
322 .num_dapm_widgets = ARRAY_SIZE(tse850_dapm_widgets),
323 .dapm_routes = tse850_intercon,
324 .num_dapm_routes = ARRAY_SIZE(tse850_intercon),
325 .fully_routed = true,
328 static int tse850_dt_init(struct platform_device *pdev)
330 struct device_node *np = pdev->dev.of_node;
331 struct device_node *codec_np, *cpu_np;
332 struct snd_soc_card *card = &tse850_card;
333 struct snd_soc_dai_link *dailink = &tse850_dailink;
334 struct tse850_priv *tse850 = snd_soc_card_get_drvdata(card);
336 if (!np) {
337 dev_err(&pdev->dev, "only device tree supported\n");
338 return -EINVAL;
341 cpu_np = of_parse_phandle(np, "axentia,ssc-controller", 0);
342 if (!cpu_np) {
343 dev_err(&pdev->dev, "failed to get dai and pcm info\n");
344 return -EINVAL;
346 dailink->cpu_of_node = cpu_np;
347 dailink->platform_of_node = cpu_np;
348 tse850->ssc_id = of_alias_get_id(cpu_np, "ssc");
349 of_node_put(cpu_np);
351 codec_np = of_parse_phandle(np, "axentia,audio-codec", 0);
352 if (!codec_np) {
353 dev_err(&pdev->dev, "failed to get codec info\n");
354 return -EINVAL;
356 dailink->codec_of_node = codec_np;
357 of_node_put(codec_np);
359 return 0;
362 static int tse850_probe(struct platform_device *pdev)
364 struct snd_soc_card *card = &tse850_card;
365 struct device *dev = card->dev = &pdev->dev;
366 struct tse850_priv *tse850;
367 int ret;
369 tse850 = devm_kzalloc(dev, sizeof(*tse850), GFP_KERNEL);
370 if (!tse850)
371 return -ENOMEM;
373 snd_soc_card_set_drvdata(card, tse850);
375 ret = tse850_dt_init(pdev);
376 if (ret) {
377 dev_err(dev, "failed to init dt info\n");
378 return ret;
381 tse850->add = devm_gpiod_get(dev, "axentia,add", GPIOD_OUT_HIGH);
382 if (IS_ERR(tse850->add)) {
383 if (PTR_ERR(tse850->add) != -EPROBE_DEFER)
384 dev_err(dev, "failed to get 'add' gpio\n");
385 return PTR_ERR(tse850->add);
387 tse850->add_cache = 1;
389 tse850->loop1 = devm_gpiod_get(dev, "axentia,loop1", GPIOD_OUT_HIGH);
390 if (IS_ERR(tse850->loop1)) {
391 if (PTR_ERR(tse850->loop1) != -EPROBE_DEFER)
392 dev_err(dev, "failed to get 'loop1' gpio\n");
393 return PTR_ERR(tse850->loop1);
395 tse850->loop1_cache = 1;
397 tse850->loop2 = devm_gpiod_get(dev, "axentia,loop2", GPIOD_OUT_HIGH);
398 if (IS_ERR(tse850->loop2)) {
399 if (PTR_ERR(tse850->loop2) != -EPROBE_DEFER)
400 dev_err(dev, "failed to get 'loop2' gpio\n");
401 return PTR_ERR(tse850->loop2);
403 tse850->loop2_cache = 1;
405 tse850->ana = devm_regulator_get(dev, "axentia,ana");
406 if (IS_ERR(tse850->ana)) {
407 if (PTR_ERR(tse850->ana) != -EPROBE_DEFER)
408 dev_err(dev, "failed to get 'ana' regulator\n");
409 return PTR_ERR(tse850->ana);
412 ret = regulator_enable(tse850->ana);
413 if (ret < 0) {
414 dev_err(dev, "failed to enable the 'ana' regulator\n");
415 return ret;
418 ret = atmel_ssc_set_audio(tse850->ssc_id);
419 if (ret != 0) {
420 dev_err(dev,
421 "failed to set SSC %d for audio\n", tse850->ssc_id);
422 goto err_disable_ana;
425 ret = snd_soc_register_card(card);
426 if (ret) {
427 dev_err(dev, "snd_soc_register_card failed\n");
428 goto err_put_audio;
431 return 0;
433 err_put_audio:
434 atmel_ssc_put_audio(tse850->ssc_id);
435 err_disable_ana:
436 regulator_disable(tse850->ana);
437 return ret;
440 static int tse850_remove(struct platform_device *pdev)
442 struct snd_soc_card *card = platform_get_drvdata(pdev);
443 struct tse850_priv *tse850 = snd_soc_card_get_drvdata(card);
445 snd_soc_unregister_card(card);
446 atmel_ssc_put_audio(tse850->ssc_id);
447 regulator_disable(tse850->ana);
449 return 0;
452 static const struct of_device_id tse850_dt_ids[] = {
453 { .compatible = "axentia,tse850-pcm5142", },
454 { /* sentinel */ }
456 MODULE_DEVICE_TABLE(of, tse850_dt_ids);
458 static struct platform_driver tse850_driver = {
459 .driver = {
460 .name = "axentia-tse850-pcm5142",
461 .of_match_table = of_match_ptr(tse850_dt_ids),
463 .probe = tse850_probe,
464 .remove = tse850_remove,
467 module_platform_driver(tse850_driver);
469 /* Module information */
470 MODULE_AUTHOR("Peter Rosin <peda@axentia.se>");
471 MODULE_DESCRIPTION("ALSA SoC driver for TSE-850 with PCM5142 codec");
472 MODULE_LICENSE("GPL");