// SPDX-License-Identifier: GPL-2.0-only /* * Sony imx412 Camera Sensor Driver * * Copyright (C) 2021 Intel Corporation */ #include <linux/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 … #define IMX412_MODE_STANDBY … #define IMX412_MODE_STREAMING … /* Lines per frame */ #define IMX412_REG_LPFR … /* Chip ID */ #define IMX412_REG_ID … #define IMX412_ID … /* Exposure control */ #define IMX412_REG_EXPOSURE_CIT … #define IMX412_EXPOSURE_MIN … #define IMX412_EXPOSURE_OFFSET … #define IMX412_EXPOSURE_STEP … #define IMX412_EXPOSURE_DEFAULT … /* Analog gain control */ #define IMX412_REG_AGAIN … #define IMX412_AGAIN_MIN … #define IMX412_AGAIN_MAX … #define IMX412_AGAIN_STEP … #define IMX412_AGAIN_DEFAULT … /* Group hold register */ #define IMX412_REG_HOLD … /* Input clock rate */ #define IMX412_INCLK_RATE … /* CSI2 HW configuration */ #define IMX412_LINK_FREQ … #define IMX412_NUM_DATA_LANES … #define IMX412_REG_MIN … #define IMX412_REG_MAX … /** * struct imx412_reg - imx412 sensor register * @address: Register address * @val: Register value */ struct imx412_reg { … }; /** * 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 { … }; /** * 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 { … }; static const char * const imx412_supply_names[] = …; /** * 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 { … }; static const s64 link_freq[] = …; /* Sensor mode registers */ static const struct imx412_reg mode_4056x3040_regs[] = …; /* Supported sensor mode configurations */ static const struct imx412_mode supported_mode = …; /** * to_imx412() - imx412 V4L2 sub-device to imx412 device. * @subdev: pointer to imx412 V4L2 sub-device * * Return: pointer to imx412 device */ static inline struct imx412 *to_imx412(struct v4l2_subdev *subdev) { … } /** * imx412_read_reg() - Read registers. * @imx412: pointer to imx412 device * @reg: register address * @len: length of bytes to read. Max supported bytes is 4 * @val: pointer to register value to be filled. * * Return: 0 if successful, error code otherwise. */ static int imx412_read_reg(struct imx412 *imx412, u16 reg, u32 len, u32 *val) { … } /** * imx412_write_reg() - Write register * @imx412: pointer to imx412 device * @reg: register address * @len: length of bytes. Max supported bytes is 4 * @val: register value * * Return: 0 if successful, error code otherwise. */ static int imx412_write_reg(struct imx412 *imx412, u16 reg, u32 len, u32 val) { … } /** * imx412_write_regs() - Write a list of registers * @imx412: pointer to imx412 device * @regs: list of registers to be written * @len: length of registers array * * Return: 0 if successful, error code otherwise. */ static int imx412_write_regs(struct imx412 *imx412, const struct imx412_reg *regs, u32 len) { … } /** * imx412_update_controls() - Update control ranges based on streaming mode * @imx412: pointer to imx412 device * @mode: pointer to imx412_mode sensor mode * * Return: 0 if successful, error code otherwise. */ static int imx412_update_controls(struct imx412 *imx412, const struct imx412_mode *mode) { … } /** * imx412_update_exp_gain() - Set updated exposure and gain * @imx412: pointer to imx412 device * @exposure: updated exposure value * @gain: updated analog gain value * * Return: 0 if successful, error code otherwise. */ static int imx412_update_exp_gain(struct imx412 *imx412, u32 exposure, u32 gain) { … } /** * imx412_set_ctrl() - Set subdevice control * @ctrl: pointer to v4l2_ctrl structure * * Supported controls: * - V4L2_CID_VBLANK * - cluster controls: * - V4L2_CID_ANALOGUE_GAIN * - V4L2_CID_EXPOSURE * * Return: 0 if successful, error code otherwise. */ static int imx412_set_ctrl(struct v4l2_ctrl *ctrl) { … } /* V4l2 subdevice control ops*/ static const struct v4l2_ctrl_ops imx412_ctrl_ops = …; /** * imx412_enum_mbus_code() - Enumerate V4L2 sub-device mbus codes * @sd: pointer to imx412 V4L2 sub-device structure * @sd_state: V4L2 sub-device configuration * @code: V4L2 sub-device code enumeration need to be filled * * Return: 0 if successful, error code otherwise. */ static int imx412_enum_mbus_code(struct v4l2_subdev *sd, struct v4l2_subdev_state *sd_state, struct v4l2_subdev_mbus_code_enum *code) { … } /** * imx412_enum_frame_size() - Enumerate V4L2 sub-device frame sizes * @sd: pointer to imx412 V4L2 sub-device structure * @sd_state: V4L2 sub-device configuration * @fsize: V4L2 sub-device size enumeration need to be filled * * Return: 0 if successful, error code otherwise. */ static int imx412_enum_frame_size(struct v4l2_subdev *sd, struct v4l2_subdev_state *sd_state, struct v4l2_subdev_frame_size_enum *fsize) { … } /** * imx412_fill_pad_format() - Fill subdevice pad format * from selected sensor mode * @imx412: pointer to imx412 device * @mode: pointer to imx412_mode sensor mode * @fmt: V4L2 sub-device format need to be filled */ static void imx412_fill_pad_format(struct imx412 *imx412, const struct imx412_mode *mode, struct v4l2_subdev_format *fmt) { … } /** * imx412_get_pad_format() - Get subdevice pad format * @sd: pointer to imx412 V4L2 sub-device structure * @sd_state: V4L2 sub-device configuration * @fmt: V4L2 sub-device format need to be set * * Return: 0 if successful, error code otherwise. */ static int imx412_get_pad_format(struct v4l2_subdev *sd, struct v4l2_subdev_state *sd_state, struct v4l2_subdev_format *fmt) { … } /** * imx412_set_pad_format() - Set subdevice pad format * @sd: pointer to imx412 V4L2 sub-device structure * @sd_state: V4L2 sub-device configuration * @fmt: V4L2 sub-device format need to be set * * Return: 0 if successful, error code otherwise. */ static int imx412_set_pad_format(struct v4l2_subdev *sd, struct v4l2_subdev_state *sd_state, struct v4l2_subdev_format *fmt) { … } /** * imx412_init_state() - Initialize sub-device state * @sd: pointer to imx412 V4L2 sub-device structure * @sd_state: V4L2 sub-device configuration * * Return: 0 if successful, error code otherwise. */ static int imx412_init_state(struct v4l2_subdev *sd, struct v4l2_subdev_state *sd_state) { … } /** * imx412_start_streaming() - Start sensor stream * @imx412: pointer to imx412 device * * Return: 0 if successful, error code otherwise. */ static int imx412_start_streaming(struct imx412 *imx412) { … } /** * imx412_stop_streaming() - Stop sensor stream * @imx412: pointer to imx412 device * * Return: 0 if successful, error code otherwise. */ static int imx412_stop_streaming(struct imx412 *imx412) { … } /** * imx412_set_stream() - Enable sensor streaming * @sd: pointer to imx412 subdevice * @enable: set to enable sensor streaming * * Return: 0 if successful, error code otherwise. */ static int imx412_set_stream(struct v4l2_subdev *sd, int enable) { … } /** * imx412_detect() - Detect imx412 sensor * @imx412: pointer to imx412 device * * Return: 0 if successful, -EIO if sensor id does not match */ static int imx412_detect(struct imx412 *imx412) { … } /** * imx412_parse_hw_config() - Parse HW configuration and check if supported * @imx412: pointer to imx412 device * * Return: 0 if successful, error code otherwise. */ static int imx412_parse_hw_config(struct imx412 *imx412) { … } /* V4l2 subdevice ops */ static const struct v4l2_subdev_video_ops imx412_video_ops = …; static const struct v4l2_subdev_pad_ops imx412_pad_ops = …; static const struct v4l2_subdev_ops imx412_subdev_ops = …; static const struct v4l2_subdev_internal_ops imx412_internal_ops = …; /** * imx412_power_on() - Sensor power on sequence * @dev: pointer to i2c device * * Return: 0 if successful, error code otherwise. */ static int imx412_power_on(struct device *dev) { … } /** * imx412_power_off() - Sensor power off sequence * @dev: pointer to i2c device * * Return: 0 if successful, error code otherwise. */ static int imx412_power_off(struct device *dev) { … } /** * imx412_init_controls() - Initialize sensor subdevice controls * @imx412: pointer to imx412 device * * Return: 0 if successful, error code otherwise. */ static int imx412_init_controls(struct imx412 *imx412) { … } /** * imx412_probe() - I2C client device binding * @client: pointer to i2c client device * * Return: 0 if successful, error code otherwise. */ static int imx412_probe(struct i2c_client *client) { … } /** * imx412_remove() - I2C client device unbinding * @client: pointer to I2C client device * * Return: 0 if successful, error code otherwise. */ static void imx412_remove(struct i2c_client *client) { … } static const struct dev_pm_ops imx412_pm_ops = …; static const struct of_device_id imx412_of_match[] = …; MODULE_DEVICE_TABLE(of, imx412_of_match); static struct i2c_driver imx412_driver = …; module_i2c_driver(…) …; MODULE_DESCRIPTION(…) …; MODULE_LICENSE(…) …;