xref: /linux/drivers/gpu/ipu-v3/ipu-prg.c (revision a1ff5a7d78a036d6c2178ee5acd6ba4946243800)
12025cf9eSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2ea9c2605SLucas Stach /*
3ea9c2605SLucas Stach  * Copyright (c) 2016-2017 Lucas Stach, Pengutronix
4ea9c2605SLucas Stach  */
5ea9c2605SLucas Stach 
6ea9c2605SLucas Stach #include <drm/drm_fourcc.h>
7ea9c2605SLucas Stach #include <linux/clk.h>
8ea9c2605SLucas Stach #include <linux/err.h>
9263c3b80SLucas Stach #include <linux/iopoll.h>
10ea9c2605SLucas Stach #include <linux/mfd/syscon.h>
11ea9c2605SLucas Stach #include <linux/mfd/syscon/imx6q-iomuxc-gpr.h>
12ea9c2605SLucas Stach #include <linux/module.h>
13ea9c2605SLucas Stach #include <linux/of.h>
14ea9c2605SLucas Stach #include <linux/platform_device.h>
1572944089SLucas Stach #include <linux/pm_runtime.h>
16ea9c2605SLucas Stach #include <linux/regmap.h>
17ea9c2605SLucas Stach #include <video/imx-ipu-v3.h>
18ea9c2605SLucas Stach 
19ea9c2605SLucas Stach #include "ipu-prv.h"
20ea9c2605SLucas Stach 
21ea9c2605SLucas Stach #define IPU_PRG_CTL				0x00
22ea9c2605SLucas Stach #define  IPU_PRG_CTL_BYPASS(i)			(1 << (0 + i))
23ea9c2605SLucas Stach #define  IPU_PRG_CTL_SOFT_ARID_MASK		0x3
24ea9c2605SLucas Stach #define  IPU_PRG_CTL_SOFT_ARID_SHIFT(i)		(8 + i * 2)
25ea9c2605SLucas Stach #define  IPU_PRG_CTL_SOFT_ARID(i, v)		((v & 0x3) << (8 + 2 * i))
26ea9c2605SLucas Stach #define  IPU_PRG_CTL_SO(i)			(1 << (16 + i))
27ea9c2605SLucas Stach #define  IPU_PRG_CTL_VFLIP(i)			(1 << (19 + i))
28ea9c2605SLucas Stach #define  IPU_PRG_CTL_BLOCK_MODE(i)		(1 << (22 + i))
29ea9c2605SLucas Stach #define  IPU_PRG_CTL_CNT_LOAD_EN(i)		(1 << (25 + i))
30ea9c2605SLucas Stach #define  IPU_PRG_CTL_SOFTRST			(1 << 30)
31ea9c2605SLucas Stach #define  IPU_PRG_CTL_SHADOW_EN			(1 << 31)
32ea9c2605SLucas Stach 
33ea9c2605SLucas Stach #define IPU_PRG_STATUS				0x04
34ea9c2605SLucas Stach #define  IPU_PRG_STATUS_BUFFER0_READY(i)	(1 << (0 + i * 2))
35ea9c2605SLucas Stach #define  IPU_PRG_STATUS_BUFFER1_READY(i)	(1 << (1 + i * 2))
36ea9c2605SLucas Stach 
37ea9c2605SLucas Stach #define IPU_PRG_QOS				0x08
38ea9c2605SLucas Stach #define  IPU_PRG_QOS_ARID_MASK			0xf
39ea9c2605SLucas Stach #define  IPU_PRG_QOS_ARID_SHIFT(i)		(0 + i * 4)
40ea9c2605SLucas Stach 
41ea9c2605SLucas Stach #define IPU_PRG_REG_UPDATE			0x0c
42ea9c2605SLucas Stach #define  IPU_PRG_REG_UPDATE_REG_UPDATE		(1 << 0)
43ea9c2605SLucas Stach 
44ea9c2605SLucas Stach #define IPU_PRG_STRIDE(i)			(0x10 + i * 0x4)
45ea9c2605SLucas Stach #define  IPU_PRG_STRIDE_STRIDE_MASK		0x3fff
46ea9c2605SLucas Stach 
47ea9c2605SLucas Stach #define IPU_PRG_CROP_LINE			0x1c
48ea9c2605SLucas Stach 
49ea9c2605SLucas Stach #define IPU_PRG_THD				0x20
50ea9c2605SLucas Stach 
51ea9c2605SLucas Stach #define IPU_PRG_BADDR(i)			(0x24 + i * 0x4)
52ea9c2605SLucas Stach 
53ea9c2605SLucas Stach #define IPU_PRG_OFFSET(i)			(0x30 + i * 0x4)
54ea9c2605SLucas Stach 
55ea9c2605SLucas Stach #define IPU_PRG_ILO(i)				(0x3c + i * 0x4)
56ea9c2605SLucas Stach 
57ea9c2605SLucas Stach #define IPU_PRG_HEIGHT(i)			(0x48 + i * 0x4)
58ea9c2605SLucas Stach #define  IPU_PRG_HEIGHT_PRE_HEIGHT_MASK		0xfff
59ea9c2605SLucas Stach #define  IPU_PRG_HEIGHT_PRE_HEIGHT_SHIFT	0
60ea9c2605SLucas Stach #define  IPU_PRG_HEIGHT_IPU_HEIGHT_MASK		0xfff
61ea9c2605SLucas Stach #define  IPU_PRG_HEIGHT_IPU_HEIGHT_SHIFT	16
62ea9c2605SLucas Stach 
63ea9c2605SLucas Stach struct ipu_prg_channel {
64ea9c2605SLucas Stach 	bool			enabled;
65ea9c2605SLucas Stach 	int			used_pre;
66ea9c2605SLucas Stach };
67ea9c2605SLucas Stach 
68ea9c2605SLucas Stach struct ipu_prg {
69ea9c2605SLucas Stach 	struct list_head	list;
70ea9c2605SLucas Stach 	struct device		*dev;
71ea9c2605SLucas Stach 	int			id;
72ea9c2605SLucas Stach 
73ea9c2605SLucas Stach 	void __iomem		*regs;
74ea9c2605SLucas Stach 	struct clk		*clk_ipg, *clk_axi;
75ea9c2605SLucas Stach 	struct regmap		*iomuxc_gpr;
76ea9c2605SLucas Stach 	struct ipu_pre		*pres[3];
77ea9c2605SLucas Stach 
78ea9c2605SLucas Stach 	struct ipu_prg_channel	chan[3];
79ea9c2605SLucas Stach };
80ea9c2605SLucas Stach 
81ea9c2605SLucas Stach static DEFINE_MUTEX(ipu_prg_list_mutex);
82ea9c2605SLucas Stach static LIST_HEAD(ipu_prg_list);
83ea9c2605SLucas Stach 
84ea9c2605SLucas Stach struct ipu_prg *
ipu_prg_lookup_by_phandle(struct device * dev,const char * name,int ipu_id)85ea9c2605SLucas Stach ipu_prg_lookup_by_phandle(struct device *dev, const char *name, int ipu_id)
86ea9c2605SLucas Stach {
87ea9c2605SLucas Stach 	struct device_node *prg_node = of_parse_phandle(dev->of_node,
88ea9c2605SLucas Stach 							name, 0);
89ea9c2605SLucas Stach 	struct ipu_prg *prg;
90ea9c2605SLucas Stach 
91ea9c2605SLucas Stach 	mutex_lock(&ipu_prg_list_mutex);
92ea9c2605SLucas Stach 	list_for_each_entry(prg, &ipu_prg_list, list) {
93ea9c2605SLucas Stach 		if (prg_node == prg->dev->of_node) {
94ea9c2605SLucas Stach 			mutex_unlock(&ipu_prg_list_mutex);
95e88728f4SVivek Gautam 			device_link_add(dev, prg->dev,
96e88728f4SVivek Gautam 					DL_FLAG_AUTOREMOVE_CONSUMER);
97ea9c2605SLucas Stach 			prg->id = ipu_id;
983addaba8STobias Jordan 			of_node_put(prg_node);
99ea9c2605SLucas Stach 			return prg;
100ea9c2605SLucas Stach 		}
101ea9c2605SLucas Stach 	}
102ea9c2605SLucas Stach 	mutex_unlock(&ipu_prg_list_mutex);
103ea9c2605SLucas Stach 
1043addaba8STobias Jordan 	of_node_put(prg_node);
1053addaba8STobias Jordan 
106ea9c2605SLucas Stach 	return NULL;
107ea9c2605SLucas Stach }
108ea9c2605SLucas Stach 
ipu_prg_max_active_channels(void)109ea9c2605SLucas Stach int ipu_prg_max_active_channels(void)
110ea9c2605SLucas Stach {
111ea9c2605SLucas Stach 	return ipu_pre_get_available_count();
112ea9c2605SLucas Stach }
113ea9c2605SLucas Stach EXPORT_SYMBOL_GPL(ipu_prg_max_active_channels);
114ea9c2605SLucas Stach 
ipu_prg_present(struct ipu_soc * ipu)115ea9c2605SLucas Stach bool ipu_prg_present(struct ipu_soc *ipu)
116ea9c2605SLucas Stach {
117ea9c2605SLucas Stach 	if (ipu->prg_priv)
118ea9c2605SLucas Stach 		return true;
119ea9c2605SLucas Stach 
120ea9c2605SLucas Stach 	return false;
121ea9c2605SLucas Stach }
122ea9c2605SLucas Stach EXPORT_SYMBOL_GPL(ipu_prg_present);
123ea9c2605SLucas Stach 
ipu_prg_format_supported(struct ipu_soc * ipu,uint32_t format,uint64_t modifier)124ea9c2605SLucas Stach bool ipu_prg_format_supported(struct ipu_soc *ipu, uint32_t format,
125ea9c2605SLucas Stach 			      uint64_t modifier)
126ea9c2605SLucas Stach {
127ea9c2605SLucas Stach 	const struct drm_format_info *info = drm_format_info(format);
128ea9c2605SLucas Stach 
129ea9c2605SLucas Stach 	if (info->num_planes != 1)
130ea9c2605SLucas Stach 		return false;
131ea9c2605SLucas Stach 
132a2ceec52SLucas Stach 	switch (modifier) {
133a2ceec52SLucas Stach 	case DRM_FORMAT_MOD_LINEAR:
134a2ceec52SLucas Stach 	case DRM_FORMAT_MOD_VIVANTE_TILED:
135a2ceec52SLucas Stach 	case DRM_FORMAT_MOD_VIVANTE_SUPER_TILED:
136ea9c2605SLucas Stach 		return true;
137a2ceec52SLucas Stach 	default:
138a2ceec52SLucas Stach 		return false;
139a2ceec52SLucas Stach 	}
140ea9c2605SLucas Stach }
141ea9c2605SLucas Stach EXPORT_SYMBOL_GPL(ipu_prg_format_supported);
142ea9c2605SLucas Stach 
ipu_prg_enable(struct ipu_soc * ipu)143ea9c2605SLucas Stach int ipu_prg_enable(struct ipu_soc *ipu)
144ea9c2605SLucas Stach {
145ea9c2605SLucas Stach 	struct ipu_prg *prg = ipu->prg_priv;
146ea9c2605SLucas Stach 
147ea9c2605SLucas Stach 	if (!prg)
148ea9c2605SLucas Stach 		return 0;
149ea9c2605SLucas Stach 
15072944089SLucas Stach 	return pm_runtime_get_sync(prg->dev);
151ea9c2605SLucas Stach }
152ea9c2605SLucas Stach EXPORT_SYMBOL_GPL(ipu_prg_enable);
153ea9c2605SLucas Stach 
ipu_prg_disable(struct ipu_soc * ipu)154ea9c2605SLucas Stach void ipu_prg_disable(struct ipu_soc *ipu)
155ea9c2605SLucas Stach {
156ea9c2605SLucas Stach 	struct ipu_prg *prg = ipu->prg_priv;
157ea9c2605SLucas Stach 
158ea9c2605SLucas Stach 	if (!prg)
159ea9c2605SLucas Stach 		return;
160ea9c2605SLucas Stach 
16172944089SLucas Stach 	pm_runtime_put(prg->dev);
162ea9c2605SLucas Stach }
163ea9c2605SLucas Stach EXPORT_SYMBOL_GPL(ipu_prg_disable);
164ea9c2605SLucas Stach 
165ea9c2605SLucas Stach /*
166ea9c2605SLucas Stach  * The channel configuartion functions below are not thread safe, as they
167ea9c2605SLucas Stach  * must be only called from the atomic commit path in the DRM driver, which
168ea9c2605SLucas Stach  * is properly serialized.
169ea9c2605SLucas Stach  */
ipu_prg_ipu_to_prg_chan(int ipu_chan)170ea9c2605SLucas Stach static int ipu_prg_ipu_to_prg_chan(int ipu_chan)
171ea9c2605SLucas Stach {
172ea9c2605SLucas Stach 	/*
173ea9c2605SLucas Stach 	 * This isn't clearly documented in the RM, but IPU to PRG channel
174ea9c2605SLucas Stach 	 * assignment is fixed, as only with this mapping the control signals
175ea9c2605SLucas Stach 	 * match up.
176ea9c2605SLucas Stach 	 */
177ea9c2605SLucas Stach 	switch (ipu_chan) {
178ea9c2605SLucas Stach 	case IPUV3_CHANNEL_MEM_BG_SYNC:
179ea9c2605SLucas Stach 		return 0;
180ea9c2605SLucas Stach 	case IPUV3_CHANNEL_MEM_FG_SYNC:
181ea9c2605SLucas Stach 		return 1;
182ea9c2605SLucas Stach 	case IPUV3_CHANNEL_MEM_DC_SYNC:
183ea9c2605SLucas Stach 		return 2;
184ea9c2605SLucas Stach 	default:
185ea9c2605SLucas Stach 		return -EINVAL;
186ea9c2605SLucas Stach 	}
187ea9c2605SLucas Stach }
188ea9c2605SLucas Stach 
ipu_prg_get_pre(struct ipu_prg * prg,int prg_chan)189ea9c2605SLucas Stach static int ipu_prg_get_pre(struct ipu_prg *prg, int prg_chan)
190ea9c2605SLucas Stach {
191ea9c2605SLucas Stach 	int i, ret;
192ea9c2605SLucas Stach 
193ea9c2605SLucas Stach 	/* channel 0 is special as it is hardwired to one of the PREs */
194ea9c2605SLucas Stach 	if (prg_chan == 0) {
195ea9c2605SLucas Stach 		ret = ipu_pre_get(prg->pres[0]);
196ea9c2605SLucas Stach 		if (ret)
197ea9c2605SLucas Stach 			goto fail;
198ea9c2605SLucas Stach 		prg->chan[prg_chan].used_pre = 0;
199ea9c2605SLucas Stach 		return 0;
200ea9c2605SLucas Stach 	}
201ea9c2605SLucas Stach 
202ea9c2605SLucas Stach 	for (i = 1; i < 3; i++) {
203ea9c2605SLucas Stach 		ret = ipu_pre_get(prg->pres[i]);
204ea9c2605SLucas Stach 		if (!ret) {
205ea9c2605SLucas Stach 			u32 val, mux;
206ea9c2605SLucas Stach 			int shift;
207ea9c2605SLucas Stach 
208ea9c2605SLucas Stach 			prg->chan[prg_chan].used_pre = i;
209ea9c2605SLucas Stach 
210ea9c2605SLucas Stach 			/* configure the PRE to PRG channel mux */
211ea9c2605SLucas Stach 			shift = (i == 1) ? 12 : 14;
212ea9c2605SLucas Stach 			mux = (prg->id << 1) | (prg_chan - 1);
213ea9c2605SLucas Stach 			regmap_update_bits(prg->iomuxc_gpr, IOMUXC_GPR5,
214ea9c2605SLucas Stach 					   0x3 << shift, mux << shift);
215ea9c2605SLucas Stach 
216ea9c2605SLucas Stach 			/* check other mux, must not point to same channel */
217ea9c2605SLucas Stach 			shift = (i == 1) ? 14 : 12;
218ea9c2605SLucas Stach 			regmap_read(prg->iomuxc_gpr, IOMUXC_GPR5, &val);
219ea9c2605SLucas Stach 			if (((val >> shift) & 0x3) == mux) {
220ea9c2605SLucas Stach 				regmap_update_bits(prg->iomuxc_gpr, IOMUXC_GPR5,
221ea9c2605SLucas Stach 						   0x3 << shift,
222ea9c2605SLucas Stach 						   (mux ^ 0x1) << shift);
223ea9c2605SLucas Stach 			}
224ea9c2605SLucas Stach 
225ea9c2605SLucas Stach 			return 0;
226ea9c2605SLucas Stach 		}
227ea9c2605SLucas Stach 	}
228ea9c2605SLucas Stach 
229ea9c2605SLucas Stach fail:
230ea9c2605SLucas Stach 	dev_err(prg->dev, "could not get PRE for PRG chan %d", prg_chan);
231ea9c2605SLucas Stach 	return ret;
232ea9c2605SLucas Stach }
233ea9c2605SLucas Stach 
ipu_prg_put_pre(struct ipu_prg * prg,int prg_chan)234ea9c2605SLucas Stach static void ipu_prg_put_pre(struct ipu_prg *prg, int prg_chan)
235ea9c2605SLucas Stach {
236ea9c2605SLucas Stach 	struct ipu_prg_channel *chan = &prg->chan[prg_chan];
237ea9c2605SLucas Stach 
238ea9c2605SLucas Stach 	ipu_pre_put(prg->pres[chan->used_pre]);
239ea9c2605SLucas Stach 	chan->used_pre = -1;
240ea9c2605SLucas Stach }
241ea9c2605SLucas Stach 
ipu_prg_channel_disable(struct ipuv3_channel * ipu_chan)242ea9c2605SLucas Stach void ipu_prg_channel_disable(struct ipuv3_channel *ipu_chan)
243ea9c2605SLucas Stach {
244ea9c2605SLucas Stach 	int prg_chan = ipu_prg_ipu_to_prg_chan(ipu_chan->num);
245ea9c2605SLucas Stach 	struct ipu_prg *prg = ipu_chan->ipu->prg_priv;
246746d024cSArnd Bergmann 	struct ipu_prg_channel *chan;
247ea9c2605SLucas Stach 	u32 val;
248ea9c2605SLucas Stach 
249746d024cSArnd Bergmann 	if (prg_chan < 0)
250746d024cSArnd Bergmann 		return;
251746d024cSArnd Bergmann 
252746d024cSArnd Bergmann 	chan = &prg->chan[prg_chan];
253746d024cSArnd Bergmann 	if (!chan->enabled)
254ea9c2605SLucas Stach 		return;
255ea9c2605SLucas Stach 
25672944089SLucas Stach 	pm_runtime_get_sync(prg->dev);
257ea9c2605SLucas Stach 
258ea9c2605SLucas Stach 	val = readl(prg->regs + IPU_PRG_CTL);
259ea9c2605SLucas Stach 	val |= IPU_PRG_CTL_BYPASS(prg_chan);
260ea9c2605SLucas Stach 	writel(val, prg->regs + IPU_PRG_CTL);
261ea9c2605SLucas Stach 
262ea9c2605SLucas Stach 	val = IPU_PRG_REG_UPDATE_REG_UPDATE;
263ea9c2605SLucas Stach 	writel(val, prg->regs + IPU_PRG_REG_UPDATE);
264ea9c2605SLucas Stach 
26572944089SLucas Stach 	pm_runtime_put(prg->dev);
266ea9c2605SLucas Stach 
267ea9c2605SLucas Stach 	ipu_prg_put_pre(prg, prg_chan);
268ea9c2605SLucas Stach 
269ea9c2605SLucas Stach 	chan->enabled = false;
270ea9c2605SLucas Stach }
271ea9c2605SLucas Stach EXPORT_SYMBOL_GPL(ipu_prg_channel_disable);
272ea9c2605SLucas Stach 
ipu_prg_channel_configure(struct ipuv3_channel * ipu_chan,unsigned int axi_id,unsigned int width,unsigned int height,unsigned int stride,u32 format,uint64_t modifier,unsigned long * eba)273ea9c2605SLucas Stach int ipu_prg_channel_configure(struct ipuv3_channel *ipu_chan,
274ea9c2605SLucas Stach 			      unsigned int axi_id, unsigned int width,
275ea9c2605SLucas Stach 			      unsigned int height, unsigned int stride,
276a2ceec52SLucas Stach 			      u32 format, uint64_t modifier, unsigned long *eba)
277ea9c2605SLucas Stach {
278ea9c2605SLucas Stach 	int prg_chan = ipu_prg_ipu_to_prg_chan(ipu_chan->num);
279ea9c2605SLucas Stach 	struct ipu_prg *prg = ipu_chan->ipu->prg_priv;
280746d024cSArnd Bergmann 	struct ipu_prg_channel *chan;
281ea9c2605SLucas Stach 	u32 val;
282ea9c2605SLucas Stach 	int ret;
283ea9c2605SLucas Stach 
284ea9c2605SLucas Stach 	if (prg_chan < 0)
285ea9c2605SLucas Stach 		return prg_chan;
286ea9c2605SLucas Stach 
287746d024cSArnd Bergmann 	chan = &prg->chan[prg_chan];
288746d024cSArnd Bergmann 
289ea9c2605SLucas Stach 	if (chan->enabled) {
2904dbc7d5dSLucas Stach 		ipu_pre_update(prg->pres[chan->used_pre], modifier, *eba);
291ea9c2605SLucas Stach 		return 0;
292ea9c2605SLucas Stach 	}
293ea9c2605SLucas Stach 
294ea9c2605SLucas Stach 	ret = ipu_prg_get_pre(prg, prg_chan);
295ea9c2605SLucas Stach 	if (ret)
296ea9c2605SLucas Stach 		return ret;
297ea9c2605SLucas Stach 
298ea9c2605SLucas Stach 	ipu_pre_configure(prg->pres[chan->used_pre],
299a2ceec52SLucas Stach 			  width, height, stride, format, modifier, *eba);
300ea9c2605SLucas Stach 
301ea9c2605SLucas Stach 
30272944089SLucas Stach 	pm_runtime_get_sync(prg->dev);
303ea9c2605SLucas Stach 
304ea9c2605SLucas Stach 	val = (stride - 1) & IPU_PRG_STRIDE_STRIDE_MASK;
305ea9c2605SLucas Stach 	writel(val, prg->regs + IPU_PRG_STRIDE(prg_chan));
306ea9c2605SLucas Stach 
307ea9c2605SLucas Stach 	val = ((height & IPU_PRG_HEIGHT_PRE_HEIGHT_MASK) <<
308ea9c2605SLucas Stach 	       IPU_PRG_HEIGHT_PRE_HEIGHT_SHIFT) |
309ea9c2605SLucas Stach 	      ((height & IPU_PRG_HEIGHT_IPU_HEIGHT_MASK) <<
310ea9c2605SLucas Stach 	       IPU_PRG_HEIGHT_IPU_HEIGHT_SHIFT);
311ea9c2605SLucas Stach 	writel(val, prg->regs + IPU_PRG_HEIGHT(prg_chan));
312ea9c2605SLucas Stach 
313ea9c2605SLucas Stach 	val = ipu_pre_get_baddr(prg->pres[chan->used_pre]);
314ea9c2605SLucas Stach 	*eba = val;
315ea9c2605SLucas Stach 	writel(val, prg->regs + IPU_PRG_BADDR(prg_chan));
316ea9c2605SLucas Stach 
317ea9c2605SLucas Stach 	val = readl(prg->regs + IPU_PRG_CTL);
318ea9c2605SLucas Stach 	/* config AXI ID */
319ea9c2605SLucas Stach 	val &= ~(IPU_PRG_CTL_SOFT_ARID_MASK <<
320ea9c2605SLucas Stach 		 IPU_PRG_CTL_SOFT_ARID_SHIFT(prg_chan));
321ea9c2605SLucas Stach 	val |= IPU_PRG_CTL_SOFT_ARID(prg_chan, axi_id);
322ea9c2605SLucas Stach 	/* enable channel */
323ea9c2605SLucas Stach 	val &= ~IPU_PRG_CTL_BYPASS(prg_chan);
324ea9c2605SLucas Stach 	writel(val, prg->regs + IPU_PRG_CTL);
325ea9c2605SLucas Stach 
326ea9c2605SLucas Stach 	val = IPU_PRG_REG_UPDATE_REG_UPDATE;
327ea9c2605SLucas Stach 	writel(val, prg->regs + IPU_PRG_REG_UPDATE);
328ea9c2605SLucas Stach 
329263c3b80SLucas Stach 	/* wait for both double buffers to be filled */
330263c3b80SLucas Stach 	readl_poll_timeout(prg->regs + IPU_PRG_STATUS, val,
331263c3b80SLucas Stach 			   (val & IPU_PRG_STATUS_BUFFER0_READY(prg_chan)) &&
332263c3b80SLucas Stach 			   (val & IPU_PRG_STATUS_BUFFER1_READY(prg_chan)),
333263c3b80SLucas Stach 			   5, 1000);
334263c3b80SLucas Stach 
33572944089SLucas Stach 	pm_runtime_put(prg->dev);
336ea9c2605SLucas Stach 
337ea9c2605SLucas Stach 	chan->enabled = true;
338ea9c2605SLucas Stach 	return 0;
339ea9c2605SLucas Stach }
340ea9c2605SLucas Stach EXPORT_SYMBOL_GPL(ipu_prg_channel_configure);
341ea9c2605SLucas Stach 
ipu_prg_channel_configure_pending(struct ipuv3_channel * ipu_chan)3424bfbd561SLucas Stach bool ipu_prg_channel_configure_pending(struct ipuv3_channel *ipu_chan)
3434bfbd561SLucas Stach {
3444bfbd561SLucas Stach 	int prg_chan = ipu_prg_ipu_to_prg_chan(ipu_chan->num);
3454bfbd561SLucas Stach 	struct ipu_prg *prg = ipu_chan->ipu->prg_priv;
3464bfbd561SLucas Stach 	struct ipu_prg_channel *chan;
3474bfbd561SLucas Stach 
3484bfbd561SLucas Stach 	if (prg_chan < 0)
3494bfbd561SLucas Stach 		return false;
3504bfbd561SLucas Stach 
3514bfbd561SLucas Stach 	chan = &prg->chan[prg_chan];
3524bfbd561SLucas Stach 	WARN_ON(!chan->enabled);
3534bfbd561SLucas Stach 
3544bfbd561SLucas Stach 	return ipu_pre_update_pending(prg->pres[chan->used_pre]);
3554bfbd561SLucas Stach }
3564bfbd561SLucas Stach EXPORT_SYMBOL_GPL(ipu_prg_channel_configure_pending);
3574bfbd561SLucas Stach 
ipu_prg_probe(struct platform_device * pdev)358ea9c2605SLucas Stach static int ipu_prg_probe(struct platform_device *pdev)
359ea9c2605SLucas Stach {
360ea9c2605SLucas Stach 	struct device *dev = &pdev->dev;
361ea9c2605SLucas Stach 	struct ipu_prg *prg;
362ea9c2605SLucas Stach 	u32 val;
363ea9c2605SLucas Stach 	int i, ret;
364ea9c2605SLucas Stach 
365ea9c2605SLucas Stach 	prg = devm_kzalloc(dev, sizeof(*prg), GFP_KERNEL);
366ea9c2605SLucas Stach 	if (!prg)
367ea9c2605SLucas Stach 		return -ENOMEM;
368ea9c2605SLucas Stach 
369c1f386abSYangtao Li 	prg->regs = devm_platform_ioremap_resource(pdev, 0);
370ea9c2605SLucas Stach 	if (IS_ERR(prg->regs))
371ea9c2605SLucas Stach 		return PTR_ERR(prg->regs);
372ea9c2605SLucas Stach 
373ea9c2605SLucas Stach 	prg->clk_ipg = devm_clk_get(dev, "ipg");
374ea9c2605SLucas Stach 	if (IS_ERR(prg->clk_ipg))
375ea9c2605SLucas Stach 		return PTR_ERR(prg->clk_ipg);
376ea9c2605SLucas Stach 
377ea9c2605SLucas Stach 	prg->clk_axi = devm_clk_get(dev, "axi");
378ea9c2605SLucas Stach 	if (IS_ERR(prg->clk_axi))
379ea9c2605SLucas Stach 		return PTR_ERR(prg->clk_axi);
380ea9c2605SLucas Stach 
381ea9c2605SLucas Stach 	prg->iomuxc_gpr =
382ea9c2605SLucas Stach 		syscon_regmap_lookup_by_compatible("fsl,imx6q-iomuxc-gpr");
383ea9c2605SLucas Stach 	if (IS_ERR(prg->iomuxc_gpr))
384ea9c2605SLucas Stach 		return PTR_ERR(prg->iomuxc_gpr);
385ea9c2605SLucas Stach 
386ea9c2605SLucas Stach 	for (i = 0; i < 3; i++) {
387ea9c2605SLucas Stach 		prg->pres[i] = ipu_pre_lookup_by_phandle(dev, "fsl,pres", i);
388ea9c2605SLucas Stach 		if (!prg->pres[i])
389ea9c2605SLucas Stach 			return -EPROBE_DEFER;
390ea9c2605SLucas Stach 	}
391ea9c2605SLucas Stach 
392ea9c2605SLucas Stach 	ret = clk_prepare_enable(prg->clk_ipg);
393ea9c2605SLucas Stach 	if (ret)
394ea9c2605SLucas Stach 		return ret;
395ea9c2605SLucas Stach 
39672944089SLucas Stach 	ret = clk_prepare_enable(prg->clk_axi);
39772944089SLucas Stach 	if (ret) {
39872944089SLucas Stach 		clk_disable_unprepare(prg->clk_ipg);
39972944089SLucas Stach 		return ret;
40072944089SLucas Stach 	}
40172944089SLucas Stach 
402ea9c2605SLucas Stach 	/* init to free running mode */
403ea9c2605SLucas Stach 	val = readl(prg->regs + IPU_PRG_CTL);
404ea9c2605SLucas Stach 	val |= IPU_PRG_CTL_SHADOW_EN;
405ea9c2605SLucas Stach 	writel(val, prg->regs + IPU_PRG_CTL);
406ea9c2605SLucas Stach 
407ea9c2605SLucas Stach 	/* disable address threshold */
408ea9c2605SLucas Stach 	writel(0xffffffff, prg->regs + IPU_PRG_THD);
409ea9c2605SLucas Stach 
41072944089SLucas Stach 	pm_runtime_set_active(dev);
41172944089SLucas Stach 	pm_runtime_enable(dev);
412ea9c2605SLucas Stach 
413ea9c2605SLucas Stach 	prg->dev = dev;
414ea9c2605SLucas Stach 	platform_set_drvdata(pdev, prg);
415ea9c2605SLucas Stach 	mutex_lock(&ipu_prg_list_mutex);
416ea9c2605SLucas Stach 	list_add(&prg->list, &ipu_prg_list);
417ea9c2605SLucas Stach 	mutex_unlock(&ipu_prg_list_mutex);
418ea9c2605SLucas Stach 
419ea9c2605SLucas Stach 	return 0;
420ea9c2605SLucas Stach }
421ea9c2605SLucas Stach 
ipu_prg_remove(struct platform_device * pdev)422*4402a5aaSUwe Kleine-König static void ipu_prg_remove(struct platform_device *pdev)
423ea9c2605SLucas Stach {
424ea9c2605SLucas Stach 	struct ipu_prg *prg = platform_get_drvdata(pdev);
425ea9c2605SLucas Stach 
426ea9c2605SLucas Stach 	mutex_lock(&ipu_prg_list_mutex);
427ea9c2605SLucas Stach 	list_del(&prg->list);
428ea9c2605SLucas Stach 	mutex_unlock(&ipu_prg_list_mutex);
429ea9c2605SLucas Stach }
430ea9c2605SLucas Stach 
43172944089SLucas Stach #ifdef CONFIG_PM
prg_suspend(struct device * dev)43272944089SLucas Stach static int prg_suspend(struct device *dev)
43372944089SLucas Stach {
43472944089SLucas Stach 	struct ipu_prg *prg = dev_get_drvdata(dev);
43572944089SLucas Stach 
43672944089SLucas Stach 	clk_disable_unprepare(prg->clk_axi);
43772944089SLucas Stach 	clk_disable_unprepare(prg->clk_ipg);
43872944089SLucas Stach 
43972944089SLucas Stach 	return 0;
44072944089SLucas Stach }
44172944089SLucas Stach 
prg_resume(struct device * dev)44272944089SLucas Stach static int prg_resume(struct device *dev)
44372944089SLucas Stach {
44472944089SLucas Stach 	struct ipu_prg *prg = dev_get_drvdata(dev);
44572944089SLucas Stach 	int ret;
44672944089SLucas Stach 
44772944089SLucas Stach 	ret = clk_prepare_enable(prg->clk_ipg);
44872944089SLucas Stach 	if (ret)
44972944089SLucas Stach 		return ret;
45072944089SLucas Stach 
45172944089SLucas Stach 	ret = clk_prepare_enable(prg->clk_axi);
45272944089SLucas Stach 	if (ret) {
45372944089SLucas Stach 		clk_disable_unprepare(prg->clk_ipg);
45472944089SLucas Stach 		return ret;
45572944089SLucas Stach 	}
45672944089SLucas Stach 
45772944089SLucas Stach 	return 0;
45872944089SLucas Stach }
45972944089SLucas Stach #endif
46072944089SLucas Stach 
46172944089SLucas Stach static const struct dev_pm_ops prg_pm_ops = {
46272944089SLucas Stach 	SET_RUNTIME_PM_OPS(prg_suspend, prg_resume, NULL)
46372944089SLucas Stach };
46472944089SLucas Stach 
465ea9c2605SLucas Stach static const struct of_device_id ipu_prg_dt_ids[] = {
466ea9c2605SLucas Stach 	{ .compatible = "fsl,imx6qp-prg", },
467ea9c2605SLucas Stach 	{ /* sentinel */ },
468ea9c2605SLucas Stach };
469ea9c2605SLucas Stach 
470ea9c2605SLucas Stach struct platform_driver ipu_prg_drv = {
471ea9c2605SLucas Stach 	.probe		= ipu_prg_probe,
472*4402a5aaSUwe Kleine-König 	.remove_new	= ipu_prg_remove,
473ea9c2605SLucas Stach 	.driver		= {
474ea9c2605SLucas Stach 		.name	= "imx-ipu-prg",
47572944089SLucas Stach 		.pm	= &prg_pm_ops,
476ea9c2605SLucas Stach 		.of_match_table = ipu_prg_dt_ids,
477ea9c2605SLucas Stach 	},
478ea9c2605SLucas Stach };
479