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