ASoC: amd: Fix potential NULL pointer dereference
[sfrench/cifs-2.6.git] / sound / soc / amd / raven / acp3x-pcm-dma.c
index 06c1985c4d56dbb6c51c67c2c4638746b0a5eb24..3d58338fa3cf79b3d9c28351cfd9411167fe51a5 100644 (file)
@@ -1,22 +1,14 @@
-/*
- * AMD ALSA SoC PCM Driver
- *
- * Copyright 2016 Advanced Micro Devices, Inc.
- *
- * This program is free software; you can redistribute it and/or modify it
- * under the terms and conditions of the GNU General Public License,
- * version 2, as published by the Free Software Foundation.
- *
- * This program is distributed in the hope it will be useful, but WITHOUT
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
- * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
- * more details.
- */
+// SPDX-License-Identifier: GPL-2.0+
+//
+// AMD ALSA SoC PCM Driver
+//
+//Copyright 2016 Advanced Micro Devices, Inc.
 
 #include <linux/platform_device.h>
 #include <linux/module.h>
 #include <linux/err.h>
 #include <linux/io.h>
+#include <linux/pm_runtime.h>
 #include <sound/pcm.h>
 #include <sound/pcm_params.h>
 #include <sound/soc.h>
@@ -27,7 +19,9 @@
 #define DRV_NAME "acp3x-i2s-audio"
 
 struct i2s_dev_data {
+       bool tdm_mode;
        unsigned int i2s_irq;
+       u32 tdm_fmt;
        void __iomem *acp3x_base;
        struct snd_pcm_substream *play_stream;
        struct snd_pcm_substream *capture_stream;
@@ -306,6 +300,7 @@ static int acp3x_dma_open(struct snd_pcm_substream *substream)
                                            SNDRV_PCM_HW_PARAM_PERIODS);
        if (ret < 0) {
                dev_err(component->dev, "set integer constraint failed\n");
+               kfree(i2s_data);
                return ret;
        }
 
