xref: /openbmc/linux/drivers/media/i2c/ov2680.c (revision a99a041c22e82856cbf5f315e3bd4b913d3ba0ed)
13ee47cadSRui Miguel Silva // SPDX-License-Identifier: GPL-2.0
23ee47cadSRui Miguel Silva /*
33ee47cadSRui Miguel Silva  * Omnivision OV2680 CMOS Image Sensor driver
43ee47cadSRui Miguel Silva  *
53ee47cadSRui Miguel Silva  * Copyright (C) 2018 Linaro Ltd
63ee47cadSRui Miguel Silva  *
73ee47cadSRui Miguel Silva  * Based on OV5640 Sensor Driver
83ee47cadSRui Miguel Silva  * Copyright (C) 2011-2013 Freescale Semiconductor, Inc. All Rights Reserved.
93ee47cadSRui Miguel Silva  * Copyright (C) 2014-2017 Mentor Graphics Inc.
103ee47cadSRui Miguel Silva  *
113ee47cadSRui Miguel Silva  */
123ee47cadSRui Miguel Silva 
133ee47cadSRui Miguel Silva #include <linux/clk.h>
143ee47cadSRui Miguel Silva #include <linux/delay.h>
153ee47cadSRui Miguel Silva #include <linux/err.h>
169289998eSHans de Goede #include <linux/gpio/consumer.h>
173ee47cadSRui Miguel Silva #include <linux/i2c.h>
183ee47cadSRui Miguel Silva #include <linux/init.h>
197c7e33b7SRob Herring #include <linux/mod_devicetable.h>
203ee47cadSRui Miguel Silva #include <linux/module.h>
21990732a6SHans de Goede #include <linux/pm_runtime.h>
229289998eSHans de Goede #include <linux/regmap.h>
233ee47cadSRui Miguel Silva #include <linux/regulator/consumer.h>
243ee47cadSRui Miguel Silva 
259289998eSHans de Goede #include <media/v4l2-cci.h>
263ee47cadSRui Miguel Silva #include <media/v4l2-common.h>
273ee47cadSRui Miguel Silva #include <media/v4l2-ctrls.h>
283ee47cadSRui Miguel Silva #include <media/v4l2-subdev.h>
293ee47cadSRui Miguel Silva 
303ee47cadSRui Miguel Silva #define OV2680_CHIP_ID				0x2680
313ee47cadSRui Miguel Silva 
329289998eSHans de Goede #define OV2680_REG_STREAM_CTRL			CCI_REG8(0x0100)
339289998eSHans de Goede #define OV2680_REG_SOFT_RESET			CCI_REG8(0x0103)
343ee47cadSRui Miguel Silva 
359289998eSHans de Goede #define OV2680_REG_CHIP_ID			CCI_REG16(0x300a)
36*a99a041cSHans de Goede #define OV2680_REG_SC_CMMN_SUB_ID		CCI_REG8(0x302a)
378e50a122SHans de Goede #define OV2680_REG_PLL_MULTIPLIER		CCI_REG16(0x3081)
383ee47cadSRui Miguel Silva 
399289998eSHans de Goede #define OV2680_REG_EXPOSURE_PK			CCI_REG24(0x3500)
409289998eSHans de Goede #define OV2680_REG_R_MANUAL			CCI_REG8(0x3503)
419289998eSHans de Goede #define OV2680_REG_GAIN_PK			CCI_REG16(0x350a)
4263f47529SHans de Goede 
4363f47529SHans de Goede #define OV2680_REG_SENSOR_CTRL_0A		CCI_REG8(0x370a)
4463f47529SHans de Goede 
4563f47529SHans de Goede #define OV2680_REG_HORIZONTAL_START		CCI_REG16(0x3800)
4663f47529SHans de Goede #define OV2680_REG_VERTICAL_START		CCI_REG16(0x3802)
4763f47529SHans de Goede #define OV2680_REG_HORIZONTAL_END		CCI_REG16(0x3804)
4863f47529SHans de Goede #define OV2680_REG_VERTICAL_END			CCI_REG16(0x3806)
4963f47529SHans de Goede #define OV2680_REG_HORIZONTAL_OUTPUT_SIZE	CCI_REG16(0x3808)
5063f47529SHans de Goede #define OV2680_REG_VERTICAL_OUTPUT_SIZE		CCI_REG16(0x380a)
519289998eSHans de Goede #define OV2680_REG_TIMING_HTS			CCI_REG16(0x380c)
529289998eSHans de Goede #define OV2680_REG_TIMING_VTS			CCI_REG16(0x380e)
5363f47529SHans de Goede #define OV2680_REG_ISP_X_WIN			CCI_REG16(0x3810)
5463f47529SHans de Goede #define OV2680_REG_ISP_Y_WIN			CCI_REG16(0x3812)
5563f47529SHans de Goede #define OV2680_REG_X_INC			CCI_REG8(0x3814)
5663f47529SHans de Goede #define OV2680_REG_Y_INC			CCI_REG8(0x3815)
579289998eSHans de Goede #define OV2680_REG_FORMAT1			CCI_REG8(0x3820)
589289998eSHans de Goede #define OV2680_REG_FORMAT2			CCI_REG8(0x3821)
593ee47cadSRui Miguel Silva 
609289998eSHans de Goede #define OV2680_REG_ISP_CTRL00			CCI_REG8(0x5080)
613ee47cadSRui Miguel Silva 
6263f47529SHans de Goede #define OV2680_REG_X_WIN			CCI_REG16(0x5704)
6363f47529SHans de Goede #define OV2680_REG_Y_WIN			CCI_REG16(0x5706)
6463f47529SHans de Goede 
653ee47cadSRui Miguel Silva #define OV2680_FRAME_RATE			30
663ee47cadSRui Miguel Silva 
6763f47529SHans de Goede #define OV2680_NATIVE_WIDTH			1616
6863f47529SHans de Goede #define OV2680_NATIVE_HEIGHT			1216
693b378b35SHans de Goede #define OV2680_NATIVE_START_LEFT		0
703b378b35SHans de Goede #define OV2680_NATIVE_START_TOP			0
7163f47529SHans de Goede #define OV2680_ACTIVE_WIDTH			1600
7263f47529SHans de Goede #define OV2680_ACTIVE_HEIGHT			1200
733b378b35SHans de Goede #define OV2680_ACTIVE_START_LEFT		8
743b378b35SHans de Goede #define OV2680_ACTIVE_START_TOP			8
753b378b35SHans de Goede #define OV2680_MIN_CROP_WIDTH			2
763b378b35SHans de Goede #define OV2680_MIN_CROP_HEIGHT			2
7763f47529SHans de Goede 
7863f47529SHans de Goede /* 66MHz pixel clock: 66MHz / 1704 * 1294 = 30fps */
7963f47529SHans de Goede #define OV2680_PIXELS_PER_LINE			1704
8063f47529SHans de Goede #define OV2680_LINES_PER_FRAME			1294
8163f47529SHans de Goede 
8263f47529SHans de Goede /* If possible send 16 extra rows / lines to the ISP as padding */
8363f47529SHans de Goede #define OV2680_END_MARGIN			16
843ee47cadSRui Miguel Silva 
8505d6bd86SHans de Goede /* Max exposure time is VTS - 8 */
8605d6bd86SHans de Goede #define OV2680_INTEGRATION_TIME_MARGIN		8
8705d6bd86SHans de Goede 
886d6849b2SHans de Goede #define OV2680_DEFAULT_WIDTH			800
896d6849b2SHans de Goede #define OV2680_DEFAULT_HEIGHT			600
906d6849b2SHans de Goede 
9163f47529SHans de Goede /* For enum_frame_size() full-size + binned-/quarter-size */
9263f47529SHans de Goede #define OV2680_FRAME_SIZES			2
933ee47cadSRui Miguel Silva 
943ee47cadSRui Miguel Silva static const char * const ov2680_supply_name[] = {
953ee47cadSRui Miguel Silva 	"DOVDD",
963ee47cadSRui Miguel Silva 	"DVDD",
973ee47cadSRui Miguel Silva 	"AVDD",
983ee47cadSRui Miguel Silva };
993ee47cadSRui Miguel Silva 
1003ee47cadSRui Miguel Silva #define OV2680_NUM_SUPPLIES ARRAY_SIZE(ov2680_supply_name)
1013ee47cadSRui Miguel Silva 
1028e50a122SHans de Goede enum {
1038e50a122SHans de Goede 	OV2680_19_2_MHZ,
1048e50a122SHans de Goede 	OV2680_24_MHZ,
1058e50a122SHans de Goede };
1068e50a122SHans de Goede 
1078e50a122SHans de Goede static const unsigned long ov2680_xvclk_freqs[] = {
1088e50a122SHans de Goede 	[OV2680_19_2_MHZ] = 19200000,
1098e50a122SHans de Goede 	[OV2680_24_MHZ] = 24000000,
1108e50a122SHans de Goede };
1118e50a122SHans de Goede 
1128e50a122SHans de Goede static const u8 ov2680_pll_multipliers[] = {
1138e50a122SHans de Goede 	[OV2680_19_2_MHZ] = 69,
1148e50a122SHans de Goede 	[OV2680_24_MHZ] = 55,
1158e50a122SHans de Goede };
1168e50a122SHans de Goede 
1173ee47cadSRui Miguel Silva struct ov2680_ctrls {
1183ee47cadSRui Miguel Silva 	struct v4l2_ctrl_handler handler;
1193ee47cadSRui Miguel Silva 	struct v4l2_ctrl *exposure;
1203ee47cadSRui Miguel Silva 	struct v4l2_ctrl *gain;
1213ee47cadSRui Miguel Silva 	struct v4l2_ctrl *hflip;
1223ee47cadSRui Miguel Silva 	struct v4l2_ctrl *vflip;
1233ee47cadSRui Miguel Silva 	struct v4l2_ctrl *test_pattern;
1243ee47cadSRui Miguel Silva };
1253ee47cadSRui Miguel Silva 
126f614dfb8SHans de Goede struct ov2680_mode {
1273b378b35SHans de Goede 	struct v4l2_rect		crop;
128f614dfb8SHans de Goede 	struct v4l2_mbus_framefmt	fmt;
129f614dfb8SHans de Goede 	struct v4l2_fract		frame_interval;
13063f47529SHans de Goede 	bool				binning;
13163f47529SHans de Goede 	u16				h_start;
13263f47529SHans de Goede 	u16				v_start;
13363f47529SHans de Goede 	u16				h_end;
13463f47529SHans de Goede 	u16				v_end;
13563f47529SHans de Goede 	u16				h_output_size;
13663f47529SHans de Goede 	u16				v_output_size;
13763f47529SHans de Goede 	u16				hts;
13863f47529SHans de Goede 	u16				vts;
139f614dfb8SHans de Goede };
140f614dfb8SHans de Goede 
1413ee47cadSRui Miguel Silva struct ov2680_dev {
1427adfdecbSHans de Goede 	struct device			*dev;
1439289998eSHans de Goede 	struct regmap			*regmap;
1443ee47cadSRui Miguel Silva 	struct v4l2_subdev		sd;
1453ee47cadSRui Miguel Silva 
1463ee47cadSRui Miguel Silva 	struct media_pad		pad;
1473ee47cadSRui Miguel Silva 	struct clk			*xvclk;
1483ee47cadSRui Miguel Silva 	u32				xvclk_freq;
1498e50a122SHans de Goede 	u8				pll_mult;
1503ee47cadSRui Miguel Silva 	struct regulator_bulk_data	supplies[OV2680_NUM_SUPPLIES];
1513ee47cadSRui Miguel Silva 
152e9305a23SHans de Goede 	struct gpio_desc		*pwdn_gpio;
1533ee47cadSRui Miguel Silva 	struct mutex			lock; /* protect members */
1543ee47cadSRui Miguel Silva 
1553ee47cadSRui Miguel Silva 	bool				is_streaming;
1563ee47cadSRui Miguel Silva 
1573ee47cadSRui Miguel Silva 	struct ov2680_ctrls		ctrls;
158f614dfb8SHans de Goede 	struct ov2680_mode		mode;
1593ee47cadSRui Miguel Silva };
1603ee47cadSRui Miguel Silva 
1613b378b35SHans de Goede static const struct v4l2_rect ov2680_default_crop = {
1623b378b35SHans de Goede 	.left = OV2680_ACTIVE_START_LEFT,
1633b378b35SHans de Goede 	.top = OV2680_ACTIVE_START_TOP,
1643b378b35SHans de Goede 	.width = OV2680_ACTIVE_WIDTH,
1653b378b35SHans de Goede 	.height = OV2680_ACTIVE_HEIGHT,
1663b378b35SHans de Goede };
1673b378b35SHans de Goede 
1683ee47cadSRui Miguel Silva static const char * const test_pattern_menu[] = {
1693ee47cadSRui Miguel Silva 	"Disabled",
1703ee47cadSRui Miguel Silva 	"Color Bars",
1713ee47cadSRui Miguel Silva 	"Random Data",
1723ee47cadSRui Miguel Silva 	"Square",
1733ee47cadSRui Miguel Silva 	"Black Image",
1743ee47cadSRui Miguel Silva };
1753ee47cadSRui Miguel Silva 
1763ee47cadSRui Miguel Silva static const int ov2680_hv_flip_bayer_order[] = {
1773ee47cadSRui Miguel Silva 	MEDIA_BUS_FMT_SBGGR10_1X10,
1783ee47cadSRui Miguel Silva 	MEDIA_BUS_FMT_SGRBG10_1X10,
1793ee47cadSRui Miguel Silva 	MEDIA_BUS_FMT_SGBRG10_1X10,
1803ee47cadSRui Miguel Silva 	MEDIA_BUS_FMT_SRGGB10_1X10,
1813ee47cadSRui Miguel Silva };
1823ee47cadSRui Miguel Silva 
18363f47529SHans de Goede static const struct reg_sequence ov2680_global_setting[] = {
1849b8e6ee8SHans de Goede 	/* MIPI PHY, 0x10 -> 0x1c enable bp_c_hs_en_lat and bp_d_hs_en_lat */
1859b8e6ee8SHans de Goede 	{0x3016, 0x1c},
1869b8e6ee8SHans de Goede 
18763f47529SHans de Goede 	/* R MANUAL set exposure and gain to manual (hw does not do auto) */
18863f47529SHans de Goede 	{0x3503, 0x03},
1890a61cf33SHans de Goede 
1909b8e6ee8SHans de Goede 	/* Analog control register tweaks */
1919b8e6ee8SHans de Goede 	{0x3603, 0x39}, /* Reset value 0x99 */
1929b8e6ee8SHans de Goede 	{0x3604, 0x24}, /* Reset value 0x74 */
1939b8e6ee8SHans de Goede 	{0x3621, 0x37}, /* Reset value 0x44 */
1949b8e6ee8SHans de Goede 
1959b8e6ee8SHans de Goede 	/* Sensor control register tweaks */
1969b8e6ee8SHans de Goede 	{0x3701, 0x64}, /* Reset value 0x61 */
1979b8e6ee8SHans de Goede 	{0x3705, 0x3c}, /* Reset value 0x21 */
1989b8e6ee8SHans de Goede 	{0x370c, 0x50}, /* Reset value 0x10 */
1999b8e6ee8SHans de Goede 	{0x370d, 0xc0}, /* Reset value 0x00 */
2009b8e6ee8SHans de Goede 	{0x3718, 0x88}, /* Reset value 0x80 */
2019b8e6ee8SHans de Goede 
2029b8e6ee8SHans de Goede 	/* PSRAM tweaks */
2039b8e6ee8SHans de Goede 	{0x3781, 0x80}, /* Reset value 0x00 */
2049b8e6ee8SHans de Goede 	{0x3784, 0x0c}, /* Reset value 0x00, based on OV2680_R1A_AM10.ovt */
2059b8e6ee8SHans de Goede 	{0x3789, 0x60}, /* Reset value 0x50 */
2069b8e6ee8SHans de Goede 
2079b8e6ee8SHans de Goede 	/* BLC CTRL00 0x01 -> 0x81 set avg_weight to 8 */
2089b8e6ee8SHans de Goede 	{0x4000, 0x81},
2099b8e6ee8SHans de Goede 
2100a61cf33SHans de Goede 	/* Set black level compensation range to 0 - 3 (default 0 - 11) */
2110a61cf33SHans de Goede 	{0x4008, 0x00},
2120a61cf33SHans de Goede 	{0x4009, 0x03},
2130a61cf33SHans de Goede 
2149b8e6ee8SHans de Goede 	/* VFIFO R2 0x00 -> 0x02 set Frame reset enable */
2159b8e6ee8SHans de Goede 	{0x4602, 0x02},
2169b8e6ee8SHans de Goede 
2179b8e6ee8SHans de Goede 	/* MIPI ctrl CLK PREPARE MIN change from 0x26 (38) -> 0x36 (54) */
2189b8e6ee8SHans de Goede 	{0x481f, 0x36},
2199b8e6ee8SHans de Goede 
2209b8e6ee8SHans de Goede 	/* MIPI ctrl CLK LPX P MIN change from 0x32 (50) -> 0x36 (54) */
2219b8e6ee8SHans de Goede 	{0x4825, 0x36},
2229b8e6ee8SHans de Goede 
2239b8e6ee8SHans de Goede 	/* R ISP CTRL2 0x20 -> 0x30, set sof_sel bit */
2249b8e6ee8SHans de Goede 	{0x5002, 0x30},
2259b8e6ee8SHans de Goede 
22663f47529SHans de Goede 	/*
22763f47529SHans de Goede 	 * Window CONTROL 0x00 -> 0x01, enable manual window control,
22863f47529SHans de Goede 	 * this is necessary for full size flip and mirror support.
22963f47529SHans de Goede 	 */
23063f47529SHans de Goede 	{0x5708, 0x01},
2319b8e6ee8SHans de Goede 
2329b8e6ee8SHans de Goede 	/*
2339b8e6ee8SHans de Goede 	 * DPC CTRL0 0x14 -> 0x3e, set enable_tail, enable_3x3_cluster
2349b8e6ee8SHans de Goede 	 * and enable_general_tail bits based OV2680_R1A_AM10.ovt.
2359b8e6ee8SHans de Goede 	 */
2369b8e6ee8SHans de Goede 	{0x5780, 0x3e},
2379b8e6ee8SHans de Goede 
2389b8e6ee8SHans de Goede 	/* DPC MORE CONNECTION CASE THRE 0x0c (12) -> 0x02 (2) */
2399b8e6ee8SHans de Goede 	{0x5788, 0x02},
2409b8e6ee8SHans de Goede 
2419b8e6ee8SHans de Goede 	/* DPC GAIN LIST1 0x0f (15) -> 0x08 (8) */
2429b8e6ee8SHans de Goede 	{0x578e, 0x08},
2439b8e6ee8SHans de Goede 
2449b8e6ee8SHans de Goede 	/* DPC GAIN LIST2 0x3f (63) -> 0x0c (12) */
2459b8e6ee8SHans de Goede 	{0x578f, 0x0c},
2469b8e6ee8SHans de Goede 
2479b8e6ee8SHans de Goede 	/* DPC THRE RATIO 0x04 (4) -> 0x00 (0) */
2489b8e6ee8SHans de Goede 	{0x5792, 0x00},
2493ee47cadSRui Miguel Silva };
2503ee47cadSRui Miguel Silva 
2513ee47cadSRui Miguel Silva static struct ov2680_dev *to_ov2680_dev(struct v4l2_subdev *sd)
2523ee47cadSRui Miguel Silva {
2533ee47cadSRui Miguel Silva 	return container_of(sd, struct ov2680_dev, sd);
2543ee47cadSRui Miguel Silva }
2553ee47cadSRui Miguel Silva 
2563ee47cadSRui Miguel Silva static inline struct v4l2_subdev *ctrl_to_sd(struct v4l2_ctrl *ctrl)
2573ee47cadSRui Miguel Silva {
2583ee47cadSRui Miguel Silva 	return &container_of(ctrl->handler, struct ov2680_dev,
2593ee47cadSRui Miguel Silva 			     ctrls.handler)->sd;
2603ee47cadSRui Miguel Silva }
2613ee47cadSRui Miguel Silva 
2623ee47cadSRui Miguel Silva static void ov2680_power_up(struct ov2680_dev *sensor)
2633ee47cadSRui Miguel Silva {
264e9305a23SHans de Goede 	if (!sensor->pwdn_gpio)
2653ee47cadSRui Miguel Silva 		return;
2663ee47cadSRui Miguel Silva 
267e9305a23SHans de Goede 	gpiod_set_value(sensor->pwdn_gpio, 0);
2683ee47cadSRui Miguel Silva 	usleep_range(5000, 10000);
2693ee47cadSRui Miguel Silva }
2703ee47cadSRui Miguel Silva 
2713ee47cadSRui Miguel Silva static void ov2680_power_down(struct ov2680_dev *sensor)
2723ee47cadSRui Miguel Silva {
273e9305a23SHans de Goede 	if (!sensor->pwdn_gpio)
2743ee47cadSRui Miguel Silva 		return;
2753ee47cadSRui Miguel Silva 
276e9305a23SHans de Goede 	gpiod_set_value(sensor->pwdn_gpio, 1);
2773ee47cadSRui Miguel Silva 	usleep_range(5000, 10000);
2783ee47cadSRui Miguel Silva }
2793ee47cadSRui Miguel Silva 
2806d6849b2SHans de Goede static void ov2680_set_bayer_order(struct ov2680_dev *sensor,
2816d6849b2SHans de Goede 				   struct v4l2_mbus_framefmt *fmt)
2823ee47cadSRui Miguel Silva {
28350a7bad4SHans de Goede 	int hv_flip = 0;
2843ee47cadSRui Miguel Silva 
28550a7bad4SHans de Goede 	if (sensor->ctrls.vflip && sensor->ctrls.vflip->val)
28650a7bad4SHans de Goede 		hv_flip += 1;
2873ee47cadSRui Miguel Silva 
28850a7bad4SHans de Goede 	if (sensor->ctrls.hflip && sensor->ctrls.hflip->val)
28950a7bad4SHans de Goede 		hv_flip += 2;
2903ee47cadSRui Miguel Silva 
2916d6849b2SHans de Goede 	fmt->code = ov2680_hv_flip_bayer_order[hv_flip];
2926d6849b2SHans de Goede }
2936d6849b2SHans de Goede 
29423321b91SHans de Goede static struct v4l2_mbus_framefmt *
29523321b91SHans de Goede __ov2680_get_pad_format(struct ov2680_dev *sensor,
29623321b91SHans de Goede 			struct v4l2_subdev_state *state,
29723321b91SHans de Goede 			unsigned int pad,
29823321b91SHans de Goede 			enum v4l2_subdev_format_whence which)
29923321b91SHans de Goede {
30023321b91SHans de Goede 	if (which == V4L2_SUBDEV_FORMAT_TRY)
30123321b91SHans de Goede 		return v4l2_subdev_get_try_format(&sensor->sd, state, pad);
30223321b91SHans de Goede 
30323321b91SHans de Goede 	return &sensor->mode.fmt;
30423321b91SHans de Goede }
30523321b91SHans de Goede 
3063b378b35SHans de Goede static struct v4l2_rect *
3073b378b35SHans de Goede __ov2680_get_pad_crop(struct ov2680_dev *sensor,
3083b378b35SHans de Goede 		      struct v4l2_subdev_state *state,
3093b378b35SHans de Goede 		      unsigned int pad,
3103b378b35SHans de Goede 		      enum v4l2_subdev_format_whence which)
3113b378b35SHans de Goede {
3123b378b35SHans de Goede 	if (which == V4L2_SUBDEV_FORMAT_TRY)
3133b378b35SHans de Goede 		return v4l2_subdev_get_try_crop(&sensor->sd, state, pad);
3143b378b35SHans de Goede 
3153b378b35SHans de Goede 	return &sensor->mode.crop;
3163b378b35SHans de Goede }
3173b378b35SHans de Goede 
3186d6849b2SHans de Goede static void ov2680_fill_format(struct ov2680_dev *sensor,
3196d6849b2SHans de Goede 			       struct v4l2_mbus_framefmt *fmt,
3206d6849b2SHans de Goede 			       unsigned int width, unsigned int height)
3216d6849b2SHans de Goede {
3226d6849b2SHans de Goede 	memset(fmt, 0, sizeof(*fmt));
3236d6849b2SHans de Goede 	fmt->width = width;
3246d6849b2SHans de Goede 	fmt->height = height;
3256d6849b2SHans de Goede 	fmt->field = V4L2_FIELD_NONE;
3266d6849b2SHans de Goede 	fmt->colorspace = V4L2_COLORSPACE_SRGB;
3276d6849b2SHans de Goede 	ov2680_set_bayer_order(sensor, fmt);
3283ee47cadSRui Miguel Silva }
3293ee47cadSRui Miguel Silva 
33063f47529SHans de Goede static void ov2680_calc_mode(struct ov2680_dev *sensor)
33163f47529SHans de Goede {
33263f47529SHans de Goede 	int width = sensor->mode.fmt.width;
33363f47529SHans de Goede 	int height = sensor->mode.fmt.height;
33463f47529SHans de Goede 	int orig_width = width;
33563f47529SHans de Goede 	int orig_height = height;
33663f47529SHans de Goede 
3373b378b35SHans de Goede 	if (width  <= (sensor->mode.crop.width / 2) &&
3383b378b35SHans de Goede 	    height <= (sensor->mode.crop.height / 2)) {
33963f47529SHans de Goede 		sensor->mode.binning = true;
34063f47529SHans de Goede 		width *= 2;
34163f47529SHans de Goede 		height *= 2;
34263f47529SHans de Goede 	} else {
34363f47529SHans de Goede 		sensor->mode.binning = false;
34463f47529SHans de Goede 	}
34563f47529SHans de Goede 
3463b378b35SHans de Goede 	sensor->mode.h_start = (sensor->mode.crop.left +
3473b378b35SHans de Goede 				(sensor->mode.crop.width - width) / 2) & ~1;
3483b378b35SHans de Goede 	sensor->mode.v_start = (sensor->mode.crop.top +
3493b378b35SHans de Goede 				(sensor->mode.crop.height - height) / 2) & ~1;
35063f47529SHans de Goede 	sensor->mode.h_end =
35163f47529SHans de Goede 		min(sensor->mode.h_start + width + OV2680_END_MARGIN - 1,
35263f47529SHans de Goede 		    OV2680_NATIVE_WIDTH - 1);
35363f47529SHans de Goede 	sensor->mode.v_end =
35463f47529SHans de Goede 		min(sensor->mode.v_start + height + OV2680_END_MARGIN - 1,
35563f47529SHans de Goede 		    OV2680_NATIVE_HEIGHT - 1);
35663f47529SHans de Goede 	sensor->mode.h_output_size = orig_width;
35763f47529SHans de Goede 	sensor->mode.v_output_size = orig_height;
35863f47529SHans de Goede 	sensor->mode.hts = OV2680_PIXELS_PER_LINE;
35963f47529SHans de Goede 	sensor->mode.vts = OV2680_LINES_PER_FRAME;
36063f47529SHans de Goede }
36163f47529SHans de Goede 
36263f47529SHans de Goede static int ov2680_set_mode(struct ov2680_dev *sensor)
36363f47529SHans de Goede {
36463f47529SHans de Goede 	u8 sensor_ctrl_0a, inc, fmt1, fmt2;
36563f47529SHans de Goede 	int ret = 0;
36663f47529SHans de Goede 
36763f47529SHans de Goede 	if (sensor->mode.binning) {
36863f47529SHans de Goede 		sensor_ctrl_0a = 0x23;
36963f47529SHans de Goede 		inc = 0x31;
37063f47529SHans de Goede 		fmt1 = 0xc2;
37163f47529SHans de Goede 		fmt2 = 0x01;
37263f47529SHans de Goede 	} else {
37363f47529SHans de Goede 		sensor_ctrl_0a = 0x21;
37463f47529SHans de Goede 		inc = 0x11;
37563f47529SHans de Goede 		fmt1 = 0xc0;
37663f47529SHans de Goede 		fmt2 = 0x00;
37763f47529SHans de Goede 	}
37863f47529SHans de Goede 
37963f47529SHans de Goede 	cci_write(sensor->regmap, OV2680_REG_SENSOR_CTRL_0A,
38063f47529SHans de Goede 		  sensor_ctrl_0a, &ret);
38163f47529SHans de Goede 	cci_write(sensor->regmap, OV2680_REG_HORIZONTAL_START,
38263f47529SHans de Goede 		  sensor->mode.h_start, &ret);
38363f47529SHans de Goede 	cci_write(sensor->regmap, OV2680_REG_VERTICAL_START,
38463f47529SHans de Goede 		  sensor->mode.v_start, &ret);
38563f47529SHans de Goede 	cci_write(sensor->regmap, OV2680_REG_HORIZONTAL_END,
38663f47529SHans de Goede 		  sensor->mode.h_end, &ret);
38763f47529SHans de Goede 	cci_write(sensor->regmap, OV2680_REG_VERTICAL_END,
38863f47529SHans de Goede 		  sensor->mode.v_end, &ret);
38963f47529SHans de Goede 	cci_write(sensor->regmap, OV2680_REG_HORIZONTAL_OUTPUT_SIZE,
39063f47529SHans de Goede 		  sensor->mode.h_output_size, &ret);
39163f47529SHans de Goede 	cci_write(sensor->regmap, OV2680_REG_VERTICAL_OUTPUT_SIZE,
39263f47529SHans de Goede 		  sensor->mode.v_output_size, &ret);
39363f47529SHans de Goede 	cci_write(sensor->regmap, OV2680_REG_TIMING_HTS,
39463f47529SHans de Goede 		  sensor->mode.hts, &ret);
39563f47529SHans de Goede 	cci_write(sensor->regmap, OV2680_REG_TIMING_VTS,
39663f47529SHans de Goede 		  sensor->mode.vts, &ret);
39763f47529SHans de Goede 	cci_write(sensor->regmap, OV2680_REG_ISP_X_WIN, 0, &ret);
39863f47529SHans de Goede 	cci_write(sensor->regmap, OV2680_REG_ISP_Y_WIN, 0, &ret);
39963f47529SHans de Goede 	cci_write(sensor->regmap, OV2680_REG_X_INC, inc, &ret);
40063f47529SHans de Goede 	cci_write(sensor->regmap, OV2680_REG_Y_INC, inc, &ret);
40163f47529SHans de Goede 	cci_write(sensor->regmap, OV2680_REG_X_WIN,
40263f47529SHans de Goede 		  sensor->mode.h_output_size, &ret);
40363f47529SHans de Goede 	cci_write(sensor->regmap, OV2680_REG_Y_WIN,
40463f47529SHans de Goede 		  sensor->mode.v_output_size, &ret);
40563f47529SHans de Goede 	cci_write(sensor->regmap, OV2680_REG_FORMAT1, fmt1, &ret);
40663f47529SHans de Goede 	cci_write(sensor->regmap, OV2680_REG_FORMAT2, fmt2, &ret);
40763f47529SHans de Goede 
40863f47529SHans de Goede 	return ret;
40963f47529SHans de Goede }
41063f47529SHans de Goede 
411d5d08ad3SHans de Goede static int ov2680_set_vflip(struct ov2680_dev *sensor, s32 val)
4123ee47cadSRui Miguel Silva {
4133ee47cadSRui Miguel Silva 	int ret;
4143ee47cadSRui Miguel Silva 
415d5d08ad3SHans de Goede 	if (sensor->is_streaming)
416d5d08ad3SHans de Goede 		return -EBUSY;
417d5d08ad3SHans de Goede 
4189289998eSHans de Goede 	ret = cci_update_bits(sensor->regmap, OV2680_REG_FORMAT1,
4199289998eSHans de Goede 			      BIT(2), val ? BIT(2) : 0, NULL);
4203ee47cadSRui Miguel Silva 	if (ret < 0)
4213ee47cadSRui Miguel Silva 		return ret;
4223ee47cadSRui Miguel Silva 
423f614dfb8SHans de Goede 	ov2680_set_bayer_order(sensor, &sensor->mode.fmt);
42450a7bad4SHans de Goede 	return 0;
4253ee47cadSRui Miguel Silva }
4263ee47cadSRui Miguel Silva 
427d5d08ad3SHans de Goede static int ov2680_set_hflip(struct ov2680_dev *sensor, s32 val)
4283ee47cadSRui Miguel Silva {
4293ee47cadSRui Miguel Silva 	int ret;
4303ee47cadSRui Miguel Silva 
431d5d08ad3SHans de Goede 	if (sensor->is_streaming)
432d5d08ad3SHans de Goede 		return -EBUSY;
4333ee47cadSRui Miguel Silva 
4349289998eSHans de Goede 	ret = cci_update_bits(sensor->regmap, OV2680_REG_FORMAT2,
4359289998eSHans de Goede 			      BIT(2), val ? BIT(2) : 0, NULL);
4363ee47cadSRui Miguel Silva 	if (ret < 0)
4373ee47cadSRui Miguel Silva 		return ret;
4383ee47cadSRui Miguel Silva 
439f614dfb8SHans de Goede 	ov2680_set_bayer_order(sensor, &sensor->mode.fmt);
44050a7bad4SHans de Goede 	return 0;
4413ee47cadSRui Miguel Silva }
4423ee47cadSRui Miguel Silva 
4433ee47cadSRui Miguel Silva static int ov2680_test_pattern_set(struct ov2680_dev *sensor, int value)
4443ee47cadSRui Miguel Silva {
4459289998eSHans de Goede 	int ret = 0;
4463ee47cadSRui Miguel Silva 
4473ee47cadSRui Miguel Silva 	if (!value)
4489289998eSHans de Goede 		return cci_update_bits(sensor->regmap, OV2680_REG_ISP_CTRL00,
4499289998eSHans de Goede 				       BIT(7), 0, NULL);
4503ee47cadSRui Miguel Silva 
4519289998eSHans de Goede 	cci_update_bits(sensor->regmap, OV2680_REG_ISP_CTRL00,
4529289998eSHans de Goede 			0x03, value - 1, &ret);
4539289998eSHans de Goede 	cci_update_bits(sensor->regmap, OV2680_REG_ISP_CTRL00,
4549289998eSHans de Goede 			BIT(7), BIT(7), &ret);
4559289998eSHans de Goede 
4563ee47cadSRui Miguel Silva 	return ret;
4573ee47cadSRui Miguel Silva }
4583ee47cadSRui Miguel Silva 
4597b5a42e6SHans de Goede static int ov2680_gain_set(struct ov2680_dev *sensor, u32 gain)
4603ee47cadSRui Miguel Silva {
4619289998eSHans de Goede 	return cci_write(sensor->regmap, OV2680_REG_GAIN_PK, gain, NULL);
4623ee47cadSRui Miguel Silva }
4633ee47cadSRui Miguel Silva 
4647b5a42e6SHans de Goede static int ov2680_exposure_set(struct ov2680_dev *sensor, u32 exp)
4653ee47cadSRui Miguel Silva {
4669289998eSHans de Goede 	return cci_write(sensor->regmap, OV2680_REG_EXPOSURE_PK, exp << 4,
4679289998eSHans de Goede 			 NULL);
4683ee47cadSRui Miguel Silva }
4693ee47cadSRui Miguel Silva 
4703ee47cadSRui Miguel Silva static int ov2680_stream_enable(struct ov2680_dev *sensor)
4713ee47cadSRui Miguel Silva {
4723ee47cadSRui Miguel Silva 	int ret;
4733ee47cadSRui Miguel Silva 
4748e50a122SHans de Goede 	ret = cci_write(sensor->regmap, OV2680_REG_PLL_MULTIPLIER,
4758e50a122SHans de Goede 			sensor->pll_mult, NULL);
4768e50a122SHans de Goede 	if (ret < 0)
4778e50a122SHans de Goede 		return ret;
4788e50a122SHans de Goede 
4799289998eSHans de Goede 	ret = regmap_multi_reg_write(sensor->regmap,
48063f47529SHans de Goede 				     ov2680_global_setting,
48163f47529SHans de Goede 				     ARRAY_SIZE(ov2680_global_setting));
482990732a6SHans de Goede 	if (ret < 0)
483990732a6SHans de Goede 		return ret;
484990732a6SHans de Goede 
48563f47529SHans de Goede 	ret = ov2680_set_mode(sensor);
4863ee47cadSRui Miguel Silva 	if (ret < 0)
4873ee47cadSRui Miguel Silva 		return ret;
4883ee47cadSRui Miguel Silva 
4897b5a42e6SHans de Goede 	/* Restore value of all ctrls */
4907b5a42e6SHans de Goede 	ret = __v4l2_ctrl_handler_setup(&sensor->ctrls.handler);
4913ee47cadSRui Miguel Silva 	if (ret < 0)
4923ee47cadSRui Miguel Silva 		return ret;
4933ee47cadSRui Miguel Silva 
494990732a6SHans de Goede 	return cci_write(sensor->regmap, OV2680_REG_STREAM_CTRL, 1, NULL);
4953ee47cadSRui Miguel Silva }
4963ee47cadSRui Miguel Silva 
497990732a6SHans de Goede static int ov2680_stream_disable(struct ov2680_dev *sensor)
4983ee47cadSRui Miguel Silva {
499990732a6SHans de Goede 	return cci_write(sensor->regmap, OV2680_REG_STREAM_CTRL, 0, NULL);
5003ee47cadSRui Miguel Silva }
5013ee47cadSRui Miguel Silva 
5023ee47cadSRui Miguel Silva static int ov2680_power_off(struct ov2680_dev *sensor)
5033ee47cadSRui Miguel Silva {
5043ee47cadSRui Miguel Silva 	clk_disable_unprepare(sensor->xvclk);
5053ee47cadSRui Miguel Silva 	ov2680_power_down(sensor);
5063ee47cadSRui Miguel Silva 	regulator_bulk_disable(OV2680_NUM_SUPPLIES, sensor->supplies);
5073ee47cadSRui Miguel Silva 	return 0;
5083ee47cadSRui Miguel Silva }
5093ee47cadSRui Miguel Silva 
5103ee47cadSRui Miguel Silva static int ov2680_power_on(struct ov2680_dev *sensor)
5113ee47cadSRui Miguel Silva {
5123ee47cadSRui Miguel Silva 	int ret;
5133ee47cadSRui Miguel Silva 
5143ee47cadSRui Miguel Silva 	ret = regulator_bulk_enable(OV2680_NUM_SUPPLIES, sensor->supplies);
5153ee47cadSRui Miguel Silva 	if (ret < 0) {
5167adfdecbSHans de Goede 		dev_err(sensor->dev, "failed to enable regulators: %d\n", ret);
5173ee47cadSRui Miguel Silva 		return ret;
5183ee47cadSRui Miguel Silva 	}
5193ee47cadSRui Miguel Silva 
520e9305a23SHans de Goede 	if (!sensor->pwdn_gpio) {
5219289998eSHans de Goede 		ret = cci_write(sensor->regmap, OV2680_REG_SOFT_RESET, 0x01,
5229289998eSHans de Goede 				NULL);
5233ee47cadSRui Miguel Silva 		if (ret != 0) {
5247adfdecbSHans de Goede 			dev_err(sensor->dev, "sensor soft reset failed\n");
52584b4bd7eSHans de Goede 			goto err_disable_regulators;
5263ee47cadSRui Miguel Silva 		}
5273ee47cadSRui Miguel Silva 		usleep_range(1000, 2000);
5283ee47cadSRui Miguel Silva 	} else {
5293ee47cadSRui Miguel Silva 		ov2680_power_down(sensor);
5303ee47cadSRui Miguel Silva 		ov2680_power_up(sensor);
5313ee47cadSRui Miguel Silva 	}
5323ee47cadSRui Miguel Silva 
5333ee47cadSRui Miguel Silva 	ret = clk_prepare_enable(sensor->xvclk);
5343ee47cadSRui Miguel Silva 	if (ret < 0)
53584b4bd7eSHans de Goede 		goto err_disable_regulators;
5363ee47cadSRui Miguel Silva 
5373ee47cadSRui Miguel Silva 	return 0;
53884b4bd7eSHans de Goede 
53984b4bd7eSHans de Goede err_disable_regulators:
54084b4bd7eSHans de Goede 	regulator_bulk_disable(OV2680_NUM_SUPPLIES, sensor->supplies);
54184b4bd7eSHans de Goede 	return ret;
5423ee47cadSRui Miguel Silva }
5433ee47cadSRui Miguel Silva 
5443ee47cadSRui Miguel Silva static int ov2680_s_g_frame_interval(struct v4l2_subdev *sd,
5453ee47cadSRui Miguel Silva 				     struct v4l2_subdev_frame_interval *fi)
5463ee47cadSRui Miguel Silva {
5473ee47cadSRui Miguel Silva 	struct ov2680_dev *sensor = to_ov2680_dev(sd);
5483ee47cadSRui Miguel Silva 
5493ee47cadSRui Miguel Silva 	mutex_lock(&sensor->lock);
550f614dfb8SHans de Goede 	fi->interval = sensor->mode.frame_interval;
5513ee47cadSRui Miguel Silva 	mutex_unlock(&sensor->lock);
5523ee47cadSRui Miguel Silva 
5533ee47cadSRui Miguel Silva 	return 0;
5543ee47cadSRui Miguel Silva }
5553ee47cadSRui Miguel Silva 
5563ee47cadSRui Miguel Silva static int ov2680_s_stream(struct v4l2_subdev *sd, int enable)
5573ee47cadSRui Miguel Silva {
5583ee47cadSRui Miguel Silva 	struct ov2680_dev *sensor = to_ov2680_dev(sd);
5593ee47cadSRui Miguel Silva 	int ret = 0;
5603ee47cadSRui Miguel Silva 
5613ee47cadSRui Miguel Silva 	mutex_lock(&sensor->lock);
5623ee47cadSRui Miguel Silva 
5633ee47cadSRui Miguel Silva 	if (sensor->is_streaming == !!enable)
5643ee47cadSRui Miguel Silva 		goto unlock;
5653ee47cadSRui Miguel Silva 
566990732a6SHans de Goede 	if (enable) {
567990732a6SHans de Goede 		ret = pm_runtime_resume_and_get(sensor->sd.dev);
5683ee47cadSRui Miguel Silva 		if (ret < 0)
5693ee47cadSRui Miguel Silva 			goto unlock;
5703ee47cadSRui Miguel Silva 
5713ee47cadSRui Miguel Silva 		ret = ov2680_stream_enable(sensor);
572990732a6SHans de Goede 		if (ret < 0) {
573990732a6SHans de Goede 			pm_runtime_put(sensor->sd.dev);
574990732a6SHans de Goede 			goto unlock;
575990732a6SHans de Goede 		}
576990732a6SHans de Goede 	} else {
5773ee47cadSRui Miguel Silva 		ret = ov2680_stream_disable(sensor);
578990732a6SHans de Goede 		pm_runtime_put(sensor->sd.dev);
579990732a6SHans de Goede 	}
5803ee47cadSRui Miguel Silva 
5813ee47cadSRui Miguel Silva 	sensor->is_streaming = !!enable;
5823ee47cadSRui Miguel Silva 
5833ee47cadSRui Miguel Silva unlock:
5843ee47cadSRui Miguel Silva 	mutex_unlock(&sensor->lock);
5853ee47cadSRui Miguel Silva 
5863ee47cadSRui Miguel Silva 	return ret;
5873ee47cadSRui Miguel Silva }
5883ee47cadSRui Miguel Silva 
5893ee47cadSRui Miguel Silva static int ov2680_enum_mbus_code(struct v4l2_subdev *sd,
5900d346d2aSTomi Valkeinen 				 struct v4l2_subdev_state *sd_state,
5913ee47cadSRui Miguel Silva 				 struct v4l2_subdev_mbus_code_enum *code)
5923ee47cadSRui Miguel Silva {
5933ee47cadSRui Miguel Silva 	struct ov2680_dev *sensor = to_ov2680_dev(sd);
5943ee47cadSRui Miguel Silva 
5951259259fSHans de Goede 	if (code->index != 0)
5963ee47cadSRui Miguel Silva 		return -EINVAL;
5973ee47cadSRui Miguel Silva 
598f614dfb8SHans de Goede 	code->code = sensor->mode.fmt.code;
5993ee47cadSRui Miguel Silva 
6003ee47cadSRui Miguel Silva 	return 0;
6013ee47cadSRui Miguel Silva }
6023ee47cadSRui Miguel Silva 
6033ee47cadSRui Miguel Silva static int ov2680_get_fmt(struct v4l2_subdev *sd,
6040d346d2aSTomi Valkeinen 			  struct v4l2_subdev_state *sd_state,
6053ee47cadSRui Miguel Silva 			  struct v4l2_subdev_format *format)
6063ee47cadSRui Miguel Silva {
6073ee47cadSRui Miguel Silva 	struct ov2680_dev *sensor = to_ov2680_dev(sd);
60823321b91SHans de Goede 	struct v4l2_mbus_framefmt *fmt;
6093ee47cadSRui Miguel Silva 
61023321b91SHans de Goede 	fmt = __ov2680_get_pad_format(sensor, sd_state, format->pad,
61123321b91SHans de Goede 				      format->which);
61223321b91SHans de Goede 
6133ee47cadSRui Miguel Silva 	mutex_lock(&sensor->lock);
6143ee47cadSRui Miguel Silva 	format->format = *fmt;
6153ee47cadSRui Miguel Silva 	mutex_unlock(&sensor->lock);
6163ee47cadSRui Miguel Silva 
61749c282d5SHans de Goede 	return 0;
6183ee47cadSRui Miguel Silva }
6193ee47cadSRui Miguel Silva 
6203ee47cadSRui Miguel Silva static int ov2680_set_fmt(struct v4l2_subdev *sd,
6210d346d2aSTomi Valkeinen 			  struct v4l2_subdev_state *sd_state,
6223ee47cadSRui Miguel Silva 			  struct v4l2_subdev_format *format)
6233ee47cadSRui Miguel Silva {
6243ee47cadSRui Miguel Silva 	struct ov2680_dev *sensor = to_ov2680_dev(sd);
6253ee47cadSRui Miguel Silva 	struct v4l2_mbus_framefmt *try_fmt;
6263b378b35SHans de Goede 	const struct v4l2_rect *crop;
62763f47529SHans de Goede 	unsigned int width, height;
6283ee47cadSRui Miguel Silva 	int ret = 0;
6293ee47cadSRui Miguel Silva 
6303b378b35SHans de Goede 	crop = __ov2680_get_pad_crop(sensor, sd_state, format->pad,
6313b378b35SHans de Goede 				     format->which);
6323b378b35SHans de Goede 
6333b378b35SHans de Goede 	/* Limit set_fmt max size to crop width / height */
6343b378b35SHans de Goede 	width = clamp_val(ALIGN(format->format.width, 2),
6353b378b35SHans de Goede 			  OV2680_MIN_CROP_WIDTH, crop->width);
6363b378b35SHans de Goede 	height = clamp_val(ALIGN(format->format.height, 2),
6373b378b35SHans de Goede 			   OV2680_MIN_CROP_HEIGHT, crop->height);
6383ee47cadSRui Miguel Silva 
63963f47529SHans de Goede 	ov2680_fill_format(sensor, &format->format, width, height);
640c0e97a4bSHans de Goede 
6413ee47cadSRui Miguel Silva 	if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
6420d346d2aSTomi Valkeinen 		try_fmt = v4l2_subdev_get_try_format(sd, sd_state, 0);
643c0e97a4bSHans de Goede 		*try_fmt = format->format;
644e521b9ccSHans de Goede 		return 0;
645e521b9ccSHans de Goede 	}
646e521b9ccSHans de Goede 
647e521b9ccSHans de Goede 	mutex_lock(&sensor->lock);
648e521b9ccSHans de Goede 
649e521b9ccSHans de Goede 	if (sensor->is_streaming) {
650e521b9ccSHans de Goede 		ret = -EBUSY;
6513ee47cadSRui Miguel Silva 		goto unlock;
6523ee47cadSRui Miguel Silva 	}
6533ee47cadSRui Miguel Silva 
654f614dfb8SHans de Goede 	sensor->mode.fmt = format->format;
65563f47529SHans de Goede 	ov2680_calc_mode(sensor);
6563ee47cadSRui Miguel Silva 
6573ee47cadSRui Miguel Silva unlock:
6583ee47cadSRui Miguel Silva 	mutex_unlock(&sensor->lock);
6593ee47cadSRui Miguel Silva 
6603ee47cadSRui Miguel Silva 	return ret;
6613ee47cadSRui Miguel Silva }
6623ee47cadSRui Miguel Silva 
6633b378b35SHans de Goede static int ov2680_get_selection(struct v4l2_subdev *sd,
6643b378b35SHans de Goede 				struct v4l2_subdev_state *state,
6653b378b35SHans de Goede 				struct v4l2_subdev_selection *sel)
6663b378b35SHans de Goede {
6673b378b35SHans de Goede 	struct ov2680_dev *sensor = to_ov2680_dev(sd);
6683b378b35SHans de Goede 
6693b378b35SHans de Goede 	switch (sel->target) {
6703b378b35SHans de Goede 	case V4L2_SEL_TGT_CROP:
6713b378b35SHans de Goede 		mutex_lock(&sensor->lock);
6723b378b35SHans de Goede 		sel->r = *__ov2680_get_pad_crop(sensor, state, sel->pad,
6733b378b35SHans de Goede 						sel->which);
6743b378b35SHans de Goede 		mutex_unlock(&sensor->lock);
6753b378b35SHans de Goede 		break;
6763b378b35SHans de Goede 	case V4L2_SEL_TGT_NATIVE_SIZE:
6773b378b35SHans de Goede 	case V4L2_SEL_TGT_CROP_BOUNDS:
6783b378b35SHans de Goede 		sel->r.top = 0;
6793b378b35SHans de Goede 		sel->r.left = 0;
6803b378b35SHans de Goede 		sel->r.width = OV2680_NATIVE_WIDTH;
6813b378b35SHans de Goede 		sel->r.height = OV2680_NATIVE_HEIGHT;
6823b378b35SHans de Goede 		break;
6833b378b35SHans de Goede 	case V4L2_SEL_TGT_CROP_DEFAULT:
6843b378b35SHans de Goede 		sel->r = ov2680_default_crop;
6853b378b35SHans de Goede 		break;
6863b378b35SHans de Goede 	default:
6873b378b35SHans de Goede 		return -EINVAL;
6883b378b35SHans de Goede 	}
6893b378b35SHans de Goede 
6903b378b35SHans de Goede 	return 0;
6913b378b35SHans de Goede }
6923b378b35SHans de Goede 
6933b378b35SHans de Goede static int ov2680_set_selection(struct v4l2_subdev *sd,
6943b378b35SHans de Goede 				struct v4l2_subdev_state *state,
6953b378b35SHans de Goede 				struct v4l2_subdev_selection *sel)
6963b378b35SHans de Goede {
6973b378b35SHans de Goede 	struct ov2680_dev *sensor = to_ov2680_dev(sd);
6983b378b35SHans de Goede 	struct v4l2_mbus_framefmt *format;
6993b378b35SHans de Goede 	struct v4l2_rect *crop;
7003b378b35SHans de Goede 	struct v4l2_rect rect;
7013b378b35SHans de Goede 
7023b378b35SHans de Goede 	if (sel->target != V4L2_SEL_TGT_CROP)
7033b378b35SHans de Goede 		return -EINVAL;
7043b378b35SHans de Goede 
7053b378b35SHans de Goede 	/*
7063b378b35SHans de Goede 	 * Clamp the boundaries of the crop rectangle to the size of the sensor
7073b378b35SHans de Goede 	 * pixel array. Align to multiples of 2 to ensure Bayer pattern isn't
7083b378b35SHans de Goede 	 * disrupted.
7093b378b35SHans de Goede 	 */
7103b378b35SHans de Goede 	rect.left = clamp_val(ALIGN(sel->r.left, 2),
7113b378b35SHans de Goede 			      OV2680_NATIVE_START_LEFT, OV2680_NATIVE_WIDTH);
7123b378b35SHans de Goede 	rect.top = clamp_val(ALIGN(sel->r.top, 2),
7133b378b35SHans de Goede 			     OV2680_NATIVE_START_TOP, OV2680_NATIVE_HEIGHT);
7143b378b35SHans de Goede 	rect.width = clamp_val(ALIGN(sel->r.width, 2),
7153b378b35SHans de Goede 			       OV2680_MIN_CROP_WIDTH, OV2680_NATIVE_WIDTH);
7163b378b35SHans de Goede 	rect.height = clamp_val(ALIGN(sel->r.height, 2),
7173b378b35SHans de Goede 				OV2680_MIN_CROP_HEIGHT, OV2680_NATIVE_HEIGHT);
7183b378b35SHans de Goede 
7193b378b35SHans de Goede 	/* Make sure the crop rectangle isn't outside the bounds of the array */
7203b378b35SHans de Goede 	rect.width = min_t(unsigned int, rect.width,
7213b378b35SHans de Goede 			   OV2680_NATIVE_WIDTH - rect.left);
7223b378b35SHans de Goede 	rect.height = min_t(unsigned int, rect.height,
7233b378b35SHans de Goede 			    OV2680_NATIVE_HEIGHT - rect.top);
7243b378b35SHans de Goede 
7253b378b35SHans de Goede 	crop = __ov2680_get_pad_crop(sensor, state, sel->pad, sel->which);
7263b378b35SHans de Goede 
7273b378b35SHans de Goede 	mutex_lock(&sensor->lock);
7283b378b35SHans de Goede 	if (rect.width != crop->width || rect.height != crop->height) {
7293b378b35SHans de Goede 		/*
7303b378b35SHans de Goede 		 * Reset the output image size if the crop rectangle size has
7313b378b35SHans de Goede 		 * been modified.
7323b378b35SHans de Goede 		 */
7333b378b35SHans de Goede 		format = __ov2680_get_pad_format(sensor, state, sel->pad,
7343b378b35SHans de Goede 						 sel->which);
7353b378b35SHans de Goede 		format->width = rect.width;
7363b378b35SHans de Goede 		format->height = rect.height;
7373b378b35SHans de Goede 	}
7383b378b35SHans de Goede 
7393b378b35SHans de Goede 	*crop = rect;
7403b378b35SHans de Goede 	mutex_unlock(&sensor->lock);
7413b378b35SHans de Goede 
7423b378b35SHans de Goede 	sel->r = rect;
7433b378b35SHans de Goede 
7443b378b35SHans de Goede 	return 0;
7453b378b35SHans de Goede }
7463b378b35SHans de Goede 
7473ee47cadSRui Miguel Silva static int ov2680_init_cfg(struct v4l2_subdev *sd,
7480d346d2aSTomi Valkeinen 			   struct v4l2_subdev_state *sd_state)
7493ee47cadSRui Miguel Silva {
7506d6849b2SHans de Goede 	struct ov2680_dev *sensor = to_ov2680_dev(sd);
7513ee47cadSRui Miguel Silva 
7523b378b35SHans de Goede 	sd_state->pads[0].try_crop = ov2680_default_crop;
7533b378b35SHans de Goede 
7546d6849b2SHans de Goede 	ov2680_fill_format(sensor, &sd_state->pads[0].try_fmt,
7556d6849b2SHans de Goede 			   OV2680_DEFAULT_WIDTH, OV2680_DEFAULT_HEIGHT);
7566d6849b2SHans de Goede 	return 0;
7573ee47cadSRui Miguel Silva }
7583ee47cadSRui Miguel Silva 
7593ee47cadSRui Miguel Silva static int ov2680_enum_frame_size(struct v4l2_subdev *sd,
7600d346d2aSTomi Valkeinen 				  struct v4l2_subdev_state *sd_state,
7613ee47cadSRui Miguel Silva 				  struct v4l2_subdev_frame_size_enum *fse)
7623ee47cadSRui Miguel Silva {
7633b378b35SHans de Goede 	struct ov2680_dev *sensor = to_ov2680_dev(sd);
7643b378b35SHans de Goede 	struct v4l2_rect *crop;
7653b378b35SHans de Goede 
76663f47529SHans de Goede 	if (fse->index >= OV2680_FRAME_SIZES)
7673ee47cadSRui Miguel Silva 		return -EINVAL;
7683ee47cadSRui Miguel Silva 
7693b378b35SHans de Goede 	crop = __ov2680_get_pad_crop(sensor, sd_state, fse->pad, fse->which);
7703b378b35SHans de Goede 	if (!crop)
7713b378b35SHans de Goede 		return -EINVAL;
7723b378b35SHans de Goede 
7733b378b35SHans de Goede 	fse->min_width = crop->width / (fse->index + 1);
7743b378b35SHans de Goede 	fse->min_height = crop->height / (fse->index + 1);
77563f47529SHans de Goede 	fse->max_width = fse->min_width;
77663f47529SHans de Goede 	fse->max_height = fse->min_height;
7773ee47cadSRui Miguel Silva 
7783ee47cadSRui Miguel Silva 	return 0;
7793ee47cadSRui Miguel Silva }
7803ee47cadSRui Miguel Silva 
78163f47529SHans de Goede static bool ov2680_valid_frame_size(struct v4l2_subdev *sd,
78263f47529SHans de Goede 				    struct v4l2_subdev_state *sd_state,
78363f47529SHans de Goede 				    struct v4l2_subdev_frame_interval_enum *fie)
7844007015eSHans de Goede {
78563f47529SHans de Goede 	struct v4l2_subdev_frame_size_enum fse = {
78663f47529SHans de Goede 		.pad = fie->pad,
78763f47529SHans de Goede 		.which = fie->which,
78863f47529SHans de Goede 	};
7894007015eSHans de Goede 	int i;
7904007015eSHans de Goede 
79163f47529SHans de Goede 	for (i = 0; i < OV2680_FRAME_SIZES; i++) {
79263f47529SHans de Goede 		fse.index = i;
79363f47529SHans de Goede 
79463f47529SHans de Goede 		if (ov2680_enum_frame_size(sd, sd_state, &fse))
79563f47529SHans de Goede 			return false;
79663f47529SHans de Goede 
79763f47529SHans de Goede 		if (fie->width == fse.min_width &&
79863f47529SHans de Goede 		    fie->height == fse.min_height)
7994007015eSHans de Goede 			return true;
8004007015eSHans de Goede 	}
8014007015eSHans de Goede 
8024007015eSHans de Goede 	return false;
8034007015eSHans de Goede }
8044007015eSHans de Goede 
8053ee47cadSRui Miguel Silva static int ov2680_enum_frame_interval(struct v4l2_subdev *sd,
8060d346d2aSTomi Valkeinen 			      struct v4l2_subdev_state *sd_state,
8073ee47cadSRui Miguel Silva 			      struct v4l2_subdev_frame_interval_enum *fie)
8083ee47cadSRui Miguel Silva {
8094007015eSHans de Goede 	struct ov2680_dev *sensor = to_ov2680_dev(sd);
8103ee47cadSRui Miguel Silva 
8114007015eSHans de Goede 	/* Only 1 framerate */
81263f47529SHans de Goede 	if (fie->index || !ov2680_valid_frame_size(sd, sd_state, fie))
8133ee47cadSRui Miguel Silva 		return -EINVAL;
8143ee47cadSRui Miguel Silva 
815f614dfb8SHans de Goede 	fie->interval = sensor->mode.frame_interval;
8163ee47cadSRui Miguel Silva 
8173ee47cadSRui Miguel Silva 	return 0;
8183ee47cadSRui Miguel Silva }
8193ee47cadSRui Miguel Silva 
8203ee47cadSRui Miguel Silva static int ov2680_s_ctrl(struct v4l2_ctrl *ctrl)
8213ee47cadSRui Miguel Silva {
8223ee47cadSRui Miguel Silva 	struct v4l2_subdev *sd = ctrl_to_sd(ctrl);
8233ee47cadSRui Miguel Silva 	struct ov2680_dev *sensor = to_ov2680_dev(sd);
82437f7e57eSHans de Goede 	int ret;
8253ee47cadSRui Miguel Silva 
82637f7e57eSHans de Goede 	/* Only apply changes to the controls if the device is powered up */
82737f7e57eSHans de Goede 	if (!pm_runtime_get_if_in_use(sensor->sd.dev)) {
828f614dfb8SHans de Goede 		ov2680_set_bayer_order(sensor, &sensor->mode.fmt);
8293ee47cadSRui Miguel Silva 		return 0;
83037f7e57eSHans de Goede 	}
8313ee47cadSRui Miguel Silva 
8323ee47cadSRui Miguel Silva 	switch (ctrl->id) {
83305d6bd86SHans de Goede 	case V4L2_CID_ANALOGUE_GAIN:
83437f7e57eSHans de Goede 		ret = ov2680_gain_set(sensor, ctrl->val);
83537f7e57eSHans de Goede 		break;
8363ee47cadSRui Miguel Silva 	case V4L2_CID_EXPOSURE:
83737f7e57eSHans de Goede 		ret = ov2680_exposure_set(sensor, ctrl->val);
83837f7e57eSHans de Goede 		break;
8393ee47cadSRui Miguel Silva 	case V4L2_CID_VFLIP:
84037f7e57eSHans de Goede 		ret = ov2680_set_vflip(sensor, ctrl->val);
84137f7e57eSHans de Goede 		break;
8423ee47cadSRui Miguel Silva 	case V4L2_CID_HFLIP:
84337f7e57eSHans de Goede 		ret = ov2680_set_hflip(sensor, ctrl->val);
84437f7e57eSHans de Goede 		break;
8453ee47cadSRui Miguel Silva 	case V4L2_CID_TEST_PATTERN:
84637f7e57eSHans de Goede 		ret = ov2680_test_pattern_set(sensor, ctrl->val);
84737f7e57eSHans de Goede 		break;
8483ee47cadSRui Miguel Silva 	default:
84937f7e57eSHans de Goede 		ret = -EINVAL;
8503ee47cadSRui Miguel Silva 		break;
8513ee47cadSRui Miguel Silva 	}
8523ee47cadSRui Miguel Silva 
85337f7e57eSHans de Goede 	pm_runtime_put(sensor->sd.dev);
85437f7e57eSHans de Goede 	return ret;
8553ee47cadSRui Miguel Silva }
8563ee47cadSRui Miguel Silva 
8573ee47cadSRui Miguel Silva static const struct v4l2_ctrl_ops ov2680_ctrl_ops = {
8583ee47cadSRui Miguel Silva 	.s_ctrl = ov2680_s_ctrl,
8593ee47cadSRui Miguel Silva };
8603ee47cadSRui Miguel Silva 
8613ee47cadSRui Miguel Silva static const struct v4l2_subdev_video_ops ov2680_video_ops = {
8623ee47cadSRui Miguel Silva 	.g_frame_interval	= ov2680_s_g_frame_interval,
8633ee47cadSRui Miguel Silva 	.s_frame_interval	= ov2680_s_g_frame_interval,
8643ee47cadSRui Miguel Silva 	.s_stream		= ov2680_s_stream,
8653ee47cadSRui Miguel Silva };
8663ee47cadSRui Miguel Silva 
8673ee47cadSRui Miguel Silva static const struct v4l2_subdev_pad_ops ov2680_pad_ops = {
8683ee47cadSRui Miguel Silva 	.init_cfg		= ov2680_init_cfg,
8693ee47cadSRui Miguel Silva 	.enum_mbus_code		= ov2680_enum_mbus_code,
8703ee47cadSRui Miguel Silva 	.enum_frame_size	= ov2680_enum_frame_size,
8713ee47cadSRui Miguel Silva 	.enum_frame_interval	= ov2680_enum_frame_interval,
8723b378b35SHans de Goede 	.get_fmt		= ov2680_get_fmt,
8733b378b35SHans de Goede 	.set_fmt		= ov2680_set_fmt,
8743b378b35SHans de Goede 	.get_selection		= ov2680_get_selection,
8753b378b35SHans de Goede 	.set_selection		= ov2680_set_selection,
8763ee47cadSRui Miguel Silva };
8773ee47cadSRui Miguel Silva 
8783ee47cadSRui Miguel Silva static const struct v4l2_subdev_ops ov2680_subdev_ops = {
8793ee47cadSRui Miguel Silva 	.video	= &ov2680_video_ops,
8803ee47cadSRui Miguel Silva 	.pad	= &ov2680_pad_ops,
8813ee47cadSRui Miguel Silva };
8823ee47cadSRui Miguel Silva 
8833ee47cadSRui Miguel Silva static int ov2680_mode_init(struct ov2680_dev *sensor)
8843ee47cadSRui Miguel Silva {
8853ee47cadSRui Miguel Silva 	/* set initial mode */
8863b378b35SHans de Goede 	sensor->mode.crop = ov2680_default_crop;
887f614dfb8SHans de Goede 	ov2680_fill_format(sensor, &sensor->mode.fmt,
8886d6849b2SHans de Goede 			   OV2680_DEFAULT_WIDTH, OV2680_DEFAULT_HEIGHT);
88963f47529SHans de Goede 	ov2680_calc_mode(sensor);
8903ee47cadSRui Miguel Silva 
891f614dfb8SHans de Goede 	sensor->mode.frame_interval.denominator = OV2680_FRAME_RATE;
892f614dfb8SHans de Goede 	sensor->mode.frame_interval.numerator = 1;
8933ee47cadSRui Miguel Silva 
8943ee47cadSRui Miguel Silva 	return 0;
8953ee47cadSRui Miguel Silva }
8963ee47cadSRui Miguel Silva 
897e7f4861dSJavier Martinez Canillas static int ov2680_v4l2_register(struct ov2680_dev *sensor)
8983ee47cadSRui Miguel Silva {
8997adfdecbSHans de Goede 	struct i2c_client *client = to_i2c_client(sensor->dev);
9003ee47cadSRui Miguel Silva 	const struct v4l2_ctrl_ops *ops = &ov2680_ctrl_ops;
9013ee47cadSRui Miguel Silva 	struct ov2680_ctrls *ctrls = &sensor->ctrls;
9023ee47cadSRui Miguel Silva 	struct v4l2_ctrl_handler *hdl = &ctrls->handler;
90305d6bd86SHans de Goede 	int exp_max = OV2680_LINES_PER_FRAME - OV2680_INTEGRATION_TIME_MARGIN;
9043ee47cadSRui Miguel Silva 	int ret = 0;
9053ee47cadSRui Miguel Silva 
9067adfdecbSHans de Goede 	v4l2_i2c_subdev_init(&sensor->sd, client, &ov2680_subdev_ops);
9073ee47cadSRui Miguel Silva 
9083ee47cadSRui Miguel Silva 	sensor->sd.flags = V4L2_SUBDEV_FL_HAS_DEVNODE;
9093ee47cadSRui Miguel Silva 	sensor->pad.flags = MEDIA_PAD_FL_SOURCE;
9103ee47cadSRui Miguel Silva 	sensor->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
9113ee47cadSRui Miguel Silva 
9123ee47cadSRui Miguel Silva 	ret = media_entity_pads_init(&sensor->sd.entity, 1, &sensor->pad);
9133ee47cadSRui Miguel Silva 	if (ret < 0)
9143ee47cadSRui Miguel Silva 		return ret;
9153ee47cadSRui Miguel Silva 
9167b5a42e6SHans de Goede 	v4l2_ctrl_handler_init(hdl, 5);
9173ee47cadSRui Miguel Silva 
9183ee47cadSRui Miguel Silva 	hdl->lock = &sensor->lock;
9193ee47cadSRui Miguel Silva 
9203ee47cadSRui Miguel Silva 	ctrls->vflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_VFLIP, 0, 1, 1, 0);
9213ee47cadSRui Miguel Silva 	ctrls->hflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_HFLIP, 0, 1, 1, 0);
9223ee47cadSRui Miguel Silva 
9233ee47cadSRui Miguel Silva 	ctrls->test_pattern = v4l2_ctrl_new_std_menu_items(hdl,
9243ee47cadSRui Miguel Silva 					&ov2680_ctrl_ops, V4L2_CID_TEST_PATTERN,
9253ee47cadSRui Miguel Silva 					ARRAY_SIZE(test_pattern_menu) - 1,
9263ee47cadSRui Miguel Silva 					0, 0, test_pattern_menu);
9273ee47cadSRui Miguel Silva 
9283ee47cadSRui Miguel Silva 	ctrls->exposure = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_EXPOSURE,
92905d6bd86SHans de Goede 					    0, exp_max, 1, exp_max);
9303ee47cadSRui Miguel Silva 
93105d6bd86SHans de Goede 	ctrls->gain = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_ANALOGUE_GAIN,
93205d6bd86SHans de Goede 					0, 1023, 1, 250);
9333ee47cadSRui Miguel Silva 
9343ee47cadSRui Miguel Silva 	if (hdl->error) {
9353ee47cadSRui Miguel Silva 		ret = hdl->error;
9363ee47cadSRui Miguel Silva 		goto cleanup_entity;
9373ee47cadSRui Miguel Silva 	}
9383ee47cadSRui Miguel Silva 
93966274280SDave Stevenson 	ctrls->vflip->flags |= V4L2_CTRL_FLAG_MODIFY_LAYOUT;
94066274280SDave Stevenson 	ctrls->hflip->flags |= V4L2_CTRL_FLAG_MODIFY_LAYOUT;
9413ee47cadSRui Miguel Silva 
9423ee47cadSRui Miguel Silva 	sensor->sd.ctrl_handler = hdl;
9433ee47cadSRui Miguel Silva 
9443ee47cadSRui Miguel Silva 	ret = v4l2_async_register_subdev(&sensor->sd);
9453ee47cadSRui Miguel Silva 	if (ret < 0)
9463ee47cadSRui Miguel Silva 		goto cleanup_entity;
9473ee47cadSRui Miguel Silva 
9483ee47cadSRui Miguel Silva 	return 0;
9493ee47cadSRui Miguel Silva 
9503ee47cadSRui Miguel Silva cleanup_entity:
9513ee47cadSRui Miguel Silva 	media_entity_cleanup(&sensor->sd.entity);
9523ee47cadSRui Miguel Silva 	v4l2_ctrl_handler_free(hdl);
9533ee47cadSRui Miguel Silva 
9543ee47cadSRui Miguel Silva 	return ret;
9553ee47cadSRui Miguel Silva }
9563ee47cadSRui Miguel Silva 
9573ee47cadSRui Miguel Silva static int ov2680_get_regulators(struct ov2680_dev *sensor)
9583ee47cadSRui Miguel Silva {
9593ee47cadSRui Miguel Silva 	int i;
9603ee47cadSRui Miguel Silva 
9613ee47cadSRui Miguel Silva 	for (i = 0; i < OV2680_NUM_SUPPLIES; i++)
9623ee47cadSRui Miguel Silva 		sensor->supplies[i].supply = ov2680_supply_name[i];
9633ee47cadSRui Miguel Silva 
9647adfdecbSHans de Goede 	return devm_regulator_bulk_get(sensor->dev,
9657adfdecbSHans de Goede 				       OV2680_NUM_SUPPLIES, sensor->supplies);
9663ee47cadSRui Miguel Silva }
9673ee47cadSRui Miguel Silva 
9683ee47cadSRui Miguel Silva static int ov2680_check_id(struct ov2680_dev *sensor)
9693ee47cadSRui Miguel Silva {
970*a99a041cSHans de Goede 	u64 chip_id, rev;
971*a99a041cSHans de Goede 	int ret = 0;
9723ee47cadSRui Miguel Silva 
973*a99a041cSHans de Goede 	cci_read(sensor->regmap, OV2680_REG_CHIP_ID, &chip_id, &ret);
974*a99a041cSHans de Goede 	cci_read(sensor->regmap, OV2680_REG_SC_CMMN_SUB_ID, &rev, &ret);
9753ee47cadSRui Miguel Silva 	if (ret < 0) {
9767adfdecbSHans de Goede 		dev_err(sensor->dev, "failed to read chip id\n");
977*a99a041cSHans de Goede 		return ret;
9783ee47cadSRui Miguel Silva 	}
9793ee47cadSRui Miguel Silva 
9803ee47cadSRui Miguel Silva 	if (chip_id != OV2680_CHIP_ID) {
9817adfdecbSHans de Goede 		dev_err(sensor->dev, "chip id: 0x%04llx does not match expected 0x%04x\n",
9823ee47cadSRui Miguel Silva 			chip_id, OV2680_CHIP_ID);
9833ee47cadSRui Miguel Silva 		return -ENODEV;
9843ee47cadSRui Miguel Silva 	}
9853ee47cadSRui Miguel Silva 
986*a99a041cSHans de Goede 	dev_info(sensor->dev, "sensor_revision id = 0x%llx, rev= %lld\n",
987*a99a041cSHans de Goede 		 chip_id, rev & 0x0f);
988*a99a041cSHans de Goede 
9893ee47cadSRui Miguel Silva 	return 0;
9903ee47cadSRui Miguel Silva }
9913ee47cadSRui Miguel Silva 
992913c55a8SChristophe JAILLET static int ov2680_parse_dt(struct ov2680_dev *sensor)
9933ee47cadSRui Miguel Silva {
9947adfdecbSHans de Goede 	struct device *dev = sensor->dev;
99583634470SHans de Goede 	struct fwnode_handle *ep_fwnode;
996e9305a23SHans de Goede 	struct gpio_desc *gpio;
997ec7dfad5SHans de Goede 	unsigned int rate = 0;
9988e50a122SHans de Goede 	int i, ret;
9993ee47cadSRui Miguel Silva 
1000e9305a23SHans de Goede 	/*
100183634470SHans de Goede 	 * Sometimes the fwnode graph is initialized by the bridge driver.
100283634470SHans de Goede 	 * Bridge drivers doing this may also add GPIO mappings, wait for this.
100383634470SHans de Goede 	 */
100483634470SHans de Goede 	ep_fwnode = fwnode_graph_get_next_endpoint(dev_fwnode(dev), NULL);
100583634470SHans de Goede 	if (!ep_fwnode)
100683634470SHans de Goede 		return dev_err_probe(dev, -EPROBE_DEFER,
100783634470SHans de Goede 				     "waiting for fwnode graph endpoint\n");
100883634470SHans de Goede 
100983634470SHans de Goede 	fwnode_handle_put(ep_fwnode);
101083634470SHans de Goede 
101183634470SHans de Goede 	/*
1012e9305a23SHans de Goede 	 * The pin we want is named XSHUTDN in the datasheet. Linux sensor
1013e9305a23SHans de Goede 	 * drivers have standardized on using "powerdown" as con-id name
1014e9305a23SHans de Goede 	 * for powerdown or shutdown pins. Older DTB files use "reset",
1015e9305a23SHans de Goede 	 * so fallback to that if there is no "powerdown" pin.
1016e9305a23SHans de Goede 	 */
1017e9305a23SHans de Goede 	gpio = devm_gpiod_get_optional(dev, "powerdown", GPIOD_OUT_HIGH);
1018e9305a23SHans de Goede 	if (!gpio)
1019e9305a23SHans de Goede 		gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
1020e9305a23SHans de Goede 
1021e9305a23SHans de Goede 	ret = PTR_ERR_OR_ZERO(gpio);
10223ee47cadSRui Miguel Silva 	if (ret < 0) {
10233ee47cadSRui Miguel Silva 		dev_dbg(dev, "error while getting reset gpio: %d\n", ret);
10243ee47cadSRui Miguel Silva 		return ret;
10253ee47cadSRui Miguel Silva 	}
10263ee47cadSRui Miguel Silva 
1027e9305a23SHans de Goede 	sensor->pwdn_gpio = gpio;
1028e9305a23SHans de Goede 
1029ec7dfad5SHans de Goede 	sensor->xvclk = devm_clk_get_optional(dev, "xvclk");
10303ee47cadSRui Miguel Silva 	if (IS_ERR(sensor->xvclk)) {
10313ee47cadSRui Miguel Silva 		dev_err(dev, "xvclk clock missing or invalid\n");
10323ee47cadSRui Miguel Silva 		return PTR_ERR(sensor->xvclk);
10333ee47cadSRui Miguel Silva 	}
10343ee47cadSRui Miguel Silva 
1035ec7dfad5SHans de Goede 	/*
1036ec7dfad5SHans de Goede 	 * We could have either a 24MHz or 19.2MHz clock rate from either DT or
1037ec7dfad5SHans de Goede 	 * ACPI... but we also need to support the weird IPU3 case which will
1038ec7dfad5SHans de Goede 	 * have an external clock AND a clock-frequency property. Check for the
1039ec7dfad5SHans de Goede 	 * clock-frequency property and if found, set that rate if we managed
1040ec7dfad5SHans de Goede 	 * to acquire a clock. This should cover the ACPI case. If the system
1041ec7dfad5SHans de Goede 	 * uses devicetree then the configured rate should already be set, so
1042ec7dfad5SHans de Goede 	 * we can just read it.
1043ec7dfad5SHans de Goede 	 */
1044ec7dfad5SHans de Goede 	ret = fwnode_property_read_u32(dev_fwnode(dev), "clock-frequency",
1045ec7dfad5SHans de Goede 				       &rate);
1046ec7dfad5SHans de Goede 	if (ret && !sensor->xvclk)
1047ec7dfad5SHans de Goede 		return dev_err_probe(dev, ret, "invalid clock config\n");
1048ec7dfad5SHans de Goede 
1049ec7dfad5SHans de Goede 	if (!ret && sensor->xvclk) {
1050ec7dfad5SHans de Goede 		ret = clk_set_rate(sensor->xvclk, rate);
1051ec7dfad5SHans de Goede 		if (ret)
1052ec7dfad5SHans de Goede 			return dev_err_probe(dev, ret,
1053ec7dfad5SHans de Goede 					     "failed to set clock rate\n");
1054ec7dfad5SHans de Goede 	}
1055ec7dfad5SHans de Goede 
1056ec7dfad5SHans de Goede 	sensor->xvclk_freq = rate ?: clk_get_rate(sensor->xvclk);
10578e50a122SHans de Goede 
10588e50a122SHans de Goede 	for (i = 0; i < ARRAY_SIZE(ov2680_xvclk_freqs); i++) {
10598e50a122SHans de Goede 		if (sensor->xvclk_freq == ov2680_xvclk_freqs[i])
10608e50a122SHans de Goede 			break;
10613ee47cadSRui Miguel Silva 	}
10623ee47cadSRui Miguel Silva 
10638e50a122SHans de Goede 	if (i == ARRAY_SIZE(ov2680_xvclk_freqs))
10648e50a122SHans de Goede 		return dev_err_probe(dev, -EINVAL,
10658e50a122SHans de Goede 				     "unsupported xvclk frequency %d Hz\n",
10668e50a122SHans de Goede 				     sensor->xvclk_freq);
10678e50a122SHans de Goede 
10688e50a122SHans de Goede 	sensor->pll_mult = ov2680_pll_multipliers[i];
10698e50a122SHans de Goede 
10703ee47cadSRui Miguel Silva 	return 0;
10713ee47cadSRui Miguel Silva }
10723ee47cadSRui Miguel Silva 
10733ee47cadSRui Miguel Silva static int ov2680_probe(struct i2c_client *client)
10743ee47cadSRui Miguel Silva {
10753ee47cadSRui Miguel Silva 	struct device *dev = &client->dev;
10763ee47cadSRui Miguel Silva 	struct ov2680_dev *sensor;
10773ee47cadSRui Miguel Silva 	int ret;
10783ee47cadSRui Miguel Silva 
10793ee47cadSRui Miguel Silva 	sensor = devm_kzalloc(dev, sizeof(*sensor), GFP_KERNEL);
10803ee47cadSRui Miguel Silva 	if (!sensor)
10813ee47cadSRui Miguel Silva 		return -ENOMEM;
10823ee47cadSRui Miguel Silva 
10837adfdecbSHans de Goede 	sensor->dev = &client->dev;
10843ee47cadSRui Miguel Silva 
10859289998eSHans de Goede 	sensor->regmap = devm_cci_regmap_init_i2c(client, 16);
10869289998eSHans de Goede 	if (IS_ERR(sensor->regmap))
10879289998eSHans de Goede 		return PTR_ERR(sensor->regmap);
10889289998eSHans de Goede 
1089913c55a8SChristophe JAILLET 	ret = ov2680_parse_dt(sensor);
10903ee47cadSRui Miguel Silva 	if (ret < 0)
109183634470SHans de Goede 		return ret;
10923ee47cadSRui Miguel Silva 
10933ee47cadSRui Miguel Silva 	ret = ov2680_mode_init(sensor);
10943ee47cadSRui Miguel Silva 	if (ret < 0)
10953ee47cadSRui Miguel Silva 		return ret;
10963ee47cadSRui Miguel Silva 
10973ee47cadSRui Miguel Silva 	ret = ov2680_get_regulators(sensor);
10983ee47cadSRui Miguel Silva 	if (ret < 0) {
10993ee47cadSRui Miguel Silva 		dev_err(dev, "failed to get regulators\n");
11003ee47cadSRui Miguel Silva 		return ret;
11013ee47cadSRui Miguel Silva 	}
11023ee47cadSRui Miguel Silva 
11033ee47cadSRui Miguel Silva 	mutex_init(&sensor->lock);
11043ee47cadSRui Miguel Silva 
1105990732a6SHans de Goede 	/*
1106990732a6SHans de Goede 	 * Power up and verify the chip now, so that if runtime pm is
1107990732a6SHans de Goede 	 * disabled the chip is left on and streaming will work.
1108990732a6SHans de Goede 	 */
1109990732a6SHans de Goede 	ret = ov2680_power_on(sensor);
11103ee47cadSRui Miguel Silva 	if (ret < 0)
11113ee47cadSRui Miguel Silva 		goto lock_destroy;
11123ee47cadSRui Miguel Silva 
1113990732a6SHans de Goede 	ret = ov2680_check_id(sensor);
1114990732a6SHans de Goede 	if (ret < 0)
1115990732a6SHans de Goede 		goto err_powerdown;
1116990732a6SHans de Goede 
1117990732a6SHans de Goede 	pm_runtime_set_active(&client->dev);
1118990732a6SHans de Goede 	pm_runtime_get_noresume(&client->dev);
1119990732a6SHans de Goede 	pm_runtime_enable(&client->dev);
1120990732a6SHans de Goede 
1121e7f4861dSJavier Martinez Canillas 	ret = ov2680_v4l2_register(sensor);
11223ee47cadSRui Miguel Silva 	if (ret < 0)
1123990732a6SHans de Goede 		goto err_pm_runtime;
1124990732a6SHans de Goede 
1125990732a6SHans de Goede 	pm_runtime_set_autosuspend_delay(&client->dev, 1000);
1126990732a6SHans de Goede 	pm_runtime_use_autosuspend(&client->dev);
1127990732a6SHans de Goede 	pm_runtime_put_autosuspend(&client->dev);
11283ee47cadSRui Miguel Silva 
11293ee47cadSRui Miguel Silva 	return 0;
11303ee47cadSRui Miguel Silva 
1131990732a6SHans de Goede err_pm_runtime:
1132990732a6SHans de Goede 	pm_runtime_disable(&client->dev);
1133990732a6SHans de Goede 	pm_runtime_put_noidle(&client->dev);
1134990732a6SHans de Goede err_powerdown:
1135990732a6SHans de Goede 	ov2680_power_off(sensor);
11363ee47cadSRui Miguel Silva lock_destroy:
1137b7a41762SJavier Martinez Canillas 	dev_err(dev, "ov2680 init fail: %d\n", ret);
11383ee47cadSRui Miguel Silva 	mutex_destroy(&sensor->lock);
11393ee47cadSRui Miguel Silva 
11403ee47cadSRui Miguel Silva 	return ret;
11413ee47cadSRui Miguel Silva }
11423ee47cadSRui Miguel Silva 
1143ed5c2f5fSUwe Kleine-König static void ov2680_remove(struct i2c_client *client)
11443ee47cadSRui Miguel Silva {
11453ee47cadSRui Miguel Silva 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
11463ee47cadSRui Miguel Silva 	struct ov2680_dev *sensor = to_ov2680_dev(sd);
11473ee47cadSRui Miguel Silva 
11483ee47cadSRui Miguel Silva 	v4l2_async_unregister_subdev(&sensor->sd);
11493ee47cadSRui Miguel Silva 	mutex_destroy(&sensor->lock);
11503ee47cadSRui Miguel Silva 	media_entity_cleanup(&sensor->sd.entity);
11513ee47cadSRui Miguel Silva 	v4l2_ctrl_handler_free(&sensor->ctrls.handler);
1152990732a6SHans de Goede 
1153990732a6SHans de Goede 	/*
1154990732a6SHans de Goede 	 * Disable runtime PM. In case runtime PM is disabled in the kernel,
1155990732a6SHans de Goede 	 * make sure to turn power off manually.
1156990732a6SHans de Goede 	 */
1157990732a6SHans de Goede 	pm_runtime_disable(&client->dev);
1158990732a6SHans de Goede 	if (!pm_runtime_status_suspended(&client->dev))
1159990732a6SHans de Goede 		ov2680_power_off(sensor);
1160990732a6SHans de Goede 	pm_runtime_set_suspended(&client->dev);
11613ee47cadSRui Miguel Silva }
11623ee47cadSRui Miguel Silva 
1163990732a6SHans de Goede static int ov2680_suspend(struct device *dev)
11643ee47cadSRui Miguel Silva {
11657519296dSKrzysztof Kozlowski 	struct v4l2_subdev *sd = dev_get_drvdata(dev);
11663ee47cadSRui Miguel Silva 	struct ov2680_dev *sensor = to_ov2680_dev(sd);
11673ee47cadSRui Miguel Silva 
11683ee47cadSRui Miguel Silva 	if (sensor->is_streaming)
11693ee47cadSRui Miguel Silva 		ov2680_stream_disable(sensor);
11703ee47cadSRui Miguel Silva 
1171990732a6SHans de Goede 	return ov2680_power_off(sensor);
11723ee47cadSRui Miguel Silva }
11733ee47cadSRui Miguel Silva 
1174990732a6SHans de Goede static int ov2680_resume(struct device *dev)
11753ee47cadSRui Miguel Silva {
11767519296dSKrzysztof Kozlowski 	struct v4l2_subdev *sd = dev_get_drvdata(dev);
11773ee47cadSRui Miguel Silva 	struct ov2680_dev *sensor = to_ov2680_dev(sd);
11783ee47cadSRui Miguel Silva 	int ret;
11793ee47cadSRui Miguel Silva 
1180990732a6SHans de Goede 	ret = ov2680_power_on(sensor);
1181990732a6SHans de Goede 	if (ret < 0)
1182990732a6SHans de Goede 		goto stream_disable;
1183990732a6SHans de Goede 
11843ee47cadSRui Miguel Silva 	if (sensor->is_streaming) {
11853ee47cadSRui Miguel Silva 		ret = ov2680_stream_enable(sensor);
11863ee47cadSRui Miguel Silva 		if (ret < 0)
11873ee47cadSRui Miguel Silva 			goto stream_disable;
11883ee47cadSRui Miguel Silva 	}
11893ee47cadSRui Miguel Silva 
11903ee47cadSRui Miguel Silva 	return 0;
11913ee47cadSRui Miguel Silva 
11923ee47cadSRui Miguel Silva stream_disable:
11933ee47cadSRui Miguel Silva 	ov2680_stream_disable(sensor);
11943ee47cadSRui Miguel Silva 	sensor->is_streaming = false;
11953ee47cadSRui Miguel Silva 
11963ee47cadSRui Miguel Silva 	return ret;
11973ee47cadSRui Miguel Silva }
11983ee47cadSRui Miguel Silva 
1199990732a6SHans de Goede static DEFINE_RUNTIME_DEV_PM_OPS(ov2680_pm_ops, ov2680_suspend, ov2680_resume,
1200990732a6SHans de Goede 				 NULL);
12013ee47cadSRui Miguel Silva 
12023ee47cadSRui Miguel Silva static const struct of_device_id ov2680_dt_ids[] = {
12033ee47cadSRui Miguel Silva 	{ .compatible = "ovti,ov2680" },
12043ee47cadSRui Miguel Silva 	{ /* sentinel */ },
12053ee47cadSRui Miguel Silva };
12063ee47cadSRui Miguel Silva MODULE_DEVICE_TABLE(of, ov2680_dt_ids);
12073ee47cadSRui Miguel Silva 
1208df3ecab8SHans de Goede static const struct acpi_device_id ov2680_acpi_ids[] = {
1209df3ecab8SHans de Goede 	{ "OVTI2680" },
1210df3ecab8SHans de Goede 	{ /* sentinel */ }
1211df3ecab8SHans de Goede };
1212df3ecab8SHans de Goede MODULE_DEVICE_TABLE(acpi, ov2680_acpi_ids);
1213df3ecab8SHans de Goede 
12143ee47cadSRui Miguel Silva static struct i2c_driver ov2680_i2c_driver = {
12153ee47cadSRui Miguel Silva 	.driver = {
12163ee47cadSRui Miguel Silva 		.name  = "ov2680",
1217990732a6SHans de Goede 		.pm = pm_sleep_ptr(&ov2680_pm_ops),
121814155c4fSKrzysztof Kozlowski 		.of_match_table	= ov2680_dt_ids,
1219df3ecab8SHans de Goede 		.acpi_match_table = ov2680_acpi_ids,
12203ee47cadSRui Miguel Silva 	},
1221aaeb31c0SUwe Kleine-König 	.probe		= ov2680_probe,
12223ee47cadSRui Miguel Silva 	.remove		= ov2680_remove,
12233ee47cadSRui Miguel Silva };
12243ee47cadSRui Miguel Silva module_i2c_driver(ov2680_i2c_driver);
12253ee47cadSRui Miguel Silva 
12263ee47cadSRui Miguel Silva MODULE_AUTHOR("Rui Miguel Silva <rui.silva@linaro.org>");
12273ee47cadSRui Miguel Silva MODULE_DESCRIPTION("OV2680 CMOS Image Sensor driver");
12283ee47cadSRui Miguel Silva MODULE_LICENSE("GPL v2");
1229