blob: 702e61a9c02b71cdb13c04414ba4a3991d01c68b [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>
24#include <media/v4l2-chip-ident.h>
25#include <media/v4l2-common.h>
26#include <media/soc_camera.h>
27#include <media/ov772x.h>
28
29/*
30 * register offset
31 */
32#define GAIN 0x00 /* AGC - Gain control gain setting */
33#define BLUE 0x01 /* AWB - Blue channel gain setting */
34#define RED 0x02 /* AWB - Red channel gain setting */
35#define GREEN 0x03 /* AWB - Green channel gain setting */
36#define COM1 0x04 /* Common control 1 */
37#define BAVG 0x05 /* U/B Average Level */
38#define GAVG 0x06 /* Y/Gb Average Level */
39#define RAVG 0x07 /* V/R Average Level */
40#define AECH 0x08 /* Exposure Value - AEC MSBs */
41#define COM2 0x09 /* Common control 2 */
42#define PID 0x0A /* Product ID Number MSB */
43#define VER 0x0B /* Product ID Number LSB */
44#define COM3 0x0C /* Common control 3 */
45#define COM4 0x0D /* Common control 4 */
46#define COM5 0x0E /* Common control 5 */
47#define COM6 0x0F /* Common control 6 */
48#define AEC 0x10 /* Exposure Value */
49#define CLKRC 0x11 /* Internal clock */
50#define COM7 0x12 /* Common control 7 */
51#define COM8 0x13 /* Common control 8 */
52#define COM9 0x14 /* Common control 9 */
53#define COM10 0x15 /* Common control 10 */
Kuninori Morimoto3cac2ca2008-12-18 11:07:11 -030054#define REG16 0x16 /* Register 16 */
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -030055#define HSTART 0x17 /* Horizontal sensor size */
56#define HSIZE 0x18 /* Horizontal frame (HREF column) end high 8-bit */
57#define VSTART 0x19 /* Vertical frame (row) start high 8-bit */
58#define VSIZE 0x1A /* Vertical sensor size */
59#define PSHFT 0x1B /* Data format - pixel delay select */
60#define MIDH 0x1C /* Manufacturer ID byte - high */
61#define MIDL 0x1D /* Manufacturer ID byte - low */
62#define LAEC 0x1F /* Fine AEC value */
63#define COM11 0x20 /* Common control 11 */
64#define BDBASE 0x22 /* Banding filter Minimum AEC value */
65#define DBSTEP 0x23 /* Banding filter Maximum Setp */
66#define AEW 0x24 /* AGC/AEC - Stable operating region (upper limit) */
67#define AEB 0x25 /* AGC/AEC - Stable operating region (lower limit) */
68#define VPT 0x26 /* AGC/AEC Fast mode operating region */
Kuninori Morimoto3cac2ca2008-12-18 11:07:11 -030069#define REG28 0x28 /* Register 28 */
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -030070#define HOUTSIZE 0x29 /* Horizontal data output size MSBs */
71#define EXHCH 0x2A /* Dummy pixel insert MSB */
72#define EXHCL 0x2B /* Dummy pixel insert LSB */
73#define VOUTSIZE 0x2C /* Vertical data output size MSBs */
74#define ADVFL 0x2D /* LSB of insert dummy lines in Vertical direction */
75#define ADVFH 0x2E /* MSG of insert dummy lines in Vertical direction */
76#define YAVE 0x2F /* Y/G Channel Average value */
77#define LUMHTH 0x30 /* Histogram AEC/AGC Luminance high level threshold */
78#define LUMLTH 0x31 /* Histogram AEC/AGC Luminance low level threshold */
79#define HREF 0x32 /* Image start and size control */
80#define DM_LNL 0x33 /* Dummy line low 8 bits */
81#define DM_LNH 0x34 /* Dummy line high 8 bits */
82#define ADOFF_B 0x35 /* AD offset compensation value for B channel */
83#define ADOFF_R 0x36 /* AD offset compensation value for R channel */
84#define ADOFF_GB 0x37 /* AD offset compensation value for Gb channel */
85#define ADOFF_GR 0x38 /* AD offset compensation value for Gr channel */
86#define OFF_B 0x39 /* Analog process B channel offset value */
87#define OFF_R 0x3A /* Analog process R channel offset value */
88#define OFF_GB 0x3B /* Analog process Gb channel offset value */
89#define OFF_GR 0x3C /* Analog process Gr channel offset value */
90#define COM12 0x3D /* Common control 12 */
91#define COM13 0x3E /* Common control 13 */
92#define COM14 0x3F /* Common control 14 */
93#define COM15 0x40 /* Common control 15*/
94#define COM16 0x41 /* Common control 16 */
95#define TGT_B 0x42 /* BLC blue channel target value */
96#define TGT_R 0x43 /* BLC red channel target value */
97#define TGT_GB 0x44 /* BLC Gb channel target value */
98#define TGT_GR 0x45 /* BLC Gr channel target value */
Kuninori Morimoto3cac2ca2008-12-18 11:07:11 -030099/* for ov7720 */
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -0300100#define LCC0 0x46 /* Lens correction control 0 */
101#define LCC1 0x47 /* Lens correction option 1 - X coordinate */
102#define LCC2 0x48 /* Lens correction option 2 - Y coordinate */
103#define LCC3 0x49 /* Lens correction option 3 */
104#define LCC4 0x4A /* Lens correction option 4 - radius of the circular */
105#define LCC5 0x4B /* Lens correction option 5 */
106#define LCC6 0x4C /* Lens correction option 6 */
Kuninori Morimoto3cac2ca2008-12-18 11:07:11 -0300107/* for ov7725 */
108#define LC_CTR 0x46 /* Lens correction control */
109#define LC_XC 0x47 /* X coordinate of lens correction center relative */
110#define LC_YC 0x48 /* Y coordinate of lens correction center relative */
111#define LC_COEF 0x49 /* Lens correction coefficient */
112#define LC_RADI 0x4A /* Lens correction radius */
113#define LC_COEFB 0x4B /* Lens B channel compensation coefficient */
114#define LC_COEFR 0x4C /* Lens R channel compensation coefficient */
115
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -0300116#define FIXGAIN 0x4D /* Analog fix gain amplifer */
117#define AREF0 0x4E /* Sensor reference control */
118#define AREF1 0x4F /* Sensor reference current control */
119#define AREF2 0x50 /* Analog reference control */
120#define AREF3 0x51 /* ADC reference control */
121#define AREF4 0x52 /* ADC reference control */
122#define AREF5 0x53 /* ADC reference control */
123#define AREF6 0x54 /* Analog reference control */
124#define AREF7 0x55 /* Analog reference control */
125#define UFIX 0x60 /* U channel fixed value output */
126#define VFIX 0x61 /* V channel fixed value output */
Kuninori Morimotob3d7b2a2008-12-01 09:44:51 -0300127#define AWBB_BLK 0x62 /* AWB option for advanced AWB */
128#define AWB_CTRL0 0x63 /* AWB control byte 0 */
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -0300129#define DSP_CTRL1 0x64 /* DSP control byte 1 */
130#define DSP_CTRL2 0x65 /* DSP control byte 2 */
131#define DSP_CTRL3 0x66 /* DSP control byte 3 */
132#define DSP_CTRL4 0x67 /* DSP control byte 4 */
Kuninori Morimotob3d7b2a2008-12-01 09:44:51 -0300133#define AWB_BIAS 0x68 /* AWB BLC level clip */
134#define AWB_CTRL1 0x69 /* AWB control 1 */
135#define AWB_CTRL2 0x6A /* AWB control 2 */
136#define AWB_CTRL3 0x6B /* AWB control 3 */
137#define AWB_CTRL4 0x6C /* AWB control 4 */
138#define AWB_CTRL5 0x6D /* AWB control 5 */
139#define AWB_CTRL6 0x6E /* AWB control 6 */
140#define AWB_CTRL7 0x6F /* AWB control 7 */
141#define AWB_CTRL8 0x70 /* AWB control 8 */
142#define AWB_CTRL9 0x71 /* AWB control 9 */
143#define AWB_CTRL10 0x72 /* AWB control 10 */
144#define AWB_CTRL11 0x73 /* AWB control 11 */
145#define AWB_CTRL12 0x74 /* AWB control 12 */
146#define AWB_CTRL13 0x75 /* AWB control 13 */
147#define AWB_CTRL14 0x76 /* AWB control 14 */
148#define AWB_CTRL15 0x77 /* AWB control 15 */
149#define AWB_CTRL16 0x78 /* AWB control 16 */
150#define AWB_CTRL17 0x79 /* AWB control 17 */
151#define AWB_CTRL18 0x7A /* AWB control 18 */
152#define AWB_CTRL19 0x7B /* AWB control 19 */
153#define AWB_CTRL20 0x7C /* AWB control 20 */
154#define AWB_CTRL21 0x7D /* AWB control 21 */
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -0300155#define GAM1 0x7E /* Gamma Curve 1st segment input end point */
156#define GAM2 0x7F /* Gamma Curve 2nd segment input end point */
157#define GAM3 0x80 /* Gamma Curve 3rd segment input end point */
158#define GAM4 0x81 /* Gamma Curve 4th segment input end point */
159#define GAM5 0x82 /* Gamma Curve 5th segment input end point */
160#define GAM6 0x83 /* Gamma Curve 6th segment input end point */
161#define GAM7 0x84 /* Gamma Curve 7th segment input end point */
162#define GAM8 0x85 /* Gamma Curve 8th segment input end point */
163#define GAM9 0x86 /* Gamma Curve 9th segment input end point */
164#define GAM10 0x87 /* Gamma Curve 10th segment input end point */
165#define GAM11 0x88 /* Gamma Curve 11th segment input end point */
166#define GAM12 0x89 /* Gamma Curve 12th segment input end point */
167#define GAM13 0x8A /* Gamma Curve 13th segment input end point */
168#define GAM14 0x8B /* Gamma Curve 14th segment input end point */
169#define GAM15 0x8C /* Gamma Curve 15th segment input end point */
170#define SLOP 0x8D /* Gamma curve highest segment slope */
171#define DNSTH 0x8E /* De-noise threshold */
172#define EDGE0 0x8F /* Edge enhancement control 0 */
173#define EDGE1 0x90 /* Edge enhancement control 1 */
174#define DNSOFF 0x91 /* Auto De-noise threshold control */
175#define EDGE2 0x92 /* Edge enhancement strength low point control */
176#define EDGE3 0x93 /* Edge enhancement strength high point control */
177#define MTX1 0x94 /* Matrix coefficient 1 */
178#define MTX2 0x95 /* Matrix coefficient 2 */
179#define MTX3 0x96 /* Matrix coefficient 3 */
180#define MTX4 0x97 /* Matrix coefficient 4 */
181#define MTX5 0x98 /* Matrix coefficient 5 */
182#define MTX6 0x99 /* Matrix coefficient 6 */
183#define MTX_CTRL 0x9A /* Matrix control */
184#define BRIGHT 0x9B /* Brightness control */
185#define CNTRST 0x9C /* Contrast contrast */
186#define CNTRST_CTRL 0x9D /* Contrast contrast center */
187#define UVAD_J0 0x9E /* Auto UV adjust contrast 0 */
188#define UVAD_J1 0x9F /* Auto UV adjust contrast 1 */
189#define SCAL0 0xA0 /* Scaling control 0 */
190#define SCAL1 0xA1 /* Scaling control 1 */
191#define SCAL2 0xA2 /* Scaling control 2 */
192#define FIFODLYM 0xA3 /* FIFO manual mode delay control */
193#define FIFODLYA 0xA4 /* FIFO auto mode delay control */
194#define SDE 0xA6 /* Special digital effect control */
195#define USAT 0xA7 /* U component saturation control */
196#define VSAT 0xA8 /* V component saturation control */
Kuninori Morimoto3cac2ca2008-12-18 11:07:11 -0300197/* for ov7720 */
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -0300198#define HUE0 0xA9 /* Hue control 0 */
199#define HUE1 0xAA /* Hue control 1 */
Kuninori Morimoto3cac2ca2008-12-18 11:07:11 -0300200/* for ov7725 */
201#define HUECOS 0xA9 /* Cosine value */
202#define HUESIN 0xAA /* Sine value */
203
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -0300204#define SIGN 0xAB /* Sign bit for Hue and contrast */
205#define DSPAUTO 0xAC /* DSP auto function ON/OFF control */
206
207/*
208 * register detail
209 */
210
211/* COM2 */
212#define SOFT_SLEEP_MODE 0x10 /* Soft sleep mode */
213 /* Output drive capability */
214#define OCAP_1x 0x00 /* 1x */
215#define OCAP_2x 0x01 /* 2x */
216#define OCAP_3x 0x02 /* 3x */
217#define OCAP_4x 0x03 /* 4x */
218
219/* COM3 */
220#define SWAP_MASK 0x38
221
222#define VFIMG_ON_OFF 0x80 /* Vertical flip image ON/OFF selection */
223#define HMIMG_ON_OFF 0x40 /* Horizontal mirror image ON/OFF selection */
224#define SWAP_RGB 0x20 /* Swap B/R output sequence in RGB mode */
225#define SWAP_YUV 0x10 /* Swap Y/UV output sequence in YUV mode */
226#define SWAP_ML 0x08 /* Swap output MSB/LSB */
227 /* Tri-state option for output clock */
228#define NOTRI_CLOCK 0x04 /* 0: Tri-state at this period */
229 /* 1: No tri-state at this period */
230 /* Tri-state option for output data */
231#define NOTRI_DATA 0x02 /* 0: Tri-state at this period */
232 /* 1: No tri-state at this period */
233#define SCOLOR_TEST 0x01 /* Sensor color bar test pattern */
234
235/* COM4 */
236 /* PLL frequency control */
237#define PLL_BYPASS 0x00 /* 00: Bypass PLL */
238#define PLL_4x 0x40 /* 01: PLL 4x */
239#define PLL_6x 0x80 /* 10: PLL 6x */
240#define PLL_8x 0xc0 /* 11: PLL 8x */
241 /* AEC evaluate window */
242#define AEC_FULL 0x00 /* 00: Full window */
243#define AEC_1p2 0x10 /* 01: 1/2 window */
244#define AEC_1p4 0x20 /* 10: 1/4 window */
245#define AEC_2p3 0x30 /* 11: Low 2/3 window */
246
247/* COM5 */
248#define AFR_ON_OFF 0x80 /* Auto frame rate control ON/OFF selection */
249#define AFR_SPPED 0x40 /* Auto frame rate control speed slection */
250 /* Auto frame rate max rate control */
251#define AFR_NO_RATE 0x00 /* No reduction of frame rate */
252#define AFR_1p2 0x10 /* Max reduction to 1/2 frame rate */
253#define AFR_1p4 0x20 /* Max reduction to 1/4 frame rate */
254#define AFR_1p8 0x30 /* Max reduction to 1/8 frame rate */
255 /* Auto frame rate active point control */
256#define AF_2x 0x00 /* Add frame when AGC reaches 2x gain */
257#define AF_4x 0x04 /* Add frame when AGC reaches 4x gain */
258#define AF_8x 0x08 /* Add frame when AGC reaches 8x gain */
259#define AF_16x 0x0c /* Add frame when AGC reaches 16x gain */
260 /* AEC max step control */
261#define AEC_NO_LIMIT 0x01 /* 0 : AEC incease step has limit */
262 /* 1 : No limit to AEC increase step */
263
264/* COM7 */
265 /* SCCB Register Reset */
266#define SCCB_RESET 0x80 /* 0 : No change */
267 /* 1 : Resets all registers to default */
268 /* Resolution selection */
269#define SLCT_MASK 0x40 /* Mask of VGA or QVGA */
270#define SLCT_VGA 0x00 /* 0 : VGA */
271#define SLCT_QVGA 0x40 /* 1 : QVGA */
272#define ITU656_ON_OFF 0x20 /* ITU656 protocol ON/OFF selection */
273 /* RGB output format control */
Kuninori Morimotocdce7c02009-02-23 12:12:58 -0300274#define FMT_MASK 0x0c /* Mask of color format */
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -0300275#define FMT_GBR422 0x00 /* 00 : GBR 4:2:2 */
276#define FMT_RGB565 0x04 /* 01 : RGB 565 */
277#define FMT_RGB555 0x08 /* 10 : RGB 555 */
278#define FMT_RGB444 0x0c /* 11 : RGB 444 */
279 /* Output format control */
Kuninori Morimotocdce7c02009-02-23 12:12:58 -0300280#define OFMT_MASK 0x03 /* Mask of output format */
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -0300281#define OFMT_YUV 0x00 /* 00 : YUV */
282#define OFMT_P_BRAW 0x01 /* 01 : Processed Bayer RAW */
283#define OFMT_RGB 0x02 /* 10 : RGB */
284#define OFMT_BRAW 0x03 /* 11 : Bayer RAW */
285
286/* COM8 */
287#define FAST_ALGO 0x80 /* Enable fast AGC/AEC algorithm */
288 /* AEC Setp size limit */
289#define UNLMT_STEP 0x40 /* 0 : Step size is limited */
290 /* 1 : Unlimited step size */
291#define BNDF_ON_OFF 0x20 /* Banding filter ON/OFF */
292#define AEC_BND 0x10 /* Enable AEC below banding value */
293#define AEC_ON_OFF 0x08 /* Fine AEC ON/OFF control */
294#define AGC_ON 0x04 /* AGC Enable */
295#define AWB_ON 0x02 /* AWB Enable */
296#define AEC_ON 0x01 /* AEC Enable */
297
298/* COM9 */
299#define BASE_AECAGC 0x80 /* Histogram or average based AEC/AGC */
300 /* Automatic gain ceiling - maximum AGC value */
301#define GAIN_2x 0x00 /* 000 : 2x */
302#define GAIN_4x 0x10 /* 001 : 4x */
303#define GAIN_8x 0x20 /* 010 : 8x */
Kuninori Morimotocdce7c02009-02-23 12:12:58 -0300304#define GAIN_16x 0x30 /* 011 : 16x */
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -0300305#define GAIN_32x 0x40 /* 100 : 32x */
306#define GAIN_64x 0x50 /* 101 : 64x */
307#define GAIN_128x 0x60 /* 110 : 128x */
308#define DROP_VSYNC 0x04 /* Drop VSYNC output of corrupt frame */
309#define DROP_HREF 0x02 /* Drop HREF output of corrupt frame */
310
311/* COM11 */
312#define SGLF_ON_OFF 0x02 /* Single frame ON/OFF selection */
313#define SGLF_TRIG 0x01 /* Single frame transfer trigger */
314
315/* EXHCH */
316#define VSIZE_LSB 0x04 /* Vertical data output size LSB */
317
318/* DSP_CTRL1 */
319#define FIFO_ON 0x80 /* FIFO enable/disable selection */
320#define UV_ON_OFF 0x40 /* UV adjust function ON/OFF selection */
321#define YUV444_2_422 0x20 /* YUV444 to 422 UV channel option selection */
322#define CLR_MTRX_ON_OFF 0x10 /* Color matrix ON/OFF selection */
323#define INTPLT_ON_OFF 0x08 /* Interpolation ON/OFF selection */
324#define GMM_ON_OFF 0x04 /* Gamma function ON/OFF selection */
325#define AUTO_BLK_ON_OFF 0x02 /* Black defect auto correction ON/OFF */
326#define AUTO_WHT_ON_OFF 0x01 /* White define auto correction ON/OFF */
327
328/* DSP_CTRL3 */
329#define UV_MASK 0x80 /* UV output sequence option */
330#define UV_ON 0x80 /* ON */
331#define UV_OFF 0x00 /* OFF */
332#define CBAR_MASK 0x20 /* DSP Color bar mask */
333#define CBAR_ON 0x20 /* ON */
334#define CBAR_OFF 0x00 /* OFF */
335
336/* HSTART */
337#define HST_VGA 0x23
338#define HST_QVGA 0x3F
339
340/* HSIZE */
341#define HSZ_VGA 0xA0
342#define HSZ_QVGA 0x50
343
344/* VSTART */
345#define VST_VGA 0x07
346#define VST_QVGA 0x03
347
348/* VSIZE */
349#define VSZ_VGA 0xF0
350#define VSZ_QVGA 0x78
351
352/* HOUTSIZE */
353#define HOSZ_VGA 0xA0
354#define HOSZ_QVGA 0x50
355
356/* VOUTSIZE */
357#define VOSZ_VGA 0xF0
358#define VOSZ_QVGA 0x78
359
360/*
Kuninori Morimotoaeabc882008-12-18 11:05:49 -0300361 * ID
362 */
363#define OV7720 0x7720
Kuninori Morimoto3cac2ca2008-12-18 11:07:11 -0300364#define OV7725 0x7721
Kuninori Morimotoaeabc882008-12-18 11:05:49 -0300365#define VERSION(pid, ver) ((pid<<8)|(ver&0xFF))
366
367/*
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -0300368 * struct
369 */
370struct regval_list {
371 unsigned char reg_num;
372 unsigned char value;
373};
374
375struct ov772x_color_format {
376 char *name;
377 __u32 fourcc;
Kuninori Morimotocdce7c02009-02-23 12:12:58 -0300378 u8 dsp3;
379 u8 com3;
380 u8 com7;
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -0300381};
382
383struct ov772x_win_size {
384 char *name;
385 __u32 width;
386 __u32 height;
387 unsigned char com7_bit;
388 const struct regval_list *regs;
389};
390
391struct ov772x_priv {
392 struct ov772x_camera_info *info;
393 struct i2c_client *client;
394 struct soc_camera_device icd;
395 const struct ov772x_color_format *fmt;
396 const struct ov772x_win_size *win;
Kuninori Morimotoaeabc882008-12-18 11:05:49 -0300397 int model;
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -0300398};
399
400#define ENDMARKER { 0xff, 0xff }
401
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -0300402/*
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -0300403 * register setting for window size
404 */
405static const struct regval_list ov772x_qvga_regs[] = {
406 { HSTART, HST_QVGA },
407 { HSIZE, HSZ_QVGA },
408 { VSTART, VST_QVGA },
409 { VSIZE, VSZ_QVGA },
410 { HOUTSIZE, HOSZ_QVGA },
411 { VOUTSIZE, VOSZ_QVGA },
412 ENDMARKER,
413};
414
415static const struct regval_list ov772x_vga_regs[] = {
416 { HSTART, HST_VGA },
417 { HSIZE, HSZ_VGA },
418 { VSTART, VST_VGA },
419 { VSIZE, VSZ_VGA },
420 { HOUTSIZE, HOSZ_VGA },
421 { VOUTSIZE, VOSZ_VGA },
422 ENDMARKER,
423};
424
425/*
426 * supported format list
427 */
428
429#define SETFOURCC(type) .name = (#type), .fourcc = (V4L2_PIX_FMT_ ## type)
430static const struct soc_camera_data_format ov772x_fmt_lists[] = {
431 {
432 SETFOURCC(YUYV),
433 .depth = 16,
434 .colorspace = V4L2_COLORSPACE_JPEG,
435 },
436 {
437 SETFOURCC(YVYU),
438 .depth = 16,
439 .colorspace = V4L2_COLORSPACE_JPEG,
440 },
441 {
442 SETFOURCC(UYVY),
443 .depth = 16,
444 .colorspace = V4L2_COLORSPACE_JPEG,
445 },
446 {
447 SETFOURCC(RGB555),
448 .depth = 16,
449 .colorspace = V4L2_COLORSPACE_SRGB,
450 },
451 {
452 SETFOURCC(RGB555X),
453 .depth = 16,
454 .colorspace = V4L2_COLORSPACE_SRGB,
455 },
456 {
457 SETFOURCC(RGB565),
458 .depth = 16,
459 .colorspace = V4L2_COLORSPACE_SRGB,
460 },
461 {
462 SETFOURCC(RGB565X),
463 .depth = 16,
464 .colorspace = V4L2_COLORSPACE_SRGB,
465 },
466};
467
468/*
469 * color format list
470 */
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -0300471static const struct ov772x_color_format ov772x_cfmts[] = {
Kuninori Morimoto2941e812009-02-23 12:12:58 -0300472 {
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -0300473 SETFOURCC(YUYV),
Kuninori Morimotocdce7c02009-02-23 12:12:58 -0300474 .dsp3 = 0x0,
475 .com3 = SWAP_YUV,
476 .com7 = OFMT_YUV,
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -0300477 },
478 {
479 SETFOURCC(YVYU),
Kuninori Morimotocdce7c02009-02-23 12:12:58 -0300480 .dsp3 = UV_ON,
481 .com3 = SWAP_YUV,
482 .com7 = OFMT_YUV,
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -0300483 },
484 {
485 SETFOURCC(UYVY),
Kuninori Morimotocdce7c02009-02-23 12:12:58 -0300486 .dsp3 = 0x0,
487 .com3 = 0x0,
488 .com7 = OFMT_YUV,
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -0300489 },
490 {
491 SETFOURCC(RGB555),
Kuninori Morimotocdce7c02009-02-23 12:12:58 -0300492 .dsp3 = 0x0,
493 .com3 = SWAP_RGB,
494 .com7 = FMT_RGB555 | OFMT_RGB,
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -0300495 },
496 {
497 SETFOURCC(RGB555X),
Kuninori Morimotocdce7c02009-02-23 12:12:58 -0300498 .dsp3 = 0x0,
499 .com3 = 0x0,
500 .com7 = FMT_RGB555 | OFMT_RGB,
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -0300501 },
502 {
503 SETFOURCC(RGB565),
Kuninori Morimotocdce7c02009-02-23 12:12:58 -0300504 .dsp3 = 0x0,
505 .com3 = SWAP_RGB,
506 .com7 = FMT_RGB565 | OFMT_RGB,
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -0300507 },
508 {
509 SETFOURCC(RGB565X),
Kuninori Morimotocdce7c02009-02-23 12:12:58 -0300510 .dsp3 = 0x0,
511 .com3 = 0x0,
512 .com7 = FMT_RGB565 | OFMT_RGB,
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -0300513 },
514};
515
516
517/*
518 * window size list
519 */
520#define VGA_WIDTH 640
521#define VGA_HEIGHT 480
522#define QVGA_WIDTH 320
523#define QVGA_HEIGHT 240
524#define MAX_WIDTH VGA_WIDTH
525#define MAX_HEIGHT VGA_HEIGHT
526
527static const struct ov772x_win_size ov772x_win_vga = {
528 .name = "VGA",
529 .width = VGA_WIDTH,
530 .height = VGA_HEIGHT,
531 .com7_bit = SLCT_VGA,
532 .regs = ov772x_vga_regs,
533};
534
535static const struct ov772x_win_size ov772x_win_qvga = {
536 .name = "QVGA",
537 .width = QVGA_WIDTH,
538 .height = QVGA_HEIGHT,
539 .com7_bit = SLCT_QVGA,
540 .regs = ov772x_qvga_regs,
541};
542
543
544/*
545 * general function
546 */
547
548static int ov772x_write_array(struct i2c_client *client,
549 const struct regval_list *vals)
550{
551 while (vals->reg_num != 0xff) {
552 int ret = i2c_smbus_write_byte_data(client,
553 vals->reg_num,
554 vals->value);
555 if (ret < 0)
556 return ret;
557 vals++;
558 }
559 return 0;
560}
561
562static int ov772x_mask_set(struct i2c_client *client,
563 u8 command,
564 u8 mask,
565 u8 set)
566{
567 s32 val = i2c_smbus_read_byte_data(client, command);
568 val &= ~mask;
569 val |= set;
570
571 return i2c_smbus_write_byte_data(client, command, val);
572}
573
574static int ov772x_reset(struct i2c_client *client)
575{
576 int ret = i2c_smbus_write_byte_data(client, COM7, SCCB_RESET);
577 msleep(1);
578 return ret;
579}
580
581/*
582 * soc_camera_ops function
583 */
584
585static int ov772x_init(struct soc_camera_device *icd)
586{
Kuninori Morimotob90c0322008-12-01 09:44:48 -0300587 struct ov772x_priv *priv = container_of(icd, struct ov772x_priv, icd);
588 int ret = 0;
589
590 if (priv->info->link.power) {
591 ret = priv->info->link.power(&priv->client->dev, 1);
592 if (ret < 0)
593 return ret;
594 }
595
596 if (priv->info->link.reset)
597 ret = priv->info->link.reset(&priv->client->dev);
598
599 return ret;
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -0300600}
601
602static int ov772x_release(struct soc_camera_device *icd)
603{
Kuninori Morimotob90c0322008-12-01 09:44:48 -0300604 struct ov772x_priv *priv = container_of(icd, struct ov772x_priv, icd);
605 int ret = 0;
606
607 if (priv->info->link.power)
608 ret = priv->info->link.power(&priv->client->dev, 0);
609
610 return ret;
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -0300611}
612
613static int ov772x_start_capture(struct soc_camera_device *icd)
614{
615 struct ov772x_priv *priv = container_of(icd, struct ov772x_priv, icd);
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -0300616
Kuninori Morimoto2941e812009-02-23 12:12:58 -0300617 if (!priv->win || !priv->fmt) {
618 dev_err(&icd->dev, "norm or win select error\n");
619 return -EPERM;
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -0300620 }
621
Kuninori Morimoto7dcb2122008-12-29 06:04:22 -0300622 dev_dbg(&icd->dev,
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -0300623 "format %s, win %s\n", priv->fmt->name, priv->win->name);
624
Kuninori Morimoto2941e812009-02-23 12:12:58 -0300625 return 0;
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -0300626}
627
628static int ov772x_stop_capture(struct soc_camera_device *icd)
629{
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -0300630 return 0;
631}
632
633static int ov772x_set_bus_param(struct soc_camera_device *icd,
634 unsigned long flags)
635{
636 return 0;
637}
638
639static unsigned long ov772x_query_bus_param(struct soc_camera_device *icd)
640{
641 struct ov772x_priv *priv = container_of(icd, struct ov772x_priv, icd);
Guennadi Liakhovetskibd73b362008-12-23 05:54:45 -0300642 struct soc_camera_link *icl = priv->client->dev.platform_data;
643 unsigned long flags = SOCAM_PCLK_SAMPLE_RISING | SOCAM_MASTER |
644 SOCAM_VSYNC_ACTIVE_HIGH | SOCAM_HSYNC_ACTIVE_HIGH |
Guennadi Liakhovetski2d9329f2009-02-23 12:12:58 -0300645 SOCAM_DATA_ACTIVE_HIGH | priv->info->buswidth;
Guennadi Liakhovetskibd73b362008-12-23 05:54:45 -0300646
647 return soc_camera_apply_sensor_flags(icl, flags);
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -0300648}
649
650static int ov772x_get_chip_id(struct soc_camera_device *icd,
Hans Verkuilaecde8b52008-12-30 07:14:19 -0300651 struct v4l2_dbg_chip_ident *id)
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -0300652{
Kuninori Morimotoaeabc882008-12-18 11:05:49 -0300653 struct ov772x_priv *priv = container_of(icd, struct ov772x_priv, icd);
654
655 id->ident = priv->model;
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -0300656 id->revision = 0;
657
658 return 0;
659}
660
661#ifdef CONFIG_VIDEO_ADV_DEBUG
662static int ov772x_get_register(struct soc_camera_device *icd,
Hans Verkuilaecde8b52008-12-30 07:14:19 -0300663 struct v4l2_dbg_register *reg)
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -0300664{
665 struct ov772x_priv *priv = container_of(icd, struct ov772x_priv, icd);
666 int ret;
667
Hans Verkuilaecde8b52008-12-30 07:14:19 -0300668 reg->size = 1;
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -0300669 if (reg->reg > 0xff)
670 return -EINVAL;
671
672 ret = i2c_smbus_read_byte_data(priv->client, reg->reg);
673 if (ret < 0)
674 return ret;
675
676 reg->val = (__u64)ret;
677
678 return 0;
679}
680
681static int ov772x_set_register(struct soc_camera_device *icd,
Hans Verkuilaecde8b52008-12-30 07:14:19 -0300682 struct v4l2_dbg_register *reg)
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -0300683{
684 struct ov772x_priv *priv = container_of(icd, struct ov772x_priv, icd);
685
686 if (reg->reg > 0xff ||
687 reg->val > 0xff)
688 return -EINVAL;
689
690 return i2c_smbus_write_byte_data(priv->client, reg->reg, reg->val);
691}
692#endif
693
Kuninori Morimotof82a8562008-12-29 06:04:44 -0300694static const struct ov772x_win_size*
695ov772x_select_win(u32 width, u32 height)
696{
697 __u32 diff;
698 const struct ov772x_win_size *win;
699
700 /* default is QVGA */
701 diff = abs(width - ov772x_win_qvga.width) +
702 abs(height - ov772x_win_qvga.height);
703 win = &ov772x_win_qvga;
704
705 /* VGA */
706 if (diff >
707 abs(width - ov772x_win_vga.width) +
708 abs(height - ov772x_win_vga.height))
709 win = &ov772x_win_vga;
710
711 return win;
712}
713
Guennadi Liakhovetskid8fac212008-12-01 09:45:21 -0300714static int ov772x_set_fmt(struct soc_camera_device *icd,
715 __u32 pixfmt,
716 struct v4l2_rect *rect)
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -0300717{
718 struct ov772x_priv *priv = container_of(icd, struct ov772x_priv, icd);
719 int ret = -EINVAL;
Kuninori Morimotocdce7c02009-02-23 12:12:58 -0300720 u8 val;
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -0300721 int i;
722
723 /*
724 * select format
725 */
726 priv->fmt = NULL;
727 for (i = 0; i < ARRAY_SIZE(ov772x_cfmts); i++) {
728 if (pixfmt == ov772x_cfmts[i].fourcc) {
729 priv->fmt = ov772x_cfmts + i;
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -0300730 break;
731 }
732 }
Kuninori Morimoto2941e812009-02-23 12:12:58 -0300733 if (!priv->fmt)
734 goto ov772x_set_fmt_error;
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -0300735
Kuninori Morimotof82a8562008-12-29 06:04:44 -0300736 /*
737 * select win
738 */
739 priv->win = ov772x_select_win(rect->width, rect->height);
740
Kuninori Morimoto2941e812009-02-23 12:12:58 -0300741 /*
742 * reset hardware
743 */
744 ov772x_reset(priv->client);
745
746 /*
Kuninori Morimoto2941e812009-02-23 12:12:58 -0300747 * set size format
748 */
749 ret = ov772x_write_array(priv->client, priv->win->regs);
750 if (ret < 0)
751 goto ov772x_set_fmt_error;
752
753 /*
Kuninori Morimotocdce7c02009-02-23 12:12:58 -0300754 * set DSP_CTRL3
Kuninori Morimoto2941e812009-02-23 12:12:58 -0300755 */
Kuninori Morimotocdce7c02009-02-23 12:12:58 -0300756 val = priv->fmt->dsp3;
757 if (val) {
758 ret = ov772x_mask_set(priv->client,
759 DSP_CTRL3, UV_MASK, val);
760 if (ret < 0)
761 goto ov772x_set_fmt_error;
762 }
763
764 /*
765 * set COM3
766 */
767 val = priv->fmt->com3;
Kuninori Morimoto2941e812009-02-23 12:12:58 -0300768 ret = ov772x_mask_set(priv->client,
Kuninori Morimotocdce7c02009-02-23 12:12:58 -0300769 COM3, SWAP_MASK, val);
Kuninori Morimoto2941e812009-02-23 12:12:58 -0300770 if (ret < 0)
771 goto ov772x_set_fmt_error;
772
773 /*
Kuninori Morimotocdce7c02009-02-23 12:12:58 -0300774 * set COM7
Kuninori Morimoto2941e812009-02-23 12:12:58 -0300775 */
Kuninori Morimotocdce7c02009-02-23 12:12:58 -0300776 val = priv->win->com7_bit | priv->fmt->com7;
777 ret = ov772x_mask_set(priv->client,
778 COM7, (SLCT_MASK | FMT_MASK | OFMT_MASK),
779 val);
780 if (ret < 0)
781 goto ov772x_set_fmt_error;
Kuninori Morimoto2941e812009-02-23 12:12:58 -0300782
783 return ret;
784
785ov772x_set_fmt_error:
786
787 ov772x_reset(priv->client);
788 priv->win = NULL;
789 priv->fmt = NULL;
790
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -0300791 return ret;
792}
793
Guennadi Liakhovetskid8fac212008-12-01 09:45:21 -0300794static int ov772x_try_fmt(struct soc_camera_device *icd,
795 struct v4l2_format *f)
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -0300796{
Kuninori Morimotof82a8562008-12-29 06:04:44 -0300797 struct v4l2_pix_format *pix = &f->fmt.pix;
798 const struct ov772x_win_size *win;
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -0300799
Kuninori Morimotof82a8562008-12-29 06:04:44 -0300800 /*
801 * select suitable win
802 */
803 win = ov772x_select_win(pix->width, pix->height);
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -0300804
Kuninori Morimotof82a8562008-12-29 06:04:44 -0300805 pix->width = win->width;
806 pix->height = win->height;
807 pix->field = V4L2_FIELD_NONE;
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -0300808
809 return 0;
810}
811
812static int ov772x_video_probe(struct soc_camera_device *icd)
813{
814 struct ov772x_priv *priv = container_of(icd, struct ov772x_priv, icd);
815 u8 pid, ver;
Kuninori Morimotoaeabc882008-12-18 11:05:49 -0300816 const char *devname;
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -0300817
818 /*
819 * We must have a parent by now. And it cannot be a wrong one.
820 * So this entire test is completely redundant.
821 */
822 if (!icd->dev.parent ||
823 to_soc_camera_host(icd->dev.parent)->nr != icd->iface)
824 return -ENODEV;
825
826 /*
827 * ov772x only use 8 or 10 bit bus width
828 */
829 if (SOCAM_DATAWIDTH_10 != priv->info->buswidth &&
830 SOCAM_DATAWIDTH_8 != priv->info->buswidth) {
831 dev_err(&icd->dev, "bus width error\n");
832 return -ENODEV;
833 }
834
835 icd->formats = ov772x_fmt_lists;
836 icd->num_formats = ARRAY_SIZE(ov772x_fmt_lists);
837
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -0300838 /*
839 * check and show product ID and manufacturer ID
840 */
841 pid = i2c_smbus_read_byte_data(priv->client, PID);
842 ver = i2c_smbus_read_byte_data(priv->client, VER);
Kuninori Morimotoaeabc882008-12-18 11:05:49 -0300843
844 switch (VERSION(pid, ver)) {
845 case OV7720:
846 devname = "ov7720";
847 priv->model = V4L2_IDENT_OV7720;
848 break;
Kuninori Morimoto3cac2ca2008-12-18 11:07:11 -0300849 case OV7725:
850 devname = "ov7725";
851 priv->model = V4L2_IDENT_OV7725;
852 break;
Kuninori Morimotoaeabc882008-12-18 11:05:49 -0300853 default:
Kuninori Morimotob90c0322008-12-01 09:44:48 -0300854 dev_err(&icd->dev,
855 "Product ID error %x:%x\n", pid, ver);
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -0300856 return -ENODEV;
857 }
858
859 dev_info(&icd->dev,
Kuninori Morimotoaeabc882008-12-18 11:05:49 -0300860 "%s Product ID %0x:%0x Manufacturer ID %x:%x\n",
861 devname,
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -0300862 pid,
863 ver,
864 i2c_smbus_read_byte_data(priv->client, MIDH),
865 i2c_smbus_read_byte_data(priv->client, MIDL));
866
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -0300867 return soc_camera_video_start(icd);
868}
869
870static void ov772x_video_remove(struct soc_camera_device *icd)
871{
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -0300872 soc_camera_video_stop(icd);
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -0300873}
874
875static struct soc_camera_ops ov772x_ops = {
876 .owner = THIS_MODULE,
877 .probe = ov772x_video_probe,
878 .remove = ov772x_video_remove,
879 .init = ov772x_init,
880 .release = ov772x_release,
881 .start_capture = ov772x_start_capture,
882 .stop_capture = ov772x_stop_capture,
Guennadi Liakhovetskid8fac212008-12-01 09:45:21 -0300883 .set_fmt = ov772x_set_fmt,
884 .try_fmt = ov772x_try_fmt,
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -0300885 .set_bus_param = ov772x_set_bus_param,
886 .query_bus_param = ov772x_query_bus_param,
887 .get_chip_id = ov772x_get_chip_id,
888#ifdef CONFIG_VIDEO_ADV_DEBUG
889 .get_register = ov772x_get_register,
890 .set_register = ov772x_set_register,
891#endif
892};
893
894/*
895 * i2c_driver function
896 */
897
Hans Verkuilbef216b2008-10-30 10:57:14 -0300898static int ov772x_probe(struct i2c_client *client,
899 const struct i2c_device_id *did)
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -0300900{
901 struct ov772x_priv *priv;
902 struct ov772x_camera_info *info;
903 struct soc_camera_device *icd;
904 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
905 int ret;
906
907 info = client->dev.platform_data;
908 if (!info)
909 return -EINVAL;
910
911 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
912 dev_err(&adapter->dev,
913 "I2C-Adapter doesn't support "
914 "I2C_FUNC_SMBUS_BYTE_DATA\n");
915 return -EIO;
916 }
917
918 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
919 if (!priv)
920 return -ENOMEM;
921
922 priv->info = info;
923 priv->client = client;
924 i2c_set_clientdata(client, priv);
925
926 icd = &priv->icd;
927 icd->ops = &ov772x_ops;
928 icd->control = &client->dev;
929 icd->width_max = MAX_WIDTH;
930 icd->height_max = MAX_HEIGHT;
931 icd->iface = priv->info->link.bus_id;
932
933 ret = soc_camera_device_register(icd);
934
Kuninori Morimoto77fe3d42008-12-29 06:04:37 -0300935 if (ret) {
936 i2c_set_clientdata(client, NULL);
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -0300937 kfree(priv);
Kuninori Morimoto77fe3d42008-12-29 06:04:37 -0300938 }
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -0300939
940 return ret;
941}
942
943static int ov772x_remove(struct i2c_client *client)
944{
945 struct ov772x_priv *priv = i2c_get_clientdata(client);
946
947 soc_camera_device_unregister(&priv->icd);
Kuninori Morimoto77fe3d42008-12-29 06:04:37 -0300948 i2c_set_clientdata(client, NULL);
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -0300949 kfree(priv);
950 return 0;
951}
952
953static const struct i2c_device_id ov772x_id[] = {
Kuninori Morimotoaeabc882008-12-18 11:05:49 -0300954 { "ov772x", 0 },
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -0300955 { }
956};
957MODULE_DEVICE_TABLE(i2c, ov772x_id);
958
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -0300959static struct i2c_driver ov772x_i2c_driver = {
960 .driver = {
961 .name = "ov772x",
962 },
963 .probe = ov772x_probe,
964 .remove = ov772x_remove,
965 .id_table = ov772x_id,
966};
967
968/*
969 * module function
970 */
971
972static int __init ov772x_module_init(void)
973{
Kuninori Morimoto08a66ae2008-10-24 15:43:07 -0300974 return i2c_add_driver(&ov772x_i2c_driver);
975}
976
977static void __exit ov772x_module_exit(void)
978{
979 i2c_del_driver(&ov772x_i2c_driver);
980}
981
982module_init(ov772x_module_init);
983module_exit(ov772x_module_exit);
984
985MODULE_DESCRIPTION("SoC Camera driver for ov772x");
986MODULE_AUTHOR("Kuninori Morimoto");
987MODULE_LICENSE("GPL v2");