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