xref: /openbmc/linux/drivers/media/i2c/imx334.c (revision 1ac731c529cd4d6adbce134754b51ff7d822b145)
19746b117SMartina Krasteva // SPDX-License-Identifier: GPL-2.0-only
29746b117SMartina Krasteva /*
39746b117SMartina Krasteva  * Sony imx334 sensor driver
49746b117SMartina Krasteva  *
59746b117SMartina Krasteva  * Copyright (C) 2021 Intel Corporation
69746b117SMartina Krasteva  */
79746b117SMartina Krasteva #include <asm/unaligned.h>
89746b117SMartina Krasteva 
99746b117SMartina Krasteva #include <linux/clk.h>
109746b117SMartina Krasteva #include <linux/delay.h>
119746b117SMartina Krasteva #include <linux/i2c.h>
129746b117SMartina Krasteva #include <linux/module.h>
139746b117SMartina Krasteva #include <linux/pm_runtime.h>
149746b117SMartina Krasteva 
159746b117SMartina Krasteva #include <media/v4l2-ctrls.h>
169746b117SMartina Krasteva #include <media/v4l2-fwnode.h>
179746b117SMartina Krasteva #include <media/v4l2-subdev.h>
189746b117SMartina Krasteva 
199746b117SMartina Krasteva /* Streaming Mode */
209746b117SMartina Krasteva #define IMX334_REG_MODE_SELECT	0x3000
219746b117SMartina Krasteva #define IMX334_MODE_STANDBY	0x01
229746b117SMartina Krasteva #define IMX334_MODE_STREAMING	0x00
239746b117SMartina Krasteva 
249746b117SMartina Krasteva /* Lines per frame */
259746b117SMartina Krasteva #define IMX334_REG_LPFR		0x3030
269746b117SMartina Krasteva 
279746b117SMartina Krasteva /* Chip ID */
289746b117SMartina Krasteva #define IMX334_REG_ID		0x3044
299746b117SMartina Krasteva #define IMX334_ID		0x1e
309746b117SMartina Krasteva 
319746b117SMartina Krasteva /* Exposure control */
329746b117SMartina Krasteva #define IMX334_REG_SHUTTER	0x3058
339746b117SMartina Krasteva #define IMX334_EXPOSURE_MIN	1
349746b117SMartina Krasteva #define IMX334_EXPOSURE_OFFSET	5
359746b117SMartina Krasteva #define IMX334_EXPOSURE_STEP	1
369746b117SMartina Krasteva #define IMX334_EXPOSURE_DEFAULT	0x0648
379746b117SMartina Krasteva 
389746b117SMartina Krasteva /* Analog gain control */
399746b117SMartina Krasteva #define IMX334_REG_AGAIN	0x30e8
409746b117SMartina Krasteva #define IMX334_AGAIN_MIN	0
419746b117SMartina Krasteva #define IMX334_AGAIN_MAX	240
429746b117SMartina Krasteva #define IMX334_AGAIN_STEP	1
439746b117SMartina Krasteva #define IMX334_AGAIN_DEFAULT	0
449746b117SMartina Krasteva 
459746b117SMartina Krasteva /* Group hold register */
469746b117SMartina Krasteva #define IMX334_REG_HOLD		0x3001
479746b117SMartina Krasteva 
489746b117SMartina Krasteva /* Input clock rate */
499746b117SMartina Krasteva #define IMX334_INCLK_RATE	24000000
509746b117SMartina Krasteva 
519746b117SMartina Krasteva /* CSI2 HW configuration */
529746b117SMartina Krasteva #define IMX334_LINK_FREQ_891M	891000000
539746b117SMartina Krasteva #define IMX334_LINK_FREQ_445M	445500000
549746b117SMartina Krasteva #define IMX334_NUM_DATA_LANES	4
559746b117SMartina Krasteva 
569746b117SMartina Krasteva #define IMX334_REG_MIN		0x00
579746b117SMartina Krasteva #define IMX334_REG_MAX		0xfffff
589746b117SMartina Krasteva 
599746b117SMartina Krasteva /**
609746b117SMartina Krasteva  * struct imx334_reg - imx334 sensor register
619746b117SMartina Krasteva  * @address: Register address
629746b117SMartina Krasteva  * @val: Register value
639746b117SMartina Krasteva  */
649746b117SMartina Krasteva struct imx334_reg {
659746b117SMartina Krasteva 	u16 address;
669746b117SMartina Krasteva 	u8 val;
679746b117SMartina Krasteva };
689746b117SMartina Krasteva 
699746b117SMartina Krasteva /**
709746b117SMartina Krasteva  * struct imx334_reg_list - imx334 sensor register list
719746b117SMartina Krasteva  * @num_of_regs: Number of registers in the list
729746b117SMartina Krasteva  * @regs: Pointer to register list
739746b117SMartina Krasteva  */
749746b117SMartina Krasteva struct imx334_reg_list {
759746b117SMartina Krasteva 	u32 num_of_regs;
769746b117SMartina Krasteva 	const struct imx334_reg *regs;
779746b117SMartina Krasteva };
789746b117SMartina Krasteva 
799746b117SMartina Krasteva /**
809746b117SMartina Krasteva  * struct imx334_mode - imx334 sensor mode structure
819746b117SMartina Krasteva  * @width: Frame width
829746b117SMartina Krasteva  * @height: Frame height
839746b117SMartina Krasteva  * @hblank: Horizontal blanking in lines
849746b117SMartina Krasteva  * @vblank: Vertical blanking in lines
859746b117SMartina Krasteva  * @vblank_min: Minimal vertical blanking in lines
869746b117SMartina Krasteva  * @vblank_max: Maximum vertical blanking in lines
879746b117SMartina Krasteva  * @pclk: Sensor pixel clock
889746b117SMartina Krasteva  * @link_freq_idx: Link frequency index
899746b117SMartina Krasteva  * @reg_list: Register list for sensor mode
909746b117SMartina Krasteva  */
919746b117SMartina Krasteva struct imx334_mode {
929746b117SMartina Krasteva 	u32 width;
939746b117SMartina Krasteva 	u32 height;
949746b117SMartina Krasteva 	u32 hblank;
959746b117SMartina Krasteva 	u32 vblank;
969746b117SMartina Krasteva 	u32 vblank_min;
979746b117SMartina Krasteva 	u32 vblank_max;
989746b117SMartina Krasteva 	u64 pclk;
999746b117SMartina Krasteva 	u32 link_freq_idx;
1009746b117SMartina Krasteva 	struct imx334_reg_list reg_list;
1019746b117SMartina Krasteva };
1029746b117SMartina Krasteva 
1039746b117SMartina Krasteva /**
1049746b117SMartina Krasteva  * struct imx334 - imx334 sensor device structure
1059746b117SMartina Krasteva  * @dev: Pointer to generic device
1069746b117SMartina Krasteva  * @client: Pointer to i2c client
1079746b117SMartina Krasteva  * @sd: V4L2 sub-device
1089746b117SMartina Krasteva  * @pad: Media pad. Only one pad supported
1099746b117SMartina Krasteva  * @reset_gpio: Sensor reset gpio
1109746b117SMartina Krasteva  * @inclk: Sensor input clock
1119746b117SMartina Krasteva  * @ctrl_handler: V4L2 control handler
1129746b117SMartina Krasteva  * @link_freq_ctrl: Pointer to link frequency control
1139746b117SMartina Krasteva  * @pclk_ctrl: Pointer to pixel clock control
1149746b117SMartina Krasteva  * @hblank_ctrl: Pointer to horizontal blanking control
1159746b117SMartina Krasteva  * @vblank_ctrl: Pointer to vertical blanking control
1169746b117SMartina Krasteva  * @exp_ctrl: Pointer to exposure control
1179746b117SMartina Krasteva  * @again_ctrl: Pointer to analog gain control
1189746b117SMartina Krasteva  * @vblank: Vertical blanking in lines
1199746b117SMartina Krasteva  * @cur_mode: Pointer to current selected sensor mode
120*7462df58SShravan Chippa  * @mutex: Mutex for serializing sensor controls
1219746b117SMartina Krasteva  * @menu_skip_mask: Menu skip mask for link_freq_ctrl
1229746b117SMartina Krasteva  * @cur_code: current selected format code
1239746b117SMartina Krasteva  * @streaming: Flag indicating streaming state
1249746b117SMartina Krasteva  */
1259746b117SMartina Krasteva struct imx334 {
1269746b117SMartina Krasteva 	struct device *dev;
1279746b117SMartina Krasteva 	struct i2c_client *client;
1289746b117SMartina Krasteva 	struct v4l2_subdev sd;
1299746b117SMartina Krasteva 	struct media_pad pad;
1309746b117SMartina Krasteva 	struct gpio_desc *reset_gpio;
1319746b117SMartina Krasteva 	struct clk *inclk;
1329746b117SMartina Krasteva 	struct v4l2_ctrl_handler ctrl_handler;
1339746b117SMartina Krasteva 	struct v4l2_ctrl *link_freq_ctrl;
1349746b117SMartina Krasteva 	struct v4l2_ctrl *pclk_ctrl;
1359746b117SMartina Krasteva 	struct v4l2_ctrl *hblank_ctrl;
1369746b117SMartina Krasteva 	struct v4l2_ctrl *vblank_ctrl;
1379746b117SMartina Krasteva 	struct {
1389746b117SMartina Krasteva 		struct v4l2_ctrl *exp_ctrl;
1399746b117SMartina Krasteva 		struct v4l2_ctrl *again_ctrl;
1409746b117SMartina Krasteva 	};
1419746b117SMartina Krasteva 	u32 vblank;
142*7462df58SShravan Chippa 	const struct imx334_mode *cur_mode;
1439746b117SMartina Krasteva 	struct mutex mutex;
1449746b117SMartina Krasteva 	unsigned long menu_skip_mask;
1459746b117SMartina Krasteva 	u32 cur_code;
1469746b117SMartina Krasteva 	bool streaming;
1479746b117SMartina Krasteva };
1489746b117SMartina Krasteva 
1499746b117SMartina Krasteva static const s64 link_freq[] = {
150*7462df58SShravan Chippa 	IMX334_LINK_FREQ_891M,
151*7462df58SShravan Chippa 	IMX334_LINK_FREQ_445M,
152*7462df58SShravan Chippa };
153*7462df58SShravan Chippa 
154*7462df58SShravan Chippa /* Sensor mode registers for 1920x1080@30fps */
155*7462df58SShravan Chippa static const struct imx334_reg mode_1920x1080_regs[] = {
156*7462df58SShravan Chippa 	{0x3000, 0x01},
157*7462df58SShravan Chippa 	{0x3018, 0x04},
158*7462df58SShravan Chippa 	{0x3030, 0xca},
159*7462df58SShravan Chippa 	{0x3031, 0x08},
160*7462df58SShravan Chippa 	{0x3032, 0x00},
161*7462df58SShravan Chippa 	{0x3034, 0x4c},
162*7462df58SShravan Chippa 	{0x3035, 0x04},
163*7462df58SShravan Chippa 	{0x302c, 0xf0},
164*7462df58SShravan Chippa 	{0x302d, 0x03},
165*7462df58SShravan Chippa 	{0x302e, 0x80},
166*7462df58SShravan Chippa 	{0x302f, 0x07},
167*7462df58SShravan Chippa 	{0x3074, 0xcc},
168*7462df58SShravan Chippa 	{0x3075, 0x02},
169*7462df58SShravan Chippa 	{0x308e, 0xcd},
170*7462df58SShravan Chippa 	{0x308f, 0x02},
171*7462df58SShravan Chippa 	{0x3076, 0x38},
172*7462df58SShravan Chippa 	{0x3077, 0x04},
173*7462df58SShravan Chippa 	{0x3090, 0x38},
174*7462df58SShravan Chippa 	{0x3091, 0x04},
175*7462df58SShravan Chippa 	{0x3308, 0x38},
176*7462df58SShravan Chippa 	{0x3309, 0x04},
177*7462df58SShravan Chippa 	{0x30C6, 0x00},
178*7462df58SShravan Chippa 	{0x30c7, 0x00},
179*7462df58SShravan Chippa 	{0x30ce, 0x00},
180*7462df58SShravan Chippa 	{0x30cf, 0x00},
181*7462df58SShravan Chippa 	{0x30d8, 0x18},
182*7462df58SShravan Chippa 	{0x30d9, 0x0a},
183*7462df58SShravan Chippa 	{0x304c, 0x00},
184*7462df58SShravan Chippa 	{0x304e, 0x00},
185*7462df58SShravan Chippa 	{0x304f, 0x00},
186*7462df58SShravan Chippa 	{0x3050, 0x00},
187*7462df58SShravan Chippa 	{0x30b6, 0x00},
188*7462df58SShravan Chippa 	{0x30b7, 0x00},
189*7462df58SShravan Chippa 	{0x3116, 0x08},
190*7462df58SShravan Chippa 	{0x3117, 0x00},
191*7462df58SShravan Chippa 	{0x31a0, 0x20},
192*7462df58SShravan Chippa 	{0x31a1, 0x0f},
193*7462df58SShravan Chippa 	{0x300c, 0x3b},
194*7462df58SShravan Chippa 	{0x300d, 0x29},
195*7462df58SShravan Chippa 	{0x314c, 0x29},
196*7462df58SShravan Chippa 	{0x314d, 0x01},
197*7462df58SShravan Chippa 	{0x315a, 0x06},
198*7462df58SShravan Chippa 	{0x3168, 0xa0},
199*7462df58SShravan Chippa 	{0x316a, 0x7e},
200*7462df58SShravan Chippa 	{0x319e, 0x02},
201*7462df58SShravan Chippa 	{0x3199, 0x00},
202*7462df58SShravan Chippa 	{0x319d, 0x00},
203*7462df58SShravan Chippa 	{0x31dd, 0x03},
204*7462df58SShravan Chippa 	{0x3300, 0x00},
205*7462df58SShravan Chippa 	{0x341c, 0xff},
206*7462df58SShravan Chippa 	{0x341d, 0x01},
207*7462df58SShravan Chippa 	{0x3a01, 0x03},
208*7462df58SShravan Chippa 	{0x3a18, 0x7f},
209*7462df58SShravan Chippa 	{0x3a19, 0x00},
210*7462df58SShravan Chippa 	{0x3a1a, 0x37},
211*7462df58SShravan Chippa 	{0x3a1b, 0x00},
212*7462df58SShravan Chippa 	{0x3a1c, 0x37},
213*7462df58SShravan Chippa 	{0x3a1d, 0x00},
214*7462df58SShravan Chippa 	{0x3a1e, 0xf7},
215*7462df58SShravan Chippa 	{0x3a1f, 0x00},
216*7462df58SShravan Chippa 	{0x3a20, 0x3f},
217*7462df58SShravan Chippa 	{0x3a21, 0x00},
218*7462df58SShravan Chippa 	{0x3a20, 0x6f},
219*7462df58SShravan Chippa 	{0x3a21, 0x00},
220*7462df58SShravan Chippa 	{0x3a20, 0x3f},
221*7462df58SShravan Chippa 	{0x3a21, 0x00},
222*7462df58SShravan Chippa 	{0x3a20, 0x5f},
223*7462df58SShravan Chippa 	{0x3a21, 0x00},
224*7462df58SShravan Chippa 	{0x3a20, 0x2f},
225*7462df58SShravan Chippa 	{0x3a21, 0x00},
226*7462df58SShravan Chippa 	{0x3078, 0x02},
227*7462df58SShravan Chippa 	{0x3079, 0x00},
228*7462df58SShravan Chippa 	{0x307a, 0x00},
229*7462df58SShravan Chippa 	{0x307b, 0x00},
230*7462df58SShravan Chippa 	{0x3080, 0x02},
231*7462df58SShravan Chippa 	{0x3081, 0x00},
232*7462df58SShravan Chippa 	{0x3082, 0x00},
233*7462df58SShravan Chippa 	{0x3083, 0x00},
234*7462df58SShravan Chippa 	{0x3088, 0x02},
235*7462df58SShravan Chippa 	{0x3094, 0x00},
236*7462df58SShravan Chippa 	{0x3095, 0x00},
237*7462df58SShravan Chippa 	{0x3096, 0x00},
238*7462df58SShravan Chippa 	{0x309b, 0x02},
239*7462df58SShravan Chippa 	{0x309c, 0x00},
240*7462df58SShravan Chippa 	{0x309d, 0x00},
241*7462df58SShravan Chippa 	{0x309e, 0x00},
242*7462df58SShravan Chippa 	{0x30a4, 0x00},
243*7462df58SShravan Chippa 	{0x30a5, 0x00},
244*7462df58SShravan Chippa 	{0x3288, 0x21},
245*7462df58SShravan Chippa 	{0x328a, 0x02},
246*7462df58SShravan Chippa 	{0x3414, 0x05},
247*7462df58SShravan Chippa 	{0x3416, 0x18},
248*7462df58SShravan Chippa 	{0x35Ac, 0x0e},
249*7462df58SShravan Chippa 	{0x3648, 0x01},
250*7462df58SShravan Chippa 	{0x364a, 0x04},
251*7462df58SShravan Chippa 	{0x364c, 0x04},
252*7462df58SShravan Chippa 	{0x3678, 0x01},
253*7462df58SShravan Chippa 	{0x367c, 0x31},
254*7462df58SShravan Chippa 	{0x367e, 0x31},
255*7462df58SShravan Chippa 	{0x3708, 0x02},
256*7462df58SShravan Chippa 	{0x3714, 0x01},
257*7462df58SShravan Chippa 	{0x3715, 0x02},
258*7462df58SShravan Chippa 	{0x3716, 0x02},
259*7462df58SShravan Chippa 	{0x3717, 0x02},
260*7462df58SShravan Chippa 	{0x371c, 0x3d},
261*7462df58SShravan Chippa 	{0x371d, 0x3f},
262*7462df58SShravan Chippa 	{0x372c, 0x00},
263*7462df58SShravan Chippa 	{0x372d, 0x00},
264*7462df58SShravan Chippa 	{0x372e, 0x46},
265*7462df58SShravan Chippa 	{0x372f, 0x00},
266*7462df58SShravan Chippa 	{0x3730, 0x89},
267*7462df58SShravan Chippa 	{0x3731, 0x00},
268*7462df58SShravan Chippa 	{0x3732, 0x08},
269*7462df58SShravan Chippa 	{0x3733, 0x01},
270*7462df58SShravan Chippa 	{0x3734, 0xfe},
271*7462df58SShravan Chippa 	{0x3735, 0x05},
272*7462df58SShravan Chippa 	{0x375d, 0x00},
273*7462df58SShravan Chippa 	{0x375e, 0x00},
274*7462df58SShravan Chippa 	{0x375f, 0x61},
275*7462df58SShravan Chippa 	{0x3760, 0x06},
276*7462df58SShravan Chippa 	{0x3768, 0x1b},
277*7462df58SShravan Chippa 	{0x3769, 0x1b},
278*7462df58SShravan Chippa 	{0x376a, 0x1a},
279*7462df58SShravan Chippa 	{0x376b, 0x19},
280*7462df58SShravan Chippa 	{0x376c, 0x18},
281*7462df58SShravan Chippa 	{0x376d, 0x14},
282*7462df58SShravan Chippa 	{0x376e, 0x0f},
283*7462df58SShravan Chippa 	{0x3776, 0x00},
284*7462df58SShravan Chippa 	{0x3777, 0x00},
285*7462df58SShravan Chippa 	{0x3778, 0x46},
286*7462df58SShravan Chippa 	{0x3779, 0x00},
287*7462df58SShravan Chippa 	{0x377a, 0x08},
288*7462df58SShravan Chippa 	{0x377b, 0x01},
289*7462df58SShravan Chippa 	{0x377c, 0x45},
290*7462df58SShravan Chippa 	{0x377d, 0x01},
291*7462df58SShravan Chippa 	{0x377e, 0x23},
292*7462df58SShravan Chippa 	{0x377f, 0x02},
293*7462df58SShravan Chippa 	{0x3780, 0xd9},
294*7462df58SShravan Chippa 	{0x3781, 0x03},
295*7462df58SShravan Chippa 	{0x3782, 0xf5},
296*7462df58SShravan Chippa 	{0x3783, 0x06},
297*7462df58SShravan Chippa 	{0x3784, 0xa5},
298*7462df58SShravan Chippa 	{0x3788, 0x0f},
299*7462df58SShravan Chippa 	{0x378a, 0xd9},
300*7462df58SShravan Chippa 	{0x378b, 0x03},
301*7462df58SShravan Chippa 	{0x378c, 0xeb},
302*7462df58SShravan Chippa 	{0x378d, 0x05},
303*7462df58SShravan Chippa 	{0x378e, 0x87},
304*7462df58SShravan Chippa 	{0x378f, 0x06},
305*7462df58SShravan Chippa 	{0x3790, 0xf5},
306*7462df58SShravan Chippa 	{0x3792, 0x43},
307*7462df58SShravan Chippa 	{0x3794, 0x7a},
308*7462df58SShravan Chippa 	{0x3796, 0xa1},
309*7462df58SShravan Chippa 	{0x37b0, 0x37},
310*7462df58SShravan Chippa 	{0x3e04, 0x0e},
311*7462df58SShravan Chippa 	{0x30e8, 0x50},
312*7462df58SShravan Chippa 	{0x30e9, 0x00},
313*7462df58SShravan Chippa 	{0x3e04, 0x0e},
3149746b117SMartina Krasteva 	{0x3002, 0x00},
3159746b117SMartina Krasteva };
3169746b117SMartina Krasteva 
3179746b117SMartina Krasteva /* Sensor mode registers for 3840x2160@30fps */
3189746b117SMartina Krasteva static const struct imx334_reg mode_3840x2160_regs[] = {
3199746b117SMartina Krasteva 	{0x3000, 0x01},
3209746b117SMartina Krasteva 	{0x3002, 0x00},
3219746b117SMartina Krasteva 	{0x3018, 0x04},
3229746b117SMartina Krasteva 	{0x37b0, 0x36},
3239746b117SMartina Krasteva 	{0x304c, 0x00},
3249746b117SMartina Krasteva 	{0x300c, 0x3b},
3259746b117SMartina Krasteva 	{0x300d, 0x2a},
3269746b117SMartina Krasteva 	{0x3034, 0x26},
3279746b117SMartina Krasteva 	{0x3035, 0x02},
3289746b117SMartina Krasteva 	{0x314c, 0x29},
3299746b117SMartina Krasteva 	{0x314d, 0x01},
3309746b117SMartina Krasteva 	{0x315a, 0x02},
3319746b117SMartina Krasteva 	{0x3168, 0xa0},
3325ca5515aSShravan Chippa 	{0x316a, 0x7e},
3339746b117SMartina Krasteva 	{0x3288, 0x21},
3349746b117SMartina Krasteva 	{0x328a, 0x02},
3359746b117SMartina Krasteva 	{0x302c, 0x3c},
3369746b117SMartina Krasteva 	{0x302d, 0x00},
3379746b117SMartina Krasteva 	{0x302e, 0x00},
3389746b117SMartina Krasteva 	{0x302f, 0x0f},
3399746b117SMartina Krasteva 	{0x3076, 0x70},
3409746b117SMartina Krasteva 	{0x3077, 0x08},
3419746b117SMartina Krasteva 	{0x3090, 0x70},
3429746b117SMartina Krasteva 	{0x3091, 0x08},
3439746b117SMartina Krasteva 	{0x30d8, 0x20},
3449746b117SMartina Krasteva 	{0x30d9, 0x12},
3459746b117SMartina Krasteva 	{0x3308, 0x70},
3469746b117SMartina Krasteva 	{0x3309, 0x08},
3479746b117SMartina Krasteva 	{0x3414, 0x05},
3489746b117SMartina Krasteva 	{0x3416, 0x18},
3499746b117SMartina Krasteva 	{0x35ac, 0x0e},
3509746b117SMartina Krasteva 	{0x3648, 0x01},
3519746b117SMartina Krasteva 	{0x364a, 0x04},
3529746b117SMartina Krasteva 	{0x364c, 0x04},
3539746b117SMartina Krasteva 	{0x3678, 0x01},
3549746b117SMartina Krasteva 	{0x367c, 0x31},
3559746b117SMartina Krasteva 	{0x367e, 0x31},
3569746b117SMartina Krasteva 	{0x3708, 0x02},
3579746b117SMartina Krasteva 	{0x3714, 0x01},
3589746b117SMartina Krasteva 	{0x3715, 0x02},
3599746b117SMartina Krasteva 	{0x3716, 0x02},
3609746b117SMartina Krasteva 	{0x3717, 0x02},
3619746b117SMartina Krasteva 	{0x371c, 0x3d},
3629746b117SMartina Krasteva 	{0x371d, 0x3f},
3639746b117SMartina Krasteva 	{0x372c, 0x00},
3649746b117SMartina Krasteva 	{0x372d, 0x00},
3659746b117SMartina Krasteva 	{0x372e, 0x46},
3669746b117SMartina Krasteva 	{0x372f, 0x00},
3679746b117SMartina Krasteva 	{0x3730, 0x89},
3689746b117SMartina Krasteva 	{0x3731, 0x00},
3699746b117SMartina Krasteva 	{0x3732, 0x08},
3709746b117SMartina Krasteva 	{0x3733, 0x01},
3719746b117SMartina Krasteva 	{0x3734, 0xfe},
3729746b117SMartina Krasteva 	{0x3735, 0x05},
3739746b117SMartina Krasteva 	{0x375d, 0x00},
3749746b117SMartina Krasteva 	{0x375e, 0x00},
3759746b117SMartina Krasteva 	{0x375f, 0x61},
3769746b117SMartina Krasteva 	{0x3760, 0x06},
3779746b117SMartina Krasteva 	{0x3768, 0x1b},
3789746b117SMartina Krasteva 	{0x3769, 0x1b},
3799746b117SMartina Krasteva 	{0x376a, 0x1a},
3809746b117SMartina Krasteva 	{0x376b, 0x19},
3819746b117SMartina Krasteva 	{0x376c, 0x18},
3829746b117SMartina Krasteva 	{0x376d, 0x14},
3839746b117SMartina Krasteva 	{0x376e, 0x0f},
3849746b117SMartina Krasteva 	{0x3776, 0x00},
3859746b117SMartina Krasteva 	{0x3777, 0x00},
3869746b117SMartina Krasteva 	{0x3778, 0x46},
3879746b117SMartina Krasteva 	{0x3779, 0x00},
3889746b117SMartina Krasteva 	{0x377a, 0x08},
3899746b117SMartina Krasteva 	{0x377b, 0x01},
3909746b117SMartina Krasteva 	{0x377c, 0x45},
3919746b117SMartina Krasteva 	{0x377d, 0x01},
3929746b117SMartina Krasteva 	{0x377e, 0x23},
3939746b117SMartina Krasteva 	{0x377f, 0x02},
3949746b117SMartina Krasteva 	{0x3780, 0xd9},
3959746b117SMartina Krasteva 	{0x3781, 0x03},
3969746b117SMartina Krasteva 	{0x3782, 0xf5},
3979746b117SMartina Krasteva 	{0x3783, 0x06},
3989746b117SMartina Krasteva 	{0x3784, 0xa5},
3999746b117SMartina Krasteva 	{0x3788, 0x0f},
4009746b117SMartina Krasteva 	{0x378a, 0xd9},
4019746b117SMartina Krasteva 	{0x378b, 0x03},
4029746b117SMartina Krasteva 	{0x378c, 0xeb},
4039746b117SMartina Krasteva 	{0x378d, 0x05},
4049746b117SMartina Krasteva 	{0x378e, 0x87},
4059746b117SMartina Krasteva 	{0x378f, 0x06},
4069746b117SMartina Krasteva 	{0x3790, 0xf5},
4075ca5515aSShravan Chippa 	{0x3792, 0x43},
4089746b117SMartina Krasteva 	{0x3794, 0x7a},
4095ca5515aSShravan Chippa 	{0x3796, 0xa1},
4105ca5515aSShravan Chippa 	{0x3e04, 0x0e},
4115ca5515aSShravan Chippa 	{0x319e, 0x00},
4125ca5515aSShravan Chippa 	{0x3a00, 0x01},
4135ca5515aSShravan Chippa 	{0x3a18, 0xbf},
4145ca5515aSShravan Chippa 	{0x3a19, 0x00},
4155ca5515aSShravan Chippa 	{0x3a1a, 0x67},
4165ca5515aSShravan Chippa 	{0x3a1b, 0x00},
4175ca5515aSShravan Chippa 	{0x3a1c, 0x6f},
4185ca5515aSShravan Chippa 	{0x3a1d, 0x00},
4195ca5515aSShravan Chippa 	{0x3a1e, 0xd7},
4205ca5515aSShravan Chippa 	{0x3a1f, 0x01},
4215ca5515aSShravan Chippa 	{0x3a20, 0x6f},
4225ca5515aSShravan Chippa 	{0x3a21, 0x00},
4235ca5515aSShravan Chippa 	{0x3a22, 0xcf},
4245ca5515aSShravan Chippa 	{0x3a23, 0x00},
4255ca5515aSShravan Chippa 	{0x3a24, 0x6f},
4265ca5515aSShravan Chippa 	{0x3a25, 0x00},
4279746b117SMartina Krasteva 	{0x3a26, 0xb7},
4289746b117SMartina Krasteva 	{0x3a27, 0x00},
429*7462df58SShravan Chippa 	{0x3a28, 0x5f},
430*7462df58SShravan Chippa 	{0x3a29, 0x00},
431*7462df58SShravan Chippa };
432*7462df58SShravan Chippa 
433*7462df58SShravan Chippa static const struct imx334_reg raw10_framefmt_regs[] = {
434*7462df58SShravan Chippa 	{0x3050, 0x00},
435*7462df58SShravan Chippa 	{0x319d, 0x00},
436*7462df58SShravan Chippa 	{0x341c, 0xff},
437*7462df58SShravan Chippa 	{0x341d, 0x01},
438*7462df58SShravan Chippa };
439*7462df58SShravan Chippa 
440*7462df58SShravan Chippa static const struct imx334_reg raw12_framefmt_regs[] = {
441*7462df58SShravan Chippa 	{0x3050, 0x01},
442*7462df58SShravan Chippa 	{0x319d, 0x01},
443*7462df58SShravan Chippa 	{0x341c, 0x47},
444*7462df58SShravan Chippa 	{0x341d, 0x00},
445*7462df58SShravan Chippa };
446*7462df58SShravan Chippa 
447*7462df58SShravan Chippa static const u32 imx334_mbus_codes[] = {
4489746b117SMartina Krasteva 	MEDIA_BUS_FMT_SRGGB12_1X12,
449*7462df58SShravan Chippa 	MEDIA_BUS_FMT_SRGGB10_1X10,
450*7462df58SShravan Chippa };
4519746b117SMartina Krasteva 
4529746b117SMartina Krasteva /* Supported sensor mode configurations */
4539746b117SMartina Krasteva static const struct imx334_mode supported_modes[] = {
4549746b117SMartina Krasteva 	{
4559746b117SMartina Krasteva 		.width = 3840,
4569746b117SMartina Krasteva 		.height = 2160,
4579746b117SMartina Krasteva 		.hblank = 560,
4589746b117SMartina Krasteva 		.vblank = 2340,
4599746b117SMartina Krasteva 		.vblank_min = 90,
4609746b117SMartina Krasteva 		.vblank_max = 132840,
4619746b117SMartina Krasteva 		.pclk = 594000000,
4629746b117SMartina Krasteva 		.link_freq_idx = 0,
463*7462df58SShravan Chippa 		.reg_list = {
464*7462df58SShravan Chippa 			.num_of_regs = ARRAY_SIZE(mode_3840x2160_regs),
465*7462df58SShravan Chippa 			.regs = mode_3840x2160_regs,
466*7462df58SShravan Chippa 		},
467*7462df58SShravan Chippa 	}, {
468*7462df58SShravan Chippa 		.width = 1920,
469*7462df58SShravan Chippa 		.height = 1080,
470*7462df58SShravan Chippa 		.hblank = 2480,
471*7462df58SShravan Chippa 		.vblank = 1170,
472*7462df58SShravan Chippa 		.vblank_min = 45,
473*7462df58SShravan Chippa 		.vblank_max = 132840,
474*7462df58SShravan Chippa 		.pclk = 297000000,
475*7462df58SShravan Chippa 		.link_freq_idx = 1,
476*7462df58SShravan Chippa 		.reg_list = {
4779746b117SMartina Krasteva 			.num_of_regs = ARRAY_SIZE(mode_1920x1080_regs),
4789746b117SMartina Krasteva 			.regs = mode_1920x1080_regs,
4799746b117SMartina Krasteva 		},
4809746b117SMartina Krasteva 	},
4819746b117SMartina Krasteva };
4829746b117SMartina Krasteva 
4839746b117SMartina Krasteva /**
4849746b117SMartina Krasteva  * to_imx334() - imv334 V4L2 sub-device to imx334 device.
4859746b117SMartina Krasteva  * @subdev: pointer to imx334 V4L2 sub-device
4869746b117SMartina Krasteva  *
4879746b117SMartina Krasteva  * Return: pointer to imx334 device
4889746b117SMartina Krasteva  */
to_imx334(struct v4l2_subdev * subdev)4899746b117SMartina Krasteva static inline struct imx334 *to_imx334(struct v4l2_subdev *subdev)
4909746b117SMartina Krasteva {
4919746b117SMartina Krasteva 	return container_of(subdev, struct imx334, sd);
4929746b117SMartina Krasteva }
4939746b117SMartina Krasteva 
4949746b117SMartina Krasteva /**
4959746b117SMartina Krasteva  * imx334_read_reg() - Read registers.
4969746b117SMartina Krasteva  * @imx334: pointer to imx334 device
4979746b117SMartina Krasteva  * @reg: register address
4989746b117SMartina Krasteva  * @len: length of bytes to read. Max supported bytes is 4
4999746b117SMartina Krasteva  * @val: pointer to register value to be filled.
5009746b117SMartina Krasteva  *
5019746b117SMartina Krasteva  * Big endian register addresses with little endian values.
5029746b117SMartina Krasteva  *
5039746b117SMartina Krasteva  * Return: 0 if successful, error code otherwise.
5049746b117SMartina Krasteva  */
imx334_read_reg(struct imx334 * imx334,u16 reg,u32 len,u32 * val)5059746b117SMartina Krasteva static int imx334_read_reg(struct imx334 *imx334, u16 reg, u32 len, u32 *val)
5069746b117SMartina Krasteva {
5079746b117SMartina Krasteva 	struct i2c_client *client = v4l2_get_subdevdata(&imx334->sd);
5089746b117SMartina Krasteva 	struct i2c_msg msgs[2] = {0};
5099746b117SMartina Krasteva 	u8 addr_buf[2] = {0};
5109746b117SMartina Krasteva 	u8 data_buf[4] = {0};
5119746b117SMartina Krasteva 	int ret;
5129746b117SMartina Krasteva 
5139746b117SMartina Krasteva 	if (WARN_ON(len > 4))
5149746b117SMartina Krasteva 		return -EINVAL;
5159746b117SMartina Krasteva 
5169746b117SMartina Krasteva 	put_unaligned_be16(reg, addr_buf);
5179746b117SMartina Krasteva 
5189746b117SMartina Krasteva 	/* Write register address */
5199746b117SMartina Krasteva 	msgs[0].addr = client->addr;
5209746b117SMartina Krasteva 	msgs[0].flags = 0;
5219746b117SMartina Krasteva 	msgs[0].len = ARRAY_SIZE(addr_buf);
5229746b117SMartina Krasteva 	msgs[0].buf = addr_buf;
5239746b117SMartina Krasteva 
5249746b117SMartina Krasteva 	/* Read data from register */
5259746b117SMartina Krasteva 	msgs[1].addr = client->addr;
5269746b117SMartina Krasteva 	msgs[1].flags = I2C_M_RD;
5279746b117SMartina Krasteva 	msgs[1].len = len;
5289746b117SMartina Krasteva 	msgs[1].buf = data_buf;
5299746b117SMartina Krasteva 
5309746b117SMartina Krasteva 	ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
5319746b117SMartina Krasteva 	if (ret != ARRAY_SIZE(msgs))
5329746b117SMartina Krasteva 		return -EIO;
5339746b117SMartina Krasteva 
5349746b117SMartina Krasteva 	*val = get_unaligned_le32(data_buf);
5359746b117SMartina Krasteva 
5369746b117SMartina Krasteva 	return 0;
5379746b117SMartina Krasteva }
5389746b117SMartina Krasteva 
5399746b117SMartina Krasteva /**
5409746b117SMartina Krasteva  * imx334_write_reg() - Write register
5419746b117SMartina Krasteva  * @imx334: pointer to imx334 device
5429746b117SMartina Krasteva  * @reg: register address
5439746b117SMartina Krasteva  * @len: length of bytes. Max supported bytes is 4
5449746b117SMartina Krasteva  * @val: register value
5459746b117SMartina Krasteva  *
5469746b117SMartina Krasteva  * Big endian register addresses with little endian values.
5479746b117SMartina Krasteva  *
5489746b117SMartina Krasteva  * Return: 0 if successful, error code otherwise.
5499746b117SMartina Krasteva  */
imx334_write_reg(struct imx334 * imx334,u16 reg,u32 len,u32 val)5509746b117SMartina Krasteva static int imx334_write_reg(struct imx334 *imx334, u16 reg, u32 len, u32 val)
5519746b117SMartina Krasteva {
5529746b117SMartina Krasteva 	struct i2c_client *client = v4l2_get_subdevdata(&imx334->sd);
5539746b117SMartina Krasteva 	u8 buf[6] = {0};
5549746b117SMartina Krasteva 
5559746b117SMartina Krasteva 	if (WARN_ON(len > 4))
5569746b117SMartina Krasteva 		return -EINVAL;
5579746b117SMartina Krasteva 
5589746b117SMartina Krasteva 	put_unaligned_be16(reg, buf);
5599746b117SMartina Krasteva 	put_unaligned_le32(val, buf + 2);
5609746b117SMartina Krasteva 	if (i2c_master_send(client, buf, len + 2) != len + 2)
5619746b117SMartina Krasteva 		return -EIO;
5629746b117SMartina Krasteva 
5639746b117SMartina Krasteva 	return 0;
5649746b117SMartina Krasteva }
5659746b117SMartina Krasteva 
5669746b117SMartina Krasteva /**
5679746b117SMartina Krasteva  * imx334_write_regs() - Write a list of registers
5689746b117SMartina Krasteva  * @imx334: pointer to imx334 device
5699746b117SMartina Krasteva  * @regs: list of registers to be written
5709746b117SMartina Krasteva  * @len: length of registers array
5719746b117SMartina Krasteva  *
5729746b117SMartina Krasteva  * Return: 0 if successful, error code otherwise.
5739746b117SMartina Krasteva  */
imx334_write_regs(struct imx334 * imx334,const struct imx334_reg * regs,u32 len)5749746b117SMartina Krasteva static int imx334_write_regs(struct imx334 *imx334,
5759746b117SMartina Krasteva 			     const struct imx334_reg *regs, u32 len)
5769746b117SMartina Krasteva {
5779746b117SMartina Krasteva 	unsigned int i;
5789746b117SMartina Krasteva 	int ret;
5799746b117SMartina Krasteva 
5809746b117SMartina Krasteva 	for (i = 0; i < len; i++) {
5819746b117SMartina Krasteva 		ret = imx334_write_reg(imx334, regs[i].address, 1, regs[i].val);
5829746b117SMartina Krasteva 		if (ret)
5839746b117SMartina Krasteva 			return ret;
5849746b117SMartina Krasteva 	}
5859746b117SMartina Krasteva 
5869746b117SMartina Krasteva 	return 0;
5879746b117SMartina Krasteva }
5889746b117SMartina Krasteva 
5899746b117SMartina Krasteva /**
5909746b117SMartina Krasteva  * imx334_update_controls() - Update control ranges based on streaming mode
5919746b117SMartina Krasteva  * @imx334: pointer to imx334 device
5929746b117SMartina Krasteva  * @mode: pointer to imx334_mode sensor mode
5939746b117SMartina Krasteva  *
5949746b117SMartina Krasteva  * Return: 0 if successful, error code otherwise.
5959746b117SMartina Krasteva  */
imx334_update_controls(struct imx334 * imx334,const struct imx334_mode * mode)5969746b117SMartina Krasteva static int imx334_update_controls(struct imx334 *imx334,
5979746b117SMartina Krasteva 				  const struct imx334_mode *mode)
5989746b117SMartina Krasteva {
5999746b117SMartina Krasteva 	int ret;
6009746b117SMartina Krasteva 
601d6e34091SShravan Chippa 	ret = __v4l2_ctrl_s_ctrl(imx334->link_freq_ctrl, mode->link_freq_idx);
602d6e34091SShravan Chippa 	if (ret)
6039746b117SMartina Krasteva 		return ret;
6049746b117SMartina Krasteva 
6059746b117SMartina Krasteva 	ret = __v4l2_ctrl_modify_range(imx334->pclk_ctrl, mode->pclk,
6069746b117SMartina Krasteva 				       mode->pclk, 1, mode->pclk);
6079746b117SMartina Krasteva 	if (ret)
6089746b117SMartina Krasteva 		return ret;
6099746b117SMartina Krasteva 
6109746b117SMartina Krasteva 	ret = __v4l2_ctrl_modify_range(imx334->hblank_ctrl, mode->hblank,
6119746b117SMartina Krasteva 				       mode->hblank, 1, mode->hblank);
6129746b117SMartina Krasteva 	if (ret)
6139746b117SMartina Krasteva 		return ret;
6149746b117SMartina Krasteva 
6159746b117SMartina Krasteva 	ret =  __v4l2_ctrl_modify_range(imx334->vblank_ctrl, mode->vblank_min,
6169746b117SMartina Krasteva 					mode->vblank_max, 1, mode->vblank);
6179746b117SMartina Krasteva 	if (ret)
6189746b117SMartina Krasteva 		return ret;
6199746b117SMartina Krasteva 
6209746b117SMartina Krasteva 	return __v4l2_ctrl_s_ctrl(imx334->vblank_ctrl, mode->vblank);
6219746b117SMartina Krasteva }
6229746b117SMartina Krasteva 
6239746b117SMartina Krasteva /**
6249746b117SMartina Krasteva  * imx334_update_exp_gain() - Set updated exposure and gain
6259746b117SMartina Krasteva  * @imx334: pointer to imx334 device
6269746b117SMartina Krasteva  * @exposure: updated exposure value
6279746b117SMartina Krasteva  * @gain: updated analog gain value
6289746b117SMartina Krasteva  *
6299746b117SMartina Krasteva  * Return: 0 if successful, error code otherwise.
6309746b117SMartina Krasteva  */
imx334_update_exp_gain(struct imx334 * imx334,u32 exposure,u32 gain)6319746b117SMartina Krasteva static int imx334_update_exp_gain(struct imx334 *imx334, u32 exposure, u32 gain)
6329746b117SMartina Krasteva {
6339746b117SMartina Krasteva 	u32 lpfr, shutter;
6349746b117SMartina Krasteva 	int ret;
6359746b117SMartina Krasteva 
6369746b117SMartina Krasteva 	lpfr = imx334->vblank + imx334->cur_mode->height;
6379746b117SMartina Krasteva 	shutter = lpfr - exposure;
6389746b117SMartina Krasteva 
6399746b117SMartina Krasteva 	dev_dbg(imx334->dev, "Set long exp %u analog gain %u sh0 %u lpfr %u",
6409746b117SMartina Krasteva 		exposure, gain, shutter, lpfr);
6419746b117SMartina Krasteva 
6429746b117SMartina Krasteva 	ret = imx334_write_reg(imx334, IMX334_REG_HOLD, 1, 1);
6439746b117SMartina Krasteva 	if (ret)
6449746b117SMartina Krasteva 		return ret;
6459746b117SMartina Krasteva 
6469746b117SMartina Krasteva 	ret = imx334_write_reg(imx334, IMX334_REG_LPFR, 3, lpfr);
6479746b117SMartina Krasteva 	if (ret)
6489746b117SMartina Krasteva 		goto error_release_group_hold;
6499746b117SMartina Krasteva 
6509746b117SMartina Krasteva 	ret = imx334_write_reg(imx334, IMX334_REG_SHUTTER, 3, shutter);
6519746b117SMartina Krasteva 	if (ret)
6529746b117SMartina Krasteva 		goto error_release_group_hold;
6539746b117SMartina Krasteva 
6549746b117SMartina Krasteva 	ret = imx334_write_reg(imx334, IMX334_REG_AGAIN, 1, gain);
6559746b117SMartina Krasteva 
6569746b117SMartina Krasteva error_release_group_hold:
6579746b117SMartina Krasteva 	imx334_write_reg(imx334, IMX334_REG_HOLD, 1, 0);
6589746b117SMartina Krasteva 
6599746b117SMartina Krasteva 	return ret;
6609746b117SMartina Krasteva }
6619746b117SMartina Krasteva 
6629746b117SMartina Krasteva /**
6639746b117SMartina Krasteva  * imx334_set_ctrl() - Set subdevice control
6649746b117SMartina Krasteva  * @ctrl: pointer to v4l2_ctrl structure
6659746b117SMartina Krasteva  *
6669746b117SMartina Krasteva  * Supported controls:
6679746b117SMartina Krasteva  * - V4L2_CID_VBLANK
6689746b117SMartina Krasteva  * - cluster controls:
6699746b117SMartina Krasteva  *   - V4L2_CID_ANALOGUE_GAIN
6709746b117SMartina Krasteva  *   - V4L2_CID_EXPOSURE
6719746b117SMartina Krasteva  *
6729746b117SMartina Krasteva  * Return: 0 if successful, error code otherwise.
6739746b117SMartina Krasteva  */
imx334_set_ctrl(struct v4l2_ctrl * ctrl)6749746b117SMartina Krasteva static int imx334_set_ctrl(struct v4l2_ctrl *ctrl)
6759746b117SMartina Krasteva {
6769746b117SMartina Krasteva 	struct imx334 *imx334 =
6779746b117SMartina Krasteva 		container_of(ctrl->handler, struct imx334, ctrl_handler);
6789746b117SMartina Krasteva 	u32 analog_gain;
6799746b117SMartina Krasteva 	u32 exposure;
6809746b117SMartina Krasteva 	int ret;
6819746b117SMartina Krasteva 
6829746b117SMartina Krasteva 	switch (ctrl->id) {
6839746b117SMartina Krasteva 	case V4L2_CID_VBLANK:
6849746b117SMartina Krasteva 		imx334->vblank = imx334->vblank_ctrl->val;
6859746b117SMartina Krasteva 
6869746b117SMartina Krasteva 		dev_dbg(imx334->dev, "Received vblank %u, new lpfr %u",
6879746b117SMartina Krasteva 			imx334->vblank,
6889746b117SMartina Krasteva 			imx334->vblank + imx334->cur_mode->height);
6899746b117SMartina Krasteva 
6909746b117SMartina Krasteva 		ret = __v4l2_ctrl_modify_range(imx334->exp_ctrl,
6919746b117SMartina Krasteva 					       IMX334_EXPOSURE_MIN,
6929746b117SMartina Krasteva 					       imx334->vblank +
6939746b117SMartina Krasteva 					       imx334->cur_mode->height -
6949746b117SMartina Krasteva 					       IMX334_EXPOSURE_OFFSET,
6959746b117SMartina Krasteva 					       1, IMX334_EXPOSURE_DEFAULT);
6969746b117SMartina Krasteva 		break;
6979746b117SMartina Krasteva 	case V4L2_CID_EXPOSURE:
6989746b117SMartina Krasteva 
6999746b117SMartina Krasteva 		/* Set controls only if sensor is in power on state */
7009746b117SMartina Krasteva 		if (!pm_runtime_get_if_in_use(imx334->dev))
701d6e34091SShravan Chippa 			return 0;
702d6e34091SShravan Chippa 
703d6e34091SShravan Chippa 		exposure = ctrl->val;
7049746b117SMartina Krasteva 		analog_gain = imx334->again_ctrl->val;
7059746b117SMartina Krasteva 
7069746b117SMartina Krasteva 		dev_dbg(imx334->dev, "Received exp %u analog gain %u",
7079746b117SMartina Krasteva 			exposure, analog_gain);
7089746b117SMartina Krasteva 
7099746b117SMartina Krasteva 		ret = imx334_update_exp_gain(imx334, exposure, analog_gain);
7109746b117SMartina Krasteva 
7119746b117SMartina Krasteva 		pm_runtime_put(imx334->dev);
7129746b117SMartina Krasteva 
7139746b117SMartina Krasteva 		break;
7149746b117SMartina Krasteva 	case V4L2_CID_PIXEL_RATE:
7159746b117SMartina Krasteva 	case V4L2_CID_LINK_FREQ:
7169746b117SMartina Krasteva 	case V4L2_CID_HBLANK:
717*7462df58SShravan Chippa 		ret = 0;
718*7462df58SShravan Chippa 		break;
719*7462df58SShravan Chippa 	default:
720*7462df58SShravan Chippa 		dev_err(imx334->dev, "Invalid control %d", ctrl->id);
721*7462df58SShravan Chippa 		ret = -EINVAL;
722*7462df58SShravan Chippa 	}
723*7462df58SShravan Chippa 
724*7462df58SShravan Chippa 	return ret;
725*7462df58SShravan Chippa }
726*7462df58SShravan Chippa 
727*7462df58SShravan Chippa /* V4l2 subdevice control ops*/
728*7462df58SShravan Chippa static const struct v4l2_ctrl_ops imx334_ctrl_ops = {
7299746b117SMartina Krasteva 	.s_ctrl = imx334_set_ctrl,
7309746b117SMartina Krasteva };
7319746b117SMartina Krasteva 
imx334_get_format_code(struct imx334 * imx334,u32 code)7320d346d2aSTomi Valkeinen static int imx334_get_format_code(struct imx334 *imx334, u32 code)
7339746b117SMartina Krasteva {
7349746b117SMartina Krasteva 	unsigned int i;
7359746b117SMartina Krasteva 
7369746b117SMartina Krasteva 	for (i = 0; i < ARRAY_SIZE(imx334_mbus_codes); i++) {
7379746b117SMartina Krasteva 		if (imx334_mbus_codes[i] == code)
7380d346d2aSTomi Valkeinen 			return imx334_mbus_codes[i];
7399746b117SMartina Krasteva 	}
7409746b117SMartina Krasteva 
741*7462df58SShravan Chippa 	return imx334_mbus_codes[0];
7429746b117SMartina Krasteva }
7439746b117SMartina Krasteva 
744*7462df58SShravan Chippa /**
7459746b117SMartina Krasteva  * imx334_enum_mbus_code() - Enumerate V4L2 sub-device mbus codes
7469746b117SMartina Krasteva  * @sd: pointer to imx334 V4L2 sub-device structure
7479746b117SMartina Krasteva  * @sd_state: V4L2 sub-device state
7489746b117SMartina Krasteva  * @code: V4L2 sub-device code enumeration need to be filled
7499746b117SMartina Krasteva  *
7509746b117SMartina Krasteva  * Return: 0 if successful, error code otherwise.
7519746b117SMartina Krasteva  */
imx334_enum_mbus_code(struct v4l2_subdev * sd,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_mbus_code_enum * code)7520d346d2aSTomi Valkeinen static int imx334_enum_mbus_code(struct v4l2_subdev *sd,
7539746b117SMartina Krasteva 				 struct v4l2_subdev_state *sd_state,
7549746b117SMartina Krasteva 				 struct v4l2_subdev_mbus_code_enum *code)
7559746b117SMartina Krasteva {
7569746b117SMartina Krasteva 	if (code->index >= ARRAY_SIZE(imx334_mbus_codes))
7579746b117SMartina Krasteva 		return -EINVAL;
7580d346d2aSTomi Valkeinen 
7599746b117SMartina Krasteva 	code->code = imx334_mbus_codes[code->index];
7609746b117SMartina Krasteva 
761*7462df58SShravan Chippa 	return 0;
762*7462df58SShravan Chippa }
763*7462df58SShravan Chippa 
764*7462df58SShravan Chippa /**
7659746b117SMartina Krasteva  * imx334_enum_frame_size() - Enumerate V4L2 sub-device frame sizes
7669746b117SMartina Krasteva  * @sd: pointer to imx334 V4L2 sub-device structure
767*7462df58SShravan Chippa  * @sd_state: V4L2 sub-device state
768*7462df58SShravan Chippa  * @fsize: V4L2 sub-device size enumeration need to be filled
769*7462df58SShravan Chippa  *
7709746b117SMartina Krasteva  * Return: 0 if successful, error code otherwise.
7719746b117SMartina Krasteva  */
imx334_enum_frame_size(struct v4l2_subdev * sd,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_frame_size_enum * fsize)772*7462df58SShravan Chippa static int imx334_enum_frame_size(struct v4l2_subdev *sd,
7739746b117SMartina Krasteva 				  struct v4l2_subdev_state *sd_state,
774*7462df58SShravan Chippa 				  struct v4l2_subdev_frame_size_enum *fsize)
7759746b117SMartina Krasteva {
7769746b117SMartina Krasteva 	struct imx334 *imx334 = to_imx334(sd);
7779746b117SMartina Krasteva 	u32 code;
7789746b117SMartina Krasteva 
7799746b117SMartina Krasteva 	if (fsize->index >= ARRAY_SIZE(supported_modes))
7809746b117SMartina Krasteva 		return -EINVAL;
7819746b117SMartina Krasteva 
7829746b117SMartina Krasteva 	code = imx334_get_format_code(imx334, fsize->code);
7839746b117SMartina Krasteva 
7849746b117SMartina Krasteva 	if (fsize->code != code)
7859746b117SMartina Krasteva 		return -EINVAL;
7869746b117SMartina Krasteva 
7879746b117SMartina Krasteva 	fsize->min_width = supported_modes[fsize->index].width;
7889746b117SMartina Krasteva 	fsize->max_width = fsize->min_width;
7899746b117SMartina Krasteva 	fsize->min_height = supported_modes[fsize->index].height;
7909746b117SMartina Krasteva 	fsize->max_height = fsize->min_height;
7919746b117SMartina Krasteva 
7929746b117SMartina Krasteva 	return 0;
7939746b117SMartina Krasteva }
7949746b117SMartina Krasteva 
7959746b117SMartina Krasteva /**
7969746b117SMartina Krasteva  * imx334_fill_pad_format() - Fill subdevice pad format
7979746b117SMartina Krasteva  *                            from selected sensor mode
7989746b117SMartina Krasteva  * @imx334: pointer to imx334 device
7999746b117SMartina Krasteva  * @mode: pointer to imx334_mode sensor mode
8009746b117SMartina Krasteva  * @fmt: V4L2 sub-device format need to be filled
8019746b117SMartina Krasteva  */
imx334_fill_pad_format(struct imx334 * imx334,const struct imx334_mode * mode,struct v4l2_subdev_format * fmt)8029746b117SMartina Krasteva static void imx334_fill_pad_format(struct imx334 *imx334,
8030d346d2aSTomi Valkeinen 				   const struct imx334_mode *mode,
8049746b117SMartina Krasteva 				   struct v4l2_subdev_format *fmt)
8059746b117SMartina Krasteva {
8069746b117SMartina Krasteva 	fmt->format.width = mode->width;
8079746b117SMartina Krasteva 	fmt->format.height = mode->height;
8089746b117SMartina Krasteva 	fmt->format.field = V4L2_FIELD_NONE;
8090d346d2aSTomi Valkeinen 	fmt->format.colorspace = V4L2_COLORSPACE_RAW;
8109746b117SMartina Krasteva 	fmt->format.ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
8119746b117SMartina Krasteva 	fmt->format.quantization = V4L2_QUANTIZATION_DEFAULT;
8129746b117SMartina Krasteva 	fmt->format.xfer_func = V4L2_XFER_FUNC_NONE;
8139746b117SMartina Krasteva }
8149746b117SMartina Krasteva 
8159746b117SMartina Krasteva /**
8169746b117SMartina Krasteva  * imx334_get_pad_format() - Get subdevice pad format
8179746b117SMartina Krasteva  * @sd: pointer to imx334 V4L2 sub-device structure
8189746b117SMartina Krasteva  * @sd_state: V4L2 sub-device state
8190d346d2aSTomi Valkeinen  * @fmt: V4L2 sub-device format need to be set
8209746b117SMartina Krasteva  *
8219746b117SMartina Krasteva  * Return: 0 if successful, error code otherwise.
822*7462df58SShravan Chippa  */
imx334_get_pad_format(struct v4l2_subdev * sd,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_format * fmt)8239746b117SMartina Krasteva static int imx334_get_pad_format(struct v4l2_subdev *sd,
8249746b117SMartina Krasteva 				 struct v4l2_subdev_state *sd_state,
8259746b117SMartina Krasteva 				 struct v4l2_subdev_format *fmt)
8269746b117SMartina Krasteva {
8279746b117SMartina Krasteva 	struct imx334 *imx334 = to_imx334(sd);
8289746b117SMartina Krasteva 
8299746b117SMartina Krasteva 	mutex_lock(&imx334->mutex);
8309746b117SMartina Krasteva 
8319746b117SMartina Krasteva 	if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
8329746b117SMartina Krasteva 		struct v4l2_mbus_framefmt *framefmt;
8339746b117SMartina Krasteva 
8340d346d2aSTomi Valkeinen 		framefmt = v4l2_subdev_get_try_format(sd, sd_state, fmt->pad);
8359746b117SMartina Krasteva 		fmt->format = *framefmt;
8369746b117SMartina Krasteva 	} else {
8379746b117SMartina Krasteva 		fmt->format.code = imx334->cur_code;
8389746b117SMartina Krasteva 		imx334_fill_pad_format(imx334, imx334->cur_mode, fmt);
8399746b117SMartina Krasteva 	}
8400d346d2aSTomi Valkeinen 
8419746b117SMartina Krasteva 	mutex_unlock(&imx334->mutex);
8429746b117SMartina Krasteva 
8439746b117SMartina Krasteva 	return 0;
8449746b117SMartina Krasteva }
8459746b117SMartina Krasteva 
8469746b117SMartina Krasteva /**
8479746b117SMartina Krasteva  * imx334_set_pad_format() - Set subdevice pad format
8489746b117SMartina Krasteva  * @sd: pointer to imx334 V4L2 sub-device structure
849*7462df58SShravan Chippa  * @sd_state: V4L2 sub-device state
850*7462df58SShravan Chippa  * @fmt: V4L2 sub-device format need to be set
851*7462df58SShravan Chippa  *
852*7462df58SShravan Chippa  * Return: 0 if successful, error code otherwise.
853*7462df58SShravan Chippa  */
imx334_set_pad_format(struct v4l2_subdev * sd,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_format * fmt)8549746b117SMartina Krasteva static int imx334_set_pad_format(struct v4l2_subdev *sd,
855*7462df58SShravan Chippa 				 struct v4l2_subdev_state *sd_state,
8569746b117SMartina Krasteva 				 struct v4l2_subdev_format *fmt)
8579746b117SMartina Krasteva {
8589746b117SMartina Krasteva 	struct imx334 *imx334 = to_imx334(sd);
8599746b117SMartina Krasteva 	const struct imx334_mode *mode;
8600d346d2aSTomi Valkeinen 	int ret = 0;
8619746b117SMartina Krasteva 
862*7462df58SShravan Chippa 	mutex_lock(&imx334->mutex);
863*7462df58SShravan Chippa 
8649746b117SMartina Krasteva 	mode = v4l2_find_nearest_size(supported_modes,
8659746b117SMartina Krasteva 				      ARRAY_SIZE(supported_modes),
8669746b117SMartina Krasteva 				      width, height,
8679746b117SMartina Krasteva 				      fmt->format.width, fmt->format.height);
8689746b117SMartina Krasteva 
8699746b117SMartina Krasteva 	imx334_fill_pad_format(imx334, mode, fmt);
8709746b117SMartina Krasteva 	fmt->format.code = imx334_get_format_code(imx334, fmt->format.code);
8719746b117SMartina Krasteva 
8729746b117SMartina Krasteva 	if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
8739746b117SMartina Krasteva 		struct v4l2_mbus_framefmt *framefmt;
8749746b117SMartina Krasteva 
8759746b117SMartina Krasteva 		framefmt = v4l2_subdev_get_try_format(sd, sd_state, fmt->pad);
8769746b117SMartina Krasteva 		*framefmt = fmt->format;
8770d346d2aSTomi Valkeinen 	} else if (imx334->cur_mode != mode || imx334->cur_code != fmt->format.code) {
8789746b117SMartina Krasteva 		imx334->cur_code = fmt->format.code;
8799746b117SMartina Krasteva 		ret = imx334_update_controls(imx334, mode);
8809746b117SMartina Krasteva 		if (!ret)
8819746b117SMartina Krasteva 			imx334->cur_mode = mode;
8820d346d2aSTomi Valkeinen 	}
8839746b117SMartina Krasteva 
8849746b117SMartina Krasteva 	mutex_unlock(&imx334->mutex);
8859746b117SMartina Krasteva 
8869746b117SMartina Krasteva 	return ret;
8870d346d2aSTomi Valkeinen }
888*7462df58SShravan Chippa 
8899746b117SMartina Krasteva /**
8900d346d2aSTomi Valkeinen  * imx334_init_pad_cfg() - Initialize sub-device pad configuration
8919746b117SMartina Krasteva  * @sd: pointer to imx334 V4L2 sub-device structure
8929746b117SMartina Krasteva  * @sd_state: V4L2 sub-device state
893*7462df58SShravan Chippa  *
894*7462df58SShravan Chippa  * Return: 0 if successful, error code otherwise.
895*7462df58SShravan Chippa  */
imx334_init_pad_cfg(struct v4l2_subdev * sd,struct v4l2_subdev_state * sd_state)896*7462df58SShravan Chippa static int imx334_init_pad_cfg(struct v4l2_subdev *sd,
897*7462df58SShravan Chippa 			       struct v4l2_subdev_state *sd_state)
898*7462df58SShravan Chippa {
899*7462df58SShravan Chippa 	struct imx334 *imx334 = to_imx334(sd);
900*7462df58SShravan Chippa 	struct v4l2_subdev_format fmt = { 0 };
901*7462df58SShravan Chippa 
902*7462df58SShravan Chippa 	fmt.which = sd_state ? V4L2_SUBDEV_FORMAT_TRY : V4L2_SUBDEV_FORMAT_ACTIVE;
903*7462df58SShravan Chippa 
904*7462df58SShravan Chippa 	mutex_lock(&imx334->mutex);
905*7462df58SShravan Chippa 
906*7462df58SShravan Chippa 	imx334_fill_pad_format(imx334, imx334->cur_mode, &fmt);
907*7462df58SShravan Chippa 
9089746b117SMartina Krasteva 	__v4l2_ctrl_modify_range(imx334->link_freq_ctrl, 0,
9099746b117SMartina Krasteva 				 __fls(imx334->menu_skip_mask),
9109746b117SMartina Krasteva 				 ~(imx334->menu_skip_mask),
9119746b117SMartina Krasteva 				 __ffs(imx334->menu_skip_mask));
9129746b117SMartina Krasteva 
9139746b117SMartina Krasteva 	mutex_unlock(&imx334->mutex);
9149746b117SMartina Krasteva 
9159746b117SMartina Krasteva 	return imx334_set_pad_format(sd, sd_state, &fmt);
9169746b117SMartina Krasteva }
9179746b117SMartina Krasteva 
imx334_set_framefmt(struct imx334 * imx334)9189746b117SMartina Krasteva static int imx334_set_framefmt(struct imx334 *imx334)
9199746b117SMartina Krasteva {
9209746b117SMartina Krasteva 	switch (imx334->cur_code) {
9219746b117SMartina Krasteva 	case MEDIA_BUS_FMT_SRGGB10_1X10:
9229746b117SMartina Krasteva 		return imx334_write_regs(imx334, raw10_framefmt_regs,
9239746b117SMartina Krasteva 					 ARRAY_SIZE(raw10_framefmt_regs));
9249746b117SMartina Krasteva 
9259746b117SMartina Krasteva 	case MEDIA_BUS_FMT_SRGGB12_1X12:
9269746b117SMartina Krasteva 		return imx334_write_regs(imx334, raw12_framefmt_regs,
9279746b117SMartina Krasteva 					 ARRAY_SIZE(raw12_framefmt_regs));
928*7462df58SShravan Chippa 	}
929*7462df58SShravan Chippa 
930*7462df58SShravan Chippa 	return -EINVAL;
931*7462df58SShravan Chippa }
932*7462df58SShravan Chippa 
933*7462df58SShravan Chippa /**
934*7462df58SShravan Chippa  * imx334_start_streaming() - Start sensor stream
9359746b117SMartina Krasteva  * @imx334: pointer to imx334 device
9369746b117SMartina Krasteva  *
9379746b117SMartina Krasteva  * Return: 0 if successful, error code otherwise.
9389746b117SMartina Krasteva  */
imx334_start_streaming(struct imx334 * imx334)9399746b117SMartina Krasteva static int imx334_start_streaming(struct imx334 *imx334)
9409746b117SMartina Krasteva {
9419746b117SMartina Krasteva 	const struct imx334_reg_list *reg_list;
9429746b117SMartina Krasteva 	int ret;
9439746b117SMartina Krasteva 
9449746b117SMartina Krasteva 	/* Write sensor mode registers */
9459746b117SMartina Krasteva 	reg_list = &imx334->cur_mode->reg_list;
9469746b117SMartina Krasteva 	ret = imx334_write_regs(imx334, reg_list->regs,
9479746b117SMartina Krasteva 				reg_list->num_of_regs);
9489746b117SMartina Krasteva 	if (ret) {
9499746b117SMartina Krasteva 		dev_err(imx334->dev, "fail to write initial registers");
9509746b117SMartina Krasteva 		return ret;
9519746b117SMartina Krasteva 	}
9529746b117SMartina Krasteva 
9539746b117SMartina Krasteva 	ret = imx334_set_framefmt(imx334);
9549746b117SMartina Krasteva 	if (ret) {
9559746b117SMartina Krasteva 		dev_err(imx334->dev, "%s failed to set frame format: %d\n",
9569746b117SMartina Krasteva 			__func__, ret);
9579746b117SMartina Krasteva 		return ret;
9589746b117SMartina Krasteva 	}
9599746b117SMartina Krasteva 
9609746b117SMartina Krasteva 	/* Setup handler will write actual exposure and gain */
9619746b117SMartina Krasteva 	ret =  __v4l2_ctrl_handler_setup(imx334->sd.ctrl_handler);
9629746b117SMartina Krasteva 	if (ret) {
9639746b117SMartina Krasteva 		dev_err(imx334->dev, "fail to setup handler");
9649746b117SMartina Krasteva 		return ret;
9659746b117SMartina Krasteva 	}
9669746b117SMartina Krasteva 
9679746b117SMartina Krasteva 	/* Start streaming */
9689746b117SMartina Krasteva 	ret = imx334_write_reg(imx334, IMX334_REG_MODE_SELECT,
9699746b117SMartina Krasteva 			       1, IMX334_MODE_STREAMING);
9709746b117SMartina Krasteva 	if (ret) {
9719746b117SMartina Krasteva 		dev_err(imx334->dev, "fail to start streaming");
9729746b117SMartina Krasteva 		return ret;
9739746b117SMartina Krasteva 	}
9749746b117SMartina Krasteva 
9759746b117SMartina Krasteva 	return 0;
9769746b117SMartina Krasteva }
9779746b117SMartina Krasteva 
9789746b117SMartina Krasteva /**
9799746b117SMartina Krasteva  * imx334_stop_streaming() - Stop sensor stream
9809746b117SMartina Krasteva  * @imx334: pointer to imx334 device
9819746b117SMartina Krasteva  *
9829746b117SMartina Krasteva  * Return: 0 if successful, error code otherwise.
9839746b117SMartina Krasteva  */
imx334_stop_streaming(struct imx334 * imx334)9849746b117SMartina Krasteva static int imx334_stop_streaming(struct imx334 *imx334)
98562c90446SMauro Carvalho Chehab {
98662c90446SMauro Carvalho Chehab 	return imx334_write_reg(imx334, IMX334_REG_MODE_SELECT,
98762c90446SMauro Carvalho Chehab 				1, IMX334_MODE_STANDBY);
9889746b117SMartina Krasteva }
9899746b117SMartina Krasteva 
9909746b117SMartina Krasteva /**
9919746b117SMartina Krasteva  * imx334_set_stream() - Enable sensor streaming
9929746b117SMartina Krasteva  * @sd: pointer to imx334 subdevice
9939746b117SMartina Krasteva  * @enable: set to enable sensor streaming
9949746b117SMartina Krasteva  *
9959746b117SMartina Krasteva  * Return: 0 if successful, error code otherwise.
9969746b117SMartina Krasteva  */
imx334_set_stream(struct v4l2_subdev * sd,int enable)9979746b117SMartina Krasteva static int imx334_set_stream(struct v4l2_subdev *sd, int enable)
9989746b117SMartina Krasteva {
9999746b117SMartina Krasteva 	struct imx334 *imx334 = to_imx334(sd);
10009746b117SMartina Krasteva 	int ret;
10019746b117SMartina Krasteva 
10029746b117SMartina Krasteva 	mutex_lock(&imx334->mutex);
10039746b117SMartina Krasteva 
10049746b117SMartina Krasteva 	if (imx334->streaming == enable) {
100562c90446SMauro Carvalho Chehab 		mutex_unlock(&imx334->mutex);
10069746b117SMartina Krasteva 		return 0;
10079746b117SMartina Krasteva 	}
10089746b117SMartina Krasteva 
10099746b117SMartina Krasteva 	if (enable) {
10109746b117SMartina Krasteva 		ret = pm_runtime_resume_and_get(imx334->dev);
10119746b117SMartina Krasteva 		if (ret < 0)
10129746b117SMartina Krasteva 			goto error_unlock;
10139746b117SMartina Krasteva 
10149746b117SMartina Krasteva 		ret = imx334_start_streaming(imx334);
10159746b117SMartina Krasteva 		if (ret)
10169746b117SMartina Krasteva 			goto error_power_off;
10179746b117SMartina Krasteva 	} else {
10189746b117SMartina Krasteva 		imx334_stop_streaming(imx334);
10199746b117SMartina Krasteva 		pm_runtime_put(imx334->dev);
10209746b117SMartina Krasteva 	}
10219746b117SMartina Krasteva 
10229746b117SMartina Krasteva 	imx334->streaming = enable;
10239746b117SMartina Krasteva 
10249746b117SMartina Krasteva 	mutex_unlock(&imx334->mutex);
10259746b117SMartina Krasteva 
10269746b117SMartina Krasteva 	return 0;
10279746b117SMartina Krasteva 
10289746b117SMartina Krasteva error_power_off:
10299746b117SMartina Krasteva 	pm_runtime_put(imx334->dev);
10309746b117SMartina Krasteva error_unlock:
10319746b117SMartina Krasteva 	mutex_unlock(&imx334->mutex);
10329746b117SMartina Krasteva 
10339746b117SMartina Krasteva 	return ret;
10349746b117SMartina Krasteva }
10359746b117SMartina Krasteva 
10369746b117SMartina Krasteva /**
10379746b117SMartina Krasteva  * imx334_detect() - Detect imx334 sensor
10389746b117SMartina Krasteva  * @imx334: pointer to imx334 device
10399746b117SMartina Krasteva  *
10409746b117SMartina Krasteva  * Return: 0 if successful, -EIO if sensor id does not match
10419746b117SMartina Krasteva  */
imx334_detect(struct imx334 * imx334)10429746b117SMartina Krasteva static int imx334_detect(struct imx334 *imx334)
10439746b117SMartina Krasteva {
10449746b117SMartina Krasteva 	int ret;
10459746b117SMartina Krasteva 	u32 val;
10469746b117SMartina Krasteva 
10479746b117SMartina Krasteva 	ret = imx334_read_reg(imx334, IMX334_REG_ID, 2, &val);
10489746b117SMartina Krasteva 	if (ret)
10499746b117SMartina Krasteva 		return ret;
10509746b117SMartina Krasteva 
10519746b117SMartina Krasteva 	if (val != IMX334_ID) {
10529746b117SMartina Krasteva 		dev_err(imx334->dev, "chip id mismatch: %x!=%x",
10539746b117SMartina Krasteva 			IMX334_ID, val);
10549746b117SMartina Krasteva 		return -ENXIO;
10559746b117SMartina Krasteva 	}
10569746b117SMartina Krasteva 
10579746b117SMartina Krasteva 	return 0;
10589746b117SMartina Krasteva }
1059c702e2f7SHans Verkuil 
1060c702e2f7SHans Verkuil /**
10619746b117SMartina Krasteva  * imx334_parse_hw_config() - Parse HW configuration and check if supported
10629746b117SMartina Krasteva  * @imx334: pointer to imx334 device
10639746b117SMartina Krasteva  *
10649746b117SMartina Krasteva  * Return: 0 if successful, error code otherwise.
10659746b117SMartina Krasteva  */
imx334_parse_hw_config(struct imx334 * imx334)10669746b117SMartina Krasteva static int imx334_parse_hw_config(struct imx334 *imx334)
10679746b117SMartina Krasteva {
10689746b117SMartina Krasteva 	struct fwnode_handle *fwnode = dev_fwnode(imx334->dev);
10699746b117SMartina Krasteva 	struct v4l2_fwnode_endpoint bus_cfg = {
10709746b117SMartina Krasteva 		.bus_type = V4L2_MBUS_CSI2_DPHY
10719746b117SMartina Krasteva 	};
10729746b117SMartina Krasteva 	struct fwnode_handle *ep;
10739746b117SMartina Krasteva 	unsigned long rate;
10749746b117SMartina Krasteva 	unsigned int i, j;
10759746b117SMartina Krasteva 	int ret;
10769746b117SMartina Krasteva 
10779746b117SMartina Krasteva 	if (!fwnode)
10789746b117SMartina Krasteva 		return -ENXIO;
10799746b117SMartina Krasteva 
10809746b117SMartina Krasteva 	/* Request optional reset pin */
10819746b117SMartina Krasteva 	imx334->reset_gpio = devm_gpiod_get_optional(imx334->dev, "reset",
10829746b117SMartina Krasteva 						     GPIOD_OUT_LOW);
10839746b117SMartina Krasteva 	if (IS_ERR(imx334->reset_gpio)) {
10849746b117SMartina Krasteva 		dev_err(imx334->dev, "failed to get reset gpio %ld",
10859746b117SMartina Krasteva 			PTR_ERR(imx334->reset_gpio));
10869746b117SMartina Krasteva 		return PTR_ERR(imx334->reset_gpio);
10879746b117SMartina Krasteva 	}
10889746b117SMartina Krasteva 
10899746b117SMartina Krasteva 	/* Get sensor input clock */
10909746b117SMartina Krasteva 	imx334->inclk = devm_clk_get(imx334->dev, NULL);
10919746b117SMartina Krasteva 	if (IS_ERR(imx334->inclk)) {
10929746b117SMartina Krasteva 		dev_err(imx334->dev, "could not get inclk");
10939746b117SMartina Krasteva 		return PTR_ERR(imx334->inclk);
10949746b117SMartina Krasteva 	}
10959746b117SMartina Krasteva 
10969746b117SMartina Krasteva 	rate = clk_get_rate(imx334->inclk);
10979746b117SMartina Krasteva 	if (rate != IMX334_INCLK_RATE) {
10989746b117SMartina Krasteva 		dev_err(imx334->dev, "inclk frequency mismatch");
10999746b117SMartina Krasteva 		return -EINVAL;
11009746b117SMartina Krasteva 	}
11019746b117SMartina Krasteva 
11029746b117SMartina Krasteva 	ep = fwnode_graph_get_next_endpoint(fwnode, NULL);
11039746b117SMartina Krasteva 	if (!ep)
11049746b117SMartina Krasteva 		return -ENXIO;
11059746b117SMartina Krasteva 
11069746b117SMartina Krasteva 	ret = v4l2_fwnode_endpoint_alloc_parse(ep, &bus_cfg);
11079746b117SMartina Krasteva 	fwnode_handle_put(ep);
11089746b117SMartina Krasteva 	if (ret)
11099746b117SMartina Krasteva 		return ret;
11109746b117SMartina Krasteva 
11119746b117SMartina Krasteva 	if (bus_cfg.bus.mipi_csi2.num_data_lanes != IMX334_NUM_DATA_LANES) {
11129746b117SMartina Krasteva 		dev_err(imx334->dev,
11139746b117SMartina Krasteva 			"number of CSI2 data lanes %d is not supported",
11149746b117SMartina Krasteva 			bus_cfg.bus.mipi_csi2.num_data_lanes);
11159746b117SMartina Krasteva 		ret = -EINVAL;
11169746b117SMartina Krasteva 		goto done_endpoint_free;
11179746b117SMartina Krasteva 	}
11189746b117SMartina Krasteva 
11199746b117SMartina Krasteva 	if (!bus_cfg.nr_of_link_frequencies) {
11209746b117SMartina Krasteva 		dev_err(imx334->dev, "no link frequencies defined");
11219746b117SMartina Krasteva 		ret = -EINVAL;
11229746b117SMartina Krasteva 		goto done_endpoint_free;
11239746b117SMartina Krasteva 	}
11249746b117SMartina Krasteva 
11259746b117SMartina Krasteva 	for (i = 0; i < bus_cfg.nr_of_link_frequencies; i++) {
11269746b117SMartina Krasteva 		for (j = 0; j < ARRAY_SIZE(link_freq); j++) {
11279746b117SMartina Krasteva 			if (bus_cfg.link_frequencies[i] == link_freq[j]) {
11289746b117SMartina Krasteva 				set_bit(j, &imx334->menu_skip_mask);
11299746b117SMartina Krasteva 				break;
11309746b117SMartina Krasteva 			}
11319746b117SMartina Krasteva 		}
11329746b117SMartina Krasteva 
11339746b117SMartina Krasteva 		if (j == ARRAY_SIZE(link_freq)) {
11349746b117SMartina Krasteva 			ret = dev_err_probe(imx334->dev, -EINVAL,
11359746b117SMartina Krasteva 					    "no supported link freq found\n");
11369746b117SMartina Krasteva 			goto done_endpoint_free;
11379746b117SMartina Krasteva 		}
11389746b117SMartina Krasteva 	}
11399746b117SMartina Krasteva 
11409746b117SMartina Krasteva done_endpoint_free:
11419746b117SMartina Krasteva 	v4l2_fwnode_endpoint_free(&bus_cfg);
11429746b117SMartina Krasteva 
11439746b117SMartina Krasteva 	return ret;
11449746b117SMartina Krasteva }
11459746b117SMartina Krasteva 
11469746b117SMartina Krasteva /* V4l2 subdevice ops */
11479746b117SMartina Krasteva static const struct v4l2_subdev_video_ops imx334_video_ops = {
11489746b117SMartina Krasteva 	.s_stream = imx334_set_stream,
11499746b117SMartina Krasteva };
11509746b117SMartina Krasteva 
11519746b117SMartina Krasteva static const struct v4l2_subdev_pad_ops imx334_pad_ops = {
11529746b117SMartina Krasteva 	.init_cfg = imx334_init_pad_cfg,
11539746b117SMartina Krasteva 	.enum_mbus_code = imx334_enum_mbus_code,
11549746b117SMartina Krasteva 	.enum_frame_size = imx334_enum_frame_size,
11559746b117SMartina Krasteva 	.get_fmt = imx334_get_pad_format,
11569746b117SMartina Krasteva 	.set_fmt = imx334_set_pad_format,
11579746b117SMartina Krasteva };
11589746b117SMartina Krasteva 
11599746b117SMartina Krasteva static const struct v4l2_subdev_ops imx334_subdev_ops = {
11609746b117SMartina Krasteva 	.video = &imx334_video_ops,
11619746b117SMartina Krasteva 	.pad = &imx334_pad_ops,
11629746b117SMartina Krasteva };
11639746b117SMartina Krasteva 
11649746b117SMartina Krasteva /**
11659746b117SMartina Krasteva  * imx334_power_on() - Sensor power on sequence
11669746b117SMartina Krasteva  * @dev: pointer to i2c device
11679746b117SMartina Krasteva  *
11689746b117SMartina Krasteva  * Return: 0 if successful, error code otherwise.
11699746b117SMartina Krasteva  */
imx334_power_on(struct device * dev)11709746b117SMartina Krasteva static int imx334_power_on(struct device *dev)
11719746b117SMartina Krasteva {
11729746b117SMartina Krasteva 	struct v4l2_subdev *sd = dev_get_drvdata(dev);
11739746b117SMartina Krasteva 	struct imx334 *imx334 = to_imx334(sd);
11749746b117SMartina Krasteva 	int ret;
11759746b117SMartina Krasteva 
11769746b117SMartina Krasteva 	gpiod_set_value_cansleep(imx334->reset_gpio, 1);
11779746b117SMartina Krasteva 
11789746b117SMartina Krasteva 	ret = clk_prepare_enable(imx334->inclk);
11799746b117SMartina Krasteva 	if (ret) {
11809746b117SMartina Krasteva 		dev_err(imx334->dev, "fail to enable inclk");
11819746b117SMartina Krasteva 		goto error_reset;
11829746b117SMartina Krasteva 	}
11839746b117SMartina Krasteva 
11849746b117SMartina Krasteva 	usleep_range(18000, 20000);
11859746b117SMartina Krasteva 
11869746b117SMartina Krasteva 	return 0;
11879746b117SMartina Krasteva 
11889746b117SMartina Krasteva error_reset:
11899746b117SMartina Krasteva 	gpiod_set_value_cansleep(imx334->reset_gpio, 0);
11909746b117SMartina Krasteva 
11919746b117SMartina Krasteva 	return ret;
11929746b117SMartina Krasteva }
11939746b117SMartina Krasteva 
11949746b117SMartina Krasteva /**
11959746b117SMartina Krasteva  * imx334_power_off() - Sensor power off sequence
11969746b117SMartina Krasteva  * @dev: pointer to i2c device
11979746b117SMartina Krasteva  *
11989746b117SMartina Krasteva  * Return: 0 if successful, error code otherwise.
11999746b117SMartina Krasteva  */
imx334_power_off(struct device * dev)12009746b117SMartina Krasteva static int imx334_power_off(struct device *dev)
12019746b117SMartina Krasteva {
12029746b117SMartina Krasteva 	struct v4l2_subdev *sd = dev_get_drvdata(dev);
12039746b117SMartina Krasteva 	struct imx334 *imx334 = to_imx334(sd);
12049746b117SMartina Krasteva 
12059746b117SMartina Krasteva 	gpiod_set_value_cansleep(imx334->reset_gpio, 0);
12069746b117SMartina Krasteva 
12079746b117SMartina Krasteva 	clk_disable_unprepare(imx334->inclk);
12089746b117SMartina Krasteva 
12099746b117SMartina Krasteva 	return 0;
12109746b117SMartina Krasteva }
12119746b117SMartina Krasteva 
12129746b117SMartina Krasteva /**
12139746b117SMartina Krasteva  * imx334_init_controls() - Initialize sensor subdevice controls
12149746b117SMartina Krasteva  * @imx334: pointer to imx334 device
12159746b117SMartina Krasteva  *
12169746b117SMartina Krasteva  * Return: 0 if successful, error code otherwise.
12179746b117SMartina Krasteva  */
imx334_init_controls(struct imx334 * imx334)12189746b117SMartina Krasteva static int imx334_init_controls(struct imx334 *imx334)
12199746b117SMartina Krasteva {
12209746b117SMartina Krasteva 	struct v4l2_ctrl_handler *ctrl_hdlr = &imx334->ctrl_handler;
12219746b117SMartina Krasteva 	const struct imx334_mode *mode = imx334->cur_mode;
12229746b117SMartina Krasteva 	u32 lpfr;
12239746b117SMartina Krasteva 	int ret;
12249746b117SMartina Krasteva 
12259746b117SMartina Krasteva 	ret = v4l2_ctrl_handler_init(ctrl_hdlr, 6);
12269746b117SMartina Krasteva 	if (ret)
12279746b117SMartina Krasteva 		return ret;
12289746b117SMartina Krasteva 
12299746b117SMartina Krasteva 	/* Serialize controls with sensor device */
12309746b117SMartina Krasteva 	ctrl_hdlr->lock = &imx334->mutex;
12319746b117SMartina Krasteva 
12329746b117SMartina Krasteva 	/* Initialize exposure and gain */
12339746b117SMartina Krasteva 	lpfr = mode->vblank + mode->height;
12349746b117SMartina Krasteva 	imx334->exp_ctrl = v4l2_ctrl_new_std(ctrl_hdlr,
12359746b117SMartina Krasteva 					     &imx334_ctrl_ops,
12369746b117SMartina Krasteva 					     V4L2_CID_EXPOSURE,
12379746b117SMartina Krasteva 					     IMX334_EXPOSURE_MIN,
12389746b117SMartina Krasteva 					     lpfr - IMX334_EXPOSURE_OFFSET,
12399746b117SMartina Krasteva 					     IMX334_EXPOSURE_STEP,
12409746b117SMartina Krasteva 					     IMX334_EXPOSURE_DEFAULT);
12419746b117SMartina Krasteva 
12429746b117SMartina Krasteva 	imx334->again_ctrl = v4l2_ctrl_new_std(ctrl_hdlr,
12439746b117SMartina Krasteva 					       &imx334_ctrl_ops,
12449746b117SMartina Krasteva 					       V4L2_CID_ANALOGUE_GAIN,
12459746b117SMartina Krasteva 					       IMX334_AGAIN_MIN,
12469746b117SMartina Krasteva 					       IMX334_AGAIN_MAX,
12479746b117SMartina Krasteva 					       IMX334_AGAIN_STEP,
12489746b117SMartina Krasteva 					       IMX334_AGAIN_DEFAULT);
12499746b117SMartina Krasteva 
12509746b117SMartina Krasteva 	v4l2_ctrl_cluster(2, &imx334->exp_ctrl);
12519746b117SMartina Krasteva 
12529746b117SMartina Krasteva 	imx334->vblank_ctrl = v4l2_ctrl_new_std(ctrl_hdlr,
12539746b117SMartina Krasteva 						&imx334_ctrl_ops,
12549746b117SMartina Krasteva 						V4L2_CID_VBLANK,
12559746b117SMartina Krasteva 						mode->vblank_min,
12569746b117SMartina Krasteva 						mode->vblank_max,
12579746b117SMartina Krasteva 						1, mode->vblank);
12589746b117SMartina Krasteva 
12599746b117SMartina Krasteva 	/* Read only controls */
12609746b117SMartina Krasteva 	imx334->pclk_ctrl = v4l2_ctrl_new_std(ctrl_hdlr,
12619746b117SMartina Krasteva 					      &imx334_ctrl_ops,
12629746b117SMartina Krasteva 					      V4L2_CID_PIXEL_RATE,
12639746b117SMartina Krasteva 					      mode->pclk, mode->pclk,
12649746b117SMartina Krasteva 					      1, mode->pclk);
12659746b117SMartina Krasteva 
12669746b117SMartina Krasteva 	imx334->link_freq_ctrl = v4l2_ctrl_new_int_menu(ctrl_hdlr,
12679746b117SMartina Krasteva 							&imx334_ctrl_ops,
12689746b117SMartina Krasteva 							V4L2_CID_LINK_FREQ,
12699746b117SMartina Krasteva 							__fls(imx334->menu_skip_mask),
12709746b117SMartina Krasteva 							__ffs(imx334->menu_skip_mask),
12719746b117SMartina Krasteva 							link_freq);
12729746b117SMartina Krasteva 
12739746b117SMartina Krasteva 	if (imx334->link_freq_ctrl)
12749746b117SMartina Krasteva 		imx334->link_freq_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
12759746b117SMartina Krasteva 
12769746b117SMartina Krasteva 	imx334->hblank_ctrl = v4l2_ctrl_new_std(ctrl_hdlr,
12779746b117SMartina Krasteva 						&imx334_ctrl_ops,
12789746b117SMartina Krasteva 						V4L2_CID_HBLANK,
12799746b117SMartina Krasteva 						IMX334_REG_MIN,
12809746b117SMartina Krasteva 						IMX334_REG_MAX,
12819746b117SMartina Krasteva 						1, mode->hblank);
12829746b117SMartina Krasteva 	if (imx334->hblank_ctrl)
12839746b117SMartina Krasteva 		imx334->hblank_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
12849746b117SMartina Krasteva 
12859746b117SMartina Krasteva 	if (ctrl_hdlr->error) {
12869746b117SMartina Krasteva 		dev_err(imx334->dev, "control init failed: %d",
12879746b117SMartina Krasteva 			ctrl_hdlr->error);
12889746b117SMartina Krasteva 		v4l2_ctrl_handler_free(ctrl_hdlr);
12899746b117SMartina Krasteva 		return ctrl_hdlr->error;
12909746b117SMartina Krasteva 	}
12919746b117SMartina Krasteva 
12929746b117SMartina Krasteva 	imx334->sd.ctrl_handler = ctrl_hdlr;
12939746b117SMartina Krasteva 
12949746b117SMartina Krasteva 	return 0;
12959746b117SMartina Krasteva }
12969746b117SMartina Krasteva 
12979746b117SMartina Krasteva /**
12989746b117SMartina Krasteva  * imx334_probe() - I2C client device binding
12999746b117SMartina Krasteva  * @client: pointer to i2c client device
13009746b117SMartina Krasteva  *
13019746b117SMartina Krasteva  * Return: 0 if successful, error code otherwise.
13029746b117SMartina Krasteva  */
imx334_probe(struct i2c_client * client)13039746b117SMartina Krasteva static int imx334_probe(struct i2c_client *client)
13049746b117SMartina Krasteva {
1305*7462df58SShravan Chippa 	struct imx334 *imx334;
1306*7462df58SShravan Chippa 	int ret;
13079746b117SMartina Krasteva 
13089746b117SMartina Krasteva 	imx334 = devm_kzalloc(&client->dev, sizeof(*imx334), GFP_KERNEL);
13099746b117SMartina Krasteva 	if (!imx334)
13109746b117SMartina Krasteva 		return -ENOMEM;
13119746b117SMartina Krasteva 
13129746b117SMartina Krasteva 	imx334->dev = &client->dev;
13139746b117SMartina Krasteva 
13149746b117SMartina Krasteva 	/* Initialize subdev */
13159746b117SMartina Krasteva 	v4l2_i2c_subdev_init(&imx334->sd, client, &imx334_subdev_ops);
13169746b117SMartina Krasteva 
13179746b117SMartina Krasteva 	ret = imx334_parse_hw_config(imx334);
13189746b117SMartina Krasteva 	if (ret) {
13199746b117SMartina Krasteva 		dev_err(imx334->dev, "HW configuration is not supported");
13209746b117SMartina Krasteva 		return ret;
13219746b117SMartina Krasteva 	}
13229746b117SMartina Krasteva 
13239746b117SMartina Krasteva 	mutex_init(&imx334->mutex);
13249746b117SMartina Krasteva 
13259746b117SMartina Krasteva 	ret = imx334_power_on(imx334->dev);
13269746b117SMartina Krasteva 	if (ret) {
132715786f7bSSakari Ailus 		dev_err(imx334->dev, "failed to power-on the sensor");
13289746b117SMartina Krasteva 		goto error_mutex_destroy;
13299746b117SMartina Krasteva 	}
13309746b117SMartina Krasteva 
13319746b117SMartina Krasteva 	/* Check module identity */
13329746b117SMartina Krasteva 	ret = imx334_detect(imx334);
13339746b117SMartina Krasteva 	if (ret) {
13349746b117SMartina Krasteva 		dev_err(imx334->dev, "failed to find sensor: %d", ret);
13359746b117SMartina Krasteva 		goto error_power_off;
13369746b117SMartina Krasteva 	}
13379746b117SMartina Krasteva 
13389746b117SMartina Krasteva 	/* Set default mode to max resolution */
13399746b117SMartina Krasteva 	imx334->cur_mode = &supported_modes[__ffs(imx334->menu_skip_mask)];
13409746b117SMartina Krasteva 	imx334->cur_code = imx334_mbus_codes[0];
13419746b117SMartina Krasteva 	imx334->vblank = imx334->cur_mode->vblank;
13429746b117SMartina Krasteva 
13439746b117SMartina Krasteva 	ret = imx334_init_controls(imx334);
13449746b117SMartina Krasteva 	if (ret) {
13459746b117SMartina Krasteva 		dev_err(imx334->dev, "failed to init controls: %d", ret);
13469746b117SMartina Krasteva 		goto error_power_off;
13479746b117SMartina Krasteva 	}
13489746b117SMartina Krasteva 
13499746b117SMartina Krasteva 	/* Initialize subdev */
13509746b117SMartina Krasteva 	imx334->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
13519746b117SMartina Krasteva 	imx334->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
13529746b117SMartina Krasteva 
13539746b117SMartina Krasteva 	/* Initialize source pad */
13549746b117SMartina Krasteva 	imx334->pad.flags = MEDIA_PAD_FL_SOURCE;
13559746b117SMartina Krasteva 	ret = media_entity_pads_init(&imx334->sd.entity, 1, &imx334->pad);
13569746b117SMartina Krasteva 	if (ret) {
13579746b117SMartina Krasteva 		dev_err(imx334->dev, "failed to init entity pads: %d", ret);
1358ed5c2f5fSUwe Kleine-König 		goto error_handler_free;
13599746b117SMartina Krasteva 	}
13609746b117SMartina Krasteva 
13619746b117SMartina Krasteva 	ret = v4l2_async_register_subdev_sensor(&imx334->sd);
13629746b117SMartina Krasteva 	if (ret < 0) {
13639746b117SMartina Krasteva 		dev_err(imx334->dev,
13649746b117SMartina Krasteva 			"failed to register async subdev: %d", ret);
13659746b117SMartina Krasteva 		goto error_media_entity;
13669746b117SMartina Krasteva 	}
13679746b117SMartina Krasteva 
13689746b117SMartina Krasteva 	pm_runtime_set_active(imx334->dev);
13699746b117SMartina Krasteva 	pm_runtime_enable(imx334->dev);
13709746b117SMartina Krasteva 	pm_runtime_idle(imx334->dev);
13719746b117SMartina Krasteva 
13729746b117SMartina Krasteva 	return 0;
13739746b117SMartina Krasteva 
13749746b117SMartina Krasteva error_media_entity:
13759746b117SMartina Krasteva 	media_entity_cleanup(&imx334->sd.entity);
13769746b117SMartina Krasteva error_handler_free:
13779746b117SMartina Krasteva 	v4l2_ctrl_handler_free(imx334->sd.ctrl_handler);
13789746b117SMartina Krasteva error_power_off:
13799746b117SMartina Krasteva 	imx334_power_off(imx334->dev);
13809746b117SMartina Krasteva error_mutex_destroy:
13819746b117SMartina Krasteva 	mutex_destroy(&imx334->mutex);
13829746b117SMartina Krasteva 
13839746b117SMartina Krasteva 	return ret;
13849746b117SMartina Krasteva }
13859746b117SMartina Krasteva 
13869746b117SMartina Krasteva /**
13879746b117SMartina Krasteva  * imx334_remove() - I2C client device unbinding
13889746b117SMartina Krasteva  * @client: pointer to I2C client device
13899746b117SMartina Krasteva  *
13909746b117SMartina Krasteva  * Return: 0 if successful, error code otherwise.
13919746b117SMartina Krasteva  */
imx334_remove(struct i2c_client * client)13929746b117SMartina Krasteva static void imx334_remove(struct i2c_client *client)
13939746b117SMartina Krasteva {
13949746b117SMartina Krasteva 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
13959746b117SMartina Krasteva 	struct imx334 *imx334 = to_imx334(sd);
13969746b117SMartina Krasteva 
13979746b117SMartina Krasteva 	v4l2_async_unregister_subdev(sd);
1398 	media_entity_cleanup(&sd->entity);
1399 	v4l2_ctrl_handler_free(sd->ctrl_handler);
1400 
1401 	pm_runtime_disable(&client->dev);
1402 	pm_runtime_suspended(&client->dev);
1403 
1404 	mutex_destroy(&imx334->mutex);
1405 }
1406 
1407 static const struct dev_pm_ops imx334_pm_ops = {
1408 	SET_RUNTIME_PM_OPS(imx334_power_off, imx334_power_on, NULL)
1409 };
1410 
1411 static const struct of_device_id imx334_of_match[] = {
1412 	{ .compatible = "sony,imx334" },
1413 	{ }
1414 };
1415 
1416 MODULE_DEVICE_TABLE(of, imx334_of_match);
1417 
1418 static struct i2c_driver imx334_driver = {
1419 	.probe = imx334_probe,
1420 	.remove = imx334_remove,
1421 	.driver = {
1422 		.name = "imx334",
1423 		.pm = &imx334_pm_ops,
1424 		.of_match_table = imx334_of_match,
1425 	},
1426 };
1427 
1428 module_i2c_driver(imx334_driver);
1429 
1430 MODULE_DESCRIPTION("Sony imx334 sensor driver");
1431 MODULE_LICENSE("GPL");
1432