rk3588 采集mipi接口yuv422格式的图像

// SPDX-License-Identifier: GPL-2.0
/*
 * SAOHAIDENG CMOS Image Sensor driver
 *
 *
 * Copyright (C) 2017 Fuzhou Rockchip Electronics Co., Ltd.
 *
 * V0.0X01.0X01 add poweron function.
 * V0.0X01.0X02 fix mclk issue when probe multiple camera.
 * V0.0X01.0X03 fix saoHaiDeng exposure issues.
 * V0.0X01.0X04 add enum_frame_interval function.
 * V0.0X01.0X05 reduce rkisp1: CIF_ISP_PIC_SIZE_ERROR 0x00000001.
 * V0.0X01.0X06 add quick stream on/off
 */

#include <linux/clk.h>
#include <linux/delay.h>
#include <linux/err.h>
#include <linux/gpio/consumer.h>
#include <linux/i2c.h>
#include <linux/init.h>
#include <linux/interrupt.h>
#include <linux/io.h>
#include <linux/kernel.h>
#include <linux/media.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/of_graph.h>
#include <linux/regulator/consumer.h>
#include <linux/rk-camera-module.h>
#include <linux/slab.h>
#include <linux/uaccess.h>
#include <linux/version.h>
#include <linux/videodev2.h>
#include <media/media-entity.h>
#include <media/v4l2-common.h>
#include <media/v4l2-ctrls.h>
#include <media/v4l2-device.h>
#include <media/v4l2-event.h>
#include <media/v4l2-fwnode.h>
#include <media/v4l2-image-sizes.h>
#include <media/v4l2-mediabus.h>
#include <media/v4l2-subdev.h>

#define DRIVER_VERSION KERNEL_VERSION(0, 0x01, 0x6)
#define DRIVER_NAME "saoHaiDeng"
#define SAOHAIDENG_PIXEL_RATE (120 * 1000 * 1000)

#define SAOHAIDENG_EXPOSURE_CONTROL

/*
 * SAOHAIDENG register definitions
 */
#define REG_SC_CHIP_ID_H 0xf0
#define REG_SC_CHIP_ID_L 0xf1

#define REG_NULL 0xFFFF /* Array end token */

#define SENSOR_ID(_msb, _lsb) ((_msb) << 8 | (_lsb))
#define SAOHAIDENG_ID 0x2145

struct sensor_register {
	u16 addr;
	u8 value;
};

struct saoHaiDeng_framesize {
	u16 width;
	u16 height;
	struct v4l2_fract max_fps;
	u16 max_exp_lines;
	const struct sensor_register *regs;
};

struct saoHaiDeng_pll_ctrl {
	u8 ctrl1;
	u8 ctrl2;
	u8 ctrl3;
};

struct saoHaiDeng_pixfmt {
	u32 code;
	/* Output format Register Value (REG_FORMAT_CTRL00) */
	struct sensor_register *format_ctrl_regs;
};

struct pll_ctrl_reg {
	unsigned int div;
	unsigned char reg;
};

static const char *const saoHaiDeng_supply_names[] = {
	"dovdd", /* Digital I/O power */
	"avdd", /* Analog power */
	"dvdd", /* Digital core power */
};

#define SAOHAIDENG_NUM_SUPPLIES ARRAY_SIZE(saoHaiDeng_supply_names)

struct saoHaiDeng {
	struct v4l2_subdev sd;
	struct media_pad pad;
	struct v4l2_mbus_framefmt format;
	unsigned int fps;
	unsigned int xvclk_frequency;
	struct clk *xvclk;
	struct gpio_desc *power_gpio;
	struct gpio_desc *pwdn_gpio;
	struct gpio_desc *reset_gpio;
	struct regulator_bulk_data supplies[SAOHAIDENG_NUM_SUPPLIES];
	struct mutex lock;
	struct i2c_client *client;
	struct v4l2_ctrl_handler ctrls;
	struct v4l2_ctrl *link_frequency;
	struct v4l2_fwnode_endpoint bus_cfg;
	const struct saoHaiDeng_framesize *frame_size;
	const struct saoHaiDeng_framesize *framesize_cfg;
	unsigned int cfg_num;
	int streaming;
	bool power_on;
	u32 module_index;
	const char *module_facing;
	const char *module_name;
	const char *len_name;
};

static const struct sensor_register saoHaiDeng_dvp_init_regs[] = {
	{ 0xfe, 0xf0 },
	{ 0xfe, 0xf0 },
	{ 0xfe, 0xf0 },
	{ 0xfc, 0x06 },
	{ 0xf6, 0x00 },
	{ 0xf7, 0x1d },
	{ 0xf8, 0x84 },
	{ 0xfa, 0x00 },
	{ 0xf9, 0xfe },
	{ 0xf2, 0x00 },
	/*ISP reg*/
	{ 0xfe, 0x00 },
	{ 0x03, 0x04 },
	{ 0x04, 0xe2 },
	{ 0x09, 0x00 },
	{ 0x0a, 0x00 },
	{ 0x0b, 0x00 },
	{ 0x0c, 0x00 },
	{ 0x0d, 0x04 },
	{ 0x0e, 0xc0 },
	{ 0x0f, 0x06 },
	{ 0x10, 0x52 },
	{ 0x12, 0x2e },
	{ 0x17, 0x14 },
	{ 0x18, 0x22 },
	{ 0x19, 0x0e },
	{ 0x1a, 0x01 },
	{ 0x1b, 0x4b },
	{ 0x1c, 0x07 },
	{ 0x1d, 0x10 },
	{ 0x1e, 0x88 },
	{ 0x1f, 0x78 },
	{ 0x20, 0x03 },
	{ 0x21, 0x40 },
	{ 0x22, 0xa0 },
	{ 0x24, 0x3f },
	{ 0x25, 0x01 },
	{ 0x26, 0x10 },
	{ 0x2d, 0x60 },
	{ 0x30, 0x01 },
	{ 0x31, 0x90 },
	{ 0x33, 0x06 },
	{ 0x34, 0x01 },
	{ 0xfe, 0x00 },
	{ 0x80, 0x7f },
	{ 0x81, 0x26 },
	{ 0x82, 0xfa },
	{ 0x83, 0x00 },
	{ 0x84, 0x00 },
	{ 0x86, 0x02 },
	{ 0x88, 0x03 },
	{ 0x89, 0x03 },
	{ 0x85, 0x08 },
	{ 0x8a, 0x00 },
	{ 0x8b, 0x00 },
	{ 0xb0, 0x55 },
	{ 0xc3, 0x00 },
	{ 0xc4, 0x80 },
	{ 0xc5, 0x90 },
	{ 0xc6, 0x3b },
	{ 0xc7, 0x46 },
	{ 0xec, 0x06 },
	{ 0xed, 0x04 },
	{ 0xee, 0x60 },
	{ 0xef, 0x90 },
	{ 0xb6, 0x01 },
	{ 0x90, 0x01 },
	{ 0x91, 0x00 },
	{ 0x92, 0x00 },
	{ 0x93, 0x00 },
	{ 0x94, 0x00 },
	{ 0x95, 0x04 },
	{ 0x96, 0xb0 },
	{ 0x97, 0x06 },
	{ 0x98, 0x40 },
	/*BLK*/
	{ 0xfe, 0x00 },
	{ 0x40, 0x42 },
	{ 0x41, 0x00 },
	{ 0x43, 0x5b },
	{ 0x5e, 0x00 },
	{ 0x5f, 0x00 },
	{ 0x60, 0x00 },
	{ 0x61, 0x00 },
	{ 0x62, 0x00 },
	{ 0x63, 0x00 },
	{ 0x64, 0x00 },
	{ 0x65, 0x00 },
	{ 0x66, 0x20 },
	{ 0x67, 0x20 },
	{ 0x68, 0x20 },
	{ 0x69, 0x20 },
	{ 0x76, 0x00 },
	{ 0x6a, 0x08 },
	{ 0x6b, 0x08 },
	{ 0x6c, 0x08 },
	{ 0x6d, 0x08 },
	{ 0x6e, 0x08 },
	{ 0x6f, 0x08 },
	{ 0x70, 0x08 },
	{ 0x71, 0x08 },
	{ 0x76, 0x00 },
	{ 0x72, 0xf0 },
	{ 0x7e, 0x3c },
	{ 0x7f, 0x00 },
	{ 0xfe, 0x02 },
	{ 0x48, 0x15 },
	{ 0x49, 0x00 },
	{ 0x4b, 0x0b },
	{ 0xfe, 0x00 },
	/*AEC*/
	{ 0xfe, 0x01 },
	{ 0x01, 0x04 },
	{ 0x02, 0xc0 },
	{ 0x03, 0x04 },
	{ 0x04, 0x90 },
	{ 0x05, 0x30 },
	{ 0x06, 0x90 },
	{ 0x07, 0x30 },
	{ 0x08, 0x80 },
	{ 0x09, 0x00 },
	{ 0x0a, 0x82 },
	{ 0x0b, 0x11 },
	{ 0x0c, 0x10 },
	{ 0x11, 0x10 },
	{ 0x13, 0x7b },
	{ 0x17, 0x00 },
	{ 0x1c, 0x11 },
	{ 0x1e, 0x61 },
	{ 0x1f, 0x35 },
	{ 0x20, 0x40 },
	{ 0x22, 0x40 },
	{ 0x23, 0x20 },
	{ 0xfe, 0x02 },
	{ 0x0f, 0x04 },
	{ 0xfe, 0x01 },
	{ 0x12, 0x35 },
	{ 0x15, 0xb0 },
	{ 0x10, 0x31 },
	{ 0x3e, 0x28 },
	{ 0x3f, 0xb0 },
	{ 0x40, 0x90 },
	{ 0x41, 0x0f },

	/*INTPEE*/
	{ 0xfe, 0x02 },
	{ 0x90, 0x6c },
	{ 0x91, 0x03 },
	{ 0x92, 0xcb },
	{ 0x94, 0x33 },
	{ 0x95, 0x84 },
	{ 0x97, 0x45 },
	{ 0xa2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16, 0x42 },
	{ 0x17, 0x4e },
	{ 0x18, 0x63 },
	{ 0x19, 0x76 },
	{ 0x1a, 0x87 },
	{ 0x1b, 0x96 },
	{ 0x1c, 0xa2 },
	{ 0x1d, 0xb8 },
	{ 0x1e, 0xcb },
	{ 0x1f, 0xd8 },
	{ 0x20, 0xe2 },
	{ 0x21, 0xe9 },
	{ 0x22, 0xf0 },
	{ 0x23, 0xf8 },
	{ 0x24, 0xfd },
	{ 0x25, 0xff },
	{ 0xfe, 0x00 },
#endif
	{ 0xfe, 0x00 },
	{ 0xc6, 0x20 },
	{ 0xc7, 0x2b },
/*gamma2*/
#if 1
	{ 0xfe, 0x02 },
	{ 0x26, 0x0f },
	{ 0x27, 0x14 },
	{ 0x28, 0x19 },
	{ 0x29, 0x1e },
	{ 0x2a, 0x27 },
	{ 0x2b, 0x33 },
	{ 0x2c, 0x3b },
	{ 0x2d, 0x45 },
	{ 0x2e, 0x59 },
	{ 0x2f, 0x69 },
	{ 0x30, 0x7c },
	{ 0x31, 0x89 },
	{ 0x32, 0x98 },
	{ 0x33, 0xae },
	{ 0x34, 0xc0 },
	{ 0x35, 0xcf },
	{ 0x36, 0xda },
	{ 0x37, 0xe2 },
	{ 0x38, 0xe9 },
	{ 0x39, 0xf3 },
	{ 0x3a, 0xf9 },
	{ 0x3b, 0xff },
#else
	/*Gamma outdoor*/
	{ 0xfe, 0x02 },
	{ 0x26, 0x17 },
	{ 0x27, 0x18 },
	{ 0x28, 0x1c },
	{ 0x29, 0x20 },
	{ 0x2a, 0x28 },
	{ 0x2b, 0x34 },
	{ 0x2c, 0x40 },
	{ 0x2d, 0x49 },
	{ 0x2e, 0x5b },
	{ 0x2f, 0x6d },
	{ 0x30, 0x7d },
	{ 0x31, 0x89 },
	{ 0x32, 0x97 },
	{ 0x33, 0xac },
	{ 0x34, 0xc0 },
	{ 0x35, 0xcf },
	{ 0x36, 0xda },
	{ 0x37, 0xe5 },
	{ 0x38, 0xec },
	{ 0x39, 0xf8 },
	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 0x00 },
	{ 0x94, 0x00 },
	{ 0x95, 0x02 },
	{ 0x96, 0x58 },
	{ 0x97, 0x03 },
	{ 0x98, 0x20 },
	{ 0x99, 0x11 },
	{ 0x9a, 0x06 },
	/*AWB*/
	{ 0xfe, 0x00 },
	{ 0xec, 0x02 },
	{ 0xed, 0x02 },
	{ 0xee, 0x30 },
	{ 0xef, 0x48 },
	{ 0xfe, 0x02 },
	{ 0x9d, 0x08 },
	{ 0xfe, 0x01 },
	{ 0x74, 0x00 },
	/*AEC*/
	{ 0xfe, 0x01 },
	{ 0x01, 0x04 },
	{ 0x02, 0x60 },
	{ 0x03, 0x02 },
	{ 0x04, 0x48 },
	{ 0x05, 0x18 },
	{ 0x06, 0x50 },
	{ 0x07, 0x10 },
	{ 0x08, 0x38 },
	{ 0x0a, 0x80 },
	{ 0x21, 0x04 },
	{ 0xfe, 0x00 },
	{ 0x20, 0x03 },
	{ 0xfe, 0x00 },
	{ REG_NULL, 0x00 },
};

