linux/drivers/media/i2c/ov5640.c
<<
>>
Prefs
   1/*
   2 * Copyright (C) 2011-2013 Freescale Semiconductor, Inc. All Rights Reserved.
   3 * Copyright (C) 2014-2017 Mentor Graphics Inc.
   4 *
   5 * This program is free software; you can redistribute it and/or modify
   6 * it under the terms of the GNU General Public License as published by
   7 * the Free Software Foundation; either version 2 of the License, or
   8 * (at your option) any later version.
   9 */
  10
  11#include <linux/clk.h>
  12#include <linux/clk-provider.h>
  13#include <linux/clkdev.h>
  14#include <linux/ctype.h>
  15#include <linux/delay.h>
  16#include <linux/device.h>
  17#include <linux/gpio/consumer.h>
  18#include <linux/i2c.h>
  19#include <linux/init.h>
  20#include <linux/module.h>
  21#include <linux/of_device.h>
  22#include <linux/regulator/consumer.h>
  23#include <linux/slab.h>
  24#include <linux/types.h>
  25#include <media/v4l2-async.h>
  26#include <media/v4l2-ctrls.h>
  27#include <media/v4l2-device.h>
  28#include <media/v4l2-fwnode.h>
  29#include <media/v4l2-subdev.h>
  30
  31/* min/typical/max system clock (xclk) frequencies */
  32#define OV5640_XCLK_MIN  6000000
  33#define OV5640_XCLK_MAX 24000000
  34
  35#define OV5640_DEFAULT_SLAVE_ID 0x3c
  36
  37#define OV5640_REG_SYS_RESET02          0x3002
  38#define OV5640_REG_SYS_CLOCK_ENABLE02   0x3006
  39#define OV5640_REG_SYS_CTRL0            0x3008
  40#define OV5640_REG_CHIP_ID              0x300a
  41#define OV5640_REG_IO_MIPI_CTRL00       0x300e
  42#define OV5640_REG_PAD_OUTPUT_ENABLE01  0x3017
  43#define OV5640_REG_PAD_OUTPUT_ENABLE02  0x3018
  44#define OV5640_REG_PAD_OUTPUT00         0x3019
  45#define OV5640_REG_SYSTEM_CONTROL1      0x302e
  46#define OV5640_REG_SC_PLL_CTRL0         0x3034
  47#define OV5640_REG_SC_PLL_CTRL1         0x3035
  48#define OV5640_REG_SC_PLL_CTRL2         0x3036
  49#define OV5640_REG_SC_PLL_CTRL3         0x3037
  50#define OV5640_REG_SLAVE_ID             0x3100
  51#define OV5640_REG_SCCB_SYS_CTRL1       0x3103
  52#define OV5640_REG_SYS_ROOT_DIVIDER     0x3108
  53#define OV5640_REG_AWB_R_GAIN           0x3400
  54#define OV5640_REG_AWB_G_GAIN           0x3402
  55#define OV5640_REG_AWB_B_GAIN           0x3404
  56#define OV5640_REG_AWB_MANUAL_CTRL      0x3406
  57#define OV5640_REG_AEC_PK_EXPOSURE_HI   0x3500
  58#define OV5640_REG_AEC_PK_EXPOSURE_MED  0x3501
  59#define OV5640_REG_AEC_PK_EXPOSURE_LO   0x3502
  60#define OV5640_REG_AEC_PK_MANUAL        0x3503
  61#define OV5640_REG_AEC_PK_REAL_GAIN     0x350a
  62#define OV5640_REG_AEC_PK_VTS           0x350c
  63#define OV5640_REG_TIMING_HTS           0x380c
  64#define OV5640_REG_TIMING_VTS           0x380e
  65#define OV5640_REG_TIMING_TC_REG21      0x3821
  66#define OV5640_REG_AEC_CTRL00           0x3a00
  67#define OV5640_REG_AEC_B50_STEP         0x3a08
  68#define OV5640_REG_AEC_B60_STEP         0x3a0a
  69#define OV5640_REG_AEC_CTRL0D           0x3a0d
  70#define OV5640_REG_AEC_CTRL0E           0x3a0e
  71#define OV5640_REG_AEC_CTRL0F           0x3a0f
  72#define OV5640_REG_AEC_CTRL10           0x3a10
  73#define OV5640_REG_AEC_CTRL11           0x3a11
  74#define OV5640_REG_AEC_CTRL1B           0x3a1b
  75#define OV5640_REG_AEC_CTRL1E           0x3a1e
  76#define OV5640_REG_AEC_CTRL1F           0x3a1f
  77#define OV5640_REG_HZ5060_CTRL00        0x3c00
  78#define OV5640_REG_HZ5060_CTRL01        0x3c01
  79#define OV5640_REG_SIGMADELTA_CTRL0C    0x3c0c
  80#define OV5640_REG_FRAME_CTRL01         0x4202
  81#define OV5640_REG_FORMAT_CONTROL00     0x4300
  82#define OV5640_REG_POLARITY_CTRL00      0x4740
  83#define OV5640_REG_MIPI_CTRL00          0x4800
  84#define OV5640_REG_DEBUG_MODE           0x4814
  85#define OV5640_REG_ISP_FORMAT_MUX_CTRL  0x501f
  86#define OV5640_REG_PRE_ISP_TEST_SET1    0x503d
  87#define OV5640_REG_SDE_CTRL0            0x5580
  88#define OV5640_REG_SDE_CTRL1            0x5581
  89#define OV5640_REG_SDE_CTRL3            0x5583
  90#define OV5640_REG_SDE_CTRL4            0x5584
  91#define OV5640_REG_SDE_CTRL5            0x5585
  92#define OV5640_REG_AVG_READOUT          0x56a1
  93
  94enum ov5640_mode_id {
  95        OV5640_MODE_QCIF_176_144 = 0,
  96        OV5640_MODE_QVGA_320_240,
  97        OV5640_MODE_VGA_640_480,
  98        OV5640_MODE_NTSC_720_480,
  99        OV5640_MODE_PAL_720_576,
 100        OV5640_MODE_XGA_1024_768,
 101        OV5640_MODE_720P_1280_720,
 102        OV5640_MODE_1080P_1920_1080,
 103        OV5640_MODE_QSXGA_2592_1944,
 104        OV5640_NUM_MODES,
 105};
 106
 107enum ov5640_frame_rate {
 108        OV5640_15_FPS = 0,
 109        OV5640_30_FPS,
 110        OV5640_NUM_FRAMERATES,
 111};
 112
 113struct ov5640_pixfmt {
 114        u32 code;
 115        u32 colorspace;
 116};
 117
 118static const struct ov5640_pixfmt ov5640_formats[] = {
 119        { MEDIA_BUS_FMT_JPEG_1X8, V4L2_COLORSPACE_JPEG, },
 120        { MEDIA_BUS_FMT_UYVY8_2X8, V4L2_COLORSPACE_SRGB, },
 121        { MEDIA_BUS_FMT_YUYV8_2X8, V4L2_COLORSPACE_SRGB, },
 122        { MEDIA_BUS_FMT_RGB565_2X8_LE, V4L2_COLORSPACE_SRGB, },
 123        { MEDIA_BUS_FMT_RGB565_2X8_BE, V4L2_COLORSPACE_SRGB, },
 124};
 125
 126/*
 127 * FIXME: remove this when a subdev API becomes available
 128 * to set the MIPI CSI-2 virtual channel.
 129 */
 130static unsigned int virtual_channel;
 131module_param(virtual_channel, uint, 0444);
 132MODULE_PARM_DESC(virtual_channel,
 133                 "MIPI CSI-2 virtual channel (0..3), default 0");
 134
 135static const int ov5640_framerates[] = {
 136        [OV5640_15_FPS] = 15,
 137        [OV5640_30_FPS] = 30,
 138};
 139
 140/* regulator supplies */
 141static const char * const ov5640_supply_name[] = {
 142        "DOVDD", /* Digital I/O (1.8V) supply */
 143        "DVDD",  /* Digital Core (1.5V) supply */
 144        "AVDD",  /* Analog (2.8V) supply */
 145};
 146
 147#define OV5640_NUM_SUPPLIES ARRAY_SIZE(ov5640_supply_name)
 148
 149/*
 150 * Image size under 1280 * 960 are SUBSAMPLING
 151 * Image size upper 1280 * 960 are SCALING
 152 */
 153enum ov5640_downsize_mode {
 154        SUBSAMPLING,
 155        SCALING,
 156};
 157
 158struct reg_value {
 159        u16 reg_addr;
 160        u8 val;
 161        u8 mask;
 162        u32 delay_ms;
 163};
 164
 165struct ov5640_mode_info {
 166        enum ov5640_mode_id id;
 167        enum ov5640_downsize_mode dn_mode;
 168        u32 width;
 169        u32 height;
 170        const struct reg_value *reg_data;
 171        u32 reg_data_size;
 172};
 173
 174struct ov5640_ctrls {
 175        struct v4l2_ctrl_handler handler;
 176        struct {
 177                struct v4l2_ctrl *auto_exp;
 178                struct v4l2_ctrl *exposure;
 179        };
 180        struct {
 181                struct v4l2_ctrl *auto_wb;
 182                struct v4l2_ctrl *blue_balance;
 183                struct v4l2_ctrl *red_balance;
 184        };
 185        struct {
 186                struct v4l2_ctrl *auto_gain;
 187                struct v4l2_ctrl *gain;
 188        };
 189        struct v4l2_ctrl *brightness;
 190        struct v4l2_ctrl *saturation;
 191        struct v4l2_ctrl *contrast;
 192        struct v4l2_ctrl *hue;
 193        struct v4l2_ctrl *test_pattern;
 194};
 195
 196struct ov5640_dev {
 197        struct i2c_client *i2c_client;
 198        struct v4l2_subdev sd;
 199        struct media_pad pad;
 200        struct v4l2_fwnode_endpoint ep; /* the parsed DT endpoint info */
 201        struct clk *xclk; /* system clock to OV5640 */
 202        u32 xclk_freq;
 203
 204        struct regulator_bulk_data supplies[OV5640_NUM_SUPPLIES];
 205        struct gpio_desc *reset_gpio;
 206        struct gpio_desc *pwdn_gpio;
 207
 208        /* lock to protect all members below */
 209        struct mutex lock;
 210
 211        int power_count;
 212
 213        struct v4l2_mbus_framefmt fmt;
 214
 215        const struct ov5640_mode_info *current_mode;
 216        enum ov5640_frame_rate current_fr;
 217        struct v4l2_fract frame_interval;
 218
 219        struct ov5640_ctrls ctrls;
 220
 221        u32 prev_sysclk, prev_hts;
 222        u32 ae_low, ae_high, ae_target;
 223
 224        bool pending_mode_change;
 225        bool streaming;
 226};
 227
 228static inline struct ov5640_dev *to_ov5640_dev(struct v4l2_subdev *sd)
 229{
 230        return container_of(sd, struct ov5640_dev, sd);
 231}
 232
 233static inline struct v4l2_subdev *ctrl_to_sd(struct v4l2_ctrl *ctrl)
 234{
 235        return &container_of(ctrl->handler, struct ov5640_dev,
 236                             ctrls.handler)->sd;
 237}
 238
 239/*
 240 * FIXME: all of these register tables are likely filled with
 241 * entries that set the register to their power-on default values,
 242 * and which are otherwise not touched by this driver. Those entries
 243 * should be identified and removed to speed register load time
 244 * over i2c.
 245 */
 246
 247static const struct reg_value ov5640_init_setting_30fps_VGA[] = {
 248        {0x3103, 0x11, 0, 0}, {0x3008, 0x82, 0, 5}, {0x3008, 0x42, 0, 0},
 249        {0x3103, 0x03, 0, 0}, {0x3017, 0x00, 0, 0}, {0x3018, 0x00, 0, 0},
 250        {0x3034, 0x18, 0, 0}, {0x3035, 0x14, 0, 0}, {0x3036, 0x38, 0, 0},
 251        {0x3037, 0x13, 0, 0}, {0x3108, 0x01, 0, 0}, {0x3630, 0x36, 0, 0},
 252        {0x3631, 0x0e, 0, 0}, {0x3632, 0xe2, 0, 0}, {0x3633, 0x12, 0, 0},
 253        {0x3621, 0xe0, 0, 0}, {0x3704, 0xa0, 0, 0}, {0x3703, 0x5a, 0, 0},
 254        {0x3715, 0x78, 0, 0}, {0x3717, 0x01, 0, 0}, {0x370b, 0x60, 0, 0},
 255        {0x3705, 0x1a, 0, 0}, {0x3905, 0x02, 0, 0}, {0x3906, 0x10, 0, 0},
 256        {0x3901, 0x0a, 0, 0}, {0x3731, 0x12, 0, 0}, {0x3600, 0x08, 0, 0},
 257        {0x3601, 0x33, 0, 0}, {0x302d, 0x60, 0, 0}, {0x3620, 0x52, 0, 0},
 258        {0x371b, 0x20, 0, 0}, {0x471c, 0x50, 0, 0}, {0x3a13, 0x43, 0, 0},
 259        {0x3a18, 0x00, 0, 0}, {0x3a19, 0xf8, 0, 0}, {0x3635, 0x13, 0, 0},
 260        {0x3636, 0x03, 0, 0}, {0x3634, 0x40, 0, 0}, {0x3622, 0x01, 0, 0},
 261        {0x3c01, 0xa4, 0, 0}, {0x3c04, 0x28, 0, 0}, {0x3c05, 0x98, 0, 0},
 262        {0x3c06, 0x00, 0, 0}, {0x3c07, 0x08, 0, 0}, {0x3c08, 0x00, 0, 0},
 263        {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
 264        {0x3820, 0x41, 0, 0}, {0x3821, 0x07, 0, 0}, {0x3814, 0x31, 0, 0},
 265        {0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
 266        {0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
 267        {0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
 268        {0x3808, 0x02, 0, 0}, {0x3809, 0x80, 0, 0}, {0x380a, 0x01, 0, 0},
 269        {0x380b, 0xe0, 0, 0}, {0x380c, 0x07, 0, 0}, {0x380d, 0x68, 0, 0},
 270        {0x380e, 0x03, 0, 0}, {0x380f, 0xd8, 0, 0}, {0x3810, 0x00, 0, 0},
 271        {0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0},
 272        {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
 273        {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
 274        {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
 275        {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
 276        {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
 277        {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x3000, 0x00, 0, 0},
 278        {0x3002, 0x1c, 0, 0}, {0x3004, 0xff, 0, 0}, {0x3006, 0xc3, 0, 0},
 279        {0x300e, 0x45, 0, 0}, {0x302e, 0x08, 0, 0}, {0x4300, 0x3f, 0, 0},
 280        {0x501f, 0x00, 0, 0}, {0x4713, 0x03, 0, 0}, {0x4407, 0x04, 0, 0},
 281        {0x440e, 0x00, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
 282        {0x4837, 0x0a, 0, 0}, {0x4800, 0x04, 0, 0}, {0x3824, 0x02, 0, 0},
 283        {0x5000, 0xa7, 0, 0}, {0x5001, 0xa3, 0, 0}, {0x5180, 0xff, 0, 0},
 284        {0x5181, 0xf2, 0, 0}, {0x5182, 0x00, 0, 0}, {0x5183, 0x14, 0, 0},
 285        {0x5184, 0x25, 0, 0}, {0x5185, 0x24, 0, 0}, {0x5186, 0x09, 0, 0},
 286        {0x5187, 0x09, 0, 0}, {0x5188, 0x09, 0, 0}, {0x5189, 0x88, 0, 0},
 287        {0x518a, 0x54, 0, 0}, {0x518b, 0xee, 0, 0}, {0x518c, 0xb2, 0, 0},
 288        {0x518d, 0x50, 0, 0}, {0x518e, 0x34, 0, 0}, {0x518f, 0x6b, 0, 0},
 289        {0x5190, 0x46, 0, 0}, {0x5191, 0xf8, 0, 0}, {0x5192, 0x04, 0, 0},
 290        {0x5193, 0x70, 0, 0}, {0x5194, 0xf0, 0, 0}, {0x5195, 0xf0, 0, 0},
 291        {0x5196, 0x03, 0, 0}, {0x5197, 0x01, 0, 0}, {0x5198, 0x04, 0, 0},
 292        {0x5199, 0x6c, 0, 0}, {0x519a, 0x04, 0, 0}, {0x519b, 0x00, 0, 0},
 293        {0x519c, 0x09, 0, 0}, {0x519d, 0x2b, 0, 0}, {0x519e, 0x38, 0, 0},
 294        {0x5381, 0x1e, 0, 0}, {0x5382, 0x5b, 0, 0}, {0x5383, 0x08, 0, 0},
 295        {0x5384, 0x0a, 0, 0}, {0x5385, 0x7e, 0, 0}, {0x5386, 0x88, 0, 0},
 296        {0x5387, 0x7c, 0, 0}, {0x5388, 0x6c, 0, 0}, {0x5389, 0x10, 0, 0},
 297        {0x538a, 0x01, 0, 0}, {0x538b, 0x98, 0, 0}, {0x5300, 0x08, 0, 0},
 298        {0x5301, 0x30, 0, 0}, {0x5302, 0x10, 0, 0}, {0x5303, 0x00, 0, 0},
 299        {0x5304, 0x08, 0, 0}, {0x5305, 0x30, 0, 0}, {0x5306, 0x08, 0, 0},
 300        {0x5307, 0x16, 0, 0}, {0x5309, 0x08, 0, 0}, {0x530a, 0x30, 0, 0},
 301        {0x530b, 0x04, 0, 0}, {0x530c, 0x06, 0, 0}, {0x5480, 0x01, 0, 0},
 302        {0x5481, 0x08, 0, 0}, {0x5482, 0x14, 0, 0}, {0x5483, 0x28, 0, 0},
 303        {0x5484, 0x51, 0, 0}, {0x5485, 0x65, 0, 0}, {0x5486, 0x71, 0, 0},
 304        {0x5487, 0x7d, 0, 0}, {0x5488, 0x87, 0, 0}, {0x5489, 0x91, 0, 0},
 305        {0x548a, 0x9a, 0, 0}, {0x548b, 0xaa, 0, 0}, {0x548c, 0xb8, 0, 0},
 306        {0x548d, 0xcd, 0, 0}, {0x548e, 0xdd, 0, 0}, {0x548f, 0xea, 0, 0},
 307        {0x5490, 0x1d, 0, 0}, {0x5580, 0x02, 0, 0}, {0x5583, 0x40, 0, 0},
 308        {0x5584, 0x10, 0, 0}, {0x5589, 0x10, 0, 0}, {0x558a, 0x00, 0, 0},
 309        {0x558b, 0xf8, 0, 0}, {0x5800, 0x23, 0, 0}, {0x5801, 0x14, 0, 0},
 310        {0x5802, 0x0f, 0, 0}, {0x5803, 0x0f, 0, 0}, {0x5804, 0x12, 0, 0},
 311        {0x5805, 0x26, 0, 0}, {0x5806, 0x0c, 0, 0}, {0x5807, 0x08, 0, 0},
 312        {0x5808, 0x05, 0, 0}, {0x5809, 0x05, 0, 0}, {0x580a, 0x08, 0, 0},
 313        {0x580b, 0x0d, 0, 0}, {0x580c, 0x08, 0, 0}, {0x580d, 0x03, 0, 0},
 314        {0x580e, 0x00, 0, 0}, {0x580f, 0x00, 0, 0}, {0x5810, 0x03, 0, 0},
 315        {0x5811, 0x09, 0, 0}, {0x5812, 0x07, 0, 0}, {0x5813, 0x03, 0, 0},
 316        {0x5814, 0x00, 0, 0}, {0x5815, 0x01, 0, 0}, {0x5816, 0x03, 0, 0},
 317        {0x5817, 0x08, 0, 0}, {0x5818, 0x0d, 0, 0}, {0x5819, 0x08, 0, 0},
 318        {0x581a, 0x05, 0, 0}, {0x581b, 0x06, 0, 0}, {0x581c, 0x08, 0, 0},
 319        {0x581d, 0x0e, 0, 0}, {0x581e, 0x29, 0, 0}, {0x581f, 0x17, 0, 0},
 320        {0x5820, 0x11, 0, 0}, {0x5821, 0x11, 0, 0}, {0x5822, 0x15, 0, 0},
 321        {0x5823, 0x28, 0, 0}, {0x5824, 0x46, 0, 0}, {0x5825, 0x26, 0, 0},
 322        {0x5826, 0x08, 0, 0}, {0x5827, 0x26, 0, 0}, {0x5828, 0x64, 0, 0},
 323        {0x5829, 0x26, 0, 0}, {0x582a, 0x24, 0, 0}, {0x582b, 0x22, 0, 0},
 324        {0x582c, 0x24, 0, 0}, {0x582d, 0x24, 0, 0}, {0x582e, 0x06, 0, 0},
 325        {0x582f, 0x22, 0, 0}, {0x5830, 0x40, 0, 0}, {0x5831, 0x42, 0, 0},
 326        {0x5832, 0x24, 0, 0}, {0x5833, 0x26, 0, 0}, {0x5834, 0x24, 0, 0},
 327        {0x5835, 0x22, 0, 0}, {0x5836, 0x22, 0, 0}, {0x5837, 0x26, 0, 0},
 328        {0x5838, 0x44, 0, 0}, {0x5839, 0x24, 0, 0}, {0x583a, 0x26, 0, 0},
 329        {0x583b, 0x28, 0, 0}, {0x583c, 0x42, 0, 0}, {0x583d, 0xce, 0, 0},
 330        {0x5025, 0x00, 0, 0}, {0x3a0f, 0x30, 0, 0}, {0x3a10, 0x28, 0, 0},
 331        {0x3a1b, 0x30, 0, 0}, {0x3a1e, 0x26, 0, 0}, {0x3a11, 0x60, 0, 0},
 332        {0x3a1f, 0x14, 0, 0}, {0x3008, 0x02, 0, 0}, {0x3c00, 0x04, 0, 300},
 333};
 334
 335static const struct reg_value ov5640_setting_30fps_VGA_640_480[] = {
 336        {0x3035, 0x14, 0, 0}, {0x3036, 0x38, 0, 0}, {0x3c07, 0x08, 0, 0},
 337        {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
 338        {0x3820, 0x41, 0, 0}, {0x3821, 0x07, 0, 0}, {0x3814, 0x31, 0, 0},
 339        {0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
 340        {0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
 341        {0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
 342        {0x3808, 0x02, 0, 0}, {0x3809, 0x80, 0, 0}, {0x380a, 0x01, 0, 0},
 343        {0x380b, 0xe0, 0, 0}, {0x380c, 0x07, 0, 0}, {0x380d, 0x68, 0, 0},
 344        {0x380e, 0x04, 0, 0}, {0x380f, 0x38, 0, 0}, {0x3810, 0x00, 0, 0},
 345        {0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0},
 346        {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
 347        {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
 348        {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x0e, 0, 0},
 349        {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
 350        {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
 351        {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x03, 0, 0},
 352        {0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
 353        {0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0}, {0x3503, 0x00, 0, 0},
 354};
 355
 356static const struct reg_value ov5640_setting_15fps_VGA_640_480[] = {
 357        {0x3035, 0x22, 0, 0}, {0x3036, 0x38, 0, 0}, {0x3c07, 0x08, 0, 0},
 358        {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
 359        {0x3820, 0x41, 0, 0}, {0x3821, 0x07, 0, 0}, {0x3814, 0x31, 0, 0},
 360        {0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
 361        {0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
 362        {0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
 363        {0x3808, 0x02, 0, 0}, {0x3809, 0x80, 0, 0}, {0x380a, 0x01, 0, 0},
 364        {0x380b, 0xe0, 0, 0}, {0x380c, 0x07, 0, 0}, {0x380d, 0x68, 0, 0},
 365        {0x380e, 0x03, 0, 0}, {0x380f, 0xd8, 0, 0}, {0x3810, 0x00, 0, 0},
 366        {0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0},
 367        {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
 368        {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
 369        {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
 370        {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
 371        {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
 372        {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x03, 0, 0},
 373        {0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
 374        {0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0},
 375};
 376
 377static const struct reg_value ov5640_setting_30fps_XGA_1024_768[] = {
 378        {0x3035, 0x14, 0, 0}, {0x3036, 0x38, 0, 0}, {0x3c07, 0x08, 0, 0},
 379        {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
 380        {0x3820, 0x41, 0, 0}, {0x3821, 0x07, 0, 0}, {0x3814, 0x31, 0, 0},
 381        {0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
 382        {0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
 383        {0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
 384        {0x3808, 0x02, 0, 0}, {0x3809, 0x80, 0, 0}, {0x380a, 0x01, 0, 0},
 385        {0x380b, 0xe0, 0, 0}, {0x380c, 0x07, 0, 0}, {0x380d, 0x68, 0, 0},
 386        {0x380e, 0x04, 0, 0}, {0x380f, 0x38, 0, 0}, {0x3810, 0x00, 0, 0},
 387        {0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0},
 388        {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
 389        {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
 390        {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x0e, 0, 0},
 391        {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
 392        {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
 393        {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x03, 0, 0},
 394        {0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
 395        {0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0}, {0x3503, 0x00, 0, 0},
 396        {0x3808, 0x04, 0, 0}, {0x3809, 0x00, 0, 0}, {0x380a, 0x03, 0, 0},
 397        {0x380b, 0x00, 0, 0}, {0x3035, 0x12, 0, 0},
 398};
 399
 400static const struct reg_value ov5640_setting_15fps_XGA_1024_768[] = {
 401        {0x3035, 0x22, 0, 0}, {0x3036, 0x38, 0, 0}, {0x3c07, 0x08, 0, 0},
 402        {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
 403        {0x3820, 0x41, 0, 0}, {0x3821, 0x07, 0, 0}, {0x3814, 0x31, 0, 0},
 404        {0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
 405        {0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
 406        {0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
 407        {0x3808, 0x02, 0, 0}, {0x3809, 0x80, 0, 0}, {0x380a, 0x01, 0, 0},
 408        {0x380b, 0xe0, 0, 0}, {0x380c, 0x07, 0, 0}, {0x380d, 0x68, 0, 0},
 409        {0x380e, 0x03, 0, 0}, {0x380f, 0xd8, 0, 0}, {0x3810, 0x00, 0, 0},
 410        {0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0},
 411        {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
 412        {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
 413        {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
 414        {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
 415        {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
 416        {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x03, 0, 0},
 417        {0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
 418        {0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0}, {0x3808, 0x04, 0, 0},
 419        {0x3809, 0x00, 0, 0}, {0x380a, 0x03, 0, 0}, {0x380b, 0x00, 0, 0},
 420};
 421
 422static const struct reg_value ov5640_setting_30fps_QVGA_320_240[] = {
 423        {0x3035, 0x14, 0, 0}, {0x3036, 0x38, 0, 0}, {0x3c07, 0x08, 0, 0},
 424        {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
 425        {0x3820, 0x41, 0, 0}, {0x3821, 0x07, 0, 0}, {0x3814, 0x31, 0, 0},
 426        {0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
 427        {0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
 428        {0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
 429        {0x3808, 0x01, 0, 0}, {0x3809, 0x40, 0, 0}, {0x380a, 0x00, 0, 0},
 430        {0x380b, 0xf0, 0, 0}, {0x380c, 0x07, 0, 0}, {0x380d, 0x68, 0, 0},
 431        {0x380e, 0x03, 0, 0}, {0x380f, 0xd8, 0, 0}, {0x3810, 0x00, 0, 0},
 432        {0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0},
 433        {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
 434        {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
 435        {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
 436        {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
 437        {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
 438        {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x03, 0, 0},
 439        {0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
 440        {0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0},
 441};
 442
 443static const struct reg_value ov5640_setting_15fps_QVGA_320_240[] = {
 444        {0x3035, 0x22, 0, 0}, {0x3036, 0x38, 0, 0}, {0x3c07, 0x08, 0, 0},
 445        {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
 446        {0x3820, 0x41, 0, 0}, {0x3821, 0x07, 0, 0}, {0x3814, 0x31, 0, 0},
 447        {0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
 448        {0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
 449        {0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
 450        {0x3808, 0x01, 0, 0}, {0x3809, 0x40, 0, 0}, {0x380a, 0x00, 0, 0},
 451        {0x380b, 0xf0, 0, 0}, {0x380c, 0x07, 0, 0}, {0x380d, 0x68, 0, 0},
 452        {0x380e, 0x03, 0, 0}, {0x380f, 0xd8, 0, 0}, {0x3810, 0x00, 0, 0},
 453        {0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0},
 454        {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
 455        {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
 456        {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
 457        {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
 458        {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
 459        {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x03, 0, 0},
 460        {0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
 461        {0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0},
 462};
 463
 464static const struct reg_value ov5640_setting_30fps_QCIF_176_144[] = {
 465        {0x3035, 0x14, 0, 0}, {0x3036, 0x38, 0, 0}, {0x3c07, 0x08, 0, 0},
 466        {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
 467        {0x3820, 0x41, 0, 0}, {0x3821, 0x07, 0, 0}, {0x3814, 0x31, 0, 0},
 468        {0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
 469        {0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
 470        {0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
 471        {0x3808, 0x00, 0, 0}, {0x3809, 0xb0, 0, 0}, {0x380a, 0x00, 0, 0},
 472        {0x380b, 0x90, 0, 0}, {0x380c, 0x07, 0, 0}, {0x380d, 0x68, 0, 0},
 473        {0x380e, 0x03, 0, 0}, {0x380f, 0xd8, 0, 0}, {0x3810, 0x00, 0, 0},
 474        {0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0},
 475        {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
 476        {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
 477        {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
 478        {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
 479        {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
 480        {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x03, 0, 0},
 481        {0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
 482        {0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0},
 483};
 484
 485static const struct reg_value ov5640_setting_15fps_QCIF_176_144[] = {
 486        {0x3035, 0x22, 0, 0}, {0x3036, 0x38, 0, 0}, {0x3c07, 0x08, 0, 0},
 487        {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
 488        {0x3820, 0x41, 0, 0}, {0x3821, 0x07, 0, 0}, {0x3814, 0x31, 0, 0},
 489        {0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
 490        {0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
 491        {0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
 492        {0x3808, 0x00, 0, 0}, {0x3809, 0xb0, 0, 0}, {0x380a, 0x00, 0, 0},
 493        {0x380b, 0x90, 0, 0}, {0x380c, 0x07, 0, 0}, {0x380d, 0x68, 0, 0},
 494        {0x380e, 0x03, 0, 0}, {0x380f, 0xd8, 0, 0}, {0x3810, 0x00, 0, 0},
 495        {0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0},
 496        {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
 497        {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
 498        {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
 499        {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
 500        {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
 501        {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x03, 0, 0},
 502        {0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
 503        {0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0},
 504};
 505
 506static const struct reg_value ov5640_setting_30fps_NTSC_720_480[] = {
 507        {0x3035, 0x12, 0, 0}, {0x3036, 0x38, 0, 0}, {0x3c07, 0x08, 0, 0},
 508        {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
 509        {0x3820, 0x41, 0, 0}, {0x3821, 0x07, 0, 0}, {0x3814, 0x31, 0, 0},
 510        {0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
 511        {0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
 512        {0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
 513        {0x3808, 0x02, 0, 0}, {0x3809, 0xd0, 0, 0}, {0x380a, 0x01, 0, 0},
 514        {0x380b, 0xe0, 0, 0}, {0x380c, 0x07, 0, 0}, {0x380d, 0x68, 0, 0},
 515        {0x380e, 0x03, 0, 0}, {0x380f, 0xd8, 0, 0}, {0x3810, 0x00, 0, 0},
 516        {0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x3c, 0, 0},
 517        {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
 518        {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
 519        {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
 520        {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
 521        {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
 522        {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x03, 0, 0},
 523        {0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
 524        {0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0},
 525};
 526
 527static const struct reg_value ov5640_setting_15fps_NTSC_720_480[] = {
 528        {0x3035, 0x22, 0, 0}, {0x3036, 0x38, 0, 0}, {0x3c07, 0x08, 0, 0},
 529        {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
 530        {0x3820, 0x41, 0, 0}, {0x3821, 0x07, 0, 0}, {0x3814, 0x31, 0, 0},
 531        {0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
 532        {0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
 533        {0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
 534        {0x3808, 0x02, 0, 0}, {0x3809, 0xd0, 0, 0}, {0x380a, 0x01, 0, 0},
 535        {0x380b, 0xe0, 0, 0}, {0x380c, 0x07, 0, 0}, {0x380d, 0x68, 0, 0},
 536        {0x380e, 0x03, 0, 0}, {0x380f, 0xd8, 0, 0}, {0x3810, 0x00, 0, 0},
 537        {0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x3c, 0, 0},
 538        {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
 539        {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
 540        {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
 541        {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
 542        {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
 543        {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x03, 0, 0},
 544        {0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
 545        {0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0},
 546};
 547
 548static const struct reg_value ov5640_setting_30fps_PAL_720_576[] = {
 549        {0x3035, 0x12, 0, 0}, {0x3036, 0x38, 0, 0}, {0x3c07, 0x08, 0, 0},
 550        {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
 551        {0x3820, 0x41, 0, 0}, {0x3821, 0x07, 0, 0}, {0x3814, 0x31, 0, 0},
 552        {0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
 553        {0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
 554        {0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
 555        {0x3808, 0x02, 0, 0}, {0x3809, 0xd0, 0, 0}, {0x380a, 0x02, 0, 0},
 556        {0x380b, 0x40, 0, 0}, {0x380c, 0x07, 0, 0}, {0x380d, 0x68, 0, 0},
 557        {0x380e, 0x03, 0, 0}, {0x380f, 0xd8, 0, 0}, {0x3810, 0x00, 0, 0},
 558        {0x3811, 0x38, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0},
 559        {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
 560        {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
 561        {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
 562        {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
 563        {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
 564        {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x03, 0, 0},
 565        {0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
 566        {0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0},
 567};
 568
 569static const struct reg_value ov5640_setting_15fps_PAL_720_576[] = {
 570        {0x3035, 0x22, 0, 0}, {0x3036, 0x38, 0, 0}, {0x3c07, 0x08, 0, 0},
 571        {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
 572        {0x3820, 0x41, 0, 0}, {0x3821, 0x07, 0, 0}, {0x3814, 0x31, 0, 0},
 573        {0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
 574        {0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
 575        {0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
 576        {0x3808, 0x02, 0, 0}, {0x3809, 0xd0, 0, 0}, {0x380a, 0x02, 0, 0},
 577        {0x380b, 0x40, 0, 0}, {0x380c, 0x07, 0, 0}, {0x380d, 0x68, 0, 0},
 578        {0x380e, 0x03, 0, 0}, {0x380f, 0xd8, 0, 0}, {0x3810, 0x00, 0, 0},
 579        {0x3811, 0x38, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0},
 580        {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
 581        {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
 582        {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
 583        {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
 584        {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
 585        {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x03, 0, 0},
 586        {0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
 587        {0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0},
 588};
 589
 590static const struct reg_value ov5640_setting_30fps_720P_1280_720[] = {
 591        {0x3008, 0x42, 0, 0},
 592        {0x3035, 0x21, 0, 0}, {0x3036, 0x54, 0, 0}, {0x3c07, 0x07, 0, 0},
 593        {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
 594        {0x3820, 0x41, 0, 0}, {0x3821, 0x07, 0, 0}, {0x3814, 0x31, 0, 0},
 595        {0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
 596        {0x3802, 0x00, 0, 0}, {0x3803, 0xfa, 0, 0}, {0x3804, 0x0a, 0, 0},
 597        {0x3805, 0x3f, 0, 0}, {0x3806, 0x06, 0, 0}, {0x3807, 0xa9, 0, 0},
 598        {0x3808, 0x05, 0, 0}, {0x3809, 0x00, 0, 0}, {0x380a, 0x02, 0, 0},
 599        {0x380b, 0xd0, 0, 0}, {0x380c, 0x07, 0, 0}, {0x380d, 0x64, 0, 0},
 600        {0x380e, 0x02, 0, 0}, {0x380f, 0xe4, 0, 0}, {0x3810, 0x00, 0, 0},
 601        {0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x04, 0, 0},
 602        {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
 603        {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x02, 0, 0},
 604        {0x3a03, 0xe4, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0xbc, 0, 0},
 605        {0x3a0a, 0x01, 0, 0}, {0x3a0b, 0x72, 0, 0}, {0x3a0e, 0x01, 0, 0},
 606        {0x3a0d, 0x02, 0, 0}, {0x3a14, 0x02, 0, 0}, {0x3a15, 0xe4, 0, 0},
 607        {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x02, 0, 0},
 608        {0x4407, 0x04, 0, 0}, {0x460b, 0x37, 0, 0}, {0x460c, 0x20, 0, 0},
 609        {0x3824, 0x04, 0, 0}, {0x5001, 0x83, 0, 0}, {0x4005, 0x1a, 0, 0},
 610        {0x3008, 0x02, 0, 0}, {0x3503, 0,    0, 0},
 611};
 612
 613static const struct reg_value ov5640_setting_15fps_720P_1280_720[] = {
 614        {0x3035, 0x41, 0, 0}, {0x3036, 0x54, 0, 0}, {0x3c07, 0x07, 0, 0},
 615        {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
 616        {0x3820, 0x41, 0, 0}, {0x3821, 0x07, 0, 0}, {0x3814, 0x31, 0, 0},
 617        {0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
 618        {0x3802, 0x00, 0, 0}, {0x3803, 0xfa, 0, 0}, {0x3804, 0x0a, 0, 0},
 619        {0x3805, 0x3f, 0, 0}, {0x3806, 0x06, 0, 0}, {0x3807, 0xa9, 0, 0},
 620        {0x3808, 0x05, 0, 0}, {0x3809, 0x00, 0, 0}, {0x380a, 0x02, 0, 0},
 621        {0x380b, 0xd0, 0, 0}, {0x380c, 0x07, 0, 0}, {0x380d, 0x64, 0, 0},
 622        {0x380e, 0x02, 0, 0}, {0x380f, 0xe4, 0, 0}, {0x3810, 0x00, 0, 0},
 623        {0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x04, 0, 0},
 624        {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
 625        {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x02, 0, 0},
 626        {0x3a03, 0xe4, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0xbc, 0, 0},
 627        {0x3a0a, 0x01, 0, 0}, {0x3a0b, 0x72, 0, 0}, {0x3a0e, 0x01, 0, 0},
 628        {0x3a0d, 0x02, 0, 0}, {0x3a14, 0x02, 0, 0}, {0x3a15, 0xe4, 0, 0},
 629        {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x02, 0, 0},
 630        {0x4407, 0x04, 0, 0}, {0x460b, 0x37, 0, 0}, {0x460c, 0x20, 0, 0},
 631        {0x3824, 0x04, 0, 0}, {0x5001, 0x83, 0, 0},
 632};
 633
 634static const struct reg_value ov5640_setting_30fps_1080P_1920_1080[] = {
 635        {0x3008, 0x42, 0, 0},
 636        {0x3035, 0x21, 0, 0}, {0x3036, 0x54, 0, 0}, {0x3c07, 0x08, 0, 0},
 637        {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
 638        {0x3820, 0x40, 0, 0}, {0x3821, 0x06, 0, 0}, {0x3814, 0x11, 0, 0},
 639        {0x3815, 0x11, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
 640        {0x3802, 0x00, 0, 0}, {0x3803, 0x00, 0, 0}, {0x3804, 0x0a, 0, 0},
 641        {0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9f, 0, 0},
 642        {0x3808, 0x0a, 0, 0}, {0x3809, 0x20, 0, 0}, {0x380a, 0x07, 0, 0},
 643        {0x380b, 0x98, 0, 0}, {0x380c, 0x0b, 0, 0}, {0x380d, 0x1c, 0, 0},
 644        {0x380e, 0x07, 0, 0}, {0x380f, 0xb0, 0, 0}, {0x3810, 0x00, 0, 0},
 645        {0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x04, 0, 0},
 646        {0x3618, 0x04, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x21, 0, 0},
 647        {0x3709, 0x12, 0, 0}, {0x370c, 0x00, 0, 0}, {0x3a02, 0x03, 0, 0},
 648        {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
 649        {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
 650        {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
 651        {0x4001, 0x02, 0, 0}, {0x4004, 0x06, 0, 0}, {0x4713, 0x03, 0, 0},
 652        {0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
 653        {0x3824, 0x02, 0, 0}, {0x5001, 0x83, 0, 0}, {0x3035, 0x11, 0, 0},
 654        {0x3036, 0x54, 0, 0}, {0x3c07, 0x07, 0, 0}, {0x3c08, 0x00, 0, 0},
 655        {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
 656        {0x3800, 0x01, 0, 0}, {0x3801, 0x50, 0, 0}, {0x3802, 0x01, 0, 0},
 657        {0x3803, 0xb2, 0, 0}, {0x3804, 0x08, 0, 0}, {0x3805, 0xef, 0, 0},
 658        {0x3806, 0x05, 0, 0}, {0x3807, 0xf1, 0, 0}, {0x3808, 0x07, 0, 0},
 659        {0x3809, 0x80, 0, 0}, {0x380a, 0x04, 0, 0}, {0x380b, 0x38, 0, 0},
 660        {0x380c, 0x09, 0, 0}, {0x380d, 0xc4, 0, 0}, {0x380e, 0x04, 0, 0},
 661        {0x380f, 0x60, 0, 0}, {0x3612, 0x2b, 0, 0}, {0x3708, 0x64, 0, 0},
 662        {0x3a02, 0x04, 0, 0}, {0x3a03, 0x60, 0, 0}, {0x3a08, 0x01, 0, 0},
 663        {0x3a09, 0x50, 0, 0}, {0x3a0a, 0x01, 0, 0}, {0x3a0b, 0x18, 0, 0},
 664        {0x3a0e, 0x03, 0, 0}, {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x04, 0, 0},
 665        {0x3a15, 0x60, 0, 0}, {0x4713, 0x02, 0, 0}, {0x4407, 0x04, 0, 0},
 666        {0x460b, 0x37, 0, 0}, {0x460c, 0x20, 0, 0}, {0x3824, 0x04, 0, 0},
 667        {0x4005, 0x1a, 0, 0}, {0x3008, 0x02, 0, 0},
 668        {0x3503, 0, 0, 0},
 669};
 670
 671static const struct reg_value ov5640_setting_15fps_1080P_1920_1080[] = {
 672        {0x3008, 0x42, 0, 0},
 673        {0x3035, 0x21, 0, 0}, {0x3036, 0x54, 0, 0}, {0x3c07, 0x08, 0, 0},
 674        {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
 675        {0x3820, 0x40, 0, 0}, {0x3821, 0x06, 0, 0}, {0x3814, 0x11, 0, 0},
 676        {0x3815, 0x11, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
 677        {0x3802, 0x00, 0, 0}, {0x3803, 0x00, 0, 0}, {0x3804, 0x0a, 0, 0},
 678        {0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9f, 0, 0},
 679        {0x3808, 0x0a, 0, 0}, {0x3809, 0x20, 0, 0}, {0x380a, 0x07, 0, 0},
 680        {0x380b, 0x98, 0, 0}, {0x380c, 0x0b, 0, 0}, {0x380d, 0x1c, 0, 0},
 681        {0x380e, 0x07, 0, 0}, {0x380f, 0xb0, 0, 0}, {0x3810, 0x00, 0, 0},
 682        {0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x04, 0, 0},
 683        {0x3618, 0x04, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x21, 0, 0},
 684        {0x3709, 0x12, 0, 0}, {0x370c, 0x00, 0, 0}, {0x3a02, 0x03, 0, 0},
 685        {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
 686        {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
 687        {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
 688        {0x4001, 0x02, 0, 0}, {0x4004, 0x06, 0, 0}, {0x4713, 0x03, 0, 0},
 689        {0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
 690        {0x3824, 0x02, 0, 0}, {0x5001, 0x83, 0, 0}, {0x3035, 0x21, 0, 0},
 691        {0x3036, 0x54, 0, 1}, {0x3c07, 0x07, 0, 0}, {0x3c08, 0x00, 0, 0},
 692        {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
 693        {0x3800, 0x01, 0, 0}, {0x3801, 0x50, 0, 0}, {0x3802, 0x01, 0, 0},
 694        {0x3803, 0xb2, 0, 0}, {0x3804, 0x08, 0, 0}, {0x3805, 0xef, 0, 0},
 695        {0x3806, 0x05, 0, 0}, {0x3807, 0xf1, 0, 0}, {0x3808, 0x07, 0, 0},
 696        {0x3809, 0x80, 0, 0}, {0x380a, 0x04, 0, 0}, {0x380b, 0x38, 0, 0},
 697        {0x380c, 0x09, 0, 0}, {0x380d, 0xc4, 0, 0}, {0x380e, 0x04, 0, 0},
 698        {0x380f, 0x60, 0, 0}, {0x3612, 0x2b, 0, 0}, {0x3708, 0x64, 0, 0},
 699        {0x3a02, 0x04, 0, 0}, {0x3a03, 0x60, 0, 0}, {0x3a08, 0x01, 0, 0},
 700        {0x3a09, 0x50, 0, 0}, {0x3a0a, 0x01, 0, 0}, {0x3a0b, 0x18, 0, 0},
 701        {0x3a0e, 0x03, 0, 0}, {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x04, 0, 0},
 702        {0x3a15, 0x60, 0, 0}, {0x4713, 0x02, 0, 0}, {0x4407, 0x04, 0, 0},
 703        {0x460b, 0x37, 0, 0}, {0x460c, 0x20, 0, 0}, {0x3824, 0x04, 0, 0},
 704        {0x4005, 0x1a, 0, 0}, {0x3008, 0x02, 0, 0}, {0x3503, 0, 0, 0},
 705};
 706
 707static const struct reg_value ov5640_setting_15fps_QSXGA_2592_1944[] = {
 708        {0x3820, 0x40, 0, 0}, {0x3821, 0x06, 0, 0},
 709        {0x3035, 0x21, 0, 0}, {0x3036, 0x54, 0, 0}, {0x3c07, 0x08, 0, 0},
 710        {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
 711        {0x3820, 0x40, 0, 0}, {0x3821, 0x06, 0, 0}, {0x3814, 0x11, 0, 0},
 712        {0x3815, 0x11, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
 713        {0x3802, 0x00, 0, 0}, {0x3803, 0x00, 0, 0}, {0x3804, 0x0a, 0, 0},
 714        {0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9f, 0, 0},
 715        {0x3808, 0x0a, 0, 0}, {0x3809, 0x20, 0, 0}, {0x380a, 0x07, 0, 0},
 716        {0x380b, 0x98, 0, 0}, {0x380c, 0x0b, 0, 0}, {0x380d, 0x1c, 0, 0},
 717        {0x380e, 0x07, 0, 0}, {0x380f, 0xb0, 0, 0}, {0x3810, 0x00, 0, 0},
 718        {0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x04, 0, 0},
 719        {0x3618, 0x04, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x21, 0, 0},
 720        {0x3709, 0x12, 0, 0}, {0x370c, 0x00, 0, 0}, {0x3a02, 0x03, 0, 0},
 721        {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
 722        {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
 723        {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
 724        {0x4001, 0x02, 0, 0}, {0x4004, 0x06, 0, 0}, {0x4713, 0x03, 0, 0},
 725        {0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
 726        {0x3824, 0x02, 0, 0}, {0x5001, 0x83, 0, 70},
 727};
 728
 729/* power-on sensor init reg table */
 730static const struct ov5640_mode_info ov5640_mode_init_data = {
 731        0, SUBSAMPLING, 640, 480, ov5640_init_setting_30fps_VGA,
 732        ARRAY_SIZE(ov5640_init_setting_30fps_VGA),
 733};
 734
 735static const struct ov5640_mode_info
 736ov5640_mode_data[OV5640_NUM_FRAMERATES][OV5640_NUM_MODES] = {
 737        {
 738                {OV5640_MODE_QCIF_176_144, SUBSAMPLING, 176, 144,
 739                 ov5640_setting_15fps_QCIF_176_144,
 740                 ARRAY_SIZE(ov5640_setting_15fps_QCIF_176_144)},
 741                {OV5640_MODE_QVGA_320_240, SUBSAMPLING, 320,  240,
 742                 ov5640_setting_15fps_QVGA_320_240,
 743                 ARRAY_SIZE(ov5640_setting_15fps_QVGA_320_240)},
 744                {OV5640_MODE_VGA_640_480, SUBSAMPLING, 640,  480,
 745                 ov5640_setting_15fps_VGA_640_480,
 746                 ARRAY_SIZE(ov5640_setting_15fps_VGA_640_480)},
 747                {OV5640_MODE_NTSC_720_480, SUBSAMPLING, 720, 480,
 748                 ov5640_setting_15fps_NTSC_720_480,
 749                 ARRAY_SIZE(ov5640_setting_15fps_NTSC_720_480)},
 750                {OV5640_MODE_PAL_720_576, SUBSAMPLING, 720, 576,
 751                 ov5640_setting_15fps_PAL_720_576,
 752                 ARRAY_SIZE(ov5640_setting_15fps_PAL_720_576)},
 753                {OV5640_MODE_XGA_1024_768, SUBSAMPLING, 1024, 768,
 754                 ov5640_setting_15fps_XGA_1024_768,
 755                 ARRAY_SIZE(ov5640_setting_15fps_XGA_1024_768)},
 756                {OV5640_MODE_720P_1280_720, SUBSAMPLING, 1280, 720,
 757                 ov5640_setting_15fps_720P_1280_720,
 758                 ARRAY_SIZE(ov5640_setting_15fps_720P_1280_720)},
 759                {OV5640_MODE_1080P_1920_1080, SCALING, 1920, 1080,
 760                 ov5640_setting_15fps_1080P_1920_1080,
 761                 ARRAY_SIZE(ov5640_setting_15fps_1080P_1920_1080)},
 762                {OV5640_MODE_QSXGA_2592_1944, SCALING, 2592, 1944,
 763                 ov5640_setting_15fps_QSXGA_2592_1944,
 764                 ARRAY_SIZE(ov5640_setting_15fps_QSXGA_2592_1944)},
 765        }, {
 766                {OV5640_MODE_QCIF_176_144, SUBSAMPLING, 176, 144,
 767                 ov5640_setting_30fps_QCIF_176_144,
 768                 ARRAY_SIZE(ov5640_setting_30fps_QCIF_176_144)},
 769                {OV5640_MODE_QVGA_320_240, SUBSAMPLING, 320,  240,
 770                 ov5640_setting_30fps_QVGA_320_240,
 771                 ARRAY_SIZE(ov5640_setting_30fps_QVGA_320_240)},
 772                {OV5640_MODE_VGA_640_480, SUBSAMPLING, 640,  480,
 773                 ov5640_setting_30fps_VGA_640_480,
 774                 ARRAY_SIZE(ov5640_setting_30fps_VGA_640_480)},
 775                {OV5640_MODE_NTSC_720_480, SUBSAMPLING, 720, 480,
 776                 ov5640_setting_30fps_NTSC_720_480,
 777                 ARRAY_SIZE(ov5640_setting_30fps_NTSC_720_480)},
 778                {OV5640_MODE_PAL_720_576, SUBSAMPLING, 720, 576,
 779                 ov5640_setting_30fps_PAL_720_576,
 780                 ARRAY_SIZE(ov5640_setting_30fps_PAL_720_576)},
 781                {OV5640_MODE_XGA_1024_768, SUBSAMPLING, 1024, 768,
 782                 ov5640_setting_30fps_XGA_1024_768,
 783                 ARRAY_SIZE(ov5640_setting_30fps_XGA_1024_768)},
 784                {OV5640_MODE_720P_1280_720, SUBSAMPLING, 1280, 720,
 785                 ov5640_setting_30fps_720P_1280_720,
 786                 ARRAY_SIZE(ov5640_setting_30fps_720P_1280_720)},
 787                {OV5640_MODE_1080P_1920_1080, SCALING, 1920, 1080,
 788                 ov5640_setting_30fps_1080P_1920_1080,
 789                 ARRAY_SIZE(ov5640_setting_30fps_1080P_1920_1080)},
 790                {OV5640_MODE_QSXGA_2592_1944, -1, 0, 0, NULL, 0},
 791        },
 792};
 793
 794static int ov5640_init_slave_id(struct ov5640_dev *sensor)
 795{
 796        struct i2c_client *client = sensor->i2c_client;
 797        struct i2c_msg msg;
 798        u8 buf[3];
 799        int ret;
 800
 801        if (client->addr == OV5640_DEFAULT_SLAVE_ID)
 802                return 0;
 803
 804        buf[0] = OV5640_REG_SLAVE_ID >> 8;
 805        buf[1] = OV5640_REG_SLAVE_ID & 0xff;
 806        buf[2] = client->addr << 1;
 807
 808        msg.addr = OV5640_DEFAULT_SLAVE_ID;
 809        msg.flags = 0;
 810        msg.buf = buf;
 811        msg.len = sizeof(buf);
 812
 813        ret = i2c_transfer(client->adapter, &msg, 1);
 814        if (ret < 0) {
 815                dev_err(&client->dev, "%s: failed with %d\n", __func__, ret);
 816                return ret;
 817        }
 818
 819        return 0;
 820}
 821
 822static int ov5640_write_reg(struct ov5640_dev *sensor, u16 reg, u8 val)
 823{
 824        struct i2c_client *client = sensor->i2c_client;
 825        struct i2c_msg msg;
 826        u8 buf[3];
 827        int ret;
 828
 829        buf[0] = reg >> 8;
 830        buf[1] = reg & 0xff;
 831        buf[2] = val;
 832
 833        msg.addr = client->addr;
 834        msg.flags = client->flags;
 835        msg.buf = buf;
 836        msg.len = sizeof(buf);
 837
 838        ret = i2c_transfer(client->adapter, &msg, 1);
 839        if (ret < 0) {
 840                dev_err(&client->dev, "%s: error: reg=%x, val=%x\n",
 841                        __func__, reg, val);
 842                return ret;
 843        }
 844
 845        return 0;
 846}
 847
 848static int ov5640_read_reg(struct ov5640_dev *sensor, u16 reg, u8 *val)
 849{
 850        struct i2c_client *client = sensor->i2c_client;
 851        struct i2c_msg msg[2];
 852        u8 buf[2];
 853        int ret;
 854
 855        buf[0] = reg >> 8;
 856        buf[1] = reg & 0xff;
 857
 858        msg[0].addr = client->addr;
 859        msg[0].flags = client->flags;
 860        msg[0].buf = buf;
 861        msg[0].len = sizeof(buf);
 862
 863        msg[1].addr = client->addr;
 864        msg[1].flags = client->flags | I2C_M_RD;
 865        msg[1].buf = buf;
 866        msg[1].len = 1;
 867
 868        ret = i2c_transfer(client->adapter, msg, 2);
 869        if (ret < 0) {
 870                dev_err(&client->dev, "%s: error: reg=%x\n",
 871                        __func__, reg);
 872                return ret;
 873        }
 874
 875        *val = buf[0];
 876        return 0;
 877}
 878
 879static int ov5640_read_reg16(struct ov5640_dev *sensor, u16 reg, u16 *val)
 880{
 881        u8 hi, lo;
 882        int ret;
 883
 884        ret = ov5640_read_reg(sensor, reg, &hi);
 885        if (ret)
 886                return ret;
 887        ret = ov5640_read_reg(sensor, reg + 1, &lo);
 888        if (ret)
 889                return ret;
 890
 891        *val = ((u16)hi << 8) | (u16)lo;
 892        return 0;
 893}
 894
 895static int ov5640_write_reg16(struct ov5640_dev *sensor, u16 reg, u16 val)
 896{
 897        int ret;
 898
 899        ret = ov5640_write_reg(sensor, reg, val >> 8);
 900        if (ret)
 901                return ret;
 902
 903        return ov5640_write_reg(sensor, reg + 1, val & 0xff);
 904}
 905
 906static int ov5640_mod_reg(struct ov5640_dev *sensor, u16 reg,
 907                          u8 mask, u8 val)
 908{
 909        u8 readval;
 910        int ret;
 911
 912        ret = ov5640_read_reg(sensor, reg, &readval);
 913        if (ret)
 914                return ret;
 915
 916        readval &= ~mask;
 917        val &= mask;
 918        val |= readval;
 919
 920        return ov5640_write_reg(sensor, reg, val);
 921}
 922
 923/* download ov5640 settings to sensor through i2c */
 924static int ov5640_load_regs(struct ov5640_dev *sensor,
 925                            const struct ov5640_mode_info *mode)
 926{
 927        const struct reg_value *regs = mode->reg_data;
 928        unsigned int i;
 929        u32 delay_ms;
 930        u16 reg_addr;
 931        u8 mask, val;
 932        int ret = 0;
 933
 934        for (i = 0; i < mode->reg_data_size; ++i, ++regs) {
 935                delay_ms = regs->delay_ms;
 936                reg_addr = regs->reg_addr;
 937                val = regs->val;
 938                mask = regs->mask;
 939
 940                if (mask)
 941                        ret = ov5640_mod_reg(sensor, reg_addr, mask, val);
 942                else
 943                        ret = ov5640_write_reg(sensor, reg_addr, val);
 944                if (ret)
 945                        break;
 946
 947                if (delay_ms)
 948                        usleep_range(1000 * delay_ms, 1000 * delay_ms + 100);
 949        }
 950
 951        return ret;
 952}
 953
 954/* read exposure, in number of line periods */
 955static int ov5640_get_exposure(struct ov5640_dev *sensor)
 956{
 957        int exp, ret;
 958        u8 temp;
 959
 960        ret = ov5640_read_reg(sensor, OV5640_REG_AEC_PK_EXPOSURE_HI, &temp);
 961        if (ret)
 962                return ret;
 963        exp = ((int)temp & 0x0f) << 16;
 964        ret = ov5640_read_reg(sensor, OV5640_REG_AEC_PK_EXPOSURE_MED, &temp);
 965        if (ret)
 966                return ret;
 967        exp |= ((int)temp << 8);
 968        ret = ov5640_read_reg(sensor, OV5640_REG_AEC_PK_EXPOSURE_LO, &temp);
 969        if (ret)
 970                return ret;
 971        exp |= (int)temp;
 972
 973        return exp >> 4;
 974}
 975
 976/* write exposure, given number of line periods */
 977static int ov5640_set_exposure(struct ov5640_dev *sensor, u32 exposure)
 978{
 979        int ret;
 980
 981        exposure <<= 4;
 982
 983        ret = ov5640_write_reg(sensor,
 984                               OV5640_REG_AEC_PK_EXPOSURE_LO,
 985                               exposure & 0xff);
 986        if (ret)
 987                return ret;
 988        ret = ov5640_write_reg(sensor,
 989                               OV5640_REG_AEC_PK_EXPOSURE_MED,
 990                               (exposure >> 8) & 0xff);
 991        if (ret)
 992                return ret;
 993        return ov5640_write_reg(sensor,
 994                                OV5640_REG_AEC_PK_EXPOSURE_HI,
 995                                (exposure >> 16) & 0x0f);
 996}
 997
 998static int ov5640_get_gain(struct ov5640_dev *sensor)
 999{
1000        u16 gain;
1001        int ret;
1002
1003        ret = ov5640_read_reg16(sensor, OV5640_REG_AEC_PK_REAL_GAIN, &gain);
1004        if (ret)
1005                return ret;
1006
1007        return gain & 0x3ff;
1008}
1009
1010static int ov5640_set_stream_dvp(struct ov5640_dev *sensor, bool on)
1011{
1012        int ret;
1013        unsigned int flags = sensor->ep.bus.parallel.flags;
1014        u8 pclk_pol = 0;
1015        u8 hsync_pol = 0;
1016        u8 vsync_pol = 0;
1017
1018        /*
1019         * Note about parallel port configuration.
1020         *
1021         * When configured in parallel mode, the OV5640 will
1022         * output 10 bits data on DVP data lines [9:0].
1023         * If only 8 bits data are wanted, the 8 bits data lines
1024         * of the camera interface must be physically connected
1025         * on the DVP data lines [9:2].
1026         *
1027         * Control lines polarity can be configured through
1028         * devicetree endpoint control lines properties.
1029         * If no endpoint control lines properties are set,
1030         * polarity will be as below:
1031         * - VSYNC:     active high
1032         * - HREF:      active low
1033         * - PCLK:      active low
1034         */
1035
1036        if (on) {
1037                /*
1038                 * reset MIPI PCLK/SERCLK divider
1039                 *
1040                 * SC PLL CONTRL1 0
1041                 * - [3..0]:    MIPI PCLK/SERCLK divider
1042                 */
1043                ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL1, 0x0f, 0);
1044                if (ret)
1045                        return ret;
1046
1047                /*
1048                 * configure parallel port control lines polarity
1049                 *
1050                 * POLARITY CTRL0
1051                 * - [5]:       PCLK polarity (0: active low, 1: active high)
1052                 * - [1]:       HREF polarity (0: active low, 1: active high)
1053                 * - [0]:       VSYNC polarity (mismatch here between
1054                 *              datasheet and hardware, 0 is active high
1055                 *              and 1 is active low...)
1056                 */
1057                if (flags & V4L2_MBUS_PCLK_SAMPLE_RISING)
1058                        pclk_pol = 1;
1059                if (flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH)
1060                        hsync_pol = 1;
1061                if (flags & V4L2_MBUS_VSYNC_ACTIVE_LOW)
1062                        vsync_pol = 1;
1063
1064                ret = ov5640_write_reg(sensor,
1065                                       OV5640_REG_POLARITY_CTRL00,
1066                                       (pclk_pol << 5) |
1067                                       (hsync_pol << 1) |
1068                                       vsync_pol);
1069
1070                if (ret)
1071                        return ret;
1072        }
1073
1074        /*
1075         * powerdown MIPI TX/RX PHY & disable MIPI
1076         *
1077         * MIPI CONTROL 00
1078         * 4:    PWDN PHY TX
1079         * 3:    PWDN PHY RX
1080         * 2:    MIPI enable
1081         */
1082        ret = ov5640_write_reg(sensor,
1083                               OV5640_REG_IO_MIPI_CTRL00, on ? 0x18 : 0);
1084        if (ret)
1085                return ret;
1086
1087        /*
1088         * enable VSYNC/HREF/PCLK DVP control lines
1089         * & D[9:6] DVP data lines
1090         *
1091         * PAD OUTPUT ENABLE 01
1092         * - 6:         VSYNC output enable
1093         * - 5:         HREF output enable
1094         * - 4:         PCLK output enable
1095         * - [3:0]:     D[9:6] output enable
1096         */
1097        ret = ov5640_write_reg(sensor,
1098                               OV5640_REG_PAD_OUTPUT_ENABLE01,
1099                               on ? 0x7f : 0);
1100        if (ret)
1101                return ret;
1102
1103        /*
1104         * enable D[5:0] DVP data lines
1105         *
1106         * PAD OUTPUT ENABLE 02
1107         * - [7:2]:     D[5:0] output enable
1108         */
1109        return ov5640_write_reg(sensor,
1110                                OV5640_REG_PAD_OUTPUT_ENABLE02,
1111                                on ? 0xfc : 0);
1112}
1113
1114static int ov5640_set_stream_mipi(struct ov5640_dev *sensor, bool on)
1115{
1116        int ret;
1117
1118        ret = ov5640_mod_reg(sensor, OV5640_REG_MIPI_CTRL00, BIT(5),
1119                             on ? 0 : BIT(5));
1120        if (ret)
1121                return ret;
1122        ret = ov5640_write_reg(sensor, OV5640_REG_PAD_OUTPUT00,
1123                               on ? 0x00 : 0x70);
1124        if (ret)
1125                return ret;
1126
1127        return ov5640_write_reg(sensor, OV5640_REG_FRAME_CTRL01,
1128                                on ? 0x00 : 0x0f);
1129}
1130
1131static int ov5640_get_sysclk(struct ov5640_dev *sensor)
1132{
1133         /* calculate sysclk */
1134        u32 xvclk = sensor->xclk_freq / 10000;
1135        u32 multiplier, prediv, VCO, sysdiv, pll_rdiv;
1136        u32 sclk_rdiv_map[] = {1, 2, 4, 8};
1137        u32 bit_div2x = 1, sclk_rdiv, sysclk;
1138        u8 temp1, temp2;
1139        int ret;
1140
1141        ret = ov5640_read_reg(sensor, OV5640_REG_SC_PLL_CTRL0, &temp1);
1142        if (ret)
1143                return ret;
1144        temp2 = temp1 & 0x0f;
1145        if (temp2 == 8 || temp2 == 10)
1146                bit_div2x = temp2 / 2;
1147
1148        ret = ov5640_read_reg(sensor, OV5640_REG_SC_PLL_CTRL1, &temp1);
1149        if (ret)
1150                return ret;
1151        sysdiv = temp1 >> 4;
1152        if (sysdiv == 0)
1153                sysdiv = 16;
1154
1155        ret = ov5640_read_reg(sensor, OV5640_REG_SC_PLL_CTRL2, &temp1);
1156        if (ret)
1157                return ret;
1158        multiplier = temp1;
1159
1160        ret = ov5640_read_reg(sensor, OV5640_REG_SC_PLL_CTRL3, &temp1);
1161        if (ret)
1162                return ret;
1163        prediv = temp1 & 0x0f;
1164        pll_rdiv = ((temp1 >> 4) & 0x01) + 1;
1165
1166        ret = ov5640_read_reg(sensor, OV5640_REG_SYS_ROOT_DIVIDER, &temp1);
1167        if (ret)
1168                return ret;
1169        temp2 = temp1 & 0x03;
1170        sclk_rdiv = sclk_rdiv_map[temp2];
1171
1172        if (!prediv || !sysdiv || !pll_rdiv || !bit_div2x)
1173                return -EINVAL;
1174
1175        VCO = xvclk * multiplier / prediv;
1176
1177        sysclk = VCO / sysdiv / pll_rdiv * 2 / bit_div2x / sclk_rdiv;
1178
1179        return sysclk;
1180}
1181
1182static int ov5640_set_night_mode(struct ov5640_dev *sensor)
1183{
1184         /* read HTS from register settings */
1185        u8 mode;
1186        int ret;
1187
1188        ret = ov5640_read_reg(sensor, OV5640_REG_AEC_CTRL00, &mode);
1189        if (ret)
1190                return ret;
1191        mode &= 0xfb;
1192        return ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL00, mode);
1193}
1194
1195static int ov5640_get_hts(struct ov5640_dev *sensor)
1196{
1197        /* read HTS from register settings */
1198        u16 hts;
1199        int ret;
1200
1201        ret = ov5640_read_reg16(sensor, OV5640_REG_TIMING_HTS, &hts);
1202        if (ret)
1203                return ret;
1204        return hts;
1205}
1206
1207static int ov5640_get_vts(struct ov5640_dev *sensor)
1208{
1209        u16 vts;
1210        int ret;
1211
1212        ret = ov5640_read_reg16(sensor, OV5640_REG_TIMING_VTS, &vts);
1213        if (ret)
1214                return ret;
1215        return vts;
1216}
1217
1218static int ov5640_set_vts(struct ov5640_dev *sensor, int vts)
1219{
1220        return ov5640_write_reg16(sensor, OV5640_REG_TIMING_VTS, vts);
1221}
1222
1223static int ov5640_get_light_freq(struct ov5640_dev *sensor)
1224{
1225        /* get banding filter value */
1226        int ret, light_freq = 0;
1227        u8 temp, temp1;
1228
1229        ret = ov5640_read_reg(sensor, OV5640_REG_HZ5060_CTRL01, &temp);
1230        if (ret)
1231                return ret;
1232
1233        if (temp & 0x80) {
1234                /* manual */
1235                ret = ov5640_read_reg(sensor, OV5640_REG_HZ5060_CTRL00,
1236                                      &temp1);
1237                if (ret)
1238                        return ret;
1239                if (temp1 & 0x04) {
1240                        /* 50Hz */
1241                        light_freq = 50;
1242                } else {
1243                        /* 60Hz */
1244                        light_freq = 60;
1245                }
1246        } else {
1247                /* auto */
1248                ret = ov5640_read_reg(sensor, OV5640_REG_SIGMADELTA_CTRL0C,
1249                                      &temp1);
1250                if (ret)
1251                        return ret;
1252
1253                if (temp1 & 0x01) {
1254                        /* 50Hz */
1255                        light_freq = 50;
1256                } else {
1257                        /* 60Hz */
1258                }
1259        }
1260
1261        return light_freq;
1262}
1263
1264static int ov5640_set_bandingfilter(struct ov5640_dev *sensor)
1265{
1266        u32 band_step60, max_band60, band_step50, max_band50, prev_vts;
1267        int ret;
1268
1269        /* read preview PCLK */
1270        ret = ov5640_get_sysclk(sensor);
1271        if (ret < 0)
1272                return ret;
1273        if (ret == 0)
1274                return -EINVAL;
1275        sensor->prev_sysclk = ret;
1276        /* read preview HTS */
1277        ret = ov5640_get_hts(sensor);
1278        if (ret < 0)
1279                return ret;
1280        if (ret == 0)
1281                return -EINVAL;
1282        sensor->prev_hts = ret;
1283
1284        /* read preview VTS */
1285        ret = ov5640_get_vts(sensor);
1286        if (ret < 0)
1287                return ret;
1288        prev_vts = ret;
1289
1290        /* calculate banding filter */
1291        /* 60Hz */
1292        band_step60 = sensor->prev_sysclk * 100 / sensor->prev_hts * 100 / 120;
1293        ret = ov5640_write_reg16(sensor, OV5640_REG_AEC_B60_STEP, band_step60);
1294        if (ret)
1295                return ret;
1296        if (!band_step60)
1297                return -EINVAL;
1298        max_band60 = (int)((prev_vts - 4) / band_step60);
1299        ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL0D, max_band60);
1300        if (ret)
1301                return ret;
1302
1303        /* 50Hz */
1304        band_step50 = sensor->prev_sysclk * 100 / sensor->prev_hts;
1305        ret = ov5640_write_reg16(sensor, OV5640_REG_AEC_B50_STEP, band_step50);
1306        if (ret)
1307                return ret;
1308        if (!band_step50)
1309                return -EINVAL;
1310        max_band50 = (int)((prev_vts - 4) / band_step50);
1311        return ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL0E, max_band50);
1312}
1313
1314static int ov5640_set_ae_target(struct ov5640_dev *sensor, int target)
1315{
1316        /* stable in high */
1317        u32 fast_high, fast_low;
1318        int ret;
1319
1320        sensor->ae_low = target * 23 / 25;      /* 0.92 */
1321        sensor->ae_high = target * 27 / 25;     /* 1.08 */
1322
1323        fast_high = sensor->ae_high << 1;
1324        if (fast_high > 255)
1325                fast_high = 255;
1326
1327        fast_low = sensor->ae_low >> 1;
1328
1329        ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL0F, sensor->ae_high);
1330        if (ret)
1331                return ret;
1332        ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL10, sensor->ae_low);
1333        if (ret)
1334                return ret;
1335        ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL1B, sensor->ae_high);
1336        if (ret)
1337                return ret;
1338        ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL1E, sensor->ae_low);
1339        if (ret)
1340                return ret;
1341        ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL11, fast_high);
1342        if (ret)
1343                return ret;
1344        return ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL1F, fast_low);
1345}
1346
1347static int ov5640_binning_on(struct ov5640_dev *sensor)
1348{
1349        u8 temp;
1350        int ret;
1351
1352        ret = ov5640_read_reg(sensor, OV5640_REG_TIMING_TC_REG21, &temp);
1353        if (ret)
1354                return ret;
1355        temp &= 0xfe;
1356        return temp ? 1 : 0;
1357}
1358
1359static int ov5640_set_virtual_channel(struct ov5640_dev *sensor)
1360{
1361        struct i2c_client *client = sensor->i2c_client;
1362        u8 temp, channel = virtual_channel;
1363        int ret;
1364
1365        if (channel > 3) {
1366                dev_err(&client->dev,
1367                        "%s: wrong virtual_channel parameter, expected (0..3), got %d\n",
1368                        __func__, channel);
1369                return -EINVAL;
1370        }
1371
1372        ret = ov5640_read_reg(sensor, OV5640_REG_DEBUG_MODE, &temp);
1373        if (ret)
1374                return ret;
1375        temp &= ~(3 << 6);
1376        temp |= (channel << 6);
1377        return ov5640_write_reg(sensor, OV5640_REG_DEBUG_MODE, temp);
1378}
1379
1380static const struct ov5640_mode_info *
1381ov5640_find_mode(struct ov5640_dev *sensor, enum ov5640_frame_rate fr,
1382                 int width, int height, bool nearest)
1383{
1384        const struct ov5640_mode_info *mode = NULL;
1385        int i;
1386
1387        for (i = OV5640_NUM_MODES - 1; i >= 0; i--) {
1388                mode = &ov5640_mode_data[fr][i];
1389
1390                if (!mode->reg_data)
1391                        continue;
1392
1393                if ((nearest && mode->width <= width &&
1394                     mode->height <= height) ||
1395                    (!nearest && mode->width == width &&
1396                     mode->height == height))
1397                        break;
1398        }
1399
1400        if (nearest && i < 0)
1401                mode = &ov5640_mode_data[fr][0];
1402
1403        return mode;
1404}
1405
1406/*
1407 * sensor changes between scaling and subsampling, go through
1408 * exposure calculation
1409 */
1410static int ov5640_set_mode_exposure_calc(struct ov5640_dev *sensor,
1411                                         const struct ov5640_mode_info *mode)
1412{
1413        u32 prev_shutter, prev_gain16;
1414        u32 cap_shutter, cap_gain16;
1415        u32 cap_sysclk, cap_hts, cap_vts;
1416        u32 light_freq, cap_bandfilt, cap_maxband;
1417        u32 cap_gain16_shutter;
1418        u8 average;
1419        int ret;
1420
1421        if (!mode->reg_data)
1422                return -EINVAL;
1423
1424        /* read preview shutter */
1425        ret = ov5640_get_exposure(sensor);
1426        if (ret < 0)
1427                return ret;
1428        prev_shutter = ret;
1429        ret = ov5640_binning_on(sensor);
1430        if (ret < 0)
1431                return ret;
1432        if (ret && mode->id != OV5640_MODE_720P_1280_720 &&
1433            mode->id != OV5640_MODE_1080P_1920_1080)
1434                prev_shutter *= 2;
1435
1436        /* read preview gain */
1437        ret = ov5640_get_gain(sensor);
1438        if (ret < 0)
1439                return ret;
1440        prev_gain16 = ret;
1441
1442        /* get average */
1443        ret = ov5640_read_reg(sensor, OV5640_REG_AVG_READOUT, &average);
1444        if (ret)
1445                return ret;
1446
1447        /* turn off night mode for capture */
1448        ret = ov5640_set_night_mode(sensor);
1449        if (ret < 0)
1450                return ret;
1451
1452        /* Write capture setting */
1453        ret = ov5640_load_regs(sensor, mode);
1454        if (ret < 0)
1455                return ret;
1456
1457        /* read capture VTS */
1458        ret = ov5640_get_vts(sensor);
1459        if (ret < 0)
1460                return ret;
1461        cap_vts = ret;
1462        ret = ov5640_get_hts(sensor);
1463        if (ret < 0)
1464                return ret;
1465        if (ret == 0)
1466                return -EINVAL;
1467        cap_hts = ret;
1468
1469        ret = ov5640_get_sysclk(sensor);
1470        if (ret < 0)
1471                return ret;
1472        if (ret == 0)
1473                return -EINVAL;
1474        cap_sysclk = ret;
1475
1476        /* calculate capture banding filter */
1477        ret = ov5640_get_light_freq(sensor);
1478        if (ret < 0)
1479                return ret;
1480        light_freq = ret;
1481
1482        if (light_freq == 60) {
1483                /* 60Hz */
1484                cap_bandfilt = cap_sysclk * 100 / cap_hts * 100 / 120;
1485        } else {
1486                /* 50Hz */
1487                cap_bandfilt = cap_sysclk * 100 / cap_hts;
1488        }
1489
1490        if (!sensor->prev_sysclk) {
1491                ret = ov5640_get_sysclk(sensor);
1492                if (ret < 0)
1493                        return ret;
1494                if (ret == 0)
1495                        return -EINVAL;
1496                sensor->prev_sysclk = ret;
1497        }
1498
1499        if (!cap_bandfilt)
1500                return -EINVAL;
1501
1502        cap_maxband = (int)((cap_vts - 4) / cap_bandfilt);
1503
1504        /* calculate capture shutter/gain16 */
1505        if (average > sensor->ae_low && average < sensor->ae_high) {
1506                /* in stable range */
1507                cap_gain16_shutter =
1508                        prev_gain16 * prev_shutter *
1509                        cap_sysclk / sensor->prev_sysclk *
1510                        sensor->prev_hts / cap_hts *
1511                        sensor->ae_target / average;
1512        } else {
1513                cap_gain16_shutter =
1514                        prev_gain16 * prev_shutter *
1515                        cap_sysclk / sensor->prev_sysclk *
1516                        sensor->prev_hts / cap_hts;
1517        }
1518
1519        /* gain to shutter */
1520        if (cap_gain16_shutter < (cap_bandfilt * 16)) {
1521                /* shutter < 1/100 */
1522                cap_shutter = cap_gain16_shutter / 16;
1523                if (cap_shutter < 1)
1524                        cap_shutter = 1;
1525
1526                cap_gain16 = cap_gain16_shutter / cap_shutter;
1527                if (cap_gain16 < 16)
1528                        cap_gain16 = 16;
1529        } else {
1530                if (cap_gain16_shutter > (cap_bandfilt * cap_maxband * 16)) {
1531                        /* exposure reach max */
1532                        cap_shutter = cap_bandfilt * cap_maxband;
1533                        if (!cap_shutter)
1534                                return -EINVAL;
1535
1536                        cap_gain16 = cap_gain16_shutter / cap_shutter;
1537                } else {
1538                        /* 1/100 < (cap_shutter = n/100) =< max */
1539                        cap_shutter =
1540                                ((int)(cap_gain16_shutter / 16 / cap_bandfilt))
1541                                * cap_bandfilt;
1542                        if (!cap_shutter)
1543                                return -EINVAL;
1544
1545                        cap_gain16 = cap_gain16_shutter / cap_shutter;
1546                }
1547        }
1548
1549        /* set capture gain */
1550        ret = __v4l2_ctrl_s_ctrl(sensor->ctrls.gain, cap_gain16);
1551        if (ret)
1552                return ret;
1553
1554        /* write capture shutter */
1555        if (cap_shutter > (cap_vts - 4)) {
1556                cap_vts = cap_shutter + 4;
1557                ret = ov5640_set_vts(sensor, cap_vts);
1558                if (ret < 0)
1559                        return ret;
1560        }
1561
1562        /* set exposure */
1563        return __v4l2_ctrl_s_ctrl(sensor->ctrls.exposure, cap_shutter);
1564}
1565
1566/*
1567 * if sensor changes inside scaling or subsampling
1568 * change mode directly
1569 */
1570static int ov5640_set_mode_direct(struct ov5640_dev *sensor,
1571                                  const struct ov5640_mode_info *mode)
1572{
1573        int ret;
1574
1575        if (!mode->reg_data)
1576                return -EINVAL;
1577
1578        /* Write capture setting */
1579        ret = ov5640_load_regs(sensor, mode);
1580        if (ret < 0)
1581                return ret;
1582
1583        /* turn auto gain/exposure back on for direct mode */
1584        ret = __v4l2_ctrl_s_ctrl(sensor->ctrls.auto_gain, 1);
1585        if (ret)
1586                return ret;
1587        return __v4l2_ctrl_s_ctrl(sensor->ctrls.auto_exp, V4L2_EXPOSURE_AUTO);
1588}
1589
1590static int ov5640_set_mode(struct ov5640_dev *sensor,
1591                           const struct ov5640_mode_info *orig_mode)
1592{
1593        const struct ov5640_mode_info *mode = sensor->current_mode;
1594        enum ov5640_downsize_mode dn_mode, orig_dn_mode;
1595        int ret;
1596
1597        dn_mode = mode->dn_mode;
1598        orig_dn_mode = orig_mode->dn_mode;
1599
1600        /* auto gain and exposure must be turned off when changing modes */
1601        ret = __v4l2_ctrl_s_ctrl(sensor->ctrls.auto_gain, 0);
1602        if (ret)
1603                return ret;
1604        ret = __v4l2_ctrl_s_ctrl(sensor->ctrls.auto_exp, V4L2_EXPOSURE_MANUAL);
1605        if (ret)
1606                return ret;
1607
1608        if ((dn_mode == SUBSAMPLING && orig_dn_mode == SCALING) ||
1609            (dn_mode == SCALING && orig_dn_mode == SUBSAMPLING)) {
1610                /*
1611                 * change between subsampling and scaling
1612                 * go through exposure calucation
1613                 */
1614                ret = ov5640_set_mode_exposure_calc(sensor, mode);
1615        } else {
1616                /*
1617                 * change inside subsampling or scaling
1618                 * download firmware directly
1619                 */
1620                ret = ov5640_set_mode_direct(sensor, mode);
1621        }
1622
1623        if (ret < 0)
1624                return ret;
1625
1626        ret = ov5640_set_ae_target(sensor, sensor->ae_target);
1627        if (ret < 0)
1628                return ret;
1629        ret = ov5640_get_light_freq(sensor);
1630        if (ret < 0)
1631                return ret;
1632        ret = ov5640_set_bandingfilter(sensor);
1633        if (ret < 0)
1634                return ret;
1635        ret = ov5640_set_virtual_channel(sensor);
1636        if (ret < 0)
1637                return ret;
1638
1639        sensor->pending_mode_change = false;
1640
1641        return 0;
1642}
1643
1644static int ov5640_set_framefmt(struct ov5640_dev *sensor,
1645                               struct v4l2_mbus_framefmt *format);
1646
1647/* restore the last set video mode after chip power-on */
1648static int ov5640_restore_mode(struct ov5640_dev *sensor)
1649{
1650        int ret;
1651
1652        /* first load the initial register values */
1653        ret = ov5640_load_regs(sensor, &ov5640_mode_init_data);
1654        if (ret < 0)
1655                return ret;
1656
1657        /* now restore the last capture mode */
1658        ret = ov5640_set_mode(sensor, &ov5640_mode_init_data);
1659        if (ret < 0)
1660                return ret;
1661
1662        return ov5640_set_framefmt(sensor, &sensor->fmt);
1663}
1664
1665static void ov5640_power(struct ov5640_dev *sensor, bool enable)
1666{
1667        gpiod_set_value_cansleep(sensor->pwdn_gpio, enable ? 0 : 1);
1668}
1669
1670static void ov5640_reset(struct ov5640_dev *sensor)
1671{
1672        if (!sensor->reset_gpio)
1673                return;
1674
1675        gpiod_set_value_cansleep(sensor->reset_gpio, 0);
1676
1677        /* camera power cycle */
1678        ov5640_power(sensor, false);
1679        usleep_range(5000, 10000);
1680        ov5640_power(sensor, true);
1681        usleep_range(5000, 10000);
1682
1683        gpiod_set_value_cansleep(sensor->reset_gpio, 1);
1684        usleep_range(1000, 2000);
1685
1686        gpiod_set_value_cansleep(sensor->reset_gpio, 0);
1687        usleep_range(5000, 10000);
1688}
1689
1690static int ov5640_set_power_on(struct ov5640_dev *sensor)
1691{
1692        struct i2c_client *client = sensor->i2c_client;
1693        int ret;
1694
1695        ret = clk_prepare_enable(sensor->xclk);
1696        if (ret) {
1697                dev_err(&client->dev, "%s: failed to enable clock\n",
1698                        __func__);
1699                return ret;
1700        }
1701
1702        ret = regulator_bulk_enable(OV5640_NUM_SUPPLIES,
1703                                    sensor->supplies);
1704        if (ret) {
1705                dev_err(&client->dev, "%s: failed to enable regulators\n",
1706                        __func__);
1707                goto xclk_off;
1708        }
1709
1710        ov5640_reset(sensor);
1711        ov5640_power(sensor, true);
1712
1713        ret = ov5640_init_slave_id(sensor);
1714        if (ret)
1715                goto power_off;
1716
1717        return 0;
1718
1719power_off:
1720        ov5640_power(sensor, false);
1721        regulator_bulk_disable(OV5640_NUM_SUPPLIES, sensor->supplies);
1722xclk_off:
1723        clk_disable_unprepare(sensor->xclk);
1724        return ret;
1725}
1726
1727static void ov5640_set_power_off(struct ov5640_dev *sensor)
1728{
1729        ov5640_power(sensor, false);
1730        regulator_bulk_disable(OV5640_NUM_SUPPLIES, sensor->supplies);
1731        clk_disable_unprepare(sensor->xclk);
1732}
1733
1734static int ov5640_set_power(struct ov5640_dev *sensor, bool on)
1735{
1736        int ret = 0;
1737
1738        if (on) {
1739                ret = ov5640_set_power_on(sensor);
1740                if (ret)
1741                        return ret;
1742
1743                ret = ov5640_restore_mode(sensor);
1744                if (ret)
1745                        goto power_off;
1746
1747                if (sensor->ep.bus_type == V4L2_MBUS_CSI2) {
1748                        /*
1749                         * start streaming briefly followed by stream off in
1750                         * order to coax the clock lane into LP-11 state.
1751                         */
1752                        ret = ov5640_set_stream_mipi(sensor, true);
1753                        if (ret)
1754                                goto power_off;
1755                        usleep_range(1000, 2000);
1756                        ret = ov5640_set_stream_mipi(sensor, false);
1757                        if (ret)
1758                                goto power_off;
1759                }
1760
1761                return 0;
1762        }
1763
1764power_off:
1765        ov5640_set_power_off(sensor);
1766        return ret;
1767}
1768
1769/* --------------- Subdev Operations --------------- */
1770
1771static int ov5640_s_power(struct v4l2_subdev *sd, int on)
1772{
1773        struct ov5640_dev *sensor = to_ov5640_dev(sd);
1774        int ret = 0;
1775
1776        mutex_lock(&sensor->lock);
1777
1778        /*
1779         * If the power count is modified from 0 to != 0 or from != 0 to 0,
1780         * update the power state.
1781         */
1782        if (sensor->power_count == !on) {
1783                ret = ov5640_set_power(sensor, !!on);
1784                if (ret)
1785                        goto out;
1786        }
1787
1788        /* Update the power count. */
1789        sensor->power_count += on ? 1 : -1;
1790        WARN_ON(sensor->power_count < 0);
1791out:
1792        mutex_unlock(&sensor->lock);
1793
1794        if (on && !ret && sensor->power_count == 1) {
1795                /* restore controls */
1796                ret = v4l2_ctrl_handler_setup(&sensor->ctrls.handler);
1797        }
1798
1799        return ret;
1800}
1801
1802static int ov5640_try_frame_interval(struct ov5640_dev *sensor,
1803                                     struct v4l2_fract *fi,
1804                                     u32 width, u32 height)
1805{
1806        const struct ov5640_mode_info *mode;
1807        u32 minfps, maxfps, fps;
1808        int ret;
1809
1810        minfps = ov5640_framerates[OV5640_15_FPS];
1811        maxfps = ov5640_framerates[OV5640_30_FPS];
1812
1813        if (fi->numerator == 0) {
1814                fi->denominator = maxfps;
1815                fi->numerator = 1;
1816                return OV5640_30_FPS;
1817        }
1818
1819        fps = DIV_ROUND_CLOSEST(fi->denominator, fi->numerator);
1820
1821        fi->numerator = 1;
1822        if (fps > maxfps)
1823                fi->denominator = maxfps;
1824        else if (fps < minfps)
1825                fi->denominator = minfps;
1826        else if (2 * fps >= 2 * minfps + (maxfps - minfps))
1827                fi->denominator = maxfps;
1828        else
1829                fi->denominator = minfps;
1830
1831        ret = (fi->denominator == minfps) ? OV5640_15_FPS : OV5640_30_FPS;
1832
1833        mode = ov5640_find_mode(sensor, ret, width, height, false);
1834        return mode ? ret : -EINVAL;
1835}
1836
1837static int ov5640_get_fmt(struct v4l2_subdev *sd,
1838                          struct v4l2_subdev_pad_config *cfg,
1839                          struct v4l2_subdev_format *format)
1840{
1841        struct ov5640_dev *sensor = to_ov5640_dev(sd);
1842        struct v4l2_mbus_framefmt *fmt;
1843
1844        if (format->pad != 0)
1845                return -EINVAL;
1846
1847        mutex_lock(&sensor->lock);
1848
1849        if (format->which == V4L2_SUBDEV_FORMAT_TRY)
1850                fmt = v4l2_subdev_get_try_format(&sensor->sd, cfg,
1851                                                 format->pad);
1852        else
1853                fmt = &sensor->fmt;
1854
1855        format->format = *fmt;
1856
1857        mutex_unlock(&sensor->lock);
1858
1859        return 0;
1860}
1861
1862static int ov5640_try_fmt_internal(struct v4l2_subdev *sd,
1863                                   struct v4l2_mbus_framefmt *fmt,
1864                                   enum ov5640_frame_rate fr,
1865                                   const struct ov5640_mode_info **new_mode)
1866{
1867        struct ov5640_dev *sensor = to_ov5640_dev(sd);
1868        const struct ov5640_mode_info *mode;
1869        int i;
1870
1871        mode = ov5640_find_mode(sensor, fr, fmt->width, fmt->height, true);
1872        if (!mode)
1873                return -EINVAL;
1874        fmt->width = mode->width;
1875        fmt->height = mode->height;
1876
1877        if (new_mode)
1878                *new_mode = mode;
1879
1880        for (i = 0; i < ARRAY_SIZE(ov5640_formats); i++)
1881                if (ov5640_formats[i].code == fmt->code)
1882                        break;
1883        if (i >= ARRAY_SIZE(ov5640_formats))
1884                i = 0;
1885
1886        fmt->code = ov5640_formats[i].code;
1887        fmt->colorspace = ov5640_formats[i].colorspace;
1888        fmt->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(fmt->colorspace);
1889        fmt->quantization = V4L2_QUANTIZATION_FULL_RANGE;
1890        fmt->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(fmt->colorspace);
1891
1892        return 0;
1893}
1894
1895static int ov5640_set_fmt(struct v4l2_subdev *sd,
1896                          struct v4l2_subdev_pad_config *cfg,
1897                          struct v4l2_subdev_format *format)
1898{
1899        struct ov5640_dev *sensor = to_ov5640_dev(sd);
1900        const struct ov5640_mode_info *new_mode;
1901        struct v4l2_mbus_framefmt *mbus_fmt = &format->format;
1902        int ret;
1903
1904        if (format->pad != 0)
1905                return -EINVAL;
1906
1907        mutex_lock(&sensor->lock);
1908
1909        if (sensor->streaming) {
1910                ret = -EBUSY;
1911                goto out;
1912        }
1913
1914        ret = ov5640_try_fmt_internal(sd, mbus_fmt,
1915                                      sensor->current_fr, &new_mode);
1916        if (ret)
1917                goto out;
1918
1919        if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
1920                struct v4l2_mbus_framefmt *fmt =
1921                        v4l2_subdev_get_try_format(sd, cfg, 0);
1922
1923                *fmt = *mbus_fmt;
1924                goto out;
1925        }
1926
1927        sensor->current_mode = new_mode;
1928        sensor->fmt = *mbus_fmt;
1929        sensor->pending_mode_change = true;
1930out:
1931        mutex_unlock(&sensor->lock);
1932        return ret;
1933}
1934
1935static int ov5640_set_framefmt(struct ov5640_dev *sensor,
1936                               struct v4l2_mbus_framefmt *format)
1937{
1938        int ret = 0;
1939        bool is_rgb = false;
1940        bool is_jpeg = false;
1941        u8 val;
1942
1943        switch (format->code) {
1944        case MEDIA_BUS_FMT_UYVY8_2X8:
1945                /* YUV422, UYVY */
1946                val = 0x3f;
1947                break;
1948        case MEDIA_BUS_FMT_YUYV8_2X8:
1949                /* YUV422, YUYV */
1950                val = 0x30;
1951                break;
1952        case MEDIA_BUS_FMT_RGB565_2X8_LE:
1953                /* RGB565 {g[2:0],b[4:0]},{r[4:0],g[5:3]} */
1954                val = 0x6F;
1955                is_rgb = true;
1956                break;
1957        case MEDIA_BUS_FMT_RGB565_2X8_BE:
1958                /* RGB565 {r[4:0],g[5:3]},{g[2:0],b[4:0]} */
1959                val = 0x61;
1960                is_rgb = true;
1961                break;
1962        case MEDIA_BUS_FMT_JPEG_1X8:
1963                /* YUV422, YUYV */
1964                val = 0x30;
1965                is_jpeg = true;
1966                break;
1967        default:
1968                return -EINVAL;
1969        }
1970
1971        /* FORMAT CONTROL00: YUV and RGB formatting */
1972        ret = ov5640_write_reg(sensor, OV5640_REG_FORMAT_CONTROL00, val);
1973        if (ret)
1974                return ret;
1975
1976        /* FORMAT MUX CONTROL: ISP YUV or RGB */
1977        ret = ov5640_write_reg(sensor, OV5640_REG_ISP_FORMAT_MUX_CTRL,
1978                               is_rgb ? 0x01 : 0x00);
1979        if (ret)
1980                return ret;
1981
1982        /*
1983         * TIMING TC REG21:
1984         * - [5]:       JPEG enable
1985         */
1986        ret = ov5640_mod_reg(sensor, OV5640_REG_TIMING_TC_REG21,
1987                             BIT(5), is_jpeg ? BIT(5) : 0);
1988        if (ret)
1989                return ret;
1990
1991        /*
1992         * SYSTEM RESET02:
1993         * - [4]:       Reset JFIFO
1994         * - [3]:       Reset SFIFO
1995         * - [2]:       Reset JPEG
1996         */
1997        ret = ov5640_mod_reg(sensor, OV5640_REG_SYS_RESET02,
1998                             BIT(4) | BIT(3) | BIT(2),
1999                             is_jpeg ? 0 : (BIT(4) | BIT(3) | BIT(2)));
2000        if (ret)
2001                return ret;
2002
2003        /*
2004         * CLOCK ENABLE02:
2005         * - [5]:       Enable JPEG 2x clock
2006         * - [3]:       Enable JPEG clock
2007         */
2008        return ov5640_mod_reg(sensor, OV5640_REG_SYS_CLOCK_ENABLE02,
2009                              BIT(5) | BIT(3),
2010                              is_jpeg ? (BIT(5) | BIT(3)) : 0);
2011}
2012
2013/*
2014 * Sensor Controls.
2015 */
2016
2017static int ov5640_set_ctrl_hue(struct ov5640_dev *sensor, int value)
2018{
2019        int ret;
2020
2021        if (value) {
2022                ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0,
2023                                     BIT(0), BIT(0));
2024                if (ret)
2025                        return ret;
2026                ret = ov5640_write_reg16(sensor, OV5640_REG_SDE_CTRL1, value);
2027        } else {
2028                ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0, BIT(0), 0);
2029        }
2030
2031        return ret;
2032}
2033
2034static int ov5640_set_ctrl_contrast(struct ov5640_dev *sensor, int value)
2035{
2036        int ret;
2037
2038        if (value) {
2039                ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0,
2040                                     BIT(2), BIT(2));
2041                if (ret)
2042                        return ret;
2043                ret = ov5640_write_reg(sensor, OV5640_REG_SDE_CTRL5,
2044                                       value & 0xff);
2045        } else {
2046                ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0, BIT(2), 0);
2047        }
2048
2049        return ret;
2050}
2051
2052static int ov5640_set_ctrl_saturation(struct ov5640_dev *sensor, int value)
2053{
2054        int ret;
2055
2056        if (value) {
2057                ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0,
2058                                     BIT(1), BIT(1));
2059                if (ret)
2060                        return ret;
2061                ret = ov5640_write_reg(sensor, OV5640_REG_SDE_CTRL3,
2062                                       value & 0xff);
2063                if (ret)
2064                        return ret;
2065                ret = ov5640_write_reg(sensor, OV5640_REG_SDE_CTRL4,
2066                                       value & 0xff);
2067        } else {
2068                ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0, BIT(1), 0);
2069        }
2070
2071        return ret;
2072}
2073
2074static int ov5640_set_ctrl_white_balance(struct ov5640_dev *sensor, int awb)
2075{
2076        int ret;
2077
2078        ret = ov5640_mod_reg(sensor, OV5640_REG_AWB_MANUAL_CTRL,
2079                             BIT(0), awb ? 0 : 1);
2080        if (ret)
2081                return ret;
2082
2083        if (!awb) {
2084                u16 red = (u16)sensor->ctrls.red_balance->val;
2085                u16 blue = (u16)sensor->ctrls.blue_balance->val;
2086
2087                ret = ov5640_write_reg16(sensor, OV5640_REG_AWB_R_GAIN, red);
2088                if (ret)
2089                        return ret;
2090                ret = ov5640_write_reg16(sensor, OV5640_REG_AWB_B_GAIN, blue);
2091        }
2092
2093        return ret;
2094}
2095
2096static int ov5640_set_ctrl_exposure(struct ov5640_dev *sensor, int exp)
2097{
2098        struct ov5640_ctrls *ctrls = &sensor->ctrls;
2099        bool auto_exposure = (exp == V4L2_EXPOSURE_AUTO);
2100        int ret = 0;
2101
2102        if (ctrls->auto_exp->is_new) {
2103                ret = ov5640_mod_reg(sensor, OV5640_REG_AEC_PK_MANUAL,
2104                                     BIT(0), auto_exposure ? 0 : BIT(0));
2105                if (ret)
2106                        return ret;
2107        }
2108
2109        if (!auto_exposure && ctrls->exposure->is_new) {
2110                u16 max_exp;
2111
2112                ret = ov5640_read_reg16(sensor, OV5640_REG_AEC_PK_VTS,
2113                                        &max_exp);
2114                if (ret)
2115                        return ret;
2116                ret = ov5640_get_vts(sensor);
2117                if (ret < 0)
2118                        return ret;
2119                max_exp += ret;
2120                ret = 0;
2121
2122                if (ctrls->exposure->val < max_exp)
2123                        ret = ov5640_set_exposure(sensor, ctrls->exposure->val);
2124        }
2125
2126        return ret;
2127}
2128
2129static int ov5640_set_ctrl_gain(struct ov5640_dev *sensor, int auto_gain)
2130{
2131        struct ov5640_ctrls *ctrls = &sensor->ctrls;
2132        int ret = 0;
2133
2134        if (ctrls->auto_gain->is_new) {
2135                ret = ov5640_mod_reg(sensor, OV5640_REG_AEC_PK_MANUAL,
2136                                     BIT(1),
2137                                     ctrls->auto_gain->val ? 0 : BIT(1));
2138                if (ret)
2139                        return ret;
2140        }
2141
2142        if (!auto_gain && ctrls->gain->is_new) {
2143                u16 gain = (u16)ctrls->gain->val;
2144
2145                ret = ov5640_write_reg16(sensor, OV5640_REG_AEC_PK_REAL_GAIN,
2146                                         gain & 0x3ff);
2147        }
2148
2149        return ret;
2150}
2151
2152static int ov5640_set_ctrl_test_pattern(struct ov5640_dev *sensor, int value)
2153{
2154        return ov5640_mod_reg(sensor, OV5640_REG_PRE_ISP_TEST_SET1,
2155                              0xa4, value ? 0xa4 : 0);
2156}
2157
2158static int ov5640_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
2159{
2160        struct v4l2_subdev *sd = ctrl_to_sd(ctrl);
2161        struct ov5640_dev *sensor = to_ov5640_dev(sd);
2162        int val;
2163
2164        /* v4l2_ctrl_lock() locks our own mutex */
2165
2166        switch (ctrl->id) {
2167        case V4L2_CID_AUTOGAIN:
2168                if (!ctrl->val)
2169                        return 0;
2170                val = ov5640_get_gain(sensor);
2171                if (val < 0)
2172                        return val;
2173                sensor->ctrls.gain->val = val;
2174                break;
2175        case V4L2_CID_EXPOSURE_AUTO:
2176                if (ctrl->val == V4L2_EXPOSURE_MANUAL)
2177                        return 0;
2178                val = ov5640_get_exposure(sensor);
2179                if (val < 0)
2180                        return val;
2181                sensor->ctrls.exposure->val = val;
2182                break;
2183        }
2184
2185        return 0;
2186}
2187
2188static int ov5640_s_ctrl(struct v4l2_ctrl *ctrl)
2189{
2190        struct v4l2_subdev *sd = ctrl_to_sd(ctrl);
2191        struct ov5640_dev *sensor = to_ov5640_dev(sd);
2192        int ret;
2193
2194        /* v4l2_ctrl_lock() locks our own mutex */
2195
2196        /*
2197         * If the device is not powered up by the host driver do
2198         * not apply any controls to H/W at this time. Instead
2199         * the controls will be restored right after power-up.
2200         */
2201        if (sensor->power_count == 0)
2202                return 0;
2203
2204        switch (ctrl->id) {
2205        case V4L2_CID_AUTOGAIN:
2206                ret = ov5640_set_ctrl_gain(sensor, ctrl->val);
2207                break;
2208        case V4L2_CID_EXPOSURE_AUTO:
2209                ret = ov5640_set_ctrl_exposure(sensor, ctrl->val);
2210                break;
2211        case V4L2_CID_AUTO_WHITE_BALANCE:
2212                ret = ov5640_set_ctrl_white_balance(sensor, ctrl->val);
2213                break;
2214        case V4L2_CID_HUE:
2215                ret = ov5640_set_ctrl_hue(sensor, ctrl->val);
2216                break;
2217        case V4L2_CID_CONTRAST:
2218                ret = ov5640_set_ctrl_contrast(sensor, ctrl->val);
2219                break;
2220        case V4L2_CID_SATURATION:
2221                ret = ov5640_set_ctrl_saturation(sensor, ctrl->val);
2222                break;
2223        case V4L2_CID_TEST_PATTERN:
2224                ret = ov5640_set_ctrl_test_pattern(sensor, ctrl->val);
2225                break;
2226        default:
2227                ret = -EINVAL;
2228                break;
2229        }
2230
2231        return ret;
2232}
2233
2234static const struct v4l2_ctrl_ops ov5640_ctrl_ops = {
2235        .g_volatile_ctrl = ov5640_g_volatile_ctrl,
2236        .s_ctrl = ov5640_s_ctrl,
2237};
2238
2239static const char * const test_pattern_menu[] = {
2240        "Disabled",
2241        "Color bars",
2242};
2243
2244static int ov5640_init_controls(struct ov5640_dev *sensor)
2245{
2246        const struct v4l2_ctrl_ops *ops = &ov5640_ctrl_ops;
2247        struct ov5640_ctrls *ctrls = &sensor->ctrls;
2248        struct v4l2_ctrl_handler *hdl = &ctrls->handler;
2249        int ret;
2250
2251        v4l2_ctrl_handler_init(hdl, 32);
2252
2253        /* we can use our own mutex for the ctrl lock */
2254        hdl->lock = &sensor->lock;
2255
2256        /* Auto/manual white balance */
2257        ctrls->auto_wb = v4l2_ctrl_new_std(hdl, ops,
2258                                           V4L2_CID_AUTO_WHITE_BALANCE,
2259                                           0, 1, 1, 1);
2260        ctrls->blue_balance = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_BLUE_BALANCE,
2261                                                0, 4095, 1, 0);
2262        ctrls->red_balance = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_RED_BALANCE,
2263                                               0, 4095, 1, 0);
2264        /* Auto/manual exposure */
2265        ctrls->auto_exp = v4l2_ctrl_new_std_menu(hdl, ops,
2266                                                 V4L2_CID_EXPOSURE_AUTO,
2267                                                 V4L2_EXPOSURE_MANUAL, 0,
2268                                                 V4L2_EXPOSURE_AUTO);
2269        ctrls->exposure = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_EXPOSURE,
2270                                            0, 65535, 1, 0);
2271        /* Auto/manual gain */
2272        ctrls->auto_gain = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_AUTOGAIN,
2273                                             0, 1, 1, 1);
2274        ctrls->gain = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_GAIN,
2275                                        0, 1023, 1, 0);
2276
2277        ctrls->saturation = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_SATURATION,
2278                                              0, 255, 1, 64);
2279        ctrls->hue = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_HUE,
2280                                       0, 359, 1, 0);
2281        ctrls->contrast = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_CONTRAST,
2282                                            0, 255, 1, 0);
2283        ctrls->test_pattern =
2284                v4l2_ctrl_new_std_menu_items(hdl, ops, V4L2_CID_TEST_PATTERN,
2285                                             ARRAY_SIZE(test_pattern_menu) - 1,
2286                                             0, 0, test_pattern_menu);
2287
2288        if (hdl->error) {
2289                ret = hdl->error;
2290                goto free_ctrls;
2291        }
2292
2293        ctrls->gain->flags |= V4L2_CTRL_FLAG_VOLATILE;
2294        ctrls->exposure->flags |= V4L2_CTRL_FLAG_VOLATILE;
2295
2296        v4l2_ctrl_auto_cluster(3, &ctrls->auto_wb, 0, false);
2297        v4l2_ctrl_auto_cluster(2, &ctrls->auto_gain, 0, true);
2298        v4l2_ctrl_auto_cluster(2, &ctrls->auto_exp, 1, true);
2299
2300        sensor->sd.ctrl_handler = hdl;
2301        return 0;
2302
2303free_ctrls:
2304        v4l2_ctrl_handler_free(hdl);
2305        return ret;
2306}
2307
2308static int ov5640_enum_frame_size(struct v4l2_subdev *sd,
2309                                  struct v4l2_subdev_pad_config *cfg,
2310                                  struct v4l2_subdev_frame_size_enum *fse)
2311{
2312        if (fse->pad != 0)
2313                return -EINVAL;
2314        if (fse->index >= OV5640_NUM_MODES)
2315                return -EINVAL;
2316
2317        fse->min_width =
2318                ov5640_mode_data[0][fse->index].width;
2319        fse->max_width = fse->min_width;
2320        fse->min_height =
2321                ov5640_mode_data[0][fse->index].height;
2322        fse->max_height = fse->min_height;
2323
2324        return 0;
2325}
2326
2327static int ov5640_enum_frame_interval(
2328        struct v4l2_subdev *sd,
2329        struct v4l2_subdev_pad_config *cfg,
2330        struct v4l2_subdev_frame_interval_enum *fie)
2331{
2332        struct ov5640_dev *sensor = to_ov5640_dev(sd);
2333        struct v4l2_fract tpf;
2334        int ret;
2335
2336        if (fie->pad != 0)
2337                return -EINVAL;
2338        if (fie->index >= OV5640_NUM_FRAMERATES)
2339                return -EINVAL;
2340
2341        tpf.numerator = 1;
2342        tpf.denominator = ov5640_framerates[fie->index];
2343
2344        ret = ov5640_try_frame_interval(sensor, &tpf,
2345                                        fie->width, fie->height);
2346        if (ret < 0)
2347                return -EINVAL;
2348
2349        fie->interval = tpf;
2350        return 0;
2351}
2352
2353static int ov5640_g_frame_interval(struct v4l2_subdev *sd,
2354                                   struct v4l2_subdev_frame_interval *fi)
2355{
2356        struct ov5640_dev *sensor = to_ov5640_dev(sd);
2357
2358        mutex_lock(&sensor->lock);
2359        fi->interval = sensor->frame_interval;
2360        mutex_unlock(&sensor->lock);
2361
2362        return 0;
2363}
2364
2365static int ov5640_s_frame_interval(struct v4l2_subdev *sd,
2366                                   struct v4l2_subdev_frame_interval *fi)
2367{
2368        struct ov5640_dev *sensor = to_ov5640_dev(sd);
2369        const struct ov5640_mode_info *mode;
2370        int frame_rate, ret = 0;
2371
2372        if (fi->pad != 0)
2373                return -EINVAL;
2374
2375        mutex_lock(&sensor->lock);
2376
2377        if (sensor->streaming) {
2378                ret = -EBUSY;
2379                goto out;
2380        }
2381
2382        mode = sensor->current_mode;
2383
2384        frame_rate = ov5640_try_frame_interval(sensor, &fi->interval,
2385                                               mode->width, mode->height);
2386        if (frame_rate < 0)
2387                frame_rate = OV5640_15_FPS;
2388
2389        sensor->current_fr = frame_rate;
2390        sensor->frame_interval = fi->interval;
2391        sensor->current_mode = ov5640_find_mode(sensor, frame_rate, mode->width,
2392                                                mode->height, true);
2393        sensor->pending_mode_change = true;
2394out:
2395        mutex_unlock(&sensor->lock);
2396        return ret;
2397}
2398
2399static int ov5640_enum_mbus_code(struct v4l2_subdev *sd,
2400                                 struct v4l2_subdev_pad_config *cfg,
2401                                 struct v4l2_subdev_mbus_code_enum *code)
2402{
2403        if (code->pad != 0)
2404                return -EINVAL;
2405        if (code->index >= ARRAY_SIZE(ov5640_formats))
2406                return -EINVAL;
2407
2408        code->code = ov5640_formats[code->index].code;
2409        return 0;
2410}
2411
2412static int ov5640_s_stream(struct v4l2_subdev *sd, int enable)
2413{
2414        struct ov5640_dev *sensor = to_ov5640_dev(sd);
2415        int ret = 0;
2416
2417        mutex_lock(&sensor->lock);
2418
2419        if (sensor->streaming == !enable) {
2420                if (enable && sensor->pending_mode_change) {
2421                        ret = ov5640_set_mode(sensor, sensor->current_mode);
2422                        if (ret)
2423                                goto out;
2424
2425                        ret = ov5640_set_framefmt(sensor, &sensor->fmt);
2426                        if (ret)
2427                                goto out;
2428                }
2429
2430                if (sensor->ep.bus_type == V4L2_MBUS_CSI2)
2431                        ret = ov5640_set_stream_mipi(sensor, enable);
2432                else
2433                        ret = ov5640_set_stream_dvp(sensor, enable);
2434
2435                if (!ret)
2436                        sensor->streaming = enable;
2437        }
2438out:
2439        mutex_unlock(&sensor->lock);
2440        return ret;
2441}
2442
2443static const struct v4l2_subdev_core_ops ov5640_core_ops = {
2444        .s_power = ov5640_s_power,
2445};
2446
2447static const struct v4l2_subdev_video_ops ov5640_video_ops = {
2448        .g_frame_interval = ov5640_g_frame_interval,
2449        .s_frame_interval = ov5640_s_frame_interval,
2450        .s_stream = ov5640_s_stream,
2451};
2452
2453static const struct v4l2_subdev_pad_ops ov5640_pad_ops = {
2454        .enum_mbus_code = ov5640_enum_mbus_code,
2455        .get_fmt = ov5640_get_fmt,
2456        .set_fmt = ov5640_set_fmt,
2457        .enum_frame_size = ov5640_enum_frame_size,
2458        .enum_frame_interval = ov5640_enum_frame_interval,
2459};
2460
2461static const struct v4l2_subdev_ops ov5640_subdev_ops = {
2462        .core = &ov5640_core_ops,
2463        .video = &ov5640_video_ops,
2464        .pad = &ov5640_pad_ops,
2465};
2466
2467static int ov5640_get_regulators(struct ov5640_dev *sensor)
2468{
2469        int i;
2470
2471        for (i = 0; i < OV5640_NUM_SUPPLIES; i++)
2472                sensor->supplies[i].supply = ov5640_supply_name[i];
2473
2474        return devm_regulator_bulk_get(&sensor->i2c_client->dev,
2475                                       OV5640_NUM_SUPPLIES,
2476                                       sensor->supplies);
2477}
2478
2479static int ov5640_check_chip_id(struct ov5640_dev *sensor)
2480{
2481        struct i2c_client *client = sensor->i2c_client;
2482        int ret = 0;
2483        u16 chip_id;
2484
2485        ret = ov5640_set_power_on(sensor);
2486        if (ret)
2487                return ret;
2488
2489        ret = ov5640_read_reg16(sensor, OV5640_REG_CHIP_ID, &chip_id);
2490        if (ret) {
2491                dev_err(&client->dev, "%s: failed to read chip identifier\n",
2492                        __func__);
2493                goto power_off;
2494        }
2495
2496        if (chip_id != 0x5640) {
2497                dev_err(&client->dev, "%s: wrong chip identifier, expected 0x5640, got 0x%x\n",
2498                        __func__, chip_id);
2499                ret = -ENXIO;
2500        }
2501
2502power_off:
2503        ov5640_set_power_off(sensor);
2504        return ret;
2505}
2506
2507static int ov5640_probe(struct i2c_client *client,
2508                        const struct i2c_device_id *id)
2509{
2510        struct device *dev = &client->dev;
2511        struct fwnode_handle *endpoint;
2512        struct ov5640_dev *sensor;
2513        struct v4l2_mbus_framefmt *fmt;
2514        int ret;
2515
2516        sensor = devm_kzalloc(dev, sizeof(*sensor), GFP_KERNEL);
2517        if (!sensor)
2518                return -ENOMEM;
2519
2520        sensor->i2c_client = client;
2521        fmt = &sensor->fmt;
2522        fmt->code = ov5640_formats[0].code;
2523        fmt->colorspace = ov5640_formats[0].colorspace;
2524        fmt->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(fmt->colorspace);
2525        fmt->quantization = V4L2_QUANTIZATION_FULL_RANGE;
2526        fmt->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(fmt->colorspace);
2527        fmt->width = 640;
2528        fmt->height = 480;
2529        fmt->field = V4L2_FIELD_NONE;
2530        sensor->frame_interval.numerator = 1;
2531        sensor->frame_interval.denominator = ov5640_framerates[OV5640_30_FPS];
2532        sensor->current_fr = OV5640_30_FPS;
2533        sensor->current_mode =
2534                &ov5640_mode_data[OV5640_30_FPS][OV5640_MODE_VGA_640_480];
2535        sensor->pending_mode_change = true;
2536
2537        sensor->ae_target = 52;
2538
2539        endpoint = fwnode_graph_get_next_endpoint(
2540                of_fwnode_handle(client->dev.of_node), NULL);
2541        if (!endpoint) {
2542                dev_err(dev, "endpoint node not found\n");
2543                return -EINVAL;
2544        }
2545
2546        ret = v4l2_fwnode_endpoint_parse(endpoint, &sensor->ep);
2547        fwnode_handle_put(endpoint);
2548        if (ret) {
2549                dev_err(dev, "Could not parse endpoint\n");
2550                return ret;
2551        }
2552
2553        /* get system clock (xclk) */
2554        sensor->xclk = devm_clk_get(dev, "xclk");
2555        if (IS_ERR(sensor->xclk)) {
2556                dev_err(dev, "failed to get xclk\n");
2557                return PTR_ERR(sensor->xclk);
2558        }
2559
2560        sensor->xclk_freq = clk_get_rate(sensor->xclk);
2561        if (sensor->xclk_freq < OV5640_XCLK_MIN ||
2562            sensor->xclk_freq > OV5640_XCLK_MAX) {
2563                dev_err(dev, "xclk frequency out of range: %d Hz\n",
2564                        sensor->xclk_freq);
2565                return -EINVAL;
2566        }
2567
2568        /* request optional power down pin */
2569        sensor->pwdn_gpio = devm_gpiod_get_optional(dev, "powerdown",
2570                                                    GPIOD_OUT_HIGH);
2571        /* request optional reset pin */
2572        sensor->reset_gpio = devm_gpiod_get_optional(dev, "reset",
2573                                                     GPIOD_OUT_HIGH);
2574
2575        v4l2_i2c_subdev_init(&sensor->sd, client, &ov5640_subdev_ops);
2576
2577        sensor->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
2578        sensor->pad.flags = MEDIA_PAD_FL_SOURCE;
2579        sensor->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
2580        ret = media_entity_pads_init(&sensor->sd.entity, 1, &sensor->pad);
2581        if (ret)
2582                return ret;
2583
2584        ret = ov5640_get_regulators(sensor);
2585        if (ret)
2586                return ret;
2587
2588        mutex_init(&sensor->lock);
2589
2590        ret = ov5640_check_chip_id(sensor);
2591        if (ret)
2592                goto entity_cleanup;
2593
2594        ret = ov5640_init_controls(sensor);
2595        if (ret)
2596                goto entity_cleanup;
2597
2598        ret = v4l2_async_register_subdev(&sensor->sd);
2599        if (ret)
2600                goto free_ctrls;
2601
2602        return 0;
2603
2604free_ctrls:
2605        v4l2_ctrl_handler_free(&sensor->ctrls.handler);
2606entity_cleanup:
2607        mutex_destroy(&sensor->lock);
2608        media_entity_cleanup(&sensor->sd.entity);
2609        return ret;
2610}
2611
2612static int ov5640_remove(struct i2c_client *client)
2613{
2614        struct v4l2_subdev *sd = i2c_get_clientdata(client);
2615        struct ov5640_dev *sensor = to_ov5640_dev(sd);
2616
2617        v4l2_async_unregister_subdev(&sensor->sd);
2618        mutex_destroy(&sensor->lock);
2619        media_entity_cleanup(&sensor->sd.entity);
2620        v4l2_ctrl_handler_free(&sensor->ctrls.handler);
2621
2622        return 0;
2623}
2624
2625static const struct i2c_device_id ov5640_id[] = {
2626        {"ov5640", 0},
2627        {},
2628};
2629MODULE_DEVICE_TABLE(i2c, ov5640_id);
2630
2631static const struct of_device_id ov5640_dt_ids[] = {
2632        { .compatible = "ovti,ov5640" },
2633        { /* sentinel */ }
2634};
2635MODULE_DEVICE_TABLE(of, ov5640_dt_ids);
2636
2637static struct i2c_driver ov5640_i2c_driver = {
2638        .driver = {
2639                .name  = "ov5640",
2640                .of_match_table = ov5640_dt_ids,
2641        },
2642        .id_table = ov5640_id,
2643        .probe    = ov5640_probe,
2644        .remove   = ov5640_remove,
2645};
2646
2647module_i2c_driver(ov5640_i2c_driver);
2648
2649MODULE_DESCRIPTION("OV5640 MIPI Camera Subdev Driver");
2650MODULE_LICENSE("GPL");
2651