mantis_i2c.c 4.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
/*
	Mantis PCI bridge driver

	Copyright (C) 2005, 2006 Manu Abraham (abraham.manu@gmail.com)

	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.
*/

#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <asm/io.h>
#include <linux/ioport.h>
#include <asm/pgtable.h>
#include <asm/page.h>
#include "mantis_common.h"

#define I2C_HW_B_MANTIS		0x1c

static int mantis_ack_wait(struct mantis_pci *mantis)
{
	int rc = 0;
36
	u32 timeout = 0;
37 38

	if (wait_event_interruptible_timeout(mantis->i2c_wq,
39 40
					     mantis->mantis_int_stat & MANTIS_INT_I2CDONE,
					     msecs_to_jiffies(50)) == -ERESTARTSYS) {
41

42
		dprintk(verbose, MANTIS_DEBUG, 1, "Master !I2CDONE");
43
		rc = -EREMOTEIO;
44 45 46 47 48 49 50 51 52
	}
	while (!(mantis->mantis_int_stat & MANTIS_INT_I2CRACK)) {
		dprintk(verbose, MANTIS_DEBUG, 1, "Waiting for Slave RACK");
		mantis->mantis_int_stat = mmread(MANTIS_INT_STAT);
		msleep(5);
		timeout++;
		if (timeout > 500) {
			dprintk(verbose, MANTIS_ERROR, 1, "Slave RACK Fail !");
			rc = -EREMOTEIO;
53 54 55
			break;
		}
	}
56
	udelay(350);
57 58 59 60 61 62 63 64

	return rc;
}

static int mantis_i2c_read(struct mantis_pci *mantis, const struct i2c_msg *msg)
{
	u32 rxd, i;

65 66 67
	dprintk(verbose, MANTIS_INFO, 0, "        %s:  Address=[0x%02x] <R>[ ",
		__func__, msg->addr);

68 69 70 71 72 73 74 75 76
	for (i = 0; i < msg->len; i++) {
		rxd = (msg->addr << 25) | (1 << 24)
					| MANTIS_I2C_RATE_3
					| MANTIS_I2C_STOP
					| MANTIS_I2C_PGMODE;

		if (i == (msg->len - 1))
			rxd &= ~MANTIS_I2C_STOP;

77
		mmwrite(MANTIS_INT_I2CDONE, MANTIS_INT_STAT);
78
		mmwrite(rxd, MANTIS_I2CDATA_CTL);
79
		if (mantis_ack_wait(mantis) != 0) {
80
			dprintk(verbose, MANTIS_DEBUG, 1, "ACK failed<R>");
81
			return -EREMOTEIO;
82 83 84
		}
		rxd = mmread(MANTIS_I2CDATA_CTL);
		msg->buf[i] = (u8)((rxd >> 8) & 0xFF);
85
		dprintk(verbose, MANTIS_INFO, 0, "%02x ", msg->buf[i]);
86
	}
87
	dprintk(verbose, MANTIS_INFO, 0, "]\n");
88 89 90 91 92 93 94 95 96

	return 0;
}

static int mantis_i2c_write(struct mantis_pci *mantis, const struct i2c_msg *msg)
{
	int i;
	u32 txd = 0;

97 98 99
	dprintk(verbose, MANTIS_INFO, 0, "        %s: Address=[0x%02x] <W>[ ",
		__func__, msg->addr);

100
	for (i = 0; i < msg->len; i++) {
101
		dprintk(verbose, MANTIS_INFO, 0, "%02x ", msg->buf[i]);
102 103 104 105 106 107 108 109
		txd = (msg->addr << 25) | (msg->buf[i] << 8)
					| MANTIS_I2C_RATE_3
					| MANTIS_I2C_STOP
					| MANTIS_I2C_PGMODE;

		if (i == (msg->len - 1))
			txd &= ~MANTIS_I2C_STOP;

110
		mmwrite(MANTIS_INT_I2CDONE, MANTIS_INT_STAT);
111
		mmwrite(txd, MANTIS_I2CDATA_CTL);
112
		if (mantis_ack_wait(mantis) != 0) {
113
			dprintk(verbose, MANTIS_DEBUG, 1, "ACK failed<W>");
114
			return -EREMOTEIO;
115 116
		}
	}
117
	dprintk(verbose, MANTIS_INFO, 0, "]\n");
118 119 120 121 122 123 124 125 126 127

	return 0;
}

static int mantis_i2c_xfer(struct i2c_adapter *adapter, struct i2c_msg *msgs, int num)
{
	int ret = 0, i;
	struct mantis_pci *mantis;

	mantis = i2c_get_adapdata(adapter);
128
	mutex_lock(&mantis->i2c_lock);
129 130 131 132 133 134 135 136 137
	for (i = 0; i < num; i++) {
		if (msgs[i].flags & I2C_M_RD)
			ret = mantis_i2c_read(mantis, &msgs[i]);
		else
			ret = mantis_i2c_write(mantis, &msgs[i]);

		if (ret < 0)
			return ret;
	}
138
	mutex_unlock(&mantis->i2c_lock);
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162

	return num;
}

static u32 mantis_i2c_func(struct i2c_adapter *adapter)
{
	return I2C_FUNC_SMBUS_EMUL;
}

static struct i2c_algorithm mantis_algo = {
	.master_xfer		= mantis_i2c_xfer,
	.functionality		= mantis_i2c_func,
};

static struct i2c_adapter mantis_i2c_adapter = {
	.owner			= THIS_MODULE,
	.name			= "Mantis I2C",
	.id			= I2C_HW_B_MANTIS,
	.class			= I2C_CLASS_TV_DIGITAL,
	.algo			= &mantis_algo,
};

int __devinit mantis_i2c_init(struct mantis_pci *mantis)
{
163
	u32 intstat, intmask;
164 165
	struct i2c_adapter *i2c_adapter = &mantis->adapter;
	struct pci_dev *pdev		= mantis->pdev;
166

167
	mutex_init(&mantis->i2c_lock);
168 169 170 171 172
	memcpy(i2c_adapter, &mantis_i2c_adapter, sizeof (mantis_i2c_adapter));
	i2c_set_adapdata(i2c_adapter, mantis);

	i2c_adapter->dev.parent = &pdev->dev;
	mantis->i2c_rc		= i2c_add_adapter(i2c_adapter);
173 174 175 176 177 178
	if (mantis->i2c_rc < 0)
		return mantis->i2c_rc;

	dprintk(verbose, MANTIS_DEBUG, 1, "Initializing I2C ..");

	intstat = mmread(MANTIS_INT_STAT);
179
	intmask = mmread(MANTIS_INT_MASK);
180
	mmwrite(intstat, MANTIS_INT_STAT);
181
	mmwrite(intmask | MANTIS_INT_I2CDONE, MANTIS_INT_MASK);
182

183
	dprintk(verbose, MANTIS_DEBUG, 1, "[0x%08x/%08x]", intstat, intmask);
184 185 186 187 188 189 190 191 192

	return 0;
}

int __devexit mantis_i2c_exit(struct mantis_pci *mantis)
{
	dprintk(verbose, MANTIS_DEBUG, 1, "Removing I2C adapter");
	return i2c_del_adapter(&mantis->adapter);
}