// SPDX-License-Identifier: GPL-2.0
// Copyright (c) 2022 Intel Corporation.
#include <linux/unaligned.h>
#include <linux/acpi.h>
#include <linux/clk.h>
#include <linux/i2c.h>
#include <linux/gpio/consumer.h>
#include <linux/module.h>
#include <linux/delay.h>
#include <linux/pm_runtime.h>
#include <linux/regulator/consumer.h>
#include <media/v4l2-common.h>
#include <media/v4l2-ctrls.h>
#include <media/v4l2-device.h>
#include <media/v4l2-fwnode.h>
#define OV08X40_REG_VALUE_08BIT 1
#define OV08X40_REG_VALUE_16BIT 2
#define OV08X40_REG_VALUE_24BIT 3
#define OV08X40_REG_MODE_SELECT 0 x0100
#define OV08X40_MODE_STANDBY 0 x00
#define OV08X40_MODE_STREAMING 0 x01
#define OV08X40_REG_AO_STANDBY 0 x1000
#define OV08X40_AO_STREAMING 0 x04
#define OV08X40_REG_MS_SELECT 0 x1001
#define OV08X40_MS_STANDBY 0 x00
#define OV08X40_MS_STREAMING 0 x04
#define OV08X40_REG_SOFTWARE_RST 0 x0103
#define OV08X40_SOFTWARE_RST 0 x01
/* Chip ID */
#define OV08X40_REG_CHIP_ID 0 x300a
#define OV08X40_CHIP_ID 0 x560858
/* V_TIMING internal */
#define OV08X40_REG_VTS 0 x380e
#define OV08X40_VTS_30FPS 0 x09c4 /* the VTS need to be half in normal mode */
#define OV08X40_VTS_BIN_30FPS 0 x115c
#define OV08X40_VTS_MAX 0 x7fff
/* H TIMING internal */
#define OV08X40_REG_HTS 0 x380c
#define OV08X40_HTS_30FPS 0 x0280
/* Exposure control */
#define OV08X40_REG_EXPOSURE 0 x3500
#define OV08X40_EXPOSURE_MAX_MARGIN 8
#define OV08X40_EXPOSURE_BIN_MAX_MARGIN 2
#define OV08X40_EXPOSURE_MIN 4
#define OV08X40_EXPOSURE_STEP 1
#define OV08X40_EXPOSURE_DEFAULT 0 x40
/* Short Exposure control */
#define OV08X40_REG_SHORT_EXPOSURE 0 x3540
/* Analog gain control */
#define OV08X40_REG_ANALOG_GAIN 0 x3508
#define OV08X40_ANA_GAIN_MIN 0 x80
#define OV08X40_ANA_GAIN_MAX 0 x07c0
#define OV08X40_ANA_GAIN_STEP 1
#define OV08X40_ANA_GAIN_DEFAULT 0 x80
/* Digital gain control */
#define OV08X40_REG_DGTL_GAIN_H 0 x350a
#define OV08X40_REG_DGTL_GAIN_M 0 x350b
#define OV08X40_REG_DGTL_GAIN_L 0 x350c
#define OV08X40_DGTL_GAIN_MIN 1024 /* Min = 1 X */
#define OV08X40_DGTL_GAIN_MAX (4096 - 1 ) /* Max = 4 X */
#define OV08X40_DGTL_GAIN_DEFAULT 2560 /* Default gain = 2.5 X */
#define OV08X40_DGTL_GAIN_STEP 1 /* Each step = 1/1024 */
#define OV08X40_DGTL_GAIN_L_SHIFT 6
#define OV08X40_DGTL_GAIN_L_MASK 0 x3
#define OV08X40_DGTL_GAIN_M_SHIFT 2
#define OV08X40_DGTL_GAIN_M_MASK 0 xff
#define OV08X40_DGTL_GAIN_H_SHIFT 10
#define OV08X40_DGTL_GAIN_H_MASK 0 x1F
/* Test Pattern Control */
#define OV08X40_REG_TEST_PATTERN 0 x50C1
#define OV08X40_REG_ISP 0 x5000
#define OV08X40_REG_SHORT_TEST_PATTERN 0 x53C1
#define OV08X40_TEST_PATTERN_ENABLE BIT(0 )
#define OV08X40_TEST_PATTERN_MASK 0 xcf
#define OV08X40_TEST_PATTERN_BAR_SHIFT 4
/* Flip Control */
#define OV08X40_REG_VFLIP 0 x3820
#define OV08X40_REG_MIRROR 0 x3821
/* Horizontal Window Offset */
#define OV08X40_REG_H_WIN_OFFSET 0 x3811
/* Vertical Window Offset */
#define OV08X40_REG_V_WIN_OFFSET 0 x3813
/* Burst Register */
#define OV08X40_REG_XTALK_FIRST_A 0 x5a80
#define OV08X40_REG_XTALK_LAST_A 0 x5b9f
#define OV08X40_REG_XTALK_FIRST_B 0 x5bc0
#define OV08X40_REG_XTALK_LAST_B 0 x5f1f
enum {
OV08X40_LINK_FREQ_400MHZ_INDEX,
OV08X40_LINK_FREQ_749MHZ_INDEX,
};
struct ov08x40_reg {
u16 address;
u8 val;
};
struct ov08x40_reg_list {
u32 num_of_regs;
const struct ov08x40_reg *regs;
};
/* Link frequency config */
struct ov08x40_link_freq_config {
/* registers for this link frequency */
struct ov08x40_reg_list reg_list;
};
/* Mode : resolution and related config&values */
struct ov08x40_mode {
/* Frame width */
u32 width;
/* Frame height */
u32 height;
u32 lanes;
/* V-timing */
u32 vts_def;
u32 vts_min;
/* Line Length Pixels */
u32 llp;
/* Index of Link frequency config to be used */
u32 link_freq_index;
/* Default register values */
struct ov08x40_reg_list reg_list;
/* Exposure calculation */
u16 exposure_margin;
u16 exposure_shift;
};
static const struct ov08x40_reg ov08x40_global_regs[] = {
{0 x1216, 0 x60},
{0 x1217, 0 x5b},
{0 x1218, 0 x00},
{0 x1220, 0 x24},
{0 x198a, 0 x00},
{0 x198b, 0 x01},
{0 x198e, 0 x00},
{0 x198f, 0 x01},
{0 x3009, 0 x04},
{0 x3015, 0 x00},
{0 x3016, 0 xb0},
{0 x3017, 0 xf0},
{0 x3018, 0 xf0},
{0 x3019, 0 xd2},
{0 x301a, 0 xb0},
{0 x301c, 0 x81},
{0 x301d, 0 x02},
{0 x301e, 0 x80},
{0 x3022, 0 xf0},
{0 x3025, 0 x89},
{0 x3030, 0 x03},
{0 x3044, 0 xc2},
{0 x3050, 0 x35},
{0 x3051, 0 x60},
{0 x3052, 0 x25},
{0 x3053, 0 x00},
{0 x3054, 0 x00},
{0 x3055, 0 x02},
{0 x3056, 0 x80},
{0 x3057, 0 x80},
{0 x3058, 0 x80},
{0 x3059, 0 x00},
{0 x3107, 0 x86},
{0 x3401, 0 x80},
{0 x3402, 0 x8c},
{0 x3404, 0 x01},
{0 x3407, 0 x01},
{0 x341b, 0 x30},
{0 x3420, 0 x00},
{0 x3421, 0 x00},
{0 x3422, 0 x00},
{0 x3423, 0 x00},
{0 x3424, 0 x00},
{0 x3425, 0 x00},
{0 x3426, 0 x10},
{0 x3427, 0 x00},
{0 x3428, 0 x0f},
{0 x3429, 0 x00},
{0 x342a, 0 x00},
{0 x342b, 0 x00},
{0 x342c, 0 x00},
{0 x342d, 0 x00},
{0 x342e, 0 x00},
{0 x342f, 0 x11},
{0 x3430, 0 x11},
{0 x3431, 0 x10},
{0 x3432, 0 x00},
{0 x3433, 0 x00},
{0 x3434, 0 x00},
{0 x3435, 0 x00},
{0 x3436, 0 x00},
{0 x3437, 0 x00},
{0 x3442, 0 x02},
{0 x3443, 0 x02},
{0 x3444, 0 x07},
{0 x3450, 0 x00},
{0 x3451, 0 x00},
{0 x3452, 0 x18},
{0 x3453, 0 x18},
{0 x3454, 0 x00},
{0 x3455, 0 x80},
{0 x3456, 0 x08},
{0 x3500, 0 x00},
{0 x3502, 0 x10},
{0 x3504, 0 x4c},
{0 x3506, 0 x30},
{0 x3507, 0 x00},
{0 x350a, 0 x01},
{0 x350b, 0 x00},
{0 x350c, 0 x00},
{0 x3540, 0 x00},
{0 x3544, 0 x4c},
{0 x3546, 0 x30},
{0 x3547, 0 x00},
{0 x3549, 0 x00},
{0 x354a, 0 x01},
{0 x354b, 0 x00},
{0 x354c, 0 x00},
{0 x3601, 0 x40},
{0 x3602, 0 x90},
{0 x3608, 0 x0a},
{0 x3609, 0 x08},
{0 x360f, 0 x99},
{0 x3680, 0 xa4},
{0 x3682, 0 x80},
{0 x3688, 0 x02},
{0 x368a, 0 x2e},
{0 x368e, 0 x71},
{0 x3696, 0 xd1},
{0 x3699, 0 x00},
{0 x369a, 0 x00},
{0 x36a4, 0 x00},
{0 x36a6, 0 x00},
{0 x3711, 0 x00},
{0 x3713, 0 x00},
{0 x3716, 0 x00},
{0 x3718, 0 x07},
{0 x371a, 0 x1c},
{0 x371b, 0 x00},
{0 x3720, 0 x08},
{0 x3725, 0 x32},
{0 x3727, 0 x05},
{0 x3760, 0 x02},
{0 x3762, 0 x02},
{0 x3763, 0 x02},
{0 x3764, 0 x02},
{0 x3765, 0 x2c},
{0 x3766, 0 x04},
{0 x3767, 0 x2c},
{0 x3768, 0 x02},
{0 x3769, 0 x00},
{0 x376b, 0 x20},
{0 x37b2, 0 x01},
{0 x3800, 0 x00},
{0 x3801, 0 x00},
{0 x3802, 0 x00},
{0 x3804, 0 x0f},
{0 x3805, 0 x1f},
{0 x3806, 0 x09},
{0 x380c, 0 x02},
{0 x3810, 0 x00},
{0 x3812, 0 x00},
{0 x3814, 0 x11},
{0 x3815, 0 x11},
{0 x3822, 0 x00},
{0 x3828, 0 x0f},
{0 x382a, 0 x80},
{0 x382e, 0 x41},
{0 x3837, 0 x08},
{0 x383a, 0 x81},
{0 x383b, 0 x81},
{0 x383c, 0 x11},
{0 x383d, 0 x11},
{0 x383e, 0 x00},
{0 x383f, 0 x38},
{0 x3840, 0 x00},
{0 x3847, 0 x00},
{0 x384a, 0 x00},
{0 x384c, 0 x02},
{0 x3856, 0 x50},
{0 x3857, 0 x30},
{0 x3858, 0 x80},
{0 x3859, 0 x40},
{0 x3860, 0 x00},
{0 x3888, 0 x00},
{0 x3889, 0 x00},
{0 x388a, 0 x00},
{0 x388b, 0 x00},
{0 x388c, 0 x00},
{0 x388d, 0 x00},
{0 x388e, 0 x00},
{0 x388f, 0 x00},
{0 x3895, 0 x00},
{0 x3911, 0 x90},
{0 x3913, 0 x90},
{0 x3921, 0 x0f},
{0 x3928, 0 x15},
{0 x3929, 0 x2a},
{0 x392c, 0 x02},
{0 x392e, 0 x04},
{0 x392f, 0 x03},
{0 x3931, 0 x07},
{0 x3932, 0 x10},
{0 x3938, 0 x09},
{0 x3a1f, 0 x8a},
{0 x3a22, 0 x91},
{0 x3a23, 0 x15},
{0 x3a25, 0 x96},
{0 x3a28, 0 xb4},
{0 x3a29, 0 x26},
{0 x3a2b, 0 xba},
{0 x3a2e, 0 xbf},
{0 x3a2f, 0 x18},
{0 x3a31, 0 xc1},
{0 x3a74, 0 x84},
{0 x3a99, 0 x84},
{0 x3ab9, 0 xa6},
{0 x3aba, 0 xba},
{0 x3b0a, 0 x01},
{0 x3b0b, 0 x00},
{0 x3b0e, 0 x01},
{0 x3b0f, 0 x00},
{0 x3b12, 0 x84},
{0 x3b14, 0 xbb},
{0 x3b15, 0 xbf},
{0 x3b1b, 0 xc9},
{0 x3b21, 0 xc9},
{0 x3b3f, 0 x9d},
{0 x3b45, 0 x9d},
{0 x3c84, 0 x00},
{0 x3d84, 0 x04},
{0 x3d85, 0 x8b},
{0 x3daa, 0 x80},
{0 x3dab, 0 x14},
{0 x3dac, 0 x80},
{0 x3dad, 0 xc8},
{0 x3dae, 0 x81},
{0 x3daf, 0 x7b},
{0 x3f00, 0 x10},
{0 x3f01, 0 x11},
{0 x3f06, 0 x0d},
{0 x3f07, 0 x0b},
{0 x3f08, 0 x0d},
{0 x3f09, 0 x0b},
{0 x3f0a, 0 x01},
{0 x3f0b, 0 x11},
{0 x3f0c, 0 x33},
{0 x4001, 0 x07},
{0 x4007, 0 x20},
{0 x4008, 0 x00},
{0 x4009, 0 x05},
{0 x400a, 0 x00},
{0 x400c, 0 x00},
{0 x400e, 0 x14},
{0 x4010, 0 xf4},
{0 x4011, 0 x03},
{0 x4012, 0 x55},
{0 x4015, 0 x00},
{0 x4017, 0 x00},
{0 x4018, 0 x0f},
{0 x4019, 0 x00},
{0 x401a, 0 x40},
{0 x401b, 0 x08},
{0 x401c, 0 x00},
{0 x401d, 0 x10},
{0 x401e, 0 x02},
{0 x401f, 0 x00},
{0 x4020, 0 x04},
{0 x4021, 0 x00},
{0 x4022, 0 x04},
{0 x4023, 0 x00},
{0 x4024, 0 x04},
{0 x4025, 0 x00},
{0 x4026, 0 x04},
{0 x4027, 0 x00},
{0 x4030, 0 x00},
{0 x4031, 0 x00},
{0 x4032, 0 x00},
{0 x4033, 0 x00},
{0 x4034, 0 x00},
{0 x4035, 0 x00},
{0 x4036, 0 x00},
{0 x4037, 0 x00},
{0 x4040, 0 x00},
{0 x4041, 0 x80},
{0 x4042, 0 x00},
{0 x4043, 0 x80},
{0 x4044, 0 x00},
{0 x4045, 0 x80},
{0 x4046, 0 x00},
{0 x4047, 0 x80},
{0 x4050, 0 x06},
{0 x4051, 0 xff},
{0 x4052, 0 xff},
{0 x4053, 0 xff},
{0 x4054, 0 xff},
{0 x4055, 0 xff},
{0 x4056, 0 xff},
{0 x4057, 0 x7f},
{0 x4058, 0 x00},
{0 x4059, 0 x00},
{0 x405a, 0 x00},
{0 x405b, 0 x00},
{0 x405c, 0 x07},
{0 x405d, 0 xff},
{0 x405e, 0 x07},
{0 x405f, 0 xff},
{0 x4060, 0 x00},
{0 x4061, 0 x00},
{0 x4062, 0 x00},
{0 x4063, 0 x00},
{0 x4064, 0 x00},
{0 x4065, 0 x00},
{0 x4066, 0 x00},
{0 x4067, 0 x00},
{0 x4068, 0 x00},
{0 x4069, 0 x00},
{0 x406a, 0 x00},
{0 x406b, 0 x00},
{0 x406c, 0 x00},
{0 x406d, 0 x00},
{0 x406e, 0 x00},
{0 x406f, 0 x00},
{0 x4070, 0 x00},
{0 x4071, 0 x00},
{0 x4072, 0 x00},
{0 x4073, 0 x00},
{0 x4074, 0 x00},
{0 x4075, 0 x00},
{0 x4076, 0 x00},
{0 x4077, 0 x00},
{0 x4078, 0 x00},
{0 x4079, 0 x00},
{0 x407a, 0 x00},
{0 x407b, 0 x00},
{0 x407c, 0 x00},
{0 x407d, 0 x00},
{0 x407e, 0 x00},
{0 x407f, 0 x00},
{0 x4080, 0 x78},
{0 x4081, 0 x78},
{0 x4082, 0 x78},
{0 x4083, 0 x78},
{0 x40e0, 0 x00},
{0 x40e1, 0 x00},
{0 x40e2, 0 x00},
{0 x40e3, 0 x00},
{0 x40e4, 0 x00},
{0 x40e5, 0 x00},
{0 x40e6, 0 x00},
{0 x40e7, 0 x00},
{0 x40e8, 0 x00},
{0 x40e9, 0 x80},
{0 x40ea, 0 x00},
{0 x40eb, 0 x80},
{0 x40ec, 0 x00},
{0 x40ed, 0 x80},
{0 x40ee, 0 x00},
{0 x40ef, 0 x80},
{0 x40f0, 0 x02},
{0 x40f1, 0 x04},
{0 x4300, 0 x00},
{0 x4301, 0 x00},
{0 x4302, 0 x00},
{0 x4303, 0 x00},
{0 x4304, 0 x00},
{0 x4305, 0 x00},
{0 x4306, 0 x00},
{0 x4307, 0 x00},
{0 x4308, 0 x00},
{0 x4309, 0 x00},
{0 x430a, 0 x00},
{0 x430b, 0 xff},
{0 x430c, 0 xff},
{0 x430d, 0 x00},
{0 x430e, 0 x00},
{0 x4315, 0 x00},
{0 x4316, 0 x00},
{0 x4317, 0 x00},
{0 x4318, 0 x00},
{0 x4319, 0 x00},
{0 x431a, 0 x00},
{0 x431b, 0 x00},
{0 x431c, 0 x00},
{0 x4500, 0 x07},
{0 x4502, 0 x00},
{0 x4503, 0 x0f},
{0 x4504, 0 x80},
{0 x4506, 0 x01},
{0 x4509, 0 x05},
{0 x450c, 0 x00},
{0 x450d, 0 x20},
{0 x450e, 0 x00},
{0 x450f, 0 x00},
{0 x4510, 0 x00},
{0 x4523, 0 x00},
{0 x4526, 0 x00},
{0 x4543, 0 x00},
{0 x4544, 0 x00},
{0 x4545, 0 x00},
{0 x4546, 0 x00},
{0 x4547, 0 x10},
{0 x4602, 0 x00},
{0 x4603, 0 x15},
{0 x460b, 0 x07},
{0 x4680, 0 x11},
{0 x4686, 0 x00},
{0 x4687, 0 x00},
{0 x4700, 0 x00},
{0 x4800, 0 x64},
{0 x4806, 0 x40},
{0 x480b, 0 x10},
{0 x480c, 0 x80},
{0 x480f, 0 x32},
{0 x4813, 0 xe4},
{0 x4884, 0 x04},
{0 x4c00, 0 xf8},
{0 x4c01, 0 x44},
{0 x4c03, 0 x00},
{0 x4d00, 0 x00},
{0 x4d01, 0 x16},
{0 x4d04, 0 x10},
{0 x4d05, 0 x00},
{0 x4d06, 0 x0c},
{0 x4d07, 0 x00},
{0 x5008, 0 xb0},
{0 x50c1, 0 x00},
{0 x53c1, 0 x00},
{0 x5f40, 0 x00},
{0 x5f41, 0 x40},
};
static const struct ov08x40_reg_list ov08x40_global_setting = {
.num_of_regs = ARRAY_SIZE(ov08x40_global_regs),
.regs = ov08x40_global_regs,
};
static const struct ov08x40_reg mipi_data_rate_800mbps[] = {
{0 x0103, 0 x01},
{0 x1000, 0 x00},
{0 x1601, 0 xd0},
{0 x1001, 0 x04},
{0 x5004, 0 x53},
{0 x5110, 0 x00},
{0 x5111, 0 x14},
{0 x5112, 0 x01},
{0 x5113, 0 x7b},
{0 x5114, 0 x00},
{0 x5152, 0 xa3},
{0 x5a52, 0 x1f},
{0 x5a1a, 0 x0e},
{0 x5a1b, 0 x10},
{0 x5a1f, 0 x0e},
{0 x5a27, 0 x0e},
{0 x6002, 0 x2e},
{0 x0300, 0 x3a}, /* PLL CTRL */
{0 x0301, 0 xc8},
{0 x0302, 0 x31},
{0 x0303, 0 x03},
{0 x0304, 0 x01},
{0 x0305, 0 xa1},
{0 x0306, 0 x04},
{0 x0307, 0 x01},
{0 x0308, 0 x03},
{0 x0309, 0 x03},
{0 x0310, 0 x0a},
{0 x0311, 0 x02},
{0 x0312, 0 x01},
{0 x0313, 0 x08},
{0 x0314, 0 x66},
{0 x0315, 0 x00},
{0 x0316, 0 x34},
{0 x0320, 0 x02},
{0 x0321, 0 x03},
{0 x0323, 0 x05},
{0 x0324, 0 x01},
{0 x0325, 0 xb8},
{0 x0326, 0 x4a},
{0 x0327, 0 x04},
{0 x0329, 0 x00},
{0 x032a, 0 x05},
{0 x032b, 0 x00},
{0 x032c, 0 x00},
{0 x032d, 0 x00},
{0 x032e, 0 x02},
{0 x032f, 0 xa0},
{0 x0350, 0 x00},
{0 x0360, 0 x01},
{0 x3012, 0 x41}, /* MIPI SC Lanes */
};
static const struct ov08x40_reg mipi_data_rate_1500mbps[] = {
{0 x0103, 0 x01},
{0 x1000, 0 x00},
{0 x1601, 0 xd0},
{0 x1001, 0 x04},
{0 x5004, 0 x53},
{0 x5110, 0 x00},
{0 x5111, 0 x14},
{0 x5112, 0 x01},
{0 x5113, 0 x7b},
{0 x5114, 0 x00},
{0 x5152, 0 xa3},
{0 x5a52, 0 x1f},
{0 x5a1a, 0 x0e},
{0 x5a1b, 0 x10},
{0 x5a1f, 0 x0e},
{0 x5a27, 0 x0e},
{0 x6002, 0 x2e},
{0 x0300, 0 x3a}, /* PLL */
{0 x0301, 0 x88},
{0 x0302, 0 x31},
{0 x0303, 0 x05},
{0 x0304, 0 x01},
{0 x0305, 0 x38},
{0 x0306, 0 x04},
{0 x0307, 0 x00},
{0 x0308, 0 x03},
{0 x0309, 0 x02},
{0 x0310, 0 x0a},
{0 x0311, 0 x02},
{0 x0312, 0 x01},
{0 x0313, 0 x08},
{0 x0314, 0 x00},
{0 x0315, 0 x00},
{0 x0316, 0 x2c},
{0 x0320, 0 x02},
{0 x0321, 0 x03},
{0 x0323, 0 x05},
{0 x0324, 0 x01},
{0 x0325, 0 xb8},
{0 x0326, 0 x4a},
{0 x0327, 0 x04},
{0 x0329, 0 x00},
{0 x032a, 0 x05},
{0 x032b, 0 x00},
{0 x032c, 0 x00},
{0 x032d, 0 x00},
{0 x032e, 0 x02},
{0 x032f, 0 xa0},
{0 x0350, 0 x00},
{0 x0360, 0 x01},
{0 x3012, 0 x21}, /* MIPI SC Lanes */
};
static const struct ov08x40_reg mode_3856x2176_regs_800mbps[] = {
{0 x5000, 0 x5d},
{0 x5001, 0 x20},
{0 x3012, 0 x41},
{0 x3400, 0 x1c},
{0 x3419, 0 x13},
{0 x341a, 0 x89},
{0 x3426, 0 x00},
{0 x3501, 0 x02},
{0 x3502, 0 x00},
{0 x3508, 0 x01},
{0 x3509, 0 x00},
{0 x3541, 0 x01},
{0 x3542, 0 x00},
{0 x3548, 0 x01},
{0 x3712, 0 x51},
{0 x3714, 0 x24},
{0 x3761, 0 x17},
{0 x376e, 0 x03},
{0 x37b0, 0 x00},
{0 x37b1, 0 xab},
{0 x37b3, 0 x82},
{0 x37b4, 0 x00},
{0 x37b5, 0 xe4},
{0 x37b6, 0 x01},
{0 x37b7, 0 xee},
{0 x3820, 0 x00},
{0 x3821, 0 x04},
{0 x3823, 0 x04},
{0 x384d, 0 x80},
{0 x3894, 0 x00},
{0 x400b, 0 x08},
{0 x400d, 0 x08},
{0 x4016, 0 x2d},
{0 x4501, 0 x00},
{0 x4542, 0 x00},
{0 x4837, 0 x14},
{0 x4850, 0 x42},
{0 x3a20, 0 x00},
{0 x3939, 0 x9d},
{0 x3902, 0 x0e},
{0 x3903, 0 x0e},
{0 x3904, 0 x0e},
{0 x3905, 0 x0e},
{0 x3906, 0 x07},
{0 x3907, 0 x0d},
{0 x3908, 0 x11},
{0 x3909, 0 x12},
{0 x390c, 0 x33},
{0 x390d, 0 x66},
{0 x390e, 0 xaa},
{0 x3915, 0 x90},
{0 x3917, 0 x90},
{0 x3440, 0 xa4},
{0 x3a26, 0 x1d},
{0 x3a2c, 0 x4a},
{0 x3a32, 0 x55},
{0 x392d, 0 x02},
{0 x3930, 0 x08},
{0 x3933, 0 x0c},
{0 x392a, 0 x54},
{0 x392b, 0 xa8},
{0 x380d, 0 x80},
{0 x380e, 0 x13},
{0 x380f, 0 x88},
{0 x3803, 0 x70},
{0 x3807, 0 x0f},
{0 x3808, 0 x0f},
{0 x3809, 0 x10},
{0 x380a, 0 x08},
{0 x380b, 0 x80},
{0 x3811, 0 x08},
{0 x3813, 0 x10},
{0 x3501, 0 x10},
{0 x3508, 0 x0f},
{0 x3509, 0 x80},
{0 x3813, 0 x0f},
};
/* OV08X 1C 3856x2176_DPHY1500M-2L */
static const struct ov08x40_reg mode_3856x2176_regs_1500mbps[] = {
{0 x5000, 0 x5d},
{0 x5001, 0 x20},
{0 x3012, 0 x21},
{0 x3400, 0 x1c},
{0 x3419, 0 x12},
{0 x341a, 0 x99},
{0 x3426, 0 x00},
{0 x3501, 0 x02},
{0 x3502, 0 x00},
{0 x3508, 0 x01},
{0 x3509, 0 x00},
{0 x3541, 0 x01},
{0 x3542, 0 x00},
{0 x3548, 0 x01},
{0 x3712, 0 x51},
{0 x3714, 0 x24},
{0 x3761, 0 x17},
{0 x376e, 0 x03},
{0 x37b0, 0 x00},
{0 x37b1, 0 xab},
{0 x37b3, 0 x82},
{0 x37b4, 0 x00},
{0 x37b5, 0 xe4},
{0 x37b6, 0 x01},
{0 x37b7, 0 xee},
{0 x3803, 0 x70},
{0 x3807, 0 x0f},
{0 x3808, 0 x0f},
{0 x3809, 0 x10},
{0 x380a, 0 x08},
{0 x380b, 0 x80},
{0 x380d, 0 xa0},
{0 x380e, 0 x12},
{0 x380f, 0 x98},
{0 x3811, 0 x08},
{0 x3813, 0 x10},
{0 x3820, 0 x00},
{0 x3821, 0 x04},
{0 x3823, 0 x04},
{0 x384d, 0 xa0},
{0 x3894, 0 x00},
{0 x400b, 0 x08},
{0 x400d, 0 x08},
{0 x4016, 0 x2d},
{0 x4501, 0 x00},
{0 x4542, 0 x00},
{0 x4837, 0 x0a},
{0 x4850, 0 x47},
{0 x3a20, 0 x00},
{0 x3939, 0 x9d},
{0 x3902, 0 x0e},
{0 x3903, 0 x0e},
{0 x3904, 0 x0e},
{0 x3905, 0 x0e},
{0 x3906, 0 x07},
{0 x3907, 0 x0d},
{0 x3908, 0 x11},
{0 x3909, 0 x12},
{0 x390c, 0 x33},
{0 x390d, 0 x66},
{0 x390e, 0 xaa},
{0 x3915, 0 x90},
{0 x3917, 0 x90},
{0 x3440, 0 xa4},
{0 x3a26, 0 x1d},
{0 x3a2c, 0 x4a},
{0 x3a32, 0 x55},
{0 x392d, 0 x02},
{0 x3930, 0 x08},
{0 x3933, 0 x0c},
{0 x392a, 0 x54},
{0 x392b, 0 xa8},
{0 x3501, 0 x10},
{0 x3508, 0 x0f},
{0 x3509, 0 x80},
{0 x3813, 0 x0f},
};
/* OV08X 4C1stg 1928x1088_DPHY1500M-2L 30fps */
static const struct ov08x40_reg mode_1928x1088_regs_1500mbps[] = {
{0 x5000, 0 x55},
{0 x5001, 0 x00},
{0 x3012, 0 x21},
{0 x3400, 0 x30},
{0 x3419, 0 x08},
{0 x341a, 0 x4f},
{0 x3426, 0 x00},
{0 x3501, 0 x02},
{0 x3502, 0 x00},
{0 x3508, 0 x01},
{0 x3509, 0 x00},
{0 x3541, 0 x01},
{0 x3542, 0 x00},
{0 x3548, 0 x01},
{0 x3712, 0 x50},
{0 x3714, 0 x21},
{0 x3761, 0 x28},
{0 x376e, 0 x07},
{0 x37b0, 0 x01},
{0 x37b1, 0 x0f},
{0 x37b3, 0 xd6},
{0 x37b4, 0 x01},
{0 x37b5, 0 x48},
{0 x37b6, 0 x02},
{0 x37b7, 0 x40},
{0 x3803, 0 x78},
{0 x3807, 0 x07},
{0 x3808, 0 x07},
{0 x3809, 0 x88},
{0 x380a, 0 x04},
{0 x380b, 0 x40},
{0 x380d, 0 xf0},
{0 x380e, 0 x08},
{0 x380f, 0 x4e},
{0 x3811, 0 x04},
{0 x3813, 0 x03},
{0 x3820, 0 x02},
{0 x3821, 0 x14},
{0 x3823, 0 x84},
{0 x384d, 0 xf0},
{0 x3894, 0 x03},
{0 x400b, 0 x04},
{0 x400d, 0 x04},
{0 x4016, 0 x27},
{0 x4501, 0 x10},
{0 x4542, 0 x01},
{0 x4837, 0 x0a},
{0 x4850, 0 x47},
{0 x4911, 0 x00},
{0 x4919, 0 x00},
{0 x491a, 0 x40},
{0 x4920, 0 x04},
{0 x4921, 0 x00},
{0 x4922, 0 x04},
{0 x4923, 0 x00},
{0 x4924, 0 x04},
{0 x4925, 0 x00},
{0 x4926, 0 x04},
{0 x4927, 0 x00},
{0 x4930, 0 x00},
{0 x4931, 0 x00},
{0 x4932, 0 x00},
{0 x4933, 0 x00},
{0 x4934, 0 x00},
{0 x4935, 0 x00},
{0 x4936, 0 x00},
{0 x4937, 0 x00},
{0 x4940, 0 x00},
{0 x4941, 0 x80},
{0 x4942, 0 x00},
{0 x4943, 0 x80},
{0 x4944, 0 x00},
{0 x4945, 0 x80},
{0 x4946, 0 x00},
{0 x4947, 0 x80},
{0 x4960, 0 x00},
{0 x4961, 0 x00},
{0 x4962, 0 x00},
{0 x4963, 0 x00},
{0 x4964, 0 x00},
{0 x4965, 0 x00},
{0 x4966, 0 x00},
{0 x4967, 0 x00},
{0 x4968, 0 x00},
{0 x4969, 0 x00},
{0 x496a, 0 x00},
{0 x496b, 0 x00},
{0 x496c, 0 x00},
{0 x496d, 0 x00},
{0 x496e, 0 x00},
{0 x496f, 0 x00},
{0 x4970, 0 x00},
{0 x4971, 0 x00},
{0 x4972, 0 x00},
{0 x4973, 0 x00},
{0 x4974, 0 x00},
{0 x4975, 0 x00},
{0 x4976, 0 x00},
{0 x4977, 0 x00},
{0 x4978, 0 x00},
{0 x4979, 0 x00},
{0 x497a, 0 x00},
{0 x497b, 0 x00},
{0 x497c, 0 x00},
{0 x497d, 0 x00},
{0 x497e, 0 x00},
{0 x497f, 0 x00},
{0 x49e0, 0 x00},
{0 x49e1, 0 x00},
{0 x49e2, 0 x00},
{0 x49e3, 0 x00},
{0 x49e4, 0 x00},
{0 x49e5, 0 x00},
{0 x49e6, 0 x00},
{0 x49e7, 0 x00},
{0 x49e8, 0 x00},
{0 x49e9, 0 x80},
{0 x49ea, 0 x00},
{0 x49eb, 0 x80},
{0 x49ec, 0 x00},
{0 x49ed, 0 x80},
{0 x49ee, 0 x00},
{0 x49ef, 0 x80},
{0 x49f0, 0 x02},
{0 x49f1, 0 x04},
{0 x3a20, 0 x05},
{0 x3939, 0 x6b},
{0 x3902, 0 x10},
{0 x3903, 0 x10},
{0 x3904, 0 x10},
{0 x3905, 0 x10},
{0 x3906, 0 x01},
{0 x3907, 0 x0b},
{0 x3908, 0 x10},
{0 x3909, 0 x13},
{0 x390b, 0 x11},
{0 x390c, 0 x21},
{0 x390d, 0 x32},
{0 x390e, 0 x76},
{0 x3a1a, 0 x1c},
{0 x3a26, 0 x17},
{0 x3a2c, 0 x50},
{0 x3a32, 0 x4f},
{0 x3ace, 0 x01},
{0 x3ad2, 0 x01},
{0 x3ad6, 0 x01},
{0 x3ada, 0 x01},
{0 x3ade, 0 x01},
{0 x3ae2, 0 x01},
{0 x3aee, 0 x01},
{0 x3af2, 0 x01},
{0 x3af6, 0 x01},
{0 x3afa, 0 x01},
{0 x3afe, 0 x01},
{0 x3b02, 0 x01},
{0 x3b06, 0 x01},
{0 x392d, 0 x01},
{0 x3930, 0 x09},
{0 x3933, 0 x0d},
{0 x392a, 0 x52},
{0 x392b, 0 xa3},
{0 x340b, 0 x1b},
{0 x3501, 0 x01},
{0 x3508, 0 x0f},
{0 x3509, 0 x00},
{0 x3541, 0 x00},
{0 x3542, 0 x80},
{0 x3548, 0 x0f},
{0 x3813, 0 x03},
};
static const struct ov08x40_reg mode_3856x2416_regs[] = {
{0 x5000, 0 x5d},
{0 x5001, 0 x20},
{0 x3012, 0 x41},
{0 x3400, 0 x1c},
{0 x3419, 0 x13},
{0 x341a, 0 x89},
{0 x3426, 0 x00},
{0 x3501, 0 x02},
{0 x3502, 0 x00},
{0 x3508, 0 x01},
{0 x3509, 0 x00},
{0 x3541, 0 x01},
{0 x3542, 0 x00},
{0 x3548, 0 x01},
{0 x3712, 0 x51},
{0 x3714, 0 x24},
{0 x3761, 0 x17},
{0 x376e, 0 x03},
{0 x37b0, 0 x00},
{0 x37b1, 0 xab},
{0 x37b3, 0 x82},
{0 x37b4, 0 x00},
{0 x37b5, 0 xe4},
{0 x37b6, 0 x01},
{0 x37b7, 0 xee},
{0 x3803, 0 x00},
{0 x3807, 0 x7f},
{0 x3808, 0 x0f},
{0 x3809, 0 x10},
{0 x380a, 0 x09},
{0 x380b, 0 x70},
{0 x380d, 0 x80},
{0 x380e, 0 x13},
{0 x380f, 0 x88},
{0 x3811, 0 x08},
{0 x3813, 0 x07},
{0 x3820, 0 x00},
{0 x3821, 0 x04},
{0 x3823, 0 x04},
{0 x384d, 0 x80},
{0 x3894, 0 x00},
{0 x400b, 0 x08},
{0 x400d, 0 x08},
{0 x4016, 0 x2d},
{0 x4501, 0 x00},
{0 x4542, 0 x00},
{0 x4837, 0 x14},
{0 x4850, 0 x42},
{0 x3a20, 0 x00},
{0 x3939, 0 x9d},
{0 x3902, 0 x0e},
{0 x3903, 0 x0e},
{0 x3904, 0 x0e},
{0 x3905, 0 x0e},
{0 x3906, 0 x07},
{0 x3907, 0 x0d},
{0 x3908, 0 x11},
{0 x3909, 0 x12},
{0 x390c, 0 x33},
{0 x390d, 0 x66},
{0 x390e, 0 xaa},
{0 x3915, 0 x90},
{0 x3917, 0 x90},
{0 x3440, 0 xa4},
{0 x3a26, 0 x1d},
{0 x3a2c, 0 x4a},
{0 x3a32, 0 x55},
{0 x392d, 0 x02},
{0 x3930, 0 x08},
{0 x3933, 0 x0c},
{0 x392a, 0 x54},
{0 x392b, 0 xa8},
{0 x3501, 0 x10},
{0 x3508, 0 x0f},
{0 x3509, 0 x80},
};
static const struct ov08x40_reg mode_1928x1208_regs[] = {
{0 x5000, 0 x55},
{0 x5001, 0 x00},
{0 x3012, 0 x41},
{0 x3400, 0 x1c},
{0 x3419, 0 x08},
{0 x341a, 0 xaf},
{0 x3426, 0 x00},
{0 x3501, 0 x02},
{0 x3502, 0 x00},
{0 x3508, 0 x01},
{0 x3509, 0 x00},
{0 x3541, 0 x01},
{0 x3542, 0 x00},
{0 x3548, 0 x01},
{0 x3712, 0 x50},
{0 x3714, 0 x21},
{0 x3761, 0 x28},
{0 x376e, 0 x07},
{0 x37b0, 0 x01},
{0 x37b1, 0 x0f},
{0 x37b3, 0 xd6},
{0 x37b4, 0 x01},
{0 x37b5, 0 x48},
{0 x37b6, 0 x02},
{0 x37b7, 0 x40},
{0 x3803, 0 x00},
{0 x3807, 0 x7f},
{0 x3808, 0 x07},
{0 x3809, 0 x88},
{0 x380a, 0 x04},
{0 x380b, 0 xb8},
{0 x380d, 0 xd0},
{0 x380e, 0 x11},
{0 x380f, 0 x5c},
{0 x3811, 0 x04},
{0 x3813, 0 x03},
{0 x3820, 0 x02},
{0 x3821, 0 x14},
{0 x3823, 0 x04},
{0 x384d, 0 xd0},
{0 x3894, 0 x00},
{0 x400b, 0 x04},
{0 x400d, 0 x04},
{0 x4016, 0 x27},
{0 x4501, 0 x10},
{0 x4542, 0 x00},
{0 x4837, 0 x14},
{0 x4850, 0 x42},
{0 x3a20, 0 x05},
{0 x3939, 0 x6b},
{0 x3902, 0 x10},
{0 x3903, 0 x10},
{0 x3904, 0 x10},
{0 x3905, 0 x10},
{0 x3906, 0 x01},
{0 x3907, 0 x0b},
{0 x3908, 0 x10},
{0 x3909, 0 x13},
{0 x390b, 0 x11},
{0 x390c, 0 x21},
{0 x390d, 0 x32},
{0 x390e, 0 x76},
{0 x3a1a, 0 x1c},
{0 x3a26, 0 x17},
{0 x3a2c, 0 x50},
{0 x3a32, 0 x4f},
{0 x3ace, 0 x01},
{0 x3ad2, 0 x01},
{0 x3ad6, 0 x01},
{0 x3ada, 0 x01},
{0 x3ade, 0 x01},
{0 x3ae2, 0 x01},
{0 x3aee, 0 x01},
{0 x3af2, 0 x01},
{0 x3af6, 0 x01},
{0 x3afa, 0 x01},
{0 x3afe, 0 x01},
{0 x3b02, 0 x01},
{0 x3b06, 0 x01},
{0 x392d, 0 x01},
{0 x3930, 0 x09},
{0 x3933, 0 x0d},
{0 x392a, 0 x52},
{0 x392b, 0 xa3},
{0 x340b, 0 x1b},
{0 x3501, 0 x08},
{0 x3508, 0 x04},
{0 x3509, 0 x00},
};
static const char * const ov08x40_test_pattern_menu[] = {
"Disabled" ,
"Vertical Color Bar Type 1" ,
"Vertical Color Bar Type 2" ,
"Vertical Color Bar Type 3" ,
"Vertical Color Bar Type 4"
};
/* Configurations for supported link frequencies */
#define OV08X40_LINK_FREQ_400MHZ 400000000 ULL
#define OV08X40_LINK_FREQ_749MHZ 749000000 ULL
#define OV08X40_SCLK_96MHZ 96000000 ULL
#define OV08X40_XVCLK 19200000
#define OV08X40_DATA_LANES 4
/*
* pixel_rate = link_freq * data-rate * nr_of_lanes / bits_per_sample
* data rate => double data rate; number of lanes => 4; bits per pixel => 10
*/
static u64 link_freq_to_pixel_rate(u64 f)
{
f *= 2 * OV08X40_DATA_LANES;
do_div(f, 10 );
return f;
}
/* Menu items for LINK_FREQ V4L2 control */
static const s64 link_freq_menu_items[] = {
OV08X40_LINK_FREQ_400MHZ,
OV08X40_LINK_FREQ_749MHZ,
};
/* Link frequency configs */
static const struct ov08x40_link_freq_config link_freq_configs[] = {
[OV08X40_LINK_FREQ_400MHZ_INDEX] = {
.reg_list = {
.num_of_regs = ARRAY_SIZE(mipi_data_rate_800mbps),
.regs = mipi_data_rate_800mbps,
}
},
[OV08X40_LINK_FREQ_749MHZ_INDEX] = {
.reg_list = {
.num_of_regs = ARRAY_SIZE(mipi_data_rate_1500mbps),
.regs = mipi_data_rate_1500mbps,
}
},
};
/* Mode configs */
static const struct ov08x40_mode supported_modes[] = {
{
.width = 3856 ,
.height = 2416 ,
.vts_def = OV08X40_VTS_30FPS,
.vts_min = OV08X40_VTS_30FPS,
.llp = 0 x10aa, /* in normal mode, tline time = 2 * HTS / SCLK */
.lanes = 4 ,
.reg_list = {
.num_of_regs = ARRAY_SIZE(mode_3856x2416_regs),
.regs = mode_3856x2416_regs,
},
.link_freq_index = OV08X40_LINK_FREQ_400MHZ_INDEX,
.exposure_shift = 1 ,
.exposure_margin = OV08X40_EXPOSURE_MAX_MARGIN,
},
{
.width = 3856 ,
.height = 2176 ,
.vts_def = OV08X40_VTS_30FPS,
.vts_min = OV08X40_VTS_30FPS,
.llp = 0 x10aa, /* in normal mode, tline time = 2 * HTS / SCLK */
.lanes = 4 ,
.reg_list = {
.num_of_regs = ARRAY_SIZE(mode_3856x2176_regs_800mbps),
.regs = mode_3856x2176_regs_800mbps,
},
.link_freq_index = OV08X40_LINK_FREQ_400MHZ_INDEX,
.exposure_shift = 1 ,
.exposure_margin = OV08X40_EXPOSURE_MAX_MARGIN,
},
{
.width = 1928 ,
.height = 1208 ,
.vts_def = OV08X40_VTS_BIN_30FPS,
.vts_min = OV08X40_VTS_BIN_30FPS,
.llp = 0 x960,
.lanes = 4 ,
.reg_list = {
.num_of_regs = ARRAY_SIZE(mode_1928x1208_regs),
.regs = mode_1928x1208_regs,
},
.link_freq_index = OV08X40_LINK_FREQ_400MHZ_INDEX,
.exposure_shift = 0 ,
.exposure_margin = OV08X40_EXPOSURE_BIN_MAX_MARGIN,
},
{
.width = 3856 ,
.height = 2176 ,
.vts_def = OV08X40_VTS_30FPS,
.vts_min = OV08X40_VTS_30FPS,
.llp = 0 x10aa, /* in normal mode, tline time = 2 * HTS / SCLK */
.lanes = 2 ,
.reg_list = {
.num_of_regs = ARRAY_SIZE(mode_3856x2176_regs_1500mbps),
.regs = mode_3856x2176_regs_1500mbps,
},
.link_freq_index = OV08X40_LINK_FREQ_749MHZ_INDEX,
.exposure_shift = 1 ,
.exposure_margin = OV08X40_EXPOSURE_MAX_MARGIN,
},
{
.width = 1928 ,
.height = 1088 ,
.vts_def = OV08X40_VTS_BIN_30FPS,
.vts_min = OV08X40_VTS_BIN_30FPS,
.llp = 0 x960,
.lanes = 2 ,
.reg_list = {
.num_of_regs = ARRAY_SIZE(mode_1928x1088_regs_1500mbps),
.regs = mode_1928x1088_regs_1500mbps,
},
.link_freq_index = OV08X40_LINK_FREQ_749MHZ_INDEX,
.exposure_shift = 0 ,
.exposure_margin = OV08X40_EXPOSURE_MAX_MARGIN,
},
};
static const char * const ov08x40_supply_names[] = {
"dovdd" , /* Digital I/O power */
"avdd" , /* Analog power */
"dvdd" , /* Digital core power */
};
struct ov08x40 {
struct v4l2_subdev sd;
struct media_pad pad;
struct v4l2_ctrl_handler ctrl_handler;
/* V4L2 Controls */
struct v4l2_ctrl *link_freq;
struct v4l2_ctrl *pixel_rate;
struct v4l2_ctrl *vblank;
struct v4l2_ctrl *hblank;
struct v4l2_ctrl *exposure;
struct clk *xvclk;
struct gpio_desc *reset_gpio;
struct regulator_bulk_data supplies[ARRAY_SIZE(ov08x40_supply_names)];
/* Current mode */
const struct ov08x40_mode *cur_mode;
/* Mutex for serialized access */
struct mutex mutex;
/* data lanes */
u8 mipi_lanes;
/* True if the device has been identified */
bool identified;
unsigned long link_freq_bitmap;
};
#define to_ov08x40(_sd) container_of(_sd, struct ov08x40, sd)
static int ov08x40_power_on(struct device *dev)
{
struct v4l2_subdev *sd = dev_get_drvdata(dev);
struct ov08x40 *ov08x = to_ov08x40(sd);
int ret;
ret = clk_prepare_enable(ov08x->xvclk);
if (ret < 0 ) {
dev_err(dev, "failed to enable xvclk\n" );
return ret;
}
if (ov08x->reset_gpio) {
gpiod_set_value_cansleep(ov08x->reset_gpio, 1 );
usleep_range(1000 , 2000 );
}
ret = regulator_bulk_enable(ARRAY_SIZE(ov08x40_supply_names),
ov08x->supplies);
if (ret < 0 ) {
dev_err(dev, "failed to enable regulators\n" );
goto disable_clk;
}
gpiod_set_value_cansleep(ov08x->reset_gpio, 0 );
usleep_range(5000 , 5500 );
return 0 ;
disable_clk:
gpiod_set_value_cansleep(ov08x->reset_gpio, 1 );
clk_disable_unprepare(ov08x->xvclk);
return ret;
}
static int ov08x40_power_off(struct device *dev)
{
struct v4l2_subdev *sd = dev_get_drvdata(dev);
struct ov08x40 *ov08x = to_ov08x40(sd);
gpiod_set_value_cansleep(ov08x->reset_gpio, 1 );
regulator_bulk_disable(ARRAY_SIZE(ov08x40_supply_names),
ov08x->supplies);
clk_disable_unprepare(ov08x->xvclk);
return 0 ;
}
/* Read registers up to 4 at a time */
static int ov08x40_read_reg(struct ov08x40 *ov08x,
u16 reg, u32 len, u32 *val)
{
struct i2c_client *client = v4l2_get_subdevdata(&ov08x->sd);
struct i2c_msg msgs[2 ];
u8 *data_be_p;
int ret;
__be32 data_be = 0 ;
__be16 reg_addr_be = cpu_to_be16(reg);
if (len > 4 )
return -EINVAL;
data_be_p = (u8 *)&data_be;
/* Write register address */
msgs[0 ].addr = client->addr;
msgs[0 ].flags = 0 ;
msgs[0 ].len = 2 ;
msgs[0 ].buf = (u8 *)®_addr_be;
/* Read data from register */
msgs[1 ].addr = client->addr;
msgs[1 ].flags = I2C_M_RD;
msgs[1 ].len = len;
msgs[1 ].buf = &data_be_p[4 - len];
ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
if (ret != ARRAY_SIZE(msgs))
return ret < 0 ? ret : -EIO;
*val = be32_to_cpu(data_be);
return 0 ;
}
static int __ov08x40_burst_fill_regs(struct i2c_client *client, u16 first_reg,
u16 last_reg, size_t num_regs, u8 val)
{
struct i2c_msg msgs;
size_t i;
int ret;
msgs.addr = client->addr;
msgs.flags = 0 ;
msgs.len = 2 + num_regs;
msgs.buf = kmalloc(msgs.len, GFP_KERNEL);
if (!msgs.buf)
return -ENOMEM;
put_unaligned_be16(first_reg, msgs.buf);
for (i = 0 ; i < num_regs; ++i)
msgs.buf[2 + i] = val;
ret = i2c_transfer(client->adapter, &msgs, 1 );
kfree(msgs.buf);
if (ret != 1 ) {
dev_err(&client->dev, "Failed regs transferred: %d\n" , ret);
return -EIO;
}
return 0 ;
}
static int ov08x40_burst_fill_regs(struct ov08x40 *ov08x, u16 first_reg,
u16 last_reg, u8 val)
{
struct i2c_client *client = v4l2_get_subdevdata(&ov08x->sd);
size_t num_regs, num_write_regs;
int ret;
num_regs = last_reg - first_reg + 1 ;
num_write_regs = num_regs;
if (client->adapter->quirks && client->adapter->quirks->max_write_len)
num_write_regs = client->adapter->quirks->max_write_len - 2 ;
while (first_reg < last_reg) {
ret = __ov08x40_burst_fill_regs(client, first_reg, last_reg,
num_write_regs, val);
if (ret)
return ret;
first_reg += num_write_regs;
}
return 0 ;
}
/* Write registers up to 4 at a time */
static int ov08x40_write_reg(struct ov08x40 *ov08x,
u16 reg, u32 len, u32 __val)
{
struct i2c_client *client = v4l2_get_subdevdata(&ov08x->sd);
int buf_i, val_i, ret;
u8 buf[6 ], *val_p;
__be32 val;
if (len > 4 )
return -EINVAL;
buf[0 ] = reg >> 8 ;
buf[1 ] = reg & 0 xff;
val = cpu_to_be32(__val);
val_p = (u8 *)&val;
buf_i = 2 ;
val_i = 4 - len;
while (val_i < 4 )
buf[buf_i++] = val_p[val_i++];
ret = i2c_master_send(client, buf, len + 2 );
if (ret != len + 2 )
return ret < 0 ? ret : -EIO;
return 0 ;
}
/* Write a list of registers */
static int ov08x40_write_regs(struct ov08x40 *ov08x,
const struct ov08x40_reg *regs, u32 len)
{
struct i2c_client *client = v4l2_get_subdevdata(&ov08x->sd);
int ret;
u32 i;
for (i = 0 ; i < len; i++) {
ret = ov08x40_write_reg(ov08x, regs[i].address, 1 ,
regs[i].val);
if (ret) {
dev_err_ratelimited(&client->dev,
"Failed to write reg 0x%4.4x. error = %d\n" ,
regs[i].address, ret);
return ret;
}
}
return 0 ;
}
static int ov08x40_write_reg_list(struct ov08x40 *ov08x,
const struct ov08x40_reg_list *r_list)
{
return ov08x40_write_regs(ov08x, r_list->regs, r_list->num_of_regs);
}
static int ov08x40_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
{
const struct ov08x40_mode *default_mode = &supported_modes[0 ];
struct ov08x40 *ov08x = to_ov08x40(sd);
struct v4l2_mbus_framefmt *try_fmt =
v4l2_subdev_state_get_format(fh->state, 0 );
mutex_lock(&ov08x->mutex);
/* Initialize try_fmt */
try_fmt->width = default_mode->width;
try_fmt->height = default_mode->height;
try_fmt->code = MEDIA_BUS_FMT_SGRBG10_1X10;
try_fmt->field = V4L2_FIELD_NONE;
/* No crop or compose */
mutex_unlock(&ov08x->mutex);
return 0 ;
}
static int ov08x40_update_digital_gain(struct ov08x40 *ov08x, u32 d_gain)
{
int ret;
u32 val;
/*
* 0x350C[1:0], 0x350B[7:0], 0x350A[4:0]
*/
val = (d_gain & OV08X40_DGTL_GAIN_L_MASK) << OV08X40_DGTL_GAIN_L_SHIFT;
ret = ov08x40_write_reg(ov08x, OV08X40_REG_DGTL_GAIN_L,
OV08X40_REG_VALUE_08BIT, val);
if (ret)
return ret;
val = (d_gain >> OV08X40_DGTL_GAIN_M_SHIFT) & OV08X40_DGTL_GAIN_M_MASK;
ret = ov08x40_write_reg(ov08x, OV08X40_REG_DGTL_GAIN_M,
OV08X40_REG_VALUE_08BIT, val);
if (ret)
return ret;
val = (d_gain >> OV08X40_DGTL_GAIN_H_SHIFT) & OV08X40_DGTL_GAIN_H_MASK;
return ov08x40_write_reg(ov08x, OV08X40_REG_DGTL_GAIN_H,
OV08X40_REG_VALUE_08BIT, val);
}
static int ov08x40_enable_test_pattern(struct ov08x40 *ov08x, u32 pattern)
{
int ret;
u32 val;
ret = ov08x40_read_reg(ov08x, OV08X40_REG_TEST_PATTERN,
OV08X40_REG_VALUE_08BIT, &val);
if (ret)
return ret;
if (pattern) {
ret = ov08x40_read_reg(ov08x, OV08X40_REG_ISP,
OV08X40_REG_VALUE_08BIT, &val);
if (ret)
return ret;
ret = ov08x40_write_reg(ov08x, OV08X40_REG_ISP,
OV08X40_REG_VALUE_08BIT,
val | BIT(1 ));
if (ret)
return ret;
ret = ov08x40_read_reg(ov08x, OV08X40_REG_SHORT_TEST_PATTERN,
OV08X40_REG_VALUE_08BIT, &val);
if (ret)
return ret;
ret = ov08x40_write_reg(ov08x, OV08X40_REG_SHORT_TEST_PATTERN,
OV08X40_REG_VALUE_08BIT,
val | BIT(0 ));
if (ret)
return ret;
ret = ov08x40_read_reg(ov08x, OV08X40_REG_TEST_PATTERN,
OV08X40_REG_VALUE_08BIT, &val);
if (ret)
return ret;
val &= OV08X40_TEST_PATTERN_MASK;
val |= ((pattern - 1 ) << OV08X40_TEST_PATTERN_BAR_SHIFT) |
OV08X40_TEST_PATTERN_ENABLE;
} else {
val &= ~OV08X40_TEST_PATTERN_ENABLE;
}
return ov08x40_write_reg(ov08x, OV08X40_REG_TEST_PATTERN,
OV08X40_REG_VALUE_08BIT, val);
}
static int ov08x40_set_ctrl_hflip(struct ov08x40 *ov08x, u32 ctrl_val)
{
int ret;
u32 val;
ret = ov08x40_read_reg(ov08x, OV08X40_REG_MIRROR,
OV08X40_REG_VALUE_08BIT, &val);
if (ret)
return ret;
return ov08x40_write_reg(ov08x, OV08X40_REG_MIRROR,
OV08X40_REG_VALUE_08BIT,
ctrl_val ? val & ~BIT(2 ) : val | BIT(2 ));
}
static int ov08x40_set_ctrl_vflip(struct ov08x40 *ov08x, u32 ctrl_val)
{
int ret;
u32 val;
ret = ov08x40_read_reg(ov08x, OV08X40_REG_VFLIP,
OV08X40_REG_VALUE_08BIT, &val);
if (ret)
return ret;
return ov08x40_write_reg(ov08x, OV08X40_REG_VFLIP,
OV08X40_REG_VALUE_08BIT,
ctrl_val ? val | BIT(2 ) : val & ~BIT(2 ));
}
static int ov08x40_set_ctrl(struct v4l2_ctrl *ctrl)
{
struct ov08x40 *ov08x = container_of(ctrl->handler,
struct ov08x40, ctrl_handler);
struct i2c_client *client = v4l2_get_subdevdata(&ov08x->sd);
s64 max;
int exp;
int fll;
int ret = 0 ;
/* Propagate change of current control to all related controls */
switch (ctrl->id) {
case V4L2_CID_VBLANK:
/* Update max exposure while meeting expected vblanking */
/*
* because in normal mode, 1 HTS = 0.5 tline
* fps = sclk / hts / vts
* so the vts value needs to be double
*/
max = ((ov08x->cur_mode->height + ctrl->val) <<
ov08x->cur_mode->exposure_shift) -
ov08x->cur_mode->exposure_margin;
__v4l2_ctrl_modify_range(ov08x->exposure,
ov08x->exposure->minimum,
max, ov08x->exposure->step, max);
break ;
}
/*
* Applying V4L2 control value only happens
* when power is up for streaming
*/
if (!pm_runtime_get_if_in_use(&client->dev))
return 0 ;
switch (ctrl->id) {
case V4L2_CID_ANALOGUE_GAIN:
ret = ov08x40_write_reg(ov08x, OV08X40_REG_ANALOG_GAIN,
OV08X40_REG_VALUE_16BIT,
ctrl->val << 1 );
break ;
case V4L2_CID_DIGITAL_GAIN:
ret = ov08x40_update_digital_gain(ov08x, ctrl->val);
break ;
case V4L2_CID_EXPOSURE:
exp = (ctrl->val << ov08x->cur_mode->exposure_shift) -
ov08x->cur_mode->exposure_margin;
ret = ov08x40_write_reg(ov08x, OV08X40_REG_EXPOSURE,
OV08X40_REG_VALUE_24BIT,
exp);
break ;
case V4L2_CID_VBLANK:
fll = ((ov08x->cur_mode->height + ctrl->val) <<
ov08x->cur_mode->exposure_shift);
ret = ov08x40_write_reg(ov08x, OV08X40_REG_VTS,
OV08X40_REG_VALUE_16BIT,
fll);
break ;
case V4L2_CID_TEST_PATTERN:
ret = ov08x40_enable_test_pattern(ov08x, ctrl->val);
break ;
case V4L2_CID_HFLIP:
ov08x40_set_ctrl_hflip(ov08x, ctrl->val);
break ;
case V4L2_CID_VFLIP:
ov08x40_set_ctrl_vflip(ov08x, ctrl->val);
break ;
default :
dev_info(&client->dev,
"ctrl(id:0x%x,val:0x%x) is not handled\n" ,
ctrl->id, ctrl->val);
break ;
}
pm_runtime_put(&client->dev);
return ret;
}
static bool filter_by_mipi_lanes(const void *array, size_t index,
const void *context)
{
const struct ov08x40_mode *mode = array;
const struct ov08x40 *ov08x = context;
return mode->lanes == ov08x->mipi_lanes;
}
static const struct v4l2_ctrl_ops ov08x40_ctrl_ops = {
.s_ctrl = ov08x40_set_ctrl,
};
static int ov08x40_enum_mbus_code(struct v4l2_subdev *sd,
struct v4l2_subdev_state *sd_state,
struct v4l2_subdev_mbus_code_enum *code)
{
/* Only one bayer order(GRBG) is supported */
if (code->index > 0 )
return -EINVAL;
code->code = MEDIA_BUS_FMT_SGRBG10_1X10;
return 0 ;
}
static int ov08x40_enum_frame_size(struct v4l2_subdev *sd,
struct v4l2_subdev_state *sd_state,
struct v4l2_subdev_frame_size_enum *fse)
{
struct ov08x40 *ov08x = to_ov08x40(sd);
size_t i, count = 0 ;
if (fse->code != MEDIA_BUS_FMT_SGRBG10_1X10)
return -EINVAL;
for (i = 0 ; i < ARRAY_SIZE(supported_modes); i++) {
if (!filter_by_mipi_lanes(&supported_modes[i], i, ov08x))
continue ;
if (count == fse->index) {
fse->min_width = supported_modes[i].width;
fse->max_width = fse->min_width;
fse->min_height = supported_modes[i].height;
fse->max_height = fse->min_height;
return 0 ;
}
count++;
}
return -EINVAL;
}
static void ov08x40_update_pad_format(const struct ov08x40_mode *mode,
struct v4l2_subdev_format *fmt)
{
fmt->format.width = mode->width;
fmt->format.height = mode->height;
fmt->format.code = MEDIA_BUS_FMT_SGRBG10_1X10;
fmt->format.field = V4L2_FIELD_NONE;
}
static int ov08x40_do_get_pad_format(struct ov08x40 *ov08x,
struct v4l2_subdev_state *sd_state,
struct v4l2_subdev_format *fmt)
{
struct v4l2_mbus_framefmt *framefmt;
if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
framefmt = v4l2_subdev_state_get_format(sd_state, fmt->pad);
fmt->format = *framefmt;
} else {
ov08x40_update_pad_format(ov08x->cur_mode, fmt);
}
return 0 ;
}
static int ov08x40_get_pad_format(struct v4l2_subdev *sd,
struct v4l2_subdev_state *sd_state,
struct v4l2_subdev_format *fmt)
{
struct ov08x40 *ov08x = to_ov08x40(sd);
int ret;
mutex_lock(&ov08x->mutex);
ret = ov08x40_do_get_pad_format(ov08x, sd_state, fmt);
mutex_unlock(&ov08x->mutex);
return ret;
}
static int
ov08x40_set_pad_format(struct v4l2_subdev *sd,
struct v4l2_subdev_state *sd_state,
struct v4l2_subdev_format *fmt)
{
struct ov08x40 *ov08x = to_ov08x40(sd);
const struct ov08x40_mode *mode;
struct v4l2_mbus_framefmt *framefmt;
s32 vblank_def;
s32 vblank_min;
s64 h_blank;
s64 pixel_rate;
s64 link_freq;
u64 steps;
mutex_lock(&ov08x->mutex);
/* Only one raw bayer(GRBG) order is supported */
if (fmt->format.code != MEDIA_BUS_FMT_SGRBG10_1X10)
fmt->format.code = MEDIA_BUS_FMT_SGRBG10_1X10;
mode = v4l2_find_nearest_size_conditional(supported_modes,
ARRAY_SIZE(supported_modes),
width, height,
fmt->format.width,
fmt->format.height,
filter_by_mipi_lanes,
ov08x);
ov08x40_update_pad_format(mode, fmt);
if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
framefmt = v4l2_subdev_state_get_format(sd_state, fmt->pad);
*framefmt = fmt->format;
} else {
ov08x->cur_mode = mode;
__v4l2_ctrl_s_ctrl(ov08x->link_freq, mode->link_freq_index);
link_freq = link_freq_menu_items[mode->link_freq_index];
pixel_rate = link_freq_to_pixel_rate(link_freq);
__v4l2_ctrl_s_ctrl_int64(ov08x->pixel_rate, pixel_rate);
/* Update limits and set FPS to default */
vblank_def = ov08x->cur_mode->vts_def -
ov08x->cur_mode->height;
vblank_min = ov08x->cur_mode->vts_min -
ov08x->cur_mode->height;
/*
* The frame length line should be aligned to a multiple of 4,
* as provided by the sensor vendor, in normal mode.
*/
steps = mode->exposure_shift == 1 ? 4 : 1 ;
__v4l2_ctrl_modify_range(ov08x->vblank, vblank_min,
OV08X40_VTS_MAX
- ov08x->cur_mode->height,
steps,
vblank_def);
__v4l2_ctrl_s_ctrl(ov08x->vblank, vblank_def);
h_blank = ov08x->cur_mode->llp - ov08x->cur_mode->width;
__v4l2_ctrl_modify_range(ov08x->hblank, h_blank,
h_blank, 1 , h_blank);
}
mutex_unlock(&ov08x->mutex);
return 0 ;
}
static int ov08x40_start_streaming(struct ov08x40 *ov08x)
{
struct i2c_client *client = v4l2_get_subdevdata(&ov08x->sd);
const struct ov08x40_reg_list *reg_list;
int ret, link_freq_index;
/* Get out of from software reset */
ret = ov08x40_write_reg(ov08x, OV08X40_REG_SOFTWARE_RST,
OV08X40_REG_VALUE_08BIT, OV08X40_SOFTWARE_RST);
if (ret) {
dev_err(&client->dev, "%s failed to set powerup registers\n" ,
__func__);
return ret;
}
link_freq_index = ov08x->cur_mode->link_freq_index;
reg_list = &link_freq_configs[link_freq_index].reg_list;
ret = ov08x40_write_reg_list(ov08x, reg_list);
if (ret) {
dev_err(&client->dev, "%s failed to set plls\n" , __func__);
return ret;
}
reg_list = &ov08x40_global_setting;
ret = ov08x40_write_reg_list(ov08x, reg_list);
if (ret) {
dev_err(&client->dev, "%s failed to set global setting\n" ,
__func__);
return ret;
}
/* Apply default values of current mode */
reg_list = &ov08x->cur_mode->reg_list;
ret = ov08x40_write_reg_list(ov08x, reg_list);
if (ret) {
dev_err(&client->dev, "%s failed to set mode\n" , __func__);
return ret;
}
/* Use i2c burst to write register on full size registers */
if (ov08x->cur_mode->exposure_shift == 1 ) {
ret = ov08x40_burst_fill_regs(ov08x, OV08X40_REG_XTALK_FIRST_A,
OV08X40_REG_XTALK_LAST_A, 0 x75);
if (ret == 0 )
ret = ov08x40_burst_fill_regs(ov08x,
OV08X40_REG_XTALK_FIRST_B,
OV08X40_REG_XTALK_LAST_B,
0 x75);
}
if (ret) {
dev_err(&client->dev, "%s failed to set regs\n" , __func__);
return ret;
}
/* Apply customized values from user */
ret = __v4l2_ctrl_handler_setup(ov08x->sd.ctrl_handler);
if (ret)
return ret;
return ov08x40_write_reg(ov08x, OV08X40_REG_MODE_SELECT,
OV08X40_REG_VALUE_08BIT,
OV08X40_MODE_STREAMING);
}
/* Stop streaming */
static int ov08x40_stop_streaming(struct ov08x40 *ov08x)
{
return ov08x40_write_reg(ov08x, OV08X40_REG_MODE_SELECT,
OV08X40_REG_VALUE_08BIT, OV08X40_MODE_STANDBY);
}
/* Verify chip ID */
static int ov08x40_identify_module(struct ov08x40 *ov08x)
{
struct i2c_client *client = v4l2_get_subdevdata(&ov08x->sd);
int ret;
u32 val;
if (ov08x->identified)
return 0 ;
ret = ov08x40_read_reg(ov08x, OV08X40_REG_CHIP_ID,
OV08X40_REG_VALUE_24BIT, &val);
if (ret) {
dev_err(&client->dev, "error reading chip-id register: %d\n" , ret);
return ret;
}
if (val != OV08X40_CHIP_ID) {
dev_err(&client->dev, "chip id mismatch: %x!=%x\n" ,
OV08X40_CHIP_ID, val);
return -ENXIO;
}
dev_dbg(&client->dev, "chip id 0x%x\n" , val);
ov08x->identified = true ;
return 0 ;
}
static int ov08x40_set_stream(struct v4l2_subdev *sd, int enable)
{
struct ov08x40 *ov08x = to_ov08x40(sd);
struct i2c_client *client = v4l2_get_subdevdata(sd);
int ret = 0 ;
mutex_lock(&ov08x->mutex);
if (enable) {
ret = pm_runtime_resume_and_get(&client->dev);
if (ret < 0 )
goto err_unlock;
ret = ov08x40_identify_module(ov08x);
if (ret)
goto err_rpm_put;
/*
* Apply default & customized values
* and then start streaming.
*/
ret = ov08x40_start_streaming(ov08x);
if (ret)
goto err_rpm_put;
} else {
ov08x40_stop_streaming(ov08x);
pm_runtime_put(&client->dev);
}
mutex_unlock(&ov08x->mutex);
return ret;
err_rpm_put:
pm_runtime_put(&client->dev);
err_unlock:
mutex_unlock(&ov08x->mutex);
return ret;
}
static const struct v4l2_subdev_video_ops ov08x40_video_ops = {
.s_stream = ov08x40_set_stream,
};
static const struct v4l2_subdev_pad_ops ov08x40_pad_ops = {
.enum_mbus_code = ov08x40_enum_mbus_code,
.get_fmt = ov08x40_get_pad_format,
.set_fmt = ov08x40_set_pad_format,
.enum_frame_size = ov08x40_enum_frame_size,
};
static const struct v4l2_subdev_ops ov08x40_subdev_ops = {
.video = &ov08x40_video_ops,
.pad = &ov08x40_pad_ops,
};
static const struct media_entity_operations ov08x40_subdev_entity_ops = {
.link_validate = v4l2_subdev_link_validate,
};
static const struct v4l2_subdev_internal_ops ov08x40_internal_ops = {
.open = ov08x40_open,
};
static int ov08x40_init_controls(struct ov08x40 *ov08x)
{
struct i2c_client *client = v4l2_get_subdevdata(&ov08x->sd);
struct v4l2_fwnode_device_properties props;
struct v4l2_ctrl_handler *ctrl_hdlr;
s64 exposure_max;
s64 vblank_def;
s64 vblank_min;
s64 hblank;
s64 pixel_rate_min;
s64 pixel_rate_max;
const struct ov08x40_mode *mode;
int ret;
ctrl_hdlr = &ov08x->ctrl_handler;
ret = v4l2_ctrl_handler_init(ctrl_hdlr, 10 );
if (ret)
return ret;
mutex_init(&ov08x->mutex);
ctrl_hdlr->lock = &ov08x->mutex;
ov08x->link_freq = v4l2_ctrl_new_int_menu(ctrl_hdlr,
&ov08x40_ctrl_ops,
V4L2_CID_LINK_FREQ,
__fls(ov08x->link_freq_bitmap),
__ffs(ov08x->link_freq_bitmap),
link_freq_menu_items);
if (ov08x->link_freq)
ov08x->link_freq->flags |= V4L2_CTRL_FLAG_READ_ONLY;
pixel_rate_max = link_freq_to_pixel_rate(link_freq_menu_items[0 ]);
pixel_rate_min = 0 ;
/* By default, PIXEL_RATE is read only */
ov08x->pixel_rate = v4l2_ctrl_new_std(ctrl_hdlr, &ov08x40_ctrl_ops,
V4L2_CID_PIXEL_RATE,
pixel_rate_min, pixel_rate_max,
1 , pixel_rate_max);
mode = ov08x->cur_mode;
vblank_def = mode->vts_def - mode->height;
vblank_min = mode->vts_min - mode->height;
ov08x->vblank = v4l2_ctrl_new_std(ctrl_hdlr, &ov08x40_ctrl_ops,
V4L2_CID_VBLANK,
vblank_min,
OV08X40_VTS_MAX - mode->height, 1 ,
vblank_def);
hblank = ov08x->cur_mode->llp - ov08x->cur_mode->width;
ov08x->hblank = v4l2_ctrl_new_std(ctrl_hdlr, &ov08x40_ctrl_ops,
V4L2_CID_HBLANK,
hblank, hblank, 1 , hblank);
if (ov08x->hblank)
ov08x->hblank->flags |= V4L2_CTRL_FLAG_READ_ONLY;
exposure_max = mode->vts_def - OV08X40_EXPOSURE_MAX_MARGIN;
ov08x->exposure = v4l2_ctrl_new_std(ctrl_hdlr, &ov08x40_ctrl_ops,
V4L2_CID_EXPOSURE,
OV08X40_EXPOSURE_MIN,
exposure_max, OV08X40_EXPOSURE_STEP,
exposure_max);
v4l2_ctrl_new_std(ctrl_hdlr, &ov08x40_ctrl_ops, V4L2_CID_ANALOGUE_GAIN,
OV08X40_ANA_GAIN_MIN, OV08X40_ANA_GAIN_MAX,
OV08X40_ANA_GAIN_STEP, OV08X40_ANA_GAIN_DEFAULT);
/* Digital gain */
v4l2_ctrl_new_std(ctrl_hdlr, &ov08x40_ctrl_ops, V4L2_CID_DIGITAL_GAIN,
OV08X40_DGTL_GAIN_MIN, OV08X40_DGTL_GAIN_MAX,
OV08X40_DGTL_GAIN_STEP, OV08X40_DGTL_GAIN_DEFAULT);
v4l2_ctrl_new_std_menu_items(ctrl_hdlr, &ov08x40_ctrl_ops,
V4L2_CID_TEST_PATTERN,
ARRAY_SIZE(ov08x40_test_pattern_menu) - 1 ,
0 , 0 , ov08x40_test_pattern_menu);
v4l2_ctrl_new_std(ctrl_hdlr, &ov08x40_ctrl_ops,
V4L2_CID_HFLIP, 0 , 1 , 1 , 0 );
v4l2_ctrl_new_std(ctrl_hdlr, &ov08x40_ctrl_ops,
V4L2_CID_VFLIP, 0 , 1 , 1 , 0 );
if (ctrl_hdlr->error) {
ret = ctrl_hdlr->error;
dev_err(&client->dev, "%s control init failed (%d)\n" ,
__func__, ret);
goto error;
}
ret = v4l2_fwnode_device_parse(&client->dev, &props);
if (ret)
goto error;
ret = v4l2_ctrl_new_fwnode_properties(ctrl_hdlr, &ov08x40_ctrl_ops,
&props);
if (ret)
goto error;
ov08x->sd.ctrl_handler = ctrl_hdlr;
return 0 ;
error:
v4l2_ctrl_handler_free(ctrl_hdlr);
mutex_destroy(&ov08x->mutex);
return ret;
}
static void ov08x40_free_controls(struct ov08x40 *ov08x)
{
v4l2_ctrl_handler_free(ov08x->sd.ctrl_handler);
mutex_destroy(&ov08x->mutex);
}
static int ov08x40_check_hwcfg(struct ov08x40 *ov08x, struct device *dev)
{
struct v4l2_fwnode_endpoint bus_cfg = {
.bus_type = V4L2_MBUS_CSI2_DPHY
};
struct fwnode_handle *ep;
struct fwnode_handle *fwnode = dev_fwnode(dev);
unsigned int i;
int ret;
u32 xvclk_rate;
/*
* Sometimes the fwnode graph is initialized by the bridge driver.
* Bridge drivers doing this also add sensor properties, wait for this.
*/
ep = fwnode_graph_get_next_endpoint(fwnode, NULL);
if (!ep)
return dev_err_probe(dev, -EPROBE_DEFER,
"waiting for fwnode graph endpoint\n" );
ret = v4l2_fwnode_endpoint_alloc_parse(ep, &bus_cfg);
fwnode_handle_put(ep);
if (ret)
return dev_err_probe(dev, ret, "parsing endpoint failed\n" );
ov08x->reset_gpio = devm_gpiod_get_optional(dev, "reset" ,
GPIOD_OUT_HIGH);
if (IS_ERR(ov08x->reset_gpio)) {
ret = dev_err_probe(dev, PTR_ERR(ov08x->reset_gpio),
"getting reset GPIO\n" );
goto out_err;
}
for (i = 0 ; i < ARRAY_SIZE(ov08x40_supply_names); i++)
ov08x->supplies[i].supply = ov08x40_supply_names[i];
ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(ov08x40_supply_names),
ov08x->supplies);
if (ret)
goto out_err;
ov08x->xvclk = devm_clk_get_optional(dev, NULL);
if (IS_ERR(ov08x->xvclk)) {
ret = dev_err_probe(dev, PTR_ERR(ov08x->xvclk),
"getting xvclk\n" );
goto out_err;
}
if (ov08x->xvclk) {
xvclk_rate = clk_get_rate(ov08x->xvclk);
} else {
ret = fwnode_property_read_u32(dev_fwnode(dev), "clock-frequency" ,
&xvclk_rate);
if (ret) {
dev_err(dev, "can't get clock frequency\n" );
goto out_err;
}
}
if (xvclk_rate != OV08X40_XVCLK) {
dev_err(dev, "external clock %d is not supported\n" ,
xvclk_rate);
ret = -EINVAL;
goto out_err;
}
switch (bus_cfg.bus.mipi_csi2.num_data_lanes) {
case 2 :
case 4 :
ov08x->mipi_lanes = bus_cfg.bus.mipi_csi2.num_data_lanes;
break ;
default :
dev_err(dev, "number of CSI2 data lanes %d is not supported\n" ,
bus_cfg.bus.mipi_csi2.num_data_lanes);
ret = -EINVAL;
goto out_err;
}
if (!bus_cfg.nr_of_link_frequencies) {
dev_err(dev, "no link frequencies defined\n" );
ret = -EINVAL;
goto out_err;
}
ret = v4l2_link_freq_to_bitmap(dev, bus_cfg.link_frequencies,
bus_cfg.nr_of_link_frequencies,
link_freq_menu_items,
ARRAY_SIZE(link_freq_menu_items),
&ov08x->link_freq_bitmap);
out_err:
v4l2_fwnode_endpoint_free(&bus_cfg);
return ret;
}
static int ov08x40_probe(struct i2c_client *client)
{ struct ov08x40 *ov08x;
int ret;
bool full_power;
ov08x = devm_kzalloc(&client->dev, sizeof (*ov08x), GFP_KERNEL);
if (!ov08x)
return -ENOMEM;
/* Check HW config */
ret = ov08x40_check_hwcfg(ov08x, &client->dev);
if (ret)
return ret;
/* Initialize subdev */
v4l2_i2c_subdev_init(&ov08x->sd, client, &ov08x40_subdev_ops);
full_power = acpi_dev_state_d0(&client->dev);
if (full_power) {
ret = ov08x40_power_on(&client->dev);
if (ret) {
dev_err(&client->dev, "failed to power on\n" );
return ret;
}
/* Check module identity */
ret = ov08x40_identify_module(ov08x);
if (ret)
goto probe_power_off;
}
/* Set default mode to max resolution */
ov08x->cur_mode = &supported_modes[0 ];
ret = ov08x40_init_controls(ov08x);
if (ret)
goto probe_power_off;
/* Initialize subdev */
ov08x->sd.internal_ops = &ov08x40_internal_ops;
ov08x->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
ov08x->sd.entity.ops = &ov08x40_subdev_entity_ops;
ov08x->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
/* Initialize source pad */
ov08x->pad.flags = MEDIA_PAD_FL_SOURCE;
ret = media_entity_pads_init(&ov08x->sd.entity, 1 , &ov08x->pad);
if (ret) {
dev_err(&client->dev, "%s failed:%d\n" , __func__, ret);
goto error_handler_free;
}
ret = v4l2_async_register_subdev_sensor(&ov08x->sd);
if (ret < 0 )
goto error_media_entity;
if (full_power)
pm_runtime_set_active(&client->dev);
pm_runtime_enable(&client->dev);
pm_runtime_idle(&client->dev);
return 0 ;
error_media_entity:
media_entity_cleanup(&ov08x->sd.entity);
error_handler_free:
ov08x40_free_controls(ov08x);
probe_power_off:
ov08x40_power_off(&client->dev);
return ret;
}
static void ov08x40_remove(struct i2c_client *client)
{
struct v4l2_subdev *sd = i2c_get_clientdata(client);
struct ov08x40 *ov08x = to_ov08x40(sd);
v4l2_async_unregister_subdev(sd);
media_entity_cleanup(&sd->entity);
ov08x40_free_controls(ov08x);
pm_runtime_disable(&client->dev);
if (!pm_runtime_status_suspended(&client->dev))
ov08x40_power_off(&client->dev);
pm_runtime_set_suspended(&client->dev);
}
static DEFINE_RUNTIME_DEV_PM_OPS(ov08x40_pm_ops, ov08x40_power_off,
ov08x40_power_on, NULL);
#ifdef CONFIG_ACPI
static const struct acpi_device_id ov08x40_acpi_ids[] = {
{"OVTI08F4" },
{ /* sentinel */ }
};
MODULE_DEVICE_TABLE(acpi, ov08x40_acpi_ids);
#endif
static const struct of_device_id ov08x40_of_match[] = {
{ .compatible = "ovti,ov08x40" },
{ /* sentinel */ }
};
MODULE_DEVICE_TABLE(of, ov08x40_of_match);
static struct i2c_driver ov08x40_i2c_driver = {
.driver = {
.name = "ov08x40" ,
.acpi_match_table = ACPI_PTR(ov08x40_acpi_ids),
.of_match_table = ov08x40_of_match,
.pm = pm_sleep_ptr(&ov08x40_pm_ops),
},
.probe = ov08x40_probe,
.remove = ov08x40_remove,
.flags = I2C_DRV_ACPI_WAIVE_D0_PROBE,
};
module_i2c_driver(ov08x40_i2c_driver);
MODULE_AUTHOR("Jason Chen <jason.z.chen@intel.com>" );
MODULE_AUTHOR("Qingwu Zhang <qingwu.zhang@intel.com>" );
MODULE_AUTHOR("Shawn Tu" );
MODULE_DESCRIPTION("OmniVision OV08X40 sensor driver" );
MODULE_LICENSE("GPL" );
Messung V0.5 in Prozent C=94 H=92 G=92
¤ Dauer der Verarbeitung: 0.40 Sekunden
(vorverarbeitet am 2026-06-08)
¤
*© Formatika GbR, Deutschland