xref: /linux/drivers/gpu/ipu-v3/ipu-prg.c (revision 3addaba8141bc6a4f649a48f46e552af32922147)
1ea9c2605SLucas Stach /*
2ea9c2605SLucas Stach  * Copyright (c) 2016-2017 Lucas Stach, Pengutronix
3ea9c2605SLucas Stach  *
4ea9c2605SLucas Stach  * This program is free software; you can redistribute it and/or modify it
5ea9c2605SLucas Stach  * under the terms and conditions of the GNU General Public License,
6ea9c2605SLucas Stach  * version 2, as published by the Free Software Foundation.
7ea9c2605SLucas Stach  *
8ea9c2605SLucas Stach  * This program is distributed in the hope it will be useful, but WITHOUT
9ea9c2605SLucas Stach  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10ea9c2605SLucas Stach  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
11ea9c2605SLucas Stach  * more details.
12ea9c2605SLucas Stach  */
13ea9c2605SLucas Stach 
14ea9c2605SLucas Stach #include <drm/drm_fourcc.h>
15ea9c2605SLucas Stach #include <linux/clk.h>
16ea9c2605SLucas Stach #include <linux/err.h>
17263c3b80SLucas Stach #include <linux/iopoll.h>
18ea9c2605SLucas Stach #include <linux/mfd/syscon.h>
19ea9c2605SLucas Stach #include <linux/mfd/syscon/imx6q-iomuxc-gpr.h>
20ea9c2605SLucas Stach #include <linux/module.h>
21ea9c2605SLucas Stach #include <linux/of.h>
22ea9c2605SLucas Stach #include <linux/platform_device.h>
2372944089SLucas Stach #include <linux/pm_runtime.h>
24ea9c2605SLucas Stach #include <linux/regmap.h>
25ea9c2605SLucas Stach #include <video/imx-ipu-v3.h>
26ea9c2605SLucas Stach 
27ea9c2605SLucas Stach #include "ipu-prv.h"
28ea9c2605SLucas Stach 
29ea9c2605SLucas Stach #define IPU_PRG_CTL				0x00
30ea9c2605SLucas Stach #define  IPU_PRG_CTL_BYPASS(i)			(1 << (0 + i))
31ea9c2605SLucas Stach #define  IPU_PRG_CTL_SOFT_ARID_MASK		0x3
32ea9c2605SLucas Stach #define  IPU_PRG_CTL_SOFT_ARID_SHIFT(i)		(8 + i * 2)
33ea9c2605SLucas Stach #define  IPU_PRG_CTL_SOFT_ARID(i, v)		((v & 0x3) << (8 + 2 * i))
34ea9c2605SLucas Stach #define  IPU_PRG_CTL_SO(i)			(1 << (16 + i))
35ea9c2605SLucas Stach #define  IPU_PRG_CTL_VFLIP(i)			(1 << (19 + i))
36ea9c2605SLucas Stach #define  IPU_PRG_CTL_BLOCK_MODE(i)		(1 << (22 + i))
37ea9c2605SLucas Stach #define  IPU_PRG_CTL_CNT_LOAD_EN(i)		(1 << (25 + i))
38ea9c2605SLucas Stach #define  IPU_PRG_CTL_SOFTRST			(1 << 30)
39ea9c2605SLucas Stach #define  IPU_PRG_CTL_SHADOW_EN			(1 << 31)
40ea9c2605SLucas Stach 
41ea9c2605SLucas Stach #define IPU_PRG_STATUS				0x04
42ea9c2605SLucas Stach #define  IPU_PRG_STATUS_BUFFER0_READY(i)	(1 << (0 + i * 2))
43ea9c2605SLucas Stach #define  IPU_PRG_STATUS_BUFFER1_READY(i)	(1 << (1 + i * 2))
44ea9c2605SLucas Stach 
45ea9c2605SLucas Stach #define IPU_PRG_QOS				0x08
46ea9c2605SLucas Stach #define  IPU_PRG_QOS_ARID_MASK			0xf
47ea9c2605SLucas Stach #define  IPU_PRG_QOS_ARID_SHIFT(i)		(0 + i * 4)
48ea9c2605SLucas Stach 
49ea9c2605SLucas Stach #define IPU_PRG_REG_UPDATE			0x0c
50ea9c2605SLucas Stach #define  IPU_PRG_REG_UPDATE_REG_UPDATE		(1 << 0)
51ea9c2605SLucas Stach 
52ea9c2605SLucas Stach #define IPU_PRG_STRIDE(i)			(0x10 + i * 0x4)
53ea9c2605SLucas Stach #define  IPU_PRG_STRIDE_STRIDE_MASK		0x3fff
54ea9c2605SLucas Stach 
55ea9c2605SLucas Stach #define IPU_PRG_CROP_LINE			0x1c
56ea9c2605SLucas Stach 
57ea9c2605SLucas Stach #define IPU_PRG_THD				0x20
58ea9c2605SLucas Stach 
59ea9c2605SLucas Stach #define IPU_PRG_BADDR(i)			(0x24 + i * 0x4)
60ea9c2605SLucas Stach 
61ea9c2605SLucas Stach #define IPU_PRG_OFFSET(i)			(0x30 + i * 0x4)
62ea9c2605SLucas Stach 
63ea9c2605SLucas Stach #define IPU_PRG_ILO(i)				(0x3c + i * 0x4)
64ea9c2605SLucas Stach 
65ea9c2605SLucas Stach #define IPU_PRG_HEIGHT(i)			(0x48 + i * 0x4)
66ea9c2605SLucas Stach #define  IPU_PRG_HEIGHT_PRE_HEIGHT_MASK		0xfff
67ea9c2605SLucas Stach #define  IPU_PRG_HEIGHT_PRE_HEIGHT_SHIFT	0
68ea9c2605SLucas Stach #define  IPU_PRG_HEIGHT_IPU_HEIGHT_MASK		0xfff
69ea9c2605SLucas Stach #define  IPU_PRG_HEIGHT_IPU_HEIGHT_SHIFT	16
70ea9c2605SLucas Stach 
71ea9c2605SLucas Stach struct ipu_prg_channel {
72ea9c2605SLucas Stach 	bool			enabled;
73ea9c2605SLucas Stach 	int			used_pre;
74ea9c2605SLucas Stach };
75ea9c2605SLucas Stach 
76ea9c2605SLucas Stach struct ipu_prg {
77ea9c2605SLucas Stach 	struct list_head	list;
78ea9c2605SLucas Stach 	struct device		*dev;
79ea9c2605SLucas Stach 	int			id;
80ea9c2605SLucas Stach 
81ea9c2605SLucas Stach 	void __iomem		*regs;
82ea9c2605SLucas Stach 	struct clk		*clk_ipg, *clk_axi;
83ea9c2605SLucas Stach 	struct regmap		*iomuxc_gpr;
84ea9c2605SLucas Stach 	struct ipu_pre		*pres[3];
85ea9c2605SLucas Stach 
86ea9c2605SLucas Stach 	struct ipu_prg_channel	chan[3];
87ea9c2605SLucas Stach };
88ea9c2605SLucas Stach 
89ea9c2605SLucas Stach static DEFINE_MUTEX(ipu_prg_list_mutex);
90ea9c2605SLucas Stach static LIST_HEAD(ipu_prg_list);
91ea9c2605SLucas Stach 
92ea9c2605SLucas Stach struct ipu_prg *
93ea9c2605SLucas Stach ipu_prg_lookup_by_phandle(struct device *dev, const char *name, int ipu_id)
94ea9c2605SLucas Stach {
95ea9c2605SLucas Stach 	struct device_node *prg_node = of_parse_phandle(dev->of_node,
96ea9c2605SLucas Stach 							name, 0);
97ea9c2605SLucas Stach 	struct ipu_prg *prg;
98ea9c2605SLucas Stach 
99ea9c2605SLucas Stach 	mutex_lock(&ipu_prg_list_mutex);
100ea9c2605SLucas Stach 	list_for_each_entry(prg, &ipu_prg_list, list) {
101ea9c2605SLucas Stach 		if (prg_node == prg->dev->of_node) {
102ea9c2605SLucas Stach 			mutex_unlock(&ipu_prg_list_mutex);
103ea9c2605SLucas Stach 			device_link_add(dev, prg->dev, DL_FLAG_AUTOREMOVE);
104ea9c2605SLucas Stach 			prg->id = ipu_id;
105*3addaba8STobias Jordan 			of_node_put(prg_node);
106ea9c2605SLucas Stach 			return prg;
107ea9c2605SLucas Stach 		}
108ea9c2605SLucas Stach 	}
109ea9c2605SLucas Stach 	mutex_unlock(&ipu_prg_list_mutex);
110ea9c2605SLucas Stach 
111*3addaba8STobias Jordan 	of_node_put(prg_node);
112*3addaba8STobias Jordan 
113ea9c2605SLucas Stach 	return NULL;
114ea9c2605SLucas Stach }
115ea9c2605SLucas Stach 
116ea9c2605SLucas Stach int ipu_prg_max_active_channels(void)
117ea9c2605SLucas Stach {
118ea9c2605SLucas Stach 	return ipu_pre_get_available_count();
119ea9c2605SLucas Stach }
120ea9c2605SLucas Stach EXPORT_SYMBOL_GPL(ipu_prg_max_active_channels);
121ea9c2605SLucas Stach 
122ea9c2605SLucas Stach bool ipu_prg_present(struct ipu_soc *ipu)
123ea9c2605SLucas Stach {
124ea9c2605SLucas Stach 	if (ipu->prg_priv)
125ea9c2605SLucas Stach 		return true;
126ea9c2605SLucas Stach 
127ea9c2605SLucas Stach 	return false;
128ea9c2605SLucas Stach }
129ea9c2605SLucas Stach EXPORT_SYMBOL_GPL(ipu_prg_present);
130ea9c2605SLucas Stach 
131ea9c2605SLucas Stach bool ipu_prg_format_supported(struct ipu_soc *ipu, uint32_t format,
132ea9c2605SLucas Stach 			      uint64_t modifier)
133ea9c2605SLucas Stach {
134ea9c2605SLucas Stach 	const struct drm_format_info *info = drm_format_info(format);
135ea9c2605SLucas Stach 
136ea9c2605SLucas Stach 	if (info->num_planes != 1)
137ea9c2605SLucas Stach 		return false;
138ea9c2605SLucas Stach 
139a2ceec52SLucas Stach 	switch (modifier) {
140a2ceec52SLucas Stach 	case DRM_FORMAT_MOD_LINEAR:
141a2ceec52SLucas Stach 	case DRM_FORMAT_MOD_VIVANTE_TILED:
142a2ceec52SLucas Stach 	case DRM_FORMAT_MOD_VIVANTE_SUPER_TILED:
143ea9c2605SLucas Stach 		return true;
144a2ceec52SLucas Stach 	default:
145a2ceec52SLucas Stach 		return false;
146a2ceec52SLucas Stach 	}
147ea9c2605SLucas Stach }
148ea9c2605SLucas Stach EXPORT_SYMBOL_GPL(ipu_prg_format_supported);
149ea9c2605SLucas Stach 
150ea9c2605SLucas Stach int ipu_prg_enable(struct ipu_soc *ipu)
151ea9c2605SLucas Stach {
152ea9c2605SLucas Stach 	struct ipu_prg *prg = ipu->prg_priv;
153ea9c2605SLucas Stach 
154ea9c2605SLucas Stach 	if (!prg)
155ea9c2605SLucas Stach 		return 0;
156ea9c2605SLucas Stach 
15772944089SLucas Stach 	return pm_runtime_get_sync(prg->dev);
158ea9c2605SLucas Stach }
159ea9c2605SLucas Stach EXPORT_SYMBOL_GPL(ipu_prg_enable);
160ea9c2605SLucas Stach 
161ea9c2605SLucas Stach void ipu_prg_disable(struct ipu_soc *ipu)
162ea9c2605SLucas Stach {
163ea9c2605SLucas Stach 	struct ipu_prg *prg = ipu->prg_priv;
164ea9c2605SLucas Stach 
165ea9c2605SLucas Stach 	if (!prg)
166ea9c2605SLucas Stach 		return;
167ea9c2605SLucas Stach 
16872944089SLucas Stach 	pm_runtime_put(prg->dev);
169ea9c2605SLucas Stach }
170ea9c2605SLucas Stach EXPORT_SYMBOL_GPL(ipu_prg_disable);
171ea9c2605SLucas Stach 
172ea9c2605SLucas Stach /*
173ea9c2605SLucas Stach  * The channel configuartion functions below are not thread safe, as they
174ea9c2605SLucas Stach  * must be only called from the atomic commit path in the DRM driver, which
175ea9c2605SLucas Stach  * is properly serialized.
176ea9c2605SLucas Stach  */
177ea9c2605SLucas Stach static int ipu_prg_ipu_to_prg_chan(int ipu_chan)
178ea9c2605SLucas Stach {
179ea9c2605SLucas Stach 	/*
180ea9c2605SLucas Stach 	 * This isn't clearly documented in the RM, but IPU to PRG channel
181ea9c2605SLucas Stach 	 * assignment is fixed, as only with this mapping the control signals
182ea9c2605SLucas Stach 	 * match up.
183ea9c2605SLucas Stach 	 */
184ea9c2605SLucas Stach 	switch (ipu_chan) {
185ea9c2605SLucas Stach 	case IPUV3_CHANNEL_MEM_BG_SYNC:
186ea9c2605SLucas Stach 		return 0;
187ea9c2605SLucas Stach 	case IPUV3_CHANNEL_MEM_FG_SYNC:
188ea9c2605SLucas Stach 		return 1;
189ea9c2605SLucas Stach 	case IPUV3_CHANNEL_MEM_DC_SYNC:
190ea9c2605SLucas Stach 		return 2;
191ea9c2605SLucas Stach 	default:
192ea9c2605SLucas Stach 		return -EINVAL;
193ea9c2605SLucas Stach 	}
194ea9c2605SLucas Stach }
195ea9c2605SLucas Stach 
196ea9c2605SLucas Stach static int ipu_prg_get_pre(struct ipu_prg *prg, int prg_chan)
197ea9c2605SLucas Stach {
198ea9c2605SLucas Stach 	int i, ret;
199ea9c2605SLucas Stach 
200ea9c2605SLucas Stach 	/* channel 0 is special as it is hardwired to one of the PREs */
201ea9c2605SLucas Stach 	if (prg_chan == 0) {
202ea9c2605SLucas Stach 		ret = ipu_pre_get(prg->pres[0]);
203ea9c2605SLucas Stach 		if (ret)
204ea9c2605SLucas Stach 			goto fail;
205ea9c2605SLucas Stach 		prg->chan[prg_chan].used_pre = 0;
206ea9c2605SLucas Stach 		return 0;
207ea9c2605SLucas Stach 	}
208ea9c2605SLucas Stach 
209ea9c2605SLucas Stach 	for (i = 1; i < 3; i++) {
210ea9c2605SLucas Stach 		ret = ipu_pre_get(prg->pres[i]);
211ea9c2605SLucas Stach 		if (!ret) {
212ea9c2605SLucas Stach 			u32 val, mux;
213ea9c2605SLucas Stach 			int shift;
214ea9c2605SLucas Stach 
215ea9c2605SLucas Stach 			prg->chan[prg_chan].used_pre = i;
216ea9c2605SLucas Stach 
217ea9c2605SLucas Stach 			/* configure the PRE to PRG channel mux */
218ea9c2605SLucas Stach 			shift = (i == 1) ? 12 : 14;
219ea9c2605SLucas Stach 			mux = (prg->id << 1) | (prg_chan - 1);
220ea9c2605SLucas Stach 			regmap_update_bits(prg->iomuxc_gpr, IOMUXC_GPR5,
221ea9c2605SLucas Stach 					   0x3 << shift, mux << shift);
222ea9c2605SLucas Stach 
223ea9c2605SLucas Stach 			/* check other mux, must not point to same channel */
224ea9c2605SLucas Stach 			shift = (i == 1) ? 14 : 12;
225ea9c2605SLucas Stach 			regmap_read(prg->iomuxc_gpr, IOMUXC_GPR5, &val);
226ea9c2605SLucas Stach 			if (((val >> shift) & 0x3) == mux) {
227ea9c2605SLucas Stach 				regmap_update_bits(prg->iomuxc_gpr, IOMUXC_GPR5,
228ea9c2605SLucas Stach 						   0x3 << shift,
229ea9c2605SLucas Stach 						   (mux ^ 0x1) << shift);
230ea9c2605SLucas Stach 			}
231ea9c2605SLucas Stach 
232ea9c2605SLucas Stach 			return 0;
233ea9c2605SLucas Stach 		}
234ea9c2605SLucas Stach 	}
235ea9c2605SLucas Stach 
236ea9c2605SLucas Stach fail:
237ea9c2605SLucas Stach 	dev_err(prg->dev, "could not get PRE for PRG chan %d", prg_chan);
238ea9c2605SLucas Stach 	return ret;
239ea9c2605SLucas Stach }
240ea9c2605SLucas Stach 
241ea9c2605SLucas Stach static void ipu_prg_put_pre(struct ipu_prg *prg, int prg_chan)
242ea9c2605SLucas Stach {
243ea9c2605SLucas Stach 	struct ipu_prg_channel *chan = &prg->chan[prg_chan];
244ea9c2605SLucas Stach 
245ea9c2605SLucas Stach 	ipu_pre_put(prg->pres[chan->used_pre]);
246ea9c2605SLucas Stach 	chan->used_pre = -1;
247ea9c2605SLucas Stach }
248ea9c2605SLucas Stach 
249ea9c2605SLucas Stach void ipu_prg_channel_disable(struct ipuv3_channel *ipu_chan)
250ea9c2605SLucas Stach {
251ea9c2605SLucas Stach 	int prg_chan = ipu_prg_ipu_to_prg_chan(ipu_chan->num);
252ea9c2605SLucas Stach 	struct ipu_prg *prg = ipu_chan->ipu->prg_priv;
253ea9c2605SLucas Stach 	struct ipu_prg_channel *chan = &prg->chan[prg_chan];
254ea9c2605SLucas Stach 	u32 val;
255ea9c2605SLucas Stach 
256ea9c2605SLucas Stach 	if (!chan->enabled || prg_chan < 0)
257ea9c2605SLucas Stach 		return;
258ea9c2605SLucas Stach 
25972944089SLucas Stach 	pm_runtime_get_sync(prg->dev);
260ea9c2605SLucas Stach 
261ea9c2605SLucas Stach 	val = readl(prg->regs + IPU_PRG_CTL);
262ea9c2605SLucas Stach 	val |= IPU_PRG_CTL_BYPASS(prg_chan);
263ea9c2605SLucas Stach 	writel(val, prg->regs + IPU_PRG_CTL);
264ea9c2605SLucas Stach 
265ea9c2605SLucas Stach 	val = IPU_PRG_REG_UPDATE_REG_UPDATE;
266ea9c2605SLucas Stach 	writel(val, prg->regs + IPU_PRG_REG_UPDATE);
267ea9c2605SLucas Stach 
26872944089SLucas Stach 	pm_runtime_put(prg->dev);
269ea9c2605SLucas Stach 
270ea9c2605SLucas Stach 	ipu_prg_put_pre(prg, prg_chan);
271ea9c2605SLucas Stach 
272ea9c2605SLucas Stach 	chan->enabled = false;
273ea9c2605SLucas Stach }
274ea9c2605SLucas Stach EXPORT_SYMBOL_GPL(ipu_prg_channel_disable);
275ea9c2605SLucas Stach 
276ea9c2605SLucas Stach int ipu_prg_channel_configure(struct ipuv3_channel *ipu_chan,
277ea9c2605SLucas Stach 			      unsigned int axi_id, unsigned int width,
278ea9c2605SLucas Stach 			      unsigned int height, unsigned int stride,
279a2ceec52SLucas Stach 			      u32 format, uint64_t modifier, unsigned long *eba)
280ea9c2605SLucas Stach {
281ea9c2605SLucas Stach 	int prg_chan = ipu_prg_ipu_to_prg_chan(ipu_chan->num);
282ea9c2605SLucas Stach 	struct ipu_prg *prg = ipu_chan->ipu->prg_priv;
283ea9c2605SLucas Stach 	struct ipu_prg_channel *chan = &prg->chan[prg_chan];
284ea9c2605SLucas Stach 	u32 val;
285ea9c2605SLucas Stach 	int ret;
286ea9c2605SLucas Stach 
287ea9c2605SLucas Stach 	if (prg_chan < 0)
288ea9c2605SLucas Stach 		return prg_chan;
289ea9c2605SLucas Stach 
290ea9c2605SLucas Stach 	if (chan->enabled) {
291ea9c2605SLucas Stach 		ipu_pre_update(prg->pres[chan->used_pre], *eba);
292ea9c2605SLucas Stach 		return 0;
293ea9c2605SLucas Stach 	}
294ea9c2605SLucas Stach 
295ea9c2605SLucas Stach 	ret = ipu_prg_get_pre(prg, prg_chan);
296ea9c2605SLucas Stach 	if (ret)
297ea9c2605SLucas Stach 		return ret;
298ea9c2605SLucas Stach 
299ea9c2605SLucas Stach 	ipu_pre_configure(prg->pres[chan->used_pre],
300a2ceec52SLucas Stach 			  width, height, stride, format, modifier, *eba);
301ea9c2605SLucas Stach 
302ea9c2605SLucas Stach 
30372944089SLucas Stach 	pm_runtime_get_sync(prg->dev);
304ea9c2605SLucas Stach 
305ea9c2605SLucas Stach 	val = (stride - 1) & IPU_PRG_STRIDE_STRIDE_MASK;
306ea9c2605SLucas Stach 	writel(val, prg->regs + IPU_PRG_STRIDE(prg_chan));
307ea9c2605SLucas Stach 
308ea9c2605SLucas Stach 	val = ((height & IPU_PRG_HEIGHT_PRE_HEIGHT_MASK) <<
309ea9c2605SLucas Stach 	       IPU_PRG_HEIGHT_PRE_HEIGHT_SHIFT) |
310ea9c2605SLucas Stach 	      ((height & IPU_PRG_HEIGHT_IPU_HEIGHT_MASK) <<
311ea9c2605SLucas Stach 	       IPU_PRG_HEIGHT_IPU_HEIGHT_SHIFT);
312ea9c2605SLucas Stach 	writel(val, prg->regs + IPU_PRG_HEIGHT(prg_chan));
313ea9c2605SLucas Stach 
314ea9c2605SLucas Stach 	val = ipu_pre_get_baddr(prg->pres[chan->used_pre]);
315ea9c2605SLucas Stach 	*eba = val;
316ea9c2605SLucas Stach 	writel(val, prg->regs + IPU_PRG_BADDR(prg_chan));
317ea9c2605SLucas Stach 
318ea9c2605SLucas Stach 	val = readl(prg->regs + IPU_PRG_CTL);
319ea9c2605SLucas Stach 	/* config AXI ID */
320ea9c2605SLucas Stach 	val &= ~(IPU_PRG_CTL_SOFT_ARID_MASK <<
321ea9c2605SLucas Stach 		 IPU_PRG_CTL_SOFT_ARID_SHIFT(prg_chan));
322ea9c2605SLucas Stach 	val |= IPU_PRG_CTL_SOFT_ARID(prg_chan, axi_id);
323ea9c2605SLucas Stach 	/* enable channel */
324ea9c2605SLucas Stach 	val &= ~IPU_PRG_CTL_BYPASS(prg_chan);
325ea9c2605SLucas Stach 	writel(val, prg->regs + IPU_PRG_CTL);
326ea9c2605SLucas Stach 
327ea9c2605SLucas Stach 	val = IPU_PRG_REG_UPDATE_REG_UPDATE;
328ea9c2605SLucas Stach 	writel(val, prg->regs + IPU_PRG_REG_UPDATE);
329ea9c2605SLucas Stach 
330263c3b80SLucas Stach 	/* wait for both double buffers to be filled */
331263c3b80SLucas Stach 	readl_poll_timeout(prg->regs + IPU_PRG_STATUS, val,
332263c3b80SLucas Stach 			   (val & IPU_PRG_STATUS_BUFFER0_READY(prg_chan)) &&
333263c3b80SLucas Stach 			   (val & IPU_PRG_STATUS_BUFFER1_READY(prg_chan)),
334263c3b80SLucas Stach 			   5, 1000);
335263c3b80SLucas Stach 
33672944089SLucas Stach 	pm_runtime_put(prg->dev);
337ea9c2605SLucas Stach 
338ea9c2605SLucas Stach 	chan->enabled = true;
339ea9c2605SLucas Stach 	return 0;
340ea9c2605SLucas Stach }
341ea9c2605SLucas Stach EXPORT_SYMBOL_GPL(ipu_prg_channel_configure);
342ea9c2605SLucas Stach 
343ea9c2605SLucas Stach static int ipu_prg_probe(struct platform_device *pdev)
344ea9c2605SLucas Stach {
345ea9c2605SLucas Stach 	struct device *dev = &pdev->dev;
346ea9c2605SLucas Stach 	struct resource *res;
347ea9c2605SLucas Stach 	struct ipu_prg *prg;
348ea9c2605SLucas Stach 	u32 val;
349ea9c2605SLucas Stach 	int i, ret;
350ea9c2605SLucas Stach 
351ea9c2605SLucas Stach 	prg = devm_kzalloc(dev, sizeof(*prg), GFP_KERNEL);
352ea9c2605SLucas Stach 	if (!prg)
353ea9c2605SLucas Stach 		return -ENOMEM;
354ea9c2605SLucas Stach 
355ea9c2605SLucas Stach 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
356ea9c2605SLucas Stach 	prg->regs = devm_ioremap_resource(&pdev->dev, res);
357ea9c2605SLucas Stach 	if (IS_ERR(prg->regs))
358ea9c2605SLucas Stach 		return PTR_ERR(prg->regs);
359ea9c2605SLucas Stach 
360ea9c2605SLucas Stach 
361ea9c2605SLucas Stach 	prg->clk_ipg = devm_clk_get(dev, "ipg");
362ea9c2605SLucas Stach 	if (IS_ERR(prg->clk_ipg))
363ea9c2605SLucas Stach 		return PTR_ERR(prg->clk_ipg);
364ea9c2605SLucas Stach 
365ea9c2605SLucas Stach 	prg->clk_axi = devm_clk_get(dev, "axi");
366ea9c2605SLucas Stach 	if (IS_ERR(prg->clk_axi))
367ea9c2605SLucas Stach 		return PTR_ERR(prg->clk_axi);
368ea9c2605SLucas Stach 
369ea9c2605SLucas Stach 	prg->iomuxc_gpr =
370ea9c2605SLucas Stach 		syscon_regmap_lookup_by_compatible("fsl,imx6q-iomuxc-gpr");
371ea9c2605SLucas Stach 	if (IS_ERR(prg->iomuxc_gpr))
372ea9c2605SLucas Stach 		return PTR_ERR(prg->iomuxc_gpr);
373ea9c2605SLucas Stach 
374ea9c2605SLucas Stach 	for (i = 0; i < 3; i++) {
375ea9c2605SLucas Stach 		prg->pres[i] = ipu_pre_lookup_by_phandle(dev, "fsl,pres", i);
376ea9c2605SLucas Stach 		if (!prg->pres[i])
377ea9c2605SLucas Stach 			return -EPROBE_DEFER;
378ea9c2605SLucas Stach 	}
379ea9c2605SLucas Stach 
380ea9c2605SLucas Stach 	ret = clk_prepare_enable(prg->clk_ipg);
381ea9c2605SLucas Stach 	if (ret)
382ea9c2605SLucas Stach 		return ret;
383ea9c2605SLucas Stach 
38472944089SLucas Stach 	ret = clk_prepare_enable(prg->clk_axi);
38572944089SLucas Stach 	if (ret) {
38672944089SLucas Stach 		clk_disable_unprepare(prg->clk_ipg);
38772944089SLucas Stach 		return ret;
38872944089SLucas Stach 	}
38972944089SLucas Stach 
390ea9c2605SLucas Stach 	/* init to free running mode */
391ea9c2605SLucas Stach 	val = readl(prg->regs + IPU_PRG_CTL);
392ea9c2605SLucas Stach 	val |= IPU_PRG_CTL_SHADOW_EN;
393ea9c2605SLucas Stach 	writel(val, prg->regs + IPU_PRG_CTL);
394ea9c2605SLucas Stach 
395ea9c2605SLucas Stach 	/* disable address threshold */
396ea9c2605SLucas Stach 	writel(0xffffffff, prg->regs + IPU_PRG_THD);
397ea9c2605SLucas Stach 
39872944089SLucas Stach 	pm_runtime_set_active(dev);
39972944089SLucas Stach 	pm_runtime_enable(dev);
400ea9c2605SLucas Stach 
401ea9c2605SLucas Stach 	prg->dev = dev;
402ea9c2605SLucas Stach 	platform_set_drvdata(pdev, prg);
403ea9c2605SLucas Stach 	mutex_lock(&ipu_prg_list_mutex);
404ea9c2605SLucas Stach 	list_add(&prg->list, &ipu_prg_list);
405ea9c2605SLucas Stach 	mutex_unlock(&ipu_prg_list_mutex);
406ea9c2605SLucas Stach 
407ea9c2605SLucas Stach 	return 0;
408ea9c2605SLucas Stach }
409ea9c2605SLucas Stach 
410ea9c2605SLucas Stach static int ipu_prg_remove(struct platform_device *pdev)
411ea9c2605SLucas Stach {
412ea9c2605SLucas Stach 	struct ipu_prg *prg = platform_get_drvdata(pdev);
413ea9c2605SLucas Stach 
414ea9c2605SLucas Stach 	mutex_lock(&ipu_prg_list_mutex);
415ea9c2605SLucas Stach 	list_del(&prg->list);
416ea9c2605SLucas Stach 	mutex_unlock(&ipu_prg_list_mutex);
417ea9c2605SLucas Stach 
418ea9c2605SLucas Stach 	return 0;
419ea9c2605SLucas Stach }
420ea9c2605SLucas Stach 
42172944089SLucas Stach #ifdef CONFIG_PM
42272944089SLucas Stach static int prg_suspend(struct device *dev)
42372944089SLucas Stach {
42472944089SLucas Stach 	struct ipu_prg *prg = dev_get_drvdata(dev);
42572944089SLucas Stach 
42672944089SLucas Stach 	clk_disable_unprepare(prg->clk_axi);
42772944089SLucas Stach 	clk_disable_unprepare(prg->clk_ipg);
42872944089SLucas Stach 
42972944089SLucas Stach 	return 0;
43072944089SLucas Stach }
43172944089SLucas Stach 
43272944089SLucas Stach static int prg_resume(struct device *dev)
43372944089SLucas Stach {
43472944089SLucas Stach 	struct ipu_prg *prg = dev_get_drvdata(dev);
43572944089SLucas Stach 	int ret;
43672944089SLucas Stach 
43772944089SLucas Stach 	ret = clk_prepare_enable(prg->clk_ipg);
43872944089SLucas Stach 	if (ret)
43972944089SLucas Stach 		return ret;
44072944089SLucas Stach 
44172944089SLucas Stach 	ret = clk_prepare_enable(prg->clk_axi);
44272944089SLucas Stach 	if (ret) {
44372944089SLucas Stach 		clk_disable_unprepare(prg->clk_ipg);
44472944089SLucas Stach 		return ret;
44572944089SLucas Stach 	}
44672944089SLucas Stach 
44772944089SLucas Stach 	return 0;
44872944089SLucas Stach }
44972944089SLucas Stach #endif
45072944089SLucas Stach 
45172944089SLucas Stach static const struct dev_pm_ops prg_pm_ops = {
45272944089SLucas Stach 	SET_RUNTIME_PM_OPS(prg_suspend, prg_resume, NULL)
45372944089SLucas Stach };
45472944089SLucas Stach 
455ea9c2605SLucas Stach static const struct of_device_id ipu_prg_dt_ids[] = {
456ea9c2605SLucas Stach 	{ .compatible = "fsl,imx6qp-prg", },
457ea9c2605SLucas Stach 	{ /* sentinel */ },
458ea9c2605SLucas Stach };
459ea9c2605SLucas Stach 
460ea9c2605SLucas Stach struct platform_driver ipu_prg_drv = {
461ea9c2605SLucas Stach 	.probe		= ipu_prg_probe,
462ea9c2605SLucas Stach 	.remove		= ipu_prg_remove,
463ea9c2605SLucas Stach 	.driver		= {
464ea9c2605SLucas Stach 		.name	= "imx-ipu-prg",
46572944089SLucas Stach 		.pm	= &prg_pm_ops,
466ea9c2605SLucas Stach 		.of_match_table = ipu_prg_dt_ids,
467ea9c2605SLucas Stach 	},
468ea9c2605SLucas Stach };
469