1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * Copyright (C) Fuzhou Rockchip Electronics Co.Ltd
4  * Author: Jacob Chen <jacob-chen@iotwrt.com>
5  */
6 #ifndef __RGA_H__
7 #define __RGA_H__
8 
9 #include <linux/platform_device.h>
10 #include <media/videobuf2-v4l2.h>
11 #include <media/v4l2-ctrls.h>
12 #include <media/v4l2-device.h>
13 
14 #define RGA_NAME "rockchip-rga"
15 
16 struct rga_fmt {
17 	u32 fourcc;
18 	int depth;
19 	u8 uv_factor;
20 	u8 y_div;
21 	u8 x_div;
22 	u8 color_swap;
23 	u8 hw_format;
24 };
25 
26 struct rga_frame {
27 	/* Original dimensions */
28 	u32 width;
29 	u32 height;
30 	u32 colorspace;
31 
32 	/* Crop */
33 	struct v4l2_rect crop;
34 
35 	/* Image format */
36 	struct rga_fmt *fmt;
37 
38 	/* Variables that can calculated once and reused */
39 	u32 stride;
40 	u32 size;
41 };
42 
43 struct rockchip_rga_version {
44 	u32 major;
45 	u32 minor;
46 };
47 
48 struct rga_ctx {
49 	struct v4l2_fh fh;
50 	struct rockchip_rga *rga;
51 	struct rga_frame in;
52 	struct rga_frame out;
53 	struct v4l2_ctrl_handler ctrl_handler;
54 
55 	/* Control values */
56 	u32 op;
57 	u32 hflip;
58 	u32 vflip;
59 	u32 rotate;
60 	u32 fill_color;
61 };
62 
63 struct rockchip_rga {
64 	struct v4l2_device v4l2_dev;
65 	struct v4l2_m2m_dev *m2m_dev;
66 	struct video_device *vfd;
67 
68 	struct device *dev;
69 	struct regmap *grf;
70 	void __iomem *regs;
71 	struct clk *sclk;
72 	struct clk *aclk;
73 	struct clk *hclk;
74 	struct rockchip_rga_version version;
75 
76 	/* vfd lock */
77 	struct mutex mutex;
78 	/* ctrl parm lock */
79 	spinlock_t ctrl_lock;
80 
81 	struct rga_ctx *curr;
82 	dma_addr_t cmdbuf_phy;
83 	void *cmdbuf_virt;
84 	unsigned int *src_mmu_pages;
85 	unsigned int *dst_mmu_pages;
86 };
87 
88 struct rga_frame *rga_get_frame(struct rga_ctx *ctx, enum v4l2_buf_type type);
89 
90 /* RGA Buffers Manage */
91 extern const struct vb2_ops rga_qops;
92 void rga_buf_map(struct vb2_buffer *vb);
93 
94 /* RGA Hardware */
rga_write(struct rockchip_rga * rga,u32 reg,u32 value)95 static inline void rga_write(struct rockchip_rga *rga, u32 reg, u32 value)
96 {
97 	writel(value, rga->regs + reg);
98 };
99 
rga_read(struct rockchip_rga * rga,u32 reg)100 static inline u32 rga_read(struct rockchip_rga *rga, u32 reg)
101 {
102 	return readl(rga->regs + reg);
103 };
104 
rga_mod(struct rockchip_rga * rga,u32 reg,u32 val,u32 mask)105 static inline void rga_mod(struct rockchip_rga *rga, u32 reg, u32 val, u32 mask)
106 {
107 	u32 temp = rga_read(rga, reg) & ~(mask);
108 
109 	temp |= val & mask;
110 	rga_write(rga, reg, temp);
111 };
112 
113 void rga_hw_start(struct rockchip_rga *rga);
114 
115 #endif
116