ASoC: OMAP: mcbsp, mcpdm, dmic: Let omap-pcm to pick the dma_type
[firefly-linux-kernel-4.4.55.git] / sound / soc / omap / omap-dmic.c
1 /*
2  * omap-dmic.c  --  OMAP ASoC DMIC DAI driver
3  *
4  * Copyright (C) 2010 - 2011 Texas Instruments
5  *
6  * Author: David Lambert <dlambert@ti.com>
7  *         Misael Lopez Cruz <misael.lopez@ti.com>
8  *         Liam Girdwood <lrg@ti.com>
9  *         Peter Ujfalusi <peter.ujfalusi@ti.com>
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * version 2 as published by the Free Software Foundation.
14  *
15  * This program is distributed in the hope that it will be useful, but
16  * WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
23  * 02110-1301 USA
24  *
25  */
26
27 #include <linux/init.h>
28 #include <linux/module.h>
29 #include <linux/platform_device.h>
30 #include <linux/err.h>
31 #include <linux/clk.h>
32 #include <linux/io.h>
33 #include <linux/slab.h>
34 #include <linux/pm_runtime.h>
35 #include <linux/of_device.h>
36
37 #include <sound/core.h>
38 #include <sound/pcm.h>
39 #include <sound/pcm_params.h>
40 #include <sound/initval.h>
41 #include <sound/soc.h>
42
43 #include "omap-pcm.h"
44 #include "omap-dmic.h"
45
46 struct omap_dmic {
47         struct device *dev;
48         void __iomem *io_base;
49         struct clk *fclk;
50         int fclk_freq;
51         int out_freq;
52         int clk_div;
53         int sysclk;
54         int threshold;
55         u32 ch_enabled;
56         bool active;
57         struct mutex mutex;
58 };
59
60 /*
61  * Stream DMA parameters
62  */
63 static struct omap_pcm_dma_data omap_dmic_dai_dma_params = {
64         .name           = "DMIC capture",
65 };
66
67 static inline void omap_dmic_write(struct omap_dmic *dmic, u16 reg, u32 val)
68 {
69         __raw_writel(val, dmic->io_base + reg);
70 }
71
72 static inline int omap_dmic_read(struct omap_dmic *dmic, u16 reg)
73 {
74         return __raw_readl(dmic->io_base + reg);
75 }
76
77 static inline void omap_dmic_start(struct omap_dmic *dmic)
78 {
79         u32 ctrl = omap_dmic_read(dmic, OMAP_DMIC_CTRL_REG);
80
81         /* Configure DMA controller */
82         omap_dmic_write(dmic, OMAP_DMIC_DMAENABLE_SET_REG,
83                         OMAP_DMIC_DMA_ENABLE);
84
85         omap_dmic_write(dmic, OMAP_DMIC_CTRL_REG, ctrl | dmic->ch_enabled);
86 }
87
88 static inline void omap_dmic_stop(struct omap_dmic *dmic)
89 {
90         u32 ctrl = omap_dmic_read(dmic, OMAP_DMIC_CTRL_REG);
91         omap_dmic_write(dmic, OMAP_DMIC_CTRL_REG,
92                         ctrl & ~OMAP_DMIC_UP_ENABLE_MASK);
93
94         /* Disable DMA request generation */
95         omap_dmic_write(dmic, OMAP_DMIC_DMAENABLE_CLR_REG,
96                         OMAP_DMIC_DMA_ENABLE);
97
98 }
99
100 static inline int dmic_is_enabled(struct omap_dmic *dmic)
101 {
102         return omap_dmic_read(dmic, OMAP_DMIC_CTRL_REG) &
103                                                 OMAP_DMIC_UP_ENABLE_MASK;
104 }
105
106 static int omap_dmic_dai_startup(struct snd_pcm_substream *substream,
107                                   struct snd_soc_dai *dai)
108 {
109         struct omap_dmic *dmic = snd_soc_dai_get_drvdata(dai);
110         int ret = 0;
111
112         mutex_lock(&dmic->mutex);
113
114         if (!dai->active)
115                 dmic->active = 1;
116         else
117                 ret = -EBUSY;
118
119         mutex_unlock(&dmic->mutex);
120
121         return ret;
122 }
123
124 static void omap_dmic_dai_shutdown(struct snd_pcm_substream *substream,
125                                     struct snd_soc_dai *dai)
126 {
127         struct omap_dmic *dmic = snd_soc_dai_get_drvdata(dai);
128
129         mutex_lock(&dmic->mutex);
130
131         if (!dai->active)
132                 dmic->active = 0;
133
134         mutex_unlock(&dmic->mutex);
135 }
136
137 static int omap_dmic_select_divider(struct omap_dmic *dmic, int sample_rate)
138 {
139         int divider = -EINVAL;
140
141         /*
142          * 192KHz rate is only supported with 19.2MHz/3.84MHz clock
143          * configuration.
144          */
145         if (sample_rate == 192000) {
146                 if (dmic->fclk_freq == 19200000 && dmic->out_freq == 3840000)
147                         divider = 0x6; /* Divider: 5 (192KHz sampling rate) */
148                 else
149                         dev_err(dmic->dev,
150                                 "invalid clock configuration for 192KHz\n");
151
152                 return divider;
153         }
154
155         switch (dmic->out_freq) {
156         case 1536000:
157                 if (dmic->fclk_freq != 24576000)
158                         goto div_err;
159                 divider = 0x4; /* Divider: 16 */
160                 break;
161         case 2400000:
162                 switch (dmic->fclk_freq) {
163                 case 12000000:
164                         divider = 0x5; /* Divider: 5 */
165                         break;
166                 case 19200000:
167                         divider = 0x0; /* Divider: 8 */
168                         break;
169                 case 24000000:
170                         divider = 0x2; /* Divider: 10 */
171                         break;
172                 default:
173                         goto div_err;
174                 }
175                 break;
176         case 3072000:
177                 if (dmic->fclk_freq != 24576000)
178                         goto div_err;
179                 divider = 0x3; /* Divider: 8 */
180                 break;
181         case 3840000:
182                 if (dmic->fclk_freq != 19200000)
183                         goto div_err;
184                 divider = 0x1; /* Divider: 5 (96KHz sampling rate) */
185                 break;
186         default:
187                 dev_err(dmic->dev, "invalid out frequency: %dHz\n",
188                         dmic->out_freq);
189                 break;
190         }
191
192         return divider;
193
194 div_err:
195         dev_err(dmic->dev, "invalid out frequency %dHz for %dHz input\n",
196                 dmic->out_freq, dmic->fclk_freq);
197         return -EINVAL;
198 }
199
200 static int omap_dmic_dai_hw_params(struct snd_pcm_substream *substream,
201                                     struct snd_pcm_hw_params *params,
202                                     struct snd_soc_dai *dai)
203 {
204         struct omap_dmic *dmic = snd_soc_dai_get_drvdata(dai);
205         int channels;
206
207         dmic->clk_div = omap_dmic_select_divider(dmic, params_rate(params));
208         if (dmic->clk_div < 0) {
209                 dev_err(dmic->dev, "no valid divider for %dHz from %dHz\n",
210                         dmic->out_freq, dmic->fclk_freq);
211                 return -EINVAL;
212         }
213
214         dmic->ch_enabled = 0;
215         channels = params_channels(params);
216         switch (channels) {
217         case 6:
218                 dmic->ch_enabled |= OMAP_DMIC_UP3_ENABLE;
219         case 4:
220                 dmic->ch_enabled |= OMAP_DMIC_UP2_ENABLE;
221         case 2:
222                 dmic->ch_enabled |= OMAP_DMIC_UP1_ENABLE;
223                 break;
224         default:
225                 dev_err(dmic->dev, "invalid number of legacy channels\n");
226                 return -EINVAL;
227         }
228
229         /* packet size is threshold * channels */
230         omap_dmic_dai_dma_params.packet_size = dmic->threshold * channels;
231         snd_soc_dai_set_dma_data(dai, substream, &omap_dmic_dai_dma_params);
232
233         return 0;
234 }
235
236 static int omap_dmic_dai_prepare(struct snd_pcm_substream *substream,
237                                   struct snd_soc_dai *dai)
238 {
239         struct omap_dmic *dmic = snd_soc_dai_get_drvdata(dai);
240         u32 ctrl;
241
242         /* Configure uplink threshold */
243         omap_dmic_write(dmic, OMAP_DMIC_FIFO_CTRL_REG, dmic->threshold);
244
245         ctrl = omap_dmic_read(dmic, OMAP_DMIC_CTRL_REG);
246
247         /* Set dmic out format */
248         ctrl &= ~(OMAP_DMIC_FORMAT | OMAP_DMIC_POLAR_MASK);
249         ctrl |= (OMAP_DMICOUTFORMAT_LJUST | OMAP_DMIC_POLAR1 |
250                  OMAP_DMIC_POLAR2 | OMAP_DMIC_POLAR3);
251
252         /* Configure dmic clock divider */
253         ctrl &= ~OMAP_DMIC_CLK_DIV_MASK;
254         ctrl |= OMAP_DMIC_CLK_DIV(dmic->clk_div);
255
256         omap_dmic_write(dmic, OMAP_DMIC_CTRL_REG, ctrl);
257
258         omap_dmic_write(dmic, OMAP_DMIC_CTRL_REG,
259                         ctrl | OMAP_DMICOUTFORMAT_LJUST | OMAP_DMIC_POLAR1 |
260                         OMAP_DMIC_POLAR2 | OMAP_DMIC_POLAR3);
261
262         return 0;
263 }
264
265 static int omap_dmic_dai_trigger(struct snd_pcm_substream *substream,
266                                   int cmd, struct snd_soc_dai *dai)
267 {
268         struct omap_dmic *dmic = snd_soc_dai_get_drvdata(dai);
269
270         switch (cmd) {
271         case SNDRV_PCM_TRIGGER_START:
272                 omap_dmic_start(dmic);
273                 break;
274         case SNDRV_PCM_TRIGGER_STOP:
275                 omap_dmic_stop(dmic);
276                 break;
277         default:
278                 break;
279         }
280
281         return 0;
282 }
283
284 static int omap_dmic_select_fclk(struct omap_dmic *dmic, int clk_id,
285                                  unsigned int freq)
286 {
287         struct clk *parent_clk;
288         char *parent_clk_name;
289         int ret = 0;
290
291         switch (freq) {
292         case 12000000:
293         case 19200000:
294         case 24000000:
295         case 24576000:
296                 break;
297         default:
298                 dev_err(dmic->dev, "invalid input frequency: %dHz\n", freq);
299                 dmic->fclk_freq = 0;
300                 return -EINVAL;
301         }
302
303         if (dmic->sysclk == clk_id) {
304                 dmic->fclk_freq = freq;
305                 return 0;
306         }
307
308         /* re-parent not allowed if a stream is ongoing */
309         if (dmic->active && dmic_is_enabled(dmic)) {
310                 dev_err(dmic->dev, "can't re-parent when DMIC active\n");
311                 return -EBUSY;
312         }
313
314         switch (clk_id) {
315         case OMAP_DMIC_SYSCLK_PAD_CLKS:
316                 parent_clk_name = "pad_clks_ck";
317                 break;
318         case OMAP_DMIC_SYSCLK_SLIMBLUS_CLKS:
319                 parent_clk_name = "slimbus_clk";
320                 break;
321         case OMAP_DMIC_SYSCLK_SYNC_MUX_CLKS:
322                 parent_clk_name = "dmic_sync_mux_ck";
323                 break;
324         default:
325                 dev_err(dmic->dev, "fclk clk_id (%d) not supported\n", clk_id);
326                 return -EINVAL;
327         }
328
329         parent_clk = clk_get(dmic->dev, parent_clk_name);
330         if (IS_ERR(parent_clk)) {
331                 dev_err(dmic->dev, "can't get %s\n", parent_clk_name);
332                 return -ENODEV;
333         }
334
335         mutex_lock(&dmic->mutex);
336         if (dmic->active) {
337                 /* disable clock while reparenting */
338                 pm_runtime_put_sync(dmic->dev);
339                 ret = clk_set_parent(dmic->fclk, parent_clk);
340                 pm_runtime_get_sync(dmic->dev);
341         } else {
342                 ret = clk_set_parent(dmic->fclk, parent_clk);
343         }
344         mutex_unlock(&dmic->mutex);
345
346         if (ret < 0) {
347                 dev_err(dmic->dev, "re-parent failed\n");
348                 goto err_busy;
349         }
350
351         dmic->sysclk = clk_id;
352         dmic->fclk_freq = freq;
353
354 err_busy:
355         clk_put(parent_clk);
356
357         return ret;
358 }
359
360 static int omap_dmic_select_outclk(struct omap_dmic *dmic, int clk_id,
361                                     unsigned int freq)
362 {
363         int ret = 0;
364
365         if (clk_id != OMAP_DMIC_ABE_DMIC_CLK) {
366                 dev_err(dmic->dev, "output clk_id (%d) not supported\n",
367                         clk_id);
368                 return -EINVAL;
369         }
370
371         switch (freq) {
372         case 1536000:
373         case 2400000:
374         case 3072000:
375         case 3840000:
376                 dmic->out_freq = freq;
377                 break;
378         default:
379                 dev_err(dmic->dev, "invalid out frequency: %dHz\n", freq);
380                 dmic->out_freq = 0;
381                 ret = -EINVAL;
382         }
383
384         return ret;
385 }
386
387 static int omap_dmic_set_dai_sysclk(struct snd_soc_dai *dai, int clk_id,
388                                     unsigned int freq, int dir)
389 {
390         struct omap_dmic *dmic = snd_soc_dai_get_drvdata(dai);
391
392         if (dir == SND_SOC_CLOCK_IN)
393                 return omap_dmic_select_fclk(dmic, clk_id, freq);
394         else if (dir == SND_SOC_CLOCK_OUT)
395                 return omap_dmic_select_outclk(dmic, clk_id, freq);
396
397         dev_err(dmic->dev, "invalid clock direction (%d)\n", dir);
398         return -EINVAL;
399 }
400
401 static const struct snd_soc_dai_ops omap_dmic_dai_ops = {
402         .startup        = omap_dmic_dai_startup,
403         .shutdown       = omap_dmic_dai_shutdown,
404         .hw_params      = omap_dmic_dai_hw_params,
405         .prepare        = omap_dmic_dai_prepare,
406         .trigger        = omap_dmic_dai_trigger,
407         .set_sysclk     = omap_dmic_set_dai_sysclk,
408 };
409
410 static int omap_dmic_probe(struct snd_soc_dai *dai)
411 {
412         struct omap_dmic *dmic = snd_soc_dai_get_drvdata(dai);
413
414         pm_runtime_enable(dmic->dev);
415
416         /* Disable lines while request is ongoing */
417         pm_runtime_get_sync(dmic->dev);
418         omap_dmic_write(dmic, OMAP_DMIC_CTRL_REG, 0x00);
419         pm_runtime_put_sync(dmic->dev);
420
421         /* Configure DMIC threshold value */
422         dmic->threshold = OMAP_DMIC_THRES_MAX - 3;
423         return 0;
424 }
425
426 static int omap_dmic_remove(struct snd_soc_dai *dai)
427 {
428         struct omap_dmic *dmic = snd_soc_dai_get_drvdata(dai);
429
430         pm_runtime_disable(dmic->dev);
431
432         return 0;
433 }
434
435 static struct snd_soc_dai_driver omap_dmic_dai = {
436         .name = "omap-dmic",
437         .probe = omap_dmic_probe,
438         .remove = omap_dmic_remove,
439         .capture = {
440                 .channels_min = 2,
441                 .channels_max = 6,
442                 .rates = SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_192000,
443                 .formats = SNDRV_PCM_FMTBIT_S32_LE,
444                 .sig_bits = 24,
445         },
446         .ops = &omap_dmic_dai_ops,
447 };
448
449 static __devinit int asoc_dmic_probe(struct platform_device *pdev)
450 {
451         struct omap_dmic *dmic;
452         struct resource *res;
453         int ret;
454
455         dmic = devm_kzalloc(&pdev->dev, sizeof(struct omap_dmic), GFP_KERNEL);
456         if (!dmic)
457                 return -ENOMEM;
458
459         platform_set_drvdata(pdev, dmic);
460         dmic->dev = &pdev->dev;
461         dmic->sysclk = OMAP_DMIC_SYSCLK_SYNC_MUX_CLKS;
462
463         mutex_init(&dmic->mutex);
464
465         dmic->fclk = clk_get(dmic->dev, "dmic_fck");
466         if (IS_ERR(dmic->fclk)) {
467                 dev_err(dmic->dev, "cant get dmic_fck\n");
468                 return -ENODEV;
469         }
470
471         res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dma");
472         if (!res) {
473                 dev_err(dmic->dev, "invalid dma memory resource\n");
474                 ret = -ENODEV;
475                 goto err_put_clk;
476         }
477         omap_dmic_dai_dma_params.port_addr = res->start + OMAP_DMIC_DATA_REG;
478
479         res = platform_get_resource(pdev, IORESOURCE_DMA, 0);
480         if (!res) {
481                 dev_err(dmic->dev, "invalid dma resource\n");
482                 ret = -ENODEV;
483                 goto err_put_clk;
484         }
485         omap_dmic_dai_dma_params.dma_req = res->start;
486
487         res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "mpu");
488         if (!res) {
489                 dev_err(dmic->dev, "invalid memory resource\n");
490                 ret = -ENODEV;
491                 goto err_put_clk;
492         }
493
494         if (!devm_request_mem_region(&pdev->dev, res->start,
495                                      resource_size(res), pdev->name)) {
496                 dev_err(dmic->dev, "memory region already claimed\n");
497                 ret = -ENODEV;
498                 goto err_put_clk;
499         }
500
501         dmic->io_base = devm_ioremap(&pdev->dev, res->start,
502                                      resource_size(res));
503         if (!dmic->io_base) {
504                 ret = -ENOMEM;
505                 goto err_put_clk;
506         }
507
508         ret = snd_soc_register_dai(&pdev->dev, &omap_dmic_dai);
509         if (ret)
510                 goto err_put_clk;
511
512         return 0;
513
514 err_put_clk:
515         clk_put(dmic->fclk);
516         return ret;
517 }
518
519 static int __devexit asoc_dmic_remove(struct platform_device *pdev)
520 {
521         struct omap_dmic *dmic = platform_get_drvdata(pdev);
522
523         snd_soc_unregister_dai(&pdev->dev);
524         clk_put(dmic->fclk);
525
526         return 0;
527 }
528
529 static const struct of_device_id omap_dmic_of_match[] = {
530         { .compatible = "ti,omap4-dmic", },
531         { }
532 };
533 MODULE_DEVICE_TABLE(of, omap_dmic_of_match);
534
535 static struct platform_driver asoc_dmic_driver = {
536         .driver = {
537                 .name = "omap-dmic",
538                 .owner = THIS_MODULE,
539                 .of_match_table = omap_dmic_of_match,
540         },
541         .probe = asoc_dmic_probe,
542         .remove = __devexit_p(asoc_dmic_remove),
543 };
544
545 module_platform_driver(asoc_dmic_driver);
546
547 MODULE_ALIAS("platform:omap-dmic");
548 MODULE_AUTHOR("Peter Ujfalusi <peter.ujfalusi@ti.com>");
549 MODULE_DESCRIPTION("OMAP DMIC ASoC Interface");
550 MODULE_LICENSE("GPL");