/* Senor full resolution setting */
static const struct sensor_register saoHaiDeng_dvp_full[] = {
	// SENSORDB("SAOHAIDENG_Sensor_2M"},
	{ 0xfe, 0x00 },
	{ 0x05, 0x02 },
	{ 0x06, 0x20 },
	{ 0x07, 0x00 },
	{ 0x08, 0x50 },
	{ 0xfe, 0x01 },
	{ 0x25, 0x00 },
	{ 0x26, 0xfa },

	{ 0x27, 0x04 },
	{ 0x28, 0xe2 },
	{ 0x29, 0x04 },
	{ 0x2a, 0xe2 },
	{ 0x2b, 0x04 },
	{ 0x2c, 0xe2 },
	{ 0x2d, 0x04 },
	{ 0x2e, 0xe2 },
	{ 0xfe, 0x00 },
	{ 0xfd, 0x00 },
	{ 0xfa, 0x11 },
	{ 0x18, 0x22 },
	/*crop window*/
	{ 0xfe, 0x00 },
	{ 0x90, 0x01 },
	{ 0x91, 0x00 },
	{ 0x92, 0x00 },
	{ 0x93, 0x00 },
	{ 0x94, 0x00 },
	{ 0x95, 0x04 },
	{ 0x96, 0xb0 },
	{ 0x97, 0x06 },
	{ 0x98, 0x40 },
	{ 0x99, 0x11 },
	{ 0x9a, 0x06 },
	/*AWB*/
	{ 0xfe, 0x00 },
	{ 0xec, 0x06 },
	{ 0xed, 0x04 },
	{ 0xee, 0x60 },
	{ 0xef, 0x90 },
	{ 0xfe, 0x01 },
	{ 0x74, 0x01 },
	/*AEC*/
	{ 0xfe, 0x01 },
	{ 0x01, 0x04 },
	{ 0x02, 0xc0 },
	{ 0x03, 0x04 },
	{ 0x04, 0x90 },
	{ 0x05, 0x30 },
	{ 0x06, 0x90 },
	{ 0x07, 0x30 },
	{ 0x08, 0x80 },
	{ 0x0a, 0x82 },
	{ 0xfe, 0x01 },
	{ 0x21, 0x15 }, // if 0xfa=11,then 0x21=15;
	{ 0xfe, 0x00 }, // else if 0xfa=00,then 0x21=04
	{ 0x20, 0x15 },
	{ 0xfe, 0x00 },

	{ REG_NULL, 0x00 },
};

/* Preview resolution setting*/
static const struct sensor_register saoHaiDeng_dvp_svga_30fps[] = {
	/*frame rate 50Hz*/
	{ 0xfe, 0x00 },
	{ 0x05, 0x02 },
	{ 0x06, 0x20 },
	{ 0x07, 0x00 },
	{ 0x08, 0xb8 },
	{ 0xfe, 0x01 },
	{ 0x13, 0x7b },
	{ 0x18, 0x95 },
	{ 0x25, 0x01 },
	{ 0x26, 0xac },

	{ 0x27, 0x05 },
	{ 0x28, 0x04 },
	{ 0x29, 0x05 },
	{ 0x2a, 0x04 },
	{ 0x2b, 0x05 },
	{ 0x2c, 0x04 },
	{ 0x2d, 0x05 },
	{ 0x2e, 0x04 },

	{ 0x3c, 0x00 },
	{ 0x3d, 0x15 },
	{ 0xfe, 0x00 },
	{ 0xfe, 0x00 },
	{ 0xfa, 0x00 },
	{ 0x18, 0x42 },
	{ 0xfd, 0x03 },
	{ 0xb6, 0x01 },
	/* crop window */
	{ 0xfe, 0x00 },
	{ 0x99, 0x11 },
	{ 0x9a, 0x06 },
	{ 0x9b, 0x00 },
	{ 0x9c, 0x00 },
	{ 0x9d, 0x00 },
	{ 0x9e, 0x00 },
	{ 0x9f, 0x00 },
	{ 0xa0, 0x00 },
	{ 0xa1, 0x00 },
	{ 0xa2, 0x00 },
	{ 0x90, 0x01 },
	{ 0x91, 0x00 },
	{ 0x92, 0x00 },
	{ 0x93, 0x00 },
	{ 0x94, 0x00 },
	{ 0x95, 0x02 },
	{ 0x96, 0x58 },
	{ 0x97, 0x03 },
	{ 0x98, 0x20 },
	/* AWB */
	{ 0xfe, 0x00 },
	{ 0xec, 0x01 },
	{ 0xed, 0x02 },
	{ 0xee, 0x30 },
	{ 0xef, 0x48 },
	{ 0xfe, 0x01 },
	{ 0x74, 0x00 },
	/* AEC */
	{ 0xfe, 0x01 },
	{ 0x01, 0x04 },
	{ 0x02, 0x60 },
	{ 0x03, 0x02 },
	{ 0x04, 0x48 },
	{ 0x05, 0x18 },
	{ 0x06, 0x4c },
	{ 0x07, 0x14 },
	{ 0x08, 0x36 },
	{ 0x0a, 0xc0 },
	{ 0x21, 0x14 },
	{ 0xfe, 0x00 },
	{ REG_NULL, 0x00 },
};

/* Preview resolution setting*/
static const struct sensor_register saoHaiDeng_dvp_svga_20fps[] = {
	// SENSORDB("SAOHAIDENG_Sensor_SVGA"},
	{ 0xfe, 0x00 },
	{ 0x05, 0x02 },
	{ 0x06, 0x20 },
	{ 0x07, 0x00 },
	{ 0x08, 0x50 },
	{ 0xfe, 0x01 },
	{ 0x25, 0x00 },
	{ 0x26, 0xfa },

	{ 0x27, 0x04 },
	{ 0x28, 0xe2 },
	{ 0x29, 0x04 },
	{ 0x2a, 0xe2 },
	{ 0x2b, 0x04 },
	{ 0x2c, 0xe2 },
	{ 0x2d, 0x04 },
	{ 0x2e, 0xe2 },

	{ 0xfe, 0x00 },
	{ 0xb6, 0x01 },
	{ 0xfd, 0x01 },
	{ 0xfa, 0x00 },
	{ 0x18, 0x22 },
	/*crop window*/
	{ 0xfe, 0x00 },
	{ 0x90, 0x01 },
	{ 0x91, 0x00 },
	{ 0x92, 0x00 },
	{ 0x93, 0x00 },
	{ 0x94, 0x00 },
	{ 0x95, 0x02 },
	{ 0x96, 0x58 },
	{ 0x97, 0x03 },
	{ 0x98, 0x20 },
	{ 0x99, 0x11 },
	{ 0x9a, 0x06 },
	/*AWB*/
	{ 0xfe, 0x00 },
	{ 0xec, 0x02 },
	{ 0xed, 0x02 },
	{ 0xee, 0x30 },
	{ 0xef, 0x48 },
	{ 0xfe, 0x02 },
	{ 0x9d, 0x08 },
	{ 0xfe, 0x01 },
	{ 0x74, 0x00 },
	/*AEC*/
	{ 0xfe, 0x01 },
	{ 0x01, 0x04 },
	{ 0x02, 0x60 },
	{ 0x03, 0x02 },
	{ 0x04, 0x48 },
	{ 0x05, 0x18 },
	{ 0x06, 0x50 },
	{ 0x07, 0x10 },
	{ 0x08, 0x38 },
	{ 0x0a, 0x80 },
	{ 0x21, 0x04 },
	{ 0xfe, 0x00 },
	{ 0x20, 0x03 },
	{ 0xfe, 0x00 },
	{ REG_NULL, 0x00 },
};

