Lines Matching +full:i2s +full:- +full:regs
1 // SPDX-License-Identifier: GPL-2.0-only
3 * Xtfpga I2S controller driver
17 #define DRV_NAME "xtfpga-i2s"
46 * I2S controller operation:
61 void __iomem *regs; member
69 * read-side critical section. Trigger callback sets and clears the
76 unsigned (*tx_fn)(struct xtfpga_i2s *i2s,
123 * xtfpga_pcm_tx_2x16 for 16-bit stereo.
125 * FIFO consists of 32-bit words, one word per channel, always 2 channels.
126 * If I2S interface is configured with smaller sample resolution, only
131 struct xtfpga_i2s *i2s, struct snd_pcm_runtime *runtime, \
135 (void *)runtime->dma_area; \
137 for (; i2s->tx_fifo_level < i2s->tx_fifo_high; \
138 i2s->tx_fifo_level += 2) { \
140 i2s->regs + XTFPGA_I2S_CHAN0_DATA); \
141 iowrite32(p[tx_ptr][channels - 1], \
142 i2s->regs + XTFPGA_I2S_CHAN0_DATA); \
143 if (++tx_ptr >= runtime->buffer_size) \
156 static bool xtfpga_pcm_push_tx(struct xtfpga_i2s *i2s) in xtfpga_pcm_push_tx() argument
162 tx_substream = rcu_dereference(i2s->tx_substream); in xtfpga_pcm_push_tx()
165 unsigned tx_ptr = READ_ONCE(i2s->tx_ptr); in xtfpga_pcm_push_tx()
166 unsigned new_tx_ptr = i2s->tx_fn(i2s, tx_substream->runtime, in xtfpga_pcm_push_tx()
169 cmpxchg(&i2s->tx_ptr, tx_ptr, new_tx_ptr); in xtfpga_pcm_push_tx()
176 static void xtfpga_pcm_refill_fifo(struct xtfpga_i2s *i2s) in xtfpga_pcm_refill_fifo() argument
181 regmap_read(i2s->regmap, XTFPGA_I2S_INT_STATUS, in xtfpga_pcm_refill_fifo()
185 bool tx_active = xtfpga_pcm_push_tx(i2s); in xtfpga_pcm_refill_fifo()
187 regmap_write(i2s->regmap, XTFPGA_I2S_INT_STATUS, in xtfpga_pcm_refill_fifo()
190 regmap_read(i2s->regmap, XTFPGA_I2S_INT_STATUS, in xtfpga_pcm_refill_fifo()
201 i2s->tx_fifo_level = i2s->tx_fifo_low; in xtfpga_pcm_refill_fifo()
205 regmap_write(i2s->regmap, XTFPGA_I2S_INT_MASK, in xtfpga_pcm_refill_fifo()
208 regmap_write(i2s->regmap, XTFPGA_I2S_INT_MASK, in xtfpga_pcm_refill_fifo()
212 regmap_update_bits(i2s->regmap, XTFPGA_I2S_CONFIG, in xtfpga_pcm_refill_fifo()
218 regmap_update_bits(i2s->regmap, XTFPGA_I2S_CONFIG, in xtfpga_pcm_refill_fifo()
225 struct xtfpga_i2s *i2s = dev_id; in xtfpga_i2s_threaded_irq_handler() local
229 regmap_read(i2s->regmap, XTFPGA_I2S_CONFIG, &config); in xtfpga_i2s_threaded_irq_handler()
230 regmap_read(i2s->regmap, XTFPGA_I2S_INT_MASK, &int_mask); in xtfpga_i2s_threaded_irq_handler()
231 regmap_read(i2s->regmap, XTFPGA_I2S_INT_STATUS, &int_status); in xtfpga_i2s_threaded_irq_handler()
241 i2s->tx_fifo_level = 0; in xtfpga_i2s_threaded_irq_handler()
242 regmap_update_bits(i2s->regmap, XTFPGA_I2S_CONFIG, in xtfpga_i2s_threaded_irq_handler()
248 i2s->tx_fifo_level = i2s->tx_fifo_low; in xtfpga_i2s_threaded_irq_handler()
252 tx_substream = rcu_dereference(i2s->tx_substream); in xtfpga_i2s_threaded_irq_handler()
257 dev_dbg_ratelimited(i2s->dev, "%s: underrun\n", in xtfpga_i2s_threaded_irq_handler()
265 xtfpga_pcm_refill_fifo(i2s); in xtfpga_i2s_threaded_irq_handler()
273 struct xtfpga_i2s *i2s = snd_soc_dai_get_drvdata(dai); in xtfpga_i2s_startup() local
275 snd_soc_dai_set_dma_data(dai, substream, i2s); in xtfpga_i2s_startup()
283 struct xtfpga_i2s *i2s = snd_soc_dai_get_drvdata(dai); in xtfpga_i2s_hw_params() local
291 regmap_update_bits(i2s->regmap, XTFPGA_I2S_CONFIG, in xtfpga_i2s_hw_params()
296 err = clk_set_rate(i2s->clk, freq); in xtfpga_i2s_hw_params()
300 /* ratio field of the config register controls MCLK->I2S clock in xtfpga_i2s_hw_params()
301 * derivation: I2S clock = MCLK / (2 * (ratio + 2)). in xtfpga_i2s_hw_params()
306 ratio = (freq - (srate * sample_size * 8)) / in xtfpga_i2s_hw_params()
309 regmap_update_bits(i2s->regmap, XTFPGA_I2S_CONFIG, in xtfpga_i2s_hw_params()
313 i2s->tx_fifo_low = XTFPGA_I2S_FIFO_SIZE / 2; in xtfpga_i2s_hw_params()
317 i2s->tx_fifo_low / 2 >= period_size * 2 && in xtfpga_i2s_hw_params()
320 i2s->tx_fifo_low /= 2; in xtfpga_i2s_hw_params()
322 i2s->tx_fifo_high = 2 * i2s->tx_fifo_low; in xtfpga_i2s_hw_params()
324 regmap_update_bits(i2s->regmap, XTFPGA_I2S_CONFIG, in xtfpga_i2s_hw_params()
328 dev_dbg(i2s->dev, in xtfpga_i2s_hw_params()
331 dev_dbg(i2s->dev, "%s freq: %u, ratio: %u, level: %u\n", in xtfpga_i2s_hw_params()
341 return -EINVAL; in xtfpga_i2s_set_fmt()
343 return -EINVAL; in xtfpga_i2s_set_fmt()
345 return -EINVAL; in xtfpga_i2s_set_fmt()
371 struct snd_pcm_runtime *runtime = substream->runtime; in xtfpga_pcm_open()
377 runtime->private_data = p; in xtfpga_pcm_open()
393 struct snd_pcm_runtime *runtime = substream->runtime; in xtfpga_pcm_hw_params()
394 struct xtfpga_i2s *i2s = runtime->private_data; in xtfpga_pcm_hw_params() local
403 return -EINVAL; in xtfpga_pcm_hw_params()
409 i2s->tx_fn = (channels == 1) ? in xtfpga_pcm_hw_params()
415 i2s->tx_fn = (channels == 1) ? in xtfpga_pcm_hw_params()
421 return -EINVAL; in xtfpga_pcm_hw_params()
431 struct snd_pcm_runtime *runtime = substream->runtime; in xtfpga_pcm_trigger()
432 struct xtfpga_i2s *i2s = runtime->private_data; in xtfpga_pcm_trigger() local
438 WRITE_ONCE(i2s->tx_ptr, 0); in xtfpga_pcm_trigger()
439 rcu_assign_pointer(i2s->tx_substream, substream); in xtfpga_pcm_trigger()
440 xtfpga_pcm_refill_fifo(i2s); in xtfpga_pcm_trigger()
446 rcu_assign_pointer(i2s->tx_substream, NULL); in xtfpga_pcm_trigger()
450 ret = -EINVAL; in xtfpga_pcm_trigger()
459 struct snd_pcm_runtime *runtime = substream->runtime; in xtfpga_pcm_pointer()
460 struct xtfpga_i2s *i2s = runtime->private_data; in xtfpga_pcm_pointer() local
461 snd_pcm_uframes_t pos = READ_ONCE(i2s->tx_ptr); in xtfpga_pcm_pointer()
463 return pos < runtime->buffer_size ? pos : 0; in xtfpga_pcm_pointer()
469 struct snd_card *card = rtd->card->snd_card; in xtfpga_pcm_new()
472 snd_pcm_set_managed_buffer_all(rtd->pcm, SNDRV_DMA_TYPE_DEV, in xtfpga_pcm_new()
473 card->dev, size, size); in xtfpga_pcm_new()
496 .name = "xtfpga-i2s",
511 struct xtfpga_i2s *i2s = dev_get_drvdata(dev); in xtfpga_i2s_runtime_suspend() local
513 clk_disable_unprepare(i2s->clk); in xtfpga_i2s_runtime_suspend()
519 struct xtfpga_i2s *i2s = dev_get_drvdata(dev); in xtfpga_i2s_runtime_resume() local
522 ret = clk_prepare_enable(i2s->clk); in xtfpga_i2s_runtime_resume()
532 struct xtfpga_i2s *i2s; in xtfpga_i2s_probe() local
535 i2s = devm_kzalloc(&pdev->dev, sizeof(*i2s), GFP_KERNEL); in xtfpga_i2s_probe()
536 if (!i2s) { in xtfpga_i2s_probe()
537 err = -ENOMEM; in xtfpga_i2s_probe()
540 platform_set_drvdata(pdev, i2s); in xtfpga_i2s_probe()
541 i2s->dev = &pdev->dev; in xtfpga_i2s_probe()
542 dev_dbg(&pdev->dev, "dev: %p, i2s: %p\n", &pdev->dev, i2s); in xtfpga_i2s_probe()
544 i2s->regs = devm_platform_ioremap_resource(pdev, 0); in xtfpga_i2s_probe()
545 if (IS_ERR(i2s->regs)) { in xtfpga_i2s_probe()
546 err = PTR_ERR(i2s->regs); in xtfpga_i2s_probe()
550 i2s->regmap = devm_regmap_init_mmio(&pdev->dev, i2s->regs, in xtfpga_i2s_probe()
552 if (IS_ERR(i2s->regmap)) { in xtfpga_i2s_probe()
553 dev_err(&pdev->dev, "regmap init failed\n"); in xtfpga_i2s_probe()
554 err = PTR_ERR(i2s->regmap); in xtfpga_i2s_probe()
558 i2s->clk = devm_clk_get(&pdev->dev, NULL); in xtfpga_i2s_probe()
559 if (IS_ERR(i2s->clk)) { in xtfpga_i2s_probe()
560 dev_err(&pdev->dev, "couldn't get clock\n"); in xtfpga_i2s_probe()
561 err = PTR_ERR(i2s->clk); in xtfpga_i2s_probe()
565 regmap_write(i2s->regmap, XTFPGA_I2S_CONFIG, in xtfpga_i2s_probe()
567 regmap_write(i2s->regmap, XTFPGA_I2S_INT_STATUS, XTFPGA_I2S_INT_VALID); in xtfpga_i2s_probe()
568 regmap_write(i2s->regmap, XTFPGA_I2S_INT_MASK, XTFPGA_I2S_INT_UNDERRUN); in xtfpga_i2s_probe()
575 err = devm_request_threaded_irq(&pdev->dev, irq, NULL, in xtfpga_i2s_probe()
578 pdev->name, i2s); in xtfpga_i2s_probe()
580 dev_err(&pdev->dev, "request_irq failed\n"); in xtfpga_i2s_probe()
584 err = devm_snd_soc_register_component(&pdev->dev, in xtfpga_i2s_probe()
589 dev_err(&pdev->dev, "couldn't register component\n"); in xtfpga_i2s_probe()
593 pm_runtime_enable(&pdev->dev); in xtfpga_i2s_probe()
594 if (!pm_runtime_enabled(&pdev->dev)) { in xtfpga_i2s_probe()
595 err = xtfpga_i2s_runtime_resume(&pdev->dev); in xtfpga_i2s_probe()
602 pm_runtime_disable(&pdev->dev); in xtfpga_i2s_probe()
604 dev_err(&pdev->dev, "%s: err = %d\n", __func__, err); in xtfpga_i2s_probe()
610 struct xtfpga_i2s *i2s = dev_get_drvdata(&pdev->dev); in xtfpga_i2s_remove() local
612 if (i2s->regmap && !IS_ERR(i2s->regmap)) { in xtfpga_i2s_remove()
613 regmap_write(i2s->regmap, XTFPGA_I2S_CONFIG, 0); in xtfpga_i2s_remove()
614 regmap_write(i2s->regmap, XTFPGA_I2S_INT_MASK, 0); in xtfpga_i2s_remove()
615 regmap_write(i2s->regmap, XTFPGA_I2S_INT_STATUS, in xtfpga_i2s_remove()
618 pm_runtime_disable(&pdev->dev); in xtfpga_i2s_remove()
619 if (!pm_runtime_status_suspended(&pdev->dev)) in xtfpga_i2s_remove()
620 xtfpga_i2s_runtime_suspend(&pdev->dev); in xtfpga_i2s_remove()
625 { .compatible = "cdns,xtfpga-i2s", },
640 .name = "xtfpga-i2s",
649 MODULE_DESCRIPTION("xtfpga I2S controller driver");