/*
 * OmniVision OV6620/OV6120 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 ov6x20 {
	int auto_brt;
	int auto_exp;
	int backlight;
	int bandfilt;
	int mirror;
};

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)
{
	struct ov6x20 *s;
	int rc;

	PDEBUG(4, "");

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

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

	rc = ov_write_regvals(&ov->internal_client, regvalsNorm6x20);

	return rc;
}

static int
ov6x20_unconfigure(struct usb_ov511 *ov)
{
	kfree(ov->spriv);
	return 0;
}

static int
ov6x20_set_control(struct usb_ov511 *ov, struct ovsensor_control *ctl)
{
	struct ov6x20 *s = ov->spriv;
	struct i2c_client *c= &ov->internal_client;
	int rc;
	int v = ctl->value;

	switch (ctl->id) {
	case OVSENSOR_CID_CONT:
		rc = ov_write(c, REG_CNT, v >> 8);
		break;
	case OVSENSOR_CID_BRIGHT:
		rc = ov_write(c, REG_BRT, v >> 8);
		break;
	case OVSENSOR_CID_SAT:
		rc = ov_write(c, REG_SAT, v >> 8);
		break;
	case OVSENSOR_CID_HUE:
		rc = ov_write(c, REG_RED, 0xFF - (v >> 8));
		if (rc < 0)
			goto out;

		rc = ov_write(c, REG_BLUE, v >> 8);
		break;
	case OVSENSOR_CID_EXP:
		rc = ov_write(c, REG_EXP, v);
		break;
	case OVSENSOR_CID_FREQ:
	{
		int sixty = (v == 60);

		rc = ov_write(c, 0x2b, sixty?0xa8:0x28);
		if (rc < 0)
			goto out;

		rc = ov_write(c, 0x2a, sixty?0x84:0xa4);
		break;
	}
	case OVSENSOR_CID_BANDFILT:
		rc = ov_write_mask(c, 0x2d, v?0x04:0x00, 0x04);
		s->bandfilt = v;
		break;
	case OVSENSOR_CID_AUTOBRIGHT:
		rc = ov_write_mask(c, 0x2d, v?0x10:0x00, 0x10);
		s->auto_brt = v;
		break;
	case OVSENSOR_CID_AUTOEXP:
		rc = ov_write_mask(c, 0x13, v?0x01:0x00, 0x01);
		s->auto_exp = v;
		break;
	case OVSENSOR_CID_BACKLIGHT:
	{
		rc = ov_write_mask(c, 0x4e, v?0xe0:0xc0, 0xe0);
		if (rc < 0)
			goto out;

		rc = ov_write_mask(c, 0x29, v?0x08:0x00, 0x08);
		if (rc < 0)
			goto out;

		rc = ov_write_mask(c, 0x0e, v?0x80:0x00, 0x80);
		s->backlight = v;
		break;
	}
	case OVSENSOR_CID_MIRROR:
		rc = ov_write_mask(c, 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
ov6x20_get_control(struct usb_ov511 *ov, struct ovsensor_control *ctl)
{
	struct ov6x20 *s = ov->spriv;
	struct i2c_client *c= &ov->internal_client;
	int rc = 0;
	unsigned char val = 0;

	switch (ctl->id) {
	case OVSENSOR_CID_CONT:
		rc = ov_read(c, REG_CNT, &val);
		ctl->value = val << 8;
		break;
	case OVSENSOR_CID_BRIGHT:
		rc = ov_read(c, REG_BRT, &val);
		ctl->value = val << 8;
		break;
	case OVSENSOR_CID_SAT:
		rc = ov_read(c, REG_SAT, &val);
		ctl->value = val << 8;
		break;
	case OVSENSOR_CID_HUE:
		rc = ov_read(c, REG_BLUE, &val);
		ctl->value = val << 8;
		break;
	case OVSENSOR_CID_EXP:
		rc = ov_read(c, 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
ov6x20_mode_init(struct usb_ov511 *ov, struct ovsensor_window *win, int qvga)
{
	struct i2c_client *c= &ov->internal_client;
	int clock;

	/******** QVGA-specific regs ********/

	ov_write(c, 0x14, qvga?0x24:0x04);

	/******** Palette-specific regs ********/

	if (win->format == VIDEO_PALETTE_GREY)
		ov_write_mask(c, 0x13, 0x20, 0x20);
	else
		ov_write_mask(c, 0x13, 0x00, 0x20);

	/******** Clock programming ********/

	/* The OV6620 needs special handling. This prevents the 
	 * severe banding that normally occurs */

	/* Clock down */
	ov_write(c, 0x2a, 0x04);

	if (ov->compress) {
//		clock = 0;    /* This ensures the highest frame rate */
		clock = 3;
	} else if (win->clockdiv == -1) {   /* If user didn't override it */
		clock = 3;    /* Gives better exposure time */
	} else {
		clock = win->clockdiv;
	}

	PDEBUG(4, "Setting clock divisor to %d", clock);

	ov_write(c, 0x11, clock);

	ov_write(c, 0x2a, 0x84);
	/* This next setting is critical. It seems to improve
	 * the gain or the contrast. The "reserved" bits seem
	 * to have some effect in this case. */
	ov_write(c, 0x2d, 0x85);

	return 0;
}

static int
ov6x20_set_window(struct usb_ov511 *ov, struct ovsensor_window *win)
{
	struct i2c_client *c= &ov->internal_client;
	int ret;
	int hwsbase, hwebase, vwsbase, vwebase, hwsize, vwsize; 
	int hoffset, voffset, hwscale = 0, vwscale = 0;

	hwsbase = 0x38;
	hwebase = 0x3a;
	vwsbase = 0x05;
	vwebase = 0x06;

	if (win->width > 176 && win->height > 144) {		/* CIF */
		ret = ov6x20_mode_init(ov, win, 0);
		if (ret < 0)
			return ret;
		hwscale = 1;
		vwscale = 1;  /* The datasheet says 0; it's wrong */
		hwsize = 352;
		vwsize = 288;
	} else if (win->width > 176 || win->height > 144) {
		err("Illegal dimensions");
		return -EINVAL;
	} else {						/* QCIF */
		ret = ov6x20_mode_init(ov, win, 1);
		if (ret < 0)
			return ret;
		hwsize = 176;
		vwsize = 144;
	}

	/* Center the window */
	hoffset = ((hwsize - win->width) / 2) >> hwscale;
	voffset = ((vwsize - win->height) / 2) >> vwscale;

	/* FIXME! - This needs to be changed to support 160x120 and 6620!!! */
	if (win->sub_flag) {
		ov_write(c, 0x17, hwsbase+(ov->subx>>hwscale));
		ov_write(c, 0x18, hwebase+((ov->subx+ov->subw)>>hwscale));
		ov_write(c, 0x19, vwsbase+(ov->suby>>vwscale));
		ov_write(c, 0x1a, vwebase+((ov->suby+ov->subh)>>vwscale));
	} else {
		ov_write(c, 0x17, hwsbase + hoffset);
		ov_write(c, 0x18, hwebase + hoffset + (hwsize>>hwscale));
		ov_write(c, 0x19, vwsbase + voffset);
		ov_write(c, 0x1a, vwebase + voffset + (vwsize>>vwscale));
	}

	return 0;
}

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

struct ovsensor_ops ov6x20_ops = {
	.configure =	ov6x20_configure,
	.unconfigure =	ov6x20_unconfigure,
	.command =	ov6x20_command,
};
