1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * Copyright (C) 2013 - 2014 Texas Instruments, Inc.
4  *
5  * Benoit Parrot <bparrot@ti.com>
6  * Lad, Prabhakar <prabhakar.csengg@gmail.com>
7  */
8 
9 #ifndef AM437X_VPFE_H
10 #define AM437X_VPFE_H
11 
12 #include <linux/am437x-vpfe.h>
13 #include <linux/clk.h>
14 #include <linux/completion.h>
15 #include <linux/device.h>
16 #include <linux/io.h>
17 #include <linux/i2c.h>
18 #include <linux/videodev2.h>
19 
20 #include <media/v4l2-dev.h>
21 #include <media/v4l2-device.h>
22 #include <media/v4l2-ioctl.h>
23 #include <media/videobuf2-v4l2.h>
24 #include <media/videobuf2-dma-contig.h>
25 
26 #include "am437x-vpfe_regs.h"
27 
28 enum vpfe_pin_pol {
29 	VPFE_PINPOL_POSITIVE = 0,
30 	VPFE_PINPOL_NEGATIVE,
31 };
32 
33 enum vpfe_hw_if_type {
34 	/* Raw Bayer */
35 	VPFE_RAW_BAYER = 0,
36 	/* BT656 - 8 bit */
37 	VPFE_BT656,
38 	/* BT656 - 10 bit */
39 	VPFE_BT656_10BIT,
40 	/* YCbCr - 8 bit with external sync */
41 	VPFE_YCBCR_SYNC_8,
42 	/* YCbCr - 16 bit with external sync */
43 	VPFE_YCBCR_SYNC_16,
44 };
45 
46 /* interface description */
47 struct vpfe_hw_if_param {
48 	enum vpfe_hw_if_type if_type;
49 	enum vpfe_pin_pol hdpol;
50 	enum vpfe_pin_pol vdpol;
51 	unsigned int bus_width;
52 };
53 
54 #define VPFE_MAX_SUBDEV		1
55 #define VPFE_MAX_INPUTS		1
56 
57 struct vpfe_std_info {
58 	int active_pixels;
59 	int active_lines;
60 	/* current frame format */
61 	int frame_format;
62 };
63 
64 struct vpfe_route {
65 	u32 input;
66 	u32 output;
67 };
68 
69 struct vpfe_subdev_info {
70 	/* Sub device group id */
71 	int grp_id;
72 	/* inputs available at the sub device */
73 	struct v4l2_input inputs[VPFE_MAX_INPUTS];
74 	/* Sub dev routing information for each input */
75 	struct vpfe_route *routes;
76 	/* check if sub dev supports routing */
77 	int can_route;
78 	/* ccdc bus/interface configuration */
79 	struct vpfe_hw_if_param vpfe_param;
80 	struct v4l2_subdev *sd;
81 };
82 
83 struct vpfe_config {
84 	/* information about each subdev */
85 	struct vpfe_subdev_info sub_devs[VPFE_MAX_SUBDEV];
86 	/* Flat array, arranged in groups */
87 	struct v4l2_async_subdev *asd[VPFE_MAX_SUBDEV];
88 };
89 
90 struct vpfe_cap_buffer {
91 	struct vb2_v4l2_buffer vb;
92 	struct list_head list;
93 };
94 
95 enum ccdc_pixfmt {
96 	CCDC_PIXFMT_RAW = 0,
97 	CCDC_PIXFMT_YCBCR_16BIT,
98 	CCDC_PIXFMT_YCBCR_8BIT,
99 };
100 
101 enum ccdc_frmfmt {
102 	CCDC_FRMFMT_PROGRESSIVE = 0,
103 	CCDC_FRMFMT_INTERLACED,
104 };
105 
106 /* PIXEL ORDER IN MEMORY from LSB to MSB */
107 /* only applicable for 8-bit input mode  */
108 enum ccdc_pixorder {
109 	CCDC_PIXORDER_YCBYCR,
110 	CCDC_PIXORDER_CBYCRY,
111 };
112 
113 enum ccdc_buftype {
114 	CCDC_BUFTYPE_FLD_INTERLEAVED,
115 	CCDC_BUFTYPE_FLD_SEPARATED
116 };
117 
118 
119 /* returns the highest bit used for the gamma */
120 static inline u8 ccdc_gamma_width_max_bit(enum vpfe_ccdc_gamma_width width)
121 {
122 	return 15 - width;
123 }
124 
125 /* returns the highest bit used for this data size */
126 static inline u8 ccdc_data_size_max_bit(enum vpfe_ccdc_data_size sz)
127 {
128 	return sz == VPFE_CCDC_DATA_8BITS ? 7 : 15 - sz;
129 }
130 
131 /* Structure for CCDC configuration parameters for raw capture mode */
132 struct ccdc_params_raw {
133 	/* pixel format */
134 	enum ccdc_pixfmt pix_fmt;
135 	/* progressive or interlaced frame */
136 	enum ccdc_frmfmt frm_fmt;
137 	struct v4l2_rect win;
138 	/* Current Format Bytes Per Pixels */
139 	unsigned int bytesperpixel;
140 	/* Current Format Bytes per Lines
141 	 * (Aligned to 32 bytes) used for HORZ_INFO
142 	 */
143 	unsigned int bytesperline;
144 	/* field id polarity */
145 	enum vpfe_pin_pol fid_pol;
146 	/* vertical sync polarity */
147 	enum vpfe_pin_pol vd_pol;
148 	/* horizontal sync polarity */
149 	enum vpfe_pin_pol hd_pol;
150 	/* interleaved or separated fields */
151 	enum ccdc_buftype buf_type;
152 	/*
153 	 * enable to store the image in inverse
154 	 * order in memory(bottom to top)
155 	 */
156 	unsigned char image_invert_enable;
157 	/* configurable parameters */
158 	struct vpfe_ccdc_config_params_raw config_params;
159 };
160 
161 struct ccdc_params_ycbcr {
162 	/* pixel format */
163 	enum ccdc_pixfmt pix_fmt;
164 	/* progressive or interlaced frame */
165 	enum ccdc_frmfmt frm_fmt;
166 	struct v4l2_rect win;
167 	/* Current Format Bytes Per Pixels */
168 	unsigned int bytesperpixel;
169 	/* Current Format Bytes per Lines
170 	 * (Aligned to 32 bytes) used for HORZ_INFO
171 	 */
172 	unsigned int bytesperline;
173 	/* field id polarity */
174 	enum vpfe_pin_pol fid_pol;
175 	/* vertical sync polarity */
176 	enum vpfe_pin_pol vd_pol;
177 	/* horizontal sync polarity */
178 	enum vpfe_pin_pol hd_pol;
179 	/* enable BT.656 embedded sync mode */
180 	int bt656_enable;
181 	/* cb:y:cr:y or y:cb:y:cr in memory */
182 	enum ccdc_pixorder pix_order;
183 	/* interleaved or separated fields  */
184 	enum ccdc_buftype buf_type;
185 };
186 
187 /*
188  * CCDC operational configuration
189  */
190 struct ccdc_config {
191 	/* CCDC interface type */
192 	enum vpfe_hw_if_type if_type;
193 	/* Raw Bayer configuration */
194 	struct ccdc_params_raw bayer;
195 	/* YCbCr configuration */
196 	struct ccdc_params_ycbcr ycbcr;
197 	/* ccdc base address */
198 	void __iomem *base_addr;
199 };
200 
201 struct vpfe_ccdc {
202 	struct ccdc_config ccdc_cfg;
203 	u32 ccdc_ctx[VPFE_REG_END / sizeof(u32)];
204 };
205 
206 /*
207  * struct vpfe_fmt - VPFE media bus format information
208  * fourcc: V4L2 pixel format code
209  * code: V4L2 media bus format code
210  * bitsperpixel: Bits per pixel over the bus
211  */
212 struct vpfe_fmt {
213 	u32 fourcc;
214 	u32 code;
215 	u32 bitsperpixel;
216 };
217 
218 /*
219  * When formats[] is modified make sure to adjust this value also.
220  * Expect compile time warnings if VPFE_NUM_FORMATS is smaller then
221  * the number of elements in formats[].
222  */
223 #define VPFE_NUM_FORMATS	10
224 
225 struct vpfe_device {
226 	/* V4l2 specific parameters */
227 	/* Identifies video device for this channel */
228 	struct video_device video_dev;
229 	/* sub devices */
230 	struct v4l2_subdev **sd;
231 	/* vpfe cfg */
232 	struct vpfe_config *cfg;
233 	/* V4l2 device */
234 	struct v4l2_device v4l2_dev;
235 	/* parent device */
236 	struct device *pdev;
237 	/* subdevice async Notifier */
238 	struct v4l2_async_notifier notifier;
239 	/* Indicates id of the field which is being displayed */
240 	unsigned field;
241 	unsigned sequence;
242 	/* current interface type */
243 	struct vpfe_hw_if_param vpfe_if_params;
244 	/* ptr to currently selected sub device */
245 	struct vpfe_subdev_info *current_subdev;
246 	/* current input at the sub device */
247 	int current_input;
248 	/* Keeps track of the information about the standard */
249 	struct vpfe_std_info std_info;
250 	/* std index into std table */
251 	int std_index;
252 	/* IRQs used when CCDC output to SDRAM */
253 	unsigned int irq;
254 	/* Pointer pointing to current v4l2_buffer */
255 	struct vpfe_cap_buffer *cur_frm;
256 	/* Pointer pointing to next v4l2_buffer */
257 	struct vpfe_cap_buffer *next_frm;
258 	/* Used to store pixel format */
259 	struct v4l2_format fmt;
260 	/* Used to keep a reference to the current vpfe_fmt */
261 	struct vpfe_fmt *current_vpfe_fmt;
262 	struct vpfe_fmt	*active_fmt[VPFE_NUM_FORMATS];
263 	unsigned int num_active_fmt;
264 
265 	/*
266 	 * used when IMP is chained to store the crop window which
267 	 * is different from the image window
268 	 */
269 	struct v4l2_rect crop;
270 	/* Buffer queue used in vb2 */
271 	struct vb2_queue buffer_queue;
272 	/* Queue of filled frames */
273 	struct list_head dma_queue;
274 	/* IRQ lock for DMA queue */
275 	spinlock_t dma_queue_lock;
276 	/* lock used to access this structure */
277 	struct mutex lock;
278 	/*
279 	 * offset where second field starts from the starting of the
280 	 * buffer for field separated YCbCr formats
281 	 */
282 	u32 field_off;
283 	struct vpfe_ccdc ccdc;
284 	int stopping;
285 	struct completion capture_stop;
286 };
287 
288 #endif	/* AM437X_VPFE_H */
289