linux/drivers/media/i2c/ov5670.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0
   2// Copyright (c) 2017 Intel Corporation.
   3
   4#include <linux/acpi.h>
   5#include <linux/i2c.h>
   6#include <linux/module.h>
   7#include <linux/pm_runtime.h>
   8#include <media/v4l2-ctrls.h>
   9#include <media/v4l2-device.h>
  10#include <media/v4l2-fwnode.h>
  11
  12#define OV5670_REG_CHIP_ID              0x300a
  13#define OV5670_CHIP_ID                  0x005670
  14
  15#define OV5670_REG_MODE_SELECT          0x0100
  16#define OV5670_MODE_STANDBY             0x00
  17#define OV5670_MODE_STREAMING           0x01
  18
  19#define OV5670_REG_SOFTWARE_RST         0x0103
  20#define OV5670_SOFTWARE_RST             0x01
  21
  22/* vertical-timings from sensor */
  23#define OV5670_REG_VTS                  0x380e
  24#define OV5670_VTS_30FPS                0x0808 /* default for 30 fps */
  25#define OV5670_VTS_MAX                  0xffff
  26
  27/* horizontal-timings from sensor */
  28#define OV5670_REG_HTS                  0x380c
  29
  30/*
  31 * Pixels-per-line(PPL) = Time-per-line * pixel-rate
  32 * In OV5670, Time-per-line = HTS/SCLK.
  33 * HTS is fixed for all resolutions, not recommended to change.
  34 */
  35#define OV5670_FIXED_PPL                2724    /* Pixels per line */
  36
  37/* Exposure controls from sensor */
  38#define OV5670_REG_EXPOSURE             0x3500
  39#define OV5670_EXPOSURE_MIN             4
  40#define OV5670_EXPOSURE_STEP            1
  41
  42/* Analog gain controls from sensor */
  43#define OV5670_REG_ANALOG_GAIN          0x3508
  44#define ANALOG_GAIN_MIN                 0
  45#define ANALOG_GAIN_MAX                 8191
  46#define ANALOG_GAIN_STEP                1
  47#define ANALOG_GAIN_DEFAULT             128
  48
  49/* Digital gain controls from sensor */
  50#define OV5670_REG_R_DGTL_GAIN          0x5032
  51#define OV5670_REG_G_DGTL_GAIN          0x5034
  52#define OV5670_REG_B_DGTL_GAIN          0x5036
  53#define OV5670_DGTL_GAIN_MIN            0
  54#define OV5670_DGTL_GAIN_MAX            4095
  55#define OV5670_DGTL_GAIN_STEP           1
  56#define OV5670_DGTL_GAIN_DEFAULT        1024
  57
  58/* Test Pattern Control */
  59#define OV5670_REG_TEST_PATTERN         0x4303
  60#define OV5670_TEST_PATTERN_ENABLE      BIT(3)
  61#define OV5670_REG_TEST_PATTERN_CTRL    0x4320
  62
  63#define OV5670_REG_VALUE_08BIT          1
  64#define OV5670_REG_VALUE_16BIT          2
  65#define OV5670_REG_VALUE_24BIT          3
  66
  67/* Initial number of frames to skip to avoid possible garbage */
  68#define OV5670_NUM_OF_SKIP_FRAMES       2
  69
  70struct ov5670_reg {
  71        u16 address;
  72        u8 val;
  73};
  74
  75struct ov5670_reg_list {
  76        u32 num_of_regs;
  77        const struct ov5670_reg *regs;
  78};
  79
  80struct ov5670_link_freq_config {
  81        u32 pixel_rate;
  82        const struct ov5670_reg_list reg_list;
  83};
  84
  85struct ov5670_mode {
  86        /* Frame width in pixels */
  87        u32 width;
  88
  89        /* Frame height in pixels */
  90        u32 height;
  91
  92        /* Default vertical timining size */
  93        u32 vts_def;
  94
  95        /* Min vertical timining size */
  96        u32 vts_min;
  97
  98        /* Link frequency needed for this resolution */
  99        u32 link_freq_index;
 100
 101        /* Sensor register settings for this resolution */
 102        const struct ov5670_reg_list reg_list;
 103};
 104
 105static const struct ov5670_reg mipi_data_rate_840mbps[] = {
 106        {0x0300, 0x04},
 107        {0x0301, 0x00},
 108        {0x0302, 0x84},
 109        {0x0303, 0x00},
 110        {0x0304, 0x03},
 111        {0x0305, 0x01},
 112        {0x0306, 0x01},
 113        {0x030a, 0x00},
 114        {0x030b, 0x00},
 115        {0x030c, 0x00},
 116        {0x030d, 0x26},
 117        {0x030e, 0x00},
 118        {0x030f, 0x06},
 119        {0x0312, 0x01},
 120        {0x3031, 0x0a},
 121};
 122
 123static const struct ov5670_reg mode_2592x1944_regs[] = {
 124        {0x3000, 0x00},
 125        {0x3002, 0x21},
 126        {0x3005, 0xf0},
 127        {0x3007, 0x00},
 128        {0x3015, 0x0f},
 129        {0x3018, 0x32},
 130        {0x301a, 0xf0},
 131        {0x301b, 0xf0},
 132        {0x301c, 0xf0},
 133        {0x301d, 0xf0},
 134        {0x301e, 0xf0},
 135        {0x3030, 0x00},
 136        {0x3031, 0x0a},
 137        {0x303c, 0xff},
 138        {0x303e, 0xff},
 139        {0x3040, 0xf0},
 140        {0x3041, 0x00},
 141        {0x3042, 0xf0},
 142        {0x3106, 0x11},
 143        {0x3500, 0x00},
 144        {0x3501, 0x80},
 145        {0x3502, 0x00},
 146        {0x3503, 0x04},
 147        {0x3504, 0x03},
 148        {0x3505, 0x83},
 149        {0x3508, 0x04},
 150        {0x3509, 0x00},
 151        {0x350e, 0x04},
 152        {0x350f, 0x00},
 153        {0x3510, 0x00},
 154        {0x3511, 0x02},
 155        {0x3512, 0x00},
 156        {0x3601, 0xc8},
 157        {0x3610, 0x88},
 158        {0x3612, 0x48},
 159        {0x3614, 0x5b},
 160        {0x3615, 0x96},
 161        {0x3621, 0xd0},
 162        {0x3622, 0x00},
 163        {0x3623, 0x00},
 164        {0x3633, 0x13},
 165        {0x3634, 0x13},
 166        {0x3635, 0x13},
 167        {0x3636, 0x13},
 168        {0x3645, 0x13},
 169        {0x3646, 0x82},
 170        {0x3650, 0x00},
 171        {0x3652, 0xff},
 172        {0x3655, 0x20},
 173        {0x3656, 0xff},
 174        {0x365a, 0xff},
 175        {0x365e, 0xff},
 176        {0x3668, 0x00},
 177        {0x366a, 0x07},
 178        {0x366e, 0x10},
 179        {0x366d, 0x00},
 180        {0x366f, 0x80},
 181        {0x3700, 0x28},
 182        {0x3701, 0x10},
 183        {0x3702, 0x3a},
 184        {0x3703, 0x19},
 185        {0x3704, 0x10},
 186        {0x3705, 0x00},
 187        {0x3706, 0x66},
 188        {0x3707, 0x08},
 189        {0x3708, 0x34},
 190        {0x3709, 0x40},
 191        {0x370a, 0x01},
 192        {0x370b, 0x1b},
 193        {0x3714, 0x24},
 194        {0x371a, 0x3e},
 195        {0x3733, 0x00},
 196        {0x3734, 0x00},
 197        {0x373a, 0x05},
 198        {0x373b, 0x06},
 199        {0x373c, 0x0a},
 200        {0x373f, 0xa0},
 201        {0x3755, 0x00},
 202        {0x3758, 0x00},
 203        {0x375b, 0x0e},
 204        {0x3766, 0x5f},
 205        {0x3768, 0x00},
 206        {0x3769, 0x22},
 207        {0x3773, 0x08},
 208        {0x3774, 0x1f},
 209        {0x3776, 0x06},
 210        {0x37a0, 0x88},
 211        {0x37a1, 0x5c},
 212        {0x37a7, 0x88},
 213        {0x37a8, 0x70},
 214        {0x37aa, 0x88},
 215        {0x37ab, 0x48},
 216        {0x37b3, 0x66},
 217        {0x37c2, 0x04},
 218        {0x37c5, 0x00},
 219        {0x37c8, 0x00},
 220        {0x3800, 0x00},
 221        {0x3801, 0x0c},
 222        {0x3802, 0x00},
 223        {0x3803, 0x04},
 224        {0x3804, 0x0a},
 225        {0x3805, 0x33},
 226        {0x3806, 0x07},
 227        {0x3807, 0xa3},
 228        {0x3808, 0x0a},
 229        {0x3809, 0x20},
 230        {0x380a, 0x07},
 231        {0x380b, 0x98},
 232        {0x380c, 0x06},
 233        {0x380d, 0x90},
 234        {0x380e, 0x08},
 235        {0x380f, 0x08},
 236        {0x3811, 0x04},
 237        {0x3813, 0x02},
 238        {0x3814, 0x01},
 239        {0x3815, 0x01},
 240        {0x3816, 0x00},
 241        {0x3817, 0x00},
 242        {0x3818, 0x00},
 243        {0x3819, 0x00},
 244        {0x3820, 0x84},
 245        {0x3821, 0x46},
 246        {0x3822, 0x48},
 247        {0x3826, 0x00},
 248        {0x3827, 0x08},
 249        {0x382a, 0x01},
 250        {0x382b, 0x01},
 251        {0x3830, 0x08},
 252        {0x3836, 0x02},
 253        {0x3837, 0x00},
 254        {0x3838, 0x10},
 255        {0x3841, 0xff},
 256        {0x3846, 0x48},
 257        {0x3861, 0x00},
 258        {0x3862, 0x04},
 259        {0x3863, 0x06},
 260        {0x3a11, 0x01},
 261        {0x3a12, 0x78},
 262        {0x3b00, 0x00},
 263        {0x3b02, 0x00},
 264        {0x3b03, 0x00},
 265        {0x3b04, 0x00},
 266        {0x3b05, 0x00},
 267        {0x3c00, 0x89},
 268        {0x3c01, 0xab},
 269        {0x3c02, 0x01},
 270        {0x3c03, 0x00},
 271        {0x3c04, 0x00},
 272        {0x3c05, 0x03},
 273        {0x3c06, 0x00},
 274        {0x3c07, 0x05},
 275        {0x3c0c, 0x00},
 276        {0x3c0d, 0x00},
 277        {0x3c0e, 0x00},
 278        {0x3c0f, 0x00},
 279        {0x3c40, 0x00},
 280        {0x3c41, 0xa3},
 281        {0x3c43, 0x7d},
 282        {0x3c45, 0xd7},
 283        {0x3c47, 0xfc},
 284        {0x3c50, 0x05},
 285        {0x3c52, 0xaa},
 286        {0x3c54, 0x71},
 287        {0x3c56, 0x80},
 288        {0x3d85, 0x17},
 289        {0x3f03, 0x00},
 290        {0x3f0a, 0x00},
 291        {0x3f0b, 0x00},
 292        {0x4001, 0x60},
 293        {0x4009, 0x0d},
 294        {0x4020, 0x00},
 295        {0x4021, 0x00},
 296        {0x4022, 0x00},
 297        {0x4023, 0x00},
 298        {0x4024, 0x00},
 299        {0x4025, 0x00},
 300        {0x4026, 0x00},
 301        {0x4027, 0x00},
 302        {0x4028, 0x00},
 303        {0x4029, 0x00},
 304        {0x402a, 0x00},
 305        {0x402b, 0x00},
 306        {0x402c, 0x00},
 307        {0x402d, 0x00},
 308        {0x402e, 0x00},
 309        {0x402f, 0x00},
 310        {0x4040, 0x00},
 311        {0x4041, 0x03},
 312        {0x4042, 0x00},
 313        {0x4043, 0x7A},
 314        {0x4044, 0x00},
 315        {0x4045, 0x7A},
 316        {0x4046, 0x00},
 317        {0x4047, 0x7A},
 318        {0x4048, 0x00},
 319        {0x4049, 0x7A},
 320        {0x4307, 0x30},
 321        {0x4500, 0x58},
 322        {0x4501, 0x04},
 323        {0x4502, 0x40},
 324        {0x4503, 0x10},
 325        {0x4508, 0xaa},
 326        {0x4509, 0xaa},
 327        {0x450a, 0x00},
 328        {0x450b, 0x00},
 329        {0x4600, 0x01},
 330        {0x4601, 0x03},
 331        {0x4700, 0xa4},
 332        {0x4800, 0x4c},
 333        {0x4816, 0x53},
 334        {0x481f, 0x40},
 335        {0x4837, 0x13},
 336        {0x5000, 0x56},
 337        {0x5001, 0x01},
 338        {0x5002, 0x28},
 339        {0x5004, 0x0c},
 340        {0x5006, 0x0c},
 341        {0x5007, 0xe0},
 342        {0x5008, 0x01},
 343        {0x5009, 0xb0},
 344        {0x5901, 0x00},
 345        {0x5a01, 0x00},
 346        {0x5a03, 0x00},
 347        {0x5a04, 0x0c},
 348        {0x5a05, 0xe0},
 349        {0x5a06, 0x09},
 350        {0x5a07, 0xb0},
 351        {0x5a08, 0x06},
 352        {0x5e00, 0x00},
 353        {0x3734, 0x40},
 354        {0x5b00, 0x01},
 355        {0x5b01, 0x10},
 356        {0x5b02, 0x01},
 357        {0x5b03, 0xdb},
 358        {0x3d8c, 0x71},
 359        {0x3d8d, 0xea},
 360        {0x4017, 0x08},
 361        {0x3618, 0x2a},
 362        {0x5780, 0x3e},
 363        {0x5781, 0x0f},
 364        {0x5782, 0x44},
 365        {0x5783, 0x02},
 366        {0x5784, 0x01},
 367        {0x5785, 0x01},
 368        {0x5786, 0x00},
 369        {0x5787, 0x04},
 370        {0x5788, 0x02},
 371        {0x5789, 0x0f},
 372        {0x578a, 0xfd},
 373        {0x578b, 0xf5},
 374        {0x578c, 0xf5},
 375        {0x578d, 0x03},
 376        {0x578e, 0x08},
 377        {0x578f, 0x0c},
 378        {0x5790, 0x08},
 379        {0x5791, 0x06},
 380        {0x5792, 0x00},
 381        {0x5793, 0x52},
 382        {0x5794, 0xa3},
 383        {0x3503, 0x00},
 384        {0x5045, 0x05},
 385        {0x4003, 0x40},
 386        {0x5048, 0x40}
 387};
 388
 389static const struct ov5670_reg mode_1296x972_regs[] = {
 390        {0x3000, 0x00},
 391        {0x3002, 0x21},
 392        {0x3005, 0xf0},
 393        {0x3007, 0x00},
 394        {0x3015, 0x0f},
 395        {0x3018, 0x32},
 396        {0x301a, 0xf0},
 397        {0x301b, 0xf0},
 398        {0x301c, 0xf0},
 399        {0x301d, 0xf0},
 400        {0x301e, 0xf0},
 401        {0x3030, 0x00},
 402        {0x3031, 0x0a},
 403        {0x303c, 0xff},
 404        {0x303e, 0xff},
 405        {0x3040, 0xf0},
 406        {0x3041, 0x00},
 407        {0x3042, 0xf0},
 408        {0x3106, 0x11},
 409        {0x3500, 0x00},
 410        {0x3501, 0x80},
 411        {0x3502, 0x00},
 412        {0x3503, 0x04},
 413        {0x3504, 0x03},
 414        {0x3505, 0x83},
 415        {0x3508, 0x07},
 416        {0x3509, 0x80},
 417        {0x350e, 0x04},
 418        {0x350f, 0x00},
 419        {0x3510, 0x00},
 420        {0x3511, 0x02},
 421        {0x3512, 0x00},
 422        {0x3601, 0xc8},
 423        {0x3610, 0x88},
 424        {0x3612, 0x48},
 425        {0x3614, 0x5b},
 426        {0x3615, 0x96},
 427        {0x3621, 0xd0},
 428        {0x3622, 0x00},
 429        {0x3623, 0x00},
 430        {0x3633, 0x13},
 431        {0x3634, 0x13},
 432        {0x3635, 0x13},
 433        {0x3636, 0x13},
 434        {0x3645, 0x13},
 435        {0x3646, 0x82},
 436        {0x3650, 0x00},
 437        {0x3652, 0xff},
 438        {0x3655, 0x20},
 439        {0x3656, 0xff},
 440        {0x365a, 0xff},
 441        {0x365e, 0xff},
 442        {0x3668, 0x00},
 443        {0x366a, 0x07},
 444        {0x366e, 0x08},
 445        {0x366d, 0x00},
 446        {0x366f, 0x80},
 447        {0x3700, 0x28},
 448        {0x3701, 0x10},
 449        {0x3702, 0x3a},
 450        {0x3703, 0x19},
 451        {0x3704, 0x10},
 452        {0x3705, 0x00},
 453        {0x3706, 0x66},
 454        {0x3707, 0x08},
 455        {0x3708, 0x34},
 456        {0x3709, 0x40},
 457        {0x370a, 0x01},
 458        {0x370b, 0x1b},
 459        {0x3714, 0x24},
 460        {0x371a, 0x3e},
 461        {0x3733, 0x00},
 462        {0x3734, 0x00},
 463        {0x373a, 0x05},
 464        {0x373b, 0x06},
 465        {0x373c, 0x0a},
 466        {0x373f, 0xa0},
 467        {0x3755, 0x00},
 468        {0x3758, 0x00},
 469        {0x375b, 0x0e},
 470        {0x3766, 0x5f},
 471        {0x3768, 0x00},
 472        {0x3769, 0x22},
 473        {0x3773, 0x08},
 474        {0x3774, 0x1f},
 475        {0x3776, 0x06},
 476        {0x37a0, 0x88},
 477        {0x37a1, 0x5c},
 478        {0x37a7, 0x88},
 479        {0x37a8, 0x70},
 480        {0x37aa, 0x88},
 481        {0x37ab, 0x48},
 482        {0x37b3, 0x66},
 483        {0x37c2, 0x04},
 484        {0x37c5, 0x00},
 485        {0x37c8, 0x00},
 486        {0x3800, 0x00},
 487        {0x3801, 0x0c},
 488        {0x3802, 0x00},
 489        {0x3803, 0x04},
 490        {0x3804, 0x0a},
 491        {0x3805, 0x33},
 492        {0x3806, 0x07},
 493        {0x3807, 0xa3},
 494        {0x3808, 0x05},
 495        {0x3809, 0x10},
 496        {0x380a, 0x03},
 497        {0x380b, 0xcc},
 498        {0x380c, 0x06},
 499        {0x380d, 0x90},
 500        {0x380e, 0x08},
 501        {0x380f, 0x08},
 502        {0x3811, 0x04},
 503        {0x3813, 0x04},
 504        {0x3814, 0x03},
 505        {0x3815, 0x01},
 506        {0x3816, 0x00},
 507        {0x3817, 0x00},
 508        {0x3818, 0x00},
 509        {0x3819, 0x00},
 510        {0x3820, 0x94},
 511        {0x3821, 0x47},
 512        {0x3822, 0x48},
 513        {0x3826, 0x00},
 514        {0x3827, 0x08},
 515        {0x382a, 0x03},
 516        {0x382b, 0x01},
 517        {0x3830, 0x08},
 518        {0x3836, 0x02},
 519        {0x3837, 0x00},
 520        {0x3838, 0x10},
 521        {0x3841, 0xff},
 522        {0x3846, 0x48},
 523        {0x3861, 0x00},
 524        {0x3862, 0x04},
 525        {0x3863, 0x06},
 526        {0x3a11, 0x01},
 527        {0x3a12, 0x78},
 528        {0x3b00, 0x00},
 529        {0x3b02, 0x00},
 530        {0x3b03, 0x00},
 531        {0x3b04, 0x00},
 532        {0x3b05, 0x00},
 533        {0x3c00, 0x89},
 534        {0x3c01, 0xab},
 535        {0x3c02, 0x01},
 536        {0x3c03, 0x00},
 537        {0x3c04, 0x00},
 538        {0x3c05, 0x03},
 539        {0x3c06, 0x00},
 540        {0x3c07, 0x05},
 541        {0x3c0c, 0x00},
 542        {0x3c0d, 0x00},
 543        {0x3c0e, 0x00},
 544        {0x3c0f, 0x00},
 545        {0x3c40, 0x00},
 546        {0x3c41, 0xa3},
 547        {0x3c43, 0x7d},
 548        {0x3c45, 0xd7},
 549        {0x3c47, 0xfc},
 550        {0x3c50, 0x05},
 551        {0x3c52, 0xaa},
 552        {0x3c54, 0x71},
 553        {0x3c56, 0x80},
 554        {0x3d85, 0x17},
 555        {0x3f03, 0x00},
 556        {0x3f0a, 0x00},
 557        {0x3f0b, 0x00},
 558        {0x4001, 0x60},
 559        {0x4009, 0x05},
 560        {0x4020, 0x00},
 561        {0x4021, 0x00},
 562        {0x4022, 0x00},
 563        {0x4023, 0x00},
 564        {0x4024, 0x00},
 565        {0x4025, 0x00},
 566        {0x4026, 0x00},
 567        {0x4027, 0x00},
 568        {0x4028, 0x00},
 569        {0x4029, 0x00},
 570        {0x402a, 0x00},
 571        {0x402b, 0x00},
 572        {0x402c, 0x00},
 573        {0x402d, 0x00},
 574        {0x402e, 0x00},
 575        {0x402f, 0x00},
 576        {0x4040, 0x00},
 577        {0x4041, 0x03},
 578        {0x4042, 0x00},
 579        {0x4043, 0x7A},
 580        {0x4044, 0x00},
 581        {0x4045, 0x7A},
 582        {0x4046, 0x00},
 583        {0x4047, 0x7A},
 584        {0x4048, 0x00},
 585        {0x4049, 0x7A},
 586        {0x4307, 0x30},
 587        {0x4500, 0x58},
 588        {0x4501, 0x04},
 589        {0x4502, 0x48},
 590        {0x4503, 0x10},
 591        {0x4508, 0x55},
 592        {0x4509, 0x55},
 593        {0x450a, 0x00},
 594        {0x450b, 0x00},
 595        {0x4600, 0x00},
 596        {0x4601, 0x81},
 597        {0x4700, 0xa4},
 598        {0x4800, 0x4c},
 599        {0x4816, 0x53},
 600        {0x481f, 0x40},
 601        {0x4837, 0x13},
 602        {0x5000, 0x56},
 603        {0x5001, 0x01},
 604        {0x5002, 0x28},
 605        {0x5004, 0x0c},
 606        {0x5006, 0x0c},
 607        {0x5007, 0xe0},
 608        {0x5008, 0x01},
 609        {0x5009, 0xb0},
 610        {0x5901, 0x00},
 611        {0x5a01, 0x00},
 612        {0x5a03, 0x00},
 613        {0x5a04, 0x0c},
 614        {0x5a05, 0xe0},
 615        {0x5a06, 0x09},
 616        {0x5a07, 0xb0},
 617        {0x5a08, 0x06},
 618        {0x5e00, 0x00},
 619        {0x3734, 0x40},
 620        {0x5b00, 0x01},
 621        {0x5b01, 0x10},
 622        {0x5b02, 0x01},
 623        {0x5b03, 0xdb},
 624        {0x3d8c, 0x71},
 625        {0x3d8d, 0xea},
 626        {0x4017, 0x10},
 627        {0x3618, 0x2a},
 628        {0x5780, 0x3e},
 629        {0x5781, 0x0f},
 630        {0x5782, 0x44},
 631        {0x5783, 0x02},
 632        {0x5784, 0x01},
 633        {0x5785, 0x01},
 634        {0x5786, 0x00},
 635        {0x5787, 0x04},
 636        {0x5788, 0x02},
 637        {0x5789, 0x0f},
 638        {0x578a, 0xfd},
 639        {0x578b, 0xf5},
 640        {0x578c, 0xf5},
 641        {0x578d, 0x03},
 642        {0x578e, 0x08},
 643        {0x578f, 0x0c},
 644        {0x5790, 0x08},
 645        {0x5791, 0x04},
 646        {0x5792, 0x00},
 647        {0x5793, 0x52},
 648        {0x5794, 0xa3},
 649        {0x3503, 0x00},
 650        {0x5045, 0x05},
 651        {0x4003, 0x40},
 652        {0x5048, 0x40}
 653};
 654
 655static const struct ov5670_reg mode_648x486_regs[] = {
 656        {0x3000, 0x00},
 657        {0x3002, 0x21},
 658        {0x3005, 0xf0},
 659        {0x3007, 0x00},
 660        {0x3015, 0x0f},
 661        {0x3018, 0x32},
 662        {0x301a, 0xf0},
 663        {0x301b, 0xf0},
 664        {0x301c, 0xf0},
 665        {0x301d, 0xf0},
 666        {0x301e, 0xf0},
 667        {0x3030, 0x00},
 668        {0x3031, 0x0a},
 669        {0x303c, 0xff},
 670        {0x303e, 0xff},
 671        {0x3040, 0xf0},
 672        {0x3041, 0x00},
 673        {0x3042, 0xf0},
 674        {0x3106, 0x11},
 675        {0x3500, 0x00},
 676        {0x3501, 0x80},
 677        {0x3502, 0x00},
 678        {0x3503, 0x04},
 679        {0x3504, 0x03},
 680        {0x3505, 0x83},
 681        {0x3508, 0x04},
 682        {0x3509, 0x00},
 683        {0x350e, 0x04},
 684        {0x350f, 0x00},
 685        {0x3510, 0x00},
 686        {0x3511, 0x02},
 687        {0x3512, 0x00},
 688        {0x3601, 0xc8},
 689        {0x3610, 0x88},
 690        {0x3612, 0x48},
 691        {0x3614, 0x5b},
 692        {0x3615, 0x96},
 693        {0x3621, 0xd0},
 694        {0x3622, 0x00},
 695        {0x3623, 0x04},
 696        {0x3633, 0x13},
 697        {0x3634, 0x13},
 698        {0x3635, 0x13},
 699        {0x3636, 0x13},
 700        {0x3645, 0x13},
 701        {0x3646, 0x82},
 702        {0x3650, 0x00},
 703        {0x3652, 0xff},
 704        {0x3655, 0x20},
 705        {0x3656, 0xff},
 706        {0x365a, 0xff},
 707        {0x365e, 0xff},
 708        {0x3668, 0x00},
 709        {0x366a, 0x07},
 710        {0x366e, 0x08},
 711        {0x366d, 0x00},
 712        {0x366f, 0x80},
 713        {0x3700, 0x28},
 714        {0x3701, 0x10},
 715        {0x3702, 0x3a},
 716        {0x3703, 0x19},
 717        {0x3704, 0x10},
 718        {0x3705, 0x00},
 719        {0x3706, 0x66},
 720        {0x3707, 0x08},
 721        {0x3708, 0x34},
 722        {0x3709, 0x40},
 723        {0x370a, 0x01},
 724        {0x370b, 0x1b},
 725        {0x3714, 0x24},
 726        {0x371a, 0x3e},
 727        {0x3733, 0x00},
 728        {0x3734, 0x00},
 729        {0x373a, 0x05},
 730        {0x373b, 0x06},
 731        {0x373c, 0x0a},
 732        {0x373f, 0xa0},
 733        {0x3755, 0x00},
 734        {0x3758, 0x00},
 735        {0x375b, 0x0e},
 736        {0x3766, 0x5f},
 737        {0x3768, 0x00},
 738        {0x3769, 0x22},
 739        {0x3773, 0x08},
 740        {0x3774, 0x1f},
 741        {0x3776, 0x06},
 742        {0x37a0, 0x88},
 743        {0x37a1, 0x5c},
 744        {0x37a7, 0x88},
 745        {0x37a8, 0x70},
 746        {0x37aa, 0x88},
 747        {0x37ab, 0x48},
 748        {0x37b3, 0x66},
 749        {0x37c2, 0x04},
 750        {0x37c5, 0x00},
 751        {0x37c8, 0x00},
 752        {0x3800, 0x00},
 753        {0x3801, 0x0c},
 754        {0x3802, 0x00},
 755        {0x3803, 0x04},
 756        {0x3804, 0x0a},
 757        {0x3805, 0x33},
 758        {0x3806, 0x07},
 759        {0x3807, 0xa3},
 760        {0x3808, 0x02},
 761        {0x3809, 0x88},
 762        {0x380a, 0x01},
 763        {0x380b, 0xe6},
 764        {0x380c, 0x06},
 765        {0x380d, 0x90},
 766        {0x380e, 0x08},
 767        {0x380f, 0x08},
 768        {0x3811, 0x04},
 769        {0x3813, 0x02},
 770        {0x3814, 0x07},
 771        {0x3815, 0x01},
 772        {0x3816, 0x00},
 773        {0x3817, 0x00},
 774        {0x3818, 0x00},
 775        {0x3819, 0x00},
 776        {0x3820, 0x94},
 777        {0x3821, 0xc6},
 778        {0x3822, 0x48},
 779        {0x3826, 0x00},
 780        {0x3827, 0x08},
 781        {0x382a, 0x07},
 782        {0x382b, 0x01},
 783        {0x3830, 0x08},
 784        {0x3836, 0x02},
 785        {0x3837, 0x00},
 786        {0x3838, 0x10},
 787        {0x3841, 0xff},
 788        {0x3846, 0x48},
 789        {0x3861, 0x00},
 790        {0x3862, 0x04},
 791        {0x3863, 0x06},
 792        {0x3a11, 0x01},
 793        {0x3a12, 0x78},
 794        {0x3b00, 0x00},
 795        {0x3b02, 0x00},
 796        {0x3b03, 0x00},
 797        {0x3b04, 0x00},
 798        {0x3b05, 0x00},
 799        {0x3c00, 0x89},
 800        {0x3c01, 0xab},
 801        {0x3c02, 0x01},
 802        {0x3c03, 0x00},
 803        {0x3c04, 0x00},
 804        {0x3c05, 0x03},
 805        {0x3c06, 0x00},
 806        {0x3c07, 0x05},
 807        {0x3c0c, 0x00},
 808        {0x3c0d, 0x00},
 809        {0x3c0e, 0x00},
 810        {0x3c0f, 0x00},
 811        {0x3c40, 0x00},
 812        {0x3c41, 0xa3},
 813        {0x3c43, 0x7d},
 814        {0x3c45, 0xd7},
 815        {0x3c47, 0xfc},
 816        {0x3c50, 0x05},
 817        {0x3c52, 0xaa},
 818        {0x3c54, 0x71},
 819        {0x3c56, 0x80},
 820        {0x3d85, 0x17},
 821        {0x3f03, 0x00},
 822        {0x3f0a, 0x00},
 823        {0x3f0b, 0x00},
 824        {0x4001, 0x60},
 825        {0x4009, 0x05},
 826        {0x4020, 0x00},
 827        {0x4021, 0x00},
 828        {0x4022, 0x00},
 829        {0x4023, 0x00},
 830        {0x4024, 0x00},
 831        {0x4025, 0x00},
 832        {0x4026, 0x00},
 833        {0x4027, 0x00},
 834        {0x4028, 0x00},
 835        {0x4029, 0x00},
 836        {0x402a, 0x00},
 837        {0x402b, 0x00},
 838        {0x402c, 0x00},
 839        {0x402d, 0x00},
 840        {0x402e, 0x00},
 841        {0x402f, 0x00},
 842        {0x4040, 0x00},
 843        {0x4041, 0x03},
 844        {0x4042, 0x00},
 845        {0x4043, 0x7A},
 846        {0x4044, 0x00},
 847        {0x4045, 0x7A},
 848        {0x4046, 0x00},
 849        {0x4047, 0x7A},
 850        {0x4048, 0x00},
 851        {0x4049, 0x7A},
 852        {0x4307, 0x30},
 853        {0x4500, 0x58},
 854        {0x4501, 0x04},
 855        {0x4502, 0x40},
 856        {0x4503, 0x10},
 857        {0x4508, 0x55},
 858        {0x4509, 0x55},
 859        {0x450a, 0x02},
 860        {0x450b, 0x00},
 861        {0x4600, 0x00},
 862        {0x4601, 0x40},
 863        {0x4700, 0xa4},
 864        {0x4800, 0x4c},
 865        {0x4816, 0x53},
 866        {0x481f, 0x40},
 867        {0x4837, 0x13},
 868        {0x5000, 0x56},
 869        {0x5001, 0x01},
 870        {0x5002, 0x28},
 871        {0x5004, 0x0c},
 872        {0x5006, 0x0c},
 873        {0x5007, 0xe0},
 874        {0x5008, 0x01},
 875        {0x5009, 0xb0},
 876        {0x5901, 0x00},
 877        {0x5a01, 0x00},
 878        {0x5a03, 0x00},
 879        {0x5a04, 0x0c},
 880        {0x5a05, 0xe0},
 881        {0x5a06, 0x09},
 882        {0x5a07, 0xb0},
 883        {0x5a08, 0x06},
 884        {0x5e00, 0x00},
 885        {0x3734, 0x40},
 886        {0x5b00, 0x01},
 887        {0x5b01, 0x10},
 888        {0x5b02, 0x01},
 889        {0x5b03, 0xdb},
 890        {0x3d8c, 0x71},
 891        {0x3d8d, 0xea},
 892        {0x4017, 0x10},
 893        {0x3618, 0x2a},
 894        {0x5780, 0x3e},
 895        {0x5781, 0x0f},
 896        {0x5782, 0x44},
 897        {0x5783, 0x02},
 898        {0x5784, 0x01},
 899        {0x5785, 0x01},
 900        {0x5786, 0x00},
 901        {0x5787, 0x04},
 902        {0x5788, 0x02},
 903        {0x5789, 0x0f},
 904        {0x578a, 0xfd},
 905        {0x578b, 0xf5},
 906        {0x578c, 0xf5},
 907        {0x578d, 0x03},
 908        {0x578e, 0x08},
 909        {0x578f, 0x0c},
 910        {0x5790, 0x08},
 911        {0x5791, 0x06},
 912        {0x5792, 0x00},
 913        {0x5793, 0x52},
 914        {0x5794, 0xa3},
 915        {0x3503, 0x00},
 916        {0x5045, 0x05},
 917        {0x4003, 0x40},
 918        {0x5048, 0x40}
 919};
 920
 921static const struct ov5670_reg mode_2560x1440_regs[] = {
 922        {0x3000, 0x00},
 923        {0x3002, 0x21},
 924        {0x3005, 0xf0},
 925        {0x3007, 0x00},
 926        {0x3015, 0x0f},
 927        {0x3018, 0x32},
 928        {0x301a, 0xf0},
 929        {0x301b, 0xf0},
 930        {0x301c, 0xf0},
 931        {0x301d, 0xf0},
 932        {0x301e, 0xf0},
 933        {0x3030, 0x00},
 934        {0x3031, 0x0a},
 935        {0x303c, 0xff},
 936        {0x303e, 0xff},
 937        {0x3040, 0xf0},
 938        {0x3041, 0x00},
 939        {0x3042, 0xf0},
 940        {0x3106, 0x11},
 941        {0x3500, 0x00},
 942        {0x3501, 0x80},
 943        {0x3502, 0x00},
 944        {0x3503, 0x04},
 945        {0x3504, 0x03},
 946        {0x3505, 0x83},
 947        {0x3508, 0x04},
 948        {0x3509, 0x00},
 949        {0x350e, 0x04},
 950        {0x350f, 0x00},
 951        {0x3510, 0x00},
 952        {0x3511, 0x02},
 953        {0x3512, 0x00},
 954        {0x3601, 0xc8},
 955        {0x3610, 0x88},
 956        {0x3612, 0x48},
 957        {0x3614, 0x5b},
 958        {0x3615, 0x96},
 959        {0x3621, 0xd0},
 960        {0x3622, 0x00},
 961        {0x3623, 0x00},
 962        {0x3633, 0x13},
 963        {0x3634, 0x13},
 964        {0x3635, 0x13},
 965        {0x3636, 0x13},
 966        {0x3645, 0x13},
 967        {0x3646, 0x82},
 968        {0x3650, 0x00},
 969        {0x3652, 0xff},
 970        {0x3655, 0x20},
 971        {0x3656, 0xff},
 972        {0x365a, 0xff},
 973        {0x365e, 0xff},
 974        {0x3668, 0x00},
 975        {0x366a, 0x07},
 976        {0x366e, 0x10},
 977        {0x366d, 0x00},
 978        {0x366f, 0x80},
 979        {0x3700, 0x28},
 980        {0x3701, 0x10},
 981        {0x3702, 0x3a},
 982        {0x3703, 0x19},
 983        {0x3704, 0x10},
 984        {0x3705, 0x00},
 985        {0x3706, 0x66},
 986        {0x3707, 0x08},
 987        {0x3708, 0x34},
 988        {0x3709, 0x40},
 989        {0x370a, 0x01},
 990        {0x370b, 0x1b},
 991        {0x3714, 0x24},
 992        {0x371a, 0x3e},
 993        {0x3733, 0x00},
 994        {0x3734, 0x00},
 995        {0x373a, 0x05},
 996        {0x373b, 0x06},
 997        {0x373c, 0x0a},
 998        {0x373f, 0xa0},
 999        {0x3755, 0x00},
1000        {0x3758, 0x00},
1001        {0x375b, 0x0e},
1002        {0x3766, 0x5f},
1003        {0x3768, 0x00},
1004        {0x3769, 0x22},
1005        {0x3773, 0x08},
1006        {0x3774, 0x1f},
1007        {0x3776, 0x06},
1008        {0x37a0, 0x88},
1009        {0x37a1, 0x5c},
1010        {0x37a7, 0x88},
1011        {0x37a8, 0x70},
1012        {0x37aa, 0x88},
1013        {0x37ab, 0x48},
1014        {0x37b3, 0x66},
1015        {0x37c2, 0x04},
1016        {0x37c5, 0x00},
1017        {0x37c8, 0x00},
1018        {0x3800, 0x00},
1019        {0x3801, 0x0c},
1020        {0x3802, 0x00},
1021        {0x3803, 0x04},
1022        {0x3804, 0x0a},
1023        {0x3805, 0x33},
1024        {0x3806, 0x07},
1025        {0x3807, 0xa3},
1026        {0x3808, 0x0a},
1027        {0x3809, 0x00},
1028        {0x380a, 0x05},
1029        {0x380b, 0xa0},
1030        {0x380c, 0x06},
1031        {0x380d, 0x90},
1032        {0x380e, 0x08},
1033        {0x380f, 0x08},
1034        {0x3811, 0x04},
1035        {0x3813, 0x02},
1036        {0x3814, 0x01},
1037        {0x3815, 0x01},
1038        {0x3816, 0x00},
1039        {0x3817, 0x00},
1040        {0x3818, 0x00},
1041        {0x3819, 0x00},
1042        {0x3820, 0x84},
1043        {0x3821, 0x46},
1044        {0x3822, 0x48},
1045        {0x3826, 0x00},
1046        {0x3827, 0x08},
1047        {0x382a, 0x01},
1048        {0x382b, 0x01},
1049        {0x3830, 0x08},
1050        {0x3836, 0x02},
1051        {0x3837, 0x00},
1052        {0x3838, 0x10},
1053        {0x3841, 0xff},
1054        {0x3846, 0x48},
1055        {0x3861, 0x00},
1056        {0x3862, 0x04},
1057        {0x3863, 0x06},
1058        {0x3a11, 0x01},
1059        {0x3a12, 0x78},
1060        {0x3b00, 0x00},
1061        {0x3b02, 0x00},
1062        {0x3b03, 0x00},
1063        {0x3b04, 0x00},
1064        {0x3b05, 0x00},
1065        {0x3c00, 0x89},
1066        {0x3c01, 0xab},
1067        {0x3c02, 0x01},
1068        {0x3c03, 0x00},
1069        {0x3c04, 0x00},
1070        {0x3c05, 0x03},
1071        {0x3c06, 0x00},
1072        {0x3c07, 0x05},
1073        {0x3c0c, 0x00},
1074        {0x3c0d, 0x00},
1075        {0x3c0e, 0x00},
1076        {0x3c0f, 0x00},
1077        {0x3c40, 0x00},
1078        {0x3c41, 0xa3},
1079        {0x3c43, 0x7d},
1080        {0x3c45, 0xd7},
1081        {0x3c47, 0xfc},
1082        {0x3c50, 0x05},
1083        {0x3c52, 0xaa},
1084        {0x3c54, 0x71},
1085        {0x3c56, 0x80},
1086        {0x3d85, 0x17},
1087        {0x3f03, 0x00},
1088        {0x3f0a, 0x00},
1089        {0x3f0b, 0x00},
1090        {0x4001, 0x60},
1091        {0x4009, 0x0d},
1092        {0x4020, 0x00},
1093        {0x4021, 0x00},
1094        {0x4022, 0x00},
1095        {0x4023, 0x00},
1096        {0x4024, 0x00},
1097        {0x4025, 0x00},
1098        {0x4026, 0x00},
1099        {0x4027, 0x00},
1100        {0x4028, 0x00},
1101        {0x4029, 0x00},
1102        {0x402a, 0x00},
1103        {0x402b, 0x00},
1104        {0x402c, 0x00},
1105        {0x402d, 0x00},
1106        {0x402e, 0x00},
1107        {0x402f, 0x00},
1108        {0x4040, 0x00},
1109        {0x4041, 0x03},
1110        {0x4042, 0x00},
1111        {0x4043, 0x7A},
1112        {0x4044, 0x00},
1113        {0x4045, 0x7A},
1114        {0x4046, 0x00},
1115        {0x4047, 0x7A},
1116        {0x4048, 0x00},
1117        {0x4049, 0x7A},
1118        {0x4307, 0x30},
1119        {0x4500, 0x58},
1120        {0x4501, 0x04},
1121        {0x4502, 0x40},
1122        {0x4503, 0x10},
1123        {0x4508, 0xaa},
1124        {0x4509, 0xaa},
1125        {0x450a, 0x00},
1126        {0x450b, 0x00},
1127        {0x4600, 0x01},
1128        {0x4601, 0x00},
1129        {0x4700, 0xa4},
1130        {0x4800, 0x4c},
1131        {0x4816, 0x53},
1132        {0x481f, 0x40},
1133        {0x4837, 0x13},
1134        {0x5000, 0x56},
1135        {0x5001, 0x01},
1136        {0x5002, 0x28},
1137        {0x5004, 0x0c},
1138        {0x5006, 0x0c},
1139        {0x5007, 0xe0},
1140        {0x5008, 0x01},
1141        {0x5009, 0xb0},
1142        {0x5901, 0x00},
1143        {0x5a01, 0x00},
1144        {0x5a03, 0x00},
1145        {0x5a04, 0x0c},
1146        {0x5a05, 0xe0},
1147        {0x5a06, 0x09},
1148        {0x5a07, 0xb0},
1149        {0x5a08, 0x06},
1150        {0x5e00, 0x00},
1151        {0x3734, 0x40},
1152        {0x5b00, 0x01},
1153        {0x5b01, 0x10},
1154        {0x5b02, 0x01},
1155        {0x5b03, 0xdb},
1156        {0x3d8c, 0x71},
1157        {0x3d8d, 0xea},
1158        {0x4017, 0x08},
1159        {0x3618, 0x2a},
1160        {0x5780, 0x3e},
1161        {0x5781, 0x0f},
1162        {0x5782, 0x44},
1163        {0x5783, 0x02},
1164        {0x5784, 0x01},
1165        {0x5785, 0x01},
1166        {0x5786, 0x00},
1167        {0x5787, 0x04},
1168        {0x5788, 0x02},
1169        {0x5789, 0x0f},
1170        {0x578a, 0xfd},
1171        {0x578b, 0xf5},
1172        {0x578c, 0xf5},
1173        {0x578d, 0x03},
1174        {0x578e, 0x08},
1175        {0x578f, 0x0c},
1176        {0x5790, 0x08},
1177        {0x5791, 0x06},
1178        {0x5792, 0x00},
1179        {0x5793, 0x52},
1180        {0x5794, 0xa3},
1181        {0x5045, 0x05},
1182        {0x4003, 0x40},
1183        {0x5048, 0x40}
1184};
1185
1186static const struct ov5670_reg mode_1280x720_regs[] = {
1187        {0x3000, 0x00},
1188        {0x3002, 0x21},
1189        {0x3005, 0xf0},
1190        {0x3007, 0x00},
1191        {0x3015, 0x0f},
1192        {0x3018, 0x32},
1193        {0x301a, 0xf0},
1194        {0x301b, 0xf0},
1195        {0x301c, 0xf0},
1196        {0x301d, 0xf0},
1197        {0x301e, 0xf0},
1198        {0x3030, 0x00},
1199        {0x3031, 0x0a},
1200        {0x303c, 0xff},
1201        {0x303e, 0xff},
1202        {0x3040, 0xf0},
1203        {0x3041, 0x00},
1204        {0x3042, 0xf0},
1205        {0x3106, 0x11},
1206        {0x3500, 0x00},
1207        {0x3501, 0x80},
1208        {0x3502, 0x00},
1209        {0x3503, 0x04},
1210        {0x3504, 0x03},
1211        {0x3505, 0x83},
1212        {0x3508, 0x04},
1213        {0x3509, 0x00},
1214        {0x350e, 0x04},
1215        {0x350f, 0x00},
1216        {0x3510, 0x00},
1217        {0x3511, 0x02},
1218        {0x3512, 0x00},
1219        {0x3601, 0xc8},
1220        {0x3610, 0x88},
1221        {0x3612, 0x48},
1222        {0x3614, 0x5b},
1223        {0x3615, 0x96},
1224        {0x3621, 0xd0},
1225        {0x3622, 0x00},
1226        {0x3623, 0x00},
1227        {0x3633, 0x13},
1228        {0x3634, 0x13},
1229        {0x3635, 0x13},
1230        {0x3636, 0x13},
1231        {0x3645, 0x13},
1232        {0x3646, 0x82},
1233        {0x3650, 0x00},
1234        {0x3652, 0xff},
1235        {0x3655, 0x20},
1236        {0x3656, 0xff},
1237        {0x365a, 0xff},
1238        {0x365e, 0xff},
1239        {0x3668, 0x00},
1240        {0x366a, 0x07},
1241        {0x366e, 0x08},
1242        {0x366d, 0x00},
1243        {0x366f, 0x80},
1244        {0x3700, 0x28},
1245        {0x3701, 0x10},
1246        {0x3702, 0x3a},
1247        {0x3703, 0x19},
1248        {0x3704, 0x10},
1249        {0x3705, 0x00},
1250        {0x3706, 0x66},
1251        {0x3707, 0x08},
1252        {0x3708, 0x34},
1253        {0x3709, 0x40},
1254        {0x370a, 0x01},
1255        {0x370b, 0x1b},
1256        {0x3714, 0x24},
1257        {0x371a, 0x3e},
1258        {0x3733, 0x00},
1259        {0x3734, 0x00},
1260        {0x373a, 0x05},
1261        {0x373b, 0x06},
1262        {0x373c, 0x0a},
1263        {0x373f, 0xa0},
1264        {0x3755, 0x00},
1265        {0x3758, 0x00},
1266        {0x375b, 0x0e},
1267        {0x3766, 0x5f},
1268        {0x3768, 0x00},
1269        {0x3769, 0x22},
1270        {0x3773, 0x08},
1271        {0x3774, 0x1f},
1272        {0x3776, 0x06},
1273        {0x37a0, 0x88},
1274        {0x37a1, 0x5c},
1275        {0x37a7, 0x88},
1276        {0x37a8, 0x70},
1277        {0x37aa, 0x88},
1278        {0x37ab, 0x48},
1279        {0x37b3, 0x66},
1280        {0x37c2, 0x04},
1281        {0x37c5, 0x00},
1282        {0x37c8, 0x00},
1283        {0x3800, 0x00},
1284        {0x3801, 0x0c},
1285        {0x3802, 0x00},
1286        {0x3803, 0x04},
1287        {0x3804, 0x0a},
1288        {0x3805, 0x33},
1289        {0x3806, 0x07},
1290        {0x3807, 0xa3},
1291        {0x3808, 0x05},
1292        {0x3809, 0x00},
1293        {0x380a, 0x02},
1294        {0x380b, 0xd0},
1295        {0x380c, 0x06},
1296        {0x380d, 0x90},
1297        {0x380e, 0x08},
1298        {0x380f, 0x08},
1299        {0x3811, 0x04},
1300        {0x3813, 0x02},
1301        {0x3814, 0x03},
1302        {0x3815, 0x01},
1303        {0x3816, 0x00},
1304        {0x3817, 0x00},
1305        {0x3818, 0x00},
1306        {0x3819, 0x00},
1307        {0x3820, 0x94},
1308        {0x3821, 0x47},
1309        {0x3822, 0x48},
1310        {0x3826, 0x00},
1311        {0x3827, 0x08},
1312        {0x382a, 0x03},
1313        {0x382b, 0x01},
1314        {0x3830, 0x08},
1315        {0x3836, 0x02},
1316        {0x3837, 0x00},
1317        {0x3838, 0x10},
1318        {0x3841, 0xff},
1319        {0x3846, 0x48},
1320        {0x3861, 0x00},
1321        {0x3862, 0x04},
1322        {0x3863, 0x06},
1323        {0x3a11, 0x01},
1324        {0x3a12, 0x78},
1325        {0x3b00, 0x00},
1326        {0x3b02, 0x00},
1327        {0x3b03, 0x00},
1328        {0x3b04, 0x00},
1329        {0x3b05, 0x00},
1330        {0x3c00, 0x89},
1331        {0x3c01, 0xab},
1332        {0x3c02, 0x01},
1333        {0x3c03, 0x00},
1334        {0x3c04, 0x00},
1335        {0x3c05, 0x03},
1336        {0x3c06, 0x00},
1337        {0x3c07, 0x05},
1338        {0x3c0c, 0x00},
1339        {0x3c0d, 0x00},
1340        {0x3c0e, 0x00},
1341        {0x3c0f, 0x00},
1342        {0x3c40, 0x00},
1343        {0x3c41, 0xa3},
1344        {0x3c43, 0x7d},
1345        {0x3c45, 0xd7},
1346        {0x3c47, 0xfc},
1347        {0x3c50, 0x05},
1348        {0x3c52, 0xaa},
1349        {0x3c54, 0x71},
1350        {0x3c56, 0x80},
1351        {0x3d85, 0x17},
1352        {0x3f03, 0x00},
1353        {0x3f0a, 0x00},
1354        {0x3f0b, 0x00},
1355        {0x4001, 0x60},
1356        {0x4009, 0x05},
1357        {0x4020, 0x00},
1358        {0x4021, 0x00},
1359        {0x4022, 0x00},
1360        {0x4023, 0x00},
1361        {0x4024, 0x00},
1362        {0x4025, 0x00},
1363        {0x4026, 0x00},
1364        {0x4027, 0x00},
1365        {0x4028, 0x00},
1366        {0x4029, 0x00},
1367        {0x402a, 0x00},
1368        {0x402b, 0x00},
1369        {0x402c, 0x00},
1370        {0x402d, 0x00},
1371        {0x402e, 0x00},
1372        {0x402f, 0x00},
1373        {0x4040, 0x00},
1374        {0x4041, 0x03},
1375        {0x4042, 0x00},
1376        {0x4043, 0x7A},
1377        {0x4044, 0x00},
1378        {0x4045, 0x7A},
1379        {0x4046, 0x00},
1380        {0x4047, 0x7A},
1381        {0x4048, 0x00},
1382        {0x4049, 0x7A},
1383        {0x4307, 0x30},
1384        {0x4500, 0x58},
1385        {0x4501, 0x04},
1386        {0x4502, 0x48},
1387        {0x4503, 0x10},
1388        {0x4508, 0x55},
1389        {0x4509, 0x55},
1390        {0x450a, 0x00},
1391        {0x450b, 0x00},
1392        {0x4600, 0x00},
1393        {0x4601, 0x80},
1394        {0x4700, 0xa4},
1395        {0x4800, 0x4c},
1396        {0x4816, 0x53},
1397        {0x481f, 0x40},
1398        {0x4837, 0x13},
1399        {0x5000, 0x56},
1400        {0x5001, 0x01},
1401        {0x5002, 0x28},
1402        {0x5004, 0x0c},
1403        {0x5006, 0x0c},
1404        {0x5007, 0xe0},
1405        {0x5008, 0x01},
1406        {0x5009, 0xb0},
1407        {0x5901, 0x00},
1408        {0x5a01, 0x00},
1409        {0x5a03, 0x00},
1410        {0x5a04, 0x0c},
1411        {0x5a05, 0xe0},
1412        {0x5a06, 0x09},
1413        {0x5a07, 0xb0},
1414        {0x5a08, 0x06},
1415        {0x5e00, 0x00},
1416        {0x3734, 0x40},
1417        {0x5b00, 0x01},
1418        {0x5b01, 0x10},
1419        {0x5b02, 0x01},
1420        {0x5b03, 0xdb},
1421        {0x3d8c, 0x71},
1422        {0x3d8d, 0xea},
1423        {0x4017, 0x10},
1424        {0x3618, 0x2a},
1425        {0x5780, 0x3e},
1426        {0x5781, 0x0f},
1427        {0x5782, 0x44},
1428        {0x5783, 0x02},
1429        {0x5784, 0x01},
1430        {0x5785, 0x01},
1431        {0x5786, 0x00},
1432        {0x5787, 0x04},
1433        {0x5788, 0x02},
1434        {0x5789, 0x0f},
1435        {0x578a, 0xfd},
1436        {0x578b, 0xf5},
1437        {0x578c, 0xf5},
1438        {0x578d, 0x03},
1439        {0x578e, 0x08},
1440        {0x578f, 0x0c},
1441        {0x5790, 0x08},
1442        {0x5791, 0x06},
1443        {0x5792, 0x00},
1444        {0x5793, 0x52},
1445        {0x5794, 0xa3},
1446        {0x3503, 0x00},
1447        {0x5045, 0x05},
1448        {0x4003, 0x40},
1449        {0x5048, 0x40}
1450};
1451
1452static const struct ov5670_reg mode_640x360_regs[] = {
1453        {0x3000, 0x00},
1454        {0x3002, 0x21},
1455        {0x3005, 0xf0},
1456        {0x3007, 0x00},
1457        {0x3015, 0x0f},
1458        {0x3018, 0x32},
1459        {0x301a, 0xf0},
1460        {0x301b, 0xf0},
1461        {0x301c, 0xf0},
1462        {0x301d, 0xf0},
1463        {0x301e, 0xf0},
1464        {0x3030, 0x00},
1465        {0x3031, 0x0a},
1466        {0x303c, 0xff},
1467        {0x303e, 0xff},
1468        {0x3040, 0xf0},
1469        {0x3041, 0x00},
1470        {0x3042, 0xf0},
1471        {0x3106, 0x11},
1472        {0x3500, 0x00},
1473        {0x3501, 0x80},
1474        {0x3502, 0x00},
1475        {0x3503, 0x04},
1476        {0x3504, 0x03},
1477        {0x3505, 0x83},
1478        {0x3508, 0x04},
1479        {0x3509, 0x00},
1480        {0x350e, 0x04},
1481        {0x350f, 0x00},
1482        {0x3510, 0x00},
1483        {0x3511, 0x02},
1484        {0x3512, 0x00},
1485        {0x3601, 0xc8},
1486        {0x3610, 0x88},
1487        {0x3612, 0x48},
1488        {0x3614, 0x5b},
1489        {0x3615, 0x96},
1490        {0x3621, 0xd0},
1491        {0x3622, 0x00},
1492        {0x3623, 0x04},
1493        {0x3633, 0x13},
1494        {0x3634, 0x13},
1495        {0x3635, 0x13},
1496        {0x3636, 0x13},
1497        {0x3645, 0x13},
1498        {0x3646, 0x82},
1499        {0x3650, 0x00},
1500        {0x3652, 0xff},
1501        {0x3655, 0x20},
1502        {0x3656, 0xff},
1503        {0x365a, 0xff},
1504        {0x365e, 0xff},
1505        {0x3668, 0x00},
1506        {0x366a, 0x07},
1507        {0x366e, 0x08},
1508        {0x366d, 0x00},
1509        {0x366f, 0x80},
1510        {0x3700, 0x28},
1511        {0x3701, 0x10},
1512        {0x3702, 0x3a},
1513        {0x3703, 0x19},
1514        {0x3704, 0x10},
1515        {0x3705, 0x00},
1516        {0x3706, 0x66},
1517        {0x3707, 0x08},
1518        {0x3708, 0x34},
1519        {0x3709, 0x40},
1520        {0x370a, 0x01},
1521        {0x370b, 0x1b},
1522        {0x3714, 0x24},
1523        {0x371a, 0x3e},
1524        {0x3733, 0x00},
1525        {0x3734, 0x00},
1526        {0x373a, 0x05},
1527        {0x373b, 0x06},
1528        {0x373c, 0x0a},
1529        {0x373f, 0xa0},
1530        {0x3755, 0x00},
1531        {0x3758, 0x00},
1532        {0x375b, 0x0e},
1533        {0x3766, 0x5f},
1534        {0x3768, 0x00},
1535        {0x3769, 0x22},
1536        {0x3773, 0x08},
1537        {0x3774, 0x1f},
1538        {0x3776, 0x06},
1539        {0x37a0, 0x88},
1540        {0x37a1, 0x5c},
1541        {0x37a7, 0x88},
1542        {0x37a8, 0x70},
1543        {0x37aa, 0x88},
1544        {0x37ab, 0x48},
1545        {0x37b3, 0x66},
1546        {0x37c2, 0x04},
1547        {0x37c5, 0x00},
1548        {0x37c8, 0x00},
1549        {0x3800, 0x00},
1550        {0x3801, 0x0c},
1551        {0x3802, 0x00},
1552        {0x3803, 0x04},
1553        {0x3804, 0x0a},
1554        {0x3805, 0x33},
1555        {0x3806, 0x07},
1556        {0x3807, 0xa3},
1557        {0x3808, 0x02},
1558        {0x3809, 0x80},
1559        {0x380a, 0x01},
1560        {0x380b, 0x68},
1561        {0x380c, 0x06},
1562        {0x380d, 0x90},
1563        {0x380e, 0x08},
1564        {0x380f, 0x08},
1565        {0x3811, 0x04},
1566        {0x3813, 0x02},
1567        {0x3814, 0x07},
1568        {0x3815, 0x01},
1569        {0x3816, 0x00},
1570        {0x3817, 0x00},
1571        {0x3818, 0x00},
1572        {0x3819, 0x00},
1573        {0x3820, 0x94},
1574        {0x3821, 0xc6},
1575        {0x3822, 0x48},
1576        {0x3826, 0x00},
1577        {0x3827, 0x08},
1578        {0x382a, 0x07},
1579        {0x382b, 0x01},
1580        {0x3830, 0x08},
1581        {0x3836, 0x02},
1582        {0x3837, 0x00},
1583        {0x3838, 0x10},
1584        {0x3841, 0xff},
1585        {0x3846, 0x48},
1586        {0x3861, 0x00},
1587        {0x3862, 0x04},
1588        {0x3863, 0x06},
1589        {0x3a11, 0x01},
1590        {0x3a12, 0x78},
1591        {0x3b00, 0x00},
1592        {0x3b02, 0x00},
1593        {0x3b03, 0x00},
1594        {0x3b04, 0x00},
1595        {0x3b05, 0x00},
1596        {0x3c00, 0x89},
1597        {0x3c01, 0xab},
1598        {0x3c02, 0x01},
1599        {0x3c03, 0x00},
1600        {0x3c04, 0x00},
1601        {0x3c05, 0x03},
1602        {0x3c06, 0x00},
1603        {0x3c07, 0x05},
1604        {0x3c0c, 0x00},
1605        {0x3c0d, 0x00},
1606        {0x3c0e, 0x00},
1607        {0x3c0f, 0x00},
1608        {0x3c40, 0x00},
1609        {0x3c41, 0xa3},
1610        {0x3c43, 0x7d},
1611        {0x3c45, 0xd7},
1612        {0x3c47, 0xfc},
1613        {0x3c50, 0x05},
1614        {0x3c52, 0xaa},
1615        {0x3c54, 0x71},
1616        {0x3c56, 0x80},
1617        {0x3d85, 0x17},
1618        {0x3f03, 0x00},
1619        {0x3f0a, 0x00},
1620        {0x3f0b, 0x00},
1621        {0x4001, 0x60},
1622        {0x4009, 0x05},
1623        {0x4020, 0x00},
1624        {0x4021, 0x00},
1625        {0x4022, 0x00},
1626        {0x4023, 0x00},
1627        {0x4024, 0x00},
1628        {0x4025, 0x00},
1629        {0x4026, 0x00},
1630        {0x4027, 0x00},
1631        {0x4028, 0x00},
1632        {0x4029, 0x00},
1633        {0x402a, 0x00},
1634        {0x402b, 0x00},
1635        {0x402c, 0x00},
1636        {0x402d, 0x00},
1637        {0x402e, 0x00},
1638        {0x402f, 0x00},
1639        {0x4040, 0x00},
1640        {0x4041, 0x03},
1641        {0x4042, 0x00},
1642        {0x4043, 0x7A},
1643        {0x4044, 0x00},
1644        {0x4045, 0x7A},
1645        {0x4046, 0x00},
1646        {0x4047, 0x7A},
1647        {0x4048, 0x00},
1648        {0x4049, 0x7A},
1649        {0x4307, 0x30},
1650        {0x4500, 0x58},
1651        {0x4501, 0x04},
1652        {0x4502, 0x40},
1653        {0x4503, 0x10},
1654        {0x4508, 0x55},
1655        {0x4509, 0x55},
1656        {0x450a, 0x02},
1657        {0x450b, 0x00},
1658        {0x4600, 0x00},
1659        {0x4601, 0x40},
1660        {0x4700, 0xa4},
1661        {0x4800, 0x4c},
1662        {0x4816, 0x53},
1663        {0x481f, 0x40},
1664        {0x4837, 0x13},
1665        {0x5000, 0x56},
1666        {0x5001, 0x01},
1667        {0x5002, 0x28},
1668        {0x5004, 0x0c},
1669        {0x5006, 0x0c},
1670        {0x5007, 0xe0},
1671        {0x5008, 0x01},
1672        {0x5009, 0xb0},
1673        {0x5901, 0x00},
1674        {0x5a01, 0x00},
1675        {0x5a03, 0x00},
1676        {0x5a04, 0x0c},
1677        {0x5a05, 0xe0},
1678        {0x5a06, 0x09},
1679        {0x5a07, 0xb0},
1680        {0x5a08, 0x06},
1681        {0x5e00, 0x00},
1682        {0x3734, 0x40},
1683        {0x5b00, 0x01},
1684        {0x5b01, 0x10},
1685        {0x5b02, 0x01},
1686        {0x5b03, 0xdb},
1687        {0x3d8c, 0x71},
1688        {0x3d8d, 0xea},
1689        {0x4017, 0x10},
1690        {0x3618, 0x2a},
1691        {0x5780, 0x3e},
1692        {0x5781, 0x0f},
1693        {0x5782, 0x44},
1694        {0x5783, 0x02},
1695        {0x5784, 0x01},
1696        {0x5785, 0x01},
1697        {0x5786, 0x00},
1698        {0x5787, 0x04},
1699        {0x5788, 0x02},
1700        {0x5789, 0x0f},
1701        {0x578a, 0xfd},
1702        {0x578b, 0xf5},
1703        {0x578c, 0xf5},
1704        {0x578d, 0x03},
1705        {0x578e, 0x08},
1706        {0x578f, 0x0c},
1707        {0x5790, 0x08},
1708        {0x5791, 0x06},
1709        {0x5792, 0x00},
1710        {0x5793, 0x52},
1711        {0x5794, 0xa3},
1712        {0x3503, 0x00},
1713        {0x5045, 0x05},
1714        {0x4003, 0x40},
1715        {0x5048, 0x40}
1716};
1717
1718static const char * const ov5670_test_pattern_menu[] = {
1719        "Disabled",
1720        "Vertical Color Bar Type 1",
1721};
1722
1723/* Supported link frequencies */
1724#define OV5670_LINK_FREQ_422MHZ         422400000
1725#define OV5670_LINK_FREQ_422MHZ_INDEX   0
1726static const struct ov5670_link_freq_config link_freq_configs[] = {
1727        {
1728                /* pixel_rate = link_freq * 2 * nr_of_lanes / bits_per_sample */
1729                .pixel_rate = (OV5670_LINK_FREQ_422MHZ * 2 * 2) / 10,
1730                .reg_list = {
1731                        .num_of_regs = ARRAY_SIZE(mipi_data_rate_840mbps),
1732                        .regs = mipi_data_rate_840mbps,
1733                }
1734        }
1735};
1736
1737static const s64 link_freq_menu_items[] = {
1738        OV5670_LINK_FREQ_422MHZ
1739};
1740
1741/*
1742 * OV5670 sensor supports following resolutions with full FOV:
1743 * 4:3  ==> {2592x1944, 1296x972, 648x486}
1744 * 16:9 ==> {2560x1440, 1280x720, 640x360}
1745 */
1746static const struct ov5670_mode supported_modes[] = {
1747        {
1748                .width = 2592,
1749                .height = 1944,
1750                .vts_def = OV5670_VTS_30FPS,
1751                .vts_min = OV5670_VTS_30FPS,
1752                .reg_list = {
1753                        .num_of_regs = ARRAY_SIZE(mode_2592x1944_regs),
1754                        .regs = mode_2592x1944_regs,
1755                },
1756                .link_freq_index = OV5670_LINK_FREQ_422MHZ_INDEX,
1757        },
1758        {
1759                .width = 1296,
1760                .height = 972,
1761                .vts_def = OV5670_VTS_30FPS,
1762                .vts_min = 996,
1763                .reg_list = {
1764                        .num_of_regs = ARRAY_SIZE(mode_1296x972_regs),
1765                        .regs = mode_1296x972_regs,
1766                },
1767                .link_freq_index = OV5670_LINK_FREQ_422MHZ_INDEX,
1768        },
1769        {
1770                .width = 648,
1771                .height = 486,
1772                .vts_def = OV5670_VTS_30FPS,
1773                .vts_min = 516,
1774                .reg_list = {
1775                        .num_of_regs = ARRAY_SIZE(mode_648x486_regs),
1776                        .regs = mode_648x486_regs,
1777                },
1778                .link_freq_index = OV5670_LINK_FREQ_422MHZ_INDEX,
1779        },
1780        {
1781                .width = 2560,
1782                .height = 1440,
1783                .vts_def = OV5670_VTS_30FPS,
1784                .vts_min = OV5670_VTS_30FPS,
1785                .reg_list = {
1786                        .num_of_regs = ARRAY_SIZE(mode_2560x1440_regs),
1787                        .regs = mode_2560x1440_regs,
1788                },
1789                .link_freq_index = OV5670_LINK_FREQ_422MHZ_INDEX,
1790        },
1791        {
1792                .width = 1280,
1793                .height = 720,
1794                .vts_def = OV5670_VTS_30FPS,
1795                .vts_min = 1020,
1796                .reg_list = {
1797                        .num_of_regs = ARRAY_SIZE(mode_1280x720_regs),
1798                        .regs = mode_1280x720_regs,
1799                },
1800                .link_freq_index = OV5670_LINK_FREQ_422MHZ_INDEX,
1801        },
1802        {
1803                .width = 640,
1804                .height = 360,
1805                .vts_def = OV5670_VTS_30FPS,
1806                .vts_min = 510,
1807                .reg_list = {
1808                        .num_of_regs = ARRAY_SIZE(mode_640x360_regs),
1809                        .regs = mode_640x360_regs,
1810                },
1811                .link_freq_index = OV5670_LINK_FREQ_422MHZ_INDEX,
1812        }
1813};
1814
1815struct ov5670 {
1816        struct v4l2_subdev sd;
1817        struct media_pad pad;
1818
1819        struct v4l2_ctrl_handler ctrl_handler;
1820        /* V4L2 Controls */
1821        struct v4l2_ctrl *link_freq;
1822        struct v4l2_ctrl *pixel_rate;
1823        struct v4l2_ctrl *vblank;
1824        struct v4l2_ctrl *hblank;
1825        struct v4l2_ctrl *exposure;
1826
1827        /* Current mode */
1828        const struct ov5670_mode *cur_mode;
1829
1830        /* To serialize asynchronus callbacks */
1831        struct mutex mutex;
1832
1833        /* Streaming on/off */
1834        bool streaming;
1835};
1836
1837#define to_ov5670(_sd)  container_of(_sd, struct ov5670, sd)
1838
1839/* Read registers up to 4 at a time */
1840static int ov5670_read_reg(struct ov5670 *ov5670, u16 reg, unsigned int len,
1841                           u32 *val)
1842{
1843        struct i2c_client *client = v4l2_get_subdevdata(&ov5670->sd);
1844        struct i2c_msg msgs[2];
1845        u8 *data_be_p;
1846        __be32 data_be = 0;
1847        __be16 reg_addr_be = cpu_to_be16(reg);
1848        int ret;
1849
1850        if (len > 4)
1851                return -EINVAL;
1852
1853        data_be_p = (u8 *)&data_be;
1854        /* Write register address */
1855        msgs[0].addr = client->addr;
1856        msgs[0].flags = 0;
1857        msgs[0].len = 2;
1858        msgs[0].buf = (u8 *)&reg_addr_be;
1859
1860        /* Read data from register */
1861        msgs[1].addr = client->addr;
1862        msgs[1].flags = I2C_M_RD;
1863        msgs[1].len = len;
1864        msgs[1].buf = &data_be_p[4 - len];
1865
1866        ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
1867        if (ret != ARRAY_SIZE(msgs))
1868                return -EIO;
1869
1870        *val = be32_to_cpu(data_be);
1871
1872        return 0;
1873}
1874
1875/* Write registers up to 4 at a time */
1876static int ov5670_write_reg(struct ov5670 *ov5670, u16 reg, unsigned int len,
1877                            u32 val)
1878{
1879        struct i2c_client *client = v4l2_get_subdevdata(&ov5670->sd);
1880        int buf_i;
1881        int val_i;
1882        u8 buf[6];
1883        u8 *val_p;
1884        __be32 tmp;
1885
1886        if (len > 4)
1887                return -EINVAL;
1888
1889        buf[0] = reg >> 8;
1890        buf[1] = reg & 0xff;
1891
1892        tmp = cpu_to_be32(val);
1893        val_p = (u8 *)&tmp;
1894        buf_i = 2;
1895        val_i = 4 - len;
1896
1897        while (val_i < 4)
1898                buf[buf_i++] = val_p[val_i++];
1899
1900        if (i2c_master_send(client, buf, len + 2) != len + 2)
1901                return -EIO;
1902
1903        return 0;
1904}
1905
1906/* Write a list of registers */
1907static int ov5670_write_regs(struct ov5670 *ov5670,
1908                             const struct ov5670_reg *regs, unsigned int len)
1909{
1910        struct i2c_client *client = v4l2_get_subdevdata(&ov5670->sd);
1911        unsigned int i;
1912        int ret;
1913
1914        for (i = 0; i < len; i++) {
1915                ret = ov5670_write_reg(ov5670, regs[i].address, 1, regs[i].val);
1916                if (ret) {
1917                        dev_err_ratelimited(
1918                                &client->dev,
1919                                "Failed to write reg 0x%4.4x. error = %d\n",
1920                                regs[i].address, ret);
1921
1922                        return ret;
1923                }
1924        }
1925
1926        return 0;
1927}
1928
1929static int ov5670_write_reg_list(struct ov5670 *ov5670,
1930                                 const struct ov5670_reg_list *r_list)
1931{
1932        return ov5670_write_regs(ov5670, r_list->regs, r_list->num_of_regs);
1933}
1934
1935/* Open sub-device */
1936static int ov5670_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
1937{
1938        struct ov5670 *ov5670 = to_ov5670(sd);
1939        struct v4l2_mbus_framefmt *try_fmt =
1940                                v4l2_subdev_get_try_format(sd, fh->state, 0);
1941
1942        mutex_lock(&ov5670->mutex);
1943
1944        /* Initialize try_fmt */
1945        try_fmt->width = ov5670->cur_mode->width;
1946        try_fmt->height = ov5670->cur_mode->height;
1947        try_fmt->code = MEDIA_BUS_FMT_SGRBG10_1X10;
1948        try_fmt->field = V4L2_FIELD_NONE;
1949
1950        /* No crop or compose */
1951        mutex_unlock(&ov5670->mutex);
1952
1953        return 0;
1954}
1955
1956static int ov5670_update_digital_gain(struct ov5670 *ov5670, u32 d_gain)
1957{
1958        int ret;
1959
1960        ret = ov5670_write_reg(ov5670, OV5670_REG_R_DGTL_GAIN,
1961                               OV5670_REG_VALUE_16BIT, d_gain);
1962        if (ret)
1963                return ret;
1964
1965        ret = ov5670_write_reg(ov5670, OV5670_REG_G_DGTL_GAIN,
1966                               OV5670_REG_VALUE_16BIT, d_gain);
1967        if (ret)
1968                return ret;
1969
1970        return ov5670_write_reg(ov5670, OV5670_REG_B_DGTL_GAIN,
1971                                OV5670_REG_VALUE_16BIT, d_gain);
1972}
1973
1974static int ov5670_enable_test_pattern(struct ov5670 *ov5670, u32 pattern)
1975{
1976        u32 val;
1977        int ret;
1978
1979        /* Set the bayer order that we support */
1980        ret = ov5670_write_reg(ov5670, OV5670_REG_TEST_PATTERN_CTRL,
1981                               OV5670_REG_VALUE_08BIT, 0);
1982        if (ret)
1983                return ret;
1984
1985        ret = ov5670_read_reg(ov5670, OV5670_REG_TEST_PATTERN,
1986                              OV5670_REG_VALUE_08BIT, &val);
1987        if (ret)
1988                return ret;
1989
1990        if (pattern)
1991                val |= OV5670_TEST_PATTERN_ENABLE;
1992        else
1993                val &= ~OV5670_TEST_PATTERN_ENABLE;
1994
1995        return ov5670_write_reg(ov5670, OV5670_REG_TEST_PATTERN,
1996                                OV5670_REG_VALUE_08BIT, val);
1997}
1998
1999/* Initialize control handlers */
2000static int ov5670_set_ctrl(struct v4l2_ctrl *ctrl)
2001{
2002        struct ov5670 *ov5670 = container_of(ctrl->handler,
2003                                             struct ov5670, ctrl_handler);
2004        struct i2c_client *client = v4l2_get_subdevdata(&ov5670->sd);
2005        s64 max;
2006        int ret = 0;
2007
2008        /* Propagate change of current control to all related controls */
2009        switch (ctrl->id) {
2010        case V4L2_CID_VBLANK:
2011                /* Update max exposure while meeting expected vblanking */
2012                max = ov5670->cur_mode->height + ctrl->val - 8;
2013                __v4l2_ctrl_modify_range(ov5670->exposure,
2014                                         ov5670->exposure->minimum, max,
2015                                         ov5670->exposure->step, max);
2016                break;
2017        }
2018
2019        /* V4L2 controls values will be applied only when power is already up */
2020        if (!pm_runtime_get_if_in_use(&client->dev))
2021                return 0;
2022
2023        switch (ctrl->id) {
2024        case V4L2_CID_ANALOGUE_GAIN:
2025                ret = ov5670_write_reg(ov5670, OV5670_REG_ANALOG_GAIN,
2026                                       OV5670_REG_VALUE_16BIT, ctrl->val);
2027                break;
2028        case V4L2_CID_DIGITAL_GAIN:
2029                ret = ov5670_update_digital_gain(ov5670, ctrl->val);
2030                break;
2031        case V4L2_CID_EXPOSURE:
2032                /* 4 least significant bits of expsoure are fractional part */
2033                ret = ov5670_write_reg(ov5670, OV5670_REG_EXPOSURE,
2034                                       OV5670_REG_VALUE_24BIT, ctrl->val << 4);
2035                break;
2036        case V4L2_CID_VBLANK:
2037                /* Update VTS that meets expected vertical blanking */
2038                ret = ov5670_write_reg(ov5670, OV5670_REG_VTS,
2039                                       OV5670_REG_VALUE_16BIT,
2040                                       ov5670->cur_mode->height + ctrl->val);
2041                break;
2042        case V4L2_CID_TEST_PATTERN:
2043                ret = ov5670_enable_test_pattern(ov5670, ctrl->val);
2044                break;
2045        default:
2046                dev_info(&client->dev, "%s Unhandled id:0x%x, val:0x%x\n",
2047                         __func__, ctrl->id, ctrl->val);
2048                break;
2049        }
2050
2051        pm_runtime_put(&client->dev);
2052
2053        return ret;
2054}
2055
2056static const struct v4l2_ctrl_ops ov5670_ctrl_ops = {
2057        .s_ctrl = ov5670_set_ctrl,
2058};
2059
2060/* Initialize control handlers */
2061static int ov5670_init_controls(struct ov5670 *ov5670)
2062{
2063        struct i2c_client *client = v4l2_get_subdevdata(&ov5670->sd);
2064        struct v4l2_fwnode_device_properties props;
2065        struct v4l2_ctrl_handler *ctrl_hdlr;
2066        s64 vblank_max;
2067        s64 vblank_def;
2068        s64 vblank_min;
2069        s64 exposure_max;
2070        int ret;
2071
2072        ctrl_hdlr = &ov5670->ctrl_handler;
2073        ret = v4l2_ctrl_handler_init(ctrl_hdlr, 10);
2074        if (ret)
2075                return ret;
2076
2077        ctrl_hdlr->lock = &ov5670->mutex;
2078        ov5670->link_freq = v4l2_ctrl_new_int_menu(ctrl_hdlr,
2079                                                   &ov5670_ctrl_ops,
2080                                                   V4L2_CID_LINK_FREQ,
2081                                                   0, 0, link_freq_menu_items);
2082        if (ov5670->link_freq)
2083                ov5670->link_freq->flags |= V4L2_CTRL_FLAG_READ_ONLY;
2084
2085        /* By default, V4L2_CID_PIXEL_RATE is read only */
2086        ov5670->pixel_rate = v4l2_ctrl_new_std(ctrl_hdlr, &ov5670_ctrl_ops,
2087                                               V4L2_CID_PIXEL_RATE,
2088                                               link_freq_configs[0].pixel_rate,
2089                                               link_freq_configs[0].pixel_rate,
2090                                               1,
2091                                               link_freq_configs[0].pixel_rate);
2092
2093        vblank_max = OV5670_VTS_MAX - ov5670->cur_mode->height;
2094        vblank_def = ov5670->cur_mode->vts_def - ov5670->cur_mode->height;
2095        vblank_min = ov5670->cur_mode->vts_min - ov5670->cur_mode->height;
2096        ov5670->vblank = v4l2_ctrl_new_std(ctrl_hdlr, &ov5670_ctrl_ops,
2097                                           V4L2_CID_VBLANK, vblank_min,
2098                                           vblank_max, 1, vblank_def);
2099
2100        ov5670->hblank = v4l2_ctrl_new_std(
2101                                ctrl_hdlr, &ov5670_ctrl_ops, V4L2_CID_HBLANK,
2102                                OV5670_FIXED_PPL - ov5670->cur_mode->width,
2103                                OV5670_FIXED_PPL - ov5670->cur_mode->width, 1,
2104                                OV5670_FIXED_PPL - ov5670->cur_mode->width);
2105        if (ov5670->hblank)
2106                ov5670->hblank->flags |= V4L2_CTRL_FLAG_READ_ONLY;
2107
2108        /* Get min, max, step, default from sensor */
2109        v4l2_ctrl_new_std(ctrl_hdlr, &ov5670_ctrl_ops, V4L2_CID_ANALOGUE_GAIN,
2110                          ANALOG_GAIN_MIN, ANALOG_GAIN_MAX, ANALOG_GAIN_STEP,
2111                          ANALOG_GAIN_DEFAULT);
2112
2113        /* Digital gain */
2114        v4l2_ctrl_new_std(ctrl_hdlr, &ov5670_ctrl_ops, V4L2_CID_DIGITAL_GAIN,
2115                          OV5670_DGTL_GAIN_MIN, OV5670_DGTL_GAIN_MAX,
2116                          OV5670_DGTL_GAIN_STEP, OV5670_DGTL_GAIN_DEFAULT);
2117
2118        /* Get min, max, step, default from sensor */
2119        exposure_max = ov5670->cur_mode->vts_def - 8;
2120        ov5670->exposure = v4l2_ctrl_new_std(ctrl_hdlr, &ov5670_ctrl_ops,
2121                                             V4L2_CID_EXPOSURE,
2122                                             OV5670_EXPOSURE_MIN,
2123                                             exposure_max, OV5670_EXPOSURE_STEP,
2124                                             exposure_max);
2125
2126        v4l2_ctrl_new_std_menu_items(ctrl_hdlr, &ov5670_ctrl_ops,
2127                                     V4L2_CID_TEST_PATTERN,
2128                                     ARRAY_SIZE(ov5670_test_pattern_menu) - 1,
2129                                     0, 0, ov5670_test_pattern_menu);
2130
2131        if (ctrl_hdlr->error) {
2132                ret = ctrl_hdlr->error;
2133                goto error;
2134        }
2135
2136        ret = v4l2_fwnode_device_parse(&client->dev, &props);
2137        if (ret)
2138                goto error;
2139
2140        ret = v4l2_ctrl_new_fwnode_properties(ctrl_hdlr, &ov5670_ctrl_ops,
2141                                              &props);
2142        if (ret)
2143                goto error;
2144
2145        ov5670->sd.ctrl_handler = ctrl_hdlr;
2146
2147        return 0;
2148
2149error:
2150        v4l2_ctrl_handler_free(ctrl_hdlr);
2151
2152        return ret;
2153}
2154
2155static int ov5670_enum_mbus_code(struct v4l2_subdev *sd,
2156                                 struct v4l2_subdev_state *sd_state,
2157                                 struct v4l2_subdev_mbus_code_enum *code)
2158{
2159        /* Only one bayer order GRBG is supported */
2160        if (code->index > 0)
2161                return -EINVAL;
2162
2163        code->code = MEDIA_BUS_FMT_SGRBG10_1X10;
2164
2165        return 0;
2166}
2167
2168static int ov5670_enum_frame_size(struct v4l2_subdev *sd,
2169                                  struct v4l2_subdev_state *sd_state,
2170                                  struct v4l2_subdev_frame_size_enum *fse)
2171{
2172        if (fse->index >= ARRAY_SIZE(supported_modes))
2173                return -EINVAL;
2174
2175        if (fse->code != MEDIA_BUS_FMT_SGRBG10_1X10)
2176                return -EINVAL;
2177
2178        fse->min_width = supported_modes[fse->index].width;
2179        fse->max_width = fse->min_width;
2180        fse->min_height = supported_modes[fse->index].height;
2181        fse->max_height = fse->min_height;
2182
2183        return 0;
2184}
2185
2186static void ov5670_update_pad_format(const struct ov5670_mode *mode,
2187                                     struct v4l2_subdev_format *fmt)
2188{
2189        fmt->format.width = mode->width;
2190        fmt->format.height = mode->height;
2191        fmt->format.code = MEDIA_BUS_FMT_SGRBG10_1X10;
2192        fmt->format.field = V4L2_FIELD_NONE;
2193}
2194
2195static int ov5670_do_get_pad_format(struct ov5670 *ov5670,
2196                                    struct v4l2_subdev_state *sd_state,
2197                                    struct v4l2_subdev_format *fmt)
2198{
2199        if (fmt->which == V4L2_SUBDEV_FORMAT_TRY)
2200                fmt->format = *v4l2_subdev_get_try_format(&ov5670->sd,
2201                                                          sd_state,
2202                                                          fmt->pad);
2203        else
2204                ov5670_update_pad_format(ov5670->cur_mode, fmt);
2205
2206        return 0;
2207}
2208
2209static int ov5670_get_pad_format(struct v4l2_subdev *sd,
2210                                 struct v4l2_subdev_state *sd_state,
2211                                 struct v4l2_subdev_format *fmt)
2212{
2213        struct ov5670 *ov5670 = to_ov5670(sd);
2214        int ret;
2215
2216        mutex_lock(&ov5670->mutex);
2217        ret = ov5670_do_get_pad_format(ov5670, sd_state, fmt);
2218        mutex_unlock(&ov5670->mutex);
2219
2220        return ret;
2221}
2222
2223static int ov5670_set_pad_format(struct v4l2_subdev *sd,
2224                                 struct v4l2_subdev_state *sd_state,
2225                                 struct v4l2_subdev_format *fmt)
2226{
2227        struct ov5670 *ov5670 = to_ov5670(sd);
2228        const struct ov5670_mode *mode;
2229        s32 vblank_def;
2230        s32 h_blank;
2231
2232        mutex_lock(&ov5670->mutex);
2233
2234        fmt->format.code = MEDIA_BUS_FMT_SGRBG10_1X10;
2235
2236        mode = v4l2_find_nearest_size(supported_modes,
2237                                      ARRAY_SIZE(supported_modes),
2238                                      width, height,
2239                                      fmt->format.width, fmt->format.height);
2240        ov5670_update_pad_format(mode, fmt);
2241        if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
2242                *v4l2_subdev_get_try_format(sd, sd_state, fmt->pad) = fmt->format;
2243        } else {
2244                ov5670->cur_mode = mode;
2245                __v4l2_ctrl_s_ctrl(ov5670->link_freq, mode->link_freq_index);
2246                __v4l2_ctrl_s_ctrl_int64(
2247                        ov5670->pixel_rate,
2248                        link_freq_configs[mode->link_freq_index].pixel_rate);
2249                /* Update limits and set FPS to default */
2250                vblank_def = ov5670->cur_mode->vts_def -
2251                             ov5670->cur_mode->height;
2252                __v4l2_ctrl_modify_range(
2253                        ov5670->vblank,
2254                        ov5670->cur_mode->vts_min - ov5670->cur_mode->height,
2255                        OV5670_VTS_MAX - ov5670->cur_mode->height, 1,
2256                        vblank_def);
2257                __v4l2_ctrl_s_ctrl(ov5670->vblank, vblank_def);
2258                h_blank = OV5670_FIXED_PPL - ov5670->cur_mode->width;
2259                __v4l2_ctrl_modify_range(ov5670->hblank, h_blank, h_blank, 1,
2260                                         h_blank);
2261        }
2262
2263        mutex_unlock(&ov5670->mutex);
2264
2265        return 0;
2266}
2267
2268static int ov5670_get_skip_frames(struct v4l2_subdev *sd, u32 *frames)
2269{
2270        *frames = OV5670_NUM_OF_SKIP_FRAMES;
2271
2272        return 0;
2273}
2274
2275/* Prepare streaming by writing default values and customized values */
2276static int ov5670_start_streaming(struct ov5670 *ov5670)
2277{
2278        struct i2c_client *client = v4l2_get_subdevdata(&ov5670->sd);
2279        const struct ov5670_reg_list *reg_list;
2280        int link_freq_index;
2281        int ret;
2282
2283        /* Get out of from software reset */
2284        ret = ov5670_write_reg(ov5670, OV5670_REG_SOFTWARE_RST,
2285                               OV5670_REG_VALUE_08BIT, OV5670_SOFTWARE_RST);
2286        if (ret) {
2287                dev_err(&client->dev, "%s failed to set powerup registers\n",
2288                        __func__);
2289                return ret;
2290        }
2291
2292        /* Setup PLL */
2293        link_freq_index = ov5670->cur_mode->link_freq_index;
2294        reg_list = &link_freq_configs[link_freq_index].reg_list;
2295        ret = ov5670_write_reg_list(ov5670, reg_list);
2296        if (ret) {
2297                dev_err(&client->dev, "%s failed to set plls\n", __func__);
2298                return ret;
2299        }
2300
2301        /* Apply default values of current mode */
2302        reg_list = &ov5670->cur_mode->reg_list;
2303        ret = ov5670_write_reg_list(ov5670, reg_list);
2304        if (ret) {
2305                dev_err(&client->dev, "%s failed to set mode\n", __func__);
2306                return ret;
2307        }
2308
2309        ret = __v4l2_ctrl_handler_setup(ov5670->sd.ctrl_handler);
2310        if (ret)
2311                return ret;
2312
2313        /* Write stream on list */
2314        ret = ov5670_write_reg(ov5670, OV5670_REG_MODE_SELECT,
2315                               OV5670_REG_VALUE_08BIT, OV5670_MODE_STREAMING);
2316        if (ret) {
2317                dev_err(&client->dev, "%s failed to set stream\n", __func__);
2318                return ret;
2319        }
2320
2321        return 0;
2322}
2323
2324static int ov5670_stop_streaming(struct ov5670 *ov5670)
2325{
2326        struct i2c_client *client = v4l2_get_subdevdata(&ov5670->sd);
2327        int ret;
2328
2329        ret = ov5670_write_reg(ov5670, OV5670_REG_MODE_SELECT,
2330                               OV5670_REG_VALUE_08BIT, OV5670_MODE_STANDBY);
2331        if (ret)
2332                dev_err(&client->dev, "%s failed to set stream\n", __func__);
2333
2334        /* Return success even if it was an error, as there is nothing the
2335         * caller can do about it.
2336         */
2337        return 0;
2338}
2339
2340static int ov5670_set_stream(struct v4l2_subdev *sd, int enable)
2341{
2342        struct ov5670 *ov5670 = to_ov5670(sd);
2343        struct i2c_client *client = v4l2_get_subdevdata(sd);
2344        int ret = 0;
2345
2346        mutex_lock(&ov5670->mutex);
2347        if (ov5670->streaming == enable)
2348                goto unlock_and_return;
2349
2350        if (enable) {
2351                ret = pm_runtime_resume_and_get(&client->dev);
2352                if (ret < 0)
2353                        goto unlock_and_return;
2354
2355                ret = ov5670_start_streaming(ov5670);
2356                if (ret)
2357                        goto error;
2358        } else {
2359                ret = ov5670_stop_streaming(ov5670);
2360                pm_runtime_put(&client->dev);
2361        }
2362        ov5670->streaming = enable;
2363        goto unlock_and_return;
2364
2365error:
2366        pm_runtime_put(&client->dev);
2367
2368unlock_and_return:
2369        mutex_unlock(&ov5670->mutex);
2370
2371        return ret;
2372}
2373
2374static int __maybe_unused ov5670_suspend(struct device *dev)
2375{
2376        struct v4l2_subdev *sd = dev_get_drvdata(dev);
2377        struct ov5670 *ov5670 = to_ov5670(sd);
2378
2379        if (ov5670->streaming)
2380                ov5670_stop_streaming(ov5670);
2381
2382        return 0;
2383}
2384
2385static int __maybe_unused ov5670_resume(struct device *dev)
2386{
2387        struct v4l2_subdev *sd = dev_get_drvdata(dev);
2388        struct ov5670 *ov5670 = to_ov5670(sd);
2389        int ret;
2390
2391        if (ov5670->streaming) {
2392                ret = ov5670_start_streaming(ov5670);
2393                if (ret) {
2394                        ov5670_stop_streaming(ov5670);
2395                        return ret;
2396                }
2397        }
2398
2399        return 0;
2400}
2401
2402/* Verify chip ID */
2403static int ov5670_identify_module(struct ov5670 *ov5670)
2404{
2405        struct i2c_client *client = v4l2_get_subdevdata(&ov5670->sd);
2406        int ret;
2407        u32 val;
2408
2409        ret = ov5670_read_reg(ov5670, OV5670_REG_CHIP_ID,
2410                              OV5670_REG_VALUE_24BIT, &val);
2411        if (ret)
2412                return ret;
2413
2414        if (val != OV5670_CHIP_ID) {
2415                dev_err(&client->dev, "chip id mismatch: %x!=%x\n",
2416                        OV5670_CHIP_ID, val);
2417                return -ENXIO;
2418        }
2419
2420        return 0;
2421}
2422
2423static const struct v4l2_subdev_video_ops ov5670_video_ops = {
2424        .s_stream = ov5670_set_stream,
2425};
2426
2427static const struct v4l2_subdev_pad_ops ov5670_pad_ops = {
2428        .enum_mbus_code = ov5670_enum_mbus_code,
2429        .get_fmt = ov5670_get_pad_format,
2430        .set_fmt = ov5670_set_pad_format,
2431        .enum_frame_size = ov5670_enum_frame_size,
2432};
2433
2434static const struct v4l2_subdev_sensor_ops ov5670_sensor_ops = {
2435        .g_skip_frames = ov5670_get_skip_frames,
2436};
2437
2438static const struct v4l2_subdev_ops ov5670_subdev_ops = {
2439        .video = &ov5670_video_ops,
2440        .pad = &ov5670_pad_ops,
2441        .sensor = &ov5670_sensor_ops,
2442};
2443
2444static const struct media_entity_operations ov5670_subdev_entity_ops = {
2445        .link_validate = v4l2_subdev_link_validate,
2446};
2447
2448static const struct v4l2_subdev_internal_ops ov5670_internal_ops = {
2449        .open = ov5670_open,
2450};
2451
2452static int ov5670_probe(struct i2c_client *client)
2453{
2454        struct ov5670 *ov5670;
2455        const char *err_msg;
2456        u32 input_clk = 0;
2457        int ret;
2458
2459        device_property_read_u32(&client->dev, "clock-frequency", &input_clk);
2460        if (input_clk != 19200000)
2461                return -EINVAL;
2462
2463        ov5670 = devm_kzalloc(&client->dev, sizeof(*ov5670), GFP_KERNEL);
2464        if (!ov5670) {
2465                ret = -ENOMEM;
2466                err_msg = "devm_kzalloc() error";
2467                goto error_print;
2468        }
2469
2470        /* Initialize subdev */
2471        v4l2_i2c_subdev_init(&ov5670->sd, client, &ov5670_subdev_ops);
2472
2473        /* Check module identity */
2474        ret = ov5670_identify_module(ov5670);
2475        if (ret) {
2476                err_msg = "ov5670_identify_module() error";
2477                goto error_print;
2478        }
2479
2480        mutex_init(&ov5670->mutex);
2481
2482        /* Set default mode to max resolution */
2483        ov5670->cur_mode = &supported_modes[0];
2484
2485        ret = ov5670_init_controls(ov5670);
2486        if (ret) {
2487                err_msg = "ov5670_init_controls() error";
2488                goto error_mutex_destroy;
2489        }
2490
2491        ov5670->sd.internal_ops = &ov5670_internal_ops;
2492        ov5670->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
2493        ov5670->sd.entity.ops = &ov5670_subdev_entity_ops;
2494        ov5670->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
2495
2496        /* Source pad initialization */
2497        ov5670->pad.flags = MEDIA_PAD_FL_SOURCE;
2498        ret = media_entity_pads_init(&ov5670->sd.entity, 1, &ov5670->pad);
2499        if (ret) {
2500                err_msg = "media_entity_pads_init() error";
2501                goto error_handler_free;
2502        }
2503
2504        /* Async register for subdev */
2505        ret = v4l2_async_register_subdev_sensor(&ov5670->sd);
2506        if (ret < 0) {
2507                err_msg = "v4l2_async_register_subdev() error";
2508                goto error_entity_cleanup;
2509        }
2510
2511        ov5670->streaming = false;
2512
2513        /*
2514         * Device is already turned on by i2c-core with ACPI domain PM.
2515         * Enable runtime PM and turn off the device.
2516         */
2517        pm_runtime_set_active(&client->dev);
2518        pm_runtime_enable(&client->dev);
2519        pm_runtime_idle(&client->dev);
2520
2521        return 0;
2522
2523error_entity_cleanup:
2524        media_entity_cleanup(&ov5670->sd.entity);
2525
2526error_handler_free:
2527        v4l2_ctrl_handler_free(ov5670->sd.ctrl_handler);
2528
2529error_mutex_destroy:
2530        mutex_destroy(&ov5670->mutex);
2531
2532error_print:
2533        dev_err(&client->dev, "%s: %s %d\n", __func__, err_msg, ret);
2534
2535        return ret;
2536}
2537
2538static int ov5670_remove(struct i2c_client *client)
2539{
2540        struct v4l2_subdev *sd = i2c_get_clientdata(client);
2541        struct ov5670 *ov5670 = to_ov5670(sd);
2542
2543        v4l2_async_unregister_subdev(sd);
2544        media_entity_cleanup(&sd->entity);
2545        v4l2_ctrl_handler_free(sd->ctrl_handler);
2546        mutex_destroy(&ov5670->mutex);
2547
2548        pm_runtime_disable(&client->dev);
2549
2550        return 0;
2551}
2552
2553static const struct dev_pm_ops ov5670_pm_ops = {
2554        SET_SYSTEM_SLEEP_PM_OPS(ov5670_suspend, ov5670_resume)
2555};
2556
2557#ifdef CONFIG_ACPI
2558static const struct acpi_device_id ov5670_acpi_ids[] = {
2559        {"INT3479"},
2560        { /* sentinel */ }
2561};
2562
2563MODULE_DEVICE_TABLE(acpi, ov5670_acpi_ids);
2564#endif
2565
2566static struct i2c_driver ov5670_i2c_driver = {
2567        .driver = {
2568                .name = "ov5670",
2569                .pm = &ov5670_pm_ops,
2570                .acpi_match_table = ACPI_PTR(ov5670_acpi_ids),
2571        },
2572        .probe_new = ov5670_probe,
2573        .remove = ov5670_remove,
2574};
2575
2576module_i2c_driver(ov5670_i2c_driver);
2577
2578MODULE_AUTHOR("Rapolu, Chiranjeevi <chiranjeevi.rapolu@intel.com>");
2579MODULE_AUTHOR("Yang, Hyungwoo <hyungwoo.yang@intel.com>");
2580MODULE_DESCRIPTION("Omnivision ov5670 sensor driver");
2581MODULE_LICENSE("GPL v2");
2582