@@ -327,7 +322,6 @@ static int acp3x_dma_hw_params(struct snd_pcm_substream *substream,
 {
        int status;
        u64 size;
-       struct snd_dma_buffer *dma_buffer;
        struct page *pg;
        struct snd_pcm_runtime *runtime = substream->runtime;
        struct i2s_stream_instance *rtd = runtime->private_data;
@@ -335,7 +329,6 @@ static int acp3x_dma_hw_params(struct snd_pcm_substream *substream,
        if (!rtd)
                return -EINVAL;
 
-       dma_buffer = &substream->dma_buffer;
        size = params_buffer_bytes(params);
        status = snd_pcm_lib_malloc_pages(substream, size);
        if (status < 0)
@@ -423,10 +416,153 @@ static struct snd_pcm_ops acp3x_dma_ops = {
        .mmap = acp3x_dma_mmap,
 };
 
+
+static int acp3x_dai_i2s_set_fmt(struct snd_soc_dai *cpu_dai, unsigned int fmt)
+{
+
+       struct i2s_dev_data *adata = snd_soc_dai_get_drvdata(cpu_dai);
+
+       switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
+       case SND_SOC_DAIFMT_I2S:
+               adata->tdm_mode = false;
+               break;
+       case SND_SOC_DAIFMT_DSP_A:
+               adata->tdm_mode = true;
+               break;
+       default:
+               return -EINVAL;
+       }
+
+       return 0;
+}
+
+static int acp3x_dai_set_tdm_slot(struct snd_soc_dai *cpu_dai, u32 tx_mask,
+                                 u32 rx_mask, int slots, int slot_width)
+{
+       u32 val = 0;
+       u16 slot_len;
+
+       struct i2s_dev_data *adata = snd_soc_dai_get_drvdata(cpu_dai);
+
+       switch (slot_width) {
+       case SLOT_WIDTH_8:
+               slot_len = 8;
+               break;
+       case SLOT_WIDTH_16:
+               slot_len = 16;
+               break;
+       case SLOT_WIDTH_24:
+               slot_len = 24;
+               break;
+       case SLOT_WIDTH_32:
+               slot_len = 0;
+               break;
+       default:
+               return -EINVAL;
+       }
+
+       val = rv_readl(adata->acp3x_base + mmACP_BTTDM_ITER);
+       rv_writel((val | 0x2), adata->acp3x_base + mmACP_BTTDM_ITER);
+       val = rv_readl(adata->acp3x_base + mmACP_BTTDM_IRER);
+       rv_writel((val | 0x2), adata->acp3x_base + mmACP_BTTDM_IRER);
+
+       val = (FRM_LEN | (slots << 15) | (slot_len << 18));
+       rv_writel(val, adata->acp3x_base + mmACP_BTTDM_TXFRMT);
+       rv_writel(val, adata->acp3x_base + mmACP_BTTDM_RXFRMT);
+
+       adata->tdm_fmt = val;
+       return 0;
+}
+
+static int acp3x_dai_i2s_hwparams(struct snd_pcm_substream *substream,
+                                 struct snd_pcm_hw_params *params,
+                                 struct snd_soc_dai *dai)
+{
+       u32 val = 0;
+       struct i2s_stream_instance *rtd = substream->runtime->private_data;
+
+       switch (params_format(params)) {
+       case SNDRV_PCM_FORMAT_U8:
+       case SNDRV_PCM_FORMAT_S8:
+               rtd->xfer_resolution = 0x0;
+               break;
+       case SNDRV_PCM_FORMAT_S16_LE:
+               rtd->xfer_resolution = 0x02;
+               break;
+       case SNDRV_PCM_FORMAT_S24_LE:
+               rtd->xfer_resolution = 0x04;
+               break;
+       case SNDRV_PCM_FORMAT_S32_LE:
+               rtd->xfer_resolution = 0x05;
+               break;
+       default:
+               return -EINVAL;
+       }
+       val = rv_readl(rtd->acp3x_base + mmACP_BTTDM_ITER);
+       val = val | (rtd->xfer_resolution  << 3);
+       if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
+               rv_writel(val, rtd->acp3x_base + mmACP_BTTDM_ITER);
+       else
+               rv_writel(val, rtd->acp3x_base + mmACP_BTTDM_IRER);
+
+       return 0;
+}
+
+static int acp3x_dai_i2s_trigger(struct snd_pcm_substream *substream,
+                                int cmd, struct snd_soc_dai *dai)
+{
+       int ret = 0;
+       struct i2s_stream_instance *rtd = substream->runtime->private_data;
+       u32 val, period_bytes;
+
+       period_bytes = frames_to_bytes(substream->runtime,
+                                      substream->runtime->period_size);
+       switch (cmd) {
+       case SNDRV_PCM_TRIGGER_START:
+       case SNDRV_PCM_TRIGGER_RESUME:
+       case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
+               if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
+                       rv_writel(period_bytes, rtd->acp3x_base +
+                                 mmACP_BT_TX_INTR_WATERMARK_SIZE);
+                       val = rv_readl(rtd->acp3x_base + mmACP_BTTDM_ITER);
+                       val = val | BIT(0);
+                       rv_writel(val, rtd->acp3x_base + mmACP_BTTDM_ITER);
+               } else {
+                       rv_writel(period_bytes, rtd->acp3x_base +
+                                 mmACP_BT_RX_INTR_WATERMARK_SIZE);
+                       val = rv_readl(rtd->acp3x_base + mmACP_BTTDM_IRER);
+                       val = val | BIT(0);
+                       rv_writel(val, rtd->acp3x_base + mmACP_BTTDM_IRER);
+               }
+               rv_writel(1, rtd->acp3x_base + mmACP_BTTDM_IER);
+               break;
+       case SNDRV_PCM_TRIGGER_STOP:
+       case SNDRV_PCM_TRIGGER_SUSPEND:
+       case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
+               if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
+                       val = rv_readl(rtd->acp3x_base + mmACP_BTTDM_ITER);
+                       val = val & ~BIT(0);
+                       rv_writel(val, rtd->acp3x_base + mmACP_BTTDM_ITER);
+               } else {
+                       val = rv_readl(rtd->acp3x_base + mmACP_BTTDM_IRER);
+                       val = val & ~BIT(0);
+                       rv_writel(val, rtd->acp3x_base + mmACP_BTTDM_IRER);
+               }
+               rv_writel(0, rtd->acp3x_base + mmACP_BTTDM_IER);
+               break;
+       default:
+               ret = -EINVAL;
+               break;
+       }
+
+       return ret;
+}
+
 struct snd_soc_dai_ops acp3x_dai_i2s_ops = {
-       .hw_params = NULL,
-       .trigger   = NULL,
-       .set_fmt = NULL,
+       .hw_params = acp3x_dai_i2s_hwparams,
+       .trigger   = acp3x_dai_i2s_trigger,
+       .set_fmt = acp3x_dai_i2s_set_fmt,
+       .set_tdm_slot = acp3x_dai_set_tdm_slot,
 };
 
 static struct snd_soc_dai_driver acp3x_i2s_dai_driver = {
@@ -475,14 +611,16 @@ static int acp3x_audio_probe(struct platform_device *pdev)
        }
        irqflags = *((unsigned int *)(pdev->dev.platform_data));
 
