18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Omnivision OV2680 CMOS Image Sensor driver
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2018 Linaro Ltd
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * Based on OV5640 Sensor Driver
88c2ecf20Sopenharmony_ci * Copyright (C) 2011-2013 Freescale Semiconductor, Inc. All Rights Reserved.
98c2ecf20Sopenharmony_ci * Copyright (C) 2014-2017 Mentor Graphics Inc.
108c2ecf20Sopenharmony_ci *
118c2ecf20Sopenharmony_ci */
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci#include <asm/unaligned.h>
148c2ecf20Sopenharmony_ci#include <linux/clk.h>
158c2ecf20Sopenharmony_ci#include <linux/delay.h>
168c2ecf20Sopenharmony_ci#include <linux/err.h>
178c2ecf20Sopenharmony_ci#include <linux/i2c.h>
188c2ecf20Sopenharmony_ci#include <linux/init.h>
198c2ecf20Sopenharmony_ci#include <linux/module.h>
208c2ecf20Sopenharmony_ci#include <linux/of_device.h>
218c2ecf20Sopenharmony_ci#include <linux/gpio/consumer.h>
228c2ecf20Sopenharmony_ci#include <linux/regulator/consumer.h>
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci#include <media/v4l2-common.h>
258c2ecf20Sopenharmony_ci#include <media/v4l2-ctrls.h>
268c2ecf20Sopenharmony_ci#include <media/v4l2-subdev.h>
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci#define OV2680_XVCLK_VALUE	24000000
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci#define OV2680_CHIP_ID		0x2680
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci#define OV2680_REG_STREAM_CTRL		0x0100
338c2ecf20Sopenharmony_ci#define OV2680_REG_SOFT_RESET		0x0103
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci#define OV2680_REG_CHIP_ID_HIGH		0x300a
368c2ecf20Sopenharmony_ci#define OV2680_REG_CHIP_ID_LOW		0x300b
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ci#define OV2680_REG_R_MANUAL		0x3503
398c2ecf20Sopenharmony_ci#define OV2680_REG_GAIN_PK		0x350a
408c2ecf20Sopenharmony_ci#define OV2680_REG_EXPOSURE_PK_HIGH	0x3500
418c2ecf20Sopenharmony_ci#define OV2680_REG_TIMING_HTS		0x380c
428c2ecf20Sopenharmony_ci#define OV2680_REG_TIMING_VTS		0x380e
438c2ecf20Sopenharmony_ci#define OV2680_REG_FORMAT1		0x3820
448c2ecf20Sopenharmony_ci#define OV2680_REG_FORMAT2		0x3821
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci#define OV2680_REG_ISP_CTRL00		0x5080
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci#define OV2680_FRAME_RATE		30
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ci#define OV2680_REG_VALUE_8BIT		1
518c2ecf20Sopenharmony_ci#define OV2680_REG_VALUE_16BIT		2
528c2ecf20Sopenharmony_ci#define OV2680_REG_VALUE_24BIT		3
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci#define OV2680_WIDTH_MAX		1600
558c2ecf20Sopenharmony_ci#define OV2680_HEIGHT_MAX		1200
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_cienum ov2680_mode_id {
588c2ecf20Sopenharmony_ci	OV2680_MODE_QUXGA_800_600,
598c2ecf20Sopenharmony_ci	OV2680_MODE_720P_1280_720,
608c2ecf20Sopenharmony_ci	OV2680_MODE_UXGA_1600_1200,
618c2ecf20Sopenharmony_ci	OV2680_MODE_MAX,
628c2ecf20Sopenharmony_ci};
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_cistruct reg_value {
658c2ecf20Sopenharmony_ci	u16 reg_addr;
668c2ecf20Sopenharmony_ci	u8 val;
678c2ecf20Sopenharmony_ci};
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_cistatic const char * const ov2680_supply_name[] = {
708c2ecf20Sopenharmony_ci	"DOVDD",
718c2ecf20Sopenharmony_ci	"DVDD",
728c2ecf20Sopenharmony_ci	"AVDD",
738c2ecf20Sopenharmony_ci};
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci#define OV2680_NUM_SUPPLIES ARRAY_SIZE(ov2680_supply_name)
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_cistruct ov2680_mode_info {
788c2ecf20Sopenharmony_ci	const char *name;
798c2ecf20Sopenharmony_ci	enum ov2680_mode_id id;
808c2ecf20Sopenharmony_ci	u32 width;
818c2ecf20Sopenharmony_ci	u32 height;
828c2ecf20Sopenharmony_ci	const struct reg_value *reg_data;
838c2ecf20Sopenharmony_ci	u32 reg_data_size;
848c2ecf20Sopenharmony_ci};
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_cistruct ov2680_ctrls {
878c2ecf20Sopenharmony_ci	struct v4l2_ctrl_handler handler;
888c2ecf20Sopenharmony_ci	struct v4l2_ctrl *exposure;
898c2ecf20Sopenharmony_ci	struct v4l2_ctrl *gain;
908c2ecf20Sopenharmony_ci	struct v4l2_ctrl *hflip;
918c2ecf20Sopenharmony_ci	struct v4l2_ctrl *vflip;
928c2ecf20Sopenharmony_ci	struct v4l2_ctrl *test_pattern;
938c2ecf20Sopenharmony_ci};
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_cistruct ov2680_dev {
968c2ecf20Sopenharmony_ci	struct i2c_client		*i2c_client;
978c2ecf20Sopenharmony_ci	struct v4l2_subdev		sd;
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_ci	struct media_pad		pad;
1008c2ecf20Sopenharmony_ci	struct clk			*xvclk;
1018c2ecf20Sopenharmony_ci	u32				xvclk_freq;
1028c2ecf20Sopenharmony_ci	struct regulator_bulk_data	supplies[OV2680_NUM_SUPPLIES];
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci	struct gpio_desc		*reset_gpio;
1058c2ecf20Sopenharmony_ci	struct mutex			lock; /* protect members */
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_ci	bool				mode_pending_changes;
1088c2ecf20Sopenharmony_ci	bool				is_enabled;
1098c2ecf20Sopenharmony_ci	bool				is_streaming;
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_ci	struct ov2680_ctrls		ctrls;
1128c2ecf20Sopenharmony_ci	struct v4l2_mbus_framefmt	fmt;
1138c2ecf20Sopenharmony_ci	struct v4l2_fract		frame_interval;
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ci	const struct ov2680_mode_info	*current_mode;
1168c2ecf20Sopenharmony_ci};
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_cistatic const char * const test_pattern_menu[] = {
1198c2ecf20Sopenharmony_ci	"Disabled",
1208c2ecf20Sopenharmony_ci	"Color Bars",
1218c2ecf20Sopenharmony_ci	"Random Data",
1228c2ecf20Sopenharmony_ci	"Square",
1238c2ecf20Sopenharmony_ci	"Black Image",
1248c2ecf20Sopenharmony_ci};
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_cistatic const int ov2680_hv_flip_bayer_order[] = {
1278c2ecf20Sopenharmony_ci	MEDIA_BUS_FMT_SBGGR10_1X10,
1288c2ecf20Sopenharmony_ci	MEDIA_BUS_FMT_SGRBG10_1X10,
1298c2ecf20Sopenharmony_ci	MEDIA_BUS_FMT_SGBRG10_1X10,
1308c2ecf20Sopenharmony_ci	MEDIA_BUS_FMT_SRGGB10_1X10,
1318c2ecf20Sopenharmony_ci};
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_cistatic const struct reg_value ov2680_setting_30fps_QUXGA_800_600[] = {
1348c2ecf20Sopenharmony_ci	{0x3086, 0x01}, {0x370a, 0x23}, {0x3808, 0x03}, {0x3809, 0x20},
1358c2ecf20Sopenharmony_ci	{0x380a, 0x02}, {0x380b, 0x58}, {0x380c, 0x06}, {0x380d, 0xac},
1368c2ecf20Sopenharmony_ci	{0x380e, 0x02}, {0x380f, 0x84}, {0x3811, 0x04}, {0x3813, 0x04},
1378c2ecf20Sopenharmony_ci	{0x3814, 0x31}, {0x3815, 0x31}, {0x3820, 0xc0}, {0x4008, 0x00},
1388c2ecf20Sopenharmony_ci	{0x4009, 0x03}, {0x4837, 0x1e}, {0x3501, 0x4e}, {0x3502, 0xe0},
1398c2ecf20Sopenharmony_ci	{0x3503, 0x03},
1408c2ecf20Sopenharmony_ci};
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_cistatic const struct reg_value ov2680_setting_30fps_720P_1280_720[] = {
1438c2ecf20Sopenharmony_ci	{0x3086, 0x00}, {0x3808, 0x05}, {0x3809, 0x00}, {0x380a, 0x02},
1448c2ecf20Sopenharmony_ci	{0x380b, 0xd0}, {0x380c, 0x06}, {0x380d, 0xa8}, {0x380e, 0x05},
1458c2ecf20Sopenharmony_ci	{0x380f, 0x0e}, {0x3811, 0x08}, {0x3813, 0x06}, {0x3814, 0x11},
1468c2ecf20Sopenharmony_ci	{0x3815, 0x11}, {0x3820, 0xc0}, {0x4008, 0x00},
1478c2ecf20Sopenharmony_ci};
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_cistatic const struct reg_value ov2680_setting_30fps_UXGA_1600_1200[] = {
1508c2ecf20Sopenharmony_ci	{0x3086, 0x00}, {0x3501, 0x4e}, {0x3502, 0xe0}, {0x3808, 0x06},
1518c2ecf20Sopenharmony_ci	{0x3809, 0x40}, {0x380a, 0x04}, {0x380b, 0xb0}, {0x380c, 0x06},
1528c2ecf20Sopenharmony_ci	{0x380d, 0xa8}, {0x380e, 0x05}, {0x380f, 0x0e}, {0x3811, 0x00},
1538c2ecf20Sopenharmony_ci	{0x3813, 0x00}, {0x3814, 0x11}, {0x3815, 0x11}, {0x3820, 0xc0},
1548c2ecf20Sopenharmony_ci	{0x4008, 0x00}, {0x4837, 0x18}
1558c2ecf20Sopenharmony_ci};
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_cistatic const struct ov2680_mode_info ov2680_mode_init_data = {
1588c2ecf20Sopenharmony_ci	"mode_quxga_800_600", OV2680_MODE_QUXGA_800_600, 800, 600,
1598c2ecf20Sopenharmony_ci	ov2680_setting_30fps_QUXGA_800_600,
1608c2ecf20Sopenharmony_ci	ARRAY_SIZE(ov2680_setting_30fps_QUXGA_800_600),
1618c2ecf20Sopenharmony_ci};
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_cistatic const struct ov2680_mode_info ov2680_mode_data[OV2680_MODE_MAX] = {
1648c2ecf20Sopenharmony_ci	{"mode_quxga_800_600", OV2680_MODE_QUXGA_800_600,
1658c2ecf20Sopenharmony_ci	 800, 600, ov2680_setting_30fps_QUXGA_800_600,
1668c2ecf20Sopenharmony_ci	 ARRAY_SIZE(ov2680_setting_30fps_QUXGA_800_600)},
1678c2ecf20Sopenharmony_ci	{"mode_720p_1280_720", OV2680_MODE_720P_1280_720,
1688c2ecf20Sopenharmony_ci	 1280, 720, ov2680_setting_30fps_720P_1280_720,
1698c2ecf20Sopenharmony_ci	 ARRAY_SIZE(ov2680_setting_30fps_720P_1280_720)},
1708c2ecf20Sopenharmony_ci	{"mode_uxga_1600_1200", OV2680_MODE_UXGA_1600_1200,
1718c2ecf20Sopenharmony_ci	 1600, 1200, ov2680_setting_30fps_UXGA_1600_1200,
1728c2ecf20Sopenharmony_ci	 ARRAY_SIZE(ov2680_setting_30fps_UXGA_1600_1200)},
1738c2ecf20Sopenharmony_ci};
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_cistatic struct ov2680_dev *to_ov2680_dev(struct v4l2_subdev *sd)
1768c2ecf20Sopenharmony_ci{
1778c2ecf20Sopenharmony_ci	return container_of(sd, struct ov2680_dev, sd);
1788c2ecf20Sopenharmony_ci}
1798c2ecf20Sopenharmony_ci
1808c2ecf20Sopenharmony_cistatic struct device *ov2680_to_dev(struct ov2680_dev *sensor)
1818c2ecf20Sopenharmony_ci{
1828c2ecf20Sopenharmony_ci	return &sensor->i2c_client->dev;
1838c2ecf20Sopenharmony_ci}
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_cistatic inline struct v4l2_subdev *ctrl_to_sd(struct v4l2_ctrl *ctrl)
1868c2ecf20Sopenharmony_ci{
1878c2ecf20Sopenharmony_ci	return &container_of(ctrl->handler, struct ov2680_dev,
1888c2ecf20Sopenharmony_ci			     ctrls.handler)->sd;
1898c2ecf20Sopenharmony_ci}
1908c2ecf20Sopenharmony_ci
1918c2ecf20Sopenharmony_cistatic int __ov2680_write_reg(struct ov2680_dev *sensor, u16 reg,
1928c2ecf20Sopenharmony_ci			      unsigned int len, u32 val)
1938c2ecf20Sopenharmony_ci{
1948c2ecf20Sopenharmony_ci	struct i2c_client *client = sensor->i2c_client;
1958c2ecf20Sopenharmony_ci	u8 buf[6];
1968c2ecf20Sopenharmony_ci	int ret;
1978c2ecf20Sopenharmony_ci
1988c2ecf20Sopenharmony_ci	if (len > 4)
1998c2ecf20Sopenharmony_ci		return -EINVAL;
2008c2ecf20Sopenharmony_ci
2018c2ecf20Sopenharmony_ci	put_unaligned_be16(reg, buf);
2028c2ecf20Sopenharmony_ci	put_unaligned_be32(val << (8 * (4 - len)), buf + 2);
2038c2ecf20Sopenharmony_ci	ret = i2c_master_send(client, buf, len + 2);
2048c2ecf20Sopenharmony_ci	if (ret != len + 2) {
2058c2ecf20Sopenharmony_ci		dev_err(&client->dev, "write error: reg=0x%4x: %d\n", reg, ret);
2068c2ecf20Sopenharmony_ci		return -EIO;
2078c2ecf20Sopenharmony_ci	}
2088c2ecf20Sopenharmony_ci
2098c2ecf20Sopenharmony_ci	return 0;
2108c2ecf20Sopenharmony_ci}
2118c2ecf20Sopenharmony_ci
2128c2ecf20Sopenharmony_ci#define ov2680_write_reg(s, r, v) \
2138c2ecf20Sopenharmony_ci	__ov2680_write_reg(s, r, OV2680_REG_VALUE_8BIT, v)
2148c2ecf20Sopenharmony_ci
2158c2ecf20Sopenharmony_ci#define ov2680_write_reg16(s, r, v) \
2168c2ecf20Sopenharmony_ci	__ov2680_write_reg(s, r, OV2680_REG_VALUE_16BIT, v)
2178c2ecf20Sopenharmony_ci
2188c2ecf20Sopenharmony_ci#define ov2680_write_reg24(s, r, v) \
2198c2ecf20Sopenharmony_ci	__ov2680_write_reg(s, r, OV2680_REG_VALUE_24BIT, v)
2208c2ecf20Sopenharmony_ci
2218c2ecf20Sopenharmony_cistatic int __ov2680_read_reg(struct ov2680_dev *sensor, u16 reg,
2228c2ecf20Sopenharmony_ci			     unsigned int len, u32 *val)
2238c2ecf20Sopenharmony_ci{
2248c2ecf20Sopenharmony_ci	struct i2c_client *client = sensor->i2c_client;
2258c2ecf20Sopenharmony_ci	struct i2c_msg msgs[2];
2268c2ecf20Sopenharmony_ci	u8 addr_buf[2] = { reg >> 8, reg & 0xff };
2278c2ecf20Sopenharmony_ci	u8 data_buf[4] = { 0, };
2288c2ecf20Sopenharmony_ci	int ret;
2298c2ecf20Sopenharmony_ci
2308c2ecf20Sopenharmony_ci	if (len > 4)
2318c2ecf20Sopenharmony_ci		return -EINVAL;
2328c2ecf20Sopenharmony_ci
2338c2ecf20Sopenharmony_ci	msgs[0].addr = client->addr;
2348c2ecf20Sopenharmony_ci	msgs[0].flags = 0;
2358c2ecf20Sopenharmony_ci	msgs[0].len = ARRAY_SIZE(addr_buf);
2368c2ecf20Sopenharmony_ci	msgs[0].buf = addr_buf;
2378c2ecf20Sopenharmony_ci
2388c2ecf20Sopenharmony_ci	msgs[1].addr = client->addr;
2398c2ecf20Sopenharmony_ci	msgs[1].flags = I2C_M_RD;
2408c2ecf20Sopenharmony_ci	msgs[1].len = len;
2418c2ecf20Sopenharmony_ci	msgs[1].buf = &data_buf[4 - len];
2428c2ecf20Sopenharmony_ci
2438c2ecf20Sopenharmony_ci	ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
2448c2ecf20Sopenharmony_ci	if (ret != ARRAY_SIZE(msgs)) {
2458c2ecf20Sopenharmony_ci		dev_err(&client->dev, "read error: reg=0x%4x: %d\n", reg, ret);
2468c2ecf20Sopenharmony_ci		return -EIO;
2478c2ecf20Sopenharmony_ci	}
2488c2ecf20Sopenharmony_ci
2498c2ecf20Sopenharmony_ci	*val = get_unaligned_be32(data_buf);
2508c2ecf20Sopenharmony_ci
2518c2ecf20Sopenharmony_ci	return 0;
2528c2ecf20Sopenharmony_ci}
2538c2ecf20Sopenharmony_ci
2548c2ecf20Sopenharmony_ci#define ov2680_read_reg(s, r, v) \
2558c2ecf20Sopenharmony_ci	__ov2680_read_reg(s, r, OV2680_REG_VALUE_8BIT, v)
2568c2ecf20Sopenharmony_ci
2578c2ecf20Sopenharmony_ci#define ov2680_read_reg16(s, r, v) \
2588c2ecf20Sopenharmony_ci	__ov2680_read_reg(s, r, OV2680_REG_VALUE_16BIT, v)
2598c2ecf20Sopenharmony_ci
2608c2ecf20Sopenharmony_ci#define ov2680_read_reg24(s, r, v) \
2618c2ecf20Sopenharmony_ci	__ov2680_read_reg(s, r, OV2680_REG_VALUE_24BIT, v)
2628c2ecf20Sopenharmony_ci
2638c2ecf20Sopenharmony_cistatic int ov2680_mod_reg(struct ov2680_dev *sensor, u16 reg, u8 mask, u8 val)
2648c2ecf20Sopenharmony_ci{
2658c2ecf20Sopenharmony_ci	u32 readval;
2668c2ecf20Sopenharmony_ci	int ret;
2678c2ecf20Sopenharmony_ci
2688c2ecf20Sopenharmony_ci	ret = ov2680_read_reg(sensor, reg, &readval);
2698c2ecf20Sopenharmony_ci	if (ret < 0)
2708c2ecf20Sopenharmony_ci		return ret;
2718c2ecf20Sopenharmony_ci
2728c2ecf20Sopenharmony_ci	readval &= ~mask;
2738c2ecf20Sopenharmony_ci	val &= mask;
2748c2ecf20Sopenharmony_ci	val |= readval;
2758c2ecf20Sopenharmony_ci
2768c2ecf20Sopenharmony_ci	return ov2680_write_reg(sensor, reg, val);
2778c2ecf20Sopenharmony_ci}
2788c2ecf20Sopenharmony_ci
2798c2ecf20Sopenharmony_cistatic int ov2680_load_regs(struct ov2680_dev *sensor,
2808c2ecf20Sopenharmony_ci			    const struct ov2680_mode_info *mode)
2818c2ecf20Sopenharmony_ci{
2828c2ecf20Sopenharmony_ci	const struct reg_value *regs = mode->reg_data;
2838c2ecf20Sopenharmony_ci	unsigned int i;
2848c2ecf20Sopenharmony_ci	int ret = 0;
2858c2ecf20Sopenharmony_ci	u16 reg_addr;
2868c2ecf20Sopenharmony_ci	u8 val;
2878c2ecf20Sopenharmony_ci
2888c2ecf20Sopenharmony_ci	for (i = 0; i < mode->reg_data_size; ++i, ++regs) {
2898c2ecf20Sopenharmony_ci		reg_addr = regs->reg_addr;
2908c2ecf20Sopenharmony_ci		val = regs->val;
2918c2ecf20Sopenharmony_ci
2928c2ecf20Sopenharmony_ci		ret = ov2680_write_reg(sensor, reg_addr, val);
2938c2ecf20Sopenharmony_ci		if (ret)
2948c2ecf20Sopenharmony_ci			break;
2958c2ecf20Sopenharmony_ci	}
2968c2ecf20Sopenharmony_ci
2978c2ecf20Sopenharmony_ci	return ret;
2988c2ecf20Sopenharmony_ci}
2998c2ecf20Sopenharmony_ci
3008c2ecf20Sopenharmony_cistatic void ov2680_power_up(struct ov2680_dev *sensor)
3018c2ecf20Sopenharmony_ci{
3028c2ecf20Sopenharmony_ci	if (!sensor->reset_gpio)
3038c2ecf20Sopenharmony_ci		return;
3048c2ecf20Sopenharmony_ci
3058c2ecf20Sopenharmony_ci	gpiod_set_value(sensor->reset_gpio, 0);
3068c2ecf20Sopenharmony_ci	usleep_range(5000, 10000);
3078c2ecf20Sopenharmony_ci}
3088c2ecf20Sopenharmony_ci
3098c2ecf20Sopenharmony_cistatic void ov2680_power_down(struct ov2680_dev *sensor)
3108c2ecf20Sopenharmony_ci{
3118c2ecf20Sopenharmony_ci	if (!sensor->reset_gpio)
3128c2ecf20Sopenharmony_ci		return;
3138c2ecf20Sopenharmony_ci
3148c2ecf20Sopenharmony_ci	gpiod_set_value(sensor->reset_gpio, 1);
3158c2ecf20Sopenharmony_ci	usleep_range(5000, 10000);
3168c2ecf20Sopenharmony_ci}
3178c2ecf20Sopenharmony_ci
3188c2ecf20Sopenharmony_cistatic void ov2680_set_bayer_order(struct ov2680_dev *sensor)
3198c2ecf20Sopenharmony_ci{
3208c2ecf20Sopenharmony_ci	int hv_flip = 0;
3218c2ecf20Sopenharmony_ci
3228c2ecf20Sopenharmony_ci	if (sensor->ctrls.vflip && sensor->ctrls.vflip->val)
3238c2ecf20Sopenharmony_ci		hv_flip += 1;
3248c2ecf20Sopenharmony_ci
3258c2ecf20Sopenharmony_ci	if (sensor->ctrls.hflip && sensor->ctrls.hflip->val)
3268c2ecf20Sopenharmony_ci		hv_flip += 2;
3278c2ecf20Sopenharmony_ci
3288c2ecf20Sopenharmony_ci	sensor->fmt.code = ov2680_hv_flip_bayer_order[hv_flip];
3298c2ecf20Sopenharmony_ci}
3308c2ecf20Sopenharmony_ci
3318c2ecf20Sopenharmony_cistatic int ov2680_set_vflip(struct ov2680_dev *sensor, s32 val)
3328c2ecf20Sopenharmony_ci{
3338c2ecf20Sopenharmony_ci	int ret;
3348c2ecf20Sopenharmony_ci
3358c2ecf20Sopenharmony_ci	if (sensor->is_streaming)
3368c2ecf20Sopenharmony_ci		return -EBUSY;
3378c2ecf20Sopenharmony_ci
3388c2ecf20Sopenharmony_ci	ret = ov2680_mod_reg(sensor, OV2680_REG_FORMAT1,
3398c2ecf20Sopenharmony_ci			     BIT(2), val ? BIT(2) : 0);
3408c2ecf20Sopenharmony_ci	if (ret < 0)
3418c2ecf20Sopenharmony_ci		return ret;
3428c2ecf20Sopenharmony_ci
3438c2ecf20Sopenharmony_ci	ov2680_set_bayer_order(sensor);
3448c2ecf20Sopenharmony_ci	return 0;
3458c2ecf20Sopenharmony_ci}
3468c2ecf20Sopenharmony_ci
3478c2ecf20Sopenharmony_cistatic int ov2680_set_hflip(struct ov2680_dev *sensor, s32 val)
3488c2ecf20Sopenharmony_ci{
3498c2ecf20Sopenharmony_ci	int ret;
3508c2ecf20Sopenharmony_ci
3518c2ecf20Sopenharmony_ci	if (sensor->is_streaming)
3528c2ecf20Sopenharmony_ci		return -EBUSY;
3538c2ecf20Sopenharmony_ci
3548c2ecf20Sopenharmony_ci	ret = ov2680_mod_reg(sensor, OV2680_REG_FORMAT2,
3558c2ecf20Sopenharmony_ci			     BIT(2), val ? BIT(2) : 0);
3568c2ecf20Sopenharmony_ci	if (ret < 0)
3578c2ecf20Sopenharmony_ci		return ret;
3588c2ecf20Sopenharmony_ci
3598c2ecf20Sopenharmony_ci	ov2680_set_bayer_order(sensor);
3608c2ecf20Sopenharmony_ci	return 0;
3618c2ecf20Sopenharmony_ci}
3628c2ecf20Sopenharmony_ci
3638c2ecf20Sopenharmony_cistatic int ov2680_test_pattern_set(struct ov2680_dev *sensor, int value)
3648c2ecf20Sopenharmony_ci{
3658c2ecf20Sopenharmony_ci	int ret;
3668c2ecf20Sopenharmony_ci
3678c2ecf20Sopenharmony_ci	if (!value)
3688c2ecf20Sopenharmony_ci		return ov2680_mod_reg(sensor, OV2680_REG_ISP_CTRL00, BIT(7), 0);
3698c2ecf20Sopenharmony_ci
3708c2ecf20Sopenharmony_ci	ret = ov2680_mod_reg(sensor, OV2680_REG_ISP_CTRL00, 0x03, value - 1);
3718c2ecf20Sopenharmony_ci	if (ret < 0)
3728c2ecf20Sopenharmony_ci		return ret;
3738c2ecf20Sopenharmony_ci
3748c2ecf20Sopenharmony_ci	ret = ov2680_mod_reg(sensor, OV2680_REG_ISP_CTRL00, BIT(7), BIT(7));
3758c2ecf20Sopenharmony_ci	if (ret < 0)
3768c2ecf20Sopenharmony_ci		return ret;
3778c2ecf20Sopenharmony_ci
3788c2ecf20Sopenharmony_ci	return 0;
3798c2ecf20Sopenharmony_ci}
3808c2ecf20Sopenharmony_ci
3818c2ecf20Sopenharmony_cistatic int ov2680_gain_set(struct ov2680_dev *sensor, u32 gain)
3828c2ecf20Sopenharmony_ci{
3838c2ecf20Sopenharmony_ci	return ov2680_write_reg16(sensor, OV2680_REG_GAIN_PK, gain);
3848c2ecf20Sopenharmony_ci}
3858c2ecf20Sopenharmony_ci
3868c2ecf20Sopenharmony_cistatic int ov2680_exposure_set(struct ov2680_dev *sensor, u32 exp)
3878c2ecf20Sopenharmony_ci{
3888c2ecf20Sopenharmony_ci	return ov2680_write_reg24(sensor, OV2680_REG_EXPOSURE_PK_HIGH,
3898c2ecf20Sopenharmony_ci				  exp << 4);
3908c2ecf20Sopenharmony_ci}
3918c2ecf20Sopenharmony_ci
3928c2ecf20Sopenharmony_cistatic int ov2680_stream_enable(struct ov2680_dev *sensor)
3938c2ecf20Sopenharmony_ci{
3948c2ecf20Sopenharmony_ci	return ov2680_write_reg(sensor, OV2680_REG_STREAM_CTRL, 1);
3958c2ecf20Sopenharmony_ci}
3968c2ecf20Sopenharmony_ci
3978c2ecf20Sopenharmony_cistatic int ov2680_stream_disable(struct ov2680_dev *sensor)
3988c2ecf20Sopenharmony_ci{
3998c2ecf20Sopenharmony_ci	return ov2680_write_reg(sensor, OV2680_REG_STREAM_CTRL, 0);
4008c2ecf20Sopenharmony_ci}
4018c2ecf20Sopenharmony_ci
4028c2ecf20Sopenharmony_cistatic int ov2680_mode_set(struct ov2680_dev *sensor)
4038c2ecf20Sopenharmony_ci{
4048c2ecf20Sopenharmony_ci	int ret;
4058c2ecf20Sopenharmony_ci
4068c2ecf20Sopenharmony_ci	ret = ov2680_load_regs(sensor, sensor->current_mode);
4078c2ecf20Sopenharmony_ci	if (ret < 0)
4088c2ecf20Sopenharmony_ci		return ret;
4098c2ecf20Sopenharmony_ci
4108c2ecf20Sopenharmony_ci	/* Restore value of all ctrls */
4118c2ecf20Sopenharmony_ci	ret = __v4l2_ctrl_handler_setup(&sensor->ctrls.handler);
4128c2ecf20Sopenharmony_ci	if (ret < 0)
4138c2ecf20Sopenharmony_ci		return ret;
4148c2ecf20Sopenharmony_ci
4158c2ecf20Sopenharmony_ci	sensor->mode_pending_changes = false;
4168c2ecf20Sopenharmony_ci
4178c2ecf20Sopenharmony_ci	return 0;
4188c2ecf20Sopenharmony_ci}
4198c2ecf20Sopenharmony_ci
4208c2ecf20Sopenharmony_cistatic int ov2680_mode_restore(struct ov2680_dev *sensor)
4218c2ecf20Sopenharmony_ci{
4228c2ecf20Sopenharmony_ci	int ret;
4238c2ecf20Sopenharmony_ci
4248c2ecf20Sopenharmony_ci	ret = ov2680_load_regs(sensor, &ov2680_mode_init_data);
4258c2ecf20Sopenharmony_ci	if (ret < 0)
4268c2ecf20Sopenharmony_ci		return ret;
4278c2ecf20Sopenharmony_ci
4288c2ecf20Sopenharmony_ci	return ov2680_mode_set(sensor);
4298c2ecf20Sopenharmony_ci}
4308c2ecf20Sopenharmony_ci
4318c2ecf20Sopenharmony_cistatic int ov2680_power_off(struct ov2680_dev *sensor)
4328c2ecf20Sopenharmony_ci{
4338c2ecf20Sopenharmony_ci	if (!sensor->is_enabled)
4348c2ecf20Sopenharmony_ci		return 0;
4358c2ecf20Sopenharmony_ci
4368c2ecf20Sopenharmony_ci	clk_disable_unprepare(sensor->xvclk);
4378c2ecf20Sopenharmony_ci	ov2680_power_down(sensor);
4388c2ecf20Sopenharmony_ci	regulator_bulk_disable(OV2680_NUM_SUPPLIES, sensor->supplies);
4398c2ecf20Sopenharmony_ci	sensor->is_enabled = false;
4408c2ecf20Sopenharmony_ci
4418c2ecf20Sopenharmony_ci	return 0;
4428c2ecf20Sopenharmony_ci}
4438c2ecf20Sopenharmony_ci
4448c2ecf20Sopenharmony_cistatic int ov2680_power_on(struct ov2680_dev *sensor)
4458c2ecf20Sopenharmony_ci{
4468c2ecf20Sopenharmony_ci	struct device *dev = ov2680_to_dev(sensor);
4478c2ecf20Sopenharmony_ci	int ret;
4488c2ecf20Sopenharmony_ci
4498c2ecf20Sopenharmony_ci	if (sensor->is_enabled)
4508c2ecf20Sopenharmony_ci		return 0;
4518c2ecf20Sopenharmony_ci
4528c2ecf20Sopenharmony_ci	ret = regulator_bulk_enable(OV2680_NUM_SUPPLIES, sensor->supplies);
4538c2ecf20Sopenharmony_ci	if (ret < 0) {
4548c2ecf20Sopenharmony_ci		dev_err(dev, "failed to enable regulators: %d\n", ret);
4558c2ecf20Sopenharmony_ci		return ret;
4568c2ecf20Sopenharmony_ci	}
4578c2ecf20Sopenharmony_ci
4588c2ecf20Sopenharmony_ci	if (!sensor->reset_gpio) {
4598c2ecf20Sopenharmony_ci		ret = ov2680_write_reg(sensor, OV2680_REG_SOFT_RESET, 0x01);
4608c2ecf20Sopenharmony_ci		if (ret != 0) {
4618c2ecf20Sopenharmony_ci			dev_err(dev, "sensor soft reset failed\n");
4628c2ecf20Sopenharmony_ci			goto err_disable_regulators;
4638c2ecf20Sopenharmony_ci		}
4648c2ecf20Sopenharmony_ci		usleep_range(1000, 2000);
4658c2ecf20Sopenharmony_ci	} else {
4668c2ecf20Sopenharmony_ci		ov2680_power_down(sensor);
4678c2ecf20Sopenharmony_ci		ov2680_power_up(sensor);
4688c2ecf20Sopenharmony_ci	}
4698c2ecf20Sopenharmony_ci
4708c2ecf20Sopenharmony_ci	ret = clk_prepare_enable(sensor->xvclk);
4718c2ecf20Sopenharmony_ci	if (ret < 0)
4728c2ecf20Sopenharmony_ci		goto err_disable_regulators;
4738c2ecf20Sopenharmony_ci
4748c2ecf20Sopenharmony_ci	sensor->is_enabled = true;
4758c2ecf20Sopenharmony_ci
4768c2ecf20Sopenharmony_ci	/* Set clock lane into LP-11 state */
4778c2ecf20Sopenharmony_ci	ov2680_stream_enable(sensor);
4788c2ecf20Sopenharmony_ci	usleep_range(1000, 2000);
4798c2ecf20Sopenharmony_ci	ov2680_stream_disable(sensor);
4808c2ecf20Sopenharmony_ci
4818c2ecf20Sopenharmony_ci	return 0;
4828c2ecf20Sopenharmony_ci
4838c2ecf20Sopenharmony_cierr_disable_regulators:
4848c2ecf20Sopenharmony_ci	regulator_bulk_disable(OV2680_NUM_SUPPLIES, sensor->supplies);
4858c2ecf20Sopenharmony_ci	return ret;
4868c2ecf20Sopenharmony_ci}
4878c2ecf20Sopenharmony_ci
4888c2ecf20Sopenharmony_cistatic int ov2680_s_power(struct v4l2_subdev *sd, int on)
4898c2ecf20Sopenharmony_ci{
4908c2ecf20Sopenharmony_ci	struct ov2680_dev *sensor = to_ov2680_dev(sd);
4918c2ecf20Sopenharmony_ci	int ret = 0;
4928c2ecf20Sopenharmony_ci
4938c2ecf20Sopenharmony_ci	mutex_lock(&sensor->lock);
4948c2ecf20Sopenharmony_ci
4958c2ecf20Sopenharmony_ci	if (on)
4968c2ecf20Sopenharmony_ci		ret = ov2680_power_on(sensor);
4978c2ecf20Sopenharmony_ci	else
4988c2ecf20Sopenharmony_ci		ret = ov2680_power_off(sensor);
4998c2ecf20Sopenharmony_ci
5008c2ecf20Sopenharmony_ci	if (on && ret == 0)
5018c2ecf20Sopenharmony_ci		ret = ov2680_mode_restore(sensor);
5028c2ecf20Sopenharmony_ci
5038c2ecf20Sopenharmony_ci	mutex_unlock(&sensor->lock);
5048c2ecf20Sopenharmony_ci
5058c2ecf20Sopenharmony_ci	return ret;
5068c2ecf20Sopenharmony_ci}
5078c2ecf20Sopenharmony_ci
5088c2ecf20Sopenharmony_cistatic int ov2680_s_g_frame_interval(struct v4l2_subdev *sd,
5098c2ecf20Sopenharmony_ci				     struct v4l2_subdev_frame_interval *fi)
5108c2ecf20Sopenharmony_ci{
5118c2ecf20Sopenharmony_ci	struct ov2680_dev *sensor = to_ov2680_dev(sd);
5128c2ecf20Sopenharmony_ci
5138c2ecf20Sopenharmony_ci	mutex_lock(&sensor->lock);
5148c2ecf20Sopenharmony_ci	fi->interval = sensor->frame_interval;
5158c2ecf20Sopenharmony_ci	mutex_unlock(&sensor->lock);
5168c2ecf20Sopenharmony_ci
5178c2ecf20Sopenharmony_ci	return 0;
5188c2ecf20Sopenharmony_ci}
5198c2ecf20Sopenharmony_ci
5208c2ecf20Sopenharmony_cistatic int ov2680_s_stream(struct v4l2_subdev *sd, int enable)
5218c2ecf20Sopenharmony_ci{
5228c2ecf20Sopenharmony_ci	struct ov2680_dev *sensor = to_ov2680_dev(sd);
5238c2ecf20Sopenharmony_ci	int ret = 0;
5248c2ecf20Sopenharmony_ci
5258c2ecf20Sopenharmony_ci	mutex_lock(&sensor->lock);
5268c2ecf20Sopenharmony_ci
5278c2ecf20Sopenharmony_ci	if (sensor->is_streaming == !!enable)
5288c2ecf20Sopenharmony_ci		goto unlock;
5298c2ecf20Sopenharmony_ci
5308c2ecf20Sopenharmony_ci	if (enable && sensor->mode_pending_changes) {
5318c2ecf20Sopenharmony_ci		ret = ov2680_mode_set(sensor);
5328c2ecf20Sopenharmony_ci		if (ret < 0)
5338c2ecf20Sopenharmony_ci			goto unlock;
5348c2ecf20Sopenharmony_ci	}
5358c2ecf20Sopenharmony_ci
5368c2ecf20Sopenharmony_ci	if (enable)
5378c2ecf20Sopenharmony_ci		ret = ov2680_stream_enable(sensor);
5388c2ecf20Sopenharmony_ci	else
5398c2ecf20Sopenharmony_ci		ret = ov2680_stream_disable(sensor);
5408c2ecf20Sopenharmony_ci
5418c2ecf20Sopenharmony_ci	sensor->is_streaming = !!enable;
5428c2ecf20Sopenharmony_ci
5438c2ecf20Sopenharmony_ciunlock:
5448c2ecf20Sopenharmony_ci	mutex_unlock(&sensor->lock);
5458c2ecf20Sopenharmony_ci
5468c2ecf20Sopenharmony_ci	return ret;
5478c2ecf20Sopenharmony_ci}
5488c2ecf20Sopenharmony_ci
5498c2ecf20Sopenharmony_cistatic int ov2680_enum_mbus_code(struct v4l2_subdev *sd,
5508c2ecf20Sopenharmony_ci				 struct v4l2_subdev_pad_config *cfg,
5518c2ecf20Sopenharmony_ci				 struct v4l2_subdev_mbus_code_enum *code)
5528c2ecf20Sopenharmony_ci{
5538c2ecf20Sopenharmony_ci	struct ov2680_dev *sensor = to_ov2680_dev(sd);
5548c2ecf20Sopenharmony_ci
5558c2ecf20Sopenharmony_ci	if (code->pad != 0 || code->index != 0)
5568c2ecf20Sopenharmony_ci		return -EINVAL;
5578c2ecf20Sopenharmony_ci
5588c2ecf20Sopenharmony_ci	code->code = sensor->fmt.code;
5598c2ecf20Sopenharmony_ci
5608c2ecf20Sopenharmony_ci	return 0;
5618c2ecf20Sopenharmony_ci}
5628c2ecf20Sopenharmony_ci
5638c2ecf20Sopenharmony_cistatic int ov2680_get_fmt(struct v4l2_subdev *sd,
5648c2ecf20Sopenharmony_ci			  struct v4l2_subdev_pad_config *cfg,
5658c2ecf20Sopenharmony_ci			  struct v4l2_subdev_format *format)
5668c2ecf20Sopenharmony_ci{
5678c2ecf20Sopenharmony_ci	struct ov2680_dev *sensor = to_ov2680_dev(sd);
5688c2ecf20Sopenharmony_ci	struct v4l2_mbus_framefmt *fmt = NULL;
5698c2ecf20Sopenharmony_ci	int ret = 0;
5708c2ecf20Sopenharmony_ci
5718c2ecf20Sopenharmony_ci	if (format->pad != 0)
5728c2ecf20Sopenharmony_ci		return -EINVAL;
5738c2ecf20Sopenharmony_ci
5748c2ecf20Sopenharmony_ci	mutex_lock(&sensor->lock);
5758c2ecf20Sopenharmony_ci
5768c2ecf20Sopenharmony_ci	if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
5778c2ecf20Sopenharmony_ci#ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
5788c2ecf20Sopenharmony_ci		fmt = v4l2_subdev_get_try_format(&sensor->sd, cfg, format->pad);
5798c2ecf20Sopenharmony_ci#else
5808c2ecf20Sopenharmony_ci		ret = -EINVAL;
5818c2ecf20Sopenharmony_ci#endif
5828c2ecf20Sopenharmony_ci	} else {
5838c2ecf20Sopenharmony_ci		fmt = &sensor->fmt;
5848c2ecf20Sopenharmony_ci	}
5858c2ecf20Sopenharmony_ci
5868c2ecf20Sopenharmony_ci	if (fmt)
5878c2ecf20Sopenharmony_ci		format->format = *fmt;
5888c2ecf20Sopenharmony_ci
5898c2ecf20Sopenharmony_ci	mutex_unlock(&sensor->lock);
5908c2ecf20Sopenharmony_ci
5918c2ecf20Sopenharmony_ci	return ret;
5928c2ecf20Sopenharmony_ci}
5938c2ecf20Sopenharmony_ci
5948c2ecf20Sopenharmony_cistatic int ov2680_set_fmt(struct v4l2_subdev *sd,
5958c2ecf20Sopenharmony_ci			  struct v4l2_subdev_pad_config *cfg,
5968c2ecf20Sopenharmony_ci			  struct v4l2_subdev_format *format)
5978c2ecf20Sopenharmony_ci{
5988c2ecf20Sopenharmony_ci	struct ov2680_dev *sensor = to_ov2680_dev(sd);
5998c2ecf20Sopenharmony_ci	struct v4l2_mbus_framefmt *fmt = &format->format;
6008c2ecf20Sopenharmony_ci#ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
6018c2ecf20Sopenharmony_ci	struct v4l2_mbus_framefmt *try_fmt;
6028c2ecf20Sopenharmony_ci#endif
6038c2ecf20Sopenharmony_ci	const struct ov2680_mode_info *mode;
6048c2ecf20Sopenharmony_ci	int ret = 0;
6058c2ecf20Sopenharmony_ci
6068c2ecf20Sopenharmony_ci	if (format->pad != 0)
6078c2ecf20Sopenharmony_ci		return -EINVAL;
6088c2ecf20Sopenharmony_ci
6098c2ecf20Sopenharmony_ci	mutex_lock(&sensor->lock);
6108c2ecf20Sopenharmony_ci
6118c2ecf20Sopenharmony_ci	if (sensor->is_streaming) {
6128c2ecf20Sopenharmony_ci		ret = -EBUSY;
6138c2ecf20Sopenharmony_ci		goto unlock;
6148c2ecf20Sopenharmony_ci	}
6158c2ecf20Sopenharmony_ci
6168c2ecf20Sopenharmony_ci	mode = v4l2_find_nearest_size(ov2680_mode_data,
6178c2ecf20Sopenharmony_ci				      ARRAY_SIZE(ov2680_mode_data), width,
6188c2ecf20Sopenharmony_ci				      height, fmt->width, fmt->height);
6198c2ecf20Sopenharmony_ci	if (!mode) {
6208c2ecf20Sopenharmony_ci		ret = -EINVAL;
6218c2ecf20Sopenharmony_ci		goto unlock;
6228c2ecf20Sopenharmony_ci	}
6238c2ecf20Sopenharmony_ci
6248c2ecf20Sopenharmony_ci	if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
6258c2ecf20Sopenharmony_ci#ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
6268c2ecf20Sopenharmony_ci		try_fmt = v4l2_subdev_get_try_format(sd, cfg, 0);
6278c2ecf20Sopenharmony_ci		format->format = *try_fmt;
6288c2ecf20Sopenharmony_ci#endif
6298c2ecf20Sopenharmony_ci		goto unlock;
6308c2ecf20Sopenharmony_ci	}
6318c2ecf20Sopenharmony_ci
6328c2ecf20Sopenharmony_ci	fmt->width = mode->width;
6338c2ecf20Sopenharmony_ci	fmt->height = mode->height;
6348c2ecf20Sopenharmony_ci	fmt->code = sensor->fmt.code;
6358c2ecf20Sopenharmony_ci	fmt->colorspace = sensor->fmt.colorspace;
6368c2ecf20Sopenharmony_ci
6378c2ecf20Sopenharmony_ci	sensor->current_mode = mode;
6388c2ecf20Sopenharmony_ci	sensor->fmt = format->format;
6398c2ecf20Sopenharmony_ci	sensor->mode_pending_changes = true;
6408c2ecf20Sopenharmony_ci
6418c2ecf20Sopenharmony_ciunlock:
6428c2ecf20Sopenharmony_ci	mutex_unlock(&sensor->lock);
6438c2ecf20Sopenharmony_ci
6448c2ecf20Sopenharmony_ci	return ret;
6458c2ecf20Sopenharmony_ci}
6468c2ecf20Sopenharmony_ci
6478c2ecf20Sopenharmony_cistatic int ov2680_init_cfg(struct v4l2_subdev *sd,
6488c2ecf20Sopenharmony_ci			   struct v4l2_subdev_pad_config *cfg)
6498c2ecf20Sopenharmony_ci{
6508c2ecf20Sopenharmony_ci	struct v4l2_subdev_format fmt = {
6518c2ecf20Sopenharmony_ci		.which = cfg ? V4L2_SUBDEV_FORMAT_TRY
6528c2ecf20Sopenharmony_ci				: V4L2_SUBDEV_FORMAT_ACTIVE,
6538c2ecf20Sopenharmony_ci		.format = {
6548c2ecf20Sopenharmony_ci			.width = 800,
6558c2ecf20Sopenharmony_ci			.height = 600,
6568c2ecf20Sopenharmony_ci		}
6578c2ecf20Sopenharmony_ci	};
6588c2ecf20Sopenharmony_ci
6598c2ecf20Sopenharmony_ci	return ov2680_set_fmt(sd, cfg, &fmt);
6608c2ecf20Sopenharmony_ci}
6618c2ecf20Sopenharmony_ci
6628c2ecf20Sopenharmony_cistatic int ov2680_enum_frame_size(struct v4l2_subdev *sd,
6638c2ecf20Sopenharmony_ci				  struct v4l2_subdev_pad_config *cfg,
6648c2ecf20Sopenharmony_ci				  struct v4l2_subdev_frame_size_enum *fse)
6658c2ecf20Sopenharmony_ci{
6668c2ecf20Sopenharmony_ci	int index = fse->index;
6678c2ecf20Sopenharmony_ci
6688c2ecf20Sopenharmony_ci	if (index >= OV2680_MODE_MAX || index < 0)
6698c2ecf20Sopenharmony_ci		return -EINVAL;
6708c2ecf20Sopenharmony_ci
6718c2ecf20Sopenharmony_ci	fse->min_width = ov2680_mode_data[index].width;
6728c2ecf20Sopenharmony_ci	fse->min_height = ov2680_mode_data[index].height;
6738c2ecf20Sopenharmony_ci	fse->max_width = ov2680_mode_data[index].width;
6748c2ecf20Sopenharmony_ci	fse->max_height = ov2680_mode_data[index].height;
6758c2ecf20Sopenharmony_ci
6768c2ecf20Sopenharmony_ci	return 0;
6778c2ecf20Sopenharmony_ci}
6788c2ecf20Sopenharmony_ci
6798c2ecf20Sopenharmony_cistatic int ov2680_enum_frame_interval(struct v4l2_subdev *sd,
6808c2ecf20Sopenharmony_ci			      struct v4l2_subdev_pad_config *cfg,
6818c2ecf20Sopenharmony_ci			      struct v4l2_subdev_frame_interval_enum *fie)
6828c2ecf20Sopenharmony_ci{
6838c2ecf20Sopenharmony_ci	struct v4l2_fract tpf;
6848c2ecf20Sopenharmony_ci
6858c2ecf20Sopenharmony_ci	if (fie->index >= OV2680_MODE_MAX || fie->width > OV2680_WIDTH_MAX ||
6868c2ecf20Sopenharmony_ci	    fie->height > OV2680_HEIGHT_MAX ||
6878c2ecf20Sopenharmony_ci	    fie->which > V4L2_SUBDEV_FORMAT_ACTIVE)
6888c2ecf20Sopenharmony_ci		return -EINVAL;
6898c2ecf20Sopenharmony_ci
6908c2ecf20Sopenharmony_ci	tpf.denominator = OV2680_FRAME_RATE;
6918c2ecf20Sopenharmony_ci	tpf.numerator = 1;
6928c2ecf20Sopenharmony_ci
6938c2ecf20Sopenharmony_ci	fie->interval = tpf;
6948c2ecf20Sopenharmony_ci
6958c2ecf20Sopenharmony_ci	return 0;
6968c2ecf20Sopenharmony_ci}
6978c2ecf20Sopenharmony_ci
6988c2ecf20Sopenharmony_cistatic int ov2680_s_ctrl(struct v4l2_ctrl *ctrl)
6998c2ecf20Sopenharmony_ci{
7008c2ecf20Sopenharmony_ci	struct v4l2_subdev *sd = ctrl_to_sd(ctrl);
7018c2ecf20Sopenharmony_ci	struct ov2680_dev *sensor = to_ov2680_dev(sd);
7028c2ecf20Sopenharmony_ci
7038c2ecf20Sopenharmony_ci	if (!sensor->is_enabled)
7048c2ecf20Sopenharmony_ci		return 0;
7058c2ecf20Sopenharmony_ci
7068c2ecf20Sopenharmony_ci	switch (ctrl->id) {
7078c2ecf20Sopenharmony_ci	case V4L2_CID_GAIN:
7088c2ecf20Sopenharmony_ci		return ov2680_gain_set(sensor, ctrl->val);
7098c2ecf20Sopenharmony_ci	case V4L2_CID_EXPOSURE:
7108c2ecf20Sopenharmony_ci		return ov2680_exposure_set(sensor, ctrl->val);
7118c2ecf20Sopenharmony_ci	case V4L2_CID_VFLIP:
7128c2ecf20Sopenharmony_ci		return ov2680_set_vflip(sensor, ctrl->val);
7138c2ecf20Sopenharmony_ci	case V4L2_CID_HFLIP:
7148c2ecf20Sopenharmony_ci		return ov2680_set_hflip(sensor, ctrl->val);
7158c2ecf20Sopenharmony_ci	case V4L2_CID_TEST_PATTERN:
7168c2ecf20Sopenharmony_ci		return ov2680_test_pattern_set(sensor, ctrl->val);
7178c2ecf20Sopenharmony_ci	default:
7188c2ecf20Sopenharmony_ci		break;
7198c2ecf20Sopenharmony_ci	}
7208c2ecf20Sopenharmony_ci
7218c2ecf20Sopenharmony_ci	return -EINVAL;
7228c2ecf20Sopenharmony_ci}
7238c2ecf20Sopenharmony_ci
7248c2ecf20Sopenharmony_cistatic const struct v4l2_ctrl_ops ov2680_ctrl_ops = {
7258c2ecf20Sopenharmony_ci	.s_ctrl = ov2680_s_ctrl,
7268c2ecf20Sopenharmony_ci};
7278c2ecf20Sopenharmony_ci
7288c2ecf20Sopenharmony_cistatic const struct v4l2_subdev_core_ops ov2680_core_ops = {
7298c2ecf20Sopenharmony_ci	.s_power = ov2680_s_power,
7308c2ecf20Sopenharmony_ci};
7318c2ecf20Sopenharmony_ci
7328c2ecf20Sopenharmony_cistatic const struct v4l2_subdev_video_ops ov2680_video_ops = {
7338c2ecf20Sopenharmony_ci	.g_frame_interval	= ov2680_s_g_frame_interval,
7348c2ecf20Sopenharmony_ci	.s_frame_interval	= ov2680_s_g_frame_interval,
7358c2ecf20Sopenharmony_ci	.s_stream		= ov2680_s_stream,
7368c2ecf20Sopenharmony_ci};
7378c2ecf20Sopenharmony_ci
7388c2ecf20Sopenharmony_cistatic const struct v4l2_subdev_pad_ops ov2680_pad_ops = {
7398c2ecf20Sopenharmony_ci	.init_cfg		= ov2680_init_cfg,
7408c2ecf20Sopenharmony_ci	.enum_mbus_code		= ov2680_enum_mbus_code,
7418c2ecf20Sopenharmony_ci	.get_fmt		= ov2680_get_fmt,
7428c2ecf20Sopenharmony_ci	.set_fmt		= ov2680_set_fmt,
7438c2ecf20Sopenharmony_ci	.enum_frame_size	= ov2680_enum_frame_size,
7448c2ecf20Sopenharmony_ci	.enum_frame_interval	= ov2680_enum_frame_interval,
7458c2ecf20Sopenharmony_ci};
7468c2ecf20Sopenharmony_ci
7478c2ecf20Sopenharmony_cistatic const struct v4l2_subdev_ops ov2680_subdev_ops = {
7488c2ecf20Sopenharmony_ci	.core	= &ov2680_core_ops,
7498c2ecf20Sopenharmony_ci	.video	= &ov2680_video_ops,
7508c2ecf20Sopenharmony_ci	.pad	= &ov2680_pad_ops,
7518c2ecf20Sopenharmony_ci};
7528c2ecf20Sopenharmony_ci
7538c2ecf20Sopenharmony_cistatic int ov2680_mode_init(struct ov2680_dev *sensor)
7548c2ecf20Sopenharmony_ci{
7558c2ecf20Sopenharmony_ci	const struct ov2680_mode_info *init_mode;
7568c2ecf20Sopenharmony_ci
7578c2ecf20Sopenharmony_ci	/* set initial mode */
7588c2ecf20Sopenharmony_ci	sensor->fmt.code = MEDIA_BUS_FMT_SBGGR10_1X10;
7598c2ecf20Sopenharmony_ci	sensor->fmt.width = 800;
7608c2ecf20Sopenharmony_ci	sensor->fmt.height = 600;
7618c2ecf20Sopenharmony_ci	sensor->fmt.field = V4L2_FIELD_NONE;
7628c2ecf20Sopenharmony_ci	sensor->fmt.colorspace = V4L2_COLORSPACE_SRGB;
7638c2ecf20Sopenharmony_ci
7648c2ecf20Sopenharmony_ci	sensor->frame_interval.denominator = OV2680_FRAME_RATE;
7658c2ecf20Sopenharmony_ci	sensor->frame_interval.numerator = 1;
7668c2ecf20Sopenharmony_ci
7678c2ecf20Sopenharmony_ci	init_mode = &ov2680_mode_init_data;
7688c2ecf20Sopenharmony_ci
7698c2ecf20Sopenharmony_ci	sensor->current_mode = init_mode;
7708c2ecf20Sopenharmony_ci
7718c2ecf20Sopenharmony_ci	sensor->mode_pending_changes = true;
7728c2ecf20Sopenharmony_ci
7738c2ecf20Sopenharmony_ci	return 0;
7748c2ecf20Sopenharmony_ci}
7758c2ecf20Sopenharmony_ci
7768c2ecf20Sopenharmony_cistatic int ov2680_v4l2_register(struct ov2680_dev *sensor)
7778c2ecf20Sopenharmony_ci{
7788c2ecf20Sopenharmony_ci	const struct v4l2_ctrl_ops *ops = &ov2680_ctrl_ops;
7798c2ecf20Sopenharmony_ci	struct ov2680_ctrls *ctrls = &sensor->ctrls;
7808c2ecf20Sopenharmony_ci	struct v4l2_ctrl_handler *hdl = &ctrls->handler;
7818c2ecf20Sopenharmony_ci	int ret = 0;
7828c2ecf20Sopenharmony_ci
7838c2ecf20Sopenharmony_ci	v4l2_i2c_subdev_init(&sensor->sd, sensor->i2c_client,
7848c2ecf20Sopenharmony_ci			     &ov2680_subdev_ops);
7858c2ecf20Sopenharmony_ci
7868c2ecf20Sopenharmony_ci#ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
7878c2ecf20Sopenharmony_ci	sensor->sd.flags = V4L2_SUBDEV_FL_HAS_DEVNODE;
7888c2ecf20Sopenharmony_ci#endif
7898c2ecf20Sopenharmony_ci	sensor->pad.flags = MEDIA_PAD_FL_SOURCE;
7908c2ecf20Sopenharmony_ci	sensor->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
7918c2ecf20Sopenharmony_ci
7928c2ecf20Sopenharmony_ci	ret = media_entity_pads_init(&sensor->sd.entity, 1, &sensor->pad);
7938c2ecf20Sopenharmony_ci	if (ret < 0)
7948c2ecf20Sopenharmony_ci		return ret;
7958c2ecf20Sopenharmony_ci
7968c2ecf20Sopenharmony_ci	v4l2_ctrl_handler_init(hdl, 5);
7978c2ecf20Sopenharmony_ci
7988c2ecf20Sopenharmony_ci	hdl->lock = &sensor->lock;
7998c2ecf20Sopenharmony_ci
8008c2ecf20Sopenharmony_ci	ctrls->vflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_VFLIP, 0, 1, 1, 0);
8018c2ecf20Sopenharmony_ci	ctrls->hflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_HFLIP, 0, 1, 1, 0);
8028c2ecf20Sopenharmony_ci
8038c2ecf20Sopenharmony_ci	ctrls->test_pattern = v4l2_ctrl_new_std_menu_items(hdl,
8048c2ecf20Sopenharmony_ci					&ov2680_ctrl_ops, V4L2_CID_TEST_PATTERN,
8058c2ecf20Sopenharmony_ci					ARRAY_SIZE(test_pattern_menu) - 1,
8068c2ecf20Sopenharmony_ci					0, 0, test_pattern_menu);
8078c2ecf20Sopenharmony_ci
8088c2ecf20Sopenharmony_ci	ctrls->exposure = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_EXPOSURE,
8098c2ecf20Sopenharmony_ci					    0, 32767, 1, 0);
8108c2ecf20Sopenharmony_ci
8118c2ecf20Sopenharmony_ci	ctrls->gain = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_GAIN, 0, 2047, 1, 0);
8128c2ecf20Sopenharmony_ci
8138c2ecf20Sopenharmony_ci	if (hdl->error) {
8148c2ecf20Sopenharmony_ci		ret = hdl->error;
8158c2ecf20Sopenharmony_ci		goto cleanup_entity;
8168c2ecf20Sopenharmony_ci	}
8178c2ecf20Sopenharmony_ci
8188c2ecf20Sopenharmony_ci	ctrls->vflip->flags |= V4L2_CTRL_FLAG_MODIFY_LAYOUT;
8198c2ecf20Sopenharmony_ci	ctrls->hflip->flags |= V4L2_CTRL_FLAG_MODIFY_LAYOUT;
8208c2ecf20Sopenharmony_ci
8218c2ecf20Sopenharmony_ci	sensor->sd.ctrl_handler = hdl;
8228c2ecf20Sopenharmony_ci
8238c2ecf20Sopenharmony_ci	ret = v4l2_async_register_subdev(&sensor->sd);
8248c2ecf20Sopenharmony_ci	if (ret < 0)
8258c2ecf20Sopenharmony_ci		goto cleanup_entity;
8268c2ecf20Sopenharmony_ci
8278c2ecf20Sopenharmony_ci	return 0;
8288c2ecf20Sopenharmony_ci
8298c2ecf20Sopenharmony_cicleanup_entity:
8308c2ecf20Sopenharmony_ci	media_entity_cleanup(&sensor->sd.entity);
8318c2ecf20Sopenharmony_ci	v4l2_ctrl_handler_free(hdl);
8328c2ecf20Sopenharmony_ci
8338c2ecf20Sopenharmony_ci	return ret;
8348c2ecf20Sopenharmony_ci}
8358c2ecf20Sopenharmony_ci
8368c2ecf20Sopenharmony_cistatic int ov2680_get_regulators(struct ov2680_dev *sensor)
8378c2ecf20Sopenharmony_ci{
8388c2ecf20Sopenharmony_ci	int i;
8398c2ecf20Sopenharmony_ci
8408c2ecf20Sopenharmony_ci	for (i = 0; i < OV2680_NUM_SUPPLIES; i++)
8418c2ecf20Sopenharmony_ci		sensor->supplies[i].supply = ov2680_supply_name[i];
8428c2ecf20Sopenharmony_ci
8438c2ecf20Sopenharmony_ci	return devm_regulator_bulk_get(&sensor->i2c_client->dev,
8448c2ecf20Sopenharmony_ci				       OV2680_NUM_SUPPLIES,
8458c2ecf20Sopenharmony_ci				       sensor->supplies);
8468c2ecf20Sopenharmony_ci}
8478c2ecf20Sopenharmony_ci
8488c2ecf20Sopenharmony_cistatic int ov2680_check_id(struct ov2680_dev *sensor)
8498c2ecf20Sopenharmony_ci{
8508c2ecf20Sopenharmony_ci	struct device *dev = ov2680_to_dev(sensor);
8518c2ecf20Sopenharmony_ci	u32 chip_id;
8528c2ecf20Sopenharmony_ci	int ret;
8538c2ecf20Sopenharmony_ci
8548c2ecf20Sopenharmony_ci	ov2680_power_on(sensor);
8558c2ecf20Sopenharmony_ci
8568c2ecf20Sopenharmony_ci	ret = ov2680_read_reg16(sensor, OV2680_REG_CHIP_ID_HIGH, &chip_id);
8578c2ecf20Sopenharmony_ci	if (ret < 0) {
8588c2ecf20Sopenharmony_ci		dev_err(dev, "failed to read chip id high\n");
8598c2ecf20Sopenharmony_ci		return -ENODEV;
8608c2ecf20Sopenharmony_ci	}
8618c2ecf20Sopenharmony_ci
8628c2ecf20Sopenharmony_ci	if (chip_id != OV2680_CHIP_ID) {
8638c2ecf20Sopenharmony_ci		dev_err(dev, "chip id: 0x%04x does not match expected 0x%04x\n",
8648c2ecf20Sopenharmony_ci			chip_id, OV2680_CHIP_ID);
8658c2ecf20Sopenharmony_ci		return -ENODEV;
8668c2ecf20Sopenharmony_ci	}
8678c2ecf20Sopenharmony_ci
8688c2ecf20Sopenharmony_ci	return 0;
8698c2ecf20Sopenharmony_ci}
8708c2ecf20Sopenharmony_ci
8718c2ecf20Sopenharmony_cistatic int ov2680_parse_dt(struct ov2680_dev *sensor)
8728c2ecf20Sopenharmony_ci{
8738c2ecf20Sopenharmony_ci	struct device *dev = ov2680_to_dev(sensor);
8748c2ecf20Sopenharmony_ci	int ret;
8758c2ecf20Sopenharmony_ci
8768c2ecf20Sopenharmony_ci	sensor->reset_gpio = devm_gpiod_get_optional(dev, "reset",
8778c2ecf20Sopenharmony_ci						     GPIOD_OUT_HIGH);
8788c2ecf20Sopenharmony_ci	ret = PTR_ERR_OR_ZERO(sensor->reset_gpio);
8798c2ecf20Sopenharmony_ci	if (ret < 0) {
8808c2ecf20Sopenharmony_ci		dev_dbg(dev, "error while getting reset gpio: %d\n", ret);
8818c2ecf20Sopenharmony_ci		return ret;
8828c2ecf20Sopenharmony_ci	}
8838c2ecf20Sopenharmony_ci
8848c2ecf20Sopenharmony_ci	sensor->xvclk = devm_clk_get(dev, "xvclk");
8858c2ecf20Sopenharmony_ci	if (IS_ERR(sensor->xvclk)) {
8868c2ecf20Sopenharmony_ci		dev_err(dev, "xvclk clock missing or invalid\n");
8878c2ecf20Sopenharmony_ci		return PTR_ERR(sensor->xvclk);
8888c2ecf20Sopenharmony_ci	}
8898c2ecf20Sopenharmony_ci
8908c2ecf20Sopenharmony_ci	sensor->xvclk_freq = clk_get_rate(sensor->xvclk);
8918c2ecf20Sopenharmony_ci	if (sensor->xvclk_freq != OV2680_XVCLK_VALUE) {
8928c2ecf20Sopenharmony_ci		dev_err(dev, "wrong xvclk frequency %d HZ, expected: %d Hz\n",
8938c2ecf20Sopenharmony_ci			sensor->xvclk_freq, OV2680_XVCLK_VALUE);
8948c2ecf20Sopenharmony_ci		return -EINVAL;
8958c2ecf20Sopenharmony_ci	}
8968c2ecf20Sopenharmony_ci
8978c2ecf20Sopenharmony_ci	return 0;
8988c2ecf20Sopenharmony_ci}
8998c2ecf20Sopenharmony_ci
9008c2ecf20Sopenharmony_cistatic int ov2680_probe(struct i2c_client *client)
9018c2ecf20Sopenharmony_ci{
9028c2ecf20Sopenharmony_ci	struct device *dev = &client->dev;
9038c2ecf20Sopenharmony_ci	struct ov2680_dev *sensor;
9048c2ecf20Sopenharmony_ci	int ret;
9058c2ecf20Sopenharmony_ci
9068c2ecf20Sopenharmony_ci	sensor = devm_kzalloc(dev, sizeof(*sensor), GFP_KERNEL);
9078c2ecf20Sopenharmony_ci	if (!sensor)
9088c2ecf20Sopenharmony_ci		return -ENOMEM;
9098c2ecf20Sopenharmony_ci
9108c2ecf20Sopenharmony_ci	sensor->i2c_client = client;
9118c2ecf20Sopenharmony_ci
9128c2ecf20Sopenharmony_ci	ret = ov2680_parse_dt(sensor);
9138c2ecf20Sopenharmony_ci	if (ret < 0)
9148c2ecf20Sopenharmony_ci		return -EINVAL;
9158c2ecf20Sopenharmony_ci
9168c2ecf20Sopenharmony_ci	ret = ov2680_mode_init(sensor);
9178c2ecf20Sopenharmony_ci	if (ret < 0)
9188c2ecf20Sopenharmony_ci		return ret;
9198c2ecf20Sopenharmony_ci
9208c2ecf20Sopenharmony_ci	ret = ov2680_get_regulators(sensor);
9218c2ecf20Sopenharmony_ci	if (ret < 0) {
9228c2ecf20Sopenharmony_ci		dev_err(dev, "failed to get regulators\n");
9238c2ecf20Sopenharmony_ci		return ret;
9248c2ecf20Sopenharmony_ci	}
9258c2ecf20Sopenharmony_ci
9268c2ecf20Sopenharmony_ci	mutex_init(&sensor->lock);
9278c2ecf20Sopenharmony_ci
9288c2ecf20Sopenharmony_ci	ret = ov2680_check_id(sensor);
9298c2ecf20Sopenharmony_ci	if (ret < 0)
9308c2ecf20Sopenharmony_ci		goto lock_destroy;
9318c2ecf20Sopenharmony_ci
9328c2ecf20Sopenharmony_ci	ret = ov2680_v4l2_register(sensor);
9338c2ecf20Sopenharmony_ci	if (ret < 0)
9348c2ecf20Sopenharmony_ci		goto lock_destroy;
9358c2ecf20Sopenharmony_ci
9368c2ecf20Sopenharmony_ci	dev_info(dev, "ov2680 init correctly\n");
9378c2ecf20Sopenharmony_ci
9388c2ecf20Sopenharmony_ci	return 0;
9398c2ecf20Sopenharmony_ci
9408c2ecf20Sopenharmony_cilock_destroy:
9418c2ecf20Sopenharmony_ci	dev_err(dev, "ov2680 init fail: %d\n", ret);
9428c2ecf20Sopenharmony_ci	mutex_destroy(&sensor->lock);
9438c2ecf20Sopenharmony_ci
9448c2ecf20Sopenharmony_ci	return ret;
9458c2ecf20Sopenharmony_ci}
9468c2ecf20Sopenharmony_ci
9478c2ecf20Sopenharmony_cistatic int ov2680_remove(struct i2c_client *client)
9488c2ecf20Sopenharmony_ci{
9498c2ecf20Sopenharmony_ci	struct v4l2_subdev *sd = i2c_get_clientdata(client);
9508c2ecf20Sopenharmony_ci	struct ov2680_dev *sensor = to_ov2680_dev(sd);
9518c2ecf20Sopenharmony_ci
9528c2ecf20Sopenharmony_ci	v4l2_async_unregister_subdev(&sensor->sd);
9538c2ecf20Sopenharmony_ci	mutex_destroy(&sensor->lock);
9548c2ecf20Sopenharmony_ci	media_entity_cleanup(&sensor->sd.entity);
9558c2ecf20Sopenharmony_ci	v4l2_ctrl_handler_free(&sensor->ctrls.handler);
9568c2ecf20Sopenharmony_ci
9578c2ecf20Sopenharmony_ci	return 0;
9588c2ecf20Sopenharmony_ci}
9598c2ecf20Sopenharmony_ci
9608c2ecf20Sopenharmony_cistatic int __maybe_unused ov2680_suspend(struct device *dev)
9618c2ecf20Sopenharmony_ci{
9628c2ecf20Sopenharmony_ci	struct i2c_client *client = to_i2c_client(dev);
9638c2ecf20Sopenharmony_ci	struct v4l2_subdev *sd = i2c_get_clientdata(client);
9648c2ecf20Sopenharmony_ci	struct ov2680_dev *sensor = to_ov2680_dev(sd);
9658c2ecf20Sopenharmony_ci
9668c2ecf20Sopenharmony_ci	if (sensor->is_streaming)
9678c2ecf20Sopenharmony_ci		ov2680_stream_disable(sensor);
9688c2ecf20Sopenharmony_ci
9698c2ecf20Sopenharmony_ci	return 0;
9708c2ecf20Sopenharmony_ci}
9718c2ecf20Sopenharmony_ci
9728c2ecf20Sopenharmony_cistatic int __maybe_unused ov2680_resume(struct device *dev)
9738c2ecf20Sopenharmony_ci{
9748c2ecf20Sopenharmony_ci	struct i2c_client *client = to_i2c_client(dev);
9758c2ecf20Sopenharmony_ci	struct v4l2_subdev *sd = i2c_get_clientdata(client);
9768c2ecf20Sopenharmony_ci	struct ov2680_dev *sensor = to_ov2680_dev(sd);
9778c2ecf20Sopenharmony_ci	int ret;
9788c2ecf20Sopenharmony_ci
9798c2ecf20Sopenharmony_ci	if (sensor->is_streaming) {
9808c2ecf20Sopenharmony_ci		ret = ov2680_stream_enable(sensor);
9818c2ecf20Sopenharmony_ci		if (ret < 0)
9828c2ecf20Sopenharmony_ci			goto stream_disable;
9838c2ecf20Sopenharmony_ci	}
9848c2ecf20Sopenharmony_ci
9858c2ecf20Sopenharmony_ci	return 0;
9868c2ecf20Sopenharmony_ci
9878c2ecf20Sopenharmony_cistream_disable:
9888c2ecf20Sopenharmony_ci	ov2680_stream_disable(sensor);
9898c2ecf20Sopenharmony_ci	sensor->is_streaming = false;
9908c2ecf20Sopenharmony_ci
9918c2ecf20Sopenharmony_ci	return ret;
9928c2ecf20Sopenharmony_ci}
9938c2ecf20Sopenharmony_ci
9948c2ecf20Sopenharmony_cistatic const struct dev_pm_ops ov2680_pm_ops = {
9958c2ecf20Sopenharmony_ci	SET_SYSTEM_SLEEP_PM_OPS(ov2680_suspend, ov2680_resume)
9968c2ecf20Sopenharmony_ci};
9978c2ecf20Sopenharmony_ci
9988c2ecf20Sopenharmony_cistatic const struct of_device_id ov2680_dt_ids[] = {
9998c2ecf20Sopenharmony_ci	{ .compatible = "ovti,ov2680" },
10008c2ecf20Sopenharmony_ci	{ /* sentinel */ },
10018c2ecf20Sopenharmony_ci};
10028c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, ov2680_dt_ids);
10038c2ecf20Sopenharmony_ci
10048c2ecf20Sopenharmony_cistatic struct i2c_driver ov2680_i2c_driver = {
10058c2ecf20Sopenharmony_ci	.driver = {
10068c2ecf20Sopenharmony_ci		.name  = "ov2680",
10078c2ecf20Sopenharmony_ci		.pm = &ov2680_pm_ops,
10088c2ecf20Sopenharmony_ci		.of_match_table	= of_match_ptr(ov2680_dt_ids),
10098c2ecf20Sopenharmony_ci	},
10108c2ecf20Sopenharmony_ci	.probe_new	= ov2680_probe,
10118c2ecf20Sopenharmony_ci	.remove		= ov2680_remove,
10128c2ecf20Sopenharmony_ci};
10138c2ecf20Sopenharmony_cimodule_i2c_driver(ov2680_i2c_driver);
10148c2ecf20Sopenharmony_ci
10158c2ecf20Sopenharmony_ciMODULE_AUTHOR("Rui Miguel Silva <rui.silva@linaro.org>");
10168c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("OV2680 CMOS Image Sensor driver");
10178c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2");
1018