1 // SPDX-License-Identifier: GPL-2.0-only
3 * IMG parallel output controller driver
5 * Copyright (C) 2015 Imagination Technologies Ltd.
7 * Author: Damien Horsley <Damien.Horsley@imgtec.com>
10 #include <linux/clk.h>
11 #include <linux/init.h>
12 #include <linux/kernel.h>
13 #include <linux/module.h>
15 #include <linux/platform_device.h>
16 #include <linux/pm_runtime.h>
17 #include <linux/reset.h>
19 #include <sound/core.h>
20 #include <sound/dmaengine_pcm.h>
21 #include <sound/initval.h>
22 #include <sound/pcm.h>
23 #include <sound/pcm_params.h>
24 #include <sound/soc.h>
26 #define IMG_PRL_OUT_TX_FIFO 0
28 #define IMG_PRL_OUT_CTL 0x4
29 #define IMG_PRL_OUT_CTL_CH_MASK BIT(4)
30 #define IMG_PRL_OUT_CTL_PACKH_MASK BIT(3)
31 #define IMG_PRL_OUT_CTL_EDGE_MASK BIT(2)
32 #define IMG_PRL_OUT_CTL_ME_MASK BIT(1)
33 #define IMG_PRL_OUT_CTL_SRST_MASK BIT(0)
39 struct snd_dmaengine_dai_dma_data dma_data
;
41 struct reset_control
*rst
;
44 static int img_prl_out_suspend(struct device
*dev
)
46 struct img_prl_out
*prl
= dev_get_drvdata(dev
);
48 clk_disable_unprepare(prl
->clk_ref
);
53 static int img_prl_out_resume(struct device
*dev
)
55 struct img_prl_out
*prl
= dev_get_drvdata(dev
);
58 ret
= clk_prepare_enable(prl
->clk_ref
);
60 dev_err(dev
, "clk_enable failed: %d\n", ret
);
67 static inline void img_prl_out_writel(struct img_prl_out
*prl
,
70 writel(val
, prl
->base
+ reg
);
73 static inline u32
img_prl_out_readl(struct img_prl_out
*prl
, u32 reg
)
75 return readl(prl
->base
+ reg
);
78 static void img_prl_out_reset(struct img_prl_out
*prl
)
82 ctl
= img_prl_out_readl(prl
, IMG_PRL_OUT_CTL
) &
83 ~IMG_PRL_OUT_CTL_ME_MASK
;
85 reset_control_assert(prl
->rst
);
86 reset_control_deassert(prl
->rst
);
88 img_prl_out_writel(prl
, ctl
, IMG_PRL_OUT_CTL
);
91 static int img_prl_out_trigger(struct snd_pcm_substream
*substream
, int cmd
,
92 struct snd_soc_dai
*dai
)
94 struct img_prl_out
*prl
= snd_soc_dai_get_drvdata(dai
);
98 case SNDRV_PCM_TRIGGER_START
:
99 case SNDRV_PCM_TRIGGER_RESUME
:
100 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
101 reg
= img_prl_out_readl(prl
, IMG_PRL_OUT_CTL
);
102 reg
|= IMG_PRL_OUT_CTL_ME_MASK
;
103 img_prl_out_writel(prl
, reg
, IMG_PRL_OUT_CTL
);
105 case SNDRV_PCM_TRIGGER_STOP
:
106 case SNDRV_PCM_TRIGGER_SUSPEND
:
107 case SNDRV_PCM_TRIGGER_PAUSE_PUSH
:
108 img_prl_out_reset(prl
);
117 static int img_prl_out_hw_params(struct snd_pcm_substream
*substream
,
118 struct snd_pcm_hw_params
*params
, struct snd_soc_dai
*dai
)
120 struct img_prl_out
*prl
= snd_soc_dai_get_drvdata(dai
);
121 unsigned int rate
, channels
;
122 u32 reg
, control_set
= 0;
124 rate
= params_rate(params
);
125 channels
= params_channels(params
);
127 switch (params_format(params
)) {
128 case SNDRV_PCM_FORMAT_S32_LE
:
129 control_set
|= IMG_PRL_OUT_CTL_PACKH_MASK
;
131 case SNDRV_PCM_FORMAT_S24_LE
:
140 clk_set_rate(prl
->clk_ref
, rate
* 256);
142 reg
= img_prl_out_readl(prl
, IMG_PRL_OUT_CTL
);
143 reg
= (reg
& ~IMG_PRL_OUT_CTL_PACKH_MASK
) | control_set
;
144 img_prl_out_writel(prl
, reg
, IMG_PRL_OUT_CTL
);
149 static int img_prl_out_set_fmt(struct snd_soc_dai
*dai
, unsigned int fmt
)
151 struct img_prl_out
*prl
= snd_soc_dai_get_drvdata(dai
);
152 u32 reg
, control_set
= 0;
155 switch (fmt
& SND_SOC_DAIFMT_INV_MASK
) {
156 case SND_SOC_DAIFMT_NB_NF
:
158 case SND_SOC_DAIFMT_NB_IF
:
159 control_set
|= IMG_PRL_OUT_CTL_EDGE_MASK
;
165 ret
= pm_runtime_get_sync(prl
->dev
);
169 reg
= img_prl_out_readl(prl
, IMG_PRL_OUT_CTL
);
170 reg
= (reg
& ~IMG_PRL_OUT_CTL_EDGE_MASK
) | control_set
;
171 img_prl_out_writel(prl
, reg
, IMG_PRL_OUT_CTL
);
172 pm_runtime_put(prl
->dev
);
177 static const struct snd_soc_dai_ops img_prl_out_dai_ops
= {
178 .trigger
= img_prl_out_trigger
,
179 .hw_params
= img_prl_out_hw_params
,
180 .set_fmt
= img_prl_out_set_fmt
183 static int img_prl_out_dai_probe(struct snd_soc_dai
*dai
)
185 struct img_prl_out
*prl
= snd_soc_dai_get_drvdata(dai
);
187 snd_soc_dai_init_dma_data(dai
, &prl
->dma_data
, NULL
);
192 static struct snd_soc_dai_driver img_prl_out_dai
= {
193 .probe
= img_prl_out_dai_probe
,
197 .rates
= SNDRV_PCM_RATE_8000_192000
,
198 .formats
= SNDRV_PCM_FMTBIT_S32_LE
| SNDRV_PCM_FMTBIT_S24_LE
200 .ops
= &img_prl_out_dai_ops
203 static const struct snd_soc_component_driver img_prl_out_component
= {
204 .name
= "img-prl-out"
207 static int img_prl_out_probe(struct platform_device
*pdev
)
209 struct img_prl_out
*prl
;
210 struct resource
*res
;
213 struct device
*dev
= &pdev
->dev
;
215 prl
= devm_kzalloc(&pdev
->dev
, sizeof(*prl
), GFP_KERNEL
);
219 platform_set_drvdata(pdev
, prl
);
221 prl
->dev
= &pdev
->dev
;
223 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
224 base
= devm_ioremap_resource(&pdev
->dev
, res
);
226 return PTR_ERR(base
);
230 prl
->rst
= devm_reset_control_get_exclusive(&pdev
->dev
, "rst");
231 if (IS_ERR(prl
->rst
)) {
232 if (PTR_ERR(prl
->rst
) != -EPROBE_DEFER
)
233 dev_err(&pdev
->dev
, "No top level reset found\n");
234 return PTR_ERR(prl
->rst
);
237 prl
->clk_sys
= devm_clk_get(&pdev
->dev
, "sys");
238 if (IS_ERR(prl
->clk_sys
)) {
239 if (PTR_ERR(prl
->clk_sys
) != -EPROBE_DEFER
)
240 dev_err(dev
, "Failed to acquire clock 'sys'\n");
241 return PTR_ERR(prl
->clk_sys
);
244 prl
->clk_ref
= devm_clk_get(&pdev
->dev
, "ref");
245 if (IS_ERR(prl
->clk_ref
)) {
246 if (PTR_ERR(prl
->clk_ref
) != -EPROBE_DEFER
)
247 dev_err(dev
, "Failed to acquire clock 'ref'\n");
248 return PTR_ERR(prl
->clk_ref
);
251 ret
= clk_prepare_enable(prl
->clk_sys
);
255 img_prl_out_writel(prl
, IMG_PRL_OUT_CTL_EDGE_MASK
, IMG_PRL_OUT_CTL
);
256 img_prl_out_reset(prl
);
258 pm_runtime_enable(&pdev
->dev
);
259 if (!pm_runtime_enabled(&pdev
->dev
)) {
260 ret
= img_prl_out_resume(&pdev
->dev
);
265 prl
->dma_data
.addr
= res
->start
+ IMG_PRL_OUT_TX_FIFO
;
266 prl
->dma_data
.addr_width
= 4;
267 prl
->dma_data
.maxburst
= 4;
269 ret
= devm_snd_soc_register_component(&pdev
->dev
,
270 &img_prl_out_component
,
271 &img_prl_out_dai
, 1);
275 ret
= devm_snd_dmaengine_pcm_register(&pdev
->dev
, NULL
, 0);
282 if (!pm_runtime_status_suspended(&pdev
->dev
))
283 img_prl_out_suspend(&pdev
->dev
);
285 pm_runtime_disable(&pdev
->dev
);
286 clk_disable_unprepare(prl
->clk_sys
);
291 static int img_prl_out_dev_remove(struct platform_device
*pdev
)
293 struct img_prl_out
*prl
= platform_get_drvdata(pdev
);
295 pm_runtime_disable(&pdev
->dev
);
296 if (!pm_runtime_status_suspended(&pdev
->dev
))
297 img_prl_out_suspend(&pdev
->dev
);
299 clk_disable_unprepare(prl
->clk_sys
);
304 static const struct of_device_id img_prl_out_of_match
[] = {
305 { .compatible
= "img,parallel-out" },
308 MODULE_DEVICE_TABLE(of
, img_prl_out_of_match
);
310 static const struct dev_pm_ops img_prl_out_pm_ops
= {
311 SET_RUNTIME_PM_OPS(img_prl_out_suspend
,
312 img_prl_out_resume
, NULL
)
315 static struct platform_driver img_prl_out_driver
= {
317 .name
= "img-parallel-out",
318 .of_match_table
= img_prl_out_of_match
,
319 .pm
= &img_prl_out_pm_ops
321 .probe
= img_prl_out_probe
,
322 .remove
= img_prl_out_dev_remove
324 module_platform_driver(img_prl_out_driver
);
326 MODULE_AUTHOR("Damien Horsley <Damien.Horsley@imgtec.com>");
327 MODULE_DESCRIPTION("IMG Parallel Output Driver");
328 MODULE_LICENSE("GPL v2");