18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (c) 2017 Lucas Stach, Pengutronix
48c2ecf20Sopenharmony_ci */
58c2ecf20Sopenharmony_ci
68c2ecf20Sopenharmony_ci#include <drm/drm_fourcc.h>
78c2ecf20Sopenharmony_ci#include <linux/clk.h>
88c2ecf20Sopenharmony_ci#include <linux/err.h>
98c2ecf20Sopenharmony_ci#include <linux/genalloc.h>
108c2ecf20Sopenharmony_ci#include <linux/module.h>
118c2ecf20Sopenharmony_ci#include <linux/of.h>
128c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
138c2ecf20Sopenharmony_ci#include <video/imx-ipu-v3.h>
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci#include "ipu-prv.h"
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci#define IPU_PRE_MAX_WIDTH	2048
188c2ecf20Sopenharmony_ci#define IPU_PRE_NUM_SCANLINES	8
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci#define IPU_PRE_CTRL					0x000
218c2ecf20Sopenharmony_ci#define IPU_PRE_CTRL_SET				0x004
228c2ecf20Sopenharmony_ci#define  IPU_PRE_CTRL_ENABLE				(1 << 0)
238c2ecf20Sopenharmony_ci#define  IPU_PRE_CTRL_BLOCK_EN				(1 << 1)
248c2ecf20Sopenharmony_ci#define  IPU_PRE_CTRL_BLOCK_16				(1 << 2)
258c2ecf20Sopenharmony_ci#define  IPU_PRE_CTRL_SDW_UPDATE			(1 << 4)
268c2ecf20Sopenharmony_ci#define  IPU_PRE_CTRL_VFLIP				(1 << 5)
278c2ecf20Sopenharmony_ci#define  IPU_PRE_CTRL_SO				(1 << 6)
288c2ecf20Sopenharmony_ci#define  IPU_PRE_CTRL_INTERLACED_FIELD			(1 << 7)
298c2ecf20Sopenharmony_ci#define  IPU_PRE_CTRL_HANDSHAKE_EN			(1 << 8)
308c2ecf20Sopenharmony_ci#define  IPU_PRE_CTRL_HANDSHAKE_LINE_NUM(v)		((v & 0x3) << 9)
318c2ecf20Sopenharmony_ci#define  IPU_PRE_CTRL_HANDSHAKE_ABORT_SKIP_EN		(1 << 11)
328c2ecf20Sopenharmony_ci#define  IPU_PRE_CTRL_EN_REPEAT				(1 << 28)
338c2ecf20Sopenharmony_ci#define  IPU_PRE_CTRL_TPR_REST_SEL			(1 << 29)
348c2ecf20Sopenharmony_ci#define  IPU_PRE_CTRL_CLKGATE				(1 << 30)
358c2ecf20Sopenharmony_ci#define  IPU_PRE_CTRL_SFTRST				(1 << 31)
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci#define IPU_PRE_CUR_BUF					0x030
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ci#define IPU_PRE_NEXT_BUF				0x040
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci#define IPU_PRE_TPR_CTRL				0x070
428c2ecf20Sopenharmony_ci#define  IPU_PRE_TPR_CTRL_TILE_FORMAT(v)		((v & 0xff) << 0)
438c2ecf20Sopenharmony_ci#define  IPU_PRE_TPR_CTRL_TILE_FORMAT_MASK		0xff
448c2ecf20Sopenharmony_ci#define  IPU_PRE_TPR_CTRL_TILE_FORMAT_16_BIT		(1 << 0)
458c2ecf20Sopenharmony_ci#define  IPU_PRE_TPR_CTRL_TILE_FORMAT_SPLIT_BUF		(1 << 4)
468c2ecf20Sopenharmony_ci#define  IPU_PRE_TPR_CTRL_TILE_FORMAT_SINGLE_BUF	(1 << 5)
478c2ecf20Sopenharmony_ci#define  IPU_PRE_TPR_CTRL_TILE_FORMAT_SUPER_TILED	(1 << 6)
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci#define IPU_PRE_PREFETCH_ENG_CTRL			0x080
508c2ecf20Sopenharmony_ci#define  IPU_PRE_PREF_ENG_CTRL_PREFETCH_EN		(1 << 0)
518c2ecf20Sopenharmony_ci#define  IPU_PRE_PREF_ENG_CTRL_RD_NUM_BYTES(v)		((v & 0x7) << 1)
528c2ecf20Sopenharmony_ci#define  IPU_PRE_PREF_ENG_CTRL_INPUT_ACTIVE_BPP(v)	((v & 0x3) << 4)
538c2ecf20Sopenharmony_ci#define  IPU_PRE_PREF_ENG_CTRL_INPUT_PIXEL_FORMAT(v)	((v & 0x7) << 8)
548c2ecf20Sopenharmony_ci#define  IPU_PRE_PREF_ENG_CTRL_SHIFT_BYPASS		(1 << 11)
558c2ecf20Sopenharmony_ci#define  IPU_PRE_PREF_ENG_CTRL_FIELD_INVERSE		(1 << 12)
568c2ecf20Sopenharmony_ci#define  IPU_PRE_PREF_ENG_CTRL_PARTIAL_UV_SWAP		(1 << 14)
578c2ecf20Sopenharmony_ci#define  IPU_PRE_PREF_ENG_CTRL_TPR_COOR_OFFSET_EN	(1 << 15)
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ci#define IPU_PRE_PREFETCH_ENG_INPUT_SIZE			0x0a0
608c2ecf20Sopenharmony_ci#define  IPU_PRE_PREFETCH_ENG_INPUT_SIZE_WIDTH(v)	((v & 0xffff) << 0)
618c2ecf20Sopenharmony_ci#define  IPU_PRE_PREFETCH_ENG_INPUT_SIZE_HEIGHT(v)	((v & 0xffff) << 16)
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ci#define IPU_PRE_PREFETCH_ENG_PITCH			0x0d0
648c2ecf20Sopenharmony_ci#define  IPU_PRE_PREFETCH_ENG_PITCH_Y(v)		((v & 0xffff) << 0)
658c2ecf20Sopenharmony_ci#define  IPU_PRE_PREFETCH_ENG_PITCH_UV(v)		((v & 0xffff) << 16)
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci#define IPU_PRE_STORE_ENG_CTRL				0x110
688c2ecf20Sopenharmony_ci#define  IPU_PRE_STORE_ENG_CTRL_STORE_EN		(1 << 0)
698c2ecf20Sopenharmony_ci#define  IPU_PRE_STORE_ENG_CTRL_WR_NUM_BYTES(v)		((v & 0x7) << 1)
708c2ecf20Sopenharmony_ci#define  IPU_PRE_STORE_ENG_CTRL_OUTPUT_ACTIVE_BPP(v)	((v & 0x3) << 4)
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ci#define IPU_PRE_STORE_ENG_STATUS			0x120
738c2ecf20Sopenharmony_ci#define  IPU_PRE_STORE_ENG_STATUS_STORE_BLOCK_X_MASK	0xffff
748c2ecf20Sopenharmony_ci#define  IPU_PRE_STORE_ENG_STATUS_STORE_BLOCK_X_SHIFT	0
758c2ecf20Sopenharmony_ci#define  IPU_PRE_STORE_ENG_STATUS_STORE_BLOCK_Y_MASK	0x3fff
768c2ecf20Sopenharmony_ci#define  IPU_PRE_STORE_ENG_STATUS_STORE_BLOCK_Y_SHIFT	16
778c2ecf20Sopenharmony_ci#define  IPU_PRE_STORE_ENG_STATUS_STORE_FIFO_FULL	(1 << 30)
788c2ecf20Sopenharmony_ci#define  IPU_PRE_STORE_ENG_STATUS_STORE_FIELD		(1 << 31)
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci#define IPU_PRE_STORE_ENG_SIZE				0x130
818c2ecf20Sopenharmony_ci#define  IPU_PRE_STORE_ENG_SIZE_INPUT_WIDTH(v)		((v & 0xffff) << 0)
828c2ecf20Sopenharmony_ci#define  IPU_PRE_STORE_ENG_SIZE_INPUT_HEIGHT(v)		((v & 0xffff) << 16)
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci#define IPU_PRE_STORE_ENG_PITCH				0x140
858c2ecf20Sopenharmony_ci#define  IPU_PRE_STORE_ENG_PITCH_OUT_PITCH(v)		((v & 0xffff) << 0)
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci#define IPU_PRE_STORE_ENG_ADDR				0x150
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_cistruct ipu_pre {
908c2ecf20Sopenharmony_ci	struct list_head	list;
918c2ecf20Sopenharmony_ci	struct device		*dev;
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci	void __iomem		*regs;
948c2ecf20Sopenharmony_ci	struct clk		*clk_axi;
958c2ecf20Sopenharmony_ci	struct gen_pool		*iram;
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_ci	dma_addr_t		buffer_paddr;
988c2ecf20Sopenharmony_ci	void			*buffer_virt;
998c2ecf20Sopenharmony_ci	bool			in_use;
1008c2ecf20Sopenharmony_ci	unsigned int		safe_window_end;
1018c2ecf20Sopenharmony_ci	unsigned int		last_bufaddr;
1028c2ecf20Sopenharmony_ci};
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_cistatic DEFINE_MUTEX(ipu_pre_list_mutex);
1058c2ecf20Sopenharmony_cistatic LIST_HEAD(ipu_pre_list);
1068c2ecf20Sopenharmony_cistatic int available_pres;
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ciint ipu_pre_get_available_count(void)
1098c2ecf20Sopenharmony_ci{
1108c2ecf20Sopenharmony_ci	return available_pres;
1118c2ecf20Sopenharmony_ci}
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_cistruct ipu_pre *
1148c2ecf20Sopenharmony_ciipu_pre_lookup_by_phandle(struct device *dev, const char *name, int index)
1158c2ecf20Sopenharmony_ci{
1168c2ecf20Sopenharmony_ci	struct device_node *pre_node = of_parse_phandle(dev->of_node,
1178c2ecf20Sopenharmony_ci							name, index);
1188c2ecf20Sopenharmony_ci	struct ipu_pre *pre;
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci	mutex_lock(&ipu_pre_list_mutex);
1218c2ecf20Sopenharmony_ci	list_for_each_entry(pre, &ipu_pre_list, list) {
1228c2ecf20Sopenharmony_ci		if (pre_node == pre->dev->of_node) {
1238c2ecf20Sopenharmony_ci			mutex_unlock(&ipu_pre_list_mutex);
1248c2ecf20Sopenharmony_ci			device_link_add(dev, pre->dev,
1258c2ecf20Sopenharmony_ci					DL_FLAG_AUTOREMOVE_CONSUMER);
1268c2ecf20Sopenharmony_ci			of_node_put(pre_node);
1278c2ecf20Sopenharmony_ci			return pre;
1288c2ecf20Sopenharmony_ci		}
1298c2ecf20Sopenharmony_ci	}
1308c2ecf20Sopenharmony_ci	mutex_unlock(&ipu_pre_list_mutex);
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ci	of_node_put(pre_node);
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci	return NULL;
1358c2ecf20Sopenharmony_ci}
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_ciint ipu_pre_get(struct ipu_pre *pre)
1388c2ecf20Sopenharmony_ci{
1398c2ecf20Sopenharmony_ci	u32 val;
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_ci	if (pre->in_use)
1428c2ecf20Sopenharmony_ci		return -EBUSY;
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ci	/* first get the engine out of reset and remove clock gating */
1458c2ecf20Sopenharmony_ci	writel(0, pre->regs + IPU_PRE_CTRL);
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_ci	/* init defaults that should be applied to all streams */
1488c2ecf20Sopenharmony_ci	val = IPU_PRE_CTRL_HANDSHAKE_ABORT_SKIP_EN |
1498c2ecf20Sopenharmony_ci	      IPU_PRE_CTRL_HANDSHAKE_EN |
1508c2ecf20Sopenharmony_ci	      IPU_PRE_CTRL_TPR_REST_SEL |
1518c2ecf20Sopenharmony_ci	      IPU_PRE_CTRL_SDW_UPDATE;
1528c2ecf20Sopenharmony_ci	writel(val, pre->regs + IPU_PRE_CTRL);
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_ci	pre->in_use = true;
1558c2ecf20Sopenharmony_ci	return 0;
1568c2ecf20Sopenharmony_ci}
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_civoid ipu_pre_put(struct ipu_pre *pre)
1598c2ecf20Sopenharmony_ci{
1608c2ecf20Sopenharmony_ci	writel(IPU_PRE_CTRL_SFTRST, pre->regs + IPU_PRE_CTRL);
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_ci	pre->in_use = false;
1638c2ecf20Sopenharmony_ci}
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_civoid ipu_pre_configure(struct ipu_pre *pre, unsigned int width,
1668c2ecf20Sopenharmony_ci		       unsigned int height, unsigned int stride, u32 format,
1678c2ecf20Sopenharmony_ci		       uint64_t modifier, unsigned int bufaddr)
1688c2ecf20Sopenharmony_ci{
1698c2ecf20Sopenharmony_ci	const struct drm_format_info *info = drm_format_info(format);
1708c2ecf20Sopenharmony_ci	u32 active_bpp = info->cpp[0] >> 1;
1718c2ecf20Sopenharmony_ci	u32 val;
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_ci	/* calculate safe window for ctrl register updates */
1748c2ecf20Sopenharmony_ci	if (modifier == DRM_FORMAT_MOD_LINEAR)
1758c2ecf20Sopenharmony_ci		pre->safe_window_end = height - 2;
1768c2ecf20Sopenharmony_ci	else
1778c2ecf20Sopenharmony_ci		pre->safe_window_end = DIV_ROUND_UP(height, 4) - 1;
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_ci	writel(bufaddr, pre->regs + IPU_PRE_CUR_BUF);
1808c2ecf20Sopenharmony_ci	writel(bufaddr, pre->regs + IPU_PRE_NEXT_BUF);
1818c2ecf20Sopenharmony_ci	pre->last_bufaddr = bufaddr;
1828c2ecf20Sopenharmony_ci
1838c2ecf20Sopenharmony_ci	val = IPU_PRE_PREF_ENG_CTRL_INPUT_PIXEL_FORMAT(0) |
1848c2ecf20Sopenharmony_ci	      IPU_PRE_PREF_ENG_CTRL_INPUT_ACTIVE_BPP(active_bpp) |
1858c2ecf20Sopenharmony_ci	      IPU_PRE_PREF_ENG_CTRL_RD_NUM_BYTES(4) |
1868c2ecf20Sopenharmony_ci	      IPU_PRE_PREF_ENG_CTRL_SHIFT_BYPASS |
1878c2ecf20Sopenharmony_ci	      IPU_PRE_PREF_ENG_CTRL_PREFETCH_EN;
1888c2ecf20Sopenharmony_ci	writel(val, pre->regs + IPU_PRE_PREFETCH_ENG_CTRL);
1898c2ecf20Sopenharmony_ci
1908c2ecf20Sopenharmony_ci	val = IPU_PRE_PREFETCH_ENG_INPUT_SIZE_WIDTH(width) |
1918c2ecf20Sopenharmony_ci	      IPU_PRE_PREFETCH_ENG_INPUT_SIZE_HEIGHT(height);
1928c2ecf20Sopenharmony_ci	writel(val, pre->regs + IPU_PRE_PREFETCH_ENG_INPUT_SIZE);
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_ci	val = IPU_PRE_PREFETCH_ENG_PITCH_Y(stride);
1958c2ecf20Sopenharmony_ci	writel(val, pre->regs + IPU_PRE_PREFETCH_ENG_PITCH);
1968c2ecf20Sopenharmony_ci
1978c2ecf20Sopenharmony_ci	val = IPU_PRE_STORE_ENG_CTRL_OUTPUT_ACTIVE_BPP(active_bpp) |
1988c2ecf20Sopenharmony_ci	      IPU_PRE_STORE_ENG_CTRL_WR_NUM_BYTES(4) |
1998c2ecf20Sopenharmony_ci	      IPU_PRE_STORE_ENG_CTRL_STORE_EN;
2008c2ecf20Sopenharmony_ci	writel(val, pre->regs + IPU_PRE_STORE_ENG_CTRL);
2018c2ecf20Sopenharmony_ci
2028c2ecf20Sopenharmony_ci	val = IPU_PRE_STORE_ENG_SIZE_INPUT_WIDTH(width) |
2038c2ecf20Sopenharmony_ci	      IPU_PRE_STORE_ENG_SIZE_INPUT_HEIGHT(height);
2048c2ecf20Sopenharmony_ci	writel(val, pre->regs + IPU_PRE_STORE_ENG_SIZE);
2058c2ecf20Sopenharmony_ci
2068c2ecf20Sopenharmony_ci	val = IPU_PRE_STORE_ENG_PITCH_OUT_PITCH(stride);
2078c2ecf20Sopenharmony_ci	writel(val, pre->regs + IPU_PRE_STORE_ENG_PITCH);
2088c2ecf20Sopenharmony_ci
2098c2ecf20Sopenharmony_ci	writel(pre->buffer_paddr, pre->regs + IPU_PRE_STORE_ENG_ADDR);
2108c2ecf20Sopenharmony_ci
2118c2ecf20Sopenharmony_ci	val = readl(pre->regs + IPU_PRE_TPR_CTRL);
2128c2ecf20Sopenharmony_ci	val &= ~IPU_PRE_TPR_CTRL_TILE_FORMAT_MASK;
2138c2ecf20Sopenharmony_ci	if (modifier != DRM_FORMAT_MOD_LINEAR) {
2148c2ecf20Sopenharmony_ci		/* only support single buffer formats for now */
2158c2ecf20Sopenharmony_ci		val |= IPU_PRE_TPR_CTRL_TILE_FORMAT_SINGLE_BUF;
2168c2ecf20Sopenharmony_ci		if (modifier == DRM_FORMAT_MOD_VIVANTE_SUPER_TILED)
2178c2ecf20Sopenharmony_ci			val |= IPU_PRE_TPR_CTRL_TILE_FORMAT_SUPER_TILED;
2188c2ecf20Sopenharmony_ci		if (info->cpp[0] == 2)
2198c2ecf20Sopenharmony_ci			val |= IPU_PRE_TPR_CTRL_TILE_FORMAT_16_BIT;
2208c2ecf20Sopenharmony_ci	}
2218c2ecf20Sopenharmony_ci	writel(val, pre->regs + IPU_PRE_TPR_CTRL);
2228c2ecf20Sopenharmony_ci
2238c2ecf20Sopenharmony_ci	val = readl(pre->regs + IPU_PRE_CTRL);
2248c2ecf20Sopenharmony_ci	val |= IPU_PRE_CTRL_EN_REPEAT | IPU_PRE_CTRL_ENABLE |
2258c2ecf20Sopenharmony_ci	       IPU_PRE_CTRL_SDW_UPDATE;
2268c2ecf20Sopenharmony_ci	if (modifier == DRM_FORMAT_MOD_LINEAR)
2278c2ecf20Sopenharmony_ci		val &= ~IPU_PRE_CTRL_BLOCK_EN;
2288c2ecf20Sopenharmony_ci	else
2298c2ecf20Sopenharmony_ci		val |= IPU_PRE_CTRL_BLOCK_EN;
2308c2ecf20Sopenharmony_ci	writel(val, pre->regs + IPU_PRE_CTRL);
2318c2ecf20Sopenharmony_ci}
2328c2ecf20Sopenharmony_ci
2338c2ecf20Sopenharmony_civoid ipu_pre_update(struct ipu_pre *pre, unsigned int bufaddr)
2348c2ecf20Sopenharmony_ci{
2358c2ecf20Sopenharmony_ci	unsigned long timeout = jiffies + msecs_to_jiffies(5);
2368c2ecf20Sopenharmony_ci	unsigned short current_yblock;
2378c2ecf20Sopenharmony_ci	u32 val;
2388c2ecf20Sopenharmony_ci
2398c2ecf20Sopenharmony_ci	if (bufaddr == pre->last_bufaddr)
2408c2ecf20Sopenharmony_ci		return;
2418c2ecf20Sopenharmony_ci
2428c2ecf20Sopenharmony_ci	writel(bufaddr, pre->regs + IPU_PRE_NEXT_BUF);
2438c2ecf20Sopenharmony_ci	pre->last_bufaddr = bufaddr;
2448c2ecf20Sopenharmony_ci
2458c2ecf20Sopenharmony_ci	do {
2468c2ecf20Sopenharmony_ci		if (time_after(jiffies, timeout)) {
2478c2ecf20Sopenharmony_ci			dev_warn(pre->dev, "timeout waiting for PRE safe window\n");
2488c2ecf20Sopenharmony_ci			return;
2498c2ecf20Sopenharmony_ci		}
2508c2ecf20Sopenharmony_ci
2518c2ecf20Sopenharmony_ci		val = readl(pre->regs + IPU_PRE_STORE_ENG_STATUS);
2528c2ecf20Sopenharmony_ci		current_yblock =
2538c2ecf20Sopenharmony_ci			(val >> IPU_PRE_STORE_ENG_STATUS_STORE_BLOCK_Y_SHIFT) &
2548c2ecf20Sopenharmony_ci			IPU_PRE_STORE_ENG_STATUS_STORE_BLOCK_Y_MASK;
2558c2ecf20Sopenharmony_ci	} while (current_yblock == 0 || current_yblock >= pre->safe_window_end);
2568c2ecf20Sopenharmony_ci
2578c2ecf20Sopenharmony_ci	writel(IPU_PRE_CTRL_SDW_UPDATE, pre->regs + IPU_PRE_CTRL_SET);
2588c2ecf20Sopenharmony_ci}
2598c2ecf20Sopenharmony_ci
2608c2ecf20Sopenharmony_cibool ipu_pre_update_pending(struct ipu_pre *pre)
2618c2ecf20Sopenharmony_ci{
2628c2ecf20Sopenharmony_ci	return !!(readl_relaxed(pre->regs + IPU_PRE_CTRL) &
2638c2ecf20Sopenharmony_ci		  IPU_PRE_CTRL_SDW_UPDATE);
2648c2ecf20Sopenharmony_ci}
2658c2ecf20Sopenharmony_ci
2668c2ecf20Sopenharmony_ciu32 ipu_pre_get_baddr(struct ipu_pre *pre)
2678c2ecf20Sopenharmony_ci{
2688c2ecf20Sopenharmony_ci	return (u32)pre->buffer_paddr;
2698c2ecf20Sopenharmony_ci}
2708c2ecf20Sopenharmony_ci
2718c2ecf20Sopenharmony_cistatic int ipu_pre_probe(struct platform_device *pdev)
2728c2ecf20Sopenharmony_ci{
2738c2ecf20Sopenharmony_ci	struct device *dev = &pdev->dev;
2748c2ecf20Sopenharmony_ci	struct resource *res;
2758c2ecf20Sopenharmony_ci	struct ipu_pre *pre;
2768c2ecf20Sopenharmony_ci
2778c2ecf20Sopenharmony_ci	pre = devm_kzalloc(dev, sizeof(*pre), GFP_KERNEL);
2788c2ecf20Sopenharmony_ci	if (!pre)
2798c2ecf20Sopenharmony_ci		return -ENOMEM;
2808c2ecf20Sopenharmony_ci
2818c2ecf20Sopenharmony_ci	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2828c2ecf20Sopenharmony_ci	pre->regs = devm_ioremap_resource(&pdev->dev, res);
2838c2ecf20Sopenharmony_ci	if (IS_ERR(pre->regs))
2848c2ecf20Sopenharmony_ci		return PTR_ERR(pre->regs);
2858c2ecf20Sopenharmony_ci
2868c2ecf20Sopenharmony_ci	pre->clk_axi = devm_clk_get(dev, "axi");
2878c2ecf20Sopenharmony_ci	if (IS_ERR(pre->clk_axi))
2888c2ecf20Sopenharmony_ci		return PTR_ERR(pre->clk_axi);
2898c2ecf20Sopenharmony_ci
2908c2ecf20Sopenharmony_ci	pre->iram = of_gen_pool_get(dev->of_node, "fsl,iram", 0);
2918c2ecf20Sopenharmony_ci	if (!pre->iram)
2928c2ecf20Sopenharmony_ci		return -EPROBE_DEFER;
2938c2ecf20Sopenharmony_ci
2948c2ecf20Sopenharmony_ci	/*
2958c2ecf20Sopenharmony_ci	 * Allocate IRAM buffer with maximum size. This could be made dynamic,
2968c2ecf20Sopenharmony_ci	 * but as there is no other user of this IRAM region and we can fit all
2978c2ecf20Sopenharmony_ci	 * max sized buffers into it, there is no need yet.
2988c2ecf20Sopenharmony_ci	 */
2998c2ecf20Sopenharmony_ci	pre->buffer_virt = gen_pool_dma_alloc(pre->iram, IPU_PRE_MAX_WIDTH *
3008c2ecf20Sopenharmony_ci					      IPU_PRE_NUM_SCANLINES * 4,
3018c2ecf20Sopenharmony_ci					      &pre->buffer_paddr);
3028c2ecf20Sopenharmony_ci	if (!pre->buffer_virt)
3038c2ecf20Sopenharmony_ci		return -ENOMEM;
3048c2ecf20Sopenharmony_ci
3058c2ecf20Sopenharmony_ci	clk_prepare_enable(pre->clk_axi);
3068c2ecf20Sopenharmony_ci
3078c2ecf20Sopenharmony_ci	pre->dev = dev;
3088c2ecf20Sopenharmony_ci	platform_set_drvdata(pdev, pre);
3098c2ecf20Sopenharmony_ci	mutex_lock(&ipu_pre_list_mutex);
3108c2ecf20Sopenharmony_ci	list_add(&pre->list, &ipu_pre_list);
3118c2ecf20Sopenharmony_ci	available_pres++;
3128c2ecf20Sopenharmony_ci	mutex_unlock(&ipu_pre_list_mutex);
3138c2ecf20Sopenharmony_ci
3148c2ecf20Sopenharmony_ci	return 0;
3158c2ecf20Sopenharmony_ci}
3168c2ecf20Sopenharmony_ci
3178c2ecf20Sopenharmony_cistatic int ipu_pre_remove(struct platform_device *pdev)
3188c2ecf20Sopenharmony_ci{
3198c2ecf20Sopenharmony_ci	struct ipu_pre *pre = platform_get_drvdata(pdev);
3208c2ecf20Sopenharmony_ci
3218c2ecf20Sopenharmony_ci	mutex_lock(&ipu_pre_list_mutex);
3228c2ecf20Sopenharmony_ci	list_del(&pre->list);
3238c2ecf20Sopenharmony_ci	available_pres--;
3248c2ecf20Sopenharmony_ci	mutex_unlock(&ipu_pre_list_mutex);
3258c2ecf20Sopenharmony_ci
3268c2ecf20Sopenharmony_ci	clk_disable_unprepare(pre->clk_axi);
3278c2ecf20Sopenharmony_ci
3288c2ecf20Sopenharmony_ci	if (pre->buffer_virt)
3298c2ecf20Sopenharmony_ci		gen_pool_free(pre->iram, (unsigned long)pre->buffer_virt,
3308c2ecf20Sopenharmony_ci			      IPU_PRE_MAX_WIDTH * IPU_PRE_NUM_SCANLINES * 4);
3318c2ecf20Sopenharmony_ci	return 0;
3328c2ecf20Sopenharmony_ci}
3338c2ecf20Sopenharmony_ci
3348c2ecf20Sopenharmony_cistatic const struct of_device_id ipu_pre_dt_ids[] = {
3358c2ecf20Sopenharmony_ci	{ .compatible = "fsl,imx6qp-pre", },
3368c2ecf20Sopenharmony_ci	{ /* sentinel */ },
3378c2ecf20Sopenharmony_ci};
3388c2ecf20Sopenharmony_ci
3398c2ecf20Sopenharmony_cistruct platform_driver ipu_pre_drv = {
3408c2ecf20Sopenharmony_ci	.probe		= ipu_pre_probe,
3418c2ecf20Sopenharmony_ci	.remove		= ipu_pre_remove,
3428c2ecf20Sopenharmony_ci	.driver		= {
3438c2ecf20Sopenharmony_ci		.name	= "imx-ipu-pre",
3448c2ecf20Sopenharmony_ci		.of_match_table = ipu_pre_dt_ids,
3458c2ecf20Sopenharmony_ci	},
3468c2ecf20Sopenharmony_ci};
347