/*
* V4L2 SoC Camera driver for OmniVision OV6650 Camera Sensor
*
* Copyright (C) 2010 Janusz Krzysztofik <jkrzyszt@tis.icnet.pl>
*
* Based on OmniVision OV96xx Camera Driver
* Copyright (C) 2009 Marek Vasut <marek.vasut@gmail.com>
*
* Based on ov772x camera driver:
* Copyright (C) 2008 Renesas Solutions Corp.
* Kuninori Morimoto <morimoto.kuninori@renesas.com>
*
* Based on ov7670 and soc_camera_platform driver,
* Copyright 2006-7 Jonathan Corbet <corbet@lwn.net>
* Copyright (C) 2008 Magnus Damm
* Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de>
*
* Hardware specific bits initialy based on former work by Matt Callow
* drivers/media/video/omap/sensor_ov6650.c
* Copyright (C) 2006 Matt Callow
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*/
#include <linux/bitops.h>
#include <linux/delay.h>
#include <linux/i2c.h>
#include <linux/slab.h>
#include <media/soc_camera.h>
#include <media/v4l2-chip-ident.h>
/* Register definitions */
#define REG_GAIN 0x00 /* range 00 - 3F */
#define REG_BLUE 0x01
#define REG_RED 0x02
#define REG_SAT 0x03 /* [7:4] saturation [0:3] reserved */
#define REG_HUE 0x04 /* [7:6] rsrvd [5] hue en [4:0] hue */
#define REG_BRT 0x06
#define REG_PIDH 0x0a
#define REG_PIDL 0x0b
#define REG_AECH 0x10
#define REG_CLKRC 0x11 /* Data Format and Internal Clock */
/* [7:6] Input system clock (MHz)*/
/* 00=8, 01=12, 10=16, 11=24 */
/* [5:0]: Internal Clock Pre-Scaler */
#define REG_COMA 0x12 /* [7] Reset */
#define REG_COMB 0x13
#define REG_COMC 0x14
#define REG_COMD 0x15
#define REG_COML 0x16
#define REG_HSTRT 0x17
#define REG_HSTOP 0x18
#define REG_VSTRT 0x19
#define REG_VSTOP 0x1a
#define REG_PSHFT 0x1b
#define REG_MIDH 0x1c
#define REG_MIDL 0x1d
#define REG_HSYNS 0x1e
#define REG_HSYNE 0x1f
#define REG_COME 0x20
#define REG_YOFF 0x21
#define REG_UOFF 0x22
#define REG_VOFF 0x23
#define REG_AEW 0x24
#define REG_AEB 0x25
#define REG_COMF 0x26
#define REG_COMG 0x27
#define REG_COMH 0x28
#define REG_COMI 0x29
#define REG_FRARL 0x2b
#define REG_COMJ 0x2c
#define REG_COMK 0x2d
#define REG_AVGY 0x2e
#define REG_REF0 0x2f
#define REG_REF1 0x30
#define REG_REF2 0x31
#define REG_FRAJH 0x32
#define REG_FRAJL 0x33
#define REG_FACT 0x34
#define REG_L1AEC 0x35
#define REG_AVGU 0x36
#define REG_AVGV 0x37
#define REG_SPCB 0x60
#define REG_SPCC 0x61
#define REG_GAM1 0x62
#define REG_GAM2 0x63
#define REG_GAM3 0x64
#define REG_SPCD 0x65
#define REG_SPCE 0x68
#define REG_ADCL 0x69
#define REG_RMCO 0x6c
#define REG_GMCO 0x6d
#define REG_BMCO 0x6e
/* Register bits, values, etc. */
#define OV6650_PIDH 0x66 /* high byte of product ID number */
#define OV6650_PIDL 0x50 /* low byte of product ID number */
#define OV6650_MIDH 0x7F /* high byte of mfg ID */
#define OV6650_MIDL 0xA2 /* low byte of mfg ID */
#define DEF_GAIN 0x00
#define DEF_BLUE 0x80
#define DEF_RED 0x80
#define SAT_SHIFT 4
#define SAT_MASK (0xf << SAT_SHIFT)
#define SET_SAT(x) (((x) << SAT_SHIFT) & SAT_MASK)
#define HUE_EN BIT(5)
#define HUE_MASK 0x1f
#define DEF_HUE 0x10
#define SET_HUE(x) (HUE_EN | ((x) & HUE_MASK))
#define DEF_AECH 0x4D
#define CLKRC_6MHz 0x00
#define CLKRC_12MHz 0x40
#define CLKRC_16MHz 0x80
#define CLKRC_24MHz 0xc0
#define CLKRC_DIV_MASK 0x3f
#define GET_CLKRC_DIV(x) (((x) & CLKRC_DIV_MASK) + 1)
#define COMA_RESET BIT(7)
#define COMA_QCIF BIT(5)
#define COMA_RAW_RGB BIT(4)
#define COMA_RGB BIT(3)
#define COMA_BW BIT(2)
#define COMA_WORD_SWAP BIT(1)
#define COMA_BYTE_SWAP BIT(0)
#define DEF_COMA 0x00
#define COMB_FLIP_V BIT(7)
#define COMB_FLIP_H BIT(5)
#define COMB_BAND_FILTER BIT(4)
#define COMB_AWB BIT(2)
#define COMB_AGC BIT(1)
#define COMB_AEC BIT(0)
#define DEF_COMB 0x5f
#define COML_ONE_CHANNEL BIT(7)
#define DEF_HSTRT 0x24
#define DEF_HSTOP 0xd4
#define DEF_VSTRT 0x04
#define DEF_VSTOP 0x94
#define COMF_HREF_LOW BIT(4)
#define COMJ_PCLK_RISING BIT(4)
#define COMJ_VSYNC_HIGH BIT(0)
/* supported resolutions */
#define W_QCIF (DEF_HSTOP - DEF_HSTRT)
#define W_CIF (W_QCIF << 1)
#define H_QCIF (DEF_VSTOP - DEF_VSTRT)
#define H_CIF (H_QCIF << 1)
#define FRAME_RATE_MAX 30
struct ov6650_reg {
u8 reg;
u8 val;
};
struct ov6650 {
struct v4l2_subdev subdev;
int gain;
int blue;
int red;
int saturation;
int hue;
int brightness;
int exposure;
int gamma;
int aec;
bool vflip;
bool hflip;
bool awb;
bool agc;
bool half_scale; /* scale down output by 2 */
struct v4l2_rect rect; /* sensor cropping window */
unsigned long pclk_limit; /* from host */
unsigned long pclk_max; /* from resolution and format */
struct v4l2_fract tpf; /* as requested with s_parm */
enum v4l2_mbus_pixelcode code;
enum v4l2_colorspace colorspace;
};
static enum v4l2_mbus_pixelcode ov6650_codes[] = {
V4L2_MBUS_FMT_YUYV8_2X8,
V4L2_MBUS_FMT_UYVY8_2X8,
V4L2_MBUS_FMT_YVYU8_2X8,
V4L2_MBUS_FMT_VYUY8_2X8,
V4L2_MBUS_FMT_SBGGR8_1X8,
V4L2_MBUS_FMT_GREY8_1X8,
};
static const struct v4l2_queryctrl ov6650_controls[] = {
{
.id = V4L2_CID_AUTOGAIN,
.type = V4L2_CTRL_TYPE_BOOLEAN,
.name = "AGC",
.minimum = 0,
.maximum