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