// SPDX-License-Identifier: GPL-2.0
// Copyright (c) 2022 Intel Corporation.
#include <linux/acpi.h>
#include <linux/clk.h>
#include <linux/delay.h>
#include <linux/gpio/consumer.h>
#include <linux/i2c.h>
#include <linux/module.h>
#include <linux/pm_runtime.h>
#include <linux/regmap.h>
#include <media/v4l2-cci.h>
#include <media/v4l2-ctrls.h>
#include <media/v4l2-device.h>
#include <media/v4l2-fwnode.h>
#define OV02C10_LINK_FREQ_400MHZ 400000000ULL
#define OV02C10_MCLK 19200000
#define OV02C10_RGB_DEPTH 10
#define OV02C10_REG_CHIP_ID CCI_REG16(0x300a)
#define OV02C10_CHIP_ID 0x5602
#define OV02C10_REG_STREAM_CONTROL CCI_REG8(0x0100)
#define OV02C10_REG_HTS CCI_REG16(0x380c)
/* vertical-timings from sensor */
#define OV02C10_REG_VTS CCI_REG16(0x380e)
#define OV02C10_VTS_MAX 0xffff
/* Exposure controls from sensor */
#define OV02C10_REG_EXPOSURE CCI_REG16(0x3501)
#define OV02C10_EXPOSURE_MIN 4
#define OV02C10_EXPOSURE_MAX_MARGIN 8
#define OV02C10_EXPOSURE_STEP 1
/* Analog gain controls from sensor */
#define OV02C10_REG_ANALOG_GAIN CCI_REG16(0x3508)
#define OV02C10_ANAL_GAIN_MIN 0x10
#define OV02C10_ANAL_GAIN_MAX 0xf8
#define OV02C10_ANAL_GAIN_STEP 1
#define OV02C10_ANAL_GAIN_DEFAULT 0x10
/* Digital gain controls from sensor */
#define OV02C10_REG_DIGITAL_GAIN CCI_REG24(0x350a)
#define OV02C10_DGTL_GAIN_MIN 0x0400
#define OV02C10_DGTL_GAIN_MAX 0x3fff
#define OV02C10_DGTL_GAIN_STEP 1
#define OV02C10_DGTL_GAIN_DEFAULT 0x0400
/* Rotate */
#define OV02C10_ROTATE_CONTROL CCI_REG8(0x3820)
#define OV02C10_ISP_X_WIN_CONTROL CCI_REG16(0x3810)
#define OV02C10_ISP_Y_WIN_CONTROL CCI_REG16(0x3812)
#define OV02C10_CONFIG_ROTATE 0x18
/* Test Pattern Control */
#define OV02C10_REG_TEST_PATTERN CCI_REG8(0x4503)
#define OV02C10_TEST_PATTERN_ENABLE BIT(7)
struct ov02c10_mode {
/* Frame width in pixels */
u32 width;
/* Frame height in pixels */
u32 height;
/* Horizontal timining size */
u32 hts;
/* Min vertical timining size */
u32 vts_min;
/* Sensor register settings for this resolution */
const struct reg_sequence *reg_sequence;
const int sequence_length;
/* Sensor register settings for 1 or 2 lane config */
const struct reg_sequence *lane_settings[2];
const int lane_settings_length[2];
};
static const struct reg_sequence sensor_1928x1092_30fps_setting[] = {
{0x0301, 0x08},
{0x0303, 0x06},
{0x0304, 0x01},
{0x0305, 0xe0},
{0x0313, 0x40},
{0x031c, 0x4f},
{0x3020, 0x97},
{0x3022, 0x01},
{0x3026, 0xb4},
{0x303b, 0x00},
{0x303c, 0x4f},
{0x303d, 0xe6},
{0x303e, 0x00},
{0x303f, 0x03},
{0x3021, 0x23},
{0x3501, 0x04},
{0x3502, 0x6c},
{0x3504, 0x0c},
{0x3507, 0x00},
{0x3508, 0x08},
{0x3509, 0x00},
{0x350a, 0x01},
{0x350b, 0x00},
{0x350c, 0x41},
{0x3600, 0x84},
{0x3603, 0x08},
{0x3610, 0x57},
{0x3611, 0x1b},
{0x3613, 0x78},
{0x3623, 0x00},
{0x3632, 0xa0},
{0x3642, 0xe8},
{0x364c, 0x70},
{0x365f, 0x0f},
{0x3708, 0x30},
{0x3714, 0x24},
{0x3725, 0x02},
{0x3737, 0x08},
{0x3739, 0x28},
{0x3749, 0x32},
{0x374a, 0x32},
{0x374b, 0x32},
{0x374c, 0x32},
{0x374d, 0x81},
{0x374e, 0x81},
{0x374f, 0x81},
{0x3752, 0x36},
{0x3753, 0x36},
{0x3754, 0x36},
{0x3761, 0x00},
{0x376c, 0x81},
{0x3774, 0x18},
{0x3776, 0x08},
{0x377c, 0x81},
{0x377d, 0x81},
{0x377e, 0x81},
{0x37a0, 0x44},
{0x37a6, 0x44},
{0x37aa, 0x0d},
{0x37ae, 0x00},
{0x37cb, 0x03},
{0x37cc, 0x01},
{0x37d8, 0x02},
{0x37d9, 0x10},
{0x37e1, 0x10},
{0x37e2, 0x18},
{0x37e3, 0x08},
{0x37e4, 0x08},
{0x37e5, 0x02},
{0x37e6, 0x08},
/* 1928x1092 */
{0x3800, 0x00},
{0x3801, 0x00},
{0x3802, 0x00},
{0x3803, 0x00},
{0x3804, 0x07},
{0x3805, 0x8f},
{0x3806, 0x04},
{0x3807, 0x47},
{0x3808, 0x07},
{0x3809, 0x88},
{0x380a, 0x04},
{0x380b, 0x44},
{0x3814, 0x01},
{0x3815, 0x01},
{0x3816, 0x01},
{0x3817,<