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 controls 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/consumer.h>
39 #include <linux/module.h>
40 #include <linux/of.h>
41 #include <linux/regulator/consumer.h>
42 
43 #include <sound/soc.h>
44 #include <sound/pcm_params.h>
45 
46 struct tse850_priv {
47 	struct gpio_desc *add;
48 	struct gpio_desc *loop1;
49 	struct gpio_desc *loop2;
50 
51 	struct regulator *ana;
52 
53 	int add_cache;
54 	int loop1_cache;
55 	int loop2_cache;
56 };
57 
tse850_get_mux1(struct snd_kcontrol * kctrl,struct snd_ctl_elem_value * ucontrol)58 static int tse850_get_mux1(struct snd_kcontrol *kctrl,
59 			   struct snd_ctl_elem_value *ucontrol)
60 {
61 	struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kctrl);
62 	struct snd_soc_card *card = dapm->card;
63 	struct tse850_priv *tse850 = snd_soc_card_get_drvdata(card);
64 
65 	ucontrol->value.enumerated.item[0] = tse850->loop1_cache;
66 
67 	return 0;
68 }
69 
tse850_put_mux1(struct snd_kcontrol * kctrl,struct snd_ctl_elem_value * ucontrol)70 static int tse850_put_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 	struct soc_enum *e = (struct soc_enum *)kctrl->private_value;
77 	unsigned int val = ucontrol->value.enumerated.item[0];
78 
79 	if (val >= e->items)
80 		return -EINVAL;
81 
82 	gpiod_set_value_cansleep(tse850->loop1, val);
83 	tse850->loop1_cache = val;
84 
85 	return snd_soc_dapm_put_enum_double(kctrl, ucontrol);
86 }
87 
tse850_get_mux2(struct snd_kcontrol * kctrl,struct snd_ctl_elem_value * ucontrol)88 static int tse850_get_mux2(struct snd_kcontrol *kctrl,
89 			   struct snd_ctl_elem_value *ucontrol)
90 {
91 	struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kctrl);
92 	struct snd_soc_card *card = dapm->card;
93 	struct tse850_priv *tse850 = snd_soc_card_get_drvdata(card);
94 
95 	ucontrol->value.enumerated.item[0] = tse850->loop2_cache;
96 
97 	return 0;
98 }
99 
tse850_put_mux2(struct snd_kcontrol * kctrl,struct snd_ctl_elem_value * ucontrol)100 static int tse850_put_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 	struct soc_enum *e = (struct soc_enum *)kctrl->private_value;
107 	unsigned int val = ucontrol->value.enumerated.item[0];
108 
109 	if (val >= e->items)
110 		return -EINVAL;
111 
112 	gpiod_set_value_cansleep(tse850->loop2, val);
113 	tse850->loop2_cache = val;
114 
115 	return snd_soc_dapm_put_enum_double(kctrl, ucontrol);
116 }
117 
tse850_get_mix(struct snd_kcontrol * kctrl,struct snd_ctl_elem_value * ucontrol)118 static int tse850_get_mix(struct snd_kcontrol *kctrl,
119 			  struct snd_ctl_elem_value *ucontrol)
120 {
121 	struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kctrl);
122 	struct snd_soc_card *card = dapm->card;
123 	struct tse850_priv *tse850 = snd_soc_card_get_drvdata(card);
124 
125 	ucontrol->value.enumerated.item[0] = tse850->add_cache;
126 
127 	return 0;
128 }
129 
tse850_put_mix(struct snd_kcontrol * kctrl,struct snd_ctl_elem_value * ucontrol)130 static int tse850_put_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 	int connect = !!ucontrol->value.integer.value[0];
137 
138 	if (tse850->add_cache == connect)
139 		return 0;
140 
141 	/*
142 	 * Hmmm, this gpiod_set_value_cansleep call should probably happen
143 	 * inside snd_soc_dapm_mixer_update_power in the loop.
144 	 */
145 	gpiod_set_value_cansleep(tse850->add, connect);
146 	tse850->add_cache = connect;
147 
148 	snd_soc_dapm_mixer_update_power(dapm, kctrl, connect, NULL);
149 	return 1;
150 }
151 
tse850_get_ana(struct snd_kcontrol * kctrl,struct snd_ctl_elem_value * ucontrol)152 static int tse850_get_ana(struct snd_kcontrol *kctrl,
153 			  struct snd_ctl_elem_value *ucontrol)
154 {
155 	struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kctrl);
156 	struct snd_soc_card *card = dapm->card;
157 	struct tse850_priv *tse850 = snd_soc_card_get_drvdata(card);
158 	int ret;
159 
160 	ret = regulator_get_voltage(tse850->ana);
161 	if (ret < 0)
162 		return ret;
163 
164 	/*
165 	 * Map regulator output values like so:
166 	 *      -11.5V to "Low" (enum 0)
167 	 * 11.5V-12.5V to "12V" (enum 1)
168 	 * 12.5V-13.5V to "13V" (enum 2)
169 	 *     ...
170 	 * 18.5V-19.5V to "19V" (enum 8)
171 	 * 19.5V-      to "20V" (enum 9)
172 	 */
173 	if (ret < 11000000)
174 		ret = 11000000;
175 	else if (ret > 20000000)
176 		ret = 20000000;
177 	ret -= 11000000;
178 	ret = (ret + 500000) / 1000000;
179 
180 	ucontrol->value.enumerated.item[0] = ret;
181 
182 	return 0;
183 }
184 
tse850_put_ana(struct snd_kcontrol * kctrl,struct snd_ctl_elem_value * ucontrol)185 static int tse850_put_ana(struct snd_kcontrol *kctrl,
186 			  struct snd_ctl_elem_value *ucontrol)
187 {
188 	struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kctrl);
189 	struct snd_soc_card *card = dapm->card;
190 	struct tse850_priv *tse850 = snd_soc_card_get_drvdata(card);
191 	struct soc_enum *e = (struct soc_enum *)kctrl->private_value;
192 	unsigned int uV = ucontrol->value.enumerated.item[0];
193 	int ret;
194 
195 	if (uV >= e->items)
196 		return -EINVAL;
197 
198 	/*
199 	 * Map enum zero (Low) to 2 volts on the regulator, do this since
200 	 * the ana regulator is supplied by the system 12V voltage and
201 	 * requesting anything below the system voltage causes the system
202 	 * voltage to be passed through the regulator. Also, the ana
203 	 * regulator induces noise when requesting voltages near the
204 	 * system voltage. So, by mapping Low to 2V, that noise is
205 	 * eliminated when all that is needed is 12V (the system voltage).
206 	 */
207 	if (uV)
208 		uV = 11000000 + (1000000 * uV);
209 	else
210 		uV = 2000000;
211 
212 	ret = regulator_set_voltage(tse850->ana, uV, uV);
213 	if (ret < 0)
214 		return ret;
215 
216 	return snd_soc_dapm_put_enum_double(kctrl, ucontrol);
217 }
218 
219 static const char * const mux_text[] = { "Mixer", "Loop" };
220 
221 static const struct soc_enum mux_enum =
222 	SOC_ENUM_SINGLE(SND_SOC_NOPM, 0, ARRAY_SIZE(mux_text), mux_text);
223 
224 static const struct snd_kcontrol_new mux1 =
225 	SOC_DAPM_ENUM_EXT("MUX1", mux_enum, tse850_get_mux1, tse850_put_mux1);
226 
227 static const struct snd_kcontrol_new mux2 =
228 	SOC_DAPM_ENUM_EXT("MUX2", mux_enum, tse850_get_mux2, tse850_put_mux2);
229 
230 static const struct snd_kcontrol_new mix[] = {
231 	SOC_SINGLE_EXT("IN Switch", SND_SOC_NOPM, 0, 1, 0,
232 		       tse850_get_mix, tse850_put_mix),
233 };
234 
235 static const char * const ana_text[] = {
236 	"Low", "12V", "13V", "14V", "15V", "16V", "17V", "18V", "19V", "20V"
237 };
238 
239 static const struct soc_enum ana_enum =
240 	SOC_ENUM_SINGLE(SND_SOC_NOPM, 0, ARRAY_SIZE(ana_text), ana_text);
241 
242 static const struct snd_kcontrol_new out =
243 	SOC_DAPM_ENUM_EXT("ANA", ana_enum, tse850_get_ana, tse850_put_ana);
244 
245 static const struct snd_soc_dapm_widget tse850_dapm_widgets[] = {
246 	SND_SOC_DAPM_LINE("OUT1", NULL),
247 	SND_SOC_DAPM_LINE("OUT2", NULL),
248 	SND_SOC_DAPM_LINE("IN1", NULL),
249 	SND_SOC_DAPM_LINE("IN2", NULL),
250 	SND_SOC_DAPM_INPUT("DAC"),
251 	SND_SOC_DAPM_AIF_IN("AIFINL", "Playback", 0, SND_SOC_NOPM, 0, 0),
252 	SND_SOC_DAPM_AIF_IN("AIFINR", "Playback", 1, SND_SOC_NOPM, 0, 0),
253 	SOC_MIXER_ARRAY("MIX", SND_SOC_NOPM, 0, 0, mix),
254 	SND_SOC_DAPM_MUX("MUX1", SND_SOC_NOPM, 0, 0, &mux1),
255 	SND_SOC_DAPM_MUX("MUX2", SND_SOC_NOPM, 0, 0, &mux2),
256 	SND_SOC_DAPM_OUT_DRV("OUT", SND_SOC_NOPM, 0, 0, &out, 1),
257 };
258 
259 /*
260  * These connections are not entirely correct, since both IN1 and IN2
261  * are always fed to MIX (if the "IN switch" is set so), i.e. without
262  * regard to the loop1 and loop2 relays that according to this only
263  * control MUX1 and MUX2 but in fact also control how the input signals
264  * are routed.
265  * But, 1) I don't know how to do it right, and 2) it doesn't seem to
266  * matter in practice since nothing is powered in those sections anyway.
267  */
268 static const struct snd_soc_dapm_route tse850_intercon[] = {
269 	{ "OUT1", NULL, "MUX1" },
270 	{ "OUT2", NULL, "MUX2" },
271 
272 	{ "MUX1", "Loop",  "IN1" },
273 	{ "MUX1", "Mixer", "OUT" },
274 
275 	{ "MUX2", "Loop",  "IN2" },
276 	{ "MUX2", "Mixer", "OUT" },
277 
278 	{ "OUT", NULL, "MIX" },
279 
280 	{ "MIX", NULL, "DAC" },
281 	{ "MIX", "IN Switch", "IN1" },
282 	{ "MIX", "IN Switch", "IN2" },
283 
284 	/* connect board input to the codec left channel output pin */
285 	{ "DAC", NULL, "OUTL" },
286 };
287 
288 SND_SOC_DAILINK_DEFS(pcm,
289 	DAILINK_COMP_ARRAY(COMP_EMPTY()),
290 	DAILINK_COMP_ARRAY(COMP_CODEC(NULL, "pcm512x-hifi")),
291 	DAILINK_COMP_ARRAY(COMP_EMPTY()));
292 
293 static struct snd_soc_dai_link tse850_dailink = {
294 	.name = "TSE-850",
295 	.stream_name = "TSE-850-PCM",
296 	.dai_fmt = SND_SOC_DAIFMT_I2S
297 		 | SND_SOC_DAIFMT_NB_NF
298 		 | SND_SOC_DAIFMT_CBP_CFC,
299 	SND_SOC_DAILINK_REG(pcm),
300 };
301 
302 static struct snd_soc_card tse850_card = {
303 	.name = "TSE-850-ASoC",
304 	.owner = THIS_MODULE,
305 	.dai_link = &tse850_dailink,
306 	.num_links = 1,
307 	.dapm_widgets = tse850_dapm_widgets,
308 	.num_dapm_widgets = ARRAY_SIZE(tse850_dapm_widgets),
309 	.dapm_routes = tse850_intercon,
310 	.num_dapm_routes = ARRAY_SIZE(tse850_intercon),
311 	.fully_routed = true,
312 };
313 
tse850_dt_init(struct platform_device * pdev)314 static int tse850_dt_init(struct platform_device *pdev)
315 {
316 	struct device_node *np = pdev->dev.of_node;
317 	struct device_node *codec_np, *cpu_np;
318 	struct snd_soc_dai_link *dailink = &tse850_dailink;
319 
320 	if (!np) {
321 		dev_err(&pdev->dev, "only device tree supported\n");
322 		return -EINVAL;
323 	}
324 
325 	cpu_np = of_parse_phandle(np, "axentia,cpu-dai", 0);
326 	if (!cpu_np) {
327 		dev_err(&pdev->dev, "failed to get cpu dai\n");
328 		return -EINVAL;
329 	}
330 	dailink->cpus->of_node = cpu_np;
331 	dailink->platforms->of_node = cpu_np;
332 	of_node_put(cpu_np);
333 
334 	codec_np = of_parse_phandle(np, "axentia,audio-codec", 0);
335 	if (!codec_np) {
336 		dev_err(&pdev->dev, "failed to get codec info\n");
337 		return -EINVAL;
338 	}
339 	dailink->codecs->of_node = codec_np;
340 	of_node_put(codec_np);
341 
342 	return 0;
343 }
344 
tse850_probe(struct platform_device * pdev)345 static int tse850_probe(struct platform_device *pdev)
346 {
347 	struct snd_soc_card *card = &tse850_card;
348 	struct device *dev = card->dev = &pdev->dev;
349 	struct tse850_priv *tse850;
350 	int ret;
351 
352 	tse850 = devm_kzalloc(dev, sizeof(*tse850), GFP_KERNEL);
353 	if (!tse850)
354 		return -ENOMEM;
355 
356 	snd_soc_card_set_drvdata(card, tse850);
357 
358 	ret = tse850_dt_init(pdev);
359 	if (ret) {
360 		dev_err(dev, "failed to init dt info\n");
361 		return ret;
362 	}
363 
364 	tse850->add = devm_gpiod_get(dev, "axentia,add", GPIOD_OUT_HIGH);
365 	if (IS_ERR(tse850->add))
366 		return dev_err_probe(dev, PTR_ERR(tse850->add),
367 				     "failed to get 'add' gpio\n");
368 	tse850->add_cache = 1;
369 
370 	tse850->loop1 = devm_gpiod_get(dev, "axentia,loop1", GPIOD_OUT_HIGH);
371 	if (IS_ERR(tse850->loop1))
372 		return dev_err_probe(dev, PTR_ERR(tse850->loop1),
373 				     "failed to get 'loop1' gpio\n");
374 	tse850->loop1_cache = 1;
375 
376 	tse850->loop2 = devm_gpiod_get(dev, "axentia,loop2", GPIOD_OUT_HIGH);
377 	if (IS_ERR(tse850->loop2))
378 		return dev_err_probe(dev, PTR_ERR(tse850->loop2),
379 				     "failed to get 'loop2' gpio\n");
380 	tse850->loop2_cache = 1;
381 
382 	tse850->ana = devm_regulator_get(dev, "axentia,ana");
383 	if (IS_ERR(tse850->ana))
384 		return dev_err_probe(dev, PTR_ERR(tse850->ana),
385 				     "failed to get 'ana' regulator\n");
386 
387 	ret = regulator_enable(tse850->ana);
388 	if (ret < 0) {
389 		dev_err(dev, "failed to enable the 'ana' regulator\n");
390 		return ret;
391 	}
392 
393 	ret = snd_soc_register_card(card);
394 	if (ret) {
395 		dev_err(dev, "snd_soc_register_card failed\n");
396 		goto err_disable_ana;
397 	}
398 
399 	return 0;
400 
401 err_disable_ana:
402 	regulator_disable(tse850->ana);
403 	return ret;
404 }
405 
tse850_remove(struct platform_device * pdev)406 static void tse850_remove(struct platform_device *pdev)
407 {
408 	struct snd_soc_card *card = platform_get_drvdata(pdev);
409 	struct tse850_priv *tse850 = snd_soc_card_get_drvdata(card);
410 
411 	snd_soc_unregister_card(card);
412 	regulator_disable(tse850->ana);
413 }
414 
415 static const struct of_device_id tse850_dt_ids[] = {
416 	{ .compatible = "axentia,tse850-pcm5142", },
417 	{ /* sentinel */ }
418 };
419 MODULE_DEVICE_TABLE(of, tse850_dt_ids);
420 
421 static struct platform_driver tse850_driver = {
422 	.driver = {
423 		.name = "axentia-tse850-pcm5142",
424 		.of_match_table = tse850_dt_ids,
425 	},
426 	.probe = tse850_probe,
427 	.remove = tse850_remove,
428 };
429 
430 module_platform_driver(tse850_driver);
431 
432 /* Module information */
433 MODULE_AUTHOR("Peter Rosin <peda@axentia.se>");
434 MODULE_DESCRIPTION("ALSA SoC driver for TSE-850 with PCM5142 codec");
435 MODULE_LICENSE("GPL v2");
436