Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * ov772x Camera Driver
0004  *
0005  * Copyright (C) 2017 Jacopo Mondi <jacopo+renesas@jmondi.org>
0006  *
0007  * Copyright (C) 2008 Renesas Solutions Corp.
0008  * Kuninori Morimoto <morimoto.kuninori@renesas.com>
0009  *
0010  * Based on ov7670 and soc_camera_platform driver,
0011  *
0012  * Copyright 2006-7 Jonathan Corbet <corbet@lwn.net>
0013  * Copyright (C) 2008 Magnus Damm
0014  * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de>
0015  */
0016 
0017 #include <linux/clk.h>
0018 #include <linux/delay.h>
0019 #include <linux/gpio/consumer.h>
0020 #include <linux/i2c.h>
0021 #include <linux/init.h>
0022 #include <linux/kernel.h>
0023 #include <linux/module.h>
0024 #include <linux/regmap.h>
0025 #include <linux/slab.h>
0026 #include <linux/v4l2-mediabus.h>
0027 #include <linux/videodev2.h>
0028 
0029 #include <media/i2c/ov772x.h>
0030 
0031 #include <media/v4l2-ctrls.h>
0032 #include <media/v4l2-device.h>
0033 #include <media/v4l2-event.h>
0034 #include <media/v4l2-fwnode.h>
0035 #include <media/v4l2-image-sizes.h>
0036 #include <media/v4l2-subdev.h>
0037 
0038 /*
0039  * register offset
0040  */
0041 #define GAIN        0x00 /* AGC - Gain control gain setting */
0042 #define BLUE        0x01 /* AWB - Blue channel gain setting */
0043 #define RED         0x02 /* AWB - Red   channel gain setting */
0044 #define GREEN       0x03 /* AWB - Green channel gain setting */
0045 #define COM1        0x04 /* Common control 1 */
0046 #define BAVG        0x05 /* U/B Average Level */
0047 #define GAVG        0x06 /* Y/Gb Average Level */
0048 #define RAVG        0x07 /* V/R Average Level */
0049 #define AECH        0x08 /* Exposure Value - AEC MSBs */
0050 #define COM2        0x09 /* Common control 2 */
0051 #define PID         0x0A /* Product ID Number MSB */
0052 #define VER         0x0B /* Product ID Number LSB */
0053 #define COM3        0x0C /* Common control 3 */
0054 #define COM4        0x0D /* Common control 4 */
0055 #define COM5        0x0E /* Common control 5 */
0056 #define COM6        0x0F /* Common control 6 */
0057 #define AEC         0x10 /* Exposure Value */
0058 #define CLKRC       0x11 /* Internal clock */
0059 #define COM7        0x12 /* Common control 7 */
0060 #define COM8        0x13 /* Common control 8 */
0061 #define COM9        0x14 /* Common control 9 */
0062 #define COM10       0x15 /* Common control 10 */
0063 #define REG16       0x16 /* Register 16 */
0064 #define HSTART      0x17 /* Horizontal sensor size */
0065 #define HSIZE       0x18 /* Horizontal frame (HREF column) end high 8-bit */
0066 #define VSTART      0x19 /* Vertical frame (row) start high 8-bit */
0067 #define VSIZE       0x1A /* Vertical sensor size */
0068 #define PSHFT       0x1B /* Data format - pixel delay select */
0069 #define MIDH        0x1C /* Manufacturer ID byte - high */
0070 #define MIDL        0x1D /* Manufacturer ID byte - low  */
0071 #define LAEC        0x1F /* Fine AEC value */
0072 #define COM11       0x20 /* Common control 11 */
0073 #define BDBASE      0x22 /* Banding filter Minimum AEC value */
0074 #define DBSTEP      0x23 /* Banding filter Maximum Setp */
0075 #define AEW         0x24 /* AGC/AEC - Stable operating region (upper limit) */
0076 #define AEB         0x25 /* AGC/AEC - Stable operating region (lower limit) */
0077 #define VPT         0x26 /* AGC/AEC Fast mode operating region */
0078 #define REG28       0x28 /* Register 28 */
0079 #define HOUTSIZE    0x29 /* Horizontal data output size MSBs */
0080 #define EXHCH       0x2A /* Dummy pixel insert MSB */
0081 #define EXHCL       0x2B /* Dummy pixel insert LSB */
0082 #define VOUTSIZE    0x2C /* Vertical data output size MSBs */
0083 #define ADVFL       0x2D /* LSB of insert dummy lines in Vertical direction */
0084 #define ADVFH       0x2E /* MSG of insert dummy lines in Vertical direction */
0085 #define YAVE        0x2F /* Y/G Channel Average value */
0086 #define LUMHTH      0x30 /* Histogram AEC/AGC Luminance high level threshold */
0087 #define LUMLTH      0x31 /* Histogram AEC/AGC Luminance low  level threshold */
0088 #define HREF        0x32 /* Image start and size control */
0089 #define DM_LNL      0x33 /* Dummy line low  8 bits */
0090 #define DM_LNH      0x34 /* Dummy line high 8 bits */
0091 #define ADOFF_B     0x35 /* AD offset compensation value for B  channel */
0092 #define ADOFF_R     0x36 /* AD offset compensation value for R  channel */
0093 #define ADOFF_GB    0x37 /* AD offset compensation value for Gb channel */
0094 #define ADOFF_GR    0x38 /* AD offset compensation value for Gr channel */
0095 #define OFF_B       0x39 /* Analog process B  channel offset value */
0096 #define OFF_R       0x3A /* Analog process R  channel offset value */
0097 #define OFF_GB      0x3B /* Analog process Gb channel offset value */
0098 #define OFF_GR      0x3C /* Analog process Gr channel offset value */
0099 #define COM12       0x3D /* Common control 12 */
0100 #define COM13       0x3E /* Common control 13 */
0101 #define COM14       0x3F /* Common control 14 */
0102 #define COM15       0x40 /* Common control 15*/
0103 #define COM16       0x41 /* Common control 16 */
0104 #define TGT_B       0x42 /* BLC blue channel target value */
0105 #define TGT_R       0x43 /* BLC red  channel target value */
0106 #define TGT_GB      0x44 /* BLC Gb   channel target value */
0107 #define TGT_GR      0x45 /* BLC Gr   channel target value */
0108 /* for ov7720 */
0109 #define LCC0        0x46 /* Lens correction control 0 */
0110 #define LCC1        0x47 /* Lens correction option 1 - X coordinate */
0111 #define LCC2        0x48 /* Lens correction option 2 - Y coordinate */
0112 #define LCC3        0x49 /* Lens correction option 3 */
0113 #define LCC4        0x4A /* Lens correction option 4 - radius of the circular */
0114 #define LCC5        0x4B /* Lens correction option 5 */
0115 #define LCC6        0x4C /* Lens correction option 6 */
0116 /* for ov7725 */
0117 #define LC_CTR      0x46 /* Lens correction control */
0118 #define LC_XC       0x47 /* X coordinate of lens correction center relative */
0119 #define LC_YC       0x48 /* Y coordinate of lens correction center relative */
0120 #define LC_COEF     0x49 /* Lens correction coefficient */
0121 #define LC_RADI     0x4A /* Lens correction radius */
0122 #define LC_COEFB    0x4B /* Lens B channel compensation coefficient */
0123 #define LC_COEFR    0x4C /* Lens R channel compensation coefficient */
0124 
0125 #define FIXGAIN     0x4D /* Analog fix gain amplifer */
0126 #define AREF0       0x4E /* Sensor reference control */
0127 #define AREF1       0x4F /* Sensor reference current control */
0128 #define AREF2       0x50 /* Analog reference control */
0129 #define AREF3       0x51 /* ADC    reference control */
0130 #define AREF4       0x52 /* ADC    reference control */
0131 #define AREF5       0x53 /* ADC    reference control */
0132 #define AREF6       0x54 /* Analog reference control */
0133 #define AREF7       0x55 /* Analog reference control */
0134 #define UFIX        0x60 /* U channel fixed value output */
0135 #define VFIX        0x61 /* V channel fixed value output */
0136 #define AWBB_BLK    0x62 /* AWB option for advanced AWB */
0137 #define AWB_CTRL0   0x63 /* AWB control byte 0 */
0138 #define DSP_CTRL1   0x64 /* DSP control byte 1 */
0139 #define DSP_CTRL2   0x65 /* DSP control byte 2 */
0140 #define DSP_CTRL3   0x66 /* DSP control byte 3 */
0141 #define DSP_CTRL4   0x67 /* DSP control byte 4 */
0142 #define AWB_BIAS    0x68 /* AWB BLC level clip */
0143 #define AWB_CTRL1   0x69 /* AWB control  1 */
0144 #define AWB_CTRL2   0x6A /* AWB control  2 */
0145 #define AWB_CTRL3   0x6B /* AWB control  3 */
0146 #define AWB_CTRL4   0x6C /* AWB control  4 */
0147 #define AWB_CTRL5   0x6D /* AWB control  5 */
0148 #define AWB_CTRL6   0x6E /* AWB control  6 */
0149 #define AWB_CTRL7   0x6F /* AWB control  7 */
0150 #define AWB_CTRL8   0x70 /* AWB control  8 */
0151 #define AWB_CTRL9   0x71 /* AWB control  9 */
0152 #define AWB_CTRL10  0x72 /* AWB control 10 */
0153 #define AWB_CTRL11  0x73 /* AWB control 11 */
0154 #define AWB_CTRL12  0x74 /* AWB control 12 */
0155 #define AWB_CTRL13  0x75 /* AWB control 13 */
0156 #define AWB_CTRL14  0x76 /* AWB control 14 */
0157 #define AWB_CTRL15  0x77 /* AWB control 15 */
0158 #define AWB_CTRL16  0x78 /* AWB control 16 */
0159 #define AWB_CTRL17  0x79 /* AWB control 17 */
0160 #define AWB_CTRL18  0x7A /* AWB control 18 */
0161 #define AWB_CTRL19  0x7B /* AWB control 19 */
0162 #define AWB_CTRL20  0x7C /* AWB control 20 */
0163 #define AWB_CTRL21  0x7D /* AWB control 21 */
0164 #define GAM1        0x7E /* Gamma Curve  1st segment input end point */
0165 #define GAM2        0x7F /* Gamma Curve  2nd segment input end point */
0166 #define GAM3        0x80 /* Gamma Curve  3rd segment input end point */
0167 #define GAM4        0x81 /* Gamma Curve  4th segment input end point */
0168 #define GAM5        0x82 /* Gamma Curve  5th segment input end point */
0169 #define GAM6        0x83 /* Gamma Curve  6th segment input end point */
0170 #define GAM7        0x84 /* Gamma Curve  7th segment input end point */
0171 #define GAM8        0x85 /* Gamma Curve  8th segment input end point */
0172 #define GAM9        0x86 /* Gamma Curve  9th segment input end point */
0173 #define GAM10       0x87 /* Gamma Curve 10th segment input end point */
0174 #define GAM11       0x88 /* Gamma Curve 11th segment input end point */
0175 #define GAM12       0x89 /* Gamma Curve 12th segment input end point */
0176 #define GAM13       0x8A /* Gamma Curve 13th segment input end point */
0177 #define GAM14       0x8B /* Gamma Curve 14th segment input end point */
0178 #define GAM15       0x8C /* Gamma Curve 15th segment input end point */
0179 #define SLOP        0x8D /* Gamma curve highest segment slope */
0180 #define DNSTH       0x8E /* De-noise threshold */
0181 #define EDGE_STRNGT 0x8F /* Edge strength  control when manual mode */
0182 #define EDGE_TRSHLD 0x90 /* Edge threshold control when manual mode */
0183 #define DNSOFF      0x91 /* Auto De-noise threshold control */
0184 #define EDGE_UPPER  0x92 /* Edge strength upper limit when Auto mode */
0185 #define EDGE_LOWER  0x93 /* Edge strength lower limit when Auto mode */
0186 #define MTX1        0x94 /* Matrix coefficient 1 */
0187 #define MTX2        0x95 /* Matrix coefficient 2 */
0188 #define MTX3        0x96 /* Matrix coefficient 3 */
0189 #define MTX4        0x97 /* Matrix coefficient 4 */
0190 #define MTX5        0x98 /* Matrix coefficient 5 */
0191 #define MTX6        0x99 /* Matrix coefficient 6 */
0192 #define MTX_CTRL    0x9A /* Matrix control */
0193 #define BRIGHT      0x9B /* Brightness control */
0194 #define CNTRST      0x9C /* Contrast contrast */
0195 #define CNTRST_CTRL 0x9D /* Contrast contrast center */
0196 #define UVAD_J0     0x9E /* Auto UV adjust contrast 0 */
0197 #define UVAD_J1     0x9F /* Auto UV adjust contrast 1 */
0198 #define SCAL0       0xA0 /* Scaling control 0 */
0199 #define SCAL1       0xA1 /* Scaling control 1 */
0200 #define SCAL2       0xA2 /* Scaling control 2 */
0201 #define FIFODLYM    0xA3 /* FIFO manual mode delay control */
0202 #define FIFODLYA    0xA4 /* FIFO auto   mode delay control */
0203 #define SDE         0xA6 /* Special digital effect control */
0204 #define USAT        0xA7 /* U component saturation control */
0205 #define VSAT        0xA8 /* V component saturation control */
0206 /* for ov7720 */
0207 #define HUE0        0xA9 /* Hue control 0 */
0208 #define HUE1        0xAA /* Hue control 1 */
0209 /* for ov7725 */
0210 #define HUECOS      0xA9 /* Cosine value */
0211 #define HUESIN      0xAA /* Sine value */
0212 
0213 #define SIGN        0xAB /* Sign bit for Hue and contrast */
0214 #define DSPAUTO     0xAC /* DSP auto function ON/OFF control */
0215 
0216 /*
0217  * register detail
0218  */
0219 
0220 /* COM2 */
0221 #define SOFT_SLEEP_MODE 0x10    /* Soft sleep mode */
0222                 /* Output drive capability */
0223 #define OCAP_1x         0x00    /* 1x */
0224 #define OCAP_2x         0x01    /* 2x */
0225 #define OCAP_3x         0x02    /* 3x */
0226 #define OCAP_4x         0x03    /* 4x */
0227 
0228 /* COM3 */
0229 #define SWAP_MASK       (SWAP_RGB | SWAP_YUV | SWAP_ML)
0230 #define IMG_MASK        (VFLIP_IMG | HFLIP_IMG | SCOLOR_TEST)
0231 
0232 #define VFLIP_IMG       0x80    /* Vertical flip image ON/OFF selection */
0233 #define HFLIP_IMG       0x40    /* Horizontal mirror image ON/OFF selection */
0234 #define SWAP_RGB        0x20    /* Swap B/R  output sequence in RGB mode */
0235 #define SWAP_YUV        0x10    /* Swap Y/UV output sequence in YUV mode */
0236 #define SWAP_ML         0x08    /* Swap output MSB/LSB */
0237                 /* Tri-state option for output clock */
0238 #define NOTRI_CLOCK     0x04    /*   0: Tri-state    at this period */
0239                 /*   1: No tri-state at this period */
0240                 /* Tri-state option for output data */
0241 #define NOTRI_DATA      0x02    /*   0: Tri-state    at this period */
0242                 /*   1: No tri-state at this period */
0243 #define SCOLOR_TEST     0x01    /* Sensor color bar test pattern */
0244 
0245 /* COM4 */
0246                 /* PLL frequency control */
0247 #define PLL_BYPASS      0x00    /*  00: Bypass PLL */
0248 #define PLL_4x          0x40    /*  01: PLL 4x */
0249 #define PLL_6x          0x80    /*  10: PLL 6x */
0250 #define PLL_8x          0xc0    /*  11: PLL 8x */
0251                 /* AEC evaluate window */
0252 #define AEC_FULL        0x00    /*  00: Full window */
0253 #define AEC_1p2         0x10    /*  01: 1/2  window */
0254 #define AEC_1p4         0x20    /*  10: 1/4  window */
0255 #define AEC_2p3         0x30    /*  11: Low 2/3 window */
0256 #define COM4_RESERVED   0x01    /* Reserved bit */
0257 
0258 /* COM5 */
0259 #define AFR_ON_OFF      0x80    /* Auto frame rate control ON/OFF selection */
0260 #define AFR_SPPED       0x40    /* Auto frame rate control speed selection */
0261                 /* Auto frame rate max rate control */
0262 #define AFR_NO_RATE     0x00    /*     No  reduction of frame rate */
0263 #define AFR_1p2         0x10    /*     Max reduction to 1/2 frame rate */
0264 #define AFR_1p4         0x20    /*     Max reduction to 1/4 frame rate */
0265 #define AFR_1p8         0x30    /* Max reduction to 1/8 frame rate */
0266                 /* Auto frame rate active point control */
0267 #define AF_2x           0x00    /*     Add frame when AGC reaches  2x gain */
0268 #define AF_4x           0x04    /*     Add frame when AGC reaches  4x gain */
0269 #define AF_8x           0x08    /*     Add frame when AGC reaches  8x gain */
0270 #define AF_16x          0x0c    /* Add frame when AGC reaches 16x gain */
0271                 /* AEC max step control */
0272 #define AEC_NO_LIMIT    0x01    /*   0 : AEC incease step has limit */
0273                 /*   1 : No limit to AEC increase step */
0274 /* CLKRC */
0275                 /* Input clock divider register */
0276 #define CLKRC_RESERVED  0x80    /* Reserved bit */
0277 #define CLKRC_DIV(n)    ((n) - 1)
0278 
0279 /* COM7 */
0280                 /* SCCB Register Reset */
0281 #define SCCB_RESET      0x80    /*   0 : No change */
0282                 /*   1 : Resets all registers to default */
0283                 /* Resolution selection */
0284 #define SLCT_MASK       0x40    /*   Mask of VGA or QVGA */
0285 #define SLCT_VGA        0x00    /*   0 : VGA */
0286 #define SLCT_QVGA       0x40    /*   1 : QVGA */
0287 #define ITU656_ON_OFF   0x20    /* ITU656 protocol ON/OFF selection */
0288 #define SENSOR_RAW  0x10    /* Sensor RAW */
0289                 /* RGB output format control */
0290 #define FMT_MASK        0x0c    /*      Mask of color format */
0291 #define FMT_GBR422      0x00    /*      00 : GBR 4:2:2 */
0292 #define FMT_RGB565      0x04    /*      01 : RGB 565 */
0293 #define FMT_RGB555      0x08    /*      10 : RGB 555 */
0294 #define FMT_RGB444      0x0c    /* 11 : RGB 444 */
0295                 /* Output format control */
0296 #define OFMT_MASK       0x03    /*      Mask of output format */
0297 #define OFMT_YUV        0x00    /*      00 : YUV */
0298 #define OFMT_P_BRAW     0x01    /*      01 : Processed Bayer RAW */
0299 #define OFMT_RGB        0x02    /*      10 : RGB */
0300 #define OFMT_BRAW       0x03    /* 11 : Bayer RAW */
0301 
0302 /* COM8 */
0303 #define FAST_ALGO       0x80    /* Enable fast AGC/AEC algorithm */
0304                 /* AEC Setp size limit */
0305 #define UNLMT_STEP      0x40    /*   0 : Step size is limited */
0306                 /*   1 : Unlimited step size */
0307 #define BNDF_ON_OFF     0x20    /* Banding filter ON/OFF */
0308 #define AEC_BND         0x10    /* Enable AEC below banding value */
0309 #define AEC_ON_OFF      0x08    /* Fine AEC ON/OFF control */
0310 #define AGC_ON          0x04    /* AGC Enable */
0311 #define AWB_ON          0x02    /* AWB Enable */
0312 #define AEC_ON          0x01    /* AEC Enable */
0313 
0314 /* COM9 */
0315 #define BASE_AECAGC     0x80    /* Histogram or average based AEC/AGC */
0316                 /* Automatic gain ceiling - maximum AGC value */
0317 #define GAIN_2x         0x00    /*    000 :   2x */
0318 #define GAIN_4x         0x10    /*    001 :   4x */
0319 #define GAIN_8x         0x20    /*    010 :   8x */
0320 #define GAIN_16x        0x30    /*    011 :  16x */
0321 #define GAIN_32x        0x40    /*    100 :  32x */
0322 #define GAIN_64x        0x50    /* 101 :  64x */
0323 #define GAIN_128x       0x60    /* 110 : 128x */
0324 #define DROP_VSYNC      0x04    /* Drop VSYNC output of corrupt frame */
0325 #define DROP_HREF       0x02    /* Drop HREF  output of corrupt frame */
0326 
0327 /* COM11 */
0328 #define SGLF_ON_OFF     0x02    /* Single frame ON/OFF selection */
0329 #define SGLF_TRIG       0x01    /* Single frame transfer trigger */
0330 
0331 /* HREF */
0332 #define HREF_VSTART_SHIFT   6   /* VSTART LSB */
0333 #define HREF_HSTART_SHIFT   4   /* HSTART 2 LSBs */
0334 #define HREF_VSIZE_SHIFT    2   /* VSIZE LSB */
0335 #define HREF_HSIZE_SHIFT    0   /* HSIZE 2 LSBs */
0336 
0337 /* EXHCH */
0338 #define EXHCH_VSIZE_SHIFT   2   /* VOUTSIZE LSB */
0339 #define EXHCH_HSIZE_SHIFT   0   /* HOUTSIZE 2 LSBs */
0340 
0341 /* DSP_CTRL1 */
0342 #define FIFO_ON         0x80    /* FIFO enable/disable selection */
0343 #define UV_ON_OFF       0x40    /* UV adjust function ON/OFF selection */
0344 #define YUV444_2_422    0x20    /* YUV444 to 422 UV channel option selection */
0345 #define CLR_MTRX_ON_OFF 0x10    /* Color matrix ON/OFF selection */
0346 #define INTPLT_ON_OFF   0x08    /* Interpolation ON/OFF selection */
0347 #define GMM_ON_OFF      0x04    /* Gamma function ON/OFF selection */
0348 #define AUTO_BLK_ON_OFF 0x02    /* Black defect auto correction ON/OFF */
0349 #define AUTO_WHT_ON_OFF 0x01    /* White define auto correction ON/OFF */
0350 
0351 /* DSP_CTRL3 */
0352 #define UV_MASK         0x80    /* UV output sequence option */
0353 #define UV_ON           0x80    /*   ON */
0354 #define UV_OFF          0x00    /*   OFF */
0355 #define CBAR_MASK       0x20    /* DSP Color bar mask */
0356 #define CBAR_ON         0x20    /*   ON */
0357 #define CBAR_OFF        0x00    /*   OFF */
0358 
0359 /* DSP_CTRL4 */
0360 #define DSP_OFMT_YUV    0x00
0361 #define DSP_OFMT_RGB    0x00
0362 #define DSP_OFMT_RAW8   0x02
0363 #define DSP_OFMT_RAW10  0x03
0364 
0365 /* DSPAUTO (DSP Auto Function ON/OFF Control) */
0366 #define AWB_ACTRL       0x80 /* AWB auto threshold control */
0367 #define DENOISE_ACTRL   0x40 /* De-noise auto threshold control */
0368 #define EDGE_ACTRL      0x20 /* Edge enhancement auto strength control */
0369 #define UV_ACTRL        0x10 /* UV adjust auto slope control */
0370 #define SCAL0_ACTRL     0x08 /* Auto scaling factor control */
0371 #define SCAL1_2_ACTRL   0x04 /* Auto scaling factor control */
0372 
0373 #define OV772X_MAX_WIDTH    VGA_WIDTH
0374 #define OV772X_MAX_HEIGHT   VGA_HEIGHT
0375 
0376 /*
0377  * ID
0378  */
0379 #define OV7720  0x7720
0380 #define OV7725  0x7721
0381 #define VERSION(pid, ver) ((pid << 8) | (ver & 0xFF))
0382 
0383 /*
0384  * PLL multipliers
0385  */
0386 static struct {
0387     unsigned int mult;
0388     u8 com4;
0389 } ov772x_pll[] = {
0390     { 1, PLL_BYPASS, },
0391     { 4, PLL_4x, },
0392     { 6, PLL_6x, },
0393     { 8, PLL_8x, },
0394 };
0395 
0396 /*
0397  * struct
0398  */
0399 
0400 struct ov772x_color_format {
0401     u32 code;
0402     enum v4l2_colorspace colorspace;
0403     u8 dsp3;
0404     u8 dsp4;
0405     u8 com3;
0406     u8 com7;
0407 };
0408 
0409 struct ov772x_win_size {
0410     char                     *name;
0411     unsigned char             com7_bit;
0412     unsigned int          sizeimage;
0413     struct v4l2_rect      rect;
0414 };
0415 
0416 struct ov772x_priv {
0417     struct v4l2_subdev                subdev;
0418     struct v4l2_ctrl_handler      hdl;
0419     struct clk           *clk;
0420     struct regmap            *regmap;
0421     struct ov772x_camera_info        *info;
0422     struct gpio_desc         *pwdn_gpio;
0423     struct gpio_desc         *rstb_gpio;
0424     const struct ov772x_color_format *cfmt;
0425     const struct ov772x_win_size     *win;
0426     struct v4l2_ctrl         *vflip_ctrl;
0427     struct v4l2_ctrl         *hflip_ctrl;
0428     unsigned int              test_pattern;
0429     /* band_filter = COM8[5] ? 256 - BDBASE : 0 */
0430     struct v4l2_ctrl         *band_filter_ctrl;
0431     unsigned int              fps;
0432     /* lock to protect power_count and streaming */
0433     struct mutex              lock;
0434     int               power_count;
0435     int               streaming;
0436 #ifdef CONFIG_MEDIA_CONTROLLER
0437     struct media_pad pad;
0438 #endif
0439     enum v4l2_mbus_type       bus_type;
0440 };
0441 
0442 /*
0443  * supported color format list
0444  */
0445 static const struct ov772x_color_format ov772x_cfmts[] = {
0446     {
0447         .code       = MEDIA_BUS_FMT_YUYV8_2X8,
0448         .colorspace = V4L2_COLORSPACE_SRGB,
0449         .dsp3       = 0x0,
0450         .dsp4       = DSP_OFMT_YUV,
0451         .com3       = SWAP_YUV,
0452         .com7       = OFMT_YUV,
0453     },
0454     {
0455         .code       = MEDIA_BUS_FMT_YVYU8_2X8,
0456         .colorspace = V4L2_COLORSPACE_SRGB,
0457         .dsp3       = UV_ON,
0458         .dsp4       = DSP_OFMT_YUV,
0459         .com3       = SWAP_YUV,
0460         .com7       = OFMT_YUV,
0461     },
0462     {
0463         .code       = MEDIA_BUS_FMT_UYVY8_2X8,
0464         .colorspace = V4L2_COLORSPACE_SRGB,
0465         .dsp3       = 0x0,
0466         .dsp4       = DSP_OFMT_YUV,
0467         .com3       = 0x0,
0468         .com7       = OFMT_YUV,
0469     },
0470     {
0471         .code       = MEDIA_BUS_FMT_RGB555_2X8_PADHI_LE,
0472         .colorspace = V4L2_COLORSPACE_SRGB,
0473         .dsp3       = 0x0,
0474         .dsp4       = DSP_OFMT_YUV,
0475         .com3       = SWAP_RGB,
0476         .com7       = FMT_RGB555 | OFMT_RGB,
0477     },
0478     {
0479         .code       = MEDIA_BUS_FMT_RGB555_2X8_PADHI_BE,
0480         .colorspace = V4L2_COLORSPACE_SRGB,
0481         .dsp3       = 0x0,
0482         .dsp4       = DSP_OFMT_YUV,
0483         .com3       = 0x0,
0484         .com7       = FMT_RGB555 | OFMT_RGB,
0485     },
0486     {
0487         .code       = MEDIA_BUS_FMT_RGB565_2X8_LE,
0488         .colorspace = V4L2_COLORSPACE_SRGB,
0489         .dsp3       = 0x0,
0490         .dsp4       = DSP_OFMT_YUV,
0491         .com3       = SWAP_RGB,
0492         .com7       = FMT_RGB565 | OFMT_RGB,
0493     },
0494     {
0495         .code       = MEDIA_BUS_FMT_RGB565_2X8_BE,
0496         .colorspace = V4L2_COLORSPACE_SRGB,
0497         .dsp3       = 0x0,
0498         .dsp4       = DSP_OFMT_YUV,
0499         .com3       = 0x0,
0500         .com7       = FMT_RGB565 | OFMT_RGB,
0501     },
0502     {
0503         /* Setting DSP4 to DSP_OFMT_RAW8 still gives 10-bit output,
0504          * regardless of the COM7 value. We can thus only support 10-bit
0505          * Bayer until someone figures it out.
0506          */
0507         .code       = MEDIA_BUS_FMT_SBGGR10_1X10,
0508         .colorspace = V4L2_COLORSPACE_SRGB,
0509         .dsp3       = 0x0,
0510         .dsp4       = DSP_OFMT_RAW10,
0511         .com3       = 0x0,
0512         .com7       = SENSOR_RAW | OFMT_BRAW,
0513     },
0514 };
0515 
0516 /*
0517  * window size list
0518  */
0519 
0520 static const struct ov772x_win_size ov772x_win_sizes[] = {
0521     {
0522         .name       = "VGA",
0523         .com7_bit   = SLCT_VGA,
0524         .sizeimage  = 510 * 748,
0525         .rect = {
0526             .left   = 140,
0527             .top    = 14,
0528             .width  = VGA_WIDTH,
0529             .height = VGA_HEIGHT,
0530         },
0531     }, {
0532         .name       = "QVGA",
0533         .com7_bit   = SLCT_QVGA,
0534         .sizeimage  = 278 * 576,
0535         .rect = {
0536             .left   = 252,
0537             .top    = 6,
0538             .width  = QVGA_WIDTH,
0539             .height = QVGA_HEIGHT,
0540         },
0541     },
0542 };
0543 
0544 static const char * const ov772x_test_pattern_menu[] = {
0545     "Disabled",
0546     "Vertical Color Bar Type 1",
0547 };
0548 
0549 /*
0550  * frame rate settings lists
0551  */
0552 static const unsigned int ov772x_frame_intervals[] = { 5, 10, 15, 20, 30, 60 };
0553 
0554 /*
0555  * general function
0556  */
0557 
0558 static struct ov772x_priv *to_ov772x(struct v4l2_subdev *sd)
0559 {
0560     return container_of(sd, struct ov772x_priv, subdev);
0561 }
0562 
0563 static int ov772x_reset(struct ov772x_priv *priv)
0564 {
0565     int ret;
0566 
0567     ret = regmap_write(priv->regmap, COM7, SCCB_RESET);
0568     if (ret < 0)
0569         return ret;
0570 
0571     usleep_range(1000, 5000);
0572 
0573     return regmap_update_bits(priv->regmap, COM2, SOFT_SLEEP_MODE,
0574                   SOFT_SLEEP_MODE);
0575 }
0576 
0577 /*
0578  * subdev ops
0579  */
0580 
0581 static int ov772x_s_stream(struct v4l2_subdev *sd, int enable)
0582 {
0583     struct i2c_client *client = v4l2_get_subdevdata(sd);
0584     struct ov772x_priv *priv = to_ov772x(sd);
0585     int ret = 0;
0586 
0587     mutex_lock(&priv->lock);
0588 
0589     if (priv->streaming == enable)
0590         goto done;
0591 
0592     if (priv->bus_type == V4L2_MBUS_BT656) {
0593         ret = regmap_update_bits(priv->regmap, COM7, ITU656_ON_OFF,
0594                      enable ?
0595                      ITU656_ON_OFF : ~ITU656_ON_OFF);
0596         if (ret)
0597             goto done;
0598     }
0599 
0600     ret = regmap_update_bits(priv->regmap, COM2, SOFT_SLEEP_MODE,
0601                  enable ? 0 : SOFT_SLEEP_MODE);
0602     if (ret)
0603         goto done;
0604 
0605     if (enable) {
0606         dev_dbg(&client->dev, "format %d, win %s\n",
0607             priv->cfmt->code, priv->win->name);
0608     }
0609     priv->streaming = enable;
0610 
0611 done:
0612     mutex_unlock(&priv->lock);
0613 
0614     return ret;
0615 }
0616 
0617 static unsigned int ov772x_select_fps(struct ov772x_priv *priv,
0618                       struct v4l2_fract *tpf)
0619 {
0620     unsigned int fps = tpf->numerator ?
0621                tpf->denominator / tpf->numerator :
0622                tpf->denominator;
0623     unsigned int best_diff;
0624     unsigned int diff;
0625     unsigned int idx;
0626     unsigned int i;
0627 
0628     /* Approximate to the closest supported frame interval. */
0629     best_diff = ~0L;
0630     for (i = 0, idx = 0; i < ARRAY_SIZE(ov772x_frame_intervals); i++) {
0631         diff = abs(fps - ov772x_frame_intervals[i]);
0632         if (diff < best_diff) {
0633             idx = i;
0634             best_diff = diff;
0635         }
0636     }
0637 
0638     return ov772x_frame_intervals[idx];
0639 }
0640 
0641 static int ov772x_set_frame_rate(struct ov772x_priv *priv,
0642                  unsigned int fps,
0643                  const struct ov772x_color_format *cfmt,
0644                  const struct ov772x_win_size *win)
0645 {
0646     unsigned long fin = clk_get_rate(priv->clk);
0647     unsigned int best_diff;
0648     unsigned int fsize;
0649     unsigned int pclk;
0650     unsigned int diff;
0651     unsigned int i;
0652     u8 clkrc = 0;
0653     u8 com4 = 0;
0654     int ret;
0655 
0656     /* Use image size (with blankings) to calculate desired pixel clock. */
0657     switch (cfmt->com7 & OFMT_MASK) {
0658     case OFMT_BRAW:
0659         fsize = win->sizeimage;
0660         break;
0661     case OFMT_RGB:
0662     case OFMT_YUV:
0663     default:
0664         fsize = win->sizeimage * 2;
0665         break;
0666     }
0667 
0668     pclk = fps * fsize;
0669 
0670     /*
0671      * Pixel clock generation circuit is pretty simple:
0672      *
0673      * Fin -> [ / CLKRC_div] -> [ * PLL_mult] -> pclk
0674      *
0675      * Try to approximate the desired pixel clock testing all available
0676      * PLL multipliers (1x, 4x, 6x, 8x) and calculate corresponding
0677      * divisor with:
0678      *
0679      * div = PLL_mult * Fin / pclk
0680      *
0681      * and re-calculate the pixel clock using it:
0682      *
0683      * pclk = Fin * PLL_mult / CLKRC_div
0684      *
0685      * Choose the PLL_mult and CLKRC_div pair that gives a pixel clock
0686      * closer to the desired one.
0687      *
0688      * The desired pixel clock is calculated using a known frame size
0689      * (blanking included) and FPS.
0690      */
0691     best_diff = ~0L;
0692     for (i = 0; i < ARRAY_SIZE(ov772x_pll); i++) {
0693         unsigned int pll_mult = ov772x_pll[i].mult;
0694         unsigned int pll_out = pll_mult * fin;
0695         unsigned int t_pclk;
0696         unsigned int div;
0697 
0698         if (pll_out < pclk)
0699             continue;
0700 
0701         div = DIV_ROUND_CLOSEST(pll_out, pclk);
0702         t_pclk = DIV_ROUND_CLOSEST(fin * pll_mult, div);
0703         diff = abs(pclk - t_pclk);
0704         if (diff < best_diff) {
0705             best_diff = diff;
0706             clkrc = CLKRC_DIV(div);
0707             com4 = ov772x_pll[i].com4;
0708         }
0709     }
0710 
0711     ret = regmap_write(priv->regmap, COM4, com4 | COM4_RESERVED);
0712     if (ret < 0)
0713         return ret;
0714 
0715     ret = regmap_write(priv->regmap, CLKRC, clkrc | CLKRC_RESERVED);
0716     if (ret < 0)
0717         return ret;
0718 
0719     return 0;
0720 }
0721 
0722 static int ov772x_g_frame_interval(struct v4l2_subdev *sd,
0723                    struct v4l2_subdev_frame_interval *ival)
0724 {
0725     struct ov772x_priv *priv = to_ov772x(sd);
0726     struct v4l2_fract *tpf = &ival->interval;
0727 
0728     tpf->numerator = 1;
0729     tpf->denominator = priv->fps;
0730 
0731     return 0;
0732 }
0733 
0734 static int ov772x_s_frame_interval(struct v4l2_subdev *sd,
0735                    struct v4l2_subdev_frame_interval *ival)
0736 {
0737     struct ov772x_priv *priv = to_ov772x(sd);
0738     struct v4l2_fract *tpf = &ival->interval;
0739     unsigned int fps;
0740     int ret = 0;
0741 
0742     mutex_lock(&priv->lock);
0743 
0744     if (priv->streaming) {
0745         ret = -EBUSY;
0746         goto error;
0747     }
0748 
0749     fps = ov772x_select_fps(priv, tpf);
0750 
0751     /*
0752      * If the device is not powered up by the host driver do
0753      * not apply any changes to H/W at this time. Instead
0754      * the frame rate will be restored right after power-up.
0755      */
0756     if (priv->power_count > 0) {
0757         ret = ov772x_set_frame_rate(priv, fps, priv->cfmt, priv->win);
0758         if (ret)
0759             goto error;
0760     }
0761 
0762     tpf->numerator = 1;
0763     tpf->denominator = fps;
0764     priv->fps = fps;
0765 
0766 error:
0767     mutex_unlock(&priv->lock);
0768 
0769     return ret;
0770 }
0771 
0772 static int ov772x_s_ctrl(struct v4l2_ctrl *ctrl)
0773 {
0774     struct ov772x_priv *priv = container_of(ctrl->handler,
0775                         struct ov772x_priv, hdl);
0776     struct regmap *regmap = priv->regmap;
0777     int ret = 0;
0778     u8 val;
0779 
0780     /* v4l2_ctrl_lock() locks our own mutex */
0781 
0782     /*
0783      * If the device is not powered up by the host driver do
0784      * not apply any controls to H/W at this time. Instead
0785      * the controls will be restored right after power-up.
0786      */
0787     if (priv->power_count == 0)
0788         return 0;
0789 
0790     switch (ctrl->id) {
0791     case V4L2_CID_VFLIP:
0792         val = ctrl->val ? VFLIP_IMG : 0x00;
0793         if (priv->info && (priv->info->flags & OV772X_FLAG_VFLIP))
0794             val ^= VFLIP_IMG;
0795         return regmap_update_bits(regmap, COM3, VFLIP_IMG, val);
0796     case V4L2_CID_HFLIP:
0797         val = ctrl->val ? HFLIP_IMG : 0x00;
0798         if (priv->info && (priv->info->flags & OV772X_FLAG_HFLIP))
0799             val ^= HFLIP_IMG;
0800         return regmap_update_bits(regmap, COM3, HFLIP_IMG, val);
0801     case V4L2_CID_BAND_STOP_FILTER:
0802         if (!ctrl->val) {
0803             /* Switch the filter off, it is on now */
0804             ret = regmap_update_bits(regmap, BDBASE, 0xff, 0xff);
0805             if (!ret)
0806                 ret = regmap_update_bits(regmap, COM8,
0807                              BNDF_ON_OFF, 0);
0808         } else {
0809             /* Switch the filter on, set AEC low limit */
0810             val = 256 - ctrl->val;
0811             ret = regmap_update_bits(regmap, COM8,
0812                          BNDF_ON_OFF, BNDF_ON_OFF);
0813             if (!ret)
0814                 ret = regmap_update_bits(regmap, BDBASE,
0815                              0xff, val);
0816         }
0817 
0818         return ret;
0819     case V4L2_CID_TEST_PATTERN:
0820         priv->test_pattern = ctrl->val;
0821         return 0;
0822     }
0823 
0824     return -EINVAL;
0825 }
0826 
0827 #ifdef CONFIG_VIDEO_ADV_DEBUG
0828 static int ov772x_g_register(struct v4l2_subdev *sd,
0829                  struct v4l2_dbg_register *reg)
0830 {
0831     struct ov772x_priv *priv = to_ov772x(sd);
0832     int ret;
0833     unsigned int val;
0834 
0835     reg->size = 1;
0836     if (reg->reg > 0xff)
0837         return -EINVAL;
0838 
0839     ret = regmap_read(priv->regmap, reg->reg, &val);
0840     if (ret < 0)
0841         return ret;
0842 
0843     reg->val = (__u64)val;
0844 
0845     return 0;
0846 }
0847 
0848 static int ov772x_s_register(struct v4l2_subdev *sd,
0849                  const struct v4l2_dbg_register *reg)
0850 {
0851     struct ov772x_priv *priv = to_ov772x(sd);
0852 
0853     if (reg->reg > 0xff ||
0854         reg->val > 0xff)
0855         return -EINVAL;
0856 
0857     return regmap_write(priv->regmap, reg->reg, reg->val);
0858 }
0859 #endif
0860 
0861 static int ov772x_power_on(struct ov772x_priv *priv)
0862 {
0863     struct i2c_client *client = v4l2_get_subdevdata(&priv->subdev);
0864     int ret;
0865 
0866     if (priv->clk) {
0867         ret = clk_prepare_enable(priv->clk);
0868         if (ret)
0869             return ret;
0870     }
0871 
0872     if (priv->pwdn_gpio) {
0873         gpiod_set_value(priv->pwdn_gpio, 1);
0874         usleep_range(500, 1000);
0875     }
0876 
0877     /*
0878      * FIXME: The reset signal is connected to a shared GPIO on some
0879      * platforms (namely the SuperH Migo-R). Until a framework becomes
0880      * available to handle this cleanly, request the GPIO temporarily
0881      * to avoid conflicts.
0882      */
0883     priv->rstb_gpio = gpiod_get_optional(&client->dev, "reset",
0884                          GPIOD_OUT_LOW);
0885     if (IS_ERR(priv->rstb_gpio)) {
0886         dev_info(&client->dev, "Unable to get GPIO \"reset\"");
0887         clk_disable_unprepare(priv->clk);
0888         return PTR_ERR(priv->rstb_gpio);
0889     }
0890 
0891     if (priv->rstb_gpio) {
0892         gpiod_set_value(priv->rstb_gpio, 1);
0893         usleep_range(500, 1000);
0894         gpiod_set_value(priv->rstb_gpio, 0);
0895         usleep_range(500, 1000);
0896 
0897         gpiod_put(priv->rstb_gpio);
0898     }
0899 
0900     return 0;
0901 }
0902 
0903 static int ov772x_power_off(struct ov772x_priv *priv)
0904 {
0905     clk_disable_unprepare(priv->clk);
0906 
0907     if (priv->pwdn_gpio) {
0908         gpiod_set_value(priv->pwdn_gpio, 0);
0909         usleep_range(500, 1000);
0910     }
0911 
0912     return 0;
0913 }
0914 
0915 static int ov772x_set_params(struct ov772x_priv *priv,
0916                  const struct ov772x_color_format *cfmt,
0917                  const struct ov772x_win_size *win);
0918 
0919 static int ov772x_s_power(struct v4l2_subdev *sd, int on)
0920 {
0921     struct ov772x_priv *priv = to_ov772x(sd);
0922     int ret = 0;
0923 
0924     mutex_lock(&priv->lock);
0925 
0926     /* If the power count is modified from 0 to != 0 or from != 0 to 0,
0927      * update the power state.
0928      */
0929     if (priv->power_count == !on) {
0930         if (on) {
0931             ret = ov772x_power_on(priv);
0932             /*
0933              * Restore the format, the frame rate, and
0934              * the controls
0935              */
0936             if (!ret)
0937                 ret = ov772x_set_params(priv, priv->cfmt,
0938                             priv->win);
0939         } else {
0940             ret = ov772x_power_off(priv);
0941         }
0942     }
0943 
0944     if (!ret) {
0945         /* Update the power count. */
0946         priv->power_count += on ? 1 : -1;
0947         WARN(priv->power_count < 0, "Unbalanced power count\n");
0948         WARN(priv->power_count > 1, "Duplicated s_power call\n");
0949     }
0950 
0951     mutex_unlock(&priv->lock);
0952 
0953     return ret;
0954 }
0955 
0956 static const struct ov772x_win_size *ov772x_select_win(u32 width, u32 height)
0957 {
0958     const struct ov772x_win_size *win = &ov772x_win_sizes[0];
0959     u32 best_diff = UINT_MAX;
0960     unsigned int i;
0961 
0962     for (i = 0; i < ARRAY_SIZE(ov772x_win_sizes); ++i) {
0963         u32 diff = abs(width - ov772x_win_sizes[i].rect.width)
0964              + abs(height - ov772x_win_sizes[i].rect.height);
0965         if (diff < best_diff) {
0966             best_diff = diff;
0967             win = &ov772x_win_sizes[i];
0968         }
0969     }
0970 
0971     return win;
0972 }
0973 
0974 static void ov772x_select_params(const struct v4l2_mbus_framefmt *mf,
0975                  const struct ov772x_color_format **cfmt,
0976                  const struct ov772x_win_size **win)
0977 {
0978     unsigned int i;
0979 
0980     /* Select a format. */
0981     *cfmt = &ov772x_cfmts[0];
0982 
0983     for (i = 0; i < ARRAY_SIZE(ov772x_cfmts); i++) {
0984         if (mf->code == ov772x_cfmts[i].code) {
0985             *cfmt = &ov772x_cfmts[i];
0986             break;
0987         }
0988     }
0989 
0990     /* Select a window size. */
0991     *win = ov772x_select_win(mf->width, mf->height);
0992 }
0993 
0994 static int ov772x_edgectrl(struct ov772x_priv *priv)
0995 {
0996     struct regmap *regmap = priv->regmap;
0997     int ret;
0998 
0999     if (!priv->info)
1000         return 0;
1001 
1002     if (priv->info->edgectrl.strength & OV772X_MANUAL_EDGE_CTRL) {
1003         /*
1004          * Manual Edge Control Mode.
1005          *
1006          * Edge auto strength bit is set by default.
1007          * Remove it when manual mode.
1008          */
1009 
1010         ret = regmap_update_bits(regmap, DSPAUTO, EDGE_ACTRL, 0x00);
1011         if (ret < 0)
1012             return ret;
1013 
1014         ret = regmap_update_bits(regmap, EDGE_TRSHLD,
1015                      OV772X_EDGE_THRESHOLD_MASK,
1016                      priv->info->edgectrl.threshold);
1017         if (ret < 0)
1018             return ret;
1019 
1020         ret = regmap_update_bits(regmap, EDGE_STRNGT,
1021                      OV772X_EDGE_STRENGTH_MASK,
1022                      priv->info->edgectrl.strength);
1023         if (ret < 0)
1024             return ret;
1025 
1026     } else if (priv->info->edgectrl.upper > priv->info->edgectrl.lower) {
1027         /*
1028          * Auto Edge Control Mode.
1029          *
1030          * Set upper and lower limit.
1031          */
1032         ret = regmap_update_bits(regmap, EDGE_UPPER,
1033                      OV772X_EDGE_UPPER_MASK,
1034                      priv->info->edgectrl.upper);
1035         if (ret < 0)
1036             return ret;
1037 
1038         ret = regmap_update_bits(regmap, EDGE_LOWER,
1039                      OV772X_EDGE_LOWER_MASK,
1040                      priv->info->edgectrl.lower);
1041         if (ret < 0)
1042             return ret;
1043     }
1044 
1045     return 0;
1046 }
1047 
1048 static int ov772x_set_params(struct ov772x_priv *priv,
1049                  const struct ov772x_color_format *cfmt,
1050                  const struct ov772x_win_size *win)
1051 {
1052     int ret;
1053     u8  val;
1054 
1055     /* Reset hardware. */
1056     ov772x_reset(priv);
1057 
1058     /* Edge Ctrl. */
1059     ret = ov772x_edgectrl(priv);
1060     if (ret < 0)
1061         return ret;
1062 
1063     /* Format and window size. */
1064     ret = regmap_write(priv->regmap, HSTART, win->rect.left >> 2);
1065     if (ret < 0)
1066         goto ov772x_set_fmt_error;
1067     ret = regmap_write(priv->regmap, HSIZE, win->rect.width >> 2);
1068     if (ret < 0)
1069         goto ov772x_set_fmt_error;
1070     ret = regmap_write(priv->regmap, VSTART, win->rect.top >> 1);
1071     if (ret < 0)
1072         goto ov772x_set_fmt_error;
1073     ret = regmap_write(priv->regmap, VSIZE, win->rect.height >> 1);
1074     if (ret < 0)
1075         goto ov772x_set_fmt_error;
1076     ret = regmap_write(priv->regmap, HOUTSIZE, win->rect.width >> 2);
1077     if (ret < 0)
1078         goto ov772x_set_fmt_error;
1079     ret = regmap_write(priv->regmap, VOUTSIZE, win->rect.height >> 1);
1080     if (ret < 0)
1081         goto ov772x_set_fmt_error;
1082     ret = regmap_write(priv->regmap, HREF,
1083                ((win->rect.top & 1) << HREF_VSTART_SHIFT) |
1084                ((win->rect.left & 3) << HREF_HSTART_SHIFT) |
1085                ((win->rect.height & 1) << HREF_VSIZE_SHIFT) |
1086                ((win->rect.width & 3) << HREF_HSIZE_SHIFT));
1087     if (ret < 0)
1088         goto ov772x_set_fmt_error;
1089     ret = regmap_write(priv->regmap, EXHCH,
1090                ((win->rect.height & 1) << EXHCH_VSIZE_SHIFT) |
1091                ((win->rect.width & 3) << EXHCH_HSIZE_SHIFT));
1092     if (ret < 0)
1093         goto ov772x_set_fmt_error;
1094 
1095     /* Set DSP_CTRL3. */
1096     val = cfmt->dsp3;
1097     if (val) {
1098         ret = regmap_update_bits(priv->regmap, DSP_CTRL3, UV_MASK, val);
1099         if (ret < 0)
1100             goto ov772x_set_fmt_error;
1101     }
1102 
1103     /* DSP_CTRL4: AEC reference point and DSP output format. */
1104     if (cfmt->dsp4) {
1105         ret = regmap_write(priv->regmap, DSP_CTRL4, cfmt->dsp4);
1106         if (ret < 0)
1107             goto ov772x_set_fmt_error;
1108     }
1109 
1110     /* Set COM3. */
1111     val = cfmt->com3;
1112     if (priv->info && (priv->info->flags & OV772X_FLAG_VFLIP))
1113         val |= VFLIP_IMG;
1114     if (priv->info && (priv->info->flags & OV772X_FLAG_HFLIP))
1115         val |= HFLIP_IMG;
1116     if (priv->vflip_ctrl->val)
1117         val ^= VFLIP_IMG;
1118     if (priv->hflip_ctrl->val)
1119         val ^= HFLIP_IMG;
1120     if (priv->test_pattern)
1121         val |= SCOLOR_TEST;
1122 
1123     ret = regmap_update_bits(priv->regmap, COM3, SWAP_MASK | IMG_MASK, val);
1124     if (ret < 0)
1125         goto ov772x_set_fmt_error;
1126 
1127     /* COM7: Sensor resolution and output format control. */
1128     ret = regmap_write(priv->regmap, COM7, win->com7_bit | cfmt->com7);
1129     if (ret < 0)
1130         goto ov772x_set_fmt_error;
1131 
1132     /* COM4, CLKRC: Set pixel clock and framerate. */
1133     ret = ov772x_set_frame_rate(priv, priv->fps, cfmt, win);
1134     if (ret < 0)
1135         goto ov772x_set_fmt_error;
1136 
1137     /* Set COM8. */
1138     if (priv->band_filter_ctrl->val) {
1139         unsigned short band_filter = priv->band_filter_ctrl->val;
1140 
1141         ret = regmap_update_bits(priv->regmap, COM8,
1142                      BNDF_ON_OFF, BNDF_ON_OFF);
1143         if (!ret)
1144             ret = regmap_update_bits(priv->regmap, BDBASE,
1145                          0xff, 256 - band_filter);
1146         if (ret < 0)
1147             goto ov772x_set_fmt_error;
1148     }
1149 
1150     return ret;
1151 
1152 ov772x_set_fmt_error:
1153 
1154     ov772x_reset(priv);
1155 
1156     return ret;
1157 }
1158 
1159 static int ov772x_get_selection(struct v4l2_subdev *sd,
1160                 struct v4l2_subdev_state *sd_state,
1161                 struct v4l2_subdev_selection *sel)
1162 {
1163     struct ov772x_priv *priv = to_ov772x(sd);
1164 
1165     if (sel->which != V4L2_SUBDEV_FORMAT_ACTIVE)
1166         return -EINVAL;
1167 
1168     sel->r.left = 0;
1169     sel->r.top = 0;
1170     switch (sel->target) {
1171     case V4L2_SEL_TGT_CROP_BOUNDS:
1172     case V4L2_SEL_TGT_CROP:
1173         sel->r.width = priv->win->rect.width;
1174         sel->r.height = priv->win->rect.height;
1175         return 0;
1176     default:
1177         return -EINVAL;
1178     }
1179 }
1180 
1181 static int ov772x_get_fmt(struct v4l2_subdev *sd,
1182               struct v4l2_subdev_state *sd_state,
1183               struct v4l2_subdev_format *format)
1184 {
1185     struct v4l2_mbus_framefmt *mf = &format->format;
1186     struct ov772x_priv *priv = to_ov772x(sd);
1187 
1188     if (format->pad)
1189         return -EINVAL;
1190 
1191     mf->width   = priv->win->rect.width;
1192     mf->height  = priv->win->rect.height;
1193     mf->code    = priv->cfmt->code;
1194     mf->colorspace  = priv->cfmt->colorspace;
1195     mf->field   = V4L2_FIELD_NONE;
1196 
1197     return 0;
1198 }
1199 
1200 static int ov772x_set_fmt(struct v4l2_subdev *sd,
1201               struct v4l2_subdev_state *sd_state,
1202               struct v4l2_subdev_format *format)
1203 {
1204     struct ov772x_priv *priv = to_ov772x(sd);
1205     struct v4l2_mbus_framefmt *mf = &format->format;
1206     const struct ov772x_color_format *cfmt;
1207     const struct ov772x_win_size *win;
1208     int ret = 0;
1209 
1210     if (format->pad)
1211         return -EINVAL;
1212 
1213     ov772x_select_params(mf, &cfmt, &win);
1214 
1215     mf->code = cfmt->code;
1216     mf->width = win->rect.width;
1217     mf->height = win->rect.height;
1218     mf->field = V4L2_FIELD_NONE;
1219     mf->colorspace = cfmt->colorspace;
1220     mf->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
1221     mf->quantization = V4L2_QUANTIZATION_DEFAULT;
1222     mf->xfer_func = V4L2_XFER_FUNC_DEFAULT;
1223 
1224     if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
1225         sd_state->pads->try_fmt = *mf;
1226         return 0;
1227     }
1228 
1229     mutex_lock(&priv->lock);
1230 
1231     if (priv->streaming) {
1232         ret = -EBUSY;
1233         goto error;
1234     }
1235 
1236     /*
1237      * If the device is not powered up by the host driver do
1238      * not apply any changes to H/W at this time. Instead
1239      * the format will be restored right after power-up.
1240      */
1241     if (priv->power_count > 0) {
1242         ret = ov772x_set_params(priv, cfmt, win);
1243         if (ret < 0)
1244             goto error;
1245     }
1246     priv->win = win;
1247     priv->cfmt = cfmt;
1248 
1249 error:
1250     mutex_unlock(&priv->lock);
1251 
1252     return ret;
1253 }
1254 
1255 static int ov772x_video_probe(struct ov772x_priv *priv)
1256 {
1257     struct i2c_client  *client = v4l2_get_subdevdata(&priv->subdev);
1258     int         pid, ver, midh, midl;
1259     const char         *devname;
1260     int         ret;
1261 
1262     ret = ov772x_power_on(priv);
1263     if (ret < 0)
1264         return ret;
1265 
1266     /* Check and show product ID and manufacturer ID. */
1267     ret = regmap_read(priv->regmap, PID, &pid);
1268     if (ret < 0)
1269         return ret;
1270     ret = regmap_read(priv->regmap, VER, &ver);
1271     if (ret < 0)
1272         return ret;
1273 
1274     switch (VERSION(pid, ver)) {
1275     case OV7720:
1276         devname     = "ov7720";
1277         break;
1278     case OV7725:
1279         devname     = "ov7725";
1280         break;
1281     default:
1282         dev_err(&client->dev,
1283             "Product ID error %x:%x\n", pid, ver);
1284         ret = -ENODEV;
1285         goto done;
1286     }
1287 
1288     ret = regmap_read(priv->regmap, MIDH, &midh);
1289     if (ret < 0)
1290         return ret;
1291     ret = regmap_read(priv->regmap, MIDL, &midl);
1292     if (ret < 0)
1293         return ret;
1294 
1295     dev_info(&client->dev,
1296          "%s Product ID %0x:%0x Manufacturer ID %x:%x\n",
1297          devname, pid, ver, midh, midl);
1298 
1299     ret = v4l2_ctrl_handler_setup(&priv->hdl);
1300 
1301 done:
1302     ov772x_power_off(priv);
1303 
1304     return ret;
1305 }
1306 
1307 static const struct v4l2_ctrl_ops ov772x_ctrl_ops = {
1308     .s_ctrl = ov772x_s_ctrl,
1309 };
1310 
1311 static const struct v4l2_subdev_core_ops ov772x_subdev_core_ops = {
1312     .log_status = v4l2_ctrl_subdev_log_status,
1313     .subscribe_event = v4l2_ctrl_subdev_subscribe_event,
1314     .unsubscribe_event = v4l2_event_subdev_unsubscribe,
1315 #ifdef CONFIG_VIDEO_ADV_DEBUG
1316     .g_register = ov772x_g_register,
1317     .s_register = ov772x_s_register,
1318 #endif
1319     .s_power    = ov772x_s_power,
1320 };
1321 
1322 static int ov772x_enum_frame_interval(struct v4l2_subdev *sd,
1323                       struct v4l2_subdev_state *sd_state,
1324                       struct v4l2_subdev_frame_interval_enum *fie)
1325 {
1326     if (fie->pad || fie->index >= ARRAY_SIZE(ov772x_frame_intervals))
1327         return -EINVAL;
1328 
1329     if (fie->width != VGA_WIDTH && fie->width != QVGA_WIDTH)
1330         return -EINVAL;
1331     if (fie->height != VGA_HEIGHT && fie->height != QVGA_HEIGHT)
1332         return -EINVAL;
1333 
1334     fie->interval.numerator = 1;
1335     fie->interval.denominator = ov772x_frame_intervals[fie->index];
1336 
1337     return 0;
1338 }
1339 
1340 static int ov772x_enum_mbus_code(struct v4l2_subdev *sd,
1341                  struct v4l2_subdev_state *sd_state,
1342                  struct v4l2_subdev_mbus_code_enum *code)
1343 {
1344     if (code->pad || code->index >= ARRAY_SIZE(ov772x_cfmts))
1345         return -EINVAL;
1346 
1347     code->code = ov772x_cfmts[code->index].code;
1348 
1349     return 0;
1350 }
1351 
1352 static const struct v4l2_subdev_video_ops ov772x_subdev_video_ops = {
1353     .s_stream       = ov772x_s_stream,
1354     .s_frame_interval   = ov772x_s_frame_interval,
1355     .g_frame_interval   = ov772x_g_frame_interval,
1356 };
1357 
1358 static const struct v4l2_subdev_pad_ops ov772x_subdev_pad_ops = {
1359     .enum_frame_interval    = ov772x_enum_frame_interval,
1360     .enum_mbus_code     = ov772x_enum_mbus_code,
1361     .get_selection      = ov772x_get_selection,
1362     .get_fmt        = ov772x_get_fmt,
1363     .set_fmt        = ov772x_set_fmt,
1364 };
1365 
1366 static const struct v4l2_subdev_ops ov772x_subdev_ops = {
1367     .core   = &ov772x_subdev_core_ops,
1368     .video  = &ov772x_subdev_video_ops,
1369     .pad    = &ov772x_subdev_pad_ops,
1370 };
1371 
1372 static int ov772x_parse_dt(struct i2c_client *client,
1373                struct ov772x_priv *priv)
1374 {
1375     struct v4l2_fwnode_endpoint bus_cfg = {
1376         .bus_type = V4L2_MBUS_PARALLEL
1377     };
1378     struct fwnode_handle *ep;
1379     int ret;
1380 
1381     ep = fwnode_graph_get_next_endpoint(dev_fwnode(&client->dev), NULL);
1382     if (!ep) {
1383         dev_err(&client->dev, "Endpoint node not found\n");
1384         return -EINVAL;
1385     }
1386 
1387     /*
1388      * For backward compatibility with older DTS where the
1389      * bus-type property was not mandatory, assume
1390      * V4L2_MBUS_PARALLEL as it was the only supported bus at the
1391      * time. v4l2_fwnode_endpoint_alloc_parse() will not fail if
1392      * 'bus-type' is not specified.
1393      */
1394     ret = v4l2_fwnode_endpoint_alloc_parse(ep, &bus_cfg);
1395     if (ret) {
1396         bus_cfg = (struct v4l2_fwnode_endpoint)
1397               { .bus_type = V4L2_MBUS_BT656 };
1398         ret = v4l2_fwnode_endpoint_alloc_parse(ep, &bus_cfg);
1399         if (ret)
1400             goto error_fwnode_put;
1401     }
1402 
1403     priv->bus_type = bus_cfg.bus_type;
1404     v4l2_fwnode_endpoint_free(&bus_cfg);
1405 
1406 error_fwnode_put:
1407     fwnode_handle_put(ep);
1408 
1409     return ret;
1410 }
1411 
1412 /*
1413  * i2c_driver function
1414  */
1415 
1416 static int ov772x_probe(struct i2c_client *client)
1417 {
1418     struct ov772x_priv  *priv;
1419     int         ret;
1420     static const struct regmap_config ov772x_regmap_config = {
1421         .reg_bits = 8,
1422         .val_bits = 8,
1423         .max_register = DSPAUTO,
1424     };
1425 
1426     if (!client->dev.of_node && !client->dev.platform_data) {
1427         dev_err(&client->dev,
1428             "Missing ov772x platform data for non-DT device\n");
1429         return -EINVAL;
1430     }
1431 
1432     priv = devm_kzalloc(&client->dev, sizeof(*priv), GFP_KERNEL);
1433     if (!priv)
1434         return -ENOMEM;
1435 
1436     priv->regmap = devm_regmap_init_sccb(client, &ov772x_regmap_config);
1437     if (IS_ERR(priv->regmap)) {
1438         dev_err(&client->dev, "Failed to allocate register map\n");
1439         return PTR_ERR(priv->regmap);
1440     }
1441 
1442     priv->info = client->dev.platform_data;
1443     mutex_init(&priv->lock);
1444 
1445     v4l2_i2c_subdev_init(&priv->subdev, client, &ov772x_subdev_ops);
1446     priv->subdev.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE |
1447                   V4L2_SUBDEV_FL_HAS_EVENTS;
1448     v4l2_ctrl_handler_init(&priv->hdl, 3);
1449     /* Use our mutex for the controls */
1450     priv->hdl.lock = &priv->lock;
1451     priv->vflip_ctrl = v4l2_ctrl_new_std(&priv->hdl, &ov772x_ctrl_ops,
1452                          V4L2_CID_VFLIP, 0, 1, 1, 0);
1453     priv->hflip_ctrl = v4l2_ctrl_new_std(&priv->hdl, &ov772x_ctrl_ops,
1454                          V4L2_CID_HFLIP, 0, 1, 1, 0);
1455     priv->band_filter_ctrl = v4l2_ctrl_new_std(&priv->hdl, &ov772x_ctrl_ops,
1456                            V4L2_CID_BAND_STOP_FILTER,
1457                            0, 256, 1, 0);
1458     v4l2_ctrl_new_std_menu_items(&priv->hdl, &ov772x_ctrl_ops,
1459                      V4L2_CID_TEST_PATTERN,
1460                      ARRAY_SIZE(ov772x_test_pattern_menu) - 1,
1461                      0, 0, ov772x_test_pattern_menu);
1462     priv->subdev.ctrl_handler = &priv->hdl;
1463     if (priv->hdl.error) {
1464         ret = priv->hdl.error;
1465         goto error_mutex_destroy;
1466     }
1467 
1468     priv->clk = clk_get(&client->dev, NULL);
1469     if (IS_ERR(priv->clk)) {
1470         dev_err(&client->dev, "Unable to get xclk clock\n");
1471         ret = PTR_ERR(priv->clk);
1472         goto error_ctrl_free;
1473     }
1474 
1475     priv->pwdn_gpio = gpiod_get_optional(&client->dev, "powerdown",
1476                          GPIOD_OUT_LOW);
1477     if (IS_ERR(priv->pwdn_gpio)) {
1478         dev_info(&client->dev, "Unable to get GPIO \"powerdown\"");
1479         ret = PTR_ERR(priv->pwdn_gpio);
1480         goto error_clk_put;
1481     }
1482 
1483     ret = ov772x_parse_dt(client, priv);
1484     if (ret)
1485         goto error_clk_put;
1486 
1487     ret = ov772x_video_probe(priv);
1488     if (ret < 0)
1489         goto error_gpio_put;
1490 
1491 #ifdef CONFIG_MEDIA_CONTROLLER
1492     priv->pad.flags = MEDIA_PAD_FL_SOURCE;
1493     priv->subdev.entity.function = MEDIA_ENT_F_CAM_SENSOR;
1494     ret = media_entity_pads_init(&priv->subdev.entity, 1, &priv->pad);
1495     if (ret < 0)
1496         goto error_gpio_put;
1497 #endif
1498 
1499     priv->cfmt = &ov772x_cfmts[0];
1500     priv->win = &ov772x_win_sizes[0];
1501     priv->fps = 15;
1502 
1503     ret = v4l2_async_register_subdev(&priv->subdev);
1504     if (ret)
1505         goto error_entity_cleanup;
1506 
1507     return 0;
1508 
1509 error_entity_cleanup:
1510     media_entity_cleanup(&priv->subdev.entity);
1511 error_gpio_put:
1512     if (priv->pwdn_gpio)
1513         gpiod_put(priv->pwdn_gpio);
1514 error_clk_put:
1515     clk_put(priv->clk);
1516 error_ctrl_free:
1517     v4l2_ctrl_handler_free(&priv->hdl);
1518 error_mutex_destroy:
1519     mutex_destroy(&priv->lock);
1520 
1521     return ret;
1522 }
1523 
1524 static int ov772x_remove(struct i2c_client *client)
1525 {
1526     struct ov772x_priv *priv = to_ov772x(i2c_get_clientdata(client));
1527 
1528     media_entity_cleanup(&priv->subdev.entity);
1529     clk_put(priv->clk);
1530     if (priv->pwdn_gpio)
1531         gpiod_put(priv->pwdn_gpio);
1532     v4l2_async_unregister_subdev(&priv->subdev);
1533     v4l2_ctrl_handler_free(&priv->hdl);
1534     mutex_destroy(&priv->lock);
1535 
1536     return 0;
1537 }
1538 
1539 static const struct i2c_device_id ov772x_id[] = {
1540     { "ov772x", 0 },
1541     { }
1542 };
1543 MODULE_DEVICE_TABLE(i2c, ov772x_id);
1544 
1545 static const struct of_device_id ov772x_of_match[] = {
1546     { .compatible = "ovti,ov7725", },
1547     { .compatible = "ovti,ov7720", },
1548     { /* sentinel */ },
1549 };
1550 MODULE_DEVICE_TABLE(of, ov772x_of_match);
1551 
1552 static struct i2c_driver ov772x_i2c_driver = {
1553     .driver = {
1554         .name = "ov772x",
1555         .of_match_table = ov772x_of_match,
1556     },
1557     .probe_new = ov772x_probe,
1558     .remove   = ov772x_remove,
1559     .id_table = ov772x_id,
1560 };
1561 
1562 module_i2c_driver(ov772x_i2c_driver);
1563 
1564 MODULE_DESCRIPTION("V4L2 driver for OV772x image sensor");
1565 MODULE_AUTHOR("Kuninori Morimoto");
1566 MODULE_LICENSE("GPL v2");