• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
2 /*
3  * vin_video.h for video api
4  *
5  * Copyright (c) 2017 by Allwinnertech Co., Ltd.  http://www.allwinnertech.com
6  *
7  * Authors:  Zhao Wei <zhaowei@allwinnertech.com>
8  * Yang Feng <yangfeng@allwinnertech.com>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License version 2 as
12  * published by the Free Software Foundation.
13  */
14 
15 #ifndef _VIN_VIDEO_H_
16 #define _VIN_VIDEO_H_
17 
18 #include <linux/types.h>
19 #include <linux/interrupt.h>
20 #include <linux/i2c.h>
21 #include <linux/workqueue.h>
22 #include <linux/pm_runtime.h>
23 #include <media/v4l2-ctrls.h>
24 #include <media/v4l2-device.h>
25 #include <media/v4l2-ioctl.h>
26 #include <media/v4l2-common.h>
27 #include <media/v4l2-mediabus.h>
28 #include <media/v4l2-subdev.h>
29 #include <media/videobuf2-v4l2.h>
30 #include "../platform/platform_cfg.h"
31 #include "../vin-vipp/sunxi_scaler.h"
32 #include "dma_reg.h"
33 
34 /* buffer for one video frame */
35 struct vin_buffer {
36 	struct vb2_v4l2_buffer vb;
37 	struct list_head list;
38 	void *paddr;
39 	enum vb2_buffer_state state;
40 };
41 
42 #define VIN_SD_PAD_SINK		0
43 #define VIN_SD_PAD_SOURCE	1
44 #define VIN_SD_PADS_NUM		2
45 
46 enum vin_subdev_ind {
47 	VIN_IND_SENSOR,
48 	VIN_IND_MIPI,
49 	VIN_IND_CSI,
50 	VIN_IND_ISP,
51 	VIN_IND_SCALER,
52 	VIN_IND_CAPTURE,
53 	VIN_IND_TDM_RX,
54 	VIN_IND_ACTUATOR,
55 	VIN_IND_FLASH,
56 	VIN_IND_STAT,
57 	VIN_IND_MAX,
58 };
59 
60 enum vin_state_flags {
61 	VIN_LPM,
62 	VIN_STREAM,
63 	VIN_BUSY,
64 	VIN_STATE_MAX,
65 };
66 
67 #define vin_lpm(dev) test_bit(VIN_LPM, &(dev)->state)
68 #define vin_busy(dev) test_bit(VIN_BUSY, &(dev)->state)
69 #define vin_streaming(dev) test_bit(VIN_STREAM, &(dev)->state)
70 #define CEIL_EXP(a, b) (((a)>>(b)) + (((a)&((1<<(b))-1)) ? 1 : 0))
71 
72 struct timeval {
73 	long		tv_sec;		/* seconds */
74 	long		tv_usec;	/* microseconds */
75 };
76 struct vin_pipeline {
77 	struct media_pipeline pipe;
78 	struct v4l2_subdev *sd[VIN_IND_MAX];
79 };
80 
81 enum vin_fmt_flags {
82 	VIN_FMT_YUV = (1 << 0),
83 	VIN_FMT_RAW = (1 << 1),
84 	VIN_FMT_RGB = (1 << 2),
85 	VIN_FMT_OSD = (1 << 3),
86 	VIN_FMT_MAX,
87 	/* all possible flags raised */
88 	VIN_FMT_ALL = (((VIN_FMT_MAX - 1) << 1) - 1),
89 };
90 
91 
92 struct vin_fmt {
93 	u32	mbus_code;
94 	char	*name;
95 	u32	mbus_type;
96 	u32	fourcc;
97 	u16	memplanes;
98 	u16	colplanes;
99 	u8	depth[VIDEO_MAX_PLANES];
100 	u16	mdataplanes;
101 	u16	flags;
102 	enum v4l2_colorspace color;
103 	enum v4l2_field	field;
104 };
105 
106 struct vin_addr {
107 	u32	y;
108 	u32	cb;
109 	u32	cr;
110 };
111 
112 struct vin_frame {
113 	u32	o_width;
114 	u32	o_height;
115 	u32	offs_h;
116 	u32	offs_v;
117 	u32	width;
118 	u32	height;
119 	unsigned long		payload[VIDEO_MAX_PLANES];
120 	unsigned long		bytesperline[VIDEO_MAX_PLANES];
121 	struct vin_addr	paddr;
122 	struct vin_fmt	fmt;
123 };
124 
125 /* osd settings */
126 struct vin_osd {
127 	u8 is_set;
128 	u8 ov_set_cnt;
129 	u8 overlay_en;
130 	u8 cover_en;
131 	u8 orl_en;
132 	u8 overlay_cnt;
133 	u8 cover_cnt;
134 	u8 orl_cnt;
135 	u8 inv_th;
136 	u8 inverse_close[MAX_OVERLAY_NUM];
137 	u8 inv_w_rgn[MAX_OVERLAY_NUM];
138 	u8 inv_h_rgn[MAX_OVERLAY_NUM];
139 	u8 global_alpha[MAX_OVERLAY_NUM];
140 	u8 y_bmp_avp[MAX_OVERLAY_NUM];
141 	u8 yuv_cover[3][MAX_COVER_NUM];
142 	u8 yuv_orl[3][MAX_ORL_NUM];
143 	u8 orl_width;
144 	int chromakey;
145 	enum vipp_osd_argb overlay_fmt;
146 	struct vin_mm ov_mask[2];	/* bitmap addr */
147 	struct v4l2_rect ov_win[MAX_OVERLAY_NUM];	/* position */
148 	struct v4l2_rect cv_win[MAX_COVER_NUM];	/* position */
149 	struct v4l2_rect orl_win[MAX_ORL_NUM];	/* position */
150 	int rgb_cover[MAX_COVER_NUM];
151 	int rgb_orl[MAX_ORL_NUM];
152 	struct vin_fmt *fmt;
153 };
154 
155 struct vin_vid_cap {
156 	struct video_device vdev;
157 	struct vin_frame frame;
158 	struct vin_osd osd;
159 	/* video capture */
160 	struct vb2_queue vb_vidq;
161 	struct device *dev;
162 	struct list_head vidq_active;
163 	struct list_head vidq_done;
164 	unsigned int isp_wdr_mode;
165 	unsigned int capture_mode;
166 	unsigned int buf_byte_size; /* including main and thumb buffer */
167 	/*working state */
168 	bool registered;
169 	bool special_active;
170 	bool dma_parms_alloc;
171 	struct mutex lock;
172 	unsigned int first_flag; /* indicate the first time triggering irq */
173 	struct timeval ts;
174 	spinlock_t slock;
175 	struct vin_pipeline pipe;
176 	struct vin_core *vinc;
177 	struct v4l2_subdev subdev;
178 	struct media_pad vd_pad;
179 	struct media_pad sd_pads[VIN_SD_PADS_NUM];
180 	bool user_subdev_api;
181 	struct v4l2_ctrl_handler ctrl_handler;
182 	struct v4l2_ctrl *ae_win[4];	/* wb win cluster */
183 	struct v4l2_ctrl *af_win[4];	/* af win cluster */
184 	struct work_struct s_stream_task;
185 	struct work_struct pipeline_reset_task;
186 	unsigned long state;
187 	unsigned int frame_delay_cnt;
188 	struct dma_lbc_cmp lbc_cmp;
189 	struct dma_bufa_threshold threshold;
190 	void (*vin_buffer_process)(int id);
191 };
192 
193 #if defined CONFIG_ARCH_SUN8IW12P1
vin_cmp(const void * a,const void * b)194 static inline int vin_cmp(const void *a, const void *b)
195 {
196 	return *(int *)a - *(int *)b;
197 }
198 
vin_swap(void * a,void * b,int size)199 static inline void vin_swap(void *a, void *b, int size)
200 {
201 	int t = *(int *)a;
202 	*(int *)a = *(int *)b;
203 	*(int *)b = t;
204 }
205 
vin_unique(int * a,int number)206 static inline int vin_unique(int *a, int number)
207 {
208 	int i, k = 0;
209 
210 	for (i = 1; i < number; i++) {
211 		if (a[k] != a[i]) {
212 			k++;
213 			a[k] = a[i];
214 		}
215 	}
216 	return k + 1;
217 }
218 #endif
219 int vin_set_addr(struct vin_core *vinc, struct vb2_buffer *vb,
220 		      struct vin_frame *frame, struct vin_addr *paddr);
221 int vin_timer_init(struct vin_core *vinc);
222 void vin_timer_del(struct vin_core *vinc);
223 void vin_timer_update(struct vin_core *vinc, int ms);
224 int sensor_flip_option(struct vin_vid_cap *cap, struct v4l2_control c);
225 void vin_set_next_buf_addr(struct vin_core *vinc);
226 void vin_get_rest_buf_cnt(struct vin_core *vinc);
227 int vin_initialize_capture_subdev(struct vin_core *vinc);
228 void vin_cleanup_capture_subdev(struct vin_core *vinc);
229 
230 #endif /*_VIN_VIDEO_H_*/
231