blob: b70ffba584948d9954d45ecb698a6ef713e4aa45 [file] [log] [blame]
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -03001/*
2 * ov772x Camera Driver
3 *
4 * Copyright (C) 2008 Renesas Solutions Corp.
5 * Kuninori Morimoto <morimoto.kuninori@renesas.com>
6 *
7 * Based on ov7670 and soc_camera_platform driver,
8 *
9 * Copyright 2006-7 Jonathan Corbet <corbet@lwn.net>
10 * Copyright (C) 2008 Magnus Damm
11 * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de>
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License version 2 as
15 * published by the Free Software Foundation.
16 */
17
18#include <linux/init.h>
19#include <linux/module.h>
20#include <linux/i2c.h>
21#include <linux/slab.h>
22#include <linux/delay.h>
23#include <linux/videodev2.h>
Guennadi Liakhovetski00cf08f2011-07-26 12:25:07 -030024
25#include <media/ov772x.h>
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -030026#include <media/soc_camera.h>
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -030027#include <media/soc_mediabus.h>
Guennadi Liakhovetski00cf08f2011-07-26 12:25:07 -030028#include <media/v4l2-chip-ident.h>
29#include <media/v4l2-subdev.h>
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -030030
31/*
32 * register offset
33 */
34#define GAIN 0x00 /* AGC - Gain control gain setting */
35#define BLUE 0x01 /* AWB - Blue channel gain setting */
36#define RED 0x02 /* AWB - Red channel gain setting */
37#define GREEN 0x03 /* AWB - Green channel gain setting */
38#define COM1 0x04 /* Common control 1 */
39#define BAVG 0x05 /* U/B Average Level */
40#define GAVG 0x06 /* Y/Gb Average Level */
41#define RAVG 0x07 /* V/R Average Level */
42#define AECH 0x08 /* Exposure Value - AEC MSBs */
43#define COM2 0x09 /* Common control 2 */
44#define PID 0x0A /* Product ID Number MSB */
45#define VER 0x0B /* Product ID Number LSB */
46#define COM3 0x0C /* Common control 3 */
47#define COM4 0x0D /* Common control 4 */
48#define COM5 0x0E /* Common control 5 */
49#define COM6 0x0F /* Common control 6 */
50#define AEC 0x10 /* Exposure Value */
51#define CLKRC 0x11 /* Internal clock */
52#define COM7 0x12 /* Common control 7 */
53#define COM8 0x13 /* Common control 8 */
54#define COM9 0x14 /* Common control 9 */
55#define COM10 0x15 /* Common control 10 */
Kuninori Morimoto3cac2ca2008-12-18 11:07:11 -030056#define REG16 0x16 /* Register 16 */
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -030057#define HSTART 0x17 /* Horizontal sensor size */
58#define HSIZE 0x18 /* Horizontal frame (HREF column) end high 8-bit */
59#define VSTART 0x19 /* Vertical frame (row) start high 8-bit */
60#define VSIZE 0x1A /* Vertical sensor size */
61#define PSHFT 0x1B /* Data format - pixel delay select */
62#define MIDH 0x1C /* Manufacturer ID byte - high */
63#define MIDL 0x1D /* Manufacturer ID byte - low */
64#define LAEC 0x1F /* Fine AEC value */
65#define COM11 0x20 /* Common control 11 */
66#define BDBASE 0x22 /* Banding filter Minimum AEC value */
67#define DBSTEP 0x23 /* Banding filter Maximum Setp */
68#define AEW 0x24 /* AGC/AEC - Stable operating region (upper limit) */
69#define AEB 0x25 /* AGC/AEC - Stable operating region (lower limit) */
70#define VPT 0x26 /* AGC/AEC Fast mode operating region */
Kuninori Morimoto3cac2ca2008-12-18 11:07:11 -030071#define REG28 0x28 /* Register 28 */
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -030072#define HOUTSIZE 0x29 /* Horizontal data output size MSBs */
73#define EXHCH 0x2A /* Dummy pixel insert MSB */
74#define EXHCL 0x2B /* Dummy pixel insert LSB */
75#define VOUTSIZE 0x2C /* Vertical data output size MSBs */
76#define ADVFL 0x2D /* LSB of insert dummy lines in Vertical direction */
77#define ADVFH 0x2E /* MSG of insert dummy lines in Vertical direction */
78#define YAVE 0x2F /* Y/G Channel Average value */
79#define LUMHTH 0x30 /* Histogram AEC/AGC Luminance high level threshold */
80#define LUMLTH 0x31 /* Histogram AEC/AGC Luminance low level threshold */
81#define HREF 0x32 /* Image start and size control */
82#define DM_LNL 0x33 /* Dummy line low 8 bits */
83#define DM_LNH 0x34 /* Dummy line high 8 bits */
84#define ADOFF_B 0x35 /* AD offset compensation value for B channel */
85#define ADOFF_R 0x36 /* AD offset compensation value for R channel */
86#define ADOFF_GB 0x37 /* AD offset compensation value for Gb channel */
87#define ADOFF_GR 0x38 /* AD offset compensation value for Gr channel */
88#define OFF_B 0x39 /* Analog process B channel offset value */
89#define OFF_R 0x3A /* Analog process R channel offset value */
90#define OFF_GB 0x3B /* Analog process Gb channel offset value */
91#define OFF_GR 0x3C /* Analog process Gr channel offset value */
92#define COM12 0x3D /* Common control 12 */
93#define COM13 0x3E /* Common control 13 */
94#define COM14 0x3F /* Common control 14 */
95#define COM15 0x40 /* Common control 15*/
96#define COM16 0x41 /* Common control 16 */
97#define TGT_B 0x42 /* BLC blue channel target value */
98#define TGT_R 0x43 /* BLC red channel target value */
99#define TGT_GB 0x44 /* BLC Gb channel target value */
100#define TGT_GR 0x45 /* BLC Gr channel target value */
Kuninori Morimoto3cac2ca2008-12-18 11:07:11 -0300101/* for ov7720 */
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -0300102#define LCC0 0x46 /* Lens correction control 0 */
103#define LCC1 0x47 /* Lens correction option 1 - X coordinate */
104#define LCC2 0x48 /* Lens correction option 2 - Y coordinate */
105#define LCC3 0x49 /* Lens correction option 3 */
106#define LCC4 0x4A /* Lens correction option 4 - radius of the circular */
107#define LCC5 0x4B /* Lens correction option 5 */
108#define LCC6 0x4C /* Lens correction option 6 */
Kuninori Morimoto3cac2ca2008-12-18 11:07:11 -0300109/* for ov7725 */
110#define LC_CTR 0x46 /* Lens correction control */
111#define LC_XC 0x47 /* X coordinate of lens correction center relative */
112#define LC_YC 0x48 /* Y coordinate of lens correction center relative */
113#define LC_COEF 0x49 /* Lens correction coefficient */
114#define LC_RADI 0x4A /* Lens correction radius */
115#define LC_COEFB 0x4B /* Lens B channel compensation coefficient */
116#define LC_COEFR 0x4C /* Lens R channel compensation coefficient */
117
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -0300118#define FIXGAIN 0x4D /* Analog fix gain amplifer */
119#define AREF0 0x4E /* Sensor reference control */
120#define AREF1 0x4F /* Sensor reference current control */
121#define AREF2 0x50 /* Analog reference control */
122#define AREF3 0x51 /* ADC reference control */
123#define AREF4 0x52 /* ADC reference control */
124#define AREF5 0x53 /* ADC reference control */
125#define AREF6 0x54 /* Analog reference control */
126#define AREF7 0x55 /* Analog reference control */
127#define UFIX 0x60 /* U channel fixed value output */
128#define VFIX 0x61 /* V channel fixed value output */
Kuninori Morimotob3d7b2a2008-12-01 09:44:51 -0300129#define AWBB_BLK 0x62 /* AWB option for advanced AWB */
130#define AWB_CTRL0 0x63 /* AWB control byte 0 */
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -0300131#define DSP_CTRL1 0x64 /* DSP control byte 1 */
132#define DSP_CTRL2 0x65 /* DSP control byte 2 */
133#define DSP_CTRL3 0x66 /* DSP control byte 3 */
134#define DSP_CTRL4 0x67 /* DSP control byte 4 */
Kuninori Morimotob3d7b2a2008-12-01 09:44:51 -0300135#define AWB_BIAS 0x68 /* AWB BLC level clip */
136#define AWB_CTRL1 0x69 /* AWB control 1 */
137#define AWB_CTRL2 0x6A /* AWB control 2 */
138#define AWB_CTRL3 0x6B /* AWB control 3 */
139#define AWB_CTRL4 0x6C /* AWB control 4 */
140#define AWB_CTRL5 0x6D /* AWB control 5 */
141#define AWB_CTRL6 0x6E /* AWB control 6 */
142#define AWB_CTRL7 0x6F /* AWB control 7 */
143#define AWB_CTRL8 0x70 /* AWB control 8 */
144#define AWB_CTRL9 0x71 /* AWB control 9 */
145#define AWB_CTRL10 0x72 /* AWB control 10 */
146#define AWB_CTRL11 0x73 /* AWB control 11 */
147#define AWB_CTRL12 0x74 /* AWB control 12 */
148#define AWB_CTRL13 0x75 /* AWB control 13 */
149#define AWB_CTRL14 0x76 /* AWB control 14 */
150#define AWB_CTRL15 0x77 /* AWB control 15 */
151#define AWB_CTRL16 0x78 /* AWB control 16 */
152#define AWB_CTRL17 0x79 /* AWB control 17 */
153#define AWB_CTRL18 0x7A /* AWB control 18 */
154#define AWB_CTRL19 0x7B /* AWB control 19 */
155#define AWB_CTRL20 0x7C /* AWB control 20 */
156#define AWB_CTRL21 0x7D /* AWB control 21 */
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -0300157#define GAM1 0x7E /* Gamma Curve 1st segment input end point */
158#define GAM2 0x7F /* Gamma Curve 2nd segment input end point */
159#define GAM3 0x80 /* Gamma Curve 3rd segment input end point */
160#define GAM4 0x81 /* Gamma Curve 4th segment input end point */
161#define GAM5 0x82 /* Gamma Curve 5th segment input end point */
162#define GAM6 0x83 /* Gamma Curve 6th segment input end point */
163#define GAM7 0x84 /* Gamma Curve 7th segment input end point */
164#define GAM8 0x85 /* Gamma Curve 8th segment input end point */
165#define GAM9 0x86 /* Gamma Curve 9th segment input end point */
166#define GAM10 0x87 /* Gamma Curve 10th segment input end point */
167#define GAM11 0x88 /* Gamma Curve 11th segment input end point */
168#define GAM12 0x89 /* Gamma Curve 12th segment input end point */
169#define GAM13 0x8A /* Gamma Curve 13th segment input end point */
170#define GAM14 0x8B /* Gamma Curve 14th segment input end point */
171#define GAM15 0x8C /* Gamma Curve 15th segment input end point */
172#define SLOP 0x8D /* Gamma curve highest segment slope */
173#define DNSTH 0x8E /* De-noise threshold */
Kuninori Morimotodb6cbc82009-03-31 03:44:22 -0300174#define EDGE_STRNGT 0x8F /* Edge strength control when manual mode */
175#define EDGE_TRSHLD 0x90 /* Edge threshold control when manual mode */
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -0300176#define DNSOFF 0x91 /* Auto De-noise threshold control */
Kuninori Morimotodb6cbc82009-03-31 03:44:22 -0300177#define EDGE_UPPER 0x92 /* Edge strength upper limit when Auto mode */
178#define EDGE_LOWER 0x93 /* Edge strength lower limit when Auto mode */
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -0300179#define MTX1 0x94 /* Matrix coefficient 1 */
180#define MTX2 0x95 /* Matrix coefficient 2 */
181#define MTX3 0x96 /* Matrix coefficient 3 */
182#define MTX4 0x97 /* Matrix coefficient 4 */
183#define MTX5 0x98 /* Matrix coefficient 5 */
184#define MTX6 0x99 /* Matrix coefficient 6 */
185#define MTX_CTRL 0x9A /* Matrix control */
186#define BRIGHT 0x9B /* Brightness control */
187#define CNTRST 0x9C /* Contrast contrast */
188#define CNTRST_CTRL 0x9D /* Contrast contrast center */
189#define UVAD_J0 0x9E /* Auto UV adjust contrast 0 */
190#define UVAD_J1 0x9F /* Auto UV adjust contrast 1 */
191#define SCAL0 0xA0 /* Scaling control 0 */
192#define SCAL1 0xA1 /* Scaling control 1 */
193#define SCAL2 0xA2 /* Scaling control 2 */
194#define FIFODLYM 0xA3 /* FIFO manual mode delay control */
195#define FIFODLYA 0xA4 /* FIFO auto mode delay control */
196#define SDE 0xA6 /* Special digital effect control */
197#define USAT 0xA7 /* U component saturation control */
198#define VSAT 0xA8 /* V component saturation control */
Kuninori Morimoto3cac2ca2008-12-18 11:07:11 -0300199/* for ov7720 */
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -0300200#define HUE0 0xA9 /* Hue control 0 */
201#define HUE1 0xAA /* Hue control 1 */
Kuninori Morimoto3cac2ca2008-12-18 11:07:11 -0300202/* for ov7725 */
203#define HUECOS 0xA9 /* Cosine value */
204#define HUESIN 0xAA /* Sine value */
205
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -0300206#define SIGN 0xAB /* Sign bit for Hue and contrast */
207#define DSPAUTO 0xAC /* DSP auto function ON/OFF control */
208
209/*
210 * register detail
211 */
212
213/* COM2 */
214#define SOFT_SLEEP_MODE 0x10 /* Soft sleep mode */
215 /* Output drive capability */
216#define OCAP_1x 0x00 /* 1x */
217#define OCAP_2x 0x01 /* 2x */
218#define OCAP_3x 0x02 /* 3x */
219#define OCAP_4x 0x03 /* 4x */
220
221/* COM3 */
Kuninori Morimoto05148912009-02-23 12:12:58 -0300222#define SWAP_MASK (SWAP_RGB | SWAP_YUV | SWAP_ML)
223#define IMG_MASK (VFLIP_IMG | HFLIP_IMG)
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -0300224
Kuninori Morimoto05148912009-02-23 12:12:58 -0300225#define VFLIP_IMG 0x80 /* Vertical flip image ON/OFF selection */
226#define HFLIP_IMG 0x40 /* Horizontal mirror image ON/OFF selection */
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -0300227#define SWAP_RGB 0x20 /* Swap B/R output sequence in RGB mode */
228#define SWAP_YUV 0x10 /* Swap Y/UV output sequence in YUV mode */
229#define SWAP_ML 0x08 /* Swap output MSB/LSB */
230 /* Tri-state option for output clock */
231#define NOTRI_CLOCK 0x04 /* 0: Tri-state at this period */
232 /* 1: No tri-state at this period */
233 /* Tri-state option for output data */
234#define NOTRI_DATA 0x02 /* 0: Tri-state at this period */
235 /* 1: No tri-state at this period */
236#define SCOLOR_TEST 0x01 /* Sensor color bar test pattern */
237
238/* COM4 */
239 /* PLL frequency control */
240#define PLL_BYPASS 0x00 /* 00: Bypass PLL */
241#define PLL_4x 0x40 /* 01: PLL 4x */
242#define PLL_6x 0x80 /* 10: PLL 6x */
243#define PLL_8x 0xc0 /* 11: PLL 8x */
244 /* AEC evaluate window */
245#define AEC_FULL 0x00 /* 00: Full window */
246#define AEC_1p2 0x10 /* 01: 1/2 window */
247#define AEC_1p4 0x20 /* 10: 1/4 window */
248#define AEC_2p3 0x30 /* 11: Low 2/3 window */
249
250/* COM5 */
251#define AFR_ON_OFF 0x80 /* Auto frame rate control ON/OFF selection */
Uwe Kleine-Königb71a8eb2009-10-06 12:42:51 +0200252#define AFR_SPPED 0x40 /* Auto frame rate control speed selection */
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -0300253 /* Auto frame rate max rate control */
254#define AFR_NO_RATE 0x00 /* No reduction of frame rate */
255#define AFR_1p2 0x10 /* Max reduction to 1/2 frame rate */
256#define AFR_1p4 0x20 /* Max reduction to 1/4 frame rate */
257#define AFR_1p8 0x30 /* Max reduction to 1/8 frame rate */
258 /* Auto frame rate active point control */
259#define AF_2x 0x00 /* Add frame when AGC reaches 2x gain */
260#define AF_4x 0x04 /* Add frame when AGC reaches 4x gain */
261#define AF_8x 0x08 /* Add frame when AGC reaches 8x gain */
262#define AF_16x 0x0c /* Add frame when AGC reaches 16x gain */
263 /* AEC max step control */
264#define AEC_NO_LIMIT 0x01 /* 0 : AEC incease step has limit */
265 /* 1 : No limit to AEC increase step */
266
267/* COM7 */
268 /* SCCB Register Reset */
269#define SCCB_RESET 0x80 /* 0 : No change */
270 /* 1 : Resets all registers to default */
271 /* Resolution selection */
272#define SLCT_MASK 0x40 /* Mask of VGA or QVGA */
273#define SLCT_VGA 0x00 /* 0 : VGA */
274#define SLCT_QVGA 0x40 /* 1 : QVGA */
275#define ITU656_ON_OFF 0x20 /* ITU656 protocol ON/OFF selection */
276 /* RGB output format control */
Kuninori Morimotocdce7c02009-02-23 12:12:58 -0300277#define FMT_MASK 0x0c /* Mask of color format */
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -0300278#define FMT_GBR422 0x00 /* 00 : GBR 4:2:2 */
279#define FMT_RGB565 0x04 /* 01 : RGB 565 */
280#define FMT_RGB555 0x08 /* 10 : RGB 555 */
281#define FMT_RGB444 0x0c /* 11 : RGB 444 */
282 /* Output format control */
Kuninori Morimotocdce7c02009-02-23 12:12:58 -0300283#define OFMT_MASK 0x03 /* Mask of output format */
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -0300284#define OFMT_YUV 0x00 /* 00 : YUV */
285#define OFMT_P_BRAW 0x01 /* 01 : Processed Bayer RAW */
286#define OFMT_RGB 0x02 /* 10 : RGB */
287#define OFMT_BRAW 0x03 /* 11 : Bayer RAW */
288
289/* COM8 */
290#define FAST_ALGO 0x80 /* Enable fast AGC/AEC algorithm */
291 /* AEC Setp size limit */
292#define UNLMT_STEP 0x40 /* 0 : Step size is limited */
293 /* 1 : Unlimited step size */
294#define BNDF_ON_OFF 0x20 /* Banding filter ON/OFF */
295#define AEC_BND 0x10 /* Enable AEC below banding value */
296#define AEC_ON_OFF 0x08 /* Fine AEC ON/OFF control */
297#define AGC_ON 0x04 /* AGC Enable */
298#define AWB_ON 0x02 /* AWB Enable */
299#define AEC_ON 0x01 /* AEC Enable */
300
301/* COM9 */
302#define BASE_AECAGC 0x80 /* Histogram or average based AEC/AGC */
303 /* Automatic gain ceiling - maximum AGC value */
304#define GAIN_2x 0x00 /* 000 : 2x */
305#define GAIN_4x 0x10 /* 001 : 4x */
306#define GAIN_8x 0x20 /* 010 : 8x */
Kuninori Morimotocdce7c02009-02-23 12:12:58 -0300307#define GAIN_16x 0x30 /* 011 : 16x */
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -0300308#define GAIN_32x 0x40 /* 100 : 32x */
309#define GAIN_64x 0x50 /* 101 : 64x */
310#define GAIN_128x 0x60 /* 110 : 128x */
311#define DROP_VSYNC 0x04 /* Drop VSYNC output of corrupt frame */
312#define DROP_HREF 0x02 /* Drop HREF output of corrupt frame */
313
314/* COM11 */
315#define SGLF_ON_OFF 0x02 /* Single frame ON/OFF selection */
316#define SGLF_TRIG 0x01 /* Single frame transfer trigger */
317
318/* EXHCH */
319#define VSIZE_LSB 0x04 /* Vertical data output size LSB */
320
321/* DSP_CTRL1 */
322#define FIFO_ON 0x80 /* FIFO enable/disable selection */
323#define UV_ON_OFF 0x40 /* UV adjust function ON/OFF selection */
324#define YUV444_2_422 0x20 /* YUV444 to 422 UV channel option selection */
325#define CLR_MTRX_ON_OFF 0x10 /* Color matrix ON/OFF selection */
326#define INTPLT_ON_OFF 0x08 /* Interpolation ON/OFF selection */
327#define GMM_ON_OFF 0x04 /* Gamma function ON/OFF selection */
328#define AUTO_BLK_ON_OFF 0x02 /* Black defect auto correction ON/OFF */
329#define AUTO_WHT_ON_OFF 0x01 /* White define auto correction ON/OFF */
330
331/* DSP_CTRL3 */
332#define UV_MASK 0x80 /* UV output sequence option */
333#define UV_ON 0x80 /* ON */
334#define UV_OFF 0x00 /* OFF */
335#define CBAR_MASK 0x20 /* DSP Color bar mask */
336#define CBAR_ON 0x20 /* ON */
337#define CBAR_OFF 0x00 /* OFF */
338
339/* HSTART */
340#define HST_VGA 0x23
341#define HST_QVGA 0x3F
342
343/* HSIZE */
344#define HSZ_VGA 0xA0
345#define HSZ_QVGA 0x50
346
347/* VSTART */
348#define VST_VGA 0x07
349#define VST_QVGA 0x03
350
351/* VSIZE */
352#define VSZ_VGA 0xF0
353#define VSZ_QVGA 0x78
354
355/* HOUTSIZE */
356#define HOSZ_VGA 0xA0
357#define HOSZ_QVGA 0x50
358
359/* VOUTSIZE */
360#define VOSZ_VGA 0xF0
361#define VOSZ_QVGA 0x78
362
Kuninori Morimotodb6cbc82009-03-31 03:44:22 -0300363/* DSPAUTO (DSP Auto Function ON/OFF Control) */
364#define AWB_ACTRL 0x80 /* AWB auto threshold control */
365#define DENOISE_ACTRL 0x40 /* De-noise auto threshold control */
366#define EDGE_ACTRL 0x20 /* Edge enhancement auto strength control */
367#define UV_ACTRL 0x10 /* UV adjust auto slope control */
368#define SCAL0_ACTRL 0x08 /* Auto scaling factor control */
369#define SCAL1_2_ACTRL 0x04 /* Auto scaling factor control */
370
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -0300371/*
Kuninori Morimotoaeabc882008-12-18 11:05:49 -0300372 * ID
373 */
374#define OV7720 0x7720
Kuninori Morimoto3cac2ca2008-12-18 11:07:11 -0300375#define OV7725 0x7721
Kuninori Morimotoaeabc882008-12-18 11:05:49 -0300376#define VERSION(pid, ver) ((pid<<8)|(ver&0xFF))
377
378/*
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -0300379 * struct
380 */
381struct regval_list {
382 unsigned char reg_num;
383 unsigned char value;
384};
385
386struct ov772x_color_format {
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300387 enum v4l2_mbus_pixelcode code;
388 enum v4l2_colorspace colorspace;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300389 u8 dsp3;
390 u8 com3;
391 u8 com7;
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -0300392};
393
394struct ov772x_win_size {
395 char *name;
396 __u32 width;
397 __u32 height;
398 unsigned char com7_bit;
399 const struct regval_list *regs;
400};
401
402struct ov772x_priv {
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300403 struct v4l2_subdev subdev;
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -0300404 struct ov772x_camera_info *info;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300405 const struct ov772x_color_format *cfmt;
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -0300406 const struct ov772x_win_size *win;
Kuninori Morimotoaeabc882008-12-18 11:05:49 -0300407 int model;
Guennadi Liakhovetskia813d012009-08-25 11:44:14 -0300408 unsigned short flag_vflip:1;
409 unsigned short flag_hflip:1;
Guennadi Liakhovetski96c75392009-08-25 11:53:23 -0300410 /* band_filter = COM8[5] ? 256 - BDBASE : 0 */
411 unsigned short band_filter;
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -0300412};
413
414#define ENDMARKER { 0xff, 0xff }
415
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -0300416/*
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -0300417 * register setting for window size
418 */
419static const struct regval_list ov772x_qvga_regs[] = {
420 { HSTART, HST_QVGA },
421 { HSIZE, HSZ_QVGA },
422 { VSTART, VST_QVGA },
423 { VSIZE, VSZ_QVGA },
424 { HOUTSIZE, HOSZ_QVGA },
425 { VOUTSIZE, VOSZ_QVGA },
426 ENDMARKER,
427};
428
429static const struct regval_list ov772x_vga_regs[] = {
430 { HSTART, HST_VGA },
431 { HSIZE, HSZ_VGA },
432 { VSTART, VST_VGA },
433 { VSIZE, VSZ_VGA },
434 { HOUTSIZE, HOSZ_VGA },
435 { VOUTSIZE, VOSZ_VGA },
436 ENDMARKER,
437};
438
439/*
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300440 * supported color format list
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -0300441 */
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -0300442static const struct ov772x_color_format ov772x_cfmts[] = {
Kuninori Morimoto2941e812009-02-23 12:12:58 -0300443 {
Guennadi Liakhovetskiace6e972010-07-22 16:52:51 -0300444 .code = V4L2_MBUS_FMT_YUYV8_2X8,
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300445 .colorspace = V4L2_COLORSPACE_JPEG,
446 .dsp3 = 0x0,
447 .com3 = SWAP_YUV,
448 .com7 = OFMT_YUV,
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -0300449 },
450 {
Guennadi Liakhovetskiace6e972010-07-22 16:52:51 -0300451 .code = V4L2_MBUS_FMT_YVYU8_2X8,
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300452 .colorspace = V4L2_COLORSPACE_JPEG,
453 .dsp3 = UV_ON,
454 .com3 = SWAP_YUV,
455 .com7 = OFMT_YUV,
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -0300456 },
457 {
Guennadi Liakhovetskiace6e972010-07-22 16:52:51 -0300458 .code = V4L2_MBUS_FMT_UYVY8_2X8,
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300459 .colorspace = V4L2_COLORSPACE_JPEG,
460 .dsp3 = 0x0,
461 .com3 = 0x0,
462 .com7 = OFMT_YUV,
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -0300463 },
464 {
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300465 .code = V4L2_MBUS_FMT_RGB555_2X8_PADHI_LE,
466 .colorspace = V4L2_COLORSPACE_SRGB,
467 .dsp3 = 0x0,
468 .com3 = SWAP_RGB,
469 .com7 = FMT_RGB555 | OFMT_RGB,
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -0300470 },
471 {
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300472 .code = V4L2_MBUS_FMT_RGB555_2X8_PADHI_BE,
473 .colorspace = V4L2_COLORSPACE_SRGB,
474 .dsp3 = 0x0,
475 .com3 = 0x0,
476 .com7 = FMT_RGB555 | OFMT_RGB,
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -0300477 },
478 {
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300479 .code = V4L2_MBUS_FMT_RGB565_2X8_LE,
480 .colorspace = V4L2_COLORSPACE_SRGB,
481 .dsp3 = 0x0,
482 .com3 = SWAP_RGB,
483 .com7 = FMT_RGB565 | OFMT_RGB,
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -0300484 },
485 {
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300486 .code = V4L2_MBUS_FMT_RGB565_2X8_BE,
487 .colorspace = V4L2_COLORSPACE_SRGB,
488 .dsp3 = 0x0,
489 .com3 = 0x0,
490 .com7 = FMT_RGB565 | OFMT_RGB,
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -0300491 },
492};
493
494
495/*
496 * window size list
497 */
498#define VGA_WIDTH 640
499#define VGA_HEIGHT 480
500#define QVGA_WIDTH 320
501#define QVGA_HEIGHT 240
502#define MAX_WIDTH VGA_WIDTH
503#define MAX_HEIGHT VGA_HEIGHT
504
505static const struct ov772x_win_size ov772x_win_vga = {
506 .name = "VGA",
507 .width = VGA_WIDTH,
508 .height = VGA_HEIGHT,
509 .com7_bit = SLCT_VGA,
510 .regs = ov772x_vga_regs,
511};
512
513static const struct ov772x_win_size ov772x_win_qvga = {
514 .name = "QVGA",
515 .width = QVGA_WIDTH,
516 .height = QVGA_HEIGHT,
517 .com7_bit = SLCT_QVGA,
518 .regs = ov772x_qvga_regs,
519};
520
Kuninori Morimoto05148912009-02-23 12:12:58 -0300521static const struct v4l2_queryctrl ov772x_controls[] = {
522 {
523 .id = V4L2_CID_VFLIP,
524 .type = V4L2_CTRL_TYPE_BOOLEAN,
525 .name = "Flip Vertically",
526 .minimum = 0,
527 .maximum = 1,
528 .step = 1,
529 .default_value = 0,
530 },
531 {
532 .id = V4L2_CID_HFLIP,
533 .type = V4L2_CTRL_TYPE_BOOLEAN,
534 .name = "Flip Horizontally",
535 .minimum = 0,
536 .maximum = 1,
537 .step = 1,
538 .default_value = 0,
539 },
Guennadi Liakhovetskia813d012009-08-25 11:44:14 -0300540 {
541 .id = V4L2_CID_BAND_STOP_FILTER,
542 .type = V4L2_CTRL_TYPE_INTEGER,
543 .name = "Band-stop filter",
544 .minimum = 0,
545 .maximum = 256,
546 .step = 1,
547 .default_value = 0,
548 },
Kuninori Morimoto05148912009-02-23 12:12:58 -0300549};
550
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -0300551/*
552 * general function
553 */
554
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300555static struct ov772x_priv *to_ov772x(const struct i2c_client *client)
556{
Guennadi Liakhovetski96c75392009-08-25 11:53:23 -0300557 return container_of(i2c_get_clientdata(client), struct ov772x_priv,
558 subdev);
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300559}
560
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -0300561static int ov772x_write_array(struct i2c_client *client,
562 const struct regval_list *vals)
563{
564 while (vals->reg_num != 0xff) {
565 int ret = i2c_smbus_write_byte_data(client,
566 vals->reg_num,
567 vals->value);
568 if (ret < 0)
569 return ret;
570 vals++;
571 }
572 return 0;
573}
574
575static int ov772x_mask_set(struct i2c_client *client,
576 u8 command,
577 u8 mask,
578 u8 set)
579{
580 s32 val = i2c_smbus_read_byte_data(client, command);
Kuninori Morimoto66b46e62009-02-23 12:12:58 -0300581 if (val < 0)
582 return val;
583
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -0300584 val &= ~mask;
Kuninori Morimoto66b46e62009-02-23 12:12:58 -0300585 val |= set & mask;
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -0300586
587 return i2c_smbus_write_byte_data(client, command, val);
588}
589
590static int ov772x_reset(struct i2c_client *client)
591{
592 int ret = i2c_smbus_write_byte_data(client, COM7, SCCB_RESET);
593 msleep(1);
594 return ret;
595}
596
597/*
598 * soc_camera_ops function
599 */
600
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300601static int ov772x_s_stream(struct v4l2_subdev *sd, int enable)
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -0300602{
Laurent Pinchartc4ce6d12010-07-30 17:24:54 -0300603 struct i2c_client *client = v4l2_get_subdevdata(sd);
Guennadi Liakhovetskic85bb692010-12-30 08:37:06 -0300604 struct ov772x_priv *priv = container_of(sd, struct ov772x_priv, subdev);
Kuninori Morimotob90c0322008-12-01 09:44:48 -0300605
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300606 if (!enable) {
607 ov772x_mask_set(client, COM2, SOFT_SLEEP_MODE, SOFT_SLEEP_MODE);
608 return 0;
Kuninori Morimotob90c0322008-12-01 09:44:48 -0300609 }
610
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300611 if (!priv->win || !priv->cfmt) {
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300612 dev_err(&client->dev, "norm or win select error\n");
Kuninori Morimoto2941e812009-02-23 12:12:58 -0300613 return -EPERM;
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -0300614 }
615
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300616 ov772x_mask_set(client, COM2, SOFT_SLEEP_MODE, 0);
Kuninori Morimotof340e3f2009-03-13 06:08:20 -0300617
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300618 dev_dbg(&client->dev, "format %d, win %s\n",
619 priv->cfmt->code, priv->win->name);
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -0300620
Kuninori Morimoto2941e812009-02-23 12:12:58 -0300621 return 0;
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -0300622}
623
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -0300624static int ov772x_set_bus_param(struct soc_camera_device *icd,
625 unsigned long flags)
626{
627 return 0;
628}
629
630static unsigned long ov772x_query_bus_param(struct soc_camera_device *icd)
631{
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300632 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
633 struct ov772x_priv *priv = i2c_get_clientdata(client);
634 struct soc_camera_link *icl = to_soc_camera_link(icd);
Guennadi Liakhovetskibd73b362008-12-23 05:54:45 -0300635 unsigned long flags = SOCAM_PCLK_SAMPLE_RISING | SOCAM_MASTER |
636 SOCAM_VSYNC_ACTIVE_HIGH | SOCAM_HSYNC_ACTIVE_HIGH |
Kuninori Morimoto3675c752010-01-06 18:42:16 +0900637 SOCAM_DATA_ACTIVE_HIGH;
638
639 if (priv->info->flags & OV772X_FLAG_8BIT)
640 flags |= SOCAM_DATAWIDTH_8;
641 else
642 flags |= SOCAM_DATAWIDTH_10;
Guennadi Liakhovetskibd73b362008-12-23 05:54:45 -0300643
644 return soc_camera_apply_sensor_flags(icl, flags);
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -0300645}
646
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300647static int ov772x_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
Kuninori Morimoto05148912009-02-23 12:12:58 -0300648{
Guennadi Liakhovetskic85bb692010-12-30 08:37:06 -0300649 struct ov772x_priv *priv = container_of(sd, struct ov772x_priv, subdev);
Kuninori Morimoto05148912009-02-23 12:12:58 -0300650
651 switch (ctrl->id) {
652 case V4L2_CID_VFLIP:
653 ctrl->value = priv->flag_vflip;
654 break;
655 case V4L2_CID_HFLIP:
656 ctrl->value = priv->flag_hflip;
657 break;
Guennadi Liakhovetskia813d012009-08-25 11:44:14 -0300658 case V4L2_CID_BAND_STOP_FILTER:
659 ctrl->value = priv->band_filter;
660 break;
Kuninori Morimoto05148912009-02-23 12:12:58 -0300661 }
662 return 0;
663}
664
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300665static int ov772x_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
Kuninori Morimoto05148912009-02-23 12:12:58 -0300666{
Laurent Pinchartc4ce6d12010-07-30 17:24:54 -0300667 struct i2c_client *client = v4l2_get_subdevdata(sd);
Guennadi Liakhovetskic85bb692010-12-30 08:37:06 -0300668 struct ov772x_priv *priv = container_of(sd, struct ov772x_priv, subdev);
Kuninori Morimoto05148912009-02-23 12:12:58 -0300669 int ret = 0;
670 u8 val;
671
672 switch (ctrl->id) {
673 case V4L2_CID_VFLIP:
674 val = ctrl->value ? VFLIP_IMG : 0x00;
675 priv->flag_vflip = ctrl->value;
676 if (priv->info->flags & OV772X_FLAG_VFLIP)
677 val ^= VFLIP_IMG;
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300678 ret = ov772x_mask_set(client, COM3, VFLIP_IMG, val);
Kuninori Morimoto05148912009-02-23 12:12:58 -0300679 break;
680 case V4L2_CID_HFLIP:
681 val = ctrl->value ? HFLIP_IMG : 0x00;
682 priv->flag_hflip = ctrl->value;
683 if (priv->info->flags & OV772X_FLAG_HFLIP)
684 val ^= HFLIP_IMG;
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300685 ret = ov772x_mask_set(client, COM3, HFLIP_IMG, val);
Kuninori Morimoto05148912009-02-23 12:12:58 -0300686 break;
Guennadi Liakhovetskia813d012009-08-25 11:44:14 -0300687 case V4L2_CID_BAND_STOP_FILTER:
688 if ((unsigned)ctrl->value > 256)
689 ctrl->value = 256;
690 if (ctrl->value == priv->band_filter)
691 break;
692 if (!ctrl->value) {
693 /* Switch the filter off, it is on now */
694 ret = ov772x_mask_set(client, BDBASE, 0xff, 0xff);
695 if (!ret)
696 ret = ov772x_mask_set(client, COM8,
697 BNDF_ON_OFF, 0);
698 } else {
699 /* Switch the filter on, set AEC low limit */
700 val = 256 - ctrl->value;
701 ret = ov772x_mask_set(client, COM8,
702 BNDF_ON_OFF, BNDF_ON_OFF);
703 if (!ret)
704 ret = ov772x_mask_set(client, BDBASE,
705 0xff, val);
706 }
707 if (!ret)
708 priv->band_filter = ctrl->value;
709 break;
Kuninori Morimoto05148912009-02-23 12:12:58 -0300710 }
711
712 return ret;
713}
714
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300715static int ov772x_g_chip_ident(struct v4l2_subdev *sd,
716 struct v4l2_dbg_chip_ident *id)
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -0300717{
Guennadi Liakhovetskic85bb692010-12-30 08:37:06 -0300718 struct ov772x_priv *priv = container_of(sd, struct ov772x_priv, subdev);
Kuninori Morimotoaeabc882008-12-18 11:05:49 -0300719
720 id->ident = priv->model;
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -0300721 id->revision = 0;
722
723 return 0;
724}
725
726#ifdef CONFIG_VIDEO_ADV_DEBUG
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300727static int ov772x_g_register(struct v4l2_subdev *sd,
728 struct v4l2_dbg_register *reg)
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -0300729{
Laurent Pinchartc4ce6d12010-07-30 17:24:54 -0300730 struct i2c_client *client = v4l2_get_subdevdata(sd);
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300731 int ret;
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -0300732
Hans Verkuilaecde8b2008-12-30 07:14:19 -0300733 reg->size = 1;
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -0300734 if (reg->reg > 0xff)
735 return -EINVAL;
736
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300737 ret = i2c_smbus_read_byte_data(client, reg->reg);
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -0300738 if (ret < 0)
739 return ret;
740
741 reg->val = (__u64)ret;
742
743 return 0;
744}
745
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300746static int ov772x_s_register(struct v4l2_subdev *sd,
747 struct v4l2_dbg_register *reg)
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -0300748{
Laurent Pinchartc4ce6d12010-07-30 17:24:54 -0300749 struct i2c_client *client = v4l2_get_subdevdata(sd);
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -0300750
751 if (reg->reg > 0xff ||
752 reg->val > 0xff)
753 return -EINVAL;
754
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300755 return i2c_smbus_write_byte_data(client, reg->reg, reg->val);
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -0300756}
757#endif
758
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300759static const struct ov772x_win_size *ov772x_select_win(u32 width, u32 height)
Kuninori Morimotof82a8562008-12-29 06:04:44 -0300760{
761 __u32 diff;
762 const struct ov772x_win_size *win;
763
764 /* default is QVGA */
765 diff = abs(width - ov772x_win_qvga.width) +
766 abs(height - ov772x_win_qvga.height);
767 win = &ov772x_win_qvga;
768
769 /* VGA */
770 if (diff >
771 abs(width - ov772x_win_vga.width) +
772 abs(height - ov772x_win_vga.height))
773 win = &ov772x_win_vga;
774
775 return win;
776}
777
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300778static int ov772x_set_params(struct i2c_client *client, u32 *width, u32 *height,
779 enum v4l2_mbus_pixelcode code)
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -0300780{
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300781 struct ov772x_priv *priv = to_ov772x(client);
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -0300782 int ret = -EINVAL;
Kuninori Morimotocdce7c02009-02-23 12:12:58 -0300783 u8 val;
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -0300784 int i;
785
786 /*
787 * select format
788 */
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300789 priv->cfmt = NULL;
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -0300790 for (i = 0; i < ARRAY_SIZE(ov772x_cfmts); i++) {
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300791 if (code == ov772x_cfmts[i].code) {
792 priv->cfmt = ov772x_cfmts + i;
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -0300793 break;
794 }
795 }
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300796 if (!priv->cfmt)
Kuninori Morimoto2941e812009-02-23 12:12:58 -0300797 goto ov772x_set_fmt_error;
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -0300798
Kuninori Morimotof82a8562008-12-29 06:04:44 -0300799 /*
800 * select win
801 */
Guennadi Liakhovetskibf62e1d2009-08-25 11:46:42 -0300802 priv->win = ov772x_select_win(*width, *height);
Kuninori Morimotof82a8562008-12-29 06:04:44 -0300803
Kuninori Morimoto2941e812009-02-23 12:12:58 -0300804 /*
805 * reset hardware
806 */
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300807 ov772x_reset(client);
Kuninori Morimoto2941e812009-02-23 12:12:58 -0300808
809 /*
Kuninori Morimotodb6cbc82009-03-31 03:44:22 -0300810 * Edge Ctrl
811 */
812 if (priv->info->edgectrl.strength & OV772X_MANUAL_EDGE_CTRL) {
813
814 /*
815 * Manual Edge Control Mode
816 *
817 * Edge auto strength bit is set by default.
818 * Remove it when manual mode.
819 */
820
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300821 ret = ov772x_mask_set(client, DSPAUTO, EDGE_ACTRL, 0x00);
Kuninori Morimotodb6cbc82009-03-31 03:44:22 -0300822 if (ret < 0)
823 goto ov772x_set_fmt_error;
824
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300825 ret = ov772x_mask_set(client,
Guennadi Liakhovetski2891f372011-07-26 12:20:42 -0300826 EDGE_TRSHLD, OV772X_EDGE_THRESHOLD_MASK,
Kuninori Morimotodb6cbc82009-03-31 03:44:22 -0300827 priv->info->edgectrl.threshold);
828 if (ret < 0)
829 goto ov772x_set_fmt_error;
830
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300831 ret = ov772x_mask_set(client,
Guennadi Liakhovetski2891f372011-07-26 12:20:42 -0300832 EDGE_STRNGT, OV772X_EDGE_STRENGTH_MASK,
Kuninori Morimotodb6cbc82009-03-31 03:44:22 -0300833 priv->info->edgectrl.strength);
834 if (ret < 0)
835 goto ov772x_set_fmt_error;
836
837 } else if (priv->info->edgectrl.upper > priv->info->edgectrl.lower) {
838 /*
839 * Auto Edge Control Mode
840 *
841 * set upper and lower limit
842 */
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300843 ret = ov772x_mask_set(client,
Guennadi Liakhovetski2891f372011-07-26 12:20:42 -0300844 EDGE_UPPER, OV772X_EDGE_UPPER_MASK,
Kuninori Morimotodb6cbc82009-03-31 03:44:22 -0300845 priv->info->edgectrl.upper);
846 if (ret < 0)
847 goto ov772x_set_fmt_error;
848
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300849 ret = ov772x_mask_set(client,
Guennadi Liakhovetski2891f372011-07-26 12:20:42 -0300850 EDGE_LOWER, OV772X_EDGE_LOWER_MASK,
Kuninori Morimotodb6cbc82009-03-31 03:44:22 -0300851 priv->info->edgectrl.lower);
852 if (ret < 0)
853 goto ov772x_set_fmt_error;
854 }
855
856 /*
Kuninori Morimoto2941e812009-02-23 12:12:58 -0300857 * set size format
858 */
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300859 ret = ov772x_write_array(client, priv->win->regs);
Kuninori Morimoto2941e812009-02-23 12:12:58 -0300860 if (ret < 0)
861 goto ov772x_set_fmt_error;
862
863 /*
Kuninori Morimotocdce7c02009-02-23 12:12:58 -0300864 * set DSP_CTRL3
Kuninori Morimoto2941e812009-02-23 12:12:58 -0300865 */
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300866 val = priv->cfmt->dsp3;
Kuninori Morimotocdce7c02009-02-23 12:12:58 -0300867 if (val) {
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300868 ret = ov772x_mask_set(client,
Kuninori Morimotocdce7c02009-02-23 12:12:58 -0300869 DSP_CTRL3, UV_MASK, val);
870 if (ret < 0)
871 goto ov772x_set_fmt_error;
872 }
873
874 /*
875 * set COM3
876 */
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300877 val = priv->cfmt->com3;
Kuninori Morimoto05148912009-02-23 12:12:58 -0300878 if (priv->info->flags & OV772X_FLAG_VFLIP)
879 val |= VFLIP_IMG;
880 if (priv->info->flags & OV772X_FLAG_HFLIP)
881 val |= HFLIP_IMG;
882 if (priv->flag_vflip)
883 val ^= VFLIP_IMG;
884 if (priv->flag_hflip)
885 val ^= HFLIP_IMG;
886
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300887 ret = ov772x_mask_set(client,
Kuninori Morimoto05148912009-02-23 12:12:58 -0300888 COM3, SWAP_MASK | IMG_MASK, val);
Kuninori Morimoto2941e812009-02-23 12:12:58 -0300889 if (ret < 0)
890 goto ov772x_set_fmt_error;
891
892 /*
Kuninori Morimotocdce7c02009-02-23 12:12:58 -0300893 * set COM7
Kuninori Morimoto2941e812009-02-23 12:12:58 -0300894 */
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300895 val = priv->win->com7_bit | priv->cfmt->com7;
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300896 ret = ov772x_mask_set(client,
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300897 COM7, SLCT_MASK | FMT_MASK | OFMT_MASK,
Kuninori Morimotocdce7c02009-02-23 12:12:58 -0300898 val);
899 if (ret < 0)
900 goto ov772x_set_fmt_error;
Kuninori Morimoto2941e812009-02-23 12:12:58 -0300901
Guennadi Liakhovetskia813d012009-08-25 11:44:14 -0300902 /*
903 * set COM8
904 */
905 if (priv->band_filter) {
906 ret = ov772x_mask_set(client, COM8, BNDF_ON_OFF, 1);
907 if (!ret)
908 ret = ov772x_mask_set(client, BDBASE,
909 0xff, 256 - priv->band_filter);
910 if (ret < 0)
911 goto ov772x_set_fmt_error;
912 }
913
Guennadi Liakhovetskibf62e1d2009-08-25 11:46:42 -0300914 *width = priv->win->width;
915 *height = priv->win->height;
916
Kuninori Morimoto2941e812009-02-23 12:12:58 -0300917 return ret;
918
919ov772x_set_fmt_error:
920
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300921 ov772x_reset(client);
Kuninori Morimoto2941e812009-02-23 12:12:58 -0300922 priv->win = NULL;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300923 priv->cfmt = NULL;
Kuninori Morimoto2941e812009-02-23 12:12:58 -0300924
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -0300925 return ret;
926}
927
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300928static int ov772x_g_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
929{
930 a->c.left = 0;
931 a->c.top = 0;
932 a->c.width = VGA_WIDTH;
933 a->c.height = VGA_HEIGHT;
934 a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
935
936 return 0;
937}
938
939static int ov772x_cropcap(struct v4l2_subdev *sd, struct v4l2_cropcap *a)
940{
941 a->bounds.left = 0;
942 a->bounds.top = 0;
943 a->bounds.width = VGA_WIDTH;
944 a->bounds.height = VGA_HEIGHT;
945 a->defrect = a->bounds;
946 a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
947 a->pixelaspect.numerator = 1;
948 a->pixelaspect.denominator = 1;
949
950 return 0;
951}
952
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300953static int ov772x_g_fmt(struct v4l2_subdev *sd,
954 struct v4l2_mbus_framefmt *mf)
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300955{
Laurent Pinchartc4ce6d12010-07-30 17:24:54 -0300956 struct i2c_client *client = v4l2_get_subdevdata(sd);
Guennadi Liakhovetskic85bb692010-12-30 08:37:06 -0300957 struct ov772x_priv *priv = container_of(sd, struct ov772x_priv, subdev);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300958
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300959 if (!priv->win || !priv->cfmt) {
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300960 u32 width = VGA_WIDTH, height = VGA_HEIGHT;
961 int ret = ov772x_set_params(client, &width, &height,
Guennadi Liakhovetskiace6e972010-07-22 16:52:51 -0300962 V4L2_MBUS_FMT_YUYV8_2X8);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300963 if (ret < 0)
964 return ret;
965 }
966
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300967 mf->width = priv->win->width;
968 mf->height = priv->win->height;
969 mf->code = priv->cfmt->code;
970 mf->colorspace = priv->cfmt->colorspace;
971 mf->field = V4L2_FIELD_NONE;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300972
973 return 0;
974}
975
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300976static int ov772x_s_fmt(struct v4l2_subdev *sd,
977 struct v4l2_mbus_framefmt *mf)
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300978{
Laurent Pinchartc4ce6d12010-07-30 17:24:54 -0300979 struct i2c_client *client = v4l2_get_subdevdata(sd);
Guennadi Liakhovetskic85bb692010-12-30 08:37:06 -0300980 struct ov772x_priv *priv = container_of(sd, struct ov772x_priv, subdev);
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300981 int ret = ov772x_set_params(client, &mf->width, &mf->height,
982 mf->code);
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300983
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300984 if (!ret)
985 mf->colorspace = priv->cfmt->colorspace;
986
987 return ret;
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300988}
989
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300990static int ov772x_try_fmt(struct v4l2_subdev *sd,
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300991 struct v4l2_mbus_framefmt *mf)
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -0300992{
Guennadi Liakhovetskic85bb692010-12-30 08:37:06 -0300993 struct ov772x_priv *priv = container_of(sd, struct ov772x_priv, subdev);
Kuninori Morimotof82a8562008-12-29 06:04:44 -0300994 const struct ov772x_win_size *win;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300995 int i;
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -0300996
Kuninori Morimotof82a8562008-12-29 06:04:44 -0300997 /*
998 * select suitable win
999 */
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001000 win = ov772x_select_win(mf->width, mf->height);
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -03001001
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001002 mf->width = win->width;
1003 mf->height = win->height;
1004 mf->field = V4L2_FIELD_NONE;
1005
1006 for (i = 0; i < ARRAY_SIZE(ov772x_cfmts); i++)
1007 if (mf->code == ov772x_cfmts[i].code)
1008 break;
1009
1010 if (i == ARRAY_SIZE(ov772x_cfmts)) {
1011 /* Unsupported format requested. Propose either */
1012 if (priv->cfmt) {
1013 /* the current one or */
1014 mf->colorspace = priv->cfmt->colorspace;
1015 mf->code = priv->cfmt->code;
1016 } else {
1017 /* the default one */
1018 mf->colorspace = ov772x_cfmts[0].colorspace;
1019 mf->code = ov772x_cfmts[0].code;
1020 }
1021 } else {
1022 /* Also return the colorspace */
1023 mf->colorspace = ov772x_cfmts[i].colorspace;
1024 }
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -03001025
1026 return 0;
1027}
1028
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001029static int ov772x_video_probe(struct soc_camera_device *icd,
1030 struct i2c_client *client)
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -03001031{
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001032 struct ov772x_priv *priv = to_ov772x(client);
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -03001033 u8 pid, ver;
Kuninori Morimotoaeabc882008-12-18 11:05:49 -03001034 const char *devname;
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -03001035
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -03001036 /* We must have a parent by now. And it cannot be a wrong one. */
1037 BUG_ON(!icd->parent ||
1038 to_soc_camera_host(icd->parent)->nr != icd->iface);
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -03001039
1040 /*
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -03001041 * check and show product ID and manufacturer ID
1042 */
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001043 pid = i2c_smbus_read_byte_data(client, PID);
1044 ver = i2c_smbus_read_byte_data(client, VER);
Kuninori Morimotoaeabc882008-12-18 11:05:49 -03001045
1046 switch (VERSION(pid, ver)) {
1047 case OV7720:
1048 devname = "ov7720";
1049 priv->model = V4L2_IDENT_OV7720;
1050 break;
Kuninori Morimoto3cac2ca2008-12-18 11:07:11 -03001051 case OV7725:
1052 devname = "ov7725";
1053 priv->model = V4L2_IDENT_OV7725;
1054 break;
Kuninori Morimotoaeabc882008-12-18 11:05:49 -03001055 default:
Guennadi Liakhovetski85f8be62009-08-25 11:47:00 -03001056 dev_err(&client->dev,
Kuninori Morimotob90c0322008-12-01 09:44:48 -03001057 "Product ID error %x:%x\n", pid, ver);
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001058 return -ENODEV;
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -03001059 }
1060
Guennadi Liakhovetski85f8be62009-08-25 11:47:00 -03001061 dev_info(&client->dev,
Kuninori Morimotoaeabc882008-12-18 11:05:49 -03001062 "%s Product ID %0x:%0x Manufacturer ID %x:%x\n",
1063 devname,
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -03001064 pid,
1065 ver,
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001066 i2c_smbus_read_byte_data(client, MIDH),
1067 i2c_smbus_read_byte_data(client, MIDL));
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -03001068
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001069 return 0;
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -03001070}
1071
1072static struct soc_camera_ops ov772x_ops = {
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -03001073 .set_bus_param = ov772x_set_bus_param,
1074 .query_bus_param = ov772x_query_bus_param,
Kuninori Morimoto05148912009-02-23 12:12:58 -03001075 .controls = ov772x_controls,
1076 .num_controls = ARRAY_SIZE(ov772x_controls),
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001077};
1078
1079static struct v4l2_subdev_core_ops ov772x_subdev_core_ops = {
1080 .g_ctrl = ov772x_g_ctrl,
1081 .s_ctrl = ov772x_s_ctrl,
1082 .g_chip_ident = ov772x_g_chip_ident,
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -03001083#ifdef CONFIG_VIDEO_ADV_DEBUG
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001084 .g_register = ov772x_g_register,
1085 .s_register = ov772x_s_register,
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -03001086#endif
1087};
1088
Hans Verkuil3805f202010-05-08 17:55:00 -03001089static int ov772x_enum_fmt(struct v4l2_subdev *sd, unsigned int index,
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001090 enum v4l2_mbus_pixelcode *code)
1091{
Hans Verkuil3805f202010-05-08 17:55:00 -03001092 if (index >= ARRAY_SIZE(ov772x_cfmts))
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001093 return -EINVAL;
1094
1095 *code = ov772x_cfmts[index].code;
1096 return 0;
1097}
1098
Guennadi Liakhovetski00cf08f2011-07-26 12:25:07 -03001099static int ov772x_g_mbus_config(struct v4l2_subdev *sd,
1100 struct v4l2_mbus_config *cfg)
1101{
1102 struct i2c_client *client = v4l2_get_subdevdata(sd);
1103 struct soc_camera_device *icd = client->dev.platform_data;
1104 struct soc_camera_link *icl = to_soc_camera_link(icd);
1105
1106 cfg->flags = V4L2_MBUS_PCLK_SAMPLE_RISING | V4L2_MBUS_MASTER |
1107 V4L2_MBUS_VSYNC_ACTIVE_HIGH | V4L2_MBUS_HSYNC_ACTIVE_HIGH |
1108 V4L2_MBUS_DATA_ACTIVE_HIGH;
1109 cfg->type = V4L2_MBUS_PARALLEL;
1110 cfg->flags = soc_camera_apply_board_flags(icl, cfg);
1111
1112 return 0;
1113}
1114
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001115static struct v4l2_subdev_video_ops ov772x_subdev_video_ops = {
1116 .s_stream = ov772x_s_stream,
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001117 .g_mbus_fmt = ov772x_g_fmt,
1118 .s_mbus_fmt = ov772x_s_fmt,
1119 .try_mbus_fmt = ov772x_try_fmt,
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001120 .cropcap = ov772x_cropcap,
1121 .g_crop = ov772x_g_crop,
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001122 .enum_mbus_fmt = ov772x_enum_fmt,
Guennadi Liakhovetski00cf08f2011-07-26 12:25:07 -03001123 .g_mbus_config = ov772x_g_mbus_config,
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001124};
1125
1126static struct v4l2_subdev_ops ov772x_subdev_ops = {
1127 .core = &ov772x_subdev_core_ops,
1128 .video = &ov772x_subdev_video_ops,
1129};
1130
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -03001131/*
1132 * i2c_driver function
1133 */
1134
Hans Verkuilbef216b2008-10-30 10:57:14 -03001135static int ov772x_probe(struct i2c_client *client,
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001136 const struct i2c_device_id *did)
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -03001137{
1138 struct ov772x_priv *priv;
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001139 struct soc_camera_device *icd = client->dev.platform_data;
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -03001140 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001141 struct soc_camera_link *icl;
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -03001142 int ret;
1143
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001144 if (!icd) {
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001145 dev_err(&client->dev, "OV772X: missing soc-camera data!\n");
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001146 return -EINVAL;
1147 }
1148
1149 icl = to_soc_camera_link(icd);
Guennadi Liakhovetski0f448292009-12-11 11:31:35 -03001150 if (!icl || !icl->priv)
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -03001151 return -EINVAL;
1152
1153 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
1154 dev_err(&adapter->dev,
1155 "I2C-Adapter doesn't support "
1156 "I2C_FUNC_SMBUS_BYTE_DATA\n");
1157 return -EIO;
1158 }
1159
1160 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
1161 if (!priv)
1162 return -ENOMEM;
1163
Kuninori Morimoto3675c752010-01-06 18:42:16 +09001164 priv->info = icl->priv;
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001165
1166 v4l2_i2c_subdev_init(&priv->subdev, client, &ov772x_subdev_ops);
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -03001167
Guennadi Liakhovetskia0705b02009-08-25 11:46:17 -03001168 icd->ops = &ov772x_ops;
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -03001169
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001170 ret = ov772x_video_probe(icd, client);
Kuninori Morimoto77fe3d42008-12-29 06:04:37 -03001171 if (ret) {
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001172 icd->ops = NULL;
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -03001173 kfree(priv);
Kuninori Morimoto77fe3d42008-12-29 06:04:37 -03001174 }
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -03001175
1176 return ret;
1177}
1178
1179static int ov772x_remove(struct i2c_client *client)
1180{
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001181 struct ov772x_priv *priv = to_ov772x(client);
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001182 struct soc_camera_device *icd = client->dev.platform_data;
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -03001183
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001184 icd->ops = NULL;
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -03001185 kfree(priv);
1186 return 0;
1187}
1188
1189static const struct i2c_device_id ov772x_id[] = {
Kuninori Morimotoaeabc882008-12-18 11:05:49 -03001190 { "ov772x", 0 },
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -03001191 { }
1192};
1193MODULE_DEVICE_TABLE(i2c, ov772x_id);
1194
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -03001195static struct i2c_driver ov772x_i2c_driver = {
1196 .driver = {
1197 .name = "ov772x",
1198 },
1199 .probe = ov772x_probe,
1200 .remove = ov772x_remove,
1201 .id_table = ov772x_id,
1202};
1203
1204/*
1205 * module function
1206 */
1207
1208static int __init ov772x_module_init(void)
1209{
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -03001210 return i2c_add_driver(&ov772x_i2c_driver);
1211}
1212
1213static void __exit ov772x_module_exit(void)
1214{
1215 i2c_del_driver(&ov772x_i2c_driver);
1216}
1217
1218module_init(ov772x_module_init);
1219module_exit(ov772x_module_exit);
1220
1221MODULE_DESCRIPTION("SoC Camera driver for ov772x");
1222MODULE_AUTHOR("Kuninori Morimoto");
1223MODULE_LICENSE("GPL v2");