GNU Linux-libre 4.14.290-gnu1
[releases.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 #include <sound/dmaengine_pcm.h>
43 #include <sound/omap-pcm.h>
44
45 #include "omap-dmic.h"
46
47 struct omap_dmic {
48         struct device *dev;
49         void __iomem *io_base;
50         struct clk *fclk;
51         struct pm_qos_request pm_qos_req;
52         int latency;
53         int fclk_freq;
54         int out_freq;
55         int clk_div;
56         int sysclk;
57         int threshold;
58         u32 ch_enabled;
59         bool active;
60         struct mutex mutex;
61
62         struct snd_dmaengine_dai_dma_data dma_data;
63 };
64
65 static inline void omap_dmic_write(struct omap_dmic *dmic, u16 reg, u32 val)
66 {
67         writel_relaxed(val, dmic->io_base + reg);
68 }
69
70 static inline int omap_dmic_read(struct omap_dmic *dmic, u16 reg)
71 {
72         return readl_relaxed(dmic->io_base + reg);
73 }
74
75 static inline void omap_dmic_start(struct omap_dmic *dmic)
76 {
77         u32 ctrl = omap_dmic_read(dmic, OMAP_DMIC_CTRL_REG);
78
79         /* Configure DMA controller */
80         omap_dmic_write(dmic, OMAP_DMIC_DMAENABLE_SET_REG,
81                         OMAP_DMIC_DMA_ENABLE);
82
83         omap_dmic_write(dmic, OMAP_DMIC_CTRL_REG, ctrl | dmic->ch_enabled);
84 }
85
86 static inline void omap_dmic_stop(struct omap_dmic *dmic)
87 {
88         u32 ctrl = omap_dmic_read(dmic, OMAP_DMIC_CTRL_REG);
89         omap_dmic_write(dmic, OMAP_DMIC_CTRL_REG,
90                         ctrl & ~OMAP_DMIC_UP_ENABLE_MASK);
91
92         /* Disable DMA request generation */
93         omap_dmic_write(dmic, OMAP_DMIC_DMAENABLE_CLR_REG,
94                         OMAP_DMIC_DMA_ENABLE);
95
96 }
97
98 static inline int dmic_is_enabled(struct omap_dmic *dmic)
99 {
100         return omap_dmic_read(dmic, OMAP_DMIC_CTRL_REG) &
101                                                 OMAP_DMIC_UP_ENABLE_MASK;
102 }
103
104 static int omap_dmic_dai_startup(struct snd_pcm_substream *substream,
105                                   struct snd_soc_dai *dai)
106 {
107         struct omap_dmic *dmic = snd_soc_dai_get_drvdata(dai);
108         int ret = 0;
109
110         mutex_lock(&dmic->mutex);
111
112         if (!dai->active)
113                 dmic->active = 1;
114         else
115                 ret = -EBUSY;
116
117         mutex_unlock(&dmic->mutex);
118
119         return ret;
120 }
121
122 static void omap_dmic_dai_shutdown(struct snd_pcm_substream *substream,
123                                     struct snd_soc_dai *dai)
124 {
125         struct omap_dmic *dmic = snd_soc_dai_get_drvdata(dai);
126
127         mutex_lock(&dmic->mutex);
128
129         pm_qos_remove_request(&dmic->pm_qos_req);
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         struct snd_dmaengine_dai_dma_data *dma_data;
206         int channels;
207
208         dmic->clk_div = omap_dmic_select_divider(dmic, params_rate(params));
209         if (dmic->clk_div < 0) {
210                 dev_err(dmic->dev, "no valid divider for %dHz from %dHz\n",
211                         dmic->out_freq, dmic->fclk_freq);
212                 return -EINVAL;
213         }
214
215         dmic->ch_enabled = 0;
216         channels = params_channels(params);
217         switch (channels) {
218         case 6:
219                 dmic->ch_enabled |= OMAP_DMIC_UP3_ENABLE;
220         case 4:
221                 dmic->ch_enabled |= OMAP_DMIC_UP2_ENABLE;
222         case 2:
223                 dmic->ch_enabled |= OMAP_DMIC_UP1_ENABLE;
224                 break;
225         default:
226                 dev_err(dmic->dev, "invalid number of legacy channels\n");
227                 return -EINVAL;
228         }
229
230         /* packet size is threshold * channels */
231         dma_data = snd_soc_dai_get_dma_data(dai, substream);
232         dma_data->maxburst = dmic->threshold * channels;
233         dmic->latency = (OMAP_DMIC_THRES_MAX - dmic->threshold) * USEC_PER_SEC /
234                         params_rate(params);
235
236         return 0;
237 }
238
239 static int omap_dmic_dai_prepare(struct snd_pcm_substream *substream,
240                                   struct snd_soc_dai *dai)
241 {
242         struct omap_dmic *dmic = snd_soc_dai_get_drvdata(dai);
243         u32 ctrl;
244
245         if (pm_qos_request_active(&dmic->pm_qos_req))
246                 pm_qos_update_request(&dmic->pm_qos_req, dmic->latency);
247
248         /* Configure uplink threshold */
249         omap_dmic_write(dmic, OMAP_DMIC_FIFO_CTRL_REG, dmic->threshold);
250
251         ctrl = omap_dmic_read(dmic, OMAP_DMIC_CTRL_REG);
252
253         /* Set dmic out format */
254         ctrl &= ~(OMAP_DMIC_FORMAT | OMAP_DMIC_POLAR_MASK);
255         ctrl |= (OMAP_DMICOUTFORMAT_LJUST | OMAP_DMIC_POLAR1 |
256                  OMAP_DMIC_POLAR2 | OMAP_DMIC_POLAR3);
257
258         /* Configure dmic clock divider */
259         ctrl &= ~OMAP_DMIC_CLK_DIV_MASK;
260         ctrl |= OMAP_DMIC_CLK_DIV(dmic->clk_div);
261
262         omap_dmic_write(dmic, OMAP_DMIC_CTRL_REG, ctrl);
263
264         omap_dmic_write(dmic, OMAP_DMIC_CTRL_REG,
265                         ctrl | OMAP_DMICOUTFORMAT_LJUST | OMAP_DMIC_POLAR1 |
266                         OMAP_DMIC_POLAR2 | OMAP_DMIC_POLAR3);
267
268         return 0;
269 }
270
271 static int omap_dmic_dai_trigger(struct snd_pcm_substream *substream,
272                                   int cmd, struct snd_soc_dai *dai)
273 {
274         struct omap_dmic *dmic = snd_soc_dai_get_drvdata(dai);
275
276         switch (cmd) {
277         case SNDRV_PCM_TRIGGER_START:
278                 omap_dmic_start(dmic);
279                 break;
280         case SNDRV_PCM_TRIGGER_STOP:
281                 omap_dmic_stop(dmic);
282                 break;
283         default:
284                 break;
285         }
286
287         return 0;
288 }
289
290 static int omap_dmic_select_fclk(struct omap_dmic *dmic, int clk_id,
291                                  unsigned int freq)
292 {
293         struct clk *parent_clk;
294         char *parent_clk_name;
295         int ret = 0;
296
297         switch (freq) {
298         case 12000000:
299         case 19200000:
300         case 24000000:
301         case 24576000:
302                 break;
303         default:
304                 dev_err(dmic->dev, "invalid input frequency: %dHz\n", freq);
305                 dmic->fclk_freq = 0;
306                 return -EINVAL;
307         }
308
309         if (dmic->sysclk == clk_id) {
310                 dmic->fclk_freq = freq;
311                 return 0;
312         }
313
314         /* re-parent not allowed if a stream is ongoing */
315         if (dmic->active && dmic_is_enabled(dmic)) {
316                 dev_err(dmic->dev, "can't re-parent when DMIC active\n");
317                 return -EBUSY;
318         }
319
320         switch (clk_id) {
321         case OMAP_DMIC_SYSCLK_PAD_CLKS:
322                 parent_clk_name = "pad_clks_ck";
323                 break;
324         case OMAP_DMIC_SYSCLK_SLIMBLUS_CLKS:
325                 parent_clk_name = "slimbus_clk";
326                 break;
327         case OMAP_DMIC_SYSCLK_SYNC_MUX_CLKS:
328                 parent_clk_name = "dmic_sync_mux_ck";
329                 break;
330         default:
331                 dev_err(dmic->dev, "fclk clk_id (%d) not supported\n", clk_id);
332                 return -EINVAL;
333         }
334
335         parent_clk = clk_get(dmic->dev, parent_clk_name);
336         if (IS_ERR(parent_clk)) {
337                 dev_err(dmic->dev, "can't get %s\n", parent_clk_name);
338                 return -ENODEV;
339         }
340
341         mutex_lock(&dmic->mutex);
342         if (dmic->active) {
343                 /* disable clock while reparenting */
344                 pm_runtime_put_sync(dmic->dev);
345                 ret = clk_set_parent(dmic->fclk, parent_clk);
346                 pm_runtime_get_sync(dmic->dev);
347         } else {
348                 ret = clk_set_parent(dmic->fclk, parent_clk);
349         }
350         mutex_unlock(&dmic->mutex);
351
352         if (ret < 0) {
353                 dev_err(dmic->dev, "re-parent failed\n");
354                 goto err_busy;
355         }
356
357         dmic->sysclk = clk_id;
358         dmic->fclk_freq = freq;
359
360 err_busy:
361         clk_put(parent_clk);
362
363         return ret;
364 }
365
366 static int omap_dmic_select_outclk(struct omap_dmic *dmic, int clk_id,
367                                     unsigned int freq)
368 {
369         int ret = 0;
370
371         if (clk_id != OMAP_DMIC_ABE_DMIC_CLK) {
372                 dev_err(dmic->dev, "output clk_id (%d) not supported\n",
373                         clk_id);
374                 return -EINVAL;
375         }
376
377         switch (freq) {
378         case 1536000:
379         case 2400000:
380         case 3072000:
381         case 3840000:
382                 dmic->out_freq = freq;
383                 break;
384         default:
385                 dev_err(dmic->dev, "invalid out frequency: %dHz\n", freq);
386                 dmic->out_freq = 0;
387                 ret = -EINVAL;
388         }
389
390         return ret;
391 }
392
393 static int omap_dmic_set_dai_sysclk(struct snd_soc_dai *dai, int clk_id,
394                                     unsigned int freq, int dir)
395 {
396         struct omap_dmic *dmic = snd_soc_dai_get_drvdata(dai);
397
398         if (dir == SND_SOC_CLOCK_IN)
399                 return omap_dmic_select_fclk(dmic, clk_id, freq);
400         else if (dir == SND_SOC_CLOCK_OUT)
401                 return omap_dmic_select_outclk(dmic, clk_id, freq);
402
403         dev_err(dmic->dev, "invalid clock direction (%d)\n", dir);
404         return -EINVAL;
405 }
406
407 static const struct snd_soc_dai_ops omap_dmic_dai_ops = {
408         .startup        = omap_dmic_dai_startup,
409         .shutdown       = omap_dmic_dai_shutdown,
410         .hw_params      = omap_dmic_dai_hw_params,
411         .prepare        = omap_dmic_dai_prepare,
412         .trigger        = omap_dmic_dai_trigger,
413         .set_sysclk     = omap_dmic_set_dai_sysclk,
414 };
415
416 static int omap_dmic_probe(struct snd_soc_dai *dai)
417 {
418         struct omap_dmic *dmic = snd_soc_dai_get_drvdata(dai);
419
420         pm_runtime_enable(dmic->dev);
421
422         /* Disable lines while request is ongoing */
423         pm_runtime_get_sync(dmic->dev);
424         omap_dmic_write(dmic, OMAP_DMIC_CTRL_REG, 0x00);
425         pm_runtime_put_sync(dmic->dev);
426
427         /* Configure DMIC threshold value */
428         dmic->threshold = OMAP_DMIC_THRES_MAX - 3;
429
430         snd_soc_dai_init_dma_data(dai, NULL, &dmic->dma_data);
431
432         return 0;
433 }
434
435 static int omap_dmic_remove(struct snd_soc_dai *dai)
436 {
437         struct omap_dmic *dmic = snd_soc_dai_get_drvdata(dai);
438
439         pm_runtime_disable(dmic->dev);
440
441         return 0;
442 }
443
444 static struct snd_soc_dai_driver omap_dmic_dai = {
445         .name = "omap-dmic",
446         .probe = omap_dmic_probe,
447         .remove = omap_dmic_remove,
448         .capture = {
449                 .channels_min = 2,
450                 .channels_max = 6,
451                 .rates = SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_192000,
452                 .formats = SNDRV_PCM_FMTBIT_S32_LE,
453                 .sig_bits = 24,
454         },
455         .ops = &omap_dmic_dai_ops,
456 };
457
458 static const struct snd_soc_component_driver omap_dmic_component = {
459         .name           = "omap-dmic",
460 };
461
462 static int asoc_dmic_probe(struct platform_device *pdev)
463 {
464         struct omap_dmic *dmic;
465         struct resource *res;
466         int ret;
467
468         dmic = devm_kzalloc(&pdev->dev, sizeof(struct omap_dmic), GFP_KERNEL);
469         if (!dmic)
470                 return -ENOMEM;
471
472         platform_set_drvdata(pdev, dmic);
473         dmic->dev = &pdev->dev;
474         dmic->sysclk = OMAP_DMIC_SYSCLK_SYNC_MUX_CLKS;
475
476         mutex_init(&dmic->mutex);
477
478         dmic->fclk = devm_clk_get(dmic->dev, "fck");
479         if (IS_ERR(dmic->fclk)) {
480                 dev_err(dmic->dev, "cant get fck\n");
481                 return -ENODEV;
482         }
483
484         res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dma");
485         if (!res) {
486                 dev_err(dmic->dev, "invalid dma memory resource\n");
487                 return -ENODEV;
488         }
489         dmic->dma_data.addr = res->start + OMAP_DMIC_DATA_REG;
490
491         dmic->dma_data.filter_data = "up_link";
492
493         res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "mpu");
494         dmic->io_base = devm_ioremap_resource(&pdev->dev, res);
495         if (IS_ERR(dmic->io_base))
496                 return PTR_ERR(dmic->io_base);
497
498
499         ret = devm_snd_soc_register_component(&pdev->dev,
500                                               &omap_dmic_component,
501                                               &omap_dmic_dai, 1);
502         if (ret)
503                 return ret;
504
505         ret = omap_pcm_platform_register(&pdev->dev);
506         if (ret)
507                 return ret;
508
509         return 0;
510 }
511
512 static const struct of_device_id omap_dmic_of_match[] = {
513         { .compatible = "ti,omap4-dmic", },
514         { }
515 };
516 MODULE_DEVICE_TABLE(of, omap_dmic_of_match);
517
518 static struct platform_driver asoc_dmic_driver = {
519         .driver = {
520                 .name = "omap-dmic",
521                 .of_match_table = omap_dmic_of_match,
522         },
523         .probe = asoc_dmic_probe,
524 };
525
526 module_platform_driver(asoc_dmic_driver);
527
528 MODULE_ALIAS("platform:omap-dmic");
529 MODULE_AUTHOR("Peter Ujfalusi <peter.ujfalusi@ti.com>");
530 MODULE_DESCRIPTION("OMAP DMIC ASoC Interface");
531 MODULE_LICENSE("GPL");