// SPDX-License-Identifier: GPL-2.0-only
/*
* Sony imx412 Camera Sensor Driver
*
* Copyright (C) 2021 Intel Corporation
*/
#include <asm/unaligned.h>
#include <linux/clk.h>
#include <linux/delay.h>
#include <linux/i2c.h>
#include <linux/module.h>
#include <linux/pm_runtime.h>
#include <linux/regulator/consumer.h>
#include <media/v4l2-ctrls.h>
#include <media/v4l2-fwnode.h>
#include <media/v4l2-subdev.h>
/* Streaming Mode */
#define IMX412_REG_MODE_SELECT 0x0100
#define IMX412_MODE_STANDBY 0x00
#define IMX412_MODE_STREAMING 0x01
/* Lines per frame */
#define IMX412_REG_LPFR 0x0340
/* Chip ID */
#define IMX412_REG_ID 0x0016
#define IMX412_ID 0x577
/* Exposure control */
#define IMX412_REG_EXPOSURE_CIT 0x0202
#define IMX412_EXPOSURE_MIN 8
#define IMX412_EXPOSURE_OFFSET 22
#define IMX412_EXPOSURE_STEP 1
#define IMX412_EXPOSURE_DEFAULT 0x0648
/* Analog gain control */
#define IMX412_REG_AGAIN 0x0204
#define IMX412_AGAIN_MIN 0
#define IMX412_AGAIN_MAX 978
#define IMX412_AGAIN_STEP 1
#define IMX412_AGAIN_DEFAULT 0
/* Group hold register */
#define IMX412_REG_HOLD 0x0104
/* Input clock rate */
#define IMX412_INCLK_RATE 24000000
/* CSI2 HW configuration */
#define IMX412_LINK_FREQ 600000000
#define IMX412_NUM_DATA_LANES 4
#define IMX412_REG_MIN 0x00
#define IMX412_REG_MAX 0xffff
/**
* struct imx412_reg - imx412 sensor register
* @address: Register address
* @val: Register value
*/
struct imx412_reg {
u16 address;
u8 val;
};
/**
* struct imx412_reg_list - imx412 sensor register list
* @num_of_regs: Number of registers in the list
* @regs: Pointer to register list
*/
struct imx412_reg_list {
u32 num_of_regs;
const struct imx412_reg *regs;
};
/**
* struct imx412_mode - imx412 sensor mode structure
* @width: Frame width
* @height: Frame height
* @code: Format code
* @hblank: Horizontal blanking in lines
* @vblank: Vertical blanking in lines
* @vblank_min: Minimum vertical blanking in lines
* @vblank_max: Maximum vertical blanking in lines
* @pclk: Sensor pixel clock
* @link_freq_idx: Link frequency index
* @reg_list: Register list for sensor mode
*/
struct imx412_mode {
u32 width;
u32 height;
u32 code;
u32 hblank;
u32 vblank;
u32 vblank_min;
u32 vblank_max;
u64 pclk;
u32 link_freq_idx;
struct imx412_reg_list reg_list;
};
static const char * const imx412_supply_names[] = {
"dovdd", /* Digital I/O power */
"avdd", /* Analog power */
"dvdd", /* Digital core power */
};
/**
* struct imx412 - imx412 sensor device structure
* @dev: Pointer to generic device
* @client: Pointer to i2c client
* @sd: V4L2 sub-device
* @pad: Media pad. Only one pad supported
* @reset_gpio: Sensor reset gpio
* @inclk: Sensor input clock
* @supplies: Regulator supplies
* @ctrl_handler: V4L2 control handler
* @link_freq_ctrl: Pointer to link frequency control
* @pclk_ctrl: Pointer to pixel clock control
* @hblank_ctrl: Pointer to horizontal blanking control
* @vblank_ctrl: Pointer to vertical blanking control
* @exp_ctrl: Pointer to exposure control
* @again_ctrl: Pointer to analog gain control
* @vblank: Vertical blanking in lines
* @cur_mode: Pointer to current selected sensor mode
* @mutex: Mutex for serializing sensor controls
*/
struct imx412 {
struct device *dev;
struct i2c_client *client;
struct v4l2_subdev sd;
struct media_pad pad;
struct gpio_desc *reset_gpio;
struct clk *inclk;
struct regulator_bulk_data supplies[ARRAY_SIZE(imx412_supply_names)];
struct v4l2_ctrl_handler ctrl_handler;
struct v4l2_ctrl *link_freq_ctrl;
struct v4l2_ctrl *pclk_ctrl;
struct v4l2_ctrl *hblank_ctrl;
struct v4l2_ctrl *vblank_ctrl;
struct {
struct v4l2_ctrl *exp_ctrl;
struct v4l2_ctrl *again_ctrl;
};
u32 vblank;
const struct imx412_mode *cur_mode;
struct mutex mutex;
};
static const s64 link_freq[] = {
IMX412_LINK_FREQ,
};
/* Sensor mode registers */
static const struct imx412_reg mode_4056x3040_regs[] = {
{0x0136, 0x18},
{0x0137, 0x00},
{0x3c7e, 0x08},
{0x3c7f, 0x02},
{0x38a8