xref: /OK3568_Linux_fs/kernel/drivers/media/i2c/sc301iot.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * SC301IOT driver
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright (C) 2022 Fuzhou Rockchip Electronics Co., Ltd.
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  */
8*4882a593Smuzhiyun 
9*4882a593Smuzhiyun #include <linux/clk.h>
10*4882a593Smuzhiyun #include <linux/device.h>
11*4882a593Smuzhiyun #include <linux/delay.h>
12*4882a593Smuzhiyun #include <linux/gpio/consumer.h>
13*4882a593Smuzhiyun #include <linux/i2c.h>
14*4882a593Smuzhiyun #include <linux/module.h>
15*4882a593Smuzhiyun #include <linux/pm_runtime.h>
16*4882a593Smuzhiyun #include <linux/regulator/consumer.h>
17*4882a593Smuzhiyun #include <linux/sysfs.h>
18*4882a593Smuzhiyun #include <linux/slab.h>
19*4882a593Smuzhiyun #include <linux/version.h>
20*4882a593Smuzhiyun #include <linux/rk-camera-module.h>
21*4882a593Smuzhiyun #include <linux/rk-preisp.h>
22*4882a593Smuzhiyun #include <media/media-entity.h>
23*4882a593Smuzhiyun #include <media/v4l2-async.h>
24*4882a593Smuzhiyun #include <media/v4l2-ctrls.h>
25*4882a593Smuzhiyun #include <media/v4l2-subdev.h>
26*4882a593Smuzhiyun #include <linux/pinctrl/consumer.h>
27*4882a593Smuzhiyun #include "../platform/rockchip/isp/rkisp_tb_helper.h"
28*4882a593Smuzhiyun 
29*4882a593Smuzhiyun #define DRIVER_VERSION			KERNEL_VERSION(0, 0x01, 0x00)
30*4882a593Smuzhiyun 
31*4882a593Smuzhiyun #ifndef V4L2_CID_DIGITAL_GAIN
32*4882a593Smuzhiyun #define V4L2_CID_DIGITAL_GAIN		V4L2_CID_GAIN
33*4882a593Smuzhiyun #endif
34*4882a593Smuzhiyun 
35*4882a593Smuzhiyun #define SC301IOT_LANES			2
36*4882a593Smuzhiyun #define SC301IOT_BITS_PER_SAMPLE		10
37*4882a593Smuzhiyun #define SC301IOT_LINK_FREQ_594		540000000// 540Mbps
38*4882a593Smuzhiyun 
39*4882a593Smuzhiyun /* pixel rate = link frequency * 2 * lanes / BITS_PER_SAMPLE */
40*4882a593Smuzhiyun #define PIXEL_RATE_WITH_594M_10BIT	(SC301IOT_LINK_FREQ_594 / SC301IOT_BITS_PER_SAMPLE * \
41*4882a593Smuzhiyun 						2 * SC301IOT_LANES)
42*4882a593Smuzhiyun #define SC301IOT_XVCLK_FREQ		24000000
43*4882a593Smuzhiyun 
44*4882a593Smuzhiyun 
45*4882a593Smuzhiyun #define CHIP_ID				0xcc40
46*4882a593Smuzhiyun #define SC301IOT_REG_CHIP_ID		0x3107
47*4882a593Smuzhiyun 
48*4882a593Smuzhiyun #define SC301IOT_REG_CTRL_MODE		0x0100
49*4882a593Smuzhiyun #define SC301IOT_MODE_SW_STANDBY		0x0
50*4882a593Smuzhiyun #define SC301IOT_MODE_STREAMING		BIT(0)
51*4882a593Smuzhiyun 
52*4882a593Smuzhiyun #define SC301IOT_REG_EXPOSURE_H		0x3e00
53*4882a593Smuzhiyun #define SC301IOT_REG_EXPOSURE_M		0x3e01
54*4882a593Smuzhiyun #define SC301IOT_REG_EXPOSURE_L		0x3e02
55*4882a593Smuzhiyun #define SC301IOT_REG_SEXPOSURE_H		0x3e22
56*4882a593Smuzhiyun #define SC301IOT_REG_SEXPOSURE_M		0x3e04
57*4882a593Smuzhiyun #define SC301IOT_REG_SEXPOSURE_L		0x3e05
58*4882a593Smuzhiyun #define	SC301IOT_EXPOSURE_MIN		2
59*4882a593Smuzhiyun #define	SC301IOT_EXPOSURE_STEP		1
60*4882a593Smuzhiyun #define SC301IOT_VTS_MIN			0x640
61*4882a593Smuzhiyun #define SC301IOT_VTS_MAX			0x7fff
62*4882a593Smuzhiyun 
63*4882a593Smuzhiyun #define SC301IOT_REG_DIG_GAIN		0x3e06
64*4882a593Smuzhiyun #define SC301IOT_REG_DIG_FINE_GAIN	0x3e07
65*4882a593Smuzhiyun //#define SC301IOT_REG_ANA_GAIN		0x3e08
66*4882a593Smuzhiyun #define SC301IOT_REG_ANA_GAIN	0x3e09
67*4882a593Smuzhiyun #define SC301IOT_REG_SDIG_GAIN		0x3e10
68*4882a593Smuzhiyun #define SC301IOT_REG_SDIG_FINE_GAIN	0x3e11
69*4882a593Smuzhiyun //#define SC301IOT_REG_SANA_GAIN		0x3e12
70*4882a593Smuzhiyun #define SC301IOT_REG_SANA_GAIN	0x3e13
71*4882a593Smuzhiyun #define SC301IOT_GAIN_MIN		0x0040
72*4882a593Smuzhiyun #define SC301IOT_GAIN_MAX		(6426) //(100.416*64)
73*4882a593Smuzhiyun #define SC301IOT_GAIN_STEP		1
74*4882a593Smuzhiyun #define SC301IOT_GAIN_DEFAULT		0x0400
75*4882a593Smuzhiyun #define SC301IOT_LGAIN			0
76*4882a593Smuzhiyun #define SC301IOT_SGAIN			1
77*4882a593Smuzhiyun 
78*4882a593Smuzhiyun #define SC301IOT_REG_GROUP_HOLD		0x3812
79*4882a593Smuzhiyun #define SC301IOT_GROUP_HOLD_START	0x00
80*4882a593Smuzhiyun #define SC301IOT_GROUP_HOLD_END		0x30
81*4882a593Smuzhiyun 
82*4882a593Smuzhiyun //#define SC301IOT_REG_HIGH_TEMP_H		0x3974
83*4882a593Smuzhiyun //#define SC301IOT_REG_HIGH_TEMP_L		0x3975
84*4882a593Smuzhiyun 
85*4882a593Smuzhiyun #define SC301IOT_REG_TEST_PATTERN	0x4501
86*4882a593Smuzhiyun #define SC301IOT_TEST_PATTERN_BIT_MASK	BIT(3)
87*4882a593Smuzhiyun 
88*4882a593Smuzhiyun #define SC301IOT_REG_VTS_H		0x320e
89*4882a593Smuzhiyun #define SC301IOT_REG_VTS_L		0x320f
90*4882a593Smuzhiyun 
91*4882a593Smuzhiyun #define SC301IOT_FLIP_MIRROR_REG		0x3221
92*4882a593Smuzhiyun 
93*4882a593Smuzhiyun #define SC301IOT_FETCH_EXP_H(VAL)		(((VAL) >> 12) & 0xF)
94*4882a593Smuzhiyun #define SC301IOT_FETCH_EXP_M(VAL)		(((VAL) >> 4) & 0xFF)
95*4882a593Smuzhiyun #define SC301IOT_FETCH_EXP_L(VAL)		(((VAL) & 0xF) << 4)
96*4882a593Smuzhiyun 
97*4882a593Smuzhiyun #define SC301IOT_FETCH_AGAIN_H(VAL)		(((VAL) >> 8) & 0x03)
98*4882a593Smuzhiyun #define SC301IOT_FETCH_AGAIN_L(VAL)		((VAL) & 0xFF)
99*4882a593Smuzhiyun 
100*4882a593Smuzhiyun #define SC301IOT_FETCH_MIRROR(VAL, ENABLE)	(ENABLE ? VAL | 0x06 : VAL & 0xf9)
101*4882a593Smuzhiyun #define SC301IOT_FETCH_FLIP(VAL, ENABLE)		(ENABLE ? VAL | 0x60 : VAL & 0x9f)
102*4882a593Smuzhiyun 
103*4882a593Smuzhiyun #define REG_DELAY			0xFFFE
104*4882a593Smuzhiyun #define REG_NULL			0xFFFF
105*4882a593Smuzhiyun 
106*4882a593Smuzhiyun #define SC301IOT_REG_VALUE_08BIT		1
107*4882a593Smuzhiyun #define SC301IOT_REG_VALUE_16BIT		2
108*4882a593Smuzhiyun #define SC301IOT_REG_VALUE_24BIT		3
109*4882a593Smuzhiyun 
110*4882a593Smuzhiyun #define OF_CAMERA_PINCTRL_STATE_DEFAULT	"rockchip,camera_default"
111*4882a593Smuzhiyun #define OF_CAMERA_PINCTRL_STATE_SLEEP	"rockchip,camera_sleep"
112*4882a593Smuzhiyun #define OF_CAMERA_HDR_MODE		"rockchip,camera-hdr-mode"
113*4882a593Smuzhiyun #define SC301IOT_NAME			"SC301IOT"
114*4882a593Smuzhiyun 
115*4882a593Smuzhiyun static const char * const SC301IOT_supply_names[] = {
116*4882a593Smuzhiyun 	"avdd",		/* Analog power */
117*4882a593Smuzhiyun 	"dovdd",	/* Digital I/O power */
118*4882a593Smuzhiyun 	"dvdd",		/* Digital core power */
119*4882a593Smuzhiyun };
120*4882a593Smuzhiyun 
121*4882a593Smuzhiyun #define SC301IOT_NUM_SUPPLIES ARRAY_SIZE(SC301IOT_supply_names)
122*4882a593Smuzhiyun 
123*4882a593Smuzhiyun struct regval {
124*4882a593Smuzhiyun 	u16 addr;
125*4882a593Smuzhiyun 	u8 val;
126*4882a593Smuzhiyun };
127*4882a593Smuzhiyun 
128*4882a593Smuzhiyun struct SC301IOT_mode {
129*4882a593Smuzhiyun 	u32 bus_fmt;
130*4882a593Smuzhiyun 	u32 width;
131*4882a593Smuzhiyun 	u32 height;
132*4882a593Smuzhiyun 	struct v4l2_fract max_fps;
133*4882a593Smuzhiyun 	u32 hts_def;
134*4882a593Smuzhiyun 	u32 vts_def;
135*4882a593Smuzhiyun 	u32 exp_def;
136*4882a593Smuzhiyun 	const struct regval *reg_list;
137*4882a593Smuzhiyun 	u32 hdr_mode;
138*4882a593Smuzhiyun 	u32 vc[PAD_MAX];
139*4882a593Smuzhiyun };
140*4882a593Smuzhiyun 
141*4882a593Smuzhiyun struct SC301IOT {
142*4882a593Smuzhiyun 	struct i2c_client	*client;
143*4882a593Smuzhiyun 	struct clk		*xvclk;
144*4882a593Smuzhiyun 	struct gpio_desc	*reset_gpio;
145*4882a593Smuzhiyun 	struct gpio_desc	*pwdn_gpio;
146*4882a593Smuzhiyun 	struct regulator_bulk_data supplies[SC301IOT_NUM_SUPPLIES];
147*4882a593Smuzhiyun 
148*4882a593Smuzhiyun 	struct pinctrl		*pinctrl;
149*4882a593Smuzhiyun 	struct pinctrl_state	*pins_default;
150*4882a593Smuzhiyun 	struct pinctrl_state	*pins_sleep;
151*4882a593Smuzhiyun 
152*4882a593Smuzhiyun 	struct v4l2_subdev	subdev;
153*4882a593Smuzhiyun 	struct media_pad	pad;
154*4882a593Smuzhiyun 	struct v4l2_ctrl_handler ctrl_handler;
155*4882a593Smuzhiyun 	struct v4l2_ctrl	*exposure;
156*4882a593Smuzhiyun 	struct v4l2_ctrl	*anal_gain;
157*4882a593Smuzhiyun 	struct v4l2_ctrl	*digi_gain;
158*4882a593Smuzhiyun 	struct v4l2_ctrl	*hblank;
159*4882a593Smuzhiyun 	struct v4l2_ctrl	*vblank;
160*4882a593Smuzhiyun 	struct v4l2_ctrl	*test_pattern;
161*4882a593Smuzhiyun 	struct mutex		mutex;
162*4882a593Smuzhiyun 	struct v4l2_fract	cur_fps;
163*4882a593Smuzhiyun 	bool			streaming;
164*4882a593Smuzhiyun 	bool			power_on;
165*4882a593Smuzhiyun 	const struct SC301IOT_mode *cur_mode;
166*4882a593Smuzhiyun 	u32			module_index;
167*4882a593Smuzhiyun 	const char		*module_facing;
168*4882a593Smuzhiyun 	const char		*module_name;
169*4882a593Smuzhiyun 	const char		*len_name;
170*4882a593Smuzhiyun 	u32			cur_vts;
171*4882a593Smuzhiyun 	bool			has_init_exp;
172*4882a593Smuzhiyun 	struct preisp_hdrae_exp_s init_hdrae_exp;
173*4882a593Smuzhiyun 	bool			is_thunderboot;
174*4882a593Smuzhiyun 	bool			is_first_streamoff;
175*4882a593Smuzhiyun 	u32         sync_mode;
176*4882a593Smuzhiyun };
177*4882a593Smuzhiyun 
178*4882a593Smuzhiyun #define to_SC301IOT(sd) container_of(sd, struct SC301IOT, subdev)
179*4882a593Smuzhiyun 
180*4882a593Smuzhiyun /*
181*4882a593Smuzhiyun  * Xclk 24Mhz
182*4882a593Smuzhiyun  */
183*4882a593Smuzhiyun static const struct regval SC301IOT_global_regs[] = {
184*4882a593Smuzhiyun 	{REG_NULL, 0x00},
185*4882a593Smuzhiyun };
186*4882a593Smuzhiyun 
187*4882a593Smuzhiyun /*
188*4882a593Smuzhiyun  * Xclk 24Mhz
189*4882a593Smuzhiyun  * max_framerate 30fps
190*4882a593Smuzhiyun  * mipi_datarate per lane 540Mbps, 2lane
191*4882a593Smuzhiyun  */
192*4882a593Smuzhiyun static const struct regval SC301IOT_linear_10_2048x1536_regs[] = {
193*4882a593Smuzhiyun 	{0x0103, 0x01},
194*4882a593Smuzhiyun 	{0x0100, 0x00},
195*4882a593Smuzhiyun 	{0x36e9, 0x80},
196*4882a593Smuzhiyun 	{0x37f9, 0x80},
197*4882a593Smuzhiyun 	{0x301c, 0x78},
198*4882a593Smuzhiyun 	{0x301f, 0x11},
199*4882a593Smuzhiyun 	{0x30b8, 0x44},
200*4882a593Smuzhiyun 	{0x3208, 0x08},
201*4882a593Smuzhiyun 	{0x3209, 0x00},
202*4882a593Smuzhiyun 	{0x320a, 0x06},
203*4882a593Smuzhiyun 	{0x320b, 0x00},
204*4882a593Smuzhiyun 	{0x320c, 0x04},
205*4882a593Smuzhiyun 	{0x320d, 0x65},
206*4882a593Smuzhiyun 	{0x320e, 0x06},
207*4882a593Smuzhiyun 	{0x320f, 0x40},
208*4882a593Smuzhiyun 	{0x3214, 0x11},
209*4882a593Smuzhiyun 	{0x3215, 0x11},
210*4882a593Smuzhiyun 	// {0x3223, 0xc0},
211*4882a593Smuzhiyun 	{0x3253, 0x0c},
212*4882a593Smuzhiyun 	{0x3274, 0x09},
213*4882a593Smuzhiyun 	{0x3301, 0x08},
214*4882a593Smuzhiyun 	{0x3306, 0x58},
215*4882a593Smuzhiyun 	{0x3308, 0x08},
216*4882a593Smuzhiyun 	{0x330a, 0x00},
217*4882a593Smuzhiyun 	{0x330b, 0xe0},
218*4882a593Smuzhiyun 	{0x330e, 0x10},
219*4882a593Smuzhiyun 	{0x3314, 0x14},
220*4882a593Smuzhiyun 	{0x331e, 0x55},
221*4882a593Smuzhiyun 	{0x331f, 0x7d},
222*4882a593Smuzhiyun 	{0x3333, 0x10},
223*4882a593Smuzhiyun 	{0x3334, 0x40},
224*4882a593Smuzhiyun 	{0x335e, 0x06},
225*4882a593Smuzhiyun 	{0x335f, 0x08},
226*4882a593Smuzhiyun 	{0x3364, 0x5e},
227*4882a593Smuzhiyun 	{0x337c, 0x02},
228*4882a593Smuzhiyun 	{0x337d, 0x0a},
229*4882a593Smuzhiyun 	{0x3390, 0x01},
230*4882a593Smuzhiyun 	{0x3391, 0x03},
231*4882a593Smuzhiyun 	{0x3392, 0x07},
232*4882a593Smuzhiyun 	{0x3393, 0x08},
233*4882a593Smuzhiyun 	{0x3394, 0x08},
234*4882a593Smuzhiyun 	{0x3395, 0x08},
235*4882a593Smuzhiyun 	{0x3396, 0x08},
236*4882a593Smuzhiyun 	{0x3397, 0x09},
237*4882a593Smuzhiyun 	{0x3398, 0x1f},
238*4882a593Smuzhiyun 	{0x3399, 0x08},
239*4882a593Smuzhiyun 	{0x339a, 0x0a},
240*4882a593Smuzhiyun 	{0x339b, 0x40},
241*4882a593Smuzhiyun 	{0x339c, 0x88},
242*4882a593Smuzhiyun 	{0x33a2, 0x04},
243*4882a593Smuzhiyun 	{0x33ad, 0x0c},
244*4882a593Smuzhiyun 	{0x33b1, 0x80},
245*4882a593Smuzhiyun 	{0x33b3, 0x30},
246*4882a593Smuzhiyun 	{0x33f9, 0x68},
247*4882a593Smuzhiyun 	{0x33fb, 0x80},
248*4882a593Smuzhiyun 	{0x33fc, 0x48},
249*4882a593Smuzhiyun 	{0x33fd, 0x5f},
250*4882a593Smuzhiyun 	{0x349f, 0x03},
251*4882a593Smuzhiyun 	{0x34a6, 0x48},
252*4882a593Smuzhiyun 	{0x34a7, 0x5f},
253*4882a593Smuzhiyun 	{0x34a8, 0x30},
254*4882a593Smuzhiyun 	{0x34a9, 0x30},
255*4882a593Smuzhiyun 	{0x34aa, 0x00},
256*4882a593Smuzhiyun 	{0x34ab, 0xf0},
257*4882a593Smuzhiyun 	{0x34ac, 0x01},
258*4882a593Smuzhiyun 	{0x34ad, 0x08},
259*4882a593Smuzhiyun 	{0x34f8, 0x5f},
260*4882a593Smuzhiyun 	{0x34f9, 0x10},
261*4882a593Smuzhiyun 	{0x3630, 0xf0},
262*4882a593Smuzhiyun 	{0x3631, 0x85},
263*4882a593Smuzhiyun 	{0x3632, 0x74},
264*4882a593Smuzhiyun 	{0x3633, 0x22},
265*4882a593Smuzhiyun 	{0x3637, 0x4d},
266*4882a593Smuzhiyun 	{0x3638, 0xcb},
267*4882a593Smuzhiyun 	{0x363a, 0x8b},
268*4882a593Smuzhiyun 	{0x363c, 0x08},
269*4882a593Smuzhiyun 	{0x3640, 0x00},
270*4882a593Smuzhiyun 	{0x3641, 0x38},
271*4882a593Smuzhiyun 	{0x3670, 0x4e},
272*4882a593Smuzhiyun 	{0x3674, 0xc0},
273*4882a593Smuzhiyun 	{0x3675, 0xb0},
274*4882a593Smuzhiyun 	{0x3676, 0xa0},
275*4882a593Smuzhiyun 	{0x3677, 0x83},
276*4882a593Smuzhiyun 	{0x3678, 0x87},
277*4882a593Smuzhiyun 	{0x3679, 0x8a},
278*4882a593Smuzhiyun 	{0x367c, 0x49},
279*4882a593Smuzhiyun 	{0x367d, 0x4f},
280*4882a593Smuzhiyun 	{0x367e, 0x48},
281*4882a593Smuzhiyun 	{0x367f, 0x4b},
282*4882a593Smuzhiyun 	{0x3690, 0x33},
283*4882a593Smuzhiyun 	{0x3691, 0x33},
284*4882a593Smuzhiyun 	{0x3692, 0x44},
285*4882a593Smuzhiyun 	{0x3699, 0x8a},
286*4882a593Smuzhiyun 	{0x369a, 0xa1},
287*4882a593Smuzhiyun 	{0x369b, 0xc2},
288*4882a593Smuzhiyun 	{0x369c, 0x48},
289*4882a593Smuzhiyun 	{0x369d, 0x4f},
290*4882a593Smuzhiyun 	{0x36a2, 0x4b},
291*4882a593Smuzhiyun 	{0x36a3, 0x4f},
292*4882a593Smuzhiyun 	{0x36ea, 0x09},
293*4882a593Smuzhiyun 	{0x36eb, 0x0d},
294*4882a593Smuzhiyun 	{0x36ec, 0x1c},
295*4882a593Smuzhiyun 	{0x36ed, 0x25},
296*4882a593Smuzhiyun 	{0x370f, 0x01},
297*4882a593Smuzhiyun 	{0x3714, 0x00},
298*4882a593Smuzhiyun 	{0x3722, 0x09},
299*4882a593Smuzhiyun 	{0x3724, 0x41},
300*4882a593Smuzhiyun 	{0x3725, 0xc1},
301*4882a593Smuzhiyun 	{0x3728, 0x00},
302*4882a593Smuzhiyun 	{0x3771, 0x09},
303*4882a593Smuzhiyun 	{0x3772, 0x05},
304*4882a593Smuzhiyun 	{0x3773, 0x05},
305*4882a593Smuzhiyun 	{0x377a, 0x48},
306*4882a593Smuzhiyun 	{0x377b, 0x49},
307*4882a593Smuzhiyun 	{0x37fa, 0x09},
308*4882a593Smuzhiyun 	{0x37fb, 0x33},
309*4882a593Smuzhiyun 	{0x37fc, 0x11},
310*4882a593Smuzhiyun 	{0x37fd, 0x18},
311*4882a593Smuzhiyun 	{0x3905, 0x8d},
312*4882a593Smuzhiyun 	{0x391d, 0x08},
313*4882a593Smuzhiyun 	{0x3922, 0x1a},
314*4882a593Smuzhiyun 	{0x3926, 0x21},
315*4882a593Smuzhiyun 	{0x3933, 0x80},
316*4882a593Smuzhiyun 	{0x3934, 0x0d},
317*4882a593Smuzhiyun 	{0x3937, 0x6a},
318*4882a593Smuzhiyun 	{0x3939, 0x00},
319*4882a593Smuzhiyun 	{0x393a, 0x0e},
320*4882a593Smuzhiyun 	{0x39dc, 0x02},
321*4882a593Smuzhiyun 	{0x3e00, 0x00},
322*4882a593Smuzhiyun 	{0x3e01, 0x63},
323*4882a593Smuzhiyun 	{0x3e02, 0x80},
324*4882a593Smuzhiyun 	{0x3e03, 0x0b},
325*4882a593Smuzhiyun 	{0x3e1b, 0x2a},
326*4882a593Smuzhiyun 	{0x4407, 0x34},
327*4882a593Smuzhiyun 	{0x440e, 0x02},
328*4882a593Smuzhiyun 	{0x5001, 0x40},
329*4882a593Smuzhiyun 	{0x5007, 0x80},
330*4882a593Smuzhiyun 	{0x36e9, 0x24},
331*4882a593Smuzhiyun 	{0x37f9, 0x24},
332*4882a593Smuzhiyun 	{0x3251, 0x90},
333*4882a593Smuzhiyun 
334*4882a593Smuzhiyun 	/* strong signal */
335*4882a593Smuzhiyun 	{0x3650, 0x33},
336*4882a593Smuzhiyun 	{0x3651, 0x7f},
337*4882a593Smuzhiyun 
338*4882a593Smuzhiyun 	{0x3028, 0x05},
339*4882a593Smuzhiyun 	{REG_NULL, 0x00},
340*4882a593Smuzhiyun };
341*4882a593Smuzhiyun 
342*4882a593Smuzhiyun /*
343*4882a593Smuzhiyun  * Xclk 24Mhz
344*4882a593Smuzhiyun  * max_framerate 30fps
345*4882a593Smuzhiyun  * mipi_datarate per lane 1080Mbps, HDR 2lane
346*4882a593Smuzhiyun  */
347*4882a593Smuzhiyun static const struct regval SC301IOT_hdr_10_2048x1536_regs[] = {
348*4882a593Smuzhiyun 	{0x0103, 0x01},
349*4882a593Smuzhiyun 	{0x0100, 0x00},
350*4882a593Smuzhiyun 	{0x36e9, 0x80},
351*4882a593Smuzhiyun 	{0x37f9, 0x80},
352*4882a593Smuzhiyun 	{0x301c, 0x78},
353*4882a593Smuzhiyun 	{0x301f, 0x12},
354*4882a593Smuzhiyun 	{0x30b8, 0x44},
355*4882a593Smuzhiyun 	{0x3208, 0x08},
356*4882a593Smuzhiyun 	{0x3209, 0x00},
357*4882a593Smuzhiyun 	{0x320a, 0x06},
358*4882a593Smuzhiyun 	{0x320b, 0x00},
359*4882a593Smuzhiyun 	{0x320c, 0x04},
360*4882a593Smuzhiyun 	{0x320d, 0x65},
361*4882a593Smuzhiyun 	{0x320e, 0x0c},
362*4882a593Smuzhiyun 	{0x320f, 0x80},
363*4882a593Smuzhiyun 	{0x3214, 0x11},
364*4882a593Smuzhiyun 	{0x3215, 0x11},
365*4882a593Smuzhiyun 	// {0x3223, 0xc0},
366*4882a593Smuzhiyun 	{0x3250, 0xff},
367*4882a593Smuzhiyun 	{0x3253, 0x0c},
368*4882a593Smuzhiyun 	{0x3274, 0x09},
369*4882a593Smuzhiyun 	{0x3281, 0x01},
370*4882a593Smuzhiyun 	{0x3301, 0x08},
371*4882a593Smuzhiyun 	{0x3304, 0x80},
372*4882a593Smuzhiyun 	{0x3306, 0x58},
373*4882a593Smuzhiyun 	{0x3308, 0x08},
374*4882a593Smuzhiyun 	{0x3309, 0xa0},
375*4882a593Smuzhiyun 	{0x330a, 0x00},
376*4882a593Smuzhiyun 	{0x330b, 0xe0},
377*4882a593Smuzhiyun 	{0x330e, 0x10},
378*4882a593Smuzhiyun 	{0x3314, 0x14},
379*4882a593Smuzhiyun 	{0x331e, 0x71},
380*4882a593Smuzhiyun 	{0x331f, 0x91},
381*4882a593Smuzhiyun 	{0x3333, 0x10},
382*4882a593Smuzhiyun 	{0x3334, 0x40},
383*4882a593Smuzhiyun 	{0x335e, 0x06},
384*4882a593Smuzhiyun 	{0x335f, 0x08},
385*4882a593Smuzhiyun 	{0x3364, 0x5e},
386*4882a593Smuzhiyun 	{0x337c, 0x02},
387*4882a593Smuzhiyun 	{0x337d, 0x0a},
388*4882a593Smuzhiyun 	{0x3390, 0x01},
389*4882a593Smuzhiyun 	{0x3391, 0x03},
390*4882a593Smuzhiyun 	{0x3392, 0x07},
391*4882a593Smuzhiyun 	{0x3393, 0x08},
392*4882a593Smuzhiyun 	{0x3394, 0x08},
393*4882a593Smuzhiyun 	{0x3395, 0x08},
394*4882a593Smuzhiyun 	{0x3396, 0x08},
395*4882a593Smuzhiyun 	{0x3397, 0x09},
396*4882a593Smuzhiyun 	{0x3398, 0x1f},
397*4882a593Smuzhiyun 	{0x3399, 0x08},
398*4882a593Smuzhiyun 	{0x339a, 0x14},
399*4882a593Smuzhiyun 	{0x339b, 0x28},
400*4882a593Smuzhiyun 	{0x339c, 0x78},
401*4882a593Smuzhiyun 	{0x33a2, 0x04},
402*4882a593Smuzhiyun 	{0x33ad, 0x0c},
403*4882a593Smuzhiyun 	{0x33b1, 0x80},
404*4882a593Smuzhiyun 	{0x33b3, 0x38},
405*4882a593Smuzhiyun 	{0x33f9, 0x58},
406*4882a593Smuzhiyun 	{0x33fb, 0x80},
407*4882a593Smuzhiyun 	{0x33fc, 0x48},
408*4882a593Smuzhiyun 	{0x33fd, 0x4f},
409*4882a593Smuzhiyun 	{0x349f, 0x03},
410*4882a593Smuzhiyun 	{0x34a6, 0x48},
411*4882a593Smuzhiyun 	{0x34a7, 0x4f},
412*4882a593Smuzhiyun 	{0x34a8, 0x38},
413*4882a593Smuzhiyun 	{0x34a9, 0x28},
414*4882a593Smuzhiyun 	{0x34aa, 0x00},
415*4882a593Smuzhiyun 	{0x34ab, 0xe0},
416*4882a593Smuzhiyun 	{0x34ac, 0x01},
417*4882a593Smuzhiyun 	{0x34ad, 0x08},
418*4882a593Smuzhiyun 	{0x34f8, 0x5f},
419*4882a593Smuzhiyun 	{0x34f9, 0x18},
420*4882a593Smuzhiyun 	{0x3630, 0xf0},
421*4882a593Smuzhiyun 	{0x3631, 0x85},
422*4882a593Smuzhiyun 	{0x3632, 0x74},
423*4882a593Smuzhiyun 	{0x3633, 0x22},
424*4882a593Smuzhiyun 	{0x3637, 0x4d},
425*4882a593Smuzhiyun 	{0x3638, 0xcb},
426*4882a593Smuzhiyun 	{0x363a, 0x8b},
427*4882a593Smuzhiyun 	{0x363c, 0x08},
428*4882a593Smuzhiyun 	{0x3641, 0x38},
429*4882a593Smuzhiyun 	{0x3670, 0x4e},
430*4882a593Smuzhiyun 	{0x3674, 0xc0},
431*4882a593Smuzhiyun 	{0x3675, 0xa0},
432*4882a593Smuzhiyun 	{0x3676, 0x90},
433*4882a593Smuzhiyun 	{0x3677, 0x83},
434*4882a593Smuzhiyun 	{0x3678, 0x86},
435*4882a593Smuzhiyun 	{0x3679, 0x89},
436*4882a593Smuzhiyun 	{0x367c, 0x48},
437*4882a593Smuzhiyun 	{0x367d, 0x4f},
438*4882a593Smuzhiyun 	{0x367e, 0x48},
439*4882a593Smuzhiyun 	{0x367f, 0x4b},
440*4882a593Smuzhiyun 	{0x3690, 0x33},
441*4882a593Smuzhiyun 	{0x3691, 0x44},
442*4882a593Smuzhiyun 	{0x3692, 0x55},
443*4882a593Smuzhiyun 	{0x3699, 0x8a},
444*4882a593Smuzhiyun 	{0x369a, 0xa1},
445*4882a593Smuzhiyun 	{0x369b, 0xc2},
446*4882a593Smuzhiyun 	{0x369c, 0x48},
447*4882a593Smuzhiyun 	{0x369d, 0x4f},
448*4882a593Smuzhiyun 	{0x36a2, 0x4b},
449*4882a593Smuzhiyun 	{0x36a3, 0x4f},
450*4882a593Smuzhiyun 	{0x36ea, 0x09},
451*4882a593Smuzhiyun 	{0x36eb, 0x0d},
452*4882a593Smuzhiyun 	{0x36ec, 0x0c},
453*4882a593Smuzhiyun 	{0x36ed, 0x25},
454*4882a593Smuzhiyun 	{0x370f, 0x01},
455*4882a593Smuzhiyun 	{0x3714, 0x00},
456*4882a593Smuzhiyun 	{0x3722, 0x01},
457*4882a593Smuzhiyun 	{0x3724, 0x41},
458*4882a593Smuzhiyun 	{0x3725, 0xc1},
459*4882a593Smuzhiyun 	{0x3728, 0x00},
460*4882a593Smuzhiyun 	{0x3771, 0x09},
461*4882a593Smuzhiyun 	{0x3772, 0x09},
462*4882a593Smuzhiyun 	{0x3773, 0x05},
463*4882a593Smuzhiyun 	{0x377a, 0x48},
464*4882a593Smuzhiyun 	{0x377b, 0x4f},
465*4882a593Smuzhiyun 	{0x37fa, 0x09},
466*4882a593Smuzhiyun 	{0x37fb, 0x31},
467*4882a593Smuzhiyun 	{0x37fc, 0x10},
468*4882a593Smuzhiyun 	{0x37fd, 0x18},
469*4882a593Smuzhiyun 	{0x3905, 0x8d},
470*4882a593Smuzhiyun 	{0x391d, 0x08},
471*4882a593Smuzhiyun 	{0x3922, 0x1a},
472*4882a593Smuzhiyun 	{0x3926, 0x21},
473*4882a593Smuzhiyun 	{0x3933, 0x80},
474*4882a593Smuzhiyun 	{0x3934, 0x0d},
475*4882a593Smuzhiyun 	{0x3937, 0x6a},
476*4882a593Smuzhiyun 	{0x3939, 0x00},
477*4882a593Smuzhiyun 	{0x393a, 0x0e},
478*4882a593Smuzhiyun 	{0x39dc, 0x02},
479*4882a593Smuzhiyun 	{0x3e00, 0x00},
480*4882a593Smuzhiyun 	{0x3e01, 0xb9},
481*4882a593Smuzhiyun 	{0x3e02, 0xc0},
482*4882a593Smuzhiyun 	{0x3e03, 0x0b},
483*4882a593Smuzhiyun 	{0x3e04, 0x0b},
484*4882a593Smuzhiyun 	{0x3e05, 0xa0},
485*4882a593Smuzhiyun 	{0x3e1b, 0x2a},
486*4882a593Smuzhiyun 	{0x3e23, 0x00},
487*4882a593Smuzhiyun 	{0x3e24, 0xbf},
488*4882a593Smuzhiyun 	{0x4407, 0x34},
489*4882a593Smuzhiyun 	{0x440e, 0x02},
490*4882a593Smuzhiyun 	{0x4509, 0x10},
491*4882a593Smuzhiyun 	{0x4816, 0x71},
492*4882a593Smuzhiyun 	{0x5001, 0x40},
493*4882a593Smuzhiyun 	{0x5007, 0x80},
494*4882a593Smuzhiyun 	{0x36e9, 0x24},
495*4882a593Smuzhiyun 	{0x37f9, 0x24},
496*4882a593Smuzhiyun 	{0x3251, 0x90},
497*4882a593Smuzhiyun 
498*4882a593Smuzhiyun 	/* strong signal */
499*4882a593Smuzhiyun 	{0x3650, 0x33},
500*4882a593Smuzhiyun 	{0x3651, 0x7f},
501*4882a593Smuzhiyun 
502*4882a593Smuzhiyun 	{0x3028, 0x05},
503*4882a593Smuzhiyun 	{REG_NULL, 0x00},
504*4882a593Smuzhiyun };
505*4882a593Smuzhiyun 
506*4882a593Smuzhiyun /*
507*4882a593Smuzhiyun  * Xclk 24Mhz
508*4882a593Smuzhiyun  * max_framerate 30fps
509*4882a593Smuzhiyun  * mipi_datarate per lane 540Mbps, 2lane
510*4882a593Smuzhiyun  */
511*4882a593Smuzhiyun static const struct regval SC301IOT_linear_10_1536x1536_regs[] = {
512*4882a593Smuzhiyun 	{0x0103, 0x01},
513*4882a593Smuzhiyun 	{0x0100, 0x00},
514*4882a593Smuzhiyun 	{0x36e9, 0x80},
515*4882a593Smuzhiyun 	{0x37f9, 0x80},
516*4882a593Smuzhiyun 	{0x301c, 0x78},
517*4882a593Smuzhiyun 	{0x301f, 0x11},
518*4882a593Smuzhiyun 	{0x30b8, 0x44},
519*4882a593Smuzhiyun 	{0x3208, 0x06},
520*4882a593Smuzhiyun 	{0x3209, 0x00},
521*4882a593Smuzhiyun 	{0x320a, 0x06},
522*4882a593Smuzhiyun 	{0x320b, 0x00},
523*4882a593Smuzhiyun 	{0x320c, 0x04},
524*4882a593Smuzhiyun 	{0x320d, 0x65},
525*4882a593Smuzhiyun 	{0x320e, 0x06},
526*4882a593Smuzhiyun 	{0x320f, 0x40},
527*4882a593Smuzhiyun 	{0x3210, 0x01},
528*4882a593Smuzhiyun 	{0x3214, 0x11},
529*4882a593Smuzhiyun 	{0x3215, 0x11},
530*4882a593Smuzhiyun 	// {0x3223, 0xc0},
531*4882a593Smuzhiyun 	{0x3253, 0x0c},
532*4882a593Smuzhiyun 	{0x3274, 0x09},
533*4882a593Smuzhiyun 	{0x3301, 0x08},
534*4882a593Smuzhiyun 	{0x3306, 0x58},
535*4882a593Smuzhiyun 	{0x3308, 0x08},
536*4882a593Smuzhiyun 	{0x330a, 0x00},
537*4882a593Smuzhiyun 	{0x330b, 0xe0},
538*4882a593Smuzhiyun 	{0x330e, 0x10},
539*4882a593Smuzhiyun 	{0x3314, 0x14},
540*4882a593Smuzhiyun 	{0x331e, 0x55},
541*4882a593Smuzhiyun 	{0x331f, 0x7d},
542*4882a593Smuzhiyun 	{0x3333, 0x10},
543*4882a593Smuzhiyun 	{0x3334, 0x40},
544*4882a593Smuzhiyun 	{0x335e, 0x06},
545*4882a593Smuzhiyun 	{0x335f, 0x08},
546*4882a593Smuzhiyun 	{0x3364, 0x5e},
547*4882a593Smuzhiyun 	{0x337c, 0x02},
548*4882a593Smuzhiyun 	{0x337d, 0x0a},
549*4882a593Smuzhiyun 	{0x3390, 0x01},
550*4882a593Smuzhiyun 	{0x3391, 0x03},
551*4882a593Smuzhiyun 	{0x3392, 0x07},
552*4882a593Smuzhiyun 	{0x3393, 0x08},
553*4882a593Smuzhiyun 	{0x3394, 0x08},
554*4882a593Smuzhiyun 	{0x3395, 0x08},
555*4882a593Smuzhiyun 	{0x3396, 0x08},
556*4882a593Smuzhiyun 	{0x3397, 0x09},
557*4882a593Smuzhiyun 	{0x3398, 0x1f},
558*4882a593Smuzhiyun 	{0x3399, 0x08},
559*4882a593Smuzhiyun 	{0x339a, 0x0a},
560*4882a593Smuzhiyun 	{0x339b, 0x40},
561*4882a593Smuzhiyun 	{0x339c, 0x88},
562*4882a593Smuzhiyun 	{0x33a2, 0x04},
563*4882a593Smuzhiyun 	{0x33ad, 0x0c},
564*4882a593Smuzhiyun 	{0x33b1, 0x80},
565*4882a593Smuzhiyun 	{0x33b3, 0x30},
566*4882a593Smuzhiyun 	{0x33f9, 0x68},
567*4882a593Smuzhiyun 	{0x33fb, 0x80},
568*4882a593Smuzhiyun 	{0x33fc, 0x48},
569*4882a593Smuzhiyun 	{0x33fd, 0x5f},
570*4882a593Smuzhiyun 	{0x349f, 0x03},
571*4882a593Smuzhiyun 	{0x34a6, 0x48},
572*4882a593Smuzhiyun 	{0x34a7, 0x5f},
573*4882a593Smuzhiyun 	{0x34a8, 0x30},
574*4882a593Smuzhiyun 	{0x34a9, 0x30},
575*4882a593Smuzhiyun 	{0x34aa, 0x00},
576*4882a593Smuzhiyun 	{0x34ab, 0xf0},
577*4882a593Smuzhiyun 	{0x34ac, 0x01},
578*4882a593Smuzhiyun 	{0x34ad, 0x08},
579*4882a593Smuzhiyun 	{0x34f8, 0x5f},
580*4882a593Smuzhiyun 	{0x34f9, 0x10},
581*4882a593Smuzhiyun 	{0x3630, 0xf0},
582*4882a593Smuzhiyun 	{0x3631, 0x85},
583*4882a593Smuzhiyun 	{0x3632, 0x74},
584*4882a593Smuzhiyun 	{0x3633, 0x22},
585*4882a593Smuzhiyun 	{0x3637, 0x4d},
586*4882a593Smuzhiyun 	{0x3638, 0xcb},
587*4882a593Smuzhiyun 	{0x363a, 0x8b},
588*4882a593Smuzhiyun 	{0x363c, 0x08},
589*4882a593Smuzhiyun 	{0x3640, 0x00},
590*4882a593Smuzhiyun 	{0x3641, 0x38},
591*4882a593Smuzhiyun 	{0x3670, 0x4e},
592*4882a593Smuzhiyun 	{0x3674, 0xc0},
593*4882a593Smuzhiyun 	{0x3675, 0xb0},
594*4882a593Smuzhiyun 	{0x3676, 0xa0},
595*4882a593Smuzhiyun 	{0x3677, 0x83},
596*4882a593Smuzhiyun 	{0x3678, 0x87},
597*4882a593Smuzhiyun 	{0x3679, 0x8a},
598*4882a593Smuzhiyun 	{0x367c, 0x49},
599*4882a593Smuzhiyun 	{0x367d, 0x4f},
600*4882a593Smuzhiyun 	{0x367e, 0x48},
601*4882a593Smuzhiyun 	{0x367f, 0x4b},
602*4882a593Smuzhiyun 	{0x3690, 0x33},
603*4882a593Smuzhiyun 	{0x3691, 0x33},
604*4882a593Smuzhiyun 	{0x3692, 0x44},
605*4882a593Smuzhiyun 	{0x3699, 0x8a},
606*4882a593Smuzhiyun 	{0x369a, 0xa1},
607*4882a593Smuzhiyun 	{0x369b, 0xc2},
608*4882a593Smuzhiyun 	{0x369c, 0x48},
609*4882a593Smuzhiyun 	{0x369d, 0x4f},
610*4882a593Smuzhiyun 	{0x36a2, 0x4b},
611*4882a593Smuzhiyun 	{0x36a3, 0x4f},
612*4882a593Smuzhiyun 	{0x36ea, 0x09},
613*4882a593Smuzhiyun 	{0x36eb, 0x0d},
614*4882a593Smuzhiyun 	{0x36ec, 0x1c},
615*4882a593Smuzhiyun 	{0x36ed, 0x25},
616*4882a593Smuzhiyun 	{0x370f, 0x01},
617*4882a593Smuzhiyun 	{0x3714, 0x00},
618*4882a593Smuzhiyun 	{0x3722, 0x09},
619*4882a593Smuzhiyun 	{0x3724, 0x41},
620*4882a593Smuzhiyun 	{0x3725, 0xc1},
621*4882a593Smuzhiyun 	{0x3728, 0x00},
622*4882a593Smuzhiyun 	{0x3771, 0x09},
623*4882a593Smuzhiyun 	{0x3772, 0x05},
624*4882a593Smuzhiyun 	{0x3773, 0x05},
625*4882a593Smuzhiyun 	{0x377a, 0x48},
626*4882a593Smuzhiyun 	{0x377b, 0x49},
627*4882a593Smuzhiyun 	{0x37fa, 0x09},
628*4882a593Smuzhiyun 	{0x37fb, 0x33},
629*4882a593Smuzhiyun 	{0x37fc, 0x11},
630*4882a593Smuzhiyun 	{0x37fd, 0x18},
631*4882a593Smuzhiyun 	{0x3905, 0x8d},
632*4882a593Smuzhiyun 	{0x391d, 0x08},
633*4882a593Smuzhiyun 	{0x3922, 0x1a},
634*4882a593Smuzhiyun 	{0x3926, 0x21},
635*4882a593Smuzhiyun 	{0x3933, 0x80},
636*4882a593Smuzhiyun 	{0x3934, 0x0d},
637*4882a593Smuzhiyun 	{0x3937, 0x6a},
638*4882a593Smuzhiyun 	{0x3939, 0x00},
639*4882a593Smuzhiyun 	{0x393a, 0x0e},
640*4882a593Smuzhiyun 	{0x39dc, 0x02},
641*4882a593Smuzhiyun 	{0x3e00, 0x00},
642*4882a593Smuzhiyun 	{0x3e01, 0x63},
643*4882a593Smuzhiyun 	{0x3e02, 0x80},
644*4882a593Smuzhiyun 	{0x3e03, 0x0b},
645*4882a593Smuzhiyun 	{0x3e1b, 0x2a},
646*4882a593Smuzhiyun 	{0x4407, 0x34},
647*4882a593Smuzhiyun 	{0x440e, 0x02},
648*4882a593Smuzhiyun 	{0x5001, 0x40},
649*4882a593Smuzhiyun 	{0x5007, 0x80},
650*4882a593Smuzhiyun 	{0x36e9, 0x24},
651*4882a593Smuzhiyun 	{0x37f9, 0x24},
652*4882a593Smuzhiyun 	{0x3251, 0x90},
653*4882a593Smuzhiyun 
654*4882a593Smuzhiyun 	/* strong signal */
655*4882a593Smuzhiyun 	{0x3650, 0x33},
656*4882a593Smuzhiyun 	{0x3651, 0x7f},
657*4882a593Smuzhiyun 
658*4882a593Smuzhiyun 	{0x3028, 0x05},
659*4882a593Smuzhiyun 	{REG_NULL, 0x00},
660*4882a593Smuzhiyun };
661*4882a593Smuzhiyun 
662*4882a593Smuzhiyun /*
663*4882a593Smuzhiyun  * Xclk 24Mhz
664*4882a593Smuzhiyun  * max_framerate 30fps
665*4882a593Smuzhiyun  * mipi_datarate per lane 1080Mbps, HDR 2lane
666*4882a593Smuzhiyun  */
667*4882a593Smuzhiyun static const struct regval SC301IOT_hdr_10_1536x1536_regs[] = {
668*4882a593Smuzhiyun 	{0x0103,  0x01},
669*4882a593Smuzhiyun 	{0x0100, 0x00},
670*4882a593Smuzhiyun 	{0x36e9,  0x80},
671*4882a593Smuzhiyun 	{0x37f9, 0x80},
672*4882a593Smuzhiyun 	{0x301c, 0x78},
673*4882a593Smuzhiyun 	{0x301f, 0x12},
674*4882a593Smuzhiyun 	{0x30b8, 0x44},
675*4882a593Smuzhiyun 	{0x3208, 0x06},
676*4882a593Smuzhiyun 	{0x3209, 0x00},
677*4882a593Smuzhiyun 	{0x320a, 0x06},
678*4882a593Smuzhiyun 	{0x320b, 0x00},
679*4882a593Smuzhiyun 	{0x320c, 0x04},
680*4882a593Smuzhiyun 	{0x320d, 0x65},
681*4882a593Smuzhiyun 	{0x320e, 0x0c},
682*4882a593Smuzhiyun 	{0x320f, 0x80},
683*4882a593Smuzhiyun 	{0x3210, 0x01},
684*4882a593Smuzhiyun 	{0x3214, 0x11},
685*4882a593Smuzhiyun 	{0x3215, 0x11},
686*4882a593Smuzhiyun 	// {0x3223, 0xc0},
687*4882a593Smuzhiyun 	{0x3250, 0xff},
688*4882a593Smuzhiyun 	{0x3253, 0x0c},
689*4882a593Smuzhiyun 	{0x3274, 0x09},
690*4882a593Smuzhiyun 	{0x3281, 0x01},
691*4882a593Smuzhiyun 	{0x3301, 0x08},
692*4882a593Smuzhiyun 	{0x3304, 0x80},
693*4882a593Smuzhiyun 	{0x3306, 0x58},
694*4882a593Smuzhiyun 	{0x3308, 0x08},
695*4882a593Smuzhiyun 	{0x3309, 0xa0},
696*4882a593Smuzhiyun 	{0x330a, 0x00},
697*4882a593Smuzhiyun 	{0x330b, 0xe0},
698*4882a593Smuzhiyun 	{0x330e, 0x10},
699*4882a593Smuzhiyun 	{0x3314, 0x14},
700*4882a593Smuzhiyun 	{0x331e, 0x71},
701*4882a593Smuzhiyun 	{0x331f, 0x91},
702*4882a593Smuzhiyun 	{0x3333, 0x10},
703*4882a593Smuzhiyun 	{0x3334, 0x40},
704*4882a593Smuzhiyun 	{0x335e, 0x06},
705*4882a593Smuzhiyun 	{0x335f, 0x08},
706*4882a593Smuzhiyun 	{0x3364, 0x5e},
707*4882a593Smuzhiyun 	{0x337c, 0x02},
708*4882a593Smuzhiyun 	{0x337d, 0x0a},
709*4882a593Smuzhiyun 	{0x3390, 0x01},
710*4882a593Smuzhiyun 	{0x3391, 0x03},
711*4882a593Smuzhiyun 	{0x3392, 0x07},
712*4882a593Smuzhiyun 	{0x3393, 0x08},
713*4882a593Smuzhiyun 	{0x3394, 0x08},
714*4882a593Smuzhiyun 	{0x3395, 0x08},
715*4882a593Smuzhiyun 	{0x3396, 0x08},
716*4882a593Smuzhiyun 	{0x3397, 0x09},
717*4882a593Smuzhiyun 	{0x3398, 0x1f},
718*4882a593Smuzhiyun 	{0x3399, 0x08},
719*4882a593Smuzhiyun 	{0x339a, 0x14},
720*4882a593Smuzhiyun 	{0x339b, 0x28},
721*4882a593Smuzhiyun 	{0x339c, 0x78},
722*4882a593Smuzhiyun 	{0x33a2, 0x04},
723*4882a593Smuzhiyun 	{0x33ad, 0x0c},
724*4882a593Smuzhiyun 	{0x33b1, 0x80},
725*4882a593Smuzhiyun 	{0x33b3, 0x38},
726*4882a593Smuzhiyun 	{0x33f9, 0x58},
727*4882a593Smuzhiyun 	{0x33fb, 0x80},
728*4882a593Smuzhiyun 	{0x33fc, 0x48},
729*4882a593Smuzhiyun 	{0x33fd, 0x4f},
730*4882a593Smuzhiyun 	{0x349f, 0x03},
731*4882a593Smuzhiyun 	{0x34a6, 0x48},
732*4882a593Smuzhiyun 	{0x34a7, 0x4f},
733*4882a593Smuzhiyun 	{0x34a8, 0x38},
734*4882a593Smuzhiyun 	{0x34a9, 0x28},
735*4882a593Smuzhiyun 	{0x34aa, 0x00},
736*4882a593Smuzhiyun 	{0x34ab, 0xe0},
737*4882a593Smuzhiyun 	{0x34ac, 0x01},
738*4882a593Smuzhiyun 	{0x34ad, 0x08},
739*4882a593Smuzhiyun 	{0x34f8, 0x5f},
740*4882a593Smuzhiyun 	{0x34f9, 0x18},
741*4882a593Smuzhiyun 	{0x3630, 0xf0},
742*4882a593Smuzhiyun 	{0x3631, 0x85},
743*4882a593Smuzhiyun 	{0x3632, 0x74},
744*4882a593Smuzhiyun 	{0x3633, 0x22},
745*4882a593Smuzhiyun 	{0x3637, 0x4d},
746*4882a593Smuzhiyun 	{0x3638, 0xcb},
747*4882a593Smuzhiyun 	{0x363a, 0x8b},
748*4882a593Smuzhiyun 	{0x363c, 0x08},
749*4882a593Smuzhiyun 	{0x3641, 0x38},
750*4882a593Smuzhiyun 	{0x3670, 0x4e},
751*4882a593Smuzhiyun 	{0x3674, 0xc0},
752*4882a593Smuzhiyun 	{0x3675, 0xa0},
753*4882a593Smuzhiyun 	{0x3676, 0x90},
754*4882a593Smuzhiyun 	{0x3677, 0x83},
755*4882a593Smuzhiyun 	{0x3678, 0x86},
756*4882a593Smuzhiyun 	{0x3679, 0x89},
757*4882a593Smuzhiyun 	{0x367c, 0x48},
758*4882a593Smuzhiyun 	{0x367d, 0x4f},
759*4882a593Smuzhiyun 	{0x367e, 0x48},
760*4882a593Smuzhiyun 	{0x367f, 0x4b},
761*4882a593Smuzhiyun 	{0x3690, 0x33},
762*4882a593Smuzhiyun 	{0x3691, 0x44},
763*4882a593Smuzhiyun 	{0x3692, 0x55},
764*4882a593Smuzhiyun 	{0x3699, 0x8a},
765*4882a593Smuzhiyun 	{0x369a, 0xa1},
766*4882a593Smuzhiyun 	{0x369b, 0xc2},
767*4882a593Smuzhiyun 	{0x369c, 0x48},
768*4882a593Smuzhiyun 	{0x369d, 0x4f},
769*4882a593Smuzhiyun 	{0x36a2, 0x4b},
770*4882a593Smuzhiyun 	{0x36a3, 0x4f},
771*4882a593Smuzhiyun 	{0x36ea, 0x09},
772*4882a593Smuzhiyun 	{0x36eb, 0x0d},
773*4882a593Smuzhiyun 	{0x36ec, 0x0c},
774*4882a593Smuzhiyun 	{0x36ed, 0x25},
775*4882a593Smuzhiyun 	{0x370f, 0x01},
776*4882a593Smuzhiyun 	{0x3714, 0x00},
777*4882a593Smuzhiyun 	{0x3722, 0x01},
778*4882a593Smuzhiyun 	{0x3724, 0x41},
779*4882a593Smuzhiyun 	{0x3725, 0xc1},
780*4882a593Smuzhiyun 	{0x3728, 0x00},
781*4882a593Smuzhiyun 	{0x3771, 0x09},
782*4882a593Smuzhiyun 	{0x3772, 0x09},
783*4882a593Smuzhiyun 	{0x3773, 0x05},
784*4882a593Smuzhiyun 	{0x377a, 0x48},
785*4882a593Smuzhiyun 	{0x377b, 0x4f},
786*4882a593Smuzhiyun 	{0x37fa, 0x09},
787*4882a593Smuzhiyun 	{0x37fb, 0x31},
788*4882a593Smuzhiyun 	{0x37fc, 0x10},
789*4882a593Smuzhiyun 	{0x37fd, 0x18},
790*4882a593Smuzhiyun 	{0x3905, 0x8d},
791*4882a593Smuzhiyun 	{0x391d, 0x08},
792*4882a593Smuzhiyun 	{0x3922, 0x1a},
793*4882a593Smuzhiyun 	{0x3926, 0x21},
794*4882a593Smuzhiyun 	{0x3933, 0x80},
795*4882a593Smuzhiyun 	{0x3934, 0x0d},
796*4882a593Smuzhiyun 	{0x3937, 0x6a},
797*4882a593Smuzhiyun 	{0x3939, 0x00},
798*4882a593Smuzhiyun 	{0x393a, 0x0e},
799*4882a593Smuzhiyun 	{0x39dc, 0x02},
800*4882a593Smuzhiyun 	{0x3e00, 0x00},
801*4882a593Smuzhiyun 	{0x3e01, 0xb9},
802*4882a593Smuzhiyun 	{0x3e02, 0xc0},
803*4882a593Smuzhiyun 	{0x3e03, 0x0b},
804*4882a593Smuzhiyun 	{0x3e04, 0x0b},
805*4882a593Smuzhiyun 	{0x3e05, 0xa0},
806*4882a593Smuzhiyun 	{0x3e1b, 0x2a},
807*4882a593Smuzhiyun 	{0x3e23, 0x00},
808*4882a593Smuzhiyun 	{0x3e24, 0xbf},
809*4882a593Smuzhiyun 	{0x4407, 0x34},
810*4882a593Smuzhiyun 	{0x440e, 0x02},
811*4882a593Smuzhiyun 	{0x4509, 0x10},
812*4882a593Smuzhiyun 	{0x4816, 0x71},
813*4882a593Smuzhiyun 	{0x5001, 0x40},
814*4882a593Smuzhiyun 	{0x5007, 0x80},
815*4882a593Smuzhiyun 	{0x36e9, 0x24},
816*4882a593Smuzhiyun 	{0x37f9, 0x24},
817*4882a593Smuzhiyun 	{0x3251, 0x90},
818*4882a593Smuzhiyun 
819*4882a593Smuzhiyun 	/* strong signal */
820*4882a593Smuzhiyun 	{0x3650, 0x33},
821*4882a593Smuzhiyun 	{0x3651, 0x7f},
822*4882a593Smuzhiyun 
823*4882a593Smuzhiyun 	{0x3028, 0x05},
824*4882a593Smuzhiyun 	{REG_NULL, 0x00},
825*4882a593Smuzhiyun };
826*4882a593Smuzhiyun 
827*4882a593Smuzhiyun static const struct SC301IOT_mode supported_modes[] = {
828*4882a593Smuzhiyun 	{
829*4882a593Smuzhiyun 		.width = 2048,
830*4882a593Smuzhiyun 		.height = 1536,
831*4882a593Smuzhiyun 		.max_fps = {
832*4882a593Smuzhiyun 			.numerator = 10000,
833*4882a593Smuzhiyun 			.denominator = 300000,
834*4882a593Smuzhiyun 		},
835*4882a593Smuzhiyun 		.exp_def = 0x638,
836*4882a593Smuzhiyun 		.hts_def = 0x8ca,
837*4882a593Smuzhiyun 		.vts_def = 0x640,
838*4882a593Smuzhiyun 		.bus_fmt = MEDIA_BUS_FMT_SBGGR10_1X10,
839*4882a593Smuzhiyun 		.reg_list = SC301IOT_linear_10_2048x1536_regs,
840*4882a593Smuzhiyun 		.hdr_mode = NO_HDR,
841*4882a593Smuzhiyun 		.vc[PAD0] = V4L2_MBUS_CSI2_CHANNEL_0,
842*4882a593Smuzhiyun 	}, {
843*4882a593Smuzhiyun 		.width = 2048,
844*4882a593Smuzhiyun 		.height = 1536,
845*4882a593Smuzhiyun 		.max_fps = {
846*4882a593Smuzhiyun 			.numerator = 10000,
847*4882a593Smuzhiyun 			.denominator = 300000,
848*4882a593Smuzhiyun 		},
849*4882a593Smuzhiyun 		.exp_def = 0xb9c,
850*4882a593Smuzhiyun 		.hts_def = 0x8ca,
851*4882a593Smuzhiyun 		.vts_def = 0xc80,
852*4882a593Smuzhiyun 		.bus_fmt = MEDIA_BUS_FMT_SBGGR10_1X10,
853*4882a593Smuzhiyun 		.reg_list = SC301IOT_hdr_10_2048x1536_regs,
854*4882a593Smuzhiyun 		.hdr_mode = HDR_X2,
855*4882a593Smuzhiyun 		.vc[PAD0] = V4L2_MBUS_CSI2_CHANNEL_1,
856*4882a593Smuzhiyun 		.vc[PAD1] = V4L2_MBUS_CSI2_CHANNEL_0,//L->csi wr0
857*4882a593Smuzhiyun 		.vc[PAD2] = V4L2_MBUS_CSI2_CHANNEL_1,
858*4882a593Smuzhiyun 		.vc[PAD3] = V4L2_MBUS_CSI2_CHANNEL_1,//M->csi wr2
859*4882a593Smuzhiyun 	}, {
860*4882a593Smuzhiyun 		.width = 1536,
861*4882a593Smuzhiyun 		.height = 1536,
862*4882a593Smuzhiyun 		.max_fps = {
863*4882a593Smuzhiyun 			.numerator = 10000,
864*4882a593Smuzhiyun 			.denominator = 300000,
865*4882a593Smuzhiyun 		},
866*4882a593Smuzhiyun 		.exp_def = 0x96,
867*4882a593Smuzhiyun 		.hts_def = 0x8ca,
868*4882a593Smuzhiyun 		.vts_def = 0x640,
869*4882a593Smuzhiyun 		.bus_fmt = MEDIA_BUS_FMT_SBGGR10_1X10,
870*4882a593Smuzhiyun 		.reg_list = SC301IOT_linear_10_1536x1536_regs,
871*4882a593Smuzhiyun 		.hdr_mode = NO_HDR,
872*4882a593Smuzhiyun 		.vc[PAD0] = V4L2_MBUS_CSI2_CHANNEL_0,
873*4882a593Smuzhiyun 	}, {
874*4882a593Smuzhiyun 		.width = 1536,
875*4882a593Smuzhiyun 		.height = 1536,
876*4882a593Smuzhiyun 		.max_fps = {
877*4882a593Smuzhiyun 			.numerator = 10000,
878*4882a593Smuzhiyun 			.denominator = 300000,
879*4882a593Smuzhiyun 		},
880*4882a593Smuzhiyun 		.exp_def = 0xb9c,
881*4882a593Smuzhiyun 		.hts_def = 0x8ca,
882*4882a593Smuzhiyun 		.vts_def = 0xc80,
883*4882a593Smuzhiyun 		.bus_fmt = MEDIA_BUS_FMT_SBGGR10_1X10,
884*4882a593Smuzhiyun 		.reg_list = SC301IOT_hdr_10_1536x1536_regs,
885*4882a593Smuzhiyun 		.hdr_mode = HDR_X2,
886*4882a593Smuzhiyun 		.vc[PAD0] = V4L2_MBUS_CSI2_CHANNEL_1,
887*4882a593Smuzhiyun 		.vc[PAD1] = V4L2_MBUS_CSI2_CHANNEL_0,//L->csi wr0
888*4882a593Smuzhiyun 		.vc[PAD2] = V4L2_MBUS_CSI2_CHANNEL_1,
889*4882a593Smuzhiyun 		.vc[PAD3] = V4L2_MBUS_CSI2_CHANNEL_1,//M->csi wr2
890*4882a593Smuzhiyun 	},
891*4882a593Smuzhiyun };
892*4882a593Smuzhiyun 
893*4882a593Smuzhiyun static const s64 link_freq_menu_items[] = {
894*4882a593Smuzhiyun 	SC301IOT_LINK_FREQ_594
895*4882a593Smuzhiyun };
896*4882a593Smuzhiyun 
897*4882a593Smuzhiyun static const char * const SC301IOT_test_pattern_menu[] = {
898*4882a593Smuzhiyun 	"Disabled",
899*4882a593Smuzhiyun 	"Vertical Color Bar Type 1",
900*4882a593Smuzhiyun 	"Vertical Color Bar Type 2",
901*4882a593Smuzhiyun 	"Vertical Color Bar Type 3",
902*4882a593Smuzhiyun 	"Vertical Color Bar Type 4"
903*4882a593Smuzhiyun };
904*4882a593Smuzhiyun 
905*4882a593Smuzhiyun /* Write registers up to 4 at a time */
SC301IOT_write_reg(struct i2c_client * client,u16 reg,u32 len,u32 val)906*4882a593Smuzhiyun static int SC301IOT_write_reg(struct i2c_client *client, u16 reg,
907*4882a593Smuzhiyun 			    u32 len, u32 val)
908*4882a593Smuzhiyun {
909*4882a593Smuzhiyun 	u32 buf_i, val_i;
910*4882a593Smuzhiyun 	u8 buf[6];
911*4882a593Smuzhiyun 	u8 *val_p;
912*4882a593Smuzhiyun 	__be32 val_be;
913*4882a593Smuzhiyun 
914*4882a593Smuzhiyun 	if (len > 4)
915*4882a593Smuzhiyun 		return -EINVAL;
916*4882a593Smuzhiyun 
917*4882a593Smuzhiyun 	buf[0] = reg >> 8;
918*4882a593Smuzhiyun 	buf[1] = reg & 0xff;
919*4882a593Smuzhiyun 
920*4882a593Smuzhiyun 	val_be = cpu_to_be32(val);
921*4882a593Smuzhiyun 	val_p = (u8 *)&val_be;
922*4882a593Smuzhiyun 	buf_i = 2;
923*4882a593Smuzhiyun 	val_i = 4 - len;
924*4882a593Smuzhiyun 
925*4882a593Smuzhiyun 	while (val_i < 4)
926*4882a593Smuzhiyun 		buf[buf_i++] = val_p[val_i++];
927*4882a593Smuzhiyun 
928*4882a593Smuzhiyun 	if (i2c_master_send(client, buf, len + 2) != len + 2)
929*4882a593Smuzhiyun 		return -EIO;
930*4882a593Smuzhiyun 
931*4882a593Smuzhiyun 	return 0;
932*4882a593Smuzhiyun }
933*4882a593Smuzhiyun 
SC301IOT_write_array(struct i2c_client * client,const struct regval * regs)934*4882a593Smuzhiyun static int SC301IOT_write_array(struct i2c_client *client,
935*4882a593Smuzhiyun 			       const struct regval *regs)
936*4882a593Smuzhiyun {
937*4882a593Smuzhiyun 	u32 i;
938*4882a593Smuzhiyun 	int ret = 0;
939*4882a593Smuzhiyun 
940*4882a593Smuzhiyun 	for (i = 0; ret == 0 && regs[i].addr != REG_NULL; i++)
941*4882a593Smuzhiyun 		ret = SC301IOT_write_reg(client, regs[i].addr,
942*4882a593Smuzhiyun 					SC301IOT_REG_VALUE_08BIT, regs[i].val);
943*4882a593Smuzhiyun 
944*4882a593Smuzhiyun 	return ret;
945*4882a593Smuzhiyun }
946*4882a593Smuzhiyun 
947*4882a593Smuzhiyun /* Read registers up to 4 at a time */
SC301IOT_read_reg(struct i2c_client * client,u16 reg,unsigned int len,u32 * val)948*4882a593Smuzhiyun static int SC301IOT_read_reg(struct i2c_client *client, u16 reg, unsigned int len,
949*4882a593Smuzhiyun 			    u32 *val)
950*4882a593Smuzhiyun {
951*4882a593Smuzhiyun 	struct i2c_msg msgs[2];
952*4882a593Smuzhiyun 	u8 *data_be_p;
953*4882a593Smuzhiyun 	__be32 data_be = 0;
954*4882a593Smuzhiyun 	__be16 reg_addr_be = cpu_to_be16(reg);
955*4882a593Smuzhiyun 	int ret;
956*4882a593Smuzhiyun 
957*4882a593Smuzhiyun 	if (len > 4 || !len)
958*4882a593Smuzhiyun 		return -EINVAL;
959*4882a593Smuzhiyun 
960*4882a593Smuzhiyun 	data_be_p = (u8 *)&data_be;
961*4882a593Smuzhiyun 	/* Write register address */
962*4882a593Smuzhiyun 	msgs[0].addr = client->addr;
963*4882a593Smuzhiyun 	msgs[0].flags = 0;
964*4882a593Smuzhiyun 	msgs[0].len = 2;
965*4882a593Smuzhiyun 	msgs[0].buf = (u8 *)&reg_addr_be;
966*4882a593Smuzhiyun 
967*4882a593Smuzhiyun 	/* Read data from register */
968*4882a593Smuzhiyun 	msgs[1].addr = client->addr;
969*4882a593Smuzhiyun 	msgs[1].flags = I2C_M_RD;
970*4882a593Smuzhiyun 	msgs[1].len = len;
971*4882a593Smuzhiyun 	msgs[1].buf = &data_be_p[4 - len];
972*4882a593Smuzhiyun 
973*4882a593Smuzhiyun 	ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
974*4882a593Smuzhiyun 	if (ret != ARRAY_SIZE(msgs))
975*4882a593Smuzhiyun 		return -EIO;
976*4882a593Smuzhiyun 
977*4882a593Smuzhiyun 	*val = be32_to_cpu(data_be);
978*4882a593Smuzhiyun 
979*4882a593Smuzhiyun 	return 0;
980*4882a593Smuzhiyun }
981*4882a593Smuzhiyun 
982*4882a593Smuzhiyun 
983*4882a593Smuzhiyun 
984*4882a593Smuzhiyun 
985*4882a593Smuzhiyun /* mode: 0 = lgain  1 = sgain */
SC301IOT_set_gain_reg(struct SC301IOT * SC301IOT,u32 gain,int mode)986*4882a593Smuzhiyun static int SC301IOT_set_gain_reg(struct SC301IOT *SC301IOT, u32 gain, int mode)
987*4882a593Smuzhiyun {
988*4882a593Smuzhiyun 	u8 ANA_Coarse_gain_reg = 0x00, DIG_Fine_gain_reg = 0x80;
989*4882a593Smuzhiyun 	u32 ANA_Coarse_gain = 1024, DIG_gain_reg = 0x00;
990*4882a593Smuzhiyun 	int ret = 0;
991*4882a593Smuzhiyun 
992*4882a593Smuzhiyun 
993*4882a593Smuzhiyun 	gain = gain * 16;
994*4882a593Smuzhiyun 	if (gain <= 1024)
995*4882a593Smuzhiyun 		gain = 1024;
996*4882a593Smuzhiyun 	else if (gain > SC301IOT_GAIN_MAX * 16)
997*4882a593Smuzhiyun 		gain = SC301IOT_GAIN_MAX * 16;
998*4882a593Smuzhiyun 
999*4882a593Smuzhiyun 	if (gain < 1606) {               // start Ana again
1000*4882a593Smuzhiyun 		ANA_Coarse_gain = 1024;
1001*4882a593Smuzhiyun 		ANA_Coarse_gain_reg = 0x00;
1002*4882a593Smuzhiyun 	} else if (gain < 3397) {
1003*4882a593Smuzhiyun 		ANA_Coarse_gain = 1606;
1004*4882a593Smuzhiyun 		ANA_Coarse_gain_reg = 0x40;
1005*4882a593Smuzhiyun 	} else if (gain < 6426) {
1006*4882a593Smuzhiyun 		ANA_Coarse_gain = 3397;
1007*4882a593Smuzhiyun 		ANA_Coarse_gain_reg = 0x48;
1008*4882a593Smuzhiyun 	} else if (gain < 12853) {
1009*4882a593Smuzhiyun 		ANA_Coarse_gain = 6426;
1010*4882a593Smuzhiyun 		ANA_Coarse_gain_reg = 0x49;
1011*4882a593Smuzhiyun 	} else if (gain < 25706) {
1012*4882a593Smuzhiyun 		ANA_Coarse_gain = 12853;
1013*4882a593Smuzhiyun 		ANA_Coarse_gain_reg = 0x4b;
1014*4882a593Smuzhiyun 	} else if (gain < 51412) {
1015*4882a593Smuzhiyun 		ANA_Coarse_gain = 25706;
1016*4882a593Smuzhiyun 		ANA_Coarse_gain_reg = 0x4f;
1017*4882a593Smuzhiyun 	} else {
1018*4882a593Smuzhiyun 		ANA_Coarse_gain = 51412;
1019*4882a593Smuzhiyun 		ANA_Coarse_gain_reg = 0x5f;
1020*4882a593Smuzhiyun 	}
1021*4882a593Smuzhiyun 	gain = gain * 1024 / ANA_Coarse_gain;	// start Dig again
1022*4882a593Smuzhiyun 	if (gain <= 1024)
1023*4882a593Smuzhiyun 		gain = 1024;
1024*4882a593Smuzhiyun 	else if (gain >= 2031)
1025*4882a593Smuzhiyun 		gain = 2031;
1026*4882a593Smuzhiyun 	DIG_Fine_gain_reg = gain/8;
1027*4882a593Smuzhiyun 
1028*4882a593Smuzhiyun 	if (mode == SC301IOT_LGAIN) {
1029*4882a593Smuzhiyun 		ret = SC301IOT_write_reg(SC301IOT->client,
1030*4882a593Smuzhiyun 					SC301IOT_REG_DIG_GAIN,
1031*4882a593Smuzhiyun 					SC301IOT_REG_VALUE_08BIT,
1032*4882a593Smuzhiyun 					DIG_gain_reg & 0xF);
1033*4882a593Smuzhiyun 		ret |= SC301IOT_write_reg(SC301IOT->client,
1034*4882a593Smuzhiyun 					 SC301IOT_REG_DIG_FINE_GAIN,
1035*4882a593Smuzhiyun 					 SC301IOT_REG_VALUE_08BIT,
1036*4882a593Smuzhiyun 					 DIG_Fine_gain_reg);
1037*4882a593Smuzhiyun 		ret |= SC301IOT_write_reg(SC301IOT->client,
1038*4882a593Smuzhiyun 					 SC301IOT_REG_ANA_GAIN,
1039*4882a593Smuzhiyun 					 SC301IOT_REG_VALUE_08BIT,
1040*4882a593Smuzhiyun 					 ANA_Coarse_gain_reg);
1041*4882a593Smuzhiyun 	} else {
1042*4882a593Smuzhiyun 		ret = SC301IOT_write_reg(SC301IOT->client,
1043*4882a593Smuzhiyun 					SC301IOT_REG_SDIG_GAIN,
1044*4882a593Smuzhiyun 					SC301IOT_REG_VALUE_08BIT,
1045*4882a593Smuzhiyun 					DIG_gain_reg & 0xF);
1046*4882a593Smuzhiyun 		ret |= SC301IOT_write_reg(SC301IOT->client,
1047*4882a593Smuzhiyun 					 SC301IOT_REG_SDIG_FINE_GAIN,
1048*4882a593Smuzhiyun 					 SC301IOT_REG_VALUE_08BIT,
1049*4882a593Smuzhiyun 					 DIG_Fine_gain_reg);
1050*4882a593Smuzhiyun 		ret |= SC301IOT_write_reg(SC301IOT->client,
1051*4882a593Smuzhiyun 					 SC301IOT_REG_SANA_GAIN,
1052*4882a593Smuzhiyun 					 SC301IOT_REG_VALUE_08BIT,
1053*4882a593Smuzhiyun 					 ANA_Coarse_gain_reg);
1054*4882a593Smuzhiyun 	}
1055*4882a593Smuzhiyun 	return ret;
1056*4882a593Smuzhiyun }
1057*4882a593Smuzhiyun 
SC301IOT_set_hdrae(struct SC301IOT * SC301IOT,struct preisp_hdrae_exp_s * ae)1058*4882a593Smuzhiyun static int SC301IOT_set_hdrae(struct SC301IOT *SC301IOT,
1059*4882a593Smuzhiyun 			    struct preisp_hdrae_exp_s *ae)
1060*4882a593Smuzhiyun {
1061*4882a593Smuzhiyun 	int ret = 0;
1062*4882a593Smuzhiyun 	u32 l_exp_time, m_exp_time, s_exp_time;
1063*4882a593Smuzhiyun 	u32 l_a_gain, m_a_gain, s_a_gain;
1064*4882a593Smuzhiyun 
1065*4882a593Smuzhiyun 	if (!SC301IOT->has_init_exp && !SC301IOT->streaming) {
1066*4882a593Smuzhiyun 		SC301IOT->init_hdrae_exp = *ae;
1067*4882a593Smuzhiyun 		SC301IOT->has_init_exp = true;
1068*4882a593Smuzhiyun 		dev_dbg(&SC301IOT->client->dev, "SC301IOT don't stream, record exp for hdr!\n");
1069*4882a593Smuzhiyun 		return ret;
1070*4882a593Smuzhiyun 	}
1071*4882a593Smuzhiyun 	l_exp_time = ae->long_exp_reg;
1072*4882a593Smuzhiyun 	m_exp_time = ae->middle_exp_reg;
1073*4882a593Smuzhiyun 	s_exp_time = ae->short_exp_reg;
1074*4882a593Smuzhiyun 	l_a_gain = ae->long_gain_reg;
1075*4882a593Smuzhiyun 	m_a_gain = ae->middle_gain_reg;
1076*4882a593Smuzhiyun 	s_a_gain = ae->short_gain_reg;
1077*4882a593Smuzhiyun 
1078*4882a593Smuzhiyun 	dev_dbg(&SC301IOT->client->dev,
1079*4882a593Smuzhiyun 		"rev exp req: L_exp: 0x%x, 0x%x, M_exp: 0x%x, 0x%x S_exp: 0x%x, 0x%x\n",
1080*4882a593Smuzhiyun 		l_exp_time, m_exp_time, s_exp_time,
1081*4882a593Smuzhiyun 		l_a_gain, m_a_gain, s_a_gain);
1082*4882a593Smuzhiyun 
1083*4882a593Smuzhiyun 	if (SC301IOT->cur_mode->hdr_mode == HDR_X2) {
1084*4882a593Smuzhiyun 		//2 stagger
1085*4882a593Smuzhiyun 		l_a_gain = m_a_gain;
1086*4882a593Smuzhiyun 		l_exp_time = m_exp_time;
1087*4882a593Smuzhiyun 	}
1088*4882a593Smuzhiyun 
1089*4882a593Smuzhiyun 	//set exposure
1090*4882a593Smuzhiyun 	//l_exp_time = ae->long_exp_reg;
1091*4882a593Smuzhiyun 	//s_exp_time = ae->short_exp_reg;
1092*4882a593Smuzhiyun 
1093*4882a593Smuzhiyun 	l_exp_time = l_exp_time * 2;
1094*4882a593Smuzhiyun 	s_exp_time = s_exp_time * 2;
1095*4882a593Smuzhiyun 	if (l_exp_time > 2998)                  //(3200 - 191 - 11)
1096*4882a593Smuzhiyun 		l_exp_time = 2998;
1097*4882a593Smuzhiyun 	if (s_exp_time > 182)                //(191 - 9)
1098*4882a593Smuzhiyun 		s_exp_time = 182;
1099*4882a593Smuzhiyun 
1100*4882a593Smuzhiyun 	ret = SC301IOT_write_reg(SC301IOT->client,
1101*4882a593Smuzhiyun 				SC301IOT_REG_EXPOSURE_H,
1102*4882a593Smuzhiyun 				SC301IOT_REG_VALUE_08BIT,
1103*4882a593Smuzhiyun 				SC301IOT_FETCH_EXP_H(l_exp_time));
1104*4882a593Smuzhiyun 	ret |= SC301IOT_write_reg(SC301IOT->client,
1105*4882a593Smuzhiyun 				 SC301IOT_REG_EXPOSURE_M,
1106*4882a593Smuzhiyun 				 SC301IOT_REG_VALUE_08BIT,
1107*4882a593Smuzhiyun 				 SC301IOT_FETCH_EXP_M(l_exp_time));
1108*4882a593Smuzhiyun 	ret |= SC301IOT_write_reg(SC301IOT->client,
1109*4882a593Smuzhiyun 				 SC301IOT_REG_EXPOSURE_L,
1110*4882a593Smuzhiyun 				 SC301IOT_REG_VALUE_08BIT,
1111*4882a593Smuzhiyun 				 SC301IOT_FETCH_EXP_L(l_exp_time));
1112*4882a593Smuzhiyun 	ret |= SC301IOT_write_reg(SC301IOT->client,
1113*4882a593Smuzhiyun 				 SC301IOT_REG_SEXPOSURE_M,
1114*4882a593Smuzhiyun 				 SC301IOT_REG_VALUE_08BIT,
1115*4882a593Smuzhiyun 				 SC301IOT_FETCH_EXP_M(s_exp_time));
1116*4882a593Smuzhiyun 	ret |= SC301IOT_write_reg(SC301IOT->client,
1117*4882a593Smuzhiyun 				 SC301IOT_REG_SEXPOSURE_L,
1118*4882a593Smuzhiyun 				 SC301IOT_REG_VALUE_08BIT,
1119*4882a593Smuzhiyun 				 SC301IOT_FETCH_EXP_L(s_exp_time));
1120*4882a593Smuzhiyun 
1121*4882a593Smuzhiyun 
1122*4882a593Smuzhiyun 	ret |= SC301IOT_set_gain_reg(SC301IOT, l_a_gain, SC301IOT_LGAIN);
1123*4882a593Smuzhiyun 	ret |= SC301IOT_set_gain_reg(SC301IOT, s_a_gain, SC301IOT_SGAIN);
1124*4882a593Smuzhiyun 	return ret;
1125*4882a593Smuzhiyun }
1126*4882a593Smuzhiyun 
SC301IOT_get_reso_dist(const struct SC301IOT_mode * mode,struct v4l2_mbus_framefmt * framefmt)1127*4882a593Smuzhiyun static int SC301IOT_get_reso_dist(const struct SC301IOT_mode *mode,
1128*4882a593Smuzhiyun 				 struct v4l2_mbus_framefmt *framefmt)
1129*4882a593Smuzhiyun {
1130*4882a593Smuzhiyun 	return abs(mode->width - framefmt->width) +
1131*4882a593Smuzhiyun 	       abs(mode->height - framefmt->height);
1132*4882a593Smuzhiyun }
1133*4882a593Smuzhiyun 
1134*4882a593Smuzhiyun static const struct SC301IOT_mode *
SC301IOT_find_best_fit(struct v4l2_subdev_format * fmt)1135*4882a593Smuzhiyun SC301IOT_find_best_fit(struct v4l2_subdev_format *fmt)
1136*4882a593Smuzhiyun {
1137*4882a593Smuzhiyun 	struct v4l2_mbus_framefmt *framefmt = &fmt->format;
1138*4882a593Smuzhiyun 	int dist;
1139*4882a593Smuzhiyun 	int cur_best_fit = 0;
1140*4882a593Smuzhiyun 	int cur_best_fit_dist = -1;
1141*4882a593Smuzhiyun 	unsigned int i;
1142*4882a593Smuzhiyun 
1143*4882a593Smuzhiyun 	for (i = 0; i < ARRAY_SIZE(supported_modes); i++) {
1144*4882a593Smuzhiyun 		dist = SC301IOT_get_reso_dist(&supported_modes[i], framefmt);
1145*4882a593Smuzhiyun 		if (cur_best_fit_dist == -1 || dist < cur_best_fit_dist) {
1146*4882a593Smuzhiyun 			cur_best_fit_dist = dist;
1147*4882a593Smuzhiyun 			cur_best_fit = i;
1148*4882a593Smuzhiyun 		}
1149*4882a593Smuzhiyun 	}
1150*4882a593Smuzhiyun 
1151*4882a593Smuzhiyun 	return &supported_modes[cur_best_fit];
1152*4882a593Smuzhiyun }
1153*4882a593Smuzhiyun 
SC301IOT_set_fmt(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_format * fmt)1154*4882a593Smuzhiyun static int SC301IOT_set_fmt(struct v4l2_subdev *sd,
1155*4882a593Smuzhiyun 			   struct v4l2_subdev_pad_config *cfg,
1156*4882a593Smuzhiyun 			   struct v4l2_subdev_format *fmt)
1157*4882a593Smuzhiyun {
1158*4882a593Smuzhiyun 	struct SC301IOT *SC301IOT = to_SC301IOT(sd);
1159*4882a593Smuzhiyun 	const struct SC301IOT_mode *mode;
1160*4882a593Smuzhiyun 	s64 h_blank, vblank_def;
1161*4882a593Smuzhiyun 
1162*4882a593Smuzhiyun 	mutex_lock(&SC301IOT->mutex);
1163*4882a593Smuzhiyun 
1164*4882a593Smuzhiyun 	mode = SC301IOT_find_best_fit(fmt);
1165*4882a593Smuzhiyun 	fmt->format.code = mode->bus_fmt;
1166*4882a593Smuzhiyun 	fmt->format.width = mode->width;
1167*4882a593Smuzhiyun 	fmt->format.height = mode->height;
1168*4882a593Smuzhiyun 	fmt->format.field = V4L2_FIELD_NONE;
1169*4882a593Smuzhiyun 	if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1170*4882a593Smuzhiyun #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
1171*4882a593Smuzhiyun 		*v4l2_subdev_get_try_format(sd, cfg, fmt->pad) = fmt->format;
1172*4882a593Smuzhiyun #else
1173*4882a593Smuzhiyun 		mutex_unlock(&SC301IOT->mutex);
1174*4882a593Smuzhiyun 		return -ENOTTY;
1175*4882a593Smuzhiyun #endif
1176*4882a593Smuzhiyun 	} else {
1177*4882a593Smuzhiyun 		SC301IOT->cur_mode = mode;
1178*4882a593Smuzhiyun 		h_blank = mode->hts_def - mode->width;
1179*4882a593Smuzhiyun 		__v4l2_ctrl_modify_range(SC301IOT->hblank, h_blank,
1180*4882a593Smuzhiyun 					 h_blank, 1, h_blank);
1181*4882a593Smuzhiyun 		vblank_def = mode->vts_def - mode->height;
1182*4882a593Smuzhiyun 		__v4l2_ctrl_modify_range(SC301IOT->vblank, vblank_def,
1183*4882a593Smuzhiyun 					 SC301IOT_VTS_MAX - mode->height,
1184*4882a593Smuzhiyun 					 1, vblank_def);
1185*4882a593Smuzhiyun 		SC301IOT->cur_fps = mode->max_fps;
1186*4882a593Smuzhiyun 		SC301IOT->cur_vts = mode->vts_def;
1187*4882a593Smuzhiyun 	}
1188*4882a593Smuzhiyun 
1189*4882a593Smuzhiyun 	mutex_unlock(&SC301IOT->mutex);
1190*4882a593Smuzhiyun 
1191*4882a593Smuzhiyun 	return 0;
1192*4882a593Smuzhiyun }
1193*4882a593Smuzhiyun 
SC301IOT_get_fmt(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_format * fmt)1194*4882a593Smuzhiyun static int SC301IOT_get_fmt(struct v4l2_subdev *sd,
1195*4882a593Smuzhiyun 			   struct v4l2_subdev_pad_config *cfg,
1196*4882a593Smuzhiyun 			   struct v4l2_subdev_format *fmt)
1197*4882a593Smuzhiyun {
1198*4882a593Smuzhiyun 	struct SC301IOT *SC301IOT = to_SC301IOT(sd);
1199*4882a593Smuzhiyun 	const struct SC301IOT_mode *mode = SC301IOT->cur_mode;
1200*4882a593Smuzhiyun 
1201*4882a593Smuzhiyun 	mutex_lock(&SC301IOT->mutex);
1202*4882a593Smuzhiyun 	if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1203*4882a593Smuzhiyun #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
1204*4882a593Smuzhiyun 		fmt->format = *v4l2_subdev_get_try_format(sd, cfg, fmt->pad);
1205*4882a593Smuzhiyun #else
1206*4882a593Smuzhiyun 		mutex_unlock(&SC301IOT->mutex);
1207*4882a593Smuzhiyun 		return -ENOTTY;
1208*4882a593Smuzhiyun #endif
1209*4882a593Smuzhiyun 	} else {
1210*4882a593Smuzhiyun 		fmt->format.width = mode->width;
1211*4882a593Smuzhiyun 		fmt->format.height = mode->height;
1212*4882a593Smuzhiyun 		fmt->format.code = mode->bus_fmt;
1213*4882a593Smuzhiyun 		fmt->format.field = V4L2_FIELD_NONE;
1214*4882a593Smuzhiyun 		/* format info: width/height/data type/virctual channel */
1215*4882a593Smuzhiyun 		if (fmt->pad < PAD_MAX && mode->hdr_mode != NO_HDR)
1216*4882a593Smuzhiyun 			fmt->reserved[0] = mode->vc[fmt->pad];
1217*4882a593Smuzhiyun 		else
1218*4882a593Smuzhiyun 			fmt->reserved[0] = mode->vc[PAD0];
1219*4882a593Smuzhiyun 	}
1220*4882a593Smuzhiyun 	mutex_unlock(&SC301IOT->mutex);
1221*4882a593Smuzhiyun 	return 0;
1222*4882a593Smuzhiyun }
1223*4882a593Smuzhiyun 
SC301IOT_enum_mbus_code(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_mbus_code_enum * code)1224*4882a593Smuzhiyun static int SC301IOT_enum_mbus_code(struct v4l2_subdev *sd,
1225*4882a593Smuzhiyun 				  struct v4l2_subdev_pad_config *cfg,
1226*4882a593Smuzhiyun 				  struct v4l2_subdev_mbus_code_enum *code)
1227*4882a593Smuzhiyun {
1228*4882a593Smuzhiyun 	struct SC301IOT *SC301IOT = to_SC301IOT(sd);
1229*4882a593Smuzhiyun 
1230*4882a593Smuzhiyun 	if (code->index != 0)
1231*4882a593Smuzhiyun 		return -EINVAL;
1232*4882a593Smuzhiyun 	code->code = SC301IOT->cur_mode->bus_fmt;
1233*4882a593Smuzhiyun 
1234*4882a593Smuzhiyun 	return 0;
1235*4882a593Smuzhiyun }
1236*4882a593Smuzhiyun 
SC301IOT_enum_frame_sizes(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_frame_size_enum * fse)1237*4882a593Smuzhiyun static int SC301IOT_enum_frame_sizes(struct v4l2_subdev *sd,
1238*4882a593Smuzhiyun 				    struct v4l2_subdev_pad_config *cfg,
1239*4882a593Smuzhiyun 				    struct v4l2_subdev_frame_size_enum *fse)
1240*4882a593Smuzhiyun {
1241*4882a593Smuzhiyun 	if (fse->index >= ARRAY_SIZE(supported_modes))
1242*4882a593Smuzhiyun 		return -EINVAL;
1243*4882a593Smuzhiyun 
1244*4882a593Smuzhiyun 	if (fse->code != supported_modes[0].bus_fmt)
1245*4882a593Smuzhiyun 		return -EINVAL;
1246*4882a593Smuzhiyun 
1247*4882a593Smuzhiyun 	fse->min_width  = supported_modes[fse->index].width;
1248*4882a593Smuzhiyun 	fse->max_width  = supported_modes[fse->index].width;
1249*4882a593Smuzhiyun 	fse->max_height = supported_modes[fse->index].height;
1250*4882a593Smuzhiyun 	fse->min_height = supported_modes[fse->index].height;
1251*4882a593Smuzhiyun 
1252*4882a593Smuzhiyun 	return 0;
1253*4882a593Smuzhiyun }
1254*4882a593Smuzhiyun 
SC301IOT_enable_test_pattern(struct SC301IOT * SC301IOT,u32 pattern)1255*4882a593Smuzhiyun static int SC301IOT_enable_test_pattern(struct SC301IOT *SC301IOT, u32 pattern)
1256*4882a593Smuzhiyun {
1257*4882a593Smuzhiyun 	u32 val = 0;
1258*4882a593Smuzhiyun 	int ret = 0;
1259*4882a593Smuzhiyun 
1260*4882a593Smuzhiyun 	ret = SC301IOT_read_reg(SC301IOT->client, SC301IOT_REG_TEST_PATTERN,
1261*4882a593Smuzhiyun 			       SC301IOT_REG_VALUE_08BIT, &val);
1262*4882a593Smuzhiyun 	if (pattern)
1263*4882a593Smuzhiyun 		val |= SC301IOT_TEST_PATTERN_BIT_MASK;
1264*4882a593Smuzhiyun 	else
1265*4882a593Smuzhiyun 		val &= ~SC301IOT_TEST_PATTERN_BIT_MASK;
1266*4882a593Smuzhiyun 
1267*4882a593Smuzhiyun 	ret |= SC301IOT_write_reg(SC301IOT->client, SC301IOT_REG_TEST_PATTERN,
1268*4882a593Smuzhiyun 				 SC301IOT_REG_VALUE_08BIT, val);
1269*4882a593Smuzhiyun 	return ret;
1270*4882a593Smuzhiyun }
1271*4882a593Smuzhiyun 
SC301IOT_g_frame_interval(struct v4l2_subdev * sd,struct v4l2_subdev_frame_interval * fi)1272*4882a593Smuzhiyun static int SC301IOT_g_frame_interval(struct v4l2_subdev *sd,
1273*4882a593Smuzhiyun 				    struct v4l2_subdev_frame_interval *fi)
1274*4882a593Smuzhiyun {
1275*4882a593Smuzhiyun 	struct SC301IOT *SC301IOT = to_SC301IOT(sd);
1276*4882a593Smuzhiyun 	const struct SC301IOT_mode *mode = SC301IOT->cur_mode;
1277*4882a593Smuzhiyun 
1278*4882a593Smuzhiyun 	if (SC301IOT->streaming)
1279*4882a593Smuzhiyun 		fi->interval = SC301IOT->cur_fps;
1280*4882a593Smuzhiyun 	else
1281*4882a593Smuzhiyun 		fi->interval = mode->max_fps;
1282*4882a593Smuzhiyun 
1283*4882a593Smuzhiyun 	return 0;
1284*4882a593Smuzhiyun }
1285*4882a593Smuzhiyun 
SC301IOT_g_mbus_config(struct v4l2_subdev * sd,unsigned int pad_id,struct v4l2_mbus_config * config)1286*4882a593Smuzhiyun static int SC301IOT_g_mbus_config(struct v4l2_subdev *sd, unsigned int pad_id,
1287*4882a593Smuzhiyun 				 struct v4l2_mbus_config *config)
1288*4882a593Smuzhiyun {
1289*4882a593Smuzhiyun 	struct SC301IOT *SC301IOT = to_SC301IOT(sd);
1290*4882a593Smuzhiyun 	const struct SC301IOT_mode *mode = SC301IOT->cur_mode;
1291*4882a593Smuzhiyun 	u32 val = 1 << (SC301IOT_LANES - 1) |
1292*4882a593Smuzhiyun 		V4L2_MBUS_CSI2_CHANNEL_0 |
1293*4882a593Smuzhiyun 		V4L2_MBUS_CSI2_CONTINUOUS_CLOCK;
1294*4882a593Smuzhiyun 
1295*4882a593Smuzhiyun 	if (mode->hdr_mode != NO_HDR)
1296*4882a593Smuzhiyun 		val |= V4L2_MBUS_CSI2_CHANNEL_1;
1297*4882a593Smuzhiyun 	if (mode->hdr_mode == HDR_X3)
1298*4882a593Smuzhiyun 		val |= V4L2_MBUS_CSI2_CHANNEL_2;
1299*4882a593Smuzhiyun 
1300*4882a593Smuzhiyun 	config->type = V4L2_MBUS_CSI2_DPHY;
1301*4882a593Smuzhiyun 	config->flags = val;
1302*4882a593Smuzhiyun 
1303*4882a593Smuzhiyun 	return 0;
1304*4882a593Smuzhiyun }
1305*4882a593Smuzhiyun 
SC301IOT_get_module_inf(struct SC301IOT * SC301IOT,struct rkmodule_inf * inf)1306*4882a593Smuzhiyun static void SC301IOT_get_module_inf(struct SC301IOT *SC301IOT,
1307*4882a593Smuzhiyun 				   struct rkmodule_inf *inf)
1308*4882a593Smuzhiyun {
1309*4882a593Smuzhiyun 	memset(inf, 0, sizeof(*inf));
1310*4882a593Smuzhiyun 	strscpy(inf->base.sensor, SC301IOT_NAME, sizeof(inf->base.sensor));
1311*4882a593Smuzhiyun 	strscpy(inf->base.module, SC301IOT->module_name,
1312*4882a593Smuzhiyun 		sizeof(inf->base.module));
1313*4882a593Smuzhiyun 	strscpy(inf->base.lens, SC301IOT->len_name, sizeof(inf->base.lens));
1314*4882a593Smuzhiyun }
1315*4882a593Smuzhiyun 
SC301IOT_get_channel_info(struct SC301IOT * SC301IOT,struct rkmodule_channel_info * ch_info)1316*4882a593Smuzhiyun static int SC301IOT_get_channel_info(struct SC301IOT *SC301IOT,
1317*4882a593Smuzhiyun 					    struct rkmodule_channel_info *ch_info)
1318*4882a593Smuzhiyun {
1319*4882a593Smuzhiyun 	if (ch_info->index < PAD0 || ch_info->index >= PAD_MAX)
1320*4882a593Smuzhiyun 		return -EINVAL;
1321*4882a593Smuzhiyun 	ch_info->vc = SC301IOT->cur_mode->vc[ch_info->index];
1322*4882a593Smuzhiyun 	ch_info->width = SC301IOT->cur_mode->width;
1323*4882a593Smuzhiyun 	ch_info->height = SC301IOT->cur_mode->height;
1324*4882a593Smuzhiyun 	ch_info->bus_fmt = SC301IOT->cur_mode->bus_fmt;
1325*4882a593Smuzhiyun 	return 0;
1326*4882a593Smuzhiyun }
1327*4882a593Smuzhiyun 
SC301IOT_ioctl(struct v4l2_subdev * sd,unsigned int cmd,void * arg)1328*4882a593Smuzhiyun static long SC301IOT_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
1329*4882a593Smuzhiyun {
1330*4882a593Smuzhiyun 	struct SC301IOT *SC301IOT = to_SC301IOT(sd);
1331*4882a593Smuzhiyun 	struct rkmodule_hdr_cfg *hdr;
1332*4882a593Smuzhiyun 	struct rkmodule_channel_info *ch_info;
1333*4882a593Smuzhiyun 	u32 i, h, w;
1334*4882a593Smuzhiyun 	long ret = 0;
1335*4882a593Smuzhiyun 	u32 stream = 0;
1336*4882a593Smuzhiyun 	u32 sync_mode = 4;
1337*4882a593Smuzhiyun 
1338*4882a593Smuzhiyun 	switch (cmd) {
1339*4882a593Smuzhiyun 	case RKMODULE_GET_MODULE_INFO:
1340*4882a593Smuzhiyun 		SC301IOT_get_module_inf(SC301IOT, (struct rkmodule_inf *)arg);
1341*4882a593Smuzhiyun 		break;
1342*4882a593Smuzhiyun 	case RKMODULE_GET_HDR_CFG:
1343*4882a593Smuzhiyun 		hdr = (struct rkmodule_hdr_cfg *)arg;
1344*4882a593Smuzhiyun 		hdr->esp.mode = HDR_NORMAL_VC;
1345*4882a593Smuzhiyun 		hdr->hdr_mode = SC301IOT->cur_mode->hdr_mode;
1346*4882a593Smuzhiyun 		break;
1347*4882a593Smuzhiyun 	case RKMODULE_SET_HDR_CFG:
1348*4882a593Smuzhiyun 		hdr = (struct rkmodule_hdr_cfg *)arg;
1349*4882a593Smuzhiyun 		w = SC301IOT->cur_mode->width;
1350*4882a593Smuzhiyun 		h = SC301IOT->cur_mode->height;
1351*4882a593Smuzhiyun 		for (i = 0; i < ARRAY_SIZE(supported_modes); i++) {
1352*4882a593Smuzhiyun 			if (w == supported_modes[i].width &&
1353*4882a593Smuzhiyun 			    h == supported_modes[i].height &&
1354*4882a593Smuzhiyun 			    supported_modes[i].hdr_mode == hdr->hdr_mode) {
1355*4882a593Smuzhiyun 				SC301IOT->cur_mode = &supported_modes[i];
1356*4882a593Smuzhiyun 				break;
1357*4882a593Smuzhiyun 			}
1358*4882a593Smuzhiyun 		}
1359*4882a593Smuzhiyun 		if (i == ARRAY_SIZE(supported_modes)) {
1360*4882a593Smuzhiyun 			dev_err(&SC301IOT->client->dev,
1361*4882a593Smuzhiyun 				"not find hdr mode:%d %dx%d config\n",
1362*4882a593Smuzhiyun 				hdr->hdr_mode, w, h);
1363*4882a593Smuzhiyun 			ret = -EINVAL;
1364*4882a593Smuzhiyun 		} else {
1365*4882a593Smuzhiyun 			w = SC301IOT->cur_mode->hts_def - SC301IOT->cur_mode->width;
1366*4882a593Smuzhiyun 			h = SC301IOT->cur_mode->vts_def - SC301IOT->cur_mode->height;
1367*4882a593Smuzhiyun 			__v4l2_ctrl_modify_range(SC301IOT->hblank, w, w, 1, w);
1368*4882a593Smuzhiyun 			__v4l2_ctrl_modify_range(SC301IOT->vblank,
1369*4882a593Smuzhiyun 				h,
1370*4882a593Smuzhiyun 				SC301IOT_VTS_MAX - SC301IOT->cur_mode->height, 1, h);
1371*4882a593Smuzhiyun 			SC301IOT->cur_fps = SC301IOT->cur_mode->max_fps;
1372*4882a593Smuzhiyun 			SC301IOT->cur_vts = SC301IOT->cur_mode->vts_def;
1373*4882a593Smuzhiyun 		}
1374*4882a593Smuzhiyun 		break;
1375*4882a593Smuzhiyun 	case PREISP_CMD_SET_HDRAE_EXP:
1376*4882a593Smuzhiyun 		SC301IOT_set_hdrae(SC301IOT, arg);
1377*4882a593Smuzhiyun 		break;
1378*4882a593Smuzhiyun 	case RKMODULE_SET_QUICK_STREAM:
1379*4882a593Smuzhiyun 
1380*4882a593Smuzhiyun 		stream = *((u32 *)arg);
1381*4882a593Smuzhiyun 
1382*4882a593Smuzhiyun 		if (stream)
1383*4882a593Smuzhiyun 			ret = SC301IOT_write_reg(SC301IOT->client, SC301IOT_REG_CTRL_MODE,
1384*4882a593Smuzhiyun 				 SC301IOT_REG_VALUE_08BIT, SC301IOT_MODE_STREAMING);
1385*4882a593Smuzhiyun 		else
1386*4882a593Smuzhiyun 			ret = SC301IOT_write_reg(SC301IOT->client, SC301IOT_REG_CTRL_MODE,
1387*4882a593Smuzhiyun 				 SC301IOT_REG_VALUE_08BIT, SC301IOT_MODE_SW_STANDBY);
1388*4882a593Smuzhiyun 		break;
1389*4882a593Smuzhiyun 	case RKMODULE_GET_SYNC_MODE:
1390*4882a593Smuzhiyun 		*((u32 *)arg) = SC301IOT->sync_mode;
1391*4882a593Smuzhiyun 		break;
1392*4882a593Smuzhiyun 	case RKMODULE_SET_SYNC_MODE:
1393*4882a593Smuzhiyun 		sync_mode = *((u32 *)arg);
1394*4882a593Smuzhiyun 		if (sync_mode > 3)
1395*4882a593Smuzhiyun 			break;
1396*4882a593Smuzhiyun 		SC301IOT->sync_mode = sync_mode;
1397*4882a593Smuzhiyun 		dev_info(&SC301IOT->client->dev, "sync_mode = [%u]\n", SC301IOT->sync_mode);
1398*4882a593Smuzhiyun 		break;
1399*4882a593Smuzhiyun 	case RKMODULE_GET_CHANNEL_INFO:
1400*4882a593Smuzhiyun 		ch_info = (struct rkmodule_channel_info *)arg;
1401*4882a593Smuzhiyun 		ret = SC301IOT_get_channel_info(SC301IOT, ch_info);
1402*4882a593Smuzhiyun 		break;
1403*4882a593Smuzhiyun 	default:
1404*4882a593Smuzhiyun 		ret = -ENOIOCTLCMD;
1405*4882a593Smuzhiyun 		break;
1406*4882a593Smuzhiyun 	}
1407*4882a593Smuzhiyun 
1408*4882a593Smuzhiyun 	return ret;
1409*4882a593Smuzhiyun }
1410*4882a593Smuzhiyun 
1411*4882a593Smuzhiyun #ifdef CONFIG_COMPAT
SC301IOT_compat_ioctl32(struct v4l2_subdev * sd,unsigned int cmd,unsigned long arg)1412*4882a593Smuzhiyun static long SC301IOT_compat_ioctl32(struct v4l2_subdev *sd,
1413*4882a593Smuzhiyun 				   unsigned int cmd, unsigned long arg)
1414*4882a593Smuzhiyun {
1415*4882a593Smuzhiyun 	void __user *up = compat_ptr(arg);
1416*4882a593Smuzhiyun 	struct rkmodule_inf *inf;
1417*4882a593Smuzhiyun 	struct rkmodule_awb_cfg *cfg;
1418*4882a593Smuzhiyun 	struct rkmodule_hdr_cfg *hdr;
1419*4882a593Smuzhiyun 	struct preisp_hdrae_exp_s *hdrae;
1420*4882a593Smuzhiyun 	struct rkmodule_channel_info *ch_info;
1421*4882a593Smuzhiyun 	long ret;
1422*4882a593Smuzhiyun 	u32 stream = 0;
1423*4882a593Smuzhiyun 
1424*4882a593Smuzhiyun 	switch (cmd) {
1425*4882a593Smuzhiyun 	case RKMODULE_GET_MODULE_INFO:
1426*4882a593Smuzhiyun 		inf = kzalloc(sizeof(*inf), GFP_KERNEL);
1427*4882a593Smuzhiyun 		if (!inf) {
1428*4882a593Smuzhiyun 			ret = -ENOMEM;
1429*4882a593Smuzhiyun 			return ret;
1430*4882a593Smuzhiyun 		}
1431*4882a593Smuzhiyun 
1432*4882a593Smuzhiyun 		ret = SC301IOT_ioctl(sd, cmd, inf);
1433*4882a593Smuzhiyun 		if (!ret) {
1434*4882a593Smuzhiyun 			if (copy_to_user(up, inf, sizeof(*inf))) {
1435*4882a593Smuzhiyun 				kfree(inf);
1436*4882a593Smuzhiyun 				return -EFAULT;
1437*4882a593Smuzhiyun 			}
1438*4882a593Smuzhiyun 		}
1439*4882a593Smuzhiyun 		kfree(inf);
1440*4882a593Smuzhiyun 		break;
1441*4882a593Smuzhiyun 	case RKMODULE_AWB_CFG:
1442*4882a593Smuzhiyun 		cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
1443*4882a593Smuzhiyun 		if (!cfg) {
1444*4882a593Smuzhiyun 			ret = -ENOMEM;
1445*4882a593Smuzhiyun 			return ret;
1446*4882a593Smuzhiyun 		}
1447*4882a593Smuzhiyun 		if (copy_from_user(cfg, up, sizeof(*cfg))) {
1448*4882a593Smuzhiyun 			kfree(cfg);
1449*4882a593Smuzhiyun 			return -EFAULT;
1450*4882a593Smuzhiyun 		}
1451*4882a593Smuzhiyun 		ret = SC301IOT_ioctl(sd, cmd, cfg);
1452*4882a593Smuzhiyun 		kfree(cfg);
1453*4882a593Smuzhiyun 		break;
1454*4882a593Smuzhiyun 	case RKMODULE_GET_HDR_CFG:
1455*4882a593Smuzhiyun 		hdr = kzalloc(sizeof(*hdr), GFP_KERNEL);
1456*4882a593Smuzhiyun 		if (!hdr) {
1457*4882a593Smuzhiyun 			ret = -ENOMEM;
1458*4882a593Smuzhiyun 			return ret;
1459*4882a593Smuzhiyun 		}
1460*4882a593Smuzhiyun 
1461*4882a593Smuzhiyun 		ret = SC301IOT_ioctl(sd, cmd, hdr);
1462*4882a593Smuzhiyun 		if (!ret) {
1463*4882a593Smuzhiyun 			if (copy_to_user(up, hdr, sizeof(*hdr))) {
1464*4882a593Smuzhiyun 				kfree(hdr);
1465*4882a593Smuzhiyun 				return -EFAULT;
1466*4882a593Smuzhiyun 			}
1467*4882a593Smuzhiyun 		}
1468*4882a593Smuzhiyun 		kfree(hdr);
1469*4882a593Smuzhiyun 		break;
1470*4882a593Smuzhiyun 	case RKMODULE_SET_HDR_CFG:
1471*4882a593Smuzhiyun 		hdr = kzalloc(sizeof(*hdr), GFP_KERNEL);
1472*4882a593Smuzhiyun 		if (!hdr) {
1473*4882a593Smuzhiyun 			ret = -ENOMEM;
1474*4882a593Smuzhiyun 			return ret;
1475*4882a593Smuzhiyun 		}
1476*4882a593Smuzhiyun 		if (copy_from_user(hdr, up, sizeof(*hdr))) {
1477*4882a593Smuzhiyun 			kfree(hdr);
1478*4882a593Smuzhiyun 			return -EFAULT;
1479*4882a593Smuzhiyun 		}
1480*4882a593Smuzhiyun 		ret = SC301IOT_ioctl(sd, cmd, hdr);
1481*4882a593Smuzhiyun 		kfree(hdr);
1482*4882a593Smuzhiyun 		break;
1483*4882a593Smuzhiyun 	case PREISP_CMD_SET_HDRAE_EXP:
1484*4882a593Smuzhiyun 		hdrae = kzalloc(sizeof(*hdrae), GFP_KERNEL);
1485*4882a593Smuzhiyun 		if (!hdrae) {
1486*4882a593Smuzhiyun 			ret = -ENOMEM;
1487*4882a593Smuzhiyun 			return ret;
1488*4882a593Smuzhiyun 		}
1489*4882a593Smuzhiyun 		if (copy_from_user(hdrae, up, sizeof(*hdrae))) {
1490*4882a593Smuzhiyun 			kfree(hdrae);
1491*4882a593Smuzhiyun 			return -EFAULT;
1492*4882a593Smuzhiyun 		}
1493*4882a593Smuzhiyun 		ret = SC301IOT_ioctl(sd, cmd, hdrae);
1494*4882a593Smuzhiyun 		kfree(hdrae);
1495*4882a593Smuzhiyun 		break;
1496*4882a593Smuzhiyun 	case RKMODULE_SET_QUICK_STREAM:
1497*4882a593Smuzhiyun 		if (copy_from_user(&stream, up, sizeof(u32)))
1498*4882a593Smuzhiyun 			return -EFAULT;
1499*4882a593Smuzhiyun 		ret = SC301IOT_ioctl(sd, cmd, &stream);
1500*4882a593Smuzhiyun 		break;
1501*4882a593Smuzhiyun 	case RKMODULE_GET_CHANNEL_INFO:
1502*4882a593Smuzhiyun 		ch_info = kzalloc(sizeof(*ch_info), GFP_KERNEL);
1503*4882a593Smuzhiyun 		if (!ch_info) {
1504*4882a593Smuzhiyun 			ret = -ENOMEM;
1505*4882a593Smuzhiyun 			return ret;
1506*4882a593Smuzhiyun 		}
1507*4882a593Smuzhiyun 
1508*4882a593Smuzhiyun 		ret = SC301IOT_ioctl(sd, cmd, ch_info);
1509*4882a593Smuzhiyun 		if (!ret) {
1510*4882a593Smuzhiyun 			ret = copy_to_user(up, ch_info, sizeof(*ch_info));
1511*4882a593Smuzhiyun 			if (ret)
1512*4882a593Smuzhiyun 				ret = -EFAULT;
1513*4882a593Smuzhiyun 		}
1514*4882a593Smuzhiyun 		kfree(ch_info);
1515*4882a593Smuzhiyun 		break;
1516*4882a593Smuzhiyun 	default:
1517*4882a593Smuzhiyun 		ret = -ENOIOCTLCMD;
1518*4882a593Smuzhiyun 		break;
1519*4882a593Smuzhiyun 	}
1520*4882a593Smuzhiyun 
1521*4882a593Smuzhiyun 	return ret;
1522*4882a593Smuzhiyun }
1523*4882a593Smuzhiyun #endif
1524*4882a593Smuzhiyun 
SC301IOT_s_frame_interval(struct v4l2_subdev * sd,struct v4l2_subdev_frame_interval * fi)1525*4882a593Smuzhiyun static int SC301IOT_s_frame_interval(struct v4l2_subdev *sd,
1526*4882a593Smuzhiyun 				   struct v4l2_subdev_frame_interval *fi)
1527*4882a593Smuzhiyun {
1528*4882a593Smuzhiyun 	struct SC301IOT *SC301IOT = to_SC301IOT(sd);
1529*4882a593Smuzhiyun 	struct device *dev = sd->dev;
1530*4882a593Smuzhiyun 	int ret = -1;
1531*4882a593Smuzhiyun 	s64 vblank_def;
1532*4882a593Smuzhiyun 	u32 fps_set, current_fps;
1533*4882a593Smuzhiyun 
1534*4882a593Smuzhiyun 	fps_set = DIV_ROUND_CLOSEST(fi->interval.denominator, fi->interval.numerator);
1535*4882a593Smuzhiyun 	dev_info(dev, "%s set fps = %u\n", __func__, fps_set);
1536*4882a593Smuzhiyun 
1537*4882a593Smuzhiyun 	mutex_lock(&SC301IOT->mutex);
1538*4882a593Smuzhiyun 
1539*4882a593Smuzhiyun 	current_fps = DIV_ROUND_CLOSEST(SC301IOT->cur_mode->max_fps.denominator,
1540*4882a593Smuzhiyun 			SC301IOT->cur_mode->max_fps.numerator);
1541*4882a593Smuzhiyun 	vblank_def = SC301IOT->cur_mode->vts_def * current_fps / fps_set -
1542*4882a593Smuzhiyun 						SC301IOT->cur_mode->height;
1543*4882a593Smuzhiyun 	if (SC301IOT->sync_mode == SLAVE_MODE)
1544*4882a593Smuzhiyun 		vblank_def -= 3;  // adjust vts
1545*4882a593Smuzhiyun 	ret = __v4l2_ctrl_s_ctrl(SC301IOT->vblank, vblank_def);
1546*4882a593Smuzhiyun 	mutex_unlock(&SC301IOT->mutex);
1547*4882a593Smuzhiyun 	if (ret < 0)
1548*4882a593Smuzhiyun 		dev_err(dev, "%s __v4l2_ctrl_s_ctrl error - %d\n", __func__, ret);
1549*4882a593Smuzhiyun 	return ret;
1550*4882a593Smuzhiyun }
1551*4882a593Smuzhiyun 
__SC301IOT_start_stream(struct SC301IOT * SC301IOT)1552*4882a593Smuzhiyun static int __SC301IOT_start_stream(struct SC301IOT *SC301IOT)
1553*4882a593Smuzhiyun {
1554*4882a593Smuzhiyun 	int ret;
1555*4882a593Smuzhiyun 
1556*4882a593Smuzhiyun 	if (!SC301IOT->is_thunderboot) {
1557*4882a593Smuzhiyun 		ret = SC301IOT_write_array(SC301IOT->client, SC301IOT->cur_mode->reg_list);
1558*4882a593Smuzhiyun 		if (ret)
1559*4882a593Smuzhiyun 			return ret;
1560*4882a593Smuzhiyun 
1561*4882a593Smuzhiyun 		/* In case these controls are set before streaming */
1562*4882a593Smuzhiyun 		ret = __v4l2_ctrl_handler_setup(&SC301IOT->ctrl_handler);
1563*4882a593Smuzhiyun 		if (ret)
1564*4882a593Smuzhiyun 			return ret;
1565*4882a593Smuzhiyun 		if (SC301IOT->has_init_exp && SC301IOT->cur_mode->hdr_mode != NO_HDR) {
1566*4882a593Smuzhiyun 			ret = SC301IOT_ioctl(&SC301IOT->subdev, PREISP_CMD_SET_HDRAE_EXP,
1567*4882a593Smuzhiyun 					&SC301IOT->init_hdrae_exp);
1568*4882a593Smuzhiyun 			if (ret) {
1569*4882a593Smuzhiyun 				dev_err(&SC301IOT->client->dev,
1570*4882a593Smuzhiyun 					"init exp fail in hdr mode\n");
1571*4882a593Smuzhiyun 				return ret;
1572*4882a593Smuzhiyun 			}
1573*4882a593Smuzhiyun 		}
1574*4882a593Smuzhiyun 
1575*4882a593Smuzhiyun 		if (SC301IOT->sync_mode == SLAVE_MODE) {
1576*4882a593Smuzhiyun 			SC301IOT_write_reg(SC301IOT->client, 0x3222,
1577*4882a593Smuzhiyun 					SC301IOT_REG_VALUE_08BIT, 0x01);
1578*4882a593Smuzhiyun 			SC301IOT_write_reg(SC301IOT->client, 0x3223,
1579*4882a593Smuzhiyun 					SC301IOT_REG_VALUE_08BIT, 0xc8);
1580*4882a593Smuzhiyun 			SC301IOT_write_reg(SC301IOT->client, 0x3225,
1581*4882a593Smuzhiyun 					SC301IOT_REG_VALUE_08BIT, 0x10);
1582*4882a593Smuzhiyun 			SC301IOT_write_reg(SC301IOT->client, 0x322e,
1583*4882a593Smuzhiyun 					SC301IOT_REG_VALUE_08BIT, (SC301IOT->cur_vts - 4) >> 8);
1584*4882a593Smuzhiyun 			SC301IOT_write_reg(SC301IOT->client, 0x322f,
1585*4882a593Smuzhiyun 					SC301IOT_REG_VALUE_08BIT, (SC301IOT->cur_vts - 4) & 0xff);
1586*4882a593Smuzhiyun 		} else if (SC301IOT->sync_mode == NO_SYNC_MODE) {
1587*4882a593Smuzhiyun 			SC301IOT_write_reg(SC301IOT->client, 0x3222,
1588*4882a593Smuzhiyun 						SC301IOT_REG_VALUE_08BIT, 0x00);
1589*4882a593Smuzhiyun 			SC301IOT_write_reg(SC301IOT->client, 0x3223,
1590*4882a593Smuzhiyun 						SC301IOT_REG_VALUE_08BIT, 0xd0);
1591*4882a593Smuzhiyun 			SC301IOT_write_reg(SC301IOT->client, 0x3225,
1592*4882a593Smuzhiyun 						SC301IOT_REG_VALUE_08BIT, 0x00);
1593*4882a593Smuzhiyun 			SC301IOT_write_reg(SC301IOT->client, 0x322e,
1594*4882a593Smuzhiyun 						SC301IOT_REG_VALUE_08BIT, 0x00);
1595*4882a593Smuzhiyun 			SC301IOT_write_reg(SC301IOT->client, 0x322f,
1596*4882a593Smuzhiyun 						SC301IOT_REG_VALUE_08BIT, 0x02);
1597*4882a593Smuzhiyun 		}
1598*4882a593Smuzhiyun 	}
1599*4882a593Smuzhiyun 
1600*4882a593Smuzhiyun 	dev_dbg(&SC301IOT->client->dev, "start stream\n");
1601*4882a593Smuzhiyun 	return SC301IOT_write_reg(SC301IOT->client, SC301IOT_REG_CTRL_MODE,
1602*4882a593Smuzhiyun 				 SC301IOT_REG_VALUE_08BIT, SC301IOT_MODE_STREAMING);
1603*4882a593Smuzhiyun }
1604*4882a593Smuzhiyun 
__SC301IOT_stop_stream(struct SC301IOT * SC301IOT)1605*4882a593Smuzhiyun static int __SC301IOT_stop_stream(struct SC301IOT *SC301IOT)
1606*4882a593Smuzhiyun {
1607*4882a593Smuzhiyun 	SC301IOT->has_init_exp = false;
1608*4882a593Smuzhiyun 	dev_dbg(&SC301IOT->client->dev, "stop stream\n");
1609*4882a593Smuzhiyun 	if (SC301IOT->is_thunderboot)
1610*4882a593Smuzhiyun 		SC301IOT->is_first_streamoff = true;
1611*4882a593Smuzhiyun 	return SC301IOT_write_reg(SC301IOT->client, SC301IOT_REG_CTRL_MODE,
1612*4882a593Smuzhiyun 				 SC301IOT_REG_VALUE_08BIT, SC301IOT_MODE_SW_STANDBY);
1613*4882a593Smuzhiyun }
1614*4882a593Smuzhiyun 
1615*4882a593Smuzhiyun static int __SC301IOT_power_on(struct SC301IOT *SC301IOT);
SC301IOT_s_stream(struct v4l2_subdev * sd,int on)1616*4882a593Smuzhiyun static int SC301IOT_s_stream(struct v4l2_subdev *sd, int on)
1617*4882a593Smuzhiyun {
1618*4882a593Smuzhiyun 	struct SC301IOT *SC301IOT = to_SC301IOT(sd);
1619*4882a593Smuzhiyun 	struct i2c_client *client = SC301IOT->client;
1620*4882a593Smuzhiyun 	int ret = 0;
1621*4882a593Smuzhiyun 
1622*4882a593Smuzhiyun 	mutex_lock(&SC301IOT->mutex);
1623*4882a593Smuzhiyun 	on = !!on;
1624*4882a593Smuzhiyun 	if (on == SC301IOT->streaming)
1625*4882a593Smuzhiyun 		goto unlock_and_return;
1626*4882a593Smuzhiyun 
1627*4882a593Smuzhiyun 	if (on) {
1628*4882a593Smuzhiyun 		if (SC301IOT->is_thunderboot && rkisp_tb_get_state() == RKISP_TB_NG) {
1629*4882a593Smuzhiyun 			SC301IOT->is_thunderboot = false;
1630*4882a593Smuzhiyun 			__SC301IOT_power_on(SC301IOT);
1631*4882a593Smuzhiyun 		}
1632*4882a593Smuzhiyun 
1633*4882a593Smuzhiyun 		ret = pm_runtime_get_sync(&client->dev);
1634*4882a593Smuzhiyun 		if (ret < 0) {
1635*4882a593Smuzhiyun 			pm_runtime_put_noidle(&client->dev);
1636*4882a593Smuzhiyun 			goto unlock_and_return;
1637*4882a593Smuzhiyun 		}
1638*4882a593Smuzhiyun 
1639*4882a593Smuzhiyun 		ret = __SC301IOT_start_stream(SC301IOT);
1640*4882a593Smuzhiyun 		if (ret) {
1641*4882a593Smuzhiyun 			v4l2_err(sd, "start stream failed while write regs\n");
1642*4882a593Smuzhiyun 			pm_runtime_put(&client->dev);
1643*4882a593Smuzhiyun 			goto unlock_and_return;
1644*4882a593Smuzhiyun 		}
1645*4882a593Smuzhiyun 	} else {
1646*4882a593Smuzhiyun 		__SC301IOT_stop_stream(SC301IOT);
1647*4882a593Smuzhiyun 		pm_runtime_put(&client->dev);
1648*4882a593Smuzhiyun 	}
1649*4882a593Smuzhiyun 
1650*4882a593Smuzhiyun 	SC301IOT->streaming = on;
1651*4882a593Smuzhiyun 
1652*4882a593Smuzhiyun unlock_and_return:
1653*4882a593Smuzhiyun 	mutex_unlock(&SC301IOT->mutex);
1654*4882a593Smuzhiyun 
1655*4882a593Smuzhiyun 	return ret;
1656*4882a593Smuzhiyun }
1657*4882a593Smuzhiyun 
SC301IOT_s_power(struct v4l2_subdev * sd,int on)1658*4882a593Smuzhiyun static int SC301IOT_s_power(struct v4l2_subdev *sd, int on)
1659*4882a593Smuzhiyun {
1660*4882a593Smuzhiyun 	struct SC301IOT *SC301IOT = to_SC301IOT(sd);
1661*4882a593Smuzhiyun 	struct i2c_client *client = SC301IOT->client;
1662*4882a593Smuzhiyun 	int ret = 0;
1663*4882a593Smuzhiyun 
1664*4882a593Smuzhiyun 	mutex_lock(&SC301IOT->mutex);
1665*4882a593Smuzhiyun 
1666*4882a593Smuzhiyun 	/* If the power state is not modified - no work to do. */
1667*4882a593Smuzhiyun 	if (SC301IOT->power_on == !!on)
1668*4882a593Smuzhiyun 		goto unlock_and_return;
1669*4882a593Smuzhiyun 
1670*4882a593Smuzhiyun 	if (on) {
1671*4882a593Smuzhiyun 		ret = pm_runtime_get_sync(&client->dev);
1672*4882a593Smuzhiyun 		if (ret < 0) {
1673*4882a593Smuzhiyun 			pm_runtime_put_noidle(&client->dev);
1674*4882a593Smuzhiyun 			goto unlock_and_return;
1675*4882a593Smuzhiyun 		}
1676*4882a593Smuzhiyun 
1677*4882a593Smuzhiyun 		if (!SC301IOT->is_thunderboot) {
1678*4882a593Smuzhiyun 			ret = SC301IOT_write_array(SC301IOT->client, SC301IOT_global_regs);
1679*4882a593Smuzhiyun 			if (ret) {
1680*4882a593Smuzhiyun 				v4l2_err(sd, "could not set init registers\n");
1681*4882a593Smuzhiyun 				pm_runtime_put_noidle(&client->dev);
1682*4882a593Smuzhiyun 				goto unlock_and_return;
1683*4882a593Smuzhiyun 			}
1684*4882a593Smuzhiyun 		}
1685*4882a593Smuzhiyun 
1686*4882a593Smuzhiyun 		SC301IOT->power_on = true;
1687*4882a593Smuzhiyun 	} else {
1688*4882a593Smuzhiyun 		pm_runtime_put(&client->dev);
1689*4882a593Smuzhiyun 		SC301IOT->power_on = false;
1690*4882a593Smuzhiyun 	}
1691*4882a593Smuzhiyun 
1692*4882a593Smuzhiyun unlock_and_return:
1693*4882a593Smuzhiyun 	mutex_unlock(&SC301IOT->mutex);
1694*4882a593Smuzhiyun 
1695*4882a593Smuzhiyun 	return ret;
1696*4882a593Smuzhiyun }
1697*4882a593Smuzhiyun 
1698*4882a593Smuzhiyun /* Calculate the delay in us by clock rate and clock cycles */
SC301IOT_cal_delay(u32 cycles)1699*4882a593Smuzhiyun static inline u32 SC301IOT_cal_delay(u32 cycles)
1700*4882a593Smuzhiyun {
1701*4882a593Smuzhiyun 	return DIV_ROUND_UP(cycles, SC301IOT_XVCLK_FREQ / 1000 / 1000);
1702*4882a593Smuzhiyun }
1703*4882a593Smuzhiyun 
__SC301IOT_power_on(struct SC301IOT * SC301IOT)1704*4882a593Smuzhiyun static int __SC301IOT_power_on(struct SC301IOT *SC301IOT)
1705*4882a593Smuzhiyun {
1706*4882a593Smuzhiyun 	int ret;
1707*4882a593Smuzhiyun 	u32 delay_us;
1708*4882a593Smuzhiyun 	struct device *dev = &SC301IOT->client->dev;
1709*4882a593Smuzhiyun 
1710*4882a593Smuzhiyun 	if (!IS_ERR_OR_NULL(SC301IOT->pins_default)) {
1711*4882a593Smuzhiyun 		ret = pinctrl_select_state(SC301IOT->pinctrl,
1712*4882a593Smuzhiyun 					   SC301IOT->pins_default);
1713*4882a593Smuzhiyun 		if (ret < 0)
1714*4882a593Smuzhiyun 			dev_err(dev, "could not set pins\n");
1715*4882a593Smuzhiyun 	}
1716*4882a593Smuzhiyun 	ret = clk_set_rate(SC301IOT->xvclk, SC301IOT_XVCLK_FREQ);
1717*4882a593Smuzhiyun 	if (ret < 0)
1718*4882a593Smuzhiyun 		dev_warn(dev, "Failed to set xvclk rate (24MHz)\n");
1719*4882a593Smuzhiyun 	if (clk_get_rate(SC301IOT->xvclk) != SC301IOT_XVCLK_FREQ)
1720*4882a593Smuzhiyun 		dev_warn(dev, "xvclk mismatched, modes are based on 24MHz\n");
1721*4882a593Smuzhiyun 	ret = clk_prepare_enable(SC301IOT->xvclk);
1722*4882a593Smuzhiyun 	if (ret < 0) {
1723*4882a593Smuzhiyun 		dev_err(dev, "Failed to enable xvclk\n");
1724*4882a593Smuzhiyun 		goto disable_clk;
1725*4882a593Smuzhiyun 	}
1726*4882a593Smuzhiyun 	if (SC301IOT->is_thunderboot)
1727*4882a593Smuzhiyun 		return 0;
1728*4882a593Smuzhiyun 	if (!IS_ERR(SC301IOT->reset_gpio))
1729*4882a593Smuzhiyun 		gpiod_set_value_cansleep(SC301IOT->reset_gpio, 0);
1730*4882a593Smuzhiyun 
1731*4882a593Smuzhiyun 	ret = regulator_bulk_enable(SC301IOT_NUM_SUPPLIES, SC301IOT->supplies);
1732*4882a593Smuzhiyun 	if (ret < 0) {
1733*4882a593Smuzhiyun 		dev_err(dev, "Failed to enable regulators\n");
1734*4882a593Smuzhiyun 		goto disable_clk;
1735*4882a593Smuzhiyun 	}
1736*4882a593Smuzhiyun 	if (!IS_ERR(SC301IOT->reset_gpio))
1737*4882a593Smuzhiyun 		gpiod_set_value_cansleep(SC301IOT->reset_gpio, 1);
1738*4882a593Smuzhiyun 
1739*4882a593Smuzhiyun 	usleep_range(500, 1000);
1740*4882a593Smuzhiyun 	if (!IS_ERR(SC301IOT->pwdn_gpio))
1741*4882a593Smuzhiyun 		gpiod_set_value_cansleep(SC301IOT->pwdn_gpio, 1);
1742*4882a593Smuzhiyun 	usleep_range(4500, 5000);
1743*4882a593Smuzhiyun 
1744*4882a593Smuzhiyun 	if (!IS_ERR(SC301IOT->reset_gpio))
1745*4882a593Smuzhiyun 		usleep_range(6000, 8000);
1746*4882a593Smuzhiyun 	else
1747*4882a593Smuzhiyun 		usleep_range(12000, 16000);
1748*4882a593Smuzhiyun 
1749*4882a593Smuzhiyun 	/* 8192 cycles prior to first SCCB transaction */
1750*4882a593Smuzhiyun 	delay_us = SC301IOT_cal_delay(8192);
1751*4882a593Smuzhiyun 	usleep_range(delay_us, delay_us * 2);
1752*4882a593Smuzhiyun 
1753*4882a593Smuzhiyun 	return 0;
1754*4882a593Smuzhiyun 
1755*4882a593Smuzhiyun disable_clk:
1756*4882a593Smuzhiyun 	clk_disable_unprepare(SC301IOT->xvclk);
1757*4882a593Smuzhiyun 
1758*4882a593Smuzhiyun 	return ret;
1759*4882a593Smuzhiyun }
1760*4882a593Smuzhiyun 
__SC301IOT_power_off(struct SC301IOT * SC301IOT)1761*4882a593Smuzhiyun static void __SC301IOT_power_off(struct SC301IOT *SC301IOT)
1762*4882a593Smuzhiyun {
1763*4882a593Smuzhiyun 	int ret;
1764*4882a593Smuzhiyun 	struct device *dev = &SC301IOT->client->dev;
1765*4882a593Smuzhiyun 
1766*4882a593Smuzhiyun 	clk_disable_unprepare(SC301IOT->xvclk);
1767*4882a593Smuzhiyun 	if (SC301IOT->is_thunderboot) {
1768*4882a593Smuzhiyun 		if (SC301IOT->is_first_streamoff) {
1769*4882a593Smuzhiyun 			SC301IOT->is_thunderboot = false;
1770*4882a593Smuzhiyun 			SC301IOT->is_first_streamoff = false;
1771*4882a593Smuzhiyun 		} else {
1772*4882a593Smuzhiyun 			return;
1773*4882a593Smuzhiyun 		}
1774*4882a593Smuzhiyun 	}
1775*4882a593Smuzhiyun 
1776*4882a593Smuzhiyun 	if (!IS_ERR(SC301IOT->pwdn_gpio))
1777*4882a593Smuzhiyun 		gpiod_set_value_cansleep(SC301IOT->pwdn_gpio, 0);
1778*4882a593Smuzhiyun 	if (!IS_ERR(SC301IOT->reset_gpio))
1779*4882a593Smuzhiyun 		gpiod_set_value_cansleep(SC301IOT->reset_gpio, 0);
1780*4882a593Smuzhiyun 	if (!IS_ERR_OR_NULL(SC301IOT->pins_sleep)) {
1781*4882a593Smuzhiyun 		ret = pinctrl_select_state(SC301IOT->pinctrl,
1782*4882a593Smuzhiyun 					   SC301IOT->pins_sleep);
1783*4882a593Smuzhiyun 		if (ret < 0)
1784*4882a593Smuzhiyun 			dev_dbg(dev, "could not set pins\n");
1785*4882a593Smuzhiyun 	}
1786*4882a593Smuzhiyun 	regulator_bulk_disable(SC301IOT_NUM_SUPPLIES, SC301IOT->supplies);
1787*4882a593Smuzhiyun }
1788*4882a593Smuzhiyun 
SC301IOT_runtime_resume(struct device * dev)1789*4882a593Smuzhiyun static int SC301IOT_runtime_resume(struct device *dev)
1790*4882a593Smuzhiyun {
1791*4882a593Smuzhiyun 	struct i2c_client *client = to_i2c_client(dev);
1792*4882a593Smuzhiyun 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
1793*4882a593Smuzhiyun 	struct SC301IOT *SC301IOT = to_SC301IOT(sd);
1794*4882a593Smuzhiyun 
1795*4882a593Smuzhiyun 	return __SC301IOT_power_on(SC301IOT);
1796*4882a593Smuzhiyun }
1797*4882a593Smuzhiyun 
SC301IOT_runtime_suspend(struct device * dev)1798*4882a593Smuzhiyun static int SC301IOT_runtime_suspend(struct device *dev)
1799*4882a593Smuzhiyun {
1800*4882a593Smuzhiyun 	struct i2c_client *client = to_i2c_client(dev);
1801*4882a593Smuzhiyun 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
1802*4882a593Smuzhiyun 	struct SC301IOT *SC301IOT = to_SC301IOT(sd);
1803*4882a593Smuzhiyun 
1804*4882a593Smuzhiyun 	__SC301IOT_power_off(SC301IOT);
1805*4882a593Smuzhiyun 
1806*4882a593Smuzhiyun 	return 0;
1807*4882a593Smuzhiyun }
1808*4882a593Smuzhiyun 
1809*4882a593Smuzhiyun #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
SC301IOT_open(struct v4l2_subdev * sd,struct v4l2_subdev_fh * fh)1810*4882a593Smuzhiyun static int SC301IOT_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
1811*4882a593Smuzhiyun {
1812*4882a593Smuzhiyun 	struct SC301IOT *SC301IOT = to_SC301IOT(sd);
1813*4882a593Smuzhiyun 	struct v4l2_mbus_framefmt *try_fmt =
1814*4882a593Smuzhiyun 				v4l2_subdev_get_try_format(sd, fh->pad, 0);
1815*4882a593Smuzhiyun 	const struct SC301IOT_mode *def_mode = &supported_modes[0];
1816*4882a593Smuzhiyun 
1817*4882a593Smuzhiyun 	mutex_lock(&SC301IOT->mutex);
1818*4882a593Smuzhiyun 	/* Initialize try_fmt */
1819*4882a593Smuzhiyun 	try_fmt->width = def_mode->width;
1820*4882a593Smuzhiyun 	try_fmt->height = def_mode->height;
1821*4882a593Smuzhiyun 	try_fmt->code = def_mode->bus_fmt;
1822*4882a593Smuzhiyun 	try_fmt->field = V4L2_FIELD_NONE;
1823*4882a593Smuzhiyun 
1824*4882a593Smuzhiyun 	mutex_unlock(&SC301IOT->mutex);
1825*4882a593Smuzhiyun 	/* No crop or compose */
1826*4882a593Smuzhiyun 
1827*4882a593Smuzhiyun 	return 0;
1828*4882a593Smuzhiyun }
1829*4882a593Smuzhiyun #endif
1830*4882a593Smuzhiyun 
SC301IOT_enum_frame_interval(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_frame_interval_enum * fie)1831*4882a593Smuzhiyun static int SC301IOT_enum_frame_interval(struct v4l2_subdev *sd,
1832*4882a593Smuzhiyun 				       struct v4l2_subdev_pad_config *cfg,
1833*4882a593Smuzhiyun 				       struct v4l2_subdev_frame_interval_enum *fie)
1834*4882a593Smuzhiyun {
1835*4882a593Smuzhiyun 	if (fie->index >= ARRAY_SIZE(supported_modes))
1836*4882a593Smuzhiyun 		return -EINVAL;
1837*4882a593Smuzhiyun 
1838*4882a593Smuzhiyun 	fie->code = supported_modes[fie->index].bus_fmt;
1839*4882a593Smuzhiyun 	fie->width = supported_modes[fie->index].width;
1840*4882a593Smuzhiyun 	fie->height = supported_modes[fie->index].height;
1841*4882a593Smuzhiyun 	fie->interval = supported_modes[fie->index].max_fps;
1842*4882a593Smuzhiyun 	fie->reserved[0] = supported_modes[fie->index].hdr_mode;
1843*4882a593Smuzhiyun 	return 0;
1844*4882a593Smuzhiyun }
1845*4882a593Smuzhiyun 
1846*4882a593Smuzhiyun static const struct dev_pm_ops SC301IOT_pm_ops = {
1847*4882a593Smuzhiyun 	SET_RUNTIME_PM_OPS(SC301IOT_runtime_suspend,
1848*4882a593Smuzhiyun 			   SC301IOT_runtime_resume, NULL)
1849*4882a593Smuzhiyun };
1850*4882a593Smuzhiyun 
1851*4882a593Smuzhiyun #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
1852*4882a593Smuzhiyun static const struct v4l2_subdev_internal_ops SC301IOT_internal_ops = {
1853*4882a593Smuzhiyun 	.open = SC301IOT_open,
1854*4882a593Smuzhiyun };
1855*4882a593Smuzhiyun #endif
1856*4882a593Smuzhiyun 
1857*4882a593Smuzhiyun static const struct v4l2_subdev_core_ops SC301IOT_core_ops = {
1858*4882a593Smuzhiyun 	.s_power = SC301IOT_s_power,
1859*4882a593Smuzhiyun 	.ioctl = SC301IOT_ioctl,
1860*4882a593Smuzhiyun #ifdef CONFIG_COMPAT
1861*4882a593Smuzhiyun 	.compat_ioctl32 = SC301IOT_compat_ioctl32,
1862*4882a593Smuzhiyun #endif
1863*4882a593Smuzhiyun };
1864*4882a593Smuzhiyun 
1865*4882a593Smuzhiyun static const struct v4l2_subdev_video_ops SC301IOT_video_ops = {
1866*4882a593Smuzhiyun 	.s_stream = SC301IOT_s_stream,
1867*4882a593Smuzhiyun 	.g_frame_interval = SC301IOT_g_frame_interval,
1868*4882a593Smuzhiyun 	.s_frame_interval = SC301IOT_s_frame_interval,
1869*4882a593Smuzhiyun };
1870*4882a593Smuzhiyun 
1871*4882a593Smuzhiyun static const struct v4l2_subdev_pad_ops SC301IOT_pad_ops = {
1872*4882a593Smuzhiyun 	.enum_mbus_code = SC301IOT_enum_mbus_code,
1873*4882a593Smuzhiyun 	.enum_frame_size = SC301IOT_enum_frame_sizes,
1874*4882a593Smuzhiyun 	.enum_frame_interval = SC301IOT_enum_frame_interval,
1875*4882a593Smuzhiyun 	.get_fmt = SC301IOT_get_fmt,
1876*4882a593Smuzhiyun 	.set_fmt = SC301IOT_set_fmt,
1877*4882a593Smuzhiyun 	.get_mbus_config = SC301IOT_g_mbus_config,
1878*4882a593Smuzhiyun };
1879*4882a593Smuzhiyun 
1880*4882a593Smuzhiyun static const struct v4l2_subdev_ops SC301IOT_subdev_ops = {
1881*4882a593Smuzhiyun 	.core	= &SC301IOT_core_ops,
1882*4882a593Smuzhiyun 	.video	= &SC301IOT_video_ops,
1883*4882a593Smuzhiyun 	.pad	= &SC301IOT_pad_ops,
1884*4882a593Smuzhiyun };
1885*4882a593Smuzhiyun 
SC301IOT_modify_fps_info(struct SC301IOT * SC301IOT)1886*4882a593Smuzhiyun static void SC301IOT_modify_fps_info(struct SC301IOT *SC301IOT)
1887*4882a593Smuzhiyun {
1888*4882a593Smuzhiyun 	const struct SC301IOT_mode *mode = SC301IOT->cur_mode;
1889*4882a593Smuzhiyun 
1890*4882a593Smuzhiyun 	SC301IOT->cur_fps.denominator = mode->max_fps.denominator * mode->vts_def /
1891*4882a593Smuzhiyun 					SC301IOT->cur_vts;
1892*4882a593Smuzhiyun }
1893*4882a593Smuzhiyun 
SC301IOT_set_ctrl(struct v4l2_ctrl * ctrl)1894*4882a593Smuzhiyun static int SC301IOT_set_ctrl(struct v4l2_ctrl *ctrl)
1895*4882a593Smuzhiyun {
1896*4882a593Smuzhiyun 	struct SC301IOT *SC301IOT = container_of(ctrl->handler,
1897*4882a593Smuzhiyun 					       struct SC301IOT, ctrl_handler);
1898*4882a593Smuzhiyun 	struct i2c_client *client = SC301IOT->client;
1899*4882a593Smuzhiyun 	s64 max;
1900*4882a593Smuzhiyun 	int ret = 0;
1901*4882a593Smuzhiyun 	u32 val = 0;
1902*4882a593Smuzhiyun 
1903*4882a593Smuzhiyun 	/* Propagate change of current control to all related controls */
1904*4882a593Smuzhiyun 	switch (ctrl->id) {
1905*4882a593Smuzhiyun 	case V4L2_CID_VBLANK:
1906*4882a593Smuzhiyun 		/* Update max exposure while meeting expected vblanking */
1907*4882a593Smuzhiyun 		max = SC301IOT->cur_mode->height + ctrl->val - 4;
1908*4882a593Smuzhiyun 		__v4l2_ctrl_modify_range(SC301IOT->exposure,
1909*4882a593Smuzhiyun 					 SC301IOT->exposure->minimum, max,
1910*4882a593Smuzhiyun 					 SC301IOT->exposure->step,
1911*4882a593Smuzhiyun 					 SC301IOT->exposure->default_value);
1912*4882a593Smuzhiyun 		break;
1913*4882a593Smuzhiyun 	}
1914*4882a593Smuzhiyun 
1915*4882a593Smuzhiyun 	if (!pm_runtime_get_if_in_use(&client->dev))
1916*4882a593Smuzhiyun 		return 0;
1917*4882a593Smuzhiyun 
1918*4882a593Smuzhiyun 	switch (ctrl->id) {
1919*4882a593Smuzhiyun 	case V4L2_CID_EXPOSURE:
1920*4882a593Smuzhiyun 		if (SC301IOT->cur_mode->hdr_mode == NO_HDR) {
1921*4882a593Smuzhiyun 			ctrl->val = ctrl->val;
1922*4882a593Smuzhiyun 			/* 4 least significant bits of expsoure are fractional part */
1923*4882a593Smuzhiyun 			ret = SC301IOT_write_reg(SC301IOT->client,
1924*4882a593Smuzhiyun 						SC301IOT_REG_EXPOSURE_H,
1925*4882a593Smuzhiyun 						SC301IOT_REG_VALUE_08BIT,
1926*4882a593Smuzhiyun 						SC301IOT_FETCH_EXP_H(ctrl->val));
1927*4882a593Smuzhiyun 			ret |= SC301IOT_write_reg(SC301IOT->client,
1928*4882a593Smuzhiyun 						 SC301IOT_REG_EXPOSURE_M,
1929*4882a593Smuzhiyun 						 SC301IOT_REG_VALUE_08BIT,
1930*4882a593Smuzhiyun 						 SC301IOT_FETCH_EXP_M(ctrl->val));
1931*4882a593Smuzhiyun 			ret |= SC301IOT_write_reg(SC301IOT->client,
1932*4882a593Smuzhiyun 						 SC301IOT_REG_EXPOSURE_L,
1933*4882a593Smuzhiyun 						 SC301IOT_REG_VALUE_08BIT,
1934*4882a593Smuzhiyun 						 SC301IOT_FETCH_EXP_L(ctrl->val));
1935*4882a593Smuzhiyun 		}
1936*4882a593Smuzhiyun 		break;
1937*4882a593Smuzhiyun 	case V4L2_CID_ANALOGUE_GAIN:
1938*4882a593Smuzhiyun 		if (SC301IOT->cur_mode->hdr_mode == NO_HDR)
1939*4882a593Smuzhiyun 			ret = SC301IOT_set_gain_reg(SC301IOT, ctrl->val, SC301IOT_LGAIN);
1940*4882a593Smuzhiyun 		break;
1941*4882a593Smuzhiyun 	case V4L2_CID_VBLANK:
1942*4882a593Smuzhiyun 		ret = SC301IOT_write_reg(SC301IOT->client,
1943*4882a593Smuzhiyun 					SC301IOT_REG_VTS_H,
1944*4882a593Smuzhiyun 					SC301IOT_REG_VALUE_08BIT,
1945*4882a593Smuzhiyun 					(ctrl->val + SC301IOT->cur_mode->height)
1946*4882a593Smuzhiyun 					>> 8);
1947*4882a593Smuzhiyun 		ret |= SC301IOT_write_reg(SC301IOT->client,
1948*4882a593Smuzhiyun 					 SC301IOT_REG_VTS_L,
1949*4882a593Smuzhiyun 					 SC301IOT_REG_VALUE_08BIT,
1950*4882a593Smuzhiyun 					 (ctrl->val + SC301IOT->cur_mode->height)
1951*4882a593Smuzhiyun 					 & 0xff);
1952*4882a593Smuzhiyun 		if (!ret)
1953*4882a593Smuzhiyun 			SC301IOT->cur_vts = ctrl->val + SC301IOT->cur_mode->height;
1954*4882a593Smuzhiyun 		SC301IOT_modify_fps_info(SC301IOT);
1955*4882a593Smuzhiyun 		break;
1956*4882a593Smuzhiyun 	case V4L2_CID_TEST_PATTERN:
1957*4882a593Smuzhiyun 		ret = SC301IOT_enable_test_pattern(SC301IOT, ctrl->val);
1958*4882a593Smuzhiyun 		break;
1959*4882a593Smuzhiyun 	case V4L2_CID_HFLIP:
1960*4882a593Smuzhiyun 		ret = SC301IOT_read_reg(SC301IOT->client, SC301IOT_FLIP_MIRROR_REG,
1961*4882a593Smuzhiyun 				       SC301IOT_REG_VALUE_08BIT, &val);
1962*4882a593Smuzhiyun 		ret |= SC301IOT_write_reg(SC301IOT->client, SC301IOT_FLIP_MIRROR_REG,
1963*4882a593Smuzhiyun 					 SC301IOT_REG_VALUE_08BIT,
1964*4882a593Smuzhiyun 					 SC301IOT_FETCH_MIRROR(val, ctrl->val));
1965*4882a593Smuzhiyun 		break;
1966*4882a593Smuzhiyun 	case V4L2_CID_VFLIP:
1967*4882a593Smuzhiyun 		ret = SC301IOT_read_reg(SC301IOT->client, SC301IOT_FLIP_MIRROR_REG,
1968*4882a593Smuzhiyun 				       SC301IOT_REG_VALUE_08BIT, &val);
1969*4882a593Smuzhiyun 		ret |= SC301IOT_write_reg(SC301IOT->client, SC301IOT_FLIP_MIRROR_REG,
1970*4882a593Smuzhiyun 					 SC301IOT_REG_VALUE_08BIT,
1971*4882a593Smuzhiyun 					 SC301IOT_FETCH_FLIP(val, ctrl->val));
1972*4882a593Smuzhiyun 		break;
1973*4882a593Smuzhiyun 	default:
1974*4882a593Smuzhiyun 		dev_warn(&client->dev, "%s Unhandled id:0x%x, val:0x%x\n",
1975*4882a593Smuzhiyun 			 __func__, ctrl->id, ctrl->val);
1976*4882a593Smuzhiyun 		break;
1977*4882a593Smuzhiyun 	}
1978*4882a593Smuzhiyun 
1979*4882a593Smuzhiyun 	pm_runtime_put(&client->dev);
1980*4882a593Smuzhiyun 
1981*4882a593Smuzhiyun 	return ret;
1982*4882a593Smuzhiyun }
1983*4882a593Smuzhiyun 
1984*4882a593Smuzhiyun static const struct v4l2_ctrl_ops SC301IOT_ctrl_ops = {
1985*4882a593Smuzhiyun 	.s_ctrl = SC301IOT_set_ctrl,
1986*4882a593Smuzhiyun };
1987*4882a593Smuzhiyun 
SC301IOT_initialize_controls(struct SC301IOT * SC301IOT)1988*4882a593Smuzhiyun static int SC301IOT_initialize_controls(struct SC301IOT *SC301IOT)
1989*4882a593Smuzhiyun {
1990*4882a593Smuzhiyun 	const struct SC301IOT_mode *mode;
1991*4882a593Smuzhiyun 	struct v4l2_ctrl_handler *handler;
1992*4882a593Smuzhiyun 	struct v4l2_ctrl *ctrl;
1993*4882a593Smuzhiyun 	s64 exposure_max, vblank_def;
1994*4882a593Smuzhiyun 	u32 h_blank;
1995*4882a593Smuzhiyun 	int ret;
1996*4882a593Smuzhiyun 
1997*4882a593Smuzhiyun 	handler = &SC301IOT->ctrl_handler;
1998*4882a593Smuzhiyun 	mode = SC301IOT->cur_mode;
1999*4882a593Smuzhiyun 	ret = v4l2_ctrl_handler_init(handler, 9);
2000*4882a593Smuzhiyun 	if (ret)
2001*4882a593Smuzhiyun 		return ret;
2002*4882a593Smuzhiyun 	handler->lock = &SC301IOT->mutex;
2003*4882a593Smuzhiyun 
2004*4882a593Smuzhiyun 	ctrl = v4l2_ctrl_new_int_menu(handler, NULL, V4L2_CID_LINK_FREQ,
2005*4882a593Smuzhiyun 				      0, 0, link_freq_menu_items);
2006*4882a593Smuzhiyun 	if (ctrl)
2007*4882a593Smuzhiyun 		ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
2008*4882a593Smuzhiyun 
2009*4882a593Smuzhiyun 	v4l2_ctrl_new_std(handler, NULL, V4L2_CID_PIXEL_RATE,
2010*4882a593Smuzhiyun 			  0, PIXEL_RATE_WITH_594M_10BIT, 1, PIXEL_RATE_WITH_594M_10BIT);
2011*4882a593Smuzhiyun 
2012*4882a593Smuzhiyun 	h_blank = mode->hts_def - mode->width;
2013*4882a593Smuzhiyun 	SC301IOT->hblank = v4l2_ctrl_new_std(handler, NULL, V4L2_CID_HBLANK,
2014*4882a593Smuzhiyun 					    h_blank, h_blank, 1, h_blank);
2015*4882a593Smuzhiyun 	if (SC301IOT->hblank)
2016*4882a593Smuzhiyun 		SC301IOT->hblank->flags |= V4L2_CTRL_FLAG_READ_ONLY;
2017*4882a593Smuzhiyun 	vblank_def = mode->vts_def - mode->height;
2018*4882a593Smuzhiyun 	SC301IOT->vblank = v4l2_ctrl_new_std(handler, &SC301IOT_ctrl_ops,
2019*4882a593Smuzhiyun 					    V4L2_CID_VBLANK, vblank_def,
2020*4882a593Smuzhiyun 					    SC301IOT_VTS_MAX - mode->height,
2021*4882a593Smuzhiyun 					    1, vblank_def);
2022*4882a593Smuzhiyun 	exposure_max = mode->vts_def - 8;
2023*4882a593Smuzhiyun 	SC301IOT->exposure = v4l2_ctrl_new_std(handler, &SC301IOT_ctrl_ops,
2024*4882a593Smuzhiyun 					      V4L2_CID_EXPOSURE, SC301IOT_EXPOSURE_MIN,
2025*4882a593Smuzhiyun 					      exposure_max, SC301IOT_EXPOSURE_STEP,
2026*4882a593Smuzhiyun 					      mode->exp_def);
2027*4882a593Smuzhiyun 	SC301IOT->anal_gain = v4l2_ctrl_new_std(handler, &SC301IOT_ctrl_ops,
2028*4882a593Smuzhiyun 					       V4L2_CID_ANALOGUE_GAIN, SC301IOT_GAIN_MIN,
2029*4882a593Smuzhiyun 					       SC301IOT_GAIN_MAX, SC301IOT_GAIN_STEP,
2030*4882a593Smuzhiyun 					       SC301IOT_GAIN_DEFAULT);
2031*4882a593Smuzhiyun 	SC301IOT->test_pattern = v4l2_ctrl_new_std_menu_items(handler,
2032*4882a593Smuzhiyun 							    &SC301IOT_ctrl_ops,
2033*4882a593Smuzhiyun 					V4L2_CID_TEST_PATTERN,
2034*4882a593Smuzhiyun 					ARRAY_SIZE(SC301IOT_test_pattern_menu) - 1,
2035*4882a593Smuzhiyun 					0, 0, SC301IOT_test_pattern_menu);
2036*4882a593Smuzhiyun 	v4l2_ctrl_new_std(handler, &SC301IOT_ctrl_ops,
2037*4882a593Smuzhiyun 				V4L2_CID_HFLIP, 0, 1, 1, 0);
2038*4882a593Smuzhiyun 
2039*4882a593Smuzhiyun 	v4l2_ctrl_new_std(handler, &SC301IOT_ctrl_ops,
2040*4882a593Smuzhiyun 				V4L2_CID_VFLIP, 0, 1, 1, 0);
2041*4882a593Smuzhiyun 
2042*4882a593Smuzhiyun 	if (handler->error) {
2043*4882a593Smuzhiyun 		ret = handler->error;
2044*4882a593Smuzhiyun 		dev_err(&SC301IOT->client->dev,
2045*4882a593Smuzhiyun 			"Failed to init controls(%d)\n", ret);
2046*4882a593Smuzhiyun 		goto err_free_handler;
2047*4882a593Smuzhiyun 	}
2048*4882a593Smuzhiyun 
2049*4882a593Smuzhiyun 	SC301IOT->subdev.ctrl_handler = handler;
2050*4882a593Smuzhiyun 	SC301IOT->has_init_exp = false;
2051*4882a593Smuzhiyun 	SC301IOT->cur_fps = mode->max_fps;
2052*4882a593Smuzhiyun 	SC301IOT->cur_vts = mode->vts_def;
2053*4882a593Smuzhiyun 
2054*4882a593Smuzhiyun 	return 0;
2055*4882a593Smuzhiyun 
2056*4882a593Smuzhiyun err_free_handler:
2057*4882a593Smuzhiyun 	v4l2_ctrl_handler_free(handler);
2058*4882a593Smuzhiyun 
2059*4882a593Smuzhiyun 	return ret;
2060*4882a593Smuzhiyun }
2061*4882a593Smuzhiyun 
SC301IOT_check_sensor_id(struct SC301IOT * SC301IOT,struct i2c_client * client)2062*4882a593Smuzhiyun static int SC301IOT_check_sensor_id(struct SC301IOT *SC301IOT,
2063*4882a593Smuzhiyun 				   struct i2c_client *client)
2064*4882a593Smuzhiyun {
2065*4882a593Smuzhiyun 	struct device *dev = &SC301IOT->client->dev;
2066*4882a593Smuzhiyun 	u32 id = 0;
2067*4882a593Smuzhiyun 	int ret;
2068*4882a593Smuzhiyun 
2069*4882a593Smuzhiyun 	if (SC301IOT->is_thunderboot) {
2070*4882a593Smuzhiyun 		dev_info(dev, "Enable thunderboot mode, skip sensor id check\n");
2071*4882a593Smuzhiyun 		return 0;
2072*4882a593Smuzhiyun 	}
2073*4882a593Smuzhiyun 
2074*4882a593Smuzhiyun 	ret = SC301IOT_read_reg(client, SC301IOT_REG_CHIP_ID,
2075*4882a593Smuzhiyun 			       SC301IOT_REG_VALUE_16BIT, &id);
2076*4882a593Smuzhiyun 	if (id != CHIP_ID) {
2077*4882a593Smuzhiyun 		dev_err(dev, "Unexpected chip id(0x%04x), ret(%d)\n", id, ret);
2078*4882a593Smuzhiyun 		return -ENODEV;
2079*4882a593Smuzhiyun 	}
2080*4882a593Smuzhiyun 
2081*4882a593Smuzhiyun 	dev_info(dev, "Detected chip id (0x%04x)\n", id);
2082*4882a593Smuzhiyun 
2083*4882a593Smuzhiyun 	return 0;
2084*4882a593Smuzhiyun }
2085*4882a593Smuzhiyun 
SC301IOT_configure_regulators(struct SC301IOT * SC301IOT)2086*4882a593Smuzhiyun static int SC301IOT_configure_regulators(struct SC301IOT *SC301IOT)
2087*4882a593Smuzhiyun {
2088*4882a593Smuzhiyun 	unsigned int i;
2089*4882a593Smuzhiyun 
2090*4882a593Smuzhiyun 	for (i = 0; i < SC301IOT_NUM_SUPPLIES; i++)
2091*4882a593Smuzhiyun 		SC301IOT->supplies[i].supply = SC301IOT_supply_names[i];
2092*4882a593Smuzhiyun 
2093*4882a593Smuzhiyun 	return devm_regulator_bulk_get(&SC301IOT->client->dev,
2094*4882a593Smuzhiyun 				       SC301IOT_NUM_SUPPLIES,
2095*4882a593Smuzhiyun 				       SC301IOT->supplies);
2096*4882a593Smuzhiyun }
2097*4882a593Smuzhiyun 
SC301IOT_probe(struct i2c_client * client,const struct i2c_device_id * id)2098*4882a593Smuzhiyun static int SC301IOT_probe(struct i2c_client *client,
2099*4882a593Smuzhiyun 			 const struct i2c_device_id *id)
2100*4882a593Smuzhiyun {
2101*4882a593Smuzhiyun 	struct device *dev = &client->dev;
2102*4882a593Smuzhiyun 	struct device_node *node = dev->of_node;
2103*4882a593Smuzhiyun 	struct SC301IOT *SC301IOT;
2104*4882a593Smuzhiyun 	struct v4l2_subdev *sd;
2105*4882a593Smuzhiyun 	char facing[2];
2106*4882a593Smuzhiyun 	int ret;
2107*4882a593Smuzhiyun 	u32 i, hdr_mode = 0;
2108*4882a593Smuzhiyun 	const char *sync_mode_name = NULL;
2109*4882a593Smuzhiyun 
2110*4882a593Smuzhiyun 	dev_info(dev, "driver version: %02x.%02x.%02x",
2111*4882a593Smuzhiyun 		 DRIVER_VERSION >> 16,
2112*4882a593Smuzhiyun 		 (DRIVER_VERSION & 0xff00) >> 8,
2113*4882a593Smuzhiyun 		 DRIVER_VERSION & 0x00ff);
2114*4882a593Smuzhiyun 
2115*4882a593Smuzhiyun 	SC301IOT = devm_kzalloc(dev, sizeof(*SC301IOT), GFP_KERNEL);
2116*4882a593Smuzhiyun 	if (!SC301IOT)
2117*4882a593Smuzhiyun 		return -ENOMEM;
2118*4882a593Smuzhiyun 
2119*4882a593Smuzhiyun 	of_property_read_u32(node, OF_CAMERA_HDR_MODE, &hdr_mode);
2120*4882a593Smuzhiyun 	ret = of_property_read_u32(node, RKMODULE_CAMERA_MODULE_INDEX,
2121*4882a593Smuzhiyun 				   &SC301IOT->module_index);
2122*4882a593Smuzhiyun 	ret |= of_property_read_string(node, RKMODULE_CAMERA_MODULE_FACING,
2123*4882a593Smuzhiyun 				       &SC301IOT->module_facing);
2124*4882a593Smuzhiyun 	ret |= of_property_read_string(node, RKMODULE_CAMERA_MODULE_NAME,
2125*4882a593Smuzhiyun 				       &SC301IOT->module_name);
2126*4882a593Smuzhiyun 	ret |= of_property_read_string(node, RKMODULE_CAMERA_LENS_NAME,
2127*4882a593Smuzhiyun 				       &SC301IOT->len_name);
2128*4882a593Smuzhiyun 	if (ret) {
2129*4882a593Smuzhiyun 		dev_err(dev, "could not get module information!\n");
2130*4882a593Smuzhiyun 		return -EINVAL;
2131*4882a593Smuzhiyun 	}
2132*4882a593Smuzhiyun 
2133*4882a593Smuzhiyun 	SC301IOT->is_thunderboot = IS_ENABLED(CONFIG_VIDEO_ROCKCHIP_THUNDER_BOOT_ISP);
2134*4882a593Smuzhiyun 	SC301IOT->sync_mode = NO_SYNC_MODE;
2135*4882a593Smuzhiyun 	ret = of_property_read_string(node, RKMODULE_CAMERA_SYNC_MODE, &sync_mode_name);
2136*4882a593Smuzhiyun 	if (!ret) {
2137*4882a593Smuzhiyun 		if (strcmp(sync_mode_name, RKMODULE_EXTERNAL_MASTER_MODE) == 0)
2138*4882a593Smuzhiyun 			SC301IOT->sync_mode = EXTERNAL_MASTER_MODE;
2139*4882a593Smuzhiyun 		else if (strcmp(sync_mode_name, RKMODULE_INTERNAL_MASTER_MODE) == 0)
2140*4882a593Smuzhiyun 			SC301IOT->sync_mode = INTERNAL_MASTER_MODE;
2141*4882a593Smuzhiyun 		else if (strcmp(sync_mode_name, RKMODULE_SLAVE_MODE) == 0)
2142*4882a593Smuzhiyun 			SC301IOT->sync_mode = SLAVE_MODE;
2143*4882a593Smuzhiyun 	}
2144*4882a593Smuzhiyun 
2145*4882a593Smuzhiyun 	switch (SC301IOT->sync_mode) {
2146*4882a593Smuzhiyun 	default:
2147*4882a593Smuzhiyun 		SC301IOT->sync_mode = NO_SYNC_MODE; break;
2148*4882a593Smuzhiyun 	case NO_SYNC_MODE:
2149*4882a593Smuzhiyun 		dev_info(dev, "sync_mode = [NO_SYNC_MODE]\n"); break;
2150*4882a593Smuzhiyun 	case EXTERNAL_MASTER_MODE:
2151*4882a593Smuzhiyun 	case INTERNAL_MASTER_MODE:
2152*4882a593Smuzhiyun 		dev_info(dev, "sync_mode = [MASTER_MODE]\n"); break;
2153*4882a593Smuzhiyun 	case SLAVE_MODE:
2154*4882a593Smuzhiyun 		dev_info(dev, "sync_mode = [SLAVE_MODE]\n"); break;
2155*4882a593Smuzhiyun 	}
2156*4882a593Smuzhiyun 
2157*4882a593Smuzhiyun 	SC301IOT->client = client;
2158*4882a593Smuzhiyun 	for (i = 0; i < ARRAY_SIZE(supported_modes); i++) {
2159*4882a593Smuzhiyun 		if (hdr_mode == supported_modes[i].hdr_mode) {
2160*4882a593Smuzhiyun 			SC301IOT->cur_mode = &supported_modes[i];
2161*4882a593Smuzhiyun 			break;
2162*4882a593Smuzhiyun 		}
2163*4882a593Smuzhiyun 	}
2164*4882a593Smuzhiyun 	if (i == ARRAY_SIZE(supported_modes))
2165*4882a593Smuzhiyun 		SC301IOT->cur_mode = &supported_modes[0];
2166*4882a593Smuzhiyun 
2167*4882a593Smuzhiyun 	SC301IOT->xvclk = devm_clk_get(dev, "xvclk");
2168*4882a593Smuzhiyun 	if (IS_ERR(SC301IOT->xvclk)) {
2169*4882a593Smuzhiyun 		dev_err(dev, "Failed to get xvclk\n");
2170*4882a593Smuzhiyun 		return -EINVAL;
2171*4882a593Smuzhiyun 	}
2172*4882a593Smuzhiyun 
2173*4882a593Smuzhiyun 	if (SC301IOT->is_thunderboot) {
2174*4882a593Smuzhiyun 		SC301IOT->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_ASIS);
2175*4882a593Smuzhiyun 		if (IS_ERR(SC301IOT->reset_gpio))
2176*4882a593Smuzhiyun 			dev_warn(dev, "Failed to get reset-gpios\n");
2177*4882a593Smuzhiyun 
2178*4882a593Smuzhiyun 		SC301IOT->pwdn_gpio = devm_gpiod_get(dev, "pwdn", GPIOD_ASIS);
2179*4882a593Smuzhiyun 		if (IS_ERR(SC301IOT->pwdn_gpio))
2180*4882a593Smuzhiyun 			dev_warn(dev, "Failed to get pwdn-gpios\n");
2181*4882a593Smuzhiyun 	} else {
2182*4882a593Smuzhiyun 		SC301IOT->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_LOW);
2183*4882a593Smuzhiyun 		if (IS_ERR(SC301IOT->reset_gpio))
2184*4882a593Smuzhiyun 			dev_warn(dev, "Failed to get reset-gpios\n");
2185*4882a593Smuzhiyun 
2186*4882a593Smuzhiyun 		SC301IOT->pwdn_gpio = devm_gpiod_get(dev, "pwdn", GPIOD_OUT_LOW);
2187*4882a593Smuzhiyun 		if (IS_ERR(SC301IOT->pwdn_gpio))
2188*4882a593Smuzhiyun 			dev_warn(dev, "Failed to get pwdn-gpios\n");
2189*4882a593Smuzhiyun 	}
2190*4882a593Smuzhiyun 
2191*4882a593Smuzhiyun 	SC301IOT->pinctrl = devm_pinctrl_get(dev);
2192*4882a593Smuzhiyun 	if (!IS_ERR(SC301IOT->pinctrl)) {
2193*4882a593Smuzhiyun 		SC301IOT->pins_default =
2194*4882a593Smuzhiyun 			pinctrl_lookup_state(SC301IOT->pinctrl,
2195*4882a593Smuzhiyun 					     OF_CAMERA_PINCTRL_STATE_DEFAULT);
2196*4882a593Smuzhiyun 		if (IS_ERR(SC301IOT->pins_default))
2197*4882a593Smuzhiyun 			dev_err(dev, "could not get default pinstate\n");
2198*4882a593Smuzhiyun 
2199*4882a593Smuzhiyun 		SC301IOT->pins_sleep =
2200*4882a593Smuzhiyun 			pinctrl_lookup_state(SC301IOT->pinctrl,
2201*4882a593Smuzhiyun 					     OF_CAMERA_PINCTRL_STATE_SLEEP);
2202*4882a593Smuzhiyun 		if (IS_ERR(SC301IOT->pins_sleep))
2203*4882a593Smuzhiyun 			dev_err(dev, "could not get sleep pinstate\n");
2204*4882a593Smuzhiyun 	} else {
2205*4882a593Smuzhiyun 		dev_err(dev, "no pinctrl\n");
2206*4882a593Smuzhiyun 	}
2207*4882a593Smuzhiyun 
2208*4882a593Smuzhiyun 	ret = SC301IOT_configure_regulators(SC301IOT);
2209*4882a593Smuzhiyun 	if (ret) {
2210*4882a593Smuzhiyun 		dev_err(dev, "Failed to get power regulators\n");
2211*4882a593Smuzhiyun 		return ret;
2212*4882a593Smuzhiyun 	}
2213*4882a593Smuzhiyun 
2214*4882a593Smuzhiyun 	mutex_init(&SC301IOT->mutex);
2215*4882a593Smuzhiyun 
2216*4882a593Smuzhiyun 	sd = &SC301IOT->subdev;
2217*4882a593Smuzhiyun 	v4l2_i2c_subdev_init(sd, client, &SC301IOT_subdev_ops);
2218*4882a593Smuzhiyun 	ret = SC301IOT_initialize_controls(SC301IOT);
2219*4882a593Smuzhiyun 	if (ret)
2220*4882a593Smuzhiyun 		goto err_destroy_mutex;
2221*4882a593Smuzhiyun 
2222*4882a593Smuzhiyun 	ret = __SC301IOT_power_on(SC301IOT);
2223*4882a593Smuzhiyun 	if (ret)
2224*4882a593Smuzhiyun 		goto err_free_handler;
2225*4882a593Smuzhiyun 
2226*4882a593Smuzhiyun 	ret = SC301IOT_check_sensor_id(SC301IOT, client);
2227*4882a593Smuzhiyun 	if (ret)
2228*4882a593Smuzhiyun 		goto err_power_off;
2229*4882a593Smuzhiyun 
2230*4882a593Smuzhiyun #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
2231*4882a593Smuzhiyun 	sd->internal_ops = &SC301IOT_internal_ops;
2232*4882a593Smuzhiyun 	sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE |
2233*4882a593Smuzhiyun 		     V4L2_SUBDEV_FL_HAS_EVENTS;
2234*4882a593Smuzhiyun #endif
2235*4882a593Smuzhiyun #if defined(CONFIG_MEDIA_CONTROLLER)
2236*4882a593Smuzhiyun 	SC301IOT->pad.flags = MEDIA_PAD_FL_SOURCE;
2237*4882a593Smuzhiyun 	sd->entity.function = MEDIA_ENT_F_CAM_SENSOR;
2238*4882a593Smuzhiyun 	ret = media_entity_pads_init(&sd->entity, 1, &SC301IOT->pad);
2239*4882a593Smuzhiyun 	if (ret < 0)
2240*4882a593Smuzhiyun 		goto err_power_off;
2241*4882a593Smuzhiyun #endif
2242*4882a593Smuzhiyun 
2243*4882a593Smuzhiyun 	memset(facing, 0, sizeof(facing));
2244*4882a593Smuzhiyun 	if (strcmp(SC301IOT->module_facing, "back") == 0)
2245*4882a593Smuzhiyun 		facing[0] = 'b';
2246*4882a593Smuzhiyun 	else
2247*4882a593Smuzhiyun 		facing[0] = 'f';
2248*4882a593Smuzhiyun 
2249*4882a593Smuzhiyun 	snprintf(sd->name, sizeof(sd->name), "m%02d_%s_%s %s",
2250*4882a593Smuzhiyun 		 SC301IOT->module_index, facing,
2251*4882a593Smuzhiyun 		 SC301IOT_NAME, dev_name(sd->dev));
2252*4882a593Smuzhiyun 	ret = v4l2_async_register_subdev_sensor_common(sd);
2253*4882a593Smuzhiyun 	if (ret) {
2254*4882a593Smuzhiyun 		dev_err(dev, "v4l2 async register subdev failed\n");
2255*4882a593Smuzhiyun 		goto err_clean_entity;
2256*4882a593Smuzhiyun 	}
2257*4882a593Smuzhiyun 
2258*4882a593Smuzhiyun 	pm_runtime_set_active(dev);
2259*4882a593Smuzhiyun 	pm_runtime_enable(dev);
2260*4882a593Smuzhiyun 	pm_runtime_idle(dev);
2261*4882a593Smuzhiyun 
2262*4882a593Smuzhiyun 	return 0;
2263*4882a593Smuzhiyun 
2264*4882a593Smuzhiyun err_clean_entity:
2265*4882a593Smuzhiyun #if defined(CONFIG_MEDIA_CONTROLLER)
2266*4882a593Smuzhiyun 	media_entity_cleanup(&sd->entity);
2267*4882a593Smuzhiyun #endif
2268*4882a593Smuzhiyun err_power_off:
2269*4882a593Smuzhiyun 	__SC301IOT_power_off(SC301IOT);
2270*4882a593Smuzhiyun err_free_handler:
2271*4882a593Smuzhiyun 	v4l2_ctrl_handler_free(&SC301IOT->ctrl_handler);
2272*4882a593Smuzhiyun err_destroy_mutex:
2273*4882a593Smuzhiyun 	mutex_destroy(&SC301IOT->mutex);
2274*4882a593Smuzhiyun 
2275*4882a593Smuzhiyun 	return ret;
2276*4882a593Smuzhiyun }
2277*4882a593Smuzhiyun 
SC301IOT_remove(struct i2c_client * client)2278*4882a593Smuzhiyun static int SC301IOT_remove(struct i2c_client *client)
2279*4882a593Smuzhiyun {
2280*4882a593Smuzhiyun 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
2281*4882a593Smuzhiyun 	struct SC301IOT *SC301IOT = to_SC301IOT(sd);
2282*4882a593Smuzhiyun 
2283*4882a593Smuzhiyun 	v4l2_async_unregister_subdev(sd);
2284*4882a593Smuzhiyun #if defined(CONFIG_MEDIA_CONTROLLER)
2285*4882a593Smuzhiyun 	media_entity_cleanup(&sd->entity);
2286*4882a593Smuzhiyun #endif
2287*4882a593Smuzhiyun 	v4l2_ctrl_handler_free(&SC301IOT->ctrl_handler);
2288*4882a593Smuzhiyun 	mutex_destroy(&SC301IOT->mutex);
2289*4882a593Smuzhiyun 
2290*4882a593Smuzhiyun 	pm_runtime_disable(&client->dev);
2291*4882a593Smuzhiyun 	if (!pm_runtime_status_suspended(&client->dev))
2292*4882a593Smuzhiyun 		__SC301IOT_power_off(SC301IOT);
2293*4882a593Smuzhiyun 	pm_runtime_set_suspended(&client->dev);
2294*4882a593Smuzhiyun 
2295*4882a593Smuzhiyun 	return 0;
2296*4882a593Smuzhiyun }
2297*4882a593Smuzhiyun 
2298*4882a593Smuzhiyun #if IS_ENABLED(CONFIG_OF)
2299*4882a593Smuzhiyun static const struct of_device_id SC301IOT_of_match[] = {
2300*4882a593Smuzhiyun 	{ .compatible = "smartsens,SC301IOT" },
2301*4882a593Smuzhiyun 	{},
2302*4882a593Smuzhiyun };
2303*4882a593Smuzhiyun MODULE_DEVICE_TABLE(of, SC301IOT_of_match);
2304*4882a593Smuzhiyun #endif
2305*4882a593Smuzhiyun 
2306*4882a593Smuzhiyun static const struct i2c_device_id SC301IOT_match_id[] = {
2307*4882a593Smuzhiyun 	{ "smartsens,SC301IOT", 0 },
2308*4882a593Smuzhiyun 	{ },
2309*4882a593Smuzhiyun };
2310*4882a593Smuzhiyun 
2311*4882a593Smuzhiyun static struct i2c_driver SC301IOT_i2c_driver = {
2312*4882a593Smuzhiyun 	.driver = {
2313*4882a593Smuzhiyun 		.name = SC301IOT_NAME,
2314*4882a593Smuzhiyun 		.pm = &SC301IOT_pm_ops,
2315*4882a593Smuzhiyun 		.of_match_table = of_match_ptr(SC301IOT_of_match),
2316*4882a593Smuzhiyun 	},
2317*4882a593Smuzhiyun 	.probe		= &SC301IOT_probe,
2318*4882a593Smuzhiyun 	.remove		= &SC301IOT_remove,
2319*4882a593Smuzhiyun 	.id_table	= SC301IOT_match_id,
2320*4882a593Smuzhiyun };
2321*4882a593Smuzhiyun 
sensor_mod_init(void)2322*4882a593Smuzhiyun static int __init sensor_mod_init(void)
2323*4882a593Smuzhiyun {
2324*4882a593Smuzhiyun 	return i2c_add_driver(&SC301IOT_i2c_driver);
2325*4882a593Smuzhiyun }
2326*4882a593Smuzhiyun 
sensor_mod_exit(void)2327*4882a593Smuzhiyun static void __exit sensor_mod_exit(void)
2328*4882a593Smuzhiyun {
2329*4882a593Smuzhiyun 	i2c_del_driver(&SC301IOT_i2c_driver);
2330*4882a593Smuzhiyun }
2331*4882a593Smuzhiyun 
2332*4882a593Smuzhiyun #if defined(CONFIG_VIDEO_ROCKCHIP_THUNDER_BOOT_ISP) && !defined(CONFIG_INITCALL_ASYNC)
2333*4882a593Smuzhiyun subsys_initcall(sensor_mod_init);
2334*4882a593Smuzhiyun #else
2335*4882a593Smuzhiyun device_initcall_sync(sensor_mod_init);
2336*4882a593Smuzhiyun #endif
2337*4882a593Smuzhiyun module_exit(sensor_mod_exit);
2338*4882a593Smuzhiyun 
2339*4882a593Smuzhiyun MODULE_DESCRIPTION("smartsens SC301IOT sensor driver");
2340*4882a593Smuzhiyun MODULE_LICENSE("GPL");
2341