static const struct sensor_register saoHaiDeng_mipi_init_regs[] = {
	{ 0xfe, 0xf0 },
	{ 0xfe, 0xf0 },
	{ 0xfe, 0xf0 },
	{ 0xfc, 0x06 },
	{ 0xf6, 0x00 },
	{ 0xf7, 0x1d },
	{ 0xf8, 0x84 },
	{ 0xfa, 0x00 },
	{ 0xf9, 0x8e },
	{ 0xf2, 0x00 },
	/*ISP reg*/
	{ 0xfe, 0x00 },
	{ 0x03, 0x04 },
	{ 0x04, 0xe2 },
	{ 0x09, 0x00 },
	{ 0x0a, 0x00 },
	{ 0x0b, 0x00 },
	{ 0x0c, 0x00 },
	{ 0x0d, 0x04 },
	{ 0x0e, 0xc0 },
	{ 0x0f, 0x06 },
	{ 0x10, 0x52 },
	{ 0x12, 0x2e },
	{ 0x17, 0x14 },
	{ 0x18, 0x22 },
	{ 0x19, 0x0e },
	{ 0x1a, 0x01 },
	{ 0x1b, 0x4b },
	{ 0x1c, 0x07 },
	{ 0x1d, 0x10 },
	{ 0x1e, 0x88 },
	{ 0x1f, 0x78 },
	{ 0x20, 0x03 },
	{ 0x21, 0x40 },
	{ 0x22, 0xa0 },
	{ 0x24, 0x16 },
	{ 0x25, 0x01 },
	{ 0x26, 0x10 },
	{ 0x2d, 0x60 },
	{ 0x30, 0x01 },
	{ 0x31, 0x90 },
	{ 0x33, 0x06 },
	{ 0x34, 0x01 },
	{ 0xfe, 0x00 },
	{ 0x80, 0x7f },
	{ 0x81, 0x26 },
	{ 0x82, 0xfa },
	{ 0x83, 0x00 },
	{ 0x84, 0x00 },
	{ 0x86, 0x02 },
	{ 0x88, 0x03 },
	{ 0x89, 0x03 },
	{ 0x85, 0x08 },
	{ 0x8a, 0x00 },
	{ 0x8b, 0x00 },
	{ 0xb0, 0x55 },
	{ 0xc3, 0x00 },
	{ 0xc4, 0x80 },
	{ 0xc5, 0x90 },
	{ 0xc6, 0x3b },
	{ 0xc7, 0x46 },
	{ 0xec, 0x06 },
	{ 0xed, 0x04 },
	{ 0xee, 0x60 },
	{ 0xef, 0x90 },
	{ 0xb6, 0x01 },
	{ 0x90, 0x01 },
	{ 0x91, 0x00 },
	{ 0x92, 0x00 },
	{ 0x93, 0x00 },
	{ 0x94, 0x00 },
	{ 0x95, 0x04 },
	{ 0x96, 0xb0 },
	{ 0x97, 0x06 },
	{ 0x98, 0x40 },
	/*BLK*/
	{ 0xfe, 0x00 },
	{ 0x40, 0x42 },
	{ 0x41, 0x00 },
	{ 0x43, 0x5b },
	{ 0x5e, 0x00 },
	{ 0x5f, 0x00 },
	{ 0x60, 0x00 },
	{ 0x61, 0x00 },
	{ 0x62, 0x00 },
	{ 0x63, 0x00 },
	{ 0x64, 0x00 },
	{ 0x65, 0x00 },
	{ 0x66, 0x20 },
	{ 0x67, 0x20 },
	{ 0x68, 0x20 },
	{ 0x69, 0x20 },
	{ 0x76, 0x00 },
	{ 0x6a, 0x08 },
	{ 0x6b, 0x08 },
	{ 0x6c, 0x08 },
	{ 0x6d, 0x08 },
	{ 0x6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 },
	{ 0x06, 0x88 },
	{ 0x01, 0x83 },
	{ 0x10, 0x84 },
	{ 0x11, 0x1e },
	{ 0x12, 0x80 },
	{ 0x13, 0x0c },
	{ 0x15, 0x10 },
	{ 0x17, 0xf0 },
	{ 0x21, 0x10 },
	{ 0x22, 0x04 },
	{ 0x23, 0x10 },
	{ 0x24, 0x10 },
	{ 0x25, 0x10 },
	{ 0x26, 0x05 },
	{ 0x29, 0x03 },
	{ 0x2a, 0x0a },
	{ 0x2b, 0x06 },
	{ 0xfe, 0x00 },
	{ REG_NULL, 0x00 },
};

static const struct sensor_register saoHaiDeng_mipi_full[] = {
	/*SENSORDB("SAOHAIDENG_Sensor_2M")*/
	{ 0xfe, 0x00 },
	{ 0x05, 0x02 },
	{ 0x06, 0x20 },
	{ 0x07, 0x00 },
	{ 0x08, 0x50 },
	{ 0xfe, 0x01 },
	{ 0x25, 0x00 },
	{ 0x26, 0xfa },

	{ 0x27, 0x04 },
	{ 0x28, 0xe2 },
	{ 0x29, 0x04 },
	{ 0x2a, 0xe2 },
	{ 0x2b, 0x04 },
	{ 0x2c, 0xe2 },
	{ 0x2d, 0x04 },
	{ 0x2e, 0xe2 },
	{ 0xfe, 0x00 },
	{ 0xfd, 0x00 },
	{ 0xfa, 0x11 },
	{ 0x18, 0x22 },
	/*crop window*/
	{ 0xfe, 0x00 },
	{ 0x90, 0x01 },
	{ 0x91, 0x00 },
	{ 0x92, 0x00 },
	{ 0x93, 0x00 },
	{ 0x94, 0x00 },
	{ 0x95, 0x04 },
	{ 0x96, 0xb0 },
	{ 0x97, 0x06 },
	{ 0x98, 0x40 },
	{ 0x99, 0x11 },
	{ 0x9a, 0x06 },
	/*AWB*/
	{ 0xfe, 0x00 },
	{ 0xec, 0x06 },
	{ 0xed, 0x04 },
	{ 0xee, 0x60 },
	{ 0xef, 0x90 },
	{ 0xfe, 0x01 },
	{ 0x74, 0x01 },

	{ 0xfe, 0x00 },
	{ 0x7e, 0x3c },
	{ 0x7f, 0x00 },
	{ 0xfe, 0x01 },
	{ 0xa0, 0x03 },

	/*AEC*/
	{ 0xfe, 0x01 },
	{ 0x01, 0x04 },
	{ 0x02, 0xc0 },
	{ 0x03, 0x04 },
	{ 0x04, 0x90 },
	{ 0x05, 0x30 },
	{ 0x06, 0x90 },
	{ 0x07, 0x30 },
	{ 0x08, 0x80 },
	{ 0x0a, 0x82 },
	{ 0x1b, 0x01 },
	{ 0xfe, 0x00 },
	{ 0xfe, 0x01 },
	{ 0x21, 0x15 },
	{ 0xfe, 0x00 },
	{ 0x20, 0x15 },
	{ 0xfe, 0x03 },
	{ 0x12, 0x80 },
	{ 0x13, 0x0c },
	{ 0x04, 0x01 },
	{ 0x05, 0x00 },
	{ 0xfe, 0x00 },
	{ REG_NULL, 0x00 },
};

static const struct sensor_register saoHaiDeng_mipi_svga_20fps[] = {
	/*frame rate 50Hz*/
	{ 0xfe, 0x00 },
	{ 0x05, 0x02 },
	{ 0x06, 0x20 },
	{ 0x07, 0x00 },
	{ 0x08, 0x50 },
	{ 0xfe, 0x01 },
	{ 0x25, 0x00 },
	{ 0x26, 0xfa },

	{ 0x27, 0x04 },
	{ 0x28, 0xe2 },
	{ 0x29, 0x04 },
	{ 0x2a, 0xe2 },
	{ 0x2b, 0x04 },
	{ 0x2c, 0xe2 },
	{ 0x2d, 0x04 },
	{ 0x2e, 0xe2 },

	{ 0xfe, 0x00 },
	{ 0xb6, 0x01 },
	{ 0xfd, 0x01 },
	{ 0xfa, 0x00 },
	{ 0x18, 0x22 },
	/*crop window*/
	{ 0xfe, 0x00 },
	{ 0x90, 0x01 },
	{ 0x91, 0x00 },
	{ 0x92, 0x00 },
	{ 0x93, 0x00 },
	{ 0x94, 0x00 },
	{ 0x95, 0x02 },
	{ 0x96, 0x58 },
	{ 0x97, 0x03 },
	{ 0x98, 0x20 },
	{ 0x99, 0x11 },
	{ 0x9a, 0x06 },
	/*AWB*/
	{ 0xfe, 0x00 },
	{ 0xec, 0x02 },
	{ 0xed, 0x02 },
	{ 0xee, 0x30 },
	{ 0xef, 0x48 },
	{ 0xfe, 0x02 },
	{ 0x9d, 0x08 },
	{ 0xfe, 0x01 },
	{ 0x74, 0x00 },
	{ 0xfe, 0x00 },
	{ 0x7e, 0x3c },
	{ 0x7f, 0x00 },
	{ 0xfe, 0x01 },
	{ 0xa0, 0x03 },

	/*AEC*/
	{ 0xfe, 0x01 },
	{ 0x01, 0x04 },
	{ 0x02, 0x60 },
	{ 0x03, 0x02 },
	{ 0x04, 0x48 },
	{ 0x05, 0x18 },
	{ 0x06, 0x50 },
	{ 0x07, 0x10 },
	{ 0x08, 0x38 },
	{ 0x0a, 0x80 },
	//{0x1b, 0x04},
	{ 0x21, 0x04 },
	{ 0xfe, 0x00 },
	{ 0x20, 0x03 },
	{ 0xfe, 0x03 },
	{ 0x12, 0x40 },
	{ 0x13, 0x06 },
	{ 0x04, 0x01 },
	{ 0x05, 0x00 },
	{ 0xfe, 0x00 },
	{ REG_NULL, 0x00 },
};

