18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-only */ 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Copyright (C) 2005-2006 Micronas USA Inc. 48c2ecf20Sopenharmony_ci */ 58c2ecf20Sopenharmony_ci 68c2ecf20Sopenharmony_ci/* 78c2ecf20Sopenharmony_ci * This is the private include file for the go7007 driver. It should not 88c2ecf20Sopenharmony_ci * be included by anybody but the driver itself, and especially not by 98c2ecf20Sopenharmony_ci * user-space applications. 108c2ecf20Sopenharmony_ci */ 118c2ecf20Sopenharmony_ci 128c2ecf20Sopenharmony_ci#include <media/v4l2-device.h> 138c2ecf20Sopenharmony_ci#include <media/v4l2-ctrls.h> 148c2ecf20Sopenharmony_ci#include <media/v4l2-fh.h> 158c2ecf20Sopenharmony_ci#include <media/videobuf2-v4l2.h> 168c2ecf20Sopenharmony_ci 178c2ecf20Sopenharmony_cistruct go7007; 188c2ecf20Sopenharmony_ci 198c2ecf20Sopenharmony_ci/* IDs to activate board-specific support code */ 208c2ecf20Sopenharmony_ci#define GO7007_BOARDID_MATRIX_II 0 218c2ecf20Sopenharmony_ci#define GO7007_BOARDID_MATRIX_RELOAD 1 228c2ecf20Sopenharmony_ci#define GO7007_BOARDID_STAR_TREK 2 238c2ecf20Sopenharmony_ci#define GO7007_BOARDID_PCI_VOYAGER 3 248c2ecf20Sopenharmony_ci#define GO7007_BOARDID_XMEN 4 258c2ecf20Sopenharmony_ci#define GO7007_BOARDID_XMEN_II 5 268c2ecf20Sopenharmony_ci#define GO7007_BOARDID_XMEN_III 6 278c2ecf20Sopenharmony_ci#define GO7007_BOARDID_MATRIX_REV 7 288c2ecf20Sopenharmony_ci#define GO7007_BOARDID_PX_M402U 8 298c2ecf20Sopenharmony_ci#define GO7007_BOARDID_PX_TV402U 9 308c2ecf20Sopenharmony_ci#define GO7007_BOARDID_LIFEVIEW_LR192 10 /* TV Walker Ultra */ 318c2ecf20Sopenharmony_ci#define GO7007_BOARDID_ENDURA 11 328c2ecf20Sopenharmony_ci#define GO7007_BOARDID_ADLINK_MPG24 12 338c2ecf20Sopenharmony_ci#define GO7007_BOARDID_SENSORAY_2250 13 /* Sensoray 2250/2251 */ 348c2ecf20Sopenharmony_ci#define GO7007_BOARDID_ADS_USBAV_709 14 358c2ecf20Sopenharmony_ci 368c2ecf20Sopenharmony_ci/* Various characteristics of each board */ 378c2ecf20Sopenharmony_ci#define GO7007_BOARD_HAS_AUDIO (1<<0) 388c2ecf20Sopenharmony_ci#define GO7007_BOARD_USE_ONBOARD_I2C (1<<1) 398c2ecf20Sopenharmony_ci#define GO7007_BOARD_HAS_TUNER (1<<2) 408c2ecf20Sopenharmony_ci 418c2ecf20Sopenharmony_ci/* Characteristics of sensor devices */ 428c2ecf20Sopenharmony_ci#define GO7007_SENSOR_VALID_POLAR (1<<0) 438c2ecf20Sopenharmony_ci#define GO7007_SENSOR_HREF_POLAR (1<<1) 448c2ecf20Sopenharmony_ci#define GO7007_SENSOR_VREF_POLAR (1<<2) 458c2ecf20Sopenharmony_ci#define GO7007_SENSOR_FIELD_ID_POLAR (1<<3) 468c2ecf20Sopenharmony_ci#define GO7007_SENSOR_BIT_WIDTH (1<<4) 478c2ecf20Sopenharmony_ci#define GO7007_SENSOR_VALID_ENABLE (1<<5) 488c2ecf20Sopenharmony_ci#define GO7007_SENSOR_656 (1<<6) 498c2ecf20Sopenharmony_ci#define GO7007_SENSOR_CONFIG_MASK 0x7f 508c2ecf20Sopenharmony_ci#define GO7007_SENSOR_TV (1<<7) 518c2ecf20Sopenharmony_ci#define GO7007_SENSOR_VBI (1<<8) 528c2ecf20Sopenharmony_ci#define GO7007_SENSOR_SCALING (1<<9) 538c2ecf20Sopenharmony_ci#define GO7007_SENSOR_SAA7115 (1<<10) 548c2ecf20Sopenharmony_ci 558c2ecf20Sopenharmony_ci/* Characteristics of audio sensor devices */ 568c2ecf20Sopenharmony_ci#define GO7007_AUDIO_I2S_MODE_1 (1) 578c2ecf20Sopenharmony_ci#define GO7007_AUDIO_I2S_MODE_2 (2) 588c2ecf20Sopenharmony_ci#define GO7007_AUDIO_I2S_MODE_3 (3) 598c2ecf20Sopenharmony_ci#define GO7007_AUDIO_BCLK_POLAR (1<<2) 608c2ecf20Sopenharmony_ci#define GO7007_AUDIO_WORD_14 (14<<4) 618c2ecf20Sopenharmony_ci#define GO7007_AUDIO_WORD_16 (16<<4) 628c2ecf20Sopenharmony_ci#define GO7007_AUDIO_ONE_CHANNEL (1<<11) 638c2ecf20Sopenharmony_ci#define GO7007_AUDIO_I2S_MASTER (1<<16) 648c2ecf20Sopenharmony_ci#define GO7007_AUDIO_OKI_MODE (1<<17) 658c2ecf20Sopenharmony_ci 668c2ecf20Sopenharmony_ci#define GO7007_CID_CUSTOM_BASE (V4L2_CID_DETECT_CLASS_BASE + 0x1000) 678c2ecf20Sopenharmony_ci#define V4L2_CID_PIXEL_THRESHOLD0 (GO7007_CID_CUSTOM_BASE+1) 688c2ecf20Sopenharmony_ci#define V4L2_CID_MOTION_THRESHOLD0 (GO7007_CID_CUSTOM_BASE+2) 698c2ecf20Sopenharmony_ci#define V4L2_CID_MB_THRESHOLD0 (GO7007_CID_CUSTOM_BASE+3) 708c2ecf20Sopenharmony_ci#define V4L2_CID_PIXEL_THRESHOLD1 (GO7007_CID_CUSTOM_BASE+4) 718c2ecf20Sopenharmony_ci#define V4L2_CID_MOTION_THRESHOLD1 (GO7007_CID_CUSTOM_BASE+5) 728c2ecf20Sopenharmony_ci#define V4L2_CID_MB_THRESHOLD1 (GO7007_CID_CUSTOM_BASE+6) 738c2ecf20Sopenharmony_ci#define V4L2_CID_PIXEL_THRESHOLD2 (GO7007_CID_CUSTOM_BASE+7) 748c2ecf20Sopenharmony_ci#define V4L2_CID_MOTION_THRESHOLD2 (GO7007_CID_CUSTOM_BASE+8) 758c2ecf20Sopenharmony_ci#define V4L2_CID_MB_THRESHOLD2 (GO7007_CID_CUSTOM_BASE+9) 768c2ecf20Sopenharmony_ci#define V4L2_CID_PIXEL_THRESHOLD3 (GO7007_CID_CUSTOM_BASE+10) 778c2ecf20Sopenharmony_ci#define V4L2_CID_MOTION_THRESHOLD3 (GO7007_CID_CUSTOM_BASE+11) 788c2ecf20Sopenharmony_ci#define V4L2_CID_MB_THRESHOLD3 (GO7007_CID_CUSTOM_BASE+12) 798c2ecf20Sopenharmony_ci 808c2ecf20Sopenharmony_cistruct go7007_board_info { 818c2ecf20Sopenharmony_ci unsigned int flags; 828c2ecf20Sopenharmony_ci int hpi_buffer_cap; 838c2ecf20Sopenharmony_ci unsigned int sensor_flags; 848c2ecf20Sopenharmony_ci int sensor_width; 858c2ecf20Sopenharmony_ci int sensor_height; 868c2ecf20Sopenharmony_ci int sensor_framerate; 878c2ecf20Sopenharmony_ci int sensor_h_offset; 888c2ecf20Sopenharmony_ci int sensor_v_offset; 898c2ecf20Sopenharmony_ci unsigned int audio_flags; 908c2ecf20Sopenharmony_ci int audio_rate; 918c2ecf20Sopenharmony_ci int audio_bclk_div; 928c2ecf20Sopenharmony_ci int audio_main_div; 938c2ecf20Sopenharmony_ci int num_i2c_devs; 948c2ecf20Sopenharmony_ci struct go_i2c { 958c2ecf20Sopenharmony_ci const char *type; 968c2ecf20Sopenharmony_ci unsigned int is_video:1; 978c2ecf20Sopenharmony_ci unsigned int is_audio:1; 988c2ecf20Sopenharmony_ci int addr; 998c2ecf20Sopenharmony_ci u32 flags; 1008c2ecf20Sopenharmony_ci } i2c_devs[5]; 1018c2ecf20Sopenharmony_ci int num_inputs; 1028c2ecf20Sopenharmony_ci struct { 1038c2ecf20Sopenharmony_ci int video_input; 1048c2ecf20Sopenharmony_ci int audio_index; 1058c2ecf20Sopenharmony_ci char *name; 1068c2ecf20Sopenharmony_ci } inputs[4]; 1078c2ecf20Sopenharmony_ci int video_config; 1088c2ecf20Sopenharmony_ci int num_aud_inputs; 1098c2ecf20Sopenharmony_ci struct { 1108c2ecf20Sopenharmony_ci int audio_input; 1118c2ecf20Sopenharmony_ci char *name; 1128c2ecf20Sopenharmony_ci } aud_inputs[3]; 1138c2ecf20Sopenharmony_ci}; 1148c2ecf20Sopenharmony_ci 1158c2ecf20Sopenharmony_cistruct go7007_hpi_ops { 1168c2ecf20Sopenharmony_ci int (*interface_reset)(struct go7007 *go); 1178c2ecf20Sopenharmony_ci int (*write_interrupt)(struct go7007 *go, int addr, int data); 1188c2ecf20Sopenharmony_ci int (*read_interrupt)(struct go7007 *go); 1198c2ecf20Sopenharmony_ci int (*stream_start)(struct go7007 *go); 1208c2ecf20Sopenharmony_ci int (*stream_stop)(struct go7007 *go); 1218c2ecf20Sopenharmony_ci int (*send_firmware)(struct go7007 *go, u8 *data, int len); 1228c2ecf20Sopenharmony_ci int (*send_command)(struct go7007 *go, unsigned int cmd, void *arg); 1238c2ecf20Sopenharmony_ci void (*release)(struct go7007 *go); 1248c2ecf20Sopenharmony_ci}; 1258c2ecf20Sopenharmony_ci 1268c2ecf20Sopenharmony_ci/* The video buffer size must be a multiple of PAGE_SIZE */ 1278c2ecf20Sopenharmony_ci#define GO7007_BUF_PAGES (128 * 1024 / PAGE_SIZE) 1288c2ecf20Sopenharmony_ci#define GO7007_BUF_SIZE (GO7007_BUF_PAGES << PAGE_SHIFT) 1298c2ecf20Sopenharmony_ci 1308c2ecf20Sopenharmony_cistruct go7007_buffer { 1318c2ecf20Sopenharmony_ci struct vb2_v4l2_buffer vb; 1328c2ecf20Sopenharmony_ci struct list_head list; 1338c2ecf20Sopenharmony_ci unsigned int frame_offset; 1348c2ecf20Sopenharmony_ci u32 modet_active; 1358c2ecf20Sopenharmony_ci}; 1368c2ecf20Sopenharmony_ci 1378c2ecf20Sopenharmony_ci#define GO7007_RATIO_1_1 0 1388c2ecf20Sopenharmony_ci#define GO7007_RATIO_4_3 1 1398c2ecf20Sopenharmony_ci#define GO7007_RATIO_16_9 2 1408c2ecf20Sopenharmony_ci 1418c2ecf20Sopenharmony_cienum go7007_parser_state { 1428c2ecf20Sopenharmony_ci STATE_DATA, 1438c2ecf20Sopenharmony_ci STATE_00, 1448c2ecf20Sopenharmony_ci STATE_00_00, 1458c2ecf20Sopenharmony_ci STATE_00_00_01, 1468c2ecf20Sopenharmony_ci STATE_FF, 1478c2ecf20Sopenharmony_ci STATE_VBI_LEN_A, 1488c2ecf20Sopenharmony_ci STATE_VBI_LEN_B, 1498c2ecf20Sopenharmony_ci STATE_MODET_MAP, 1508c2ecf20Sopenharmony_ci STATE_UNPARSED, 1518c2ecf20Sopenharmony_ci}; 1528c2ecf20Sopenharmony_ci 1538c2ecf20Sopenharmony_cistruct go7007 { 1548c2ecf20Sopenharmony_ci struct device *dev; 1558c2ecf20Sopenharmony_ci u8 bus_info[32]; 1568c2ecf20Sopenharmony_ci const struct go7007_board_info *board_info; 1578c2ecf20Sopenharmony_ci unsigned int board_id; 1588c2ecf20Sopenharmony_ci int tuner_type; 1598c2ecf20Sopenharmony_ci int channel_number; /* for multi-channel boards like Adlink PCI-MPG24 */ 1608c2ecf20Sopenharmony_ci char name[64]; 1618c2ecf20Sopenharmony_ci struct video_device vdev; 1628c2ecf20Sopenharmony_ci void *boot_fw; 1638c2ecf20Sopenharmony_ci unsigned boot_fw_len; 1648c2ecf20Sopenharmony_ci struct v4l2_device v4l2_dev; 1658c2ecf20Sopenharmony_ci struct v4l2_ctrl_handler hdl; 1668c2ecf20Sopenharmony_ci struct v4l2_ctrl *mpeg_video_encoding; 1678c2ecf20Sopenharmony_ci struct v4l2_ctrl *mpeg_video_gop_size; 1688c2ecf20Sopenharmony_ci struct v4l2_ctrl *mpeg_video_gop_closure; 1698c2ecf20Sopenharmony_ci struct v4l2_ctrl *mpeg_video_bitrate; 1708c2ecf20Sopenharmony_ci struct v4l2_ctrl *mpeg_video_aspect_ratio; 1718c2ecf20Sopenharmony_ci struct v4l2_ctrl *mpeg_video_b_frames; 1728c2ecf20Sopenharmony_ci struct v4l2_ctrl *mpeg_video_rep_seqheader; 1738c2ecf20Sopenharmony_ci struct v4l2_ctrl *modet_mode; 1748c2ecf20Sopenharmony_ci enum { STATUS_INIT, STATUS_ONLINE, STATUS_SHUTDOWN } status; 1758c2ecf20Sopenharmony_ci spinlock_t spinlock; 1768c2ecf20Sopenharmony_ci struct mutex hw_lock; 1778c2ecf20Sopenharmony_ci struct mutex serialize_lock; 1788c2ecf20Sopenharmony_ci int audio_enabled; 1798c2ecf20Sopenharmony_ci struct v4l2_subdev *sd_video; 1808c2ecf20Sopenharmony_ci struct v4l2_subdev *sd_audio; 1818c2ecf20Sopenharmony_ci u8 usb_buf[16]; 1828c2ecf20Sopenharmony_ci 1838c2ecf20Sopenharmony_ci /* Video input */ 1848c2ecf20Sopenharmony_ci int input; 1858c2ecf20Sopenharmony_ci int aud_input; 1868c2ecf20Sopenharmony_ci enum { GO7007_STD_NTSC, GO7007_STD_PAL, GO7007_STD_OTHER } standard; 1878c2ecf20Sopenharmony_ci v4l2_std_id std; 1888c2ecf20Sopenharmony_ci int sensor_framerate; 1898c2ecf20Sopenharmony_ci int width; 1908c2ecf20Sopenharmony_ci int height; 1918c2ecf20Sopenharmony_ci int encoder_h_offset; 1928c2ecf20Sopenharmony_ci int encoder_v_offset; 1938c2ecf20Sopenharmony_ci unsigned int encoder_h_halve:1; 1948c2ecf20Sopenharmony_ci unsigned int encoder_v_halve:1; 1958c2ecf20Sopenharmony_ci unsigned int encoder_subsample:1; 1968c2ecf20Sopenharmony_ci 1978c2ecf20Sopenharmony_ci /* Encoder config */ 1988c2ecf20Sopenharmony_ci u32 format; 1998c2ecf20Sopenharmony_ci int bitrate; 2008c2ecf20Sopenharmony_ci int fps_scale; 2018c2ecf20Sopenharmony_ci int pali; 2028c2ecf20Sopenharmony_ci int aspect_ratio; 2038c2ecf20Sopenharmony_ci int gop_size; 2048c2ecf20Sopenharmony_ci unsigned int ipb:1; 2058c2ecf20Sopenharmony_ci unsigned int closed_gop:1; 2068c2ecf20Sopenharmony_ci unsigned int repeat_seqhead:1; 2078c2ecf20Sopenharmony_ci unsigned int seq_header_enable:1; 2088c2ecf20Sopenharmony_ci unsigned int gop_header_enable:1; 2098c2ecf20Sopenharmony_ci unsigned int dvd_mode:1; 2108c2ecf20Sopenharmony_ci unsigned int interlace_coding:1; 2118c2ecf20Sopenharmony_ci 2128c2ecf20Sopenharmony_ci /* Motion detection */ 2138c2ecf20Sopenharmony_ci unsigned int modet_enable:1; 2148c2ecf20Sopenharmony_ci struct { 2158c2ecf20Sopenharmony_ci unsigned int enable:1; 2168c2ecf20Sopenharmony_ci int pixel_threshold; 2178c2ecf20Sopenharmony_ci int motion_threshold; 2188c2ecf20Sopenharmony_ci int mb_threshold; 2198c2ecf20Sopenharmony_ci } modet[4]; 2208c2ecf20Sopenharmony_ci unsigned char modet_map[1624]; 2218c2ecf20Sopenharmony_ci unsigned char active_map[216]; 2228c2ecf20Sopenharmony_ci u32 modet_event_status; 2238c2ecf20Sopenharmony_ci 2248c2ecf20Sopenharmony_ci /* Video streaming */ 2258c2ecf20Sopenharmony_ci struct mutex queue_lock; 2268c2ecf20Sopenharmony_ci struct vb2_queue vidq; 2278c2ecf20Sopenharmony_ci enum go7007_parser_state state; 2288c2ecf20Sopenharmony_ci int parse_length; 2298c2ecf20Sopenharmony_ci u16 modet_word; 2308c2ecf20Sopenharmony_ci int seen_frame; 2318c2ecf20Sopenharmony_ci u32 next_seq; 2328c2ecf20Sopenharmony_ci struct list_head vidq_active; 2338c2ecf20Sopenharmony_ci wait_queue_head_t frame_waitq; 2348c2ecf20Sopenharmony_ci struct go7007_buffer *active_buf; 2358c2ecf20Sopenharmony_ci 2368c2ecf20Sopenharmony_ci /* Audio streaming */ 2378c2ecf20Sopenharmony_ci void (*audio_deliver)(struct go7007 *go, u8 *buf, int length); 2388c2ecf20Sopenharmony_ci void *snd_context; 2398c2ecf20Sopenharmony_ci 2408c2ecf20Sopenharmony_ci /* I2C */ 2418c2ecf20Sopenharmony_ci int i2c_adapter_online; 2428c2ecf20Sopenharmony_ci struct i2c_adapter i2c_adapter; 2438c2ecf20Sopenharmony_ci 2448c2ecf20Sopenharmony_ci /* HPI driver */ 2458c2ecf20Sopenharmony_ci const struct go7007_hpi_ops *hpi_ops; 2468c2ecf20Sopenharmony_ci void *hpi_context; 2478c2ecf20Sopenharmony_ci int interrupt_available; 2488c2ecf20Sopenharmony_ci wait_queue_head_t interrupt_waitq; 2498c2ecf20Sopenharmony_ci unsigned short interrupt_value; 2508c2ecf20Sopenharmony_ci unsigned short interrupt_data; 2518c2ecf20Sopenharmony_ci}; 2528c2ecf20Sopenharmony_ci 2538c2ecf20Sopenharmony_cistatic inline struct go7007 *to_go7007(struct v4l2_device *v4l2_dev) 2548c2ecf20Sopenharmony_ci{ 2558c2ecf20Sopenharmony_ci return container_of(v4l2_dev, struct go7007, v4l2_dev); 2568c2ecf20Sopenharmony_ci} 2578c2ecf20Sopenharmony_ci 2588c2ecf20Sopenharmony_ci/* All of these must be called with the hpi_lock mutex held! */ 2598c2ecf20Sopenharmony_ci#define go7007_interface_reset(go) \ 2608c2ecf20Sopenharmony_ci ((go)->hpi_ops->interface_reset(go)) 2618c2ecf20Sopenharmony_ci#define go7007_write_interrupt(go, x, y) \ 2628c2ecf20Sopenharmony_ci ((go)->hpi_ops->write_interrupt)((go), (x), (y)) 2638c2ecf20Sopenharmony_ci#define go7007_stream_start(go) \ 2648c2ecf20Sopenharmony_ci ((go)->hpi_ops->stream_start(go)) 2658c2ecf20Sopenharmony_ci#define go7007_stream_stop(go) \ 2668c2ecf20Sopenharmony_ci ((go)->hpi_ops->stream_stop(go)) 2678c2ecf20Sopenharmony_ci#define go7007_send_firmware(go, x, y) \ 2688c2ecf20Sopenharmony_ci ((go)->hpi_ops->send_firmware)((go), (x), (y)) 2698c2ecf20Sopenharmony_ci#define go7007_write_addr(go, x, y) \ 2708c2ecf20Sopenharmony_ci ((go)->hpi_ops->write_interrupt)((go), (x)|0x8000, (y)) 2718c2ecf20Sopenharmony_ci 2728c2ecf20Sopenharmony_ci/* go7007-driver.c */ 2738c2ecf20Sopenharmony_ciint go7007_read_addr(struct go7007 *go, u16 addr, u16 *data); 2748c2ecf20Sopenharmony_ciint go7007_read_interrupt(struct go7007 *go, u16 *value, u16 *data); 2758c2ecf20Sopenharmony_ciint go7007_boot_encoder(struct go7007 *go, int init_i2c); 2768c2ecf20Sopenharmony_ciint go7007_reset_encoder(struct go7007 *go); 2778c2ecf20Sopenharmony_ciint go7007_register_encoder(struct go7007 *go, unsigned num_i2c_devs); 2788c2ecf20Sopenharmony_ciint go7007_start_encoder(struct go7007 *go); 2798c2ecf20Sopenharmony_civoid go7007_parse_video_stream(struct go7007 *go, u8 *buf, int length); 2808c2ecf20Sopenharmony_cistruct go7007 *go7007_alloc(const struct go7007_board_info *board, 2818c2ecf20Sopenharmony_ci struct device *dev); 2828c2ecf20Sopenharmony_civoid go7007_update_board(struct go7007 *go); 2838c2ecf20Sopenharmony_ci 2848c2ecf20Sopenharmony_ci/* go7007-fw.c */ 2858c2ecf20Sopenharmony_ciint go7007_construct_fw_image(struct go7007 *go, u8 **fw, int *fwlen); 2868c2ecf20Sopenharmony_ci 2878c2ecf20Sopenharmony_ci/* go7007-i2c.c */ 2888c2ecf20Sopenharmony_ciint go7007_i2c_init(struct go7007 *go); 2898c2ecf20Sopenharmony_ciint go7007_i2c_remove(struct go7007 *go); 2908c2ecf20Sopenharmony_ci 2918c2ecf20Sopenharmony_ci/* go7007-v4l2.c */ 2928c2ecf20Sopenharmony_ciint go7007_v4l2_init(struct go7007 *go); 2938c2ecf20Sopenharmony_ciint go7007_v4l2_ctrl_init(struct go7007 *go); 2948c2ecf20Sopenharmony_civoid go7007_v4l2_remove(struct go7007 *go); 2958c2ecf20Sopenharmony_ci 2968c2ecf20Sopenharmony_ci/* snd-go7007.c */ 2978c2ecf20Sopenharmony_ciint go7007_snd_init(struct go7007 *go); 2988c2ecf20Sopenharmony_ciint go7007_snd_remove(struct go7007 *go); 299