xref: /OK3568_Linux_fs/kernel/drivers/media/platform/rockchip/isp/common.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /*
2  * Rockchip isp1 driver
3  *
4  * Copyright (C) 2017 Rockchip Electronics Co., Ltd.
5  *
6  * This software is available to you under a choice of one of two
7  * licenses.  You may choose to be licensed under the terms of the GNU
8  * General Public License (GPL) Version 2, available from the file
9  * COPYING in the main directory of this source tree, or the
10  * OpenIB.org BSD license below:
11  *
12  *     Redistribution and use in source and binary forms, with or
13  *     without modification, are permitted provided that the following
14  *     conditions are met:
15  *
16  *      - Redistributions of source code must retain the above
17  *        copyright notice, this list of conditions and the following
18  *        disclaimer.
19  *
20  *      - Redistributions in binary form must reproduce the above
21  *        copyright notice, this list of conditions and the following
22  *        disclaimer in the documentation and/or other materials
23  *        provided with the distribution.
24  *
25  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32  * SOFTWARE.
33  */
34 
35 #ifndef _RKISP_COMMON_H
36 #define _RKISP_COMMON_H
37 
38 #include <linux/clk.h>
39 #include <linux/media.h>
40 #include <linux/mutex.h>
41 #include <linux/rk-video-format.h>
42 #include <linux/slab.h>
43 #include <linux/soc/rockchip/rk_sdmmc.h>
44 #include <media/media-device.h>
45 #include <media/media-entity.h>
46 #include <media/v4l2-ctrls.h>
47 #include <media/v4l2-device.h>
48 #include <media/v4l2-mc.h>
49 #include <media/videobuf2-dma-contig.h>
50 #include <media/videobuf2-v4l2.h>
51 
52 #define RKISP_DEFAULT_WIDTH		800
53 #define RKISP_DEFAULT_HEIGHT		600
54 
55 #define RKISP_PLANE_Y			0
56 #define RKISP_PLANE_CB			1
57 #define RKISP_PLANE_CR			2
58 
59 #define RKISP_EMDDATA_FIFO_MAX		4
60 #define RKISP_DMATX_CHECK              0xA5A5A5A5
61 
62 #define RKISP_MOTION_DECT_TS_SIZE	16
63 
64 struct rkisp_device;
65 
66 /* ISP_V10_1 for only support MP */
67 enum rkisp_isp_ver {
68 	ISP_V10 = 0x00,
69 	ISP_V10_1 = 0x01,
70 	ISP_V11 = 0x10,
71 	ISP_V12 = 0x20,
72 	ISP_V13 = 0x30,
73 	ISP_V20 = 0x40,
74 	ISP_V21 = 0x50,
75 	ISP_V30 = 0x60,
76 	ISP_V32 = 0x70,
77 	ISP_V32_L = 0x80,
78 };
79 
80 enum rkisp_sd_type {
81 	RKISP_SD_SENSOR,
82 	RKISP_SD_PHY_CSI,
83 	RKISP_SD_VCM,
84 	RKISP_SD_FLASH,
85 	RKISP_SD_MAX,
86 };
87 
88 /* One structure per video node */
89 struct rkisp_vdev_node {
90 	struct vb2_queue buf_queue;
91 	struct video_device vdev;
92 	struct media_pad pad;
93 };
94 
95 enum rkisp_fmt_pix_type {
96 	FMT_YUV,
97 	FMT_RGB,
98 	FMT_BAYER,
99 	FMT_JPEG,
100 	FMT_FBCGAIN,
101 	FMT_EBD,
102 	FMT_SPD,
103 	FMT_FBC,
104 	FMT_MAX
105 };
106 
107 enum rkisp_fmt_raw_pat_type {
108 	RAW_RGGB = 0,
109 	RAW_GRBG,
110 	RAW_GBRG,
111 	RAW_BGGR,
112 };
113 
114 struct rkisp_buffer {
115 	struct vb2_v4l2_buffer vb;
116 	struct list_head queue;
117 	void *vaddr[VIDEO_MAX_PLANES];
118 	u32 buff_addr[VIDEO_MAX_PLANES];
119 	int dev_id;
120 	void *other;
121 };
122 
123 struct rkisp_dummy_buffer {
124 	struct list_head queue;
125 	struct dma_buf *dbuf;
126 	dma_addr_t dma_addr;
127 	struct page **pages;
128 	void *mem_priv;
129 	void *vaddr;
130 	u32 size;
131 	int dma_fd;
132 	bool is_need_vaddr;
133 	bool is_need_dbuf;
134 	bool is_need_dmafd;
135 };
136 
137 extern int rkisp_debug;
138 extern bool rkisp_monitor;
139 extern bool rkisp_irq_dbg;
140 extern u64 rkisp_debug_reg;
141 extern struct platform_driver rkisp_plat_drv;
142 
143 static inline
vdev_to_node(struct video_device * vdev)144 struct rkisp_vdev_node *vdev_to_node(struct video_device *vdev)
145 {
146 	return container_of(vdev, struct rkisp_vdev_node, vdev);
147 }
148 
queue_to_node(struct vb2_queue * q)149 static inline struct rkisp_vdev_node *queue_to_node(struct vb2_queue *q)
150 {
151 	return container_of(q, struct rkisp_vdev_node, buf_queue);
152 }
153 
to_rkisp_buffer(struct vb2_v4l2_buffer * vb)154 static inline struct rkisp_buffer *to_rkisp_buffer(struct vb2_v4l2_buffer *vb)
155 {
156 	return container_of(vb, struct rkisp_buffer, vb);
157 }
158 
to_vb2_queue(struct file * file)159 static inline struct vb2_queue *to_vb2_queue(struct file *file)
160 {
161 	struct rkisp_vdev_node *vnode = video_drvdata(file);
162 
163 	return &vnode->buf_queue;
164 }
165 
166 void rkisp_write(struct rkisp_device *dev, u32 reg, u32 val, bool is_direct);
167 u32 rkisp_read(struct rkisp_device *dev, u32 reg, bool is_direct);
168 void rkisp_set_bits(struct rkisp_device *dev, u32 reg, u32 mask, u32 val, bool is_direct);
169 void rkisp_clear_bits(struct rkisp_device *dev, u32 reg, u32 mask, bool is_direct);
170 
171 void rkisp_write_reg_cache(struct rkisp_device *dev, u32 reg, u32 val);
172 u32 rkisp_read_reg_cache(struct rkisp_device *dev, u32 reg);
173 void rkisp_set_reg_cache_bits(struct rkisp_device *dev, u32 reg, u32 mask, u32 val);
174 void rkisp_clear_reg_cache_bits(struct rkisp_device *dev, u32 reg, u32 mask);
175 
176 /* for dual isp, config for next isp reg */
177 void rkisp_next_write(struct rkisp_device *dev, u32 reg, u32 val, bool is_direct);
178 u32 rkisp_next_read(struct rkisp_device *dev, u32 reg, bool is_direct);
179 void rkisp_next_set_bits(struct rkisp_device *dev, u32 reg, u32 mask, u32 val, bool is_direct);
180 void rkisp_next_clear_bits(struct rkisp_device *dev, u32 reg, u32 mask, bool is_direct);
181 
182 void rkisp_next_write_reg_cache(struct rkisp_device *dev, u32 reg, u32 val);
183 u32 rkisp_next_read_reg_cache(struct rkisp_device *dev, u32 reg);
184 void rkisp_next_set_reg_cache_bits(struct rkisp_device *dev, u32 reg, u32 mask, u32 val);
185 void rkisp_next_clear_reg_cache_bits(struct rkisp_device *dev, u32 reg, u32 mask);
186 
187 static inline void
rkisp_unite_write(struct rkisp_device * dev,u32 reg,u32 val,bool is_direct,bool is_unite)188 rkisp_unite_write(struct rkisp_device *dev, u32 reg, u32 val, bool is_direct, bool is_unite)
189 {
190 	rkisp_write(dev, reg, val, is_direct);
191 	if (is_unite)
192 		rkisp_next_write(dev, reg, val, is_direct);
193 }
194 
195 static inline void
rkisp_unite_set_bits(struct rkisp_device * dev,u32 reg,u32 mask,u32 val,bool is_direct,bool is_unite)196 rkisp_unite_set_bits(struct rkisp_device *dev, u32 reg, u32 mask,
197 		     u32 val, bool is_direct, bool is_unite)
198 {
199 	rkisp_set_bits(dev, reg, mask, val, is_direct);
200 	if (is_unite)
201 		rkisp_next_set_bits(dev, reg, mask, val, is_direct);
202 }
203 
204 static inline void
rkisp_unite_clear_bits(struct rkisp_device * dev,u32 reg,u32 mask,bool is_direct,bool is_unite)205 rkisp_unite_clear_bits(struct rkisp_device *dev, u32 reg, u32 mask,
206 		       bool is_direct, bool is_unite)
207 {
208 	rkisp_clear_bits(dev, reg, mask, is_direct);
209 	if (is_unite)
210 		rkisp_next_clear_bits(dev, reg, mask, is_direct);
211 }
212 
213 void rkisp_update_regs(struct rkisp_device *dev, u32 start, u32 end);
214 
215 int rkisp_alloc_buffer(struct rkisp_device *dev, struct rkisp_dummy_buffer *buf);
216 void rkisp_free_buffer(struct rkisp_device *dev, struct rkisp_dummy_buffer *buf);
217 void rkisp_prepare_buffer(struct rkisp_device *dev, struct rkisp_dummy_buffer *buf);
218 void rkisp_finish_buffer(struct rkisp_device *dev, struct rkisp_dummy_buffer *buf);
219 
220 int rkisp_attach_hw(struct rkisp_device *isp);
221 int rkisp_alloc_common_dummy_buf(struct rkisp_device *dev);
222 void rkisp_free_common_dummy_buf(struct rkisp_device *dev);
223 
224 void rkisp_set_clk_rate(struct clk *clk, unsigned long rate);
225 #endif /* _RKISP_COMMON_H */
226