1 // SPDX-License-Identifier: GPL-2.0-only
3 * omap-dmic.c -- OMAP ASoC DMIC DAI driver
5 * Copyright (C) 2010 - 2011 Texas Instruments
7 * Author: David Lambert <dlambert@ti.com>
8 * Misael Lopez Cruz <misael.lopez@ti.com>
9 * Liam Girdwood <lrg@ti.com>
10 * Peter Ujfalusi <peter.ujfalusi@ti.com>
13 #include <linux/init.h>
14 #include <linux/mod_devicetable.h>
15 #include <linux/module.h>
16 #include <linux/platform_device.h>
17 #include <linux/err.h>
18 #include <linux/clk.h>
20 #include <linux/slab.h>
21 #include <linux/pm_runtime.h>
23 #include <sound/core.h>
24 #include <sound/pcm.h>
25 #include <sound/pcm_params.h>
26 #include <sound/initval.h>
27 #include <sound/soc.h>
28 #include <sound/dmaengine_pcm.h>
30 #include "omap-dmic.h"
35 void __iomem
*io_base
;
37 struct pm_qos_request pm_qos_req
;
48 struct snd_dmaengine_dai_dma_data dma_data
;
51 static inline void omap_dmic_write(struct omap_dmic
*dmic
, u16 reg
, u32 val
)
53 writel_relaxed(val
, dmic
->io_base
+ reg
);
56 static inline int omap_dmic_read(struct omap_dmic
*dmic
, u16 reg
)
58 return readl_relaxed(dmic
->io_base
+ reg
);
61 static inline void omap_dmic_start(struct omap_dmic
*dmic
)
63 u32 ctrl
= omap_dmic_read(dmic
, OMAP_DMIC_CTRL_REG
);
65 /* Configure DMA controller */
66 omap_dmic_write(dmic
, OMAP_DMIC_DMAENABLE_SET_REG
,
67 OMAP_DMIC_DMA_ENABLE
);
69 omap_dmic_write(dmic
, OMAP_DMIC_CTRL_REG
, ctrl
| dmic
->ch_enabled
);
72 static inline void omap_dmic_stop(struct omap_dmic
*dmic
)
74 u32 ctrl
= omap_dmic_read(dmic
, OMAP_DMIC_CTRL_REG
);
75 omap_dmic_write(dmic
, OMAP_DMIC_CTRL_REG
,
76 ctrl
& ~OMAP_DMIC_UP_ENABLE_MASK
);
78 /* Disable DMA request generation */
79 omap_dmic_write(dmic
, OMAP_DMIC_DMAENABLE_CLR_REG
,
80 OMAP_DMIC_DMA_ENABLE
);
84 static inline int dmic_is_enabled(struct omap_dmic
*dmic
)
86 return omap_dmic_read(dmic
, OMAP_DMIC_CTRL_REG
) &
87 OMAP_DMIC_UP_ENABLE_MASK
;
90 static int omap_dmic_dai_startup(struct snd_pcm_substream
*substream
,
91 struct snd_soc_dai
*dai
)
93 struct omap_dmic
*dmic
= snd_soc_dai_get_drvdata(dai
);
96 mutex_lock(&dmic
->mutex
);
98 if (!snd_soc_dai_active(dai
))
103 mutex_unlock(&dmic
->mutex
);
108 static void omap_dmic_dai_shutdown(struct snd_pcm_substream
*substream
,
109 struct snd_soc_dai
*dai
)
111 struct omap_dmic
*dmic
= snd_soc_dai_get_drvdata(dai
);
113 mutex_lock(&dmic
->mutex
);
115 cpu_latency_qos_remove_request(&dmic
->pm_qos_req
);
117 if (!snd_soc_dai_active(dai
))
120 mutex_unlock(&dmic
->mutex
);
123 static int omap_dmic_select_divider(struct omap_dmic
*dmic
, int sample_rate
)
125 int divider
= -EINVAL
;
128 * 192KHz rate is only supported with 19.2MHz/3.84MHz clock
131 if (sample_rate
== 192000) {
132 if (dmic
->fclk_freq
== 19200000 && dmic
->out_freq
== 3840000)
133 divider
= 0x6; /* Divider: 5 (192KHz sampling rate) */
136 "invalid clock configuration for 192KHz\n");
141 switch (dmic
->out_freq
) {
143 if (dmic
->fclk_freq
!= 24576000)
145 divider
= 0x4; /* Divider: 16 */
148 switch (dmic
->fclk_freq
) {
150 divider
= 0x5; /* Divider: 5 */
153 divider
= 0x0; /* Divider: 8 */
156 divider
= 0x2; /* Divider: 10 */
163 if (dmic
->fclk_freq
!= 24576000)
165 divider
= 0x3; /* Divider: 8 */
168 if (dmic
->fclk_freq
!= 19200000)
170 divider
= 0x1; /* Divider: 5 (96KHz sampling rate) */
173 dev_err(dmic
->dev
, "invalid out frequency: %dHz\n",
181 dev_err(dmic
->dev
, "invalid out frequency %dHz for %dHz input\n",
182 dmic
->out_freq
, dmic
->fclk_freq
);
186 static int omap_dmic_dai_hw_params(struct snd_pcm_substream
*substream
,
187 struct snd_pcm_hw_params
*params
,
188 struct snd_soc_dai
*dai
)
190 struct omap_dmic
*dmic
= snd_soc_dai_get_drvdata(dai
);
191 struct snd_dmaengine_dai_dma_data
*dma_data
;
194 dmic
->clk_div
= omap_dmic_select_divider(dmic
, params_rate(params
));
195 if (dmic
->clk_div
< 0) {
196 dev_err(dmic
->dev
, "no valid divider for %dHz from %dHz\n",
197 dmic
->out_freq
, dmic
->fclk_freq
);
201 dmic
->ch_enabled
= 0;
202 channels
= params_channels(params
);
205 dmic
->ch_enabled
|= OMAP_DMIC_UP3_ENABLE
;
208 dmic
->ch_enabled
|= OMAP_DMIC_UP2_ENABLE
;
211 dmic
->ch_enabled
|= OMAP_DMIC_UP1_ENABLE
;
214 dev_err(dmic
->dev
, "invalid number of legacy channels\n");
218 /* packet size is threshold * channels */
219 dma_data
= snd_soc_dai_get_dma_data(dai
, substream
);
220 dma_data
->maxburst
= dmic
->threshold
* channels
;
221 dmic
->latency
= (OMAP_DMIC_THRES_MAX
- dmic
->threshold
) * USEC_PER_SEC
/
227 static int omap_dmic_dai_prepare(struct snd_pcm_substream
*substream
,
228 struct snd_soc_dai
*dai
)
230 struct omap_dmic
*dmic
= snd_soc_dai_get_drvdata(dai
);
233 if (cpu_latency_qos_request_active(&dmic
->pm_qos_req
))
234 cpu_latency_qos_update_request(&dmic
->pm_qos_req
,
237 /* Configure uplink threshold */
238 omap_dmic_write(dmic
, OMAP_DMIC_FIFO_CTRL_REG
, dmic
->threshold
);
240 ctrl
= omap_dmic_read(dmic
, OMAP_DMIC_CTRL_REG
);
242 /* Set dmic out format */
243 ctrl
&= ~(OMAP_DMIC_FORMAT
| OMAP_DMIC_POLAR_MASK
);
244 ctrl
|= (OMAP_DMICOUTFORMAT_LJUST
| OMAP_DMIC_POLAR1
|
245 OMAP_DMIC_POLAR2
| OMAP_DMIC_POLAR3
);
247 /* Configure dmic clock divider */
248 ctrl
&= ~OMAP_DMIC_CLK_DIV_MASK
;
249 ctrl
|= OMAP_DMIC_CLK_DIV(dmic
->clk_div
);
251 omap_dmic_write(dmic
, OMAP_DMIC_CTRL_REG
, ctrl
);
253 omap_dmic_write(dmic
, OMAP_DMIC_CTRL_REG
,
254 ctrl
| OMAP_DMICOUTFORMAT_LJUST
| OMAP_DMIC_POLAR1
|
255 OMAP_DMIC_POLAR2
| OMAP_DMIC_POLAR3
);
260 static int omap_dmic_dai_trigger(struct snd_pcm_substream
*substream
,
261 int cmd
, struct snd_soc_dai
*dai
)
263 struct omap_dmic
*dmic
= snd_soc_dai_get_drvdata(dai
);
266 case SNDRV_PCM_TRIGGER_START
:
267 omap_dmic_start(dmic
);
269 case SNDRV_PCM_TRIGGER_STOP
:
270 omap_dmic_stop(dmic
);
279 static int omap_dmic_select_fclk(struct omap_dmic
*dmic
, int clk_id
,
282 struct clk
*parent_clk
, *mux
;
283 char *parent_clk_name
;
293 dev_err(dmic
->dev
, "invalid input frequency: %dHz\n", freq
);
298 if (dmic
->sysclk
== clk_id
) {
299 dmic
->fclk_freq
= freq
;
303 /* re-parent not allowed if a stream is ongoing */
304 if (dmic
->active
&& dmic_is_enabled(dmic
)) {
305 dev_err(dmic
->dev
, "can't re-parent when DMIC active\n");
310 case OMAP_DMIC_SYSCLK_PAD_CLKS
:
311 parent_clk_name
= "pad_clks_ck";
313 case OMAP_DMIC_SYSCLK_SLIMBLUS_CLKS
:
314 parent_clk_name
= "slimbus_clk";
316 case OMAP_DMIC_SYSCLK_SYNC_MUX_CLKS
:
317 parent_clk_name
= "dmic_sync_mux_ck";
320 dev_err(dmic
->dev
, "fclk clk_id (%d) not supported\n", clk_id
);
324 parent_clk
= clk_get(dmic
->dev
, parent_clk_name
);
325 if (IS_ERR(parent_clk
)) {
326 dev_err(dmic
->dev
, "can't get %s\n", parent_clk_name
);
330 mux
= clk_get_parent(dmic
->fclk
);
332 dev_err(dmic
->dev
, "can't get fck mux parent\n");
337 mutex_lock(&dmic
->mutex
);
339 /* disable clock while reparenting */
340 pm_runtime_put_sync(dmic
->dev
);
341 ret
= clk_set_parent(mux
, parent_clk
);
342 pm_runtime_get_sync(dmic
->dev
);
344 ret
= clk_set_parent(mux
, parent_clk
);
346 mutex_unlock(&dmic
->mutex
);
349 dev_err(dmic
->dev
, "re-parent failed\n");
353 dmic
->sysclk
= clk_id
;
354 dmic
->fclk_freq
= freq
;
363 static int omap_dmic_select_outclk(struct omap_dmic
*dmic
, int clk_id
,
368 if (clk_id
!= OMAP_DMIC_ABE_DMIC_CLK
) {
369 dev_err(dmic
->dev
, "output clk_id (%d) not supported\n",
379 dmic
->out_freq
= freq
;
382 dev_err(dmic
->dev
, "invalid out frequency: %dHz\n", freq
);
390 static int omap_dmic_set_dai_sysclk(struct snd_soc_dai
*dai
, int clk_id
,
391 unsigned int freq
, int dir
)
393 struct omap_dmic
*dmic
= snd_soc_dai_get_drvdata(dai
);
395 if (dir
== SND_SOC_CLOCK_IN
)
396 return omap_dmic_select_fclk(dmic
, clk_id
, freq
);
397 else if (dir
== SND_SOC_CLOCK_OUT
)
398 return omap_dmic_select_outclk(dmic
, clk_id
, freq
);
400 dev_err(dmic
->dev
, "invalid clock direction (%d)\n", dir
);
404 static int omap_dmic_probe(struct snd_soc_dai
*dai
)
406 struct omap_dmic
*dmic
= snd_soc_dai_get_drvdata(dai
);
408 pm_runtime_enable(dmic
->dev
);
410 /* Disable lines while request is ongoing */
411 pm_runtime_get_sync(dmic
->dev
);
412 omap_dmic_write(dmic
, OMAP_DMIC_CTRL_REG
, 0x00);
413 pm_runtime_put_sync(dmic
->dev
);
415 /* Configure DMIC threshold value */
416 dmic
->threshold
= OMAP_DMIC_THRES_MAX
- 3;
418 snd_soc_dai_init_dma_data(dai
, NULL
, &dmic
->dma_data
);
423 static int omap_dmic_remove(struct snd_soc_dai
*dai
)
425 struct omap_dmic
*dmic
= snd_soc_dai_get_drvdata(dai
);
427 pm_runtime_disable(dmic
->dev
);
432 static const struct snd_soc_dai_ops omap_dmic_dai_ops
= {
433 .probe
= omap_dmic_probe
,
434 .remove
= omap_dmic_remove
,
435 .startup
= omap_dmic_dai_startup
,
436 .shutdown
= omap_dmic_dai_shutdown
,
437 .hw_params
= omap_dmic_dai_hw_params
,
438 .prepare
= omap_dmic_dai_prepare
,
439 .trigger
= omap_dmic_dai_trigger
,
440 .set_sysclk
= omap_dmic_set_dai_sysclk
,
443 static struct snd_soc_dai_driver omap_dmic_dai
= {
448 .rates
= SNDRV_PCM_RATE_96000
| SNDRV_PCM_RATE_192000
,
449 .formats
= SNDRV_PCM_FMTBIT_S32_LE
,
452 .ops
= &omap_dmic_dai_ops
,
455 static const struct snd_soc_component_driver omap_dmic_component
= {
457 .legacy_dai_naming
= 1,
460 static int asoc_dmic_probe(struct platform_device
*pdev
)
462 struct omap_dmic
*dmic
;
463 struct resource
*res
;
466 dmic
= devm_kzalloc(&pdev
->dev
, sizeof(struct omap_dmic
), GFP_KERNEL
);
470 platform_set_drvdata(pdev
, dmic
);
471 dmic
->dev
= &pdev
->dev
;
472 dmic
->sysclk
= OMAP_DMIC_SYSCLK_SYNC_MUX_CLKS
;
474 mutex_init(&dmic
->mutex
);
476 dmic
->fclk
= devm_clk_get(dmic
->dev
, "fck");
477 if (IS_ERR(dmic
->fclk
)) {
478 dev_err(dmic
->dev
, "can't get fck\n");
482 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "dma");
484 dev_err(dmic
->dev
, "invalid dma memory resource\n");
487 dmic
->dma_data
.addr
= res
->start
+ OMAP_DMIC_DATA_REG
;
489 dmic
->dma_data
.filter_data
= "up_link";
491 dmic
->io_base
= devm_platform_ioremap_resource_byname(pdev
, "mpu");
492 if (IS_ERR(dmic
->io_base
))
493 return PTR_ERR(dmic
->io_base
);
495 ret
= devm_snd_soc_register_component(&pdev
->dev
,
496 &omap_dmic_component
,
501 ret
= sdma_pcm_platform_register(&pdev
->dev
, NULL
, "up_link");
508 static const struct of_device_id omap_dmic_of_match
[] = {
509 { .compatible
= "ti,omap4-dmic", },
512 MODULE_DEVICE_TABLE(of
, omap_dmic_of_match
);
514 static struct platform_driver asoc_dmic_driver
= {
517 .of_match_table
= omap_dmic_of_match
,
519 .probe
= asoc_dmic_probe
,
522 module_platform_driver(asoc_dmic_driver
);
524 MODULE_ALIAS("platform:omap-dmic");
525 MODULE_AUTHOR("Peter Ujfalusi <peter.ujfalusi@ti.com>");
526 MODULE_DESCRIPTION("OMAP DMIC ASoC Interface");
527 MODULE_LICENSE("GPL");