xref: /openbmc/linux/sound/soc/atmel/tse850-pcm5142.c (revision 82e6fdd6)
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 struct tse850_priv {
55 	struct gpio_desc *add;
56 	struct gpio_desc *loop1;
57 	struct gpio_desc *loop2;
58 
59 	struct regulator *ana;
60 
61 	int add_cache;
62 	int loop1_cache;
63 	int loop2_cache;
64 };
65 
66 static int tse850_get_mux1(struct snd_kcontrol *kctrl,
67 			   struct snd_ctl_elem_value *ucontrol)
68 {
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);
72 
73 	ucontrol->value.enumerated.item[0] = tse850->loop1_cache;
74 
75 	return 0;
76 }
77 
78 static int tse850_put_mux1(struct snd_kcontrol *kctrl,
79 			   struct snd_ctl_elem_value *ucontrol)
80 {
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];
86 
87 	if (val >= e->items)
88 		return -EINVAL;
89 
90 	gpiod_set_value_cansleep(tse850->loop1, val);
91 	tse850->loop1_cache = val;
92 
93 	return snd_soc_dapm_put_enum_double(kctrl, ucontrol);
94 }
95 
96 static int tse850_get_mux2(struct snd_kcontrol *kctrl,
97 			   struct snd_ctl_elem_value *ucontrol)
98 {
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);
102 
103 	ucontrol->value.enumerated.item[0] = tse850->loop2_cache;
104 
105 	return 0;
106 }
107 
108 static int tse850_put_mux2(struct snd_kcontrol *kctrl,
109 			   struct snd_ctl_elem_value *ucontrol)
110 {
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];
116 
117 	if (val >= e->items)
118 		return -EINVAL;
119 
120 	gpiod_set_value_cansleep(tse850->loop2, val);
121 	tse850->loop2_cache = val;
122 
123 	return snd_soc_dapm_put_enum_double(kctrl, ucontrol);
124 }
125 
126 int tse850_get_mix(struct snd_kcontrol *kctrl,
127 		   struct snd_ctl_elem_value *ucontrol)
128 {
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);
132 
133 	ucontrol->value.enumerated.item[0] = tse850->add_cache;
134 
135 	return 0;
136 }
137 
138 int tse850_put_mix(struct snd_kcontrol *kctrl,
139 		   struct snd_ctl_elem_value *ucontrol)
140 {
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];
145 
146 	if (tse850->add_cache == connect)
147 		return 0;
148 
149 	/*
150 	 * Hmmm, this gpiod_set_value_cansleep call should probably happen
151 	 * inside snd_soc_dapm_mixer_update_power in the loop.
152 	 */
153 	gpiod_set_value_cansleep(tse850->add, connect);
154 	tse850->add_cache = connect;
155 
156 	snd_soc_dapm_mixer_update_power(dapm, kctrl, connect, NULL);
157 	return 1;
158 }
159 
160 int tse850_get_ana(struct snd_kcontrol *kctrl,
161 		   struct snd_ctl_elem_value *ucontrol)
162 {
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);
166 	int ret;
167 
168 	ret = regulator_get_voltage(tse850->ana);
169 	if (ret < 0)
170 		return ret;
171 
172 	/*
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)
177 	 *     ...
178 	 * 18.5V-19.5V to "19V" (enum 8)
179 	 * 19.5V-      to "20V" (enum 9)
180 	 */
181 	if (ret < 11000000)
182 		ret = 11000000;
183 	else if (ret > 20000000)
184 		ret = 20000000;
185 	ret -= 11000000;
186 	ret = (ret + 500000) / 1000000;
187 
188 	ucontrol->value.enumerated.item[0] = ret;
189 
190 	return 0;
191 }
192 
193 int tse850_put_ana(struct snd_kcontrol *kctrl,
194 		   struct snd_ctl_elem_value *ucontrol)
195 {
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];
201 	int ret;
202 
203 	if (uV >= e->items)
204 		return -EINVAL;
205 
206 	/*
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).
214 	 */
215 	if (uV)
216 		uV = 11000000 + (1000000 * uV);
217 	else
218 		uV = 2000000;
219 
220 	ret = regulator_set_voltage(tse850->ana, uV, uV);
221 	if (ret < 0)
222 		return ret;
223 
224 	return snd_soc_dapm_put_enum_double(kctrl, ucontrol);
225 }
226 
227 static const char * const mux_text[] = { "Mixer", "Loop" };
228 
229 static const struct soc_enum mux_enum =
230 	SOC_ENUM_SINGLE(SND_SOC_NOPM, 0, ARRAY_SIZE(mux_text), mux_text);
231 
232 static const struct snd_kcontrol_new mux1 =
233 	SOC_DAPM_ENUM_EXT("MUX1", mux_enum, tse850_get_mux1, tse850_put_mux1);
234 
235 static const struct snd_kcontrol_new mux2 =
236 	SOC_DAPM_ENUM_EXT("MUX2", mux_enum, tse850_get_mux2, tse850_put_mux2);
237 
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, \
241 	.get = xget, \
242 	.put = xput, \
243 	.private_value = SOC_SINGLE_VALUE(reg, shift, max, invert, 0) }
244 
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),
248 };
249 
250 static const char * const ana_text[] = {
251 	"Low", "12V", "13V", "14V", "15V", "16V", "17V", "18V", "19V", "20V"
252 };
253 
254 static const struct soc_enum ana_enum =
255 	SOC_ENUM_SINGLE(SND_SOC_NOPM, 0, ARRAY_SIZE(ana_text), ana_text);
256 
257 static const struct snd_kcontrol_new out =
258 	SOC_DAPM_ENUM_EXT("ANA", ana_enum, tse850_get_ana, tse850_put_ana);
259 
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),
272 };
273 
274 /*
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
279  * are routed.
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.
282  */
283 static const struct snd_soc_dapm_route tse850_intercon[] = {
284 	{ "OUT1", NULL, "MUX1" },
285 	{ "OUT2", NULL, "MUX2" },
286 
287 	{ "MUX1", "Loop",  "IN1" },
288 	{ "MUX1", "Mixer", "OUT" },
289 
290 	{ "MUX2", "Loop",  "IN2" },
291 	{ "MUX2", "Mixer", "OUT" },
292 
293 	{ "OUT", NULL, "MIX" },
294 
295 	{ "MIX", NULL, "DAC" },
296 	{ "MIX", "IN Switch", "IN1" },
297 	{ "MIX", "IN Switch", "IN2" },
298 
299 	/* connect board input to the codec left channel output pin */
300 	{ "DAC", NULL, "OUTL" },
301 };
302 
303 static struct snd_soc_dai_link tse850_dailink = {
304 	.name = "TSE-850",
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,
310 };
311 
312 static struct snd_soc_card tse850_card = {
313 	.name = "TSE-850-ASoC",
314 	.owner = THIS_MODULE,
315 	.dai_link = &tse850_dailink,
316 	.num_links = 1,
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,
322 };
323 
324 static int tse850_dt_init(struct platform_device *pdev)
325 {
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;
329 
330 	if (!np) {
331 		dev_err(&pdev->dev, "only device tree supported\n");
332 		return -EINVAL;
333 	}
334 
335 	cpu_np = of_parse_phandle(np, "axentia,cpu-dai", 0);
336 	if (!cpu_np) {
337 		dev_err(&pdev->dev, "failed to get cpu dai\n");
338 		return -EINVAL;
339 	}
340 	dailink->cpu_of_node = cpu_np;
341 	dailink->platform_of_node = cpu_np;
342 	of_node_put(cpu_np);
343 
344 	codec_np = of_parse_phandle(np, "axentia,audio-codec", 0);
345 	if (!codec_np) {
346 		dev_err(&pdev->dev, "failed to get codec info\n");
347 		return -EINVAL;
348 	}
349 	dailink->codec_of_node = codec_np;
350 	of_node_put(codec_np);
351 
352 	return 0;
353 }
354 
355 static int tse850_probe(struct platform_device *pdev)
356 {
357 	struct snd_soc_card *card = &tse850_card;
358 	struct device *dev = card->dev = &pdev->dev;
359 	struct tse850_priv *tse850;
360 	int ret;
361 
362 	tse850 = devm_kzalloc(dev, sizeof(*tse850), GFP_KERNEL);
363 	if (!tse850)
364 		return -ENOMEM;
365 
366 	snd_soc_card_set_drvdata(card, tse850);
367 
368 	ret = tse850_dt_init(pdev);
369 	if (ret) {
370 		dev_err(dev, "failed to init dt info\n");
371 		return ret;
372 	}
373 
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);
379 	}
380 	tse850->add_cache = 1;
381 
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);
387 	}
388 	tse850->loop1_cache = 1;
389 
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);
395 	}
396 	tse850->loop2_cache = 1;
397 
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);
403 	}
404 
405 	ret = regulator_enable(tse850->ana);
406 	if (ret < 0) {
407 		dev_err(dev, "failed to enable the 'ana' regulator\n");
408 		return ret;
409 	}
410 
411 	ret = snd_soc_register_card(card);
412 	if (ret) {
413 		dev_err(dev, "snd_soc_register_card failed\n");
414 		goto err_disable_ana;
415 	}
416 
417 	return 0;
418 
419 err_disable_ana:
420 	regulator_disable(tse850->ana);
421 	return ret;
422 }
423 
424 static int tse850_remove(struct platform_device *pdev)
425 {
426 	struct snd_soc_card *card = platform_get_drvdata(pdev);
427 	struct tse850_priv *tse850 = snd_soc_card_get_drvdata(card);
428 
429 	snd_soc_unregister_card(card);
430 	regulator_disable(tse850->ana);
431 
432 	return 0;
433 }
434 
435 static const struct of_device_id tse850_dt_ids[] = {
436 	{ .compatible = "axentia,tse850-pcm5142", },
437 	{ /* sentinel */ }
438 };
439 MODULE_DEVICE_TABLE(of, tse850_dt_ids);
440 
441 static struct platform_driver tse850_driver = {
442 	.driver = {
443 		.name = "axentia-tse850-pcm5142",
444 		.of_match_table = of_match_ptr(tse850_dt_ids),
445 	},
446 	.probe = tse850_probe,
447 	.remove = tse850_remove,
448 };
449 
450 module_platform_driver(tse850_driver);
451 
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");
456