175a6faf6SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
239582322SDamien.Horsley /*
339582322SDamien.Horsley * IMG SPDIF output controller driver
439582322SDamien.Horsley *
539582322SDamien.Horsley * Copyright (C) 2015 Imagination Technologies Ltd.
639582322SDamien.Horsley *
739582322SDamien.Horsley * Author: Damien Horsley <Damien.Horsley@imgtec.com>
839582322SDamien.Horsley */
939582322SDamien.Horsley
1039582322SDamien.Horsley #include <linux/clk.h>
1139582322SDamien.Horsley #include <linux/init.h>
1239582322SDamien.Horsley #include <linux/kernel.h>
1339582322SDamien.Horsley #include <linux/module.h>
1439582322SDamien.Horsley #include <linux/of.h>
1539582322SDamien.Horsley #include <linux/platform_device.h>
1639582322SDamien.Horsley #include <linux/pm_runtime.h>
1739582322SDamien.Horsley #include <linux/reset.h>
1839582322SDamien.Horsley
1939582322SDamien.Horsley #include <sound/core.h>
2039582322SDamien.Horsley #include <sound/dmaengine_pcm.h>
2139582322SDamien.Horsley #include <sound/initval.h>
2239582322SDamien.Horsley #include <sound/pcm.h>
2339582322SDamien.Horsley #include <sound/pcm_params.h>
2439582322SDamien.Horsley #include <sound/soc.h>
2539582322SDamien.Horsley
2639582322SDamien.Horsley #define IMG_SPDIF_OUT_TX_FIFO 0x0
2739582322SDamien.Horsley
2839582322SDamien.Horsley #define IMG_SPDIF_OUT_CTL 0x4
2939582322SDamien.Horsley #define IMG_SPDIF_OUT_CTL_FS_MASK BIT(4)
3039582322SDamien.Horsley #define IMG_SPDIF_OUT_CTL_CLK_MASK BIT(2)
3139582322SDamien.Horsley #define IMG_SPDIF_OUT_CTL_SRT_MASK BIT(0)
3239582322SDamien.Horsley
3339582322SDamien.Horsley #define IMG_SPDIF_OUT_CSL 0x14
3439582322SDamien.Horsley
3539582322SDamien.Horsley #define IMG_SPDIF_OUT_CSH_UV 0x18
3639582322SDamien.Horsley #define IMG_SPDIF_OUT_CSH_UV_CSH_SHIFT 0
3739582322SDamien.Horsley #define IMG_SPDIF_OUT_CSH_UV_CSH_MASK 0xff
3839582322SDamien.Horsley
3939582322SDamien.Horsley struct img_spdif_out {
4039582322SDamien.Horsley spinlock_t lock;
4139582322SDamien.Horsley void __iomem *base;
4239582322SDamien.Horsley struct clk *clk_sys;
4339582322SDamien.Horsley struct clk *clk_ref;
4439582322SDamien.Horsley struct snd_dmaengine_dai_dma_data dma_data;
4539582322SDamien.Horsley struct device *dev;
4639582322SDamien.Horsley struct reset_control *rst;
47634a09d0SEd Blake u32 suspend_ctl;
48634a09d0SEd Blake u32 suspend_csl;
49634a09d0SEd Blake u32 suspend_csh;
5039582322SDamien.Horsley };
5139582322SDamien.Horsley
img_spdif_out_runtime_suspend(struct device * dev)522ab18dfeSEd Blake static int img_spdif_out_runtime_suspend(struct device *dev)
5339582322SDamien.Horsley {
5439582322SDamien.Horsley struct img_spdif_out *spdif = dev_get_drvdata(dev);
5539582322SDamien.Horsley
5639582322SDamien.Horsley clk_disable_unprepare(spdif->clk_ref);
57e8639d0cSEd Blake clk_disable_unprepare(spdif->clk_sys);
5839582322SDamien.Horsley
5939582322SDamien.Horsley return 0;
6039582322SDamien.Horsley }
6139582322SDamien.Horsley
img_spdif_out_runtime_resume(struct device * dev)622ab18dfeSEd Blake static int img_spdif_out_runtime_resume(struct device *dev)
6339582322SDamien.Horsley {
6439582322SDamien.Horsley struct img_spdif_out *spdif = dev_get_drvdata(dev);
6539582322SDamien.Horsley int ret;
6639582322SDamien.Horsley
67e8639d0cSEd Blake ret = clk_prepare_enable(spdif->clk_sys);
68e8639d0cSEd Blake if (ret) {
69e8639d0cSEd Blake dev_err(dev, "clk_enable failed: %d\n", ret);
70e8639d0cSEd Blake return ret;
71e8639d0cSEd Blake }
72e8639d0cSEd Blake
7339582322SDamien.Horsley ret = clk_prepare_enable(spdif->clk_ref);
7439582322SDamien.Horsley if (ret) {
7539582322SDamien.Horsley dev_err(dev, "clk_enable failed: %d\n", ret);
76e8639d0cSEd Blake clk_disable_unprepare(spdif->clk_sys);
7739582322SDamien.Horsley return ret;
7839582322SDamien.Horsley }
7939582322SDamien.Horsley
8039582322SDamien.Horsley return 0;
8139582322SDamien.Horsley }
8239582322SDamien.Horsley
img_spdif_out_writel(struct img_spdif_out * spdif,u32 val,u32 reg)8339582322SDamien.Horsley static inline void img_spdif_out_writel(struct img_spdif_out *spdif, u32 val,
8439582322SDamien.Horsley u32 reg)
8539582322SDamien.Horsley {
8639582322SDamien.Horsley writel(val, spdif->base + reg);
8739582322SDamien.Horsley }
8839582322SDamien.Horsley
img_spdif_out_readl(struct img_spdif_out * spdif,u32 reg)8939582322SDamien.Horsley static inline u32 img_spdif_out_readl(struct img_spdif_out *spdif, u32 reg)
9039582322SDamien.Horsley {
9139582322SDamien.Horsley return readl(spdif->base + reg);
9239582322SDamien.Horsley }
9339582322SDamien.Horsley
img_spdif_out_reset(struct img_spdif_out * spdif)9439582322SDamien.Horsley static void img_spdif_out_reset(struct img_spdif_out *spdif)
9539582322SDamien.Horsley {
9639582322SDamien.Horsley u32 ctl, status_low, status_high;
9739582322SDamien.Horsley
9839582322SDamien.Horsley ctl = img_spdif_out_readl(spdif, IMG_SPDIF_OUT_CTL) &
9939582322SDamien.Horsley ~IMG_SPDIF_OUT_CTL_SRT_MASK;
10039582322SDamien.Horsley status_low = img_spdif_out_readl(spdif, IMG_SPDIF_OUT_CSL);
10139582322SDamien.Horsley status_high = img_spdif_out_readl(spdif, IMG_SPDIF_OUT_CSH_UV);
10239582322SDamien.Horsley
10339582322SDamien.Horsley reset_control_assert(spdif->rst);
10439582322SDamien.Horsley reset_control_deassert(spdif->rst);
10539582322SDamien.Horsley
10639582322SDamien.Horsley img_spdif_out_writel(spdif, ctl, IMG_SPDIF_OUT_CTL);
10739582322SDamien.Horsley img_spdif_out_writel(spdif, status_low, IMG_SPDIF_OUT_CSL);
10839582322SDamien.Horsley img_spdif_out_writel(spdif, status_high, IMG_SPDIF_OUT_CSH_UV);
10939582322SDamien.Horsley }
11039582322SDamien.Horsley
img_spdif_out_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)11139582322SDamien.Horsley static int img_spdif_out_info(struct snd_kcontrol *kcontrol,
11239582322SDamien.Horsley struct snd_ctl_elem_info *uinfo)
11339582322SDamien.Horsley {
11439582322SDamien.Horsley uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
11539582322SDamien.Horsley uinfo->count = 1;
11639582322SDamien.Horsley
11739582322SDamien.Horsley return 0;
11839582322SDamien.Horsley }
11939582322SDamien.Horsley
img_spdif_out_get_status_mask(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)12039582322SDamien.Horsley static int img_spdif_out_get_status_mask(struct snd_kcontrol *kcontrol,
12139582322SDamien.Horsley struct snd_ctl_elem_value *ucontrol)
12239582322SDamien.Horsley {
12339582322SDamien.Horsley ucontrol->value.iec958.status[0] = 0xff;
12439582322SDamien.Horsley ucontrol->value.iec958.status[1] = 0xff;
12539582322SDamien.Horsley ucontrol->value.iec958.status[2] = 0xff;
12639582322SDamien.Horsley ucontrol->value.iec958.status[3] = 0xff;
12739582322SDamien.Horsley ucontrol->value.iec958.status[4] = 0xff;
12839582322SDamien.Horsley
12939582322SDamien.Horsley return 0;
13039582322SDamien.Horsley }
13139582322SDamien.Horsley
img_spdif_out_get_status(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)13239582322SDamien.Horsley static int img_spdif_out_get_status(struct snd_kcontrol *kcontrol,
13339582322SDamien.Horsley struct snd_ctl_elem_value *ucontrol)
13439582322SDamien.Horsley {
13539582322SDamien.Horsley struct snd_soc_dai *cpu_dai = snd_kcontrol_chip(kcontrol);
13639582322SDamien.Horsley struct img_spdif_out *spdif = snd_soc_dai_get_drvdata(cpu_dai);
13739582322SDamien.Horsley u32 reg;
13839582322SDamien.Horsley unsigned long flags;
13939582322SDamien.Horsley
14039582322SDamien.Horsley spin_lock_irqsave(&spdif->lock, flags);
14139582322SDamien.Horsley
14239582322SDamien.Horsley reg = img_spdif_out_readl(spdif, IMG_SPDIF_OUT_CSL);
14339582322SDamien.Horsley ucontrol->value.iec958.status[0] = reg & 0xff;
14439582322SDamien.Horsley ucontrol->value.iec958.status[1] = (reg >> 8) & 0xff;
14539582322SDamien.Horsley ucontrol->value.iec958.status[2] = (reg >> 16) & 0xff;
14639582322SDamien.Horsley ucontrol->value.iec958.status[3] = (reg >> 24) & 0xff;
14739582322SDamien.Horsley
14839582322SDamien.Horsley reg = img_spdif_out_readl(spdif, IMG_SPDIF_OUT_CSH_UV);
14939582322SDamien.Horsley ucontrol->value.iec958.status[4] =
15039582322SDamien.Horsley (reg & IMG_SPDIF_OUT_CSH_UV_CSH_MASK) >>
15139582322SDamien.Horsley IMG_SPDIF_OUT_CSH_UV_CSH_SHIFT;
15239582322SDamien.Horsley
15339582322SDamien.Horsley spin_unlock_irqrestore(&spdif->lock, flags);
15439582322SDamien.Horsley
15539582322SDamien.Horsley return 0;
15639582322SDamien.Horsley }
15739582322SDamien.Horsley
img_spdif_out_set_status(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)15839582322SDamien.Horsley static int img_spdif_out_set_status(struct snd_kcontrol *kcontrol,
15939582322SDamien.Horsley struct snd_ctl_elem_value *ucontrol)
16039582322SDamien.Horsley {
16139582322SDamien.Horsley struct snd_soc_dai *cpu_dai = snd_kcontrol_chip(kcontrol);
16239582322SDamien.Horsley struct img_spdif_out *spdif = snd_soc_dai_get_drvdata(cpu_dai);
16339582322SDamien.Horsley u32 reg;
16439582322SDamien.Horsley unsigned long flags;
16539582322SDamien.Horsley
16639582322SDamien.Horsley reg = ((u32)ucontrol->value.iec958.status[3] << 24);
16739582322SDamien.Horsley reg |= ((u32)ucontrol->value.iec958.status[2] << 16);
16839582322SDamien.Horsley reg |= ((u32)ucontrol->value.iec958.status[1] << 8);
16939582322SDamien.Horsley reg |= (u32)ucontrol->value.iec958.status[0];
17039582322SDamien.Horsley
17139582322SDamien.Horsley spin_lock_irqsave(&spdif->lock, flags);
17239582322SDamien.Horsley
17339582322SDamien.Horsley img_spdif_out_writel(spdif, reg, IMG_SPDIF_OUT_CSL);
17439582322SDamien.Horsley
17539582322SDamien.Horsley reg = img_spdif_out_readl(spdif, IMG_SPDIF_OUT_CSH_UV);
17639582322SDamien.Horsley reg &= ~IMG_SPDIF_OUT_CSH_UV_CSH_MASK;
17739582322SDamien.Horsley reg |= (u32)ucontrol->value.iec958.status[4] <<
17839582322SDamien.Horsley IMG_SPDIF_OUT_CSH_UV_CSH_SHIFT;
17939582322SDamien.Horsley img_spdif_out_writel(spdif, reg, IMG_SPDIF_OUT_CSH_UV);
18039582322SDamien.Horsley
18139582322SDamien.Horsley spin_unlock_irqrestore(&spdif->lock, flags);
18239582322SDamien.Horsley
18339582322SDamien.Horsley return 0;
18439582322SDamien.Horsley }
18539582322SDamien.Horsley
18639582322SDamien.Horsley static struct snd_kcontrol_new img_spdif_out_controls[] = {
18739582322SDamien.Horsley {
18839582322SDamien.Horsley .access = SNDRV_CTL_ELEM_ACCESS_READ,
18939582322SDamien.Horsley .iface = SNDRV_CTL_ELEM_IFACE_PCM,
19039582322SDamien.Horsley .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, MASK),
19139582322SDamien.Horsley .info = img_spdif_out_info,
19239582322SDamien.Horsley .get = img_spdif_out_get_status_mask
19339582322SDamien.Horsley },
19439582322SDamien.Horsley {
19539582322SDamien.Horsley .iface = SNDRV_CTL_ELEM_IFACE_PCM,
19639582322SDamien.Horsley .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
19739582322SDamien.Horsley .info = img_spdif_out_info,
19839582322SDamien.Horsley .get = img_spdif_out_get_status,
19939582322SDamien.Horsley .put = img_spdif_out_set_status
20039582322SDamien.Horsley }
20139582322SDamien.Horsley };
20239582322SDamien.Horsley
img_spdif_out_trigger(struct snd_pcm_substream * substream,int cmd,struct snd_soc_dai * dai)20339582322SDamien.Horsley static int img_spdif_out_trigger(struct snd_pcm_substream *substream, int cmd,
20439582322SDamien.Horsley struct snd_soc_dai *dai)
20539582322SDamien.Horsley {
20639582322SDamien.Horsley struct img_spdif_out *spdif = snd_soc_dai_get_drvdata(dai);
20739582322SDamien.Horsley u32 reg;
20839582322SDamien.Horsley unsigned long flags;
20939582322SDamien.Horsley
21039582322SDamien.Horsley switch (cmd) {
21139582322SDamien.Horsley case SNDRV_PCM_TRIGGER_START:
21239582322SDamien.Horsley case SNDRV_PCM_TRIGGER_RESUME:
21339582322SDamien.Horsley case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
21439582322SDamien.Horsley reg = img_spdif_out_readl(spdif, IMG_SPDIF_OUT_CTL);
21539582322SDamien.Horsley reg |= IMG_SPDIF_OUT_CTL_SRT_MASK;
21639582322SDamien.Horsley img_spdif_out_writel(spdif, reg, IMG_SPDIF_OUT_CTL);
21739582322SDamien.Horsley break;
21839582322SDamien.Horsley case SNDRV_PCM_TRIGGER_STOP:
21939582322SDamien.Horsley case SNDRV_PCM_TRIGGER_SUSPEND:
22039582322SDamien.Horsley case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
22139582322SDamien.Horsley spin_lock_irqsave(&spdif->lock, flags);
22239582322SDamien.Horsley img_spdif_out_reset(spdif);
22339582322SDamien.Horsley spin_unlock_irqrestore(&spdif->lock, flags);
22439582322SDamien.Horsley break;
22539582322SDamien.Horsley default:
22639582322SDamien.Horsley return -EINVAL;
22739582322SDamien.Horsley }
22839582322SDamien.Horsley
22939582322SDamien.Horsley return 0;
23039582322SDamien.Horsley }
23139582322SDamien.Horsley
img_spdif_out_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params,struct snd_soc_dai * dai)23239582322SDamien.Horsley static int img_spdif_out_hw_params(struct snd_pcm_substream *substream,
23339582322SDamien.Horsley struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
23439582322SDamien.Horsley {
23539582322SDamien.Horsley struct img_spdif_out *spdif = snd_soc_dai_get_drvdata(dai);
23639582322SDamien.Horsley unsigned int channels;
23739582322SDamien.Horsley long pre_div_a, pre_div_b, diff_a, diff_b, rate, clk_rate;
23839582322SDamien.Horsley u32 reg;
23939582322SDamien.Horsley snd_pcm_format_t format;
24039582322SDamien.Horsley
24139582322SDamien.Horsley rate = params_rate(params);
24239582322SDamien.Horsley format = params_format(params);
24339582322SDamien.Horsley channels = params_channels(params);
24439582322SDamien.Horsley
24539582322SDamien.Horsley dev_dbg(spdif->dev, "hw_params rate %ld channels %u format %u\n",
24639582322SDamien.Horsley rate, channels, format);
24739582322SDamien.Horsley
24839582322SDamien.Horsley if (format != SNDRV_PCM_FORMAT_S32_LE)
24939582322SDamien.Horsley return -EINVAL;
25039582322SDamien.Horsley
25139582322SDamien.Horsley if (channels != 2)
25239582322SDamien.Horsley return -EINVAL;
25339582322SDamien.Horsley
25439582322SDamien.Horsley pre_div_a = clk_round_rate(spdif->clk_ref, rate * 256);
25539582322SDamien.Horsley if (pre_div_a < 0)
25639582322SDamien.Horsley return pre_div_a;
25739582322SDamien.Horsley pre_div_b = clk_round_rate(spdif->clk_ref, rate * 384);
25839582322SDamien.Horsley if (pre_div_b < 0)
25939582322SDamien.Horsley return pre_div_b;
26039582322SDamien.Horsley
26139582322SDamien.Horsley diff_a = abs((pre_div_a / 256) - rate);
26239582322SDamien.Horsley diff_b = abs((pre_div_b / 384) - rate);
26339582322SDamien.Horsley
26439582322SDamien.Horsley /* If diffs are equal, use lower clock rate */
26539582322SDamien.Horsley if (diff_a > diff_b)
26639582322SDamien.Horsley clk_set_rate(spdif->clk_ref, pre_div_b);
26739582322SDamien.Horsley else
26839582322SDamien.Horsley clk_set_rate(spdif->clk_ref, pre_div_a);
26939582322SDamien.Horsley
27039582322SDamien.Horsley /*
27139582322SDamien.Horsley * Another driver (eg machine driver) may have rejected the above
27239582322SDamien.Horsley * change. Get the current rate and set the register bit according to
27339582322SDamien.Horsley * the new min diff
27439582322SDamien.Horsley */
27539582322SDamien.Horsley clk_rate = clk_get_rate(spdif->clk_ref);
27639582322SDamien.Horsley
27739582322SDamien.Horsley diff_a = abs((clk_rate / 256) - rate);
27839582322SDamien.Horsley diff_b = abs((clk_rate / 384) - rate);
27939582322SDamien.Horsley
28039582322SDamien.Horsley reg = img_spdif_out_readl(spdif, IMG_SPDIF_OUT_CTL);
28139582322SDamien.Horsley if (diff_a <= diff_b)
28239582322SDamien.Horsley reg &= ~IMG_SPDIF_OUT_CTL_CLK_MASK;
28339582322SDamien.Horsley else
28439582322SDamien.Horsley reg |= IMG_SPDIF_OUT_CTL_CLK_MASK;
28539582322SDamien.Horsley img_spdif_out_writel(spdif, reg, IMG_SPDIF_OUT_CTL);
28639582322SDamien.Horsley
28739582322SDamien.Horsley return 0;
28839582322SDamien.Horsley }
28939582322SDamien.Horsley
img_spdif_out_dai_probe(struct snd_soc_dai * dai)29039582322SDamien.Horsley static int img_spdif_out_dai_probe(struct snd_soc_dai *dai)
29139582322SDamien.Horsley {
29239582322SDamien.Horsley struct img_spdif_out *spdif = snd_soc_dai_get_drvdata(dai);
29339582322SDamien.Horsley
29439582322SDamien.Horsley snd_soc_dai_init_dma_data(dai, &spdif->dma_data, NULL);
29539582322SDamien.Horsley
29639582322SDamien.Horsley snd_soc_add_dai_controls(dai, img_spdif_out_controls,
29739582322SDamien.Horsley ARRAY_SIZE(img_spdif_out_controls));
29839582322SDamien.Horsley
29939582322SDamien.Horsley return 0;
30039582322SDamien.Horsley }
30139582322SDamien.Horsley
302ca6b2aacSKuninori Morimoto static const struct snd_soc_dai_ops img_spdif_out_dai_ops = {
30339582322SDamien.Horsley .probe = img_spdif_out_dai_probe,
304ca6b2aacSKuninori Morimoto .trigger = img_spdif_out_trigger,
305ca6b2aacSKuninori Morimoto .hw_params = img_spdif_out_hw_params
306ca6b2aacSKuninori Morimoto };
307ca6b2aacSKuninori Morimoto
308ca6b2aacSKuninori Morimoto static struct snd_soc_dai_driver img_spdif_out_dai = {
30939582322SDamien.Horsley .playback = {
31039582322SDamien.Horsley .channels_min = 2,
31139582322SDamien.Horsley .channels_max = 2,
31239582322SDamien.Horsley .rates = SNDRV_PCM_RATE_8000_192000,
31339582322SDamien.Horsley .formats = SNDRV_PCM_FMTBIT_S32_LE
31439582322SDamien.Horsley },
31539582322SDamien.Horsley .ops = &img_spdif_out_dai_ops
31639582322SDamien.Horsley };
31739582322SDamien.Horsley
31839582322SDamien.Horsley static const struct snd_soc_component_driver img_spdif_out_component = {
3195f9d6998SCharles Keepax .name = "img-spdif-out",
3205f9d6998SCharles Keepax .legacy_dai_naming = 1,
32139582322SDamien.Horsley };
32239582322SDamien.Horsley
img_spdif_out_probe(struct platform_device * pdev)32339582322SDamien.Horsley static int img_spdif_out_probe(struct platform_device *pdev)
32439582322SDamien.Horsley {
32539582322SDamien.Horsley struct img_spdif_out *spdif;
32639582322SDamien.Horsley struct resource *res;
32739582322SDamien.Horsley void __iomem *base;
32839582322SDamien.Horsley int ret;
32939582322SDamien.Horsley struct device *dev = &pdev->dev;
33039582322SDamien.Horsley
33139582322SDamien.Horsley spdif = devm_kzalloc(&pdev->dev, sizeof(*spdif), GFP_KERNEL);
33239582322SDamien.Horsley if (!spdif)
33339582322SDamien.Horsley return -ENOMEM;
33439582322SDamien.Horsley
33539582322SDamien.Horsley platform_set_drvdata(pdev, spdif);
33639582322SDamien.Horsley
33739582322SDamien.Horsley spdif->dev = &pdev->dev;
33839582322SDamien.Horsley
339942f2671SYang Yingliang base = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
34039582322SDamien.Horsley if (IS_ERR(base))
34139582322SDamien.Horsley return PTR_ERR(base);
34239582322SDamien.Horsley
34339582322SDamien.Horsley spdif->base = base;
34439582322SDamien.Horsley
3453a4e1b93SPhilipp Zabel spdif->rst = devm_reset_control_get_exclusive(&pdev->dev, "rst");
346ef12f373SKuninori Morimoto if (IS_ERR(spdif->rst))
347ef12f373SKuninori Morimoto return dev_err_probe(&pdev->dev, PTR_ERR(spdif->rst),
348ef12f373SKuninori Morimoto "No top level reset found\n");
34939582322SDamien.Horsley
35039582322SDamien.Horsley spdif->clk_sys = devm_clk_get(&pdev->dev, "sys");
351ef12f373SKuninori Morimoto if (IS_ERR(spdif->clk_sys))
352ef12f373SKuninori Morimoto return dev_err_probe(dev, PTR_ERR(spdif->clk_sys),
353ef12f373SKuninori Morimoto "Failed to acquire clock 'sys'\n");
35439582322SDamien.Horsley
35539582322SDamien.Horsley spdif->clk_ref = devm_clk_get(&pdev->dev, "ref");
356ef12f373SKuninori Morimoto if (IS_ERR(spdif->clk_ref))
357ef12f373SKuninori Morimoto return dev_err_probe(dev, PTR_ERR(spdif->clk_ref),
358ef12f373SKuninori Morimoto "Failed to acquire clock 'ref'\n");
35939582322SDamien.Horsley
36039582322SDamien.Horsley pm_runtime_enable(&pdev->dev);
36139582322SDamien.Horsley if (!pm_runtime_enabled(&pdev->dev)) {
3622ab18dfeSEd Blake ret = img_spdif_out_runtime_resume(&pdev->dev);
36339582322SDamien.Horsley if (ret)
36439582322SDamien.Horsley goto err_pm_disable;
36539582322SDamien.Horsley }
366b1d1b025SMinghao Chi ret = pm_runtime_resume_and_get(&pdev->dev);
367b1d1b025SMinghao Chi if (ret < 0)
368e8639d0cSEd Blake goto err_suspend;
369e8639d0cSEd Blake
370e8639d0cSEd Blake img_spdif_out_writel(spdif, IMG_SPDIF_OUT_CTL_FS_MASK,
371e8639d0cSEd Blake IMG_SPDIF_OUT_CTL);
372e8639d0cSEd Blake
373e8639d0cSEd Blake img_spdif_out_reset(spdif);
374e8639d0cSEd Blake pm_runtime_put(&pdev->dev);
37539582322SDamien.Horsley
37639582322SDamien.Horsley spin_lock_init(&spdif->lock);
37739582322SDamien.Horsley
37839582322SDamien.Horsley spdif->dma_data.addr = res->start + IMG_SPDIF_OUT_TX_FIFO;
37939582322SDamien.Horsley spdif->dma_data.addr_width = 4;
38039582322SDamien.Horsley spdif->dma_data.maxburst = 4;
38139582322SDamien.Horsley
38239582322SDamien.Horsley ret = devm_snd_soc_register_component(&pdev->dev,
38339582322SDamien.Horsley &img_spdif_out_component,
38439582322SDamien.Horsley &img_spdif_out_dai, 1);
38539582322SDamien.Horsley if (ret)
38639582322SDamien.Horsley goto err_suspend;
38739582322SDamien.Horsley
38839582322SDamien.Horsley ret = devm_snd_dmaengine_pcm_register(&pdev->dev, NULL, 0);
38939582322SDamien.Horsley if (ret)
39039582322SDamien.Horsley goto err_suspend;
39139582322SDamien.Horsley
39239582322SDamien.Horsley dev_dbg(&pdev->dev, "Probe successful\n");
39339582322SDamien.Horsley
39439582322SDamien.Horsley return 0;
39539582322SDamien.Horsley
39639582322SDamien.Horsley err_suspend:
39739582322SDamien.Horsley if (!pm_runtime_status_suspended(&pdev->dev))
3982ab18dfeSEd Blake img_spdif_out_runtime_suspend(&pdev->dev);
39939582322SDamien.Horsley err_pm_disable:
40039582322SDamien.Horsley pm_runtime_disable(&pdev->dev);
40139582322SDamien.Horsley
40239582322SDamien.Horsley return ret;
40339582322SDamien.Horsley }
40439582322SDamien.Horsley
img_spdif_out_dev_remove(struct platform_device * pdev)4054e894b2eSUwe Kleine-König static void img_spdif_out_dev_remove(struct platform_device *pdev)
40639582322SDamien.Horsley {
40739582322SDamien.Horsley pm_runtime_disable(&pdev->dev);
40839582322SDamien.Horsley if (!pm_runtime_status_suspended(&pdev->dev))
4092ab18dfeSEd Blake img_spdif_out_runtime_suspend(&pdev->dev);
41039582322SDamien.Horsley }
41139582322SDamien.Horsley
412634a09d0SEd Blake #ifdef CONFIG_PM_SLEEP
img_spdif_out_suspend(struct device * dev)413634a09d0SEd Blake static int img_spdif_out_suspend(struct device *dev)
414634a09d0SEd Blake {
415634a09d0SEd Blake struct img_spdif_out *spdif = dev_get_drvdata(dev);
416634a09d0SEd Blake int ret;
417634a09d0SEd Blake
418634a09d0SEd Blake if (pm_runtime_status_suspended(dev)) {
419634a09d0SEd Blake ret = img_spdif_out_runtime_resume(dev);
420634a09d0SEd Blake if (ret)
421634a09d0SEd Blake return ret;
422634a09d0SEd Blake }
423634a09d0SEd Blake
424634a09d0SEd Blake spdif->suspend_ctl = img_spdif_out_readl(spdif, IMG_SPDIF_OUT_CTL);
425634a09d0SEd Blake spdif->suspend_csl = img_spdif_out_readl(spdif, IMG_SPDIF_OUT_CSL);
426634a09d0SEd Blake spdif->suspend_csh = img_spdif_out_readl(spdif, IMG_SPDIF_OUT_CSH_UV);
427634a09d0SEd Blake
428634a09d0SEd Blake img_spdif_out_runtime_suspend(dev);
429634a09d0SEd Blake
430634a09d0SEd Blake return 0;
431634a09d0SEd Blake }
432634a09d0SEd Blake
img_spdif_out_resume(struct device * dev)433634a09d0SEd Blake static int img_spdif_out_resume(struct device *dev)
434634a09d0SEd Blake {
435634a09d0SEd Blake struct img_spdif_out *spdif = dev_get_drvdata(dev);
436634a09d0SEd Blake int ret;
437634a09d0SEd Blake
438634a09d0SEd Blake ret = img_spdif_out_runtime_resume(dev);
439634a09d0SEd Blake if (ret)
440634a09d0SEd Blake return ret;
441634a09d0SEd Blake
442634a09d0SEd Blake img_spdif_out_writel(spdif, spdif->suspend_ctl, IMG_SPDIF_OUT_CTL);
443634a09d0SEd Blake img_spdif_out_writel(spdif, spdif->suspend_csl, IMG_SPDIF_OUT_CSL);
444634a09d0SEd Blake img_spdif_out_writel(spdif, spdif->suspend_csh, IMG_SPDIF_OUT_CSH_UV);
445634a09d0SEd Blake
446634a09d0SEd Blake if (pm_runtime_status_suspended(dev))
447634a09d0SEd Blake img_spdif_out_runtime_suspend(dev);
448634a09d0SEd Blake
449634a09d0SEd Blake return 0;
450634a09d0SEd Blake }
451634a09d0SEd Blake #endif
45239582322SDamien.Horsley static const struct of_device_id img_spdif_out_of_match[] = {
45339582322SDamien.Horsley { .compatible = "img,spdif-out" },
45439582322SDamien.Horsley {}
45539582322SDamien.Horsley };
45639582322SDamien.Horsley MODULE_DEVICE_TABLE(of, img_spdif_out_of_match);
45739582322SDamien.Horsley
45839582322SDamien.Horsley static const struct dev_pm_ops img_spdif_out_pm_ops = {
4592ab18dfeSEd Blake SET_RUNTIME_PM_OPS(img_spdif_out_runtime_suspend,
4602ab18dfeSEd Blake img_spdif_out_runtime_resume, NULL)
461634a09d0SEd Blake SET_SYSTEM_SLEEP_PM_OPS(img_spdif_out_suspend, img_spdif_out_resume)
46239582322SDamien.Horsley };
46339582322SDamien.Horsley
46439582322SDamien.Horsley static struct platform_driver img_spdif_out_driver = {
46539582322SDamien.Horsley .driver = {
46639582322SDamien.Horsley .name = "img-spdif-out",
46739582322SDamien.Horsley .of_match_table = img_spdif_out_of_match,
46839582322SDamien.Horsley .pm = &img_spdif_out_pm_ops
46939582322SDamien.Horsley },
47039582322SDamien.Horsley .probe = img_spdif_out_probe,
471*130af75bSUwe Kleine-König .remove = img_spdif_out_dev_remove
47239582322SDamien.Horsley };
47339582322SDamien.Horsley module_platform_driver(img_spdif_out_driver);
47439582322SDamien.Horsley
47539582322SDamien.Horsley MODULE_AUTHOR("Damien Horsley <Damien.Horsley@imgtec.com>");
47639582322SDamien.Horsley MODULE_DESCRIPTION("IMG SPDIF Output driver");
47739582322SDamien.Horsley MODULE_LICENSE("GPL v2");
478