Merge remote-tracking branches 'asoc/topic/adau17x1', 'asoc/topic/adsp', 'asoc/topic...
[platform/kernel/linux-exynos.git] / sound / soc / atmel / tse850-pcm5142.c
1 /*
2  * TSE-850 audio - ASoC driver for the Axentia TSE-850 with a PCM5142 codec
3  *
4  * Copyright (C) 2016 Axentia Technologies AB
5  *
6  * Author: Peter Rosin <peda@axentia.se>
7  *
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.
11  */
12
13 /*
14  *               loop1 relays
15  *   IN1 +---o  +------------+  o---+ OUT1
16  *            \                /
17  *             +              +
18  *             |   /          |
19  *             +--o  +--.     |
20  *             |  add   |     |
21  *             |        V     |
22  *             |      .---.   |
23  *   DAC +----------->|Sum|---+
24  *             |      '---'   |
25  *             |              |
26  *             +              +
27  *
28  *   IN2 +---o--+------------+--o---+ OUT2
29  *               loop2 relays
30  *
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'.
35  *
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.
41  */
42
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>
50
51 #include <sound/soc.h>
52 #include <sound/pcm_params.h>
53
54 #include "atmel_ssc_dai.h"
55
56 struct tse850_priv {
57         int ssc_id;
58
59         struct gpio_desc *add;
60         struct gpio_desc *loop1;
61         struct gpio_desc *loop2;
62
63         struct regulator *ana;
64
65         int add_cache;
66         int loop1_cache;
67         int loop2_cache;
68 };
69
70 static int tse850_get_mux1(struct snd_kcontrol *kctrl,
71                            struct snd_ctl_elem_value *ucontrol)
72 {
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);
76
77         ucontrol->value.enumerated.item[0] = tse850->loop1_cache;
78
79         return 0;
80 }
81
82 static int tse850_put_mux1(struct snd_kcontrol *kctrl,
83                            struct snd_ctl_elem_value *ucontrol)
84 {
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];
90
91         if (val >= e->items)
92                 return -EINVAL;
93
94         gpiod_set_value_cansleep(tse850->loop1, val);
95         tse850->loop1_cache = val;
96
97         return snd_soc_dapm_put_enum_double(kctrl, ucontrol);
98 }
99
100 static int tse850_get_mux2(struct snd_kcontrol *kctrl,
101                            struct snd_ctl_elem_value *ucontrol)
102 {
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);
106
107         ucontrol->value.enumerated.item[0] = tse850->loop2_cache;
108
109         return 0;
110 }
111
112 static int tse850_put_mux2(struct snd_kcontrol *kctrl,
113                            struct snd_ctl_elem_value *ucontrol)
114 {
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];
120
121         if (val >= e->items)
122                 return -EINVAL;
123
124         gpiod_set_value_cansleep(tse850->loop2, val);
125         tse850->loop2_cache = val;
126
127         return snd_soc_dapm_put_enum_double(kctrl, ucontrol);
128 }
129
130 int tse850_get_mix(struct snd_kcontrol *kctrl,
131                    struct snd_ctl_elem_value *ucontrol)
132 {
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);
136
137         ucontrol->value.enumerated.item[0] = tse850->add_cache;
138
139         return 0;
140 }
141
142 int tse850_put_mix(struct snd_kcontrol *kctrl,
143                    struct snd_ctl_elem_value *ucontrol)
144 {
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];
149
150         if (tse850->add_cache == connect)
151                 return 0;
152
153         /*
154          * Hmmm, this gpiod_set_value_cansleep call should probably happen
155          * inside snd_soc_dapm_mixer_update_power in the loop.
156          */
157         gpiod_set_value_cansleep(tse850->add, connect);
158         tse850->add_cache = connect;
159
160         snd_soc_dapm_mixer_update_power(dapm, kctrl, connect, NULL);
161         return 1;
162 }
163
164 int tse850_get_ana(struct snd_kcontrol *kctrl,
165                    struct snd_ctl_elem_value *ucontrol)
166 {
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;
171
172         ret = regulator_get_voltage(tse850->ana);
173         if (ret < 0)
174                 return ret;
175
176         /*
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)
184          */
185         if (ret < 11000000)
186                 ret = 11000000;
187         else if (ret > 20000000)
188                 ret = 20000000;
189         ret -= 11000000;
190         ret = (ret + 500000) / 1000000;
191
192         ucontrol->value.enumerated.item[0] = ret;
193
194         return 0;
195 }
196
197 int tse850_put_ana(struct snd_kcontrol *kctrl,
198                    struct snd_ctl_elem_value *ucontrol)
199 {
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;
206
207         if (uV >= e->items)
208                 return -EINVAL;
209
210         /*
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).
218          */
219         if (uV)
220                 uV = 11000000 + (1000000 * uV);
221         else
222                 uV = 2000000;
223
224         ret = regulator_set_voltage(tse850->ana, uV, uV);
225         if (ret < 0)
226                 return ret;
227
228         return snd_soc_dapm_put_enum_double(kctrl, ucontrol);
229 }
230
231 static const char * const mux_text[] = { "Mixer", "Loop" };
232
233 static const struct soc_enum mux_enum =
234         SOC_ENUM_SINGLE(SND_SOC_NOPM, 0, 2, mux_text);
235
236 static const struct snd_kcontrol_new mux1 =
237         SOC_DAPM_ENUM_EXT("MUX1", mux_enum, tse850_get_mux1, tse850_put_mux1);
238
239 static const struct snd_kcontrol_new mux2 =
240         SOC_DAPM_ENUM_EXT("MUX2", mux_enum, tse850_get_mux2, tse850_put_mux2);
241
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) }
248
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),
252 };
253
254 static const char * const ana_text[] = {
255         "Low", "12V", "13V", "14V", "15V", "16V", "17V", "18V", "19V", "20V"
256 };
257
258 static const struct soc_enum ana_enum =
259         SOC_ENUM_SINGLE(SND_SOC_NOPM, 0, 9, ana_text);
260
261 static const struct snd_kcontrol_new out =
262         SOC_DAPM_ENUM_EXT("ANA", ana_enum, tse850_get_ana, tse850_put_ana);
263
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),
276 };
277
278 /*
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.
286  */
287 static const struct snd_soc_dapm_route tse850_intercon[] = {
288         { "OUT1", NULL, "MUX1" },
289         { "OUT2", NULL, "MUX2" },
290
291         { "MUX1", "Loop",  "IN1" },
292         { "MUX1", "Mixer", "OUT" },
293
294         { "MUX2", "Loop",  "IN2" },
295         { "MUX2", "Mixer", "OUT" },
296
297         { "OUT", NULL, "MIX" },
298
299         { "MIX", NULL, "DAC" },
300         { "MIX", "IN Switch", "IN1" },
301         { "MIX", "IN Switch", "IN2" },
302
303         /* connect board input to the codec left channel output pin */
304         { "DAC", NULL, "OUTL" },
305 };
306
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,
314 };
315
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,
326 };
327
328 static int tse850_dt_init(struct platform_device *pdev)
329 {
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);
335
336         if (!np) {
337                 dev_err(&pdev->dev, "only device tree supported\n");
338                 return -EINVAL;
339         }
340
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;
345         }
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);
350
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;
355         }
356         dailink->codec_of_node = codec_np;
357         of_node_put(codec_np);
358
359         return 0;
360 }
361
362 static int tse850_probe(struct platform_device *pdev)
363 {
364         struct snd_soc_card *card = &tse850_card;
365         struct device *dev = card->dev = &pdev->dev;
366         struct tse850_priv *tse850;
367         int ret;
368
369         tse850 = devm_kzalloc(dev, sizeof(*tse850), GFP_KERNEL);
370         if (!tse850)
371                 return -ENOMEM;
372
373         snd_soc_card_set_drvdata(card, tse850);
374
375         ret = tse850_dt_init(pdev);
376         if (ret) {
377                 dev_err(dev, "failed to init dt info\n");
378                 return ret;
379         }
380
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);
386         }
387         tse850->add_cache = 1;
388
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);
394         }
395         tse850->loop1_cache = 1;
396
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);
402         }
403         tse850->loop2_cache = 1;
404
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);
410         }
411
412         ret = regulator_enable(tse850->ana);
413         if (ret < 0) {
414                 dev_err(dev, "failed to enable the 'ana' regulator\n");
415                 return ret;
416         }
417
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;
423         }
424
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;
429         }
430
431         return 0;
432
433 err_put_audio:
434         atmel_ssc_put_audio(tse850->ssc_id);
435 err_disable_ana:
436         regulator_disable(tse850->ana);
437         return ret;
438 }
439
440 static int tse850_remove(struct platform_device *pdev)
441 {
442         struct snd_soc_card *card = platform_get_drvdata(pdev);
443         struct tse850_priv *tse850 = snd_soc_card_get_drvdata(card);
444
445         snd_soc_unregister_card(card);
446         atmel_ssc_put_audio(tse850->ssc_id);
447         regulator_disable(tse850->ana);
448
449         return 0;
450 }
451
452 static const struct of_device_id tse850_dt_ids[] = {
453         { .compatible = "axentia,tse850-pcm5142", },
454         { /* sentinel */ }
455 };
456 MODULE_DEVICE_TABLE(of, tse850_dt_ids);
457
458 static struct platform_driver tse850_driver = {
459         .driver = {
460                 .name = "axentia-tse850-pcm5142",
461                 .of_match_table = of_match_ptr(tse850_dt_ids),
462         },
463         .probe = tse850_probe,
464         .remove = tse850_remove,
465 };
466
467 module_platform_driver(tse850_driver);
468
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");