/*
 * OmniVision OV6630/OV6130 Camera Chip Support Code
 *
 * Copyright (c) 1999-2002 Mark McClelland <mark@alpha.dyndns.org>
 * http://alpha.dyndns.org/ov511/
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

/* This file is not a module yet */
#define __NO_VERSION__

#if defined(OUTSIDE_KERNEL)
	#if !defined(EXPORT_SYMTAB)
		#define EXPORT_SYMTAB
	#endif

	#include <linux/config.h>

	#if defined(CONFIG_MODVERSIONS) && !defined(MODVERSIONS)
		#define MODVERSIONS
	#endif

	#include <linux/version.h>

	#ifdef MODVERSIONS
		#include <linux/modversions.h>
	#endif
#else
	#include <linux/config.h>
	#include <linux/version.h>
#endif

#include <linux/slab.h>
#include "ov511.h"

/* These may not be correct!! They are only here to make the code compile. */
#define REG_GAIN		0x00	/* gain setting (5:0) */
#define REG_BLUE		0x01	/* blue channel balance */
#define REG_RED			0x02	/* red channel balance */
#define REG_SAT			0x03	/* saturation */
					/* 04 reserved */
#define REG_CNT			0x05	/* Y contrast */
#define REG_BRT			0x06	/* Y brightness */
					/* 08-0b reserved */
#define REG_BLUE_BIAS		0x0C	/* blue channel bias (5:0) */
#define REG_RED_BIAS		0x0D	/* read channel bias (5:0) */
#define REG_GAMMA_COEFF		0x0E	/* gamma settings */
#define REG_WB_RANGE		0x0F	/* AEC/ALC/S-AWB settings */
#define REG_EXP			0x10	/* manual exposure setting */

extern int debug;	/* Exists in ov511.c and ov518.c */

struct ov6x30 {
	struct ov_i2c *i2c;
	int auto_brt;
	int auto_exp;
	int backlight;
	int bandfilt;
	int mirror;
};

/* This chip is undocumented so many of these are guesses. OK=verified,
 * A=Added since 6620, U=unknown function (not a 6620 reg) */
static struct ovsensor_regvals regvalsNorm6x30[] = {
	/*OK*/	{ 0x12, 0x80 }, /* reset */
	/*00?*/	{ 0x11, 0x01 },
	/*OK*/	{ 0x03, 0x60 },
	/*0A?*/	{ 0x05, 0x7f }, /* For when autoadjust is off */
		{ 0x07, 0xa8 },
		/* The ratio of 0x0c and 0x0d  controls the white point */
	/*OK*/	{ 0x0c, 0x24 },
	/*OK*/	{ 0x0d, 0x24 },
	/*A*/	{ 0x0e, 0x20 },

//	/*24?*/	{ 0x12, 0x28 }, /* Enable AGC */
//		{ 0x12, 0x24 }, /* Enable AGC */

//	/*A*/	{ 0x13, 0x21 },
//	/*A*/	{ 0x13, 0x25 }, /* Tristate Y and UV busses */

//	/*04?*/	{ 0x14, 0x80 },
		/* 0x16: 0x06 helps frame stability with moving objects */
	/*03?*/	{ 0x16, 0x06 },
//	/*OK*/	{ 0x20, 0x30 }, /* Aperture correction enable */
		// 21 & 22? The suggested values look wrong. Go with default
	/*A*/	{ 0x23, 0xc0 },
	/*A*/	{ 0x25, 0x9a }, // Check this against default
//	/*OK*/	{ 0x26, 0xb2 }, /* BLC enable */

		/* 0x28: 0x05 Selects RGB format if RGB on */
//	/*04?*/	{ 0x28, 0x05 },
//	/*04?*/	{ 0x28, 0x45 }, // DEBUG: Tristate UV bus

