2 * IMG parallel output controller driver
4 * Copyright (C) 2015 Imagination Technologies Ltd.
6 * Author: Damien Horsley <Damien.Horsley@imgtec.com>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms and conditions of the GNU General Public License,
10 * version 2, as published by the Free Software Foundation.
13 #include <linux/clk.h>
14 #include <linux/init.h>
15 #include <linux/kernel.h>
16 #include <linux/module.h>
18 #include <linux/platform_device.h>
19 #include <linux/pm_runtime.h>
20 #include <linux/reset.h>
22 #include <sound/core.h>
23 #include <sound/dmaengine_pcm.h>
24 #include <sound/initval.h>
25 #include <sound/pcm.h>
26 #include <sound/pcm_params.h>
27 #include <sound/soc.h>
29 #define IMG_PRL_OUT_TX_FIFO 0
31 #define IMG_PRL_OUT_CTL 0x4
32 #define IMG_PRL_OUT_CTL_CH_MASK BIT(4)
33 #define IMG_PRL_OUT_CTL_PACKH_MASK BIT(3)
34 #define IMG_PRL_OUT_CTL_EDGE_MASK BIT(2)
35 #define IMG_PRL_OUT_CTL_ME_MASK BIT(1)
36 #define IMG_PRL_OUT_CTL_SRST_MASK BIT(0)
42 struct snd_dmaengine_dai_dma_data dma_data
;
44 struct reset_control
*rst
;
47 static int img_prl_out_suspend(struct device
*dev
)
49 struct img_prl_out
*prl
= dev_get_drvdata(dev
);
51 clk_disable_unprepare(prl
->clk_ref
);
56 static int img_prl_out_resume(struct device
*dev
)
58 struct img_prl_out
*prl
= dev_get_drvdata(dev
);
61 ret
= clk_prepare_enable(prl
->clk_ref
);
63 dev_err(dev
, "clk_enable failed: %d\n", ret
);
70 static inline void img_prl_out_writel(struct img_prl_out
*prl
,
73 writel(val
, prl
->base
+ reg
);
76 static inline u32
img_prl_out_readl(struct img_prl_out
*prl
, u32 reg
)
78 return readl(prl
->base
+ reg
);
81 static void img_prl_out_reset(struct img_prl_out
*prl
)
85 ctl
= img_prl_out_readl(prl
, IMG_PRL_OUT_CTL
) &
86 ~IMG_PRL_OUT_CTL_ME_MASK
;
88 reset_control_assert(prl
->rst
);
89 reset_control_deassert(prl
->rst
);
91 img_prl_out_writel(prl
, ctl
, IMG_PRL_OUT_CTL
);
94 static int img_prl_out_trigger(struct snd_pcm_substream
*substream
, int cmd
,
95 struct snd_soc_dai
*dai
)
97 struct img_prl_out
*prl
= snd_soc_dai_get_drvdata(dai
);
101 case SNDRV_PCM_TRIGGER_START
:
102 case SNDRV_PCM_TRIGGER_RESUME
:
103 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
104 reg
= img_prl_out_readl(prl
, IMG_PRL_OUT_CTL
);
105 reg
|= IMG_PRL_OUT_CTL_ME_MASK
;
106 img_prl_out_writel(prl
, reg
, IMG_PRL_OUT_CTL
);
108 case SNDRV_PCM_TRIGGER_STOP
:
109 case SNDRV_PCM_TRIGGER_SUSPEND
:
110 case SNDRV_PCM_TRIGGER_PAUSE_PUSH
:
111 img_prl_out_reset(prl
);
120 static int img_prl_out_hw_params(struct snd_pcm_substream
*substream
,
121 struct snd_pcm_hw_params
*params
, struct snd_soc_dai
*dai
)
123 struct img_prl_out
*prl
= snd_soc_dai_get_drvdata(dai
);
124 unsigned int rate
, channels
;
125 u32 reg
, control_set
= 0;
127 rate
= params_rate(params
);
128 channels
= params_channels(params
);
130 switch (params_format(params
)) {
131 case SNDRV_PCM_FORMAT_S32_LE
:
132 control_set
|= IMG_PRL_OUT_CTL_PACKH_MASK
;
134 case SNDRV_PCM_FORMAT_S24_LE
:
143 clk_set_rate(prl
->clk_ref
, rate
* 256);
145 reg
= img_prl_out_readl(prl
, IMG_PRL_OUT_CTL
);
146 reg
= (reg
& ~IMG_PRL_OUT_CTL_PACKH_MASK
) | control_set
;
147 img_prl_out_writel(prl
, reg
, IMG_PRL_OUT_CTL
);
152 static int img_prl_out_set_fmt(struct snd_soc_dai
*dai
, unsigned int fmt
)
154 struct img_prl_out
*prl
= snd_soc_dai_get_drvdata(dai
);
155 u32 reg
, control_set
= 0;
158 switch (fmt
& SND_SOC_DAIFMT_INV_MASK
) {
159 case SND_SOC_DAIFMT_NB_NF
:
161 case SND_SOC_DAIFMT_NB_IF
:
162 control_set
|= IMG_PRL_OUT_CTL_EDGE_MASK
;
168 ret
= pm_runtime_get_sync(prl
->dev
);
172 reg
= img_prl_out_readl(prl
, IMG_PRL_OUT_CTL
);
173 reg
= (reg
& ~IMG_PRL_OUT_CTL_EDGE_MASK
) | control_set
;
174 img_prl_out_writel(prl
, reg
, IMG_PRL_OUT_CTL
);
175 pm_runtime_put(prl
->dev
);
180 static const struct snd_soc_dai_ops img_prl_out_dai_ops
= {
181 .trigger
= img_prl_out_trigger
,
182 .hw_params
= img_prl_out_hw_params
,
183 .set_fmt
= img_prl_out_set_fmt
186 static int img_prl_out_dai_probe(struct snd_soc_dai
*dai
)
188 struct img_prl_out
*prl
= snd_soc_dai_get_drvdata(dai
);
190 snd_soc_dai_init_dma_data(dai
, &prl
->dma_data
, NULL
);
195 static struct snd_soc_dai_driver img_prl_out_dai
= {
196 .probe
= img_prl_out_dai_probe
,
200 .rates
= SNDRV_PCM_RATE_8000_192000
,
201 .formats
= SNDRV_PCM_FMTBIT_S32_LE
| SNDRV_PCM_FMTBIT_S24_LE
203 .ops
= &img_prl_out_dai_ops
206 static const struct snd_soc_component_driver img_prl_out_component
= {
207 .name
= "img-prl-out"
210 static int img_prl_out_probe(struct platform_device
*pdev
)
212 struct img_prl_out
*prl
;
213 struct resource
*res
;
216 struct device
*dev
= &pdev
->dev
;
218 prl
= devm_kzalloc(&pdev
->dev
, sizeof(*prl
), GFP_KERNEL
);
222 platform_set_drvdata(pdev
, prl
);
224 prl
->dev
= &pdev
->dev
;
226 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
227 base
= devm_ioremap_resource(&pdev
->dev
, res
);
229 return PTR_ERR(base
);
233 prl
->rst
= devm_reset_control_get_exclusive(&pdev
->dev
, "rst");
234 if (IS_ERR(prl
->rst
)) {
235 if (PTR_ERR(prl
->rst
) != -EPROBE_DEFER
)
236 dev_err(&pdev
->dev
, "No top level reset found\n");
237 return PTR_ERR(prl
->rst
);
240 prl
->clk_sys
= devm_clk_get(&pdev
->dev
, "sys");
241 if (IS_ERR(prl
->clk_sys
)) {
242 if (PTR_ERR(prl
->clk_sys
) != -EPROBE_DEFER
)
243 dev_err(dev
, "Failed to acquire clock 'sys'\n");
244 return PTR_ERR(prl
->clk_sys
);
247 prl
->clk_ref
= devm_clk_get(&pdev
->dev
, "ref");
248 if (IS_ERR(prl
->clk_ref
)) {
249 if (PTR_ERR(prl
->clk_ref
) != -EPROBE_DEFER
)
250 dev_err(dev
, "Failed to acquire clock 'ref'\n");
251 return PTR_ERR(prl
->clk_ref
);
254 ret
= clk_prepare_enable(prl
->clk_sys
);
258 img_prl_out_writel(prl
, IMG_PRL_OUT_CTL_EDGE_MASK
, IMG_PRL_OUT_CTL
);
259 img_prl_out_reset(prl
);
261 pm_runtime_enable(&pdev
->dev
);
262 if (!pm_runtime_enabled(&pdev
->dev
)) {
263 ret
= img_prl_out_resume(&pdev
->dev
);
268 prl
->dma_data
.addr
= res
->start
+ IMG_PRL_OUT_TX_FIFO
;
269 prl
->dma_data
.addr_width
= 4;
270 prl
->dma_data
.maxburst
= 4;
272 ret
= devm_snd_soc_register_component(&pdev
->dev
,
273 &img_prl_out_component
,
274 &img_prl_out_dai
, 1);
278 ret
= devm_snd_dmaengine_pcm_register(&pdev
->dev
, NULL
, 0);
285 if (!pm_runtime_status_suspended(&pdev
->dev
))
286 img_prl_out_suspend(&pdev
->dev
);
288 pm_runtime_disable(&pdev
->dev
);
289 clk_disable_unprepare(prl
->clk_sys
);
294 static int img_prl_out_dev_remove(struct platform_device
*pdev
)
296 struct img_prl_out
*prl
= platform_get_drvdata(pdev
);
298 pm_runtime_disable(&pdev
->dev
);
299 if (!pm_runtime_status_suspended(&pdev
->dev
))
300 img_prl_out_suspend(&pdev
->dev
);
302 clk_disable_unprepare(prl
->clk_sys
);
307 static const struct of_device_id img_prl_out_of_match
[] = {
308 { .compatible
= "img,parallel-out" },
311 MODULE_DEVICE_TABLE(of
, img_prl_out_of_match
);
313 static const struct dev_pm_ops img_prl_out_pm_ops
= {
314 SET_RUNTIME_PM_OPS(img_prl_out_suspend
,
315 img_prl_out_resume
, NULL
)
318 static struct platform_driver img_prl_out_driver
= {
320 .name
= "img-parallel-out",
321 .of_match_table
= img_prl_out_of_match
,
322 .pm
= &img_prl_out_pm_ops
324 .probe
= img_prl_out_probe
,
325 .remove
= img_prl_out_dev_remove
327 module_platform_driver(img_prl_out_driver
);
329 MODULE_AUTHOR("Damien Horsley <Damien.Horsley@imgtec.com>");
330 MODULE_DESCRIPTION("IMG Parallel Output Driver");
331 MODULE_LICENSE("GPL v2");