/*
 * OmniVision OV6620/OV6120 Camera Chip Support Code
 *
 * Copyright (c) 1999-2002 Mark McClelland <mmcclell@bigfoot.com>
 * 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 "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 */

/* ******************** Common functions ************************ */

static int
ovsensor_write_regvals(struct usb_ov511 *ov, struct ovsensor_regvals *rvals)
{
	int rc;

	while (rvals->reg != 0xff) {
		rc = ov->i2c->w(ov, rvals->reg, rvals->val);
		if (rc < 0)
			return rc;
		rvals++;
	}

	return 0;
}

/* ************************************************************** */

static struct ovsensor_regvals regvalsNorm6x20[] = {
	{ 0x12, 0x80 }, /* reset */
	{ 0x11, 0x01 },
	{ 0x03, 0x60 },
	{ 0x05, 0x7f }, /* For when autoadjust is off */
	{ 0x07, 0xa8 },
	/* The ratio of 0x0c and 0x0d controls the white point */
	{ 0x0c, 0x24 },
	{ 0x0d, 0x24 },
	{ 0x0f, 0x15 }, /* COMS */
	{ 0x10, 0x75 }, /* AEC Exposure time */
	{ 0x12, 0x24 }, /* Enable AGC */
	{ 0x14, 0x04 },
	/* 0x16: 0x06 helps frame stability with moving objects */
	{ 0x16, 0x06 },
//	{ 0x20, 0x30 }, /* Aperture correction enable */
	{ 0x26, 0xb2 }, /* BLC enable */
	/* 0x28: 0x05 Selects RGB format if RGB on */
	{ 0x28, 0x05 },
	{ 0x2a, 0x04 }, /* Disable framerate adjust */
//	{ 0x2b, 0xac }, /* Framerate; Set 2a[7] first */
	{ 0x2d, 0x99 },
	{ 0x33, 0xa0 }, /* Color Procesing Parameter */
	{ 0x34, 0xd2 }, /* Max A/D range */
	{ 0x38, 0x8b },
	{ 0x39, 0x40 },

	{ 0x3c, 0x39 }, /* Enable AEC mode changing */
	{ 0x3c, 0x3c }, /* Change AEC mode */
	{ 0x3c, 0x24 }, /* Disable AEC mode changing */

	{ 0x3d, 0x80 },
	/* These next two registers (0x4a, 0x4b) are undocumented. They
	 * control the color balance */
	{ 0x4a, 0x80 },
	{ 0x4b, 0x80 },
	{ 0x4d, 0xd2 }, /* This reduces noise a bit */
	{ 0x4e, 0xc1 },
	{ 0x4f, 0x04 },
// Do 50-53 have any effect?
// Toggle 0x12[2] off and on here?
	{ 0xff, 0xff },	/* END MARKER */
};

/* This initializes the OV6x20 sensor and relevant variables. */
static int 
ov6x20_configure(struct usb_ov511 *ov)
{
	int rc;

	PDEBUG(4, "");
	
	/* Set sensor-specific vars */
	ov->maxwidth = 352;
	ov->maxheight = 288;
	ov->minwidth = 64;
	ov->minheight = 48;

	// FIXME: These do not match the actual settings yet
	ov->brightness = 0x80 << 8;
	ov->contrast = 0x80 << 8;
	ov->colour = 0x80 << 8;
	ov->hue = 0x80 << 8;

	rc = ovsensor_write_regvals(ov, regvalsNorm6x20);

	return rc;
}

static int
ov6x20_command(struct usb_ov511 *ov, int cmd, void *arg) {
	struct ov_i2c *i2c = ov->i2c;
	int rc;
	unsigned char val = 0;

	PDEBUG(3, "cmd=%d", cmd);

	switch (cmd) {
	case OVSENSOR_CMD_S_CONT:
		rc = i2c->w(ov, REG_CNT, *((unsigned short *)arg) >> 8);
		break;
	case OVSENSOR_CMD_G_CONT:
		rc = i2c->r(ov, REG_CNT, &val);
		*((unsigned short *)arg) = val << 8;
		break;
	case OVSENSOR_CMD_S_BRIGHT:
		rc = i2c->w(ov, REG_BRT, *((unsigned short *)arg) >> 8);
		break;
	case OVSENSOR_CMD_G_BRIGHT:
		rc = i2c->r(ov, REG_BRT, &val);
		*((unsigned short *)arg) = val << 8;
		break;
	case OVSENSOR_CMD_S_SAT:
		rc = i2c->w(ov, REG_SAT, *((unsigned short *)arg) >> 8);
		break;
	case OVSENSOR_CMD_G_SAT:
		rc = i2c->r(ov, REG_SAT, &val);
		*((unsigned short *)arg) = val << 8;
		break;
	case OVSENSOR_CMD_S_HUE:
		rc = i2c->w(ov, REG_RED, 
			    0xFF - (*((unsigned short *)arg) >> 8));
		if (rc < 0)
			goto out;

		rc = i2c->w(ov, REG_BLUE, *((unsigned short *)arg) >> 8);
		break;
	case OVSENSOR_CMD_G_HUE:
		rc = i2c->r(ov, REG_BLUE, &val);
		*((unsigned short *)arg) = val << 8;
		break;
	case OVSENSOR_CMD_S_EXP:
		rc = i2c->w(ov, REG_EXP, *((unsigned char *)arg));
		break;
	case OVSENSOR_CMD_G_EXP:
		rc = i2c->r(ov, REG_EXP, &val);
		*((unsigned char *)arg) = val;
		break;
	case OVSENSOR_CMD_S_FREQ:
	{
		int sixty = *(int *)arg == 60;

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

		rc = ov->i2c->w(ov, 0x2a, sixty?0x84:0xa4);

		break;
	}
	case OVSENSOR_CMD_BANDFILT:
		rc = ov->i2c->w_mask(ov, 0x2d, (*(int *)arg)?0x04:0x00, 0x04);
		break;
	case OVSENSOR_CMD_AUTOBRIGHT:
		rc = ov->i2c->w_mask(ov, 0x2d, (*(int *)arg)?0x10:0x00, 0x10);
		break;
	case OVSENSOR_CMD_AUTOEXP:
		rc = ov->i2c->w_mask(ov, 0x13, (*(int *)arg)?0x01:0x00, 0x01);
		break;
	case OVSENSOR_CMD_BACKLIGHT:
	{
		int enable = (*(int *)arg);

		rc = ov->i2c->w_mask(ov, 0x4e, enable?0xe0:0xc0, 0xe0);
		if (rc < 0)
			goto out;

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

		rc = ov->i2c->w_mask(ov, 0x0e, enable?0x80:0x00, 0x80);
		break;
	}
	case OVSENSOR_CMD_MIRROR:
		rc = ov->i2c->w_mask(ov, 0x12, (*(int *)arg)?0x40:0x00, 0x40);
		break;
	default:
		PDEBUG(2, "command not supported: %d", cmd);
		return -EPERM;
	}

out:
	return rc;
}

struct ovsensor_ops ov6x20_ops = {
	configure:	ov6x20_configure,
	command:	ov6x20_command,
};
