xref: /linux/drivers/gpu/ipu-v3/ipu-pre.c (revision a1ff5a7d78a036d6c2178ee5acd6ba4946243800)
12025cf9eSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2d2a34232SLucas Stach /*
3d2a34232SLucas Stach  * Copyright (c) 2017 Lucas Stach, Pengutronix
4d2a34232SLucas Stach  */
5d2a34232SLucas Stach 
6d2a34232SLucas Stach #include <drm/drm_fourcc.h>
7d2a34232SLucas Stach #include <linux/clk.h>
8d5316cddSLucas Stach #include <linux/delay.h>
9d2a34232SLucas Stach #include <linux/err.h>
10d2a34232SLucas Stach #include <linux/genalloc.h>
11d2a34232SLucas Stach #include <linux/module.h>
12d2a34232SLucas Stach #include <linux/of.h>
13d2a34232SLucas Stach #include <linux/platform_device.h>
14d2a34232SLucas Stach #include <video/imx-ipu-v3.h>
15d2a34232SLucas Stach 
16d2a34232SLucas Stach #include "ipu-prv.h"
17d2a34232SLucas Stach 
18d2a34232SLucas Stach #define IPU_PRE_MAX_WIDTH	2048
19d2a34232SLucas Stach #define IPU_PRE_NUM_SCANLINES	8
20d2a34232SLucas Stach 
21d2a34232SLucas Stach #define IPU_PRE_CTRL					0x000
22d2a34232SLucas Stach #define IPU_PRE_CTRL_SET				0x004
23d2a34232SLucas Stach #define  IPU_PRE_CTRL_ENABLE				(1 << 0)
24d2a34232SLucas Stach #define  IPU_PRE_CTRL_BLOCK_EN				(1 << 1)
25d2a34232SLucas Stach #define  IPU_PRE_CTRL_BLOCK_16				(1 << 2)
26d2a34232SLucas Stach #define  IPU_PRE_CTRL_SDW_UPDATE			(1 << 4)
27d2a34232SLucas Stach #define  IPU_PRE_CTRL_VFLIP				(1 << 5)
28d2a34232SLucas Stach #define  IPU_PRE_CTRL_SO				(1 << 6)
29d2a34232SLucas Stach #define  IPU_PRE_CTRL_INTERLACED_FIELD			(1 << 7)
30d2a34232SLucas Stach #define  IPU_PRE_CTRL_HANDSHAKE_EN			(1 << 8)
31d2a34232SLucas Stach #define  IPU_PRE_CTRL_HANDSHAKE_LINE_NUM(v)		((v & 0x3) << 9)
32d2a34232SLucas Stach #define  IPU_PRE_CTRL_HANDSHAKE_ABORT_SKIP_EN		(1 << 11)
33d2a34232SLucas Stach #define  IPU_PRE_CTRL_EN_REPEAT				(1 << 28)
34d2a34232SLucas Stach #define  IPU_PRE_CTRL_TPR_REST_SEL			(1 << 29)
35d2a34232SLucas Stach #define  IPU_PRE_CTRL_CLKGATE				(1 << 30)
36d2a34232SLucas Stach #define  IPU_PRE_CTRL_SFTRST				(1 << 31)
37d2a34232SLucas Stach 
38d2a34232SLucas Stach #define IPU_PRE_CUR_BUF					0x030
39d2a34232SLucas Stach 
40d2a34232SLucas Stach #define IPU_PRE_NEXT_BUF				0x040
41d2a34232SLucas Stach 
42d2a34232SLucas Stach #define IPU_PRE_TPR_CTRL				0x070
43d2a34232SLucas Stach #define  IPU_PRE_TPR_CTRL_TILE_FORMAT(v)		((v & 0xff) << 0)
44d2a34232SLucas Stach #define  IPU_PRE_TPR_CTRL_TILE_FORMAT_MASK		0xff
452f64a554SLucas Stach #define  IPU_PRE_TPR_CTRL_TILE_FORMAT_16_BIT		(1 << 0)
462f64a554SLucas Stach #define  IPU_PRE_TPR_CTRL_TILE_FORMAT_SPLIT_BUF		(1 << 4)
472f64a554SLucas Stach #define  IPU_PRE_TPR_CTRL_TILE_FORMAT_SINGLE_BUF	(1 << 5)
482f64a554SLucas Stach #define  IPU_PRE_TPR_CTRL_TILE_FORMAT_SUPER_TILED	(1 << 6)
49d2a34232SLucas Stach 
50d2a34232SLucas Stach #define IPU_PRE_PREFETCH_ENG_CTRL			0x080
51d2a34232SLucas Stach #define  IPU_PRE_PREF_ENG_CTRL_PREFETCH_EN		(1 << 0)
52d2a34232SLucas Stach #define  IPU_PRE_PREF_ENG_CTRL_RD_NUM_BYTES(v)		((v & 0x7) << 1)
53d2a34232SLucas Stach #define  IPU_PRE_PREF_ENG_CTRL_INPUT_ACTIVE_BPP(v)	((v & 0x3) << 4)
54d2a34232SLucas Stach #define  IPU_PRE_PREF_ENG_CTRL_INPUT_PIXEL_FORMAT(v)	((v & 0x7) << 8)
55d2a34232SLucas Stach #define  IPU_PRE_PREF_ENG_CTRL_SHIFT_BYPASS		(1 << 11)
56d2a34232SLucas Stach #define  IPU_PRE_PREF_ENG_CTRL_FIELD_INVERSE		(1 << 12)
57d2a34232SLucas Stach #define  IPU_PRE_PREF_ENG_CTRL_PARTIAL_UV_SWAP		(1 << 14)
58d2a34232SLucas Stach #define  IPU_PRE_PREF_ENG_CTRL_TPR_COOR_OFFSET_EN	(1 << 15)
59d2a34232SLucas Stach 
60d2a34232SLucas Stach #define IPU_PRE_PREFETCH_ENG_INPUT_SIZE			0x0a0
61d2a34232SLucas Stach #define  IPU_PRE_PREFETCH_ENG_INPUT_SIZE_WIDTH(v)	((v & 0xffff) << 0)
62d2a34232SLucas Stach #define  IPU_PRE_PREFETCH_ENG_INPUT_SIZE_HEIGHT(v)	((v & 0xffff) << 16)
63d2a34232SLucas Stach 
64d2a34232SLucas Stach #define IPU_PRE_PREFETCH_ENG_PITCH			0x0d0
65d2a34232SLucas Stach #define  IPU_PRE_PREFETCH_ENG_PITCH_Y(v)		((v & 0xffff) << 0)
66d2a34232SLucas Stach #define  IPU_PRE_PREFETCH_ENG_PITCH_UV(v)		((v & 0xffff) << 16)
67d2a34232SLucas Stach 
68d2a34232SLucas Stach #define IPU_PRE_STORE_ENG_CTRL				0x110
69d2a34232SLucas Stach #define  IPU_PRE_STORE_ENG_CTRL_STORE_EN		(1 << 0)
70d2a34232SLucas Stach #define  IPU_PRE_STORE_ENG_CTRL_WR_NUM_BYTES(v)		((v & 0x7) << 1)
71d2a34232SLucas Stach #define  IPU_PRE_STORE_ENG_CTRL_OUTPUT_ACTIVE_BPP(v)	((v & 0x3) << 4)
72d2a34232SLucas Stach 
7311aff4b4SLucas Stach #define IPU_PRE_STORE_ENG_STATUS			0x120
7411aff4b4SLucas Stach #define  IPU_PRE_STORE_ENG_STATUS_STORE_BLOCK_X_MASK	0xffff
7511aff4b4SLucas Stach #define  IPU_PRE_STORE_ENG_STATUS_STORE_BLOCK_X_SHIFT	0
7611aff4b4SLucas Stach #define  IPU_PRE_STORE_ENG_STATUS_STORE_BLOCK_Y_MASK	0x3fff
7711aff4b4SLucas Stach #define  IPU_PRE_STORE_ENG_STATUS_STORE_BLOCK_Y_SHIFT	16
7811aff4b4SLucas Stach #define  IPU_PRE_STORE_ENG_STATUS_STORE_FIFO_FULL	(1 << 30)
7911aff4b4SLucas Stach #define  IPU_PRE_STORE_ENG_STATUS_STORE_FIELD		(1 << 31)
8011aff4b4SLucas Stach 
81d2a34232SLucas Stach #define IPU_PRE_STORE_ENG_SIZE				0x130
82d2a34232SLucas Stach #define  IPU_PRE_STORE_ENG_SIZE_INPUT_WIDTH(v)		((v & 0xffff) << 0)
83d2a34232SLucas Stach #define  IPU_PRE_STORE_ENG_SIZE_INPUT_HEIGHT(v)		((v & 0xffff) << 16)
84d2a34232SLucas Stach 
85d2a34232SLucas Stach #define IPU_PRE_STORE_ENG_PITCH				0x140
86d2a34232SLucas Stach #define  IPU_PRE_STORE_ENG_PITCH_OUT_PITCH(v)		((v & 0xffff) << 0)
87d2a34232SLucas Stach 
88d2a34232SLucas Stach #define IPU_PRE_STORE_ENG_ADDR				0x150
89d2a34232SLucas Stach 
90d2a34232SLucas Stach struct ipu_pre {
91d2a34232SLucas Stach 	struct list_head	list;
92d2a34232SLucas Stach 	struct device		*dev;
93d2a34232SLucas Stach 
94d2a34232SLucas Stach 	void __iomem		*regs;
95d2a34232SLucas Stach 	struct clk		*clk_axi;
96d2a34232SLucas Stach 	struct gen_pool		*iram;
97d2a34232SLucas Stach 
98d2a34232SLucas Stach 	dma_addr_t		buffer_paddr;
99d2a34232SLucas Stach 	void			*buffer_virt;
100ee7ff5e2SLucas Stach 
101ee7ff5e2SLucas Stach 	struct {
102d2a34232SLucas Stach 		bool		in_use;
1034dbc7d5dSLucas Stach 		uint64_t	modifier;
1044dbc7d5dSLucas Stach 		unsigned int	height;
10511aff4b4SLucas Stach 		unsigned int	safe_window_end;
106ee7ff5e2SLucas Stach 		unsigned int	bufaddr;
1074dbc7d5dSLucas Stach 		u32		ctrl;
1084dbc7d5dSLucas Stach 		u8		cpp;
109ee7ff5e2SLucas Stach 	} cur;
110d2a34232SLucas Stach };
111d2a34232SLucas Stach 
112d2a34232SLucas Stach static DEFINE_MUTEX(ipu_pre_list_mutex);
113d2a34232SLucas Stach static LIST_HEAD(ipu_pre_list);
114d2a34232SLucas Stach static int available_pres;
115d2a34232SLucas Stach 
ipu_pre_get_available_count(void)116d2a34232SLucas Stach int ipu_pre_get_available_count(void)
117d2a34232SLucas Stach {
118d2a34232SLucas Stach 	return available_pres;
119d2a34232SLucas Stach }
120d2a34232SLucas Stach 
121d2a34232SLucas Stach struct ipu_pre *
ipu_pre_lookup_by_phandle(struct device * dev,const char * name,int index)122d2a34232SLucas Stach ipu_pre_lookup_by_phandle(struct device *dev, const char *name, int index)
123d2a34232SLucas Stach {
12481112c6fSR Sundar 	struct device_node *pre_node __free(device_node) =
12581112c6fSR Sundar 		of_parse_phandle(dev->of_node, name, index);
126d2a34232SLucas Stach 	struct ipu_pre *pre;
127d2a34232SLucas Stach 
128d2a34232SLucas Stach 	mutex_lock(&ipu_pre_list_mutex);
129d2a34232SLucas Stach 	list_for_each_entry(pre, &ipu_pre_list, list) {
130d2a34232SLucas Stach 		if (pre_node == pre->dev->of_node) {
131d2a34232SLucas Stach 			mutex_unlock(&ipu_pre_list_mutex);
132e88728f4SVivek Gautam 			device_link_add(dev, pre->dev,
133e88728f4SVivek Gautam 					DL_FLAG_AUTOREMOVE_CONSUMER);
134d2a34232SLucas Stach 			return pre;
135d2a34232SLucas Stach 		}
136d2a34232SLucas Stach 	}
137d2a34232SLucas Stach 	mutex_unlock(&ipu_pre_list_mutex);
138d2a34232SLucas Stach 
139d2a34232SLucas Stach 	return NULL;
140d2a34232SLucas Stach }
141d2a34232SLucas Stach 
ipu_pre_get(struct ipu_pre * pre)142d2a34232SLucas Stach int ipu_pre_get(struct ipu_pre *pre)
143d2a34232SLucas Stach {
144d2a34232SLucas Stach 	u32 val;
145d2a34232SLucas Stach 
146ee7ff5e2SLucas Stach 	if (pre->cur.in_use)
147d2a34232SLucas Stach 		return -EBUSY;
148d2a34232SLucas Stach 
149d2a34232SLucas Stach 	/* first get the engine out of reset and remove clock gating */
150d2a34232SLucas Stach 	writel(0, pre->regs + IPU_PRE_CTRL);
151d2a34232SLucas Stach 
152d2a34232SLucas Stach 	/* init defaults that should be applied to all streams */
153d2a34232SLucas Stach 	val = IPU_PRE_CTRL_HANDSHAKE_ABORT_SKIP_EN |
154d2a34232SLucas Stach 	      IPU_PRE_CTRL_HANDSHAKE_EN |
155d2a34232SLucas Stach 	      IPU_PRE_CTRL_TPR_REST_SEL |
1562f64a554SLucas Stach 	      IPU_PRE_CTRL_SDW_UPDATE;
157d2a34232SLucas Stach 	writel(val, pre->regs + IPU_PRE_CTRL);
158d2a34232SLucas Stach 
159ee7ff5e2SLucas Stach 	pre->cur.in_use = true;
160d2a34232SLucas Stach 	return 0;
161d2a34232SLucas Stach }
162d2a34232SLucas Stach 
ipu_pre_put(struct ipu_pre * pre)163d2a34232SLucas Stach void ipu_pre_put(struct ipu_pre *pre)
164d2a34232SLucas Stach {
16547c298f7SLucas Stach 	writel(IPU_PRE_CTRL_SFTRST, pre->regs + IPU_PRE_CTRL);
166d2a34232SLucas Stach 
167ee7ff5e2SLucas Stach 	pre->cur.in_use = false;
168d2a34232SLucas Stach }
169d2a34232SLucas Stach 
1704dbc7d5dSLucas Stach static inline void
ipu_pre_update_safe_window(struct ipu_pre * pre)1714dbc7d5dSLucas Stach ipu_pre_update_safe_window(struct ipu_pre *pre)
1724dbc7d5dSLucas Stach {
1734dbc7d5dSLucas Stach 	if (pre->cur.modifier == DRM_FORMAT_MOD_LINEAR)
1744dbc7d5dSLucas Stach 		pre->cur.safe_window_end = pre->cur.height - 2;
1754dbc7d5dSLucas Stach 	else
1764dbc7d5dSLucas Stach 		pre->cur.safe_window_end = DIV_ROUND_UP(pre->cur.height, 4) - 1;
1774dbc7d5dSLucas Stach }
1784dbc7d5dSLucas Stach 
1794dbc7d5dSLucas Stach static void
ipu_pre_configure_modifier(struct ipu_pre * pre,uint64_t modifier)1804dbc7d5dSLucas Stach ipu_pre_configure_modifier(struct ipu_pre *pre, uint64_t modifier)
1814dbc7d5dSLucas Stach {
1824dbc7d5dSLucas Stach 	u32 val;
1834dbc7d5dSLucas Stach 
1844dbc7d5dSLucas Stach 	val = readl(pre->regs + IPU_PRE_TPR_CTRL);
1854dbc7d5dSLucas Stach 	val &= ~IPU_PRE_TPR_CTRL_TILE_FORMAT_MASK;
1864dbc7d5dSLucas Stach 	if (modifier != DRM_FORMAT_MOD_LINEAR) {
1874dbc7d5dSLucas Stach 		/* only support single buffer formats for now */
1884dbc7d5dSLucas Stach 		val |= IPU_PRE_TPR_CTRL_TILE_FORMAT_SINGLE_BUF;
1894dbc7d5dSLucas Stach 		if (modifier == DRM_FORMAT_MOD_VIVANTE_SUPER_TILED)
1904dbc7d5dSLucas Stach 			val |= IPU_PRE_TPR_CTRL_TILE_FORMAT_SUPER_TILED;
1914dbc7d5dSLucas Stach 		if (pre->cur.cpp == 2)
1924dbc7d5dSLucas Stach 			val |= IPU_PRE_TPR_CTRL_TILE_FORMAT_16_BIT;
1934dbc7d5dSLucas Stach 	}
1944dbc7d5dSLucas Stach 	writel(val, pre->regs + IPU_PRE_TPR_CTRL);
1954dbc7d5dSLucas Stach 
1964dbc7d5dSLucas Stach 	if (modifier == DRM_FORMAT_MOD_LINEAR)
1974dbc7d5dSLucas Stach 		pre->cur.ctrl &= ~IPU_PRE_CTRL_BLOCK_EN;
1984dbc7d5dSLucas Stach 	else
1994dbc7d5dSLucas Stach 		pre->cur.ctrl |= IPU_PRE_CTRL_BLOCK_EN;
2004dbc7d5dSLucas Stach 
2014dbc7d5dSLucas Stach 	pre->cur.modifier = modifier;
2024dbc7d5dSLucas Stach }
2034dbc7d5dSLucas Stach 
ipu_pre_configure(struct ipu_pre * pre,unsigned int width,unsigned int height,unsigned int stride,u32 format,uint64_t modifier,unsigned int bufaddr)204d2a34232SLucas Stach void ipu_pre_configure(struct ipu_pre *pre, unsigned int width,
205d2a34232SLucas Stach 		       unsigned int height, unsigned int stride, u32 format,
2062f64a554SLucas Stach 		       uint64_t modifier, unsigned int bufaddr)
207d2a34232SLucas Stach {
208d2a34232SLucas Stach 	const struct drm_format_info *info = drm_format_info(format);
209d2a34232SLucas Stach 	u32 active_bpp = info->cpp[0] >> 1;
210d2a34232SLucas Stach 	u32 val;
211d2a34232SLucas Stach 
2124dbc7d5dSLucas Stach 	pre->cur.bufaddr = bufaddr;
2134dbc7d5dSLucas Stach 	pre->cur.height = height;
2144dbc7d5dSLucas Stach 	pre->cur.cpp = info->cpp[0];
2154dbc7d5dSLucas Stach 	pre->cur.ctrl = readl(pre->regs + IPU_PRE_CTRL);
2164dbc7d5dSLucas Stach 
21711aff4b4SLucas Stach 	/* calculate safe window for ctrl register updates */
2184dbc7d5dSLucas Stach 	ipu_pre_update_safe_window(pre);
21911aff4b4SLucas Stach 
220d2a34232SLucas Stach 	writel(bufaddr, pre->regs + IPU_PRE_CUR_BUF);
221d2a34232SLucas Stach 	writel(bufaddr, pre->regs + IPU_PRE_NEXT_BUF);
222d2a34232SLucas Stach 
223d2a34232SLucas Stach 	val = IPU_PRE_PREF_ENG_CTRL_INPUT_PIXEL_FORMAT(0) |
224d2a34232SLucas Stach 	      IPU_PRE_PREF_ENG_CTRL_INPUT_ACTIVE_BPP(active_bpp) |
225d2a34232SLucas Stach 	      IPU_PRE_PREF_ENG_CTRL_RD_NUM_BYTES(4) |
226d2a34232SLucas Stach 	      IPU_PRE_PREF_ENG_CTRL_SHIFT_BYPASS |
227d2a34232SLucas Stach 	      IPU_PRE_PREF_ENG_CTRL_PREFETCH_EN;
228d2a34232SLucas Stach 	writel(val, pre->regs + IPU_PRE_PREFETCH_ENG_CTRL);
229d2a34232SLucas Stach 
230d2a34232SLucas Stach 	val = IPU_PRE_PREFETCH_ENG_INPUT_SIZE_WIDTH(width) |
231d2a34232SLucas Stach 	      IPU_PRE_PREFETCH_ENG_INPUT_SIZE_HEIGHT(height);
232d2a34232SLucas Stach 	writel(val, pre->regs + IPU_PRE_PREFETCH_ENG_INPUT_SIZE);
233d2a34232SLucas Stach 
234d2a34232SLucas Stach 	val = IPU_PRE_PREFETCH_ENG_PITCH_Y(stride);
235d2a34232SLucas Stach 	writel(val, pre->regs + IPU_PRE_PREFETCH_ENG_PITCH);
236d2a34232SLucas Stach 
237d2a34232SLucas Stach 	val = IPU_PRE_STORE_ENG_CTRL_OUTPUT_ACTIVE_BPP(active_bpp) |
238d2a34232SLucas Stach 	      IPU_PRE_STORE_ENG_CTRL_WR_NUM_BYTES(4) |
239d2a34232SLucas Stach 	      IPU_PRE_STORE_ENG_CTRL_STORE_EN;
240d2a34232SLucas Stach 	writel(val, pre->regs + IPU_PRE_STORE_ENG_CTRL);
241d2a34232SLucas Stach 
242d2a34232SLucas Stach 	val = IPU_PRE_STORE_ENG_SIZE_INPUT_WIDTH(width) |
243d2a34232SLucas Stach 	      IPU_PRE_STORE_ENG_SIZE_INPUT_HEIGHT(height);
244d2a34232SLucas Stach 	writel(val, pre->regs + IPU_PRE_STORE_ENG_SIZE);
245d2a34232SLucas Stach 
246d2a34232SLucas Stach 	val = IPU_PRE_STORE_ENG_PITCH_OUT_PITCH(stride);
247d2a34232SLucas Stach 	writel(val, pre->regs + IPU_PRE_STORE_ENG_PITCH);
248d2a34232SLucas Stach 
249d2a34232SLucas Stach 	writel(pre->buffer_paddr, pre->regs + IPU_PRE_STORE_ENG_ADDR);
250d2a34232SLucas Stach 
2514dbc7d5dSLucas Stach 	ipu_pre_configure_modifier(pre, modifier);
2522f64a554SLucas Stach 
2534dbc7d5dSLucas Stach 	pre->cur.ctrl |= IPU_PRE_CTRL_EN_REPEAT | IPU_PRE_CTRL_ENABLE;
2544dbc7d5dSLucas Stach 	writel(pre->cur.ctrl | IPU_PRE_CTRL_SDW_UPDATE,
2554dbc7d5dSLucas Stach 	       pre->regs + IPU_PRE_CTRL);
256d2a34232SLucas Stach }
257d2a34232SLucas Stach 
ipu_pre_update(struct ipu_pre * pre,uint64_t modifier,unsigned int bufaddr)2584dbc7d5dSLucas Stach void ipu_pre_update(struct ipu_pre *pre, uint64_t modifier, unsigned int bufaddr)
259d2a34232SLucas Stach {
2604dbc7d5dSLucas Stach 	if (bufaddr == pre->cur.bufaddr &&
2614dbc7d5dSLucas Stach 	    modifier == pre->cur.modifier)
262eb0200a4SLucas Stach 		return;
263eb0200a4SLucas Stach 
264d2a34232SLucas Stach 	writel(bufaddr, pre->regs + IPU_PRE_NEXT_BUF);
265ee7ff5e2SLucas Stach 	pre->cur.bufaddr = bufaddr;
26611aff4b4SLucas Stach 
2674dbc7d5dSLucas Stach 	if (modifier != pre->cur.modifier)
2684dbc7d5dSLucas Stach 		ipu_pre_configure_modifier(pre, modifier);
2694dbc7d5dSLucas Stach 
270d5316cddSLucas Stach 	for (int i = 0;; i++) {
271d5316cddSLucas Stach 		unsigned short current_yblock;
272d5316cddSLucas Stach 		u32 val;
273d5316cddSLucas Stach 
274d5316cddSLucas Stach 		if (i > 500) {
27511aff4b4SLucas Stach 			dev_warn(pre->dev, "timeout waiting for PRE safe window\n");
27611aff4b4SLucas Stach 			return;
27711aff4b4SLucas Stach 		}
27811aff4b4SLucas Stach 
27911aff4b4SLucas Stach 		val = readl(pre->regs + IPU_PRE_STORE_ENG_STATUS);
28011aff4b4SLucas Stach 		current_yblock =
28111aff4b4SLucas Stach 			(val >> IPU_PRE_STORE_ENG_STATUS_STORE_BLOCK_Y_SHIFT) &
28211aff4b4SLucas Stach 			IPU_PRE_STORE_ENG_STATUS_STORE_BLOCK_Y_MASK;
283d5316cddSLucas Stach 
284d5316cddSLucas Stach 		if (current_yblock != 0 &&
285d5316cddSLucas Stach 		    current_yblock < pre->cur.safe_window_end)
286d5316cddSLucas Stach 			break;
287d5316cddSLucas Stach 
288d5316cddSLucas Stach 		udelay(10);
289d5316cddSLucas Stach 		cpu_relax();
290d5316cddSLucas Stach 	}
29111aff4b4SLucas Stach 
2924dbc7d5dSLucas Stach 	writel(pre->cur.ctrl | IPU_PRE_CTRL_SDW_UPDATE,
2934dbc7d5dSLucas Stach 	       pre->regs + IPU_PRE_CTRL);
2944dbc7d5dSLucas Stach 
2954dbc7d5dSLucas Stach 	/* calculate safe window for the next update with the new modifier */
2964dbc7d5dSLucas Stach 	ipu_pre_update_safe_window(pre);
297d2a34232SLucas Stach }
298d2a34232SLucas Stach 
ipu_pre_update_pending(struct ipu_pre * pre)2990a29b1abSLucas Stach bool ipu_pre_update_pending(struct ipu_pre *pre)
3000a29b1abSLucas Stach {
3010a29b1abSLucas Stach 	return !!(readl_relaxed(pre->regs + IPU_PRE_CTRL) &
3020a29b1abSLucas Stach 		  IPU_PRE_CTRL_SDW_UPDATE);
3030a29b1abSLucas Stach }
3040a29b1abSLucas Stach 
ipu_pre_get_baddr(struct ipu_pre * pre)305d2a34232SLucas Stach u32 ipu_pre_get_baddr(struct ipu_pre *pre)
306d2a34232SLucas Stach {
307d2a34232SLucas Stach 	return (u32)pre->buffer_paddr;
308d2a34232SLucas Stach }
309d2a34232SLucas Stach 
ipu_pre_probe(struct platform_device * pdev)310d2a34232SLucas Stach static int ipu_pre_probe(struct platform_device *pdev)
311d2a34232SLucas Stach {
312d2a34232SLucas Stach 	struct device *dev = &pdev->dev;
313d2a34232SLucas Stach 	struct ipu_pre *pre;
314d2a34232SLucas Stach 
315d2a34232SLucas Stach 	pre = devm_kzalloc(dev, sizeof(*pre), GFP_KERNEL);
316d2a34232SLucas Stach 	if (!pre)
317d2a34232SLucas Stach 		return -ENOMEM;
318d2a34232SLucas Stach 
31998935088SYangtao Li 	pre->regs = devm_platform_ioremap_resource(pdev, 0);
320d2a34232SLucas Stach 	if (IS_ERR(pre->regs))
321d2a34232SLucas Stach 		return PTR_ERR(pre->regs);
322d2a34232SLucas Stach 
323d2a34232SLucas Stach 	pre->clk_axi = devm_clk_get(dev, "axi");
324d2a34232SLucas Stach 	if (IS_ERR(pre->clk_axi))
325d2a34232SLucas Stach 		return PTR_ERR(pre->clk_axi);
326d2a34232SLucas Stach 
327d2a34232SLucas Stach 	pre->iram = of_gen_pool_get(dev->of_node, "fsl,iram", 0);
328d2a34232SLucas Stach 	if (!pre->iram)
329d2a34232SLucas Stach 		return -EPROBE_DEFER;
330d2a34232SLucas Stach 
331d2a34232SLucas Stach 	/*
332d2a34232SLucas Stach 	 * Allocate IRAM buffer with maximum size. This could be made dynamic,
333d2a34232SLucas Stach 	 * but as there is no other user of this IRAM region and we can fit all
334d2a34232SLucas Stach 	 * max sized buffers into it, there is no need yet.
335d2a34232SLucas Stach 	 */
336d2a34232SLucas Stach 	pre->buffer_virt = gen_pool_dma_alloc(pre->iram, IPU_PRE_MAX_WIDTH *
337d2a34232SLucas Stach 					      IPU_PRE_NUM_SCANLINES * 4,
338d2a34232SLucas Stach 					      &pre->buffer_paddr);
339d2a34232SLucas Stach 	if (!pre->buffer_virt)
340d2a34232SLucas Stach 		return -ENOMEM;
341d2a34232SLucas Stach 
34247c298f7SLucas Stach 	clk_prepare_enable(pre->clk_axi);
34347c298f7SLucas Stach 
344d2a34232SLucas Stach 	pre->dev = dev;
345d2a34232SLucas Stach 	platform_set_drvdata(pdev, pre);
346d2a34232SLucas Stach 	mutex_lock(&ipu_pre_list_mutex);
347d2a34232SLucas Stach 	list_add(&pre->list, &ipu_pre_list);
348d2a34232SLucas Stach 	available_pres++;
349d2a34232SLucas Stach 	mutex_unlock(&ipu_pre_list_mutex);
350d2a34232SLucas Stach 
351d2a34232SLucas Stach 	return 0;
352d2a34232SLucas Stach }
353d2a34232SLucas Stach 
ipu_pre_remove(struct platform_device * pdev)354*4402a5aaSUwe Kleine-König static void ipu_pre_remove(struct platform_device *pdev)
355d2a34232SLucas Stach {
356d2a34232SLucas Stach 	struct ipu_pre *pre = platform_get_drvdata(pdev);
357d2a34232SLucas Stach 
358d2a34232SLucas Stach 	mutex_lock(&ipu_pre_list_mutex);
359d2a34232SLucas Stach 	list_del(&pre->list);
360d2a34232SLucas Stach 	available_pres--;
361d2a34232SLucas Stach 	mutex_unlock(&ipu_pre_list_mutex);
362d2a34232SLucas Stach 
36347c298f7SLucas Stach 	clk_disable_unprepare(pre->clk_axi);
36447c298f7SLucas Stach 
365d2a34232SLucas Stach 	if (pre->buffer_virt)
366d2a34232SLucas Stach 		gen_pool_free(pre->iram, (unsigned long)pre->buffer_virt,
367d2a34232SLucas Stach 			      IPU_PRE_MAX_WIDTH * IPU_PRE_NUM_SCANLINES * 4);
368d2a34232SLucas Stach }
369d2a34232SLucas Stach 
370d2a34232SLucas Stach static const struct of_device_id ipu_pre_dt_ids[] = {
371d2a34232SLucas Stach 	{ .compatible = "fsl,imx6qp-pre", },
372d2a34232SLucas Stach 	{ /* sentinel */ },
373d2a34232SLucas Stach };
374d2a34232SLucas Stach 
375d2a34232SLucas Stach struct platform_driver ipu_pre_drv = {
376d2a34232SLucas Stach 	.probe		= ipu_pre_probe,
377*4402a5aaSUwe Kleine-König 	.remove_new	= ipu_pre_remove,
378d2a34232SLucas Stach 	.driver		= {
379d2a34232SLucas Stach 		.name	= "imx-ipu-pre",
380d2a34232SLucas Stach 		.of_match_table = ipu_pre_dt_ids,
381d2a34232SLucas Stach 	},
382d2a34232SLucas Stach };
383