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 12122845bf2SJacopo Mondi enum ov5640_pixel_rate_id { 12222845bf2SJacopo Mondi OV5640_PIXEL_RATE_168M, 12322845bf2SJacopo Mondi OV5640_PIXEL_RATE_148M, 12422845bf2SJacopo Mondi OV5640_PIXEL_RATE_124M, 12522845bf2SJacopo Mondi OV5640_PIXEL_RATE_96M, 12622845bf2SJacopo Mondi OV5640_PIXEL_RATE_48M, 12722845bf2SJacopo Mondi OV5640_NUM_PIXEL_RATES, 12822845bf2SJacopo Mondi }; 12922845bf2SJacopo Mondi 13022845bf2SJacopo Mondi /* 13122845bf2SJacopo Mondi * The chip manual suggests 24/48/96/192 MHz pixel clocks. 13222845bf2SJacopo Mondi * 13322845bf2SJacopo Mondi * 192MHz exceeds the sysclk limits; use 168MHz as maximum pixel rate for 13422845bf2SJacopo Mondi * full resolution mode @15 FPS. 13522845bf2SJacopo Mondi */ 13622845bf2SJacopo Mondi static const u32 ov5640_pixel_rates[] = { 13722845bf2SJacopo Mondi [OV5640_PIXEL_RATE_168M] = 168000000, 13822845bf2SJacopo Mondi [OV5640_PIXEL_RATE_148M] = 148000000, 13922845bf2SJacopo Mondi [OV5640_PIXEL_RATE_124M] = 124000000, 14022845bf2SJacopo Mondi [OV5640_PIXEL_RATE_96M] = 96000000, 14122845bf2SJacopo Mondi [OV5640_PIXEL_RATE_48M] = 48000000, 14222845bf2SJacopo Mondi }; 14322845bf2SJacopo Mondi 144b7ed3abdSLoic Poulain enum ov5640_format_mux { 145b7ed3abdSLoic Poulain OV5640_FMT_MUX_YUV422 = 0, 146b7ed3abdSLoic Poulain OV5640_FMT_MUX_RGB, 147b7ed3abdSLoic Poulain OV5640_FMT_MUX_DITHER, 148b7ed3abdSLoic Poulain OV5640_FMT_MUX_RAW_DPC, 149b7ed3abdSLoic Poulain OV5640_FMT_MUX_SNR_RAW, 150b7ed3abdSLoic Poulain OV5640_FMT_MUX_RAW_CIP, 151b7ed3abdSLoic Poulain }; 152b7ed3abdSLoic Poulain 153*2d7671f6SJacopo Mondi static const struct ov5640_pixfmt { 154e3ee691dSHugues Fruchet u32 code; 155e3ee691dSHugues Fruchet u32 colorspace; 156*2d7671f6SJacopo Mondi u8 bpp; 157*2d7671f6SJacopo Mondi } ov5640_formats[] = { 158*2d7671f6SJacopo Mondi { 159*2d7671f6SJacopo Mondi .code = MEDIA_BUS_FMT_JPEG_1X8, 160*2d7671f6SJacopo Mondi .colorspace = V4L2_COLORSPACE_JPEG, 161*2d7671f6SJacopo Mondi .bpp = 16, 162*2d7671f6SJacopo Mondi }, { 163*2d7671f6SJacopo Mondi .code = MEDIA_BUS_FMT_UYVY8_2X8, 164*2d7671f6SJacopo Mondi .colorspace = V4L2_COLORSPACE_SRGB, 165*2d7671f6SJacopo Mondi .bpp = 16, 166*2d7671f6SJacopo Mondi }, { 167*2d7671f6SJacopo Mondi .code = MEDIA_BUS_FMT_UYVY8_1X16, 168*2d7671f6SJacopo Mondi .colorspace = V4L2_COLORSPACE_SRGB, 169*2d7671f6SJacopo Mondi .bpp = 16, 170*2d7671f6SJacopo Mondi }, { 171*2d7671f6SJacopo Mondi .code = MEDIA_BUS_FMT_YUYV8_2X8, 172*2d7671f6SJacopo Mondi .colorspace = V4L2_COLORSPACE_SRGB, 173*2d7671f6SJacopo Mondi .bpp = 16, 174*2d7671f6SJacopo Mondi }, { 175*2d7671f6SJacopo Mondi .code = MEDIA_BUS_FMT_YUYV8_1X16, 176*2d7671f6SJacopo Mondi .colorspace = V4L2_COLORSPACE_SRGB, 177*2d7671f6SJacopo Mondi .bpp = 16, 178*2d7671f6SJacopo Mondi }, { 179*2d7671f6SJacopo Mondi .code = MEDIA_BUS_FMT_RGB565_2X8_LE, 180*2d7671f6SJacopo Mondi .colorspace = V4L2_COLORSPACE_SRGB, 181*2d7671f6SJacopo Mondi .bpp = 16, 182*2d7671f6SJacopo Mondi }, { 183*2d7671f6SJacopo Mondi .code = MEDIA_BUS_FMT_RGB565_2X8_BE, 184*2d7671f6SJacopo Mondi .colorspace = V4L2_COLORSPACE_SRGB, 185*2d7671f6SJacopo Mondi .bpp = 16, 186*2d7671f6SJacopo Mondi }, { 187*2d7671f6SJacopo Mondi .code = MEDIA_BUS_FMT_SBGGR8_1X8, 188*2d7671f6SJacopo Mondi .colorspace = V4L2_COLORSPACE_SRGB, 189*2d7671f6SJacopo Mondi .bpp = 8, 190*2d7671f6SJacopo Mondi }, { 191*2d7671f6SJacopo Mondi .code = MEDIA_BUS_FMT_SGBRG8_1X8, 192*2d7671f6SJacopo Mondi .colorspace = V4L2_COLORSPACE_SRGB, 193*2d7671f6SJacopo Mondi .bpp = 8 194*2d7671f6SJacopo Mondi }, { 195*2d7671f6SJacopo Mondi .code = MEDIA_BUS_FMT_SGRBG8_1X8, 196*2d7671f6SJacopo Mondi .colorspace = V4L2_COLORSPACE_SRGB, 197*2d7671f6SJacopo Mondi .bpp = 8, 198*2d7671f6SJacopo Mondi }, { 199*2d7671f6SJacopo Mondi .code = MEDIA_BUS_FMT_SRGGB8_1X8, 200*2d7671f6SJacopo Mondi .colorspace = V4L2_COLORSPACE_SRGB, 201*2d7671f6SJacopo Mondi .bpp = 8, 202*2d7671f6SJacopo Mondi }, 203e3ee691dSHugues Fruchet }; 204e3ee691dSHugues Fruchet 20519a81c14SSteve Longerbeam /* 20619a81c14SSteve Longerbeam * FIXME: remove this when a subdev API becomes available 20719a81c14SSteve Longerbeam * to set the MIPI CSI-2 virtual channel. 20819a81c14SSteve Longerbeam */ 20919a81c14SSteve Longerbeam static unsigned int virtual_channel; 2108670d70aSHugues Fruchet module_param(virtual_channel, uint, 0444); 21119a81c14SSteve Longerbeam MODULE_PARM_DESC(virtual_channel, 21219a81c14SSteve Longerbeam "MIPI CSI-2 virtual channel (0..3), default 0"); 21319a81c14SSteve Longerbeam 21419a81c14SSteve Longerbeam static const int ov5640_framerates[] = { 21519a81c14SSteve Longerbeam [OV5640_15_FPS] = 15, 21619a81c14SSteve Longerbeam [OV5640_30_FPS] = 30, 217e823fb16SMaxime Ripard [OV5640_60_FPS] = 60, 21819a81c14SSteve Longerbeam }; 21919a81c14SSteve Longerbeam 22019a81c14SSteve Longerbeam /* regulator supplies */ 22119a81c14SSteve Longerbeam static const char * const ov5640_supply_name[] = { 22241d8d7f5SHugues Fruchet "DOVDD", /* Digital I/O (1.8V) supply */ 22319a81c14SSteve Longerbeam "AVDD", /* Analog (2.8V) supply */ 22424c8ac89SFabio Estevam "DVDD", /* Digital Core (1.5V) supply */ 22519a81c14SSteve Longerbeam }; 22619a81c14SSteve Longerbeam 22719a81c14SSteve Longerbeam #define OV5640_NUM_SUPPLIES ARRAY_SIZE(ov5640_supply_name) 22819a81c14SSteve Longerbeam 22919a81c14SSteve Longerbeam /* 23019a81c14SSteve Longerbeam * Image size under 1280 * 960 are SUBSAMPLING 23119a81c14SSteve Longerbeam * Image size upper 1280 * 960 are SCALING 23219a81c14SSteve Longerbeam */ 23319a81c14SSteve Longerbeam enum ov5640_downsize_mode { 23419a81c14SSteve Longerbeam SUBSAMPLING, 23519a81c14SSteve Longerbeam SCALING, 23619a81c14SSteve Longerbeam }; 23719a81c14SSteve Longerbeam 23819a81c14SSteve Longerbeam struct reg_value { 23919a81c14SSteve Longerbeam u16 reg_addr; 24019a81c14SSteve Longerbeam u8 val; 24119a81c14SSteve Longerbeam u8 mask; 24219a81c14SSteve Longerbeam u32 delay_ms; 24319a81c14SSteve Longerbeam }; 24419a81c14SSteve Longerbeam 24519a81c14SSteve Longerbeam struct ov5640_mode_info { 24619a81c14SSteve Longerbeam enum ov5640_mode_id id; 24719a81c14SSteve Longerbeam enum ov5640_downsize_mode dn_mode; 24822845bf2SJacopo Mondi enum ov5640_pixel_rate_id pixel_rate; 249dba13a0bSMaxime Ripard u32 hact; 250476dec01SMaxime Ripard u32 htot; 251dba13a0bSMaxime Ripard u32 vact; 252476dec01SMaxime Ripard u32 vtot; 25319a81c14SSteve Longerbeam const struct reg_value *reg_data; 25419a81c14SSteve Longerbeam u32 reg_data_size; 2555554c80eSAdam Ford u32 max_fps; 25619a81c14SSteve Longerbeam }; 25719a81c14SSteve Longerbeam 25819a81c14SSteve Longerbeam struct ov5640_ctrls { 25919a81c14SSteve Longerbeam struct v4l2_ctrl_handler handler; 260cc196e48SBenoit Parrot struct v4l2_ctrl *pixel_rate; 26119a81c14SSteve Longerbeam struct { 26219a81c14SSteve Longerbeam struct v4l2_ctrl *auto_exp; 26319a81c14SSteve Longerbeam struct v4l2_ctrl *exposure; 26419a81c14SSteve Longerbeam }; 26519a81c14SSteve Longerbeam struct { 26619a81c14SSteve Longerbeam struct v4l2_ctrl *auto_wb; 26719a81c14SSteve Longerbeam struct v4l2_ctrl *blue_balance; 26819a81c14SSteve Longerbeam struct v4l2_ctrl *red_balance; 26919a81c14SSteve Longerbeam }; 27019a81c14SSteve Longerbeam struct { 27119a81c14SSteve Longerbeam struct v4l2_ctrl *auto_gain; 27219a81c14SSteve Longerbeam struct v4l2_ctrl *gain; 27319a81c14SSteve Longerbeam }; 27419a81c14SSteve Longerbeam struct v4l2_ctrl *brightness; 2751068fecaSMylène Josserand struct v4l2_ctrl *light_freq; 27619a81c14SSteve Longerbeam struct v4l2_ctrl *saturation; 27719a81c14SSteve Longerbeam struct v4l2_ctrl *contrast; 27819a81c14SSteve Longerbeam struct v4l2_ctrl *hue; 27919a81c14SSteve Longerbeam struct v4l2_ctrl *test_pattern; 280ce85705aSHugues Fruchet struct v4l2_ctrl *hflip; 281ce85705aSHugues Fruchet struct v4l2_ctrl *vflip; 28219a81c14SSteve Longerbeam }; 28319a81c14SSteve Longerbeam 28419a81c14SSteve Longerbeam struct ov5640_dev { 28519a81c14SSteve Longerbeam struct i2c_client *i2c_client; 28619a81c14SSteve Longerbeam struct v4l2_subdev sd; 28719a81c14SSteve Longerbeam struct media_pad pad; 28819a81c14SSteve Longerbeam struct v4l2_fwnode_endpoint ep; /* the parsed DT endpoint info */ 28919a81c14SSteve Longerbeam struct clk *xclk; /* system clock to OV5640 */ 29019a81c14SSteve Longerbeam u32 xclk_freq; 29119a81c14SSteve Longerbeam 29219a81c14SSteve Longerbeam struct regulator_bulk_data supplies[OV5640_NUM_SUPPLIES]; 29319a81c14SSteve Longerbeam struct gpio_desc *reset_gpio; 29419a81c14SSteve Longerbeam struct gpio_desc *pwdn_gpio; 295c3f3ba3eSHugues Fruchet bool upside_down; 29619a81c14SSteve Longerbeam 29719a81c14SSteve Longerbeam /* lock to protect all members below */ 29819a81c14SSteve Longerbeam struct mutex lock; 29919a81c14SSteve Longerbeam 30019a81c14SSteve Longerbeam int power_count; 30119a81c14SSteve Longerbeam 30219a81c14SSteve Longerbeam struct v4l2_mbus_framefmt fmt; 303fb98e29fSHugues Fruchet bool pending_fmt_change; 30419a81c14SSteve Longerbeam 30519a81c14SSteve Longerbeam const struct ov5640_mode_info *current_mode; 306985cdcb0SHugues Fruchet const struct ov5640_mode_info *last_mode; 30719a81c14SSteve Longerbeam enum ov5640_frame_rate current_fr; 30819a81c14SSteve Longerbeam struct v4l2_fract frame_interval; 30919a81c14SSteve Longerbeam 31019a81c14SSteve Longerbeam struct ov5640_ctrls ctrls; 31119a81c14SSteve Longerbeam 31219a81c14SSteve Longerbeam u32 prev_sysclk, prev_hts; 31319a81c14SSteve Longerbeam u32 ae_low, ae_high, ae_target; 31419a81c14SSteve Longerbeam 31519a81c14SSteve Longerbeam bool pending_mode_change; 31619a81c14SSteve Longerbeam bool streaming; 31719a81c14SSteve Longerbeam }; 31819a81c14SSteve Longerbeam 31919a81c14SSteve Longerbeam static inline struct ov5640_dev *to_ov5640_dev(struct v4l2_subdev *sd) 32019a81c14SSteve Longerbeam { 32119a81c14SSteve Longerbeam return container_of(sd, struct ov5640_dev, sd); 32219a81c14SSteve Longerbeam } 32319a81c14SSteve Longerbeam 32419a81c14SSteve Longerbeam static inline struct v4l2_subdev *ctrl_to_sd(struct v4l2_ctrl *ctrl) 32519a81c14SSteve Longerbeam { 32619a81c14SSteve Longerbeam return &container_of(ctrl->handler, struct ov5640_dev, 32719a81c14SSteve Longerbeam ctrls.handler)->sd; 32819a81c14SSteve Longerbeam } 32919a81c14SSteve Longerbeam 3308e823f5cSJacopo Mondi static inline bool ov5640_is_csi2(const struct ov5640_dev *sensor) 3318e823f5cSJacopo Mondi { 3328e823f5cSJacopo Mondi return sensor->ep.bus_type == V4L2_MBUS_CSI2_DPHY; 3338e823f5cSJacopo Mondi } 3348e823f5cSJacopo Mondi 33519a81c14SSteve Longerbeam /* 33619a81c14SSteve Longerbeam * FIXME: all of these register tables are likely filled with 33719a81c14SSteve Longerbeam * entries that set the register to their power-on default values, 33819a81c14SSteve Longerbeam * and which are otherwise not touched by this driver. Those entries 33919a81c14SSteve Longerbeam * should be identified and removed to speed register load time 34019a81c14SSteve Longerbeam * over i2c. 34119a81c14SSteve Longerbeam */ 342fb98e29fSHugues Fruchet /* YUV422 UYVY VGA@30fps */ 34319a81c14SSteve Longerbeam static const struct reg_value ov5640_init_setting_30fps_VGA[] = { 34419a81c14SSteve Longerbeam {0x3103, 0x11, 0, 0}, {0x3008, 0x82, 0, 5}, {0x3008, 0x42, 0, 0}, 345576f5d4bSLad Prabhakar {0x3103, 0x03, 0, 0}, {0x3630, 0x36, 0, 0}, 34619a81c14SSteve Longerbeam {0x3631, 0x0e, 0, 0}, {0x3632, 0xe2, 0, 0}, {0x3633, 0x12, 0, 0}, 34719a81c14SSteve Longerbeam {0x3621, 0xe0, 0, 0}, {0x3704, 0xa0, 0, 0}, {0x3703, 0x5a, 0, 0}, 34819a81c14SSteve Longerbeam {0x3715, 0x78, 0, 0}, {0x3717, 0x01, 0, 0}, {0x370b, 0x60, 0, 0}, 34919a81c14SSteve Longerbeam {0x3705, 0x1a, 0, 0}, {0x3905, 0x02, 0, 0}, {0x3906, 0x10, 0, 0}, 35019a81c14SSteve Longerbeam {0x3901, 0x0a, 0, 0}, {0x3731, 0x12, 0, 0}, {0x3600, 0x08, 0, 0}, 35119a81c14SSteve Longerbeam {0x3601, 0x33, 0, 0}, {0x302d, 0x60, 0, 0}, {0x3620, 0x52, 0, 0}, 35219a81c14SSteve Longerbeam {0x371b, 0x20, 0, 0}, {0x471c, 0x50, 0, 0}, {0x3a13, 0x43, 0, 0}, 35319a81c14SSteve Longerbeam {0x3a18, 0x00, 0, 0}, {0x3a19, 0xf8, 0, 0}, {0x3635, 0x13, 0, 0}, 35419a81c14SSteve Longerbeam {0x3636, 0x03, 0, 0}, {0x3634, 0x40, 0, 0}, {0x3622, 0x01, 0, 0}, 35519a81c14SSteve Longerbeam {0x3c01, 0xa4, 0, 0}, {0x3c04, 0x28, 0, 0}, {0x3c05, 0x98, 0, 0}, 35619a81c14SSteve Longerbeam {0x3c06, 0x00, 0, 0}, {0x3c07, 0x08, 0, 0}, {0x3c08, 0x00, 0, 0}, 35719a81c14SSteve Longerbeam {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0}, 35819a81c14SSteve Longerbeam {0x3820, 0x41, 0, 0}, {0x3821, 0x07, 0, 0}, {0x3814, 0x31, 0, 0}, 35919a81c14SSteve Longerbeam {0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0}, 36019a81c14SSteve Longerbeam {0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0}, 36119a81c14SSteve Longerbeam {0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0}, 362476dec01SMaxime Ripard {0x3810, 0x00, 0, 0}, 36319a81c14SSteve Longerbeam {0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0}, 36419a81c14SSteve Longerbeam {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0}, 36519a81c14SSteve Longerbeam {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0}, 36619a81c14SSteve Longerbeam {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0}, 36719a81c14SSteve Longerbeam {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0}, 36819a81c14SSteve Longerbeam {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0}, 36919a81c14SSteve Longerbeam {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x3000, 0x00, 0, 0}, 37019a81c14SSteve Longerbeam {0x3002, 0x1c, 0, 0}, {0x3004, 0xff, 0, 0}, {0x3006, 0xc3, 0, 0}, 371aa4bb8b8SJacopo Mondi {0x302e, 0x08, 0, 0}, {0x4300, 0x3f, 0, 0}, 3722b5c18f9SChen-Yu Tsai {0x501f, 0x00, 0, 0}, {0x4407, 0x04, 0, 0}, 37319a81c14SSteve Longerbeam {0x440e, 0x00, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0}, 374aa4bb8b8SJacopo Mondi {0x4837, 0x0a, 0, 0}, {0x3824, 0x02, 0, 0}, 37519a81c14SSteve Longerbeam {0x5000, 0xa7, 0, 0}, {0x5001, 0xa3, 0, 0}, {0x5180, 0xff, 0, 0}, 37619a81c14SSteve Longerbeam {0x5181, 0xf2, 0, 0}, {0x5182, 0x00, 0, 0}, {0x5183, 0x14, 0, 0}, 37719a81c14SSteve Longerbeam {0x5184, 0x25, 0, 0}, {0x5185, 0x24, 0, 0}, {0x5186, 0x09, 0, 0}, 37819a81c14SSteve Longerbeam {0x5187, 0x09, 0, 0}, {0x5188, 0x09, 0, 0}, {0x5189, 0x88, 0, 0}, 37919a81c14SSteve Longerbeam {0x518a, 0x54, 0, 0}, {0x518b, 0xee, 0, 0}, {0x518c, 0xb2, 0, 0}, 38019a81c14SSteve Longerbeam {0x518d, 0x50, 0, 0}, {0x518e, 0x34, 0, 0}, {0x518f, 0x6b, 0, 0}, 38119a81c14SSteve Longerbeam {0x5190, 0x46, 0, 0}, {0x5191, 0xf8, 0, 0}, {0x5192, 0x04, 0, 0}, 38219a81c14SSteve Longerbeam {0x5193, 0x70, 0, 0}, {0x5194, 0xf0, 0, 0}, {0x5195, 0xf0, 0, 0}, 38319a81c14SSteve Longerbeam {0x5196, 0x03, 0, 0}, {0x5197, 0x01, 0, 0}, {0x5198, 0x04, 0, 0}, 38419a81c14SSteve Longerbeam {0x5199, 0x6c, 0, 0}, {0x519a, 0x04, 0, 0}, {0x519b, 0x00, 0, 0}, 38519a81c14SSteve Longerbeam {0x519c, 0x09, 0, 0}, {0x519d, 0x2b, 0, 0}, {0x519e, 0x38, 0, 0}, 38619a81c14SSteve Longerbeam {0x5381, 0x1e, 0, 0}, {0x5382, 0x5b, 0, 0}, {0x5383, 0x08, 0, 0}, 38719a81c14SSteve Longerbeam {0x5384, 0x0a, 0, 0}, {0x5385, 0x7e, 0, 0}, {0x5386, 0x88, 0, 0}, 38819a81c14SSteve Longerbeam {0x5387, 0x7c, 0, 0}, {0x5388, 0x6c, 0, 0}, {0x5389, 0x10, 0, 0}, 38919a81c14SSteve Longerbeam {0x538a, 0x01, 0, 0}, {0x538b, 0x98, 0, 0}, {0x5300, 0x08, 0, 0}, 39019a81c14SSteve Longerbeam {0x5301, 0x30, 0, 0}, {0x5302, 0x10, 0, 0}, {0x5303, 0x00, 0, 0}, 39119a81c14SSteve Longerbeam {0x5304, 0x08, 0, 0}, {0x5305, 0x30, 0, 0}, {0x5306, 0x08, 0, 0}, 39219a81c14SSteve Longerbeam {0x5307, 0x16, 0, 0}, {0x5309, 0x08, 0, 0}, {0x530a, 0x30, 0, 0}, 39319a81c14SSteve Longerbeam {0x530b, 0x04, 0, 0}, {0x530c, 0x06, 0, 0}, {0x5480, 0x01, 0, 0}, 39419a81c14SSteve Longerbeam {0x5481, 0x08, 0, 0}, {0x5482, 0x14, 0, 0}, {0x5483, 0x28, 0, 0}, 39519a81c14SSteve Longerbeam {0x5484, 0x51, 0, 0}, {0x5485, 0x65, 0, 0}, {0x5486, 0x71, 0, 0}, 39619a81c14SSteve Longerbeam {0x5487, 0x7d, 0, 0}, {0x5488, 0x87, 0, 0}, {0x5489, 0x91, 0, 0}, 39719a81c14SSteve Longerbeam {0x548a, 0x9a, 0, 0}, {0x548b, 0xaa, 0, 0}, {0x548c, 0xb8, 0, 0}, 39819a81c14SSteve Longerbeam {0x548d, 0xcd, 0, 0}, {0x548e, 0xdd, 0, 0}, {0x548f, 0xea, 0, 0}, 39919a81c14SSteve Longerbeam {0x5490, 0x1d, 0, 0}, {0x5580, 0x02, 0, 0}, {0x5583, 0x40, 0, 0}, 40019a81c14SSteve Longerbeam {0x5584, 0x10, 0, 0}, {0x5589, 0x10, 0, 0}, {0x558a, 0x00, 0, 0}, 40119a81c14SSteve Longerbeam {0x558b, 0xf8, 0, 0}, {0x5800, 0x23, 0, 0}, {0x5801, 0x14, 0, 0}, 40219a81c14SSteve Longerbeam {0x5802, 0x0f, 0, 0}, {0x5803, 0x0f, 0, 0}, {0x5804, 0x12, 0, 0}, 40319a81c14SSteve Longerbeam {0x5805, 0x26, 0, 0}, {0x5806, 0x0c, 0, 0}, {0x5807, 0x08, 0, 0}, 40419a81c14SSteve Longerbeam {0x5808, 0x05, 0, 0}, {0x5809, 0x05, 0, 0}, {0x580a, 0x08, 0, 0}, 40519a81c14SSteve Longerbeam {0x580b, 0x0d, 0, 0}, {0x580c, 0x08, 0, 0}, {0x580d, 0x03, 0, 0}, 40619a81c14SSteve Longerbeam {0x580e, 0x00, 0, 0}, {0x580f, 0x00, 0, 0}, {0x5810, 0x03, 0, 0}, 40719a81c14SSteve Longerbeam {0x5811, 0x09, 0, 0}, {0x5812, 0x07, 0, 0}, {0x5813, 0x03, 0, 0}, 40819a81c14SSteve Longerbeam {0x5814, 0x00, 0, 0}, {0x5815, 0x01, 0, 0}, {0x5816, 0x03, 0, 0}, 40919a81c14SSteve Longerbeam {0x5817, 0x08, 0, 0}, {0x5818, 0x0d, 0, 0}, {0x5819, 0x08, 0, 0}, 41019a81c14SSteve Longerbeam {0x581a, 0x05, 0, 0}, {0x581b, 0x06, 0, 0}, {0x581c, 0x08, 0, 0}, 41119a81c14SSteve Longerbeam {0x581d, 0x0e, 0, 0}, {0x581e, 0x29, 0, 0}, {0x581f, 0x17, 0, 0}, 41219a81c14SSteve Longerbeam {0x5820, 0x11, 0, 0}, {0x5821, 0x11, 0, 0}, {0x5822, 0x15, 0, 0}, 41319a81c14SSteve Longerbeam {0x5823, 0x28, 0, 0}, {0x5824, 0x46, 0, 0}, {0x5825, 0x26, 0, 0}, 41419a81c14SSteve Longerbeam {0x5826, 0x08, 0, 0}, {0x5827, 0x26, 0, 0}, {0x5828, 0x64, 0, 0}, 41519a81c14SSteve Longerbeam {0x5829, 0x26, 0, 0}, {0x582a, 0x24, 0, 0}, {0x582b, 0x22, 0, 0}, 41619a81c14SSteve Longerbeam {0x582c, 0x24, 0, 0}, {0x582d, 0x24, 0, 0}, {0x582e, 0x06, 0, 0}, 41719a81c14SSteve Longerbeam {0x582f, 0x22, 0, 0}, {0x5830, 0x40, 0, 0}, {0x5831, 0x42, 0, 0}, 41819a81c14SSteve Longerbeam {0x5832, 0x24, 0, 0}, {0x5833, 0x26, 0, 0}, {0x5834, 0x24, 0, 0}, 41919a81c14SSteve Longerbeam {0x5835, 0x22, 0, 0}, {0x5836, 0x22, 0, 0}, {0x5837, 0x26, 0, 0}, 42019a81c14SSteve Longerbeam {0x5838, 0x44, 0, 0}, {0x5839, 0x24, 0, 0}, {0x583a, 0x26, 0, 0}, 42119a81c14SSteve Longerbeam {0x583b, 0x28, 0, 0}, {0x583c, 0x42, 0, 0}, {0x583d, 0xce, 0, 0}, 42219a81c14SSteve Longerbeam {0x5025, 0x00, 0, 0}, {0x3a0f, 0x30, 0, 0}, {0x3a10, 0x28, 0, 0}, 42319a81c14SSteve Longerbeam {0x3a1b, 0x30, 0, 0}, {0x3a1e, 0x26, 0, 0}, {0x3a11, 0x60, 0, 0}, 42419a81c14SSteve Longerbeam {0x3a1f, 0x14, 0, 0}, {0x3008, 0x02, 0, 0}, {0x3c00, 0x04, 0, 300}, 42519a81c14SSteve Longerbeam }; 42619a81c14SSteve Longerbeam 427086c25f8SMaxime Ripard static const struct reg_value ov5640_setting_VGA_640_480[] = { 428c14d107eSMaxime Ripard {0x3c07, 0x08, 0, 0}, 42919a81c14SSteve Longerbeam {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0}, 430ce85705aSHugues Fruchet {0x3814, 0x31, 0, 0}, 43119a81c14SSteve Longerbeam {0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0}, 43219a81c14SSteve Longerbeam {0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0}, 43319a81c14SSteve Longerbeam {0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0}, 434476dec01SMaxime Ripard {0x3810, 0x00, 0, 0}, 43519a81c14SSteve Longerbeam {0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0}, 43619a81c14SSteve Longerbeam {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0}, 43719a81c14SSteve Longerbeam {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0}, 43819a81c14SSteve Longerbeam {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0}, 43919a81c14SSteve Longerbeam {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0}, 44019a81c14SSteve Longerbeam {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0}, 4412b5c18f9SChen-Yu Tsai {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, 44219a81c14SSteve Longerbeam {0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0}, 44319a81c14SSteve Longerbeam {0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0}, 44419a81c14SSteve Longerbeam }; 44519a81c14SSteve Longerbeam 446086c25f8SMaxime Ripard static const struct reg_value ov5640_setting_XGA_1024_768[] = { 447c14d107eSMaxime Ripard {0x3c07, 0x08, 0, 0}, 44819a81c14SSteve Longerbeam {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0}, 449ce85705aSHugues Fruchet {0x3814, 0x31, 0, 0}, 45019a81c14SSteve Longerbeam {0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0}, 45119a81c14SSteve Longerbeam {0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0}, 45219a81c14SSteve Longerbeam {0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0}, 453476dec01SMaxime Ripard {0x3810, 0x00, 0, 0}, 45419a81c14SSteve Longerbeam {0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0}, 45519a81c14SSteve Longerbeam {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0}, 45619a81c14SSteve Longerbeam {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0}, 45719a81c14SSteve Longerbeam {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0}, 45819a81c14SSteve Longerbeam {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0}, 45919a81c14SSteve Longerbeam {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0}, 4602b5c18f9SChen-Yu Tsai {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, 46119a81c14SSteve Longerbeam {0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0}, 46286633417SMaxime Ripard {0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0}, 46319a81c14SSteve Longerbeam }; 46419a81c14SSteve Longerbeam 465086c25f8SMaxime Ripard static const struct reg_value ov5640_setting_QVGA_320_240[] = { 466c14d107eSMaxime Ripard {0x3c07, 0x08, 0, 0}, 46719a81c14SSteve Longerbeam {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0}, 468ce85705aSHugues Fruchet {0x3814, 0x31, 0, 0}, 46919a81c14SSteve Longerbeam {0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0}, 47019a81c14SSteve Longerbeam {0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0}, 47119a81c14SSteve Longerbeam {0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0}, 472476dec01SMaxime Ripard {0x3810, 0x00, 0, 0}, 47319a81c14SSteve Longerbeam {0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0}, 47419a81c14SSteve Longerbeam {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0}, 47519a81c14SSteve Longerbeam {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0}, 47619a81c14SSteve Longerbeam {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0}, 47719a81c14SSteve Longerbeam {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0}, 47819a81c14SSteve Longerbeam {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0}, 4792b5c18f9SChen-Yu Tsai {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, 48019a81c14SSteve Longerbeam {0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0}, 48119a81c14SSteve Longerbeam {0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0}, 48219a81c14SSteve Longerbeam }; 48319a81c14SSteve Longerbeam 48432ea5e05SHugues Fruchet static const struct reg_value ov5640_setting_QQVGA_160_120[] = { 48532ea5e05SHugues Fruchet {0x3c07, 0x08, 0, 0}, 48632ea5e05SHugues Fruchet {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0}, 48732ea5e05SHugues Fruchet {0x3814, 0x31, 0, 0}, 48832ea5e05SHugues Fruchet {0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0}, 48932ea5e05SHugues Fruchet {0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0}, 49032ea5e05SHugues Fruchet {0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0}, 49132ea5e05SHugues Fruchet {0x3810, 0x00, 0, 0}, 49232ea5e05SHugues Fruchet {0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0}, 49332ea5e05SHugues Fruchet {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0}, 49432ea5e05SHugues Fruchet {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0}, 49532ea5e05SHugues Fruchet {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0}, 49632ea5e05SHugues Fruchet {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0}, 49732ea5e05SHugues Fruchet {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0}, 49832ea5e05SHugues Fruchet {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, 49932ea5e05SHugues Fruchet {0x4407, 0x04, 0, 0}, {0x5001, 0xa3, 0, 0}, 50032ea5e05SHugues Fruchet }; 50132ea5e05SHugues Fruchet 502086c25f8SMaxime Ripard static const struct reg_value ov5640_setting_QCIF_176_144[] = { 503c14d107eSMaxime Ripard {0x3c07, 0x08, 0, 0}, 50419a81c14SSteve Longerbeam {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0}, 505ce85705aSHugues Fruchet {0x3814, 0x31, 0, 0}, 50619a81c14SSteve Longerbeam {0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0}, 50719a81c14SSteve Longerbeam {0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0}, 50819a81c14SSteve Longerbeam {0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0}, 509476dec01SMaxime Ripard {0x3810, 0x00, 0, 0}, 51019a81c14SSteve Longerbeam {0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0}, 51119a81c14SSteve Longerbeam {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0}, 51219a81c14SSteve Longerbeam {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0}, 51319a81c14SSteve Longerbeam {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0}, 51419a81c14SSteve Longerbeam {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0}, 51519a81c14SSteve Longerbeam {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0}, 5162b5c18f9SChen-Yu Tsai {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, 51719a81c14SSteve Longerbeam {0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0}, 51819a81c14SSteve Longerbeam {0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0}, 51919a81c14SSteve Longerbeam }; 52019a81c14SSteve Longerbeam 521086c25f8SMaxime Ripard static const struct reg_value ov5640_setting_NTSC_720_480[] = { 522c14d107eSMaxime Ripard {0x3c07, 0x08, 0, 0}, 52319a81c14SSteve Longerbeam {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0}, 524ce85705aSHugues Fruchet {0x3814, 0x31, 0, 0}, 52519a81c14SSteve Longerbeam {0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0}, 52619a81c14SSteve Longerbeam {0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0}, 52719a81c14SSteve Longerbeam {0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0}, 528476dec01SMaxime Ripard {0x3810, 0x00, 0, 0}, 52919a81c14SSteve Longerbeam {0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x3c, 0, 0}, 53019a81c14SSteve Longerbeam {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0}, 53119a81c14SSteve Longerbeam {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0}, 53219a81c14SSteve Longerbeam {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0}, 53319a81c14SSteve Longerbeam {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0}, 53419a81c14SSteve Longerbeam {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0}, 5352b5c18f9SChen-Yu Tsai {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, 53619a81c14SSteve Longerbeam {0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0}, 53719a81c14SSteve Longerbeam {0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0}, 53819a81c14SSteve Longerbeam }; 53919a81c14SSteve Longerbeam 540086c25f8SMaxime Ripard static const struct reg_value ov5640_setting_PAL_720_576[] = { 541c14d107eSMaxime Ripard {0x3c07, 0x08, 0, 0}, 54219a81c14SSteve Longerbeam {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0}, 543ce85705aSHugues Fruchet {0x3814, 0x31, 0, 0}, 54419a81c14SSteve Longerbeam {0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0}, 54519a81c14SSteve Longerbeam {0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0}, 54619a81c14SSteve Longerbeam {0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0}, 547476dec01SMaxime Ripard {0x3810, 0x00, 0, 0}, 54819a81c14SSteve Longerbeam {0x3811, 0x38, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0}, 54919a81c14SSteve Longerbeam {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0}, 55019a81c14SSteve Longerbeam {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0}, 55119a81c14SSteve Longerbeam {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0}, 55219a81c14SSteve Longerbeam {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0}, 55319a81c14SSteve Longerbeam {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0}, 5542b5c18f9SChen-Yu Tsai {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, 55519a81c14SSteve Longerbeam {0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0}, 55619a81c14SSteve Longerbeam {0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0}, 55719a81c14SSteve Longerbeam }; 55819a81c14SSteve Longerbeam 559086c25f8SMaxime Ripard static const struct reg_value ov5640_setting_720P_1280_720[] = { 560c14d107eSMaxime Ripard {0x3c07, 0x07, 0, 0}, 56119a81c14SSteve Longerbeam {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0}, 562ce85705aSHugues Fruchet {0x3814, 0x31, 0, 0}, 56319a81c14SSteve Longerbeam {0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0}, 56419a81c14SSteve Longerbeam {0x3802, 0x00, 0, 0}, {0x3803, 0xfa, 0, 0}, {0x3804, 0x0a, 0, 0}, 56519a81c14SSteve Longerbeam {0x3805, 0x3f, 0, 0}, {0x3806, 0x06, 0, 0}, {0x3807, 0xa9, 0, 0}, 566476dec01SMaxime Ripard {0x3810, 0x00, 0, 0}, 56719a81c14SSteve Longerbeam {0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x04, 0, 0}, 56819a81c14SSteve Longerbeam {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0}, 56919a81c14SSteve Longerbeam {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x02, 0, 0}, 57019a81c14SSteve Longerbeam {0x3a03, 0xe4, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0xbc, 0, 0}, 57119a81c14SSteve Longerbeam {0x3a0a, 0x01, 0, 0}, {0x3a0b, 0x72, 0, 0}, {0x3a0e, 0x01, 0, 0}, 57219a81c14SSteve Longerbeam {0x3a0d, 0x02, 0, 0}, {0x3a14, 0x02, 0, 0}, {0x3a15, 0xe4, 0, 0}, 5732b5c18f9SChen-Yu Tsai {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, 57419a81c14SSteve Longerbeam {0x4407, 0x04, 0, 0}, {0x460b, 0x37, 0, 0}, {0x460c, 0x20, 0, 0}, 57519a81c14SSteve Longerbeam {0x3824, 0x04, 0, 0}, {0x5001, 0x83, 0, 0}, 57619a81c14SSteve Longerbeam }; 57719a81c14SSteve Longerbeam 578086c25f8SMaxime Ripard static const struct reg_value ov5640_setting_1080P_1920_1080[] = { 579c14d107eSMaxime Ripard {0x3c07, 0x08, 0, 0}, 58019a81c14SSteve Longerbeam {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0}, 581ce85705aSHugues Fruchet {0x3814, 0x11, 0, 0}, 58219a81c14SSteve Longerbeam {0x3815, 0x11, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0}, 58319a81c14SSteve Longerbeam {0x3802, 0x00, 0, 0}, {0x3803, 0x00, 0, 0}, {0x3804, 0x0a, 0, 0}, 58419a81c14SSteve Longerbeam {0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9f, 0, 0}, 585476dec01SMaxime Ripard {0x3810, 0x00, 0, 0}, 58619a81c14SSteve Longerbeam {0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x04, 0, 0}, 58719a81c14SSteve Longerbeam {0x3618, 0x04, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x21, 0, 0}, 58819a81c14SSteve Longerbeam {0x3709, 0x12, 0, 0}, {0x370c, 0x00, 0, 0}, {0x3a02, 0x03, 0, 0}, 58919a81c14SSteve Longerbeam {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0}, 59019a81c14SSteve Longerbeam {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0}, 59119a81c14SSteve Longerbeam {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0}, 5922b5c18f9SChen-Yu Tsai {0x4001, 0x02, 0, 0}, {0x4004, 0x06, 0, 0}, 59319a81c14SSteve Longerbeam {0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0}, 594c14d107eSMaxime Ripard {0x3824, 0x02, 0, 0}, {0x5001, 0x83, 0, 0}, 595c14d107eSMaxime Ripard {0x3c07, 0x07, 0, 0}, {0x3c08, 0x00, 0, 0}, 59619a81c14SSteve Longerbeam {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0}, 59719a81c14SSteve Longerbeam {0x3800, 0x01, 0, 0}, {0x3801, 0x50, 0, 0}, {0x3802, 0x01, 0, 0}, 59819a81c14SSteve Longerbeam {0x3803, 0xb2, 0, 0}, {0x3804, 0x08, 0, 0}, {0x3805, 0xef, 0, 0}, 59986633417SMaxime Ripard {0x3806, 0x05, 0, 0}, {0x3807, 0xf1, 0, 0}, 600476dec01SMaxime Ripard {0x3612, 0x2b, 0, 0}, {0x3708, 0x64, 0, 0}, 60119a81c14SSteve Longerbeam {0x3a02, 0x04, 0, 0}, {0x3a03, 0x60, 0, 0}, {0x3a08, 0x01, 0, 0}, 60219a81c14SSteve Longerbeam {0x3a09, 0x50, 0, 0}, {0x3a0a, 0x01, 0, 0}, {0x3a0b, 0x18, 0, 0}, 60319a81c14SSteve Longerbeam {0x3a0e, 0x03, 0, 0}, {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x04, 0, 0}, 6042b5c18f9SChen-Yu Tsai {0x3a15, 0x60, 0, 0}, {0x4407, 0x04, 0, 0}, 60519a81c14SSteve Longerbeam {0x460b, 0x37, 0, 0}, {0x460c, 0x20, 0, 0}, {0x3824, 0x04, 0, 0}, 60692b9096cSBenoit Parrot {0x4005, 0x1a, 0, 0}, 60719a81c14SSteve Longerbeam }; 60819a81c14SSteve Longerbeam 609086c25f8SMaxime Ripard static const struct reg_value ov5640_setting_QSXGA_2592_1944[] = { 610c14d107eSMaxime Ripard {0x3c07, 0x08, 0, 0}, 61119a81c14SSteve Longerbeam {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0}, 612ce85705aSHugues Fruchet {0x3814, 0x11, 0, 0}, 61319a81c14SSteve Longerbeam {0x3815, 0x11, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0}, 61419a81c14SSteve Longerbeam {0x3802, 0x00, 0, 0}, {0x3803, 0x00, 0, 0}, {0x3804, 0x0a, 0, 0}, 61519a81c14SSteve Longerbeam {0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9f, 0, 0}, 616476dec01SMaxime Ripard {0x3810, 0x00, 0, 0}, 61719a81c14SSteve Longerbeam {0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x04, 0, 0}, 61819a81c14SSteve Longerbeam {0x3618, 0x04, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x21, 0, 0}, 61919a81c14SSteve Longerbeam {0x3709, 0x12, 0, 0}, {0x370c, 0x00, 0, 0}, {0x3a02, 0x03, 0, 0}, 62019a81c14SSteve Longerbeam {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0}, 62119a81c14SSteve Longerbeam {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0}, 62219a81c14SSteve Longerbeam {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0}, 6232b5c18f9SChen-Yu Tsai {0x4001, 0x02, 0, 0}, {0x4004, 0x06, 0, 0}, 62419a81c14SSteve Longerbeam {0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0}, 62519a81c14SSteve Longerbeam {0x3824, 0x02, 0, 0}, {0x5001, 0x83, 0, 70}, 62619a81c14SSteve Longerbeam }; 62719a81c14SSteve Longerbeam 62819a81c14SSteve Longerbeam /* power-on sensor init reg table */ 62919a81c14SSteve Longerbeam static const struct ov5640_mode_info ov5640_mode_init_data = { 63022845bf2SJacopo Mondi 0, SUBSAMPLING, 63122845bf2SJacopo Mondi OV5640_PIXEL_RATE_96M, 63222845bf2SJacopo Mondi 640, 1896, 480, 984, 633476dec01SMaxime Ripard ov5640_init_setting_30fps_VGA, 63419a81c14SSteve Longerbeam ARRAY_SIZE(ov5640_init_setting_30fps_VGA), 6355554c80eSAdam Ford OV5640_30_FPS, 63619a81c14SSteve Longerbeam }; 63719a81c14SSteve Longerbeam 63819a81c14SSteve Longerbeam static const struct ov5640_mode_info 639086c25f8SMaxime Ripard ov5640_mode_data[OV5640_NUM_MODES] = { 6408409d017SJacopo Mondi { 6418409d017SJacopo Mondi /* 160x120 */ 6428409d017SJacopo Mondi OV5640_MODE_QQVGA_160_120, SUBSAMPLING, 64322845bf2SJacopo Mondi OV5640_PIXEL_RATE_48M, 64432ea5e05SHugues Fruchet 160, 1896, 120, 984, 64532ea5e05SHugues Fruchet ov5640_setting_QQVGA_160_120, 64632ea5e05SHugues Fruchet ARRAY_SIZE(ov5640_setting_QQVGA_160_120), 6478409d017SJacopo Mondi OV5640_30_FPS 6488409d017SJacopo Mondi }, { 6498409d017SJacopo Mondi /* 176x144 */ 6508409d017SJacopo Mondi OV5640_MODE_QCIF_176_144, SUBSAMPLING, 65122845bf2SJacopo Mondi OV5640_PIXEL_RATE_48M, 652476dec01SMaxime Ripard 176, 1896, 144, 984, 653086c25f8SMaxime Ripard ov5640_setting_QCIF_176_144, 6545554c80eSAdam Ford ARRAY_SIZE(ov5640_setting_QCIF_176_144), 6558409d017SJacopo Mondi OV5640_30_FPS 6568409d017SJacopo Mondi }, { 6578409d017SJacopo Mondi /* 320x240 */ 6588409d017SJacopo Mondi OV5640_MODE_QVGA_320_240, SUBSAMPLING, 65922845bf2SJacopo Mondi OV5640_PIXEL_RATE_48M, 660476dec01SMaxime Ripard 320, 1896, 240, 984, 661086c25f8SMaxime Ripard ov5640_setting_QVGA_320_240, 6625554c80eSAdam Ford ARRAY_SIZE(ov5640_setting_QVGA_320_240), 6638409d017SJacopo Mondi OV5640_30_FPS 6648409d017SJacopo Mondi }, { 6658409d017SJacopo Mondi /* 640x480 */ 6668409d017SJacopo Mondi OV5640_MODE_VGA_640_480, SUBSAMPLING, 66722845bf2SJacopo Mondi OV5640_PIXEL_RATE_48M, 668476dec01SMaxime Ripard 640, 1896, 480, 1080, 669086c25f8SMaxime Ripard ov5640_setting_VGA_640_480, 6705554c80eSAdam Ford ARRAY_SIZE(ov5640_setting_VGA_640_480), 6718409d017SJacopo Mondi OV5640_60_FPS 6728409d017SJacopo Mondi }, { 6738409d017SJacopo Mondi /* 720x480 */ 6748409d017SJacopo Mondi OV5640_MODE_NTSC_720_480, SUBSAMPLING, 67522845bf2SJacopo Mondi OV5640_PIXEL_RATE_96M, 676476dec01SMaxime Ripard 720, 1896, 480, 984, 677086c25f8SMaxime Ripard ov5640_setting_NTSC_720_480, 6785554c80eSAdam Ford ARRAY_SIZE(ov5640_setting_NTSC_720_480), 6798409d017SJacopo Mondi OV5640_30_FPS 6808409d017SJacopo Mondi }, { 6818409d017SJacopo Mondi /* 720x576 */ 6828409d017SJacopo Mondi OV5640_MODE_PAL_720_576, SUBSAMPLING, 68322845bf2SJacopo Mondi OV5640_PIXEL_RATE_96M, 684476dec01SMaxime Ripard 720, 1896, 576, 984, 685086c25f8SMaxime Ripard ov5640_setting_PAL_720_576, 6865554c80eSAdam Ford ARRAY_SIZE(ov5640_setting_PAL_720_576), 6878409d017SJacopo Mondi OV5640_30_FPS 6888409d017SJacopo Mondi }, { 6898409d017SJacopo Mondi /* 1024x768 */ 6908409d017SJacopo Mondi OV5640_MODE_XGA_1024_768, SUBSAMPLING, 69122845bf2SJacopo Mondi OV5640_PIXEL_RATE_96M, 692476dec01SMaxime Ripard 1024, 1896, 768, 1080, 693086c25f8SMaxime Ripard ov5640_setting_XGA_1024_768, 6945554c80eSAdam Ford ARRAY_SIZE(ov5640_setting_XGA_1024_768), 6958409d017SJacopo Mondi OV5640_30_FPS 6968409d017SJacopo Mondi }, { 6978409d017SJacopo Mondi /* 1280x720 */ 6988409d017SJacopo Mondi OV5640_MODE_720P_1280_720, SUBSAMPLING, 69922845bf2SJacopo Mondi OV5640_PIXEL_RATE_124M, 700476dec01SMaxime Ripard 1280, 1892, 720, 740, 701086c25f8SMaxime Ripard ov5640_setting_720P_1280_720, 7025554c80eSAdam Ford ARRAY_SIZE(ov5640_setting_720P_1280_720), 7038409d017SJacopo Mondi OV5640_30_FPS 7048409d017SJacopo Mondi }, { 7058409d017SJacopo Mondi /* 1920x1080 */ 7068409d017SJacopo Mondi OV5640_MODE_1080P_1920_1080, SCALING, 70722845bf2SJacopo Mondi OV5640_PIXEL_RATE_148M, 708476dec01SMaxime Ripard 1920, 2500, 1080, 1120, 709086c25f8SMaxime Ripard ov5640_setting_1080P_1920_1080, 7105554c80eSAdam Ford ARRAY_SIZE(ov5640_setting_1080P_1920_1080), 7118409d017SJacopo Mondi OV5640_30_FPS 7128409d017SJacopo Mondi }, { 7138409d017SJacopo Mondi /* 2592x1944 */ 7148409d017SJacopo Mondi OV5640_MODE_QSXGA_2592_1944, SCALING, 71522845bf2SJacopo Mondi OV5640_PIXEL_RATE_168M, 716476dec01SMaxime Ripard 2592, 2844, 1944, 1968, 717086c25f8SMaxime Ripard ov5640_setting_QSXGA_2592_1944, 7185554c80eSAdam Ford ARRAY_SIZE(ov5640_setting_QSXGA_2592_1944), 7198409d017SJacopo Mondi OV5640_15_FPS 7208409d017SJacopo Mondi }, 72119a81c14SSteve Longerbeam }; 72219a81c14SSteve Longerbeam 72319a81c14SSteve Longerbeam static int ov5640_init_slave_id(struct ov5640_dev *sensor) 72419a81c14SSteve Longerbeam { 72519a81c14SSteve Longerbeam struct i2c_client *client = sensor->i2c_client; 72619a81c14SSteve Longerbeam struct i2c_msg msg; 72719a81c14SSteve Longerbeam u8 buf[3]; 72819a81c14SSteve Longerbeam int ret; 72919a81c14SSteve Longerbeam 73019a81c14SSteve Longerbeam if (client->addr == OV5640_DEFAULT_SLAVE_ID) 73119a81c14SSteve Longerbeam return 0; 73219a81c14SSteve Longerbeam 73319a81c14SSteve Longerbeam buf[0] = OV5640_REG_SLAVE_ID >> 8; 73419a81c14SSteve Longerbeam buf[1] = OV5640_REG_SLAVE_ID & 0xff; 73519a81c14SSteve Longerbeam buf[2] = client->addr << 1; 73619a81c14SSteve Longerbeam 73719a81c14SSteve Longerbeam msg.addr = OV5640_DEFAULT_SLAVE_ID; 73819a81c14SSteve Longerbeam msg.flags = 0; 73919a81c14SSteve Longerbeam msg.buf = buf; 74019a81c14SSteve Longerbeam msg.len = sizeof(buf); 74119a81c14SSteve Longerbeam 74219a81c14SSteve Longerbeam ret = i2c_transfer(client->adapter, &msg, 1); 74319a81c14SSteve Longerbeam if (ret < 0) { 74419a81c14SSteve Longerbeam dev_err(&client->dev, "%s: failed with %d\n", __func__, ret); 74519a81c14SSteve Longerbeam return ret; 74619a81c14SSteve Longerbeam } 74719a81c14SSteve Longerbeam 74819a81c14SSteve Longerbeam return 0; 74919a81c14SSteve Longerbeam } 75019a81c14SSteve Longerbeam 75119a81c14SSteve Longerbeam static int ov5640_write_reg(struct ov5640_dev *sensor, u16 reg, u8 val) 75219a81c14SSteve Longerbeam { 75319a81c14SSteve Longerbeam struct i2c_client *client = sensor->i2c_client; 75419a81c14SSteve Longerbeam struct i2c_msg msg; 75519a81c14SSteve Longerbeam u8 buf[3]; 75619a81c14SSteve Longerbeam int ret; 75719a81c14SSteve Longerbeam 75819a81c14SSteve Longerbeam buf[0] = reg >> 8; 75919a81c14SSteve Longerbeam buf[1] = reg & 0xff; 76019a81c14SSteve Longerbeam buf[2] = val; 76119a81c14SSteve Longerbeam 76219a81c14SSteve Longerbeam msg.addr = client->addr; 76319a81c14SSteve Longerbeam msg.flags = client->flags; 76419a81c14SSteve Longerbeam msg.buf = buf; 76519a81c14SSteve Longerbeam msg.len = sizeof(buf); 76619a81c14SSteve Longerbeam 76719a81c14SSteve Longerbeam ret = i2c_transfer(client->adapter, &msg, 1); 76819a81c14SSteve Longerbeam if (ret < 0) { 7693924c623SHugues Fruchet dev_err(&client->dev, "%s: error: reg=%x, val=%x\n", 77019a81c14SSteve Longerbeam __func__, reg, val); 77119a81c14SSteve Longerbeam return ret; 77219a81c14SSteve Longerbeam } 77319a81c14SSteve Longerbeam 77419a81c14SSteve Longerbeam return 0; 77519a81c14SSteve Longerbeam } 77619a81c14SSteve Longerbeam 77719a81c14SSteve Longerbeam static int ov5640_read_reg(struct ov5640_dev *sensor, u16 reg, u8 *val) 77819a81c14SSteve Longerbeam { 77919a81c14SSteve Longerbeam struct i2c_client *client = sensor->i2c_client; 78019a81c14SSteve Longerbeam struct i2c_msg msg[2]; 78119a81c14SSteve Longerbeam u8 buf[2]; 78219a81c14SSteve Longerbeam int ret; 78319a81c14SSteve Longerbeam 78419a81c14SSteve Longerbeam buf[0] = reg >> 8; 78519a81c14SSteve Longerbeam buf[1] = reg & 0xff; 78619a81c14SSteve Longerbeam 78719a81c14SSteve Longerbeam msg[0].addr = client->addr; 78819a81c14SSteve Longerbeam msg[0].flags = client->flags; 78919a81c14SSteve Longerbeam msg[0].buf = buf; 79019a81c14SSteve Longerbeam msg[0].len = sizeof(buf); 79119a81c14SSteve Longerbeam 79219a81c14SSteve Longerbeam msg[1].addr = client->addr; 79319a81c14SSteve Longerbeam msg[1].flags = client->flags | I2C_M_RD; 79419a81c14SSteve Longerbeam msg[1].buf = buf; 79519a81c14SSteve Longerbeam msg[1].len = 1; 79619a81c14SSteve Longerbeam 79719a81c14SSteve Longerbeam ret = i2c_transfer(client->adapter, msg, 2); 7983924c623SHugues Fruchet if (ret < 0) { 7993924c623SHugues Fruchet dev_err(&client->dev, "%s: error: reg=%x\n", 8003924c623SHugues Fruchet __func__, reg); 80119a81c14SSteve Longerbeam return ret; 8023924c623SHugues Fruchet } 80319a81c14SSteve Longerbeam 80419a81c14SSteve Longerbeam *val = buf[0]; 80519a81c14SSteve Longerbeam return 0; 80619a81c14SSteve Longerbeam } 80719a81c14SSteve Longerbeam 80819a81c14SSteve Longerbeam static int ov5640_read_reg16(struct ov5640_dev *sensor, u16 reg, u16 *val) 80919a81c14SSteve Longerbeam { 81019a81c14SSteve Longerbeam u8 hi, lo; 81119a81c14SSteve Longerbeam int ret; 81219a81c14SSteve Longerbeam 81319a81c14SSteve Longerbeam ret = ov5640_read_reg(sensor, reg, &hi); 81419a81c14SSteve Longerbeam if (ret) 81519a81c14SSteve Longerbeam return ret; 81619a81c14SSteve Longerbeam ret = ov5640_read_reg(sensor, reg + 1, &lo); 81719a81c14SSteve Longerbeam if (ret) 81819a81c14SSteve Longerbeam return ret; 81919a81c14SSteve Longerbeam 82019a81c14SSteve Longerbeam *val = ((u16)hi << 8) | (u16)lo; 82119a81c14SSteve Longerbeam return 0; 82219a81c14SSteve Longerbeam } 82319a81c14SSteve Longerbeam 82419a81c14SSteve Longerbeam static int ov5640_write_reg16(struct ov5640_dev *sensor, u16 reg, u16 val) 82519a81c14SSteve Longerbeam { 82619a81c14SSteve Longerbeam int ret; 82719a81c14SSteve Longerbeam 82819a81c14SSteve Longerbeam ret = ov5640_write_reg(sensor, reg, val >> 8); 82919a81c14SSteve Longerbeam if (ret) 83019a81c14SSteve Longerbeam return ret; 83119a81c14SSteve Longerbeam 83219a81c14SSteve Longerbeam return ov5640_write_reg(sensor, reg + 1, val & 0xff); 83319a81c14SSteve Longerbeam } 83419a81c14SSteve Longerbeam 83519a81c14SSteve Longerbeam static int ov5640_mod_reg(struct ov5640_dev *sensor, u16 reg, 83619a81c14SSteve Longerbeam u8 mask, u8 val) 83719a81c14SSteve Longerbeam { 83819a81c14SSteve Longerbeam u8 readval; 83919a81c14SSteve Longerbeam int ret; 84019a81c14SSteve Longerbeam 84119a81c14SSteve Longerbeam ret = ov5640_read_reg(sensor, reg, &readval); 84219a81c14SSteve Longerbeam if (ret) 84319a81c14SSteve Longerbeam return ret; 84419a81c14SSteve Longerbeam 84519a81c14SSteve Longerbeam readval &= ~mask; 84619a81c14SSteve Longerbeam val &= mask; 84719a81c14SSteve Longerbeam val |= readval; 84819a81c14SSteve Longerbeam 84919a81c14SSteve Longerbeam return ov5640_write_reg(sensor, reg, val); 85019a81c14SSteve Longerbeam } 85119a81c14SSteve Longerbeam 852aa288248SMaxime Ripard /* 853aa288248SMaxime Ripard * After trying the various combinations, reading various 854f8a7647dSMauro Carvalho Chehab * documentations spread around the net, and from the various 855aa288248SMaxime Ripard * feedback, the clock tree is probably as follows: 856aa288248SMaxime Ripard * 857aa288248SMaxime Ripard * +--------------+ 858aa288248SMaxime Ripard * | Ext. Clock | 859aa288248SMaxime Ripard * +-+------------+ 860aa288248SMaxime Ripard * | +----------+ 861aa288248SMaxime Ripard * +->| PLL1 | - reg 0x3036, for the multiplier 862aa288248SMaxime Ripard * +-+--------+ - reg 0x3037, bits 0-3 for the pre-divider 863aa288248SMaxime Ripard * | +--------------+ 864aa288248SMaxime Ripard * +->| System Clock | - reg 0x3035, bits 4-7 865aa288248SMaxime Ripard * +-+------------+ 866aa288248SMaxime Ripard * | +--------------+ 867aa288248SMaxime Ripard * +->| MIPI Divider | - reg 0x3035, bits 0-3 868aa288248SMaxime Ripard * | +-+------------+ 869aa288248SMaxime Ripard * | +----------------> MIPI SCLK 870aa288248SMaxime Ripard * | + +-----+ 871aa288248SMaxime Ripard * | +->| / 2 |-------> MIPI BIT CLK 872aa288248SMaxime Ripard * | +-----+ 873aa288248SMaxime Ripard * | +--------------+ 874aa288248SMaxime Ripard * +->| PLL Root Div | - reg 0x3037, bit 4 875aa288248SMaxime Ripard * +-+------------+ 876aa288248SMaxime Ripard * | +---------+ 8774c85f628SPaul Kocialkowski * +->| Bit Div | - reg 0x3034, bits 0-3 878aa288248SMaxime Ripard * +-+-------+ 879aa288248SMaxime Ripard * | +-------------+ 880aa288248SMaxime Ripard * +->| SCLK Div | - reg 0x3108, bits 0-1 881aa288248SMaxime Ripard * | +-+-----------+ 882aa288248SMaxime Ripard * | +---------------> SCLK 883aa288248SMaxime Ripard * | +-------------+ 884aa288248SMaxime Ripard * +->| SCLK 2X Div | - reg 0x3108, bits 2-3 885aa288248SMaxime Ripard * | +-+-----------+ 886aa288248SMaxime Ripard * | +---------------> SCLK 2X 887aa288248SMaxime Ripard * | +-------------+ 888aa288248SMaxime Ripard * +->| PCLK Div | - reg 0x3108, bits 4-5 889aa288248SMaxime Ripard * ++------------+ 890aa288248SMaxime Ripard * + +-----------+ 891aa288248SMaxime Ripard * +->| P_DIV | - reg 0x3035, bits 0-3 892aa288248SMaxime Ripard * +-----+-----+ 893aa288248SMaxime Ripard * +------------> PCLK 894aa288248SMaxime Ripard * 895aa288248SMaxime Ripard * This is deviating from the datasheet at least for the register 896aa288248SMaxime Ripard * 0x3108, since it's said here that the PCLK would be clocked from 897aa288248SMaxime Ripard * the PLL. 898aa288248SMaxime Ripard * 899aa288248SMaxime Ripard * There seems to be also (unverified) constraints: 900aa288248SMaxime Ripard * - the PLL pre-divider output rate should be in the 4-27MHz range 901aa288248SMaxime Ripard * - the PLL multiplier output rate should be in the 500-1000MHz range 902aa288248SMaxime Ripard * - PCLK >= SCLK * 2 in YUV, >= SCLK in Raw or JPEG 903aa288248SMaxime Ripard * 904aa288248SMaxime Ripard * In the two latter cases, these constraints are met since our 905aa288248SMaxime Ripard * factors are hardcoded. If we were to change that, we would need to 906aa288248SMaxime Ripard * take this into account. The only varying parts are the PLL 907aa288248SMaxime Ripard * multiplier and the system clock divider, which are shared between 908aa288248SMaxime Ripard * all these clocks so won't cause any issue. 909aa288248SMaxime Ripard */ 910aa288248SMaxime Ripard 911aa288248SMaxime Ripard /* 912aa288248SMaxime Ripard * This is supposed to be ranging from 1 to 8, but the value is always 913aa288248SMaxime Ripard * set to 3 in the vendor kernels. 914aa288248SMaxime Ripard */ 915aa288248SMaxime Ripard #define OV5640_PLL_PREDIV 3 916aa288248SMaxime Ripard 917aa288248SMaxime Ripard #define OV5640_PLL_MULT_MIN 4 918aa288248SMaxime Ripard #define OV5640_PLL_MULT_MAX 252 919aa288248SMaxime Ripard 920aa288248SMaxime Ripard /* 921aa288248SMaxime Ripard * This is supposed to be ranging from 1 to 16, but the value is 922aa288248SMaxime Ripard * always set to either 1 or 2 in the vendor kernels. 923aa288248SMaxime Ripard */ 924aa288248SMaxime Ripard #define OV5640_SYSDIV_MIN 1 925aa288248SMaxime Ripard #define OV5640_SYSDIV_MAX 16 926aa288248SMaxime Ripard 927aa288248SMaxime Ripard /* 928aa288248SMaxime Ripard * Hardcode these values for scaler and non-scaler modes. 929aa288248SMaxime Ripard * FIXME: to be re-calcualted for 1 data lanes setups 930aa288248SMaxime Ripard */ 931aa288248SMaxime Ripard #define OV5640_MIPI_DIV_PCLK 2 932aa288248SMaxime Ripard #define OV5640_MIPI_DIV_SCLK 1 933aa288248SMaxime Ripard 934aa288248SMaxime Ripard /* 935aa288248SMaxime Ripard * This is supposed to be ranging from 1 to 2, but the value is always 936aa288248SMaxime Ripard * set to 2 in the vendor kernels. 937aa288248SMaxime Ripard */ 938aa288248SMaxime Ripard #define OV5640_PLL_ROOT_DIV 2 939aa288248SMaxime Ripard #define OV5640_PLL_CTRL3_PLL_ROOT_DIV_2 BIT(4) 940aa288248SMaxime Ripard 941aa288248SMaxime Ripard /* 942aa288248SMaxime Ripard * We only supports 8-bit formats at the moment 943aa288248SMaxime Ripard */ 944aa288248SMaxime Ripard #define OV5640_BIT_DIV 2 945aa288248SMaxime Ripard #define OV5640_PLL_CTRL0_MIPI_MODE_8BIT 0x08 946aa288248SMaxime Ripard 947aa288248SMaxime Ripard /* 948aa288248SMaxime Ripard * This is supposed to be ranging from 1 to 8, but the value is always 949aa288248SMaxime Ripard * set to 2 in the vendor kernels. 950aa288248SMaxime Ripard */ 951aa288248SMaxime Ripard #define OV5640_SCLK_ROOT_DIV 2 952aa288248SMaxime Ripard 953aa288248SMaxime Ripard /* 954aa288248SMaxime Ripard * This is hardcoded so that the consistency is maintained between SCLK and 955aa288248SMaxime Ripard * SCLK 2x. 956aa288248SMaxime Ripard */ 957aa288248SMaxime Ripard #define OV5640_SCLK2X_ROOT_DIV (OV5640_SCLK_ROOT_DIV / 2) 958aa288248SMaxime Ripard 959aa288248SMaxime Ripard /* 960aa288248SMaxime Ripard * This is supposed to be ranging from 1 to 8, but the value is always 961aa288248SMaxime Ripard * set to 1 in the vendor kernels. 962aa288248SMaxime Ripard */ 963aa288248SMaxime Ripard #define OV5640_PCLK_ROOT_DIV 1 964aa288248SMaxime Ripard #define OV5640_PLL_SYS_ROOT_DIVIDER_BYPASS 0x00 965aa288248SMaxime Ripard 966aa288248SMaxime Ripard static unsigned long ov5640_compute_sys_clk(struct ov5640_dev *sensor, 967aa288248SMaxime Ripard u8 pll_prediv, u8 pll_mult, 968aa288248SMaxime Ripard u8 sysdiv) 969aa288248SMaxime Ripard { 970aa288248SMaxime Ripard unsigned long sysclk = sensor->xclk_freq / pll_prediv * pll_mult; 971aa288248SMaxime Ripard 972aa288248SMaxime Ripard /* PLL1 output cannot exceed 1GHz. */ 973aa288248SMaxime Ripard if (sysclk / 1000000 > 1000) 974aa288248SMaxime Ripard return 0; 975aa288248SMaxime Ripard 976aa288248SMaxime Ripard return sysclk / sysdiv; 977aa288248SMaxime Ripard } 978aa288248SMaxime Ripard 979aa288248SMaxime Ripard static unsigned long ov5640_calc_sys_clk(struct ov5640_dev *sensor, 980aa288248SMaxime Ripard unsigned long rate, 981aa288248SMaxime Ripard u8 *pll_prediv, u8 *pll_mult, 982aa288248SMaxime Ripard u8 *sysdiv) 983aa288248SMaxime Ripard { 984aa288248SMaxime Ripard unsigned long best = ~0; 985aa288248SMaxime Ripard u8 best_sysdiv = 1, best_mult = 1; 986aa288248SMaxime Ripard u8 _sysdiv, _pll_mult; 987aa288248SMaxime Ripard 988aa288248SMaxime Ripard for (_sysdiv = OV5640_SYSDIV_MIN; 989aa288248SMaxime Ripard _sysdiv <= OV5640_SYSDIV_MAX; 990aa288248SMaxime Ripard _sysdiv++) { 991aa288248SMaxime Ripard for (_pll_mult = OV5640_PLL_MULT_MIN; 992aa288248SMaxime Ripard _pll_mult <= OV5640_PLL_MULT_MAX; 993aa288248SMaxime Ripard _pll_mult++) { 994aa288248SMaxime Ripard unsigned long _rate; 995aa288248SMaxime Ripard 996aa288248SMaxime Ripard /* 997aa288248SMaxime Ripard * The PLL multiplier cannot be odd if above 998aa288248SMaxime Ripard * 127. 999aa288248SMaxime Ripard */ 1000aa288248SMaxime Ripard if (_pll_mult > 127 && (_pll_mult % 2)) 1001aa288248SMaxime Ripard continue; 1002aa288248SMaxime Ripard 1003aa288248SMaxime Ripard _rate = ov5640_compute_sys_clk(sensor, 1004aa288248SMaxime Ripard OV5640_PLL_PREDIV, 1005aa288248SMaxime Ripard _pll_mult, _sysdiv); 1006aa288248SMaxime Ripard 1007aa288248SMaxime Ripard /* 1008aa288248SMaxime Ripard * We have reached the maximum allowed PLL1 output, 1009aa288248SMaxime Ripard * increase sysdiv. 1010aa288248SMaxime Ripard */ 10112e3df204SAdam Ford if (!_rate) 1012aa288248SMaxime Ripard break; 1013aa288248SMaxime Ripard 1014aa288248SMaxime Ripard /* 1015aa288248SMaxime Ripard * Prefer rates above the expected clock rate than 1016aa288248SMaxime Ripard * below, even if that means being less precise. 1017aa288248SMaxime Ripard */ 1018aa288248SMaxime Ripard if (_rate < rate) 1019aa288248SMaxime Ripard continue; 1020aa288248SMaxime Ripard 1021aa288248SMaxime Ripard if (abs(rate - _rate) < abs(rate - best)) { 1022aa288248SMaxime Ripard best = _rate; 1023aa288248SMaxime Ripard best_sysdiv = _sysdiv; 1024aa288248SMaxime Ripard best_mult = _pll_mult; 1025aa288248SMaxime Ripard } 1026aa288248SMaxime Ripard 1027aa288248SMaxime Ripard if (_rate == rate) 1028aa288248SMaxime Ripard goto out; 1029aa288248SMaxime Ripard } 1030aa288248SMaxime Ripard } 1031aa288248SMaxime Ripard 1032aa288248SMaxime Ripard out: 1033aa288248SMaxime Ripard *sysdiv = best_sysdiv; 1034aa288248SMaxime Ripard *pll_prediv = OV5640_PLL_PREDIV; 1035aa288248SMaxime Ripard *pll_mult = best_mult; 1036aa288248SMaxime Ripard 1037aa288248SMaxime Ripard return best; 1038aa288248SMaxime Ripard } 1039aa288248SMaxime Ripard 1040aa288248SMaxime Ripard /* 1041aa288248SMaxime Ripard * ov5640_set_mipi_pclk() - Calculate the clock tree configuration values 1042aa288248SMaxime Ripard * for the MIPI CSI-2 output. 1043aa288248SMaxime Ripard * 1044aa288248SMaxime Ripard * @rate: The requested bandwidth per lane in bytes per second. 1045aa288248SMaxime Ripard * 'Bandwidth Per Lane' is calculated as: 1046aa288248SMaxime Ripard * bpl = HTOT * VTOT * FPS * bpp / num_lanes; 1047aa288248SMaxime Ripard * 1048aa288248SMaxime Ripard * This function use the requested bandwidth to calculate: 1049aa288248SMaxime Ripard * - sample_rate = bpl / (bpp / num_lanes); 1050aa288248SMaxime Ripard * = bpl / (PLL_RDIV * BIT_DIV * PCLK_DIV * MIPI_DIV / num_lanes); 1051aa288248SMaxime Ripard * 1052aa288248SMaxime Ripard * - mipi_sclk = bpl / MIPI_DIV / 2; ( / 2 is for CSI-2 DDR) 1053aa288248SMaxime Ripard * 1054aa288248SMaxime Ripard * with these fixed parameters: 1055aa288248SMaxime Ripard * PLL_RDIV = 2; 1056aa288248SMaxime Ripard * BIT_DIVIDER = 2; (MIPI_BIT_MODE == 8 ? 2 : 2,5); 1057aa288248SMaxime Ripard * PCLK_DIV = 1; 1058aa288248SMaxime Ripard * 1059aa288248SMaxime Ripard * The MIPI clock generation differs for modes that use the scaler and modes 1060aa288248SMaxime Ripard * that do not. In case the scaler is in use, the MIPI_SCLK generates the MIPI 1061aa288248SMaxime Ripard * BIT CLk, and thus: 1062aa288248SMaxime Ripard * 1063aa288248SMaxime Ripard * - mipi_sclk = bpl / MIPI_DIV / 2; 1064aa288248SMaxime Ripard * MIPI_DIV = 1; 1065aa288248SMaxime Ripard * 1066aa288248SMaxime Ripard * For modes that do not go through the scaler, the MIPI BIT CLOCK is generated 1067aa288248SMaxime Ripard * from the pixel clock, and thus: 1068aa288248SMaxime Ripard * 1069aa288248SMaxime Ripard * - sample_rate = bpl / (bpp / num_lanes); 1070aa288248SMaxime Ripard * = bpl / (2 * 2 * 1 * MIPI_DIV / num_lanes); 1071aa288248SMaxime Ripard * = bpl / (4 * MIPI_DIV / num_lanes); 1072aa288248SMaxime Ripard * - MIPI_DIV = bpp / (4 * num_lanes); 1073aa288248SMaxime Ripard * 1074aa288248SMaxime Ripard * FIXME: this have been tested with 16bpp and 2 lanes setup only. 1075aa288248SMaxime Ripard * MIPI_DIV is fixed to value 2, but it -might- be changed according to the 1076aa288248SMaxime Ripard * above formula for setups with 1 lane or image formats with different bpp. 1077aa288248SMaxime Ripard * 1078aa288248SMaxime Ripard * FIXME: this deviates from the sensor manual documentation which is quite 1079aa288248SMaxime Ripard * thin on the MIPI clock tree generation part. 1080aa288248SMaxime Ripard */ 1081aa288248SMaxime Ripard static int ov5640_set_mipi_pclk(struct ov5640_dev *sensor, 1082aa288248SMaxime Ripard unsigned long rate) 1083aa288248SMaxime Ripard { 1084aa288248SMaxime Ripard const struct ov5640_mode_info *mode = sensor->current_mode; 1085aa288248SMaxime Ripard u8 prediv, mult, sysdiv; 1086aa288248SMaxime Ripard u8 mipi_div; 1087aa288248SMaxime Ripard int ret; 1088aa288248SMaxime Ripard 1089aa288248SMaxime Ripard /* 1090aa288248SMaxime Ripard * 1280x720 is reported to use 'SUBSAMPLING' only, 1091aa288248SMaxime Ripard * but according to the sensor manual it goes through the 1092aa288248SMaxime Ripard * scaler before subsampling. 1093aa288248SMaxime Ripard */ 1094aa288248SMaxime Ripard if (mode->dn_mode == SCALING || 1095aa288248SMaxime Ripard (mode->id == OV5640_MODE_720P_1280_720)) 1096aa288248SMaxime Ripard mipi_div = OV5640_MIPI_DIV_SCLK; 1097aa288248SMaxime Ripard else 1098aa288248SMaxime Ripard mipi_div = OV5640_MIPI_DIV_PCLK; 1099aa288248SMaxime Ripard 1100aa288248SMaxime Ripard ov5640_calc_sys_clk(sensor, rate, &prediv, &mult, &sysdiv); 1101aa288248SMaxime Ripard 1102aa288248SMaxime Ripard ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL0, 1103aa288248SMaxime Ripard 0x0f, OV5640_PLL_CTRL0_MIPI_MODE_8BIT); 1104aa288248SMaxime Ripard 1105aa288248SMaxime Ripard ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL1, 1106aa288248SMaxime Ripard 0xff, sysdiv << 4 | mipi_div); 1107aa288248SMaxime Ripard if (ret) 1108aa288248SMaxime Ripard return ret; 1109aa288248SMaxime Ripard 1110aa288248SMaxime Ripard ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL2, 0xff, mult); 1111aa288248SMaxime Ripard if (ret) 1112aa288248SMaxime Ripard return ret; 1113aa288248SMaxime Ripard 1114aa288248SMaxime Ripard ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL3, 1115aa288248SMaxime Ripard 0x1f, OV5640_PLL_CTRL3_PLL_ROOT_DIV_2 | prediv); 1116aa288248SMaxime Ripard if (ret) 1117aa288248SMaxime Ripard return ret; 1118aa288248SMaxime Ripard 1119aa288248SMaxime Ripard return ov5640_mod_reg(sensor, OV5640_REG_SYS_ROOT_DIVIDER, 1120aa288248SMaxime Ripard 0x30, OV5640_PLL_SYS_ROOT_DIVIDER_BYPASS); 1121aa288248SMaxime Ripard } 1122aa288248SMaxime Ripard 1123aa288248SMaxime Ripard static unsigned long ov5640_calc_pclk(struct ov5640_dev *sensor, 1124aa288248SMaxime Ripard unsigned long rate, 1125aa288248SMaxime Ripard u8 *pll_prediv, u8 *pll_mult, u8 *sysdiv, 1126aa288248SMaxime Ripard u8 *pll_rdiv, u8 *bit_div, u8 *pclk_div) 1127aa288248SMaxime Ripard { 1128aa288248SMaxime Ripard unsigned long _rate = rate * OV5640_PLL_ROOT_DIV * OV5640_BIT_DIV * 1129aa288248SMaxime Ripard OV5640_PCLK_ROOT_DIV; 1130aa288248SMaxime Ripard 1131aa288248SMaxime Ripard _rate = ov5640_calc_sys_clk(sensor, _rate, pll_prediv, pll_mult, 1132aa288248SMaxime Ripard sysdiv); 1133aa288248SMaxime Ripard *pll_rdiv = OV5640_PLL_ROOT_DIV; 1134aa288248SMaxime Ripard *bit_div = OV5640_BIT_DIV; 1135aa288248SMaxime Ripard *pclk_div = OV5640_PCLK_ROOT_DIV; 1136aa288248SMaxime Ripard 1137aa288248SMaxime Ripard return _rate / *pll_rdiv / *bit_div / *pclk_div; 1138aa288248SMaxime Ripard } 1139aa288248SMaxime Ripard 1140aa288248SMaxime Ripard static int ov5640_set_dvp_pclk(struct ov5640_dev *sensor, unsigned long rate) 1141aa288248SMaxime Ripard { 1142aa288248SMaxime Ripard u8 prediv, mult, sysdiv, pll_rdiv, bit_div, pclk_div; 1143aa288248SMaxime Ripard int ret; 1144aa288248SMaxime Ripard 1145aa288248SMaxime Ripard ov5640_calc_pclk(sensor, rate, &prediv, &mult, &sysdiv, &pll_rdiv, 1146aa288248SMaxime Ripard &bit_div, &pclk_div); 1147aa288248SMaxime Ripard 1148aa288248SMaxime Ripard if (bit_div == 2) 1149aa288248SMaxime Ripard bit_div = 8; 1150aa288248SMaxime Ripard 1151aa288248SMaxime Ripard ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL0, 1152aa288248SMaxime Ripard 0x0f, bit_div); 1153aa288248SMaxime Ripard if (ret) 1154aa288248SMaxime Ripard return ret; 1155aa288248SMaxime Ripard 1156aa288248SMaxime Ripard /* 1157aa288248SMaxime Ripard * We need to set sysdiv according to the clock, and to clear 1158aa288248SMaxime Ripard * the MIPI divider. 1159aa288248SMaxime Ripard */ 1160aa288248SMaxime Ripard ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL1, 1161aa288248SMaxime Ripard 0xff, sysdiv << 4); 1162aa288248SMaxime Ripard if (ret) 1163aa288248SMaxime Ripard return ret; 1164aa288248SMaxime Ripard 1165aa288248SMaxime Ripard ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL2, 1166aa288248SMaxime Ripard 0xff, mult); 1167aa288248SMaxime Ripard if (ret) 1168aa288248SMaxime Ripard return ret; 1169aa288248SMaxime Ripard 1170aa288248SMaxime Ripard ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL3, 1171aa288248SMaxime Ripard 0x1f, prediv | ((pll_rdiv - 1) << 4)); 1172aa288248SMaxime Ripard if (ret) 1173aa288248SMaxime Ripard return ret; 1174aa288248SMaxime Ripard 1175aa288248SMaxime Ripard return ov5640_mod_reg(sensor, OV5640_REG_SYS_ROOT_DIVIDER, 0x30, 1176aa288248SMaxime Ripard (ilog2(pclk_div) << 4)); 1177aa288248SMaxime Ripard } 1178aa288248SMaxime Ripard 11797cb013b1SChen-Yu Tsai /* set JPEG framing sizes */ 11807cb013b1SChen-Yu Tsai static int ov5640_set_jpeg_timings(struct ov5640_dev *sensor, 11817cb013b1SChen-Yu Tsai const struct ov5640_mode_info *mode) 11827cb013b1SChen-Yu Tsai { 11837cb013b1SChen-Yu Tsai int ret; 11847cb013b1SChen-Yu Tsai 11852b5c18f9SChen-Yu Tsai /* 11862b5c18f9SChen-Yu Tsai * compression mode 3 timing 11872b5c18f9SChen-Yu Tsai * 11882b5c18f9SChen-Yu Tsai * Data is transmitted with programmable width (VFIFO_HSIZE). 11892b5c18f9SChen-Yu Tsai * No padding done. Last line may have less data. Varying 11902b5c18f9SChen-Yu Tsai * number of lines per frame, depending on amount of data. 11912b5c18f9SChen-Yu Tsai */ 11922b5c18f9SChen-Yu Tsai ret = ov5640_mod_reg(sensor, OV5640_REG_JPG_MODE_SELECT, 0x7, 0x3); 11932b5c18f9SChen-Yu Tsai if (ret < 0) 11942b5c18f9SChen-Yu Tsai return ret; 11952b5c18f9SChen-Yu Tsai 11967cb013b1SChen-Yu Tsai ret = ov5640_write_reg16(sensor, OV5640_REG_VFIFO_HSIZE, mode->hact); 11977cb013b1SChen-Yu Tsai if (ret < 0) 11987cb013b1SChen-Yu Tsai return ret; 11997cb013b1SChen-Yu Tsai 12007cb013b1SChen-Yu Tsai return ov5640_write_reg16(sensor, OV5640_REG_VFIFO_VSIZE, mode->vact); 12017cb013b1SChen-Yu Tsai } 12027cb013b1SChen-Yu Tsai 120319a81c14SSteve Longerbeam /* download ov5640 settings to sensor through i2c */ 1204bad1774eSJacopo Mondi static int ov5640_set_timings(struct ov5640_dev *sensor, 1205bad1774eSJacopo Mondi const struct ov5640_mode_info *mode) 1206bad1774eSJacopo Mondi { 1207bad1774eSJacopo Mondi int ret; 1208bad1774eSJacopo Mondi 12097cb013b1SChen-Yu Tsai if (sensor->fmt.code == MEDIA_BUS_FMT_JPEG_1X8) { 12107cb013b1SChen-Yu Tsai ret = ov5640_set_jpeg_timings(sensor, mode); 12117cb013b1SChen-Yu Tsai if (ret < 0) 12127cb013b1SChen-Yu Tsai return ret; 12137cb013b1SChen-Yu Tsai } 12147cb013b1SChen-Yu Tsai 1215bad1774eSJacopo Mondi ret = ov5640_write_reg16(sensor, OV5640_REG_TIMING_DVPHO, mode->hact); 1216bad1774eSJacopo Mondi if (ret < 0) 1217bad1774eSJacopo Mondi return ret; 1218bad1774eSJacopo Mondi 1219bad1774eSJacopo Mondi ret = ov5640_write_reg16(sensor, OV5640_REG_TIMING_DVPVO, mode->vact); 1220bad1774eSJacopo Mondi if (ret < 0) 1221bad1774eSJacopo Mondi return ret; 1222bad1774eSJacopo Mondi 1223bad1774eSJacopo Mondi ret = ov5640_write_reg16(sensor, OV5640_REG_TIMING_HTS, mode->htot); 1224bad1774eSJacopo Mondi if (ret < 0) 1225bad1774eSJacopo Mondi return ret; 1226bad1774eSJacopo Mondi 1227bad1774eSJacopo Mondi return ov5640_write_reg16(sensor, OV5640_REG_TIMING_VTS, mode->vtot); 1228bad1774eSJacopo Mondi } 1229bad1774eSJacopo Mondi 123019a81c14SSteve Longerbeam static int ov5640_load_regs(struct ov5640_dev *sensor, 123119a81c14SSteve Longerbeam const struct ov5640_mode_info *mode) 123219a81c14SSteve Longerbeam { 123319a81c14SSteve Longerbeam const struct reg_value *regs = mode->reg_data; 123419a81c14SSteve Longerbeam unsigned int i; 123519a81c14SSteve Longerbeam u32 delay_ms; 123619a81c14SSteve Longerbeam u16 reg_addr; 123719a81c14SSteve Longerbeam u8 mask, val; 123819a81c14SSteve Longerbeam int ret = 0; 123919a81c14SSteve Longerbeam 124019a81c14SSteve Longerbeam for (i = 0; i < mode->reg_data_size; ++i, ++regs) { 124119a81c14SSteve Longerbeam delay_ms = regs->delay_ms; 124219a81c14SSteve Longerbeam reg_addr = regs->reg_addr; 124319a81c14SSteve Longerbeam val = regs->val; 124419a81c14SSteve Longerbeam mask = regs->mask; 124519a81c14SSteve Longerbeam 12463b987d70SLad Prabhakar /* remain in power down mode for DVP */ 12473b987d70SLad Prabhakar if (regs->reg_addr == OV5640_REG_SYS_CTRL0 && 12483b987d70SLad Prabhakar val == OV5640_REG_SYS_CTRL0_SW_PWUP && 12498e823f5cSJacopo Mondi !ov5640_is_csi2(sensor)) 12503b987d70SLad Prabhakar continue; 12513b987d70SLad Prabhakar 125219a81c14SSteve Longerbeam if (mask) 125319a81c14SSteve Longerbeam ret = ov5640_mod_reg(sensor, reg_addr, mask, val); 125419a81c14SSteve Longerbeam else 125519a81c14SSteve Longerbeam ret = ov5640_write_reg(sensor, reg_addr, val); 125619a81c14SSteve Longerbeam if (ret) 125719a81c14SSteve Longerbeam break; 125819a81c14SSteve Longerbeam 125919a81c14SSteve Longerbeam if (delay_ms) 126019a81c14SSteve Longerbeam usleep_range(1000 * delay_ms, 1000 * delay_ms + 100); 126119a81c14SSteve Longerbeam } 126219a81c14SSteve Longerbeam 1263bad1774eSJacopo Mondi return ov5640_set_timings(sensor, mode); 126419a81c14SSteve Longerbeam } 126519a81c14SSteve Longerbeam 1266dc29a1c1SHugues Fruchet static int ov5640_set_autoexposure(struct ov5640_dev *sensor, bool on) 1267dc29a1c1SHugues Fruchet { 1268dc29a1c1SHugues Fruchet return ov5640_mod_reg(sensor, OV5640_REG_AEC_PK_MANUAL, 1269dc29a1c1SHugues Fruchet BIT(0), on ? 0 : BIT(0)); 1270dc29a1c1SHugues Fruchet } 1271dc29a1c1SHugues Fruchet 127219a81c14SSteve Longerbeam /* read exposure, in number of line periods */ 127319a81c14SSteve Longerbeam static int ov5640_get_exposure(struct ov5640_dev *sensor) 127419a81c14SSteve Longerbeam { 127519a81c14SSteve Longerbeam int exp, ret; 127619a81c14SSteve Longerbeam u8 temp; 127719a81c14SSteve Longerbeam 127819a81c14SSteve Longerbeam ret = ov5640_read_reg(sensor, OV5640_REG_AEC_PK_EXPOSURE_HI, &temp); 127919a81c14SSteve Longerbeam if (ret) 128019a81c14SSteve Longerbeam return ret; 128119a81c14SSteve Longerbeam exp = ((int)temp & 0x0f) << 16; 128219a81c14SSteve Longerbeam ret = ov5640_read_reg(sensor, OV5640_REG_AEC_PK_EXPOSURE_MED, &temp); 128319a81c14SSteve Longerbeam if (ret) 128419a81c14SSteve Longerbeam return ret; 128519a81c14SSteve Longerbeam exp |= ((int)temp << 8); 128619a81c14SSteve Longerbeam ret = ov5640_read_reg(sensor, OV5640_REG_AEC_PK_EXPOSURE_LO, &temp); 128719a81c14SSteve Longerbeam if (ret) 128819a81c14SSteve Longerbeam return ret; 128919a81c14SSteve Longerbeam exp |= (int)temp; 129019a81c14SSteve Longerbeam 129119a81c14SSteve Longerbeam return exp >> 4; 129219a81c14SSteve Longerbeam } 129319a81c14SSteve Longerbeam 129419a81c14SSteve Longerbeam /* write exposure, given number of line periods */ 129519a81c14SSteve Longerbeam static int ov5640_set_exposure(struct ov5640_dev *sensor, u32 exposure) 129619a81c14SSteve Longerbeam { 129719a81c14SSteve Longerbeam int ret; 129819a81c14SSteve Longerbeam 129919a81c14SSteve Longerbeam exposure <<= 4; 130019a81c14SSteve Longerbeam 130119a81c14SSteve Longerbeam ret = ov5640_write_reg(sensor, 130219a81c14SSteve Longerbeam OV5640_REG_AEC_PK_EXPOSURE_LO, 130319a81c14SSteve Longerbeam exposure & 0xff); 130419a81c14SSteve Longerbeam if (ret) 130519a81c14SSteve Longerbeam return ret; 130619a81c14SSteve Longerbeam ret = ov5640_write_reg(sensor, 130719a81c14SSteve Longerbeam OV5640_REG_AEC_PK_EXPOSURE_MED, 130819a81c14SSteve Longerbeam (exposure >> 8) & 0xff); 130919a81c14SSteve Longerbeam if (ret) 131019a81c14SSteve Longerbeam return ret; 131119a81c14SSteve Longerbeam return ov5640_write_reg(sensor, 131219a81c14SSteve Longerbeam OV5640_REG_AEC_PK_EXPOSURE_HI, 131319a81c14SSteve Longerbeam (exposure >> 16) & 0x0f); 131419a81c14SSteve Longerbeam } 131519a81c14SSteve Longerbeam 131619a81c14SSteve Longerbeam static int ov5640_get_gain(struct ov5640_dev *sensor) 131719a81c14SSteve Longerbeam { 131819a81c14SSteve Longerbeam u16 gain; 131919a81c14SSteve Longerbeam int ret; 132019a81c14SSteve Longerbeam 132119a81c14SSteve Longerbeam ret = ov5640_read_reg16(sensor, OV5640_REG_AEC_PK_REAL_GAIN, &gain); 132219a81c14SSteve Longerbeam if (ret) 132319a81c14SSteve Longerbeam return ret; 132419a81c14SSteve Longerbeam 132519a81c14SSteve Longerbeam return gain & 0x3ff; 132619a81c14SSteve Longerbeam } 132719a81c14SSteve Longerbeam 13283cca8ef5SHugues Fruchet static int ov5640_set_gain(struct ov5640_dev *sensor, int gain) 13293cca8ef5SHugues Fruchet { 13303cca8ef5SHugues Fruchet return ov5640_write_reg16(sensor, OV5640_REG_AEC_PK_REAL_GAIN, 13313cca8ef5SHugues Fruchet (u16)gain & 0x3ff); 13323cca8ef5SHugues Fruchet } 13333cca8ef5SHugues Fruchet 13343cca8ef5SHugues Fruchet static int ov5640_set_autogain(struct ov5640_dev *sensor, bool on) 13353cca8ef5SHugues Fruchet { 13363cca8ef5SHugues Fruchet return ov5640_mod_reg(sensor, OV5640_REG_AEC_PK_MANUAL, 13373cca8ef5SHugues Fruchet BIT(1), on ? 0 : BIT(1)); 13383cca8ef5SHugues Fruchet } 13393cca8ef5SHugues Fruchet 1340f22996dbSHugues Fruchet static int ov5640_set_stream_dvp(struct ov5640_dev *sensor, bool on) 1341f22996dbSHugues Fruchet { 13423b987d70SLad Prabhakar return ov5640_write_reg(sensor, OV5640_REG_SYS_CTRL0, on ? 13433b987d70SLad Prabhakar OV5640_REG_SYS_CTRL0_SW_PWUP : 13443b987d70SLad Prabhakar OV5640_REG_SYS_CTRL0_SW_PWDN); 1345f22996dbSHugues Fruchet } 1346f22996dbSHugues Fruchet 1347f22996dbSHugues Fruchet static int ov5640_set_stream_mipi(struct ov5640_dev *sensor, bool on) 134819a81c14SSteve Longerbeam { 134919a81c14SSteve Longerbeam int ret; 135019a81c14SSteve Longerbeam 1351aa4bb8b8SJacopo Mondi /* 1352aa4bb8b8SJacopo Mondi * Enable/disable the MIPI interface 1353aa4bb8b8SJacopo Mondi * 1354aa4bb8b8SJacopo Mondi * 0x300e = on ? 0x45 : 0x40 1355aa4bb8b8SJacopo Mondi * 1356aa4bb8b8SJacopo Mondi * FIXME: the sensor manual (version 2.03) reports 1357aa4bb8b8SJacopo Mondi * [7:5] = 000 : 1 data lane mode 1358aa4bb8b8SJacopo Mondi * [7:5] = 001 : 2 data lanes mode 1359aa4bb8b8SJacopo Mondi * But this settings do not work, while the following ones 1360aa4bb8b8SJacopo Mondi * have been validated for 2 data lanes mode. 1361aa4bb8b8SJacopo Mondi * 1362aa4bb8b8SJacopo Mondi * [7:5] = 010 : 2 data lanes mode 1363aa4bb8b8SJacopo Mondi * [4] = 0 : Power up MIPI HS Tx 1364aa4bb8b8SJacopo Mondi * [3] = 0 : Power up MIPI LS Rx 1365aa4bb8b8SJacopo Mondi * [2] = 1/0 : MIPI interface enable/disable 1366aa4bb8b8SJacopo Mondi * [1:0] = 01/00: FIXME: 'debug' 1367aa4bb8b8SJacopo Mondi */ 1368aa4bb8b8SJacopo Mondi ret = ov5640_write_reg(sensor, OV5640_REG_IO_MIPI_CTRL00, 1369aa4bb8b8SJacopo Mondi on ? 0x45 : 0x40); 137019a81c14SSteve Longerbeam if (ret) 137119a81c14SSteve Longerbeam return ret; 137219a81c14SSteve Longerbeam 137319a81c14SSteve Longerbeam return ov5640_write_reg(sensor, OV5640_REG_FRAME_CTRL01, 137419a81c14SSteve Longerbeam on ? 0x00 : 0x0f); 137519a81c14SSteve Longerbeam } 137619a81c14SSteve Longerbeam 137719a81c14SSteve Longerbeam static int ov5640_get_sysclk(struct ov5640_dev *sensor) 137819a81c14SSteve Longerbeam { 137919a81c14SSteve Longerbeam /* calculate sysclk */ 138019a81c14SSteve Longerbeam u32 xvclk = sensor->xclk_freq / 10000; 138119a81c14SSteve Longerbeam u32 multiplier, prediv, VCO, sysdiv, pll_rdiv; 138219a81c14SSteve Longerbeam u32 sclk_rdiv_map[] = {1, 2, 4, 8}; 138319a81c14SSteve Longerbeam u32 bit_div2x = 1, sclk_rdiv, sysclk; 138419a81c14SSteve Longerbeam u8 temp1, temp2; 138519a81c14SSteve Longerbeam int ret; 138619a81c14SSteve Longerbeam 138719a81c14SSteve Longerbeam ret = ov5640_read_reg(sensor, OV5640_REG_SC_PLL_CTRL0, &temp1); 138819a81c14SSteve Longerbeam if (ret) 138919a81c14SSteve Longerbeam return ret; 139019a81c14SSteve Longerbeam temp2 = temp1 & 0x0f; 139119a81c14SSteve Longerbeam if (temp2 == 8 || temp2 == 10) 139219a81c14SSteve Longerbeam bit_div2x = temp2 / 2; 139319a81c14SSteve Longerbeam 139419a81c14SSteve Longerbeam ret = ov5640_read_reg(sensor, OV5640_REG_SC_PLL_CTRL1, &temp1); 139519a81c14SSteve Longerbeam if (ret) 139619a81c14SSteve Longerbeam return ret; 139719a81c14SSteve Longerbeam sysdiv = temp1 >> 4; 139819a81c14SSteve Longerbeam if (sysdiv == 0) 139919a81c14SSteve Longerbeam sysdiv = 16; 140019a81c14SSteve Longerbeam 140119a81c14SSteve Longerbeam ret = ov5640_read_reg(sensor, OV5640_REG_SC_PLL_CTRL2, &temp1); 140219a81c14SSteve Longerbeam if (ret) 140319a81c14SSteve Longerbeam return ret; 140419a81c14SSteve Longerbeam multiplier = temp1; 140519a81c14SSteve Longerbeam 140619a81c14SSteve Longerbeam ret = ov5640_read_reg(sensor, OV5640_REG_SC_PLL_CTRL3, &temp1); 140719a81c14SSteve Longerbeam if (ret) 140819a81c14SSteve Longerbeam return ret; 140919a81c14SSteve Longerbeam prediv = temp1 & 0x0f; 141019a81c14SSteve Longerbeam pll_rdiv = ((temp1 >> 4) & 0x01) + 1; 141119a81c14SSteve Longerbeam 141219a81c14SSteve Longerbeam ret = ov5640_read_reg(sensor, OV5640_REG_SYS_ROOT_DIVIDER, &temp1); 141319a81c14SSteve Longerbeam if (ret) 141419a81c14SSteve Longerbeam return ret; 141519a81c14SSteve Longerbeam temp2 = temp1 & 0x03; 141619a81c14SSteve Longerbeam sclk_rdiv = sclk_rdiv_map[temp2]; 141719a81c14SSteve Longerbeam 141819a81c14SSteve Longerbeam if (!prediv || !sysdiv || !pll_rdiv || !bit_div2x) 141919a81c14SSteve Longerbeam return -EINVAL; 142019a81c14SSteve Longerbeam 142119a81c14SSteve Longerbeam VCO = xvclk * multiplier / prediv; 142219a81c14SSteve Longerbeam 142319a81c14SSteve Longerbeam sysclk = VCO / sysdiv / pll_rdiv * 2 / bit_div2x / sclk_rdiv; 142419a81c14SSteve Longerbeam 142519a81c14SSteve Longerbeam return sysclk; 142619a81c14SSteve Longerbeam } 142719a81c14SSteve Longerbeam 142819a81c14SSteve Longerbeam static int ov5640_set_night_mode(struct ov5640_dev *sensor) 142919a81c14SSteve Longerbeam { 143019a81c14SSteve Longerbeam /* read HTS from register settings */ 143119a81c14SSteve Longerbeam u8 mode; 143219a81c14SSteve Longerbeam int ret; 143319a81c14SSteve Longerbeam 143419a81c14SSteve Longerbeam ret = ov5640_read_reg(sensor, OV5640_REG_AEC_CTRL00, &mode); 143519a81c14SSteve Longerbeam if (ret) 143619a81c14SSteve Longerbeam return ret; 143719a81c14SSteve Longerbeam mode &= 0xfb; 143819a81c14SSteve Longerbeam return ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL00, mode); 143919a81c14SSteve Longerbeam } 144019a81c14SSteve Longerbeam 144119a81c14SSteve Longerbeam static int ov5640_get_hts(struct ov5640_dev *sensor) 144219a81c14SSteve Longerbeam { 144319a81c14SSteve Longerbeam /* read HTS from register settings */ 144419a81c14SSteve Longerbeam u16 hts; 144519a81c14SSteve Longerbeam int ret; 144619a81c14SSteve Longerbeam 144719a81c14SSteve Longerbeam ret = ov5640_read_reg16(sensor, OV5640_REG_TIMING_HTS, &hts); 144819a81c14SSteve Longerbeam if (ret) 144919a81c14SSteve Longerbeam return ret; 145019a81c14SSteve Longerbeam return hts; 145119a81c14SSteve Longerbeam } 145219a81c14SSteve Longerbeam 145319a81c14SSteve Longerbeam static int ov5640_get_vts(struct ov5640_dev *sensor) 145419a81c14SSteve Longerbeam { 145519a81c14SSteve Longerbeam u16 vts; 145619a81c14SSteve Longerbeam int ret; 145719a81c14SSteve Longerbeam 145819a81c14SSteve Longerbeam ret = ov5640_read_reg16(sensor, OV5640_REG_TIMING_VTS, &vts); 145919a81c14SSteve Longerbeam if (ret) 146019a81c14SSteve Longerbeam return ret; 146119a81c14SSteve Longerbeam return vts; 146219a81c14SSteve Longerbeam } 146319a81c14SSteve Longerbeam 146419a81c14SSteve Longerbeam static int ov5640_set_vts(struct ov5640_dev *sensor, int vts) 146519a81c14SSteve Longerbeam { 146619a81c14SSteve Longerbeam return ov5640_write_reg16(sensor, OV5640_REG_TIMING_VTS, vts); 146719a81c14SSteve Longerbeam } 146819a81c14SSteve Longerbeam 146919a81c14SSteve Longerbeam static int ov5640_get_light_freq(struct ov5640_dev *sensor) 147019a81c14SSteve Longerbeam { 147119a81c14SSteve Longerbeam /* get banding filter value */ 147219a81c14SSteve Longerbeam int ret, light_freq = 0; 147319a81c14SSteve Longerbeam u8 temp, temp1; 147419a81c14SSteve Longerbeam 147519a81c14SSteve Longerbeam ret = ov5640_read_reg(sensor, OV5640_REG_HZ5060_CTRL01, &temp); 147619a81c14SSteve Longerbeam if (ret) 147719a81c14SSteve Longerbeam return ret; 147819a81c14SSteve Longerbeam 147919a81c14SSteve Longerbeam if (temp & 0x80) { 148019a81c14SSteve Longerbeam /* manual */ 148119a81c14SSteve Longerbeam ret = ov5640_read_reg(sensor, OV5640_REG_HZ5060_CTRL00, 148219a81c14SSteve Longerbeam &temp1); 148319a81c14SSteve Longerbeam if (ret) 148419a81c14SSteve Longerbeam return ret; 148519a81c14SSteve Longerbeam if (temp1 & 0x04) { 148619a81c14SSteve Longerbeam /* 50Hz */ 148719a81c14SSteve Longerbeam light_freq = 50; 148819a81c14SSteve Longerbeam } else { 148919a81c14SSteve Longerbeam /* 60Hz */ 149019a81c14SSteve Longerbeam light_freq = 60; 149119a81c14SSteve Longerbeam } 149219a81c14SSteve Longerbeam } else { 149319a81c14SSteve Longerbeam /* auto */ 149419a81c14SSteve Longerbeam ret = ov5640_read_reg(sensor, OV5640_REG_SIGMADELTA_CTRL0C, 149519a81c14SSteve Longerbeam &temp1); 149619a81c14SSteve Longerbeam if (ret) 149719a81c14SSteve Longerbeam return ret; 149819a81c14SSteve Longerbeam 149919a81c14SSteve Longerbeam if (temp1 & 0x01) { 150019a81c14SSteve Longerbeam /* 50Hz */ 150119a81c14SSteve Longerbeam light_freq = 50; 150219a81c14SSteve Longerbeam } else { 150319a81c14SSteve Longerbeam /* 60Hz */ 150419a81c14SSteve Longerbeam } 150519a81c14SSteve Longerbeam } 150619a81c14SSteve Longerbeam 150719a81c14SSteve Longerbeam return light_freq; 150819a81c14SSteve Longerbeam } 150919a81c14SSteve Longerbeam 151019a81c14SSteve Longerbeam static int ov5640_set_bandingfilter(struct ov5640_dev *sensor) 151119a81c14SSteve Longerbeam { 151219a81c14SSteve Longerbeam u32 band_step60, max_band60, band_step50, max_band50, prev_vts; 151319a81c14SSteve Longerbeam int ret; 151419a81c14SSteve Longerbeam 151519a81c14SSteve Longerbeam /* read preview PCLK */ 151619a81c14SSteve Longerbeam ret = ov5640_get_sysclk(sensor); 151719a81c14SSteve Longerbeam if (ret < 0) 151819a81c14SSteve Longerbeam return ret; 151919a81c14SSteve Longerbeam if (ret == 0) 152019a81c14SSteve Longerbeam return -EINVAL; 152119a81c14SSteve Longerbeam sensor->prev_sysclk = ret; 152219a81c14SSteve Longerbeam /* read preview HTS */ 152319a81c14SSteve Longerbeam ret = ov5640_get_hts(sensor); 152419a81c14SSteve Longerbeam if (ret < 0) 152519a81c14SSteve Longerbeam return ret; 152619a81c14SSteve Longerbeam if (ret == 0) 152719a81c14SSteve Longerbeam return -EINVAL; 152819a81c14SSteve Longerbeam sensor->prev_hts = ret; 152919a81c14SSteve Longerbeam 153019a81c14SSteve Longerbeam /* read preview VTS */ 153119a81c14SSteve Longerbeam ret = ov5640_get_vts(sensor); 153219a81c14SSteve Longerbeam if (ret < 0) 153319a81c14SSteve Longerbeam return ret; 153419a81c14SSteve Longerbeam prev_vts = ret; 153519a81c14SSteve Longerbeam 153619a81c14SSteve Longerbeam /* calculate banding filter */ 153719a81c14SSteve Longerbeam /* 60Hz */ 153819a81c14SSteve Longerbeam band_step60 = sensor->prev_sysclk * 100 / sensor->prev_hts * 100 / 120; 153919a81c14SSteve Longerbeam ret = ov5640_write_reg16(sensor, OV5640_REG_AEC_B60_STEP, band_step60); 154019a81c14SSteve Longerbeam if (ret) 154119a81c14SSteve Longerbeam return ret; 154219a81c14SSteve Longerbeam if (!band_step60) 154319a81c14SSteve Longerbeam return -EINVAL; 154419a81c14SSteve Longerbeam max_band60 = (int)((prev_vts - 4) / band_step60); 154519a81c14SSteve Longerbeam ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL0D, max_band60); 154619a81c14SSteve Longerbeam if (ret) 154719a81c14SSteve Longerbeam return ret; 154819a81c14SSteve Longerbeam 154919a81c14SSteve Longerbeam /* 50Hz */ 155019a81c14SSteve Longerbeam band_step50 = sensor->prev_sysclk * 100 / sensor->prev_hts; 155119a81c14SSteve Longerbeam ret = ov5640_write_reg16(sensor, OV5640_REG_AEC_B50_STEP, band_step50); 155219a81c14SSteve Longerbeam if (ret) 155319a81c14SSteve Longerbeam return ret; 155419a81c14SSteve Longerbeam if (!band_step50) 155519a81c14SSteve Longerbeam return -EINVAL; 155619a81c14SSteve Longerbeam max_band50 = (int)((prev_vts - 4) / band_step50); 155719a81c14SSteve Longerbeam return ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL0E, max_band50); 155819a81c14SSteve Longerbeam } 155919a81c14SSteve Longerbeam 156019a81c14SSteve Longerbeam static int ov5640_set_ae_target(struct ov5640_dev *sensor, int target) 156119a81c14SSteve Longerbeam { 156219a81c14SSteve Longerbeam /* stable in high */ 156319a81c14SSteve Longerbeam u32 fast_high, fast_low; 156419a81c14SSteve Longerbeam int ret; 156519a81c14SSteve Longerbeam 156619a81c14SSteve Longerbeam sensor->ae_low = target * 23 / 25; /* 0.92 */ 156719a81c14SSteve Longerbeam sensor->ae_high = target * 27 / 25; /* 1.08 */ 156819a81c14SSteve Longerbeam 156919a81c14SSteve Longerbeam fast_high = sensor->ae_high << 1; 157019a81c14SSteve Longerbeam if (fast_high > 255) 157119a81c14SSteve Longerbeam fast_high = 255; 157219a81c14SSteve Longerbeam 157319a81c14SSteve Longerbeam fast_low = sensor->ae_low >> 1; 157419a81c14SSteve Longerbeam 157519a81c14SSteve Longerbeam ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL0F, sensor->ae_high); 157619a81c14SSteve Longerbeam if (ret) 157719a81c14SSteve Longerbeam return ret; 157819a81c14SSteve Longerbeam ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL10, sensor->ae_low); 157919a81c14SSteve Longerbeam if (ret) 158019a81c14SSteve Longerbeam return ret; 158119a81c14SSteve Longerbeam ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL1B, sensor->ae_high); 158219a81c14SSteve Longerbeam if (ret) 158319a81c14SSteve Longerbeam return ret; 158419a81c14SSteve Longerbeam ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL1E, sensor->ae_low); 158519a81c14SSteve Longerbeam if (ret) 158619a81c14SSteve Longerbeam return ret; 158719a81c14SSteve Longerbeam ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL11, fast_high); 158819a81c14SSteve Longerbeam if (ret) 158919a81c14SSteve Longerbeam return ret; 159019a81c14SSteve Longerbeam return ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL1F, fast_low); 159119a81c14SSteve Longerbeam } 159219a81c14SSteve Longerbeam 1593c2c3f42dSHugues Fruchet static int ov5640_get_binning(struct ov5640_dev *sensor) 159419a81c14SSteve Longerbeam { 159519a81c14SSteve Longerbeam u8 temp; 159619a81c14SSteve Longerbeam int ret; 159719a81c14SSteve Longerbeam 159819a81c14SSteve Longerbeam ret = ov5640_read_reg(sensor, OV5640_REG_TIMING_TC_REG21, &temp); 159919a81c14SSteve Longerbeam if (ret) 160019a81c14SSteve Longerbeam return ret; 1601c2c3f42dSHugues Fruchet 1602c2c3f42dSHugues Fruchet return temp & BIT(0); 160319a81c14SSteve Longerbeam } 160419a81c14SSteve Longerbeam 1605ce85705aSHugues Fruchet static int ov5640_set_binning(struct ov5640_dev *sensor, bool enable) 1606ce85705aSHugues Fruchet { 1607ce85705aSHugues Fruchet int ret; 1608ce85705aSHugues Fruchet 1609ce85705aSHugues Fruchet /* 1610ce85705aSHugues Fruchet * TIMING TC REG21: 1611ce85705aSHugues Fruchet * - [0]: Horizontal binning enable 1612ce85705aSHugues Fruchet */ 1613ce85705aSHugues Fruchet ret = ov5640_mod_reg(sensor, OV5640_REG_TIMING_TC_REG21, 1614ce85705aSHugues Fruchet BIT(0), enable ? BIT(0) : 0); 1615ce85705aSHugues Fruchet if (ret) 1616ce85705aSHugues Fruchet return ret; 1617ce85705aSHugues Fruchet /* 1618ce85705aSHugues Fruchet * TIMING TC REG20: 1619ce85705aSHugues Fruchet * - [0]: Undocumented, but hardcoded init sequences 1620ce85705aSHugues Fruchet * are always setting REG21/REG20 bit 0 to same value... 1621ce85705aSHugues Fruchet */ 1622ce85705aSHugues Fruchet return ov5640_mod_reg(sensor, OV5640_REG_TIMING_TC_REG20, 1623ce85705aSHugues Fruchet BIT(0), enable ? BIT(0) : 0); 1624ce85705aSHugues Fruchet } 1625ce85705aSHugues Fruchet 162619a81c14SSteve Longerbeam static int ov5640_set_virtual_channel(struct ov5640_dev *sensor) 162719a81c14SSteve Longerbeam { 16288670d70aSHugues Fruchet struct i2c_client *client = sensor->i2c_client; 162919a81c14SSteve Longerbeam u8 temp, channel = virtual_channel; 163019a81c14SSteve Longerbeam int ret; 163119a81c14SSteve Longerbeam 16328670d70aSHugues Fruchet if (channel > 3) { 16338670d70aSHugues Fruchet dev_err(&client->dev, 16348670d70aSHugues Fruchet "%s: wrong virtual_channel parameter, expected (0..3), got %d\n", 16358670d70aSHugues Fruchet __func__, channel); 163619a81c14SSteve Longerbeam return -EINVAL; 16378670d70aSHugues Fruchet } 163819a81c14SSteve Longerbeam 163919a81c14SSteve Longerbeam ret = ov5640_read_reg(sensor, OV5640_REG_DEBUG_MODE, &temp); 164019a81c14SSteve Longerbeam if (ret) 164119a81c14SSteve Longerbeam return ret; 164219a81c14SSteve Longerbeam temp &= ~(3 << 6); 164319a81c14SSteve Longerbeam temp |= (channel << 6); 164419a81c14SSteve Longerbeam return ov5640_write_reg(sensor, OV5640_REG_DEBUG_MODE, temp); 164519a81c14SSteve Longerbeam } 164619a81c14SSteve Longerbeam 164719a81c14SSteve Longerbeam static const struct ov5640_mode_info * 164819a81c14SSteve Longerbeam ov5640_find_mode(struct ov5640_dev *sensor, enum ov5640_frame_rate fr, 164919a81c14SSteve Longerbeam int width, int height, bool nearest) 165019a81c14SSteve Longerbeam { 16513c4a7372SHugues Fruchet const struct ov5640_mode_info *mode; 165219a81c14SSteve Longerbeam 1653086c25f8SMaxime Ripard mode = v4l2_find_nearest_size(ov5640_mode_data, 1654086c25f8SMaxime Ripard ARRAY_SIZE(ov5640_mode_data), 16553c4a7372SHugues Fruchet hact, vact, 16563c4a7372SHugues Fruchet width, height); 165719a81c14SSteve Longerbeam 16583c4a7372SHugues Fruchet if (!mode || 16593c4a7372SHugues Fruchet (!nearest && (mode->hact != width || mode->vact != height))) 16603c4a7372SHugues Fruchet return NULL; 166119a81c14SSteve Longerbeam 16625554c80eSAdam Ford /* Check to see if the current mode exceeds the max frame rate */ 16635554c80eSAdam Ford if (ov5640_framerates[fr] > ov5640_framerates[mode->max_fps]) 1664981e4454SBenoit Parrot return NULL; 1665981e4454SBenoit Parrot 166619a81c14SSteve Longerbeam return mode; 166719a81c14SSteve Longerbeam } 166819a81c14SSteve Longerbeam 1669cc196e48SBenoit Parrot static u64 ov5640_calc_pixel_rate(struct ov5640_dev *sensor) 1670cc196e48SBenoit Parrot { 1671cc196e48SBenoit Parrot u64 rate; 1672cc196e48SBenoit Parrot 1673cc196e48SBenoit Parrot rate = sensor->current_mode->vtot * sensor->current_mode->htot; 1674cc196e48SBenoit Parrot rate *= ov5640_framerates[sensor->current_fr]; 1675cc196e48SBenoit Parrot 1676cc196e48SBenoit Parrot return rate; 1677cc196e48SBenoit Parrot } 1678cc196e48SBenoit Parrot 167919a81c14SSteve Longerbeam /* 168019a81c14SSteve Longerbeam * sensor changes between scaling and subsampling, go through 168119a81c14SSteve Longerbeam * exposure calculation 168219a81c14SSteve Longerbeam */ 168341d8d7f5SHugues Fruchet static int ov5640_set_mode_exposure_calc(struct ov5640_dev *sensor, 168441d8d7f5SHugues Fruchet const struct ov5640_mode_info *mode) 168519a81c14SSteve Longerbeam { 168619a81c14SSteve Longerbeam u32 prev_shutter, prev_gain16; 168719a81c14SSteve Longerbeam u32 cap_shutter, cap_gain16; 168819a81c14SSteve Longerbeam u32 cap_sysclk, cap_hts, cap_vts; 168919a81c14SSteve Longerbeam u32 light_freq, cap_bandfilt, cap_maxband; 169019a81c14SSteve Longerbeam u32 cap_gain16_shutter; 169119a81c14SSteve Longerbeam u8 average; 169219a81c14SSteve Longerbeam int ret; 169319a81c14SSteve Longerbeam 169441d8d7f5SHugues Fruchet if (!mode->reg_data) 169519a81c14SSteve Longerbeam return -EINVAL; 169619a81c14SSteve Longerbeam 169719a81c14SSteve Longerbeam /* read preview shutter */ 169819a81c14SSteve Longerbeam ret = ov5640_get_exposure(sensor); 169919a81c14SSteve Longerbeam if (ret < 0) 170019a81c14SSteve Longerbeam return ret; 170119a81c14SSteve Longerbeam prev_shutter = ret; 1702c2c3f42dSHugues Fruchet ret = ov5640_get_binning(sensor); 170319a81c14SSteve Longerbeam if (ret < 0) 170419a81c14SSteve Longerbeam return ret; 170519a81c14SSteve Longerbeam if (ret && mode->id != OV5640_MODE_720P_1280_720 && 170619a81c14SSteve Longerbeam mode->id != OV5640_MODE_1080P_1920_1080) 170719a81c14SSteve Longerbeam prev_shutter *= 2; 170819a81c14SSteve Longerbeam 170919a81c14SSteve Longerbeam /* read preview gain */ 171019a81c14SSteve Longerbeam ret = ov5640_get_gain(sensor); 171119a81c14SSteve Longerbeam if (ret < 0) 171219a81c14SSteve Longerbeam return ret; 171319a81c14SSteve Longerbeam prev_gain16 = ret; 171419a81c14SSteve Longerbeam 171519a81c14SSteve Longerbeam /* get average */ 171619a81c14SSteve Longerbeam ret = ov5640_read_reg(sensor, OV5640_REG_AVG_READOUT, &average); 171719a81c14SSteve Longerbeam if (ret) 171819a81c14SSteve Longerbeam return ret; 171919a81c14SSteve Longerbeam 172019a81c14SSteve Longerbeam /* turn off night mode for capture */ 172119a81c14SSteve Longerbeam ret = ov5640_set_night_mode(sensor); 172219a81c14SSteve Longerbeam if (ret < 0) 172319a81c14SSteve Longerbeam return ret; 172419a81c14SSteve Longerbeam 172519a81c14SSteve Longerbeam /* Write capture setting */ 172619a81c14SSteve Longerbeam ret = ov5640_load_regs(sensor, mode); 172719a81c14SSteve Longerbeam if (ret < 0) 172819a81c14SSteve Longerbeam return ret; 172919a81c14SSteve Longerbeam 173019a81c14SSteve Longerbeam /* read capture VTS */ 173119a81c14SSteve Longerbeam ret = ov5640_get_vts(sensor); 173219a81c14SSteve Longerbeam if (ret < 0) 173319a81c14SSteve Longerbeam return ret; 173419a81c14SSteve Longerbeam cap_vts = ret; 173519a81c14SSteve Longerbeam ret = ov5640_get_hts(sensor); 173619a81c14SSteve Longerbeam if (ret < 0) 173719a81c14SSteve Longerbeam return ret; 173819a81c14SSteve Longerbeam if (ret == 0) 173919a81c14SSteve Longerbeam return -EINVAL; 174019a81c14SSteve Longerbeam cap_hts = ret; 174119a81c14SSteve Longerbeam 174219a81c14SSteve Longerbeam ret = ov5640_get_sysclk(sensor); 174319a81c14SSteve Longerbeam if (ret < 0) 174419a81c14SSteve Longerbeam return ret; 174519a81c14SSteve Longerbeam if (ret == 0) 174619a81c14SSteve Longerbeam return -EINVAL; 174719a81c14SSteve Longerbeam cap_sysclk = ret; 174819a81c14SSteve Longerbeam 174919a81c14SSteve Longerbeam /* calculate capture banding filter */ 175019a81c14SSteve Longerbeam ret = ov5640_get_light_freq(sensor); 175119a81c14SSteve Longerbeam if (ret < 0) 175219a81c14SSteve Longerbeam return ret; 175319a81c14SSteve Longerbeam light_freq = ret; 175419a81c14SSteve Longerbeam 175519a81c14SSteve Longerbeam if (light_freq == 60) { 175619a81c14SSteve Longerbeam /* 60Hz */ 175719a81c14SSteve Longerbeam cap_bandfilt = cap_sysclk * 100 / cap_hts * 100 / 120; 175819a81c14SSteve Longerbeam } else { 175919a81c14SSteve Longerbeam /* 50Hz */ 176019a81c14SSteve Longerbeam cap_bandfilt = cap_sysclk * 100 / cap_hts; 176119a81c14SSteve Longerbeam } 176219a81c14SSteve Longerbeam 176319a81c14SSteve Longerbeam if (!sensor->prev_sysclk) { 176419a81c14SSteve Longerbeam ret = ov5640_get_sysclk(sensor); 176519a81c14SSteve Longerbeam if (ret < 0) 176619a81c14SSteve Longerbeam return ret; 176719a81c14SSteve Longerbeam if (ret == 0) 176819a81c14SSteve Longerbeam return -EINVAL; 176919a81c14SSteve Longerbeam sensor->prev_sysclk = ret; 177019a81c14SSteve Longerbeam } 177119a81c14SSteve Longerbeam 177219a81c14SSteve Longerbeam if (!cap_bandfilt) 177319a81c14SSteve Longerbeam return -EINVAL; 177419a81c14SSteve Longerbeam 177519a81c14SSteve Longerbeam cap_maxband = (int)((cap_vts - 4) / cap_bandfilt); 177619a81c14SSteve Longerbeam 177719a81c14SSteve Longerbeam /* calculate capture shutter/gain16 */ 177819a81c14SSteve Longerbeam if (average > sensor->ae_low && average < sensor->ae_high) { 177919a81c14SSteve Longerbeam /* in stable range */ 178019a81c14SSteve Longerbeam cap_gain16_shutter = 178119a81c14SSteve Longerbeam prev_gain16 * prev_shutter * 178219a81c14SSteve Longerbeam cap_sysclk / sensor->prev_sysclk * 178319a81c14SSteve Longerbeam sensor->prev_hts / cap_hts * 178419a81c14SSteve Longerbeam sensor->ae_target / average; 178519a81c14SSteve Longerbeam } else { 178619a81c14SSteve Longerbeam cap_gain16_shutter = 178719a81c14SSteve Longerbeam prev_gain16 * prev_shutter * 178819a81c14SSteve Longerbeam cap_sysclk / sensor->prev_sysclk * 178919a81c14SSteve Longerbeam sensor->prev_hts / cap_hts; 179019a81c14SSteve Longerbeam } 179119a81c14SSteve Longerbeam 179219a81c14SSteve Longerbeam /* gain to shutter */ 179319a81c14SSteve Longerbeam if (cap_gain16_shutter < (cap_bandfilt * 16)) { 179419a81c14SSteve Longerbeam /* shutter < 1/100 */ 179519a81c14SSteve Longerbeam cap_shutter = cap_gain16_shutter / 16; 179619a81c14SSteve Longerbeam if (cap_shutter < 1) 179719a81c14SSteve Longerbeam cap_shutter = 1; 179819a81c14SSteve Longerbeam 179919a81c14SSteve Longerbeam cap_gain16 = cap_gain16_shutter / cap_shutter; 180019a81c14SSteve Longerbeam if (cap_gain16 < 16) 180119a81c14SSteve Longerbeam cap_gain16 = 16; 180219a81c14SSteve Longerbeam } else { 180319a81c14SSteve Longerbeam if (cap_gain16_shutter > (cap_bandfilt * cap_maxband * 16)) { 180419a81c14SSteve Longerbeam /* exposure reach max */ 180519a81c14SSteve Longerbeam cap_shutter = cap_bandfilt * cap_maxband; 180619a81c14SSteve Longerbeam if (!cap_shutter) 180719a81c14SSteve Longerbeam return -EINVAL; 180819a81c14SSteve Longerbeam 180919a81c14SSteve Longerbeam cap_gain16 = cap_gain16_shutter / cap_shutter; 181019a81c14SSteve Longerbeam } else { 181119a81c14SSteve Longerbeam /* 1/100 < (cap_shutter = n/100) =< max */ 181219a81c14SSteve Longerbeam cap_shutter = 181319a81c14SSteve Longerbeam ((int)(cap_gain16_shutter / 16 / cap_bandfilt)) 181419a81c14SSteve Longerbeam * cap_bandfilt; 181519a81c14SSteve Longerbeam if (!cap_shutter) 181619a81c14SSteve Longerbeam return -EINVAL; 181719a81c14SSteve Longerbeam 181819a81c14SSteve Longerbeam cap_gain16 = cap_gain16_shutter / cap_shutter; 181919a81c14SSteve Longerbeam } 182019a81c14SSteve Longerbeam } 182119a81c14SSteve Longerbeam 182219a81c14SSteve Longerbeam /* set capture gain */ 18233cca8ef5SHugues Fruchet ret = ov5640_set_gain(sensor, cap_gain16); 182419a81c14SSteve Longerbeam if (ret) 182519a81c14SSteve Longerbeam return ret; 182619a81c14SSteve Longerbeam 182719a81c14SSteve Longerbeam /* write capture shutter */ 182819a81c14SSteve Longerbeam if (cap_shutter > (cap_vts - 4)) { 182919a81c14SSteve Longerbeam cap_vts = cap_shutter + 4; 183019a81c14SSteve Longerbeam ret = ov5640_set_vts(sensor, cap_vts); 183119a81c14SSteve Longerbeam if (ret < 0) 183219a81c14SSteve Longerbeam return ret; 183319a81c14SSteve Longerbeam } 183419a81c14SSteve Longerbeam 183519a81c14SSteve Longerbeam /* set exposure */ 18363cca8ef5SHugues Fruchet return ov5640_set_exposure(sensor, cap_shutter); 183719a81c14SSteve Longerbeam } 183819a81c14SSteve Longerbeam 183919a81c14SSteve Longerbeam /* 184019a81c14SSteve Longerbeam * if sensor changes inside scaling or subsampling 184119a81c14SSteve Longerbeam * change mode directly 184219a81c14SSteve Longerbeam */ 184319a81c14SSteve Longerbeam static int ov5640_set_mode_direct(struct ov5640_dev *sensor, 18443cca8ef5SHugues Fruchet const struct ov5640_mode_info *mode) 184519a81c14SSteve Longerbeam { 184641d8d7f5SHugues Fruchet if (!mode->reg_data) 184719a81c14SSteve Longerbeam return -EINVAL; 184819a81c14SSteve Longerbeam 184919a81c14SSteve Longerbeam /* Write capture setting */ 18503cca8ef5SHugues Fruchet return ov5640_load_regs(sensor, mode); 185119a81c14SSteve Longerbeam } 185219a81c14SSteve Longerbeam 1853985cdcb0SHugues Fruchet static int ov5640_set_mode(struct ov5640_dev *sensor) 185419a81c14SSteve Longerbeam { 185519a81c14SSteve Longerbeam const struct ov5640_mode_info *mode = sensor->current_mode; 1856985cdcb0SHugues Fruchet const struct ov5640_mode_info *orig_mode = sensor->last_mode; 185719a81c14SSteve Longerbeam enum ov5640_downsize_mode dn_mode, orig_dn_mode; 18583cca8ef5SHugues Fruchet bool auto_gain = sensor->ctrls.auto_gain->val == 1; 1859dc29a1c1SHugues Fruchet bool auto_exp = sensor->ctrls.auto_exp->val == V4L2_EXPOSURE_AUTO; 1860aa288248SMaxime Ripard unsigned long rate; 186119a81c14SSteve Longerbeam int ret; 186219a81c14SSteve Longerbeam 186319a81c14SSteve Longerbeam dn_mode = mode->dn_mode; 186419a81c14SSteve Longerbeam orig_dn_mode = orig_mode->dn_mode; 186519a81c14SSteve Longerbeam 186619a81c14SSteve Longerbeam /* auto gain and exposure must be turned off when changing modes */ 18673cca8ef5SHugues Fruchet if (auto_gain) { 18683cca8ef5SHugues Fruchet ret = ov5640_set_autogain(sensor, false); 186919a81c14SSteve Longerbeam if (ret) 187019a81c14SSteve Longerbeam return ret; 18713cca8ef5SHugues Fruchet } 1872bf4a4b51SMaxime Ripard 18733cca8ef5SHugues Fruchet if (auto_exp) { 1874dc29a1c1SHugues Fruchet ret = ov5640_set_autoexposure(sensor, false); 187519a81c14SSteve Longerbeam if (ret) 18763cca8ef5SHugues Fruchet goto restore_auto_gain; 18773cca8ef5SHugues Fruchet } 187819a81c14SSteve Longerbeam 1879aa288248SMaxime Ripard /* 1880aa288248SMaxime Ripard * All the formats we support have 16 bits per pixel, seems to require 1881aa288248SMaxime Ripard * the same rate than YUV, so we can just use 16 bpp all the time. 1882aa288248SMaxime Ripard */ 1883cc196e48SBenoit Parrot rate = ov5640_calc_pixel_rate(sensor) * 16; 18848e823f5cSJacopo Mondi if (ov5640_is_csi2(sensor)) { 1885aa288248SMaxime Ripard rate = rate / sensor->ep.bus.mipi_csi2.num_data_lanes; 1886aa288248SMaxime Ripard ret = ov5640_set_mipi_pclk(sensor, rate); 1887aa288248SMaxime Ripard } else { 1888aa288248SMaxime Ripard rate = rate / sensor->ep.bus.parallel.bus_width; 1889aa288248SMaxime Ripard ret = ov5640_set_dvp_pclk(sensor, rate); 1890aa288248SMaxime Ripard } 1891aa288248SMaxime Ripard 1892aa288248SMaxime Ripard if (ret < 0) 1893aa288248SMaxime Ripard return 0; 1894aa288248SMaxime Ripard 189519a81c14SSteve Longerbeam if ((dn_mode == SUBSAMPLING && orig_dn_mode == SCALING) || 189619a81c14SSteve Longerbeam (dn_mode == SCALING && orig_dn_mode == SUBSAMPLING)) { 189719a81c14SSteve Longerbeam /* 189819a81c14SSteve Longerbeam * change between subsampling and scaling 18993cca8ef5SHugues Fruchet * go through exposure calculation 190019a81c14SSteve Longerbeam */ 190119a81c14SSteve Longerbeam ret = ov5640_set_mode_exposure_calc(sensor, mode); 190219a81c14SSteve Longerbeam } else { 190319a81c14SSteve Longerbeam /* 190419a81c14SSteve Longerbeam * change inside subsampling or scaling 190519a81c14SSteve Longerbeam * download firmware directly 190619a81c14SSteve Longerbeam */ 19073cca8ef5SHugues Fruchet ret = ov5640_set_mode_direct(sensor, mode); 190819a81c14SSteve Longerbeam } 190919a81c14SSteve Longerbeam if (ret < 0) 19103cca8ef5SHugues Fruchet goto restore_auto_exp_gain; 19113cca8ef5SHugues Fruchet 19123cca8ef5SHugues Fruchet /* restore auto gain and exposure */ 19133cca8ef5SHugues Fruchet if (auto_gain) 19143cca8ef5SHugues Fruchet ov5640_set_autogain(sensor, true); 19153cca8ef5SHugues Fruchet if (auto_exp) 19163cca8ef5SHugues Fruchet ov5640_set_autoexposure(sensor, true); 191719a81c14SSteve Longerbeam 1918ce85705aSHugues Fruchet ret = ov5640_set_binning(sensor, dn_mode != SCALING); 1919ce85705aSHugues Fruchet if (ret < 0) 1920ce85705aSHugues Fruchet return ret; 192119a81c14SSteve Longerbeam ret = ov5640_set_ae_target(sensor, sensor->ae_target); 192219a81c14SSteve Longerbeam if (ret < 0) 192319a81c14SSteve Longerbeam return ret; 192419a81c14SSteve Longerbeam ret = ov5640_get_light_freq(sensor); 192519a81c14SSteve Longerbeam if (ret < 0) 192619a81c14SSteve Longerbeam return ret; 192719a81c14SSteve Longerbeam ret = ov5640_set_bandingfilter(sensor); 192819a81c14SSteve Longerbeam if (ret < 0) 192919a81c14SSteve Longerbeam return ret; 193019a81c14SSteve Longerbeam ret = ov5640_set_virtual_channel(sensor); 193119a81c14SSteve Longerbeam if (ret < 0) 193219a81c14SSteve Longerbeam return ret; 193319a81c14SSteve Longerbeam 193419a81c14SSteve Longerbeam sensor->pending_mode_change = false; 1935985cdcb0SHugues Fruchet sensor->last_mode = mode; 193619a81c14SSteve Longerbeam 193719a81c14SSteve Longerbeam return 0; 19383cca8ef5SHugues Fruchet 19393cca8ef5SHugues Fruchet restore_auto_exp_gain: 19403cca8ef5SHugues Fruchet if (auto_exp) 19413cca8ef5SHugues Fruchet ov5640_set_autoexposure(sensor, true); 19423cca8ef5SHugues Fruchet restore_auto_gain: 19433cca8ef5SHugues Fruchet if (auto_gain) 19443cca8ef5SHugues Fruchet ov5640_set_autogain(sensor, true); 19453cca8ef5SHugues Fruchet 19463cca8ef5SHugues Fruchet return ret; 194719a81c14SSteve Longerbeam } 194819a81c14SSteve Longerbeam 194919ad26f9SAkinobu Mita static int ov5640_set_framefmt(struct ov5640_dev *sensor, 195019ad26f9SAkinobu Mita struct v4l2_mbus_framefmt *format); 195119ad26f9SAkinobu Mita 195219a81c14SSteve Longerbeam /* restore the last set video mode after chip power-on */ 195319a81c14SSteve Longerbeam static int ov5640_restore_mode(struct ov5640_dev *sensor) 195419a81c14SSteve Longerbeam { 195519a81c14SSteve Longerbeam int ret; 195619a81c14SSteve Longerbeam 195719a81c14SSteve Longerbeam /* first load the initial register values */ 195819a81c14SSteve Longerbeam ret = ov5640_load_regs(sensor, &ov5640_mode_init_data); 195919a81c14SSteve Longerbeam if (ret < 0) 196019a81c14SSteve Longerbeam return ret; 1961985cdcb0SHugues Fruchet sensor->last_mode = &ov5640_mode_init_data; 196219a81c14SSteve Longerbeam 19638f57c2f8SMaxime Ripard ret = ov5640_mod_reg(sensor, OV5640_REG_SYS_ROOT_DIVIDER, 0x3f, 19647851fe7aSMaxime Ripard (ilog2(OV5640_SCLK2X_ROOT_DIV) << 2) | 19657851fe7aSMaxime Ripard ilog2(OV5640_SCLK_ROOT_DIV)); 19668f57c2f8SMaxime Ripard if (ret) 19678f57c2f8SMaxime Ripard return ret; 19688f57c2f8SMaxime Ripard 196919a81c14SSteve Longerbeam /* now restore the last capture mode */ 1970985cdcb0SHugues Fruchet ret = ov5640_set_mode(sensor); 197119ad26f9SAkinobu Mita if (ret < 0) 197219ad26f9SAkinobu Mita return ret; 197319ad26f9SAkinobu Mita 197419ad26f9SAkinobu Mita return ov5640_set_framefmt(sensor, &sensor->fmt); 197519a81c14SSteve Longerbeam } 197619a81c14SSteve Longerbeam 197719a81c14SSteve Longerbeam static void ov5640_power(struct ov5640_dev *sensor, bool enable) 197819a81c14SSteve Longerbeam { 19791fddc5daSHugues Fruchet gpiod_set_value_cansleep(sensor->pwdn_gpio, enable ? 0 : 1); 198019a81c14SSteve Longerbeam } 198119a81c14SSteve Longerbeam 198219a81c14SSteve Longerbeam static void ov5640_reset(struct ov5640_dev *sensor) 198319a81c14SSteve Longerbeam { 198419a81c14SSteve Longerbeam if (!sensor->reset_gpio) 198519a81c14SSteve Longerbeam return; 198619a81c14SSteve Longerbeam 19871fddc5daSHugues Fruchet gpiod_set_value_cansleep(sensor->reset_gpio, 0); 198819a81c14SSteve Longerbeam 198919a81c14SSteve Longerbeam /* camera power cycle */ 199019a81c14SSteve Longerbeam ov5640_power(sensor, false); 199119a81c14SSteve Longerbeam usleep_range(5000, 10000); 199219a81c14SSteve Longerbeam ov5640_power(sensor, true); 199319a81c14SSteve Longerbeam usleep_range(5000, 10000); 199419a81c14SSteve Longerbeam 19951fddc5daSHugues Fruchet gpiod_set_value_cansleep(sensor->reset_gpio, 1); 199619a81c14SSteve Longerbeam usleep_range(1000, 2000); 199719a81c14SSteve Longerbeam 19981fddc5daSHugues Fruchet gpiod_set_value_cansleep(sensor->reset_gpio, 0); 19991d4c41f3SLoic Poulain usleep_range(20000, 25000); 200019a81c14SSteve Longerbeam } 200119a81c14SSteve Longerbeam 20020f7acb52SHugues Fruchet static int ov5640_set_power_on(struct ov5640_dev *sensor) 200319a81c14SSteve Longerbeam { 20040f7acb52SHugues Fruchet struct i2c_client *client = sensor->i2c_client; 20050f7acb52SHugues Fruchet int ret; 200619a81c14SSteve Longerbeam 20070f7acb52SHugues Fruchet ret = clk_prepare_enable(sensor->xclk); 20080f7acb52SHugues Fruchet if (ret) { 20090f7acb52SHugues Fruchet dev_err(&client->dev, "%s: failed to enable clock\n", 20100f7acb52SHugues Fruchet __func__); 20110f7acb52SHugues Fruchet return ret; 20120f7acb52SHugues Fruchet } 201319a81c14SSteve Longerbeam 201419a81c14SSteve Longerbeam ret = regulator_bulk_enable(OV5640_NUM_SUPPLIES, 201519a81c14SSteve Longerbeam sensor->supplies); 20160f7acb52SHugues Fruchet if (ret) { 20170f7acb52SHugues Fruchet dev_err(&client->dev, "%s: failed to enable regulators\n", 20180f7acb52SHugues Fruchet __func__); 201919a81c14SSteve Longerbeam goto xclk_off; 20200f7acb52SHugues Fruchet } 202119a81c14SSteve Longerbeam 202219a81c14SSteve Longerbeam ov5640_reset(sensor); 202319a81c14SSteve Longerbeam ov5640_power(sensor, true); 202419a81c14SSteve Longerbeam 202519a81c14SSteve Longerbeam ret = ov5640_init_slave_id(sensor); 202619a81c14SSteve Longerbeam if (ret) 202719a81c14SSteve Longerbeam goto power_off; 202819a81c14SSteve Longerbeam 20290f7acb52SHugues Fruchet return 0; 20300f7acb52SHugues Fruchet 20310f7acb52SHugues Fruchet power_off: 20320f7acb52SHugues Fruchet ov5640_power(sensor, false); 20330f7acb52SHugues Fruchet regulator_bulk_disable(OV5640_NUM_SUPPLIES, sensor->supplies); 20340f7acb52SHugues Fruchet xclk_off: 20350f7acb52SHugues Fruchet clk_disable_unprepare(sensor->xclk); 20360f7acb52SHugues Fruchet return ret; 20370f7acb52SHugues Fruchet } 20380f7acb52SHugues Fruchet 20390f7acb52SHugues Fruchet static void ov5640_set_power_off(struct ov5640_dev *sensor) 20400f7acb52SHugues Fruchet { 20410f7acb52SHugues Fruchet ov5640_power(sensor, false); 20420f7acb52SHugues Fruchet regulator_bulk_disable(OV5640_NUM_SUPPLIES, sensor->supplies); 20430f7acb52SHugues Fruchet clk_disable_unprepare(sensor->xclk); 20440f7acb52SHugues Fruchet } 20450f7acb52SHugues Fruchet 2046b1751ae6SLad Prabhakar static int ov5640_set_power_mipi(struct ov5640_dev *sensor, bool on) 2047b1751ae6SLad Prabhakar { 2048b1751ae6SLad Prabhakar int ret; 2049b1751ae6SLad Prabhakar 2050b1751ae6SLad Prabhakar if (!on) { 2051b1751ae6SLad Prabhakar /* Reset MIPI bus settings to their default values. */ 2052b1751ae6SLad Prabhakar ov5640_write_reg(sensor, OV5640_REG_IO_MIPI_CTRL00, 0x58); 2053b1751ae6SLad Prabhakar ov5640_write_reg(sensor, OV5640_REG_MIPI_CTRL00, 0x04); 2054b1751ae6SLad Prabhakar ov5640_write_reg(sensor, OV5640_REG_PAD_OUTPUT00, 0x00); 2055b1751ae6SLad Prabhakar return 0; 2056b1751ae6SLad Prabhakar } 2057b1751ae6SLad Prabhakar 2058b1751ae6SLad Prabhakar /* 2059b1751ae6SLad Prabhakar * Power up MIPI HS Tx and LS Rx; 2 data lanes mode 2060b1751ae6SLad Prabhakar * 2061b1751ae6SLad Prabhakar * 0x300e = 0x40 2062b1751ae6SLad Prabhakar * [7:5] = 010 : 2 data lanes mode (see FIXME note in 2063b1751ae6SLad Prabhakar * "ov5640_set_stream_mipi()") 2064b1751ae6SLad Prabhakar * [4] = 0 : Power up MIPI HS Tx 2065b1751ae6SLad Prabhakar * [3] = 0 : Power up MIPI LS Rx 2066b1751ae6SLad Prabhakar * [2] = 0 : MIPI interface disabled 2067b1751ae6SLad Prabhakar */ 2068b1751ae6SLad Prabhakar ret = ov5640_write_reg(sensor, OV5640_REG_IO_MIPI_CTRL00, 0x40); 2069b1751ae6SLad Prabhakar if (ret) 2070b1751ae6SLad Prabhakar return ret; 2071b1751ae6SLad Prabhakar 2072b1751ae6SLad Prabhakar /* 2073b1751ae6SLad Prabhakar * Gate clock and set LP11 in 'no packets mode' (idle) 2074b1751ae6SLad Prabhakar * 2075b1751ae6SLad Prabhakar * 0x4800 = 0x24 2076b1751ae6SLad Prabhakar * [5] = 1 : Gate clock when 'no packets' 2077b1751ae6SLad Prabhakar * [2] = 1 : MIPI bus in LP11 when 'no packets' 2078b1751ae6SLad Prabhakar */ 2079b1751ae6SLad Prabhakar ret = ov5640_write_reg(sensor, OV5640_REG_MIPI_CTRL00, 0x24); 2080b1751ae6SLad Prabhakar if (ret) 2081b1751ae6SLad Prabhakar return ret; 2082b1751ae6SLad Prabhakar 2083b1751ae6SLad Prabhakar /* 2084b1751ae6SLad Prabhakar * Set data lanes and clock in LP11 when 'sleeping' 2085b1751ae6SLad Prabhakar * 2086b1751ae6SLad Prabhakar * 0x3019 = 0x70 2087b1751ae6SLad Prabhakar * [6] = 1 : MIPI data lane 2 in LP11 when 'sleeping' 2088b1751ae6SLad Prabhakar * [5] = 1 : MIPI data lane 1 in LP11 when 'sleeping' 2089b1751ae6SLad Prabhakar * [4] = 1 : MIPI clock lane in LP11 when 'sleeping' 2090b1751ae6SLad Prabhakar */ 2091b1751ae6SLad Prabhakar ret = ov5640_write_reg(sensor, OV5640_REG_PAD_OUTPUT00, 0x70); 2092b1751ae6SLad Prabhakar if (ret) 2093b1751ae6SLad Prabhakar return ret; 2094b1751ae6SLad Prabhakar 2095b1751ae6SLad Prabhakar /* Give lanes some time to coax into LP11 state. */ 2096b1751ae6SLad Prabhakar usleep_range(500, 1000); 2097b1751ae6SLad Prabhakar 2098b1751ae6SLad Prabhakar return 0; 2099b1751ae6SLad Prabhakar } 2100b1751ae6SLad Prabhakar 2101576f5d4bSLad Prabhakar static int ov5640_set_power_dvp(struct ov5640_dev *sensor, bool on) 2102576f5d4bSLad Prabhakar { 2103311a6408SLad Prabhakar unsigned int flags = sensor->ep.bus.parallel.flags; 210468579b32SHugues Fruchet bool bt656 = sensor->ep.bus_type == V4L2_MBUS_BT656; 210568579b32SHugues Fruchet u8 polarities = 0; 2106576f5d4bSLad Prabhakar int ret; 2107576f5d4bSLad Prabhakar 2108576f5d4bSLad Prabhakar if (!on) { 2109576f5d4bSLad Prabhakar /* Reset settings to their default values. */ 211068579b32SHugues Fruchet ov5640_write_reg(sensor, OV5640_REG_CCIR656_CTRL00, 0x00); 2111311a6408SLad Prabhakar ov5640_write_reg(sensor, OV5640_REG_IO_MIPI_CTRL00, 0x58); 2112311a6408SLad Prabhakar ov5640_write_reg(sensor, OV5640_REG_POLARITY_CTRL00, 0x20); 2113576f5d4bSLad Prabhakar ov5640_write_reg(sensor, OV5640_REG_PAD_OUTPUT_ENABLE01, 0x00); 2114576f5d4bSLad Prabhakar ov5640_write_reg(sensor, OV5640_REG_PAD_OUTPUT_ENABLE02, 0x00); 2115576f5d4bSLad Prabhakar return 0; 2116576f5d4bSLad Prabhakar } 2117576f5d4bSLad Prabhakar 2118576f5d4bSLad Prabhakar /* 2119311a6408SLad Prabhakar * Note about parallel port configuration. 2120311a6408SLad Prabhakar * 2121311a6408SLad Prabhakar * When configured in parallel mode, the OV5640 will 2122311a6408SLad Prabhakar * output 10 bits data on DVP data lines [9:0]. 2123311a6408SLad Prabhakar * If only 8 bits data are wanted, the 8 bits data lines 2124311a6408SLad Prabhakar * of the camera interface must be physically connected 2125311a6408SLad Prabhakar * on the DVP data lines [9:2]. 2126311a6408SLad Prabhakar * 2127311a6408SLad Prabhakar * Control lines polarity can be configured through 2128311a6408SLad Prabhakar * devicetree endpoint control lines properties. 2129311a6408SLad Prabhakar * If no endpoint control lines properties are set, 2130311a6408SLad Prabhakar * polarity will be as below: 2131311a6408SLad Prabhakar * - VSYNC: active high 2132311a6408SLad Prabhakar * - HREF: active low 2133311a6408SLad Prabhakar * - PCLK: active low 213468579b32SHugues Fruchet * 213568579b32SHugues Fruchet * VSYNC & HREF are not configured if BT656 bus mode is selected 2136311a6408SLad Prabhakar */ 213768579b32SHugues Fruchet 213868579b32SHugues Fruchet /* 213968579b32SHugues Fruchet * BT656 embedded synchronization configuration 214068579b32SHugues Fruchet * 214168579b32SHugues Fruchet * CCIR656 CTRL00 214268579b32SHugues Fruchet * - [7]: SYNC code selection (0: auto generate sync code, 214368579b32SHugues Fruchet * 1: sync code from regs 0x4732-0x4735) 214468579b32SHugues Fruchet * - [6]: f value in CCIR656 SYNC code when fixed f value 214568579b32SHugues Fruchet * - [5]: Fixed f value 214668579b32SHugues Fruchet * - [4:3]: Blank toggle data options (00: data=1'h040/1'h200, 214768579b32SHugues Fruchet * 01: data from regs 0x4736-0x4738, 10: always keep 0) 214868579b32SHugues Fruchet * - [1]: Clip data disable 214968579b32SHugues Fruchet * - [0]: CCIR656 mode enable 215068579b32SHugues Fruchet * 215168579b32SHugues Fruchet * Default CCIR656 SAV/EAV mode with default codes 215268579b32SHugues Fruchet * SAV=0xff000080 & EAV=0xff00009d is enabled here with settings: 215368579b32SHugues Fruchet * - CCIR656 mode enable 215468579b32SHugues Fruchet * - auto generation of sync codes 215568579b32SHugues Fruchet * - blank toggle data 1'h040/1'h200 215668579b32SHugues Fruchet * - clip reserved data (0x00 & 0xff changed to 0x01 & 0xfe) 215768579b32SHugues Fruchet */ 215868579b32SHugues Fruchet ret = ov5640_write_reg(sensor, OV5640_REG_CCIR656_CTRL00, 215968579b32SHugues Fruchet bt656 ? 0x01 : 0x00); 216068579b32SHugues Fruchet if (ret) 216168579b32SHugues Fruchet return ret; 216268579b32SHugues Fruchet 2163311a6408SLad Prabhakar /* 2164311a6408SLad Prabhakar * configure parallel port control lines polarity 2165311a6408SLad Prabhakar * 2166311a6408SLad Prabhakar * POLARITY CTRL0 2167311a6408SLad Prabhakar * - [5]: PCLK polarity (0: active low, 1: active high) 2168311a6408SLad Prabhakar * - [1]: HREF polarity (0: active low, 1: active high) 2169311a6408SLad Prabhakar * - [0]: VSYNC polarity (mismatch here between 2170311a6408SLad Prabhakar * datasheet and hardware, 0 is active high 2171311a6408SLad Prabhakar * and 1 is active low...) 2172311a6408SLad Prabhakar */ 217368579b32SHugues Fruchet if (!bt656) { 2174311a6408SLad Prabhakar if (flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH) 217568579b32SHugues Fruchet polarities |= BIT(1); 2176311a6408SLad Prabhakar if (flags & V4L2_MBUS_VSYNC_ACTIVE_LOW) 217768579b32SHugues Fruchet polarities |= BIT(0); 217868579b32SHugues Fruchet } 217968579b32SHugues Fruchet if (flags & V4L2_MBUS_PCLK_SAMPLE_RISING) 218068579b32SHugues Fruchet polarities |= BIT(5); 2181311a6408SLad Prabhakar 218268579b32SHugues Fruchet ret = ov5640_write_reg(sensor, OV5640_REG_POLARITY_CTRL00, polarities); 2183311a6408SLad Prabhakar if (ret) 2184311a6408SLad Prabhakar return ret; 2185311a6408SLad Prabhakar 2186311a6408SLad Prabhakar /* 218768579b32SHugues Fruchet * powerdown MIPI TX/RX PHY & enable DVP 2188311a6408SLad Prabhakar * 2189311a6408SLad Prabhakar * MIPI CONTROL 00 219068579b32SHugues Fruchet * [4] = 1 : Power down MIPI HS Tx 219168579b32SHugues Fruchet * [3] = 1 : Power down MIPI LS Rx 219268579b32SHugues Fruchet * [2] = 0 : DVP enable (MIPI disable) 2193311a6408SLad Prabhakar */ 2194311a6408SLad Prabhakar ret = ov5640_write_reg(sensor, OV5640_REG_IO_MIPI_CTRL00, 0x18); 2195311a6408SLad Prabhakar if (ret) 2196311a6408SLad Prabhakar return ret; 2197311a6408SLad Prabhakar 2198311a6408SLad Prabhakar /* 2199576f5d4bSLad Prabhakar * enable VSYNC/HREF/PCLK DVP control lines 2200576f5d4bSLad Prabhakar * & D[9:6] DVP data lines 2201576f5d4bSLad Prabhakar * 2202576f5d4bSLad Prabhakar * PAD OUTPUT ENABLE 01 2203576f5d4bSLad Prabhakar * - 6: VSYNC output enable 2204576f5d4bSLad Prabhakar * - 5: HREF output enable 2205576f5d4bSLad Prabhakar * - 4: PCLK output enable 2206576f5d4bSLad Prabhakar * - [3:0]: D[9:6] output enable 2207576f5d4bSLad Prabhakar */ 22084039b037SLad Prabhakar ret = ov5640_write_reg(sensor, OV5640_REG_PAD_OUTPUT_ENABLE01, 220968579b32SHugues Fruchet bt656 ? 0x1f : 0x7f); 2210576f5d4bSLad Prabhakar if (ret) 2211576f5d4bSLad Prabhakar return ret; 2212576f5d4bSLad Prabhakar 2213576f5d4bSLad Prabhakar /* 2214576f5d4bSLad Prabhakar * enable D[5:0] DVP data lines 2215576f5d4bSLad Prabhakar * 2216576f5d4bSLad Prabhakar * PAD OUTPUT ENABLE 02 2217576f5d4bSLad Prabhakar * - [7:2]: D[5:0] output enable 2218576f5d4bSLad Prabhakar */ 2219576f5d4bSLad Prabhakar return ov5640_write_reg(sensor, OV5640_REG_PAD_OUTPUT_ENABLE02, 0xfc); 2220576f5d4bSLad Prabhakar } 2221576f5d4bSLad Prabhakar 22220f7acb52SHugues Fruchet static int ov5640_set_power(struct ov5640_dev *sensor, bool on) 22230f7acb52SHugues Fruchet { 22240f7acb52SHugues Fruchet int ret = 0; 22250f7acb52SHugues Fruchet 22260f7acb52SHugues Fruchet if (on) { 22270f7acb52SHugues Fruchet ret = ov5640_set_power_on(sensor); 22280f7acb52SHugues Fruchet if (ret) 22290f7acb52SHugues Fruchet return ret; 22300f7acb52SHugues Fruchet 223119a81c14SSteve Longerbeam ret = ov5640_restore_mode(sensor); 223219a81c14SSteve Longerbeam if (ret) 223319a81c14SSteve Longerbeam goto power_off; 2234b1751ae6SLad Prabhakar } 223519a81c14SSteve Longerbeam 2236576f5d4bSLad Prabhakar if (sensor->ep.bus_type == V4L2_MBUS_CSI2_DPHY) 2237b1751ae6SLad Prabhakar ret = ov5640_set_power_mipi(sensor, on); 2238576f5d4bSLad Prabhakar else 2239576f5d4bSLad Prabhakar ret = ov5640_set_power_dvp(sensor, on); 2240b1751ae6SLad Prabhakar if (ret) 2241b1751ae6SLad Prabhakar goto power_off; 2242aa4bb8b8SJacopo Mondi 2243b1751ae6SLad Prabhakar if (!on) 2244aa4bb8b8SJacopo Mondi ov5640_set_power_off(sensor); 224519a81c14SSteve Longerbeam 224619a81c14SSteve Longerbeam return 0; 224719a81c14SSteve Longerbeam 224819a81c14SSteve Longerbeam power_off: 22490f7acb52SHugues Fruchet ov5640_set_power_off(sensor); 225019a81c14SSteve Longerbeam return ret; 225119a81c14SSteve Longerbeam } 225219a81c14SSteve Longerbeam 225319a81c14SSteve Longerbeam /* --------------- Subdev Operations --------------- */ 225419a81c14SSteve Longerbeam 225519a81c14SSteve Longerbeam static int ov5640_s_power(struct v4l2_subdev *sd, int on) 225619a81c14SSteve Longerbeam { 225719a81c14SSteve Longerbeam struct ov5640_dev *sensor = to_ov5640_dev(sd); 225819a81c14SSteve Longerbeam int ret = 0; 225919a81c14SSteve Longerbeam 226019a81c14SSteve Longerbeam mutex_lock(&sensor->lock); 226119a81c14SSteve Longerbeam 226219a81c14SSteve Longerbeam /* 226319a81c14SSteve Longerbeam * If the power count is modified from 0 to != 0 or from != 0 to 0, 226419a81c14SSteve Longerbeam * update the power state. 226519a81c14SSteve Longerbeam */ 226619a81c14SSteve Longerbeam if (sensor->power_count == !on) { 226719a81c14SSteve Longerbeam ret = ov5640_set_power(sensor, !!on); 226819a81c14SSteve Longerbeam if (ret) 226919a81c14SSteve Longerbeam goto out; 227019a81c14SSteve Longerbeam } 227119a81c14SSteve Longerbeam 227219a81c14SSteve Longerbeam /* Update the power count. */ 227319a81c14SSteve Longerbeam sensor->power_count += on ? 1 : -1; 227419a81c14SSteve Longerbeam WARN_ON(sensor->power_count < 0); 227519a81c14SSteve Longerbeam out: 227619a81c14SSteve Longerbeam mutex_unlock(&sensor->lock); 227719a81c14SSteve Longerbeam 227819a81c14SSteve Longerbeam if (on && !ret && sensor->power_count == 1) { 227919a81c14SSteve Longerbeam /* restore controls */ 228019a81c14SSteve Longerbeam ret = v4l2_ctrl_handler_setup(&sensor->ctrls.handler); 228119a81c14SSteve Longerbeam } 228219a81c14SSteve Longerbeam 228319a81c14SSteve Longerbeam return ret; 228419a81c14SSteve Longerbeam } 228519a81c14SSteve Longerbeam 228619a81c14SSteve Longerbeam static int ov5640_try_frame_interval(struct ov5640_dev *sensor, 228719a81c14SSteve Longerbeam struct v4l2_fract *fi, 228819a81c14SSteve Longerbeam u32 width, u32 height) 228919a81c14SSteve Longerbeam { 229019a81c14SSteve Longerbeam const struct ov5640_mode_info *mode; 22916530a5ebSJagan Teki enum ov5640_frame_rate rate = OV5640_15_FPS; 2292f6cc192fSMaxime Ripard int minfps, maxfps, best_fps, fps; 2293f6cc192fSMaxime Ripard int i; 229419a81c14SSteve Longerbeam 229519a81c14SSteve Longerbeam minfps = ov5640_framerates[OV5640_15_FPS]; 2296e823fb16SMaxime Ripard maxfps = ov5640_framerates[OV5640_60_FPS]; 229719a81c14SSteve Longerbeam 229819a81c14SSteve Longerbeam if (fi->numerator == 0) { 229919a81c14SSteve Longerbeam fi->denominator = maxfps; 230019a81c14SSteve Longerbeam fi->numerator = 1; 2301e823fb16SMaxime Ripard rate = OV5640_60_FPS; 2302e823fb16SMaxime Ripard goto find_mode; 230319a81c14SSteve Longerbeam } 230419a81c14SSteve Longerbeam 2305f6cc192fSMaxime Ripard fps = clamp_val(DIV_ROUND_CLOSEST(fi->denominator, fi->numerator), 2306f6cc192fSMaxime Ripard minfps, maxfps); 2307f6cc192fSMaxime Ripard 2308f6cc192fSMaxime Ripard best_fps = minfps; 2309f6cc192fSMaxime Ripard for (i = 0; i < ARRAY_SIZE(ov5640_framerates); i++) { 2310f6cc192fSMaxime Ripard int curr_fps = ov5640_framerates[i]; 2311f6cc192fSMaxime Ripard 2312f6cc192fSMaxime Ripard if (abs(curr_fps - fps) < abs(best_fps - fps)) { 2313f6cc192fSMaxime Ripard best_fps = curr_fps; 2314f6cc192fSMaxime Ripard rate = i; 2315f6cc192fSMaxime Ripard } 2316f6cc192fSMaxime Ripard } 231719a81c14SSteve Longerbeam 231819a81c14SSteve Longerbeam fi->numerator = 1; 2319f6cc192fSMaxime Ripard fi->denominator = best_fps; 232019a81c14SSteve Longerbeam 2321e823fb16SMaxime Ripard find_mode: 23225a3ad937SMaxime Ripard mode = ov5640_find_mode(sensor, rate, width, height, false); 23235a3ad937SMaxime Ripard return mode ? rate : -EINVAL; 232419a81c14SSteve Longerbeam } 232519a81c14SSteve Longerbeam 232619a81c14SSteve Longerbeam static int ov5640_get_fmt(struct v4l2_subdev *sd, 23270d346d2aSTomi Valkeinen struct v4l2_subdev_state *sd_state, 232819a81c14SSteve Longerbeam struct v4l2_subdev_format *format) 232919a81c14SSteve Longerbeam { 233019a81c14SSteve Longerbeam struct ov5640_dev *sensor = to_ov5640_dev(sd); 233119a81c14SSteve Longerbeam struct v4l2_mbus_framefmt *fmt; 233219a81c14SSteve Longerbeam 233319a81c14SSteve Longerbeam if (format->pad != 0) 233419a81c14SSteve Longerbeam return -EINVAL; 233519a81c14SSteve Longerbeam 233619a81c14SSteve Longerbeam mutex_lock(&sensor->lock); 233719a81c14SSteve Longerbeam 233819a81c14SSteve Longerbeam if (format->which == V4L2_SUBDEV_FORMAT_TRY) 23390d346d2aSTomi Valkeinen fmt = v4l2_subdev_get_try_format(&sensor->sd, sd_state, 234019a81c14SSteve Longerbeam format->pad); 234119a81c14SSteve Longerbeam else 234219a81c14SSteve Longerbeam fmt = &sensor->fmt; 234319a81c14SSteve Longerbeam 234419a81c14SSteve Longerbeam format->format = *fmt; 234519a81c14SSteve Longerbeam 234619a81c14SSteve Longerbeam mutex_unlock(&sensor->lock); 234719a81c14SSteve Longerbeam 234819a81c14SSteve Longerbeam return 0; 234919a81c14SSteve Longerbeam } 235019a81c14SSteve Longerbeam 235119a81c14SSteve Longerbeam static int ov5640_try_fmt_internal(struct v4l2_subdev *sd, 235219a81c14SSteve Longerbeam struct v4l2_mbus_framefmt *fmt, 235319a81c14SSteve Longerbeam enum ov5640_frame_rate fr, 235419a81c14SSteve Longerbeam const struct ov5640_mode_info **new_mode) 235519a81c14SSteve Longerbeam { 235619a81c14SSteve Longerbeam struct ov5640_dev *sensor = to_ov5640_dev(sd); 235719a81c14SSteve Longerbeam const struct ov5640_mode_info *mode; 2358e3ee691dSHugues Fruchet int i; 235919a81c14SSteve Longerbeam 236019a81c14SSteve Longerbeam mode = ov5640_find_mode(sensor, fr, fmt->width, fmt->height, true); 236119a81c14SSteve Longerbeam if (!mode) 236219a81c14SSteve Longerbeam return -EINVAL; 2363dba13a0bSMaxime Ripard fmt->width = mode->hact; 2364dba13a0bSMaxime Ripard fmt->height = mode->vact; 236519a81c14SSteve Longerbeam 236619a81c14SSteve Longerbeam if (new_mode) 236719a81c14SSteve Longerbeam *new_mode = mode; 2368e3ee691dSHugues Fruchet 2369e3ee691dSHugues Fruchet for (i = 0; i < ARRAY_SIZE(ov5640_formats); i++) 2370e3ee691dSHugues Fruchet if (ov5640_formats[i].code == fmt->code) 2371e3ee691dSHugues Fruchet break; 2372e3ee691dSHugues Fruchet if (i >= ARRAY_SIZE(ov5640_formats)) 2373e6441fdeSHugues Fruchet i = 0; 2374e6441fdeSHugues Fruchet 2375e6441fdeSHugues Fruchet fmt->code = ov5640_formats[i].code; 2376e6441fdeSHugues Fruchet fmt->colorspace = ov5640_formats[i].colorspace; 2377e6441fdeSHugues Fruchet fmt->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(fmt->colorspace); 2378e6441fdeSHugues Fruchet fmt->quantization = V4L2_QUANTIZATION_FULL_RANGE; 2379e6441fdeSHugues Fruchet fmt->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(fmt->colorspace); 2380e3ee691dSHugues Fruchet 238119a81c14SSteve Longerbeam return 0; 238219a81c14SSteve Longerbeam } 238319a81c14SSteve Longerbeam 238419a81c14SSteve Longerbeam static int ov5640_set_fmt(struct v4l2_subdev *sd, 23850d346d2aSTomi Valkeinen struct v4l2_subdev_state *sd_state, 238619a81c14SSteve Longerbeam struct v4l2_subdev_format *format) 238719a81c14SSteve Longerbeam { 238819a81c14SSteve Longerbeam struct ov5640_dev *sensor = to_ov5640_dev(sd); 238919a81c14SSteve Longerbeam const struct ov5640_mode_info *new_mode; 2390e6441fdeSHugues Fruchet struct v4l2_mbus_framefmt *mbus_fmt = &format->format; 239119a81c14SSteve Longerbeam int ret; 239219a81c14SSteve Longerbeam 239319a81c14SSteve Longerbeam if (format->pad != 0) 239419a81c14SSteve Longerbeam return -EINVAL; 239519a81c14SSteve Longerbeam 239619a81c14SSteve Longerbeam mutex_lock(&sensor->lock); 239719a81c14SSteve Longerbeam 239819a81c14SSteve Longerbeam if (sensor->streaming) { 239919a81c14SSteve Longerbeam ret = -EBUSY; 240019a81c14SSteve Longerbeam goto out; 240119a81c14SSteve Longerbeam } 240219a81c14SSteve Longerbeam 2403e6441fdeSHugues Fruchet ret = ov5640_try_fmt_internal(sd, mbus_fmt, 240419a81c14SSteve Longerbeam sensor->current_fr, &new_mode); 240519a81c14SSteve Longerbeam if (ret) 240619a81c14SSteve Longerbeam goto out; 240719a81c14SSteve Longerbeam 2408e738f5ddSMirela Rabulea if (format->which == V4L2_SUBDEV_FORMAT_TRY) { 2409e738f5ddSMirela Rabulea *v4l2_subdev_get_try_format(sd, sd_state, 0) = *mbus_fmt; 2410e738f5ddSMirela Rabulea goto out; 2411e738f5ddSMirela Rabulea } 241219a81c14SSteve Longerbeam 24136949d864SHugues Fruchet if (new_mode != sensor->current_mode) { 241419a81c14SSteve Longerbeam sensor->current_mode = new_mode; 241519a81c14SSteve Longerbeam sensor->pending_mode_change = true; 24166949d864SHugues Fruchet } 241707115449SJacopo Mondi if (mbus_fmt->code != sensor->fmt.code) 2418fb98e29fSHugues Fruchet sensor->pending_fmt_change = true; 241907115449SJacopo Mondi 2420e738f5ddSMirela Rabulea /* update format even if code is unchanged, resolution might change */ 2421e738f5ddSMirela Rabulea sensor->fmt = *mbus_fmt; 2422e738f5ddSMirela Rabulea 2423cc196e48SBenoit Parrot __v4l2_ctrl_s_ctrl_int64(sensor->ctrls.pixel_rate, 2424cc196e48SBenoit Parrot ov5640_calc_pixel_rate(sensor)); 242519a81c14SSteve Longerbeam out: 242619a81c14SSteve Longerbeam mutex_unlock(&sensor->lock); 242719a81c14SSteve Longerbeam return ret; 242819a81c14SSteve Longerbeam } 242919a81c14SSteve Longerbeam 2430e3ee691dSHugues Fruchet static int ov5640_set_framefmt(struct ov5640_dev *sensor, 2431e3ee691dSHugues Fruchet struct v4l2_mbus_framefmt *format) 2432e3ee691dSHugues Fruchet { 2433e3ee691dSHugues Fruchet int ret = 0; 2434d47c4126SHugues Fruchet bool is_jpeg = false; 2435b7ed3abdSLoic Poulain u8 fmt, mux; 2436e3ee691dSHugues Fruchet 2437e3ee691dSHugues Fruchet switch (format->code) { 24381536fbdbSXavier Roumegue case MEDIA_BUS_FMT_UYVY8_1X16: 2439e3ee691dSHugues Fruchet case MEDIA_BUS_FMT_UYVY8_2X8: 2440e3ee691dSHugues Fruchet /* YUV422, UYVY */ 2441b7ed3abdSLoic Poulain fmt = 0x3f; 2442b7ed3abdSLoic Poulain mux = OV5640_FMT_MUX_YUV422; 2443e3ee691dSHugues Fruchet break; 24441536fbdbSXavier Roumegue case MEDIA_BUS_FMT_YUYV8_1X16: 2445e3ee691dSHugues Fruchet case MEDIA_BUS_FMT_YUYV8_2X8: 2446e3ee691dSHugues Fruchet /* YUV422, YUYV */ 2447b7ed3abdSLoic Poulain fmt = 0x30; 2448b7ed3abdSLoic Poulain mux = OV5640_FMT_MUX_YUV422; 2449e3ee691dSHugues Fruchet break; 2450e3ee691dSHugues Fruchet case MEDIA_BUS_FMT_RGB565_2X8_LE: 2451e3ee691dSHugues Fruchet /* RGB565 {g[2:0],b[4:0]},{r[4:0],g[5:3]} */ 2452b7ed3abdSLoic Poulain fmt = 0x6F; 2453b7ed3abdSLoic Poulain mux = OV5640_FMT_MUX_RGB; 2454e3ee691dSHugues Fruchet break; 2455e3ee691dSHugues Fruchet case MEDIA_BUS_FMT_RGB565_2X8_BE: 2456e3ee691dSHugues Fruchet /* RGB565 {r[4:0],g[5:3]},{g[2:0],b[4:0]} */ 2457b7ed3abdSLoic Poulain fmt = 0x61; 2458b7ed3abdSLoic Poulain mux = OV5640_FMT_MUX_RGB; 2459e3ee691dSHugues Fruchet break; 2460d47c4126SHugues Fruchet case MEDIA_BUS_FMT_JPEG_1X8: 2461d47c4126SHugues Fruchet /* YUV422, YUYV */ 2462b7ed3abdSLoic Poulain fmt = 0x30; 2463b7ed3abdSLoic Poulain mux = OV5640_FMT_MUX_YUV422; 2464d47c4126SHugues Fruchet is_jpeg = true; 2465d47c4126SHugues Fruchet break; 2466b7ed3abdSLoic Poulain case MEDIA_BUS_FMT_SBGGR8_1X8: 2467b7ed3abdSLoic Poulain /* Raw, BGBG... / GRGR... */ 2468b7ed3abdSLoic Poulain fmt = 0x00; 2469b7ed3abdSLoic Poulain mux = OV5640_FMT_MUX_RAW_DPC; 2470b7ed3abdSLoic Poulain break; 2471b7ed3abdSLoic Poulain case MEDIA_BUS_FMT_SGBRG8_1X8: 2472b7ed3abdSLoic Poulain /* Raw bayer, GBGB... / RGRG... */ 2473b7ed3abdSLoic Poulain fmt = 0x01; 2474b7ed3abdSLoic Poulain mux = OV5640_FMT_MUX_RAW_DPC; 2475b7ed3abdSLoic Poulain break; 2476b7ed3abdSLoic Poulain case MEDIA_BUS_FMT_SGRBG8_1X8: 2477b7ed3abdSLoic Poulain /* Raw bayer, GRGR... / BGBG... */ 2478b7ed3abdSLoic Poulain fmt = 0x02; 2479b7ed3abdSLoic Poulain mux = OV5640_FMT_MUX_RAW_DPC; 2480b7ed3abdSLoic Poulain break; 2481b7ed3abdSLoic Poulain case MEDIA_BUS_FMT_SRGGB8_1X8: 2482b7ed3abdSLoic Poulain /* Raw bayer, RGRG... / GBGB... */ 2483b7ed3abdSLoic Poulain fmt = 0x03; 2484b7ed3abdSLoic Poulain mux = OV5640_FMT_MUX_RAW_DPC; 2485b7ed3abdSLoic Poulain break; 2486e3ee691dSHugues Fruchet default: 2487e3ee691dSHugues Fruchet return -EINVAL; 2488e3ee691dSHugues Fruchet } 2489e3ee691dSHugues Fruchet 2490e3ee691dSHugues Fruchet /* FORMAT CONTROL00: YUV and RGB formatting */ 2491b7ed3abdSLoic Poulain ret = ov5640_write_reg(sensor, OV5640_REG_FORMAT_CONTROL00, fmt); 2492e3ee691dSHugues Fruchet if (ret) 2493e3ee691dSHugues Fruchet return ret; 2494e3ee691dSHugues Fruchet 2495e3ee691dSHugues Fruchet /* FORMAT MUX CONTROL: ISP YUV or RGB */ 2496b7ed3abdSLoic Poulain ret = ov5640_write_reg(sensor, OV5640_REG_ISP_FORMAT_MUX_CTRL, mux); 2497d47c4126SHugues Fruchet if (ret) 2498d47c4126SHugues Fruchet return ret; 2499d47c4126SHugues Fruchet 2500d47c4126SHugues Fruchet /* 2501d47c4126SHugues Fruchet * TIMING TC REG21: 2502d47c4126SHugues Fruchet * - [5]: JPEG enable 2503d47c4126SHugues Fruchet */ 2504d47c4126SHugues Fruchet ret = ov5640_mod_reg(sensor, OV5640_REG_TIMING_TC_REG21, 2505d47c4126SHugues Fruchet BIT(5), is_jpeg ? BIT(5) : 0); 2506d47c4126SHugues Fruchet if (ret) 2507d47c4126SHugues Fruchet return ret; 2508d47c4126SHugues Fruchet 2509d47c4126SHugues Fruchet /* 2510d47c4126SHugues Fruchet * SYSTEM RESET02: 2511d47c4126SHugues Fruchet * - [4]: Reset JFIFO 2512d47c4126SHugues Fruchet * - [3]: Reset SFIFO 2513d47c4126SHugues Fruchet * - [2]: Reset JPEG 2514d47c4126SHugues Fruchet */ 2515d47c4126SHugues Fruchet ret = ov5640_mod_reg(sensor, OV5640_REG_SYS_RESET02, 2516d47c4126SHugues Fruchet BIT(4) | BIT(3) | BIT(2), 2517d47c4126SHugues Fruchet is_jpeg ? 0 : (BIT(4) | BIT(3) | BIT(2))); 2518d47c4126SHugues Fruchet if (ret) 2519d47c4126SHugues Fruchet return ret; 2520d47c4126SHugues Fruchet 2521d47c4126SHugues Fruchet /* 2522d47c4126SHugues Fruchet * CLOCK ENABLE02: 2523d47c4126SHugues Fruchet * - [5]: Enable JPEG 2x clock 2524d47c4126SHugues Fruchet * - [3]: Enable JPEG clock 2525d47c4126SHugues Fruchet */ 2526d47c4126SHugues Fruchet return ov5640_mod_reg(sensor, OV5640_REG_SYS_CLOCK_ENABLE02, 2527d47c4126SHugues Fruchet BIT(5) | BIT(3), 2528d47c4126SHugues Fruchet is_jpeg ? (BIT(5) | BIT(3)) : 0); 2529e3ee691dSHugues Fruchet } 253019a81c14SSteve Longerbeam 253119a81c14SSteve Longerbeam /* 253219a81c14SSteve Longerbeam * Sensor Controls. 253319a81c14SSteve Longerbeam */ 253419a81c14SSteve Longerbeam 253519a81c14SSteve Longerbeam static int ov5640_set_ctrl_hue(struct ov5640_dev *sensor, int value) 253619a81c14SSteve Longerbeam { 253719a81c14SSteve Longerbeam int ret; 253819a81c14SSteve Longerbeam 253919a81c14SSteve Longerbeam if (value) { 254019a81c14SSteve Longerbeam ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0, 254119a81c14SSteve Longerbeam BIT(0), BIT(0)); 254219a81c14SSteve Longerbeam if (ret) 254319a81c14SSteve Longerbeam return ret; 254419a81c14SSteve Longerbeam ret = ov5640_write_reg16(sensor, OV5640_REG_SDE_CTRL1, value); 254519a81c14SSteve Longerbeam } else { 254619a81c14SSteve Longerbeam ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0, BIT(0), 0); 254719a81c14SSteve Longerbeam } 254819a81c14SSteve Longerbeam 254919a81c14SSteve Longerbeam return ret; 255019a81c14SSteve Longerbeam } 255119a81c14SSteve Longerbeam 255219a81c14SSteve Longerbeam static int ov5640_set_ctrl_contrast(struct ov5640_dev *sensor, int value) 255319a81c14SSteve Longerbeam { 255419a81c14SSteve Longerbeam int ret; 255519a81c14SSteve Longerbeam 255619a81c14SSteve Longerbeam if (value) { 255719a81c14SSteve Longerbeam ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0, 255819a81c14SSteve Longerbeam BIT(2), BIT(2)); 255919a81c14SSteve Longerbeam if (ret) 256019a81c14SSteve Longerbeam return ret; 256119a81c14SSteve Longerbeam ret = ov5640_write_reg(sensor, OV5640_REG_SDE_CTRL5, 256219a81c14SSteve Longerbeam value & 0xff); 256319a81c14SSteve Longerbeam } else { 256419a81c14SSteve Longerbeam ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0, BIT(2), 0); 256519a81c14SSteve Longerbeam } 256619a81c14SSteve Longerbeam 256719a81c14SSteve Longerbeam return ret; 256819a81c14SSteve Longerbeam } 256919a81c14SSteve Longerbeam 257019a81c14SSteve Longerbeam static int ov5640_set_ctrl_saturation(struct ov5640_dev *sensor, int value) 257119a81c14SSteve Longerbeam { 257219a81c14SSteve Longerbeam int ret; 257319a81c14SSteve Longerbeam 257419a81c14SSteve Longerbeam if (value) { 257519a81c14SSteve Longerbeam ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0, 257619a81c14SSteve Longerbeam BIT(1), BIT(1)); 257719a81c14SSteve Longerbeam if (ret) 257819a81c14SSteve Longerbeam return ret; 257919a81c14SSteve Longerbeam ret = ov5640_write_reg(sensor, OV5640_REG_SDE_CTRL3, 258019a81c14SSteve Longerbeam value & 0xff); 258119a81c14SSteve Longerbeam if (ret) 258219a81c14SSteve Longerbeam return ret; 258319a81c14SSteve Longerbeam ret = ov5640_write_reg(sensor, OV5640_REG_SDE_CTRL4, 258419a81c14SSteve Longerbeam value & 0xff); 258519a81c14SSteve Longerbeam } else { 258619a81c14SSteve Longerbeam ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0, BIT(1), 0); 258719a81c14SSteve Longerbeam } 258819a81c14SSteve Longerbeam 258919a81c14SSteve Longerbeam return ret; 259019a81c14SSteve Longerbeam } 259119a81c14SSteve Longerbeam 259219a81c14SSteve Longerbeam static int ov5640_set_ctrl_white_balance(struct ov5640_dev *sensor, int awb) 259319a81c14SSteve Longerbeam { 259419a81c14SSteve Longerbeam int ret; 259519a81c14SSteve Longerbeam 259619a81c14SSteve Longerbeam ret = ov5640_mod_reg(sensor, OV5640_REG_AWB_MANUAL_CTRL, 259719a81c14SSteve Longerbeam BIT(0), awb ? 0 : 1); 259819a81c14SSteve Longerbeam if (ret) 259919a81c14SSteve Longerbeam return ret; 260019a81c14SSteve Longerbeam 260119a81c14SSteve Longerbeam if (!awb) { 260219a81c14SSteve Longerbeam u16 red = (u16)sensor->ctrls.red_balance->val; 260319a81c14SSteve Longerbeam u16 blue = (u16)sensor->ctrls.blue_balance->val; 260419a81c14SSteve Longerbeam 260519a81c14SSteve Longerbeam ret = ov5640_write_reg16(sensor, OV5640_REG_AWB_R_GAIN, red); 260619a81c14SSteve Longerbeam if (ret) 260719a81c14SSteve Longerbeam return ret; 260819a81c14SSteve Longerbeam ret = ov5640_write_reg16(sensor, OV5640_REG_AWB_B_GAIN, blue); 260919a81c14SSteve Longerbeam } 261019a81c14SSteve Longerbeam 261119a81c14SSteve Longerbeam return ret; 261219a81c14SSteve Longerbeam } 261319a81c14SSteve Longerbeam 26143cca8ef5SHugues Fruchet static int ov5640_set_ctrl_exposure(struct ov5640_dev *sensor, 26153cca8ef5SHugues Fruchet enum v4l2_exposure_auto_type auto_exposure) 261619a81c14SSteve Longerbeam { 261719a81c14SSteve Longerbeam struct ov5640_ctrls *ctrls = &sensor->ctrls; 26183cca8ef5SHugues Fruchet bool auto_exp = (auto_exposure == V4L2_EXPOSURE_AUTO); 261919a81c14SSteve Longerbeam int ret = 0; 262019a81c14SSteve Longerbeam 262119a81c14SSteve Longerbeam if (ctrls->auto_exp->is_new) { 26223cca8ef5SHugues Fruchet ret = ov5640_set_autoexposure(sensor, auto_exp); 262319a81c14SSteve Longerbeam if (ret) 262419a81c14SSteve Longerbeam return ret; 262519a81c14SSteve Longerbeam } 262619a81c14SSteve Longerbeam 26273cca8ef5SHugues Fruchet if (!auto_exp && ctrls->exposure->is_new) { 262819a81c14SSteve Longerbeam u16 max_exp; 262919a81c14SSteve Longerbeam 263019a81c14SSteve Longerbeam ret = ov5640_read_reg16(sensor, OV5640_REG_AEC_PK_VTS, 263119a81c14SSteve Longerbeam &max_exp); 263219a81c14SSteve Longerbeam if (ret) 263319a81c14SSteve Longerbeam return ret; 263419a81c14SSteve Longerbeam ret = ov5640_get_vts(sensor); 263519a81c14SSteve Longerbeam if (ret < 0) 263619a81c14SSteve Longerbeam return ret; 263719a81c14SSteve Longerbeam max_exp += ret; 26386146fde3SHugues Fruchet ret = 0; 263919a81c14SSteve Longerbeam 264019a81c14SSteve Longerbeam if (ctrls->exposure->val < max_exp) 264119a81c14SSteve Longerbeam ret = ov5640_set_exposure(sensor, ctrls->exposure->val); 264219a81c14SSteve Longerbeam } 264319a81c14SSteve Longerbeam 264419a81c14SSteve Longerbeam return ret; 264519a81c14SSteve Longerbeam } 264619a81c14SSteve Longerbeam 26473cca8ef5SHugues Fruchet static int ov5640_set_ctrl_gain(struct ov5640_dev *sensor, bool auto_gain) 264819a81c14SSteve Longerbeam { 264919a81c14SSteve Longerbeam struct ov5640_ctrls *ctrls = &sensor->ctrls; 265019a81c14SSteve Longerbeam int ret = 0; 265119a81c14SSteve Longerbeam 265219a81c14SSteve Longerbeam if (ctrls->auto_gain->is_new) { 26533cca8ef5SHugues Fruchet ret = ov5640_set_autogain(sensor, auto_gain); 265419a81c14SSteve Longerbeam if (ret) 265519a81c14SSteve Longerbeam return ret; 265619a81c14SSteve Longerbeam } 265719a81c14SSteve Longerbeam 26583cca8ef5SHugues Fruchet if (!auto_gain && ctrls->gain->is_new) 26593cca8ef5SHugues Fruchet ret = ov5640_set_gain(sensor, ctrls->gain->val); 266019a81c14SSteve Longerbeam 266119a81c14SSteve Longerbeam return ret; 266219a81c14SSteve Longerbeam } 266319a81c14SSteve Longerbeam 26649f6d7bacSChen-Yu Tsai static const char * const test_pattern_menu[] = { 26659f6d7bacSChen-Yu Tsai "Disabled", 26669f6d7bacSChen-Yu Tsai "Color bars", 2667bddc5cdfSChen-Yu Tsai "Color bars w/ rolling bar", 2668bddc5cdfSChen-Yu Tsai "Color squares", 2669bddc5cdfSChen-Yu Tsai "Color squares w/ rolling bar", 26709f6d7bacSChen-Yu Tsai }; 26719f6d7bacSChen-Yu Tsai 2672a0c29afbSChen-Yu Tsai #define OV5640_TEST_ENABLE BIT(7) 2673a0c29afbSChen-Yu Tsai #define OV5640_TEST_ROLLING BIT(6) /* rolling horizontal bar */ 2674a0c29afbSChen-Yu Tsai #define OV5640_TEST_TRANSPARENT BIT(5) 2675a0c29afbSChen-Yu Tsai #define OV5640_TEST_SQUARE_BW BIT(4) /* black & white squares */ 2676a0c29afbSChen-Yu Tsai #define OV5640_TEST_BAR_STANDARD (0 << 2) 2677a0c29afbSChen-Yu Tsai #define OV5640_TEST_BAR_VERT_CHANGE_1 (1 << 2) 2678a0c29afbSChen-Yu Tsai #define OV5640_TEST_BAR_HOR_CHANGE (2 << 2) 2679a0c29afbSChen-Yu Tsai #define OV5640_TEST_BAR_VERT_CHANGE_2 (3 << 2) 2680a0c29afbSChen-Yu Tsai #define OV5640_TEST_BAR (0 << 0) 2681a0c29afbSChen-Yu Tsai #define OV5640_TEST_RANDOM (1 << 0) 2682a0c29afbSChen-Yu Tsai #define OV5640_TEST_SQUARE (2 << 0) 2683a0c29afbSChen-Yu Tsai #define OV5640_TEST_BLACK (3 << 0) 2684a0c29afbSChen-Yu Tsai 2685a0c29afbSChen-Yu Tsai static const u8 test_pattern_val[] = { 2686a0c29afbSChen-Yu Tsai 0, 26872aff1fc3SChen-Yu Tsai OV5640_TEST_ENABLE | OV5640_TEST_BAR_VERT_CHANGE_1 | 2688a0c29afbSChen-Yu Tsai OV5640_TEST_BAR, 2689bddc5cdfSChen-Yu Tsai OV5640_TEST_ENABLE | OV5640_TEST_ROLLING | 2690bddc5cdfSChen-Yu Tsai OV5640_TEST_BAR_VERT_CHANGE_1 | OV5640_TEST_BAR, 2691bddc5cdfSChen-Yu Tsai OV5640_TEST_ENABLE | OV5640_TEST_SQUARE, 2692bddc5cdfSChen-Yu Tsai OV5640_TEST_ENABLE | OV5640_TEST_ROLLING | OV5640_TEST_SQUARE, 2693a0c29afbSChen-Yu Tsai }; 2694a0c29afbSChen-Yu Tsai 269519a81c14SSteve Longerbeam static int ov5640_set_ctrl_test_pattern(struct ov5640_dev *sensor, int value) 269619a81c14SSteve Longerbeam { 2697a0c29afbSChen-Yu Tsai return ov5640_write_reg(sensor, OV5640_REG_PRE_ISP_TEST_SET1, 2698a0c29afbSChen-Yu Tsai test_pattern_val[value]); 269919a81c14SSteve Longerbeam } 270019a81c14SSteve Longerbeam 27011068fecaSMylène Josserand static int ov5640_set_ctrl_light_freq(struct ov5640_dev *sensor, int value) 27021068fecaSMylène Josserand { 27031068fecaSMylène Josserand int ret; 27041068fecaSMylène Josserand 27051068fecaSMylène Josserand ret = ov5640_mod_reg(sensor, OV5640_REG_HZ5060_CTRL01, BIT(7), 27061068fecaSMylène Josserand (value == V4L2_CID_POWER_LINE_FREQUENCY_AUTO) ? 27071068fecaSMylène Josserand 0 : BIT(7)); 27081068fecaSMylène Josserand if (ret) 27091068fecaSMylène Josserand return ret; 27101068fecaSMylène Josserand 27111068fecaSMylène Josserand return ov5640_mod_reg(sensor, OV5640_REG_HZ5060_CTRL00, BIT(2), 27121068fecaSMylène Josserand (value == V4L2_CID_POWER_LINE_FREQUENCY_50HZ) ? 27131068fecaSMylène Josserand BIT(2) : 0); 27141068fecaSMylène Josserand } 27151068fecaSMylène Josserand 2716ce85705aSHugues Fruchet static int ov5640_set_ctrl_hflip(struct ov5640_dev *sensor, int value) 2717ce85705aSHugues Fruchet { 2718ce85705aSHugues Fruchet /* 2719c3f3ba3eSHugues Fruchet * If sensor is mounted upside down, mirror logic is inversed. 2720c3f3ba3eSHugues Fruchet * 2721ce85705aSHugues Fruchet * Sensor is a BSI (Back Side Illuminated) one, 2722ce85705aSHugues Fruchet * so image captured is physically mirrored. 2723ce85705aSHugues Fruchet * This is why mirror logic is inversed in 2724ce85705aSHugues Fruchet * order to cancel this mirror effect. 2725ce85705aSHugues Fruchet */ 2726ce85705aSHugues Fruchet 2727ce85705aSHugues Fruchet /* 2728ce85705aSHugues Fruchet * TIMING TC REG21: 2729ce85705aSHugues Fruchet * - [2]: ISP mirror 2730ce85705aSHugues Fruchet * - [1]: Sensor mirror 2731ce85705aSHugues Fruchet */ 2732ce85705aSHugues Fruchet return ov5640_mod_reg(sensor, OV5640_REG_TIMING_TC_REG21, 2733ce85705aSHugues Fruchet BIT(2) | BIT(1), 2734c3f3ba3eSHugues Fruchet (!(value ^ sensor->upside_down)) ? 2735c3f3ba3eSHugues Fruchet (BIT(2) | BIT(1)) : 0); 2736ce85705aSHugues Fruchet } 2737ce85705aSHugues Fruchet 2738ce85705aSHugues Fruchet static int ov5640_set_ctrl_vflip(struct ov5640_dev *sensor, int value) 2739ce85705aSHugues Fruchet { 2740c3f3ba3eSHugues Fruchet /* If sensor is mounted upside down, flip logic is inversed */ 2741c3f3ba3eSHugues Fruchet 2742ce85705aSHugues Fruchet /* 2743ce85705aSHugues Fruchet * TIMING TC REG20: 2744ce85705aSHugues Fruchet * - [2]: ISP vflip 2745ce85705aSHugues Fruchet * - [1]: Sensor vflip 2746ce85705aSHugues Fruchet */ 2747ce85705aSHugues Fruchet return ov5640_mod_reg(sensor, OV5640_REG_TIMING_TC_REG20, 2748ce85705aSHugues Fruchet BIT(2) | BIT(1), 2749c3f3ba3eSHugues Fruchet (value ^ sensor->upside_down) ? 2750c3f3ba3eSHugues Fruchet (BIT(2) | BIT(1)) : 0); 2751ce85705aSHugues Fruchet } 2752ce85705aSHugues Fruchet 275319a81c14SSteve Longerbeam static int ov5640_g_volatile_ctrl(struct v4l2_ctrl *ctrl) 275419a81c14SSteve Longerbeam { 275519a81c14SSteve Longerbeam struct v4l2_subdev *sd = ctrl_to_sd(ctrl); 275619a81c14SSteve Longerbeam struct ov5640_dev *sensor = to_ov5640_dev(sd); 275719a81c14SSteve Longerbeam int val; 275819a81c14SSteve Longerbeam 275919a81c14SSteve Longerbeam /* v4l2_ctrl_lock() locks our own mutex */ 276019a81c14SSteve Longerbeam 276119a81c14SSteve Longerbeam switch (ctrl->id) { 276219a81c14SSteve Longerbeam case V4L2_CID_AUTOGAIN: 276319a81c14SSteve Longerbeam val = ov5640_get_gain(sensor); 276419a81c14SSteve Longerbeam if (val < 0) 276519a81c14SSteve Longerbeam return val; 276619a81c14SSteve Longerbeam sensor->ctrls.gain->val = val; 276719a81c14SSteve Longerbeam break; 276819a81c14SSteve Longerbeam case V4L2_CID_EXPOSURE_AUTO: 276919a81c14SSteve Longerbeam val = ov5640_get_exposure(sensor); 277019a81c14SSteve Longerbeam if (val < 0) 277119a81c14SSteve Longerbeam return val; 277219a81c14SSteve Longerbeam sensor->ctrls.exposure->val = val; 277319a81c14SSteve Longerbeam break; 277419a81c14SSteve Longerbeam } 277519a81c14SSteve Longerbeam 277619a81c14SSteve Longerbeam return 0; 277719a81c14SSteve Longerbeam } 277819a81c14SSteve Longerbeam 277919a81c14SSteve Longerbeam static int ov5640_s_ctrl(struct v4l2_ctrl *ctrl) 278019a81c14SSteve Longerbeam { 278119a81c14SSteve Longerbeam struct v4l2_subdev *sd = ctrl_to_sd(ctrl); 278219a81c14SSteve Longerbeam struct ov5640_dev *sensor = to_ov5640_dev(sd); 278319a81c14SSteve Longerbeam int ret; 278419a81c14SSteve Longerbeam 278519a81c14SSteve Longerbeam /* v4l2_ctrl_lock() locks our own mutex */ 278619a81c14SSteve Longerbeam 278719a81c14SSteve Longerbeam /* 278819a81c14SSteve Longerbeam * If the device is not powered up by the host driver do 278919a81c14SSteve Longerbeam * not apply any controls to H/W at this time. Instead 279019a81c14SSteve Longerbeam * the controls will be restored right after power-up. 279119a81c14SSteve Longerbeam */ 279219a81c14SSteve Longerbeam if (sensor->power_count == 0) 279319a81c14SSteve Longerbeam return 0; 279419a81c14SSteve Longerbeam 279519a81c14SSteve Longerbeam switch (ctrl->id) { 279619a81c14SSteve Longerbeam case V4L2_CID_AUTOGAIN: 279719a81c14SSteve Longerbeam ret = ov5640_set_ctrl_gain(sensor, ctrl->val); 279819a81c14SSteve Longerbeam break; 279919a81c14SSteve Longerbeam case V4L2_CID_EXPOSURE_AUTO: 280019a81c14SSteve Longerbeam ret = ov5640_set_ctrl_exposure(sensor, ctrl->val); 280119a81c14SSteve Longerbeam break; 280219a81c14SSteve Longerbeam case V4L2_CID_AUTO_WHITE_BALANCE: 280319a81c14SSteve Longerbeam ret = ov5640_set_ctrl_white_balance(sensor, ctrl->val); 280419a81c14SSteve Longerbeam break; 280519a81c14SSteve Longerbeam case V4L2_CID_HUE: 280619a81c14SSteve Longerbeam ret = ov5640_set_ctrl_hue(sensor, ctrl->val); 280719a81c14SSteve Longerbeam break; 280819a81c14SSteve Longerbeam case V4L2_CID_CONTRAST: 280919a81c14SSteve Longerbeam ret = ov5640_set_ctrl_contrast(sensor, ctrl->val); 281019a81c14SSteve Longerbeam break; 281119a81c14SSteve Longerbeam case V4L2_CID_SATURATION: 281219a81c14SSteve Longerbeam ret = ov5640_set_ctrl_saturation(sensor, ctrl->val); 281319a81c14SSteve Longerbeam break; 281419a81c14SSteve Longerbeam case V4L2_CID_TEST_PATTERN: 281519a81c14SSteve Longerbeam ret = ov5640_set_ctrl_test_pattern(sensor, ctrl->val); 281619a81c14SSteve Longerbeam break; 28171068fecaSMylène Josserand case V4L2_CID_POWER_LINE_FREQUENCY: 28181068fecaSMylène Josserand ret = ov5640_set_ctrl_light_freq(sensor, ctrl->val); 28191068fecaSMylène Josserand break; 2820ce85705aSHugues Fruchet case V4L2_CID_HFLIP: 2821ce85705aSHugues Fruchet ret = ov5640_set_ctrl_hflip(sensor, ctrl->val); 2822ce85705aSHugues Fruchet break; 2823ce85705aSHugues Fruchet case V4L2_CID_VFLIP: 2824ce85705aSHugues Fruchet ret = ov5640_set_ctrl_vflip(sensor, ctrl->val); 2825ce85705aSHugues Fruchet break; 282619a81c14SSteve Longerbeam default: 282719a81c14SSteve Longerbeam ret = -EINVAL; 282819a81c14SSteve Longerbeam break; 282919a81c14SSteve Longerbeam } 283019a81c14SSteve Longerbeam 283119a81c14SSteve Longerbeam return ret; 283219a81c14SSteve Longerbeam } 283319a81c14SSteve Longerbeam 283419a81c14SSteve Longerbeam static const struct v4l2_ctrl_ops ov5640_ctrl_ops = { 283519a81c14SSteve Longerbeam .g_volatile_ctrl = ov5640_g_volatile_ctrl, 283619a81c14SSteve Longerbeam .s_ctrl = ov5640_s_ctrl, 283719a81c14SSteve Longerbeam }; 283819a81c14SSteve Longerbeam 283919a81c14SSteve Longerbeam static int ov5640_init_controls(struct ov5640_dev *sensor) 284019a81c14SSteve Longerbeam { 284122845bf2SJacopo Mondi const struct ov5640_mode_info *mode = sensor->current_mode; 284219a81c14SSteve Longerbeam const struct v4l2_ctrl_ops *ops = &ov5640_ctrl_ops; 284319a81c14SSteve Longerbeam struct ov5640_ctrls *ctrls = &sensor->ctrls; 284419a81c14SSteve Longerbeam struct v4l2_ctrl_handler *hdl = &ctrls->handler; 284519a81c14SSteve Longerbeam int ret; 284619a81c14SSteve Longerbeam 284719a81c14SSteve Longerbeam v4l2_ctrl_handler_init(hdl, 32); 284819a81c14SSteve Longerbeam 284919a81c14SSteve Longerbeam /* we can use our own mutex for the ctrl lock */ 285019a81c14SSteve Longerbeam hdl->lock = &sensor->lock; 285119a81c14SSteve Longerbeam 2852cc196e48SBenoit Parrot /* Clock related controls */ 2853cc196e48SBenoit Parrot ctrls->pixel_rate = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_PIXEL_RATE, 285422845bf2SJacopo Mondi ov5640_pixel_rates[OV5640_NUM_PIXEL_RATES - 1], 285522845bf2SJacopo Mondi ov5640_pixel_rates[0], 1, 285622845bf2SJacopo Mondi ov5640_pixel_rates[mode->pixel_rate]); 2857cc196e48SBenoit Parrot 285819a81c14SSteve Longerbeam /* Auto/manual white balance */ 285919a81c14SSteve Longerbeam ctrls->auto_wb = v4l2_ctrl_new_std(hdl, ops, 286019a81c14SSteve Longerbeam V4L2_CID_AUTO_WHITE_BALANCE, 286119a81c14SSteve Longerbeam 0, 1, 1, 1); 286219a81c14SSteve Longerbeam ctrls->blue_balance = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_BLUE_BALANCE, 286319a81c14SSteve Longerbeam 0, 4095, 1, 0); 286419a81c14SSteve Longerbeam ctrls->red_balance = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_RED_BALANCE, 286519a81c14SSteve Longerbeam 0, 4095, 1, 0); 286619a81c14SSteve Longerbeam /* Auto/manual exposure */ 286719a81c14SSteve Longerbeam ctrls->auto_exp = v4l2_ctrl_new_std_menu(hdl, ops, 286819a81c14SSteve Longerbeam V4L2_CID_EXPOSURE_AUTO, 286919a81c14SSteve Longerbeam V4L2_EXPOSURE_MANUAL, 0, 287019a81c14SSteve Longerbeam V4L2_EXPOSURE_AUTO); 287119a81c14SSteve Longerbeam ctrls->exposure = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_EXPOSURE, 287219a81c14SSteve Longerbeam 0, 65535, 1, 0); 287319a81c14SSteve Longerbeam /* Auto/manual gain */ 287419a81c14SSteve Longerbeam ctrls->auto_gain = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_AUTOGAIN, 287519a81c14SSteve Longerbeam 0, 1, 1, 1); 287619a81c14SSteve Longerbeam ctrls->gain = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_GAIN, 287719a81c14SSteve Longerbeam 0, 1023, 1, 0); 287819a81c14SSteve Longerbeam 287919a81c14SSteve Longerbeam ctrls->saturation = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_SATURATION, 288019a81c14SSteve Longerbeam 0, 255, 1, 64); 288119a81c14SSteve Longerbeam ctrls->hue = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_HUE, 288219a81c14SSteve Longerbeam 0, 359, 1, 0); 288319a81c14SSteve Longerbeam ctrls->contrast = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_CONTRAST, 288419a81c14SSteve Longerbeam 0, 255, 1, 0); 288519a81c14SSteve Longerbeam ctrls->test_pattern = 288619a81c14SSteve Longerbeam v4l2_ctrl_new_std_menu_items(hdl, ops, V4L2_CID_TEST_PATTERN, 288719a81c14SSteve Longerbeam ARRAY_SIZE(test_pattern_menu) - 1, 288819a81c14SSteve Longerbeam 0, 0, test_pattern_menu); 2889ce85705aSHugues Fruchet ctrls->hflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_HFLIP, 2890ce85705aSHugues Fruchet 0, 1, 1, 0); 2891ce85705aSHugues Fruchet ctrls->vflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_VFLIP, 2892ce85705aSHugues Fruchet 0, 1, 1, 0); 289319a81c14SSteve Longerbeam 28941068fecaSMylène Josserand ctrls->light_freq = 28951068fecaSMylène Josserand v4l2_ctrl_new_std_menu(hdl, ops, 28961068fecaSMylène Josserand V4L2_CID_POWER_LINE_FREQUENCY, 28971068fecaSMylène Josserand V4L2_CID_POWER_LINE_FREQUENCY_AUTO, 0, 28981068fecaSMylène Josserand V4L2_CID_POWER_LINE_FREQUENCY_50HZ); 28991068fecaSMylène Josserand 290019a81c14SSteve Longerbeam if (hdl->error) { 290119a81c14SSteve Longerbeam ret = hdl->error; 290219a81c14SSteve Longerbeam goto free_ctrls; 290319a81c14SSteve Longerbeam } 290419a81c14SSteve Longerbeam 2905cc196e48SBenoit Parrot ctrls->pixel_rate->flags |= V4L2_CTRL_FLAG_READ_ONLY; 290619a81c14SSteve Longerbeam ctrls->gain->flags |= V4L2_CTRL_FLAG_VOLATILE; 290719a81c14SSteve Longerbeam ctrls->exposure->flags |= V4L2_CTRL_FLAG_VOLATILE; 290819a81c14SSteve Longerbeam 290919a81c14SSteve Longerbeam v4l2_ctrl_auto_cluster(3, &ctrls->auto_wb, 0, false); 291019a81c14SSteve Longerbeam v4l2_ctrl_auto_cluster(2, &ctrls->auto_gain, 0, true); 291119a81c14SSteve Longerbeam v4l2_ctrl_auto_cluster(2, &ctrls->auto_exp, 1, true); 291219a81c14SSteve Longerbeam 291319a81c14SSteve Longerbeam sensor->sd.ctrl_handler = hdl; 291419a81c14SSteve Longerbeam return 0; 291519a81c14SSteve Longerbeam 291619a81c14SSteve Longerbeam free_ctrls: 291719a81c14SSteve Longerbeam v4l2_ctrl_handler_free(hdl); 291819a81c14SSteve Longerbeam return ret; 291919a81c14SSteve Longerbeam } 292019a81c14SSteve Longerbeam 292119a81c14SSteve Longerbeam static int ov5640_enum_frame_size(struct v4l2_subdev *sd, 29220d346d2aSTomi Valkeinen struct v4l2_subdev_state *sd_state, 292319a81c14SSteve Longerbeam struct v4l2_subdev_frame_size_enum *fse) 292419a81c14SSteve Longerbeam { 292519a81c14SSteve Longerbeam if (fse->pad != 0) 292619a81c14SSteve Longerbeam return -EINVAL; 292719a81c14SSteve Longerbeam if (fse->index >= OV5640_NUM_MODES) 292819a81c14SSteve Longerbeam return -EINVAL; 292919a81c14SSteve Longerbeam 293041d8d7f5SHugues Fruchet fse->min_width = 2931086c25f8SMaxime Ripard ov5640_mode_data[fse->index].hact; 293241d8d7f5SHugues Fruchet fse->max_width = fse->min_width; 293341d8d7f5SHugues Fruchet fse->min_height = 2934086c25f8SMaxime Ripard ov5640_mode_data[fse->index].vact; 293541d8d7f5SHugues Fruchet fse->max_height = fse->min_height; 293619a81c14SSteve Longerbeam 293719a81c14SSteve Longerbeam return 0; 293819a81c14SSteve Longerbeam } 293919a81c14SSteve Longerbeam 294019a81c14SSteve Longerbeam static int ov5640_enum_frame_interval( 294119a81c14SSteve Longerbeam struct v4l2_subdev *sd, 29420d346d2aSTomi Valkeinen struct v4l2_subdev_state *sd_state, 294319a81c14SSteve Longerbeam struct v4l2_subdev_frame_interval_enum *fie) 294419a81c14SSteve Longerbeam { 294519a81c14SSteve Longerbeam struct ov5640_dev *sensor = to_ov5640_dev(sd); 294619a81c14SSteve Longerbeam struct v4l2_fract tpf; 294719a81c14SSteve Longerbeam int ret; 294819a81c14SSteve Longerbeam 294919a81c14SSteve Longerbeam if (fie->pad != 0) 295019a81c14SSteve Longerbeam return -EINVAL; 295119a81c14SSteve Longerbeam if (fie->index >= OV5640_NUM_FRAMERATES) 295219a81c14SSteve Longerbeam return -EINVAL; 295319a81c14SSteve Longerbeam 295419a81c14SSteve Longerbeam tpf.numerator = 1; 295519a81c14SSteve Longerbeam tpf.denominator = ov5640_framerates[fie->index]; 295619a81c14SSteve Longerbeam 295719a81c14SSteve Longerbeam ret = ov5640_try_frame_interval(sensor, &tpf, 295819a81c14SSteve Longerbeam fie->width, fie->height); 295919a81c14SSteve Longerbeam if (ret < 0) 296019a81c14SSteve Longerbeam return -EINVAL; 296119a81c14SSteve Longerbeam 296219a81c14SSteve Longerbeam fie->interval = tpf; 296319a81c14SSteve Longerbeam return 0; 296419a81c14SSteve Longerbeam } 296519a81c14SSteve Longerbeam 296619a81c14SSteve Longerbeam static int ov5640_g_frame_interval(struct v4l2_subdev *sd, 296719a81c14SSteve Longerbeam struct v4l2_subdev_frame_interval *fi) 296819a81c14SSteve Longerbeam { 296919a81c14SSteve Longerbeam struct ov5640_dev *sensor = to_ov5640_dev(sd); 297019a81c14SSteve Longerbeam 297119a81c14SSteve Longerbeam mutex_lock(&sensor->lock); 297219a81c14SSteve Longerbeam fi->interval = sensor->frame_interval; 297319a81c14SSteve Longerbeam mutex_unlock(&sensor->lock); 297419a81c14SSteve Longerbeam 297519a81c14SSteve Longerbeam return 0; 297619a81c14SSteve Longerbeam } 297719a81c14SSteve Longerbeam 297819a81c14SSteve Longerbeam static int ov5640_s_frame_interval(struct v4l2_subdev *sd, 297919a81c14SSteve Longerbeam struct v4l2_subdev_frame_interval *fi) 298019a81c14SSteve Longerbeam { 298119a81c14SSteve Longerbeam struct ov5640_dev *sensor = to_ov5640_dev(sd); 298219a81c14SSteve Longerbeam const struct ov5640_mode_info *mode; 298319a81c14SSteve Longerbeam int frame_rate, ret = 0; 298419a81c14SSteve Longerbeam 298519a81c14SSteve Longerbeam if (fi->pad != 0) 298619a81c14SSteve Longerbeam return -EINVAL; 298719a81c14SSteve Longerbeam 298819a81c14SSteve Longerbeam mutex_lock(&sensor->lock); 298919a81c14SSteve Longerbeam 299019a81c14SSteve Longerbeam if (sensor->streaming) { 299119a81c14SSteve Longerbeam ret = -EBUSY; 299219a81c14SSteve Longerbeam goto out; 299319a81c14SSteve Longerbeam } 299419a81c14SSteve Longerbeam 299519a81c14SSteve Longerbeam mode = sensor->current_mode; 299619a81c14SSteve Longerbeam 299719a81c14SSteve Longerbeam frame_rate = ov5640_try_frame_interval(sensor, &fi->interval, 2998dba13a0bSMaxime Ripard mode->hact, mode->vact); 2999e823fb16SMaxime Ripard if (frame_rate < 0) { 3000e823fb16SMaxime Ripard /* Always return a valid frame interval value */ 3001e823fb16SMaxime Ripard fi->interval = sensor->frame_interval; 3002e823fb16SMaxime Ripard goto out; 3003e823fb16SMaxime Ripard } 300419a81c14SSteve Longerbeam 30053c4a7372SHugues Fruchet mode = ov5640_find_mode(sensor, frame_rate, mode->hact, 3006dba13a0bSMaxime Ripard mode->vact, true); 30073c4a7372SHugues Fruchet if (!mode) { 30083c4a7372SHugues Fruchet ret = -EINVAL; 30093c4a7372SHugues Fruchet goto out; 30103c4a7372SHugues Fruchet } 30113c4a7372SHugues Fruchet 30120929983eSHugues Fruchet if (mode != sensor->current_mode || 30130929983eSHugues Fruchet frame_rate != sensor->current_fr) { 30140929983eSHugues Fruchet sensor->current_fr = frame_rate; 30150929983eSHugues Fruchet sensor->frame_interval = fi->interval; 30163c4a7372SHugues Fruchet sensor->current_mode = mode; 301719a81c14SSteve Longerbeam sensor->pending_mode_change = true; 3018cc196e48SBenoit Parrot 3019cc196e48SBenoit Parrot __v4l2_ctrl_s_ctrl_int64(sensor->ctrls.pixel_rate, 3020cc196e48SBenoit Parrot ov5640_calc_pixel_rate(sensor)); 30216949d864SHugues Fruchet } 302219a81c14SSteve Longerbeam out: 302319a81c14SSteve Longerbeam mutex_unlock(&sensor->lock); 302419a81c14SSteve Longerbeam return ret; 302519a81c14SSteve Longerbeam } 302619a81c14SSteve Longerbeam 302719a81c14SSteve Longerbeam static int ov5640_enum_mbus_code(struct v4l2_subdev *sd, 30280d346d2aSTomi Valkeinen struct v4l2_subdev_state *sd_state, 302919a81c14SSteve Longerbeam struct v4l2_subdev_mbus_code_enum *code) 303019a81c14SSteve Longerbeam { 303119a81c14SSteve Longerbeam if (code->pad != 0) 303219a81c14SSteve Longerbeam return -EINVAL; 3033e3ee691dSHugues Fruchet if (code->index >= ARRAY_SIZE(ov5640_formats)) 303419a81c14SSteve Longerbeam return -EINVAL; 303519a81c14SSteve Longerbeam 3036e3ee691dSHugues Fruchet code->code = ov5640_formats[code->index].code; 303719a81c14SSteve Longerbeam return 0; 303819a81c14SSteve Longerbeam } 303919a81c14SSteve Longerbeam 304019a81c14SSteve Longerbeam static int ov5640_s_stream(struct v4l2_subdev *sd, int enable) 304119a81c14SSteve Longerbeam { 304219a81c14SSteve Longerbeam struct ov5640_dev *sensor = to_ov5640_dev(sd); 304319a81c14SSteve Longerbeam int ret = 0; 304419a81c14SSteve Longerbeam 304519a81c14SSteve Longerbeam mutex_lock(&sensor->lock); 304619a81c14SSteve Longerbeam 304719a81c14SSteve Longerbeam if (sensor->streaming == !enable) { 304819a81c14SSteve Longerbeam if (enable && sensor->pending_mode_change) { 3049985cdcb0SHugues Fruchet ret = ov5640_set_mode(sensor); 305019a81c14SSteve Longerbeam if (ret) 305119a81c14SSteve Longerbeam goto out; 3052fb98e29fSHugues Fruchet } 3053e3ee691dSHugues Fruchet 3054fb98e29fSHugues Fruchet if (enable && sensor->pending_fmt_change) { 3055e3ee691dSHugues Fruchet ret = ov5640_set_framefmt(sensor, &sensor->fmt); 3056e3ee691dSHugues Fruchet if (ret) 3057e3ee691dSHugues Fruchet goto out; 3058fb98e29fSHugues Fruchet sensor->pending_fmt_change = false; 305919a81c14SSteve Longerbeam } 306019a81c14SSteve Longerbeam 30618e823f5cSJacopo Mondi if (ov5640_is_csi2(sensor)) 3062f22996dbSHugues Fruchet ret = ov5640_set_stream_mipi(sensor, enable); 3063f22996dbSHugues Fruchet else 3064f22996dbSHugues Fruchet ret = ov5640_set_stream_dvp(sensor, enable); 3065f22996dbSHugues Fruchet 306619a81c14SSteve Longerbeam if (!ret) 306719a81c14SSteve Longerbeam sensor->streaming = enable; 306819a81c14SSteve Longerbeam } 306919a81c14SSteve Longerbeam out: 307019a81c14SSteve Longerbeam mutex_unlock(&sensor->lock); 307119a81c14SSteve Longerbeam return ret; 307219a81c14SSteve Longerbeam } 307319a81c14SSteve Longerbeam 307419a81c14SSteve Longerbeam static const struct v4l2_subdev_core_ops ov5640_core_ops = { 307519a81c14SSteve Longerbeam .s_power = ov5640_s_power, 30762d18fbc5SAkinobu Mita .log_status = v4l2_ctrl_subdev_log_status, 30772d18fbc5SAkinobu Mita .subscribe_event = v4l2_ctrl_subdev_subscribe_event, 30782d18fbc5SAkinobu Mita .unsubscribe_event = v4l2_event_subdev_unsubscribe, 307919a81c14SSteve Longerbeam }; 308019a81c14SSteve Longerbeam 308119a81c14SSteve Longerbeam static const struct v4l2_subdev_video_ops ov5640_video_ops = { 308219a81c14SSteve Longerbeam .g_frame_interval = ov5640_g_frame_interval, 308319a81c14SSteve Longerbeam .s_frame_interval = ov5640_s_frame_interval, 308419a81c14SSteve Longerbeam .s_stream = ov5640_s_stream, 308519a81c14SSteve Longerbeam }; 308619a81c14SSteve Longerbeam 308719a81c14SSteve Longerbeam static const struct v4l2_subdev_pad_ops ov5640_pad_ops = { 308819a81c14SSteve Longerbeam .enum_mbus_code = ov5640_enum_mbus_code, 308919a81c14SSteve Longerbeam .get_fmt = ov5640_get_fmt, 309019a81c14SSteve Longerbeam .set_fmt = ov5640_set_fmt, 309119a81c14SSteve Longerbeam .enum_frame_size = ov5640_enum_frame_size, 309219a81c14SSteve Longerbeam .enum_frame_interval = ov5640_enum_frame_interval, 309319a81c14SSteve Longerbeam }; 309419a81c14SSteve Longerbeam 309519a81c14SSteve Longerbeam static const struct v4l2_subdev_ops ov5640_subdev_ops = { 309619a81c14SSteve Longerbeam .core = &ov5640_core_ops, 309719a81c14SSteve Longerbeam .video = &ov5640_video_ops, 309819a81c14SSteve Longerbeam .pad = &ov5640_pad_ops, 309919a81c14SSteve Longerbeam }; 310019a81c14SSteve Longerbeam 310119a81c14SSteve Longerbeam static int ov5640_get_regulators(struct ov5640_dev *sensor) 310219a81c14SSteve Longerbeam { 310319a81c14SSteve Longerbeam int i; 310419a81c14SSteve Longerbeam 310519a81c14SSteve Longerbeam for (i = 0; i < OV5640_NUM_SUPPLIES; i++) 310619a81c14SSteve Longerbeam sensor->supplies[i].supply = ov5640_supply_name[i]; 310719a81c14SSteve Longerbeam 310819a81c14SSteve Longerbeam return devm_regulator_bulk_get(&sensor->i2c_client->dev, 310919a81c14SSteve Longerbeam OV5640_NUM_SUPPLIES, 311019a81c14SSteve Longerbeam sensor->supplies); 311119a81c14SSteve Longerbeam } 311219a81c14SSteve Longerbeam 31130f7acb52SHugues Fruchet static int ov5640_check_chip_id(struct ov5640_dev *sensor) 31140f7acb52SHugues Fruchet { 31150f7acb52SHugues Fruchet struct i2c_client *client = sensor->i2c_client; 31160f7acb52SHugues Fruchet int ret = 0; 31170f7acb52SHugues Fruchet u16 chip_id; 31180f7acb52SHugues Fruchet 31190f7acb52SHugues Fruchet ret = ov5640_set_power_on(sensor); 31200f7acb52SHugues Fruchet if (ret) 31210f7acb52SHugues Fruchet return ret; 31220f7acb52SHugues Fruchet 31230f7acb52SHugues Fruchet ret = ov5640_read_reg16(sensor, OV5640_REG_CHIP_ID, &chip_id); 31240f7acb52SHugues Fruchet if (ret) { 31250f7acb52SHugues Fruchet dev_err(&client->dev, "%s: failed to read chip identifier\n", 31260f7acb52SHugues Fruchet __func__); 31270f7acb52SHugues Fruchet goto power_off; 31280f7acb52SHugues Fruchet } 31290f7acb52SHugues Fruchet 31300f7acb52SHugues Fruchet if (chip_id != 0x5640) { 31310f7acb52SHugues Fruchet dev_err(&client->dev, "%s: wrong chip identifier, expected 0x5640, got 0x%x\n", 31320f7acb52SHugues Fruchet __func__, chip_id); 31330f7acb52SHugues Fruchet ret = -ENXIO; 31340f7acb52SHugues Fruchet } 31350f7acb52SHugues Fruchet 31360f7acb52SHugues Fruchet power_off: 31370f7acb52SHugues Fruchet ov5640_set_power_off(sensor); 31380f7acb52SHugues Fruchet return ret; 31390f7acb52SHugues Fruchet } 31400f7acb52SHugues Fruchet 3141e6714993SKieran Bingham static int ov5640_probe(struct i2c_client *client) 314219a81c14SSteve Longerbeam { 314319a81c14SSteve Longerbeam struct device *dev = &client->dev; 314419a81c14SSteve Longerbeam struct fwnode_handle *endpoint; 314519a81c14SSteve Longerbeam struct ov5640_dev *sensor; 3146e6441fdeSHugues Fruchet struct v4l2_mbus_framefmt *fmt; 3147c3f3ba3eSHugues Fruchet u32 rotation; 314819a81c14SSteve Longerbeam int ret; 314919a81c14SSteve Longerbeam 315019a81c14SSteve Longerbeam sensor = devm_kzalloc(dev, sizeof(*sensor), GFP_KERNEL); 315119a81c14SSteve Longerbeam if (!sensor) 315219a81c14SSteve Longerbeam return -ENOMEM; 315319a81c14SSteve Longerbeam 315419a81c14SSteve Longerbeam sensor->i2c_client = client; 3155fb98e29fSHugues Fruchet 3156fb98e29fSHugues Fruchet /* 3157fb98e29fSHugues Fruchet * default init sequence initialize sensor to 3158fb98e29fSHugues Fruchet * YUV422 UYVY VGA@30fps 3159fb98e29fSHugues Fruchet */ 3160e6441fdeSHugues Fruchet fmt = &sensor->fmt; 3161fb98e29fSHugues Fruchet fmt->code = MEDIA_BUS_FMT_UYVY8_2X8; 3162fb98e29fSHugues Fruchet fmt->colorspace = V4L2_COLORSPACE_SRGB; 3163e6441fdeSHugues Fruchet fmt->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(fmt->colorspace); 3164e6441fdeSHugues Fruchet fmt->quantization = V4L2_QUANTIZATION_FULL_RANGE; 3165e6441fdeSHugues Fruchet fmt->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(fmt->colorspace); 3166e6441fdeSHugues Fruchet fmt->width = 640; 3167e6441fdeSHugues Fruchet fmt->height = 480; 3168e6441fdeSHugues Fruchet fmt->field = V4L2_FIELD_NONE; 316919a81c14SSteve Longerbeam sensor->frame_interval.numerator = 1; 317019a81c14SSteve Longerbeam sensor->frame_interval.denominator = ov5640_framerates[OV5640_30_FPS]; 317119a81c14SSteve Longerbeam sensor->current_fr = OV5640_30_FPS; 317219a81c14SSteve Longerbeam sensor->current_mode = 3173086c25f8SMaxime Ripard &ov5640_mode_data[OV5640_MODE_VGA_640_480]; 3174985cdcb0SHugues Fruchet sensor->last_mode = sensor->current_mode; 317519a81c14SSteve Longerbeam 317619a81c14SSteve Longerbeam sensor->ae_target = 52; 317719a81c14SSteve Longerbeam 3178c3f3ba3eSHugues Fruchet /* optional indication of physical rotation of sensor */ 3179c3f3ba3eSHugues Fruchet ret = fwnode_property_read_u32(dev_fwnode(&client->dev), "rotation", 3180c3f3ba3eSHugues Fruchet &rotation); 3181c3f3ba3eSHugues Fruchet if (!ret) { 3182c3f3ba3eSHugues Fruchet switch (rotation) { 3183c3f3ba3eSHugues Fruchet case 180: 3184c3f3ba3eSHugues Fruchet sensor->upside_down = true; 31851771e9fbSGustavo A. R. Silva fallthrough; 3186c3f3ba3eSHugues Fruchet case 0: 3187c3f3ba3eSHugues Fruchet break; 3188c3f3ba3eSHugues Fruchet default: 3189c3f3ba3eSHugues Fruchet dev_warn(dev, "%u degrees rotation is not supported, ignoring...\n", 3190c3f3ba3eSHugues Fruchet rotation); 3191c3f3ba3eSHugues Fruchet } 3192c3f3ba3eSHugues Fruchet } 3193c3f3ba3eSHugues Fruchet 3194ce96bcf5SSakari Ailus endpoint = fwnode_graph_get_next_endpoint(dev_fwnode(&client->dev), 3195ce96bcf5SSakari Ailus NULL); 319619a81c14SSteve Longerbeam if (!endpoint) { 319719a81c14SSteve Longerbeam dev_err(dev, "endpoint node not found\n"); 319819a81c14SSteve Longerbeam return -EINVAL; 319919a81c14SSteve Longerbeam } 320019a81c14SSteve Longerbeam 320119a81c14SSteve Longerbeam ret = v4l2_fwnode_endpoint_parse(endpoint, &sensor->ep); 320219a81c14SSteve Longerbeam fwnode_handle_put(endpoint); 320319a81c14SSteve Longerbeam if (ret) { 320419a81c14SSteve Longerbeam dev_err(dev, "Could not parse endpoint\n"); 320519a81c14SSteve Longerbeam return ret; 320619a81c14SSteve Longerbeam } 320719a81c14SSteve Longerbeam 32082c61e48dSLad Prabhakar if (sensor->ep.bus_type != V4L2_MBUS_PARALLEL && 32092c61e48dSLad Prabhakar sensor->ep.bus_type != V4L2_MBUS_CSI2_DPHY && 32102c61e48dSLad Prabhakar sensor->ep.bus_type != V4L2_MBUS_BT656) { 32112c61e48dSLad Prabhakar dev_err(dev, "Unsupported bus type %d\n", sensor->ep.bus_type); 32122c61e48dSLad Prabhakar return -EINVAL; 32132c61e48dSLad Prabhakar } 32142c61e48dSLad Prabhakar 321519a81c14SSteve Longerbeam /* get system clock (xclk) */ 321619a81c14SSteve Longerbeam sensor->xclk = devm_clk_get(dev, "xclk"); 321719a81c14SSteve Longerbeam if (IS_ERR(sensor->xclk)) { 321819a81c14SSteve Longerbeam dev_err(dev, "failed to get xclk\n"); 321919a81c14SSteve Longerbeam return PTR_ERR(sensor->xclk); 322019a81c14SSteve Longerbeam } 322119a81c14SSteve Longerbeam 322219a81c14SSteve Longerbeam sensor->xclk_freq = clk_get_rate(sensor->xclk); 322319a81c14SSteve Longerbeam if (sensor->xclk_freq < OV5640_XCLK_MIN || 322419a81c14SSteve Longerbeam sensor->xclk_freq > OV5640_XCLK_MAX) { 322519a81c14SSteve Longerbeam dev_err(dev, "xclk frequency out of range: %d Hz\n", 322619a81c14SSteve Longerbeam sensor->xclk_freq); 322719a81c14SSteve Longerbeam return -EINVAL; 322819a81c14SSteve Longerbeam } 322919a81c14SSteve Longerbeam 323019a81c14SSteve Longerbeam /* request optional power down pin */ 323119a81c14SSteve Longerbeam sensor->pwdn_gpio = devm_gpiod_get_optional(dev, "powerdown", 323219a81c14SSteve Longerbeam GPIOD_OUT_HIGH); 32338791a102SFabio Estevam if (IS_ERR(sensor->pwdn_gpio)) 32348791a102SFabio Estevam return PTR_ERR(sensor->pwdn_gpio); 32358791a102SFabio Estevam 323619a81c14SSteve Longerbeam /* request optional reset pin */ 323719a81c14SSteve Longerbeam sensor->reset_gpio = devm_gpiod_get_optional(dev, "reset", 323819a81c14SSteve Longerbeam GPIOD_OUT_HIGH); 32398791a102SFabio Estevam if (IS_ERR(sensor->reset_gpio)) 32408791a102SFabio Estevam return PTR_ERR(sensor->reset_gpio); 324119a81c14SSteve Longerbeam 324219a81c14SSteve Longerbeam v4l2_i2c_subdev_init(&sensor->sd, client, &ov5640_subdev_ops); 324319a81c14SSteve Longerbeam 32442d18fbc5SAkinobu Mita sensor->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE | 32452d18fbc5SAkinobu Mita V4L2_SUBDEV_FL_HAS_EVENTS; 324619a81c14SSteve Longerbeam sensor->pad.flags = MEDIA_PAD_FL_SOURCE; 324719a81c14SSteve Longerbeam sensor->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR; 324819a81c14SSteve Longerbeam ret = media_entity_pads_init(&sensor->sd.entity, 1, &sensor->pad); 324919a81c14SSteve Longerbeam if (ret) 325019a81c14SSteve Longerbeam return ret; 325119a81c14SSteve Longerbeam 325219a81c14SSteve Longerbeam ret = ov5640_get_regulators(sensor); 325319a81c14SSteve Longerbeam if (ret) 325419a81c14SSteve Longerbeam return ret; 325519a81c14SSteve Longerbeam 325619a81c14SSteve Longerbeam mutex_init(&sensor->lock); 325719a81c14SSteve Longerbeam 32580f7acb52SHugues Fruchet ret = ov5640_check_chip_id(sensor); 32590f7acb52SHugues Fruchet if (ret) 32600f7acb52SHugues Fruchet goto entity_cleanup; 32610f7acb52SHugues Fruchet 326219a81c14SSteve Longerbeam ret = ov5640_init_controls(sensor); 326319a81c14SSteve Longerbeam if (ret) 326419a81c14SSteve Longerbeam goto entity_cleanup; 326519a81c14SSteve Longerbeam 326615786f7bSSakari Ailus ret = v4l2_async_register_subdev_sensor(&sensor->sd); 326719a81c14SSteve Longerbeam if (ret) 326819a81c14SSteve Longerbeam goto free_ctrls; 326919a81c14SSteve Longerbeam 327019a81c14SSteve Longerbeam return 0; 327119a81c14SSteve Longerbeam 327219a81c14SSteve Longerbeam free_ctrls: 327319a81c14SSteve Longerbeam v4l2_ctrl_handler_free(&sensor->ctrls.handler); 327419a81c14SSteve Longerbeam entity_cleanup: 327519a81c14SSteve Longerbeam media_entity_cleanup(&sensor->sd.entity); 3276bfcba38dSTomi Valkeinen mutex_destroy(&sensor->lock); 327719a81c14SSteve Longerbeam return ret; 327819a81c14SSteve Longerbeam } 327919a81c14SSteve Longerbeam 328019a81c14SSteve Longerbeam static int ov5640_remove(struct i2c_client *client) 328119a81c14SSteve Longerbeam { 328219a81c14SSteve Longerbeam struct v4l2_subdev *sd = i2c_get_clientdata(client); 328319a81c14SSteve Longerbeam struct ov5640_dev *sensor = to_ov5640_dev(sd); 328419a81c14SSteve Longerbeam 328519a81c14SSteve Longerbeam v4l2_async_unregister_subdev(&sensor->sd); 328619a81c14SSteve Longerbeam media_entity_cleanup(&sensor->sd.entity); 328719a81c14SSteve Longerbeam v4l2_ctrl_handler_free(&sensor->ctrls.handler); 3288bfcba38dSTomi Valkeinen mutex_destroy(&sensor->lock); 328919a81c14SSteve Longerbeam 329019a81c14SSteve Longerbeam return 0; 329119a81c14SSteve Longerbeam } 329219a81c14SSteve Longerbeam 329319a81c14SSteve Longerbeam static const struct i2c_device_id ov5640_id[] = { 329419a81c14SSteve Longerbeam {"ov5640", 0}, 329519a81c14SSteve Longerbeam {}, 329619a81c14SSteve Longerbeam }; 329719a81c14SSteve Longerbeam MODULE_DEVICE_TABLE(i2c, ov5640_id); 329819a81c14SSteve Longerbeam 329919a81c14SSteve Longerbeam static const struct of_device_id ov5640_dt_ids[] = { 330019a81c14SSteve Longerbeam { .compatible = "ovti,ov5640" }, 330119a81c14SSteve Longerbeam { /* sentinel */ } 330219a81c14SSteve Longerbeam }; 330319a81c14SSteve Longerbeam MODULE_DEVICE_TABLE(of, ov5640_dt_ids); 330419a81c14SSteve Longerbeam 330519a81c14SSteve Longerbeam static struct i2c_driver ov5640_i2c_driver = { 330619a81c14SSteve Longerbeam .driver = { 330719a81c14SSteve Longerbeam .name = "ov5640", 330819a81c14SSteve Longerbeam .of_match_table = ov5640_dt_ids, 330919a81c14SSteve Longerbeam }, 331019a81c14SSteve Longerbeam .id_table = ov5640_id, 3311e6714993SKieran Bingham .probe_new = ov5640_probe, 331219a81c14SSteve Longerbeam .remove = ov5640_remove, 331319a81c14SSteve Longerbeam }; 331419a81c14SSteve Longerbeam 331519a81c14SSteve Longerbeam module_i2c_driver(ov5640_i2c_driver); 331619a81c14SSteve Longerbeam 331719a81c14SSteve Longerbeam MODULE_DESCRIPTION("OV5640 MIPI Camera Subdev Driver"); 331819a81c14SSteve Longerbeam MODULE_LICENSE("GPL"); 3319