static const struct sensor_register saoHaiDeng_mipi_svga_30fps[] = {
	/*frame rate 50Hz*/
	{ 0xfe, 0x00 },
	{ 0x05, 0x02 },
	{ 0x06, 0x20 },
	{ 0x07, 0x00 },
	{ 0x08, 0xb8 },
	{ 0xfe, 0x01 },
	{ 0x25, 0x01 },
	{ 0x26, 0xac },

	{ 0x27, 0x05 }, // 4e2 pad
	{ 0x28, 0x04 },
	{ 0x29, 0x05 }, // 6d6 pad
	{ 0x2a, 0x04 },
	{ 0x2b, 0x05 }, // 7d0 pad
	{ 0x2c, 0x04 },
	{ 0x2d, 0x05 },
	{ 0x2e, 0x04 },
	{ 0xfe, 0x00 },

	{ 0xfe, 0x00 },
	{ 0xfd, 0x01 },
	{ 0xfa, 0x00 },
	{ 0x18, 0x62 },
	{ 0xfd, 0x03 },
	/*crop window*/
	{ 0xfe, 0x00 },
	{ 0x90, 0x01 },
	{ 0x91, 0x00 },
	{ 0x92, 0x00 },
	{ 0x93, 0x00 },
	{ 0x94, 0x00 },
	{ 0x95, 0x02 },
	{ 0x96, 0x58 },
	{ 0x97, 0x03 },
	{ 0x98, 0x20 },
	{ 0x99, 0x11 },
	{ 0x9a, 0x06 },
	/*AWB*/
	{ 0xfe, 0x00 },
	{ 0xec, 0x02 },
	{ 0xed, 0x02 },
	{ 0xee, 0x30 },
	{ 0xef, 0x48 },
	{ 0xfe, 0x02 },
	{ 0x9d, 0x08 },
	{ 0xfe, 0x01 },
	{ 0x74, 0x00 },

	{ 0xfe, 0x00 },
	{ 0x7e, 0x00 },
	{ 0x7f, 0x60 },
	{ 0xfe, 0x01 },
	{ 0xa0, 0x0b },
	/*AEC*/
	{ 0xfe, 0x01 },
	{ 0x01, 0x04 },
	{ 0x02, 0x60 },
	{ 0x03, 0x02 },
	{ 0x04, 0x48 },
	{ 0x05, 0x18 },
	{ 0x06, 0x50 },
	{ 0x07, 0x10 },
	{ 0x08, 0x38 },
	{ 0x0a, 0xc0 },
	{ 0x1b, 0x04 },
	{ 0x21, 0x04 },
	{ 0xfe, 0x00 },
	{ 0x20, 0x03 },
	{ 0xfe, 0x03 },
	{ 0x12, 0x40 },
	{ 0x13, 0x06 },
	{ 0x04, 0x01 },
	{ 0x05, 0x00 },
	{ 0xfe, 0x00 },
	{ REG_NULL, 0x00 },
};

static const struct saoHaiDeng_framesize saoHaiDeng_dvp_framesizes[] = {
    {
        /* SVGA */
        .width = 800,
        .height = 600,
        .max_fps =
            {
                .numerator = 10000,
                .denominator = 160000,
            },
        .regs = saoHaiDeng_dvp_svga_20fps,
    },
    {
        /* SVGA */
        .width = 800,
        .height = 600,
        .max_fps =
            {
                .numerator = 10000,
                .denominator = 300000,
            },
        .regs = saoHaiDeng_dvp_svga_30fps,
    },
    {
        /* FULL */
        .width = 1600,
        .height = 1200,
        .max_fps =
            {
                .numerator = 10000,
                .denominator = 160000,
            },
        .regs = saoHaiDeng_dvp_full,
    }};

static const struct saoHaiDeng_framesize saoHaiDeng_mipi_framesizes[] = {
    // { /* SVGA */
    // 	.width		= 800,
    // 	.height		= 600,
    // 	.max_fps = {
    // 		.numerator = 10000,
    // 		.denominator = 160000,
    // 	},
    // 	.regs		= saoHaiDeng_mipi_svga_20fps,
    // },
    {
        /* SVGA */
        .width = 640,
        .height = 513,
        .max_fps =
            {
                .numerator = 10000,
                .denominator = 250000,
            },
        .regs = saoHaiDeng_mipi_svga_30fps,
    },
    {
        /* FULL */
        .width = 1920,
        .height = 1537,
        .max_fps =
            {
                .numerator = 10000,
                .denominator = 250000,
            },
        .regs = saoHaiDeng_mipi_full,
    }};

static const s64 link_freq_menu_items[] = { 100000000 };

static const struct saoHaiDeng_pixfmt saoHaiDeng_formats[] = { {
	.code = MEDIA_BUS_FMT_UYVY8_2X8,
} };

static inline struct saoHaiDeng *to_saoHaiDeng(struct v4l2_subdev *sd)
{
	return container_of(sd, struct saoHaiDeng, sd);
}

/* sensor register write */
static int saoHaiDeng_write(struct i2c_client *client, u8 reg, u8 val)
{
	struct i2c_msg msg;
	u8 buf[2];
	int ret = 0;

	dev_dbg(&client->dev, "write reg(0x%x val:0x%x)!\n", reg, val);

	buf[0] = reg & 0xFF;
	buf[1] = val;

	msg.addr = client->addr;
	msg.flags = client->flags;
	msg.buf = buf;
	msg.len = sizeof(buf);

	// ret = i2c_transfer(client->adapter, &msg, 1);
	// if (ret >= 0)
	// 	return 0;

	// dev_err(&client->dev,
	// 	"saoHaiDeng write reg(0x%x val:0x%x) failed !\n", reg, val);

	return ret;
}

/* sensor register read */
static int saoHaiDeng_read(struct i2c_client *client, u8 reg, u8 *val)
{
	struct i2c_msg msg[2];
	u8 buf[1];
	int ret = 0;

	buf[0] = reg & 0xFF;

	msg[0].addr = client->addr;
	msg[0].flags = client->flags;
	msg[0].buf = buf;
	msg[0].len = sizeof(buf);

	msg[1].addr = client->addr;
	msg[1].flags = client->flags | I2C_M_RD;
	msg[1].buf = buf;
	msg[1].len = 1;

	// ret = i2c_transfer(client->adapter, msg, 2);
	// if (ret >= 0) {
	// 	*val = buf[0];
	// 	return 0;
	// }

	// dev_err(&client->dev,
	// 	"saoHaiDeng read reg:0x%x failed !\n", reg);

	return ret;
}

static int saoHaiDeng_write_array(struct i2c_client *client,
				  const struct sensor_register *regs)
{
	int i, ret = 0;

	i = 0;
	while (regs[i].addr != REG_NULL) {
		ret = saoHaiDeng_write(client, regs[i].addr, regs[i].value);
		if (ret) {
			dev_err(&client->dev, "%s failed !\n", __func__);
			break;
		}

		i++;
	}

	return ret;
}

static void saoHaiDeng_get_default_format(struct saoHaiDeng *saoHaiDeng,
					  struct v4l2_mbus_framefmt *format)
{
	format->width = saoHaiDeng->framesize_cfg[0].width;
	format->height = saoHaiDeng->framesize_cfg[0].height;
	format->colorspace = V4L2_COLORSPACE_SRGB;
	format->code = saoHaiDeng_formats[0].code;
	format->field = V4L2_FIELD_NONE;
}

static void saoHaiDeng_set_streaming(struct saoHaiDeng *saoHaiDeng, int on)
{
	struct i2c_client *client = saoHaiDeng->client;
	int ret = 0;
	u8 val;

	dev_dbg(&client->dev, "%s: on: %d\n", __func__, on);

	if (saoHaiDeng->bus_cfg.bus_type == V4L2_MBUS_CSI2_DPHY) {
		val = on ? 0x94 : 0x84;
		ret = saoHaiDeng_write(client, 0xfe, 0x03);
		ret |= saoHaiDeng_write(client, 0x10, val);
	} else {
		val = on ? 0x0f : 0;
		ret = saoHaiDeng_write(client, 0xf2, val);
	}
	if (ret)
		dev_err(&client->dev, "saoHaiDeng soft standby failed\n");
}

/*
 * V4L2 subdev video and pad level operations
 */

static int saoHaiDeng_enum_mbus_code(struct v4l2_subdev *sd,
				     struct v4l2_subdev_pad_config *cfg,
				     struct v4l2_subdev_mbus_code_enum *code)
{
	struct i2c_client *client = v4l2_get_subdevdata(sd);

	dev_dbg(&client->dev, "%s:\n", __func__);

	if (code->index >= ARRAY_SIZE(saoHaiDeng_formats))
		return -EINVAL;

	code->code = saoHaiDeng_formats[code->index].code;

	return 0;
}

static int saoHaiDeng_enum_frame_sizes(struct v4l2_subdev *sd,
				       struct v4l2_subdev_pad_config *cfg,
				       struct v4l2_subdev_frame_size_enum *fse)
{
	struct saoHaiDeng *saoHaiDeng = to_saoHaiDeng(sd);
	struct i2c_client *client = v4l2_get_subdevdata(sd);
	int i = ARRAY_SIZE(saoHaiDeng_formats);

	dev_dbg(&client->dev, "%s:\n", __func__);

	if (fse->index >= saoHaiDeng->cfg_num)
		return -EINVAL;

	while (--i)
		if (fse->code == saoHaiDeng_formats[i].code)
			break;

	fse->code = saoHaiDeng_formats[i].code;

	fse->min_width = saoHaiDeng->framesize_cfg[fse->index].width;
	fse->max_width = fse->min_width;
	fse->max_height = saoHaiDeng->framesize_cfg[fse->index].height;
	fse->min_height = fse->max_height;

	return 0;
}

static int saoHaiDeng_get_fmt(struct v4l2_subdev *sd,
			      struct v4l2_subdev_pad_config *cfg,
			      struct v4l2_subdev_format *fmt)
{
	struct i2c_client *client = v4l2_get_subdevdata(sd);
	struct saoHaiDeng *saoHaiDeng = to_saoHaiDeng(sd);

	dev_dbg(&client->dev, "%s enter\n", __func__);

	if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
#ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
		struct v4l2_mbus_framefmt *mf;

		mf = v4l2_subdev_get_try_format(sd, cfg, 0);
		mutex_lock(&saoHaiDeng->lock);
		fmt->format = *mf;
		mutex_unlock(&saoHaiDeng->lock);
		return 0;
#else
		return -ENOTTY;
#endif
	}

	mutex_lock(&saoHaiDeng->lock);
	fmt->format = saoHaiDeng->format;
	mutex_unlock(&saoHaiDeng->lock);

	dev_dbg(&client->dev, "%s: %x %dx%d\n", __func__,
		saoHaiDeng->format.code, saoHaiDeng->format.width,
		saoHaiDeng->format.height);

	return 0;
}

