mantis_i2c.c 5.0 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
/*
	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 <asm/io.h>
#include <linux/ioport.h>
23 24 25 26 27 28 29 30 31
#include <linux/pci.h>
#include <linux/i2c.h>

#include "dmxdev.h"
#include "dvbdev.h"
#include "dvb_demux.h"
#include "dvb_frontend.h"
#include "dvb_net.h"

32
#include "mantis_common.h"
33 34
#include "mantis_reg.h"
#include "mantis_i2c.h"
35 36 37 38 39 40

#define I2C_HW_B_MANTIS		0x1c

static int mantis_ack_wait(struct mantis_pci *mantis)
{
	int rc = 0;
41
	u32 timeout = 0;
42

43 44 45
	if (wait_event_timeout(mantis->i2c_wq,
			       mantis->mantis_int_stat & MANTIS_INT_I2CDONE,
			       msecs_to_jiffies(50)) == -ERESTARTSYS) {
46

47
		dprintk(MANTIS_DEBUG, 1, "Master !I2CDONE");
48
		rc = -EREMOTEIO;
49
	}
50

51
	while (!(mantis->mantis_int_stat & MANTIS_INT_I2CRACK)) {
52
		dprintk(MANTIS_DEBUG, 1, "Waiting for Slave RACK");
53 54 55 56
		mantis->mantis_int_stat = mmread(MANTIS_INT_STAT);
		msleep(5);
		timeout++;
		if (timeout > 500) {
57
			dprintk(MANTIS_ERROR, 1, "Slave RACK Fail !");
58
			rc = -EREMOTEIO;
59 60 61
			break;
		}
	}
62
	udelay(350);
63 64 65 66 67 68 69 70

	return rc;
}

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

71
	dprintk(MANTIS_INFO, 0, "        %s:  Address=[0x%02x] <R>[ ",
72 73
		__func__, msg->addr);

74 75 76 77 78 79 80 81 82
	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;

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

	return 0;
}

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

103
	dprintk(MANTIS_INFO, 0, "        %s: Address=[0x%02x] <W>[ ",
104 105
		__func__, msg->addr);

106
	for (i = 0; i < msg->len; i++) {
107
		dprintk(MANTIS_INFO, 0, "%02x ", msg->buf[i]);
108 109 110 111 112 113 114 115
		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;

116
		mmwrite(MANTIS_INT_I2CDONE, MANTIS_INT_STAT);
117
		mmwrite(txd, MANTIS_I2CDATA_CTL);
118
		if (mantis_ack_wait(mantis) != 0) {
119
			dprintk(MANTIS_DEBUG, 1, "ACK failed<W>");
120
			return -EREMOTEIO;
121 122
		}
	}
123
	dprintk(MANTIS_INFO, 0, "]\n");
124 125 126 127 128 129 130 131 132 133

	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);
134
	mutex_lock(&mantis->i2c_lock);
135 136 137 138 139 140 141
	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)
142
			goto bail_out;
143
	}
144
	mutex_unlock(&mantis->i2c_lock);
145 146

	return num;
147 148 149 150

bail_out:
	mutex_unlock(&mantis->i2c_lock);
	return ret;
151 152 153 154 155 156 157 158 159 160 161 162 163 164
}

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,
};

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

169
	init_waitqueue_head(&mantis->i2c_wq);
170
	mutex_init(&mantis->i2c_lock);
171
	strncpy(i2c_adapter->name, "Mantis I2C", sizeof (i2c_adapter->name));
172 173
	i2c_set_adapdata(i2c_adapter, mantis);

174 175 176 177 178 179 180 181 182
	i2c_adapter->owner	= THIS_MODULE;
	i2c_adapter->class	= I2C_CLASS_TV_DIGITAL;
	i2c_adapter->algo	= &mantis_algo;
	i2c_adapter->algo_data	= NULL;
	i2c_adapter->id		= I2C_HW_B_MANTIS;
	i2c_adapter->timeout	= 500;
	i2c_adapter->retries	= 3;
	i2c_adapter->dev.parent	= &pdev->dev;

183
	mantis->i2c_rc		= i2c_add_adapter(i2c_adapter);
184 185 186
	if (mantis->i2c_rc < 0)
		return mantis->i2c_rc;

187
	dprintk(MANTIS_DEBUG, 1, "Initializing I2C ..");
188 189

	intstat = mmread(MANTIS_INT_STAT);
190
	intmask = mmread(MANTIS_INT_MASK);
191
	mmwrite(intstat, MANTIS_INT_STAT);
192
	mmwrite(intmask | MANTIS_INT_I2CDONE, MANTIS_INT_MASK);
193

194
	dprintk(MANTIS_DEBUG, 1, "Status=<%02x> Mask=<%02x>", intstat, intmask);
195 196 197

	return 0;
}
198
EXPORT_SYMBOL_GPL(mantis_i2c_init);
199 200 201

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