xref: /linux/drivers/media/platform/rockchip/rga/rga.h (revision ae22a94997b8a03dcb3c922857c203246711f9d4)
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 	struct v4l2_pix_format_mplane pix;
38 
39 	/* Variables that can calculated once and reused */
40 	u32 stride;
41 	u32 size;
42 };
43 
44 struct rga_dma_desc {
45 	u32 addr;
46 };
47 
48 struct rockchip_rga_version {
49 	u32 major;
50 	u32 minor;
51 };
52 
53 struct rga_ctx {
54 	struct v4l2_fh fh;
55 	struct rockchip_rga *rga;
56 	struct rga_frame in;
57 	struct rga_frame out;
58 	struct v4l2_ctrl_handler ctrl_handler;
59 
60 	/* Control values */
61 	u32 op;
62 	u32 hflip;
63 	u32 vflip;
64 	u32 rotate;
65 	u32 fill_color;
66 };
67 
68 struct rockchip_rga {
69 	struct v4l2_device v4l2_dev;
70 	struct v4l2_m2m_dev *m2m_dev;
71 	struct video_device *vfd;
72 
73 	struct device *dev;
74 	struct regmap *grf;
75 	void __iomem *regs;
76 	struct clk *sclk;
77 	struct clk *aclk;
78 	struct clk *hclk;
79 	struct rockchip_rga_version version;
80 
81 	/* vfd lock */
82 	struct mutex mutex;
83 	/* ctrl parm lock */
84 	spinlock_t ctrl_lock;
85 
86 	struct rga_ctx *curr;
87 	dma_addr_t cmdbuf_phy;
88 	void *cmdbuf_virt;
89 };
90 
91 struct rga_addr_offset {
92 	unsigned int y_off;
93 	unsigned int u_off;
94 	unsigned int v_off;
95 };
96 
97 struct rga_vb_buffer {
98 	struct vb2_v4l2_buffer vb_buf;
99 	struct list_head queue;
100 
101 	/* RGA MMU mapping for this buffer */
102 	struct rga_dma_desc *dma_desc;
103 	dma_addr_t dma_desc_pa;
104 	size_t n_desc;
105 
106 	/* Plane offsets of this buffer into the mapping */
107 	struct rga_addr_offset offset;
108 };
109 
110 static inline struct rga_vb_buffer *vb_to_rga(struct vb2_v4l2_buffer *vb)
111 {
112 	return container_of(vb, struct rga_vb_buffer, vb_buf);
113 }
114 
115 struct rga_frame *rga_get_frame(struct rga_ctx *ctx, enum v4l2_buf_type type);
116 
117 /* RGA Buffers Manage */
118 extern const struct vb2_ops rga_qops;
119 
120 /* RGA Hardware */
121 static inline void rga_write(struct rockchip_rga *rga, u32 reg, u32 value)
122 {
123 	writel(value, rga->regs + reg);
124 };
125 
126 static inline u32 rga_read(struct rockchip_rga *rga, u32 reg)
127 {
128 	return readl(rga->regs + reg);
129 };
130 
131 static inline void rga_mod(struct rockchip_rga *rga, u32 reg, u32 val, u32 mask)
132 {
133 	u32 temp = rga_read(rga, reg) & ~(mask);
134 
135 	temp |= val & mask;
136 	rga_write(rga, reg, temp);
137 };
138 
139 void rga_hw_start(struct rockchip_rga *rga,
140 		  struct rga_vb_buffer *src, struct rga_vb_buffer *dst);
141 
142 #endif
143