Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    3)  * sc210iot sensor driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    5)  * Copyright (C) 2020 Rockchip Electronics Co., Ltd.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    6)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    7)  * V0.0X01.0X00 first version.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    8)  * V0.0X01.0X01 add quick stream on/off
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    9)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   10) #include <linux/clk.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   11) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   12) #include <linux/gpio/consumer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   13) #include <linux/i2c.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   14) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   15) #include <linux/pm_runtime.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   16) #include <linux/of_graph.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   17) #include <linux/regmap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   18) #include <linux/pinctrl/consumer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   19) #include <linux/regulator/consumer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   20) #include <linux/version.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   21) #include <linux/rk-camera-module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   22) #include <linux/rk-preisp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   23) #include <media/v4l2-async.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   24) #include <media/media-entity.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   25) #include <media/v4l2-ctrls.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   26) #include <media/v4l2-device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   27) #include <media/v4l2-fwnode.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   28) #include <media/v4l2-subdev.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   29) #include "../platform/rockchip/isp/rkisp_tb_helper.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   30) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   31) #define DRIVER_VERSION		KERNEL_VERSION(0, 0x01, 0x01)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   32) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   33) #define OF_CAMERA_PINCTRL_STATE_DEFAULT	"rockchip,camera_default"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   34) #define OF_CAMERA_PINCTRL_STATE_SLEEP	"rockchip,camera_sleep"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   35) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   36) #define SC210IOT_NAME		"sc210iot"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   37) #define SC210IOT_MEDIA_BUS_FMT	MEDIA_BUS_FMT_SBGGR10_1X10
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   38) #define MIPI_FREQ		371250000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   39) #define SC210IOT_XVCLK_FREQ	27000000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   40) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   41) #define SC210IOT_REG_CHIP_ID_H	0x3108
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   42) #define SC210IOT_REG_CHIP_ID_L	0x3107
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   43) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   44) #define SC210IOT_REG_EXP_LONG_H	0x3e00
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   45) #define SC210IOT_REG_EXP_LONG_M	0x3e01
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   46) #define SC210IOT_REG_EXP_LONG_L	0x3e02
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   47) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   48) #define SC210IOT_REG_GAIN_LONG_3	0x3e06
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   49) #define SC210IOT_REG_GAIN_LONG_2	0x3e07
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   50) #define SC210IOT_REG_GAIN_LONG_1	0x3e08
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   51) #define SC210IOT_REG_GAIN_LONG_0	0x3e09
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   52) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   53) #define SC210IOT_REG_MIRROR_FLIP	0x3221
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   54) #define MIRROR_MASK		0x6
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   55) #define FLIP_MASK		0x60
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   56) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   57) #define SC210IOT_REG_CTRL_MODE	0x0100
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   58) #define SC210IOT_MODE_SW_STANDBY	0x0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   59) #define SC210IOT_MODE_STREAMING	BIT(0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   60) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   61) #define SC210IOT_CHIP_ID		0x17cb
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   62) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   63) #define SC210IOT_REG_VTS_H	0x320e
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   64) #define SC210IOT_REG_VTS_L	0x320f
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   65) #define SC210IOT_VTS_MAX		0x3FFF
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   66) #define SC210IOT_HTS_MAX		0xFFF
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   67) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   68) #define SC210IOT_EXPOSURE_MAX	0x3FFF
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   69) #define SC210IOT_EXPOSURE_MIN	1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   70) #define SC210IOT_EXPOSURE_STEP	1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   71) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   72) #define SC210IOT_GAIN_MIN		0x40
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   73) #define SC210IOT_GAIN_MAX		0x8000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   74) #define SC210IOT_GAIN_STEP		1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   75) #define SC210IOT_GAIN_DEFAULT	64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   76) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   77) #define SC210IOT_SOFTWARE_RESET_REG	0x0103
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   78) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   79) #define SC210IOT_LANES		2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   80) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   81) static const char * const sc210iot_supply_names[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   82) 	"dovdd",    /* Digital I/O power */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   83) 	"avdd",     /* Analog power */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   84) 	"dvdd",     /* Digital power */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   85) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   86) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   87) #define SC210IOT_NUM_SUPPLIES ARRAY_SIZE(sc210iot_supply_names)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   88) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   89) #define to_sc210iot(sd) container_of(sd, struct sc210iot, subdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   90) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   91) enum {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   92) 	LINK_FREQ_INDEX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   93) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   94) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   95) struct gain_section {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   96) 	u16 min_gain;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   97) 	u16 max_gain;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   98) 	u16 again_regs_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   99) 	u16 again_regs_stop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  100) 	u16 again_deviation;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  101) 	u16 dgain_regs_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  102) 	u16 dgain_regs_stop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  103) 	u16 dgain_deviation;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  104) 	u16 steps;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  105) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  106) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  107) struct sc210iot_mode {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  108) 	u32 width;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  109) 	u32 height;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  110) 	struct v4l2_fract max_fps;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  111) 	u32 hts_def;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  112) 	u32 vts_def;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  113) 	u32 exp_def;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  114) 	u32 link_freq_index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  115) 	const struct reg_sequence *reg_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  116) 	u32 reg_num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  117) 	u32 hdr_mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  118) 	u32 vc[PAD_MAX];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  119) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  121) struct sc210iot {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  122) 	struct device	*dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  123) 	struct clk	*xvclk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  124) 	struct regmap	*regmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  125) 	struct gpio_desc *reset_gpio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  126) 	struct gpio_desc *pwdn_gpio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  127) 	struct regulator_bulk_data supplies[SC210IOT_NUM_SUPPLIES];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  128) 	struct pinctrl		*pinctrl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  129) 	struct pinctrl_state	*pins_default;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  130) 	struct pinctrl_state	*pins_sleep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  131) 	struct v4l2_subdev  subdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  132) 	struct media_pad    pad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  133) 	struct v4l2_ctrl_handler ctrl_handler;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  134) 	struct v4l2_ctrl    *exposure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  135) 	struct v4l2_ctrl    *anal_gain;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  136) 	struct v4l2_ctrl    *hblank;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  137) 	struct v4l2_ctrl    *vblank;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  138) 	struct v4l2_ctrl    *h_flip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  139) 	struct v4l2_ctrl    *v_flip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  140) 	struct v4l2_ctrl    *link_freq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  141) 	struct v4l2_ctrl    *pixel_rate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  142) 	struct mutex        lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  143) 	struct v4l2_fract cur_fps;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  144) 	u32		cur_vts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  145) 	bool		streaming;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  146) 	bool		power_on;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  147) 	bool		is_thunderboot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  148) 	bool		is_thunderboot_ng;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  149) 	bool		is_first_streamoff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  150) 	unsigned int	cfg_num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  151) 	const struct sc210iot_mode *cur_mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  152) 	u32		module_index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  153) 	const char      *module_facing;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  154) 	const char      *module_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  155) 	const char      *len_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  156) 	bool		has_init_exp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  157) 	struct preisp_hdrae_exp_s init_hdrae_exp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  158) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  159) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  160) static const struct regmap_config sc210iot_regmap_config = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  161) 	.reg_bits = 16,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  162) 	.val_bits = 8,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  163) 	.max_register = 0x6f00,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  164) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  165) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  166) static const s64 link_freq_menu_items[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  167) 	MIPI_FREQ,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  168) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  169) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  170) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  171)  * window size=1920*1080 mipi@2lane
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  172)  * mclk=27M mipi_clk=371.25Mbps
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  173)  * pixel_line_total=2200 line_frame_total=1125
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  174)  * row_time=29.62us frame_rate=30fps
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  175)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  176) static const struct reg_sequence sc210iot_1080p_liner_30fps_settings[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  177) 	{0x0103, 0x01},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  178) 	{0x0100, 0x00},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  179) 	{0x36e9, 0x80},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  180) 	{0x36f9, 0x80},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  181) 	{0x301c, 0x78},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  182) 	{0x3208, 0x07},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  183) 	{0x3209, 0x80},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  184) 	{0x320a, 0x04},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  185) 	{0x320b, 0x38},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  186) 	{0x320e, 0x04},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  187) 	{0x320f, 0x65},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  188) 	{0x3214, 0x11},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  189) 	{0x3215, 0x11},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  190) 	{0x3253, 0x0c},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  191) 	{0x3274, 0x09},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  192) 	{0x3301, 0x05},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  193) 	{0x3304, 0x68},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  194) 	{0x3306, 0x40},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  195) 	{0x330b, 0xcc},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  196) 	{0x331c, 0x01},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  197) 	{0x331e, 0x61},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  198) 	{0x3333, 0x10},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  199) 	{0x3364, 0x17},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  200) 	{0x3391, 0x18},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  201) 	{0x3392, 0x38},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  202) 	{0x3393, 0x08},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  203) 	{0x3394, 0x0b},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  204) 	{0x3395, 0x50},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  205) 	{0x3620, 0x88},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  206) 	{0x3622, 0x06},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  207) 	{0x3630, 0xf8},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  208) 	{0x3634, 0x44},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  209) 	{0x3637, 0x16},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  210) 	{0x363a, 0x1f},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  211) 	{0x3670, 0x1c},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  212) 	{0x3677, 0x84},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  213) 	{0x3678, 0x86},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  214) 	{0x3679, 0x8b},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  215) 	{0x367e, 0x18},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  216) 	{0x367f, 0x38},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  217) 	{0x3690, 0x53},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  218) 	{0x3691, 0x63},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  219) 	{0x3692, 0x63},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  220) 	{0x369c, 0x08},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  221) 	{0x369d, 0x38},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  222) 	{0x36a4, 0x08},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  223) 	{0x36a5, 0x18},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  224) 	{0x36a8, 0x08},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  225) 	{0x36a9, 0x28},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  226) 	{0x36aa, 0x2a},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  227) 	{0x36fc, 0x11},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  228) 	{0x36fd, 0x14},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  229) 	{0x3e01, 0x8c},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  230) 	{0x3e03, 0x0b},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  231) 	{0x3e08, 0x03},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  232) 	{0x3e09, 0x20},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  233) 	{0x3e1b, 0x15},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  234) 	{0x3f03, 0x01},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  235) 	{0x36e9, 0x20},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  236) 	{0x36f9, 0x24},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  237) 	{0x0100, 0x01},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  238) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  239) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  240) static const struct sc210iot_mode supported_modes[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  241) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  242) 		.width = 1920,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  243) 		.height = 1080,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  244) 		.max_fps = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  245) 			.numerator = 10000,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  246) 			.denominator = 300000,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  247) 		},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  248) 		.exp_def = 0x460,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  249) 		.hts_def = 0x898,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  250) 		.vts_def = 0x465,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  251) 		.link_freq_index = LINK_FREQ_INDEX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  252) 		.reg_list = sc210iot_1080p_liner_30fps_settings,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  253) 		.reg_num = ARRAY_SIZE(sc210iot_1080p_liner_30fps_settings),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  254) 		.hdr_mode = NO_HDR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  255) 		.vc[PAD0] = V4L2_MBUS_CSI2_CHANNEL_0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  256) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  257) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  258) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  259) /* pixel rate = link frequency * 2 * lanes / BITS_PER_SAMPLE */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  260) static u64 to_pixel_rate(u32 index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  261) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  262) 	u64 pixel_rate = link_freq_menu_items[index] * 2 * SC210IOT_LANES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  263) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  264) 	do_div(pixel_rate, 10);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  265) 	return pixel_rate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  266) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  267) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  268) static inline int sc210iot_read_reg(struct sc210iot *sc210iot, u16 addr, u8 *value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  269) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  270) 	unsigned int val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  271) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  272) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  273) 	ret = regmap_read(sc210iot->regmap, addr, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  274) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  275) 		dev_err(sc210iot->dev, "i2c read failed at addr: %x\n", addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  276) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  277) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  278) 	*value = val & 0xff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  279) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  280) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  281) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  282) static int __sc210iot_power_on(struct sc210iot *sc210iot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  283) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  284) static inline int sc210iot_write_reg(struct sc210iot *sc210iot, u16 addr, u8 value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  285) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  286) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  287) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  288) 	ret = regmap_write(sc210iot->regmap, addr, value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  289) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  290) 		dev_err(sc210iot->dev, "i2c write failed at addr: %x\n", addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  291) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  292) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  293) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  294) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  295) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  296) static const struct gain_section gain_sections[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  297) 	{    64,   128, 0x0320, 0x033f,  64, 0x0080, 0x0080,     0, 32 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  298) 	{   128,   256, 0x0720, 0x073f, 128, 0x0080, 0x0080,     0, 32 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  299) 	{   256,   512, 0x0f20, 0x0f3f, 256, 0x0080, 0x0080,     0, 32 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  300) 	{   512,  1024, 0x1f20, 0x1f3f, 512, 0x0080, 0x0080,     0, 32 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  301) 	{  1024,  2048, 0x1f3f, 0x1f3f,   0, 0x0080, 0x00fc,  1024, 32 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  302) 	{  2048,  4096, 0x1f3f, 0x1f3f,   0, 0x0180, 0x01fc,  2048, 32 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  303) 	{  4096,  8192, 0x1f3f, 0x1f3f,   0, 0x0380, 0x03fc,  4096, 32 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  304) 	{  8192, 16384, 0x1f3f, 0x1f3f,   0, 0x0780, 0x07fc,  8192, 32 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  305) 	{ 16384, 32768, 0x1f3f, 0x1f3f,   0, 0x0f80, 0x0ffc, 16384, 32 }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  306) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  307) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  308) static int sc210iot_set_gain(struct sc210iot *sc210iot, u32 gain)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  309) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  310) 	int ret, i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  311) 	int offset, step, step_len, reg_step_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  312) 	int a_gain = 0, d_gain = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  313) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  314) 	dev_dbg(sc210iot->dev, "%s: gain : %d\n", __func__, gain);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  315) 	if (sc210iot->is_thunderboot && rkisp_tb_get_state() == RKISP_TB_NG) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  316) 		sc210iot->is_thunderboot = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  317) 		sc210iot->is_thunderboot_ng = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  318) 		__sc210iot_power_on(sc210iot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  319) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  320) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  321) 	for (i = 0; i < ARRAY_SIZE(gain_sections) - 1; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  322) 		if ((gain_sections[i].min_gain <= gain) && (gain < gain_sections[i].max_gain))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  323) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  324) 	if (gain_sections[i].again_deviation) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  325) 		offset = gain - gain_sections[i].min_gain;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  326) 		step_len = gain_sections[i].again_deviation / gain_sections[i].steps;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  327) 		reg_step_len = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  328) 		step = offset / step_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  329) 		a_gain = gain_sections[i].again_regs_start + step * reg_step_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  330) 		d_gain = gain_sections[i].dgain_regs_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  331) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  332) 		offset = gain - gain_sections[i].min_gain;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  333) 		step_len = gain_sections[i].dgain_deviation / gain_sections[i].steps;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  334) 		step = offset / step_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  335) 		reg_step_len = 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  336) 		a_gain = gain_sections[i].again_regs_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  337) 		d_gain = gain_sections[i].dgain_regs_start + step * reg_step_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  338) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  339) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  340) 	if (a_gain > gain_sections[i].again_regs_stop)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  341) 		a_gain = gain_sections[i].again_regs_stop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  342) 	if (d_gain > gain_sections[i].dgain_regs_stop)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  343) 		d_gain = gain_sections[i].dgain_regs_stop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  344) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  345) 	dev_dbg(sc210iot->dev, "%s: a_gain: 0x%x d_gain: 0x%x\n", __func__, a_gain, d_gain);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  346) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  347) 	ret  = sc210iot_write_reg(sc210iot, SC210IOT_REG_GAIN_LONG_1, a_gain >> 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  348) 	ret |= sc210iot_write_reg(sc210iot, SC210IOT_REG_GAIN_LONG_0, a_gain & 0xff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  349) 	ret |= sc210iot_write_reg(sc210iot, SC210IOT_REG_GAIN_LONG_3, d_gain >> 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  350) 	ret |= sc210iot_write_reg(sc210iot, SC210IOT_REG_GAIN_LONG_2, d_gain & 0xff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  351) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  352) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  353) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  354) static int sc210iot_set_exp(struct sc210iot *sc210iot, u32 exp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  355) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  356) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  357) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  358) 	dev_dbg(sc210iot->dev, "%s: exp : %d\n", __func__, exp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  359) 	ret  = sc210iot_write_reg(sc210iot, SC210IOT_REG_EXP_LONG_H,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  360) 					(exp >> 12) & 0xf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  361) 	ret |= sc210iot_write_reg(sc210iot, SC210IOT_REG_EXP_LONG_M,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  362) 					(exp >> 4) & 0xff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  363) 	ret |= sc210iot_write_reg(sc210iot, SC210IOT_REG_EXP_LONG_L,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  364) 					(exp & 0xf) << 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  365) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  366) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  367) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  368) static void sc210iot_modify_fps_info(struct sc210iot *sc210iot)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  369) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  370) 	const struct sc210iot_mode *mode = sc210iot->cur_mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  371) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  372) 	sc210iot->cur_fps.denominator = mode->max_fps.denominator * mode->vts_def /
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  373) 					sc210iot->cur_vts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  374) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  375) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  376) static int sc210iot_set_ctrl(struct v4l2_ctrl *ctrl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  377) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  378) 	struct sc210iot *sc210iot = container_of(ctrl->handler,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  379) 					     struct sc210iot, ctrl_handler);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  380) 	s64 max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  381) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  382) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  383) 	/* Propagate change of current control to all related controls */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  384) 	switch (ctrl->id) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  385) 	case V4L2_CID_VBLANK:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  386) 		/* Update max exposure while meeting expected vblanking */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  387) 		max = sc210iot->cur_mode->height + ctrl->val - 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  388) 		__v4l2_ctrl_modify_range(sc210iot->exposure,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  389) 					 sc210iot->exposure->minimum, max,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  390) 					 sc210iot->exposure->step,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  391) 					 sc210iot->exposure->default_value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  392) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  393) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  394) 	if (!pm_runtime_get_if_in_use(sc210iot->dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  395) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  396) 	switch (ctrl->id) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  397) 	case V4L2_CID_EXPOSURE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  398) 		ret = sc210iot_set_exp(sc210iot, ctrl->val << 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  399) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  400) 	case V4L2_CID_ANALOGUE_GAIN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  401) 		ret = sc210iot_set_gain(sc210iot, ctrl->val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  402) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  403) 	case V4L2_CID_VBLANK:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  404) 		dev_dbg(sc210iot->dev, "set vblank 0x%x\n", ctrl->val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  405) 		ret = sc210iot_write_reg(sc210iot, SC210IOT_REG_VTS_H,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  406) 					(ctrl->val + sc210iot->cur_mode->height) >> 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  407) 		ret |= sc210iot_write_reg(sc210iot, SC210IOT_REG_VTS_L,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  408) 					(ctrl->val + sc210iot->cur_mode->height) & 0xff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  409) 		if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  410) 			sc210iot->cur_vts = ctrl->val + sc210iot->cur_mode->height;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  411) 		if (sc210iot->cur_vts != sc210iot->cur_mode->vts_def)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  412) 			sc210iot_modify_fps_info(sc210iot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  413) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  414) 	case V4L2_CID_HFLIP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  415) 		regmap_update_bits(sc210iot->regmap, SC210IOT_REG_MIRROR_FLIP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  416) 				   MIRROR_MASK, ctrl->val ? MIRROR_MASK : 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  417) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  418) 	case V4L2_CID_VFLIP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  419) 		regmap_update_bits(sc210iot->regmap, SC210IOT_REG_MIRROR_FLIP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  420) 				   FLIP_MASK,  ctrl->val ? FLIP_MASK : 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  421) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  422) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  423) 		dev_warn(sc210iot->dev, "%s Unhandled id:0x%x, val:0x%x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  424) 			 __func__, ctrl->id, ctrl->val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  425) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  426) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  427) 	pm_runtime_put(sc210iot->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  428) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  429) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  430) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  431) static const struct v4l2_ctrl_ops sc210iot_ctrl_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  432) 	.s_ctrl = sc210iot_set_ctrl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  433) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  434) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  435) static int sc210iot_get_regulators(struct sc210iot *sc210iot)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  436) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  437) 	unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  438) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  439) 	for (i = 0; i < SC210IOT_NUM_SUPPLIES; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  440) 		sc210iot->supplies[i].supply = sc210iot_supply_names[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  441) 	return devm_regulator_bulk_get(sc210iot->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  442) 				       SC210IOT_NUM_SUPPLIES,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  443) 				       sc210iot->supplies);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  444) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  445) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  446) static int sc210iot_initialize_controls(struct sc210iot *sc210iot)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  447) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  448) 	const struct sc210iot_mode *mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  449) 	struct v4l2_ctrl_handler *handler;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  450) 	s64 exposure_max, vblank_def;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  451) 	u32 h_blank;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  452) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  453) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  454) 	handler = &sc210iot->ctrl_handler;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  455) 	mode = sc210iot->cur_mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  456) 	ret = v4l2_ctrl_handler_init(handler, 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  457) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  458) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  459) 	handler->lock = &sc210iot->lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  460) 	sc210iot->link_freq = v4l2_ctrl_new_int_menu(handler, NULL, V4L2_CID_LINK_FREQ,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  461) 						  ARRAY_SIZE(link_freq_menu_items) - 1, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  462) 						  link_freq_menu_items);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  463) 	sc210iot->pixel_rate = v4l2_ctrl_new_std(handler, NULL, V4L2_CID_PIXEL_RATE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  464) 					      0, to_pixel_rate(LINK_FREQ_INDEX),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  465) 					      1, to_pixel_rate(LINK_FREQ_INDEX));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  466) 	h_blank = mode->hts_def - mode->width;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  467) 	sc210iot->hblank = v4l2_ctrl_new_std(handler, NULL, V4L2_CID_HBLANK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  468) 					  h_blank, h_blank, 1, h_blank);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  469) 	if (sc210iot->hblank)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  470) 		sc210iot->hblank->flags |= V4L2_CTRL_FLAG_READ_ONLY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  471) 	vblank_def = mode->vts_def - mode->height;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  472) 	sc210iot->vblank = v4l2_ctrl_new_std(handler, &sc210iot_ctrl_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  473) 					  V4L2_CID_VBLANK, vblank_def,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  474) 					  SC210IOT_VTS_MAX - mode->height,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  475) 					  1, vblank_def);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  476) 	exposure_max = mode->vts_def - 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  477) 	sc210iot->exposure = v4l2_ctrl_new_std(handler, &sc210iot_ctrl_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  478) 					    V4L2_CID_EXPOSURE, SC210IOT_EXPOSURE_MIN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  479) 					    exposure_max, SC210IOT_EXPOSURE_STEP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  480) 					    mode->exp_def);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  481) 	sc210iot->anal_gain = v4l2_ctrl_new_std(handler, &sc210iot_ctrl_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  482) 					     V4L2_CID_ANALOGUE_GAIN, SC210IOT_GAIN_MIN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  483) 					     SC210IOT_GAIN_MAX, SC210IOT_GAIN_STEP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  484) 					     SC210IOT_GAIN_DEFAULT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  485) 	sc210iot->h_flip = v4l2_ctrl_new_std(handler, &sc210iot_ctrl_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  486) 					  V4L2_CID_HFLIP, 0, 1, 1, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  487) 	sc210iot->v_flip = v4l2_ctrl_new_std(handler, &sc210iot_ctrl_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  488) 					  V4L2_CID_VFLIP, 0, 1, 1, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  489) 	if (handler->error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  490) 		ret = handler->error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  491) 		dev_err(sc210iot->dev, "Failed to init controls(%d)\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  492) 		goto err_free_handler;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  493) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  494) 	sc210iot->subdev.ctrl_handler = handler;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  495) 	sc210iot->has_init_exp = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  496) 	sc210iot->cur_fps = mode->max_fps;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  497) 	sc210iot->cur_vts = mode->vts_def;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  498) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  499) err_free_handler:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  500) 	v4l2_ctrl_handler_free(handler);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  501) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  502) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  503) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  504) static int __sc210iot_power_on(struct sc210iot *sc210iot)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  505) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  506) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  507) 	struct device *dev = sc210iot->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  508) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  509) 	if (sc210iot->is_thunderboot)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  510) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  511) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  512) 	if (!IS_ERR_OR_NULL(sc210iot->pins_default)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  513) 		ret = pinctrl_select_state(sc210iot->pinctrl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  514) 					   sc210iot->pins_default);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  515) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  516) 			dev_err(dev, "could not set pins\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  517) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  518) 	ret = clk_set_rate(sc210iot->xvclk, SC210IOT_XVCLK_FREQ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  519) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  520) 		dev_warn(dev, "Failed to set xvclk rate\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  521) 	if (clk_get_rate(sc210iot->xvclk) != SC210IOT_XVCLK_FREQ)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  522) 		dev_warn(dev, "xvclk mismatched, modes are based on 27MHz\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  523) 	ret = clk_prepare_enable(sc210iot->xvclk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  524) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  525) 		dev_err(dev, "Failed to enable xvclk\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  526) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  527) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  528) 	ret = regulator_bulk_enable(SC210IOT_NUM_SUPPLIES, sc210iot->supplies);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  529) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  530) 		dev_err(dev, "Failed to enable regulators\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  531) 		goto disable_clk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  532) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  533) 	if (!IS_ERR(sc210iot->reset_gpio))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  534) 		gpiod_direction_output(sc210iot->reset_gpio, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  535) 	usleep_range(1000, 2000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  536) 	if (!IS_ERR(sc210iot->pwdn_gpio))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  537) 		gpiod_direction_output(sc210iot->pwdn_gpio, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  538) 	if (!IS_ERR(sc210iot->reset_gpio))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  539) 		gpiod_direction_output(sc210iot->reset_gpio, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  540) 	usleep_range(10000, 20000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  541) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  542) disable_clk:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  543) 	clk_disable_unprepare(sc210iot->xvclk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  544) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  545) 	if (!IS_ERR_OR_NULL(sc210iot->pins_sleep))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  546) 		pinctrl_select_state(sc210iot->pinctrl, sc210iot->pins_sleep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  547) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  548) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  549) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  550) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  551) static void __sc210iot_power_off(struct sc210iot *sc210iot)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  552) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  553) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  554) 	struct device *dev = sc210iot->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  555) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  556) 	if (sc210iot->is_thunderboot) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  557) 		if (sc210iot->is_first_streamoff) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  558) 			sc210iot->is_thunderboot = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  559) 			sc210iot->is_first_streamoff = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  560) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  561) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  562) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  563) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  564) 	if (!IS_ERR_OR_NULL(sc210iot->pins_sleep)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  565) 		ret = pinctrl_select_state(sc210iot->pinctrl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  566) 					   sc210iot->pins_sleep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  567) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  568) 			dev_dbg(dev, "could not set pins\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  569) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  570) 	if (!IS_ERR(sc210iot->reset_gpio))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  571) 		gpiod_direction_output(sc210iot->reset_gpio, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  572) 	if (!IS_ERR(sc210iot->pwdn_gpio))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  573) 		gpiod_direction_output(sc210iot->pwdn_gpio, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  574) 	if (sc210iot->is_thunderboot_ng) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  575) 		sc210iot->is_thunderboot_ng = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  576) 		regulator_bulk_disable(SC210IOT_NUM_SUPPLIES, sc210iot->supplies);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  577) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  578) 	clk_disable_unprepare(sc210iot->xvclk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  579) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  580) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  581) static int sc210iot_check_sensor_id(struct sc210iot *sc210iot)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  582) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  583) 	u8 id_h = 0, id_l = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  584) 	u16 id = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  585) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  586) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  587) 	if (sc210iot->is_thunderboot) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  588) 		dev_info(sc210iot->dev, "Enable thunderboot mode, skip sensor id check\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  589) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  590) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  591) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  592) 	ret = sc210iot_read_reg(sc210iot, SC210IOT_REG_CHIP_ID_H, &id_h);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  593) 	ret |= sc210iot_read_reg(sc210iot, SC210IOT_REG_CHIP_ID_L, &id_l);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  594) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  595) 		dev_err(sc210iot->dev, "Failed to read sensor id, (%d)\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  596) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  597) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  598) 	id = id_h << 8 | id_l;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  599) 	if (id != SC210IOT_CHIP_ID) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  600) 		dev_err(sc210iot->dev, "sensor id: %04X mismatched\n", id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  601) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  602) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  603) 	dev_info(sc210iot->dev, "Detected SC210IOT sensor\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  604) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  605) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  606) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  607) static void sc210iot_get_module_inf(struct sc210iot *sc210iot,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  608) 				  struct rkmodule_inf *inf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  609) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  610) 	memset(inf, 0, sizeof(*inf));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  611) 	strlcpy(inf->base.lens, sc210iot->len_name, sizeof(inf->base.lens));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  612) 	strlcpy(inf->base.sensor, SC210IOT_NAME, sizeof(inf->base.sensor));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  613) 	strlcpy(inf->base.module, sc210iot->module_name, sizeof(inf->base.module));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  614) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  615) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  616) static long sc210iot_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  617) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  618) 	struct sc210iot *sc210iot = to_sc210iot(sd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  619) 	struct rkmodule_hdr_cfg *hdr_cfg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  620) 	long ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  621) 	u32 stream = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  622) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  623) 	switch (cmd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  624) 	case RKMODULE_GET_HDR_CFG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  625) 		hdr_cfg = (struct rkmodule_hdr_cfg *)arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  626) 		hdr_cfg->esp.mode = HDR_NORMAL_VC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  627) 		hdr_cfg->hdr_mode = sc210iot->cur_mode->hdr_mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  628) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  629) 	case RKMODULE_GET_MODULE_INFO:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  630) 		sc210iot_get_module_inf(sc210iot, (struct rkmodule_inf *)arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  631) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  632) 	case RKMODULE_SET_HDR_CFG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  633) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  634) 	case RKMODULE_SET_QUICK_STREAM:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  635) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  636) 		stream = *((u32 *)arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  637) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  638) 		if (stream)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  639) 			ret = sc210iot_write_reg(sc210iot,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  640) 						 SC210IOT_REG_CTRL_MODE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  641) 						 SC210IOT_MODE_STREAMING);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  642) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  643) 			ret = sc210iot_write_reg(sc210iot,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  644) 						 SC210IOT_REG_CTRL_MODE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  645) 						 SC210IOT_MODE_SW_STANDBY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  646) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  647) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  648) 		ret = -ENOIOCTLCMD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  649) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  650) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  651) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  652) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  653) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  654) static int __sc210iot_start_stream(struct sc210iot *sc210iot)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  655) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  656) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  657) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  658) 	if (!sc210iot->is_thunderboot) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  659) 		ret = regmap_multi_reg_write(sc210iot->regmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  660) 					     sc210iot->cur_mode->reg_list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  661) 					     sc210iot->cur_mode->reg_num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  662) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  663) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  664) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  665) 	__v4l2_ctrl_handler_setup(&sc210iot->ctrl_handler);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  666) 	return sc210iot_write_reg(sc210iot,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  667) 				  SC210IOT_REG_CTRL_MODE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  668) 				  SC210IOT_MODE_STREAMING);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  669) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  670) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  671) static int __sc210iot_stop_stream(struct sc210iot *sc210iot)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  672) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  673) 	sc210iot->has_init_exp = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  674) 	if (sc210iot->is_thunderboot)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  675) 		sc210iot->is_first_streamoff = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  676) 	return sc210iot_write_reg(sc210iot, SC210IOT_REG_CTRL_MODE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  677) 				  SC210IOT_MODE_SW_STANDBY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  678) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  679) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  680) #ifdef CONFIG_COMPAT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  681) static long sc210iot_compat_ioctl32(struct v4l2_subdev *sd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  682) 				  unsigned int cmd, unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  683) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  684) 	void __user *up = compat_ptr(arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  685) 	struct rkmodule_inf *inf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  686) 	struct rkmodule_hdr_cfg *hdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  687) 	long ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  688) 	u32 stream = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  689) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  690) 	switch (cmd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  691) 	case RKMODULE_GET_MODULE_INFO:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  692) 		inf = kzalloc(sizeof(*inf), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  693) 		if (!inf) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  694) 			ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  695) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  696) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  697) 		ret = sc210iot_ioctl(sd, cmd, inf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  698) 		if (!ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  699) 			ret = copy_to_user(up, inf, sizeof(*inf));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  700) 			if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  701) 				ret = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  702) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  703) 		kfree(inf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  704) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  705) 	case RKMODULE_GET_HDR_CFG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  706) 		hdr = kzalloc(sizeof(*hdr), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  707) 		if (!hdr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  708) 			ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  709) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  710) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  711) 		ret = sc210iot_ioctl(sd, cmd, hdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  712) 		if (!ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  713) 			ret = copy_to_user(up, hdr, sizeof(*hdr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  714) 			if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  715) 				ret = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  716) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  717) 		kfree(hdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  718) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  719) 	case RKMODULE_SET_QUICK_STREAM:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  720) 		ret = copy_from_user(&stream, up, sizeof(u32));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  721) 		if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  722) 			ret = sc210iot_ioctl(sd, cmd, &stream);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  723) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  724) 			ret = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  725) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  726) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  727) 		ret = -ENOIOCTLCMD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  728) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  729) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  730) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  731) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  732) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  733) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  734) static int sc210iot_s_stream(struct v4l2_subdev *sd, int on)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  735) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  736) 	struct sc210iot *sc210iot = to_sc210iot(sd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  737) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  738) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  739) 	mutex_lock(&sc210iot->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  740) 	on = !!on;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  741) 	if (on == sc210iot->streaming)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  742) 		goto unlock_and_return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  743) 	if (on) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  744) 		if (sc210iot->is_thunderboot && rkisp_tb_get_state() == RKISP_TB_NG) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  745) 			sc210iot->is_thunderboot = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  746) 			__sc210iot_power_on(sc210iot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  747) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  748) 		ret = pm_runtime_get_sync(sc210iot->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  749) 		if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  750) 			pm_runtime_put_noidle(sc210iot->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  751) 			goto unlock_and_return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  752) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  753) 		ret = __sc210iot_start_stream(sc210iot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  754) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  755) 			dev_err(sc210iot->dev, "Failed to start sc210iot stream\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  756) 			pm_runtime_put(sc210iot->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  757) 			goto unlock_and_return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  758) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  759) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  760) 		__sc210iot_stop_stream(sc210iot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  761) 		pm_runtime_put(sc210iot->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  762) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  763) 	sc210iot->streaming = on;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  764) unlock_and_return:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  765) 	mutex_unlock(&sc210iot->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  766) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  767) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  768) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  769) static int sc210iot_g_frame_interval(struct v4l2_subdev *sd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  770) 				   struct v4l2_subdev_frame_interval *fi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  771) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  772) 	struct sc210iot *sc210iot = to_sc210iot(sd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  773) 	const struct sc210iot_mode *mode = sc210iot->cur_mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  774) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  775) 	if (sc210iot->streaming)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  776) 		fi->interval = sc210iot->cur_fps;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  777) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  778) 		fi->interval = mode->max_fps;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  779) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  780) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  781) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  782) static int sc210iot_g_mbus_config(struct v4l2_subdev *sd, unsigned int pad_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  783) 				struct v4l2_mbus_config *config)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  784) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  785) 	struct sc210iot *sc210iot = to_sc210iot(sd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  786) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  787) 	u32 val = 1 << (SC210IOT_LANES - 1) | V4L2_MBUS_CSI2_CHANNEL_0 |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  788) 		  V4L2_MBUS_CSI2_CONTINUOUS_CLOCK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  789) 	config->type = V4L2_MBUS_CSI2_DPHY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  790) 	config->flags = (sc210iot->cur_mode->hdr_mode == NO_HDR) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  791) 			val : (val | V4L2_MBUS_CSI2_CHANNEL_1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  792) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  793) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  794) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  795) static int sc210iot_enum_mbus_code(struct v4l2_subdev *sd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  796) 				 struct v4l2_subdev_pad_config *cfg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  797) 				 struct v4l2_subdev_mbus_code_enum *code)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  798) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  799) 	if (code->index != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  800) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  801) 	code->code = SC210IOT_MEDIA_BUS_FMT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  802) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  803) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  804) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  805) static int sc210iot_enum_frame_sizes(struct v4l2_subdev *sd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  806) 				   struct v4l2_subdev_pad_config *cfg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  807) 				   struct v4l2_subdev_frame_size_enum *fse)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  808) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  809) 	struct sc210iot *sc210iot = to_sc210iot(sd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  810) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  811) 	if (fse->index >= sc210iot->cfg_num)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  812) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  813) 	if (fse->code != SC210IOT_MEDIA_BUS_FMT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  814) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  815) 	fse->min_width  = supported_modes[fse->index].width;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  816) 	fse->max_width  = supported_modes[fse->index].width;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  817) 	fse->max_height = supported_modes[fse->index].height;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  818) 	fse->min_height = supported_modes[fse->index].height;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  819) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  820) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  821) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  822) static int sc210iot_enum_frame_interval(struct v4l2_subdev *sd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  823) 						  struct v4l2_subdev_pad_config *cfg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  824) 						  struct v4l2_subdev_frame_interval_enum *fie)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  825) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  826) 	struct sc210iot *sc210iot = to_sc210iot(sd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  827) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  828) 	if (fie->index >= sc210iot->cfg_num)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  829) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  830) 	fie->code = SC210IOT_MEDIA_BUS_FMT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  831) 	fie->width = supported_modes[fie->index].width;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  832) 	fie->height = supported_modes[fie->index].height;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  833) 	fie->interval = supported_modes[fie->index].max_fps;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  834) 	fie->reserved[0] = supported_modes[fie->index].hdr_mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  835) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  836) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  837) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  838) static int sc210iot_set_fmt(struct v4l2_subdev *sd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  839) 			  struct v4l2_subdev_pad_config *cfg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  840) 			  struct v4l2_subdev_format *fmt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  841) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  842) 	struct sc210iot *sc210iot = to_sc210iot(sd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  843) 	const struct sc210iot_mode *mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  844) 	s64 h_blank, vblank_def;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  845) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  846) 	mutex_lock(&sc210iot->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  847) 	mode = v4l2_find_nearest_size(supported_modes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  848) 				      ARRAY_SIZE(supported_modes),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  849) 				      width, height,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  850) 				      fmt->format.width, fmt->format.height);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  851) 	fmt->format.code = SC210IOT_MEDIA_BUS_FMT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  852) 	fmt->format.width = mode->width;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  853) 	fmt->format.height = mode->height;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  854) 	fmt->format.field = V4L2_FIELD_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  855) 	if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  856) #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  857) 		*v4l2_subdev_get_try_format(sd, cfg, fmt->pad) = fmt->format;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  858) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  859) 		mutex_unlock(&sc210iot->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  860) 		return -ENOTTY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  861) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  862) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  863) 		sc210iot->cur_mode = mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  864) 		__v4l2_ctrl_s_ctrl(sc210iot->link_freq, mode->link_freq_index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  865) 		__v4l2_ctrl_s_ctrl_int64(sc210iot->pixel_rate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  866) 					 to_pixel_rate(mode->link_freq_index));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  867) 		h_blank = mode->hts_def - mode->width;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  868) 		__v4l2_ctrl_modify_range(sc210iot->hblank, h_blank,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  869) 					 h_blank, 1, h_blank);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  870) 		vblank_def = mode->vts_def - mode->height;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  871) 		__v4l2_ctrl_modify_range(sc210iot->vblank, vblank_def,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  872) 					 SC210IOT_VTS_MAX - mode->height,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  873) 					 1, vblank_def);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  874) 		sc210iot->cur_fps = mode->max_fps;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  875) 		sc210iot->cur_vts = mode->vts_def;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  876) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  877) 	mutex_unlock(&sc210iot->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  878) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  879) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  880) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  881) static int sc210iot_get_fmt(struct v4l2_subdev *sd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  882) 			  struct v4l2_subdev_pad_config *cfg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  883) 			  struct v4l2_subdev_format *fmt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  884) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  885) 	struct sc210iot *sc210iot = to_sc210iot(sd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  886) 	const struct sc210iot_mode *mode = sc210iot->cur_mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  887) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  888) 	mutex_lock(&sc210iot->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  889) 	if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  890) #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  891) 		fmt->format = *v4l2_subdev_get_try_format(sd, cfg, fmt->pad);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  892) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  893) 		mutex_unlock(&sc210iot->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  894) 		return -ENOTTY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  895) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  896) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  897) 		fmt->format.width = mode->width;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  898) 		fmt->format.height = mode->height;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  899) 		fmt->format.code = SC210IOT_MEDIA_BUS_FMT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  900) 		fmt->format.field = V4L2_FIELD_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  901) 		fmt->reserved[0] = mode->vc[PAD0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  902) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  903) 	mutex_unlock(&sc210iot->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  904) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  905) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  906) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  907) #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  908) static int sc210iot_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  909) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  910) 	struct sc210iot *sc210iot = to_sc210iot(sd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  911) 	struct v4l2_mbus_framefmt *try_fmt =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  912) 				v4l2_subdev_get_try_format(sd, fh->pad, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  913) 	const struct sc210iot_mode *def_mode = &supported_modes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  914) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  915) 	mutex_lock(&sc210iot->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  916) 	/* Initialize try_fmt */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  917) 	try_fmt->width = def_mode->width;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  918) 	try_fmt->height = def_mode->height;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  919) 	try_fmt->code = SC210IOT_MEDIA_BUS_FMT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  920) 	try_fmt->field = V4L2_FIELD_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  921) 	mutex_unlock(&sc210iot->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  922) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  923) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  924) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  925) #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  926) static const struct v4l2_subdev_internal_ops sc210iot_internal_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  927) 	.open = sc210iot_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  928) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  929) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  930) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  931) static int sc210iot_s_power(struct v4l2_subdev *sd, int on)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  932) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  933) 	struct sc210iot *sc210iot = to_sc210iot(sd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  934) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  935) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  936) 	mutex_lock(&sc210iot->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  937) 	if (sc210iot->power_on == !!on)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  938) 		goto unlock_and_return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  939) 	if (on) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  940) 		ret = pm_runtime_get_sync(sc210iot->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  941) 		if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  942) 			pm_runtime_put_noidle(sc210iot->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  943) 			goto unlock_and_return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  944) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  945) 		if (!sc210iot->is_thunderboot) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  946) 			ret |= sc210iot_write_reg(sc210iot,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  947) 						  SC210IOT_SOFTWARE_RESET_REG, 0x01);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  948) 			usleep_range(100, 200);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  949) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  950) 		sc210iot->power_on = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  951) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  952) 		pm_runtime_put(sc210iot->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  953) 		sc210iot->power_on = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  954) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  955) unlock_and_return:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  956) 	mutex_unlock(&sc210iot->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  957) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  958) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  959) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  960) static const struct v4l2_subdev_core_ops sc210iot_core_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  961) 	.s_power = sc210iot_s_power,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  962) 	.ioctl = sc210iot_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  963) #ifdef CONFIG_COMPAT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  964) 	.compat_ioctl32 = sc210iot_compat_ioctl32,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  965) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  966) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  967) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  968) static const struct v4l2_subdev_video_ops sc210iot_video_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  969) 	.s_stream = sc210iot_s_stream,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  970) 	.g_frame_interval = sc210iot_g_frame_interval,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  971) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  972) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  973) static const struct v4l2_subdev_pad_ops sc210iot_pad_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  974) 	.enum_mbus_code = sc210iot_enum_mbus_code,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  975) 	.enum_frame_size = sc210iot_enum_frame_sizes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  976) 	.enum_frame_interval = sc210iot_enum_frame_interval,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  977) 	.get_fmt = sc210iot_get_fmt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  978) 	.set_fmt = sc210iot_set_fmt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  979) 	.get_mbus_config = sc210iot_g_mbus_config,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  980) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  981) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  982) static const struct v4l2_subdev_ops sc210iot_subdev_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  983) 	.core   = &sc210iot_core_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  984) 	.video  = &sc210iot_video_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  985) 	.pad    = &sc210iot_pad_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  986) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  987) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  988) static int sc210iot_runtime_resume(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  989) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  990) 	struct i2c_client *client = to_i2c_client(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  991) 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  992) 	struct sc210iot *sc210iot = to_sc210iot(sd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  993) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  994) 	__sc210iot_power_on(sc210iot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  995) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  996) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  997) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  998) static int sc210iot_runtime_suspend(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  999) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) 	struct i2c_client *client = to_i2c_client(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) 	struct sc210iot *sc210iot = to_sc210iot(sd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) 	__sc210iot_power_off(sc210iot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) static const struct dev_pm_ops sc210iot_pm_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) 	SET_RUNTIME_PM_OPS(sc210iot_runtime_suspend,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) 			   sc210iot_runtime_resume, NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) static int sc210iot_probe(struct i2c_client *client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) 			 const struct i2c_device_id *id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) 	struct device *dev = &client->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) 	struct device_node *node = dev->of_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) 	struct sc210iot *sc210iot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) 	struct v4l2_subdev *sd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) 	char facing[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) 	dev_info(dev, "driver version: %02x.%02x.%02x",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) 		 DRIVER_VERSION >> 16,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) 		 (DRIVER_VERSION & 0xff00) >> 8,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) 		 DRIVER_VERSION & 0x00ff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) 	sc210iot = devm_kzalloc(dev, sizeof(*sc210iot), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) 	if (!sc210iot)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) 	sc210iot->dev = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) 	sc210iot->regmap = devm_regmap_init_i2c(client, &sc210iot_regmap_config);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) 	if (IS_ERR(sc210iot->regmap)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) 		dev_err(dev, "Failed to initialize I2C\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) 	ret = of_property_read_u32(node, RKMODULE_CAMERA_MODULE_INDEX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) 				   &sc210iot->module_index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) 	ret |= of_property_read_string(node, RKMODULE_CAMERA_MODULE_FACING,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) 				       &sc210iot->module_facing);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) 	ret |= of_property_read_string(node, RKMODULE_CAMERA_MODULE_NAME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) 				       &sc210iot->module_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) 	ret |= of_property_read_string(node, RKMODULE_CAMERA_LENS_NAME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) 				       &sc210iot->len_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) 		dev_err(dev, "Failed to get module information\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) 	sc210iot->is_thunderboot = IS_ENABLED(CONFIG_VIDEO_ROCKCHIP_THUNDER_BOOT_ISP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) 	sc210iot->xvclk = devm_clk_get(sc210iot->dev, "xvclk");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) 	if (IS_ERR(sc210iot->xvclk)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) 		dev_err(sc210iot->dev, "Failed to get xvclk\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) 	sc210iot->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_ASIS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) 	if (IS_ERR(sc210iot->reset_gpio))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) 		dev_warn(dev, "Failed to get reset-gpios\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) 	sc210iot->pwdn_gpio = devm_gpiod_get(dev, "pwdn", GPIOD_ASIS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) 	if (IS_ERR(sc210iot->pwdn_gpio))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) 		dev_warn(dev, "Failed to get pwdn-gpios\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) 	ret = sc210iot_get_regulators(sc210iot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) 		dev_err(dev, "Failed to get regulators\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) 	sc210iot->pinctrl = devm_pinctrl_get(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) 	if (!IS_ERR(sc210iot->pinctrl)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) 		sc210iot->pins_default =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) 			pinctrl_lookup_state(sc210iot->pinctrl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) 					     OF_CAMERA_PINCTRL_STATE_DEFAULT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) 		if (IS_ERR(sc210iot->pins_default))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) 			dev_info(dev, "could not get default pinstate\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) 		sc210iot->pins_sleep =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) 			pinctrl_lookup_state(sc210iot->pinctrl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) 					     OF_CAMERA_PINCTRL_STATE_SLEEP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) 		if (IS_ERR(sc210iot->pins_sleep))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) 			dev_info(dev, "could not get sleep pinstate\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) 		dev_info(dev, "no pinctrl\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) 	mutex_init(&sc210iot->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) 	/* set default mode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) 	sc210iot->cur_mode = &supported_modes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) 	sc210iot->cfg_num = ARRAY_SIZE(supported_modes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) 	sd = &sc210iot->subdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) 	v4l2_i2c_subdev_init(sd, client, &sc210iot_subdev_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) 	ret = sc210iot_initialize_controls(sc210iot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) 		goto err_destroy_mutex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) 	ret = __sc210iot_power_on(sc210iot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) 		goto err_free_handler;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) 	ret = sc210iot_check_sensor_id(sc210iot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) 		goto err_power_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) 	sd->internal_ops = &sc210iot_internal_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) 	sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) #ifdef CONFIG_MEDIA_CONTROLLER
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) 	sc210iot->pad.flags = MEDIA_PAD_FL_SOURCE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) 	sd->entity.function = MEDIA_ENT_F_CAM_SENSOR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) 	ret = media_entity_pads_init(&sd->entity, 1, &sc210iot->pad);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) 		goto err_power_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) 	memset(facing, 0, sizeof(facing));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) 	if (strcmp(sc210iot->module_facing, "back") == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) 		facing[0] = 'b';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) 		facing[0] = 'f';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) 	snprintf(sd->name, sizeof(sd->name), "m%02d_%s_%s %s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) 		 sc210iot->module_index, facing,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) 		 SC210IOT_NAME, dev_name(sd->dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) 	ret = v4l2_async_register_subdev_sensor_common(sd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) 		dev_err(dev, "Failed to register v4l2 async subdev\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) 		goto err_clean_entity;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) 	pm_runtime_set_active(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) 	pm_runtime_enable(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) 	pm_runtime_idle(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) err_clean_entity:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) #ifdef CONFIG_MEDIA_CONTROLLER
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) 	media_entity_cleanup(&sd->entity);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) err_power_off:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) 	__sc210iot_power_off(sc210iot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) err_free_handler:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) 	v4l2_ctrl_handler_free(&sc210iot->ctrl_handler);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) err_destroy_mutex:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) 	mutex_destroy(&sc210iot->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) static int sc210iot_remove(struct i2c_client *client)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) 	struct sc210iot *sc210iot = to_sc210iot(sd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) 	v4l2_async_unregister_subdev(sd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) #ifdef CONFIG_MEDIA_CONTROLLER
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) 	media_entity_cleanup(&sd->entity);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) 	v4l2_ctrl_handler_free(&sc210iot->ctrl_handler);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) 	mutex_destroy(&sc210iot->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) 	pm_runtime_disable(&client->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) 	if (!pm_runtime_status_suspended(&client->dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) 		__sc210iot_power_off(sc210iot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) 	pm_runtime_set_suspended(&client->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) static const struct i2c_device_id sc210iot_match_id[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) 	{ "sc210iot", 0 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) 	{ },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) static const struct of_device_id sc210iot_of_match[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) 	{ .compatible = "smartsens,sc210iot" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) 	{},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) MODULE_DEVICE_TABLE(of, sc210iot_of_match);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) static struct i2c_driver sc210iot_i2c_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) 	.driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) 		.name = SC210IOT_NAME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) 		.pm = &sc210iot_pm_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) 		.of_match_table = of_match_ptr(sc210iot_of_match),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) 	.probe      = &sc210iot_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) 	.remove     = &sc210iot_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) 	.id_table   = sc210iot_match_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) static int __init sensor_mod_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) 	return i2c_add_driver(&sc210iot_i2c_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) static void __exit sensor_mod_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) 	i2c_del_driver(&sc210iot_i2c_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) device_initcall_sync(sensor_mod_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) module_exit(sensor_mod_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) MODULE_DESCRIPTION("Smartsens sc210iot Image Sensor driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) MODULE_LICENSE("GPL v2");