xref: /openbmc/linux/drivers/media/i2c/ov2680.c (revision 7b5a42e6ae71927359ea67a2c22570ba97fa4059)
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 <asm/unaligned.h>
143ee47cadSRui Miguel Silva #include <linux/clk.h>
153ee47cadSRui Miguel Silva #include <linux/delay.h>
163ee47cadSRui Miguel Silva #include <linux/err.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>
213ee47cadSRui Miguel Silva #include <linux/gpio/consumer.h>
223ee47cadSRui Miguel Silva #include <linux/regulator/consumer.h>
233ee47cadSRui Miguel Silva 
243ee47cadSRui Miguel Silva #include <media/v4l2-common.h>
253ee47cadSRui Miguel Silva #include <media/v4l2-ctrls.h>
263ee47cadSRui Miguel Silva #include <media/v4l2-subdev.h>
273ee47cadSRui Miguel Silva 
283ee47cadSRui Miguel Silva #define OV2680_XVCLK_VALUE	24000000
293ee47cadSRui Miguel Silva 
303ee47cadSRui Miguel Silva #define OV2680_CHIP_ID		0x2680
313ee47cadSRui Miguel Silva 
323ee47cadSRui Miguel Silva #define OV2680_REG_STREAM_CTRL		0x0100
333ee47cadSRui Miguel Silva #define OV2680_REG_SOFT_RESET		0x0103
343ee47cadSRui Miguel Silva 
353ee47cadSRui Miguel Silva #define OV2680_REG_CHIP_ID_HIGH		0x300a
363ee47cadSRui Miguel Silva #define OV2680_REG_CHIP_ID_LOW		0x300b
373ee47cadSRui Miguel Silva 
383ee47cadSRui Miguel Silva #define OV2680_REG_R_MANUAL		0x3503
393ee47cadSRui Miguel Silva #define OV2680_REG_GAIN_PK		0x350a
403ee47cadSRui Miguel Silva #define OV2680_REG_EXPOSURE_PK_HIGH	0x3500
413ee47cadSRui Miguel Silva #define OV2680_REG_TIMING_HTS		0x380c
423ee47cadSRui Miguel Silva #define OV2680_REG_TIMING_VTS		0x380e
433ee47cadSRui Miguel Silva #define OV2680_REG_FORMAT1		0x3820
443ee47cadSRui Miguel Silva #define OV2680_REG_FORMAT2		0x3821
453ee47cadSRui Miguel Silva 
463ee47cadSRui Miguel Silva #define OV2680_REG_ISP_CTRL00		0x5080
473ee47cadSRui Miguel Silva 
483ee47cadSRui Miguel Silva #define OV2680_FRAME_RATE		30
493ee47cadSRui Miguel Silva 
503ee47cadSRui Miguel Silva #define OV2680_REG_VALUE_8BIT		1
513ee47cadSRui Miguel Silva #define OV2680_REG_VALUE_16BIT		2
523ee47cadSRui Miguel Silva #define OV2680_REG_VALUE_24BIT		3
533ee47cadSRui Miguel Silva 
543ee47cadSRui Miguel Silva #define OV2680_WIDTH_MAX		1600
553ee47cadSRui Miguel Silva #define OV2680_HEIGHT_MAX		1200
563ee47cadSRui Miguel Silva 
573ee47cadSRui Miguel Silva enum ov2680_mode_id {
583ee47cadSRui Miguel Silva 	OV2680_MODE_QUXGA_800_600,
593ee47cadSRui Miguel Silva 	OV2680_MODE_720P_1280_720,
603ee47cadSRui Miguel Silva 	OV2680_MODE_UXGA_1600_1200,
613ee47cadSRui Miguel Silva 	OV2680_MODE_MAX,
623ee47cadSRui Miguel Silva };
633ee47cadSRui Miguel Silva 
643ee47cadSRui Miguel Silva struct reg_value {
653ee47cadSRui Miguel Silva 	u16 reg_addr;
663ee47cadSRui Miguel Silva 	u8 val;
673ee47cadSRui Miguel Silva };
683ee47cadSRui Miguel Silva 
693ee47cadSRui Miguel Silva static const char * const ov2680_supply_name[] = {
703ee47cadSRui Miguel Silva 	"DOVDD",
713ee47cadSRui Miguel Silva 	"DVDD",
723ee47cadSRui Miguel Silva 	"AVDD",
733ee47cadSRui Miguel Silva };
743ee47cadSRui Miguel Silva 
753ee47cadSRui Miguel Silva #define OV2680_NUM_SUPPLIES ARRAY_SIZE(ov2680_supply_name)
763ee47cadSRui Miguel Silva 
773ee47cadSRui Miguel Silva struct ov2680_mode_info {
783ee47cadSRui Miguel Silva 	const char *name;
793ee47cadSRui Miguel Silva 	enum ov2680_mode_id id;
803ee47cadSRui Miguel Silva 	u32 width;
813ee47cadSRui Miguel Silva 	u32 height;
823ee47cadSRui Miguel Silva 	const struct reg_value *reg_data;
833ee47cadSRui Miguel Silva 	u32 reg_data_size;
843ee47cadSRui Miguel Silva };
853ee47cadSRui Miguel Silva 
863ee47cadSRui Miguel Silva struct ov2680_ctrls {
873ee47cadSRui Miguel Silva 	struct v4l2_ctrl_handler handler;
883ee47cadSRui Miguel Silva 	struct v4l2_ctrl *exposure;
893ee47cadSRui Miguel Silva 	struct v4l2_ctrl *gain;
903ee47cadSRui Miguel Silva 	struct v4l2_ctrl *hflip;
913ee47cadSRui Miguel Silva 	struct v4l2_ctrl *vflip;
923ee47cadSRui Miguel Silva 	struct v4l2_ctrl *test_pattern;
933ee47cadSRui Miguel Silva };
943ee47cadSRui Miguel Silva 
953ee47cadSRui Miguel Silva struct ov2680_dev {
963ee47cadSRui Miguel Silva 	struct i2c_client		*i2c_client;
973ee47cadSRui Miguel Silva 	struct v4l2_subdev		sd;
983ee47cadSRui Miguel Silva 
993ee47cadSRui Miguel Silva 	struct media_pad		pad;
1003ee47cadSRui Miguel Silva 	struct clk			*xvclk;
1013ee47cadSRui Miguel Silva 	u32				xvclk_freq;
1023ee47cadSRui Miguel Silva 	struct regulator_bulk_data	supplies[OV2680_NUM_SUPPLIES];
1033ee47cadSRui Miguel Silva 
1043ee47cadSRui Miguel Silva 	struct gpio_desc		*reset_gpio;
1053ee47cadSRui Miguel Silva 	struct mutex			lock; /* protect members */
1063ee47cadSRui Miguel Silva 
1073ee47cadSRui Miguel Silva 	bool				mode_pending_changes;
1083ee47cadSRui Miguel Silva 	bool				is_enabled;
1093ee47cadSRui Miguel Silva 	bool				is_streaming;
1103ee47cadSRui Miguel Silva 
1113ee47cadSRui Miguel Silva 	struct ov2680_ctrls		ctrls;
1123ee47cadSRui Miguel Silva 	struct v4l2_mbus_framefmt	fmt;
1133ee47cadSRui Miguel Silva 	struct v4l2_fract		frame_interval;
1143ee47cadSRui Miguel Silva 
1153ee47cadSRui Miguel Silva 	const struct ov2680_mode_info	*current_mode;
1163ee47cadSRui Miguel Silva };
1173ee47cadSRui Miguel Silva 
1183ee47cadSRui Miguel Silva static const char * const test_pattern_menu[] = {
1193ee47cadSRui Miguel Silva 	"Disabled",
1203ee47cadSRui Miguel Silva 	"Color Bars",
1213ee47cadSRui Miguel Silva 	"Random Data",
1223ee47cadSRui Miguel Silva 	"Square",
1233ee47cadSRui Miguel Silva 	"Black Image",
1243ee47cadSRui Miguel Silva };
1253ee47cadSRui Miguel Silva 
1263ee47cadSRui Miguel Silva static const int ov2680_hv_flip_bayer_order[] = {
1273ee47cadSRui Miguel Silva 	MEDIA_BUS_FMT_SBGGR10_1X10,
1283ee47cadSRui Miguel Silva 	MEDIA_BUS_FMT_SGRBG10_1X10,
1293ee47cadSRui Miguel Silva 	MEDIA_BUS_FMT_SGBRG10_1X10,
1303ee47cadSRui Miguel Silva 	MEDIA_BUS_FMT_SRGGB10_1X10,
1313ee47cadSRui Miguel Silva };
1323ee47cadSRui Miguel Silva 
1333ee47cadSRui Miguel Silva static const struct reg_value ov2680_setting_30fps_QUXGA_800_600[] = {
1343ee47cadSRui Miguel Silva 	{0x3086, 0x01}, {0x370a, 0x23}, {0x3808, 0x03}, {0x3809, 0x20},
1353ee47cadSRui Miguel Silva 	{0x380a, 0x02}, {0x380b, 0x58}, {0x380c, 0x06}, {0x380d, 0xac},
1363ee47cadSRui Miguel Silva 	{0x380e, 0x02}, {0x380f, 0x84}, {0x3811, 0x04}, {0x3813, 0x04},
1373ee47cadSRui Miguel Silva 	{0x3814, 0x31}, {0x3815, 0x31}, {0x3820, 0xc0}, {0x4008, 0x00},
1383ee47cadSRui Miguel Silva 	{0x4009, 0x03}, {0x4837, 0x1e}, {0x3501, 0x4e}, {0x3502, 0xe0},
139*7b5a42e6SHans de Goede 	{0x3503, 0x03},
1403ee47cadSRui Miguel Silva };
1413ee47cadSRui Miguel Silva 
1423ee47cadSRui Miguel Silva static const struct reg_value ov2680_setting_30fps_720P_1280_720[] = {
1433ee47cadSRui Miguel Silva 	{0x3086, 0x00}, {0x3808, 0x05}, {0x3809, 0x00}, {0x380a, 0x02},
1443ee47cadSRui Miguel Silva 	{0x380b, 0xd0}, {0x380c, 0x06}, {0x380d, 0xa8}, {0x380e, 0x05},
1453ee47cadSRui Miguel Silva 	{0x380f, 0x0e}, {0x3811, 0x08}, {0x3813, 0x06}, {0x3814, 0x11},
1463ee47cadSRui Miguel Silva 	{0x3815, 0x11}, {0x3820, 0xc0}, {0x4008, 0x00},
1473ee47cadSRui Miguel Silva };
1483ee47cadSRui Miguel Silva 
1493ee47cadSRui Miguel Silva static const struct reg_value ov2680_setting_30fps_UXGA_1600_1200[] = {
1503ee47cadSRui Miguel Silva 	{0x3086, 0x00}, {0x3501, 0x4e}, {0x3502, 0xe0}, {0x3808, 0x06},
1513ee47cadSRui Miguel Silva 	{0x3809, 0x40}, {0x380a, 0x04}, {0x380b, 0xb0}, {0x380c, 0x06},
1523ee47cadSRui Miguel Silva 	{0x380d, 0xa8}, {0x380e, 0x05}, {0x380f, 0x0e}, {0x3811, 0x00},
1533ee47cadSRui Miguel Silva 	{0x3813, 0x00}, {0x3814, 0x11}, {0x3815, 0x11}, {0x3820, 0xc0},
1543ee47cadSRui Miguel Silva 	{0x4008, 0x00}, {0x4837, 0x18}
1553ee47cadSRui Miguel Silva };
1563ee47cadSRui Miguel Silva 
1573ee47cadSRui Miguel Silva static const struct ov2680_mode_info ov2680_mode_init_data = {
1583ee47cadSRui Miguel Silva 	"mode_quxga_800_600", OV2680_MODE_QUXGA_800_600, 800, 600,
1593ee47cadSRui Miguel Silva 	ov2680_setting_30fps_QUXGA_800_600,
1603ee47cadSRui Miguel Silva 	ARRAY_SIZE(ov2680_setting_30fps_QUXGA_800_600),
1613ee47cadSRui Miguel Silva };
1623ee47cadSRui Miguel Silva 
1633ee47cadSRui Miguel Silva static const struct ov2680_mode_info ov2680_mode_data[OV2680_MODE_MAX] = {
1643ee47cadSRui Miguel Silva 	{"mode_quxga_800_600", OV2680_MODE_QUXGA_800_600,
1653ee47cadSRui Miguel Silva 	 800, 600, ov2680_setting_30fps_QUXGA_800_600,
1663ee47cadSRui Miguel Silva 	 ARRAY_SIZE(ov2680_setting_30fps_QUXGA_800_600)},
1673ee47cadSRui Miguel Silva 	{"mode_720p_1280_720", OV2680_MODE_720P_1280_720,
1683ee47cadSRui Miguel Silva 	 1280, 720, ov2680_setting_30fps_720P_1280_720,
1693ee47cadSRui Miguel Silva 	 ARRAY_SIZE(ov2680_setting_30fps_720P_1280_720)},
1703ee47cadSRui Miguel Silva 	{"mode_uxga_1600_1200", OV2680_MODE_UXGA_1600_1200,
1713ee47cadSRui Miguel Silva 	 1600, 1200, ov2680_setting_30fps_UXGA_1600_1200,
1723ee47cadSRui Miguel Silva 	 ARRAY_SIZE(ov2680_setting_30fps_UXGA_1600_1200)},
1733ee47cadSRui Miguel Silva };
1743ee47cadSRui Miguel Silva 
1753ee47cadSRui Miguel Silva static struct ov2680_dev *to_ov2680_dev(struct v4l2_subdev *sd)
1763ee47cadSRui Miguel Silva {
1773ee47cadSRui Miguel Silva 	return container_of(sd, struct ov2680_dev, sd);
1783ee47cadSRui Miguel Silva }
1793ee47cadSRui Miguel Silva 
1803ee47cadSRui Miguel Silva static struct device *ov2680_to_dev(struct ov2680_dev *sensor)
1813ee47cadSRui Miguel Silva {
1823ee47cadSRui Miguel Silva 	return &sensor->i2c_client->dev;
1833ee47cadSRui Miguel Silva }
1843ee47cadSRui Miguel Silva 
1853ee47cadSRui Miguel Silva static inline struct v4l2_subdev *ctrl_to_sd(struct v4l2_ctrl *ctrl)
1863ee47cadSRui Miguel Silva {
1873ee47cadSRui Miguel Silva 	return &container_of(ctrl->handler, struct ov2680_dev,
1883ee47cadSRui Miguel Silva 			     ctrls.handler)->sd;
1893ee47cadSRui Miguel Silva }
1903ee47cadSRui Miguel Silva 
1913ee47cadSRui Miguel Silva static int __ov2680_write_reg(struct ov2680_dev *sensor, u16 reg,
1923ee47cadSRui Miguel Silva 			      unsigned int len, u32 val)
1933ee47cadSRui Miguel Silva {
1943ee47cadSRui Miguel Silva 	struct i2c_client *client = sensor->i2c_client;
1953ee47cadSRui Miguel Silva 	u8 buf[6];
1963ee47cadSRui Miguel Silva 	int ret;
1973ee47cadSRui Miguel Silva 
1983ee47cadSRui Miguel Silva 	if (len > 4)
1993ee47cadSRui Miguel Silva 		return -EINVAL;
2003ee47cadSRui Miguel Silva 
2013ee47cadSRui Miguel Silva 	put_unaligned_be16(reg, buf);
2023ee47cadSRui Miguel Silva 	put_unaligned_be32(val << (8 * (4 - len)), buf + 2);
2033ee47cadSRui Miguel Silva 	ret = i2c_master_send(client, buf, len + 2);
2043ee47cadSRui Miguel Silva 	if (ret != len + 2) {
2053ee47cadSRui Miguel Silva 		dev_err(&client->dev, "write error: reg=0x%4x: %d\n", reg, ret);
2063ee47cadSRui Miguel Silva 		return -EIO;
2073ee47cadSRui Miguel Silva 	}
2083ee47cadSRui Miguel Silva 
2093ee47cadSRui Miguel Silva 	return 0;
2103ee47cadSRui Miguel Silva }
2113ee47cadSRui Miguel Silva 
2123ee47cadSRui Miguel Silva #define ov2680_write_reg(s, r, v) \
2133ee47cadSRui Miguel Silva 	__ov2680_write_reg(s, r, OV2680_REG_VALUE_8BIT, v)
2143ee47cadSRui Miguel Silva 
2153ee47cadSRui Miguel Silva #define ov2680_write_reg16(s, r, v) \
2163ee47cadSRui Miguel Silva 	__ov2680_write_reg(s, r, OV2680_REG_VALUE_16BIT, v)
2173ee47cadSRui Miguel Silva 
2183ee47cadSRui Miguel Silva #define ov2680_write_reg24(s, r, v) \
2193ee47cadSRui Miguel Silva 	__ov2680_write_reg(s, r, OV2680_REG_VALUE_24BIT, v)
2203ee47cadSRui Miguel Silva 
2213ee47cadSRui Miguel Silva static int __ov2680_read_reg(struct ov2680_dev *sensor, u16 reg,
2223ee47cadSRui Miguel Silva 			     unsigned int len, u32 *val)
2233ee47cadSRui Miguel Silva {
2243ee47cadSRui Miguel Silva 	struct i2c_client *client = sensor->i2c_client;
2253ee47cadSRui Miguel Silva 	struct i2c_msg msgs[2];
2263ee47cadSRui Miguel Silva 	u8 addr_buf[2] = { reg >> 8, reg & 0xff };
2273ee47cadSRui Miguel Silva 	u8 data_buf[4] = { 0, };
2283ee47cadSRui Miguel Silva 	int ret;
2293ee47cadSRui Miguel Silva 
2303ee47cadSRui Miguel Silva 	if (len > 4)
2313ee47cadSRui Miguel Silva 		return -EINVAL;
2323ee47cadSRui Miguel Silva 
2333ee47cadSRui Miguel Silva 	msgs[0].addr = client->addr;
2343ee47cadSRui Miguel Silva 	msgs[0].flags = 0;
2353ee47cadSRui Miguel Silva 	msgs[0].len = ARRAY_SIZE(addr_buf);
2363ee47cadSRui Miguel Silva 	msgs[0].buf = addr_buf;
2373ee47cadSRui Miguel Silva 
2383ee47cadSRui Miguel Silva 	msgs[1].addr = client->addr;
2393ee47cadSRui Miguel Silva 	msgs[1].flags = I2C_M_RD;
2403ee47cadSRui Miguel Silva 	msgs[1].len = len;
2413ee47cadSRui Miguel Silva 	msgs[1].buf = &data_buf[4 - len];
2423ee47cadSRui Miguel Silva 
2433ee47cadSRui Miguel Silva 	ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
2443ee47cadSRui Miguel Silva 	if (ret != ARRAY_SIZE(msgs)) {
2453ee47cadSRui Miguel Silva 		dev_err(&client->dev, "read error: reg=0x%4x: %d\n", reg, ret);
2463ee47cadSRui Miguel Silva 		return -EIO;
2473ee47cadSRui Miguel Silva 	}
2483ee47cadSRui Miguel Silva 
2493ee47cadSRui Miguel Silva 	*val = get_unaligned_be32(data_buf);
2503ee47cadSRui Miguel Silva 
2513ee47cadSRui Miguel Silva 	return 0;
2523ee47cadSRui Miguel Silva }
2533ee47cadSRui Miguel Silva 
2543ee47cadSRui Miguel Silva #define ov2680_read_reg(s, r, v) \
2553ee47cadSRui Miguel Silva 	__ov2680_read_reg(s, r, OV2680_REG_VALUE_8BIT, v)
2563ee47cadSRui Miguel Silva 
2573ee47cadSRui Miguel Silva #define ov2680_read_reg16(s, r, v) \
2583ee47cadSRui Miguel Silva 	__ov2680_read_reg(s, r, OV2680_REG_VALUE_16BIT, v)
2593ee47cadSRui Miguel Silva 
2603ee47cadSRui Miguel Silva #define ov2680_read_reg24(s, r, v) \
2613ee47cadSRui Miguel Silva 	__ov2680_read_reg(s, r, OV2680_REG_VALUE_24BIT, v)
2623ee47cadSRui Miguel Silva 
2633ee47cadSRui Miguel Silva static int ov2680_mod_reg(struct ov2680_dev *sensor, u16 reg, u8 mask, u8 val)
2643ee47cadSRui Miguel Silva {
2653ee47cadSRui Miguel Silva 	u32 readval;
2663ee47cadSRui Miguel Silva 	int ret;
2673ee47cadSRui Miguel Silva 
2683ee47cadSRui Miguel Silva 	ret = ov2680_read_reg(sensor, reg, &readval);
2693ee47cadSRui Miguel Silva 	if (ret < 0)
2703ee47cadSRui Miguel Silva 		return ret;
2713ee47cadSRui Miguel Silva 
2723ee47cadSRui Miguel Silva 	readval &= ~mask;
2733ee47cadSRui Miguel Silva 	val &= mask;
2743ee47cadSRui Miguel Silva 	val |= readval;
2753ee47cadSRui Miguel Silva 
2763ee47cadSRui Miguel Silva 	return ov2680_write_reg(sensor, reg, val);
2773ee47cadSRui Miguel Silva }
2783ee47cadSRui Miguel Silva 
2793ee47cadSRui Miguel Silva static int ov2680_load_regs(struct ov2680_dev *sensor,
2803ee47cadSRui Miguel Silva 			    const struct ov2680_mode_info *mode)
2813ee47cadSRui Miguel Silva {
2823ee47cadSRui Miguel Silva 	const struct reg_value *regs = mode->reg_data;
2833ee47cadSRui Miguel Silva 	unsigned int i;
2843ee47cadSRui Miguel Silva 	int ret = 0;
2853ee47cadSRui Miguel Silva 	u16 reg_addr;
2863ee47cadSRui Miguel Silva 	u8 val;
2873ee47cadSRui Miguel Silva 
2883ee47cadSRui Miguel Silva 	for (i = 0; i < mode->reg_data_size; ++i, ++regs) {
2893ee47cadSRui Miguel Silva 		reg_addr = regs->reg_addr;
2903ee47cadSRui Miguel Silva 		val = regs->val;
2913ee47cadSRui Miguel Silva 
2923ee47cadSRui Miguel Silva 		ret = ov2680_write_reg(sensor, reg_addr, val);
2933ee47cadSRui Miguel Silva 		if (ret)
2943ee47cadSRui Miguel Silva 			break;
2953ee47cadSRui Miguel Silva 	}
2963ee47cadSRui Miguel Silva 
2973ee47cadSRui Miguel Silva 	return ret;
2983ee47cadSRui Miguel Silva }
2993ee47cadSRui Miguel Silva 
3003ee47cadSRui Miguel Silva static void ov2680_power_up(struct ov2680_dev *sensor)
3013ee47cadSRui Miguel Silva {
3023ee47cadSRui Miguel Silva 	if (!sensor->reset_gpio)
3033ee47cadSRui Miguel Silva 		return;
3043ee47cadSRui Miguel Silva 
3053ee47cadSRui Miguel Silva 	gpiod_set_value(sensor->reset_gpio, 0);
3063ee47cadSRui Miguel Silva 	usleep_range(5000, 10000);
3073ee47cadSRui Miguel Silva }
3083ee47cadSRui Miguel Silva 
3093ee47cadSRui Miguel Silva static void ov2680_power_down(struct ov2680_dev *sensor)
3103ee47cadSRui Miguel Silva {
3113ee47cadSRui Miguel Silva 	if (!sensor->reset_gpio)
3123ee47cadSRui Miguel Silva 		return;
3133ee47cadSRui Miguel Silva 
3143ee47cadSRui Miguel Silva 	gpiod_set_value(sensor->reset_gpio, 1);
3153ee47cadSRui Miguel Silva 	usleep_range(5000, 10000);
3163ee47cadSRui Miguel Silva }
3173ee47cadSRui Miguel Silva 
3183ee47cadSRui Miguel Silva static int ov2680_bayer_order(struct ov2680_dev *sensor)
3193ee47cadSRui Miguel Silva {
3203ee47cadSRui Miguel Silva 	u32 format1;
3213ee47cadSRui Miguel Silva 	u32 format2;
3223ee47cadSRui Miguel Silva 	u32 hv_flip;
3233ee47cadSRui Miguel Silva 	int ret;
3243ee47cadSRui Miguel Silva 
3253ee47cadSRui Miguel Silva 	ret = ov2680_read_reg(sensor, OV2680_REG_FORMAT1, &format1);
3263ee47cadSRui Miguel Silva 	if (ret < 0)
3273ee47cadSRui Miguel Silva 		return ret;
3283ee47cadSRui Miguel Silva 
3293ee47cadSRui Miguel Silva 	ret = ov2680_read_reg(sensor, OV2680_REG_FORMAT2, &format2);
3303ee47cadSRui Miguel Silva 	if (ret < 0)
3313ee47cadSRui Miguel Silva 		return ret;
3323ee47cadSRui Miguel Silva 
3333ee47cadSRui Miguel Silva 	hv_flip = (format2 & BIT(2)  << 1) | (format1 & BIT(2));
3343ee47cadSRui Miguel Silva 
3353ee47cadSRui Miguel Silva 	sensor->fmt.code = ov2680_hv_flip_bayer_order[hv_flip];
3363ee47cadSRui Miguel Silva 
3373ee47cadSRui Miguel Silva 	return 0;
3383ee47cadSRui Miguel Silva }
3393ee47cadSRui Miguel Silva 
3403ee47cadSRui Miguel Silva static int ov2680_vflip_enable(struct ov2680_dev *sensor)
3413ee47cadSRui Miguel Silva {
3423ee47cadSRui Miguel Silva 	int ret;
3433ee47cadSRui Miguel Silva 
3443ee47cadSRui Miguel Silva 	ret = ov2680_mod_reg(sensor, OV2680_REG_FORMAT1, BIT(2), BIT(2));
3453ee47cadSRui Miguel Silva 	if (ret < 0)
3463ee47cadSRui Miguel Silva 		return ret;
3473ee47cadSRui Miguel Silva 
3483ee47cadSRui Miguel Silva 	return ov2680_bayer_order(sensor);
3493ee47cadSRui Miguel Silva }
3503ee47cadSRui Miguel Silva 
3513ee47cadSRui Miguel Silva static int ov2680_vflip_disable(struct ov2680_dev *sensor)
3523ee47cadSRui Miguel Silva {
3533ee47cadSRui Miguel Silva 	int ret;
3543ee47cadSRui Miguel Silva 
3553ee47cadSRui Miguel Silva 	ret = ov2680_mod_reg(sensor, OV2680_REG_FORMAT1, BIT(2), BIT(0));
3563ee47cadSRui Miguel Silva 	if (ret < 0)
3573ee47cadSRui Miguel Silva 		return ret;
3583ee47cadSRui Miguel Silva 
3593ee47cadSRui Miguel Silva 	return ov2680_bayer_order(sensor);
3603ee47cadSRui Miguel Silva }
3613ee47cadSRui Miguel Silva 
3623ee47cadSRui Miguel Silva static int ov2680_hflip_enable(struct ov2680_dev *sensor)
3633ee47cadSRui Miguel Silva {
3643ee47cadSRui Miguel Silva 	int ret;
3653ee47cadSRui Miguel Silva 
3663ee47cadSRui Miguel Silva 	ret = ov2680_mod_reg(sensor, OV2680_REG_FORMAT2, BIT(2), BIT(2));
3673ee47cadSRui Miguel Silva 	if (ret < 0)
3683ee47cadSRui Miguel Silva 		return ret;
3693ee47cadSRui Miguel Silva 
3703ee47cadSRui Miguel Silva 	return ov2680_bayer_order(sensor);
3713ee47cadSRui Miguel Silva }
3723ee47cadSRui Miguel Silva 
3733ee47cadSRui Miguel Silva static int ov2680_hflip_disable(struct ov2680_dev *sensor)
3743ee47cadSRui Miguel Silva {
3753ee47cadSRui Miguel Silva 	int ret;
3763ee47cadSRui Miguel Silva 
3773ee47cadSRui Miguel Silva 	ret = ov2680_mod_reg(sensor, OV2680_REG_FORMAT2, BIT(2), BIT(0));
3783ee47cadSRui Miguel Silva 	if (ret < 0)
3793ee47cadSRui Miguel Silva 		return ret;
3803ee47cadSRui Miguel Silva 
3813ee47cadSRui Miguel Silva 	return ov2680_bayer_order(sensor);
3823ee47cadSRui Miguel Silva }
3833ee47cadSRui Miguel Silva 
3843ee47cadSRui Miguel Silva static int ov2680_test_pattern_set(struct ov2680_dev *sensor, int value)
3853ee47cadSRui Miguel Silva {
3863ee47cadSRui Miguel Silva 	int ret;
3873ee47cadSRui Miguel Silva 
3883ee47cadSRui Miguel Silva 	if (!value)
3893ee47cadSRui Miguel Silva 		return ov2680_mod_reg(sensor, OV2680_REG_ISP_CTRL00, BIT(7), 0);
3903ee47cadSRui Miguel Silva 
3913ee47cadSRui Miguel Silva 	ret = ov2680_mod_reg(sensor, OV2680_REG_ISP_CTRL00, 0x03, value - 1);
3923ee47cadSRui Miguel Silva 	if (ret < 0)
3933ee47cadSRui Miguel Silva 		return ret;
3943ee47cadSRui Miguel Silva 
3953ee47cadSRui Miguel Silva 	ret = ov2680_mod_reg(sensor, OV2680_REG_ISP_CTRL00, BIT(7), BIT(7));
3963ee47cadSRui Miguel Silva 	if (ret < 0)
3973ee47cadSRui Miguel Silva 		return ret;
3983ee47cadSRui Miguel Silva 
3993ee47cadSRui Miguel Silva 	return 0;
4003ee47cadSRui Miguel Silva }
4013ee47cadSRui Miguel Silva 
402*7b5a42e6SHans de Goede static int ov2680_gain_set(struct ov2680_dev *sensor, u32 gain)
4033ee47cadSRui Miguel Silva {
404*7b5a42e6SHans de Goede 	return ov2680_write_reg16(sensor, OV2680_REG_GAIN_PK, gain);
4053ee47cadSRui Miguel Silva }
4063ee47cadSRui Miguel Silva 
407*7b5a42e6SHans de Goede static int ov2680_exposure_set(struct ov2680_dev *sensor, u32 exp)
4083ee47cadSRui Miguel Silva {
409*7b5a42e6SHans de Goede 	return ov2680_write_reg24(sensor, OV2680_REG_EXPOSURE_PK_HIGH,
410*7b5a42e6SHans de Goede 				  exp << 4);
4113ee47cadSRui Miguel Silva }
4123ee47cadSRui Miguel Silva 
4133ee47cadSRui Miguel Silva static int ov2680_stream_enable(struct ov2680_dev *sensor)
4143ee47cadSRui Miguel Silva {
4153ee47cadSRui Miguel Silva 	return ov2680_write_reg(sensor, OV2680_REG_STREAM_CTRL, 1);
4163ee47cadSRui Miguel Silva }
4173ee47cadSRui Miguel Silva 
4183ee47cadSRui Miguel Silva static int ov2680_stream_disable(struct ov2680_dev *sensor)
4193ee47cadSRui Miguel Silva {
4203ee47cadSRui Miguel Silva 	return ov2680_write_reg(sensor, OV2680_REG_STREAM_CTRL, 0);
4213ee47cadSRui Miguel Silva }
4223ee47cadSRui Miguel Silva 
4233ee47cadSRui Miguel Silva static int ov2680_mode_set(struct ov2680_dev *sensor)
4243ee47cadSRui Miguel Silva {
4253ee47cadSRui Miguel Silva 	int ret;
4263ee47cadSRui Miguel Silva 
4273ee47cadSRui Miguel Silva 	ret = ov2680_load_regs(sensor, sensor->current_mode);
4283ee47cadSRui Miguel Silva 	if (ret < 0)
4293ee47cadSRui Miguel Silva 		return ret;
4303ee47cadSRui Miguel Silva 
431*7b5a42e6SHans de Goede 	/* Restore value of all ctrls */
432*7b5a42e6SHans de Goede 	ret = __v4l2_ctrl_handler_setup(&sensor->ctrls.handler);
4333ee47cadSRui Miguel Silva 	if (ret < 0)
4343ee47cadSRui Miguel Silva 		return ret;
4353ee47cadSRui Miguel Silva 
4363ee47cadSRui Miguel Silva 	sensor->mode_pending_changes = false;
4373ee47cadSRui Miguel Silva 
4383ee47cadSRui Miguel Silva 	return 0;
4393ee47cadSRui Miguel Silva }
4403ee47cadSRui Miguel Silva 
4413ee47cadSRui Miguel Silva static int ov2680_mode_restore(struct ov2680_dev *sensor)
4423ee47cadSRui Miguel Silva {
4433ee47cadSRui Miguel Silva 	int ret;
4443ee47cadSRui Miguel Silva 
4453ee47cadSRui Miguel Silva 	ret = ov2680_load_regs(sensor, &ov2680_mode_init_data);
4463ee47cadSRui Miguel Silva 	if (ret < 0)
4473ee47cadSRui Miguel Silva 		return ret;
4483ee47cadSRui Miguel Silva 
4493ee47cadSRui Miguel Silva 	return ov2680_mode_set(sensor);
4503ee47cadSRui Miguel Silva }
4513ee47cadSRui Miguel Silva 
4523ee47cadSRui Miguel Silva static int ov2680_power_off(struct ov2680_dev *sensor)
4533ee47cadSRui Miguel Silva {
4543ee47cadSRui Miguel Silva 	if (!sensor->is_enabled)
4553ee47cadSRui Miguel Silva 		return 0;
4563ee47cadSRui Miguel Silva 
4573ee47cadSRui Miguel Silva 	clk_disable_unprepare(sensor->xvclk);
4583ee47cadSRui Miguel Silva 	ov2680_power_down(sensor);
4593ee47cadSRui Miguel Silva 	regulator_bulk_disable(OV2680_NUM_SUPPLIES, sensor->supplies);
4603ee47cadSRui Miguel Silva 	sensor->is_enabled = false;
4613ee47cadSRui Miguel Silva 
4623ee47cadSRui Miguel Silva 	return 0;
4633ee47cadSRui Miguel Silva }
4643ee47cadSRui Miguel Silva 
4653ee47cadSRui Miguel Silva static int ov2680_power_on(struct ov2680_dev *sensor)
4663ee47cadSRui Miguel Silva {
4673ee47cadSRui Miguel Silva 	struct device *dev = ov2680_to_dev(sensor);
4683ee47cadSRui Miguel Silva 	int ret;
4693ee47cadSRui Miguel Silva 
4703ee47cadSRui Miguel Silva 	if (sensor->is_enabled)
4713ee47cadSRui Miguel Silva 		return 0;
4723ee47cadSRui Miguel Silva 
4733ee47cadSRui Miguel Silva 	ret = regulator_bulk_enable(OV2680_NUM_SUPPLIES, sensor->supplies);
4743ee47cadSRui Miguel Silva 	if (ret < 0) {
4753ee47cadSRui Miguel Silva 		dev_err(dev, "failed to enable regulators: %d\n", ret);
4763ee47cadSRui Miguel Silva 		return ret;
4773ee47cadSRui Miguel Silva 	}
4783ee47cadSRui Miguel Silva 
4793ee47cadSRui Miguel Silva 	if (!sensor->reset_gpio) {
4803ee47cadSRui Miguel Silva 		ret = ov2680_write_reg(sensor, OV2680_REG_SOFT_RESET, 0x01);
4813ee47cadSRui Miguel Silva 		if (ret != 0) {
4823ee47cadSRui Miguel Silva 			dev_err(dev, "sensor soft reset failed\n");
4833ee47cadSRui Miguel Silva 			return ret;
4843ee47cadSRui Miguel Silva 		}
4853ee47cadSRui Miguel Silva 		usleep_range(1000, 2000);
4863ee47cadSRui Miguel Silva 	} else {
4873ee47cadSRui Miguel Silva 		ov2680_power_down(sensor);
4883ee47cadSRui Miguel Silva 		ov2680_power_up(sensor);
4893ee47cadSRui Miguel Silva 	}
4903ee47cadSRui Miguel Silva 
4913ee47cadSRui Miguel Silva 	ret = clk_prepare_enable(sensor->xvclk);
4923ee47cadSRui Miguel Silva 	if (ret < 0)
4933ee47cadSRui Miguel Silva 		return ret;
4943ee47cadSRui Miguel Silva 
4953ee47cadSRui Miguel Silva 	sensor->is_enabled = true;
4963ee47cadSRui Miguel Silva 
4973ee47cadSRui Miguel Silva 	/* Set clock lane into LP-11 state */
4983ee47cadSRui Miguel Silva 	ov2680_stream_enable(sensor);
4993ee47cadSRui Miguel Silva 	usleep_range(1000, 2000);
5003ee47cadSRui Miguel Silva 	ov2680_stream_disable(sensor);
5013ee47cadSRui Miguel Silva 
5023ee47cadSRui Miguel Silva 	return 0;
5033ee47cadSRui Miguel Silva }
5043ee47cadSRui Miguel Silva 
5053ee47cadSRui Miguel Silva static int ov2680_s_power(struct v4l2_subdev *sd, int on)
5063ee47cadSRui Miguel Silva {
5073ee47cadSRui Miguel Silva 	struct ov2680_dev *sensor = to_ov2680_dev(sd);
5083ee47cadSRui Miguel Silva 	int ret = 0;
5093ee47cadSRui Miguel Silva 
5103ee47cadSRui Miguel Silva 	mutex_lock(&sensor->lock);
5113ee47cadSRui Miguel Silva 
5123ee47cadSRui Miguel Silva 	if (on)
5133ee47cadSRui Miguel Silva 		ret = ov2680_power_on(sensor);
5143ee47cadSRui Miguel Silva 	else
5153ee47cadSRui Miguel Silva 		ret = ov2680_power_off(sensor);
5163ee47cadSRui Miguel Silva 
517*7b5a42e6SHans de Goede 	if (on && ret == 0)
518c45fbdf2SRui Miguel Silva 		ret = ov2680_mode_restore(sensor);
519*7b5a42e6SHans de Goede 
520*7b5a42e6SHans de Goede 	mutex_unlock(&sensor->lock);
5213ee47cadSRui Miguel Silva 
5223ee47cadSRui Miguel Silva 	return ret;
5233ee47cadSRui Miguel Silva }
5243ee47cadSRui Miguel Silva 
5253ee47cadSRui Miguel Silva static int ov2680_s_g_frame_interval(struct v4l2_subdev *sd,
5263ee47cadSRui Miguel Silva 				     struct v4l2_subdev_frame_interval *fi)
5273ee47cadSRui Miguel Silva {
5283ee47cadSRui Miguel Silva 	struct ov2680_dev *sensor = to_ov2680_dev(sd);
5293ee47cadSRui Miguel Silva 
5303ee47cadSRui Miguel Silva 	mutex_lock(&sensor->lock);
5313ee47cadSRui Miguel Silva 	fi->interval = sensor->frame_interval;
5323ee47cadSRui Miguel Silva 	mutex_unlock(&sensor->lock);
5333ee47cadSRui Miguel Silva 
5343ee47cadSRui Miguel Silva 	return 0;
5353ee47cadSRui Miguel Silva }
5363ee47cadSRui Miguel Silva 
5373ee47cadSRui Miguel Silva static int ov2680_s_stream(struct v4l2_subdev *sd, int enable)
5383ee47cadSRui Miguel Silva {
5393ee47cadSRui Miguel Silva 	struct ov2680_dev *sensor = to_ov2680_dev(sd);
5403ee47cadSRui Miguel Silva 	int ret = 0;
5413ee47cadSRui Miguel Silva 
5423ee47cadSRui Miguel Silva 	mutex_lock(&sensor->lock);
5433ee47cadSRui Miguel Silva 
5443ee47cadSRui Miguel Silva 	if (sensor->is_streaming == !!enable)
5453ee47cadSRui Miguel Silva 		goto unlock;
5463ee47cadSRui Miguel Silva 
5473ee47cadSRui Miguel Silva 	if (enable && sensor->mode_pending_changes) {
5483ee47cadSRui Miguel Silva 		ret = ov2680_mode_set(sensor);
5493ee47cadSRui Miguel Silva 		if (ret < 0)
5503ee47cadSRui Miguel Silva 			goto unlock;
5513ee47cadSRui Miguel Silva 	}
5523ee47cadSRui Miguel Silva 
5533ee47cadSRui Miguel Silva 	if (enable)
5543ee47cadSRui Miguel Silva 		ret = ov2680_stream_enable(sensor);
5553ee47cadSRui Miguel Silva 	else
5563ee47cadSRui Miguel Silva 		ret = ov2680_stream_disable(sensor);
5573ee47cadSRui Miguel Silva 
5583ee47cadSRui Miguel Silva 	sensor->is_streaming = !!enable;
5593ee47cadSRui Miguel Silva 
5603ee47cadSRui Miguel Silva unlock:
5613ee47cadSRui Miguel Silva 	mutex_unlock(&sensor->lock);
5623ee47cadSRui Miguel Silva 
5633ee47cadSRui Miguel Silva 	return ret;
5643ee47cadSRui Miguel Silva }
5653ee47cadSRui Miguel Silva 
5663ee47cadSRui Miguel Silva static int ov2680_enum_mbus_code(struct v4l2_subdev *sd,
5670d346d2aSTomi Valkeinen 				 struct v4l2_subdev_state *sd_state,
5683ee47cadSRui Miguel Silva 				 struct v4l2_subdev_mbus_code_enum *code)
5693ee47cadSRui Miguel Silva {
5703ee47cadSRui Miguel Silva 	struct ov2680_dev *sensor = to_ov2680_dev(sd);
5713ee47cadSRui Miguel Silva 
5723ee47cadSRui Miguel Silva 	if (code->pad != 0 || code->index != 0)
5733ee47cadSRui Miguel Silva 		return -EINVAL;
5743ee47cadSRui Miguel Silva 
5753ee47cadSRui Miguel Silva 	code->code = sensor->fmt.code;
5763ee47cadSRui Miguel Silva 
5773ee47cadSRui Miguel Silva 	return 0;
5783ee47cadSRui Miguel Silva }
5793ee47cadSRui Miguel Silva 
5803ee47cadSRui Miguel Silva static int ov2680_get_fmt(struct v4l2_subdev *sd,
5810d346d2aSTomi Valkeinen 			  struct v4l2_subdev_state *sd_state,
5823ee47cadSRui Miguel Silva 			  struct v4l2_subdev_format *format)
5833ee47cadSRui Miguel Silva {
5843ee47cadSRui Miguel Silva 	struct ov2680_dev *sensor = to_ov2680_dev(sd);
5853ee47cadSRui Miguel Silva 	struct v4l2_mbus_framefmt *fmt = NULL;
5863ee47cadSRui Miguel Silva 	int ret = 0;
5873ee47cadSRui Miguel Silva 
5883ee47cadSRui Miguel Silva 	if (format->pad != 0)
5893ee47cadSRui Miguel Silva 		return -EINVAL;
5903ee47cadSRui Miguel Silva 
5913ee47cadSRui Miguel Silva 	mutex_lock(&sensor->lock);
5923ee47cadSRui Miguel Silva 
5933ee47cadSRui Miguel Silva 	if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
5943ee47cadSRui Miguel Silva #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
5950d346d2aSTomi Valkeinen 		fmt = v4l2_subdev_get_try_format(&sensor->sd, sd_state,
5960d346d2aSTomi Valkeinen 						 format->pad);
5973ee47cadSRui Miguel Silva #else
5982dbcb6fbSHans Verkuil 		ret = -EINVAL;
5993ee47cadSRui Miguel Silva #endif
6003ee47cadSRui Miguel Silva 	} else {
6013ee47cadSRui Miguel Silva 		fmt = &sensor->fmt;
6023ee47cadSRui Miguel Silva 	}
6033ee47cadSRui Miguel Silva 
6043ee47cadSRui Miguel Silva 	if (fmt)
6053ee47cadSRui Miguel Silva 		format->format = *fmt;
6063ee47cadSRui Miguel Silva 
6073ee47cadSRui Miguel Silva 	mutex_unlock(&sensor->lock);
6083ee47cadSRui Miguel Silva 
6093ee47cadSRui Miguel Silva 	return ret;
6103ee47cadSRui Miguel Silva }
6113ee47cadSRui Miguel Silva 
6123ee47cadSRui Miguel Silva static int ov2680_set_fmt(struct v4l2_subdev *sd,
6130d346d2aSTomi Valkeinen 			  struct v4l2_subdev_state *sd_state,
6143ee47cadSRui Miguel Silva 			  struct v4l2_subdev_format *format)
6153ee47cadSRui Miguel Silva {
6163ee47cadSRui Miguel Silva 	struct ov2680_dev *sensor = to_ov2680_dev(sd);
6173ee47cadSRui Miguel Silva 	struct v4l2_mbus_framefmt *fmt = &format->format;
6183ee47cadSRui Miguel Silva #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
6193ee47cadSRui Miguel Silva 	struct v4l2_mbus_framefmt *try_fmt;
6203ee47cadSRui Miguel Silva #endif
6213ee47cadSRui Miguel Silva 	const struct ov2680_mode_info *mode;
6223ee47cadSRui Miguel Silva 	int ret = 0;
6233ee47cadSRui Miguel Silva 
6243ee47cadSRui Miguel Silva 	if (format->pad != 0)
6253ee47cadSRui Miguel Silva 		return -EINVAL;
6263ee47cadSRui Miguel Silva 
6273ee47cadSRui Miguel Silva 	mutex_lock(&sensor->lock);
6283ee47cadSRui Miguel Silva 
6293ee47cadSRui Miguel Silva 	if (sensor->is_streaming) {
6303ee47cadSRui Miguel Silva 		ret = -EBUSY;
6313ee47cadSRui Miguel Silva 		goto unlock;
6323ee47cadSRui Miguel Silva 	}
6333ee47cadSRui Miguel Silva 
6343ee47cadSRui Miguel Silva 	mode = v4l2_find_nearest_size(ov2680_mode_data,
6353ee47cadSRui Miguel Silva 				      ARRAY_SIZE(ov2680_mode_data), width,
6363ee47cadSRui Miguel Silva 				      height, fmt->width, fmt->height);
6373ee47cadSRui Miguel Silva 	if (!mode) {
6383ee47cadSRui Miguel Silva 		ret = -EINVAL;
6393ee47cadSRui Miguel Silva 		goto unlock;
6403ee47cadSRui Miguel Silva 	}
6413ee47cadSRui Miguel Silva 
6423ee47cadSRui Miguel Silva 	if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
6433ee47cadSRui Miguel Silva #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
6440d346d2aSTomi Valkeinen 		try_fmt = v4l2_subdev_get_try_format(sd, sd_state, 0);
6453ee47cadSRui Miguel Silva 		format->format = *try_fmt;
6463ee47cadSRui Miguel Silva #endif
6473ee47cadSRui Miguel Silva 		goto unlock;
6483ee47cadSRui Miguel Silva 	}
6493ee47cadSRui Miguel Silva 
6503ee47cadSRui Miguel Silva 	fmt->width = mode->width;
6513ee47cadSRui Miguel Silva 	fmt->height = mode->height;
6523ee47cadSRui Miguel Silva 	fmt->code = sensor->fmt.code;
6533ee47cadSRui Miguel Silva 	fmt->colorspace = sensor->fmt.colorspace;
6543ee47cadSRui Miguel Silva 
6553ee47cadSRui Miguel Silva 	sensor->current_mode = mode;
6563ee47cadSRui Miguel Silva 	sensor->fmt = format->format;
6573ee47cadSRui Miguel Silva 	sensor->mode_pending_changes = true;
6583ee47cadSRui Miguel Silva 
6593ee47cadSRui Miguel Silva unlock:
6603ee47cadSRui Miguel Silva 	mutex_unlock(&sensor->lock);
6613ee47cadSRui Miguel Silva 
6623ee47cadSRui Miguel Silva 	return ret;
6633ee47cadSRui Miguel Silva }
6643ee47cadSRui Miguel Silva 
6653ee47cadSRui Miguel Silva static int ov2680_init_cfg(struct v4l2_subdev *sd,
6660d346d2aSTomi Valkeinen 			   struct v4l2_subdev_state *sd_state)
6673ee47cadSRui Miguel Silva {
6683ee47cadSRui Miguel Silva 	struct v4l2_subdev_format fmt = {
6690d346d2aSTomi Valkeinen 		.which = sd_state ? V4L2_SUBDEV_FORMAT_TRY
6703ee47cadSRui Miguel Silva 		: V4L2_SUBDEV_FORMAT_ACTIVE,
6713ee47cadSRui Miguel Silva 		.format = {
6723ee47cadSRui Miguel Silva 			.width = 800,
6733ee47cadSRui Miguel Silva 			.height = 600,
6743ee47cadSRui Miguel Silva 		}
6753ee47cadSRui Miguel Silva 	};
6763ee47cadSRui Miguel Silva 
6770d346d2aSTomi Valkeinen 	return ov2680_set_fmt(sd, sd_state, &fmt);
6783ee47cadSRui Miguel Silva }
6793ee47cadSRui Miguel Silva 
6803ee47cadSRui Miguel Silva static int ov2680_enum_frame_size(struct v4l2_subdev *sd,
6810d346d2aSTomi Valkeinen 				  struct v4l2_subdev_state *sd_state,
6823ee47cadSRui Miguel Silva 				  struct v4l2_subdev_frame_size_enum *fse)
6833ee47cadSRui Miguel Silva {
6843ee47cadSRui Miguel Silva 	int index = fse->index;
6853ee47cadSRui Miguel Silva 
6863ee47cadSRui Miguel Silva 	if (index >= OV2680_MODE_MAX || index < 0)
6873ee47cadSRui Miguel Silva 		return -EINVAL;
6883ee47cadSRui Miguel Silva 
6893ee47cadSRui Miguel Silva 	fse->min_width = ov2680_mode_data[index].width;
6903ee47cadSRui Miguel Silva 	fse->min_height = ov2680_mode_data[index].height;
6913ee47cadSRui Miguel Silva 	fse->max_width = ov2680_mode_data[index].width;
6923ee47cadSRui Miguel Silva 	fse->max_height = ov2680_mode_data[index].height;
6933ee47cadSRui Miguel Silva 
6943ee47cadSRui Miguel Silva 	return 0;
6953ee47cadSRui Miguel Silva }
6963ee47cadSRui Miguel Silva 
6973ee47cadSRui Miguel Silva static int ov2680_enum_frame_interval(struct v4l2_subdev *sd,
6980d346d2aSTomi Valkeinen 			      struct v4l2_subdev_state *sd_state,
6993ee47cadSRui Miguel Silva 			      struct v4l2_subdev_frame_interval_enum *fie)
7003ee47cadSRui Miguel Silva {
7013ee47cadSRui Miguel Silva 	struct v4l2_fract tpf;
7023ee47cadSRui Miguel Silva 
7033ee47cadSRui Miguel Silva 	if (fie->index >= OV2680_MODE_MAX || fie->width > OV2680_WIDTH_MAX ||
7043ee47cadSRui Miguel Silva 	    fie->height > OV2680_HEIGHT_MAX ||
7053ee47cadSRui Miguel Silva 	    fie->which > V4L2_SUBDEV_FORMAT_ACTIVE)
7063ee47cadSRui Miguel Silva 		return -EINVAL;
7073ee47cadSRui Miguel Silva 
7083ee47cadSRui Miguel Silva 	tpf.denominator = OV2680_FRAME_RATE;
7093ee47cadSRui Miguel Silva 	tpf.numerator = 1;
7103ee47cadSRui Miguel Silva 
7113ee47cadSRui Miguel Silva 	fie->interval = tpf;
7123ee47cadSRui Miguel Silva 
7133ee47cadSRui Miguel Silva 	return 0;
7143ee47cadSRui Miguel Silva }
7153ee47cadSRui Miguel Silva 
7163ee47cadSRui Miguel Silva static int ov2680_s_ctrl(struct v4l2_ctrl *ctrl)
7173ee47cadSRui Miguel Silva {
7183ee47cadSRui Miguel Silva 	struct v4l2_subdev *sd = ctrl_to_sd(ctrl);
7193ee47cadSRui Miguel Silva 	struct ov2680_dev *sensor = to_ov2680_dev(sd);
7203ee47cadSRui Miguel Silva 
7213ee47cadSRui Miguel Silva 	if (!sensor->is_enabled)
7223ee47cadSRui Miguel Silva 		return 0;
7233ee47cadSRui Miguel Silva 
7243ee47cadSRui Miguel Silva 	switch (ctrl->id) {
7253ee47cadSRui Miguel Silva 	case V4L2_CID_GAIN:
726*7b5a42e6SHans de Goede 		return ov2680_gain_set(sensor, ctrl->val);
7273ee47cadSRui Miguel Silva 	case V4L2_CID_EXPOSURE:
728*7b5a42e6SHans de Goede 		return ov2680_exposure_set(sensor, ctrl->val);
7293ee47cadSRui Miguel Silva 	case V4L2_CID_VFLIP:
7303ee47cadSRui Miguel Silva 		if (sensor->is_streaming)
7313ee47cadSRui Miguel Silva 			return -EBUSY;
7323ee47cadSRui Miguel Silva 		if (ctrl->val)
7333ee47cadSRui Miguel Silva 			return ov2680_vflip_enable(sensor);
7343ee47cadSRui Miguel Silva 		else
7353ee47cadSRui Miguel Silva 			return ov2680_vflip_disable(sensor);
7363ee47cadSRui Miguel Silva 	case V4L2_CID_HFLIP:
7373ee47cadSRui Miguel Silva 		if (sensor->is_streaming)
7383ee47cadSRui Miguel Silva 			return -EBUSY;
7393ee47cadSRui Miguel Silva 		if (ctrl->val)
7403ee47cadSRui Miguel Silva 			return ov2680_hflip_enable(sensor);
7413ee47cadSRui Miguel Silva 		else
7423ee47cadSRui Miguel Silva 			return ov2680_hflip_disable(sensor);
7433ee47cadSRui Miguel Silva 	case V4L2_CID_TEST_PATTERN:
7443ee47cadSRui Miguel Silva 		return ov2680_test_pattern_set(sensor, ctrl->val);
7453ee47cadSRui Miguel Silva 	default:
7463ee47cadSRui Miguel Silva 		break;
7473ee47cadSRui Miguel Silva 	}
7483ee47cadSRui Miguel Silva 
7493ee47cadSRui Miguel Silva 	return -EINVAL;
7503ee47cadSRui Miguel Silva }
7513ee47cadSRui Miguel Silva 
7523ee47cadSRui Miguel Silva static const struct v4l2_ctrl_ops ov2680_ctrl_ops = {
7533ee47cadSRui Miguel Silva 	.s_ctrl = ov2680_s_ctrl,
7543ee47cadSRui Miguel Silva };
7553ee47cadSRui Miguel Silva 
7563ee47cadSRui Miguel Silva static const struct v4l2_subdev_core_ops ov2680_core_ops = {
7573ee47cadSRui Miguel Silva 	.s_power = ov2680_s_power,
7583ee47cadSRui Miguel Silva };
7593ee47cadSRui Miguel Silva 
7603ee47cadSRui Miguel Silva static const struct v4l2_subdev_video_ops ov2680_video_ops = {
7613ee47cadSRui Miguel Silva 	.g_frame_interval	= ov2680_s_g_frame_interval,
7623ee47cadSRui Miguel Silva 	.s_frame_interval	= ov2680_s_g_frame_interval,
7633ee47cadSRui Miguel Silva 	.s_stream		= ov2680_s_stream,
7643ee47cadSRui Miguel Silva };
7653ee47cadSRui Miguel Silva 
7663ee47cadSRui Miguel Silva static const struct v4l2_subdev_pad_ops ov2680_pad_ops = {
7673ee47cadSRui Miguel Silva 	.init_cfg		= ov2680_init_cfg,
7683ee47cadSRui Miguel Silva 	.enum_mbus_code		= ov2680_enum_mbus_code,
7693ee47cadSRui Miguel Silva 	.get_fmt		= ov2680_get_fmt,
7703ee47cadSRui Miguel Silva 	.set_fmt		= ov2680_set_fmt,
7713ee47cadSRui Miguel Silva 	.enum_frame_size	= ov2680_enum_frame_size,
7723ee47cadSRui Miguel Silva 	.enum_frame_interval	= ov2680_enum_frame_interval,
7733ee47cadSRui Miguel Silva };
7743ee47cadSRui Miguel Silva 
7753ee47cadSRui Miguel Silva static const struct v4l2_subdev_ops ov2680_subdev_ops = {
7763ee47cadSRui Miguel Silva 	.core	= &ov2680_core_ops,
7773ee47cadSRui Miguel Silva 	.video	= &ov2680_video_ops,
7783ee47cadSRui Miguel Silva 	.pad	= &ov2680_pad_ops,
7793ee47cadSRui Miguel Silva };
7803ee47cadSRui Miguel Silva 
7813ee47cadSRui Miguel Silva static int ov2680_mode_init(struct ov2680_dev *sensor)
7823ee47cadSRui Miguel Silva {
7833ee47cadSRui Miguel Silva 	const struct ov2680_mode_info *init_mode;
7843ee47cadSRui Miguel Silva 
7853ee47cadSRui Miguel Silva 	/* set initial mode */
7863ee47cadSRui Miguel Silva 	sensor->fmt.code = MEDIA_BUS_FMT_SBGGR10_1X10;
7873ee47cadSRui Miguel Silva 	sensor->fmt.width = 800;
7883ee47cadSRui Miguel Silva 	sensor->fmt.height = 600;
7893ee47cadSRui Miguel Silva 	sensor->fmt.field = V4L2_FIELD_NONE;
7903ee47cadSRui Miguel Silva 	sensor->fmt.colorspace = V4L2_COLORSPACE_SRGB;
7913ee47cadSRui Miguel Silva 
7923ee47cadSRui Miguel Silva 	sensor->frame_interval.denominator = OV2680_FRAME_RATE;
7933ee47cadSRui Miguel Silva 	sensor->frame_interval.numerator = 1;
7943ee47cadSRui Miguel Silva 
7953ee47cadSRui Miguel Silva 	init_mode = &ov2680_mode_init_data;
7963ee47cadSRui Miguel Silva 
7973ee47cadSRui Miguel Silva 	sensor->current_mode = init_mode;
7983ee47cadSRui Miguel Silva 
7993ee47cadSRui Miguel Silva 	sensor->mode_pending_changes = true;
8003ee47cadSRui Miguel Silva 
8013ee47cadSRui Miguel Silva 	return 0;
8023ee47cadSRui Miguel Silva }
8033ee47cadSRui Miguel Silva 
804e7f4861dSJavier Martinez Canillas static int ov2680_v4l2_register(struct ov2680_dev *sensor)
8053ee47cadSRui Miguel Silva {
8063ee47cadSRui Miguel Silva 	const struct v4l2_ctrl_ops *ops = &ov2680_ctrl_ops;
8073ee47cadSRui Miguel Silva 	struct ov2680_ctrls *ctrls = &sensor->ctrls;
8083ee47cadSRui Miguel Silva 	struct v4l2_ctrl_handler *hdl = &ctrls->handler;
8093ee47cadSRui Miguel Silva 	int ret = 0;
8103ee47cadSRui Miguel Silva 
8113ee47cadSRui Miguel Silva 	v4l2_i2c_subdev_init(&sensor->sd, sensor->i2c_client,
8123ee47cadSRui Miguel Silva 			     &ov2680_subdev_ops);
8133ee47cadSRui Miguel Silva 
8143ee47cadSRui Miguel Silva #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
8153ee47cadSRui Miguel Silva 	sensor->sd.flags = V4L2_SUBDEV_FL_HAS_DEVNODE;
8163ee47cadSRui Miguel Silva #endif
8173ee47cadSRui Miguel Silva 	sensor->pad.flags = MEDIA_PAD_FL_SOURCE;
8183ee47cadSRui Miguel Silva 	sensor->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
8193ee47cadSRui Miguel Silva 
8203ee47cadSRui Miguel Silva 	ret = media_entity_pads_init(&sensor->sd.entity, 1, &sensor->pad);
8213ee47cadSRui Miguel Silva 	if (ret < 0)
8223ee47cadSRui Miguel Silva 		return ret;
8233ee47cadSRui Miguel Silva 
824*7b5a42e6SHans de Goede 	v4l2_ctrl_handler_init(hdl, 5);
8253ee47cadSRui Miguel Silva 
8263ee47cadSRui Miguel Silva 	hdl->lock = &sensor->lock;
8273ee47cadSRui Miguel Silva 
8283ee47cadSRui Miguel Silva 	ctrls->vflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_VFLIP, 0, 1, 1, 0);
8293ee47cadSRui Miguel Silva 	ctrls->hflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_HFLIP, 0, 1, 1, 0);
8303ee47cadSRui Miguel Silva 
8313ee47cadSRui Miguel Silva 	ctrls->test_pattern = v4l2_ctrl_new_std_menu_items(hdl,
8323ee47cadSRui Miguel Silva 					&ov2680_ctrl_ops, V4L2_CID_TEST_PATTERN,
8333ee47cadSRui Miguel Silva 					ARRAY_SIZE(test_pattern_menu) - 1,
8343ee47cadSRui Miguel Silva 					0, 0, test_pattern_menu);
8353ee47cadSRui Miguel Silva 
8363ee47cadSRui Miguel Silva 	ctrls->exposure = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_EXPOSURE,
8373ee47cadSRui Miguel Silva 					    0, 32767, 1, 0);
8383ee47cadSRui Miguel Silva 
8393ee47cadSRui Miguel Silva 	ctrls->gain = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_GAIN, 0, 2047, 1, 0);
8403ee47cadSRui Miguel Silva 
8413ee47cadSRui Miguel Silva 	if (hdl->error) {
8423ee47cadSRui Miguel Silva 		ret = hdl->error;
8433ee47cadSRui Miguel Silva 		goto cleanup_entity;
8443ee47cadSRui Miguel Silva 	}
8453ee47cadSRui Miguel Silva 
84666274280SDave Stevenson 	ctrls->vflip->flags |= V4L2_CTRL_FLAG_MODIFY_LAYOUT;
84766274280SDave Stevenson 	ctrls->hflip->flags |= V4L2_CTRL_FLAG_MODIFY_LAYOUT;
8483ee47cadSRui Miguel Silva 
8493ee47cadSRui Miguel Silva 	sensor->sd.ctrl_handler = hdl;
8503ee47cadSRui Miguel Silva 
8513ee47cadSRui Miguel Silva 	ret = v4l2_async_register_subdev(&sensor->sd);
8523ee47cadSRui Miguel Silva 	if (ret < 0)
8533ee47cadSRui Miguel Silva 		goto cleanup_entity;
8543ee47cadSRui Miguel Silva 
8553ee47cadSRui Miguel Silva 	return 0;
8563ee47cadSRui Miguel Silva 
8573ee47cadSRui Miguel Silva cleanup_entity:
8583ee47cadSRui Miguel Silva 	media_entity_cleanup(&sensor->sd.entity);
8593ee47cadSRui Miguel Silva 	v4l2_ctrl_handler_free(hdl);
8603ee47cadSRui Miguel Silva 
8613ee47cadSRui Miguel Silva 	return ret;
8623ee47cadSRui Miguel Silva }
8633ee47cadSRui Miguel Silva 
8643ee47cadSRui Miguel Silva static int ov2680_get_regulators(struct ov2680_dev *sensor)
8653ee47cadSRui Miguel Silva {
8663ee47cadSRui Miguel Silva 	int i;
8673ee47cadSRui Miguel Silva 
8683ee47cadSRui Miguel Silva 	for (i = 0; i < OV2680_NUM_SUPPLIES; i++)
8693ee47cadSRui Miguel Silva 		sensor->supplies[i].supply = ov2680_supply_name[i];
8703ee47cadSRui Miguel Silva 
8713ee47cadSRui Miguel Silva 	return devm_regulator_bulk_get(&sensor->i2c_client->dev,
8723ee47cadSRui Miguel Silva 				       OV2680_NUM_SUPPLIES,
8733ee47cadSRui Miguel Silva 				       sensor->supplies);
8743ee47cadSRui Miguel Silva }
8753ee47cadSRui Miguel Silva 
8763ee47cadSRui Miguel Silva static int ov2680_check_id(struct ov2680_dev *sensor)
8773ee47cadSRui Miguel Silva {
8783ee47cadSRui Miguel Silva 	struct device *dev = ov2680_to_dev(sensor);
8793ee47cadSRui Miguel Silva 	u32 chip_id;
8803ee47cadSRui Miguel Silva 	int ret;
8813ee47cadSRui Miguel Silva 
8823ee47cadSRui Miguel Silva 	ov2680_power_on(sensor);
8833ee47cadSRui Miguel Silva 
8843ee47cadSRui Miguel Silva 	ret = ov2680_read_reg16(sensor, OV2680_REG_CHIP_ID_HIGH, &chip_id);
8853ee47cadSRui Miguel Silva 	if (ret < 0) {
8863ee47cadSRui Miguel Silva 		dev_err(dev, "failed to read chip id high\n");
8873ee47cadSRui Miguel Silva 		return -ENODEV;
8883ee47cadSRui Miguel Silva 	}
8893ee47cadSRui Miguel Silva 
8903ee47cadSRui Miguel Silva 	if (chip_id != OV2680_CHIP_ID) {
8913ee47cadSRui Miguel Silva 		dev_err(dev, "chip id: 0x%04x does not match expected 0x%04x\n",
8923ee47cadSRui Miguel Silva 			chip_id, OV2680_CHIP_ID);
8933ee47cadSRui Miguel Silva 		return -ENODEV;
8943ee47cadSRui Miguel Silva 	}
8953ee47cadSRui Miguel Silva 
8963ee47cadSRui Miguel Silva 	return 0;
8973ee47cadSRui Miguel Silva }
8983ee47cadSRui Miguel Silva 
899913c55a8SChristophe JAILLET static int ov2680_parse_dt(struct ov2680_dev *sensor)
9003ee47cadSRui Miguel Silva {
9013ee47cadSRui Miguel Silva 	struct device *dev = ov2680_to_dev(sensor);
9023ee47cadSRui Miguel Silva 	int ret;
9033ee47cadSRui Miguel Silva 
9043ee47cadSRui Miguel Silva 	sensor->reset_gpio = devm_gpiod_get_optional(dev, "reset",
9053ee47cadSRui Miguel Silva 						     GPIOD_OUT_HIGH);
9063ee47cadSRui Miguel Silva 	ret = PTR_ERR_OR_ZERO(sensor->reset_gpio);
9073ee47cadSRui Miguel Silva 	if (ret < 0) {
9083ee47cadSRui Miguel Silva 		dev_dbg(dev, "error while getting reset gpio: %d\n", ret);
9093ee47cadSRui Miguel Silva 		return ret;
9103ee47cadSRui Miguel Silva 	}
9113ee47cadSRui Miguel Silva 
9123ee47cadSRui Miguel Silva 	sensor->xvclk = devm_clk_get(dev, "xvclk");
9133ee47cadSRui Miguel Silva 	if (IS_ERR(sensor->xvclk)) {
9143ee47cadSRui Miguel Silva 		dev_err(dev, "xvclk clock missing or invalid\n");
9153ee47cadSRui Miguel Silva 		return PTR_ERR(sensor->xvclk);
9163ee47cadSRui Miguel Silva 	}
9173ee47cadSRui Miguel Silva 
9183ee47cadSRui Miguel Silva 	sensor->xvclk_freq = clk_get_rate(sensor->xvclk);
9193ee47cadSRui Miguel Silva 	if (sensor->xvclk_freq != OV2680_XVCLK_VALUE) {
9203ee47cadSRui Miguel Silva 		dev_err(dev, "wrong xvclk frequency %d HZ, expected: %d Hz\n",
9213ee47cadSRui Miguel Silva 			sensor->xvclk_freq, OV2680_XVCLK_VALUE);
9223ee47cadSRui Miguel Silva 		return -EINVAL;
9233ee47cadSRui Miguel Silva 	}
9243ee47cadSRui Miguel Silva 
9253ee47cadSRui Miguel Silva 	return 0;
9263ee47cadSRui Miguel Silva }
9273ee47cadSRui Miguel Silva 
9283ee47cadSRui Miguel Silva static int ov2680_probe(struct i2c_client *client)
9293ee47cadSRui Miguel Silva {
9303ee47cadSRui Miguel Silva 	struct device *dev = &client->dev;
9313ee47cadSRui Miguel Silva 	struct ov2680_dev *sensor;
9323ee47cadSRui Miguel Silva 	int ret;
9333ee47cadSRui Miguel Silva 
9343ee47cadSRui Miguel Silva 	sensor = devm_kzalloc(dev, sizeof(*sensor), GFP_KERNEL);
9353ee47cadSRui Miguel Silva 	if (!sensor)
9363ee47cadSRui Miguel Silva 		return -ENOMEM;
9373ee47cadSRui Miguel Silva 
9383ee47cadSRui Miguel Silva 	sensor->i2c_client = client;
9393ee47cadSRui Miguel Silva 
940913c55a8SChristophe JAILLET 	ret = ov2680_parse_dt(sensor);
9413ee47cadSRui Miguel Silva 	if (ret < 0)
9423ee47cadSRui Miguel Silva 		return -EINVAL;
9433ee47cadSRui Miguel Silva 
9443ee47cadSRui Miguel Silva 	ret = ov2680_mode_init(sensor);
9453ee47cadSRui Miguel Silva 	if (ret < 0)
9463ee47cadSRui Miguel Silva 		return ret;
9473ee47cadSRui Miguel Silva 
9483ee47cadSRui Miguel Silva 	ret = ov2680_get_regulators(sensor);
9493ee47cadSRui Miguel Silva 	if (ret < 0) {
9503ee47cadSRui Miguel Silva 		dev_err(dev, "failed to get regulators\n");
9513ee47cadSRui Miguel Silva 		return ret;
9523ee47cadSRui Miguel Silva 	}
9533ee47cadSRui Miguel Silva 
9543ee47cadSRui Miguel Silva 	mutex_init(&sensor->lock);
9553ee47cadSRui Miguel Silva 
956b7a41762SJavier Martinez Canillas 	ret = ov2680_check_id(sensor);
9573ee47cadSRui Miguel Silva 	if (ret < 0)
9583ee47cadSRui Miguel Silva 		goto lock_destroy;
9593ee47cadSRui Miguel Silva 
960e7f4861dSJavier Martinez Canillas 	ret = ov2680_v4l2_register(sensor);
9613ee47cadSRui Miguel Silva 	if (ret < 0)
962b7a41762SJavier Martinez Canillas 		goto lock_destroy;
9633ee47cadSRui Miguel Silva 
9643ee47cadSRui Miguel Silva 	dev_info(dev, "ov2680 init correctly\n");
9653ee47cadSRui Miguel Silva 
9663ee47cadSRui Miguel Silva 	return 0;
9673ee47cadSRui Miguel Silva 
9683ee47cadSRui Miguel Silva lock_destroy:
969b7a41762SJavier Martinez Canillas 	dev_err(dev, "ov2680 init fail: %d\n", ret);
9703ee47cadSRui Miguel Silva 	mutex_destroy(&sensor->lock);
9713ee47cadSRui Miguel Silva 
9723ee47cadSRui Miguel Silva 	return ret;
9733ee47cadSRui Miguel Silva }
9743ee47cadSRui Miguel Silva 
975ed5c2f5fSUwe Kleine-König static void ov2680_remove(struct i2c_client *client)
9763ee47cadSRui Miguel Silva {
9773ee47cadSRui Miguel Silva 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
9783ee47cadSRui Miguel Silva 	struct ov2680_dev *sensor = to_ov2680_dev(sd);
9793ee47cadSRui Miguel Silva 
9803ee47cadSRui Miguel Silva 	v4l2_async_unregister_subdev(&sensor->sd);
9813ee47cadSRui Miguel Silva 	mutex_destroy(&sensor->lock);
9823ee47cadSRui Miguel Silva 	media_entity_cleanup(&sensor->sd.entity);
9833ee47cadSRui Miguel Silva 	v4l2_ctrl_handler_free(&sensor->ctrls.handler);
9843ee47cadSRui Miguel Silva }
9853ee47cadSRui Miguel Silva 
9863ee47cadSRui Miguel Silva static int __maybe_unused ov2680_suspend(struct device *dev)
9873ee47cadSRui Miguel Silva {
9887519296dSKrzysztof Kozlowski 	struct v4l2_subdev *sd = dev_get_drvdata(dev);
9893ee47cadSRui Miguel Silva 	struct ov2680_dev *sensor = to_ov2680_dev(sd);
9903ee47cadSRui Miguel Silva 
9913ee47cadSRui Miguel Silva 	if (sensor->is_streaming)
9923ee47cadSRui Miguel Silva 		ov2680_stream_disable(sensor);
9933ee47cadSRui Miguel Silva 
9943ee47cadSRui Miguel Silva 	return 0;
9953ee47cadSRui Miguel Silva }
9963ee47cadSRui Miguel Silva 
9973ee47cadSRui Miguel Silva static int __maybe_unused ov2680_resume(struct device *dev)
9983ee47cadSRui Miguel Silva {
9997519296dSKrzysztof Kozlowski 	struct v4l2_subdev *sd = dev_get_drvdata(dev);
10003ee47cadSRui Miguel Silva 	struct ov2680_dev *sensor = to_ov2680_dev(sd);
10013ee47cadSRui Miguel Silva 	int ret;
10023ee47cadSRui Miguel Silva 
10033ee47cadSRui Miguel Silva 	if (sensor->is_streaming) {
10043ee47cadSRui Miguel Silva 		ret = ov2680_stream_enable(sensor);
10053ee47cadSRui Miguel Silva 		if (ret < 0)
10063ee47cadSRui Miguel Silva 			goto stream_disable;
10073ee47cadSRui Miguel Silva 	}
10083ee47cadSRui Miguel Silva 
10093ee47cadSRui Miguel Silva 	return 0;
10103ee47cadSRui Miguel Silva 
10113ee47cadSRui Miguel Silva stream_disable:
10123ee47cadSRui Miguel Silva 	ov2680_stream_disable(sensor);
10133ee47cadSRui Miguel Silva 	sensor->is_streaming = false;
10143ee47cadSRui Miguel Silva 
10153ee47cadSRui Miguel Silva 	return ret;
10163ee47cadSRui Miguel Silva }
10173ee47cadSRui Miguel Silva 
10183ee47cadSRui Miguel Silva static const struct dev_pm_ops ov2680_pm_ops = {
10193ee47cadSRui Miguel Silva 	SET_SYSTEM_SLEEP_PM_OPS(ov2680_suspend, ov2680_resume)
10203ee47cadSRui Miguel Silva };
10213ee47cadSRui Miguel Silva 
10223ee47cadSRui Miguel Silva static const struct of_device_id ov2680_dt_ids[] = {
10233ee47cadSRui Miguel Silva 	{ .compatible = "ovti,ov2680" },
10243ee47cadSRui Miguel Silva 	{ /* sentinel */ },
10253ee47cadSRui Miguel Silva };
10263ee47cadSRui Miguel Silva MODULE_DEVICE_TABLE(of, ov2680_dt_ids);
10273ee47cadSRui Miguel Silva 
10283ee47cadSRui Miguel Silva static struct i2c_driver ov2680_i2c_driver = {
10293ee47cadSRui Miguel Silva 	.driver = {
10303ee47cadSRui Miguel Silva 		.name  = "ov2680",
10313ee47cadSRui Miguel Silva 		.pm = &ov2680_pm_ops,
103214155c4fSKrzysztof Kozlowski 		.of_match_table	= ov2680_dt_ids,
10333ee47cadSRui Miguel Silva 	},
1034aaeb31c0SUwe Kleine-König 	.probe		= ov2680_probe,
10353ee47cadSRui Miguel Silva 	.remove		= ov2680_remove,
10363ee47cadSRui Miguel Silva };
10373ee47cadSRui Miguel Silva module_i2c_driver(ov2680_i2c_driver);
10383ee47cadSRui Miguel Silva 
10393ee47cadSRui Miguel Silva MODULE_AUTHOR("Rui Miguel Silva <rui.silva@linaro.org>");
10403ee47cadSRui Miguel Silva MODULE_DESCRIPTION("OV2680 CMOS Image Sensor driver");
10413ee47cadSRui Miguel Silva MODULE_LICENSE("GPL v2");
1042