-       adata = devm_kzalloc(&pdev->dev, sizeof(struct i2s_dev_data),
-                            GFP_KERNEL);
        res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
        if (!res) {
                dev_err(&pdev->dev, "IORESOURCE_IRQ FAILED\n");
                        return -ENODEV;
        }
 
+       adata = devm_kzalloc(&pdev->dev, sizeof(*adata), GFP_KERNEL);
+       if (!adata)
+               return -ENOMEM;
+
        adata->acp3x_base = devm_ioremap(&pdev->dev, res->start,
                                         resource_size(res));
 
@@ -515,6 +653,9 @@ static int acp3x_audio_probe(struct platform_device *pdev)
                goto dev_err;
        }
 
+       pm_runtime_set_autosuspend_delay(&pdev->dev, 10000);
+       pm_runtime_use_autosuspend(&pdev->dev);
+       pm_runtime_enable(&pdev->dev);
        return 0;
 dev_err:
        status = acp3x_deinit(adata->acp3x_base);
@@ -537,14 +678,95 @@ static int acp3x_audio_remove(struct platform_device *pdev)
        else
                dev_info(&pdev->dev, "ACP de-initialized\n");
 
+       pm_runtime_disable(&pdev->dev);
        return 0;
 }
 
+static int acp3x_resume(struct device *dev)
+{
+       int status;
+       u32 val;
+       struct i2s_dev_data *adata = dev_get_drvdata(dev);
+
+       status = acp3x_init(adata->acp3x_base);
+       if (status)
+               return -ENODEV;
+
+       if (adata->play_stream && adata->play_stream->runtime) {
+               struct i2s_stream_instance *rtd =
+                       adata->play_stream->runtime->private_data;
+               config_acp3x_dma(rtd, SNDRV_PCM_STREAM_PLAYBACK);
+               rv_writel((rtd->xfer_resolution  << 3),
+                         rtd->acp3x_base + mmACP_BTTDM_ITER);
+               if (adata->tdm_mode == true) {
+                       rv_writel(adata->tdm_fmt, adata->acp3x_base +
+                                 mmACP_BTTDM_TXFRMT);
+                       val = rv_readl(adata->acp3x_base + mmACP_BTTDM_ITER);
+                       rv_writel((val | 0x2), adata->acp3x_base +
+                                 mmACP_BTTDM_ITER);
+               }
+       }
+
+       if (adata->capture_stream && adata->capture_stream->runtime) {
+               struct i2s_stream_instance *rtd =
+                       adata->capture_stream->runtime->private_data;
+               config_acp3x_dma(rtd, SNDRV_PCM_STREAM_CAPTURE);
+               rv_writel((rtd->xfer_resolution  << 3),
+                         rtd->acp3x_base + mmACP_BTTDM_IRER);
+               if (adata->tdm_mode == true) {
+                       rv_writel(adata->tdm_fmt, adata->acp3x_base +
+                                 mmACP_BTTDM_RXFRMT);
+                       val = rv_readl(adata->acp3x_base + mmACP_BTTDM_IRER);
+                       rv_writel((val | 0x2), adata->acp3x_base +
+                                 mmACP_BTTDM_IRER);
+               }
+       }
+
+       rv_writel(1, adata->acp3x_base + mmACP_EXTERNAL_INTR_ENB);
+       return 0;
+}
+
+
+static int acp3x_pcm_runtime_suspend(struct device *dev)
+{
+       int status;
+       struct i2s_dev_data *adata = dev_get_drvdata(dev);
+
+       status = acp3x_deinit(adata->acp3x_base);
+       if (status)
+               dev_err(dev, "ACP de-init failed\n");
+       else
+               dev_info(dev, "ACP de-initialized\n");
+
+       rv_writel(0, adata->acp3x_base + mmACP_EXTERNAL_INTR_ENB);
+
+       return 0;
+}
+
+static int acp3x_pcm_runtime_resume(struct device *dev)
+{
+       int status;
+       struct i2s_dev_data *adata = dev_get_drvdata(dev);
+
+       status = acp3x_init(adata->acp3x_base);
+       if (status)
+               return -ENODEV;
+       rv_writel(1, adata->acp3x_base + mmACP_EXTERNAL_INTR_ENB);
+       return 0;
+}
+
+static const struct dev_pm_ops acp3x_pm_ops = {
+       .runtime_suspend = acp3x_pcm_runtime_suspend,
+       .runtime_resume = acp3x_pcm_runtime_resume,
+       .resume = acp3x_resume,
+};
+
 static struct platform_driver acp3x_dma_driver = {
        .probe = acp3x_audio_probe,
        .remove = acp3x_audio_remove,
        .driver = {
                .name = "acp3x_rv_i2s",
+               .pm = &acp3x_pm_ops,
        },
 };