	/*OK*/	{ 0x2a, 0x04 }, /* Disable framerate adjust */
//	/*OK*/	{ 0x2b, 0xac }, /* Framerate; Set 2a[7] first */
//	/*U*/	{ 0x2c, 0xa0 },
		{ 0x2d, 0x99 },
//	/*A*/	{ 0x33, 0x26 }, // Reserved bits on 6620
//	/*d2?*/	{ 0x34, 0x03 }, /* Max A/D range */
//	/*U*/	{ 0x36, 0x8f }, // May not be necessary
//	/*U*/	{ 0x37, 0x80 }, // May not be necessary
//	/*8b?*/	{ 0x38, 0x83 },
//	/*40?*/	{ 0x39, 0xc0 }, // 6630 adds bit 7
//		{ 0x3c, 0x39 }, /* Enable AEC mode changing */
//		{ 0x3c, 0x3c }, /* Change AEC mode */
//		{ 0x3c, 0x24 }, /* Disable AEC mode changing */
	/*OK*/	{ 0x3d, 0x80 },
//	/*A*/	{ 0x3f, 0x0e },
//	/*U*/	{ 0x40, 0x00 },
//	/*U*/	{ 0x41, 0x00 },
//	/*U*/	{ 0x42, 0x80 },
//	/*U*/	{ 0x43, 0x3f },
//	/*U*/	{ 0x44, 0x80 },
//	/*U*/	{ 0x45, 0x20 },
//	/*U*/	{ 0x46, 0x20 },
//	/*U*/	{ 0x47, 0x80 },
//	/*U*/	{ 0x48, 0x7f },
//	/*U*/	{ 0x49, 0x00 },

		/* These next two registers (0x4a, 0x4b) are undocumented. They
		 * control the color balance */
//	/*OK?*/	{ 0x4a, 0x80 }, // Check these
//	/*OK?*/	{ 0x4b, 0x80 },
//	/*U*/	{ 0x4c, 0xd0 }, 
	/*d2?*/	{ 0x4d, 0x10 }, /* This reduces noise a bit */
	/*c1?*/	{ 0x4e, 0x40 },
	/*04?*/	{ 0x4f, 0x07 },
//	/*U*/	{ 0x50, 0xff },
	/*U*/	{ 0x54, 0x23 },
//	/*U*/	{ 0x55, 0xff },
//	/*U*/	{ 0x56, 0x12 },
	/*U*/	{ 0x57, 0x81 },
//	/*U*/	{ 0x58, 0x75 },
	/*U*/	{ 0x59, 0x01 },
	/*U*/	{ 0x5a, 0x2c },
	/*U*/	{ 0x5b, 0x0f },
//	/*U*/	{ 0x5c, 0x10 },
		{ 0xff, 0xff },	/* END MARKER */
};

/* This initializes the OV6x30 sensor and relevant variables. */
static int
ov6x30_configure(struct usb_ov511 *ov)
{
	struct ov6x30 *s;
	int rc;

	PDEBUG(4, "entered");

	ov->spriv = s = kmalloc(sizeof *s, GFP_KERNEL);
	if (!s)
		return -ENOMEM;
	memset(s, 0, sizeof *s);

	s->i2c = ov->i2c;
	s->auto_brt = 1;
	s->auto_exp = 1;

	/* Set sensor-specific vars */
	ov->maxwidth = 352;
	ov->maxheight = 288;
	ov->minwidth = 64;
	ov->minheight = 48;

	rc = ovsensor_write_regvals(ov, regvalsNorm6x30);

	return rc;
}

