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