1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * tegra_asoc_machine.c - Universal ASoC machine driver for NVIDIA Tegra boards.
4  */
5 
6 #include <linux/clk.h>
7 #include <linux/export.h>
8 #include <linux/gpio/consumer.h>
9 #include <linux/module.h>
10 #include <linux/of.h>
11 #include <linux/of_device.h>
12 #include <linux/platform_device.h>
13 #include <linux/slab.h>
14 
15 #include <sound/core.h>
16 #include <sound/jack.h>
17 #include <sound/pcm.h>
18 #include <sound/pcm_params.h>
19 #include <sound/soc.h>
20 
21 #include "tegra_asoc_machine.h"
22 
23 /* Headphones Jack */
24 
25 static struct snd_soc_jack tegra_machine_hp_jack;
26 
27 static struct snd_soc_jack_pin tegra_machine_hp_jack_pins[] = {
28 	{ .pin = "Headphone",  .mask = SND_JACK_HEADPHONE },
29 	{ .pin = "Headphones", .mask = SND_JACK_HEADPHONE },
30 };
31 
32 static struct snd_soc_jack_gpio tegra_machine_hp_jack_gpio = {
33 	.name = "Headphones detection",
34 	.report = SND_JACK_HEADPHONE,
35 	.debounce_time = 150,
36 };
37 
38 /* Headset Jack */
39 
40 static struct snd_soc_jack tegra_machine_headset_jack;
41 
42 static struct snd_soc_jack_pin tegra_machine_headset_jack_pins[] = {
43 	{ .pin = "Headset Mic", .mask = SND_JACK_MICROPHONE },
44 	{ .pin = "Headset Stereophone", .mask = SND_JACK_HEADPHONE },
45 };
46 
47 static struct snd_soc_jack_gpio tegra_machine_headset_jack_gpio = {
48 	.name = "Headset detection",
49 	.report = SND_JACK_HEADSET,
50 	.debounce_time = 150,
51 };
52 
53 /* Mic Jack */
coupled_mic_hp_check(void * data)54 static int coupled_mic_hp_check(void *data)
55 {
56 	struct tegra_machine *machine = (struct tegra_machine *)data;
57 
58 	/* Detect mic insertion only if 3.5 jack is in */
59 	if (gpiod_get_value_cansleep(machine->gpiod_hp_det) &&
60 	    gpiod_get_value_cansleep(machine->gpiod_mic_det))
61 		return SND_JACK_MICROPHONE;
62 
63 	return 0;
64 }
65 
66 static struct snd_soc_jack tegra_machine_mic_jack;
67 
68 static struct snd_soc_jack_pin tegra_machine_mic_jack_pins[] = {
69 	{ .pin = "Mic Jack",    .mask = SND_JACK_MICROPHONE },
70 	{ .pin = "Headset Mic", .mask = SND_JACK_MICROPHONE },
71 };
72 
73 static struct snd_soc_jack_gpio tegra_machine_mic_jack_gpio = {
74 	.name = "Mic detection",
75 	.report = SND_JACK_MICROPHONE,
76 	.debounce_time = 150,
77 };
78 
tegra_machine_event(struct snd_soc_dapm_widget * w,struct snd_kcontrol * k,int event)79 static int tegra_machine_event(struct snd_soc_dapm_widget *w,
80 			       struct snd_kcontrol *k, int event)
81 {
82 	struct snd_soc_dapm_context *dapm = w->dapm;
83 	struct tegra_machine *machine = snd_soc_card_get_drvdata(dapm->card);
84 
85 	if (!strcmp(w->name, "Int Spk") || !strcmp(w->name, "Speakers"))
86 		gpiod_set_value_cansleep(machine->gpiod_spkr_en,
87 					 SND_SOC_DAPM_EVENT_ON(event));
88 
89 	if (!strcmp(w->name, "Mic Jack") || !strcmp(w->name, "Headset Mic"))
90 		gpiod_set_value_cansleep(machine->gpiod_ext_mic_en,
91 					 SND_SOC_DAPM_EVENT_ON(event));
92 
93 	if (!strcmp(w->name, "Int Mic") || !strcmp(w->name, "Internal Mic 2"))
94 		gpiod_set_value_cansleep(machine->gpiod_int_mic_en,
95 					 SND_SOC_DAPM_EVENT_ON(event));
96 
97 	if (!strcmp(w->name, "Headphone") || !strcmp(w->name, "Headphone Jack"))
98 		gpiod_set_value_cansleep(machine->gpiod_hp_mute,
99 					 !SND_SOC_DAPM_EVENT_ON(event));
100 
101 	return 0;
102 }
103 
104 static const struct snd_soc_dapm_widget tegra_machine_dapm_widgets[] = {
105 	SND_SOC_DAPM_HP("Headphone Jack", tegra_machine_event),
106 	SND_SOC_DAPM_HP("Headphone", tegra_machine_event),
107 	SND_SOC_DAPM_HP("Headset Stereophone", NULL),
108 	SND_SOC_DAPM_HP("Headphones", NULL),
109 	SND_SOC_DAPM_SPK("Speakers", tegra_machine_event),
110 	SND_SOC_DAPM_SPK("Int Spk", tegra_machine_event),
111 	SND_SOC_DAPM_SPK("Earpiece", NULL),
112 	SND_SOC_DAPM_MIC("Int Mic", tegra_machine_event),
113 	SND_SOC_DAPM_MIC("Mic Jack", tegra_machine_event),
114 	SND_SOC_DAPM_MIC("Internal Mic 1", NULL),
115 	SND_SOC_DAPM_MIC("Internal Mic 2", tegra_machine_event),
116 	SND_SOC_DAPM_MIC("Headset Mic", tegra_machine_event),
117 	SND_SOC_DAPM_MIC("Digital Mic", NULL),
118 	SND_SOC_DAPM_MIC("Mic", NULL),
119 	SND_SOC_DAPM_LINE("Line In Jack", NULL),
120 	SND_SOC_DAPM_LINE("Line In", NULL),
121 	SND_SOC_DAPM_LINE("LineIn", NULL),
122 };
123 
124 static const struct snd_kcontrol_new tegra_machine_controls[] = {
125 	SOC_DAPM_PIN_SWITCH("Speakers"),
126 	SOC_DAPM_PIN_SWITCH("Int Spk"),
127 	SOC_DAPM_PIN_SWITCH("Earpiece"),
128 	SOC_DAPM_PIN_SWITCH("Int Mic"),
129 	SOC_DAPM_PIN_SWITCH("Headset Mic"),
130 	SOC_DAPM_PIN_SWITCH("Internal Mic 1"),
131 	SOC_DAPM_PIN_SWITCH("Internal Mic 2"),
132 	SOC_DAPM_PIN_SWITCH("Headphones"),
133 	SOC_DAPM_PIN_SWITCH("Mic Jack"),
134 };
135 
tegra_asoc_machine_init(struct snd_soc_pcm_runtime * rtd)136 int tegra_asoc_machine_init(struct snd_soc_pcm_runtime *rtd)
137 {
138 	struct snd_soc_card *card = rtd->card;
139 	struct tegra_machine *machine = snd_soc_card_get_drvdata(card);
140 	const char *jack_name;
141 	int err;
142 
143 	if (machine->gpiod_hp_det && machine->asoc->add_hp_jack) {
144 		if (machine->asoc->hp_jack_name)
145 			jack_name = machine->asoc->hp_jack_name;
146 		else
147 			jack_name = "Headphones Jack";
148 
149 		err = snd_soc_card_jack_new_pins(card, jack_name,
150 						 SND_JACK_HEADPHONE,
151 						 &tegra_machine_hp_jack,
152 						 tegra_machine_hp_jack_pins,
153 						 ARRAY_SIZE(tegra_machine_hp_jack_pins));
154 		if (err) {
155 			dev_err(rtd->dev,
156 				"Headphones Jack creation failed: %d\n", err);
157 			return err;
158 		}
159 
160 		tegra_machine_hp_jack_gpio.desc = machine->gpiod_hp_det;
161 
162 		err = snd_soc_jack_add_gpios(&tegra_machine_hp_jack, 1,
163 					     &tegra_machine_hp_jack_gpio);
164 		if (err)
165 			dev_err(rtd->dev, "HP GPIOs not added: %d\n", err);
166 	}
167 
168 	if (machine->gpiod_hp_det && machine->asoc->add_headset_jack) {
169 		err = snd_soc_card_jack_new_pins(card, "Headset Jack",
170 						 SND_JACK_HEADSET,
171 						 &tegra_machine_headset_jack,
172 						 tegra_machine_headset_jack_pins,
173 						 ARRAY_SIZE(tegra_machine_headset_jack_pins));
174 		if (err) {
175 			dev_err(rtd->dev,
176 				"Headset Jack creation failed: %d\n", err);
177 			return err;
178 		}
179 
180 		tegra_machine_headset_jack_gpio.desc = machine->gpiod_hp_det;
181 
182 		err = snd_soc_jack_add_gpios(&tegra_machine_headset_jack, 1,
183 					     &tegra_machine_headset_jack_gpio);
184 		if (err)
185 			dev_err(rtd->dev, "Headset GPIOs not added: %d\n", err);
186 	}
187 
188 	if (machine->gpiod_mic_det && machine->asoc->add_mic_jack) {
189 		err = snd_soc_card_jack_new_pins(rtd->card, "Mic Jack",
190 						 SND_JACK_MICROPHONE,
191 						 &tegra_machine_mic_jack,
192 						 tegra_machine_mic_jack_pins,
193 						 ARRAY_SIZE(tegra_machine_mic_jack_pins));
194 		if (err) {
195 			dev_err(rtd->dev, "Mic Jack creation failed: %d\n", err);
196 			return err;
197 		}
198 
199 		tegra_machine_mic_jack_gpio.data = machine;
200 		tegra_machine_mic_jack_gpio.desc = machine->gpiod_mic_det;
201 
202 		if (of_property_read_bool(card->dev->of_node,
203 					  "nvidia,coupled-mic-hp-det")) {
204 			tegra_machine_mic_jack_gpio.desc = machine->gpiod_hp_det;
205 			tegra_machine_mic_jack_gpio.jack_status_check = coupled_mic_hp_check;
206 		}
207 
208 		err = snd_soc_jack_add_gpios(&tegra_machine_mic_jack, 1,
209 					     &tegra_machine_mic_jack_gpio);
210 		if (err)
211 			dev_err(rtd->dev, "Mic GPIOs not added: %d\n", err);
212 	}
213 
214 	return 0;
215 }
216 EXPORT_SYMBOL_GPL(tegra_asoc_machine_init);
217 
tegra_machine_mclk_rate_128(unsigned int srate)218 static unsigned int tegra_machine_mclk_rate_128(unsigned int srate)
219 {
220 	return 128 * srate;
221 }
222 
tegra_machine_mclk_rate_256(unsigned int srate)223 static unsigned int tegra_machine_mclk_rate_256(unsigned int srate)
224 {
225 	return 256 * srate;
226 }
227 
tegra_machine_mclk_rate_512(unsigned int srate)228 static unsigned int tegra_machine_mclk_rate_512(unsigned int srate)
229 {
230 	return 512 * srate;
231 }
232 
tegra_machine_mclk_rate_12mhz(unsigned int srate)233 static unsigned int tegra_machine_mclk_rate_12mhz(unsigned int srate)
234 {
235 	unsigned int mclk;
236 
237 	switch (srate) {
238 	case 8000:
239 	case 16000:
240 	case 24000:
241 	case 32000:
242 	case 48000:
243 	case 64000:
244 	case 96000:
245 		mclk = 12288000;
246 		break;
247 	case 11025:
248 	case 22050:
249 	case 44100:
250 	case 88200:
251 		mclk = 11289600;
252 		break;
253 	default:
254 		mclk = 12000000;
255 		break;
256 	}
257 
258 	return mclk;
259 }
260 
tegra_machine_mclk_rate_6mhz(unsigned int srate)261 static unsigned int tegra_machine_mclk_rate_6mhz(unsigned int srate)
262 {
263 	unsigned int mclk;
264 
265 	switch (srate) {
266 	case 8000:
267 	case 16000:
268 	case 64000:
269 		mclk = 8192000;
270 		break;
271 	case 11025:
272 	case 22050:
273 	case 88200:
274 		mclk = 11289600;
275 		break;
276 	case 96000:
277 		mclk = 12288000;
278 		break;
279 	default:
280 		mclk = 256 * srate;
281 		break;
282 	}
283 
284 	return mclk;
285 }
286 
tegra_machine_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params)287 static int tegra_machine_hw_params(struct snd_pcm_substream *substream,
288 				   struct snd_pcm_hw_params *params)
289 {
290 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
291 	struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
292 	struct snd_soc_card *card = rtd->card;
293 	struct tegra_machine *machine = snd_soc_card_get_drvdata(card);
294 	unsigned int srate = params_rate(params);
295 	unsigned int mclk = machine->asoc->mclk_rate(srate);
296 	unsigned int clk_id = machine->asoc->mclk_id;
297 	unsigned int new_baseclock;
298 	int err;
299 
300 	switch (srate) {
301 	case 11025:
302 	case 22050:
303 	case 44100:
304 	case 88200:
305 		if (of_machine_is_compatible("nvidia,tegra20"))
306 			new_baseclock = 56448000;
307 		else if (of_machine_is_compatible("nvidia,tegra30"))
308 			new_baseclock = 564480000;
309 		else
310 			new_baseclock = 282240000;
311 		break;
312 	case 8000:
313 	case 16000:
314 	case 32000:
315 	case 48000:
316 	case 64000:
317 	case 96000:
318 		if (of_machine_is_compatible("nvidia,tegra20"))
319 			new_baseclock = 73728000;
320 		else if (of_machine_is_compatible("nvidia,tegra30"))
321 			new_baseclock = 552960000;
322 		else
323 			new_baseclock = 368640000;
324 		break;
325 	default:
326 		dev_err(card->dev, "Invalid sound rate: %u\n", srate);
327 		return -EINVAL;
328 	}
329 
330 	if (new_baseclock != machine->set_baseclock ||
331 	    mclk != machine->set_mclk) {
332 		machine->set_baseclock = 0;
333 		machine->set_mclk = 0;
334 
335 		clk_disable_unprepare(machine->clk_cdev1);
336 
337 		err = clk_set_rate(machine->clk_pll_a, new_baseclock);
338 		if (err) {
339 			dev_err(card->dev, "Can't set pll_a rate: %d\n", err);
340 			return err;
341 		}
342 
343 		err = clk_set_rate(machine->clk_pll_a_out0, mclk);
344 		if (err) {
345 			dev_err(card->dev, "Can't set pll_a_out0 rate: %d\n", err);
346 			return err;
347 		}
348 
349 		/* Don't set cdev1/extern1 rate; it's locked to pll_a_out0 */
350 
351 		err = clk_prepare_enable(machine->clk_cdev1);
352 		if (err) {
353 			dev_err(card->dev, "Can't enable cdev1: %d\n", err);
354 			return err;
355 		}
356 
357 		machine->set_baseclock = new_baseclock;
358 		machine->set_mclk = mclk;
359 	}
360 
361 	err = snd_soc_dai_set_sysclk(codec_dai, clk_id, mclk, SND_SOC_CLOCK_IN);
362 	if (err < 0) {
363 		dev_err(card->dev, "codec_dai clock not set: %d\n", err);
364 		return err;
365 	}
366 
367 	return 0;
368 }
369 
370 static const struct snd_soc_ops tegra_machine_snd_ops = {
371 	.hw_params = tegra_machine_hw_params,
372 };
373 
tegra_machine_node_release(void * of_node)374 static void tegra_machine_node_release(void *of_node)
375 {
376 	of_node_put(of_node);
377 }
378 
379 static struct device_node *
tegra_machine_parse_phandle(struct device * dev,const char * name)380 tegra_machine_parse_phandle(struct device *dev, const char *name)
381 {
382 	struct device_node *np;
383 	int err;
384 
385 	np = of_parse_phandle(dev->of_node, name, 0);
386 	if (!np) {
387 		dev_err(dev, "Property '%s' missing or invalid\n", name);
388 		return ERR_PTR(-EINVAL);
389 	}
390 
391 	err = devm_add_action_or_reset(dev, tegra_machine_node_release, np);
392 	if (err)
393 		return ERR_PTR(err);
394 
395 	return np;
396 }
397 
tegra_machine_unregister_codec(void * pdev)398 static void tegra_machine_unregister_codec(void *pdev)
399 {
400 	platform_device_unregister(pdev);
401 }
402 
tegra_machine_register_codec(struct device * dev,const char * name)403 static int tegra_machine_register_codec(struct device *dev, const char *name)
404 {
405 	struct platform_device *pdev;
406 	int err;
407 
408 	if (!name)
409 		return 0;
410 
411 	pdev = platform_device_register_simple(name, -1, NULL, 0);
412 	if (IS_ERR(pdev))
413 		return PTR_ERR(pdev);
414 
415 	err = devm_add_action_or_reset(dev, tegra_machine_unregister_codec,
416 				       pdev);
417 	if (err)
418 		return err;
419 
420 	return 0;
421 }
422 
tegra_asoc_machine_probe(struct platform_device * pdev)423 int tegra_asoc_machine_probe(struct platform_device *pdev)
424 {
425 	struct device_node *np_codec, *np_i2s, *np_ac97;
426 	const struct tegra_asoc_data *asoc;
427 	struct device *dev = &pdev->dev;
428 	struct tegra_machine *machine;
429 	struct snd_soc_card *card;
430 	struct gpio_desc *gpiod;
431 	int err;
432 
433 	machine = devm_kzalloc(dev, sizeof(*machine), GFP_KERNEL);
434 	if (!machine)
435 		return -ENOMEM;
436 
437 	asoc = of_device_get_match_data(dev);
438 	card = asoc->card;
439 	card->dev = dev;
440 
441 	machine->asoc = asoc;
442 	machine->mic_jack = &tegra_machine_mic_jack;
443 	machine->hp_jack_gpio = &tegra_machine_hp_jack_gpio;
444 	snd_soc_card_set_drvdata(card, machine);
445 
446 	gpiod = devm_gpiod_get_optional(dev, "nvidia,hp-mute", GPIOD_OUT_HIGH);
447 	machine->gpiod_hp_mute = gpiod;
448 	if (IS_ERR(gpiod))
449 		return PTR_ERR(gpiod);
450 
451 	gpiod = devm_gpiod_get_optional(dev, "nvidia,hp-det", GPIOD_IN);
452 	machine->gpiod_hp_det = gpiod;
453 	if (IS_ERR(gpiod))
454 		return PTR_ERR(gpiod);
455 
456 	gpiod = devm_gpiod_get_optional(dev, "nvidia,mic-det", GPIOD_IN);
457 	machine->gpiod_mic_det = gpiod;
458 	if (IS_ERR(gpiod))
459 		return PTR_ERR(gpiod);
460 
461 	gpiod = devm_gpiod_get_optional(dev, "nvidia,spkr-en", GPIOD_OUT_LOW);
462 	machine->gpiod_spkr_en = gpiod;
463 	if (IS_ERR(gpiod))
464 		return PTR_ERR(gpiod);
465 
466 	gpiod = devm_gpiod_get_optional(dev, "nvidia,int-mic-en", GPIOD_OUT_LOW);
467 	machine->gpiod_int_mic_en = gpiod;
468 	if (IS_ERR(gpiod))
469 		return PTR_ERR(gpiod);
470 
471 	gpiod = devm_gpiod_get_optional(dev, "nvidia,ext-mic-en", GPIOD_OUT_LOW);
472 	machine->gpiod_ext_mic_en = gpiod;
473 	if (IS_ERR(gpiod))
474 		return PTR_ERR(gpiod);
475 
476 	err = snd_soc_of_parse_card_name(card, "nvidia,model");
477 	if (err)
478 		return err;
479 
480 	if (!card->dapm_routes) {
481 		err = snd_soc_of_parse_audio_routing(card, "nvidia,audio-routing");
482 		if (err)
483 			return err;
484 	}
485 
486 	if (asoc->set_ac97) {
487 		err = tegra_machine_register_codec(dev, asoc->codec_dev_name);
488 		if (err)
489 			return err;
490 
491 		np_ac97 = tegra_machine_parse_phandle(dev, "nvidia,ac97-controller");
492 		if (IS_ERR(np_ac97))
493 			return PTR_ERR(np_ac97);
494 
495 		card->dai_link->cpus->of_node = np_ac97;
496 		card->dai_link->platforms->of_node = np_ac97;
497 	} else {
498 		np_codec = tegra_machine_parse_phandle(dev, "nvidia,audio-codec");
499 		if (IS_ERR(np_codec))
500 			return PTR_ERR(np_codec);
501 
502 		np_i2s = tegra_machine_parse_phandle(dev, "nvidia,i2s-controller");
503 		if (IS_ERR(np_i2s))
504 			return PTR_ERR(np_i2s);
505 
506 		card->dai_link->cpus->of_node = np_i2s;
507 		card->dai_link->codecs->of_node = np_codec;
508 		card->dai_link->platforms->of_node = np_i2s;
509 	}
510 
511 	if (asoc->add_common_controls) {
512 		card->controls = tegra_machine_controls;
513 		card->num_controls = ARRAY_SIZE(tegra_machine_controls);
514 	}
515 
516 	if (asoc->add_common_dapm_widgets) {
517 		card->dapm_widgets = tegra_machine_dapm_widgets;
518 		card->num_dapm_widgets = ARRAY_SIZE(tegra_machine_dapm_widgets);
519 	}
520 
521 	if (asoc->add_common_snd_ops)
522 		card->dai_link->ops = &tegra_machine_snd_ops;
523 
524 	if (!card->owner)
525 		card->owner = THIS_MODULE;
526 	if (!card->driver_name)
527 		card->driver_name = "tegra";
528 
529 	machine->clk_pll_a = devm_clk_get(dev, "pll_a");
530 	if (IS_ERR(machine->clk_pll_a)) {
531 		dev_err(dev, "Can't retrieve clk pll_a\n");
532 		return PTR_ERR(machine->clk_pll_a);
533 	}
534 
535 	machine->clk_pll_a_out0 = devm_clk_get(dev, "pll_a_out0");
536 	if (IS_ERR(machine->clk_pll_a_out0)) {
537 		dev_err(dev, "Can't retrieve clk pll_a_out0\n");
538 		return PTR_ERR(machine->clk_pll_a_out0);
539 	}
540 
541 	machine->clk_cdev1 = devm_clk_get(dev, "mclk");
542 	if (IS_ERR(machine->clk_cdev1)) {
543 		dev_err(dev, "Can't retrieve clk cdev1\n");
544 		return PTR_ERR(machine->clk_cdev1);
545 	}
546 
547 	/*
548 	 * If clock parents are not set in DT, configure here to use clk_out_1
549 	 * as mclk and extern1 as parent for Tegra30 and higher.
550 	 */
551 	if (!of_property_present(dev->of_node, "assigned-clock-parents") &&
552 	    !of_machine_is_compatible("nvidia,tegra20")) {
553 		struct clk *clk_out_1, *clk_extern1;
554 
555 		dev_warn(dev, "Configuring clocks for a legacy device-tree\n");
556 		dev_warn(dev, "Please update DT to use assigned-clock-parents\n");
557 
558 		clk_extern1 = devm_clk_get(dev, "extern1");
559 		if (IS_ERR(clk_extern1)) {
560 			dev_err(dev, "Can't retrieve clk extern1\n");
561 			return PTR_ERR(clk_extern1);
562 		}
563 
564 		err = clk_set_parent(clk_extern1, machine->clk_pll_a_out0);
565 		if (err < 0) {
566 			dev_err(dev, "Set parent failed for clk extern1\n");
567 			return err;
568 		}
569 
570 		clk_out_1 = devm_clk_get(dev, "pmc_clk_out_1");
571 		if (IS_ERR(clk_out_1)) {
572 			dev_err(dev, "Can't retrieve pmc_clk_out_1\n");
573 			return PTR_ERR(clk_out_1);
574 		}
575 
576 		err = clk_set_parent(clk_out_1, clk_extern1);
577 		if (err < 0) {
578 			dev_err(dev, "Set parent failed for pmc_clk_out_1\n");
579 			return err;
580 		}
581 
582 		machine->clk_cdev1 = clk_out_1;
583 	}
584 
585 	if (asoc->set_ac97) {
586 		/*
587 		 * AC97 rate is fixed at 24.576MHz and is used for both the
588 		 * host controller and the external codec
589 		 */
590 		err = clk_set_rate(machine->clk_pll_a, 73728000);
591 		if (err) {
592 			dev_err(dev, "Can't set pll_a rate: %d\n", err);
593 			return err;
594 		}
595 
596 		err = clk_set_rate(machine->clk_pll_a_out0, 24576000);
597 		if (err) {
598 			dev_err(dev, "Can't set pll_a_out0 rate: %d\n", err);
599 			return err;
600 		}
601 
602 		machine->set_baseclock = 73728000;
603 		machine->set_mclk = 24576000;
604 	}
605 
606 	/*
607 	 * FIXME: There is some unknown dependency between audio MCLK disable
608 	 * and suspend-resume functionality on Tegra30, although audio MCLK is
609 	 * only needed for audio.
610 	 */
611 	err = clk_prepare_enable(machine->clk_cdev1);
612 	if (err) {
613 		dev_err(dev, "Can't enable cdev1: %d\n", err);
614 		return err;
615 	}
616 
617 	err = devm_snd_soc_register_card(dev, card);
618 	if (err)
619 		return err;
620 
621 	return 0;
622 }
623 EXPORT_SYMBOL_GPL(tegra_asoc_machine_probe);
624 
625 /* WM8753 machine */
626 
627 SND_SOC_DAILINK_DEFS(wm8753_hifi,
628 	DAILINK_COMP_ARRAY(COMP_EMPTY()),
629 	DAILINK_COMP_ARRAY(COMP_CODEC(NULL, "wm8753-hifi")),
630 	DAILINK_COMP_ARRAY(COMP_EMPTY()));
631 
632 static struct snd_soc_dai_link tegra_wm8753_dai = {
633 	.name = "WM8753",
634 	.stream_name = "WM8753 PCM",
635 	.dai_fmt = SND_SOC_DAIFMT_I2S |
636 		   SND_SOC_DAIFMT_NB_NF |
637 		   SND_SOC_DAIFMT_CBS_CFS,
638 	SND_SOC_DAILINK_REG(wm8753_hifi),
639 };
640 
641 static struct snd_soc_card snd_soc_tegra_wm8753 = {
642 	.components = "codec:wm8753",
643 	.dai_link = &tegra_wm8753_dai,
644 	.num_links = 1,
645 	.fully_routed = true,
646 };
647 
648 static const struct tegra_asoc_data tegra_wm8753_data = {
649 	.mclk_rate = tegra_machine_mclk_rate_12mhz,
650 	.card = &snd_soc_tegra_wm8753,
651 	.add_common_dapm_widgets = true,
652 	.add_common_snd_ops = true,
653 };
654 
655 /* WM9712 machine */
656 
tegra_wm9712_init(struct snd_soc_pcm_runtime * rtd)657 static int tegra_wm9712_init(struct snd_soc_pcm_runtime *rtd)
658 {
659 	return snd_soc_dapm_force_enable_pin(&rtd->card->dapm, "Mic Bias");
660 }
661 
662 SND_SOC_DAILINK_DEFS(wm9712_hifi,
663 	DAILINK_COMP_ARRAY(COMP_EMPTY()),
664 	DAILINK_COMP_ARRAY(COMP_CODEC("wm9712-codec", "wm9712-hifi")),
665 	DAILINK_COMP_ARRAY(COMP_EMPTY()));
666 
667 static struct snd_soc_dai_link tegra_wm9712_dai = {
668 	.name = "AC97 HiFi",
669 	.stream_name = "AC97 HiFi",
670 	.init = tegra_wm9712_init,
671 	SND_SOC_DAILINK_REG(wm9712_hifi),
672 };
673 
674 static struct snd_soc_card snd_soc_tegra_wm9712 = {
675 	.components = "codec:wm9712",
676 	.dai_link = &tegra_wm9712_dai,
677 	.num_links = 1,
678 	.fully_routed = true,
679 };
680 
681 static const struct tegra_asoc_data tegra_wm9712_data = {
682 	.card = &snd_soc_tegra_wm9712,
683 	.add_common_dapm_widgets = true,
684 	.codec_dev_name = "wm9712-codec",
685 	.set_ac97 = true,
686 };
687 
688 /* MAX98090 machine */
689 
690 SND_SOC_DAILINK_DEFS(max98090_hifi,
691 	DAILINK_COMP_ARRAY(COMP_EMPTY()),
692 	DAILINK_COMP_ARRAY(COMP_CODEC(NULL, "HiFi")),
693 	DAILINK_COMP_ARRAY(COMP_EMPTY()));
694 
695 static struct snd_soc_dai_link tegra_max98090_dai = {
696 	.name = "max98090",
697 	.stream_name = "max98090 PCM",
698 	.init = tegra_asoc_machine_init,
699 	.dai_fmt = SND_SOC_DAIFMT_I2S |
700 		   SND_SOC_DAIFMT_NB_NF |
701 		   SND_SOC_DAIFMT_CBS_CFS,
702 	SND_SOC_DAILINK_REG(max98090_hifi),
703 };
704 
705 static struct snd_soc_card snd_soc_tegra_max98090 = {
706 	.components = "codec:max98090",
707 	.dai_link = &tegra_max98090_dai,
708 	.num_links = 1,
709 	.fully_routed = true,
710 };
711 
712 static const struct tegra_asoc_data tegra_max98090_data = {
713 	.mclk_rate = tegra_machine_mclk_rate_12mhz,
714 	.card = &snd_soc_tegra_max98090,
715 	.hp_jack_name = "Headphones",
716 	.add_common_dapm_widgets = true,
717 	.add_common_controls = true,
718 	.add_common_snd_ops = true,
719 	.add_mic_jack = true,
720 	.add_hp_jack = true,
721 };
722 
723 /* MAX98088 machine */
724 
725 SND_SOC_DAILINK_DEFS(max98088_hifi,
726 	DAILINK_COMP_ARRAY(COMP_EMPTY()),
727 	DAILINK_COMP_ARRAY(COMP_CODEC(NULL, "HiFi")),
728 	DAILINK_COMP_ARRAY(COMP_EMPTY()));
729 
730 static struct snd_soc_dai_link tegra_max98088_dai = {
731 	.name = "MAX98088",
732 	.stream_name = "MAX98088 PCM",
733 	.init = tegra_asoc_machine_init,
734 	.dai_fmt = SND_SOC_DAIFMT_I2S |
735 		   SND_SOC_DAIFMT_NB_NF |
736 		   SND_SOC_DAIFMT_CBS_CFS,
737 	SND_SOC_DAILINK_REG(max98088_hifi),
738 };
739 
740 static struct snd_soc_card snd_soc_tegra_max98088 = {
741 	.components = "codec:max98088",
742 	.dai_link = &tegra_max98088_dai,
743 	.num_links = 1,
744 	.fully_routed = true,
745 };
746 
747 static const struct tegra_asoc_data tegra_max98088_data = {
748 	.mclk_rate = tegra_machine_mclk_rate_12mhz,
749 	.card = &snd_soc_tegra_max98088,
750 	.add_common_dapm_widgets = true,
751 	.add_common_controls = true,
752 	.add_common_snd_ops = true,
753 	.add_mic_jack = true,
754 	.add_hp_jack = true,
755 };
756 
757 /* SGTL5000 machine */
758 
759 SND_SOC_DAILINK_DEFS(sgtl5000_hifi,
760 	DAILINK_COMP_ARRAY(COMP_EMPTY()),
761 	DAILINK_COMP_ARRAY(COMP_CODEC(NULL, "sgtl5000")),
762 	DAILINK_COMP_ARRAY(COMP_EMPTY()));
763 
764 static struct snd_soc_dai_link tegra_sgtl5000_dai = {
765 	.name = "sgtl5000",
766 	.stream_name = "HiFi",
767 	.dai_fmt = SND_SOC_DAIFMT_I2S |
768 		   SND_SOC_DAIFMT_NB_NF |
769 		   SND_SOC_DAIFMT_CBS_CFS,
770 	SND_SOC_DAILINK_REG(sgtl5000_hifi),
771 };
772 
773 static struct snd_soc_card snd_soc_tegra_sgtl5000 = {
774 	.components = "codec:sgtl5000",
775 	.dai_link = &tegra_sgtl5000_dai,
776 	.num_links = 1,
777 	.fully_routed = true,
778 };
779 
780 static const struct tegra_asoc_data tegra_sgtl5000_data = {
781 	.mclk_rate = tegra_machine_mclk_rate_12mhz,
782 	.card = &snd_soc_tegra_sgtl5000,
783 	.add_common_dapm_widgets = true,
784 	.add_common_snd_ops = true,
785 };
786 
787 /* TLV320AIC23 machine */
788 
789 static const struct snd_soc_dapm_widget trimslice_dapm_widgets[] = {
790 	SND_SOC_DAPM_HP("Line Out", NULL),
791 	SND_SOC_DAPM_LINE("Line In", NULL),
792 };
793 
794 static const struct snd_soc_dapm_route trimslice_audio_map[] = {
795 	{"Line Out", NULL, "LOUT"},
796 	{"Line Out", NULL, "ROUT"},
797 
798 	{"LLINEIN", NULL, "Line In"},
799 	{"RLINEIN", NULL, "Line In"},
800 };
801 
802 SND_SOC_DAILINK_DEFS(tlv320aic23_hifi,
803 	DAILINK_COMP_ARRAY(COMP_EMPTY()),
804 	DAILINK_COMP_ARRAY(COMP_CODEC(NULL, "tlv320aic23-hifi")),
805 	DAILINK_COMP_ARRAY(COMP_EMPTY()));
806 
807 static struct snd_soc_dai_link tegra_tlv320aic23_dai = {
808 	.name = "TLV320AIC23",
809 	.stream_name = "AIC23",
810 	.dai_fmt = SND_SOC_DAIFMT_I2S |
811 		   SND_SOC_DAIFMT_NB_NF |
812 		   SND_SOC_DAIFMT_CBS_CFS,
813 	SND_SOC_DAILINK_REG(tlv320aic23_hifi),
814 };
815 
816 static struct snd_soc_card snd_soc_tegra_trimslice = {
817 	.name = "tegra-trimslice",
818 	.components = "codec:tlv320aic23",
819 	.dai_link = &tegra_tlv320aic23_dai,
820 	.num_links = 1,
821 	.dapm_widgets = trimslice_dapm_widgets,
822 	.num_dapm_widgets = ARRAY_SIZE(trimslice_dapm_widgets),
823 	.dapm_routes = trimslice_audio_map,
824 	.num_dapm_routes = ARRAY_SIZE(trimslice_audio_map),
825 	.fully_routed = true,
826 };
827 
828 static const struct tegra_asoc_data tegra_trimslice_data = {
829 	.mclk_rate = tegra_machine_mclk_rate_128,
830 	.card = &snd_soc_tegra_trimslice,
831 	.add_common_snd_ops = true,
832 };
833 
834 /* RT5677 machine */
835 
tegra_rt5677_init(struct snd_soc_pcm_runtime * rtd)836 static int tegra_rt5677_init(struct snd_soc_pcm_runtime *rtd)
837 {
838 	struct snd_soc_card *card = rtd->card;
839 	int err;
840 
841 	err = tegra_asoc_machine_init(rtd);
842 	if (err)
843 		return err;
844 
845 	snd_soc_dapm_force_enable_pin(&card->dapm, "MICBIAS1");
846 
847 	return 0;
848 }
849 
850 SND_SOC_DAILINK_DEFS(rt5677_aif1,
851 	DAILINK_COMP_ARRAY(COMP_EMPTY()),
852 	DAILINK_COMP_ARRAY(COMP_CODEC(NULL, "rt5677-aif1")),
853 	DAILINK_COMP_ARRAY(COMP_EMPTY()));
854 
855 static struct snd_soc_dai_link tegra_rt5677_dai = {
856 	.name = "RT5677",
857 	.stream_name = "RT5677 PCM",
858 	.init = tegra_rt5677_init,
859 	.dai_fmt = SND_SOC_DAIFMT_I2S |
860 		   SND_SOC_DAIFMT_NB_NF |
861 		   SND_SOC_DAIFMT_CBS_CFS,
862 	SND_SOC_DAILINK_REG(rt5677_aif1),
863 };
864 
865 static struct snd_soc_card snd_soc_tegra_rt5677 = {
866 	.components = "codec:rt5677",
867 	.dai_link = &tegra_rt5677_dai,
868 	.num_links = 1,
869 	.fully_routed = true,
870 };
871 
872 static const struct tegra_asoc_data tegra_rt5677_data = {
873 	.mclk_rate = tegra_machine_mclk_rate_256,
874 	.card = &snd_soc_tegra_rt5677,
875 	.add_common_dapm_widgets = true,
876 	.add_common_controls = true,
877 	.add_common_snd_ops = true,
878 	.add_mic_jack = true,
879 	.add_hp_jack = true,
880 };
881 
882 /* RT5640 machine */
883 
884 SND_SOC_DAILINK_DEFS(rt5640_aif1,
885 	DAILINK_COMP_ARRAY(COMP_EMPTY()),
886 	DAILINK_COMP_ARRAY(COMP_CODEC(NULL, "rt5640-aif1")),
887 	DAILINK_COMP_ARRAY(COMP_EMPTY()));
888 
889 static struct snd_soc_dai_link tegra_rt5640_dai = {
890 	.name = "RT5640",
891 	.stream_name = "RT5640 PCM",
892 	.init = tegra_asoc_machine_init,
893 	.dai_fmt = SND_SOC_DAIFMT_I2S |
894 		   SND_SOC_DAIFMT_NB_NF |
895 		   SND_SOC_DAIFMT_CBS_CFS,
896 	SND_SOC_DAILINK_REG(rt5640_aif1),
897 };
898 
899 static struct snd_soc_card snd_soc_tegra_rt5640 = {
900 	.components = "codec:rt5640",
901 	.dai_link = &tegra_rt5640_dai,
902 	.num_links = 1,
903 	.fully_routed = true,
904 };
905 
906 static const struct tegra_asoc_data tegra_rt5640_data = {
907 	.mclk_rate = tegra_machine_mclk_rate_256,
908 	.card = &snd_soc_tegra_rt5640,
909 	.add_common_dapm_widgets = true,
910 	.add_common_controls = true,
911 	.add_common_snd_ops = true,
912 	.add_hp_jack = true,
913 };
914 
915 /* RT5632 machine */
916 
917 SND_SOC_DAILINK_DEFS(rt5632_hifi,
918 	DAILINK_COMP_ARRAY(COMP_EMPTY()),
919 	DAILINK_COMP_ARRAY(COMP_CODEC(NULL, "alc5632-hifi")),
920 	DAILINK_COMP_ARRAY(COMP_EMPTY()));
921 
922 static struct snd_soc_dai_link tegra_rt5632_dai = {
923 	.name = "ALC5632",
924 	.stream_name = "ALC5632 PCM",
925 	.init = tegra_rt5677_init,
926 	.dai_fmt = SND_SOC_DAIFMT_I2S |
927 		   SND_SOC_DAIFMT_NB_NF |
928 		   SND_SOC_DAIFMT_CBS_CFS,
929 	SND_SOC_DAILINK_REG(rt5632_hifi),
930 };
931 
932 static struct snd_soc_card snd_soc_tegra_rt5632 = {
933 	.components = "codec:rt5632",
934 	.dai_link = &tegra_rt5632_dai,
935 	.num_links = 1,
936 	.fully_routed = true,
937 };
938 
939 static const struct tegra_asoc_data tegra_rt5632_data = {
940 	.mclk_rate = tegra_machine_mclk_rate_512,
941 	.card = &snd_soc_tegra_rt5632,
942 	.add_common_dapm_widgets = true,
943 	.add_common_controls = true,
944 	.add_common_snd_ops = true,
945 	.add_headset_jack = true,
946 };
947 
948 /* RT5631 machine */
949 
950 SND_SOC_DAILINK_DEFS(rt5631_hifi,
951 	DAILINK_COMP_ARRAY(COMP_EMPTY()),
952 	DAILINK_COMP_ARRAY(COMP_CODEC(NULL, "rt5631-hifi")),
953 	DAILINK_COMP_ARRAY(COMP_EMPTY()));
954 
955 static struct snd_soc_dai_link tegra_rt5631_dai = {
956 	.name = "RT5631",
957 	.stream_name = "RT5631 PCM",
958 	.init = tegra_asoc_machine_init,
959 	.dai_fmt = SND_SOC_DAIFMT_I2S |
960 		   SND_SOC_DAIFMT_NB_NF |
961 		   SND_SOC_DAIFMT_CBS_CFS,
962 	SND_SOC_DAILINK_REG(rt5631_hifi),
963 };
964 
965 static struct snd_soc_card snd_soc_tegra_rt5631 = {
966 	.components = "codec:rt5631",
967 	.dai_link = &tegra_rt5631_dai,
968 	.num_links = 1,
969 	.fully_routed = true,
970 };
971 
972 static const struct tegra_asoc_data tegra_rt5631_data = {
973 	.mclk_rate = tegra_machine_mclk_rate_6mhz,
974 	.card = &snd_soc_tegra_rt5631,
975 	.add_common_dapm_widgets = true,
976 	.add_common_controls = true,
977 	.add_common_snd_ops = true,
978 	.add_mic_jack = true,
979 	.add_hp_jack = true,
980 };
981 
982 static const struct of_device_id tegra_machine_of_match[] = {
983 	{ .compatible = "nvidia,tegra-audio-trimslice", .data = &tegra_trimslice_data },
984 	{ .compatible = "nvidia,tegra-audio-max98090", .data = &tegra_max98090_data },
985 	{ .compatible = "nvidia,tegra-audio-max98088", .data = &tegra_max98088_data },
986 	{ .compatible = "nvidia,tegra-audio-max98089", .data = &tegra_max98088_data },
987 	{ .compatible = "nvidia,tegra-audio-sgtl5000", .data = &tegra_sgtl5000_data },
988 	{ .compatible = "nvidia,tegra-audio-wm9712", .data = &tegra_wm9712_data },
989 	{ .compatible = "nvidia,tegra-audio-wm8753", .data = &tegra_wm8753_data },
990 	{ .compatible = "nvidia,tegra-audio-rt5677", .data = &tegra_rt5677_data },
991 	{ .compatible = "nvidia,tegra-audio-rt5640", .data = &tegra_rt5640_data },
992 	{ .compatible = "nvidia,tegra-audio-alc5632", .data = &tegra_rt5632_data },
993 	{ .compatible = "nvidia,tegra-audio-rt5631", .data = &tegra_rt5631_data },
994 	{},
995 };
996 MODULE_DEVICE_TABLE(of, tegra_machine_of_match);
997 
998 static struct platform_driver tegra_asoc_machine_driver = {
999 	.driver = {
1000 		.name = "tegra-audio",
1001 		.of_match_table = tegra_machine_of_match,
1002 		.pm = &snd_soc_pm_ops,
1003 	},
1004 	.probe = tegra_asoc_machine_probe,
1005 };
1006 module_platform_driver(tegra_asoc_machine_driver);
1007 
1008 MODULE_AUTHOR("Anatol Pomozov <anatol@google.com>");
1009 MODULE_AUTHOR("Andrey Danin <danindrey@mail.ru>");
1010 MODULE_AUTHOR("Dmitry Osipenko <digetx@gmail.com>");
1011 MODULE_AUTHOR("Ion Agorria <ion@agorria.com>");
1012 MODULE_AUTHOR("Leon Romanovsky <leon@leon.nu>");
1013 MODULE_AUTHOR("Lucas Stach <dev@lynxeye.de>");
1014 MODULE_AUTHOR("Marc Dietrich <marvin24@gmx.de>");
1015 MODULE_AUTHOR("Marcel Ziswiler <marcel@ziswiler.com>");
1016 MODULE_AUTHOR("Mike Rapoport <mike@compulab.co.il>");
1017 MODULE_AUTHOR("Stephen Warren <swarren@nvidia.com>");
1018 MODULE_AUTHOR("Svyatoslav Ryhel <clamor95@gmail.com>");
1019 MODULE_DESCRIPTION("Tegra machine ASoC driver");
1020 MODULE_LICENSE("GPL");
1021