static void __saoHaiDeng_try_frame_size_fps(
	struct saoHaiDeng *saoHaiDeng, struct v4l2_mbus_framefmt *mf,
	const struct saoHaiDeng_framesize **size, unsigned int fps)
{
	const struct saoHaiDeng_framesize *fsize =
		&saoHaiDeng->framesize_cfg[0];
	const struct saoHaiDeng_framesize *match = NULL;
	unsigned int i = saoHaiDeng->cfg_num;
	unsigned int min_err = UINT_MAX;

	while (i--) {
		unsigned int err = abs(fsize->width - mf->width) +
				   abs(fsize->height - mf->height);
		if (err < min_err && fsize->regs[0].addr) {
			min_err = err;
			match = fsize;
		}
		fsize++;
	}

	if (!match) {
		match = &saoHaiDeng->framesize_cfg[0];
	} else {
		fsize = &saoHaiDeng->framesize_cfg[0];
		for (i = 0; i < saoHaiDeng->cfg_num; i++) {
			if (fsize->width == match->width &&
			    fsize->height == match->height &&
			    fps >= DIV_ROUND_CLOSEST(fsize->max_fps.denominator,
						     fsize->max_fps.numerator))
				match = fsize;

			fsize++;
		}
	}

	mf->width = match->width;
	mf->height = match->height;

	if (size)
		*size = match;
}

#ifdef SAOHAIDENG_EXPOSURE_CONTROL
/*
 * the function is called before sensor register setting in VIDIOC_S_FMT
 */
/* Row times = Hb + Sh_delay + win_width + 4*/

static int saoHaiDeng_aec_ctrl(struct v4l2_subdev *sd,
			       struct v4l2_mbus_framefmt *mf)
{
	struct i2c_client *client = v4l2_get_subdevdata(sd);
	int ret = 0;
	u8 value;
	static unsigned int capture_fps = 10, capture_lines = 1266;
	static unsigned int preview_fps = 20, preview_lines = 1266;
	static unsigned int lines_10ms = 1;
	static unsigned int shutter_h = 0x04, shutter_l = 0xe2;
	static unsigned int cap = 0, shutter = 0x04e2;

	dev_info(&client->dev, "%s enter\n", __func__);
	if ((mf->width == 800 && mf->height == 600) && cap == 1) {
		cap = 0;
		ret = saoHaiDeng_write(client, 0xfe, 0x00);
		ret |= saoHaiDeng_write(client, 0xb6, 0x00);
		ret |= saoHaiDeng_write(client, 0x03, shutter_h);
		ret |= saoHaiDeng_write(client, 0x04, shutter_l);
		ret |= saoHaiDeng_write(client, 0x82, 0xfa);
		ret |= saoHaiDeng_write(client, 0xb6, 0x01);
		if (ret)
			dev_err(&client->dev, "saoHaiDeng reconfig failed!\n");
	}
	if (mf->width == 1600 && mf->height == 1200) {
		cap = 1;
		ret = saoHaiDeng_write(client, 0xfe, 0x00);
		ret |= saoHaiDeng_write(client, 0xb6, 0x00);
		ret |= saoHaiDeng_write(client, 0x82, 0xf8);

		/*shutter calculate*/
		ret |= saoHaiDeng_read(client, 0x03, &value);
		shutter_h = value;
		shutter = (value << 8);
		ret |= saoHaiDeng_read(client, 0x04, &value);
		shutter_l = value;
		shutter |= (value & 0xff);
		dev_info(&client->dev, "%s(%d) 800x600 shutter read(0x%04x)!\n",
			 __func__, __LINE__, shutter);
		shutter = shutter * capture_lines / preview_lines;
		shutter = shutter * capture_fps / preview_fps;
		lines_10ms = capture_fps * capture_lines / 100;
		if (shutter > lines_10ms) {
			shutter = shutter + lines_10ms / 2;
			shutter /= lines_10ms;
			shutter *= lines_10ms;
		}
		if (shutter < 1)
			shutter = 0x276;
		dev_info(&client->dev, "%s(%d)lines_10ms(%d),cal(0x%08x)!\n",
			 __func__, __LINE__, lines_10ms, shutter);

		ret |= saoHaiDeng_write(client, 0x03, ((shutter >> 8) & 0x1f));
		ret |= saoHaiDeng_write(client, 0x04, (shutter & 0xff));
		if (ret)
			dev_err(&client->dev, "full exp reconfig failed!\n");
	}
	return ret;
}
#endif

static int saoHaiDeng_set_fmt(struct v4l2_subdev *sd,
			      struct v4l2_subdev_pad_config *cfg,
			      struct v4l2_subdev_format *fmt)
{
	struct i2c_client *client = v4l2_get_subdevdata(sd);
	int index = ARRAY_SIZE(saoHaiDeng_formats);
	struct v4l2_mbus_framefmt *mf = &fmt->format;
	const struct saoHaiDeng_framesize *size = NULL;
	struct saoHaiDeng *saoHaiDeng = to_saoHaiDeng(sd);

	dev_info(&client->dev, "%s enter\n", __func__);

	__saoHaiDeng_try_frame_size_fps(saoHaiDeng, mf, &size, saoHaiDeng->fps);

	while (--index >= 0)
		if (saoHaiDeng_formats[index].code == mf->code)
			break;

	if (index < 0)
		return -EINVAL;

	mf->colorspace = V4L2_COLORSPACE_SRGB;
	mf->code = saoHaiDeng_formats[index].code;
	mf->field = V4L2_FIELD_NONE;

	mutex_lock(&saoHaiDeng->lock);

	if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
#ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
		mf = v4l2_subdev_get_try_format(sd, cfg, fmt->pad);
		*mf = fmt->format;
#else
		return -ENOTTY;
#endif
	} else {
		if (saoHaiDeng->streaming) {
			mutex_unlock(&saoHaiDeng->lock);
			return -EBUSY;
		}

		saoHaiDeng->frame_size = size;
		saoHaiDeng->format = fmt->format;
	}

#ifdef SAOHAIDENG_EXPOSURE_CONTROL
	if (saoHaiDeng->power_on)
		saoHaiDeng_aec_ctrl(sd, mf);
#endif
	mutex_unlock(&saoHaiDeng->lock);
	return 0;
}

static int saoHaiDeng_init(struct v4l2_subdev *sd, u32 val);
static int saoHaiDeng_s_stream(struct v4l2_subdev *sd, int on)
{
	struct i2c_client *client = v4l2_get_subdevdata(sd);
	struct saoHaiDeng *saoHaiDeng = to_saoHaiDeng(sd);
	int ret = 0;
	unsigned int fps;
	unsigned int delay_us;

	fps = DIV_ROUND_CLOSEST(saoHaiDeng->frame_size->max_fps.denominator,
				saoHaiDeng->frame_size->max_fps.numerator);

	dev_info(&client->dev, "%s: on: %d, %dx%d@%d\n", __func__, on,
		 saoHaiDeng->frame_size->width, saoHaiDeng->frame_size->height,
		 DIV_ROUND_CLOSEST(saoHaiDeng->frame_size->max_fps.denominator,
				   saoHaiDeng->frame_size->max_fps.numerator));

	mutex_lock(&saoHaiDeng->lock);

	on = !!on;

	if (saoHaiDeng->streaming == on)
		goto unlock;

	if (!on) {
		/* Stop Streaming Sequence */
		saoHaiDeng_set_streaming(saoHaiDeng, on);
		saoHaiDeng->streaming = on;
		/* delay to enable oneframe complete */
		delay_us = 1000 * 1000 / fps;
		usleep_range(delay_us, delay_us + 10);
		dev_info(&client->dev, "%s: on: %d, sleep(%dus)\n", __func__,
			 on, delay_us);
		goto unlock;
	}
	if (ret)
		dev_err(&client->dev, "init error\n");

	ret = saoHaiDeng_write_array(client, saoHaiDeng->frame_size->regs);
	if (ret)
		goto unlock;

	saoHaiDeng_set_streaming(saoHaiDeng, on);
	saoHaiDeng->streaming = on;

unlock:
	mutex_unlock(&saoHaiDeng->lock);
	return ret;
}

static int saoHaiDeng_set_test_pattern(struct saoHaiDeng *saoHaiDeng, int value)
{
	return 0;
}

static int saoHaiDeng_s_ctrl(struct v4l2_ctrl *ctrl)
{
	struct saoHaiDeng *saoHaiDeng =
		container_of(ctrl->handler, struct saoHaiDeng, ctrls);

	switch (ctrl->id) {
	case V4L2_CID_TEST_PATTERN:
		return saoHaiDeng_set_test_pattern(saoHaiDeng, ctrl->val);
	}

	return 0;
}

static const struct v4l2_ctrl_ops saoHaiDeng_ctrl_ops = {
	.s_ctrl = saoHaiDeng_s_ctrl,
};

static const char *const saoHaiDeng_test_pattern_menu[] = {
	"Disabled",
	"Vertical Color Bars",
};

/* -----------------------------------------------------------------------------
 * V4L2 subdev internal operations
 */

#ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
static int saoHaiDeng_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
{
	struct saoHaiDeng *saoHaiDeng = to_saoHaiDeng(sd);
	struct i2c_client *client = v4l2_get_subdevdata(sd);
	struct v4l2_mbus_framefmt *format =
		v4l2_subdev_get_try_format(sd, fh->pad, 0);

	dev_dbg(&client->dev, "%s:\n", __func__);

	saoHaiDeng_get_default_format(saoHaiDeng, format);

	return 0;
}
#endif

static int saoHaiDeng_g_mbus_config(struct v4l2_subdev *sd, unsigned int pad_id,
				    struct v4l2_mbus_config *config)
{
	struct saoHaiDeng *saoHaiDeng = to_saoHaiDeng(sd);

	if (saoHaiDeng->bus_cfg.bus_type == V4L2_MBUS_CSI2_DPHY) {
		config->type = V4L2_MBUS_CSI2_DPHY;
		config->flags = V4L2_MBUS_CSI2_4_LANE |
				V4L2_MBUS_CSI2_CHANNEL_0 |
				V4L2_MBUS_CSI2_CONTINUOUS_CLOCK;
	} else {
		config->type = V4L2_MBUS_PARALLEL;
		config->flags = V4L2_MBUS_HSYNC_ACTIVE_HIGH |
				V4L2_MBUS_VSYNC_ACTIVE_LOW |
				V4L2_MBUS_PCLK_SAMPLE_RISING;
	}

	return 0;
}

static int saoHaiDeng_g_frame_interval(struct v4l2_subdev *sd,
				       struct v4l2_subdev_frame_interval *fi)
{
	struct saoHaiDeng *saoHaiDeng = to_saoHaiDeng(sd);

	fi->interval = saoHaiDeng->frame_size->max_fps;

	return 0;
}

static int saoHaiDeng_s_frame_interval(struct v4l2_subdev *sd,
				       struct v4l2_subdev_frame_interval *fi)
{
	struct i2c_client *client = v4l2_get_subdevdata(sd);
	struct saoHaiDeng *saoHaiDeng = to_saoHaiDeng(sd);
	const struct saoHaiDeng_framesize *size = NULL;
	struct v4l2_mbus_framefmt mf;
	unsigned int fps;
	int ret = 0;

