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