2 Mantis PCI bridge driver
4 Copyright (C) 2005, 2006 Manu Abraham (abraham.manu@gmail.com)
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include <linux/module.h>
22 #include <linux/moduleparam.h>
23 #include <linux/init.h>
24 #include <linux/delay.h>
26 #include <linux/ioport.h>
27 #include <asm/pgtable.h>
29 #include "mantis_common.h"
31 #define I2C_HW_B_MANTIS 0x1c
33 static int mantis_ack_wait(struct mantis_pci *mantis)
38 if (wait_event_interruptible_timeout(mantis->i2c_wq,
39 mantis->mantis_int_stat & MANTIS_INT_I2CDONE,
40 msecs_to_jiffies(50)) == -ERESTARTSYS) {
42 dprintk(verbose, MANTIS_DEBUG, 1, "Master !I2CDONE");
45 while (!(mantis->mantis_int_stat & MANTIS_INT_I2CRACK)) {
46 dprintk(verbose, MANTIS_DEBUG, 1, "Waiting for Slave RACK");
47 mantis->mantis_int_stat = mmread(MANTIS_INT_STAT);
51 dprintk(verbose, MANTIS_ERROR, 1, "Slave RACK Fail !");
61 static int mantis_i2c_read(struct mantis_pci *mantis, const struct i2c_msg *msg)
65 dprintk(verbose, MANTIS_INFO, 0, " %s: Address=[0x%02x] <R>[ ",
68 for (i = 0; i < msg->len; i++) {
69 rxd = (msg->addr << 25) | (1 << 24)
74 if (i == (msg->len - 1))
75 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>");
83 rxd = mmread(MANTIS_I2CDATA_CTL);
84 msg->buf[i] = (u8)((rxd >> 8) & 0xFF);
85 dprintk(verbose, MANTIS_INFO, 0, "%02x ", msg->buf[i]);
87 dprintk(verbose, MANTIS_INFO, 0, "]\n");
92 static int mantis_i2c_write(struct mantis_pci *mantis, const struct i2c_msg *msg)
97 dprintk(verbose, MANTIS_INFO, 0, " %s: Address=[0x%02x] <W>[ ",
100 for (i = 0; i < msg->len; i++) {
101 dprintk(verbose, MANTIS_INFO, 0, "%02x ", msg->buf[i]);
102 txd = (msg->addr << 25) | (msg->buf[i] << 8)
107 if (i == (msg->len - 1))
108 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>");
117 dprintk(verbose, MANTIS_INFO, 0, "]\n");
122 static int mantis_i2c_xfer(struct i2c_adapter *adapter, struct i2c_msg *msgs, int num)
125 struct mantis_pci *mantis;
127 mantis = i2c_get_adapdata(adapter);
128 mutex_lock(&mantis->i2c_lock);
129 for (i = 0; i < num; i++) {
130 if (msgs[i].flags & I2C_M_RD)
131 ret = mantis_i2c_read(mantis, &msgs[i]);
133 ret = mantis_i2c_write(mantis, &msgs[i]);
138 mutex_unlock(&mantis->i2c_lock);
143 static u32 mantis_i2c_func(struct i2c_adapter *adapter)
145 return I2C_FUNC_SMBUS_EMUL;
148 static struct i2c_algorithm mantis_algo = {
149 .master_xfer = mantis_i2c_xfer,
150 .functionality = mantis_i2c_func,
153 static struct i2c_adapter mantis_i2c_adapter = {
154 .owner = THIS_MODULE,
155 .name = "Mantis I2C",
156 .id = I2C_HW_B_MANTIS,
157 .class = I2C_CLASS_TV_DIGITAL,
158 .algo = &mantis_algo,
161 int __devinit mantis_i2c_init(struct mantis_pci *mantis)
163 u32 intstat, intmask;
164 struct i2c_adapter *i2c_adapter = &mantis->adapter;
165 struct pci_dev *pdev = mantis->pdev;
167 mutex_init(&mantis->i2c_lock);
168 memcpy(i2c_adapter, &mantis_i2c_adapter, sizeof (mantis_i2c_adapter));
169 i2c_set_adapdata(i2c_adapter, mantis);
171 i2c_adapter->dev.parent = &pdev->dev;
172 mantis->i2c_rc = i2c_add_adapter(i2c_adapter);
173 if (mantis->i2c_rc < 0)
174 return mantis->i2c_rc;
176 dprintk(verbose, MANTIS_DEBUG, 1, "Initializing I2C ..");
178 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);
183 dprintk(verbose, MANTIS_DEBUG, 1, "[0x%08x/%08x]", intstat, intmask);
188 int __devexit mantis_i2c_exit(struct mantis_pci *mantis)
190 dprintk(verbose, MANTIS_DEBUG, 1, "Removing I2C adapter");
191 return i2c_del_adapter(&mantis->adapter);