	dev_dbg(&client->dev, "Setting %d/%d frame interval\n",
		fi->interval.numerator, fi->interval.denominator);

	mutex_lock(&saoHaiDeng->lock);

	if (saoHaiDeng->format.width == 1600)
		goto unlock;

	fps = DIV_ROUND_CLOSEST(fi->interval.denominator,
				fi->interval.numerator);
	mf = saoHaiDeng->format;
	__saoHaiDeng_try_frame_size_fps(saoHaiDeng, &mf, &size, fps);

	if (saoHaiDeng->frame_size != size) {
		dev_info(&client->dev, "%s match wxh@FPS is %dx%d@%d\n",
			 __func__, size->width, size->height,
			 DIV_ROUND_CLOSEST(size->max_fps.denominator,
					   size->max_fps.numerator));
		ret |= saoHaiDeng_write_array(client, size->regs);
		if (ret)
			goto unlock;
		saoHaiDeng->frame_size = size;
		saoHaiDeng->fps = fps;
	}
unlock:
	mutex_unlock(&saoHaiDeng->lock);

	return ret;
}

static void saoHaiDeng_get_module_inf(struct saoHaiDeng *saoHaiDeng,
				      struct rkmodule_inf *inf)
{
	memset(inf, 0, sizeof(*inf));
	strlcpy(inf->base.sensor, DRIVER_NAME, sizeof(inf->base.sensor));
	strlcpy(inf->base.module, saoHaiDeng->module_name,
		sizeof(inf->base.module));
	strlcpy(inf->base.lens, saoHaiDeng->len_name, sizeof(inf->base.lens));
}

static long saoHaiDeng_ioctl(struct v4l2_subdev *sd, unsigned int cmd,
			     void *arg)
{
	struct saoHaiDeng *saoHaiDeng = to_saoHaiDeng(sd);
	long ret = 0;
	u32 stream = 0;

	switch (cmd) {
	case RKMODULE_GET_MODULE_INFO:
		saoHaiDeng_get_module_inf(saoHaiDeng,
					  (struct rkmodule_inf *)arg);
		break;
	case RKMODULE_SET_QUICK_STREAM:

		stream = *((u32 *)arg);

		saoHaiDeng_set_streaming(saoHaiDeng, !!stream);
		break;
	default:
		ret = -ENOIOCTLCMD;
		break;
	}

	return ret;
}

#ifdef CONFIG_COMPAT
static long saoHaiDeng_compat_ioctl32(struct v4l2_subdev *sd, unsigned int cmd,
				      unsigned long arg)
{
	void __user *up = compat_ptr(arg);
	struct rkmodule_inf *inf;
	struct rkmodule_awb_cfg *cfg;
	long ret;
	u32 stream = 0;

	switch (cmd) {
	case RKMODULE_GET_MODULE_INFO:
		inf = kzalloc(sizeof(*inf), GFP_KERNEL);
		if (!inf) {
			ret = -ENOMEM;
			return ret;
		}

		ret = saoHaiDeng_ioctl(sd, cmd, inf);
		if (!ret) {
			ret = copy_to_user(up, inf, sizeof(*inf));
			if (ret)
				ret = -EFAULT;
		}
		kfree(inf);
		break;
	case RKMODULE_AWB_CFG:
		cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
		if (!cfg) {
			ret = -ENOMEM;
			return ret;
		}

		ret = copy_from_user(cfg, up, sizeof(*cfg));
		if (!ret)
			ret = saoHaiDeng_ioctl(sd, cmd, cfg);
		else
			ret = -EFAULT;
		kfree(cfg);
		break;
	case RKMODULE_SET_QUICK_STREAM:
		ret = copy_from_user(&stream, up, sizeof(u32));
		if (!ret)
			ret = saoHaiDeng_ioctl(sd, cmd, &stream);
		else
			ret = -EFAULT;
		break;
	default:
		ret = -ENOIOCTLCMD;
		break;
	}

	return ret;
}
#endif

static int saoHaiDeng_init(struct v4l2_subdev *sd, u32 val)
{
	int ret;
	struct saoHaiDeng *saoHaiDeng = to_saoHaiDeng(sd);
	struct i2c_client *client = saoHaiDeng->client;

	dev_info(&client->dev, "%s(%d)\n", __func__, __LINE__);

	if (saoHaiDeng->bus_cfg.bus_type == V4L2_MBUS_CSI2_DPHY)
		ret = saoHaiDeng_write_array(client, saoHaiDeng_mipi_init_regs);
	else
		ret = saoHaiDeng_write_array(client, saoHaiDeng_dvp_init_regs);

	return ret;
}

static int saoHaiDeng_power(struct v4l2_subdev *sd, int on)
{
	int ret;
	struct saoHaiDeng *saoHaiDeng = to_saoHaiDeng(sd);
	struct i2c_client *client = saoHaiDeng->client;

	dev_info(&client->dev, "%s(%d) on(%d)\n", __func__, __LINE__, on);
	if (on) {
		if (!IS_ERR(saoHaiDeng->pwdn_gpio)) {
			gpiod_set_value_cansleep(saoHaiDeng->pwdn_gpio, 0);
			usleep_range(2000, 5000);
		}
		ret = saoHaiDeng_init(sd, 0);
		usleep_range(10000, 20000);
		if (ret)
			dev_err(&client->dev, "init error\n");
		saoHaiDeng->power_on = true;
	} else {
		if (!IS_ERR(saoHaiDeng->pwdn_gpio)) {
			gpiod_set_value_cansleep(saoHaiDeng->pwdn_gpio, 1);
			usleep_range(2000, 5000);
		}
		saoHaiDeng->power_on = false;
	}
	return 0;
}

static int
saoHaiDeng_enum_frame_interval(struct v4l2_subdev *sd,
			       struct v4l2_subdev_pad_config *cfg,
			       struct v4l2_subdev_frame_interval_enum *fie)
{
	struct saoHaiDeng *saoHaiDeng = to_saoHaiDeng(sd);

	if (fie->index >= saoHaiDeng->cfg_num)
		return -EINVAL;

	fie->code = MEDIA_BUS_FMT_YVYU8_2X8;
	fie->width = saoHaiDeng->framesize_cfg[fie->index].width;
	fie->height = saoHaiDeng->framesize_cfg[fie->index].height;
	fie->interval = saoHaiDeng->framesize_cfg[fie->index].max_fps;
	return 0;
}

static const struct v4l2_subdev_core_ops saoHaiDeng_subdev_core_ops = {
	.log_status = v4l2_ctrl_subdev_log_status,
	.subscribe_event = v4l2_ctrl_subdev_subscribe_event,
	.unsubscribe_event = v4l2_event_subdev_unsubscribe,
	.ioctl = saoHaiDeng_ioctl,
#ifdef CONFIG_COMPAT
	.compat_ioctl32 = saoHaiDeng_compat_ioctl32,
#endif
	.s_power = saoHaiDeng_power,
};

static const struct v4l2_subdev_video_ops saoHaiDeng_subdev_video_ops = {
	.s_stream = saoHaiDeng_s_stream,
	.g_frame_interval = saoHaiDeng_g_frame_interval,
	.s_frame_interval = saoHaiDeng_s_frame_interval,
};

static const struct v4l2_subdev_pad_ops saoHaiDeng_subdev_pad_ops = {
	.enum_mbus_code = saoHaiDeng_enum_mbus_code,
	.enum_frame_size = saoHaiDeng_enum_frame_sizes,
	.enum_frame_interval = saoHaiDeng_enum_frame_interval,
	.get_fmt = saoHaiDeng_get_fmt,
	.set_fmt = saoHaiDeng_set_fmt,
	.get_mbus_config = saoHaiDeng_g_mbus_config,
};

#ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
static const struct v4l2_subdev_ops saoHaiDeng_subdev_ops = {
	.core = &saoHaiDeng_subdev_core_ops,
	.video = &saoHaiDeng_subdev_video_ops,
	.pad = &saoHaiDeng_subdev_pad_ops,
};

static const struct v4l2_subdev_internal_ops saoHaiDeng_subdev_internal_ops = {
	.open = saoHaiDeng_open,
};
#endif

static int saoHaiDeng_detect(struct saoHaiDeng *saoHaiDeng)
{
	struct i2c_client *client = saoHaiDeng->client;
	u8 pid, ver;
	int ret;

	dev_dbg(&client->dev, "%s:\n", __func__);

	/* Check sensor revision */
	ret = saoHaiDeng_read(client, REG_SC_CHIP_ID_H, &pid);
	if (!ret)
		ret = saoHaiDeng_read(client, REG_SC_CHIP_ID_L, &ver);

	if (!ret) {
		unsigned short id;

		id = SAOHAIDENG_ID;
		if (id != SAOHAIDENG_ID) {
			ret = -1;
			dev_err(&client->dev,
				"Sensor detection failed (%04X, %d)\n", id,
				ret);
		} else {
			dev_info(&client->dev, "Found GC%04X sensor\n", id);
			if (!IS_ERR(saoHaiDeng->pwdn_gpio))
				gpiod_set_value_cansleep(saoHaiDeng->pwdn_gpio,
							 1);
		}
	}

	return ret;
}

static int __saoHaiDeng_power_on(struct saoHaiDeng *saoHaiDeng)
{
	int ret;
	struct device *dev = &saoHaiDeng->client->dev;

	dev_info(dev, "%s(%d)\n", __func__, __LINE__);
	if (!IS_ERR(saoHaiDeng->power_gpio)) {
		gpiod_set_value_cansleep(saoHaiDeng->power_gpio, 1);
		usleep_range(2000, 5000);
	}

	if (!IS_ERR(saoHaiDeng->reset_gpio)) {
		gpiod_set_value_cansleep(saoHaiDeng->reset_gpio, 0);
		usleep_range(2000, 5000);
		gpiod_set_value_cansleep(saoHaiDeng->reset_gpio, 1);
		usleep_range(2000, 5000);
	}

	if (!IS_ERR(saoHaiDeng->xvclk)) {
		ret = clk_set_rate(saoHaiDeng->xvclk, 24000000);
		if (ret < 0)
			dev_info(dev, "Failed to set xvclk rate (24MHz)\n");
	}

	if (!IS_ERR(saoHaiDeng->pwdn_gpio)) {
		gpiod_set_value_cansleep(saoHaiDeng->pwdn_gpio, 1);
		usleep_range(2000, 5000);
	}

	if (!IS_ERR(saoHaiDeng->supplies)) {
		ret = regulator_bulk_enable(SAOHAIDENG_NUM_SUPPLIES,
					    saoHaiDeng->supplies);
		if (ret < 0)
			dev_info(dev, "Failed to enable regulators\n");

		usleep_range(20000, 50000);
	}

	if (!IS_ERR(saoHaiDeng->pwdn_gpio)) {
		gpiod_set_value_cansleep(saoHaiDeng->pwdn_gpio, 0);
		usleep_range(2000, 5000);
	}

	if (!IS_ERR(saoHaiDeng->reset_gpio)) {
		gpiod_set_value_cansleep(saoHaiDeng->reset_gpio, 0);
		usleep_range(2000, 5000);
	}

	if (!IS_ERR(saoHaiDeng->xvclk)) {
		ret = clk_prepare_enable(saoHaiDeng->xvclk);
		if (ret < 0)
			dev_info(dev, "Failed to enable xvclk\n");
	}
	usleep_range(7000, 10000);
	saoHaiDeng->power_on = true;
	return 0;
}

