blob: 45d9e6bd962844cd5562d657d9187e0d1561292e [file] [log] [blame]
Manu Abraham41e840b2009-12-02 21:57:10 -03001/*
2 Mantis PCI bridge driver
3
4 Copyright (C) 2005, 2006 Manu Abraham (abraham.manu@gmail.com)
5
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.
10
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.
15
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.
19*/
20
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"
30
31#define I2C_HW_B_MANTIS 0x1c
32
33static int mantis_ack_wait(struct mantis_pci *mantis)
34{
35 int rc = 0;
Manu Abrahamdf0cca12009-12-02 22:07:24 -030036 u32 timeout = 0;
Manu Abraham41e840b2009-12-02 21:57:10 -030037
38 if (wait_event_interruptible_timeout(mantis->i2c_wq,
Manu Abrahamdf0cca12009-12-02 22:07:24 -030039 mantis->mantis_int_stat & MANTIS_INT_I2CDONE,
40 msecs_to_jiffies(50)) == -ERESTARTSYS) {
Manu Abraham41e840b2009-12-02 21:57:10 -030041
Manu Abraham715d3412009-12-03 05:37:51 -030042 dprintk(verbose, MANTIS_DEBUG, 1, "Master !I2CDONE");
Manu Abraham41e840b2009-12-02 21:57:10 -030043 rc = -EREMOTEIO;
Manu Abrahamdf0cca12009-12-02 22:07:24 -030044 }
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);
48 msleep(5);
49 timeout++;
50 if (timeout > 500) {
51 dprintk(verbose, MANTIS_ERROR, 1, "Slave RACK Fail !");
52 rc = -EREMOTEIO;
Manu Abraham41e840b2009-12-02 21:57:10 -030053 break;
54 }
55 }
Manu Abrahamdf0cca12009-12-02 22:07:24 -030056 udelay(350);
Manu Abraham41e840b2009-12-02 21:57:10 -030057
58 return rc;
59}
60
61static int mantis_i2c_read(struct mantis_pci *mantis, const struct i2c_msg *msg)
62{
63 u32 rxd, i;
64
Manu Abraham715d3412009-12-03 05:37:51 -030065 dprintk(verbose, MANTIS_INFO, 0, " %s: Address=[0x%02x] <R>[ ",
66 __func__, msg->addr);
67
Manu Abraham41e840b2009-12-02 21:57:10 -030068 for (i = 0; i < msg->len; i++) {
69 rxd = (msg->addr << 25) | (1 << 24)
70 | MANTIS_I2C_RATE_3
71 | MANTIS_I2C_STOP
72 | MANTIS_I2C_PGMODE;
73
74 if (i == (msg->len - 1))
75 rxd &= ~MANTIS_I2C_STOP;
76
Manu Abrahamdf0cca12009-12-02 22:07:24 -030077 mmwrite(MANTIS_INT_I2CDONE, MANTIS_INT_STAT);
Manu Abraham41e840b2009-12-02 21:57:10 -030078 mmwrite(rxd, MANTIS_I2CDATA_CTL);
Manu Abrahamdf0cca12009-12-02 22:07:24 -030079 if (mantis_ack_wait(mantis) != 0) {
Manu Abraham41e840b2009-12-02 21:57:10 -030080 dprintk(verbose, MANTIS_DEBUG, 1, "ACK failed<R>");
Manu Abrahamdf0cca12009-12-02 22:07:24 -030081 return -EREMOTEIO;
Manu Abraham41e840b2009-12-02 21:57:10 -030082 }
83 rxd = mmread(MANTIS_I2CDATA_CTL);
84 msg->buf[i] = (u8)((rxd >> 8) & 0xFF);
Manu Abrahamdf0cca12009-12-02 22:07:24 -030085 dprintk(verbose, MANTIS_INFO, 0, "%02x ", msg->buf[i]);
Manu Abraham41e840b2009-12-02 21:57:10 -030086 }
Manu Abrahamdf0cca12009-12-02 22:07:24 -030087 dprintk(verbose, MANTIS_INFO, 0, "]\n");
Manu Abraham41e840b2009-12-02 21:57:10 -030088
89 return 0;
90}
91
92static int mantis_i2c_write(struct mantis_pci *mantis, const struct i2c_msg *msg)
93{
94 int i;
95 u32 txd = 0;
96
Manu Abraham715d3412009-12-03 05:37:51 -030097 dprintk(verbose, MANTIS_INFO, 0, " %s: Address=[0x%02x] <W>[ ",
98 __func__, msg->addr);
99
Manu Abraham41e840b2009-12-02 21:57:10 -0300100 for (i = 0; i < msg->len; i++) {
Manu Abrahamdf0cca12009-12-02 22:07:24 -0300101 dprintk(verbose, MANTIS_INFO, 0, "%02x ", msg->buf[i]);
Manu Abraham41e840b2009-12-02 21:57:10 -0300102 txd = (msg->addr << 25) | (msg->buf[i] << 8)
103 | MANTIS_I2C_RATE_3
104 | MANTIS_I2C_STOP
105 | MANTIS_I2C_PGMODE;
106
107 if (i == (msg->len - 1))
108 txd &= ~MANTIS_I2C_STOP;
109
Manu Abrahamdf0cca12009-12-02 22:07:24 -0300110 mmwrite(MANTIS_INT_I2CDONE, MANTIS_INT_STAT);
Manu Abraham41e840b2009-12-02 21:57:10 -0300111 mmwrite(txd, MANTIS_I2CDATA_CTL);
Manu Abrahamdf0cca12009-12-02 22:07:24 -0300112 if (mantis_ack_wait(mantis) != 0) {
Manu Abraham41e840b2009-12-02 21:57:10 -0300113 dprintk(verbose, MANTIS_DEBUG, 1, "ACK failed<W>");
Manu Abrahamdf0cca12009-12-02 22:07:24 -0300114 return -EREMOTEIO;
Manu Abraham41e840b2009-12-02 21:57:10 -0300115 }
Manu Abraham41e840b2009-12-02 21:57:10 -0300116 }
Manu Abrahamdf0cca12009-12-02 22:07:24 -0300117 dprintk(verbose, MANTIS_INFO, 0, "]\n");
Manu Abraham41e840b2009-12-02 21:57:10 -0300118
119 return 0;
120}
121
122static int mantis_i2c_xfer(struct i2c_adapter *adapter, struct i2c_msg *msgs, int num)
123{
124 int ret = 0, i;
125 struct mantis_pci *mantis;
126
127 mantis = i2c_get_adapdata(adapter);
Manu Abrahame2f67e42009-12-03 05:42:10 -0300128 mutex_lock(&mantis->i2c_lock);
Manu Abraham41e840b2009-12-02 21:57:10 -0300129 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]);
134
135 if (ret < 0)
Manu Abraham99d96e42009-12-04 04:40:16 -0300136 goto bail_out;
Manu Abraham41e840b2009-12-02 21:57:10 -0300137 }
Manu Abrahame2f67e42009-12-03 05:42:10 -0300138 mutex_unlock(&mantis->i2c_lock);
Manu Abraham41e840b2009-12-02 21:57:10 -0300139
140 return num;
Manu Abraham99d96e42009-12-04 04:40:16 -0300141
142bail_out:
143 mutex_unlock(&mantis->i2c_lock);
144 return ret;
Manu Abraham41e840b2009-12-02 21:57:10 -0300145}
146
147static u32 mantis_i2c_func(struct i2c_adapter *adapter)
148{
149 return I2C_FUNC_SMBUS_EMUL;
150}
151
152static struct i2c_algorithm mantis_algo = {
153 .master_xfer = mantis_i2c_xfer,
154 .functionality = mantis_i2c_func,
155};
156
157static struct i2c_adapter mantis_i2c_adapter = {
158 .owner = THIS_MODULE,
159 .name = "Mantis I2C",
160 .id = I2C_HW_B_MANTIS,
161 .class = I2C_CLASS_TV_DIGITAL,
162 .algo = &mantis_algo,
163};
164
165int __devinit mantis_i2c_init(struct mantis_pci *mantis)
166{
Manu Abrahamdf0cca12009-12-02 22:07:24 -0300167 u32 intstat, intmask;
Manu Abraham8154bad2009-12-03 05:46:06 -0300168 struct i2c_adapter *i2c_adapter = &mantis->adapter;
169 struct pci_dev *pdev = mantis->pdev;
Manu Abraham41e840b2009-12-02 21:57:10 -0300170
Manu Abrahame2f67e42009-12-03 05:42:10 -0300171 mutex_init(&mantis->i2c_lock);
Manu Abraham8154bad2009-12-03 05:46:06 -0300172 memcpy(i2c_adapter, &mantis_i2c_adapter, sizeof (mantis_i2c_adapter));
173 i2c_set_adapdata(i2c_adapter, mantis);
174
175 i2c_adapter->dev.parent = &pdev->dev;
176 mantis->i2c_rc = i2c_add_adapter(i2c_adapter);
Manu Abraham41e840b2009-12-02 21:57:10 -0300177 if (mantis->i2c_rc < 0)
178 return mantis->i2c_rc;
179
180 dprintk(verbose, MANTIS_DEBUG, 1, "Initializing I2C ..");
181
Manu Abraham41e840b2009-12-02 21:57:10 -0300182 intstat = mmread(MANTIS_INT_STAT);
Manu Abrahamdf0cca12009-12-02 22:07:24 -0300183 intmask = mmread(MANTIS_INT_MASK);
Manu Abraham41e840b2009-12-02 21:57:10 -0300184 mmwrite(intstat, MANTIS_INT_STAT);
Manu Abrahamdf0cca12009-12-02 22:07:24 -0300185 mmwrite(intmask | MANTIS_INT_I2CDONE, MANTIS_INT_MASK);
Manu Abraham41e840b2009-12-02 21:57:10 -0300186
Manu Abrahamdf0cca12009-12-02 22:07:24 -0300187 dprintk(verbose, MANTIS_DEBUG, 1, "[0x%08x/%08x]", intstat, intmask);
Manu Abraham41e840b2009-12-02 21:57:10 -0300188
189 return 0;
190}
191
192int __devexit mantis_i2c_exit(struct mantis_pci *mantis)
193{
194 dprintk(verbose, MANTIS_DEBUG, 1, "Removing I2C adapter");
195 return i2c_del_adapter(&mantis->adapter);
196}