2019-06-04 16:11:33 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2015-10-08 21:31:13 +08:00
|
|
|
/* sound/soc/rockchip/rk_spdif.c
|
|
|
|
*
|
|
|
|
* ALSA SoC Audio Layer - Rockchip I2S Controller driver
|
|
|
|
*
|
|
|
|
* Copyright (c) 2014 Rockchip Electronics Co. Ltd.
|
|
|
|
* Author: Jianqun <jay.xu@rock-chips.com>
|
|
|
|
* Copyright (c) 2015 Collabora Ltd.
|
|
|
|
* Author: Sjoerd Simons <sjoerd.simons@collabora.co.uk>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/delay.h>
|
|
|
|
#include <linux/clk.h>
|
|
|
|
#include <linux/pm_runtime.h>
|
|
|
|
#include <linux/mfd/syscon.h>
|
|
|
|
#include <linux/regmap.h>
|
|
|
|
#include <sound/pcm_params.h>
|
|
|
|
#include <sound/dmaengine_pcm.h>
|
|
|
|
|
|
|
|
#include "rockchip_spdif.h"
|
|
|
|
|
|
|
|
enum rk_spdif_type {
|
|
|
|
RK_SPDIF_RK3066,
|
|
|
|
RK_SPDIF_RK3188,
|
|
|
|
RK_SPDIF_RK3288,
|
2016-03-04 18:31:54 +08:00
|
|
|
RK_SPDIF_RK3366,
|
2015-10-08 21:31:13 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
#define RK3288_GRF_SOC_CON2 0x24c
|
|
|
|
|
|
|
|
struct rk_spdif_dev {
|
|
|
|
struct device *dev;
|
|
|
|
|
|
|
|
struct clk *mclk;
|
|
|
|
struct clk *hclk;
|
|
|
|
|
|
|
|
struct snd_dmaengine_dai_dma_data playback_dma_data;
|
|
|
|
|
|
|
|
struct regmap *regmap;
|
|
|
|
};
|
|
|
|
|
2020-11-26 00:44:24 +08:00
|
|
|
static const struct of_device_id rk_spdif_match[] __maybe_unused = {
|
2015-10-08 21:31:13 +08:00
|
|
|
{ .compatible = "rockchip,rk3066-spdif",
|
2016-03-04 18:31:54 +08:00
|
|
|
.data = (void *)RK_SPDIF_RK3066 },
|
2015-10-08 21:31:13 +08:00
|
|
|
{ .compatible = "rockchip,rk3188-spdif",
|
2016-03-04 18:31:54 +08:00
|
|
|
.data = (void *)RK_SPDIF_RK3188 },
|
2017-06-09 15:59:32 +08:00
|
|
|
{ .compatible = "rockchip,rk3228-spdif",
|
|
|
|
.data = (void *)RK_SPDIF_RK3366 },
|
2015-10-08 21:31:13 +08:00
|
|
|
{ .compatible = "rockchip,rk3288-spdif",
|
2016-03-04 18:31:54 +08:00
|
|
|
.data = (void *)RK_SPDIF_RK3288 },
|
2017-06-09 15:59:32 +08:00
|
|
|
{ .compatible = "rockchip,rk3328-spdif",
|
|
|
|
.data = (void *)RK_SPDIF_RK3366 },
|
2016-03-04 18:31:54 +08:00
|
|
|
{ .compatible = "rockchip,rk3366-spdif",
|
|
|
|
.data = (void *)RK_SPDIF_RK3366 },
|
|
|
|
{ .compatible = "rockchip,rk3368-spdif",
|
|
|
|
.data = (void *)RK_SPDIF_RK3366 },
|
|
|
|
{ .compatible = "rockchip,rk3399-spdif",
|
|
|
|
.data = (void *)RK_SPDIF_RK3366 },
|
2021-08-24 18:16:01 +08:00
|
|
|
{ .compatible = "rockchip,rk3568-spdif",
|
|
|
|
.data = (void *)RK_SPDIF_RK3366 },
|
2015-10-08 21:31:13 +08:00
|
|
|
{},
|
|
|
|
};
|
|
|
|
MODULE_DEVICE_TABLE(of, rk_spdif_match);
|
|
|
|
|
2016-03-02 23:59:06 +08:00
|
|
|
static int __maybe_unused rk_spdif_runtime_suspend(struct device *dev)
|
2015-10-08 21:31:13 +08:00
|
|
|
{
|
|
|
|
struct rk_spdif_dev *spdif = dev_get_drvdata(dev);
|
|
|
|
|
2016-09-07 14:30:21 +08:00
|
|
|
regcache_cache_only(spdif->regmap, true);
|
2015-10-08 21:31:13 +08:00
|
|
|
clk_disable_unprepare(spdif->mclk);
|
|
|
|
clk_disable_unprepare(spdif->hclk);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-03-02 23:59:06 +08:00
|
|
|
static int __maybe_unused rk_spdif_runtime_resume(struct device *dev)
|
2015-10-08 21:31:13 +08:00
|
|
|
{
|
|
|
|
struct rk_spdif_dev *spdif = dev_get_drvdata(dev);
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = clk_prepare_enable(spdif->mclk);
|
|
|
|
if (ret) {
|
|
|
|
dev_err(spdif->dev, "mclk clock enable failed %d\n", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = clk_prepare_enable(spdif->hclk);
|
|
|
|
if (ret) {
|
2022-12-08 14:39:00 +08:00
|
|
|
clk_disable_unprepare(spdif->mclk);
|
2015-10-08 21:31:13 +08:00
|
|
|
dev_err(spdif->dev, "hclk clock enable failed %d\n", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-09-07 14:30:21 +08:00
|
|
|
regcache_cache_only(spdif->regmap, false);
|
|
|
|
regcache_mark_dirty(spdif->regmap);
|
|
|
|
|
|
|
|
ret = regcache_sync(spdif->regmap);
|
|
|
|
if (ret) {
|
|
|
|
clk_disable_unprepare(spdif->mclk);
|
|
|
|
clk_disable_unprepare(spdif->hclk);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2015-10-08 21:31:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int rk_spdif_hw_params(struct snd_pcm_substream *substream,
|
2021-08-24 18:16:00 +08:00
|
|
|
struct snd_pcm_hw_params *params,
|
|
|
|
struct snd_soc_dai *dai)
|
2015-10-08 21:31:13 +08:00
|
|
|
{
|
|
|
|
struct rk_spdif_dev *spdif = snd_soc_dai_get_drvdata(dai);
|
|
|
|
unsigned int val = SPDIF_CFGR_HALFWORD_ENABLE;
|
|
|
|
int srate, mclk;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
srate = params_rate(params);
|
2016-07-18 22:34:34 +08:00
|
|
|
mclk = srate * 128;
|
2015-10-08 21:31:13 +08:00
|
|
|
|
|
|
|
switch (params_format(params)) {
|
|
|
|
case SNDRV_PCM_FORMAT_S16_LE:
|
|
|
|
val |= SPDIF_CFGR_VDW_16;
|
|
|
|
break;
|
|
|
|
case SNDRV_PCM_FORMAT_S20_3LE:
|
|
|
|
val |= SPDIF_CFGR_VDW_20;
|
|
|
|
break;
|
|
|
|
case SNDRV_PCM_FORMAT_S24_LE:
|
|
|
|
val |= SPDIF_CFGR_VDW_24;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set clock and calculate divider */
|
|
|
|
ret = clk_set_rate(spdif->mclk, mclk);
|
|
|
|
if (ret != 0) {
|
|
|
|
dev_err(spdif->dev, "Failed to set module clock rate: %d\n",
|
|
|
|
ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = regmap_update_bits(spdif->regmap, SPDIF_CFGR,
|
2021-08-24 18:16:00 +08:00
|
|
|
SPDIF_CFGR_CLK_DIV_MASK |
|
|
|
|
SPDIF_CFGR_HALFWORD_ENABLE |
|
|
|
|
SDPIF_CFGR_VDW_MASK, val);
|
2015-10-08 21:31:13 +08:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int rk_spdif_trigger(struct snd_pcm_substream *substream,
|
2021-08-24 18:16:00 +08:00
|
|
|
int cmd, struct snd_soc_dai *dai)
|
2015-10-08 21:31:13 +08:00
|
|
|
{
|
|
|
|
struct rk_spdif_dev *spdif = snd_soc_dai_get_drvdata(dai);
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
switch (cmd) {
|
|
|
|
case SNDRV_PCM_TRIGGER_START:
|
|
|
|
case SNDRV_PCM_TRIGGER_RESUME:
|
|
|
|
case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
|
|
|
|
ret = regmap_update_bits(spdif->regmap, SPDIF_DMACR,
|
2021-08-24 18:16:00 +08:00
|
|
|
SPDIF_DMACR_TDE_ENABLE |
|
|
|
|
SPDIF_DMACR_TDL_MASK,
|
|
|
|
SPDIF_DMACR_TDE_ENABLE |
|
|
|
|
SPDIF_DMACR_TDL(16));
|
2015-10-08 21:31:13 +08:00
|
|
|
|
|
|
|
if (ret != 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
ret = regmap_update_bits(spdif->regmap, SPDIF_XFER,
|
2021-08-24 18:16:00 +08:00
|
|
|
SPDIF_XFER_TXS_START,
|
|
|
|
SPDIF_XFER_TXS_START);
|
2015-10-08 21:31:13 +08:00
|
|
|
break;
|
|
|
|
case SNDRV_PCM_TRIGGER_SUSPEND:
|
|
|
|
case SNDRV_PCM_TRIGGER_STOP:
|
|
|
|
case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
|
|
|
|
ret = regmap_update_bits(spdif->regmap, SPDIF_DMACR,
|
2021-08-24 18:16:00 +08:00
|
|
|
SPDIF_DMACR_TDE_ENABLE,
|
|
|
|
SPDIF_DMACR_TDE_DISABLE);
|
2015-10-08 21:31:13 +08:00
|
|
|
|
|
|
|
if (ret != 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
ret = regmap_update_bits(spdif->regmap, SPDIF_XFER,
|
2021-08-24 18:16:00 +08:00
|
|
|
SPDIF_XFER_TXS_START,
|
|
|
|
SPDIF_XFER_TXS_STOP);
|
2015-10-08 21:31:13 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ret = -EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int rk_spdif_dai_probe(struct snd_soc_dai *dai)
|
|
|
|
{
|
|
|
|
struct rk_spdif_dev *spdif = snd_soc_dai_get_drvdata(dai);
|
|
|
|
|
2023-01-31 10:01:02 +08:00
|
|
|
snd_soc_dai_dma_data_set_playback(dai, &spdif->playback_dma_data);
|
2015-10-08 21:31:13 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct snd_soc_dai_ops rk_spdif_dai_ops = {
|
2023-08-09 06:57:58 +08:00
|
|
|
.probe = rk_spdif_dai_probe,
|
2015-10-08 21:31:13 +08:00
|
|
|
.hw_params = rk_spdif_hw_params,
|
|
|
|
.trigger = rk_spdif_trigger,
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct snd_soc_dai_driver rk_spdif_dai = {
|
|
|
|
.playback = {
|
|
|
|
.stream_name = "Playback",
|
|
|
|
.channels_min = 2,
|
|
|
|
.channels_max = 2,
|
|
|
|
.rates = (SNDRV_PCM_RATE_32000 |
|
|
|
|
SNDRV_PCM_RATE_44100 |
|
|
|
|
SNDRV_PCM_RATE_48000 |
|
|
|
|
SNDRV_PCM_RATE_96000 |
|
|
|
|
SNDRV_PCM_RATE_192000),
|
|
|
|
.formats = (SNDRV_PCM_FMTBIT_S16_LE |
|
|
|
|
SNDRV_PCM_FMTBIT_S20_3LE |
|
|
|
|
SNDRV_PCM_FMTBIT_S24_LE),
|
|
|
|
},
|
|
|
|
.ops = &rk_spdif_dai_ops,
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct snd_soc_component_driver rk_spdif_component = {
|
|
|
|
.name = "rockchip-spdif",
|
2022-06-23 20:51:40 +08:00
|
|
|
.legacy_dai_naming = 1,
|
2015-10-08 21:31:13 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
static bool rk_spdif_wr_reg(struct device *dev, unsigned int reg)
|
|
|
|
{
|
|
|
|
switch (reg) {
|
|
|
|
case SPDIF_CFGR:
|
|
|
|
case SPDIF_DMACR:
|
|
|
|
case SPDIF_INTCR:
|
|
|
|
case SPDIF_XFER:
|
|
|
|
case SPDIF_SMPDR:
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool rk_spdif_rd_reg(struct device *dev, unsigned int reg)
|
|
|
|
{
|
|
|
|
switch (reg) {
|
|
|
|
case SPDIF_CFGR:
|
|
|
|
case SPDIF_SDBLR:
|
|
|
|
case SPDIF_INTCR:
|
|
|
|
case SPDIF_INTSR:
|
|
|
|
case SPDIF_XFER:
|
2021-08-24 18:15:59 +08:00
|
|
|
case SPDIF_SMPDR:
|
2015-10-08 21:31:13 +08:00
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool rk_spdif_volatile_reg(struct device *dev, unsigned int reg)
|
|
|
|
{
|
|
|
|
switch (reg) {
|
|
|
|
case SPDIF_INTSR:
|
|
|
|
case SPDIF_SDBLR:
|
2021-08-24 18:15:59 +08:00
|
|
|
case SPDIF_SMPDR:
|
2015-10-08 21:31:13 +08:00
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct regmap_config rk_spdif_regmap_config = {
|
|
|
|
.reg_bits = 32,
|
|
|
|
.reg_stride = 4,
|
|
|
|
.val_bits = 32,
|
|
|
|
.max_register = SPDIF_SMPDR,
|
|
|
|
.writeable_reg = rk_spdif_wr_reg,
|
|
|
|
.readable_reg = rk_spdif_rd_reg,
|
|
|
|
.volatile_reg = rk_spdif_volatile_reg,
|
|
|
|
.cache_type = REGCACHE_FLAT,
|
|
|
|
};
|
|
|
|
|
|
|
|
static int rk_spdif_probe(struct platform_device *pdev)
|
|
|
|
{
|
|
|
|
struct device_node *np = pdev->dev.of_node;
|
|
|
|
struct rk_spdif_dev *spdif;
|
|
|
|
const struct of_device_id *match;
|
|
|
|
struct resource *res;
|
|
|
|
void __iomem *regs;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
match = of_match_node(rk_spdif_match, np);
|
2015-11-14 01:15:56 +08:00
|
|
|
if (match->data == (void *)RK_SPDIF_RK3288) {
|
2015-10-08 21:31:13 +08:00
|
|
|
struct regmap *grf;
|
|
|
|
|
|
|
|
grf = syscon_regmap_lookup_by_phandle(np, "rockchip,grf");
|
|
|
|
if (IS_ERR(grf)) {
|
|
|
|
dev_err(&pdev->dev,
|
2021-08-24 18:16:00 +08:00
|
|
|
"rockchip_spdif missing 'rockchip,grf'\n");
|
2015-10-08 21:31:13 +08:00
|
|
|
return PTR_ERR(grf);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Select the 8 channel SPDIF solution on RK3288 as
|
|
|
|
* the 2 channel one does not appear to work
|
|
|
|
*/
|
|
|
|
regmap_write(grf, RK3288_GRF_SOC_CON2, BIT(1) << 16);
|
|
|
|
}
|
|
|
|
|
|
|
|
spdif = devm_kzalloc(&pdev->dev, sizeof(*spdif), GFP_KERNEL);
|
|
|
|
if (!spdif)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
spdif->hclk = devm_clk_get(&pdev->dev, "hclk");
|
2020-07-13 18:26:00 +08:00
|
|
|
if (IS_ERR(spdif->hclk))
|
2015-10-08 21:31:13 +08:00
|
|
|
return PTR_ERR(spdif->hclk);
|
|
|
|
|
|
|
|
spdif->mclk = devm_clk_get(&pdev->dev, "mclk");
|
2020-07-13 18:26:00 +08:00
|
|
|
if (IS_ERR(spdif->mclk))
|
|
|
|
return PTR_ERR(spdif->mclk);
|
2015-10-08 21:31:13 +08:00
|
|
|
|
2021-06-15 22:15:02 +08:00
|
|
|
regs = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
|
2020-07-13 18:26:00 +08:00
|
|
|
if (IS_ERR(regs))
|
|
|
|
return PTR_ERR(regs);
|
2015-10-08 21:31:13 +08:00
|
|
|
|
|
|
|
spdif->regmap = devm_regmap_init_mmio_clk(&pdev->dev, "hclk", regs,
|
|
|
|
&rk_spdif_regmap_config);
|
2020-07-13 18:26:00 +08:00
|
|
|
if (IS_ERR(spdif->regmap))
|
|
|
|
return PTR_ERR(spdif->regmap);
|
2015-10-08 21:31:13 +08:00
|
|
|
|
|
|
|
spdif->playback_dma_data.addr = res->start + SPDIF_SMPDR;
|
|
|
|
spdif->playback_dma_data.addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
|
|
|
|
spdif->playback_dma_data.maxburst = 4;
|
|
|
|
|
|
|
|
spdif->dev = &pdev->dev;
|
|
|
|
dev_set_drvdata(&pdev->dev, spdif);
|
|
|
|
|
|
|
|
pm_runtime_enable(&pdev->dev);
|
2020-07-13 18:26:00 +08:00
|
|
|
if (!pm_runtime_enabled(&pdev->dev)) {
|
|
|
|
ret = rk_spdif_runtime_resume(&pdev->dev);
|
|
|
|
if (ret)
|
|
|
|
goto err_pm_runtime;
|
|
|
|
}
|
2015-10-08 21:31:13 +08:00
|
|
|
|
|
|
|
ret = devm_snd_soc_register_component(&pdev->dev,
|
|
|
|
&rk_spdif_component,
|
|
|
|
&rk_spdif_dai, 1);
|
|
|
|
if (ret) {
|
|
|
|
dev_err(&pdev->dev, "Could not register DAI\n");
|
2020-07-13 18:26:00 +08:00
|
|
|
goto err_pm_suspend;
|
2015-10-08 21:31:13 +08:00
|
|
|
}
|
|
|
|
|
2015-10-13 10:37:10 +08:00
|
|
|
ret = devm_snd_dmaengine_pcm_register(&pdev->dev, NULL, 0);
|
2015-10-08 21:31:13 +08:00
|
|
|
if (ret) {
|
|
|
|
dev_err(&pdev->dev, "Could not register PCM\n");
|
2020-07-13 18:26:00 +08:00
|
|
|
goto err_pm_suspend;
|
2015-10-08 21:31:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
2020-07-13 18:26:00 +08:00
|
|
|
err_pm_suspend:
|
|
|
|
if (!pm_runtime_status_suspended(&pdev->dev))
|
|
|
|
rk_spdif_runtime_suspend(&pdev->dev);
|
2015-10-08 21:31:13 +08:00
|
|
|
err_pm_runtime:
|
|
|
|
pm_runtime_disable(&pdev->dev);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2023-03-15 23:06:55 +08:00
|
|
|
static void rk_spdif_remove(struct platform_device *pdev)
|
2015-10-08 21:31:13 +08:00
|
|
|
{
|
|
|
|
pm_runtime_disable(&pdev->dev);
|
|
|
|
if (!pm_runtime_status_suspended(&pdev->dev))
|
|
|
|
rk_spdif_runtime_suspend(&pdev->dev);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct dev_pm_ops rk_spdif_pm_ops = {
|
|
|
|
SET_RUNTIME_PM_OPS(rk_spdif_runtime_suspend, rk_spdif_runtime_resume,
|
|
|
|
NULL)
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct platform_driver rk_spdif_driver = {
|
|
|
|
.probe = rk_spdif_probe,
|
2024-09-09 23:12:30 +08:00
|
|
|
.remove = rk_spdif_remove,
|
2015-10-08 21:31:13 +08:00
|
|
|
.driver = {
|
|
|
|
.name = "rockchip-spdif",
|
|
|
|
.of_match_table = of_match_ptr(rk_spdif_match),
|
|
|
|
.pm = &rk_spdif_pm_ops,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
module_platform_driver(rk_spdif_driver);
|
|
|
|
|
|
|
|
MODULE_ALIAS("platform:rockchip-spdif");
|
|
|
|
MODULE_DESCRIPTION("ROCKCHIP SPDIF transceiver Interface");
|
|
|
|
MODULE_AUTHOR("Sjoerd Simons <sjoerd.simons@collabora.co.uk>");
|
|
|
|
MODULE_LICENSE("GPL v2");
|