/*
 *  /drivers/i2c/busses/i2c-wmt.c
 *
 *  Copyright (C) 2008 WonderMedia Technologies, Inc.
 *  Copyright (C) 2011 Tony Prisk
 *
 * 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/errno.h>
#include <linux/i2c.h>
#include <linux/i2c-id.h>

#include <mach/i2c.h>

static int wmt_i2c_valid_messages(struct i2c_msg msgs[], int num)
{
	int i;
	if ((num < 1) || (num > MAX_MESSAGES))
		return -EINVAL;

	for (i = 0; i < num; i++) {
		if (&msgs[i] == NULL)
			return -EINVAL;
		else if (msgs[i].buf == NULL)
			return -EINVAL;
	}
	return 1;
}

static int wmt_i2c_do_xfer(struct i2c_adapter *i2c_adap,
				struct i2c_msg msgs[],
				int num)
{
	int i;
	struct i2c_algo_wmt_data *adap;
	struct i2c_msg *pmsg = NULL;
	int ret = 0;

	adap = i2c_adap->algo_data;

	for (i = 0; ret >= 0 && i < num; i++) {
	int last = ((i + 1) == num);
	int restart = (i != 0);
	pmsg = &msgs[i];

	if (pmsg->flags & I2C_M_NOSTART)
		restart = 1;

	if (pmsg->flags & I2C_M_RD)
		ret = adap->read_msg(adap->priv_data, pmsg->addr, pmsg->buf, pmsg->len, restart, last);
	else
		ret = adap->write_msg(adap->priv_data, pmsg->addr, pmsg->buf, pmsg->len, restart, last);
	}


	if (ret < 0)
		return ret;
	else
		return i;
}

static int wmt_i2c_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg msgs[], int num)
{
	int ret;
	int i;

	ret = wmt_i2c_valid_messages(msgs, num);

	if (ret < 0)
		return ret;

	for (i = i2c_adap->retries; i >= 0; i--) {
		ret = wmt_i2c_do_xfer(i2c_adap, msgs, num);
		if (ret > 0)
			return ret;
		udelay(100);
	}
	printk(KERN_ERR "[i2c] failed after %i retries\n", i2c_adap->retries);
	return -EREMOTEIO;
}

static u32 wmt_i2c_functionality(struct i2c_adapter *i2c_adap)
{
	return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
}
/*
static int wmt_i2c_control(struct i2c_adapter *i2c_adap,
				unsigned int cmd,
				unsigned long arg)
{
	int ret = 0;
	struct i2c_algo_wmt_data *adap = i2c_adap->algo_data;

	switch(cmd) {
	case I2C_SET_STANDARD_MODE:
		adap->set_mode(adap->priv_data, I2C_STANDARD_MODE);
		break;
	case I2C_SET_FAST_MODE:
		adap->set_mode(adap->priv_data, I2C_FAST_MODE);
		break;
	default:
		ret = -EINVAL;
		break;
	}

	return ret;
}
*/
struct i2c_algorithm wmt_i2c_algorithm = {
	.master_xfer	= wmt_i2c_xfer,
	.functionality	= wmt_i2c_functionality
};

int wmt_i2c_add_bus(struct i2c_adapter *i2c_adap)
{
	i2c_adap->algo = &wmt_i2c_algorithm;
	i2c_add_adapter(i2c_adap);

	return 0;
}
EXPORT_SYMBOL(wmt_i2c_add_bus);

int wmt_i2c_del_bus(struct i2c_adapter *i2c_adap)
{
	int res;
	res = i2c_del_adapter(i2c_adap);
	if (res < 0)
		return res;

	return 0;
}
EXPORT_SYMBOL(wmt_i2c_del_bus);

static int __init wmt_i2c_algo_init(void)
{
	return 0;
}


module_init(wmt_i2c_algo_init);

MODULE_AUTHOR("Tony Prisk");
MODULE_DESCRIPTION("WMT I2C Algorithm driver");
MODULE_LICENSE("GPL");

