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