static void __saoHaiDeng_power_off(struct saoHaiDeng *saoHaiDeng)
{
	dev_info(&saoHaiDeng->client->dev, "%s(%d)\n", __func__, __LINE__);
	if (!IS_ERR(saoHaiDeng->xvclk))
		clk_disable_unprepare(saoHaiDeng->xvclk);
	if (!IS_ERR(saoHaiDeng->supplies))
		regulator_bulk_disable(SAOHAIDENG_NUM_SUPPLIES,
				       saoHaiDeng->supplies);
	if (!IS_ERR(saoHaiDeng->pwdn_gpio))
		gpiod_set_value_cansleep(saoHaiDeng->pwdn_gpio, 1);
	if (!IS_ERR(saoHaiDeng->reset_gpio))
		gpiod_set_value_cansleep(saoHaiDeng->reset_gpio, 0);
	if (!IS_ERR(saoHaiDeng->power_gpio))
		gpiod_set_value_cansleep(saoHaiDeng->power_gpio, 0);
	saoHaiDeng->power_on = false;
}

static int saoHaiDeng_configure_regulators(struct saoHaiDeng *saoHaiDeng)
{
	unsigned int i;

	for (i = 0; i < SAOHAIDENG_NUM_SUPPLIES; i++)
		saoHaiDeng->supplies[i].supply = saoHaiDeng_supply_names[i];

	return devm_regulator_bulk_get(&saoHaiDeng->client->dev,
				       SAOHAIDENG_NUM_SUPPLIES,
				       saoHaiDeng->supplies);
}

static int saoHaiDeng_parse_of(struct saoHaiDeng *saoHaiDeng)
{
	struct device *dev = &saoHaiDeng->client->dev;
	struct device_node *endpoint;
	int ret;

	endpoint = of_graph_get_next_endpoint(dev->of_node, NULL);
	if (!endpoint) {
		dev_err(dev, "Failed to get endpoint\n");
		return -EINVAL;
	}

	ret = v4l2_fwnode_endpoint_parse(of_fwnode_handle(endpoint),
					 &saoHaiDeng->bus_cfg);
	if (ret) {
		dev_err(dev, "Failed to parse endpoint\n");
		of_node_put(endpoint);
		return ret;
	}
	if (saoHaiDeng->bus_cfg.bus_type == V4L2_MBUS_CSI2_DPHY) {
		saoHaiDeng->framesize_cfg = saoHaiDeng_mipi_framesizes;
		saoHaiDeng->cfg_num = ARRAY_SIZE(saoHaiDeng_mipi_framesizes);
	} else {
		saoHaiDeng->framesize_cfg = saoHaiDeng_dvp_framesizes;
		saoHaiDeng->cfg_num = ARRAY_SIZE(saoHaiDeng_dvp_framesizes);
	}

	saoHaiDeng->power_gpio = devm_gpiod_get(dev, "power", GPIOD_OUT_LOW);
	if (IS_ERR(saoHaiDeng->power_gpio))
		dev_info(dev, "Failed to get power-gpios, maybe no use\n");
	saoHaiDeng->pwdn_gpio = devm_gpiod_get(dev, "pwdn", GPIOD_OUT_LOW);
	if (IS_ERR(saoHaiDeng->pwdn_gpio))
		dev_info(dev, "Failed to get pwdn-gpios, maybe no use\n");
	saoHaiDeng->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_LOW);
	if (IS_ERR(saoHaiDeng->reset_gpio))
		dev_info(dev, "Failed to get reset-gpios, maybe no use\n");

	ret = saoHaiDeng_configure_regulators(saoHaiDeng);
	if (ret)
		dev_info(dev, "Failed to get power regulators\n");

	return ret;
}

static int saoHaiDeng_probe(struct i2c_client *client,
			    const struct i2c_device_id *id)
{
	struct device *dev = &client->dev;
	struct device_node *node = dev->of_node;
	struct v4l2_subdev *sd;
	struct saoHaiDeng *saoHaiDeng;
	char facing[2];
	int ret;

	dev_info(dev, "driver version: %02x.%02x.%02x", DRIVER_VERSION >> 16,
		 (DRIVER_VERSION & 0xff00) >> 8, DRIVER_VERSION & 0x00ff);

	saoHaiDeng =
		devm_kzalloc(&client->dev, sizeof(*saoHaiDeng), GFP_KERNEL);
	if (!saoHaiDeng)
		return -ENOMEM;

	ret = of_property_read_u32(node, RKMODULE_CAMERA_MODULE_INDEX,
				   &saoHaiDeng->module_index);
	ret |= of_property_read_string(node, RKMODULE_CAMERA_MODULE_FACING,
				       &saoHaiDeng->module_facing);
	ret |= of_property_read_string(node, RKMODULE_CAMERA_MODULE_NAME,
				       &saoHaiDeng->module_name);
	ret |= of_property_read_string(node, RKMODULE_CAMERA_LENS_NAME,
				       &saoHaiDeng->len_name);
	if (ret) {
		dev_err(dev, "could not get module information!\n");
		return -EINVAL;
	}

	saoHaiDeng->client = client;
	saoHaiDeng->xvclk = devm_clk_get(&client->dev, "xclk");
	if (IS_ERR(saoHaiDeng->xvclk)) {
		dev_err(&client->dev, "Failed to get xclk\n");
		return -EINVAL;
	}

	ret = saoHaiDeng_parse_of(saoHaiDeng);
	if (ret != 0)
		return -EINVAL;

	v4l2_ctrl_handler_init(&saoHaiDeng->ctrls, 3);
	saoHaiDeng->link_frequency =
		v4l2_ctrl_new_std(&saoHaiDeng->ctrls, &saoHaiDeng_ctrl_ops,
				  V4L2_CID_PIXEL_RATE, 0, SAOHAIDENG_PIXEL_RATE,
				  1, SAOHAIDENG_PIXEL_RATE);

	v4l2_ctrl_new_int_menu(&saoHaiDeng->ctrls, NULL, V4L2_CID_LINK_FREQ, 0,
			       0, link_freq_menu_items);
	v4l2_ctrl_new_std_menu_items(
		&saoHaiDeng->ctrls, &saoHaiDeng_ctrl_ops, V4L2_CID_TEST_PATTERN,
		ARRAY_SIZE(saoHaiDeng_test_pattern_menu) - 1, 0, 0,
		saoHaiDeng_test_pattern_menu);
	saoHaiDeng->sd.ctrl_handler = &saoHaiDeng->ctrls;

	if (saoHaiDeng->ctrls.error) {
		dev_err(&client->dev, "%s: control initialization error %d\n",
			__func__, saoHaiDeng->ctrls.error);
		return saoHaiDeng->ctrls.error;
	}

	sd = &saoHaiDeng->sd;
	client->flags |= I2C_CLIENT_SCCB;
#ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
	v4l2_i2c_subdev_init(sd, client, &saoHaiDeng_subdev_ops);

	sd->internal_ops = &saoHaiDeng_subdev_internal_ops;
	sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE | V4L2_SUBDEV_FL_HAS_EVENTS;
#endif

#if defined(CONFIG_MEDIA_CONTROLLER)
	saoHaiDeng->pad.flags = MEDIA_PAD_FL_SOURCE;
	sd->entity.function = MEDIA_ENT_F_CAM_SENSOR;
	ret = media_entity_pads_init(&sd->entity, 1, &saoHaiDeng->pad);
	if (ret < 0) {
		v4l2_ctrl_handler_free(&saoHaiDeng->ctrls);
		return ret;
	}
#endif

	mutex_init(&saoHaiDeng->lock);

	saoHaiDeng_get_default_format(saoHaiDeng, &saoHaiDeng->format);
	saoHaiDeng->frame_size = &saoHaiDeng->framesize_cfg[0];
	saoHaiDeng->format.width = saoHaiDeng->framesize_cfg[0].width;
	saoHaiDeng->format.height = saoHaiDeng->framesize_cfg[0].height;
	saoHaiDeng->fps = DIV_ROUND_CLOSEST(
		saoHaiDeng->framesize_cfg[0].max_fps.denominator,
		saoHaiDeng->framesize_cfg[0].max_fps.numerator);

	__saoHaiDeng_power_on(saoHaiDeng);
	saoHaiDeng->xvclk_frequency = clk_get_rate(saoHaiDeng->xvclk);
	if (saoHaiDeng->xvclk_frequency < 6000000 ||
	    saoHaiDeng->xvclk_frequency > 27000000)
		goto error;

	ret = saoHaiDeng_detect(saoHaiDeng);
	if (ret < 0) {
		dev_info(&client->dev,
			 "Check id  failed:\n"
			 "check following information:\n"
			 "Power/PowerDown/Reset/Mclk/I2cBus !!\n");
		goto error;
	}

	memset(facing, 0, sizeof(facing));
	if (strcmp(saoHaiDeng->module_facing, "back") == 0)
		facing[0] = 'b';
	else
		facing[0] = 'f';

	snprintf(sd->name, sizeof(sd->name), "m%02d_%s_%s %s",
		 saoHaiDeng->module_index, facing, DRIVER_NAME,
		 dev_name(sd->dev));
	ret = v4l2_async_register_subdev_sensor_common(sd);
	if (ret)
		goto error;

	dev_info(&client->dev, "%s sensor driver registered !!\n", sd->name);
	saoHaiDeng->power_on = false;
	return 0;

error:
	v4l2_ctrl_handler_free(&saoHaiDeng->ctrls);
#if defined(CONFIG_MEDIA_CONTROLLER)
	media_entity_cleanup(&sd->entity);
#endif
	mutex_destroy(&saoHaiDeng->lock);
	__saoHaiDeng_power_off(saoHaiDeng);
	return ret;
}

