xref: /openbmc/linux/sound/soc/ti/davinci-evm.c (revision 0bf49ffb)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * ASoC driver for TI DAVINCI EVM platform
4  *
5  * Author:      Vladimir Barinov, <vbarinov@embeddedalley.com>
6  * Copyright:   (C) 2007 MontaVista Software, Inc., <source@mvista.com>
7  */
8 
9 #include <linux/module.h>
10 #include <linux/moduleparam.h>
11 #include <linux/timer.h>
12 #include <linux/interrupt.h>
13 #include <linux/platform_device.h>
14 #include <linux/i2c.h>
15 #include <linux/of_platform.h>
16 #include <linux/clk.h>
17 #include <sound/core.h>
18 #include <sound/pcm.h>
19 #include <sound/soc.h>
20 
21 #include <asm/dma.h>
22 #include <asm/mach-types.h>
23 
24 struct snd_soc_card_drvdata_davinci {
25 	struct clk *mclk;
26 	unsigned sysclk;
27 };
28 
29 static int evm_startup(struct snd_pcm_substream *substream)
30 {
31 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
32 	struct snd_soc_card *soc_card = rtd->card;
33 	struct snd_soc_card_drvdata_davinci *drvdata =
34 		snd_soc_card_get_drvdata(soc_card);
35 
36 	if (drvdata->mclk)
37 		return clk_prepare_enable(drvdata->mclk);
38 
39 	return 0;
40 }
41 
42 static void evm_shutdown(struct snd_pcm_substream *substream)
43 {
44 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
45 	struct snd_soc_card *soc_card = rtd->card;
46 	struct snd_soc_card_drvdata_davinci *drvdata =
47 		snd_soc_card_get_drvdata(soc_card);
48 
49 	clk_disable_unprepare(drvdata->mclk);
50 }
51 
52 static int evm_hw_params(struct snd_pcm_substream *substream,
53 			 struct snd_pcm_hw_params *params)
54 {
55 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
56 	struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
57 	struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
58 	struct snd_soc_card *soc_card = rtd->card;
59 	int ret = 0;
60 	unsigned sysclk = ((struct snd_soc_card_drvdata_davinci *)
61 			   snd_soc_card_get_drvdata(soc_card))->sysclk;
62 
63 	/* set the codec system clock */
64 	ret = snd_soc_dai_set_sysclk(codec_dai, 0, sysclk, SND_SOC_CLOCK_OUT);
65 	if (ret < 0)
66 		return ret;
67 
68 	/* set the CPU system clock */
69 	ret = snd_soc_dai_set_sysclk(cpu_dai, 0, sysclk, SND_SOC_CLOCK_OUT);
70 	if (ret < 0 && ret != -ENOTSUPP)
71 		return ret;
72 
73 	return 0;
74 }
75 
76 static struct snd_soc_ops evm_ops = {
77 	.startup = evm_startup,
78 	.shutdown = evm_shutdown,
79 	.hw_params = evm_hw_params,
80 };
81 
82 /* davinci-evm machine dapm widgets */
83 static const struct snd_soc_dapm_widget aic3x_dapm_widgets[] = {
84 	SND_SOC_DAPM_HP("Headphone Jack", NULL),
85 	SND_SOC_DAPM_LINE("Line Out", NULL),
86 	SND_SOC_DAPM_MIC("Mic Jack", NULL),
87 	SND_SOC_DAPM_LINE("Line In", NULL),
88 };
89 
90 /* davinci-evm machine audio_mapnections to the codec pins */
91 static const struct snd_soc_dapm_route audio_map[] = {
92 	/* Headphone connected to HPLOUT, HPROUT */
93 	{"Headphone Jack", NULL, "HPLOUT"},
94 	{"Headphone Jack", NULL, "HPROUT"},
95 
96 	/* Line Out connected to LLOUT, RLOUT */
97 	{"Line Out", NULL, "LLOUT"},
98 	{"Line Out", NULL, "RLOUT"},
99 
100 	/* Mic connected to (MIC3L | MIC3R) */
101 	{"MIC3L", NULL, "Mic Bias"},
102 	{"MIC3R", NULL, "Mic Bias"},
103 	{"Mic Bias", NULL, "Mic Jack"},
104 
105 	/* Line In connected to (LINE1L | LINE2L), (LINE1R | LINE2R) */
106 	{"LINE1L", NULL, "Line In"},
107 	{"LINE2L", NULL, "Line In"},
108 	{"LINE1R", NULL, "Line In"},
109 	{"LINE2R", NULL, "Line In"},
110 };
111 
112 /* Logic for a aic3x as connected on a davinci-evm */
113 static int evm_aic3x_init(struct snd_soc_pcm_runtime *rtd)
114 {
115 	struct snd_soc_card *card = rtd->card;
116 	struct device_node *np = card->dev->of_node;
117 	int ret;
118 
119 	/* Add davinci-evm specific widgets */
120 	snd_soc_dapm_new_controls(&card->dapm, aic3x_dapm_widgets,
121 				  ARRAY_SIZE(aic3x_dapm_widgets));
122 
123 	if (np) {
124 		ret = snd_soc_of_parse_audio_routing(card, "ti,audio-routing");
125 		if (ret)
126 			return ret;
127 	} else {
128 		/* Set up davinci-evm specific audio path audio_map */
129 		snd_soc_dapm_add_routes(&card->dapm, audio_map,
130 					ARRAY_SIZE(audio_map));
131 	}
132 
133 	/* not connected */
134 	snd_soc_dapm_nc_pin(&card->dapm, "MONO_LOUT");
135 	snd_soc_dapm_nc_pin(&card->dapm, "HPLCOM");
136 	snd_soc_dapm_nc_pin(&card->dapm, "HPRCOM");
137 
138 	return 0;
139 }
140 
141 /* davinci-evm digital audio interface glue - connects codec <--> CPU */
142 SND_SOC_DAILINK_DEFS(dm6446,
143 	DAILINK_COMP_ARRAY(COMP_CPU("davinci-mcbsp")),
144 	DAILINK_COMP_ARRAY(COMP_CODEC("tlv320aic3x-codec.1-001b",
145 				      "tlv320aic3x-hifi")),
146 	DAILINK_COMP_ARRAY(COMP_PLATFORM("davinci-mcbsp")));
147 
148 static struct snd_soc_dai_link dm6446_evm_dai = {
149 	.name = "TLV320AIC3X",
150 	.stream_name = "AIC3X",
151 	.init = evm_aic3x_init,
152 	.ops = &evm_ops,
153 	.dai_fmt = SND_SOC_DAIFMT_DSP_B | SND_SOC_DAIFMT_CBM_CFM |
154 		   SND_SOC_DAIFMT_IB_NF,
155 	SND_SOC_DAILINK_REG(dm6446),
156 };
157 
158 SND_SOC_DAILINK_DEFS(dm355,
159 	DAILINK_COMP_ARRAY(COMP_CPU("davinci-mcbsp.1")),
160 	DAILINK_COMP_ARRAY(COMP_CODEC("tlv320aic3x-codec.1-001b",
161 				      "tlv320aic3x-hifi")),
162 	DAILINK_COMP_ARRAY(COMP_PLATFORM("davinci-mcbsp.1")));
163 
164 static struct snd_soc_dai_link dm355_evm_dai = {
165 	.name = "TLV320AIC3X",
166 	.stream_name = "AIC3X",
167 	.init = evm_aic3x_init,
168 	.ops = &evm_ops,
169 	.dai_fmt = SND_SOC_DAIFMT_DSP_B | SND_SOC_DAIFMT_CBM_CFM |
170 		   SND_SOC_DAIFMT_IB_NF,
171 	SND_SOC_DAILINK_REG(dm355),
172 };
173 
174 #ifdef CONFIG_SND_SOC_DM365_AIC3X_CODEC
175 SND_SOC_DAILINK_DEFS(dm365,
176 	DAILINK_COMP_ARRAY(COMP_CPU("davinci-mcbsp")),
177 	DAILINK_COMP_ARRAY(COMP_CODEC("tlv320aic3x-codec.1-0018",
178 				      "tlv320aic3x-hifi")),
179 	DAILINK_COMP_ARRAY(COMP_PLATFORM("davinci-mcbsp")));
180 #elif defined(CONFIG_SND_SOC_DM365_VOICE_CODEC)
181 SND_SOC_DAILINK_DEFS(dm365,
182 	DAILINK_COMP_ARRAY(COMP_CPU("davinci-vcif")),
183 	DAILINK_COMP_ARRAY(COMP_CODEC("cq93vc-codec", "cq93vc-hifi")),
184 	DAILINK_COMP_ARRAY(COMP_PLATFORM("davinci-vcif")));
185 #endif
186 
187 static struct snd_soc_dai_link dm365_evm_dai = {
188 #ifdef CONFIG_SND_SOC_DM365_AIC3X_CODEC
189 	.name = "TLV320AIC3X",
190 	.stream_name = "AIC3X",
191 	.init = evm_aic3x_init,
192 	.ops = &evm_ops,
193 	.dai_fmt = SND_SOC_DAIFMT_DSP_B | SND_SOC_DAIFMT_CBM_CFM |
194 		   SND_SOC_DAIFMT_IB_NF,
195 	SND_SOC_DAILINK_REG(dm365),
196 #elif defined(CONFIG_SND_SOC_DM365_VOICE_CODEC)
197 	.name = "Voice Codec - CQ93VC",
198 	.stream_name = "CQ93",
199 	SND_SOC_DAILINK_REG(dm365),
200 #endif
201 };
202 
203 SND_SOC_DAILINK_DEFS(dm6467_aic3x,
204 	DAILINK_COMP_ARRAY(COMP_CPU("davinci-mcasp.0")),
205 	DAILINK_COMP_ARRAY(COMP_CODEC("tlv320aic3x-codec.0-001a",
206 				      "tlv320aic3x-hifi")),
207 	DAILINK_COMP_ARRAY(COMP_PLATFORM("davinci-mcasp.0")));
208 
209 SND_SOC_DAILINK_DEFS(dm6467_spdif,
210 	DAILINK_COMP_ARRAY(COMP_CPU("davinci-mcasp.1")),
211 	DAILINK_COMP_ARRAY(COMP_CODEC("spdif_dit", "dit-hifi")),
212 	DAILINK_COMP_ARRAY(COMP_PLATFORM("davinci-mcasp.1")));
213 
214 static struct snd_soc_dai_link dm6467_evm_dai[] = {
215 	{
216 		.name = "TLV320AIC3X",
217 		.stream_name = "AIC3X",
218 		.init = evm_aic3x_init,
219 		.ops = &evm_ops,
220 		.dai_fmt = SND_SOC_DAIFMT_DSP_B | SND_SOC_DAIFMT_CBM_CFM |
221 			   SND_SOC_DAIFMT_IB_NF,
222 		SND_SOC_DAILINK_REG(dm6467_aic3x),
223 	},
224 	{
225 		.name = "McASP",
226 		.stream_name = "spdif",
227 		.dai_fmt = SND_SOC_DAIFMT_DSP_B | SND_SOC_DAIFMT_CBM_CFM |
228 			   SND_SOC_DAIFMT_IB_NF,
229 		SND_SOC_DAILINK_REG(dm6467_spdif),
230 	},
231 };
232 
233 SND_SOC_DAILINK_DEFS(da830,
234 	DAILINK_COMP_ARRAY(COMP_CPU("davinci-mcasp.1")),
235 	DAILINK_COMP_ARRAY(COMP_CODEC("tlv320aic3x-codec.1-0018",
236 				      "tlv320aic3x-hifi")),
237 	DAILINK_COMP_ARRAY(COMP_PLATFORM("davinci-mcasp.1")));
238 
239 static struct snd_soc_dai_link da830_evm_dai = {
240 	.name = "TLV320AIC3X",
241 	.stream_name = "AIC3X",
242 	.init = evm_aic3x_init,
243 	.ops = &evm_ops,
244 	.dai_fmt = SND_SOC_DAIFMT_DSP_B | SND_SOC_DAIFMT_CBM_CFM |
245 		   SND_SOC_DAIFMT_IB_NF,
246 	SND_SOC_DAILINK_REG(da830),
247 };
248 
249 SND_SOC_DAILINK_DEFS(da850,
250 	DAILINK_COMP_ARRAY(COMP_CPU("davinci-mcasp.0")),
251 	DAILINK_COMP_ARRAY(COMP_CODEC("tlv320aic3x-codec.1-0018",
252 				      "tlv320aic3x-hifi")),
253 	DAILINK_COMP_ARRAY(COMP_PLATFORM("davinci-mcasp.0")));
254 
255 static struct snd_soc_dai_link da850_evm_dai = {
256 	.name = "TLV320AIC3X",
257 	.stream_name = "AIC3X",
258 	.init = evm_aic3x_init,
259 	.ops = &evm_ops,
260 	.dai_fmt = SND_SOC_DAIFMT_DSP_B | SND_SOC_DAIFMT_CBM_CFM |
261 		   SND_SOC_DAIFMT_IB_NF,
262 	SND_SOC_DAILINK_REG(da850),
263 };
264 
265 /* davinci dm6446 evm audio machine driver */
266 /*
267  * ASP0 in DM6446 EVM is clocked by U55, as configured by
268  * board-dm644x-evm.c using GPIOs from U18.  There are six
269  * options; here we "know" we use a 48 KHz sample rate.
270  */
271 static struct snd_soc_card_drvdata_davinci dm6446_snd_soc_card_drvdata = {
272 	.sysclk = 12288000,
273 };
274 
275 static struct snd_soc_card dm6446_snd_soc_card_evm = {
276 	.name = "DaVinci DM6446 EVM",
277 	.owner = THIS_MODULE,
278 	.dai_link = &dm6446_evm_dai,
279 	.num_links = 1,
280 	.drvdata = &dm6446_snd_soc_card_drvdata,
281 };
282 
283 /* davinci dm355 evm audio machine driver */
284 /* ASP1 on DM355 EVM is clocked by an external oscillator */
285 static struct snd_soc_card_drvdata_davinci dm355_snd_soc_card_drvdata = {
286 	.sysclk = 27000000,
287 };
288 
289 static struct snd_soc_card dm355_snd_soc_card_evm = {
290 	.name = "DaVinci DM355 EVM",
291 	.owner = THIS_MODULE,
292 	.dai_link = &dm355_evm_dai,
293 	.num_links = 1,
294 	.drvdata = &dm355_snd_soc_card_drvdata,
295 };
296 
297 /* davinci dm365 evm audio machine driver */
298 static struct snd_soc_card_drvdata_davinci dm365_snd_soc_card_drvdata = {
299 	.sysclk = 27000000,
300 };
301 
302 static struct snd_soc_card dm365_snd_soc_card_evm = {
303 	.name = "DaVinci DM365 EVM",
304 	.owner = THIS_MODULE,
305 	.dai_link = &dm365_evm_dai,
306 	.num_links = 1,
307 	.drvdata = &dm365_snd_soc_card_drvdata,
308 };
309 
310 /* davinci dm6467 evm audio machine driver */
311 static struct snd_soc_card_drvdata_davinci dm6467_snd_soc_card_drvdata = {
312 	.sysclk = 27000000,
313 };
314 
315 static struct snd_soc_card dm6467_snd_soc_card_evm = {
316 	.name = "DaVinci DM6467 EVM",
317 	.owner = THIS_MODULE,
318 	.dai_link = dm6467_evm_dai,
319 	.num_links = ARRAY_SIZE(dm6467_evm_dai),
320 	.drvdata = &dm6467_snd_soc_card_drvdata,
321 };
322 
323 static struct snd_soc_card_drvdata_davinci da830_snd_soc_card_drvdata = {
324 	.sysclk = 24576000,
325 };
326 
327 static struct snd_soc_card da830_snd_soc_card = {
328 	.name = "DA830/OMAP-L137 EVM",
329 	.owner = THIS_MODULE,
330 	.dai_link = &da830_evm_dai,
331 	.num_links = 1,
332 	.drvdata = &da830_snd_soc_card_drvdata,
333 };
334 
335 static struct snd_soc_card_drvdata_davinci da850_snd_soc_card_drvdata = {
336 	.sysclk = 24576000,
337 };
338 
339 static struct snd_soc_card da850_snd_soc_card = {
340 	.name = "DA850/OMAP-L138 EVM",
341 	.owner = THIS_MODULE,
342 	.dai_link = &da850_evm_dai,
343 	.num_links = 1,
344 	.drvdata = &da850_snd_soc_card_drvdata,
345 };
346 
347 #if defined(CONFIG_OF)
348 
349 /*
350  * The struct is used as place holder. It will be completely
351  * filled with data from dt node.
352  */
353 SND_SOC_DAILINK_DEFS(evm,
354 	DAILINK_COMP_ARRAY(COMP_EMPTY()),
355 	DAILINK_COMP_ARRAY(COMP_CODEC(NULL, "tlv320aic3x-hifi")),
356 	DAILINK_COMP_ARRAY(COMP_EMPTY()));
357 
358 static struct snd_soc_dai_link evm_dai_tlv320aic3x = {
359 	.name		= "TLV320AIC3X",
360 	.stream_name	= "AIC3X",
361 	.ops            = &evm_ops,
362 	.init           = evm_aic3x_init,
363 	.dai_fmt = SND_SOC_DAIFMT_DSP_B | SND_SOC_DAIFMT_CBM_CFM |
364 		   SND_SOC_DAIFMT_IB_NF,
365 	SND_SOC_DAILINK_REG(evm),
366 };
367 
368 static const struct of_device_id davinci_evm_dt_ids[] = {
369 	{
370 		.compatible = "ti,da830-evm-audio",
371 		.data = (void *) &evm_dai_tlv320aic3x,
372 	},
373 	{ /* sentinel */ }
374 };
375 MODULE_DEVICE_TABLE(of, davinci_evm_dt_ids);
376 
377 /* davinci evm audio machine driver */
378 static struct snd_soc_card evm_soc_card = {
379 	.owner = THIS_MODULE,
380 	.num_links = 1,
381 };
382 
383 static int davinci_evm_probe(struct platform_device *pdev)
384 {
385 	struct device_node *np = pdev->dev.of_node;
386 	const struct of_device_id *match;
387 	struct snd_soc_dai_link *dai;
388 	struct snd_soc_card_drvdata_davinci *drvdata = NULL;
389 	struct clk *mclk;
390 	int ret = 0;
391 
392 	match = of_match_device(of_match_ptr(davinci_evm_dt_ids), &pdev->dev);
393 	if (!match) {
394 		dev_err(&pdev->dev, "Error: No device match found\n");
395 		return -ENODEV;
396 	}
397 
398 	dai = (struct snd_soc_dai_link *) match->data;
399 
400 	evm_soc_card.dai_link = dai;
401 
402 	dai->codecs->of_node = of_parse_phandle(np, "ti,audio-codec", 0);
403 	if (!dai->codecs->of_node)
404 		return -EINVAL;
405 
406 	dai->cpus->of_node = of_parse_phandle(np, "ti,mcasp-controller", 0);
407 	if (!dai->cpus->of_node)
408 		return -EINVAL;
409 
410 	dai->platforms->of_node = dai->cpus->of_node;
411 
412 	evm_soc_card.dev = &pdev->dev;
413 	ret = snd_soc_of_parse_card_name(&evm_soc_card, "ti,model");
414 	if (ret)
415 		return ret;
416 
417 	mclk = devm_clk_get(&pdev->dev, "mclk");
418 	if (PTR_ERR(mclk) == -EPROBE_DEFER) {
419 		return -EPROBE_DEFER;
420 	} else if (IS_ERR(mclk)) {
421 		dev_dbg(&pdev->dev, "mclk not found.\n");
422 		mclk = NULL;
423 	}
424 
425 	drvdata = devm_kzalloc(&pdev->dev, sizeof(*drvdata), GFP_KERNEL);
426 	if (!drvdata)
427 		return -ENOMEM;
428 
429 	drvdata->mclk = mclk;
430 
431 	ret = of_property_read_u32(np, "ti,codec-clock-rate", &drvdata->sysclk);
432 
433 	if (ret < 0) {
434 		if (!drvdata->mclk) {
435 			dev_err(&pdev->dev,
436 				"No clock or clock rate defined.\n");
437 			return -EINVAL;
438 		}
439 		drvdata->sysclk = clk_get_rate(drvdata->mclk);
440 	} else if (drvdata->mclk) {
441 		unsigned int requestd_rate = drvdata->sysclk;
442 		clk_set_rate(drvdata->mclk, drvdata->sysclk);
443 		drvdata->sysclk = clk_get_rate(drvdata->mclk);
444 		if (drvdata->sysclk != requestd_rate)
445 			dev_warn(&pdev->dev,
446 				 "Could not get requested rate %u using %u.\n",
447 				 requestd_rate, drvdata->sysclk);
448 	}
449 
450 	snd_soc_card_set_drvdata(&evm_soc_card, drvdata);
451 	ret = devm_snd_soc_register_card(&pdev->dev, &evm_soc_card);
452 
453 	if (ret)
454 		dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n", ret);
455 
456 	return ret;
457 }
458 
459 static struct platform_driver davinci_evm_driver = {
460 	.probe		= davinci_evm_probe,
461 	.driver		= {
462 		.name	= "davinci_evm",
463 		.pm	= &snd_soc_pm_ops,
464 		.of_match_table = of_match_ptr(davinci_evm_dt_ids),
465 	},
466 };
467 #endif
468 
469 static struct platform_device *evm_snd_device;
470 
471 static int __init evm_init(void)
472 {
473 	struct snd_soc_card *evm_snd_dev_data;
474 	int index;
475 	int ret;
476 
477 	/*
478 	 * If dtb is there, the devices will be created dynamically.
479 	 * Only register platfrom driver structure.
480 	 */
481 #if defined(CONFIG_OF)
482 	if (of_have_populated_dt())
483 		return platform_driver_register(&davinci_evm_driver);
484 #endif
485 
486 	if (machine_is_davinci_evm()) {
487 		evm_snd_dev_data = &dm6446_snd_soc_card_evm;
488 		index = 0;
489 	} else if (machine_is_davinci_dm355_evm()) {
490 		evm_snd_dev_data = &dm355_snd_soc_card_evm;
491 		index = 1;
492 	} else if (machine_is_davinci_dm365_evm()) {
493 		evm_snd_dev_data = &dm365_snd_soc_card_evm;
494 		index = 0;
495 	} else if (machine_is_davinci_dm6467_evm()) {
496 		evm_snd_dev_data = &dm6467_snd_soc_card_evm;
497 		index = 0;
498 	} else if (machine_is_davinci_da830_evm()) {
499 		evm_snd_dev_data = &da830_snd_soc_card;
500 		index = 1;
501 	} else if (machine_is_davinci_da850_evm()) {
502 		evm_snd_dev_data = &da850_snd_soc_card;
503 		index = 0;
504 	} else
505 		return -EINVAL;
506 
507 	evm_snd_device = platform_device_alloc("soc-audio", index);
508 	if (!evm_snd_device)
509 		return -ENOMEM;
510 
511 	platform_set_drvdata(evm_snd_device, evm_snd_dev_data);
512 	ret = platform_device_add(evm_snd_device);
513 	if (ret)
514 		platform_device_put(evm_snd_device);
515 
516 	return ret;
517 }
518 
519 static void __exit evm_exit(void)
520 {
521 #if defined(CONFIG_OF)
522 	if (of_have_populated_dt()) {
523 		platform_driver_unregister(&davinci_evm_driver);
524 		return;
525 	}
526 #endif
527 
528 	platform_device_unregister(evm_snd_device);
529 }
530 
531 module_init(evm_init);
532 module_exit(evm_exit);
533 
534 MODULE_AUTHOR("Vladimir Barinov");
535 MODULE_DESCRIPTION("TI DAVINCI EVM ASoC driver");
536 MODULE_LICENSE("GPL");
537