xref: /openbmc/linux/sound/soc/atmel/tse850-pcm5142.c (revision cd238eff)
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 //             +              +
22 //
23 //   IN2 +---o--+------------+--o---+ OUT2
24 //               loop2 relays
25 //
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'.
30 //
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.
36 
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>
44 
45 #include <sound/soc.h>
46 #include <sound/pcm_params.h>
47 
48 struct tse850_priv {
49 	struct gpio_desc *add;
50 	struct gpio_desc *loop1;
51 	struct gpio_desc *loop2;
52 
53 	struct regulator *ana;
54 
55 	int add_cache;
56 	int loop1_cache;
57 	int loop2_cache;
58 };
59 
60 static int tse850_get_mux1(struct snd_kcontrol *kctrl,
61 			   struct snd_ctl_elem_value *ucontrol)
62 {
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);
66 
67 	ucontrol->value.enumerated.item[0] = tse850->loop1_cache;
68 
69 	return 0;
70 }
71 
72 static int tse850_put_mux1(struct snd_kcontrol *kctrl,
73 			   struct snd_ctl_elem_value *ucontrol)
74 {
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];
80 
81 	if (val >= e->items)
82 		return -EINVAL;
83 
84 	gpiod_set_value_cansleep(tse850->loop1, val);
85 	tse850->loop1_cache = val;
86 
87 	return snd_soc_dapm_put_enum_double(kctrl, ucontrol);
88 }
89 
90 static int tse850_get_mux2(struct snd_kcontrol *kctrl,
91 			   struct snd_ctl_elem_value *ucontrol)
92 {
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);
96 
97 	ucontrol->value.enumerated.item[0] = tse850->loop2_cache;
98 
99 	return 0;
100 }
101 
102 static int tse850_put_mux2(struct snd_kcontrol *kctrl,
103 			   struct snd_ctl_elem_value *ucontrol)
104 {
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];
110 
111 	if (val >= e->items)
112 		return -EINVAL;
113 
114 	gpiod_set_value_cansleep(tse850->loop2, val);
115 	tse850->loop2_cache = val;
116 
117 	return snd_soc_dapm_put_enum_double(kctrl, ucontrol);
118 }
119 
120 static int tse850_get_mix(struct snd_kcontrol *kctrl,
121 			  struct snd_ctl_elem_value *ucontrol)
122 {
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);
126 
127 	ucontrol->value.enumerated.item[0] = tse850->add_cache;
128 
129 	return 0;
130 }
131 
132 static int tse850_put_mix(struct snd_kcontrol *kctrl,
133 			  struct snd_ctl_elem_value *ucontrol)
134 {
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];
139 
140 	if (tse850->add_cache == connect)
141 		return 0;
142 
143 	/*
144 	 * Hmmm, this gpiod_set_value_cansleep call should probably happen
145 	 * inside snd_soc_dapm_mixer_update_power in the loop.
146 	 */
147 	gpiod_set_value_cansleep(tse850->add, connect);
148 	tse850->add_cache = connect;
149 
150 	snd_soc_dapm_mixer_update_power(dapm, kctrl, connect, NULL);
151 	return 1;
152 }
153 
154 static int tse850_get_ana(struct snd_kcontrol *kctrl,
155 			  struct snd_ctl_elem_value *ucontrol)
156 {
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;
161 
162 	ret = regulator_get_voltage(tse850->ana);
163 	if (ret < 0)
164 		return ret;
165 
166 	/*
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)
174 	 */
175 	if (ret < 11000000)
176 		ret = 11000000;
177 	else if (ret > 20000000)
178 		ret = 20000000;
179 	ret -= 11000000;
180 	ret = (ret + 500000) / 1000000;
181 
182 	ucontrol->value.enumerated.item[0] = ret;
183 
184 	return 0;
185 }
186 
187 static int tse850_put_ana(struct snd_kcontrol *kctrl,
188 			  struct snd_ctl_elem_value *ucontrol)
189 {
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;
196 
197 	if (uV >= e->items)
198 		return -EINVAL;
199 
200 	/*
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).
208 	 */
209 	if (uV)
210 		uV = 11000000 + (1000000 * uV);
211 	else
212 		uV = 2000000;
213 
214 	ret = regulator_set_voltage(tse850->ana, uV, uV);
215 	if (ret < 0)
216 		return ret;
217 
218 	return snd_soc_dapm_put_enum_double(kctrl, ucontrol);
219 }
220 
221 static const char * const mux_text[] = { "Mixer", "Loop" };
222 
223 static const struct soc_enum mux_enum =
224 	SOC_ENUM_SINGLE(SND_SOC_NOPM, 0, ARRAY_SIZE(mux_text), mux_text);
225 
226 static const struct snd_kcontrol_new mux1 =
227 	SOC_DAPM_ENUM_EXT("MUX1", mux_enum, tse850_get_mux1, tse850_put_mux1);
228 
229 static const struct snd_kcontrol_new mux2 =
230 	SOC_DAPM_ENUM_EXT("MUX2", mux_enum, tse850_get_mux2, tse850_put_mux2);
231 
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) }
238 
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),
242 };
243 
244 static const char * const ana_text[] = {
245 	"Low", "12V", "13V", "14V", "15V", "16V", "17V", "18V", "19V", "20V"
246 };
247 
248 static const struct soc_enum ana_enum =
249 	SOC_ENUM_SINGLE(SND_SOC_NOPM, 0, ARRAY_SIZE(ana_text), ana_text);
250 
251 static const struct snd_kcontrol_new out =
252 	SOC_DAPM_ENUM_EXT("ANA", ana_enum, tse850_get_ana, tse850_put_ana);
253 
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),
266 };
267 
268 /*
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.
276  */
277 static const struct snd_soc_dapm_route tse850_intercon[] = {
278 	{ "OUT1", NULL, "MUX1" },
279 	{ "OUT2", NULL, "MUX2" },
280 
281 	{ "MUX1", "Loop",  "IN1" },
282 	{ "MUX1", "Mixer", "OUT" },
283 
284 	{ "MUX2", "Loop",  "IN2" },
285 	{ "MUX2", "Mixer", "OUT" },
286 
287 	{ "OUT", NULL, "MIX" },
288 
289 	{ "MIX", NULL, "DAC" },
290 	{ "MIX", "IN Switch", "IN1" },
291 	{ "MIX", "IN Switch", "IN2" },
292 
293 	/* connect board input to the codec left channel output pin */
294 	{ "DAC", NULL, "OUTL" },
295 };
296 
297 static struct snd_soc_dai_link tse850_dailink = {
298 	.name = "TSE-850",
299 	.stream_name = "TSE-850-PCM",
300 	.codec_dai_name = "pcm512x-hifi",
301 	.dai_fmt = SND_SOC_DAIFMT_I2S
302 		 | SND_SOC_DAIFMT_NB_NF
303 		 | SND_SOC_DAIFMT_CBM_CFS,
304 };
305 
306 static struct snd_soc_card tse850_card = {
307 	.name = "TSE-850-ASoC",
308 	.owner = THIS_MODULE,
309 	.dai_link = &tse850_dailink,
310 	.num_links = 1,
311 	.dapm_widgets = tse850_dapm_widgets,
312 	.num_dapm_widgets = ARRAY_SIZE(tse850_dapm_widgets),
313 	.dapm_routes = tse850_intercon,
314 	.num_dapm_routes = ARRAY_SIZE(tse850_intercon),
315 	.fully_routed = true,
316 };
317 
318 static int tse850_dt_init(struct platform_device *pdev)
319 {
320 	struct device_node *np = pdev->dev.of_node;
321 	struct device_node *codec_np, *cpu_np;
322 	struct snd_soc_dai_link *dailink = &tse850_dailink;
323 
324 	if (!np) {
325 		dev_err(&pdev->dev, "only device tree supported\n");
326 		return -EINVAL;
327 	}
328 
329 	cpu_np = of_parse_phandle(np, "axentia,cpu-dai", 0);
330 	if (!cpu_np) {
331 		dev_err(&pdev->dev, "failed to get cpu dai\n");
332 		return -EINVAL;
333 	}
334 	dailink->cpu_of_node = cpu_np;
335 	dailink->platform_of_node = cpu_np;
336 	of_node_put(cpu_np);
337 
338 	codec_np = of_parse_phandle(np, "axentia,audio-codec", 0);
339 	if (!codec_np) {
340 		dev_err(&pdev->dev, "failed to get codec info\n");
341 		return -EINVAL;
342 	}
343 	dailink->codec_of_node = codec_np;
344 	of_node_put(codec_np);
345 
346 	return 0;
347 }
348 
349 static int tse850_probe(struct platform_device *pdev)
350 {
351 	struct snd_soc_card *card = &tse850_card;
352 	struct device *dev = card->dev = &pdev->dev;
353 	struct tse850_priv *tse850;
354 	int ret;
355 
356 	tse850 = devm_kzalloc(dev, sizeof(*tse850), GFP_KERNEL);
357 	if (!tse850)
358 		return -ENOMEM;
359 
360 	snd_soc_card_set_drvdata(card, tse850);
361 
362 	ret = tse850_dt_init(pdev);
363 	if (ret) {
364 		dev_err(dev, "failed to init dt info\n");
365 		return ret;
366 	}
367 
368 	tse850->add = devm_gpiod_get(dev, "axentia,add", GPIOD_OUT_HIGH);
369 	if (IS_ERR(tse850->add)) {
370 		if (PTR_ERR(tse850->add) != -EPROBE_DEFER)
371 			dev_err(dev, "failed to get 'add' gpio\n");
372 		return PTR_ERR(tse850->add);
373 	}
374 	tse850->add_cache = 1;
375 
376 	tse850->loop1 = devm_gpiod_get(dev, "axentia,loop1", GPIOD_OUT_HIGH);
377 	if (IS_ERR(tse850->loop1)) {
378 		if (PTR_ERR(tse850->loop1) != -EPROBE_DEFER)
379 			dev_err(dev, "failed to get 'loop1' gpio\n");
380 		return PTR_ERR(tse850->loop1);
381 	}
382 	tse850->loop1_cache = 1;
383 
384 	tse850->loop2 = devm_gpiod_get(dev, "axentia,loop2", GPIOD_OUT_HIGH);
385 	if (IS_ERR(tse850->loop2)) {
386 		if (PTR_ERR(tse850->loop2) != -EPROBE_DEFER)
387 			dev_err(dev, "failed to get 'loop2' gpio\n");
388 		return PTR_ERR(tse850->loop2);
389 	}
390 	tse850->loop2_cache = 1;
391 
392 	tse850->ana = devm_regulator_get(dev, "axentia,ana");
393 	if (IS_ERR(tse850->ana)) {
394 		if (PTR_ERR(tse850->ana) != -EPROBE_DEFER)
395 			dev_err(dev, "failed to get 'ana' regulator\n");
396 		return PTR_ERR(tse850->ana);
397 	}
398 
399 	ret = regulator_enable(tse850->ana);
400 	if (ret < 0) {
401 		dev_err(dev, "failed to enable the 'ana' regulator\n");
402 		return ret;
403 	}
404 
405 	ret = snd_soc_register_card(card);
406 	if (ret) {
407 		dev_err(dev, "snd_soc_register_card failed\n");
408 		goto err_disable_ana;
409 	}
410 
411 	return 0;
412 
413 err_disable_ana:
414 	regulator_disable(tse850->ana);
415 	return ret;
416 }
417 
418 static int tse850_remove(struct platform_device *pdev)
419 {
420 	struct snd_soc_card *card = platform_get_drvdata(pdev);
421 	struct tse850_priv *tse850 = snd_soc_card_get_drvdata(card);
422 
423 	snd_soc_unregister_card(card);
424 	regulator_disable(tse850->ana);
425 
426 	return 0;
427 }
428 
429 static const struct of_device_id tse850_dt_ids[] = {
430 	{ .compatible = "axentia,tse850-pcm5142", },
431 	{ /* sentinel */ }
432 };
433 MODULE_DEVICE_TABLE(of, tse850_dt_ids);
434 
435 static struct platform_driver tse850_driver = {
436 	.driver = {
437 		.name = "axentia-tse850-pcm5142",
438 		.of_match_table = of_match_ptr(tse850_dt_ids),
439 	},
440 	.probe = tse850_probe,
441 	.remove = tse850_remove,
442 };
443 
444 module_platform_driver(tse850_driver);
445 
446 /* Module information */
447 MODULE_AUTHOR("Peter Rosin <peda@axentia.se>");
448 MODULE_DESCRIPTION("ALSA SoC driver for TSE-850 with PCM5142 codec");
449 MODULE_LICENSE("GPL v2");
450