12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 219a81c14SSteve Longerbeam /* 319a81c14SSteve Longerbeam * Copyright (C) 2011-2013 Freescale Semiconductor, Inc. All Rights Reserved. 419a81c14SSteve Longerbeam * Copyright (C) 2014-2017 Mentor Graphics Inc. 519a81c14SSteve Longerbeam */ 619a81c14SSteve Longerbeam 719a81c14SSteve Longerbeam #include <linux/clk.h> 819a81c14SSteve Longerbeam #include <linux/clk-provider.h> 919a81c14SSteve Longerbeam #include <linux/clkdev.h> 1019a81c14SSteve Longerbeam #include <linux/ctype.h> 1119a81c14SSteve Longerbeam #include <linux/delay.h> 1219a81c14SSteve Longerbeam #include <linux/device.h> 1341d8d7f5SHugues Fruchet #include <linux/gpio/consumer.h> 1419a81c14SSteve Longerbeam #include <linux/i2c.h> 1519a81c14SSteve Longerbeam #include <linux/init.h> 1619a81c14SSteve Longerbeam #include <linux/module.h> 1719a81c14SSteve Longerbeam #include <linux/of_device.h> 1841d8d7f5SHugues Fruchet #include <linux/regulator/consumer.h> 1919a81c14SSteve Longerbeam #include <linux/slab.h> 2019a81c14SSteve Longerbeam #include <linux/types.h> 2119a81c14SSteve Longerbeam #include <media/v4l2-async.h> 2219a81c14SSteve Longerbeam #include <media/v4l2-ctrls.h> 2319a81c14SSteve Longerbeam #include <media/v4l2-device.h> 242d18fbc5SAkinobu Mita #include <media/v4l2-event.h> 2519a81c14SSteve Longerbeam #include <media/v4l2-fwnode.h> 2619a81c14SSteve Longerbeam #include <media/v4l2-subdev.h> 2719a81c14SSteve Longerbeam 2819a81c14SSteve Longerbeam /* min/typical/max system clock (xclk) frequencies */ 2919a81c14SSteve Longerbeam #define OV5640_XCLK_MIN 6000000 3041cb1c73SPhilipp Puschmann #define OV5640_XCLK_MAX 54000000 3119a81c14SSteve Longerbeam 3219a81c14SSteve Longerbeam #define OV5640_DEFAULT_SLAVE_ID 0x3c 3319a81c14SSteve Longerbeam 34d47c4126SHugues Fruchet #define OV5640_REG_SYS_RESET02 0x3002 35d47c4126SHugues Fruchet #define OV5640_REG_SYS_CLOCK_ENABLE02 0x3006 36f22996dbSHugues Fruchet #define OV5640_REG_SYS_CTRL0 0x3008 373b987d70SLad Prabhakar #define OV5640_REG_SYS_CTRL0_SW_PWDN 0x42 383b987d70SLad Prabhakar #define OV5640_REG_SYS_CTRL0_SW_PWUP 0x02 3919a81c14SSteve Longerbeam #define OV5640_REG_CHIP_ID 0x300a 40f22996dbSHugues Fruchet #define OV5640_REG_IO_MIPI_CTRL00 0x300e 41f22996dbSHugues Fruchet #define OV5640_REG_PAD_OUTPUT_ENABLE01 0x3017 42f22996dbSHugues Fruchet #define OV5640_REG_PAD_OUTPUT_ENABLE02 0x3018 4319a81c14SSteve Longerbeam #define OV5640_REG_PAD_OUTPUT00 0x3019 44f22996dbSHugues Fruchet #define OV5640_REG_SYSTEM_CONTROL1 0x302e 4519a81c14SSteve Longerbeam #define OV5640_REG_SC_PLL_CTRL0 0x3034 4619a81c14SSteve Longerbeam #define OV5640_REG_SC_PLL_CTRL1 0x3035 4719a81c14SSteve Longerbeam #define OV5640_REG_SC_PLL_CTRL2 0x3036 4819a81c14SSteve Longerbeam #define OV5640_REG_SC_PLL_CTRL3 0x3037 4919a81c14SSteve Longerbeam #define OV5640_REG_SLAVE_ID 0x3100 50f22996dbSHugues Fruchet #define OV5640_REG_SCCB_SYS_CTRL1 0x3103 5119a81c14SSteve Longerbeam #define OV5640_REG_SYS_ROOT_DIVIDER 0x3108 5219a81c14SSteve Longerbeam #define OV5640_REG_AWB_R_GAIN 0x3400 5319a81c14SSteve Longerbeam #define OV5640_REG_AWB_G_GAIN 0x3402 5419a81c14SSteve Longerbeam #define OV5640_REG_AWB_B_GAIN 0x3404 5519a81c14SSteve Longerbeam #define OV5640_REG_AWB_MANUAL_CTRL 0x3406 5619a81c14SSteve Longerbeam #define OV5640_REG_AEC_PK_EXPOSURE_HI 0x3500 5719a81c14SSteve Longerbeam #define OV5640_REG_AEC_PK_EXPOSURE_MED 0x3501 5819a81c14SSteve Longerbeam #define OV5640_REG_AEC_PK_EXPOSURE_LO 0x3502 5919a81c14SSteve Longerbeam #define OV5640_REG_AEC_PK_MANUAL 0x3503 6019a81c14SSteve Longerbeam #define OV5640_REG_AEC_PK_REAL_GAIN 0x350a 6119a81c14SSteve Longerbeam #define OV5640_REG_AEC_PK_VTS 0x350c 6286633417SMaxime Ripard #define OV5640_REG_TIMING_DVPHO 0x3808 6386633417SMaxime Ripard #define OV5640_REG_TIMING_DVPVO 0x380a 6419a81c14SSteve Longerbeam #define OV5640_REG_TIMING_HTS 0x380c 6519a81c14SSteve Longerbeam #define OV5640_REG_TIMING_VTS 0x380e 66ce85705aSHugues Fruchet #define OV5640_REG_TIMING_TC_REG20 0x3820 6719a81c14SSteve Longerbeam #define OV5640_REG_TIMING_TC_REG21 0x3821 6819a81c14SSteve Longerbeam #define OV5640_REG_AEC_CTRL00 0x3a00 6919a81c14SSteve Longerbeam #define OV5640_REG_AEC_B50_STEP 0x3a08 7019a81c14SSteve Longerbeam #define OV5640_REG_AEC_B60_STEP 0x3a0a 7119a81c14SSteve Longerbeam #define OV5640_REG_AEC_CTRL0D 0x3a0d 7219a81c14SSteve Longerbeam #define OV5640_REG_AEC_CTRL0E 0x3a0e 7319a81c14SSteve Longerbeam #define OV5640_REG_AEC_CTRL0F 0x3a0f 7419a81c14SSteve Longerbeam #define OV5640_REG_AEC_CTRL10 0x3a10 7519a81c14SSteve Longerbeam #define OV5640_REG_AEC_CTRL11 0x3a11 7619a81c14SSteve Longerbeam #define OV5640_REG_AEC_CTRL1B 0x3a1b 7719a81c14SSteve Longerbeam #define OV5640_REG_AEC_CTRL1E 0x3a1e 7819a81c14SSteve Longerbeam #define OV5640_REG_AEC_CTRL1F 0x3a1f 7919a81c14SSteve Longerbeam #define OV5640_REG_HZ5060_CTRL00 0x3c00 8019a81c14SSteve Longerbeam #define OV5640_REG_HZ5060_CTRL01 0x3c01 8119a81c14SSteve Longerbeam #define OV5640_REG_SIGMADELTA_CTRL0C 0x3c0c 8219a81c14SSteve Longerbeam #define OV5640_REG_FRAME_CTRL01 0x4202 83e3ee691dSHugues Fruchet #define OV5640_REG_FORMAT_CONTROL00 0x4300 847cb013b1SChen-Yu Tsai #define OV5640_REG_VFIFO_HSIZE 0x4602 857cb013b1SChen-Yu Tsai #define OV5640_REG_VFIFO_VSIZE 0x4604 862b5c18f9SChen-Yu Tsai #define OV5640_REG_JPG_MODE_SELECT 0x4713 874039b037SLad Prabhakar #define OV5640_REG_CCIR656_CTRL00 0x4730 88f22996dbSHugues Fruchet #define OV5640_REG_POLARITY_CTRL00 0x4740 8919a81c14SSteve Longerbeam #define OV5640_REG_MIPI_CTRL00 0x4800 9019a81c14SSteve Longerbeam #define OV5640_REG_DEBUG_MODE 0x4814 91e3ee691dSHugues Fruchet #define OV5640_REG_ISP_FORMAT_MUX_CTRL 0x501f 9219a81c14SSteve Longerbeam #define OV5640_REG_PRE_ISP_TEST_SET1 0x503d 9319a81c14SSteve Longerbeam #define OV5640_REG_SDE_CTRL0 0x5580 9419a81c14SSteve Longerbeam #define OV5640_REG_SDE_CTRL1 0x5581 9519a81c14SSteve Longerbeam #define OV5640_REG_SDE_CTRL3 0x5583 9619a81c14SSteve Longerbeam #define OV5640_REG_SDE_CTRL4 0x5584 9719a81c14SSteve Longerbeam #define OV5640_REG_SDE_CTRL5 0x5585 9819a81c14SSteve Longerbeam #define OV5640_REG_AVG_READOUT 0x56a1 9919a81c14SSteve Longerbeam 10019a81c14SSteve Longerbeam enum ov5640_mode_id { 10132ea5e05SHugues Fruchet OV5640_MODE_QQVGA_160_120 = 0, 10232ea5e05SHugues Fruchet OV5640_MODE_QCIF_176_144, 10319a81c14SSteve Longerbeam OV5640_MODE_QVGA_320_240, 10419a81c14SSteve Longerbeam OV5640_MODE_VGA_640_480, 10519a81c14SSteve Longerbeam OV5640_MODE_NTSC_720_480, 10619a81c14SSteve Longerbeam OV5640_MODE_PAL_720_576, 10719a81c14SSteve Longerbeam OV5640_MODE_XGA_1024_768, 10819a81c14SSteve Longerbeam OV5640_MODE_720P_1280_720, 10919a81c14SSteve Longerbeam OV5640_MODE_1080P_1920_1080, 11019a81c14SSteve Longerbeam OV5640_MODE_QSXGA_2592_1944, 11119a81c14SSteve Longerbeam OV5640_NUM_MODES, 11219a81c14SSteve Longerbeam }; 11319a81c14SSteve Longerbeam 11419a81c14SSteve Longerbeam enum ov5640_frame_rate { 11519a81c14SSteve Longerbeam OV5640_15_FPS = 0, 11619a81c14SSteve Longerbeam OV5640_30_FPS, 117e823fb16SMaxime Ripard OV5640_60_FPS, 11819a81c14SSteve Longerbeam OV5640_NUM_FRAMERATES, 11919a81c14SSteve Longerbeam }; 12019a81c14SSteve Longerbeam 121b7ed3abdSLoic Poulain enum ov5640_format_mux { 122b7ed3abdSLoic Poulain OV5640_FMT_MUX_YUV422 = 0, 123b7ed3abdSLoic Poulain OV5640_FMT_MUX_RGB, 124b7ed3abdSLoic Poulain OV5640_FMT_MUX_DITHER, 125b7ed3abdSLoic Poulain OV5640_FMT_MUX_RAW_DPC, 126b7ed3abdSLoic Poulain OV5640_FMT_MUX_SNR_RAW, 127b7ed3abdSLoic Poulain OV5640_FMT_MUX_RAW_CIP, 128b7ed3abdSLoic Poulain }; 129b7ed3abdSLoic Poulain 130e3ee691dSHugues Fruchet struct ov5640_pixfmt { 131e3ee691dSHugues Fruchet u32 code; 132e3ee691dSHugues Fruchet u32 colorspace; 133e3ee691dSHugues Fruchet }; 134e3ee691dSHugues Fruchet 135e3ee691dSHugues Fruchet static const struct ov5640_pixfmt ov5640_formats[] = { 136d47c4126SHugues Fruchet { MEDIA_BUS_FMT_JPEG_1X8, V4L2_COLORSPACE_JPEG, }, 137e3ee691dSHugues Fruchet { MEDIA_BUS_FMT_UYVY8_2X8, V4L2_COLORSPACE_SRGB, }, 138e3ee691dSHugues Fruchet { MEDIA_BUS_FMT_YUYV8_2X8, V4L2_COLORSPACE_SRGB, }, 139e3ee691dSHugues Fruchet { MEDIA_BUS_FMT_RGB565_2X8_LE, V4L2_COLORSPACE_SRGB, }, 140e3ee691dSHugues Fruchet { MEDIA_BUS_FMT_RGB565_2X8_BE, V4L2_COLORSPACE_SRGB, }, 141b7ed3abdSLoic Poulain { MEDIA_BUS_FMT_SBGGR8_1X8, V4L2_COLORSPACE_SRGB, }, 142b7ed3abdSLoic Poulain { MEDIA_BUS_FMT_SGBRG8_1X8, V4L2_COLORSPACE_SRGB, }, 143b7ed3abdSLoic Poulain { MEDIA_BUS_FMT_SGRBG8_1X8, V4L2_COLORSPACE_SRGB, }, 144b7ed3abdSLoic Poulain { MEDIA_BUS_FMT_SRGGB8_1X8, V4L2_COLORSPACE_SRGB, }, 145e3ee691dSHugues Fruchet }; 146e3ee691dSHugues Fruchet 14719a81c14SSteve Longerbeam /* 14819a81c14SSteve Longerbeam * FIXME: remove this when a subdev API becomes available 14919a81c14SSteve Longerbeam * to set the MIPI CSI-2 virtual channel. 15019a81c14SSteve Longerbeam */ 15119a81c14SSteve Longerbeam static unsigned int virtual_channel; 1528670d70aSHugues Fruchet module_param(virtual_channel, uint, 0444); 15319a81c14SSteve Longerbeam MODULE_PARM_DESC(virtual_channel, 15419a81c14SSteve Longerbeam "MIPI CSI-2 virtual channel (0..3), default 0"); 15519a81c14SSteve Longerbeam 15619a81c14SSteve Longerbeam static const int ov5640_framerates[] = { 15719a81c14SSteve Longerbeam [OV5640_15_FPS] = 15, 15819a81c14SSteve Longerbeam [OV5640_30_FPS] = 30, 159e823fb16SMaxime Ripard [OV5640_60_FPS] = 60, 16019a81c14SSteve Longerbeam }; 16119a81c14SSteve Longerbeam 16219a81c14SSteve Longerbeam /* regulator supplies */ 16319a81c14SSteve Longerbeam static const char * const ov5640_supply_name[] = { 16441d8d7f5SHugues Fruchet "DOVDD", /* Digital I/O (1.8V) supply */ 16519a81c14SSteve Longerbeam "AVDD", /* Analog (2.8V) supply */ 16624c8ac89SFabio Estevam "DVDD", /* Digital Core (1.5V) supply */ 16719a81c14SSteve Longerbeam }; 16819a81c14SSteve Longerbeam 16919a81c14SSteve Longerbeam #define OV5640_NUM_SUPPLIES ARRAY_SIZE(ov5640_supply_name) 17019a81c14SSteve Longerbeam 17119a81c14SSteve Longerbeam /* 17219a81c14SSteve Longerbeam * Image size under 1280 * 960 are SUBSAMPLING 17319a81c14SSteve Longerbeam * Image size upper 1280 * 960 are SCALING 17419a81c14SSteve Longerbeam */ 17519a81c14SSteve Longerbeam enum ov5640_downsize_mode { 17619a81c14SSteve Longerbeam SUBSAMPLING, 17719a81c14SSteve Longerbeam SCALING, 17819a81c14SSteve Longerbeam }; 17919a81c14SSteve Longerbeam 18019a81c14SSteve Longerbeam struct reg_value { 18119a81c14SSteve Longerbeam u16 reg_addr; 18219a81c14SSteve Longerbeam u8 val; 18319a81c14SSteve Longerbeam u8 mask; 18419a81c14SSteve Longerbeam u32 delay_ms; 18519a81c14SSteve Longerbeam }; 18619a81c14SSteve Longerbeam 18719a81c14SSteve Longerbeam struct ov5640_mode_info { 18819a81c14SSteve Longerbeam enum ov5640_mode_id id; 18919a81c14SSteve Longerbeam enum ov5640_downsize_mode dn_mode; 190dba13a0bSMaxime Ripard u32 hact; 191476dec01SMaxime Ripard u32 htot; 192dba13a0bSMaxime Ripard u32 vact; 193476dec01SMaxime Ripard u32 vtot; 19419a81c14SSteve Longerbeam const struct reg_value *reg_data; 19519a81c14SSteve Longerbeam u32 reg_data_size; 1965554c80eSAdam Ford u32 max_fps; 19719a81c14SSteve Longerbeam }; 19819a81c14SSteve Longerbeam 19919a81c14SSteve Longerbeam struct ov5640_ctrls { 20019a81c14SSteve Longerbeam struct v4l2_ctrl_handler handler; 201cc196e48SBenoit Parrot struct v4l2_ctrl *pixel_rate; 20219a81c14SSteve Longerbeam struct { 20319a81c14SSteve Longerbeam struct v4l2_ctrl *auto_exp; 20419a81c14SSteve Longerbeam struct v4l2_ctrl *exposure; 20519a81c14SSteve Longerbeam }; 20619a81c14SSteve Longerbeam struct { 20719a81c14SSteve Longerbeam struct v4l2_ctrl *auto_wb; 20819a81c14SSteve Longerbeam struct v4l2_ctrl *blue_balance; 20919a81c14SSteve Longerbeam struct v4l2_ctrl *red_balance; 21019a81c14SSteve Longerbeam }; 21119a81c14SSteve Longerbeam struct { 21219a81c14SSteve Longerbeam struct v4l2_ctrl *auto_gain; 21319a81c14SSteve Longerbeam struct v4l2_ctrl *gain; 21419a81c14SSteve Longerbeam }; 21519a81c14SSteve Longerbeam struct v4l2_ctrl *brightness; 2161068fecaSMylène Josserand struct v4l2_ctrl *light_freq; 21719a81c14SSteve Longerbeam struct v4l2_ctrl *saturation; 21819a81c14SSteve Longerbeam struct v4l2_ctrl *contrast; 21919a81c14SSteve Longerbeam struct v4l2_ctrl *hue; 22019a81c14SSteve Longerbeam struct v4l2_ctrl *test_pattern; 221ce85705aSHugues Fruchet struct v4l2_ctrl *hflip; 222ce85705aSHugues Fruchet struct v4l2_ctrl *vflip; 22319a81c14SSteve Longerbeam }; 22419a81c14SSteve Longerbeam 22519a81c14SSteve Longerbeam struct ov5640_dev { 22619a81c14SSteve Longerbeam struct i2c_client *i2c_client; 22719a81c14SSteve Longerbeam struct v4l2_subdev sd; 22819a81c14SSteve Longerbeam struct media_pad pad; 22919a81c14SSteve Longerbeam struct v4l2_fwnode_endpoint ep; /* the parsed DT endpoint info */ 23019a81c14SSteve Longerbeam struct clk *xclk; /* system clock to OV5640 */ 23119a81c14SSteve Longerbeam u32 xclk_freq; 23219a81c14SSteve Longerbeam 23319a81c14SSteve Longerbeam struct regulator_bulk_data supplies[OV5640_NUM_SUPPLIES]; 23419a81c14SSteve Longerbeam struct gpio_desc *reset_gpio; 23519a81c14SSteve Longerbeam struct gpio_desc *pwdn_gpio; 236c3f3ba3eSHugues Fruchet bool upside_down; 23719a81c14SSteve Longerbeam 23819a81c14SSteve Longerbeam /* lock to protect all members below */ 23919a81c14SSteve Longerbeam struct mutex lock; 24019a81c14SSteve Longerbeam 24119a81c14SSteve Longerbeam int power_count; 24219a81c14SSteve Longerbeam 24319a81c14SSteve Longerbeam struct v4l2_mbus_framefmt fmt; 244fb98e29fSHugues Fruchet bool pending_fmt_change; 24519a81c14SSteve Longerbeam 24619a81c14SSteve Longerbeam const struct ov5640_mode_info *current_mode; 247985cdcb0SHugues Fruchet const struct ov5640_mode_info *last_mode; 24819a81c14SSteve Longerbeam enum ov5640_frame_rate current_fr; 24919a81c14SSteve Longerbeam struct v4l2_fract frame_interval; 25019a81c14SSteve Longerbeam 25119a81c14SSteve Longerbeam struct ov5640_ctrls ctrls; 25219a81c14SSteve Longerbeam 25319a81c14SSteve Longerbeam u32 prev_sysclk, prev_hts; 25419a81c14SSteve Longerbeam u32 ae_low, ae_high, ae_target; 25519a81c14SSteve Longerbeam 25619a81c14SSteve Longerbeam bool pending_mode_change; 25719a81c14SSteve Longerbeam bool streaming; 25819a81c14SSteve Longerbeam }; 25919a81c14SSteve Longerbeam 26019a81c14SSteve Longerbeam static inline struct ov5640_dev *to_ov5640_dev(struct v4l2_subdev *sd) 26119a81c14SSteve Longerbeam { 26219a81c14SSteve Longerbeam return container_of(sd, struct ov5640_dev, sd); 26319a81c14SSteve Longerbeam } 26419a81c14SSteve Longerbeam 26519a81c14SSteve Longerbeam static inline struct v4l2_subdev *ctrl_to_sd(struct v4l2_ctrl *ctrl) 26619a81c14SSteve Longerbeam { 26719a81c14SSteve Longerbeam return &container_of(ctrl->handler, struct ov5640_dev, 26819a81c14SSteve Longerbeam ctrls.handler)->sd; 26919a81c14SSteve Longerbeam } 27019a81c14SSteve Longerbeam 27119a81c14SSteve Longerbeam /* 27219a81c14SSteve Longerbeam * FIXME: all of these register tables are likely filled with 27319a81c14SSteve Longerbeam * entries that set the register to their power-on default values, 27419a81c14SSteve Longerbeam * and which are otherwise not touched by this driver. Those entries 27519a81c14SSteve Longerbeam * should be identified and removed to speed register load time 27619a81c14SSteve Longerbeam * over i2c. 27719a81c14SSteve Longerbeam */ 278fb98e29fSHugues Fruchet /* YUV422 UYVY VGA@30fps */ 27919a81c14SSteve Longerbeam static const struct reg_value ov5640_init_setting_30fps_VGA[] = { 28019a81c14SSteve Longerbeam {0x3103, 0x11, 0, 0}, {0x3008, 0x82, 0, 5}, {0x3008, 0x42, 0, 0}, 281576f5d4bSLad Prabhakar {0x3103, 0x03, 0, 0}, {0x3630, 0x36, 0, 0}, 28219a81c14SSteve Longerbeam {0x3631, 0x0e, 0, 0}, {0x3632, 0xe2, 0, 0}, {0x3633, 0x12, 0, 0}, 28319a81c14SSteve Longerbeam {0x3621, 0xe0, 0, 0}, {0x3704, 0xa0, 0, 0}, {0x3703, 0x5a, 0, 0}, 28419a81c14SSteve Longerbeam {0x3715, 0x78, 0, 0}, {0x3717, 0x01, 0, 0}, {0x370b, 0x60, 0, 0}, 28519a81c14SSteve Longerbeam {0x3705, 0x1a, 0, 0}, {0x3905, 0x02, 0, 0}, {0x3906, 0x10, 0, 0}, 28619a81c14SSteve Longerbeam {0x3901, 0x0a, 0, 0}, {0x3731, 0x12, 0, 0}, {0x3600, 0x08, 0, 0}, 28719a81c14SSteve Longerbeam {0x3601, 0x33, 0, 0}, {0x302d, 0x60, 0, 0}, {0x3620, 0x52, 0, 0}, 28819a81c14SSteve Longerbeam {0x371b, 0x20, 0, 0}, {0x471c, 0x50, 0, 0}, {0x3a13, 0x43, 0, 0}, 28919a81c14SSteve Longerbeam {0x3a18, 0x00, 0, 0}, {0x3a19, 0xf8, 0, 0}, {0x3635, 0x13, 0, 0}, 29019a81c14SSteve Longerbeam {0x3636, 0x03, 0, 0}, {0x3634, 0x40, 0, 0}, {0x3622, 0x01, 0, 0}, 29119a81c14SSteve Longerbeam {0x3c01, 0xa4, 0, 0}, {0x3c04, 0x28, 0, 0}, {0x3c05, 0x98, 0, 0}, 29219a81c14SSteve Longerbeam {0x3c06, 0x00, 0, 0}, {0x3c07, 0x08, 0, 0}, {0x3c08, 0x00, 0, 0}, 29319a81c14SSteve Longerbeam {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0}, 29419a81c14SSteve Longerbeam {0x3820, 0x41, 0, 0}, {0x3821, 0x07, 0, 0}, {0x3814, 0x31, 0, 0}, 29519a81c14SSteve Longerbeam {0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0}, 29619a81c14SSteve Longerbeam {0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0}, 29719a81c14SSteve Longerbeam {0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0}, 298476dec01SMaxime Ripard {0x3810, 0x00, 0, 0}, 29919a81c14SSteve Longerbeam {0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0}, 30019a81c14SSteve Longerbeam {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0}, 30119a81c14SSteve Longerbeam {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0}, 30219a81c14SSteve Longerbeam {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0}, 30319a81c14SSteve Longerbeam {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0}, 30419a81c14SSteve Longerbeam {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0}, 30519a81c14SSteve Longerbeam {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x3000, 0x00, 0, 0}, 30619a81c14SSteve Longerbeam {0x3002, 0x1c, 0, 0}, {0x3004, 0xff, 0, 0}, {0x3006, 0xc3, 0, 0}, 307aa4bb8b8SJacopo Mondi {0x302e, 0x08, 0, 0}, {0x4300, 0x3f, 0, 0}, 3082b5c18f9SChen-Yu Tsai {0x501f, 0x00, 0, 0}, {0x4407, 0x04, 0, 0}, 30919a81c14SSteve Longerbeam {0x440e, 0x00, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0}, 310aa4bb8b8SJacopo Mondi {0x4837, 0x0a, 0, 0}, {0x3824, 0x02, 0, 0}, 31119a81c14SSteve Longerbeam {0x5000, 0xa7, 0, 0}, {0x5001, 0xa3, 0, 0}, {0x5180, 0xff, 0, 0}, 31219a81c14SSteve Longerbeam {0x5181, 0xf2, 0, 0}, {0x5182, 0x00, 0, 0}, {0x5183, 0x14, 0, 0}, 31319a81c14SSteve Longerbeam {0x5184, 0x25, 0, 0}, {0x5185, 0x24, 0, 0}, {0x5186, 0x09, 0, 0}, 31419a81c14SSteve Longerbeam {0x5187, 0x09, 0, 0}, {0x5188, 0x09, 0, 0}, {0x5189, 0x88, 0, 0}, 31519a81c14SSteve Longerbeam {0x518a, 0x54, 0, 0}, {0x518b, 0xee, 0, 0}, {0x518c, 0xb2, 0, 0}, 31619a81c14SSteve Longerbeam {0x518d, 0x50, 0, 0}, {0x518e, 0x34, 0, 0}, {0x518f, 0x6b, 0, 0}, 31719a81c14SSteve Longerbeam {0x5190, 0x46, 0, 0}, {0x5191, 0xf8, 0, 0}, {0x5192, 0x04, 0, 0}, 31819a81c14SSteve Longerbeam {0x5193, 0x70, 0, 0}, {0x5194, 0xf0, 0, 0}, {0x5195, 0xf0, 0, 0}, 31919a81c14SSteve Longerbeam {0x5196, 0x03, 0, 0}, {0x5197, 0x01, 0, 0}, {0x5198, 0x04, 0, 0}, 32019a81c14SSteve Longerbeam {0x5199, 0x6c, 0, 0}, {0x519a, 0x04, 0, 0}, {0x519b, 0x00, 0, 0}, 32119a81c14SSteve Longerbeam {0x519c, 0x09, 0, 0}, {0x519d, 0x2b, 0, 0}, {0x519e, 0x38, 0, 0}, 32219a81c14SSteve Longerbeam {0x5381, 0x1e, 0, 0}, {0x5382, 0x5b, 0, 0}, {0x5383, 0x08, 0, 0}, 32319a81c14SSteve Longerbeam {0x5384, 0x0a, 0, 0}, {0x5385, 0x7e, 0, 0}, {0x5386, 0x88, 0, 0}, 32419a81c14SSteve Longerbeam {0x5387, 0x7c, 0, 0}, {0x5388, 0x6c, 0, 0}, {0x5389, 0x10, 0, 0}, 32519a81c14SSteve Longerbeam {0x538a, 0x01, 0, 0}, {0x538b, 0x98, 0, 0}, {0x5300, 0x08, 0, 0}, 32619a81c14SSteve Longerbeam {0x5301, 0x30, 0, 0}, {0x5302, 0x10, 0, 0}, {0x5303, 0x00, 0, 0}, 32719a81c14SSteve Longerbeam {0x5304, 0x08, 0, 0}, {0x5305, 0x30, 0, 0}, {0x5306, 0x08, 0, 0}, 32819a81c14SSteve Longerbeam {0x5307, 0x16, 0, 0}, {0x5309, 0x08, 0, 0}, {0x530a, 0x30, 0, 0}, 32919a81c14SSteve Longerbeam {0x530b, 0x04, 0, 0}, {0x530c, 0x06, 0, 0}, {0x5480, 0x01, 0, 0}, 33019a81c14SSteve Longerbeam {0x5481, 0x08, 0, 0}, {0x5482, 0x14, 0, 0}, {0x5483, 0x28, 0, 0}, 33119a81c14SSteve Longerbeam {0x5484, 0x51, 0, 0}, {0x5485, 0x65, 0, 0}, {0x5486, 0x71, 0, 0}, 33219a81c14SSteve Longerbeam {0x5487, 0x7d, 0, 0}, {0x5488, 0x87, 0, 0}, {0x5489, 0x91, 0, 0}, 33319a81c14SSteve Longerbeam {0x548a, 0x9a, 0, 0}, {0x548b, 0xaa, 0, 0}, {0x548c, 0xb8, 0, 0}, 33419a81c14SSteve Longerbeam {0x548d, 0xcd, 0, 0}, {0x548e, 0xdd, 0, 0}, {0x548f, 0xea, 0, 0}, 33519a81c14SSteve Longerbeam {0x5490, 0x1d, 0, 0}, {0x5580, 0x02, 0, 0}, {0x5583, 0x40, 0, 0}, 33619a81c14SSteve Longerbeam {0x5584, 0x10, 0, 0}, {0x5589, 0x10, 0, 0}, {0x558a, 0x00, 0, 0}, 33719a81c14SSteve Longerbeam {0x558b, 0xf8, 0, 0}, {0x5800, 0x23, 0, 0}, {0x5801, 0x14, 0, 0}, 33819a81c14SSteve Longerbeam {0x5802, 0x0f, 0, 0}, {0x5803, 0x0f, 0, 0}, {0x5804, 0x12, 0, 0}, 33919a81c14SSteve Longerbeam {0x5805, 0x26, 0, 0}, {0x5806, 0x0c, 0, 0}, {0x5807, 0x08, 0, 0}, 34019a81c14SSteve Longerbeam {0x5808, 0x05, 0, 0}, {0x5809, 0x05, 0, 0}, {0x580a, 0x08, 0, 0}, 34119a81c14SSteve Longerbeam {0x580b, 0x0d, 0, 0}, {0x580c, 0x08, 0, 0}, {0x580d, 0x03, 0, 0}, 34219a81c14SSteve Longerbeam {0x580e, 0x00, 0, 0}, {0x580f, 0x00, 0, 0}, {0x5810, 0x03, 0, 0}, 34319a81c14SSteve Longerbeam {0x5811, 0x09, 0, 0}, {0x5812, 0x07, 0, 0}, {0x5813, 0x03, 0, 0}, 34419a81c14SSteve Longerbeam {0x5814, 0x00, 0, 0}, {0x5815, 0x01, 0, 0}, {0x5816, 0x03, 0, 0}, 34519a81c14SSteve Longerbeam {0x5817, 0x08, 0, 0}, {0x5818, 0x0d, 0, 0}, {0x5819, 0x08, 0, 0}, 34619a81c14SSteve Longerbeam {0x581a, 0x05, 0, 0}, {0x581b, 0x06, 0, 0}, {0x581c, 0x08, 0, 0}, 34719a81c14SSteve Longerbeam {0x581d, 0x0e, 0, 0}, {0x581e, 0x29, 0, 0}, {0x581f, 0x17, 0, 0}, 34819a81c14SSteve Longerbeam {0x5820, 0x11, 0, 0}, {0x5821, 0x11, 0, 0}, {0x5822, 0x15, 0, 0}, 34919a81c14SSteve Longerbeam {0x5823, 0x28, 0, 0}, {0x5824, 0x46, 0, 0}, {0x5825, 0x26, 0, 0}, 35019a81c14SSteve Longerbeam {0x5826, 0x08, 0, 0}, {0x5827, 0x26, 0, 0}, {0x5828, 0x64, 0, 0}, 35119a81c14SSteve Longerbeam {0x5829, 0x26, 0, 0}, {0x582a, 0x24, 0, 0}, {0x582b, 0x22, 0, 0}, 35219a81c14SSteve Longerbeam {0x582c, 0x24, 0, 0}, {0x582d, 0x24, 0, 0}, {0x582e, 0x06, 0, 0}, 35319a81c14SSteve Longerbeam {0x582f, 0x22, 0, 0}, {0x5830, 0x40, 0, 0}, {0x5831, 0x42, 0, 0}, 35419a81c14SSteve Longerbeam {0x5832, 0x24, 0, 0}, {0x5833, 0x26, 0, 0}, {0x5834, 0x24, 0, 0}, 35519a81c14SSteve Longerbeam {0x5835, 0x22, 0, 0}, {0x5836, 0x22, 0, 0}, {0x5837, 0x26, 0, 0}, 35619a81c14SSteve Longerbeam {0x5838, 0x44, 0, 0}, {0x5839, 0x24, 0, 0}, {0x583a, 0x26, 0, 0}, 35719a81c14SSteve Longerbeam {0x583b, 0x28, 0, 0}, {0x583c, 0x42, 0, 0}, {0x583d, 0xce, 0, 0}, 35819a81c14SSteve Longerbeam {0x5025, 0x00, 0, 0}, {0x3a0f, 0x30, 0, 0}, {0x3a10, 0x28, 0, 0}, 35919a81c14SSteve Longerbeam {0x3a1b, 0x30, 0, 0}, {0x3a1e, 0x26, 0, 0}, {0x3a11, 0x60, 0, 0}, 36019a81c14SSteve Longerbeam {0x3a1f, 0x14, 0, 0}, {0x3008, 0x02, 0, 0}, {0x3c00, 0x04, 0, 300}, 36119a81c14SSteve Longerbeam }; 36219a81c14SSteve Longerbeam 363086c25f8SMaxime Ripard static const struct reg_value ov5640_setting_VGA_640_480[] = { 364c14d107eSMaxime Ripard {0x3c07, 0x08, 0, 0}, 36519a81c14SSteve Longerbeam {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0}, 366ce85705aSHugues Fruchet {0x3814, 0x31, 0, 0}, 36719a81c14SSteve Longerbeam {0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0}, 36819a81c14SSteve Longerbeam {0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0}, 36919a81c14SSteve Longerbeam {0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0}, 370476dec01SMaxime Ripard {0x3810, 0x00, 0, 0}, 37119a81c14SSteve Longerbeam {0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0}, 37219a81c14SSteve Longerbeam {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0}, 37319a81c14SSteve Longerbeam {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0}, 37419a81c14SSteve Longerbeam {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0}, 37519a81c14SSteve Longerbeam {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0}, 37619a81c14SSteve Longerbeam {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0}, 3772b5c18f9SChen-Yu Tsai {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, 37819a81c14SSteve Longerbeam {0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0}, 37919a81c14SSteve Longerbeam {0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0}, 38019a81c14SSteve Longerbeam }; 38119a81c14SSteve Longerbeam 382086c25f8SMaxime Ripard static const struct reg_value ov5640_setting_XGA_1024_768[] = { 383c14d107eSMaxime Ripard {0x3c07, 0x08, 0, 0}, 38419a81c14SSteve Longerbeam {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0}, 385ce85705aSHugues Fruchet {0x3814, 0x31, 0, 0}, 38619a81c14SSteve Longerbeam {0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0}, 38719a81c14SSteve Longerbeam {0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0}, 38819a81c14SSteve Longerbeam {0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0}, 389476dec01SMaxime Ripard {0x3810, 0x00, 0, 0}, 39019a81c14SSteve Longerbeam {0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0}, 39119a81c14SSteve Longerbeam {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0}, 39219a81c14SSteve Longerbeam {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0}, 39319a81c14SSteve Longerbeam {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0}, 39419a81c14SSteve Longerbeam {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0}, 39519a81c14SSteve Longerbeam {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0}, 3962b5c18f9SChen-Yu Tsai {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, 39719a81c14SSteve Longerbeam {0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0}, 39886633417SMaxime Ripard {0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0}, 39919a81c14SSteve Longerbeam }; 40019a81c14SSteve Longerbeam 401086c25f8SMaxime Ripard static const struct reg_value ov5640_setting_QVGA_320_240[] = { 402c14d107eSMaxime Ripard {0x3c07, 0x08, 0, 0}, 40319a81c14SSteve Longerbeam {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0}, 404ce85705aSHugues Fruchet {0x3814, 0x31, 0, 0}, 40519a81c14SSteve Longerbeam {0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0}, 40619a81c14SSteve Longerbeam {0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0}, 40719a81c14SSteve Longerbeam {0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0}, 408476dec01SMaxime Ripard {0x3810, 0x00, 0, 0}, 40919a81c14SSteve Longerbeam {0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0}, 41019a81c14SSteve Longerbeam {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0}, 41119a81c14SSteve Longerbeam {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0}, 41219a81c14SSteve Longerbeam {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0}, 41319a81c14SSteve Longerbeam {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0}, 41419a81c14SSteve Longerbeam {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0}, 4152b5c18f9SChen-Yu Tsai {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, 41619a81c14SSteve Longerbeam {0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0}, 41719a81c14SSteve Longerbeam {0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0}, 41819a81c14SSteve Longerbeam }; 41919a81c14SSteve Longerbeam 42032ea5e05SHugues Fruchet static const struct reg_value ov5640_setting_QQVGA_160_120[] = { 42132ea5e05SHugues Fruchet {0x3c07, 0x08, 0, 0}, 42232ea5e05SHugues Fruchet {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0}, 42332ea5e05SHugues Fruchet {0x3814, 0x31, 0, 0}, 42432ea5e05SHugues Fruchet {0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0}, 42532ea5e05SHugues Fruchet {0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0}, 42632ea5e05SHugues Fruchet {0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0}, 42732ea5e05SHugues Fruchet {0x3810, 0x00, 0, 0}, 42832ea5e05SHugues Fruchet {0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0}, 42932ea5e05SHugues Fruchet {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0}, 43032ea5e05SHugues Fruchet {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0}, 43132ea5e05SHugues Fruchet {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0}, 43232ea5e05SHugues Fruchet {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0}, 43332ea5e05SHugues Fruchet {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0}, 43432ea5e05SHugues Fruchet {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, 43532ea5e05SHugues Fruchet {0x4407, 0x04, 0, 0}, {0x5001, 0xa3, 0, 0}, 43632ea5e05SHugues Fruchet }; 43732ea5e05SHugues Fruchet 438086c25f8SMaxime Ripard static const struct reg_value ov5640_setting_QCIF_176_144[] = { 439c14d107eSMaxime Ripard {0x3c07, 0x08, 0, 0}, 44019a81c14SSteve Longerbeam {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0}, 441ce85705aSHugues Fruchet {0x3814, 0x31, 0, 0}, 44219a81c14SSteve Longerbeam {0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0}, 44319a81c14SSteve Longerbeam {0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0}, 44419a81c14SSteve Longerbeam {0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0}, 445476dec01SMaxime Ripard {0x3810, 0x00, 0, 0}, 44619a81c14SSteve Longerbeam {0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0}, 44719a81c14SSteve Longerbeam {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0}, 44819a81c14SSteve Longerbeam {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0}, 44919a81c14SSteve Longerbeam {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0}, 45019a81c14SSteve Longerbeam {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0}, 45119a81c14SSteve Longerbeam {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0}, 4522b5c18f9SChen-Yu Tsai {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, 45319a81c14SSteve Longerbeam {0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0}, 45419a81c14SSteve Longerbeam {0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0}, 45519a81c14SSteve Longerbeam }; 45619a81c14SSteve Longerbeam 457086c25f8SMaxime Ripard static const struct reg_value ov5640_setting_NTSC_720_480[] = { 458c14d107eSMaxime Ripard {0x3c07, 0x08, 0, 0}, 45919a81c14SSteve Longerbeam {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0}, 460ce85705aSHugues Fruchet {0x3814, 0x31, 0, 0}, 46119a81c14SSteve Longerbeam {0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0}, 46219a81c14SSteve Longerbeam {0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0}, 46319a81c14SSteve Longerbeam {0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0}, 464476dec01SMaxime Ripard {0x3810, 0x00, 0, 0}, 46519a81c14SSteve Longerbeam {0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x3c, 0, 0}, 46619a81c14SSteve Longerbeam {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0}, 46719a81c14SSteve Longerbeam {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0}, 46819a81c14SSteve Longerbeam {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0}, 46919a81c14SSteve Longerbeam {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0}, 47019a81c14SSteve Longerbeam {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0}, 4712b5c18f9SChen-Yu Tsai {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, 47219a81c14SSteve Longerbeam {0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0}, 47319a81c14SSteve Longerbeam {0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0}, 47419a81c14SSteve Longerbeam }; 47519a81c14SSteve Longerbeam 476086c25f8SMaxime Ripard static const struct reg_value ov5640_setting_PAL_720_576[] = { 477c14d107eSMaxime Ripard {0x3c07, 0x08, 0, 0}, 47819a81c14SSteve Longerbeam {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0}, 479ce85705aSHugues Fruchet {0x3814, 0x31, 0, 0}, 48019a81c14SSteve Longerbeam {0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0}, 48119a81c14SSteve Longerbeam {0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0}, 48219a81c14SSteve Longerbeam {0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0}, 483476dec01SMaxime Ripard {0x3810, 0x00, 0, 0}, 48419a81c14SSteve Longerbeam {0x3811, 0x38, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0}, 48519a81c14SSteve Longerbeam {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0}, 48619a81c14SSteve Longerbeam {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0}, 48719a81c14SSteve Longerbeam {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0}, 48819a81c14SSteve Longerbeam {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0}, 48919a81c14SSteve Longerbeam {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0}, 4902b5c18f9SChen-Yu Tsai {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, 49119a81c14SSteve Longerbeam {0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0}, 49219a81c14SSteve Longerbeam {0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0}, 49319a81c14SSteve Longerbeam }; 49419a81c14SSteve Longerbeam 495086c25f8SMaxime Ripard static const struct reg_value ov5640_setting_720P_1280_720[] = { 496c14d107eSMaxime Ripard {0x3c07, 0x07, 0, 0}, 49719a81c14SSteve Longerbeam {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0}, 498ce85705aSHugues Fruchet {0x3814, 0x31, 0, 0}, 49919a81c14SSteve Longerbeam {0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0}, 50019a81c14SSteve Longerbeam {0x3802, 0x00, 0, 0}, {0x3803, 0xfa, 0, 0}, {0x3804, 0x0a, 0, 0}, 50119a81c14SSteve Longerbeam {0x3805, 0x3f, 0, 0}, {0x3806, 0x06, 0, 0}, {0x3807, 0xa9, 0, 0}, 502476dec01SMaxime Ripard {0x3810, 0x00, 0, 0}, 50319a81c14SSteve Longerbeam {0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x04, 0, 0}, 50419a81c14SSteve Longerbeam {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0}, 50519a81c14SSteve Longerbeam {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x02, 0, 0}, 50619a81c14SSteve Longerbeam {0x3a03, 0xe4, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0xbc, 0, 0}, 50719a81c14SSteve Longerbeam {0x3a0a, 0x01, 0, 0}, {0x3a0b, 0x72, 0, 0}, {0x3a0e, 0x01, 0, 0}, 50819a81c14SSteve Longerbeam {0x3a0d, 0x02, 0, 0}, {0x3a14, 0x02, 0, 0}, {0x3a15, 0xe4, 0, 0}, 5092b5c18f9SChen-Yu Tsai {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, 51019a81c14SSteve Longerbeam {0x4407, 0x04, 0, 0}, {0x460b, 0x37, 0, 0}, {0x460c, 0x20, 0, 0}, 51119a81c14SSteve Longerbeam {0x3824, 0x04, 0, 0}, {0x5001, 0x83, 0, 0}, 51219a81c14SSteve Longerbeam }; 51319a81c14SSteve Longerbeam 514086c25f8SMaxime Ripard static const struct reg_value ov5640_setting_1080P_1920_1080[] = { 515c14d107eSMaxime Ripard {0x3c07, 0x08, 0, 0}, 51619a81c14SSteve Longerbeam {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0}, 517ce85705aSHugues Fruchet {0x3814, 0x11, 0, 0}, 51819a81c14SSteve Longerbeam {0x3815, 0x11, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0}, 51919a81c14SSteve Longerbeam {0x3802, 0x00, 0, 0}, {0x3803, 0x00, 0, 0}, {0x3804, 0x0a, 0, 0}, 52019a81c14SSteve Longerbeam {0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9f, 0, 0}, 521476dec01SMaxime Ripard {0x3810, 0x00, 0, 0}, 52219a81c14SSteve Longerbeam {0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x04, 0, 0}, 52319a81c14SSteve Longerbeam {0x3618, 0x04, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x21, 0, 0}, 52419a81c14SSteve Longerbeam {0x3709, 0x12, 0, 0}, {0x370c, 0x00, 0, 0}, {0x3a02, 0x03, 0, 0}, 52519a81c14SSteve Longerbeam {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0}, 52619a81c14SSteve Longerbeam {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0}, 52719a81c14SSteve Longerbeam {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0}, 5282b5c18f9SChen-Yu Tsai {0x4001, 0x02, 0, 0}, {0x4004, 0x06, 0, 0}, 52919a81c14SSteve Longerbeam {0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0}, 530c14d107eSMaxime Ripard {0x3824, 0x02, 0, 0}, {0x5001, 0x83, 0, 0}, 531c14d107eSMaxime Ripard {0x3c07, 0x07, 0, 0}, {0x3c08, 0x00, 0, 0}, 53219a81c14SSteve Longerbeam {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0}, 53319a81c14SSteve Longerbeam {0x3800, 0x01, 0, 0}, {0x3801, 0x50, 0, 0}, {0x3802, 0x01, 0, 0}, 53419a81c14SSteve Longerbeam {0x3803, 0xb2, 0, 0}, {0x3804, 0x08, 0, 0}, {0x3805, 0xef, 0, 0}, 53586633417SMaxime Ripard {0x3806, 0x05, 0, 0}, {0x3807, 0xf1, 0, 0}, 536476dec01SMaxime Ripard {0x3612, 0x2b, 0, 0}, {0x3708, 0x64, 0, 0}, 53719a81c14SSteve Longerbeam {0x3a02, 0x04, 0, 0}, {0x3a03, 0x60, 0, 0}, {0x3a08, 0x01, 0, 0}, 53819a81c14SSteve Longerbeam {0x3a09, 0x50, 0, 0}, {0x3a0a, 0x01, 0, 0}, {0x3a0b, 0x18, 0, 0}, 53919a81c14SSteve Longerbeam {0x3a0e, 0x03, 0, 0}, {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x04, 0, 0}, 5402b5c18f9SChen-Yu Tsai {0x3a15, 0x60, 0, 0}, {0x4407, 0x04, 0, 0}, 54119a81c14SSteve Longerbeam {0x460b, 0x37, 0, 0}, {0x460c, 0x20, 0, 0}, {0x3824, 0x04, 0, 0}, 54292b9096cSBenoit Parrot {0x4005, 0x1a, 0, 0}, 54319a81c14SSteve Longerbeam }; 54419a81c14SSteve Longerbeam 545086c25f8SMaxime Ripard static const struct reg_value ov5640_setting_QSXGA_2592_1944[] = { 546c14d107eSMaxime Ripard {0x3c07, 0x08, 0, 0}, 54719a81c14SSteve Longerbeam {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0}, 548ce85705aSHugues Fruchet {0x3814, 0x11, 0, 0}, 54919a81c14SSteve Longerbeam {0x3815, 0x11, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0}, 55019a81c14SSteve Longerbeam {0x3802, 0x00, 0, 0}, {0x3803, 0x00, 0, 0}, {0x3804, 0x0a, 0, 0}, 55119a81c14SSteve Longerbeam {0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9f, 0, 0}, 552476dec01SMaxime Ripard {0x3810, 0x00, 0, 0}, 55319a81c14SSteve Longerbeam {0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x04, 0, 0}, 55419a81c14SSteve Longerbeam {0x3618, 0x04, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x21, 0, 0}, 55519a81c14SSteve Longerbeam {0x3709, 0x12, 0, 0}, {0x370c, 0x00, 0, 0}, {0x3a02, 0x03, 0, 0}, 55619a81c14SSteve Longerbeam {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0}, 55719a81c14SSteve Longerbeam {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0}, 55819a81c14SSteve Longerbeam {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0}, 5592b5c18f9SChen-Yu Tsai {0x4001, 0x02, 0, 0}, {0x4004, 0x06, 0, 0}, 56019a81c14SSteve Longerbeam {0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0}, 56119a81c14SSteve Longerbeam {0x3824, 0x02, 0, 0}, {0x5001, 0x83, 0, 70}, 56219a81c14SSteve Longerbeam }; 56319a81c14SSteve Longerbeam 56419a81c14SSteve Longerbeam /* power-on sensor init reg table */ 56519a81c14SSteve Longerbeam static const struct ov5640_mode_info ov5640_mode_init_data = { 566476dec01SMaxime Ripard 0, SUBSAMPLING, 640, 1896, 480, 984, 567476dec01SMaxime Ripard ov5640_init_setting_30fps_VGA, 56819a81c14SSteve Longerbeam ARRAY_SIZE(ov5640_init_setting_30fps_VGA), 5695554c80eSAdam Ford OV5640_30_FPS, 57019a81c14SSteve Longerbeam }; 57119a81c14SSteve Longerbeam 57219a81c14SSteve Longerbeam static const struct ov5640_mode_info 573086c25f8SMaxime Ripard ov5640_mode_data[OV5640_NUM_MODES] = { 57432ea5e05SHugues Fruchet {OV5640_MODE_QQVGA_160_120, SUBSAMPLING, 57532ea5e05SHugues Fruchet 160, 1896, 120, 984, 57632ea5e05SHugues Fruchet ov5640_setting_QQVGA_160_120, 57732ea5e05SHugues Fruchet ARRAY_SIZE(ov5640_setting_QQVGA_160_120), 57832ea5e05SHugues Fruchet OV5640_30_FPS}, 579476dec01SMaxime Ripard {OV5640_MODE_QCIF_176_144, SUBSAMPLING, 580476dec01SMaxime Ripard 176, 1896, 144, 984, 581086c25f8SMaxime Ripard ov5640_setting_QCIF_176_144, 5825554c80eSAdam Ford ARRAY_SIZE(ov5640_setting_QCIF_176_144), 5835554c80eSAdam Ford OV5640_30_FPS}, 584476dec01SMaxime Ripard {OV5640_MODE_QVGA_320_240, SUBSAMPLING, 585476dec01SMaxime Ripard 320, 1896, 240, 984, 586086c25f8SMaxime Ripard ov5640_setting_QVGA_320_240, 5875554c80eSAdam Ford ARRAY_SIZE(ov5640_setting_QVGA_320_240), 5885554c80eSAdam Ford OV5640_30_FPS}, 589476dec01SMaxime Ripard {OV5640_MODE_VGA_640_480, SUBSAMPLING, 590476dec01SMaxime Ripard 640, 1896, 480, 1080, 591086c25f8SMaxime Ripard ov5640_setting_VGA_640_480, 5925554c80eSAdam Ford ARRAY_SIZE(ov5640_setting_VGA_640_480), 5935554c80eSAdam Ford OV5640_60_FPS}, 594476dec01SMaxime Ripard {OV5640_MODE_NTSC_720_480, SUBSAMPLING, 595476dec01SMaxime Ripard 720, 1896, 480, 984, 596086c25f8SMaxime Ripard ov5640_setting_NTSC_720_480, 5975554c80eSAdam Ford ARRAY_SIZE(ov5640_setting_NTSC_720_480), 5985554c80eSAdam Ford OV5640_30_FPS}, 599476dec01SMaxime Ripard {OV5640_MODE_PAL_720_576, SUBSAMPLING, 600476dec01SMaxime Ripard 720, 1896, 576, 984, 601086c25f8SMaxime Ripard ov5640_setting_PAL_720_576, 6025554c80eSAdam Ford ARRAY_SIZE(ov5640_setting_PAL_720_576), 6035554c80eSAdam Ford OV5640_30_FPS}, 604476dec01SMaxime Ripard {OV5640_MODE_XGA_1024_768, SUBSAMPLING, 605476dec01SMaxime Ripard 1024, 1896, 768, 1080, 606086c25f8SMaxime Ripard ov5640_setting_XGA_1024_768, 6075554c80eSAdam Ford ARRAY_SIZE(ov5640_setting_XGA_1024_768), 6085554c80eSAdam Ford OV5640_30_FPS}, 609476dec01SMaxime Ripard {OV5640_MODE_720P_1280_720, SUBSAMPLING, 610476dec01SMaxime Ripard 1280, 1892, 720, 740, 611086c25f8SMaxime Ripard ov5640_setting_720P_1280_720, 6125554c80eSAdam Ford ARRAY_SIZE(ov5640_setting_720P_1280_720), 6135554c80eSAdam Ford OV5640_30_FPS}, 614476dec01SMaxime Ripard {OV5640_MODE_1080P_1920_1080, SCALING, 615476dec01SMaxime Ripard 1920, 2500, 1080, 1120, 616086c25f8SMaxime Ripard ov5640_setting_1080P_1920_1080, 6175554c80eSAdam Ford ARRAY_SIZE(ov5640_setting_1080P_1920_1080), 6185554c80eSAdam Ford OV5640_30_FPS}, 619476dec01SMaxime Ripard {OV5640_MODE_QSXGA_2592_1944, SCALING, 620476dec01SMaxime Ripard 2592, 2844, 1944, 1968, 621086c25f8SMaxime Ripard ov5640_setting_QSXGA_2592_1944, 6225554c80eSAdam Ford ARRAY_SIZE(ov5640_setting_QSXGA_2592_1944), 6235554c80eSAdam Ford OV5640_15_FPS}, 62419a81c14SSteve Longerbeam }; 62519a81c14SSteve Longerbeam 62619a81c14SSteve Longerbeam static int ov5640_init_slave_id(struct ov5640_dev *sensor) 62719a81c14SSteve Longerbeam { 62819a81c14SSteve Longerbeam struct i2c_client *client = sensor->i2c_client; 62919a81c14SSteve Longerbeam struct i2c_msg msg; 63019a81c14SSteve Longerbeam u8 buf[3]; 63119a81c14SSteve Longerbeam int ret; 63219a81c14SSteve Longerbeam 63319a81c14SSteve Longerbeam if (client->addr == OV5640_DEFAULT_SLAVE_ID) 63419a81c14SSteve Longerbeam return 0; 63519a81c14SSteve Longerbeam 63619a81c14SSteve Longerbeam buf[0] = OV5640_REG_SLAVE_ID >> 8; 63719a81c14SSteve Longerbeam buf[1] = OV5640_REG_SLAVE_ID & 0xff; 63819a81c14SSteve Longerbeam buf[2] = client->addr << 1; 63919a81c14SSteve Longerbeam 64019a81c14SSteve Longerbeam msg.addr = OV5640_DEFAULT_SLAVE_ID; 64119a81c14SSteve Longerbeam msg.flags = 0; 64219a81c14SSteve Longerbeam msg.buf = buf; 64319a81c14SSteve Longerbeam msg.len = sizeof(buf); 64419a81c14SSteve Longerbeam 64519a81c14SSteve Longerbeam ret = i2c_transfer(client->adapter, &msg, 1); 64619a81c14SSteve Longerbeam if (ret < 0) { 64719a81c14SSteve Longerbeam dev_err(&client->dev, "%s: failed with %d\n", __func__, ret); 64819a81c14SSteve Longerbeam return ret; 64919a81c14SSteve Longerbeam } 65019a81c14SSteve Longerbeam 65119a81c14SSteve Longerbeam return 0; 65219a81c14SSteve Longerbeam } 65319a81c14SSteve Longerbeam 65419a81c14SSteve Longerbeam static int ov5640_write_reg(struct ov5640_dev *sensor, u16 reg, u8 val) 65519a81c14SSteve Longerbeam { 65619a81c14SSteve Longerbeam struct i2c_client *client = sensor->i2c_client; 65719a81c14SSteve Longerbeam struct i2c_msg msg; 65819a81c14SSteve Longerbeam u8 buf[3]; 65919a81c14SSteve Longerbeam int ret; 66019a81c14SSteve Longerbeam 66119a81c14SSteve Longerbeam buf[0] = reg >> 8; 66219a81c14SSteve Longerbeam buf[1] = reg & 0xff; 66319a81c14SSteve Longerbeam buf[2] = val; 66419a81c14SSteve Longerbeam 66519a81c14SSteve Longerbeam msg.addr = client->addr; 66619a81c14SSteve Longerbeam msg.flags = client->flags; 66719a81c14SSteve Longerbeam msg.buf = buf; 66819a81c14SSteve Longerbeam msg.len = sizeof(buf); 66919a81c14SSteve Longerbeam 67019a81c14SSteve Longerbeam ret = i2c_transfer(client->adapter, &msg, 1); 67119a81c14SSteve Longerbeam if (ret < 0) { 6723924c623SHugues Fruchet dev_err(&client->dev, "%s: error: reg=%x, val=%x\n", 67319a81c14SSteve Longerbeam __func__, reg, val); 67419a81c14SSteve Longerbeam return ret; 67519a81c14SSteve Longerbeam } 67619a81c14SSteve Longerbeam 67719a81c14SSteve Longerbeam return 0; 67819a81c14SSteve Longerbeam } 67919a81c14SSteve Longerbeam 68019a81c14SSteve Longerbeam static int ov5640_read_reg(struct ov5640_dev *sensor, u16 reg, u8 *val) 68119a81c14SSteve Longerbeam { 68219a81c14SSteve Longerbeam struct i2c_client *client = sensor->i2c_client; 68319a81c14SSteve Longerbeam struct i2c_msg msg[2]; 68419a81c14SSteve Longerbeam u8 buf[2]; 68519a81c14SSteve Longerbeam int ret; 68619a81c14SSteve Longerbeam 68719a81c14SSteve Longerbeam buf[0] = reg >> 8; 68819a81c14SSteve Longerbeam buf[1] = reg & 0xff; 68919a81c14SSteve Longerbeam 69019a81c14SSteve Longerbeam msg[0].addr = client->addr; 69119a81c14SSteve Longerbeam msg[0].flags = client->flags; 69219a81c14SSteve Longerbeam msg[0].buf = buf; 69319a81c14SSteve Longerbeam msg[0].len = sizeof(buf); 69419a81c14SSteve Longerbeam 69519a81c14SSteve Longerbeam msg[1].addr = client->addr; 69619a81c14SSteve Longerbeam msg[1].flags = client->flags | I2C_M_RD; 69719a81c14SSteve Longerbeam msg[1].buf = buf; 69819a81c14SSteve Longerbeam msg[1].len = 1; 69919a81c14SSteve Longerbeam 70019a81c14SSteve Longerbeam ret = i2c_transfer(client->adapter, msg, 2); 7013924c623SHugues Fruchet if (ret < 0) { 7023924c623SHugues Fruchet dev_err(&client->dev, "%s: error: reg=%x\n", 7033924c623SHugues Fruchet __func__, reg); 70419a81c14SSteve Longerbeam return ret; 7053924c623SHugues Fruchet } 70619a81c14SSteve Longerbeam 70719a81c14SSteve Longerbeam *val = buf[0]; 70819a81c14SSteve Longerbeam return 0; 70919a81c14SSteve Longerbeam } 71019a81c14SSteve Longerbeam 71119a81c14SSteve Longerbeam static int ov5640_read_reg16(struct ov5640_dev *sensor, u16 reg, u16 *val) 71219a81c14SSteve Longerbeam { 71319a81c14SSteve Longerbeam u8 hi, lo; 71419a81c14SSteve Longerbeam int ret; 71519a81c14SSteve Longerbeam 71619a81c14SSteve Longerbeam ret = ov5640_read_reg(sensor, reg, &hi); 71719a81c14SSteve Longerbeam if (ret) 71819a81c14SSteve Longerbeam return ret; 71919a81c14SSteve Longerbeam ret = ov5640_read_reg(sensor, reg + 1, &lo); 72019a81c14SSteve Longerbeam if (ret) 72119a81c14SSteve Longerbeam return ret; 72219a81c14SSteve Longerbeam 72319a81c14SSteve Longerbeam *val = ((u16)hi << 8) | (u16)lo; 72419a81c14SSteve Longerbeam return 0; 72519a81c14SSteve Longerbeam } 72619a81c14SSteve Longerbeam 72719a81c14SSteve Longerbeam static int ov5640_write_reg16(struct ov5640_dev *sensor, u16 reg, u16 val) 72819a81c14SSteve Longerbeam { 72919a81c14SSteve Longerbeam int ret; 73019a81c14SSteve Longerbeam 73119a81c14SSteve Longerbeam ret = ov5640_write_reg(sensor, reg, val >> 8); 73219a81c14SSteve Longerbeam if (ret) 73319a81c14SSteve Longerbeam return ret; 73419a81c14SSteve Longerbeam 73519a81c14SSteve Longerbeam return ov5640_write_reg(sensor, reg + 1, val & 0xff); 73619a81c14SSteve Longerbeam } 73719a81c14SSteve Longerbeam 73819a81c14SSteve Longerbeam static int ov5640_mod_reg(struct ov5640_dev *sensor, u16 reg, 73919a81c14SSteve Longerbeam u8 mask, u8 val) 74019a81c14SSteve Longerbeam { 74119a81c14SSteve Longerbeam u8 readval; 74219a81c14SSteve Longerbeam int ret; 74319a81c14SSteve Longerbeam 74419a81c14SSteve Longerbeam ret = ov5640_read_reg(sensor, reg, &readval); 74519a81c14SSteve Longerbeam if (ret) 74619a81c14SSteve Longerbeam return ret; 74719a81c14SSteve Longerbeam 74819a81c14SSteve Longerbeam readval &= ~mask; 74919a81c14SSteve Longerbeam val &= mask; 75019a81c14SSteve Longerbeam val |= readval; 75119a81c14SSteve Longerbeam 75219a81c14SSteve Longerbeam return ov5640_write_reg(sensor, reg, val); 75319a81c14SSteve Longerbeam } 75419a81c14SSteve Longerbeam 755aa288248SMaxime Ripard /* 756aa288248SMaxime Ripard * After trying the various combinations, reading various 757f8a7647dSMauro Carvalho Chehab * documentations spread around the net, and from the various 758aa288248SMaxime Ripard * feedback, the clock tree is probably as follows: 759aa288248SMaxime Ripard * 760aa288248SMaxime Ripard * +--------------+ 761aa288248SMaxime Ripard * | Ext. Clock | 762aa288248SMaxime Ripard * +-+------------+ 763aa288248SMaxime Ripard * | +----------+ 764aa288248SMaxime Ripard * +->| PLL1 | - reg 0x3036, for the multiplier 765aa288248SMaxime Ripard * +-+--------+ - reg 0x3037, bits 0-3 for the pre-divider 766aa288248SMaxime Ripard * | +--------------+ 767aa288248SMaxime Ripard * +->| System Clock | - reg 0x3035, bits 4-7 768aa288248SMaxime Ripard * +-+------------+ 769aa288248SMaxime Ripard * | +--------------+ 770aa288248SMaxime Ripard * +->| MIPI Divider | - reg 0x3035, bits 0-3 771aa288248SMaxime Ripard * | +-+------------+ 772aa288248SMaxime Ripard * | +----------------> MIPI SCLK 773aa288248SMaxime Ripard * | + +-----+ 774aa288248SMaxime Ripard * | +->| / 2 |-------> MIPI BIT CLK 775aa288248SMaxime Ripard * | +-----+ 776aa288248SMaxime Ripard * | +--------------+ 777aa288248SMaxime Ripard * +->| PLL Root Div | - reg 0x3037, bit 4 778aa288248SMaxime Ripard * +-+------------+ 779aa288248SMaxime Ripard * | +---------+ 7804c85f628SPaul Kocialkowski * +->| Bit Div | - reg 0x3034, bits 0-3 781aa288248SMaxime Ripard * +-+-------+ 782aa288248SMaxime Ripard * | +-------------+ 783aa288248SMaxime Ripard * +->| SCLK Div | - reg 0x3108, bits 0-1 784aa288248SMaxime Ripard * | +-+-----------+ 785aa288248SMaxime Ripard * | +---------------> SCLK 786aa288248SMaxime Ripard * | +-------------+ 787aa288248SMaxime Ripard * +->| SCLK 2X Div | - reg 0x3108, bits 2-3 788aa288248SMaxime Ripard * | +-+-----------+ 789aa288248SMaxime Ripard * | +---------------> SCLK 2X 790aa288248SMaxime Ripard * | +-------------+ 791aa288248SMaxime Ripard * +->| PCLK Div | - reg 0x3108, bits 4-5 792aa288248SMaxime Ripard * ++------------+ 793aa288248SMaxime Ripard * + +-----------+ 794aa288248SMaxime Ripard * +->| P_DIV | - reg 0x3035, bits 0-3 795aa288248SMaxime Ripard * +-----+-----+ 796aa288248SMaxime Ripard * +------------> PCLK 797aa288248SMaxime Ripard * 798aa288248SMaxime Ripard * This is deviating from the datasheet at least for the register 799aa288248SMaxime Ripard * 0x3108, since it's said here that the PCLK would be clocked from 800aa288248SMaxime Ripard * the PLL. 801aa288248SMaxime Ripard * 802aa288248SMaxime Ripard * There seems to be also (unverified) constraints: 803aa288248SMaxime Ripard * - the PLL pre-divider output rate should be in the 4-27MHz range 804aa288248SMaxime Ripard * - the PLL multiplier output rate should be in the 500-1000MHz range 805aa288248SMaxime Ripard * - PCLK >= SCLK * 2 in YUV, >= SCLK in Raw or JPEG 806aa288248SMaxime Ripard * 807aa288248SMaxime Ripard * In the two latter cases, these constraints are met since our 808aa288248SMaxime Ripard * factors are hardcoded. If we were to change that, we would need to 809aa288248SMaxime Ripard * take this into account. The only varying parts are the PLL 810aa288248SMaxime Ripard * multiplier and the system clock divider, which are shared between 811aa288248SMaxime Ripard * all these clocks so won't cause any issue. 812aa288248SMaxime Ripard */ 813aa288248SMaxime Ripard 814aa288248SMaxime Ripard /* 815aa288248SMaxime Ripard * This is supposed to be ranging from 1 to 8, but the value is always 816aa288248SMaxime Ripard * set to 3 in the vendor kernels. 817aa288248SMaxime Ripard */ 818aa288248SMaxime Ripard #define OV5640_PLL_PREDIV 3 819aa288248SMaxime Ripard 820aa288248SMaxime Ripard #define OV5640_PLL_MULT_MIN 4 821aa288248SMaxime Ripard #define OV5640_PLL_MULT_MAX 252 822aa288248SMaxime Ripard 823aa288248SMaxime Ripard /* 824aa288248SMaxime Ripard * This is supposed to be ranging from 1 to 16, but the value is 825aa288248SMaxime Ripard * always set to either 1 or 2 in the vendor kernels. 826aa288248SMaxime Ripard */ 827aa288248SMaxime Ripard #define OV5640_SYSDIV_MIN 1 828aa288248SMaxime Ripard #define OV5640_SYSDIV_MAX 16 829aa288248SMaxime Ripard 830aa288248SMaxime Ripard /* 831aa288248SMaxime Ripard * Hardcode these values for scaler and non-scaler modes. 832aa288248SMaxime Ripard * FIXME: to be re-calcualted for 1 data lanes setups 833aa288248SMaxime Ripard */ 834aa288248SMaxime Ripard #define OV5640_MIPI_DIV_PCLK 2 835aa288248SMaxime Ripard #define OV5640_MIPI_DIV_SCLK 1 836aa288248SMaxime Ripard 837aa288248SMaxime Ripard /* 838aa288248SMaxime Ripard * This is supposed to be ranging from 1 to 2, but the value is always 839aa288248SMaxime Ripard * set to 2 in the vendor kernels. 840aa288248SMaxime Ripard */ 841aa288248SMaxime Ripard #define OV5640_PLL_ROOT_DIV 2 842aa288248SMaxime Ripard #define OV5640_PLL_CTRL3_PLL_ROOT_DIV_2 BIT(4) 843aa288248SMaxime Ripard 844aa288248SMaxime Ripard /* 845aa288248SMaxime Ripard * We only supports 8-bit formats at the moment 846aa288248SMaxime Ripard */ 847aa288248SMaxime Ripard #define OV5640_BIT_DIV 2 848aa288248SMaxime Ripard #define OV5640_PLL_CTRL0_MIPI_MODE_8BIT 0x08 849aa288248SMaxime Ripard 850aa288248SMaxime Ripard /* 851aa288248SMaxime Ripard * This is supposed to be ranging from 1 to 8, but the value is always 852aa288248SMaxime Ripard * set to 2 in the vendor kernels. 853aa288248SMaxime Ripard */ 854aa288248SMaxime Ripard #define OV5640_SCLK_ROOT_DIV 2 855aa288248SMaxime Ripard 856aa288248SMaxime Ripard /* 857aa288248SMaxime Ripard * This is hardcoded so that the consistency is maintained between SCLK and 858aa288248SMaxime Ripard * SCLK 2x. 859aa288248SMaxime Ripard */ 860aa288248SMaxime Ripard #define OV5640_SCLK2X_ROOT_DIV (OV5640_SCLK_ROOT_DIV / 2) 861aa288248SMaxime Ripard 862aa288248SMaxime Ripard /* 863aa288248SMaxime Ripard * This is supposed to be ranging from 1 to 8, but the value is always 864aa288248SMaxime Ripard * set to 1 in the vendor kernels. 865aa288248SMaxime Ripard */ 866aa288248SMaxime Ripard #define OV5640_PCLK_ROOT_DIV 1 867aa288248SMaxime Ripard #define OV5640_PLL_SYS_ROOT_DIVIDER_BYPASS 0x00 868aa288248SMaxime Ripard 869aa288248SMaxime Ripard static unsigned long ov5640_compute_sys_clk(struct ov5640_dev *sensor, 870aa288248SMaxime Ripard u8 pll_prediv, u8 pll_mult, 871aa288248SMaxime Ripard u8 sysdiv) 872aa288248SMaxime Ripard { 873aa288248SMaxime Ripard unsigned long sysclk = sensor->xclk_freq / pll_prediv * pll_mult; 874aa288248SMaxime Ripard 875aa288248SMaxime Ripard /* PLL1 output cannot exceed 1GHz. */ 876aa288248SMaxime Ripard if (sysclk / 1000000 > 1000) 877aa288248SMaxime Ripard return 0; 878aa288248SMaxime Ripard 879aa288248SMaxime Ripard return sysclk / sysdiv; 880aa288248SMaxime Ripard } 881aa288248SMaxime Ripard 882aa288248SMaxime Ripard static unsigned long ov5640_calc_sys_clk(struct ov5640_dev *sensor, 883aa288248SMaxime Ripard unsigned long rate, 884aa288248SMaxime Ripard u8 *pll_prediv, u8 *pll_mult, 885aa288248SMaxime Ripard u8 *sysdiv) 886aa288248SMaxime Ripard { 887aa288248SMaxime Ripard unsigned long best = ~0; 888aa288248SMaxime Ripard u8 best_sysdiv = 1, best_mult = 1; 889aa288248SMaxime Ripard u8 _sysdiv, _pll_mult; 890aa288248SMaxime Ripard 891aa288248SMaxime Ripard for (_sysdiv = OV5640_SYSDIV_MIN; 892aa288248SMaxime Ripard _sysdiv <= OV5640_SYSDIV_MAX; 893aa288248SMaxime Ripard _sysdiv++) { 894aa288248SMaxime Ripard for (_pll_mult = OV5640_PLL_MULT_MIN; 895aa288248SMaxime Ripard _pll_mult <= OV5640_PLL_MULT_MAX; 896aa288248SMaxime Ripard _pll_mult++) { 897aa288248SMaxime Ripard unsigned long _rate; 898aa288248SMaxime Ripard 899aa288248SMaxime Ripard /* 900aa288248SMaxime Ripard * The PLL multiplier cannot be odd if above 901aa288248SMaxime Ripard * 127. 902aa288248SMaxime Ripard */ 903aa288248SMaxime Ripard if (_pll_mult > 127 && (_pll_mult % 2)) 904aa288248SMaxime Ripard continue; 905aa288248SMaxime Ripard 906aa288248SMaxime Ripard _rate = ov5640_compute_sys_clk(sensor, 907aa288248SMaxime Ripard OV5640_PLL_PREDIV, 908aa288248SMaxime Ripard _pll_mult, _sysdiv); 909aa288248SMaxime Ripard 910aa288248SMaxime Ripard /* 911aa288248SMaxime Ripard * We have reached the maximum allowed PLL1 output, 912aa288248SMaxime Ripard * increase sysdiv. 913aa288248SMaxime Ripard */ 9142e3df204SAdam Ford if (!_rate) 915aa288248SMaxime Ripard break; 916aa288248SMaxime Ripard 917aa288248SMaxime Ripard /* 918aa288248SMaxime Ripard * Prefer rates above the expected clock rate than 919aa288248SMaxime Ripard * below, even if that means being less precise. 920aa288248SMaxime Ripard */ 921aa288248SMaxime Ripard if (_rate < rate) 922aa288248SMaxime Ripard continue; 923aa288248SMaxime Ripard 924aa288248SMaxime Ripard if (abs(rate - _rate) < abs(rate - best)) { 925aa288248SMaxime Ripard best = _rate; 926aa288248SMaxime Ripard best_sysdiv = _sysdiv; 927aa288248SMaxime Ripard best_mult = _pll_mult; 928aa288248SMaxime Ripard } 929aa288248SMaxime Ripard 930aa288248SMaxime Ripard if (_rate == rate) 931aa288248SMaxime Ripard goto out; 932aa288248SMaxime Ripard } 933aa288248SMaxime Ripard } 934aa288248SMaxime Ripard 935aa288248SMaxime Ripard out: 936aa288248SMaxime Ripard *sysdiv = best_sysdiv; 937aa288248SMaxime Ripard *pll_prediv = OV5640_PLL_PREDIV; 938aa288248SMaxime Ripard *pll_mult = best_mult; 939aa288248SMaxime Ripard 940aa288248SMaxime Ripard return best; 941aa288248SMaxime Ripard } 942aa288248SMaxime Ripard 943aa288248SMaxime Ripard /* 944aa288248SMaxime Ripard * ov5640_set_mipi_pclk() - Calculate the clock tree configuration values 945aa288248SMaxime Ripard * for the MIPI CSI-2 output. 946aa288248SMaxime Ripard * 947aa288248SMaxime Ripard * @rate: The requested bandwidth per lane in bytes per second. 948aa288248SMaxime Ripard * 'Bandwidth Per Lane' is calculated as: 949aa288248SMaxime Ripard * bpl = HTOT * VTOT * FPS * bpp / num_lanes; 950aa288248SMaxime Ripard * 951aa288248SMaxime Ripard * This function use the requested bandwidth to calculate: 952aa288248SMaxime Ripard * - sample_rate = bpl / (bpp / num_lanes); 953aa288248SMaxime Ripard * = bpl / (PLL_RDIV * BIT_DIV * PCLK_DIV * MIPI_DIV / num_lanes); 954aa288248SMaxime Ripard * 955aa288248SMaxime Ripard * - mipi_sclk = bpl / MIPI_DIV / 2; ( / 2 is for CSI-2 DDR) 956aa288248SMaxime Ripard * 957aa288248SMaxime Ripard * with these fixed parameters: 958aa288248SMaxime Ripard * PLL_RDIV = 2; 959aa288248SMaxime Ripard * BIT_DIVIDER = 2; (MIPI_BIT_MODE == 8 ? 2 : 2,5); 960aa288248SMaxime Ripard * PCLK_DIV = 1; 961aa288248SMaxime Ripard * 962aa288248SMaxime Ripard * The MIPI clock generation differs for modes that use the scaler and modes 963aa288248SMaxime Ripard * that do not. In case the scaler is in use, the MIPI_SCLK generates the MIPI 964aa288248SMaxime Ripard * BIT CLk, and thus: 965aa288248SMaxime Ripard * 966aa288248SMaxime Ripard * - mipi_sclk = bpl / MIPI_DIV / 2; 967aa288248SMaxime Ripard * MIPI_DIV = 1; 968aa288248SMaxime Ripard * 969aa288248SMaxime Ripard * For modes that do not go through the scaler, the MIPI BIT CLOCK is generated 970aa288248SMaxime Ripard * from the pixel clock, and thus: 971aa288248SMaxime Ripard * 972aa288248SMaxime Ripard * - sample_rate = bpl / (bpp / num_lanes); 973aa288248SMaxime Ripard * = bpl / (2 * 2 * 1 * MIPI_DIV / num_lanes); 974aa288248SMaxime Ripard * = bpl / (4 * MIPI_DIV / num_lanes); 975aa288248SMaxime Ripard * - MIPI_DIV = bpp / (4 * num_lanes); 976aa288248SMaxime Ripard * 977aa288248SMaxime Ripard * FIXME: this have been tested with 16bpp and 2 lanes setup only. 978aa288248SMaxime Ripard * MIPI_DIV is fixed to value 2, but it -might- be changed according to the 979aa288248SMaxime Ripard * above formula for setups with 1 lane or image formats with different bpp. 980aa288248SMaxime Ripard * 981aa288248SMaxime Ripard * FIXME: this deviates from the sensor manual documentation which is quite 982aa288248SMaxime Ripard * thin on the MIPI clock tree generation part. 983aa288248SMaxime Ripard */ 984aa288248SMaxime Ripard static int ov5640_set_mipi_pclk(struct ov5640_dev *sensor, 985aa288248SMaxime Ripard unsigned long rate) 986aa288248SMaxime Ripard { 987aa288248SMaxime Ripard const struct ov5640_mode_info *mode = sensor->current_mode; 988aa288248SMaxime Ripard u8 prediv, mult, sysdiv; 989aa288248SMaxime Ripard u8 mipi_div; 990aa288248SMaxime Ripard int ret; 991aa288248SMaxime Ripard 992aa288248SMaxime Ripard /* 993aa288248SMaxime Ripard * 1280x720 is reported to use 'SUBSAMPLING' only, 994aa288248SMaxime Ripard * but according to the sensor manual it goes through the 995aa288248SMaxime Ripard * scaler before subsampling. 996aa288248SMaxime Ripard */ 997aa288248SMaxime Ripard if (mode->dn_mode == SCALING || 998aa288248SMaxime Ripard (mode->id == OV5640_MODE_720P_1280_720)) 999aa288248SMaxime Ripard mipi_div = OV5640_MIPI_DIV_SCLK; 1000aa288248SMaxime Ripard else 1001aa288248SMaxime Ripard mipi_div = OV5640_MIPI_DIV_PCLK; 1002aa288248SMaxime Ripard 1003aa288248SMaxime Ripard ov5640_calc_sys_clk(sensor, rate, &prediv, &mult, &sysdiv); 1004aa288248SMaxime Ripard 1005aa288248SMaxime Ripard ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL0, 1006aa288248SMaxime Ripard 0x0f, OV5640_PLL_CTRL0_MIPI_MODE_8BIT); 1007aa288248SMaxime Ripard 1008aa288248SMaxime Ripard ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL1, 1009aa288248SMaxime Ripard 0xff, sysdiv << 4 | mipi_div); 1010aa288248SMaxime Ripard if (ret) 1011aa288248SMaxime Ripard return ret; 1012aa288248SMaxime Ripard 1013aa288248SMaxime Ripard ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL2, 0xff, mult); 1014aa288248SMaxime Ripard if (ret) 1015aa288248SMaxime Ripard return ret; 1016aa288248SMaxime Ripard 1017aa288248SMaxime Ripard ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL3, 1018aa288248SMaxime Ripard 0x1f, OV5640_PLL_CTRL3_PLL_ROOT_DIV_2 | prediv); 1019aa288248SMaxime Ripard if (ret) 1020aa288248SMaxime Ripard return ret; 1021aa288248SMaxime Ripard 1022aa288248SMaxime Ripard return ov5640_mod_reg(sensor, OV5640_REG_SYS_ROOT_DIVIDER, 1023aa288248SMaxime Ripard 0x30, OV5640_PLL_SYS_ROOT_DIVIDER_BYPASS); 1024aa288248SMaxime Ripard } 1025aa288248SMaxime Ripard 1026aa288248SMaxime Ripard static unsigned long ov5640_calc_pclk(struct ov5640_dev *sensor, 1027aa288248SMaxime Ripard unsigned long rate, 1028aa288248SMaxime Ripard u8 *pll_prediv, u8 *pll_mult, u8 *sysdiv, 1029aa288248SMaxime Ripard u8 *pll_rdiv, u8 *bit_div, u8 *pclk_div) 1030aa288248SMaxime Ripard { 1031aa288248SMaxime Ripard unsigned long _rate = rate * OV5640_PLL_ROOT_DIV * OV5640_BIT_DIV * 1032aa288248SMaxime Ripard OV5640_PCLK_ROOT_DIV; 1033aa288248SMaxime Ripard 1034aa288248SMaxime Ripard _rate = ov5640_calc_sys_clk(sensor, _rate, pll_prediv, pll_mult, 1035aa288248SMaxime Ripard sysdiv); 1036aa288248SMaxime Ripard *pll_rdiv = OV5640_PLL_ROOT_DIV; 1037aa288248SMaxime Ripard *bit_div = OV5640_BIT_DIV; 1038aa288248SMaxime Ripard *pclk_div = OV5640_PCLK_ROOT_DIV; 1039aa288248SMaxime Ripard 1040aa288248SMaxime Ripard return _rate / *pll_rdiv / *bit_div / *pclk_div; 1041aa288248SMaxime Ripard } 1042aa288248SMaxime Ripard 1043aa288248SMaxime Ripard static int ov5640_set_dvp_pclk(struct ov5640_dev *sensor, unsigned long rate) 1044aa288248SMaxime Ripard { 1045aa288248SMaxime Ripard u8 prediv, mult, sysdiv, pll_rdiv, bit_div, pclk_div; 1046aa288248SMaxime Ripard int ret; 1047aa288248SMaxime Ripard 1048aa288248SMaxime Ripard ov5640_calc_pclk(sensor, rate, &prediv, &mult, &sysdiv, &pll_rdiv, 1049aa288248SMaxime Ripard &bit_div, &pclk_div); 1050aa288248SMaxime Ripard 1051aa288248SMaxime Ripard if (bit_div == 2) 1052aa288248SMaxime Ripard bit_div = 8; 1053aa288248SMaxime Ripard 1054aa288248SMaxime Ripard ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL0, 1055aa288248SMaxime Ripard 0x0f, bit_div); 1056aa288248SMaxime Ripard if (ret) 1057aa288248SMaxime Ripard return ret; 1058aa288248SMaxime Ripard 1059aa288248SMaxime Ripard /* 1060aa288248SMaxime Ripard * We need to set sysdiv according to the clock, and to clear 1061aa288248SMaxime Ripard * the MIPI divider. 1062aa288248SMaxime Ripard */ 1063aa288248SMaxime Ripard ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL1, 1064aa288248SMaxime Ripard 0xff, sysdiv << 4); 1065aa288248SMaxime Ripard if (ret) 1066aa288248SMaxime Ripard return ret; 1067aa288248SMaxime Ripard 1068aa288248SMaxime Ripard ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL2, 1069aa288248SMaxime Ripard 0xff, mult); 1070aa288248SMaxime Ripard if (ret) 1071aa288248SMaxime Ripard return ret; 1072aa288248SMaxime Ripard 1073aa288248SMaxime Ripard ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL3, 1074aa288248SMaxime Ripard 0x1f, prediv | ((pll_rdiv - 1) << 4)); 1075aa288248SMaxime Ripard if (ret) 1076aa288248SMaxime Ripard return ret; 1077aa288248SMaxime Ripard 1078aa288248SMaxime Ripard return ov5640_mod_reg(sensor, OV5640_REG_SYS_ROOT_DIVIDER, 0x30, 1079aa288248SMaxime Ripard (ilog2(pclk_div) << 4)); 1080aa288248SMaxime Ripard } 1081aa288248SMaxime Ripard 10827cb013b1SChen-Yu Tsai /* set JPEG framing sizes */ 10837cb013b1SChen-Yu Tsai static int ov5640_set_jpeg_timings(struct ov5640_dev *sensor, 10847cb013b1SChen-Yu Tsai const struct ov5640_mode_info *mode) 10857cb013b1SChen-Yu Tsai { 10867cb013b1SChen-Yu Tsai int ret; 10877cb013b1SChen-Yu Tsai 10882b5c18f9SChen-Yu Tsai /* 10892b5c18f9SChen-Yu Tsai * compression mode 3 timing 10902b5c18f9SChen-Yu Tsai * 10912b5c18f9SChen-Yu Tsai * Data is transmitted with programmable width (VFIFO_HSIZE). 10922b5c18f9SChen-Yu Tsai * No padding done. Last line may have less data. Varying 10932b5c18f9SChen-Yu Tsai * number of lines per frame, depending on amount of data. 10942b5c18f9SChen-Yu Tsai */ 10952b5c18f9SChen-Yu Tsai ret = ov5640_mod_reg(sensor, OV5640_REG_JPG_MODE_SELECT, 0x7, 0x3); 10962b5c18f9SChen-Yu Tsai if (ret < 0) 10972b5c18f9SChen-Yu Tsai return ret; 10982b5c18f9SChen-Yu Tsai 10997cb013b1SChen-Yu Tsai ret = ov5640_write_reg16(sensor, OV5640_REG_VFIFO_HSIZE, mode->hact); 11007cb013b1SChen-Yu Tsai if (ret < 0) 11017cb013b1SChen-Yu Tsai return ret; 11027cb013b1SChen-Yu Tsai 11037cb013b1SChen-Yu Tsai return ov5640_write_reg16(sensor, OV5640_REG_VFIFO_VSIZE, mode->vact); 11047cb013b1SChen-Yu Tsai } 11057cb013b1SChen-Yu Tsai 110619a81c14SSteve Longerbeam /* download ov5640 settings to sensor through i2c */ 1107bad1774eSJacopo Mondi static int ov5640_set_timings(struct ov5640_dev *sensor, 1108bad1774eSJacopo Mondi const struct ov5640_mode_info *mode) 1109bad1774eSJacopo Mondi { 1110bad1774eSJacopo Mondi int ret; 1111bad1774eSJacopo Mondi 11127cb013b1SChen-Yu Tsai if (sensor->fmt.code == MEDIA_BUS_FMT_JPEG_1X8) { 11137cb013b1SChen-Yu Tsai ret = ov5640_set_jpeg_timings(sensor, mode); 11147cb013b1SChen-Yu Tsai if (ret < 0) 11157cb013b1SChen-Yu Tsai return ret; 11167cb013b1SChen-Yu Tsai } 11177cb013b1SChen-Yu Tsai 1118bad1774eSJacopo Mondi ret = ov5640_write_reg16(sensor, OV5640_REG_TIMING_DVPHO, mode->hact); 1119bad1774eSJacopo Mondi if (ret < 0) 1120bad1774eSJacopo Mondi return ret; 1121bad1774eSJacopo Mondi 1122bad1774eSJacopo Mondi ret = ov5640_write_reg16(sensor, OV5640_REG_TIMING_DVPVO, mode->vact); 1123bad1774eSJacopo Mondi if (ret < 0) 1124bad1774eSJacopo Mondi return ret; 1125bad1774eSJacopo Mondi 1126bad1774eSJacopo Mondi ret = ov5640_write_reg16(sensor, OV5640_REG_TIMING_HTS, mode->htot); 1127bad1774eSJacopo Mondi if (ret < 0) 1128bad1774eSJacopo Mondi return ret; 1129bad1774eSJacopo Mondi 1130bad1774eSJacopo Mondi return ov5640_write_reg16(sensor, OV5640_REG_TIMING_VTS, mode->vtot); 1131bad1774eSJacopo Mondi } 1132bad1774eSJacopo Mondi 113319a81c14SSteve Longerbeam static int ov5640_load_regs(struct ov5640_dev *sensor, 113419a81c14SSteve Longerbeam const struct ov5640_mode_info *mode) 113519a81c14SSteve Longerbeam { 113619a81c14SSteve Longerbeam const struct reg_value *regs = mode->reg_data; 113719a81c14SSteve Longerbeam unsigned int i; 113819a81c14SSteve Longerbeam u32 delay_ms; 113919a81c14SSteve Longerbeam u16 reg_addr; 114019a81c14SSteve Longerbeam u8 mask, val; 114119a81c14SSteve Longerbeam int ret = 0; 114219a81c14SSteve Longerbeam 114319a81c14SSteve Longerbeam for (i = 0; i < mode->reg_data_size; ++i, ++regs) { 114419a81c14SSteve Longerbeam delay_ms = regs->delay_ms; 114519a81c14SSteve Longerbeam reg_addr = regs->reg_addr; 114619a81c14SSteve Longerbeam val = regs->val; 114719a81c14SSteve Longerbeam mask = regs->mask; 114819a81c14SSteve Longerbeam 11493b987d70SLad Prabhakar /* remain in power down mode for DVP */ 11503b987d70SLad Prabhakar if (regs->reg_addr == OV5640_REG_SYS_CTRL0 && 11513b987d70SLad Prabhakar val == OV5640_REG_SYS_CTRL0_SW_PWUP && 11523b987d70SLad Prabhakar sensor->ep.bus_type != V4L2_MBUS_CSI2_DPHY) 11533b987d70SLad Prabhakar continue; 11543b987d70SLad Prabhakar 115519a81c14SSteve Longerbeam if (mask) 115619a81c14SSteve Longerbeam ret = ov5640_mod_reg(sensor, reg_addr, mask, val); 115719a81c14SSteve Longerbeam else 115819a81c14SSteve Longerbeam ret = ov5640_write_reg(sensor, reg_addr, val); 115919a81c14SSteve Longerbeam if (ret) 116019a81c14SSteve Longerbeam break; 116119a81c14SSteve Longerbeam 116219a81c14SSteve Longerbeam if (delay_ms) 116319a81c14SSteve Longerbeam usleep_range(1000 * delay_ms, 1000 * delay_ms + 100); 116419a81c14SSteve Longerbeam } 116519a81c14SSteve Longerbeam 1166bad1774eSJacopo Mondi return ov5640_set_timings(sensor, mode); 116719a81c14SSteve Longerbeam } 116819a81c14SSteve Longerbeam 1169dc29a1c1SHugues Fruchet static int ov5640_set_autoexposure(struct ov5640_dev *sensor, bool on) 1170dc29a1c1SHugues Fruchet { 1171dc29a1c1SHugues Fruchet return ov5640_mod_reg(sensor, OV5640_REG_AEC_PK_MANUAL, 1172dc29a1c1SHugues Fruchet BIT(0), on ? 0 : BIT(0)); 1173dc29a1c1SHugues Fruchet } 1174dc29a1c1SHugues Fruchet 117519a81c14SSteve Longerbeam /* read exposure, in number of line periods */ 117619a81c14SSteve Longerbeam static int ov5640_get_exposure(struct ov5640_dev *sensor) 117719a81c14SSteve Longerbeam { 117819a81c14SSteve Longerbeam int exp, ret; 117919a81c14SSteve Longerbeam u8 temp; 118019a81c14SSteve Longerbeam 118119a81c14SSteve Longerbeam ret = ov5640_read_reg(sensor, OV5640_REG_AEC_PK_EXPOSURE_HI, &temp); 118219a81c14SSteve Longerbeam if (ret) 118319a81c14SSteve Longerbeam return ret; 118419a81c14SSteve Longerbeam exp = ((int)temp & 0x0f) << 16; 118519a81c14SSteve Longerbeam ret = ov5640_read_reg(sensor, OV5640_REG_AEC_PK_EXPOSURE_MED, &temp); 118619a81c14SSteve Longerbeam if (ret) 118719a81c14SSteve Longerbeam return ret; 118819a81c14SSteve Longerbeam exp |= ((int)temp << 8); 118919a81c14SSteve Longerbeam ret = ov5640_read_reg(sensor, OV5640_REG_AEC_PK_EXPOSURE_LO, &temp); 119019a81c14SSteve Longerbeam if (ret) 119119a81c14SSteve Longerbeam return ret; 119219a81c14SSteve Longerbeam exp |= (int)temp; 119319a81c14SSteve Longerbeam 119419a81c14SSteve Longerbeam return exp >> 4; 119519a81c14SSteve Longerbeam } 119619a81c14SSteve Longerbeam 119719a81c14SSteve Longerbeam /* write exposure, given number of line periods */ 119819a81c14SSteve Longerbeam static int ov5640_set_exposure(struct ov5640_dev *sensor, u32 exposure) 119919a81c14SSteve Longerbeam { 120019a81c14SSteve Longerbeam int ret; 120119a81c14SSteve Longerbeam 120219a81c14SSteve Longerbeam exposure <<= 4; 120319a81c14SSteve Longerbeam 120419a81c14SSteve Longerbeam ret = ov5640_write_reg(sensor, 120519a81c14SSteve Longerbeam OV5640_REG_AEC_PK_EXPOSURE_LO, 120619a81c14SSteve Longerbeam exposure & 0xff); 120719a81c14SSteve Longerbeam if (ret) 120819a81c14SSteve Longerbeam return ret; 120919a81c14SSteve Longerbeam ret = ov5640_write_reg(sensor, 121019a81c14SSteve Longerbeam OV5640_REG_AEC_PK_EXPOSURE_MED, 121119a81c14SSteve Longerbeam (exposure >> 8) & 0xff); 121219a81c14SSteve Longerbeam if (ret) 121319a81c14SSteve Longerbeam return ret; 121419a81c14SSteve Longerbeam return ov5640_write_reg(sensor, 121519a81c14SSteve Longerbeam OV5640_REG_AEC_PK_EXPOSURE_HI, 121619a81c14SSteve Longerbeam (exposure >> 16) & 0x0f); 121719a81c14SSteve Longerbeam } 121819a81c14SSteve Longerbeam 121919a81c14SSteve Longerbeam static int ov5640_get_gain(struct ov5640_dev *sensor) 122019a81c14SSteve Longerbeam { 122119a81c14SSteve Longerbeam u16 gain; 122219a81c14SSteve Longerbeam int ret; 122319a81c14SSteve Longerbeam 122419a81c14SSteve Longerbeam ret = ov5640_read_reg16(sensor, OV5640_REG_AEC_PK_REAL_GAIN, &gain); 122519a81c14SSteve Longerbeam if (ret) 122619a81c14SSteve Longerbeam return ret; 122719a81c14SSteve Longerbeam 122819a81c14SSteve Longerbeam return gain & 0x3ff; 122919a81c14SSteve Longerbeam } 123019a81c14SSteve Longerbeam 12313cca8ef5SHugues Fruchet static int ov5640_set_gain(struct ov5640_dev *sensor, int gain) 12323cca8ef5SHugues Fruchet { 12333cca8ef5SHugues Fruchet return ov5640_write_reg16(sensor, OV5640_REG_AEC_PK_REAL_GAIN, 12343cca8ef5SHugues Fruchet (u16)gain & 0x3ff); 12353cca8ef5SHugues Fruchet } 12363cca8ef5SHugues Fruchet 12373cca8ef5SHugues Fruchet static int ov5640_set_autogain(struct ov5640_dev *sensor, bool on) 12383cca8ef5SHugues Fruchet { 12393cca8ef5SHugues Fruchet return ov5640_mod_reg(sensor, OV5640_REG_AEC_PK_MANUAL, 12403cca8ef5SHugues Fruchet BIT(1), on ? 0 : BIT(1)); 12413cca8ef5SHugues Fruchet } 12423cca8ef5SHugues Fruchet 1243f22996dbSHugues Fruchet static int ov5640_set_stream_dvp(struct ov5640_dev *sensor, bool on) 1244f22996dbSHugues Fruchet { 12453b987d70SLad Prabhakar return ov5640_write_reg(sensor, OV5640_REG_SYS_CTRL0, on ? 12463b987d70SLad Prabhakar OV5640_REG_SYS_CTRL0_SW_PWUP : 12473b987d70SLad Prabhakar OV5640_REG_SYS_CTRL0_SW_PWDN); 1248f22996dbSHugues Fruchet } 1249f22996dbSHugues Fruchet 1250f22996dbSHugues Fruchet static int ov5640_set_stream_mipi(struct ov5640_dev *sensor, bool on) 125119a81c14SSteve Longerbeam { 125219a81c14SSteve Longerbeam int ret; 125319a81c14SSteve Longerbeam 1254aa4bb8b8SJacopo Mondi /* 1255aa4bb8b8SJacopo Mondi * Enable/disable the MIPI interface 1256aa4bb8b8SJacopo Mondi * 1257aa4bb8b8SJacopo Mondi * 0x300e = on ? 0x45 : 0x40 1258aa4bb8b8SJacopo Mondi * 1259aa4bb8b8SJacopo Mondi * FIXME: the sensor manual (version 2.03) reports 1260aa4bb8b8SJacopo Mondi * [7:5] = 000 : 1 data lane mode 1261aa4bb8b8SJacopo Mondi * [7:5] = 001 : 2 data lanes mode 1262aa4bb8b8SJacopo Mondi * But this settings do not work, while the following ones 1263aa4bb8b8SJacopo Mondi * have been validated for 2 data lanes mode. 1264aa4bb8b8SJacopo Mondi * 1265aa4bb8b8SJacopo Mondi * [7:5] = 010 : 2 data lanes mode 1266aa4bb8b8SJacopo Mondi * [4] = 0 : Power up MIPI HS Tx 1267aa4bb8b8SJacopo Mondi * [3] = 0 : Power up MIPI LS Rx 1268aa4bb8b8SJacopo Mondi * [2] = 1/0 : MIPI interface enable/disable 1269aa4bb8b8SJacopo Mondi * [1:0] = 01/00: FIXME: 'debug' 1270aa4bb8b8SJacopo Mondi */ 1271aa4bb8b8SJacopo Mondi ret = ov5640_write_reg(sensor, OV5640_REG_IO_MIPI_CTRL00, 1272aa4bb8b8SJacopo Mondi on ? 0x45 : 0x40); 127319a81c14SSteve Longerbeam if (ret) 127419a81c14SSteve Longerbeam return ret; 127519a81c14SSteve Longerbeam 127619a81c14SSteve Longerbeam return ov5640_write_reg(sensor, OV5640_REG_FRAME_CTRL01, 127719a81c14SSteve Longerbeam on ? 0x00 : 0x0f); 127819a81c14SSteve Longerbeam } 127919a81c14SSteve Longerbeam 128019a81c14SSteve Longerbeam static int ov5640_get_sysclk(struct ov5640_dev *sensor) 128119a81c14SSteve Longerbeam { 128219a81c14SSteve Longerbeam /* calculate sysclk */ 128319a81c14SSteve Longerbeam u32 xvclk = sensor->xclk_freq / 10000; 128419a81c14SSteve Longerbeam u32 multiplier, prediv, VCO, sysdiv, pll_rdiv; 128519a81c14SSteve Longerbeam u32 sclk_rdiv_map[] = {1, 2, 4, 8}; 128619a81c14SSteve Longerbeam u32 bit_div2x = 1, sclk_rdiv, sysclk; 128719a81c14SSteve Longerbeam u8 temp1, temp2; 128819a81c14SSteve Longerbeam int ret; 128919a81c14SSteve Longerbeam 129019a81c14SSteve Longerbeam ret = ov5640_read_reg(sensor, OV5640_REG_SC_PLL_CTRL0, &temp1); 129119a81c14SSteve Longerbeam if (ret) 129219a81c14SSteve Longerbeam return ret; 129319a81c14SSteve Longerbeam temp2 = temp1 & 0x0f; 129419a81c14SSteve Longerbeam if (temp2 == 8 || temp2 == 10) 129519a81c14SSteve Longerbeam bit_div2x = temp2 / 2; 129619a81c14SSteve Longerbeam 129719a81c14SSteve Longerbeam ret = ov5640_read_reg(sensor, OV5640_REG_SC_PLL_CTRL1, &temp1); 129819a81c14SSteve Longerbeam if (ret) 129919a81c14SSteve Longerbeam return ret; 130019a81c14SSteve Longerbeam sysdiv = temp1 >> 4; 130119a81c14SSteve Longerbeam if (sysdiv == 0) 130219a81c14SSteve Longerbeam sysdiv = 16; 130319a81c14SSteve Longerbeam 130419a81c14SSteve Longerbeam ret = ov5640_read_reg(sensor, OV5640_REG_SC_PLL_CTRL2, &temp1); 130519a81c14SSteve Longerbeam if (ret) 130619a81c14SSteve Longerbeam return ret; 130719a81c14SSteve Longerbeam multiplier = temp1; 130819a81c14SSteve Longerbeam 130919a81c14SSteve Longerbeam ret = ov5640_read_reg(sensor, OV5640_REG_SC_PLL_CTRL3, &temp1); 131019a81c14SSteve Longerbeam if (ret) 131119a81c14SSteve Longerbeam return ret; 131219a81c14SSteve Longerbeam prediv = temp1 & 0x0f; 131319a81c14SSteve Longerbeam pll_rdiv = ((temp1 >> 4) & 0x01) + 1; 131419a81c14SSteve Longerbeam 131519a81c14SSteve Longerbeam ret = ov5640_read_reg(sensor, OV5640_REG_SYS_ROOT_DIVIDER, &temp1); 131619a81c14SSteve Longerbeam if (ret) 131719a81c14SSteve Longerbeam return ret; 131819a81c14SSteve Longerbeam temp2 = temp1 & 0x03; 131919a81c14SSteve Longerbeam sclk_rdiv = sclk_rdiv_map[temp2]; 132019a81c14SSteve Longerbeam 132119a81c14SSteve Longerbeam if (!prediv || !sysdiv || !pll_rdiv || !bit_div2x) 132219a81c14SSteve Longerbeam return -EINVAL; 132319a81c14SSteve Longerbeam 132419a81c14SSteve Longerbeam VCO = xvclk * multiplier / prediv; 132519a81c14SSteve Longerbeam 132619a81c14SSteve Longerbeam sysclk = VCO / sysdiv / pll_rdiv * 2 / bit_div2x / sclk_rdiv; 132719a81c14SSteve Longerbeam 132819a81c14SSteve Longerbeam return sysclk; 132919a81c14SSteve Longerbeam } 133019a81c14SSteve Longerbeam 133119a81c14SSteve Longerbeam static int ov5640_set_night_mode(struct ov5640_dev *sensor) 133219a81c14SSteve Longerbeam { 133319a81c14SSteve Longerbeam /* read HTS from register settings */ 133419a81c14SSteve Longerbeam u8 mode; 133519a81c14SSteve Longerbeam int ret; 133619a81c14SSteve Longerbeam 133719a81c14SSteve Longerbeam ret = ov5640_read_reg(sensor, OV5640_REG_AEC_CTRL00, &mode); 133819a81c14SSteve Longerbeam if (ret) 133919a81c14SSteve Longerbeam return ret; 134019a81c14SSteve Longerbeam mode &= 0xfb; 134119a81c14SSteve Longerbeam return ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL00, mode); 134219a81c14SSteve Longerbeam } 134319a81c14SSteve Longerbeam 134419a81c14SSteve Longerbeam static int ov5640_get_hts(struct ov5640_dev *sensor) 134519a81c14SSteve Longerbeam { 134619a81c14SSteve Longerbeam /* read HTS from register settings */ 134719a81c14SSteve Longerbeam u16 hts; 134819a81c14SSteve Longerbeam int ret; 134919a81c14SSteve Longerbeam 135019a81c14SSteve Longerbeam ret = ov5640_read_reg16(sensor, OV5640_REG_TIMING_HTS, &hts); 135119a81c14SSteve Longerbeam if (ret) 135219a81c14SSteve Longerbeam return ret; 135319a81c14SSteve Longerbeam return hts; 135419a81c14SSteve Longerbeam } 135519a81c14SSteve Longerbeam 135619a81c14SSteve Longerbeam static int ov5640_get_vts(struct ov5640_dev *sensor) 135719a81c14SSteve Longerbeam { 135819a81c14SSteve Longerbeam u16 vts; 135919a81c14SSteve Longerbeam int ret; 136019a81c14SSteve Longerbeam 136119a81c14SSteve Longerbeam ret = ov5640_read_reg16(sensor, OV5640_REG_TIMING_VTS, &vts); 136219a81c14SSteve Longerbeam if (ret) 136319a81c14SSteve Longerbeam return ret; 136419a81c14SSteve Longerbeam return vts; 136519a81c14SSteve Longerbeam } 136619a81c14SSteve Longerbeam 136719a81c14SSteve Longerbeam static int ov5640_set_vts(struct ov5640_dev *sensor, int vts) 136819a81c14SSteve Longerbeam { 136919a81c14SSteve Longerbeam return ov5640_write_reg16(sensor, OV5640_REG_TIMING_VTS, vts); 137019a81c14SSteve Longerbeam } 137119a81c14SSteve Longerbeam 137219a81c14SSteve Longerbeam static int ov5640_get_light_freq(struct ov5640_dev *sensor) 137319a81c14SSteve Longerbeam { 137419a81c14SSteve Longerbeam /* get banding filter value */ 137519a81c14SSteve Longerbeam int ret, light_freq = 0; 137619a81c14SSteve Longerbeam u8 temp, temp1; 137719a81c14SSteve Longerbeam 137819a81c14SSteve Longerbeam ret = ov5640_read_reg(sensor, OV5640_REG_HZ5060_CTRL01, &temp); 137919a81c14SSteve Longerbeam if (ret) 138019a81c14SSteve Longerbeam return ret; 138119a81c14SSteve Longerbeam 138219a81c14SSteve Longerbeam if (temp & 0x80) { 138319a81c14SSteve Longerbeam /* manual */ 138419a81c14SSteve Longerbeam ret = ov5640_read_reg(sensor, OV5640_REG_HZ5060_CTRL00, 138519a81c14SSteve Longerbeam &temp1); 138619a81c14SSteve Longerbeam if (ret) 138719a81c14SSteve Longerbeam return ret; 138819a81c14SSteve Longerbeam if (temp1 & 0x04) { 138919a81c14SSteve Longerbeam /* 50Hz */ 139019a81c14SSteve Longerbeam light_freq = 50; 139119a81c14SSteve Longerbeam } else { 139219a81c14SSteve Longerbeam /* 60Hz */ 139319a81c14SSteve Longerbeam light_freq = 60; 139419a81c14SSteve Longerbeam } 139519a81c14SSteve Longerbeam } else { 139619a81c14SSteve Longerbeam /* auto */ 139719a81c14SSteve Longerbeam ret = ov5640_read_reg(sensor, OV5640_REG_SIGMADELTA_CTRL0C, 139819a81c14SSteve Longerbeam &temp1); 139919a81c14SSteve Longerbeam if (ret) 140019a81c14SSteve Longerbeam return ret; 140119a81c14SSteve Longerbeam 140219a81c14SSteve Longerbeam if (temp1 & 0x01) { 140319a81c14SSteve Longerbeam /* 50Hz */ 140419a81c14SSteve Longerbeam light_freq = 50; 140519a81c14SSteve Longerbeam } else { 140619a81c14SSteve Longerbeam /* 60Hz */ 140719a81c14SSteve Longerbeam } 140819a81c14SSteve Longerbeam } 140919a81c14SSteve Longerbeam 141019a81c14SSteve Longerbeam return light_freq; 141119a81c14SSteve Longerbeam } 141219a81c14SSteve Longerbeam 141319a81c14SSteve Longerbeam static int ov5640_set_bandingfilter(struct ov5640_dev *sensor) 141419a81c14SSteve Longerbeam { 141519a81c14SSteve Longerbeam u32 band_step60, max_band60, band_step50, max_band50, prev_vts; 141619a81c14SSteve Longerbeam int ret; 141719a81c14SSteve Longerbeam 141819a81c14SSteve Longerbeam /* read preview PCLK */ 141919a81c14SSteve Longerbeam ret = ov5640_get_sysclk(sensor); 142019a81c14SSteve Longerbeam if (ret < 0) 142119a81c14SSteve Longerbeam return ret; 142219a81c14SSteve Longerbeam if (ret == 0) 142319a81c14SSteve Longerbeam return -EINVAL; 142419a81c14SSteve Longerbeam sensor->prev_sysclk = ret; 142519a81c14SSteve Longerbeam /* read preview HTS */ 142619a81c14SSteve Longerbeam ret = ov5640_get_hts(sensor); 142719a81c14SSteve Longerbeam if (ret < 0) 142819a81c14SSteve Longerbeam return ret; 142919a81c14SSteve Longerbeam if (ret == 0) 143019a81c14SSteve Longerbeam return -EINVAL; 143119a81c14SSteve Longerbeam sensor->prev_hts = ret; 143219a81c14SSteve Longerbeam 143319a81c14SSteve Longerbeam /* read preview VTS */ 143419a81c14SSteve Longerbeam ret = ov5640_get_vts(sensor); 143519a81c14SSteve Longerbeam if (ret < 0) 143619a81c14SSteve Longerbeam return ret; 143719a81c14SSteve Longerbeam prev_vts = ret; 143819a81c14SSteve Longerbeam 143919a81c14SSteve Longerbeam /* calculate banding filter */ 144019a81c14SSteve Longerbeam /* 60Hz */ 144119a81c14SSteve Longerbeam band_step60 = sensor->prev_sysclk * 100 / sensor->prev_hts * 100 / 120; 144219a81c14SSteve Longerbeam ret = ov5640_write_reg16(sensor, OV5640_REG_AEC_B60_STEP, band_step60); 144319a81c14SSteve Longerbeam if (ret) 144419a81c14SSteve Longerbeam return ret; 144519a81c14SSteve Longerbeam if (!band_step60) 144619a81c14SSteve Longerbeam return -EINVAL; 144719a81c14SSteve Longerbeam max_band60 = (int)((prev_vts - 4) / band_step60); 144819a81c14SSteve Longerbeam ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL0D, max_band60); 144919a81c14SSteve Longerbeam if (ret) 145019a81c14SSteve Longerbeam return ret; 145119a81c14SSteve Longerbeam 145219a81c14SSteve Longerbeam /* 50Hz */ 145319a81c14SSteve Longerbeam band_step50 = sensor->prev_sysclk * 100 / sensor->prev_hts; 145419a81c14SSteve Longerbeam ret = ov5640_write_reg16(sensor, OV5640_REG_AEC_B50_STEP, band_step50); 145519a81c14SSteve Longerbeam if (ret) 145619a81c14SSteve Longerbeam return ret; 145719a81c14SSteve Longerbeam if (!band_step50) 145819a81c14SSteve Longerbeam return -EINVAL; 145919a81c14SSteve Longerbeam max_band50 = (int)((prev_vts - 4) / band_step50); 146019a81c14SSteve Longerbeam return ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL0E, max_band50); 146119a81c14SSteve Longerbeam } 146219a81c14SSteve Longerbeam 146319a81c14SSteve Longerbeam static int ov5640_set_ae_target(struct ov5640_dev *sensor, int target) 146419a81c14SSteve Longerbeam { 146519a81c14SSteve Longerbeam /* stable in high */ 146619a81c14SSteve Longerbeam u32 fast_high, fast_low; 146719a81c14SSteve Longerbeam int ret; 146819a81c14SSteve Longerbeam 146919a81c14SSteve Longerbeam sensor->ae_low = target * 23 / 25; /* 0.92 */ 147019a81c14SSteve Longerbeam sensor->ae_high = target * 27 / 25; /* 1.08 */ 147119a81c14SSteve Longerbeam 147219a81c14SSteve Longerbeam fast_high = sensor->ae_high << 1; 147319a81c14SSteve Longerbeam if (fast_high > 255) 147419a81c14SSteve Longerbeam fast_high = 255; 147519a81c14SSteve Longerbeam 147619a81c14SSteve Longerbeam fast_low = sensor->ae_low >> 1; 147719a81c14SSteve Longerbeam 147819a81c14SSteve Longerbeam ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL0F, sensor->ae_high); 147919a81c14SSteve Longerbeam if (ret) 148019a81c14SSteve Longerbeam return ret; 148119a81c14SSteve Longerbeam ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL10, sensor->ae_low); 148219a81c14SSteve Longerbeam if (ret) 148319a81c14SSteve Longerbeam return ret; 148419a81c14SSteve Longerbeam ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL1B, sensor->ae_high); 148519a81c14SSteve Longerbeam if (ret) 148619a81c14SSteve Longerbeam return ret; 148719a81c14SSteve Longerbeam ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL1E, sensor->ae_low); 148819a81c14SSteve Longerbeam if (ret) 148919a81c14SSteve Longerbeam return ret; 149019a81c14SSteve Longerbeam ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL11, fast_high); 149119a81c14SSteve Longerbeam if (ret) 149219a81c14SSteve Longerbeam return ret; 149319a81c14SSteve Longerbeam return ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL1F, fast_low); 149419a81c14SSteve Longerbeam } 149519a81c14SSteve Longerbeam 1496c2c3f42dSHugues Fruchet static int ov5640_get_binning(struct ov5640_dev *sensor) 149719a81c14SSteve Longerbeam { 149819a81c14SSteve Longerbeam u8 temp; 149919a81c14SSteve Longerbeam int ret; 150019a81c14SSteve Longerbeam 150119a81c14SSteve Longerbeam ret = ov5640_read_reg(sensor, OV5640_REG_TIMING_TC_REG21, &temp); 150219a81c14SSteve Longerbeam if (ret) 150319a81c14SSteve Longerbeam return ret; 1504c2c3f42dSHugues Fruchet 1505c2c3f42dSHugues Fruchet return temp & BIT(0); 150619a81c14SSteve Longerbeam } 150719a81c14SSteve Longerbeam 1508ce85705aSHugues Fruchet static int ov5640_set_binning(struct ov5640_dev *sensor, bool enable) 1509ce85705aSHugues Fruchet { 1510ce85705aSHugues Fruchet int ret; 1511ce85705aSHugues Fruchet 1512ce85705aSHugues Fruchet /* 1513ce85705aSHugues Fruchet * TIMING TC REG21: 1514ce85705aSHugues Fruchet * - [0]: Horizontal binning enable 1515ce85705aSHugues Fruchet */ 1516ce85705aSHugues Fruchet ret = ov5640_mod_reg(sensor, OV5640_REG_TIMING_TC_REG21, 1517ce85705aSHugues Fruchet BIT(0), enable ? BIT(0) : 0); 1518ce85705aSHugues Fruchet if (ret) 1519ce85705aSHugues Fruchet return ret; 1520ce85705aSHugues Fruchet /* 1521ce85705aSHugues Fruchet * TIMING TC REG20: 1522ce85705aSHugues Fruchet * - [0]: Undocumented, but hardcoded init sequences 1523ce85705aSHugues Fruchet * are always setting REG21/REG20 bit 0 to same value... 1524ce85705aSHugues Fruchet */ 1525ce85705aSHugues Fruchet return ov5640_mod_reg(sensor, OV5640_REG_TIMING_TC_REG20, 1526ce85705aSHugues Fruchet BIT(0), enable ? BIT(0) : 0); 1527ce85705aSHugues Fruchet } 1528ce85705aSHugues Fruchet 152919a81c14SSteve Longerbeam static int ov5640_set_virtual_channel(struct ov5640_dev *sensor) 153019a81c14SSteve Longerbeam { 15318670d70aSHugues Fruchet struct i2c_client *client = sensor->i2c_client; 153219a81c14SSteve Longerbeam u8 temp, channel = virtual_channel; 153319a81c14SSteve Longerbeam int ret; 153419a81c14SSteve Longerbeam 15358670d70aSHugues Fruchet if (channel > 3) { 15368670d70aSHugues Fruchet dev_err(&client->dev, 15378670d70aSHugues Fruchet "%s: wrong virtual_channel parameter, expected (0..3), got %d\n", 15388670d70aSHugues Fruchet __func__, channel); 153919a81c14SSteve Longerbeam return -EINVAL; 15408670d70aSHugues Fruchet } 154119a81c14SSteve Longerbeam 154219a81c14SSteve Longerbeam ret = ov5640_read_reg(sensor, OV5640_REG_DEBUG_MODE, &temp); 154319a81c14SSteve Longerbeam if (ret) 154419a81c14SSteve Longerbeam return ret; 154519a81c14SSteve Longerbeam temp &= ~(3 << 6); 154619a81c14SSteve Longerbeam temp |= (channel << 6); 154719a81c14SSteve Longerbeam return ov5640_write_reg(sensor, OV5640_REG_DEBUG_MODE, temp); 154819a81c14SSteve Longerbeam } 154919a81c14SSteve Longerbeam 155019a81c14SSteve Longerbeam static const struct ov5640_mode_info * 155119a81c14SSteve Longerbeam ov5640_find_mode(struct ov5640_dev *sensor, enum ov5640_frame_rate fr, 155219a81c14SSteve Longerbeam int width, int height, bool nearest) 155319a81c14SSteve Longerbeam { 15543c4a7372SHugues Fruchet const struct ov5640_mode_info *mode; 155519a81c14SSteve Longerbeam 1556086c25f8SMaxime Ripard mode = v4l2_find_nearest_size(ov5640_mode_data, 1557086c25f8SMaxime Ripard ARRAY_SIZE(ov5640_mode_data), 15583c4a7372SHugues Fruchet hact, vact, 15593c4a7372SHugues Fruchet width, height); 156019a81c14SSteve Longerbeam 15613c4a7372SHugues Fruchet if (!mode || 15623c4a7372SHugues Fruchet (!nearest && (mode->hact != width || mode->vact != height))) 15633c4a7372SHugues Fruchet return NULL; 156419a81c14SSteve Longerbeam 15655554c80eSAdam Ford /* Check to see if the current mode exceeds the max frame rate */ 15665554c80eSAdam Ford if (ov5640_framerates[fr] > ov5640_framerates[mode->max_fps]) 1567981e4454SBenoit Parrot return NULL; 1568981e4454SBenoit Parrot 156919a81c14SSteve Longerbeam return mode; 157019a81c14SSteve Longerbeam } 157119a81c14SSteve Longerbeam 1572cc196e48SBenoit Parrot static u64 ov5640_calc_pixel_rate(struct ov5640_dev *sensor) 1573cc196e48SBenoit Parrot { 1574cc196e48SBenoit Parrot u64 rate; 1575cc196e48SBenoit Parrot 1576cc196e48SBenoit Parrot rate = sensor->current_mode->vtot * sensor->current_mode->htot; 1577cc196e48SBenoit Parrot rate *= ov5640_framerates[sensor->current_fr]; 1578cc196e48SBenoit Parrot 1579cc196e48SBenoit Parrot return rate; 1580cc196e48SBenoit Parrot } 1581cc196e48SBenoit Parrot 158219a81c14SSteve Longerbeam /* 158319a81c14SSteve Longerbeam * sensor changes between scaling and subsampling, go through 158419a81c14SSteve Longerbeam * exposure calculation 158519a81c14SSteve Longerbeam */ 158641d8d7f5SHugues Fruchet static int ov5640_set_mode_exposure_calc(struct ov5640_dev *sensor, 158741d8d7f5SHugues Fruchet const struct ov5640_mode_info *mode) 158819a81c14SSteve Longerbeam { 158919a81c14SSteve Longerbeam u32 prev_shutter, prev_gain16; 159019a81c14SSteve Longerbeam u32 cap_shutter, cap_gain16; 159119a81c14SSteve Longerbeam u32 cap_sysclk, cap_hts, cap_vts; 159219a81c14SSteve Longerbeam u32 light_freq, cap_bandfilt, cap_maxband; 159319a81c14SSteve Longerbeam u32 cap_gain16_shutter; 159419a81c14SSteve Longerbeam u8 average; 159519a81c14SSteve Longerbeam int ret; 159619a81c14SSteve Longerbeam 159741d8d7f5SHugues Fruchet if (!mode->reg_data) 159819a81c14SSteve Longerbeam return -EINVAL; 159919a81c14SSteve Longerbeam 160019a81c14SSteve Longerbeam /* read preview shutter */ 160119a81c14SSteve Longerbeam ret = ov5640_get_exposure(sensor); 160219a81c14SSteve Longerbeam if (ret < 0) 160319a81c14SSteve Longerbeam return ret; 160419a81c14SSteve Longerbeam prev_shutter = ret; 1605c2c3f42dSHugues Fruchet ret = ov5640_get_binning(sensor); 160619a81c14SSteve Longerbeam if (ret < 0) 160719a81c14SSteve Longerbeam return ret; 160819a81c14SSteve Longerbeam if (ret && mode->id != OV5640_MODE_720P_1280_720 && 160919a81c14SSteve Longerbeam mode->id != OV5640_MODE_1080P_1920_1080) 161019a81c14SSteve Longerbeam prev_shutter *= 2; 161119a81c14SSteve Longerbeam 161219a81c14SSteve Longerbeam /* read preview gain */ 161319a81c14SSteve Longerbeam ret = ov5640_get_gain(sensor); 161419a81c14SSteve Longerbeam if (ret < 0) 161519a81c14SSteve Longerbeam return ret; 161619a81c14SSteve Longerbeam prev_gain16 = ret; 161719a81c14SSteve Longerbeam 161819a81c14SSteve Longerbeam /* get average */ 161919a81c14SSteve Longerbeam ret = ov5640_read_reg(sensor, OV5640_REG_AVG_READOUT, &average); 162019a81c14SSteve Longerbeam if (ret) 162119a81c14SSteve Longerbeam return ret; 162219a81c14SSteve Longerbeam 162319a81c14SSteve Longerbeam /* turn off night mode for capture */ 162419a81c14SSteve Longerbeam ret = ov5640_set_night_mode(sensor); 162519a81c14SSteve Longerbeam if (ret < 0) 162619a81c14SSteve Longerbeam return ret; 162719a81c14SSteve Longerbeam 162819a81c14SSteve Longerbeam /* Write capture setting */ 162919a81c14SSteve Longerbeam ret = ov5640_load_regs(sensor, mode); 163019a81c14SSteve Longerbeam if (ret < 0) 163119a81c14SSteve Longerbeam return ret; 163219a81c14SSteve Longerbeam 163319a81c14SSteve Longerbeam /* read capture VTS */ 163419a81c14SSteve Longerbeam ret = ov5640_get_vts(sensor); 163519a81c14SSteve Longerbeam if (ret < 0) 163619a81c14SSteve Longerbeam return ret; 163719a81c14SSteve Longerbeam cap_vts = ret; 163819a81c14SSteve Longerbeam ret = ov5640_get_hts(sensor); 163919a81c14SSteve Longerbeam if (ret < 0) 164019a81c14SSteve Longerbeam return ret; 164119a81c14SSteve Longerbeam if (ret == 0) 164219a81c14SSteve Longerbeam return -EINVAL; 164319a81c14SSteve Longerbeam cap_hts = ret; 164419a81c14SSteve Longerbeam 164519a81c14SSteve Longerbeam ret = ov5640_get_sysclk(sensor); 164619a81c14SSteve Longerbeam if (ret < 0) 164719a81c14SSteve Longerbeam return ret; 164819a81c14SSteve Longerbeam if (ret == 0) 164919a81c14SSteve Longerbeam return -EINVAL; 165019a81c14SSteve Longerbeam cap_sysclk = ret; 165119a81c14SSteve Longerbeam 165219a81c14SSteve Longerbeam /* calculate capture banding filter */ 165319a81c14SSteve Longerbeam ret = ov5640_get_light_freq(sensor); 165419a81c14SSteve Longerbeam if (ret < 0) 165519a81c14SSteve Longerbeam return ret; 165619a81c14SSteve Longerbeam light_freq = ret; 165719a81c14SSteve Longerbeam 165819a81c14SSteve Longerbeam if (light_freq == 60) { 165919a81c14SSteve Longerbeam /* 60Hz */ 166019a81c14SSteve Longerbeam cap_bandfilt = cap_sysclk * 100 / cap_hts * 100 / 120; 166119a81c14SSteve Longerbeam } else { 166219a81c14SSteve Longerbeam /* 50Hz */ 166319a81c14SSteve Longerbeam cap_bandfilt = cap_sysclk * 100 / cap_hts; 166419a81c14SSteve Longerbeam } 166519a81c14SSteve Longerbeam 166619a81c14SSteve Longerbeam if (!sensor->prev_sysclk) { 166719a81c14SSteve Longerbeam ret = ov5640_get_sysclk(sensor); 166819a81c14SSteve Longerbeam if (ret < 0) 166919a81c14SSteve Longerbeam return ret; 167019a81c14SSteve Longerbeam if (ret == 0) 167119a81c14SSteve Longerbeam return -EINVAL; 167219a81c14SSteve Longerbeam sensor->prev_sysclk = ret; 167319a81c14SSteve Longerbeam } 167419a81c14SSteve Longerbeam 167519a81c14SSteve Longerbeam if (!cap_bandfilt) 167619a81c14SSteve Longerbeam return -EINVAL; 167719a81c14SSteve Longerbeam 167819a81c14SSteve Longerbeam cap_maxband = (int)((cap_vts - 4) / cap_bandfilt); 167919a81c14SSteve Longerbeam 168019a81c14SSteve Longerbeam /* calculate capture shutter/gain16 */ 168119a81c14SSteve Longerbeam if (average > sensor->ae_low && average < sensor->ae_high) { 168219a81c14SSteve Longerbeam /* in stable range */ 168319a81c14SSteve Longerbeam cap_gain16_shutter = 168419a81c14SSteve Longerbeam prev_gain16 * prev_shutter * 168519a81c14SSteve Longerbeam cap_sysclk / sensor->prev_sysclk * 168619a81c14SSteve Longerbeam sensor->prev_hts / cap_hts * 168719a81c14SSteve Longerbeam sensor->ae_target / average; 168819a81c14SSteve Longerbeam } else { 168919a81c14SSteve Longerbeam cap_gain16_shutter = 169019a81c14SSteve Longerbeam prev_gain16 * prev_shutter * 169119a81c14SSteve Longerbeam cap_sysclk / sensor->prev_sysclk * 169219a81c14SSteve Longerbeam sensor->prev_hts / cap_hts; 169319a81c14SSteve Longerbeam } 169419a81c14SSteve Longerbeam 169519a81c14SSteve Longerbeam /* gain to shutter */ 169619a81c14SSteve Longerbeam if (cap_gain16_shutter < (cap_bandfilt * 16)) { 169719a81c14SSteve Longerbeam /* shutter < 1/100 */ 169819a81c14SSteve Longerbeam cap_shutter = cap_gain16_shutter / 16; 169919a81c14SSteve Longerbeam if (cap_shutter < 1) 170019a81c14SSteve Longerbeam cap_shutter = 1; 170119a81c14SSteve Longerbeam 170219a81c14SSteve Longerbeam cap_gain16 = cap_gain16_shutter / cap_shutter; 170319a81c14SSteve Longerbeam if (cap_gain16 < 16) 170419a81c14SSteve Longerbeam cap_gain16 = 16; 170519a81c14SSteve Longerbeam } else { 170619a81c14SSteve Longerbeam if (cap_gain16_shutter > (cap_bandfilt * cap_maxband * 16)) { 170719a81c14SSteve Longerbeam /* exposure reach max */ 170819a81c14SSteve Longerbeam cap_shutter = cap_bandfilt * cap_maxband; 170919a81c14SSteve Longerbeam if (!cap_shutter) 171019a81c14SSteve Longerbeam return -EINVAL; 171119a81c14SSteve Longerbeam 171219a81c14SSteve Longerbeam cap_gain16 = cap_gain16_shutter / cap_shutter; 171319a81c14SSteve Longerbeam } else { 171419a81c14SSteve Longerbeam /* 1/100 < (cap_shutter = n/100) =< max */ 171519a81c14SSteve Longerbeam cap_shutter = 171619a81c14SSteve Longerbeam ((int)(cap_gain16_shutter / 16 / cap_bandfilt)) 171719a81c14SSteve Longerbeam * cap_bandfilt; 171819a81c14SSteve Longerbeam if (!cap_shutter) 171919a81c14SSteve Longerbeam return -EINVAL; 172019a81c14SSteve Longerbeam 172119a81c14SSteve Longerbeam cap_gain16 = cap_gain16_shutter / cap_shutter; 172219a81c14SSteve Longerbeam } 172319a81c14SSteve Longerbeam } 172419a81c14SSteve Longerbeam 172519a81c14SSteve Longerbeam /* set capture gain */ 17263cca8ef5SHugues Fruchet ret = ov5640_set_gain(sensor, cap_gain16); 172719a81c14SSteve Longerbeam if (ret) 172819a81c14SSteve Longerbeam return ret; 172919a81c14SSteve Longerbeam 173019a81c14SSteve Longerbeam /* write capture shutter */ 173119a81c14SSteve Longerbeam if (cap_shutter > (cap_vts - 4)) { 173219a81c14SSteve Longerbeam cap_vts = cap_shutter + 4; 173319a81c14SSteve Longerbeam ret = ov5640_set_vts(sensor, cap_vts); 173419a81c14SSteve Longerbeam if (ret < 0) 173519a81c14SSteve Longerbeam return ret; 173619a81c14SSteve Longerbeam } 173719a81c14SSteve Longerbeam 173819a81c14SSteve Longerbeam /* set exposure */ 17393cca8ef5SHugues Fruchet return ov5640_set_exposure(sensor, cap_shutter); 174019a81c14SSteve Longerbeam } 174119a81c14SSteve Longerbeam 174219a81c14SSteve Longerbeam /* 174319a81c14SSteve Longerbeam * if sensor changes inside scaling or subsampling 174419a81c14SSteve Longerbeam * change mode directly 174519a81c14SSteve Longerbeam */ 174619a81c14SSteve Longerbeam static int ov5640_set_mode_direct(struct ov5640_dev *sensor, 17473cca8ef5SHugues Fruchet const struct ov5640_mode_info *mode) 174819a81c14SSteve Longerbeam { 174941d8d7f5SHugues Fruchet if (!mode->reg_data) 175019a81c14SSteve Longerbeam return -EINVAL; 175119a81c14SSteve Longerbeam 175219a81c14SSteve Longerbeam /* Write capture setting */ 17533cca8ef5SHugues Fruchet return ov5640_load_regs(sensor, mode); 175419a81c14SSteve Longerbeam } 175519a81c14SSteve Longerbeam 1756985cdcb0SHugues Fruchet static int ov5640_set_mode(struct ov5640_dev *sensor) 175719a81c14SSteve Longerbeam { 175819a81c14SSteve Longerbeam const struct ov5640_mode_info *mode = sensor->current_mode; 1759985cdcb0SHugues Fruchet const struct ov5640_mode_info *orig_mode = sensor->last_mode; 176019a81c14SSteve Longerbeam enum ov5640_downsize_mode dn_mode, orig_dn_mode; 17613cca8ef5SHugues Fruchet bool auto_gain = sensor->ctrls.auto_gain->val == 1; 1762dc29a1c1SHugues Fruchet bool auto_exp = sensor->ctrls.auto_exp->val == V4L2_EXPOSURE_AUTO; 1763aa288248SMaxime Ripard unsigned long rate; 176419a81c14SSteve Longerbeam int ret; 176519a81c14SSteve Longerbeam 176619a81c14SSteve Longerbeam dn_mode = mode->dn_mode; 176719a81c14SSteve Longerbeam orig_dn_mode = orig_mode->dn_mode; 176819a81c14SSteve Longerbeam 176919a81c14SSteve Longerbeam /* auto gain and exposure must be turned off when changing modes */ 17703cca8ef5SHugues Fruchet if (auto_gain) { 17713cca8ef5SHugues Fruchet ret = ov5640_set_autogain(sensor, false); 177219a81c14SSteve Longerbeam if (ret) 177319a81c14SSteve Longerbeam return ret; 17743cca8ef5SHugues Fruchet } 1775bf4a4b51SMaxime Ripard 17763cca8ef5SHugues Fruchet if (auto_exp) { 1777dc29a1c1SHugues Fruchet ret = ov5640_set_autoexposure(sensor, false); 177819a81c14SSteve Longerbeam if (ret) 17793cca8ef5SHugues Fruchet goto restore_auto_gain; 17803cca8ef5SHugues Fruchet } 178119a81c14SSteve Longerbeam 1782aa288248SMaxime Ripard /* 1783aa288248SMaxime Ripard * All the formats we support have 16 bits per pixel, seems to require 1784aa288248SMaxime Ripard * the same rate than YUV, so we can just use 16 bpp all the time. 1785aa288248SMaxime Ripard */ 1786cc196e48SBenoit Parrot rate = ov5640_calc_pixel_rate(sensor) * 16; 1787aa288248SMaxime Ripard if (sensor->ep.bus_type == V4L2_MBUS_CSI2_DPHY) { 1788aa288248SMaxime Ripard rate = rate / sensor->ep.bus.mipi_csi2.num_data_lanes; 1789aa288248SMaxime Ripard ret = ov5640_set_mipi_pclk(sensor, rate); 1790aa288248SMaxime Ripard } else { 1791aa288248SMaxime Ripard rate = rate / sensor->ep.bus.parallel.bus_width; 1792aa288248SMaxime Ripard ret = ov5640_set_dvp_pclk(sensor, rate); 1793aa288248SMaxime Ripard } 1794aa288248SMaxime Ripard 1795aa288248SMaxime Ripard if (ret < 0) 1796aa288248SMaxime Ripard return 0; 1797aa288248SMaxime Ripard 179819a81c14SSteve Longerbeam if ((dn_mode == SUBSAMPLING && orig_dn_mode == SCALING) || 179919a81c14SSteve Longerbeam (dn_mode == SCALING && orig_dn_mode == SUBSAMPLING)) { 180019a81c14SSteve Longerbeam /* 180119a81c14SSteve Longerbeam * change between subsampling and scaling 18023cca8ef5SHugues Fruchet * go through exposure calculation 180319a81c14SSteve Longerbeam */ 180419a81c14SSteve Longerbeam ret = ov5640_set_mode_exposure_calc(sensor, mode); 180519a81c14SSteve Longerbeam } else { 180619a81c14SSteve Longerbeam /* 180719a81c14SSteve Longerbeam * change inside subsampling or scaling 180819a81c14SSteve Longerbeam * download firmware directly 180919a81c14SSteve Longerbeam */ 18103cca8ef5SHugues Fruchet ret = ov5640_set_mode_direct(sensor, mode); 181119a81c14SSteve Longerbeam } 181219a81c14SSteve Longerbeam if (ret < 0) 18133cca8ef5SHugues Fruchet goto restore_auto_exp_gain; 18143cca8ef5SHugues Fruchet 18153cca8ef5SHugues Fruchet /* restore auto gain and exposure */ 18163cca8ef5SHugues Fruchet if (auto_gain) 18173cca8ef5SHugues Fruchet ov5640_set_autogain(sensor, true); 18183cca8ef5SHugues Fruchet if (auto_exp) 18193cca8ef5SHugues Fruchet ov5640_set_autoexposure(sensor, true); 182019a81c14SSteve Longerbeam 1821ce85705aSHugues Fruchet ret = ov5640_set_binning(sensor, dn_mode != SCALING); 1822ce85705aSHugues Fruchet if (ret < 0) 1823ce85705aSHugues Fruchet return ret; 182419a81c14SSteve Longerbeam ret = ov5640_set_ae_target(sensor, sensor->ae_target); 182519a81c14SSteve Longerbeam if (ret < 0) 182619a81c14SSteve Longerbeam return ret; 182719a81c14SSteve Longerbeam ret = ov5640_get_light_freq(sensor); 182819a81c14SSteve Longerbeam if (ret < 0) 182919a81c14SSteve Longerbeam return ret; 183019a81c14SSteve Longerbeam ret = ov5640_set_bandingfilter(sensor); 183119a81c14SSteve Longerbeam if (ret < 0) 183219a81c14SSteve Longerbeam return ret; 183319a81c14SSteve Longerbeam ret = ov5640_set_virtual_channel(sensor); 183419a81c14SSteve Longerbeam if (ret < 0) 183519a81c14SSteve Longerbeam return ret; 183619a81c14SSteve Longerbeam 183719a81c14SSteve Longerbeam sensor->pending_mode_change = false; 1838985cdcb0SHugues Fruchet sensor->last_mode = mode; 183919a81c14SSteve Longerbeam 184019a81c14SSteve Longerbeam return 0; 18413cca8ef5SHugues Fruchet 18423cca8ef5SHugues Fruchet restore_auto_exp_gain: 18433cca8ef5SHugues Fruchet if (auto_exp) 18443cca8ef5SHugues Fruchet ov5640_set_autoexposure(sensor, true); 18453cca8ef5SHugues Fruchet restore_auto_gain: 18463cca8ef5SHugues Fruchet if (auto_gain) 18473cca8ef5SHugues Fruchet ov5640_set_autogain(sensor, true); 18483cca8ef5SHugues Fruchet 18493cca8ef5SHugues Fruchet return ret; 185019a81c14SSteve Longerbeam } 185119a81c14SSteve Longerbeam 185219ad26f9SAkinobu Mita static int ov5640_set_framefmt(struct ov5640_dev *sensor, 185319ad26f9SAkinobu Mita struct v4l2_mbus_framefmt *format); 185419ad26f9SAkinobu Mita 185519a81c14SSteve Longerbeam /* restore the last set video mode after chip power-on */ 185619a81c14SSteve Longerbeam static int ov5640_restore_mode(struct ov5640_dev *sensor) 185719a81c14SSteve Longerbeam { 185819a81c14SSteve Longerbeam int ret; 185919a81c14SSteve Longerbeam 186019a81c14SSteve Longerbeam /* first load the initial register values */ 186119a81c14SSteve Longerbeam ret = ov5640_load_regs(sensor, &ov5640_mode_init_data); 186219a81c14SSteve Longerbeam if (ret < 0) 186319a81c14SSteve Longerbeam return ret; 1864985cdcb0SHugues Fruchet sensor->last_mode = &ov5640_mode_init_data; 186519a81c14SSteve Longerbeam 18668f57c2f8SMaxime Ripard ret = ov5640_mod_reg(sensor, OV5640_REG_SYS_ROOT_DIVIDER, 0x3f, 18677851fe7aSMaxime Ripard (ilog2(OV5640_SCLK2X_ROOT_DIV) << 2) | 18687851fe7aSMaxime Ripard ilog2(OV5640_SCLK_ROOT_DIV)); 18698f57c2f8SMaxime Ripard if (ret) 18708f57c2f8SMaxime Ripard return ret; 18718f57c2f8SMaxime Ripard 187219a81c14SSteve Longerbeam /* now restore the last capture mode */ 1873985cdcb0SHugues Fruchet ret = ov5640_set_mode(sensor); 187419ad26f9SAkinobu Mita if (ret < 0) 187519ad26f9SAkinobu Mita return ret; 187619ad26f9SAkinobu Mita 187719ad26f9SAkinobu Mita return ov5640_set_framefmt(sensor, &sensor->fmt); 187819a81c14SSteve Longerbeam } 187919a81c14SSteve Longerbeam 188019a81c14SSteve Longerbeam static void ov5640_power(struct ov5640_dev *sensor, bool enable) 188119a81c14SSteve Longerbeam { 18821fddc5daSHugues Fruchet gpiod_set_value_cansleep(sensor->pwdn_gpio, enable ? 0 : 1); 188319a81c14SSteve Longerbeam } 188419a81c14SSteve Longerbeam 188519a81c14SSteve Longerbeam static void ov5640_reset(struct ov5640_dev *sensor) 188619a81c14SSteve Longerbeam { 188719a81c14SSteve Longerbeam if (!sensor->reset_gpio) 188819a81c14SSteve Longerbeam return; 188919a81c14SSteve Longerbeam 18901fddc5daSHugues Fruchet gpiod_set_value_cansleep(sensor->reset_gpio, 0); 189119a81c14SSteve Longerbeam 189219a81c14SSteve Longerbeam /* camera power cycle */ 189319a81c14SSteve Longerbeam ov5640_power(sensor, false); 189419a81c14SSteve Longerbeam usleep_range(5000, 10000); 189519a81c14SSteve Longerbeam ov5640_power(sensor, true); 189619a81c14SSteve Longerbeam usleep_range(5000, 10000); 189719a81c14SSteve Longerbeam 18981fddc5daSHugues Fruchet gpiod_set_value_cansleep(sensor->reset_gpio, 1); 189919a81c14SSteve Longerbeam usleep_range(1000, 2000); 190019a81c14SSteve Longerbeam 19011fddc5daSHugues Fruchet gpiod_set_value_cansleep(sensor->reset_gpio, 0); 19021d4c41f3SLoic Poulain usleep_range(20000, 25000); 190319a81c14SSteve Longerbeam } 190419a81c14SSteve Longerbeam 19050f7acb52SHugues Fruchet static int ov5640_set_power_on(struct ov5640_dev *sensor) 190619a81c14SSteve Longerbeam { 19070f7acb52SHugues Fruchet struct i2c_client *client = sensor->i2c_client; 19080f7acb52SHugues Fruchet int ret; 190919a81c14SSteve Longerbeam 19100f7acb52SHugues Fruchet ret = clk_prepare_enable(sensor->xclk); 19110f7acb52SHugues Fruchet if (ret) { 19120f7acb52SHugues Fruchet dev_err(&client->dev, "%s: failed to enable clock\n", 19130f7acb52SHugues Fruchet __func__); 19140f7acb52SHugues Fruchet return ret; 19150f7acb52SHugues Fruchet } 191619a81c14SSteve Longerbeam 191719a81c14SSteve Longerbeam ret = regulator_bulk_enable(OV5640_NUM_SUPPLIES, 191819a81c14SSteve Longerbeam sensor->supplies); 19190f7acb52SHugues Fruchet if (ret) { 19200f7acb52SHugues Fruchet dev_err(&client->dev, "%s: failed to enable regulators\n", 19210f7acb52SHugues Fruchet __func__); 192219a81c14SSteve Longerbeam goto xclk_off; 19230f7acb52SHugues Fruchet } 192419a81c14SSteve Longerbeam 192519a81c14SSteve Longerbeam ov5640_reset(sensor); 192619a81c14SSteve Longerbeam ov5640_power(sensor, true); 192719a81c14SSteve Longerbeam 192819a81c14SSteve Longerbeam ret = ov5640_init_slave_id(sensor); 192919a81c14SSteve Longerbeam if (ret) 193019a81c14SSteve Longerbeam goto power_off; 193119a81c14SSteve Longerbeam 19320f7acb52SHugues Fruchet return 0; 19330f7acb52SHugues Fruchet 19340f7acb52SHugues Fruchet power_off: 19350f7acb52SHugues Fruchet ov5640_power(sensor, false); 19360f7acb52SHugues Fruchet regulator_bulk_disable(OV5640_NUM_SUPPLIES, sensor->supplies); 19370f7acb52SHugues Fruchet xclk_off: 19380f7acb52SHugues Fruchet clk_disable_unprepare(sensor->xclk); 19390f7acb52SHugues Fruchet return ret; 19400f7acb52SHugues Fruchet } 19410f7acb52SHugues Fruchet 19420f7acb52SHugues Fruchet static void ov5640_set_power_off(struct ov5640_dev *sensor) 19430f7acb52SHugues Fruchet { 19440f7acb52SHugues Fruchet ov5640_power(sensor, false); 19450f7acb52SHugues Fruchet regulator_bulk_disable(OV5640_NUM_SUPPLIES, sensor->supplies); 19460f7acb52SHugues Fruchet clk_disable_unprepare(sensor->xclk); 19470f7acb52SHugues Fruchet } 19480f7acb52SHugues Fruchet 1949b1751ae6SLad Prabhakar static int ov5640_set_power_mipi(struct ov5640_dev *sensor, bool on) 1950b1751ae6SLad Prabhakar { 1951b1751ae6SLad Prabhakar int ret; 1952b1751ae6SLad Prabhakar 1953b1751ae6SLad Prabhakar if (!on) { 1954b1751ae6SLad Prabhakar /* Reset MIPI bus settings to their default values. */ 1955b1751ae6SLad Prabhakar ov5640_write_reg(sensor, OV5640_REG_IO_MIPI_CTRL00, 0x58); 1956b1751ae6SLad Prabhakar ov5640_write_reg(sensor, OV5640_REG_MIPI_CTRL00, 0x04); 1957b1751ae6SLad Prabhakar ov5640_write_reg(sensor, OV5640_REG_PAD_OUTPUT00, 0x00); 1958b1751ae6SLad Prabhakar return 0; 1959b1751ae6SLad Prabhakar } 1960b1751ae6SLad Prabhakar 1961b1751ae6SLad Prabhakar /* 1962b1751ae6SLad Prabhakar * Power up MIPI HS Tx and LS Rx; 2 data lanes mode 1963b1751ae6SLad Prabhakar * 1964b1751ae6SLad Prabhakar * 0x300e = 0x40 1965b1751ae6SLad Prabhakar * [7:5] = 010 : 2 data lanes mode (see FIXME note in 1966b1751ae6SLad Prabhakar * "ov5640_set_stream_mipi()") 1967b1751ae6SLad Prabhakar * [4] = 0 : Power up MIPI HS Tx 1968b1751ae6SLad Prabhakar * [3] = 0 : Power up MIPI LS Rx 1969b1751ae6SLad Prabhakar * [2] = 0 : MIPI interface disabled 1970b1751ae6SLad Prabhakar */ 1971b1751ae6SLad Prabhakar ret = ov5640_write_reg(sensor, OV5640_REG_IO_MIPI_CTRL00, 0x40); 1972b1751ae6SLad Prabhakar if (ret) 1973b1751ae6SLad Prabhakar return ret; 1974b1751ae6SLad Prabhakar 1975b1751ae6SLad Prabhakar /* 1976b1751ae6SLad Prabhakar * Gate clock and set LP11 in 'no packets mode' (idle) 1977b1751ae6SLad Prabhakar * 1978b1751ae6SLad Prabhakar * 0x4800 = 0x24 1979b1751ae6SLad Prabhakar * [5] = 1 : Gate clock when 'no packets' 1980b1751ae6SLad Prabhakar * [2] = 1 : MIPI bus in LP11 when 'no packets' 1981b1751ae6SLad Prabhakar */ 1982b1751ae6SLad Prabhakar ret = ov5640_write_reg(sensor, OV5640_REG_MIPI_CTRL00, 0x24); 1983b1751ae6SLad Prabhakar if (ret) 1984b1751ae6SLad Prabhakar return ret; 1985b1751ae6SLad Prabhakar 1986b1751ae6SLad Prabhakar /* 1987b1751ae6SLad Prabhakar * Set data lanes and clock in LP11 when 'sleeping' 1988b1751ae6SLad Prabhakar * 1989b1751ae6SLad Prabhakar * 0x3019 = 0x70 1990b1751ae6SLad Prabhakar * [6] = 1 : MIPI data lane 2 in LP11 when 'sleeping' 1991b1751ae6SLad Prabhakar * [5] = 1 : MIPI data lane 1 in LP11 when 'sleeping' 1992b1751ae6SLad Prabhakar * [4] = 1 : MIPI clock lane in LP11 when 'sleeping' 1993b1751ae6SLad Prabhakar */ 1994b1751ae6SLad Prabhakar ret = ov5640_write_reg(sensor, OV5640_REG_PAD_OUTPUT00, 0x70); 1995b1751ae6SLad Prabhakar if (ret) 1996b1751ae6SLad Prabhakar return ret; 1997b1751ae6SLad Prabhakar 1998b1751ae6SLad Prabhakar /* Give lanes some time to coax into LP11 state. */ 1999b1751ae6SLad Prabhakar usleep_range(500, 1000); 2000b1751ae6SLad Prabhakar 2001b1751ae6SLad Prabhakar return 0; 2002b1751ae6SLad Prabhakar } 2003b1751ae6SLad Prabhakar 2004576f5d4bSLad Prabhakar static int ov5640_set_power_dvp(struct ov5640_dev *sensor, bool on) 2005576f5d4bSLad Prabhakar { 2006311a6408SLad Prabhakar unsigned int flags = sensor->ep.bus.parallel.flags; 2007*68579b32SHugues Fruchet bool bt656 = sensor->ep.bus_type == V4L2_MBUS_BT656; 2008*68579b32SHugues Fruchet u8 polarities = 0; 2009576f5d4bSLad Prabhakar int ret; 2010576f5d4bSLad Prabhakar 2011576f5d4bSLad Prabhakar if (!on) { 2012576f5d4bSLad Prabhakar /* Reset settings to their default values. */ 2013*68579b32SHugues Fruchet ov5640_write_reg(sensor, OV5640_REG_CCIR656_CTRL00, 0x00); 2014311a6408SLad Prabhakar ov5640_write_reg(sensor, OV5640_REG_IO_MIPI_CTRL00, 0x58); 2015311a6408SLad Prabhakar ov5640_write_reg(sensor, OV5640_REG_POLARITY_CTRL00, 0x20); 2016576f5d4bSLad Prabhakar ov5640_write_reg(sensor, OV5640_REG_PAD_OUTPUT_ENABLE01, 0x00); 2017576f5d4bSLad Prabhakar ov5640_write_reg(sensor, OV5640_REG_PAD_OUTPUT_ENABLE02, 0x00); 2018576f5d4bSLad Prabhakar return 0; 2019576f5d4bSLad Prabhakar } 2020576f5d4bSLad Prabhakar 2021576f5d4bSLad Prabhakar /* 2022311a6408SLad Prabhakar * Note about parallel port configuration. 2023311a6408SLad Prabhakar * 2024311a6408SLad Prabhakar * When configured in parallel mode, the OV5640 will 2025311a6408SLad Prabhakar * output 10 bits data on DVP data lines [9:0]. 2026311a6408SLad Prabhakar * If only 8 bits data are wanted, the 8 bits data lines 2027311a6408SLad Prabhakar * of the camera interface must be physically connected 2028311a6408SLad Prabhakar * on the DVP data lines [9:2]. 2029311a6408SLad Prabhakar * 2030311a6408SLad Prabhakar * Control lines polarity can be configured through 2031311a6408SLad Prabhakar * devicetree endpoint control lines properties. 2032311a6408SLad Prabhakar * If no endpoint control lines properties are set, 2033311a6408SLad Prabhakar * polarity will be as below: 2034311a6408SLad Prabhakar * - VSYNC: active high 2035311a6408SLad Prabhakar * - HREF: active low 2036311a6408SLad Prabhakar * - PCLK: active low 2037*68579b32SHugues Fruchet * 2038*68579b32SHugues Fruchet * VSYNC & HREF are not configured if BT656 bus mode is selected 2039311a6408SLad Prabhakar */ 2040*68579b32SHugues Fruchet 2041*68579b32SHugues Fruchet /* 2042*68579b32SHugues Fruchet * BT656 embedded synchronization configuration 2043*68579b32SHugues Fruchet * 2044*68579b32SHugues Fruchet * CCIR656 CTRL00 2045*68579b32SHugues Fruchet * - [7]: SYNC code selection (0: auto generate sync code, 2046*68579b32SHugues Fruchet * 1: sync code from regs 0x4732-0x4735) 2047*68579b32SHugues Fruchet * - [6]: f value in CCIR656 SYNC code when fixed f value 2048*68579b32SHugues Fruchet * - [5]: Fixed f value 2049*68579b32SHugues Fruchet * - [4:3]: Blank toggle data options (00: data=1'h040/1'h200, 2050*68579b32SHugues Fruchet * 01: data from regs 0x4736-0x4738, 10: always keep 0) 2051*68579b32SHugues Fruchet * - [1]: Clip data disable 2052*68579b32SHugues Fruchet * - [0]: CCIR656 mode enable 2053*68579b32SHugues Fruchet * 2054*68579b32SHugues Fruchet * Default CCIR656 SAV/EAV mode with default codes 2055*68579b32SHugues Fruchet * SAV=0xff000080 & EAV=0xff00009d is enabled here with settings: 2056*68579b32SHugues Fruchet * - CCIR656 mode enable 2057*68579b32SHugues Fruchet * - auto generation of sync codes 2058*68579b32SHugues Fruchet * - blank toggle data 1'h040/1'h200 2059*68579b32SHugues Fruchet * - clip reserved data (0x00 & 0xff changed to 0x01 & 0xfe) 2060*68579b32SHugues Fruchet */ 2061*68579b32SHugues Fruchet ret = ov5640_write_reg(sensor, OV5640_REG_CCIR656_CTRL00, 2062*68579b32SHugues Fruchet bt656 ? 0x01 : 0x00); 2063*68579b32SHugues Fruchet if (ret) 2064*68579b32SHugues Fruchet return ret; 2065*68579b32SHugues Fruchet 2066311a6408SLad Prabhakar /* 2067311a6408SLad Prabhakar * configure parallel port control lines polarity 2068311a6408SLad Prabhakar * 2069311a6408SLad Prabhakar * POLARITY CTRL0 2070311a6408SLad Prabhakar * - [5]: PCLK polarity (0: active low, 1: active high) 2071311a6408SLad Prabhakar * - [1]: HREF polarity (0: active low, 1: active high) 2072311a6408SLad Prabhakar * - [0]: VSYNC polarity (mismatch here between 2073311a6408SLad Prabhakar * datasheet and hardware, 0 is active high 2074311a6408SLad Prabhakar * and 1 is active low...) 2075311a6408SLad Prabhakar */ 2076*68579b32SHugues Fruchet if (!bt656) { 2077311a6408SLad Prabhakar if (flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH) 2078*68579b32SHugues Fruchet polarities |= BIT(1); 2079311a6408SLad Prabhakar if (flags & V4L2_MBUS_VSYNC_ACTIVE_LOW) 2080*68579b32SHugues Fruchet polarities |= BIT(0); 2081*68579b32SHugues Fruchet } 2082*68579b32SHugues Fruchet if (flags & V4L2_MBUS_PCLK_SAMPLE_RISING) 2083*68579b32SHugues Fruchet polarities |= BIT(5); 2084311a6408SLad Prabhakar 2085*68579b32SHugues Fruchet ret = ov5640_write_reg(sensor, OV5640_REG_POLARITY_CTRL00, polarities); 2086311a6408SLad Prabhakar if (ret) 2087311a6408SLad Prabhakar return ret; 2088311a6408SLad Prabhakar 2089311a6408SLad Prabhakar /* 2090*68579b32SHugues Fruchet * powerdown MIPI TX/RX PHY & enable DVP 2091311a6408SLad Prabhakar * 2092311a6408SLad Prabhakar * MIPI CONTROL 00 2093*68579b32SHugues Fruchet * [4] = 1 : Power down MIPI HS Tx 2094*68579b32SHugues Fruchet * [3] = 1 : Power down MIPI LS Rx 2095*68579b32SHugues Fruchet * [2] = 0 : DVP enable (MIPI disable) 2096311a6408SLad Prabhakar */ 2097311a6408SLad Prabhakar ret = ov5640_write_reg(sensor, OV5640_REG_IO_MIPI_CTRL00, 0x18); 2098311a6408SLad Prabhakar if (ret) 2099311a6408SLad Prabhakar return ret; 2100311a6408SLad Prabhakar 2101311a6408SLad Prabhakar /* 2102576f5d4bSLad Prabhakar * enable VSYNC/HREF/PCLK DVP control lines 2103576f5d4bSLad Prabhakar * & D[9:6] DVP data lines 2104576f5d4bSLad Prabhakar * 2105576f5d4bSLad Prabhakar * PAD OUTPUT ENABLE 01 2106576f5d4bSLad Prabhakar * - 6: VSYNC output enable 2107576f5d4bSLad Prabhakar * - 5: HREF output enable 2108576f5d4bSLad Prabhakar * - 4: PCLK output enable 2109576f5d4bSLad Prabhakar * - [3:0]: D[9:6] output enable 2110576f5d4bSLad Prabhakar */ 21114039b037SLad Prabhakar ret = ov5640_write_reg(sensor, OV5640_REG_PAD_OUTPUT_ENABLE01, 2112*68579b32SHugues Fruchet bt656 ? 0x1f : 0x7f); 2113576f5d4bSLad Prabhakar if (ret) 2114576f5d4bSLad Prabhakar return ret; 2115576f5d4bSLad Prabhakar 2116576f5d4bSLad Prabhakar /* 2117576f5d4bSLad Prabhakar * enable D[5:0] DVP data lines 2118576f5d4bSLad Prabhakar * 2119576f5d4bSLad Prabhakar * PAD OUTPUT ENABLE 02 2120576f5d4bSLad Prabhakar * - [7:2]: D[5:0] output enable 2121576f5d4bSLad Prabhakar */ 2122576f5d4bSLad Prabhakar return ov5640_write_reg(sensor, OV5640_REG_PAD_OUTPUT_ENABLE02, 0xfc); 2123576f5d4bSLad Prabhakar } 2124576f5d4bSLad Prabhakar 21250f7acb52SHugues Fruchet static int ov5640_set_power(struct ov5640_dev *sensor, bool on) 21260f7acb52SHugues Fruchet { 21270f7acb52SHugues Fruchet int ret = 0; 21280f7acb52SHugues Fruchet 21290f7acb52SHugues Fruchet if (on) { 21300f7acb52SHugues Fruchet ret = ov5640_set_power_on(sensor); 21310f7acb52SHugues Fruchet if (ret) 21320f7acb52SHugues Fruchet return ret; 21330f7acb52SHugues Fruchet 213419a81c14SSteve Longerbeam ret = ov5640_restore_mode(sensor); 213519a81c14SSteve Longerbeam if (ret) 213619a81c14SSteve Longerbeam goto power_off; 2137b1751ae6SLad Prabhakar } 213819a81c14SSteve Longerbeam 2139576f5d4bSLad Prabhakar if (sensor->ep.bus_type == V4L2_MBUS_CSI2_DPHY) 2140b1751ae6SLad Prabhakar ret = ov5640_set_power_mipi(sensor, on); 2141576f5d4bSLad Prabhakar else 2142576f5d4bSLad Prabhakar ret = ov5640_set_power_dvp(sensor, on); 2143b1751ae6SLad Prabhakar if (ret) 2144b1751ae6SLad Prabhakar goto power_off; 2145aa4bb8b8SJacopo Mondi 2146b1751ae6SLad Prabhakar if (!on) 2147aa4bb8b8SJacopo Mondi ov5640_set_power_off(sensor); 214819a81c14SSteve Longerbeam 214919a81c14SSteve Longerbeam return 0; 215019a81c14SSteve Longerbeam 215119a81c14SSteve Longerbeam power_off: 21520f7acb52SHugues Fruchet ov5640_set_power_off(sensor); 215319a81c14SSteve Longerbeam return ret; 215419a81c14SSteve Longerbeam } 215519a81c14SSteve Longerbeam 215619a81c14SSteve Longerbeam /* --------------- Subdev Operations --------------- */ 215719a81c14SSteve Longerbeam 215819a81c14SSteve Longerbeam static int ov5640_s_power(struct v4l2_subdev *sd, int on) 215919a81c14SSteve Longerbeam { 216019a81c14SSteve Longerbeam struct ov5640_dev *sensor = to_ov5640_dev(sd); 216119a81c14SSteve Longerbeam int ret = 0; 216219a81c14SSteve Longerbeam 216319a81c14SSteve Longerbeam mutex_lock(&sensor->lock); 216419a81c14SSteve Longerbeam 216519a81c14SSteve Longerbeam /* 216619a81c14SSteve Longerbeam * If the power count is modified from 0 to != 0 or from != 0 to 0, 216719a81c14SSteve Longerbeam * update the power state. 216819a81c14SSteve Longerbeam */ 216919a81c14SSteve Longerbeam if (sensor->power_count == !on) { 217019a81c14SSteve Longerbeam ret = ov5640_set_power(sensor, !!on); 217119a81c14SSteve Longerbeam if (ret) 217219a81c14SSteve Longerbeam goto out; 217319a81c14SSteve Longerbeam } 217419a81c14SSteve Longerbeam 217519a81c14SSteve Longerbeam /* Update the power count. */ 217619a81c14SSteve Longerbeam sensor->power_count += on ? 1 : -1; 217719a81c14SSteve Longerbeam WARN_ON(sensor->power_count < 0); 217819a81c14SSteve Longerbeam out: 217919a81c14SSteve Longerbeam mutex_unlock(&sensor->lock); 218019a81c14SSteve Longerbeam 218119a81c14SSteve Longerbeam if (on && !ret && sensor->power_count == 1) { 218219a81c14SSteve Longerbeam /* restore controls */ 218319a81c14SSteve Longerbeam ret = v4l2_ctrl_handler_setup(&sensor->ctrls.handler); 218419a81c14SSteve Longerbeam } 218519a81c14SSteve Longerbeam 218619a81c14SSteve Longerbeam return ret; 218719a81c14SSteve Longerbeam } 218819a81c14SSteve Longerbeam 218919a81c14SSteve Longerbeam static int ov5640_try_frame_interval(struct ov5640_dev *sensor, 219019a81c14SSteve Longerbeam struct v4l2_fract *fi, 219119a81c14SSteve Longerbeam u32 width, u32 height) 219219a81c14SSteve Longerbeam { 219319a81c14SSteve Longerbeam const struct ov5640_mode_info *mode; 21946530a5ebSJagan Teki enum ov5640_frame_rate rate = OV5640_15_FPS; 2195f6cc192fSMaxime Ripard int minfps, maxfps, best_fps, fps; 2196f6cc192fSMaxime Ripard int i; 219719a81c14SSteve Longerbeam 219819a81c14SSteve Longerbeam minfps = ov5640_framerates[OV5640_15_FPS]; 2199e823fb16SMaxime Ripard maxfps = ov5640_framerates[OV5640_60_FPS]; 220019a81c14SSteve Longerbeam 220119a81c14SSteve Longerbeam if (fi->numerator == 0) { 220219a81c14SSteve Longerbeam fi->denominator = maxfps; 220319a81c14SSteve Longerbeam fi->numerator = 1; 2204e823fb16SMaxime Ripard rate = OV5640_60_FPS; 2205e823fb16SMaxime Ripard goto find_mode; 220619a81c14SSteve Longerbeam } 220719a81c14SSteve Longerbeam 2208f6cc192fSMaxime Ripard fps = clamp_val(DIV_ROUND_CLOSEST(fi->denominator, fi->numerator), 2209f6cc192fSMaxime Ripard minfps, maxfps); 2210f6cc192fSMaxime Ripard 2211f6cc192fSMaxime Ripard best_fps = minfps; 2212f6cc192fSMaxime Ripard for (i = 0; i < ARRAY_SIZE(ov5640_framerates); i++) { 2213f6cc192fSMaxime Ripard int curr_fps = ov5640_framerates[i]; 2214f6cc192fSMaxime Ripard 2215f6cc192fSMaxime Ripard if (abs(curr_fps - fps) < abs(best_fps - fps)) { 2216f6cc192fSMaxime Ripard best_fps = curr_fps; 2217f6cc192fSMaxime Ripard rate = i; 2218f6cc192fSMaxime Ripard } 2219f6cc192fSMaxime Ripard } 222019a81c14SSteve Longerbeam 222119a81c14SSteve Longerbeam fi->numerator = 1; 2222f6cc192fSMaxime Ripard fi->denominator = best_fps; 222319a81c14SSteve Longerbeam 2224e823fb16SMaxime Ripard find_mode: 22255a3ad937SMaxime Ripard mode = ov5640_find_mode(sensor, rate, width, height, false); 22265a3ad937SMaxime Ripard return mode ? rate : -EINVAL; 222719a81c14SSteve Longerbeam } 222819a81c14SSteve Longerbeam 222919a81c14SSteve Longerbeam static int ov5640_get_fmt(struct v4l2_subdev *sd, 223019a81c14SSteve Longerbeam struct v4l2_subdev_pad_config *cfg, 223119a81c14SSteve Longerbeam struct v4l2_subdev_format *format) 223219a81c14SSteve Longerbeam { 223319a81c14SSteve Longerbeam struct ov5640_dev *sensor = to_ov5640_dev(sd); 223419a81c14SSteve Longerbeam struct v4l2_mbus_framefmt *fmt; 223519a81c14SSteve Longerbeam 223619a81c14SSteve Longerbeam if (format->pad != 0) 223719a81c14SSteve Longerbeam return -EINVAL; 223819a81c14SSteve Longerbeam 223919a81c14SSteve Longerbeam mutex_lock(&sensor->lock); 224019a81c14SSteve Longerbeam 224119a81c14SSteve Longerbeam if (format->which == V4L2_SUBDEV_FORMAT_TRY) 224219a81c14SSteve Longerbeam fmt = v4l2_subdev_get_try_format(&sensor->sd, cfg, 224319a81c14SSteve Longerbeam format->pad); 224419a81c14SSteve Longerbeam else 224519a81c14SSteve Longerbeam fmt = &sensor->fmt; 224619a81c14SSteve Longerbeam 224719a81c14SSteve Longerbeam format->format = *fmt; 224819a81c14SSteve Longerbeam 224919a81c14SSteve Longerbeam mutex_unlock(&sensor->lock); 225019a81c14SSteve Longerbeam 225119a81c14SSteve Longerbeam return 0; 225219a81c14SSteve Longerbeam } 225319a81c14SSteve Longerbeam 225419a81c14SSteve Longerbeam static int ov5640_try_fmt_internal(struct v4l2_subdev *sd, 225519a81c14SSteve Longerbeam struct v4l2_mbus_framefmt *fmt, 225619a81c14SSteve Longerbeam enum ov5640_frame_rate fr, 225719a81c14SSteve Longerbeam const struct ov5640_mode_info **new_mode) 225819a81c14SSteve Longerbeam { 225919a81c14SSteve Longerbeam struct ov5640_dev *sensor = to_ov5640_dev(sd); 226019a81c14SSteve Longerbeam const struct ov5640_mode_info *mode; 2261e3ee691dSHugues Fruchet int i; 226219a81c14SSteve Longerbeam 226319a81c14SSteve Longerbeam mode = ov5640_find_mode(sensor, fr, fmt->width, fmt->height, true); 226419a81c14SSteve Longerbeam if (!mode) 226519a81c14SSteve Longerbeam return -EINVAL; 2266dba13a0bSMaxime Ripard fmt->width = mode->hact; 2267dba13a0bSMaxime Ripard fmt->height = mode->vact; 226819a81c14SSteve Longerbeam 226919a81c14SSteve Longerbeam if (new_mode) 227019a81c14SSteve Longerbeam *new_mode = mode; 2271e3ee691dSHugues Fruchet 2272e3ee691dSHugues Fruchet for (i = 0; i < ARRAY_SIZE(ov5640_formats); i++) 2273e3ee691dSHugues Fruchet if (ov5640_formats[i].code == fmt->code) 2274e3ee691dSHugues Fruchet break; 2275e3ee691dSHugues Fruchet if (i >= ARRAY_SIZE(ov5640_formats)) 2276e6441fdeSHugues Fruchet i = 0; 2277e6441fdeSHugues Fruchet 2278e6441fdeSHugues Fruchet fmt->code = ov5640_formats[i].code; 2279e6441fdeSHugues Fruchet fmt->colorspace = ov5640_formats[i].colorspace; 2280e6441fdeSHugues Fruchet fmt->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(fmt->colorspace); 2281e6441fdeSHugues Fruchet fmt->quantization = V4L2_QUANTIZATION_FULL_RANGE; 2282e6441fdeSHugues Fruchet fmt->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(fmt->colorspace); 2283e3ee691dSHugues Fruchet 228419a81c14SSteve Longerbeam return 0; 228519a81c14SSteve Longerbeam } 228619a81c14SSteve Longerbeam 228719a81c14SSteve Longerbeam static int ov5640_set_fmt(struct v4l2_subdev *sd, 228819a81c14SSteve Longerbeam struct v4l2_subdev_pad_config *cfg, 228919a81c14SSteve Longerbeam struct v4l2_subdev_format *format) 229019a81c14SSteve Longerbeam { 229119a81c14SSteve Longerbeam struct ov5640_dev *sensor = to_ov5640_dev(sd); 229219a81c14SSteve Longerbeam const struct ov5640_mode_info *new_mode; 2293e6441fdeSHugues Fruchet struct v4l2_mbus_framefmt *mbus_fmt = &format->format; 229407115449SJacopo Mondi struct v4l2_mbus_framefmt *fmt; 229519a81c14SSteve Longerbeam int ret; 229619a81c14SSteve Longerbeam 229719a81c14SSteve Longerbeam if (format->pad != 0) 229819a81c14SSteve Longerbeam return -EINVAL; 229919a81c14SSteve Longerbeam 230019a81c14SSteve Longerbeam mutex_lock(&sensor->lock); 230119a81c14SSteve Longerbeam 230219a81c14SSteve Longerbeam if (sensor->streaming) { 230319a81c14SSteve Longerbeam ret = -EBUSY; 230419a81c14SSteve Longerbeam goto out; 230519a81c14SSteve Longerbeam } 230619a81c14SSteve Longerbeam 2307e6441fdeSHugues Fruchet ret = ov5640_try_fmt_internal(sd, mbus_fmt, 230819a81c14SSteve Longerbeam sensor->current_fr, &new_mode); 230919a81c14SSteve Longerbeam if (ret) 231019a81c14SSteve Longerbeam goto out; 231119a81c14SSteve Longerbeam 231207115449SJacopo Mondi if (format->which == V4L2_SUBDEV_FORMAT_TRY) 231307115449SJacopo Mondi fmt = v4l2_subdev_get_try_format(sd, cfg, 0); 231407115449SJacopo Mondi else 231507115449SJacopo Mondi fmt = &sensor->fmt; 231619a81c14SSteve Longerbeam 2317e6441fdeSHugues Fruchet *fmt = *mbus_fmt; 231819a81c14SSteve Longerbeam 23196949d864SHugues Fruchet if (new_mode != sensor->current_mode) { 232019a81c14SSteve Longerbeam sensor->current_mode = new_mode; 232119a81c14SSteve Longerbeam sensor->pending_mode_change = true; 23226949d864SHugues Fruchet } 232307115449SJacopo Mondi if (mbus_fmt->code != sensor->fmt.code) 2324fb98e29fSHugues Fruchet sensor->pending_fmt_change = true; 232507115449SJacopo Mondi 2326cc196e48SBenoit Parrot __v4l2_ctrl_s_ctrl_int64(sensor->ctrls.pixel_rate, 2327cc196e48SBenoit Parrot ov5640_calc_pixel_rate(sensor)); 232819a81c14SSteve Longerbeam out: 232919a81c14SSteve Longerbeam mutex_unlock(&sensor->lock); 233019a81c14SSteve Longerbeam return ret; 233119a81c14SSteve Longerbeam } 233219a81c14SSteve Longerbeam 2333e3ee691dSHugues Fruchet static int ov5640_set_framefmt(struct ov5640_dev *sensor, 2334e3ee691dSHugues Fruchet struct v4l2_mbus_framefmt *format) 2335e3ee691dSHugues Fruchet { 2336e3ee691dSHugues Fruchet int ret = 0; 2337d47c4126SHugues Fruchet bool is_jpeg = false; 2338b7ed3abdSLoic Poulain u8 fmt, mux; 2339e3ee691dSHugues Fruchet 2340e3ee691dSHugues Fruchet switch (format->code) { 2341e3ee691dSHugues Fruchet case MEDIA_BUS_FMT_UYVY8_2X8: 2342e3ee691dSHugues Fruchet /* YUV422, UYVY */ 2343b7ed3abdSLoic Poulain fmt = 0x3f; 2344b7ed3abdSLoic Poulain mux = OV5640_FMT_MUX_YUV422; 2345e3ee691dSHugues Fruchet break; 2346e3ee691dSHugues Fruchet case MEDIA_BUS_FMT_YUYV8_2X8: 2347e3ee691dSHugues Fruchet /* YUV422, YUYV */ 2348b7ed3abdSLoic Poulain fmt = 0x30; 2349b7ed3abdSLoic Poulain mux = OV5640_FMT_MUX_YUV422; 2350e3ee691dSHugues Fruchet break; 2351e3ee691dSHugues Fruchet case MEDIA_BUS_FMT_RGB565_2X8_LE: 2352e3ee691dSHugues Fruchet /* RGB565 {g[2:0],b[4:0]},{r[4:0],g[5:3]} */ 2353b7ed3abdSLoic Poulain fmt = 0x6F; 2354b7ed3abdSLoic Poulain mux = OV5640_FMT_MUX_RGB; 2355e3ee691dSHugues Fruchet break; 2356e3ee691dSHugues Fruchet case MEDIA_BUS_FMT_RGB565_2X8_BE: 2357e3ee691dSHugues Fruchet /* RGB565 {r[4:0],g[5:3]},{g[2:0],b[4:0]} */ 2358b7ed3abdSLoic Poulain fmt = 0x61; 2359b7ed3abdSLoic Poulain mux = OV5640_FMT_MUX_RGB; 2360e3ee691dSHugues Fruchet break; 2361d47c4126SHugues Fruchet case MEDIA_BUS_FMT_JPEG_1X8: 2362d47c4126SHugues Fruchet /* YUV422, YUYV */ 2363b7ed3abdSLoic Poulain fmt = 0x30; 2364b7ed3abdSLoic Poulain mux = OV5640_FMT_MUX_YUV422; 2365d47c4126SHugues Fruchet is_jpeg = true; 2366d47c4126SHugues Fruchet break; 2367b7ed3abdSLoic Poulain case MEDIA_BUS_FMT_SBGGR8_1X8: 2368b7ed3abdSLoic Poulain /* Raw, BGBG... / GRGR... */ 2369b7ed3abdSLoic Poulain fmt = 0x00; 2370b7ed3abdSLoic Poulain mux = OV5640_FMT_MUX_RAW_DPC; 2371b7ed3abdSLoic Poulain break; 2372b7ed3abdSLoic Poulain case MEDIA_BUS_FMT_SGBRG8_1X8: 2373b7ed3abdSLoic Poulain /* Raw bayer, GBGB... / RGRG... */ 2374b7ed3abdSLoic Poulain fmt = 0x01; 2375b7ed3abdSLoic Poulain mux = OV5640_FMT_MUX_RAW_DPC; 2376b7ed3abdSLoic Poulain break; 2377b7ed3abdSLoic Poulain case MEDIA_BUS_FMT_SGRBG8_1X8: 2378b7ed3abdSLoic Poulain /* Raw bayer, GRGR... / BGBG... */ 2379b7ed3abdSLoic Poulain fmt = 0x02; 2380b7ed3abdSLoic Poulain mux = OV5640_FMT_MUX_RAW_DPC; 2381b7ed3abdSLoic Poulain break; 2382b7ed3abdSLoic Poulain case MEDIA_BUS_FMT_SRGGB8_1X8: 2383b7ed3abdSLoic Poulain /* Raw bayer, RGRG... / GBGB... */ 2384b7ed3abdSLoic Poulain fmt = 0x03; 2385b7ed3abdSLoic Poulain mux = OV5640_FMT_MUX_RAW_DPC; 2386b7ed3abdSLoic Poulain break; 2387e3ee691dSHugues Fruchet default: 2388e3ee691dSHugues Fruchet return -EINVAL; 2389e3ee691dSHugues Fruchet } 2390e3ee691dSHugues Fruchet 2391e3ee691dSHugues Fruchet /* FORMAT CONTROL00: YUV and RGB formatting */ 2392b7ed3abdSLoic Poulain ret = ov5640_write_reg(sensor, OV5640_REG_FORMAT_CONTROL00, fmt); 2393e3ee691dSHugues Fruchet if (ret) 2394e3ee691dSHugues Fruchet return ret; 2395e3ee691dSHugues Fruchet 2396e3ee691dSHugues Fruchet /* FORMAT MUX CONTROL: ISP YUV or RGB */ 2397b7ed3abdSLoic Poulain ret = ov5640_write_reg(sensor, OV5640_REG_ISP_FORMAT_MUX_CTRL, mux); 2398d47c4126SHugues Fruchet if (ret) 2399d47c4126SHugues Fruchet return ret; 2400d47c4126SHugues Fruchet 2401d47c4126SHugues Fruchet /* 2402d47c4126SHugues Fruchet * TIMING TC REG21: 2403d47c4126SHugues Fruchet * - [5]: JPEG enable 2404d47c4126SHugues Fruchet */ 2405d47c4126SHugues Fruchet ret = ov5640_mod_reg(sensor, OV5640_REG_TIMING_TC_REG21, 2406d47c4126SHugues Fruchet BIT(5), is_jpeg ? BIT(5) : 0); 2407d47c4126SHugues Fruchet if (ret) 2408d47c4126SHugues Fruchet return ret; 2409d47c4126SHugues Fruchet 2410d47c4126SHugues Fruchet /* 2411d47c4126SHugues Fruchet * SYSTEM RESET02: 2412d47c4126SHugues Fruchet * - [4]: Reset JFIFO 2413d47c4126SHugues Fruchet * - [3]: Reset SFIFO 2414d47c4126SHugues Fruchet * - [2]: Reset JPEG 2415d47c4126SHugues Fruchet */ 2416d47c4126SHugues Fruchet ret = ov5640_mod_reg(sensor, OV5640_REG_SYS_RESET02, 2417d47c4126SHugues Fruchet BIT(4) | BIT(3) | BIT(2), 2418d47c4126SHugues Fruchet is_jpeg ? 0 : (BIT(4) | BIT(3) | BIT(2))); 2419d47c4126SHugues Fruchet if (ret) 2420d47c4126SHugues Fruchet return ret; 2421d47c4126SHugues Fruchet 2422d47c4126SHugues Fruchet /* 2423d47c4126SHugues Fruchet * CLOCK ENABLE02: 2424d47c4126SHugues Fruchet * - [5]: Enable JPEG 2x clock 2425d47c4126SHugues Fruchet * - [3]: Enable JPEG clock 2426d47c4126SHugues Fruchet */ 2427d47c4126SHugues Fruchet return ov5640_mod_reg(sensor, OV5640_REG_SYS_CLOCK_ENABLE02, 2428d47c4126SHugues Fruchet BIT(5) | BIT(3), 2429d47c4126SHugues Fruchet is_jpeg ? (BIT(5) | BIT(3)) : 0); 2430e3ee691dSHugues Fruchet } 243119a81c14SSteve Longerbeam 243219a81c14SSteve Longerbeam /* 243319a81c14SSteve Longerbeam * Sensor Controls. 243419a81c14SSteve Longerbeam */ 243519a81c14SSteve Longerbeam 243619a81c14SSteve Longerbeam static int ov5640_set_ctrl_hue(struct ov5640_dev *sensor, int value) 243719a81c14SSteve Longerbeam { 243819a81c14SSteve Longerbeam int ret; 243919a81c14SSteve Longerbeam 244019a81c14SSteve Longerbeam if (value) { 244119a81c14SSteve Longerbeam ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0, 244219a81c14SSteve Longerbeam BIT(0), BIT(0)); 244319a81c14SSteve Longerbeam if (ret) 244419a81c14SSteve Longerbeam return ret; 244519a81c14SSteve Longerbeam ret = ov5640_write_reg16(sensor, OV5640_REG_SDE_CTRL1, value); 244619a81c14SSteve Longerbeam } else { 244719a81c14SSteve Longerbeam ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0, BIT(0), 0); 244819a81c14SSteve Longerbeam } 244919a81c14SSteve Longerbeam 245019a81c14SSteve Longerbeam return ret; 245119a81c14SSteve Longerbeam } 245219a81c14SSteve Longerbeam 245319a81c14SSteve Longerbeam static int ov5640_set_ctrl_contrast(struct ov5640_dev *sensor, int value) 245419a81c14SSteve Longerbeam { 245519a81c14SSteve Longerbeam int ret; 245619a81c14SSteve Longerbeam 245719a81c14SSteve Longerbeam if (value) { 245819a81c14SSteve Longerbeam ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0, 245919a81c14SSteve Longerbeam BIT(2), BIT(2)); 246019a81c14SSteve Longerbeam if (ret) 246119a81c14SSteve Longerbeam return ret; 246219a81c14SSteve Longerbeam ret = ov5640_write_reg(sensor, OV5640_REG_SDE_CTRL5, 246319a81c14SSteve Longerbeam value & 0xff); 246419a81c14SSteve Longerbeam } else { 246519a81c14SSteve Longerbeam ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0, BIT(2), 0); 246619a81c14SSteve Longerbeam } 246719a81c14SSteve Longerbeam 246819a81c14SSteve Longerbeam return ret; 246919a81c14SSteve Longerbeam } 247019a81c14SSteve Longerbeam 247119a81c14SSteve Longerbeam static int ov5640_set_ctrl_saturation(struct ov5640_dev *sensor, int value) 247219a81c14SSteve Longerbeam { 247319a81c14SSteve Longerbeam int ret; 247419a81c14SSteve Longerbeam 247519a81c14SSteve Longerbeam if (value) { 247619a81c14SSteve Longerbeam ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0, 247719a81c14SSteve Longerbeam BIT(1), BIT(1)); 247819a81c14SSteve Longerbeam if (ret) 247919a81c14SSteve Longerbeam return ret; 248019a81c14SSteve Longerbeam ret = ov5640_write_reg(sensor, OV5640_REG_SDE_CTRL3, 248119a81c14SSteve Longerbeam value & 0xff); 248219a81c14SSteve Longerbeam if (ret) 248319a81c14SSteve Longerbeam return ret; 248419a81c14SSteve Longerbeam ret = ov5640_write_reg(sensor, OV5640_REG_SDE_CTRL4, 248519a81c14SSteve Longerbeam value & 0xff); 248619a81c14SSteve Longerbeam } else { 248719a81c14SSteve Longerbeam ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0, BIT(1), 0); 248819a81c14SSteve Longerbeam } 248919a81c14SSteve Longerbeam 249019a81c14SSteve Longerbeam return ret; 249119a81c14SSteve Longerbeam } 249219a81c14SSteve Longerbeam 249319a81c14SSteve Longerbeam static int ov5640_set_ctrl_white_balance(struct ov5640_dev *sensor, int awb) 249419a81c14SSteve Longerbeam { 249519a81c14SSteve Longerbeam int ret; 249619a81c14SSteve Longerbeam 249719a81c14SSteve Longerbeam ret = ov5640_mod_reg(sensor, OV5640_REG_AWB_MANUAL_CTRL, 249819a81c14SSteve Longerbeam BIT(0), awb ? 0 : 1); 249919a81c14SSteve Longerbeam if (ret) 250019a81c14SSteve Longerbeam return ret; 250119a81c14SSteve Longerbeam 250219a81c14SSteve Longerbeam if (!awb) { 250319a81c14SSteve Longerbeam u16 red = (u16)sensor->ctrls.red_balance->val; 250419a81c14SSteve Longerbeam u16 blue = (u16)sensor->ctrls.blue_balance->val; 250519a81c14SSteve Longerbeam 250619a81c14SSteve Longerbeam ret = ov5640_write_reg16(sensor, OV5640_REG_AWB_R_GAIN, red); 250719a81c14SSteve Longerbeam if (ret) 250819a81c14SSteve Longerbeam return ret; 250919a81c14SSteve Longerbeam ret = ov5640_write_reg16(sensor, OV5640_REG_AWB_B_GAIN, blue); 251019a81c14SSteve Longerbeam } 251119a81c14SSteve Longerbeam 251219a81c14SSteve Longerbeam return ret; 251319a81c14SSteve Longerbeam } 251419a81c14SSteve Longerbeam 25153cca8ef5SHugues Fruchet static int ov5640_set_ctrl_exposure(struct ov5640_dev *sensor, 25163cca8ef5SHugues Fruchet enum v4l2_exposure_auto_type auto_exposure) 251719a81c14SSteve Longerbeam { 251819a81c14SSteve Longerbeam struct ov5640_ctrls *ctrls = &sensor->ctrls; 25193cca8ef5SHugues Fruchet bool auto_exp = (auto_exposure == V4L2_EXPOSURE_AUTO); 252019a81c14SSteve Longerbeam int ret = 0; 252119a81c14SSteve Longerbeam 252219a81c14SSteve Longerbeam if (ctrls->auto_exp->is_new) { 25233cca8ef5SHugues Fruchet ret = ov5640_set_autoexposure(sensor, auto_exp); 252419a81c14SSteve Longerbeam if (ret) 252519a81c14SSteve Longerbeam return ret; 252619a81c14SSteve Longerbeam } 252719a81c14SSteve Longerbeam 25283cca8ef5SHugues Fruchet if (!auto_exp && ctrls->exposure->is_new) { 252919a81c14SSteve Longerbeam u16 max_exp; 253019a81c14SSteve Longerbeam 253119a81c14SSteve Longerbeam ret = ov5640_read_reg16(sensor, OV5640_REG_AEC_PK_VTS, 253219a81c14SSteve Longerbeam &max_exp); 253319a81c14SSteve Longerbeam if (ret) 253419a81c14SSteve Longerbeam return ret; 253519a81c14SSteve Longerbeam ret = ov5640_get_vts(sensor); 253619a81c14SSteve Longerbeam if (ret < 0) 253719a81c14SSteve Longerbeam return ret; 253819a81c14SSteve Longerbeam max_exp += ret; 25396146fde3SHugues Fruchet ret = 0; 254019a81c14SSteve Longerbeam 254119a81c14SSteve Longerbeam if (ctrls->exposure->val < max_exp) 254219a81c14SSteve Longerbeam ret = ov5640_set_exposure(sensor, ctrls->exposure->val); 254319a81c14SSteve Longerbeam } 254419a81c14SSteve Longerbeam 254519a81c14SSteve Longerbeam return ret; 254619a81c14SSteve Longerbeam } 254719a81c14SSteve Longerbeam 25483cca8ef5SHugues Fruchet static int ov5640_set_ctrl_gain(struct ov5640_dev *sensor, bool auto_gain) 254919a81c14SSteve Longerbeam { 255019a81c14SSteve Longerbeam struct ov5640_ctrls *ctrls = &sensor->ctrls; 255119a81c14SSteve Longerbeam int ret = 0; 255219a81c14SSteve Longerbeam 255319a81c14SSteve Longerbeam if (ctrls->auto_gain->is_new) { 25543cca8ef5SHugues Fruchet ret = ov5640_set_autogain(sensor, auto_gain); 255519a81c14SSteve Longerbeam if (ret) 255619a81c14SSteve Longerbeam return ret; 255719a81c14SSteve Longerbeam } 255819a81c14SSteve Longerbeam 25593cca8ef5SHugues Fruchet if (!auto_gain && ctrls->gain->is_new) 25603cca8ef5SHugues Fruchet ret = ov5640_set_gain(sensor, ctrls->gain->val); 256119a81c14SSteve Longerbeam 256219a81c14SSteve Longerbeam return ret; 256319a81c14SSteve Longerbeam } 256419a81c14SSteve Longerbeam 25659f6d7bacSChen-Yu Tsai static const char * const test_pattern_menu[] = { 25669f6d7bacSChen-Yu Tsai "Disabled", 25679f6d7bacSChen-Yu Tsai "Color bars", 2568bddc5cdfSChen-Yu Tsai "Color bars w/ rolling bar", 2569bddc5cdfSChen-Yu Tsai "Color squares", 2570bddc5cdfSChen-Yu Tsai "Color squares w/ rolling bar", 25719f6d7bacSChen-Yu Tsai }; 25729f6d7bacSChen-Yu Tsai 2573a0c29afbSChen-Yu Tsai #define OV5640_TEST_ENABLE BIT(7) 2574a0c29afbSChen-Yu Tsai #define OV5640_TEST_ROLLING BIT(6) /* rolling horizontal bar */ 2575a0c29afbSChen-Yu Tsai #define OV5640_TEST_TRANSPARENT BIT(5) 2576a0c29afbSChen-Yu Tsai #define OV5640_TEST_SQUARE_BW BIT(4) /* black & white squares */ 2577a0c29afbSChen-Yu Tsai #define OV5640_TEST_BAR_STANDARD (0 << 2) 2578a0c29afbSChen-Yu Tsai #define OV5640_TEST_BAR_VERT_CHANGE_1 (1 << 2) 2579a0c29afbSChen-Yu Tsai #define OV5640_TEST_BAR_HOR_CHANGE (2 << 2) 2580a0c29afbSChen-Yu Tsai #define OV5640_TEST_BAR_VERT_CHANGE_2 (3 << 2) 2581a0c29afbSChen-Yu Tsai #define OV5640_TEST_BAR (0 << 0) 2582a0c29afbSChen-Yu Tsai #define OV5640_TEST_RANDOM (1 << 0) 2583a0c29afbSChen-Yu Tsai #define OV5640_TEST_SQUARE (2 << 0) 2584a0c29afbSChen-Yu Tsai #define OV5640_TEST_BLACK (3 << 0) 2585a0c29afbSChen-Yu Tsai 2586a0c29afbSChen-Yu Tsai static const u8 test_pattern_val[] = { 2587a0c29afbSChen-Yu Tsai 0, 25882aff1fc3SChen-Yu Tsai OV5640_TEST_ENABLE | OV5640_TEST_BAR_VERT_CHANGE_1 | 2589a0c29afbSChen-Yu Tsai OV5640_TEST_BAR, 2590bddc5cdfSChen-Yu Tsai OV5640_TEST_ENABLE | OV5640_TEST_ROLLING | 2591bddc5cdfSChen-Yu Tsai OV5640_TEST_BAR_VERT_CHANGE_1 | OV5640_TEST_BAR, 2592bddc5cdfSChen-Yu Tsai OV5640_TEST_ENABLE | OV5640_TEST_SQUARE, 2593bddc5cdfSChen-Yu Tsai OV5640_TEST_ENABLE | OV5640_TEST_ROLLING | OV5640_TEST_SQUARE, 2594a0c29afbSChen-Yu Tsai }; 2595a0c29afbSChen-Yu Tsai 259619a81c14SSteve Longerbeam static int ov5640_set_ctrl_test_pattern(struct ov5640_dev *sensor, int value) 259719a81c14SSteve Longerbeam { 2598a0c29afbSChen-Yu Tsai return ov5640_write_reg(sensor, OV5640_REG_PRE_ISP_TEST_SET1, 2599a0c29afbSChen-Yu Tsai test_pattern_val[value]); 260019a81c14SSteve Longerbeam } 260119a81c14SSteve Longerbeam 26021068fecaSMylène Josserand static int ov5640_set_ctrl_light_freq(struct ov5640_dev *sensor, int value) 26031068fecaSMylène Josserand { 26041068fecaSMylène Josserand int ret; 26051068fecaSMylène Josserand 26061068fecaSMylène Josserand ret = ov5640_mod_reg(sensor, OV5640_REG_HZ5060_CTRL01, BIT(7), 26071068fecaSMylène Josserand (value == V4L2_CID_POWER_LINE_FREQUENCY_AUTO) ? 26081068fecaSMylène Josserand 0 : BIT(7)); 26091068fecaSMylène Josserand if (ret) 26101068fecaSMylène Josserand return ret; 26111068fecaSMylène Josserand 26121068fecaSMylène Josserand return ov5640_mod_reg(sensor, OV5640_REG_HZ5060_CTRL00, BIT(2), 26131068fecaSMylène Josserand (value == V4L2_CID_POWER_LINE_FREQUENCY_50HZ) ? 26141068fecaSMylène Josserand BIT(2) : 0); 26151068fecaSMylène Josserand } 26161068fecaSMylène Josserand 2617ce85705aSHugues Fruchet static int ov5640_set_ctrl_hflip(struct ov5640_dev *sensor, int value) 2618ce85705aSHugues Fruchet { 2619ce85705aSHugues Fruchet /* 2620c3f3ba3eSHugues Fruchet * If sensor is mounted upside down, mirror logic is inversed. 2621c3f3ba3eSHugues Fruchet * 2622ce85705aSHugues Fruchet * Sensor is a BSI (Back Side Illuminated) one, 2623ce85705aSHugues Fruchet * so image captured is physically mirrored. 2624ce85705aSHugues Fruchet * This is why mirror logic is inversed in 2625ce85705aSHugues Fruchet * order to cancel this mirror effect. 2626ce85705aSHugues Fruchet */ 2627ce85705aSHugues Fruchet 2628ce85705aSHugues Fruchet /* 2629ce85705aSHugues Fruchet * TIMING TC REG21: 2630ce85705aSHugues Fruchet * - [2]: ISP mirror 2631ce85705aSHugues Fruchet * - [1]: Sensor mirror 2632ce85705aSHugues Fruchet */ 2633ce85705aSHugues Fruchet return ov5640_mod_reg(sensor, OV5640_REG_TIMING_TC_REG21, 2634ce85705aSHugues Fruchet BIT(2) | BIT(1), 2635c3f3ba3eSHugues Fruchet (!(value ^ sensor->upside_down)) ? 2636c3f3ba3eSHugues Fruchet (BIT(2) | BIT(1)) : 0); 2637ce85705aSHugues Fruchet } 2638ce85705aSHugues Fruchet 2639ce85705aSHugues Fruchet static int ov5640_set_ctrl_vflip(struct ov5640_dev *sensor, int value) 2640ce85705aSHugues Fruchet { 2641c3f3ba3eSHugues Fruchet /* If sensor is mounted upside down, flip logic is inversed */ 2642c3f3ba3eSHugues Fruchet 2643ce85705aSHugues Fruchet /* 2644ce85705aSHugues Fruchet * TIMING TC REG20: 2645ce85705aSHugues Fruchet * - [2]: ISP vflip 2646ce85705aSHugues Fruchet * - [1]: Sensor vflip 2647ce85705aSHugues Fruchet */ 2648ce85705aSHugues Fruchet return ov5640_mod_reg(sensor, OV5640_REG_TIMING_TC_REG20, 2649ce85705aSHugues Fruchet BIT(2) | BIT(1), 2650c3f3ba3eSHugues Fruchet (value ^ sensor->upside_down) ? 2651c3f3ba3eSHugues Fruchet (BIT(2) | BIT(1)) : 0); 2652ce85705aSHugues Fruchet } 2653ce85705aSHugues Fruchet 265419a81c14SSteve Longerbeam static int ov5640_g_volatile_ctrl(struct v4l2_ctrl *ctrl) 265519a81c14SSteve Longerbeam { 265619a81c14SSteve Longerbeam struct v4l2_subdev *sd = ctrl_to_sd(ctrl); 265719a81c14SSteve Longerbeam struct ov5640_dev *sensor = to_ov5640_dev(sd); 265819a81c14SSteve Longerbeam int val; 265919a81c14SSteve Longerbeam 266019a81c14SSteve Longerbeam /* v4l2_ctrl_lock() locks our own mutex */ 266119a81c14SSteve Longerbeam 266219a81c14SSteve Longerbeam switch (ctrl->id) { 266319a81c14SSteve Longerbeam case V4L2_CID_AUTOGAIN: 266419a81c14SSteve Longerbeam val = ov5640_get_gain(sensor); 266519a81c14SSteve Longerbeam if (val < 0) 266619a81c14SSteve Longerbeam return val; 266719a81c14SSteve Longerbeam sensor->ctrls.gain->val = val; 266819a81c14SSteve Longerbeam break; 266919a81c14SSteve Longerbeam case V4L2_CID_EXPOSURE_AUTO: 267019a81c14SSteve Longerbeam val = ov5640_get_exposure(sensor); 267119a81c14SSteve Longerbeam if (val < 0) 267219a81c14SSteve Longerbeam return val; 267319a81c14SSteve Longerbeam sensor->ctrls.exposure->val = val; 267419a81c14SSteve Longerbeam break; 267519a81c14SSteve Longerbeam } 267619a81c14SSteve Longerbeam 267719a81c14SSteve Longerbeam return 0; 267819a81c14SSteve Longerbeam } 267919a81c14SSteve Longerbeam 268019a81c14SSteve Longerbeam static int ov5640_s_ctrl(struct v4l2_ctrl *ctrl) 268119a81c14SSteve Longerbeam { 268219a81c14SSteve Longerbeam struct v4l2_subdev *sd = ctrl_to_sd(ctrl); 268319a81c14SSteve Longerbeam struct ov5640_dev *sensor = to_ov5640_dev(sd); 268419a81c14SSteve Longerbeam int ret; 268519a81c14SSteve Longerbeam 268619a81c14SSteve Longerbeam /* v4l2_ctrl_lock() locks our own mutex */ 268719a81c14SSteve Longerbeam 268819a81c14SSteve Longerbeam /* 268919a81c14SSteve Longerbeam * If the device is not powered up by the host driver do 269019a81c14SSteve Longerbeam * not apply any controls to H/W at this time. Instead 269119a81c14SSteve Longerbeam * the controls will be restored right after power-up. 269219a81c14SSteve Longerbeam */ 269319a81c14SSteve Longerbeam if (sensor->power_count == 0) 269419a81c14SSteve Longerbeam return 0; 269519a81c14SSteve Longerbeam 269619a81c14SSteve Longerbeam switch (ctrl->id) { 269719a81c14SSteve Longerbeam case V4L2_CID_AUTOGAIN: 269819a81c14SSteve Longerbeam ret = ov5640_set_ctrl_gain(sensor, ctrl->val); 269919a81c14SSteve Longerbeam break; 270019a81c14SSteve Longerbeam case V4L2_CID_EXPOSURE_AUTO: 270119a81c14SSteve Longerbeam ret = ov5640_set_ctrl_exposure(sensor, ctrl->val); 270219a81c14SSteve Longerbeam break; 270319a81c14SSteve Longerbeam case V4L2_CID_AUTO_WHITE_BALANCE: 270419a81c14SSteve Longerbeam ret = ov5640_set_ctrl_white_balance(sensor, ctrl->val); 270519a81c14SSteve Longerbeam break; 270619a81c14SSteve Longerbeam case V4L2_CID_HUE: 270719a81c14SSteve Longerbeam ret = ov5640_set_ctrl_hue(sensor, ctrl->val); 270819a81c14SSteve Longerbeam break; 270919a81c14SSteve Longerbeam case V4L2_CID_CONTRAST: 271019a81c14SSteve Longerbeam ret = ov5640_set_ctrl_contrast(sensor, ctrl->val); 271119a81c14SSteve Longerbeam break; 271219a81c14SSteve Longerbeam case V4L2_CID_SATURATION: 271319a81c14SSteve Longerbeam ret = ov5640_set_ctrl_saturation(sensor, ctrl->val); 271419a81c14SSteve Longerbeam break; 271519a81c14SSteve Longerbeam case V4L2_CID_TEST_PATTERN: 271619a81c14SSteve Longerbeam ret = ov5640_set_ctrl_test_pattern(sensor, ctrl->val); 271719a81c14SSteve Longerbeam break; 27181068fecaSMylène Josserand case V4L2_CID_POWER_LINE_FREQUENCY: 27191068fecaSMylène Josserand ret = ov5640_set_ctrl_light_freq(sensor, ctrl->val); 27201068fecaSMylène Josserand break; 2721ce85705aSHugues Fruchet case V4L2_CID_HFLIP: 2722ce85705aSHugues Fruchet ret = ov5640_set_ctrl_hflip(sensor, ctrl->val); 2723ce85705aSHugues Fruchet break; 2724ce85705aSHugues Fruchet case V4L2_CID_VFLIP: 2725ce85705aSHugues Fruchet ret = ov5640_set_ctrl_vflip(sensor, ctrl->val); 2726ce85705aSHugues Fruchet break; 272719a81c14SSteve Longerbeam default: 272819a81c14SSteve Longerbeam ret = -EINVAL; 272919a81c14SSteve Longerbeam break; 273019a81c14SSteve Longerbeam } 273119a81c14SSteve Longerbeam 273219a81c14SSteve Longerbeam return ret; 273319a81c14SSteve Longerbeam } 273419a81c14SSteve Longerbeam 273519a81c14SSteve Longerbeam static const struct v4l2_ctrl_ops ov5640_ctrl_ops = { 273619a81c14SSteve Longerbeam .g_volatile_ctrl = ov5640_g_volatile_ctrl, 273719a81c14SSteve Longerbeam .s_ctrl = ov5640_s_ctrl, 273819a81c14SSteve Longerbeam }; 273919a81c14SSteve Longerbeam 274019a81c14SSteve Longerbeam static int ov5640_init_controls(struct ov5640_dev *sensor) 274119a81c14SSteve Longerbeam { 274219a81c14SSteve Longerbeam const struct v4l2_ctrl_ops *ops = &ov5640_ctrl_ops; 274319a81c14SSteve Longerbeam struct ov5640_ctrls *ctrls = &sensor->ctrls; 274419a81c14SSteve Longerbeam struct v4l2_ctrl_handler *hdl = &ctrls->handler; 274519a81c14SSteve Longerbeam int ret; 274619a81c14SSteve Longerbeam 274719a81c14SSteve Longerbeam v4l2_ctrl_handler_init(hdl, 32); 274819a81c14SSteve Longerbeam 274919a81c14SSteve Longerbeam /* we can use our own mutex for the ctrl lock */ 275019a81c14SSteve Longerbeam hdl->lock = &sensor->lock; 275119a81c14SSteve Longerbeam 2752cc196e48SBenoit Parrot /* Clock related controls */ 2753cc196e48SBenoit Parrot ctrls->pixel_rate = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_PIXEL_RATE, 2754cc196e48SBenoit Parrot 0, INT_MAX, 1, 2755cc196e48SBenoit Parrot ov5640_calc_pixel_rate(sensor)); 2756cc196e48SBenoit Parrot 275719a81c14SSteve Longerbeam /* Auto/manual white balance */ 275819a81c14SSteve Longerbeam ctrls->auto_wb = v4l2_ctrl_new_std(hdl, ops, 275919a81c14SSteve Longerbeam V4L2_CID_AUTO_WHITE_BALANCE, 276019a81c14SSteve Longerbeam 0, 1, 1, 1); 276119a81c14SSteve Longerbeam ctrls->blue_balance = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_BLUE_BALANCE, 276219a81c14SSteve Longerbeam 0, 4095, 1, 0); 276319a81c14SSteve Longerbeam ctrls->red_balance = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_RED_BALANCE, 276419a81c14SSteve Longerbeam 0, 4095, 1, 0); 276519a81c14SSteve Longerbeam /* Auto/manual exposure */ 276619a81c14SSteve Longerbeam ctrls->auto_exp = v4l2_ctrl_new_std_menu(hdl, ops, 276719a81c14SSteve Longerbeam V4L2_CID_EXPOSURE_AUTO, 276819a81c14SSteve Longerbeam V4L2_EXPOSURE_MANUAL, 0, 276919a81c14SSteve Longerbeam V4L2_EXPOSURE_AUTO); 277019a81c14SSteve Longerbeam ctrls->exposure = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_EXPOSURE, 277119a81c14SSteve Longerbeam 0, 65535, 1, 0); 277219a81c14SSteve Longerbeam /* Auto/manual gain */ 277319a81c14SSteve Longerbeam ctrls->auto_gain = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_AUTOGAIN, 277419a81c14SSteve Longerbeam 0, 1, 1, 1); 277519a81c14SSteve Longerbeam ctrls->gain = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_GAIN, 277619a81c14SSteve Longerbeam 0, 1023, 1, 0); 277719a81c14SSteve Longerbeam 277819a81c14SSteve Longerbeam ctrls->saturation = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_SATURATION, 277919a81c14SSteve Longerbeam 0, 255, 1, 64); 278019a81c14SSteve Longerbeam ctrls->hue = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_HUE, 278119a81c14SSteve Longerbeam 0, 359, 1, 0); 278219a81c14SSteve Longerbeam ctrls->contrast = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_CONTRAST, 278319a81c14SSteve Longerbeam 0, 255, 1, 0); 278419a81c14SSteve Longerbeam ctrls->test_pattern = 278519a81c14SSteve Longerbeam v4l2_ctrl_new_std_menu_items(hdl, ops, V4L2_CID_TEST_PATTERN, 278619a81c14SSteve Longerbeam ARRAY_SIZE(test_pattern_menu) - 1, 278719a81c14SSteve Longerbeam 0, 0, test_pattern_menu); 2788ce85705aSHugues Fruchet ctrls->hflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_HFLIP, 2789ce85705aSHugues Fruchet 0, 1, 1, 0); 2790ce85705aSHugues Fruchet ctrls->vflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_VFLIP, 2791ce85705aSHugues Fruchet 0, 1, 1, 0); 279219a81c14SSteve Longerbeam 27931068fecaSMylène Josserand ctrls->light_freq = 27941068fecaSMylène Josserand v4l2_ctrl_new_std_menu(hdl, ops, 27951068fecaSMylène Josserand V4L2_CID_POWER_LINE_FREQUENCY, 27961068fecaSMylène Josserand V4L2_CID_POWER_LINE_FREQUENCY_AUTO, 0, 27971068fecaSMylène Josserand V4L2_CID_POWER_LINE_FREQUENCY_50HZ); 27981068fecaSMylène Josserand 279919a81c14SSteve Longerbeam if (hdl->error) { 280019a81c14SSteve Longerbeam ret = hdl->error; 280119a81c14SSteve Longerbeam goto free_ctrls; 280219a81c14SSteve Longerbeam } 280319a81c14SSteve Longerbeam 2804cc196e48SBenoit Parrot ctrls->pixel_rate->flags |= V4L2_CTRL_FLAG_READ_ONLY; 280519a81c14SSteve Longerbeam ctrls->gain->flags |= V4L2_CTRL_FLAG_VOLATILE; 280619a81c14SSteve Longerbeam ctrls->exposure->flags |= V4L2_CTRL_FLAG_VOLATILE; 280719a81c14SSteve Longerbeam 280819a81c14SSteve Longerbeam v4l2_ctrl_auto_cluster(3, &ctrls->auto_wb, 0, false); 280919a81c14SSteve Longerbeam v4l2_ctrl_auto_cluster(2, &ctrls->auto_gain, 0, true); 281019a81c14SSteve Longerbeam v4l2_ctrl_auto_cluster(2, &ctrls->auto_exp, 1, true); 281119a81c14SSteve Longerbeam 281219a81c14SSteve Longerbeam sensor->sd.ctrl_handler = hdl; 281319a81c14SSteve Longerbeam return 0; 281419a81c14SSteve Longerbeam 281519a81c14SSteve Longerbeam free_ctrls: 281619a81c14SSteve Longerbeam v4l2_ctrl_handler_free(hdl); 281719a81c14SSteve Longerbeam return ret; 281819a81c14SSteve Longerbeam } 281919a81c14SSteve Longerbeam 282019a81c14SSteve Longerbeam static int ov5640_enum_frame_size(struct v4l2_subdev *sd, 282119a81c14SSteve Longerbeam struct v4l2_subdev_pad_config *cfg, 282219a81c14SSteve Longerbeam struct v4l2_subdev_frame_size_enum *fse) 282319a81c14SSteve Longerbeam { 282419a81c14SSteve Longerbeam if (fse->pad != 0) 282519a81c14SSteve Longerbeam return -EINVAL; 282619a81c14SSteve Longerbeam if (fse->index >= OV5640_NUM_MODES) 282719a81c14SSteve Longerbeam return -EINVAL; 282819a81c14SSteve Longerbeam 282941d8d7f5SHugues Fruchet fse->min_width = 2830086c25f8SMaxime Ripard ov5640_mode_data[fse->index].hact; 283141d8d7f5SHugues Fruchet fse->max_width = fse->min_width; 283241d8d7f5SHugues Fruchet fse->min_height = 2833086c25f8SMaxime Ripard ov5640_mode_data[fse->index].vact; 283441d8d7f5SHugues Fruchet fse->max_height = fse->min_height; 283519a81c14SSteve Longerbeam 283619a81c14SSteve Longerbeam return 0; 283719a81c14SSteve Longerbeam } 283819a81c14SSteve Longerbeam 283919a81c14SSteve Longerbeam static int ov5640_enum_frame_interval( 284019a81c14SSteve Longerbeam struct v4l2_subdev *sd, 284119a81c14SSteve Longerbeam struct v4l2_subdev_pad_config *cfg, 284219a81c14SSteve Longerbeam struct v4l2_subdev_frame_interval_enum *fie) 284319a81c14SSteve Longerbeam { 284419a81c14SSteve Longerbeam struct ov5640_dev *sensor = to_ov5640_dev(sd); 284519a81c14SSteve Longerbeam struct v4l2_fract tpf; 284619a81c14SSteve Longerbeam int ret; 284719a81c14SSteve Longerbeam 284819a81c14SSteve Longerbeam if (fie->pad != 0) 284919a81c14SSteve Longerbeam return -EINVAL; 285019a81c14SSteve Longerbeam if (fie->index >= OV5640_NUM_FRAMERATES) 285119a81c14SSteve Longerbeam return -EINVAL; 285219a81c14SSteve Longerbeam 285319a81c14SSteve Longerbeam tpf.numerator = 1; 285419a81c14SSteve Longerbeam tpf.denominator = ov5640_framerates[fie->index]; 285519a81c14SSteve Longerbeam 285619a81c14SSteve Longerbeam ret = ov5640_try_frame_interval(sensor, &tpf, 285719a81c14SSteve Longerbeam fie->width, fie->height); 285819a81c14SSteve Longerbeam if (ret < 0) 285919a81c14SSteve Longerbeam return -EINVAL; 286019a81c14SSteve Longerbeam 286119a81c14SSteve Longerbeam fie->interval = tpf; 286219a81c14SSteve Longerbeam return 0; 286319a81c14SSteve Longerbeam } 286419a81c14SSteve Longerbeam 286519a81c14SSteve Longerbeam static int ov5640_g_frame_interval(struct v4l2_subdev *sd, 286619a81c14SSteve Longerbeam struct v4l2_subdev_frame_interval *fi) 286719a81c14SSteve Longerbeam { 286819a81c14SSteve Longerbeam struct ov5640_dev *sensor = to_ov5640_dev(sd); 286919a81c14SSteve Longerbeam 287019a81c14SSteve Longerbeam mutex_lock(&sensor->lock); 287119a81c14SSteve Longerbeam fi->interval = sensor->frame_interval; 287219a81c14SSteve Longerbeam mutex_unlock(&sensor->lock); 287319a81c14SSteve Longerbeam 287419a81c14SSteve Longerbeam return 0; 287519a81c14SSteve Longerbeam } 287619a81c14SSteve Longerbeam 287719a81c14SSteve Longerbeam static int ov5640_s_frame_interval(struct v4l2_subdev *sd, 287819a81c14SSteve Longerbeam struct v4l2_subdev_frame_interval *fi) 287919a81c14SSteve Longerbeam { 288019a81c14SSteve Longerbeam struct ov5640_dev *sensor = to_ov5640_dev(sd); 288119a81c14SSteve Longerbeam const struct ov5640_mode_info *mode; 288219a81c14SSteve Longerbeam int frame_rate, ret = 0; 288319a81c14SSteve Longerbeam 288419a81c14SSteve Longerbeam if (fi->pad != 0) 288519a81c14SSteve Longerbeam return -EINVAL; 288619a81c14SSteve Longerbeam 288719a81c14SSteve Longerbeam mutex_lock(&sensor->lock); 288819a81c14SSteve Longerbeam 288919a81c14SSteve Longerbeam if (sensor->streaming) { 289019a81c14SSteve Longerbeam ret = -EBUSY; 289119a81c14SSteve Longerbeam goto out; 289219a81c14SSteve Longerbeam } 289319a81c14SSteve Longerbeam 289419a81c14SSteve Longerbeam mode = sensor->current_mode; 289519a81c14SSteve Longerbeam 289619a81c14SSteve Longerbeam frame_rate = ov5640_try_frame_interval(sensor, &fi->interval, 2897dba13a0bSMaxime Ripard mode->hact, mode->vact); 2898e823fb16SMaxime Ripard if (frame_rate < 0) { 2899e823fb16SMaxime Ripard /* Always return a valid frame interval value */ 2900e823fb16SMaxime Ripard fi->interval = sensor->frame_interval; 2901e823fb16SMaxime Ripard goto out; 2902e823fb16SMaxime Ripard } 290319a81c14SSteve Longerbeam 29043c4a7372SHugues Fruchet mode = ov5640_find_mode(sensor, frame_rate, mode->hact, 2905dba13a0bSMaxime Ripard mode->vact, true); 29063c4a7372SHugues Fruchet if (!mode) { 29073c4a7372SHugues Fruchet ret = -EINVAL; 29083c4a7372SHugues Fruchet goto out; 29093c4a7372SHugues Fruchet } 29103c4a7372SHugues Fruchet 29110929983eSHugues Fruchet if (mode != sensor->current_mode || 29120929983eSHugues Fruchet frame_rate != sensor->current_fr) { 29130929983eSHugues Fruchet sensor->current_fr = frame_rate; 29140929983eSHugues Fruchet sensor->frame_interval = fi->interval; 29153c4a7372SHugues Fruchet sensor->current_mode = mode; 291619a81c14SSteve Longerbeam sensor->pending_mode_change = true; 2917cc196e48SBenoit Parrot 2918cc196e48SBenoit Parrot __v4l2_ctrl_s_ctrl_int64(sensor->ctrls.pixel_rate, 2919cc196e48SBenoit Parrot ov5640_calc_pixel_rate(sensor)); 29206949d864SHugues Fruchet } 292119a81c14SSteve Longerbeam out: 292219a81c14SSteve Longerbeam mutex_unlock(&sensor->lock); 292319a81c14SSteve Longerbeam return ret; 292419a81c14SSteve Longerbeam } 292519a81c14SSteve Longerbeam 292619a81c14SSteve Longerbeam static int ov5640_enum_mbus_code(struct v4l2_subdev *sd, 292719a81c14SSteve Longerbeam struct v4l2_subdev_pad_config *cfg, 292819a81c14SSteve Longerbeam struct v4l2_subdev_mbus_code_enum *code) 292919a81c14SSteve Longerbeam { 293019a81c14SSteve Longerbeam if (code->pad != 0) 293119a81c14SSteve Longerbeam return -EINVAL; 2932e3ee691dSHugues Fruchet if (code->index >= ARRAY_SIZE(ov5640_formats)) 293319a81c14SSteve Longerbeam return -EINVAL; 293419a81c14SSteve Longerbeam 2935e3ee691dSHugues Fruchet code->code = ov5640_formats[code->index].code; 293619a81c14SSteve Longerbeam return 0; 293719a81c14SSteve Longerbeam } 293819a81c14SSteve Longerbeam 293919a81c14SSteve Longerbeam static int ov5640_s_stream(struct v4l2_subdev *sd, int enable) 294019a81c14SSteve Longerbeam { 294119a81c14SSteve Longerbeam struct ov5640_dev *sensor = to_ov5640_dev(sd); 294219a81c14SSteve Longerbeam int ret = 0; 294319a81c14SSteve Longerbeam 294419a81c14SSteve Longerbeam mutex_lock(&sensor->lock); 294519a81c14SSteve Longerbeam 294619a81c14SSteve Longerbeam if (sensor->streaming == !enable) { 294719a81c14SSteve Longerbeam if (enable && sensor->pending_mode_change) { 2948985cdcb0SHugues Fruchet ret = ov5640_set_mode(sensor); 294919a81c14SSteve Longerbeam if (ret) 295019a81c14SSteve Longerbeam goto out; 2951fb98e29fSHugues Fruchet } 2952e3ee691dSHugues Fruchet 2953fb98e29fSHugues Fruchet if (enable && sensor->pending_fmt_change) { 2954e3ee691dSHugues Fruchet ret = ov5640_set_framefmt(sensor, &sensor->fmt); 2955e3ee691dSHugues Fruchet if (ret) 2956e3ee691dSHugues Fruchet goto out; 2957fb98e29fSHugues Fruchet sensor->pending_fmt_change = false; 295819a81c14SSteve Longerbeam } 295919a81c14SSteve Longerbeam 29602d95e7edSSakari Ailus if (sensor->ep.bus_type == V4L2_MBUS_CSI2_DPHY) 2961f22996dbSHugues Fruchet ret = ov5640_set_stream_mipi(sensor, enable); 2962f22996dbSHugues Fruchet else 2963f22996dbSHugues Fruchet ret = ov5640_set_stream_dvp(sensor, enable); 2964f22996dbSHugues Fruchet 296519a81c14SSteve Longerbeam if (!ret) 296619a81c14SSteve Longerbeam sensor->streaming = enable; 296719a81c14SSteve Longerbeam } 296819a81c14SSteve Longerbeam out: 296919a81c14SSteve Longerbeam mutex_unlock(&sensor->lock); 297019a81c14SSteve Longerbeam return ret; 297119a81c14SSteve Longerbeam } 297219a81c14SSteve Longerbeam 297319a81c14SSteve Longerbeam static const struct v4l2_subdev_core_ops ov5640_core_ops = { 297419a81c14SSteve Longerbeam .s_power = ov5640_s_power, 29752d18fbc5SAkinobu Mita .log_status = v4l2_ctrl_subdev_log_status, 29762d18fbc5SAkinobu Mita .subscribe_event = v4l2_ctrl_subdev_subscribe_event, 29772d18fbc5SAkinobu Mita .unsubscribe_event = v4l2_event_subdev_unsubscribe, 297819a81c14SSteve Longerbeam }; 297919a81c14SSteve Longerbeam 298019a81c14SSteve Longerbeam static const struct v4l2_subdev_video_ops ov5640_video_ops = { 298119a81c14SSteve Longerbeam .g_frame_interval = ov5640_g_frame_interval, 298219a81c14SSteve Longerbeam .s_frame_interval = ov5640_s_frame_interval, 298319a81c14SSteve Longerbeam .s_stream = ov5640_s_stream, 298419a81c14SSteve Longerbeam }; 298519a81c14SSteve Longerbeam 298619a81c14SSteve Longerbeam static const struct v4l2_subdev_pad_ops ov5640_pad_ops = { 298719a81c14SSteve Longerbeam .enum_mbus_code = ov5640_enum_mbus_code, 298819a81c14SSteve Longerbeam .get_fmt = ov5640_get_fmt, 298919a81c14SSteve Longerbeam .set_fmt = ov5640_set_fmt, 299019a81c14SSteve Longerbeam .enum_frame_size = ov5640_enum_frame_size, 299119a81c14SSteve Longerbeam .enum_frame_interval = ov5640_enum_frame_interval, 299219a81c14SSteve Longerbeam }; 299319a81c14SSteve Longerbeam 299419a81c14SSteve Longerbeam static const struct v4l2_subdev_ops ov5640_subdev_ops = { 299519a81c14SSteve Longerbeam .core = &ov5640_core_ops, 299619a81c14SSteve Longerbeam .video = &ov5640_video_ops, 299719a81c14SSteve Longerbeam .pad = &ov5640_pad_ops, 299819a81c14SSteve Longerbeam }; 299919a81c14SSteve Longerbeam 300019a81c14SSteve Longerbeam static int ov5640_get_regulators(struct ov5640_dev *sensor) 300119a81c14SSteve Longerbeam { 300219a81c14SSteve Longerbeam int i; 300319a81c14SSteve Longerbeam 300419a81c14SSteve Longerbeam for (i = 0; i < OV5640_NUM_SUPPLIES; i++) 300519a81c14SSteve Longerbeam sensor->supplies[i].supply = ov5640_supply_name[i]; 300619a81c14SSteve Longerbeam 300719a81c14SSteve Longerbeam return devm_regulator_bulk_get(&sensor->i2c_client->dev, 300819a81c14SSteve Longerbeam OV5640_NUM_SUPPLIES, 300919a81c14SSteve Longerbeam sensor->supplies); 301019a81c14SSteve Longerbeam } 301119a81c14SSteve Longerbeam 30120f7acb52SHugues Fruchet static int ov5640_check_chip_id(struct ov5640_dev *sensor) 30130f7acb52SHugues Fruchet { 30140f7acb52SHugues Fruchet struct i2c_client *client = sensor->i2c_client; 30150f7acb52SHugues Fruchet int ret = 0; 30160f7acb52SHugues Fruchet u16 chip_id; 30170f7acb52SHugues Fruchet 30180f7acb52SHugues Fruchet ret = ov5640_set_power_on(sensor); 30190f7acb52SHugues Fruchet if (ret) 30200f7acb52SHugues Fruchet return ret; 30210f7acb52SHugues Fruchet 30220f7acb52SHugues Fruchet ret = ov5640_read_reg16(sensor, OV5640_REG_CHIP_ID, &chip_id); 30230f7acb52SHugues Fruchet if (ret) { 30240f7acb52SHugues Fruchet dev_err(&client->dev, "%s: failed to read chip identifier\n", 30250f7acb52SHugues Fruchet __func__); 30260f7acb52SHugues Fruchet goto power_off; 30270f7acb52SHugues Fruchet } 30280f7acb52SHugues Fruchet 30290f7acb52SHugues Fruchet if (chip_id != 0x5640) { 30300f7acb52SHugues Fruchet dev_err(&client->dev, "%s: wrong chip identifier, expected 0x5640, got 0x%x\n", 30310f7acb52SHugues Fruchet __func__, chip_id); 30320f7acb52SHugues Fruchet ret = -ENXIO; 30330f7acb52SHugues Fruchet } 30340f7acb52SHugues Fruchet 30350f7acb52SHugues Fruchet power_off: 30360f7acb52SHugues Fruchet ov5640_set_power_off(sensor); 30370f7acb52SHugues Fruchet return ret; 30380f7acb52SHugues Fruchet } 30390f7acb52SHugues Fruchet 3040e6714993SKieran Bingham static int ov5640_probe(struct i2c_client *client) 304119a81c14SSteve Longerbeam { 304219a81c14SSteve Longerbeam struct device *dev = &client->dev; 304319a81c14SSteve Longerbeam struct fwnode_handle *endpoint; 304419a81c14SSteve Longerbeam struct ov5640_dev *sensor; 3045e6441fdeSHugues Fruchet struct v4l2_mbus_framefmt *fmt; 3046c3f3ba3eSHugues Fruchet u32 rotation; 304719a81c14SSteve Longerbeam int ret; 304819a81c14SSteve Longerbeam 304919a81c14SSteve Longerbeam sensor = devm_kzalloc(dev, sizeof(*sensor), GFP_KERNEL); 305019a81c14SSteve Longerbeam if (!sensor) 305119a81c14SSteve Longerbeam return -ENOMEM; 305219a81c14SSteve Longerbeam 305319a81c14SSteve Longerbeam sensor->i2c_client = client; 3054fb98e29fSHugues Fruchet 3055fb98e29fSHugues Fruchet /* 3056fb98e29fSHugues Fruchet * default init sequence initialize sensor to 3057fb98e29fSHugues Fruchet * YUV422 UYVY VGA@30fps 3058fb98e29fSHugues Fruchet */ 3059e6441fdeSHugues Fruchet fmt = &sensor->fmt; 3060fb98e29fSHugues Fruchet fmt->code = MEDIA_BUS_FMT_UYVY8_2X8; 3061fb98e29fSHugues Fruchet fmt->colorspace = V4L2_COLORSPACE_SRGB; 3062e6441fdeSHugues Fruchet fmt->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(fmt->colorspace); 3063e6441fdeSHugues Fruchet fmt->quantization = V4L2_QUANTIZATION_FULL_RANGE; 3064e6441fdeSHugues Fruchet fmt->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(fmt->colorspace); 3065e6441fdeSHugues Fruchet fmt->width = 640; 3066e6441fdeSHugues Fruchet fmt->height = 480; 3067e6441fdeSHugues Fruchet fmt->field = V4L2_FIELD_NONE; 306819a81c14SSteve Longerbeam sensor->frame_interval.numerator = 1; 306919a81c14SSteve Longerbeam sensor->frame_interval.denominator = ov5640_framerates[OV5640_30_FPS]; 307019a81c14SSteve Longerbeam sensor->current_fr = OV5640_30_FPS; 307119a81c14SSteve Longerbeam sensor->current_mode = 3072086c25f8SMaxime Ripard &ov5640_mode_data[OV5640_MODE_VGA_640_480]; 3073985cdcb0SHugues Fruchet sensor->last_mode = sensor->current_mode; 307419a81c14SSteve Longerbeam 307519a81c14SSteve Longerbeam sensor->ae_target = 52; 307619a81c14SSteve Longerbeam 3077c3f3ba3eSHugues Fruchet /* optional indication of physical rotation of sensor */ 3078c3f3ba3eSHugues Fruchet ret = fwnode_property_read_u32(dev_fwnode(&client->dev), "rotation", 3079c3f3ba3eSHugues Fruchet &rotation); 3080c3f3ba3eSHugues Fruchet if (!ret) { 3081c3f3ba3eSHugues Fruchet switch (rotation) { 3082c3f3ba3eSHugues Fruchet case 180: 3083c3f3ba3eSHugues Fruchet sensor->upside_down = true; 30841771e9fbSGustavo A. R. Silva fallthrough; 3085c3f3ba3eSHugues Fruchet case 0: 3086c3f3ba3eSHugues Fruchet break; 3087c3f3ba3eSHugues Fruchet default: 3088c3f3ba3eSHugues Fruchet dev_warn(dev, "%u degrees rotation is not supported, ignoring...\n", 3089c3f3ba3eSHugues Fruchet rotation); 3090c3f3ba3eSHugues Fruchet } 3091c3f3ba3eSHugues Fruchet } 3092c3f3ba3eSHugues Fruchet 3093ce96bcf5SSakari Ailus endpoint = fwnode_graph_get_next_endpoint(dev_fwnode(&client->dev), 3094ce96bcf5SSakari Ailus NULL); 309519a81c14SSteve Longerbeam if (!endpoint) { 309619a81c14SSteve Longerbeam dev_err(dev, "endpoint node not found\n"); 309719a81c14SSteve Longerbeam return -EINVAL; 309819a81c14SSteve Longerbeam } 309919a81c14SSteve Longerbeam 310019a81c14SSteve Longerbeam ret = v4l2_fwnode_endpoint_parse(endpoint, &sensor->ep); 310119a81c14SSteve Longerbeam fwnode_handle_put(endpoint); 310219a81c14SSteve Longerbeam if (ret) { 310319a81c14SSteve Longerbeam dev_err(dev, "Could not parse endpoint\n"); 310419a81c14SSteve Longerbeam return ret; 310519a81c14SSteve Longerbeam } 310619a81c14SSteve Longerbeam 31072c61e48dSLad Prabhakar if (sensor->ep.bus_type != V4L2_MBUS_PARALLEL && 31082c61e48dSLad Prabhakar sensor->ep.bus_type != V4L2_MBUS_CSI2_DPHY && 31092c61e48dSLad Prabhakar sensor->ep.bus_type != V4L2_MBUS_BT656) { 31102c61e48dSLad Prabhakar dev_err(dev, "Unsupported bus type %d\n", sensor->ep.bus_type); 31112c61e48dSLad Prabhakar return -EINVAL; 31122c61e48dSLad Prabhakar } 31132c61e48dSLad Prabhakar 311419a81c14SSteve Longerbeam /* get system clock (xclk) */ 311519a81c14SSteve Longerbeam sensor->xclk = devm_clk_get(dev, "xclk"); 311619a81c14SSteve Longerbeam if (IS_ERR(sensor->xclk)) { 311719a81c14SSteve Longerbeam dev_err(dev, "failed to get xclk\n"); 311819a81c14SSteve Longerbeam return PTR_ERR(sensor->xclk); 311919a81c14SSteve Longerbeam } 312019a81c14SSteve Longerbeam 312119a81c14SSteve Longerbeam sensor->xclk_freq = clk_get_rate(sensor->xclk); 312219a81c14SSteve Longerbeam if (sensor->xclk_freq < OV5640_XCLK_MIN || 312319a81c14SSteve Longerbeam sensor->xclk_freq > OV5640_XCLK_MAX) { 312419a81c14SSteve Longerbeam dev_err(dev, "xclk frequency out of range: %d Hz\n", 312519a81c14SSteve Longerbeam sensor->xclk_freq); 312619a81c14SSteve Longerbeam return -EINVAL; 312719a81c14SSteve Longerbeam } 312819a81c14SSteve Longerbeam 312919a81c14SSteve Longerbeam /* request optional power down pin */ 313019a81c14SSteve Longerbeam sensor->pwdn_gpio = devm_gpiod_get_optional(dev, "powerdown", 313119a81c14SSteve Longerbeam GPIOD_OUT_HIGH); 31328791a102SFabio Estevam if (IS_ERR(sensor->pwdn_gpio)) 31338791a102SFabio Estevam return PTR_ERR(sensor->pwdn_gpio); 31348791a102SFabio Estevam 313519a81c14SSteve Longerbeam /* request optional reset pin */ 313619a81c14SSteve Longerbeam sensor->reset_gpio = devm_gpiod_get_optional(dev, "reset", 313719a81c14SSteve Longerbeam GPIOD_OUT_HIGH); 31388791a102SFabio Estevam if (IS_ERR(sensor->reset_gpio)) 31398791a102SFabio Estevam return PTR_ERR(sensor->reset_gpio); 314019a81c14SSteve Longerbeam 314119a81c14SSteve Longerbeam v4l2_i2c_subdev_init(&sensor->sd, client, &ov5640_subdev_ops); 314219a81c14SSteve Longerbeam 31432d18fbc5SAkinobu Mita sensor->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE | 31442d18fbc5SAkinobu Mita V4L2_SUBDEV_FL_HAS_EVENTS; 314519a81c14SSteve Longerbeam sensor->pad.flags = MEDIA_PAD_FL_SOURCE; 314619a81c14SSteve Longerbeam sensor->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR; 314719a81c14SSteve Longerbeam ret = media_entity_pads_init(&sensor->sd.entity, 1, &sensor->pad); 314819a81c14SSteve Longerbeam if (ret) 314919a81c14SSteve Longerbeam return ret; 315019a81c14SSteve Longerbeam 315119a81c14SSteve Longerbeam ret = ov5640_get_regulators(sensor); 315219a81c14SSteve Longerbeam if (ret) 315319a81c14SSteve Longerbeam return ret; 315419a81c14SSteve Longerbeam 315519a81c14SSteve Longerbeam mutex_init(&sensor->lock); 315619a81c14SSteve Longerbeam 31570f7acb52SHugues Fruchet ret = ov5640_check_chip_id(sensor); 31580f7acb52SHugues Fruchet if (ret) 31590f7acb52SHugues Fruchet goto entity_cleanup; 31600f7acb52SHugues Fruchet 316119a81c14SSteve Longerbeam ret = ov5640_init_controls(sensor); 316219a81c14SSteve Longerbeam if (ret) 316319a81c14SSteve Longerbeam goto entity_cleanup; 316419a81c14SSteve Longerbeam 31652f1fc5efSLuca Weiss ret = v4l2_async_register_subdev_sensor_common(&sensor->sd); 316619a81c14SSteve Longerbeam if (ret) 316719a81c14SSteve Longerbeam goto free_ctrls; 316819a81c14SSteve Longerbeam 316919a81c14SSteve Longerbeam return 0; 317019a81c14SSteve Longerbeam 317119a81c14SSteve Longerbeam free_ctrls: 317219a81c14SSteve Longerbeam v4l2_ctrl_handler_free(&sensor->ctrls.handler); 317319a81c14SSteve Longerbeam entity_cleanup: 317419a81c14SSteve Longerbeam media_entity_cleanup(&sensor->sd.entity); 3175bfcba38dSTomi Valkeinen mutex_destroy(&sensor->lock); 317619a81c14SSteve Longerbeam return ret; 317719a81c14SSteve Longerbeam } 317819a81c14SSteve Longerbeam 317919a81c14SSteve Longerbeam static int ov5640_remove(struct i2c_client *client) 318019a81c14SSteve Longerbeam { 318119a81c14SSteve Longerbeam struct v4l2_subdev *sd = i2c_get_clientdata(client); 318219a81c14SSteve Longerbeam struct ov5640_dev *sensor = to_ov5640_dev(sd); 318319a81c14SSteve Longerbeam 318419a81c14SSteve Longerbeam v4l2_async_unregister_subdev(&sensor->sd); 318519a81c14SSteve Longerbeam media_entity_cleanup(&sensor->sd.entity); 318619a81c14SSteve Longerbeam v4l2_ctrl_handler_free(&sensor->ctrls.handler); 3187bfcba38dSTomi Valkeinen mutex_destroy(&sensor->lock); 318819a81c14SSteve Longerbeam 318919a81c14SSteve Longerbeam return 0; 319019a81c14SSteve Longerbeam } 319119a81c14SSteve Longerbeam 319219a81c14SSteve Longerbeam static const struct i2c_device_id ov5640_id[] = { 319319a81c14SSteve Longerbeam {"ov5640", 0}, 319419a81c14SSteve Longerbeam {}, 319519a81c14SSteve Longerbeam }; 319619a81c14SSteve Longerbeam MODULE_DEVICE_TABLE(i2c, ov5640_id); 319719a81c14SSteve Longerbeam 319819a81c14SSteve Longerbeam static const struct of_device_id ov5640_dt_ids[] = { 319919a81c14SSteve Longerbeam { .compatible = "ovti,ov5640" }, 320019a81c14SSteve Longerbeam { /* sentinel */ } 320119a81c14SSteve Longerbeam }; 320219a81c14SSteve Longerbeam MODULE_DEVICE_TABLE(of, ov5640_dt_ids); 320319a81c14SSteve Longerbeam 320419a81c14SSteve Longerbeam static struct i2c_driver ov5640_i2c_driver = { 320519a81c14SSteve Longerbeam .driver = { 320619a81c14SSteve Longerbeam .name = "ov5640", 320719a81c14SSteve Longerbeam .of_match_table = ov5640_dt_ids, 320819a81c14SSteve Longerbeam }, 320919a81c14SSteve Longerbeam .id_table = ov5640_id, 3210e6714993SKieran Bingham .probe_new = ov5640_probe, 321119a81c14SSteve Longerbeam .remove = ov5640_remove, 321219a81c14SSteve Longerbeam }; 321319a81c14SSteve Longerbeam 321419a81c14SSteve Longerbeam module_i2c_driver(ov5640_i2c_driver); 321519a81c14SSteve Longerbeam 321619a81c14SSteve Longerbeam MODULE_DESCRIPTION("OV5640 MIPI Camera Subdev Driver"); 321719a81c14SSteve Longerbeam MODULE_LICENSE("GPL"); 3218