From d079e6543523ece011e6fb552a9d8d4b027d92c1 Mon Sep 17 00:00:00 2001 From: Jianwei Fan Date: Wed, 9 Mar 2022 15:49:27 +0800 Subject: [PATCH] media: i2c: imx214: adapt to kernel-5.10 and add 4lane mode support Signed-off-by: Jianwei Fan Change-Id: I05129ea47403f173fda55ddc4ccccc9fce5791f8 --- drivers/media/i2c/Kconfig | 8 + drivers/media/i2c/Makefile | 1 + drivers/media/i2c/imx214.c | 2578 ++++++++++++++++-------- drivers/media/i2c/imx214_eeprom.c | 377 ++++ drivers/media/i2c/imx214_eeprom_head.h | 61 + 5 files changed, 2163 insertions(+), 862 deletions(-) create mode 100644 drivers/media/i2c/imx214_eeprom.c create mode 100644 drivers/media/i2c/imx214_eeprom_head.h diff --git a/drivers/media/i2c/Kconfig b/drivers/media/i2c/Kconfig index b5371bf17c85..ef4bf1403e42 100644 --- a/drivers/media/i2c/Kconfig +++ b/drivers/media/i2c/Kconfig @@ -965,6 +965,14 @@ config VIDEO_IMX214 To compile this driver as a module, choose M here: the module will be called imx214. +config VIDEO_IMX214_EEPROM + tristate "Sony imx214 sensor otp from eeprom support" + depends on VIDEO_V4L2 && I2C + depends on VIDEO_IMX214 + select V4L2_FWNODE + help + This driver supports IMX214 OTP load from eeprom. + config VIDEO_IMX219 tristate "Sony IMX219 sensor support" depends on I2C && VIDEO_V4L2 diff --git a/drivers/media/i2c/Makefile b/drivers/media/i2c/Makefile index 3ad270431aa9..cd7ea4ac3fb3 100644 --- a/drivers/media/i2c/Makefile +++ b/drivers/media/i2c/Makefile @@ -151,6 +151,7 @@ obj-$(CONFIG_VIDEO_GC5025) += gc5025.o obj-$(CONFIG_VIDEO_GC8034) += gc8034.o obj-$(CONFIG_VIDEO_HI556) += hi556.o obj-$(CONFIG_VIDEO_IMX214) += imx214.o +obj-$(CONFIG_VIDEO_IMX214_EEPROM) += imx214_eeprom.o obj-$(CONFIG_VIDEO_IMX219) += imx219.o obj-$(CONFIG_VIDEO_IMX258) += imx258.o obj-$(CONFIG_VIDEO_IMX258_EEPROM) += imx258_eeprom.o diff --git a/drivers/media/i2c/imx214.c b/drivers/media/i2c/imx214.c index cee1a4817af9..065ae3dfca05 100644 --- a/drivers/media/i2c/imx214.c +++ b/drivers/media/i2c/imx214.c @@ -1,309 +1,194 @@ // SPDX-License-Identifier: GPL-2.0 /* - * imx214.c - imx214 sensor driver + * imx214 camera driver * - * Copyright 2018 Qtechnology A/S + * Copyright (C) 2022 Rockchip Electronics Co., Ltd. + * + * V0.0X01.0X00 first version. + * V0.0X01.0X01 fix compile errors. + * V0.0X01.0X02 add 4lane mode support. * - * Ricardo Ribalda */ + #include +#include #include #include #include #include #include -#include #include +#include +#include +#include +#include #include +#include #include -#include #include +#include +#include +#include +#include +#include +#include "imx214_eeprom_head.h" -#define IMX214_DEFAULT_CLK_FREQ 24000000 -#define IMX214_DEFAULT_LINK_FREQ 480000000 -#define IMX214_DEFAULT_PIXEL_RATE ((IMX214_DEFAULT_LINK_FREQ * 8LL) / 10) -#define IMX214_FPS 30 -#define IMX214_MBUS_CODE MEDIA_BUS_FMT_SRGGB10_1X10 +#define DRIVER_VERSION KERNEL_VERSION(0, 0x01, 0x02) -static const char * const imx214_supply_name[] = { - "vdda", - "vddd", - "vdddo", +#ifndef V4L2_CID_DIGITAL_GAIN +#define V4L2_CID_DIGITAL_GAIN V4L2_CID_GAIN +#endif + +#define IMX214_LINK_FREQ_600MHZ 600000000U +/* pixel rate = link frequency * 2 * lanes / BITS_PER_SAMPLE */ +#define IMX214_PIXEL_RATE (IMX214_LINK_FREQ_600MHZ * 2LL * 4LL / 10LL) +#define IMX214_XVCLK_FREQ 24000000 + +#define CHIP_ID 0x0214 +#define IMX214_REG_CHIP_ID 0x0016 + +#define IMX214_REG_CTRL_MODE 0x0100 +#define IMX214_MODE_SW_STANDBY 0x0 +#define IMX214_MODE_STREAMING BIT(0) + +#define IMX214_REG_EXPOSURE 0x0202 +#define IMX214_EXPOSURE_MIN 4 +#define IMX214_EXPOSURE_STEP 1 +#define IMX214_VTS_MAX 0xffff + +#define IMX214_REG_GAIN_H 0x0204 +#define IMX214_REG_GAIN_L 0x0205 +#define IMX214_GAIN_MIN 0x200 +#define IMX214_GAIN_MAX 0x1fff +#define IMX214_GAIN_STEP 0x200 +#define IMX214_GAIN_DEFAULT 0x800 + +#define IMX214_REG_TEST_PATTERN 0x5e00 +#define IMX214_TEST_PATTERN_ENABLE 0x80 +#define IMX214_TEST_PATTERN_DISABLE 0x0 + +#define IMX214_REG_VTS 0x0340 + +#define REG_NULL 0xFFFF + +#define IMX214_REG_VALUE_08BIT 1 +#define IMX214_REG_VALUE_16BIT 2 +#define IMX214_REG_VALUE_24BIT 3 + +#define IMX214_BITS_PER_SAMPLE 10 + +#define OF_CAMERA_PINCTRL_STATE_DEFAULT "rockchip,camera_default" +#define OF_CAMERA_PINCTRL_STATE_SLEEP "rockchip,camera_sleep" + +#define IMX214_NAME "imx214" +#define IMX214_MEDIA_BUS_FMT MEDIA_BUS_FMT_SBGGR10_1X10 + +/* OTP MACRO */ +#define MODULE_BKX 0X01 +#define MODULE_TYPE MODULE_BKX +#if MODULE_TYPE == MODULE_BKX +#define RG_Ratio_Typical_Default (0x026e) +#define BG_Ratio_Typical_Default (0x0280) +#else +#define RG_Ratio_Typical_Default (0x16f) +#define BG_Ratio_Typical_Default (0x16f) +#endif + +static const char * const imx214_supply_names[] = { + "avdd", /* Analog power */ + "dovdd", /* Digital I/O power */ + "dvdd", /* Digital core power */ }; -#define IMX214_NUM_SUPPLIES ARRAY_SIZE(imx214_supply_name) +#define IMX214_NUM_SUPPLIES ARRAY_SIZE(imx214_supply_names) -struct imx214 { - struct device *dev; - struct clk *xclk; - struct regmap *regmap; - - struct v4l2_subdev sd; - struct media_pad pad; - struct v4l2_mbus_framefmt fmt; - struct v4l2_rect crop; - - struct v4l2_ctrl_handler ctrls; - struct v4l2_ctrl *pixel_rate; - struct v4l2_ctrl *link_freq; - struct v4l2_ctrl *exposure; - struct v4l2_ctrl *unit_size; - - struct regulator_bulk_data supplies[IMX214_NUM_SUPPLIES]; - - struct gpio_desc *enable_gpio; - - /* - * Serialize control access, get/set format, get selection - * and start streaming. - */ - struct mutex mutex; - - bool streaming; -}; - -struct reg_8 { +struct regval { u16 addr; u8 val; }; -enum { - IMX214_TABLE_WAIT_MS = 0, - IMX214_TABLE_END, - IMX214_MAX_RETRIES, - IMX214_WAIT_MS +struct imx214_mode { + u32 width; + u32 height; + struct v4l2_fract max_fps; + u32 hts_def; + u32 vts_def; + u32 exp_def; + u32 link_freq_idx; + u32 bpp; + const struct regval *reg_list; }; -/*From imx214_mode_tbls.h*/ -static const struct reg_8 mode_4096x2304[] = { - {0x0114, 0x03}, - {0x0220, 0x00}, - {0x0221, 0x11}, - {0x0222, 0x01}, - {0x0340, 0x0C}, - {0x0341, 0x7A}, - {0x0342, 0x13}, - {0x0343, 0x90}, - {0x0344, 0x00}, - {0x0345, 0x38}, - {0x0346, 0x01}, - {0x0347, 0x98}, - {0x0348, 0x10}, - {0x0349, 0x37}, - {0x034A, 0x0A}, - {0x034B, 0x97}, - {0x0381, 0x01}, - {0x0383, 0x01}, - {0x0385, 0x01}, - {0x0387, 0x01}, - {0x0900, 0x00}, - {0x0901, 0x00}, - {0x0902, 0x00}, - {0x3000, 0x35}, - {0x3054, 0x01}, - {0x305C, 0x11}, +struct imx214 { + struct i2c_client *client; + struct clk *xvclk; + struct gpio_desc *power_gpio; + struct gpio_desc *reset_gpio; + struct gpio_desc *pwdn_gpio; + struct regulator_bulk_data supplies[IMX214_NUM_SUPPLIES]; - {0x0112, 0x0A}, - {0x0113, 0x0A}, - {0x034C, 0x10}, - {0x034D, 0x00}, - {0x034E, 0x09}, - {0x034F, 0x00}, - {0x0401, 0x00}, - {0x0404, 0x00}, - {0x0405, 0x10}, - {0x0408, 0x00}, - {0x0409, 0x00}, - {0x040A, 0x00}, - {0x040B, 0x00}, - {0x040C, 0x10}, - {0x040D, 0x00}, - {0x040E, 0x09}, - {0x040F, 0x00}, + struct pinctrl *pinctrl; + struct pinctrl_state *pins_default; + struct pinctrl_state *pins_sleep; - {0x0301, 0x05}, - {0x0303, 0x02}, - {0x0305, 0x03}, - {0x0306, 0x00}, - {0x0307, 0x96}, - {0x0309, 0x0A}, - {0x030B, 0x01}, - {0x0310, 0x00}, - - {0x0820, 0x12}, - {0x0821, 0xC0}, - {0x0822, 0x00}, - {0x0823, 0x00}, - - {0x3A03, 0x09}, - {0x3A04, 0x50}, - {0x3A05, 0x01}, - - {0x0B06, 0x01}, - {0x30A2, 0x00}, - - {0x30B4, 0x00}, - - {0x3A02, 0xFF}, - - {0x3011, 0x00}, - {0x3013, 0x01}, - - {0x0202, 0x0C}, - {0x0203, 0x70}, - {0x0224, 0x01}, - {0x0225, 0xF4}, - - {0x0204, 0x00}, - {0x0205, 0x00}, - {0x020E, 0x01}, - {0x020F, 0x00}, - {0x0210, 0x01}, - {0x0211, 0x00}, - {0x0212, 0x01}, - {0x0213, 0x00}, - {0x0214, 0x01}, - {0x0215, 0x00}, - {0x0216, 0x00}, - {0x0217, 0x00}, - - {0x4170, 0x00}, - {0x4171, 0x10}, - {0x4176, 0x00}, - {0x4177, 0x3C}, - {0xAE20, 0x04}, - {0xAE21, 0x5C}, - - {IMX214_TABLE_WAIT_MS, 10}, - {0x0138, 0x01}, - {IMX214_TABLE_END, 0x00} + struct v4l2_subdev subdev; + struct media_pad pad; + struct v4l2_ctrl_handler ctrl_handler; + struct v4l2_ctrl *exposure; + struct v4l2_ctrl *anal_gain; + struct v4l2_ctrl *digi_gain; + struct v4l2_ctrl *hblank; + struct v4l2_ctrl *vblank; + struct v4l2_ctrl *pixel_rate; + struct v4l2_ctrl *link_freq; + struct v4l2_ctrl *test_pattern; + struct mutex mutex; + struct v4l2_fwnode_endpoint bus_cfg; + bool streaming; + bool power_on; + const struct imx214_mode *support_modes; + const struct imx214_mode *cur_mode; + u32 module_index; + u32 cfg_num; + const char *module_facing; + const char *module_name; + const char *len_name; + struct imx214_otp_info *otp; + struct rkmodule_inf module_inf; + struct rkmodule_awb_cfg awb_cfg; + struct rkmodule_lsc_cfg lsc_cfg; }; -static const struct reg_8 mode_1920x1080[] = { - {0x0114, 0x03}, - {0x0220, 0x00}, - {0x0221, 0x11}, - {0x0222, 0x01}, - {0x0340, 0x0C}, - {0x0341, 0x7A}, - {0x0342, 0x13}, - {0x0343, 0x90}, - {0x0344, 0x04}, - {0x0345, 0x78}, - {0x0346, 0x03}, - {0x0347, 0xFC}, - {0x0348, 0x0B}, - {0x0349, 0xF7}, - {0x034A, 0x08}, - {0x034B, 0x33}, - {0x0381, 0x01}, - {0x0383, 0x01}, - {0x0385, 0x01}, - {0x0387, 0x01}, - {0x0900, 0x00}, - {0x0901, 0x00}, - {0x0902, 0x00}, - {0x3000, 0x35}, - {0x3054, 0x01}, - {0x305C, 0x11}, +#define to_imx214(sd) container_of(sd, struct imx214, subdev) - {0x0112, 0x0A}, - {0x0113, 0x0A}, - {0x034C, 0x07}, - {0x034D, 0x80}, - {0x034E, 0x04}, - {0x034F, 0x38}, - {0x0401, 0x00}, - {0x0404, 0x00}, - {0x0405, 0x10}, - {0x0408, 0x00}, - {0x0409, 0x00}, - {0x040A, 0x00}, - {0x040B, 0x00}, - {0x040C, 0x07}, - {0x040D, 0x80}, - {0x040E, 0x04}, - {0x040F, 0x38}, - - {0x0301, 0x05}, - {0x0303, 0x02}, - {0x0305, 0x03}, - {0x0306, 0x00}, - {0x0307, 0x96}, - {0x0309, 0x0A}, - {0x030B, 0x01}, - {0x0310, 0x00}, - - {0x0820, 0x12}, - {0x0821, 0xC0}, - {0x0822, 0x00}, - {0x0823, 0x00}, - - {0x3A03, 0x04}, - {0x3A04, 0xF8}, - {0x3A05, 0x02}, - - {0x0B06, 0x01}, - {0x30A2, 0x00}, - - {0x30B4, 0x00}, - - {0x3A02, 0xFF}, - - {0x3011, 0x00}, - {0x3013, 0x01}, - - {0x0202, 0x0C}, - {0x0203, 0x70}, - {0x0224, 0x01}, - {0x0225, 0xF4}, - - {0x0204, 0x00}, - {0x0205, 0x00}, - {0x020E, 0x01}, - {0x020F, 0x00}, - {0x0210, 0x01}, - {0x0211, 0x00}, - {0x0212, 0x01}, - {0x0213, 0x00}, - {0x0214, 0x01}, - {0x0215, 0x00}, - {0x0216, 0x00}, - {0x0217, 0x00}, - - {0x4170, 0x00}, - {0x4171, 0x10}, - {0x4176, 0x00}, - {0x4177, 0x3C}, - {0xAE20, 0x04}, - {0xAE21, 0x5C}, - - {IMX214_TABLE_WAIT_MS, 10}, - {0x0138, 0x01}, - {IMX214_TABLE_END, 0x00} +struct imx214_id_name { + u32 id; + char name[RKMODULE_NAME_LEN]; }; -static const struct reg_8 mode_table_common[] = { - /* software reset */ +static const struct imx214_id_name imx214_module_info[] = { + {0x36, "GuangDongLiteArray"}, + {0x0d, "CameraKing"}, + {0x00, "Unknown"} +}; - /* software standby settings */ - {0x0100, 0x00}, - - /* ATR setting */ - {0x9300, 0x02}, - - /* external clock setting */ +static const struct imx214_id_name imx214_lens_info[] = { + {0x47, "Sunny 3923C"}, + {0x07, "Largen 9611A6"}, + {0x00, "Unknown"} +}; +/* + * Xclk 24Mhz + */ +static const struct regval imx214_global_regs[] = { {0x0136, 0x18}, {0x0137, 0x00}, - - /* global setting */ - /* basic config */ {0x0101, 0x00}, {0x0105, 0x01}, {0x0106, 0x01}, {0x4550, 0x02}, - {0x4601, 0x00}, - {0x4642, 0x05}, + {0x4601, 0x04}, + {0x4642, 0x01}, {0x6227, 0x11}, {0x6276, 0x00}, {0x900E, 0x06}, @@ -318,22 +203,20 @@ static const struct reg_8 mode_table_common[] = { {0xA80A, 0x62}, {0xA80B, 0x83}, {0xAE33, 0x00}, - - /* analog setting */ {0x4174, 0x00}, {0x4175, 0x11}, {0x4612, 0x29}, - {0x461B, 0x12}, + {0x461B, 0x2C}, {0x461F, 0x06}, {0x4635, 0x07}, {0x4637, 0x30}, {0x463F, 0x18}, {0x4641, 0x0D}, - {0x465B, 0x12}, - {0x465F, 0x11}, - {0x4663, 0x11}, - {0x4667, 0x0F}, - {0x466F, 0x0F}, + {0x465B, 0x2C}, + {0x465F, 0x2B}, + {0x4663, 0x2B}, + {0x4667, 0x24}, + {0x466F, 0x24}, {0x470E, 0x09}, {0x4909, 0xAB}, {0x490B, 0x95}, @@ -343,338 +226,1392 @@ static const struct reg_8 mode_table_common[] = { {0x4A73, 0x62}, {0x4A85, 0x00}, {0x4A87, 0xFF}, - - /* embedded data */ - {0x5041, 0x04}, {0x583C, 0x04}, {0x620E, 0x04}, {0x6EB2, 0x01}, {0x6EB3, 0x00}, {0x9300, 0x02}, - - /* imagequality */ - /* HDR setting */ - {0x3001, 0x07}, - {0x6D12, 0x3F}, - {0x6D13, 0xFF}, - {0x9344, 0x03}, - {0x9706, 0x10}, - {0x9707, 0x03}, - {0x9708, 0x03}, - {0x9E04, 0x01}, - {0x9E05, 0x00}, - {0x9E0C, 0x01}, - {0x9E0D, 0x02}, - {0x9E24, 0x00}, - {0x9E25, 0x8C}, - {0x9E26, 0x00}, - {0x9E27, 0x94}, - {0x9E28, 0x00}, - {0x9E29, 0x96}, - - /* CNR parameter setting */ - {0x69DB, 0x01}, - - /* Moire reduction */ - {0x6957, 0x01}, - - /* image enhancement */ - {0x6987, 0x17}, - {0x698A, 0x03}, - {0x698B, 0x03}, - - /* white balanace */ - {0x0B8E, 0x01}, - {0x0B8F, 0x00}, - {0x0B90, 0x01}, - {0x0B91, 0x00}, - {0x0B92, 0x01}, - {0x0B93, 0x00}, - {0x0B94, 0x01}, - {0x0B95, 0x00}, - - /* ATR setting */ - {0x6E50, 0x00}, - {0x6E51, 0x32}, - {0x9340, 0x00}, - {0x9341, 0x3C}, - {0x9342, 0x03}, - {0x9343, 0xFF}, - {IMX214_TABLE_END, 0x00} + {REG_NULL, 0x00}, }; -/* - * Declare modes in order, from biggest - * to smallest height. - */ -static const struct imx214_mode { - u32 width; - u32 height; - const struct reg_8 *reg_table; -} imx214_modes[] = { +static const struct regval imx214_2104x1560_30fps_regs_2lane[] = { + {0x0114, 0x01}, + {0x0220, 0x00}, + {0x0221, 0x11}, + {0x0222, 0x01}, + {0x0340, 0x06}, + {0x0341, 0x40}, + {0x0342, 0x13}, + {0x0343, 0x90}, + {0x0344, 0x00}, + {0x0345, 0x00}, + {0x0346, 0x00}, + {0x0347, 0x00}, + {0x0348, 0x10}, + {0x0349, 0x6F}, + {0x034A, 0x0C}, + {0x034B, 0x2F}, + {0x0381, 0x01}, + {0x0383, 0x01}, + {0x0385, 0x01}, + {0x0387, 0x01}, + {0x0900, 0x01}, + {0x0901, 0x22}, + {0x0902, 0x02}, + {0x3000, 0x35}, + {0x3054, 0x01}, + {0x305C, 0x11}, + {0x0112, 0x0A}, + {0x0113, 0x0A}, + {0x034C, 0x08}, + {0x034D, 0x38}, + {0x034E, 0x06}, + {0x034F, 0x18}, + {0x0401, 0x00}, + {0x0404, 0x00}, + {0x0405, 0x10}, + {0x0408, 0x00}, + {0x0409, 0x00}, + {0x040A, 0x00}, + {0x040B, 0x00}, + {0x040C, 0x08}, + {0x040D, 0x38}, + {0x040E, 0x06}, + {0x040F, 0x18}, + {0x0301, 0x05}, + {0x0303, 0x04}, + {0x0305, 0x03}, + {0x0306, 0x00}, + {0x0307, 0x96}, + {0x0309, 0x0A}, + {0x030B, 0x01}, + {0x0310, 0x00}, + {0x0820, 0x09}, + {0x0821, 0x60}, + {0x0822, 0x00}, + {0x0823, 0x00}, + {0x3A03, 0x06}, + {0x3A04, 0x68}, + {0x3A05, 0x01}, + {0x0B06, 0x01}, + {0x30A2, 0x00}, + {0x30B4, 0x00}, + {0x3A02, 0xFF}, + {0x3011, 0x00}, + {0x3013, 0x01}, + {0x4170, 0x00}, + {0x4171, 0x10}, + {0x4176, 0x00}, + {0x4177, 0x3C}, + {0xAE20, 0x04}, + {0xAE21, 0x5C}, + {0x0100, 0x00}, + {REG_NULL, 0x00}, +}; + +static const struct regval imx214_4208x3120_15fps_regs_2lane[] = { + {0x0114, 0x01}, + {0x0220, 0x00}, + {0x0221, 0x11}, + {0x0222, 0x01}, + {0x0340, 0x0C}, + {0x0341, 0x58}, + {0x0342, 0x13}, + {0x0343, 0x90}, + {0x0344, 0x00}, + {0x0345, 0x00}, + {0x0346, 0x00}, + {0x0347, 0x00}, + {0x0348, 0x10}, + {0x0349, 0x6F}, + {0x034A, 0x0C}, + {0x034B, 0x2F}, + {0x0381, 0x01}, + {0x0383, 0x01}, + {0x0385, 0x01}, + {0x0387, 0x01}, + {0x0900, 0x00}, + {0x0901, 0x00}, + {0x0902, 0x00}, + {0x3000, 0x35}, + {0x3054, 0x01}, + {0x305C, 0x11}, + {0x0112, 0x0A}, + {0x0113, 0x0A}, + {0x034C, 0x10}, + {0x034D, 0x70}, + {0x034E, 0x0C}, + {0x034F, 0x30}, + {0x0401, 0x00}, + {0x0404, 0x00}, + {0x0405, 0x10}, + {0x0408, 0x00}, + {0x0409, 0x00}, + {0x040A, 0x00}, + {0x040B, 0x00}, + {0x040C, 0x10}, + {0x040D, 0x70}, + {0x040E, 0x0C}, + {0x040F, 0x30}, + {0x0301, 0x05}, + {0x0303, 0x04}, + {0x0305, 0x03}, + {0x0306, 0x00}, + {0x0307, 0x96}, + {0x0309, 0x0A}, + {0x030B, 0x01}, + {0x0310, 0x00}, + {0x0820, 0x09}, + {0x0821, 0x60}, + {0x0822, 0x00}, + {0x0823, 0x00}, + {0x3A03, 0x08}, + {0x3A04, 0x70}, + {0x3A05, 0x02}, + {0x0B06, 0x01}, + {0x30A2, 0x00}, + {0x30B4, 0x00}, + {0x3A02, 0xFF}, + {0x3011, 0x00}, + {0x3013, 0x01}, + {0x4170, 0x00}, + {0x4171, 0x10}, + {0x4176, 0x00}, + {0x4177, 0x3C}, + {0xAE20, 0x04}, + {0xAE21, 0x5C}, + {0x0100, 0x00}, + {REG_NULL, 0x00}, +}; + +static const struct regval imx214_2104x1560_30fps_regs_4lane[] = { + {0x0114, 0x03}, + {0x0220, 0x00}, + {0x0221, 0x11}, + {0x0222, 0x01}, + {0x0340, 0x08}, + {0x0341, 0x3E}, + {0x0342, 0x13}, + {0x0343, 0x90}, + {0x0344, 0x00}, + {0x0345, 0x00}, + {0x0346, 0x00}, + {0x0347, 0x00}, + {0x0348, 0x10}, + {0x0349, 0x6F}, + {0x034A, 0x0C}, + {0x034B, 0x2F}, + {0x0381, 0x01}, + {0x0383, 0x01}, + {0x0385, 0x01}, + {0x0387, 0x01}, + {0x0900, 0x01}, + {0x0901, 0x22}, + {0x0902, 0x02}, + {0x3000, 0x35}, + {0x3054, 0x01}, + {0x305C, 0x11}, + {0x0112, 0x0A}, + {0x0113, 0x0A}, + {0x034C, 0x08}, + {0x034D, 0x38}, + {0x034E, 0x06}, + {0x034F, 0x18}, + {0x0401, 0x00}, + {0x0404, 0x00}, + {0x0405, 0x10}, + {0x0408, 0x00}, + {0x0409, 0x00}, + {0x040A, 0x00}, + {0x040B, 0x00}, + {0x040C, 0x08}, + {0x040D, 0x38}, + {0x040E, 0x06}, + {0x040F, 0x18}, + {0x0301, 0x05}, + {0x0303, 0x02}, + {0x0305, 0x03}, + {0x0306, 0x00}, + {0x0307, 0x64}, + {0x0309, 0x0A}, + {0x030B, 0x01}, + {0x0310, 0x00}, + {0x0820, 0x0C}, + {0x0821, 0x80}, + {0x0822, 0x00}, + {0x0823, 0x00}, + {0x3A03, 0x06}, + {0x3A04, 0x68}, + {0x3A05, 0x01}, + {0x0B06, 0x01}, + {0x30A2, 0x00}, + {0x30B4, 0x00}, + {0x3A02, 0xFF}, + {0x3011, 0x00}, + {0x3013, 0x00}, + {0x0202, 0x08}, + {0x0203, 0x34}, + {0x0224, 0x01}, + {0x0225, 0xF4}, + {0x0204, 0x00}, + {0x0205, 0x00}, + {0x020E, 0x01}, + {0x020F, 0x00}, + {0x0210, 0x01}, + {0x0211, 0x00}, + {0x0212, 0x01}, + {0x0213, 0x00}, + {0x0214, 0x01}, + {0x0215, 0x00}, + {0x0216, 0x00}, + {0x0217, 0x00}, + {0x4170, 0x00}, + {0x4171, 0x10}, + {0x4176, 0x00}, + {0x4177, 0x3C}, + {0xAE20, 0x04}, + {0xAE21, 0x5C}, + {0x0138, 0x01}, + {0x0100, 0x00}, + {REG_NULL, 0x00}, +}; + +static const struct regval imx214_4208x3120_30fps_regs_4lane[] = { + {0x0114, 0x03}, + {0x0220, 0x00}, + {0x0221, 0x11}, + {0x0222, 0x01}, + {0x0340, 0x0C}, + {0x0341, 0x58}, + {0x0342, 0x13}, + {0x0343, 0x90}, + {0x0344, 0x00}, + {0x0345, 0x00}, + {0x0346, 0x00}, + {0x0347, 0x00}, + {0x0348, 0x10}, + {0x0349, 0x6F}, + {0x034A, 0x0C}, + {0x034B, 0x2F}, + {0x0381, 0x01}, + {0x0383, 0x01}, + {0x0385, 0x01}, + {0x0387, 0x01}, + {0x0900, 0x00}, + {0x0901, 0x00}, + {0x0902, 0x00}, + {0x3000, 0x35}, + {0x3054, 0x01}, + {0x305C, 0x11}, + {0x0112, 0x0A}, + {0x0113, 0x0A}, + {0x034C, 0x10}, + {0x034D, 0x70}, + {0x034E, 0x0C}, + {0x034F, 0x30}, + {0x0401, 0x00}, + {0x0404, 0x00}, + {0x0405, 0x10}, + {0x0408, 0x00}, + {0x0409, 0x00}, + {0x040A, 0x00}, + {0x040B, 0x00}, + {0x040C, 0x10}, + {0x040D, 0x70}, + {0x040E, 0x0C}, + {0x040F, 0x30}, + {0x0301, 0x05}, + {0x0303, 0x02}, + {0x0305, 0x03}, + {0x0306, 0x00}, + {0x0307, 0x96}, + {0x0309, 0x0A}, + {0x030B, 0x01}, + {0x0310, 0x00}, + {0x0820, 0x12}, + {0x0821, 0xC0}, + {0x0822, 0x00}, + {0x0823, 0x00}, + {0x3A03, 0x09}, + {0x3A04, 0x20}, + {0x3A05, 0x01}, + {0x0B06, 0x01}, + {0x30A2, 0x00}, + {0x30B4, 0x00}, + {0x3A02, 0xFF}, + {0x3011, 0x00}, + {0x3013, 0x01}, + {0x0202, 0x0C}, + {0x0203, 0x4E}, + {0x0224, 0x01}, + {0x0225, 0xF4}, + {0x0204, 0x00}, + {0x0205, 0x00}, + {0x020E, 0x01}, + {0x020F, 0x00}, + {0x0210, 0x01}, + {0x0211, 0x00}, + {0x0212, 0x01}, + {0x0213, 0x00}, + {0x0214, 0x01}, + {0x0215, 0x00}, + {0x0216, 0x00}, + {0x0217, 0x00}, + {0x4170, 0x00}, + {0x4171, 0x10}, + {0x4176, 0x00}, + {0x4177, 0x3C}, + {0xAE20, 0x04}, + {0xAE21, 0x5C}, + {0x0100, 0x00}, + {REG_NULL, 0x00}, +}; + +static const struct imx214_mode supported_modes_2lane[] = { { - .width = 4096, - .height = 2304, - .reg_table = mode_4096x2304, + .width = 4208, + .height = 3120, + .max_fps = { + .numerator = 10000, + .denominator = 150000, + }, + .exp_def = 0x0c70, + .hts_def = 0x1390, + .vts_def = 0x0c7a, + .bpp = 10, + .reg_list = imx214_4208x3120_15fps_regs_2lane, + .link_freq_idx = 0, }, { - .width = 1920, - .height = 1080, - .reg_table = mode_1920x1080, + .width = 2104, + .height = 1560, + .max_fps = { + .numerator = 10000, + .denominator = 300000, + }, + .exp_def = 0x0630, + .hts_def = 0x1390, + .vts_def = 0x0640, + .bpp = 10, + .reg_list = imx214_2104x1560_30fps_regs_2lane, + .link_freq_idx = 0, }, }; -static inline struct imx214 *to_imx214(struct v4l2_subdev *sd) +static const struct imx214_mode supported_modes_4lane[] = { + { + .width = 4208, + .height = 3120, + .max_fps = { + .numerator = 10000, + .denominator = 300000, + }, + .exp_def = 0x0c50, + .hts_def = 0x1390, + .vts_def = 0x0c58, + .bpp = 10, + .reg_list = imx214_4208x3120_30fps_regs_4lane, + .link_freq_idx = 0, + }, + { + .width = 2104, + .height = 1560, + .max_fps = { + .numerator = 10000, + .denominator = 300000, + }, + .exp_def = 0x083a, + .hts_def = 0x1390, + .vts_def = 0x083E, + .bpp = 10, + .reg_list = imx214_2104x1560_30fps_regs_4lane, + .link_freq_idx = 0, + }, +}; + +static const s64 link_freq_items[] = { + IMX214_LINK_FREQ_600MHZ, +}; + +static const char * const imx214_test_pattern_menu[] = { + "Disabled", + "Vertical Color Bar Type 1", + "Vertical Color Bar Type 2", + "Vertical Color Bar Type 3", + "Vertical Color Bar Type 4" +}; + +/* Write registers up to 4 at a time */ +static int imx214_write_reg(struct i2c_client *client, u16 reg, + u32 len, u32 val) { - return container_of(sd, struct imx214, sd); -} + u32 buf_i, val_i; + u8 buf[6]; + u8 *val_p; + __be32 val_be; -static int __maybe_unused imx214_power_on(struct device *dev) -{ - struct i2c_client *client = to_i2c_client(dev); - struct v4l2_subdev *sd = i2c_get_clientdata(client); - struct imx214 *imx214 = to_imx214(sd); - int ret; + dev_dbg(&client->dev, "write reg(0x%x val:0x%x)!\n", reg, val); - ret = regulator_bulk_enable(IMX214_NUM_SUPPLIES, imx214->supplies); - if (ret < 0) { - dev_err(imx214->dev, "failed to enable regulators: %d\n", ret); - return ret; - } + if (len > 4) + return -EINVAL; - usleep_range(2000, 3000); + buf[0] = reg >> 8; + buf[1] = reg & 0xff; - ret = clk_prepare_enable(imx214->xclk); - if (ret < 0) { - regulator_bulk_disable(IMX214_NUM_SUPPLIES, imx214->supplies); - dev_err(imx214->dev, "clk prepare enable failed\n"); - return ret; - } + val_be = cpu_to_be32(val); + val_p = (u8 *)&val_be; + buf_i = 2; + val_i = 4 - len; - gpiod_set_value_cansleep(imx214->enable_gpio, 1); - usleep_range(12000, 15000); + while (val_i < 4) + buf[buf_i++] = val_p[val_i++]; + + if (i2c_master_send(client, buf, len + 2) != len + 2) + return -EIO; return 0; } -static int __maybe_unused imx214_power_off(struct device *dev) +static int imx214_write_array(struct i2c_client *client, + const struct regval *regs) +{ + u32 i; + int ret = 0; + + for (i = 0; ret == 0 && regs[i].addr != REG_NULL; i++) + ret = imx214_write_reg(client, regs[i].addr, + IMX214_REG_VALUE_08BIT, + regs[i].val); + + return ret; +} + +/* Read registers up to 4 at a time */ +static int imx214_read_reg(struct i2c_client *client, u16 reg, + unsigned int len, u32 *val) +{ + struct i2c_msg msgs[2]; + u8 *data_be_p; + __be32 data_be = 0; + __be16 reg_addr_be = cpu_to_be16(reg); + int ret; + + if (len > 4 || !len) + 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 -EIO; + + *val = be32_to_cpu(data_be); + + return 0; +} + +static int imx214_get_reso_dist(const struct imx214_mode *mode, + struct v4l2_mbus_framefmt *framefmt) +{ + return abs(mode->width - framefmt->width) + + abs(mode->height - framefmt->height); +} + +static const struct imx214_mode * +imx214_find_best_fit(struct imx214 *imx214, struct v4l2_subdev_format *fmt) +{ + struct v4l2_mbus_framefmt *framefmt = &fmt->format; + int dist; + int cur_best_fit = 0; + int cur_best_fit_dist = -1; + unsigned int i; + + for (i = 0; i < imx214->cfg_num; i++) { + dist = imx214_get_reso_dist(&imx214->support_modes[i], framefmt); + if (cur_best_fit_dist == -1 || dist < cur_best_fit_dist) { + cur_best_fit_dist = dist; + cur_best_fit = i; + } + } + + return &imx214->support_modes[cur_best_fit]; +} + +static int imx214_set_fmt(struct v4l2_subdev *sd, + struct v4l2_subdev_pad_config *cfg, + struct v4l2_subdev_format *fmt) { - struct i2c_client *client = to_i2c_client(dev); - struct v4l2_subdev *sd = i2c_get_clientdata(client); struct imx214 *imx214 = to_imx214(sd); + const struct imx214_mode *mode; + s64 h_blank, vblank_def; + u64 pixel_rate = 0; + u32 lane_num = imx214->bus_cfg.bus.mipi_csi2.num_data_lanes; - gpiod_set_value_cansleep(imx214->enable_gpio, 0); + mutex_lock(&imx214->mutex); - clk_disable_unprepare(imx214->xclk); + mode = imx214_find_best_fit(imx214, fmt); + fmt->format.code = IMX214_MEDIA_BUS_FMT; + fmt->format.width = mode->width; + fmt->format.height = mode->height; + fmt->format.field = V4L2_FIELD_NONE; + if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) { +#ifdef CONFIG_VIDEO_V4L2_SUBDEV_API + *v4l2_subdev_get_try_format(sd, cfg, fmt->pad) = fmt->format; +#else + mutex_unlock(&imx214->mutex); + return -ENOTTY; +#endif + } else { + imx214->cur_mode = mode; + h_blank = mode->hts_def - mode->width; + __v4l2_ctrl_modify_range(imx214->hblank, h_blank, + h_blank, 1, h_blank); + vblank_def = mode->vts_def - mode->height; + __v4l2_ctrl_modify_range(imx214->vblank, vblank_def, + IMX214_VTS_MAX - mode->height, + 1, vblank_def); + pixel_rate = (u32)link_freq_items[mode->link_freq_idx] / mode->bpp * 2 * lane_num; - regulator_bulk_disable(IMX214_NUM_SUPPLIES, imx214->supplies); - usleep_range(10, 20); + __v4l2_ctrl_s_ctrl_int64(imx214->pixel_rate, + pixel_rate); + __v4l2_ctrl_s_ctrl(imx214->link_freq, + mode->link_freq_idx); + } + + mutex_unlock(&imx214->mutex); + + return 0; +} + +static int imx214_get_fmt(struct v4l2_subdev *sd, + struct v4l2_subdev_pad_config *cfg, + struct v4l2_subdev_format *fmt) +{ + struct imx214 *imx214 = to_imx214(sd); + const struct imx214_mode *mode = imx214->cur_mode; + + mutex_lock(&imx214->mutex); + if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) { +#ifdef CONFIG_VIDEO_V4L2_SUBDEV_API + fmt->format = *v4l2_subdev_get_try_format(sd, cfg, fmt->pad); +#else + mutex_unlock(&imx214->mutex); + return -ENOTTY; +#endif + } else { + fmt->format.width = mode->width; + fmt->format.height = mode->height; + fmt->format.code = IMX214_MEDIA_BUS_FMT; + fmt->format.field = V4L2_FIELD_NONE; + } + mutex_unlock(&imx214->mutex); return 0; } static int imx214_enum_mbus_code(struct v4l2_subdev *sd, - struct v4l2_subdev_pad_config *cfg, - struct v4l2_subdev_mbus_code_enum *code) -{ - if (code->index > 0) - return -EINVAL; - - code->code = IMX214_MBUS_CODE; - - return 0; -} - -static int imx214_enum_frame_size(struct v4l2_subdev *subdev, struct v4l2_subdev_pad_config *cfg, - struct v4l2_subdev_frame_size_enum *fse) + struct v4l2_subdev_mbus_code_enum *code) { - if (fse->code != IMX214_MBUS_CODE) + if (code->index != 0) return -EINVAL; - - if (fse->index >= ARRAY_SIZE(imx214_modes)) - return -EINVAL; - - fse->min_width = fse->max_width = imx214_modes[fse->index].width; - fse->min_height = fse->max_height = imx214_modes[fse->index].height; + code->code = IMX214_MEDIA_BUS_FMT; return 0; } -#ifdef CONFIG_VIDEO_ADV_DEBUG -static int imx214_s_register(struct v4l2_subdev *subdev, - const struct v4l2_dbg_register *reg) +static int imx214_enum_frame_sizes(struct v4l2_subdev *sd, + struct v4l2_subdev_pad_config *cfg, + struct v4l2_subdev_frame_size_enum *fse) { - struct imx214 *imx214 = container_of(subdev, struct imx214, sd); + struct imx214 *imx214 = to_imx214(sd); - return regmap_write(imx214->regmap, reg->reg, reg->val); + if (fse->index >= imx214->cfg_num) + return -EINVAL; + + if (fse->code != IMX214_MEDIA_BUS_FMT) + return -EINVAL; + + fse->min_width = imx214->support_modes[fse->index].width; + fse->max_width = imx214->support_modes[fse->index].width; + fse->max_height = imx214->support_modes[fse->index].height; + fse->min_height = imx214->support_modes[fse->index].height; + + return 0; } -static int imx214_g_register(struct v4l2_subdev *subdev, - struct v4l2_dbg_register *reg) +static int imx214_enable_test_pattern(struct imx214 *imx214, u32 pattern) { - struct imx214 *imx214 = container_of(subdev, struct imx214, sd); - unsigned int aux; - int ret; + u32 val; - reg->size = 1; - ret = regmap_read(imx214->regmap, reg->reg, &aux); - reg->val = aux; + if (pattern) + val = (pattern - 1) | IMX214_TEST_PATTERN_ENABLE; + else + val = IMX214_TEST_PATTERN_DISABLE; + + return imx214_write_reg(imx214->client, + IMX214_REG_TEST_PATTERN, + IMX214_REG_VALUE_08BIT, + val); +} + +static int imx214_g_frame_interval(struct v4l2_subdev *sd, + struct v4l2_subdev_frame_interval *fi) +{ + struct imx214 *imx214 = to_imx214(sd); + const struct imx214_mode *mode = imx214->cur_mode; + + mutex_lock(&imx214->mutex); + fi->interval = mode->max_fps; + mutex_unlock(&imx214->mutex); + + return 0; +} + +static void imx214_get_otp(struct imx214_otp_info *otp, + struct rkmodule_inf *inf) +{ + u32 i; + + /* fac */ + if (otp->flag & 0x80) { + inf->fac.flag = 1; + inf->fac.year = otp->year; + inf->fac.month = otp->month; + inf->fac.day = otp->day; + for (i = 0; i < ARRAY_SIZE(imx214_module_info) - 1; i++) { + if (imx214_module_info[i].id == otp->module_id) + break; + } + strscpy(inf->fac.module, imx214_module_info[i].name, + sizeof(inf->fac.module)); + + for (i = 0; i < ARRAY_SIZE(imx214_lens_info) - 1; i++) { + if (imx214_lens_info[i].id == otp->lens_id) + break; + } + strscpy(inf->fac.lens, imx214_lens_info[i].name, + sizeof(inf->fac.lens)); + } + /* awb */ + if (otp->flag & 0x40) { + inf->awb.flag = 1; + inf->awb.r_value = otp->rg_ratio; + inf->awb.b_value = otp->bg_ratio; + inf->awb.gr_value = 0x400; + inf->awb.gb_value = 0x400; + + inf->awb.golden_r_value = 0; + inf->awb.golden_b_value = 0; + inf->awb.golden_gr_value = 0; + inf->awb.golden_gb_value = 0; + } + /* af */ + if (otp->flag & 0x20) { + inf->af.flag = 1; + inf->af.af_otp[0].vcm_start = otp->vcm_start; + inf->af.af_otp[0].vcm_end = otp->vcm_end; + inf->af.af_otp[0].vcm_dir = otp->vcm_dir; + } + /* lsc */ + if (otp->flag & 0x10) { + inf->lsc.flag = 1; + inf->lsc.decimal_bits = 0; + inf->lsc.lsc_w = 9; + inf->lsc.lsc_h = 14; + + for (i = 0; i < 126; i++) { + inf->lsc.lsc_r[i] = otp->lenc[i]; + inf->lsc.lsc_gr[i] = otp->lenc[i + 126]; + inf->lsc.lsc_gb[i] = otp->lenc[i + 252]; + inf->lsc.lsc_b[i] = otp->lenc[i + 378]; + } + } +} + +static void imx214_get_module_inf(struct imx214 *imx214, + struct rkmodule_inf *inf) +{ + struct imx214_otp_info *otp = imx214->otp; + + memset(inf, 0, sizeof(*inf)); + strscpy(inf->base.sensor, IMX214_NAME, sizeof(inf->base.sensor)); + strscpy(inf->base.module, imx214->module_name, + sizeof(inf->base.module)); + strscpy(inf->base.lens, imx214->len_name, sizeof(inf->base.lens)); + if (otp) + imx214_get_otp(otp, inf); +} + +static void imx214_set_awb_cfg(struct imx214 *imx214, + struct rkmodule_awb_cfg *cfg) +{ + mutex_lock(&imx214->mutex); + memcpy(&imx214->awb_cfg, cfg, sizeof(*cfg)); + mutex_unlock(&imx214->mutex); +} + +static void imx214_set_lsc_cfg(struct imx214 *imx214, + struct rkmodule_lsc_cfg *cfg) +{ + mutex_lock(&imx214->mutex); + memcpy(&imx214->lsc_cfg, cfg, sizeof(*cfg)); + mutex_unlock(&imx214->mutex); +} + +static long imx214_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg) +{ + struct imx214 *imx214 = to_imx214(sd); + long ret = 0; + u32 stream = 0; + + switch (cmd) { + case RKMODULE_GET_MODULE_INFO: + imx214_get_module_inf(imx214, (struct rkmodule_inf *)arg); + break; + case RKMODULE_AWB_CFG: + imx214_set_awb_cfg(imx214, (struct rkmodule_awb_cfg *)arg); + break; + case RKMODULE_LSC_CFG: + imx214_set_lsc_cfg(imx214, (struct rkmodule_lsc_cfg *)arg); + break; + case RKMODULE_SET_QUICK_STREAM: + + stream = *((u32 *)arg); + + if (stream) + ret = imx214_write_reg(imx214->client, + IMX214_REG_CTRL_MODE, + IMX214_REG_VALUE_08BIT, + IMX214_MODE_STREAMING); + else + ret = imx214_write_reg(imx214->client, + IMX214_REG_CTRL_MODE, + IMX214_REG_VALUE_08BIT, + IMX214_MODE_SW_STANDBY); + break; + default: + ret = -ENOIOCTLCMD; + break; + } + + return ret; +} + +#ifdef CONFIG_COMPAT +static long imx214_compat_ioctl32(struct v4l2_subdev *sd, + unsigned int cmd, unsigned long arg) +{ + void __user *up = compat_ptr(arg); + struct rkmodule_inf *inf; + struct rkmodule_awb_cfg *cfg; + struct rkmodule_lsc_cfg *lsc_cfg; + long ret = 0; + u32 stream = 0; + + switch (cmd) { + case RKMODULE_GET_MODULE_INFO: + inf = kzalloc(sizeof(*inf), GFP_KERNEL); + if (!inf) { + ret = -ENOMEM; + return ret; + } + + ret = imx214_ioctl(sd, cmd, inf); + if (!ret) { + ret = copy_to_user(up, inf, sizeof(*inf)); + if (ret) + ret = -EFAULT; + } + kfree(inf); + break; + case RKMODULE_AWB_CFG: + cfg = kzalloc(sizeof(*cfg), GFP_KERNEL); + if (!cfg) { + ret = -ENOMEM; + return ret; + } + + ret = copy_from_user(cfg, up, sizeof(*cfg)); + if (!ret) + ret = imx214_ioctl(sd, cmd, cfg); + else + ret = -EFAULT; + kfree(cfg); + break; + case RKMODULE_LSC_CFG: + lsc_cfg = kzalloc(sizeof(*lsc_cfg), GFP_KERNEL); + if (!lsc_cfg) { + ret = -ENOMEM; + return ret; + } + + ret = copy_from_user(lsc_cfg, up, sizeof(*lsc_cfg)); + if (!ret) + ret = imx214_ioctl(sd, cmd, lsc_cfg); + else + ret = -EFAULT; + kfree(lsc_cfg); + break; + case RKMODULE_SET_QUICK_STREAM: + ret = copy_from_user(&stream, up, sizeof(u32)); + if (!ret) + ret = imx214_ioctl(sd, cmd, &stream); + else + ret = -EFAULT; + break; + default: + ret = -ENOIOCTLCMD; + break; + } return ret; } #endif -static const struct v4l2_subdev_core_ops imx214_core_ops = { -#ifdef CONFIG_VIDEO_ADV_DEBUG - .g_register = imx214_g_register, - .s_register = imx214_s_register, +static int imx214_apply_otp(struct imx214 *imx214) +{ + int R_gain, G_gain, B_gain, base_gain; + struct i2c_client *client = imx214->client; + struct imx214_otp_info *otp_ptr = imx214->otp; + struct rkmodule_awb_cfg *awb_cfg = &imx214->awb_cfg; + struct rkmodule_lsc_cfg *lsc_cfg = &imx214->lsc_cfg; + u32 golden_bg_ratio = 0; + u32 golden_rg_ratio = 0; + u32 golden_g_value = 0; + u32 bg_ratio; + u32 rg_ratio; + //u32 g_value; + u32 i; + + if (!otp_ptr) + return 0; + if (awb_cfg->enable) { + golden_g_value = (awb_cfg->golden_gb_value + + awb_cfg->golden_gr_value) / 2; + if (golden_g_value != 0) { + golden_rg_ratio = awb_cfg->golden_r_value * 0x400 + / golden_g_value; + golden_bg_ratio = awb_cfg->golden_b_value * 0x400 + / golden_g_value; + } else { + golden_rg_ratio = RG_Ratio_Typical_Default; + golden_bg_ratio = BG_Ratio_Typical_Default; + } + } + /* apply OTP WB Calibration */ + if ((otp_ptr->flag & 0x40) && golden_bg_ratio && golden_rg_ratio) { + rg_ratio = otp_ptr->rg_ratio; + bg_ratio = otp_ptr->bg_ratio; + dev_dbg(&client->dev, "rg:0x%x,bg:0x%x,gol rg:0x%x,bg:0x%x\n", + rg_ratio, bg_ratio, golden_rg_ratio, golden_bg_ratio); + /* calculate G gain */ + R_gain = golden_rg_ratio * 1000 / rg_ratio; + B_gain = golden_bg_ratio * 1000 / bg_ratio; + G_gain = 1000; + if (R_gain < 1000 || B_gain < 1000) { + if (R_gain < B_gain) + base_gain = R_gain; + else + base_gain = B_gain; + } else { + base_gain = G_gain; + } + R_gain = 0x100 * R_gain / (base_gain); + B_gain = 0x100 * B_gain / (base_gain); + G_gain = 0x100 * G_gain / (base_gain); + /* update sensor WB gain */ + if (R_gain > 0x100) { + imx214_write_reg(client, 0x0210, + IMX214_REG_VALUE_08BIT, R_gain >> 8); + imx214_write_reg(client, 0x0211, + IMX214_REG_VALUE_08BIT, R_gain & 0x00ff); + } + if (G_gain > 0x100) { + imx214_write_reg(client, 0x020e, + IMX214_REG_VALUE_08BIT, G_gain >> 8); + imx214_write_reg(client, 0x020f, + IMX214_REG_VALUE_08BIT, G_gain & 0x00ff); + imx214_write_reg(client, 0x0214, + IMX214_REG_VALUE_08BIT, G_gain >> 8); + imx214_write_reg(client, 0x0215, + IMX214_REG_VALUE_08BIT, G_gain & 0x00ff); + } + if (B_gain > 0x100) { + imx214_write_reg(client, 0x0212, + IMX214_REG_VALUE_08BIT, B_gain >> 8); + imx214_write_reg(client, 0x0213, + IMX214_REG_VALUE_08BIT, B_gain & 0x00ff); + } + dev_dbg(&client->dev, "apply awb gain: 0x%x, 0x%x, 0x%x\n", + R_gain, G_gain, B_gain); + } + + /* apply OTP Lenc Calibration */ + if ((otp_ptr->flag & 0x10) && lsc_cfg->enable) { + for (i = 0; i < 504; i++) { + imx214_write_reg(client, 0xA300 + i, + IMX214_REG_VALUE_08BIT, otp_ptr->lenc[i]); + dev_dbg(&client->dev, "apply lenc[%d]: 0x%x\n", + i, otp_ptr->lenc[i]); + } + usleep_range(1000, 2000); + //choose lsc table 1 + imx214_write_reg(client, 0x3021, + IMX214_REG_VALUE_08BIT, 0x01); + //enable lsc + imx214_write_reg(client, 0x0B00, + IMX214_REG_VALUE_08BIT, 0x01); + } + + /* apply OTP SPC Calibration */ + if (otp_ptr->flag & 0x08) { + for (i = 0; i < 63; i++) { + imx214_write_reg(client, 0xD04C + i, + IMX214_REG_VALUE_08BIT, otp_ptr->spc[i]); + dev_dbg(&client->dev, "apply spc[%d]: 0x%x\n", + i, otp_ptr->spc[i]); + imx214_write_reg(client, 0xD08C + i, + IMX214_REG_VALUE_08BIT, otp_ptr->spc[i + 63]); + dev_dbg(&client->dev, "apply spc[%d]: 0x%x\n", + i + 63, otp_ptr->spc[i + 63]); + } + //enable spc + imx214_write_reg(client, 0x7BC8, + IMX214_REG_VALUE_08BIT, 0x01); + } + + return 0; +} + +static int __imx214_start_stream(struct imx214 *imx214) +{ + int ret; + + ret = imx214_write_array(imx214->client, imx214->cur_mode->reg_list); + if (ret) + return ret; + + /* In case these controls are set before streaming */ + mutex_unlock(&imx214->mutex); + ret = v4l2_ctrl_handler_setup(&imx214->ctrl_handler); + mutex_lock(&imx214->mutex); + if (ret) + return ret; + + ret = imx214_apply_otp(imx214); + if (ret) + return ret; + + return imx214_write_reg(imx214->client, + IMX214_REG_CTRL_MODE, + IMX214_REG_VALUE_08BIT, + IMX214_MODE_STREAMING); +} + +static int __imx214_stop_stream(struct imx214 *imx214) +{ + return imx214_write_reg(imx214->client, + IMX214_REG_CTRL_MODE, + IMX214_REG_VALUE_08BIT, + IMX214_MODE_SW_STANDBY); +} + +static int imx214_s_stream(struct v4l2_subdev *sd, int on) +{ + struct imx214 *imx214 = to_imx214(sd); + struct i2c_client *client = imx214->client; + int ret = 0; + + dev_info(&client->dev, "%s: on: %d, %dx%d@%d\n", __func__, on, + imx214->cur_mode->width, + imx214->cur_mode->height, + DIV_ROUND_CLOSEST(imx214->cur_mode->max_fps.denominator, + imx214->cur_mode->max_fps.numerator)); + + + mutex_lock(&imx214->mutex); + on = !!on; + if (on == imx214->streaming) + goto unlock_and_return; + + if (on) { + ret = pm_runtime_get_sync(&client->dev); + if (ret < 0) { + pm_runtime_put_noidle(&client->dev); + goto unlock_and_return; + } + + ret = __imx214_start_stream(imx214); + if (ret) { + v4l2_err(sd, "start stream failed while write regs\n"); + pm_runtime_put(&client->dev); + goto unlock_and_return; + } + } else { + __imx214_stop_stream(imx214); + pm_runtime_put(&client->dev); + } + + imx214->streaming = on; + +unlock_and_return: + mutex_unlock(&imx214->mutex); + + return ret; +} + +static int imx214_s_power(struct v4l2_subdev *sd, int on) +{ + struct imx214 *imx214 = to_imx214(sd); + struct i2c_client *client = imx214->client; + int ret = 0; + + mutex_lock(&imx214->mutex); + + /* If the power state is not modified - no work to do. */ + if (imx214->power_on == !!on) + goto unlock_and_return; + + if (on) { + ret = pm_runtime_get_sync(&client->dev); + if (ret < 0) { + pm_runtime_put_noidle(&client->dev); + goto unlock_and_return; + } + + ret = imx214_write_array(imx214->client, imx214_global_regs); + if (ret) { + v4l2_err(sd, "could not set init registers\n"); + pm_runtime_put_noidle(&client->dev); + goto unlock_and_return; + } + + imx214->power_on = true; + } else { + pm_runtime_put(&client->dev); + imx214->power_on = false; + } + +unlock_and_return: + mutex_unlock(&imx214->mutex); + + return ret; +} + +/* Calculate the delay in us by clock rate and clock cycles */ +static inline u32 imx214_cal_delay(u32 cycles) +{ + return DIV_ROUND_UP(cycles, IMX214_XVCLK_FREQ / 1000 / 1000); +} + +static int __imx214_power_on(struct imx214 *imx214) +{ + int ret; + u32 delay_us; + struct device *dev = &imx214->client->dev; + + if (!IS_ERR(imx214->power_gpio)) + gpiod_set_value_cansleep(imx214->power_gpio, 1); + + usleep_range(1000, 2000); + + if (!IS_ERR_OR_NULL(imx214->pins_default)) { + ret = pinctrl_select_state(imx214->pinctrl, + imx214->pins_default); + if (ret < 0) + dev_err(dev, "could not set pins\n"); + } + ret = clk_set_rate(imx214->xvclk, IMX214_XVCLK_FREQ); + if (ret < 0) + dev_warn(dev, "Failed to set xvclk rate (24MHz)\n"); + if (clk_get_rate(imx214->xvclk) != IMX214_XVCLK_FREQ) + dev_warn(dev, "xvclk mismatched, modes are based on 24MHz\n"); + ret = clk_prepare_enable(imx214->xvclk); + if (ret < 0) { + dev_err(dev, "Failed to enable xvclk\n"); + return ret; + } + if (!IS_ERR(imx214->reset_gpio)) + gpiod_set_value_cansleep(imx214->reset_gpio, 0); + + ret = regulator_bulk_enable(IMX214_NUM_SUPPLIES, imx214->supplies); + if (ret < 0) { + dev_err(dev, "Failed to enable regulators\n"); + goto disable_clk; + } + + if (!IS_ERR(imx214->reset_gpio)) + gpiod_set_value_cansleep(imx214->reset_gpio, 1); + + usleep_range(500, 1000); + if (!IS_ERR(imx214->pwdn_gpio)) + gpiod_set_value_cansleep(imx214->pwdn_gpio, 1); + + /* 8192 cycles prior to first SCCB transaction */ + delay_us = imx214_cal_delay(8192); + usleep_range(delay_us, delay_us * 2); + + return 0; + +disable_clk: + clk_disable_unprepare(imx214->xvclk); + + return ret; +} + +static void __imx214_power_off(struct imx214 *imx214) +{ + int ret; + struct device *dev = &imx214->client->dev; + + if (!IS_ERR(imx214->pwdn_gpio)) + gpiod_set_value_cansleep(imx214->pwdn_gpio, 0); + clk_disable_unprepare(imx214->xvclk); + if (!IS_ERR(imx214->reset_gpio)) + gpiod_set_value_cansleep(imx214->reset_gpio, 0); + + if (!IS_ERR_OR_NULL(imx214->pins_sleep)) { + ret = pinctrl_select_state(imx214->pinctrl, + imx214->pins_sleep); + if (ret < 0) + dev_dbg(dev, "could not set pins\n"); + } + if (!IS_ERR(imx214->power_gpio)) + gpiod_set_value_cansleep(imx214->power_gpio, 0); + + regulator_bulk_disable(IMX214_NUM_SUPPLIES, imx214->supplies); +} + +static int imx214_runtime_resume(struct device *dev) +{ + struct i2c_client *client = to_i2c_client(dev); + struct v4l2_subdev *sd = i2c_get_clientdata(client); + struct imx214 *imx214 = to_imx214(sd); + + return __imx214_power_on(imx214); +} + +static int imx214_runtime_suspend(struct device *dev) +{ + struct i2c_client *client = to_i2c_client(dev); + struct v4l2_subdev *sd = i2c_get_clientdata(client); + struct imx214 *imx214 = to_imx214(sd); + + __imx214_power_off(imx214); + + return 0; +} + +#ifdef CONFIG_VIDEO_V4L2_SUBDEV_API +static int imx214_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh) +{ + struct imx214 *imx214 = to_imx214(sd); + struct v4l2_mbus_framefmt *try_fmt = + v4l2_subdev_get_try_format(sd, fh->pad, 0); + const struct imx214_mode *def_mode = &imx214->support_modes[0]; + + mutex_lock(&imx214->mutex); + /* Initialize try_fmt */ + try_fmt->width = def_mode->width; + try_fmt->height = def_mode->height; + try_fmt->code = IMX214_MEDIA_BUS_FMT; + try_fmt->field = V4L2_FIELD_NONE; + + mutex_unlock(&imx214->mutex); + /* No crop or compose */ + + return 0; +} #endif -}; -static struct v4l2_mbus_framefmt * -__imx214_get_pad_format(struct imx214 *imx214, - struct v4l2_subdev_pad_config *cfg, - unsigned int pad, - enum v4l2_subdev_format_whence which) -{ - switch (which) { - case V4L2_SUBDEV_FORMAT_TRY: - return v4l2_subdev_get_try_format(&imx214->sd, cfg, pad); - case V4L2_SUBDEV_FORMAT_ACTIVE: - return &imx214->fmt; - default: - return NULL; - } -} - -static int imx214_get_format(struct v4l2_subdev *sd, - struct v4l2_subdev_pad_config *cfg, - struct v4l2_subdev_format *format) +static int imx214_enum_frame_interval(struct v4l2_subdev *sd, + struct v4l2_subdev_pad_config *cfg, + struct v4l2_subdev_frame_interval_enum *fie) { struct imx214 *imx214 = to_imx214(sd); - mutex_lock(&imx214->mutex); - format->format = *__imx214_get_pad_format(imx214, cfg, format->pad, - format->which); - mutex_unlock(&imx214->mutex); + if (fie->index >= imx214->cfg_num) + return -EINVAL; + + if (fie->code != IMX214_MEDIA_BUS_FMT) + return -EINVAL; + + fie->width = imx214->support_modes[fie->index].width; + fie->height = imx214->support_modes[fie->index].height; + fie->interval = imx214->support_modes[fie->index].max_fps; return 0; } -static struct v4l2_rect * -__imx214_get_pad_crop(struct imx214 *imx214, struct v4l2_subdev_pad_config *cfg, - unsigned int pad, enum v4l2_subdev_format_whence which) -{ - switch (which) { - case V4L2_SUBDEV_FORMAT_TRY: - return v4l2_subdev_get_try_crop(&imx214->sd, cfg, pad); - case V4L2_SUBDEV_FORMAT_ACTIVE: - return &imx214->crop; - default: - return NULL; - } -} - -static int imx214_set_format(struct v4l2_subdev *sd, - struct v4l2_subdev_pad_config *cfg, - struct v4l2_subdev_format *format) +static int imx214_g_mbus_config(struct v4l2_subdev *sd, unsigned int pad, + struct v4l2_mbus_config *config) { struct imx214 *imx214 = to_imx214(sd); - struct v4l2_mbus_framefmt *__format; - struct v4l2_rect *__crop; - const struct imx214_mode *mode; + u32 lane_num = imx214->bus_cfg.bus.mipi_csi2.num_data_lanes; + u32 val = 0; - mutex_lock(&imx214->mutex); + val = 1 << (lane_num - 1) | + V4L2_MBUS_CSI2_CHANNEL_0 | + V4L2_MBUS_CSI2_CONTINUOUS_CLOCK; - __crop = __imx214_get_pad_crop(imx214, cfg, format->pad, format->which); - - mode = v4l2_find_nearest_size(imx214_modes, - ARRAY_SIZE(imx214_modes), width, height, - format->format.width, - format->format.height); - - __crop->width = mode->width; - __crop->height = mode->height; - - __format = __imx214_get_pad_format(imx214, cfg, format->pad, - format->which); - __format->width = __crop->width; - __format->height = __crop->height; - __format->code = IMX214_MBUS_CODE; - __format->field = V4L2_FIELD_NONE; - __format->colorspace = V4L2_COLORSPACE_SRGB; - __format->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(__format->colorspace); - __format->quantization = V4L2_MAP_QUANTIZATION_DEFAULT(true, - __format->colorspace, __format->ycbcr_enc); - __format->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(__format->colorspace); - - format->format = *__format; - - mutex_unlock(&imx214->mutex); + config->type = V4L2_MBUS_CSI2_DPHY; + config->flags = val; return 0; } +#define CROP_START(SRC, DST) (((SRC) - (DST)) / 2 / 4 * 4) +#define DST_WIDTH_2096 2096 +#define DST_HEIGHT_1560 1560 + static int imx214_get_selection(struct v4l2_subdev *sd, struct v4l2_subdev_pad_config *cfg, struct v4l2_subdev_selection *sel) { struct imx214 *imx214 = to_imx214(sd); - if (sel->target != V4L2_SEL_TGT_CROP) - return -EINVAL; + if (sel->target == V4L2_SEL_TGT_CROP_BOUNDS) { + if (imx214->cur_mode->width == 2104) { + sel->r.left = CROP_START(imx214->cur_mode->width, DST_WIDTH_2096); + sel->r.width = DST_WIDTH_2096; + sel->r.top = CROP_START(imx214->cur_mode->height, DST_HEIGHT_1560); + sel->r.height = DST_HEIGHT_1560; + } else { + sel->r.left = CROP_START(imx214->cur_mode->width, + imx214->cur_mode->width); + sel->r.width = imx214->cur_mode->width; + sel->r.top = CROP_START(imx214->cur_mode->height, + imx214->cur_mode->height); + sel->r.height = imx214->cur_mode->height; + } + return 0; + } - mutex_lock(&imx214->mutex); - sel->r = *__imx214_get_pad_crop(imx214, cfg, sel->pad, - sel->which); - mutex_unlock(&imx214->mutex); - return 0; + return -EINVAL; } -static int imx214_entity_init_cfg(struct v4l2_subdev *subdev, - struct v4l2_subdev_pad_config *cfg) +static const struct dev_pm_ops imx214_pm_ops = { + SET_RUNTIME_PM_OPS(imx214_runtime_suspend, + imx214_runtime_resume, NULL) +}; + +#ifdef CONFIG_VIDEO_V4L2_SUBDEV_API +static const struct v4l2_subdev_internal_ops imx214_internal_ops = { + .open = imx214_open, +}; +#endif + +static const struct v4l2_subdev_core_ops imx214_core_ops = { + .s_power = imx214_s_power, + .ioctl = imx214_ioctl, +#ifdef CONFIG_COMPAT + .compat_ioctl32 = imx214_compat_ioctl32, +#endif +}; + +static const struct v4l2_subdev_video_ops imx214_video_ops = { + .s_stream = imx214_s_stream, + .g_frame_interval = imx214_g_frame_interval, +}; + +static const struct v4l2_subdev_pad_ops imx214_pad_ops = { + .enum_mbus_code = imx214_enum_mbus_code, + .enum_frame_size = imx214_enum_frame_sizes, + .enum_frame_interval = imx214_enum_frame_interval, + .get_fmt = imx214_get_fmt, + .set_fmt = imx214_set_fmt, + .get_selection = imx214_get_selection, + .get_mbus_config = imx214_g_mbus_config, +}; + +static const struct v4l2_subdev_ops imx214_subdev_ops = { + .core = &imx214_core_ops, + .video = &imx214_video_ops, + .pad = &imx214_pad_ops, +}; + +static int imx214_set_gain_reg(struct imx214 *imx214, u32 a_gain) { - struct v4l2_subdev_format fmt = { }; + int ret = 0; + u32 gain_reg = 0; - fmt.which = cfg ? V4L2_SUBDEV_FORMAT_TRY : V4L2_SUBDEV_FORMAT_ACTIVE; - fmt.format.width = imx214_modes[0].width; - fmt.format.height = imx214_modes[0].height; + gain_reg = (512 - (512 * 512 / a_gain)); + if (gain_reg > 480) + gain_reg = 480; - imx214_set_format(subdev, cfg, &fmt); - - return 0; + ret = imx214_write_reg(imx214->client, + IMX214_REG_GAIN_H, + IMX214_REG_VALUE_08BIT, + ((gain_reg & 0x100) >> 8)); + ret |= imx214_write_reg(imx214->client, + IMX214_REG_GAIN_L, + IMX214_REG_VALUE_08BIT, + (gain_reg & 0xff)); + return ret; } static int imx214_set_ctrl(struct v4l2_ctrl *ctrl) { struct imx214 *imx214 = container_of(ctrl->handler, - struct imx214, ctrls); - u8 vals[2]; - int ret; + struct imx214, ctrl_handler); + struct i2c_client *client = imx214->client; + s64 max; + int ret = 0; - /* - * Applying V4L2 control value only happens - * when power is up for streaming - */ - if (!pm_runtime_get_if_in_use(imx214->dev)) + /* Propagate change of current control to all related controls */ + switch (ctrl->id) { + case V4L2_CID_VBLANK: + /* Update max exposure while meeting expected vblanking */ + max = imx214->cur_mode->height + ctrl->val - 4; + __v4l2_ctrl_modify_range(imx214->exposure, + imx214->exposure->minimum, max, + imx214->exposure->step, + imx214->exposure->default_value); + break; + } + + if (!pm_runtime_get_if_in_use(&client->dev)) return 0; switch (ctrl->id) { case V4L2_CID_EXPOSURE: - vals[1] = ctrl->val; - vals[0] = ctrl->val >> 8; - ret = regmap_bulk_write(imx214->regmap, 0x202, vals, 2); - if (ret < 0) - dev_err(imx214->dev, "Error %d\n", ret); - ret = 0; + /* 4 least significant bits of expsoure are fractional part */ + ret = imx214_write_reg(imx214->client, + IMX214_REG_EXPOSURE, + IMX214_REG_VALUE_16BIT, + ctrl->val); + break; + case V4L2_CID_ANALOGUE_GAIN: + ret = imx214_set_gain_reg(imx214, ctrl->val); + break; + case V4L2_CID_VBLANK: + ret = imx214_write_reg(imx214->client, + IMX214_REG_VTS, + IMX214_REG_VALUE_16BIT, + ctrl->val + imx214->cur_mode->height); + break; + case V4L2_CID_TEST_PATTERN: + ret = imx214_enable_test_pattern(imx214, ctrl->val); break; - default: - ret = -EINVAL; + dev_warn(&client->dev, "%s Unhandled id:0x%x, val:0x%x\n", + __func__, ctrl->id, ctrl->val); + break; } - pm_runtime_put(imx214->dev); + pm_runtime_put(&client->dev); return ret; } @@ -683,398 +1620,299 @@ static const struct v4l2_ctrl_ops imx214_ctrl_ops = { .s_ctrl = imx214_set_ctrl, }; -#define MAX_CMD 4 -static int imx214_write_table(struct imx214 *imx214, - const struct reg_8 table[]) -{ - u8 vals[MAX_CMD]; - int i; - int ret; - - for (; table->addr != IMX214_TABLE_END ; table++) { - if (table->addr == IMX214_TABLE_WAIT_MS) { - usleep_range(table->val * 1000, - table->val * 1000 + 500); - continue; - } - - for (i = 0; i < MAX_CMD; i++) { - if (table[i].addr != (table[0].addr + i)) - break; - vals[i] = table[i].val; - } - - ret = regmap_bulk_write(imx214->regmap, table->addr, vals, i); - - if (ret) { - dev_err(imx214->dev, "write_table error: %d\n", ret); - return ret; - } - - table += i - 1; - } - - return 0; -} - -static int imx214_start_streaming(struct imx214 *imx214) +static int imx214_initialize_controls(struct imx214 *imx214) { const struct imx214_mode *mode; + struct v4l2_ctrl_handler *handler; + s64 exposure_max, vblank_def; + u32 h_blank; int ret; + u64 dst_pixel_rate = 0; + u32 lane_num = imx214->bus_cfg.bus.mipi_csi2.num_data_lanes; - mutex_lock(&imx214->mutex); - ret = imx214_write_table(imx214, mode_table_common); - if (ret < 0) { - dev_err(imx214->dev, "could not sent common table %d\n", ret); - goto error; + handler = &imx214->ctrl_handler; + mode = imx214->cur_mode; + ret = v4l2_ctrl_handler_init(handler, 8); + if (ret) + return ret; + handler->lock = &imx214->mutex; + + imx214->link_freq = v4l2_ctrl_new_int_menu(handler, NULL, + V4L2_CID_LINK_FREQ, + 1, 0, link_freq_items); + + dst_pixel_rate = (u32)link_freq_items[mode->link_freq_idx] / mode->bpp * 2 * lane_num; + + imx214->pixel_rate = v4l2_ctrl_new_std(handler, NULL, + V4L2_CID_PIXEL_RATE, + 0, IMX214_PIXEL_RATE, + 1, dst_pixel_rate); + + __v4l2_ctrl_s_ctrl(imx214->link_freq, + mode->link_freq_idx); + + h_blank = mode->hts_def - mode->width; + imx214->hblank = v4l2_ctrl_new_std(handler, NULL, V4L2_CID_HBLANK, + h_blank, h_blank, 1, h_blank); + if (imx214->hblank) + imx214->hblank->flags |= V4L2_CTRL_FLAG_READ_ONLY; + + vblank_def = mode->vts_def - mode->height; + imx214->vblank = v4l2_ctrl_new_std(handler, &imx214_ctrl_ops, + V4L2_CID_VBLANK, vblank_def, + IMX214_VTS_MAX - mode->height, + 1, vblank_def); + + exposure_max = mode->vts_def - 4; + imx214->exposure = v4l2_ctrl_new_std(handler, &imx214_ctrl_ops, + V4L2_CID_EXPOSURE, IMX214_EXPOSURE_MIN, + exposure_max, IMX214_EXPOSURE_STEP, + mode->exp_def); + + imx214->anal_gain = v4l2_ctrl_new_std(handler, &imx214_ctrl_ops, + V4L2_CID_ANALOGUE_GAIN, IMX214_GAIN_MIN, + IMX214_GAIN_MAX, IMX214_GAIN_STEP, + IMX214_GAIN_DEFAULT); + + imx214->test_pattern = v4l2_ctrl_new_std_menu_items(handler, + &imx214_ctrl_ops, V4L2_CID_TEST_PATTERN, + ARRAY_SIZE(imx214_test_pattern_menu) - 1, + 0, 0, imx214_test_pattern_menu); + + if (handler->error) { + ret = handler->error; + dev_err(&imx214->client->dev, + "Failed to init controls(%d)\n", ret); + goto err_free_handler; } - mode = v4l2_find_nearest_size(imx214_modes, - ARRAY_SIZE(imx214_modes), width, height, - imx214->fmt.width, imx214->fmt.height); - ret = imx214_write_table(imx214, mode->reg_table); - if (ret < 0) { - dev_err(imx214->dev, "could not sent mode table %d\n", ret); - goto error; - } - ret = __v4l2_ctrl_handler_setup(&imx214->ctrls); - if (ret < 0) { - dev_err(imx214->dev, "could not sync v4l2 controls\n"); - goto error; - } - ret = regmap_write(imx214->regmap, 0x100, 1); - if (ret < 0) { - dev_err(imx214->dev, "could not sent start table %d\n", ret); - goto error; - } + imx214->subdev.ctrl_handler = handler; - mutex_unlock(&imx214->mutex); return 0; -error: - mutex_unlock(&imx214->mutex); - return ret; -} - -static int imx214_stop_streaming(struct imx214 *imx214) -{ - int ret; - - ret = regmap_write(imx214->regmap, 0x100, 0); - if (ret < 0) - dev_err(imx214->dev, "could not sent stop table %d\n", ret); +err_free_handler: + v4l2_ctrl_handler_free(handler); return ret; } -static int imx214_s_stream(struct v4l2_subdev *subdev, int enable) +static int imx214_check_sensor_id(struct imx214 *imx214, + struct i2c_client *client) { - struct imx214 *imx214 = to_imx214(subdev); + struct device *dev = &imx214->client->dev; + u32 id = 0; int ret; - if (imx214->streaming == enable) - return 0; - - if (enable) { - ret = pm_runtime_get_sync(imx214->dev); - if (ret < 0) { - pm_runtime_put_noidle(imx214->dev); - return ret; - } - - ret = imx214_start_streaming(imx214); - if (ret < 0) - goto err_rpm_put; - } else { - ret = imx214_stop_streaming(imx214); - if (ret < 0) - goto err_rpm_put; - pm_runtime_put(imx214->dev); + ret = imx214_read_reg(client, IMX214_REG_CHIP_ID, + IMX214_REG_VALUE_16BIT, &id); + if (id != CHIP_ID) { + dev_err(dev, "Unexpected sensor id(%04x), ret(%d)\n", id, ret); + return -ENODEV; } - imx214->streaming = enable; - return 0; - -err_rpm_put: - pm_runtime_put(imx214->dev); - return ret; -} - -static int imx214_g_frame_interval(struct v4l2_subdev *subdev, - struct v4l2_subdev_frame_interval *fival) -{ - fival->interval.numerator = 1; - fival->interval.denominator = IMX214_FPS; + dev_info(dev, "Detected OV%04x sensor\n", id); return 0; } -static int imx214_enum_frame_interval(struct v4l2_subdev *subdev, - struct v4l2_subdev_pad_config *cfg, - struct v4l2_subdev_frame_interval_enum *fie) -{ - const struct imx214_mode *mode; - - if (fie->index != 0) - return -EINVAL; - - mode = v4l2_find_nearest_size(imx214_modes, - ARRAY_SIZE(imx214_modes), width, height, - fie->width, fie->height); - - fie->code = IMX214_MBUS_CODE; - fie->width = mode->width; - fie->height = mode->height; - fie->interval.numerator = 1; - fie->interval.denominator = IMX214_FPS; - - return 0; -} - -static const struct v4l2_subdev_video_ops imx214_video_ops = { - .s_stream = imx214_s_stream, - .g_frame_interval = imx214_g_frame_interval, - .s_frame_interval = imx214_g_frame_interval, -}; - -static const struct v4l2_subdev_pad_ops imx214_subdev_pad_ops = { - .enum_mbus_code = imx214_enum_mbus_code, - .enum_frame_size = imx214_enum_frame_size, - .enum_frame_interval = imx214_enum_frame_interval, - .get_fmt = imx214_get_format, - .set_fmt = imx214_set_format, - .get_selection = imx214_get_selection, - .init_cfg = imx214_entity_init_cfg, -}; - -static const struct v4l2_subdev_ops imx214_subdev_ops = { - .core = &imx214_core_ops, - .video = &imx214_video_ops, - .pad = &imx214_subdev_pad_ops, -}; - -static const struct regmap_config sensor_regmap_config = { - .reg_bits = 16, - .val_bits = 8, - .cache_type = REGCACHE_RBTREE, -}; - -static int imx214_get_regulators(struct device *dev, struct imx214 *imx214) +static int imx214_configure_regulators(struct imx214 *imx214) { unsigned int i; for (i = 0; i < IMX214_NUM_SUPPLIES; i++) - imx214->supplies[i].supply = imx214_supply_name[i]; + imx214->supplies[i].supply = imx214_supply_names[i]; - return devm_regulator_bulk_get(dev, IMX214_NUM_SUPPLIES, + return devm_regulator_bulk_get(&imx214->client->dev, + IMX214_NUM_SUPPLIES, imx214->supplies); } -static int imx214_parse_fwnode(struct device *dev) -{ - struct fwnode_handle *endpoint; - struct v4l2_fwnode_endpoint bus_cfg = { - .bus_type = V4L2_MBUS_CSI2_DPHY, - }; - unsigned int i; - int ret; - - endpoint = fwnode_graph_get_next_endpoint(dev_fwnode(dev), NULL); - if (!endpoint) { - dev_err(dev, "endpoint node not found\n"); - return -EINVAL; - } - - ret = v4l2_fwnode_endpoint_alloc_parse(endpoint, &bus_cfg); - if (ret) { - dev_err(dev, "parsing endpoint node failed\n"); - goto done; - } - - for (i = 0; i < bus_cfg.nr_of_link_frequencies; i++) - if (bus_cfg.link_frequencies[i] == IMX214_DEFAULT_LINK_FREQ) - break; - - if (i == bus_cfg.nr_of_link_frequencies) { - dev_err(dev, "link-frequencies %d not supported, Please review your DT\n", - IMX214_DEFAULT_LINK_FREQ); - ret = -EINVAL; - goto done; - } - -done: - v4l2_fwnode_endpoint_free(&bus_cfg); - fwnode_handle_put(endpoint); - return ret; -} - -static int __maybe_unused imx214_suspend(struct device *dev) -{ - struct i2c_client *client = to_i2c_client(dev); - struct v4l2_subdev *sd = i2c_get_clientdata(client); - struct imx214 *imx214 = to_imx214(sd); - - if (imx214->streaming) - imx214_stop_streaming(imx214); - - return 0; -} - -static int __maybe_unused imx214_resume(struct device *dev) -{ - struct i2c_client *client = to_i2c_client(dev); - struct v4l2_subdev *sd = i2c_get_clientdata(client); - struct imx214 *imx214 = to_imx214(sd); - int ret; - - if (imx214->streaming) { - ret = imx214_start_streaming(imx214); - if (ret) - goto error; - } - - return 0; - -error: - imx214_stop_streaming(imx214); - imx214->streaming = 0; - return ret; -} - -static int imx214_probe(struct i2c_client *client) +static int imx214_probe(struct i2c_client *client, + const struct i2c_device_id *id) { struct device *dev = &client->dev; + struct device_node *node = dev->of_node; struct imx214 *imx214; - static const s64 link_freq[] = { - IMX214_DEFAULT_LINK_FREQ, - }; - static const struct v4l2_area unit_size = { - .width = 1120, - .height = 1120, - }; + struct v4l2_subdev *sd; + struct device_node *endpoint; + char facing[2]; + struct device_node *eeprom_ctrl_node; + struct i2c_client *eeprom_ctrl_client; + struct v4l2_subdev *eeprom_ctrl; + struct imx214_otp_info *otp_ptr; int ret; - ret = imx214_parse_fwnode(dev); - if (ret) - return ret; + dev_info(dev, "driver version: %02x.%02x.%02x", + DRIVER_VERSION >> 16, + (DRIVER_VERSION & 0xff00) >> 8, + DRIVER_VERSION & 0x00ff); imx214 = devm_kzalloc(dev, sizeof(*imx214), GFP_KERNEL); if (!imx214) return -ENOMEM; - imx214->dev = dev; - - imx214->xclk = devm_clk_get(dev, NULL); - if (IS_ERR(imx214->xclk)) { - dev_err(dev, "could not get xclk"); - return PTR_ERR(imx214->xclk); + ret = of_property_read_u32(node, RKMODULE_CAMERA_MODULE_INDEX, + &imx214->module_index); + ret |= of_property_read_string(node, RKMODULE_CAMERA_MODULE_FACING, + &imx214->module_facing); + ret |= of_property_read_string(node, RKMODULE_CAMERA_MODULE_NAME, + &imx214->module_name); + ret |= of_property_read_string(node, RKMODULE_CAMERA_LENS_NAME, + &imx214->len_name); + if (ret) { + dev_err(dev, "could not get module information!\n"); + return -EINVAL; } - ret = clk_set_rate(imx214->xclk, IMX214_DEFAULT_CLK_FREQ); + imx214->client = client; + endpoint = of_graph_get_next_endpoint(dev->of_node, NULL); + if (!endpoint) { + dev_err(dev, "Failed to get endpoint\n"); + return -EINVAL; + } + ret = v4l2_fwnode_endpoint_parse(of_fwnode_handle(endpoint), + &imx214->bus_cfg); if (ret) { - dev_err(dev, "could not set xclk frequency\n"); + dev_err(dev, "Failed to get bus cfg\n"); + return ret; + } + if (imx214->bus_cfg.bus.mipi_csi2.num_data_lanes == 4) { + imx214->support_modes = supported_modes_4lane; + imx214->cfg_num = ARRAY_SIZE(supported_modes_4lane); + } else { + imx214->support_modes = supported_modes_2lane; + imx214->cfg_num = ARRAY_SIZE(supported_modes_2lane); + } + imx214->cur_mode = &imx214->support_modes[0]; + + imx214->xvclk = devm_clk_get(dev, "xvclk"); + if (IS_ERR(imx214->xvclk)) { + dev_err(dev, "Failed to get xvclk\n"); + return -EINVAL; + } + + imx214->power_gpio = devm_gpiod_get(dev, "power", GPIOD_OUT_LOW); + if (IS_ERR(imx214->power_gpio)) + dev_warn(dev, "Failed to get power-gpios, maybe no use\n"); + + imx214->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_LOW); + if (IS_ERR(imx214->reset_gpio)) + dev_warn(dev, "Failed to get reset-gpios\n"); + + imx214->pwdn_gpio = devm_gpiod_get(dev, "pwdn", GPIOD_OUT_LOW); + if (IS_ERR(imx214->pwdn_gpio)) + dev_warn(dev, "Failed to get pwdn-gpios\n"); + + ret = imx214_configure_regulators(imx214); + if (ret) { + dev_err(dev, "Failed to get power regulators\n"); return ret; } - ret = imx214_get_regulators(dev, imx214); - if (ret < 0) { - dev_err(dev, "cannot get regulators\n"); - return ret; + imx214->pinctrl = devm_pinctrl_get(dev); + if (!IS_ERR(imx214->pinctrl)) { + imx214->pins_default = + pinctrl_lookup_state(imx214->pinctrl, + OF_CAMERA_PINCTRL_STATE_DEFAULT); + if (IS_ERR(imx214->pins_default)) + dev_err(dev, "could not get default pinstate\n"); + + imx214->pins_sleep = + pinctrl_lookup_state(imx214->pinctrl, + OF_CAMERA_PINCTRL_STATE_SLEEP); + if (IS_ERR(imx214->pins_sleep)) + dev_err(dev, "could not get sleep pinstate\n"); } - imx214->enable_gpio = devm_gpiod_get(dev, "enable", GPIOD_OUT_LOW); - if (IS_ERR(imx214->enable_gpio)) { - dev_err(dev, "cannot get enable gpio\n"); - return PTR_ERR(imx214->enable_gpio); - } - - imx214->regmap = devm_regmap_init_i2c(client, &sensor_regmap_config); - if (IS_ERR(imx214->regmap)) { - dev_err(dev, "regmap init failed\n"); - return PTR_ERR(imx214->regmap); - } - - v4l2_i2c_subdev_init(&imx214->sd, client, &imx214_subdev_ops); - - /* - * Enable power initially, to avoid warnings - * from clk_disable on power_off - */ - imx214_power_on(imx214->dev); - - pm_runtime_set_active(imx214->dev); - pm_runtime_enable(imx214->dev); - pm_runtime_idle(imx214->dev); - - v4l2_ctrl_handler_init(&imx214->ctrls, 3); - - imx214->pixel_rate = v4l2_ctrl_new_std(&imx214->ctrls, NULL, - V4L2_CID_PIXEL_RATE, 0, - IMX214_DEFAULT_PIXEL_RATE, 1, - IMX214_DEFAULT_PIXEL_RATE); - imx214->link_freq = v4l2_ctrl_new_int_menu(&imx214->ctrls, NULL, - V4L2_CID_LINK_FREQ, - ARRAY_SIZE(link_freq) - 1, - 0, link_freq); - if (imx214->link_freq) - imx214->link_freq->flags |= V4L2_CTRL_FLAG_READ_ONLY; - - /* - * WARNING! - * Values obtained reverse engineering blobs and/or devices. - * Ranges and functionality might be wrong. - * - * Sony, please release some register set documentation for the - * device. - * - * Yours sincerely, Ricardo. - */ - imx214->exposure = v4l2_ctrl_new_std(&imx214->ctrls, &imx214_ctrl_ops, - V4L2_CID_EXPOSURE, - 0, 3184, 1, 0x0c70); - - imx214->unit_size = v4l2_ctrl_new_std_compound(&imx214->ctrls, - NULL, - V4L2_CID_UNIT_CELL_SIZE, - v4l2_ctrl_ptr_create((void *)&unit_size)); - ret = imx214->ctrls.error; - if (ret) { - dev_err(&client->dev, "%s control init failed (%d)\n", - __func__, ret); - goto free_ctrl; - } - - imx214->sd.ctrl_handler = &imx214->ctrls; mutex_init(&imx214->mutex); - imx214->ctrls.lock = &imx214->mutex; - imx214->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE; + sd = &imx214->subdev; + v4l2_i2c_subdev_init(sd, client, &imx214_subdev_ops); + ret = imx214_initialize_controls(imx214); + if (ret) + goto err_destroy_mutex; + + ret = __imx214_power_on(imx214); + if (ret) + goto err_free_handler; + + ret = imx214_check_sensor_id(imx214, client); + if (ret) + goto err_power_off; + + eeprom_ctrl_node = of_parse_phandle(node, "eeprom-ctrl", 0); + if (eeprom_ctrl_node) { + eeprom_ctrl_client = + of_find_i2c_device_by_node(eeprom_ctrl_node); + of_node_put(eeprom_ctrl_node); + if (IS_ERR_OR_NULL(eeprom_ctrl_client)) { + dev_err(dev, "can not get node\n"); + goto continue_probe; + } + eeprom_ctrl = i2c_get_clientdata(eeprom_ctrl_client); + if (IS_ERR_OR_NULL(eeprom_ctrl)) { + dev_err(dev, "can not get eeprom i2c client\n"); + } else { + otp_ptr = devm_kzalloc(dev, sizeof(*otp_ptr), + GFP_KERNEL); + if (!otp_ptr) + return -ENOMEM; + ret = v4l2_subdev_call(eeprom_ctrl, + core, ioctl, 0, otp_ptr); + if (!ret) { + imx214->otp = otp_ptr; + } else { + imx214->otp = NULL; + devm_kfree(dev, otp_ptr); + } + } + } + +continue_probe: + +#ifdef CONFIG_VIDEO_V4L2_SUBDEV_API + sd->internal_ops = &imx214_internal_ops; + sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE; +#endif +#if defined(CONFIG_MEDIA_CONTROLLER) imx214->pad.flags = MEDIA_PAD_FL_SOURCE; - imx214->sd.dev = &client->dev; - imx214->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR; + sd->entity.function = MEDIA_ENT_F_CAM_SENSOR; + ret = media_entity_pads_init(&sd->entity, 1, &imx214->pad); + if (ret < 0) + goto err_power_off; +#endif - ret = media_entity_pads_init(&imx214->sd.entity, 1, &imx214->pad); - if (ret < 0) { - dev_err(dev, "could not register media entity\n"); - goto free_ctrl; + memset(facing, 0, sizeof(facing)); + if (strcmp(imx214->module_facing, "back") == 0) + facing[0] = 'b'; + else + facing[0] = 'f'; + + snprintf(sd->name, sizeof(sd->name), "m%02d_%s_%s %s", + imx214->module_index, facing, + IMX214_NAME, dev_name(sd->dev)); + ret = v4l2_async_register_subdev_sensor_common(sd); + if (ret) { + dev_err(dev, "v4l2 async register subdev failed\n"); + goto err_clean_entity; } - imx214_entity_init_cfg(&imx214->sd, NULL); - - ret = v4l2_async_register_subdev_sensor_common(&imx214->sd); - if (ret < 0) { - dev_err(dev, "could not register v4l2 device\n"); - goto free_entity; - } + pm_runtime_set_active(dev); + pm_runtime_enable(dev); + pm_runtime_idle(dev); return 0; -free_entity: - media_entity_cleanup(&imx214->sd.entity); -free_ctrl: +err_clean_entity: +#if defined(CONFIG_MEDIA_CONTROLLER) + media_entity_cleanup(&sd->entity); +#endif +err_power_off: + __imx214_power_off(imx214); +err_free_handler: + v4l2_ctrl_handler_free(&imx214->ctrl_handler); +err_destroy_mutex: mutex_destroy(&imx214->mutex); - v4l2_ctrl_handler_free(&imx214->ctrls); - pm_runtime_disable(imx214->dev); return ret; } @@ -1084,41 +1922,57 @@ static int imx214_remove(struct i2c_client *client) struct v4l2_subdev *sd = i2c_get_clientdata(client); struct imx214 *imx214 = to_imx214(sd); - v4l2_async_unregister_subdev(&imx214->sd); - media_entity_cleanup(&imx214->sd.entity); - v4l2_ctrl_handler_free(&imx214->ctrls); + v4l2_async_unregister_subdev(sd); +#if defined(CONFIG_MEDIA_CONTROLLER) + media_entity_cleanup(&sd->entity); +#endif + v4l2_ctrl_handler_free(&imx214->ctrl_handler); + mutex_destroy(&imx214->mutex); pm_runtime_disable(&client->dev); + if (!pm_runtime_status_suspended(&client->dev)) + __imx214_power_off(imx214); pm_runtime_set_suspended(&client->dev); - mutex_destroy(&imx214->mutex); - return 0; } +#if IS_ENABLED(CONFIG_OF) static const struct of_device_id imx214_of_match[] = { { .compatible = "sony,imx214" }, - { } + {}, }; MODULE_DEVICE_TABLE(of, imx214_of_match); +#endif -static const struct dev_pm_ops imx214_pm_ops = { - SET_SYSTEM_SLEEP_PM_OPS(imx214_suspend, imx214_resume) - SET_RUNTIME_PM_OPS(imx214_power_off, imx214_power_on, NULL) +static const struct i2c_device_id imx214_match_id[] = { + { "sony,imx214", 0 }, + {}, }; static struct i2c_driver imx214_i2c_driver = { .driver = { - .of_match_table = imx214_of_match, + .name = IMX214_NAME, .pm = &imx214_pm_ops, - .name = "imx214", + .of_match_table = of_match_ptr(imx214_of_match), }, - .probe_new = imx214_probe, - .remove = imx214_remove, + .probe = &imx214_probe, + .remove = &imx214_remove, + .id_table = imx214_match_id, }; -module_i2c_driver(imx214_i2c_driver); +static int __init sensor_mod_init(void) +{ + return i2c_add_driver(&imx214_i2c_driver); +} -MODULE_DESCRIPTION("Sony IMX214 Camera driver"); -MODULE_AUTHOR("Ricardo Ribalda "); +static void __exit sensor_mod_exit(void) +{ + i2c_del_driver(&imx214_i2c_driver); +} + +device_initcall_sync(sensor_mod_init); +module_exit(sensor_mod_exit); + +MODULE_DESCRIPTION("Sony imx214 sensor driver"); MODULE_LICENSE("GPL v2"); diff --git a/drivers/media/i2c/imx214_eeprom.c b/drivers/media/i2c/imx214_eeprom.c new file mode 100644 index 000000000000..94d7460e3f94 --- /dev/null +++ b/drivers/media/i2c/imx214_eeprom.c @@ -0,0 +1,377 @@ +// SPDX-License-Identifier: GPL-2.0 +// Copyright (c) 2022 Fuzhou Rockchip Electronics Co., Ltd. + +#include +#include +#include +#include +#include +#include +#include +#include "imx214_eeprom_head.h" + +#define DEVICE_NAME "imx214_eeprom" + +static inline struct imx214_eeprom_device + *sd_to_imx214_eeprom(struct v4l2_subdev *subdev) +{ + return container_of(subdev, struct imx214_eeprom_device, sd); +} + +/* Read registers up to 4 at a time */ +static int imx214_read_reg_otp(struct i2c_client *client, u16 reg, + unsigned int len, u32 *val) +{ + struct i2c_msg msgs[2]; + u8 *data_be_p; + __be32 data_be = 0; + __be16 reg_addr_be = cpu_to_be16(reg); + int ret; + + if (len > 4 || !len) + 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 -EIO; + + *val = be32_to_cpu(data_be); + + return 0; +} + +static u8 get_vendor_flag(struct i2c_client *client) +{ + u8 vendor_flag = 0; + + if (client->addr == SLAVE_ADDRESS_GZ) + vendor_flag |= 0x80; + return vendor_flag; +} + +static int imx214_otp_read_gz(struct imx214_eeprom_device *imx214_eeprom_dev) +{ + struct i2c_client *client = imx214_eeprom_dev->client; + int otp_flag, i; + struct imx214_otp_info *otp_ptr; + struct device *dev = &imx214_eeprom_dev->client->dev; + int ret = 0; + u32 r_value, gr_value, gb_value, b_value; + u32 temp = 0; + u32 checksum = 0; + + otp_ptr = kzalloc(sizeof(*otp_ptr), GFP_KERNEL); + if (!otp_ptr) + return -ENOMEM; + + otp_flag = 0; + /* OTP base information*/ + ret = imx214_read_reg_otp(client, GZ_INFO_FLAG_REG, + 1, &otp_flag); + if (otp_flag == 0x01) { + otp_ptr->flag = 0x80; /* valid INFO in OTP */ + ret |= imx214_read_reg_otp(client, GZ_ID_REG, + 1, &otp_ptr->module_id); + ret |= imx214_read_reg_otp(client, GZ_LENS_ID_REG, + 1, &otp_ptr->lens_id); + ret |= imx214_read_reg_otp(client, GZ_PRODUCT_YEAR_REG, + 1, &otp_ptr->year); + ret |= imx214_read_reg_otp(client, GZ_PRODUCT_MONTH_REG, + 1, &otp_ptr->month); + ret |= imx214_read_reg_otp(client, GZ_PRODUCT_DAY_REG, + 1, &otp_ptr->day); + dev_dbg(dev, "fac info: module(0x%x) lens(0x%x) time(%d_%d_%d)!\n", + otp_ptr->module_id, + otp_ptr->lens_id, + otp_ptr->year, + otp_ptr->month, + otp_ptr->day); + if (ret) + goto err; + } + + /* OTP WB calibration data */ + ret = imx214_read_reg_otp(client, GZ_AWB_FLAG_REG, + 1, &otp_flag); + if (otp_flag == 0x01) { + otp_ptr->flag |= 0x40; /* valid AWB in OTP */ + ret |= imx214_read_reg_otp(client, GZ_CUR_R_REG, + 1, &r_value); + checksum += r_value; + ret |= imx214_read_reg_otp(client, GZ_CUR_GR_REG, + 1, &gr_value); + checksum += gr_value; + ret |= imx214_read_reg_otp(client, GZ_CUR_GB_REG, + 1, &gb_value); + checksum += gb_value; + ret |= imx214_read_reg_otp(client, GZ_CUR_B_REG, + 1, &b_value); + checksum += b_value; + otp_ptr->rg_ratio = + r_value * 1024 / ((gr_value + gb_value) / 2); + otp_ptr->bg_ratio = + b_value * 1024 / ((gr_value + gb_value) / 2); + ret |= imx214_read_reg_otp(client, GZ_GOLDEN_R_REG, + 1, &r_value); + checksum += r_value; + ret |= imx214_read_reg_otp(client, GZ_GOLDEN_GR_REG, + 1, &gr_value); + checksum += gr_value; + ret |= imx214_read_reg_otp(client, GZ_GOLDEN_GB_REG, + 1, &gb_value); + checksum += gb_value; + ret |= imx214_read_reg_otp(client, GZ_GOLDEN_B_REG, + 1, &b_value); + checksum += b_value; + otp_ptr->rg_golden = + r_value * 1024 / ((gr_value + gb_value) / 2); + otp_ptr->bg_golden = + b_value * 1024 / ((gr_value + gb_value) / 2); + ret |= imx214_read_reg_otp(client, GZ_AWB_CHECKSUM_REG, + 1, &temp); + if (ret != 0 || (checksum % 0xff) != temp) { + dev_err(dev, "otp awb info: check sum (%d,%d),ret = %d !\n", + checksum, + temp, + ret); + goto err; + } + dev_dbg(dev, "awb cur:(rg 0x%x, bg 0x%x,)\n", + otp_ptr->rg_ratio, otp_ptr->bg_ratio); + dev_dbg(dev, "awb gol:(rg 0x%x, bg 0x%x)\n", + otp_ptr->rg_golden, otp_ptr->bg_golden); + } + + checksum = 0; + /* OTP LSC calibration data */ + ret = imx214_read_reg_otp(client, GZ_LSC_FLAG_REG, + 1, &otp_flag); + if (otp_flag == 0x01) { + otp_ptr->flag |= 0x10; /* valid LSC in OTP */ + for (i = 0; i < 504; i++) { + ret |= imx214_read_reg_otp(client, + GZ_LSC_DATA_START_REG + i, + 1, &temp); + otp_ptr->lenc[i] = temp; + checksum += temp; + dev_dbg(dev, + "otp read lsc addr = 0x%04x, lenc[%d] = %d\n", + GZ_LSC_DATA_START_REG + i, i, temp); + } + ret |= imx214_read_reg_otp(client, GZ_LSC_CHECKSUM_REG, + 1, &temp); + if (ret != 0 || (checksum % 0xff) != temp) { + dev_err(dev, + "otp lsc info: check sum (%d,%d),ret = %d !\n", + checksum, temp, ret); + goto err; + } + } + + checksum = 0; + /* OTP VCM calibration data */ + ret = imx214_read_reg_otp(client, GZ_VCM_FLAG_REG, + 1, &otp_flag); + if (otp_flag == 0x01) { + otp_ptr->flag |= 0x20; /* valid VCM in OTP */ + ret |= imx214_read_reg_otp(client, GZ_VCM_DIR_REG, + 1, &otp_ptr->vcm_dir); + checksum += otp_ptr->vcm_dir; + ret |= imx214_read_reg_otp(client, GZ_VCM_START_REG, + 1, &temp); + checksum += temp; + ret |= imx214_read_reg_otp(client, GZ_VCM_START_REG + 1, + 1, &otp_ptr->vcm_start); + checksum += otp_ptr->vcm_start; + otp_ptr->vcm_start |= (temp << 8); + ret |= imx214_read_reg_otp(client, GZ_VCM_END_REG, + 1, &temp); + checksum += temp; + ret |= imx214_read_reg_otp(client, GZ_VCM_END_REG + 1, + 1, &otp_ptr->vcm_end); + checksum += otp_ptr->vcm_end; + otp_ptr->vcm_end |= (temp << 8); + ret |= imx214_read_reg_otp(client, GZ_VCM_CHECKSUM_REG, + 1, &temp); + if (ret != 0 || (checksum % 0xff) != temp) { + dev_err(dev, + "otp VCM info: check sum (%d,%d),ret = %d !\n", + checksum, temp, ret); + goto err; + } + dev_dbg(dev, "vcm_info: 0x%x, 0x%x, 0x%x!\n", + otp_ptr->vcm_start, + otp_ptr->vcm_end, + otp_ptr->vcm_dir); + } + + checksum = 0; + /* OTP SPC calibration data */ + ret = imx214_read_reg_otp(client, GZ_SPC_FLAG_REG, + 1, &otp_flag); + if (otp_flag == 0x01) { + otp_ptr->flag |= 0x08; /* valid LSC in OTP */ + for (i = 0; i < 126; i++) { + ret |= imx214_read_reg_otp(client, + GZ_SPC_DATA_START_REG + i, + 1, &temp); + otp_ptr->spc[i] = (uint8_t)temp; + checksum += temp; + dev_dbg(dev, + "otp read spc addr = 0x%04x, spc[%d] = %d\n", + GZ_SPC_DATA_START_REG + i, i, temp); + } + ret |= imx214_read_reg_otp(client, GZ_SPC_CHECKSUM_REG, + 1, &temp); + if (ret != 0 || (checksum % 0xff) != temp) { + dev_err(dev, + "otp spc info: check sum (%d,%d),ret = %d !\n", + checksum, temp, ret); + goto err; + } + } + + if (otp_ptr->flag) { + imx214_eeprom_dev->otp = otp_ptr; + } else { + imx214_eeprom_dev->otp = NULL; + kfree(otp_ptr); + } + + return 0; +err: + imx214_eeprom_dev->otp = NULL; + kfree(otp_ptr); + return -EINVAL; +} + +static int imx214_otp_read(struct imx214_eeprom_device *imx214_eeprom_dev) +{ + u8 vendor_flag = 0; + struct i2c_client *client = imx214_eeprom_dev->client; + + vendor_flag = get_vendor_flag(client); + if (vendor_flag == 0x80) + imx214_otp_read_gz(imx214_eeprom_dev); + return 0; +} + +static long imx214_eeprom_ioctl(struct v4l2_subdev *sd, + unsigned int cmd, void *arg) +{ + struct imx214_eeprom_device *imx214_eeprom_dev = + sd_to_imx214_eeprom(sd); + imx214_otp_read(imx214_eeprom_dev); + if (arg && imx214_eeprom_dev->otp) + memcpy(arg, imx214_eeprom_dev->otp, + sizeof(struct imx214_otp_info)); + return 0; +} + +static const struct v4l2_subdev_core_ops imx214_eeprom_core_ops = { + .ioctl = imx214_eeprom_ioctl, +}; + +static const struct v4l2_subdev_ops imx214_eeprom_ops = { + .core = &imx214_eeprom_core_ops, +}; + +static void imx214_eeprom_subdev_cleanup(struct imx214_eeprom_device *dev) +{ + v4l2_device_unregister_subdev(&dev->sd); + media_entity_cleanup(&dev->sd.entity); +} + +static int imx214_eeprom_probe(struct i2c_client *client, + const struct i2c_device_id *id) +{ + struct imx214_eeprom_device *imx214_eeprom_dev; + + dev_info(&client->dev, "probing...\n"); + imx214_eeprom_dev = devm_kzalloc(&client->dev, + sizeof(*imx214_eeprom_dev), + GFP_KERNEL); + + if (!imx214_eeprom_dev) { + dev_err(&client->dev, "Probe failed\n"); + return -ENOMEM; + } + v4l2_i2c_subdev_init(&imx214_eeprom_dev->sd, + client, &imx214_eeprom_ops); + imx214_eeprom_dev->client = client; + pm_runtime_set_active(&client->dev); + pm_runtime_enable(&client->dev); + pm_runtime_idle(&client->dev); + + dev_info(&client->dev, "probing successful\n"); + + return 0; +} + +static int imx214_eeprom_remove(struct i2c_client *client) +{ + struct v4l2_subdev *sd = i2c_get_clientdata(client); + struct imx214_eeprom_device *imx214_eeprom_dev = + sd_to_imx214_eeprom(sd); + kfree(imx214_eeprom_dev->otp); + pm_runtime_disable(&client->dev); + imx214_eeprom_subdev_cleanup(imx214_eeprom_dev); + + return 0; +} + +static int __maybe_unused imx214_eeprom_suspend(struct device *dev) +{ + return 0; +} + +static int __maybe_unused imx214_eeprom_resume(struct device *dev) +{ + return 0; +} + +static const struct i2c_device_id imx214_eeprom_id_table[] = { + { DEVICE_NAME, 0 }, + { { 0 } } +}; +MODULE_DEVICE_TABLE(i2c, imx214_eeprom_id_table); + +static const struct of_device_id imx214_eeprom_of_table[] = { + { .compatible = "sony,imx214_eeprom" }, + { { 0 } } +}; +MODULE_DEVICE_TABLE(of, imx214_eeprom_of_table); + +static const struct dev_pm_ops imx214_eeprom_pm_ops = { + SET_SYSTEM_SLEEP_PM_OPS(imx214_eeprom_suspend, imx214_eeprom_resume) + SET_RUNTIME_PM_OPS(imx214_eeprom_suspend, imx214_eeprom_resume, NULL) +}; + +static struct i2c_driver imx214_eeprom_i2c_driver = { + .driver = { + .name = DEVICE_NAME, + .pm = &imx214_eeprom_pm_ops, + .of_match_table = imx214_eeprom_of_table, + }, + .probe = &imx214_eeprom_probe, + .remove = &imx214_eeprom_remove, + .id_table = imx214_eeprom_id_table, +}; + +module_i2c_driver(imx214_eeprom_i2c_driver); + +MODULE_DESCRIPTION("IMX214 OTP driver"); +MODULE_LICENSE("GPL v2"); diff --git a/drivers/media/i2c/imx214_eeprom_head.h b/drivers/media/i2c/imx214_eeprom_head.h new file mode 100644 index 000000000000..49866ae40d08 --- /dev/null +++ b/drivers/media/i2c/imx214_eeprom_head.h @@ -0,0 +1,61 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* Copyright (c) 2022 Fuzhou Rockchip Electronics Co., Ltd. */ + +#ifndef IMX214_EEPROM_HEAD_H +#define IMX214_EEPROM_HEAD_H + +#define SLAVE_ADDRESS_GZ 0x50 +#define GZ_INFO_FLAG_REG 0X0000 +#define GZ_ID_REG 0X0005 +#define GZ_LENS_ID_REG 0X0006 +#define GZ_PRODUCT_YEAR_REG 0X000A +#define GZ_PRODUCT_MONTH_REG 0X000B +#define GZ_PRODUCT_DAY_REG 0X000C +#define GZ_AWB_FLAG_REG 0x001c +#define GZ_CUR_R_REG 0x001d +#define GZ_CUR_GR_REG 0x001e +#define GZ_CUR_GB_REG 0x001f +#define GZ_CUR_B_REG 0x0020 +#define GZ_GOLDEN_R_REG 0x0021 +#define GZ_GOLDEN_GR_REG 0x0022 +#define GZ_GOLDEN_GB_REG 0x0023 +#define GZ_GOLDEN_B_REG 0x0024 +#define GZ_AWB_CHECKSUM_REG 0x0025 +#define GZ_LSC_FLAG_REG 0X003A +#define GZ_LSC_DATA_START_REG 0x003B +#define GZ_LSC_CHECKSUM_REG 0x0233 +#define GZ_VCM_FLAG_REG 0X0788 +#define GZ_VCM_DIR_REG 0X0789 +#define GZ_VCM_START_REG 0X078C +#define GZ_VCM_END_REG 0X078A +#define GZ_VCM_CHECKSUM_REG 0x0790 +#define GZ_SPC_FLAG_REG 0X0CE1 +#define GZ_SPC_DATA_START_REG 0x0CE2 +#define GZ_SPC_CHECKSUM_REG 0x0d60 + +struct imx214_otp_info { + u32 flag; //bit[7]: info bit[6]:wb bit[5]:vcm bit[4]:lenc bit[3]:spc + u32 module_id; + u32 lens_id; + u32 year; + u32 month; + u32 day; + u32 rg_ratio; + u32 bg_ratio; + u32 rg_golden; + u32 bg_golden; + int vcm_start; + int vcm_end; + int vcm_dir; + u8 lenc[504]; + u8 spc[126]; +}; + +/* imx214_eeprom device structure */ +struct imx214_eeprom_device { + struct v4l2_subdev sd; + struct i2c_client *client; + struct imx214_otp_info *otp; +}; + +#endif /* IMX214_EEPROM_HEAD_H */