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