root/drivers/staging/media/soc_camera/soc_ov5642.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. to_ov5642
  2. ov5642_find_datafmt
  3. reg_read
  4. reg_write
  5. reg_write16
  6. ov5642_get_register
  7. ov5642_set_register
  8. ov5642_write_array
  9. ov5642_set_resolution
  10. ov5642_set_fmt
  11. ov5642_get_fmt
  12. ov5642_enum_mbus_code
  13. ov5642_set_selection
  14. ov5642_get_selection
  15. ov5642_g_mbus_config
  16. ov5642_s_power
  17. ov5642_video_probe
  18. ov5642_probe
  19. ov5642_remove

   1 // SPDX-License-Identifier: GPL-2.0
   2 /*
   3  * Driver for OV5642 CMOS Image Sensor from Omnivision
   4  *
   5  * Copyright (C) 2011, Bastian Hecht <hechtb@gmail.com>
   6  *
   7  * Based on Sony IMX074 Camera Driver
   8  * Copyright (C) 2010, Guennadi Liakhovetski <g.liakhovetski@gmx.de>
   9  *
  10  * Based on Omnivision OV7670 Camera Driver
  11  * Copyright (C) 2006-7 Jonathan Corbet <corbet@lwn.net>
  12  */
  13 #include <linux/bitops.h>
  14 #include <linux/delay.h>
  15 #include <linux/i2c.h>
  16 #include <linux/kernel.h>
  17 #include <linux/slab.h>
  18 #include <linux/videodev2.h>
  19 #include <linux/module.h>
  20 #include <linux/v4l2-mediabus.h>
  21 
  22 #include <media/soc_camera.h>
  23 #include <media/v4l2-clk.h>
  24 #include <media/v4l2-subdev.h>
  25 
  26 /* OV5642 registers */
  27 #define REG_CHIP_ID_HIGH                0x300a
  28 #define REG_CHIP_ID_LOW                 0x300b
  29 
  30 #define REG_WINDOW_START_X_HIGH         0x3800
  31 #define REG_WINDOW_START_X_LOW          0x3801
  32 #define REG_WINDOW_START_Y_HIGH         0x3802
  33 #define REG_WINDOW_START_Y_LOW          0x3803
  34 #define REG_WINDOW_WIDTH_HIGH           0x3804
  35 #define REG_WINDOW_WIDTH_LOW            0x3805
  36 #define REG_WINDOW_HEIGHT_HIGH          0x3806
  37 #define REG_WINDOW_HEIGHT_LOW           0x3807
  38 #define REG_OUT_WIDTH_HIGH              0x3808
  39 #define REG_OUT_WIDTH_LOW               0x3809
  40 #define REG_OUT_HEIGHT_HIGH             0x380a
  41 #define REG_OUT_HEIGHT_LOW              0x380b
  42 #define REG_OUT_TOTAL_WIDTH_HIGH        0x380c
  43 #define REG_OUT_TOTAL_WIDTH_LOW         0x380d
  44 #define REG_OUT_TOTAL_HEIGHT_HIGH       0x380e
  45 #define REG_OUT_TOTAL_HEIGHT_LOW        0x380f
  46 #define REG_OUTPUT_FORMAT               0x4300
  47 #define REG_ISP_CTRL_01                 0x5001
  48 #define REG_AVG_WINDOW_END_X_HIGH       0x5682
  49 #define REG_AVG_WINDOW_END_X_LOW        0x5683
  50 #define REG_AVG_WINDOW_END_Y_HIGH       0x5686
  51 #define REG_AVG_WINDOW_END_Y_LOW        0x5687
  52 
  53 /* active pixel array size */
  54 #define OV5642_SENSOR_SIZE_X    2592
  55 #define OV5642_SENSOR_SIZE_Y    1944
  56 
  57 /*
  58  * About OV5642 resolution, cropping and binning:
  59  * This sensor supports it all, at least in the feature description.
  60  * Unfortunately, no combination of appropriate registers settings could make
  61  * the chip work the intended way. As it works with predefined register lists,
  62  * some undocumented registers are presumably changed there to achieve their
  63  * goals.
  64  * This driver currently only works for resolutions up to 720 lines with a
  65  * 1:1 scale. Hopefully these restrictions will be removed in the future.
  66  */
  67 #define OV5642_MAX_WIDTH        OV5642_SENSOR_SIZE_X
  68 #define OV5642_MAX_HEIGHT       720
  69 
  70 /* default sizes */
  71 #define OV5642_DEFAULT_WIDTH    1280
  72 #define OV5642_DEFAULT_HEIGHT   OV5642_MAX_HEIGHT
  73 
  74 /* minimum extra blanking */
  75 #define BLANKING_EXTRA_WIDTH            500
  76 #define BLANKING_EXTRA_HEIGHT           20
  77 
  78 /*
  79  * the sensor's autoexposure is buggy when setting total_height low.
  80  * It tries to expose longer than 1 frame period without taking care of it
  81  * and this leads to weird output. So we set 1000 lines as minimum.
  82  */
  83 #define BLANKING_MIN_HEIGHT             1000
  84 
  85 struct regval_list {
  86         u16 reg_num;
  87         u8 value;
  88 };
  89 
  90 static struct regval_list ov5642_default_regs_init[] = {
  91         { 0x3103, 0x93 },
  92         { 0x3008, 0x82 },
  93         { 0x3017, 0x7f },
  94         { 0x3018, 0xfc },
  95         { 0x3810, 0xc2 },
  96         { 0x3615, 0xf0 },
  97         { 0x3000, 0x0  },
  98         { 0x3001, 0x0  },
  99         { 0x3002, 0x0  },
 100         { 0x3003, 0x0  },
 101         { 0x3004, 0xff },
 102         { 0x3030, 0x2b },
 103         { 0x3011, 0x8  },
 104         { 0x3010, 0x10 },
 105         { 0x3604, 0x60 },
 106         { 0x3622, 0x60 },
 107         { 0x3621, 0x9  },
 108         { 0x3709, 0x0  },
 109         { 0x4000, 0x21 },
 110         { 0x401d, 0x22 },
 111         { 0x3600, 0x54 },
 112         { 0x3605, 0x4  },
 113         { 0x3606, 0x3f },
 114         { 0x3c01, 0x80 },
 115         { 0x300d, 0x22 },
 116         { 0x3623, 0x22 },
 117         { 0x5000, 0x4f },
 118         { 0x5020, 0x4  },
 119         { 0x5181, 0x79 },
 120         { 0x5182, 0x0  },
 121         { 0x5185, 0x22 },
 122         { 0x5197, 0x1  },
 123         { 0x5500, 0xa  },
 124         { 0x5504, 0x0  },
 125         { 0x5505, 0x7f },
 126         { 0x5080, 0x8  },
 127         { 0x300e, 0x18 },
 128         { 0x4610, 0x0  },
 129         { 0x471d, 0x5  },
 130         { 0x4708, 0x6  },
 131         { 0x370c, 0xa0 },
 132         { 0x5687, 0x94 },
 133         { 0x501f, 0x0  },
 134         { 0x5000, 0x4f },
 135         { 0x5001, 0xcf },
 136         { 0x4300, 0x30 },
 137         { 0x4300, 0x30 },
 138         { 0x460b, 0x35 },
 139         { 0x471d, 0x0  },
 140         { 0x3002, 0xc  },
 141         { 0x3002, 0x0  },
 142         { 0x4713, 0x3  },
 143         { 0x471c, 0x50 },
 144         { 0x4721, 0x2  },
 145         { 0x4402, 0x90 },
 146         { 0x460c, 0x22 },
 147         { 0x3815, 0x44 },
 148         { 0x3503, 0x7  },
 149         { 0x3501, 0x73 },
 150         { 0x3502, 0x80 },
 151         { 0x350b, 0x0  },
 152         { 0x3818, 0xc8 },
 153         { 0x3824, 0x11 },
 154         { 0x3a00, 0x78 },
 155         { 0x3a1a, 0x4  },
 156         { 0x3a13, 0x30 },
 157         { 0x3a18, 0x0  },
 158         { 0x3a19, 0x7c },
 159         { 0x3a08, 0x12 },
 160         { 0x3a09, 0xc0 },
 161         { 0x3a0a, 0xf  },
 162         { 0x3a0b, 0xa0 },
 163         { 0x350c, 0x7  },
 164         { 0x350d, 0xd0 },
 165         { 0x3a0d, 0x8  },
 166         { 0x3a0e, 0x6  },
 167         { 0x3500, 0x0  },
 168         { 0x3501, 0x0  },
 169         { 0x3502, 0x0  },
 170         { 0x350a, 0x0  },
 171         { 0x350b, 0x0  },
 172         { 0x3503, 0x0  },
 173         { 0x3a0f, 0x3c },
 174         { 0x3a10, 0x32 },
 175         { 0x3a1b, 0x3c },
 176         { 0x3a1e, 0x32 },
 177         { 0x3a11, 0x80 },
 178         { 0x3a1f, 0x20 },
 179         { 0x3030, 0x2b },
 180         { 0x3a02, 0x0  },
 181         { 0x3a03, 0x7d },
 182         { 0x3a04, 0x0  },
 183         { 0x3a14, 0x0  },
 184         { 0x3a15, 0x7d },
 185         { 0x3a16, 0x0  },
 186         { 0x3a00, 0x78 },
 187         { 0x3a08, 0x9  },
 188         { 0x3a09, 0x60 },
 189         { 0x3a0a, 0x7  },
 190         { 0x3a0b, 0xd0 },
 191         { 0x3a0d, 0x10 },
 192         { 0x3a0e, 0xd  },
 193         { 0x4407, 0x4  },
 194         { 0x5193, 0x70 },
 195         { 0x589b, 0x0  },
 196         { 0x589a, 0xc0 },
 197         { 0x401e, 0x20 },
 198         { 0x4001, 0x42 },
 199         { 0x401c, 0x6  },
 200         { 0x3825, 0xac },
 201         { 0x3827, 0xc  },
 202         { 0x528a, 0x1  },
 203         { 0x528b, 0x4  },
 204         { 0x528c, 0x8  },
 205         { 0x528d, 0x10 },
 206         { 0x528e, 0x20 },
 207         { 0x528f, 0x28 },
 208         { 0x5290, 0x30 },
 209         { 0x5292, 0x0  },
 210         { 0x5293, 0x1  },
 211         { 0x5294, 0x0  },
 212         { 0x5295, 0x4  },
 213         { 0x5296, 0x0  },
 214         { 0x5297, 0x8  },
 215         { 0x5298, 0x0  },
 216         { 0x5299, 0x10 },
 217         { 0x529a, 0x0  },
 218         { 0x529b, 0x20 },
 219         { 0x529c, 0x0  },
 220         { 0x529d, 0x28 },
 221         { 0x529e, 0x0  },
 222         { 0x529f, 0x30 },
 223         { 0x5282, 0x0  },
 224         { 0x5300, 0x0  },
 225         { 0x5301, 0x20 },
 226         { 0x5302, 0x0  },
 227         { 0x5303, 0x7c },
 228         { 0x530c, 0x0  },
 229         { 0x530d, 0xc  },
 230         { 0x530e, 0x20 },
 231         { 0x530f, 0x80 },
 232         { 0x5310, 0x20 },
 233         { 0x5311, 0x80 },
 234         { 0x5308, 0x20 },
 235         { 0x5309, 0x40 },
 236         { 0x5304, 0x0  },
 237         { 0x5305, 0x30 },
 238         { 0x5306, 0x0  },
 239         { 0x5307, 0x80 },
 240         { 0x5314, 0x8  },
 241         { 0x5315, 0x20 },
 242         { 0x5319, 0x30 },
 243         { 0x5316, 0x10 },
 244         { 0x5317, 0x0  },
 245         { 0x5318, 0x2  },
 246         { 0x5380, 0x1  },
 247         { 0x5381, 0x0  },
 248         { 0x5382, 0x0  },
 249         { 0x5383, 0x4e },
 250         { 0x5384, 0x0  },
 251         { 0x5385, 0xf  },
 252         { 0x5386, 0x0  },
 253         { 0x5387, 0x0  },
 254         { 0x5388, 0x1  },
 255         { 0x5389, 0x15 },
 256         { 0x538a, 0x0  },
 257         { 0x538b, 0x31 },
 258         { 0x538c, 0x0  },
 259         { 0x538d, 0x0  },
 260         { 0x538e, 0x0  },
 261         { 0x538f, 0xf  },
 262         { 0x5390, 0x0  },
 263         { 0x5391, 0xab },
 264         { 0x5392, 0x0  },
 265         { 0x5393, 0xa2 },
 266         { 0x5394, 0x8  },
 267         { 0x5480, 0x14 },
 268         { 0x5481, 0x21 },
 269         { 0x5482, 0x36 },
 270         { 0x5483, 0x57 },
 271         { 0x5484, 0x65 },
 272         { 0x5485, 0x71 },
 273         { 0x5486, 0x7d },
 274         { 0x5487, 0x87 },
 275         { 0x5488, 0x91 },
 276         { 0x5489, 0x9a },
 277         { 0x548a, 0xaa },
 278         { 0x548b, 0xb8 },
 279         { 0x548c, 0xcd },
 280         { 0x548d, 0xdd },
 281         { 0x548e, 0xea },
 282         { 0x548f, 0x1d },
 283         { 0x5490, 0x5  },
 284         { 0x5491, 0x0  },
 285         { 0x5492, 0x4  },
 286         { 0x5493, 0x20 },
 287         { 0x5494, 0x3  },
 288         { 0x5495, 0x60 },
 289         { 0x5496, 0x2  },
 290         { 0x5497, 0xb8 },
 291         { 0x5498, 0x2  },
 292         { 0x5499, 0x86 },
 293         { 0x549a, 0x2  },
 294         { 0x549b, 0x5b },
 295         { 0x549c, 0x2  },
 296         { 0x549d, 0x3b },
 297         { 0x549e, 0x2  },
 298         { 0x549f, 0x1c },
 299         { 0x54a0, 0x2  },
 300         { 0x54a1, 0x4  },
 301         { 0x54a2, 0x1  },
 302         { 0x54a3, 0xed },
 303         { 0x54a4, 0x1  },
 304         { 0x54a5, 0xc5 },
 305         { 0x54a6, 0x1  },
 306         { 0x54a7, 0xa5 },
 307         { 0x54a8, 0x1  },
 308         { 0x54a9, 0x6c },
 309         { 0x54aa, 0x1  },
 310         { 0x54ab, 0x41 },
 311         { 0x54ac, 0x1  },
 312         { 0x54ad, 0x20 },
 313         { 0x54ae, 0x0  },
 314         { 0x54af, 0x16 },
 315         { 0x54b0, 0x1  },
 316         { 0x54b1, 0x20 },
 317         { 0x54b2, 0x0  },
 318         { 0x54b3, 0x10 },
 319         { 0x54b4, 0x0  },
 320         { 0x54b5, 0xf0 },
 321         { 0x54b6, 0x0  },
 322         { 0x54b7, 0xdf },
 323         { 0x5402, 0x3f },
 324         { 0x5403, 0x0  },
 325         { 0x3406, 0x0  },
 326         { 0x5180, 0xff },
 327         { 0x5181, 0x52 },
 328         { 0x5182, 0x11 },
 329         { 0x5183, 0x14 },
 330         { 0x5184, 0x25 },
 331         { 0x5185, 0x24 },
 332         { 0x5186, 0x6  },
 333         { 0x5187, 0x8  },
 334         { 0x5188, 0x8  },
 335         { 0x5189, 0x7c },
 336         { 0x518a, 0x60 },
 337         { 0x518b, 0xb2 },
 338         { 0x518c, 0xb2 },
 339         { 0x518d, 0x44 },
 340         { 0x518e, 0x3d },
 341         { 0x518f, 0x58 },
 342         { 0x5190, 0x46 },
 343         { 0x5191, 0xf8 },
 344         { 0x5192, 0x4  },
 345         { 0x5193, 0x70 },
 346         { 0x5194, 0xf0 },
 347         { 0x5195, 0xf0 },
 348         { 0x5196, 0x3  },
 349         { 0x5197, 0x1  },
 350         { 0x5198, 0x4  },
 351         { 0x5199, 0x12 },
 352         { 0x519a, 0x4  },
 353         { 0x519b, 0x0  },
 354         { 0x519c, 0x6  },
 355         { 0x519d, 0x82 },
 356         { 0x519e, 0x0  },
 357         { 0x5025, 0x80 },
 358         { 0x3a0f, 0x38 },
 359         { 0x3a10, 0x30 },
 360         { 0x3a1b, 0x3a },
 361         { 0x3a1e, 0x2e },
 362         { 0x3a11, 0x60 },
 363         { 0x3a1f, 0x10 },
 364         { 0x5688, 0xa6 },
 365         { 0x5689, 0x6a },
 366         { 0x568a, 0xea },
 367         { 0x568b, 0xae },
 368         { 0x568c, 0xa6 },
 369         { 0x568d, 0x6a },
 370         { 0x568e, 0x62 },
 371         { 0x568f, 0x26 },
 372         { 0x5583, 0x40 },
 373         { 0x5584, 0x40 },
 374         { 0x5580, 0x2  },
 375         { 0x5000, 0xcf },
 376         { 0x5800, 0x27 },
 377         { 0x5801, 0x19 },
 378         { 0x5802, 0x12 },
 379         { 0x5803, 0xf  },
 380         { 0x5804, 0x10 },
 381         { 0x5805, 0x15 },
 382         { 0x5806, 0x1e },
 383         { 0x5807, 0x2f },
 384         { 0x5808, 0x15 },
 385         { 0x5809, 0xd  },
 386         { 0x580a, 0xa  },
 387         { 0x580b, 0x9  },
 388         { 0x580c, 0xa  },
 389         { 0x580d, 0xc  },
 390         { 0x580e, 0x12 },
 391         { 0x580f, 0x19 },
 392         { 0x5810, 0xb  },
 393         { 0x5811, 0x7  },
 394         { 0x5812, 0x4  },
 395         { 0x5813, 0x3  },
 396         { 0x5814, 0x3  },
 397         { 0x5815, 0x6  },
 398         { 0x5816, 0xa  },
 399         { 0x5817, 0xf  },
 400         { 0x5818, 0xa  },
 401         { 0x5819, 0x5  },
 402         { 0x581a, 0x1  },
 403         { 0x581b, 0x0  },
 404         { 0x581c, 0x0  },
 405         { 0x581d, 0x3  },
 406         { 0x581e, 0x8  },
 407         { 0x581f, 0xc  },
 408         { 0x5820, 0xa  },
 409         { 0x5821, 0x5  },
 410         { 0x5822, 0x1  },
 411         { 0x5823, 0x0  },
 412         { 0x5824, 0x0  },
 413         { 0x5825, 0x3  },
 414         { 0x5826, 0x8  },
 415         { 0x5827, 0xc  },
 416         { 0x5828, 0xe  },
 417         { 0x5829, 0x8  },
 418         { 0x582a, 0x6  },
 419         { 0x582b, 0x4  },
 420         { 0x582c, 0x5  },
 421         { 0x582d, 0x7  },
 422         { 0x582e, 0xb  },
 423         { 0x582f, 0x12 },
 424         { 0x5830, 0x18 },
 425         { 0x5831, 0x10 },
 426         { 0x5832, 0xc  },
 427         { 0x5833, 0xa  },
 428         { 0x5834, 0xb  },
 429         { 0x5835, 0xe  },
 430         { 0x5836, 0x15 },
 431         { 0x5837, 0x19 },
 432         { 0x5838, 0x32 },
 433         { 0x5839, 0x1f },
 434         { 0x583a, 0x18 },
 435         { 0x583b, 0x16 },
 436         { 0x583c, 0x17 },
 437         { 0x583d, 0x1e },
 438         { 0x583e, 0x26 },
 439         { 0x583f, 0x53 },
 440         { 0x5840, 0x10 },
 441         { 0x5841, 0xf  },
 442         { 0x5842, 0xd  },
 443         { 0x5843, 0xc  },
 444         { 0x5844, 0xe  },
 445         { 0x5845, 0x9  },
 446         { 0x5846, 0x11 },
 447         { 0x5847, 0x10 },
 448         { 0x5848, 0x10 },
 449         { 0x5849, 0x10 },
 450         { 0x584a, 0x10 },
 451         { 0x584b, 0xe  },
 452         { 0x584c, 0x10 },
 453         { 0x584d, 0x10 },
 454         { 0x584e, 0x11 },
 455         { 0x584f, 0x10 },
 456         { 0x5850, 0xf  },
 457         { 0x5851, 0xc  },
 458         { 0x5852, 0xf  },
 459         { 0x5853, 0x10 },
 460         { 0x5854, 0x10 },
 461         { 0x5855, 0xf  },
 462         { 0x5856, 0xe  },
 463         { 0x5857, 0xb  },
 464         { 0x5858, 0x10 },
 465         { 0x5859, 0xd  },
 466         { 0x585a, 0xd  },
 467         { 0x585b, 0xc  },
 468         { 0x585c, 0xc  },
 469         { 0x585d, 0xc  },
 470         { 0x585e, 0xb  },
 471         { 0x585f, 0xc  },
 472         { 0x5860, 0xc  },
 473         { 0x5861, 0xc  },
 474         { 0x5862, 0xd  },
 475         { 0x5863, 0x8  },
 476         { 0x5864, 0x11 },
 477         { 0x5865, 0x18 },
 478         { 0x5866, 0x18 },
 479         { 0x5867, 0x19 },
 480         { 0x5868, 0x17 },
 481         { 0x5869, 0x19 },
 482         { 0x586a, 0x16 },
 483         { 0x586b, 0x13 },
 484         { 0x586c, 0x13 },
 485         { 0x586d, 0x12 },
 486         { 0x586e, 0x13 },
 487         { 0x586f, 0x16 },
 488         { 0x5870, 0x14 },
 489         { 0x5871, 0x12 },
 490         { 0x5872, 0x10 },
 491         { 0x5873, 0x11 },
 492         { 0x5874, 0x11 },
 493         { 0x5875, 0x16 },
 494         { 0x5876, 0x14 },
 495         { 0x5877, 0x11 },
 496         { 0x5878, 0x10 },
 497         { 0x5879, 0xf  },
 498         { 0x587a, 0x10 },
 499         { 0x587b, 0x14 },
 500         { 0x587c, 0x13 },
 501         { 0x587d, 0x12 },
 502         { 0x587e, 0x11 },
 503         { 0x587f, 0x11 },
 504         { 0x5880, 0x12 },
 505         { 0x5881, 0x15 },
 506         { 0x5882, 0x14 },
 507         { 0x5883, 0x15 },
 508         { 0x5884, 0x15 },
 509         { 0x5885, 0x15 },
 510         { 0x5886, 0x13 },
 511         { 0x5887, 0x17 },
 512         { 0x3710, 0x10 },
 513         { 0x3632, 0x51 },
 514         { 0x3702, 0x10 },
 515         { 0x3703, 0xb2 },
 516         { 0x3704, 0x18 },
 517         { 0x370b, 0x40 },
 518         { 0x370d, 0x3  },
 519         { 0x3631, 0x1  },
 520         { 0x3632, 0x52 },
 521         { 0x3606, 0x24 },
 522         { 0x3620, 0x96 },
 523         { 0x5785, 0x7  },
 524         { 0x3a13, 0x30 },
 525         { 0x3600, 0x52 },
 526         { 0x3604, 0x48 },
 527         { 0x3606, 0x1b },
 528         { 0x370d, 0xb  },
 529         { 0x370f, 0xc0 },
 530         { 0x3709, 0x1  },
 531         { 0x3823, 0x0  },
 532         { 0x5007, 0x0  },
 533         { 0x5009, 0x0  },
 534         { 0x5011, 0x0  },
 535         { 0x5013, 0x0  },
 536         { 0x519e, 0x0  },
 537         { 0x5086, 0x0  },
 538         { 0x5087, 0x0  },
 539         { 0x5088, 0x0  },
 540         { 0x5089, 0x0  },
 541         { 0x302b, 0x0  },
 542         { 0x3503, 0x7  },
 543         { 0x3011, 0x8  },
 544         { 0x350c, 0x2  },
 545         { 0x350d, 0xe4 },
 546         { 0x3621, 0xc9 },
 547         { 0x370a, 0x81 },
 548         { 0xffff, 0xff },
 549 };
 550 
 551 static struct regval_list ov5642_default_regs_finalise[] = {
 552         { 0x3810, 0xc2 },
 553         { 0x3818, 0xc9 },
 554         { 0x381c, 0x10 },
 555         { 0x381d, 0xa0 },
 556         { 0x381e, 0x5  },
 557         { 0x381f, 0xb0 },
 558         { 0x3820, 0x0  },
 559         { 0x3821, 0x0  },
 560         { 0x3824, 0x11 },
 561         { 0x3a08, 0x1b },
 562         { 0x3a09, 0xc0 },
 563         { 0x3a0a, 0x17 },
 564         { 0x3a0b, 0x20 },
 565         { 0x3a0d, 0x2  },
 566         { 0x3a0e, 0x1  },
 567         { 0x401c, 0x4  },
 568         { 0x5682, 0x5  },
 569         { 0x5683, 0x0  },
 570         { 0x5686, 0x2  },
 571         { 0x5687, 0xcc },
 572         { 0x5001, 0x4f },
 573         { 0x589b, 0x6  },
 574         { 0x589a, 0xc5 },
 575         { 0x3503, 0x0  },
 576         { 0x460c, 0x20 },
 577         { 0x460b, 0x37 },
 578         { 0x471c, 0xd0 },
 579         { 0x471d, 0x5  },
 580         { 0x3815, 0x1  },
 581         { 0x3818, 0xc1 },
 582         { 0x501f, 0x0  },
 583         { 0x5002, 0xe0 },
 584         { 0x4300, 0x32 }, /* UYVY */
 585         { 0x3002, 0x1c },
 586         { 0x4800, 0x14 },
 587         { 0x4801, 0xf  },
 588         { 0x3007, 0x3b },
 589         { 0x300e, 0x4  },
 590         { 0x4803, 0x50 },
 591         { 0x3815, 0x1  },
 592         { 0x4713, 0x2  },
 593         { 0x4842, 0x1  },
 594         { 0x300f, 0xe  },
 595         { 0x3003, 0x3  },
 596         { 0x3003, 0x1  },
 597         { 0xffff, 0xff },
 598 };
 599 
 600 struct ov5642_datafmt {
 601         u32     code;
 602         enum v4l2_colorspace            colorspace;
 603 };
 604 
 605 struct ov5642 {
 606         struct v4l2_subdev              subdev;
 607         const struct ov5642_datafmt     *fmt;
 608         struct v4l2_rect                crop_rect;
 609         struct v4l2_clk                 *clk;
 610 
 611         /* blanking information */
 612         int total_width;
 613         int total_height;
 614 };
 615 
 616 static const struct ov5642_datafmt ov5642_colour_fmts[] = {
 617         {MEDIA_BUS_FMT_UYVY8_2X8, V4L2_COLORSPACE_JPEG},
 618 };
 619 
 620 static struct ov5642 *to_ov5642(const struct i2c_client *client)
 621 {
 622         return container_of(i2c_get_clientdata(client), struct ov5642, subdev);
 623 }
 624 
 625 /* Find a data format by a pixel code in an array */
 626 static const struct ov5642_datafmt
 627                         *ov5642_find_datafmt(u32 code)
 628 {
 629         int i;
 630 
 631         for (i = 0; i < ARRAY_SIZE(ov5642_colour_fmts); i++)
 632                 if (ov5642_colour_fmts[i].code == code)
 633                         return ov5642_colour_fmts + i;
 634 
 635         return NULL;
 636 }
 637 
 638 static int reg_read(struct i2c_client *client, u16 reg, u8 *val)
 639 {
 640         int ret;
 641         /* We have 16-bit i2c addresses - care for endianness */
 642         unsigned char data[2] = { reg >> 8, reg & 0xff };
 643 
 644         ret = i2c_master_send(client, data, 2);
 645         if (ret < 2) {
 646                 dev_err(&client->dev, "%s: i2c read error, reg: %x\n",
 647                         __func__, reg);
 648                 return ret < 0 ? ret : -EIO;
 649         }
 650 
 651         ret = i2c_master_recv(client, val, 1);
 652         if (ret < 1) {
 653                 dev_err(&client->dev, "%s: i2c read error, reg: %x\n",
 654                                 __func__, reg);
 655                 return ret < 0 ? ret : -EIO;
 656         }
 657         return 0;
 658 }
 659 
 660 static int reg_write(struct i2c_client *client, u16 reg, u8 val)
 661 {
 662         int ret;
 663         unsigned char data[3] = { reg >> 8, reg & 0xff, val };
 664 
 665         ret = i2c_master_send(client, data, 3);
 666         if (ret < 3) {
 667                 dev_err(&client->dev, "%s: i2c write error, reg: %x\n",
 668                         __func__, reg);
 669                 return ret < 0 ? ret : -EIO;
 670         }
 671 
 672         return 0;
 673 }
 674 
 675 /*
 676  * convenience function to write 16 bit register values that are split up
 677  * into two consecutive high and low parts
 678  */
 679 static int reg_write16(struct i2c_client *client, u16 reg, u16 val16)
 680 {
 681         int ret;
 682 
 683         ret = reg_write(client, reg, val16 >> 8);
 684         if (ret)
 685                 return ret;
 686         return reg_write(client, reg + 1, val16 & 0x00ff);
 687 }
 688 
 689 #ifdef CONFIG_VIDEO_ADV_DEBUG
 690 static int ov5642_get_register(struct v4l2_subdev *sd,
 691                                struct v4l2_dbg_register *reg)
 692 {
 693         struct i2c_client *client = v4l2_get_subdevdata(sd);
 694         int ret;
 695         u8 val;
 696 
 697         if (reg->reg & ~0xffff)
 698                 return -EINVAL;
 699 
 700         reg->size = 1;
 701 
 702         ret = reg_read(client, reg->reg, &val);
 703         if (!ret)
 704                 reg->val = (__u64)val;
 705 
 706         return ret;
 707 }
 708 
 709 static int ov5642_set_register(struct v4l2_subdev *sd,
 710                                const struct v4l2_dbg_register *reg)
 711 {
 712         struct i2c_client *client = v4l2_get_subdevdata(sd);
 713 
 714         if (reg->reg & ~0xffff || reg->val & ~0xff)
 715                 return -EINVAL;
 716 
 717         return reg_write(client, reg->reg, reg->val);
 718 }
 719 #endif
 720 
 721 static int ov5642_write_array(struct i2c_client *client,
 722                                 struct regval_list *vals)
 723 {
 724         while (vals->reg_num != 0xffff || vals->value != 0xff) {
 725                 int ret = reg_write(client, vals->reg_num, vals->value);
 726                 if (ret < 0)
 727                         return ret;
 728                 vals++;
 729         }
 730         dev_dbg(&client->dev, "Register list loaded\n");
 731         return 0;
 732 }
 733 
 734 static int ov5642_set_resolution(struct v4l2_subdev *sd)
 735 {
 736         struct i2c_client *client = v4l2_get_subdevdata(sd);
 737         struct ov5642 *priv = to_ov5642(client);
 738         int width = priv->crop_rect.width;
 739         int height = priv->crop_rect.height;
 740         int total_width = priv->total_width;
 741         int total_height = priv->total_height;
 742         int start_x = (OV5642_SENSOR_SIZE_X - width) / 2;
 743         int start_y = (OV5642_SENSOR_SIZE_Y - height) / 2;
 744         int ret;
 745 
 746         /*
 747          * This should set the starting point for cropping.
 748          * Doesn't work so far.
 749          */
 750         ret = reg_write16(client, REG_WINDOW_START_X_HIGH, start_x);
 751         if (!ret)
 752                 ret = reg_write16(client, REG_WINDOW_START_Y_HIGH, start_y);
 753         if (!ret) {
 754                 priv->crop_rect.left = start_x;
 755                 priv->crop_rect.top = start_y;
 756         }
 757 
 758         if (!ret)
 759                 ret = reg_write16(client, REG_WINDOW_WIDTH_HIGH, width);
 760         if (!ret)
 761                 ret = reg_write16(client, REG_WINDOW_HEIGHT_HIGH, height);
 762         if (ret)
 763                 return ret;
 764         priv->crop_rect.width = width;
 765         priv->crop_rect.height = height;
 766 
 767         /* Set the output window size. Only 1:1 scale is supported so far. */
 768         ret = reg_write16(client, REG_OUT_WIDTH_HIGH, width);
 769         if (!ret)
 770                 ret = reg_write16(client, REG_OUT_HEIGHT_HIGH, height);
 771 
 772         /* Total width = output size + blanking */
 773         if (!ret)
 774                 ret = reg_write16(client, REG_OUT_TOTAL_WIDTH_HIGH, total_width);
 775         if (!ret)
 776                 ret = reg_write16(client, REG_OUT_TOTAL_HEIGHT_HIGH, total_height);
 777 
 778         /* Sets the window for AWB calculations */
 779         if (!ret)
 780                 ret = reg_write16(client, REG_AVG_WINDOW_END_X_HIGH, width);
 781         if (!ret)
 782                 ret = reg_write16(client, REG_AVG_WINDOW_END_Y_HIGH, height);
 783 
 784         return ret;
 785 }
 786 
 787 static int ov5642_set_fmt(struct v4l2_subdev *sd,
 788                 struct v4l2_subdev_pad_config *cfg,
 789                 struct v4l2_subdev_format *format)
 790 {
 791         struct v4l2_mbus_framefmt *mf = &format->format;
 792         struct i2c_client *client = v4l2_get_subdevdata(sd);
 793         struct ov5642 *priv = to_ov5642(client);
 794         const struct ov5642_datafmt *fmt = ov5642_find_datafmt(mf->code);
 795 
 796         if (format->pad)
 797                 return -EINVAL;
 798 
 799         mf->width = priv->crop_rect.width;
 800         mf->height = priv->crop_rect.height;
 801 
 802         if (!fmt) {
 803                 if (format->which == V4L2_SUBDEV_FORMAT_ACTIVE)
 804                         return -EINVAL;
 805                 mf->code        = ov5642_colour_fmts[0].code;
 806                 mf->colorspace  = ov5642_colour_fmts[0].colorspace;
 807         }
 808 
 809         mf->field       = V4L2_FIELD_NONE;
 810 
 811         if (format->which == V4L2_SUBDEV_FORMAT_ACTIVE)
 812                 priv->fmt = fmt;
 813         else
 814                 cfg->try_fmt = *mf;
 815         return 0;
 816 }
 817 
 818 static int ov5642_get_fmt(struct v4l2_subdev *sd,
 819                 struct v4l2_subdev_pad_config *cfg,
 820                 struct v4l2_subdev_format *format)
 821 {
 822         struct v4l2_mbus_framefmt *mf = &format->format;
 823         struct i2c_client *client = v4l2_get_subdevdata(sd);
 824         struct ov5642 *priv = to_ov5642(client);
 825 
 826         const struct ov5642_datafmt *fmt = priv->fmt;
 827 
 828         if (format->pad)
 829                 return -EINVAL;
 830 
 831         mf->code        = fmt->code;
 832         mf->colorspace  = fmt->colorspace;
 833         mf->width       = priv->crop_rect.width;
 834         mf->height      = priv->crop_rect.height;
 835         mf->field       = V4L2_FIELD_NONE;
 836 
 837         return 0;
 838 }
 839 
 840 static int ov5642_enum_mbus_code(struct v4l2_subdev *sd,
 841                 struct v4l2_subdev_pad_config *cfg,
 842                 struct v4l2_subdev_mbus_code_enum *code)
 843 {
 844         if (code->pad || code->index >= ARRAY_SIZE(ov5642_colour_fmts))
 845                 return -EINVAL;
 846 
 847         code->code = ov5642_colour_fmts[code->index].code;
 848         return 0;
 849 }
 850 
 851 static int ov5642_set_selection(struct v4l2_subdev *sd,
 852                 struct v4l2_subdev_pad_config *cfg,
 853                 struct v4l2_subdev_selection *sel)
 854 {
 855         struct i2c_client *client = v4l2_get_subdevdata(sd);
 856         struct ov5642 *priv = to_ov5642(client);
 857         struct v4l2_rect rect = sel->r;
 858         int ret;
 859 
 860         if (sel->which != V4L2_SUBDEV_FORMAT_ACTIVE ||
 861             sel->target != V4L2_SEL_TGT_CROP)
 862                 return -EINVAL;
 863 
 864         v4l_bound_align_image(&rect.width, 48, OV5642_MAX_WIDTH, 1,
 865                               &rect.height, 32, OV5642_MAX_HEIGHT, 1, 0);
 866 
 867         priv->crop_rect.width   = rect.width;
 868         priv->crop_rect.height  = rect.height;
 869         priv->total_width       = rect.width + BLANKING_EXTRA_WIDTH;
 870         priv->total_height      = max_t(int, rect.height +
 871                                                         BLANKING_EXTRA_HEIGHT,
 872                                                         BLANKING_MIN_HEIGHT);
 873         priv->crop_rect.width           = rect.width;
 874         priv->crop_rect.height          = rect.height;
 875 
 876         ret = ov5642_write_array(client, ov5642_default_regs_init);
 877         if (!ret)
 878                 ret = ov5642_set_resolution(sd);
 879         if (!ret)
 880                 ret = ov5642_write_array(client, ov5642_default_regs_finalise);
 881 
 882         return ret;
 883 }
 884 
 885 static int ov5642_get_selection(struct v4l2_subdev *sd,
 886                 struct v4l2_subdev_pad_config *cfg,
 887                 struct v4l2_subdev_selection *sel)
 888 {
 889         struct i2c_client *client = v4l2_get_subdevdata(sd);
 890         struct ov5642 *priv = to_ov5642(client);
 891 
 892         if (sel->which != V4L2_SUBDEV_FORMAT_ACTIVE)
 893                 return -EINVAL;
 894 
 895         switch (sel->target) {
 896         case V4L2_SEL_TGT_CROP_BOUNDS:
 897                 sel->r.left = 0;
 898                 sel->r.top = 0;
 899                 sel->r.width = OV5642_MAX_WIDTH;
 900                 sel->r.height = OV5642_MAX_HEIGHT;
 901                 return 0;
 902         case V4L2_SEL_TGT_CROP:
 903                 sel->r = priv->crop_rect;
 904                 return 0;
 905         default:
 906                 return -EINVAL;
 907         }
 908 }
 909 
 910 static int ov5642_g_mbus_config(struct v4l2_subdev *sd,
 911                                 struct v4l2_mbus_config *cfg)
 912 {
 913         cfg->type = V4L2_MBUS_CSI2_DPHY;
 914         cfg->flags = V4L2_MBUS_CSI2_2_LANE | V4L2_MBUS_CSI2_CHANNEL_0 |
 915                                         V4L2_MBUS_CSI2_CONTINUOUS_CLOCK;
 916 
 917         return 0;
 918 }
 919 
 920 static int ov5642_s_power(struct v4l2_subdev *sd, int on)
 921 {
 922         struct i2c_client *client = v4l2_get_subdevdata(sd);
 923         struct soc_camera_subdev_desc *ssdd = soc_camera_i2c_to_desc(client);
 924         struct ov5642 *priv = to_ov5642(client);
 925         int ret;
 926 
 927         if (!on)
 928                 return soc_camera_power_off(&client->dev, ssdd, priv->clk);
 929 
 930         ret = soc_camera_power_on(&client->dev, ssdd, priv->clk);
 931         if (ret < 0)
 932                 return ret;
 933 
 934         ret = ov5642_write_array(client, ov5642_default_regs_init);
 935         if (!ret)
 936                 ret = ov5642_set_resolution(sd);
 937         if (!ret)
 938                 ret = ov5642_write_array(client, ov5642_default_regs_finalise);
 939 
 940         return ret;
 941 }
 942 
 943 static const struct v4l2_subdev_video_ops ov5642_subdev_video_ops = {
 944         .g_mbus_config  = ov5642_g_mbus_config,
 945 };
 946 
 947 static const struct v4l2_subdev_pad_ops ov5642_subdev_pad_ops = {
 948         .enum_mbus_code = ov5642_enum_mbus_code,
 949         .get_selection  = ov5642_get_selection,
 950         .set_selection  = ov5642_set_selection,
 951         .get_fmt        = ov5642_get_fmt,
 952         .set_fmt        = ov5642_set_fmt,
 953 };
 954 
 955 static const struct v4l2_subdev_core_ops ov5642_subdev_core_ops = {
 956         .s_power        = ov5642_s_power,
 957 #ifdef CONFIG_VIDEO_ADV_DEBUG
 958         .g_register     = ov5642_get_register,
 959         .s_register     = ov5642_set_register,
 960 #endif
 961 };
 962 
 963 static const struct v4l2_subdev_ops ov5642_subdev_ops = {
 964         .core   = &ov5642_subdev_core_ops,
 965         .video  = &ov5642_subdev_video_ops,
 966         .pad    = &ov5642_subdev_pad_ops,
 967 };
 968 
 969 static int ov5642_video_probe(struct i2c_client *client)
 970 {
 971         struct v4l2_subdev *subdev = i2c_get_clientdata(client);
 972         int ret;
 973         u8 id_high, id_low;
 974         u16 id;
 975 
 976         ret = ov5642_s_power(subdev, 1);
 977         if (ret < 0)
 978                 return ret;
 979 
 980         /* Read sensor Model ID */
 981         ret = reg_read(client, REG_CHIP_ID_HIGH, &id_high);
 982         if (ret < 0)
 983                 goto done;
 984 
 985         id = id_high << 8;
 986 
 987         ret = reg_read(client, REG_CHIP_ID_LOW, &id_low);
 988         if (ret < 0)
 989                 goto done;
 990 
 991         id |= id_low;
 992 
 993         dev_info(&client->dev, "Chip ID 0x%04x detected\n", id);
 994 
 995         if (id != 0x5642) {
 996                 ret = -ENODEV;
 997                 goto done;
 998         }
 999 
1000         ret = 0;
1001 
1002 done:
1003         ov5642_s_power(subdev, 0);
1004         return ret;
1005 }
1006 
1007 static int ov5642_probe(struct i2c_client *client,
1008                         const struct i2c_device_id *did)
1009 {
1010         struct ov5642 *priv;
1011         struct soc_camera_subdev_desc *ssdd = soc_camera_i2c_to_desc(client);
1012         int ret;
1013 
1014         if (!ssdd) {
1015                 dev_err(&client->dev, "OV5642: missing platform data!\n");
1016                 return -EINVAL;
1017         }
1018 
1019         priv = devm_kzalloc(&client->dev, sizeof(struct ov5642), GFP_KERNEL);
1020         if (!priv)
1021                 return -ENOMEM;
1022 
1023         v4l2_i2c_subdev_init(&priv->subdev, client, &ov5642_subdev_ops);
1024 
1025         priv->fmt               = &ov5642_colour_fmts[0];
1026 
1027         priv->crop_rect.width   = OV5642_DEFAULT_WIDTH;
1028         priv->crop_rect.height  = OV5642_DEFAULT_HEIGHT;
1029         priv->crop_rect.left    = (OV5642_MAX_WIDTH - OV5642_DEFAULT_WIDTH) / 2;
1030         priv->crop_rect.top     = (OV5642_MAX_HEIGHT - OV5642_DEFAULT_HEIGHT) / 2;
1031         priv->total_width = OV5642_DEFAULT_WIDTH + BLANKING_EXTRA_WIDTH;
1032         priv->total_height = BLANKING_MIN_HEIGHT;
1033 
1034         priv->clk = v4l2_clk_get(&client->dev, "mclk");
1035         if (IS_ERR(priv->clk))
1036                 return PTR_ERR(priv->clk);
1037 
1038         ret = ov5642_video_probe(client);
1039         if (ret < 0)
1040                 v4l2_clk_put(priv->clk);
1041 
1042         return ret;
1043 }
1044 
1045 static int ov5642_remove(struct i2c_client *client)
1046 {
1047         struct soc_camera_subdev_desc *ssdd = soc_camera_i2c_to_desc(client);
1048         struct ov5642 *priv = to_ov5642(client);
1049 
1050         v4l2_clk_put(priv->clk);
1051         if (ssdd->free_bus)
1052                 ssdd->free_bus(ssdd);
1053 
1054         return 0;
1055 }
1056 
1057 static const struct i2c_device_id ov5642_id[] = {
1058         { "ov5642", 0 },
1059         { }
1060 };
1061 MODULE_DEVICE_TABLE(i2c, ov5642_id);
1062 
1063 #if IS_ENABLED(CONFIG_OF)
1064 static const struct of_device_id ov5642_of_match[] = {
1065         { .compatible = "ovti,ov5642" },
1066         { },
1067 };
1068 MODULE_DEVICE_TABLE(of, ov5642_of_match);
1069 #endif
1070 
1071 static struct i2c_driver ov5642_i2c_driver = {
1072         .driver = {
1073                 .name = "ov5642",
1074                 .of_match_table = of_match_ptr(ov5642_of_match),
1075         },
1076         .probe          = ov5642_probe,
1077         .remove         = ov5642_remove,
1078         .id_table       = ov5642_id,
1079 };
1080 
1081 module_i2c_driver(ov5642_i2c_driver);
1082 
1083 MODULE_DESCRIPTION("Omnivision OV5642 Camera driver");
1084 MODULE_AUTHOR("Bastian Hecht <hechtb@gmail.com>");
1085 MODULE_LICENSE("GPL v2");

/* [<][>][^][v][top][bottom][index][help] */