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