1 // SPDX-License-Identifier: GPL-2.0-only
3 // Copyright (C) 2020 Intel Corporation.
5 // Intel KeemBay Platform driver.
10 #include <linux/module.h>
11 #include <sound/pcm.h>
12 #include <sound/pcm_params.h>
13 #include <sound/soc.h>
14 #include "kmb_platform.h"
17 #define PERIODS_MAX 48
18 #define PERIOD_BYTES_MIN 4096
19 #define BUFFER_BYTES_MAX (PERIODS_MAX * PERIOD_BYTES_MIN)
20 #define TDM_OPERATION 1
21 #define I2S_OPERATION 0
22 #define DATA_WIDTH_CONFIG_BIT 6
23 #define TDM_CHANNEL_CONFIG_BIT 3
25 static const struct snd_pcm_hardware kmb_pcm_hardware = {
26 .info = SNDRV_PCM_INFO_INTERLEAVED |
28 SNDRV_PCM_INFO_MMAP_VALID |
29 SNDRV_PCM_INFO_BATCH |
30 SNDRV_PCM_INFO_BLOCK_TRANSFER,
31 .rates = SNDRV_PCM_RATE_8000 |
32 SNDRV_PCM_RATE_16000 |
36 .formats = SNDRV_PCM_FMTBIT_S16_LE |
37 SNDRV_PCM_FMTBIT_S24_LE |
38 SNDRV_PCM_FMTBIT_S32_LE,
41 .buffer_bytes_max = BUFFER_BYTES_MAX,
42 .period_bytes_min = PERIOD_BYTES_MIN,
43 .period_bytes_max = BUFFER_BYTES_MAX / PERIODS_MIN,
44 .periods_min = PERIODS_MIN,
45 .periods_max = PERIODS_MAX,
49 static unsigned int kmb_pcm_tx_fn(struct kmb_i2s_info *kmb_i2s,
50 struct snd_pcm_runtime *runtime,
51 unsigned int tx_ptr, bool *period_elapsed)
53 unsigned int period_pos = tx_ptr % runtime->period_size;
54 void __iomem *i2s_base = kmb_i2s->i2s_base;
55 void *buf = runtime->dma_area;
58 /* KMB i2s uses two separate L/R FIFO */
59 for (i = 0; i < kmb_i2s->fifo_th; i++) {
60 if (kmb_i2s->config.data_width == 16) {
61 writel(((u16(*)[2])buf)[tx_ptr][0], i2s_base + LRBR_LTHR(0));
62 writel(((u16(*)[2])buf)[tx_ptr][1], i2s_base + RRBR_RTHR(0));
64 writel(((u32(*)[2])buf)[tx_ptr][0], i2s_base + LRBR_LTHR(0));
65 writel(((u32(*)[2])buf)[tx_ptr][1], i2s_base + RRBR_RTHR(0));
70 if (++tx_ptr >= runtime->buffer_size)
74 *period_elapsed = period_pos >= runtime->period_size;
79 static unsigned int kmb_pcm_rx_fn(struct kmb_i2s_info *kmb_i2s,
80 struct snd_pcm_runtime *runtime,
81 unsigned int rx_ptr, bool *period_elapsed)
83 unsigned int period_pos = rx_ptr % runtime->period_size;
84 void __iomem *i2s_base = kmb_i2s->i2s_base;
85 void *buf = runtime->dma_area;
88 /* KMB i2s uses two separate L/R FIFO */
89 for (i = 0; i < kmb_i2s->fifo_th; i++) {
90 if (kmb_i2s->config.data_width == 16) {
91 ((u16(*)[2])buf)[rx_ptr][0] = readl(i2s_base + LRBR_LTHR(0));
92 ((u16(*)[2])buf)[rx_ptr][1] = readl(i2s_base + RRBR_RTHR(0));
94 ((u32(*)[2])buf)[rx_ptr][0] = readl(i2s_base + LRBR_LTHR(0));
95 ((u32(*)[2])buf)[rx_ptr][1] = readl(i2s_base + RRBR_RTHR(0));
100 if (++rx_ptr >= runtime->buffer_size)
104 *period_elapsed = period_pos >= runtime->period_size;
109 static inline void kmb_i2s_disable_channels(struct kmb_i2s_info *kmb_i2s,
114 /* Disable all channels regardless of configuration*/
115 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
116 for (i = 0; i < MAX_ISR; i++)
117 writel(0, kmb_i2s->i2s_base + TER(i));
119 for (i = 0; i < MAX_ISR; i++)
120 writel(0, kmb_i2s->i2s_base + RER(i));
124 static inline void kmb_i2s_clear_irqs(struct kmb_i2s_info *kmb_i2s, u32 stream)
126 struct i2s_clk_config_data *config = &kmb_i2s->config;
129 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
130 for (i = 0; i < config->chan_nr / 2; i++)
131 readl(kmb_i2s->i2s_base + TOR(i));
133 for (i = 0; i < config->chan_nr / 2; i++)
134 readl(kmb_i2s->i2s_base + ROR(i));
138 static inline void kmb_i2s_irq_trigger(struct kmb_i2s_info *kmb_i2s,
139 u32 stream, int chan_nr, bool trigger)
144 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
149 for (i = 0; i < chan_nr / 2; i++) {
150 irq = readl(kmb_i2s->i2s_base + IMR(i));
157 writel(irq, kmb_i2s->i2s_base + IMR(i));
161 static void kmb_pcm_operation(struct kmb_i2s_info *kmb_i2s, bool playback)
163 struct snd_pcm_substream *substream;
165 unsigned int new_ptr;
169 substream = kmb_i2s->tx_substream;
171 substream = kmb_i2s->rx_substream;
173 if (!substream || !snd_pcm_running(substream))
177 ptr = kmb_i2s->tx_ptr;
178 new_ptr = kmb_pcm_tx_fn(kmb_i2s, substream->runtime,
179 ptr, &period_elapsed);
180 cmpxchg(&kmb_i2s->tx_ptr, ptr, new_ptr);
182 ptr = kmb_i2s->rx_ptr;
183 new_ptr = kmb_pcm_rx_fn(kmb_i2s, substream->runtime,
184 ptr, &period_elapsed);
185 cmpxchg(&kmb_i2s->rx_ptr, ptr, new_ptr);
189 snd_pcm_period_elapsed(substream);
192 static int kmb_pcm_open(struct snd_soc_component *component,
193 struct snd_pcm_substream *substream)
195 struct snd_pcm_runtime *runtime = substream->runtime;
196 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
197 struct kmb_i2s_info *kmb_i2s;
199 kmb_i2s = snd_soc_dai_get_drvdata(asoc_rtd_to_cpu(rtd, 0));
200 snd_soc_set_runtime_hwparams(substream, &kmb_pcm_hardware);
201 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
202 runtime->private_data = kmb_i2s;
207 static int kmb_pcm_trigger(struct snd_soc_component *component,
208 struct snd_pcm_substream *substream, int cmd)
210 struct snd_pcm_runtime *runtime = substream->runtime;
211 struct kmb_i2s_info *kmb_i2s = runtime->private_data;
214 case SNDRV_PCM_TRIGGER_START:
215 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
217 kmb_i2s->tx_substream = substream;
220 kmb_i2s->rx_substream = substream;
223 case SNDRV_PCM_TRIGGER_STOP:
224 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
225 kmb_i2s->tx_substream = NULL;
227 kmb_i2s->rx_substream = NULL;
236 static irqreturn_t kmb_i2s_irq_handler(int irq, void *dev_id)
238 struct kmb_i2s_info *kmb_i2s = dev_id;
239 struct i2s_clk_config_data *config = &kmb_i2s->config;
240 irqreturn_t ret = IRQ_NONE;
244 for (i = 0; i < config->chan_nr / 2; i++)
245 isr[i] = readl(kmb_i2s->i2s_base + ISR(i));
247 kmb_i2s_clear_irqs(kmb_i2s, SNDRV_PCM_STREAM_PLAYBACK);
248 kmb_i2s_clear_irqs(kmb_i2s, SNDRV_PCM_STREAM_CAPTURE);
250 for (i = 0; i < config->chan_nr / 2; i++) {
252 * Check if TX fifo is empty. If empty fill FIFO with samples
254 if ((isr[i] & ISR_TXFE)) {
255 kmb_pcm_operation(kmb_i2s, true);
259 * Data available. Retrieve samples from FIFO
261 if ((isr[i] & ISR_RXDA)) {
262 kmb_pcm_operation(kmb_i2s, false);
265 /* Error Handling: TX */
266 if (isr[i] & ISR_TXFO) {
267 dev_dbg(kmb_i2s->dev, "TX overrun (ch_id=%d)\n", i);
270 /* Error Handling: RX */
271 if (isr[i] & ISR_RXFO) {
272 dev_dbg(kmb_i2s->dev, "RX overrun (ch_id=%d)\n", i);
280 static int kmb_platform_pcm_new(struct snd_soc_component *component,
281 struct snd_soc_pcm_runtime *soc_runtime)
283 size_t size = kmb_pcm_hardware.buffer_bytes_max;
284 /* Use SNDRV_DMA_TYPE_CONTINUOUS as KMB doesn't use PCI sg buffer */
285 snd_pcm_set_managed_buffer_all(soc_runtime->pcm,
286 SNDRV_DMA_TYPE_CONTINUOUS,
291 static snd_pcm_uframes_t kmb_pcm_pointer(struct snd_soc_component *component,
292 struct snd_pcm_substream *substream)
294 struct snd_pcm_runtime *runtime = substream->runtime;
295 struct kmb_i2s_info *kmb_i2s = runtime->private_data;
296 snd_pcm_uframes_t pos;
298 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
299 pos = kmb_i2s->tx_ptr;
301 pos = kmb_i2s->rx_ptr;
303 return pos < runtime->buffer_size ? pos : 0;
306 static const struct snd_soc_component_driver kmb_component = {
308 .pcm_construct = kmb_platform_pcm_new,
309 .open = kmb_pcm_open,
310 .trigger = kmb_pcm_trigger,
311 .pointer = kmb_pcm_pointer,
314 static void kmb_i2s_start(struct kmb_i2s_info *kmb_i2s,
315 struct snd_pcm_substream *substream)
317 struct i2s_clk_config_data *config = &kmb_i2s->config;
319 /* I2S Programming sequence in Keem_Bay_VPU_DB_v1.1 */
320 writel(1, kmb_i2s->i2s_base + IER);
322 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
323 writel(1, kmb_i2s->i2s_base + ITER);
325 writel(1, kmb_i2s->i2s_base + IRER);
327 kmb_i2s_irq_trigger(kmb_i2s, substream->stream, config->chan_nr, true);
330 writel(1, kmb_i2s->i2s_base + CER);
332 writel(0, kmb_i2s->i2s_base + CER);
335 static void kmb_i2s_stop(struct kmb_i2s_info *kmb_i2s,
336 struct snd_pcm_substream *substream)
338 /* I2S Programming sequence in Keem_Bay_VPU_DB_v1.1 */
339 kmb_i2s_clear_irqs(kmb_i2s, substream->stream);
341 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
342 writel(0, kmb_i2s->i2s_base + ITER);
344 writel(0, kmb_i2s->i2s_base + IRER);
346 kmb_i2s_irq_trigger(kmb_i2s, substream->stream, 8, false);
348 if (!kmb_i2s->active) {
349 writel(0, kmb_i2s->i2s_base + CER);
350 writel(0, kmb_i2s->i2s_base + IER);
354 static void kmb_disable_clk(void *clk)
356 clk_disable_unprepare(clk);
359 static int kmb_set_dai_fmt(struct snd_soc_dai *cpu_dai, unsigned int fmt)
361 struct kmb_i2s_info *kmb_i2s = snd_soc_dai_get_drvdata(cpu_dai);
364 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
365 case SND_SOC_DAIFMT_CBM_CFM:
366 kmb_i2s->master = false;
369 case SND_SOC_DAIFMT_CBS_CFS:
370 writel(MASTER_MODE, kmb_i2s->pss_base + I2S_GEN_CFG_0);
372 ret = clk_prepare_enable(kmb_i2s->clk_i2s);
376 ret = devm_add_action_or_reset(kmb_i2s->dev, kmb_disable_clk,
381 kmb_i2s->master = true;
390 static int kmb_dai_trigger(struct snd_pcm_substream *substream,
391 int cmd, struct snd_soc_dai *cpu_dai)
393 struct kmb_i2s_info *kmb_i2s = snd_soc_dai_get_drvdata(cpu_dai);
396 case SNDRV_PCM_TRIGGER_START:
397 /* Keep track of i2s activity before turn off
401 kmb_i2s_start(kmb_i2s, substream);
403 case SNDRV_PCM_TRIGGER_STOP:
405 kmb_i2s_stop(kmb_i2s, substream);
414 static void kmb_i2s_config(struct kmb_i2s_info *kmb_i2s, int stream)
416 struct i2s_clk_config_data *config = &kmb_i2s->config;
419 kmb_i2s_disable_channels(kmb_i2s, stream);
421 for (ch_reg = 0; ch_reg < config->chan_nr / 2; ch_reg++) {
422 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
423 writel(kmb_i2s->xfer_resolution,
424 kmb_i2s->i2s_base + TCR(ch_reg));
426 writel(kmb_i2s->fifo_th - 1,
427 kmb_i2s->i2s_base + TFCR(ch_reg));
429 writel(1, kmb_i2s->i2s_base + TER(ch_reg));
431 writel(kmb_i2s->xfer_resolution,
432 kmb_i2s->i2s_base + RCR(ch_reg));
434 writel(kmb_i2s->fifo_th - 1,
435 kmb_i2s->i2s_base + RFCR(ch_reg));
437 writel(1, kmb_i2s->i2s_base + RER(ch_reg));
442 static int kmb_dai_hw_params(struct snd_pcm_substream *substream,
443 struct snd_pcm_hw_params *hw_params,
444 struct snd_soc_dai *cpu_dai)
446 struct kmb_i2s_info *kmb_i2s = snd_soc_dai_get_drvdata(cpu_dai);
447 struct i2s_clk_config_data *config = &kmb_i2s->config;
448 u32 register_val, write_val;
451 switch (params_format(hw_params)) {
452 case SNDRV_PCM_FORMAT_S16_LE:
453 config->data_width = 16;
455 kmb_i2s->xfer_resolution = 0x02;
457 case SNDRV_PCM_FORMAT_S24_LE:
458 config->data_width = 24;
460 kmb_i2s->xfer_resolution = 0x04;
462 case SNDRV_PCM_FORMAT_S32_LE:
463 config->data_width = 32;
465 kmb_i2s->xfer_resolution = 0x05;
468 dev_err(kmb_i2s->dev, "kmb: unsupported PCM fmt");
472 config->chan_nr = params_channels(hw_params);
474 switch (config->chan_nr) {
475 /* TODO: This switch case will handle up to TDM8 in the near future */
476 case TWO_CHANNEL_SUPPORT:
477 write_val = ((config->chan_nr / 2) << TDM_CHANNEL_CONFIG_BIT) |
478 (config->data_width << DATA_WIDTH_CONFIG_BIT) |
479 MASTER_MODE | I2S_OPERATION;
481 writel(write_val, kmb_i2s->pss_base + I2S_GEN_CFG_0);
483 register_val = readl(kmb_i2s->pss_base + I2S_GEN_CFG_0);
484 dev_dbg(kmb_i2s->dev, "pss register = 0x%X", register_val);
487 dev_dbg(kmb_i2s->dev, "channel not supported\n");
491 kmb_i2s_config(kmb_i2s, substream->stream);
493 writel(kmb_i2s->ccr, kmb_i2s->i2s_base + CCR);
495 config->sample_rate = params_rate(hw_params);
497 if (kmb_i2s->master) {
498 /* Only 2 ch supported in Master mode */
499 u32 bitclk = config->sample_rate * config->data_width * 2;
501 ret = clk_set_rate(kmb_i2s->clk_i2s, bitclk);
503 dev_err(kmb_i2s->dev,
504 "Can't set I2S clock rate: %d\n", ret);
512 static int kmb_dai_prepare(struct snd_pcm_substream *substream,
513 struct snd_soc_dai *cpu_dai)
515 struct kmb_i2s_info *kmb_i2s = snd_soc_dai_get_drvdata(cpu_dai);
517 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
518 writel(1, kmb_i2s->i2s_base + TXFFR);
520 writel(1, kmb_i2s->i2s_base + RXFFR);
525 static struct snd_soc_dai_ops kmb_dai_ops = {
526 .trigger = kmb_dai_trigger,
527 .hw_params = kmb_dai_hw_params,
528 .prepare = kmb_dai_prepare,
529 .set_fmt = kmb_set_dai_fmt,
532 static struct snd_soc_dai_driver intel_kmb_platform_dai[] = {
534 .name = "kmb-plat-dai",
538 .rates = SNDRV_PCM_RATE_8000 |
539 SNDRV_PCM_RATE_16000 |
540 SNDRV_PCM_RATE_48000,
543 .formats = (SNDRV_PCM_FMTBIT_S32_LE |
544 SNDRV_PCM_FMTBIT_S24_LE |
545 SNDRV_PCM_FMTBIT_S16_LE),
551 * .channels_max will be overwritten
552 * if provided by Device Tree
554 .rates = SNDRV_PCM_RATE_8000 |
555 SNDRV_PCM_RATE_16000 |
556 SNDRV_PCM_RATE_48000,
559 .formats = (SNDRV_PCM_FMTBIT_S32_LE |
560 SNDRV_PCM_FMTBIT_S24_LE |
561 SNDRV_PCM_FMTBIT_S16_LE),
567 static int kmb_plat_dai_probe(struct platform_device *pdev)
569 struct snd_soc_dai_driver *kmb_i2s_dai;
570 struct device *dev = &pdev->dev;
571 struct kmb_i2s_info *kmb_i2s;
575 kmb_i2s = devm_kzalloc(dev, sizeof(*kmb_i2s), GFP_KERNEL);
579 kmb_i2s_dai = devm_kzalloc(dev, sizeof(*kmb_i2s_dai), GFP_KERNEL);
583 kmb_i2s_dai->ops = &kmb_dai_ops;
585 /* Prepare the related clocks */
586 kmb_i2s->clk_apb = devm_clk_get(dev, "apb_clk");
587 if (IS_ERR(kmb_i2s->clk_apb)) {
588 dev_err(dev, "Failed to get apb clock\n");
589 return PTR_ERR(kmb_i2s->clk_apb);
592 ret = clk_prepare_enable(kmb_i2s->clk_apb);
596 ret = devm_add_action_or_reset(dev, kmb_disable_clk, kmb_i2s->clk_apb);
598 dev_err(dev, "Failed to add clk_apb reset action\n");
602 kmb_i2s->clk_i2s = devm_clk_get(dev, "osc");
603 if (IS_ERR(kmb_i2s->clk_i2s)) {
604 dev_err(dev, "Failed to get osc clock\n");
605 return PTR_ERR(kmb_i2s->clk_i2s);
608 kmb_i2s->i2s_base = devm_platform_ioremap_resource(pdev, 0);
609 if (IS_ERR(kmb_i2s->i2s_base))
610 return PTR_ERR(kmb_i2s->i2s_base);
612 kmb_i2s->pss_base = devm_platform_ioremap_resource(pdev, 1);
613 if (IS_ERR(kmb_i2s->pss_base))
614 return PTR_ERR(kmb_i2s->pss_base);
616 kmb_i2s->dev = &pdev->dev;
618 irq = platform_get_irq_optional(pdev, 0);
620 ret = devm_request_irq(dev, irq, kmb_i2s_irq_handler, 0,
621 pdev->name, kmb_i2s);
623 dev_err(dev, "failed to request irq\n");
628 comp1_reg = readl(kmb_i2s->i2s_base + I2S_COMP_PARAM_1);
630 kmb_i2s->fifo_th = (1 << COMP1_FIFO_DEPTH(comp1_reg)) / 2;
632 ret = devm_snd_soc_register_component(dev, &kmb_component,
633 intel_kmb_platform_dai,
634 ARRAY_SIZE(intel_kmb_platform_dai));
636 dev_err(dev, "not able to register dai\n");
640 /* To ensure none of the channels are enabled at boot up */
641 kmb_i2s_disable_channels(kmb_i2s, SNDRV_PCM_STREAM_PLAYBACK);
642 kmb_i2s_disable_channels(kmb_i2s, SNDRV_PCM_STREAM_CAPTURE);
644 dev_set_drvdata(dev, kmb_i2s);
649 static const struct of_device_id kmb_plat_of_match[] = {
650 { .compatible = "intel,keembay-i2s", },
654 static struct platform_driver kmb_plat_dai_driver = {
656 .name = "kmb-plat-dai",
657 .of_match_table = kmb_plat_of_match,
659 .probe = kmb_plat_dai_probe,
662 module_platform_driver(kmb_plat_dai_driver);
664 MODULE_DESCRIPTION("ASoC Intel KeemBay Platform driver");
665 MODULE_AUTHOR("Sia Jee Heng <jee.heng.sia@intel.com>");
666 MODULE_AUTHOR("Sit, Michael Wei Hong <michael.wei.hong.sit@intel.com>");
667 MODULE_LICENSE("GPL v2");
668 MODULE_ALIAS("platform:kmb_platform");