Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Copyright (C) 2012-2013, Analog Devices Inc.
0004  * Author: Lars-Peter Clausen <lars@metafoo.de>
0005  */
0006 
0007 #include <linux/clk.h>
0008 #include <linux/init.h>
0009 #include <linux/kernel.h>
0010 #include <linux/module.h>
0011 #include <linux/of.h>
0012 #include <linux/platform_device.h>
0013 #include <linux/regmap.h>
0014 #include <linux/slab.h>
0015 
0016 #include <sound/core.h>
0017 #include <sound/pcm.h>
0018 #include <sound/pcm_params.h>
0019 #include <sound/soc.h>
0020 #include <sound/dmaengine_pcm.h>
0021 
0022 #define AXI_I2S_REG_RESET   0x00
0023 #define AXI_I2S_REG_CTRL    0x04
0024 #define AXI_I2S_REG_CLK_CTRL    0x08
0025 #define AXI_I2S_REG_STATUS  0x10
0026 
0027 #define AXI_I2S_REG_RX_FIFO 0x28
0028 #define AXI_I2S_REG_TX_FIFO 0x2C
0029 
0030 #define AXI_I2S_RESET_GLOBAL    BIT(0)
0031 #define AXI_I2S_RESET_TX_FIFO   BIT(1)
0032 #define AXI_I2S_RESET_RX_FIFO   BIT(2)
0033 
0034 #define AXI_I2S_CTRL_TX_EN  BIT(0)
0035 #define AXI_I2S_CTRL_RX_EN  BIT(1)
0036 
0037 /* The frame size is configurable, but for now we always set it 64 bit */
0038 #define AXI_I2S_BITS_PER_FRAME 64
0039 
0040 struct axi_i2s {
0041     struct regmap *regmap;
0042     struct clk *clk;
0043     struct clk *clk_ref;
0044 
0045     bool   has_capture;
0046     bool   has_playback;
0047 
0048     struct snd_soc_dai_driver dai_driver;
0049 
0050     struct snd_dmaengine_dai_dma_data capture_dma_data;
0051     struct snd_dmaengine_dai_dma_data playback_dma_data;
0052 
0053     struct snd_ratnum ratnum;
0054     struct snd_pcm_hw_constraint_ratnums rate_constraints;
0055 };
0056 
0057 static int axi_i2s_trigger(struct snd_pcm_substream *substream, int cmd,
0058     struct snd_soc_dai *dai)
0059 {
0060     struct axi_i2s *i2s = snd_soc_dai_get_drvdata(dai);
0061     unsigned int mask, val;
0062 
0063     if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
0064         mask = AXI_I2S_CTRL_RX_EN;
0065     else
0066         mask = AXI_I2S_CTRL_TX_EN;
0067 
0068     switch (cmd) {
0069     case SNDRV_PCM_TRIGGER_START:
0070     case SNDRV_PCM_TRIGGER_RESUME:
0071     case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
0072         val = mask;
0073         break;
0074     case SNDRV_PCM_TRIGGER_STOP:
0075     case SNDRV_PCM_TRIGGER_SUSPEND:
0076     case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
0077         val = 0;
0078         break;
0079     default:
0080         return -EINVAL;
0081     }
0082 
0083     regmap_update_bits(i2s->regmap, AXI_I2S_REG_CTRL, mask, val);
0084 
0085     return 0;
0086 }
0087 
0088 static int axi_i2s_hw_params(struct snd_pcm_substream *substream,
0089     struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
0090 {
0091     struct axi_i2s *i2s = snd_soc_dai_get_drvdata(dai);
0092     unsigned int bclk_div, word_size;
0093     unsigned int bclk_rate;
0094 
0095     bclk_rate = params_rate(params) * AXI_I2S_BITS_PER_FRAME;
0096 
0097     word_size = AXI_I2S_BITS_PER_FRAME / 2 - 1;
0098     bclk_div = DIV_ROUND_UP(clk_get_rate(i2s->clk_ref), bclk_rate) / 2 - 1;
0099 
0100     regmap_write(i2s->regmap, AXI_I2S_REG_CLK_CTRL, (word_size << 16) |
0101         bclk_div);
0102 
0103     return 0;
0104 }
0105 
0106 static int axi_i2s_startup(struct snd_pcm_substream *substream,
0107     struct snd_soc_dai *dai)
0108 {
0109     struct axi_i2s *i2s = snd_soc_dai_get_drvdata(dai);
0110     uint32_t mask;
0111     int ret;
0112 
0113     if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
0114         mask = AXI_I2S_RESET_RX_FIFO;
0115     else
0116         mask = AXI_I2S_RESET_TX_FIFO;
0117 
0118     regmap_write(i2s->regmap, AXI_I2S_REG_RESET, mask);
0119 
0120     ret = snd_pcm_hw_constraint_ratnums(substream->runtime, 0,
0121                SNDRV_PCM_HW_PARAM_RATE,
0122                &i2s->rate_constraints);
0123     if (ret)
0124         return ret;
0125 
0126     return clk_prepare_enable(i2s->clk_ref);
0127 }
0128 
0129 static void axi_i2s_shutdown(struct snd_pcm_substream *substream,
0130     struct snd_soc_dai *dai)
0131 {
0132     struct axi_i2s *i2s = snd_soc_dai_get_drvdata(dai);
0133 
0134     clk_disable_unprepare(i2s->clk_ref);
0135 }
0136 
0137 static int axi_i2s_dai_probe(struct snd_soc_dai *dai)
0138 {
0139     struct axi_i2s *i2s = snd_soc_dai_get_drvdata(dai);
0140 
0141     snd_soc_dai_init_dma_data(
0142         dai,
0143         i2s->has_playback ? &i2s->playback_dma_data : NULL,
0144         i2s->has_capture  ? &i2s->capture_dma_data  : NULL);
0145 
0146     return 0;
0147 }
0148 
0149 static const struct snd_soc_dai_ops axi_i2s_dai_ops = {
0150     .startup = axi_i2s_startup,
0151     .shutdown = axi_i2s_shutdown,
0152     .trigger = axi_i2s_trigger,
0153     .hw_params = axi_i2s_hw_params,
0154 };
0155 
0156 static struct snd_soc_dai_driver axi_i2s_dai = {
0157     .probe = axi_i2s_dai_probe,
0158     .ops = &axi_i2s_dai_ops,
0159     .symmetric_rate = 1,
0160 };
0161 
0162 static const struct snd_soc_component_driver axi_i2s_component = {
0163     .name = "axi-i2s",
0164     .legacy_dai_naming = 1,
0165 };
0166 
0167 static const struct regmap_config axi_i2s_regmap_config = {
0168     .reg_bits = 32,
0169     .reg_stride = 4,
0170     .val_bits = 32,
0171     .max_register = AXI_I2S_REG_STATUS,
0172 };
0173 
0174 static void axi_i2s_parse_of(struct axi_i2s *i2s, const struct device_node *np)
0175 {
0176     struct property *dma_names;
0177     const char *dma_name;
0178 
0179     of_property_for_each_string(np, "dma-names", dma_names, dma_name) {
0180         if (strcmp(dma_name, "rx") == 0)
0181             i2s->has_capture = true;
0182         if (strcmp(dma_name, "tx") == 0)
0183             i2s->has_playback = true;
0184     }
0185 }
0186 
0187 static int axi_i2s_probe(struct platform_device *pdev)
0188 {
0189     struct resource *res;
0190     struct axi_i2s *i2s;
0191     void __iomem *base;
0192     int ret;
0193 
0194     i2s = devm_kzalloc(&pdev->dev, sizeof(*i2s), GFP_KERNEL);
0195     if (!i2s)
0196         return -ENOMEM;
0197 
0198     platform_set_drvdata(pdev, i2s);
0199 
0200     axi_i2s_parse_of(i2s, pdev->dev.of_node);
0201 
0202     base = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
0203     if (IS_ERR(base))
0204         return PTR_ERR(base);
0205 
0206     i2s->regmap = devm_regmap_init_mmio(&pdev->dev, base,
0207         &axi_i2s_regmap_config);
0208     if (IS_ERR(i2s->regmap))
0209         return PTR_ERR(i2s->regmap);
0210 
0211     i2s->clk = devm_clk_get(&pdev->dev, "axi");
0212     if (IS_ERR(i2s->clk))
0213         return PTR_ERR(i2s->clk);
0214 
0215     i2s->clk_ref = devm_clk_get(&pdev->dev, "ref");
0216     if (IS_ERR(i2s->clk_ref))
0217         return PTR_ERR(i2s->clk_ref);
0218 
0219     ret = clk_prepare_enable(i2s->clk);
0220     if (ret)
0221         return ret;
0222 
0223     if (i2s->has_playback) {
0224         axi_i2s_dai.playback.channels_min = 2;
0225         axi_i2s_dai.playback.channels_max = 2;
0226         axi_i2s_dai.playback.rates = SNDRV_PCM_RATE_KNOT;
0227         axi_i2s_dai.playback.formats =
0228             SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_U32_LE;
0229 
0230         i2s->playback_dma_data.addr = res->start + AXI_I2S_REG_TX_FIFO;
0231         i2s->playback_dma_data.addr_width = 4;
0232         i2s->playback_dma_data.maxburst = 1;
0233     }
0234 
0235     if (i2s->has_capture) {
0236         axi_i2s_dai.capture.channels_min = 2;
0237         axi_i2s_dai.capture.channels_max = 2;
0238         axi_i2s_dai.capture.rates = SNDRV_PCM_RATE_KNOT;
0239         axi_i2s_dai.capture.formats =
0240             SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_U32_LE;
0241 
0242         i2s->capture_dma_data.addr = res->start + AXI_I2S_REG_RX_FIFO;
0243         i2s->capture_dma_data.addr_width = 4;
0244         i2s->capture_dma_data.maxburst = 1;
0245     }
0246 
0247     i2s->ratnum.num = clk_get_rate(i2s->clk_ref) / 2 / AXI_I2S_BITS_PER_FRAME;
0248     i2s->ratnum.den_step = 1;
0249     i2s->ratnum.den_min = 1;
0250     i2s->ratnum.den_max = 64;
0251 
0252     i2s->rate_constraints.rats = &i2s->ratnum;
0253     i2s->rate_constraints.nrats = 1;
0254 
0255     regmap_write(i2s->regmap, AXI_I2S_REG_RESET, AXI_I2S_RESET_GLOBAL);
0256 
0257     ret = devm_snd_soc_register_component(&pdev->dev, &axi_i2s_component,
0258                      &axi_i2s_dai, 1);
0259     if (ret)
0260         goto err_clk_disable;
0261 
0262     ret = devm_snd_dmaengine_pcm_register(&pdev->dev, NULL, 0);
0263     if (ret)
0264         goto err_clk_disable;
0265 
0266     dev_info(&pdev->dev, "probed, capture %s, playback %s\n",
0267          i2s->has_capture ? "enabled" : "disabled",
0268          i2s->has_playback ? "enabled" : "disabled");
0269 
0270     return 0;
0271 
0272 err_clk_disable:
0273     clk_disable_unprepare(i2s->clk);
0274     return ret;
0275 }
0276 
0277 static int axi_i2s_dev_remove(struct platform_device *pdev)
0278 {
0279     struct axi_i2s *i2s = platform_get_drvdata(pdev);
0280 
0281     clk_disable_unprepare(i2s->clk);
0282 
0283     return 0;
0284 }
0285 
0286 static const struct of_device_id axi_i2s_of_match[] = {
0287     { .compatible = "adi,axi-i2s-1.00.a", },
0288     {},
0289 };
0290 MODULE_DEVICE_TABLE(of, axi_i2s_of_match);
0291 
0292 static struct platform_driver axi_i2s_driver = {
0293     .driver = {
0294         .name = "axi-i2s",
0295         .of_match_table = axi_i2s_of_match,
0296     },
0297     .probe = axi_i2s_probe,
0298     .remove = axi_i2s_dev_remove,
0299 };
0300 module_platform_driver(axi_i2s_driver);
0301 
0302 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
0303 MODULE_DESCRIPTION("AXI I2S driver");
0304 MODULE_LICENSE("GPL");