V4L/DVB (6181): cx88: auto-load rtc and ir receiver i2c modules for FusionHDTV5 RT...
[safe/jmp/linux-2.6] / drivers / media / video / ov7670.c
index 639fc23..2bc6bdc 100644 (file)
@@ -12,7 +12,6 @@
  */
 #include <linux/init.h>
 #include <linux/module.h>
-#include <linux/moduleparam.h>
 #include <linux/slab.h>
 #include <linux/delay.h>
 #include <linux/videodev.h>
@@ -165,6 +164,10 @@ MODULE_LICENSE("GPL");
 
 #define REG_GFIX       0x69    /* Fix gain control */
 
+#define REG_REG76      0x76    /* OV's name */
+#define   R76_BLKPCOR    0x80    /* Black pixel correction enable */
+#define   R76_WHTPCOR    0x40    /* White pixel correction enable */
+
 #define REG_RGB444     0x8c    /* RGB 444 control */
 #define   R444_ENABLE    0x02    /* Turn on RGB444, overrides 5x5 */
 #define   R444_RGBX      0x01    /* Empty nibble at end */
@@ -383,6 +386,13 @@ static struct regval_list ov7670_fmt_rgb444[] = {
        { 0xff, 0xff },
 };
 
+static struct regval_list ov7670_fmt_raw[] = {
+       { REG_COM7, COM7_BAYER },
+       { REG_COM13, 0x08 }, /* No gamma, magic rsvd bit */
+       { REG_COM16, 0x3d }, /* Edge enhancement, denoise */
+       { REG_REG76, 0xe1 }, /* Pix correction, magic rsvd */
+       { 0xff, 0xff },
+};
 
 
 
@@ -405,7 +415,10 @@ static int ov7670_read(struct i2c_client *c, unsigned char reg,
 static int ov7670_write(struct i2c_client *c, unsigned char reg,
                unsigned char value)
 {
-       return i2c_smbus_write_byte_data(c, reg, value);
+       int ret = i2c_smbus_write_byte_data(c, reg, value);
+       if (reg == REG_COM7 && (value & COM7_RESET))
+               msleep(2);  /* Wait for reset to run */
+       return ret;
 }
 
 
@@ -486,32 +499,39 @@ static struct ov7670_format_struct {
        __u32 pixelformat;
        struct regval_list *regs;
        int cmatrix[CMATRIX_LEN];
+       int bpp;   /* Bytes per pixel */
 } ov7670_formats[] = {
        {
                .desc           = "YUYV 4:2:2",
                .pixelformat    = V4L2_PIX_FMT_YUYV,
                .regs           = ov7670_fmt_yuv422,
                .cmatrix        = { 128, -128, 0, -34, -94, 128 },
+               .bpp            = 2,
        },
        {
                .desc           = "RGB 444",
                .pixelformat    = V4L2_PIX_FMT_RGB444,
                .regs           = ov7670_fmt_rgb444,
                .cmatrix        = { 179, -179, 0, -61, -176, 228 },
+               .bpp            = 2,
        },
        {
                .desc           = "RGB 565",
                .pixelformat    = V4L2_PIX_FMT_RGB565,
                .regs           = ov7670_fmt_rgb565,
                .cmatrix        = { 179, -179, 0, -61, -176, 228 },
+               .bpp            = 2,
+       },
+       {
+               .desc           = "Raw RGB Bayer",
+               .pixelformat    = V4L2_PIX_FMT_SBGGR8,
+               .regs           = ov7670_fmt_raw,
+               .cmatrix        = { 0, 0, 0, 0, 0, 0 },
+               .bpp            = 1
        },
 };
-#define N_OV7670_FMTS (sizeof(ov7670_formats)/sizeof(ov7670_formats[0]))
+#define N_OV7670_FMTS ARRAY_SIZE(ov7670_formats)
 
-/*
- * All formats we support are 2 bytes/pixel.
- */
-#define BYTES_PER_PIXEL 2
 
 /*
  * Then there is the issue of window sizes.  Try to capture the info here.
@@ -599,7 +619,7 @@ static struct ov7670_win_size {
        },
 };
 
-#define N_WIN_SIZES (sizeof(ov7670_win_sizes)/sizeof(ov7670_win_sizes[0]))
+#define N_WIN_SIZES (ARRAY_SIZE(ov7670_win_sizes))
 
 
 /*
@@ -688,7 +708,7 @@ static int ov7670_try_fmt(struct i2c_client *c, struct v4l2_format *fmt,
         */
        pix->width = wsize->width;
        pix->height = wsize->height;
-       pix->bytesperline = pix->width*BYTES_PER_PIXEL;
+       pix->bytesperline = pix->width*ov7670_formats[index].bpp;
        pix->sizeimage = pix->height*pix->bytesperline;
        return 0;
 }
@@ -702,12 +722,22 @@ static int ov7670_s_fmt(struct i2c_client *c, struct v4l2_format *fmt)
        struct ov7670_format_struct *ovfmt;
        struct ov7670_win_size *wsize;
        struct ov7670_info *info = i2c_get_clientdata(c);
-       unsigned char com7;
+       unsigned char com7, clkrc;
 
        ret = ov7670_try_fmt(c, fmt, &ovfmt, &wsize);
        if (ret)
                return ret;
        /*
+        * HACK: if we're running rgb565 we need to grab then rewrite
+        * CLKRC.  If we're *not*, however, then rewriting clkrc hoses
+        * the colors.
+        */
+       if (fmt->fmt.pix.pixelformat == V4L2_PIX_FMT_RGB565) {
+               ret = ov7670_read(c, REG_CLKRC, &clkrc);
+               if (ret)
+                       return ret;
+       }
+       /*
         * COM7 is a pain in the ass, it doesn't like to be read then
         * quickly written afterward.  But we have everything we need
         * to set it absolutely here, as long as the format-specific
@@ -726,7 +756,10 @@ static int ov7670_s_fmt(struct i2c_client *c, struct v4l2_format *fmt)
        if (wsize->regs)
                ret = ov7670_write_array(c, wsize->regs);
        info->fmt = ovfmt;
-       return 0;
+
+       if (fmt->fmt.pix.pixelformat == V4L2_PIX_FMT_RGB565 && ret == 0)
+               ret = ov7670_write(c, REG_CLKRC, clkrc);
+       return ret;
 }
 
 /*
@@ -1152,7 +1185,7 @@ static struct ov7670_control {
                .query = ov7670_q_hflip,
        },
 };
-#define N_CONTROLS (sizeof(ov7670_controls)/sizeof(ov7670_controls[0]))
+#define N_CONTROLS (ARRAY_SIZE(ov7670_controls))
 
 static struct ov7670_control *ov7670_find_control(__u32 id)
 {
@@ -1249,7 +1282,9 @@ static int ov7670_attach(struct i2c_adapter *adapter)
        ret = ov7670_detect(client);
        if (ret)
                goto out_free_info;
-       i2c_attach_client(client);
+       ret = i2c_attach_client(client);
+       if (ret)
+               goto out_free_info;
        return 0;
 
   out_free_info: