ASoC: amd: Fix potential NULL pointer dereference
[sfrench/cifs-2.6.git] / sound / soc / amd / raven / acp3x-pcm-dma.c
index 15a5f3b4db7172527a39ed9002ae07e9031a7160..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>
@@ -308,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;
        }
 
@@ -329,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;
@@ -337,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)
@@ -620,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));
 
@@ -660,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);
@@ -682,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,
        },
 };