V4L/DVB (13699): [Mantis, MB86A16] Initial checkin: Mantis, MB86A16
[linux-2.6/btrfs-unstable.git] / drivers / media / dvb / mantis / mantis_i2c.c
blobcfecb344bb7afe1395149289a9c4ba2f87fd75c4
1 /*
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>
25 #include <asm/io.h>
26 #include <linux/ioport.h>
27 #include <asm/pgtable.h>
28 #include <asm/page.h>
29 #include "mantis_common.h"
31 #define I2C_HW_B_MANTIS 0x1c
33 static int mantis_ack_wait(struct mantis_pci *mantis)
35 int rc = 0;
37 if (wait_event_interruptible_timeout(mantis->i2c_wq,
38 mantis->mantis_int_stat & MANTIS_INT_I2CRACK,
39 msecs_to_jiffies(50)) == -ERESTARTSYS)
41 rc = -EREMOTEIO;
43 // Wait till we are done
44 while (mantis->mantis_int_stat & MANTIS_INT_I2CRACK){
45 if (mantis->mantis_int_stat & MANTIS_INT_I2CDONE) {
46 mantis->mantis_int_stat &= ~MANTIS_INT_I2CRACK;
47 // dprintk(verbose, MANTIS_DEBUG, 1, "SLAVE RACK 'ed .. Waiting for I2CDONE");
48 break;
52 if (mantis->mantis_int_stat & MANTIS_INT_I2CDONE) {
53 // dprintk(verbose, MANTIS_DEBUG, 1, "Mantis Int I2CDONE");
54 rc = 1;
57 mantis->mantis_int_stat &= ~MANTIS_INT_I2CDONE;
59 // ..
60 if (mantis->mantis_int_stat & MANTIS_INT_I2CRACK)
61 msleep_interruptible(10);
63 return rc;
66 static int mantis_i2c_read(struct mantis_pci *mantis, const struct i2c_msg *msg)
68 u32 rxd, i;
70 dprintk(verbose, MANTIS_DEBUG, 1, "Address=[0x%02x]", msg->addr);
71 for (i = 0; i < msg->len; i++) {
72 rxd = (msg->addr << 25) | (1 << 24)
73 | MANTIS_I2C_RATE_3
74 | MANTIS_I2C_STOP
75 | MANTIS_I2C_PGMODE;
77 if (i == (msg->len - 1))
78 rxd &= ~MANTIS_I2C_STOP;
80 mmwrite(rxd, MANTIS_I2CDATA_CTL);
81 if (mantis_ack_wait(mantis) < 0) {
82 dprintk(verbose, MANTIS_DEBUG, 1, "ACK failed<R>");
83 return -EIO;
85 rxd = mmread(MANTIS_I2CDATA_CTL);
86 msg->buf[i] = (u8)((rxd >> 8) & 0xFF);
87 dprintk(verbose, MANTIS_DEBUG, 1,
88 "Data<R[%d]>=[0x%02x]", i, msg->buf[i]);
90 msleep_interruptible(2);
93 return 0;
96 static int mantis_i2c_write(struct mantis_pci *mantis, const struct i2c_msg *msg)
98 int i;
99 u32 txd = 0;
101 dprintk(verbose, MANTIS_DEBUG, 1, "Address=[0x%02x]", msg->addr);
102 for (i = 0; i < msg->len; i++) {
103 dprintk(verbose, MANTIS_DEBUG, 1, "Data<W[%d]>=[0x%02x]", i, msg->buf[i]);
104 txd = (msg->addr << 25) | (msg->buf[i] << 8)
105 | MANTIS_I2C_RATE_3
106 | MANTIS_I2C_STOP
107 | MANTIS_I2C_PGMODE;
109 if (i == (msg->len - 1))
110 txd &= ~MANTIS_I2C_STOP;
112 mmwrite(txd, MANTIS_I2CDATA_CTL);
113 if (mantis_ack_wait(mantis) < 0) {
114 dprintk(verbose, MANTIS_DEBUG, 1, "ACK failed<W>");
115 return -1;
117 udelay(500);
120 return 0;
123 static int mantis_i2c_xfer(struct i2c_adapter *adapter, struct i2c_msg *msgs, int num)
125 int ret = 0, i;
126 struct mantis_pci *mantis;
128 mantis = i2c_get_adapdata(adapter);
129 for (i = 0; i < num; i++) {
130 if (msgs[i].flags & I2C_M_RD)
131 ret = mantis_i2c_read(mantis, &msgs[i]);
132 else
133 ret = mantis_i2c_write(mantis, &msgs[i]);
135 if (ret < 0)
136 return ret;
139 return num;
142 static u32 mantis_i2c_func(struct i2c_adapter *adapter)
144 return I2C_FUNC_SMBUS_EMUL;
147 static struct i2c_algorithm mantis_algo = {
148 .master_xfer = mantis_i2c_xfer,
149 .functionality = mantis_i2c_func,
152 static struct i2c_adapter mantis_i2c_adapter = {
153 .owner = THIS_MODULE,
154 .name = "Mantis I2C",
155 .id = I2C_HW_B_MANTIS,
156 .class = I2C_CLASS_TV_DIGITAL,
157 .algo = &mantis_algo,
160 int __devinit mantis_i2c_init(struct mantis_pci *mantis)
162 u32 intstat;
164 memcpy(&mantis->adapter, &mantis_i2c_adapter, sizeof (mantis_i2c_adapter));
165 i2c_set_adapdata(&mantis->adapter, mantis);
166 mantis->i2c_rc = i2c_add_adapter(&mantis->adapter);
167 if (mantis->i2c_rc < 0)
168 return mantis->i2c_rc;
170 dprintk(verbose, MANTIS_DEBUG, 1, "Initializing I2C ..");
172 // Clear all interrupts
173 intstat = mmread(MANTIS_INT_STAT);
174 mmwrite(intstat, MANTIS_INT_STAT);
176 mmwrite(mmread(MANTIS_INT_MASK) | MANTIS_INT_I2CDONE,
177 MANTIS_INT_MASK);
179 dprintk(verbose, MANTIS_DEBUG, 1, "[0x%08x/%08x]",
180 mmread(MANTIS_INT_STAT), mmread(MANTIS_INT_MASK));
182 return 0;
185 int __devexit mantis_i2c_exit(struct mantis_pci *mantis)
187 dprintk(verbose, MANTIS_DEBUG, 1, "Removing I2C adapter");
188 return i2c_del_adapter(&mantis->adapter);