Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * A V4L2 driver for OmniVision OV5647 cameras.
0004  *
0005  * Based on Samsung S5K6AAFX SXGA 1/6" 1.3M CMOS Image Sensor driver
0006  * Copyright (C) 2011 Sylwester Nawrocki <s.nawrocki@samsung.com>
0007  *
0008  * Based on Omnivision OV7670 Camera Driver
0009  * Copyright (C) 2006-7 Jonathan Corbet <corbet@lwn.net>
0010  *
0011  * Copyright (C) 2016, Synopsys, Inc.
0012  */
0013 
0014 #include <linux/clk.h>
0015 #include <linux/delay.h>
0016 #include <linux/gpio/consumer.h>
0017 #include <linux/i2c.h>
0018 #include <linux/init.h>
0019 #include <linux/io.h>
0020 #include <linux/module.h>
0021 #include <linux/of_graph.h>
0022 #include <linux/pm_runtime.h>
0023 #include <linux/slab.h>
0024 #include <linux/videodev2.h>
0025 #include <media/v4l2-ctrls.h>
0026 #include <media/v4l2-device.h>
0027 #include <media/v4l2-event.h>
0028 #include <media/v4l2-fwnode.h>
0029 #include <media/v4l2-image-sizes.h>
0030 #include <media/v4l2-mediabus.h>
0031 
0032 /*
0033  * From the datasheet, "20ms after PWDN goes low or 20ms after RESETB goes
0034  * high if reset is inserted after PWDN goes high, host can access sensor's
0035  * SCCB to initialize sensor."
0036  */
0037 #define PWDN_ACTIVE_DELAY_MS    20
0038 
0039 #define MIPI_CTRL00_CLOCK_LANE_GATE     BIT(5)
0040 #define MIPI_CTRL00_LINE_SYNC_ENABLE        BIT(4)
0041 #define MIPI_CTRL00_BUS_IDLE            BIT(2)
0042 #define MIPI_CTRL00_CLOCK_LANE_DISABLE      BIT(0)
0043 
0044 #define OV5647_SW_STANDBY       0x0100
0045 #define OV5647_SW_RESET         0x0103
0046 #define OV5647_REG_CHIPID_H     0x300a
0047 #define OV5647_REG_CHIPID_L     0x300b
0048 #define OV5640_REG_PAD_OUT      0x300d
0049 #define OV5647_REG_EXP_HI       0x3500
0050 #define OV5647_REG_EXP_MID      0x3501
0051 #define OV5647_REG_EXP_LO       0x3502
0052 #define OV5647_REG_AEC_AGC      0x3503
0053 #define OV5647_REG_GAIN_HI      0x350a
0054 #define OV5647_REG_GAIN_LO      0x350b
0055 #define OV5647_REG_VTS_HI       0x380e
0056 #define OV5647_REG_VTS_LO       0x380f
0057 #define OV5647_REG_FRAME_OFF_NUMBER 0x4202
0058 #define OV5647_REG_MIPI_CTRL00      0x4800
0059 #define OV5647_REG_MIPI_CTRL14      0x4814
0060 #define OV5647_REG_AWB          0x5001
0061 
0062 #define REG_TERM 0xfffe
0063 #define VAL_TERM 0xfe
0064 #define REG_DLY  0xffff
0065 
0066 /* OV5647 native and active pixel array size */
0067 #define OV5647_NATIVE_WIDTH     2624U
0068 #define OV5647_NATIVE_HEIGHT        1956U
0069 
0070 #define OV5647_PIXEL_ARRAY_LEFT     16U
0071 #define OV5647_PIXEL_ARRAY_TOP      16U
0072 #define OV5647_PIXEL_ARRAY_WIDTH    2592U
0073 #define OV5647_PIXEL_ARRAY_HEIGHT   1944U
0074 
0075 #define OV5647_VBLANK_MIN       4
0076 #define OV5647_VTS_MAX          32767
0077 
0078 #define OV5647_EXPOSURE_MIN     4
0079 #define OV5647_EXPOSURE_STEP        1
0080 #define OV5647_EXPOSURE_DEFAULT     1000
0081 #define OV5647_EXPOSURE_MAX     65535
0082 
0083 struct regval_list {
0084     u16 addr;
0085     u8 data;
0086 };
0087 
0088 struct ov5647_mode {
0089     struct v4l2_mbus_framefmt   format;
0090     struct v4l2_rect        crop;
0091     u64             pixel_rate;
0092     int             hts;
0093     int             vts;
0094     const struct regval_list    *reg_list;
0095     unsigned int            num_regs;
0096 };
0097 
0098 struct ov5647 {
0099     struct v4l2_subdev      sd;
0100     struct media_pad        pad;
0101     struct mutex            lock;
0102     struct clk          *xclk;
0103     struct gpio_desc        *pwdn;
0104     bool                clock_ncont;
0105     struct v4l2_ctrl_handler    ctrls;
0106     const struct ov5647_mode    *mode;
0107     struct v4l2_ctrl        *pixel_rate;
0108     struct v4l2_ctrl        *hblank;
0109     struct v4l2_ctrl        *vblank;
0110     struct v4l2_ctrl        *exposure;
0111     bool                streaming;
0112 };
0113 
0114 static inline struct ov5647 *to_sensor(struct v4l2_subdev *sd)
0115 {
0116     return container_of(sd, struct ov5647, sd);
0117 }
0118 
0119 static const struct regval_list sensor_oe_disable_regs[] = {
0120     {0x3000, 0x00},
0121     {0x3001, 0x00},
0122     {0x3002, 0x00},
0123 };
0124 
0125 static const struct regval_list sensor_oe_enable_regs[] = {
0126     {0x3000, 0x0f},
0127     {0x3001, 0xff},
0128     {0x3002, 0xe4},
0129 };
0130 
0131 static struct regval_list ov5647_2592x1944_10bpp[] = {
0132     {0x0100, 0x00},
0133     {0x0103, 0x01},
0134     {0x3034, 0x1a},
0135     {0x3035, 0x21},
0136     {0x3036, 0x69},
0137     {0x303c, 0x11},
0138     {0x3106, 0xf5},
0139     {0x3821, 0x06},
0140     {0x3820, 0x00},
0141     {0x3827, 0xec},
0142     {0x370c, 0x03},
0143     {0x3612, 0x5b},
0144     {0x3618, 0x04},
0145     {0x5000, 0x06},
0146     {0x5002, 0x41},
0147     {0x5003, 0x08},
0148     {0x5a00, 0x08},
0149     {0x3000, 0x00},
0150     {0x3001, 0x00},
0151     {0x3002, 0x00},
0152     {0x3016, 0x08},
0153     {0x3017, 0xe0},
0154     {0x3018, 0x44},
0155     {0x301c, 0xf8},
0156     {0x301d, 0xf0},
0157     {0x3a18, 0x00},
0158     {0x3a19, 0xf8},
0159     {0x3c01, 0x80},
0160     {0x3b07, 0x0c},
0161     {0x380c, 0x0b},
0162     {0x380d, 0x1c},
0163     {0x3814, 0x11},
0164     {0x3815, 0x11},
0165     {0x3708, 0x64},
0166     {0x3709, 0x12},
0167     {0x3808, 0x0a},
0168     {0x3809, 0x20},
0169     {0x380a, 0x07},
0170     {0x380b, 0x98},
0171     {0x3800, 0x00},
0172     {0x3801, 0x00},
0173     {0x3802, 0x00},
0174     {0x3803, 0x00},
0175     {0x3804, 0x0a},
0176     {0x3805, 0x3f},
0177     {0x3806, 0x07},
0178     {0x3807, 0xa3},
0179     {0x3811, 0x10},
0180     {0x3813, 0x06},
0181     {0x3630, 0x2e},
0182     {0x3632, 0xe2},
0183     {0x3633, 0x23},
0184     {0x3634, 0x44},
0185     {0x3636, 0x06},
0186     {0x3620, 0x64},
0187     {0x3621, 0xe0},
0188     {0x3600, 0x37},
0189     {0x3704, 0xa0},
0190     {0x3703, 0x5a},
0191     {0x3715, 0x78},
0192     {0x3717, 0x01},
0193     {0x3731, 0x02},
0194     {0x370b, 0x60},
0195     {0x3705, 0x1a},
0196     {0x3f05, 0x02},
0197     {0x3f06, 0x10},
0198     {0x3f01, 0x0a},
0199     {0x3a08, 0x01},
0200     {0x3a09, 0x28},
0201     {0x3a0a, 0x00},
0202     {0x3a0b, 0xf6},
0203     {0x3a0d, 0x08},
0204     {0x3a0e, 0x06},
0205     {0x3a0f, 0x58},
0206     {0x3a10, 0x50},
0207     {0x3a1b, 0x58},
0208     {0x3a1e, 0x50},
0209     {0x3a11, 0x60},
0210     {0x3a1f, 0x28},
0211     {0x4001, 0x02},
0212     {0x4004, 0x04},
0213     {0x4000, 0x09},
0214     {0x4837, 0x19},
0215     {0x4800, 0x24},
0216     {0x3503, 0x03},
0217     {0x0100, 0x01},
0218 };
0219 
0220 static struct regval_list ov5647_1080p30_10bpp[] = {
0221     {0x0100, 0x00},
0222     {0x0103, 0x01},
0223     {0x3034, 0x1a},
0224     {0x3035, 0x21},
0225     {0x3036, 0x62},
0226     {0x303c, 0x11},
0227     {0x3106, 0xf5},
0228     {0x3821, 0x06},
0229     {0x3820, 0x00},
0230     {0x3827, 0xec},
0231     {0x370c, 0x03},
0232     {0x3612, 0x5b},
0233     {0x3618, 0x04},
0234     {0x5000, 0x06},
0235     {0x5002, 0x41},
0236     {0x5003, 0x08},
0237     {0x5a00, 0x08},
0238     {0x3000, 0x00},
0239     {0x3001, 0x00},
0240     {0x3002, 0x00},
0241     {0x3016, 0x08},
0242     {0x3017, 0xe0},
0243     {0x3018, 0x44},
0244     {0x301c, 0xf8},
0245     {0x301d, 0xf0},
0246     {0x3a18, 0x00},
0247     {0x3a19, 0xf8},
0248     {0x3c01, 0x80},
0249     {0x3b07, 0x0c},
0250     {0x380c, 0x09},
0251     {0x380d, 0x70},
0252     {0x3814, 0x11},
0253     {0x3815, 0x11},
0254     {0x3708, 0x64},
0255     {0x3709, 0x12},
0256     {0x3808, 0x07},
0257     {0x3809, 0x80},
0258     {0x380a, 0x04},
0259     {0x380b, 0x38},
0260     {0x3800, 0x01},
0261     {0x3801, 0x5c},
0262     {0x3802, 0x01},
0263     {0x3803, 0xb2},
0264     {0x3804, 0x08},
0265     {0x3805, 0xe3},
0266     {0x3806, 0x05},
0267     {0x3807, 0xf1},
0268     {0x3811, 0x04},
0269     {0x3813, 0x02},
0270     {0x3630, 0x2e},
0271     {0x3632, 0xe2},
0272     {0x3633, 0x23},
0273     {0x3634, 0x44},
0274     {0x3636, 0x06},
0275     {0x3620, 0x64},
0276     {0x3621, 0xe0},
0277     {0x3600, 0x37},
0278     {0x3704, 0xa0},
0279     {0x3703, 0x5a},
0280     {0x3715, 0x78},
0281     {0x3717, 0x01},
0282     {0x3731, 0x02},
0283     {0x370b, 0x60},
0284     {0x3705, 0x1a},
0285     {0x3f05, 0x02},
0286     {0x3f06, 0x10},
0287     {0x3f01, 0x0a},
0288     {0x3a08, 0x01},
0289     {0x3a09, 0x4b},
0290     {0x3a0a, 0x01},
0291     {0x3a0b, 0x13},
0292     {0x3a0d, 0x04},
0293     {0x3a0e, 0x03},
0294     {0x3a0f, 0x58},
0295     {0x3a10, 0x50},
0296     {0x3a1b, 0x58},
0297     {0x3a1e, 0x50},
0298     {0x3a11, 0x60},
0299     {0x3a1f, 0x28},
0300     {0x4001, 0x02},
0301     {0x4004, 0x04},
0302     {0x4000, 0x09},
0303     {0x4837, 0x19},
0304     {0x4800, 0x34},
0305     {0x3503, 0x03},
0306     {0x0100, 0x01},
0307 };
0308 
0309 static struct regval_list ov5647_2x2binned_10bpp[] = {
0310     {0x0100, 0x00},
0311     {0x0103, 0x01},
0312     {0x3034, 0x1a},
0313     {0x3035, 0x21},
0314     {0x3036, 0x62},
0315     {0x303c, 0x11},
0316     {0x3106, 0xf5},
0317     {0x3827, 0xec},
0318     {0x370c, 0x03},
0319     {0x3612, 0x59},
0320     {0x3618, 0x00},
0321     {0x5000, 0x06},
0322     {0x5002, 0x41},
0323     {0x5003, 0x08},
0324     {0x5a00, 0x08},
0325     {0x3000, 0x00},
0326     {0x3001, 0x00},
0327     {0x3002, 0x00},
0328     {0x3016, 0x08},
0329     {0x3017, 0xe0},
0330     {0x3018, 0x44},
0331     {0x301c, 0xf8},
0332     {0x301d, 0xf0},
0333     {0x3a18, 0x00},
0334     {0x3a19, 0xf8},
0335     {0x3c01, 0x80},
0336     {0x3b07, 0x0c},
0337     {0x3800, 0x00},
0338     {0x3801, 0x00},
0339     {0x3802, 0x00},
0340     {0x3803, 0x00},
0341     {0x3804, 0x0a},
0342     {0x3805, 0x3f},
0343     {0x3806, 0x07},
0344     {0x3807, 0xa3},
0345     {0x3808, 0x05},
0346     {0x3809, 0x10},
0347     {0x380a, 0x03},
0348     {0x380b, 0xcc},
0349     {0x380c, 0x07},
0350     {0x380d, 0x68},
0351     {0x3811, 0x0c},
0352     {0x3813, 0x06},
0353     {0x3814, 0x31},
0354     {0x3815, 0x31},
0355     {0x3630, 0x2e},
0356     {0x3632, 0xe2},
0357     {0x3633, 0x23},
0358     {0x3634, 0x44},
0359     {0x3636, 0x06},
0360     {0x3620, 0x64},
0361     {0x3621, 0xe0},
0362     {0x3600, 0x37},
0363     {0x3704, 0xa0},
0364     {0x3703, 0x5a},
0365     {0x3715, 0x78},
0366     {0x3717, 0x01},
0367     {0x3731, 0x02},
0368     {0x370b, 0x60},
0369     {0x3705, 0x1a},
0370     {0x3f05, 0x02},
0371     {0x3f06, 0x10},
0372     {0x3f01, 0x0a},
0373     {0x3a08, 0x01},
0374     {0x3a09, 0x28},
0375     {0x3a0a, 0x00},
0376     {0x3a0b, 0xf6},
0377     {0x3a0d, 0x08},
0378     {0x3a0e, 0x06},
0379     {0x3a0f, 0x58},
0380     {0x3a10, 0x50},
0381     {0x3a1b, 0x58},
0382     {0x3a1e, 0x50},
0383     {0x3a11, 0x60},
0384     {0x3a1f, 0x28},
0385     {0x4001, 0x02},
0386     {0x4004, 0x04},
0387     {0x4000, 0x09},
0388     {0x4837, 0x16},
0389     {0x4800, 0x24},
0390     {0x3503, 0x03},
0391     {0x3820, 0x41},
0392     {0x3821, 0x07},
0393     {0x350a, 0x00},
0394     {0x350b, 0x10},
0395     {0x3500, 0x00},
0396     {0x3501, 0x1a},
0397     {0x3502, 0xf0},
0398     {0x3212, 0xa0},
0399     {0x0100, 0x01},
0400 };
0401 
0402 static struct regval_list ov5647_640x480_10bpp[] = {
0403     {0x0100, 0x00},
0404     {0x0103, 0x01},
0405     {0x3035, 0x11},
0406     {0x3036, 0x46},
0407     {0x303c, 0x11},
0408     {0x3821, 0x07},
0409     {0x3820, 0x41},
0410     {0x370c, 0x03},
0411     {0x3612, 0x59},
0412     {0x3618, 0x00},
0413     {0x5000, 0x06},
0414     {0x5003, 0x08},
0415     {0x5a00, 0x08},
0416     {0x3000, 0xff},
0417     {0x3001, 0xff},
0418     {0x3002, 0xff},
0419     {0x301d, 0xf0},
0420     {0x3a18, 0x00},
0421     {0x3a19, 0xf8},
0422     {0x3c01, 0x80},
0423     {0x3b07, 0x0c},
0424     {0x380c, 0x07},
0425     {0x380d, 0x3c},
0426     {0x3814, 0x35},
0427     {0x3815, 0x35},
0428     {0x3708, 0x64},
0429     {0x3709, 0x52},
0430     {0x3808, 0x02},
0431     {0x3809, 0x80},
0432     {0x380a, 0x01},
0433     {0x380b, 0xe0},
0434     {0x3800, 0x00},
0435     {0x3801, 0x10},
0436     {0x3802, 0x00},
0437     {0x3803, 0x00},
0438     {0x3804, 0x0a},
0439     {0x3805, 0x2f},
0440     {0x3806, 0x07},
0441     {0x3807, 0x9f},
0442     {0x3630, 0x2e},
0443     {0x3632, 0xe2},
0444     {0x3633, 0x23},
0445     {0x3634, 0x44},
0446     {0x3620, 0x64},
0447     {0x3621, 0xe0},
0448     {0x3600, 0x37},
0449     {0x3704, 0xa0},
0450     {0x3703, 0x5a},
0451     {0x3715, 0x78},
0452     {0x3717, 0x01},
0453     {0x3731, 0x02},
0454     {0x370b, 0x60},
0455     {0x3705, 0x1a},
0456     {0x3f05, 0x02},
0457     {0x3f06, 0x10},
0458     {0x3f01, 0x0a},
0459     {0x3a08, 0x01},
0460     {0x3a09, 0x2e},
0461     {0x3a0a, 0x00},
0462     {0x3a0b, 0xfb},
0463     {0x3a0d, 0x02},
0464     {0x3a0e, 0x01},
0465     {0x3a0f, 0x58},
0466     {0x3a10, 0x50},
0467     {0x3a1b, 0x58},
0468     {0x3a1e, 0x50},
0469     {0x3a11, 0x60},
0470     {0x3a1f, 0x28},
0471     {0x4001, 0x02},
0472     {0x4004, 0x02},
0473     {0x4000, 0x09},
0474     {0x3000, 0x00},
0475     {0x3001, 0x00},
0476     {0x3002, 0x00},
0477     {0x3017, 0xe0},
0478     {0x301c, 0xfc},
0479     {0x3636, 0x06},
0480     {0x3016, 0x08},
0481     {0x3827, 0xec},
0482     {0x3018, 0x44},
0483     {0x3035, 0x21},
0484     {0x3106, 0xf5},
0485     {0x3034, 0x1a},
0486     {0x301c, 0xf8},
0487     {0x4800, 0x34},
0488     {0x3503, 0x03},
0489     {0x0100, 0x01},
0490 };
0491 
0492 static const struct ov5647_mode ov5647_modes[] = {
0493     /* 2592x1944 full resolution full FOV 10-bit mode. */
0494     {
0495         .format = {
0496             .code       = MEDIA_BUS_FMT_SBGGR10_1X10,
0497             .colorspace = V4L2_COLORSPACE_SRGB,
0498             .field      = V4L2_FIELD_NONE,
0499             .width      = 2592,
0500             .height     = 1944
0501         },
0502         .crop = {
0503             .left       = OV5647_PIXEL_ARRAY_LEFT,
0504             .top        = OV5647_PIXEL_ARRAY_TOP,
0505             .width      = 2592,
0506             .height     = 1944
0507         },
0508         .pixel_rate = 87500000,
0509         .hts        = 2844,
0510         .vts        = 0x7b0,
0511         .reg_list   = ov5647_2592x1944_10bpp,
0512         .num_regs   = ARRAY_SIZE(ov5647_2592x1944_10bpp)
0513     },
0514     /* 1080p30 10-bit mode. Full resolution centre-cropped down to 1080p. */
0515     {
0516         .format = {
0517             .code       = MEDIA_BUS_FMT_SBGGR10_1X10,
0518             .colorspace = V4L2_COLORSPACE_SRGB,
0519             .field      = V4L2_FIELD_NONE,
0520             .width      = 1920,
0521             .height     = 1080
0522         },
0523         .crop = {
0524             .left       = 348 + OV5647_PIXEL_ARRAY_LEFT,
0525             .top        = 434 + OV5647_PIXEL_ARRAY_TOP,
0526             .width      = 1928,
0527             .height     = 1080,
0528         },
0529         .pixel_rate = 81666700,
0530         .hts        = 2416,
0531         .vts        = 0x450,
0532         .reg_list   = ov5647_1080p30_10bpp,
0533         .num_regs   = ARRAY_SIZE(ov5647_1080p30_10bpp)
0534     },
0535     /* 2x2 binned full FOV 10-bit mode. */
0536     {
0537         .format = {
0538             .code       = MEDIA_BUS_FMT_SBGGR10_1X10,
0539             .colorspace = V4L2_COLORSPACE_SRGB,
0540             .field      = V4L2_FIELD_NONE,
0541             .width      = 1296,
0542             .height     = 972
0543         },
0544         .crop = {
0545             .left       = OV5647_PIXEL_ARRAY_LEFT,
0546             .top        = OV5647_PIXEL_ARRAY_TOP,
0547             .width      = 2592,
0548             .height     = 1944,
0549         },
0550         .pixel_rate = 81666700,
0551         .hts        = 1896,
0552         .vts        = 0x59b,
0553         .reg_list   = ov5647_2x2binned_10bpp,
0554         .num_regs   = ARRAY_SIZE(ov5647_2x2binned_10bpp)
0555     },
0556     /* 10-bit VGA full FOV 60fps. 2x2 binned and subsampled down to VGA. */
0557     {
0558         .format = {
0559             .code       = MEDIA_BUS_FMT_SBGGR10_1X10,
0560             .colorspace = V4L2_COLORSPACE_SRGB,
0561             .field      = V4L2_FIELD_NONE,
0562             .width      = 640,
0563             .height     = 480
0564         },
0565         .crop = {
0566             .left       = 16 + OV5647_PIXEL_ARRAY_LEFT,
0567             .top        = OV5647_PIXEL_ARRAY_TOP,
0568             .width      = 2560,
0569             .height     = 1920,
0570         },
0571         .pixel_rate = 55000000,
0572         .hts        = 1852,
0573         .vts        = 0x1f8,
0574         .reg_list   = ov5647_640x480_10bpp,
0575         .num_regs   = ARRAY_SIZE(ov5647_640x480_10bpp)
0576     },
0577 };
0578 
0579 /* Default sensor mode is 2x2 binned 640x480 SBGGR10_1X10. */
0580 #define OV5647_DEFAULT_MODE (&ov5647_modes[3])
0581 #define OV5647_DEFAULT_FORMAT   (ov5647_modes[3].format)
0582 
0583 static int ov5647_write16(struct v4l2_subdev *sd, u16 reg, u16 val)
0584 {
0585     unsigned char data[4] = { reg >> 8, reg & 0xff, val >> 8, val & 0xff};
0586     struct i2c_client *client = v4l2_get_subdevdata(sd);
0587     int ret;
0588 
0589     ret = i2c_master_send(client, data, 4);
0590     if (ret < 0) {
0591         dev_dbg(&client->dev, "%s: i2c write error, reg: %x\n",
0592             __func__, reg);
0593         return ret;
0594     }
0595 
0596     return 0;
0597 }
0598 
0599 static int ov5647_write(struct v4l2_subdev *sd, u16 reg, u8 val)
0600 {
0601     unsigned char data[3] = { reg >> 8, reg & 0xff, val};
0602     struct i2c_client *client = v4l2_get_subdevdata(sd);
0603     int ret;
0604 
0605     ret = i2c_master_send(client, data, 3);
0606     if (ret < 0) {
0607         dev_dbg(&client->dev, "%s: i2c write error, reg: %x\n",
0608                 __func__, reg);
0609         return ret;
0610     }
0611 
0612     return 0;
0613 }
0614 
0615 static int ov5647_read(struct v4l2_subdev *sd, u16 reg, u8 *val)
0616 {
0617     unsigned char data_w[2] = { reg >> 8, reg & 0xff };
0618     struct i2c_client *client = v4l2_get_subdevdata(sd);
0619     int ret;
0620 
0621     ret = i2c_master_send(client, data_w, 2);
0622     if (ret < 0) {
0623         dev_dbg(&client->dev, "%s: i2c write error, reg: %x\n",
0624             __func__, reg);
0625         return ret;
0626     }
0627 
0628     ret = i2c_master_recv(client, val, 1);
0629     if (ret < 0) {
0630         dev_dbg(&client->dev, "%s: i2c read error, reg: %x\n",
0631                 __func__, reg);
0632         return ret;
0633     }
0634 
0635     return 0;
0636 }
0637 
0638 static int ov5647_write_array(struct v4l2_subdev *sd,
0639                   const struct regval_list *regs, int array_size)
0640 {
0641     int i, ret;
0642 
0643     for (i = 0; i < array_size; i++) {
0644         ret = ov5647_write(sd, regs[i].addr, regs[i].data);
0645         if (ret < 0)
0646             return ret;
0647     }
0648 
0649     return 0;
0650 }
0651 
0652 static int ov5647_set_virtual_channel(struct v4l2_subdev *sd, int channel)
0653 {
0654     u8 channel_id;
0655     int ret;
0656 
0657     ret = ov5647_read(sd, OV5647_REG_MIPI_CTRL14, &channel_id);
0658     if (ret < 0)
0659         return ret;
0660 
0661     channel_id &= ~(3 << 6);
0662 
0663     return ov5647_write(sd, OV5647_REG_MIPI_CTRL14,
0664                 channel_id | (channel << 6));
0665 }
0666 
0667 static int ov5647_set_mode(struct v4l2_subdev *sd)
0668 {
0669     struct i2c_client *client = v4l2_get_subdevdata(sd);
0670     struct ov5647 *sensor = to_sensor(sd);
0671     u8 resetval, rdval;
0672     int ret;
0673 
0674     ret = ov5647_read(sd, OV5647_SW_STANDBY, &rdval);
0675     if (ret < 0)
0676         return ret;
0677 
0678     ret = ov5647_write_array(sd, sensor->mode->reg_list,
0679                  sensor->mode->num_regs);
0680     if (ret < 0) {
0681         dev_err(&client->dev, "write sensor default regs error\n");
0682         return ret;
0683     }
0684 
0685     ret = ov5647_set_virtual_channel(sd, 0);
0686     if (ret < 0)
0687         return ret;
0688 
0689     ret = ov5647_read(sd, OV5647_SW_STANDBY, &resetval);
0690     if (ret < 0)
0691         return ret;
0692 
0693     if (!(resetval & 0x01)) {
0694         dev_err(&client->dev, "Device was in SW standby");
0695         ret = ov5647_write(sd, OV5647_SW_STANDBY, 0x01);
0696         if (ret < 0)
0697             return ret;
0698     }
0699 
0700     return 0;
0701 }
0702 
0703 static int ov5647_stream_on(struct v4l2_subdev *sd)
0704 {
0705     struct i2c_client *client = v4l2_get_subdevdata(sd);
0706     struct ov5647 *sensor = to_sensor(sd);
0707     u8 val = MIPI_CTRL00_BUS_IDLE;
0708     int ret;
0709 
0710     ret = ov5647_set_mode(sd);
0711     if (ret) {
0712         dev_err(&client->dev, "Failed to program sensor mode: %d\n", ret);
0713         return ret;
0714     }
0715 
0716     /* Apply customized values from user when stream starts. */
0717     ret =  __v4l2_ctrl_handler_setup(sd->ctrl_handler);
0718     if (ret)
0719         return ret;
0720 
0721     if (sensor->clock_ncont)
0722         val |= MIPI_CTRL00_CLOCK_LANE_GATE |
0723                MIPI_CTRL00_LINE_SYNC_ENABLE;
0724 
0725     ret = ov5647_write(sd, OV5647_REG_MIPI_CTRL00, val);
0726     if (ret < 0)
0727         return ret;
0728 
0729     ret = ov5647_write(sd, OV5647_REG_FRAME_OFF_NUMBER, 0x00);
0730     if (ret < 0)
0731         return ret;
0732 
0733     return ov5647_write(sd, OV5640_REG_PAD_OUT, 0x00);
0734 }
0735 
0736 static int ov5647_stream_off(struct v4l2_subdev *sd)
0737 {
0738     int ret;
0739 
0740     ret = ov5647_write(sd, OV5647_REG_MIPI_CTRL00,
0741                MIPI_CTRL00_CLOCK_LANE_GATE | MIPI_CTRL00_BUS_IDLE |
0742                MIPI_CTRL00_CLOCK_LANE_DISABLE);
0743     if (ret < 0)
0744         return ret;
0745 
0746     ret = ov5647_write(sd, OV5647_REG_FRAME_OFF_NUMBER, 0x0f);
0747     if (ret < 0)
0748         return ret;
0749 
0750     return ov5647_write(sd, OV5640_REG_PAD_OUT, 0x01);
0751 }
0752 
0753 static int ov5647_power_on(struct device *dev)
0754 {
0755     struct ov5647 *sensor = dev_get_drvdata(dev);
0756     int ret;
0757 
0758     dev_dbg(dev, "OV5647 power on\n");
0759 
0760     if (sensor->pwdn) {
0761         gpiod_set_value_cansleep(sensor->pwdn, 0);
0762         msleep(PWDN_ACTIVE_DELAY_MS);
0763     }
0764 
0765     ret = clk_prepare_enable(sensor->xclk);
0766     if (ret < 0) {
0767         dev_err(dev, "clk prepare enable failed\n");
0768         goto error_pwdn;
0769     }
0770 
0771     ret = ov5647_write_array(&sensor->sd, sensor_oe_enable_regs,
0772                  ARRAY_SIZE(sensor_oe_enable_regs));
0773     if (ret < 0) {
0774         dev_err(dev, "write sensor_oe_enable_regs error\n");
0775         goto error_clk_disable;
0776     }
0777 
0778     /* Stream off to coax lanes into LP-11 state. */
0779     ret = ov5647_stream_off(&sensor->sd);
0780     if (ret < 0) {
0781         dev_err(dev, "camera not available, check power\n");
0782         goto error_clk_disable;
0783     }
0784 
0785     return 0;
0786 
0787 error_clk_disable:
0788     clk_disable_unprepare(sensor->xclk);
0789 error_pwdn:
0790     gpiod_set_value_cansleep(sensor->pwdn, 1);
0791 
0792     return ret;
0793 }
0794 
0795 static int ov5647_power_off(struct device *dev)
0796 {
0797     struct ov5647 *sensor = dev_get_drvdata(dev);
0798     u8 rdval;
0799     int ret;
0800 
0801     dev_dbg(dev, "OV5647 power off\n");
0802 
0803     ret = ov5647_write_array(&sensor->sd, sensor_oe_disable_regs,
0804                  ARRAY_SIZE(sensor_oe_disable_regs));
0805     if (ret < 0)
0806         dev_dbg(dev, "disable oe failed\n");
0807 
0808     /* Enter software standby */
0809     ret = ov5647_read(&sensor->sd, OV5647_SW_STANDBY, &rdval);
0810     if (ret < 0)
0811         dev_dbg(dev, "software standby failed\n");
0812 
0813     rdval &= ~0x01;
0814     ret = ov5647_write(&sensor->sd, OV5647_SW_STANDBY, rdval);
0815     if (ret < 0)
0816         dev_dbg(dev, "software standby failed\n");
0817 
0818     clk_disable_unprepare(sensor->xclk);
0819     gpiod_set_value_cansleep(sensor->pwdn, 1);
0820 
0821     return 0;
0822 }
0823 
0824 #ifdef CONFIG_VIDEO_ADV_DEBUG
0825 static int ov5647_sensor_get_register(struct v4l2_subdev *sd,
0826                       struct v4l2_dbg_register *reg)
0827 {
0828     int ret;
0829     u8 val;
0830 
0831     ret = ov5647_read(sd, reg->reg & 0xff, &val);
0832     if (ret < 0)
0833         return ret;
0834 
0835     reg->val = val;
0836     reg->size = 1;
0837 
0838     return 0;
0839 }
0840 
0841 static int ov5647_sensor_set_register(struct v4l2_subdev *sd,
0842                       const struct v4l2_dbg_register *reg)
0843 {
0844     return ov5647_write(sd, reg->reg & 0xff, reg->val & 0xff);
0845 }
0846 #endif
0847 
0848 /* Subdev core operations registration */
0849 static const struct v4l2_subdev_core_ops ov5647_subdev_core_ops = {
0850     .subscribe_event    = v4l2_ctrl_subdev_subscribe_event,
0851     .unsubscribe_event  = v4l2_event_subdev_unsubscribe,
0852 #ifdef CONFIG_VIDEO_ADV_DEBUG
0853     .g_register     = ov5647_sensor_get_register,
0854     .s_register     = ov5647_sensor_set_register,
0855 #endif
0856 };
0857 
0858 static const struct v4l2_rect *
0859 __ov5647_get_pad_crop(struct ov5647 *ov5647,
0860               struct v4l2_subdev_state *sd_state,
0861               unsigned int pad, enum v4l2_subdev_format_whence which)
0862 {
0863     switch (which) {
0864     case V4L2_SUBDEV_FORMAT_TRY:
0865         return v4l2_subdev_get_try_crop(&ov5647->sd, sd_state, pad);
0866     case V4L2_SUBDEV_FORMAT_ACTIVE:
0867         return &ov5647->mode->crop;
0868     }
0869 
0870     return NULL;
0871 }
0872 
0873 static int ov5647_s_stream(struct v4l2_subdev *sd, int enable)
0874 {
0875     struct i2c_client *client = v4l2_get_subdevdata(sd);
0876     struct ov5647 *sensor = to_sensor(sd);
0877     int ret;
0878 
0879     mutex_lock(&sensor->lock);
0880     if (sensor->streaming == enable) {
0881         mutex_unlock(&sensor->lock);
0882         return 0;
0883     }
0884 
0885     if (enable) {
0886         ret = pm_runtime_resume_and_get(&client->dev);
0887         if (ret < 0)
0888             goto error_unlock;
0889 
0890         ret = ov5647_stream_on(sd);
0891         if (ret < 0) {
0892             dev_err(&client->dev, "stream start failed: %d\n", ret);
0893             goto error_pm;
0894         }
0895     } else {
0896         ret = ov5647_stream_off(sd);
0897         if (ret < 0) {
0898             dev_err(&client->dev, "stream stop failed: %d\n", ret);
0899             goto error_pm;
0900         }
0901         pm_runtime_put(&client->dev);
0902     }
0903 
0904     sensor->streaming = enable;
0905     mutex_unlock(&sensor->lock);
0906 
0907     return 0;
0908 
0909 error_pm:
0910     pm_runtime_put(&client->dev);
0911 error_unlock:
0912     mutex_unlock(&sensor->lock);
0913 
0914     return ret;
0915 }
0916 
0917 static const struct v4l2_subdev_video_ops ov5647_subdev_video_ops = {
0918     .s_stream =     ov5647_s_stream,
0919 };
0920 
0921 static int ov5647_enum_mbus_code(struct v4l2_subdev *sd,
0922                  struct v4l2_subdev_state *sd_state,
0923                  struct v4l2_subdev_mbus_code_enum *code)
0924 {
0925     if (code->index > 0)
0926         return -EINVAL;
0927 
0928     code->code = MEDIA_BUS_FMT_SBGGR10_1X10;
0929 
0930     return 0;
0931 }
0932 
0933 static int ov5647_enum_frame_size(struct v4l2_subdev *sd,
0934                   struct v4l2_subdev_state *sd_state,
0935                   struct v4l2_subdev_frame_size_enum *fse)
0936 {
0937     const struct v4l2_mbus_framefmt *fmt;
0938 
0939     if (fse->code != MEDIA_BUS_FMT_SBGGR10_1X10 ||
0940         fse->index >= ARRAY_SIZE(ov5647_modes))
0941         return -EINVAL;
0942 
0943     fmt = &ov5647_modes[fse->index].format;
0944     fse->min_width = fmt->width;
0945     fse->max_width = fmt->width;
0946     fse->min_height = fmt->height;
0947     fse->max_height = fmt->height;
0948 
0949     return 0;
0950 }
0951 
0952 static int ov5647_get_pad_fmt(struct v4l2_subdev *sd,
0953                   struct v4l2_subdev_state *sd_state,
0954                   struct v4l2_subdev_format *format)
0955 {
0956     struct v4l2_mbus_framefmt *fmt = &format->format;
0957     const struct v4l2_mbus_framefmt *sensor_format;
0958     struct ov5647 *sensor = to_sensor(sd);
0959 
0960     mutex_lock(&sensor->lock);
0961     switch (format->which) {
0962     case V4L2_SUBDEV_FORMAT_TRY:
0963         sensor_format = v4l2_subdev_get_try_format(sd, sd_state,
0964                                format->pad);
0965         break;
0966     default:
0967         sensor_format = &sensor->mode->format;
0968         break;
0969     }
0970 
0971     *fmt = *sensor_format;
0972     mutex_unlock(&sensor->lock);
0973 
0974     return 0;
0975 }
0976 
0977 static int ov5647_set_pad_fmt(struct v4l2_subdev *sd,
0978                   struct v4l2_subdev_state *sd_state,
0979                   struct v4l2_subdev_format *format)
0980 {
0981     struct v4l2_mbus_framefmt *fmt = &format->format;
0982     struct ov5647 *sensor = to_sensor(sd);
0983     const struct ov5647_mode *mode;
0984 
0985     mode = v4l2_find_nearest_size(ov5647_modes, ARRAY_SIZE(ov5647_modes),
0986                       format.width, format.height,
0987                       fmt->width, fmt->height);
0988 
0989     /* Update the sensor mode and apply at it at streamon time. */
0990     mutex_lock(&sensor->lock);
0991     if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
0992         *v4l2_subdev_get_try_format(sd, sd_state, format->pad) = mode->format;
0993     } else {
0994         int exposure_max, exposure_def;
0995         int hblank, vblank;
0996 
0997         sensor->mode = mode;
0998         __v4l2_ctrl_modify_range(sensor->pixel_rate, mode->pixel_rate,
0999                      mode->pixel_rate, 1, mode->pixel_rate);
1000 
1001         hblank = mode->hts - mode->format.width;
1002         __v4l2_ctrl_modify_range(sensor->hblank, hblank, hblank, 1,
1003                      hblank);
1004 
1005         vblank = mode->vts - mode->format.height;
1006         __v4l2_ctrl_modify_range(sensor->vblank, OV5647_VBLANK_MIN,
1007                      OV5647_VTS_MAX - mode->format.height,
1008                      1, vblank);
1009         __v4l2_ctrl_s_ctrl(sensor->vblank, vblank);
1010 
1011         exposure_max = mode->vts - 4;
1012         exposure_def = min(exposure_max, OV5647_EXPOSURE_DEFAULT);
1013         __v4l2_ctrl_modify_range(sensor->exposure,
1014                      sensor->exposure->minimum,
1015                      exposure_max, sensor->exposure->step,
1016                      exposure_def);
1017     }
1018     *fmt = mode->format;
1019     mutex_unlock(&sensor->lock);
1020 
1021     return 0;
1022 }
1023 
1024 static int ov5647_get_selection(struct v4l2_subdev *sd,
1025                 struct v4l2_subdev_state *sd_state,
1026                 struct v4l2_subdev_selection *sel)
1027 {
1028     switch (sel->target) {
1029     case V4L2_SEL_TGT_CROP: {
1030         struct ov5647 *sensor = to_sensor(sd);
1031 
1032         mutex_lock(&sensor->lock);
1033         sel->r = *__ov5647_get_pad_crop(sensor, sd_state, sel->pad,
1034                         sel->which);
1035         mutex_unlock(&sensor->lock);
1036 
1037         return 0;
1038     }
1039 
1040     case V4L2_SEL_TGT_NATIVE_SIZE:
1041         sel->r.top = 0;
1042         sel->r.left = 0;
1043         sel->r.width = OV5647_NATIVE_WIDTH;
1044         sel->r.height = OV5647_NATIVE_HEIGHT;
1045 
1046         return 0;
1047 
1048     case V4L2_SEL_TGT_CROP_DEFAULT:
1049     case V4L2_SEL_TGT_CROP_BOUNDS:
1050         sel->r.top = OV5647_PIXEL_ARRAY_TOP;
1051         sel->r.left = OV5647_PIXEL_ARRAY_LEFT;
1052         sel->r.width = OV5647_PIXEL_ARRAY_WIDTH;
1053         sel->r.height = OV5647_PIXEL_ARRAY_HEIGHT;
1054 
1055         return 0;
1056     }
1057 
1058     return -EINVAL;
1059 }
1060 
1061 static const struct v4l2_subdev_pad_ops ov5647_subdev_pad_ops = {
1062     .enum_mbus_code     = ov5647_enum_mbus_code,
1063     .enum_frame_size    = ov5647_enum_frame_size,
1064     .set_fmt        = ov5647_set_pad_fmt,
1065     .get_fmt        = ov5647_get_pad_fmt,
1066     .get_selection      = ov5647_get_selection,
1067 };
1068 
1069 static const struct v4l2_subdev_ops ov5647_subdev_ops = {
1070     .core       = &ov5647_subdev_core_ops,
1071     .video      = &ov5647_subdev_video_ops,
1072     .pad        = &ov5647_subdev_pad_ops,
1073 };
1074 
1075 static int ov5647_detect(struct v4l2_subdev *sd)
1076 {
1077     struct i2c_client *client = v4l2_get_subdevdata(sd);
1078     u8 read;
1079     int ret;
1080 
1081     ret = ov5647_write(sd, OV5647_SW_RESET, 0x01);
1082     if (ret < 0)
1083         return ret;
1084 
1085     ret = ov5647_read(sd, OV5647_REG_CHIPID_H, &read);
1086     if (ret < 0)
1087         return ret;
1088 
1089     if (read != 0x56) {
1090         dev_err(&client->dev, "ID High expected 0x56 got %x", read);
1091         return -ENODEV;
1092     }
1093 
1094     ret = ov5647_read(sd, OV5647_REG_CHIPID_L, &read);
1095     if (ret < 0)
1096         return ret;
1097 
1098     if (read != 0x47) {
1099         dev_err(&client->dev, "ID Low expected 0x47 got %x", read);
1100         return -ENODEV;
1101     }
1102 
1103     return ov5647_write(sd, OV5647_SW_RESET, 0x00);
1104 }
1105 
1106 static int ov5647_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
1107 {
1108     struct v4l2_mbus_framefmt *format =
1109                 v4l2_subdev_get_try_format(sd, fh->state, 0);
1110     struct v4l2_rect *crop = v4l2_subdev_get_try_crop(sd, fh->state, 0);
1111 
1112     crop->left = OV5647_PIXEL_ARRAY_LEFT;
1113     crop->top = OV5647_PIXEL_ARRAY_TOP;
1114     crop->width = OV5647_PIXEL_ARRAY_WIDTH;
1115     crop->height = OV5647_PIXEL_ARRAY_HEIGHT;
1116 
1117     *format = OV5647_DEFAULT_FORMAT;
1118 
1119     return 0;
1120 }
1121 
1122 static const struct v4l2_subdev_internal_ops ov5647_subdev_internal_ops = {
1123     .open = ov5647_open,
1124 };
1125 
1126 static int ov5647_s_auto_white_balance(struct v4l2_subdev *sd, u32 val)
1127 {
1128     return ov5647_write(sd, OV5647_REG_AWB, val ? 1 : 0);
1129 }
1130 
1131 static int ov5647_s_autogain(struct v4l2_subdev *sd, u32 val)
1132 {
1133     int ret;
1134     u8 reg;
1135 
1136     /* Non-zero turns on AGC by clearing bit 1.*/
1137     ret = ov5647_read(sd, OV5647_REG_AEC_AGC, &reg);
1138     if (ret)
1139         return ret;
1140 
1141     return ov5647_write(sd, OV5647_REG_AEC_AGC, val ? reg & ~BIT(1)
1142                             : reg | BIT(1));
1143 }
1144 
1145 static int ov5647_s_exposure_auto(struct v4l2_subdev *sd, u32 val)
1146 {
1147     int ret;
1148     u8 reg;
1149 
1150     /*
1151      * Everything except V4L2_EXPOSURE_MANUAL turns on AEC by
1152      * clearing bit 0.
1153      */
1154     ret = ov5647_read(sd, OV5647_REG_AEC_AGC, &reg);
1155     if (ret)
1156         return ret;
1157 
1158     return ov5647_write(sd, OV5647_REG_AEC_AGC,
1159                 val == V4L2_EXPOSURE_MANUAL ? reg | BIT(0)
1160                             : reg & ~BIT(0));
1161 }
1162 
1163 static int ov5647_s_analogue_gain(struct v4l2_subdev *sd, u32 val)
1164 {
1165     int ret;
1166 
1167     /* 10 bits of gain, 2 in the high register. */
1168     ret = ov5647_write(sd, OV5647_REG_GAIN_HI, (val >> 8) & 3);
1169     if (ret)
1170         return ret;
1171 
1172     return ov5647_write(sd, OV5647_REG_GAIN_LO, val & 0xff);
1173 }
1174 
1175 static int ov5647_s_exposure(struct v4l2_subdev *sd, u32 val)
1176 {
1177     int ret;
1178 
1179     /*
1180      * Sensor has 20 bits, but the bottom 4 bits are fractions of a line
1181      * which we leave as zero (and don't receive in "val").
1182      */
1183     ret = ov5647_write(sd, OV5647_REG_EXP_HI, (val >> 12) & 0xf);
1184     if (ret)
1185         return ret;
1186 
1187     ret = ov5647_write(sd, OV5647_REG_EXP_MID, (val >> 4) & 0xff);
1188     if (ret)
1189         return ret;
1190 
1191     return ov5647_write(sd, OV5647_REG_EXP_LO, (val & 0xf) << 4);
1192 }
1193 
1194 static int ov5647_s_ctrl(struct v4l2_ctrl *ctrl)
1195 {
1196     struct ov5647 *sensor = container_of(ctrl->handler,
1197                         struct ov5647, ctrls);
1198     struct v4l2_subdev *sd = &sensor->sd;
1199     struct i2c_client *client = v4l2_get_subdevdata(sd);
1200     int ret = 0;
1201 
1202 
1203     /* v4l2_ctrl_lock() locks our own mutex */
1204 
1205     if (ctrl->id == V4L2_CID_VBLANK) {
1206         int exposure_max, exposure_def;
1207 
1208         /* Update max exposure while meeting expected vblanking */
1209         exposure_max = sensor->mode->format.height + ctrl->val - 4;
1210         exposure_def = min(exposure_max, OV5647_EXPOSURE_DEFAULT);
1211         __v4l2_ctrl_modify_range(sensor->exposure,
1212                      sensor->exposure->minimum,
1213                      exposure_max, sensor->exposure->step,
1214                      exposure_def);
1215     }
1216 
1217     /*
1218      * If the device is not powered up do not apply any controls
1219      * to H/W at this time. Instead the controls will be restored
1220      * at s_stream(1) time.
1221      */
1222     if (pm_runtime_get_if_in_use(&client->dev) == 0)
1223         return 0;
1224 
1225     switch (ctrl->id) {
1226     case V4L2_CID_AUTO_WHITE_BALANCE:
1227         ret = ov5647_s_auto_white_balance(sd, ctrl->val);
1228         break;
1229     case V4L2_CID_AUTOGAIN:
1230         ret = ov5647_s_autogain(sd, ctrl->val);
1231         break;
1232     case V4L2_CID_EXPOSURE_AUTO:
1233         ret = ov5647_s_exposure_auto(sd, ctrl->val);
1234         break;
1235     case V4L2_CID_ANALOGUE_GAIN:
1236         ret =  ov5647_s_analogue_gain(sd, ctrl->val);
1237         break;
1238     case V4L2_CID_EXPOSURE:
1239         ret = ov5647_s_exposure(sd, ctrl->val);
1240         break;
1241     case V4L2_CID_VBLANK:
1242         ret = ov5647_write16(sd, OV5647_REG_VTS_HI,
1243                      sensor->mode->format.height + ctrl->val);
1244         break;
1245 
1246     /* Read-only, but we adjust it based on mode. */
1247     case V4L2_CID_PIXEL_RATE:
1248     case V4L2_CID_HBLANK:
1249         /* Read-only, but we adjust it based on mode. */
1250         break;
1251 
1252     default:
1253         dev_info(&client->dev,
1254              "Control (id:0x%x, val:0x%x) not supported\n",
1255              ctrl->id, ctrl->val);
1256         return -EINVAL;
1257     }
1258 
1259     pm_runtime_put(&client->dev);
1260 
1261     return ret;
1262 }
1263 
1264 static const struct v4l2_ctrl_ops ov5647_ctrl_ops = {
1265     .s_ctrl = ov5647_s_ctrl,
1266 };
1267 
1268 static int ov5647_init_controls(struct ov5647 *sensor)
1269 {
1270     struct i2c_client *client = v4l2_get_subdevdata(&sensor->sd);
1271     int hblank, exposure_max, exposure_def;
1272 
1273     v4l2_ctrl_handler_init(&sensor->ctrls, 8);
1274 
1275     v4l2_ctrl_new_std(&sensor->ctrls, &ov5647_ctrl_ops,
1276               V4L2_CID_AUTOGAIN, 0, 1, 1, 0);
1277 
1278     v4l2_ctrl_new_std(&sensor->ctrls, &ov5647_ctrl_ops,
1279               V4L2_CID_AUTO_WHITE_BALANCE, 0, 1, 1, 0);
1280 
1281     v4l2_ctrl_new_std_menu(&sensor->ctrls, &ov5647_ctrl_ops,
1282                    V4L2_CID_EXPOSURE_AUTO, V4L2_EXPOSURE_MANUAL,
1283                    0, V4L2_EXPOSURE_MANUAL);
1284 
1285     exposure_max = sensor->mode->vts - 4;
1286     exposure_def = min(exposure_max, OV5647_EXPOSURE_DEFAULT);
1287     sensor->exposure = v4l2_ctrl_new_std(&sensor->ctrls, &ov5647_ctrl_ops,
1288                          V4L2_CID_EXPOSURE,
1289                          OV5647_EXPOSURE_MIN,
1290                          exposure_max, OV5647_EXPOSURE_STEP,
1291                          exposure_def);
1292 
1293     /* min: 16 = 1.0x; max (10 bits); default: 32 = 2.0x. */
1294     v4l2_ctrl_new_std(&sensor->ctrls, &ov5647_ctrl_ops,
1295               V4L2_CID_ANALOGUE_GAIN, 16, 1023, 1, 32);
1296 
1297     /* By default, PIXEL_RATE is read only, but it does change per mode */
1298     sensor->pixel_rate = v4l2_ctrl_new_std(&sensor->ctrls, &ov5647_ctrl_ops,
1299                            V4L2_CID_PIXEL_RATE,
1300                            sensor->mode->pixel_rate,
1301                            sensor->mode->pixel_rate, 1,
1302                            sensor->mode->pixel_rate);
1303 
1304     /* By default, HBLANK is read only, but it does change per mode. */
1305     hblank = sensor->mode->hts - sensor->mode->format.width;
1306     sensor->hblank = v4l2_ctrl_new_std(&sensor->ctrls, &ov5647_ctrl_ops,
1307                        V4L2_CID_HBLANK, hblank, hblank, 1,
1308                        hblank);
1309 
1310     sensor->vblank = v4l2_ctrl_new_std(&sensor->ctrls, &ov5647_ctrl_ops,
1311                        V4L2_CID_VBLANK, OV5647_VBLANK_MIN,
1312                        OV5647_VTS_MAX -
1313                        sensor->mode->format.height, 1,
1314                        sensor->mode->vts -
1315                        sensor->mode->format.height);
1316 
1317     if (sensor->ctrls.error)
1318         goto handler_free;
1319 
1320     sensor->pixel_rate->flags |= V4L2_CTRL_FLAG_READ_ONLY;
1321     sensor->hblank->flags |= V4L2_CTRL_FLAG_READ_ONLY;
1322     sensor->sd.ctrl_handler = &sensor->ctrls;
1323 
1324     return 0;
1325 
1326 handler_free:
1327     dev_err(&client->dev, "%s Controls initialization failed (%d)\n",
1328         __func__, sensor->ctrls.error);
1329     v4l2_ctrl_handler_free(&sensor->ctrls);
1330 
1331     return sensor->ctrls.error;
1332 }
1333 
1334 static int ov5647_parse_dt(struct ov5647 *sensor, struct device_node *np)
1335 {
1336     struct v4l2_fwnode_endpoint bus_cfg = {
1337         .bus_type = V4L2_MBUS_CSI2_DPHY,
1338     };
1339     struct device_node *ep;
1340     int ret;
1341 
1342     ep = of_graph_get_next_endpoint(np, NULL);
1343     if (!ep)
1344         return -EINVAL;
1345 
1346     ret = v4l2_fwnode_endpoint_parse(of_fwnode_handle(ep), &bus_cfg);
1347     if (ret)
1348         goto out;
1349 
1350     sensor->clock_ncont = bus_cfg.bus.mipi_csi2.flags &
1351                   V4L2_MBUS_CSI2_NONCONTINUOUS_CLOCK;
1352 
1353 out:
1354     of_node_put(ep);
1355 
1356     return ret;
1357 }
1358 
1359 static int ov5647_probe(struct i2c_client *client)
1360 {
1361     struct device_node *np = client->dev.of_node;
1362     struct device *dev = &client->dev;
1363     struct ov5647 *sensor;
1364     struct v4l2_subdev *sd;
1365     u32 xclk_freq;
1366     int ret;
1367 
1368     sensor = devm_kzalloc(dev, sizeof(*sensor), GFP_KERNEL);
1369     if (!sensor)
1370         return -ENOMEM;
1371 
1372     if (IS_ENABLED(CONFIG_OF) && np) {
1373         ret = ov5647_parse_dt(sensor, np);
1374         if (ret) {
1375             dev_err(dev, "DT parsing error: %d\n", ret);
1376             return ret;
1377         }
1378     }
1379 
1380     sensor->xclk = devm_clk_get(dev, NULL);
1381     if (IS_ERR(sensor->xclk)) {
1382         dev_err(dev, "could not get xclk");
1383         return PTR_ERR(sensor->xclk);
1384     }
1385 
1386     xclk_freq = clk_get_rate(sensor->xclk);
1387     if (xclk_freq != 25000000) {
1388         dev_err(dev, "Unsupported clock frequency: %u\n", xclk_freq);
1389         return -EINVAL;
1390     }
1391 
1392     /* Request the power down GPIO asserted. */
1393     sensor->pwdn = devm_gpiod_get_optional(dev, "pwdn", GPIOD_OUT_HIGH);
1394     if (IS_ERR(sensor->pwdn)) {
1395         dev_err(dev, "Failed to get 'pwdn' gpio\n");
1396         return -EINVAL;
1397     }
1398 
1399     mutex_init(&sensor->lock);
1400 
1401     sensor->mode = OV5647_DEFAULT_MODE;
1402 
1403     ret = ov5647_init_controls(sensor);
1404     if (ret)
1405         goto mutex_destroy;
1406 
1407     sd = &sensor->sd;
1408     v4l2_i2c_subdev_init(sd, client, &ov5647_subdev_ops);
1409     sd->internal_ops = &ov5647_subdev_internal_ops;
1410     sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE | V4L2_SUBDEV_FL_HAS_EVENTS;
1411 
1412     sensor->pad.flags = MEDIA_PAD_FL_SOURCE;
1413     sd->entity.function = MEDIA_ENT_F_CAM_SENSOR;
1414     ret = media_entity_pads_init(&sd->entity, 1, &sensor->pad);
1415     if (ret < 0)
1416         goto ctrl_handler_free;
1417 
1418     ret = ov5647_power_on(dev);
1419     if (ret)
1420         goto entity_cleanup;
1421 
1422     ret = ov5647_detect(sd);
1423     if (ret < 0)
1424         goto power_off;
1425 
1426     ret = v4l2_async_register_subdev(sd);
1427     if (ret < 0)
1428         goto power_off;
1429 
1430     /* Enable runtime PM and turn off the device */
1431     pm_runtime_set_active(dev);
1432     pm_runtime_enable(dev);
1433     pm_runtime_idle(dev);
1434 
1435     dev_dbg(dev, "OmniVision OV5647 camera driver probed\n");
1436 
1437     return 0;
1438 
1439 power_off:
1440     ov5647_power_off(dev);
1441 entity_cleanup:
1442     media_entity_cleanup(&sd->entity);
1443 ctrl_handler_free:
1444     v4l2_ctrl_handler_free(&sensor->ctrls);
1445 mutex_destroy:
1446     mutex_destroy(&sensor->lock);
1447 
1448     return ret;
1449 }
1450 
1451 static int ov5647_remove(struct i2c_client *client)
1452 {
1453     struct v4l2_subdev *sd = i2c_get_clientdata(client);
1454     struct ov5647 *sensor = to_sensor(sd);
1455 
1456     v4l2_async_unregister_subdev(&sensor->sd);
1457     media_entity_cleanup(&sensor->sd.entity);
1458     v4l2_ctrl_handler_free(&sensor->ctrls);
1459     v4l2_device_unregister_subdev(sd);
1460     pm_runtime_disable(&client->dev);
1461     mutex_destroy(&sensor->lock);
1462 
1463     return 0;
1464 }
1465 
1466 static const struct dev_pm_ops ov5647_pm_ops = {
1467     SET_RUNTIME_PM_OPS(ov5647_power_off, ov5647_power_on, NULL)
1468 };
1469 
1470 static const struct i2c_device_id ov5647_id[] = {
1471     { "ov5647", 0 },
1472     { /* sentinel */ }
1473 };
1474 MODULE_DEVICE_TABLE(i2c, ov5647_id);
1475 
1476 #if IS_ENABLED(CONFIG_OF)
1477 static const struct of_device_id ov5647_of_match[] = {
1478     { .compatible = "ovti,ov5647" },
1479     { /* sentinel */ },
1480 };
1481 MODULE_DEVICE_TABLE(of, ov5647_of_match);
1482 #endif
1483 
1484 static struct i2c_driver ov5647_driver = {
1485     .driver = {
1486         .of_match_table = of_match_ptr(ov5647_of_match),
1487         .name   = "ov5647",
1488         .pm = &ov5647_pm_ops,
1489     },
1490     .probe_new  = ov5647_probe,
1491     .remove     = ov5647_remove,
1492     .id_table   = ov5647_id,
1493 };
1494 
1495 module_i2c_driver(ov5647_driver);
1496 
1497 MODULE_AUTHOR("Ramiro Oliveira <roliveir@synopsys.com>");
1498 MODULE_DESCRIPTION("A low-level driver for OmniVision ov5647 sensors");
1499 MODULE_LICENSE("GPL v2");