• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /****************************************************************************
3  *
4  *  Filename: cpia2.h
5  *
6  *  Copyright 2001, STMicrolectronics, Inc.
7  *
8  *  Contact:  steve.miller@st.com
9  *
10  *  Description:
11  *     This is a USB driver for CPiA2 based video cameras.
12  *
13  *     This driver is modelled on the cpia usb driver by
14  *     Jochen Scharrlach and Johannes Erdfeldt.
15  *
16  ****************************************************************************/
17 
18 #ifndef __CPIA2_H__
19 #define __CPIA2_H__
20 
21 #include <linux/videodev2.h>
22 #include <linux/usb.h>
23 #include <linux/poll.h>
24 #include <media/v4l2-common.h>
25 #include <media/v4l2-device.h>
26 #include <media/v4l2-ctrls.h>
27 
28 #include "cpia2_registers.h"
29 
30 /* define for verbose debug output */
31 //#define _CPIA2_DEBUG_
32 
33 /***
34  * Image defines
35  ***/
36 
37 /*  Misc constants */
38 #define ALLOW_CORRUPT 0		/* Causes collater to discard checksum */
39 
40 /* USB Transfer mode */
41 #define XFER_ISOC 0
42 #define XFER_BULK 1
43 
44 /* USB Alternates */
45 #define USBIF_CMDONLY 0
46 #define USBIF_BULK 1
47 #define USBIF_ISO_1 2	/*  128 bytes/ms */
48 #define USBIF_ISO_2 3	/*  384 bytes/ms */
49 #define USBIF_ISO_3 4	/*  640 bytes/ms */
50 #define USBIF_ISO_4 5	/*  768 bytes/ms */
51 #define USBIF_ISO_5 6	/*  896 bytes/ms */
52 #define USBIF_ISO_6 7	/* 1023 bytes/ms */
53 
54 /* Flicker Modes */
55 #define NEVER_FLICKER   0
56 #define FLICKER_60      60
57 #define FLICKER_50      50
58 
59 /* Debug flags */
60 #define DEBUG_NONE          0
61 #define DEBUG_REG           0x00000001
62 #define DEBUG_DUMP_PATCH    0x00000002
63 #define DEBUG_DUMP_REGS     0x00000004
64 
65 /***
66  * Video frame sizes
67  ***/
68 enum {
69 	VIDEOSIZE_VGA = 0,	/* 640x480 */
70 	VIDEOSIZE_CIF,		/* 352x288 */
71 	VIDEOSIZE_QVGA,		/* 320x240 */
72 	VIDEOSIZE_QCIF,		/* 176x144 */
73 	VIDEOSIZE_288_216,
74 	VIDEOSIZE_256_192,
75 	VIDEOSIZE_224_168,
76 	VIDEOSIZE_192_144,
77 };
78 
79 #define STV_IMAGE_CIF_ROWS    288
80 #define STV_IMAGE_CIF_COLS    352
81 
82 #define STV_IMAGE_QCIF_ROWS   144
83 #define STV_IMAGE_QCIF_COLS   176
84 
85 #define STV_IMAGE_VGA_ROWS    480
86 #define STV_IMAGE_VGA_COLS    640
87 
88 #define STV_IMAGE_QVGA_ROWS   240
89 #define STV_IMAGE_QVGA_COLS   320
90 
91 #define JPEG_MARKER_COM (1<<6)	/* Comment segment */
92 
93 /***
94  * Enums
95  ***/
96 /* Sensor types available with cpia2 asics */
97 enum sensors {
98 	CPIA2_SENSOR_410,
99 	CPIA2_SENSOR_500
100 };
101 
102 /* Asic types available in the CPiA2 architecture */
103 #define  CPIA2_ASIC_672 0x67
104 
105 /* Device types (stv672, stv676, etc) */
106 #define  DEVICE_STV_672   0x0001
107 #define  DEVICE_STV_676   0x0002
108 
109 enum frame_status {
110 	FRAME_EMPTY,
111 	FRAME_READING,		/* In the process of being grabbed into */
112 	FRAME_READY,		/* Ready to be read */
113 	FRAME_ERROR,
114 };
115 
116 /***
117  * Register access (for USB request byte)
118  ***/
119 enum {
120 	CAMERAACCESS_SYSTEM = 0,
121 	CAMERAACCESS_VC,
122 	CAMERAACCESS_VP,
123 	CAMERAACCESS_IDATA
124 };
125 
126 #define CAMERAACCESS_TYPE_BLOCK    0x00
127 #define CAMERAACCESS_TYPE_RANDOM   0x04
128 #define CAMERAACCESS_TYPE_MASK     0x08
129 #define CAMERAACCESS_TYPE_REPEAT   0x0C
130 
131 #define TRANSFER_READ 0
132 #define TRANSFER_WRITE 1
133 
134 #define DEFAULT_ALT   USBIF_ISO_6
135 #define DEFAULT_BRIGHTNESS 0x46
136 #define DEFAULT_CONTRAST 0x93
137 #define DEFAULT_SATURATION 0x7f
138 
139 /* Power state */
140 #define HI_POWER_MODE CPIA2_SYSTEM_CONTROL_HIGH_POWER
141 #define LO_POWER_MODE CPIA2_SYSTEM_CONTROL_LOW_POWER
142 
143 
144 /********
145  * Commands
146  *******/
147 enum {
148 	CPIA2_CMD_NONE = 0,
149 	CPIA2_CMD_GET_VERSION,
150 	CPIA2_CMD_GET_PNP_ID,
151 	CPIA2_CMD_GET_ASIC_TYPE,
152 	CPIA2_CMD_GET_SENSOR,
153 	CPIA2_CMD_GET_VP_DEVICE,
154 	CPIA2_CMD_GET_VP_BRIGHTNESS,
155 	CPIA2_CMD_SET_VP_BRIGHTNESS,
156 	CPIA2_CMD_GET_CONTRAST,
157 	CPIA2_CMD_SET_CONTRAST,
158 	CPIA2_CMD_GET_VP_SATURATION,
159 	CPIA2_CMD_SET_VP_SATURATION,
160 	CPIA2_CMD_GET_VP_GPIO_DIRECTION,
161 	CPIA2_CMD_SET_VP_GPIO_DIRECTION,
162 	CPIA2_CMD_GET_VP_GPIO_DATA,
163 	CPIA2_CMD_SET_VP_GPIO_DATA,
164 	CPIA2_CMD_GET_VC_MP_GPIO_DIRECTION,
165 	CPIA2_CMD_SET_VC_MP_GPIO_DIRECTION,
166 	CPIA2_CMD_GET_VC_MP_GPIO_DATA,
167 	CPIA2_CMD_SET_VC_MP_GPIO_DATA,
168 	CPIA2_CMD_ENABLE_PACKET_CTRL,
169 	CPIA2_CMD_GET_FLICKER_MODES,
170 	CPIA2_CMD_SET_FLICKER_MODES,
171 	CPIA2_CMD_RESET_FIFO,	/* clear fifo and enable stream block */
172 	CPIA2_CMD_SET_HI_POWER,
173 	CPIA2_CMD_SET_LOW_POWER,
174 	CPIA2_CMD_CLEAR_V2W_ERR,
175 	CPIA2_CMD_SET_USER_MODE,
176 	CPIA2_CMD_GET_USER_MODE,
177 	CPIA2_CMD_FRAMERATE_REQ,
178 	CPIA2_CMD_SET_COMPRESSION_STATE,
179 	CPIA2_CMD_GET_WAKEUP,
180 	CPIA2_CMD_SET_WAKEUP,
181 	CPIA2_CMD_GET_PW_CONTROL,
182 	CPIA2_CMD_SET_PW_CONTROL,
183 	CPIA2_CMD_GET_SYSTEM_CTRL,
184 	CPIA2_CMD_SET_SYSTEM_CTRL,
185 	CPIA2_CMD_GET_VP_SYSTEM_STATE,
186 	CPIA2_CMD_GET_VP_SYSTEM_CTRL,
187 	CPIA2_CMD_SET_VP_SYSTEM_CTRL,
188 	CPIA2_CMD_GET_VP_EXP_MODES,
189 	CPIA2_CMD_SET_VP_EXP_MODES,
190 	CPIA2_CMD_GET_DEVICE_CONFIG,
191 	CPIA2_CMD_SET_DEVICE_CONFIG,
192 	CPIA2_CMD_SET_SERIAL_ADDR,
193 	CPIA2_CMD_SET_SENSOR_CR1,
194 	CPIA2_CMD_GET_VC_CONTROL,
195 	CPIA2_CMD_SET_VC_CONTROL,
196 	CPIA2_CMD_SET_TARGET_KB,
197 	CPIA2_CMD_SET_DEF_JPEG_OPT,
198 	CPIA2_CMD_REHASH_VP4,
199 	CPIA2_CMD_GET_USER_EFFECTS,
200 	CPIA2_CMD_SET_USER_EFFECTS
201 };
202 
203 enum user_cmd {
204 	COMMAND_NONE = 0x00000001,
205 	COMMAND_SET_FPS = 0x00000002,
206 	COMMAND_SET_COLOR_PARAMS = 0x00000004,
207 	COMMAND_GET_COLOR_PARAMS = 0x00000008,
208 	COMMAND_SET_FORMAT = 0x00000010,	/* size, etc */
209 	COMMAND_SET_FLICKER = 0x00000020
210 };
211 
212 /***
213  * Some defines specific to the 676 chip
214  ***/
215 #define CAMACC_CIF      0x01
216 #define CAMACC_VGA      0x02
217 #define CAMACC_QCIF     0x04
218 #define CAMACC_QVGA     0x08
219 
220 
221 struct cpia2_register {
222 	u8 index;
223 	u8 value;
224 };
225 
226 struct cpia2_reg_mask {
227 	u8 index;
228 	u8 and_mask;
229 	u8 or_mask;
230 	u8 fill;
231 };
232 
233 struct cpia2_command {
234 	u32 command;
235 	u8 req_mode;		/* (Block or random) | registerBank */
236 	u8 reg_count;
237 	u8 direction;
238 	u8 start;
239 	union reg_types {
240 		struct cpia2_register registers[32];
241 		struct cpia2_reg_mask masks[16];
242 		u8 block_data[64];
243 		u8 *patch_data;	/* points to function defined block */
244 	} buffer;
245 };
246 
247 struct camera_params {
248 	struct {
249 		u8 firmware_revision_hi; /* For system register set (bank 0) */
250 		u8 firmware_revision_lo;
251 		u8 asic_id;	/* Video Compressor set (bank 1) */
252 		u8 asic_rev;
253 		u8 vp_device_hi;	/* Video Processor set (bank 2) */
254 		u8 vp_device_lo;
255 		u8 sensor_flags;
256 		u8 sensor_rev;
257 	} version;
258 
259 	struct {
260 		u32 device_type;     /* enumerated from vendor/product ids.
261 				      * Currently, either STV_672 or STV_676 */
262 		u16 vendor;
263 		u16 product;
264 		u16 device_revision;
265 	} pnp_id;
266 
267 	struct {
268 		u8 brightness;	/* CPIA2_VP_EXPOSURE_TARGET */
269 		u8 contrast;	/* Note: this is CPIA2_VP_YRANGE */
270 		u8 saturation;	/*  CPIA2_VP_SATURATION */
271 	} color_params;
272 
273 	struct {
274 		u8 cam_register;
275 		u8 flicker_mode_req;	/* 1 if flicker on, else never flicker */
276 	} flicker_control;
277 
278 	struct {
279 		u8 jpeg_options;
280 		u8 creep_period;
281 		u8 user_squeeze;
282 		u8 inhibit_htables;
283 	} compression;
284 
285 	struct {
286 		u8 ohsize;	/* output image size */
287 		u8 ovsize;
288 		u8 hcrop;	/* cropping start_pos/4 */
289 		u8 vcrop;
290 		u8 hphase;	/* scaling registers */
291 		u8 vphase;
292 		u8 hispan;
293 		u8 vispan;
294 		u8 hicrop;
295 		u8 vicrop;
296 		u8 hifraction;
297 		u8 vifraction;
298 	} image_size;
299 
300 	struct {
301 		int width;	/* actual window width */
302 		int height;	/* actual window height */
303 	} roi;
304 
305 	struct {
306 		u8 video_mode;
307 		u8 frame_rate;
308 		u8 video_size;	/* Not a register, just a convenience for cropped sizes */
309 		u8 gpio_direction;
310 		u8 gpio_data;
311 		u8 system_ctrl;
312 		u8 system_state;
313 		u8 lowlight_boost;	/* Bool: 0 = off, 1 = on */
314 		u8 device_config;
315 		u8 exposure_modes;
316 		u8 user_effects;
317 	} vp_params;
318 
319 	struct {
320 		u8 pw_control;
321 		u8 wakeup;
322 		u8 vc_control;
323 		u8 vc_mp_direction;
324 		u8 vc_mp_data;
325 		u8 quality;
326 	} vc_params;
327 
328 	struct {
329 		u8 power_mode;
330 		u8 system_ctrl;
331 		u8 stream_mode;	/* This is the current alternate for usb drivers */
332 		u8 allow_corrupt;
333 	} camera_state;
334 };
335 
336 #define NUM_SBUF    2
337 
338 struct cpia2_sbuf {
339 	char *data;
340 	struct urb *urb;
341 };
342 
343 struct framebuf {
344 	u64 ts;
345 	unsigned long seq;
346 	int num;
347 	int length;
348 	int max_length;
349 	volatile enum frame_status status;
350 	u8 *data;
351 	struct framebuf *next;
352 };
353 
354 struct camera_data {
355 	/* locks */
356 	struct v4l2_device v4l2_dev;
357 	struct mutex v4l2_lock;	/* serialize file operations */
358 	struct v4l2_ctrl_handler hdl;
359 	struct {
360 		/* Lights control cluster */
361 		struct v4l2_ctrl *top_light;
362 		struct v4l2_ctrl *bottom_light;
363 	};
364 	struct v4l2_ctrl *usb_alt;
365 
366 	/* camera status */
367 	int first_image_seen;
368 	enum sensors sensor_type;
369 	u8 flush;
370 	struct v4l2_fh *stream_fh;
371 	u8 mmapped;
372 	int streaming;		/* 0 = no, 1 = yes */
373 	int xfer_mode;		/* XFER_BULK or XFER_ISOC */
374 	struct camera_params params;	/* camera settings */
375 
376 	/* v4l */
377 	int video_size;			/* VIDEO_SIZE_ */
378 	struct video_device vdev;	/* v4l videodev */
379 	u32 width;
380 	u32 height;			/* Its size */
381 	__u32 pixelformat;       /* Format fourcc      */
382 
383 	/* USB */
384 	struct usb_device *dev;
385 	unsigned char iface;
386 	unsigned int cur_alt;
387 	unsigned int old_alt;
388 	struct cpia2_sbuf sbuf[NUM_SBUF];	/* Double buffering */
389 
390 	wait_queue_head_t wq_stream;
391 
392 	/* Buffering */
393 	u32 frame_size;
394 	int num_frames;
395 	unsigned long frame_count;
396 	u8 *frame_buffer;	/* frame buffer data */
397 	struct framebuf *buffers;
398 	struct framebuf * volatile curbuff;
399 	struct framebuf *workbuff;
400 
401 	/* MJPEG Extension */
402 	int APPn;		/* Number of APP segment to be written, must be 0..15 */
403 	int APP_len;		/* Length of data in JPEG APPn segment */
404 	char APP_data[60];	/* Data in the JPEG APPn segment. */
405 
406 	int COM_len;		/* Length of data in JPEG COM segment */
407 	char COM_data[60];	/* Data in JPEG COM segment */
408 };
409 
410 /* v4l */
411 int cpia2_register_camera(struct camera_data *cam);
412 void cpia2_unregister_camera(struct camera_data *cam);
413 void cpia2_camera_release(struct v4l2_device *v4l2_dev);
414 
415 /* core */
416 int cpia2_reset_camera(struct camera_data *cam);
417 int cpia2_set_low_power(struct camera_data *cam);
418 void cpia2_dbg_dump_registers(struct camera_data *cam);
419 int cpia2_match_video_size(int width, int height);
420 void cpia2_set_camera_state(struct camera_data *cam);
421 void cpia2_save_camera_state(struct camera_data *cam);
422 void cpia2_set_color_params(struct camera_data *cam);
423 void cpia2_set_brightness(struct camera_data *cam, unsigned char value);
424 void cpia2_set_contrast(struct camera_data *cam, unsigned char value);
425 void cpia2_set_saturation(struct camera_data *cam, unsigned char value);
426 int cpia2_set_flicker_mode(struct camera_data *cam, int mode);
427 void cpia2_set_format(struct camera_data *cam);
428 int cpia2_send_command(struct camera_data *cam, struct cpia2_command *cmd);
429 int cpia2_do_command(struct camera_data *cam,
430 		     unsigned int command,
431 		     unsigned char direction, unsigned char param);
432 void cpia2_deinit_camera_struct(struct camera_data *cam, struct usb_interface *intf);
433 struct camera_data *cpia2_init_camera_struct(struct usb_interface *intf);
434 int cpia2_init_camera(struct camera_data *cam);
435 int cpia2_allocate_buffers(struct camera_data *cam);
436 void cpia2_free_buffers(struct camera_data *cam);
437 long cpia2_read(struct camera_data *cam,
438 		char __user *buf, unsigned long count, int noblock);
439 __poll_t cpia2_poll(struct camera_data *cam,
440 			struct file *filp, poll_table *wait);
441 int cpia2_remap_buffer(struct camera_data *cam, struct vm_area_struct *vma);
442 void cpia2_set_property_flip(struct camera_data *cam, int prop_val);
443 void cpia2_set_property_mirror(struct camera_data *cam, int prop_val);
444 int cpia2_set_gpio(struct camera_data *cam, unsigned char setting);
445 int cpia2_set_fps(struct camera_data *cam, int framerate);
446 
447 /* usb */
448 int cpia2_usb_init(void);
449 void cpia2_usb_cleanup(void);
450 int cpia2_usb_transfer_cmd(struct camera_data *cam, void *registers,
451 			   u8 request, u8 start, u8 count, u8 direction);
452 int cpia2_usb_stream_start(struct camera_data *cam, unsigned int alternate);
453 int cpia2_usb_stream_stop(struct camera_data *cam);
454 int cpia2_usb_stream_pause(struct camera_data *cam);
455 int cpia2_usb_stream_resume(struct camera_data *cam);
456 int cpia2_usb_change_streaming_alternate(struct camera_data *cam,
457 					 unsigned int alt);
458 
459 
460 /* ----------------------- debug functions ---------------------- */
461 #ifdef _CPIA2_DEBUG_
462 #define ALOG(lev, fmt, args...) printk(lev "%s:%d %s(): " fmt, __FILE__, __LINE__, __func__, ## args)
463 #define LOG(fmt, args...) ALOG(KERN_INFO, fmt, ## args)
464 #define ERR(fmt, args...) ALOG(KERN_ERR, fmt, ## args)
465 #define DBG(fmt, args...) ALOG(KERN_DEBUG, fmt, ## args)
466 #else
467 #define ALOG(fmt,args...) printk(fmt,##args)
468 #define LOG(fmt,args...) ALOG(KERN_INFO "cpia2: "fmt,##args)
469 #define ERR(fmt,args...) ALOG(KERN_ERR "cpia2: "fmt,##args)
470 #define DBG(fmn,args...) do {} while(0)
471 #endif
472 /* No function or lineno, for shorter lines */
473 #define KINFO(fmt, args...) printk(KERN_INFO fmt,##args)
474 
475 #endif
476