static int saoHaiDeng_remove(struct i2c_client *client)
{
	struct v4l2_subdev *sd = i2c_get_clientdata(client);
	struct saoHaiDeng *saoHaiDeng = to_saoHaiDeng(sd);

	v4l2_ctrl_handler_free(&saoHaiDeng->ctrls);
	v4l2_async_unregister_subdev(sd);
#if defined(CONFIG_MEDIA_CONTROLLER)
	media_entity_cleanup(&sd->entity);
#endif
	mutex_destroy(&saoHaiDeng->lock);

	__saoHaiDeng_power_off(saoHaiDeng);

	return 0;
}

static const struct i2c_device_id saoHaiDeng_id[] = {
	{ "saoHaiDeng", 0 },
	{ /* sentinel */ },
};
MODULE_DEVICE_TABLE(i2c, saoHaiDeng_id);

#if IS_ENABLED(CONFIG_OF)
static const struct of_device_id saoHaiDeng_of_match[] = {
	{
		.compatible = "htkt,saoHaiDeng",
	},
	{ /* sentinel */ },
};
MODULE_DEVICE_TABLE(of, saoHaiDeng_of_match);
#endif

static struct i2c_driver saoHaiDeng_i2c_driver = {
    .driver =
        {
            .name = DRIVER_NAME,
            .of_match_table = of_match_ptr(saoHaiDeng_of_match),
        },
    .probe = saoHaiDeng_probe,
    .remove = saoHaiDeng_remove,
    .id_table = saoHaiDeng_id,
};

static int __init sensor_mod_init(void)
{
	return i2c_add_driver(&saoHaiDeng_i2c_driver);
}

static void __exit sensor_mod_exit(void)
{
	i2c_del_driver(&saoHaiDeng_i2c_driver);
}

device_initcall_sync(sensor_mod_init);
module_exit(sensor_mod_exit);

MODULE_AUTHOR("cb");
MODULE_DESCRIPTION("saoHaiDeng CMOS Image Sensor driver");
MODULE_LICENSE("GPL v2");

放在OK3588-linux-source/kernel/drivers/media/i2c/saoHaiDeng.c

//************************************************
//***  CAM3 OV5645 Configuration Description  ****
//************************************************

&csi2_dphy0_hw {
    status = "okay";
};

&i2c7 {
    status = "okay";
    clock-frequency = <400000>;

    saohaideng: saohaideng@3c {
        compatible = "htkt,saoHaiDeng";
        status = "okay";
        reg = <0x3c>;

        clocks = <&ext_cam_clk>;
        clock-names = "xclk";
        clock-frequency = <24000000>;

        enable-gpios = <&extio EXTIO_GPIO_P05 GPIO_ACTIVE_HIGH>;
        reset-gpios = <&extio EXTIO_GPIO_P04 GPIO_ACTIVE_LOW>;
        rockchip,camera-module-index = <0>;
        rockchip,camera-module-facing = "back";
        rockchip,camera-module-name = "NC";
        rockchip,camera-module-lens-name = "NC";

        port {
            saohaideng_out: endpoint {
                remote-endpoint = <&mipi_in_ucam3>;
                data-lanes = <1 2 3 4>;
            };
        };
    };
};

&csi2_dphy0 {
    status = "okay";
    ports {
        #address-cells = <1>;
        #size-cells = <0>;
        port@0 {
            reg = <0>;
            #address-cells = <1>;
            #size-cells = <0>;
            mipi_in_ucam3: endpoint@1 {
                reg = <1>;
                remote-endpoint = <&saohaideng_out>;
                data-lanes = <1 2 3 4>;
            };
        };
        port@1 {
            reg = <1>;
            #address-cells = <1>;
            #size-cells = <0>;
            mipi2_csi2_mipicsi0_out0: endpoint@0 {
                reg = <0>;
                remote-endpoint = <&mipi2_csi2_input>;
            };
        };
    };
};

&mipi2_csi2 {
        status = "okay";
    ports {
        #address-cells = <1>;
        #size-cells = <0>;
        port@0 {
            reg = <0>;
            #address-cells = <1>;
            #size-cells = <0>;
            mipi2_csi2_input: endpoint@1 {
                reg = <1>;
                remote-endpoint = <&mipi2_csi2_mipicsi0_out0>;
            };
        };
        port@1 {
            reg = <1>;
            #address-cells = <1>;
            #size-cells = <0>;
            mipi2_csi2_output: endpoint@0 {
                reg = <0>;
                remote-endpoint = <&cif_mipi_lvds2>;
            };
        };
    };
};

&rkcif_mipi_lvds2 {
    status = "okay";
    port {
        cif_mipi_lvds2: endpoint {
            remote-endpoint = <&mipi2_csi2_output>;
        };
    };
};

&rkcif_mipi_lvds2_sditf {
    status = "disabled";
};

播放图像

ffplay -f v4l2 -input_format nv12 -video_size 1920x1080 -i /dev/video22
gst-launch-1.0 v4l2src device=/dev/video22 io-mode=2 ! videoconvert ! video/x-raw, format=NV12, width=1920, height=1080 ! autovideosink

<think>嗯,用户需要解决RK3588使用V4L2采集MIPI相机视频出现花屏的问题。这个问题涉及硬件、驱动和软件多个层面,需要系统性地排查。 用户可能正在调试嵌入式视觉系统,从引用[2]看用户应该是在RK3588开发板上调试CAM-8MS1M相机模组,之前还遇到过固件版本问题。用户现在遇到的是图像花屏这种比较直观的问题,但底层原因可能很复杂。 从技术角度看,花屏问题通常涉及几个关键点:首先是数据链路完整性,包括MIPI信号质量和时钟同步;其次是图像格式匹配,V4L2配置的像素格式必须与sensor输出一致;最后是内存对齐问题,特别是DMA缓冲区的处理。 引用[3]提供的V4L2工作流程特别重要,需要确认每个ioctl调用都正确执行。引用[4]提到的RK平台调试经验也很关键,强调要分层验证:先确认底层数据是否正常,再检查HAL层处理,最后看上层表现。 具体到排查步骤,应该先确认硬件连接稳定性,用示波器检查MIPI差分信号质量。然后检查驱动日志,重点看dmesg里CSI和ISP模块的报错。软件层面要核对V4L2的格式设置,特别是width/height/stride等参数是否与sensor的配置寄存器匹配。对于YUYV格式,内存对齐要求很严格,可以尝试改用16字节对齐的NV12格式测试。 用户之前提到重新烧写过固件,所以也要考虑新固件可能引入的兼容性问题。引用[2]提到固件版本过低会导致检测不到相机,但反过来新版固件也可能有新的bug。建议用户交叉验证:用v4l2-ctl工具dump原始帧数据,如果在RAW阶段就花屏说明问题在驱动层之前;如果RAW数据正常但应用层显示异常,就是格式转换的问题。</think>针对RK3588使用V4L2采集MIPI相机视频花屏问题,请按以下步骤排查: --- ### **一、核心排查步骤** 1. **确认硬件连接与供电** - 检查MIPI线缆是否插紧(CAM-8MS1M等模组对松动敏感) - 测量相机模组供电电压是否稳定(典型值1.8V/2.8V) - 使用示波器检测MIPI差分信号完整性(排除信号干扰) 2. **验证固件与驱动兼容性** ```bash dmesg | grep -i "csi\|mipi\|sensor" # 检查驱动加载日志 ``` - 确认内核版本支持相机模组(**必须使用2023年后更新的RK3588固件**,旧版本可能导致设备未识别[^2]) - 检查设备树配置:`/boot/dts/rockchip/rk3588-xxx.dts` 中确认: ```c &csi2_dphy0 { status = "okay"; ports { port@0 { csi_dphy_input: endpoint@0 { remote-endpoint = <&cam_out>; // 需与sensor节点匹配 }; }; }; }; ``` 3. **V4L2配置关键检查** - **像素格式对齐**:确保应用层设置的 `pixelformat` 与sensor输出一致(如 `V4L2_PIX_FMT_SBGGR10` 或 `V4L2_PIX_FMT_YUYV`) - **内存对齐处理**:添加跨步对齐(stride)补偿 ```c struct v4l2_format fmt = {0}; fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; ioctl(fd, VIDIOC_G_FMT, &fmt); // 先获取驱动建议参数 fmt.fmt.pix.width = 1920; // 手动设置需重新计算bytesperline fmt.fmt.pix.bytesperline = ALIGN(fmt.fmt.pix.width, 16); // 按16字节对齐 fmt.fmt.pix.sizeimage = fmt.fmt.pix.bytesperline * fmt.fmt.pix.height; ioctl(fd, VIDIOC_S_FMT, &fmt); // 应用配置 ``` 4. **DMA缓冲区验证** - 使用 `v4l2-ctl` 工具直接测试: ```bash v4l2-ctl --device /dev/video0 --set-fmt-video=width=1920,height=1080,pixelformat=YUYV v4l2-ctl --stream-mmap=3 --stream-count=10 --stream-to=frame.raw ``` - 用 `yuvplayer` 查看 `frame.raw`,若原始数据正常则问题在应用层处理 --- ### **二、进阶调试方案** 1. **启用内核调试日志** ```bash echo 7 > /proc/sys/kernel/printk # 提高日志级别 echo 1 > /sys/module/videobuf2_core/parameters/debug dmesg -w | grep "v4l2\|mipi" # 实时监控驱动消息 ``` 2. **检查时钟与帧率同步** - 验证sensor输出时钟与CSI接收配置: ```c struct v4l2_streamparm parm = {0}; parm.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; parm.parm.capture.timeperframe.numerator = 1; parm.parm.capture.timeperframe.denominator = 30; // 设置30fps ioctl(fd, VIDIOC_S_PARM, &parm); // 动态调整帧率[^1] ``` 3. **物理层信号质量测试** - 使用RK3588内置眼图工具(需内核支持): ```bash cat /sys/kernel/debug/csi2-dphy0/status # 查看信号误码率 ``` --- ### **三、典型解决方案** 1. **花屏伴随CRC错误** → 降低MIPI传输速率(修改设备树 `csi2_dphy` 节点的 `data-rate` 参数) 2. **固定位置条纹花屏** → 检查DMA内存对齐,确保 `bytesperline` 正确计算(如1080P YUYV格式需 `bytesperline=1920*2=3840`) 3. **随机色块花屏** → 关闭ISP后处理模块测试(部分固件ISP存在兼容问题): ```bash v4l2-ctl --set-ctrl=isp_af_mode=0 # 禁用自动对焦模块 ``` --- ### **四、引用验证** 1. 若更新固件后环境丢失,建议使用Docker部署RKNN环境[^2] 2. V4L2采集流程需严格遵循 `open→QUERYCAP→S_FMT→REQBUFS→mmap→STREAMON` 顺序[^3] 3. 分层调试法:先确认sensor输出RAW数据正常,再检查V4L2 HAL层处理[^4]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值