static int
ov6x30_set_control(struct usb_ov511 *ov, struct ovsensor_control *ctl)
{
	struct ov6x30 *s = ov->spriv;
	struct ov_i2c *i2c = s->i2c;
	int rc;
	int v = ctl->value;

	switch (ctl->id) {
	case OVSENSOR_CID_CONT:
		rc = i2c->w_mask(ov, REG_CNT, v >> 12, 0x0f);
		break;
	case OVSENSOR_CID_BRIGHT:
		rc = i2c->w(ov, REG_BRT, v >> 8);
		break;
	case OVSENSOR_CID_SAT:
		rc = i2c->w(ov, REG_SAT, v >> 8);
		break;
	case OVSENSOR_CID_HUE:
		rc = i2c->w(ov, REG_RED, 0xFF - (v >> 8));
		if (rc < 0)
			goto out;

		rc = i2c->w(ov, REG_BLUE, v >> 8);
		break;
	case OVSENSOR_CID_EXP:
		rc = i2c->w(ov, REG_EXP, v);
		break;
	case OVSENSOR_CID_FREQ:
	{
		int sixty = (v == 60);

		rc = i2c->w(ov, 0x2b, sixty?0xa8:0x28);
		if (rc < 0)
			goto out;

		rc = i2c->w(ov, 0x2a, sixty?0x84:0xa4);
		break;
	}
	case OVSENSOR_CID_BANDFILT:
		rc = i2c->w_mask(ov, 0x2d, v?0x04:0x00, 0x04);
		s->bandfilt = v;
		break;
	case OVSENSOR_CID_AUTOBRIGHT:
		rc = i2c->w_mask(ov, 0x2d, v?0x10:0x00, 0x10);
		s->auto_brt = v;
		break;
	case OVSENSOR_CID_AUTOEXP:
		rc = i2c->w_mask(ov, 0x28, v?0x00:0x10, 0x10);
		s->auto_exp = v;
		break;
	case OVSENSOR_CID_BACKLIGHT:
	{
		rc = i2c->w_mask(ov, 0x4e, v?0x80:0x60, 0xe0);
		if (rc < 0)
			goto out;

		rc = i2c->w_mask(ov, 0x29, v?0x08:0x00, 0x08);
		if (rc < 0)
			goto out;

		rc = i2c->w_mask(ov, 0x28, v?0x02:0x00, 0x02);
		s->backlight = v;
		break;
	}
	case OVSENSOR_CID_MIRROR:
		rc = i2c->w_mask(ov, 0x12, v?0x40:0x00, 0x40);
		s->mirror = v;
		break;
	default:
		PDEBUG(2, "control not supported: %d", ctl->id);
		return -EPERM;
	}

out:
	PDEBUG(3, "id=%d, arg=%d, rc=%d", ctl->id, v, rc);
	return rc;
}

static int
ov6x30_get_control(struct usb_ov511 *ov, struct ovsensor_control *ctl)
{
	struct ov6x30 *s = ov->spriv;
	struct ov_i2c *i2c = s->i2c;
	int rc = 0;
	unsigned char val = 0;

	switch (ctl->id) {
	case OVSENSOR_CID_CONT:
		rc = i2c->r(ov, REG_CNT, &val);
		ctl->value = (val & 0x0f) << 12;
		break;
	case OVSENSOR_CID_BRIGHT:
		rc = i2c->r(ov, REG_BRT, &val);
		ctl->value = val << 8;
		break;
	case OVSENSOR_CID_SAT:
		rc = i2c->r(ov, REG_SAT, &val);
		ctl->value = val << 8;
		break;
	case OVSENSOR_CID_HUE:
		rc = i2c->r(ov, REG_BLUE, &val);
		ctl->value = val << 8;
		break;
	case OVSENSOR_CID_EXP:
		rc = i2c->r(ov, REG_EXP, &val);
		ctl->value = val;
		break;
	case OVSENSOR_CID_BANDFILT:
		ctl->value = s->bandfilt;
		break;
	case OVSENSOR_CID_AUTOBRIGHT:
		ctl->value = s->auto_brt;
		break;
	case OVSENSOR_CID_AUTOEXP:
		ctl->value = s->auto_exp;
		break;
	case OVSENSOR_CID_BACKLIGHT:
		ctl->value = s->backlight;
		break;
	case OVSENSOR_CID_MIRROR:
		ctl->value = s->mirror;
		break;
	default:
		PDEBUG(2, "control not supported: %d", ctl->id);
		return -EPERM;
	}

	PDEBUG(3, "id=%d, arg=%d, rc=%d", ctl->id, ctl->value, rc);
	return rc;
}

static int
ov6x30_command(struct usb_ov511 *ov, unsigned int cmd, void *arg)
{
	switch (cmd) {
	case OVSENSOR_CMD_S_CTRL:
		return ov6x30_set_control(ov, arg);
	case OVSENSOR_CMD_G_CTRL:
		return ov6x30_get_control(ov, arg);
	default:
		PDEBUG(2, "command not supported: %d", cmd);
		return -ENOIOCTLCMD;
	}
}

struct ovsensor_ops ov6x30_ops = {
	configure:	ov6x30_configure,
	command:	ov6x30_command,
};
