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