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 144*7a3b8d4bSJacopo Mondi /* 145*7a3b8d4bSJacopo Mondi * MIPI CSI-2 link frequencies. 146*7a3b8d4bSJacopo Mondi * 147*7a3b8d4bSJacopo Mondi * Derived from the above defined pixel rate for bpp = (8, 16, 24) and 148*7a3b8d4bSJacopo Mondi * data_lanes = (1, 2) 149*7a3b8d4bSJacopo Mondi * 150*7a3b8d4bSJacopo Mondi * link_freq = (pixel_rate * bpp) / (2 * data_lanes) 151*7a3b8d4bSJacopo Mondi */ 152*7a3b8d4bSJacopo Mondi static const s64 ov5640_csi2_link_freqs[] = { 153*7a3b8d4bSJacopo Mondi 992000000, 888000000, 768000000, 744000000, 672000000, 672000000, 154*7a3b8d4bSJacopo Mondi 592000000, 592000000, 576000000, 576000000, 496000000, 496000000, 155*7a3b8d4bSJacopo Mondi 384000000, 384000000, 384000000, 336000000, 296000000, 288000000, 156*7a3b8d4bSJacopo Mondi 248000000, 192000000, 192000000, 192000000, 96000000, 157*7a3b8d4bSJacopo Mondi }; 158*7a3b8d4bSJacopo Mondi 159*7a3b8d4bSJacopo Mondi /* Link freq for default mode: UYVY 16 bpp, 2 data lanes. */ 160*7a3b8d4bSJacopo Mondi #define OV5640_DEFAULT_LINK_FREQ 13 161*7a3b8d4bSJacopo Mondi 162b7ed3abdSLoic Poulain enum ov5640_format_mux { 163b7ed3abdSLoic Poulain OV5640_FMT_MUX_YUV422 = 0, 164b7ed3abdSLoic Poulain OV5640_FMT_MUX_RGB, 165b7ed3abdSLoic Poulain OV5640_FMT_MUX_DITHER, 166b7ed3abdSLoic Poulain OV5640_FMT_MUX_RAW_DPC, 167b7ed3abdSLoic Poulain OV5640_FMT_MUX_SNR_RAW, 168b7ed3abdSLoic Poulain OV5640_FMT_MUX_RAW_CIP, 169b7ed3abdSLoic Poulain }; 170b7ed3abdSLoic Poulain 1712d7671f6SJacopo Mondi static const struct ov5640_pixfmt { 172e3ee691dSHugues Fruchet u32 code; 173e3ee691dSHugues Fruchet u32 colorspace; 1742d7671f6SJacopo Mondi u8 bpp; 1752d7671f6SJacopo Mondi } ov5640_formats[] = { 1762d7671f6SJacopo Mondi { 1772d7671f6SJacopo Mondi .code = MEDIA_BUS_FMT_JPEG_1X8, 1782d7671f6SJacopo Mondi .colorspace = V4L2_COLORSPACE_JPEG, 1792d7671f6SJacopo Mondi .bpp = 16, 1802d7671f6SJacopo Mondi }, { 1812d7671f6SJacopo Mondi .code = MEDIA_BUS_FMT_UYVY8_2X8, 1822d7671f6SJacopo Mondi .colorspace = V4L2_COLORSPACE_SRGB, 1832d7671f6SJacopo Mondi .bpp = 16, 1842d7671f6SJacopo Mondi }, { 1852d7671f6SJacopo Mondi .code = MEDIA_BUS_FMT_UYVY8_1X16, 1862d7671f6SJacopo Mondi .colorspace = V4L2_COLORSPACE_SRGB, 1872d7671f6SJacopo Mondi .bpp = 16, 1882d7671f6SJacopo Mondi }, { 1892d7671f6SJacopo Mondi .code = MEDIA_BUS_FMT_YUYV8_2X8, 1902d7671f6SJacopo Mondi .colorspace = V4L2_COLORSPACE_SRGB, 1912d7671f6SJacopo Mondi .bpp = 16, 1922d7671f6SJacopo Mondi }, { 1932d7671f6SJacopo Mondi .code = MEDIA_BUS_FMT_YUYV8_1X16, 1942d7671f6SJacopo Mondi .colorspace = V4L2_COLORSPACE_SRGB, 1952d7671f6SJacopo Mondi .bpp = 16, 1962d7671f6SJacopo Mondi }, { 1972d7671f6SJacopo Mondi .code = MEDIA_BUS_FMT_RGB565_2X8_LE, 1982d7671f6SJacopo Mondi .colorspace = V4L2_COLORSPACE_SRGB, 1992d7671f6SJacopo Mondi .bpp = 16, 2002d7671f6SJacopo Mondi }, { 2012d7671f6SJacopo Mondi .code = MEDIA_BUS_FMT_RGB565_2X8_BE, 2022d7671f6SJacopo Mondi .colorspace = V4L2_COLORSPACE_SRGB, 2032d7671f6SJacopo Mondi .bpp = 16, 2042d7671f6SJacopo Mondi }, { 2052d7671f6SJacopo Mondi .code = MEDIA_BUS_FMT_SBGGR8_1X8, 2062d7671f6SJacopo Mondi .colorspace = V4L2_COLORSPACE_SRGB, 2072d7671f6SJacopo Mondi .bpp = 8, 2082d7671f6SJacopo Mondi }, { 2092d7671f6SJacopo Mondi .code = MEDIA_BUS_FMT_SGBRG8_1X8, 2102d7671f6SJacopo Mondi .colorspace = V4L2_COLORSPACE_SRGB, 2112d7671f6SJacopo Mondi .bpp = 8 2122d7671f6SJacopo Mondi }, { 2132d7671f6SJacopo Mondi .code = MEDIA_BUS_FMT_SGRBG8_1X8, 2142d7671f6SJacopo Mondi .colorspace = V4L2_COLORSPACE_SRGB, 2152d7671f6SJacopo Mondi .bpp = 8, 2162d7671f6SJacopo Mondi }, { 2172d7671f6SJacopo Mondi .code = MEDIA_BUS_FMT_SRGGB8_1X8, 2182d7671f6SJacopo Mondi .colorspace = V4L2_COLORSPACE_SRGB, 2192d7671f6SJacopo Mondi .bpp = 8, 2202d7671f6SJacopo Mondi }, 221e3ee691dSHugues Fruchet }; 222e3ee691dSHugues Fruchet 22319a81c14SSteve Longerbeam /* 22419a81c14SSteve Longerbeam * FIXME: remove this when a subdev API becomes available 22519a81c14SSteve Longerbeam * to set the MIPI CSI-2 virtual channel. 22619a81c14SSteve Longerbeam */ 22719a81c14SSteve Longerbeam static unsigned int virtual_channel; 2288670d70aSHugues Fruchet module_param(virtual_channel, uint, 0444); 22919a81c14SSteve Longerbeam MODULE_PARM_DESC(virtual_channel, 23019a81c14SSteve Longerbeam "MIPI CSI-2 virtual channel (0..3), default 0"); 23119a81c14SSteve Longerbeam 23219a81c14SSteve Longerbeam static const int ov5640_framerates[] = { 23319a81c14SSteve Longerbeam [OV5640_15_FPS] = 15, 23419a81c14SSteve Longerbeam [OV5640_30_FPS] = 30, 235e823fb16SMaxime Ripard [OV5640_60_FPS] = 60, 23619a81c14SSteve Longerbeam }; 23719a81c14SSteve Longerbeam 23819a81c14SSteve Longerbeam /* regulator supplies */ 23919a81c14SSteve Longerbeam static const char * const ov5640_supply_name[] = { 24041d8d7f5SHugues Fruchet "DOVDD", /* Digital I/O (1.8V) supply */ 24119a81c14SSteve Longerbeam "AVDD", /* Analog (2.8V) supply */ 24224c8ac89SFabio Estevam "DVDD", /* Digital Core (1.5V) supply */ 24319a81c14SSteve Longerbeam }; 24419a81c14SSteve Longerbeam 24519a81c14SSteve Longerbeam #define OV5640_NUM_SUPPLIES ARRAY_SIZE(ov5640_supply_name) 24619a81c14SSteve Longerbeam 24719a81c14SSteve Longerbeam /* 24819a81c14SSteve Longerbeam * Image size under 1280 * 960 are SUBSAMPLING 24919a81c14SSteve Longerbeam * Image size upper 1280 * 960 are SCALING 25019a81c14SSteve Longerbeam */ 25119a81c14SSteve Longerbeam enum ov5640_downsize_mode { 25219a81c14SSteve Longerbeam SUBSAMPLING, 25319a81c14SSteve Longerbeam SCALING, 25419a81c14SSteve Longerbeam }; 25519a81c14SSteve Longerbeam 25619a81c14SSteve Longerbeam struct reg_value { 25719a81c14SSteve Longerbeam u16 reg_addr; 25819a81c14SSteve Longerbeam u8 val; 25919a81c14SSteve Longerbeam u8 mask; 26019a81c14SSteve Longerbeam u32 delay_ms; 26119a81c14SSteve Longerbeam }; 26219a81c14SSteve Longerbeam 26319a81c14SSteve Longerbeam struct ov5640_mode_info { 26419a81c14SSteve Longerbeam enum ov5640_mode_id id; 26519a81c14SSteve Longerbeam enum ov5640_downsize_mode dn_mode; 26622845bf2SJacopo Mondi enum ov5640_pixel_rate_id pixel_rate; 267dba13a0bSMaxime Ripard u32 hact; 268476dec01SMaxime Ripard u32 htot; 269dba13a0bSMaxime Ripard u32 vact; 270476dec01SMaxime Ripard u32 vtot; 27119a81c14SSteve Longerbeam const struct reg_value *reg_data; 27219a81c14SSteve Longerbeam u32 reg_data_size; 2735554c80eSAdam Ford u32 max_fps; 27419a81c14SSteve Longerbeam }; 27519a81c14SSteve Longerbeam 27619a81c14SSteve Longerbeam struct ov5640_ctrls { 27719a81c14SSteve Longerbeam struct v4l2_ctrl_handler handler; 278cc196e48SBenoit Parrot struct v4l2_ctrl *pixel_rate; 279*7a3b8d4bSJacopo Mondi struct v4l2_ctrl *link_freq; 28019a81c14SSteve Longerbeam struct { 28119a81c14SSteve Longerbeam struct v4l2_ctrl *auto_exp; 28219a81c14SSteve Longerbeam struct v4l2_ctrl *exposure; 28319a81c14SSteve Longerbeam }; 28419a81c14SSteve Longerbeam struct { 28519a81c14SSteve Longerbeam struct v4l2_ctrl *auto_wb; 28619a81c14SSteve Longerbeam struct v4l2_ctrl *blue_balance; 28719a81c14SSteve Longerbeam struct v4l2_ctrl *red_balance; 28819a81c14SSteve Longerbeam }; 28919a81c14SSteve Longerbeam struct { 29019a81c14SSteve Longerbeam struct v4l2_ctrl *auto_gain; 29119a81c14SSteve Longerbeam struct v4l2_ctrl *gain; 29219a81c14SSteve Longerbeam }; 29319a81c14SSteve Longerbeam struct v4l2_ctrl *brightness; 2941068fecaSMylène Josserand struct v4l2_ctrl *light_freq; 29519a81c14SSteve Longerbeam struct v4l2_ctrl *saturation; 29619a81c14SSteve Longerbeam struct v4l2_ctrl *contrast; 29719a81c14SSteve Longerbeam struct v4l2_ctrl *hue; 29819a81c14SSteve Longerbeam struct v4l2_ctrl *test_pattern; 299ce85705aSHugues Fruchet struct v4l2_ctrl *hflip; 300ce85705aSHugues Fruchet struct v4l2_ctrl *vflip; 30119a81c14SSteve Longerbeam }; 30219a81c14SSteve Longerbeam 30319a81c14SSteve Longerbeam struct ov5640_dev { 30419a81c14SSteve Longerbeam struct i2c_client *i2c_client; 30519a81c14SSteve Longerbeam struct v4l2_subdev sd; 30619a81c14SSteve Longerbeam struct media_pad pad; 30719a81c14SSteve Longerbeam struct v4l2_fwnode_endpoint ep; /* the parsed DT endpoint info */ 30819a81c14SSteve Longerbeam struct clk *xclk; /* system clock to OV5640 */ 30919a81c14SSteve Longerbeam u32 xclk_freq; 31019a81c14SSteve Longerbeam 31119a81c14SSteve Longerbeam struct regulator_bulk_data supplies[OV5640_NUM_SUPPLIES]; 31219a81c14SSteve Longerbeam struct gpio_desc *reset_gpio; 31319a81c14SSteve Longerbeam struct gpio_desc *pwdn_gpio; 314c3f3ba3eSHugues Fruchet bool upside_down; 31519a81c14SSteve Longerbeam 31619a81c14SSteve Longerbeam /* lock to protect all members below */ 31719a81c14SSteve Longerbeam struct mutex lock; 31819a81c14SSteve Longerbeam 31919a81c14SSteve Longerbeam int power_count; 32019a81c14SSteve Longerbeam 32119a81c14SSteve Longerbeam struct v4l2_mbus_framefmt fmt; 322fb98e29fSHugues Fruchet bool pending_fmt_change; 32319a81c14SSteve Longerbeam 32419a81c14SSteve Longerbeam const struct ov5640_mode_info *current_mode; 325985cdcb0SHugues Fruchet const struct ov5640_mode_info *last_mode; 32619a81c14SSteve Longerbeam enum ov5640_frame_rate current_fr; 32719a81c14SSteve Longerbeam struct v4l2_fract frame_interval; 32819a81c14SSteve Longerbeam 32919a81c14SSteve Longerbeam struct ov5640_ctrls ctrls; 33019a81c14SSteve Longerbeam 33119a81c14SSteve Longerbeam u32 prev_sysclk, prev_hts; 33219a81c14SSteve Longerbeam u32 ae_low, ae_high, ae_target; 33319a81c14SSteve Longerbeam 33419a81c14SSteve Longerbeam bool pending_mode_change; 33519a81c14SSteve Longerbeam bool streaming; 33619a81c14SSteve Longerbeam }; 33719a81c14SSteve Longerbeam 33819a81c14SSteve Longerbeam static inline struct ov5640_dev *to_ov5640_dev(struct v4l2_subdev *sd) 33919a81c14SSteve Longerbeam { 34019a81c14SSteve Longerbeam return container_of(sd, struct ov5640_dev, sd); 34119a81c14SSteve Longerbeam } 34219a81c14SSteve Longerbeam 34319a81c14SSteve Longerbeam static inline struct v4l2_subdev *ctrl_to_sd(struct v4l2_ctrl *ctrl) 34419a81c14SSteve Longerbeam { 34519a81c14SSteve Longerbeam return &container_of(ctrl->handler, struct ov5640_dev, 34619a81c14SSteve Longerbeam ctrls.handler)->sd; 34719a81c14SSteve Longerbeam } 34819a81c14SSteve Longerbeam 3498e823f5cSJacopo Mondi static inline bool ov5640_is_csi2(const struct ov5640_dev *sensor) 3508e823f5cSJacopo Mondi { 3518e823f5cSJacopo Mondi return sensor->ep.bus_type == V4L2_MBUS_CSI2_DPHY; 3528e823f5cSJacopo Mondi } 3538e823f5cSJacopo Mondi 35419a81c14SSteve Longerbeam /* 35519a81c14SSteve Longerbeam * FIXME: all of these register tables are likely filled with 35619a81c14SSteve Longerbeam * entries that set the register to their power-on default values, 35719a81c14SSteve Longerbeam * and which are otherwise not touched by this driver. Those entries 35819a81c14SSteve Longerbeam * should be identified and removed to speed register load time 35919a81c14SSteve Longerbeam * over i2c. 36019a81c14SSteve Longerbeam */ 361fb98e29fSHugues Fruchet /* YUV422 UYVY VGA@30fps */ 36219a81c14SSteve Longerbeam static const struct reg_value ov5640_init_setting_30fps_VGA[] = { 36319a81c14SSteve Longerbeam {0x3103, 0x11, 0, 0}, {0x3008, 0x82, 0, 5}, {0x3008, 0x42, 0, 0}, 364576f5d4bSLad Prabhakar {0x3103, 0x03, 0, 0}, {0x3630, 0x36, 0, 0}, 36519a81c14SSteve Longerbeam {0x3631, 0x0e, 0, 0}, {0x3632, 0xe2, 0, 0}, {0x3633, 0x12, 0, 0}, 36619a81c14SSteve Longerbeam {0x3621, 0xe0, 0, 0}, {0x3704, 0xa0, 0, 0}, {0x3703, 0x5a, 0, 0}, 36719a81c14SSteve Longerbeam {0x3715, 0x78, 0, 0}, {0x3717, 0x01, 0, 0}, {0x370b, 0x60, 0, 0}, 36819a81c14SSteve Longerbeam {0x3705, 0x1a, 0, 0}, {0x3905, 0x02, 0, 0}, {0x3906, 0x10, 0, 0}, 36919a81c14SSteve Longerbeam {0x3901, 0x0a, 0, 0}, {0x3731, 0x12, 0, 0}, {0x3600, 0x08, 0, 0}, 37019a81c14SSteve Longerbeam {0x3601, 0x33, 0, 0}, {0x302d, 0x60, 0, 0}, {0x3620, 0x52, 0, 0}, 37119a81c14SSteve Longerbeam {0x371b, 0x20, 0, 0}, {0x471c, 0x50, 0, 0}, {0x3a13, 0x43, 0, 0}, 37219a81c14SSteve Longerbeam {0x3a18, 0x00, 0, 0}, {0x3a19, 0xf8, 0, 0}, {0x3635, 0x13, 0, 0}, 37319a81c14SSteve Longerbeam {0x3636, 0x03, 0, 0}, {0x3634, 0x40, 0, 0}, {0x3622, 0x01, 0, 0}, 37419a81c14SSteve Longerbeam {0x3c01, 0xa4, 0, 0}, {0x3c04, 0x28, 0, 0}, {0x3c05, 0x98, 0, 0}, 37519a81c14SSteve Longerbeam {0x3c06, 0x00, 0, 0}, {0x3c07, 0x08, 0, 0}, {0x3c08, 0x00, 0, 0}, 37619a81c14SSteve Longerbeam {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0}, 37719a81c14SSteve Longerbeam {0x3820, 0x41, 0, 0}, {0x3821, 0x07, 0, 0}, {0x3814, 0x31, 0, 0}, 37819a81c14SSteve Longerbeam {0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0}, 37919a81c14SSteve Longerbeam {0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0}, 38019a81c14SSteve Longerbeam {0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0}, 381476dec01SMaxime Ripard {0x3810, 0x00, 0, 0}, 38219a81c14SSteve Longerbeam {0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0}, 38319a81c14SSteve Longerbeam {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0}, 38419a81c14SSteve Longerbeam {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0}, 38519a81c14SSteve Longerbeam {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0}, 38619a81c14SSteve Longerbeam {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0}, 38719a81c14SSteve Longerbeam {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0}, 38819a81c14SSteve Longerbeam {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x3000, 0x00, 0, 0}, 38919a81c14SSteve Longerbeam {0x3002, 0x1c, 0, 0}, {0x3004, 0xff, 0, 0}, {0x3006, 0xc3, 0, 0}, 390aa4bb8b8SJacopo Mondi {0x302e, 0x08, 0, 0}, {0x4300, 0x3f, 0, 0}, 3912b5c18f9SChen-Yu Tsai {0x501f, 0x00, 0, 0}, {0x4407, 0x04, 0, 0}, 39219a81c14SSteve Longerbeam {0x440e, 0x00, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0}, 393aa4bb8b8SJacopo Mondi {0x4837, 0x0a, 0, 0}, {0x3824, 0x02, 0, 0}, 39419a81c14SSteve Longerbeam {0x5000, 0xa7, 0, 0}, {0x5001, 0xa3, 0, 0}, {0x5180, 0xff, 0, 0}, 39519a81c14SSteve Longerbeam {0x5181, 0xf2, 0, 0}, {0x5182, 0x00, 0, 0}, {0x5183, 0x14, 0, 0}, 39619a81c14SSteve Longerbeam {0x5184, 0x25, 0, 0}, {0x5185, 0x24, 0, 0}, {0x5186, 0x09, 0, 0}, 39719a81c14SSteve Longerbeam {0x5187, 0x09, 0, 0}, {0x5188, 0x09, 0, 0}, {0x5189, 0x88, 0, 0}, 39819a81c14SSteve Longerbeam {0x518a, 0x54, 0, 0}, {0x518b, 0xee, 0, 0}, {0x518c, 0xb2, 0, 0}, 39919a81c14SSteve Longerbeam {0x518d, 0x50, 0, 0}, {0x518e, 0x34, 0, 0}, {0x518f, 0x6b, 0, 0}, 40019a81c14SSteve Longerbeam {0x5190, 0x46, 0, 0}, {0x5191, 0xf8, 0, 0}, {0x5192, 0x04, 0, 0}, 40119a81c14SSteve Longerbeam {0x5193, 0x70, 0, 0}, {0x5194, 0xf0, 0, 0}, {0x5195, 0xf0, 0, 0}, 40219a81c14SSteve Longerbeam {0x5196, 0x03, 0, 0}, {0x5197, 0x01, 0, 0}, {0x5198, 0x04, 0, 0}, 40319a81c14SSteve Longerbeam {0x5199, 0x6c, 0, 0}, {0x519a, 0x04, 0, 0}, {0x519b, 0x00, 0, 0}, 40419a81c14SSteve Longerbeam {0x519c, 0x09, 0, 0}, {0x519d, 0x2b, 0, 0}, {0x519e, 0x38, 0, 0}, 40519a81c14SSteve Longerbeam {0x5381, 0x1e, 0, 0}, {0x5382, 0x5b, 0, 0}, {0x5383, 0x08, 0, 0}, 40619a81c14SSteve Longerbeam {0x5384, 0x0a, 0, 0}, {0x5385, 0x7e, 0, 0}, {0x5386, 0x88, 0, 0}, 40719a81c14SSteve Longerbeam {0x5387, 0x7c, 0, 0}, {0x5388, 0x6c, 0, 0}, {0x5389, 0x10, 0, 0}, 40819a81c14SSteve Longerbeam {0x538a, 0x01, 0, 0}, {0x538b, 0x98, 0, 0}, {0x5300, 0x08, 0, 0}, 40919a81c14SSteve Longerbeam {0x5301, 0x30, 0, 0}, {0x5302, 0x10, 0, 0}, {0x5303, 0x00, 0, 0}, 41019a81c14SSteve Longerbeam {0x5304, 0x08, 0, 0}, {0x5305, 0x30, 0, 0}, {0x5306, 0x08, 0, 0}, 41119a81c14SSteve Longerbeam {0x5307, 0x16, 0, 0}, {0x5309, 0x08, 0, 0}, {0x530a, 0x30, 0, 0}, 41219a81c14SSteve Longerbeam {0x530b, 0x04, 0, 0}, {0x530c, 0x06, 0, 0}, {0x5480, 0x01, 0, 0}, 41319a81c14SSteve Longerbeam {0x5481, 0x08, 0, 0}, {0x5482, 0x14, 0, 0}, {0x5483, 0x28, 0, 0}, 41419a81c14SSteve Longerbeam {0x5484, 0x51, 0, 0}, {0x5485, 0x65, 0, 0}, {0x5486, 0x71, 0, 0}, 41519a81c14SSteve Longerbeam {0x5487, 0x7d, 0, 0}, {0x5488, 0x87, 0, 0}, {0x5489, 0x91, 0, 0}, 41619a81c14SSteve Longerbeam {0x548a, 0x9a, 0, 0}, {0x548b, 0xaa, 0, 0}, {0x548c, 0xb8, 0, 0}, 41719a81c14SSteve Longerbeam {0x548d, 0xcd, 0, 0}, {0x548e, 0xdd, 0, 0}, {0x548f, 0xea, 0, 0}, 41819a81c14SSteve Longerbeam {0x5490, 0x1d, 0, 0}, {0x5580, 0x02, 0, 0}, {0x5583, 0x40, 0, 0}, 41919a81c14SSteve Longerbeam {0x5584, 0x10, 0, 0}, {0x5589, 0x10, 0, 0}, {0x558a, 0x00, 0, 0}, 42019a81c14SSteve Longerbeam {0x558b, 0xf8, 0, 0}, {0x5800, 0x23, 0, 0}, {0x5801, 0x14, 0, 0}, 42119a81c14SSteve Longerbeam {0x5802, 0x0f, 0, 0}, {0x5803, 0x0f, 0, 0}, {0x5804, 0x12, 0, 0}, 42219a81c14SSteve Longerbeam {0x5805, 0x26, 0, 0}, {0x5806, 0x0c, 0, 0}, {0x5807, 0x08, 0, 0}, 42319a81c14SSteve Longerbeam {0x5808, 0x05, 0, 0}, {0x5809, 0x05, 0, 0}, {0x580a, 0x08, 0, 0}, 42419a81c14SSteve Longerbeam {0x580b, 0x0d, 0, 0}, {0x580c, 0x08, 0, 0}, {0x580d, 0x03, 0, 0}, 42519a81c14SSteve Longerbeam {0x580e, 0x00, 0, 0}, {0x580f, 0x00, 0, 0}, {0x5810, 0x03, 0, 0}, 42619a81c14SSteve Longerbeam {0x5811, 0x09, 0, 0}, {0x5812, 0x07, 0, 0}, {0x5813, 0x03, 0, 0}, 42719a81c14SSteve Longerbeam {0x5814, 0x00, 0, 0}, {0x5815, 0x01, 0, 0}, {0x5816, 0x03, 0, 0}, 42819a81c14SSteve Longerbeam {0x5817, 0x08, 0, 0}, {0x5818, 0x0d, 0, 0}, {0x5819, 0x08, 0, 0}, 42919a81c14SSteve Longerbeam {0x581a, 0x05, 0, 0}, {0x581b, 0x06, 0, 0}, {0x581c, 0x08, 0, 0}, 43019a81c14SSteve Longerbeam {0x581d, 0x0e, 0, 0}, {0x581e, 0x29, 0, 0}, {0x581f, 0x17, 0, 0}, 43119a81c14SSteve Longerbeam {0x5820, 0x11, 0, 0}, {0x5821, 0x11, 0, 0}, {0x5822, 0x15, 0, 0}, 43219a81c14SSteve Longerbeam {0x5823, 0x28, 0, 0}, {0x5824, 0x46, 0, 0}, {0x5825, 0x26, 0, 0}, 43319a81c14SSteve Longerbeam {0x5826, 0x08, 0, 0}, {0x5827, 0x26, 0, 0}, {0x5828, 0x64, 0, 0}, 43419a81c14SSteve Longerbeam {0x5829, 0x26, 0, 0}, {0x582a, 0x24, 0, 0}, {0x582b, 0x22, 0, 0}, 43519a81c14SSteve Longerbeam {0x582c, 0x24, 0, 0}, {0x582d, 0x24, 0, 0}, {0x582e, 0x06, 0, 0}, 43619a81c14SSteve Longerbeam {0x582f, 0x22, 0, 0}, {0x5830, 0x40, 0, 0}, {0x5831, 0x42, 0, 0}, 43719a81c14SSteve Longerbeam {0x5832, 0x24, 0, 0}, {0x5833, 0x26, 0, 0}, {0x5834, 0x24, 0, 0}, 43819a81c14SSteve Longerbeam {0x5835, 0x22, 0, 0}, {0x5836, 0x22, 0, 0}, {0x5837, 0x26, 0, 0}, 43919a81c14SSteve Longerbeam {0x5838, 0x44, 0, 0}, {0x5839, 0x24, 0, 0}, {0x583a, 0x26, 0, 0}, 44019a81c14SSteve Longerbeam {0x583b, 0x28, 0, 0}, {0x583c, 0x42, 0, 0}, {0x583d, 0xce, 0, 0}, 44119a81c14SSteve Longerbeam {0x5025, 0x00, 0, 0}, {0x3a0f, 0x30, 0, 0}, {0x3a10, 0x28, 0, 0}, 44219a81c14SSteve Longerbeam {0x3a1b, 0x30, 0, 0}, {0x3a1e, 0x26, 0, 0}, {0x3a11, 0x60, 0, 0}, 44319a81c14SSteve Longerbeam {0x3a1f, 0x14, 0, 0}, {0x3008, 0x02, 0, 0}, {0x3c00, 0x04, 0, 300}, 44419a81c14SSteve Longerbeam }; 44519a81c14SSteve Longerbeam 446086c25f8SMaxime Ripard static const struct reg_value ov5640_setting_VGA_640_480[] = { 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}, 46219a81c14SSteve Longerbeam {0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0}, 46319a81c14SSteve Longerbeam }; 46419a81c14SSteve Longerbeam 465086c25f8SMaxime Ripard static const struct reg_value ov5640_setting_XGA_1024_768[] = { 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}, 48186633417SMaxime Ripard {0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0}, 48219a81c14SSteve Longerbeam }; 48319a81c14SSteve Longerbeam 484086c25f8SMaxime Ripard static const struct reg_value ov5640_setting_QVGA_320_240[] = { 485c14d107eSMaxime Ripard {0x3c07, 0x08, 0, 0}, 48619a81c14SSteve Longerbeam {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0}, 487ce85705aSHugues Fruchet {0x3814, 0x31, 0, 0}, 48819a81c14SSteve Longerbeam {0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0}, 48919a81c14SSteve Longerbeam {0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0}, 49019a81c14SSteve Longerbeam {0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0}, 491476dec01SMaxime Ripard {0x3810, 0x00, 0, 0}, 49219a81c14SSteve Longerbeam {0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0}, 49319a81c14SSteve Longerbeam {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0}, 49419a81c14SSteve Longerbeam {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0}, 49519a81c14SSteve Longerbeam {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0}, 49619a81c14SSteve Longerbeam {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0}, 49719a81c14SSteve Longerbeam {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0}, 4982b5c18f9SChen-Yu Tsai {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, 49919a81c14SSteve Longerbeam {0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0}, 50019a81c14SSteve Longerbeam {0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0}, 50119a81c14SSteve Longerbeam }; 50219a81c14SSteve Longerbeam 50332ea5e05SHugues Fruchet static const struct reg_value ov5640_setting_QQVGA_160_120[] = { 50432ea5e05SHugues Fruchet {0x3c07, 0x08, 0, 0}, 50532ea5e05SHugues Fruchet {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0}, 50632ea5e05SHugues Fruchet {0x3814, 0x31, 0, 0}, 50732ea5e05SHugues Fruchet {0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0}, 50832ea5e05SHugues Fruchet {0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0}, 50932ea5e05SHugues Fruchet {0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0}, 51032ea5e05SHugues Fruchet {0x3810, 0x00, 0, 0}, 51132ea5e05SHugues Fruchet {0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0}, 51232ea5e05SHugues Fruchet {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0}, 51332ea5e05SHugues Fruchet {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0}, 51432ea5e05SHugues Fruchet {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0}, 51532ea5e05SHugues Fruchet {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0}, 51632ea5e05SHugues Fruchet {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0}, 51732ea5e05SHugues Fruchet {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, 51832ea5e05SHugues Fruchet {0x4407, 0x04, 0, 0}, {0x5001, 0xa3, 0, 0}, 51932ea5e05SHugues Fruchet }; 52032ea5e05SHugues Fruchet 521086c25f8SMaxime Ripard static const struct reg_value ov5640_setting_QCIF_176_144[] = { 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, 0x06, 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_NTSC_720_480[] = { 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, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x3c, 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_PAL_720_576[] = { 560c14d107eSMaxime Ripard {0x3c07, 0x08, 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, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0}, 56519a81c14SSteve Longerbeam {0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0}, 566476dec01SMaxime Ripard {0x3810, 0x00, 0, 0}, 56719a81c14SSteve Longerbeam {0x3811, 0x38, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 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, 0x03, 0, 0}, 57019a81c14SSteve Longerbeam {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0}, 57119a81c14SSteve Longerbeam {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0}, 57219a81c14SSteve Longerbeam {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0}, 5732b5c18f9SChen-Yu Tsai {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, 57419a81c14SSteve Longerbeam {0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0}, 57519a81c14SSteve Longerbeam {0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0}, 57619a81c14SSteve Longerbeam }; 57719a81c14SSteve Longerbeam 578086c25f8SMaxime Ripard static const struct reg_value ov5640_setting_720P_1280_720[] = { 579c14d107eSMaxime Ripard {0x3c07, 0x07, 0, 0}, 58019a81c14SSteve Longerbeam {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0}, 581ce85705aSHugues Fruchet {0x3814, 0x31, 0, 0}, 58219a81c14SSteve Longerbeam {0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0}, 58319a81c14SSteve Longerbeam {0x3802, 0x00, 0, 0}, {0x3803, 0xfa, 0, 0}, {0x3804, 0x0a, 0, 0}, 58419a81c14SSteve Longerbeam {0x3805, 0x3f, 0, 0}, {0x3806, 0x06, 0, 0}, {0x3807, 0xa9, 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, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0}, 58819a81c14SSteve Longerbeam {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x02, 0, 0}, 58919a81c14SSteve Longerbeam {0x3a03, 0xe4, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0xbc, 0, 0}, 59019a81c14SSteve Longerbeam {0x3a0a, 0x01, 0, 0}, {0x3a0b, 0x72, 0, 0}, {0x3a0e, 0x01, 0, 0}, 59119a81c14SSteve Longerbeam {0x3a0d, 0x02, 0, 0}, {0x3a14, 0x02, 0, 0}, {0x3a15, 0xe4, 0, 0}, 5922b5c18f9SChen-Yu Tsai {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, 59319a81c14SSteve Longerbeam {0x4407, 0x04, 0, 0}, {0x460b, 0x37, 0, 0}, {0x460c, 0x20, 0, 0}, 59419a81c14SSteve Longerbeam {0x3824, 0x04, 0, 0}, {0x5001, 0x83, 0, 0}, 59519a81c14SSteve Longerbeam }; 59619a81c14SSteve Longerbeam 597086c25f8SMaxime Ripard static const struct reg_value ov5640_setting_1080P_1920_1080[] = { 598c14d107eSMaxime Ripard {0x3c07, 0x08, 0, 0}, 59919a81c14SSteve Longerbeam {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0}, 600ce85705aSHugues Fruchet {0x3814, 0x11, 0, 0}, 60119a81c14SSteve Longerbeam {0x3815, 0x11, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0}, 60219a81c14SSteve Longerbeam {0x3802, 0x00, 0, 0}, {0x3803, 0x00, 0, 0}, {0x3804, 0x0a, 0, 0}, 60319a81c14SSteve Longerbeam {0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9f, 0, 0}, 604476dec01SMaxime Ripard {0x3810, 0x00, 0, 0}, 60519a81c14SSteve Longerbeam {0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x04, 0, 0}, 60619a81c14SSteve Longerbeam {0x3618, 0x04, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x21, 0, 0}, 60719a81c14SSteve Longerbeam {0x3709, 0x12, 0, 0}, {0x370c, 0x00, 0, 0}, {0x3a02, 0x03, 0, 0}, 60819a81c14SSteve Longerbeam {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0}, 60919a81c14SSteve Longerbeam {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0}, 61019a81c14SSteve Longerbeam {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0}, 6112b5c18f9SChen-Yu Tsai {0x4001, 0x02, 0, 0}, {0x4004, 0x06, 0, 0}, 61219a81c14SSteve Longerbeam {0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0}, 613c14d107eSMaxime Ripard {0x3824, 0x02, 0, 0}, {0x5001, 0x83, 0, 0}, 614c14d107eSMaxime Ripard {0x3c07, 0x07, 0, 0}, {0x3c08, 0x00, 0, 0}, 61519a81c14SSteve Longerbeam {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0}, 61619a81c14SSteve Longerbeam {0x3800, 0x01, 0, 0}, {0x3801, 0x50, 0, 0}, {0x3802, 0x01, 0, 0}, 61719a81c14SSteve Longerbeam {0x3803, 0xb2, 0, 0}, {0x3804, 0x08, 0, 0}, {0x3805, 0xef, 0, 0}, 61886633417SMaxime Ripard {0x3806, 0x05, 0, 0}, {0x3807, 0xf1, 0, 0}, 619476dec01SMaxime Ripard {0x3612, 0x2b, 0, 0}, {0x3708, 0x64, 0, 0}, 62019a81c14SSteve Longerbeam {0x3a02, 0x04, 0, 0}, {0x3a03, 0x60, 0, 0}, {0x3a08, 0x01, 0, 0}, 62119a81c14SSteve Longerbeam {0x3a09, 0x50, 0, 0}, {0x3a0a, 0x01, 0, 0}, {0x3a0b, 0x18, 0, 0}, 62219a81c14SSteve Longerbeam {0x3a0e, 0x03, 0, 0}, {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x04, 0, 0}, 6232b5c18f9SChen-Yu Tsai {0x3a15, 0x60, 0, 0}, {0x4407, 0x04, 0, 0}, 62419a81c14SSteve Longerbeam {0x460b, 0x37, 0, 0}, {0x460c, 0x20, 0, 0}, {0x3824, 0x04, 0, 0}, 62592b9096cSBenoit Parrot {0x4005, 0x1a, 0, 0}, 62619a81c14SSteve Longerbeam }; 62719a81c14SSteve Longerbeam 628086c25f8SMaxime Ripard static const struct reg_value ov5640_setting_QSXGA_2592_1944[] = { 629c14d107eSMaxime Ripard {0x3c07, 0x08, 0, 0}, 63019a81c14SSteve Longerbeam {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0}, 631ce85705aSHugues Fruchet {0x3814, 0x11, 0, 0}, 63219a81c14SSteve Longerbeam {0x3815, 0x11, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0}, 63319a81c14SSteve Longerbeam {0x3802, 0x00, 0, 0}, {0x3803, 0x00, 0, 0}, {0x3804, 0x0a, 0, 0}, 63419a81c14SSteve Longerbeam {0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9f, 0, 0}, 635476dec01SMaxime Ripard {0x3810, 0x00, 0, 0}, 63619a81c14SSteve Longerbeam {0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x04, 0, 0}, 63719a81c14SSteve Longerbeam {0x3618, 0x04, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x21, 0, 0}, 63819a81c14SSteve Longerbeam {0x3709, 0x12, 0, 0}, {0x370c, 0x00, 0, 0}, {0x3a02, 0x03, 0, 0}, 63919a81c14SSteve Longerbeam {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0}, 64019a81c14SSteve Longerbeam {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0}, 64119a81c14SSteve Longerbeam {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0}, 6422b5c18f9SChen-Yu Tsai {0x4001, 0x02, 0, 0}, {0x4004, 0x06, 0, 0}, 64319a81c14SSteve Longerbeam {0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0}, 64419a81c14SSteve Longerbeam {0x3824, 0x02, 0, 0}, {0x5001, 0x83, 0, 70}, 64519a81c14SSteve Longerbeam }; 64619a81c14SSteve Longerbeam 64719a81c14SSteve Longerbeam /* power-on sensor init reg table */ 64819a81c14SSteve Longerbeam static const struct ov5640_mode_info ov5640_mode_init_data = { 64922845bf2SJacopo Mondi 0, SUBSAMPLING, 65022845bf2SJacopo Mondi OV5640_PIXEL_RATE_96M, 65122845bf2SJacopo Mondi 640, 1896, 480, 984, 652476dec01SMaxime Ripard ov5640_init_setting_30fps_VGA, 65319a81c14SSteve Longerbeam ARRAY_SIZE(ov5640_init_setting_30fps_VGA), 6545554c80eSAdam Ford OV5640_30_FPS, 65519a81c14SSteve Longerbeam }; 65619a81c14SSteve Longerbeam 65719a81c14SSteve Longerbeam static const struct ov5640_mode_info 658086c25f8SMaxime Ripard ov5640_mode_data[OV5640_NUM_MODES] = { 6598409d017SJacopo Mondi { 6608409d017SJacopo Mondi /* 160x120 */ 6618409d017SJacopo Mondi OV5640_MODE_QQVGA_160_120, SUBSAMPLING, 66222845bf2SJacopo Mondi OV5640_PIXEL_RATE_48M, 66332ea5e05SHugues Fruchet 160, 1896, 120, 984, 66432ea5e05SHugues Fruchet ov5640_setting_QQVGA_160_120, 66532ea5e05SHugues Fruchet ARRAY_SIZE(ov5640_setting_QQVGA_160_120), 6668409d017SJacopo Mondi OV5640_30_FPS 6678409d017SJacopo Mondi }, { 6688409d017SJacopo Mondi /* 176x144 */ 6698409d017SJacopo Mondi OV5640_MODE_QCIF_176_144, SUBSAMPLING, 67022845bf2SJacopo Mondi OV5640_PIXEL_RATE_48M, 671476dec01SMaxime Ripard 176, 1896, 144, 984, 672086c25f8SMaxime Ripard ov5640_setting_QCIF_176_144, 6735554c80eSAdam Ford ARRAY_SIZE(ov5640_setting_QCIF_176_144), 6748409d017SJacopo Mondi OV5640_30_FPS 6758409d017SJacopo Mondi }, { 6768409d017SJacopo Mondi /* 320x240 */ 6778409d017SJacopo Mondi OV5640_MODE_QVGA_320_240, SUBSAMPLING, 67822845bf2SJacopo Mondi OV5640_PIXEL_RATE_48M, 679476dec01SMaxime Ripard 320, 1896, 240, 984, 680086c25f8SMaxime Ripard ov5640_setting_QVGA_320_240, 6815554c80eSAdam Ford ARRAY_SIZE(ov5640_setting_QVGA_320_240), 6828409d017SJacopo Mondi OV5640_30_FPS 6838409d017SJacopo Mondi }, { 6848409d017SJacopo Mondi /* 640x480 */ 6858409d017SJacopo Mondi OV5640_MODE_VGA_640_480, SUBSAMPLING, 68622845bf2SJacopo Mondi OV5640_PIXEL_RATE_48M, 687476dec01SMaxime Ripard 640, 1896, 480, 1080, 688086c25f8SMaxime Ripard ov5640_setting_VGA_640_480, 6895554c80eSAdam Ford ARRAY_SIZE(ov5640_setting_VGA_640_480), 6908409d017SJacopo Mondi OV5640_60_FPS 6918409d017SJacopo Mondi }, { 6928409d017SJacopo Mondi /* 720x480 */ 6938409d017SJacopo Mondi OV5640_MODE_NTSC_720_480, SUBSAMPLING, 69422845bf2SJacopo Mondi OV5640_PIXEL_RATE_96M, 695476dec01SMaxime Ripard 720, 1896, 480, 984, 696086c25f8SMaxime Ripard ov5640_setting_NTSC_720_480, 6975554c80eSAdam Ford ARRAY_SIZE(ov5640_setting_NTSC_720_480), 6988409d017SJacopo Mondi OV5640_30_FPS 6998409d017SJacopo Mondi }, { 7008409d017SJacopo Mondi /* 720x576 */ 7018409d017SJacopo Mondi OV5640_MODE_PAL_720_576, SUBSAMPLING, 70222845bf2SJacopo Mondi OV5640_PIXEL_RATE_96M, 703476dec01SMaxime Ripard 720, 1896, 576, 984, 704086c25f8SMaxime Ripard ov5640_setting_PAL_720_576, 7055554c80eSAdam Ford ARRAY_SIZE(ov5640_setting_PAL_720_576), 7068409d017SJacopo Mondi OV5640_30_FPS 7078409d017SJacopo Mondi }, { 7088409d017SJacopo Mondi /* 1024x768 */ 7098409d017SJacopo Mondi OV5640_MODE_XGA_1024_768, SUBSAMPLING, 71022845bf2SJacopo Mondi OV5640_PIXEL_RATE_96M, 711476dec01SMaxime Ripard 1024, 1896, 768, 1080, 712086c25f8SMaxime Ripard ov5640_setting_XGA_1024_768, 7135554c80eSAdam Ford ARRAY_SIZE(ov5640_setting_XGA_1024_768), 7148409d017SJacopo Mondi OV5640_30_FPS 7158409d017SJacopo Mondi }, { 7168409d017SJacopo Mondi /* 1280x720 */ 7178409d017SJacopo Mondi OV5640_MODE_720P_1280_720, SUBSAMPLING, 71822845bf2SJacopo Mondi OV5640_PIXEL_RATE_124M, 719476dec01SMaxime Ripard 1280, 1892, 720, 740, 720086c25f8SMaxime Ripard ov5640_setting_720P_1280_720, 7215554c80eSAdam Ford ARRAY_SIZE(ov5640_setting_720P_1280_720), 7228409d017SJacopo Mondi OV5640_30_FPS 7238409d017SJacopo Mondi }, { 7248409d017SJacopo Mondi /* 1920x1080 */ 7258409d017SJacopo Mondi OV5640_MODE_1080P_1920_1080, SCALING, 72622845bf2SJacopo Mondi OV5640_PIXEL_RATE_148M, 727476dec01SMaxime Ripard 1920, 2500, 1080, 1120, 728086c25f8SMaxime Ripard ov5640_setting_1080P_1920_1080, 7295554c80eSAdam Ford ARRAY_SIZE(ov5640_setting_1080P_1920_1080), 7308409d017SJacopo Mondi OV5640_30_FPS 7318409d017SJacopo Mondi }, { 7328409d017SJacopo Mondi /* 2592x1944 */ 7338409d017SJacopo Mondi OV5640_MODE_QSXGA_2592_1944, SCALING, 73422845bf2SJacopo Mondi OV5640_PIXEL_RATE_168M, 735476dec01SMaxime Ripard 2592, 2844, 1944, 1968, 736086c25f8SMaxime Ripard ov5640_setting_QSXGA_2592_1944, 7375554c80eSAdam Ford ARRAY_SIZE(ov5640_setting_QSXGA_2592_1944), 7388409d017SJacopo Mondi OV5640_15_FPS 7398409d017SJacopo Mondi }, 74019a81c14SSteve Longerbeam }; 74119a81c14SSteve Longerbeam 74219a81c14SSteve Longerbeam static int ov5640_init_slave_id(struct ov5640_dev *sensor) 74319a81c14SSteve Longerbeam { 74419a81c14SSteve Longerbeam struct i2c_client *client = sensor->i2c_client; 74519a81c14SSteve Longerbeam struct i2c_msg msg; 74619a81c14SSteve Longerbeam u8 buf[3]; 74719a81c14SSteve Longerbeam int ret; 74819a81c14SSteve Longerbeam 74919a81c14SSteve Longerbeam if (client->addr == OV5640_DEFAULT_SLAVE_ID) 75019a81c14SSteve Longerbeam return 0; 75119a81c14SSteve Longerbeam 75219a81c14SSteve Longerbeam buf[0] = OV5640_REG_SLAVE_ID >> 8; 75319a81c14SSteve Longerbeam buf[1] = OV5640_REG_SLAVE_ID & 0xff; 75419a81c14SSteve Longerbeam buf[2] = client->addr << 1; 75519a81c14SSteve Longerbeam 75619a81c14SSteve Longerbeam msg.addr = OV5640_DEFAULT_SLAVE_ID; 75719a81c14SSteve Longerbeam msg.flags = 0; 75819a81c14SSteve Longerbeam msg.buf = buf; 75919a81c14SSteve Longerbeam msg.len = sizeof(buf); 76019a81c14SSteve Longerbeam 76119a81c14SSteve Longerbeam ret = i2c_transfer(client->adapter, &msg, 1); 76219a81c14SSteve Longerbeam if (ret < 0) { 76319a81c14SSteve Longerbeam dev_err(&client->dev, "%s: failed with %d\n", __func__, ret); 76419a81c14SSteve Longerbeam return ret; 76519a81c14SSteve Longerbeam } 76619a81c14SSteve Longerbeam 76719a81c14SSteve Longerbeam return 0; 76819a81c14SSteve Longerbeam } 76919a81c14SSteve Longerbeam 77019a81c14SSteve Longerbeam static int ov5640_write_reg(struct ov5640_dev *sensor, u16 reg, u8 val) 77119a81c14SSteve Longerbeam { 77219a81c14SSteve Longerbeam struct i2c_client *client = sensor->i2c_client; 77319a81c14SSteve Longerbeam struct i2c_msg msg; 77419a81c14SSteve Longerbeam u8 buf[3]; 77519a81c14SSteve Longerbeam int ret; 77619a81c14SSteve Longerbeam 77719a81c14SSteve Longerbeam buf[0] = reg >> 8; 77819a81c14SSteve Longerbeam buf[1] = reg & 0xff; 77919a81c14SSteve Longerbeam buf[2] = val; 78019a81c14SSteve Longerbeam 78119a81c14SSteve Longerbeam msg.addr = client->addr; 78219a81c14SSteve Longerbeam msg.flags = client->flags; 78319a81c14SSteve Longerbeam msg.buf = buf; 78419a81c14SSteve Longerbeam msg.len = sizeof(buf); 78519a81c14SSteve Longerbeam 78619a81c14SSteve Longerbeam ret = i2c_transfer(client->adapter, &msg, 1); 78719a81c14SSteve Longerbeam if (ret < 0) { 7883924c623SHugues Fruchet dev_err(&client->dev, "%s: error: reg=%x, val=%x\n", 78919a81c14SSteve Longerbeam __func__, reg, val); 79019a81c14SSteve Longerbeam return ret; 79119a81c14SSteve Longerbeam } 79219a81c14SSteve Longerbeam 79319a81c14SSteve Longerbeam return 0; 79419a81c14SSteve Longerbeam } 79519a81c14SSteve Longerbeam 79619a81c14SSteve Longerbeam static int ov5640_read_reg(struct ov5640_dev *sensor, u16 reg, u8 *val) 79719a81c14SSteve Longerbeam { 79819a81c14SSteve Longerbeam struct i2c_client *client = sensor->i2c_client; 79919a81c14SSteve Longerbeam struct i2c_msg msg[2]; 80019a81c14SSteve Longerbeam u8 buf[2]; 80119a81c14SSteve Longerbeam int ret; 80219a81c14SSteve Longerbeam 80319a81c14SSteve Longerbeam buf[0] = reg >> 8; 80419a81c14SSteve Longerbeam buf[1] = reg & 0xff; 80519a81c14SSteve Longerbeam 80619a81c14SSteve Longerbeam msg[0].addr = client->addr; 80719a81c14SSteve Longerbeam msg[0].flags = client->flags; 80819a81c14SSteve Longerbeam msg[0].buf = buf; 80919a81c14SSteve Longerbeam msg[0].len = sizeof(buf); 81019a81c14SSteve Longerbeam 81119a81c14SSteve Longerbeam msg[1].addr = client->addr; 81219a81c14SSteve Longerbeam msg[1].flags = client->flags | I2C_M_RD; 81319a81c14SSteve Longerbeam msg[1].buf = buf; 81419a81c14SSteve Longerbeam msg[1].len = 1; 81519a81c14SSteve Longerbeam 81619a81c14SSteve Longerbeam ret = i2c_transfer(client->adapter, msg, 2); 8173924c623SHugues Fruchet if (ret < 0) { 8183924c623SHugues Fruchet dev_err(&client->dev, "%s: error: reg=%x\n", 8193924c623SHugues Fruchet __func__, reg); 82019a81c14SSteve Longerbeam return ret; 8213924c623SHugues Fruchet } 82219a81c14SSteve Longerbeam 82319a81c14SSteve Longerbeam *val = buf[0]; 82419a81c14SSteve Longerbeam return 0; 82519a81c14SSteve Longerbeam } 82619a81c14SSteve Longerbeam 82719a81c14SSteve Longerbeam static int ov5640_read_reg16(struct ov5640_dev *sensor, u16 reg, u16 *val) 82819a81c14SSteve Longerbeam { 82919a81c14SSteve Longerbeam u8 hi, lo; 83019a81c14SSteve Longerbeam int ret; 83119a81c14SSteve Longerbeam 83219a81c14SSteve Longerbeam ret = ov5640_read_reg(sensor, reg, &hi); 83319a81c14SSteve Longerbeam if (ret) 83419a81c14SSteve Longerbeam return ret; 83519a81c14SSteve Longerbeam ret = ov5640_read_reg(sensor, reg + 1, &lo); 83619a81c14SSteve Longerbeam if (ret) 83719a81c14SSteve Longerbeam return ret; 83819a81c14SSteve Longerbeam 83919a81c14SSteve Longerbeam *val = ((u16)hi << 8) | (u16)lo; 84019a81c14SSteve Longerbeam return 0; 84119a81c14SSteve Longerbeam } 84219a81c14SSteve Longerbeam 84319a81c14SSteve Longerbeam static int ov5640_write_reg16(struct ov5640_dev *sensor, u16 reg, u16 val) 84419a81c14SSteve Longerbeam { 84519a81c14SSteve Longerbeam int ret; 84619a81c14SSteve Longerbeam 84719a81c14SSteve Longerbeam ret = ov5640_write_reg(sensor, reg, val >> 8); 84819a81c14SSteve Longerbeam if (ret) 84919a81c14SSteve Longerbeam return ret; 85019a81c14SSteve Longerbeam 85119a81c14SSteve Longerbeam return ov5640_write_reg(sensor, reg + 1, val & 0xff); 85219a81c14SSteve Longerbeam } 85319a81c14SSteve Longerbeam 85419a81c14SSteve Longerbeam static int ov5640_mod_reg(struct ov5640_dev *sensor, u16 reg, 85519a81c14SSteve Longerbeam u8 mask, u8 val) 85619a81c14SSteve Longerbeam { 85719a81c14SSteve Longerbeam u8 readval; 85819a81c14SSteve Longerbeam int ret; 85919a81c14SSteve Longerbeam 86019a81c14SSteve Longerbeam ret = ov5640_read_reg(sensor, reg, &readval); 86119a81c14SSteve Longerbeam if (ret) 86219a81c14SSteve Longerbeam return ret; 86319a81c14SSteve Longerbeam 86419a81c14SSteve Longerbeam readval &= ~mask; 86519a81c14SSteve Longerbeam val &= mask; 86619a81c14SSteve Longerbeam val |= readval; 86719a81c14SSteve Longerbeam 86819a81c14SSteve Longerbeam return ov5640_write_reg(sensor, reg, val); 86919a81c14SSteve Longerbeam } 87019a81c14SSteve Longerbeam 871aa288248SMaxime Ripard /* 872aa288248SMaxime Ripard * After trying the various combinations, reading various 873f8a7647dSMauro Carvalho Chehab * documentations spread around the net, and from the various 874aa288248SMaxime Ripard * feedback, the clock tree is probably as follows: 875aa288248SMaxime Ripard * 876aa288248SMaxime Ripard * +--------------+ 877aa288248SMaxime Ripard * | Ext. Clock | 878aa288248SMaxime Ripard * +-+------------+ 879aa288248SMaxime Ripard * | +----------+ 880aa288248SMaxime Ripard * +->| PLL1 | - reg 0x3036, for the multiplier 881aa288248SMaxime Ripard * +-+--------+ - reg 0x3037, bits 0-3 for the pre-divider 882aa288248SMaxime Ripard * | +--------------+ 883aa288248SMaxime Ripard * +->| System Clock | - reg 0x3035, bits 4-7 884aa288248SMaxime Ripard * +-+------------+ 885aa288248SMaxime Ripard * | +--------------+ 886aa288248SMaxime Ripard * +->| MIPI Divider | - reg 0x3035, bits 0-3 887aa288248SMaxime Ripard * | +-+------------+ 888aa288248SMaxime Ripard * | +----------------> MIPI SCLK 889aa288248SMaxime Ripard * | + +-----+ 890aa288248SMaxime Ripard * | +->| / 2 |-------> MIPI BIT CLK 891aa288248SMaxime Ripard * | +-----+ 892aa288248SMaxime Ripard * | +--------------+ 893aa288248SMaxime Ripard * +->| PLL Root Div | - reg 0x3037, bit 4 894aa288248SMaxime Ripard * +-+------------+ 895aa288248SMaxime Ripard * | +---------+ 8964c85f628SPaul Kocialkowski * +->| Bit Div | - reg 0x3034, bits 0-3 897aa288248SMaxime Ripard * +-+-------+ 898aa288248SMaxime Ripard * | +-------------+ 899aa288248SMaxime Ripard * +->| SCLK Div | - reg 0x3108, bits 0-1 900aa288248SMaxime Ripard * | +-+-----------+ 901aa288248SMaxime Ripard * | +---------------> SCLK 902aa288248SMaxime Ripard * | +-------------+ 903aa288248SMaxime Ripard * +->| SCLK 2X Div | - reg 0x3108, bits 2-3 904aa288248SMaxime Ripard * | +-+-----------+ 905aa288248SMaxime Ripard * | +---------------> SCLK 2X 906aa288248SMaxime Ripard * | +-------------+ 907aa288248SMaxime Ripard * +->| PCLK Div | - reg 0x3108, bits 4-5 908aa288248SMaxime Ripard * ++------------+ 909aa288248SMaxime Ripard * + +-----------+ 910aa288248SMaxime Ripard * +->| P_DIV | - reg 0x3035, bits 0-3 911aa288248SMaxime Ripard * +-----+-----+ 912aa288248SMaxime Ripard * +------------> PCLK 913aa288248SMaxime Ripard * 914aa288248SMaxime Ripard * This is deviating from the datasheet at least for the register 915aa288248SMaxime Ripard * 0x3108, since it's said here that the PCLK would be clocked from 916aa288248SMaxime Ripard * the PLL. 917aa288248SMaxime Ripard * 918aa288248SMaxime Ripard * There seems to be also (unverified) constraints: 919aa288248SMaxime Ripard * - the PLL pre-divider output rate should be in the 4-27MHz range 920aa288248SMaxime Ripard * - the PLL multiplier output rate should be in the 500-1000MHz range 921aa288248SMaxime Ripard * - PCLK >= SCLK * 2 in YUV, >= SCLK in Raw or JPEG 922aa288248SMaxime Ripard * 923aa288248SMaxime Ripard * In the two latter cases, these constraints are met since our 924aa288248SMaxime Ripard * factors are hardcoded. If we were to change that, we would need to 925aa288248SMaxime Ripard * take this into account. The only varying parts are the PLL 926aa288248SMaxime Ripard * multiplier and the system clock divider, which are shared between 927aa288248SMaxime Ripard * all these clocks so won't cause any issue. 928aa288248SMaxime Ripard */ 929aa288248SMaxime Ripard 930aa288248SMaxime Ripard /* 931aa288248SMaxime Ripard * This is supposed to be ranging from 1 to 8, but the value is always 932aa288248SMaxime Ripard * set to 3 in the vendor kernels. 933aa288248SMaxime Ripard */ 934aa288248SMaxime Ripard #define OV5640_PLL_PREDIV 3 935aa288248SMaxime Ripard 936aa288248SMaxime Ripard #define OV5640_PLL_MULT_MIN 4 937aa288248SMaxime Ripard #define OV5640_PLL_MULT_MAX 252 938aa288248SMaxime Ripard 939aa288248SMaxime Ripard /* 940aa288248SMaxime Ripard * This is supposed to be ranging from 1 to 16, but the value is 941aa288248SMaxime Ripard * always set to either 1 or 2 in the vendor kernels. 942aa288248SMaxime Ripard */ 943aa288248SMaxime Ripard #define OV5640_SYSDIV_MIN 1 944aa288248SMaxime Ripard #define OV5640_SYSDIV_MAX 16 945aa288248SMaxime Ripard 946aa288248SMaxime Ripard /* 947aa288248SMaxime Ripard * Hardcode these values for scaler and non-scaler modes. 948aa288248SMaxime Ripard * FIXME: to be re-calcualted for 1 data lanes setups 949aa288248SMaxime Ripard */ 950aa288248SMaxime Ripard #define OV5640_MIPI_DIV_PCLK 2 951aa288248SMaxime Ripard #define OV5640_MIPI_DIV_SCLK 1 952aa288248SMaxime Ripard 953aa288248SMaxime Ripard /* 954aa288248SMaxime Ripard * This is supposed to be ranging from 1 to 2, but the value is always 955aa288248SMaxime Ripard * set to 2 in the vendor kernels. 956aa288248SMaxime Ripard */ 957aa288248SMaxime Ripard #define OV5640_PLL_ROOT_DIV 2 958aa288248SMaxime Ripard #define OV5640_PLL_CTRL3_PLL_ROOT_DIV_2 BIT(4) 959aa288248SMaxime Ripard 960aa288248SMaxime Ripard /* 961aa288248SMaxime Ripard * We only supports 8-bit formats at the moment 962aa288248SMaxime Ripard */ 963aa288248SMaxime Ripard #define OV5640_BIT_DIV 2 964aa288248SMaxime Ripard #define OV5640_PLL_CTRL0_MIPI_MODE_8BIT 0x08 965aa288248SMaxime Ripard 966aa288248SMaxime Ripard /* 967aa288248SMaxime Ripard * This is supposed to be ranging from 1 to 8, but the value is always 968aa288248SMaxime Ripard * set to 2 in the vendor kernels. 969aa288248SMaxime Ripard */ 970aa288248SMaxime Ripard #define OV5640_SCLK_ROOT_DIV 2 971aa288248SMaxime Ripard 972aa288248SMaxime Ripard /* 973aa288248SMaxime Ripard * This is hardcoded so that the consistency is maintained between SCLK and 974aa288248SMaxime Ripard * SCLK 2x. 975aa288248SMaxime Ripard */ 976aa288248SMaxime Ripard #define OV5640_SCLK2X_ROOT_DIV (OV5640_SCLK_ROOT_DIV / 2) 977aa288248SMaxime Ripard 978aa288248SMaxime Ripard /* 979aa288248SMaxime Ripard * This is supposed to be ranging from 1 to 8, but the value is always 980aa288248SMaxime Ripard * set to 1 in the vendor kernels. 981aa288248SMaxime Ripard */ 982aa288248SMaxime Ripard #define OV5640_PCLK_ROOT_DIV 1 983aa288248SMaxime Ripard #define OV5640_PLL_SYS_ROOT_DIVIDER_BYPASS 0x00 984aa288248SMaxime Ripard 985aa288248SMaxime Ripard static unsigned long ov5640_compute_sys_clk(struct ov5640_dev *sensor, 986aa288248SMaxime Ripard u8 pll_prediv, u8 pll_mult, 987aa288248SMaxime Ripard u8 sysdiv) 988aa288248SMaxime Ripard { 989aa288248SMaxime Ripard unsigned long sysclk = sensor->xclk_freq / pll_prediv * pll_mult; 990aa288248SMaxime Ripard 991aa288248SMaxime Ripard /* PLL1 output cannot exceed 1GHz. */ 992aa288248SMaxime Ripard if (sysclk / 1000000 > 1000) 993aa288248SMaxime Ripard return 0; 994aa288248SMaxime Ripard 995aa288248SMaxime Ripard return sysclk / sysdiv; 996aa288248SMaxime Ripard } 997aa288248SMaxime Ripard 998aa288248SMaxime Ripard static unsigned long ov5640_calc_sys_clk(struct ov5640_dev *sensor, 999aa288248SMaxime Ripard unsigned long rate, 1000aa288248SMaxime Ripard u8 *pll_prediv, u8 *pll_mult, 1001aa288248SMaxime Ripard u8 *sysdiv) 1002aa288248SMaxime Ripard { 1003aa288248SMaxime Ripard unsigned long best = ~0; 1004aa288248SMaxime Ripard u8 best_sysdiv = 1, best_mult = 1; 1005aa288248SMaxime Ripard u8 _sysdiv, _pll_mult; 1006aa288248SMaxime Ripard 1007aa288248SMaxime Ripard for (_sysdiv = OV5640_SYSDIV_MIN; 1008aa288248SMaxime Ripard _sysdiv <= OV5640_SYSDIV_MAX; 1009aa288248SMaxime Ripard _sysdiv++) { 1010aa288248SMaxime Ripard for (_pll_mult = OV5640_PLL_MULT_MIN; 1011aa288248SMaxime Ripard _pll_mult <= OV5640_PLL_MULT_MAX; 1012aa288248SMaxime Ripard _pll_mult++) { 1013aa288248SMaxime Ripard unsigned long _rate; 1014aa288248SMaxime Ripard 1015aa288248SMaxime Ripard /* 1016aa288248SMaxime Ripard * The PLL multiplier cannot be odd if above 1017aa288248SMaxime Ripard * 127. 1018aa288248SMaxime Ripard */ 1019aa288248SMaxime Ripard if (_pll_mult > 127 && (_pll_mult % 2)) 1020aa288248SMaxime Ripard continue; 1021aa288248SMaxime Ripard 1022aa288248SMaxime Ripard _rate = ov5640_compute_sys_clk(sensor, 1023aa288248SMaxime Ripard OV5640_PLL_PREDIV, 1024aa288248SMaxime Ripard _pll_mult, _sysdiv); 1025aa288248SMaxime Ripard 1026aa288248SMaxime Ripard /* 1027aa288248SMaxime Ripard * We have reached the maximum allowed PLL1 output, 1028aa288248SMaxime Ripard * increase sysdiv. 1029aa288248SMaxime Ripard */ 10302e3df204SAdam Ford if (!_rate) 1031aa288248SMaxime Ripard break; 1032aa288248SMaxime Ripard 1033aa288248SMaxime Ripard /* 1034aa288248SMaxime Ripard * Prefer rates above the expected clock rate than 1035aa288248SMaxime Ripard * below, even if that means being less precise. 1036aa288248SMaxime Ripard */ 1037aa288248SMaxime Ripard if (_rate < rate) 1038aa288248SMaxime Ripard continue; 1039aa288248SMaxime Ripard 1040aa288248SMaxime Ripard if (abs(rate - _rate) < abs(rate - best)) { 1041aa288248SMaxime Ripard best = _rate; 1042aa288248SMaxime Ripard best_sysdiv = _sysdiv; 1043aa288248SMaxime Ripard best_mult = _pll_mult; 1044aa288248SMaxime Ripard } 1045aa288248SMaxime Ripard 1046aa288248SMaxime Ripard if (_rate == rate) 1047aa288248SMaxime Ripard goto out; 1048aa288248SMaxime Ripard } 1049aa288248SMaxime Ripard } 1050aa288248SMaxime Ripard 1051aa288248SMaxime Ripard out: 1052aa288248SMaxime Ripard *sysdiv = best_sysdiv; 1053aa288248SMaxime Ripard *pll_prediv = OV5640_PLL_PREDIV; 1054aa288248SMaxime Ripard *pll_mult = best_mult; 1055aa288248SMaxime Ripard 1056aa288248SMaxime Ripard return best; 1057aa288248SMaxime Ripard } 1058aa288248SMaxime Ripard 1059aa288248SMaxime Ripard /* 1060aa288248SMaxime Ripard * ov5640_set_mipi_pclk() - Calculate the clock tree configuration values 1061aa288248SMaxime Ripard * for the MIPI CSI-2 output. 1062aa288248SMaxime Ripard * 1063aa288248SMaxime Ripard * @rate: The requested bandwidth per lane in bytes per second. 1064aa288248SMaxime Ripard * 'Bandwidth Per Lane' is calculated as: 1065aa288248SMaxime Ripard * bpl = HTOT * VTOT * FPS * bpp / num_lanes; 1066aa288248SMaxime Ripard * 1067aa288248SMaxime Ripard * This function use the requested bandwidth to calculate: 1068aa288248SMaxime Ripard * - sample_rate = bpl / (bpp / num_lanes); 1069aa288248SMaxime Ripard * = bpl / (PLL_RDIV * BIT_DIV * PCLK_DIV * MIPI_DIV / num_lanes); 1070aa288248SMaxime Ripard * 1071aa288248SMaxime Ripard * - mipi_sclk = bpl / MIPI_DIV / 2; ( / 2 is for CSI-2 DDR) 1072aa288248SMaxime Ripard * 1073aa288248SMaxime Ripard * with these fixed parameters: 1074aa288248SMaxime Ripard * PLL_RDIV = 2; 1075aa288248SMaxime Ripard * BIT_DIVIDER = 2; (MIPI_BIT_MODE == 8 ? 2 : 2,5); 1076aa288248SMaxime Ripard * PCLK_DIV = 1; 1077aa288248SMaxime Ripard * 1078aa288248SMaxime Ripard * The MIPI clock generation differs for modes that use the scaler and modes 1079aa288248SMaxime Ripard * that do not. In case the scaler is in use, the MIPI_SCLK generates the MIPI 1080aa288248SMaxime Ripard * BIT CLk, and thus: 1081aa288248SMaxime Ripard * 1082aa288248SMaxime Ripard * - mipi_sclk = bpl / MIPI_DIV / 2; 1083aa288248SMaxime Ripard * MIPI_DIV = 1; 1084aa288248SMaxime Ripard * 1085aa288248SMaxime Ripard * For modes that do not go through the scaler, the MIPI BIT CLOCK is generated 1086aa288248SMaxime Ripard * from the pixel clock, and thus: 1087aa288248SMaxime Ripard * 1088aa288248SMaxime Ripard * - sample_rate = bpl / (bpp / num_lanes); 1089aa288248SMaxime Ripard * = bpl / (2 * 2 * 1 * MIPI_DIV / num_lanes); 1090aa288248SMaxime Ripard * = bpl / (4 * MIPI_DIV / num_lanes); 1091aa288248SMaxime Ripard * - MIPI_DIV = bpp / (4 * num_lanes); 1092aa288248SMaxime Ripard * 1093aa288248SMaxime Ripard * FIXME: this have been tested with 16bpp and 2 lanes setup only. 1094aa288248SMaxime Ripard * MIPI_DIV is fixed to value 2, but it -might- be changed according to the 1095aa288248SMaxime Ripard * above formula for setups with 1 lane or image formats with different bpp. 1096aa288248SMaxime Ripard * 1097aa288248SMaxime Ripard * FIXME: this deviates from the sensor manual documentation which is quite 1098aa288248SMaxime Ripard * thin on the MIPI clock tree generation part. 1099aa288248SMaxime Ripard */ 1100aa288248SMaxime Ripard static int ov5640_set_mipi_pclk(struct ov5640_dev *sensor, 1101aa288248SMaxime Ripard unsigned long rate) 1102aa288248SMaxime Ripard { 1103aa288248SMaxime Ripard const struct ov5640_mode_info *mode = sensor->current_mode; 1104aa288248SMaxime Ripard u8 prediv, mult, sysdiv; 1105aa288248SMaxime Ripard u8 mipi_div; 1106aa288248SMaxime Ripard int ret; 1107aa288248SMaxime Ripard 1108aa288248SMaxime Ripard /* 1109aa288248SMaxime Ripard * 1280x720 is reported to use 'SUBSAMPLING' only, 1110aa288248SMaxime Ripard * but according to the sensor manual it goes through the 1111aa288248SMaxime Ripard * scaler before subsampling. 1112aa288248SMaxime Ripard */ 1113aa288248SMaxime Ripard if (mode->dn_mode == SCALING || 1114aa288248SMaxime Ripard (mode->id == OV5640_MODE_720P_1280_720)) 1115aa288248SMaxime Ripard mipi_div = OV5640_MIPI_DIV_SCLK; 1116aa288248SMaxime Ripard else 1117aa288248SMaxime Ripard mipi_div = OV5640_MIPI_DIV_PCLK; 1118aa288248SMaxime Ripard 1119aa288248SMaxime Ripard ov5640_calc_sys_clk(sensor, rate, &prediv, &mult, &sysdiv); 1120aa288248SMaxime Ripard 1121aa288248SMaxime Ripard ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL0, 1122aa288248SMaxime Ripard 0x0f, OV5640_PLL_CTRL0_MIPI_MODE_8BIT); 1123aa288248SMaxime Ripard 1124aa288248SMaxime Ripard ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL1, 1125aa288248SMaxime Ripard 0xff, sysdiv << 4 | mipi_div); 1126aa288248SMaxime Ripard if (ret) 1127aa288248SMaxime Ripard return ret; 1128aa288248SMaxime Ripard 1129aa288248SMaxime Ripard ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL2, 0xff, mult); 1130aa288248SMaxime Ripard if (ret) 1131aa288248SMaxime Ripard return ret; 1132aa288248SMaxime Ripard 1133aa288248SMaxime Ripard ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL3, 1134aa288248SMaxime Ripard 0x1f, OV5640_PLL_CTRL3_PLL_ROOT_DIV_2 | prediv); 1135aa288248SMaxime Ripard if (ret) 1136aa288248SMaxime Ripard return ret; 1137aa288248SMaxime Ripard 1138aa288248SMaxime Ripard return ov5640_mod_reg(sensor, OV5640_REG_SYS_ROOT_DIVIDER, 1139aa288248SMaxime Ripard 0x30, OV5640_PLL_SYS_ROOT_DIVIDER_BYPASS); 1140aa288248SMaxime Ripard } 1141aa288248SMaxime Ripard 1142aa288248SMaxime Ripard static unsigned long ov5640_calc_pclk(struct ov5640_dev *sensor, 1143aa288248SMaxime Ripard unsigned long rate, 1144aa288248SMaxime Ripard u8 *pll_prediv, u8 *pll_mult, u8 *sysdiv, 1145aa288248SMaxime Ripard u8 *pll_rdiv, u8 *bit_div, u8 *pclk_div) 1146aa288248SMaxime Ripard { 1147aa288248SMaxime Ripard unsigned long _rate = rate * OV5640_PLL_ROOT_DIV * OV5640_BIT_DIV * 1148aa288248SMaxime Ripard OV5640_PCLK_ROOT_DIV; 1149aa288248SMaxime Ripard 1150aa288248SMaxime Ripard _rate = ov5640_calc_sys_clk(sensor, _rate, pll_prediv, pll_mult, 1151aa288248SMaxime Ripard sysdiv); 1152aa288248SMaxime Ripard *pll_rdiv = OV5640_PLL_ROOT_DIV; 1153aa288248SMaxime Ripard *bit_div = OV5640_BIT_DIV; 1154aa288248SMaxime Ripard *pclk_div = OV5640_PCLK_ROOT_DIV; 1155aa288248SMaxime Ripard 1156aa288248SMaxime Ripard return _rate / *pll_rdiv / *bit_div / *pclk_div; 1157aa288248SMaxime Ripard } 1158aa288248SMaxime Ripard 1159aa288248SMaxime Ripard static int ov5640_set_dvp_pclk(struct ov5640_dev *sensor, unsigned long rate) 1160aa288248SMaxime Ripard { 1161aa288248SMaxime Ripard u8 prediv, mult, sysdiv, pll_rdiv, bit_div, pclk_div; 1162aa288248SMaxime Ripard int ret; 1163aa288248SMaxime Ripard 1164aa288248SMaxime Ripard ov5640_calc_pclk(sensor, rate, &prediv, &mult, &sysdiv, &pll_rdiv, 1165aa288248SMaxime Ripard &bit_div, &pclk_div); 1166aa288248SMaxime Ripard 1167aa288248SMaxime Ripard if (bit_div == 2) 1168aa288248SMaxime Ripard bit_div = 8; 1169aa288248SMaxime Ripard 1170aa288248SMaxime Ripard ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL0, 1171aa288248SMaxime Ripard 0x0f, bit_div); 1172aa288248SMaxime Ripard if (ret) 1173aa288248SMaxime Ripard return ret; 1174aa288248SMaxime Ripard 1175aa288248SMaxime Ripard /* 1176aa288248SMaxime Ripard * We need to set sysdiv according to the clock, and to clear 1177aa288248SMaxime Ripard * the MIPI divider. 1178aa288248SMaxime Ripard */ 1179aa288248SMaxime Ripard ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL1, 1180aa288248SMaxime Ripard 0xff, sysdiv << 4); 1181aa288248SMaxime Ripard if (ret) 1182aa288248SMaxime Ripard return ret; 1183aa288248SMaxime Ripard 1184aa288248SMaxime Ripard ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL2, 1185aa288248SMaxime Ripard 0xff, mult); 1186aa288248SMaxime Ripard if (ret) 1187aa288248SMaxime Ripard return ret; 1188aa288248SMaxime Ripard 1189aa288248SMaxime Ripard ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL3, 1190aa288248SMaxime Ripard 0x1f, prediv | ((pll_rdiv - 1) << 4)); 1191aa288248SMaxime Ripard if (ret) 1192aa288248SMaxime Ripard return ret; 1193aa288248SMaxime Ripard 1194aa288248SMaxime Ripard return ov5640_mod_reg(sensor, OV5640_REG_SYS_ROOT_DIVIDER, 0x30, 1195aa288248SMaxime Ripard (ilog2(pclk_div) << 4)); 1196aa288248SMaxime Ripard } 1197aa288248SMaxime Ripard 11987cb013b1SChen-Yu Tsai /* set JPEG framing sizes */ 11997cb013b1SChen-Yu Tsai static int ov5640_set_jpeg_timings(struct ov5640_dev *sensor, 12007cb013b1SChen-Yu Tsai const struct ov5640_mode_info *mode) 12017cb013b1SChen-Yu Tsai { 12027cb013b1SChen-Yu Tsai int ret; 12037cb013b1SChen-Yu Tsai 12042b5c18f9SChen-Yu Tsai /* 12052b5c18f9SChen-Yu Tsai * compression mode 3 timing 12062b5c18f9SChen-Yu Tsai * 12072b5c18f9SChen-Yu Tsai * Data is transmitted with programmable width (VFIFO_HSIZE). 12082b5c18f9SChen-Yu Tsai * No padding done. Last line may have less data. Varying 12092b5c18f9SChen-Yu Tsai * number of lines per frame, depending on amount of data. 12102b5c18f9SChen-Yu Tsai */ 12112b5c18f9SChen-Yu Tsai ret = ov5640_mod_reg(sensor, OV5640_REG_JPG_MODE_SELECT, 0x7, 0x3); 12122b5c18f9SChen-Yu Tsai if (ret < 0) 12132b5c18f9SChen-Yu Tsai return ret; 12142b5c18f9SChen-Yu Tsai 12157cb013b1SChen-Yu Tsai ret = ov5640_write_reg16(sensor, OV5640_REG_VFIFO_HSIZE, mode->hact); 12167cb013b1SChen-Yu Tsai if (ret < 0) 12177cb013b1SChen-Yu Tsai return ret; 12187cb013b1SChen-Yu Tsai 12197cb013b1SChen-Yu Tsai return ov5640_write_reg16(sensor, OV5640_REG_VFIFO_VSIZE, mode->vact); 12207cb013b1SChen-Yu Tsai } 12217cb013b1SChen-Yu Tsai 122219a81c14SSteve Longerbeam /* download ov5640 settings to sensor through i2c */ 1223bad1774eSJacopo Mondi static int ov5640_set_timings(struct ov5640_dev *sensor, 1224bad1774eSJacopo Mondi const struct ov5640_mode_info *mode) 1225bad1774eSJacopo Mondi { 1226bad1774eSJacopo Mondi int ret; 1227bad1774eSJacopo Mondi 12287cb013b1SChen-Yu Tsai if (sensor->fmt.code == MEDIA_BUS_FMT_JPEG_1X8) { 12297cb013b1SChen-Yu Tsai ret = ov5640_set_jpeg_timings(sensor, mode); 12307cb013b1SChen-Yu Tsai if (ret < 0) 12317cb013b1SChen-Yu Tsai return ret; 12327cb013b1SChen-Yu Tsai } 12337cb013b1SChen-Yu Tsai 1234bad1774eSJacopo Mondi ret = ov5640_write_reg16(sensor, OV5640_REG_TIMING_DVPHO, mode->hact); 1235bad1774eSJacopo Mondi if (ret < 0) 1236bad1774eSJacopo Mondi return ret; 1237bad1774eSJacopo Mondi 1238bad1774eSJacopo Mondi ret = ov5640_write_reg16(sensor, OV5640_REG_TIMING_DVPVO, mode->vact); 1239bad1774eSJacopo Mondi if (ret < 0) 1240bad1774eSJacopo Mondi return ret; 1241bad1774eSJacopo Mondi 1242bad1774eSJacopo Mondi ret = ov5640_write_reg16(sensor, OV5640_REG_TIMING_HTS, mode->htot); 1243bad1774eSJacopo Mondi if (ret < 0) 1244bad1774eSJacopo Mondi return ret; 1245bad1774eSJacopo Mondi 1246bad1774eSJacopo Mondi return ov5640_write_reg16(sensor, OV5640_REG_TIMING_VTS, mode->vtot); 1247bad1774eSJacopo Mondi } 1248bad1774eSJacopo Mondi 124919a81c14SSteve Longerbeam static int ov5640_load_regs(struct ov5640_dev *sensor, 125019a81c14SSteve Longerbeam const struct ov5640_mode_info *mode) 125119a81c14SSteve Longerbeam { 125219a81c14SSteve Longerbeam const struct reg_value *regs = mode->reg_data; 125319a81c14SSteve Longerbeam unsigned int i; 125419a81c14SSteve Longerbeam u32 delay_ms; 125519a81c14SSteve Longerbeam u16 reg_addr; 125619a81c14SSteve Longerbeam u8 mask, val; 125719a81c14SSteve Longerbeam int ret = 0; 125819a81c14SSteve Longerbeam 125919a81c14SSteve Longerbeam for (i = 0; i < mode->reg_data_size; ++i, ++regs) { 126019a81c14SSteve Longerbeam delay_ms = regs->delay_ms; 126119a81c14SSteve Longerbeam reg_addr = regs->reg_addr; 126219a81c14SSteve Longerbeam val = regs->val; 126319a81c14SSteve Longerbeam mask = regs->mask; 126419a81c14SSteve Longerbeam 12653b987d70SLad Prabhakar /* remain in power down mode for DVP */ 12663b987d70SLad Prabhakar if (regs->reg_addr == OV5640_REG_SYS_CTRL0 && 12673b987d70SLad Prabhakar val == OV5640_REG_SYS_CTRL0_SW_PWUP && 12688e823f5cSJacopo Mondi !ov5640_is_csi2(sensor)) 12693b987d70SLad Prabhakar continue; 12703b987d70SLad Prabhakar 127119a81c14SSteve Longerbeam if (mask) 127219a81c14SSteve Longerbeam ret = ov5640_mod_reg(sensor, reg_addr, mask, val); 127319a81c14SSteve Longerbeam else 127419a81c14SSteve Longerbeam ret = ov5640_write_reg(sensor, reg_addr, val); 127519a81c14SSteve Longerbeam if (ret) 127619a81c14SSteve Longerbeam break; 127719a81c14SSteve Longerbeam 127819a81c14SSteve Longerbeam if (delay_ms) 127919a81c14SSteve Longerbeam usleep_range(1000 * delay_ms, 1000 * delay_ms + 100); 128019a81c14SSteve Longerbeam } 128119a81c14SSteve Longerbeam 1282bad1774eSJacopo Mondi return ov5640_set_timings(sensor, mode); 128319a81c14SSteve Longerbeam } 128419a81c14SSteve Longerbeam 1285dc29a1c1SHugues Fruchet static int ov5640_set_autoexposure(struct ov5640_dev *sensor, bool on) 1286dc29a1c1SHugues Fruchet { 1287dc29a1c1SHugues Fruchet return ov5640_mod_reg(sensor, OV5640_REG_AEC_PK_MANUAL, 1288dc29a1c1SHugues Fruchet BIT(0), on ? 0 : BIT(0)); 1289dc29a1c1SHugues Fruchet } 1290dc29a1c1SHugues Fruchet 129119a81c14SSteve Longerbeam /* read exposure, in number of line periods */ 129219a81c14SSteve Longerbeam static int ov5640_get_exposure(struct ov5640_dev *sensor) 129319a81c14SSteve Longerbeam { 129419a81c14SSteve Longerbeam int exp, ret; 129519a81c14SSteve Longerbeam u8 temp; 129619a81c14SSteve Longerbeam 129719a81c14SSteve Longerbeam ret = ov5640_read_reg(sensor, OV5640_REG_AEC_PK_EXPOSURE_HI, &temp); 129819a81c14SSteve Longerbeam if (ret) 129919a81c14SSteve Longerbeam return ret; 130019a81c14SSteve Longerbeam exp = ((int)temp & 0x0f) << 16; 130119a81c14SSteve Longerbeam ret = ov5640_read_reg(sensor, OV5640_REG_AEC_PK_EXPOSURE_MED, &temp); 130219a81c14SSteve Longerbeam if (ret) 130319a81c14SSteve Longerbeam return ret; 130419a81c14SSteve Longerbeam exp |= ((int)temp << 8); 130519a81c14SSteve Longerbeam ret = ov5640_read_reg(sensor, OV5640_REG_AEC_PK_EXPOSURE_LO, &temp); 130619a81c14SSteve Longerbeam if (ret) 130719a81c14SSteve Longerbeam return ret; 130819a81c14SSteve Longerbeam exp |= (int)temp; 130919a81c14SSteve Longerbeam 131019a81c14SSteve Longerbeam return exp >> 4; 131119a81c14SSteve Longerbeam } 131219a81c14SSteve Longerbeam 131319a81c14SSteve Longerbeam /* write exposure, given number of line periods */ 131419a81c14SSteve Longerbeam static int ov5640_set_exposure(struct ov5640_dev *sensor, u32 exposure) 131519a81c14SSteve Longerbeam { 131619a81c14SSteve Longerbeam int ret; 131719a81c14SSteve Longerbeam 131819a81c14SSteve Longerbeam exposure <<= 4; 131919a81c14SSteve Longerbeam 132019a81c14SSteve Longerbeam ret = ov5640_write_reg(sensor, 132119a81c14SSteve Longerbeam OV5640_REG_AEC_PK_EXPOSURE_LO, 132219a81c14SSteve Longerbeam exposure & 0xff); 132319a81c14SSteve Longerbeam if (ret) 132419a81c14SSteve Longerbeam return ret; 132519a81c14SSteve Longerbeam ret = ov5640_write_reg(sensor, 132619a81c14SSteve Longerbeam OV5640_REG_AEC_PK_EXPOSURE_MED, 132719a81c14SSteve Longerbeam (exposure >> 8) & 0xff); 132819a81c14SSteve Longerbeam if (ret) 132919a81c14SSteve Longerbeam return ret; 133019a81c14SSteve Longerbeam return ov5640_write_reg(sensor, 133119a81c14SSteve Longerbeam OV5640_REG_AEC_PK_EXPOSURE_HI, 133219a81c14SSteve Longerbeam (exposure >> 16) & 0x0f); 133319a81c14SSteve Longerbeam } 133419a81c14SSteve Longerbeam 133519a81c14SSteve Longerbeam static int ov5640_get_gain(struct ov5640_dev *sensor) 133619a81c14SSteve Longerbeam { 133719a81c14SSteve Longerbeam u16 gain; 133819a81c14SSteve Longerbeam int ret; 133919a81c14SSteve Longerbeam 134019a81c14SSteve Longerbeam ret = ov5640_read_reg16(sensor, OV5640_REG_AEC_PK_REAL_GAIN, &gain); 134119a81c14SSteve Longerbeam if (ret) 134219a81c14SSteve Longerbeam return ret; 134319a81c14SSteve Longerbeam 134419a81c14SSteve Longerbeam return gain & 0x3ff; 134519a81c14SSteve Longerbeam } 134619a81c14SSteve Longerbeam 13473cca8ef5SHugues Fruchet static int ov5640_set_gain(struct ov5640_dev *sensor, int gain) 13483cca8ef5SHugues Fruchet { 13493cca8ef5SHugues Fruchet return ov5640_write_reg16(sensor, OV5640_REG_AEC_PK_REAL_GAIN, 13503cca8ef5SHugues Fruchet (u16)gain & 0x3ff); 13513cca8ef5SHugues Fruchet } 13523cca8ef5SHugues Fruchet 13533cca8ef5SHugues Fruchet static int ov5640_set_autogain(struct ov5640_dev *sensor, bool on) 13543cca8ef5SHugues Fruchet { 13553cca8ef5SHugues Fruchet return ov5640_mod_reg(sensor, OV5640_REG_AEC_PK_MANUAL, 13563cca8ef5SHugues Fruchet BIT(1), on ? 0 : BIT(1)); 13573cca8ef5SHugues Fruchet } 13583cca8ef5SHugues Fruchet 1359f22996dbSHugues Fruchet static int ov5640_set_stream_dvp(struct ov5640_dev *sensor, bool on) 1360f22996dbSHugues Fruchet { 13613b987d70SLad Prabhakar return ov5640_write_reg(sensor, OV5640_REG_SYS_CTRL0, on ? 13623b987d70SLad Prabhakar OV5640_REG_SYS_CTRL0_SW_PWUP : 13633b987d70SLad Prabhakar OV5640_REG_SYS_CTRL0_SW_PWDN); 1364f22996dbSHugues Fruchet } 1365f22996dbSHugues Fruchet 1366f22996dbSHugues Fruchet static int ov5640_set_stream_mipi(struct ov5640_dev *sensor, bool on) 136719a81c14SSteve Longerbeam { 136819a81c14SSteve Longerbeam int ret; 136919a81c14SSteve Longerbeam 1370aa4bb8b8SJacopo Mondi /* 1371aa4bb8b8SJacopo Mondi * Enable/disable the MIPI interface 1372aa4bb8b8SJacopo Mondi * 1373aa4bb8b8SJacopo Mondi * 0x300e = on ? 0x45 : 0x40 1374aa4bb8b8SJacopo Mondi * 1375aa4bb8b8SJacopo Mondi * FIXME: the sensor manual (version 2.03) reports 1376aa4bb8b8SJacopo Mondi * [7:5] = 000 : 1 data lane mode 1377aa4bb8b8SJacopo Mondi * [7:5] = 001 : 2 data lanes mode 1378aa4bb8b8SJacopo Mondi * But this settings do not work, while the following ones 1379aa4bb8b8SJacopo Mondi * have been validated for 2 data lanes mode. 1380aa4bb8b8SJacopo Mondi * 1381aa4bb8b8SJacopo Mondi * [7:5] = 010 : 2 data lanes mode 1382aa4bb8b8SJacopo Mondi * [4] = 0 : Power up MIPI HS Tx 1383aa4bb8b8SJacopo Mondi * [3] = 0 : Power up MIPI LS Rx 1384aa4bb8b8SJacopo Mondi * [2] = 1/0 : MIPI interface enable/disable 1385aa4bb8b8SJacopo Mondi * [1:0] = 01/00: FIXME: 'debug' 1386aa4bb8b8SJacopo Mondi */ 1387aa4bb8b8SJacopo Mondi ret = ov5640_write_reg(sensor, OV5640_REG_IO_MIPI_CTRL00, 1388aa4bb8b8SJacopo Mondi on ? 0x45 : 0x40); 138919a81c14SSteve Longerbeam if (ret) 139019a81c14SSteve Longerbeam return ret; 139119a81c14SSteve Longerbeam 139219a81c14SSteve Longerbeam return ov5640_write_reg(sensor, OV5640_REG_FRAME_CTRL01, 139319a81c14SSteve Longerbeam on ? 0x00 : 0x0f); 139419a81c14SSteve Longerbeam } 139519a81c14SSteve Longerbeam 139619a81c14SSteve Longerbeam static int ov5640_get_sysclk(struct ov5640_dev *sensor) 139719a81c14SSteve Longerbeam { 139819a81c14SSteve Longerbeam /* calculate sysclk */ 139919a81c14SSteve Longerbeam u32 xvclk = sensor->xclk_freq / 10000; 140019a81c14SSteve Longerbeam u32 multiplier, prediv, VCO, sysdiv, pll_rdiv; 140119a81c14SSteve Longerbeam u32 sclk_rdiv_map[] = {1, 2, 4, 8}; 140219a81c14SSteve Longerbeam u32 bit_div2x = 1, sclk_rdiv, sysclk; 140319a81c14SSteve Longerbeam u8 temp1, temp2; 140419a81c14SSteve Longerbeam int ret; 140519a81c14SSteve Longerbeam 140619a81c14SSteve Longerbeam ret = ov5640_read_reg(sensor, OV5640_REG_SC_PLL_CTRL0, &temp1); 140719a81c14SSteve Longerbeam if (ret) 140819a81c14SSteve Longerbeam return ret; 140919a81c14SSteve Longerbeam temp2 = temp1 & 0x0f; 141019a81c14SSteve Longerbeam if (temp2 == 8 || temp2 == 10) 141119a81c14SSteve Longerbeam bit_div2x = temp2 / 2; 141219a81c14SSteve Longerbeam 141319a81c14SSteve Longerbeam ret = ov5640_read_reg(sensor, OV5640_REG_SC_PLL_CTRL1, &temp1); 141419a81c14SSteve Longerbeam if (ret) 141519a81c14SSteve Longerbeam return ret; 141619a81c14SSteve Longerbeam sysdiv = temp1 >> 4; 141719a81c14SSteve Longerbeam if (sysdiv == 0) 141819a81c14SSteve Longerbeam sysdiv = 16; 141919a81c14SSteve Longerbeam 142019a81c14SSteve Longerbeam ret = ov5640_read_reg(sensor, OV5640_REG_SC_PLL_CTRL2, &temp1); 142119a81c14SSteve Longerbeam if (ret) 142219a81c14SSteve Longerbeam return ret; 142319a81c14SSteve Longerbeam multiplier = temp1; 142419a81c14SSteve Longerbeam 142519a81c14SSteve Longerbeam ret = ov5640_read_reg(sensor, OV5640_REG_SC_PLL_CTRL3, &temp1); 142619a81c14SSteve Longerbeam if (ret) 142719a81c14SSteve Longerbeam return ret; 142819a81c14SSteve Longerbeam prediv = temp1 & 0x0f; 142919a81c14SSteve Longerbeam pll_rdiv = ((temp1 >> 4) & 0x01) + 1; 143019a81c14SSteve Longerbeam 143119a81c14SSteve Longerbeam ret = ov5640_read_reg(sensor, OV5640_REG_SYS_ROOT_DIVIDER, &temp1); 143219a81c14SSteve Longerbeam if (ret) 143319a81c14SSteve Longerbeam return ret; 143419a81c14SSteve Longerbeam temp2 = temp1 & 0x03; 143519a81c14SSteve Longerbeam sclk_rdiv = sclk_rdiv_map[temp2]; 143619a81c14SSteve Longerbeam 143719a81c14SSteve Longerbeam if (!prediv || !sysdiv || !pll_rdiv || !bit_div2x) 143819a81c14SSteve Longerbeam return -EINVAL; 143919a81c14SSteve Longerbeam 144019a81c14SSteve Longerbeam VCO = xvclk * multiplier / prediv; 144119a81c14SSteve Longerbeam 144219a81c14SSteve Longerbeam sysclk = VCO / sysdiv / pll_rdiv * 2 / bit_div2x / sclk_rdiv; 144319a81c14SSteve Longerbeam 144419a81c14SSteve Longerbeam return sysclk; 144519a81c14SSteve Longerbeam } 144619a81c14SSteve Longerbeam 144719a81c14SSteve Longerbeam static int ov5640_set_night_mode(struct ov5640_dev *sensor) 144819a81c14SSteve Longerbeam { 144919a81c14SSteve Longerbeam /* read HTS from register settings */ 145019a81c14SSteve Longerbeam u8 mode; 145119a81c14SSteve Longerbeam int ret; 145219a81c14SSteve Longerbeam 145319a81c14SSteve Longerbeam ret = ov5640_read_reg(sensor, OV5640_REG_AEC_CTRL00, &mode); 145419a81c14SSteve Longerbeam if (ret) 145519a81c14SSteve Longerbeam return ret; 145619a81c14SSteve Longerbeam mode &= 0xfb; 145719a81c14SSteve Longerbeam return ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL00, mode); 145819a81c14SSteve Longerbeam } 145919a81c14SSteve Longerbeam 146019a81c14SSteve Longerbeam static int ov5640_get_hts(struct ov5640_dev *sensor) 146119a81c14SSteve Longerbeam { 146219a81c14SSteve Longerbeam /* read HTS from register settings */ 146319a81c14SSteve Longerbeam u16 hts; 146419a81c14SSteve Longerbeam int ret; 146519a81c14SSteve Longerbeam 146619a81c14SSteve Longerbeam ret = ov5640_read_reg16(sensor, OV5640_REG_TIMING_HTS, &hts); 146719a81c14SSteve Longerbeam if (ret) 146819a81c14SSteve Longerbeam return ret; 146919a81c14SSteve Longerbeam return hts; 147019a81c14SSteve Longerbeam } 147119a81c14SSteve Longerbeam 147219a81c14SSteve Longerbeam static int ov5640_get_vts(struct ov5640_dev *sensor) 147319a81c14SSteve Longerbeam { 147419a81c14SSteve Longerbeam u16 vts; 147519a81c14SSteve Longerbeam int ret; 147619a81c14SSteve Longerbeam 147719a81c14SSteve Longerbeam ret = ov5640_read_reg16(sensor, OV5640_REG_TIMING_VTS, &vts); 147819a81c14SSteve Longerbeam if (ret) 147919a81c14SSteve Longerbeam return ret; 148019a81c14SSteve Longerbeam return vts; 148119a81c14SSteve Longerbeam } 148219a81c14SSteve Longerbeam 148319a81c14SSteve Longerbeam static int ov5640_set_vts(struct ov5640_dev *sensor, int vts) 148419a81c14SSteve Longerbeam { 148519a81c14SSteve Longerbeam return ov5640_write_reg16(sensor, OV5640_REG_TIMING_VTS, vts); 148619a81c14SSteve Longerbeam } 148719a81c14SSteve Longerbeam 148819a81c14SSteve Longerbeam static int ov5640_get_light_freq(struct ov5640_dev *sensor) 148919a81c14SSteve Longerbeam { 149019a81c14SSteve Longerbeam /* get banding filter value */ 149119a81c14SSteve Longerbeam int ret, light_freq = 0; 149219a81c14SSteve Longerbeam u8 temp, temp1; 149319a81c14SSteve Longerbeam 149419a81c14SSteve Longerbeam ret = ov5640_read_reg(sensor, OV5640_REG_HZ5060_CTRL01, &temp); 149519a81c14SSteve Longerbeam if (ret) 149619a81c14SSteve Longerbeam return ret; 149719a81c14SSteve Longerbeam 149819a81c14SSteve Longerbeam if (temp & 0x80) { 149919a81c14SSteve Longerbeam /* manual */ 150019a81c14SSteve Longerbeam ret = ov5640_read_reg(sensor, OV5640_REG_HZ5060_CTRL00, 150119a81c14SSteve Longerbeam &temp1); 150219a81c14SSteve Longerbeam if (ret) 150319a81c14SSteve Longerbeam return ret; 150419a81c14SSteve Longerbeam if (temp1 & 0x04) { 150519a81c14SSteve Longerbeam /* 50Hz */ 150619a81c14SSteve Longerbeam light_freq = 50; 150719a81c14SSteve Longerbeam } else { 150819a81c14SSteve Longerbeam /* 60Hz */ 150919a81c14SSteve Longerbeam light_freq = 60; 151019a81c14SSteve Longerbeam } 151119a81c14SSteve Longerbeam } else { 151219a81c14SSteve Longerbeam /* auto */ 151319a81c14SSteve Longerbeam ret = ov5640_read_reg(sensor, OV5640_REG_SIGMADELTA_CTRL0C, 151419a81c14SSteve Longerbeam &temp1); 151519a81c14SSteve Longerbeam if (ret) 151619a81c14SSteve Longerbeam return ret; 151719a81c14SSteve Longerbeam 151819a81c14SSteve Longerbeam if (temp1 & 0x01) { 151919a81c14SSteve Longerbeam /* 50Hz */ 152019a81c14SSteve Longerbeam light_freq = 50; 152119a81c14SSteve Longerbeam } else { 152219a81c14SSteve Longerbeam /* 60Hz */ 152319a81c14SSteve Longerbeam } 152419a81c14SSteve Longerbeam } 152519a81c14SSteve Longerbeam 152619a81c14SSteve Longerbeam return light_freq; 152719a81c14SSteve Longerbeam } 152819a81c14SSteve Longerbeam 152919a81c14SSteve Longerbeam static int ov5640_set_bandingfilter(struct ov5640_dev *sensor) 153019a81c14SSteve Longerbeam { 153119a81c14SSteve Longerbeam u32 band_step60, max_band60, band_step50, max_band50, prev_vts; 153219a81c14SSteve Longerbeam int ret; 153319a81c14SSteve Longerbeam 153419a81c14SSteve Longerbeam /* read preview PCLK */ 153519a81c14SSteve Longerbeam ret = ov5640_get_sysclk(sensor); 153619a81c14SSteve Longerbeam if (ret < 0) 153719a81c14SSteve Longerbeam return ret; 153819a81c14SSteve Longerbeam if (ret == 0) 153919a81c14SSteve Longerbeam return -EINVAL; 154019a81c14SSteve Longerbeam sensor->prev_sysclk = ret; 154119a81c14SSteve Longerbeam /* read preview HTS */ 154219a81c14SSteve Longerbeam ret = ov5640_get_hts(sensor); 154319a81c14SSteve Longerbeam if (ret < 0) 154419a81c14SSteve Longerbeam return ret; 154519a81c14SSteve Longerbeam if (ret == 0) 154619a81c14SSteve Longerbeam return -EINVAL; 154719a81c14SSteve Longerbeam sensor->prev_hts = ret; 154819a81c14SSteve Longerbeam 154919a81c14SSteve Longerbeam /* read preview VTS */ 155019a81c14SSteve Longerbeam ret = ov5640_get_vts(sensor); 155119a81c14SSteve Longerbeam if (ret < 0) 155219a81c14SSteve Longerbeam return ret; 155319a81c14SSteve Longerbeam prev_vts = ret; 155419a81c14SSteve Longerbeam 155519a81c14SSteve Longerbeam /* calculate banding filter */ 155619a81c14SSteve Longerbeam /* 60Hz */ 155719a81c14SSteve Longerbeam band_step60 = sensor->prev_sysclk * 100 / sensor->prev_hts * 100 / 120; 155819a81c14SSteve Longerbeam ret = ov5640_write_reg16(sensor, OV5640_REG_AEC_B60_STEP, band_step60); 155919a81c14SSteve Longerbeam if (ret) 156019a81c14SSteve Longerbeam return ret; 156119a81c14SSteve Longerbeam if (!band_step60) 156219a81c14SSteve Longerbeam return -EINVAL; 156319a81c14SSteve Longerbeam max_band60 = (int)((prev_vts - 4) / band_step60); 156419a81c14SSteve Longerbeam ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL0D, max_band60); 156519a81c14SSteve Longerbeam if (ret) 156619a81c14SSteve Longerbeam return ret; 156719a81c14SSteve Longerbeam 156819a81c14SSteve Longerbeam /* 50Hz */ 156919a81c14SSteve Longerbeam band_step50 = sensor->prev_sysclk * 100 / sensor->prev_hts; 157019a81c14SSteve Longerbeam ret = ov5640_write_reg16(sensor, OV5640_REG_AEC_B50_STEP, band_step50); 157119a81c14SSteve Longerbeam if (ret) 157219a81c14SSteve Longerbeam return ret; 157319a81c14SSteve Longerbeam if (!band_step50) 157419a81c14SSteve Longerbeam return -EINVAL; 157519a81c14SSteve Longerbeam max_band50 = (int)((prev_vts - 4) / band_step50); 157619a81c14SSteve Longerbeam return ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL0E, max_band50); 157719a81c14SSteve Longerbeam } 157819a81c14SSteve Longerbeam 157919a81c14SSteve Longerbeam static int ov5640_set_ae_target(struct ov5640_dev *sensor, int target) 158019a81c14SSteve Longerbeam { 158119a81c14SSteve Longerbeam /* stable in high */ 158219a81c14SSteve Longerbeam u32 fast_high, fast_low; 158319a81c14SSteve Longerbeam int ret; 158419a81c14SSteve Longerbeam 158519a81c14SSteve Longerbeam sensor->ae_low = target * 23 / 25; /* 0.92 */ 158619a81c14SSteve Longerbeam sensor->ae_high = target * 27 / 25; /* 1.08 */ 158719a81c14SSteve Longerbeam 158819a81c14SSteve Longerbeam fast_high = sensor->ae_high << 1; 158919a81c14SSteve Longerbeam if (fast_high > 255) 159019a81c14SSteve Longerbeam fast_high = 255; 159119a81c14SSteve Longerbeam 159219a81c14SSteve Longerbeam fast_low = sensor->ae_low >> 1; 159319a81c14SSteve Longerbeam 159419a81c14SSteve Longerbeam ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL0F, sensor->ae_high); 159519a81c14SSteve Longerbeam if (ret) 159619a81c14SSteve Longerbeam return ret; 159719a81c14SSteve Longerbeam ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL10, sensor->ae_low); 159819a81c14SSteve Longerbeam if (ret) 159919a81c14SSteve Longerbeam return ret; 160019a81c14SSteve Longerbeam ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL1B, sensor->ae_high); 160119a81c14SSteve Longerbeam if (ret) 160219a81c14SSteve Longerbeam return ret; 160319a81c14SSteve Longerbeam ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL1E, sensor->ae_low); 160419a81c14SSteve Longerbeam if (ret) 160519a81c14SSteve Longerbeam return ret; 160619a81c14SSteve Longerbeam ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL11, fast_high); 160719a81c14SSteve Longerbeam if (ret) 160819a81c14SSteve Longerbeam return ret; 160919a81c14SSteve Longerbeam return ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL1F, fast_low); 161019a81c14SSteve Longerbeam } 161119a81c14SSteve Longerbeam 1612c2c3f42dSHugues Fruchet static int ov5640_get_binning(struct ov5640_dev *sensor) 161319a81c14SSteve Longerbeam { 161419a81c14SSteve Longerbeam u8 temp; 161519a81c14SSteve Longerbeam int ret; 161619a81c14SSteve Longerbeam 161719a81c14SSteve Longerbeam ret = ov5640_read_reg(sensor, OV5640_REG_TIMING_TC_REG21, &temp); 161819a81c14SSteve Longerbeam if (ret) 161919a81c14SSteve Longerbeam return ret; 1620c2c3f42dSHugues Fruchet 1621c2c3f42dSHugues Fruchet return temp & BIT(0); 162219a81c14SSteve Longerbeam } 162319a81c14SSteve Longerbeam 1624ce85705aSHugues Fruchet static int ov5640_set_binning(struct ov5640_dev *sensor, bool enable) 1625ce85705aSHugues Fruchet { 1626ce85705aSHugues Fruchet int ret; 1627ce85705aSHugues Fruchet 1628ce85705aSHugues Fruchet /* 1629ce85705aSHugues Fruchet * TIMING TC REG21: 1630ce85705aSHugues Fruchet * - [0]: Horizontal binning enable 1631ce85705aSHugues Fruchet */ 1632ce85705aSHugues Fruchet ret = ov5640_mod_reg(sensor, OV5640_REG_TIMING_TC_REG21, 1633ce85705aSHugues Fruchet BIT(0), enable ? BIT(0) : 0); 1634ce85705aSHugues Fruchet if (ret) 1635ce85705aSHugues Fruchet return ret; 1636ce85705aSHugues Fruchet /* 1637ce85705aSHugues Fruchet * TIMING TC REG20: 1638ce85705aSHugues Fruchet * - [0]: Undocumented, but hardcoded init sequences 1639ce85705aSHugues Fruchet * are always setting REG21/REG20 bit 0 to same value... 1640ce85705aSHugues Fruchet */ 1641ce85705aSHugues Fruchet return ov5640_mod_reg(sensor, OV5640_REG_TIMING_TC_REG20, 1642ce85705aSHugues Fruchet BIT(0), enable ? BIT(0) : 0); 1643ce85705aSHugues Fruchet } 1644ce85705aSHugues Fruchet 164519a81c14SSteve Longerbeam static int ov5640_set_virtual_channel(struct ov5640_dev *sensor) 164619a81c14SSteve Longerbeam { 16478670d70aSHugues Fruchet struct i2c_client *client = sensor->i2c_client; 164819a81c14SSteve Longerbeam u8 temp, channel = virtual_channel; 164919a81c14SSteve Longerbeam int ret; 165019a81c14SSteve Longerbeam 16518670d70aSHugues Fruchet if (channel > 3) { 16528670d70aSHugues Fruchet dev_err(&client->dev, 16538670d70aSHugues Fruchet "%s: wrong virtual_channel parameter, expected (0..3), got %d\n", 16548670d70aSHugues Fruchet __func__, channel); 165519a81c14SSteve Longerbeam return -EINVAL; 16568670d70aSHugues Fruchet } 165719a81c14SSteve Longerbeam 165819a81c14SSteve Longerbeam ret = ov5640_read_reg(sensor, OV5640_REG_DEBUG_MODE, &temp); 165919a81c14SSteve Longerbeam if (ret) 166019a81c14SSteve Longerbeam return ret; 166119a81c14SSteve Longerbeam temp &= ~(3 << 6); 166219a81c14SSteve Longerbeam temp |= (channel << 6); 166319a81c14SSteve Longerbeam return ov5640_write_reg(sensor, OV5640_REG_DEBUG_MODE, temp); 166419a81c14SSteve Longerbeam } 166519a81c14SSteve Longerbeam 166619a81c14SSteve Longerbeam static const struct ov5640_mode_info * 166719a81c14SSteve Longerbeam ov5640_find_mode(struct ov5640_dev *sensor, enum ov5640_frame_rate fr, 166819a81c14SSteve Longerbeam int width, int height, bool nearest) 166919a81c14SSteve Longerbeam { 16703c4a7372SHugues Fruchet const struct ov5640_mode_info *mode; 167119a81c14SSteve Longerbeam 1672086c25f8SMaxime Ripard mode = v4l2_find_nearest_size(ov5640_mode_data, 1673086c25f8SMaxime Ripard ARRAY_SIZE(ov5640_mode_data), 16743c4a7372SHugues Fruchet hact, vact, 16753c4a7372SHugues Fruchet width, height); 167619a81c14SSteve Longerbeam 16773c4a7372SHugues Fruchet if (!mode || 16783c4a7372SHugues Fruchet (!nearest && (mode->hact != width || mode->vact != height))) 16793c4a7372SHugues Fruchet return NULL; 168019a81c14SSteve Longerbeam 16815554c80eSAdam Ford /* Check to see if the current mode exceeds the max frame rate */ 16825554c80eSAdam Ford if (ov5640_framerates[fr] > ov5640_framerates[mode->max_fps]) 1683981e4454SBenoit Parrot return NULL; 1684981e4454SBenoit Parrot 168519a81c14SSteve Longerbeam return mode; 168619a81c14SSteve Longerbeam } 168719a81c14SSteve Longerbeam 1688cc196e48SBenoit Parrot static u64 ov5640_calc_pixel_rate(struct ov5640_dev *sensor) 1689cc196e48SBenoit Parrot { 1690cc196e48SBenoit Parrot u64 rate; 1691cc196e48SBenoit Parrot 1692cc196e48SBenoit Parrot rate = sensor->current_mode->vtot * sensor->current_mode->htot; 1693cc196e48SBenoit Parrot rate *= ov5640_framerates[sensor->current_fr]; 1694cc196e48SBenoit Parrot 1695cc196e48SBenoit Parrot return rate; 1696cc196e48SBenoit Parrot } 1697cc196e48SBenoit Parrot 169819a81c14SSteve Longerbeam /* 169919a81c14SSteve Longerbeam * sensor changes between scaling and subsampling, go through 170019a81c14SSteve Longerbeam * exposure calculation 170119a81c14SSteve Longerbeam */ 170241d8d7f5SHugues Fruchet static int ov5640_set_mode_exposure_calc(struct ov5640_dev *sensor, 170341d8d7f5SHugues Fruchet const struct ov5640_mode_info *mode) 170419a81c14SSteve Longerbeam { 170519a81c14SSteve Longerbeam u32 prev_shutter, prev_gain16; 170619a81c14SSteve Longerbeam u32 cap_shutter, cap_gain16; 170719a81c14SSteve Longerbeam u32 cap_sysclk, cap_hts, cap_vts; 170819a81c14SSteve Longerbeam u32 light_freq, cap_bandfilt, cap_maxband; 170919a81c14SSteve Longerbeam u32 cap_gain16_shutter; 171019a81c14SSteve Longerbeam u8 average; 171119a81c14SSteve Longerbeam int ret; 171219a81c14SSteve Longerbeam 171341d8d7f5SHugues Fruchet if (!mode->reg_data) 171419a81c14SSteve Longerbeam return -EINVAL; 171519a81c14SSteve Longerbeam 171619a81c14SSteve Longerbeam /* read preview shutter */ 171719a81c14SSteve Longerbeam ret = ov5640_get_exposure(sensor); 171819a81c14SSteve Longerbeam if (ret < 0) 171919a81c14SSteve Longerbeam return ret; 172019a81c14SSteve Longerbeam prev_shutter = ret; 1721c2c3f42dSHugues Fruchet ret = ov5640_get_binning(sensor); 172219a81c14SSteve Longerbeam if (ret < 0) 172319a81c14SSteve Longerbeam return ret; 172419a81c14SSteve Longerbeam if (ret && mode->id != OV5640_MODE_720P_1280_720 && 172519a81c14SSteve Longerbeam mode->id != OV5640_MODE_1080P_1920_1080) 172619a81c14SSteve Longerbeam prev_shutter *= 2; 172719a81c14SSteve Longerbeam 172819a81c14SSteve Longerbeam /* read preview gain */ 172919a81c14SSteve Longerbeam ret = ov5640_get_gain(sensor); 173019a81c14SSteve Longerbeam if (ret < 0) 173119a81c14SSteve Longerbeam return ret; 173219a81c14SSteve Longerbeam prev_gain16 = ret; 173319a81c14SSteve Longerbeam 173419a81c14SSteve Longerbeam /* get average */ 173519a81c14SSteve Longerbeam ret = ov5640_read_reg(sensor, OV5640_REG_AVG_READOUT, &average); 173619a81c14SSteve Longerbeam if (ret) 173719a81c14SSteve Longerbeam return ret; 173819a81c14SSteve Longerbeam 173919a81c14SSteve Longerbeam /* turn off night mode for capture */ 174019a81c14SSteve Longerbeam ret = ov5640_set_night_mode(sensor); 174119a81c14SSteve Longerbeam if (ret < 0) 174219a81c14SSteve Longerbeam return ret; 174319a81c14SSteve Longerbeam 174419a81c14SSteve Longerbeam /* Write capture setting */ 174519a81c14SSteve Longerbeam ret = ov5640_load_regs(sensor, mode); 174619a81c14SSteve Longerbeam if (ret < 0) 174719a81c14SSteve Longerbeam return ret; 174819a81c14SSteve Longerbeam 174919a81c14SSteve Longerbeam /* read capture VTS */ 175019a81c14SSteve Longerbeam ret = ov5640_get_vts(sensor); 175119a81c14SSteve Longerbeam if (ret < 0) 175219a81c14SSteve Longerbeam return ret; 175319a81c14SSteve Longerbeam cap_vts = ret; 175419a81c14SSteve Longerbeam ret = ov5640_get_hts(sensor); 175519a81c14SSteve Longerbeam if (ret < 0) 175619a81c14SSteve Longerbeam return ret; 175719a81c14SSteve Longerbeam if (ret == 0) 175819a81c14SSteve Longerbeam return -EINVAL; 175919a81c14SSteve Longerbeam cap_hts = ret; 176019a81c14SSteve Longerbeam 176119a81c14SSteve Longerbeam ret = ov5640_get_sysclk(sensor); 176219a81c14SSteve Longerbeam if (ret < 0) 176319a81c14SSteve Longerbeam return ret; 176419a81c14SSteve Longerbeam if (ret == 0) 176519a81c14SSteve Longerbeam return -EINVAL; 176619a81c14SSteve Longerbeam cap_sysclk = ret; 176719a81c14SSteve Longerbeam 176819a81c14SSteve Longerbeam /* calculate capture banding filter */ 176919a81c14SSteve Longerbeam ret = ov5640_get_light_freq(sensor); 177019a81c14SSteve Longerbeam if (ret < 0) 177119a81c14SSteve Longerbeam return ret; 177219a81c14SSteve Longerbeam light_freq = ret; 177319a81c14SSteve Longerbeam 177419a81c14SSteve Longerbeam if (light_freq == 60) { 177519a81c14SSteve Longerbeam /* 60Hz */ 177619a81c14SSteve Longerbeam cap_bandfilt = cap_sysclk * 100 / cap_hts * 100 / 120; 177719a81c14SSteve Longerbeam } else { 177819a81c14SSteve Longerbeam /* 50Hz */ 177919a81c14SSteve Longerbeam cap_bandfilt = cap_sysclk * 100 / cap_hts; 178019a81c14SSteve Longerbeam } 178119a81c14SSteve Longerbeam 178219a81c14SSteve Longerbeam if (!sensor->prev_sysclk) { 178319a81c14SSteve Longerbeam ret = ov5640_get_sysclk(sensor); 178419a81c14SSteve Longerbeam if (ret < 0) 178519a81c14SSteve Longerbeam return ret; 178619a81c14SSteve Longerbeam if (ret == 0) 178719a81c14SSteve Longerbeam return -EINVAL; 178819a81c14SSteve Longerbeam sensor->prev_sysclk = ret; 178919a81c14SSteve Longerbeam } 179019a81c14SSteve Longerbeam 179119a81c14SSteve Longerbeam if (!cap_bandfilt) 179219a81c14SSteve Longerbeam return -EINVAL; 179319a81c14SSteve Longerbeam 179419a81c14SSteve Longerbeam cap_maxband = (int)((cap_vts - 4) / cap_bandfilt); 179519a81c14SSteve Longerbeam 179619a81c14SSteve Longerbeam /* calculate capture shutter/gain16 */ 179719a81c14SSteve Longerbeam if (average > sensor->ae_low && average < sensor->ae_high) { 179819a81c14SSteve Longerbeam /* in stable range */ 179919a81c14SSteve Longerbeam cap_gain16_shutter = 180019a81c14SSteve Longerbeam prev_gain16 * prev_shutter * 180119a81c14SSteve Longerbeam cap_sysclk / sensor->prev_sysclk * 180219a81c14SSteve Longerbeam sensor->prev_hts / cap_hts * 180319a81c14SSteve Longerbeam sensor->ae_target / average; 180419a81c14SSteve Longerbeam } else { 180519a81c14SSteve Longerbeam cap_gain16_shutter = 180619a81c14SSteve Longerbeam prev_gain16 * prev_shutter * 180719a81c14SSteve Longerbeam cap_sysclk / sensor->prev_sysclk * 180819a81c14SSteve Longerbeam sensor->prev_hts / cap_hts; 180919a81c14SSteve Longerbeam } 181019a81c14SSteve Longerbeam 181119a81c14SSteve Longerbeam /* gain to shutter */ 181219a81c14SSteve Longerbeam if (cap_gain16_shutter < (cap_bandfilt * 16)) { 181319a81c14SSteve Longerbeam /* shutter < 1/100 */ 181419a81c14SSteve Longerbeam cap_shutter = cap_gain16_shutter / 16; 181519a81c14SSteve Longerbeam if (cap_shutter < 1) 181619a81c14SSteve Longerbeam cap_shutter = 1; 181719a81c14SSteve Longerbeam 181819a81c14SSteve Longerbeam cap_gain16 = cap_gain16_shutter / cap_shutter; 181919a81c14SSteve Longerbeam if (cap_gain16 < 16) 182019a81c14SSteve Longerbeam cap_gain16 = 16; 182119a81c14SSteve Longerbeam } else { 182219a81c14SSteve Longerbeam if (cap_gain16_shutter > (cap_bandfilt * cap_maxband * 16)) { 182319a81c14SSteve Longerbeam /* exposure reach max */ 182419a81c14SSteve Longerbeam cap_shutter = cap_bandfilt * cap_maxband; 182519a81c14SSteve Longerbeam if (!cap_shutter) 182619a81c14SSteve Longerbeam return -EINVAL; 182719a81c14SSteve Longerbeam 182819a81c14SSteve Longerbeam cap_gain16 = cap_gain16_shutter / cap_shutter; 182919a81c14SSteve Longerbeam } else { 183019a81c14SSteve Longerbeam /* 1/100 < (cap_shutter = n/100) =< max */ 183119a81c14SSteve Longerbeam cap_shutter = 183219a81c14SSteve Longerbeam ((int)(cap_gain16_shutter / 16 / cap_bandfilt)) 183319a81c14SSteve Longerbeam * cap_bandfilt; 183419a81c14SSteve Longerbeam if (!cap_shutter) 183519a81c14SSteve Longerbeam return -EINVAL; 183619a81c14SSteve Longerbeam 183719a81c14SSteve Longerbeam cap_gain16 = cap_gain16_shutter / cap_shutter; 183819a81c14SSteve Longerbeam } 183919a81c14SSteve Longerbeam } 184019a81c14SSteve Longerbeam 184119a81c14SSteve Longerbeam /* set capture gain */ 18423cca8ef5SHugues Fruchet ret = ov5640_set_gain(sensor, cap_gain16); 184319a81c14SSteve Longerbeam if (ret) 184419a81c14SSteve Longerbeam return ret; 184519a81c14SSteve Longerbeam 184619a81c14SSteve Longerbeam /* write capture shutter */ 184719a81c14SSteve Longerbeam if (cap_shutter > (cap_vts - 4)) { 184819a81c14SSteve Longerbeam cap_vts = cap_shutter + 4; 184919a81c14SSteve Longerbeam ret = ov5640_set_vts(sensor, cap_vts); 185019a81c14SSteve Longerbeam if (ret < 0) 185119a81c14SSteve Longerbeam return ret; 185219a81c14SSteve Longerbeam } 185319a81c14SSteve Longerbeam 185419a81c14SSteve Longerbeam /* set exposure */ 18553cca8ef5SHugues Fruchet return ov5640_set_exposure(sensor, cap_shutter); 185619a81c14SSteve Longerbeam } 185719a81c14SSteve Longerbeam 185819a81c14SSteve Longerbeam /* 185919a81c14SSteve Longerbeam * if sensor changes inside scaling or subsampling 186019a81c14SSteve Longerbeam * change mode directly 186119a81c14SSteve Longerbeam */ 186219a81c14SSteve Longerbeam static int ov5640_set_mode_direct(struct ov5640_dev *sensor, 18633cca8ef5SHugues Fruchet const struct ov5640_mode_info *mode) 186419a81c14SSteve Longerbeam { 186541d8d7f5SHugues Fruchet if (!mode->reg_data) 186619a81c14SSteve Longerbeam return -EINVAL; 186719a81c14SSteve Longerbeam 186819a81c14SSteve Longerbeam /* Write capture setting */ 18693cca8ef5SHugues Fruchet return ov5640_load_regs(sensor, mode); 187019a81c14SSteve Longerbeam } 187119a81c14SSteve Longerbeam 1872985cdcb0SHugues Fruchet static int ov5640_set_mode(struct ov5640_dev *sensor) 187319a81c14SSteve Longerbeam { 187419a81c14SSteve Longerbeam const struct ov5640_mode_info *mode = sensor->current_mode; 1875985cdcb0SHugues Fruchet const struct ov5640_mode_info *orig_mode = sensor->last_mode; 187619a81c14SSteve Longerbeam enum ov5640_downsize_mode dn_mode, orig_dn_mode; 18773cca8ef5SHugues Fruchet bool auto_gain = sensor->ctrls.auto_gain->val == 1; 1878dc29a1c1SHugues Fruchet bool auto_exp = sensor->ctrls.auto_exp->val == V4L2_EXPOSURE_AUTO; 1879aa288248SMaxime Ripard unsigned long rate; 188019a81c14SSteve Longerbeam int ret; 188119a81c14SSteve Longerbeam 188219a81c14SSteve Longerbeam dn_mode = mode->dn_mode; 188319a81c14SSteve Longerbeam orig_dn_mode = orig_mode->dn_mode; 188419a81c14SSteve Longerbeam 188519a81c14SSteve Longerbeam /* auto gain and exposure must be turned off when changing modes */ 18863cca8ef5SHugues Fruchet if (auto_gain) { 18873cca8ef5SHugues Fruchet ret = ov5640_set_autogain(sensor, false); 188819a81c14SSteve Longerbeam if (ret) 188919a81c14SSteve Longerbeam return ret; 18903cca8ef5SHugues Fruchet } 1891bf4a4b51SMaxime Ripard 18923cca8ef5SHugues Fruchet if (auto_exp) { 1893dc29a1c1SHugues Fruchet ret = ov5640_set_autoexposure(sensor, false); 189419a81c14SSteve Longerbeam if (ret) 18953cca8ef5SHugues Fruchet goto restore_auto_gain; 18963cca8ef5SHugues Fruchet } 189719a81c14SSteve Longerbeam 1898aa288248SMaxime Ripard /* 1899aa288248SMaxime Ripard * All the formats we support have 16 bits per pixel, seems to require 1900aa288248SMaxime Ripard * the same rate than YUV, so we can just use 16 bpp all the time. 1901aa288248SMaxime Ripard */ 1902cc196e48SBenoit Parrot rate = ov5640_calc_pixel_rate(sensor) * 16; 19038e823f5cSJacopo Mondi if (ov5640_is_csi2(sensor)) { 1904aa288248SMaxime Ripard rate = rate / sensor->ep.bus.mipi_csi2.num_data_lanes; 1905aa288248SMaxime Ripard ret = ov5640_set_mipi_pclk(sensor, rate); 1906aa288248SMaxime Ripard } else { 1907aa288248SMaxime Ripard rate = rate / sensor->ep.bus.parallel.bus_width; 1908aa288248SMaxime Ripard ret = ov5640_set_dvp_pclk(sensor, rate); 1909aa288248SMaxime Ripard } 1910aa288248SMaxime Ripard 1911aa288248SMaxime Ripard if (ret < 0) 1912aa288248SMaxime Ripard return 0; 1913aa288248SMaxime Ripard 191419a81c14SSteve Longerbeam if ((dn_mode == SUBSAMPLING && orig_dn_mode == SCALING) || 191519a81c14SSteve Longerbeam (dn_mode == SCALING && orig_dn_mode == SUBSAMPLING)) { 191619a81c14SSteve Longerbeam /* 191719a81c14SSteve Longerbeam * change between subsampling and scaling 19183cca8ef5SHugues Fruchet * go through exposure calculation 191919a81c14SSteve Longerbeam */ 192019a81c14SSteve Longerbeam ret = ov5640_set_mode_exposure_calc(sensor, mode); 192119a81c14SSteve Longerbeam } else { 192219a81c14SSteve Longerbeam /* 192319a81c14SSteve Longerbeam * change inside subsampling or scaling 192419a81c14SSteve Longerbeam * download firmware directly 192519a81c14SSteve Longerbeam */ 19263cca8ef5SHugues Fruchet ret = ov5640_set_mode_direct(sensor, mode); 192719a81c14SSteve Longerbeam } 192819a81c14SSteve Longerbeam if (ret < 0) 19293cca8ef5SHugues Fruchet goto restore_auto_exp_gain; 19303cca8ef5SHugues Fruchet 19313cca8ef5SHugues Fruchet /* restore auto gain and exposure */ 19323cca8ef5SHugues Fruchet if (auto_gain) 19333cca8ef5SHugues Fruchet ov5640_set_autogain(sensor, true); 19343cca8ef5SHugues Fruchet if (auto_exp) 19353cca8ef5SHugues Fruchet ov5640_set_autoexposure(sensor, true); 193619a81c14SSteve Longerbeam 1937ce85705aSHugues Fruchet ret = ov5640_set_binning(sensor, dn_mode != SCALING); 1938ce85705aSHugues Fruchet if (ret < 0) 1939ce85705aSHugues Fruchet return ret; 194019a81c14SSteve Longerbeam ret = ov5640_set_ae_target(sensor, sensor->ae_target); 194119a81c14SSteve Longerbeam if (ret < 0) 194219a81c14SSteve Longerbeam return ret; 194319a81c14SSteve Longerbeam ret = ov5640_get_light_freq(sensor); 194419a81c14SSteve Longerbeam if (ret < 0) 194519a81c14SSteve Longerbeam return ret; 194619a81c14SSteve Longerbeam ret = ov5640_set_bandingfilter(sensor); 194719a81c14SSteve Longerbeam if (ret < 0) 194819a81c14SSteve Longerbeam return ret; 194919a81c14SSteve Longerbeam ret = ov5640_set_virtual_channel(sensor); 195019a81c14SSteve Longerbeam if (ret < 0) 195119a81c14SSteve Longerbeam return ret; 195219a81c14SSteve Longerbeam 195319a81c14SSteve Longerbeam sensor->pending_mode_change = false; 1954985cdcb0SHugues Fruchet sensor->last_mode = mode; 195519a81c14SSteve Longerbeam 195619a81c14SSteve Longerbeam return 0; 19573cca8ef5SHugues Fruchet 19583cca8ef5SHugues Fruchet restore_auto_exp_gain: 19593cca8ef5SHugues Fruchet if (auto_exp) 19603cca8ef5SHugues Fruchet ov5640_set_autoexposure(sensor, true); 19613cca8ef5SHugues Fruchet restore_auto_gain: 19623cca8ef5SHugues Fruchet if (auto_gain) 19633cca8ef5SHugues Fruchet ov5640_set_autogain(sensor, true); 19643cca8ef5SHugues Fruchet 19653cca8ef5SHugues Fruchet return ret; 196619a81c14SSteve Longerbeam } 196719a81c14SSteve Longerbeam 196819ad26f9SAkinobu Mita static int ov5640_set_framefmt(struct ov5640_dev *sensor, 196919ad26f9SAkinobu Mita struct v4l2_mbus_framefmt *format); 197019ad26f9SAkinobu Mita 197119a81c14SSteve Longerbeam /* restore the last set video mode after chip power-on */ 197219a81c14SSteve Longerbeam static int ov5640_restore_mode(struct ov5640_dev *sensor) 197319a81c14SSteve Longerbeam { 197419a81c14SSteve Longerbeam int ret; 197519a81c14SSteve Longerbeam 197619a81c14SSteve Longerbeam /* first load the initial register values */ 197719a81c14SSteve Longerbeam ret = ov5640_load_regs(sensor, &ov5640_mode_init_data); 197819a81c14SSteve Longerbeam if (ret < 0) 197919a81c14SSteve Longerbeam return ret; 1980985cdcb0SHugues Fruchet sensor->last_mode = &ov5640_mode_init_data; 198119a81c14SSteve Longerbeam 19828f57c2f8SMaxime Ripard ret = ov5640_mod_reg(sensor, OV5640_REG_SYS_ROOT_DIVIDER, 0x3f, 19837851fe7aSMaxime Ripard (ilog2(OV5640_SCLK2X_ROOT_DIV) << 2) | 19847851fe7aSMaxime Ripard ilog2(OV5640_SCLK_ROOT_DIV)); 19858f57c2f8SMaxime Ripard if (ret) 19868f57c2f8SMaxime Ripard return ret; 19878f57c2f8SMaxime Ripard 198819a81c14SSteve Longerbeam /* now restore the last capture mode */ 1989985cdcb0SHugues Fruchet ret = ov5640_set_mode(sensor); 199019ad26f9SAkinobu Mita if (ret < 0) 199119ad26f9SAkinobu Mita return ret; 199219ad26f9SAkinobu Mita 199319ad26f9SAkinobu Mita return ov5640_set_framefmt(sensor, &sensor->fmt); 199419a81c14SSteve Longerbeam } 199519a81c14SSteve Longerbeam 199619a81c14SSteve Longerbeam static void ov5640_power(struct ov5640_dev *sensor, bool enable) 199719a81c14SSteve Longerbeam { 19981fddc5daSHugues Fruchet gpiod_set_value_cansleep(sensor->pwdn_gpio, enable ? 0 : 1); 199919a81c14SSteve Longerbeam } 200019a81c14SSteve Longerbeam 200119a81c14SSteve Longerbeam static void ov5640_reset(struct ov5640_dev *sensor) 200219a81c14SSteve Longerbeam { 200319a81c14SSteve Longerbeam if (!sensor->reset_gpio) 200419a81c14SSteve Longerbeam return; 200519a81c14SSteve Longerbeam 20061fddc5daSHugues Fruchet gpiod_set_value_cansleep(sensor->reset_gpio, 0); 200719a81c14SSteve Longerbeam 200819a81c14SSteve Longerbeam /* camera power cycle */ 200919a81c14SSteve Longerbeam ov5640_power(sensor, false); 201019a81c14SSteve Longerbeam usleep_range(5000, 10000); 201119a81c14SSteve Longerbeam ov5640_power(sensor, true); 201219a81c14SSteve Longerbeam usleep_range(5000, 10000); 201319a81c14SSteve Longerbeam 20141fddc5daSHugues Fruchet gpiod_set_value_cansleep(sensor->reset_gpio, 1); 201519a81c14SSteve Longerbeam usleep_range(1000, 2000); 201619a81c14SSteve Longerbeam 20171fddc5daSHugues Fruchet gpiod_set_value_cansleep(sensor->reset_gpio, 0); 20181d4c41f3SLoic Poulain usleep_range(20000, 25000); 201919a81c14SSteve Longerbeam } 202019a81c14SSteve Longerbeam 20210f7acb52SHugues Fruchet static int ov5640_set_power_on(struct ov5640_dev *sensor) 202219a81c14SSteve Longerbeam { 20230f7acb52SHugues Fruchet struct i2c_client *client = sensor->i2c_client; 20240f7acb52SHugues Fruchet int ret; 202519a81c14SSteve Longerbeam 20260f7acb52SHugues Fruchet ret = clk_prepare_enable(sensor->xclk); 20270f7acb52SHugues Fruchet if (ret) { 20280f7acb52SHugues Fruchet dev_err(&client->dev, "%s: failed to enable clock\n", 20290f7acb52SHugues Fruchet __func__); 20300f7acb52SHugues Fruchet return ret; 20310f7acb52SHugues Fruchet } 203219a81c14SSteve Longerbeam 203319a81c14SSteve Longerbeam ret = regulator_bulk_enable(OV5640_NUM_SUPPLIES, 203419a81c14SSteve Longerbeam sensor->supplies); 20350f7acb52SHugues Fruchet if (ret) { 20360f7acb52SHugues Fruchet dev_err(&client->dev, "%s: failed to enable regulators\n", 20370f7acb52SHugues Fruchet __func__); 203819a81c14SSteve Longerbeam goto xclk_off; 20390f7acb52SHugues Fruchet } 204019a81c14SSteve Longerbeam 204119a81c14SSteve Longerbeam ov5640_reset(sensor); 204219a81c14SSteve Longerbeam ov5640_power(sensor, true); 204319a81c14SSteve Longerbeam 204419a81c14SSteve Longerbeam ret = ov5640_init_slave_id(sensor); 204519a81c14SSteve Longerbeam if (ret) 204619a81c14SSteve Longerbeam goto power_off; 204719a81c14SSteve Longerbeam 20480f7acb52SHugues Fruchet return 0; 20490f7acb52SHugues Fruchet 20500f7acb52SHugues Fruchet power_off: 20510f7acb52SHugues Fruchet ov5640_power(sensor, false); 20520f7acb52SHugues Fruchet regulator_bulk_disable(OV5640_NUM_SUPPLIES, sensor->supplies); 20530f7acb52SHugues Fruchet xclk_off: 20540f7acb52SHugues Fruchet clk_disable_unprepare(sensor->xclk); 20550f7acb52SHugues Fruchet return ret; 20560f7acb52SHugues Fruchet } 20570f7acb52SHugues Fruchet 20580f7acb52SHugues Fruchet static void ov5640_set_power_off(struct ov5640_dev *sensor) 20590f7acb52SHugues Fruchet { 20600f7acb52SHugues Fruchet ov5640_power(sensor, false); 20610f7acb52SHugues Fruchet regulator_bulk_disable(OV5640_NUM_SUPPLIES, sensor->supplies); 20620f7acb52SHugues Fruchet clk_disable_unprepare(sensor->xclk); 20630f7acb52SHugues Fruchet } 20640f7acb52SHugues Fruchet 2065b1751ae6SLad Prabhakar static int ov5640_set_power_mipi(struct ov5640_dev *sensor, bool on) 2066b1751ae6SLad Prabhakar { 2067b1751ae6SLad Prabhakar int ret; 2068b1751ae6SLad Prabhakar 2069b1751ae6SLad Prabhakar if (!on) { 2070b1751ae6SLad Prabhakar /* Reset MIPI bus settings to their default values. */ 2071b1751ae6SLad Prabhakar ov5640_write_reg(sensor, OV5640_REG_IO_MIPI_CTRL00, 0x58); 2072b1751ae6SLad Prabhakar ov5640_write_reg(sensor, OV5640_REG_MIPI_CTRL00, 0x04); 2073b1751ae6SLad Prabhakar ov5640_write_reg(sensor, OV5640_REG_PAD_OUTPUT00, 0x00); 2074b1751ae6SLad Prabhakar return 0; 2075b1751ae6SLad Prabhakar } 2076b1751ae6SLad Prabhakar 2077b1751ae6SLad Prabhakar /* 2078b1751ae6SLad Prabhakar * Power up MIPI HS Tx and LS Rx; 2 data lanes mode 2079b1751ae6SLad Prabhakar * 2080b1751ae6SLad Prabhakar * 0x300e = 0x40 2081b1751ae6SLad Prabhakar * [7:5] = 010 : 2 data lanes mode (see FIXME note in 2082b1751ae6SLad Prabhakar * "ov5640_set_stream_mipi()") 2083b1751ae6SLad Prabhakar * [4] = 0 : Power up MIPI HS Tx 2084b1751ae6SLad Prabhakar * [3] = 0 : Power up MIPI LS Rx 2085b1751ae6SLad Prabhakar * [2] = 0 : MIPI interface disabled 2086b1751ae6SLad Prabhakar */ 2087b1751ae6SLad Prabhakar ret = ov5640_write_reg(sensor, OV5640_REG_IO_MIPI_CTRL00, 0x40); 2088b1751ae6SLad Prabhakar if (ret) 2089b1751ae6SLad Prabhakar return ret; 2090b1751ae6SLad Prabhakar 2091b1751ae6SLad Prabhakar /* 2092b1751ae6SLad Prabhakar * Gate clock and set LP11 in 'no packets mode' (idle) 2093b1751ae6SLad Prabhakar * 2094b1751ae6SLad Prabhakar * 0x4800 = 0x24 2095b1751ae6SLad Prabhakar * [5] = 1 : Gate clock when 'no packets' 2096b1751ae6SLad Prabhakar * [2] = 1 : MIPI bus in LP11 when 'no packets' 2097b1751ae6SLad Prabhakar */ 2098b1751ae6SLad Prabhakar ret = ov5640_write_reg(sensor, OV5640_REG_MIPI_CTRL00, 0x24); 2099b1751ae6SLad Prabhakar if (ret) 2100b1751ae6SLad Prabhakar return ret; 2101b1751ae6SLad Prabhakar 2102b1751ae6SLad Prabhakar /* 2103b1751ae6SLad Prabhakar * Set data lanes and clock in LP11 when 'sleeping' 2104b1751ae6SLad Prabhakar * 2105b1751ae6SLad Prabhakar * 0x3019 = 0x70 2106b1751ae6SLad Prabhakar * [6] = 1 : MIPI data lane 2 in LP11 when 'sleeping' 2107b1751ae6SLad Prabhakar * [5] = 1 : MIPI data lane 1 in LP11 when 'sleeping' 2108b1751ae6SLad Prabhakar * [4] = 1 : MIPI clock lane in LP11 when 'sleeping' 2109b1751ae6SLad Prabhakar */ 2110b1751ae6SLad Prabhakar ret = ov5640_write_reg(sensor, OV5640_REG_PAD_OUTPUT00, 0x70); 2111b1751ae6SLad Prabhakar if (ret) 2112b1751ae6SLad Prabhakar return ret; 2113b1751ae6SLad Prabhakar 2114b1751ae6SLad Prabhakar /* Give lanes some time to coax into LP11 state. */ 2115b1751ae6SLad Prabhakar usleep_range(500, 1000); 2116b1751ae6SLad Prabhakar 2117b1751ae6SLad Prabhakar return 0; 2118b1751ae6SLad Prabhakar } 2119b1751ae6SLad Prabhakar 2120576f5d4bSLad Prabhakar static int ov5640_set_power_dvp(struct ov5640_dev *sensor, bool on) 2121576f5d4bSLad Prabhakar { 2122311a6408SLad Prabhakar unsigned int flags = sensor->ep.bus.parallel.flags; 212368579b32SHugues Fruchet bool bt656 = sensor->ep.bus_type == V4L2_MBUS_BT656; 212468579b32SHugues Fruchet u8 polarities = 0; 2125576f5d4bSLad Prabhakar int ret; 2126576f5d4bSLad Prabhakar 2127576f5d4bSLad Prabhakar if (!on) { 2128576f5d4bSLad Prabhakar /* Reset settings to their default values. */ 212968579b32SHugues Fruchet ov5640_write_reg(sensor, OV5640_REG_CCIR656_CTRL00, 0x00); 2130311a6408SLad Prabhakar ov5640_write_reg(sensor, OV5640_REG_IO_MIPI_CTRL00, 0x58); 2131311a6408SLad Prabhakar ov5640_write_reg(sensor, OV5640_REG_POLARITY_CTRL00, 0x20); 2132576f5d4bSLad Prabhakar ov5640_write_reg(sensor, OV5640_REG_PAD_OUTPUT_ENABLE01, 0x00); 2133576f5d4bSLad Prabhakar ov5640_write_reg(sensor, OV5640_REG_PAD_OUTPUT_ENABLE02, 0x00); 2134576f5d4bSLad Prabhakar return 0; 2135576f5d4bSLad Prabhakar } 2136576f5d4bSLad Prabhakar 2137576f5d4bSLad Prabhakar /* 2138311a6408SLad Prabhakar * Note about parallel port configuration. 2139311a6408SLad Prabhakar * 2140311a6408SLad Prabhakar * When configured in parallel mode, the OV5640 will 2141311a6408SLad Prabhakar * output 10 bits data on DVP data lines [9:0]. 2142311a6408SLad Prabhakar * If only 8 bits data are wanted, the 8 bits data lines 2143311a6408SLad Prabhakar * of the camera interface must be physically connected 2144311a6408SLad Prabhakar * on the DVP data lines [9:2]. 2145311a6408SLad Prabhakar * 2146311a6408SLad Prabhakar * Control lines polarity can be configured through 2147311a6408SLad Prabhakar * devicetree endpoint control lines properties. 2148311a6408SLad Prabhakar * If no endpoint control lines properties are set, 2149311a6408SLad Prabhakar * polarity will be as below: 2150311a6408SLad Prabhakar * - VSYNC: active high 2151311a6408SLad Prabhakar * - HREF: active low 2152311a6408SLad Prabhakar * - PCLK: active low 215368579b32SHugues Fruchet * 215468579b32SHugues Fruchet * VSYNC & HREF are not configured if BT656 bus mode is selected 2155311a6408SLad Prabhakar */ 215668579b32SHugues Fruchet 215768579b32SHugues Fruchet /* 215868579b32SHugues Fruchet * BT656 embedded synchronization configuration 215968579b32SHugues Fruchet * 216068579b32SHugues Fruchet * CCIR656 CTRL00 216168579b32SHugues Fruchet * - [7]: SYNC code selection (0: auto generate sync code, 216268579b32SHugues Fruchet * 1: sync code from regs 0x4732-0x4735) 216368579b32SHugues Fruchet * - [6]: f value in CCIR656 SYNC code when fixed f value 216468579b32SHugues Fruchet * - [5]: Fixed f value 216568579b32SHugues Fruchet * - [4:3]: Blank toggle data options (00: data=1'h040/1'h200, 216668579b32SHugues Fruchet * 01: data from regs 0x4736-0x4738, 10: always keep 0) 216768579b32SHugues Fruchet * - [1]: Clip data disable 216868579b32SHugues Fruchet * - [0]: CCIR656 mode enable 216968579b32SHugues Fruchet * 217068579b32SHugues Fruchet * Default CCIR656 SAV/EAV mode with default codes 217168579b32SHugues Fruchet * SAV=0xff000080 & EAV=0xff00009d is enabled here with settings: 217268579b32SHugues Fruchet * - CCIR656 mode enable 217368579b32SHugues Fruchet * - auto generation of sync codes 217468579b32SHugues Fruchet * - blank toggle data 1'h040/1'h200 217568579b32SHugues Fruchet * - clip reserved data (0x00 & 0xff changed to 0x01 & 0xfe) 217668579b32SHugues Fruchet */ 217768579b32SHugues Fruchet ret = ov5640_write_reg(sensor, OV5640_REG_CCIR656_CTRL00, 217868579b32SHugues Fruchet bt656 ? 0x01 : 0x00); 217968579b32SHugues Fruchet if (ret) 218068579b32SHugues Fruchet return ret; 218168579b32SHugues Fruchet 2182311a6408SLad Prabhakar /* 2183311a6408SLad Prabhakar * configure parallel port control lines polarity 2184311a6408SLad Prabhakar * 2185311a6408SLad Prabhakar * POLARITY CTRL0 2186311a6408SLad Prabhakar * - [5]: PCLK polarity (0: active low, 1: active high) 2187311a6408SLad Prabhakar * - [1]: HREF polarity (0: active low, 1: active high) 2188311a6408SLad Prabhakar * - [0]: VSYNC polarity (mismatch here between 2189311a6408SLad Prabhakar * datasheet and hardware, 0 is active high 2190311a6408SLad Prabhakar * and 1 is active low...) 2191311a6408SLad Prabhakar */ 219268579b32SHugues Fruchet if (!bt656) { 2193311a6408SLad Prabhakar if (flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH) 219468579b32SHugues Fruchet polarities |= BIT(1); 2195311a6408SLad Prabhakar if (flags & V4L2_MBUS_VSYNC_ACTIVE_LOW) 219668579b32SHugues Fruchet polarities |= BIT(0); 219768579b32SHugues Fruchet } 219868579b32SHugues Fruchet if (flags & V4L2_MBUS_PCLK_SAMPLE_RISING) 219968579b32SHugues Fruchet polarities |= BIT(5); 2200311a6408SLad Prabhakar 220168579b32SHugues Fruchet ret = ov5640_write_reg(sensor, OV5640_REG_POLARITY_CTRL00, polarities); 2202311a6408SLad Prabhakar if (ret) 2203311a6408SLad Prabhakar return ret; 2204311a6408SLad Prabhakar 2205311a6408SLad Prabhakar /* 220668579b32SHugues Fruchet * powerdown MIPI TX/RX PHY & enable DVP 2207311a6408SLad Prabhakar * 2208311a6408SLad Prabhakar * MIPI CONTROL 00 220968579b32SHugues Fruchet * [4] = 1 : Power down MIPI HS Tx 221068579b32SHugues Fruchet * [3] = 1 : Power down MIPI LS Rx 221168579b32SHugues Fruchet * [2] = 0 : DVP enable (MIPI disable) 2212311a6408SLad Prabhakar */ 2213311a6408SLad Prabhakar ret = ov5640_write_reg(sensor, OV5640_REG_IO_MIPI_CTRL00, 0x18); 2214311a6408SLad Prabhakar if (ret) 2215311a6408SLad Prabhakar return ret; 2216311a6408SLad Prabhakar 2217311a6408SLad Prabhakar /* 2218576f5d4bSLad Prabhakar * enable VSYNC/HREF/PCLK DVP control lines 2219576f5d4bSLad Prabhakar * & D[9:6] DVP data lines 2220576f5d4bSLad Prabhakar * 2221576f5d4bSLad Prabhakar * PAD OUTPUT ENABLE 01 2222576f5d4bSLad Prabhakar * - 6: VSYNC output enable 2223576f5d4bSLad Prabhakar * - 5: HREF output enable 2224576f5d4bSLad Prabhakar * - 4: PCLK output enable 2225576f5d4bSLad Prabhakar * - [3:0]: D[9:6] output enable 2226576f5d4bSLad Prabhakar */ 22274039b037SLad Prabhakar ret = ov5640_write_reg(sensor, OV5640_REG_PAD_OUTPUT_ENABLE01, 222868579b32SHugues Fruchet bt656 ? 0x1f : 0x7f); 2229576f5d4bSLad Prabhakar if (ret) 2230576f5d4bSLad Prabhakar return ret; 2231576f5d4bSLad Prabhakar 2232576f5d4bSLad Prabhakar /* 2233576f5d4bSLad Prabhakar * enable D[5:0] DVP data lines 2234576f5d4bSLad Prabhakar * 2235576f5d4bSLad Prabhakar * PAD OUTPUT ENABLE 02 2236576f5d4bSLad Prabhakar * - [7:2]: D[5:0] output enable 2237576f5d4bSLad Prabhakar */ 2238576f5d4bSLad Prabhakar return ov5640_write_reg(sensor, OV5640_REG_PAD_OUTPUT_ENABLE02, 0xfc); 2239576f5d4bSLad Prabhakar } 2240576f5d4bSLad Prabhakar 22410f7acb52SHugues Fruchet static int ov5640_set_power(struct ov5640_dev *sensor, bool on) 22420f7acb52SHugues Fruchet { 22430f7acb52SHugues Fruchet int ret = 0; 22440f7acb52SHugues Fruchet 22450f7acb52SHugues Fruchet if (on) { 22460f7acb52SHugues Fruchet ret = ov5640_set_power_on(sensor); 22470f7acb52SHugues Fruchet if (ret) 22480f7acb52SHugues Fruchet return ret; 22490f7acb52SHugues Fruchet 225019a81c14SSteve Longerbeam ret = ov5640_restore_mode(sensor); 225119a81c14SSteve Longerbeam if (ret) 225219a81c14SSteve Longerbeam goto power_off; 2253b1751ae6SLad Prabhakar } 225419a81c14SSteve Longerbeam 2255576f5d4bSLad Prabhakar if (sensor->ep.bus_type == V4L2_MBUS_CSI2_DPHY) 2256b1751ae6SLad Prabhakar ret = ov5640_set_power_mipi(sensor, on); 2257576f5d4bSLad Prabhakar else 2258576f5d4bSLad Prabhakar ret = ov5640_set_power_dvp(sensor, on); 2259b1751ae6SLad Prabhakar if (ret) 2260b1751ae6SLad Prabhakar goto power_off; 2261aa4bb8b8SJacopo Mondi 2262b1751ae6SLad Prabhakar if (!on) 2263aa4bb8b8SJacopo Mondi ov5640_set_power_off(sensor); 226419a81c14SSteve Longerbeam 226519a81c14SSteve Longerbeam return 0; 226619a81c14SSteve Longerbeam 226719a81c14SSteve Longerbeam power_off: 22680f7acb52SHugues Fruchet ov5640_set_power_off(sensor); 226919a81c14SSteve Longerbeam return ret; 227019a81c14SSteve Longerbeam } 227119a81c14SSteve Longerbeam 227219a81c14SSteve Longerbeam /* --------------- Subdev Operations --------------- */ 227319a81c14SSteve Longerbeam 227419a81c14SSteve Longerbeam static int ov5640_s_power(struct v4l2_subdev *sd, int on) 227519a81c14SSteve Longerbeam { 227619a81c14SSteve Longerbeam struct ov5640_dev *sensor = to_ov5640_dev(sd); 227719a81c14SSteve Longerbeam int ret = 0; 227819a81c14SSteve Longerbeam 227919a81c14SSteve Longerbeam mutex_lock(&sensor->lock); 228019a81c14SSteve Longerbeam 228119a81c14SSteve Longerbeam /* 228219a81c14SSteve Longerbeam * If the power count is modified from 0 to != 0 or from != 0 to 0, 228319a81c14SSteve Longerbeam * update the power state. 228419a81c14SSteve Longerbeam */ 228519a81c14SSteve Longerbeam if (sensor->power_count == !on) { 228619a81c14SSteve Longerbeam ret = ov5640_set_power(sensor, !!on); 228719a81c14SSteve Longerbeam if (ret) 228819a81c14SSteve Longerbeam goto out; 228919a81c14SSteve Longerbeam } 229019a81c14SSteve Longerbeam 229119a81c14SSteve Longerbeam /* Update the power count. */ 229219a81c14SSteve Longerbeam sensor->power_count += on ? 1 : -1; 229319a81c14SSteve Longerbeam WARN_ON(sensor->power_count < 0); 229419a81c14SSteve Longerbeam out: 229519a81c14SSteve Longerbeam mutex_unlock(&sensor->lock); 229619a81c14SSteve Longerbeam 229719a81c14SSteve Longerbeam if (on && !ret && sensor->power_count == 1) { 229819a81c14SSteve Longerbeam /* restore controls */ 229919a81c14SSteve Longerbeam ret = v4l2_ctrl_handler_setup(&sensor->ctrls.handler); 230019a81c14SSteve Longerbeam } 230119a81c14SSteve Longerbeam 230219a81c14SSteve Longerbeam return ret; 230319a81c14SSteve Longerbeam } 230419a81c14SSteve Longerbeam 230519a81c14SSteve Longerbeam static int ov5640_try_frame_interval(struct ov5640_dev *sensor, 230619a81c14SSteve Longerbeam struct v4l2_fract *fi, 230719a81c14SSteve Longerbeam u32 width, u32 height) 230819a81c14SSteve Longerbeam { 230919a81c14SSteve Longerbeam const struct ov5640_mode_info *mode; 23106530a5ebSJagan Teki enum ov5640_frame_rate rate = OV5640_15_FPS; 2311f6cc192fSMaxime Ripard int minfps, maxfps, best_fps, fps; 2312f6cc192fSMaxime Ripard int i; 231319a81c14SSteve Longerbeam 231419a81c14SSteve Longerbeam minfps = ov5640_framerates[OV5640_15_FPS]; 2315e823fb16SMaxime Ripard maxfps = ov5640_framerates[OV5640_60_FPS]; 231619a81c14SSteve Longerbeam 231719a81c14SSteve Longerbeam if (fi->numerator == 0) { 231819a81c14SSteve Longerbeam fi->denominator = maxfps; 231919a81c14SSteve Longerbeam fi->numerator = 1; 2320e823fb16SMaxime Ripard rate = OV5640_60_FPS; 2321e823fb16SMaxime Ripard goto find_mode; 232219a81c14SSteve Longerbeam } 232319a81c14SSteve Longerbeam 2324f6cc192fSMaxime Ripard fps = clamp_val(DIV_ROUND_CLOSEST(fi->denominator, fi->numerator), 2325f6cc192fSMaxime Ripard minfps, maxfps); 2326f6cc192fSMaxime Ripard 2327f6cc192fSMaxime Ripard best_fps = minfps; 2328f6cc192fSMaxime Ripard for (i = 0; i < ARRAY_SIZE(ov5640_framerates); i++) { 2329f6cc192fSMaxime Ripard int curr_fps = ov5640_framerates[i]; 2330f6cc192fSMaxime Ripard 2331f6cc192fSMaxime Ripard if (abs(curr_fps - fps) < abs(best_fps - fps)) { 2332f6cc192fSMaxime Ripard best_fps = curr_fps; 2333f6cc192fSMaxime Ripard rate = i; 2334f6cc192fSMaxime Ripard } 2335f6cc192fSMaxime Ripard } 233619a81c14SSteve Longerbeam 233719a81c14SSteve Longerbeam fi->numerator = 1; 2338f6cc192fSMaxime Ripard fi->denominator = best_fps; 233919a81c14SSteve Longerbeam 2340e823fb16SMaxime Ripard find_mode: 23415a3ad937SMaxime Ripard mode = ov5640_find_mode(sensor, rate, width, height, false); 23425a3ad937SMaxime Ripard return mode ? rate : -EINVAL; 234319a81c14SSteve Longerbeam } 234419a81c14SSteve Longerbeam 234519a81c14SSteve Longerbeam static int ov5640_get_fmt(struct v4l2_subdev *sd, 23460d346d2aSTomi Valkeinen struct v4l2_subdev_state *sd_state, 234719a81c14SSteve Longerbeam struct v4l2_subdev_format *format) 234819a81c14SSteve Longerbeam { 234919a81c14SSteve Longerbeam struct ov5640_dev *sensor = to_ov5640_dev(sd); 235019a81c14SSteve Longerbeam struct v4l2_mbus_framefmt *fmt; 235119a81c14SSteve Longerbeam 235219a81c14SSteve Longerbeam if (format->pad != 0) 235319a81c14SSteve Longerbeam return -EINVAL; 235419a81c14SSteve Longerbeam 235519a81c14SSteve Longerbeam mutex_lock(&sensor->lock); 235619a81c14SSteve Longerbeam 235719a81c14SSteve Longerbeam if (format->which == V4L2_SUBDEV_FORMAT_TRY) 23580d346d2aSTomi Valkeinen fmt = v4l2_subdev_get_try_format(&sensor->sd, sd_state, 235919a81c14SSteve Longerbeam format->pad); 236019a81c14SSteve Longerbeam else 236119a81c14SSteve Longerbeam fmt = &sensor->fmt; 236219a81c14SSteve Longerbeam 236319a81c14SSteve Longerbeam format->format = *fmt; 236419a81c14SSteve Longerbeam 236519a81c14SSteve Longerbeam mutex_unlock(&sensor->lock); 236619a81c14SSteve Longerbeam 236719a81c14SSteve Longerbeam return 0; 236819a81c14SSteve Longerbeam } 236919a81c14SSteve Longerbeam 237019a81c14SSteve Longerbeam static int ov5640_try_fmt_internal(struct v4l2_subdev *sd, 237119a81c14SSteve Longerbeam struct v4l2_mbus_framefmt *fmt, 237219a81c14SSteve Longerbeam enum ov5640_frame_rate fr, 237319a81c14SSteve Longerbeam const struct ov5640_mode_info **new_mode) 237419a81c14SSteve Longerbeam { 237519a81c14SSteve Longerbeam struct ov5640_dev *sensor = to_ov5640_dev(sd); 237619a81c14SSteve Longerbeam const struct ov5640_mode_info *mode; 2377e3ee691dSHugues Fruchet int i; 237819a81c14SSteve Longerbeam 237919a81c14SSteve Longerbeam mode = ov5640_find_mode(sensor, fr, fmt->width, fmt->height, true); 238019a81c14SSteve Longerbeam if (!mode) 238119a81c14SSteve Longerbeam return -EINVAL; 2382dba13a0bSMaxime Ripard fmt->width = mode->hact; 2383dba13a0bSMaxime Ripard fmt->height = mode->vact; 238419a81c14SSteve Longerbeam 238519a81c14SSteve Longerbeam if (new_mode) 238619a81c14SSteve Longerbeam *new_mode = mode; 2387e3ee691dSHugues Fruchet 2388e3ee691dSHugues Fruchet for (i = 0; i < ARRAY_SIZE(ov5640_formats); i++) 2389e3ee691dSHugues Fruchet if (ov5640_formats[i].code == fmt->code) 2390e3ee691dSHugues Fruchet break; 2391e3ee691dSHugues Fruchet if (i >= ARRAY_SIZE(ov5640_formats)) 2392e6441fdeSHugues Fruchet i = 0; 2393e6441fdeSHugues Fruchet 2394e6441fdeSHugues Fruchet fmt->code = ov5640_formats[i].code; 2395e6441fdeSHugues Fruchet fmt->colorspace = ov5640_formats[i].colorspace; 2396e6441fdeSHugues Fruchet fmt->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(fmt->colorspace); 2397e6441fdeSHugues Fruchet fmt->quantization = V4L2_QUANTIZATION_FULL_RANGE; 2398e6441fdeSHugues Fruchet fmt->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(fmt->colorspace); 2399e3ee691dSHugues Fruchet 240019a81c14SSteve Longerbeam return 0; 240119a81c14SSteve Longerbeam } 240219a81c14SSteve Longerbeam 240319a81c14SSteve Longerbeam static int ov5640_set_fmt(struct v4l2_subdev *sd, 24040d346d2aSTomi Valkeinen struct v4l2_subdev_state *sd_state, 240519a81c14SSteve Longerbeam struct v4l2_subdev_format *format) 240619a81c14SSteve Longerbeam { 240719a81c14SSteve Longerbeam struct ov5640_dev *sensor = to_ov5640_dev(sd); 240819a81c14SSteve Longerbeam const struct ov5640_mode_info *new_mode; 2409e6441fdeSHugues Fruchet struct v4l2_mbus_framefmt *mbus_fmt = &format->format; 241019a81c14SSteve Longerbeam int ret; 241119a81c14SSteve Longerbeam 241219a81c14SSteve Longerbeam if (format->pad != 0) 241319a81c14SSteve Longerbeam return -EINVAL; 241419a81c14SSteve Longerbeam 241519a81c14SSteve Longerbeam mutex_lock(&sensor->lock); 241619a81c14SSteve Longerbeam 241719a81c14SSteve Longerbeam if (sensor->streaming) { 241819a81c14SSteve Longerbeam ret = -EBUSY; 241919a81c14SSteve Longerbeam goto out; 242019a81c14SSteve Longerbeam } 242119a81c14SSteve Longerbeam 2422e6441fdeSHugues Fruchet ret = ov5640_try_fmt_internal(sd, mbus_fmt, 242319a81c14SSteve Longerbeam sensor->current_fr, &new_mode); 242419a81c14SSteve Longerbeam if (ret) 242519a81c14SSteve Longerbeam goto out; 242619a81c14SSteve Longerbeam 2427e738f5ddSMirela Rabulea if (format->which == V4L2_SUBDEV_FORMAT_TRY) { 2428e738f5ddSMirela Rabulea *v4l2_subdev_get_try_format(sd, sd_state, 0) = *mbus_fmt; 2429e738f5ddSMirela Rabulea goto out; 2430e738f5ddSMirela Rabulea } 243119a81c14SSteve Longerbeam 24326949d864SHugues Fruchet if (new_mode != sensor->current_mode) { 243319a81c14SSteve Longerbeam sensor->current_mode = new_mode; 243419a81c14SSteve Longerbeam sensor->pending_mode_change = true; 24356949d864SHugues Fruchet } 243607115449SJacopo Mondi if (mbus_fmt->code != sensor->fmt.code) 2437fb98e29fSHugues Fruchet sensor->pending_fmt_change = true; 243807115449SJacopo Mondi 2439e738f5ddSMirela Rabulea /* update format even if code is unchanged, resolution might change */ 2440e738f5ddSMirela Rabulea sensor->fmt = *mbus_fmt; 2441e738f5ddSMirela Rabulea 2442cc196e48SBenoit Parrot __v4l2_ctrl_s_ctrl_int64(sensor->ctrls.pixel_rate, 2443cc196e48SBenoit Parrot ov5640_calc_pixel_rate(sensor)); 244419a81c14SSteve Longerbeam out: 244519a81c14SSteve Longerbeam mutex_unlock(&sensor->lock); 244619a81c14SSteve Longerbeam return ret; 244719a81c14SSteve Longerbeam } 244819a81c14SSteve Longerbeam 2449e3ee691dSHugues Fruchet static int ov5640_set_framefmt(struct ov5640_dev *sensor, 2450e3ee691dSHugues Fruchet struct v4l2_mbus_framefmt *format) 2451e3ee691dSHugues Fruchet { 2452e3ee691dSHugues Fruchet int ret = 0; 2453d47c4126SHugues Fruchet bool is_jpeg = false; 2454b7ed3abdSLoic Poulain u8 fmt, mux; 2455e3ee691dSHugues Fruchet 2456e3ee691dSHugues Fruchet switch (format->code) { 24571536fbdbSXavier Roumegue case MEDIA_BUS_FMT_UYVY8_1X16: 2458e3ee691dSHugues Fruchet case MEDIA_BUS_FMT_UYVY8_2X8: 2459e3ee691dSHugues Fruchet /* YUV422, UYVY */ 2460b7ed3abdSLoic Poulain fmt = 0x3f; 2461b7ed3abdSLoic Poulain mux = OV5640_FMT_MUX_YUV422; 2462e3ee691dSHugues Fruchet break; 24631536fbdbSXavier Roumegue case MEDIA_BUS_FMT_YUYV8_1X16: 2464e3ee691dSHugues Fruchet case MEDIA_BUS_FMT_YUYV8_2X8: 2465e3ee691dSHugues Fruchet /* YUV422, YUYV */ 2466b7ed3abdSLoic Poulain fmt = 0x30; 2467b7ed3abdSLoic Poulain mux = OV5640_FMT_MUX_YUV422; 2468e3ee691dSHugues Fruchet break; 2469e3ee691dSHugues Fruchet case MEDIA_BUS_FMT_RGB565_2X8_LE: 2470e3ee691dSHugues Fruchet /* RGB565 {g[2:0],b[4:0]},{r[4:0],g[5:3]} */ 2471b7ed3abdSLoic Poulain fmt = 0x6F; 2472b7ed3abdSLoic Poulain mux = OV5640_FMT_MUX_RGB; 2473e3ee691dSHugues Fruchet break; 2474e3ee691dSHugues Fruchet case MEDIA_BUS_FMT_RGB565_2X8_BE: 2475e3ee691dSHugues Fruchet /* RGB565 {r[4:0],g[5:3]},{g[2:0],b[4:0]} */ 2476b7ed3abdSLoic Poulain fmt = 0x61; 2477b7ed3abdSLoic Poulain mux = OV5640_FMT_MUX_RGB; 2478e3ee691dSHugues Fruchet break; 2479d47c4126SHugues Fruchet case MEDIA_BUS_FMT_JPEG_1X8: 2480d47c4126SHugues Fruchet /* YUV422, YUYV */ 2481b7ed3abdSLoic Poulain fmt = 0x30; 2482b7ed3abdSLoic Poulain mux = OV5640_FMT_MUX_YUV422; 2483d47c4126SHugues Fruchet is_jpeg = true; 2484d47c4126SHugues Fruchet break; 2485b7ed3abdSLoic Poulain case MEDIA_BUS_FMT_SBGGR8_1X8: 2486b7ed3abdSLoic Poulain /* Raw, BGBG... / GRGR... */ 2487b7ed3abdSLoic Poulain fmt = 0x00; 2488b7ed3abdSLoic Poulain mux = OV5640_FMT_MUX_RAW_DPC; 2489b7ed3abdSLoic Poulain break; 2490b7ed3abdSLoic Poulain case MEDIA_BUS_FMT_SGBRG8_1X8: 2491b7ed3abdSLoic Poulain /* Raw bayer, GBGB... / RGRG... */ 2492b7ed3abdSLoic Poulain fmt = 0x01; 2493b7ed3abdSLoic Poulain mux = OV5640_FMT_MUX_RAW_DPC; 2494b7ed3abdSLoic Poulain break; 2495b7ed3abdSLoic Poulain case MEDIA_BUS_FMT_SGRBG8_1X8: 2496b7ed3abdSLoic Poulain /* Raw bayer, GRGR... / BGBG... */ 2497b7ed3abdSLoic Poulain fmt = 0x02; 2498b7ed3abdSLoic Poulain mux = OV5640_FMT_MUX_RAW_DPC; 2499b7ed3abdSLoic Poulain break; 2500b7ed3abdSLoic Poulain case MEDIA_BUS_FMT_SRGGB8_1X8: 2501b7ed3abdSLoic Poulain /* Raw bayer, RGRG... / GBGB... */ 2502b7ed3abdSLoic Poulain fmt = 0x03; 2503b7ed3abdSLoic Poulain mux = OV5640_FMT_MUX_RAW_DPC; 2504b7ed3abdSLoic Poulain break; 2505e3ee691dSHugues Fruchet default: 2506e3ee691dSHugues Fruchet return -EINVAL; 2507e3ee691dSHugues Fruchet } 2508e3ee691dSHugues Fruchet 2509e3ee691dSHugues Fruchet /* FORMAT CONTROL00: YUV and RGB formatting */ 2510b7ed3abdSLoic Poulain ret = ov5640_write_reg(sensor, OV5640_REG_FORMAT_CONTROL00, fmt); 2511e3ee691dSHugues Fruchet if (ret) 2512e3ee691dSHugues Fruchet return ret; 2513e3ee691dSHugues Fruchet 2514e3ee691dSHugues Fruchet /* FORMAT MUX CONTROL: ISP YUV or RGB */ 2515b7ed3abdSLoic Poulain ret = ov5640_write_reg(sensor, OV5640_REG_ISP_FORMAT_MUX_CTRL, mux); 2516d47c4126SHugues Fruchet if (ret) 2517d47c4126SHugues Fruchet return ret; 2518d47c4126SHugues Fruchet 2519d47c4126SHugues Fruchet /* 2520d47c4126SHugues Fruchet * TIMING TC REG21: 2521d47c4126SHugues Fruchet * - [5]: JPEG enable 2522d47c4126SHugues Fruchet */ 2523d47c4126SHugues Fruchet ret = ov5640_mod_reg(sensor, OV5640_REG_TIMING_TC_REG21, 2524d47c4126SHugues Fruchet BIT(5), is_jpeg ? BIT(5) : 0); 2525d47c4126SHugues Fruchet if (ret) 2526d47c4126SHugues Fruchet return ret; 2527d47c4126SHugues Fruchet 2528d47c4126SHugues Fruchet /* 2529d47c4126SHugues Fruchet * SYSTEM RESET02: 2530d47c4126SHugues Fruchet * - [4]: Reset JFIFO 2531d47c4126SHugues Fruchet * - [3]: Reset SFIFO 2532d47c4126SHugues Fruchet * - [2]: Reset JPEG 2533d47c4126SHugues Fruchet */ 2534d47c4126SHugues Fruchet ret = ov5640_mod_reg(sensor, OV5640_REG_SYS_RESET02, 2535d47c4126SHugues Fruchet BIT(4) | BIT(3) | BIT(2), 2536d47c4126SHugues Fruchet is_jpeg ? 0 : (BIT(4) | BIT(3) | BIT(2))); 2537d47c4126SHugues Fruchet if (ret) 2538d47c4126SHugues Fruchet return ret; 2539d47c4126SHugues Fruchet 2540d47c4126SHugues Fruchet /* 2541d47c4126SHugues Fruchet * CLOCK ENABLE02: 2542d47c4126SHugues Fruchet * - [5]: Enable JPEG 2x clock 2543d47c4126SHugues Fruchet * - [3]: Enable JPEG clock 2544d47c4126SHugues Fruchet */ 2545d47c4126SHugues Fruchet return ov5640_mod_reg(sensor, OV5640_REG_SYS_CLOCK_ENABLE02, 2546d47c4126SHugues Fruchet BIT(5) | BIT(3), 2547d47c4126SHugues Fruchet is_jpeg ? (BIT(5) | BIT(3)) : 0); 2548e3ee691dSHugues Fruchet } 254919a81c14SSteve Longerbeam 255019a81c14SSteve Longerbeam /* 255119a81c14SSteve Longerbeam * Sensor Controls. 255219a81c14SSteve Longerbeam */ 255319a81c14SSteve Longerbeam 255419a81c14SSteve Longerbeam static int ov5640_set_ctrl_hue(struct ov5640_dev *sensor, int value) 255519a81c14SSteve Longerbeam { 255619a81c14SSteve Longerbeam int ret; 255719a81c14SSteve Longerbeam 255819a81c14SSteve Longerbeam if (value) { 255919a81c14SSteve Longerbeam ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0, 256019a81c14SSteve Longerbeam BIT(0), BIT(0)); 256119a81c14SSteve Longerbeam if (ret) 256219a81c14SSteve Longerbeam return ret; 256319a81c14SSteve Longerbeam ret = ov5640_write_reg16(sensor, OV5640_REG_SDE_CTRL1, value); 256419a81c14SSteve Longerbeam } else { 256519a81c14SSteve Longerbeam ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0, BIT(0), 0); 256619a81c14SSteve Longerbeam } 256719a81c14SSteve Longerbeam 256819a81c14SSteve Longerbeam return ret; 256919a81c14SSteve Longerbeam } 257019a81c14SSteve Longerbeam 257119a81c14SSteve Longerbeam static int ov5640_set_ctrl_contrast(struct ov5640_dev *sensor, int value) 257219a81c14SSteve Longerbeam { 257319a81c14SSteve Longerbeam int ret; 257419a81c14SSteve Longerbeam 257519a81c14SSteve Longerbeam if (value) { 257619a81c14SSteve Longerbeam ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0, 257719a81c14SSteve Longerbeam BIT(2), BIT(2)); 257819a81c14SSteve Longerbeam if (ret) 257919a81c14SSteve Longerbeam return ret; 258019a81c14SSteve Longerbeam ret = ov5640_write_reg(sensor, OV5640_REG_SDE_CTRL5, 258119a81c14SSteve Longerbeam value & 0xff); 258219a81c14SSteve Longerbeam } else { 258319a81c14SSteve Longerbeam ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0, BIT(2), 0); 258419a81c14SSteve Longerbeam } 258519a81c14SSteve Longerbeam 258619a81c14SSteve Longerbeam return ret; 258719a81c14SSteve Longerbeam } 258819a81c14SSteve Longerbeam 258919a81c14SSteve Longerbeam static int ov5640_set_ctrl_saturation(struct ov5640_dev *sensor, int value) 259019a81c14SSteve Longerbeam { 259119a81c14SSteve Longerbeam int ret; 259219a81c14SSteve Longerbeam 259319a81c14SSteve Longerbeam if (value) { 259419a81c14SSteve Longerbeam ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0, 259519a81c14SSteve Longerbeam BIT(1), BIT(1)); 259619a81c14SSteve Longerbeam if (ret) 259719a81c14SSteve Longerbeam return ret; 259819a81c14SSteve Longerbeam ret = ov5640_write_reg(sensor, OV5640_REG_SDE_CTRL3, 259919a81c14SSteve Longerbeam value & 0xff); 260019a81c14SSteve Longerbeam if (ret) 260119a81c14SSteve Longerbeam return ret; 260219a81c14SSteve Longerbeam ret = ov5640_write_reg(sensor, OV5640_REG_SDE_CTRL4, 260319a81c14SSteve Longerbeam value & 0xff); 260419a81c14SSteve Longerbeam } else { 260519a81c14SSteve Longerbeam ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0, BIT(1), 0); 260619a81c14SSteve Longerbeam } 260719a81c14SSteve Longerbeam 260819a81c14SSteve Longerbeam return ret; 260919a81c14SSteve Longerbeam } 261019a81c14SSteve Longerbeam 261119a81c14SSteve Longerbeam static int ov5640_set_ctrl_white_balance(struct ov5640_dev *sensor, int awb) 261219a81c14SSteve Longerbeam { 261319a81c14SSteve Longerbeam int ret; 261419a81c14SSteve Longerbeam 261519a81c14SSteve Longerbeam ret = ov5640_mod_reg(sensor, OV5640_REG_AWB_MANUAL_CTRL, 261619a81c14SSteve Longerbeam BIT(0), awb ? 0 : 1); 261719a81c14SSteve Longerbeam if (ret) 261819a81c14SSteve Longerbeam return ret; 261919a81c14SSteve Longerbeam 262019a81c14SSteve Longerbeam if (!awb) { 262119a81c14SSteve Longerbeam u16 red = (u16)sensor->ctrls.red_balance->val; 262219a81c14SSteve Longerbeam u16 blue = (u16)sensor->ctrls.blue_balance->val; 262319a81c14SSteve Longerbeam 262419a81c14SSteve Longerbeam ret = ov5640_write_reg16(sensor, OV5640_REG_AWB_R_GAIN, red); 262519a81c14SSteve Longerbeam if (ret) 262619a81c14SSteve Longerbeam return ret; 262719a81c14SSteve Longerbeam ret = ov5640_write_reg16(sensor, OV5640_REG_AWB_B_GAIN, blue); 262819a81c14SSteve Longerbeam } 262919a81c14SSteve Longerbeam 263019a81c14SSteve Longerbeam return ret; 263119a81c14SSteve Longerbeam } 263219a81c14SSteve Longerbeam 26333cca8ef5SHugues Fruchet static int ov5640_set_ctrl_exposure(struct ov5640_dev *sensor, 26343cca8ef5SHugues Fruchet enum v4l2_exposure_auto_type auto_exposure) 263519a81c14SSteve Longerbeam { 263619a81c14SSteve Longerbeam struct ov5640_ctrls *ctrls = &sensor->ctrls; 26373cca8ef5SHugues Fruchet bool auto_exp = (auto_exposure == V4L2_EXPOSURE_AUTO); 263819a81c14SSteve Longerbeam int ret = 0; 263919a81c14SSteve Longerbeam 264019a81c14SSteve Longerbeam if (ctrls->auto_exp->is_new) { 26413cca8ef5SHugues Fruchet ret = ov5640_set_autoexposure(sensor, auto_exp); 264219a81c14SSteve Longerbeam if (ret) 264319a81c14SSteve Longerbeam return ret; 264419a81c14SSteve Longerbeam } 264519a81c14SSteve Longerbeam 26463cca8ef5SHugues Fruchet if (!auto_exp && ctrls->exposure->is_new) { 264719a81c14SSteve Longerbeam u16 max_exp; 264819a81c14SSteve Longerbeam 264919a81c14SSteve Longerbeam ret = ov5640_read_reg16(sensor, OV5640_REG_AEC_PK_VTS, 265019a81c14SSteve Longerbeam &max_exp); 265119a81c14SSteve Longerbeam if (ret) 265219a81c14SSteve Longerbeam return ret; 265319a81c14SSteve Longerbeam ret = ov5640_get_vts(sensor); 265419a81c14SSteve Longerbeam if (ret < 0) 265519a81c14SSteve Longerbeam return ret; 265619a81c14SSteve Longerbeam max_exp += ret; 26576146fde3SHugues Fruchet ret = 0; 265819a81c14SSteve Longerbeam 265919a81c14SSteve Longerbeam if (ctrls->exposure->val < max_exp) 266019a81c14SSteve Longerbeam ret = ov5640_set_exposure(sensor, ctrls->exposure->val); 266119a81c14SSteve Longerbeam } 266219a81c14SSteve Longerbeam 266319a81c14SSteve Longerbeam return ret; 266419a81c14SSteve Longerbeam } 266519a81c14SSteve Longerbeam 26663cca8ef5SHugues Fruchet static int ov5640_set_ctrl_gain(struct ov5640_dev *sensor, bool auto_gain) 266719a81c14SSteve Longerbeam { 266819a81c14SSteve Longerbeam struct ov5640_ctrls *ctrls = &sensor->ctrls; 266919a81c14SSteve Longerbeam int ret = 0; 267019a81c14SSteve Longerbeam 267119a81c14SSteve Longerbeam if (ctrls->auto_gain->is_new) { 26723cca8ef5SHugues Fruchet ret = ov5640_set_autogain(sensor, auto_gain); 267319a81c14SSteve Longerbeam if (ret) 267419a81c14SSteve Longerbeam return ret; 267519a81c14SSteve Longerbeam } 267619a81c14SSteve Longerbeam 26773cca8ef5SHugues Fruchet if (!auto_gain && ctrls->gain->is_new) 26783cca8ef5SHugues Fruchet ret = ov5640_set_gain(sensor, ctrls->gain->val); 267919a81c14SSteve Longerbeam 268019a81c14SSteve Longerbeam return ret; 268119a81c14SSteve Longerbeam } 268219a81c14SSteve Longerbeam 26839f6d7bacSChen-Yu Tsai static const char * const test_pattern_menu[] = { 26849f6d7bacSChen-Yu Tsai "Disabled", 26859f6d7bacSChen-Yu Tsai "Color bars", 2686bddc5cdfSChen-Yu Tsai "Color bars w/ rolling bar", 2687bddc5cdfSChen-Yu Tsai "Color squares", 2688bddc5cdfSChen-Yu Tsai "Color squares w/ rolling bar", 26899f6d7bacSChen-Yu Tsai }; 26909f6d7bacSChen-Yu Tsai 2691a0c29afbSChen-Yu Tsai #define OV5640_TEST_ENABLE BIT(7) 2692a0c29afbSChen-Yu Tsai #define OV5640_TEST_ROLLING BIT(6) /* rolling horizontal bar */ 2693a0c29afbSChen-Yu Tsai #define OV5640_TEST_TRANSPARENT BIT(5) 2694a0c29afbSChen-Yu Tsai #define OV5640_TEST_SQUARE_BW BIT(4) /* black & white squares */ 2695a0c29afbSChen-Yu Tsai #define OV5640_TEST_BAR_STANDARD (0 << 2) 2696a0c29afbSChen-Yu Tsai #define OV5640_TEST_BAR_VERT_CHANGE_1 (1 << 2) 2697a0c29afbSChen-Yu Tsai #define OV5640_TEST_BAR_HOR_CHANGE (2 << 2) 2698a0c29afbSChen-Yu Tsai #define OV5640_TEST_BAR_VERT_CHANGE_2 (3 << 2) 2699a0c29afbSChen-Yu Tsai #define OV5640_TEST_BAR (0 << 0) 2700a0c29afbSChen-Yu Tsai #define OV5640_TEST_RANDOM (1 << 0) 2701a0c29afbSChen-Yu Tsai #define OV5640_TEST_SQUARE (2 << 0) 2702a0c29afbSChen-Yu Tsai #define OV5640_TEST_BLACK (3 << 0) 2703a0c29afbSChen-Yu Tsai 2704a0c29afbSChen-Yu Tsai static const u8 test_pattern_val[] = { 2705a0c29afbSChen-Yu Tsai 0, 27062aff1fc3SChen-Yu Tsai OV5640_TEST_ENABLE | OV5640_TEST_BAR_VERT_CHANGE_1 | 2707a0c29afbSChen-Yu Tsai OV5640_TEST_BAR, 2708bddc5cdfSChen-Yu Tsai OV5640_TEST_ENABLE | OV5640_TEST_ROLLING | 2709bddc5cdfSChen-Yu Tsai OV5640_TEST_BAR_VERT_CHANGE_1 | OV5640_TEST_BAR, 2710bddc5cdfSChen-Yu Tsai OV5640_TEST_ENABLE | OV5640_TEST_SQUARE, 2711bddc5cdfSChen-Yu Tsai OV5640_TEST_ENABLE | OV5640_TEST_ROLLING | OV5640_TEST_SQUARE, 2712a0c29afbSChen-Yu Tsai }; 2713a0c29afbSChen-Yu Tsai 271419a81c14SSteve Longerbeam static int ov5640_set_ctrl_test_pattern(struct ov5640_dev *sensor, int value) 271519a81c14SSteve Longerbeam { 2716a0c29afbSChen-Yu Tsai return ov5640_write_reg(sensor, OV5640_REG_PRE_ISP_TEST_SET1, 2717a0c29afbSChen-Yu Tsai test_pattern_val[value]); 271819a81c14SSteve Longerbeam } 271919a81c14SSteve Longerbeam 27201068fecaSMylène Josserand static int ov5640_set_ctrl_light_freq(struct ov5640_dev *sensor, int value) 27211068fecaSMylène Josserand { 27221068fecaSMylène Josserand int ret; 27231068fecaSMylène Josserand 27241068fecaSMylène Josserand ret = ov5640_mod_reg(sensor, OV5640_REG_HZ5060_CTRL01, BIT(7), 27251068fecaSMylène Josserand (value == V4L2_CID_POWER_LINE_FREQUENCY_AUTO) ? 27261068fecaSMylène Josserand 0 : BIT(7)); 27271068fecaSMylène Josserand if (ret) 27281068fecaSMylène Josserand return ret; 27291068fecaSMylène Josserand 27301068fecaSMylène Josserand return ov5640_mod_reg(sensor, OV5640_REG_HZ5060_CTRL00, BIT(2), 27311068fecaSMylène Josserand (value == V4L2_CID_POWER_LINE_FREQUENCY_50HZ) ? 27321068fecaSMylène Josserand BIT(2) : 0); 27331068fecaSMylène Josserand } 27341068fecaSMylène Josserand 2735ce85705aSHugues Fruchet static int ov5640_set_ctrl_hflip(struct ov5640_dev *sensor, int value) 2736ce85705aSHugues Fruchet { 2737ce85705aSHugues Fruchet /* 2738c3f3ba3eSHugues Fruchet * If sensor is mounted upside down, mirror logic is inversed. 2739c3f3ba3eSHugues Fruchet * 2740ce85705aSHugues Fruchet * Sensor is a BSI (Back Side Illuminated) one, 2741ce85705aSHugues Fruchet * so image captured is physically mirrored. 2742ce85705aSHugues Fruchet * This is why mirror logic is inversed in 2743ce85705aSHugues Fruchet * order to cancel this mirror effect. 2744ce85705aSHugues Fruchet */ 2745ce85705aSHugues Fruchet 2746ce85705aSHugues Fruchet /* 2747ce85705aSHugues Fruchet * TIMING TC REG21: 2748ce85705aSHugues Fruchet * - [2]: ISP mirror 2749ce85705aSHugues Fruchet * - [1]: Sensor mirror 2750ce85705aSHugues Fruchet */ 2751ce85705aSHugues Fruchet return ov5640_mod_reg(sensor, OV5640_REG_TIMING_TC_REG21, 2752ce85705aSHugues Fruchet BIT(2) | BIT(1), 2753c3f3ba3eSHugues Fruchet (!(value ^ sensor->upside_down)) ? 2754c3f3ba3eSHugues Fruchet (BIT(2) | BIT(1)) : 0); 2755ce85705aSHugues Fruchet } 2756ce85705aSHugues Fruchet 2757ce85705aSHugues Fruchet static int ov5640_set_ctrl_vflip(struct ov5640_dev *sensor, int value) 2758ce85705aSHugues Fruchet { 2759c3f3ba3eSHugues Fruchet /* If sensor is mounted upside down, flip logic is inversed */ 2760c3f3ba3eSHugues Fruchet 2761ce85705aSHugues Fruchet /* 2762ce85705aSHugues Fruchet * TIMING TC REG20: 2763ce85705aSHugues Fruchet * - [2]: ISP vflip 2764ce85705aSHugues Fruchet * - [1]: Sensor vflip 2765ce85705aSHugues Fruchet */ 2766ce85705aSHugues Fruchet return ov5640_mod_reg(sensor, OV5640_REG_TIMING_TC_REG20, 2767ce85705aSHugues Fruchet BIT(2) | BIT(1), 2768c3f3ba3eSHugues Fruchet (value ^ sensor->upside_down) ? 2769c3f3ba3eSHugues Fruchet (BIT(2) | BIT(1)) : 0); 2770ce85705aSHugues Fruchet } 2771ce85705aSHugues Fruchet 277219a81c14SSteve Longerbeam static int ov5640_g_volatile_ctrl(struct v4l2_ctrl *ctrl) 277319a81c14SSteve Longerbeam { 277419a81c14SSteve Longerbeam struct v4l2_subdev *sd = ctrl_to_sd(ctrl); 277519a81c14SSteve Longerbeam struct ov5640_dev *sensor = to_ov5640_dev(sd); 277619a81c14SSteve Longerbeam int val; 277719a81c14SSteve Longerbeam 277819a81c14SSteve Longerbeam /* v4l2_ctrl_lock() locks our own mutex */ 277919a81c14SSteve Longerbeam 278019a81c14SSteve Longerbeam switch (ctrl->id) { 278119a81c14SSteve Longerbeam case V4L2_CID_AUTOGAIN: 278219a81c14SSteve Longerbeam val = ov5640_get_gain(sensor); 278319a81c14SSteve Longerbeam if (val < 0) 278419a81c14SSteve Longerbeam return val; 278519a81c14SSteve Longerbeam sensor->ctrls.gain->val = val; 278619a81c14SSteve Longerbeam break; 278719a81c14SSteve Longerbeam case V4L2_CID_EXPOSURE_AUTO: 278819a81c14SSteve Longerbeam val = ov5640_get_exposure(sensor); 278919a81c14SSteve Longerbeam if (val < 0) 279019a81c14SSteve Longerbeam return val; 279119a81c14SSteve Longerbeam sensor->ctrls.exposure->val = val; 279219a81c14SSteve Longerbeam break; 279319a81c14SSteve Longerbeam } 279419a81c14SSteve Longerbeam 279519a81c14SSteve Longerbeam return 0; 279619a81c14SSteve Longerbeam } 279719a81c14SSteve Longerbeam 279819a81c14SSteve Longerbeam static int ov5640_s_ctrl(struct v4l2_ctrl *ctrl) 279919a81c14SSteve Longerbeam { 280019a81c14SSteve Longerbeam struct v4l2_subdev *sd = ctrl_to_sd(ctrl); 280119a81c14SSteve Longerbeam struct ov5640_dev *sensor = to_ov5640_dev(sd); 280219a81c14SSteve Longerbeam int ret; 280319a81c14SSteve Longerbeam 280419a81c14SSteve Longerbeam /* v4l2_ctrl_lock() locks our own mutex */ 280519a81c14SSteve Longerbeam 280619a81c14SSteve Longerbeam /* 280719a81c14SSteve Longerbeam * If the device is not powered up by the host driver do 280819a81c14SSteve Longerbeam * not apply any controls to H/W at this time. Instead 280919a81c14SSteve Longerbeam * the controls will be restored right after power-up. 281019a81c14SSteve Longerbeam */ 281119a81c14SSteve Longerbeam if (sensor->power_count == 0) 281219a81c14SSteve Longerbeam return 0; 281319a81c14SSteve Longerbeam 281419a81c14SSteve Longerbeam switch (ctrl->id) { 281519a81c14SSteve Longerbeam case V4L2_CID_AUTOGAIN: 281619a81c14SSteve Longerbeam ret = ov5640_set_ctrl_gain(sensor, ctrl->val); 281719a81c14SSteve Longerbeam break; 281819a81c14SSteve Longerbeam case V4L2_CID_EXPOSURE_AUTO: 281919a81c14SSteve Longerbeam ret = ov5640_set_ctrl_exposure(sensor, ctrl->val); 282019a81c14SSteve Longerbeam break; 282119a81c14SSteve Longerbeam case V4L2_CID_AUTO_WHITE_BALANCE: 282219a81c14SSteve Longerbeam ret = ov5640_set_ctrl_white_balance(sensor, ctrl->val); 282319a81c14SSteve Longerbeam break; 282419a81c14SSteve Longerbeam case V4L2_CID_HUE: 282519a81c14SSteve Longerbeam ret = ov5640_set_ctrl_hue(sensor, ctrl->val); 282619a81c14SSteve Longerbeam break; 282719a81c14SSteve Longerbeam case V4L2_CID_CONTRAST: 282819a81c14SSteve Longerbeam ret = ov5640_set_ctrl_contrast(sensor, ctrl->val); 282919a81c14SSteve Longerbeam break; 283019a81c14SSteve Longerbeam case V4L2_CID_SATURATION: 283119a81c14SSteve Longerbeam ret = ov5640_set_ctrl_saturation(sensor, ctrl->val); 283219a81c14SSteve Longerbeam break; 283319a81c14SSteve Longerbeam case V4L2_CID_TEST_PATTERN: 283419a81c14SSteve Longerbeam ret = ov5640_set_ctrl_test_pattern(sensor, ctrl->val); 283519a81c14SSteve Longerbeam break; 28361068fecaSMylène Josserand case V4L2_CID_POWER_LINE_FREQUENCY: 28371068fecaSMylène Josserand ret = ov5640_set_ctrl_light_freq(sensor, ctrl->val); 28381068fecaSMylène Josserand break; 2839ce85705aSHugues Fruchet case V4L2_CID_HFLIP: 2840ce85705aSHugues Fruchet ret = ov5640_set_ctrl_hflip(sensor, ctrl->val); 2841ce85705aSHugues Fruchet break; 2842ce85705aSHugues Fruchet case V4L2_CID_VFLIP: 2843ce85705aSHugues Fruchet ret = ov5640_set_ctrl_vflip(sensor, ctrl->val); 2844ce85705aSHugues Fruchet break; 284519a81c14SSteve Longerbeam default: 284619a81c14SSteve Longerbeam ret = -EINVAL; 284719a81c14SSteve Longerbeam break; 284819a81c14SSteve Longerbeam } 284919a81c14SSteve Longerbeam 285019a81c14SSteve Longerbeam return ret; 285119a81c14SSteve Longerbeam } 285219a81c14SSteve Longerbeam 285319a81c14SSteve Longerbeam static const struct v4l2_ctrl_ops ov5640_ctrl_ops = { 285419a81c14SSteve Longerbeam .g_volatile_ctrl = ov5640_g_volatile_ctrl, 285519a81c14SSteve Longerbeam .s_ctrl = ov5640_s_ctrl, 285619a81c14SSteve Longerbeam }; 285719a81c14SSteve Longerbeam 285819a81c14SSteve Longerbeam static int ov5640_init_controls(struct ov5640_dev *sensor) 285919a81c14SSteve Longerbeam { 286022845bf2SJacopo Mondi const struct ov5640_mode_info *mode = sensor->current_mode; 286119a81c14SSteve Longerbeam const struct v4l2_ctrl_ops *ops = &ov5640_ctrl_ops; 286219a81c14SSteve Longerbeam struct ov5640_ctrls *ctrls = &sensor->ctrls; 286319a81c14SSteve Longerbeam struct v4l2_ctrl_handler *hdl = &ctrls->handler; 286419a81c14SSteve Longerbeam int ret; 286519a81c14SSteve Longerbeam 286619a81c14SSteve Longerbeam v4l2_ctrl_handler_init(hdl, 32); 286719a81c14SSteve Longerbeam 286819a81c14SSteve Longerbeam /* we can use our own mutex for the ctrl lock */ 286919a81c14SSteve Longerbeam hdl->lock = &sensor->lock; 287019a81c14SSteve Longerbeam 2871cc196e48SBenoit Parrot /* Clock related controls */ 2872cc196e48SBenoit Parrot ctrls->pixel_rate = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_PIXEL_RATE, 287322845bf2SJacopo Mondi ov5640_pixel_rates[OV5640_NUM_PIXEL_RATES - 1], 287422845bf2SJacopo Mondi ov5640_pixel_rates[0], 1, 287522845bf2SJacopo Mondi ov5640_pixel_rates[mode->pixel_rate]); 2876cc196e48SBenoit Parrot 2877*7a3b8d4bSJacopo Mondi ctrls->link_freq = v4l2_ctrl_new_int_menu(hdl, ops, 2878*7a3b8d4bSJacopo Mondi V4L2_CID_LINK_FREQ, 2879*7a3b8d4bSJacopo Mondi ARRAY_SIZE(ov5640_csi2_link_freqs) - 1, 2880*7a3b8d4bSJacopo Mondi OV5640_DEFAULT_LINK_FREQ, 2881*7a3b8d4bSJacopo Mondi ov5640_csi2_link_freqs); 2882*7a3b8d4bSJacopo Mondi 288319a81c14SSteve Longerbeam /* Auto/manual white balance */ 288419a81c14SSteve Longerbeam ctrls->auto_wb = v4l2_ctrl_new_std(hdl, ops, 288519a81c14SSteve Longerbeam V4L2_CID_AUTO_WHITE_BALANCE, 288619a81c14SSteve Longerbeam 0, 1, 1, 1); 288719a81c14SSteve Longerbeam ctrls->blue_balance = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_BLUE_BALANCE, 288819a81c14SSteve Longerbeam 0, 4095, 1, 0); 288919a81c14SSteve Longerbeam ctrls->red_balance = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_RED_BALANCE, 289019a81c14SSteve Longerbeam 0, 4095, 1, 0); 289119a81c14SSteve Longerbeam /* Auto/manual exposure */ 289219a81c14SSteve Longerbeam ctrls->auto_exp = v4l2_ctrl_new_std_menu(hdl, ops, 289319a81c14SSteve Longerbeam V4L2_CID_EXPOSURE_AUTO, 289419a81c14SSteve Longerbeam V4L2_EXPOSURE_MANUAL, 0, 289519a81c14SSteve Longerbeam V4L2_EXPOSURE_AUTO); 289619a81c14SSteve Longerbeam ctrls->exposure = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_EXPOSURE, 289719a81c14SSteve Longerbeam 0, 65535, 1, 0); 289819a81c14SSteve Longerbeam /* Auto/manual gain */ 289919a81c14SSteve Longerbeam ctrls->auto_gain = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_AUTOGAIN, 290019a81c14SSteve Longerbeam 0, 1, 1, 1); 290119a81c14SSteve Longerbeam ctrls->gain = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_GAIN, 290219a81c14SSteve Longerbeam 0, 1023, 1, 0); 290319a81c14SSteve Longerbeam 290419a81c14SSteve Longerbeam ctrls->saturation = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_SATURATION, 290519a81c14SSteve Longerbeam 0, 255, 1, 64); 290619a81c14SSteve Longerbeam ctrls->hue = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_HUE, 290719a81c14SSteve Longerbeam 0, 359, 1, 0); 290819a81c14SSteve Longerbeam ctrls->contrast = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_CONTRAST, 290919a81c14SSteve Longerbeam 0, 255, 1, 0); 291019a81c14SSteve Longerbeam ctrls->test_pattern = 291119a81c14SSteve Longerbeam v4l2_ctrl_new_std_menu_items(hdl, ops, V4L2_CID_TEST_PATTERN, 291219a81c14SSteve Longerbeam ARRAY_SIZE(test_pattern_menu) - 1, 291319a81c14SSteve Longerbeam 0, 0, test_pattern_menu); 2914ce85705aSHugues Fruchet ctrls->hflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_HFLIP, 2915ce85705aSHugues Fruchet 0, 1, 1, 0); 2916ce85705aSHugues Fruchet ctrls->vflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_VFLIP, 2917ce85705aSHugues Fruchet 0, 1, 1, 0); 291819a81c14SSteve Longerbeam 29191068fecaSMylène Josserand ctrls->light_freq = 29201068fecaSMylène Josserand v4l2_ctrl_new_std_menu(hdl, ops, 29211068fecaSMylène Josserand V4L2_CID_POWER_LINE_FREQUENCY, 29221068fecaSMylène Josserand V4L2_CID_POWER_LINE_FREQUENCY_AUTO, 0, 29231068fecaSMylène Josserand V4L2_CID_POWER_LINE_FREQUENCY_50HZ); 29241068fecaSMylène Josserand 292519a81c14SSteve Longerbeam if (hdl->error) { 292619a81c14SSteve Longerbeam ret = hdl->error; 292719a81c14SSteve Longerbeam goto free_ctrls; 292819a81c14SSteve Longerbeam } 292919a81c14SSteve Longerbeam 2930cc196e48SBenoit Parrot ctrls->pixel_rate->flags |= V4L2_CTRL_FLAG_READ_ONLY; 2931*7a3b8d4bSJacopo Mondi ctrls->link_freq->flags |= V4L2_CTRL_FLAG_READ_ONLY; 293219a81c14SSteve Longerbeam ctrls->gain->flags |= V4L2_CTRL_FLAG_VOLATILE; 293319a81c14SSteve Longerbeam ctrls->exposure->flags |= V4L2_CTRL_FLAG_VOLATILE; 293419a81c14SSteve Longerbeam 293519a81c14SSteve Longerbeam v4l2_ctrl_auto_cluster(3, &ctrls->auto_wb, 0, false); 293619a81c14SSteve Longerbeam v4l2_ctrl_auto_cluster(2, &ctrls->auto_gain, 0, true); 293719a81c14SSteve Longerbeam v4l2_ctrl_auto_cluster(2, &ctrls->auto_exp, 1, true); 293819a81c14SSteve Longerbeam 293919a81c14SSteve Longerbeam sensor->sd.ctrl_handler = hdl; 294019a81c14SSteve Longerbeam return 0; 294119a81c14SSteve Longerbeam 294219a81c14SSteve Longerbeam free_ctrls: 294319a81c14SSteve Longerbeam v4l2_ctrl_handler_free(hdl); 294419a81c14SSteve Longerbeam return ret; 294519a81c14SSteve Longerbeam } 294619a81c14SSteve Longerbeam 294719a81c14SSteve Longerbeam static int ov5640_enum_frame_size(struct v4l2_subdev *sd, 29480d346d2aSTomi Valkeinen struct v4l2_subdev_state *sd_state, 294919a81c14SSteve Longerbeam struct v4l2_subdev_frame_size_enum *fse) 295019a81c14SSteve Longerbeam { 295119a81c14SSteve Longerbeam if (fse->pad != 0) 295219a81c14SSteve Longerbeam return -EINVAL; 295319a81c14SSteve Longerbeam if (fse->index >= OV5640_NUM_MODES) 295419a81c14SSteve Longerbeam return -EINVAL; 295519a81c14SSteve Longerbeam 295641d8d7f5SHugues Fruchet fse->min_width = 2957086c25f8SMaxime Ripard ov5640_mode_data[fse->index].hact; 295841d8d7f5SHugues Fruchet fse->max_width = fse->min_width; 295941d8d7f5SHugues Fruchet fse->min_height = 2960086c25f8SMaxime Ripard ov5640_mode_data[fse->index].vact; 296141d8d7f5SHugues Fruchet fse->max_height = fse->min_height; 296219a81c14SSteve Longerbeam 296319a81c14SSteve Longerbeam return 0; 296419a81c14SSteve Longerbeam } 296519a81c14SSteve Longerbeam 296619a81c14SSteve Longerbeam static int ov5640_enum_frame_interval( 296719a81c14SSteve Longerbeam struct v4l2_subdev *sd, 29680d346d2aSTomi Valkeinen struct v4l2_subdev_state *sd_state, 296919a81c14SSteve Longerbeam struct v4l2_subdev_frame_interval_enum *fie) 297019a81c14SSteve Longerbeam { 297119a81c14SSteve Longerbeam struct ov5640_dev *sensor = to_ov5640_dev(sd); 297219a81c14SSteve Longerbeam struct v4l2_fract tpf; 297319a81c14SSteve Longerbeam int ret; 297419a81c14SSteve Longerbeam 297519a81c14SSteve Longerbeam if (fie->pad != 0) 297619a81c14SSteve Longerbeam return -EINVAL; 297719a81c14SSteve Longerbeam if (fie->index >= OV5640_NUM_FRAMERATES) 297819a81c14SSteve Longerbeam return -EINVAL; 297919a81c14SSteve Longerbeam 298019a81c14SSteve Longerbeam tpf.numerator = 1; 298119a81c14SSteve Longerbeam tpf.denominator = ov5640_framerates[fie->index]; 298219a81c14SSteve Longerbeam 298319a81c14SSteve Longerbeam ret = ov5640_try_frame_interval(sensor, &tpf, 298419a81c14SSteve Longerbeam fie->width, fie->height); 298519a81c14SSteve Longerbeam if (ret < 0) 298619a81c14SSteve Longerbeam return -EINVAL; 298719a81c14SSteve Longerbeam 298819a81c14SSteve Longerbeam fie->interval = tpf; 298919a81c14SSteve Longerbeam return 0; 299019a81c14SSteve Longerbeam } 299119a81c14SSteve Longerbeam 299219a81c14SSteve Longerbeam static int ov5640_g_frame_interval(struct v4l2_subdev *sd, 299319a81c14SSteve Longerbeam struct v4l2_subdev_frame_interval *fi) 299419a81c14SSteve Longerbeam { 299519a81c14SSteve Longerbeam struct ov5640_dev *sensor = to_ov5640_dev(sd); 299619a81c14SSteve Longerbeam 299719a81c14SSteve Longerbeam mutex_lock(&sensor->lock); 299819a81c14SSteve Longerbeam fi->interval = sensor->frame_interval; 299919a81c14SSteve Longerbeam mutex_unlock(&sensor->lock); 300019a81c14SSteve Longerbeam 300119a81c14SSteve Longerbeam return 0; 300219a81c14SSteve Longerbeam } 300319a81c14SSteve Longerbeam 300419a81c14SSteve Longerbeam static int ov5640_s_frame_interval(struct v4l2_subdev *sd, 300519a81c14SSteve Longerbeam struct v4l2_subdev_frame_interval *fi) 300619a81c14SSteve Longerbeam { 300719a81c14SSteve Longerbeam struct ov5640_dev *sensor = to_ov5640_dev(sd); 300819a81c14SSteve Longerbeam const struct ov5640_mode_info *mode; 300919a81c14SSteve Longerbeam int frame_rate, ret = 0; 301019a81c14SSteve Longerbeam 301119a81c14SSteve Longerbeam if (fi->pad != 0) 301219a81c14SSteve Longerbeam return -EINVAL; 301319a81c14SSteve Longerbeam 301419a81c14SSteve Longerbeam mutex_lock(&sensor->lock); 301519a81c14SSteve Longerbeam 301619a81c14SSteve Longerbeam if (sensor->streaming) { 301719a81c14SSteve Longerbeam ret = -EBUSY; 301819a81c14SSteve Longerbeam goto out; 301919a81c14SSteve Longerbeam } 302019a81c14SSteve Longerbeam 302119a81c14SSteve Longerbeam mode = sensor->current_mode; 302219a81c14SSteve Longerbeam 302319a81c14SSteve Longerbeam frame_rate = ov5640_try_frame_interval(sensor, &fi->interval, 3024dba13a0bSMaxime Ripard mode->hact, mode->vact); 3025e823fb16SMaxime Ripard if (frame_rate < 0) { 3026e823fb16SMaxime Ripard /* Always return a valid frame interval value */ 3027e823fb16SMaxime Ripard fi->interval = sensor->frame_interval; 3028e823fb16SMaxime Ripard goto out; 3029e823fb16SMaxime Ripard } 303019a81c14SSteve Longerbeam 30313c4a7372SHugues Fruchet mode = ov5640_find_mode(sensor, frame_rate, mode->hact, 3032dba13a0bSMaxime Ripard mode->vact, true); 30333c4a7372SHugues Fruchet if (!mode) { 30343c4a7372SHugues Fruchet ret = -EINVAL; 30353c4a7372SHugues Fruchet goto out; 30363c4a7372SHugues Fruchet } 30373c4a7372SHugues Fruchet 30380929983eSHugues Fruchet if (mode != sensor->current_mode || 30390929983eSHugues Fruchet frame_rate != sensor->current_fr) { 30400929983eSHugues Fruchet sensor->current_fr = frame_rate; 30410929983eSHugues Fruchet sensor->frame_interval = fi->interval; 30423c4a7372SHugues Fruchet sensor->current_mode = mode; 304319a81c14SSteve Longerbeam sensor->pending_mode_change = true; 3044cc196e48SBenoit Parrot 3045cc196e48SBenoit Parrot __v4l2_ctrl_s_ctrl_int64(sensor->ctrls.pixel_rate, 3046cc196e48SBenoit Parrot ov5640_calc_pixel_rate(sensor)); 30476949d864SHugues Fruchet } 304819a81c14SSteve Longerbeam out: 304919a81c14SSteve Longerbeam mutex_unlock(&sensor->lock); 305019a81c14SSteve Longerbeam return ret; 305119a81c14SSteve Longerbeam } 305219a81c14SSteve Longerbeam 305319a81c14SSteve Longerbeam static int ov5640_enum_mbus_code(struct v4l2_subdev *sd, 30540d346d2aSTomi Valkeinen struct v4l2_subdev_state *sd_state, 305519a81c14SSteve Longerbeam struct v4l2_subdev_mbus_code_enum *code) 305619a81c14SSteve Longerbeam { 305719a81c14SSteve Longerbeam if (code->pad != 0) 305819a81c14SSteve Longerbeam return -EINVAL; 3059e3ee691dSHugues Fruchet if (code->index >= ARRAY_SIZE(ov5640_formats)) 306019a81c14SSteve Longerbeam return -EINVAL; 306119a81c14SSteve Longerbeam 3062e3ee691dSHugues Fruchet code->code = ov5640_formats[code->index].code; 306319a81c14SSteve Longerbeam return 0; 306419a81c14SSteve Longerbeam } 306519a81c14SSteve Longerbeam 306619a81c14SSteve Longerbeam static int ov5640_s_stream(struct v4l2_subdev *sd, int enable) 306719a81c14SSteve Longerbeam { 306819a81c14SSteve Longerbeam struct ov5640_dev *sensor = to_ov5640_dev(sd); 306919a81c14SSteve Longerbeam int ret = 0; 307019a81c14SSteve Longerbeam 307119a81c14SSteve Longerbeam mutex_lock(&sensor->lock); 307219a81c14SSteve Longerbeam 307319a81c14SSteve Longerbeam if (sensor->streaming == !enable) { 307419a81c14SSteve Longerbeam if (enable && sensor->pending_mode_change) { 3075985cdcb0SHugues Fruchet ret = ov5640_set_mode(sensor); 307619a81c14SSteve Longerbeam if (ret) 307719a81c14SSteve Longerbeam goto out; 3078fb98e29fSHugues Fruchet } 3079e3ee691dSHugues Fruchet 3080fb98e29fSHugues Fruchet if (enable && sensor->pending_fmt_change) { 3081e3ee691dSHugues Fruchet ret = ov5640_set_framefmt(sensor, &sensor->fmt); 3082e3ee691dSHugues Fruchet if (ret) 3083e3ee691dSHugues Fruchet goto out; 3084fb98e29fSHugues Fruchet sensor->pending_fmt_change = false; 308519a81c14SSteve Longerbeam } 308619a81c14SSteve Longerbeam 30878e823f5cSJacopo Mondi if (ov5640_is_csi2(sensor)) 3088f22996dbSHugues Fruchet ret = ov5640_set_stream_mipi(sensor, enable); 3089f22996dbSHugues Fruchet else 3090f22996dbSHugues Fruchet ret = ov5640_set_stream_dvp(sensor, enable); 3091f22996dbSHugues Fruchet 309219a81c14SSteve Longerbeam if (!ret) 309319a81c14SSteve Longerbeam sensor->streaming = enable; 309419a81c14SSteve Longerbeam } 309519a81c14SSteve Longerbeam out: 309619a81c14SSteve Longerbeam mutex_unlock(&sensor->lock); 309719a81c14SSteve Longerbeam return ret; 309819a81c14SSteve Longerbeam } 309919a81c14SSteve Longerbeam 310019a81c14SSteve Longerbeam static const struct v4l2_subdev_core_ops ov5640_core_ops = { 310119a81c14SSteve Longerbeam .s_power = ov5640_s_power, 31022d18fbc5SAkinobu Mita .log_status = v4l2_ctrl_subdev_log_status, 31032d18fbc5SAkinobu Mita .subscribe_event = v4l2_ctrl_subdev_subscribe_event, 31042d18fbc5SAkinobu Mita .unsubscribe_event = v4l2_event_subdev_unsubscribe, 310519a81c14SSteve Longerbeam }; 310619a81c14SSteve Longerbeam 310719a81c14SSteve Longerbeam static const struct v4l2_subdev_video_ops ov5640_video_ops = { 310819a81c14SSteve Longerbeam .g_frame_interval = ov5640_g_frame_interval, 310919a81c14SSteve Longerbeam .s_frame_interval = ov5640_s_frame_interval, 311019a81c14SSteve Longerbeam .s_stream = ov5640_s_stream, 311119a81c14SSteve Longerbeam }; 311219a81c14SSteve Longerbeam 311319a81c14SSteve Longerbeam static const struct v4l2_subdev_pad_ops ov5640_pad_ops = { 311419a81c14SSteve Longerbeam .enum_mbus_code = ov5640_enum_mbus_code, 311519a81c14SSteve Longerbeam .get_fmt = ov5640_get_fmt, 311619a81c14SSteve Longerbeam .set_fmt = ov5640_set_fmt, 311719a81c14SSteve Longerbeam .enum_frame_size = ov5640_enum_frame_size, 311819a81c14SSteve Longerbeam .enum_frame_interval = ov5640_enum_frame_interval, 311919a81c14SSteve Longerbeam }; 312019a81c14SSteve Longerbeam 312119a81c14SSteve Longerbeam static const struct v4l2_subdev_ops ov5640_subdev_ops = { 312219a81c14SSteve Longerbeam .core = &ov5640_core_ops, 312319a81c14SSteve Longerbeam .video = &ov5640_video_ops, 312419a81c14SSteve Longerbeam .pad = &ov5640_pad_ops, 312519a81c14SSteve Longerbeam }; 312619a81c14SSteve Longerbeam 312719a81c14SSteve Longerbeam static int ov5640_get_regulators(struct ov5640_dev *sensor) 312819a81c14SSteve Longerbeam { 312919a81c14SSteve Longerbeam int i; 313019a81c14SSteve Longerbeam 313119a81c14SSteve Longerbeam for (i = 0; i < OV5640_NUM_SUPPLIES; i++) 313219a81c14SSteve Longerbeam sensor->supplies[i].supply = ov5640_supply_name[i]; 313319a81c14SSteve Longerbeam 313419a81c14SSteve Longerbeam return devm_regulator_bulk_get(&sensor->i2c_client->dev, 313519a81c14SSteve Longerbeam OV5640_NUM_SUPPLIES, 313619a81c14SSteve Longerbeam sensor->supplies); 313719a81c14SSteve Longerbeam } 313819a81c14SSteve Longerbeam 31390f7acb52SHugues Fruchet static int ov5640_check_chip_id(struct ov5640_dev *sensor) 31400f7acb52SHugues Fruchet { 31410f7acb52SHugues Fruchet struct i2c_client *client = sensor->i2c_client; 31420f7acb52SHugues Fruchet int ret = 0; 31430f7acb52SHugues Fruchet u16 chip_id; 31440f7acb52SHugues Fruchet 31450f7acb52SHugues Fruchet ret = ov5640_set_power_on(sensor); 31460f7acb52SHugues Fruchet if (ret) 31470f7acb52SHugues Fruchet return ret; 31480f7acb52SHugues Fruchet 31490f7acb52SHugues Fruchet ret = ov5640_read_reg16(sensor, OV5640_REG_CHIP_ID, &chip_id); 31500f7acb52SHugues Fruchet if (ret) { 31510f7acb52SHugues Fruchet dev_err(&client->dev, "%s: failed to read chip identifier\n", 31520f7acb52SHugues Fruchet __func__); 31530f7acb52SHugues Fruchet goto power_off; 31540f7acb52SHugues Fruchet } 31550f7acb52SHugues Fruchet 31560f7acb52SHugues Fruchet if (chip_id != 0x5640) { 31570f7acb52SHugues Fruchet dev_err(&client->dev, "%s: wrong chip identifier, expected 0x5640, got 0x%x\n", 31580f7acb52SHugues Fruchet __func__, chip_id); 31590f7acb52SHugues Fruchet ret = -ENXIO; 31600f7acb52SHugues Fruchet } 31610f7acb52SHugues Fruchet 31620f7acb52SHugues Fruchet power_off: 31630f7acb52SHugues Fruchet ov5640_set_power_off(sensor); 31640f7acb52SHugues Fruchet return ret; 31650f7acb52SHugues Fruchet } 31660f7acb52SHugues Fruchet 3167e6714993SKieran Bingham static int ov5640_probe(struct i2c_client *client) 316819a81c14SSteve Longerbeam { 316919a81c14SSteve Longerbeam struct device *dev = &client->dev; 317019a81c14SSteve Longerbeam struct fwnode_handle *endpoint; 317119a81c14SSteve Longerbeam struct ov5640_dev *sensor; 3172e6441fdeSHugues Fruchet struct v4l2_mbus_framefmt *fmt; 3173c3f3ba3eSHugues Fruchet u32 rotation; 317419a81c14SSteve Longerbeam int ret; 317519a81c14SSteve Longerbeam 317619a81c14SSteve Longerbeam sensor = devm_kzalloc(dev, sizeof(*sensor), GFP_KERNEL); 317719a81c14SSteve Longerbeam if (!sensor) 317819a81c14SSteve Longerbeam return -ENOMEM; 317919a81c14SSteve Longerbeam 318019a81c14SSteve Longerbeam sensor->i2c_client = client; 3181fb98e29fSHugues Fruchet 3182fb98e29fSHugues Fruchet /* 3183fb98e29fSHugues Fruchet * default init sequence initialize sensor to 3184fb98e29fSHugues Fruchet * YUV422 UYVY VGA@30fps 3185fb98e29fSHugues Fruchet */ 3186e6441fdeSHugues Fruchet fmt = &sensor->fmt; 3187fb98e29fSHugues Fruchet fmt->code = MEDIA_BUS_FMT_UYVY8_2X8; 3188fb98e29fSHugues Fruchet fmt->colorspace = V4L2_COLORSPACE_SRGB; 3189e6441fdeSHugues Fruchet fmt->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(fmt->colorspace); 3190e6441fdeSHugues Fruchet fmt->quantization = V4L2_QUANTIZATION_FULL_RANGE; 3191e6441fdeSHugues Fruchet fmt->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(fmt->colorspace); 3192e6441fdeSHugues Fruchet fmt->width = 640; 3193e6441fdeSHugues Fruchet fmt->height = 480; 3194e6441fdeSHugues Fruchet fmt->field = V4L2_FIELD_NONE; 319519a81c14SSteve Longerbeam sensor->frame_interval.numerator = 1; 319619a81c14SSteve Longerbeam sensor->frame_interval.denominator = ov5640_framerates[OV5640_30_FPS]; 319719a81c14SSteve Longerbeam sensor->current_fr = OV5640_30_FPS; 319819a81c14SSteve Longerbeam sensor->current_mode = 3199086c25f8SMaxime Ripard &ov5640_mode_data[OV5640_MODE_VGA_640_480]; 3200985cdcb0SHugues Fruchet sensor->last_mode = sensor->current_mode; 320119a81c14SSteve Longerbeam 320219a81c14SSteve Longerbeam sensor->ae_target = 52; 320319a81c14SSteve Longerbeam 3204c3f3ba3eSHugues Fruchet /* optional indication of physical rotation of sensor */ 3205c3f3ba3eSHugues Fruchet ret = fwnode_property_read_u32(dev_fwnode(&client->dev), "rotation", 3206c3f3ba3eSHugues Fruchet &rotation); 3207c3f3ba3eSHugues Fruchet if (!ret) { 3208c3f3ba3eSHugues Fruchet switch (rotation) { 3209c3f3ba3eSHugues Fruchet case 180: 3210c3f3ba3eSHugues Fruchet sensor->upside_down = true; 32111771e9fbSGustavo A. R. Silva fallthrough; 3212c3f3ba3eSHugues Fruchet case 0: 3213c3f3ba3eSHugues Fruchet break; 3214c3f3ba3eSHugues Fruchet default: 3215c3f3ba3eSHugues Fruchet dev_warn(dev, "%u degrees rotation is not supported, ignoring...\n", 3216c3f3ba3eSHugues Fruchet rotation); 3217c3f3ba3eSHugues Fruchet } 3218c3f3ba3eSHugues Fruchet } 3219c3f3ba3eSHugues Fruchet 3220ce96bcf5SSakari Ailus endpoint = fwnode_graph_get_next_endpoint(dev_fwnode(&client->dev), 3221ce96bcf5SSakari Ailus NULL); 322219a81c14SSteve Longerbeam if (!endpoint) { 322319a81c14SSteve Longerbeam dev_err(dev, "endpoint node not found\n"); 322419a81c14SSteve Longerbeam return -EINVAL; 322519a81c14SSteve Longerbeam } 322619a81c14SSteve Longerbeam 322719a81c14SSteve Longerbeam ret = v4l2_fwnode_endpoint_parse(endpoint, &sensor->ep); 322819a81c14SSteve Longerbeam fwnode_handle_put(endpoint); 322919a81c14SSteve Longerbeam if (ret) { 323019a81c14SSteve Longerbeam dev_err(dev, "Could not parse endpoint\n"); 323119a81c14SSteve Longerbeam return ret; 323219a81c14SSteve Longerbeam } 323319a81c14SSteve Longerbeam 32342c61e48dSLad Prabhakar if (sensor->ep.bus_type != V4L2_MBUS_PARALLEL && 32352c61e48dSLad Prabhakar sensor->ep.bus_type != V4L2_MBUS_CSI2_DPHY && 32362c61e48dSLad Prabhakar sensor->ep.bus_type != V4L2_MBUS_BT656) { 32372c61e48dSLad Prabhakar dev_err(dev, "Unsupported bus type %d\n", sensor->ep.bus_type); 32382c61e48dSLad Prabhakar return -EINVAL; 32392c61e48dSLad Prabhakar } 32402c61e48dSLad Prabhakar 324119a81c14SSteve Longerbeam /* get system clock (xclk) */ 324219a81c14SSteve Longerbeam sensor->xclk = devm_clk_get(dev, "xclk"); 324319a81c14SSteve Longerbeam if (IS_ERR(sensor->xclk)) { 324419a81c14SSteve Longerbeam dev_err(dev, "failed to get xclk\n"); 324519a81c14SSteve Longerbeam return PTR_ERR(sensor->xclk); 324619a81c14SSteve Longerbeam } 324719a81c14SSteve Longerbeam 324819a81c14SSteve Longerbeam sensor->xclk_freq = clk_get_rate(sensor->xclk); 324919a81c14SSteve Longerbeam if (sensor->xclk_freq < OV5640_XCLK_MIN || 325019a81c14SSteve Longerbeam sensor->xclk_freq > OV5640_XCLK_MAX) { 325119a81c14SSteve Longerbeam dev_err(dev, "xclk frequency out of range: %d Hz\n", 325219a81c14SSteve Longerbeam sensor->xclk_freq); 325319a81c14SSteve Longerbeam return -EINVAL; 325419a81c14SSteve Longerbeam } 325519a81c14SSteve Longerbeam 325619a81c14SSteve Longerbeam /* request optional power down pin */ 325719a81c14SSteve Longerbeam sensor->pwdn_gpio = devm_gpiod_get_optional(dev, "powerdown", 325819a81c14SSteve Longerbeam GPIOD_OUT_HIGH); 32598791a102SFabio Estevam if (IS_ERR(sensor->pwdn_gpio)) 32608791a102SFabio Estevam return PTR_ERR(sensor->pwdn_gpio); 32618791a102SFabio Estevam 326219a81c14SSteve Longerbeam /* request optional reset pin */ 326319a81c14SSteve Longerbeam sensor->reset_gpio = devm_gpiod_get_optional(dev, "reset", 326419a81c14SSteve Longerbeam GPIOD_OUT_HIGH); 32658791a102SFabio Estevam if (IS_ERR(sensor->reset_gpio)) 32668791a102SFabio Estevam return PTR_ERR(sensor->reset_gpio); 326719a81c14SSteve Longerbeam 326819a81c14SSteve Longerbeam v4l2_i2c_subdev_init(&sensor->sd, client, &ov5640_subdev_ops); 326919a81c14SSteve Longerbeam 32702d18fbc5SAkinobu Mita sensor->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE | 32712d18fbc5SAkinobu Mita V4L2_SUBDEV_FL_HAS_EVENTS; 327219a81c14SSteve Longerbeam sensor->pad.flags = MEDIA_PAD_FL_SOURCE; 327319a81c14SSteve Longerbeam sensor->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR; 327419a81c14SSteve Longerbeam ret = media_entity_pads_init(&sensor->sd.entity, 1, &sensor->pad); 327519a81c14SSteve Longerbeam if (ret) 327619a81c14SSteve Longerbeam return ret; 327719a81c14SSteve Longerbeam 327819a81c14SSteve Longerbeam ret = ov5640_get_regulators(sensor); 327919a81c14SSteve Longerbeam if (ret) 328019a81c14SSteve Longerbeam return ret; 328119a81c14SSteve Longerbeam 328219a81c14SSteve Longerbeam mutex_init(&sensor->lock); 328319a81c14SSteve Longerbeam 32840f7acb52SHugues Fruchet ret = ov5640_check_chip_id(sensor); 32850f7acb52SHugues Fruchet if (ret) 32860f7acb52SHugues Fruchet goto entity_cleanup; 32870f7acb52SHugues Fruchet 328819a81c14SSteve Longerbeam ret = ov5640_init_controls(sensor); 328919a81c14SSteve Longerbeam if (ret) 329019a81c14SSteve Longerbeam goto entity_cleanup; 329119a81c14SSteve Longerbeam 329215786f7bSSakari Ailus ret = v4l2_async_register_subdev_sensor(&sensor->sd); 329319a81c14SSteve Longerbeam if (ret) 329419a81c14SSteve Longerbeam goto free_ctrls; 329519a81c14SSteve Longerbeam 329619a81c14SSteve Longerbeam return 0; 329719a81c14SSteve Longerbeam 329819a81c14SSteve Longerbeam free_ctrls: 329919a81c14SSteve Longerbeam v4l2_ctrl_handler_free(&sensor->ctrls.handler); 330019a81c14SSteve Longerbeam entity_cleanup: 330119a81c14SSteve Longerbeam media_entity_cleanup(&sensor->sd.entity); 3302bfcba38dSTomi Valkeinen mutex_destroy(&sensor->lock); 330319a81c14SSteve Longerbeam return ret; 330419a81c14SSteve Longerbeam } 330519a81c14SSteve Longerbeam 330619a81c14SSteve Longerbeam static int ov5640_remove(struct i2c_client *client) 330719a81c14SSteve Longerbeam { 330819a81c14SSteve Longerbeam struct v4l2_subdev *sd = i2c_get_clientdata(client); 330919a81c14SSteve Longerbeam struct ov5640_dev *sensor = to_ov5640_dev(sd); 331019a81c14SSteve Longerbeam 331119a81c14SSteve Longerbeam v4l2_async_unregister_subdev(&sensor->sd); 331219a81c14SSteve Longerbeam media_entity_cleanup(&sensor->sd.entity); 331319a81c14SSteve Longerbeam v4l2_ctrl_handler_free(&sensor->ctrls.handler); 3314bfcba38dSTomi Valkeinen mutex_destroy(&sensor->lock); 331519a81c14SSteve Longerbeam 331619a81c14SSteve Longerbeam return 0; 331719a81c14SSteve Longerbeam } 331819a81c14SSteve Longerbeam 331919a81c14SSteve Longerbeam static const struct i2c_device_id ov5640_id[] = { 332019a81c14SSteve Longerbeam {"ov5640", 0}, 332119a81c14SSteve Longerbeam {}, 332219a81c14SSteve Longerbeam }; 332319a81c14SSteve Longerbeam MODULE_DEVICE_TABLE(i2c, ov5640_id); 332419a81c14SSteve Longerbeam 332519a81c14SSteve Longerbeam static const struct of_device_id ov5640_dt_ids[] = { 332619a81c14SSteve Longerbeam { .compatible = "ovti,ov5640" }, 332719a81c14SSteve Longerbeam { /* sentinel */ } 332819a81c14SSteve Longerbeam }; 332919a81c14SSteve Longerbeam MODULE_DEVICE_TABLE(of, ov5640_dt_ids); 333019a81c14SSteve Longerbeam 333119a81c14SSteve Longerbeam static struct i2c_driver ov5640_i2c_driver = { 333219a81c14SSteve Longerbeam .driver = { 333319a81c14SSteve Longerbeam .name = "ov5640", 333419a81c14SSteve Longerbeam .of_match_table = ov5640_dt_ids, 333519a81c14SSteve Longerbeam }, 333619a81c14SSteve Longerbeam .id_table = ov5640_id, 3337e6714993SKieran Bingham .probe_new = ov5640_probe, 333819a81c14SSteve Longerbeam .remove = ov5640_remove, 333919a81c14SSteve Longerbeam }; 334019a81c14SSteve Longerbeam 334119a81c14SSteve Longerbeam module_i2c_driver(ov5640_i2c_driver); 334219a81c14SSteve Longerbeam 334319a81c14SSteve Longerbeam MODULE_DESCRIPTION("OV5640 MIPI Camera Subdev Driver"); 334419a81c14SSteve Longerbeam MODULE_LICENSE("GPL"); 3345